5.4.13 · D2 · HinglishMemory Hierarchy & Caches

Visual walkthroughPage tables and multi-level paging

2,479 words11 min read↑ Read in English

5.4.13 · D2 · Hardware › Memory Hierarchy & Caches › Page tables and multi-level paging

Prerequisites jinpe hum thoda rely karte hain: Virtual Memory, Bit Manipulation and Masking, Address Space Layout. End mein speed fix TLB and Translation Caching aur Cache Memory Basics se connect hota hai.


Step 1 — Ek address bits ki ek row hai, kuch nahi

KYA. Ek virtual address woh number hai jo ek program hardware ko deta hai jab woh kehta hai "yahan se padho." 32-bit machine par woh number exactly 32 binary digits hota hai — 32 boxes, har ek mein 0 ya 1. Koi jadoo nahi: yeh aur ke beech ek poora number hai.

KYU yahan se shuru karein. Jo kuch bhi aage aata hai woh sirf is bit row ko groups mein slice karna aur har group ko ek chhota number maanna hai. Agar tum address ko bits ki row ke roop mein dekhte ho, toh baaki sab bookkeeping hai.

PICTURE. Neeche, 32 boxes left (high bits, sabse zyada value wale) se right (low bits, sabse kam value wale) tak drawn hain. Example address hai 0x00402ABC.

Figure — Page tables and multi-level paging

Step 2 — Bottom bits kyun special hain: the page

KYA. Memory ko equal blocks mein chop kiya jaata hai jinhein pages kehte hain. Ek common size hai 4 KiB bytes bytes. Page size ko kahein, toh yahan .

KYU yahi exact idea. Poochho: ek single page ke andar ek particular byte ko name karne ke liye mujhe kitne bits chahiye? Ek page consecutive bytes hold karta hai, se tak numbered. alag positions tak count karne ke liye tumhe chahiye

Yahan . Woh bottom bits offset hain: yeh batate hain ki page ke andar byte kahan baith'ta hai. Tool isliye aata hai kyunki har extra bit se naam ki ja sakne wali cheezein double ho jaati hain — aur doubling exactly wahi hai jo invert karta hai. Koi aur function nahi hai jo " items ke liye kitne bits" ka jawab deta ho.

PICTURE. Wohi 32 boxes, ab low ko coral mein alag kar diya gaya hai. Woh coral chunk offset hai; yeh translation ke through bina chheday jaayega.

Figure — Page tables and multi-level paging

Step 3 — Address ka baaki hissa batata hai ki kaun sa page hai

KYA. Offset ke upar sab kuch virtual page number (VPN) hai. bit address aur offset bits ke saath, VPN hai

KYU. Humein sirf translate karna hota hai ki ek byte kis page mein rehta hai, kabhi page ke andar kahan nahi (Step 4 batata hai kyun). Toh hum top bits peel off karte hain aur unhe ek number maante hain, VPN:

  • — page size se divide karo; yeh offset ko throw away karta hai.
  • — floor (round down) koi bhi fractional leftover discard karta hai, ek clean poora page number chodke.

PICTURE. Lavender = 20-bit VPN, coral = 12-bit offset. Do clean fields.

Figure — Page tables and multi-level paging

0x00402ABC ke liye, top 20 bits 0x00402 hain.


Step 4 — Offset kabhi kyun nahi badlta

KYA. Translation ke dauran VPN ko ek physical frame number (PFN) se replace kiya jaata hai, lekin offset verbatim copy hota hai.

KYU. Ek frame physical RAM ka ek block hai jo page ke same size ka hota hai. Kyunki woh same size ke hain, ek page ke andar byte number , uske frame ke andar byte number par land karta hai — "" nahi badlta. Translation sirf decide karta hai kaun sa frame, yani woh sirf top bits rewrite karta hai.

PICTURE. Ek page (lavender box, bytes ) ek frame (mint box) par placed hai. Coral offset arrow dono mein same slot ko point karta hai.

Figure — Page tables and multi-level paging

Step 5 — Ek flat table enormous hogi

KYA. Sabse simple translator ek giant array hai: entry number us page ka frame number hold karta hai. VPN bits ke saath iske liye entries chahiye.

KYU yeh dikhao. Tree ko justify karne ke liye, humein pehle dard feel karna hoga. Har entry (ek PTE) lagbhag bytes ki hai:

Un million entries mein se zyaadatar kisi cheez ki taraf point nahi karte — ek program sirf kuch pages hi touch karta hai. Yeh wasted RAM hai, aur 64-bit machines par ( entries) flat table impossible hai.

PICTURE. Ek tall table jisme kuch used (lavender) rows hain aur empty (grey) rows ka ek vast sea.

Figure — Page tables and multi-level paging

Step 6 — VPN ko do indexes mein split karo: the tree

KYA. 20-bit VPN ko do 10-bit fields mein kato. Ab address mein teen fields hain:

  • — outer index: top table (the "directory") mein ek row pick karta hai.
  • — inner index: directory jis second-level table ki taraf point karta hai usmein ek row pick karta hai.
  • — Step 2 se unchanged.

KYU 10 / 10. entries bytes KiB — har table exactly ek page hai. Aur sabse important: agar poora 1024-page region unused hai, toh uska inner table simply kabhi allocate hi nahi hota. Hum sirf un branches par memory kharch karte hain jo actually use hoti hain — yahi poora saving hai.

