ECE331: Hardware Organization and Design Lecture 29: an Introduction to Virtual Memory Adapted from Computer Organization and Design, Patterson & Hennessy, UCB
Overview Virtual memory used to protect applications from each other Portions of application located both in main memory and on disk Portions which are being used are swapped into main memory Operating system generally manages virtual memory Lots of interesting technology Brings together hardware and software ECE331: Introduction to Virtual Memory 2
Virtual Memory - Objectives 1. Allow program to be written without memory constraints program can exceed the size of the main memory 2. Many Programs sharing DRAM Memory so that context switches can occur 3. Relocation: Parts of the program can be placed at different locations in the memory instead of a big chunk Virtual Memory: I. Main Memory holds many programs running at same time (processes) II. use Main Memory as a kind of cache for disk Datapath Processor Control Cache Regs Main Memory (DRAM) Disk ECE331: Introduction to Virtual Memory 3
Disk Technology in Brief Disk is mechanical storage - Several platters, with information recorded magnetically on both surfaces tracks Disk Access Time = seek time + rotational delay + transfer time 3600 4200-5400-7200-100 00 RPM rotation speed Arm Head Sector R/W arm Inner Track takes msec Miss to disk is very expensive typical access time = millions of clock cycles Outer Track Actuator Platter ECE331: Introduction to Virtual Memory 4
Virtual to Physical Memory mapping Each process has its own private virtual address space (e.g., 2 32 Bytes); CPU actually generates virtual addresses Each computer has a physical address space (e.g., 128 MB DRAM); also called real memory Virtual Memory Address translation: mapping virtual addresses to physical addresses Allows some chunks of virtual memory to be present on disk, not in main memory Physical Memory Allows multiple programs to use (different chunks of physical) memory at the same time virtual address Processor ECE331: Introduction to Virtual Memory 5
Mapping Virtual Memory to Physical Memory Divide Memory into equal sized pages (say, 4KB each) A page of Virtual Memory can be assigned to any page frame of Physical Memory Virtual Memory 4GB Stack 128 MB Physical Memory Heap Heap Static Single Process 0 ECE331: Introduction to Virtual Memory 6 0 Code
Two Tasks sharing the Physical Memory ECE331: Introduction to Virtual Memory 7
How to Perform Address Translation? VM divides memory into equal sized pages Address translation maps entire pages offsets within the pages do not change page size is a power of two - the virtual address separates into two fields virtual address Virtual Page Number Page Offset ECE331: Introduction to Virtual Memory 8
Mapping Virtual to Physical Address Virtual Address 31 30 29 28 27..12 11 10 9 8... 3 2 1 0 Virtual Page Number Page Offset Translation 1KB page size Physical Page Number Page Offset 26...12 11 10 ECE331: Introduction to Virtual Memory 9 Physical Address 9 8... 3 2 1 0
Mapping VA to PA Want fully associative page placement A page table: data structure that CPU contains the mapping of virtual to physical pages There are several different ways, all up to the operating system, to keep and update this table Each process running in the system has its own page table M e m o r y V ir t u al S p a c e CPU Virtual Address Virtual Page No. Page Offset Hit_time = 70-100 CPU cycles Miss_penalty = 10^6 cycles Miss_rate = 1% Page Table Address Mapping ECE331: Introduction to Virtual Memory 10 Physical Page No. Page Offset Physical Address Memory
Page Table ECE331: Introduction to Virtual Memory 11
Handling Page Faults A page fault is like a cache miss Must find page in lower level of hierarchy If valid bit is zero, the Physical Page Number points to a page on disk When OS starts new process, it creates space on disk for all the pages of the process, sets all valid bits in page table to zero, and all Physical Page Numbers to point to disk called Demand Paging - pages of the process are loaded from disk only as needed ECE331: Introduction to Virtual Memory 12
Comparing the 2 hierarchies Cache Virtual Memory Block or Line Page Miss Page Fault Block Size: 32-64B Page Size: 4K-16KB Placement: Fully Associative Direct Mapped, N-way Set Associative Replacement: Least Recently Used LRU or Random (LRU) approximation Write Thru or Back Write Back How Managed: Hardware + Software Hardware (Operating System) ECE331: Introduction to Virtual Memory 13
Virtual Memory Use main memory as a cache for secondary (disk) storage Managed jointly by CPU hardware and the operating system (OS) Programs share main memory Each gets a private virtual address space holding its frequently used code and data Protected from other programs CPU and OS translate virtual addresses to physical addresses VM block is called a page VM translation miss is called a page fault ECE331: Introduction to Virtual Memory 14
Address Translation Fixed-size pages (e.g., 4K) ECE331: Introduction to Virtual Memory 15
Page Fault Penalty On page fault, the page must be fetched from disk Takes millions of clock cycles Handled by OS code Try to minimize page fault rate Fully associative placement Smart replacement algorithms ECE331: Introduction to Virtual Memory 16
Page Tables Stores placement information Array of page table entries, indexed by virtual page number Page table register in CPU points to page table in physical memory If page is present in memory PTE stores the physical page number Plus other status bits (referenced, dirty, ) If page is not present PTE can refer to location in swap space on disk ECE331: Introduction to Virtual Memory 17
Translation Using a Page Table ECE331: Introduction to Virtual Memory 18
Mapping Pages to Storage ECE331: Introduction to Virtual Memory 19
Replacement and Writes To reduce page fault rate, prefer least-recently used (LRU) replacement Reference bit (aka use bit) in PTE set to 1 on access to page Periodically cleared to 0 by OS A page with reference bit = 0 has not been used recently Disk writes take millions of cycles Block at once, not individual locations Write through is impractical Use write-back Dirty bit in PTE set when page is written ECE331: Introduction to Virtual Memory 20
Summary Virtual memory allows the appearance of a main memory that is larger than what is physically present Virtual memory can be shared by multiple applications Page table indicates how to translate from virtual to physical address Disk accesses are very slow. Pages are much larger than cache blocks Next time: Ways to speed up virtual to physical memory translation Translation look aside buffer ECE331: Introduction to Virtual Memory 21