Virtual Addresses

When a program accesses memory, it does not know or care where the physical memory backing the address is stored. It knows it is up to the operating system to map the address to the right place and provide it with the data it wants.

We call the address the userspace program is using to access memory a virtual address. The virtual address consists of two parts; the page that the virtual address is in and an offset into that page.

Page

The entire address space is divided up into pages, so each possible address must reside within a page. The page part of the virtual address acts as an index into the page table.

Offset

A page has many bytes of memory inside it, so the last bits of the virtual address are called the offset which is the location difference between the address you want and the start of the page. You require enough bits in the offset to be able to get to any byte in the page. For a 4K page you require (4K == (4 * 1024) == 4096 == 212 ==) 12 bits. Remember that the smallest amount of memory that the operating system or hardware deals with is a page, so each of these 4096 addresses reside within a single page and are dealt with as "one".

Virtual Address Translation

Virtual address translation refers to the process of finding out which physical page maps to which virtual page.

When the operating system needs to find the actual memory behind a virtual address, it only deals with the page component of the virtual address (since the smallest amount of memory it will deal with is a page). It takes the page number and looks it up in the page table. This gives a pointer to a physical address, to which the offset from the virtual address is added, giving the actual location in system memory. If it doesn't exist in the page table then the process is trying to access memory that has not been allocated to it by the operating system and the access will not be allowed.

Figure 6-2. Virtual Address Translation