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

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

John Wawrzynek & Nick Weaver

Virtual Memory: From Address Translation to Demand Paging

Virtual Memory: From Address Translation to Demand Paging

CS 61C: Great Ideas in Computer Architecture Virtual Memory. Instructors: John Wawrzynek & Vladimir Stojanovic

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

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

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

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

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

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

Modern Virtual Memory Systems. Modern Virtual Memory Systems

CS 152 Computer Architecture and Engineering. Lecture 11 - Virtual Memory and Caches

CS 152 Computer Architecture and Engineering. Lecture 9 - Virtual Memory

CS 61C: Great Ideas in Computer Architecture (Machine Structures) Intro to Virtual Memory

Lecture 9 - Virtual Memory

ecture 33 Virtual Memory Friedland and Weaver Computer Science 61C Spring 2017 April 12th, 2017

Chapter 5B. Large and Fast: Exploiting Memory Hierarchy

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

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

Processes and Virtual Memory Concepts

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

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

Lecture 9 Virtual Memory

Computer Systems. Virtual Memory. Han, Hwansoo

CS 318 Principles of Operating Systems

Lecture 21: Virtual Memory. Spring 2018 Jason Tang

Virtual Memory: Concepts

Lecture 19: Virtual Memory: Concepts

New-School Machine Structures. Overarching Theme for Today. Agenda. Review: Memory Management. The Problem 8/1/2011

CSE 153 Design of Operating Systems

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

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

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

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

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

virtual memory Page 1 CSE 361S Disk Disk

Virtual Memory Oct. 29, 2002

Last =me in Lecture 7 3 C s of cache misses Compulsory, Capacity, Conflict

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

CSE 120 Principles of Operating Systems

Virtual Memory II CSE 351 Spring

CSE 351. Virtual Memory

CSE 120 Principles of Operating Systems Spring 2017

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 24

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

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

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

ECE 4750 Computer Architecture, Fall 2017 T03 Fundamental Memory Concepts

Chapter 8. Virtual Memory

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 23

Virtual Memory. Samira Khan Apr 27, 2017

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

Virtual Memory. Computer Systems Principles

HY225 Lecture 12: DRAM and Virtual Memory

Address Translation. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CSE 560 Computer Systems Architecture

1. Memory technology & Hierarchy

CISC 360. Virtual Memory Dec. 4, 2008

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

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

Virtual Memory I. CSE 351 Spring Instructor: Ruth Anderson

CS252 Spring 2017 Graduate Computer Architecture. Lecture 17: Virtual Memory and Caches

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

Computer Architecture. R. Poss

Memory Hierarchy Requirements. Three Advantages of Virtual Memory

Virtual Memory: Concepts

CPS 104 Computer Organization and Programming Lecture 20: Virtual Memory

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

Topic 18: Virtual Memory

CS399 New Beginnings. Jonathan Walpole

Sarah L. Harris and David Money Harris. Digital Design and Computer Architecture: ARM Edition Chapter 8 <1>

Virtual Memory Nov 9, 2009"

CS 61C: Great Ideas in Computer Architecture Virtual Memory. Instructors: Krste Asanovic & Vladimir Stojanovic h>p://inst.eecs.berkeley.

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

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

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

CS 153 Design of Operating Systems Winter 2016

ECE468 Computer Organization and Architecture. Virtual Memory

Virtual Memory. CS 3410 Computer System Organization & Programming

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

ECE4680 Computer Organization and Architecture. Virtual Memory

Virtual Memory II. CSE 351 Autumn Instructor: Justin Hsia

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

Virtual Memory. Virtual Memory

VIRTUAL MEMORY II. Jo, Heeseung

Computer Structure. X86 Virtual Memory and TLB

ADDRESS TRANSLATION AND TLB

VM as a cache for disk

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

Virtual to physical address translation

Topic 18 (updated): Virtual Memory

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

Virtual Memory. Motivation:

ADDRESS TRANSLATION AND TLB

Learning to Play Well With Others

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

Digital Logic & Computer Design CS Professor Dan Moldovan Spring Copyright 2007 Elsevier 8-<1>

Lecture 24: Memory, VM, Multiproc

Transcription:

