Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
/*
 * Copyright (c) 2008-2019 Apple Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 *
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 */

#ifndef _LIBKERN_OSKEXT_H
#define _LIBKERN_OSKEXT_H

extern "C" {
#include <kern/thread_call.h>
#include <libkern/OSKextLibPrivate.h>
#include <libkern/kernel_mach_header.h>
#include <libkern/kxld.h>
#include <mach/kmod.h>

#ifdef XNU_KERNEL_PRIVATE
#include <kern/thread_call.h>
#endif /* XNU_KERNEL_PRIVATE */
}


#include <libkern/OSKextLib.h>
#include <libkern/OSKextLibPrivate.h>
#include <libkern/c++/OSObject.h>
#include <libkern/c++/OSContainers.h>

#include <libkern/c++/OSPtr.h>
#include <IOKit/IOLocks.h>

/*********************************************************************
* C functions used for callbacks.
*********************************************************************/
#ifdef XNU_KERNEL_PRIVATE
extern "C" {
void osdata_kmem_free(void * ptr, unsigned int length);
void osdata_phys_free(void * ptr, unsigned int length);
void osdata_vm_deallocate(void * ptr, unsigned int length);
void osdata_kext_free(void * ptr, unsigned int length);
void kxld_log_callback(
	KXLDLogSubsystem    subsystem,
	KXLDLogLevel        level,
	const char        * format,
	va_list             argList,
	void              * user_data);
};
#endif /* XNU_KERNEL_PRIVATE */

/*********************************************************************
* C Function Prototypes for Friend Declarations.
*********************************************************************/
class OSKext;
class OSDextStatistics;

extern "C" {
void OSKextLog(
	OSKext         * aKext,
	OSKextLogSpec    msgLogSpec,
	const char     * format, ...) __printflike(3, 4);

void OSKextVLog(
	OSKext         * aKext,
	OSKextLogSpec    msgLogSpec,
	const char     * format,
	va_list          srcArgList) __printflike(3, 0);;

#ifdef XNU_KERNEL_PRIVATE
void OSKextRemoveKextBootstrap(void);

kern_return_t OSRuntimeInitializeCPP(
	OSKext * kext);
kern_return_t OSRuntimeFinalizeCPP(
	OSKext * kext);
void OSRuntimeUnloadCPPForSegment(
	kernel_segment_command_t * segment);
void
OSRuntimeSignStructors(
	kernel_mach_header_t * header);
void
OSRuntimeSignStructorsInFileset(
	kernel_mach_header_t * fileset_header);

kern_return_t is_io_catalog_send_data(
	mach_port_t              masterPort,
	uint32_t                 flag,
	io_buf_ptr_t             inData,
	mach_msg_type_number_t   inDataCount,
	kern_return_t          * result);

void kmod_dump_log(vm_offset_t*, unsigned int, boolean_t);
void *OSKextKextForAddress(const void *addr);

kern_return_t OSKextGetLoadedKextSummaryForAddress(
	const void              * addr,
	OSKextLoadedKextSummary * summary);

#endif /* XNU_KERNEL_PRIVATE */
};

/********************************************************************/
#if PRAGMA_MARK
#pragma mark -
#endif

struct list_head {
	struct list_head *prev;
	struct list_head *next;
};

struct OSKextGrabPgoStruct {
	bool metadata;
	uint64_t *pSize;
	char *pBuffer;
	uint64_t bufferSize;
	int err;
	struct list_head list_head;
};

#ifndef container_of
#define container_of(ptr, type, member) ((type*)(((uintptr_t)ptr) - offsetof(type, member)))
#endif
/********************************************************************/

#if XNU_KERNEL_PRIVATE

struct OSKextAccount {
	vm_allocation_site_t site;

#if DEVELOPMENT || DEBUG
	struct os_refgrp     task_refgrp;
	/*
	 * '5' for the "task_" prefix. task_refgrp_name can be entirely dropped
	 * once we can directly flag the refgrp to be logged.
	 */
	char                 task_refgrp_name[5 + KMOD_MAX_NAME];
#endif /* DEVELOPMENT || DEBUG */
	uint32_t             loadTag;
	OSKext             * kext;
};

struct OSKextActiveAccount {
	uintptr_t       address;
	uintptr_t       address_end;
	OSKextAccount * account;
};
typedef struct OSKextActiveAccount OSKextActiveAccount;