PICTURE. 32 boxes teen coloured fields mein regroup hue, 0x00402 decode hota hai , .

Figure — Page tables and multi-level paging

Step 7 — The walk: pointers follow karo frame tak

KYA. Ek base register (CR3 x86 par, top table ka physical address hold karta hai) se shuru karke, hum tree ke neeche hop karte hain:

KYU do reads. Har level RAM mein rehta hai, isliye har hop ek memory access hai. Do levels ⇒ do reads data touch karne se pehle — space bachane ki kimat (Step 9 speed fix karta hai).

PICTURE. CR3 → top table row 1 → inner table row 2 → frame 0x0009C, phir coral offset 0xABC end mein snap ho jaata hai.

Figure — Page tables and multi-level paging

Worked numbers: PFN , toh


Step 8 — Degenerate case: ek unused branch

KYA. Maan lo ka valid flag = 0 hai — directory entry kisi cheez ki taraf point nahi karti.

KYU yeh matter karta hai. Yeh address space ke zyaadatar hisse ke liye normal case hai, aur exactly yahi woh tarika hai jisse tree memory bachata hai. Agar outer entry invalid hai, toh koi inner table hai hi nahi — walk turant ruk jaata hai aur hardware ek page fault raise karta hai (dekho Page Faults and Demand Paging). Humne un pages ke liye inner table par 4 KiB kabhi waste nahi kiya jo koi use hi nahi karta.

PICTURE. Directory mein ek green (valid) row aur 1023 grey (invalid) rows; ek grey row ka arrow "no table — fault" marker mein dead-end ho jaata hai.

Figure — Page tables and multi-level paging

Step 9 — Woh speed jo humne khoyi, aur TLB use wapas kaise deta hai

KYA. Walk ne humen per translation memory reads cost karaaye ( yahan). TLB ek tiny, fast cache hai jo recent results yaad rakhta hai.

KYU. Ek hit par, poora walk skip ho jaata hai — translation roughly ek cycle hoti hai. Ek miss par, hum walk karte hain, phir answer store karte hain. Average cost:

  • — fast lookup ki cost (hamesha pay hoti hai).
  • — accesses ka fraction jo TLB mein nahi mila.
  • memory reads, sirf miss par pay hote hain.

Kyunki real programs baar baar same few pages visit karte hain, miss rate tiny hoti hai — isliye tree apni memory savings rakhta hai aur flat-table speed nearly recover kar leta hai. Zyaada detail TLB and Translation Caching mein.

PICTURE. VPN se do paths: green "TLB hit → PFN" fast lane; longer lavender "TLB miss → walk the tree → fill TLB" path.

Figure — Page tables and multi-level paging

Ek-picture summary

Upar ka sab kuch ek single diagram mein: 32-bit address mein split hota hai; directory walk karta hai, inner table walk karta hai, PFN untouched offset ke saath join hoke RAM mein ek byte name karta hai; TLB poori walk ko shortcut karta hai.

Figure — Page tables and multi-level paging
Recall Feynman: poori walk plain words mein batao

Ek program kehta hai "mujhe byte number 0x00402ABC do." Bottom 12 bits, 0xABC, sirf batate hain 4 KiB room ke andar byte kahan hai — unhe hum kabhi nahi chhedein. Top 20 bits batate hain kaun sa room hai, lekin ek million rooms ki ek giant phone book rakhne ki jagah, hum unhe split karte hain: pehle 10 bits ek chhote table of contents (directory) mein ek chapter pick karte hain, agle 10 bits us chapter mein ek page pick karte hain (inner table), aur woh page humein RAM mein asli room number batata hai — the frame. Asli room number ko "andar kahan" offset ke saath glue karo aur tumhare paas asli byte hai. Agar directory kehti hai "yahan koi chapter nahi hai," hum rok dete hain — page fault — aur humne woh chapter kabhi print nahi kiya, yahi woh tarika hai jisse hum kaagaz bachate hain. Finally, hum last few lookups ki ek sticky note (TLB) rakhte hain taaki hume directory har baar na padhni pade.


Active Recall

Low bits (the offset) kabhi translate kyun nahi hote?
Pages aur frames same size ke hote hain, isliye ek page ka byte , uske frame ke byte par map hota hai; sirf page/frame number badlta hai.
kahan se aata hai?
Ek page ke andar kisi bhi byte-positions ko name karne ke liye bits chahiye.
10/10/12 split mein kya index karta hai vs ?
top-level directory ko index karta hai; us inner table ko index karta hai jis ki taraf directory point karti hai.
4 KiB pages ke saath 20-bit VPN ke liye 10/10 kyun choose karte hain?
KiB, isliye har table exactly ek page mein fit hoti hai.
VA 0x00402ABC ke liye, PFN 0x0009C: physical address kya hai?
(0x0009C << 12) | 0xABC = 0x0009CABC.
Jab outer PTE invalid ho toh kya hota hai?
Walk ruk jaata hai, koi inner table exist nahi karti, aur ek page fault raise hota hai — yahi wajah hai ki unused branches mein koi memory cost nahi hoti.
Kya tree time ya space improve karta hai, aur doosra kya recover karta hai?
Yeh space bachata hai; TLB khoyi hui translation time wapas recover karta hai.