5.4.11 · D4Memory Hierarchy & Caches

Exercises — Virtual memory and paging

1,935 words9 min readBack to topic

Before we start, one picture ties every problem together — the anatomy of an address.

Figure — Virtual memory and paging
Recall The three operations you will reuse everywhere

Let page size be bytes. For a virtual address :

  • offset — the low bits (what byte inside the page).
  • VPN — the high bits (which page).
  • Rebuild: .

Everything below is these three lines applied with care.


Level 1 — Recognition

Exercise 1.1

Which of these is translated by the MMU, and which is copied unchanged: the VPN or the offset?

Recall Solution

The VPN is translated (it becomes a PFN by looking it up in the page table). The offset is copied unchanged.

Why: moving a page into a frame is a rigid shift — byte 5 of the page is still byte 5 of the frame. Only the base changes. Look at figure s01: the yellow offset bits pass straight through, the coloured VPN bits go through the table.

Exercise 1.2

A system uses 4 KiB pages. How many bits is the offset, and what is ?

Recall Solution

bytes. So and the offset is 12 bits.

Why: the offset must be able to name every byte inside one page. With bytes per page, you need exactly bits to count through .


Level 2 — Application

Exercise 2.1

Page size (). The virtual address is . Split it into VPN and offset.

Recall Solution

Low bits last three hex digits (each hex digit is bits, so ). Why the last-3-digits shortcut: shifting right by bits is the same as dropping the last hex digits. So VPN , offset .

Exercise 2.2

Continuing 2.1: the page table maps VPN PFN . What is the physical address?

Recall Solution

Why shift the PFN by 12: frame number begins at physical byte . Shifting left by builds that base address (); ORing the untouched offset lands on the exact byte.


Level 3 — Analysis

Exercise 3.1

A machine has a -bit virtual address, pages, and each page-table entry is bytes. Compute the number of entries and the total size of a single-level page table per process.

Recall Solution

Entries (about a million). Why: one entry per virtual page; there are (address-space size)/(page size) pages. Why care: with processes that is of RAM spent purely on translation tables — the pressure that motivates Multi-level page tables.

Exercise 3.2

, , TLB hit ratio (single-level table). Find the effective access time (EAT).

Recall Solution

Rebuild the formula: on a hit, pay TLB + memory. On a miss, pay TLB + one table walk () + the real access (). Why only ns: the extra table walk is only paid by the of accesses that miss the TLB. Averaged over all accesses, that's . This is why the TLB, exploiting Locality of reference, makes translation nearly free.


Level 4 — Synthesis

Exercise 4.1

A two-level page table on a -bit machine with pages splits the -bit VPN into a -bit outer index and a -bit inner index. A process uses only its first of virtual memory (one page). Count the page-table memory actually needed, assuming -byte entries and that each table (outer or inner) fits in one page.

Figure — Virtual memory and paging
Recall Solution

Why split the VPN: so we only allocate inner tables for regions the process actually touches. See figure s02 — the outer table has slots but only one points to a real inner table.

  • The outer table always exists: entries bytes bytes .
  • Exactly one inner table is needed (the process touches one page, which lives under one outer slot): another . Why this crushes the single-level cost: single-level needed (Ex 3.1). Two-level needs for a sparse process — a saving. Unused outer slots simply stay empty (no inner table allocated).

Exercise 4.2

Same machine (two levels, , ). On a TLB miss the MMU must walk both levels (two memory reads) before the real access. With hit ratio , find EAT.

Recall Solution

Why two extra reads: a miss now reads the outer table (1 access) then the inner table (1 access) to get the PFN, then the real data. Why only vs before: the deeper walk costs instead of , but it is still only paid by the that miss: . Locality keeps even a taller tree cheap.


Level 5 — Mastery

Exercise 5.1

Design check. You must support a -bit virtual address space with pages, and you want each page table (at every level) to fit in exactly one page, using -byte entries. How many levels does the page table need?

Recall Solution

Step 1 — offset. , so the VPN is bits. Step 2 — how many index bits per level. One page holds entries, so each level consumes VPN bits. Step 3 — divide. Levels levels. Answer: 4 levels (this is exactly real x86-64's 4-level scheme, PML4 → PDPT → PD → PT). Why it works out whole: is divisible by ; if it weren't, the top level would use fewer bits (a partially-filled table).

Exercise 5.2

Capstone. On that -bit, -level machine (, ), a program strides through a huge array touching a fresh page almost every access, so the TLB hit ratio collapses to . Compute EAT, and state in one sentence which principle the program violated.

Recall Solution

A miss now walks four levels () then does the real access: Plug in : That's over slower than the raw memory time. Principle violated: poor locality of reference — by touching a new page every access, the program defeats the TLB (see Locality of reference), so almost every access pays the full -level walk.


Recall

Recall One-line self-test
  • Which part of a VA is translated? ⟶ the VPN; the offset is copied.
  • Single-level table entries for -bit VA, pages? ⟶ ().
  • EAT formula, single level? ⟶ .
  • Why do multi-level tables save memory? ⟶ inner tables are allocated lazily, only for used regions.
  • Levels for -bit VA, pages, -byte entries, tables? ⟶ .