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

Similar documents
Virtual Memory: Concepts

Virtual Memory: Concepts

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

University of Washington Virtual memory (VM)

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

Computer Systems. Virtual Memory. Han, Hwansoo

Systems Programming and Computer Architecture ( ) Timothy Roscoe

CSE 153 Design of Operating Systems

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

Processes and Virtual Memory Concepts

Virtual Memory: Concepts

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

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

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

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

Lecture 19: Virtual Memory: Concepts

Understanding the Design of Virtual Memory. Zhiqiang Lin

VIRTUAL MEMORY: CONCEPTS

VM as a cache for disk

Virtual Memory II. CSE 351 Autumn Instructor: Justin Hsia

Virtual Memory II. CSE 351 Autumn Instructor: Justin Hsia

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

Virtual Memory II CSE 351 Spring

Virtual Memory. Spring Instructors: Aykut and Erkut Erdem

Virtual Memory I. CSE 351 Spring Instructor: Ruth Anderson

Foundations of Computer Systems

Virtual Memory Oct. 29, 2002

CSE 351. Virtual Memory

Virtual Memory: Systems

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

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

CISC 360. Virtual Memory Dec. 4, 2008

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

Virtual Memory Nov 9, 2009"

University*of*Washington*

Virtual Memory. Samira Khan Apr 27, 2017

Virtual Memory. Computer Systems Principles

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

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

Virtual Memory I. CSE 351 Winter Instructor: Mark Wyse

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

Virtual Memory: Systems

virtual memory Page 1 CSE 361S Disk Disk

CS 153 Design of Operating Systems

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 24

CS 153 Design of Operating Systems

Problem: Processor- Memory Bo<leneck

Virtual Memory III /18-243: Introduc3on to Computer Systems 17 th Lecture, 23 March Instructors: Bill Nace and Gregory Kesden

Chapter 8 Virtual Memory

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

Opera&ng Systems ECE344

P6/Linux Memory System Nov 11, 2009"

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

Memory System Case Studies Oct. 13, 2008

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 23

18-447: Computer Architecture Lecture 16: Virtual Memory

Pentium/Linux Memory System March 17, 2005

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 23

Lecture 8: Memory Management

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

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

CSE 560 Computer Systems Architecture

Main Points. Address Transla+on Concept. Flexible Address Transla+on. Efficient Address Transla+on

CS 261 Fall Mike Lam, Professor. Virtual Memory

CS 152 Computer Architecture and Engineering CS252 Graduate Computer Architecture. Lecture 9 Virtual Memory

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

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

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

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

COSC3330 Computer Architecture Lecture 20. Virtual Memory

CSE 120 Principles of Operating Systems

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

Memory Management! Goals of this Lecture!

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

Cache Performance and Memory Management: From Absolute Addresses to Demand Paging. Cache Performance

Virtual Memory Virtual memory first used to relive programmers from the burden of managing overlays.

Virtual Memory: From Address Translation to Demand Paging

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

Virtual Memory III. CSE 351 Autumn Instructor: Justin Hsia

Memory Hierarchy. Mehran Rezaei

Virtual Memory. Stefanos Kaxiras. Credits: Some material and/or diagrams adapted from Hennessy & Patterson, Hill, online sources.

Chapter 5B. Large and Fast: Exploiting Memory Hierarchy

CS 61C: Great Ideas in Computer Architecture. Virtual Memory III. Instructor: Dan Garcia

Chapter 8. Virtual Memory

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

CS 152 Computer Architecture and Engineering CS252 Graduate Computer Architecture. Lecture 8 Address Transla>on

CS 153 Design of Operating Systems

Memory: Page Table Structure. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

Problem: Processor Memory BoJleneck

EN1640: Design of Computing Systems Topic 06: Memory System

CSE 120 Principles of Operating Systems Spring 2017

Virtual Memory. CS 351: Systems Programming Michael Saelee

Virtual Memory. CS 3410 Computer System Organization & Programming

Memory Hierarchy Requirements. Three Advantages of Virtual Memory

