Virtual Memory II CSE 410 Winter 2017

Slides:



Advertisements
Similar presentations
Virtual Memory October 25, 2006 Topics Address spaces Motivations for virtual memory Address translation Accelerating translation with TLBs class16.ppt.
Advertisements

Carnegie Mellon 1 Virtual Memory: Concepts : Introduction to Computer Systems 15 th Lecture, Oct. 14, 2010 Instructors: Randy Bryant and Dave O’Hallaron.
Today Virtual memory (VM) Overview and motivation
Virtual Memory October 29, 2007 Topics Address spaces Motivations for virtual memory Address translation Accelerating translation with TLBs class16.ppt.
Carnegie Mellon 1 Virtual Memory: Concepts / : Introduction to Computer Systems 16 th Lecture, Oct. 21, 2014 Instructors: Greg Ganger, Greg.
Virtual Memory: Concepts
Carnegie Mellon 1 Saint Louis University Virtual Memory CSCI 224 / ECE 317: Computer Architecture Instructor: Prof. Jason Fritts Slides adapted from Bryant.
Carnegie Mellon /18-243: Introduction to Computer Systems Instructors: Bill Nace and Gregory Kesden (c) All Rights Reserved. All work.
1 Seoul National University Virtual Memory: Systems.
1 Virtual Memory. 2 Outline Pentium/Linux Memory System Core i7 Suggested reading: 9.6, 9.7.
1 Virtual Memory: Concepts Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
Carnegie Mellon 1 Virtual Memory: Concepts Instructor: Rabi Mahapatra (TAMU) Slides: Randy Bryant and Dave O’Hallaron (CMU)
Virtual Memory Part 1 Li-Shiuan Peh Computer Science & Artificial Intelligence Lab. Massachusetts Institute of Technology May 2, 2012L22-1
Processes and Virtual Memory
Virtual Memory.  Next in memory hierarchy  Motivations:  to remove programming burdens of a small, limited amount of main memory  to allow efficient.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Virtual Memory: Concepts Slides adapted from Bryant.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Virtual Memory: Concepts CENG331 - Computer Organization.
Virtual Memory Topics Motivations for VM Address translation Accelerating translation with TLBs CS 105 “Tour of the Black Holes of Computing!”
Virtual Memory Topics Address spaces Motivations for virtual memory Address translation Accelerating translation with TLBs.
CSE 153 Design of Operating Systems Winter 2015 Lecture 11: Paging/Virtual Memory Some slides modified from originals by Dave O’hallaron.
Virtual Memory 1 Computer Organization II © McQuain Virtual Memory Use main memory as a “cache” for secondary (disk) storage – Managed jointly.
University of Washington Indirection in Virtual Memory 1 Each process gets its own private virtual address space Solves the previous problems Physical.
1 Virtual Memory. 2 Outline Case analysis –Pentium/Linux Memory System –Core i7 Suggested reading: 9.7.
CS161 – Design and Architecture of Computer
Virtual Memory Samira Khan Apr 27, 2017.
Virtual Memory Samira Khan Apr 25, 2017.
CS161 – Design and Architecture of Computer
Section 9: Virtual Memory (VM)
From Address Translation to Demand Paging
Virtual Memory: Systems
Section 9: Virtual Memory (VM)
From Address Translation to Demand Paging
Today How was the midterm review? Lab4 due today.
Virtual Memory: Concepts CENG331 - Computer Organization
Virtual Memory III CSE 351 Spring 2017
Virtual Memory.
CS 105 “Tour of the Black Holes of Computing!”
CSE 153 Design of Operating Systems Winter 2018
Virtual Memory: Systems /18-213/14-513/15-513: Introduction to Computer Systems 18th Lecture, October 25, 2018.
CS 105 “Tour of the Black Holes of Computing!”
Virtual Memory II CSE 351 Autumn 2016
CSE 153 Design of Operating Systems Winter 2018
Virtual Memory: Concepts /18-213/14-513/15-513: Introduction to Computer Systems 17th Lecture, October 23, 2018.
Virtual Memory: Systems
From Address Translation to Demand Paging
Virtual Memory: Systems
CS 105 “Tour of the Black Holes of Computing!”
Virtual Memory S04, Recitation, Section A
Pentium/Linux Memory System
Virtual Memory CSCI 380: Operating Systems Lecture #7 -- Review and Lab Suggestions William Killian.
Instructors: Majd Sakr and Khaled Harras
Pentium III / Linux Memory System April 4, 2000
Morgan Kaufmann Publishers Memory Hierarchy: Virtual Memory
Virtual Memory Nov 27, 2007 Slide Source:
Instructor: Phil Gibbons
CSE 451: Operating Systems Autumn 2005 Memory Management
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
Virtual Memory II CSE 351 Winter 2018
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CS 105 “Tour of the Black Holes of Computing!”
CSCI 380: Operating Systems William Killian
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
CS 105 “Tour of the Black Holes of Computing!”
Virtual Memory I CSE 351 Winter 2019
CSE 153 Design of Operating Systems Winter 2019
CSE 153 Design of Operating Systems Winter 2019
Virtual Memory Lecture notes from MKP and S. Yalamanchili.
Virtual Memory Use main memory as a “cache” for secondary (disk) storage Managed jointly by CPU hardware and the operating system (OS) Programs share main.
Review What are the advantages/disadvantages of pages versus segments?
Instructor: Phil Gibbons
Presentation transcript:

