Virtual Memory #2 Feb. 21, 2018

Similar documents
The cow and Zaphod... Virtual Memory #2 Feb. 21, 2007

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

CSE 120 Principles of Operating Systems Spring 2017

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

Virtual Memory. Computer Systems Principles

Address spaces and memory management

16 Sharing Main Memory Segmentation and Paging

CS 153 Design of Operating Systems Winter 2016

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

15 Sharing Main Memory Segmentation and Paging

CS 318 Principles of Operating Systems

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

Virtual Memory 1. To do. q Segmentation q Paging q A hybrid system

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

"what you don't know, that can't kill you (but can kill your performance)"

EECS 482 Introduction to Operating Systems

Memory and multiprogramming

Chapter 3 - Memory Management

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

Virtual Memory. Today. Segmentation Paging A good, if common example

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

Objectives and Functions Convenience. William Stallings Computer Organization and Architecture 7 th Edition. Efficiency

Chapter 8: Memory-Management Strategies

CS 153 Design of Operating Systems Winter 2016

Operating System Support

Memory management OS

CHAPTER 8 - MEMORY MANAGEMENT STRATEGIES

Chapter 8: Main Memory

CHAPTER 8: MEMORY MANAGEMENT. By I-Chen Lin Textbook: Operating System Concepts 9th Ed.

Chapter 8: Main Memory. Operating System Concepts 9 th Edition

Virtual Memory I. Jo, Heeseung

Virtual memory - Paging

The process. one problem

CS510 Operating System Foundations. Jonathan Walpole

Chapter 8: Main Memory

Memory Hierarchy Requirements. Three Advantages of Virtual Memory

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

Memory management. Last modified: Adaptation of Silberschatz, Galvin, Gagne slides for the textbook Applied Operating Systems Concepts

Paging. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Course Outline. Processes CPU Scheduling Synchronization & Deadlock Memory Management File Systems & I/O Distributed Systems

Paging. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CS162 Operating Systems and Systems Programming Lecture 12. Address Translation. Page 1

Memory management, part 3: outline

Recap: Memory Management

Today: Segmentation. Last Class: Paging. Costs of Using The TLB. The Translation Look-aside Buffer (TLB)

ECE 598 Advanced Operating Systems Lecture 14

Multi-Process Systems: Memory (2) Memory & paging structures: free frames. Memory & paging structures. Physical memory

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

Modeling Page Replacement: Stack Algorithms. Design Issues for Paging Systems

CS 134: Operating Systems

20-EECE-4029 Operating Systems Spring, 2013 John Franco

Opera&ng Systems ECE344

Virtual Memory. CS 351: Systems Programming Michael Saelee

Chapter 4 Memory Management. Memory Management

Memory Management Virtual Memory

Address Translation. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

ECE 571 Advanced Microprocessor-Based Design Lecture 12

Chapter 7: Main Memory. Operating System Concepts Essentials 8 th Edition

CIS Operating Systems Memory Management Address Translation for Paging. Professor Qiang Zeng Spring 2018

File Systems. OS Overview I/O. Swap. Management. Operations CPU. Hard Drive. Management. Memory. Hard Drive. CSI3131 Topics. Structure.

Chapter 9 Memory Management

Operating Systems Design Exam 2 Review: Spring 2012

VIRTUAL MEMORY. Operating Systems 2015 Spring by Euiseong Seo

CPS 104 Computer Organization and Programming Lecture 20: Virtual Memory

1. With respect to space, linked lists are and arrays are.

Virtual to physical address translation

VIRTUAL MEMORY II. Jo, Heeseung

Outline. V Computer Systems Organization II (Honors) (Introductory Operating Systems) Advantages of Multi-level Page Tables

CS307: Operating Systems

Virtual Memory 1. Virtual Memory

Virtual Memory 1. Virtual Memory

CS 4284 Systems Capstone. Virtual Memory Page Tables Godmar Back

Operating Systems CSE 410, Spring Virtual Memory. Stephen Wagner Michigan State University

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

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

Chapter 8 & Chapter 9 Main Memory & Virtual Memory

Chapter 8 Virtual Memory

Memory management, part 3: outline

Memory Management. Memory

238P: Operating Systems. Lecture 5: Address translation. Anton Burtsev January, 2018

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

CSCI 4717 Computer Architecture. Memory Management. What is Swapping? Swapping. Partitioning. Fixed-Sized Partitions (continued)

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

CS399 New Beginnings. Jonathan Walpole

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

Virtual Memory. Today. Handling bigger address spaces Speeding translation

Virtual Memory. Yannis Smaragdakis, U. Athens

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

Chapter 8. Virtual Memory

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

OPERATING SYSTEM. Chapter 9: Virtual Memory

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

Addresses in the source program are generally symbolic. A compiler will typically bind these symbolic addresses to re-locatable addresses.

Chapter 8 Memory Management

