w10a virtual memory - Swarthmore College · 2016-11-12 · Heap Text Data Stack OS Heap Memory Terminology Process 1 Process 3 Process 3 OS Process 2 Process 1 Text Data Stack OS

Post on 03-May-2020

8 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

VirtualMemory11/8/16

(electionday)

Vote!

Recall:thejoboftheOS

TheOSisaninterfacelayerbetweenauser’sprogramsandhardware.

Itprovidesanabstractviewofthehardwarethatmakesiteasiertouse.

ProgramOperatingSystem

ComputerHardware

WhatweKnowaboutPhysicalMemory(RAM)

OS0x0

max

RAMisarrayofaddressablebytes,from0x0tomax(ex)address0to230-1for1GBofRAMspace

• TheOSneedstobeinRAM• Usuallyloadedataddress0x0

• PhysicalStorageforrunningprocesses:ProcessAddressSpacesarestoredinRAM

OS0x0

max

RAM

P1

P2

P3

x:

x:P1andP2eachgettheirowncopyofx

3

Regs

Mainmemory(RAM)

Secondarystorage

Cache

Howmuchmemoryisallocated?

int main(){

int i;

printf("a stack address: %p\n", &i);

printf("a text address: %p\n", main);

printf("the difference: %d\n", (unsigned int)(&i) –

(unsigned int)(main));

}

a stack address: 0x7fff7f1e9774

a text address: 0x400596

the difference: 2128515550

>2gigabytes

Thevirtualmemoryabstraction

Solvestwoproblems:• Notenoughphysicalmemorytogoaround.• Don’twantmultipleprogramstoaccidentallymodifythesamephysicalmemorylocation.

Keyideas:• OSallocatesmemorytoprocessesasneeded.• Program’saddressesgettranslatedtophysicaladdresses.

Memory• Abstractiongoal:makeeveryprocessthinkithasthesamememorylayout.• MUCHsimplerforcompilerifthestackalwaysstartsat0xFFFFFFFF,etc.

• Reality:there’sonlysomuchmemorytogoaround,andnotwoprocessesshouldusethesame(physical)memoryaddresses.

Process1

Process3

Process3

OS

Process2

Process1

OS(withhelpfromhardware)willkeeptrackofwho’susingeachmemoryregion.

Text

Data

Stack

OS

Heap

Text

Data

Stack

OS

Heap

Text

Data

Stack

OS

Heap

MemoryTerminology

Process1

Process3

Process3

OS

Process2

Process1

Text

Data

Stack

OS

Heap

PhysicalMemory:Thecontentsofthehardware(RAM)memory.ManagedbyOS.OnlyONE ofthesefortheentiremachine!

Virtual(logical)Memory:Theabstractviewofmemorygiventoprocesses.Eachprocessgetsanindependentviewofthememory.

AddressSpace:Rangeofaddressesforaregionofmemory.

Thesetofavailablestoragelocations.

0x0

0x…(DeterminedbyamountofinstalledRAM.)

0x0

0xFFFFFFFFVirtualaddressspace(VAS):fixedsize.

Text

Data

Stack

OS

Heap

Text

Data

Stack

OS

Heap

MemoryTerminology

Process1

Process3

Process3

OS

Process2

Process1

Text

Data

Stack

OS

HeapAddressSpace:Rangeofaddressesforaregionofmemory.

Thesetofavailablestoragelocations.

0x0

0x…(DeterminedbyamountofinstalledRAM.)

0x0

0xFFFFFFFFVirtualaddressspace(VAS):fixedsize.

Note:Itiscommon forVAStoappearlargerthanphysicalmemory.32-bit(IA32):Canaddressupto4GB,mighthavelessinstalled64-bit(X86-64):Ourlabmachineshave48-bitVAS(256TB),36-bitPAS(64GB)

CohabitatingPhysicalMemory

• IfprocessisgivenCPU,mustalsobeinmemory.• Problem• Context-switchingtime(CST):10µsec• Loadingfromdisk:10MB/s• Toload1MBprocess:100msec =10,000x CST• Toomuchoverhead!Breaksillusionofsimultaneity

• Solution:keepmultipleprocessesinmemory• Contextswitchonlybetweenprocessesinmemory

MemoryIssuesandTopics

• Whereshouldprocessmemoriesbeplaced?• Topic:“Classic”memorymanagement

• Howdoesthecompilermodelmemory?• Topic:Logicalmemorymodel

• Howtodealwithlimitedphysicalmemory?• Topics:Virtualmemory,paging

Plan:Startwiththebasics(outofdate)tomotivatewhyweneedthecomplexmachineryofvirtualmemoryandpaging.

Problem:Placement

• Whereshouldprocessmemoriesbeplaced?• Topic:“Classic”memorymanagement

• Howdoesthecompilermodelmemory?• Topic:Logicalmemorymodel

• Howtodealwithlimitedphysicalmemory?• Topics:Virtualmemory,paging

MemoryManagement• Physicalmemorystartsasonebigemptyspace.

MemoryManagement• Physicalmemorystartsasonebigemptyspace.

