Sub topic 6
Multilevel, Hashed and Inverted Page Tables
This chapter assumes binary arithmetic, page-based address translation, process basics, and the meaning of a TLB. A page table maps a process's virtual pages to physical frames. Large address spaces make a simple, single-level page table expensive, so operating systems use multilevel, hashed, or inverted designs.
Why a Single-Level Page Table Is Expensive
Assume:
- Virtual address size: 32 bits
- Page size: 4 KB = \(2^{12}\) bytes
- Page-table entry size: 4 bytes = \(2^2\) bytes
The offset uses 12 bits, leaving \(32 - 12 = 20\) bits for the virtual page number. Therefore, one process needs \(2^{20}\) entries.
The table size is:
\[ 2^{20} \times 2^2 = 2^{22}\text{ bytes} = 4\text{ MB} \]
This table must exist even when the process uses only a small part of its address space.
Multilevel Page Tables
A multilevel page table divides the virtual page number into several indexes. Each index selects an entry in one level. The final entry identifies the physical frame.
For the 32-bit example, divide the 20-bit virtual page number into two 10-bit indexes:
- First-level index: 10 bits
- Second-level index: 10 bits
- Offset: 12 bits
Each level contains \(2^{10} = 1024\) entries. With 4-byte entries, each level occupies:
\[ 1024 \times 4 = 4096\text{ bytes} = 4\text{ KB} \]
The first-level table always requires 4 KB. A second-level table is allocated only for a used region of the virtual address space. This saves memory for sparse processes.
Translation requires one memory access to read the second-level entry and another to access the data, unless the translation is found in the TLB. With more levels, the table uses less allocated space for sparse addresses, but a TLB miss can require more memory accesses.
Hashed Page Tables
A hashed page table hashes the virtual page number to a bucket. Each bucket contains a chain of entries. An entry commonly stores:
- The virtual page number
- The physical frame number
- The process identifier
- A link to the next entry in the bucket
The hash function may produce the same bucket for several pages. This is a collision, handled by searching the bucket's chain. Hashed tables work well with large, sparse address spaces, especially when the address space is larger than the practical size of a direct page table.
Inverted Page Tables
An inverted page table has one entry for each physical frame, rather than one entry for each virtual page. An entry records the process and virtual page currently occupying that frame.
This greatly reduces table size when physical memory is much smaller than the total virtual address space. The cost is lookup: the system must find which physical frame matches a process and virtual page. A hash table is often used to make this search fast. Shared pages and protection metadata also require additional handling because several virtual pages can refer to one physical frame.
| Design | Main index | Memory use | Main cost |
|---|---|---|---|
| Multilevel | Virtual-address indexes | Low for sparse spaces | Extra walks on TLB misses |
| Hashed | Hash of virtual page number | Proportional to mapped pages | Collisions and chain searches |
| Inverted | Physical frame number | One entry per frame | More complex lookup and sharing |
Calculation Pattern
For a multilevel table:
- Subtract offset bits from virtual-address bits.
- Divide the remaining page-number bits among levels.
- Calculate entries per level as \(2^{\text{index bits}}\).
- Multiply entries by the PTE size.
Exercise
A system has 32-bit virtual addresses, 4 KB pages, 4-byte PTEs, and a two-level page table with 10 bits in each index. How large is each level?
Show answer
Each level has 2^10 = 1024 entries. At 4 bytes per entry, each level is 1024 × 4 = 4096 bytes, or 4 KB.