EE108B Lecture14 VirtualMemory

ChristosKozyrakis StanfordUniversity http://eeclass.stanford.edu/ee108b

C.Kozyrakis EE108bLecture14 1 Announcements

• Reminders – Lab3andPA2(parta)aredueontoday

C.Kozyrakis EE108bLecture14 2 Review:HardwareSupportforOperatingSystem

• Operatingsystem – Manageshardwareresources(CPU,memory,I/Odevices) • Onbehalfofuserapplications – Providesprotectionandisolation – Virtualization • Processesandvirtualmemory

• WhathardwaresupportisrequiredfortheOS? – Kernelandusermode • Kernelmode:accesstoallstateincludingprivilegedstate • Usermode:accesstouserstateONLY – Exceptions/ – Virtualmemorysupport

C.Kozyrakis EE108bLecture14 3 MIPSInterrupts

• WhatdoestheCPUdoonanexception? – Set EPC registertopointtotherestartlocation – ChangeCPUmodetokernelanddisableinterrupts – Set Cause registertoreasonforexceptionalsoset BadVaddr registerifaddressexception – Jumptovector • InterruptVectors – 0x8000 0000 :TLBrefill – 0x8000 0180 :Allotherexceptions • Privilegedstate – EPC – Cause – BadVaddr

C.Kozyrakis EE108bLecture14 4 AReallySimpleExceptionHandler

xcpt_cnt: la $k0, xcptcount # get address of counter lw $k1, 0($k0) # load counter addu $k1, 1 # increment counter sw $k1, 0($k0) # store counter eret # restore status reg: enable # interrupts and user mode # return to program (EPC )

• Can’tsurvivenestedexceptions – OKsincedoesnotreenableinterrupts • Doesnotuseanyuserregisters – Noneedtosavethem

C.Kozyrakis EE108bLecture14 5 Interruptvectors Acceleratinghandlerdispatch

Exception numbers • 1.Eachtypeofinterrupthas auniqueexceptionnumberk

codecode for for exceptionexception handler handler 0 0 • 2.Jumptable(interruptvector) interrupt entrykpointstoafunction code for vector code for (exceptionhandler). exceptionexception handler handler 1 1 0 1 codecode for for • 3.Handlerkiscalledeachtime 2 ... exceptionexception handler handler 2 2 exceptionkoccurs n-1 ...

codecode for for But the CPU is much faster exceptionexception handler handler n-1 n-1 than I/O devices and most OSs use a common handler anyway Make the common case fast!

C.Kozyrakis EE108bLecture14 6 ExceptionExample#1

• MemoryReference – Userwritestomemorylocation – Thatportion()ofuser’smemoryiscurrently notinmemory int a[1000]; main () – Pagehandlermustloadpageintophysical { memory a[500] = 13; – Returnstofaultinginstruction } – Successfulonsecondtry

User OS

event lw Create page and load return into memory

C.Kozyrakis EE108bLecture14 7 ExceptionExample#2

• MemoryReference – Userwritestomemorylocation int a[1000]; – Addressisnotvalid main () – Pagehandlerdetectsinvalidaddress { – Sends SIGSEG touserprocess a[5000] = 13; – Userprocessexitswith“segmentation } fault”

User Process OS

page fault event lw Detect invalid address Signal process C.Kozyrakis EE108bLecture14 8 Review:OS↔ UserCodeSwitches

• Systemcallinstructions(syscall) – VoluntarytransfertoOScodetoopenafile,write,change workingdirectory,… – Jumpstopredefinedlocation(s)intheoperatingsystem – Automaticallyswitchesfromusermodetokernelmode

PC + 4

Branch/jump PC PC • Exceptions&interrupts Excep Vec MUX – Illegalopcode,devicebyzero,…,timerexpiration,I/O,… EPC – ExceptionalinstructionbecomesajumptoOScode • Preciseexceptions – Externalinterruptsattachedtosomeinstructioninthepipeline – Newstate:EPC,Cause,BadVaddr registers

C.Kozyrakis EE108bLecture14 9 ProcessAddressSpace

