Bernhard Boser & Randy Katz

Slides:



Advertisements
Similar presentations
Computer Organization CS224 Fall 2012 Lesson 44. Virtual Memory  Use main memory as a “cache” for secondary (disk) storage l Managed jointly by CPU hardware.
Advertisements

Lecture 34: Chapter 5 Today’s topic –Virtual Memories 1.
CSE 490/590, Spring 2011 CSE 490/590 Computer Architecture Virtual Memory I Steve Ko Computer Sciences and Engineering University at Buffalo.
CSCE 212 Chapter 7 Memory Hierarchy Instructor: Jason D. Bakos.
Computer ArchitectureFall 2008 © November 10, 2007 Nael Abu-Ghazaleh Lecture 23 Virtual.
Virtual Memory and Paging J. Nelson Amaral. Large Data Sets Size of address space: – 32-bit machines: 2 32 = 4 GB – 64-bit machines: 2 64 = a huge number.
©UCB CS 162 Ch 7: Virtual Memory LECTURE 13 Instructor: L.N. Bhuyan
CS 61C: Great Ideas in Computer Architecture
1  1998 Morgan Kaufmann Publishers Chapter Seven Large and Fast: Exploiting Memory Hierarchy (Part II)
Lecture 19: Virtual Memory
Operating Systems ECE344 Ding Yuan Paging Lecture 8: Paging.
July 30, 2001Systems Architecture II1 Systems Architecture II (CS ) Lecture 8: Exploiting Memory Hierarchy: Virtual Memory * Jeremy R. Johnson Monday.
Virtual Memory Expanding Memory Multiple Concurrent Processes.
Virtual Memory Part 1 Li-Shiuan Peh Computer Science & Artificial Intelligence Lab. Massachusetts Institute of Technology May 2, 2012L22-1
1 Some Real Problem  What if a program needs more memory than the machine has? —even if individual programs fit in memory, how can we run multiple programs?
Multilevel Caches Microprocessors are getting faster and including a small high speed cache on the same chip.
CS2100 Computer Organisation Virtual Memory – Own reading only (AY2015/6) Semester 1.
Virtual Memory Ch. 8 & 9 Silberschatz Operating Systems Book.
Virtual Memory Review Goal: give illusion of a large memory Allow many processes to share single memory Strategy Break physical memory up into blocks (pages)
Constructive Computer Architecture Virtual Memory: From Address Translation to Demand Paging Arvind Computer Science & Artificial Intelligence Lab. Massachusetts.
LECTURE 12 Virtual Memory. VIRTUAL MEMORY Just as a cache can provide fast, easy access to recently-used code and data, main memory acts as a “cache”
CS203 – Advanced Computer Architecture Virtual Memory.
CS161 – Design and Architecture of Computer
Translation Lookaside Buffer
CS 61C: Great Ideas in Computer Architecture Virtual Memory Cont.
Virtual Memory Chapter 7.4.
ECE232: Hardware Organization and Design
Memory COMPUTER ARCHITECTURE
CS161 – Design and Architecture of Computer
Lecture 12 Virtual Memory.
Section 9: Virtual Memory (VM)
From Address Translation to Demand Paging
CS703 - Advanced Operating Systems
From Address Translation to Demand Paging
Today How was the midterm review? Lab4 due today.
CS 704 Advanced Computer Architecture
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.
Some Real Problem What if a program needs more memory than the machine has? even if individual programs fit in memory, how can we run multiple programs?
CS510 Operating System Foundations
CSE 153 Design of Operating Systems Winter 2018
Virtual Memory: Concepts /18-213/14-513/15-513: Introduction to Computer Systems 17th Lecture, October 23, 2018.
CS 61C: Great Ideas in Computer Architecture Lecture 23: Virtual Memory Krste Asanović & Randy H. Katz 11/12/2018.
Guest Lecturer: Justin Hsia
Lecture 14 Virtual Memory and the Alpha Memory Hierarchy
From Address Translation to Demand Paging
CS 105 “Tour of the Black Holes of Computing!”
Lecture 23: Cache, Memory, Virtual Memory
Lecture 22: Cache Hierarchies, Memory
Page that info back into your memory!
Lecture 12 Virtual Memory
Translation Lookaside Buffer
Morgan Kaufmann Publishers Memory Hierarchy: Virtual Memory
CSE 451: Operating Systems Autumn 2005 Memory Management
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE451 Virtual Memory Paging Autumn 2002
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
CSE 451: Operating Systems Autumn 2004 Page Tables, TLBs, and Other Pragmatics Hank Levy 1.
CSC3050 – Computer Architecture
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CS 105 “Tour of the Black Holes of Computing!”
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
CS 105 “Tour of the Black Holes of Computing!”
CSE 451: Operating Systems Winter 2005 Page Tables, TLBs, and Other Pragmatics Steve Gribble 1.
CSE 153 Design of Operating Systems Winter 2019
Virtual Memory Lecture notes from MKP and S. Yalamanchili.
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.
Virtual Memory.
Sarah Diesburg Operating Systems CS 3430
Review What are the advantages/disadvantages of pages versus segments?
Instructor: Phil Gibbons
Presentation transcript:

