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

Similar documents
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

Virtual Memory: From Address Translation to Demand Paging

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

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

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

Modern Virtual Memory Systems. Modern Virtual Memory Systems

John Wawrzynek & Nick Weaver

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

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

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

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

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

Chapter 5B. Large and Fast: Exploiting Memory Hierarchy

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

Lecture 9 - Virtual Memory

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

CSE 351. Virtual Memory

Virtual Memory. Virtual Memory

COSC3330 Computer Architecture Lecture 20. Virtual Memory

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

Virtual Memory. Motivation:

CS 318 Principles of Operating Systems

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

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

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

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

VIRTUAL MEMORY II. Jo, Heeseung

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

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

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

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

Virtual Memory Oct. 29, 2002

Virtual Memory. Samira Khan Apr 27, 2017

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

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

Processes and Virtual Memory Concepts

Computer Architecture Lecture 13: Virtual Memory II

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

CPS 104 Computer Organization and Programming Lecture 20: Virtual Memory

Lecture 9 Virtual Memory

Computer Science 146. Computer Architecture

Address Translation. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

virtual memory Page 1 CSE 361S Disk Disk

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

CSE 120 Principles of Operating Systems

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

CSE 120 Principles of Operating Systems Spring 2017

Virtual Memory. CS 351: Systems Programming Michael Saelee

ECE 4750 Computer Architecture, Fall 2017 T03 Fundamental Memory Concepts

Chapter 8. Virtual Memory

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

Computer Structure. X86 Virtual Memory and TLB

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

Computer Systems. Virtual Memory. Han, Hwansoo

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

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

Virtual Memory Nov 9, 2009"

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

Learning to Play Well With Others

Virtual to physical address translation

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

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

CISC 360. Virtual Memory Dec. 4, 2008

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

Virtual Memory. CS 3410 Computer System Organization & Programming

Virtual memory Paging

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

Memory Hierarchy Requirements. Three Advantages of Virtual Memory

Computer Architecture. Lecture 8: Virtual Memory

ADDRESS TRANSLATION AND TLB

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

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

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

ADDRESS TRANSLATION AND TLB

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

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

ECE468 Computer Organization and Architecture. Virtual Memory

Virtual Memory: Concepts

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

Improving Cache Performance and Memory Management: From Absolute Addresses to Demand Paging. Highly-Associative Caches

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

Virtual Memory. Kevin Webb Swarthmore College March 8, 2018

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

ECE4680 Computer Organization and Architecture. Virtual Memory

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

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

Virtual Memory. Computer Systems Principles

CS 153 Design of Operating Systems Winter 2016

ECE232: Hardware Organization and Design

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

CSE 153 Design of Operating Systems

Systems Programming and Computer Architecture ( ) Timothy Roscoe

Virtual Memory II. CSE 351 Autumn Instructor: Justin Hsia

Lecture 19: Virtual Memory: Concepts

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

Virtual Memory II. CSE 351 Autumn Instructor: Justin Hsia

How to create a process? What does process look like?

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

EN1640: Design of Computing Systems Topic 06: Memory System

Transcription:

Virtual Memory Daniel Sanchez Computer Science & Artificial Intelligence Lab M.I.T. L20-1

Reminder: Operating Systems Goals of OS: Protection and privacy: Processes cannot access each other s data Abstraction: Hide away details of underlying hardware process 1 e.g., processes open and access files instead of issuing raw commands to hard drive Resource management: Controls how processes share hardware resources (CPU, memory, disk, etc.) Operating system Hardware process N Key enabling technologies: User mode + supervisor mode Interrupts to safely transition into supervisor mode Virtual memory to abstract the storage resources of the machine Last lecture Today L20-2

Virtual Memory (VM) Systems Illusion of a large, private, uniform store Protection & Privacy Each process has a private address space Demand Paging Provides the ability to run programs larger than main memory Hides differences in machine configuration Primary Memory OS proc i Secondary Store The price of VM is address translation on each memory reference L20-3

Names for Memory Locations machine language address ISA Segmentation or Paging Machine language address As specified in machine code Virtual address ISA specifies translation of machine code address into virtual address of program variable (sometimes called effective address) Physical address virtual address Address Mapping physical address Physical Memory (DRAM) Operating system specifies mapping of virtual address into name for a physical memory location L20-4

Segmentation (Base-and-Bound) Address Translation 0x0 Process Address Space Bound Reg Bound Violation Exception 0x0 Physical Memory Base Reg 0x0fff Virtual Address + Physical Address Process Code & Data Each program s data is allocated in a contiguous segment of physical memory Physical address = Virtual Address + Segment Base Bound register provides safety and isolation 0xf..ff Base and Bound registers should not be accessed by user programs (only accessible in supervisor mode) L20-5

