CS 208 s22 — Virtual Memory

Table of Contents

1 The Memory We've Been Working With is an Illusion

  • Programs refer to virtual memory addresses
    • movq (%rdi),%rax
    • Conceptually memory is just a very large array of bytes
    • System provides private address space to each process
  • However…
    • We probably don’t have 2w bytes of physical memory
    • We certainly don’t have 2w bytes of physical memory for every program
    • Programs should not interfere with one another
      • Except in certain cases where they want to share code or data

2 Problems We Need to Solve When it Comes to Memory:

2.1 Problem 1: How Does Everything Fit?

vm-problem1.png

2.2 Problem 2: Where Does Everything Go?

vm-problem2.png

2.3 Problem 3: How to Protect One Process From Another?

vm-problem3.png

2.4 Problem 4: How Do Processes Share Memory?

vm-problem4.png

2.5 Solution: Add a Layer of Indirection

vm-indirection.png

  • each process gets it's own virtual address space
  • each byte of physical memory has
    • one physical address (PA)
    • zero, one, or more virtual addresses (VAs)
  • VAs can map to memory, disk, or nowhere (if unused)

vm-mapping.png

3 Virtual Memory Provides Important Capabilities

  • enables system to use memory efficiently by treating it as a cache for a private address space stored on disk
  • simplifies memory management via uniform address space for each process
  • protects the private address space of each process

4 Why Study Virtual Memory?

  • central to the design of many system components across many levels
  • provides the programmer with powerful tools (e.g., memory mapping)
  • misuse of memory can cause strange and insidious bugs

5 Memory Addressing

  • physical addressing: CPU sends exact physical byte address to memory
    • used by early systems, modern microcontrollers, and Cray supercomputers

addressing-physical.png

  • virtual addressing: use memory management unit (MMU) chip to translate virtual addresses to physical byte addresses using lookup table (page table) stored in memory and managed by operating system

addressing-virtual.png

6 Main Memory as a Cache

  • both virtual and physical memory partitioned into blocks (similar to hardware caches) called pages
    • memory can be viewed as a cache for pages stored on disk
    • virtual pages can be unallocated, cached (resident in memory), or uncached (only on disk)
    • a "cache miss" is called a page fault
  • in this context, cache misses are extremely expensive given the relative speed of memory and disk access (100,000x slower)
    • this disparity dominates how memory is organized
    • large page size (4KB to 2MB)
    • sophisticated replacement algorithms (beyond the scope of this course)
  • page table keeps track of mapping virtual pages to physical pages
  • temporal locaity again important for performance
    • when the working set is larger than physical memory, system can grind to a crawl due to continuous swapping of pages (thrashing)

7 Address Translation

addrtrans.png

  • a special register called the page table base register (PTBR) points to the page table
  • similar to how memory addresses split up into different segments for caching, \(n\)-bit virtual addresses split into a \(p\)-bit virtual page offset (VPO) and an (\(n-p\))-bit virtual page number (VPN)
    • VPN selects page table entry (used as an offset from the beginning of the table)
    • physical page address generated by concatenating physical page number (PPN) stored in the page table with the VPO
      • virtual and physical pages are the same size, so the VPO is also a valid offset into the physical page
  • in a page fault (valid bit is zero), MMU triggers an exception and control is transferred to a page fault handler in the operating system
    • page identified for eviction, written to disk (paged out) if it has been modified (if it's only been read, handler can just overwrite it)
    • new page swapped in, page table updated
    • restart interrupted instruction

7.1 Spreadsheet Diagram

  • link
  • page hit: virtual memory reference is in physical memory
  • page miss: virtual memory reference is NOT in physical memory
    • transfer control to page fault handler (exception)
    • handler selects page to be evicted
      • when do we need to write contents back to disk?
    • write new page to physical memory
    • update page table (validate new page, invalidate evicted page)
    • handler restarts faulting instruction

8 Management

  • multiple virtual pages from different processes can be mapped to the same physical page
  • virtual memory helps memory management by:
    • simplifying linking by allowing the same format regardless of actual physical address (e.g., code segment always starts at virtual address 0x400000)
    • simplyfying loading—loader allocated virtual pages for code and static data/literals and points page table entry at the relevant sections of the object file
      • data gets paged in as needed
    • simplifying sharing—instead of separate copies of printf in each process, distinct virtual pages all map to the same physical page
    • simplifying allocation—arrangement of physical pages is invisible to the program, and the operating system can store them anywhere within memory

9 Protection

  • want to enforce things like read-only code and kernel-only memory
  • page table permission bits natural way to do this
    • extend page table to include read/write/execute bits
    • MMU checks them on every memory access
    • if violated, raises exception and kernel sends SIGSEGV (segmentation fault) signal to process

page-table-permissions.png

9.1 Permissions Review

Section Read Write Execute
Stack 1 1 0
Heap 1 1 0
Static Data 1 1 0
Literals 1 0 0
Instructions 1 0 1

10 Quick Check

Which of the following would NOT cause a page fault?1

  • Accessing a memory address that doesn't exist
  • Accessing a part of program data for the first time
  • Dividing by zero
  • Deferencing a NULL pointer

11 Exercises

We are given a 1 MB byte-addressed machine with 4 MB of VM and 128 KB pages. How many bits wide are the following?2

  • VPN
  • PPN
  • Page Offset
  • Page Table Base Register

What is a virtual page? What is a physical page? What is the relationship between them?3

12 Practice

CSPP practice problem 9.2 (p. 807)4

Determine the number of page table entries (PTEs) that are need for the following combinations of virtual address size (\(n\)) and page size (\(P\)).

\(n\) \(P = 2^p\) Number of PTEs
16 4KB  
16 8KB  
32 4KB  
32 8KB  

Footnotes:

1

Dividing by zero will cause an exception, but not a page fault as it does not involve a memory access outside a valid page

2

128 KB is 217 bytes, 1 MB is 220 bytes, 4 MB is 222 bytes

  • VPN: 5 bits (32 128 KB pages fit into 4MB of virtual memory)
  • PPN: 3 bits (8 128 KB pages fit into 1 MB of physical memory)
  • Page Offset: 17 bits (need a 20-bit physical address to specify a byte within 1 MB of physical memory, 3 bits for PPN leaves 17 bits of offset)
  • Page Table Base Register: 20 bits (address in physical memory)
3

A virtual page is a fixed-size block of memory within a virtual address space. A physical page is a fixed-size block of memory within physical memory or on disk. A virtual and physical page are the same size. Once initialized, a virtual page maps to a physical page either in memory or on disk. The hardware/OS translates addresses within virtual pages to the corresponding addresses within physical pages.

4
\(n\) \(P = 2^p\) Number of PTEs
16 4KB 16
16 8KB 8
32 4KB 1 M
32 8KB 512 K