Lecture 12 · Virtual memory: details

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

课程源码 ZIP ↓

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

本篇目录

Maps to: CMU 15-213/15-513/14-513 Fall 2026, Lecture 12 (Oct 1), slide deck 12-vm-details. Reading: CS:APP3e §9.6–9.8. Sources: the public Fall 2026 slides and CS:APP. The worked example reuses Lecture 11’s simulator and reproduces the textbook “simple memory system”; the mmap demo uses the real system calls on an Apple M4 Pro. Code: code/lecture12/. Reading time: ~85 minutes; ~20 more for the demos.


0. How to read this lecture

0.1 Learning goals

After this lecture you should be able to:

  1. Walk a complete access through the TLB → page table → physical address → cache pipeline, computing every field.
  2. Reproduce the CS:APP simple memory system translation by hand and check it.
  3. Describe the Intel Core i7 memory system: a four-level page table and the trick that overlaps TLB lookup with L1 cache indexing.
  4. Explain what the OS does on a page fault, and the kinds of fault.
  5. Explain memory-mapped files and copy-on-write, and how program loading and fork() rest on them.

0.2 Prerequisites and scope

  • Lecture 11: virtual addressing, page tables, the TLB, multi-level tables. This lecture assembles them.
  • Lecture 10: caches — the physical address from translation is a cache address.
  • In scope: CS:APP §9.6.3–9.6.4 (the full pipeline and the Core i7), §9.7 (the Core i7 system), §9.8 (memory mapping). The region/area view of faults is §9.8.1.

0.3 Section → code map

SectionIdeaDemo in code/lecture12/
§1the end-to-end pipeline; the worked example01_worked_example.py
§2page faults and VM areas—
§3the Intel Core i7 memory system—
§4memory-mapped files; copy-on-write02_mmap.c

1. The whole pipeline on one access

1.1 The three caches

A single memory reference in a VM system can consult three caches, of three different things:

  1. the TLB — caches VPN→PPN translations;
  2. the page table, whose own entries sit in the data cache — caches PTEs;
  3. the data cache — caches the value at the physical address.

The flow: the CPU issues a virtual address → the MMU probes the TLB → on a hit it has the PPN; on a miss it walks the page table (reading PTEs, themselves cached) → it forms the physical address → that address is looked up in the data cache → hit returns the byte, miss fetches the block from memory. The crucial realization (slides): the physical address is itself a cache address, splitting into cache tag / set index / block offset (Lecture 10).

1.2 The simple memory system, end to end

The slides’ example has a fixed, small geometry so you can do every step by hand (14-bit VA, 12-bit PA, 64-byte pages; a 16-entry 4-way TLB; a 16-line direct-mapped 4-byte data cache). The field splits follow from the geometry:

  virtual:  [ VPN (8) | VPO (6) ]        TLB: VPN = [ TLBT (6) | TLBI (2) ]
  physical: [ PPN (6) | PPO (6) ]        cache: PA = [ CT (6) | CI (4) | CO (2) ]

01_worked_example.py translates and accesses:

  --- VA 0x369 ---
    VPN 0x0d  VPO 0x29   TLBT 0x03  TLBI 1
      TLB: set 1 tag 0x3 -> HIT, PPN 0x2d
      PA = PPN 0x2d | PPO 0x29 = 0xb69
    cache: PA 0xb69 -> CT 0x2d  CI 0xa  CO 1 -> hit, byte = 0x15

This is exactly the textbook result: VA 0x369 → (TLB hit) PPN 0x2D → PA 0xB69 → (cache hit) byte 0x15. Two more addresses show the other paths:

  --- VA 0x3d4 ---   TLB hit, PA 0x354, cache MISS (fetch the block)
  --- VA 0x020 ---   TLB miss -> walk the page table -> PA 0xa20, cache miss

Every field — VPN, VPO, TLBT, TLBI, PPN, CT, CI, CO — is read straight off the address by the geometry; only the hit/miss outcomes depend on what is resident.


2. Page faults and VM areas

2.1 What the OS tracks

The OS does not just hold a page table; it tracks the process’s areas (also “regions” or VMAs): contiguous ranges of virtual addresses with the same backing and permissions — the code area (from the executable, read-execute), the data area, the heap, the stack, each mapped file, each shared library. A page fault consults these areas to decide what to do.

2.2 Kinds of fault

