Virtual Memory
Virtual Memory
Topics
• Motivations for VM
• Address translation
• Accelerating translation with TLBs
Motivations for Virtual Memory
• Use Physical DRAM as a Cache for the Disk
• Address space of a process can exceed physical memory size
• Sum of address spaces of multiple processes can exceed physical
memory
• Simplify Memory Management
• Multiple processes resident in main memory.
– Each process with its own address space
• Only “active” code and data is actually in memory
– Allocate more memory to process as needed.
Provide Protection
• One process can’t interfere with another.
– because they operate in different address spaces.
• User process cannot access privileged information
– different sections of address spaces have different permissions.
C
CPU 8B a 32 B 4 KB
CPU Memory
Memory disk
disk
c
regs
regs h
e
Memory
0:
Physical 1:
Addresses
CPU
N-1:
0:
Page Table 1:
Virtual Physical
Addresses 0: Addresses
1:
CPU
P-1:
N-1:
Disk
Disk
Disk
CompOrg Fall - Virtual Memory 11
Servicing a Page Fault
Processor Signals (1) Initiate Block Read
Controller Processor
Processor
• Read block of length P Reg
(3) Read
starting at disk address Done
X and store starting at
memory address Y Cache
Cache
Read Occurs
• Direct Memory Access
Memory-I/O
Memory-I/Obus
bus
(DMA)
• Under control of I/O (2) DMA Transfer
controller I/O
I/O
Memory controller
controller
I / O Controller Memory
Signals Completion
• Interrupt the processor disk
Disk disk
Disk
• OS resumes suspended
process
CompOrg Fall - Virtual Memory 12
Motivation #2: Memory
Multiple processesManagement
can reside in physical memory.
How do we resolve address conflicts?
• what if two processes access something at the same address?
memory invisible to
kernel virtual memory user code
%esp stack
process
the “brk” ptr
memory runtime heap (via malloc)
0
Virtual 0 Address Translation Physical
Address VP 1 PP 2 Address
VP 2 Space
Space for ... (DRAM)
Process 1:
N-1
(e.g., read/only
PP 7
library code)
Virtual 0
VP 1
Address PP 10
VP 2
Space for ...
Process 2: M-1
N-1 14
CompOrg Fall - Virtual Memory
Contrast: Macintosh Memory Model
MAC OS 1–9
• Does not use traditional virtual memory
Shared Address Space
P1 Pointer Table
Process P1 A
B
“Handles” P2 Pointer Table
C
Process P2
D
E
CompOrg Fall - Virtual Memory 16
Mac vs. VM-Based Memory
Allocating, deallocating,Mgmt
and moving memory:
• can be accomplished by both techniques
Block sizes:
• Mac: variable-sized
– may be very small or very large
• VM: fixed-size
– size is equal to one page (4KB on x86 Linux systems)
Allocating contiguous chunks of memory:
• Mac: contiguous allocation is required
• VM: can map contiguous range of virtual addresses to disjoint
ranges of physical addresses
Protection
• Mac: “wild write” by one process can corrupt another’s data
page fault
fault
Processor handler
Hardware
Addr Trans Main Secondary
a Mechanism Memory memory
a'
OS performs
virtual address part of the physical address this transfer
on-chip (only if miss)
memory mgmt unit (MMU) 21
CompOrg Fall - Virtual Memory
VM Address Translation
Parameters
• P = 2p = page size (bytes).
• N = 2n = Virtual address limit
• M = 2m = Physical address limit
n–1 p p–1 0
virtual page number page offset virtual address
address translation
m–1 p p–1 0
physical page number page offset physical address
Notice that the page offset bits don't change as a result of translation
CompOrg Fall - Virtual Memory 22
Page Tables
Virtual Page Memory resident
Number page table
(physical page
Valid or disk address)
Physical Memory
1
1
0
1
1
1
0
1
0 Disk Storage
1 (swap file or
regular file system file)
if valid=0
then page
not in memory m–1 p p–1 0
physical page number (PPN) page offset
physical address
CompOrg Fall - Virtual Memory 24
Page Table Operation
Translation
• Separate (set of) page table(s) per process
• VPN forms index into page table (points to a page table entry)
Computing Physical Address
• Page Table Entry (PTE) provides information about page
– if (valid bit = 1) then the page is in memory.
» Use physical page number (PPN) to construct address
– if (valid bit = 0) then the page is on disk
» Page fault
» Must load page from disk into main memory before continuing
Checking Protection
• Access rights field indicate allowable access
– e.g., read-only, read-write, execute-only
– typically support multiple protection modes (e.g., kernel vs. user)
• Protection violation fault if user doesn’t have necessary permission
CompOrg Fall - Virtual Memory 25
Integrating VM and Cache
VA PA miss
Trans- Main
CPU Cache
lation Memory
hit
data
hit
VA PA miss
TLB Main
CPU Cache
Lookup Memory
miss hit
Trans-
lation
data
TLB hit
physical address
13 12 11 10 9 8 7 6 5 4 3 2 1 0
VPN VPO
(Virtual Page Number) (Virtual Page Offset)
11 10 9 8 7 6 5 4 3 2 1 0
PPN PPO
(Physical Page Number) (Physical Page Offset)
TLBT TLBI
13 12 11 10 9 8 7 6 5 4 3 2 1 0
VPN VPO
Set Tag PPN Valid Tag PPN Valid Tag PPN Valid Tag PPN Valid
0 03 – 0 09 0D 1 00 – 0 07 02 1
1 03 2D 1 02 – 0 04 – 0 0A – 0
2 02 – 0 08 – 0 06 – 0 03 – 0
3 07 – 0 03 0D 1 0A 34 1 02 – 0
PPN PPO
Idx Tag Valid B0 B1 B2 B3 Idx Tag Valid B0 B1 B2 B3
0 19 1 99 11 23 11 8 24 1 3A 00 51 89
1 15 0 – – – – 9 2D 0 – – – –
2 1B 1 00 02 04 08 A 2D 1 93 15 DA 3B
3 36 0 – – – – B 0B 0 – – – –
4 32 1 43 6D 8F 09 C 12 0 – – – –
5 0D 1 36 72 F0 1D D 16 1 04 96 34 15
6 31 0 – – – – E 13 1 83 77 1B D3
7 16 1 11 C2 DF 03 F 14 0 – – – –
CompOrg Fall - Virtual Memory 32
Address Translation Example
Virtual Address 0x03D4 #1
TLBT TLBI
13 12 11 10 9 8 7 6 5 4 3 2 1 0
VPN VPO
VPN ___ TLBI ___ TLBT ____ TLB Hit? __ Page Fault? __ PPN: ____
Physical Address
CT CI CO
11 10 9 8 7 6 5 4 3 2 1 0
PPN PPO