Introduction

Paging removes external fragmentation by giving up contiguity. The logical address space of a process is cut into fixed-size pages, physical memory into frames of the same size, and any page can sit in any free frame. A per-process page table records which frame holds each page, and the hardware consults it on every memory reference.

Paging costs two things: memory for the page tables, and time, because a reference now needs the page table as well as the data. The first cost leads to multilevel, hashed and inverted page tables; the second leads to the translation look-aside buffer (TLB).

Address translation, page-table entries, page-table size, multilevel and inverted tables, the TLB and effective access time, shared pages, and the internal fragmentation that paging keeps are the standard facts. Virtual Memory and Demand Paging builds on it by letting pages live on disk.

Prerequisites

Core concepts

Address split

With page size P=2dP = 2^d bytes and a logical address of nn bits, the low dd bits are the offset and the high n−dn - d bits the page number:

p=⌊aP⌋,o=a mod P,physical=f⋅P+o,p = \left\lfloor \frac{a}{P} \right\rfloor, \qquad o = a \bmod P, \qquad \text{physical} = f \cdot P + o ,

where ff is the frame number found in entry pp of the page table. Because PP is a power of 2, the split is just a choice of bits, and the offset is copied unchanged into the physical address. The number of pages is 2n−d2^{n-d}; with mm physical address bits the number of frames is 2m−d2^{m-d} and a frame number needs m−dm - d bits.

Page-table entry

FieldMeaning
Frame numberm−dm - d bits
Valid (present) bitThe page belongs to the process (and, with virtual memory, is in memory)
Protection bitsRead, write, execute permissions; a violation traps
Dirty (modified) bitSet by hardware on a write; a clean page need not be written back when evicted
Referenced (accessed) bitSet by hardware on any access; used by page-replacement approximations
Cache-disable bitFor memory-mapped device registers

The page table itself is in main memory. The page-table base register (PTBR) points to it, so a context switch changes only one register (plus the TLB state). A page-table length register (PTLR) may bound the number of valid pages.

The TLB

The TLB is a small, fast, fully associative (or set-associative) cache of recent page-to-frame translations, typically tens to a few thousand entries. On a TLB hit the frame number comes from the TLB; on a miss the page table is read from memory and the translation is loaded into the TLB, replacing an entry. The associative lookup hardware is covered with cache organisation in Cache Mapping: Direct, Associative and Set-Associative.

A TLB entry belongs to one address space. On a context switch the OS either flushes the TLB or the entries carry an address-space identifier (ASID), so that entries of several processes can coexist. TLB reach is the memory a full TLB maps: entries times page size, for example 64×4 KB=256 KB64 \times 4 \text{ KB} = 256 \text{ KB}.

How translation works

Paging translation with a TLB
Figure 1. Logical address 13,000 with 4 KB pages splits into page 3 and offset 712. The TLB holds pages 0 and 4 only, so the lookup misses; entry 3 of the page table gives frame 7, the translation is loaded into the TLB, and the physical address is 7×4096+712=29,3847 \times 4096 + 712 = 29{,}384.
  1. Split the logical address into pp and oo.
  2. Search the TLB for pp. On a hit, take ff from the TLB and go to step 5.
  3. On a miss, read entry pp of the page table at PTBR +p×+ p \times (entry size). If the valid bit is 0, trap (an invalid reference, or a page fault under virtual memory).
  4. Check the protection bits, then load (p,f)(p, f) into the TLB.
  5. Form the physical address f⋅P+of \cdot P + o and access memory.

The protection check applies on hits as well: TLB entries carry the protection bits.

A hexadecimal translation

With 4 KB pages, the logical address 0x23A7C has offset 0xA7C (the last three hex digits) and page number 0x23 (35). If page 35 is in frame 0x1F4, the physical address is 0x1F4A7C. With hexadecimal addresses and pages of 2122^{12} bytes, the offset is always the last three hex digits.

Page-table structures

Single-level size

The page table has one entry per page:

table size=2n−d×e bytes,\text{table size} = 2^{n-d} \times e \text{ bytes},

with ee the entry size. For a 32-bit logical address, 4 KB pages (d=12d = 12) and e=4e = 4 bytes: 2202^{20} entries, 4 MB per process, which must be contiguous in memory. With 1 GB of physical memory (m=30m = 30), the frame number takes 18 bits; adding valid, dirty, referenced and three protection bits gives 24 bits, so 3 bytes would do, and the entry is usually rounded up to 4 bytes.

Multilevel (hierarchical) page tables

Page the page table itself. With entry size ee, one page holds P/eP / e entries, so each level resolves lg⁡(P/e)\lg (P / e) bits of the page number. The number of levels is

k=⌈n−dlg⁡(P/e)⌉.k = \left\lceil \frac{n - d}{\lg (P / e)} \right\rceil .

When the division is not exact, the outermost table is the partly filled one.