Virtual Memory II CSE 410 Winter 2017 Instructor: Teaching Assistants: Justin Hsia Kathryn Chan, Kevin Bi, Ryan Wong, Waylon Huang, Xinyu Sui Why Boston Dynamics' New Robot Scares the Crap out of Us The latest video by robot developer Boston Dynamics, which seems to always coincide with a rise of Terminator references, has elicited a wide range of strong emotional reactions. The new robot, Handle (because it will be handling objects), brings forth a sense of awe for its speed, agility, strength, and ability to jump. At the same time, the machine's impressiveness brings forward deep-seated fears of robots-gone-wild. http://bigthink.com/david-ryan-polgar/why-boston-dynamics-new-robot-scares-the- crap-out-of-us

Administrivia Lab 4 due Tuesday (3/7) Homework 5 due Thursday (3/9) Final Exam: Tuesday, March 14 @ 2:30pm (MGH 241) Review Session: Sunday, March 12 @ 1:30pm in SAV 264 Cumulative (midterm clobber policy applies) TWO double-sided handwritten 8.5×11” cheat sheets Recommended that you reuse or remake your midterm cheat sheet

Virtual Memory (VM) Overview and motivation VM as a tool for caching Address translation VM as a tool for memory management VM as a tool for memory protection

Review: Terminology Context switch Page in Page out Thrashing Switch between processes on the same CPU Page in Move pages of virtual memory from disk to physical memory Page out Move pages of virtual memory from physical memory to disk Thrashing Total working set size of processes is larger than physical memory and causes excessive paging in and out instead of doing useful computation

VM for Managing Multiple Processes Key abstraction: each process has its own virtual address space It can view memory as a simple linear array With virtual memory, this simple linear virtual address space need not be contiguous in physical memory Process needs to store data in another VP? Just map it to any PP! Virtual Address Space for Process 1: Physical Address Space (DRAM) N-1 (e.g., read-only library code) Virtual Address Space for Process 2: VP 1 VP 2 ... PP 2 PP 6 PP 8 M-1 translation

Simplifying Linking and Loading Kernel virtual memory Memory-mapped region for shared libraries Run-time heap (created by malloc) User stack (created at runtime) Unused %rsp (stack pointer) Memory invisible to user code brk 0x400000 Read/write segment (.data, .bss) Read-only segment (.init, .text, .rodata) Loaded from the executable file Linking Each program has similar virtual address space Code, Data, and Heap always start at the same addresses Loading execve allocates virtual pages for .text and .data sections & creates PTEs marked as invalid The .text and .data sections are copied, page by page, on demand by the virtual memory system

VM for Protection and Sharing The mapping of VPs to PPs provides a simple mechanism to protect memory and to share memory between processes Sharing: map virtual pages in separate address spaces to the same physical page (here: PP 6) Protection: process can’t access physical pages to which none of its virtual pages are mapped (here: Process 2 can’t access PP 2) Virtual Address Space for Process 1: Physical Address Space (DRAM) N-1 (e.g., read-only library code) Virtual Address Space for Process 2: VP 1 VP 2 ... PP 2 PP 6 PP 8 M-1 translation

