5.4.12 · D3Memory Hierarchy & Caches

Worked examples — TLB (translation lookaside buffer)

3,275 words15 min readBack to topic

Prerequisites we lean on: Virtual Memory, Page Table, Cache (memory hierarchy), Context Switch, Page Fault, Locality of Reference.


The scenario matrix

Before solving anything, let's list every kind of thing a TLB question can throw at you. Think of this as the full deck of cards; the examples below play every card at least once.

# Scenario class What makes it different Covered by
A TLB hit translation already cached → no walk Ex 1
B Address bit-split slicing VA into VPN + offset for non-obvious page sizes Ex 2
C TLB miss, page present walk the page table, page is in RAM Ex 3
D TLB miss → page fault page not in RAM, disk load needed Ex 4
E EMAT single-level performance maths, Ex 5
F EMAT multi-level walk , the x86-64 case Ex 6
G Context switch (flush vs ASID) stale/wrong-process entries Ex 7
H Degenerate / limiting inputs , , Ex 8
I Real-world word problem loop over an array, locality in action Ex 9
J Exam twist given EMAT, solve backwards for Ex 10

Every cell A–J below is worked. Let's go.


Reminders you'll reuse (so no symbol appears unexplained)

Figure — TLB (translation lookaside buffer)

Look at the figure: the address is a ruler. A vertical red line at bit position cleanly divides it. Nothing above the line ever changes the within-page position; nothing below the line is ever translated.


Ex 1 — Scenario A: a clean TLB hit

Step 1 — how many offset bits? , so offset bits. Why this step? 12 bits = exactly 3 hex digits (each hex digit is 4 bits), which makes the split land on a hex boundary — nice.

Step 2 — slice the VA. Low 3 hex digits = offset = 0x2C4. Remaining top = 0x1F0AB = VPN. Why? The lowest bits are the offset by definition; hex digits.

Step 3 — look up VPN in TLB. 0x1F0AB is present → hit, PFN = 0x0072. Why? This is scenario A: cached translation, so we skip the page table entirely.

Step 4 — rebuild the PA. . Why shift by 12? PFN 0x72 names a 4KiB frame; its base byte address is , and shifting left 12 bits is that multiply. Then we OR the untranslated offset into the emptied low bits.

Recall Verify

Offset unchanged in VA and PA? VA low = 2C4, PA low = 2C4. ✓ ; OR 0x2C4 gives 0x722C4. ✓ No walk occurred.


Ex 2 — Scenario B: an awkward page size (bit-split care)

Step 1 — offset bits. , so offset bits. Why? Same rule: . But 13 is not a multiple of 4 — so the split does not fall on a hex boundary. Trap!

Step 2 — VPN width. VPN bits bits. Why? Total address bits minus offset bits = everything left for the page number.

Step 3 — build the offset mask, then extract the offset. To keep the low 13 bits we AND with a mask of thirteen 1-bits. That mask is . Compute it: , so , and (four hex digits 1FFF = binary 1 1111 1111 1111, which is exactly thirteen 1s). Now 0xABCD in binary (16 bits). AND with 0x1FFF keeps the low 13 bits — bit 13 (value 0x2000) and above are dropped, everything from bit 12 down stays. Bits 15–13 of 0xABCD are 101, so they are cleared; the surviving low 13 bits are 0 1011 1100 1101 = 0x0BCD. Why? Offset = lowest 13 bits; a mask is the standard "keep the low bits" trick because it is all-ones below bit 13 and all-zeros above.

Step 4 — extract VPN. Shift the whole VA right by 13: 0xABCD >> 13 = 0x5 (only the top 3 bits 101 survive). Why? VPN = everything above the offset, i.e. VA divided by .

Recall Verify

Mask: . ✓ 0xABCD & 0x1FFF = 0x0BCD (offset) and 0xABCD >> 13 = 0x5 (VPN). Reassemble: . ✓ Round-trip matches the original VA.


Ex 3 — Scenario C: TLB miss, page is in memory

Step 1 — TLB lookup fails. VPN 0x0055 absent → TLB miss. Why? Scenario C: translation simply wasn't cached yet (cold entry or evicted).

Step 2 — page-table walk. Hardware reads the page table entry in DRAM → PFN 0x0300, valid = 1. Why this step? The mapping still lives in the page table even when the TLB forgot it — the TLB is a cache, not the source of truth (Page Table is).

Step 3 — no fault, page present. Valid bit = 1 → page is in physical RAM. No Page Fault, no disk. Why? This is the whole point of the second parent mistake: a TLB miss is a fast event (one or more DRAM reads), not a slow one (disk).

Step 4 — fill and retry. Insert 0x0055 → 0x0300 into the TLB, then re-run the access → now it hits. Why? Locality of Reference: the next access to this page will now be a hit — we pay the walk once, benefit many times.

Recall Verify

Cost of this access = TLB search + 1 walk read + data read = a handful of ns. No milliseconds (disk) anywhere. ✓


Ex 4 — Scenario D: TLB miss that becomes a page fault

