Virtual Memory Samira Khan Apr 25, 2017.

Slides:



Advertisements
Similar presentations
Virtual Memory October 25, 2006 Topics Address spaces Motivations for virtual memory Address translation Accelerating translation with TLBs class16.ppt.
Advertisements

Carnegie Mellon 1 Virtual Memory: Concepts : Introduction to Computer Systems 15 th Lecture, Oct. 14, 2010 Instructors: Randy Bryant and Dave O’Hallaron.
Virtual Memory October 29, 2007 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 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.
Lecture 19: Virtual Memory
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: 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)
The Three C’s of Misses 7.5 Compulsory Misses The first time a memory location is accessed, it is always a miss Also known as cold-start misses Only way.
Carnegie Mellon /18-243: Introduction to Computer Systems Instructors: Bill Nace and Gregory Kesden (c) All Rights Reserved. All work.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Demand Paging.
Virtual Memory.  Next in memory hierarchy  Motivations:  to remove programming burdens of a small, limited amount of main memory  to allow efficient.
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!”
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.
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.
Constructive Computer Architecture Virtual Memory: From Address Translation to Demand Paging Arvind Computer Science & Artificial Intelligence Lab. Massachusetts.
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.
Computer Architecture Lecture 12: Virtual Memory I
CS161 – Design and Architecture of Computer
Virtual Memory Samira Khan Apr 27, 2017.
Virtual Memory Chapter 7.4.
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
Section 9: Virtual Memory (VM)
From Address Translation to Demand Paging
Today How was the midterm review? Lab4 due today.
CS 704 Advanced Computer Architecture
Virtual Memory.
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.
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.
Lecture 14 Virtual Memory and the Alpha Memory Hierarchy
From Address Translation to Demand Paging
CS 105 “Tour of the Black Holes of Computing!”
ECE Dept., University of Toronto
Instructors: Majd Sakr and Khaled Harras
Morgan Kaufmann Publishers Memory Hierarchy: Virtual Memory
Virtual Memory Nov 27, 2007 Slide Source:
CSE 451: Operating Systems Autumn 2005 Memory Management
Contents Memory types & memory hierarchy Virtual memory (VM)
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
Virtual Memory II CSE 351 Winter 2018
CSC3050 – Computer Architecture
CS 105 “Tour of the Black Holes of Computing!”
Computer Architecture
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
CS 105 “Tour of the Black Holes of Computing!”
CSE 153 Design of Operating Systems Winter 2019
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.
CS161 – Design and Architecture of Computer
Design of Digital Circuits Lecture 25b: Virtual Memory
Instructor: Phil Gibbons
Virtual Memory 1 1.
Presentation transcript:

Virtual Memory Samira Khan Apr 25, 2017

Memory (Programmer’s View)

Solution: Virtual Memory (today and next lecture) Hmmm, How Does This Work?! Process 1 Process 2 Process n Solution: Virtual Memory (today and next lecture)

Today Virtual Memory: Concepts Benefits of VM Address translation VM as a tool for caching VM as a tool for memory management VM as a tool for memory protection Address translation

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

The Problem Physical memory is of limited size (cost) What if you need more? Should the programmer be concerned about the size of code/data blocks fitting physical memory? How to manage data movement from disk to physical memory? How to ensure two processes do not use the same physical memory? Also, ISA can have an address space greater than the physical memory size E.g., a 64-bit address space with byte addressability What if you do not have enough physical memory?

Difficulties of Direct Physical Addressing Programmer needs to manage physical memory space Inconvenient & hard Harder when you have multiple processes Difficult to support code and data relocation Difficult to support multiple processes Protection and isolation between multiple processes Sharing of physical memory space Difficult to support data/code sharing across processes physical mem. active process’s region another process’s

Virtual Memory Idea: Give the programmer the illusion of a large address space while having a small physical memory So that the programmer does not worry about managing physical memory Programmer can assume he/she has “infinite” amount of physical memory Hardware and software cooperatively and automatically manage the physical memory space to provide the illusion Illusion is maintained for each independent process

