CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 23

Similar documents
CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 23

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 24

Lecture 19: Virtual Memory: Concepts

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

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

Computer Systems. Virtual Memory. Han, Hwansoo

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

Processes and Virtual Memory Concepts

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

Virtual Memory: Concepts

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

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

Systems Programming and Computer Architecture ( ) Timothy Roscoe

Virtual Memory. Samira Khan Apr 27, 2017

CSE 153 Design of Operating Systems

Lecture 21: Virtual Memory. Spring 2018 Jason Tang

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

PROCESS VIRTUAL MEMORY. CS124 Operating Systems Winter , Lecture 18

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

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 27, FALL 2012

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

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

Computer Architecture Lecture 13: Virtual Memory II

Understanding the Design of Virtual Memory. Zhiqiang Lin

Virtual Memory II CSE 351 Spring

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

18-447: Computer Architecture Lecture 18: Virtual Memory III. Yoongu Kim Carnegie Mellon University Spring 2013, 3/1

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

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

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

Virtual Memory Oct. 29, 2002

virtual memory Page 1 CSE 361S Disk Disk

Virtual Memory I. CSE 351 Spring Instructor: Ruth Anderson

Foundations of Computer Systems

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

CS 261 Fall Mike Lam, Professor. Virtual Memory

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

CS 550 Operating Systems Spring Memory Management: Page Replacement

VIRTUAL MEMORY: CONCEPTS

Virtual Memory. Computer Systems Principles

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

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

Virtual Memory: From Address Translation to Demand Paging

Memory System Case Studies Oct. 13, 2008

Virtual Memory. CS 3410 Computer System Organization & Programming

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

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

Pentium/Linux Memory System March 17, 2005

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

CMSC 313 Lecture 13 Project 4 Questions Reminder: Midterm Exam next Thursday 10/16 Virtual Memory

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

Virtual Memory: Concepts

CISC 360. Virtual Memory Dec. 4, 2008

Memory Management. Goals of Memory Management. Mechanism. Policies

VM as a cache for disk

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

CSE 351. Virtual Memory

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

Processes and Tasks What comprises the state of a running program (a process or task)?

Recap: Memory Management

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

Computer Structure. X86 Virtual Memory and TLB

CMSC 313 Lecture 14 Reminder: Midterm Exam next Tues (10/26) Project 4 Questions Virtual Memory on Linux/Pentium platform

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

Virtual Memory: Systems

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2014 Lecture 14

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

Address spaces and memory management

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

Operating Systems. 09. Memory Management Part 1. Paul Krzyzanowski. Rutgers University. Spring 2015

Virtual memory Paging

Administrivia. Lab 1 due Friday 12pm. We give will give short extensions to groups that run into trouble. But us:

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

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

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

Virtual to physical address translation

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

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

Embedded Systems Dr. Santanu Chaudhury Department of Electrical Engineering Indian Institute of Technology, Delhi

SE-292 High Performance Computing. Memory Hierarchy. R. Govindarajan

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

Basic Memory Management

CS399 New Beginnings. Jonathan Walpole

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

Chapter 8 & Chapter 9 Main Memory & Virtual Memory

Paging and Page Replacement Algorithms

Virtual Memory 1. Virtual Memory

Virtual Memory 1. Virtual Memory

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

Virtual Memory. Kevin Webb Swarthmore College March 8, 2018

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

CSE 120 Principles of Operating Systems

Paging. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Computer Architecture. Lecture 8: Virtual Memory

Virtual Memory I. CSE 351 Winter Instructor: Mark Wyse

CSE 560 Computer Systems Architecture

Virtual Memory: From Address Translation to Demand Paging

Memory Management! Goals of this Lecture!

Virtual Memory. CS 351: Systems Programming Michael Saelee

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

Transcription:

CS24: INTRODUCTION TO COMPUTING SYSTEMS Spring 205 Lecture 23

LAST TIME: VIRTUAL MEMORY! Began to focus on how to virtualize memory! Instead of directly addressing physical memory, introduce a level of indirection Programs use virtual addresses, not physical addresses! Divide memory up into pages of size P, P = 2 p Choose a page size much larger than byte or word (makes mapping table smaller; other reasons too)! Map each virtual page to a physical page frame Mapping is specified using a page table Each page table entry maps one virtual page to one physical page 2