Main Memory Separate Segments for Code and Data Load X Data Bound Register Virtual Address Bounds Violation? data segment Data Base Register + Program Address Space Code Bound Register Program Counter Code Base Register + Bounds Violation? code segment Pros of this separation? Prevents buggy program from overwriting code Multiple processes can share code segment L20-6

Memory Fragmentation proc 1 proc 2 proc 3 OS Space 16K 24K 24K 32K Processes 4 & 5 start proc 1 proc 2 proc 4 proc 3 OS Space 16K 24K 16K 8K 32K Processes 2 & 5 end proc 1 proc 4 proc 3 OS Space 16K 24K 16K 8K 32K free 24K proc 5 24K 24K As processes start and end, storage is fragmented. Therefore, at some point segments have to be moved around to compact the free space. L20-7

Paged Memory Systems Divide physical memory in fixed-size blocks called pages Typical page size: 4KB Interpret each virtual address as a pair <virtual page number, offset> Virtual address 32-p p Virtual Page # offset Use a page table to translate from virtual to physical page numbers Page table contains the physical page number (i.e., starting physical address) for each virtual page number Page Table Physical Page # offset Physical address L20-8

Physical Memory Private Address Space per Process Proc 1 VA1 Page Table OS pages Proc 2 VA1 Page Table Proc 3 VA1 Page Table Each process has a page table Page table has an entry for each process page free Page tables make it possible to store the pages of a program non-contiguously L20-9

Paging vs. Segmentation Pros of paging vs segmentation? Paging avoids fragmentation issues Paging allows programs that use more virtual memory than the machine s physical memory (demand paging) Cons of paging vs segmentation? Page tables are MUCH larger than base & bound regs! where do we store the page tables? L20-10

Suppose Page Tables reside in memory VPN offset + PT Base Reg PT Proc 1 Physical Page Number Virtual Page Number Translation: PPN = Mem[PT Base + VPN] PA = PPN + offset All links represent physical addresses; no VA to PA translation On process switch Kernel PT Base PT Base Reg := Kernel PT Base + new process ID PT Proc 2 Kernel PT PTB Proc i Accessing one data word or instruction requires two DRAM accesses! L20-11

Paging Implementation Issues How to reduce memory access overhead A good VM design must be fast and space-efficient What if all the pages can t fit in DRAM? What if the process page table can t fit in DRAM? What if the kernel page table can t fit in DRAM? Beyond the scope of 6.004 L20-12

Demand Paging Using main memory as a cache of disk All the pages of the processes may not fit in main memory. Therefore, DRAM is backed up by swap space on disk. Page Table Entry (PTE) contains: A resident bit to indicate if the page exists in main memory PPN (physical page number) for a memory-resident page DPN (disk page number) for a page on the disk Protection and usage bits Even if all pages fit in memory, demand paging allows bringing only what is needed from disk When a process starts, all code and data are on disk; bring pages in as they are accessed PT Base Reg Page Table PPN PPN DPN PPN DPN PPN PPN DPN DPN DPN PPN PPN Virtual address VPN VPN Offset Offset Data Pages Data word L20-13

Example: Virtual Physical Translation 0 1 2 3 4 5 6 7 8 9 A B C D E F 16-entry Page Table 0 0 1 2 -- -- 0 -- 0 0 1 4 -- -- 0 -- 0 0 1 0 1 1 1 1 -- -- 0 -- -- -- 0 -- -- -- 0 -- -- -- 0 -- -- -- 0 -- -- -- 0 -- 1 1 1 7 1 1 1 6 1 1 1 5 0 1 1 3 D W R PPN Dirty Writable Resident 8-page Phys. Mem. VPN 0x4 VPN 0x5 VPN 0x0 VPN 0xF VPN 0x2 VPN 0xE VPN 0xD VPN 0xC 0x000 0x0FC 0x100 0x1FC 0x200 0x2FC 0x300 0x3FC 0x400 0x4FC 0x500 0x5FC 0x600 0x6FC 0x700 0x7FC VPN offset 4 8 VA 3 8 PA PPN Setup: 256 bytes/page (2 8 ) 16 virtual pages (2 4 ) 8 physical pages (2 3 ) 12-bit VA (4 vpn, 8 offset) 11-bit PA (3 ppn, 8 offset) lw 0x2C8(x0) VA = 0x2C8, PA = 0x4C8 VPN = 0x2 PPN = 0x4 L20-14

