Introduction
If every program used real memory addresses, two programs could overwrite each other, a program would need to know where it is loaded, and a program larger than memory could not run at all. Virtual memory solves all three problems. Each program sees its own private, continuous address space, and the processor translates every address it uses into a real (physical) address on the fly. The operating system keeps the translation tables, and the hardware does the translating. This lesson shows how that translation works and how a small cache makes it fast.
How it works
Memory is cut into fixed-size pages, commonly 4 KB. A virtual address is split into two parts: the page number (the high bits) and the offset within the page (the low bits). Physical memory is cut into frames of the same size. A page table records, for every virtual page, which frame holds it, or that it is not in memory. To translate, the hardware looks up the page number, takes the frame number, and attaches the same offset.
PAGE = 256
page_table = {0: 5, 1: 2, 3: 7} # virtual page -> physical frame
for vaddr in [0x0010, 0x01FF, 0x0300, 0x0200]:
page, offset = divmod(vaddr, PAGE)
if page in page_table:
paddr = page_table[page] * PAGE + offset
print(hex(vaddr), "-> page", page, "offset", offset, "-> physical", hex(paddr))
else:
print(hex(vaddr), "-> page", page, "not in memory: page fault")If the page table says a page is not in memory, the hardware raises a page fault. The operating system then finds the page (on disk, or it creates an empty one), loads it into a free frame, updates the page table and restarts the instruction. If there is no free frame, it throws out another page, often the least recently used. This is how a program can use more memory than the computer has, at a price: a disk access takes millions of cycles.
There is a catch. The page table lives in memory, so a plain translation would need an extra memory access for every load and store, and often several, because tables are layered. The cure is a cache for translations, the TLB (translation lookaside buffer). It holds the 16 to 1024 most recent translations, is searched at the same time as the L1 cache, and hits more than 99% of the time because of locality. Only on a TLB miss does the hardware walk the page table.
tlb = [] # recently used pages, least recent first; room for 2
hits = 0
pages = [0, 1, 0, 0, 2, 1, 0]
for page in pages:
if page in tlb:
hits += 1
tlb.remove(page)
tlb.append(page)
print("page", page, ": TLB hit")
else:
print("page", page, ": TLB miss, read the page table")
if len(tlb) == 2:
tlb.pop(0)
tlb.append(page)
print("TLB hits:", hits, "of", len(pages))Page tables also hold permission bits: read, write, execute and user or kernel. A program that touches a page it may not use gets a fault, and that is how the operating system protects programs from each other and from itself.
Example
The program translates four virtual addresses with 256-byte pages. The page table maps pages 0, 1 and 3 to frames 5, 2 and 7, and page 2 is not in memory.
PAGE = 256
page_table = {0: 5, 1: 2, 3: 7} # virtual page -> physical frame
for vaddr in [0x0010, 0x01FF, 0x0300, 0x0200]:
page, offset = divmod(vaddr, PAGE)
if page in page_table:
paddr = page_table[page] * PAGE + offset
print(hex(vaddr), "-> page", page, "offset", offset, "-> physical", hex(paddr))
else:
print(hex(vaddr), "-> page", page, "not in memory: page fault")Line by line
PAGE = 256is the page size, andpage_tablemaps a virtual page number to a physical frame number.divmod(vaddr, PAGE)splits the address into the page number and the offset in one step.- If the page is in the table, the physical address is the frame number times the page size, plus the offset: the offset never changes.
- If the page is missing, the program reports a page fault, where a real system would run an operating-system handler.
Expected result
0x10 -> page 0 offset 16 -> physical 0x510
0x1ff -> page 1 offset 255 -> physical 0x2ff
0x300 -> page 3 offset 0 -> physical 0x700
0x200 -> page 2 not in memory: page fault
In the real world
Every modern operating system uses virtual memory. It lets two programs both use address 0x400000 without noticing each other, lets the system share one copy of a library between many programs, and lets a file be mapped into memory. When a computer runs short of RAM and slows down badly, it is swapping pages to disk: a page fault storm. Security features rely on it too: pages can be marked non-executable, and the Meltdown flaw was possible because of how kernel pages are mapped. Servers use huge pages of 2 MB to need fewer TLB entries.
Common mistakes
- Translating the whole address: only the page number is mapped; the offset is copied unchanged.
- Mixing up pages and frames: pages are virtual, frames are physical.
- Thinking a page fault is always an error. Most are normal events that the operating system handles.
- Forgetting that the TLB is a cache of the page table: it must be flushed or tagged when the table changes.
Best practices
- Use a power-of-two page size so the split is a shift and a mask.
- Write down the page size first, then the number of offset bits, before translating by hand.
- Keep the working set of a program small, so its pages stay in memory and the TLB stays effective.
Summary
Virtual memory gives every program its own address space. A virtual address is a page number and an offset, and the page table maps the page to a physical frame. A missing page causes a page fault, which the operating system handles, and the TLB caches recent translations so that most accesses are fast.
Quick check
Key takeaways
- Virtual memory gives every program its own address space and protects programs from each other.
- A virtual address is a page number plus an offset; the page table maps the page number to a frame.
- The TLB caches translations, so most accesses do not need to read the page table.
Practice problems for this lesson
Was this lesson clear?
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.