Abstraction: Virtual vs. Physical Memory Programmer sees virtual memory Can assume the memory is “infinite” Reality: Physical memory size is much smaller than what the programmer assumes The system (system software + hardware, cooperatively) maps virtual memory addresses are to physical memory The system automatically manages the physical memory space transparently to the programmer + Programmer does not need to know the physical size of memory nor manage it  A small physical memory can appear as a huge one to the programmer  Life is easier for the programmer -- More complex system software and architecture A classic example of the programmer/(micro)architect tradeoff

Basic Mechanism Indirection (in addressing) Address generated by each instruction in a program is a “virtual address” i.e., it is not the physical address used to address main memory An “address translation” mechanism maps this address to a “physical address” Address translation mechanism can be implemented in hardware and software together “At the heart [...] is the notion that ‘address’ is a concept distinct from ‘physical location.’” Peter Denning

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

Virtual Pages, Physical Frames Virtual address space divided into pages Physical address space divided into frames A virtual page is mapped to A physical frame, if the page is in physical memory A location in disk, otherwise If an accessed virtual page is not in memory, but on disk Virtual memory system brings the page into a physical frame and adjusts the mapping  this is called demand paging Page table is the table that stores the mapping of virtual pages to physical frames

Overview of Paging Mapping virtual 4GB Process 1 physical virtual 4GB Virtual Page virtual Process 1 4GB Physical Page Frame physical Mapping 16MB Virtual Page virtual Process 2 4GB

Overview of Paging Map virtual pages to physical pages By itself, a virtual page is merely an illusion Cannot actually store anything Needs to be backed-up by a physical page Before a virtual page can be accessed … It must be paired with a physical page I.e., it must be mapped to a physical page This mapping is stored in a page table On every subsequent access to the virtual page … Its mapping is looked up Then, the access is directed to the physical page

A System with Virtual Memory (Page based) Address Translation: The hardware converts virtual addresses into physical addresses via an OS-managed lookup table (page table) Memory 0: 1: N-1: Page Table Virtual Addresses Physical Addresses 0: 1: CPU P-1: Disk

Paging in Intel 80386 Intel 80386 (Mid 80s) 32-bit processor 4KB virtual/physical pages Q: What is the size of a virtual address space? A: 2^32 = 4GB Q: How many virtual pages per virtual address space? A: 4GB/4KB = 2^20 Let us assume that physical addresses are 28 bits Q: What is the size of the physical address space? A: 2^28 = 256MB Q: How many physical pages in the physical address space? A: 256MB/4KB = 65536

··· Virtual Pages 32-bit Virtual Address 31 12 11 0000000000 XXXXX 4GB 0000000000 XXXXX 4GB Virtual Page 1M-1 ··· 12KB Virtual Page 2 8KB Virtual Page 1 4KB Virtual Page 0 0KB

··· Virtual Pages 32-bit Virtual Address 31 12 11 0000000001 XXXXX 4GB 0000000001 XXXXX 4GB Virtual Page 1M-1 ··· 12KB Virtual Page 2 8KB Virtual Page 1 4KB Virtual Page 0 0KB

··· Virtual Pages 32-bit Virtual Address 31 12 11 1111111111 XXXXX 4GB 1111111111 XXXXX 4GB Virtual Page 1M-1 ··· 12KB Virtual Page 2 8KB Virtual Page 1 4KB Virtual Page 0 0KB

··· Virtual Pages 32-bit Virtual Address VPN 31 12 11 1111111111 XXXXX 1111111111 XXXXX VPN (Virtual Page No.) 4GB Virtual Page 1M-1 ··· 12KB Virtual Page 2 8KB Virtual Page 1 4KB Virtual Page 0 0KB

··· Intel 80386: Virtual Pages 32-bit Virtual Address VPN Offset 31 12 11 1111111111 XXXXX VPN (Virtual Page No.) Offset 4GB Virtual Page 1M-1 ··· 12KB Virtual Page 2 8KB Virtual Page 1 4KB Virtual Page 0 0KB

Translation Translated VPN Offset PPN Offset Assume: Virtual Page 7 is mapped to Physical Page 32 For an access to Virtual Page 7 … 31 12 11 Virtual Address: 0000000111 011001 VPN Offset Translated 27 12 11 Physical Address: 0000100000 011001 PPN Offset