LAST TIME: VIRTUAL ADDRESSING (2)! Performing this address translation in software would be horribly slow! CPU provides hardware support for virtual memory and address translation CPU has a Memory Management Unit (MMU) that performs this address translation on the fly The MMU uses a page table to perform translation CPU mov 307, %ebx Virtual Address = 307 MMU Physical Address = 7 Main Memory 0 2 3 4 5 6 7 8 9 0 3

ADDRESS TRANSLATION! Page table is indexed with virtual page number Page table entry contains the physical page number Combine physical page number with virtual page offset to get physical address! Start of page table specified in a control register MMU uses this address to look up page table entries Virtual Address n- p p- 0 Virtual Page Number Virtual Page Offset Base Register Physical Address Physical Page Number Physical Page Number Physical Page Number Physical Page Number m- p p- 0 Physical Page Number Physical Page Offset 4

VIRTUAL MEMORY: ISSUES! This approach already simplifies many things e.g. process isolation, context switches, shared memory! Our virtual memory is still limited: Must share limited main memory amongst all processes, whether running or stopped! We know not all processes are always active e.g. a process can be stopped and resumed e.g. a process may only be using a portion of its instructions and data in memory! Redefine our notion of virtual memory to be an array of N contiguous bytes stored on disk! Main memory becomes a cache for the local disk Main memory is Level 4 (L4) cache, disk is L5 memory 5

VIRTUAL MEMORY AS A CACHE! Apply same concepts as hardware caches: update page table entries to track more details Is a page of memory actually allocated for a specific virtual page number? Is the virtual page cached in DRAM main memory? (similar to extra details stored in cache lines)! Now, the memory available to all processes can be much larger than actual size of main memory Use our caching techniques to get a virtual memory much larger than main memory, but at [close to] DRAM speeds 6

VIRTUAL MEMORY AS A CACHE (2)! DRAM is cache for virtual memory stored on disk DRAM access latency is 50-00ns Disk access latency is 8-30ms (!!!) A virtual memory cache-miss is very expensive! However, disk throughput is 50+ MB/second Reading the first byte of data from disk is very slow Reading second and subsequent bytes is much faster! Example: transfer unit of byte Cache-miss penalty is 8-30ms! How about a transfer unit of 00KB?! Cache-miss penalty is 8-30ms + 2ms Not that much more expensive to retrieve! 7

VIRTUAL MEMORY: STRATEGIES! Choose a large page size (e.g. 4KB or 8KB), to compensate for large disk access latency Retrieving additional bytes doesn t add much time to the cost of a cache miss Using a much larger transfer-unit size will reduce the number of cache misses we have in the first place! Use sophisticated replacement policies in DRAM cache to avoid cache misses as much as possible! Example: DRAM cache is fully associative! Use write-back policy instead of write-through, when a program writes to a cached DRAM page Don t write a modified page to disk until it is actually evicted from main memory (One reason why OS needs to be shut down cleanly) 8

PAGE TABLE ENTRIES! Page table entries must be enhanced to support this virtual memory model! Include a valid bit, along with each address! valid = means page is cached in DRAM memory Corresponding address is the start of the page in DRAM physical memory! valid = 0 means the page is not cached in DRAM If stored address is 0, the virtual page is not allocated If address is not 0, the virtual page is allocated, and the address is the page s location on disk! IA32 page-table entries call this the present bit, i.e. Is the page present in main memory? 9

PROCESSOR AND OPERATING SYSTEM! A very important detail about virtual memory facility:! The processor provides some hardware support for virtual memory Address translation is performed in hardware, using the Memory Management Unit MMU must use a page table to perform address translation! The processor cannot completely support virtual memory in hardware, on its own! What to do when a virtual page is not in main memory? CPU has no idea how virtual memory pages are cached.! The operating system must also provide support for many virtual memory operations CPU does as much as it can on its own CPU invokes an exception when it needs the kernel s help! 0

