Lecture 11 · Virtual memory: concepts

Introduction to Computer Systems · Lecture 11。个人学习讲义与配套实验,按主题整理并持续修订。

课程源码 ZIP ↓

个人整理的学习笔记,非官方讲义;课程来源与整理说明见课程介绍。配套示例未逐一运行验证。

本篇目录

Maps to: CMU 15-213/15-513/14-513 Fall 2026, Lecture 11 (Sep 29), slide deck 11-vm-concepts. Reading: CS:APP3e §9.1–9.6. Sources: the public Fall 2026 slides and CS:APP. There is no VM lab, so the code is an original concept simulator; its geometry matches the CS:APP “simple memory system” so Lecture 12 can reproduce the textbook translation. Simulator output is deterministic (identical under Python 3.9 and 3.13). Code: code/lecture11/. Reading time: ~90 minutes; ~15 more for the demos.


0. How to read this lecture

0.1 Learning goals

After this lecture you should be able to:

  1. Explain what a virtual address space is and why each process gets its own.
  2. Split a virtual address into VPN and offset, and translate it through a page table to a physical address.
  3. Distinguish a page hit from a page fault, and describe how the OS handles a fault.
  4. Explain virtual memory as a tool for caching (DRAM over disk), memory management and protection.
  5. Explain why a TLB is needed and how it caches translations, and why page tables are multi-level.

0.2 Prerequisites and scope

  • Lecture 10: caches (the TLB and the DRAM-over-disk cache are both caches, with the same tag/index machinery).
  • Lecture 6: the process memory layout and ASLR; Lecture 7: uniform load addresses — both explained by VM here.
  • In scope: CS:APP §9.1–9.6 (physical vs virtual addressing, the page table, page hits/faults, VM as caching/management/protection, the TLB, multi-level page tables). The worked translation example and the Core i7 system are Lecture 12.

0.3 Section → code map

SectionIdeaDemo in code/lecture11/
§1–2virtual addressing; the page table; hit vs fault01_address_translation.py
§3VM as management, protection, caching01_… (private spaces)
§4the TLB02_tlb.py
§5multi-level page tables01_… (part C)

1. Virtual addressing

1.1 The picture so far is a lie

Lectures 1–10 treated an address as naming a byte in physical memory directly. The slides open: “this picture is a lie.” The addresses your program uses — the ones gdb prints, the ones in Lecture 6’s memory map — are virtual addresses. Between the CPU and physical DRAM sits the MMU (memory management unit), which translates every virtual address to a physical one on every access.

  • Physical addressing (simple embedded systems): the CPU puts a physical address on the bus; memory responds. One address space, shared by everything.
  • Virtual addressing (every general-purpose system): the CPU issues a virtual address; the MMU translates it, using a page table in memory, to a physical address, which goes to DRAM.

Memory is divided into fixed-size pages (typically 4 KiB). A virtual page is mapped to a physical page (a “frame”), or to disk, or to nothing. The unit of translation and transfer is the page.

1.2 The address split

A virtual address splits into a virtual page number (VPN) and a virtual page offset (VPO); a physical address into a physical page number (PPN) and physical page offset (PPO). The offset is the same in both — translation changes only the page number:

  virtual:  [        VPN        ][  VPO  ]
  physical: [   PPN   ][  PPO = VPO ]

The offset is log2(page size) bits (6 for the 64-byte pages of the demo; 12 for 4 KiB pages). 01_address_translation.py PART A, on the slides’ 14-bit / 64-byte system:

  VA 0x369 = VPN 0xd | VPO 0x29
    page table: VPN 0xd -> PPN 0x2d (rw)
    PA = PPN 0x2d | PPO 0x29 = 0xb69

exactly the slides’ V[0x369] → P[0xB69].


2. The page table, hits and faults

2.1 The page table

The page table is an array in memory, indexed by VPN, whose entries (PTEs) hold a valid bit, a physical page number, and permission bits. The OS maintains one per process; a hardware register (%cr3 on x86-64) points at the current one. To translate, the MMU:

  1. splits the VA into VPN and offset,
  2. reads PTE[VPN],
  3. if valid, forms the physical address PPN : offset — a page hit,
  4. if invalid, raises a page fault.

2.2 Page hit vs page fault

  • A page hit means the page is resident in DRAM; translation succeeds and the access proceeds.
  • A page fault means the page is not in DRAM (never allocated, or paged out to disk). The MMU traps to the OS’s page-fault handler, which:
    • if the access is legal, finds a free physical page (evicting one to disk if needed), loads the page’s contents (from the executable, a file, or swap), updates the PTE, and restarts the faulting instruction — which now hits;
    • if the access is illegal (an unmapped or protected address — a wild pointer), delivers SIGSEGV (the segmentation fault of Lectures 5–6).

