Alan L. Cox alc@cs.rice.edu Virtual Memory Alan L. Cox alc@cs.rice.edu Some slides adapted from CMU 15.213 slides.

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.
Instructors: Randy Bryant and Dave O’Hallaron
Today Virtual memory (VM) Overview and motivation
Virtual Memory Nov 27, 2007 Slide Source: Topics Motivations for VM Address translation Accelerating translation with TLBs class12.ppt.
P6/Linux Memory System Oct. 31, 2002
Virtual Memory October 29, 2007 Topics Address spaces Motivations for virtual memory Address translation Accelerating translation with TLBs class16.ppt.
Virtual Memory May 19, 2008 Topics Motivations for VM Address translation Accelerating translation with TLBs EECS213.
Carnegie Mellon 1 Virtual Memory: Concepts / : Introduction to Computer Systems 16 th Lecture, Oct. 21, 2014 Instructors: Greg Ganger, Greg.
1 Virtual Memory: Systems Level Andrew Case Slides adapted from jinyang Li, Randy Bryant and Dave O’Hallaron.
Virtual Memory: Concepts
Virtual & Dynamic Memory Management Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
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)
Carnegie Mellon /18-243: Introduction to Computer Systems Instructors: Bill Nace and Gregory Kesden (c) All Rights Reserved. All work.
Instructors: Majd Sakr and Khaled Harras
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!”
Carnegie Mellon 1 Virtual Memory: Systems / : Introduction to Computer Systems 17 th Lecture, Oct. 27, 2011 Instructors: Dave O’Hallaron,
University of Washington Roadmap 1 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
Virtual Memory Topics Address spaces Motivations for virtual memory Address translation Accelerating translation with TLBs.
Virtual Memory October 14, 2008 Topics Address spaces Motivations for virtual memory Address translation Accelerating translation with TLBs lecture-14.ppt.
CSE 153 Design of Operating Systems Winter 2015 Lecture 11: Paging/Virtual Memory Some slides modified from originals by Dave O’hallaron.
1 Virtual Memory (I). 2 Outline Physical and Virtual Addressing Address Spaces VM as a Tool for Caching VM as a Tool for Memory Management VM as a Tool.
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
1 Virtual Memory. 2 Outline Virtual Space Address translation Accelerating translation –with a TLB –Multilevel page tables Different points of view Suggested.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Virtual Memory: Systems CENG331 - Computer Organization.
Carnegie Mellon 1 Virtual Memory: Systems / : Introduction to Computer Systems 17 th Lecture, Mar. 19, 2015 Instructors: Seth Copen Goldstein,
University of Washington Indirection in Virtual Memory 1 Each process gets its own private virtual address space Solves the previous problems Physical.
Virtual Memory: Systems
Virtual Memory Alan L. Cox Some slides adapted from CMU slides.
Virtual Memory Samira Khan Apr 25, 2017.
Section 9: Virtual Memory (VM)
Virtual Memory: Systems
Section 9: Virtual Memory (VM)
Today How was the midterm review? Lab4 due today.
Virtual Memory: Concepts CENG331 - Computer Organization
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
Virtual Memory: Systems
CS 105 “Tour of the Black Holes of Computing!”
ECE Dept., University of Toronto
Memory System Case Studies Oct. 13, 2008
Pentium/Linux Memory System
Instructors: Majd Sakr and Khaled Harras
Pentium III / Linux Memory System April 4, 2000
Virtual Memory.
Virtual Memory Nov 27, 2007 Slide Source:
Instructor: Phil Gibbons
Virtual Memory: Systems CSCI 380: Operating Systems
Virtual Memory II CSE 351 Winter 2018
CS 105 “Tour of the Black Holes of Computing!”
CS 105 “Tour of the Black Holes of Computing!”
CSE 153 Design of Operating Systems Winter 2019
CSE 153 Design of Operating Systems Winter 2019
Instructor: Phil Gibbons
Presentation transcript:

Alan L. Cox alc@cs.rice.edu Virtual Memory Alan L. Cox alc@cs.rice.edu Some slides adapted from CMU 15.213 slides

