Virtual Memory: Concepts

Similar documents
Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Computer Systems. Virtual Memory. Han, Hwansoo

Processes and Virtual Memory Concepts

CSE 153 Design of Operating Systems

14 May 2012 Virtual Memory. Definition: A process is an instance of a running program

Carnegie Mellon. 16 th Lecture, Mar. 20, Instructors: Todd C. Mowry & Anthony Rowe

Virtual Memory: Concepts

VM as a cache for disk

This lecture. Virtual Memory. Virtual memory (VM) CS Instructor: Sanjeev Se(a

Virtual Memory: Concepts

198:231 Intro to Computer Organization. 198:231 Introduction to Computer Organization Lecture 14

Systems Programming and Computer Architecture ( ) Timothy Roscoe

Understanding the Design of Virtual Memory. Zhiqiang Lin

Lecture 19: Virtual Memory: Concepts

Virtual Memory. CS61, Lecture 15. Prof. Stephen Chong October 20, 2011

VIRTUAL MEMORY: CONCEPTS

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Virtual Memory II CSE 351 Spring

Virtual Memory II. CSE 351 Autumn Instructor: Justin Hsia

Virtual Memory. Alan L. Cox Some slides adapted from CMU slides

Foundations of Computer Systems

Virtual Memory II. CSE 351 Autumn Instructor: Justin Hsia

University of Washington Virtual memory (VM)

CSE 351. Virtual Memory

Virtual Memory. Physical Addressing. Problem 2: Capacity. Problem 1: Memory Management 11/20/15

Virtual Memory. Computer Systems Principles

Virtual Memory. Samira Khan Apr 27, 2017

Virtual Memory I. CSE 351 Spring Instructor: Ruth Anderson

Virtual Memory. Motivations for VM Address translation Accelerating translation with TLBs

Virtual Memory Oct. 29, 2002

A Few Problems with Physical Addressing. Virtual Memory Process Abstraction, Part 2: Private Address Space

virtual memory. March 23, Levels in Memory Hierarchy. DRAM vs. SRAM as a Cache. Page 1. Motivation #1: DRAM a Cache for Disk

Virtual Memory Nov 9, 2009"

Motivations for Virtual Memory Virtual Memory Oct. 29, Why VM Works? Motivation #1: DRAM a Cache for Disk

Virtual Memory: Systems

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 24

CISC 360. Virtual Memory Dec. 4, 2008

Random-Access Memory (RAM) Systemprogrammering 2007 Föreläsning 4 Virtual Memory. Locality. The CPU-Memory Gap. Topics

Random-Access Memory (RAM) Systemprogrammering 2009 Föreläsning 4 Virtual Memory. Locality. The CPU-Memory Gap. Topics! The memory hierarchy

virtual memory Page 1 CSE 361S Disk Disk

CS 153 Design of Operating Systems

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 23

Virtual Memory I. CSE 351 Winter Instructor: Mark Wyse

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 23

@2010 Badri Computer Architecture Assembly II. Virtual Memory. Topics (Chapter 9) Motivations for VM Address translation

CS 261 Fall Mike Lam, Professor. Virtual Memory

Virtual Memory: Systems

Virtual Memory. Daniel Sanchez Computer Science & Artificial Intelligence Lab M.I.T. November 15, MIT Fall 2018 L20-1

18-447: Computer Architecture Lecture 16: Virtual Memory

P6/Linux Memory System Nov 11, 2009"

CS 61C: Great Ideas in Computer Architecture. Lecture 23: Virtual Memory. Bernhard Boser & Randy Katz

CS 61C: Great Ideas in Computer Architecture. Lecture 23: Virtual Memory

Virtual Memory. Daniel Sanchez Computer Science & Artificial Intelligence Lab M.I.T. April 12, 2018 L16-1

University*of*Washington*

Virtual Memory: From Address Translation to Demand Paging

Memory Management! How the hardware and OS give application pgms:" The illusion of a large contiguous address space" Protection against each other"

Memory Management! Goals of this Lecture!

Virtual Memory. CS 3410 Computer System Organization & Programming

CSE 560 Computer Systems Architecture

Address Translation. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Memory Management. Goals of this Lecture. Motivation for Memory Hierarchy

Pentium/Linux Memory System March 17, 2005

CSE 120 Principles of Operating Systems

Memory System Case Studies Oct. 13, 2008

Virtual Memory. Patterson & Hennessey Chapter 5 ELEC 5200/6200 1

Another View of the Memory Hierarchy. Lecture #25 Virtual Memory I Memory Hierarchy Requirements. Memory Hierarchy Requirements

Agenda. CS 61C: Great Ideas in Computer Architecture. Virtual Memory II. Goals of Virtual Memory. Memory Hierarchy Requirements

Memory Hierarchy Requirements. Three Advantages of Virtual Memory

Virtual Memory. Spring Instructors: Aykut and Erkut Erdem

John Wawrzynek & Nick Weaver

Recall: Address Space Map. 13: Memory Management. Let s be reasonable. Processes Address Space. Send it to disk. Freeing up System Memory

CS 153 Design of Operating Systems

VIRTUAL MEMORY II. Jo, Heeseung

Lecture 21: Virtual Memory. Spring 2018 Jason Tang

Virtual Memory: From Address Translation to Demand Paging

Virtual Memory. CS 3410 Computer System Organization & Programming. [K. Bala, A. Bracy, E. Sirer, and H. Weatherspoon]

Computer Science 146. Computer Architecture

CS 153 Design of Operating Systems Winter 2016

CSE 120 Principles of Operating Systems Spring 2017

Chapter 8. Virtual Memory

Memory Management Topics. CS 537 Lecture 11 Memory. Virtualizing Resources

CS252 S05. Main memory management. Memory hardware. The scale of things. Memory hardware (cont.) Bottleneck

Chapter 5B. Large and Fast: Exploiting Memory Hierarchy

Handout 4 Memory Hierarchy

This Unit: Main Memory. Virtual Memory. Virtual Memory. Other Uses of Virtual Memory

Learning Outcomes. An understanding of page-based virtual memory in depth. Including the R3000 s support for virtual memory.

Learning Outcomes. An understanding of page-based virtual memory in depth. Including the R3000 s support for virtual memory.

Memory Hierarchy. Mehran Rezaei

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

CS162 Operating Systems and Systems Programming Lecture 14. Caching (Finished), Demand Paging

CS 5523 Operating Systems: Memory Management (SGG-8)

The Virtual Memory Abstraction. Memory Management. Address spaces: Physical and Virtual. Address Translation

PROCESS VIRTUAL MEMORY PART 2. CS124 Operating Systems Winter , Lecture 19

CS 61C: Great Ideas in Computer Architecture. Virtual Memory

Memory Allocation. Copyright : University of Illinois CS 241 Staff 1

CS 318 Principles of Operating Systems

Computer Architecture and Engineering. CS152 Quiz #3. March 18th, Professor Krste Asanovic. Name:

The memory of a program. Paging and Virtual Memory. Swapping. Paging. Physical to logical address mapping. Memory management: Review

CS162 Operating Systems and Systems Programming Lecture 10 Caches and TLBs"

Multi-level Translation. CS 537 Lecture 9 Paging. Example two-level page table. Multi-level Translation Analysis

Transcription:

Virtual Memory: Concepts 5-23: Introduction to Computer Systems 7 th Lecture, March 2, 27 Instructors: Franz Franchetti & Seth Copen Goldstein

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

Today Address spaces VM as a tool for caching VM as a tool for memory management VM as a tool for memory protection Address translation 3

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

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

Address Spaces Linear address space: Ordered set of contiguous non-negative integer addresses: {,, 2, 3 } Virtual address space: Set of N = 2 n virtual addresses {,, 2, 3,, N-} Physical address space: Set of M = 2 m physical addresses {,, 2, 3,, M-} 6

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 7

Today Address spaces VM as a tool for caching VM as a tool for memory management VM as a tool for memory protection Address translation 8

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 = 2 p bytes) Virtual memory Physical memory VP VP Unallocated Cached Uncached Unallocated Empty Empty PP PP Cached VP 2 n-p - Uncached Cached Uncached N- M- Empty PP 2 m-p - Virtual pages (VPs) stored on disk Physical pages (PPs) cached in DRAM 9