0xffffffff kernelmemory memory (code,data,heap,stack) invisibleto 0x80000000 usercode userstack (createdatruntime) • Eachprocesshasitsown $sp(stackpointer) privateaddressspace

brk runtimeheap (managedbymalloc) read/writesegment 0x10000000 (.data,.bss) loadedfromthe readonlysegment executablefile (.text) 0x00400000 unused 0 C.Kozyrakis EE108bLecture14 10 MotivationsforVirtualMemory

• #1UsePhysicalDRAMasaCachefortheDisk – Addressspaceofaprocesscanexceedphysicalmemorysize – Sumofaddressspacesofmultipleprocessescanexceedphysical memory • #2SimplifyMemoryManagement – Multipleprocessesresidentinmainmemory. • Eachprocesswithitsownaddressspace – Only“active” codeanddataisactuallyinmemory • Allocatemorememorytoprocessasneeded. • #3ProvideProtection – Oneprocesscan’tinterferewithanother. • becausetheyoperateindifferentaddressspaces. – Userprocesscannotaccessprivilegedinformation • differentsectionsofaddressspaceshavedifferentpermissions.

C.Kozyrakis EE108bLecture14 11 Motivation#1:DRAMa“Cache” forDisk

• Fulladdressspaceisquitelarge: – 32bitaddresses:~4,000,000,000(4billion) bytes – 64bitaddresses:~16,000,000,000,000,000,000(16quintillion)bytes • Diskstorageis~100XcheaperthanDRAMstorage • Toaccesslargeamountsofdatainacosteffectivemanner,the bulkofthedatamustbestoredondisk

100 GB: ~$125 1 GB: ~$128 4 MB: ~$120 SRAM DRAM Disk

C.Kozyrakis EE108bLecture14 12 LevelsinMemoryHierarchy

cache virtualmemory

C CPU 8Ba 32BMemory 8KB disk CPU c Memory disk regsregs h e

Register Cache Memory Disk Memory size: 128B <4MB <16GB >100GB Speed(cycles): 0.51 120 80100 510M $/Mbyte: $30/MB $0.128/MB $0.001/MB linesize: 8B 32B 8KB

larger,slower,cheaper

C.Kozyrakis EE108bLecture14 13 DRAMvs.SRAMasa“Cache”

• DRAMvs.diskismoreextremethanSRAMvs.DRAM – Accesslatencies: • DRAM~10XslowerthanSRAM • Disk~ 100,000X slowerthanDRAM – Importanceofexploitingspatiallocality: • Firstbyteis~ 100,000X slowerthansuccessivebytesondisk – vs.~4Ximprovementforpagemodevs.regularaccessestoDRAM – Bottomline: • DesigndecisionsmadeforDRAMcachesdrivenbyenormous costofmisses

SRAM DRAM Disk

C.Kozyrakis EE108bLecture14 14 ImpactofThesePropertiesonDesign

• IfDRAMwastobeorganizedsimilartoanSRAMcache,howwould wesetthefollowingdesignparameters? – Linesize? • Large,sincediskbetterattransferringlargeblocksandminimizesmissrate – Associativity? • High,tomimimize missrate – Writethroughorwriteback? • Writeback,sincecan’taffordtoperformsmallwritestodisk

C.Kozyrakis EE108bLecture14 15 LocatinganObjectina“Cache”

• SRAMCache – Tagstoredwithcacheline – Mapsfromcacheblocktomemoryblocks • Fromcachedtouncachedform – Notagforblocknotincache – Hardwareretrievesinformation • canquicklymatchagainstmultipletags “Cache” Tag Data Object Name 0: D 243 X = X? 1: X 17 • • • • • • N-1: J 105

C.Kozyrakis EE108bLecture14 16 LocatinganObjectina“Cache” (cont.)

