memory unix tables 1

38
Virtual Memory in Unix Idan Shatz

Upload: anandprm

Post on 13-Nov-2014

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Memory Unix Tables 1

Virtual Memory in Unix

Idan Shatz

Page 2: Memory Unix Tables 1

Unix’s Method

� Ready?

Page 3: Memory Unix Tables 1

The Big Picture

Main (Physical) Memory

128MB

0 Frame 0

Frame 1

Frame 24GB

A process

0 Page 0

Page 1

Page 2

Page 3

Page n

4GB

A process

0 Page 0

Page 1

Page 2

Page 3

Page n

Frame m

Swap Device

1GB

0 block 0

block 1

block 2

block p

Page out

Page in

Page 4: Memory Unix Tables 1

The Unix Big Picture

Main (Physical) Memory

128MB

0 Frame 0

Frame 1

Frame 24GB

A process

0 Page 0

Page 1

Page 2

Page 3

Page n

4GB

A process

0 Page 0

Page 1

Page 2

Page 3

Page n

Frame m

Swap Device

1GB

0 block 0

block 1

block 2

block p

Page out

Page in

RegionTable

RegionTable Page

Table

PageTable

PageTable

PageTable

RegionTable

RegionTable Page

Table

PageTable

PageTable

PageTable

PFdataTable

PFdataTable

Swap-useTable

Swap-useTable

D.B.D.Table

D.B.D.Table

D.B.D.Table

D.B.D.Table

D.B.D.Table

D.B.D.Table

D.B.D.Table

D.B.D.Table

Page 5: Memory Unix Tables 1

Tables

� Region table � Text, Data, Stack (per process)

� Page table� Virtual � Physical translation (per process)

� Disk Block Descriptor (DBD) table � Disk location of swapped pages (per process)

� PFData (Page Frame) table� Description of physical frame status (singleton)

� Swap in-use table� The same as PFData, but for the swap file (singleton)

Page 6: Memory Unix Tables 1

Process Memory

0

pro

cess a

ddre

ss s

pace

Text

Data

Stack

static allocation

dynamic allocation

4GB

Page 7: Memory Unix Tables 1

Region Table

0

pro

cess a

ddre

ss s

pace

Text

Data

Stack

static allocation

dynamic allocation

4GB

Text

Data

Stack

StartAddress

RegionSize

Permissions

Region table

Page 8: Memory Unix Tables 1

Pages and Frames

0

pro

cess a

ddre

ss s

pace

Text

Data

Stack

static allocation

dynamic allocation

4GB

Main (Physical) Memory

16MB

0

Frame (4k)

Page

Virtual Memory

Page 9: Memory Unix Tables 1

Page Table

Text

Data

Stack

StartAddress

RegionSize

Permissions

Page 0

Page 1

Page 2

Page N-2

Page N-1

PhysicalAddress

ValidFlag

Permissions Ref Mod COW Age

page table

region table

Page 10: Memory Unix Tables 1

The Unix Big Picture

Main (Physical) Memory

128MB

0 Frame 0

Frame 1

Frame 24GB

A process

0 Page 0

Page 1

Page 2

Page 3

Page n

4GB

A process

0 Page 0

Page 1

Page 2

Page 3

Page n

Frame m

Swap Device

1GB

0 block 0

block 1

block 2

block p

Page out

Page in

RegionTable

RegionTable Page

Table

PageTable

PageTable

PageTable

RegionTable

RegionTable Page

Table

PageTable

PageTable

PageTable

Page 11: Memory Unix Tables 1

Translation from Virtual to Physical address

Text

Data

Stack

StartAddress

RegionSize

Permissions

Page 0

Page 1

Page 2

Page N-2

Page N-1

PhysicalAddress

ValidFlag

Permissions Ref Mod COW Age

page table

region table

Virtual Address

Physical Address

Translation (Boooo)

Page 12: Memory Unix Tables 1

Translation from Virtual to Physical address

Text

Data

Stack

StartAddress

RegionSize

Permissions

Page 0

Page 1

Page 2

Page N-2

Page N-1

PhysicalAddress

ValidFlag

Permissions Ref Mod COW Age

page table

region table

Virtual Address offset

Frame Number offset

=

Page 13: Memory Unix Tables 1

Translation from Virtual to Physical address

Text

Data

Stack

StartAddress

RegionSize

Permissions

Page 0

Page 1

Page 2

Page N-2

Page N-1

PhysicalAddress

ValidFlag

Permissions Ref Mod COW Age

page table

