Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

1Learning Outcomes

In this section we discuss:

2Page Tables, Briefly

A page table keeps tracks of the VPN-to-PPN mappings for a given process. There is one page table per process.

We discuss the fine-grained details of page tables in another section.

3Address Translation, Conceptually

3.1Case I: Page Is In Memory

Consider a scenario where a process has a 32-bit virtual address space, and physical memory is 16 KiB and paged into four 4 KiB pages. There are four steps to address translation, as shown by Figure 1’s animation. Fow now, conceptually, a page table entry is valid if it has a physical page number (PPN) and invalid if it is labeled “disk”.

Figure 1:Address Translation, Case I: The target page is in memory.

This case is predicated on our page table entry being valid. A valid page table entry means that the virtual page has a corresponding physical page number, and therefore the page is in memory. Next, let’s explore when the page is not in memory.

3.2Case II: Page Fault

We continue our scenario with the same process. Now, suppose that the next memory access triggers a page fault, as shown in Figure 2’s animation.

Figure 2:Address Translation, Case II: The target page is not in memory, triggering a page fault. With demand paging, a page fault means the page is fetched from disk.

3.3Revisiting the Library Analogy

Let’s understand virtual memory using our library analogy of the memory hierarchy.

This analogy breaks down slightly because the page table is a page number lookup, not an address lookup. But we hope the weak analogy helps.

4The “Memory Manager”

Virtual memory manages the two levels of the memory hierarchy represented by main memory and disk. The “memory manager” performs translation and data mangement and is a combination of hardware (in the CPU) and software (the OS).

There is address translation hardware in the CPU; it is called a memory management unit. This hardware unit splits the virtual address into the virtual page number and offset within the page and accesses the page table. If the page is not currently in memory, the hardware raises a page fault exception.

Upon this page fault exception, transfer control the page fault exception handler—a supervisor-level OS procedure that performs the following:

5Demand Paging

As mentioned in an earlier section, the OS also performs the load part of CALL—meaning, the OS loads a program into a new virtual address space and runs it. Upon starting this new process, what is its memory footprint? No matter what, the OS must allocate enough space in memory for a new page table for this process. But what about memory pages corresponding to data and instructions?

Preloading in numerous pages for every new process is wasteful. For example, with tiny programs like “Hello World”, most pages are never used, so allocating the full virtual address space to memory at startup (i.e., assign every virtual page to a physical page in memory) seems nonsensical.

In a system that uses demand paging,[3] a process begins execution with none of its pages in physical memory. In other words, all of its page table entries are invalid. Then, when a page is actually requested, load the page from disk into memory.

Our VM abstraction allows a program to use the full virtual address space, but some programs use only a tiny amount of memory. Demand paging means that new processes have page tables that start with entries that are all invalid.

Figure 3:Our VM abstraction allows a program to use the full virtual address space, but some programs use only a tiny amount of memory. Demand paging means that new processes have page tables that start with entries that are all invalid.

Demand paging prevents over-allocating memory to a process (and thereby supports running many processes on a limited amount of memory), but the startup cost is high for new processes.

Footnotes
  1. We assume a single-level page table hierarchy in this course. In practice, multi-level (hierarchical) page tables are used to reduce the size of the page table. Read more in the extra section.

  2. Jim Gray’s analogy figure for your reference.

  3. Read more on Wikipedia about demand paging and its counterpart, anticipatory paging.