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

2Virtual Memory System Design

Recall that when we introduced caches in an earlier section, we extensively discussed design tradeoffs. Physical memory is just another layer of the memory hierarchy—where now, memory is a “cache” for disk. We revisit therefore revisit the design questions below, now for our virtual memory system:

3Page Table Details

In this section, we expand on the brief description of the page table from an earlier section on address translation:

Consider the page table layout in Figure 1. The page table is effectively “one giant array” with one entry per virtual page number. A valid entry means that the page is in memory, and each valid entry has a physical page number that can be used to construct a physical address on memory access. Each entry also has status bits, which we discuss below.

Each process has a page table.

Figure 1:Each process has a page table.

4Implementing Protection with Virtual Memory

Each running process has a dedicated page table. In Figure 2, there are three processes that are currently running (either currently running on the processor, or waiting to be run and completed). Each process has a separate page table, and each valid page table entry maps a virtual page from the process to a physical page in memory.

Three processes each have a page table, where valid entries in the page table map to different pages in physical memory. Processes can share physical pages using a write protection mechanism.

Figure 2:Three processes each have a page table, where valid entries in the page table map to different pages in physical memory. Processes can share physical pages using a write protection mechanism.

Remember that a key motivation for virtual memory is to allow safe sharing of a single main memory by multiple processes. We highlight key mechanisms of memory protection:[3]

  1. The flexible placement policy of virtual memory systems means that physical pages allocated to a process do not have to be allocated in order on memory. The mapping is intentionally determined by the “memory manager” (i.e., OS), which organizes page tables so that all virtual pages that should not be shared between processes are mapped to disjoint physical pages.

  2. We also see in Figure 2 that some processes can share physical pages. The write protection bit (write access bit) in page table entries can enable limited sharing of data between two processes.

  3. Portions of physical memory can be marked as protected address space, accessible only by the supervisor mode of the OS. Page tables are placed in this protected address space to ensure that user processes cannot modify any page tables (including its own).

5Status Bits

Page table status bits (1) implement various choices in virtual memory design, and (2) provide process isolation.

5.1Valid Bit

Page table entries track a valid bit to indicate if the page is in memory (DRAM) or only on disk. On each memory access, first check if page table entry is valid.

5.2Dirty Bit

Virtual systems implement a write-back policy, and most do so by tracking a dirty bit. When a page is replaced, check the dirty bit in its page table entry.

In a demand paging system, newly created page tables have all valid bits unset (off).

5.3Write Protection Bit

Address translation is a feature that allows multiple programs to easily share memory, e.g., if they have the same <stdlib.h> library code. To do so, direct two processes to the same physical page by setting the corresponding entry in each page table.

An example is shown in Figure 2. The first entry in the orange page table and the last entry in the green page table share entries. In this way, the two processes can have different virtual page numbers for the same physical page in memory.

The write protection bit, also known as write access bit, can protect a page from being written. This enables processes to share information in a limited way. From P&H 5.7: “To allow another process, say, P1, to read a page owned by process P2, P2 would ask the OS to crate a page table entry for a virtual page in P1’s address space that points to the same physical page that P2 wants to share. The OS could use the write protection bit to proevent P1 from writing the data, if that was P2’s wish.” Common write-protection applications are library code, system data, etc.

If a process violates the write protection policy by attempting to write to a protected page, an OS exception is triggered. Read more about the “memory manager” in this section.

6Hierarchical Page Tables

Footnotes
  1. To be precise, from Computer Architecture, Appendix B: “many processors provide a use bit or reference bit, which is logically set whenever a page is accessed. ... The operating system periodically clears the use bits and later records them so it caan determine which pages were touched during a time period. By keeping track in this way, the operating system can select a page that is among the least recently reference.”

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

  3. Read more about memory protection on Wikipedia.

  4. In earlier versions of RISC-V, the page table base register was called the SPTBR (“S” for “Supervisor”). V1.10 updates the name to SATP (Supervisor Address Translation and Protection). See Volume II: RISC-V Privileged ISA Specification.