2 * Copyright (c) 2008-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #ifndef _LIBKERN_OSKEXT_H
30 #define _LIBKERN_OSKEXT_H
33 #include <kern/thread_call.h>
34 #include <libkern/OSKextLibPrivate.h>
35 #include <libkern/kernel_mach_header.h>
36 #include <libkern/kxld.h>
37 #include <mach/kmod.h>
39 #ifdef XNU_KERNEL_PRIVATE
40 #include <kern/thread_call.h>
41 #endif /* XNU_KERNEL_PRIVATE */
44 #include <libkern/OSKextLib.h>
45 #include <libkern/OSKextLibPrivate.h>
46 #include <libkern/c++/OSObject.h>
47 #include <libkern/c++/OSContainers.h>
48 #include <IOKit/IOLocks.h>
50 /*********************************************************************
51 * C functions used for callbacks.
52 *********************************************************************/
53 #ifdef XNU_KERNEL_PRIVATE
55 void osdata_kmem_free(void * ptr
, unsigned int length
);
56 void osdata_phys_free(void * ptr
, unsigned int length
);
57 void osdata_vm_deallocate(void * ptr
, unsigned int length
);
58 void osdata_kext_free(void * ptr
, unsigned int length
);
59 void kxld_log_callback(
60 KXLDLogSubsystem subsystem
,
66 #endif /* XNU_KERNEL_PRIVATE */
68 /*********************************************************************
69 * C Function Prototypes for Friend Declarations.
70 *********************************************************************/
77 OSKextLogSpec msgLogSpec
,
78 const char * format
, ...)
79 __attribute__((format(printf
, 3, 4)));
83 OSKextLogSpec msgLogSpec
,
87 #ifdef XNU_KERNEL_PRIVATE
88 void OSKextRemoveKextBootstrap(void);
90 kern_return_t
OSRuntimeInitializeCPP(
91 kmod_info_t
* kmodInfo
,
93 kern_return_t
OSRuntimeFinalizeCPP(
94 kmod_info_t
* kmodInfo
,
97 kern_return_t
is_io_catalog_send_data(
98 mach_port_t masterPort
,
101 mach_msg_type_number_t inDataCount
,
102 kern_return_t
* result
);
104 void kmod_dump_log(vm_offset_t
*, unsigned int, boolean_t
);
105 void *OSKextKextForAddress(const void *addr
);
107 #endif /* XNU_KERNEL_PRIVATE */
110 /********************************************************************/
116 struct list_head
*prev
;
117 struct list_head
*next
;
120 struct OSKextGrabPgoStruct
{
126 struct list_head list_head
;
130 #define container_of(ptr,type,member) ((type*)(((uintptr_t)ptr) - offsetof(type, member)))
132 /********************************************************************/
134 #if XNU_KERNEL_PRIVATE
138 vm_allocation_site_t site
;
143 struct OSKextActiveAccount
146 uintptr_t address_end
;
147 OSKextAccount
* account
;
149 typedef struct OSKextActiveAccount OSKextActiveAccount
;
151 #endif /* XNU_KERNEL_PRIVATE */
156 /********************************************************************/
157 class OSKext
: public OSObject
159 OSDeclareDefaultStructors(OSKext
)
162 /**************************************/
163 #pragma mark Friend Declarations
164 /**************************************/
166 friend class IOCatalogue
;
167 friend class KLDBootstrap
;
168 friend class OSMetaClass
;
170 friend int OSKextGrabPgoData(uuid_t uuid
,
177 #ifdef XNU_KERNEL_PRIVATE
178 friend void OSKextVLog(
180 OSKextLogSpec msgLogSpec
,
184 friend void OSKextRemoveKextBootstrap(void);
185 friend OSReturn
OSKextUnloadKextWithLoadTag(uint32_t);
187 friend kern_return_t
kext_request(
188 host_priv_t hostPriv
,
189 /* in only */ uint32_t clientLogSpec
,
190 /* in only */ vm_offset_t requestIn
,
191 /* in only */ mach_msg_type_number_t requestLengthIn
,
192 /* out only */ vm_offset_t
* responseOut
,
193 /* out only */ mach_msg_type_number_t
* responseLengthOut
,
194 /* out only */ vm_offset_t
* logDataOut
,
195 /* out only */ mach_msg_type_number_t
* logDataLengthOut
,
196 /* out only */ kern_return_t
* op_result
);
198 friend kxld_addr_t
kern_allocate(
200 KXLDAllocateFlags
* flags
,
203 friend void kxld_log_shim(
204 KXLDLogSubsystem subsystem
,
210 friend void _OSKextConsiderUnloads(
211 __unused thread_call_param_t p0
,
212 __unused thread_call_param_t p1
);
214 friend kern_return_t
OSRuntimeInitializeCPP(
215 kmod_info_t
* kmodInfo
,
217 friend kern_return_t
OSRuntimeFinalizeCPP(
218 kmod_info_t
* kmodInfo
,
221 friend kern_return_t
is_io_catalog_send_data(
222 mach_port_t masterPort
,
225 mach_msg_type_number_t inDataCount
,
226 kern_return_t
* result
);
228 friend void kmod_panic_dump(vm_offset_t
*, unsigned int);
229 friend void kmod_dump_log(vm_offset_t
*, unsigned int, boolean_t
);
230 friend void kext_dump_panic_lists(int (*printf_func
)(const char * fmt
, ...));
231 friend void *OSKextKextForAddress(const void *addr
);
233 #endif /* XNU_KERNEL_PRIVATE */
237 /*************************
239 *************************/
240 OSDictionary
* infoDict
;
242 const OSSymbol
* bundleID
;
243 OSString
* path
; // not necessarily correct :-/
244 OSString
* executableRelPath
; // relative to bundle
246 OSKextVersion version
; // parsed
247 OSKextVersion compatibleVersion
; // parsed
249 /* These fields are required for tracking loaded kexts and
250 * will always have values for a loaded kext.
252 OSKextLoadTag loadTag
; // 'id' from old kmod_info;
253 // kOSKextInvalidLoadTag invalid
254 kmod_info_t
* kmod_info
; // address into linkedExec./alloced for interface
256 OSArray
* dependencies
; // kernel resource does not have any;
257 // links directly to kernel
259 /* Only real kexts have these; interface kexts do not.
261 OSData
* linkedExecutable
;
262 OSSet
* metaClasses
; // for C++/OSMetaClass kexts
264 /* Only interface kexts have these; non-interface kexts can get at them
265 * in the linked Executable.
267 OSData
* interfaceUUID
;
270 unsigned int loggingEnabled
:1;
272 unsigned int hasAllDependencies
:1;
273 unsigned int hasBleedthrough
:1;
275 unsigned int interface
:1;
276 unsigned int kernelComponent
:1;
277 unsigned int prelinked
:1;
278 unsigned int loaded
:1;
279 unsigned int dtraceInitialized
:1;
280 unsigned int starting
:1;
281 unsigned int started
:1;
282 unsigned int stopping
:1;
283 unsigned int unloading
:1;
285 unsigned int autounloadEnabled
:1;
286 unsigned int delayAutounload
:1; // for development
288 unsigned int CPPInitialized
:1;
289 unsigned int jettisonLinkeditSeg
:1;
292 struct list_head pendingPgoHead
;
293 uuid_t instance_uuid
;
294 OSKextAccount
* account
;
297 /**************************************/
298 #pragma mark Private Functions
299 /**************************************/
302 #ifdef XNU_KERNEL_PRIVATE
303 /* Startup/shutdown phases.
306 static void initialize(void);
307 static OSDictionary
* copyKexts(void);
308 static OSReturn
removeKextBootstrap(void);
309 static void willShutdown(void); // called by IOPMrootDomain on shutdown
310 #endif /* XNU_KERNEL_PRIVATE */
313 /* Called by power management at sleep/shutdown.
315 static bool setLoadEnabled(bool flag
);
316 static bool setUnloadEnabled(bool flag
);
317 static bool setAutounloadsEnabled(bool flag
);
318 static bool setKernelRequestsEnabled(bool flag
);
320 // all getters subject to race condition, caller beware
321 static bool getLoadEnabled(void);
322 static bool getUnloadEnabled(void);
323 static bool getAutounloadEnabled(void);
324 static bool getKernelRequestsEnabled(void);
326 /* Instance life cycle.
328 static OSKext
* withBooterData(
329 OSString
* deviceTreeName
,
330 OSData
* booterData
);
331 virtual bool initWithBooterData(
332 OSString
* deviceTreeName
,
333 OSData
* booterData
);
335 static OSKext
* withPrelinkedInfoDict(
336 OSDictionary
* infoDict
,
337 bool doCoalesedSlides
);
338 virtual bool initWithPrelinkedInfoDict(
339 OSDictionary
* infoDict
,
340 bool doCoalesedSlides
);
342 static void setAllVMAttributes(void);
344 static OSKext
* withMkext2Info(
345 OSDictionary
* anInfoDict
,
347 virtual bool initWithMkext2Info(
348 OSDictionary
* anInfoDict
,
351 virtual bool setInfoDictionaryAndPath(
352 OSDictionary
* aDictionary
,
354 virtual bool setExecutable(
355 OSData
* anExecutable
,
356 OSData
* externalData
= NULL
,
357 bool externalDataIsMkext
= false);
358 virtual bool registerIdentifier(void);
360 virtual void free(void) APPLE_KEXT_OVERRIDE
;
362 static OSReturn
removeKext(
364 bool terminateServicesAndRemovePersonalitiesFlag
= false);
366 virtual bool isInExcludeList(void);
370 static OSReturn
readMkextArchive(
372 uint32_t * checksumPtr
= NULL
);
373 static OSReturn
readMkext2Archive(
375 OSDictionary
** mkextPlistOut
,
376 uint32_t * checksumPtr
= NULL
);
377 virtual OSData
* createMkext2FileEntry(
379 OSNumber
* offsetNum
,
380 const char * entryName
);
381 virtual OSData
* extractMkext2FileData(
384 uint32_t compressedSize
,
389 virtual bool resolveDependencies(
390 OSArray
* loopStack
= NULL
); // priv/prot
391 virtual bool addBleedthroughDependencies(OSArray
* anArray
);
392 virtual bool flushDependencies(bool forceFlag
= false); // priv/prot
393 virtual uint32_t getNumDependencies(void);
394 virtual OSArray
* getDependencies(void);
396 /* User-space requests (load/generic).
398 static OSReturn
loadFromMkext(
399 OSKextLogSpec clientLogSpec
,
401 uint32_t mkextBufferLength
,
403 uint32_t * logInfoLengthOut
);
404 static OSReturn
handleRequest(
405 host_priv_t hostPriv
,
406 OSKextLogSpec clientLogSpec
,
407 char * requestBuffer
,
408 uint32_t requestLength
,
410 uint32_t * responseLengthOut
,
412 uint32_t * logInfoLengthOut
);
413 static OSReturn
serializeLogInfo(
414 OSArray
* logInfoArray
,
416 uint32_t * logInfoLengthOut
);
420 virtual OSReturn
load(
421 OSKextExcludeLevel startOpt
= kOSKextExcludeNone
,
422 OSKextExcludeLevel startMatchingOpt
= kOSKextExcludeAll
,
423 OSArray
* personalityNames
= NULL
); // priv/prot
424 virtual OSReturn
unload(void);
425 virtual OSReturn
queueKextNotification(
426 const char * notificationName
,
427 OSString
* kextIdentifier
);
429 static void recordIdentifierRequest(
430 OSString
* kextIdentifier
);
432 virtual OSReturn
slidePrelinkedExecutable(bool doCoalesedSlides
);
433 virtual OSReturn
loadExecutable(void);
434 virtual void jettisonLinkeditSegment(void);
435 virtual void jettisonDATASegmentPadding(void);
436 static void considerDestroyingLinkContext(void);
437 virtual OSData
* getExecutable(void);
438 virtual void setLinkedExecutable(OSData
* anExecutable
);
441 friend void OSKextRegisterKextsWithDTrace(void);
442 static void registerKextsWithDTrace(void);
443 virtual void registerWithDTrace(void);
444 virtual void unregisterWithDTrace(void);
445 #endif /* CONFIG_DTRACE */
447 virtual OSReturn
start(bool startDependenciesFlag
= true);
448 virtual OSReturn
stop(void);
449 virtual OSReturn
setVMAttributes(bool protect
, bool wire
);
450 virtual boolean_t
segmentShouldBeWired(kernel_segment_command_t
*seg
);
451 virtual OSReturn
validateKextMapping(bool startFlag
);
452 virtual boolean_t
verifySegmentMapping(kernel_segment_command_t
*seg
);
454 static OSArray
* copyAllKextPersonalities(
455 bool filterSafeBootFlag
= false);
457 static void setPrelinkedPersonalities(OSArray
* personalitiesArray
);
459 static void sendAllKextPersonalitiesToCatalog(
460 bool startMatching
= false);
461 virtual OSReturn
sendPersonalitiesToCatalog(
462 bool startMatching
= false,
463 OSArray
* personalityNames
= NULL
);
465 static bool canUnloadKextWithIdentifier(
466 OSString
* kextIdentifier
,
467 bool checkClassesFlag
= true);
469 static OSReturn
autounloadKext(OSKext
* aKext
);
471 /* Sync with user space.
473 static OSReturn
pingKextd(void);
475 /* Getting info about loaded kexts (kextstat).
477 static OSDictionary
* copyLoadedKextInfo(
478 OSArray
* kextIdentifiers
= NULL
,
479 OSArray
* keys
= NULL
);
480 static OSDictionary
* copyLoadedKextInfoByUUID(
481 OSArray
* kextIdentifiers
= NULL
,
482 OSArray
* keys
= NULL
);
483 static OSData
* copyKextUUIDForAddress(OSNumber
*address
= NULL
);
484 virtual OSDictionary
* copyInfo(OSArray
* keys
= NULL
);
486 /* Logging to user space.
488 static OSKextLogSpec
setUserSpaceLogFilter(
489 OSKextLogSpec userLogSpec
,
490 bool captureFlag
= false);
491 static OSArray
* clearUserSpaceLogFilter(void);
492 static OSKextLogSpec
getUserSpaceLogFilter(void);
494 /* OSMetaClasses defined by kext.
496 virtual OSReturn
addClass(
497 OSMetaClass
* aClass
,
498 uint32_t numClasses
);
499 virtual OSReturn
removeClass(
500 OSMetaClass
* aClass
);
501 virtual bool hasOSMetaClassInstances(void);
502 virtual OSSet
* getMetaClasses(void);
503 static void reportOSMetaClassInstances(
504 const char * kextIdentifier
,
505 OSKextLogSpec msgLogSpec
);
506 virtual void reportOSMetaClassInstances(
507 OSKextLogSpec msgLogSpec
);
509 /* Resource requests and other callback stuff.
511 static OSReturn
dispatchResource(OSDictionary
* requestDict
);
513 static OSReturn
dequeueCallbackForRequestTag(
514 OSKextRequestTag requestTag
,
515 OSDictionary
** callbackRecordOut
);
516 static OSReturn
dequeueCallbackForRequestTag(
517 OSNumber
* requestTagNum
,
518 OSDictionary
** callbackRecordOut
);
519 static void invokeRequestCallback(
520 OSDictionary
* callbackRecord
,
521 OSReturn requestResult
);
522 virtual void invokeOrCancelRequestCallbacks(
523 OSReturn callbackResult
,
524 bool invokeFlag
= true);
525 virtual uint32_t countRequestCallbacks(void);
531 kPrintKextsLock
= 0x01,
532 kPrintKextsUnslide
= 0x02,
533 kPrintKextsTerse
= 0x04
535 static void printKextsInBacktrace(
538 int (* printf_func
)(const char *fmt
, ...),
541 static OSKextLoadedKextSummary
*summaryForAddress(const uintptr_t addr
);
542 static void *kextForAddress(const void *addr
);
543 static boolean_t
summaryIsInBacktrace(
544 OSKextLoadedKextSummary
* summary
,
547 static void printSummary(
548 OSKextLoadedKextSummary
* summary
,
549 int (* printf_func
)(const char *fmt
, ...),
552 static int saveLoadedKextPanicListTyped(
558 static void saveLoadedKextPanicList(void);
559 void savePanicString(bool isLoading
);
560 static void printKextPanicLists(int (*printf_func
)(const char *fmt
, ...));
562 /* Kext summary support.
564 static void updateLoadedKextSummaries(void);
565 void updateLoadedKextSummary(OSKextLoadedKextSummary
*summary
);
566 void updateActiveAccount(OSKextActiveAccount
*accountp
);
568 /* C++ Initialization.
570 virtual void setCPPInitialized(bool initialized
=true);
575 /**************************************/
576 #pragma mark Public Functions
577 /**************************************/
580 // caller must release
581 static OSKext
* lookupKextWithIdentifier(const char * kextIdentifier
);
582 static OSKext
* lookupKextWithIdentifier(OSString
* kextIdentifier
);
583 static OSKext
* lookupKextWithLoadTag(OSKextLoadTag aTag
);
584 static OSKext
* lookupKextWithAddress(vm_address_t address
);
585 static OSKext
* lookupKextWithUUID(uuid_t uuid
);
587 kernel_section_t
*lookupSection(const char *segname
, const char*secname
);
589 static bool isKextWithIdentifierLoaded(const char * kextIdentifier
);
591 static OSReturn
loadKextWithIdentifier(
592 const char * kextIdentifier
,
593 Boolean allowDeferFlag
= true,
594 Boolean delayAutounloadFlag
= false,
595 OSKextExcludeLevel startOpt
= kOSKextExcludeNone
,
596 OSKextExcludeLevel startMatchingOpt
= kOSKextExcludeAll
,
597 OSArray
* personalityNames
= NULL
);
598 static OSReturn
loadKextWithIdentifier(
599 OSString
* kextIdentifier
,
600 Boolean allowDeferFlag
= true,
601 Boolean delayAutounloadFlag
= false,
602 OSKextExcludeLevel startOpt
= kOSKextExcludeNone
,
603 OSKextExcludeLevel startMatchingOpt
= kOSKextExcludeAll
,
604 OSArray
* personalityNames
= NULL
);
605 static OSReturn
removeKextWithIdentifier(
606 const char * kextIdentifier
,
607 bool terminateServicesAndRemovePersonalitiesFlag
= false);
608 static OSReturn
removeKextWithLoadTag(
609 OSKextLoadTag loadTag
,
610 bool terminateServicesAndRemovePersonalitiesFlag
= false);
612 static OSReturn
requestResource(
613 const char * kextIdentifier
,
614 const char * resourceName
,
615 OSKextRequestResourceCallback callback
,
617 OSKextRequestTag
* requestTagOut
);
618 static OSReturn
cancelRequest(
619 OSKextRequestTag requestTag
,
622 static void considerUnloads(Boolean rescheduleOnlyFlag
= false);
623 static void flushNonloadedKexts(Boolean flushPrelinkedKexts
);
624 static void setKextdActive(Boolean active
= true);
625 static void setDeferredLoadSucceeded(Boolean succeeded
= true);
626 static void considerRebuildOfPrelinkedKernel(void);
627 static void createExcludeListFromBooterData(
628 OSDictionary
* theDictionary
,
629 OSCollectionIterator
* theIterator
);
630 static void createExcludeListFromPrelinkInfo(OSArray
* theInfoArray
);
631 static boolean_t
updateExcludeList(OSDictionary
* infoDict
);
633 static bool isWaitingKextd(void);
635 virtual bool setAutounloadEnabled(bool flag
);
637 virtual const OSSymbol
* getIdentifier(void);
638 virtual const char * getIdentifierCString(void);
639 virtual OSKextVersion
getVersion(void);
640 virtual OSKextVersion
getCompatibleVersion(void);
641 virtual bool isLibrary(void);
642 virtual bool isCompatibleWithVersion(OSKextVersion aVersion
);
643 virtual OSObject
* getPropertyForHostArch(const char * key
);
645 virtual OSKextLoadTag
getLoadTag(void);
646 virtual void getSizeInfo(uint32_t *loadSize
, uint32_t *wiredSize
);
647 virtual OSData
* copyUUID(void);
648 virtual OSArray
* copyPersonalitiesArray(void);
650 /* This removes personalities naming the kext (by CFBundleIdentifier),
651 * not all personalities defined by the kext (IOPersonalityPublisher or CFBundleIdentifier).
653 virtual void removePersonalitiesFromCatalog(void);
655 /* Converts common string-valued properties to OSSymbols for lower memory consumption.
657 static void uniquePersonalityProperties(OSDictionary
* personalityDict
);
659 virtual bool declaresExecutable(void); // might be missing
660 virtual bool isInterface(void);
661 virtual bool isKernel(void);
662 virtual bool isKernelComponent(void);
663 virtual bool isExecutable(void);
664 virtual bool isLoadableInSafeBoot(void);
665 virtual bool isPrelinked(void);
666 virtual bool isLoaded(void);
667 virtual bool isStarted(void);
668 virtual bool isCPPInitialized(void);
672 #endif /* !_LIBKERN_OSKEXT_H */