4.1.15 · D4Computer Architecture (Deep)

Exercises — TLB — structure, TLB miss handling

2,757 words13 min readBack to topic

Quick symbol refresher (so line one is followable):

  • = number of bits in a virtual address; = number of bits in a physical address.
  • = page size in bytes, so = number of offset bits.
  • VPN = Virtual Page Number = the top bits (which page).
  • PFN = Physical Frame Number = the top bits of the physical address (physical width minus the offset bits — the offset is shared, so a frame number needs exactly the bits above the offset).
  • = TLB hit ratio (fraction of accesses found in the TLB), = TLB lookup time, = one DRAM access, = number of page-table levels walked on a miss.
Figure — TLB — structure, TLB miss handling

Level 1 — Recognition

Exercise 1.1 — Name the parts

A virtual address on a machine is 0x00007F3A_12345ABC. The page size is . Without doing arithmetic yet, state: how many bits are the offset, and which part of the address is the VPN?

Recall Solution

Page size bytes, so offset = 12 bits. bits = exactly hex digits (each hex digit = bits). So the bottom 3 hex digits ABC are the offset, and everything above them is the VPN: 0x7F3A12345. Why: translation happens at page granularity — only the "which page" bits (VPN) get looked up; the "where inside the page" bits (offset) pass through untouched. This is the cut in Figure s01.

Exercise 1.2 — Miss vs fault

Classify each event as TLB miss, page fault, both in sequence, or neither: (a) Translation is in the page table but not in the TLB. (b) The PTE's valid bit is 0 (page is on disk). (c) The VPN is already sitting in the TLB with Valid=1 and matching ASID.

Recall Solution

(a) TLB miss only — nanoseconds; hardware/OS just fetches the PTE. (b) A TLB miss occurs first (nothing cached), the walk then finds an invalid PTE, which raises a page faultboth in sequence. Milliseconds. (c) Neither — this is a plain TLB hit. Why it matters: a TLB miss is a caching event; a page fault is a "page not in RAM" event. See the parent's Page Faults & Demand Paging link.


Level 2 — Application

Exercise 2.1 — Bit split

A CPU has -bit virtual addresses and uses huge pages. Compute the offset bits, the VPN bits, and the number of virtual pages a process could address.

Recall Solution

bytes → offset = 21 bits. VPN bits. Number of pages . Why huge pages help: with pages the VPN was bits ( pages); a page covers more memory per TLB entry, boosting TLB reach (see Huge Pages / TLB Reach).

Exercise 2.2 — Form the physical address

Page size (). The TLB reports VPN 0x2B00F → PFN 0x000C4. The virtual address is 0x2B00F_2A7. Compute the physical address. (Figure s02 shows the bit-slot picture that this arithmetic is really doing.)

Figure — TLB — structure, TLB miss handling
Recall Solution

Offset = low 12 bits = 0x2A7 (3 hex digits). VPN = 0x2B00F → matches the TLB entry, PFN = 0x000C4. Why the shift: the PFN names a frame; a frame is bytes wide, so it must sit in the bits above the 12 offset bits. Then the untranslated offset drops back into the bottom — exactly the "slide then stitch" of Figure s02.


Level 3 — Analysis

Exercise 3.1 — EMAT with a 4-level walk

, , hit ratio , and a hardware walker touches levels on a miss. Compute the Effective Memory Access Time using the parent's boxed formula. (Figure s03 draws the walk as a chain of DRAM reads.)

Figure — TLB — structure, TLB miss handling
Recall Solution

, so the bracket . Reading it: the is the unavoidable data fetch; the extra is the average walk penalty spread over all accesses — the four DRAM hops of Figure s03, but paid only on the of accesses that miss.

Exercise 3.2 — How much does the walk cost?

Using the numbers of 3.1, what fraction of EMAT is pure translation overhead (everything except the single mandatory data access )?

Recall Solution

Overhead . Fraction . Why care: nearly one-fifth of every memory access is "just finding out where to go." This is exactly why the TLB (and huge pages) earn their silicon.

Exercise 3.3 — Solve for required hit ratio

You need EMAT with , , . What minimum hit ratio is required?

Figure — TLB — structure, TLB miss handling
Recall Solution