Objectives Be able to explain the rationale for VM Be able to explain how VM is implemented Be able to translate virtual addresses to physical addresses Be able to explain how VM benefits fork() and exec() Cox Virtual Memory

Virtual Memory Programs refer to virtual memory addresses movl (%ecx),%eax Conceptually very large array of bytes Each byte has its own address System provides address space private to particular “process” Compiler and run-time system allocate VM Where different program objects should be stored All allocation within single virtual address space 00∙∙∙∙∙∙0 FF∙∙∙∙∙∙F Cox Virtual Memory

Problem 1: How Does Everything Fit? 64-bit addresses: 16 Exabyte (16 billion GB!) Physical main memory: Few Gigabytes ? And there are many processes …. Cox Virtual Memory

Problem 2: Memory Management Physical main memory Process 1 Process 2 Process 3 … Process n stack heap .text .data … What goes where? x Cox Virtual Memory

Problem 3: How To Protect Physical main memory Process i Process j Problem 4: How To Share? Physical main memory Process i Process j Cox Virtual Memory

Solution: Indirection “All problems in computer science can be solved by another level of indirection... Except for the problem of too many layers of indirection.” – David Wheeler Virtual memory mapping Process 1 Physical memory Virtual memory Process n Each process gets its own private memory space Solves the previous problems Cox Virtual Memory

Address Spaces Virtual address space: Set of N = 2n virtual addresses {0, 1, 2, 3, …, N-1} Physical address space: Set of M = 2m physical addresses {0, 1, 2, 3, …, M-1} Clear distinction between data (bytes) and their attributes (addresses) Each object can have multiple addresses Every byte in main memory: one physical address, one (or more) virtual addresses Cox Virtual Memory

A System Using Physical Addressing Main memory 0: 1: 2: Physical address (PA) 3: CPU 4: 5: 6: 7: 8: ... M-1: Data word Used in “simple” systems like embedded microcontrollers in devices like cars, elevators, and digital picture frames Cox Virtual Memory

A System Using Virtual Addressing Main memory 0: CPU Chip 1: 2: Virtual address (VA) Physical address (PA) 3: CPU MMU 4: 5: 6: 7: 8: ... M-1: Data word Used in all modern desktops, laptops, workstations Cox Virtual Memory

Why Virtual Memory (VM)? Efficient use of limited main memory (RAM) Use RAM as a cache for parts of a virtual address space some non-cached parts stored on disk some (unallocated) non-cached parts stored nowhere Keep only active areas of virtual address space in memory transfer data back and forth as needed Simplifies memory management for programmers Each process gets the same full, private linear address space Isolates address spaces One process can’t interfere with another’s memory because they operate in different address spaces User process cannot access privileged information different sections of address spaces have different permissions Cox Virtual Memory

Outline Virtual memory (VM) Overview and motivation VM as tool for caching VM as tool for memory management VM as tool for memory protection Address translation mmap(), fork(), exec() Cox Virtual Memory

Disk VM as a Tool for Caching Virtual memory: array of N = 2n contiguous bytes think of the array (allocated part) as being stored on disk Physical main memory (DRAM) = cache for allocated virtual memory Basic unit: page; size = 2p Disk Virtual memory Physical memory VP 0 Unallocated VP 1 Cached Empty PP 0 Uncached PP 1 Unallocated Empty Cached Uncached Empty Cached PP 2m-p-1 2m-1 VP 2n-p-1 Uncached 2n-1 Virtual pages (VP's) stored on disk Physical pages (PP's) cached in DRAM Cox Virtual Memory

Memory Hierarchy: Core 2 Duo Not drawn to scale L1/L2 cache: 64 B blocks ~4 MB ~4 GB ~500 GB L1 I-cache D-cache L2 unified cache Main Memory Disk 32 KB CPU Reg Throughput: 16 B/cycle 8 B/cycle 2 B/cycle 1 B/30 cycles Latency: 3 cycles 14 cycles 100 cycles millions Miss penalty (latency): 30x Miss penalty (latency): 10,000x Cox Virtual Memory