Caching vs. Demand Paging secondary memory CPU cache primary memory CPU primary memory Caching Demand paging cache entry page frame cache block (~32 bytes) page (~4K bytes) cache miss rate (1% to 20%) page miss rate (<0.001%) cache hit (~1 cycle) page hit (~100 cycles) cache miss (~100 cycles) page miss (~5M cycles) a miss is handled a miss is handled in hardware mostly in software L20-15

Page Faults An access to a page that does not have a valid translation causes a page fault exception. OS page fault handler is invoked, handles miss: Choose a page to replace, write it back if dirty. Mark page as no longer resident Read page from disk into available physical page Update page table to show new page is resident Return control to program, which re-executes memory access Before Page Fault Page Table R=1 CPU R=0 After Page Fault Page Table R=0 CPU R=1 Physical Memory VPN 1 Disk Physical Memory VPN 5 Disk L20-16

Translation Lookaside Buffer (TLB) Problem: Address translation is very expensive! Each reference requires accessing page table Solution: Cache translations in TLB TLB hit TLB miss Single-cycle Translation Page Table Walk to refill V R W D tag PPN virtual address VPN offset (VPN = virtual page number) (PPN = physical page number) fault? hit? physical address PPN offset L20-17

TLB Designs Typically 32-128 entries, 4 to 8-way set-associative Modern processors use a hierarchy of TLBs (e.g., 128-entry L1 TLB + 2K-entry L2 TLB) Switching processes is expensive because TLB has to be flushed Alternatively, include process ID in TLB entries to avoid flushing Handling a TLB miss: Walk the page table. If the page is in memory, load the VPN PPN translation in the TLB. Otherwise, cause a page fault Page faults are always handled in software But page walks are usually handled in hardware using a memory management unit (MMU) RISC-V, x86 access page table in hardware L20-18

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 mem) Where? SEGFAULT L20-19

Example: TLB and Page Table Suppose Virtual memory of 2 32 bytes Physical memory of 2 24 bytes Page size is 2 10 (1 K) bytes 4-entry fully associative TLB Tag TLB Data VPN R D PPN ----+-------- 0 0 0 3 6 1 1 2 1 1 1 9 3 0 0 5 VPN 0 1 2 3 4 5 6 7 8 Page Table R D PPN ------- 0 0 7 1 1 9 1 0 0 0 0 5 1 0 5 0 0 3 1 1 2 1 0 4 1 0 1 1. How many pages can be stored in physical memory at once? 2 14 2. How many entries are there in the page table? 2 22 3. How many bits per entry in the page table? (Assume each entry has PPN, resident bit, dirty bit) 4. How many pages does page table take? 2 23 bytes = 2 13 pages 5. What fraction of virtual memory can be resident? 1/2 8 6. What is the physical address for virtual address 0x1804? What components are involved in the translation? [VPN=6] 0x804 7. Same for 0x1080[VPN=4] 0x1480 8. Same for 0x0FC [VPN=0] page fault L20-20 16

Problem: Size of Linear Page Table With 32-bit addresses, 4 KB pages & 4-byte PTEs: 2 20 PTEs, i.e, 4 MB page table per process We often have hundreds to thousands of processes per machine use GBs of memory just for page tables? Use larger pages? Internal fragmentation (not all memory in a page is used) Larger page fault penalty (more time to read from disk) What about a 64-bit virtual address space? Even 1MB pages would require 2 44 8-byte PTEs (35 TB!) Solution: Use a sparse page table format (size proportional to amount of mapped pages) L20-21

Hierarchical Page Table Virtual Address 31 22 21 12 11 0 p1 p2 offset 10-bit L1 index Root of the Current Page Table 10-bit L2 index p1 p2 offset (Processor Register) Level 1 Page Table page in primary memory page in secondary memory Level 2 Page Tables PTE of a nonexistent page Data Pages L20-22

Summary Virtual memory benefits: Protection and privacy: Private address space per process Demand paging: Can use main memory as a cache of disk Segmentation: Each process address space is a contiguous block (a segment) in physical memory Simple: Base and bound registers Suffers from fragmentation, no demand paging Paging: Each process address space is stored on multiple fixed-size pages. A page table maps virtual to physical pages Avoids fragmentation Enables demand paging: pages can be in main memory or disk Requires a page table access on each memory reference Making paging efficient TLB: Cache the page table Hierarchical page tables: Keep page table size small L20-23

Thank you! Next lecture: I/O and implementation issues of Virtual Memory and Exceptions L20-24