5.4.12Memory Hierarchy & Caches

TLB (translation lookaside buffer)

2,064 words9 min readdifficulty · medium5 backlinks

WHY does the TLB exist?


WHAT is it, precisely?

An address splits into a page part and an offset:

VA=VPNvirtual page number    offsetwithin page\text{VA} = \underbrace{\text{VPN}}_{\text{virtual page number}} \;\Vert\; \underbrace{\text{offset}}_{\text{within page}}

The offset is not translated — only the page number is. So the TLB maps VPN → PFN (physical frame number), and:

PA=(PFNlog2P)    offset\text{PA} = (\text{PFN} \ll \log_2 P) \;\Vert\; \text{offset}

where PP = page size.


HOW a lookup works (derive the flow)

Every memory access:

  1. Split VA into VPN + offset.
  2. Look up VPN in the TLB.
    • TLB hit → get PFN immediately → form PA → access cache/memory.
    • TLB miss → do a full page-table walk (hardware or OS) to get PFN → insert into TLB → retry.
  3. Also check the valid and permission bits (read/write/execute, user/kernel). A violation raises a fault.
Figure — TLB (translation lookaside buffer)

Performance: the Effective Memory Access Time

Let:

  • hh = TLB hit rate, tTLBt_{\text{TLB}} = time to search TLB,
  • tmt_{m} = one memory access time,
  • a walk costs WW memory accesses (1 for single-level, LL for LL-level).

On a hit: pay TLB search + one data access = tTLB+tmt_{\text{TLB}} + t_m. On a miss: pay TLB search + walk (WtmW\cdot t_m) + data access = tTLB+Wtm+tmt_{\text{TLB}} + W\,t_m + t_m.

Weighting by probability:

  EMAT=tTLB+tm+(1h)Wtm  \boxed{\;\text{EMAT} = t_{\text{TLB}} + t_m + (1-h)\,W\,t_m\;}


Context switches & consistency (the tricky part)



The 80/20 core

Recall Feynman: explain to a 12-year-old

Imagine your school has thousands of lockers, but the map telling you which locker is yours is kept in the principal's office far away. Every time you fetch a book you'd have to walk to the office, read the map, then walk to your locker — super slow. So you keep a sticky note in your pocket with the locker numbers you use most. The TLB is that sticky note: check your pocket first (fast!), and only walk to the office (the page table) when the note doesn't have it.


Flashcards

What does a TLB cache?
Recently-used virtual-page-number → physical-frame-number translations (plus permission/valid bits).
Why is the page offset never translated?
Paging moves whole pages to frames; a byte keeps its position within the page, so the low log2P\log_2 P bits are identical in VA and PA.
For a 4KiB page, how many offset bits?
12, since 4KiB=2124\text{KiB}=2^{12}.
What happens on a TLB miss?
A page-table walk finds the PFN, the mapping is inserted into the TLB, and the access is retried.
Difference between TLB miss and page fault?
TLB miss = translation not cached but page is in memory (fast walk); page fault = page not in physical memory (must load from disk).
Give the EMAT formula.
EMAT=tTLB+tm+(1h)Wtm\text{EMAT}=t_{\text{TLB}}+t_m+(1-h)W t_m, with hit rate hh and walk length WW accesses.
Why does hit rate matter so much?
The extra cost (1h)Wtm(1-h)Wt_m scales with the miss fraction; each miss costs a full walk, so small drops in hh inflate access time.
Two ways to handle the TLB on a context switch?
Flush the whole TLB, or tag entries with ASID/PCID so multiple address spaces coexist.
What is a TLB shootdown?
Invalidating a stale TLB entry across cores (via an inter-processor interrupt) when the OS changes/unmaps a page.
How is PA formed from PFN and offset?
PA=(PFNlog2P)    offsetPA=(\text{PFN}\ll\log_2 P)\;|\;\text{offset}.
Why not just make the TLB huge?
It's on the critical path of every access; larger = slower/more power. Hence multi-level TLBs (L1 small+fast, L2 bigger).

Connections

  • Virtual Memory — the TLB accelerates the address translation that paging requires.
  • Page Table — the slow structure the TLB caches; consulted on a miss (the "walk").
  • Cache (memory hierarchy) — same locality principle; VIPT caches even overlap TLB lookup with cache indexing.
  • Context Switch — triggers TLB flush or ASID handling.
  • Page Fault — a different miss: page absent from RAM, not just from the TLB.
  • Locality of Reference — why the TLB's small size still yields high hit rates.

Concept Map

requires

reads

causes

motivates

caches

lives in

splits into

looked up in

hit

miss triggers

inserts into

checks

Virtual memory paging

Page-table walk

Page table in DRAM

2x to 5x slower access

Program locality

TLB cache

VPN to PFN mappings

MMU hardware

Virtual address

VPN + offset

Form physical address

Valid and permission bits

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, virtual memory mein har ek memory access ke liye CPU ko virtual address ko physical address mein convert karna padta hai. Ye conversion page table dekhkar hoti hai — par page table khud DRAM mein hoti hai, jo slow hai. Agar har access pe page table padhna pade, to speed aadhi ya usse bhi kam ho jaaye (multi-level page table mein to 4 DRAM reads!). Isiliye TLB banaya gaya: ye ek chhota, super-fast hardware cache hai jo recently use hui VPN -> PFN translations yaad rakhta hai. Programs baar-baar same pages touch karte hain (locality), isliye TLB mostly hit hota hai aur slow walk bach jaata hai.

Key baat: address ka offset translate nahi hota. Sirf page number (upar ke bits) badalta hai, niche ke log2P\log_2 P bits (jaise 4KiB page ke liye 12 bits) waise ke waise pass ho jaate hain. Flow simple hai — Hit? Home (turant PFN mil gaya), Miss? Map (page table walk karo, PFN nikaalo, TLB mein daalo, retry karo).

Performance ka formula bhi first principles se aata hai: hamesha TLB search + ek data access lagta hai, aur extra cost sirf miss pe: EMAT=tTLB+tm+(1h)Wtm\text{EMAT}=t_{\text{TLB}}+t_m+(1-h)Wt_m. Isliye hit rate hh bahut important hai — thoda sa gira to overhead teen-guna badh jaata hai.

Ek confusion door kar lo: TLB miss aur page fault alag cheez hain. TLB miss matlab translation cache mein nahi tha par page RAM mein hai (fast walk). Page fault matlab page RAM mein hai hi nahi, disk se laana padega (hazaaron guna slow). Aur context switch pe TLB ya to flush hota hai ya ASID/PCID tags se different processes ki entries alag rakhi jaati hain.

Go deeper — visual, from zero

Test yourself — Memory Hierarchy & Caches

Connections