VPN → PPN How to keep track of VPN → PPN mappings? Page Table: A “lookup table” for the mappings Can be thought of as an array Each element in the array is called a page table entry (PTE) uint32 PAGE_TABLE[1<<20]; PAGE_TABLE[65]=981; PAGE_TABLE[3161]=1629; PAGE_TABLE[9327]=524; ...

Today Virtual Memory: Concepts Benefits of VM Address translation VM as a tool for caching VM as a tool for memory management VM as a tool for memory protection Address translation

Why Virtual Memory (VM)? Uses main memory efficiently Use DRAM as a cache for parts of a virtual address space Simplifies memory management Each process gets the same uniform linear address space Isolates address spaces One process can’t interfere with another’s memory User program cannot access privileged kernel information and code

VM as a Tool for Caching Conceptually, virtual memory is an array of N contiguous bytes stored on disk The contents of the array on disk are cached in physical memory (DRAM cache) These cache blocks are called pages (size is P = 2p bytes) Virtual memory Physical memory VP 0 Unallocated VP 1 Cached Empty PP 0 Uncached PP 1 Unallocated Empty Cached Uncached Empty Cached PP 2m-p-1 M-1 VP 2n-p-1 Uncached N-1 Virtual pages (VPs) stored on disk Physical pages (PPs) cached in DRAM

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 Consequences Large page (block) size: typically 4 KB, sometimes 4 MB Fully associative Any VP can be placed in any PP Requires a “large” mapping function – different from cache memories Highly sophisticated, expensive replacement algorithms Too complicated and open-ended to be implemented in hardware Write-back rather than write-through

Enabling Data Structure: Page Table A page table is an array of page table entries (PTEs) that maps virtual pages to physical pages. Per-process kernel data structure in DRAM Physical memory (DRAM) Physical page number or disk address VP 1 PP 0 Valid VP 2 PTE 0 null VP 7 1 VP 4 PP 3 1 Virtual memory (disk) 1 null VP 0 PTE 7 1 VP 1 Memory resident page table (DRAM) VP 2 VP 3 VP 4 VP 6 VP 7

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

Page Fault Page fault: reference to VM word that is not in physical memory (miss) Physical memory (DRAM) Physical page number or disk address Virtual address VP 1 PP 0 Valid VP 2 PTE 0 null VP 7 1 VP 4 PP 3 1 Virtual memory (disk) 1 null PTE 7 1 VP 1 Memory resident page table (DRAM) VP 2 VP 3 VP 4 VP 6 VP 7

Handling Page Fault Page miss causes page fault (an exception) Physical memory (DRAM) Physical page number or disk address Virtual address VP 1 PP 0 Valid VP 2 PTE 0 null VP 7 1 VP 4 PP 3 1 Virtual memory (disk) 1 null PTE 7 1 VP 1 Memory resident page table (DRAM) VP 2 VP 3 VP 4 VP 6 VP 7

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

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

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! Physical memory (DRAM) Physical page number or disk address Virtual address Valid VP 1 PP 0 VP 2 PTE 0 null VP 7 1 VP 3 PP 3 1 1 Virtual memory (disk) null PTE 7 1 VP 1 Memory resident page table (DRAM) VP 2 VP 3 VP 4 Key point: Waiting until the miss to copy the page to DRAM is known as demand paging VP 6 VP 7

Allocating Pages Allocating a new page (VP 5) of virtual memory. Physical memory (DRAM) Physical page number or disk address PP 0 Valid VP 1 VP 2 PTE 0 null VP 7 1 VP 3 PP 3 1 1 Virtual memory (disk) PTE 7 1 VP 1 Memory resident page table (DRAM) VP 2 VP 3 VP 4 VP 5 VP 6 VP 7

Locality to the Rescue Again! Virtual memory seems terribly inefficient, but it 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