Remember: Set Associative Cache E = 2: Two lines per set Assume: cache block size 8 bytes 2 lines per set Address : Block offset t bits v tag 2 3 4 5 6 7 v tag 2 3 4 5 6 7 v tag 2 3 4 5 6 7 v tag 2 3 4 5 6 7 Index to find set v tag 2 3 4 5 6 7 v tag 2 3 4 5 6 7 v tag 2 3 4 5 6 7 v tag 2 3 4 5 6 7 S sets

DRAM Cache Organization DRAM cache organization driven by the enormous miss penalty DRAM is about x slower than SRAM Disk is about,x 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 Valid PTE PTE 7 Physical page number or disk address null null Memory resident page table (DRAM) Physical memory (DRAM) VP VP 2 VP 7 VP 4 Virtual memory (disk) VP VP 2 VP 3 VP 4 VP 6 VP 7 PP PP 3 2

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

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

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

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

Handling Page Fault Page miss causes page fault (an exception) Page fault handler selects a victim to be evicted (here VP 4) Virtual address Valid PTE PTE 7 Physical page number or disk address null null Memory resident page table (DRAM) Physical memory (DRAM) VP VP 2 VP 7 VP 3 Virtual memory (disk) VP VP 2 VP 3 VP 4 PP PP 3 VP 6 VP 7 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! Virtual address Valid PTE PTE 7 Physical page number or disk address null null Memory resident page table (DRAM) Key point: Waiting until the miss to copy the page to DRAM is known as demand paging Physical memory (DRAM) VP VP 2 VP 7 VP 3 Virtual memory (disk) VP VP 2 VP 3 VP 4 VP 6 VP 7 PP PP 3 8

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

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 2