region table

Virtual Address offset

Page Number offset

The Calculation:(Virtual – Start)/Page Size

Page 14: Memory Unix Tables 1

Translation from Virtual to Physical address

Text

Data

Stack

StartAddress

RegionSize

Permissions

Page 0

Page 1

Page 2

Page N-2

Page N-1

PhysicalAddress

ValidFlag

Permissions Ref Mod COW Age

page table

region table

Virtual Address offset

Page Number offset

Page 15: Memory Unix Tables 1

The Unix Big Picture

Main (Physical) Memory

128MB

0 Frame 0

Frame 1

Frame 24GB

A process

0 Page 0

Page 1

Page 2

Page 3

Page n

4GB

A process

0 Page 0

Page 1

Page 2

Page 3

Page n

Frame m

Swap Device

1GB

0 block 0

block 1

block 2

block p

Page out

Page in

RegionTable

RegionTable Page

Table

PageTable

PageTable

PageTable

RegionTable

RegionTable Page

Table

PageTable

PageTable

PageTable

PFdataTable

PFdataTable

Page 16: Memory Unix Tables 1

Physical Memory & Pfdata table

Main (Physical) Memory

16MB

0

Frame (4k)

Pfdata table

SwapDevice

RefCount

BlockNumber

Swap/File

Status

Page 17: Memory Unix Tables 1

The Unix Big Picture

Main (Physical) Memory

128MB

0 Frame 0

Frame 1

Frame 24GB

A process

0 Page 0

Page 1

Page 2

Page 3

Page n

4GB

A process

0 Page 0

Page 1

Page 2

Page 3

Page n

Frame m

Swap Device

1GB

0 block 0

block 1

block 2

block p

Page out

Page in

RegionTable

RegionTable Page

Table

PageTable

PageTable

PageTable

RegionTable

RegionTable Page

Table

PageTable

PageTable

PageTable

PFdataTable

PFdataTable

Swap-useTable

Swap-useTable

Page 18: Memory Unix Tables 1

Swap Device & swap use

Swap device

1GB

0

Block (4k)

Swap use table

Ref Count

Page 19: Memory Unix Tables 1

The Unix Big Picture

Main (Physical) Memory

128MB

0 Frame 0

Frame 1

Frame 24GB

A process

0 Page 0

Page 1

Page 2

Page 3

Page n

4GB

A process

0 Page 0

Page 1

Page 2

Page 3

Page n

Frame m

Swap Device

1GB

0 block 0

block 1

block 2

block p

Page out

Page in

RegionTable

RegionTable Page

Table

PageTable

PageTable

PageTable

RegionTable

RegionTable Page

Table

PageTable

PageTable

PageTable

PFdataTable

PFdataTable

Swap-useTable

Swap-useTable

D.B.D.Table

D.B.D.Table

D.B.D.Table

D.B.D.Table

D.B.D.Table

D.B.D.Table

D.B.D.Table

D.B.D.Table

Page 20: Memory Unix Tables 1

Virtual & Swap connection

Text

Data

Stack

StartAddress

RegionSize

Permissions

Page 0

Page 1

Page 2

Page N-2

Page N-1

PhysicalAddress

ValidFlag

Permissions Ref Mod COW Age

page table

region table

Page 0

Page 1

Page 2

Page N-2

Page N-1

Disk block descriptor

table

SwapDevice

Fill 0/Demand fill

BlockNumber

Swap/File

Page 21: Memory Unix Tables 1

Example

Swap Dev. 1

Block No. 2743Page No. 7941493K

Disk Block DescriptorPage Table EntryVirtual Address

Block No. 2743

Swap Dev. 1

Ref Cnt 1

Page Frame 794

Red Cnt 1

Swap Use

Swap Dev. Block 2743Physical Page 794

Page 22: Memory Unix Tables 1

Agenda

� Page Faults

� Ref, Mod, COW and Age

� Page In/Out Method

� Pager Process

� Page-Stealer Process

� System Calls

� Exec

� Fork

Page 23: Memory Unix Tables 1

Page Faults

� 3 Types of page Faults

� Page Faults

� Protection Fault

� Segmentation Fault

Page 24: Memory Unix Tables 1

Page Faults

� 3 Types of page Faults

� Page Faults

� Happens when valid=0.

� Protection Fault

� Happens on permission violation

� Segmentation Fault

� Happens when virtual address is illegal.

Page 25: Memory Unix Tables 1

Page Faults

� 3 Types of page Faults

� Page Faults

� Happens when valid=0.