class OSKextSavedMutableSegment : public OSObject {
	OSDeclareDefaultStructors(OSKextSavedMutableSegment);
public:
	static OSPtr<OSKextSavedMutableSegment> withSegment(kernel_segment_command_t *seg);
	OSReturn restoreContents(kernel_segment_command_t *seg);
	vm_offset_t getVMAddr() const;
	vm_size_t getVMSize() const;
	virtual void free(void) APPLE_KEXT_OVERRIDE;
private:
	bool initWithSegment(kernel_segment_command_t *seg);
	kernel_segment_command_t *savedSegment;
	vm_offset_t vmaddr;
	vm_size_t   vmsize;
	void      * data;
};

typedef enum {
	kOSDextCrashPolicyNone,
	kOSDextCrashPolicyReboot,
} OSDextCrashPolicy;

enum {
	kMaxDextCrashesInOneDayDefault = 3,
};

class OSDextStatistics : public OSObject {
	OSDeclareDefaultStructors(OSDextStatistics);
public:
	static OSPtr<OSDextStatistics> create();
	virtual bool init() APPLE_KEXT_OVERRIDE;
	virtual void free() APPLE_KEXT_OVERRIDE;

	OSDextCrashPolicy recordCrash();
	size_t getCrashCount();

private:
	OSPtr<OSArray> crashes;
	IOLock * lock;
};

__enum_closed_decl(OSKextInitResult, uint8_t, {
	kOSKextInitFailure = 0,
	kOSKextInitialized,
	kOSKextAlreadyExist,
});

#endif /* XNU_KERNEL_PRIVATE */

/*
 * @class OSKext
 */
/********************************************************************/
class OSKext : public OSObject
{
	OSDeclareDefaultStructors(OSKext);

#if PRAGMA_MARK
/**************************************/
#pragma mark Friend Declarations
/**************************************/
#endif
	friend class IOCatalogue;
	friend class KLDBootstrap;
	friend class OSMetaClass;

	friend int OSKextGrabPgoData(uuid_t uuid,
	    uint64_t *pSize,
	    char *pBuffer,
	    uint64_t bufferSize,
	    int wait_for_unload,
	    int metadata);

#ifdef XNU_KERNEL_PRIVATE
	friend void OSKextVLog(
		OSKext         * aKext,
		OSKextLogSpec    msgLogSpec,
		const char     * format,
		va_list          srcArgList) __printflike(3, 0);

	friend void OSKextRemoveKextBootstrap(void);
	friend OSReturn OSKextUnloadKextWithLoadTag(uint32_t);

	friend kern_return_t kext_request(
		host_priv_t                             hostPriv,
		/* in only */ uint32_t                 clientLogSpec,
		/* in only */ vm_offset_t              requestIn,
		/* in only */ mach_msg_type_number_t   requestLengthIn,
		/* out only */ vm_offset_t            * responseOut,
		/* out only */ mach_msg_type_number_t * responseLengthOut,
		/* out only */ vm_offset_t            * logDataOut,
		/* out only */ mach_msg_type_number_t * logDataLengthOut,
		/* out only */ kern_return_t          * op_result);

	friend kxld_addr_t kern_allocate(
		u_long              size,
		KXLDAllocateFlags * flags,
		void              * user_data);

	friend void kxld_log_shim(
		KXLDLogSubsystem    subsystem,
		KXLDLogLevel        level,
		const char        * format,
		va_list             argList,
		void              * user_data);

	friend void _OSKextConsiderUnloads(
		__unused thread_call_param_t p0,
		__unused thread_call_param_t p1);

	friend kern_return_t OSRuntimeInitializeCPP(
		OSKext * kext);
	friend kern_return_t OSRuntimeFinalizeCPP(
		OSKext * kext);
	friend void OSRuntimeUnloadCPPForSegment(
		kernel_segment_command_t * segment);

	friend kern_return_t is_io_catalog_send_data(
		mach_port_t              masterPort,
		uint32_t                 flag,
		io_buf_ptr_t             inData,
		mach_msg_type_number_t   inDataCount,
		kern_return_t          * result);

	friend void kmod_panic_dump(vm_offset_t*, unsigned int);
	friend void kmod_dump_log(vm_offset_t*, unsigned int, boolean_t);
	friend void kext_dump_panic_lists(int (*printf_func)(const char * fmt, ...));
	friend void *OSKextKextForAddress(const void *addr);