Memory Protection Within Process VM implements read/write/execute permissions Extend page table entries with permission bits MMU checks these permission bits on every memory access If violated, raises exception and OS sends SIGSEGV signal to process (segmentation fault) Why no Dirty bit? Typically handled by OS. Physical Address Space PP 2 PP 4 PP 6 PP 8 PP 9 PP 11 Process i: PPN WRITE EXEC PP 6 No PP 4 Yes PP 2 READ VP 0: VP 1: VP 2: Valid • Process j: WRITE EXEC PP 9 Yes No PP 6 PP 11 READ VP 0: VP 1: VP 2: Valid PPN

Address Translation: Page Hit Cache/ Memory 2 CPU Chip PTEA MMU 1 PTE CPU VA 3 PA 4 Data 5 1) Processor sends virtual address to MMU (memory management unit) 2-3) MMU fetches PTE from page table in cache/memory (Uses PTBR to find beginning of page table for current process) 4) MMU sends physical address to cache/memory requesting data 5) Cache/memory sends data to processor VA = Virtual Address PTEA = Page Table Entry Address PTE= Page Table Entry PA = Physical Address Data = Contents of memory stored at VA originally requested by CPU

Address Translation: Page Fault Exception Page fault handler 4 2 CPU Chip Cache/ Memory Disk PTEA Victim page MMU 1 5 CPU VA PTE 7 3 New page 6 1) Processor sends virtual address to MMU 2-3) MMU fetches PTE from page table in cache/memory 4) Valid bit is zero, so MMU triggers page fault exception 5) Handler identifies victim (and, if dirty, pages it out to disk) 6) Handler pages in new page and updates PTE in memory 7) Handler returns to original process, restarting faulting instruction

Hmm… Translation Sounds Slow The MMU accesses memory twice: once to get the PTE for translation, and then again for the actual memory request The PTEs may be cached in L1 like any other memory word But they may be evicted by other data references And a hit in the L1 cache still requires 1-3 cycles What can we do to make this faster? Solution: add another cache! 🎉

Speeding up Translation with a TLB 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 Modern Intel processors have 128 or 256 entries in TLB Much faster than a page table lookup in cache/memory “The architecture of the IBM System/370” R.P. Case and A. Padegs Communications of the ACM. 21:1, 73-96, January 1978. Perhaps the first paper to use the term translation lookaside buffer. The name arises from the historical name for a cache, which was a lookaside buffer as called by those developing the Atlas system at the University of Manchester; a cache of address translations thus became a translation lookaside buffer. Even though the term lookaside buffer fell out of favor, TLB seems to have stuck, for whatever reason. “x86info -c” on Intel Core2 Duo CPU: L1 Data TLB: 4KB pages, 4-way set associative, 16 entries Data TLB: 4K pages, 4-way associative, 256 entries. L1 Data TLB: 4MB pages, 4-way set associative, 16 entries Data TLB: 4MB pages, 4-way associative, 32 entries TLB PTE VPN →

TLB Hit A TLB hit eliminates a memory access! CPU Chip TLB Cache/ MMU PA Data CPU VA CPU Chip PTE 1 2 4 5 TLB VPN 3 → A TLB hit eliminates a memory access!

TLB Miss A TLB miss incurs an additional memory access (the PTE) MMU Cache/ Memory PA Data CPU VA CPU Chip PTE 1 2 5 6 TLB VPN 4 PTEA 3 → Does a TLB miss require disk access? A TLB miss incurs an additional memory access (the PTE) Fortunately, TLB misses are rare

Fetching Data on a Memory Read Check TLB Input: VPN, Output: PPN TLB Hit: Fetch translation, return PPN TLB Miss: Check page table (in memory) Page Table Hit: Load page table entry into TLB Page Fault: Fetch page from disk to memory, update corresponding page table entry, then load entry into TLB Check cache Input: physical address, Output: data Cache Hit: Return data value to processor Cache Miss: Fetch data value from memory, store it in cache, return it to processor

