Chapter 13. Virtual Memory • Introduction • Demand Paging • Hardware Requirements • 4.3 BSD Virtual Memory • 4.3 BSD Memory Management Operations Virtual Memory 1 Introduction • Memory management unit (MMU) – Responsible for getting data to and from main memory • Virtual memory – The notion of an address space as distinct from memory locations – Translation tables – Page-based Virtual Memory 2 Introduction (cont) • Memory management in the Stone Age – Software overlay – Swapping – Demand paging – Segmentation Virtual Memory 3 Demand Paging • Primary goal – To allow a process to run in a virtual address space – To perform translations from virtual to physical addresses transparently to the process • Functional requirements – Address space management • fork( ), exec( ) – Address translation • Virtual address Virtual page number + offset Virtual Memory 4 Demand Paging (cont) – Physical memory management – Memory protection – Memory sharing – Monitoring system load – Other facilities • Memory-mapped files • Dynamically linked shared libraries • etc. Virtual Memory 5 Demand Paging (cont) • Page swapping uninitialized data pages zero-filled on first access text and executable initialized data file main memory dirty pages saved initialized data subsequent faults on outswapped pages swap area on disk stack and heap pages allocated on first access Virtual Memory 6 Demand Paging (cont) • Translation maps address space map backup store map page fault process + virtual page number hardware address translations Data physical page number physical memory map Virtual Memory 7 Demand Paging (cont) • Page replacement policies – Local v.s. global replacement policy – Locality of reference – Concept of working set – Least recently used (LRU) policy Virtual Memory 8 Hardware Requirements • MMU – Translation of virtual addresses – Using page tables or TLBs • Page table entry – Physical page frame number – Protection information – a valid bit – a modified bit – a referenced bit Virtual Memory 9 Hardware Requirements (cont) • Page table – Hardware-prescribed – Located in main memory – MMU uses only the active tables, whose locations are loaded in h/w page table registers – Typically, on a uniprocessor, there are two active page tables - one for kernel and one for the currently running process – Paging the page table to reduce the required memory Virtual Memory 10 Hardware Requirements (cont) • Address translation may fail for three reasons – Bounds error – Validation error – Protection error • MMU caches – A high-speed cache that is searched before each memory access – Translation lookaside buffer (TLB) Virtual Memory 11 Hardware Requirements (cont) • Intel x86 – Unix implementations on the x86 hides the notion of segmentation from user processes, who see a flat address space – Two-level page table – 4k byte page size – Control register CR3 stores the physical page number of the current page directory – CR3 register needs to be reset on each context switch Virtual Memory 12 Hardware Requirements (cont) – Page table entry • Physical page number, protection field • Valid, referenced, and modified bit – x86 supports 4 privilege levels of which UNIX uses only two – The kernel runs in the innermost ring, which is the most privileged – User code runs in the outermost, least privileged ring Virtual Memory 13 Address Translation on Intel x86 page directory of current process CR3 31 11 0 one of the page tables of current process PFN 31 11 0 PFN 31 21 DIR PAGE 11 0 OFFSET virtual address 31 11 PFN 0 OFFSET physical address Virtual Memory 14 Intel x86 Page Table Entry 31 12 PFN PFN D A U W P 6 5 2 1 D A U W P 0 Page Frame Number Dirty Accessed (Referenced) User (0) / Supervisor (1) Read (0) / Write (1) Present (valid) Virtual Memory 15 4.3BSD • Target platform: VAX-11 – Several BSD-based implementations emulate the VAX memory architecture in software, including its address space layout and page table entry format • Core map – Describes physical memory • Page tables – Describe virtual memory Virtual Memory 16 4.3BSD (cont) • Disk maps – Describe the swap areas • Resource maps – Manage allocation of resources such as page tables and swap space Virtual Memory 17 4.3BSD Physical Memory nonpaged pool paged pool error buffer cmap[ ] (in nonpaged pool) Virtual Memory 18 4.3BSD Physical Memory (cont) • Core map: struct cmap – Is a kernel data structure, allocated at boot time and resident in the nonpaged pool – One entry for each frame in the paged pool • Core map entry – Name • <type, owner, virtual page number> – Text page cache – Synchronization Virtual Memory 19 4.3BSD Physical to Virtual address Translation proc[ ] cmap[ ] type = data owner VPN … page table type = data owner VPN … page table text[ ] Virtual Memory 20 4.3BSD Address Space • Uses VAX-11 address space model – 32-bit machine with 512-byte page size – 4 Giga byte address space is divided into four regions of equal size – P0 • Program region • Text and data section of the process Virtual Memory 21 4.3BSD Address Space (cont) – P1 • Control region • User stack, u area, kernel stack – S0 • System region • Kernel text and data – 4th region • Reserved and not supported by current VAX h/w Virtual Memory 22 4.3BSD Page Tables • Single system page table – Map the kernel text and data • Each process has two page tables to map its P0 and P1 – Mapped by a set of contiguous PTEs in the Userptmap section of the system page table • State of a particular page of a process – Resident • The page is in physical memory, and the page table entry contains its physical page frame number Virtual Memory 23 4.3BSD Page Tables (cont) – Fill-on-demand • Fill-from-text: Text and initialized data pages are read in from the executable file upon first access • Zero-fill: Uninitialized data, heap, and stack pages are created and filled with zero when required – Outswapped • These pages may be recovered from their swap area locations Virtual Memory 24 4.3BSD Page Tables (cont) • Kernel maintains information about all nonresident pages – For swapped out pages • Kernel must stores their location on the swap device – For zero-fill pages • Kernel only needs to recognize them as such – For fill-from-text pages • Kernel must determine their location in the filesystem Virtual Memory 25 4.3BSD Page Tables (cont) • Page table entry – Since all nonresident pages have the valid bit clear, other fields can be replaced by other information that tracks these pages – Kernel maintains separate swap maps to locate those pages on the swap device Virtual Memory 26 4.3BSD Page Tables Entry (a) VAX-11 page table entry format 31 26 V PROT M valid 20 unused 0 Page Frame Number (PFN) modified (b) Ordinary page table entry 31 26 25 V PROT M 0 valid V 31 20 0 Page Frame Number (PFN) modified fill-on-demand fill-from-text (1) or zero-fill (0) PROT 1 F 26 File System Block Number 23 0 (c) Fill-on-demand page table entry Virtual Memory 27 4.3BSD Swap Space • Per region dmap structure swap space data region 0 dmmin dmap 2*dmmin 4*dmmin Virtual Memory 28 4.3BSD Swap Space (cont) • Text pages – Once the page is brought into memory, the fillon-demand PTE is overwritten by the page frame number – As a result, retrieving the page from the file involving recomputing its location and perhaps, accessing one or more indirect blocks – To avoid that, such pages are saved in swap as well Virtual Memory 29 4.3BSD Swap Space (cont) • U area holds the maps for the data and stack region • The text region swap map is part of the text structure Virtual Memory 30 4.3BSD Memory Management Operations • Process creation – Swap space, u area – Page tables • Kernel must allocate contiguous PTEs in Userptmap to map page tables for their process – Text region • The child is added to the list of processes sharing the text structure used by the parent – Data and Stack • Data and stack must be copied one page at a time • Copy-on-write, vfork( ) Virtual Memory 31 4.3BSD Memory Management Operations (cont) • Page table handling – Two types of page faults • validation, protection – Validation • No PTE for that page (bounds error) • PTE is marked invalid – Pagein( ) is called to handle the fault • The faulting virtual address PTE Virtual Memory 32 Pagein( ) (1/2) start No No Yes PFN == 0? in transit text page? Yes No text page on hash queue? set wanted flag No on free list? sleep on text struct Yes take page off free list start over when woken up Fill on demand? Yes (next page) Yes (next page) No allocate new page read page from swap set valid bit Virtual Memory 33 Pagein( ) (2/2) (previous page) Yes Fill on demand? zero-fill? Yes (previous page) No Yes take page off free list text page on hash queue? No Yes flush cache copy to disk allocate new page fill it with zeros page in buffer cache? No Read page from file mark page modified Virtual Memory 34 4.3BSD Memory Management Operations (cont) • Free page list – Ideally, to keep all garbage pages at the head of the free list, followed by some useful pages in LRU order – 4.3BSD replaces the least recently used policy by a not recently used policy • Uses referenced bit and two passes over each page • VAX-11 does not support a referenced bit in the h/w, so BSD simulates the referenced bit in software • Pagedaemon process is responsible for page replacement Virtual Memory 35 4.3BSD Memory Management Operations (cont) • Swapping – Problem of thrashing – Swapper process monitors the system load, and swap processes in and out when needed – Swapper will swap out a process in the following cases • Userptmap fragmentation • Memory shortfall • Inactive processes Virtual Memory 36 4.3BSD Memory Management Operations (cont) – Swapper performs the following task when swapping out a process • Allocates swap space for the u area, kernel stack, and page tables • Detach the process from its text region • Save the resident data and stack on swap • Release the system PTEs in Userptmap • Record the swap location of the u area in the proc structure Virtual Memory 37 Analysis of 4.3BSD • No support for execution of remote programs • No support for sharing of memory • vfork( ) is not a true substitute for fork( ) – The lack of copy-on-write hurts the performance of Ap that rely extensively on fork • No support for memory mapped files Virtual Memory 38 Analysis of 4.3BSD (cont) • Reserves enough swap space in advance to page out very single page in the process address space • No support for using swap space on remote nodes Virtual Memory 39