MemoryManagementUnit VirtualMemory ThepositionandfunctionoftheMMU - - PDF document

memory management unit virtual memory
SMART_READER_LITE
LIVE PREVIEW

MemoryManagementUnit VirtualMemory ThepositionandfunctionoftheMMU - - PDF document

MemoryManagementUnit VirtualMemory ThepositionandfunctionoftheMMU 1 2 TypicalAddress VirtualAddress VirtualAddress Paging Space 15 Space 15 SpaceLayout 14 14 Kernel 13 13


slide-1
SLIDE 1

1

1

VirtualMemory

2

MemoryManagementUnit

ThepositionandfunctionoftheMMU

3

  • VirtualMemory

– Dividedintoequal sized – A isa translationbetween

  • Apageandaframe
  • Apageandnull

– Mappingsdefinedat runtime

  • Theycanchange

– Addressspacecan haveholes – Processdoesnot havetobe contiguousin physicalmemory

  • PhysicalMemory

– Dividedinto equal sized

  • 7

6 5 4 3 2 1 15 14 13 12 11 10 9 8 1 12 10 2 3 11 VirtualAddress Space PhysicalAddress Space

Paging

1 2 3 4 5 6 7

4

TypicalAddress SpaceLayout

  • Stackregionisattop,

andcangrowdown

  • Heaphasfreespaceto

growup

  • Textistypicallyread only
  • Kernelisinareserved,

protected,sharedregion

  • 0 thpagetypicallynot

used,why? 7 6 5 4 3 2 1 15 14 13 12 11 10 9 8 VirtualAddress Space Kernel Stack Shared Libraries BSS (heap) Data Text (Code)

5

  • Aprocessmay

beonlypartially resident

– AllowsOSto storeindividual pagesondisk – Savesmemory forinfrequently useddata&code

  • Whathappensif

weaccessnon resident memory?

1 15 5 3 13 VirtualAddress Space PhysicalAddress Space 7 6 5 4 3 2 1 15 14 13 12 11 10 9 8 14 10 6 4 2 Disk : logicallypresent :Not mapped,dataondisk

6

2 4 13 4 1 3 13 Proc1Address Space

Physical AddressSpace

7 6 5 3 1 15 14 13 12 11 10 9 8 4 2 7 6 5 3 1 15 14 13 12 11 10 9 8 4 2 Proc2Address Space 3 15 15 1 2 14 14 Disk

Currently running

Memory Access 2

slide-2
SLIDE 2

2

7

PageFaults

  • Referencinganinvalidpagetriggersapagefault
  • AnexceptionhandledbytheOS
  • Broadly,twostandardpagefaulttypes

– IllegalAddress(protectionerror)

  • Signalorkilltheprocess