• DRAMCache – Eachallocatepageofvirtualmemoryhasentryinpagetable – Mappingfromvirtualpagestophysicalpages • Fromuncachedformtocachedform – Pagetableentryevenifpagenotinmemory • Specifiesdiskaddress – OSretrievesinformation “Cache” Location Data Object Name D: 0 0: 243 X J: On Disk 1: 17 • • • • • • X: 1 N-1: 105 C.Kozyrakis EE108bLecture14 17 ASystemwithPhysicalMemoryOnly

• Examples: – mostCraymachines,earlyPCs,nearlyallembeddedsystems,etc. Memory 0: Physical 1: Addresses

CPU

N-1:

AddressesgeneratedbytheCPUpointdirectlytobytesinphysicalmemory C.Kozyrakis EE108bLecture14 18 ASystemwithVirtualMemory

• Examples: Memory – workstations,servers,modernPCs,etc. 0: Page Table 1: Virtual Physical Addresses 0: Addresses 1:

CPU

P-1: N-1:

Disk Address Translation: Hardwareconverts virtual addresses to physical addresses viaanOSmanagedlookuptable( page table ) C.Kozyrakis EE108bLecture14 19 PageFaults(Similarto“CacheMisses”)

• Whatifanobjectisondiskratherthaninmemory? – Pagetableentryindicatesvirtualaddressnotinmemory – OSexceptionhandlerinvokedtomovedatafromdiskintomemory • currentprocesssuspends,otherscanresume • OShasfullcontroloverplacement,etc. Before fault After fault Memory Memory Page Table Page Table Virtual Physical Addresses Addresses Virtual Physical Addresses Addresses CPU CPU

Disk Disk C.Kozyrakis EE108bLecture14 20 ServicingaPageFault

(1)InitiateBlockRead • ProcessorSignals ProcessorProcessor Controller Reg (3)Read – ReadblockoflengthP Done startingatdiskaddress Xandstorestartingat Cache memoryaddressY Cache • ReadOccurs – DirectMemoryAccess MemoryI/Obus (DMA) MemoryI/Obus – UndercontrolofI/O (2)DMATransfer controller I/OI/O controller • I/OControllerSignals MemoryMemory controller Completion – Interruptprocessor – OSresumes diskDisk Diskdisk suspendedprocess

C.Kozyrakis EE108bLecture14 21 Motivation#2:MemoryManagement

• Multipleprocessescanresideinphysicalmemory. • Howdoweresolveaddressconflicts? – whatiftwoprocessesaccesssomethingatthesameaddress?

memory invisible to kernel user code $sp stack

the “brk” ptr runtime heap (via malloc) uninitialized data (.bss) initialized data (.data) program text (.text) forbidden 0

C.Kozyrakis EE108bLecture14 22 Solution:SeparateVirtualAddr.Spaces

– Virtualandphysicaladdressspacesdividedintoequalsizedblocks • blocksarecalled“pages” (bothvirtualandphysical) – Eachprocesshasitsownvirtualaddressspace • operatingsystemcontrolshowvirtualpagesasassignedtophysical memory 0 Virtual 0 Address Translation Physical Address VP 1 PP 2 Address Spacefor VP... 2 Space Process1: (DRAM) N-1

PP 7 (e.g., read/only Virtual 0 library code) Address VP 1 VP 2 PP 10 Spacefor ... Process2: M-1 N-1 C.Kozyrakis EE108bLecture14 23 Motivation#3:Protection

• Pagetableentrycontainsaccessrightsinformation – hardwareenforcesthisprotection(trapintoOSifviolationoccurs) Page Tables Memory Read? Write? Physical Addr 0: VP 0: Yes No PP 9 1: Process i: VP 1: Yes Yes PP 4 VP 2: No No XXXXXXX • • • • • • • • •

Read? Write? Physical Addr VP 0: Yes Yes PP 6 Process j: VP 1: Yes No PP 9 N-1: VP 2: No No XXXXXXX • • • • • • C.Kozyrakis • • EE108bLecture14• 24 PagingAddressTranslation

1K 1K

Page Table 2048 Page 2 1K Lookup 2048 Frame 2 1K 1024 Page 1 1K 1024 Frame 1 1K Frame 0 1K 0 Page 0 1K 0 Virtual Memory Physical Memory Virtual pages can map to any physical frame (fully associative)