CIS Operating Systems Memory Management Cache and Demand Paging. Professor Qiang Zeng Spring 2018

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

Lecture 21: Virtual Memory. Spring 2018 Jason Tang

Lecture 8 Memory Management Strategies (chapter 8)

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

Transcription:

15-410...The mysterious TLB... Virtual Memory #2 Feb. 21, 2018 Dave Eckhardt Brian Railing 1 L16_VM2

Last Time Mapping problem: logical vs. physical addresses Contiguous memory mapping (base, limit) Swapping taking turns in memory Paging Array mapping page numbers to frame numbers Observation: typical table is sparsely occupied Response: some sparse data structure (e.g., 2-level array) 2

Swapping Multiple user processes Sum of memory demands > system memory Goal: Allow each process 100% of system memory Take turns Temporarily evict process(es) to disk Swap daemon shuffles process in & out Can take seconds per process Creates external fragmentation problem 3

External Fragmentation ( Holes ) Process 3 Process 3 Process 1 Process 2 Process 4 Process 2 Process 4 Process 1 OS Kernel OS Kernel 4

Benefits of Paging Process growth problem Any process can use any free frame for any purpose Fragmentation compaction problem Process doesn't need to be contiguous Long delay to swap a whole process Swap part of the process instead! 5

Partial Residence P0 stack 0 P0 data 0 P0 code 1 P0 code 0 [free] P1 data 1 P0 stack 0 P1 stack 0 P1 data 0 P0 data 0 [free] P0 code 0 OS Kernel P1 stack 0 P1 data 1 P1 data 0 P1 code 0 6

Page Table Entry (PTE) flags Valid/Present bit set by OS Frame pointer is valid, no need to fault Protection bits set by OS Dirty bit Read/write/execute Hardware sets 0 1 when data stored into page OS sets 1 0 when page has been written to disk Reference bit Hardware sets 0 1 on any data access to page OS uses for page eviction (later) 7

Outline The mysterious TLB Partial memory residence (demand paging) in action The task of the page fault handler 8

Double Trouble? Triple Trouble? Program requests memory access Processor makes two memory accesses! Split address into page number, intra-page offset Add to page table base register Fetch page table entry (PTE) from memory Add frame address, intra-page offset Fetch data from memory 9

Double Trouble? Triple Trouble? Program requests memory access Processor makes two memory accesses! Split address into page number, intra-page offset Add to page table base register Fetch page table entry (PTE) from memory Add frame address, intra-page offset Fetch data from memory Can be worse than that... x86 Page-Directory/Page-Table Three physical accesses per virtual access! x86-64 has a four-level page-mapping system 10

Translation Lookaside Buffer (TLB) Problem Cannot afford double/triple/... memory latency Observation - locality of reference Solution Program often accesses nearby memory Next instruction often on same page as current instruction Next byte of string often on same page as current byte ( Array good, linked list bad ) Page-map hardware caches virtual-to-physical mappings Small, fast on-chip memory Free in comparison to slow off-chip memory 11

Simplest Possible TLB Approach Remember the most-recent virtual-to-physical translation (obtained from, e.g., Page Directory + Page Table) See if next memory access is to same page If so, skip PD/PT memory traffic; use same frame 3X speedup, cost is two 20-bit registers» Great work if you can get it 12

Simplest Possible TLB P1 Page Directory P2 Offset f08 f07 f99 f87 f29 f34 f25 Page Tables 13

Simplest Possible TLB P1 Page Directory P2 Offset f08 f07 f99 f87 f29 f34 f25 Page Tables 14

Simplest Possible TLB P1 802A5 P2 Offset f08 f07 f34 Page Directory f99 f87 f29 f34 f25 Page Tables 15

TLB Hit 802A5 802A5 Offset f34 Page Directory f08 f07 f99 f87 f29 f34 f25 Page Tables 16

TLB Miss 802A4 802A5 Offset f34 Page Directory f08 f07 f99 f87 f29 f34 f25 Page Tables 17

TLB Refill P1 802A4 P2 Offset f08 f07 f25 Page Directory f99 f87 f29 f34 f25 Page Tables 18

Simplest Possible TLB Can you think of a pathological instruction? What would it take to break a 1-entry TLB? How many TLB entries do we need, anyway? 19

TLB vs. Context Switch After we've been running a while... Interrupt!...the TLB is hot - full of page frame translations Some device is done......should switch to some other task......what are the parts of context switch, again? General-purpose registers...? 20

TLB vs. Context Switch After we've been running a while... Interrupt!...the TLB is hot - full of page frame translations Some device is done......should switch to some other task......what are the parts of context switch, again? General-purpose registers Page Table Base Register...? 21

TLB vs. Context Switch After we've been running a while... Interrupt!...the TLB is hot - full of page frame translations Some device is done......should switch to some other task......what are the parts of context switch, again? General-purpose registers Page Table Base Register Entire contents of TLB!!» (why?) 22