CS 61C: Great Ideas in Computer Architecture Lecture 23: Virtual Memory Bernhard Boser & Randy Katz http://inst.eecs.berkeley.edu/~cs61c

Agenda Virtual Memory Paged Physical Memory Swap Space Page Faults Hierarchical Page Tables Caching Page Table Entries (TLB) CS 61c Lecture 23: Virtual Memory 2

Virtual Machine 100+ Processes, managed by OS 100 s of processes OS multiplexes these over available cores But what about memory? There is only one! We cannot just save its contents in a context switch CS 61c Lecture 23: Virtual Memory 3

Virtual versus Physical Addresses Processor (& Caches) Memory (DRAM) Control Datapath PC Registers (ALU) Virtual Address? Physical Address Bytes Many of these (software & hardware cores) One main memory Processes use virtual addresses, e.g. 0 0xffff,ffff Many processes, all using same (conflicting) addresses Memory uses physical addresses (also, e.g. 0... 0xffff,ffff) Memory manager maps virtual to physical addresses CS 61c Lecture 23: Virtual Memory 4

Address Spaces Address space = set of addresses for all available memory locations Now, 2 kinds of memory addresses: Virtual Address Space set of addresses that the user program knows about Physical Address Space set of addresses that map to actual physical cells in memory hidden from user applications Memory manager maps between these two address spaces CS 61c Lecture 23: Virtual Memory 5

Conceptual Memory Manager Memory (DRAM) Concept: Real memory managers use more complex mappings. CS 61c Lecture 23: Virtual Memory 6

Responsibilities of Memory Manager 1) Map virtual to physical addresses 2) Protection: Isolate memory between processes Each process gets dedicate private memory Errors in one program won t corrupt memory of other program Prevent user programs from messing with OS memory 3) Swap memory to disk Give illusion of larger memory by storing some content on disk Disk is usually much larger & slower than DRAM Use clever caching strategies CS 61c Lecture 23: Virtual Memory 7

Agenda Virtual Memory Paged Physical Memory Swap Space Hierarchical Page Tables Caching Page Table Entries (TLB) CS 61c Lecture 23: Virtual Memory 8

Memory Manager Several options Today paged memory dominates Physical memory (DRAM) is broken into pages Typical page size: 4 KiB+ Virtual address (e.g. 32 Bits) page number (e.g. 20 Bits) offset (e.g. 12 Bits) CS 61c Lecture 23: Virtual Memory 9

Paged Memory Page Table Memory (DRAM) Page N Page Table Page Table Page 2 Page 1 Page 0 Each process has a dedicated page table. Physical memory non-consecutive. CS 61c Lecture 23: Virtual Memory 10

Paged Memory Address Translation OS keeps track of which process is active Chooses correct page table Memory manager extracts page number from virtual address Looks up page address in page table Virtual address (e.g. 32 Bits) page table entry Physical address page number offset offset Computes physical memory address from sum of page address and offset (from virtual address) Physical addresses may (but do not have to) have more or fewer bits than virtual addresses CS 61c Lecture 23: Virtual Memory 11

Protection Assigning different pages in DRAM to processes also keeps them from accessing each others memory Isolation Page tables handled by OS (in supervisory mode) Sharing is possible also OS may assign same physical page to several processes CS 61c Lecture 23: Virtual Memory 12

Write Protection Write protected 0 Page Table Memory (DRAM) Page N 0 1 Page Table 1 0 0 1 Page Table Page 2 0 Page 1 Page 0 Exception when writing to protected page (e.g. program code). CS 61c Lecture 23: Virtual Memory 13

Where do Page Tables Reside? E.g. 32-Bit virtual address, 4-KiB pages Single page table size: 4 x 2 20 Bytes = 4-MiB 0.1% of 4-GiB memory But much too large for a cache! Store page tables in memory (DRAM) Two (slow) memory accesses per lw/sw on cache miss How could we minimize the performance penalty? Transfer blocks (not words) between DRAM and processor cache o Exploit spatial locality Cache for frequently used page table entries CS 61c Lecture 23: Virtual Memory 14

Paged Table Storage in DRAM Memory (DRAM) Page Table Page Table Page Page Table lw/sw take two memory references CS 61c Lecture 23: Virtual Memory 15

