Virtual Memory and Address Translation. Review: the Program and the Process VAS text dataidata wdata header symbol table relocation records program text.

Slides:



Advertisements
Similar presentations
Part IV: Memory Management
Advertisements

MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 3 Memory Management Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
OS Fall’02 Virtual Memory Operating Systems Fall 2002.
Computer Organization CS224 Fall 2012 Lesson 44. Virtual Memory  Use main memory as a “cache” for secondary (disk) storage l Managed jointly by CPU hardware.
CS 153 Design of Operating Systems Spring 2015
OS Spring ‘04 Paging and Virtual Memory Operating Systems Spring 2004.
Paging and Virtual Memory. Memory management: Review  Fixed partitioning, dynamic partitioning  Problems Internal/external fragmentation A process can.
Memory Management. 2 How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines.
Memory Management 2010.
Chapter 3.2 : Virtual Memory
Memory Management 1 CS502 Spring 2006 Memory Management CS-502 Spring 2006.
CS-3013 & CS-502, Summer 2006 Memory Management1 CS-3013 & CS-502 Summer 2006.
Computer Organization and Architecture
Chapter 4 Memory Management 4.1 Basic memory management 4.2 Swapping
Protection and the Kernel: Mode, Space, and Context.
Traps and Faults. Review: Mode and Space data user mode kernel mode A B C “kernel space”
Memory Management From Chapter 4, Modern Operating Systems, Andrew S. Tanenbaum.
Implementing Processes. Review: Threads vs. Processes 1. The process is a kernel abstraction for an independent executing program. includes at least one.
1 Chapter 3.2 : Virtual Memory What is virtual memory? What is virtual memory? Virtual memory management schemes Virtual memory management schemes Paging.
1 Memory Management Chapter Basic memory management 4.2 Swapping (εναλλαγή) 4.3 Virtual memory (εικονική/ιδεατή μνήμη) 4.4 Page replacement algorithms.
Operating Systems ECE344 Ding Yuan Paging Lecture 8: Paging.
1 Memory Management 4.1 Basic memory management 4.2 Swapping 4.3 Virtual memory 4.4 Page replacement algorithms 4.5 Modeling page replacement algorithms.
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming  To allocate scarce memory resources.
Chapter 8 – Main Memory (Pgs ). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.
Chapter 4 Memory Management Virtual Memory.
Memory Management Fundamentals Virtual Memory. Outline Introduction Motivation for virtual memory Paging – general concepts –Principle of locality, demand.
Virtual Memory and Address Translation
Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations.
Fundamentals of Programming Languages-II Subject Code: Teaching SchemeExamination Scheme Theory: 1 Hr./WeekOnline Examination: 50 Marks Practical:
CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming.  To allocate scarce memory.
Paging (continued) & Caching CS-3013 A-term Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
Processes and Virtual Memory
Programs and Processes. The Virtual Address Space A typical process VAS space includes: user regions in the lower half V->P mappings specific to each.
CS2100 Computer Organisation Virtual Memory – Own reading only (AY2015/6) Semester 1.
Virtual Memory Ch. 8 & 9 Silberschatz Operating Systems Book.
Constructive Computer Architecture Virtual Memory: From Address Translation to Demand Paging Arvind Computer Science & Artificial Intelligence Lab. Massachusetts.
Memory Management. 2 How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines.
Virtual Memory 1 Computer Organization II © McQuain Virtual Memory Use main memory as a “cache” for secondary (disk) storage – Managed jointly.
1 Memory Management Adapted From Modern Operating Systems, Andrew S. Tanenbaum.
Chapter 7: Main Memory CS 170, Fall Program Execution & Memory Management Program execution Swapping Contiguous Memory Allocation Paging Structure.
CS161 – Design and Architecture of Computer
Translation Lookaside Buffer
MODERN OPERATING SYSTEMS Third Edition ANDREW S
CS161 – Design and Architecture of Computer
CSE 120 Principles of Operating
Virtual Memory and Demand Paging
CS703 - Advanced Operating Systems
Modeling Page Replacement Algorithms
CSE 153 Design of Operating Systems Winter 2018
Chapter 8: Main Memory.
Lecture 14 Virtual Memory and the Alpha Memory Hierarchy
Main Memory Background Swapping Contiguous Allocation Paging
CS399 New Beginnings Jonathan Walpole.
Modeling Page Replacement Algorithms
Morgan Kaufmann Publishers Memory Hierarchy: Virtual Memory
CSE 451: Operating Systems Autumn 2005 Memory Management
Virtual Memory Overcoming main memory size limitation
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
Chapter 8: Memory Management strategies
CSE451 Virtual Memory Paging Autumn 2002
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
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.
COMP755 Advanced Operating Systems
CSE 542: Operating Systems
Presentation transcript:

Virtual Memory and Address Translation

