Professional Documents
Culture Documents
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
Provide Protection
One process cant interfere with another. because they operate in different address spaces. User process cannot access privileged information different sections of address spaces have different permissions.
CompOrg Fall - Virtual Memory 2
Motivation #1: DRAM a Cache for Disk Full address space is quite large:
32-bit addresses: ~4,000,000,000 (4 billion) bytes 64-bit addresses: ~16,000,000,000,000,000,000 (16 quintillion) bytes
To access large amounts of data in a cost-effective manner, the bulk of the data must be stored on disk
256 MB: ~$320 4 MB: ~$400 SRAM DRAM 8 GB: ~$64
Disk
3
cache
virtual memory
CPU regs
8B
C a c h e
32 B
Memory
4 KB
disk
DRAM vs. SRAM as a Cache DRAM vs. disk is more extreme than SRAM vs. DRAM
Access latencies: DRAM ~10X slower than SRAM Disk ~100,000X slower than DRAM Importance of exploiting spatial locality: First byte is ~100,000X slower than successive bytes on disk vs. ~4X improvement for page-mode vs. regular accesses to DRAM Bottom line: Design decisions made for DRAM caches driven by enormous cost of misses
SRAM
DRAM
Disk
5
= X?
1: N-1:
CPU
N-1:
CPU
P-1:
N-1: Disk
Address Translation: Hardware converts virtual addresses to physical addresses via an OS-managed lookup table (page table) 10
CompOrg Fall - Virtual Memory
Page Faults (Similar to Cache Misses) What if an object is on disk rather than in memory?
Page table entry indicates virtual address not in memory OS exception handler invoked to move data from disk into memory current process suspends, others can resume OS has full control over placement, etc.
Before fault
Memory Page Table Virtual Addresses CPU Physical Addresses
After fault
Memory Page Table Virtual Addresses CPU Physical Addresses
Disk 11
Cache
Read Occurs
Direct Memory Access (DMA) Under control of I/O controller Memory-I/O bus (2) DMA Transfer Memory I/O controller
disk Disk
disk Disk
12
Motivation #2: Memory Management Multiple processes can reside in physical memory. How do we resolve address conicts?
what if two processes access something at the same address?
memory invisible to user code
%esp
the brk ptr runtime heap (via malloc) uninitialized data (.bss) initialized data (.data) program text (.text) forbidden 0 CompOrg Fall - Virtual Memory
13
Virtual Address Space for Process 1: Virtual Address Space for Process 2:
0 VP 1 VP 2
Address Translation PP 2
...
N-1 PP 7 0 VP 1 VP 2
...
N-1
Compaction
Can move any object and just update the (unique) pointer in pointer table Shared Address Space P1 Pointer Table B Process P1 A Handles Process P2 D E
CompOrg Fall - Virtual Memory 16
P2 Pointer Table
Mac vs. VM-Based Memory Mgmt Allocating, deallocating, and moving memory:
can be accomplished by both techniques
Block sizes:
Mac: variable-sized may be very small or very large VM: xed-size size is equal to one page (4KB on x86 Linux systems)
Protection
Mac: wild write by one process can corrupt anothers data
17
Based on MACH OS
Developed at CMU in late 1980s
18
Motivation #3: Protection Page table entry contains access rights information
hardware enforces this protection (trap into OS if violation occurs) Page Tables Memory
Read? Write? VP 0: Yes No Physical Addr PP 9 PP 4 XXXXXXX
0: 1:
Process i:
VP 1: Yes VP 2: No
Yes No
Physical Addr PP 6 PP 9 XXXXXXX
Process j:
VP 1: Yes VP 2: No
No No
N-1:
19
Address Translation
Address translation must be fast (it happens on every memory access). We need a fully associative placement policy. miss penalty is huge! We cant afford to go looking at every virtual page to nd the right one
we dont use the tag bits approach
20
VM Address Translation
V = {0, 1, . . . , N1} virtual address space P = {0, 1, . . . , M1} physical address space N>M
MAP: V P U {} address mapping function MAP(a)
= a' if data at virtual address a is present at physical address a' in P
= if data at virtual address a is not present in P page fault fault handler Hardware Addr Trans Mechanis m a' OS performs this transfer (only if miss)
21
Processor
a virtual address
Main Memory
Secondary memory
address translation
0 physical address
Notice that the page offset bits don't change as a result of translation
CompOrg Fall - Virtual Memory 22
Page Tables
Memory resident page table
(physical page Valid or disk address) 1 1 0 1 1 1 0 1 0 1
Physical Memory
23
Checking Protection
Access rights eld 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 doesnt have necessary permission
CompOrg Fall - Virtual Memory 25
VA CPU
valid
.
=
TLB
TLB hit
physical address tag valid tag data index byte offset
Cache
cache hit
data
CompOrg Fall - Virtual Memory 28
VPN 0 1 2 3 4 5 6 7
PPN 28 33 2 16
Valid 1 0 1 1 0 1 0 0
VPN 8 9 0A 0B 0C 0D 0E 0F
PPN 13 17 9 2D 11 0D
Valid 1 1 1 0 0 1 1 1
30
TLBI
6 5 4 3 2 1 0
VPN
Set 0 1 2 3 Tag 3 3 2 7 PPN 2D Valid 0 1 0 0 Tag 9 2 8 3 PPN 0D 0D Valid 1 0 0 1 Tag 0 4 6 0A PPN 34
VPO
Valid 0 0 0 1 Tag 7 0A 3 2 PPN 2 Valid 1 0 0 0
31
CT
8 7 6 5 4
CI
3 2 1
CO
0
PPN
Idx 0 1 2 3 4 5 6 7 Tag 19 15 1B 36 32 0D 31 16 Valid 1 0 1 0 1 1 0 1 B0 99 0 43 36 11 B1 11 2 6D 72 B2 23 4 8F F0 B3 11 8 9 1D Idx 8 9 A B C D Tag 24 2D 2D 0B 12 16
PPO
Valid 1 0 1 0 0 1 1 0 B0 3A 93 4 83 B1 0 15 96 77 32 B2 51 DA 34 1B B3 89 3B 15 D3
TLBI
6 5 4 3 2 1 0
VPO
TLBI ___ TLBT ____ TLB Hit? __ Page Fault? __ PPN: ____
Physical Address
CT
11 10 9 8 7 6 5 4
CI
3 2 1
CO
0
Problem:
Would need a 4 MB page table! 220 *4 bytes Level 1 Table
Common solution
multi-level page tables e.g., 2-level table (P6) Level 1 table: 1024 entries, each of which points to a Level 2 page table. Level 2 table: 1024 entries, each of which points to a page
CompOrg Fall - Virtual Memory
...
34
System View
User virtual address space created by mapping to set of pages Need not be contiguous Allocated dynamically Enforce protection during address translation OS manages many processes simultaneously Continually switching among processes Especially when one must wait for resource E.g., disk I/O to handle page fault
CompOrg Fall - Virtual Memory 35