Carnegie Mellon 15-213/18-243: Introduction to Computer Systems Instructors: Bill Nace and Gregory Kesden (c) 1998 - 2010. All Rights Reserved. All work.

Slides:



Advertisements
Similar presentations
Memory Management: Overlays and Virtual Memory
Advertisements

Virtual Memory October 25, 2006 Topics Address spaces Motivations for virtual memory Address translation Accelerating translation with TLBs class16.ppt.
Carnegie Mellon 1 Virtual Memory: Concepts : Introduction to Computer Systems 15 th Lecture, Oct. 14, 2010 Instructors: Randy Bryant and Dave O’Hallaron.
Today Virtual memory (VM) Overview and motivation
Virtual Memory October 29, 2007 Topics Address spaces Motivations for virtual memory Address translation Accelerating translation with TLBs class16.ppt.
Virtual Memory Topics Virtual Memory Access Page Table, TLB Programming for locality Memory Mountain Revisited.
Virtual Memory May 19, 2008 Topics Motivations for VM Address translation Accelerating translation with TLBs EECS213.
Carnegie Mellon 1 Virtual Memory: Concepts / : Introduction to Computer Systems 16 th Lecture, Oct. 21, 2014 Instructors: Greg Ganger, Greg.
Virtual Memory: Concepts
Virtual & Dynamic Memory Management Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
Carnegie Mellon 1 Saint Louis University Virtual Memory CSCI 224 / ECE 317: Computer Architecture Instructor: Prof. Jason Fritts Slides adapted from Bryant.
Carnegie Mellon /18-243: Introduction to Computer Systems Instructors: Bill Nace and Gregory Kesden (c) All Rights Reserved. All work.
1 Seoul National University Virtual Memory: Systems.
1 Virtual Memory. 2 Outline Pentium/Linux Memory System Core i7 Suggested reading: 9.6, 9.7.
1 Virtual Memory: Concepts Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
Carnegie Mellon 1 Virtual Memory: Concepts Instructor: Rabi Mahapatra (TAMU) Slides: Randy Bryant and Dave O’Hallaron (CMU)
Carnegie Mellon Introduction to Computer Systems /18-243, spring th Lecture, Mar. 17 th Instructors: Gregory Kesden and Markus Püschel.
Virtual Memory April 3, 2001 Topics Motivations for VM Address translation Accelerating translation with TLBs class20.ppt.
University of Amsterdam Computer Systems – virtual memory Arnoud Visser 1 Computer Systems Virtual Memory.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Demand Paging.
Processes and Virtual Memory
University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?
Virtual Memory.  Next in memory hierarchy  Motivations:  to remove programming burdens of a small, limited amount of main memory  to allow efficient.
Memory Management: Overlays and Virtual Memory. Agenda Overview of Virtual Memory –Review material based on Computer Architecture and OS concepts Credits.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Virtual Memory: Concepts Slides adapted from Bryant.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Virtual Memory: Concepts CENG331 - Computer Organization.
Virtual Memory Topics Motivations for VM Address translation Accelerating translation with TLBs CS 105 “Tour of the Black Holes of Computing!”
Carnegie Mellon Introduction to Computer Systems / Spring 2009 March 23, 2009 Virtual Memory.
University of Washington Roadmap 1 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
Virtual Memory Topics Address spaces Motivations for virtual memory Address translation Accelerating translation with TLBs.
Virtual Memory Topics Motivations for VM Address translation Accelerating translation with TLBs VM1 CS 105 “Tour of the Black Holes of Computing!”
Virtual Memory October 14, 2008 Topics Address spaces Motivations for virtual memory Address translation Accelerating translation with TLBs lecture-14.ppt.
CSE 153 Design of Operating Systems Winter 2015 Lecture 11: Paging/Virtual Memory Some slides modified from originals by Dave O’hallaron.
1 Virtual Memory (I). 2 Outline Physical and Virtual Addressing Address Spaces VM as a Tool for Caching VM as a Tool for Memory Management VM as a Tool.
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
1 Virtual Memory. 2 Outline Virtual Space Address translation Accelerating translation –with a TLB –Multilevel page tables Different points of view Suggested.
Virtual Memory 1 Computer Organization II © McQuain Virtual Memory Use main memory as a “cache” for secondary (disk) storage – Managed jointly.
University of Washington Indirection in Virtual Memory 1 Each process gets its own private virtual address space Solves the previous problems Physical.
Dynamic Memory Management Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
Alan L. Cox Virtual Memory Alan L. Cox Some slides adapted from CMU slides.
1 Virtual Memory. 2 Outline Case analysis –Pentium/Linux Memory System –Core i7 Suggested reading: 9.7.
Virtual Memory Samira Khan Apr 25, 2017.
Section 9: Virtual Memory (VM)
Section 9: Virtual Memory (VM)
Today How was the midterm review? Lab4 due today.
Virtual Memory: Concepts CENG331 - Computer Organization
Virtual Memory.
CS 105 “Tour of the Black Holes of Computing!”
Virtual Memory I CSE 351 Autumn 2017
CSE 153 Design of Operating Systems Winter 2018
CS 105 “Tour of the Black Holes of Computing!”
Virtual Memory II CSE 351 Autumn 2016
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 105 “Tour of the Black Holes of Computing!”
Virtual Memory II CSE 410 Winter 2017
CSE 351 Section 10 The END…Almost 3/7/12
ECE Dept., University of Toronto
Instructors: Majd Sakr and Khaled Harras
Virtual Memory Nov 27, 2007 Slide Source:
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
Virtual Memory II CSE 351 Winter 2018
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CS 105 “Tour of the Black Holes of Computing!”
CS 105 “Tour of the Black Holes of Computing!”
Virtual Memory I CSE 351 Winter 2019
CSE 153 Design of Operating Systems Winter 2019
CSE 153 Design of Operating Systems Winter 2019
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.
Instructor: Phil Gibbons
Presentation transcript:

