Paging has a hidden cost. The page table lives in memory, so to read one number from memory the hardware must first read the page table in memory. Every access has become two. And the table itself can be enormous. Two separate problems, two clever fixes.
Problem 1: too slow. The TLB
The same few pages are used again and again: the loop’s code, the array being summed, the stack. So the hardware keeps a small, very fast cache of recent translations: the TLB (translation lookaside buffer). On every access it looks the page number up in the TLB first.
- TLB hit: the TLB already knows the frame. No page table is read at all.
- TLB miss: the hardware walks the page table in memory, finds the frame, and copies the translation into the TLB for next time. If the TLB is full, the least recently used entry is replaced.
The next simulator uses a 4-entry TLB and a two-level page table (more on that in a moment). The program reads address 0x3A7F, then an address in the same page, 0x3A80. Watch the TLB card in the Table walk view.
A miss, then a hit
-
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. The page number is split over 2 table levels of 2, 2 bits. 1 page is in memory so far, and the TLB (a small cache of translations) has room for 4.
- Page faults so far
- 0
- TLB hits / misses
- 0 / 0
- Page-table pages
- 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, which is cut into the indexes 0, 3, one for each table level. 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
- TLB hits / misses
- 0 / 0
- Page-table pages
- 2
-
TLB miss
The TLB does not hold page 3, so the hardware has to read the page table in memory. The TLB starts empty, so the very first access always misses.
- Virtual address
- 0x3A7F
- Page number
- 3
- Offset
- 0xA7F
- TLB
- miss
- Page faults so far
- 0
- TLB hits / misses
- 0 / 1
- Page-table pages
- 2
-
Walk level 1
Level 1: entry 0 points to the level 2 table. Read it next.
- Virtual address
- 0x3A7F
- Page number
- 3
- Offset
- 0xA7F
- TLB
- miss
- Page faults so far
- 0
- TLB hits / misses
- 0 / 1
- Page-table pages
- 2
-
Walk level 2
Level 2: entry 3 says page 3 is in frame 7 (its present bit is 1).
- Virtual address
- 0x3A7F
- Page number
- 3
- Offset
- 0xA7F
- TLB
- miss
- Frame
- 7
- Page faults so far
- 0
- TLB hits / misses
- 0 / 1
- Page-table pages
- 2
-
Frame 7 + offset = 0x7A7F
Frame 7 times the page size 4096, plus the offset 2687, is the physical address 0x7A7F. The translation is also copied into the TLB.
- Virtual address
- 0x3A7F
- Page number
- 3
- Offset
- 0xA7F
- TLB
- miss
- Frame
- 7
- Physical address
- 0x7A7F
- Page faults so far
- 0
- TLB hits / misses
- 0 / 1
- Page-table pages
- 2
-
The program reads 0x3A80
The MMU (the memory management unit, hardware beside the CPU) splits the address. The low 12 bits are the offset, 2688 (0xA80). The rest is the page number, 3, which is cut into the indexes 0, 3, one for each table level. The offset will not change: it is the position inside the page, and the frame is the same size.
- Virtual address
- 0x3A80
- Page number
- 3
- Offset
- 0xA80
- Page faults so far
- 0
- TLB hits / misses
- 0 / 1
- Page-table pages
- 2
-
TLB hit
The TLB already knows page 3: it is in frame 7. No page table is read at all.
- Virtual address
- 0x3A80
- Page number
- 3
- Offset
- 0xA80
- TLB
- hit
- Frame
- 7
- Page faults so far
- 0
- TLB hits / misses
- 1 / 1
- Page-table pages
- 2
-
Frame 7 + offset = 0x7A80
Frame 7 times the page size, plus the offset 2688, is the physical address 0x7A80.
- Virtual address
- 0x3A80
- Page number
- 3
- Offset
- 0xA80
- TLB
- hit
- Frame
- 7
- Physical address
- 0x7A80
- Page faults so far
- 0
- TLB hits / misses
- 1 / 1
- Page-table pages
- 2
Count the page-table reads (lookups): the first access read two entries, one per level. The second read none. The TLB is hardware inside the CPU, so a hit costs almost nothing compared with a trip to memory.
A loop over an array
Here is the pattern that makes a TLB work. A loop reads consecutive numbers of an array that starts at 0x5000. A 4 KiB page holds a lot of them, so the first access of each page misses and the following ones hit. The array crosses into a second page, 0x6000, for two more reads.
Summing an array
-
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. 2 pages are in memory so far, and the TLB (a small cache of translations) has room for 2.
- Page faults so far
- 0
- TLB hits / misses
- 0 / 0
- Page-table pages
- 1
-
The program reads 0x5000
The MMU (the memory management unit, hardware beside the CPU) splits the address. The low 12 bits are the offset, 0 (0x0). The rest is the page number, 5. The offset will not change: it is the position inside the page, and the frame is the same size.
- Virtual address
- 0x5000
- Page number
- 5
- Offset
- 0x0
- Page faults so far
- 0
- TLB hits / misses
- 0 / 0
- Page-table pages
- 1
-
TLB miss
The TLB does not hold page 5, so the hardware has to read the page table in memory.
- Virtual address
- 0x5000
- Page number
- 5
- Offset
- 0x0
- TLB
- miss
- Page faults so far
- 0
- TLB hits / misses
- 0 / 1
- Page-table pages
- 1
-
Read the page table
Level 1: entry 5 says page 5 is in frame 1 (its present bit is 1).
- Virtual address
- 0x5000
- Page number
- 5
- Offset
- 0x0
- TLB
- miss
- Frame
- 1
- Page faults so far
- 0
- TLB hits / misses
- 0 / 1
- Page-table pages
- 1
-
Frame 1 + offset = 0x1000
Frame 1 times the page size 4096, plus the offset 0, is the physical address 0x1000. The translation is also copied into the TLB.
- Virtual address
- 0x5000
- Page number
- 5
- Offset
- 0x0
- TLB
- miss
- Frame
- 1
- Physical address
- 0x1000
- Page faults so far
- 0
- TLB hits / misses
- 0 / 1
- Page-table pages
- 1
-
The program reads 0x5004
The MMU (the memory management unit, hardware beside the CPU) splits the address. The low 12 bits are the offset, 4 (0x4). The rest is the page number, 5. The offset will not change: it is the position inside the page, and the frame is the same size.
- Virtual address
- 0x5004
- Page number
- 5
- Offset
- 0x4
- Page faults so far
- 0
- TLB hits / misses
- 0 / 1
- Page-table pages
- 1
-
TLB hit
The TLB already knows page 5: it is in frame 1. No page table is read at all.
- Virtual address
- 0x5004
- Page number
- 5
- Offset
- 0x4
- TLB
- hit
- Frame
- 1
- Page faults so far
- 0
- TLB hits / misses
- 1 / 1
- Page-table pages
- 1
-
Frame 1 + offset = 0x1004
Frame 1 times the page size, plus the offset 4, is the physical address 0x1004.
- Virtual address
- 0x5004
- Page number
- 5
- Offset
- 0x4
- TLB
- hit
- Frame
- 1
- Physical address
- 0x1004
- Page faults so far
- 0
- TLB hits / misses
- 1 / 1
- Page-table pages
- 1
-
The program reads 0x5008
The MMU (the memory management unit, hardware beside the CPU) splits the address. The low 12 bits are the offset, 8 (0x8). The rest is the page number, 5. The offset will not change: it is the position inside the page, and the frame is the same size.
- Virtual address
- 0x5008
- Page number
- 5
- Offset
- 0x8
- Page faults so far
- 0
- TLB hits / misses
- 1 / 1
- Page-table pages
- 1
-
TLB hit
The TLB already knows page 5: it is in frame 1. No page table is read at all.
- Virtual address
- 0x5008
- Page number
- 5
- Offset
- 0x8
- TLB
- hit
- Frame
- 1
- Page faults so far
- 0
- TLB hits / misses
- 2 / 1
- Page-table pages
- 1
-
Frame 1 + offset = 0x1008
Frame 1 times the page size, plus the offset 8, is the physical address 0x1008.
- Virtual address
- 0x5008
- Page number
- 5
- Offset
- 0x8
- TLB
- hit
- Frame
- 1
- Physical address
- 0x1008
- Page faults so far
- 0
- TLB hits / misses
- 2 / 1
- Page-table pages
- 1
-
The program reads 0x5FFC
The MMU (the memory management unit, hardware beside the CPU) splits the address. The low 12 bits are the offset, 4092 (0xFFC). The rest is the page number, 5. The offset will not change: it is the position inside the page, and the frame is the same size.
- Virtual address
- 0x5FFC
- Page number
- 5
- Offset
- 0xFFC
- Page faults so far
- 0
- TLB hits / misses
- 2 / 1
- Page-table pages
- 1
-
TLB hit
The TLB already knows page 5: it is in frame 1. No page table is read at all.
- Virtual address
- 0x5FFC
- Page number
- 5
- Offset
- 0xFFC
- TLB
- hit
- Frame
- 1
- Page faults so far
- 0
- TLB hits / misses
- 3 / 1
- Page-table pages
- 1
-
Frame 1 + offset = 0x1FFC
Frame 1 times the page size, plus the offset 4092, is the physical address 0x1FFC.
- Virtual address
- 0x5FFC
- Page number
- 5
- Offset
- 0xFFC
- TLB
- hit
- Frame
- 1
- Physical address
- 0x1FFC
- Page faults so far
- 0
- TLB hits / misses
- 3 / 1
- Page-table pages
- 1
-
The program reads 0x6000
The MMU (the memory management unit, hardware beside the CPU) splits the address. The low 12 bits are the offset, 0 (0x0). The rest is the page number, 6. The offset will not change: it is the position inside the page, and the frame is the same size.
- Virtual address
- 0x6000
- Page number
- 6
- Offset
- 0x0
- Page faults so far
- 0
- TLB hits / misses
- 3 / 1
- Page-table pages
- 1
-
TLB miss
The TLB does not hold page 6, so the hardware has to read the page table in memory.
- Virtual address
- 0x6000
- Page number
- 6
- Offset
- 0x0
- TLB
- miss
- Page faults so far
- 0
- TLB hits / misses
- 3 / 2
- Page-table pages
- 1
-
Read the page table
Level 1: entry 6 says page 6 is in frame 2 (its present bit is 1).
- Virtual address
- 0x6000
- Page number
- 6
- Offset
- 0x0
- TLB
- miss
- Frame
- 2
- Page faults so far
- 0
- TLB hits / misses
- 3 / 2
- Page-table pages
- 1
-
Frame 2 + offset = 0x2000
Frame 2 times the page size 4096, plus the offset 0, is the physical address 0x2000. The translation is also copied into the TLB.
- Virtual address
- 0x6000
- Page number
- 6
- Offset
- 0x0
- TLB
- miss
- Frame
- 2
- Physical address
- 0x2000
- Page faults so far
- 0
- TLB hits / misses
- 3 / 2
- Page-table pages
- 1
-
The program reads 0x6004
The MMU (the memory management unit, hardware beside the CPU) splits the address. The low 12 bits are the offset, 4 (0x4). The rest is the page number, 6. The offset will not change: it is the position inside the page, and the frame is the same size.
- Virtual address
- 0x6004
- Page number
- 6
- Offset
- 0x4
- Page faults so far
- 0
- TLB hits / misses
- 3 / 2
- Page-table pages
- 1
-
TLB hit
The TLB already knows page 6: it is in frame 2. No page table is read at all.
- Virtual address
- 0x6004
- Page number
- 6
- Offset
- 0x4
- TLB
- hit
- Frame
- 2
- Page faults so far
- 0
- TLB hits / misses
- 4 / 2
- Page-table pages
- 1
-
Frame 2 + offset = 0x2004
Frame 2 times the page size, plus the offset 4, is the physical address 0x2004.
- Virtual address
- 0x6004
- Page number
- 6
- Offset
- 0x4
- TLB
- hit
- Frame
- 2
- Physical address
- 0x2004
- Page faults so far
- 0
- TLB hits / misses
- 4 / 2
- Page-table pages
- 1
Six accesses and only two misses, so only two page-table walks. Open Try it: set the TLB to 0 and every access walks the table. Set it to 1, and then give the program addresses that alternate between two pages, and a one-entry TLB misses every time. A bigger TLB helps only until it can hold the pages the program keeps using. This is the same reason a cache helps any program that reuses data.
The TLB and context switches
A TLB entry says “page 3 means frame 7”. That is true for one process only. After a context switch (Lesson 2.5) the new process’s page 3 is somewhere else, so the old entries would send it to the wrong frames. The OS must either flush (empty) the TLB on a process switch, or tag every entry with the process it belongs to, as many modern CPUs allow. A flush explains why a switch between processes costs more than one between threads of the same process (Lesson 2.5): the threads share the address space and the entries stay valid, while a process switch starts with a cold TLB and a burst of misses.
Problem 2: too big. Tables in levels
A flat page table has one entry per page of the whole address space, used or not. Take a 48-bit address space with 4 KiB pages. The page number has 48 minus 12 bits. The next program works that out and what a flat table would cost if each entry takes 8 bytes. A last line sizes a table with 9 index bits, which you will need further down.
#include <stdio.h>
int main(void) {
long long vpn_bits = 48 - 12; /* 48-bit addresses, 12 offset bits */
long long entries = 1LL << vpn_bits; /* one entry per page */
long long bytes = entries * 8; /* 8 bytes per entry */
printf("entries: %lld\n", entries);
printf("bytes: %lld\n", bytes);
printf("GiB: %lld\n", bytes >> 30);
printf("one table of 9 index bits: %lld bytes\n", (1LL << 9) * 8);
return 0;
}
Output (one run)
entries: 68719476736 bytes: 549755813888 GiB: 512 one table of 9 index bits: 4096 bytes
That is 512 GiB of page table, for every process, to describe an address space it mostly does not use. Impossible. But look at what a real program uses: a little code at one end, a heap near it, a stack at the other end, and a vast empty gap between. The empty part needs no entries.
So the page number is cut into pieces and the table is built as a tree. The top-level table has an entry for each big region; the entry points to a second-level table only if something in that region is in use; and so on. A region that nothing uses has an empty entry and no table below it.
The simulator below uses a small example you can read: 16-bit addresses, 4 KiB pages, so a 4-bit page number, cut into two levels of 2 bits each. Page 3 is in use. Look at the count of page-table pages: the root and one second-level table, not a table for every region.
A two-level page table
-
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. The page number is split over 2 table levels of 2, 2 bits. 1 page is in memory so far. The root table has 4 entries (2 bits), and one second-level table exists, for the region that holds page 3.
- Page faults so far
- 0
- Page-table pages
- 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, which is cut into the indexes 0, 3, one for each table level. 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
- 2
-
Walk level 1
Level 1: entry 0 points to the level 2 table. Read it next.
- Virtual address
- 0x3A7F
- Page number
- 3
- Offset
- 0xA7F
- Page faults so far
- 0
- Page-table pages
- 2
-
Walk level 2
Level 2: 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
- 2
-
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
- 2
The price: a walk reads one entry per level, so a miss in a 2-level system costs two memory reads instead of one. The TLB is what makes that price affordable: most accesses never walk.
In the real world
x86-64 processors use a four-level page table. A virtual address has 48 bits: four indexes of 9 bits each (36 bits) and a 12-bit offset. Each table has 512 entries (2 to the power 9), and with 8-byte entries a table is exactly one 4 KiB page. The simulator splits the address the same way:
x86-64: four levels of nine bits
-
The address space
48-bit addresses, 4K pages: the offset is the low 12 bits (a page holds 4096 bytes), and the page number is the other 36 bits, so there are 68719476736 pages. The page number is split over 4 table levels of 9, 9, 9, 9 bits. 1 page is in memory so far.
- Page faults so far
- 0
- Page-table pages
- 4
-
The program reads 0x7F1234567ABC
The MMU (the memory management unit, hardware beside the CPU) splits the address. The low 12 bits are the offset, 2748 (0xABC). The rest is the page number, 34110391655, which is cut into the indexes 254, 72, 418, 359, one for each table level. The offset will not change: it is the position inside the page, and the frame is the same size.
- Virtual address
- 0x7F1234567ABC
- Page number
- 34110391655
- Offset
- 0xABC
- Page faults so far
- 0
- Page-table pages
- 4
-
Walk level 1
Level 1: entry 254 points to the level 2 table. Read it next. Each walk step reads one 8-byte entry from one table, and each table fills exactly one page.
- Virtual address
- 0x7F1234567ABC
- Page number
- 34110391655
- Offset
- 0xABC
- Page faults so far
- 0
- Page-table pages
- 4
-
Walk level 2
Level 2: entry 72 points to the level 3 table. Read it next.
- Virtual address
- 0x7F1234567ABC
- Page number
- 34110391655
- Offset
- 0xABC
- Page faults so far
- 0
- Page-table pages
- 4
-
Walk level 3
Level 3: entry 418 points to the level 4 table. Read it next.
- Virtual address
- 0x7F1234567ABC
- Page number
- 34110391655
- Offset
- 0xABC
- Page faults so far
- 0
- Page-table pages
- 4
-
Walk level 4
Level 4: entry 359 says page 34110391655 is in frame 5 (its present bit is 1).
- Virtual address
- 0x7F1234567ABC
- Page number
- 34110391655
- Offset
- 0xABC
- Frame
- 5
- Page faults so far
- 0
- Page-table pages
- 4
-
Frame 5 + offset = 0x000000005ABC
Frame 5 times the page size 4096, plus the offset 2748, is the physical address 0x000000005ABC.
- Virtual address
- 0x7F1234567ABC
- Page number
- 34110391655
- Offset
- 0xABC
- Frame
- 5
- Physical address
- 0x000000005ABC
- Page faults so far
- 0
- Page-table pages
- 4
A process that uses a handful of regions needs only a handful of 4 KiB table pages (here, 4 of them) instead of the flat table’s 512 GiB. Walking to the data takes four reads, and that is why the TLB matters so much on these machines.
The page size does not have to be 4 KiB. Larger huge pages make the offset longer and the page number shorter, so the walk is shorter and one TLB entry covers more memory. With 2 MiB pages the offset is 21 bits and three levels remain:
Huge pages: 2 MiB
-
The address space
48-bit addresses, 2M pages: the offset is the low 21 bits (a page holds 2097152 bytes), and the page number is the other 27 bits, so there are 134217728 pages. The page number is split over 3 table levels of 9, 9, 9 bits. 0 pages are in memory so far.
- Page faults so far
- 0
- Page-table pages
- 1
-
The program reads 0x000000001000
The MMU (the memory management unit, hardware beside the CPU) splits the address. The low 21 bits are the offset, 4096 (0x1000). The rest is the page number, 0, which is cut into the indexes 0, 0, 0, one for each table level. The offset will not change: it is the position inside the page, and the frame is the same size.
- Virtual address
- 0x000000001000
- Page number
- 0
- Offset
- 0x1000
- Page faults so far
- 0
- Page-table pages
- 1
-
Walk level 1
Level 1: entry 0 is empty. No table has been made for this part of the address space.
- Virtual address
- 0x000000001000
- Page number
- 0
- Offset
- 0x1000
- Page faults so far
- 0
- Page-table pages
- 1
-
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, makes 2 new page-table pages, and writes the entry: page 0 is in frame 0. Then the faulting instruction runs again.
- Virtual address
- 0x000000001000
- Page number
- 0
- Offset
- 0x1000
- Frame
- 0
- Page faults so far
- 1
- Page-table pages
- 3
-
Frame 0 + offset = 0x000000001000
The instruction runs again and this time succeeds. Frame 0 times the page size 2097152, plus the offset 4096, is the physical address 0x000000001000.
- Virtual address
- 0x000000001000
- Page number
- 0
- Offset
- 0x1000
- Frame
- 0
- Physical address
- 0x000000001000
- Page faults so far
- 1
- Page-table pages
- 3
With 1 GiB pages the offset is 30 bits and two levels remain:
Huge pages: 1 GiB
-
The address space
48-bit addresses, 1G pages: the offset is the low 30 bits (a page holds 1073741824 bytes), and the page number is the other 18 bits, so there are 262144 pages. The page number is split over 2 table levels of 9, 9 bits. 0 pages are in memory so far.
- Page faults so far
- 0
- Page-table pages
- 1
-
The program reads 0x000000001000
The MMU (the memory management unit, hardware beside the CPU) splits the address. The low 30 bits are the offset, 4096 (0x1000). The rest is the page number, 0, which is cut into the indexes 0, 0, one for each table level. The offset will not change: it is the position inside the page, and the frame is the same size.
- Virtual address
- 0x000000001000
- Page number
- 0
- Offset
- 0x1000
- Page faults so far
- 0
- Page-table pages
- 1
-
Walk level 1
Level 1: entry 0 is empty. No table has been made for this part of the address space.
- Virtual address
- 0x000000001000
- Page number
- 0
- Offset
- 0x1000
- Page faults so far
- 0
- Page-table pages
- 1
-
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, makes 1 new page-table page, and writes the entry: page 0 is in frame 0. Then the faulting instruction runs again.
- Virtual address
- 0x000000001000
- Page number
- 0
- Offset
- 0x1000
- Frame
- 0
- Page faults so far
- 1
- Page-table pages
- 2
-
Frame 0 + offset = 0x000000001000
The instruction runs again and this time succeeds. Frame 0 times the page size 1073741824, plus the offset 4096, is the physical address 0x000000001000.
- Virtual address
- 0x000000001000
- Page number
- 0
- Offset
- 0x1000
- Frame
- 0
- Physical address
- 0x000000001000
- Page faults so far
- 1
- Page-table pages
- 2
The price of a huge page is more internal fragmentation (Lesson 6.3): a program that needs a little memory still gets a whole huge page if it asks for one. :::
Quick check
Quick check
-
The translation is already in the TLB, so the hardware needs no table walk. The instruction still runs and the data is still read.
-
Most of an address space is empty, but a flat table still pays for all of it. A tree only builds tables for the regions in use.
-
The same page number means a different frame in a different process, so stale entries would give wrong answers.
-
More levels, more memory reads before the frame is known. This is why the hit rate of the TLB matters so much.
Recap
- A page-table walk is a memory read per level. The TLB, a small cache of page-to-frame translations in the CPU, avoids it on a hit; a miss walks the table and fills the TLB.
- Programs reuse pages, so most accesses hit. A TLB entry covers a whole page, not one address.
- A context switch between processes must flush the TLB or tag its entries, because page numbers mean different frames in different processes.
- A flat table needs an entry for every page, used or not: far too big for 48-bit addresses. A multi-level table is a tree with tables only for regions in use.
- x86-64 uses four levels of 9 bits and a 12-bit offset. Huge pages shorten the walk.
Next: a page that is in the table but not in memory. What the OS does when the CPU says “page fault”.
Join the discussion