![計算機系統(tǒng)基礎(chǔ):虛存_第1頁](http://file4.renrendoc.com/view14/M07/3C/11/wKhkGWZDfTiAKvN1AABu9IuXPJI179.jpg)
![計算機系統(tǒng)基礎(chǔ):虛存_第2頁](http://file4.renrendoc.com/view14/M07/3C/11/wKhkGWZDfTiAKvN1AABu9IuXPJI1792.jpg)
![計算機系統(tǒng)基礎(chǔ):虛存_第3頁](http://file4.renrendoc.com/view14/M07/3C/11/wKhkGWZDfTiAKvN1AABu9IuXPJI1793.jpg)
![計算機系統(tǒng)基礎(chǔ):虛存_第4頁](http://file4.renrendoc.com/view14/M07/3C/11/wKhkGWZDfTiAKvN1AABu9IuXPJI1794.jpg)
![計算機系統(tǒng)基礎(chǔ):虛存_第5頁](http://file4.renrendoc.com/view14/M07/3C/11/wKhkGWZDfTiAKvN1AABu9IuXPJI1795.jpg)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1操作系統(tǒng)
虛存&UnixI/O
5次課2ASystemUsingPhysicalAddressingUsedin“simple”systemslikeembeddedmicrocontrollersindeviceslikecars,elevators,anddigitalpictureframes0:1:M-1:MainmemoryCPU2:3:4:5:6:7:Physicaladdress(PA)Dataword8:...43ASystemUsingVirtualAddressingUsedinallmodernservers,desktops,andlaptopsOneofthegreatideasincomputerscience0:1:M-1:MainmemoryMMU2:3:4:5:6:7:Physicaladdress(PA)Dataword8:...CPUVirtualaddress(VA)CPUChip441004VirtualAddressingAddresstranslationConvertingavirtualaddresstoaphysicaladdressRequiresclosecooperationbetweentheCPUhardwareandtheoperatingsystemHW:thememorymanagementunit(MMU)DedicatedhardwareontheCPUchiptotranslatevirtualaddressesontheflySW:Alook-uptableStoredinmainmemoryContentsaremanagedbytheoperatingsystem5UsingMainMemoryasaCacheDRAMSRAMDisk6DesignConsiderationsLinesize?Large,sincediskbetterattransferringlargeblocksAssociativity?Full,tominimizemissrateWritethroughorwriteback?Writeback,sincecan’taffordtoperformsmallwritestodisk7PageThedataondiskispartitionedintopagesServeasthetransferunitsbetweenthediskandthemainmemoryvirtualpages(VPs)physicalpages(PPs)orpageframesPP2m-p-1PhysicalmemoryEmptyEmptyUncachedVP0VP1VP2n-p-1VirtualmemoryUnallocatedCachedUncachedUnallocatedCachedUncachedPP0PP1EmptyCached0N-1M-10Virtualpages(VPs)storedondiskPhysicalpages(PPs)cachedinDRAM8PageAttributesunallocatedallocatedcacheduncached9PageTableOsmaintainsapagetableforeachallocatedpageofvirtualmemoryAPageTableEntry(PTE)MappingfromavirtualpagetoitscorrespondingphysicalpageifthevirtualpageiscachedSpecifiesthediskaddresstostorethevirtualpagewhichisuncachedOScanuseittoretrieveinformation10PageTableNULLMemoryresidentpagetable(physicalpageordiskaddress)PhysicalMemoryDiskStorage(swapfileorregularfilesystemfile)Valid1101111000VirtualPageNumberPTE0PTE9PP0PP5VP4VP1VP211PageHitsMemoryAddressTranslation:HardwareconvertsvirtualaddressestophysicaladdressesviaanOS-managedlookuptable(pagetable)CPU0:1:N-1:0:1:P-1:PageTableDiskVirtualAddressesPhysicalAddresses12PageFaultsPagetableentryindicatesvirtualaddressnotinmemoryOSexceptionhandlerinvokedtomovedatafromdiskintomemorycurrentprocesssuspends,otherscanresumeOShasfullcontroloverplacement,etc.13PageFaultsDemandpagingCPUMemoryPageTableDiskVirtualAddressesPhysicalAddressesCPUMemoryPageTableDiskVirtualAddressesPhysicalAddressesBeforefaultAfterfault14ServicingaPageFaultProcessorSignalsControllerReadblockoflengthPstartingatdiskaddressXandstorestartingatmemoryaddressYdiskDiskdiskDiskMemory-I/ObusProcessorCacheMemoryI/OcontrollerReg(1)InitiateBlockRead15ServicingaPageFaultReadOccursDirectMemoryAccess(DMA)UndercontrolofI/OcontrollerdiskDiskdiskDiskMemory-I/ObusProcessorCacheMemoryI/OcontrollerReg(2)DMATransfer16ServicingaPageFaultI/OControllerSignalsCompletionInterruptprocessorOSresumessuspendedprocessdiskDiskdiskDiskMemory-I/ObusProcessorCacheMemoryI/OcontrollerReg(3)ReadDoneSimplifyMemoryManagementKeyidea:eachprocesshasitsownvirtualaddressspaceItcanviewmemoryasasimplelineararrayVirtualAddressSpaceforProcess1:PhysicalAddressSpace(DRAM)0N-1(e.g.,read-onlylibrarycode)VirtualAddressSpaceforProcess2:VP1VP2...0N-1VP1VP2...PP2PP6PP8...0M-1AddresstranslationSimplifyingLinkingLinkingEachprogramhassimilarvirtualaddressspaceCode,stack,andsharedlibrariesalwaysstartatthesameaddressKernelvirtualmemoryMemory-mappedregionforsharedlibrariesRun-timeheap(createdbymalloc)Userstack(createdatruntime)Unused0%esp
(stackpointer)Memoryinvisibletousercodebrk0xc00000000x080480000x40000000Read/writesegment(.data,.bss)Read-onlysegment(.init,.text,.rodata)LoadedfromtheexecutablefileSimplifyingLoadingLoadingexecve()allocatesvirtualpagesfor.textand.datasections
(createsPTEsmarkedasinvalid(uncached))The.textand.datasectionsarecopied,pagebypage,ondemandbythevirtualmemorysystemKernelvirtualmemoryRun-timeheap(createdbymalloc)Userstack(createdatruntime)Unused0%esp
(stackpointer)Memoryinvisibletousercode0xc00000000x080480000x40000000Read/writesegment(.data,.bss)Read-onlysegment(.init,.text,.rodata)LoadedfromtheexecutablefileSimplifyMemoryallocationMemoryallocationEachvirtualpagecanbemappedtoanyphysicalpageAvirtualpagecanbestoredindifferentphysicalpagesatdifferenttimesVirtualAddressSpaceforProcess1:PhysicalAddressSpace(DRAM)0N-1(e.g.,read-onlylibrarycode)VirtualAddressSpaceforProcess2:VP1VP2...0N-1VP1VP2...PP2PP6PP8...0M-1AddresstranslationSimplifyMemorySharingSharingcodeanddataamongprocessesMapvirtualpagestothesamephysicalpage
(e.g.PP6)VirtualAddressSpaceforProcess1:PhysicalAddressSpace(DRAM)0N-1(e.g.,read-onlylibrarycode)VirtualAddressSpaceforProcess2:VP1VP2...0N-1VP1VP2...PP2PP6PP8...0M-1AddresstranslationIsolateAddressSpaceExtendPTEswithpermissionbitsThesamephysicalpagehasdifferentpermissionfordifferentprocessProcessi:AddressREADWRITEPP6YesNoPP4YesYesPP2YesVP0:VP1:VP2:???Processj:YesSUPNoNoYesAddressREADWRITEPP9YesNoPP6YesYesPP11YesYesSUPNoYesNoVP0:VP1:VP2:PhysicalAddressSpacePP2PP4PP6PP8PP9PP11IsolateAddressSpaceEverytranslationwillchecktheseifviolated,sendprocessSIGSEGV(segmentationfault)Processi:AddressREADWRITEPP6YesNoPP4YesYesPP2YesVP0:VP1:VP2:???Processj:YesSUPNoNoYesAddressREADWRITEPP9YesNoPP6YesYesPP11YesYesSUPNoYesNoVP0:VP1:VP2:PhysicalAddressSpacePP2PP4PP6PP8PP9PP1124AddressTranslationProcessorHardwareAddrTransMechanismfaulthandlerMainMemorySecondarymemoryaa'
pagefaultphysicaladdressOSperformsthistransfer(onlyifmiss)virtualaddresspartoftheon-chipmemorymgmtunit(MMU)25AddressTranslationvirtualpagenumberpageoffsetvirtualaddressphysicalpagenumberpageoffsetphysicaladdress0p–1addresstranslationpm–1n–10p–1pNoticethatthepageoffsetbitsdon'tchangeasaresultoftranslation26AddressTranslationviaPageTablevirtualpagenumber(VPN)pageoffsetvirtualaddressphysicalpagenumber(PPN)pageoffsetphysicaladdress0p–1pm–1n–10p–1ppagetablebaseregister(PTBR)ifvalid=0thenpagenotinmemoryvalidphysicalpagenumber(PPN)accessVPNactsastableindex27PageHitVA:virtualaddressPTEA:pagetableentryaddressPTE:pagetableentryPA:physicaladdressMMUCache/MemoryPADataCPUVACPUChipPTEAPTE123451)ProcessorsendsvirtualaddresstoMMU2-3)MMUfetchesPTEfrompagetableinmemory4)MMUsendsphysicaladdresstocache/memory5)Cache/memorysendsdatawordtoprocessor28PageFaultsMMUCache/MemoryCPUVACPUChipPTEAPTE12345DiskPagefaulthandlerVictimpageNewpageException671)ProcessorsendsvirtualaddresstoMMU2-3)MMUfetchesPTEfrompagetableinmemory4)Validbitiszero,soMMUtriggerspagefaultexception5)Handleridentifiesvictim(and,ifdirty,pagesitouttodisk)6)HandlerpagesinnewpageandupdatesPTEinmemory7)Handlerreturnstooriginalprocess,restartingfaultinginstruction29IntegratingCachesandVMCPUTrans-lationCacheMainMemoryVAPAmisshitdata30IntegratingCachesandVMMostCaches“PhysicallyAddressed”AccessedbyphysicaladdressesAllowsmultipleprocessestohaveblocksincacheatsametimeAllowsmultipleprocessestosharepagesCachedoesn’tneedtobeconcernedwithprotectionissuesAccessrightscheckedaspartofaddresstranslation31VACPUMMUPTEAPTEPADataMemoryPAPAmissPTEAPTEAmissPTEAhitPAhitDataPTEL1cacheCPUChipIntegratingCachesandVM32SpeedingupTranslationwithaTLB“TranslationLookasideBuffer”(TLB)SmallhardwarecacheinMMUMapsvirtualpagenumberstophysicalpagenumbers33MMUCache/MemoryPADataCPUVACPUChipPTE1245TLBVPN3TLBHitATLBhiteliminatesamemoryaccess34TLBMissMMUCache/MemoryPADataCPUVACPUChipPTE1256TLBVPN4PTEA3ATLBmissincursanadditionalmemoryaccess(PTE)
Fortunately,TLBmissesarerare.Why?35SpeedingupTranslationwithaTLB36AddressTranslationvirtualpagenumberpageoffsetvirtualaddressphysicalpagenumberpageoffsetphysicaladdress0p–1addresstranslationpm–1n–10p–1pTLBtagTLBindexCachetagCacheindexCacheoffset37SpeedingupTranslationwithaTLBvirtualaddressvirtualpagenumberpageoffsetn–10p–1pvalidphysicalpagenumbertagvalidtagdatadata=cachehittagindex=TLBhitTLBCache...physicaladdressphysicalpagenumberpageoffset38Multi-LevelPageTablesGiven:X86:32-bitaddressspace
4KB(212)pagesize,4-bytePTEX86-64:48-bitaddressspace
4KB(212)pagesize,8-bytePTEProblem:X86:Wouldneeda4MBpagetable!220*4bytes(20bit=32bit–12bit)X86-64:Wouldneeda512GBpagetable!236*8bytes(36bit=48bit–12bit)39Multi-LevelPageTablesCommonsolutionmulti-levelpagetablese.g.,2-levelpagetableLevel1table:1024entries,eachofwhichpointstoaLevel2pagetable.Level2table:1024entries,eachofwhichpointstoapageLevel1Table...Level2Tables40Multi-LevelPageTablesLevel1pagetable...Level2pagetablesVP0...VP1023VP1024...VP2047Gap0PTE0...PTE1023PTE0...PTE10231023nullPTEsPTE10231023unallocatedpagesVP9215Virtualmemory(9-1K)nullPTEsPTE0PTE1PTE2(null)PTE3(null)PTE4(null)PTE5(null)PTE6(null)PTE7(null)PTE82KallocatedVMpagesforcodeanddata6KunallocatedVMpages1023unallocatedpages1allocatedVMpageforthestack32bitaddresses,4KBpages,4-bytePTEs41Multi-LevelPageTables42PDEPDBRphysicaladdressofpagetablebase(ifP=1)physicaladdressofpagebase(ifP=1)physicaladdressofpagedirectorywordoffsetintopagedirectorywordoffsetintopagetablepagedirectorypagetableVPN110VPO1012VPN2VirtualaddressPTEPPNPPO2012PhysicaladdresswordoffsetintophysicalandvirtualpagePagetablesTranslation43Case1/1:pagetableandpagepresent.MMUAction:MMUbuildphysicaladdressandfetchdataword.OSactionnoneVPNVPN1VPN2PDEPDBRPPNPPO201220VPO12p=1PTEp=1DatapagedataPagedirectoryPagetableMemDiskTranslatingwiththeP6pagetables(case1/1)44Case1/0:pagetablepresentbutpagemissing.MMUAction:pagefaultexceptionhandlerreceivesthefollowingargs:VAthatcausedfaultfaultcausedbynon-presentpageorlevelprotectionviolationread/writeuser/supervisorVPNVPN1VPN2PDEPDBR20VPO12p=1PTEPagedirectoryPagetableMemDiskDatapagedatap=0TranslatingwiththeP6pagetables(case1/0)45OSAction:Checkforalegalvirtualaddress.ReadPTEthroughPDE.Findfreephysicalpage(swappingoutcurrentpageifnecessary)ReadvirtualpagefromdiskandcopytovirtualpageRestartfaultinginstructionbyreturningfromexceptionhandler.VPNVPN1VPN2PDEPDBR20VPO12p=1PTEp=1PagedirectoryPagetableDatapagedataPPNPPO2012MemDiskTranslatingwiththeP6pagetables(case1/0,cont)46Case0/1:pagetablemissingbutpagepresent.Introducesconsistencyissue.potentiallyeverypageoutrequiresupdateofdiskpagetable.Linuxdisallowsthisifapagetableisswappedout,thenswapoutitsdatapagestoo.VPNVPN1VPN2PDEPDBR20VPO12p=0PTEp=1PagedirectoryPagetableMemDiskDatapagedataTranslatingwiththeP6pagetables(case0/1)47Case0/0:pagetableandpagemissing.MMUAction:pagefaultexceptionVPNVPN1VPN2PDEPDBR20VPO12p=0PTEPagedirectoryPagetableMemDiskDatapagedatap=0TranslatingwiththeP6pagetables(case0/0)48OSaction:swapinpagetable.restartfaultinginstructionbyreturningfromhandler.Likecase0/1fromhereon.VPNVPN1VPN2PDEPDBR20VPO12p=1PTEPagedirectoryPagetableMemDiskDatapagedatap=0TranslatingwiththeP6pagetables(case0/0,cont)49Corei7The64-bitNehalemmicroarchitectureCurrentsupport48-bit(256TB)virtualaddressspaceand52-bit(4PB)physicaladdressspaceCompatibilitymodesupport32-bit(4GB)virtualandphysicaladdressspacesCorei7Summery...50...CPUVPNVPO3612432Virtualaddress(VA)L1TLB(16sets,4entries/set)PPNPPO4012TLBmissTLB
hitphysicaladdress
(PA)result32/64CT4066L2,L3,and
mainmemoryL1d-cache
(64sets,8lines/set)L1hitL1missTLBTTLBICorei7AddressTranslationCICOVPN1VPN2VPN3VPN4PTEPTEPTEPTECR3PageTables999951PagetablephysicalbaseaddrUnusedGPSACDWTU/SR/WP=1XDDisableorenableinstructionfetchesBaseaddr
40mostsignificantbitsofbaseaddressofchildpage
table(forcespagetablestobe4KBaligned)Gglobalpage(don’tevictfromTLBontaskswitch)PSPagesizeeither4K(0)or4M(1)(definedforLevel1
PTEsonly)AReferencebit(setbyMMUonreadsandwrites,cleared
bysoftware)CDCachedisabled(1)orenabled(0)forchildpagetableWTWrite-throughorwrite-backcachepolicyU/SUserorsupervisor(kernel)modeaccesspermissionR/WRead-onlyorread-writeaccesspermissiomPChildpagetablepresentinmemory(1)ornot(0)5112119876543210AvailableforOS(pagetablelocationinsecondarystorage)P=0Level1,Level2andLevel3PageTableEntryUnused52XD626352PagetablephysicalbaseaddrUnusedGDACDWTU/SR/WP=1XDDisableorenableinstructionfetchesBaseaddr
40mostsignificantbitsofbaseaddressofchildpage
table(forcespagetablestobe4KBaligned)Gglobalpage(don’tevictfromTLBontaskswitch)DDirtybit(SetbyMMUonwrites,clearedbysoftware)AReferencebit(setbyMMUonreadsandwrites,cleared
bysoftware)CDCachedisabled(1)orenabled(0)forchildpagetableWTWrite-throughorwrite-backcachepolicyU/SUserorsupervisor(kernel)modeaccesspermissionR/WRead-onlyorread-writeaccesspermissiomPChildpagetablepresentinmemory(1)ornot(0)5112119876543210AvailableforOS(pagetablelocationinsecondarystorage)P=0Level4PageTableEntryUnused52XD626353Physical
address
ofL1PTphysicaladdressofpage512GB
region
perentryL1PT
Pageglobal
directory12Virtualaddress4012PhysicaladdressOffsetintophysicalandvirtualpagePagetablesTranslationVPN1VPN2VPN3VPN4VPO9999PPOL1PTECR31GB
region
perentryL2PT
Pageupper
directoryL1PTE2MB
region
perentryL3PT
Pagemiddle
directoryL1PTE4KB
region
perentryL4PT
Page
directoryL1PTEPPO1240999940404040CuteTrickforSpeedingUpL1AccessObservationBitsthatdetermineCIidenticalinvirtualandphysicaladdressCanindexintocachewhileaddresstranslationtakingplaceGenerallywehitinTLB,soPPNbits(CTbits)availablenext“Virtuallyindexed,physicallytagged”CachecarefullysizedtomakethispossiblePhysicaladdress(PA)CTCO366CI6Virtualaddress(VA)VPNVPO3612PPOPPNAddressTranslationNoChangeCIL1CacheCTTagCheck55ProcessorpackageCorei7MemorySystemCorex4RegistersInstructionfetchL1d-cache32KB,8-wayL1i-cache
32KB,8-wayL2unifiedcache
256KB,8-wayL3unifiedcache
8MB,16-way
(sharedbyallcores)MMU
(addrtranslation)L1d-TLB
64entries,4-wayL1i-TLB
64entries,4-wayL2unifiedTLB
512entries,4-wayQuickPathinterconnect
4links@25.6GB/s
102.4GB/stotalDDR3memorycontroller
3x64bit@10.66GB/s
32GB/stotal(sharedbyallcores)MainmemoryToother
coresToI/O
bridge56ExampleIna32-bitmachinewesubdividethevirtualaddressinto4piecesasfollows:Weusea3-levelpagetable,suchthatthefirst8bitsareforthefirstlevelandsoon.Physicaladdressesare44bitsandthereare4protectionbitsperpage8bit8bit4bit12bit57ExamplePleasegivethepagetableforaprocessthathasacodesegmentof48Kstartingataddress0x1000000,adatasegmentof600Kstartingataddress0x80000000andastacksegmentof64Kstartingataddress0xf0000000andgrowingupward(towardshigheraddresses)?58Example48Kstartingataddress0x1000000address:10,00,0,000to10,00,b,fff48K:12pages600Kstartingataddress0x80000000address:80,00,0,000to80,09,5,fff600K:150pages64Kstartingataddress0xf0000000address:f0,00,0,000tof0,00,f,fff64K:16pages59Example…f0…80…10………00…09…00……00…b…0ff…0ff…0F…b…060Process-specificdatastructures
(e.g.taskandmmstructs,pagetables,kernelstack)PhysicalmemoryKernelcodeanddataUserstackMemorymappedregion
forsharedlibrariesRun-timeheap(viamalloc)Uninitializeddata(.bss)Initializeddata(.data)Programtext(.text)Kernel
virtual
memoryProcess
virtual
memoryDifferentfor
eachprocessIdenticalfor
eachprocess%espbrkx08048000(32)
x40000000(64)LinuxVirtualMemorySystem61pgd:pagedirectoryaddressvm_prot:read/writepermissionsforthisareavm_nextvm_nexttask_structmm_structpgdmmmmapvm_area_structvm_endvm_protvm_startvm_endvm_protvm_startvm_endvm_protvm_nextvm_startprocessvirtualmemorytextdatasharedlibraries00x080480000x0804a0200x40000000vm_flagsvm_flagsvm_flagsLinuxorganizesVMasacollectionof“areas”62vm_flagssharedwithotherprocessesorprivatetothisprocessLinuxorganizesVMasacollectionof“areas”vm_nextvm_nexttask_structmm_structpgdmmmmapvm_area_structvm_endvm_protvm_startvm_endvm_protvm_startvm_endvm_protvm_nextvm_startprocessvirtualmemorytextdatasharedlibraries00x080480000x0804a0200x40000000vm_flagsvm_flagsvm_flags63IstheVAlegal?i.e.isitinanareadefinedbyavm_area_struct?ifnotthensignalsegmentationviolation(e.g.(1))vm_area_structvm_endr/ovm_nextvm_startvm_endr/wvm_nextvm_startvm_endr/ovm_nextvm_startprocessvirtualmemorytextdatasharedlibraries0writereadread123Linuxpagefaulthandling64Istheoperationlegal?i.e.,cantheprocessread/writethisarea?ifnotthensignalprotectionviolation(e.g.,(2))IfOK,handlefaulte.g.,(3)Linuxpagefaulthandlingvm_area_structvm_endr/ovm_nextvm_startvm_endr/wvm_nextvm_startvm_endr/ovm_nextvm_startprocessvirtualmemorytextdatasharedlibraries0writereadread12365CreationofnewVMareadonevia“memorymapping”createnewvm_area_structandpagetablesforareaMemorymapping66Areacanbebackedby(i.e.,getitsinitialvaluesfrom)regularfileondisk(e.g.,anexecutableobjectfile)initialpagebytescomefromasectionofafileanonymousfile(e.g.nothing)Firstfaultwillallocateaphysicalpagefullof0’s(demand-zeropage)Oncethepageiswrittento(dirtied),itislikeanyotherpageMemorymapping67Dirtypagesarecopiedbackandforthbetweenmemoryandaspecialswapfile.Memorymapping68Keypoint:novirtualpagesarecopiedintophysicalmemoryuntiltheyarereferenced!knownas“demandpaging”crucialfortimeandspaceefficiencyDemandPaging69Torunanewprogrampinthecurrentprocessusingexec():Freeallvm_area_structsandpagetablesforoldareas.kernelcode/dataMemorymappedregionforsharedlibrariesruntimeheap(viamalloc)programtext(.text)initializeddata(.data)uninitializeddata(.bss)stackforbidden0%espprocessVMbrk0xc0000000physicalmemorysameforeachprocessprocess-specificdatastructures(pagetables,taskandmmstructsKernalstack)kernelVM.data.textpdemand-zerodemand-zerolibc.so.data.textExec()revisited70Torunanewprogrampinthecurrentprocessusingexec():createnewvm_area_structsandpagetablesfornewareas.stack,bss,data,text,sharedlibs.Exec()revisitedkernelcode/dataMemorymappedregionforsharedlibrariesruntimeheap(viamalloc)programtext(.text)initializeddata(.data)uninitializeddata(.bss)stackforbidden0%espprocessVMbrk0xc0000000physicalmemorysameforeachprocessprocess-specificdatastructures(pagetables,taskandmmstructsKernalstack)kernelVM.data.textpdemand-zerodemand-zerolibc.so.data.text71Torunanewprogramp
inthecurrentprocessusingexec():createnewvm_area_structs
andpagetablesfor
newareas.textanddatabackedbyELFexecutableobjectfile.bssandstackinitializedtozero.Exec()revisitedkernelcode/dataMemorymappedregionforsharedlibrariesruntimeheap(viamalloc)programtext(.text)initializeddata(.data)uninitializeddata(.bss)stackforbidden0%espprocessVMbrk0xc0000000physicalmemorysameforeachprocessprocess-specificdatastructures(pagetables,taskandmmstructsKernalstack)kernelVM.data.textpdemand-zerodemand-zerolibc.so.data.text72Torunanewprogrampinthecurrentprocessusingexec():setPCtoentrypointin.textLinuxwillswapincodeanddatapagesasneeded.Exec()revisitedkernelcode/dataMemorymappedregionforsharedlibrariesruntimeheap(viamalloc)programtext(.text)initializeddata(.data)uninitializeddata(.bss)stackforbidden0%espprocessVMbrk0xc0000000physicalmemorysameforeachprocessprocess-specificdatastructures(pagetables,taskandmmstructsKernalstack)kernelVM.data.textpdemand-zerodemand-zerolibc.so.data.text73void*mmap(void*start,int
len,int
prot,
intflags,int
fd,intoffset)Maplenbytesstartingatoffsetoffset
ofthe
filespecifiedbyfiledescriptionfd,preferablyataddressstart(usually0fordon’tcare).prot:MAP_READ,MAP_WRITEflags:MAP_PRIVATE,MAP_SHAREDReturnapointertothemappedareaUser-levelmemorymappingvoid*mmap(void*start,int
len,
int
prot,int
flags,int
fd,int
offset)len
bytesstart(oraddresschosenbykernel)ProcessvirtualmemoryDiskfilespecifiedbyfiledescriptorfdlen
bytesoffset(bytes)00User-levelmemorymapping75Example:fastfilecopyUsefulforapplicationslikeWebserversthatneedtoquicklycopyfiles.mmapallowsfiletransferswithoutcopyingintouserspace.
User-levelmemorymapping76/**mmapcopy-usesmmaptocopyfilefdtostdout*/voidmmapcopy(intfd,intsize){char*bufp;
/*mapthefiletoanewVMarea*/bufp=mmap(0,size,PROT_READ,
MAP_PRIVATE,fd,0);
/*writetheVMareatostdout*/write(1,bufp,size);return;}mmap()example:fastfilecopy77intmain(intargc,char**argv){structstatstat;
/*checkforrequiredcommandlineargument*/if(argc!=2){printf(“usage:%s<filename>\n”,argv[0]);exit(0);}
/*openthefileandgetitssize*/fd=open(argv[1],O_RDONLY,0);fstat(fd,&stat);
mmapcopy(fd,stat.st_size);}mmap()example:fastfilecopy78Tocreateanewprocessusingfork:makecopiesoftheoldprocess’smm_struct,vm_area_struct,andpagetablesTwoprocessesaresharingalloftheirpages
(Atthispoint)Howtogetseparatespaceswithoutcopyingallthevirtualpagesfromonespacetoanother?“CopyOnWrite”(COW)technique.Fork()revisted79SharedObjectAnobjectwhichismappedintoanareaofvirtualmemoryofaprocessAnywritesthattheprocessmakestothatareaarevisibletoanyotherprocessesthathavealsomappedthesharedobjectintotheirvirtualmemoryThechangesarealsoreflectedintheoriginalobjectondiskSharedAreaAvirtualmemoryareathatasharedobjectismappedSharedObjectSharingRevisited:SharedObjectsProcess1mapsthesharedobjectSharedobjectPhysicalmemoryProcess1virtualmemoryProcess2virtualmemorySharingRevisited:SharedObjectsSharedobjectPhysicalmemoryProcess1virtualmemoryProcess2virtualmemoryProcess2mapsthesharedobjectNoticehowthevirtualaddressescanbedifferent82PrivateObjectAsopposetosharedobjectChangesmadetoanareamappedtoaprivateobjectarenotvisibletootherprocessesAnywritesthattheprocessmakestotheareaarenot
reflectedbacktotheobjectondisk.PrivateAreaSimilartosharedareaPrivateobject83Aprivateobjectbeginslifeinexactlythesamewayasasharedobject,withonlyonecopyoftheprivateobjectstoredinphysicalmemory.Copy-on-Write84TocreateanewprocessusingforkCopy-On-Writemakepagesofwriteableareasread-onlyflagvm_area_structfortheseareasasprivate“copy-on-write”.writesbyeitherprocesstothesepageswillcausepagefaultsfaulthandlerrecognizescopy-on-write,makesacopyofthepage,andrestoreswritepermissions.Fork()revistedPrivatecopy-on-writeobjectPhysicalmemoryProcess1virtualmemoryProcess2virtualmemoryPrivatecopy-on-writeareaSharingRevisited:PrivateCOWObjects86ForeachprocessthatmapstheprivateobjectThepagetableentriesforthecorrespondingprivateareaareflaggedasread-onlyTheareastructisflaggedasprivatecopy-on-writeSolongasneitherprocessattemptstowritetoitsrespectiveprivatearea,theycontinuetoshareasinglecopyoftheobjectinphysicalmemory.Copy-on-Write87ForeachprocessthatmapstheprivateobjectAssoonasaprocessattemptstowritetosomepageintheprivatearea,thewritetriggersaprotectionfaultThefaulthandlernoticesthattheprotectionexceptionwascausedbytheprocesstryingtowritetoapageinaprivatecopy-on-writeareaCopy-on-Write88ForeachprocessthatmapstheprivateobjectThefaulthandler
CreatesanewcopyofthepageinphysicalmemoryUpdatesthepagetableentrytopointtothenewcopyRestoreswritep
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 三農(nóng)產(chǎn)品網(wǎng)絡(luò)營銷作業(yè)指導(dǎo)書
- 2025年懷化考從業(yè)資格證貨運試題
- 小學(xué)二年級數(shù)學(xué)上冊口算題
- 2025年武威貨運上崗證模擬考試試題
- 2025年楚雄駕??荚囏涍\從業(yè)資格證模擬考試
- 電力調(diào)試合同(2篇)
- 電動車補充協(xié)議書范文(2篇)
- 2024-2025學(xué)年高中語文課時作業(yè)4毛澤東詞兩首含解析粵教版必修2
- 六年級班主任第二學(xué)期工作總結(jié)
- 小學(xué)班主任工作計劃二年級
- 2024年安徽省高校分類對口招生考試數(shù)學(xué)試卷真題
- ISO45001管理體系培訓(xùn)課件
- 動畫課件教學(xué)教學(xué)課件
- 會所股東合作協(xié)議書范文范本
- 綿陽市高中2022級(2025屆)高三第一次診斷性考試(一診)數(shù)學(xué)試卷(含答案逐題解析)
- 人教版(2024)七年級上冊英語期中復(fù)習(xí)單項選擇100題(含答案)
- 2024年胡麻油市場前景分析:全球胡麻油市場規(guī)模達到了25.55億美元
- 小學(xué)英語800詞分類(默寫用)
- 《 西門塔爾牛臉數(shù)據(jù)集的研究》范文
- 八年級上冊 第三單元 11《簡愛》公開課一等獎創(chuàng)新教學(xué)設(shè)計
- 真實世界研究指南 2018
評論
0/150
提交評論