Lecture 19: Virtual Memory: Concepts

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

Computer Systems. Virtual Memory. Han, Hwansoo

Processes and Virtual Memory Concepts

CSE 153 Design of Operating Systems

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

Virtual Memory: Concepts

Virtual Memory: Concepts

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

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

Virtual Memory: Concepts

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

Understanding the Design of Virtual Memory. Zhiqiang Lin

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

Systems Programming and Computer Architecture ( ) Timothy Roscoe

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

VM as a cache for disk

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

Virtual Memory II CSE 351 Spring

Virtual Memory II. CSE 351 Autumn Instructor: Justin Hsia

Virtual Memory II. CSE 351 Autumn Instructor: Justin Hsia

Virtual Memory. Computer Systems Principles

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

University of Washington Virtual memory (VM)

VIRTUAL MEMORY: CONCEPTS

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

Foundations of Computer Systems

CSE 351. Virtual Memory

Virtual Memory. Samira Khan Apr 27, 2017

Virtual Memory I. CSE 351 Spring Instructor: Ruth Anderson

Virtual Memory Oct. 29, 2002

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 23

Virtual Memory Nov 9, 2009"

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 23

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

CS 153 Design of Operating Systems

CISC 360. Virtual Memory Dec. 4, 2008

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 24

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

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

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

virtual memory Page 1 CSE 361S Disk Disk

Virtual Memory I. CSE 351 Winter Instructor: Mark Wyse

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. CS 3410 Computer System Organization & Programming

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

CSE 560 Computer Systems Architecture

Virtual Memory: Systems

CS 261 Fall Mike Lam, Professor. Virtual Memory

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

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

CS 153 Design of Operating Systems Winter 2016

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

18-447: Computer Architecture Lecture 16: Virtual Memory

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

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

Address Translation. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

P6/Linux Memory System Nov 11, 2009"

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

CSE 120 Principles of Operating Systems

Virtual Memory: Systems

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

Memory Hierarchy Requirements. Three Advantages of Virtual Memory

Memory Management! Goals of this Lecture!

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

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

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

CS 153 Design of Operating Systems

VIRTUAL MEMORY II. Jo, Heeseung

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

Memory System Case Studies Oct. 13, 2008

Computer Architecture. Lecture 8: Virtual Memory

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

University*of*Washington*

Pentium/Linux Memory System March 17, 2005

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

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

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

UC Berkeley CS61C : Machine Structures

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

CSE 120. Translation Lookaside Buffer (TLB) Implemented in Hardware. July 18, Day 5 Memory. Instructor: Neil Rhodes. Software TLB Management

Virtual Memory. CS 351: Systems Programming Michael Saelee

The memory of a program. Paging and Virtual Memory. Swapping. Paging. Physical to logical address mapping. Memory management: Review

Handout 4 Memory Hierarchy

CS162 Operating Systems and Systems Programming Lecture 14. Caching (Finished), Demand Paging

Virtual Memory: From Address Translation to Demand Paging

Chapter 8. Virtual Memory

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

John Wawrzynek & Nick Weaver

Virtual Memory 3. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. P & H Chapter 5.4

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

CSE 120 Principles of Operating Systems Spring 2017

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

CS 318 Principles of Operating Systems

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

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

Lecture 21: Virtual Memory. Spring 2018 Jason Tang

Memory Management. Goals of Memory Management. Mechanism. Policies

Lecture 15: Virtual Memory

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 27, SPRING 2013

Transcription:

CSCI-UA.2-3 Computer Systems Organization Lecture 9: Virtual Memory: Concepts Mohamed Zahran (aka Z) mzahran@cs.nyu.edu http://www.mzahran.com Some slides adapted (and slightly modified) from: Clark Barrett Jinyang Li Randy Bryant Dave O Hallaron

Physical Addressing CPU Physical address (PA) 4 Main memory : : 2: 3: 4: 5: 6: 7: 8:... M-: Data word Used by microcontrollers like those in Arduino, cars etc. Simple but fragile to program for: Buffer overrun in buflab corrupts Firefox memory!

Butler Lampson, co-inventor of PC All problems in CS can be solved by another level of indirection

Virtual Addressing CPU Chip CPU Virtual address (VA) 4 MMU Physical address (PA) 4 Main memory : : 2: 3: 4: 5: 6: 7: 8:... M-: Data word Used in all modern servers, desktops, and laptops

Why Virtual Memory (VM)? Simplified memory management Each process gets an ``exclusive linear address space Process Isolation Different processes have different virtual address spaces One process can t interfere with another s memory Uses main memory efficiently Use DRAM as a cache for the parts of a virtual address space

Address translation Key idea of VM: each process has its own virtual address space Strawman view of addr translation VA=x8 VA.. PA PA=x98 x8 x98 Valid address: [,2^32) for 32-bit machine [,2^64) for 64-bit machine.......... invalid Valid address: [,2^m) depending on how much memory your machine has Granularity of mapping? Byte-level: Map each byte in VA to a byte in PA Page-level: Map each consecutive 2^p-byte address range in VA to a 2^p-byte address range in PA

Address Translation w/ a Page Page table base register (PTBR) How kernel tells h/w where to find the page table Table Virtual address n- Virtual page number (VPN) p p- Virtual page offset (VPO) Page table address for process Page table Valid Physical page number (PPN) PTE (page table entry) Valid bit = : page not in memory (page fault) m- p p- Physical page number (PPN) Physical page offset (PPO) Physical address

Page Table Base Register (PTBR) The operating system maintains information about each process in a process control block. The page table base address for the process is stored there. The operating system loads this address into the PTBR whenever a process is scheduled for execution. Only the kernel can access PTBR

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

