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]):

PTE Address=PTBR+(Page Number p×Size of Page Table Entry)

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:

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:

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:

  1. The OS saves Process A's PTBR value into Process A's Process Control Block (PCB).
  2. The OS writes Process B's page table address directly into the PTBR.
  3. 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


what if the page table grows?
When a process requests more memory (for example, via malloc(), 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:

  1. 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.

  2. 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:

  1. Process expands: The process requests a new virtual address (e.g., growing the heap).
  2. OS allocates a new frame: The OS grabs any available 4 KB frame anywhere in physical RAM to hold the process's new data.
  3. 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).
  4. 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


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: 232 Bytes

In a standard 32-bit architecture:

2. Calculating the Number of Pages

To find how many total pages exist in a 4 GB address space using 4 KB pages, you divide the total address space by the page size:

Number of Pages=Total Address SpacePage Size=232 Bytes4 KB

Convert everything to powers of 2:

Now divide:

Number of Pages=232212=232−12=220 Pages220=1,048,576 pages

What This Means for Page Tables

If you have 1,048,576 pages, a single-level (flat) page table must have 1,048,576 entries (PTEs) to map them all:

Size of Page Table=Number of Entries×Size of 1 Entry

Assuming each Page Table Entry (PTE) takes 4 Bytes (32 bits to store physical frame number + permission bits):

Size of Page Table=220 entries×4 Bytes=220×22 Bytes=222 Bytes=4 Megabytes (MB)

Key Takeaway

Every single process in a 32-bit OS would require a 4 MB contiguous array in physical RAM just to store its page table—even if the process only uses a few kilobytes of actual code!

This is why modern operating systems use Multi-Level Page Tables to break that 4 MB block into tiny 4 KB chunks so they only allocate memory for virtual addresses that are actually being used.


4KB in 4GB is so negligible

It feels negligible when looking at one process—4 MB out of 4 GB is only 0.1% of memory!

However, the problem explodes when you look at real operating systems, multi-processing, and sparse address spaces.

Here is why that 4 MB becomes a huge bottleneck in practice:

1. Multiple Processes (The Multiplication Effect)

4 MB isn't shared—every single process requires its own independent page table.
If you have 100 processes running (a clean boot of Windows or Linux easily runs 100–200 background processes/daemons):

Memory Wasted=100 processes×4 MB=400 MB

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:

Total actual memory used by the program: ≈364 KB
Yet, to map this 364 KB program in a single-level page table, the OS must allocate the full 4 MB page table to hold entries for the millions of unused pages in between!

The paradox: The OS spends 4 MB of physical RAM just to manage a process that only uses 364 KB of data—over 1,000% overhead!

3. The Real Nightmare: 64-Bit Architectures

If 4 MB seems small in 32-bit, single-level paging completely collapses in 64-bit systems (264 bytes):
With 4 KB pages and 8 byte entries:

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 364 KB process:

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 10 MB of memory should only need 2,560 page table entries (10 MB4 KB).

However, in a single-level page table array, the hardware forces every page table to be full size (1,048,576 entries) regardless of process 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 (p) and uses it directly as an array index:

PTE Address=PTBR+(p×4 Bytes)

Look at where memory is placed in a typical process:

  1. Code & Data live near the bottom: Page 0, Page 1, Page 2...
  2. The Stack grows down from the very top of virtual address space: Page 1,048,575!
    Even if a program uses only 2 MB total, because the stack lives at address 0xFFFFFFFF (Page index 1,048,575), the underlying array must contain 1,048,576 slots so the MMU can compute PTBR + (1048575 * 4) and land inside valid memory.
    If you made the page table array smaller (say, only 2,560 entries 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:

100 Processes×4 MB per array=400 MB

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):

  1. The top level points to smaller 4 KB sub-tables.
  2. Unused regions of virtual memory simply have NULL pointers in the top-level table.
  3. The OS never allocates physical RAM for the empty space between the Heap and the Stack!

Instead of a 4 MB continuous array per process, a small process only needs 12 KB total for its page tables, bringing 100 processes down from 400 MB to just 1.2 MB.