Skip to main content

Lesson 10 of 26 · 12 min read

Paging and linear page tables

Split virtual addresses into Virtual Page Number (VPN) and page offset, map VPNs to Physical Frame Numbers via page tables, and compute translations step by step.

Four paging translation steps: Split VPN+Off, Read PTE[VPN], Check Valid/Prot, and PA = PFN|Off.
Paging divides virtual and physical memory into fixed-size chunks, keeping low offset bits unchanged across translation.
Course syllabus · lesson 10 of 26
In this lesson

Segmentation requires a whole logical region to fit inside one contiguous physical interval. Paging gives the operating system a different unit of placement. It divides the virtual address space into fixed-size pages and physical memory into same-size frames. A process can see consecutive virtual pages even if their frames sit at opposite ends of RAM. When the kernel needs one more page, any unused frame of that size can hold it.

Virtual address 0x1234 splits into VPN=1 and Offset=0x234. Hardware uses PTBR + VPN*4 to read PTE[1] in RAM, validates Valid=1 and PFN=5, and assembles physical address 0x5234 while PTE flags and paging costs sit below.
Open full-size diagram
Paging replaces contiguous bounds with a per-page lookup table: the page offset never changes during translation.

Translate a virtual address with paging

Virtual 0x35AB, Page table —, Offset 0x5AB, RAM —Virtual0x35ABPage table—Offset0x5ABRAM—

Step 1 of 3

Split a 4 KiB address

Virtual address 0x35AB has VPN 3 and the 12-bit offset 0x5AB.

Split an address at the page boundary

Start with a byte-addressed system and page size P = 2^b bytes. Integer division of the virtual address by P gives the virtual page number (VPN). The remainder gives the byte offset. A power-of-two page size turns those calculations into a shift and a mask. For 4 KiB pages, P = 4096 and b = 12.

Code exampleTrace
VPN    = VA / P = VA >> 12
Offset = VA mod P = VA & 0xFFF
PA     = PFN * P + Offset

The physical frame number, or PFN, comes from the process's mapping. The offset is the same on both sides because both units are 4096 bytes wide. Compare 0x00001FFF and 0x00002000. The first is the last byte of VPN 1 and has offset 0xFFF. The next is the first byte of VPN 2 and has offset zero. If VPN 1 maps to PFN 5 and VPN 2 maps to PFN 13, the two consecutive virtual bytes map to 0x5FFF and 0xD000.

A 32-bit virtual address with 4 KiB pages has 20 VPN bits and 12 offset bits. Since three hexadecimal digits represent 12 bits, take the final three digits for the offset and the preceding five for the VPN. For 0x89ABCDEF the split is VPN = 0x89ABC and Offset = 0xDEF. Physical addresses may have a different width from virtual addresses. Keeping the same 12 offset bits places no requirement on the width of the PFN field.

If a multi-byte instruction accesses bytes on both sides of a page boundary, the processor must account for both mappings. A four-byte read beginning at offset 0xFFE takes two bytes from one page and two from the following virtual page. Either page can fail a protection or residency check. The processor and its exception rules govern how such operations are handled. For translation exercises, split the referenced interval at each page boundary before calculating physical addresses.

Index the linear page table

In a linear page table, the kernel allocates an array of page table entries (PTEs) for an address space. Every representable VPN has a slot. The slot positions encode the VPNs, so the PTE need only record where the page maps and its associated state. A privileged page-table base register (PTBR) names the array's physical base. With a four-byte PTE, the entry address is PTEAddr = PTBR + 4 * VPN.

For example, with PTBR = 0x8000, the entry for VPN 0x123 lives at physical address 0x84AC since 0x123 * 4 = 0x48C. That is the address of a record used for translation. The PFN stored in the record then determines the physical address of the program's data. The hardware uses physical addresses to read the table in this model, which supplies a starting point without recursively translating the address of the table.

Here is a teaching description of the information that may be encoded in a PTE. These names express logical state. They do not define an architecture's bit positions or use C structure layout to pack the entry.

  • Mapped or valid state indicates whether this virtual page belongs to the process's legal address space in the model.
  • Resident or present state tells the translator whether a frame is available now. Real systems may encode some absent-page state in kernel metadata outside the hardware entry. Lesson 12 follows this distinction through a page fault.
  • Permissions constrain loads, stores, and instruction fetches. An entry may also restrict access to supervisor mode. Hardware checks the requested access type against the effective permissions.
  • Physical frame number gives the frame when the page is resident. Multiply by the page size to recover the aligned start address.
  • Accessed and dirty state help the operating system track usage. An access can mark a page as recently used, and a write can mark it modified. Architecture and kernel design determine how these bits are maintained.

The kernel prepares the initial mappings when loading a program and updates them as memory is acquired and released. For a context switch in the linear model it installs the incoming address space's PTBR. Different PTEs may point to the same physical frame, for example when two processes share readable code. The kernel keeps the physical frame allocated until all relevant mappings and other references have been removed.

Count how much memory the page table costs

With 32-bit addresses there are 2^32 possible byte addresses. Each virtual page covers 2^12 bytes. Dividing gives 2^20 = 1,048,576 possible virtual pages. The flat table stores a slot for every one, including the unmapped ones. At four bytes per slot, the cost is approximately four megabytes, exactly 4 MiB in binary units.

