C8 · Lesson 47 · Virtual memory
Pages, page tables and the TLB
On a laptop, every program believes it has the whole address space to itself, starting at the same addresses, and none of them can read another's memory. That illusion is virtual memory: each address a program uses is translated, page by page, to a physical address in RAM, using a table the operating system keeps for each process. It gives isolation and protection, lets programs use more memory than is installed, and lets the OS share and move memory freely. Translation sits on every single load, store and instruction fetch, so it has to be fast, and that is the job of a small, specialized cache called the TLB.
- Every memory access needs a translation. A TLB hit gives it in a cycle; a miss means walking the page table, one memory access per level, before the real access can start.
- The curve is the average time per access against the TLB hit rate. Below about 99% the walks add up quickly.
- Change the number of page-table levels: 64-bit processors use 4 or 5 levels, so their misses cost even more.
Predict
A 32-bit machine uses 4 KiB pages and a single-level page table with 4-byte entries. How big is each process's page table?
4 MiB: 32-bit addresses with a 12-bit offset leave 20 bits of page number, pages, each with a 4-byte entry. For a hundred processes that's 400 MiB of tables, mostly describing memory the processes never use. Multi-level tables only build the parts in use.
Name it
Pages and translation
Memory is divided into pages, typically 4 KiB. A virtual address splits into a virtual page number (VPN) and a page offset; with 4 KiB pages the offset is the low 12 bits. The page table maps each VPN to a physical page number (PPN), and
so the offset passes through unchanged. Each entry also holds a valid bit and permissions (read, write, execute, user).
Page faults
An access to a page that isn't valid, or that breaks its permissions, traps to the operating system: a page fault. The OS might load the page from disk (millions of cycles), allocate a fresh zeroed page, copy a shared page on write, or end the program for a bad access.
Multi-level tables
RISC-V's Sv32 splits the 20-bit VPN into two 10-bit indices. The first indexes a 1024-entry root table, whose entry points to a second-level table of 1024 entries. A process using a little memory needs the root and a few second-level tables, a few pages instead of 4 MiB. The cost is a page-table walk of one memory access per level.
The TLB
The translation lookaside buffer caches recent translations, typically 32 to a few thousand entries, often fully associative. Its reach is entries × page size. With hit rate h, n levels and a memory access time M, the average time per access is about
Microcontrollers usually skip all of this: one physical address space and, at most, a memory protection unit that sets permissions on a few regions.
Worked example
Translate the virtual address 0x00403A2C with 4 KiB pages, where the page table maps VPN 0x00403 to PPN 0x001F2.
Show the solution
- Split the address. The offset is the low 12 bits, 0xA2C; the VPN is 0x00403.
- Look up the page. The page table maps VPN 0x00403 to PPN 0x001F2.
- Form the physical address.
Answer the physical address is 0x001F2A2C.
Worked example
A TLB hits 99% of the time in 1 cycle; memory takes 100 cycles and the page table has 2 levels. What is the average access time, and what would it be with no TLB?
Show the solution
- Average over hits and misses.
- With no TLB. Every access walks the table:
Answer 103 cycles, against 300 with no TLB.
Worked example
How big is a flat page table for 32-bit addresses, 4 KiB pages and 4-byte entries? How much does a two-level table (10, 10 and 12 bits) need for a process with 4 MiB of code at the bottom and one page of stack at the top?
Show the solution
- A flat table. per process, mostly describing unused space.
- The two-level root. The root table is 4 KiB.
- Second-level tables. Each covers 1024 pages, 4 MiB, so the code needs one and the stack one more.
- The total. That's 3 tables, 12 KiB in all.
- The trade. The levels cost an extra memory access per walk and save memory in proportion to how sparse the address space is.
Answer 4 MiB for the flat table, and 12 KiB for the two-level one.
Check
A program uses virtual address 01312D00, with 4 KiB pages.
(a)
What is its virtual page number?
(b)
What is its page offset?
Check
A virtual page maps to physical page 01001, and the page offset is A2C. What physical address results (4 KiB pages)?
Check
A machine has 36-bit virtual addresses, pages of bytes and 8-byte page-table entries.
(a)
How many pages does a single-level table have to describe?
(b)
How big is that table, in MiB?
Check
A TLB answers in 1 cycle and hits 0.99 of the time. Memory takes 60 cycles and the page table has 4 levels.
(a)
What is the average time per access?
(b)
How many times faster is that than walking the table on every access?
Check
A TLB has 64 entries, and pages are 4 KiB.
(a)
What is the TLB's reach?
(b)
How many different pages can a loop touch over and over without a TLB miss, at most?
Check
An access normally takes 100 ns, but 10 in a million accesses page-fault, costing 5 ms each. What is the average access time?
Write units the way you normally would: 1.5 ns, 1500 ps and 0.0015 µs are the same answer.
Check
A program stores to an address on a page whose entry is valid but marked read-only. What happens?
Check
Virtual addresses have 32 bits, pages are 4 KiB and entries 4 bytes.
(a)
How big is a flat page table?
(b)
How many entries fit in one page?
Check
A TLB hits 99% of the time in 1 cycle; memory takes 60 cycles and the page table has 4 levels.
(a)
What's the average access time?
(b)
And with no TLB?
Explain why
Why doesn't the page offset need to be translated?
Pages in virtual memory and in physical memory are the same size and both start at multiples of that size, so a virtual page maps onto a physical page as a whole block. A byte's position inside its page is therefore the same in both, and only the page number changes. That's why the hardware can send the offset bits straight through, and why, with small enough caches, it can start indexing the cache with the offset while the TLB is still translating the page number.
Practice
Keep practicing
11 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.