C.Kozyrakis EE108bLecture14 25 PageTable/Translation

22 10 = 32 1 KB Pages Virtual address Page number Offset

Page Table Page Table Base Register Pagetable Access V Frame Validbitto index Rights indicateif Frame # Offset virtualpage 30 = iscurrently Accessrightsto 20 10 mapped definelegal accesses(Read, Tablelocatedin Write,Execute) physicalmemory To memory

C.Kozyrakis EE108bLecture14 26 ProcessAddressSpace

• Eachprocesshasitsownaddressspace – Eachprocessmustthereforehaveitsowntranslationtable – TranslationtablesarechangedonlybytheOS – Aprocesscannotgainaccesstoanotherprocess’saddressspace • Contextswitches – Steps 1. Savepreviousprocess’ state(registers,PC)intheProcessControlBlock (PCB) 2. Changepagetablepointertonewprocess’stable 3. FlushtheTLB(discussedlater) 4. InitializePCandregistersfromnewprocess’ PCB – Contextswitchesoccurona timeslice (whenthe scheduler determinesthenextprocessshouldgettheCPU)orwhenblocking forI/O

C.Kozyrakis EE108bLecture14 27 TranslationProcess

• Validpage – Checkaccessrights(R,W,X)againstaccesstype • Generatephysicaladdressifallowed • Generateaprotectionfaultifillegalaccess • Invalidpage – Pageisnotcurrentlymappedanda page fault isgenerated • Faultsarehandledbytheoperatingsystem – Protectionfaultisoftenaprogramerrorandtheprogramshould be terminated – Pagefaultrequiresthatanewframebeallocated,thepageentryis markedvalid,andtheinstructionrestarted • Frame table hasmappingfromphysicaladdresstovirtualaddress andtracksusedframes

C.Kozyrakis EE108bLecture14 28 UnalignedMemoryAccess

• Memoryaccessmightbealignedorunaligned

0 4 8 12 16

0 4 8 12 16

• Whathappensifunalignedaddressaccessstraddlesapage boundary? – Whatifonepageispresentandtheotherisnot? – Or,whatifneitherispresent? • MIPSarchitecturedisallowsunalignedmemoryaccess • Interestinglegacyproblemon80 whichdoessupportunaligned access

C.Kozyrakis EE108bLecture14 29 PageTableProblems

• “Internal” Fragmentationresultingfromfixedsizepagessincenot allofpagewillbefilledwithdata – Problemgetsworseaspagesizeisincreased • Eachdataaccessnowtakestwomemoryaccesses – Firstmemoryreferenceusesthepagetablebaseregistertolookup theframe – Secondmemoryaccesstoactuallyfetchthevalue • Pagetablesarelarge – Considerthecasewherethemachineoffersa32bitaddressspace anduses4KBpages • Pagetablecontains2 32 /2 10 =2 22 entries • Assumeeachentrycontains~32bits • Pagetablerequires4MBofRAMperprocess!

C.Kozyrakis EE108bLecture14 30 ImprovingAccessTime

• Butweknowhowtofixthisproblem,let’scachethepagetableand leveragetheprinciplesoflocality • Ratherthanusethenormalcaches,wewilluseaspecialcache knownastheTranslationLookaside Buffer(TLB) – AllaccessesarelookedupintheTLB • Ahitprovidesthephysicalframenumber • Onamiss,lookinthepagetablefortheframe – TLBs areusuallysmallerthanthecache • Higherassociativity iscommon • Similarspeedtoacacheaccess • Treatlikeanormalcacheandusewritebackvs.writethrough

C.Kozyrakis EE108bLecture14 31 TLBEntries

• JustholdsacachedPageTableEntry(PTE) • AdditionalbitsforLRU,etc.maybeadded Virtual Page Physical Frame Dirty LRU Valid Access Rights

• OptimizationistoaccessTLBandcacheinparallel hit Virtual Addr Phy Addr miss TLB Main CPU Cache Lookup Memory