Logical addressPage sizeEntry sizeBits per levelPage-number bitsLevels
32 bits4 KB4 B10202 (10, 10)
40 bits4 KB4 B10283 (outer 8, then 10, 10)
46 bits8 KB4 B11333 (11, 11, 11)
48 bits4 KB8 B9364 (9, 9, 9, 9)

In the 40-bit case the outer table has 28=2562^8 = 256 entries, which occupy 1 KB, a quarter of a page. The 48-bit, 4 KB, 8-byte case is the four-level layout of x86-64.

Two-level walk. For a 32-bit address with the 10, 10, 12 split, the logical address 0x00403004 has outer index p1=1p_1 = 1, inner index p2=3p_2 = 3 and offset 0x004. If entry 1 of the outer table points to the inner table in frame 0x52, and entry 3 of that inner table holds frame 0x2A1, the physical address is 0x2A1004.

Two-level page-table walk
Figure 2. Two-level translation of 0x00403004 with a 10, 10, 12 split: outer index 1 selects the inner table in frame 0x52, inner index 3 gives frame 0x2A1, and the offset 0x004 completes the physical address 0x2A1004.

Why multilevel saves memory. Only the inner tables that map used pages need to exist. A process with 16 MB of code and data at the bottom of a 32-bit space and a 4 MB stack at the top needs four inner tables for the bottom (each maps 4 MB), one for the stack and one outer table: 6 pages, 24 KB, instead of the 4 MB single-level table. If every page were mapped, the two-level layout would use 1+10241 + 1024 pages, slightly more than one level.

Cost of a walk. A kk-level table needs kk memory reads to find the frame on a TLB miss, then one more for the data: k+1k + 1 accesses in all.

Hashed page tables

For large address spaces the page number is hashed into a table whose entries are chains of (page number, frame, next) elements. A lookup hashes pp and walks the chain. Hashed tables suit sparse 64-bit address spaces; a variant, the clustered page table, maps several consecutive pages per element.

Inverted page tables

An inverted page table has one entry per physical frame, holding the (process id, page number) that occupies it. There is one table for the whole system, and its size depends on physical memory, not on the address spaces.

  • Physical memory of 4 GB with 4 KB frames gives 2202^{20} entries. With 48-bit logical addresses the page number has 36 bits; with a 12-bit process id an entry needs 48 bits = 6 bytes, so the table is 6 MB for the whole machine.
  • A conventional single-level table for the same 48-bit space with 8-byte entries would need 236×82^{36} \times 8 bytes = 512 GB per process.
  • The cost is lookup time: the table is indexed by frame, so finding a page means searching it. A hash table on (pid, page) cuts the search, at the price of one more memory access, and the TLB is consulted first.
  • Shared pages are awkward, because each frame records only one (pid, page) pair.

Shared pages

Reentrant (read-only, non-self-modifying) code can be shared: the page tables of several processes point to the same frames. Forty users running an editor with 150 KB of code and 50 KB of private data each need 40×200=8,00040 \times 200 = 8{,}000 KB without sharing and 150+40×50=2,150150 + 40 \times 50 = 2{,}150 KB with it.

Mathematical explanation: effective access time

Let mm be the memory access time, tt the TLB lookup time, hh the TLB hit ratio and kk the number of page-table levels. With a sequential lookup (TLB first, page-table memory reads only after a TLB miss; the final data access follows on either path):

EAT=h (t+m)+(1−h) (t+k m+m).\text{EAT} = h\,(t + m) + (1 - h)\,(t + k\,m + m).

State which convention a question uses. Three conventions are in use:

  • TLB time counted, data access counted (the formula above). This is the effective memory-access time.
  • TLB time neglected (t=0t = 0): EAT=h m+(1−h)(k+1) m\text{EAT} = h\,m + (1 - h)(k + 1)\,m.
  • Translation time only, without the data access that follows: T=h t+(1−h)(t+k m)T = h\,t + (1 - h)(t + k\,m). Some questions ask "the average time to translate an address"; that excludes the final memory access.

Example. t=20t = 20 ns, m=100m = 100 ns, h=0.9h = 0.9, single-level table (k=1k = 1):

  • full access: 0.9×120+0.1×220=1300.9 \times 120 + 0.1 \times 220 = 130 ns;
  • TLB time neglected: 0.9×100+0.1×200=1100.9 \times 100 + 0.1 \times 200 = 110 ns;
  • translation only: 0.9×20+0.1×120=300.9 \times 20 + 0.1 \times 120 = 30 ns;
  • without any TLB every access costs 2m=2002m = 200 ns, so the TLB saves 70 ns per access under the first convention.

With a two-level table (k=2k = 2) the miss path costs 20+200+100=32020 + 200 + 100 = 320 ns and the EAT becomes 0.9×120+0.1×320=1400.9 \times 120 + 0.1 \times 320 = 140 ns.

