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

Page Faults

Important

Question

What if a piece of data is on disk rather than in memory?

  • If the Page Table indicates that the virtual address is not in memory

A hardware exception will be generated (captured by the OS)

  • The current process suspends, others can resume
  • The OS moves the data to memory and resumes the suspended process

Page faults are generated when the Page Table tells the OS that the page is on disk
- The CPU then generates a Page Fault Exception

  • The OS must then retrieve the data from the disk
    - The OS chooses a page to evict from RAM and write to disk
    - The OS then reads the page from disk and puts it in RAM
    - The OS then updates the Page Table to map the new page
  • The OS then jumps back to the instruction that caused the page fault
    - This time won't cause a page fault since the page has been loaded into memory

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 0100 for processes 1 and 2 could be mapped onto different physical locations
Virtual Address Spaces.png|600

Memory Protection

Each Page Table entry contains access rights information
- This is hardware enforced (An exception will be triggered if violated)
Memory Protection.png|600

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

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)