hit data miss map entry Access page tables for translation (from memory!) data

C.Kozyrakis EE108bLecture14 32 AddressTranslationwithaTLB

n–1p p–1 0 virtual page number page offset virtual address

validtag physical page number . . . TLB

= TLB hit physical address

tag index byte offset valid tag data Cache

= cache hit data C.Kozyrakis EE108bLecture14 33 TLBMissHandler

• ATLBmisscanbehandledbysoftwareorhardware – Missrate:0.01%– 1% • Insoftware,aspecialOShandlerlooksupthevalueinthepage table – TeninstructionsonR3000/R4000 • Inhardware,microcodeoradedicatedfinitestatemachine(FSM) looksupthevalue – Pagetablesarestoredinregularphysicalmemory – ItisthereforelikelythatthedataisintheL2cache – Advantages • Noneedtotakeanexception • Betterperformancebutmaybedominatedbycachemiss

C.Kozyrakis EE108bLecture14 34 TLBCaveats

• Whathappensonacontextswitch? – IftheTLBentrieshaveaProcessID(PID)associatedwiththem, then nothingneedstobedone – Otherwise,theOSmustflushtheentriesintheTLB • LimitedReach – 64entryTLBwith8KBpagesmaps0.5MB – SmallerthanmanyL2caches – TLBmissrate>L2missrate! – Motivateslargerpagesize

C.Kozyrakis EE108bLecture14 35 TLBCaseStudy: MIPSR2000/R3000

• ConsidertheMIPSR2000/R3000processors – Addressesare32bitswith4KBpages(12bitoffset) – TLBhas64entries,fullyassociative – Eachentryis64bitswide:

20 6 6 20 1 1 1 1 8 Virtual Page PID 0 Physical Page N D V G 0 PID ProcessID N Donotcachememoryaddress D Dirtybit V Validbit G Global(validregardlessofPID)

C.Kozyrakis EE108bLecture14 36 PageSize

• LargerPages – Advantages • Smallerpagetables • Fewerpagefaultsandmoreefficienttransferwithlargerapplications • ImprovedTLBcoverage – Disadvantages • Higherinternalfragmentation • SmallerPages – Advantages • Improvedtimetostartupsmallprocesseswithfewerpages • Internalfragmentationislow(importantforsmallprograms) – Disadvantages • Highoverheadinlargepagetables • Generaltrendtowardlargerpages – 1978:512B – 1984:4KB – 1990:16KB – 2000:64KB

C.Kozyrakis EE108bLecture14 37 MultiplePageSizes

• Somemachinessupportmultiplepagesizestobalancecompeting goals – Pagesizedependentuponapplication • OSkerneluseslargepages • Userapplicationsusesmallerpages • SPARC:8KB,64KB,1MB,4MB • MIPSR4000:4KB– 16MB – Results • ComplicatesTLBdesignsincemustaccountforpagesize

C.Kozyrakis EE108bLecture14 38 PageSharing

• Anotherbenefitofpagingisthatwecaneasilysharepagesby mappingmultiplepagestothesamephysicalframe • Usefulinmanydifferentcircumstances – Usefulforapplicationsthatneedtosharedata – Readonlydataforapplications,OS,etc. – Example:Unix fork() systemcallcreatessecondprocesswitha “copy” ofthecurrentaddressspace • Oftenimplementedusing copy-on-write – Secondprocesshasreadonlyaccesstothepage – Ifsecondprocesswantstowrite,thenapagefaultoccursandthe pageiscopied

C.Kozyrakis EE108bLecture14 39 PageSharing(cont)

Virtual Address

1K Physical 0 Address Virtual Memory Process 1 1K

0 Physical 1K Memory

0 Virtual Memory Process 2 C.Kozyrakis EE108bLecture14 40 CopyonWrite

Virtual Address

1K Physical 0 Address Virtual Memory Process 1 1K 1K 0 Physical 1K Memory

0 Virtual Memory Process 2 C.Kozyrakis EE108bLecture14 41 PageTableSize

