Lesson 6 of 15

Page Table

The Page Table

The page table is the kernel's map from virtual page numbers to physical frame numbers. For each VPN that is mapped, the page table entry (PTE) stores the physical frame number (PFN). The full physical address is:

physical = (PFN << page_bits) | offset

A PTE of 0 means the page is not mapped — accessing it triggers a page fault, and the kernel either loads the page from disk or kills the process with SIGSEGV.

Single-Level Page Table

// pt[vpn] = PFN (0 if not mapped)
unsigned long pt[4] = {0, 5, 0, 7};
// VPN 1 → physical frame 5
// VPN 3 → physical frame 7
// VPN 0, 2 → not mapped (page fault)

Your Implementation

Write void translate(unsigned long pt[], int n, unsigned long vaddr) that prints the physical address, or "page fault" if the page is not mapped.

void translate(unsigned long pt[], int n, unsigned long vaddr) {
    int page_bits = 12;
    unsigned long vpn    = vaddr >> page_bits;
    unsigned long offset = vaddr & 0xFFF;

    if (vpn >= n || pt[vpn] == 0) {
        printf("page fault\n");
        return;
    }
    printf("0x%x\n", (pt[vpn] << page_bits) | offset);
}

Your Task

Implement translate that performs a single-level page table lookup using 4KB pages.

TCC compiler loading...
Loading...
Click "Run" to execute your code.