39
1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

Embed Size (px)

Citation preview

Page 1: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

1

Principles of Virtual Memory

Virtual Memory, Paging, Segmentation

Page 2: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

2

Overview

1. Virtual Memory2. Paging3. Segmentation4. Combined Segmentation and Paging5. Bibliography

Page 3: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

3

1. Virtual Memory

1.1 Why Virtual Memory (VM)?1.2 What is VM ? 1.3 The Mapping Process1.4 Terms & Definitions1.5 The Principle of Locality1.6 VM: Features1.7 VM: Advantages1.8 VM: Disadvantages1.9 VM: Implementation

Page 4: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

4

1.1 Why Virtual Memory (VM)?

Shortage of memory Efficient memory management

needed

OS

Process 3

Process 1

Process 2

Process 4

Memory

Process may be too big for physical memory

More active processes than physical memory can hold

Requirements of multiprogramming

Efficient protection scheme Simple way of sharing

Page 5: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

5

1.2 What is VM?Program:

....

Mov AX, 0xA0F4

....0xA0F4

0xC0F4MappingUnit

(MMU)

Virtual Memory

Physical Memory

Virtual Address

Physical

Address

Table(one perProcess)

„Piece“ of Virtual

Memory„Piece“ of Physical Memory

Note: It does not matter at which physical address a „piece“ of VM is placed, since the corresponding addresses are mapped by the mapping unit.

Page 6: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

6MMU

1.3 The Mapping Process

Usually every process has its own mapping table own virtual address space (assumed from now on)

virtualaddress

piecein physicalmemory?

memoryaccess fault

OS brings„piece“ infrom HDD

physicaladdress

OS adjustsmapping

table

translateaddress

yes

check using mapping table

Not every „piece“ of VM has to be present in PM

„Pieces“ may be loaded from HDD as they are referenced

Rarely used „pieces“ may be discarded or written out to disk ( swapping)

Page 7: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

7

1.4 Terms & Notions

Virtual memory (VM) is Not a physical device but an abstract concept Comprised of the virtual address spaces (of all

processes)

Virtual address space (VAS) (of one process) Set of visible virtual addresses (Some systems may use a single VAS for all

processes)

Resident set Pieces of a process currently in physical memory

Working set Set of pieces a process is currently working on

Page 8: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

8

1.5 The Principle of Locality

Principle of Locality: Memory references within a process tend to cluster Working set should be part of the resident set to

operate efficiently (else: frequent memory access faults) honor the principle of locality to achieve this

repeated references:

single jumps:

working set:

initializationdata

initializationcode

early phase of process lifetime

code 1 code 2 data

main phase of process lifetime

finalizationcode

final phase of process lifetime

Page 9: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

9

1.6 VM: FeaturesSwapping

Danger: Thrashing: „Piece“ just swapped out is immediately requested

again System swaps in/out all the time, no real work is

done

Thus: „piece“ for swap out has to be chosen carefully

Keep track of „piece“ usage („age of piece“) Hopefully „piece“ used frequently lately will be used

again in near future (principle of locality!)

lack ofmemory

find rarelyused „piece"

adjustmapping table

„piece“modified?

save HDDlocation

of „piece“

discard„piece“

no

write „piece“out to disk

yesno need to swap

out complete process!!!

Page 10: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

10

1.6 VM: FeaturesProtection

Each process has its own virtual address space Processes invisible to each other Process cannot access another processes memory

MMU checks protection bits on memory access (during address mapping)

„Pieces“ can be protected from being written to or being executed or even being read

System can distinguish different protection levels (user / kernel mode)

Write protection can be used to implement copy on write ( Sharing)

Page 11: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

11

1.6 VM: FeaturesSharing

„Pieces“ of different processes mapped to one single „piece“ of physical memory

Allows sharing of code (saves memory), e.g. libraries Copy on write: „piece“ may be used by several

processes until one writes to it (then that process gets its own copy)

Simplifies interprocess-communication (IPC)

Piece 2

Piece 1

Virtual memoryProcess 1

Piece 0

Piece 1

Piece 2

Piece 0

Physical memory

sharedmemor

y

Virtual memoryProcess 2

Piece 1

Piece 0

Piece 2

Page 12: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

12

1.7 VM: Advantages (1)

VM supports Swapping

Rarely used „pieces“ can be discarded or swapped out „Piece“ can be swapped back in to any free piece of

physical memory large enough, mapping unit translates addresses

Protection Sharing

Common data or code may be shared to save memory

Process need not be in memory as a whole No need for complicated overlay techniques (OS

does job) Process may even be larger than all of physical

memory Data / code can be read from disk as needed

