Operating Systems Module 6: Memory Lesson 6.3 15 min

Paging and the MMU

Cut memory into equal pages and frames, split every address into a page number and an offset, and let a page table and the MMU do the translation.

Segments left holes because their sizes differ. What if every piece of memory were exactly the same size, so that any free piece could be used for any request? Then a hole could never be “too small”, because every hole is one piece.

That is paging, and it is how almost every modern computer manages memory.

Pages and frames

Memory is cut into equal-sized pieces. A piece of a program’s virtual address space is a page. A piece of physical memory (RAM) is a frame. Pages and frames have the same size, commonly 4 KiB (4096 bytes), so any page fits in any frame.

A program no longer needs one block. Its pages can be scattered over any frames, in any order, and the program still sees one smooth range of addresses starting at 0. This smooth range is its virtual address space. The numbers the program uses are virtual addresses; the numbers on the memory chips are physical addresses. Something must translate one into the other on every access.

An address is a page number and an offset

Take a machine with 16-bit virtual addresses and 4 KiB pages. A page of 4096 bytes needs 12 bits to say which byte inside it you mean (2 to the power 12 is 4096). Those low 12 bits are the offset. The remaining 4 bits are the page number: which page. So the address is cut in two:

  • the top bits are the page number (here 4 bits, so there are 16 pages);
  • the low bits are the offset within the page (here 12 bits).

A page table says where each page lives. It is an array, indexed by the page number. Each entry holds the frame number where that page is, and a present bit: 1 if the page is in memory, 0 if it is not. The MMU (the memory management unit, hardware beside the CPU) reads the page table on every memory access, swaps the page number for the frame number, and keeps the offset as it was.

Follow the address 0x3A7F, with page 3 mapped to frame 7. Switch to the Table walk view to see the lookup too.

Translating 0x3A7F

  1. The address space 16-bit addresses, 4K pages: the offset is the low 12 bits (a page holds 4096 bytes), and the page number is the other 4 bits, so there are 16 pages. One flat page table has an entry for every page. 1 page is in memory so far.
    Page faults so far
    0
    Page-table pages
    1
  2. The program reads 0x3A7F The MMU (the memory management unit, hardware beside the CPU) splits the address. The low 12 bits are the offset, 2687 (0xA7F). The rest is the page number, 3. The offset will not change: it is the position inside the page, and the frame is the same size.
    Virtual address
    0x3A7F
    Page number
    3
    Offset
    0xA7F
    Page faults so far
    0
    Page-table pages
    1
  3. Read the page table Level 1: entry 3 says page 3 is in frame 7 (its present bit is 1).
    Virtual address
    0x3A7F
    Page number
    3
    Offset
    0xA7F
    Frame
    7
    Page faults so far
    0
    Page-table pages
    1
  4. Frame 7 + offset = 0x7A7F Frame 7 times the page size 4096, plus the offset 2687, is the physical address 0x7A7F.
    Virtual address
    0x3A7F
    Page number
    3
    Offset
    0xA7F
    Frame
    7
    Physical address
    0x7A7F
    Page faults so far
    0
    Page-table pages
    1

The offset 0xA7F came out exactly as it went in. That is why pages and frames are the same size: the position inside the page is the same position inside the frame, so the hardware only has to replace the top bits. In numbers: frame 7 starts at 7 times 4096, and adding the offset 0xA7F gives 0x7A7F.

Open Try it and change the numbers. Try a page size of 1K, and see how many bits the offset takes. Try an address that is too big for the number of bits, or a page size that is not a power of two such as 3000. The simulator tells you why it cannot do those, next to the field. (The first is impossible because an address has only so many bits. The second is impossible because the split is made by cutting bits, and that only works for sizes that are a power of two.)

Three accesses, three frames

The program does not know that its pages are scattered. Here page 0 is in frame 2, page 1 in frame 5 and page 3 in frame 7. Three addresses, one in each.