Bernhard Boser & Randy Katz http://inst.eecs.berkeley.edu/~cs61c CS 61C: Great Ideas in Computer Architecture Lecture 23: Virtual Memory Bernhard Boser & Randy Katz http://inst.eecs.berkeley.edu/~cs61c

Lecture 23: Virtual Memory Agenda Virtual Memory Paged Physical Memory Swap Space Page Faults Hierarchical Page Tables Caching Page Table Entries (TLB) CS 61c Lecture 23: Virtual Memory

Lecture 23: Virtual Memory Virtual Machine 100+ Processes, managed by OS 100’s of processes OS multiplexes these over available cores But what about memory? There is only one! We cannot just ”save” its contents in a context switch … CS 61c Lecture 23: Virtual Memory

Virtual versus Physical Addresses Processor (& Caches) Control Datapath PC Registers (ALU) ? Memory (DRAM) Bytes Virtual Address Physical Address Many of these (software & hardware cores) One main memory Processes use virtual addresses, e.g. 0 … 0xffff,ffff Many processes, all using same (conflicting) addresses Memory uses physical addresses (also, e.g. 0 ... 0xffff,ffff) Memory manager maps virtual to physical addresses CS 61c Lecture 23: Virtual Memory

Lecture 23: Virtual Memory Address Spaces Address space = set of addresses for all available memory locations Now, 2 kinds of memory addresses: Virtual Address Space set of addresses that the user program knows about Physical Address Space set of addresses that map to actual physical cells in memory hidden from user applications Memory manager maps between these two address spaces because the programs will issue virtual addresses, but we need physical addresses to access data in DRAM CS 61c Lecture 23: Virtual Memory

Conceptual Memory Manager Memory (DRAM) Concept: Real memory managers use more complex mappings. CS 61c Lecture 23: Virtual Memory

Responsibilities of Memory Manager Map virtual to physical addresses Protection: Isolate memory between processes Each process gets dedicate ”private” memory Errors in one program won’t corrupt memory of other program Prevent user programs from messing with OS’ memory Swap memory to disk Give illusion of larger memory by storing some content on disk Disk is usually much larger & slower than DRAM Use “clever” caching strategies CS 61c Lecture 23: Virtual Memory

Lecture 23: Virtual Memory Agenda Virtual Memory Paged Physical Memory Swap Space Hierarchical Page Tables Caching Page Table Entries (TLB) CS 61c Lecture 23: Virtual Memory

Lecture 23: Virtual Memory Memory Manager Several options Today “paged memory” dominates Physical memory (DRAM) is broken into pages Typical page size: 4 KiB+ Virtual address (e.g. 32 Bits) page number (e.g. 20 Bits) offset (e.g. 12 Bits) CS 61c Lecture 23: Virtual Memory

Lecture 23: Virtual Memory Paged Memory Memory (DRAM) Page Table Page N Page 2 Page 1 Page 0 Page Table Page Table Each process has a dedicated page table. Physical memory non-consecutive. CS 61c Lecture 23: Virtual Memory

Paged Memory Address Translation OS keeps track of which process is active Chooses correct page table Memory manager extracts page number from virtual address Looks up page address in page table Computes physical memory address from sum of page address and offset (from virtual address) Virtual address (e.g. 32 Bits) page table entry offset Physical address page number offset Physical addresses may (but do not have to) have more or fewer bits than virtual addresses CS 61c Lecture 23: Virtual Memory

Lecture 23: Virtual Memory Protection Assigning different pages in DRAM to processes also keeps them from accessing each others memory Isolation Page tables handled by OS (in supervisory mode) Sharing is possible also OS may assign same physical page to several processes CS 61c Lecture 23: Virtual Memory

Lecture 23: Virtual Memory Write Protection Write protected Memory (DRAM) Page Table Page N Page 2 Page 1 Page 0 1 Page Table 1 Page Table Exception when writing to protected page (e.g. program code). CS 61c Lecture 23: Virtual Memory

Where do Page Tables Reside? E.g. 32-Bit virtual address, 4-KiB pages Single page table size: 4 x 220 Bytes = 4-MiB 0.1% of 4-GiB memory But much too large for a cache! Store page tables in memory (DRAM) Two (slow) memory accesses per lw/sw on cache miss How could we minimize the performance penalty? Transfer blocks (not words) between DRAM and processor cache Exploit spatial locality Cache for frequently used page table entries … CS 61c Lecture 23: Virtual Memory