Carnegie Mellon /18-243: Introduction to Computer Systems Instructors: Bill Nace and Gregory Kesden (c) All Rights Reserved. All work contained herein is copyrighted and used by permission of the authors. Contact for permission or for more 15 th Lecture, 16 March 2010 Virtual Memory I

Carnegie Mellon Signals Kernel → Process Process → Process (using kill ) 32 types of signals Sent by updating bit in pending vector You can write your own signal handlers IDNameDefault ActionCorresponding Event 2SIGINTTerminateInterrupt (e.g., ctl-c from keyboard) 9SIGKILLTerminateKill program (cannot override or ignore) 11SIGSEGVTerminate & DumpSegmentation violation 14SIGALRMTerminateTimer signal 17SIGCHLDIgnoreChild stopped or terminated

Carnegie Mellon Signal Handlers as Concurrent Flows Signal delivered Signal received Process AProcess B user code (main) kernel code user code (main) kernel code user code (handler) context switch kernel code user code (main) I curr I next

Carnegie Mellon Nonlocal Jumps: setjmp / longjmp Powerful (but dangerous) user-level mechanism for transferring control to an arbitrary location  Controlled to way to break the procedure call / return discipline  Useful for error recovery and signal handling int setjmp(jmp_buf buf)  Must be called before longjmp  Identifies a return site for a subsequent longjmp  Called once, returns one or more times Implementation:  Remember where you are by storing the current register context, stack pointer, and PC value in jmp_buf  Return 0

Carnegie Mellon setjmp / longjmp (cont) void longjmp(jmp_buf buf, int i)  Meaning:  return from the setjmp remembered by jump buffer buf again...  … this time returning i instead of 0  Called after setjmp  Called once, but never returns longjmp Implementation:  Restore register context (stack pointer, base pointer, PC value) from jump buffer buf  Set %eax (the return value) to i  Jump to the location indicated by the PC stored in jump buf buf

