memory management lecture 7

27
Lecture 7: Memory Management CSE 120: Principles of Opera>ng Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

description

memory management lecture

Transcript of memory management lecture 7

Page 1: memory management lecture 7

Lecture7:MemoryManagementCSE120:PrinciplesofOpera>ngSystems

UCSanDiego:SummerSessionI,2009FrankUyeda

Page 2: memory management lecture 7

Announcements

•  PeerWiseques>onsduetomorrow.•  Project2isdueonFriday.– MilestoneonTuesdaynight.

•  Homework3isduenextMonday.

2

Page 3: memory management lecture 7

GoalsforToday

•  Mo>va>onforMemoryManagement•  UnderstandPaging– Howtotranslatefromvirtualtophysicaladdress

– Determinesize,structureofpagetables

3

Page 4: memory management lecture 7

MemoryManagement

•  Goalsofmemorymanagement–  Provideaconvenientabstrac>onforprogramming– Allocatescarcememoryresourcesamongcompe>ngprocesses

– Maximizeperformancewithminimaloverhead•  Mechanisms–  Physicalandvirtualaddressing–  Techniques:Par>>oning,paging,segmenta>on–  Pagetablemanagement,TLBs,VMtricks

•  Policies–  Pagereplacementalgorithms

4

Page 5: memory management lecture 7

Inthebeginning…..

•  Batchprogrammedsystems–  Programsusephysicaladdressesdirectly

–  OSloadsjob,runsit,unloadsit–  Similartowhatnachosdoesrightnow

(you’llchangethisinProject2)

5

Stack

Heap

DataSegment

TextSegment

SP

PC

Opera>ngSystem

PhysicalMemory

Page 6: memory management lecture 7

LettherebeMul>programming

•  Mul>programmingchangesallofthis– Wantmul>pleprocessesinmemoryatonce

•  OverlapI/OandCPUofmul>plejobs

– Candoitanumberofways•  Fixedandvariablepar>>oning,paging,segmenta>on

– Requirements•  Needprotec>on–restrictwhichaddressesjobscanuse•  Fasttransla>on–lookupsneedtobefast•  Fastchanges–upda>ngmemoryhardwareoncontextswitch

6

Page 7: memory management lecture 7

VirtualMemory

•  Thebasicabstrac>onprovidedbytheOSmemorymanagementisvirtualmemory–  Aprocess’saddressspaceinmemoryisnotnecessarilythesameasthephysicalmemory(RAM)addressinwhichitresides

– Whenaprocessrequestsamemoryaddress,theOSwilltranslatetheaddressfromavirtualaddresstoaphysicaladdress.

7

Page 8: memory management lecture 7

VirtualAddresses

•  Processesaccessmemoryusingavirtualaddress–  ThevirtualaddressisnotthesameasthephysicalRAMaddressinwhichitresides–  TheOS(hardwareMMU)translatesthevirtualaddressintothephysicalRAMaddress

–  Whodeterminesthemappingforthetransla>on?

8

Stack

Heap

DataSegment

TextSegment

SP

PC0x00…….(Star>ngAddress)

0xFFF…..(EndingAddress)

AddressSpace

Page 9: memory management lecture 7

VirtualAddresses•  Processesaccessmemoryusingavirtualaddress

–  ThevirtualaddressisnotthesameasthephysicalRAMaddressinwhichitresides–  TheOS(hardwareMMU)translatesthevirtualaddressintothephysicalRAMaddress–  Whodeterminesthemappingforthetransla>on?

9

Stack

Heap

DataSegment

TextSegment

SP

PC0x00…….(Star>ngAddress)

0xFFF…..(EndingAddress)

AddressSpace

Stack

Heap

DataSegment

TextSegment

PhysicalMemory

MMU

…..

VirtualMemory

Page 10: memory management lecture 7

VirtualAddresses•  Processesaccessmemoryusingavirtualaddress

–  ThevirtualaddressisnotthesameasthephysicalRAMaddressinwhichitresides–  TheOS(hardwareMMU)translatesthevirtualaddressintothephysicalRAMaddress–  Whodeterminesthemappingforthetransla>on?

