1 / 24

Virtual Memory

Virtual Memory. Alan L. Cox alc@cs.rice.edu. Some slides adapted from CMU 15.213 slides. Virtual Memory. Integral to the process abstraction Provides memory protection and sharing Enables process isolation Simplifies memory management Simplifies memory allocation and sharing

clove
Download Presentation

Virtual Memory

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Virtual Memory • Integral to the process abstraction • Provides memory protection and sharing • Enables process isolation • Simplifies memory management • Simplifies memory allocation and sharing • Simplifies linking and loading • Provides increased memory capacity • Enables physical memory to be a cache for disk storage Virtual Memory

  3. Actual addresses of bytes in physical memory Physically addressed machines Addresses generated by the CPU are physical addresses Embedded systems, early PCs, etc. Addresses are the same for all processes Memory 0: Physical Addresses 1: CPU N-1: Physical Addresses Virtual Memory

  4. The name of a memory location within a process’ address space Virtually addressed machines Addresses generated by the CPU are virtual addresses These addresses must be translated into physical addresses by the hardware Most modern systems use virtual addresses OS manages different address spaces for each process CPU Virtual Addresses Memory Virtual Addresses 0: 1: Address Translation Physical Addresses N-1: Virtual Memory

  5. Virtual Memory • Addresses within a program do not directly name locations within the physical memory of the system • Each process has its own private address space • Hardware (through exceptions and address translation) and the kernel maintain the abstraction Virtual Memory

  6. Motivations for Virtual Memory • Use Physical DRAM as a Cache for the Disk • Process’ address space can exceed physical memory size • Sum of address spaces of multiple processes can exceed physical memory size • Simplify Memory Management • Multiple processes resident in main memory • Each process with its own address space • Only “active” code and data is actually in memory • Allocate more memory to process as needed • Provide Protection • One process can’t interfere with another • Because they operate in different address spaces • User process cannot access privileged information • Different sections of address spaces have different permissions Virtual Memory

  7. Caching • The use of a smaller, faster memory to hold a subset of data from a larger, slower memory • Cache policies • What should be stored in the cache? • Where should it be stored in the cache? • Cache management • Hardware managed (processor caches) • Software managed (operating system file cache) • HW/SW managed (virtual memory) • Effective because of locality • Small fraction of memory accessed frequently Virtual Memory

  8. 500 GB: ~$70 2GB: ~$30-50 DRAM Disk Motivation #1: DRAM a “Cache” for Disk • Full address space is quite large: • 32-bit addresses: ~4x109 bytes • 64-bit addresses: ~16x1018 bytes • Disk is ~100-200X cheaper than DRAM • 500GB of DRAM: ~$7,500/12,500 (desktop/server) • 500GB of disk: ~$70 • To access large amounts of data in a cost-effective manner, the bulk of the data must be stored on disk Virtual Memory

  9. Virtual and Physical Address Spaces • Virtual and physical address spaces divided into equal-sized blocks • Blocks are called “pages” (both virtual and physical) • Virtual pages may map to physical pages, but many virtual pages are unmapped • Pages are on disk • Pages are not valid Virtual Memory

  10. Object Name X Data 0: 243 D: 1: 17 J: • • • N-1: 105 X: Locating an Object in “Cache” • DRAM Cache • Each allocated page of virtual memory has an entry in a page table • Each process effectively has its own page table • OS manages page table entries • Mapping from virtual pages to physical pages • Hardware performs translation (may need help from OS) Page Table “Cache” Location 0 On Disk • • • 1 Virtual Memory

  11. Page Faults (“Cache Misses”) • What if an object is on disk (not in memory)? • Page table indicates virtual address not in memory • OS exception handler invoked • Moves data from disk into memory • Current process suspends, others can resume • OS has full control over placement, etc. Before fault After fault Memory Memory Page Table Page Table Virtual Addresses Physical Addresses Virtual Addresses Physical Addresses CPU CPU Disk Disk Virtual Memory

  12. Processor Signals I/O Controller Read block of length P starting at disk address X and store starting at memory address Y Read Occurs Direct Memory Access (DMA) Under control of I/O controller I/O Controller Signals Completion Interrupt processor OS may resume suspended process Disk Disk Servicing a Page Fault (1) Initiate Block Read Processor Reg (3) Read Done Cache Memory-I/O bus (2) DMA Transfer I/O controller Memory Virtual Memory

  13. Multiple processes can reside in physical memory How do we resolve address conflicts? What if two processes access something at the same address? Motivation #2: Memory Management 0xFFFFFFFF 0xFFBEC000 User Stack %sp 0xFF3DC000 Shared Libraries brk Heap Read/Write Data Read-only Code and Data 0x00010000 Unused 0x00000000 Virtual Memory

  14. Separate Virtual Address Spaces • Each process has its own virtual address space • Operating system controls how virtual pages are assigned to physical memory 0 Physical Address Space (DRAM) Address Translation Virtual Address Space for Process 1: 0 VP 1 PP 2 VP 2 ... N-1 (e.g., read/only library code) PP 7 Virtual Address Space for Process 2: 0 VP 1 PP 10 VP 2 ... M-1 N-1 Virtual Memory

  15. Per-process Virtual Address Spaces • Simplifies linking • Linker can use the same address space layout for each process (even though they will be loaded into different physical locations) • Simplifies sharing • OS can map the same physical page into different virtual pages in different processes • Simplifies allocation • Can allocate contiguous virtual pages (when more stack/heap is needed) without having to find contiguous physical pages • Simplifies loading • More later… Virtual Memory

  16. 0: Read? Write? Physical Addr 1: VP 0: VP 0: Yes No PP 9 VP 1: VP 1: Yes Yes PP 4 VP 2: VP 2: No No XXXXXXX • • • • • • • • • Read? Write? Physical Addr Yes Yes PP 6 N-1: Yes No PP 9 No No XXXXXXX • • • • • • • • • Motivation #3: Protection • Page table entry contains access rights • Hardware enforces protection using exceptions Page Tables Memory Process i: Process j: Virtual Memory

  17. VM Completes the Process Abstraction • Programmer’s View • Large “flat” address space • Can allocate large blocks of contiguous addresses • Processor “owns” machine • Has private address space • Unaffected by behavior of other processes • System View • User virtual address space created by mapping to set of physical pages • Need not be contiguous • Allocated dynamically • Hardware enforces protection during address translation • OS manages many processes simultaneously • Continually switching among processes • Especially when one must wait for resource • E.g., disk I/O to handle page fault Virtual Memory

  18. Memory Mapping • Creation of new VM area done via “memory mapping” • Create new OS data structures 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) • initial page bytes are zeros • 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 Virtual Memory

  19. 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 (usually NULL for don’t care) • prot: PROT_READ, PROT_WRITE, PROT_EXEC, etc. • flags: MAP_PRIVATE, MAP_SHARED, MAP_ANON, etc. • Return a pointer to the mapped area • Example: fast file copy • Useful for applications like Web servers that need to quickly copy files • mmap allows file transfers without copying into user memory (e.g., from the disk to the network) Virtual Memory

  20. 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; • } Virtual Memory

  21. fork() Revisited • To create a new process using fork(): • 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 Virtual Memory

  22. 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 User Stack Shared Libraries Heap Read/Write Data Read-only Code and Data Unused exec() Revisited 0xFFFFFFFF 0xFFBEC000 demand-zero %sp 0xFF3DC000 .data .text libc.so brk demand-zero (.bss) .data .rodata .text 0x00010000 p 0x00000000 Virtual Memory

  23. 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 Virtual Memory

  24. Next Time • Dynamic Memory Allocation Virtual Memory

More Related