Paging, address translation and page faults

Follow one memory address from the program to the byte in RAM, then see what the system does when the page is not in memory. Memory · Operating systems.

A program reads address 1350. That is not byte 1350 of RAM, and the hardware translates the address for each access. How does it do that, and what happens when the real location does not exist yet?

Why a program's addresses are not RAM addresses

Many programs share one RAM, and the free space in it is rarely one long stretch. If a program needed an unbroken stretch, it could be refused although plenty of memory is free in pieces. So the system gives each program its own virtual addresses, numbered from 0 as if it owned a long private strip, and translates each one to a physical address, the real location in RAM.

To keep the translation cheap, both sides are cut into equal blocks. The program's strip is cut into pages, RAM is cut into frames of the same size, and any page can sit in any frame. Pages and frames are numbered from 0. A page table has one row per page and names the frame that holds it. Each row has a present bit (some books say valid bit) saying whether its page is in memory. The tables show “no frame” when it is not. A row that is not present still exists. Real entries also carry permissions, a separate question from whether the page is in memory.

Here is the example used throughout. Pages and frames are 256 bytes. The program has 6 pages, so its addresses run from 0 to 1535. RAM has 8 frames, and frame 0 belongs to another program. The table says page 0 is in frame 5, page 1 in frame 3, page 2 in frame 1, page 3 in frame 6 and page 5 in frame 2. Page 4 is not in memory. Frames 4 and 7 are free.

Translation takes three moves. Divide the address by the page size: the quotient is the page number and the remainder is the offset, the position inside the page. Find the page number’s mapping. Then build the physical address as frame number times page size, plus the offset. For address 1350, the quotient is 5 and the remainder is 70, because 5 × 256 = 1280. Row 5 names frame 2, so the physical address is 2 × 256 + 70 = 582.

Translate an address, then its neighbour

Read the steps as text

  1. Pages and frames are 256 bytes, so the low 8 bits of an address are the offset and the bits above them are the page number. 5 of the program's 6 pages are in memory.
  2. The program accesses address 1350. Dividing 1350 by 256 gives quotient 5 (the page) and remainder 70 (the offset). Row 5 names frame 2, so the physical address is 2 × 256 + 70 = 582.
  3. The program accesses address 255. Dividing 255 by 256 gives quotient 0 (the page) and remainder 255 (the offset). Row 0 names frame 5, so the physical address is 5 × 256 + 255 = 1535. Before it, address 1350 reached physical address 582.
  4. The program accesses address 256. Dividing 256 by 256 gives quotient 1 (the page) and remainder 0 (the offset). Row 1 names frame 3, so the physical address is 3 × 256 + 0 = 768. Before it, address 255 reached physical address 1535.
  5. Finished with 3 accesses completed, 0 page faults and 0 rejected.

The page number became a frame number, so the page landed somewhere else in RAM. The offset, 70, did not change, because every byte keeps its position inside the block. Neighbouring bytes show it best. Address 255 is the last byte of page 0, so it lands at 5 × 256 + 255 = 1535. Address 256 is one byte further on, the first byte of page 1, and it lands at 3 × 256 + 0 = 768. A program sees one unbroken strip, while its pages sit in whatever frames were free.

A translation lookaside buffer (TLB) caches recent translations and their permissions, so a TLB hit can avoid a page-table walk. A TLB miss alone is not a page fault: the mapping may still be present in the page table. The examples show the table mapping either way.

Page sizes are powers of two, so the hardware does not divide: because 256 is 2 to the power 8, the low 8 bits of an address are the offset and the bits above them are the page number.

When the page is not in memory

A program rarely needs all its pages at once, so the system can load a page only when something touches it. This is called demand paging. In this example page 4 is a permitted, disk-backed page that has not been loaded; its present mark is off. Address 1100 gives page 4 with offset 76, because 4 × 256 = 1024, and row 4 is marked not present.

The hardware cannot finish an access to such a page. It stops the access and hands control to the kernel, with the CPU in kernel mode. That hand-off is a page fault. If the access is permitted and repair succeeds, the kernel can take a free frame, read this disk-backed page into it and mark the row present with that frame number. Then it runs the same instruction again.

Free frames 4 and 7 were available. Any free frame is legal, and the kernel happened to pick frame 7, so the second try finds frame 7 and the access completes at 7 × 256 + 76 = 1868. The CPU is back in user mode, and the program only sees that the instruction took longer.

A page that is not in memory