10

Stack

Heap

DataSegment

TextSegment

SP

PC0x00…….(Star>ngAddress)

0xFFF…..(EndingAddress)

AddressSpace

Stack

Heap

DataSegment

TextSegment

PhysicalMemory

MMU

…..

VirtualMemory

Page 11: memory management lecture 7

VirtualMemory

•  Virtualmemoryenablesprogramstoexecutewithoutrequiringtheiren>readdressspaceresideinphysicalmemory

–  Savesspace•  Manyprogramsdonotneedalloftheircodeanddataatonce(orever),sothereisnoneedtoallocatememoryforit

–  Allowsflexibilityforapplica>onandOS•  Indirec>onallowsmovingprogramsaroundinmemory;OScanadjustamountofmemoryallocatedbaseduponitsrun‐>mebehavior

•  Allowsprocessestoaddressmoreorlessmemorythanphysicallyinstalledinthemachine

–  Isola>onandprotec>on•  Oneprocesscannotaccessmemoryaddressesinothers

•  Excep>on:sharedmemory,whichwe’vealreadycovered11

Page 12: memory management lecture 7

MemoryManagementRequirements

•  Protec>on–  Restrictwhichphysicaladdressesprocessescanuse,sotheycan’tstomponeachother

•  Fasttransla>on–  Accessingmemorymustbefast,regardlessoftheprotec>onscheme

•  Fastcontextswitching–  Overheadofupda>ngmemoryhardwareonacontextswitchmustbelow

•  Requireshardwaresupportforefficientimplementa>on

12

Page 13: memory management lecture 7

MMUandTLB

•  MemoryManagementUnit(MMU)–  Hardwareunitthattranslatesavirtualaddresstoaphysicaladdress

–  EachmemoryreferenceispassedthroughtheMMU

–  Translateavirtualaddresstoaphysicaladdress

•  Transla>onLookasideBuffer(TLB)–  Essen>allyacachefortheMMU’svirtual‐to‐physicaltransla>onstable

–  Notneededforcorrectnessbutsourceofsignificantperformancegain

13

CPUTransla>on

TableMMU

Memory

VirtualAddress

PhysicalAddress

TLB

Page 14: memory management lecture 7

MemoryAlloca>on

14

•  Howshouldweallocatememorytoprocesses?

PhysicalMemory

MMU

Page 15: memory management lecture 7

FixedPar>>ons

15

P1

P2

P3

P4

P5

PhysicalMemory

BaseRegisterP4’sBase

VirtualAddressOffset +

•  Physicalmemoryisbrokenupintofixedpar>>ons–  Hardwarerequirements:baseregister

–  Physicaladdress=virtualaddress+baseregister

Page 16: memory management lecture 7

FixedPar>>ons

•  Physicalmemoryisbrokenupintofixedpar>>ons–  Hardwarerequirements:baseregister–  Physicaladdress=virtualaddress+baseregister–  BaseregisterloadedbyOSwhenitswitchestoaprocess–  Sizeofeachpar>>onisthesameandfixed–  Howdoweprovideprotec>on?

•  Advantages–  Easytoimplement,fastcontextswitch

•  Problems–  Internalfragmenta>on:memoryinapar>>onnotusedbya

processisnotavailabletootherprocesses–  Par>>onsize:onesizedoesnotfitall(verylargeprocesses?)

16

Page 17: memory management lecture 7

VariablePar>>ons

•  Naturalextension–physicalmemoryisbrokenupintovariablesizedpar>>ons–  Hardwarerequirements:baseregisterandlimitregister–  Physicaladdress=virtualaddress+baseregister– Whydoweneedthelimitregister?Protec>on

•  If(physicaladdress>base+limit)thenprotec>onfault

17

Page 18: memory management lecture 7

VariablePar>>ons

18

P1

P2

P3

PhysicalMemoryBaseRegister

P4’sBase

VirtualAddressOffset +

LimitRegisterP3’sBase

<Yes?

No?

Protec>onFault

Page 19: memory management lecture 7

VariablePar>>ons