	friend kern_return_t OSKextGetLoadedKextSummaryForAddress(
		const void              * addr,
		OSKextLoadedKextSummary * summary);

#endif /* XNU_KERNEL_PRIVATE */

private:

/*************************
* Instance variables
*************************/
	OSPtr<OSDictionary>  infoDict;

	OSPtr<const OSSymbol>    bundleID;
	OSPtr<OSString>    path;           // not necessarily correct :-/
	OSPtr<OSString>    executableRelPath;// relative to bundle
	OSPtr<OSString>    userExecutableRelPath;// relative to bundle

	OSKextVersion    version;        // parsed
	OSKextVersion    compatibleVersion;// parsed

/* These fields are required for tracking loaded kexts and
 * will always have values for a loaded kext.
 */
	OSKextLoadTag    loadTag;        // 'id' from old kmod_info;
	                                 // kOSKextInvalidLoadTag invalid
	kmod_info_t    * kmod_info;      // address into linkedExec./alloced for interface

	OSPtr<OSArray>     dependencies;   // kernel resource does not have any;
	// links directly to kernel

/* Only real kexts have these; interface kexts do not.
 */
	OSPtr<OSData>       linkedExecutable;
	OSPtr<OSSet>        metaClasses;       // for C++/OSMetaClass kexts

/* Only interface kexts have these; non-interface kexts can get at them
 * in the linked Executable.
 */
	OSPtr<OSData>       interfaceUUID;
	OSPtr<OSData>       driverKitUUID;

	struct {
		unsigned int loggingEnabled:1;

		unsigned int hasAllDependencies:1;
		unsigned int hasBleedthrough:1;

		unsigned int interface:1;
		unsigned int kernelComponent:1;
		unsigned int prelinked:1;
		unsigned int builtin:1;
		unsigned int loaded:1;
		unsigned int dtraceInitialized:1;
		unsigned int starting:1;
		unsigned int started:1;
		unsigned int stopping:1;
		unsigned int unloading:1;
		unsigned int resetSegmentsFromVnode:1;

		unsigned int requireExplicitLoad:1;
		unsigned int autounloadEnabled:1;
		unsigned int delayAutounload:1; // for development

		unsigned int CPPInitialized:1;
		unsigned int jettisonLinkeditSeg:1;
		unsigned int resetSegmentsFromImmutableCopy:1;
		unsigned int unloadUnsupported:1;
		unsigned int dextToReplace:1;

		/* The Mach-O header contains segment addresses which are unslid. */
		unsigned int unslidMachO:1;
	} flags;

	uint32_t matchingRefCount;
	kc_kind_t kc_type;

	struct list_head pendingPgoHead;
	uuid_t instance_uuid;
	OSKextAccount * account;
	uint32_t builtinKmodIdx;
	OSPtr<OSArray> savedMutableSegments;
	OSPtr<OSDextStatistics> dextStatistics;
	OSPtr<OSData> dextUniqueID;
	uint32_t dextLaunchedCount;

#if PRAGMA_MARK
/**************************************/
#pragma mark Private Functions
/**************************************/
#endif

#ifdef XNU_KERNEL_PRIVATE
/* Startup/shutdown phases.
 */
public:
	static void           initialize(void);
	static OSPtr<OSDictionary> copyKexts(void);
	static OSReturn       removeKextBootstrap(void);
	static void           willShutdown(void);// called by IOPMrootDomain on shutdown
	static void           setWillUserspaceReboot(void);
	static void           resetAfterUserspaceReboot(void);
	static  void reportOSMetaClassInstances(
		const char     * kextIdentifier,
		OSKextLogSpec    msgLogSpec);
	static void OSKextLogDriverKitInfoLoad(OSKext *kext);
	static bool iokitDaemonAvailable(void);
	static bool driverkitEnabled(void);
#endif /* XNU_KERNEL_PRIVATE */

private:
/* Called by power management at sleep/shutdown.
 */
	static bool setLoadEnabled(bool flag);
	static bool setUnloadEnabled(bool flag);
	static bool setAutounloadsEnabled(bool flag);
	static bool setKernelRequestsEnabled(bool flag);

// all getters subject to race condition, caller beware
	static bool getLoadEnabled(void);
	static bool getUnloadEnabled(void);
	static bool getAutounloadEnabled(void);
	static bool getKernelRequestsEnabled(void);

/* Instance life cycle.
 */
	static OSData *parseDextUniqueID(
		OSDictionary * anInfoDict,
		const char *dextIDCS);
	static void setDextUniqueIDInPersonalities(
		OSDictionary * anInfoDict,
		OSData * dextUniqueID);

