Virtual Memory Separate the concept of: address space (name) from physical memory address (location)
Most common example today: wireless (cell) phones
Phone number is your id or name Location varies as you move
Maintain a Map between name Map A (phone number) and current location
713-348-3990 A Change Map to indicate new location is B B
1 Virtual Memory
Location
Physical Memory 0 Name
Program Addresses
1000 1000
CPU generates an address in the program
2000 Corresponds to a word in 2000 physical memory
5000 2 Virtual Memory
• Decouple program address and physical memory address
Physical Memory 0
Program Addresses MAP 1000 1000 1000 400
1250 1200
1500 2000
1750 UNMAPPED
2000 2000
CPU generates an address in the program Lookup Map to find 5000 physical memory address 3 Virtual Memory: Motivation
1. Memory size: Main memory size limited by cost Many programs too large to fit in main memory Some program addresses do not correspond to physical memory address (Historically: small amounts of DRAM. Today: 64-bit address space)
(Historically) Application programmer used overlays to manage memory – Program image stored on cheaper secondary storage (disk) – Only active portions of the program image reside in memory – Programmer explicitly moved required program sections into memory
Problems: Ad-hoc, repetitive, non-portable, tedious, error-prone
4 Virtual Memory: Motivation
2. Multitasking – Multiple concurrent processes sharing main memory – Partition main memory among the processes • Requires processes to have disjoint address spaces – Collectively exceed the size of main memory today • Only a fraction of concurrent processes can execute concurrently
3. Protection: A runaway (or malicious) task may destroy the memory of a different process Need to prevent a task from accessing address space of others
4. Sharing of code and data in main memory
5 Virtual Memory
Physical Address Main Memory MMU Virtual Virtual Address Address CPU TO Physical Address Map
Disk MMU: Memory Management Unit Home Location
6 Virtual Memory: Overview
Programs (code+data) reside in a virtual address space Virtual addresses are just like memory addresses (e.g. 32-bit address) Programs written/compiled exactly as if physical memory addresses Program actually stored on disk
• CPU generates a virtual address Av for some program word
• Memory management unit (MMU) intercepts virtual address Av • MMU translates the virtual address to a physical address:
• If copy of word is in main memory at address Ap
• main memory location Ap is accessed
• else physical location is some disk address Ad
• word is read from disk to memory in physical address As
• access word from its new physical address As 7 Virtual Memory: Details
Virtual address space: Program address space partitioned into fixed-size pages of size P = 2p bytes Virtual space consists of N = 2n pages : 0, 1, 2, …… 2n -1 Virtual address space has addresses in range 0, 1, 2, …., 2n+p -1
Physical address space (Main memory size) is M = 2m+p bytes Main memory partitioned into page frames of size P = 2p bytes Main memory consists of 2m pages: 0, 1, 2, …… 2m -1 Physical memory addresses are in range 0, 1, 2, …., M-1
0 2-bit page number 10-bit offset 1023 1024
2047 12-bit address 2048
3071 Page Size: 1KB = 210 bytes = 1024 bytes 3072 4 Pages 4095 Address space: 4 x 1024 = 4096 bytes 8 Virtual Memory: Details
Virtual address Av : address of word generated by the CPU VPN: Virtual page number (n bits) Offset: Displacement (offset) within a page (p bits)
Physical address Ap : Memory address of word if in main memory PFN: Physical page frame number (m bits) Offset: Displacement (offset) within a page (p bits)
If accessed word on disk it has some disk address Ad
m-bit page n-bit page number p-bit offset frame number p-bit offset
n + p bit virtual address m + p bit physical address
9 Virtual Address
Virtual address space size N = 16 bytes, n+p = 4 Page size, P = 4 bytes, p = 2 Av VPN 0 1 n Virtual address space: 2 pages = 4 pages 2 0 3 Virtual address: Av 4 n p 5 1 6 7 VPN d 8 9 2 10 11 Av = 14 = 1110 12 d 13 3 VPN = 11 Offset = 10 14 15
10 Physical Address
Main memory size M = 8 bytes, m+p = 3 Page size, P = 4 bytes, p = 2 Ap PFN 0 1 m Memory space: 2 = 2 page frames 2 0 3 Physical address: Ap 4 d m p 5 1 6 7 PFN d
Ap = 6 = 110 PFN = 1 Offset = 10
11 Virtual to Physical Address Translation
Some subset of the virtual pages are in main memory at any time Each such page is stored in some page frame The offset of a byte in a page is the same as that in the page frame
Av VPN 0 Map 1 VPN PFN 2 0 0 A 3 d Ap PFN 4 P 1 1 0 5 Q 1 2 0 A 6 1 B R 3 A 0 7 S d 2 C 8 A 3 D 9 B 2 4 P 10 10 Av = 10 10 C 5 Q 1 11 D 6 R 12 Map 7 S 13 3 14 15 12 0 10 Ap = 2 Page Tables
Mapping of virtual address to physical address done by a Page Table (PT) PT is a 1-dimensional array of page descriptors PT has one descriptor for each possible VPN (2n entries in Page Table) Each PT entry (descriptor) contains: Control Bits: P (presence), D (dirty), U (use), Protection bits …. PFN of the page if it is currently in main memory (Indication of) disk address of page if it is not in main memory Index PDU-- PFN Disk address
0 0xx-- Ad 1 1xx-- 1 Memory 1xx-- 0 address 2 0xx-- Ad 3 Disk address VPN Page Table Page Table stored in main memory: Base address of PT in special Page Table Register (PTR) 13 Virtual to Physical Address Translation
A = 10 10 10 v Protection checks when accessing Map page descriptor
Av VPN 0 10 Ap = 2 0 Disk address 1 2 0 3 PFN Index PDU-- PFN Ap 4 P Q 5 0 A R 1 0 0xx-- Ad 6 B S 1xx-- 1 1 7 1 C 0 A 2 8 1xx-- 0 D B 2 3 9 2 4 P C 0xx-- Ad 10 3 Q D 5 1 11 6 R VPN Page Table 12 7 S 13 3 14 15 14 Simple Address Translation Scheme
5 n p VPN VPN d is an index PTR into 0 P-- PFN the 1 Page P-- PFN Table
P-- PFN
P-- PFN PFN d m p 2n-1 P-- PFN
Page Table 15 Simple Address Translation
CPU: generates (n+p)-bit virtual address Av MMU:
1. Index into the Page Table using the VPN ( n MSBs of Av). The base address of the PT is available in the Page Table Register (PTR). 2. Read the page descriptor at PageTable[VPN]. Its physical memory address is PTR + VPN (scaled) 3. If the presence bit (P) of the page descriptor is ON: /* Required page is currently in main memory */ Get the m-bit PFN stored in the page descriptor Update page descriptor fields U, and W if a write access
Concatenate it with the offset field of Av (p LSBs of Av) Access main memory with the (m+p)-bit physical address Return accessed word to CPU else /* P bit OFF: requested page not in main memory*/ Handle Page Fault 16 Simple Address Translation
MMU: Page Fault Handling 1. Make space in memory by evicting a page to disk **
(a) Select a victim page to evict from main memory LRU replacement policy (approximate LRU based on U bits) (b) Write victim page to disk if it is dirty (D is true) Use Write-back policy for updates: disk writes expensive (c) Update Page Table: victim page descriptor to reflect its transfer to disk
** In practice waiting to do the eviction at the time of fault (synchronous page replacement) is not good for performance.
The Operating System maintains a pool of free pages by asynchronously flushing (evicting) the pages to disk as a background activity, whenever the free pool falls below a threshold. So steps 1(a) - (c) are done as backgroundactivities.
1. Get a free page frame from the OS. 17 Simple Address Translation
MMU: Page Fault Handling
2. Read faulting page into free page frame (a) Read victim page from disk into freed page frame (b) Update Page Table: descriptor of faulting page updated to reflect new memory location, clear U and D bits, and set P to TRUE. (c) Restart execution of instruction causing the page fault (*)
* Since servicing the page fault takes millions of cycles, the CPU does not usually wait for its completion, but begins executing some other task. At some later point in time the task is rescheduled on the CPU and this instruction is executed again.
18 Virtual Memory Operation: Example
Address Trace: 10, 4, 2 (data words C,P and W respectively) Accesses to both C and P are page hits: served from memory directly. Access to W (address 0010): VPN = 00. PT[0] indicates Page Fault!
Av VPN 0 U Disk address 1 V 2 W 0 3 X PFN Index PDU-- PFN Ap 4 P Q 5 0 A R 1 0 0xx-- Ad 6 B S 1xx-- 11 1 7 1 C 0 A 2 8 1xx-- 00 D B 2 3 9 2 4 P C 0xx-- Ad 10 3 Q D 5 1 11 6 R VPN Page Table 12 7 S 13 3 14 15 19 Page Fault Handling: Example (contd …)
Virtual page 1 (word P) accessed later than page 2 (word C) LRU policy will evict page 2 from memory, freeing up page frame 0 Virtual page 0 (word W) fetched from disk into page frame 0
Av VPN 0 U 1 V 2 W 0 3 X PFN Index PDU-- PFN Ap 4 P Q 5 U R 1 0 1xx-- 00 0 6 V S 1xx-- 11 1 7 1 W 0 A 2 8 0xx-- A X B 2 d 3 9 2 4 P C 0xx-- Ad 10 3 Q D 5 1 11 6 R VPN Page Table 12 7 S 13 3 14 15 Disk address 20