Virtual Memory II. CSE 351 Autumn Instructor: Justin Hsia

Similar documents
Virtual Memory II. CSE 351 Autumn Instructor: Justin Hsia

Virtual Memory II CSE 351 Spring

Computer Systems. Virtual Memory. Han, Hwansoo

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

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

CSE 153 Design of Operating Systems

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

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

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

Processes and Virtual Memory Concepts

Virtual Memory: Concepts

Systems Programming and Computer Architecture ( ) Timothy Roscoe

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

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

CSE 351. Virtual Memory

Virtual Memory: Concepts

Lecture 19: Virtual Memory: Concepts

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

Virtual Memory: Concepts

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

University of Washington Virtual memory (VM)

Foundations of Computer Systems

Virtual Memory Wrap Up

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

Virtual Memory Oct. 29, 2002

Virtual Memory. Samira Khan Apr 27, 2017

Virtual Memory Nov 9, 2009"

VM as a cache for disk

Understanding the Design of Virtual Memory. Zhiqiang Lin

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

Virtual Memory: Systems

CISC 360. Virtual Memory Dec. 4, 2008

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 24

Virtual Memory. Computer Systems Principles

Virtual Memory III. CSE 351 Autumn Instructor: Justin Hsia

CS 153 Design of Operating Systems

P6/Linux Memory System Nov 11, 2009"

Memory System Case Studies Oct. 13, 2008

Pentium/Linux Memory System March 17, 2005

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: Systems

Caches II. CSE 351 Autumn Instructor: Justin Hsia

virtual memory Page 1 CSE 361S Disk Disk

Virtual Memory III. CSE 351 Autumn Instructor: Justin Hsia

Caches III. CSE 351 Autumn Instructor: Justin Hsia

University*of*Washington*

Memory, Data, & Addressing I

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

Virtual Memory I. CSE 351 Spring Instructor: Ruth Anderson

Virtual Memory I. CSE 351 Winter Instructor: Mark Wyse

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

Lecture 21: Virtual Memory. Spring 2018 Jason Tang

Address Translation. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Administrivia. Caches III. Making memory accesses fast! Associativity. Cache Organization (3) Example Placement

VIRTUAL MEMORY II. Jo, Heeseung

CS 153 Design of Operating Systems

Virtual Memory. CS 3410 Computer System Organization & Programming

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

Virtual Memory: From Address Translation to Demand Paging

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

CS 318 Principles of Operating Systems

CSE 120 Principles of Operating Systems

John Wawrzynek & Nick Weaver

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

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

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

Intel P6 (Bob Colwell s Chip, CMU Alumni) The course that gives CMU its Zip! Memory System Case Studies November 7, 2007.

CSE 120 Principles of Operating Systems Spring 2017

Memory Hierarchy Requirements. Three Advantages of Virtual Memory

18-447: Computer Architecture Lecture 16: Virtual Memory

Intel P The course that gives CMU its Zip! P6/Linux Memory System November 1, P6 memory system. Review of abbreviations

Virtual Memory. Spring Instructors: Aykut and Erkut Erdem

Problem 9. VM address translation. (9 points): The following problem concerns the way virtual addresses are translated into physical addresses.

CSE 560 Computer Systems Architecture

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

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.

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

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

Virtual Memory. Virtual Memory

Virtual Memory: From Address Translation to Demand Paging

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

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 23

Virtual Memory. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Computer Architecture Lecture 13: Virtual Memory II

Translation Buffers (TLB s)

Recap: Memory Management

P6 memory system P6/Linux Memory System October 31, Overview of P6 address translation. Review of abbreviations. Topics. Symbols: ...

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 23

Question F5: Caching [10 pts]

CS 153 Design of Operating Systems Winter 2016

Lecture 9 - Virtual Memory

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

Virtual Memory Review. Page faults. Paging system summary (so far)

Page Which had internal designation P5

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

Transcription:

Virtual Memory II CSE 35 Autumn 27 Instructor: Justin Hsia Teaching Assistants: Lucas Wotton Michael Zhang Parker DeWilde Ryan Wong Sam Gehman Sam Wolfson Savanna Yee Vinny Palaniappan https://xkcd.com/495/

Administrivia Lab 4 due next Monday (/27) Homework 5 released tomorrow (/2) Processes and Virtual Memory There is lecture on Wednesday Practice and review problems, plus fun slides on a recent VM exploit Will be uploaded to Panopto, as usual Virtual Section on Virtual Memory Worksheet and solutions released on Wednesday Videos of Justin working through problems 2