VIRTUAL MEMORY EXAMPLE! Example: small virtual memory 4 physical page frames (PP) 8 virtual pages (VP)! Some virtual pages are cached in DRAM: VP, VP2, VP4, VP7! Some virtual pages are only stored on disk VP3, VP6! Two virtual pages have not been allocated VP0, VP5 PTE0 PTE PTE2 PTE3 PTE4 PTE5 PTE6 PTE7 Valid Address 0 null 0 0 null 0 Physical Memory (DRAM) VP PP0 VP2 PP VP7 PP2 VP4 PP3 Virtual Memory (disk) VP VP2 VP3 VP4 VP6 VP7

VIRTUAL MEMORY ACCESSES! Program accesses a word in Virtual Page 2 MMU looks in page table for VP2 (PTE2) Uses virtual page number as index into! Virtual page 2 is stored in physical page Valid flag is : page is cached in DRAM MMU sends physical address to DRAM main memory, and retrieves the data value PTE0 PTE PTE2 PTE3 PTE4 PTE5 PTE6 PTE7 Valid Address 0 null 0 0 null 0 Physical Memory (DRAM) VP VP2 VP7 VP4 Virtual Memory (disk) VP VP2 VP3 VP4 VP6 VP7 PP0 PP PP2 PP3 2

VIRTUAL MEMORY ACCESSES (2)! Now, program accesses a word in Virtual Page 6 MMU looks in page table for VP6, but the valid flag is 0! Virtual page isn t cached in main memory, so MMU cannot satisfy the request! CPU causes a page fault exception It s up to the kernel to resolve the problem! Kernel provides a handler for resolving page faults! The kernel manages the page table, memory pages Processor simply provides support for virtual memory PTE0 PTE PTE2 PTE3 PTE4 PTE5 PTE6 PTE7 Valid Address 0 null 0 0 null 0 Physical Memory (DRAM) VP VP2 VP7 VP4 Virtual Memory (disk) VP VP2 VP3 VP4 VP6 VP7 PP0 PP PP2 PP3 3

VIRTUAL MEMORY ACCESSES (3)! CPU generates a page fault, and kernel s handler is invoked! Page-table entry for virtual page 6 has a non-null address The kernel can load VP6 from disk into memory Valid Address! but first, some other PTE0 0 null page must be evicted! PTE! Kernel selects VP4 as PTE2 the victim page PTE3 0 If VP4 is changed, must PTE4 0 also write it back to disk PTE5 0 null! Kernel updates PTE4 to PTE6 0 show page isn t cached PTE7 Physical Memory (DRAM) VP VP2 VP7 VP4 Virtual Memory (disk) VP VP2 VP3 VP4 VP6 VP7 PP0 PP PP2 PP3 save to 4 disk

VIRTUAL MEMORY ACCESSES (4)! Now kernel loads virtual page 6 into physical page 3 Update PTE6 to point to physical page 3 in DRAM memory! Finally, kernel returns from the page-fault handler! Since this is a fault: CPU resumes executing Valid Address the instruction that PTE0 0 null caused the fault PTE! Program repeats the PTE2 access to virtual page 6 PTE3 0 This time, MMU finds PTE4 0 that PTE6 is valid PTE5 0 null MMU performs address PTE6 translation, and retrieves value from physical page 3 PTE7 Physical Memory (DRAM) VP VP2 VP7 VP6 Virtual Memory (disk) VP VP2 VP3 VP4 VP6 VP7 PP0 PP PP2 PP3 5 load from disk

ALLOCATING VIRTUAL MEMORY! If a process needs more memory, kernel can allocate another virtual memory page! Example: allocate a page for VP5 Make room on the disk for virtual page 5 Update PTE5 to point to VP5 on disk (Note: no requirement that pages on disk be kept in order)! When program begins using VP5, it can be swapped into physical memory PTE0 PTE PTE2 PTE3 PTE4 PTE5 PTE6 PTE7 Valid Address 0 null 0 0 0 Physical Memory (DRAM) VP VP2 VP7 VP6 Virtual Memory (disk) VP VP2 VP3 VP4 VP7 PP0 PP PP2 PP3 VP5 6 VP6

