Virtual Memory Hakim Weatherspoon CS 3410, Spring 2012

Slides:



Advertisements
Similar presentations
COMP 3221: Microprocessors and Embedded Systems Lectures 27: Virtual Memory - III Lecturer: Hui Wu Session 2, 2005 Modified.
Advertisements

Virtual Memory 3 Hakim Weatherspoon CS 3410, Spring 2011 Computer Science Cornell University P & H Chapter
CSE 490/590, Spring 2011 CSE 490/590 Computer Architecture Virtual Memory I Steve Ko Computer Sciences and Engineering University at Buffalo.
Virtual Memory 2 Hakim Weatherspoon CS 3410, Spring 2011 Computer Science Cornell University P & H Chapter
Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University Virtual Memory 1 P & H Chapter 5.4 (up to TLBs)
Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University Virtual Memory 2 P & H Chapter
Virtual Memory 1 Hakim Weatherspoon CS 3410, Spring 2011 Computer Science Cornell University P & H Chapter 5.4 (up to TLBs)
CS 153 Design of Operating Systems Spring 2015
Memory Management and Paging CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Caches Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University See P&H 5.1, 5.2 (except writes)
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)
Virtual Memory. Virtual Memory: Topics Why virtual memory? Virtual to physical address translation Page Table Translation Lookaside Buffer (TLB)
1 Memory Management. 2 Fixed Partitions Legend Free Space 0k 4k 16k 64k 128k Internal fragmentation (cannot be reallocated) Divide memory into n (possible.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Virtual Memory Hardware.
Virtual Memory 3 Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University P & H Chapter 5.4.
CS2100 Computer Organisation Virtual Memory – Own reading only (AY2015/6) Semester 1.
Lab4: Virtual Memory CS 3410 : Computer System Organization & Programming Spring 2015.
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)
Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University P & H Chapter 5.7.
Virtual Memory 1 Computer Organization II © McQuain Virtual Memory Use main memory as a “cache” for secondary (disk) storage – Managed jointly.
CS203 – Advanced Computer Architecture Virtual Memory.
CS161 – Design and Architecture of Computer
Translation Lookaside Buffer
Lecture 11 Virtual Memory
Virtual Memory Acknowledgment
Virtual Memory Chapter 7.4.
ECE232: Hardware Organization and Design
Memory COMPUTER ARCHITECTURE
CS161 – Design and Architecture of Computer
CSE 120 Principles of Operating
Virtual Memory User memory model so far:
CS703 - Advanced Operating Systems
Section 9: Virtual Memory (VM)
Today How was the midterm review? Lab4 due today.
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?
Virtual Memory Hakim Weatherspoon CS 3410, Spring 2013
Memory Hierarchy Virtual Memory, Address Translation
Morgan Kaufmann Publishers
CSE 153 Design of Operating Systems Winter 2018
Virtual Memory 2 Hakim Weatherspoon CS 3410, Spring 2012
Caches (Writing) Hakim Weatherspoon CS 3410, Spring 2013
Prelim 3 Review Hakim Weatherspoon CS 3410, Spring 2013
CSCI206 - Computer Organization & Programming
Caches 2 Hakim Weatherspoon CS 3410, Spring 2013 Computer Science
Virtual Memory 3 Hakim Weatherspoon CS 3410, Spring 2011
Lecture 14 Virtual Memory and the Alpha Memory Hierarchy
CS 105 “Tour of the Black Holes of Computing!”
CS399 New Beginnings Jonathan Walpole.
Prof. Hakim Weatherspoon
Virtual Memory Hardware
Translation Lookaside Buffer
Morgan Kaufmann Publishers Memory Hierarchy: Virtual Memory
Virtual Memory Nov 27, 2007 Slide Source:
CSE 451: Operating Systems Autumn 2005 Memory Management
Prof. Kavita Bala and Prof. Hakim Weatherspoon
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
Virtual Memory 2 Hakim Weatherspoon CS 3410, Spring 2012
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
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!”
CS 3410 Computer System Organization & Programming
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 Hakim Weatherspoon CS 3410 Computer Science
Virtual Memory Hakim Weatherspoon CS 3410 Computer Science
Caches & Memory.
Instructor: Phil Gibbons
Presentation transcript:

Virtual Memory Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University P & H Chapter 5.4 (up to TLBs)