On a fault, the handler classifies the faulting address against the areas (slides “Types of Faults”):

  • The address is in a valid area, page not resident → demand-page it in (from the executable, a file, or swap), update the PTE, restart the instruction. Normal operation.
  • The address is in a valid area but the access violates permissions (e.g. a write to read-only code) → protection fault → SIGSEGV.
  • The address is in no area at all (a wild or null pointer) → SIGSEGV.
  • A write to a copy-on-write page (a valid area, “read-only” as a COW marker) → copy the page, make it writable, restart (§4).

So “segmentation fault” is the OS saying the faulting address is either outside every area or used in a way the area forbids. The same page-fault machinery serves demand paging, protection, and copy-on-write.


3. The Intel Core i7 memory system

The slides work the real system (CS:APP §9.7). The essentials:

  • 48-bit virtual addresses, 4 KiB pages → a 36-bit VPN split into four 9-bit indices, so the page table is four levels deep. %cr3 points at the top level.
  • A two-level TLB (L1 and L2 TLB) caches translations; a hit skips the four-level walk entirely.
  • Per-core L1 d-cache and i-cache, a per-core L2, and a shared L3, then DRAM (the hierarchy of Lecture 10).
  • PTEs carry not just the PPN and valid bit but permission, accessed, dirty, and a no-execute (NX) bit — the hardware support for Lecture 6’s non-executable stack.

The clever trick (slides “Cute Trick for Speeding Up L1 Access”): the L1 cache is indexed by the page offset bits, which are the same in the virtual and physical address (they are not translated). So the MMU can start the TLB lookup and the L1 cache set lookup in parallel — the cache finds the candidate set from the offset while the TLB produces the PPN, and the tag compare (which needs the PPN) finishes just in time. This hides translation latency behind the cache access. It works only because the L1 is small enough that its set index fits within the page offset (a “virtually-indexed, physically-tagged” cache).


4. Memory-mapped files

4.1 The idea

Demand paging means every resident page is backed by something on disk. Usually that is swap space — but it can be a file. mmap adds an area to the address space backed by a file: after mapping, you read and write the file with ordinary loads and stores, and pages are brought in on demand by the page-fault handler. 02_mmap.c PART A:

  file before:           "ABCDEFGHIJKLMNOP"
  mapped 64 bytes at 0x...; reading m[0..3] = ABCD (no read() call)
  file after m[0..4]=:   "mmap!FGHIJKLMNOP"

A pointer write (memcpy(m, "mmap!", 5)) changed the file on disk — no write() system call. The first touch of the mapping faulted in the file’s bytes; because the mapping is MAP_SHARED, the dirtied page was written back.

4.2 Copy-on-write

A MAP_PRIVATE mapping shares the file’s pages read-only until the process writes one, at which point the page is copied and the write goes to the private copy — copy-on-write (COW). PART B:

  mapped MAP_PRIVATE; this process sees m[0..3] = ABCD
  after writing, THIS process sees m[0..4] = priv!
  file on disk:          "ABCD..."   (unchanged)

The process sees its change; the file does not. The page was shared (and read-only in the page table) until the write faulted, triggering the copy.

4.3 Why it matters

  • Program loading (Lecture 7): the loader mmaps the executable — the text segment as a read-only, executable, shared file mapping, the data segment as copy-on-write. No read() of the whole program; code is paged in as it runs.
  • fork() (Lecture 16): the child’s address space is created by marking all the parent’s pages copy-on-write, so parent and child share physical pages until one writes. fork is cheap because almost nothing is copied up front.
  • Shared memory: two processes mmap the same file MAP_SHARED to communicate through memory.

All of this is the single page-fault mechanism of Lecture 11, pointed at files.


Summary

  • A memory access flows virtual address → TLB → (miss) page-table walk → physical address → data cache → byte, consulting three caches (translations, PTEs, values). The physical address is a cache address (CT/CI/CO).
  • The CS:APP simple memory system example is reproducible: VA 0x369 → TLB hit → PPN 0x2D → PA 0xB69 → cache hit → byte 0x15. Every field is fixed by the geometry.
  • The OS tracks areas; a page fault is demand paging (resident soon), a protection or unmapped fault (SIGSEGV), or a copy-on-write fault.
  • The Core i7 uses a four-level page table for 48-bit addresses, a two-level TLB, NX bits, and overlaps TLB lookup with L1 indexing because the page offset is untranslated.
  • mmap backs memory with a file: MAP_SHARED writes reach the file, MAP_PRIVATE writes are copy-on-write. Program loading and cheap fork() are built on it.