Read the steps as text

  1. Pages and frames are 256 bytes, so the low 8 bits of an address are the offset and the bits above them are the page number. 5 of the program's 6 pages are in memory.
  2. The program accesses address 1100. Dividing 1100 by 256 gives quotient 4 (the page) and remainder 76 (the offset). Row 4 is marked not present: the page is not in memory, so the hardware stops the access and traps to the kernel.
  3. Page 4 is on disk, so the kernel takes free frame 7, reads the page from disk into it and sets row 4 to frame 7, present.
  4. The same instruction runs again with address 1100. Row 4 now names frame 7, so the physical address is 7 × 256 + 76 = 1868 and the access completes. The CPU returns to user mode.
  5. Finished with 1 access completed, 1 page fault and 0 rejected.

The disk read is the expensive part. A RAM read takes about a hundred nanoseconds. A fast SSD needs tens of microseconds, hundreds of times longer, and a spinning hard disk needs milliseconds. When every frame is already taken, the kernel must also choose a page to throw out before it can load the new one. How it chooses is what the next lesson is about.

A page does not fault on every access, only when it is not in memory. Address 300 is in page 1, in frame 3, so it translates at once to 3 × 256 + 44 = 812. Address 1100 is in page 4, which is not in memory, so it faults and loads. Read again, address 1100 now translates to 1868 with no fault. Three accesses cost one fault: the disk read is paid per page fetched, not per access.

A second touch of the same page

Read the steps as text

  1. Pages and frames are 256 bytes, so the low 8 bits of an address are the offset and the bits above them are the page number. 5 of the program's 6 pages are in memory.
  2. The program accesses address 300. Dividing 300 by 256 gives quotient 1 (the page) and remainder 44 (the offset). Row 1 names frame 3, so the physical address is 3 × 256 + 44 = 812.
  3. The program accesses address 1100. Dividing 1100 by 256 gives quotient 4 (the page) and remainder 76 (the offset). Row 4 is marked not present: the page is not in memory, so the hardware stops the access and traps to the kernel.
  4. Page 4 is on disk, so the kernel takes free frame 7, reads the page from disk into it and sets row 4 to frame 7, present.
  5. The same instruction runs again with address 1100. Row 4 now names frame 7, so the physical address is 7 × 256 + 76 = 1868 and the access completes. The CPU returns to user mode.
  6. The program accesses address 1100. Dividing 1100 by 256 gives quotient 4 (the page) and remainder 76 (the offset). Row 4 names frame 7, so the physical address is 7 × 256 + 76 = 1868. Before it, address 1100 reached physical address 1868.
  7. Finished with 3 accesses completed, 1 page fault and 0 rejected.

An address the program does not own

The program above has pages 0 to 5. Address 1535 is its last byte and translates normally. Address 1536 would be page 6, which the program does not have.

An address outside the program

Read the steps as text

  1. Pages and frames are 256 bytes, so the low 8 bits of an address are the offset and the bits above them are the page number. 5 of the program's 6 pages are in memory.
  2. The program accesses address 1535. Dividing 1535 by 256 gives quotient 5 (the page) and remainder 255 (the offset). Row 5 names frame 2, so the physical address is 2 × 256 + 255 = 767.
  3. The program accesses address 1536. Dividing 1536 by 256 gives page 6, but the table has no row 6. The hardware traps to the kernel, which rejects the access as a segmentation fault and stops the program.
  4. Finished with 1 access completed, 0 page faults and 1 rejected.

In the table, page 4 has a row that says not present, while page 6 has no row, drawn as a dashed line. In this simplified example page 6 has no mapping, so the hardware traps to the kernel. The kernel finds that the address is not inside any page of the program, so there is no page to load. It rejects the access as a segmentation fault and, by default, ends the program. A truly forbidden write can also be rejected; a write to a read-only mapping can instead be a recoverable copy-on-write fault. The test is not whether the page is in memory, but whether the access is allowed.

Recap

  • A virtual address is a page number plus an offset. The page table swaps the page number for a frame number, and the offset never changes.
  • In the illustrated permitted disk-backed case, a not-present mapping causes a fault; after a successful load the kernel retries the instruction.
  • The kernel checks the address and access permissions: some faults are recoverable, while an unmapped address or truly forbidden access can be rejected.
Go deeper

Some books use one valid-invalid bit for two different cases, a page on disk and an address outside the program, so “invalid” on an exam can mean either. Why pages are powers of two. With a 4096-byte page, the low 12 bits of an address are the offset and the remaining high bits are the page number. The hardware slices the bits apart with no division, and translation replaces only the high bits.

Wasted space in the last page. A program of 10000 bytes with 4096-byte pages needs 3 pages, because 2 pages hold only 8192 bytes. The last page holds 1808 bytes of the program, so 2288 bytes of it stay unused. This waste inside a page is called internal fragmentation, and it is the price of fixed-size pages.

Each program has its own table. Two programs can both use virtual address 0 and still reach different bytes, because each has its own page table. Page-table permissions and isolation help keep one program out of another’s private memory; mappings can also be shared deliberately.

Next: FIFO and Belady's anomaly.