Lecture 13: Address Translation

Similar documents
Main Points. Address Transla+on Concept. Flexible Address Transla+on. Efficient Address Transla+on

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

Lecture 4: Memory Management & The Programming Interface

CIS Operating Systems Memory Management Address Translation. Professor Qiang Zeng Fall 2017

Lecture 14: Cache & Virtual Memory

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 13: Address Translation

Advanced Memory Management

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

Main Memory: Address Translation

CIS Operating Systems Memory Management Cache. Professor Qiang Zeng Fall 2015

CIS Operating Systems Memory Management Cache. Professor Qiang Zeng Fall 2017

CS370 Operating Systems

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

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

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

16 Sharing Main Memory Segmentation and Paging

CS 134: Operating Systems

Operating Systems (1DT020 & 1TT802) Lecture 9 Memory Management : Demand paging & page replacement. Léon Mugwaneza

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

memory management Vaibhav Bajpai

Introduction to Operating Systems

Page 1. Review: Address Segmentation " Review: Address Segmentation " Review: Address Segmentation "

PROCESS VIRTUAL MEMORY. CS124 Operating Systems Winter , Lecture 18

Recap: Memory Management

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

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

Topic 18: Virtual Memory

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

Cl. Cl. ..., V, V, -I..., - QJ d.

CPS 104 Computer Organization and Programming Lecture 20: Virtual Memory

Topic 18 (updated): Virtual Memory

Memory management: outline

Memory management: outline

Computer Architecture. Lecture 8: Virtual Memory

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

Page Tables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

Operating Systems. Memory Management. Lecture 9 Michael O Boyle

EECS 482 Introduction to Operating Systems

ECE 571 Advanced Microprocessor-Based Design Lecture 12

Chapter 8. Virtual Memory

CS3600 SYSTEMS AND NETWORKS

Memory Hierarchy Requirements. Three Advantages of Virtual Memory

Paging. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Virtual memory Paging

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

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

Address spaces and memory management

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

Lecture 21: Virtual Memory. Spring 2018 Jason Tang

ECE 598 Advanced Operating Systems Lecture 14

Recall: Paging. Recall: Paging. Recall: Paging. CS162 Operating Systems and Systems Programming Lecture 13. Address Translation, Caching

Address Translation. Tore Larsen Material developed by: Kai Li, Princeton University

Computer Architecture Lecture 13: Virtual Memory II

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

Lecture 15: I/O Devices & Drivers

Virtual Memory. Kevin Webb Swarthmore College March 8, 2018

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

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

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

Memory Management. Dr. Yingwu Zhu

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

Chapter 8: Memory-Management Strategies

CS307: Operating Systems

Virtual to physical address translation

CS370 Operating Systems

Virtual Memory. Samira Khan Apr 27, 2017

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

CS 333 Introduction to Operating Systems Class 2 OS-Related Hardware & Software The Process Concept

Paging. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Basic Memory Management

CPE300: Digital System Architecture and Design

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

Chapter 8: Main Memory

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

CHAPTER 8 - MEMORY MANAGEMENT STRATEGIES

Virtual Memory. Virtual Memory

Lecture: Large Caches, Virtual Memory. Topics: cache innovations (Sections 2.4, B.4, B.5)

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

COS 318: Operating Systems. Virtual Memory and Address Translation

Page 1. Goals for Today" Important Aspects of Memory Multiplexing" Virtualizing Resources" CS162 Operating Systems and Systems Programming Lecture 9

Caching and Demand-Paged Virtual Memory

Chapter 8: Main Memory

Basic Memory Management. Basic Memory Management. Address Binding. Running a user program. Operating Systems 10/14/2018 CSC 256/456 1

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

Classifying Information Stored in Memory! Memory Management in a Uniprogrammed System! Segments of a Process! Processing a User Program!

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

CS 3733 Operating Systems:

Chapter 5 Memory Hierarchy Design. In-Cheol Park Dept. of EE, KAIST

Lecture 18: Reliable Storage

CS370 Operating Systems

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

Lecture 17: Virtual Memory, Large Caches. Today: virtual memory, shared/pvt caches, NUCA caches

Page 1. Goals for Today" Virtualizing Resources" Important Aspects of Memory Multiplexing" CS162 Operating Systems and Systems Programming Lecture 20

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

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

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

Goals of Memory Management

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

Transcription:

CS 422/522 Design & Implementation of Operating Systems Lecture 13: Translation Zhong Shao Dept. of Computer Science Yale University Acknowledgement: some slides are taken from previous versions of the CS422/522 lectures taught by Prof. Bryan Ford and Dr. David Wolinsky, and also from the official set of slides accompanying the OSPP textbook by Anderson and Dahlin. Main points translation concept How do we convert a virtual address to a physical address? Flexible address translation Base and bound Segmentation Paging Multilevel translation Efficient address translation Translation Lookaside Buffers ly and physically addressed caches 1

translation concept Translation Invalid Raise Exception Valid translation goals protection sharing Shared libraries, interprocess communication Sparse addresses Multiple regions of dynamic allocation (heaps/stacks) Efficiency placement Runtime lookup Compact translation tables Portability 2

