Virtual Memory
Virtual Memory: Indirection to physical memory
- The program uses a virtual memory address (program address)
- The virtual memory address is converted into a physical address (real address) {1}
- The physical address indicates a physical location of data
- Physical location can be memory or disk
graph LR V[Virtal: 5000] --> D[Disk: 9100]
Motivation
- Physical memory is much larger than RAM or Cache
- Virtual memory allows memory to appear bigger for running programs
- Support for memory sharing, protection, etc
- Allow a process to exist in non-contiguous memory
- Avoid memory fragmentation
Page Faults
Separate Address Spaces
Each address has its own virtual address space
The OS controls how these virtual addresses are mapped to physical memory
- E.g., virtual address
Memory Protection
Each Page Table entry contains access rights information
- This is hardware enforced (An exception will be triggered if violated)
Virtual Memory vs Caches
Virtual memory is like a cache, but with a different purpose
- Cache - Performance goals
- Virtual Memory - Programmability / Multi-programming goals
Cache lines are called Pages in virtual memory
- A virtual address consists of
- A virtual page number
- A page offset field (less-significant bits)
Pages and Frames
Physical memory is organised into Frames and virtual memory into Pages
- Page = Block of consecutive virtual memory
- Frames = Block of consecutive physical memory
When a process is created:
- The OS allocates the required number of frames to it
- The OS maintains the list of free frames
- The Page Table is used to keep track
Summary of Virtual Memory
Virtual memory adds a level of indirection between the virtual program addresses (VA) and the physical RAM addresses (PA)
This allows us to
- Map memory to disk ("Unlimited" memory to run larger programs)
- Keep programs from accessing each other's memory (Memory protection)
- Fill holes in the RAM address space (Efficiency)
- But we have to translate every single memory access from a VA to a PA
- Page tables for each process to keep track of all translations
- Fast translation via a hardware Translation Look-Aside Buffer (TLB)