x86 TLB Flush 1. Declare new page directory (set %cr3) Clears every entry in TLB (whoosh!) Footnote: doesn't clear global pages...» Which pages might be global? 2. INVLPG instruction Invalidates TLB entry of one specific page Is that more efficient or less? 23

x86 Type Theory Final Version 24 Instruction segment selector [PUSHL specifies selector in %SS] Process (selector (base,limit) ) [Global,Local Descriptor Tables] Segment base, address linear address TLB: linear address physical address, or... Process (linear address high page table) Page Table: linear address middle frame address Memory: frame address, offset...

Is there another way? That seems really complicated Is that hardware monster really optimal for every OS and program mix? The only way to win is not to play? Is there another way? Could we have no page tables? How would the hardware map virtual to physical??? 25

Software-loaded TLBs Reasoning We need a TLB for performance reasons OS defines each process's memory structure Which memory regions, permissions Lots of processes share frames of /bin/bash! Hardware page-mapping unit imposes its own ideas Why impose a semantic middle-man? Approach OS knows all mappings for an address space TLB contains a subset of them TLB miss generates an exception OS quickly fills in correct v p mapping 26

Software TLB features Mapping entries can be computed many ways Imagine a system with one process memory size TLB miss becomes a matter of arithmetic Mapping entries can be locked in TLB Good idea to lock the TLB-miss handler's TLB entry... Great for real-time systems Further reading http://yarchive.net/comp/software_tlb.html Software TLBs PowerPC 603, 400-series (but NOT 7xx/9xx, Cell) MIPS, some SPARC 27

TLB vs. Project 3 x86 has a nice, automatic TLB Hardware page-mapper fills it for you Activating new page directory flushes TLB automatically What could be easier? It's not totally automatic Something natural in your kernel may confuse it... TLB debugging in Simics (you will need this!) logical-to-physical (l2p) command cpu0_tlb.info, cpu0_tlb.status, cpu0.tablewalk More bits trying to tell you something [INVLPG issues with Simics 1. Simics 2, 3, 4 seem ok] 28

Partial Memory Residence Error-handling code not used by every run No need for it to occupy memory for entire duration... Tables may be allocated larger than used player players[max_players]; Computer can run very large programs Much larger than physical memory As long as active footprint fits in RAM Swapping can't do this Programs can launch faster Needn't load whole program before running 29

Virtual Memory Approach Use RAM frames as a cache for the set of all pages Some pages are fast to access (in a RAM frame) Some pages are slow to access (in a disk frame ) Page tables indicate which pages are resident Non-resident pages are missing from the TLB And have present=0 in page table entry, if we have a hardware page-mapping unit Access to a non-resident page generates a page fault Hardware invokes page-fault exception handler Hopefully most references hit in the RAM cache 30

Page fault Reasons, Responses Address is invalid/illegal deliver software exception Unix SIGSEGV Mach deliver message to thread's exception port 15-410 swexn handler, or else kill thread Process is growing stack give it a new frame Cache misses - fetch from disk Where on disk, exactly? 31

Satisfying Page Faults stack Free-frame pool bss data code Paging Space Filesystem 32

Page fault story - 1 Process issues memory reference TLB: miss (PT: not present ) Surprise! Into the kernel... Processor dumps exception frame onto kernel stack (x86) Transfers via page fault interrupt descriptor table entry Runs exception handler 33

Page fault story 2 Classify fault address Illegal address deliver an ouch, else... Code/rodata region of executable? Determine which sector of executable file Launch read() from file into an unused frame Previously resident r/w data, paged out somewhere on the paging partition Queue disk read into an unused frame First use of bss/stack page Allocate a frame full of zeroes, insert into PT 34

Page fault story 3 Block the process (for most cases) Switch to running another Handle I/O-complete interrupt Fill in PTE (present = 1) Mark process runnable Restore registers, switch page table Faulting instruction re-started transparently Single instruction may fault more than once! 35

Memory Regions vs. Page Tables What's a poor page fault handler to do? Kill process? Copy page, mark read-write? Fetch page from file? Which? Where? Page table not a good data structure Format defined by hardware Per-page nature is repetitive Not enough bits to encode OS metadata Disk sector address may possibly be > 32 bits 36

Dual-view Memory Model Logical Physical Process memory is a list of regions Holes between regions are illegal addresses Per-region methods fault(), evict(), unmap() Process memory is a list of pages Faults delegated to per-region methods Many invalid pages can be made valid But sometimes a region fault handler returns error» Handle as with hole case above 37

Page-fault story (for real) Examine fault address Look up: address region region->fault(addr, access_mode) Quickly fix up problem Or start fix, block process, run scheduler 38

Summary The mysterious TLB No longer mysterious Process address space Logical: list of regions Hardware: list of pages Fault handler is complicated Demand-load from file, page in from paging area,... 39