Bonus feature What can you do if you can (selectively) gain control whenever a program reads or writes a particular virtual memory location? Examples: Copy on write Zero on reference Fill on demand Demand paging mapped files A Preview: MIPS address translation Software-Loaded Translation lookaside buffer (TLB) Cache of virtual page -> physical page translations If TLB hit, physical address If TLB miss, trap to kernel Kernel fills TLB with translation and resumes execution Kernel can implement any page translation Page tables Multi-level page tables Inverted page tables 3

A Preview: MIPS lookup Page# Translation Lookaside Buffer (TLB) Matching Entry Page Page Access Page Table Lookup ly addressed base and bounds s View Implementation Base Base Bound Base+ Bound Raise Exception 4

ly addressed base and bounds Pros? Simple Fast (2 registers, adder, comparator) Safe Can relocate in physical memory without changing process Cons? Can t keep program from accidentally overwriting its own code Can t share code/data with other processes Can t grow stack/heap as needed Segmentation Segment is a contiguous region of virtual memory Each process has a segment table (in hardware) Entry in table = segment Segment can be located anywhere in physical memory Each segment has: start, length, access permission Processes can share segments Same start, length, same/different access permissions 5

Segmentation Process View Code Heap Stack Segment Implementation Segment Table Base Bound Access Read Raise Exception Stack Code Heap Base 3 Base+ Bound 3 Base 0 Base+ Bound 0 Base 1 Base+ Bound 1 Base 2 Base+ Bound 2 UNIX fork and copy on write UNIX fork Makes a complete copy of a process Segments allow a more efficient implementation Copy segment table into child Mark parent and child segments read-only Start child process; return to parent If child or parent writes to a segment (ex: stack, heap) * trap into kernel * make a copy of the segment and resume 6

Unix fork and copy on write (cont d) P2`s 0x0500 Code Heap Seg. 0 500 Code Heap Stack Segment Table Base Bound Access Read P1`s Heap P1`s Stack P1`s Process 2`s View Stack P2`s Heap 0x0500 Code Seg. 0 500 Code Segment Table Base Bound Access Read P1 s+ P2`s Code P2`s Stack Heap Heap Stack Zero-on-reference How much physical memory is needed for the stack or heap? Only what is currently in use When program uses memory beyond end of stack Segmentation fault into OS kernel Kernel allocates some memory * How much? Zeros the memory * avoid accidentally leaking information! Modify segment table Resume process 7

Segmentation Pros? Can share code/data segments between processes Can protect code segment from being overwritten Can transparently grow stack/heap as needed Can detect if need to copy-on-write Cons? Complex memory management * Need to find chunk of a particular size May need to rearrange memory from time to time to make room for new segment or growing segment * External fragmentation: wasted space between chunks Paged translation Manage memory in fixed size units, or pages Finding a free page is easy Bitmap allocation: 0011111100000001100 Each bit represents one physical page frame Each process has its own page table Stored in physical memory Hardware registers * pointer to page table start * page table length 8

Paging and copy on write Can we share memory between processes? Set entries in both page tables to point to same page frames Need core map of page frames to track which processes are pointing to which page frames (e.g., reference count) UNIX fork with copy on write Copy page table of parent into child process Mark all pages (in new and old page tables) as read-only Trap into kernel on write (in child or parent) Copy page Mark both as writeable Resume execution Fill on demand Can I start running a program before its code is in physical memory? Set all page table entries to invalid When a page is referenced for first time, kernel trap Kernel brings page in from disk Resume execution Remaining pages can be transferred in the background while program is running 9

Sparse address spaces Might want many separate dynamic segments Per-processor heaps Per-thread stacks -mapped files Dynamically linked libraries What if virtual address space is large? 32-bits, 4KB pages => 500K page table entries 64-bits => 4 quadrillion page table entries Multi-level translation Tree of translation tables Paged segmentation Multi-level page tables Multi-level paged segmentation Fixed-size page as lowest level unit of allocation Efficient memory allocation (compared to segments) Efficient for sparse addresses (compared to paging) Efficient disk transfers (fixed size units) Easier to build translation lookaside buffers Efficient reverse lookup (from physical -> virtual) Variable granularity for protection/sharing 10

