Virtual Memory. Study Chapters something I understand. Finally! A lecture on PAGE FAULTS! doing NAND gates. I wish we were still

Similar documents
Virtual Memory. Study Chapter Finally! A lecture on something I understand PAGE FAULTS! I wish we were still doing NAND gates

Virtual Memory Fall /19/0. L21 Virtual Memory 1

Virtual Memory. Study Chapters Recap: Memory Hierarchy of a Modern Computer System

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

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

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

Address Translation. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CS162 - Operating Systems and Systems Programming. Address Translation => Paging"

Virtual Memory: From Address Translation to Demand Paging

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

Address Translation. Tore Larsen Material developed by: Kai Li, Princeton University

CSE 120 Principles of Operating Systems

Virtual Memory. Virtual Memory

CS 153 Design of Operating Systems Winter 2016

Virtual Memory. Kevin Webb Swarthmore College March 8, 2018

CS5460: Operating Systems Lecture 14: Memory Management (Chapter 8)

VIRTUAL MEMORY II. Jo, Heeseung

CSE 120 Principles of Operating Systems Spring 2017

Topics: Memory Management (SGG, Chapter 08) 8.1, 8.2, 8.3, 8.5, 8.6 CS 3733 Operating Systems

Memory Hierarchy. Mehran Rezaei

Virtual Memory, Address Translation

CS 333 Introduction to Operating Systems. Class 11 Virtual Memory (1) Jonathan Walpole Computer Science Portland State University

Topic 18 (updated): Virtual Memory

CS 318 Principles of Operating Systems

Processes and Virtual Memory Concepts

Virtual Memory Nov 9, 2009"

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

16 Sharing Main Memory Segmentation and Paging

Virtual Memory Oct. 29, 2002

15 Sharing Main Memory Segmentation and Paging

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

COSC3330 Computer Architecture Lecture 20. Virtual Memory

Virtual Memory, Address Translation

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

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

CSE 451: Operating Systems Winter Page Table Management, TLBs and Other Pragmatics. Gary Kimura

virtual memory Page 1 CSE 361S Disk Disk

Chapter 5B. Large and Fast: Exploiting Memory Hierarchy

Virtual Memory. CS 3410 Computer System Organization & Programming

ECE 571 Advanced Microprocessor-Based Design Lecture 12

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

John Wawrzynek & Nick Weaver

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

ECE331: Hardware Organization and Design

Virtual Memory. Computer Systems Principles

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

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

COS 318: Operating Systems. Virtual Memory and Address Translation

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 23

Computer Systems. Virtual Memory. Han, Hwansoo

Chapter 5 (Part II) Large and Fast: Exploiting Memory Hierarchy. Baback Izadi Division of Engineering Programs

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

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

ECE 598 Advanced Operating Systems Lecture 14

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

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

Recall from Tuesday. Our solution to fragmentation is to split up a process s address space into smaller chunks. Physical Memory OS.

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

Memory Management. An expensive way to run multiple processes: Swapping. CPSC 410/611 : Operating Systems. Memory Management: Paging / Segmentation 1

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 23

Operating Systems. Operating Systems Sina Meraji U of T

CSE 560 Computer Systems Architecture

ECE 571 Advanced Microprocessor-Based Design Lecture 13

CSE 351. Virtual Memory

Virtual Memory: From Address Translation to Demand Paging

CS 152 Computer Architecture and Engineering. Lecture 9 - Address Translation

Virtual Memory. Samira Khan Apr 27, 2017

CSE 153 Design of Operating Systems

CS 152 Computer Architecture and Engineering. Lecture 9 - Address Translation

CPS104 Computer Organization and Programming Lecture 16: Virtual Memory. Robert Wagner

Virtual Memory. CS 351: Systems Programming Michael Saelee

Memory Hierarchy. Goal: Fast, unlimited storage at a reasonable cost per bit.

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

CS 31: Intro to Systems Virtual Memory. Kevin Webb Swarthmore College November 15, 2018