Step 1 — TLB miss → walk. VPN 0x0400 absent from the TLB → TLB miss, so hardware starts a page-table walk. Why? On any miss the TLB has no translation to offer, and the authoritative mapping lives only in the Page Table in DRAM — so we must read the page table entry to learn the frame (or that there isn't one yet).

Step 2 — valid bit = 0. The page-table entry says: not resident in RAM. This raises a page fault trap to the OS. Why? The valid bit is the flag that distinguishes "in RAM" from "on disk / never allocated."

Step 3 — OS handles the fault. OS picks a free (or evicted) frame, reads the page from disk into it, updates the page table entry (valid = 1, new PFN), then returns. Why? Only software knows the disk layout and eviction policy; hardware can't do this.

Step 4 — retry, now TLB miss → walk → hit. The faulting instruction restarts; this time the walk succeeds and fills the TLB.

Step 5 — cost comparison. DRAM access ≈ 100 ns; disk access ≈ 10 ms ns. Ratio slower than the Ex 3 walk. Why show this? To make "TLB miss ≠ page fault" quantitative: one is nanoseconds, the other is a hundred-thousandfold worse.

Recall Verify

; . ✓


Ex 5 — Scenario E: EMAT, single-level walk

Step 1 — plug into the formula. . Why this form? You always pay TLB search + one data read; the only variable extra is the walk, charged on the miss fraction .

Step 2 — evaluate the extra term. ns. Why? 5% of accesses walk, each costing one ns; average extra ns.

Step 3 — total. ns.

Step 4 — compare to no TLB. Without a TLB there is nothing to search, so ; and every access must walk, so effectively . The formula reduces to Speedup . Why? Shows the TLB nearly halves access time even at a modest 95% hit rate — locality pays off.

Recall Verify

. ✓ . ✓


Ex 6 — Scenario F: EMAT with a 4-level walk (x86-64)

Step 1 — same formula, new . . Why? Only the walk length changed: a 4-level walk is 4 DRAM reads instead of 1.

Step 2 — extra term. ns. Why? Each of the 5% misses now costs ns of walking; averaged over all accesses: ns.

Step 3 — total. ns.

Step 4 — contrast with Ex 5. Single-level = 106ns, four-level = 121ns. The same 5% miss rate now hurts more (20ns vs 5ns extra). Why it matters: deeper page tables make the TLB more valuable, not less — which is why real CPUs add L2 TLBs.

Recall Verify

. ✓ Extra term ratio . ✓


Ex 7 — Scenario G: context switch, flush vs ASID

Step 1 — the danger. VPN 0x5 means different frames in A vs B. A stale A-entry would send B to PFN 0x0100 — B reads A's memory. A correctness disaster. Why? The TLB has no idea whose address space it holds unless we tell it.

Step 2 — (i) no protection. TLB still holds A's 0x5 → 0x0100 → returns 0x0100. WRONG. B gets A's data. Why show it? This is the failure the other two options exist to prevent.

Step 3 — (ii) flush on switch. On the switch, the OS clears the whole TLB. B's access to 0x5 now misses, walks B's page table, gets correct 0x0900. Why? Correct — but the cold TLB causes a miss storm right after every switch. See Context Switch.

Step 4 — (iii) ASID tags. An ASID (address-space identifier) is a small number the OS assigns to each process's address space. Each TLB entry is tagged with the ASID of the process it belongs to. A's entry is tagged ASID=A; B searches with ASID=B, doesn't match, misses, walks, and inserts 0x5[B] → 0x0900. Both entries coexist. Why? No flush → A's entries survive for when we switch back → fewer misses. Modern win.

Recall Verify

Correct frame for B = 0x0900. Options (ii) and (iii) return 0x0900; option (i) returns 0x0100 (wrong). ✓


Ex 8 — Scenario H: degenerate & limiting inputs

Step 1 — (a) . ns. Why? No misses ever → the walk term vanishes. You still pay the 1ns TLB search + 100ns data read.

Step 2 — (b) . ns. Why? Every access misses → every access walks 4 reads. This is the worst case — the TLB is pure overhead (the extra 1ns) plus a full walk each time.

Step 3 — (c) ideal TLB, , . ns. Why? A free, always-hitting TLB reduces access time to exactly one raw memory read — the theoretical floor. You can't beat "just read the data once."

Step 4 — sanity of the range. Real EMAT lives between the floor (100ns) and the ceiling (501ns); Ex 6's 121ns sits near the floor, confirming a good hit rate hugs the best case.

Recall Verify

(a) . (b) . (c) . ✓ And . ✓


Ex 9 — Scenario I: real-world word problem (locality in a loop)

Step 1 — total accesses. bytes; at 4 bytes per int → accesses. Why? One memory access per int read.

Step 2 — accesses per page. Each 4KiB page holds ints. Why? bytes per page bytes per int.

Step 3 — count misses. The first access to each page misses (cold, not yet in the TLB); the other 1023 accesses to that same page hit. With 4 pages that gives misses total. Why? This is Locality of Reference made concrete: one miss "pays" for 1024 uses of the same translation (spatial locality).

Step 4 — hit rate. (about 99.90%). Why? Hits over total accesses. Sequential scans are the TLB's best friend.

Recall Verify

accesses; per page; misses ;


Ex 10 — Scenario J: the exam twist (solve backwards)

Step 1 — write EMAT and isolate the miss term. . Subtract the always-paid part: . Why? is paid on every access; the leftover ns must be the walk overhead.

Step 2 — solve for . . Why? Divide out the factor.

Step 3 — solve for . (86% hit rate). Why? The miss rate is , so the hit rate is its complement.

Step 4 — plug back to confirm. ns. ✓ Why? Verifying the inverse: substituting reproduces the given EMAT exactly.

Recall Verify

; . ✓


Cover-check: did we fill every cell?

Scenario matrix

A hit Ex1

B bitsplit Ex2

C miss present Ex3

D page fault Ex4

E EMAT L1 Ex5

F EMAT L4 Ex6

G ctx switch Ex7

H limits Ex8

I loop Ex9

J backward Ex10

Every card A–J is played. Nothing in the space is left unshown.