ยง21.1โ€“21.3Swap Space โ€ฆ The Page Fault

Part I OSTEP pp. 231โ€“234 ยท ~6 min read

  • memory hierarchy
  • swap space
  • page fault
  • page-fault handler

Every assumption so far has kept address spaces small enough to fit in memory โ€” all of them, at once. Real systems drop that: they add a level to the memory hierarchy , a device with more capacity than memory and (necessarily) less speed โ€” classically a hard disk. (If the big thing were fast, weโ€™d just call it memory.)

The Crux: How To Go Beyond Physical Memory

How can the OS make use of a larger, slower device to transparently provide the illusion of a large virtual address space?

Why want this? Ease of use: with a large address space you allocate naturally and never wonder whether memory has room. The contrast is instructive โ€” older systems used memory overlays, with programmers manually staging code and data into memory before every use. Try imagining it; yuck. And beyond one process, multiprogramming practically demands swapping: early machines could not possibly hold every page of every running program.

21.1 Swap Space

Reserve disk space for moving pages back and forth โ€” swap space โ€” read and written in page-sized units, with the OS remembering each swapped pageโ€™s disk address. A tiny system, mid-juggle:

Figure 21.1 (top): a 4-frame physical memory โ€” three processes squeezed in, one page eachโ€ฆ mostly
Proc 0 [VPN 0] โ“˜PFN 0Proc 1 [VPN 2]PFN 1Proc 1 [VPN 3] โ“˜PFN 2Proc 2 [VPN 0]PFN 3

click a region marked โ“˜ for details

Figure 21.1 (bottom): the 8-block swap space โ€” same colors, one level down the hierarchy
Proc 0 [VPN 1]B0Proc 0 [VPN 2]B1[Free] โ“˜B2Proc 1 [VPN 0]B3Proc 1 [VPN 1]B4Proc 3 [VPN 0] โ“˜B5Proc 2 [VPN 1]B6Proc 3 [VPN 1]B7

click a region marked โ“˜ for details

Three processes share four frames, each with the rest of its pages parked on disk; Proc 3 is entirely swapped out (hence: not running). The system is pretending โ€” convincingly โ€” that memory is far larger than it is. One footnote: swap isnโ€™t the only disk region in this game. Code pages have a pristine copy inside the program binary on disk, so the OS can discard them from memory freely and re-read them later โ€” no swap copy needed.

21.2 The Present Bit

The machinery, top down: TLB hit โ€” as ever, fast. TLB miss โ€” walk the page table; and now the PTEโ€™s present bit speaks. Present = 1: the page is in memory, extract the PFN, install, retry. Present = 0: the page is valid but living on disk. Accessing it is a page fault , and the OSโ€™s page-fault handler takes the stage.

Aside: Swapping Terminology

Odd, isnโ€™t it โ€” a perfectly legal access called a โ€œfaultโ€? It should really be a page miss. The name follows the machinery: when hardware meets something it canโ€™t handle, it raises an exception and transfers to the OS โ€” identical plumbing to a true illegal access. Same trap, same name; different meaning entirely.

21.3 The Page Fault

Whatever the TLB style โ€” hardware-managed or software-managed โ€” page faults are serviced by the OS, in essentially every system. Step through the whole life of one:

The life of a page fault: legal access, absent page โ€” trap, disk read, retry, transparency
Program(user mode)
Hardware
OS(kernel mode)
Disk
A valid page, not at home
step 1 / 10 ยท time flows downward

The page is mapped and legal โ€” but its present bit says 0: the bytes are in swap.

Aside: Why Hardware Doesnโ€™t Handle Page Faults

Hardware designers famously distrust the OS โ€” yet here they hand it the keys. Two reasons. Performance: the disk is so slow that even an OS executing tons of instructions adds negligible overhead. Simplicity: handling a fault means understanding swap space and issuing disk I/O โ€” machinery the hardware neither has nor wants. Both arrows point the same way, and even hardware types can be happy.

The essential trick sits in the PTE: while a page is absent, the bits that would hold its PFN hold its disk address instead. Fault โ†’ look up the disk address โ†’ read the page into a frame โ†’ flip present to 1, write the real PFN โ†’ retry. And note what the process experiences during the milliseconds of disk work: nothing. Itโ€™s blocked, off the CPU, while other ready processes run โ€” one more place the OS overlaps I/O with computation to keep the machine busy.

Check yourself

1.Why support address spaces bigger than physical memory at all?

2.Where does the OS keep a swapped-out page's disk location โ€” and why is swap space not the only disk region involved in paging traffic?

3.The book grumbles that 'page fault' is a misnomer. What actually happened, and why the name anyway?

4.TLB-miss handling split into hardware-managed and software-managed camps โ€” yet page faults are handled by the OS in essentially ALL systems. Why do hardware designers, who famously distrust the OS, allow it?

5.While the page's disk read is in flight, what happens to the faulting process โ€” and what does the OS do with the time?

5 questions