Multilevel Paging in OS
Page the page table itself: split the page number into one index per level, and leave out every inner table that would map nothing.
Paging the Page Table
- Split the virtual address into p1, p2 and offset.
- Read outer entry p1 (the page table base register gives the outer table's address).
- If it is empty, raise a page fault; otherwise it gives an inner table.
- Read inner entry p2 to get the frame number.
- Physical address = frame × 4096 + offset.
Multilevel paging, also called hierarchical paging, breaks one huge page table into a tree of small ones. A flat table for a 32-bit address space has 2²⁰ entries and fills 4 MB of contiguous memory. A multilevel page table splits the page number into pieces and uses each piece to index one level of the tree.
Two level paging on 32-bit x86 splits the address 10 + 10 + 12. The top 10 bits (p1) index the outer table, whose entry holds the physical address of an inner table. The next 10 bits (p2) index that inner table, whose entry holds the frame number. The last 12 bits are the offset inside the page.
Each table has 2¹⁰ = 1,024 entries of 4 bytes, which is exactly 4 KB: one page. Every piece of the page table now fits in a single frame, so the kernel never has to find 4 MB of contiguous memory. Because the walk goes from the outer level inward, Silberschatz also calls this a forward-mapped page table.
- Outer entries point to tables; only inner entries point to data frames
- The offset is never translated
- Every table is one page, so tables can be placed in any free frame
Where the Memory Saving Comes From
Splitting alone saves nothing: a full tree of 1 outer and 1,024 inner tables is slightly bigger than the flat table. The saving comes from leaving inner tables out. Each outer entry covers 2¹⁰ pages × 4 KB = 4 MB of address space, and if nothing in that 4 MB is mapped, the outer entry stays empty and no inner table exists.
A process with code at the bottom, a heap above it and a stack at the top touches three 4 MB regions. It needs one outer table and three inner tables: 4 × 4 KB = 16 KB instead of 4 MB, a saving of 256 times. The huge gap in the middle costs 1,021 empty outer entries of 4 bytes each.
- Unused 4 MB region: one empty 4-byte outer entry
- Used 4 MB region: one 4 KB inner table, even if only one page in it is mapped
- Worst case (every region used): about 0.1% more memory than a flat table
Working Out the Number of Levels
- Offset bits = log₂(page size).
- Page-number bits = address bits − offset bits.
- Bits per level = log₂(page size ÷ entry size).
- Levels = ⌈page-number bits ÷ bits per level⌉.
Exam questions give the virtual address size, the page size and the entry size, then ask how many levels are needed. The rule is that every table must fit in one page.
Take a 46-bit virtual address, 8 KB pages and 4-byte entries. The offset is log₂(8192) = 13 bits, so the page number has 46 − 13 = 33 bits. One page holds 8192 ÷ 4 = 2,048 entries, so each level uses 11 bits. 33 ÷ 11 = 3 levels, split 11 + 11 + 11 + 13.
The same rule explains real hardware. With 48-bit addresses, 4 KB pages and 8-byte entries, a page holds 512 entries (9 bits) and the 36-bit page number needs 4 levels. A full 64-bit address would need ⌈52 ÷ 9⌉ = 6 levels, which is one reason processors do not translate all 64 bits.
| System | Address split | Levels | Reads on a TLB miss |
|---|---|---|---|
| 32-bit x86 | 10 / 10 / 12 | 2 | 3 |
| 32-bit x86 with PAE | 2 / 9 / 9 / 12 | 3 | 4 |
| x86-64, 48-bit | 9 / 9 / 9 / 9 / 12 | 4 | 5 |
| x86-64, 57-bit | 9 / 9 / 9 / 9 / 9 / 12 | 5 | 6 |
- If the division is not exact, the top level is smaller than one page
- Reads on a miss = levels + 1 (the data read)
Terms, operations, and practical uses
Core vocabulary
- Outer tableThe top-level table; its entries hold addresses of inner tables.
- Inner tableThe last level; its entries hold frame numbers and permission bits.
- Page walkReading one entry per level to turn a page number into a frame.
- Forward-mapped tableAnother name for a multilevel table, walked from the outer level in.
Formulas and rules
- Entries per tablepage size ÷ entry size
- Levels⌈(address bits − offset bits) ÷ log₂(entries per table)⌉
- Region per outer entryentries per inner table × page size (4 MB on 32-bit x86)
- Miss costlevels + 1 memory reads
Related lessons
- Page Table StructuresFlat, multilevel, hashed and inverted tables side by side.
- Translation Lookaside BufferThe cache that makes a deep walk rare.
- Hashed Page TableA different answer for sparse 64-bit spaces.
- Virtual MemoryWhy each process gets its own address space.
A two-level page walk
import math
OFFSET_BITS = 12
INNER_BITS = 10
# outer[p1] -> frame holding an inner table; inner[p2] -> data frame
OUTER = {0: 0x105, 1: 0x220, 1023: 0x3FF}
INNER = {
0x105: {0: 0x101, 1: 0x102},
0x220: {0: 0x44C, 1: 0x0A1, 2: 0x77E, 3: 0x991},
0x3FF: {1023: 0x7FF},
}
def split(address):
offset = address & ((1 << OFFSET_BITS) - 1)
p2 = (address >> OFFSET_BITS) & ((1 << INNER_BITS) - 1)
p1 = address >> (OFFSET_BITS + INNER_BITS)
return p1, p2, offset
def translate(address):
"""Walk both levels. Returns (physical or None, table reads)."""
p1, p2, offset = split(address)
inner = OUTER.get(p1) # read 1
if inner is None:
return None, 1
frame = INNER[inner].get(p2) # read 2
if frame is None:
return None, 2
return (frame << OFFSET_BITS) | offset, 2
def levels(address_bits, page_bytes, entry_bytes):
"""Every table must fit in one page."""
offset_bits = int(math.log2(page_bytes))
bits_per_level = int(math.log2(page_bytes // entry_bytes))
return math.ceil((address_bits - offset_bits) / bits_per_level)
def main():
for address in (0x00403004, 0x00404000, 0x00803000):
p1, p2, offset = split(address)
physical, reads = translate(address)
where = "page fault" if physical is None else f"0x{physical:06X}"
print(f"0x{address:08X} p1={p1:<4} p2={p2:<4} offset=0x{offset:03X}"
f" -> {where:<10} ({reads} table reads)")
for bits, page, entry in ((32, 4096, 4), (46, 8192, 4), (48, 4096, 8)):
print(f"{bits}-bit, {page // 1024} KB pages, {entry} B entries: "
f"{levels(bits, page, entry)} levels")
if __name__ == "__main__":
main()#include <cmath>
#include <cstdint>
#include <cstdio>
#include <map>
constexpr int OFFSET_BITS = 12;
constexpr int INNER_BITS = 10;
// outer[p1] -> frame holding an inner table; inner[p2] -> data frame
const std::map<uint32_t, uint32_t> OUTER = {{0, 0x105}, {1, 0x220}, {1023, 0x3FF}};
const std::map<uint32_t, std::map<uint32_t, uint32_t>> INNER = {
{0x105, {{0, 0x101}, {1, 0x102}}},
{0x220, {{0, 0x44C}, {1, 0x0A1}, {2, 0x77E}, {3, 0x991}}},
{0x3FF, {{1023, 0x7FF}}},
};
// Walk both levels. Returns false on a page fault; reads counts table reads.
bool translate(uint32_t address, uint32_t& physical, int& reads) {
uint32_t offset = address & ((1u << OFFSET_BITS) - 1);
uint32_t p2 = (address >> OFFSET_BITS) & ((1u << INNER_BITS) - 1);
uint32_t p1 = address >> (OFFSET_BITS + INNER_BITS);
reads = 1;
auto outer = OUTER.find(p1);
if (outer == OUTER.end()) return false;
reads = 2;
const auto& inner = INNER.at(outer->second);
auto hit = inner.find(p2);
if (hit == inner.end()) return false;
physical = (hit->second << OFFSET_BITS) | offset;
return true;
}
// Every table must fit in one page.
int levels(int addressBits, int pageBytes, int entryBytes) {
int offsetBits = (int)std::log2(pageBytes);
int bitsPerLevel = (int)std::log2(pageBytes / entryBytes);
return (addressBits - offsetBits + bitsPerLevel - 1) / bitsPerLevel;
}
int main() {
for (uint32_t address : {0x00403004u, 0x00404000u, 0x00803000u}) {
uint32_t offset = address & 0xFFF, p2 = (address >> 12) & 0x3FF, p1 = address >> 22;
uint32_t physical = 0;
int reads = 0;
char where[16];
if (translate(address, physical, reads)) std::snprintf(where, sizeof where, "0x%06X", physical);
else std::snprintf(where, sizeof where, "page fault");
std::printf("0x%08X p1=%-4u p2=%-4u offset=0x%03X -> %-10s (%d table reads)\n",
address, p1, p2, offset, where, reads);
}
int cases[3][3] = {{32, 4096, 4}, {46, 8192, 4}, {48, 4096, 8}};
for (auto& c : cases)
std::printf("%d-bit, %d KB pages, %d B entries: %d levels\n",
c[0], c[1] / 1024, c[2], levels(c[0], c[1], c[2]));
}import java.util.Map;
class Main {
static final int OFFSET_BITS = 12;
static final int INNER_BITS = 10;
// outer[p1] -> frame holding an inner table; inner[p2] -> data frame
static final Map<Integer, Integer> OUTER = Map.of(0, 0x105, 1, 0x220, 1023, 0x3FF);
static final Map<Integer, Map<Integer, Integer>> INNER = Map.of(
0x105, Map.of(0, 0x101, 1, 0x102),
0x220, Map.of(0, 0x44C, 1, 0x0A1, 2, 0x77E, 3, 0x991),
0x3FF, Map.of(1023, 0x7FF));
static int reads = 0;
// Walk both levels. Returns -1 on a page fault.
static int translate(int address) {
int offset = address & ((1 << OFFSET_BITS) - 1);
int p2 = (address >>> OFFSET_BITS) & ((1 << INNER_BITS) - 1);
int p1 = address >>> (OFFSET_BITS + INNER_BITS);
reads = 1;
Integer inner = OUTER.get(p1);
if (inner == null) return -1;
reads = 2;
Integer frame = INNER.get(inner).get(p2);
if (frame == null) return -1;
return (frame << OFFSET_BITS) | offset;
}
// Every table must fit in one page.
static int levels(int addressBits, int pageBytes, int entryBytes) {
int offsetBits = Integer.numberOfTrailingZeros(pageBytes);
int bitsPerLevel = Integer.numberOfTrailingZeros(pageBytes / entryBytes);
return (addressBits - offsetBits + bitsPerLevel - 1) / bitsPerLevel;
}
public static void main(String[] args) {
for (int address : new int[] {0x00403004, 0x00404000, 0x00803000}) {
int physical = translate(address);
String where = physical < 0 ? "page fault" : String.format("0x%06X", physical);
System.out.printf("0x%08X p1=%-4d p2=%-4d offset=0x%03X -> %-10s (%d table reads)%n",
address, address >>> 22, (address >>> 12) & 0x3FF, address & 0xFFF, where, reads);
}
int[][] cases = {{32, 4096, 4}, {46, 8192, 4}, {48, 4096, 8}};
for (int[] c : cases)
System.out.printf("%d-bit, %d KB pages, %d B entries: %d levels%n",
c[0], c[1] / 1024, c[2], levels(c[0], c[1], c[2]));
}
}Step through it
Running on 32-bit address 0x00403004, split 10 / 10 / 12, outer entry 1 → inner table at frame 0x220
What a TLB Miss Costs
Every level adds one memory read to a translation that misses the TLB. With a 100 ns memory read, a hit costs 100 ns and a four-level miss costs 500 ns. At a 98% hit rate the average is 0.98 × 100 + 0.02 × 500 = 108 ns, only 8% slower than no paging at all, which is why deep trees are affordable.
Processors cut the miss cost further. A hardware walker reads the tables without trapping to the OS, and page-walk caches keep recent upper-level entries so most walks skip the top levels. Large pages stop the walk early: a 2 MB page on x86-64 ends at the third level, so a miss costs 4 reads instead of 5.
- Hit: 1 memory read (the data)
- Miss: levels + 1 reads
- Large pages remove a level and let one TLB entry cover 512 times more memory
Edge Cases
A walk can stop at any level. An empty outer entry ends it after one read; an inner table that exists but has an invalid entry ends it after two. Both raise a page fault, and the OS then decides whether the address is legal (demand paging) or an error.
The outer table must always be in memory, since the base register points straight at it. Inner tables are ordinary pages, so some systems, Windows among them, can page them out too. A sparse 64-bit space is the case multilevel tables handle worst: a few scattered pages can each need a full chain of tables. The hashed page table and inverted page table approach that problem differently, and page table structures compares all of them.
- Fault at the outer level: 1 read
- Fault at the inner level: 2 reads
- Scattered pages each pay for a whole path of tables