Blocks vs. Pages In caches, we dealt with individual blocks Usually ~64B on modern systems In VM, we deal with individual pages Usually ~4 KB on modern systems Common point of confusion: Bytes, Words, Blocks, Pages are all just different ways of looking at memory! CS 61c Lecture 23: Virtual Memory 16

Bytes, Words, Blocks, Pages Eg: 16 KiB DRAM, 4 KiB Pages (for VM), 128 B blocks (for caches), 4 B words (for lw/sw) 1 of 1 Memory 1 of 4 Pages per Memory 1 of 32 Blocks per Page Block 31 Word 31 16 KiB Page 3 Can think of Page memory 2 as: 4 Pages, or 128 Blocks, or 4096 Words, or Page 1 16,384 Bytes Can think of a page as: 32 Blocks, or 1024 Words Page 0 Block 0 Word 0

Agenda Virtual Memory Paged Physical Memory Swap Space Hierarchical Page Tables Caching Page Table Entries (TLB) CS 61c Lecture 23: Virtual Memory 18

Memory Hierarchy Disk Slow But huge How could we make use of its capacity (when running low on DRAM)? CS 61c 19

Aside why is disk so slow? 10,000 rpm (revolutions per minute) 6 ms per revolution Average random access time: 3 ms CS 61c Lecture 23: Virtual Memory 20

What about SSD? Made with transistors Nothing mechanical that turns Like Ginormous register file That does not forget when power is off Fast access to all cells, regardless of address Still much slower than register, DRAM Read/write blocks, not bytes Potential reliability issues CS 61c Lecture 23: Virtual Memory 21

Paged Memory Valid: page allocated DRAM/disk Memory (DRAM) Page N Page Table Disk Page Table Each process has a dedicated page table. Physical memory non-consecutive. CS 61c Lecture 23: Virtual Memory 22

Memory Access Check page table entry: Valid? Yes, valid à In DRAM? o Yes, in DRAM: read/write data o No, on disk: allocate new page in DRAM If out of memory, evict a page from DRAM Store evicted page to disk Read page from disk into memory Page fault OS intervention Read/write data Not Valid o allocate new page in DRAM If out of memory, evict a page Read/write data CS 61c Lecture 23: Virtual Memory 23

Lecture 4: Out of Memory Insufficient free memory: malloc() returns NULL $ gcc OutOfMemory.c;./a.out failed to allocate > 131 TiBytes What s going on? CS 61c Lecture 23: Virtual Memory 24

Write-Through or Write-Back? DRAM acts like cache for disk Should writes go directly to disk (write-through)? Or only when page is evicted? Which option do you propose? Implementation? CS 61c Lecture 23: Virtual Memory 25

Agenda Virtual Memory Paged Physical Memory Swap Space Hierarchical Page Tables Caching Page Table Entries (TLB) CS 61c Lecture 23: Virtual Memory 26

Size of Page Tables E.g. 32-Bit virtual address, 4-KiB pages Single page table size: 4 x 2 20 Bytes = 4-MiB 0.1% of 4-GiB memory Total size for 256 processes (each needs a page table) 256 x 4 x 2 20 Bytes = 256 x 4-MiB = 1-GiB 25% of 4-GiB memory! What about 64-bit addresses? How can we keep the size of page tables reasonable? CS 61c Lecture 23: Virtual Memory 27

Options Increase page size E.g. doubling page size cuts PT size in half At the expense of potentially wasted memory Hierarchical page tables With decreasing page size Most programs use only fraction of memory Split PT in two (or more) parts CS 61c Lecture 23: Virtual Memory 28

Hierarchical Page Table exploits sparcity of virtual address space use Virtual Address 31 22 21 12 11 p1 p2 offset 0 10-bit L1 index Root of the Current Page Table (Processor Register) 10-bit L2 index p1 page in primary memory page in secondary memory p2 Level 1 Page Table Page size 10b à 1024 x 4096B Level 2 Page Tables 12b à 4096B Physical Memory PTE of a nonexistent page Lecture 23: Virtual Memory Data Pages 29

Agenda Virtual Memory Paged Physical Memory Swap Space Hierarchical Page Tables Caching Page Table Entries (TLB) CS 61c Lecture 23: Virtual Memory 30

