39
Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Embed Size (px)

Citation preview

Page 1: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 1

Chapter 13. Virtual Memory

• Introduction

• Demand Paging

• Hardware Requirements

• 4.3 BSD Virtual Memory

• 4.3 BSD Memory Management Operations

Page 2: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 2

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

Page 3: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 3

Introduction (cont)

• Memory management in the Stone Age

– Software overlay

– Swapping

– Demand paging

– Segmentation

Page 4: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 4

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

Page 5: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 5

Demand Paging (cont)

– Physical memory management

– Memory protection

– Memory sharing

– Monitoring system load

– Other facilities

• Memory-mapped files

• Dynamically linked shared libraries

• etc.

Page 6: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 6

Demand Paging (cont)

executablefile

mainmemory

swaparea

on disk

text andinitialized data

uninitialized data pageszero-filled on first access

stack and heap pagesallocated on first access

dirty pages savedinitialized data

subsequent faults onoutswapped pages

• Page swapping

Page 7: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 7

Demand Paging (cont)• Translation maps

hardware address

translations

addressspace map

physicalmemory map

backupstore map

process+

virtual pagenumber

physicalpage

number

Data

page fault

Page 8: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 8

Demand Paging (cont)

• Page replacement policies

– Local v.s. global replacement policy

– Locality of reference

– Concept of working set

– Least recently used (LRU) policy

Page 9: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 9

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

Page 10: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 10

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

Page 11: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 11

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)

Page 12: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 12

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

Page 13: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 13

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

Page 14: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 14

Address Translation on Intel x86

CR3

PFN31 11 0

PFN31 11 0

DIR PAGE OFFSET31 21 11 0

PFN OFFSET31 11 0

page directory ofcurrent process

one of the pagetables of current

process

virtual address

physical address

Page 15: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 15

Intel x86 Page Table Entry

PFN

31 12 6

PFN Page Frame NumberD DirtyA Accessed (Referenced)U User (0) / Supervisor (1)W Read (0) / Write (1)P Present (valid)

D A U W P5 2 1 0

Page 16: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 16

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

Page 17: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 17

4.3BSD (cont)

• Disk maps

– Describe the swap areas

• Resource maps

– Manage allocation of resources such as page

tables and swap space

Page 18: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 18

4.3BSD Physical Memory

nonpagedpool

errorbuffer

pagedpool

cmap[ ](in nonpaged pool)

Page 19: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 19

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

Page 20: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 20

4.3BSD Physical to Virtual address Translation

type = dataownerVPN…

type = dataownerVPN…

cmap[ ]

proc[ ]

text[ ]

pagetable

page table

Page 21: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 21

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

Page 22: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 22

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

Page 23: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 23

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

Page 24: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 24

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

Page 25: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 25

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

Page 26: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 26

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

Page 27: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 27

Page Frame Number (PFN)PROT

31 26 20

V M unused

0

valid modified

Page Frame Number (PFN)PROT

31 26 20

V M

0

valid modified

(a) VAX-11 page table entry format

(b) Ordinary page table entry

File System Block NumberPROT

31 26 23

V

0

0

1 F

25

fill-on-demandfill-from-text (1) or zero-fill (0)

(c) Fill-on-demand page table entry

4.3BSD Page Tables Entry

Page 28: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 28

4.3BSD Swap Space

• Per region dmap structure

data region swap space

dmap0

dmmin

2*dmmin

4*dmmin

Page 29: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 29

4.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

Page 30: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 30

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

Page 31: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 31

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( )

Page 32: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 32

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

Page 33: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 33

Pagein( ) (1/2)start

Fill ondemand?

No Yes

(next page)PFN == 0?No

Yes

text page onhash queue?

(next page)

Yes

No

allocate new page

read pagefrom swap

in transittext page?

Noon free

list?

Yestake page off

free list

Yes

set wanted flag

sleep on text struct

start over whenwoken up

set valid bit

No

Page 34: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 34

Pagein( ) (2/2)

fill it with zeros

Fill ondemand?

Yes

zero-fill? Yes

allocate new page

mark pagemodified

text page onhash queue?

No

No

page in buffer cache?

No

Read pagefrom file

Yes

flush cachecopy to disk

Yes

take page offfree list

(previous page)

(previous page)

Page 35: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 35

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

Page 36: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 36

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

Page 37: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 37

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

Page 38: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 38

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

Page 39: Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations

Virtual Memory 39

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