Paged Table Storage in DRAM Memory (DRAM) Page Table Page lw/sw take two memory references CS 61c Lecture 23: Virtual Memory

Lecture 23: Virtual Memory Blocks vs. Pages In caches, we dealt with individual blocks Usually ~64B on modern systems In VM, we deal with individual pages Usually ~4 KB on modern systems Common point of confusion: Bytes, Words, Blocks, Pages are all just different ways of looking at memory! CS 61c Lecture 23: Virtual Memory

Bytes, Words, Blocks, Pages Eg: 16 KiB DRAM, 4 KiB Pages (for VM), 128 B blocks (for caches), 4 B words (for lw/sw) 1 of 4 Pages per Memory 1 of 32 Blocks per Page Block 31 Word 31 1 of 1 Memory Page 3 Page 2 Page 1 Page 0 Can think of a page as: 32 Blocks, or 1024 Words Can think of memory as: 4 Pages, or 128 Blocks, or 4096 Words, or 16,384 Bytes 16 KiB Block 0 Word 0

Lecture 23: Virtual Memory Agenda Virtual Memory Paged Physical Memory Swap Space Hierarchical Page Tables Caching Page Table Entries (TLB) CS 61c Lecture 23: Virtual Memory

Memory Hierarchy Disk Slow But huge How could we make use of its capacity (when running low on DRAM)? CS 61c

Aside … why is disk so slow? 10,000 rpm (revolutions per minute) 6 ms per revolution Average random access time: 3 ms CS 61c Lecture 23: Virtual Memory

Lecture 23: Virtual Memory What about SSD? Made with transistors Nothing mechanical that turns Like “Ginormous” register file That does not ”forget” when power is off Fast access to all cells, regardless of address Still much slower than register, DRAM Read/write blocks, not bytes Potential reliability issues CS 61c Lecture 23: Virtual Memory

Lecture 23: Virtual Memory Paged Memory Valid: page allocated DRAM/disk Memory (DRAM) Page N Page Table Disk Page Table Each process has a dedicated page table. Physical memory non-consecutive. CS 61c Lecture 23: Virtual Memory

Lecture 23: Virtual Memory Memory Access Check page table entry: Valid? Yes, valid  In DRAM? Yes, in DRAM: read/write data No, on disk: allocate new page in DRAM If out of memory, evict a page from DRAM Store evicted page to disk Read page from disk into memory Read/write data Not Valid allocate new page in DRAM If out of memory, evict a page Page fault OS intervention CS 61c Lecture 23: Virtual Memory

Lecture 23: Virtual Memory Lecture 4: Out of Memory Insufficient free memory: malloc() returns NULL $ gcc OutOfMemory.c; ./a.out failed to allocate > 131 TiBytes What’s going on? CS 61c CS 61c Lecture 23: Virtual Memory 24

Write-Through or Write-Back? DRAM acts like “cache” for disk Should writes go directly to disk (write-through)? Or only when page is evicted? Which option do you propose? Implementation? Write back Dirty bit CS 61c Lecture 23: Virtual Memory

Lecture 23: Virtual Memory Agenda Virtual Memory Paged Physical Memory Swap Space Hierarchical Page Tables Caching Page Table Entries (TLB) CS 61c Lecture 23: Virtual Memory

Size of Page Tables E.g. 32-Bit virtual address, 4-KiB pages Single page table size: 4 x 220 Bytes = 4-MiB 0.1% of 4-GiB memory Total size for 256 processes (each needs a page table) 256 x 4 x 220 Bytes = 256 x 4-MiB = 1-GiB 25% of 4-GiB memory! What about 64-bit addresses? How can we keep the size of page tables “reasonable”? CS 61c Lecture 23: Virtual Memory

Lecture 23: Virtual Memory Options Increase page size E.g. doubling page size cuts PT size in half At the expense of potentially wasted memory Hierarchical page tables With decreasing page size Most programs use only fraction of memory Split PT in two (or more) parts CS 61c Lecture 23: Virtual Memory

Lecture 23: Virtual Memory Hierarchical Page Table – exploits sparcity of virtual address space use Virtual Address 31 22 21 12 11 p1 p2 offset 10-bit L1 index 10-bit L2 index Root of the Current Page Table Physical Memory p2 p1 (Processor Register) Level 1 Page Table Page size 10b  1024 x 4096B Level 2 Page Tables 12b  4096B page in primary memory page in secondary memory PTE of a nonexistent page Lecture 23: Virtual Memory Data Pages CS252 S05

Lecture 23: Virtual Memory Agenda Virtual Memory Paged Physical Memory Swap Space Hierarchical Page Tables Caching Page Table Entries (TLB) CS 61c Lecture 23: Virtual Memory