DRAM Cache Organization DRAM cache organization driven by the enormous miss penalty DRAM is about 10x slower than SRAM Disk is about 10,000x slower than DRAM For first byte, faster for next byte Consequences Large page size: at least 4-8 KB, sometimes 4 MB Fully associative Any VP can be placed in any PP Highly sophisticated replacement algorithms Too complicated and open-ended to be implemented in hardware Write-back rather than write-through Cox Virtual Memory

A System Using Virtual Addressing Main memory 0: CPU Chip 1: 2: Virtual address (VA) Physical address (PA) 3: CPU MMU 4: 5: 6: 7: 8: ... M-1: Data word Used in all modern desktops, laptops, workstations Cox Virtual Memory

Address Translation: Page Tables A page table is an array of page table entries (PTEs) that maps virtual pages to physical pages. Here: 8 VPs Per-process kernel data structure in DRAM Physical memory (DRAM) Physical page number or disk address VP 1 PP 0 Valid VP 2 PTE 0 null VP 7 1 VP 4 PP 3 1 1 Virtual memory (disk) null PTE 7 1 VP 1 Memory resident page table (DRAM) VP 2 VP 3 VP 4 VP 6 Cox Virtual Memory VP 7

Address Translation With a Page Table Virtual page number (VPN) Virtual page offset (VPO) Virtual address Page table base register (PTBR) Valid Physical page number (PPN) Page table Page table address for process Valid bit = 0: page not in memory (page fault) Physical page number (PPN) Physical page offset (PPO) Physical address Cox Virtual Memory

Page Hit Page hit: reference to VM word that is in physical memory (DRAM) Physical page number or disk address Virtual address Valid VP 1 PP 0 VP 2 PTE 0 null VP 7 1 VP 4 PP 3 1 1 Virtual memory (disk) null PTE 7 1 VP 1 Memory resident page table (DRAM) VP 2 VP 3 VP 4 VP 6 VP 7 Cox Virtual Memory

Page Miss Page miss: reference to VM word that is not in physical memory Physical memory (DRAM) Physical page number or disk address Virtual address Valid VP 1 PP 0 VP 2 PTE 0 null VP 7 1 VP 4 PP 3 1 1 Virtual memory (disk) null PTE 7 1 VP 1 Memory resident page table (DRAM) VP 2 VP 3 VP 4 VP 6 VP 7 Cox Virtual Memory

Handling Page Fault Page miss causes page fault (an exception) Physical memory (DRAM) Physical page number or disk address Virtual address Valid VP 1 PP 0 VP 2 PTE 0 null VP 7 1 VP 4 PP 3 1 1 Virtual memory (disk) null PTE 7 1 VP 1 Memory resident page table (DRAM) VP 2 VP 3 VP 4 VP 6 VP 7 Cox Virtual Memory

Handling Page Fault Page miss causes page fault (an exception) Page fault handler selects a victim to be evicted (here VP 4) Physical memory (DRAM) Physical page number or disk address Virtual address Valid VP 1 PP 0 VP 2 PTE 0 null VP 7 1 VP 4 PP 3 1 1 Virtual memory (disk) null PTE 7 1 VP 1 Memory resident page table (DRAM) VP 2 VP 3 VP 4 VP 6 VP 7 Cox Virtual Memory

Handling Page Fault Page miss causes page fault (an exception) Page fault handler selects a victim to be evicted (here VP 4) Physical memory (DRAM) Physical page number or disk address Virtual address Valid VP 1 PP 0 VP 2 PTE 0 null VP 7 1 VP 3 PP 3 1 1 Virtual memory (disk) null PTE 7 1 VP 1 Memory resident page table (DRAM) VP 2 VP 3 VP 4 VP 6 VP 7 Cox Virtual Memory

Handling Page Fault Page miss causes page fault (an exception) Page fault handler selects a victim to be evicted (here VP 4) Offending instruction is restarted: page hit! Physical memory (DRAM) Physical page number or disk address Virtual address Valid VP 1 PP 0 VP 2 PTE 0 null VP 7 1 VP 3 PP 3 1 1 Virtual memory (disk) null PTE 7 1 VP 1 Memory resident page table (DRAM) VP 2 VP 3 VP 4 VP 6 VP 7 Cox Virtual Memory