A scattered address space

  1. The address space 16-bit addresses, 4K pages: the offset is the low 12 bits (a page holds 4096 bytes), and the page number is the other 4 bits, so there are 16 pages. One flat page table has an entry for every page. 3 pages are in memory so far.
    Page faults so far
    0
    Page-table pages
    1
  2. The program reads 0x0010 The MMU (the memory management unit, hardware beside the CPU) splits the address. The low 12 bits are the offset, 16 (0x10). The rest is the page number, 0. The offset will not change: it is the position inside the page, and the frame is the same size.
    Virtual address
    0x0010
    Page number
    0
    Offset
    0x10
    Page faults so far
    0
    Page-table pages
    1
  3. Read the page table Level 1: entry 0 says page 0 is in frame 2 (its present bit is 1).
    Virtual address
    0x0010
    Page number
    0
    Offset
    0x10
    Frame
    2
    Page faults so far
    0
    Page-table pages
    1
  4. Frame 2 + offset = 0x2010 Frame 2 times the page size 4096, plus the offset 16, is the physical address 0x2010. The program's first page, at the bottom of its address space, is actually in frame 2.
    Virtual address
    0x0010
    Page number
    0
    Offset
    0x10
    Frame
    2
    Physical address
    0x2010
    Page faults so far
    0
    Page-table pages
    1
  5. The program reads 0x1FF0 The MMU (the memory management unit, hardware beside the CPU) splits the address. The low 12 bits are the offset, 4080 (0xFF0). The rest is the page number, 1. The offset will not change: it is the position inside the page, and the frame is the same size.
    Virtual address
    0x1FF0
    Page number
    1
    Offset
    0xFF0
    Page faults so far
    0
    Page-table pages
    1
  6. Read the page table Level 1: entry 1 says page 1 is in frame 5 (its present bit is 1).
    Virtual address
    0x1FF0
    Page number
    1
    Offset
    0xFF0
    Frame
    5
    Page faults so far
    0
    Page-table pages
    1
  7. Frame 5 + offset = 0x5FF0 Frame 5 times the page size 4096, plus the offset 4080, is the physical address 0x5FF0. Page 1 is in frame 5, which is nowhere near frame 2.
    Virtual address
    0x1FF0
    Page number
    1
    Offset
    0xFF0
    Frame
    5
    Physical address
    0x5FF0
    Page faults so far
    0
    Page-table pages
    1
  8. The program reads 0x3A7F The MMU (the memory management unit, hardware beside the CPU) splits the address. The low 12 bits are the offset, 2687 (0xA7F). The rest is the page number, 3. The offset will not change: it is the position inside the page, and the frame is the same size.
    Virtual address
    0x3A7F
    Page number
    3
    Offset
    0xA7F
    Page faults so far
    0
    Page-table pages
    1
  9. Read the page table Level 1: entry 3 says page 3 is in frame 7 (its present bit is 1).
    Virtual address
    0x3A7F
    Page number
    3
    Offset
    0xA7F
    Frame
    7
    Page faults so far
    0
    Page-table pages
    1
  10. Frame 7 + offset = 0x7A7F Frame 7 times the page size 4096, plus the offset 2687, is the physical address 0x7A7F.
    Virtual address
    0x3A7F
    Page number
    3
    Offset
    0xA7F
    Frame
    7
    Physical address
    0x7A7F
    Page faults so far
    0
    Page-table pages
    1

Every access needed one lookup in the page table (the Table walk view shows it). That is a cost we will deal with in the next lesson.

A page that is not there

What if the page number points at a page that has no frame? The page table entry has its present bit at 0, and the MMU cannot translate. It raises a page fault, which is handled by the OS (Lesson 6.5 is all about this). Here the program uses page 9, which is not mapped.