Code exampleTrace
Virtual span  = 2^32 bytes = 4 GiB
Virtual pages = 2^32 / 2^12 = 2^20
PTE bytes     = 2^2
Table bytes   = 2^20 * 2^2 = 2^22 = 4,194,304 bytes

If a process maps 64 pages, those mappings cover 256 KiB of virtual space. The linear table still occupies 4 MiB, sixteen times the size of the data coverage in that example. With 100 independent address spaces using this model, the tables alone take 400 MiB. Such a table is dense in representation even when the process's mappings are sparse. As one more scaling exercise, keep the virtual width and page size fixed but double the PTE to eight bytes. The VPN count is unchanged and the table now costs 8 MiB per process.

Page size changes two costs at once. An 8 KiB page uses 13 offset bits and leaves 19 VPN bits in a 32-bit address, so a flat table with four-byte entries takes 2 MiB. Larger pages can also waste more space inside each partly used page. If a separately mapped region needs 6000 bytes and begins on a page boundary, it occupies 8192 bytes with either of these two page sizes. A region needing only 100 bytes occupies 4096 bytes in the first design or 8192 in the second, before accounting for any demand allocation policy. The distinction matters for many small independent mappings. Several ordinary C allocations may instead share a page, depending on the library allocator.

Fixed-size frames remove external fragmentation for requests for a single base frame. Internal waste can appear at the last page of a mapping or at separate protection boundaries. Requests for specially sized physically contiguous regions can still encounter fragmentation. In the ordinary paged address space, frames can be assigned independently, and allocations can grow without moving neighboring virtual pages.

Count the accesses during a load

With the flat table and no translation cache, a byte load involves a PTE read followed by a data read. The data read depends on the result of the first. Instruction fetches also require translation. In a simplified memory model where every physical access takes 80 ns and the two are serialized, one translated load takes 160 ns. CPU caches, overlap, and dedicated translation caches change actual timing, but the example exposes the dependency the hardware must avoid on the common path.

Apply the same accounting to a loop reading four-byte integers from a page-aligned array. The first 1024 integer loads use a single virtual page. With no translation cache, the model performs 1024 PTE reads even though all of them consult the same entry. At element 1024, the next access is to the start of the next virtual page, so both the VPN and the PTE address change. This reuse within a page motivates the translation lookaside buffer in the next lesson, while the size calculation motivates allocating table storage only for needed portions of the address space.

A missing or disallowed mapping ends the normal load path before the memory unit returns a program byte. In this lesson's model, a read-only PTE allows a load and rejects a store even if both use the same VPN and offset. The contents and location of a valid table entry therefore describe both an address computation and a boundary the process is required to respect.

Track the process along with the address

A process's pointer says nothing about the table base. To see why both inputs matter, take two processes that load virtual address 0x00001234. Process A's PTBR is 0x8000 and process B's is 0x480000. Both choose entry 1 and offset 0x234. The entry addresses are 0x8004 and 0x480004, respectively. If A's entry names PFN 5 and B's names PFN 9, their loads return bytes at physical 0x5234 and 0x9234. Switch back to A, restore its PTBR, and the original pointer names A's byte again.

Sharing works through the same mechanism. Suppose both address spaces need the same read-only page of instructions. A may map it at VPN 8 and B at VPN 12. If each affected PTE names PFN 7, both processes execute the same physical bytes even though their virtual addresses differ. If both load at offset 0x040, both physical addresses are 0x7040. The PTEs in the two tables retain their own mapping state and permissions.

Aliasing can also happen inside one page table when two VPNs map to a single frame. With writable aliases, a write through one mapping changes what a read through the other can see. The kernel must preserve the frame as long as either mapping still refers to it. It can remove one virtual view without releasing the bytes that the other view still needs.

Worked trace from a virtual byte to a frame

Set PTBR = 0x8000, use 4 KiB pages and four-byte PTEs, and keep the table resident in this teaching model. Suppose PTE[1] is readable and resident with PFN 5, PTE[2] is readable and resident with PFN 13, and PTE[3] is unmapped.

  1. A load at 0x00001234 splits into VPN 0x1 and offset 0x234. Read the PTE at 0x8000 + 4 = 0x8004. The PFN supplies a physical base of 0x5000, so the requested byte is at 0x5234.
  2. A four-byte read at 0x00001FFE covers VPN 1 at offsets 0xFFE and 0xFFF, then VPN 2 at offsets 0 and 1. Look up the second PTE at 0x8008. The physical bytes are 0x5FFE, 0x5FFF, 0xD000, and 0xD001 in that order.
  3. A one-byte read at 0x00003000 selects PTE[3] at 0x800C. There is no mapping, so the hardware traps and the kernel handles the illegal reference according to the process's environment.

Change one input. Switch from 4 KiB to 8 KiB pages and redo just the address split for 0x00001234. The offset mask becomes 0x1FFF. The new VPN is zero and the offset is 0x1234, so the lookup uses PTE 0. The old PFN cannot be carried across to this calculation. The new table must describe the mapping for the new page boundaries.

Next, cache the translations and pay for table pages only where needed in TLBs and multi-level page tables.

Before you move on

Try it in your workspace

Split virtual address 0x1234 with 4 KiB pages into VPN and 12-bit offset. Given VPN 1 -> PFN 5, compute the physical address (0x5234) and name which bits remain unchanged.

Keep a short note of what you tried, what passed, and what you still need to check.

Your practice record

0 of 3 checked.

Saved in this browser when storage is available. Uncheck any item to revisit it. This is your own record, not an assessment.