Common pitfalls

  • Forgetting the data cache is physically addressed. Translation comes first; the cache sees the physical address.
  • Thinking mmap reads the file up front. It maps it; pages arrive on demand via faults, and (shared) changes go back on write-back or msync.
  • Expecting MAP_PRIVATE writes to reach the file. They are copy-on-write and stay in the process.
  • Assuming fork() copies memory. It marks pages copy-on-write; copies happen only when one side writes.
  • Confusing a protection fault with an unmapped fault. Both are SIGSEGV, but one is a permission violation in a valid area, the other an address in no area.

Exercises

  1. Work the pipeline. With the demo’s geometry, translate VA 0x3D4: give VPN, VPO, TLBT, TLBI, and (using PPN 0x0D for VPN 0x0F) the PA and its CT/CI/CO.

    AnswerVPN = 0x0F, VPO = 0x14; TLBI = 0x0F & 3 = 3, TLBT = 0x0F >> 2 = 3. PPN 0x0D → PA = 0x0D<<6 | 0x14 = 0x354. CO = 0x354 & 3 = 0, CI = (0x354>>2)&0xF = 0x5, CT = 0x354>>6 = 0x0D.
  2. Fault type. Classify each: (a) first touch of a freshly malloc’d page; (b) *(int*)0 = 1; (c) a write to a string literal; (d) a write to a MAP_PRIVATE page not yet written.

    Answer(a) demand-page fault → page loaded, continue. (b) unmapped address → SIGSEGV. (c) protection fault (read-only text/rodata) → SIGSEGV. (d) copy-on-write fault → page copied, made writable, continue.
  3. Why four levels. For 48-bit VAs and 4 KiB pages, why nine bits per level and four levels?

    AnswerOffset = 12 bits, leaving 36 VPN bits. A page holds 512 = 2⁹ 8-byte PTEs, so each level indexes with 9 bits; 36 / 9 = 4 levels. Each table is exactly one page.
  4. The L1 trick. Why can the Core i7 overlap TLB lookup with L1 cache set selection?

    AnswerThe L1 set index lies within the page offset bits, which are identical in the virtual and physical address (untranslated). So the cache can select the set from the virtual offset immediately, in parallel with the TLB producing the PPN needed only for the tag compare. (Requires the L1 small enough that its index fits in the offset — virtually indexed, physically tagged.)
  5. mmap vs read. Give one advantage of mmaping a large file over reading it into a buffer.

    AnswerOnly the pages actually touched are brought in (demand paging), so you can "open" a file larger than RAM and access parts of it without reading the whole thing; the OS also shares the file's pages across processes and reclaims clean pages without writing them back.
  6. fork cost. A process uses 1 GiB of memory and calls fork(). How much is copied immediately, and when does copying happen?

    AnswerEssentially none up front — the page tables are copied and all pages marked copy-on-write. A physical page is copied only when the parent or child first *writes* it; pages only read are shared forever. So `fork` is fast regardless of memory size.

No lab is specific to this lecture, but mmap and copy-on-write reappear in malloclab (Lectures 13–14, where the heap grows via the kernel) and in the fork/exec of tshlab (Lectures 16–17). The three-caches picture explains the memory performance you measured in Lectures 9–10.

Further reading

  • CS:APP3e §9.6.3–9.8; practice problems 9.5 and the memory-system example 9.6.4. §9.8 (mmap, COW, fork, execve).
  • man 2 mmap, man 2 msync, man 2 mprotect; the Intel SDM Vol. 3A ch. 4 for the PTE format and NX bit.

Preview of Lecture 13: Dynamic memory allocation — basic

You now know how the OS hands a process its pages. Lecture 13 is about how a program carves those pages into the small, arbitrary-sized blocks that malloc and free hand out: the heap, managed with an allocator. It covers the allocator’s goals (throughput and memory utilization), internal and external fragmentation, the implicit free list with boundary tags, placement policies, splitting and coalescing — the ideas you implement in malloclab.


术语表 (Glossary, English → 中文)

English中文
address translation pipeline地址转换流水线
TLB / page table / data cache快表 / 页表 / 数据缓存
physically-indexed / physically-tagged物理索引 / 物理标记
virtually-indexed, physically-tagged虚拟索引、物理标记
page fault handler缺页处理程序
VM area (region / VMA)虚拟内存区域
protection fault保护性错误
segmentation fault (SIGSEGV)段错误
multi-level (4-level) page table多级(四级)页表
no-execute (NX) bit不可执行位
accessed / dirty bit访问位 / 脏位
memory-mapped file内存映射文件
mmap / munmap / msync映射 / 解除映射 / 同步
MAP_SHARED / MAP_PRIVATE共享映射 / 私有映射
copy-on-write (COW)写时复制
demand paging按需调页
swap space交换空间
fork / execve创建进程 / 加载执行
← 返回课程目录