cse 3320 operating systems memory...

29
CSE 3320 Operating Systems Memory Management Jia Rao Department of Computer Science and Engineering http://ranger.uta.edu/~jrao

Upload: others

Post on 11-Jul-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

CSE3320OperatingSystems

MemoryManagement

Jia RaoDepartmentofComputerScience and Engineering

http://ranger.uta.edu/~jrao

Page 2: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

RecapofPreviousClasses

• Multiprogrammingo Requiresmultipleprogramstorunatthe“same”time

o Programsmustbebroughtintomemoryandplacedwithinaprocessforittoberun

o Howtomanagethememoryoftheseprocesses?

o Whatifthememoryfootprintoftheprocessesislargerthanthephysicalmemory?

Page 3: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

MemoryManagement• Ideallyprogrammerswantmemorythatis

o largeo fasto nonvolatileo andcheap

• Memoryhierarchyo smallamountoffast,expensivememory– cacheo somemedium-speed,mediumpricemainmemoryo gigabytesofslow,cheapdiskstorage

• Memorymanagementtaskso Allocateandde-allocatememoryforprocesseso Keeptrackofusedmemoryandbywhomo Addresstranslationandprotection

Memoryischeapandlargeintoday’sdesktop,whymemory

managementisstillimportant?

Page 4: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

NoMemoryAbstraction

Threesimplewaysoforganizingmemory.(a)earlymainframes.(b)Handheldandembeddedsystems.(c)earlyPC.

• Mono-programming:Oneprogramatatime,sharingmemorywithOS

• Needformulti-programming:1.UsemultipleCPUs.2.OverlappingI/OandCPU

Page 5: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

MultiprogrammingwithFixedPartitions

• Fixed-sizememorypartitions,withoutswappingorpagingo Separateinputqueuesforeachpartitiono Singleinputqueueo Variousjobschedulers

Whatarethedisadvantages?

Page 6: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

Multiprogrammingwandw/oSwapping

• Swappingo Oneprogramatonetime

o Savetheentirememoryofthepreviousprogramtodisk

o Noaddresstranslationisrequiredandnoprotectionisneeded

• w/oswappingo Memoryisdividedintoblocks

o Eachblockisassignedaprotectionkey

o Programsworkonabsolutememory,noaddresstranslation

o Protectionisenforcedbytrappingunauthorizedaccesses

Page 7: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

RunningaProgram*Sourceprogram

Objectprogram

Loadableprogram

compiler

linker

In-memoryexecution

Staticlibrary

dynamiclibrary

• Compileandlinktimeo Alinkerperformsrelocationifthememoryaddressisknown

• Loadtimeo Mustgeneraterelocatable codeifmemorylocationisnotknownatcompiletime

Page 8: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

RelocationandProtection• Relocation:whataddresstheprogramwillbeginininmemory

o Staticrelocation:OSperformsone-timechangeofaddressesinprogram

o Dynamicrelocation:OSisabletorelocateaprogramatruntime

• Protection:mustkeepaprogramoutofotherprocesses’ partitions

Illustration of the relocation problem.

Staticrelocation– OScannotmoveitoncea programisassignedaplaceinmemory

Page 9: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

AMemoryAbstraction:AddressSpaces

• Exposingtheentirephysicalmemorytoprocesseso Dangerous,maytrashtheOS

o Inflexible,hardtorunmultipleprogramssimultaneously

• Programshouldhavetheirownviewsofmemoryo Theaddressspace– logicaladdress

o Non-overlappingaddressspaces– protection

o Moveaprogrambymappingitsaddressestoadifferentplace-relocation

Page 10: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

DynamicRelocation

• OSdynamicallyrelocatesprogramsinmemoryo Twohardwareregisters:baseandlimit

o Hardwareaddsrelocationregister(base)tovirtualaddresstogetaphysicaladdress

o Hardwarecomparesaddresswithlimitregister addressmustbelessthanlimit

Disadvantage:twooperationsoneverymemoryaccessandtheadditionisslow

Page 11: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

DealingwithMemoryOverload- Swapping

Memoryallocationchangesaso processescomeintomemoryo leavememory

Shadedregionsareunusedmemory(memoryholes)

° Swapping:bringineachprocessinitsentirety,M-D-M- …

° Keyissues:allocatingandde-allocatingmemory,keeptrackofit

Whynotmemorycompaction?

Anotherwayistousevirtualmemory

Page 12: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

Swapping– MemoryGrowing

(a)Allocatingspaceforgrowingsingle(data)segment(b)Allocatingspaceforgrowingstack&datasegment

Whystackgrowsdownward?

Page 13: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

MemoryManagementwithBitMaps

• Partofmemorywith5processes,3holeso tickmarksshowallocationunits(whatisitsdesirablesize?)o shadedregionsarefree

• Correspondingbitmap(searchingabitmapforarunofn0s?)

• Sameinformationasalist (betterusingadoubly-linkedlist)

° Keeptrackofdynamicmemoryusage:bitmapandfreelists

Page 14: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

MemoryManagementwithLinkedLists

Four neighborcombinationsfortheterminatingprocessX

° De-allocatingmemoryistoupdatethelist

Page 15: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

MemoryManagementwithLinkedLists(2)

Ref.MOS3E,OS@Austin,Columbia,RochesterUC.ColoradoSpringsCS4500/5500

° Howtoallocatememoryforanewlycreatedprocess(orswapping)?• Firstfit:allocatethefirstholethatisbigenough

• Bestfit:allocatethesmallestholethatisbig

• Worstfit:allocatethelargesthole

• HowaboutseparatePandHlistsforsearchingspeedup?Butwithwhatcost?