Address Translation & Protection Kernel/User Mode Read/Write Virtual Address Protection Check Virtual Page No. (VPN) Miss Address Translation offset Exception? Physical Address Physical Page No. (PPN) offset Every instruction and data access needs address translation and protection checks A good VM design needs to be fast (~ one cycle) and space efficient CS 61c Lecture 23: Virtual Memory 31

Translation Lookaside Buffers (TLB) Address translation is very expensive! In a two-level page table, each reference becomes three memory accesses Solution: Cache some translations in TLB TLB hit TLB miss Þ Single-Cycle Translation Þ Page-Table Walk to refill virtual address VPN offset V D tag PPN (VPN = virtual page number) (PPN = physical page number) hit? physical address PPN offset CS 61c Lecture 23: Virtual Memory 32

TLB Designs Typically 32-128 entries, usually fully associative Each entry maps a large page, hence less spatial locality across pages => more likely that two entries conflict Sometimes larger TLBs (256-512 entries) are 4-8 way set-associative Larger systems sometimes have multi-level (L1 and L2) TLBs Random or FIFO replacement policy TLB Reach : Size of largest virtual address space that can be simultaneously mapped by TLB Example: 64 TLB entries, 4KB pages, one page per entry TLB Reach =? CS 61c Lecture 23: Virtual Memory 33

VM-related events in pipeline PC Inst TLB Inst. Cache D Decode E + M Data TLB Data Cache W TLB miss? Page Fault? Protection violation? Handling a TLB miss needs a hardware or software mechanism to refill TLB usually done in hardware Handling a page fault (e.g., page is on disk) needs a precise trap so software handler can easily resume after retrieving page Protection violation may abort process TLB miss? Page Fault? Protection violation? CS 61c Lecture 23: Virtual Memory 34

Hierarchical Page Table Walk: SPARC v8 Virtual Address Context Table Register Context Register Context Table root ptr Index 1 Index 2 Index 3 Offset 31 23 17 11 0 L1 Table PTP L2 Table PTP L3 Table PTs can be swapped to disk, too What about code handling page faults? Physical Address 31 PPN PTE 11 Offset MMU does this table walk in hardware on a TLB miss FSM? CS 61c Lecture 23: Virtual Memory 35

Page-Based Virtual-Memory Machine (Hardware Page-Table Walk) Page Fault? Protection violation? Virtual Address PC Inst. TLB Physical Address Inst. Cache D Decode E + M Page Fault? Protection violation? Virtual Address Data TLB Physical Address Data Cache W Miss? Page-Table Base Register Miss? Hardware Page Table Walker Physical Address Memory Controller Physical Address Physical Address CS 61c Main Memory (DRAM) Assumes page tables held in untranslated physical memory Lecture 23: Virtual Memory 36

Address Translation: putting it all together Virtual Address TLB Lookup hardware hardware or software software miss hit Page Table Walk Protection Check the page is Ï memory Î memory denied permitted Page Fault (OS loads page) Update TLB Protection Fault Physical Address (to cache) Where? CS 61c SEGFAULT Lecture 23: Virtual Memory 37

Modern Virtual Memory Systems Illusion of a large, private, uniform store Protection & Privacy Several users/processes, each with their private address space Demand Paging Provides the ability to run programs larger than the primary memory OS user i Swapping Space (Disk) Primary Memory Hides differences in machine configurations The price is address translation on each memory reference CS 61c Lecture 23: Virtual Memory VA mapping TLB PA 38

It s just the OS Let s write execve Code the loads program into memory for execution What s the best way? CS 61c Lecture 23: Virtual Memory 39

Memory (DRAM) Disk Execve 1. Set up PT for program code and stack 2. Init argv, argc 3. Call main what happens? page fault 4. Anything wrong with this? linker? fix? Architecture matters! CS 61c Lecture 23: Virtual Memory 40

And, in Conclusion Virtual & physical addresses Program à virtual address DRAM à physical address Paged Memory 1. Facilitates virtual à physical address translation 2. Provides isolation & protection 3. Extends available memory to include disk Implementation issues Hierarchical page tables Caching page table entries (TLB) CS 61c Lecture 23: Virtual Memory 41