Β§18.1A Simple Example And Overview

Part I OSTEP pp. 185–188 Β· ~4 min read

  • paging
  • page
  • page frame
  • page table

The OS solves most space-management problems one of two ways. Chop things into variable-sized pieces β€” segmentation β€” and the space itself fragments. Or chop into fixed-sized pieces: in virtual memory this is paging , an idea going back to the Atlas (early 1960s). The address space becomes fixed-size pages ; physical memory becomes an array of page frames , each holding exactly one page.

The Crux: How To Virtualize Memory With Pages

How can we virtualize memory with pages, so as to avoid the problems of segmentation? What are the basic techniques? How do we make those techniques work well, with minimal space and time overheads?

18.1 A Simple Example And Overview

A tiny example: a 64-byte address space of four 16-byte pages, placed into a 128-byte physical memory of eight frames. Follow the colors β€” each page landed wherever a free frame happened to be:

Figure 18.1: a simple 64-byte address space β€” four 16-byte pages
page 0 of the address space β“˜0page 1 β“˜16page 2 β“˜32page 3 β“˜4864

click a region marked β“˜ for details

Figure 18.2: the same address space scattered through a 128-byte physical memory (eight frames)
reserved for OS β€” frame 0 β“˜0(unused) β€” frame 116page 3 of AS β€” frame 2 β“˜32page 0 of AS β€” frame 3 β“˜48(unused) β€” frame 464page 2 of AS β€” frame 5 β“˜80(unused) β€” frame 696page 1 of AS β€” frame 7 β“˜112128

click a region marked β“˜ for details

Two advantages are already visible. Flexibility: the system supports the address-space abstraction regardless of how the process uses it β€” no assumptions about which way the heap and stack grow. Simplicity: placing the address space took no fit policy, no splitting, no coalescing β€” the OS keeps a free list of pages and grabbed the first four (frames 3, 7, 5, 2). Any free frame serves any page; last chapter’s whole struggle dissolves.

To record where each virtual page landed, the OS keeps a per-process structure β€” the page table β€” storing one translation per virtual page: (VP 0 β†’ PF 3), (VP 1 β†’ PF 7), (VP 2 β†’ PF 5), (VP 3 β†’ PF 2). Per-process matters: another process has its own table, its virtual pages mapping to different frames (sharing aside).

Translation. Suppose the process executes movl 21, %eax. Split the virtual address into two components: the virtual page number (VPN) β€” the top bits, enough to name every page (4 pages β†’ 2 bits) β€” and the offset within the page (16 bytes β†’ 4 bits). Then replace the VPN with the frame number the page table reports, and keep the offset exactly as-is β€” it names a byte within the page, and the page moved as one intact unit:

Figure 18.3, live: the address-translation process β€” VPN replaced by PFN, offset untouched. Try 21.

virtual address

21

010101

β†’

MMU (paging)

1. VPN = top 2 bits = 1 Β· offset = 5

2. PT[1] β†’ PFN 7 βœ“

3. PA = 7Γ—16 + 5 = 117

β†’

physical address

117

VPN→ PFNvalid
031
1 β—€71
251
321

The book’s example: 21 = 010101 β†’ VPN 01 (page 1), offset 0101 (byte 5); PT[1] says PFN 7 (binary 111); result 1110101 = 117. Then: 0 β†’ 48 (page 0 starts at frame 3), 50 β†’ 34 (page 3 lives DOWN at frame 2 β€” scatter in action), 63 β†’ 47 (the very last byte). The VPN chunk’s color matches the page colors in the maps above.

Virtual 21 = binary 010101: VPN 01 β†’ page 1; page table says PFN 7 (111); physical address 1110101 = 117 β€” byte 5 of frame 7, precisely where Figure 18.2 drew page 1’s bytes.

With the overview in hand, the beguiling questions queue up: where are these page tables stored? What exactly is in them, and how big do they get? Doesn’t this make everything slow? Read on.

Check yourself

1.Segmentation chopped the address space into variable-sized pieces. What does paging's fixed-size choice buy, immediately?

2.Translate virtual address 21 in the 64-byte address space (16-byte pages; page table: 0β†’3, 1β†’7, 2β†’5, 3β†’2).

3.Why does the offset pass through translation completely unchanged?

4.Process A and process B both use virtual page 0. How do they not collide?

5.The 64-byte space needs a 2-bit VPN and 4-bit offset. A 32-bit address space with 4KB pages needs…

5 questions