Required hit ratio. For the single-level case to reach 125 ns: 120h+220(1−h)=125120h + 220(1 - h) = 125, so h=0.95h = 0.95.

Internal fragmentation and page size

A process of SS bytes needs ⌈S/P⌉\lceil S / P \rceil frames, and the last one is partly empty. A 50,000-byte process with 4 KB pages needs 13 frames (12 full pages hold 49,152 bytes), and the last frame wastes 4096−848=3,2484096 - 848 = 3{,}248 bytes. On average the waste is about half a page per process.

Related material. Tanenbaum (3.5.3) balances the two costs of page size. With average process size ss and entry size ee, the overhead per process is se/Pse/P for the page table plus P/2P/2 for internal fragmentation, which is minimised at P=2seP = \sqrt{2se}. For s=1s = 1 MB and e=8e = 8 bytes that gives 4 KB.

Worked examples

Translation with a page table

Pages of 4 KB, page table: page 0 in frame 5, page 1 in frame 2, page 2 invalid, page 3 in frame 7, page 4 in frame 9.

Logical addressPage, offsetResult
13,0003, 7127×4096+712=29,3847 \times 4096 + 712 = 29{,}384
1,0000, 1,0005×4096+1000=21,4805 \times 4096 + 1000 = 21{,}480
9,0002, 808trap: page 2 invalid
17,0004, 6169×4096+616=37,4809 \times 4096 + 616 = 37{,}480

Sizing a three-level table

46-bit logical addresses, 8 KB pages and 4-byte entries: the offset takes 13 bits, leaving 33 bits of page number. One page holds 8192/4=2048=2118192 / 4 = 2048 = 2^{11} entries, so each level resolves 11 bits and 33/11=333 / 11 = 3 levels are needed. A TLB miss then costs 3 page-table reads plus the data access.

Choosing the page size so that every level fits in one page

32-bit logical addresses and 4-byte entries. What is the smallest page size for which a two-level table works with every table, outer and inner, fitting in one page? And for three levels?

With an offset of dd bits, a page holds 2d/4=2d−22^d / 4 = 2^{d-2} entries, so each level can resolve at most d−2d - 2 bits. The page number has 32−d32 - d bits, and LL levels need

32−d≤L (d−2).32 - d \le L\,(d - 2).
  • Two levels: 32−d≤2d−432 - d \le 2d - 4 gives d≥12d \ge 12. The smallest page is 212=42^{12} = 4 KB, with the 10, 10, 12 split above.
  • Three levels: 32−d≤3d−632 - d \le 3d - 6 gives d≥9.5d \ge 9.5, so d=10d = 10: 1 KB pages, 22 page-number bits split 8, 8, 6 (or 6, 8, 8 with the partly filled table outermost), each table at most 282^8 entries of 4 bytes, exactly one page.

These are the smallest integers dd that satisfy the inequalities, so no smaller page size works for either case.

Important properties and observations

  • Paging has no external fragmentation; it has internal fragmentation in the last page of each process.
  • The offset field is the same width in logical and physical addresses; only the page number is translated.
  • Page-table size depends on the logical address space; inverted-table size depends on physical memory.
  • Multilevel tables save memory only because unused regions need no inner tables; each level adds one memory access on a TLB miss.
  • The TLB must be flushed on a context switch unless its entries carry ASIDs.

Common misconceptions.

  • "A TLB hit needs no memory access." The data access still happens; only the page-table read is saved.
  • "More levels make paging faster." They make page tables smaller and misses slower.
  • "An inverted table is indexed by page number." It is indexed by frame and searched (usually through a hash) by (pid, page).

GATE examination perspective

  • Bit-field questions. Find offset bits from the page size, page-number bits from the logical address, frame bits from the physical memory; add the stated status bits to get the entry size.
  • Page-table size. Entries times entry size; for multilevel tables, count the tables that must exist.
  • Levels. Divide page-number bits by lg⁡(P/e)\lg (P/e) and round up; the outermost level takes the remainder.
  • EAT conventions. Check whether TLB time is given or neglected, whether the data access is included, and how many levels the walk has.
  • Inverted tables. Entries equal the number of frames; the entry holds pid and page number.
  • Hex addresses. With 4 KB pages the offset is the last three hex digits.

Summary

Sources

  1. A. Silberschatz, P. B. Galvin, G. Gagne, Operating System Concepts, 10th ed., Wiley: Chapter 9 (9.3 Paging, 9.4 Structure of the Page Table).
  2. A. S. Tanenbaum, H. Bos, Modern Operating Systems, 4th ed., Pearson: Chapter 3 (3.3.1 Paging, 3.3.2 Page Tables, 3.3.3 Speeding Up Paging, 3.3.4 Page Tables for Large Memories, 3.5.3 Page Size, 3.5.5 Shared Pages).

Next steps