Virtual Memory (VM) Overview and motivation VM as a tool for caching Address translation VM as a tool for memory management VM as a tool for memory protection 3

Review: Terminology Context switch Switch between processes on the same CPU Page in Move pages of virtual memory from disk to physical memory Page out Move pages of virtual memory from physical memory to disk Thrashing Total working set size of processes is larger than physical memory and causes excessive paging in and out instead of doing useful computation 4

VM for Managing Multiple Processes Key abstraction: each process has its own virtual address space It can view memory as a simple linear array With virtual memory, this simple linear virtual address space need not be contiguous in physical memory Process needs to store data in another VP? Just map it to any PP! Virtual Address Space for Process : N VP VP 2... Address translation PP 2 PP 6 Physical Address Space (DRAM) (e.g., read only library code) Virtual Address Space for Process 2: N VP VP 2... M PP 8... 5

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

VM for Protection and Sharing The mapping of VPs to PPs provides a simple mechanism to protect memory and to share memory between processes Sharing: map virtual pages in separate address spaces to the same physical page (here: PP 6) Protection: process can t access physical pages to which none of its virtual pages are mapped (here: Process 2 can t access PP 2) Virtual Address Space for Process : Virtual Address Space for Process 2: N N VP VP 2... VP VP 2... Address translation M PP 2 PP 6 PP 8... Physical Address Space (DRAM) (e.g., read only library code) 7

Memory Protection Within Process VM implements read/write/execute permissions Extend page table entries with permission bits MMU checks these permission bits on every memory access Process i: If violated, raises exception and OS sends SIGSEGV signal to process (segmentation fault) VP : VP : VP 2: Valid READ WRITE No No EXEC No No Process j: Valid READ WRITE EXEC VP : VP : VP 2: No No No No PPN PP 6 PP 4 PP 2 PPN PP 9 PP 6 PP Physical Address Space PP 2 PP 4 PP 6 PP 8 PP 9 PP 8

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 (memory management unit) 2 3) MMU fetches PTE from page table in cache/memory (Uses PTBR to find beginning of page table for current process) 4) MMU sends physical address to cache/memory requesting data 5) Cache/memory sends data to processor VA = Virtual Address PTEA = Page Table Entry Address PTE= Page Table Entry PA = Physical Address Data = Contents of memory stored at VA originally requested by CPU 9

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 cache/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

Hmm Translation Sounds Slow The MMU accesses memory twice: once to get the PTE for translation, and then again for the actual memory request The PTEs may be cached in L like any other memory word But they may be evicted by other data references And a hit in the L cache still requires 3 cycles What can we do to make this faster? Solution: add another cache!

Speeding up Translation with a TLB Translation 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 Modern Intel processors have 28 or 256 entries in TLB Much faster than a page table lookup in cache/memory TLB PTE PTE PTE 2

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

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

Fetching Data on a Memory Read ) Check TLB Input:, Output: PPN TLB Hit: Fetch translation, return PPN TLB Miss: Check page table (in memory) Page Table Hit: Load page table entry into TLB Page Fault: Fetch page from disk to memory, update corresponding page table entry, then load entry into TLB 2) Check cache Input: physical address, Output: data Cache Hit: Return data value to processor Cache Miss: Fetch data value from memory, store it in cache, return it to processor 5

Address Translation Virtual Address TLB Miss TLB Lookup TLB Hit Page not in Mem Page Fault (OS loads page) Page Table Walk Page in Mem Update TLB Access Denied Protection Fault Protection Check Access Permitted Physical Address Find in Disk Find in Mem SIGSEGV Check cache 6

Context Switching Revisited What needs to happen when the CPU switches processes? Registers: Save state of old process, load state of new process Including the Page Table Base Register (PTBR) Memory: Nothing to do! Pages for processes already exist in memory/disk and protected from each other TLB: Invalidate all entries in TLB mapping is for old process VAs Cache: Can leave alone because storing based on PAs good for shared data 7

Summary of Address Translation Symbols Basic Parameters Number of addresses in virtual address space Number of addresses in physical address space Page size (bytes) Components of the virtual address (VA) VPO TLBI TLBT Virtual page offset Virtual page number TLB index TLB tag Components of the physical address (PA) PPO PPN Physical page offset (same as VPO) Physical page number 8

