CS 208 w20 lecture 23 outline
1 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
2 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)
- mususe of memory can cause strange and insidious bugs
3 Memory Addressing
- physical addressing: CPU sends exact physical byte address to memory
- used by early systems, modern microcontrollers, and Cray supercomputers
- 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
4 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
- fully associative (single set, pages can go anywhere)
- write-back (delay and batch writes to disk)
- 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)
5 Address Translation
- 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 kernel
- page identified for eviction, 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
5.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
6 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
- simplifying linking by allowing the same format regardless of actual physical address (e.g., code segment always starts at virtual address