]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/c++/OSKext.h
xnu-7195.81.3.tar.gz
[apple/xnu.git] / libkern / libkern / c++ / OSKext.h
1 /*
2 * Copyright (c) 2008-2019 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #ifndef _LIBKERN_OSKEXT_H
30 #define _LIBKERN_OSKEXT_H
31
32 extern "C" {
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>
38
39 #ifdef XNU_KERNEL_PRIVATE
40 #include <kern/thread_call.h>
41 #endif /* XNU_KERNEL_PRIVATE */
42 }
43
44
45 #include <libkern/OSKextLib.h>
46 #include <libkern/OSKextLibPrivate.h>
47 #include <libkern/c++/OSObject.h>
48 #include <libkern/c++/OSContainers.h>
49
50 #include <libkern/c++/OSPtr.h>
51 #include <IOKit/IOLocks.h>
52
53 /*********************************************************************
54 * C functions used for callbacks.
55 *********************************************************************/
56 #ifdef XNU_KERNEL_PRIVATE
57 extern "C" {
58 void osdata_kmem_free(void * ptr, unsigned int length);
59 void osdata_phys_free(void * ptr, unsigned int length);
60 void osdata_vm_deallocate(void * ptr, unsigned int length);
61 void osdata_kext_free(void * ptr, unsigned int length);
62 void kxld_log_callback(
63 KXLDLogSubsystem subsystem,
64 KXLDLogLevel level,
65 const char * format,
66 va_list argList,
67 void * user_data);
68 };
69 #endif /* XNU_KERNEL_PRIVATE */
70
71 /*********************************************************************
72 * C Function Prototypes for Friend Declarations.
73 *********************************************************************/
74 class OSKext;
75
76 extern "C" {
77 void OSKextLog(
78 OSKext * aKext,
79 OSKextLogSpec msgLogSpec,
80 const char * format, ...)
81 __attribute__((format(printf, 3, 4)));
82
83 void OSKextVLog(
84 OSKext * aKext,
85 OSKextLogSpec msgLogSpec,
86 const char * format,
87 va_list srcArgList);
88
89 #ifdef XNU_KERNEL_PRIVATE
90 void OSKextRemoveKextBootstrap(void);
91
92 kern_return_t OSRuntimeInitializeCPP(
93 OSKext * kext);
94 kern_return_t OSRuntimeFinalizeCPP(
95 OSKext * kext);
96 void OSRuntimeUnloadCPPForSegment(
97 kernel_segment_command_t * segment);
98 void
99 OSRuntimeSignStructors(
100 kernel_mach_header_t * header);
101 void
102 OSRuntimeSignStructorsInFileset(
103 kernel_mach_header_t * fileset_header);
104
105 kern_return_t is_io_catalog_send_data(
106 mach_port_t masterPort,
107 uint32_t flag,
108 io_buf_ptr_t inData,
109 mach_msg_type_number_t inDataCount,
110 kern_return_t * result);
111
112 void kmod_dump_log(vm_offset_t*, unsigned int, boolean_t);
113 void *OSKextKextForAddress(const void *addr);
114
115 #endif /* XNU_KERNEL_PRIVATE */
116 };
117
118 /********************************************************************/
119 #if PRAGMA_MARK
120 #pragma mark -
121 #endif
122
123 struct list_head {
124 struct list_head *prev;
125 struct list_head *next;
126 };
127
128 struct OSKextGrabPgoStruct {
129 bool metadata;
130 uint64_t *pSize;
131 char *pBuffer;
132 uint64_t bufferSize;
133 int err;
134 struct list_head list_head;
135 };
136
137 #ifndef container_of
138 #define container_of(ptr, type, member) ((type*)(((uintptr_t)ptr) - offsetof(type, member)))
139 #endif
140 /********************************************************************/
141
142 #if XNU_KERNEL_PRIVATE
143
144 struct OSKextAccount {
145 vm_allocation_site_t site;
146 uint32_t loadTag;
147 OSKext * kext;
148 };
149
150 struct OSKextActiveAccount {
151 uintptr_t address;
152 uintptr_t address_end;
153 OSKextAccount * account;
154 };
155 typedef struct OSKextActiveAccount OSKextActiveAccount;
156
157 class OSKextSavedMutableSegment : public OSObject {
158 OSDeclareDefaultStructors(OSKextSavedMutableSegment);
159 public:
160 static OSPtr<OSKextSavedMutableSegment> withSegment(kernel_segment_command_t *seg);
161 OSReturn restoreContents(kernel_segment_command_t *seg);
162 vm_offset_t getVMAddr() const;
163 vm_size_t getVMSize() const;
164 virtual void free(void) APPLE_KEXT_OVERRIDE;
165 private:
166 bool initWithSegment(kernel_segment_command_t *seg);
167 kernel_segment_command_t *savedSegment;
168 vm_offset_t vmaddr;
169 vm_size_t vmsize;
170 void * data;
171 };
172
173 #endif /* XNU_KERNEL_PRIVATE */
174
175 /*
176 * @class OSKext
177 */
178 /********************************************************************/
179 class OSKext : public OSObject
180 {
181 OSDeclareDefaultStructors(OSKext);
182
183 #if PRAGMA_MARK
184 /**************************************/
185 #pragma mark Friend Declarations
186 /**************************************/
187 #endif
188 friend class IOCatalogue;
189 friend class KLDBootstrap;
190 friend class OSMetaClass;
191
192 friend int OSKextGrabPgoData(uuid_t uuid,
193 uint64_t *pSize,
194 char *pBuffer,
195 uint64_t bufferSize,
196 int wait_for_unload,
197 int metadata);
198
199 #ifdef XNU_KERNEL_PRIVATE
200 friend void OSKextVLog(
201 OSKext * aKext,
202 OSKextLogSpec msgLogSpec,
203 const char * format,
204 va_list srcArgList);
205
206 friend void OSKextRemoveKextBootstrap(void);
207 friend OSReturn OSKextUnloadKextWithLoadTag(uint32_t);
208
209 friend kern_return_t kext_request(
210 host_priv_t hostPriv,
211 /* in only */ uint32_t clientLogSpec,
212 /* in only */ vm_offset_t requestIn,
213 /* in only */ mach_msg_type_number_t requestLengthIn,
214 /* out only */ vm_offset_t * responseOut,
215 /* out only */ mach_msg_type_number_t * responseLengthOut,
216 /* out only */ vm_offset_t * logDataOut,
217 /* out only */ mach_msg_type_number_t * logDataLengthOut,
218 /* out only */ kern_return_t * op_result);
219
220 friend kxld_addr_t kern_allocate(
221 u_long size,
222 KXLDAllocateFlags * flags,
223 void * user_data);
224
225 friend void kxld_log_shim(
226 KXLDLogSubsystem subsystem,
227 KXLDLogLevel level,
228 const char * format,
229 va_list argList,
230 void * user_data);
231
232 friend void _OSKextConsiderUnloads(
233 __unused thread_call_param_t p0,
234 __unused thread_call_param_t p1);
235
236 friend kern_return_t OSRuntimeInitializeCPP(
237 OSKext * kext);
238 friend kern_return_t OSRuntimeFinalizeCPP(
239 OSKext * kext);
240 friend void OSRuntimeUnloadCPPForSegment(
241 kernel_segment_command_t * segment);
242
243 friend kern_return_t is_io_catalog_send_data(
244 mach_port_t masterPort,
245 uint32_t flag,
246 io_buf_ptr_t inData,
247 mach_msg_type_number_t inDataCount,
248 kern_return_t * result);
249
250 friend void kmod_panic_dump(vm_offset_t*, unsigned int);
251 friend void kmod_dump_log(vm_offset_t*, unsigned int, boolean_t);
252 friend void kext_dump_panic_lists(int (*printf_func)(const char * fmt, ...));
253 friend void *OSKextKextForAddress(const void *addr);
254
255 #endif /* XNU_KERNEL_PRIVATE */
256
257 private:
258
259 /*************************
260 * Instance variables
261 *************************/
262 OSPtr<OSDictionary> infoDict;
263
264 OSPtr<const OSSymbol> bundleID;
265 OSPtr<OSString> path; // not necessarily correct :-/
266 OSPtr<OSString> executableRelPath;// relative to bundle
267 OSPtr<OSString> userExecutableRelPath;// relative to bundle
268
269 OSKextVersion version; // parsed
270 OSKextVersion compatibleVersion;// parsed
271
272 /* These fields are required for tracking loaded kexts and
273 * will always have values for a loaded kext.
274 */
275 OSKextLoadTag loadTag; // 'id' from old kmod_info;
276 // kOSKextInvalidLoadTag invalid
277 kmod_info_t * kmod_info; // address into linkedExec./alloced for interface
278
279 OSPtr<OSArray> dependencies; // kernel resource does not have any;
280 // links directly to kernel
281
282 /* Only real kexts have these; interface kexts do not.
283 */
284 OSPtr<OSData> linkedExecutable;
285 OSPtr<OSSet> metaClasses; // for C++/OSMetaClass kexts
286
287 /* Only interface kexts have these; non-interface kexts can get at them
288 * in the linked Executable.
289 */
290 OSPtr<OSData> interfaceUUID;
291 OSPtr<OSData> driverKitUUID;
292
293 struct {
294 unsigned int loggingEnabled:1;
295
296 unsigned int hasAllDependencies:1;
297 unsigned int hasBleedthrough:1;
298
299 unsigned int interface:1;
300 unsigned int kernelComponent:1;
301 unsigned int prelinked:1;
302 unsigned int builtin:1;
303 unsigned int loaded:1;
304 unsigned int dtraceInitialized:1;
305 unsigned int starting:1;
306 unsigned int started:1;
307 unsigned int stopping:1;
308 unsigned int unloading:1;
309 unsigned int resetSegmentsFromVnode:1;
310
311 unsigned int requireExplicitLoad:1;
312 unsigned int autounloadEnabled:1;
313 unsigned int delayAutounload:1; // for development
314
315 unsigned int CPPInitialized:1;
316 unsigned int jettisonLinkeditSeg:1;
317 unsigned int resetSegmentsFromImmutableCopy:1;
318 } flags;
319
320 uint32_t matchingRefCount;
321 kc_kind_t kc_type;
322
323 struct list_head pendingPgoHead;
324 uuid_t instance_uuid;
325 OSKextAccount * account;
326 uint32_t builtinKmodIdx;
327 OSPtr<OSArray> savedMutableSegments;
328
329 #if PRAGMA_MARK
330 /**************************************/
331 #pragma mark Private Functions
332 /**************************************/
333 #endif
334
335 #ifdef XNU_KERNEL_PRIVATE
336 /* Startup/shutdown phases.
337 */
338 public:
339 static void initialize(void);
340 static OSPtr<OSDictionary> copyKexts(void);
341 static OSReturn removeKextBootstrap(void);
342 static void willShutdown(void);// called by IOPMrootDomain on shutdown
343 static void willUserspaceReboot(void);
344 static void resetAfterUserspaceReboot(void);
345 static void reportOSMetaClassInstances(
346 const char * kextIdentifier,
347 OSKextLogSpec msgLogSpec);
348 static void OSKextLogDriverKitInfoLoad(OSKext *kext);
349 #endif /* XNU_KERNEL_PRIVATE */
350
351 private:
352 /* Called by power management at sleep/shutdown.
353 */
354 static bool setLoadEnabled(bool flag);
355 static bool setUnloadEnabled(bool flag);
356 static bool setAutounloadsEnabled(bool flag);
357 static bool setKernelRequestsEnabled(bool flag);
358
359 // all getters subject to race condition, caller beware
360 static bool getLoadEnabled(void);
361 static bool getUnloadEnabled(void);
362 static bool getAutounloadEnabled(void);
363 static bool getKernelRequestsEnabled(void);
364
365 /* Instance life cycle.
366 */
367 static OSPtr<OSKext> withBooterData(
368 OSString * deviceTreeName,
369 OSData * booterData);
370 virtual bool initWithBooterData(
371 OSString * deviceTreeName,
372 OSData * booterData);
373
374 static OSPtr<OSKext> withPrelinkedInfoDict(
375 OSDictionary * infoDict,
376 bool doCoalesedSlides, kc_kind_t type);
377 virtual bool initWithPrelinkedInfoDict(
378 OSDictionary * infoDict,
379 bool doCoalesedSlides, kc_kind_t type);
380
381 static OSSharedPtr<OSKext> withCodelessInfo(
382 OSDictionary * infoDict);
383 virtual bool initWithCodelessInfo(
384 OSDictionary * infoDict);
385
386 static void setAllVMAttributes(void);
387
388 virtual bool setInfoDictionaryAndPath(
389 OSDictionary * aDictionary,
390 OSString * aPath);
391 virtual bool setExecutable(
392 OSData * anExecutable,
393 OSData * externalData = NULL,
394 bool externalDataIsMkext = false);
395 virtual bool registerIdentifier(void);
396
397 virtual void free(void) APPLE_KEXT_OVERRIDE;
398
399 static OSReturn removeKext(
400 OSKext * aKext,
401 bool terminateServicesAndRemovePersonalitiesFlag = false);
402
403 virtual bool isInExcludeList(void);
404 virtual bool isLoadable(void);
405
406 /* Mkexts.
407 */
408 #if CONFIG_KXLD
409 static OSPtr<OSKext> withMkext2Info(
410 OSDictionary * anInfoDict,
411 OSData * mkextData);
412 virtual bool initWithMkext2Info(
413 OSDictionary * anInfoDict,
414 OSData * mkextData);
415
416 static OSReturn readMkextArchive(
417 OSData * mkextData,
418 uint32_t * checksumPtr = NULL);
419 static OSReturn readMkext2Archive(
420 OSData * mkextData,
421 OSDictionary ** mkextPlistOut,
422 uint32_t * checksumPtr = NULL);
423
424 static OSReturn readMkext2Archive(
425 OSData * mkextData,
426 OSSharedPtr<OSDictionary> &mkextPlistOut,
427 uint32_t * checksumPtr = NULL);
428
429 virtual OSPtr<OSData> createMkext2FileEntry(
430 OSData * mkextData,
431 OSNumber * offsetNum,
432 const char * entryName);
433 virtual OSPtr<OSData> extractMkext2FileData(
434 UInt8 * data,
435 const char * name,
436 uint32_t compressedSize,
437 uint32_t fullSize);
438 #endif // CONFIG_KXLD
439
440 /* Dependencies.
441 */
442 virtual bool resolveDependencies(
443 OSArray * loopStack = NULL); // priv/prot
444 virtual bool addBleedthroughDependencies(OSArray * anArray);
445 virtual bool flushDependencies(bool forceFlag = false); // priv/prot
446 virtual uint32_t getNumDependencies(void);
447 virtual OSArray * getDependencies(void);
448
449 /* User-space requests (load/generic).
450 */
451 static OSReturn loadFromMkext(
452 OSKextLogSpec clientLogSpec,
453 char * mkextBuffer,
454 uint32_t mkextBufferLength,
455 char ** logInfoOut,
456 uint32_t * logInfoLengthOut);
457 static OSReturn handleRequest(
458 host_priv_t hostPriv,
459 OSKextLogSpec clientLogSpec,
460 char * requestBuffer,
461 uint32_t requestLength,
462 char ** responseOut,
463 uint32_t * responseLengthOut,
464 char ** logInfoOut,
465 uint32_t * logInfoLengthOut);
466 static OSReturn loadCodelessKext(
467 OSString * kextIdentifier,
468 OSDictionary * requestDict);
469 static OSReturn serializeLogInfo(
470 OSArray * logInfoArray,
471 char ** logInfoOut,
472 uint32_t * logInfoLengthOut);
473
474 /* Loading.
475 */
476 static bool addKextsFromKextCollection(kernel_mach_header_t *mh,
477 OSDictionary *infoDict, const char *text_seg_name,
478 OSData **kcUUID, kc_kind_t type);
479
480 static bool addKextsFromKextCollection(kernel_mach_header_t *mh,
481 OSDictionary *infoDict, const char *text_seg_name,
482 OSSharedPtr<OSData> &kcUUID, kc_kind_t type);
483
484 static bool registerDeferredKextCollection(kernel_mach_header_t *mh,
485 OSSharedPtr<OSObject> &parsedXML, kc_kind_t type);
486 static OSSharedPtr<OSObject> consumeDeferredKextCollection(kc_kind_t type);
487
488 virtual OSReturn load(
489 OSKextExcludeLevel startOpt = kOSKextExcludeNone,
490 OSKextExcludeLevel startMatchingOpt = kOSKextExcludeAll,
491 OSArray * personalityNames = NULL);// priv/prot
492 virtual OSReturn unload(void);
493 virtual OSReturn queueKextNotification(
494 const char * notificationName,
495 OSString * kextIdentifier);
496
497 static void recordIdentifierRequest(
498 OSString * kextIdentifier);
499
500 virtual OSReturn slidePrelinkedExecutable(bool doCoalesedSlides);
501 virtual OSReturn loadExecutable(void);
502 virtual void jettisonLinkeditSegment(void);
503 virtual void jettisonDATASegmentPadding(void);
504 static void considerDestroyingLinkContext(void);
505 virtual OSData * getExecutable(void);
506 virtual void setLinkedExecutable(OSData * anExecutable);
507
508 #if CONFIG_DTRACE
509 friend void OSKextRegisterKextsWithDTrace(void);
510 static void registerKextsWithDTrace(void);
511 virtual void registerWithDTrace(void);
512 virtual void unregisterWithDTrace(void);
513 #endif /* CONFIG_DTRACE */
514
515 virtual OSReturn start(bool startDependenciesFlag = true);
516 virtual OSReturn stop(void);
517 virtual OSReturn setVMAttributes(bool protect, bool wire);
518 virtual boolean_t segmentShouldBeWired(kernel_segment_command_t *seg);
519 virtual OSReturn validateKextMapping(bool startFlag);
520 virtual boolean_t verifySegmentMapping(kernel_segment_command_t *seg);
521
522 static OSPtr<OSArray> copyAllKextPersonalities(
523 bool filterSafeBootFlag = false);
524
525 static void setPrelinkedPersonalities(OSArray * personalitiesArray);
526
527 static void sendAllKextPersonalitiesToCatalog(
528 bool startMatching = false);
529 virtual OSReturn sendPersonalitiesToCatalog(
530 bool startMatching = false,
531 OSArray * personalityNames = NULL);
532
533 static bool canUnloadKextWithIdentifier(
534 OSString * kextIdentifier,
535 bool checkClassesFlag = true);
536
537 static OSReturn autounloadKext(OSKext * aKext);
538
539 /* Sync with user space.
540 */
541 static OSReturn pingIOKitDaemon(void);
542
543 /* Getting info about loaded kexts (kextstat).
544 */
545 static OSPtr<OSDictionary> copyLoadedKextInfo(
546 OSArray * kextIdentifiers = NULL,
547 OSArray * keys = NULL);
548 static OSPtr<OSDictionary> copyLoadedKextInfoByUUID(
549 OSArray * kextIdentifiers = NULL,
550 OSArray * keys = NULL);
551 static OSPtr<OSDictionary> copyKextCollectionInfo(
552 OSDictionary *requestDict,
553 OSArray *infoKeys = NULL);
554 static OSPtr<OSData> copyKextUUIDForAddress(OSNumber *address = NULL);
555 virtual OSPtr<OSDictionary> copyInfo(OSArray * keys = NULL);
556
557 /* Logging to user space.
558 */
559 static OSKextLogSpec setUserSpaceLogFilter(
560 OSKextLogSpec userLogSpec,
561 bool captureFlag = false);
562 static OSPtr<OSArray> clearUserSpaceLogFilter(void);
563 static OSKextLogSpec getUserSpaceLogFilter(void);
564
565 /* OSMetaClasses defined by kext.
566 */
567 virtual OSReturn addClass(
568 OSMetaClass * aClass,
569 uint32_t numClasses);
570 virtual OSReturn removeClass(
571 OSMetaClass * aClass);
572 virtual bool hasOSMetaClassInstances(void);
573 virtual OSSet * getMetaClasses(void);
574
575 virtual void reportOSMetaClassInstances(
576 OSKextLogSpec msgLogSpec);
577
578 /* Resource requests and other callback stuff.
579 */
580 static OSReturn loadFileSetKexts(OSDictionary * requestDict);
581
582 static OSReturn loadKCFileSet(const char *filepath, kc_kind_t type);
583
584 #if defined(__x86_64__) || defined(__i386__)
585 static OSReturn mapKCFileSet(
586 void *control,
587 vm_size_t fsize,
588 kernel_mach_header_t **mh,
589 off_t file_offset,
590 uintptr_t *slide,
591 bool pageable,
592 void *map_entry_buffer);
593 static OSReturn protectKCFileSet(
594 kernel_mach_header_t *mh,
595 kc_kind_t type);
596 static OSReturn mapKCTextSegment(
597 void *control,
598 kernel_mach_header_t **mhp,
599 off_t file_offset,
600 uintptr_t *slide,
601 void *map_entry_list);
602 static void freeKCFileSetcontrol(void);
603 OSReturn resetKCFileSetSegments(void);
604 static void jettisonFileSetLinkeditSegment(kernel_mach_header_t *mh);
605 #endif //(__x86_64__) || defined(__i386__)
606
607 static OSReturn validateKCFileSetUUID(
608 OSDictionary *infoDict,
609 kc_kind_t type);
610
611 static OSReturn validateKCUUIDfromPrelinkInfo(
612 uuid_t *loaded_kcuuid,
613 kc_kind_t type,
614 OSDictionary *infoDict,
615 const char *uuid_key);
616
617 static OSReturn dispatchResource(OSDictionary * requestDict);
618
619 static OSReturn setMissingAuxKCBundles(OSDictionary * requestDict);
620
621 static OSReturn setAuxKCBundleAvailable(OSString *kextIdentifier,
622 OSDictionary *requestDict);
623
624 static OSReturn dequeueCallbackForRequestTag(
625 OSKextRequestTag requestTag,
626 LIBKERN_RETURNS_RETAINED OSDictionary ** callbackRecordOut);
627 static OSReturn dequeueCallbackForRequestTag(
628 OSNumber * requestTagNum,
629 LIBKERN_RETURNS_RETAINED OSDictionary ** callbackRecordOut);
630
631 static OSReturn dequeueCallbackForRequestTag(
632 OSKextRequestTag requestTag,
633 OSSharedPtr<OSDictionary> &callbackRecordOut);
634 static OSReturn dequeueCallbackForRequestTag(
635 OSNumber * requestTagNum,
636 OSSharedPtr<OSDictionary> &callbackRecordOut);
637
638 static void invokeRequestCallback(
639 OSDictionary * callbackRecord,
640 OSReturn requestResult);
641 virtual void invokeOrCancelRequestCallbacks(
642 OSReturn callbackResult,
643 bool invokeFlag = true);
644 virtual uint32_t countRequestCallbacks(void);
645 OSReturn resetMutableSegments(void);
646
647 /* panic() support.
648 */
649 public:
650 enum {
651 kPrintKextsLock = 0x01,
652 kPrintKextsUnslide = 0x02,
653 kPrintKextsTerse = 0x04
654 };
655 static void printKextsInBacktrace(
656 vm_offset_t * addr,
657 unsigned int cnt,
658 int (* printf_func)(const char *fmt, ...),
659 uint32_t flags);
660 bool isDriverKit(void);
661 bool isInFileset(void);
662 private:
663 static OSKextLoadedKextSummary *summaryForAddress(const uintptr_t addr);
664 static void *kextForAddress(const void *addr);
665 static boolean_t summaryIsInBacktrace(
666 OSKextLoadedKextSummary * summary,
667 vm_offset_t * addr,
668 unsigned int cnt);
669 static void printSummary(
670 OSKextLoadedKextSummary * summary,
671 int (* printf_func)(const char *fmt, ...),
672 uint32_t flags);
673
674 static int saveLoadedKextPanicListTyped(
675 const char * prefix,
676 int invertFlag,
677 int libsFlag,
678 char * paniclist,
679 uint32_t list_size);
680 static void saveLoadedKextPanicList(void);
681 void savePanicString(bool isLoading);
682 static void printKextPanicLists(int (*printf_func)(const char *fmt, ...));
683
684 /* Kext summary support.
685 */
686 static void updateLoadedKextSummaries(void);
687 void updateLoadedKextSummary(OSKextLoadedKextSummary *summary);
688 void updateActiveAccount(OSKextActiveAccount *accountp);
689
690 #ifdef XNU_KERNEL_PRIVATE
691 public:
692 #endif /* XNU_KERNEL_PRIVATE */
693
694 /* C++ Initialization.
695 */
696 virtual void setCPPInitialized(bool initialized = true);
697
698 #if PRAGMA_MARK
699 /**************************************/
700 #pragma mark Public Functions
701 /**************************************/
702 #endif
703 public:
704 // caller must release
705 static OSPtr<OSKext> lookupKextWithIdentifier(const char * kextIdentifier);
706 static OSPtr<OSKext> lookupKextWithIdentifier(OSString * kextIdentifier);
707 static OSPtr<OSKext> lookupKextWithLoadTag(OSKextLoadTag aTag);
708 static OSPtr<OSKext> lookupKextWithAddress(vm_address_t address);
709 static OSPtr<OSKext> lookupKextWithUUID(uuid_t uuid);
710
711 kernel_section_t *lookupSection(const char *segname, const char*secname);
712
713 static bool isKextWithIdentifierLoaded(const char * kextIdentifier);
714
715 static OSReturn loadKextWithIdentifier(
716 const char * kextIdentifier,
717 Boolean allowDeferFlag = true,
718 Boolean delayAutounloadFlag = false,
719 OSKextExcludeLevel startOpt = kOSKextExcludeNone,
720 OSKextExcludeLevel startMatchingOpt = kOSKextExcludeAll,
721 OSArray * personalityNames = NULL);
722
723 static OSReturn loadKextWithIdentifier(
724 OSString * kextIdentifier,
725 LIBKERN_RETURNS_RETAINED_ON_ZERO OSObject ** kextRef,
726 Boolean allowDeferFlag = true,
727 Boolean delayAutounloadFlag = false,
728 OSKextExcludeLevel startOpt = kOSKextExcludeNone,
729 OSKextExcludeLevel startMatchingOpt = kOSKextExcludeAll,
730 OSArray * personalityNames = NULL);
731
732 static OSReturn loadKextWithIdentifier(
733 OSString * kextIdentifier,
734 OSSharedPtr<OSObject> &kextRef,
735 Boolean allowDeferFlag = true,
736 Boolean delayAutounloadFlag = false,
737 OSKextExcludeLevel startOpt = kOSKextExcludeNone,
738 OSKextExcludeLevel startMatchingOpt = kOSKextExcludeAll,
739 OSArray * personalityNames = NULL);
740
741 static OSReturn loadKextFromKC(OSKext *theKext, OSDictionary *requestDict);
742
743 static void dropMatchingReferences(
744 OSSet * kexts);
745
746 bool hasDependency(const OSSymbol * depID);
747
748 static OSReturn removeKextWithIdentifier(
749 const char * kextIdentifier,
750 bool terminateServicesAndRemovePersonalitiesFlag = false);
751 static OSReturn removeKextWithLoadTag(
752 OSKextLoadTag loadTag,
753 bool terminateServicesAndRemovePersonalitiesFlag = false);
754 static OSReturn requestDaemonLaunch(
755 OSString * kextIdentifier,
756 OSString * serverName,
757 OSNumber * serverTag,
758 class IOUserServerCheckInToken ** checkInToken);
759
760 static OSReturn requestDaemonLaunch(
761 OSString * kextIdentifier,
762 OSString * serverName,
763 OSNumber * serverTag,
764 OSSharedPtr<class IOUserServerCheckInToken> &checkInToken);
765
766 static OSReturn requestResource(
767 const char * kextIdentifier,
768 const char * resourceName,
769 OSKextRequestResourceCallback callback,
770 void * context,
771 OSKextRequestTag * requestTagOut);
772 static OSReturn cancelRequest(
773 OSKextRequestTag requestTag,
774 void ** contextOut);
775
776 static void considerUnloads(Boolean rescheduleOnlyFlag = false);
777 static void flushNonloadedKexts(Boolean flushPrelinkedKexts);
778 static void setIOKitDaemonActive(bool active = true);
779 static void setDeferredLoadSucceeded(Boolean succeeded = true);
780 static void considerRebuildOfPrelinkedKernel(void);
781 static void createExcludeListFromBooterData(
782 OSDictionary * theDictionary,
783 OSCollectionIterator * theIterator);
784 static void createExcludeListFromPrelinkInfo(OSArray * theInfoArray);
785 static boolean_t updateExcludeList(OSDictionary * infoDict);
786
787 static bool pendingIOKitDaemonRequests(void);
788
789 virtual bool setAutounloadEnabled(bool flag);
790
791 virtual const OSSymbol * getIdentifier(void);
792 virtual const char * getIdentifierCString(void);
793 virtual OSKextVersion getVersion(void);
794 virtual OSKextVersion getCompatibleVersion(void);
795 virtual bool isLibrary(void);
796 virtual bool isCompatibleWithVersion(OSKextVersion aVersion);
797 virtual OSObject * getPropertyForHostArch(const char * key);
798
799 virtual OSKextLoadTag getLoadTag(void);
800 virtual void getSizeInfo(uint32_t *loadSize, uint32_t *wiredSize);
801 virtual OSPtr<OSData> copyUUID(void);
802 OSPtr<OSData> copyTextUUID(void);
803 OSPtr<OSData> copyMachoUUID(const kernel_mach_header_t * header);
804 virtual OSPtr<OSArray> copyPersonalitiesArray(void);
805 static bool copyUserExecutablePath(const OSSymbol * bundleID, char * pathResult, size_t pathSize);
806 virtual void setDriverKitUUID(OSData *uuid);
807 /* This removes personalities naming the kext (by CFBundleIdentifier),
808 * not all personalities defined by the kext (IOPersonalityPublisher or CFBundleIdentifier).
809 */
810 virtual void removePersonalitiesFromCatalog(void);
811
812 /* Converts common string-valued properties to OSSymbols for lower memory consumption.
813 */
814 static void uniquePersonalityProperties(OSDictionary * personalityDict);
815 #ifdef XNU_KERNEL_PRIVATE
816 static void uniquePersonalityProperties(OSDictionary * personalityDict, bool defaultAddKernelBundleIdentifier);
817 #endif
818
819 virtual bool declaresExecutable(void); // might be missing
820 virtual bool isInterface(void);
821 virtual bool isKernel(void);
822 virtual bool isKernelComponent(void);
823 virtual bool isExecutable(void);
824 virtual bool isLoadableInSafeBoot(void);
825 virtual bool isPrelinked(void);
826 virtual bool isLoaded(void);
827 virtual bool isStarted(void);
828 virtual bool isCPPInitialized(void);
829
830 const char *
831 getKCTypeString(void)
832 {
833 switch (kc_type) {
834 case KCKindPrimary:
835 return kKCTypePrimary;
836 case KCKindPageable:
837 return kKCTypeSystem;
838 case KCKindAuxiliary:
839 return kKCTypeAuxiliary;
840 case KCKindNone:
841 return kKCTypeCodeless;
842 default:
843 return "??";
844 }
845 }
846 };
847
848 extern "C" void OSKextResetAfterUserspaceReboot(void);
849
850 #endif /* !_LIBKERN_OSKEXT_H */