Address Translation Virtual Address TLB Lookup Protection Check TLB Miss TLB Hit Need to restart instruction. Soft and hard page faults. Page Table “Walk” Protection Check Page not in Mem Page in Mem Access Denied Access Permitted Page Fault (OS loads page) Update TLB Protection Fault Physical Address Find in Disk Find in Mem SIGSEGV Check cache CS252 S05

Context Switching Revisited What needs to happen when the CPU switches processes? Registers: Save state of old process, load state of new process Including the Page Table Base Register (PTBR) Memory: Nothing to do! Pages for processes already exist in memory/disk and protected from each other TLB: Invalidate all entries in TLB – mapping is for old process’ VAs Cache: Can leave alone because storing based on PAs – good for shared data

Summary of Address Translation Symbols Basic Parameters N= 2 𝑛 Number of addresses in virtual address space M= 2 𝑚 Number of addresses in physical address space P= 2 𝑝 Page size (bytes) Components of the virtual address (VA) VPO Virtual page offset VPN Virtual page number TLBI TLB index TLBT TLB tag Components of the physical address (PA) PPO Physical page offset (same as VPO) PPN Physical page number

Simple Memory System Example (small) Addressing 14-bit virtual addresses 12-bit physical address Page size = 64 bytes 13 12 11 10 9 8 7 6 5 4 3 2 1 VPO VPN Virtual Page Number Virtual Page Offset 11 10 9 8 7 6 5 4 3 2 1 PPO PPN Physical Page Number Physical Page Offset

Simple Memory System: Page Table Only showing first 16 entries (out of _____) Note: showing 2 hex digits for PPN even though only 6 bits Note: management bits not shown, but part of each PTE VPN PPN Valid 28 1 – 2 33 3 02 4 5 16 6 7 VPN PPN Valid 8 13 1 9 17 A 09 B – C D 2D E F 0D Size of page table = 2^(n-p) entries

Simple Memory System: TLB 16 entries total 4-way set associative Why does the TLB ignore the page offset? 13 12 11 10 9 8 7 6 5 4 3 2 1 virtual page offset virtual page number TLB index TLB tag Calculate VPN for valid TLB entry using Tag & Set Set Tag PPN Valid Tag PPN Valid Tag PPN Valid Tag PPN Valid 03 – 09 0D 1 00 – 07 02 1 1 03 2D 1 02 – 04 – 0A – 2 02 – 08 – 06 – 03 – 3 07 – 03 0D 1 0A 34 1 02 –

Simple Memory System: Cache Note: It is just coincidence that the PPN is the same width as the cache Tag Simple Memory System: Cache Direct-mapped with K = 4 B, C/K = 16 Physically addressed cache tag cache index cache offset 11 10 9 8 7 6 5 4 3 2 1 physical page number physical page offset Index Tag Valid B0 B1 B2 B3 19 1 99 11 23 15 – 2 1B 00 02 04 08 3 36 4 32 43 6D 8F 09 5 0D 72 F0 1D 6 31 7 16 C2 DF 03 Index Tag Valid B0 B1 B2 B3 8 24 1 3A 00 51 89 9 2D – A 93 15 DA 3B B 0B C 12 D 16 04 96 34 E 13 83 77 1B D3 F 14

Current State of Memory System Page table (partial): TLB: VPN PPN V 28 1 – 2 33 3 02 4 5 16 6 7 VPN PPN V 8 13 1 9 17 A 09 B – C D 2D E F 0D Set Tag PPN V 03 – 09 0D 1 00 07 02 2D 04 0A 2 08 06 3 34 TLB and Cache are separate pieces of hardware; page table stored in memory Show correlation between TLB & page table (VPN 0x0D and 0x0F in particular) Cache: Index Tag V B0 B1 B2 B3 19 1 99 11 23 15 – 2 1B 00 02 04 08 3 36 4 32 43 6D 8F 09 5 0D 72 F0 1D 6 31 7 16 C2 DF 03 Index Tag V B0 B1 B2 B3 8 24 1 3A 00 51 89 9 2D – A 93 15 DA 3B B 0B C 12 D 16 04 96 34 E 13 83 77 1B D3 F 14