VIRTUAL MEMORY AS A CACHE! DRAM main memory is a cache to virtual memory stored on disk! Caching terminology is slightly different Virtual memory was invented in 960s, before SRAM caches were needed between the CPU and DRAM! Caches have blocks, but virtual memory has pages (virtual blocks) and frames (physical blocks) Caches have cache sets and cache lines, but virtual memory has page tables and page-table entries Caches have cache-hits and cache-misses, but virtual memory has page-hits and page-faults! Nearly identical concepts, but terminology is slightly different 7

VIRTUAL MEMORY AS A CACHE (2)! Like caches, virtual memory depends on locality When programs exhibit good locality, we can get a virtual memory much larger than physical memory, but with same performance as physical memory! Processes typically reference a specific set of memory pages as they run Called the working set of the process, or the resident set, since these pages are kept in memory! If a program has good locality: The working set of pages will usually fit entirely within memory, yielding good performance 8

VIRTUAL MEMORY AS A CACHE (3)! If a program s working set doesn t fit within memory, thrashing will occur Pages are constantly swapped in and out of main memory The program runs very slowly, since many accesses incur the 8-30ms disk latency penalty! If a program runs very slowly, and you see the disk is constantly being accessed, it is probably thrashing "! UNIX provides a getrusage() function get resource usage Reports several details about virtual memory system, such as the number of page faults Also reports number of voluntary and involuntary contextswitches, number of signals received, etc. Can monitor how well-behaved your program is! 9

VIRTUAL MEMORY AND DISK FILES! Once virtual memory is disk-based, we can provide some new features! Loading programs and libraries into memory: Programs and libraries are stored as binary files containing instructions and data! When we want to start a new program: Kernel allocates a contiguous set of virtual pages large enough to hold the program s code and data Then, the kernel updates the process page table, pointing these new pages to the binary file stored on disk The new page-table entries are marked as valid = 0! i.e. the pages are not yet cached in DRAM memory! As the program runs, virtual memory system loads program s code and data into memory automatically 20

VIRTUAL MEMORY AND DISK FILES (2)! This technique is called memory mapping! Can map a disk file into memory with the UNIX mmap() function void * mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset) Maps a specific portion of the file fd into memory Portion starts at offset and contains length bytes! Function works by allocating virtual memory pages, and pointing page table entries to disk file! Instead of manually performing file I/O, simply perform reads and writes against a memory area Virtual memory system automatically loads and saves data to the file stored on disk 2

PAGES AND PERMISSIONS! We can use the virtual memory system to load programs and read-only data into memory Really don t want to let programs modify their code or read-only data. Or shared libraries, etc.! Add permission bits to each page table entry, that control read/write access, etc.! IA32 has two permission bits: Read/Write flag controls whether code can write to the page, or only read from it User/Supervisor flag controls whether code must have a higher privilege level to access the page valid R/W SUP valid R/W SUP valid valid valid R/W R/W R/W SUP SUP SUP Physical Page Number Physical Page Number Physical Page Number Physical Page Number 22

PAGES AND PERMISSIONS (2) Process Virtual Memory! Kernel uses these virtual memory features to load and run programs Load program and shared library code from disk automatically Share kernel and library code across multiple processes Isolate address space of processes! If code tries to access a page in a way disallowed by permissions, CPU raises a general protection fault! The kernel has a handler for general protection faults Typical response: terminate the process! Process Data Structures Kernel Stack Kernel Code Kernel Data Shared Libraries Process Stack Process Heap Program Code Program Data Supervisor access only! Kernel code and data is mapped into all processes. Read-only! File-backed. Mapped into multiple processes as needed. Read/write. Local to this process only. Code and some data are read- 23 only and filebacked.

IMPLEMENTATION ISSUES! Glossed over a couple of implementation issues! Issue : Where do page tables live? Showed page tables as separate from main memory CPU mov 307, %ebx Virtual Address = 307 MMU Physical Address = 7 Main Memory 0 2 3 4 5 6 7 8 9 0! In reality, page tables also live in main memory! Why would this be a problem? Hint: How long does it take to access main memory? 24

IMPLEMENTATION ISSUES (2)! If page tables live in main memory, address translation would frequently incur DRAM access overhead of 50-00ns! If you re lucky, part of page table is in L or L2 cache! CPUs frequently use a Translation Lookaside Buffer (TLB) to cache page table entries Don t even want to incur L or L2 penalty, if possible! CPU mov 307, %ebx TLB Main Memory 0 2 3 Virtual Address = 307 MMU Physical Address = 7 4 5 6 7 8 9 0 PTE 25