Address Translation: Page Fault Exception 4 Page fault handler CPU Chip 2 PTEA CPU VA 7 MMU PTE 3 Cache/ Memory ) 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 exception in kernel If VA is invalid, then kill process (SIGSEGV) If VA has been paged out to disk, then swaps in faulted page, update page table, resume faulted process

Speeding up Translation with a TLB VA->PA translation can be expensive One additional memory reference for every normal memory reference! Not too bad? Page table entries (PTEs) are cached in L like others PTEs may be evicted by other data references PTE hit still requires a small L delay Solution: 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

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

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 additional memory access (the PTE) Fortunately, TLB misses are rare. Why?

Reduce Page Table Size 4KB-page, 48-bit address space, 8-byte PTE Size of page table needed? 2 48-2 * 2 3 = 2 39 = 52 GB Wasteful: most PTEs are invalid Solution: multi-level page table Example: 2-level page table Level table: each PTE points to a page table Level 2 table: each PTE points to a page Level Table... Level 2 Tables...

A Two-Level Page Table Hierarchy Level page table Level 2 page tables Virtual memory VP PTE PTE PTE 2 (null) PTE 3 (null) PTE 4 (null) PTE... PTE 23 PTE... VP 23 VP 24... VP 247 2K allocated VM pages for code and data PTE 5 (null)... PTE 6 (null) PTE 7 (null) PTE 23 Gap 6K unallocated VM pages PTE 8 (K - 9) null PTEs 23 null PTEs 32 bit addresses, 4KB pages, 4-byte PTEs PTE 23 23 unallocated pages VP 925... 23 unallocated pages allocated VM page for the stack

Why Two-level Page Table Reduces Memory Requirement? if a PTE in the level table is null, then the corresponding level 2 page table does not even have to exist. Only the level table needs to be in main memory at all times. The level 2 page tables can be created and paged in and out by the VM system as they are needed.

VM For memory management and protection For caching

Memory management and protection Each process has an exclusive VA space One process cannot overwrite another one s memory! Sharing among processes Map different virtual pages to the same physical page Virtual Address Space for Process : VP VP 2... PP 2 Physical Address Space (DRAM) PP 6 (e.g., libc code, kernel) Virtual Address Space for Process 2: VP VP 2... PP 8...

Simplified Linking and Loading Linking Each program has similar virtual address space Code, stack, and shared libraries always start at the same address Loading execve()causes kernel to allocate virtual pages Kernel copies.text and.data sections, page by page, from disk to memory xc x4 x848 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 %esp (stack pointer) Loaded from the executable file

Memory Protection How to protect shared pages from corruption? E.g. bad process overwrites shared kernel code/data, shared libc code etc. Extend PTEs with permission bits Process i: Process j: VP : VP : VP 2: SUP No No Yes READ Yes Yes Yes WRITE No Yes Yes Address PP 6 PP 4 PP 2 SUP: whether processes must be running in kernel (supervisor) mode to access the page. VP : VP : VP 2: SUP No Yes No READ Yes Yes Yes WRITE No Yes Yes Address PP 6 PP 9 PP Physical Address Space PP 2 PP 4 PP 6 PP 8 PP 9 PP

VM as a Tool for Caching Not all processes valid VA pages fit in physical memory Key idea: treat DRAM-resident pages as a cache of on-disk pages Virtual memory Physical memory VP VP Unallocated Cached Uncached Unallocated Empty Empty PP PP Cached Uncached Empty Cached Uncached Virtual pages (VPs) stored on disk Physical pages (PPs) cached in DRAM

VM for Caching General mechanism: On page fault, load corresponding on-disk page to memory, evict a previously memory-resident to disk, set appropriate PTE entry Which entity should be performing this task? User-level process? OS? Hardware? VM Caching policy (more sophisticated than CPU cache) Fully associative: any VP can be mapped to any PP Write-back

Page Hit Page hit: reference to VM word that is in physical memory (DRAM cache hit) Virtual address Valid PTE PTE 7 Physical page number or disk address null null Memory resident page table (DRAM) Physical memory (DRAM) VP VP 2 VP 7 VP 4 Virtual memory (disk) VP VP 2 VP 3 VP 4 PP PP 3 VP 6 VP 7

Page Fault Page fault: reference to VM word that is not in physical memory (DRAM cache miss) Virtual address Valid PTE PTE 7 Physical page number or disk address null null Memory resident page table (DRAM) Kernel figures out where to find the corresponding on-disk page Physical memory (DRAM) VP VP 2 VP 7 VP 4 Virtual memory (disk) VP VP 2 VP 3 VP 4 VP 6 VP 7 PP PP 3

Handling Page Fault Page miss causes page fault (an exception) Virtual address Valid PTE PTE 7 Physical page number or disk address null null Memory resident page table (DRAM) Physical memory (DRAM) VP VP 2 VP 7 VP 4 Virtual memory (disk) VP VP 2 VP 3 VP 4 PP PP 3 VP 6 VP 7

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

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

Handling Page Fault Page miss causes page fault (an exception) Page fault handler selects a victim to be evicted (here VP 4) Offending instruction is restarted: page hit! Virtual address Valid PTE PTE 7 Physical page number or disk address null null Memory resident page table (DRAM) Physical memory (DRAM) VP VP 2 VP 7 VP 3 Virtual memory (disk) VP VP 2 VP 3 VP 4 PP PP 3 VP 6 VP 7

Why should VM caching work? Locality! At any point in time, programs tend to access a set of active virtual pages called the working set Programs with better temporal locality will have smaller working sets If (working set size < main memory size) Good performance for one process after compulsory misses If (working set sizes > main memory size ) Thrashing: Performance meltdown where pages are swapped (copied) in and out continuously

Conclusions 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 Simplifies memory management & protection Uses memory efficiently by caching virtual memory pages