Download presentation
Presentation is loading. Please wait.
Published byMariah Neal Modified over 8 years ago
1
Virtual Memory Alan L. Cox alc@cs.rice.edu Some slides adapted from CMU 15.213 slides
2
Alan L. CoxVirtual Memory2 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
3
Alan L. CoxVirtual Memory3 Physical Addresses 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 CPU 0: 1: N-1: Memory Physical Addresses
4
Alan L. CoxVirtual Memory4 Virtual Addresses 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 0: 1: N-1: Memory Physical Addresses Virtual Addresses Address Translation
5
Alan L. CoxVirtual Memory5 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
6
Alan L. CoxVirtual Memory6 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
7
Alan L. CoxVirtual Memory7 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
8
Alan L. CoxVirtual Memory8 Motivation #1: DRAM a “Cache” for Disk Full address space is quite large: 32-bit addresses: ~4x10 9 bytes 64-bit addresses: ~16x10 18 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 2GB: ~$30-50 500 GB: ~$70 Disk DRAM
9
Alan L. CoxVirtual Memory9 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
10
Alan L. CoxVirtual Memory10 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) Data 243 17 105 0: 1: N-1: X Object Name Location D: J: X: 1 0 On Disk “Cache”Page Table
11
Alan L. CoxVirtual Memory11 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. CPU Memory Page Table Disk Virtual Addresses Physical Addresses CPU Memory Page Table Disk Virtual Addresses Physical Addresses Before fault After fault
12
Alan L. CoxVirtual Memory12 Servicing a Page Fault 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 Memory-I/O bus Processor Cache Memory I/O controller I/O controller Reg (2) DMA Transfer (1) Initiate Block Read (3) Read Done Disk
13
Alan L. CoxVirtual Memory13 Motivation #2: Memory Management Multiple processes can reside in physical memory How do we resolve address conflicts? What if two processes access something at the same address? User Stack Shared Libraries Heap Read/Write Data Read-only Code and Data Unused 0xFFFFFFFF %sp 0xFFBEC000 0xFF3DC000 0x00000000 0x00010000 brk
14
Alan L. CoxVirtual Memory14 Virtual Address Space for Process 1: Physical Address Space (DRAM) VP 1 VP 2 PP 2 Address Translation 0 0 N-1 0 M-1 PP 7 PP 10 (e.g., read/only library code) Separate Virtual Address Spaces Each process has its own virtual address space Operating system controls how virtual pages are assigned to physical memory... Virtual Address Space for Process 2: VP 1 VP 2
15
Alan L. CoxVirtual Memory15 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…
16
Alan L. CoxVirtual Memory16 Motivation #3: Protection Page table entry contains access rights Hardware enforces protection using exceptions Page Tables Process i: Physical AddrRead?Write? PP 9YesNo PP 4Yes XXXXXXX No VP 0: VP 1: VP 2: Process j: 0: 1: N-1: Memory Physical AddrRead?Write? PP 6Yes PP 9YesNo XXXXXXX No VP 0: VP 1: VP 2:
17
Alan L. CoxVirtual Memory17 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
18
Alan L. CoxVirtual Memory18 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
19
Alan L. CoxVirtual Memory19 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)
20
Alan L. CoxVirtual Memory20 mmap () Example: Fast File Copy /* mmap.c - a program that uses mmap to copy itself to stdout */ /* include,,, and */ 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; }
21
Alan L. CoxVirtual Memory21 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
22
Alan L. CoxVirtual Memory22 exec () Revisited 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 0xFFFFFFFF %sp 0xFFBEC000 0xFF3DC000 0x00000000 0x00010000 brk.rodata.text p demand-zero (.bss) demand-zero libc.so.data.text.data
23
Alan L. CoxVirtual Memory23 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
24
Alan L. CoxVirtual Memory24 Next Time Dynamic Memory Allocation
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.