• Processesneedtobeinmemorytoexecute.

MemoryManagement• Physicalmemorystartsasonebigemptyspace.• Whencreatingprocess,allocatememory• Findspacethatcancontainprocess• Allocateregionwithinthatgap• Typically,leavesa(smaller)freespace

MemoryManagement• Physicalmemorystartsasonebigemptyspace.• Whencreatingprocess,allocatememory• Findspacethatcancontainprocess• Allocateregionwithinthatgap• Typically,leavesa(smaller)freespace

• Whenprocessexits,freeitsmemory• Createsagapinthephysicaladdressspace.• Ifnexttoanothergap,coalesce.

Fragmentation

• Eventually,memorybecomesfragmented• Afterrepeatedallocations/de-allocations

• Internalfragmentation• Unusedspacewithinprocess• Cannotbeallocatedtoothers• Cancomeinhandyforgrowth

• Externalfragmentation• Unusedspaceoutsideanyprocess(gaps)• Canbeallocated(toosmall/notuseful?)

WhichformoffragmentationiseasiestfortheOStoreduce/eliminate?

A. Internalfragmentation

B. Externalfragmentation

C. Neither

PlacingMemory

• Whensearchingforspace,whatiftherearemultipleoptions?• Algorithms• First(ornext)fit• Bestfit• Worstfit

• Complication• Isregionfixedorvariablesize?

PlacingMemory

• Whensearchingforspace,whatiftherearemultipleoptions?• Algorithms• First(ornext)fit• Bestfit• Worstfit

• Complication• Isregionfixedorvariablesize?

PlacingMemory

• Whensearchingforspace,whatiftherearemultipleoptions?• Algorithms• First(ornext)fit• Bestfit• Worstfit

• Complication• Isregionfixedorvariablesize?

Whichmemoryallocationalgorithmleavesthesmallestfragments(external)?

A. first-fit

B. worst-fit

C. best-fit

Isleavingsmallfragmentsagoodthingorabadthing?

PlacingMemory

• Whensearchingforspace,whatiftherearemultipleoptions?• Algorithms• First(ornext)fit:fast• Bestfit• Worstfit

• Complication• Isregionfixedorvariablesize?

PlacingMemory

• Whensearchingforspace,whatiftherearemultipleoptions?• Algorithms• First(ornext)fit• Bestfit:leavessmallfragments• Worstfit

• Complication• Isregionfixedorvariablesize?

PlacingMemory

• Whensearchingforspace,whatiftherearemultipleoptions?• Algorithms• First(ornext)fit• Bestfit• Worstfit:leaveslargefragments

• Complication• Isregionfixedorvariablesize?

Whatifitdoesn’tfit?

• Theremaystillbesignificantunusedspace• Externalfragments• Internalfragments

• Approaches

Whatifitdoesn’tfit?

• Theremaystillbesignificantunusedspace• Externalfragments• Internalfragments

• Approaches• Compaction

Whatifitdoesn’tfit?

• Theremaystillbesignificantunusedspace• Externalfragments• Internalfragments

• Approaches• Compaction• Breakprocessmemoryintopieces

• Easiertofit.• Morestatetokeeptrackof.

ProblemSummary:Placement

• Whenplacingprocess,itmaybehardtofindalargeenoughfreeregioninphysicalmemory.

• Fragmentationmakesthisharderovertime(freepiecesgetsmaller,spreadout).

• Generalsolution:don’trequireallofaprocess’smemorytobeinonepiece!

ProblemSummary:Placement

• Generalsolution:don’trequireallofaprocess’smemorytobeinonepiece!

Process1

OS

Process2

Process1

Process3

Process2

PhysicalMemory

ProblemSummary:Placement

• Generalsolution:don’trequireallofaprocess’smemorytobeinonepiece!

Process1

OS

Process2

Process1

Process3

Process2

PhysicalMemory

OS:Place

Process3

ProblemSummary:Placement

• Generalsolution:don’trequireallofaprocess’smemorytobeinonepiece!

Process1

OS

Process2

Process1

Process3

Process2

PhysicalMemory

OS:Place

Process3

Process3

Process3

ProblemSummary:Placement

• Generalsolution:don’trequireallofaprocess’smemorytobeinonepiece!

Process1

OS

Process2

Process1

Process3

Process2

PhysicalMemory

OS:Place

Process3

Process3

Process3Process3

OSmaychoosenottoplacepartsinmemoryatall.

Problem:Addressing

• Whereshouldprocessmemoriesbeplaced?• Topic:“Classic”memorymanagement

• Howdoesthecompilermodelmemory?• Topic:Logicalmemorymodel

• Howtodealwithlimitedphysicalmemory?• Topics:Virtualmemory,paging

(More)ProblemswithMemoryCohabitation

• Addressing:• Compilergeneratesmemoryreferences• Unknownwhereprocesswillbelocated

• Protection:• Modifyinganotherprocess’smemory

• Space:• Themoreprocessesthereare,thelessmemoryeachindividuallycanhave

P2

P1

P3

Compiler’sViewofMemory