Page 13: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

13

Code can be placed anywhere in physical memory without relocation (adresses are mapped!)Increased cpu utilization

more processes can be held in memory (in part) more processes in ready state

(consider: 80% HDD I/O wait time not uncommon)

1.7 VM: Advantages (2)

Page 14: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

14

1.8 VM: Disadvantages

Memory requirements (mapping tables)Longer memory access times (mapping table lookup)

Can be improved using TLB

Page 15: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

15

1.9 VM: Implementation

VM may be implemented using Paging Segmentation Combination of both

Note:Everything said in the first chapter still holds for the following chapters!

Page 16: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

16

2. Paging

2.1 What is Paging?2.2 Paging: Implementation2.3 Paging: Features2.4 Paging: Advantages2.5 Paging: Disadvantages2.6 Summary: Conversion of a Virtual Address

Page 17: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

17

2.1 What is Paging?

Page 7

Page 5Page 4Page 3Page 2Page 1

Page 6

Virtual memory(divided into equal

size pages)

Page 00x00

Page Table(one per process,

one entry per page

maintained by OS)

Page 7

Page 5Page 4Page 3Page 2Page 1

Page 6

Page 0

v

v

v

Frame 0

Frame 1

Frame 3

Frame 0Frame 1Frame 2Frame 3

Physical memory(divided into equal size page frames)

0x00v

v

v

Page 18: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

18

2.2 Paging: Implementation Typical Page Table Entry

Page Frame #

executex

writew

readr

r w x

validv

r w x v

referencedre

v re

modifiedm

re m

shareds

m s

caching disabledc

s c

super-pagesu

c su

process idpid

su pid

guard data

(extended) guard

gd

g

pid g gdg gd otherother

Page 19: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

19

2.2 Paging: ImplementationSinglelevel Page Tables

0x140x2

Virtual addressPage # Offset

Physical address

0x14Offset

0x8Frame #

Problem: Page tables can get very large, e.g. 32 bit address space,

4KB pages 2^20 entries per process 4MB at 4B per entry64 bit 16777216 GB page table!!!!

Page Table BaseRegister (PTBR)

Page Table

0x8

...

0x0 * L0x1 * L0x2 * L

L : size of entry

one entry per pageone table per

process

Page 20: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

20

2.2 Paging: ImplementationMultilevel Page Tables

OffsetPage #1Page #2Page #3

Page Directory

Page MiddleDirectory

Page Table

Page Frame # Offset

OffsetFrame #

Oversized Super-Page

v=0

not all need be present

savesmemor

y

table size can be

restricted to one page

Page 21: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

21

2.2 Paging: ImplementationInverted Page Tables

0x140xA

Virtual addressPage # Offset

0x14

Physical addressOffset

0x2Frame #

Problem: Additional information about pages not presently in memory

must still be kept somehow (e.g. normal page tables on HDD)

hash table one entry per frame

one table for all processes

Inverted Page Table

0xA PID

...

hashfkt.

0x00x1

Page 22: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

22

Page Directory(guarded)

2.2 Paging: ImplementationGuarded Page Tables (1)

0xA 0x5 0x140x2Virtual addressPage

#1OffsetPage

#2Page #3

Page MiddleDirectory

Page Table

Frame #

0x3B2

only one valid entry per table

0xA5 0x3B20x8

page faultno

yes=

Frame # or page table

base

guard

guardlength

0x14

Physical addressOffset

table not needed, if

guard in place

Page 23: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

23

2.2 Paging: ImplementationGuarded Page Tables (2)

Guarded page tables especially interesting if hardware offers only TLB (zero-level paging, MIPS)

OS has total flexibility, may use Different sizes of pages and page tables (all powers of

2 ok) and as many levels as desired Guarded page tables Inverted page tables

Optimization: guarded table entry will usually not contain guard and guard length but equivalent informationNote that handling of protection is to be modified

Page 24: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

24VM

2.3 Paging: FeaturesPrepaging

Process requests consecutive pages (or just one) OS loads following pages into memory as well

(expecting they will also be needed)Saves time when large contiguous structures are used (e.g. huge arrays)Wastes memory and time case pages not neededreferenced by

process

prepaged by OS

Page 25: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

25

2.3 Paging: FeaturesDemand Paging

On process startup only first page is loaded into physical memoryPages are then loaded as referencedSaves memory But: may cause frequent page faults until process has its working set in physical memory.

OS may adjust its policy (demand / prepaging) dependent on

Available free physical memory Process types and history

Page 26: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

26

2.3 Paging: FeaturesCheap Memory Allocation

No search for large enough a piece of PM necessary