	static OSPtr<OSKext> withBooterData(
		OSString * deviceTreeName,
		OSData   * booterData);
	virtual bool initWithBooterData(
		OSString * deviceTreeName,
		OSData   * booterData);

	static OSPtr<OSKext> withPrelinkedInfoDict(
		OSDictionary * infoDict,
		bool doCoalesedSlides, kc_kind_t type);
	virtual bool initWithPrelinkedInfoDict(
		OSDictionary * infoDict,
		bool doCoalesedSlides, kc_kind_t type);
	static OSSharedPtr<OSKext> withCodelessInfo(
		OSDictionary * infoDict, OSKextInitResult *result);

	virtual OSKextInitResult initWithCodelessInfo(
		OSDictionary * infoDict);

	static void setAllVMAttributes(void);

	virtual bool setInfoDictionaryAndPath(
		OSDictionary * aDictionary,
		OSString     * aPath);
	virtual bool setExecutable(
		OSData       * anExecutable,
		OSData       * externalData        = NULL,
		bool           externalDataIsMkext = false);
	virtual OSKextInitResult registerIdentifier(void);

	virtual void free(void) APPLE_KEXT_OVERRIDE;

	static OSReturn removeKext(
		OSKext * aKext,
		bool     terminateServicesAndRemovePersonalitiesFlag = false);

	virtual bool isInExcludeList(void);
	virtual bool isLoadable(void);

	static OSKext * allocAndInitFakeKext(
		kmod_info_t *kmod_info);

/* Mkexts.
 */
#if CONFIG_KXLD
	static OSPtr<OSKext> withMkext2Info(
		OSDictionary * anInfoDict,
		OSData       * mkextData);
	virtual bool initWithMkext2Info(
		OSDictionary * anInfoDict,
		OSData       * mkextData);

	static OSReturn readMkextArchive(
		OSData   * mkextData,
		uint32_t * checksumPtr = NULL);
	static OSReturn readMkext2Archive(
		OSData * mkextData,
		OSDictionary ** mkextPlistOut,
		uint32_t * checksumPtr = NULL);

	static OSReturn readMkext2Archive(
		OSData * mkextData,
		OSSharedPtr<OSDictionary> &mkextPlistOut,
		uint32_t * checksumPtr = NULL);

	virtual OSPtr<OSData> createMkext2FileEntry(
		OSData * mkextData,
		OSNumber * offsetNum,
		const char * entryName);
	virtual OSPtr<OSData> extractMkext2FileData(
		UInt8      * data,
		const char * name,
		uint32_t     compressedSize,
		uint32_t     fullSize);
#endif // CONFIG_KXLD

/* Dependencies.
 */
	virtual bool resolveDependencies(
		OSArray * loopStack = NULL); // priv/prot
	virtual bool addBleedthroughDependencies(OSArray * anArray);
	virtual bool flushDependencies(bool forceFlag = false); // priv/prot
	virtual uint32_t  getNumDependencies(void);
	virtual OSArray * getDependencies(void);

/* User-space requests (load/generic).
 */
	static OSReturn loadFromMkext(
		OSKextLogSpec   clientLogSpec,
		char          * mkextBuffer,
		uint32_t        mkextBufferLength,
		char         ** logInfoOut,
		uint32_t      * logInfoLengthOut);
	static OSReturn handleRequest(
		host_priv_t     hostPriv,
		OSKextLogSpec   clientLogSpec,
		char          * requestBuffer,
		uint32_t        requestLength,
		char         ** responseOut,
		uint32_t      * responseLengthOut,
		char         ** logInfoOut,
		uint32_t      * logInfoLengthOut);
	static OSReturn loadCodelessKext(
		OSString      * kextIdentifier,
		OSDictionary  * requestDict);
	static OSReturn serializeLogInfo(
		OSArray   * logInfoArray,
		char     ** logInfoOut,
		uint32_t  * logInfoLengthOut);

/* Loading.
 */
	static bool addKextsFromKextCollection(kernel_mach_header_t *mh,
	    OSDictionary *infoDict, const char *text_seg_name,
	    OSData **kcUUID, kc_kind_t type);