Today Address spaces VM as a tool for caching VM as a tool for memory management VM as a tool for memory protection Address translation 2

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

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

Simplifying Linking and Loading Linking Each program has similar virtual address space Code, data, and heap always start at the same addresses. Kernel virtual memory User stack (created at runtime) Memory-mapped region for shared libraries Memory invisible to user code %rsp (stack pointer) 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 x4 Run-time heap (created by malloc) Read/write segment (.data,.bss) Read-only segment (.init,.text,.rodata) Unused brk Loaded from the executable file 24

Today Address spaces VM as a tool for caching VM as a tool for memory management VM as a tool for memory protection Address translation 25

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

Today Address spaces VM as a tool for caching VM as a tool for memory management VM as a tool for memory protection Address translation 27

VM Address Translation Virtual Address Space V = {,,, N } Physical Address Space P = {,,, M } Address Translation MAP: V P U { } For virtual address a: MAP(a) = a if data at virtual address a is at physical address a in P MAP(a) = if data at virtual address a is not in physical memory Either invalid or stored on disk 28

Summary of Address Translation Symbols Basic Parameters N = 2 n : Number of addresses in virtual address space M = 2 m : Number of addresses in physical address space P = 2 p : Page size (bytes) Components of the virtual address (VA) TLBI: TLB index TLBT: TLB tag VPO: Virtual page offset VPN: Virtual page number Components of the physical address (PA) PPO: Physical page offset (same as VPO) PPN: Physical page number 29

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

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

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

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

Speeding up Translation with a TLB Page table entries (PTEs) are cached in L like any other memory word PTEs may be evicted by other data references PTE hit still requires a small L delay Solution: Translation Lookaside Buffer (TLB) Small set-associative hardware cache in MMU Maps virtual page numbers to physical page numbers Contains complete page table entries for small number of pages 34

Accessing the TLB MMU uses the VPN portion of the virtual address to access the TLB: TLBT matches tag of line within set n- TLB tag (TLBT) VPN p+t p+t- TLB index (TLBI) T = 2 t sets p p- VPO Set v tag PTE v tag PTE TLBI selects the set Set v tag PTE v tag PTE Set T- v tag PTE v tag PTE 35

TLB Hit CPU Chip TLB 2 VPN PTE 3 CPU VA MMU PA 4 Cache/ Memory Data 5 A TLB hit eliminates a memory access 36

TLB Miss CPU Chip TLB 4 2 PTE VPN 3 CPU VA MMU PTEA PA Cache/ Memory 5 Data 6 A TLB miss incurs an additional memory access (the PTE) Fortunately, TLB misses are rare. Why? 37

Multi-Level Page Tables Suppose: 4KB (2 2 ) page size, 48-bit address space, 8-byte PTE Level 2 Tables Problem: Would need a 52 GB page table! 2 48 * 2-2 * 2 3 = 2 39 bytes Level Table... Common solution: Multi-level page table Example: 2-level page table Level 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)... 38

We have a problem PTE PTE PTE 2 2 2 Entries of 4 bytes each PTE 3 PTE 4 PTE 5 PTE 6 PTE 7 PTE 8 PTE 8 Virtual memory VP... VP 23 VP 24... VP 247 2K allocated VM pages for code and data PTE 8 PTE 8 PTE 8 PTE 925 32 bit addresses, 4KB pages, 4-byte PTEs Gap 23 unallocated pages VP 925... 6K unallocated VM pages 23 unallocated pages allocated VM page for the stack 39

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

Translating with a k-level Page Table Page table base register (PTBR) n- VPN VIRTUAL ADDRESS VPN 2... VPN k p- VPO the Level page table a Level 2 page table...... a Level k page table PPN m- PPN p- PPO PHYSICAL ADDRESS 4

Summary Programmer s view of virtual memory Each process has its own private linear address space Cannot be corrupted by other processes System view of virtual memory Uses memory efficiently by caching virtual memory pages Efficient only because of locality Simplifies memory management and programming Simplifies protection by providing a convenient interpositioning point to check permissions 42

Academic Integrity Refresher Harry is having trouble with getopt on cachelab and googles for getopt to see how to use it. Jane is having trouble with fscanf on cachelab and talks to a friend about how to use it. Her friend shows her an example for reading two integers from a file. Sam is confused about how a cache works and goes to wikipedia to learn about caches. The he googles for cache. He reads several web pages and then starts to write his code. 43

Academic Integrity Refresher Joe has spent several hours staring at a blank sheet of paper and doesn t know how to get started. He googles for cachelab and finds a blog online which talks about how caches work. He reads it and then implements the code on his own. Mary, after several frustrating days, googles for caches and comes across a github page with the code for cachelab. She looks at the code. Then she closes her browser and implements the code on her own. 44

Academic Integrity Refresher Frank is stuck and talks to his friend about whether he should use multiple arrays or an array of structures. They don t look at code together. Andre is stuck and talks to a TA about that fields he should use to represent a cacheline. They are working together and writing things down on a piece of paper. Laura is stuck and talks to a friend about that fields she should use to represent a cacheline. They don t write anything down. 45