Today Virtual Memory: Concepts Benefits of VM Address translation VM as a tool for caching VM as a tool for memory management VM as a tool for memory protection Address translation

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 can improve locality Address translation Virtual Address Space for Process 1: Physical Address Space (DRAM) VP 1 VP 2 PP 2 ... N-1 (e.g., read-only library code) PP 6 Virtual Address Space for Process 2: PP 8 VP 1 VP 2 ... ... N-1 M-1

VM as a Tool for Memory Management Simplifying 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) Address translation Virtual Address Space for Process 1: Physical Address Space (DRAM) VP 1 VP 2 PP 2 ... N-1 (e.g., read-only library code) PP 6 Virtual Address Space for Process 2: PP 8 VP 1 VP 2 ... ... N-1 M-1

Today Virtual Memory: Concepts Benefits of VM Address translation VM as a tool for caching VM as a tool for memory management VM as a tool for memory protection Address translation

Page-Level Access Control (Protection) Not every process is allowed to access every page E.g., may need supervisor level privilege to access system pages Idea: Store access control information on a page basis in the process’s page table Enforce access control at the same time as translation  Virtual memory system serves two functions today Address translation (for illusion of large physical memory) Access control (protection)

VM as a Tool for Memory Protection Extend PTEs with permission bits MMU checks these bits on each access Physical Address Space Process i: SUP READ WRITE EXEC Address VP 0: No Yes No Yes PP 6 VP 1: No Yes Yes Yes PP 4 PP 2 VP 2: Yes Yes Yes No PP 2 • PP 4 PP 6 Process j: SUP READ WRITE EXEC Address PP 8 VP 0: No Yes No Yes PP 9 PP 9 VP 1: Yes Yes Yes Yes PP 6 VP 2: No Yes Yes Yes PP 11 PP 11

Today Virtual Memory: Concepts Benefits of VM Address translation VM as a tool for caching VM as a tool for memory management VM as a tool for memory protection Address translation

Address Translation With a Page Table Virtual address n-1 p p-1 Page table base register (PTBR) (CR3 in x86) Virtual page number (VPN) Virtual page offset (VPO) Page table Valid Physical page number (PPN) Physical page table address for the current process Valid bit = 0: Page not in memory (page fault) Valid bit = 1 m-1 p p-1 Physical page number (PPN) Physical page offset (PPO) Physical address

Address Translation: Page Hit 2 CPU Chip Cache/ Memory PTEA MMU 1 PTE CPU VA 3 PA 4 Data 5 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

Address Translation: Page Fault Exception Page fault handler 4 2 CPU Chip Cache/ Memory Disk PTEA Victim page MMU 1 5 CPU VA PTE 7 3 New page 6 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

Integrating VM and Cache PTE CPU Chip PTE MMU PTEA hit Memory PTEA miss PTEA PTEA CPU VA PA PA PA miss Data PA hit L1 cache Data VA: virtual address, PA: physical address, PTE: page table entry, PTEA = PTE address

Two Problems Problem #1: Page table is too large Two problems with page tables Problem #1: Page table is too large Problem #2: Page table is stored in memory Before every memory access, always fetch the PTE from the slow memory?  Large performance penalty

Multi-Level Page Tables Suppose: 4KB (212) page size, 48-bit address space, 8-byte PTE Problem: Would need a 512 GB page table! 248 * 2-12 * 23 = 239 bytes Common solution: Multi-level page table Example: 2-level page table Level 1 table: each PTE points to a page table (always memory resident) Level 2 table: each PTE points to a page (paged in and out like any other data) Level 1 Table ... Level 2 Tables

A Two-Level Page Table Hierarchy page tables Virtual memory VP 0 ... PTE 0 PTE 0 VP 1023 2K allocated VM pages for code and data ... PTE 1 VP 1024 PTE 1023 PTE 2 (null) ... PTE 3 (null) VP 2047 PTE 4 (null) PTE 0 Gap PTE 5 (null) ... PTE 6 (null) PTE 1023 6K unallocated VM pages PTE 7 (null) PTE 8 1023 null PTEs (1K - 9) null PTEs PTE 1023 1023 unallocated pages 1023 unallocated pages VP 9215 1 allocated VM page for the stack 32 bit addresses, 4KB pages, 4-byte PTEs ...