Title: Virtual Memory 1
1Chapter 13. Virtual Memory
- Introduction
- Demand Paging
- Hardware Requirements
- 4.3 BSD Virtual Memory
- 4.3 BSD Memory Management Operations
2Introduction
- 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
3Introduction (cont)
- Memory management in the Stone Age
- Software overlay
- Swapping
- Demand paging
- Segmentation
4Demand 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
5Demand Paging (cont)
- Physical memory management
- Memory protection
- Memory sharing
- Monitoring system load
- Other facilities
- Memory-mapped files
- Dynamically linked shared libraries
- etc.
6Demand Paging (cont)
uninitialized data pages zero-filled on first
access
dirty pages saved initialized data
main memory
swap area on disk
text and initialized data
executable file
subsequent faults on outswapped pages
stack and heap pages allocated on first access
7Demand Paging (cont)
address space map
backup store map
page fault
hardware address translations
Data
process virtual page number
physical page number
physical memory map
8Demand Paging (cont)
- Page replacement policies
- Local v.s. global replacement policy
- Locality of reference
- Concept of working set
- Least recently used (LRU) policy
9Hardware 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
10Hardware 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
11Hardware 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)
12Hardware 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
13Hardware 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
14Address 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
11
0
DIR
PAGE
OFFSET
virtual address
31
11
0
PFN
OFFSET
physical address
15Intel x86 Page Table Entry
31
12
6
5
2
1
0
PFN
D
A
U
W
P
PFN Page Frame Number D Dirty A Accessed
(Referenced) U User (0) / Supervisor (1) W Read
(0) / Write (1) P Present (valid)
164.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
174.3BSD (cont)
- Disk maps
- Describe the swap areas
- Resource maps
- Manage allocation of resources such as page
tables and swap space
184.3BSD Physical Memory
error buffer
nonpaged pool
paged pool
cmap (in nonpaged pool)
194.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
- lttype, owner, virtual page numbergt
- Text page cache
- Synchronization
204.3BSD Physical to Virtual address Translation
proc
cmap
type data owner VPN
page table
type data owner VPN
page table
text
214.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
224.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
234.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
244.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
254.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
264.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
274.3BSD Page Tables Entry
(a) VAX-11 page table entry format
31
26
20
0
Page Frame Number (PFN)
PROT
V
M
unused
valid
modified
(b) Ordinary page table entry
31
26
20
0
25
Page Frame Number (PFN)
PROT
V
M
0
valid
modified
fill-on-demand
fill-from-text (1) or zero-fill (0)
File System Block Number
PROT
V
1
F
31
26
23
0
(c) Fill-on-demand page table entry
284.3BSD Swap Space
- Per region dmap structure
data region
swap space
0
dmap
dmmin
2dmmin
4dmmin
294.3BSD Swap Space (cont)
- Text pages
- Once the page is brought into memory, the
fill-on-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
304.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
314.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( )
324.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
33Pagein( ) (1/2)
start
Fill on demand?
No
Yes
No
PFN 0?
(next page)
Yes
in transit text page?
Yes
text page onhash queue?
Yes
set wanted flag
No
(next page)
No
on free list?
No
allocate new page
sleep on text struct
Yes
take page off free list
read page from swap
start over when woken up
set valid bit
34Pagein( ) (2/2)
Fill on demand?
Yes
(previous page)
Yes
zero-fill?
(previous page)
No
allocate new page
Yes
text page onhash queue?
fill it with zeros
No
take page off free list
Yes
page in buffer cache?
flush cache copy to disk
No
Read page from file
mark page modified
354.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
364.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
374.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
38Analysis 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
39Analysis 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