Address Translation & Protection Virtual Address Virtual Page No. (VPN) offset Kernel/User Mode Miss Protection Check Read/Write Address Translation Exception? Physical Address Physical Page No. (PPN) offset Every instruction and data access needs address translation and protection checks A good VM design needs to be fast (~ one cycle) and space efficient CS 61c Lecture 23: Virtual Memory CS252 S05

Translation Lookaside Buffers (TLB) Address translation is very expensive! In a two-level page table, each reference becomes three memory accesses Solution: Cache some translations in TLB TLB hit  Single-Cycle Translation TLB miss  Page-Table Walk to refill virtual address VPN offset (VPN = virtual page number) V D tag PPN 3 memory references V – valid R – referenced (know what to evict from cache) R – D – dirty (page was written to) (PPN = physical page number) hit? physical address PPN offset CS 61c Lecture 23: Virtual Memory CS252 S05

Lecture 23: Virtual Memory TLB Designs Typically 32-128 entries, usually fully associative Each entry maps a large page, hence less spatial locality across pages => more likely that two entries conflict Sometimes larger TLBs (256-512 entries) are 4-8 way set-associative Larger systems sometimes have multi-level (L1 and L2) TLBs Random or FIFO replacement policy “TLB Reach”: Size of largest virtual address space that can be simultaneously mapped by TLB Example: 64 TLB entries, 4KB pages, one page per entry TLB Reach = _____________________________________________? 256 KiB mapped at once in the TLB CS 61c Lecture 23: Virtual Memory CS252 S05

VM-related events in pipeline PC D E M W Inst TLB Inst. Cache Decode Data TLB Data Cache + TLB miss? Page Fault? Protection violation? TLB miss? Page Fault? Protection violation? Handling a TLB miss needs a hardware or software mechanism to refill TLB usually done in hardware Handling a page fault (e.g., page is on disk) needs a precise trap so software handler can easily resume after retrieving page Protection violation may abort process CS 61c Lecture 23: Virtual Memory CS252 S05

Hierarchical Page Table Walk: SPARC v8 31 11 Offset Virtual Address Index 1 Index 2 Index 3 Offset 31 23 17 11 0 Context Table Register root ptr PTP PTE Context Table L1 Table L2 Table L3 Table Physical Address PPN PTs can be swapped to disk, too What about code handling page faults? MMU does this table walk in hardware on a TLB miss FSM? CS 61c Lecture 23: Virtual Memory CS252 S05

Page-Based Virtual-Memory Machine (Hardware Page-Table Walk) Page Fault? Protection violation? Page Fault? Protection violation? Virtual Address Virtual Address Physical Address Physical Address PC D E M W Inst. TLB Decode Data TLB Inst. Cache Data Cache + Miss? Miss? Page-Table Base Register Hardware Page Table Walker Physical Address Physical Address Memory Controller Physical Address Main Memory (DRAM) Assumes page tables held in untranslated physical memory CS 61c Lecture 23: Virtual Memory CS252 S05

Address Translation: putting it all together Virtual Address hardware hardware or software software TLB Lookup miss hit Page Table Walk Protection Check the page is Ï memory Î memory Need to restart instruction. Soft and hard page faults. denied permitted Page Fault (OS loads page) Protection Fault Physical Address (to cache) Update TLB Where? SEGFAULT CS 61c Lecture 23: Virtual Memory CS252 S05

Lecture 23: Virtual Memory Modern Virtual Memory Systems Illusion of a large, private, uniform store OS Protection & Privacy Several users/processes, each with their private address space Demand Paging Provides the ability to run programs larger than the primary memory Hides differences in machine configurations The price is address translation on each memory reference useri Swapping Space (Disk) Primary Memory Portability on machines with different memory configurations. mapping VA PA TLB CS 61c Lecture 23: Virtual Memory CS252 S05

Lecture 23: Virtual Memory It’s just the “OS” … Let’s write execve Code the loads program into memory for execution What’s the best way? Filll PT: code, stack. Set up argv, argc Jump to main  page fault Problem: linker! CS 61c Lecture 23: Virtual Memory

Lecture 23: Virtual Memory Execve Memory (DRAM) Disk Set up PT for program code and stack Init argv, argc Call main what happens? page fault Anything wrong with this? linker? fix? Fix  indirection Architecture matters! CS 61c Lecture 23: Virtual Memory

Lecture 23: Virtual Memory And, in Conclusion … Virtual & physical addresses Program  virtual address DRAM  physical address Paged Memory Facilitates virtual  physical address translation Provides isolation & protection Extends available memory to include disk Implementation issues Hierarchical page tables Caching page table entries (TLB) CS 61c Lecture 23: Virtual Memory