Goals for Today Virtual Memory Address Translation Paging Pages, page tables, and memory mgmt unit Paging Role of Operating System Context switches, working set, shared memory Performance How slow is it Making virtual memory fast Translation lookaside buffer (TLB) Virtual Memory Meets Caching

Virtual Memory

Big Picture: Multiple Processes How to Run multiple processes? Time-multiplex a single CPU core (multi-tasking) Web browser, skype, office, … all must co-exist Many cores per processor (multi-core) or many processors (multi-processor) Multiple programs run simultaneously

Big Picture: (Virtual) Memory Memory: big & slow vs Caches: small & fast Write- Back compute jump/branch targets Instruction Fetch Instruction Decode Execute Memory memory register file A alu D D B +4 addr PC control din dout inst B M memory extend new pc imm forward unit detect hazard ctrl ctrl ctrl IF/ID ID/EX EX/MEM MEM/WB

Big Picture: (Virtual) Memory Memory: big & slow vs Caches: small & fast Processor Cache Memory 0xfff…f 0x7ff…f Stack LB $1  M[ 1 ] LB $2  M[ 5 ] LB $3  M[ 1 ] LB $3  M[ 4 ] LB $2  M[ 0 ] LB $2  M[ 12 ] LB $2  M[ 12 ] LB $2  M[ 5 ] tag data 100 110 Heap 1 2 140 150 Data Text Misses: Hits: 0x000…0

Processor & Memory CPU address/data bus... … routed through caches … to main memory Simple, fast, but… Q: What happens for LW/SW to an invalid location? 0x000000000 (NULL) uninitialized pointer A: Need a memory management unit (MMU) Throw (and/or handle) an exception CPU 0xfff…f 0x7ff…f Stack Heap Data Text 0x000…0 Memory

Multiple Processes Q: What happens when another program is executed concurrently on another processor? A: The addresses will conflict Even though, CPUs may take turns using memory bus CPU 0xfff…f 0x7ff…f Stack Stack Heap Heap CPU Data Data Text Text 0x000…0 Memory

Multiple Processes Q: Can we relocate second program? CPU CPU Memory 0xfff…f 0x7ff…f Stack Stack Heap Heap CPU Data Data Text Text 0x000…0 Memory

Solution? Multiple processes/processors Q: Can we relocate second program? A: Yes, but… What if they don’t fit? What if not contiguous? Need to recompile/relink? … Stack CPU Data Stack Heap Heap CPU Data Text Text Memory

All problems in computer science can be solved by another level of indirection. – David Wheeler – or, Butler Lampson – or, Leslie Lamport – or, Steve Bellovin

Virtual Memory Virtual Memory: A Solution for All Problems Each process has its own virtual address space Programmer can code as if they own all of memory On-the-fly at runtime, for each memory access all access is indirect through a virtual address translate fake virtual address to a real physical address redirect load/store to the physical address

Address Space wikipedia

Address Space Programs load/store to virtual addresses X CPU CPU C A X B B Y C Z Z MMU MMU Y A Programs load/store to virtual addresses Actual memory uses physical addresses Memory Management Unit (MMU) Responsible for translating on the fly Essentially, just a big array of integers: paddr = PageTable[vaddr]; each mmu has own mappings Easy relocation Higher memory utilization Easy sharing

Virtual Memory Advantages Easy relocation Loader puts code anywhere in physical memory Creates virtual mappings to give illusion of correct layout Higher memory utilization Provide illusion of contiguous memory Use all physical memory, even physical address 0x0 Easy sharing Different mappings for different programs / cores And more to come…

Address Translation Pages, Page Tables, and the Memory Management Unit (MMU)

Address Translation Attempt #1: How does MMU translate addresses? paddr = PageTable[vaddr]; Granularity? Per word… Per block… Variable..… Typical: 4KB – 16KB pages 4MB – 256MB jumbo pages per word … too expensive: 4GB PageTable arrray per block… ? Variable … complicated hardware We will stick to 4kb pages

Address Translation Attempt #1: For any access to virtual address: Virtual page number Page Offset vaddr Lookup in PageTable Physical page number Page offset paddr Attempt #1: For any access to virtual address: Calculate virtual page number and page offset Lookup physical page number at PageTable[vpn] Calculate physical address as ppn:offset 4kb pages = 12 bits for page offset, 20 bits for VPN

