page tables
why does the memory have limit register but page has ptbr? Shouldn't the page table have both ptbr and ptlr? Yes, exactly right.
While the process's actual program data/code is scattered all over physical RAM in non-contiguous frames, the page table structure itself is stored in RAM as a single, contiguous array of entries.
How the Hardware Indexes the Page Table
Because the page table entries (PTEs) are allocated continuously in memory, indexing into a page table works just like indexing a standard array in C or Java (array[i]):
- Base: The
PTBRgives the starting memory address ofEntry 0. - Offset/Index: The Page Number (
) acts as the array index. - Bound Check:
PTLRensuresdoes not exceed the valid range of this contiguous array.
The Catch with Single-Level Page Tables
Storing the page table contiguously solves the program's fragmentation problem, but it introduces a new problem for the Operating System:
The Page Table itself becomes too large to store contiguously!
For example, on a standard 32-bit architecture with 4 KB pages:
pages. - If each PTE is 4 bytes, every process needs 4 MB of continuous, uninterrupted physical RAM just to store its page table—even if the program only uses a few kilobytes of memory!
The Solution: Multi-Level Paging
To fix this, modern systems break the page table itself into smaller page-sized pieces (hierarchical paging).
Instead of one massive contiguous array, you have a root table (pointed to by PTBR), where each entry points to sub-tables scattered across memory—eliminating the need for large contiguous chunks altogether.
if every process has a separate page table, why do we even need ptbr?
Because every process has its own separate page table, the CPU hardware needs a way to know which page table to look at right now.
The PTBR is not a copy of the page table; it is a single CPU register (a piece of high-speed hardware inside the processor chip itself).
Think of it as a pointer: PTBR holds the physical memory address of the currently running process's page table.
The Core Problem: Hardware Needs One Pointer
Physical RAM contains dozens or hundreds of page tables all residing in memory at the same time:
- Page Table A sits at RAM address
0x0001 0000(for Process A) - Page Table B sits at RAM address
0x0002 0000(for Process B) - Page Table C sits at RAM address
0x0003 0000(for Process C)
When the CPU executes an instruction like MOV EAX, [0x1000], the Memory Management Unit (MMU) in hardware must translate that virtual address 0x1000 into a physical RAM location.
Without a register telling the MMU where to start searching, the hardware would have no idea which page table in RAM belongs to the active process.
What Happens During a Context Switch?
When the operating system stops running Process A and switches to Process B, it performs a context switch.
During a Context Switch:
- The OS saves Process A's PTBR value into Process A's Process Control Block (PCB).
- The OS writes Process B's page table address directly into the PTBR.
- Instantly, all subsequent virtual address translations resolve using Process B's page table!
On x86 processors, the PTBR is known as the CR3 (Control Register 3) register.
Changing the value in CR3 is literally how the CPU switches address spaces from one process to another.
Summary
- In RAM: Every process has its own page table array.
- In CPU Hardware: There is only one PTBR (or one per CPU core).
- The Role of PTBR: It acts as the "steering wheel"—pointing the CPU's address translation unit to the specific page table that belongs to whatever process is executing on that core at this exact microsecond.
what if the page table grows?
When a process requests more memory (for example, viamalloc(), growing the heap, or pushing to the stack), its virtual address space grows, which means new entries must be added to map those new pages.
How the OS handles this growth depends on whether the system uses Single-Level Paging or modern Multi-Level Paging.
Case 1: Single-Level Page Tables (The Naive Approach)
In a basic single-level page table, the table is a single contiguous array in RAM bounded by the PTLR (Page Table Length Register).
If the process grows past its current page table size:
-
Check Capacity: If the existing allocated array has pre-allocated unused slots, the OS simply fills in the next valid frame mapping and increments the PTLR.
-
Reallocation (If Full): If the contiguous page table array runs out of physical contiguous space, the OS must:
- Allocate a larger contiguous block of RAM somewhere else.
- Copy the old page table entries over to the new block.
- Append the new page entries.
- Update PTBR to point to the new physical address and update PTLR with the new size.
- Free the old page table memory block.
The Problem: Reallocating large contiguous arrays in physical memory causes massive memory fragmentation—which defeats the entire purpose of using paging!
Case 2: Multi-Level Page Tables (Modern Systems)
Modern operating systems (x86-64, ARM) don't store page tables as one massive contiguous block.
Instead, they structure page tables as a tree hierarchy (like a 4-level or 5-level radix tree).
In a multi-level system, a page table grows simply by allocating a brand new leaf node (a single 4 KB frame) anywhere in RAM:
- Process expands: The process requests a new virtual address (e.g., growing the heap).
- OS allocates a new frame: The OS grabs any available 4 KB frame anywhere in physical RAM to hold the process's new data.
- OS adds sub-tables if needed: If the directory path to that virtual page doesn't exist yet, the OS allocates another 4 KB frame to act as a child page table (L2, L3, or L4 table).
- Link the pointers: The OS writes the physical address of the newly allocated frame into the parent table entry.
Why Multi-Level Paging Makes Growth Seamless
- No reallocation needed: You never have to copy or relocate existing page tables.
- No contiguous physical memory needed: Every sub-table is exactly 1 page (4 KB) in size, fitting into any free physical frame anywhere in RAM.
- PTBR never changes: The top-level root page table stays at the exact same physical memory address, so the PTBR value remains untouched while the lower levels of the tree grow dynamically.
if a 32 bit architecture has 2^32 ___, ad 4kb page size, then 2^32 / 4kb
Let's complete the math step-by-step to see where that number comes from.
1. Fill in the Blank: Bytes
In a standard 32-bit architecture:
- Memory is byte-addressable (each address refers to
of data). - A 32-bit register can generate
unique addresses. - Therefore, the maximum virtual address space is
.
2. Calculating the Number of Pages
To find how many total pages exist in a
Convert everything to powers of
Now divide:
What This Means for Page Tables
If you have
Assuming each Page Table Entry (PTE) takes
Key Takeaway
Every single process in a 32-bit OS would require a
This is why modern operating systems use Multi-Level Page Tables to break that
4KB in 4GB is so negligible
It feels negligible when looking at one process—
However, the problem explodes when you look at real operating systems, multi-processing, and sparse address spaces.
Here is why that
1. Multiple Processes (The Multiplication Effect)
If you have 100 processes running (a clean boot of Windows or Linux easily runs 100–200 background processes/daemons):
That is 10% of your entire 4 GB RAM consumed purely by OS overhead table structures, before running a single user app.
2. Most Programs Are Mostly Empty (Sparse Allocation)
In virtual memory, process address spaces are sparse. A small program might use:
-
Code & Data:
at the bottom of memory ( 0x00000000) -
Stack:
at the very top of memory ( 0xFFFFFFFF) -
Heap:
somewhere in the middle
Total actual memory used by the program:
Yet, to map this
The paradox: The OS spends
of physical RAM just to manage a process that only uses of data—over 1,000% overhead!
3. The Real Nightmare: 64-Bit Architectures
If
With
- Number of pages:
pages - Page Table Size:
A single process's page table would require 32 million Gigabytes of physical RAM just to exist.
Summary: Multi-Level Paging to the Rescue
By using Multi-Level Paging, the OS only creates page table entries for memory ranges the process is actually using.
For that
- Flat Single-Level Page Table: Costs
of RAM. - Two-Level Page Table: Costs only
of RAM (1 Outer Directory + 2 Inner Tables).
That is over a 99.7% reduction in metadata overhead!
each process need not have 1,048,576 entries. Because each process is not 4gb. So how did you get to 400MB?
That is precisely the exact flaw of Single-Level (Flat) Page Tables!
You are completely right in principle: a process using only
However, in a single-level page table array, the hardware forces every page table to be full size (
Here is why:
The Array Problem: Direct Indexing
A single-level page table is a simple contiguous array in RAM.
When the CPU translates a virtual address, it extracts the Page Number (
Look at where memory is placed in a typical process:
- Code & Data live near the bottom: Page
, Page , Page ... - The Stack grows down from the very top of virtual address space: Page
!
Even if a program uses onlytotal, because the stack lives at address 0xFFFFFFFF(Page index), the underlying array must contain slots so the MMU can compute PTBR + (1048575 * 4)and land inside valid memory.
If you made the page table array smaller (say, onlyentries long), any access to the Stack or Heap at higher virtual addresses would index past the end of the array, crashing the system!
Why Single-Level Tables Force Full Allocation
In a single-level design:
- You cannot leave gaps in an array—an array index maps directly to a offset in physical memory.
- To support accessing high addresses (like the Stack at
0xFFFFFFFF), the array must extend all the way to index. - Every entry takes
, so .
How Multi-Level Paging Solves This
This exact waste is why single-level page tables are never used in real operating systems. By breaking the flat array into a tree of smaller tables (Multi-Level Paging):
- The top level points to smaller
sub-tables. - Unused regions of virtual memory simply have NULL pointers in the top-level table.
- The OS never allocates physical RAM for the empty space between the Heap and the Stack!
Instead of a