18-447: Computer Architecture Lecture 16: Virtual Memory

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

Memory Management. Dr. Yingwu Zhu

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

CPS 104 Computer Organization and Programming Lecture 20: Virtual Memory

Operating Systems. Paging... Memory Management 2 Overview. Lecture 6 Memory management 2. Paging (contd.)

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

Chapter 6 Memory 11/3/2015. Chapter 6 Objectives. 6.2 Types of Memory. 6.1 Introduction

ECE232: Hardware Organization and Design

EECS 470. Lecture 16 Virtual Memory. Fall 2018 Jon Beaumont

LECTURE 4: LARGE AND FAST: EXPLOITING MEMORY HIERARCHY

CISC 360. Virtual Memory Dec. 4, 2008

CS370 Operating Systems

Memory Management. Reading: Silberschatz chapter 9 Reading: Stallings. chapter 7 EEL 358

Memory Management. Dr. Yingwu Zhu

Topic 18: Virtual Memory

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

PROCESS VIRTUAL MEMORY. CS124 Operating Systems Winter , Lecture 18

1. Memory technology & Hierarchy

1. Creates the illusion of an address space much larger than the physical memory

Reducing Hit Times. Critical Influence on cycle-time or CPI. small is always faster and can be put on chip

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

Virtual Memory: Concepts

CS 152 Computer Architecture and Engineering. Lecture 8 - Address Translation

EE 4683/5683: COMPUTER ARCHITECTURE

Transcription:

Virtual Memory I wish we were still doing NAND gates Study Chapters 7.4-7.8 Finally! A lecture on something I understand PAGE FAULTS! L23 Virtual Memory 1

You can never be too rich, too good looking, or have too much memory! Now that we know how to FAKE a FAST memory, we ll turn our attention to FAKING a LARGE memory. L23 Virtual Memory 2

Micron s 10. Keeping TI s memory division in business. 9. To provide unique addresses for every internet host. 8. Generates good Comp 411 final problems. 7. Performing multiplies via table lookup 6. So we never need to run garbage collection 5. To emulate a Turning Machine s tape. 4. To support lazy programmers. Top 10 Reasons for a BIG Address Space 3. Isolating ISA from IMPLEMENTATION details of HW configuration shouldn t enter into SW design HOW MUCH and WHERE memory is 2. Usage UNCERTAINTY provides for run-time expansion of stack and heap 1. Programming CONVENIENCE create regions of memory with different semantics: read-only, shared, etc. avoid annoying bookkeeping L23 Virtual Memory 3

Lessons from History There is only one mistake that can be made in computer design that is difficult to recover from not having enough address bits for memory addressing and memory management. Gordon Bell and Bill Strecker speaking about the PDP-11 in 1976 A partial list of successful machines that eventually starved to death for lack of address bits includes the PDP 8, PDP 10, PDP 11, Intel 8080, Intel 8086, Intel 80186, Intel 80286, Motorola 6800, AMI 6502, Zilog Z80, Cray-1, and Cray X-MP. Hennessy & Patterson Why? Address size determines minimum width of anything that can hold an address: PC, registers, memory words, HW for address arithmetic (branches/jumps, loads/stores). When you run out of address space it s time for a new ISA! L23 Virtual Memory 4

Squandering Address Space Address Space STACK: How much to reserve? (consider RECURSION!) HEAP: N variable-size data records... Bound N? Bound Size? OBSERVATIONS: Can t BOUND each usage... without compromising use. Actual use is SPARSE Working set even MORE sparse CODE, large monolithic programs (eg, Office, Netscape)... only small portions might be used add-ins and plug-ins shared libraries/dlls L23 Virtual Memory 5