	static bool addKextsFromKextCollection(kernel_mach_header_t *mh,
	    OSDictionary *infoDict, const char *text_seg_name,
	    OSSharedPtr<OSData> &kcUUID, kc_kind_t type);

	static bool registerDeferredKextCollection(kernel_mach_header_t *mh,
	    OSSharedPtr<OSObject> &parsedXML, kc_kind_t type);
	static OSSharedPtr<OSObject> consumeDeferredKextCollection(kc_kind_t type);

	virtual OSReturn load(
		OSKextExcludeLevel   startOpt         = kOSKextExcludeNone,
		OSKextExcludeLevel   startMatchingOpt = kOSKextExcludeAll,
		OSArray            * personalityNames = NULL);// priv/prot
	virtual OSReturn unload(void);
	static OSReturn queueKextNotification(
		const char * notificationName,
		OSString   * kextIdentifier,
		OSData     * dextUniqueIdentifier);

	static void recordIdentifierRequest(
		OSString * kextIdentifier);

	virtual OSReturn slidePrelinkedExecutable(bool doCoalesedSlides);
	virtual OSReturn loadExecutable(void);
	virtual void     jettisonLinkeditSegment(void);
	virtual void     jettisonDATASegmentPadding(void);
	static  void     considerDestroyingLinkContext(void);
	virtual OSData * getExecutable(void);
	virtual void     setLinkedExecutable(OSData * anExecutable);

#if CONFIG_DTRACE
	friend  void OSKextRegisterKextsWithDTrace(void);
	static  void registerKextsWithDTrace(void);
	virtual void registerWithDTrace(void);
	virtual void unregisterWithDTrace(void);
#endif /* CONFIG_DTRACE */

	virtual OSReturn start(bool startDependenciesFlag = true);
	virtual OSReturn stop(void);
	virtual OSReturn setVMAttributes(bool protect, bool wire);
	virtual boolean_t segmentShouldBeWired(kernel_segment_command_t *seg);
	virtual OSReturn validateKextMapping(bool startFlag);
	virtual boolean_t verifySegmentMapping(kernel_segment_command_t *seg);

	static OSPtr<OSArray> copyAllKextPersonalities(
		bool filterSafeBootFlag = false);

	static  void  setPrelinkedPersonalities(OSArray * personalitiesArray);

	static  void  sendAllKextPersonalitiesToCatalog(
		bool startMatching = false);
	virtual OSReturn  sendPersonalitiesToCatalog(
		bool      startMatching    = false,
		OSArray * personalityNames = NULL);

	static bool canUnloadKextWithIdentifier(
		OSString * kextIdentifier,
		bool       checkClassesFlag = true);

	static OSReturn autounloadKext(OSKext * aKext);

/* Sync with user space.
 */
	static OSReturn pingIOKitDaemon(void);

/* Getting info about loaded kexts (kextstat).
 */
	static  OSPtr<OSDictionary> copyLoadedKextInfo(
		OSArray * kextIdentifiers = NULL,
		OSArray * keys = NULL);
	static  OSPtr<OSDictionary> copyLoadedKextInfoByUUID(
		OSArray * kextIdentifiers = NULL,
		OSArray * keys = NULL);
	static  OSPtr<OSDictionary> copyKextCollectionInfo(
		OSDictionary *requestDict,
		OSArray  *infoKeys = NULL);
	static OSPtr<OSData> copyKextUUIDForAddress(OSNumber *address = NULL);
	static OSPtr<OSArray> copyDextsInfo(
		OSArray * kextIdentifiers = NULL,
		OSArray * keys = NULL);
	virtual OSPtr<OSDictionary> copyInfo(OSArray * keys = NULL);

/* Logging to user space.
 */
	static OSKextLogSpec setUserSpaceLogFilter(
		OSKextLogSpec  userLogSpec,
		bool           captureFlag = false);
	static OSPtr<OSArray> clearUserSpaceLogFilter(void);
	static OSKextLogSpec getUserSpaceLogFilter(void);

/* OSMetaClasses defined by kext.
 */
	virtual OSReturn addClass(
		OSMetaClass * aClass,
		uint32_t     numClasses);
	virtual OSReturn removeClass(
		OSMetaClass * aClass);
	virtual bool    hasOSMetaClassInstances(void);
	virtual OSSet * getMetaClasses(void);