Carnegie Mellon Today Virtual Memory (VM) overview and motivation VM as tool for caching VM as tool for memory management VM as tool for memory protection Address translation

Carnegie Mellon Programs refer to virtual memory addresses  movl (%ecx),%eax  Conceptually very large array of bytes  Each byte has its own address  Actually implemented with hierarchy of different memory types  System provides address space private to particular “process” Allocation: Compiler and run-time system  Where different program objects should be stored  All allocation within single virtual address space But why virtual memory? Why not physical memory? Virtual Memory Abstraction 00∙∙∙∙∙∙0 FF∙∙∙∙∙∙F

Carnegie Mellon Problem 1: How Does Everything Fit? 64-bit addresses: 16 Exabyte Physical main memory: Few Gigabytes ? And there are many processes ….

Carnegie Mellon Problem 2: Memory Management Physical main memory What goes where? stack heap.text.data … Process 1 Process 2 Process 3 … Process n x

Carnegie Mellon Problem 3: How To Protect Physical main memory Process i Process j Problem 4: How To Share? Physical main memory Process i Process j

Carnegie Mellon Solution: Level Of Indirection Each process gets its own private memory space Solves the previous problems Physical memory Virtual memory Process 1 Process n mapping

Carnegie Mellon Address Spaces Linear address space: Ordered set of contiguous non-negative integer addresses: {0, 1, 2, 3 … } Virtual address space: Set of N = 2 n virtual addresses {0, 1, 2, 3, …, N-1} Physical address space: Set of M = 2 m physical addresses {0, 1, 2, 3, …, M-1} Clean distinction between data (bytes) and their attributes (addresses) Each object can now have multiple addresses Every byte in main memory: one physical address, one (or more) virtual addresses

Carnegie Mellon A System Using Physical Addressing Used in “simple” systems like embedded microcontrollers in devices like cars, elevators, and digital picture frames 0: 1: M-1: Main memory CPU 2: 3: 4: 5: 6: 7: Physical address (PA) Data word 8:...

Carnegie Mellon A System Using Virtual Addressing Used in all modern desktops, laptops, workstations One of the great ideas in computer science MMU checks the cache 0: 1: M-1: Main memory MMU 2: 3: 4: 5: 6: 7: Physical address (PA) Data word 8:... CPU Virtual address (VA) CPU Chip

Carnegie Mellon Why Virtual Memory (VM)? Efficient use of limited main memory (RAM)  Use RAM as a cache for the parts of a virtual address space  some non-cached parts stored on disk  some (unallocated) non-cached parts stored nowhere  Keep only active areas of virtual address space in memory  transfer data back and forth as needed Simplifies memory management for programmers  Each process gets the same full, private linear address space Isolates address spaces  One process can’t interfere with another’s memory  because they operate in different address spaces  User process cannot access privileged information  different sections of address spaces have different permissions

Carnegie Mellon Today Virtual Memory (VM) overview and motivation VM as tool for caching VM as tool for memory management VM as tool for memory protection Address translation

Carnegie Mellon VM as a Tool for Caching Virtual memory: array of N = 2 n contiguous bytes  think of the array (allocated part) as being stored on disk Physical memory (DRAM) = cache for allocated virtual memory Blocks are called pages; size = 2 p PP 2 m-p -1 Physical memory Empty Uncached VP 0 VP 1 VP 2 n-p -1 Virtual memory Unallocated Cached Uncached Unallocated Cached Uncached PP 0 PP 1 Empty Cached 0 2 n -1 2 m -1 0 Virtual pages (VP's) stored on disk Physical pages (PP's) cached in DRAM Disk

Carnegie Mellon Memory Hierarchy: Core 2 Duo Disk Main Memory L2 unified cache L1 I-cache L1 D-cache CPUReg 2 B/cycle8 B/cycle16 B/cycle1 B/30 cyclesThroughput: Latency:100 cycles14 cycles3 cyclesmillions ~4 MB 32 KB ~4 GB~500 GB Not drawn to scale L1/L2 cache: 64 B blocks Miss penalty (latency): 10,000x Miss penalty (latency): 30x