Review: the Program and the Process VAS text dataidata wdata header symbol table relocation records program text data BSS user stack args/env kernel data process VAS sections segments BSS “Block Started by Symbol” (uninitialized global data) e.g., heap and sbuf go here. Args/env strings copied in by kernel when the process is created. Process text segment is initialized directly from program text section. Process data segment(s) are initialized from idata and wdata sections. Process stack and BSS (e.g., heap) segment(s) are zero-filled. Process BSS segment may be expanded at runtime with a system call (e.g., Unix sbrk) called by the heap manager routines. Text and idata segments may be write-protected.

Review: Virtual Addressing text data BSS user stack args/env kernel data virtual memory (big) physical memory (small) virtual-to-physical translations User processes address memory through virtual addresses. The kernel and the machine collude to translate virtual addresses to physical addresses. The kernel controls the virtual-physical translations in effect for each space. The machine does not allow a user process to access memory unless the kernel “says it’s OK”. The specific mechanisms for memory management and address translation are machine-dependent.

What’s in an Object File or Executable? int j = 327; char* s = “hello\n”; char sbuf[512]; int p() { int k = 0; j = write(1, s, 6); return(j); } text data idata wdata header symbol table relocation records Used by linker; may be removed after final link step and strip. Header “magic number” indicates type of image. Section table an array of (offset, len, startVA) program sections program instructions p immutable data (constants) “hello\n” writable global/static data j, s j, s,p,sbuf

Role of MMU Hardware and OS VM address translation must be very cheap (on average). Every instruction includes one or two memory references. (including the reference to the instruction itself) VM translation is supported in hardware by a Memory Management Unit or MMU. The addressing model is defined by the CPU architecture. The MMU itself is an integral part of the CPU. The role of the OS is to install the virtual-physical mapping and intervene if the MMU reports that it cannot complete the translation.

The OS Directs the MMU The OS controls the operation of the MMU to select: (1) the subset of possible virtual addresses that are valid for each process (the process virtual address space); (2) the physical translations for those virtual addresses; (3) the modes of permissible access to those virtual addresses; read/write/execute (4) the specific set of translations in effect at any instant. need rapid context switch from one address space to another MMU completes a reference only if the OS “says it’s OK”. MMU raises an exception if the reference is “not OK”.

The Translation Lookaside Buffer (TLB) An on-chip hardware translation buffer (TB or TLB) caches recently used virtual-physical translations (ptes). Alpha 21164: 48-entry fully associative TLB. A CPU pipeline stage probes the TLB to complete over 99% of address translations in a single cycle. Like other memory system caches, replacement of TLB entries is simple and controlled by hardware. e.g., Not Last Used If a translation misses in the TLB, the entry must be fetched by accessing the page table(s) in memory. cost: cycles

Care and Feeding of TLBs The OS kernel carries out its memory management functions by issuing privileged operations on the MMU. Choice 1: OS maintains page tables examined by the MMU. MMU loads TLB autonomously on each TLB miss page table format is defined by the architecture OS loads page table bases and lengths into privileged memory management registers on each context switch. Choice 2: OS controls the TLB directly. MMU raises exception if the needed pte is not in the TLB. Exception handler loads the missing pte by reading data structures in memory (software-loaded TLB).

A Simple Page Table PFN 0 PFN 1 PFN i page #i offset user virtual address PFN i + offset process page table physical memory page frames In this example, each VPN j maps to PFN j, but in practice any physical frame may be used for any virtual page. Each process/VAS has its own page table. Virtual addresses are translated relative to the current page table. The page tables are themselves stored in memory; a protected register holds a pointer to the current page table.

Page Tables (2) 32 bit address with 2 page table fields Two-level page tables Second-level page tables Top-level page table [from Tanenbaum]

Alpha Page Tables (Forward Mapped) POL3L2L1 base PFN seg 0/1 three-level page table (forward-mapped) sparse 64-bit address space (43 bits in and 21164) offset at each level is determined by specific bits in VA

A Page Table Entry (PTE) PFN valid bit: OS uses this bit to tell the MMU if the translation is valid. write-enable: OS touches this to enable or disable write access for this mapping. reference bit: MMU sets this when a reference is made through the mapping. dirty bit: MMU sets this when a store is completed to the page (page is modified). This is (roughly) what a MIPS/Nachos page table entry (pte) looks like.

Page Tables (3) Typical page table entry [from Tanenbaum]