	virtual void reportOSMetaClassInstances(
		OSKextLogSpec msgLogSpec);

/* Resource requests and other callback stuff.
 */
	static OSReturn loadFileSetKexts(OSDictionary * requestDict);

	static OSReturn loadKCFileSet(const char *filepath, kc_kind_t type);

#if defined(__x86_64__) || defined(__i386__)
	static OSReturn mapKCFileSet(
		void                 *control,
		vm_size_t            fsize,
		kernel_mach_header_t **mh,
		off_t                file_offset,
		uintptr_t            *slide,
		bool                 pageable,
		void                 *map_entry_buffer);
	static OSReturn protectKCFileSet(
		kernel_mach_header_t *mh,
		kc_kind_t            type);
	static OSReturn mapKCTextSegment(
		void                 *control,
		kernel_mach_header_t **mhp,
		off_t                file_offset,
		uintptr_t            *slide,
		void                 *map_entry_list);
	static void freeKCFileSetcontrol(void);
	OSReturn resetKCFileSetSegments(void);
#endif //(__x86_64__) || defined(__i386__)

	static void jettisonFileSetLinkeditSegment(kernel_mach_header_t *mh);
	static OSReturn validateKCFileSetUUID(
		OSDictionary         *infoDict,
		kc_kind_t            type);

	static OSReturn validateKCUUIDfromPrelinkInfo(
		uuid_t               *loaded_kcuuid,
		kc_kind_t             type,
		OSDictionary         *infoDict,
		const char           *uuid_key);

	static OSReturn dispatchResource(OSDictionary * requestDict);

	static OSReturn setMissingAuxKCBundles(OSDictionary * requestDict);

	static OSReturn setAuxKCBundleAvailable(OSString *kextIdentifier,
	    OSDictionary *requestDict);

	static OSReturn dequeueCallbackForRequestTag(
		OSKextRequestTag    requestTag,
		LIBKERN_RETURNS_RETAINED OSDictionary     ** callbackRecordOut);
	static OSReturn dequeueCallbackForRequestTag(
		OSNumber     *    requestTagNum,
		LIBKERN_RETURNS_RETAINED OSDictionary ** callbackRecordOut);

	static OSReturn dequeueCallbackForRequestTag(
		OSKextRequestTag    requestTag,
		OSSharedPtr<OSDictionary> &callbackRecordOut);
	static OSReturn dequeueCallbackForRequestTag(
		OSNumber     *    requestTagNum,
		OSSharedPtr<OSDictionary> &callbackRecordOut);

	static void invokeRequestCallback(
		OSDictionary * callbackRecord,
		OSReturn         requestResult);
	virtual void invokeOrCancelRequestCallbacks(
		OSReturn callbackResult,
		bool     invokeFlag = true);
	virtual uint32_t countRequestCallbacks(void);
	OSReturn resetMutableSegments(void);
	virtual OSData * getDextUniqueID(void);

	static bool upgradeDext(
		OSKext * olddext,
		OSKext * newdext);
	static bool removeDext(OSKext * dext);
	static void replaceDextInternal(
		OSKext * olddext,
		OSKext * newdext);
/* panic() support.
 */
public:
	enum {
		kPrintKextsLock    = 0x01,
		kPrintKextsUnslide = 0x02,
		kPrintKextsTerse   = 0x04
	};
	static void printKextsInBacktrace(
		vm_offset_t   * addr,
		unsigned int    cnt,
		int          (* printf_func)(const char *fmt, ...),
		uint32_t        flags);
	static void foreachKextInBacktrace(
		vm_offset_t   * addr,
		uint32_t        cnt,
		uint32_t        flags,
		void         (^ handler)(OSKextLoadedKextSummary *summary, uint32_t index));
	bool isDriverKit(void);
	bool isInFileset(void);
private:
	static OSKextLoadedKextSummary *summaryForAddress(const uintptr_t addr);
	static kern_return_t summaryForAddressExt(
		const void              * addr,
		OSKextLoadedKextSummary * summary);
	static void *kextForAddress(const void *addr);
	static boolean_t summaryIsInBacktrace(
		OSKextLoadedKextSummary * summary,
		vm_offset_t             * addr,
		unsigned int              cnt);
	static void printSummary(
		OSKextLoadedKextSummary * summary,
		int                    (* printf_func)(const char *fmt, ...),
		uint32_t                  flags);

