Operating systems / Paging and page tables

Paging and page tables

How a program’s addresses are turned into real places in RAM: memory cut into fixed-size pages, a table that maps them, and one address worked through by hand.

Intermediate6 steps~7 min
Page — a block of the program’s addresses
Frame — a block of real RAM, same size
Page table — says which frame holds each page
// THE SHORT VERSION

In 30 seconds

A program does not use real memory addresses. Its address space is cut into fixed-size pages, and RAM is cut into frames of the same size.

A page table records which frame holds each page. To translate an address, the processor splits it into a page number and an offset, swaps the page number for the frame number, and keeps the offset.

If a page has no frame yet, the processor raises a page fault and the operating system supplies one. Real tables are built in levels so that unused memory costs almost nothing.

Paging and page tables, step by step

STEPS01 / 06

Toy sizes: 16-bit addresses and 8 frames. Real processors also cache recent translations in a TLB, so most accesses skip the table lookup entirely.

STEP 01 / 06 Cut memory into blocks
THE MATH
page size = frame size = 4096 bytes = 0x1000
  1. Step 1: Cut memory into blocks. The addresses a program uses are cut into equal blocks called pages. The real RAM is cut into blocks of the same size, called frames. Because every block is the same size, any page fits in any frame. Here a block is 4096 bytes, a size real systems commonly use.
  2. Step 2: The page table. The operating system keeps a page table for each program: one row per page, saying which frame holds it. Neighbouring pages can sit in frames far apart, and a page may have no frame at all. The program never notices, because it only ever uses page addresses.
  3. Step 3: Split the address. The program reads address 0x2A7C. The low 12 bits say how far into a page the byte is (the offset), since 12 bits count exactly 4096 positions. The bits above them are the page number. So this address means byte 2684 of page 2.
  4. Step 4: Look up, then rebuild. The processor looks up row 2 of the page table and finds frame 7. It swaps the page number for the frame number and keeps the offset as it is. The program asked for 0x2A7C; the byte that is actually read sits at 0x7A7C in RAM.
  5. Step 5: Page fault. The program touches 0x3010, in page 3, whose row is marked not present. The processor stops the instruction and hands control to the operating system: a page fault. The OS finds a free frame, fills it (here from disk), updates the row and reruns the instruction, which now reaches 0x1010.
  6. Step 6: Tables in levels. One flat table for a 32-bit address space needs over a million rows, 4 MB per program, mostly for memory it never uses. So the page number is split again: its first part picks a row in a small top table, which points to a second table only where memory is in use. 64-bit systems chain four or five levels.
VALUES SO FAR
block4096 bytesrule
page 2····table
virtual····input
page no.····index
offset····kept
physical····result
page 3····loaded
split····bits

Spotted a mistake on this page?