Any requested amount of memory is divided into pages can be distributed over the available frames

OS keeps a list of free frames If memory is requested the first frame is taken

16 4

Linked list offree frames

Page 1Page 2

Process started, requiring 6KB(4KB pages)

Frame 0Frame 1Frame 2Frame 3Frame 4Frame 5Frame 6Frame 7

PMFrame 0Frame 1Frame 2Frame 3Frame 4Frame 5Frame 6Frame 7

PM

4

Page 1Page 2

Page 27: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

27

Processrequires memory

Paging VM system Non-Paging VM system

2.3 Paging: FeaturesSimplified Swapping

Process requires 3 frames

® swap out 3 most seldomly used pages

Swapping out the 3 most seldomly used „pieces“ will not work Swap algo must try to create free pieces as big as possible (costly!)

1 „piece“3 pages

PMPM

rarely

used

Page 28: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

28

2.4 Paging: Advantages

Allocating memory is easy and cheap Any free page is ok, OS can take first one out of list it

keeps

Eliminates external fragmentationData (page frames) can be scattered all over PM pages are mapped appropriately anywayAllows demand paging and prepagingMore efficient swapping

No need for considerations about fragmentation Just swap out page least likely to be used

Page 29: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

29

2.5 Paging: Disadvantages

Longer memory access times (page table lookup)

Can be improved using TLB Guarded page tables Inverted page tables

Memory requirements (one entry per VM page) Improve using

Multilevel page tables and variable page sizes (super-pages)

Guarded page tables Page Table Length Register (PTLR) to limit virtual

memory size

Internal fragmentation Yet only an average of about ½ page per contiguous

address range

Page 30: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

30OS

Hardware

2.6 Summary: Conversion ofa Virtual Address

Virtual address

Physicaladdress

no protectionfault

referencelegal?

copy on write?

HDD I/O complete:interrupt

memoryfull?

updatepage table

process intoready state

copypage

process intoblocking state

yes

pagefault

no

exception to process

no

TLB

page table

miss

page inmem?

hit

accessrights?

yes

update TLB

yes

exception to process

no

swap outa page

yes

HDD I/O read req.

no

yes

bring in pagefrom HDD!

process intoblocking state

Page 31: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

31

3. Segmentation

3.1 What is Segmentation?3.2 Segmentation: Advantages3.3 Segmentation: Disadvantages

Page 32: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

32

MMU

STBR

STLR

Base

Limit

OtherSegment table

3.1 What is Segmentation?

Seg 1(code) Seg 2

(data)Seg 3(stack)

Virtual memory

Segment # Offsetvirtual address

memoryaccess fault

no

Physicalmemory

Seg 2(data)

Seg 1(code)

Seg 3(stack)

0x00

as in paging:valid,

modified,protection,

etc.

offset <limit ?

External fragmentatio

n

Segment Base + Offsetphysical address

yes

Page 33: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

33

3.2 Segmentation: Advantages

As opposed to paging: No internal fragmentation (but: external

fragmentation) May save memory if segments are very small and

should not be combined into one page (e.g. for reasons of protection)

Segment tables: only one entry per actual segment as opposed to one per page in VM

Average segment size >> average page size less overhead (smaller tables)

Page 34: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

34

3.3 Segmentation: Disadvantages

External fragmentationCostly memory management algorithms

Segmentation: find free memory area big enough (search!)

Paging: keep list of free pages, any page is ok (take first!)

Segments of unequal size not suited as well for swapping

Page 35: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

35

4. Combined Segmentationand Paging (CoSP)

4.1 What is CoSP?4.2 CoSP: Advantages4.3 CoSP: Disadvantages

Page 36: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

36

limit base

Segment Table

4.1 What is CoSP?

Page Directorysize limited by

segment number

size limited by segment

limit

OffsetSeg # Page #1Page #2

Virtual Address

Page Table

Page Frame # Offset

Physical Address

not all need be present

Page 37: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

37

4.2 CoSP: Advantages

Reduces memory usage as opposed to pure paging

Page table size limited by segment size Segment table has only one entry per actual

segment

Simplifies handling protection and sharing of larger modules (define them as segments) Most advantages of paging still hold

Simplifies memory allocation Eliminates external fragmentation Supports swapping, demand paging, prepaging etc.

Page 38: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

38

4.3 CoSP: Disadvantages

Internal fragmentation Yet only an average of about ½ page per contiguous

address range

Page 1Page 2

Process requests a 6KB address range(4KB pages)

internal fragmentati

on

Page 39: 1 Principles of Virtual Memory Virtual Memory, Paging, Segmentation

39

The End