	static int saveLoadedKextPanicListTyped(
		const char * prefix,
		int          invertFlag,
		int          libsFlag,
		char       * paniclist,
		uint32_t     list_size);
	static void saveLoadedKextPanicList(void);
	void savePanicString(bool isLoading);
	static void printKextPanicLists(int (*printf_func)(const char *fmt, ...));

/* Kext summary support.
 */
	static void updateLoadedKextSummaries(void);
	void updateLoadedKextSummary(OSKextLoadedKextSummary *summary);
	void updateActiveAccount(OSKextActiveAccount *accountp);
	static void removeDaemonExitRequests(void);

#ifdef XNU_KERNEL_PRIVATE
public:
#endif /* XNU_KERNEL_PRIVATE */

/* C++ Initialization.
 */
	virtual void               setCPPInitialized(bool initialized = true);

#if PRAGMA_MARK
/**************************************/
#pragma mark Public Functions
/**************************************/
#endif
public:
	// caller must release
	static OSPtr<OSKext> lookupKextWithIdentifier(const char * kextIdentifier);
	static OSPtr<OSKext> lookupKextWithIdentifier(OSString * kextIdentifier);
	static OSPtr<OSKext> lookupKextWithLoadTag(OSKextLoadTag aTag);
	static OSPtr<OSKext> lookupKextWithAddress(vm_address_t address);
	static OSPtr<OSKext> lookupKextWithUUID(uuid_t uuid);
	static OSPtr<OSKext> lookupDextWithIdentifier(OSString * dextIdentifier, OSData *dextUniqueIdentifier);

	kernel_section_t *lookupSection(const char *segname, const char*secname);

	static bool isKextWithIdentifierLoaded(const char * kextIdentifier);

	static OSReturn loadKextWithIdentifier(
		const char       * kextIdentifier,
		Boolean            allowDeferFlag      = true,
		Boolean            delayAutounloadFlag = false,
		OSKextExcludeLevel startOpt            = kOSKextExcludeNone,
		OSKextExcludeLevel startMatchingOpt    = kOSKextExcludeAll,
		OSArray          * personalityNames    = NULL);

	static OSReturn loadKextWithIdentifier(
		OSString         * kextIdentifier,
		LIBKERN_RETURNS_RETAINED_ON_ZERO OSObject        ** kextRef,
		Boolean            allowDeferFlag      = true,
		Boolean            delayAutounloadFlag = false,
		OSKextExcludeLevel startOpt            = kOSKextExcludeNone,
		OSKextExcludeLevel startMatchingOpt    = kOSKextExcludeAll,
		OSArray          * personalityNames    = NULL);

	static OSReturn loadKextWithIdentifier(
		OSString         *    kextIdentifier,
		OSSharedPtr<OSObject> &kextRef,
		Boolean                allowDeferFlag      = true,
		Boolean                delayAutounloadFlag = false,
		OSKextExcludeLevel     startOpt            = kOSKextExcludeNone,
		OSKextExcludeLevel     startMatchingOpt    = kOSKextExcludeAll,
		OSArray              * personalityNames    = NULL);

	static OSReturn loadKextFromKC(OSKext *theKext, OSDictionary *requestDict);

	static void dropMatchingReferences(
		OSSet * kexts);

	bool hasDependency(const OSSymbol * depID);

	static OSReturn removeKextWithIdentifier(
		const char * kextIdentifier,
		bool         terminateServicesAndRemovePersonalitiesFlag = false);
	static OSReturn removeKextWithLoadTag(
		OSKextLoadTag loadTag,
		bool          terminateServicesAndRemovePersonalitiesFlag = false);
	static OSReturn requestDaemonLaunch(
		OSString        * kextIdentifier,
		OSString        * serverName,
		OSNumber        * serverTag,
		OSBoolean       * reslide,
		class IOUserServerCheckInToken * checkInToken,
			OSData *serverDUI);
	static OSReturn notifyDextUpgrade(
		OSString        * kextIdentifier,
		OSData          * dextUniqueIdentifier);
	static OSReturn requestResource(
		const char                    * kextIdentifier,
		const char                    * resourceName,
		OSKextRequestResourceCallback   callback,
		void                          * context,
		OSKextRequestTag              * requestTagOut);
	static OSReturn cancelRequest(
		OSKextRequestTag    requestTag,
		void             ** contextOut);