Memory Request Example #1 Note: It is just coincidence that the PPN is the same width as the cache Tag Memory Request Example #1 Virtual Address: 0x03D4 Physical Address: TLBI TLBT 13 12 11 10 1 9 8 7 6 5 4 3 2 VPO VPN VPN ______ TLBT _____ TLBI _____ TLB Hit? ___ Page Fault? ___ PPN _____ TLB Hit, Cache Hit 11 10 9 8 7 6 5 4 3 2 1 PPO PPN CO CI CT CT ______ CI _____ CO _____ Cache Hit? ___ Data (byte) _______

Memory Request Example #2 Note: It is just coincidence that the PPN is the same width as the cache Tag Memory Request Example #2 Virtual Address: 0x038F Physical Address: TLBI TLBT 13 12 11 10 1 9 8 7 6 5 4 3 2 VPO VPN VPN ______ TLBT _____ TLBI _____ TLB Hit? ___ Page Fault? ___ PPN _____ TLB Miss, page fault 11 10 9 8 7 6 5 4 3 2 1 PPO PPN CO CI CT CT ______ CI _____ CO _____ Cache Hit? ___ Data (byte) _______

Memory Request Example #3 Note: It is just coincidence that the PPN is the same width as the cache Tag Memory Request Example #3 Virtual Address: 0x0020 Physical Address: TLBI TLBT 13 12 11 10 9 8 7 6 1 5 4 3 2 VPO VPN VPN ______ TLBT _____ TLBI _____ TLB Hit? ___ Page Fault? ___ PPN _____ TLB Miss, Page Table Hit, Cache Miss 11 10 9 8 7 6 5 4 3 2 1 PPO PPN CO CI CT CT ______ CI _____ CO _____ Cache Hit? ___ Data (byte) _______

Memory Request Example #4 Note: It is just coincidence that the PPN is the same width as the cache Tag Memory Request Example #4 Virtual Address: 0x036B Physical Address: TLBI TLBT 13 12 11 10 1 9 8 7 6 5 4 3 2 VPO VPN VPN ______ TLBT _____ TLBI _____ TLB Hit? ___ Page Fault? ___ PPN _____ TLB Hit, Cache Hit 11 10 9 8 7 6 5 4 3 2 1 PPO PPN CO CI CT CT ______ CI _____ CO _____ Cache Hit? ___ Data (byte) _______

Virtual Memory Summary 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 Uses memory efficiently by caching virtual memory pages Efficient only because of locality Simplifies memory management and sharing Simplifies protection by providing permissions checking

Address Translation VM is complicated, but also elegant and effective 11/23/2016 Address Translation VM is complicated, but also elegant and effective Level of indirection to provide isolated memory & caching TLB as a cache of page tables avoids two trips to memory for every memory access Virtual Address TLB Lookup Page Table “Walk” Update Page Fault (OS loads page) Protection Check Physical Address TLB Miss TLB Hit Page not in Mem Access Denied Permitted Fault SIGSEGV Page Check cache Find in Disk Find in Mem Need to restart instruction. Soft and hard page faults. CS252 S05

Memory System Summary Memory Caches (L1/L2/L3) Virtual Memory Purely a speed-up technique Behavior invisible to application programmer and (mostly) OS Implemented totally in hardware Virtual Memory Supports many OS-related functions Process creation, task switching, protection Operating System (software) Allocates/shares physical memory among processes Maintains high-level tables tracking memory type, source, sharing Handles exceptions, fills in hardware-defined mapping tables Hardware Translates virtual addresses via mapping tables, enforcing permissions Accelerates mapping via translation cache (TLB)

Memory Overview movl 0x8043ab, %rdi Disk Page Main memory (DRAM) CPU Word Main memory (DRAM) CPU Cache Page Line MMU Line TLB

Memory System – Who controls what? Memory Caches (L1/L2/L3) Controlled by hardware Programmer cannot control it Programmer can write code to take advantage of it Virtual Memory Controlled by OS and hardware Programmer cannot control mapping to physical memory Programmer can control sharing and some protection via OS functions (not in CSE 351)