• Pagetablesizeisproportionaltosizeofaddressspace – Evenwhenapplicationsusefewpages,pagetableisstillverylarge • Example:80 x86PageTables – Virtualaddressesare32bits – Pagesizeis4KB – Totalnumberofpages:2 32 /2 12 =1Million – PageTableEntry(PTE)are32bitswide • 20bitFrameaddress • Dirtybit,accessedbit,valid(present)bit • 9flagandunusedbits – Totalpagetablesizeistherefore2 20 × 4bytes=4MB – But,onlyasmallfractionofthosepagesareactuallyused! • Thelargepagetablesizeismadeevenworsebythefactthatthepage tablemustbeinmemory – Otherwise,whatwouldhappenifthepagetableweresuddenly swappedout?

C.Kozyrakis EE108bLecture14 42 PagingthePageTables

• Onesolutionistousemultilevelpagetables – IfeachPTEis4bytes,theneach4KBframecanhold1024PTEs – Useanadditional“masterblock” 4KBframetoindexframes containingPTEs • Masterframehas1024PFEs (PageFrameEntries) • EachPFEpointstoaframecontaining1KBPTEs • EachPTEpointstoapage – Now,onlythe“masterblock” mustbefixedinmemory

C.Kozyrakis EE108bLecture14 43 TwolevelPaging

PFE PTE Offset PTR + + +

MasterBlock •Problems PageTable DesiredPage –Multiplepage faults •AccessingaPTEpagetablecancauseapagefault •Accessingtheactualpagecancauseasecondpagefault –TLBplaysanevenmoreimportantrole

C.Kozyrakis EE108bLecture14 44 VirtualMemoryOverview

• Wecanextendourcachingideaonestepfurtheranduse secondarystorageasoneoftheelementsinourmemoryhierarchy • Virtual memory istheprocessofusingsecondarystoragetoto createanillusionofanevenlargermemoryspacewithoutincurring thecost • Removesprogrammingburdensofmanaginglimitedphysical memory

C.Kozyrakis EE108bLecture14 45 VirtualMemoryApproach

• Presentstheillusionofalargeaddressspacetoeveryapplication – Eachprocessorprogrambelievesithasitsowncompletecopyof physicalmemory(itsowncopyoftheaddressspace) • Strategy – Pagesnotcurrentlyinusecanbewrittenbacktosecondarystorage – Whenpageownershipchanges,savethecurrentcontentstodiskso theycanberestoredlater • Costs – Addresstranslationisstillrequiredtoconvertvirtualaddressesinto physicaladdresses – Writingtothediskisaslowoperation – TLBmissescanlimitperformance

C.Kozyrakis EE108bLecture14 46 VirtualAddresses

• Eachvirtualaddresscanbemappedintooneofthreecategories – Aphysicalmemoryaddress(bywayofTLBorPT) – Alocationondisk(wesufferapagemiss) – Nothing– correspondstoaninvalidmemoryaddress

C.Kozyrakis EE108bLecture14 47 PageMisses

• Ifapagemisscorrespondstoavalidaddressthatisnolonger available,thenitmustreloaded – Locatethecontentsondiskthatwereswappedout – Findanavailablephysicalframe(orevictanotherframe) – Writethecontentsintotheframe – Updatethepagetablemappings(forboththisprocessandtheone thatwasevictedifnecessary) • Pagesarefetchedonlyon demand ,whenaccessed

Memory Disk Reg Cache

frame page

C.Kozyrakis EE108bLecture14 48 PageFault

• Programsusevirtualaddresswhichmayincludepagesthatcurrently resideinsecondarystorage • Ifanentryisnotfoundthentheaddressisnotcurrentlylocatedinphysical memory,anda page fault issaidtohaveoccurred – Apagefaultishandledbytheoperatingsystem – Theitemmustbefirstfetchedfromdiskstorageandloadedinto aphysical frame – Apagetableentrymustbecreated fault – Theaccessisrepeated handler missing (in OS) item fault Virtual Address Main address Translation valid Memory Disk physical address OS performs this transfer

C.Kozyrakis EE108bLecture14 49