Simple Memory System Example (small) Addressing 4 bit virtual addresses 2 bit physical address Page size = 64 bytes 3 2 9 8 7 6 5 4 3 2 Virtual Page Number VPO Virtual Page Offset 9 8 7 6 5 4 3 2 PPN Physical Page Number PPO Physical Page Offset 9

Simple Memory System: Page Table Only showing first 6 entries (out of ) Note: showing 2 hex digits for PPN even though only 6 bits Note: other management bits not shown, but part of PTE PPN Valid 28 2 33 3 2 4 5 6 6 7 PPN Valid 8 3 9 7 A 9 B C D 2D E F D 2

Simple Memory System: TLB 6 entries total 4 way set associative Why does the TLB ignore the page offset? TLB tag TLB index 3 2 9 8 7 6 5 4 3 2 virtual page number virtual page offset 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 2

Simple Memory System: Cache Direct mapped with = 4 B, = 6 Physically addressed Note: It is just coincidence that the PPN is the same width as the cache Tag cache tag cache index cache offset 9 8 7 6 5 4 3 2 physical page number physical page offset Index Tag Valid B B B2 B3 9 99 23 5 2 B 2 4 8 3 36 4 32 43 6D 8F 9 5 D 36 72 F D 6 3 7 6 C2 DF 3 Index Tag Valid B B B2 B3 8 24 3A 5 89 9 2D A 2D 93 5 DA 3B B B C 2 D 6 4 96 34 5 E 3 83 77 B D3 F 4 22

Current State of Memory System TLB: Set Tag PPN V Tag PPN V Tag PPN V Tag PPN V 3 9 D 7 2 3 2D 2 4 A 2 2 8 6 3 3 7 3 D A 34 2 Cache: Index Tag V B B B2 B3 9 99 23 5 2 B 2 4 8 3 36 4 32 43 6D 8F 9 5 D 36 72 F D 6 3 7 6 C2 DF 3 Page table (partial): PPN V 28 2 33 3 2 4 5 6 6 7 PPN V 8 3 9 7 A 9 B C D 2D E F D Index Tag V B B B2 B3 8 24 3A 5 89 9 2D A 2D 93 5 DA 3B B B C 2 D 6 4 96 34 5 E 3 83 77 B D3 F 4 23

Memory Request Example # Virtual Address: x3d4 TLBT TLBI 3 2 9 8 7 6 5 4 3 2 VPO Note: It is just coincidence that the PPN is the same width as the cache Tag TLBT TLBI TLB Hit? Page Fault? PPN Physical Address: CT CI CO 9 8 7 6 5 4 3 2 PPN PPO CT CI CO Cache Hit? Data (byte) 24

Memory Request Example #2 Virtual Address: x38f TLBT TLBI 3 2 9 8 7 6 5 4 3 2 VPO Note: It is just coincidence that the PPN is the same width as the cache Tag TLBT TLBI TLB Hit? Page Fault? PPN Physical Address: CT CI CO 9 8 7 6 5 4 3 2 PPN PPO CT CI CO Cache Hit? Data (byte) 25

Memory Request Example #3 Virtual Address: x2 TLBT TLBI 3 2 9 8 7 6 5 4 3 2 VPO Note: It is just coincidence that the PPN is the same width as the cache Tag TLBT TLBI TLB Hit? Page Fault? PPN Physical Address: CT CI CO 9 8 7 6 5 4 3 2 PPN PPO CT CI CO Cache Hit? Data (byte) 26

Memory Request Example #4 Virtual Address: x36b TLBT TLBI 3 2 9 8 7 6 5 4 3 2 VPO Note: It is just coincidence that the PPN is the same width as the cache Tag TLBT TLBI TLB Hit? Page Fault? PPN Physical Address: CT CI CO 9 8 7 6 5 4 3 2 PPN PPO CT CI CO Cache Hit? Data (byte) 27

Virtual Memory 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 sharing Simplifies protection by providing permissions checking 28

Memory System Summary Memory Caches (L/L2/L3) Purely a speed up technique Behavior invisible to application programmer and (mostly) OS Implemented totally in hardware Virtual Memory Supports many OS related functions Process creation, task switching, protection Operating System (software) Allocates/shares physical memory among processes Maintains high level tables tracking memory type, source, sharing Handles exceptions, fills in hardware defined mapping tables Hardware Translates virtual addresses via mapping tables, enforcing permissions Accelerates mapping via translation cache (TLB) 29