Servicing a Page Fault (1) Processor signals disk controller Read block of length P starting at disk address X and store starting at memory address Y (2) Read occurs Direct Memory Access (DMA) Under control of I/O controller (3) Controller signals completion Interrupts processor OS resumes suspended process (1) Initiate Block Read Processor Reg (3) Read Done Cache Memory-I/O bus (2) DMA Transfer I/O controller Memory Disk Disk Cox Virtual Memory

Why does it work? Locality Virtual memory works because of locality At any point in time, programs tend to access a set of active virtual pages called the working set Programs with better temporal locality will have smaller working sets If (working set size < main memory size) Good performance for one process after initial compulsory misses If ( SUM(working set sizes) > main memory size ) Thrashing: Performance meltdown where pages are swapped (copied) in and out continuously Cox Virtual Memory

Outline Virtual memory (VM) Overview and motivation VM as tool for caching VM as tool for memory management VM as tool for memory protection Address translation mmap(), fork(), exec() Cox Virtual Memory

VM as a Tool for Memory Management Memory allocation Each virtual page can be mapped to any physical page A virtual page can be stored in different physical pages at different times Sharing code and data among processes Map virtual pages to the same physical page (here: PP 6) Address translation Virtual Address Space for Process 1: Physical Address Space (DRAM) VP 1 VP 2 PP 2 ... N-1 (e.g., read-only library code) PP 6 Virtual Address Space for Process 2: PP 8 VP 1 VP 2 ... ... Cox Virtual Memory N-1 M-1

Simplifying Linking and Loading Memory invisible to user code Linking Each program has similar virtual address space Code, stack, and shared libraries always start at the same address 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 Kernel virtual memory 0xc0000000 User stack (created at runtime) %esp (stack pointer) Memory-mapped region for shared libraries 0x40000000 Run-time heap (created by malloc) Read/write segment (.data, .bss) Loaded from the executable file Read-only segment (.init, .text, .rodata) 0x08048000 Unused Cox Virtual Memory

Outline Virtual memory (VM) Overview and motivation VM as tool for caching VM as tool for memory management VM as tool for memory protection Address translation mmap(), fork(), exec() Cox Virtual Memory

VM as a Tool for Memory Protection Extend PTEs with permission bits Page fault handler checks these before remapping If violated, send process SIGSEGV (segmentation fault) kernel/supervisor mode needed Physical Address Space Process i: SUP READ WRITE Address VP 0: No Yes No PP 6 VP 1: No Yes Yes PP 4 PP 2 VP 2: Yes Yes Yes PP 2 • PP 4 PP 6 Process j: SUP READ WRITE Address PP 8 VP 0: No Yes No PP 9 PP 9 VP 1: Yes Yes Yes PP 6 VP 2: No Yes Yes PP 11 PP 11 Cox Virtual Memory

Outline Virtual memory (VM) Overview and motivation VM as tool for caching VM as tool for memory management VM as tool for memory protection Address translation mmap(), fork(), exec() Cox Virtual Memory

Address Translation: Page Hit 2 CPU Chip Cache/ Memory PTEA MMU 1 PTE CPU VA 3 PA 4 Data 5 1) Processor sends virtual address to MMU 2-3) MMU fetches PTE from page table in memory 4) MMU sends physical address to cache/memory 5) Cache/memory sends data word to processor Cox Virtual Memory

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 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 Cox Virtual Memory

Page Tables are often Multi-level in reality; accessing them is slow Given: 4KB (212) page size 48-bit address space 4-byte PTE Problem: Would need a 256 GB page table! 248 * 2-12 * 22 = 238 bytes Common solution Multi-level page tables Example: 2-level page table Level 1 table: each PTE points to a page table Level 2 table: each PTE points to a page (paged in and out like other data) Level 1 table stays in memory Level 2 tables paged in and out Level 1 Table ... Level 2 Tables Cox Virtual Memory

A Two-Level Page Table Hierarchy page tables Virtual memory VP 0 ... PTE 0 PTE 0 VP 1023 2K allocated VM pages for code and data ... PTE 1 VP 1024 PTE 1023 PTE 2 (null) ... PTE 3 (null) VP 2047 PTE 4 (null) PTE 0 Gap PTE 5 (null) ... PTE 6 (null) PTE 1023 6K unallocated VM pages PTE 7 (null) PTE 8 1023 null PTEs (1K - 9) null PTEs PTE 1023 1023 unallocated pages 1023 unallocated pages 1 allocated VM page for the stack VP 9215 Cox Virtual Memory ...