Carnegie Mellon DRAM Cache Organization DRAM cache organization driven by the enormous miss penalty  DRAM is about 10x slower than SRAM  Disk is about 10,000x slower than DRAM  For first byte, faster for next byte Consequences  Large page (block) size: typically 4-8 KB, sometimes 4 MB  Fully associative  Any VP can be placed in any PP  Requires a “large” mapping function – different from CPU caches  Highly sophisticated, expensive replacement algorithms  Too complicated and open-ended to be implemented in hardware  Write-back rather than write-through

PTE 00null PTE 71 Carnegie Mellon Address Translation: Page Tables A page table is an array of page table entries (PTEs) that maps virtual pages to physical pages. Here: 8 VPs  Per-process kernel data structure in DRAM Memory resident page table (DRAM) Physical memory (DRAM) Virtual memory (disk) Valid Physical page number or disk address VP1PP 0 VP2 VP7 VP4PP 3 VP 1 VP 2 VP 3 VP 4 VP 6 VP 7

Virtual Address Physical Address Carnegie Mellon Address Translation With a Page Table Virtual page offset (VPO)Virtual page number (VPN) Virtual address Physical page number (PPN)Physical page offset (PPO) Physical address Page table base register (PTBR) Page table address for process Page table Valid bit = 0: page not in memory (page fault) ValidPhysical page number (PPN)

Carnegie Mellon Page Hit Page hit: reference to VM word that is in physical memory Virtual address PTE 00null PTE 71 Memory resident page table (DRAM) Physical memory (DRAM) Virtual memory (disk) Valid Physical page number or disk address VP1PP 0 VP2 VP7 VP4PP 3 VP 1 VP 2 VP 3 VP 4 VP 6 VP 7

Carnegie Mellon Page Miss Page miss: reference to VM word that is not in physical memory Virtual address PTE 00null PTE 71 Memory resident page table (DRAM) Physical memory (DRAM) Virtual memory (disk) Valid Physical page number or disk address VP1PP 0 VP2 VP7 VP4PP 3 VP 1 VP 2 VP 3 VP 4 VP 6 VP 7

Carnegie Mellon Handling Page Fault Page miss causes page fault (an exception) Virtual address PTE 00null PTE 71 Memory resident page table (DRAM) Physical memory (DRAM) Virtual memory (disk) Valid Physical page number or disk address VP1PP 0 VP2 VP7 VP4PP 3 VP 1 VP 2 VP 3 VP 4 VP 6 VP 7

Carnegie Mellon Handling Page Fault Page miss causes page fault (an exception) Page fault handler selects a victim to be evicted (here VP 4) Virtual address PTE 00null PTE 71 Memory resident page table (DRAM) Physical memory (DRAM) Virtual memory (disk) Valid Physical page number or disk address VP1PP 0 VP2 VP7 VP4PP 3 VP 1 VP 2 VP 3 VP 4 VP 6 VP 7

Carnegie Mellon Handling Page Fault Page miss causes page fault (an exception) Page fault handler selects a victim to be evicted (here VP 4) Virtual address PTE 00null PTE 71 Memory resident page table (DRAM) Physical memory (DRAM) PTE 00null PTE 71 Virtual memory (disk) Valid Physical page number or disk address VP1PP 0 VP2 VP7 VP4PP 3 VP 1 VP 2 VP 3 VP 4 VP 6 VP 7 VP3 VP1PP 0 VP2 VP7 VP3PP 3

PTE 00null PTE 71 VP1PP 0 VP2 VP7 VP3PP 3 Carnegie Mellon Handling Page Fault Page miss causes page fault (an exception) Page fault handler selects a victim to be evicted (here VP 4) Offending instruction is restarted: page hit! Virtual address Memory resident page table (DRAM) Physical memory (DRAM) Virtual memory (disk) Valid Physical page number or disk address VP 1 VP 2 VP 3 VP 4 VP 6 VP 7