•  Naturalextension–physicalmemoryisbrokenupintovariablesizedpar>>ons–  Hardwarerequirements:baseregisterandlimitregister–  Physicaladdress=virtualaddress+baseregister– Whydoweneedthelimitregister?Protec>ons

•  If(physicaladdress>base+limit)thenexcep>onfault

•  Advantages–  Nointernalfragmenta>on:allocatejustenoughforprocess

•  Problems–  Externalfragmenta>on:jobloadingandunloadingproducesemptyholesscaperedthroughoutmemory

19

Page 20: memory management lecture 7

Paging

•  Pagingsolvestheexternalfragmenta>onproblembyusingfixedsizedunitsinbothphysicalandvirtualmemory

20

Page1

Page2

Page3

Page4

Page5

PhysicalMemory

Page1

Page2

PageN

VirtualMemory

…..

Page 21: memory management lecture 7

User/ProcessPerspec>ve

•  Users(andprocesses)viewmemoryasonecon>guousaddressspacefrom0toN–  Virtualaddressspace

•  Inreality,pagesarescaperedthroughoutphysicalstorage•  Themappingisinvisibletotheprogram

•  Protec>onisprovidedbecauseaprogramcannotreferencememoryoutsideofitsvirtualaddressspace–  Theaddress“0x1000”mapstodifferentphysicaladdressesindifferent

processes

21

Page 22: memory management lecture 7

Paging

•  Transla>ngaddresses–  Virtualaddresshastwoparts:virtualpagenumberandoffset

–  Virtualpagenumber(VPN)isanindexintoapagetable–  Pagetabledeterminespageframenumber(PFN)

–  PhysicaladdressisPFN::offset

22

0xBAADF00D=offsetvirtualpagenumber

0xBAADF 0x00D

Transla>onTable

pagetable

0xBAADF 0x900DFphysicalpagenumber(pageframenumber)

virtualpagenumber

virtualaddress

Page 23: memory management lecture 7

PageLookups

23

Page1

Page2

Page3

PageN

PhysicalMemory…..

Pageframe Offset

PhysicalAddress

Pagenumber Offset

VirtualAddress

Pageframe

PageTable

0xBAADF00D

0xBAADF 0xF00D

0xF00D0x900DF

0x900DF00D

0xFFFFFFFF

0x00000000

Page 24: memory management lecture 7

Paging

•  Transla>ngaddresses–  Virtualaddresshastwoparts:virtualpagenumberandoffset

–  Virtualpagenumber(VPN)isanindexintoapagetable–  Pagetabledeterminespageframenumber(PFN)

–  PhysicaladdressisPFN::offset•  Pagetables

–  Mapvirtualpagenumber(VPN)topageframenumber(PFN)•  VPNistheindexintothetablethatdeterminesPFN

–  Onepagetableentry(PTE)perpageinvirtualaddressspace•  Or,onePTEperVPN

24

Page 25: memory management lecture 7

PagingExample

•  Memoryaddressis32bits

•  Pagesare4K–  VPNisbits(VPNs),offsetisbits

•  Virtualaddressis0x7468–  Virtualpageis,offsetis_______

•  Pagetableentrycontains0x2–  Pageframebaseis0x2<<=_______–  _____thvirtualpageisaddress_______(3rdphysicalpage)

•  Physicaladdress=_____+_____=______

25

Page 26: memory management lecture 7

PagingExample

•  Memoryaddressis32bits

•  Pagesare4K–  VPNis20bits(1MVPNs),offsetis12bits

•  Virtualaddressis0x7468–  Virtualpageis0x7,offsetis_0x468__

•  Pagetableentry0x7contains0x2–  Pageframebaseis0x2<<12bits=__0x2000__–  ___7_thvirtualpageisaddress0x2000(3rdphysicalpage)

•  Physicaladdress=_0x2000_+_0x468_=_0x2468_

26

Page 27: memory management lecture 7

NextTime

•  ReadChapter8.3‐8.8•  Peerwiseques>onsduetomorrowatmidnight.

•  CheckWebsiteforcourseannouncements–  hpp://www.cs.ucsd.edu/classes/su09/cse120

27