KernelProgramming.pdf

(1847 KB) Pobierz
Kernel Programming Guide
KernelProgrammingGuide
Drivers,Kernel,&Hardware
2011-03-08
711392208.002.png
AppleInc.
©2002,2011AppleInc.
Allrightsreserved.
PowerPCandandthePowerPClogoare
trademarksofInternationalBusinessMachines
Corporation,usedunderlicensetherefrom.
SPECisaregisteredtrademarkoftheStandard
PerformanceEvaluationCorporation(SPEC).
UNIXisaregisteredtrademarkofTheOpen
Group
EventhoughApplehasreviewedthisdocument,
APPLEMAKESNOWARRANTYORREPRESENTATION,
EITHEREXPRESSORIMPLIED,WITHRESPECTTO
THISDOCUMENT,ITSQUALITY,ACCURACY,
MERCHANTABILITY,ORFITNESSFORAPARTICULAR
PURPOSE.ASARESULT,THISDOCUMENTIS
PROVIDED“ASIS,”ANDYOU,THEREADER,ARE
ASSUMINGTHEENTIRERISKASTOITSQUALITY
ANDACCURACY.
INNOEVENTWILLAPPLEBELIABLEFORDIRECT,
INDIRECT,SPECIAL,INCIDENTAL,OR
CONSEQUENTIALDAMAGESRESULTINGFROMANY
DEFECTORINACCURACYINTHISDOCUMENT,even
ifadvisedofthepossibilityofsuchdamages.
THEWARRANTYANDREMEDIESSETFORTHABOVE
AREEXCLUSIVEANDINLIEUOFALLOTHERS,ORAL
ORWRITTEN,EXPRESSORIMPLIED.NoApple
dealer,agent,oremployeeisauthorizedtomake
anymodification,extension,oradditiontothis
warranty.
Somestatesdonotallowtheexclusionorlimitation
ofimpliedwarrantiesorliabilityforincidentalor
consequentialdamages,sotheabovelimitationor
exclusionmaynotapplytoyou.Thiswarrantygives
youspecificlegalrights,andyoumayalsohave
otherrightswhichvaryfromstatetostate.
Nopartofthispublicationmaybereproduced,
storedinaretrievalsystem,ortransmitted,in
anyformorbyanymeans,mechanical,
electronic,photocopying,recording,or
otherwise,withoutpriorwrittenpermissionof
AppleInc.,withthefollowingexceptions:Any
personisherebyauthorizedtostore
documentationonasinglecomputerfor
personaluseonlyandtoprintcopiesof
documentationforpersonaluseprovidedthat
thedocumentationcontainsApple’scopyright
notice.
TheApplelogoisatrademarkofAppleInc.
Nolicenses,expressorimplied,aregranted
withrespecttoanyofthetechnologydescribed
inthisdocument.Appleretainsallintellectual
propertyrightsassociatedwiththetechnology
describedinthisdocument.Thisdocumentis
intendedtoassistapplicationdevelopersto
developapplicationsonlyforApple-labeled
computers.
AppleInc.
1InfiniteLoop
Cupertino,CA95014
408-996-1010
.MacisaregisteredservicemarkofAppleInc.
Apple,theApplelogo,AppleTalk,Carbon,
Cocoa,eMac,Finder,FireWire,Keychain,Logic,
Mac,MacOS,Macintosh,Objective-C,Pages,
Panther,PowerMac,Quartz,QuickTime,Spaces,
andXcodearetrademarksofAppleInc.,
registeredintheUnitedStatesandother
countries.
NeXTandOPENSTEParetrademarksofNeXT
Software,Inc.,registeredintheUnitedStates
andothercountries.
DECisatrademarkofDigitalEquipment
Corporation.
IntelandIntelCoreareregisteredtrademarks
ofIntelCorportationoritssubsidiariesinthe
UnitedStatesandothercountries.
JavaisaregisteredtrademarkofOracleand/or
itsaffiliates.
OpenGLisaregisteredtrademarkofSilicon
Graphics,Inc.
711392208.003.png
Contents
Chapter1 AboutThisDocument 11
WhoShouldReadThisDocument 11
RoadMap 11
OtherApplePublications 12
MachAPIReference 13
InformationontheWeb 13
Chapter2 KeepOut 15
WhyYouShouldAvoidProgrammingintheKernel 15
Chapter3 KernelArchitectureOverview 17
Darwin 18
Architecture 19
Mach 20
BSD 20
I/OKit 21
KernelExtensions 22
Chapter4 TheEarlyBootProcess 23
BootROM 23
TheBootLoader 23
Rooting 24
Chapter5 SecurityConsiderations 25
SecurityImplicationsofPaging 26
BufferOverflowsandInvalidInput 26
UserCredentials 27
BasicUserCredentials 28
AccessControlLists 29
RemoteAuthentication 29
One-TimePads 29
Time-basedauthentication 30
TemporaryFiles 31
/dev/memand/dev/kmem 31
Key-basedAuthenticationandEncryption 31
PublicKeyWeaknesses 32
UsingPublicKeysforMessageExchange 34
2011-03-08 | © 2002, 2011 Apple Inc. All Rights Reserved.
3
711392208.004.png
CONTENTS
UsingPublicKeysforIdentityVerification 34
UsingPublicKeysforDataIntegrityChecking 34
EncryptionSummary 35
ConsoleDebugging 35
CodePassing 35
Chapter6 PerformanceConsiderations 37
InterruptLatency 37
LockingBottlenecks 38
WorkingWithHighlyContendedLocks 38
ReducingContentionbyDecreasingGranularity 39
CodeProfiling 39
UsingCountersforCodeProfiling 39
LockProfiling 40
Chapter7 KernelProgrammingStyle 43
C++NamingConventions 43
BasicConventions 43
AdditionalGuidelines 44
StandardCNamingConventions 44
CommonlyUsedFunctions 46
PerformanceandStabilityTips 48
PerformanceandStabilityTips 48
StabilityTips 49
StyleSummary 50
Chapter8 MachOverview 51
MachKernelAbstractions 51
TasksandThreads 52
Ports,PortRights,PortSets,andPortNamespaces 53
MemoryManagement 54
InterprocessCommunication(IPC) 55
IPCTransactionsandEventDispatching 56
MessageQueues 56
Semaphores 57
Notifications 57
Locks 57
RemoteProcedureCall(RPC)Objects 57
TimeManagement 57
Chapter9 MemoryandVirtualMemory 59
MacOSXVMOverview 59
4
2011-03-08 | © 2002, 2011 Apple Inc. All Rights Reserved.
711392208.005.png
CONTENTS
MemoryMapsExplained 60
NamedEntries 61
UniversalPageLists(UPLs) 62
UsingMachMemoryMaps 63
OtherVMandVM-RelatedSubsystems 64
Pagers 65
WorkingSetDetectionSubsystem 65
VMSharedMemoryServerSubsystem 65
AddressSpaces 66
BackgroundInfoonPCIAddressTranslation 66
IOMemoryDescriptorChanges 67
VMSystemandpmapChanges: 68
KernelDependencyChanges 68
Summary 68
AllocatingMemoryintheKernel 68
AllocatingMemoryUsingMachRoutines 68
AllocatingMemoryFromtheI/OKit 69
Chapter10 MachSchedulingandThreadInterfaces 71
OverviewofScheduling 71
WhyDidMyThreadPriorityChange? 72
UsingMachSchedulingFromUserApplications 72
UsingthepthreadsAPItoInfluenceScheduling 73
UsingtheMachThreadAPItoInfluenceScheduling 73
UsingtheMachTaskAPItoInfluenceScheduling 75
KernelThreadAPIs 77
CreatingandDestroyingKernelThreads 77
SPLandFriends 78
WaitQueuesandWaitPrimitives 78
Chapter11 BootstrapContexts 81
HowContextsAffectUsers 82
HowContextsAffectDevelopers 82
Chapter12 I/OKitOverview 85
RedesigningtheI/OModel 85
I/OKitArchitecture 86
Families 87
Drivers 87
Nubs 87
ConnectionExample 88
ForMoreInformation 91
2011-03-08 | © 2002, 2011 Apple Inc. All Rights Reserved.
5
711392208.001.png
Zgłoś jeśli naruszono regulamin