° Example:ablockofsize2isneededformemoryallocation

Whichstrategyisthebest?

Quickfit:searchfast,mergeslow

Page 16: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

VirtualMemory° Virtualmemory:thecombinedsizeoftheprogram,data,andstackmay

exceedtheamountofphysicalmemoryavailable.

• Swappingwithoverlays;buthardandtime-consumingtosplitaprogramintooverlaysbytheprogrammer

• Whattodomoreefficiently?

Page 17: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

MappingofVirtualaddressestoPhysicaladdresses

Logicalprogramworksinitscontiguousvirtualaddressspace

ActuallocationsofthedatainphysicalmemoryAddresstranslation

donebyMMU

Page 18: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

PagingandItsTerminology

Pagetablegivestherelationbetweenvirtualaddressesandphysicalmemoryaddresses

° Terms• Pages• Pageframes• Pagehit• Pagefault• Pagereplacement

° Examples:• MOVREG,0• MOVREG,8192• MOVREG,20500• MOVREG,32780

20K– 24K:20480– 24575

24K– 28K:24576– 28671

Page 19: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

PageTables

Twoissues:

1.Mappingmustbefast

2.Pagetablecanbelarge

Whohandlespagefaults?

InternaloperationofMMUwith164KBpages

Page 20: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

StructureofaPageTableEntryVirtualAddress

Virtualpagenumber Pageoffset

Whosetsallthosebits?

/dirty

Page 21: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

TranslationLook-asideBuffers(TLB)TakingadvantageofTemporalLocality:

Awaytospeedupaddresstranslationistouseaspecialcache ofrecentlyusedpagetableentries-- thishasmanynames,butthemostfrequentlyusedisTranslationLookaside Buffer orTLB

Virtualpagenumber Cache Ref/use Dirty Protection PhysicalAddress(virtual page #) (physical page #)

TLBaccesstimecomparabletocacheaccesstime;muchlessthanPageTable(usuallyinmainmemory)accesstime

Traditionally,TLBmanagementandhandlingweredonebyMMUHardware,today,someinsoftware/OS(manyRISCmachines)

WhohandlesTLBmanagementandhandling,suchasaTLBmiss?

Page 22: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

ATLBExample

ATLBtospeeduppaging(usuallyinsideofMMUtraditionally)

Page 23: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

PageTableSizeGivena32-bitvirtualaddress,

4KBpages,4bytesperpagetableentry(memoryaddr.ordiskaddr.)

Whatisthesizeofthepagetable?

Thenumberofpagetableentries:

2^32/2^12=2^20

Thetotalsizeofpagetable:

2^20*2^2=2^22(4MB)

WhenwecalculatePageTablesize,theindexitself(virtualpagenumber)isoftenNOTincluded!

Whatifthevirtualmemoryaddressis64-bit? 2^64/2^12*2^2=2^24GB

Whatif2KBpages?

2^32/2^11*2^2=2^23(8MB)

Page 24: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

Multi-levelPageTablesSecond-level page tables

(a)32bitaddresswith2pagetablefields.(b)Two-levelpagetables

Ifonly4tablesareneeded,thetotalsizewillbe2^10*4=4KB

Example-1:PT1=1,PT2=3,Offset=4Virtualaddress:1*2^22+3*2^12+4=4206596

4M4K

Example-2:givenlogicaladdress4,206,596Whatwillbethevirtualaddressandwhereareitspositionsinthepagetables4206596/4096=1027,remainder41027/1024=1,remainder3 0-4095

4206592-4210687

Page 25: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

InvertedPageTables° Invertedpagetable:oneentryperpageframeinphysicalmemory,insteadof

oneentryperpageofvirtualaddressspace.Givena64-bitvirtualaddress,

4KBpages,256MBphysicalmemory

HowmanyentriesinthePageTable?Homemanypageframesinstead?HowlargeisthePageTableifoneentry8B?

Comparisonofatraditionalpagetablewithaninvertedpagetable

Page 26: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

InvertedPageTables(2)° Invertedpagetable:howtoexecutevirtual-to-physicaltranslation?

• TLBhelps!ButwhatifaTLBmisses?

Page 27: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

IntegratingTLB,Cache,andVMJustlikeanyothercache,theTLBcanbeorganizedasfullyassociative,

setassociative,ordirectmapped

TLBsareusuallysmall,typicallynotmorethan128- 256entriesevenonhighendmachines.Thispermitsfullyassociativelookuponthesemachines.Mostmid-rangemachinesusesmalln-waysetassociativeorganizations.

CPU TLBLookup Cache Main

Memory

VA PA miss

hit

data

Trans-lation

hit

missTranslationwith a TLB

TLBmissesorPagefault,gotoPageTabletranslationfordiskpageaddresses

Page 28: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

Putitalltogether:LinuxandX86

Logicaladdress

cr3

PageGlobalDirectory

PageUpperDirectory

PageMiddleDirectory

PageTable

Page

Switchthecr3valuewhencontextswitchingthreadsandflushtheTLB

Acommonmodelfor32-bit(two-level,4Bpte)and64-bit(four-level,8Bpte)SRC/include/linux/sched.h->task_struct->mm->pgd

SRC/kernel/sched.c->context_switch->switch_mm

Page 29: CSE 3320 Operating Systems Memory Managementranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC... · Relocation and Protection • Relocation: what address the program will begin

Summary• Twotasksofmemorymanagement

• Whymemoryabstraction?

• Managefreememory

• Twowaystodealwithmemoryoverload

o Swappingandvirtualmemory

• Virtualmemory

• PagingandPagetable