Simple PageTable CPU MMU Read Mem[0x4123B538] 0xC20A3000 Data CPU MMU Q: Where to store page tables? A: In memory, of course… Special page table base register (CR3:PTBR on x86) (Cop0:ContextRegister on MIPS) 0x90000000 0x4123B000 VADDR = 0x00401538 PFN = VADDR/4kb = VADDR>>12 = 0x401 PageTableEntry at 0x90000804 contains 0x4123B000 Data at 0x4123B538 0x10045000 0x10044000 0x00000000

Simple PageTable vaddr Physical Page Number 0x10045 0xC20A3 0x4123B vpn pgoff 0x10045000 vaddr 0x10044000 0x00000000 PTBR

Invalid Pages V Physical Page Number 1 0x10045 0xC20A3 0x4123B 0x10044 0xC20A3000 0x90000000 0x4123B000 Cool Trick #1: Don’t map all pages Need valid bit for each page table entry Q: Why? A: now access to NULL will fail A: we might not have that much physical memory A: now access to NULL will fail A: we might not have that much physical memory 0x10045000 0x10044000 0x00000000

Page Size Example Overhead for VM Attempt #1 (example) Virtual address space (for each process): total memory: 232 bytes = 4GB page size: 212 bytes = 4KB entries in PageTable? size of PageTable? Physical address space: total memory: 229 bytes = 512MB overhead for 10 processes? 220 entries in pagetable, so 222 bytes = 4MB (w/ 4byte entries), so 40MB (25% of total!) Q: Can we possibly have a “full” pagetable? A: Not enough physical memory pages – some pagetable entries will be duplicates!

Beyond Flat Page Tables Assume most of PageTable is empty How to translate addresses? Multi-level PageTable vaddr 10 bits 10 bits 10 bits 2 Word PTEntry Page Q: Benefits? A1: Don’t need 4MB contiguous physical memory A2: Don’t need to allocate every PageTable, only those containing valid PTEs Q: Drawbacks? A: Longer lookups. PDEntry Page Table PTBR Page Directory * x86 does exactly this

Beyond Flat Page Tables Assume most of PageTable is empty How to translate addresses? Multi-level PageTable Q: Benefits? A: Don’t need 4MB contiguous physical memory A: Don’t need to allocate every PageTable, only those containing valid PTEs Q: Drawbacks A: Performance: Longer lookups Q: Benefits? A1: Don’t need 4MB contiguous physical memory A2: Don’t need to allocate every PageTable, only those containing valid PTEs Q: Drawbacks? A: Longer lookups.

Page Permissions V R W X Physical Page Number 1 0x10045 0xC20A3 0x4123B 0x10044 0xC20A3000 0x90000000 0x4123B000 Cool Trick #2: Page permissions! Keep R, W, X permission bits for each page table entry Q: Why? A: can make code read-only, executable; make data read-write but not executable; etc. A: can make code read-only, executable; make data read-write but not executable; etc. 0x10045000 0x10044000 0x00000000

Aliasing V R W X Physical Page Number 1 0xC20A3 0x4123B 0x10044 0xC20A3000 0x90000000 0x4123B000 Cool Trick #3: Aliasing Map the same physical page at several virtual addresses Q: Why? A: can make different views of same data with different permissions A: can make different views of same data with different permissions 0x10045000 0x10044000 0x00000000

Paging

Paging Can we run process larger than physical memory? The “virtual” in “virtual memory” View memory as a “cache” for secondary storage Swap memory pages out to disk when not in use Page them back in when needed Assumes Temporal/Spatial Locality Pages used recently most likely to be used again soon

Paging V R W X D Physical Page Number invalid 1 0x10045 disk sector 200 disk sector 25 0x00000 0xC20A3000 0x90000000 0x4123B000 0x10045000 Cool Trick #4: Paging/Swapping Need more bits: Dirty, RecentlyUsed, … 0x00000000 A: can make code read-only, executable; make data read-write but not executable; etc. 200 25

Summary Virtual Memory Next time Address Translation Paging Pages, page tables, and memory mgmt unit Paging Next time Role of Operating System Context switches, working set, shared memory Performance How slow is it Making virtual memory fast Translation lookaside buffer (TLB) Virtual Memory Meets Caching

Administrivia Lab3 is due next Monday HW5 is due next Tuesday Extra Lab Help session: Saturday, April 7th, 12:00-2:30pm in CSUGLab HW5 is due next Tuesday Download updated version. Use updated version. Online Survey available later this evening