TRANSLATION LOOKASIDE BUFFER! Translation lookaside buffer stores one page table entry per cache line Input is virtual page number, output is page table entry! TLB often has a high degree of associativity Maximize chance that TLB contains needed page table entry! CPU mov 307, %ebx TLB VPN PTE Main Memory 0 2 3 Virtual Address = 307 MMU Physical Address = 7 4 5 6 7 8 9 0 PTE 26

IMPLEMENTATION ISSUES (3)! Issue 2: How large are page tables? Showed the processor using a single page table Real-world system doesn t work that way!! Given a 32-bit address space, and pages of 4KB: Page offsets take 2 bits, page numbers are 20 bits! To map each virtual page to a physical page, how many page table entries? 2 20 =,048,576! If each page table entry is 32 bits, how big is the entire page table? Page table for this address space would be 4MB!! Every process would require a 4MB page table Even bigger trouble for larger address spaces! 27

MULTI-LEVEL PAGE TABLES! Instead of a single page table, introduce a hierarchy of page tables n- Virtual Address p p- 0 VPN VPN 2 VPN k Virtual Page Offset PTBR Level Level 2 Level k m- Physical Address p p- 0 Physical Page Number Physical Page Offset! Idea: typically, most of a process virtual address-space will be unallocated Hierarchy of page tables will be relatively sparse 28

MULTI-LEVEL PAGE TABLES (2)! Instead of a single page table, introduce a hierarchy of page tables n- Virtual Address p p- 0 VPN VPN 2 VPN k Virtual Page Offset PTBR Level Level 2 Level k m- Physical Address p p- 0 Physical Page Number Physical Page Offset! If an entire page table at level i is empty: Don t allocate a page for the empty page-table Corresponding table entry at level i is null 29

IA32 VIRTUAL MEMORY SUPPORT! Intel Pentium-family processors provide hardware support for virtual memory! Virtual and physical address spaces are 32 bits! Pages are 4KB in size (2 2 = 4096) Pages are identified by topmost 20 bits in address! Pentium-family processors implement a two-level page table hierarchy! Level is the page directory Entries in the page directory refer to page tables, as long as the page table is not empty! Level 2 contains page tables Entries map virtual pages to physical pages 30

IA32 VIRTUAL MEMORY SUPPORT (2)! IA32 page-table hierarchy: Top 0 bits of virtual address is the page directory entry Next 0 bits is used for page table entry Bottom 2 bits used for the virtual page offset Virtual Address 3 22 2 2 0 Page Directory Entry Entry Virtual Page Offset Page Directory Base Register Page Directory Page Table Physical Address 3 2 0 Physical Page Number Physical Page Offset 3

MULTI-LEVEL PAGE TABLE EXAMPLE! Program with this memory layout: Bottom 2K pages allocated to program code and data (4KB page size 2K = 8MB program and data size) 6K pages left unallocated, for program heap to use 023 unallocated pages, plus page allocated to program stack! Structure: PDBR Page Directory PDE0 PDE PDE2 PDE7 PDE8 PDE9 null null null null null PTE0 PTE023 PTE0 PTE023 PTE022 PTE023 null null VP 0 VP 023 VP 024 VP 2047 6K pages (unallocated) 023 pages (unallocated) VP 925 8MB allocated to program code and data Program stack

ASIDE: CORE I7 AND PAGE TABLES! Intel Core i7 implements a four-level hierarchy 48 bit address space again, page tables would be too large if not broken into a hierarchy Preserves the 4KB page size of IA32! Core i7 page-table hierarchy level names: Level = page global directory Level 2 = page upper directory Level 3 = page middle directory Level 4 = page table! Each level uses 9 bits of virtual address Core i7 page table entries are 64 bits Page tables still work out to be 4KB in size! Virtual page offset is bottommost 2 bits! See CS:APP2e 9.7. for more details on Core i7 33

NEXT TIME! Continue discussion of IA32 virtual memory More hardware implementation details How Linux uses IA32 virtual memory facilities 34