Demo 01 PART A shows a fault on the slides’ invalid VPN 0x0B:

  VA 0x2ff = VPN 0xb | VPO 0x3f
    page table: VPN 0xb invalid -> PAGE FAULT

A page fault is not an error by itself — it is the normal mechanism of demand paging: pages are brought into DRAM only when first touched. (This is why the 2 GiB array in Lecture 6’s memory demo cost nothing until used.) Only an illegal access becomes a crash.


3. What virtual memory buys

The slides frame VM as three tools:

3.1 A tool for memory management

Each process gets its own page table, hence its own private virtual address space. Demo 01 PART B:

  process 1: VA 0x080 -> PA 0x440
  process 2: VA 0x080 -> PA 0xa80

The same virtual address maps to different physical pages in different processes. Consequences:

  • Uniform addresses: every program can be linked to load at the same virtual addresses (Lecture 7), because each has its own space.
  • Simplified allocation: a process’s pages can be scattered anywhere in physical memory (or on disk); the page table hides the fragmentation.
  • Sharing: two processes can map the same physical page (shared libraries, Lecture 7; shared memory) by having both page tables point at it.
  • ASLR (Lecture 6): the OS can place each region at a random virtual address by choosing the mapping.

3.2 A tool for protection

Each PTE carries permission bits (read / write / execute, user / supervisor). The MMU checks them on every access, so a write to a read-only page (the text segment, or the stack canary’s non-executable stack of Lecture 6) faults. One process cannot even name another’s physical memory, because its page table has no entry for it. This is hardware-enforced isolation.

3.3 A tool for caching

Physical DRAM is a cache for disk: a virtual address space can be larger than DRAM, with inactive pages kept on disk and brought in on demand (§2.2). The “cache” here has huge blocks (a page) and a huge miss penalty (a disk access, ~10⁶ cycles), so its design choices differ from an SRAM cache: it is fully associative (any page can go in any frame, to minimize the hugely expensive misses), uses write-back (never write-through to disk), and handles misses in software (the OS), since a disk access dwarfs the handler’s cost. Locality is what makes it work: the resident working set of pages fits in DRAM, so page faults are rare after warm-up.


4. The TLB

4.1 The problem

Every memory access now needs a translation, which is itself a memory access (to read the PTE) — and with multi-level tables (§5), several. That would make every load two or more loads. The fix is a cache of translations: the TLB (translation lookaside buffer), a small, fast, highly-associative cache inside the MMU, holding recent VPN→PPN mappings.

4.2 The TLB is a cache, addressed by the VPN

The VPN splits into a TLB index (which set) and a TLB tag (identity within the set), exactly as a memory address splits for a cache (Lecture 10). On an access the MMU probes the TLB:

  • TLB hit: the PPN is returned immediately; no page-table walk.
  • TLB miss: the MMU walks the page table, then inserts the translation into the TLB for next time.

02_tlb.py PART A:

  VA 0x369 (VPN 0x0D): TLB set 1 tag 0x3 -> miss, walk the page table
  VA 0x370 (VPN 0x0D): TLB set 1 tag 0x3 -> HIT, PPN 0x2d

Two addresses in the same page: the first misses the TLB and walks, the second hits and skips the walk. PART B shows page-level locality driving the hit rate:

  good page locality   TLB hit rate 93.8%   avg translation cost 1.25 units
  scattered pages      TLB hit rate  0.0%   avg translation cost 5.00 units

Because programs touch few pages at a time, the TLB hit rate is very high and translation is nearly free; only scattered page access (or a working set larger than the TLB reaches) pays the walk repeatedly.


5. Multi-level page tables

A flat page table has one entry per virtual page, for the whole address space, per process. Demo 01 PART C:

  A 32-bit address space with 4 KiB pages has 2^20 = 1048576 pages, so a FLAT
  page table needs 1048576 entries -- several MiB -- for EVERY process.

Most of that is empty (a process uses a tiny, sparse fraction of its space). A multi-level page table fixes this: a top-level directory whose entries point at second-level tables, each covering a chunk of the VPN space — and only the chunks in use exist.

  A process touching a few pages near the bottom and top of memory needs
  1 directory + 2 second-level tables = 3 tables, not one giant one.

Translation walks the levels: the VPN splits into one index per level, and each level’s table says where the next level’s table is (or faults). x86-64 uses four levels for its 48-bit address space. The TLB still caches the final VPN→PPN result, so a TLB hit skips the entire multi-level walk — which is exactly why the TLB matters more as the walk gets longer.