Carnegie Mellon Why does it work? Locality Virtual memory works because of locality At any point in time, programs tend to access a set of active virtual pages called the working set  Programs with better temporal locality will have smaller working sets If (working set size < main memory size)  Good performance for one process after compulsory misses If ( SUM(working set sizes) > main memory size )  Thrashing: Performance meltdown where pages are swapped (copied) in and out continuously

Carnegie Mellon Today Virtual Memory (VM) overview and motivation VM as tool for caching VM as tool for memory management VM as tool for memory protection Address translation

Carnegie Mellon VM as a Tool for Memory Management Key idea: each process has its own virtual address space  It can view memory as a simple linear array  Mapping function scatters addresses through physical memory  Well chosen mappings simplify memory allocation and management Virtual Address Space for Process 1: Physical Address Space (DRAM) 0 N-1 (e.g., read-only library code) Virtual Address Space for Process 2: VP 1 VP N-1 VP 1 VP 2... PP 2 PP 6 PP M-1 Address translation

Carnegie Mellon VM as a Tool for Memory Management Memory allocation  Each virtual page can be mapped to any physical page  A virtual page can be stored in different physical pages at different times Sharing code and data among processes  Map virtual pages to the same physical page (here: PP 6) Virtual Address Space for Process 1: Physical Address Space (DRAM) 0 N-1 (e.g., read-only library code) Virtual Address Space for Process 2: VP 1 VP N-1 VP 1 VP 2... PP 2 PP 6 PP M-1 Address translation

Carnegie Mellon Simplifying Linking and Loading Linking  Each program has similar virtual address space  Code, stack, and shared libraries always start at the same address Loading  execve() allocates virtual pages for.text and.data sections = creates PTEs marked as invalid  The.text and.data sections are copied, page by page, on demand by the virtual memory system Kernel virtual memory Memory-mapped region for shared libraries Run-time heap (created by malloc ) User stack (created at runtime) Unused 0 %esp (stack pointer) Memory invisible to user code brk 0xc x x Read/write segment (. data,. bss ) Read-only segment (.init,. text,.rodata ) Loaded from the executable file

Carnegie Mellon Today Virtual Memory (VM) overview and motivation VM as tool for caching VM as tool for memory management VM as tool for memory protection Address translation

Carnegie Mellon VM as a Tool for Memory Protection Extend PTEs with permission bits Page fault handler checks these before remapping  If violated, send process SIGSEGV (segmentation fault) Process i: Process j: Physical Address Space PP 2 PP 4 PP 6 PP 8 PP 9 PP 11 SUPREADWRITEAddress VP 0:NOYESNOPP 6 VP 1:NOYES PP 4 VP 2:YES PP 2 SUPREADWRITEAddress VP 0:NOYES PP 9 VP 1:YES NOPP 6 VP 2:NOYES PP 11

Carnegie Mellon Today Virtual Memory (VM) overview and motivation VM as tool for caching VM as tool for memory management VM as tool for memory protection Address translation

Carnegie Mellon Address Translation: Page Hit 1) Processor sends virtual address to MMU 2-3) MMU fetches PTE from page table in memory 4) MMU sends physical address to cache/memory 5) Cache/memory sends data word to processor MMU Cache/ Memory PA Data CPU VA CPU Chip PTEA PTE

MMU CPU CPU Chip Carnegie Mellon Address Translation: Page Fault 1) Processor sends virtual address to MMU 2-3) MMU fetches PTE from page table in memory 4) Valid bit is zero, so MMU triggers page fault exception 5) Handler identifies victim (and, if dirty, pages it out to disk) 6) Handler pages in new page and updates PTE in memory 7) Handler returns to original process, restarting faulting instruction Cache/ Memory VA PTEA PTE Disk Page fault handler Victim page New page Exception 6 7

Carnegie Mellon Summary Virtual / Physical Addresses Memory Pages Page Tables Address Translation Next Time: Virtual Memory II  Using TLB to speed address translation  Linux memory management