– Pagenotresident

  • Getanemptyframe
  • Loadpagefromdisk
  • Updatepage(translation)table(enterframe#,setvalidbit,etc.)
  • Restartthefaultinginstruction

8

  • Pagetablefor

residentpartof addressspace

1 15 5 4 5 2 3 13 VirtualAddress Space Physical AddressSpace 7 6 5 3 1 15 14 13 12 11 10 9 8 4 2 3 1 7 6 Page Table 1 2 3 4 5 6 7

  • Note:Someimplementationsstoredisk

blocknumbersofnon residentpagesin thepagetable(withvalidbit)

9 10

SharedPages

  • Privatecodeanddata

– Eachprocesshasown copyofcodeanddata – Codeanddatacan appearanywherein theaddressspace

  • Sharedcode

– Singlecopyofcode sharedbetweenall processesexecutingit – Codemustnotbeself modifying – Codemustappearat sameaddressinall processes

11

2 6 13 4 3 1 3 13 Proc1Address Space

Physical AddressSpace

7 6 5 3 1 15 14 13 12 11 10 9 8 4 2 2 5 4 7 Page Table 7 6 5 3 1 15 14 13 12 11 10 9 8 4 2 1 2 7 Proc2Address Space Page Table

Two(ormore) processes runningthe sameprogram andsharing thetextsection

12

PageTableStructure

  • Pagetableis(logically)anarrayof

framenumbers

– Indexbypagenumber

  • Eachpage tableentry(PTE)alsohas
  • therbits

2 5 4 7 Page Table

slide-3
SLIDE 3

3

13

PTEbits

  • Present/Absentbit

– Alsocalleditindicatesavalidmappingforthepage

  • Modifiedbit

– Alsocalleditindicatesthepagemayhavebeen modifiedinmemory

  • Referencebit

– Indicatesthepagehasbeenaccessed

  • Protectionbits

– Readpermission,Writepermission,Executepermission – Orcombinationsoftheabove

  • Cachingbit

– Usetoindicateprocessorshouldbypassthecachewhen accessingmemory

  • Example:toaccessdeviceregistersormemory

14

AddressTranslation

  • Every(virtual)memoryaddressissuedby

theCPUmustbetranslatedtophysical memory

– Every andevery instruction – Everyinstructionfetch

  • NeedTranslationHardware
  • Inpagingsystem,translationinvolves

replacepagenumberwithaframenumber

Pagetables(recap) virtualmemory

virtualandphysicalmemchoppedupinpages

  • !
  • 18

PageTables

  • Assumewehave

– 32 bitvirtualaddress(4Gbyteaddressspace) – 4KBytepagesize – Howmanypagetableentriesdoweneedforone process?

slide-4
SLIDE 4

4

19

PageTables

  • Assumewehave

– 64 bitvirtualaddress(humungous addressspace) – 4KBytepagesize – Howmanypagetableentriesdoweneedforone process?

  • Problem:

– Pagetableisverylarge – Accesshastobefast,lookupforeverymemory reference – Wheredowestorethepagetable?

  • Registers?
  • Mainmemory?

20

PageTables

  • Pagetablesareimplementedasdatastructuresinmain

memory

  • Mostprocessesdonotusethefull4GBaddressspace

– e.g.,0.1– 1MBtext,0.1– 10MBdata,0.1MBstack

  • Weneedacompactrepresentationthatdoesnotwaste

space

– Butisstillveryfasttosearch

  • Threebasicschemes

– Usedatastructuresthatadapttosparsity – Usedatastructureswhichonlyrepresentresidentpages – UseVMtechniquesforpagetables(detailslefttoextendedOS)

21

Two levelPage Table

  • 2nd –level

pagetables representing unmapped pagesarenot allocated

– Nullinthe top level pagetable

22

Two levelTranslation Alternative:InvertedPageTable

PID VPN next PID VPN

  • ffset

Index 1 2 3 4 5 6 F IPT:entryforeach frame

HashAnchorTable (HAT)

Hash ctrl

Alternative:InvertedPageTable

PID VPN next PID VPN

  • ffset

Index 1 2 F 0x40C 0x40D F F

HashAnchorTable (HAT)

Hash ctrl 0x5 0x123 1 0x1A 0x40C 0x5 0x0 2 0x40C 0x123 ppn

  • ffset
slide-5
SLIDE 5

5

26

InvertedPageTable(IPT)

  • “Invertedpagetable”isanarrayofpage

numberssorted(indexed)byframenumber(it’s aframetable).

  • Algorithm

– Computehashofpagenumber – Extractindexfromhashtable – Usethistoindexintoinvertedpagetable – MatchthePIDandpagenumberintheIPTentry – Ifmatch,usetheindexvalueasframe#for translation – Ifnomatch,getnextcandidateIPTentryfromchain field – IfNULLchainentry⇒ pagefault

27

PropertiesofIPTs

  • IPTgrowswithsizeofRAM,NOTvirtualaddressspace
  • Frametableisneededanyway(forpagereplacement,

morelater)

  • Needaseparatedatastructurefornon residentpages
  • Savesavastamountofspace(especiallyon64 bit

systems)

  • UsedinsomeIBMandHPworkstations

Given processes

  • howmanypagetableswillthesystem

havefor

– ‘normal’pagetables – invertedpagetables?

AnotherlookatsharingF

30

2 6 13 4 3 1 3 13 Proc1Address Space

Physical AddressSpace

7 6 5 3 1 15 14 13 12 11 10 9 8 4 2 2 5 4 7 Page Table 7 6 5 3 1 15 14 13 12 11 10 9 8 4 2 1 2 7 Proc2Address Space Page Table

Two(ormore) processes runningthe sameprogram andsharing thetextsection

31

  • Problem:

– Eachvirtualmemoryreferencecancausetwo physicalmemoryaccesses

  • Onetofetchthepagetableentry
  • Onetofetch/storethedata

⇒Intolerableperformanceimpact!!

  • Solution:

– High speedcacheforpagetableentries(PTEs)

  • Calleda(TLB)
  • Containsrecentlyusedpagetableentries
  • Associative,high speedmemory,similartocachememory
  • MaybeunderOScontrol(unlikememorycache)

VMImplementationIssue

slide-6
SLIDE 6

6

32

TLBoperation

On CPU hardware device!!! Data structure inmain memory

33

TranslationLookasideBuffer

  • Givenavirtualaddress,processorexaminesthe

TLB

  • IfmatchingPTEfound(),theaddressis

translated

  • Otherwise(),thepagenumberisused

toindextheprocess’spagetable

– IfPTcontainsavalidentry,reloadTLBandrestart – Otherwise,(pagefault)checkifpageisondisk

  • Ifondisk,swapitin
  • Otherwise,allocateanewpageorraiseanexception

34

TLBproperties

  • Pagetableis(logically)anarrayofframe

numbers

  • TLBholdsa(recentlyused)subsetofPTentries

– EachTLBentrymustbeidentified(tagged)withthe page#ittranslates – Accessisbyassociativelookup:

  • AllTLBentries’tagsareconcurrentlycomparedtothepage#
  • TLBisassociative(orcontent addressable)memory

35

TLBproperties

  • TLBmayormaynotbeunderdirectOScontrol

– Hardware loadedTLB

  • Onmiss,hardwareperformsPTlookupandreloadsTLB
  • Example:Pentium

– Software loadedTLB

  • Onmiss,hardwaregeneratesaTLBmissexception,and

exceptionhandlerreloadsTLB

  • Example:MIPS
  • TLBsize:typically64 128entries
  • CanhaveseparateTLBsforinstructionfetch

anddataaccess

  • TLBscanalsobeusedwithinvertedpagetables

(andothers)

36

TLBandcontextswitching

  • TLBisasharedpieceofhardware
  • Pagetablesareper process(addressspace)
  • TLBentriesare

– OncontextswitchneedtotheTLB(invalidate allentries)

  • highcontext switchingoverhead(Intelx86)

– tagentrieswith(ASID)

  • calleda
  • used(insomeform)onallmodernarchitectures
  • TLBentry:ASID,page#,frame#,validandwrite protect

bits

37

TLBeffect

  • WithoutTLB

– Averagenumberofphysicalmemory referencespervirtualreference

=2

  • WithTLB(assume99%hitratio)

– Averagenumberofphysicalmemory referencespervirtualreference

=.99*1+0.01*2 =1.01

slide-7
SLIDE 7

7

38

Recap SimplifiedComponentsof VMSystem

1 2 3 CPU FramePool

1 3 2 TLB

VirtualAddressSpaces (3processes) PageTablesfor3 processes FrameTable PhysicalMemory 39

MIPSR3000TLB

  • N=Notcacheable
  • D=Dirty=Writeprotect
  • G=Global(ignoreASID

inlookup)

  • V=validbit
  • 64TLBentries
  • Accessedviasoftwarethrough

Cooprocessor0registers

– EntryHiandEntryLo

40

R3000Address SpaceLayout

  • kuseg:

– 2gigabytes – TLBtranslated(mapped) – Cacheable(dependingon‘N’bit) – user modeandkernelmode accessible – Pagesizeis4K

kseg0 kuseg 0x00000000 0x80000000 kseg1 kseg2 0xA0000000 0xC0000000 0xFFFFFFFF

41

R3000Address SpaceLayout

– Switchingprocesses switchesthetranslation (pagetable)forkuseg kseg0 Proc3 kuseg 0x00000000 0x80000000 kseg1 kseg2 0xA0000000 0xC0000000 0xFFFFFFFF Proc2 kuseg Proc1 kuseg

42

R3000Address SpaceLayout

  • kseg0:

– 512megabytes – Fixedtranslationwindowto physicalmemory

  • 0x80000000 0x9fffffffvirtual=

0x00000000 0x1fffffffphysical

  • TLBnotused

– Cacheable – Onlykernel modeaccessible – Usuallywherethekernelcodeis placed

kseg0 kuseg

  • kseg1

kseg2

  • PhysicalMemory

43

R3000Address SpaceLayout

  • kseg1:

– 512megabytes – Fixedtranslationwindowto physicalmemory

  • 0xa0000000 0xbfffffffvirtual=

0x00000000 0x1fffffffphysical

  • TLBnotused

– "#$cacheable – Onlykernel modeaccessible – Wheredevicesareaccessed(and bootROM)

kseg0 kuseg

  • kseg1

kseg2

  • PhysicalMemory
slide-8
SLIDE 8

8

44

R3000Address SpaceLayout

  • kseg2:

– 1024megabytes – TLBtranslated(mapped) – Cacheable

  • Dependingonthe‘N’ bit

– Onlykernel modeaccessible – Canbeusedtostorethevirtual lineararraypagetable

kseg0 kuseg

  • kseg1

kseg2