• Compilergeneratesmemoryaddresses• Needsemptyregionfortext,data,heap,stack• Ideally,verylargetoallowheapandstacktogrow• Alternative:fourindependentemptyregions

• Whatcompilerneedstoknow,butdoesn’t• Physicalmemorysize• Wheretoplacedata(e.g.,stackathighend)

• Mustavoidallocatedregionsinmemory

AddressSpaces• Addressspace• Setofaddressesformemory

• Usuallylinear:0toN-1(sizeN)• PhysicalAddressSpace• 0toN-1,N =size• Kerneloccupieslowestaddresses

0

N-1

PAS

kernel

PM

Virtualvs.PhysicalAddressing• Virtualaddresses• Assumesseparatememorystartingat0• Compilergenerated• Independentoflocationinphysicalmemory

• OS:Mapvirtualtophysical

P10

N1-1

P2

0

N2-1

P3

0

N3-1

VM’sVAS’s

P2

P1

P3

0

N-1

PMPAS

Whenshouldweperformthemappingfromvirtualtophysicaladdress?Why?

A. Whentheprocessisinitiallyloaded:convertalltheaddressestophysicaladdresses

B. Whentheprocessisrunning:maptheaddressesasthey’reused.

C. Performthemappingatsomeothertime.When?

HardwareforVirtualAddressing• Baseregisterfilledwithstartaddress• Totranslateaddress,addbase• Achievesrelocation• Tomoveprocess:changebase

P2

0

N2-1

P2

P1

P3

0

N-1

Base +

HardwareforVirtualAddressing• Baseregisterfilledwithstartaddress• Totranslateaddress,addbase• Achievesrelocation• Tomoveprocess:changebase

P2

0

N2-1

P2

P1

P3

0

N-1

Base +

HardwareforVirtualAddressing• Baseregisterfilledwithstartaddress• Totranslateaddress,addbase• Achievesrelocation• Tomoveprocess:changebase• Protection? P2

0

N2-1

P2

P1

P3

0

N-1

Base +

Protection• Boundregisterworkswithbaseregister• Isaddress<bound• Yes:addtobase• No:invalidaddress,TRAP

• Achievesprotection

P2

0

N2-1

P2

P1

P3

0

N-1

Base +

<

Bound

y/n?

Whenwouldweneedtoupdatethesebase&boundregisters?

Givenwhatwecurrentlyknowaboutmemory,whatmustwedoduringacontextswitch?

• A.Allocatememorytotheswitchingprocess

• B.Loadthebaseandboundregisters

• C.Convertlogicaltophysicalmemoryaddresses

MemoryRegistersPartofContext

• OnEveryContextSwitch• Loadbase/boundregistersforselectedprocess• Onlykerneldoesloadingoftheseregisters• Kernelmustbeprotectedfromallprocesses

• Benefit• Allowseachprocesstobeseparatelylocated• Protectseachprocessfromallothers

ProblemSummary:Addressing

• Compilerhasnoideawhere,inphysicalmemory,theprocess’sdatawillbe.

• CompilergeneratesinstructionstoaccessVAS.

• Generalsolution:OSmusttranslateprocess’sVASaccessestothecorrespondingphysicalmemorylocation.

ProblemSummary:Addressing• Generalsolution:OSmusttranslateprocess’sVASaccessestothecorrespondingphysicalmemorylocation.

Process1

OS

Process2

Process1

Process3

Process2

PhysicalMemory

OS:Translate

Process3

Process3

Process3Process3

Whentheprocesstriestoaccessavirtualaddress,theOStranslatesittothecorrespondingphysicaladdress.

movl (address0x74),%eax

ProblemSummary:Addressing• Generalsolution:OSmusttranslateprocess’sVASaccessestothecorrespondingphysicalmemorylocation.

Process1

OS

Process2

Process1

Process2

PhysicalMemory

Process3

Process3

0x42F80

Process3OS:

Translate

Process3

Process3

Whentheprocesstriestoaccessavirtualaddress,theOStranslatesittothecorrespondingphysicaladdress.

movl (address0x74),%eax

Let’scombinetheseideas:

1. Allowprocessmemorytobedividedupintomultiplepieces.

2. KeepstateinOS(+hardware/registers)tomapfromvirtualaddressestophysicaladdresses.

Result:Keepatable tostorethemappingofeachregion.

ProblemSummary:Addressing• Generalsolution:OSmusttranslateprocess’sVASaccessestothecorrespondingphysicalmemorylocation.

Process1

OS

Process2

Process1

Process2

PhysicalMemory

Process3

Process3

0x42F80

Process3OS:

Translate

Process3

Process3

Whentheprocesstriestoaccessavirtualaddress,theOStranslatesittothecorrespondingphysicaladdress.

movl (address0x74),%eaxOSmustkeepatable,foreachprocess,tomapVAStoPAS.Oneentryperdividedregion.

Two(Real)Approaches• Segmentedaddressspace/memory• Partitionaddressspaceandmemoryintosegments• Segmentsaregenerallydifferentsizes

• Pagedaddressspace/memory• Partitionaddressspaceandmemoryintopages• Allpagesarethesamesize

top related