	static void     considerUnloads(Boolean rescheduleOnlyFlag = false);
	static void     flushNonloadedKexts(Boolean flushPrelinkedKexts);
	static void     setIOKitDaemonActive(bool active = true);
	static void     setDeferredLoadSucceeded(Boolean succeeded = true);
	static void     considerRebuildOfPrelinkedKernel(void);
	static void     createExcludeListFromBooterData(
		OSDictionary * theDictionary,
		OSCollectionIterator * theIterator);
	static void     createExcludeListFromPrelinkInfo(OSArray * theInfoArray);
	static boolean_t updateExcludeList(OSDictionary * infoDict);

	static bool     pendingIOKitDaemonRequests(void);

	virtual bool    setAutounloadEnabled(bool flag);

	virtual const OSObject   * getBundleExecutable(void);
	virtual const OSSymbol   * getIdentifier(void);
	virtual const char       * getIdentifierCString(void);
	virtual OSKextVersion      getVersion(void);
	virtual OSKextVersion      getCompatibleVersion(void);
	virtual bool               isLibrary(void);
	virtual bool               isCompatibleWithVersion(OSKextVersion aVersion);
	virtual OSObject         * getPropertyForHostArch(const char * key);

	virtual OSKextLoadTag      getLoadTag(void);
	virtual void               getSizeInfo(uint32_t *loadSize, uint32_t *wiredSize);
	virtual OSPtr<OSData>          copyUUID(void);
	OSPtr<OSData>                  copyTextUUID(void);
	OSPtr<OSData>                  copyMachoUUID(const kernel_mach_header_t * header);
	OSPtr<OSDextStatistics>        copyDextStatistics();
	virtual OSPtr<OSArray>         copyPersonalitiesArray(void);
	static bool                copyUserExecutablePath(const OSSymbol * bundleID, char * pathResult, size_t pathSize);
	virtual void               setDriverKitUUID(LIBKERN_CONSUMED OSData *uuid);
	static  bool               incrementDextLaunchCount(OSKext *dext, OSData *dextUniqueIDToMatch);
	static  bool               decrementDextLaunchCount(OSString *bundleID);

/* This removes personalities naming the kext (by CFBundleIdentifier),
 * not all personalities defined by the kext (IOPersonalityPublisher or CFBundleIdentifier).
 */
	virtual void               removePersonalitiesFromCatalog(void);
/*
 * This removes the personalities naming the kext (by CFBundleIdentifier), and atomically adds
 * the new personalities upgradedPersonalities.
 */
	virtual void               updatePersonalitiesInCatalog(OSArray *upgradedPersonalities);

/* Converts common string-valued properties to OSSymbols for lower memory consumption.
 */
	static void uniquePersonalityProperties(OSDictionary * personalityDict);
#ifdef XNU_KERNEL_PRIVATE
	static void uniquePersonalityProperties(OSDictionary * personalityDict, bool defaultAddKernelBundleIdentifier);
#endif

	static bool                iokitDaemonActive(void);

	virtual bool               declaresExecutable(void); // might be missing
	virtual bool               isInterface(void);
	virtual bool               isKernel(void);
	virtual bool               isKernelComponent(void);
	virtual bool               isExecutable(void);
	virtual bool               isSpecialKernelBinary(void);
	virtual bool               isLoadableInSafeBoot(void);
	virtual bool               isPrelinked(void);
	virtual bool               isLoaded(void);
	virtual bool               isStarted(void);
	virtual bool               isCPPInitialized(void);

	const char *
	getKCTypeString(void)
	{
		switch (kc_type) {
		case KCKindPrimary:
			return kKCTypePrimary;
		case KCKindPageable:
			return kKCTypeSystem;
		case KCKindAuxiliary:
			return kKCTypeAuxiliary;
		case KCKindNone:
			return kKCTypeCodeless;
		default:
			return "??";
		}
	}
};

extern "C" int OSKextIsInUserspaceReboot(void);
extern "C" void OSKextResetAfterUserspaceReboot(void);

#endif /* !_LIBKERN_OSKEXT_H */