Extending the Memory Hierarchy 3x-20x 10 4 x-10 5 x CPU FAST STATIC DYNAMIC RAM DISK "CACHE" "MAIN MEMORY" "Secondary Storage" So far, we ve used SMALL fast memory + BIG slow memory to fake a BIG FAST memory (caching). Can we combine RAM and DISK to fake DISK sized at near RAM speeds? VIRTUAL MEMORY VIRTUAL MEMORY use of RAM as cache to much larger storage pool, on slower devices TRANSPARENCY - VM locations "look" the same to program whether on use of RAM as cache to much larger storage pool, on slower devices TRANSPARENCY - VM locations "look" the same to program whether on DISK or in RAM. DISK or in RAM. ISOLATION of of actual actual RAM RAM size size from from software. L23 Virtual Memory 6

Virtual Memory ILLUSION: Huge memory (2 32 (4G) bytes? 2 64 (18E) bytes?) 2 30 Giga 2 40 Terra 2 50 Peta 2 60 Exa Memory Management Unit ACTIVE USAGE: small fraction (2 24 bytes?) Actual HARDWARE: 2 30 (1G) bytes of RAM 2 38 (256G) bytes of DISK...... maybe more, maybe less! CPU VA MMU PA RAM ELEMENTS OF DECEIT: Partition memory into manageable chunks-- Pages (4K-8K-16K-64K) MAP a few to RAM, assign others to DISK Keep HOT pages in RAM. VIRTUAL MEMORY PAGES PHYSICAL MEMORY PAGES L23 Virtual Memory 7

Simple Page Map Design Page Index Page Map A special memory that holds Physical-to-Virtual Mappings Virtual Page # Physical Page # FUNCTION: Given Virtual Address, Map to PHYSICAL address OR Cause PAGE FAULT allowing page replacement D Dirty bit (for write-back) R Resident bit (1 if in main memory) PPN Physical Page Number Why use HIGH address bits to index pages?... LOCALITY. Keeps related data on same page. Why use LOW address bits to index cache lines?... LOCALITY. Keeps related data from competing for same cache lines. Virtual Memory D R PPN X X X PAGEMAP Physical Memory L23 Virtual Memory 8

Virtual Memory vs. Cache index TAG DATA A Mem[A] B Mem[B] MAIN MEMORY CACHE: Relatively short blocks (16-64 bytes) Few lines: scarce resource miss time: 3x-20x hit time =? VPAGE NO. OFFSET index PAGEMAP PHYSICAL MEMORY VIRTUAL MEMORY: Disk: long latency, fast xfer miss time: ~10 5 x hit time write-back essential! large pages in RAM Lots of lines: one for each page Tags in page map, data in physical memory L23 Virtual Memory 9

Virtual Memory: A H/W view Pagemap Characteristics: Virtual Memory Physical Memory D R PPN One entry per virtual page! 0 0 0 0 1 0 0 1 1 0 0 1 1 0 X X X PAGEMAP RESIDENT bit = 1 for pages stored in RAM, or 0 for non-resident (disk or unallocated). Page fault when R = 0. Contains PHYSICAL page number (PPN) of each resident page DIRTY bit says we ve changed this page since loading it from disk (and therefore need to write it to disk when it s replaced) L23 Virtual Memory 10

Virtual Memory: A S/W view Problem: Translate VIRTUAL ADDRESS to PHYSICAL ADDRESS int VtoP(int VPageNo,int PO) { if (R[VPageNo] == 0) PageFault(VPageNo); return (PPN[VPageNo] << p) PO; } Virtual Page # /* Handle a missing page... */ void PageFault(int VPageNo) { int i; i = SelectLRUPage(); if (D[i] == 1) WritePage(DiskAdr[i],PPN[i]); R[i] = 0; Physical Page # } PPN[VPageNo] = PPN[i]; ReadPage(DiskAdr[VPageNo],PPN[i]); R[VPageNo] = 1; D[VPageNo] = 0; L23 Virtual Memory 11