Virtual Address Translation VPNoffset Example: typical 32-bit architecture with 8KB pages. address translation Virtual address translation maps a virtual page number (VPN) to a physical page frame number (PFN): the rest is easy. PFN offset + 00 virtual address physical address { Deliver exception to OS if translation is not valid and accessible in requested mode.

What You Should Know Basics of paged memory management Typical address space layout Basics of address translation Architectural mechanisms to support paged memory Importance for kernel protection and process isolation Why the simple page table is inadequate Motivation for and structure of hierarchical tables Motivation for and structure of hashed (inverted) tables

Background The remaining slides provide background from CPS 110. Be sure you understand why page-based memory allocation is more memory-efficient than the old way: allocating contiguous physical memory for each address space (partitioning). Two partitioning strategies: fixed and variable How to make partitioning transparent to programs How to protect memory in a partitioned system Fragmentation: internal and external Fragmentation issues for each strategy Relevance to heap managers today Approaches to variable partitioning: First fit, best fit, etc., and the role of compaction.

Memory Management 101 Once upon a time...memory was called “core”, and programs (“jobs”) were loaded and executed one by one. load image in contiguous physical memory start execution at a known physical location allocate space in high memory for stack and data address text and data using physical addresses prelink executables for known start address run to completion

Memory and Multiprogramming One day, IBM decided to load multiple jobs in memory at once. improve utilization of that expensive CPU improve system throughput Problem 1: how do programs address their memory space? load-time relocation? Problem 2: how does the OS protect memory from rogue programs? ???

Base and Bound Registers Goal: isolate jobs from one another, and from their placement in the machine memory. addresses are offsets from the job’s base address stored in a machine base register machine computes effective address on each reference initialized by OS when job is loaded machine checks each offset against job size placed by OS in a bound register

Base and Bound: Pros and Cons Pro: each job is physically contiguous simple hardware and software no need for load-time relocation of linked addresses OS may swap or move jobs as it sees fit Con: memory allocation is a royal pain job size is limited by available memory

Variable Partitioning Variable partitioning is the strategy of parking differently sized cars along a street with no marked parking space dividers. Wasted space from external fragmentation

Fixed Partitioning Wasted space from internal fragmentation

Completing a VM Reference raise exception probe page table load TLB probe TLB access physical memory access valid? page fault? signal process allocate frame page on disk? fetch from disk zero-fill load TLB start here MMU OS

Demand Paging and Page Faults OS may leave some virtual-physical translations unspecified. mark the pte for a virtual page as invalid If an unmapped page is referenced, the machine passes control to the kernel exception handler (page fault). passes faulting virtual address and attempted access mode Handler initializes a page frame, updates pte, and restarts. If a disk access is required, the OS may switch to another process after initiating the I/O. Page faults are delivered at IPL 0, just like a system call trap. Fault handler executes in context of faulted process, blocks on a semaphore or condition variable awaiting I/O completion.

Where Pages Come From text data BSS user stack args/env kernel data file volume with executable programs Fetches for clean text or data are typically fill-from-file. Modified (dirty) pages are pushed to backing store (swap) on eviction. Paged-out pages are fetched from backing store when needed. Initial references to user stack and BSS are satisfied by zero-fill on demand.

Faults Faults are similar to system calls in some respects: Faults occur as a result of a process executing an instruction. Fault handlers execute on the process kernel stack; the fault handler may block (sleep) in the kernel. The completed fault handler may return to the faulted context. But faults are different from syscall traps in other respects: Syscalls are deliberate, but faults are “accidents”. divide-by-zero, dereference invalid pointer, memory page fault Not every execution of the faulting instruction results in a fault. may depend on memory state or register contents

Options for Handling a Fault (1) 1. Some faults are handled by “patching things up” and returning to the faulted context. Example: the kernel may resolve an address fault (virtual memory fault) by installing a new virtual-physical translation. The fault handler may adjust the saved PC to re-execute the faulting instruction after returning from the fault. 2. Some faults are handled by notifying the process that the fault occurred, so it may recover in its own way. Fault handler munges the saved user context (PC, SP) to transfer control to a registered user-mode handler on return from the fault. Example: Unix signals or Microsoft NT user-mode Asynchronous Procedure Calls (APCs).

Options for Handling a Fault (2) 3. The kernel may handle unrecoverable faults by killing the user process. Program fault with no registered user-mode handler? Destroy the process, release its resources, maybe write the memory image to a file, and find another ready process/thread to run. In Unix this is the default action for many signals (e.g., SEGV). 4. How to handle faults generated by the kernel itself? Kernel follows a bogus pointer? Divides by zero? Executes an instruction that is undefined or reserved to user mode? These are generally fatal operating system errors resulting in a system crash, e.g., panic()!

Issues for Paged Memory Management The OS tries to minimize page fault costs incurred by all processes, balancing fairness, system throughput, etc. (1) fetch policy: When are pages brought into memory? prepaging: reduce page faults by bring pages in before needed clustering: reduce seeks on backing storage (2) replacement policy: How and when does the system select victim pages to be evicted/discarded from memory? (3) backing storage policy: Where does the system store evicted pages? When is the backing storage allocated? When does the system write modified pages to backing store?