� Causes pages in only (How?)

� Protection Fault

� Happens on permission violation

� Good Example: Writing to a read-only page

� Segmentation Fault

� Happens when virtual address is illegal.

Page 26: Memory Unix Tables 1

Ref & Mod

Page 0

Page 1

Page 2

Page N-2

Page N-1

PhysicalAddress

ValidFlag

Permissions Ref Mod COW Age

page table

Updated by CPU:• Ref – set to 1 when the processaccesses a page.

• Mod – set to 1 when the processmodifies a page

Page 27: Memory Unix Tables 1

COW & Age

Page 0

Page 1

Page 2

Page N-2

Page N-1

PhysicalAddress

ValidFlag

Permissions Ref Mod COW Age

page table

The other two, updated by O.S.• COW – Copy on Write Flag

The page will be copiedon first write (Fork)

• Age – Page Age (a number)=0 – to warm pages>0 – to cold pages

Page 28: Memory Unix Tables 1

The Unix Big Picture

Main (Physical) Memory

128MB

0 Frame 0

Frame 1

Frame 24GB

A process

0 Page 0

Page 1

Page 2

Page 3

Page n

4GB

A process

0 Page 0

Page 1

Page 2

Page 3

Page n

Frame m

Swap Device

1GB

0 block 0

block 1

block 2

block p

Page out

Page in

RegionTable

RegionTable Page

Table

PageTable

PageTable

PageTable

RegionTable

RegionTable Page

Table

PageTable

PageTable

PageTable

PFdataTable

PFdataTable

Swap-useTable

Swap-useTable

D.B.D.Table

D.B.D.Table

D.B.D.Table

D.B.D.Table

D.B.D.Table

D.B.D.Table

D.B.D.Table

D.B.D.Table

PagerPager

Page-stealerPage-stealer

Page 29: Memory Unix Tables 1

Pager

� Loads Pages into main memory.

� Usually, the Pager process is asleep.

� Wakes up on a Page Fault (Type 1).

Page 30: Memory Unix Tables 1

Pager

� The Page In process:

1. Look at the D.B.D table to find the page at the swap.

2. Look at the Pfdata table to check if the page is found at the main memory.

a. Skip to 4

3. Load the page from swap to main memory

4. Update tables.

Page 31: Memory Unix Tables 1

Page 0

Page 1

Page 2

Page N-2

Page N-1

Disk block descriptor

table

When Valid=0

PhysicalAddress

ValidFlag

. . . .

Page 0

Page 1

Page 2

Page N-2

Page N-1

page tableText

Data

Stack

region table

Virtual Address offset

SwapDevice

Fill 0/Demand fill

BlockNumber

Swap/File

Find the swap block number from the disk block descriptor table

Page 32: Memory Unix Tables 1

When Valid=0

Main (Physical) Memory

16MB

0

Frame (4k)

Pfdata table

SwapDevice

RefCount

BlockNumber

Swap/File

Status

Before reading the block from the swap,Unix tries to find the page in the main memory.Pfdata has a hash for speeding up the search process

Page 33: Memory Unix Tables 1

Page Stealer

� The Bad Guy �

� Wakes up when the number of free frames passes the low-water mark of free frames

� Start to free frames till the high-water mark

High

Low

Page 34: Memory Unix Tables 1

Which page to throw?

� Remember the Locality Rule?

� Throw cold pages

� The Aging process

� Happens every second

� Update Age field

� If (REF = =0) Age ++;

� Else Age=0;

� Turns off REF field

Page 35: Memory Unix Tables 1

Exec

� Create a new virtual memory

� New Region table

� New Page Tables & D.B.D

Page 36: Memory Unix Tables 1

Fork

� Duplicate the virtual memory� Text – can be shared because it is read-only

� Data & Stack – need to be duplicated

� The Problem:� Forking in order to do exec…

� Duplicate on Demand� Share while you can…� Make a copy only when needed!

Page 37: Memory Unix Tables 1

Copy on Demand

� A page at the data region can be shared until one of the processes modifies it.

� Therefore, the O.S. needs to trace the first write to the page

� Make the page Read-only.

� Turn on the COW flag.

� When we have Protection Fault

� Check if the COW is on, if so, make a duplicate.

Page 38: Memory Unix Tables 1

Fork

� Text Region� Share the page table

� Data & Stack� Copy the page table.

� To all valid pages: Read-only & COW

� Protection Fault� If COW is on

� copy the page

� Turn off COW.

� Change back to Read-Write permissions.