The HW/SW Balance IDEA: devote HARDWARE to high-traffic, performance-critical path use (slow, cheap) SOFTWARE to handle exceptional cases hardware int VtoP(int VPageNo,int PO) { if (R[VPageNo] == 0)PageFault(VPageNo); return (PPN[VPageNo] << p) PO; } software /* Handle a missing page... */ void PageFault(int VPageNo) { int i = SelectLRUPage(); if (D[i] == 1) WritePage(DiskAdr[i],PPN[i]); R[i] = 0; } PA[VPageNo] = PPN[i]; ReadPage(DiskAdr[VPageNo],PPN[i]); R[VPageNo] = 1; D[VPageNo] = 0; HARDWARE performs address translation, detects page faults: running program interrupted ( suspended ); PageFault( ) is forced; On return from PageFault; running program continues L23 Virtual Memory 12

Page Map Arithmetic p VPageNo PO v m PPageNo PO D R PPN 1 1 1 0 1 PAGEMAP PHYSICAL MEMORY (v + p) bits in virtual address (m + p) bits in physical address 2 v number of VIRTUAL pages 2 m number of PHYSICAL pages 2 p bytes per physical page 2 v+p bytes in virtual memory 2 m+p bytes in physical memory (m+2)2 v bits in the page map Typical page size: 8K 128K bytes Typical (v+p): 32 (or more) bits Typical (m+p): 27 31 bits (128 2048 MB) L23 Virtual Memory 13

Example: Page Map Arithmetic Virtual Page # SUPPOSE... 32-bit Virtual address 2 14 page size (16 KB) 2 28 RAM (256 MB) Physical Page # THEN: 2 28 /2 14 = 16384 # Physical Pages = 2 32 /2 14 = 2 18 # Virtual Pages = 262,144 # Page Map Entries = Use SRAM for page map??? OUCH! L23 Virtual Memory 14

RAM-Resident Page Maps SMALL page maps can use dedicated RAM but, gets this approach gets expensive for big ones! SOLUTION: Move page map into MAIN MEMORY: Virtual Address Physical Memory virtual page number physical page number PROBLEM: Each Each memory reference now now takes 2 accesses to to physical memory! The overhead for the pagemap is smaller than you might think. From the previous example: 4*2 18 /2 28 = 0.4 % Physical memory pages that hold page map entries L23 Virtual Memory 15

Translation Look-aside Buffer (TLB) PROBLEM: 2x performance hit each memory reference now takes 2 accesses! SOLUTION: CACHE the recent page map entries Virtual Address Physical Memory First, look in TLB On miss, do translation and store result On hit, skip translation virtual page number TLB: small, usually fully-associative cache for mapping VPN PPN physical page number IDEA: LOCALITY in in memory reference patterns SUPER locality in in references to to page page map map VARIATIONS: sparse page page map map storage paging the the page page map map L23 Virtual Memory 16

Optimizing Sparse Page Maps For large Virtual Address spaces only a small percentage of page table entries contain Mappings. This is because some range of addresses are never used by the application. How can we save space in the pagemap? Virtual Address Physical Memory virtual page number physical page TLB number On TLB miss: look up VPN in sparse data structure (e.g., a list of VPN-PPN pairs) use hash coding to speed search only have entries for ALLOCATED pages allocate new entries on demand time penalty? LOW if TLB hit rate is high Should we we do do this this in in HW HW or or SW? SW? L23 Virtual Memory 17

Multilevel Page Maps Given a HUGE virtual memory, the cost of storing all of the page map entries in RAM may STILL be too expensive SOLUTION: A hierarchical page map take advantage of the observation that while the virtual memory address space is large, it is generally sparsely populated with clusters of pages. Consider a machine with a 32-bit virtual address space and 64 MB (26-bit) of physical memory that uses 4 KB pages. Assuming 4 byte page-table entries, a single-level page map requires 4MB ( >6% of the available memory). Of these, more than 98% will reference non-resident pages (Why?). A 2-level look-up increases the size of the worse-case page table slightly. However, if a first level entry is nonresident bit it saves large amounts of memory. PTBL 32-bit virtual address 10 10 12 Usually, an on-chip register Level 1 Level 2 A clever designer will notice that if the 2 nd level tables are pagesized they too can be paged out (stored on disk) Data Doesn t that mean we now have to do 3 accesses to get what we want? L23 Virtual Memory 18

