CS 105 Tour of the Black Holes of Computing Virtual Memory Topics Motivations for VM Address translation Accelerating translation with TLBs What Is Virtual Memory If you think it s there and it s there it s real If you think it s not there and it s there it s transparent If you think it s there and it s not there it s imaginary If you think it s not there and it s not there it s nonexistent Virtual memory is imaginary memory it gives you the illusion of a memory arrangement that s not physically there 2 CS 105 Motivations for Virtual Memory Use physical DRAM as 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 more common modern case Simplify memory management Multiple processes resident in main memory Each 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 3 CS 105 Motivation 1 DRAM as 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 Disk storage is 250X cheaper than DRAM storage 1 TB of DRAM 28 000 667 MHz late 2008 prices 1 TB of disk 115 To get cost effective access to large amounts of data bulk of data must be stored on disk 2 MB 150 SRAM 4 4GB 56 DRAM 1 TB 115 Disk CS 105 Levels in Memory Hierarchy cache CPU CPU regs regs Register size speed Mbyte line size 32 B 0 3 ns 4B 8B C a c h e 32 B Cache 32 KB 4MB 2 ns 75 MB 32 B virtual memory Memory Memory 4 KB Memory 4096 MB 7 5 ns 0 014 MB 4 KB disk disk Disk Memory 1 TB 8 ms 0 00012 MB larger slower cheaper 5 CS 105 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 disk caches in DRAM driven by enormous cost of misses SRAM 6 DRAM Disk CS 105 Impact of Properties on Design If disk cache were organized like L1 L2 cache how would we set following design parameters Line size Large since disk better at transferring large blocks Associativity High to mimimize miss rate Write through or write back Write back since can t afford small writes to disk What would the impact of these choices be on Miss rate Extremely low 1 Hit time Must match cache DRAM performance Miss latency Very high 10ms Tag storage overhead Low relative to block size 7 CS 105 Locating an Object in a Cache Review of SRAM L1 L2 Cache Tag stored with cache line Maps from cache block to memory blocks From cached to uncached form Save a few bits by only storing tag of blocks that are in cache No tag for block not in cache Hardware retrieves information Can quickly match against multiple tags Object Name X X Tag Data 0 D 243 1 X J 17 105 N 1 8 Cache CS 105 Locating an Object in a Cache DRAM disk Cache Each allocated page of virtual memory has entry in page table Mapping from virtual to physical pages From uncached form to cached form Page table entry tag even if page not in memory Specifies disk address Only way to know where to find page OS retrieves information from disk as needed Page Table Cache Location Data Object Name D 0 0 243 X J On Disk 1 17 105 9 X 1 N 1 CS 105 A System with Physical Memory Only Examples Most Cray machines early PCs nearly all embedded systems etc Memory Physical Addresses 0 1 CPU N 1 Addresses generated by the CPU correspond directly to bytes in physical memory 10 CS 105 A System with Virtual Memory Examples Memory Workstations servers modern PCs etc Virtual Addresses 0 1 Page Table 0 1 Physical Addresses CPU P 1 N 1 Disk Address Translation Hardware converts virtual addresses to physical ones via OS managed lookup table page table 11 CS 105 Page Faults Like Cache Misses What if 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 VM and Multiprogramming are symbiotic Current process suspends others can resume OS has full control over placement etc Before fault Virtual Addresses Page Table Physical Addresses CPU Memory Page Table Virtual Addresses Physical Addresses CPU Disk 12 After fault Memory Disk CS 105 Servicing Page Fault Processor signals controller Read block of length P starting at disk address X and store starting at memory address Y Read occurs Direct Memory Access DMA Under control of I O controller I O controller signals completion Interrupt processor OS resumes suspended process 13 1 Initiate Block Read Processor Processor Reg 3 Read Done Cache Cache Memory I O Memory I Obus bus 2 DMA Transfer I O I O controller controller Memory Memory disk Disk disk Disk CS 105 Motivation 2 Memory Mgmt Multiple processes can reside in physical memory How do we resolve address conflicts What if two processes access something at same address kernel virtual memory stack esp Memory mapped region for shared libraries Linux x86 process memory image 14 memory invisible to user code runtime heap via malloc 0 the brk pointer uninitialized data bss initialized data data program text text forbidden CS 105 Solution Separate Virtual Address Spaces Virtual and physical address spaces divided into equal sized blocks Blocks are called pages both virtual and physical Each process has its own virtual address space Operating system controls how virtual pages are assigned to physical memory Virtual Address Space for Process 1 Virtual Address Space for Process 2 15 0 Address Translation 0 VP 1 VP 2 PP 2 N 1 PP 7 0 VP 1 VP 2 N 1 Physical Address Space DRAM e g read only library code PP 10 M 1 CS 105 Motivation 3 Protection Page table entry contains access rights information Hardware enforces this protection trap into OS if violation occurs Page Tables Read Write Process i No PP 9 VP 1 Yes Yes PP 4 No No XXXXXXX Read Write VP 0 Yes Yes 16 Physical Addr VP 0 Yes VP 2 Process j Memory 0 1 Physical Addr PP 6 VP 1 Yes No PP 9 VP 2 No No XXXXXXX N 1 CS 105 VM Address Translation Virtual Address Space V 0 1 N 1 Physical Address Space P 0 …
View Full Document
Unlocking...