Read the figure first: its horizontal axis is the TLB hit ratio (from to ), its vertical axis is EMAT in ns. The magenta curve is our formula; the dashed violet line is the target; the orange dot is where they cross — the answer we now compute algebraically. Set the formula to the target and solve for : Subtract 1: . Divide by 80: . So (98.75%). Reading the figure: EMAT falls steeply as ; the orange dot sits far to the right, showing the last few percent of hit rate buy the biggest wins — the whole design story of TLBs.


Level 4 — Synthesis

Exercise 4.1 — Context switch: to flush or not?

Process A ran and filled the TLB. Now the OS switches to Process B. Both use virtual address 0x400000, mapping to different frames. (a) Without ASIDs, what must the OS do and why? (b) With ASIDs/PCIDs, what does it do instead? (An ASID = Address Space ID; PCID = Process-Context ID, Intel x86's name for the same idea — a small tag stored in each TLB entry naming its owning address space.) (c) What happens to kernel pages tagged Global?

Recall Solution

(a) Without ASIDs, entries carry no owner tag, so A's 0x400000→frameₐ could be wrongly used by B. The OS must flush the entire TLB on every switch — throwing away good entries and forcing cold misses afterward (see Context Switching). (b) With ASIDs / PCIDs, each entry is tagged by owning address space. A lookup only hits if VPN matches AND the tag matches. B's accesses simply miss A's entries and coexist safely — no flush needed; entries reload lazily. (ASID is the generic term; PCID is Intel's implementation of it.) (c) Global entries (kernel pages, shared by all processes) match regardless of tag and are kept across the switch — never flushed, so kernel translations stay warm.

Exercise 4.2 — Design a two-level TLB budget

You may spend silicon on either a bigger L1 TLB or add an L2 TLB. L1 hit (). Option X: enlarge L1 to hit . Option Y: keep L1 at but add an L2 TLB with that catches of L1 misses (so a further of the that missed L1). , . Which gives lower EMAT?

Model for Y: a miss in L1 (prob ) checks L2 (costs ). L2 hit (prob of those) → then the data access . L2 miss (prob of those) → full walk then data. L1 hit (prob ) → . Ignore differences beyond the base paid always.

Recall Solution

Option X (single-level formula, ): Option Y (two-level), always pay L1 lookup:

  • L1 hit (): cost .
  • L1 miss → L2 lookup , then split:
    • L2 hit (): .
    • L2 miss (): walk gives PFN then one data access: . Inner bracket . So . Option Y wins (88.1 < 90.6). Why: the cheap L2 (7ns) absorbs most L1 misses far more cheaply than a full 4-level walk (320ns), even though the L1 hit rate stayed low. This is why real CPUs ship a small fast L1 TLB plus a larger L2 TLB (see Cache Memory & Associativity).

Level 5 — Mastery

Exercise 5.1 — Huge pages vs TLB reach, end to end

A workload touches of memory uniformly. The L1 TLB has entries. (a) With pages, how much memory does the TLB cover (its reach), and what fraction of the working set fits? (b) With huge pages, recompute reach and fraction. (c) Explain, using these numbers, why huge pages slash TLB misses here — and name one cost.

Recall Solution

(a) 4KB pages: reach . Fraction of the set covered . The TLB covers almost nothing → the working set thrashes the TLB, misses everywhere. (b) 2MB pages: reach . Fraction . Now half the entire working set is covered by the TLB at once — misses plummet. (c) Reach scaled by , turning coverage into . Cost: internal fragmentation (a page barely used still costs ), harder allocation of contiguous physical memory, and coarser permission granularity. It is a tradeoff, not a free win (see Huge Pages / TLB Reach).

Exercise 5.2 — Software vs hardware managed miss cost

On a MIPS-style software-managed TLB, a miss traps to the OS: trap entry/exit , then the handler does memory reads to walk the table ( each) and one special "write TLB" instruction . On an x86-style hardware-managed walker the same reads happen but with no trap and no special instruction (walker in hardware). Compare the per-miss cost (translation only, exclude the final data access which both pay).

Recall Solution

Software-managed miss: . Hardware-managed miss: . Difference of pure trap/handler overhead per miss. The tradeoff: software management gives the OS total freedom over page-table format (Page Tables (Multi-level) can be anything), but pays that overhead on every miss. Hardware management is faster but locks the table format into the ISA. Neither is universally "better."


Related: Virtual Memory · Page Tables (Multi-level) · Cache Memory & Associativity · Page Faults & Demand Paging · Context Switching · Huge Pages / TLB Reach