Paged segmentation Process memory is segmented Segment table entry: Pointer to page table Page table length (# of pages in segment) Access permissions Page table entry: Page frame Access permissions Share/protection at either page or segment-level Paged segmentation (implementation) Implementation Segment Page Exception Segment Table Page Table Size Access Read Page Table Access Read Read 11

Multilevel paging Implementation Index 1 Index 2 Index 3 Level 1 Level 2 Level 3 X86 multilevel paged segmentation Global Descriptor Table (segment table) Pointer to page table for each segment Segment length Segment access permissions Context switch: change global descriptor table register (GDTR, pointer to global descriptor table) Multilevel page table 4KB pages; each level of page table fits in one page 32-bit: two level page table (per segment) 64-bit: four level page table (per segment) Omit sub-tree if no valid addresses 12

Multilevel translation Pros: Allocate/fill only page table entries that are in use Simple memory allocation Share at segment or page level Cons: Space overhead: one pointer per virtual page Two (or more) lookups per memory reference Portability Many operating systems keep their own memory translation data structures List of memory objects (segments) page -> physical page frame page frame -> set of virtual pages One approach: Inverted page table Hash from virtual page -> physical page Space proportional to # of physical pages 13

Efficient address translation Translation lookaside buffer (TLB) Cache of recent virtual page -> physical page translations If cache hit, use translation If cache miss, walk multi-level page table Cost of translation = Cost of TLB lookup + Prob(TLB miss) * cost of page table lookup TLB and page table translation TLB Miss Page Table Invalid Raise Exception Hit Valid 14

TLB lookup Page# Translation Lookaside Buffer (TLB) Matching Entry Page Page Access Page Table Lookup MIPS software loaded TLB Software defined translation tables If translation is in TLB, ok If translation is not in TLB, trap to kernel Kernel computes translation and loads TLB Kernel can use whatever data structures it wants Pros/cons? 15

Question What is the cost of a TLB miss on a modern processor? Cost of multi-level page table walk MIPS: plus cost of trap handler entry/exit Hardware design principle The bigger the memory, the slower the memory 16

Intel i7 hierarchy i7 has 8MB as shared 3 rd level cache; 2 nd level cache is per-core 17

Question What is the cost of a first level TLB miss? Second level TLB lookup What is the cost of a second level TLB miss? x86: 2-4 level page table walk How expensive is a 4-level page table walk on a modern processor? ly addressed vs. physically addressed caches Too slow to first access TLB to find physical address, then look up address in the cache Instead, first level cache is virtually addressed In parallel, access TLB to generate physical address in case of a cache miss 18

19 ly addressed caches Raise Exception Hit Hit Valid Cache TLB Page Table Miss Miss Invalid ly addressed cache Raise Exception Hit Hit Valid Cache TLB Page Table Cache Hit Miss Miss Miss Invalid

When do TLBs work/not work? Video Buffer: 32 bits x 1K x 1K = 4MB Page# 0 1 2 3 Video Buffer 1021 1022 1023 Superpages On many systems, TLB entry can be A page A superpage: a set of contiguous pages x86: superpage is set of pages in one page table x86 TLB entries * 4KB * 2MB * 1GB 20

Superpages Page# SP Translation Lookaside Buffer (TLB) Matching Entry Superpage (SP) or Page# Superframe (SF) or Access Matching Superpage Page Table Lookup SF When do TLBs work/not work, part 2 What happens when the OS changes the permissions on a page? For demand paging, copy on write, zero on reference, TLB may contain old translation OS must ask hardware to purge TLB entry On a multicore: TLB shootdown OS must ask each CPU to purge TLB entry 21

TLB shootdown Process ID Page Page Access 1 TLB = = 0 0x0053 0x0003 1 0x4OFF 0x0012 2 TLB = = 0 0x0053 0x0003 0 0x0001 0x0005 Read 3 TLB = = 1 0x4OFF 0x0012 0 0x0001 0x0005 Read When do TLBs work/not work, part 3 What happens on a context switch? Reuse TLB? Discard TLB? Solution: Tagged TLB Each TLB entry has process ID TLB hit only if process ID matches current process 22

Implementation Page Page# Process ID Matching Entry Translation Lookaside Buffer (TLB) Process ID Page Access Page Table Lookup Aliasing Alias: two (or more) virtual cache entries that refer to the same physical memory A consequence of a tagged virtually addressed cache! A write to one copy needs to update all copies Typical solution Keep both virtual and physical address for each entry in virtually addressed cache Lookup virtually addressed cache and TLB in parallel Check if physical address from TLB matches multiple entries, and update/invalidate other copies 23

Multicore and hyperthreading Modern CPU has several functional units Instruction decode Arithmetic/branch Floating point Instruction/data cache TLB Multicore: replicate functional units (i7: 4) Share second/third level cache, second level TLB Hyperthreading: logical processors that share functional units (i7: 2) Better functional unit utilization during memory stalls No difference from the OS/programmer perspective Except for performance, affinity, translation uses Process isolation Keep a process from touching anyone else s memory, or the kernel s Efficient inter-process communication Shared regions of memory between processes Shared code segments E.g., common libraries used by many different programs Program initialization Start running a program before it is entirely in memory Dynamic memory allocation Allocate and initialize stack/heap pages on demand 24

translation (more) Cache management Page coloring Program debugging breakpoints when address is accessed Zero-copy I/O Directly from I/O device into/out of user memory mapped files Access file data using load/store instructions Demand-paged virtual memory Illusion of near-infinite memory, backed by disk or memory on other machines translation (even more) Checkpointing/restart Transparently save a copy of a process, without stopping the program while the save happens Persistent data structures Implement data structures that can survive system reboots Process migration Transparently move processes between machines Information flow control Track what data is being shared externally Distributed shared memory Illusion of memory that is shared between machines 25