Translating with a k-level Page Table Virtual Address n-1 p-1 VPN 1 VPN 2 ... VPN k VPO Level 1 page table Level 2 page table Level k page table ... ... PPN m-1 p-1 PPN PPO Physical Address Cox Virtual Memory

Speeding up Translation with a TLB Page table entries (PTEs) are cached in L1 like any other memory word PTEs may be evicted by other data references PTE hit still incurs memory access delay Solution: 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 Cox Virtual Memory

Without TLB 1) Processor sends virtual address to MMU 2 CPU Chip Cache/ Memory PTEA MMU 1 PTE CPU VA 3 PA 4 Data 5 1) Processor sends virtual address to MMU 2-3) MMU fetches PTE from page table in memory 4) MMU sends physical address to cache/memory 5) Cache/memory sends data word to processor Cox Virtual Memory

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

iterations if a multi-level TLB Miss Might need multiple iterations if a multi-level page table is used CPU Chip TLB 4 2 PTE VPN Cache/ Memory MMU 1 3 CPU VA PTEA PA 5 Data 6 A TLB miss incurs an add’l memory access (the PTE) Fortunately, TLB misses are rare Cox Virtual Memory

Simple Memory System Example 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 VPN VPO Virtual Page Number Virtual Page Offset 11 10 9 8 7 6 5 4 3 2 1 PPN PPO Physical Page Number Physical Page Offset Cox Virtual Memory

Simple Memory System Page Table Only show first 16 entries (out of 256) Assume all other entries are invalid VPN PPN Valid VPN PPN Valid 00 28 1 08 13 1 01 – 09 17 1 02 33 1 0A 09 1 03 02 1 0B – 04 – 0C – 05 16 1 0D 2D 1 06 – 0E 11 1 07 – 0F 0D 1 Cox Virtual Memory

Simple Memory System TLB 16 entries 4-way associative TLBT TLBI 13 12 11 10 9 8 7 6 5 4 3 2 1 VPN VPO 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 – Cox Virtual Memory

Simple Memory System Cache 16 lines, 4-byte block size Physically addressed Direct mapped CT CI CO 11 10 9 8 7 6 5 4 3 2 1 PPN PPO Idx Tag Valid B0 B1 B2 B3 Idx Tag Valid B0 B1 B2 B3 19 1 99 11 23 11 8 24 1 3A 00 51 89 1 15 – – – – 9 2D – – – – 2 1B 1 00 02 04 08 A 2D 1 93 15 DA 3B 3 36 – – – – B 0B – – – – 4 32 1 43 6D 8F 09 C 12 – – – – 5 0D 1 36 72 F0 1D D 16 1 04 96 34 15 6 31 – – – – E 13 1 83 77 1B D3 7 16 1 11 C2 DF 03 F 14 – – – – Cox Virtual Memory

Address Translation Example #1 Virtual Address: 0x03D4 VPN ___ TLBI ___ TLBT ____ TLB Hit? __ Page Fault? __ PPN: ____ Physical Address CO ___ CI___ CT ____ Hit? __ Byte: ____ TLBT TLBI 13 12 11 10 9 8 7 6 5 4 3 2 1 1 1 1 1 1 1 VPN VPO 0x0F 3 0x03 Y N 0x0D CT CI CO 11 10 9 8 7 6 5 4 3 2 1 1 PPN PPO 0x5 0x0D Y 0x36 Cox Virtual Memory

Address Translation Example #2 Virtual Address: 0x0B8F VPN ___ TLBI ___ TLBT ____ TLB Hit? __ Page Fault? __ PPN: ____ Physical Address CO ___ CI___ CT ____ Hit? __ Byte: ____ TLBT TLBI 13 12 11 10 9 8 7 6 5 4 3 2 1 1 1 1 1 1 1 1 1 VPN VPO 0x2E 2 0x0B N Y TBD CT CI CO 11 10 9 8 7 6 5 4 3 2 1 PPN PPO Cox Virtual Memory