Chapter 5. Large and Fast: Exploiting Memory Hierarchy

Virtual Memory I. CSE 351 Autumn Instructor: Justin Hsia

CS 153 Design of Operating Systems Winter 2016

CS 318 Principles of Operating Systems

Chapter 5. Large and Fast: Exploiting Memory Hierarchy

Lecture 15: Caches and Optimization Computer Architecture and Systems Programming ( )

Transcription:

Virtual Memory Instructor: Sanjeev Se(a This lecture (VM) Overview and mo(va(on VM as tool for caching VM as tool for memory management VM as tool for memory protec(on Address transla(on 2

Virtual Memory (Previous Lectures) 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 par(cular process Alloca@on: Compiler and run- @me system Where different program objects should be stored All alloca(on within single virtual address space But why virtual memory? Why not physical memory? FF F 3 Problem : How Does Everything Fit? 64- bit addresses: 6 Exabyte Physical main memory: Few Gigabytes? And there are many processes. 4 2

Problem 2: Memory Management Physical main memory Process Process 2 Process 3 Process n x stack heap.text.data What goes where? 5 Problem 3: How To Protect Process i Physical main memory Process j Problem 4: How To Share? Process i Physical main memory Process j 6 3

Solu@on: Level Of Indirec@on Process Physical memory mapping Process n Each process gets its own private memory space Solves the previous problems 7 Address Spaces Linear address space: Ordered set of con(guous non- nega(ve 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- } Clean dis@nc@on between data (bytes) and their a[ributes (addresses) Each object can now have mul@ple addresses Every byte in main memory: one physical address, one (or more) virtual addresses 8 4

A System Using Physical Addressing CPU Physical address (PA) 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 9 A System Using Virtual Addressing CPU Chip CPU Virtual address (VA) MMU Physical address (PA) Main memory : : 2: 3: 4: 5: 6: 7: 8:... M- : Data word Used in all modern desktops, laptops, worksta@ons One of the great ideas in computer science MMU checks the cache 5

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 ac(ve 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 informa(on different sec(ons of address spaces have different permissions This lecture (VM) Overview and mo(va(on VM as tool for caching VM as tool for memory management VM as tool for memory protec(on Address transla(on 2 6

VM as a Tool for Caching : array of N = 2 n con@guous bytes think of the array (allocated part) as being stored on disk Physical main memory = cache for allocated virtual memory Blocks are called pages; size = 2 p Physical memory Disk VP VP Unallocated Cached Uncached Unallocated Cached Uncached Cached n- p - Uncached 2 n - 2 m - Empty Empty Empty PP PP PP 2 m- p - Virtual pages (VP's) stored on disk Physical pages (PP's) cached in DRAM 3 Memory Hierarchy: Core 2 Duo L/L2 cache: 64 B blocks Not drawn to scale CPU Reg Throughput: Latency: L I- cache 32 KB L D- cache ~4 MB L2 unified cache ~4 GB ~5 GB Main Memory 6 B/cycle 8 B/cycle 2 B/cycle B/3 cycles 3 cycles 4 cycles cycles millions Disk Miss penalty (latency): 3x Miss penalty (latency):,x 4 7

DRAM Cache Organiza@on DRAM cache organiza@on driven by the enormous miss penalty DRAM is about x slower than SRAM Disk is about,x slower than DRAM For first byte, faster for next byte Consequences Large page (block) size: typically 4-8 KB, some(mes 4 MB Fully associa(ve Any VP can be placed in any PP Requires a large mapping func(on different from CPU caches Highly sophis(cated, expensive replacement algorithms Too complicated and open- ended to be implemented in hardware Write- back rather than write- through 5 Address Transla@on: 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 Physical page number or Valid disk address PTE null null PTE 7 Memory resident page table Physical memory VP (disk) VP VP 6 PP PP 3 6 8

Address Transla@on With a Page Table Page table base register (PTBR) Virtual address Virtual page number (VPN) Virtual page offset (VPO) Page table address for process Page table Valid Physical page number (PPN) Valid bit = : page not in memory (page fault) Physical page number (PPN) Physical address Physical page offset (PPO) 7 Page Hit Page hit: reference to VM word that is in physical memory Virtual address Physical page number or Valid disk address PTE null null PTE 7 Memory resident page table Physical memory VP (disk) VP VP 6 PP PP 3 8 9

Page Miss Page miss: reference to VM word that is not in physical memory Virtual address Physical page number or Valid disk address PTE null null PTE 7 Memory resident page table Physical memory VP (disk) VP VP 6 PP PP 3 9 Handling Page Fault Page miss causes page fault (an excep(on) Virtual address Physical page number or Valid disk address PTE null null PTE 7 Memory resident page table Physical memory VP (disk) VP VP 6 PP PP 3 2

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

Handling Page Fault Page miss causes page fault (an excep(on) Page fault handler selects a vic(m to be evicted (here ) Offending instruc(on is restarted: page hit! Virtual address Physical page number or Valid disk address PTE null null PTE 7 Memory resident page table Physical memory VP (disk) VP VP 6 PP PP 3 23 Why does it work? Locality works because of locality At any point in @me, programs tend to access a set of ac@ve virtual pages called the working set Programs with befer temporal locality will have smaller working sets If (working set size < main memory size) Good performance for one process aher compulsory misses If ( SUM(working set sizes) > main memory size ) Thrashing: Performance meltdown where pages are swapped (copied) in and out con(nuously 24 2

This lecture (VM) Overview and mo(va(on VM as tool for caching VM as tool for memory management VM as tool for memory protec(on Address transla(on 25 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 func(on scafers addresses through physical memory Well chosen mappings simplify memory alloca(on and management Virtual Address Space for Process : VP... Address translanon PP 2 Physical Address Space Virtual Address Space for Process 2: N- N- VP... M- PP 6 PP 8... (e.g., read- only library code) 26 3

VM as a Tool for Memory Management Memory alloca@on Each virtual page can be mapped to any physical page A virtual page can be stored in different physical pages at different (mes Sharing code and data among processes Map virtual pages to the same physical page (here: PP 6) Virtual Address Space for Process : VP... Address translanon PP 2 Physical Address Space Virtual Address Space for Process 2: N- N- VP... M- PP 6 PP 8... (e.g., read- only library code) 27 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 sec(ons = creates PTEs marked as invalid The.text and.data sec(ons are copied, page by page, on demand by the virtual memory system xc x4 x848 Kernel virtual memory User stack (created at run@me) Memory- mapped region for shared libraries Run- @me heap (created by malloc) Read/write segment (.data,.bss) Read- only segment (.init,.text,.rodata) Unused Memory invisible to user code %esp (stack pointer) brk Loaded from the executable file 28 4

This lecture (VM) Overview and mo(va(on VM as tool for caching VM as tool for memory management VM as tool for memory protec@on Address transla(on 29 VM as a Tool for Memory Protec@on Extend PTEs with permission bits Page fault handler checks these before remapping If violated, send process SIGSEGV (segmenta(on fault) Process i: VP : SUP No READ WRITE Yes No Address PP 6 Physical Address Space VP : : No Yes Yes Yes Yes Yes PP 4 PP 2 PP 2 PP 4 PP 6 Process j: VP : VP : : SUP No Yes No READ WRITE Yes Yes Yes No Yes Yes Address PP 9 PP 6 PP PP 8 PP 9 PP 3 5

This lecture (VM) Overview and mo(va(on VM as tool for caching VM as tool for memory management VM as tool for memory protec(on Address transla@on 3 Address Transla@on: 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 32 6

Address Transla@on: Page Fault Excep@on Page fault handler 4 CPU Chip CPU VA 7 MMU 2 PTEA PTE 3 Cache/ Memory Vic@m 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 excep(on 5) Handler iden(fies vic(m (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, restar(ng faul(ng instruc(on 33 Handling Page Fault Page miss causes page fault (an excep(on) Page fault handler selects a vic(m to be evicted (here ) Virtual address Physical page number or Valid disk address PTE null null PTE 7 Memory resident page table Physical memory VP (disk) VP PP PP 3 VP 6 34 7

Handling Page Fault Page miss causes page fault (an excep(on) Page fault handler selects a vic(m to be evicted (here ) Virtual address Physical page number or Valid disk address PTE null null PTE 7 Memory resident page table Physical memory VP (disk) VP VP 6 PP PP 3 35 Handling Page Fault Page miss causes page fault (an excep(on) Page fault handler selects a vic(m to be evicted (here ) Offending instruc(on is restarted: page hit! Virtual address Physical page number or Valid disk address PTE null null PTE 7 Memory resident page table Physical memory VP (disk) VP VP 6 PP PP 3 36 8

Speeding up Transla@on 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 s(ll requires a - cycle delay Solu@on: TranslaNon Lookaside Buffer (TLB) Small hardware cache in MMU Maps virtual page numbers to physical page numbers Contains complete page table entries for small number of pages 37 TLB Hit CPU Chip TLB 2 PTE VPN 3 CPU VA MMU PA 4 Cache/ Memory Data 5 A TLB hit eliminates a memory access 38 9

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 add l memory access (the PTE) Fortunately, TLB misses are rare 39 Simple Memory System Example Addressing 4- bit virtual addresses 2- bit physical address Page size = 64 bytes 3 2 9 8 7 6 5 4 3 2 VPN Virtual Page Number VPO Virtual Page Offset 9 8 7 6 5 4 3 2 PPN Physical Page Number PPO Physical Page Offset 4 2

Simple Memory System Page Table Only show first 6 entries (out of 256) VPN 2 3 4 5 6 7 PPN 28 33 2 6 Valid VPN 8 9 A B C D E F PPN 3 7 9 2D D Valid 4 Simple Memory System TLB 6 entries 4- way associa@ve TLBT TLBI 3 2 9 8 7 6 5 4 3 2 VPN VPO Set Tag PPN Valid Tag PPN Valid Tag PPN Valid Tag PPN Valid 3 9 D 7 2 3 2D 2 4 A 2 2 8 6 3 3 7 3 D A 34 2 42 2

Simple Memory System Cache 6 lines, 4- byte block size Physically addressed Direct mapped CT CI CO 9 8 7 6 5 4 3 2 PPN PPO Idx 2 3 4 Tag 9 5 B 36 32 Valid B 99 43 B 2 6D B2 23 4 8F B3 8 9 Idx 8 9 A B C Tag 24 2D 2D B 2 Valid B 3A 93 B 5 B2 5 DA B3 89 3B 5 6 7 D 3 6 36 72 C2 F DF D 3 D E F 6 3 4 4 83 96 77 34 B 5 D3 43 Address Transla@on Example # Virtual Address: x3d4 TLBT TLBI 3 2 9 8 7 6 5 4 3 2 VPN VPO VPN xf TLBI 3 TLBT x3 TLB Hit? Y Page Fault? N PPN: xd Physical Address CT CI CO 9 8 7 6 5 4 3 2 PPN PPO CO CI x5 CT xd Hit? Y Byte: x36 44 22

Address Transla@on Example #2 Virtual Address: xb8f TLBT TLBI 3 2 9 8 7 6 5 4 3 2 VPN VPO VPN x2e TLBI 2 TLBT xb TLB Hit? N Page Fault? Y PPN: TBD Physical Address CT CI CO 9 8 7 6 5 4 3 2 PPN PPO CO CI CT Hit? Byte: 45 Address Transla@on Example #3 Virtual Address: x2 TLBT TLBI 3 2 9 8 7 6 5 4 3 2 VPN VPO VPN x TLBI TLBT x TLB Hit? N Page Fault? N PPN: x28 Physical Address CT CI CO 9 8 7 6 5 4 3 2 PPN PPO CO CI x8 CT x28 Hit? N Byte: Mem 46 23

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 protec(on by providing a convenient interposi(oning point to check permissions 47 24