Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are same virtual address for different processes mapped to different physical addresses

I have taken a course about Operating System design and concept and now I am trying to study Linux kernel thoroughly. I have a question that I cannot get rid of. In modern operating systems each process has own virtual address space(VAS) (eg, 0 to 2^32-1 in 32-bit systems). This provides many advantages. But in the implementation I am confused at some points. Let me explain it by giving an example:

Let's say we have two processes p1, p2; p1 and p2 have their own VASes. An address 0x023f4a54 is mapped to different physical addresses(PA), how can it be? How is done this translation in this manner. I mean I know translation mechanism but I cannot understand that same address is mapped to different physical address when it comes different processes' address space.

0x023f4a54 in p1's VAS => PA 0x12321321
0x023f4a54 in p2's VAS => PA 0x23af2341 # (random addresses)
like image 724
dirtybit Avatar asked Aug 08 '10 18:08

dirtybit


People also ask

Can different virtual addresses map to the same physical address?

Yes, you can. There is nothing inherently wrong with mapping the same physical address to two or more different virtual addresses. This flexibility is one of the reason for the success of virtual memory.

Can two different processes have virtual memory addresses that reference the same physical memory addresses?

This is because both 0 to 4 MB and 2 GB to (2 GB + 4 MB) in the virtual address space are mapped to the same memory location (0 to 4 MB) in the physical address space. Hence, it is possible for two virtual addresses VA1 and VA2 to point to the same physical address PA.

Can two different virtual addresses map to the same physical address in Linux?

shmat() is a typical example of same physical address being mapped as two different virtual address in two different processes.

How are virtual memory addresses mapped to physical memory addresses?

To map virtual memory addresses to physical memory addresses, page tables are used. A page table consists of numerous page table entries (PTE). One memory page in a PTE contains data structures consisting of different sizes of 'words'.


1 Answers

A CPU that provides virtual memory lets you set up a mapping of the memory addresses as the CPU sees it to physical memory addresses , typically this is done by a harware unit called the MMU.

The OS kernel can program that MMU, typically not down to the individual addresses, but rather in units of pages (4096 bytes is common). This means the MMU can be programmed to translate e.g. virtual addresses 0x1000-0x2000 to be translated to physical address 0x20000-0x21000.

The OS keeps one set of these mapping per process, and before it schedules a process to run, it loads that mapping into the MMU before it switches control back to the process. This enables different mappings for different processes, and nothing stops those mappings from mapping the same virtual address to a different physical address.

All this is transparent as far as the program is concerned, it just executes instructions on the CPU, and as the CPU has been set to virtual memory mode (paged mode), every memory access is translated by the MMU before it goes out on the physical bus to the memory.

The actual implementation details are complicated, but here's some references that might provide more insight;

  • http://wiki.osdev.org/Paging
  • http://www.usenix.org/event/usenix99/full_papers/cranor/cranor.pdf
  • http://ptgmedia.pearsoncmg.com/images/0131453483/downloads/gorman_book.pdf
like image 199
nos Avatar answered Sep 28 '22 06:09

nos