Address Translation Example #3 Virtual Address: 0x0020 VPN ___ TLBI ___ TLBT ____ TLB Hit? __ Page Fault? __ PPN: ____ Physical Address CO___ CI___ CT ____ Hit? __ Byte: ____ TLBT TLBI 13 12 11 10 9 8 7 6 5 4 3 2 1 1 VPN VPO 0x00 0x00 N N 0x28 CT CI CO 11 10 9 8 7 6 5 4 3 2 1 1 PPN PPO 0x8 0x28 N In DRAM, don’t know value Cox Virtual Memory

Outline Virtual memory (VM) Overview and motivation VM as tool for caching VM as tool for memory management VM as tool for memory protection Address translation mmap(), fork(), exec() Cox Virtual Memory

Memory Mapping Creation of new VM area done via “memory mapping” Create new vm_area_struct and page tables for area Area can be backed by (i.e., get its initial values from) : Regular file on disk (e.g., an executable object file) Initial page bytes come from a section of a file Nothing (e.g., .bss) First fault will allocate a physical page full of 0's (demand-zero) Once the page is written to (dirtied), it is like any other page Dirty pages are swapped back and forth between a special swap file. Key point: no virtual pages are copied into physical memory until they are referenced! Known as “demand paging” Crucial for time and space efficiency Cox Virtual Memory

User-Level Memory Mapping void *mmap(void *start, int len, int prot, int flags, int fd, int offset) len bytes start (or address chosen by kernel) len bytes offset (bytes) Disk file specified by file descriptor fd Process virtual memory Cox Virtual Memory

User-Level Memory Mapping void *mmap(void *start, int len, int prot, int flags, int fd, int offset) Map len bytes starting at offset offset of the file specified by file description fd, preferably at address start start: may be 0 for “pick an address” prot: PROT_READ, PROT_WRITE, ... flags: MAP_PRIVATE, MAP_SHARED, ... Return a pointer to start of mapped area (may not be start) Example: fast file-copy Useful for applications like Web servers that need to quickly copy files. mmap()allows file transfers without copying into user space. Cox Virtual Memory

mmap() Example: Fast File Copy /* mmap.c - a program that uses mmap to copy itself to stdout */ /* include <unistd.h>, <sys/mman.h>, <sys/stat.h>, and <fcntl.h> */ int main() { struct stat stat; int fd, size; char *bufp; /* open the file & get its size*/ fd = open("./mmap.c", O_RDONLY); fstat(fd, &stat); size = stat.st_size; /* map the file to a new VM area */ bufp = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); /* write the VM area to stdout */ write(STDOUT_FILENO, bufp, size); return 0; } Cox Virtual Memory

fork() Revisited To create a new process using fork(): Net result: Make copies of the old process’ page table, etc. The two processes now share all of their pages Copy-on-write Allows each process to have a separate address space without copying all of the virtual pages Make pages of writeable areas read-only Flag these areas as private “copy-on-write” in OS Writes to these pages will cause protection faults Fault handler recognizes copy-on-write, makes a copy of the page, and restores write permissions Net result: Processes have identical address spaces Copies are deferred until absolutely necessary Cox Virtual Memory

Read-only Code and Data exec() Revisited 0xFFFFFFFF User Stack Shared Libraries Heap Read/Write Data Read-only Code and Data Unused To load p using exec: Delete existing page tables, etc. Create new page tables, etc.: Stack/heap/.bss are anonymous, demand-zero Code and data is mapped to ELF executable file p Shared libraries are dynamically linked and mapped Set program counter to entry point in .text OS will swap in pages from disk as they are used 0xFFBEC000 demand-zero %sp 0xFF3DC000 .data .text libc.so brk demand-zero (.bss) .data .rodata .text 0x00010000 p 0x00000000 Cox Virtual Memory

Virtual Memory Supports many OS-related functions Process creation Initial Forking children Task switching Protection/sharing Combination of hardware & software implementation Software management of tables, page allocations Hardware access of tables Page fault when no entry Hardware enforcement of protection Protection fault when invalid access Cox Virtual Memory

Next Time Dynamic Memory Allocation Cox Virtual Memory