Example: Mapping VAs to PAs Suppose virtual memory of 2 32 (4G) bytes physical memory of 2 30 (1G) bytes page size is 2 14 (16 K) bytes VPN R D PPN ----+-------- 0 0 0 2 1 1 1 7 2 1 0 0 3 1 0 5 4 0 0 5 5 1 0 3 6 1 1 2 7 1 0 4 8 1 0 1 1. How many pages can be stored in physical memory at once? 2 30-14 = 2 16 = 64K 2. How many entries are there in the page table? 2 32-14 = 2 18 = 256K 3. How many bits are necessary per entry in the page table? (Assume each entry has PPN, resident bit, dirty bit) 16 (PPN) + 2 = 18 4. How many pages does the page table require? 2 18 /(2 14-2 ) = 2 6 = 64 5. A portion of the page table is given to the left. What is the physical address for virtual address 0x00004110? L23 Virtual Memory 19

Contexts A context is a complete set of mappings from VIRTUAL to PHYSICAL locations, as dictated by the full contents of the page map: We might like to support multiple VIRTUAL to PHYSICAL Mappings and, thus, multiple Contexts. Virtual Memory Physical Memory D R X X X PAGEMAP Several programs may be simultaneously loaded into main memory, each in its separate context: Virtual Memory 1 Physical Memory Virtual Memory 2 Context Switch : Reload the page map! You end up with pages from different applications simultaneously in memory. map map L23 Virtual Memory 20

Contexts: A Sneak Preview Virtual Memory 1 Physical Memory Virtual Memory 2 Every application can be written as if it has access to all of memory, without considering where other applications reside. 1. TIMESHARING among several programs -- Separate context for each program First Glimpse at a VIRTUAL MACHINE OS loads appropriate context into pagemap when switching among pgms 2. Separate context for OS Kernel (eg, interrupt handlers)... Kernel vs User contexts Switch to Kernel context on interrupt; Switch back on interrupt return. HARDWARE SUPPORT: 2 HW pagemaps L23 Virtual Memory 21

Example: UltraSPARC II MMU Huge 64-bit address space (only 44-bits implemented) 2 43 ITLB 64 Physical Address 2 64 C# C# C# C# 2 43 Virtual Address 51 13 48 16 45 19 42 22 64 (44 used) 4 page sizes: 8KB, 64KB, 512KB, 4MB DTLB 64 41 (2200 GB) TLB miss: SW refills TLB from TSB cache (HW helps compute address) TSB Translation Storage Buffer C# = context TSB (direct-mapped) L23 Virtual Memory 22

Using Caches with Virtual Memory Virtual Cache Tags Tags match match virtual virtual addresses Physical Cache Tags Tags match match physical addresses CPU CACHE MMU DYNAMIC RAM These TAGs are physical, they hold addresses after translation. CPU MMU CACHE DYNAMIC RAM These TAGs are virtual, they represent addresses before translation. Problem: cache invalid after context switch FAST: No MMU time on HIT DISK DISK Avoids stale cache data after context switch SLOW: MMU time on HIT Counter intuitively perhaps, physically addressed Caches are the trend, because they better support parallel processing L23 Virtual Memory 23

Best of Both Worlds MMU DISK CPU DYNAMIC RAM CACHE OBSERVATION: If cache line selection is based on unmapped page offset bits, RAM access in a physical cache can overlap page map access. Tag from cache is compared with physical page number from MMU. Want small cache index go with more associativity L23 Virtual Memory 24

Summary Virtual Memory Makes a small PHYSICAL memory appear to be a large VIRTUAL one Break memory into managable chunks PAGES Pagemap A table for mapping Virtual-to-Physical pages Each entry has Resident, Dirty, and Physical Page Number Can get large if virtual address space is large Store in main memory TLB Translation Look-aside Buffer A pagemap cache Contexts Sets of virtual-to-physical mapping that allow pages from multiple applications to be in physical memory simultaneously (even if they have the same virtual addresses) L23 Virtual Memory 25