A page that is not present

  1. The address space 16-bit addresses, 4K pages: the offset is the low 12 bits (a page holds 4096 bytes), and the page number is the other 4 bits, so there are 16 pages. One flat page table has an entry for every page. 1 page is in memory so far.
    Page faults so far
    0
    Page-table pages
    1
  2. The program reads 0x9123 The MMU (the memory management unit, hardware beside the CPU) splits the address. The low 12 bits are the offset, 291 (0x123). The rest is the page number, 9. The offset will not change: it is the position inside the page, and the frame is the same size.
    Virtual address
    0x9123
    Page number
    9
    Offset
    0x123
    Page faults so far
    0
    Page-table pages
    1
  3. Read the page table Level 1: entry 9 has its present bit at 0: page 9 is not in memory.
    Virtual address
    0x9123
    Page number
    9
    Offset
    0x123
    Page faults so far
    0
    Page-table pages
    1
  4. Page fault The page is not present, so the CPU raises a page fault and runs the OS (Lesson 1.3). The OS finds the page on disk, picks the free frame 0, copies the page in, and writes the entry: page 9 is in frame 0. Then the faulting instruction runs again.
    Virtual address
    0x9123
    Page number
    9
    Offset
    0x123
    Frame
    0
    Page faults so far
    1
    Page-table pages
    1
  5. Frame 0 + offset = 0x0123 The instruction runs again and this time succeeds. Frame 0 times the page size 4096, plus the offset 291, is the physical address 0x0123.
    Virtual address
    0x9123
    Page number
    9
    Offset
    0x123
    Frame
    0
    Physical address
    0x0123
    Page faults so far
    1
    Page-table pages
    1

Here the OS found the page, put it in the lowest free frame (frame 0) and retried the access, so the final address is 0x123. In the next lessons we look at what the OS really does, and at how the page-table lookups can be made faster and smaller.

Wasted space inside a page

Paging has no external fragmentation, but it has a smaller waste. Memory is handed out in whole pages. A program that needs 10000 bytes needs three 4 KiB pages, and the last one is only partly used. The unused rest of the last page is internal fragmentation: waste inside an allocated piece. At most one page’s worth (less one byte) is wasted per allocation, which is far less than the holes that segments can leave. Smaller pages waste less, but make the page table bigger. This C program does the sum:

pages_needed.c
#include <stdio.h>

int main(void) {
    long page = 4096, need = 10000;
    long pages = (need + page - 1) / page;      /* round up to whole pages */
    printf("pages: %ld\n", pages);
    printf("wasted: %ld\n", pages * page - need);
    return 0;
}

Output (one run)

pages: 3
wasted: 2288

One page table per process

Each process has its own page table. The same virtual address 0x3A7F means one frame for one process and a different frame (or none) for another. That is how paging gives each process the private address space that base and bounds gave it (Lesson 6.1), but without needing a single block. A register in the CPU points to the current process’s page table, and the OS changes it on a context switch (Lesson 2.5). Process A cannot name a frame that belongs to B, because its page table has no entry that points there.

Quick check

Quick check

  1. A machine has 4 KiB pages. Which part of a virtual address does translation leave unchanged?
  2. What does a page table entry hold?
  3. Why does every process need its own page table?
  4. Where does the waste of paging come from?

Recap

  • Virtual memory is cut into pages, physical memory into frames, all the same size, so any page fits any frame and there is no external fragmentation.
  • A virtual address is a page number and an offset. With a power-of-two page size the split is just the top bits and the low bits.
  • The page table, an array indexed by page number, holds each page’s frame number and a present bit. The MMU does the lookup on every access and keeps the offset.
  • A page whose present bit is 0 causes a page fault, handled by the OS.
  • Every process has its own page table. Paging wastes only the unused end of the last page: internal fragmentation.

Next: a page-table lookup on every access is slow, and a flat table for a 48-bit address space would be huge. The TLB and multi-level tables fix both.

Your progress is saved in this browser. Sign in to keep it on every device. Free, no password.

Join the discussion