memmgmt.docx
- 文档编号:30364957
- 上传时间:2023-08-13
- 格式:DOCX
- 页数:51
- 大小:60.60KB
memmgmt.docx
《memmgmt.docx》由会员分享,可在线阅读,更多相关《memmgmt.docx(51页珍藏版)》请在冰豆网上搜索。
memmgmt
MemoryManagement:
WhatEveryDriverWriterNeedstoKnow
February28,2005
Abstract
Thispaperprovidesinformationaboutallocatingandusingmemoryinkernel-modedriversfortheMicrosoft®Windows®familyofoperatingsystems.Itdescribesthetypesofmemorythatareavailablefordriveruse,theappropriatetechniquesforallocatingandusingeachtype,andthebestwaystotestformemory-relatedproblems.
Thisinformationappliesforthefollowingoperatingsystems:
MicrosoftWindowsVista™
MicrosoftWindowsServer™2003
MicrosoftWindowsXP
MicrosoftWindows2000
ThecurrentversionofthispaperismaintainedontheWebat:
Referencesandresourcesdiscussedherearelistedattheendofthispaper.
Contents
Introduction4
VirtualandPhysicalMemory4
VirtualAddressSpace5
VirtualAddressSpaceon32-bitHardware5
VirtualAddressSpaceon64-bitHardware7
Types,Constants,andMacrosUsedinAddressing7
MappingVirtualMemorytoPhysicalMemory8
PhysicalAddressSpace9
Processor-RelativePhysicalAddresses12
Device-Bus-RelativePhysicalAddresses12
MemoryDescriptorLists13
PoolMemory15
IRQLConsiderations15
LookasideLists16
Caching16
Alignment16
NaturalAlignment16
Cache-LineAlignment19
KernelStack19
MemoryAllocationTechniques20
AllocatingMemoryforLong-TermStorage21
CreatingandUsingLookasideLists22
AllocatingContiguousMemory24
AccessingDeviceRegisters25
AccessingMemoryforI/OBuffers28
MappingDeviceMemoryandRegistersintoUserSpace30
SharedMemoryandSynchronization31
TestingandTroubleshooting32
ToolsforDiscoveringMemory-RelatedErrors32
BestPracticesforProgrammingandTesting33
SummaryofGuidelines34
Resources34
Disclaimer
Thisisapreliminarydocumentandmaybechangedsubstantiallypriortofinalcommercialreleaseofthesoftwaredescribedherein.
TheinformationcontainedinthisdocumentrepresentsthecurrentviewofMicrosoftCorporationontheissuesdiscussedasofthedateofpublication.BecauseMicrosoftmustrespondtochangingmarketconditions,itshouldnotbeinterpretedtobeacommitmentonthepartofMicrosoft,andMicrosoftcannotguaranteetheaccuracyofanyinformationpresentedafterthedateofpublication.
ThisWhitePaperisforinformationalpurposesonly.MICROSOFTMAKESNOWARRANTIES,EXPRESS,IMPLIEDORSTATUTORY,ASTOTHEINFORMATIONINTHISDOCUMENT.
Complyingwithallapplicablecopyrightlawsistheresponsibilityoftheuser.Withoutlimitingtherightsundercopyright,nopartofthisdocumentmaybereproduced,storedinorintroducedintoaretrievalsystem,ortransmittedinanyformorbyanymeans(electronic,mechanical,photocopying,recording,orotherwise),orforanypurpose,withouttheexpresswrittenpermissionofMicrosoftCorporation.
Microsoftmayhavepatents,patentapplications,trademarks,copyrights,orotherintellectualpropertyrightscoveringsubjectmatterinthisdocument.ExceptasexpresslyprovidedinanywrittenlicenseagreementfromMicrosoft,thefurnishingofthisdocumentdoesnotgiveyouanylicensetothesepatents,trademarks,copyrights,orotherintellectualproperty.
Unlessotherwisenoted,theexamplecompanies,organizations,products,domainnames,e-mailaddresses,logos,people,placesandeventsdepictedhereinarefictitious,andnoassociationwithanyrealcompany,organization,product,domainname,emailaddress,logo,person,placeoreventisintendedorshouldbeinferred.
©2005MicrosoftCorporation.Allrightsreserved.
Microsoft,Windows,andWindowsServerareeitherregisteredtrademarksortrademarksofMicrosoftCorporationintheUnitedStatesand/orothercountries.
Thenamesofactualcompaniesandproductsmentionedhereinmaybethetrademarksoftheirrespectiveowners.
Introduction
Kernel-modedriversallocatememoryforvariouspurposes,suchasstoringinternaldataorusingasI/Obuffers.Tohelpdriverwritersusememorycorrectly,thispaperexplainsthefollowing:
∙Fundamentalinformationaboutphysicalandvirtualmemoryandaddressspaces.
∙Typesofmemorythatareavailabletodriversandwhentouseeachtype.
∙Techniquesforallocatingmemorytosatisfyadriver’sdiverserequirements.
∙TechniquesforaccessingmemoryallocatedbyothercomponentsforI/Ooperations.
∙Techniquesforsharingmemorywithotherkernel-modeanduser-modecomponents.
∙Techniquesfortestingandtroubleshootingmemoryallocationandusageproblems.
Note:
Thispaperdoesnotcovermemoryallocationfordirectmemoryaccess(DMA).YoucanfinddetailedinformationaboutallaspectsofDMAin“DMASupportinWindowsDrivers,”whichislistedintheResourcessectionattheendofthispaper.
1VirtualandPhysicalMemory
TheamountofvirtualandphysicalmemorythatissupportedonanycomputerthatrunstheMicrosoft®Windows®operatingsystemisdeterminedbythehardwareconfigurationandtheeditionofWindowsinuse.On32-bithardware,thevirtualaddressspaceis4GBandthemaximumamountofphysicalmemoryrangesfrom4to128GB.On64-bithardware,thevirtualaddressspaceis16terabytesandthemaximumamountofmemoryrangesfrom64GBto1terabyte.
Table1liststheamountofvirtualmemoryandthemaximumamountofphysicalmemorythateacheditionofWindowssupports.
Table1.VirtualandPhysicalMemorySupportinWindows
Operatingsystemversion
Edition
Virtualmemory
Maximumphysicalmemory
MicrosoftWindowsServer™2003SP1
Standard
4GB
4GB
Web
4GB
2GB
Enterprise
4GB
64GB,ifhardwaresupportsPhysicalAddressExtension(PAE)
Enterprise(64-bit)
16terabytes
1terabyte
Datacenter
4GB
128GB,ifhardwaresupportsPAE
Datacenter(64-bit)
16terabytes
1terabyte
WindowsServer2003
Standard
4GB
4GB
Web
4GB
2GB
Enterprise
4GB
32GB,ifhardwaresupportsPAE
Enterprise(64-bit)
16terabytes
64GB
Datacenter
4GB
128GB,ifhardwaresupportsPAE
Datacenter(64-bit)
16terabytes
512GB
WindowsXP
Home
4GB
4GB
Professional
4GB
4GB
64-bitEditionVersion2003
16terabytes
128GB
Windows2000
Professional
4GB
4GB
Server
4GB
4GB
AdvancedServer
4GB
8GB
DatacenterServer
4GB
32GB,ifhardwaresupportsPAE
2VirtualAddressSpace
Avirtualaddressidentifiesalocationinvirtualmemory.Thevirtualaddressspaceisdividedintotworanges:
userspaceandsystemspace.
UserspaceistheportionofthevirtualaddressspaceintowhichWindowsmapsuser-modeprocesses,per-processdata,anduser-modedynamiclinklibraries(DLLs).Eachprocesshasitsowncontext,thatis,thecodeanddatathattheprocessuses.Whiletheprocessisrunning,aportionofthecontext,calledtheworkingset,isresidentinmemory.
Systemspace,alsocalledkernelspace,istheportionoftheaddressspaceinwhichtheoperatingsystemandkernel-modedriversareresident.Itisaccessibleonlytokernel-modecode.
Thedistinctionbetweenuserspaceandsystemspaceisimportantinmaintainingsystemsecurity.Auser-modethreadcanaccessdataonlyinthecontextofitsownprocess.Itcannotaccessdatawithinotherprocesscontextsorwithinthesystemaddressspace.Thisrestrictionpreventsuser-modethreadsfromreadingandwritingdatathatbelongstootherprocessesortotheoperatingsystemanddrivers,whichcouldresultinsecurityvulnerabilitiesandpossiblyevenasystemcrash.
Kernel-modedriversaretrustedbytheoperatingsystemandconsequentlycanaccessbothuserspaceandsystemspace.Whenadriverroutineiscalledinthecontextofauserthread,allofthethread’sdataremainsintheuser-modeaddressspace.Thedrivercanaccesstheuser-spacedataforthethread—ifittakesthenecessarysecurityprecautions—inadditiontoaccessingsystem-spacedata.Theuser-modethread,however,doesnothaveaccesstothesystem-spacedataofthedriver.
Thesizesofthesystemanduseraddressspacesdependonwhetherthehardwareis32-bitor64-bitandontheoptionsappliedtoBoot.ini.
2.1VirtualAddressSpaceon32-bitHardware
On32-bitmachines,thelower2GBofthevirtualaddressspaceisuserspaceandtheupper2GBisreservedassystemspacebydefault.Ifuser-modeapplicationsrequireadditionalvirtualaddressspace,anadministratorcanreserve3GBforuserspace,thusleavingonly1 GBofsystemspace,byapplyingthe/3GBswitchtoBoot.iniandthenrestartingthemachine.Thisswitchisalsousefulduringtestingtoseehowadriverperformswithlimitedsystemaddressspace.TheswitchissupportedonWindowsServer2003(alleditions),WindowsXP(allversions),Windows2000AdvancedServer,andWindows2000DatacenterServer.
Figure1showshowthevirtualaddressspaceisorganizedon32-bitsystems.(Thefigureisnotdrawntoscale.)
Figure1.Layoutofvirtualaddressspaceon32-bitsystems
Asthefigureshows,theuseraddressspaceoccupiesthelowerrangeofthevirtualaddressspaceandthesystemaddressspaceoccupiestheupperrange.Bydefault,bothuserandsystemspaceare2GB.WhenWindowsisstartedwiththe/3GBswitch,thesizeoftheuserspaceincreasesto3GB.Theremaining1GBofvirtualaddressspacecontainsthesystemaddressspace.Theprocesspagetablesandhyperspaceremainatthesameaddresses,butothercomponentsdonot.Whenthesystemaddressspaceislimitedto1GB,themaximumsizesofthepagedandnonpagedpoolsarereducedandlessspaceisavailableforsystempagetableentries(PTEs)andthefilesystemcacheaswell.
Thefilesystemcache,whichisimplementedinsoftware,isnotthesameastheprocessorcache,whichisimplementedinhardware.Keepinmind,however,thatthis
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- memmgmt