Lesson 11 of 26 · 13 min read
TLBs and multi-level page tables
Eliminate extra memory reads per lookup with the Translation Lookaside Buffer (TLB), handle ASID context switches, and shrink gigabyte page tables into compact multi-level trees.

Course syllabus · lesson 11 of 26
In this lesson
A flat page table charges space for every possible virtual page and adds a lookup to every translated access. Two mechanisms solve different parts of that problem. A translation lookaside buffer caches recently used translations. A multi-level page table gives the kernel a way to omit whole ranges of entries where a process has no mappings. The table must still answer correctly on a cache miss.
Compare a TLB miss with a hit
Step 1 of 4
Miss on the first access
The MMU searches the TLB for virtual page 5 in the current address space. It finds no cached translation.
Follow a TLB lookup
A translation lookaside buffer (TLB) is a hardware cache of virtual-to-physical translations. A simplified entry holds a virtual page number (VPN), an address-space tag, a physical frame number (PFN), permissions, and a bit indicating whether the slot is usable. Implementations may also encode page size and other attributes. The slot's valid bit belongs to the cache entry. A missing cache entry conveys no conclusion about whether the address is mapped.
On a hit, the MMU matches the virtual page and address-space tag, checks access permissions, and combines the PFN with the original byte offset. The data then comes from the ordinary memory hierarchy. A hit saves page-table lookups, while the load or store still requires its own access. A translation can hit and still reject a write if the cached mapping allows reads only.
On a miss, a hardware walker can read the process's tables and fill the entry. In a software-managed design, a TLB miss traps to a privileged handler that finds the mapping and installs it. The handler's own code and data must remain accessible even when the requested translation is missing, for example through wired translations or regions accessible without the same TLB mechanism. After a successful refill, the memory access can proceed or retry, depending on the architecture. If the mapping is absent or permissions fail, the fault path handles the reason instead of returning a usable translation.
Use locality to estimate translation cost
A page-aligned array of 1024 four-byte integers fits exactly in one 4 KiB page. Start with an empty data TLB and consider only the array's loads. The first element misses, then the next 1023 loads hit as long as the entry remains cached. That follows from spatial locality because the addresses share one VPN. A second pass through the array illustrates temporal locality if its translation survives between passes.
For a TLB with 64 entries for 4 KiB pages, the nominal coverage, or TLB reach, is 64 * 4096 = 262,144 bytes = 256 KiB. Actual coverage can be smaller when translations contend for the same set or instruction and data accesses share capacity. Access order matters too. At a stride of 4096 bytes, each successive load uses a different VPN and consumes a new translation while fetching as little as one integer per page. If a loop visits 65 pages in the same sequence on an ideal fully associative 64-entry TLB with least-recently-used replacement, every reuse comes after 64 distinct other pages and misses. Using fewer bytes of each page does not help this pattern fit.
For a simple effective memory access time (EMAT) estimate, let h be the TLB hit probability, t the TLB lookup time, m the cost of a physical memory read, and L the number of page-table reads on a miss. Assume a serial path, no page faults, and a miss that completes after its walk without a second separately charged probe. Both paths include one data read.
Hit time = t + m
Miss time = t + L * m + m
EMAT = h * Hit time + (1 - h) * Miss time
= t + m + (1 - h) * L * m
Set t = 2 ns, m = 80 ns, L = 2, and h = 0.99. Then a hit takes 82 ns and a miss takes 242 ns, giving EMAT = 83.6 ns. At a hit rate of 90 percent the same model gives 98 ns. This calculation isolates translation overhead. In hardware, caches can satisfy table reads, a walker can reuse cached intermediate results, and translation can overlap parts of the data-cache lookup. A software miss handler also has trap and instruction costs outside this formula.
Bind entries to the right address space
Suppose process A maps VPN 0x12345 to PFN 0x900 and process B maps the same VPN to PFN 0xA00. With an untagged TLB, the kernel needs to invalidate applicable cached user translations when switching between their address spaces. Changing the table root alone would leave a cached result for the wrong process. Invalidation prevents the wrong hit at the cost of refilling translations later.
An address space identifier (ASID) lets the TLB distinguish A's entry from B's. If A has ASID 7 and B has ASID 9, the lookups use different tags even though the VPNs match. The kernel switches the active tag together with the page table root. Translations from both address spaces can coexist across context switches. A tag identifies a mapping context and may be shared by threads in the same process.
ASIDs are finite. Before reusing one for an unrelated address space, the kernel must ensure entries from the old assignment cannot still match it. Changing an existing mapping within the same ASID also requires handling stale entries. For example, unmapping a writable page and lending its frame to another process requires invalidating relevant translations before reuse. On multiple cores this can involve sending invalidation requests to other CPUs that may hold the entry and waiting for acknowledgement. The TLB is a cache, so updates to the table and the lifetime of frames must account for cached state on all relevant processors.
Build a two-level table from page-sized nodes
Use 32-bit virtual addresses, 4 KiB pages, and four-byte entries. A table page holds 4096 / 4 = 1024 = 2^10 entries. Split the 20-bit VPN into two 10-bit indices. The first indexes a page directory (PD) and the second indexes a leaf page table (PT). The low 12 bits are still the byte offset.
PD index = VA >> 22
PT index = (VA >> 12) & 0x3FF
Offset = VA & 0xFFF
The root register holds the physical base of the directory. An allocated directory entry points to a physical frame containing a leaf table. Each leaf entry describes one virtual data page and may point to any data frame. A directory entry covers 1024 * 4096 = 4,194,304 bytes = 4 MiB of virtual space. If a range needs no leaf entries, the kernel can mark the directory entry absent and omit the whole 4 KiB leaf. In a present leaf, individual entries can still be absent, so sparse mappings within a single 4 MiB span consume at least one leaf page.
Consider a process mapping two pages at the low end of the address space and two at the high end, with each pair inside its own directory range. The table consumes one directory and two leaves. That is 3 * 4096 = 12,288 bytes versus the flat table's 4,194,304 bytes, a savings of 4,182,016 bytes. Those figures count translation structures only. The data pages cost the same under either layout. With a leaf allocated for every directory entry, total cost instead becomes 4096 + 1024 * 4096 = 4,198,400 bytes, one page above the flat table. Distribution across directory ranges determines the savings.
One extra walk step is the price for the omitted table storage. A hardware walker needs the directory result before it knows the physical address of the leaf entry. More levels extend this idea to wider address spaces by slicing off additional index bits and representing the tables as a deeper tree. Bigger pages provide a second lever. They use more offset bits, require fewer entries to cover the same range, and extend TLB reach at the cost of larger allocation granularity.
Decide when sparsity pays for another level
A directory with k allocated leaves costs 4096 * (1 + k) bytes in this model. The flat table costs 4096 * 1024 bytes. The two formats cost the same at 1023 leaves, so the two-level representation saves space whenever there are 1022 or fewer. Savings are most significant when mappings cluster in a small number of directory ranges.
To see the dependence on placement, map 1024 pages in one contiguous, directory-aligned 4 MiB range. One leaf holds all their entries and the two levels cost just 8192 bytes. Now map the same number of pages with one page in each 4 MiB range. Every directory slot needs a leaf and the table occupies 4,198,400 bytes. Both layouts map the same number of bytes and have the same theoretical walk depth. The second pays for 1023 unused PTE slots inside each leaf. The directory saves pages at the granularity of entire missing subtrees.
Table geometry also determines which addresses share the intermediate lookup. Consider 0x003FFFFF and 0x00400000. They sit one byte apart but belong to directory entries 0 and 1, respectively. The first uses leaf index 1023 and offset 0xFFF. The second starts at leaf index zero and offset zero in a different leaf. Mapping one of those pages does not supply a translation for the other. If entry 1 is absent, the walk for the second address ends after the directory read, and the hardware reports the fault. Kernel metadata then determines whether the range is illegal or has a valid mapping whose lower-level structure or data must be supplied on demand.
A walker that caches information about directory entries can benefit from repeated references even when individual page translations miss the TLB. A scan across different pages in one directory range can reuse the directory result while consulting different PTEs. That locality is one reason two conceptual levels do not always imply two accesses to main memory.
Worked trace of a 32-bit walk and refill
Assume a cold TLB, ASID 7, and a directory rooted at physical address 0x00100000. A load uses virtual address 0x12345ABC. The directory entry for that address names leaf PFN 0x3A, and its leaf entry is readable and resident with data PFN 0x2F0.
- The address splits into
PD = 0x048,PT = 0x345, andOffset = 0xABC. The full VPN is0x12345. The 10-bit boundary between directory and table does not align to a hexadecimal digit, so use the shifts and mask. - Read the PDE at
0x00100000 + 4 * 0x48 = 0x00100120. The entry gives the leaf frame base0x3A000. - Read the PTE at
0x3A000 + 4 * 0x345 = 0x3AD14. The data frame starts at0x2F0000. The load reaches0x2F0ABCafter two table reads and one data read. - Cache the mapping for ASID 7 and VPN
0x12345. A second load at0x12345ABDhits this translation and reads physical byte0x2F0ABDwith no table reads.
Change one input. Keep all table contents the same and change the active ASID to 9, assuming no entry for that tag. The second load now misses the TLB despite the identical VPN. With the same root in this experiment, the walk yields the same physical byte and populates a separately tagged entry. The extra walk makes this access take the modeled miss time rather than the modeled hit time.
Next, follow what happens when a legal page is absent in page faults, replacement policies, and VAX/VMS.
Before you move on
Try it in your workspace
Walk a 2-level page table for an address split into page directory index, page table index, and offset. Compare total memory accesses on a TLB hit vs. a TLB miss, and explain ASID tagging.
Keep a short note of what you tried, what passed, and what you still need to check.
0 of 3 checked.
Saved in this browser when storage is available. Uncheck any item to revisit it. This is your own record, not an assessment.