Summary

  • Every address a program uses is virtual; the MMU translates it to physical on every access, a page at a time.
  • A virtual address is VPN | offset; the page table (one per process, indexed by VPN) maps the VPN to a PPN, carrying the offset across. A valid PTE is a page hit; an invalid one is a page fault, handled by the OS (demand-paging the page, or SIGSEGV for an illegal access).
  • VM is a tool for memory management (private, relocatable, shareable spaces — ASLR, uniform load addresses), protection (per-page permissions, isolation), and caching (DRAM as a fully-associative, write-back, software-managed cache for disk).
  • The TLB caches translations so most accesses skip the page-table walk; the VPN splits into a TLB index and tag like any cache address, and page-level locality keeps its hit rate high.
  • Multi-level page tables store only the mapped regions, so a sparse address space is cheap; x86-64 uses four levels, and the TLB caches the whole walk’s result.

Common pitfalls

  • Thinking addresses are physical. They are virtual; the same virtual address means different memory in different processes.
  • Treating every page fault as a crash. A fault is the normal demand-paging mechanism; only an illegal access becomes SIGSEGV.
  • Forgetting the offset is unchanged. Translation replaces the page number only; the low bits pass straight through.
  • Confusing the TLB with the data cache. The TLB caches translations (VPN→PPN); the data cache caches values (by physical address). Both exist, and both are consulted per access.
  • Imagining a flat page table per process. It would be enormous and mostly empty; real tables are multi-level and sparse.

Exercises

  1. Split it. With 4 KiB pages and 48-bit virtual addresses, how many offset bits and VPN bits are there?

    Answeroffset = log2 4096 = 12 bits; VPN = 48 − 12 = 36 bits. (x86-64 splits those 36 VPN bits into four 9-bit indices, one per page-table level.)
  2. Hit or fault. Using the demo’s page table (08→13, 0A→09, 0B invalid, 0D→2D), translate VA 0x28A and VA 0x2C0 (64-byte pages).

    Answer0x28A = VPN 0x0A, offset 0x0A → PPN 0x09 → PA 0x24A (hit). 0x2C0 = VPN 0x0B, offset 0 → VPN 0x0B invalid → page fault.
  3. Private spaces. Process A and process B both read VA 0x1000 and get different values with no error. Explain.

    AnswerEach has its own page table; VPN(0x1000) maps to different physical pages for A and B. Neither can access the other's page, so both safely use the same virtual address for different data.
  4. TLB reach. A TLB has 64 entries and 4 KiB pages. How much memory can it map without a miss, and why does a large stride hurt it?

    Answer64 × 4 KiB = 256 KiB of distinct pages. A scan with stride ≥ 4 KiB touches a new page every access, so after 64 pages the TLB thrashes — each access misses and walks the table, even though the data cache might be fine.
  5. Why multi-level. A 48-bit address space with 4 KiB pages would need 2³⁶ PTEs flat. Why does a 4-level table avoid allocating most of them?

    AnswerOnly the second-, third- and fourth-level tables covering *mapped* regions are allocated; the directory entries for unused regions are simply marked not-present, so no lower table exists for them. A sparse address space needs only a handful of tables.
  6. Three caches. Name the three caches consulted on a single memory read in a VM system, and what each caches.

    AnswerThe TLB (caches VPN→PPN translations), the page table's own lines in the data cache (caches PTEs), and the data cache (caches the value at the physical address). Lecture 12 walks all three on one access.

No lab is tied to this lecture; VM is assessed on the written assignments and exams. The concepts underpin tshlab and proxylab later (each process’s private space) and explain the memory behavior you measured in Lectures 9–10.

Further reading

  • CS:APP3e §9.1–9.6; practice problems 9.1–9.4. §9.6.4 (multi-level tables) and §9.7 (the Core i7, previewed in Lecture 12).
  • The Intel SDM Vol. 3A, chapter 4 (paging), for the real x86-64 four-level walk and PTE formats.

Preview of Lecture 12: Virtual memory — details

Lecture 12 puts the whole pipeline together on one access: virtual address → TLB → (on a miss) the multi-level page table → physical address → the data cache → the byte. It works the CS:APP “simple memory system” example end to end, then shows the real Intel Core i7 translation (four-level table, the clever trick that overlaps TLB lookup with L1 indexing), and memory-mapped files (mmap) with copy-on-write — the mechanism behind program loading and cheap fork().


术语表 (Glossary, English → 中文)

English中文
virtual memory (VM)虚拟内存
virtual / physical address虚拟 / 物理 地址
address space地址空间
memory management unit (MMU)内存管理单元
page / page frame页 / 页框
virtual page number (VPN) / offset虚拟页号 / 页内偏移
physical page number (PPN)物理页号
page table页表
page table entry (PTE)页表项
valid bit / permission bits有效位 / 权限位
page hit / page fault页命中 / 缺页
demand paging按需调页
page-fault handler缺页处理程序
swap space交换空间
working set (of pages)(页的)工作集
memory protection / isolation内存保护 / 隔离
translation lookaside buffer (TLB)快表(地址转换后备缓冲)
TLB index / TLB tag快表索引 / 快表标记
TLB hit / miss快表命中 / 缺失
multi-level page table多级页表
page directory页目录
page-table walk页表遍历
← 返回课程目录