2 * Copyright (c) 2008 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_OSKEXTLIB_H
30 #define _LIBKERN_OSKEXTLIB_H
32 #include <sys/cdefs.h>
36 #include <mach/kmod.h>
37 #include <mach/vm_types.h>
38 #include <uuid/uuid.h>
41 #include <libkern/OSTypes.h>
42 #include <libkern/OSReturn.h>
44 #include <CoreFoundation/CoreFoundation.h>
45 #include <libkern/OSReturn.h>
51 * Declares functions, basic return values, and other constants
52 * related to kernel extensions (kexts).
57 /********************************************************************/
58 #pragma mark OSReturn Values for Kernel Extensions
59 /********************************************************************/
62 * @group OSReturn Values for Kernel Extensions
63 * Many kext-related functions return these values,
64 * as well as those defined under
65 * <code>@link //apple_ref/c/tdef/OSReturn OSReturn@/link</code>
66 * and other variants of <code>kern_return_t</code>.
69 #ifdef XNU_KERNEL_PRIVATE
70 /*********************************************************************
71 * Check libsyscall/mach/err_libkern.sub when editing or adding
73 *********************************************************************/
74 #endif /* XNU_KERNEL_PRIVATE */
76 #define sub_libkern_kext err_sub(2)
77 #define libkern_kext_err(code) (sys_libkern|sub_libkern_kext|(code))
81 * @define kOSKextReturnInternalError
82 * @abstract An internal error in the kext library.
83 * Contrast with <code>@link //apple_ref/c/econst/OSReturnError
84 * OSReturnError@/link</code>.
86 #define kOSKextReturnInternalError libkern_kext_err(0x1)
89 * @define kOSKextReturnNoMemory
90 * @abstract Memory allocation failed.
92 #define kOSKextReturnNoMemory libkern_kext_err(0x2)
95 * @define kOSKextReturnNoResources
96 * @abstract Some resource other than memory (such as available load tags)
99 #define kOSKextReturnNoResources libkern_kext_err(0x3)
102 * @define kOSKextReturnNotPrivileged
103 * @abstract The caller lacks privileges to perform the requested operation.
105 #define kOSKextReturnNotPrivileged libkern_kext_err(0x4)
108 * @define kOSKextReturnInvalidArgument
109 * @abstract Invalid argument.
111 #define kOSKextReturnInvalidArgument libkern_kext_err(0x5)
114 * @define kOSKextReturnNotFound
115 * @abstract Search item not found.
117 #define kOSKextReturnNotFound libkern_kext_err(0x6)
120 * @define kOSKextReturnBadData
121 * @abstract Malformed data (not used for XML).
123 #define kOSKextReturnBadData libkern_kext_err(0x7)
126 * @define kOSKextReturnSerialization
127 * @abstract Error converting or (un)serializing URL, string, or XML.
129 #define kOSKextReturnSerialization libkern_kext_err(0x8)
132 * @define kOSKextReturnUnsupported
133 * @abstract Operation is no longer or not yet supported.
135 #define kOSKextReturnUnsupported libkern_kext_err(0x9)
138 * @define kOSKextReturnDisabled
139 * @abstract Operation is currently disabled.
141 #define kOSKextReturnDisabled libkern_kext_err(0xa)
144 * @define kOSKextReturnNotAKext
145 * @abstract Bundle is not a kernel extension.
147 #define kOSKextReturnNotAKext libkern_kext_err(0xb)
150 * @define kOSKextReturnValidation
151 * @abstract Validation failures encountered; check diagnostics for details.
153 #define kOSKextReturnValidation libkern_kext_err(0xc)
156 * @define kOSKextReturnAuthentication
157 * @abstract Authetication failures encountered; check diagnostics for details.
159 #define kOSKextReturnAuthentication libkern_kext_err(0xd)
162 * @define kOSKextReturnDependencies
163 * @abstract Dependency resolution failures encountered; check diagnostics for details.
165 #define kOSKextReturnDependencies libkern_kext_err(0xe)
168 * @define kOSKextReturnArchNotFound
169 * @abstract Kext does not contain code for the requested architecture.
171 #define kOSKextReturnArchNotFound libkern_kext_err(0xf)
174 * @define kOSKextReturnCache
175 * @abstract An error occurred processing a system kext cache.
177 #define kOSKextReturnCache libkern_kext_err(0x10)
180 * @define kOSKextReturnDeferred
181 * @abstract Operation has been posted asynchronously to user space (kernel only).
183 #define kOSKextReturnDeferred libkern_kext_err(0x11)
186 * @define kOSKextReturnBootLevel
187 * @abstract Kext not loadable or operation not allowed at current boot level.
189 #define kOSKextReturnBootLevel libkern_kext_err(0x12)
192 * @define kOSKextReturnNotLoadable
193 * @abstract Kext cannot be loaded; check diagnostics for details.
195 #define kOSKextReturnNotLoadable libkern_kext_err(0x13)
198 * @define kOSKextReturnLoadedVersionDiffers
199 * @abstract A different version (or executable UUID, or executable by checksum)
200 * of the requested kext is already loaded.
202 #define kOSKextReturnLoadedVersionDiffers libkern_kext_err(0x14)
205 * @define kOSKextReturnDependencyLoadError
206 * @abstract A load error occurred on a dependency of the kext being loaded.
208 #define kOSKextReturnDependencyLoadError libkern_kext_err(0x15)
211 * @define kOSKextReturnLinkError
212 * @abstract A link failure occured with this kext or a dependency.
214 #define kOSKextReturnLinkError libkern_kext_err(0x16)
217 * @define kOSKextReturnStartStopError
218 * @abstract The kext start or stop routine returned an error.
220 #define kOSKextReturnStartStopError libkern_kext_err(0x17)
223 * @define kOSKextReturnInUse
224 * @abstract The kext is currently in use or has outstanding references,
225 * and cannot be unloaded.
227 #define kOSKextReturnInUse libkern_kext_err(0x18)
230 * @define kOSKextReturnTimeout
231 * @abstract A kext request has timed out.
233 #define kOSKextReturnTimeout libkern_kext_err(0x19)
236 * @define kOSKextReturnStopping
237 * @abstract The kext is in the process of stopping; requests cannot be made.
239 #define kOSKextReturnStopping libkern_kext_err(0x1a)
242 * @define kOSKextReturnSystemPolicy
243 * @abstract The kext was prevented from loading due to system policy.
245 #define kOSKextReturnSystemPolicy libkern_kext_err(0x1b)
249 /********************************************************************/
250 #pragma mark Kext/OSBundle Property List Keys
251 /********************************************************************/
254 * @group Kext Property List Keys
255 * These constants cover CFBundle properties defined for kernel extensions.
256 * Because they are used in the kernel, if you want to use one with
257 * CFBundle APIs you'll need to wrap it in a <code>CFSTR()</code> macro.
261 /* Define C-string versions of the CFBundle keys for use in the kernel.
263 #define kCFBundleIdentifierKey "CFBundleIdentifier"
264 #define kCFBundleIdentifierKernelKey "CFBundleIdentifierKernel"
265 #define kCFBundleVersionKey "CFBundleVersion"
266 #define kCFBundleNameKey "CFBundleName"
267 #define kCFBundleExecutableKey "CFBundleExecutable"
268 #define kCFBundlePackageTypeKey "CFBundlePackageType"
269 #define kCFBundleDriverKitUUIDKey "CFBundleDriverKitUUID"
273 * @define kOSBundleCompatibleVersionKey
274 * @abstract A string giving the backwards-compatible version of a library kext
275 * in extended Mac OS 'vers' format (####.##.##s{1-255} where 's'
276 * is a build stage 'd', 'a', 'b', 'f' or 'fc').
278 #define kOSBundleCompatibleVersionKey "OSBundleCompatibleVersion"
281 * @define kOSBundleEnableKextLoggingKey
282 * @abstract Set to true to have the kernel kext logging spec applied
284 * See <code>@link //apple_ref/c/econst/OSKextLogSpec
285 * OSKextLogSpec@/link</code>.
287 #define kOSBundleEnableKextLoggingKey "OSBundleEnableKextLogging"
290 * @define kOSBundleIsInterfaceKey
291 * @abstract A boolean value indicating whether the kext executable
292 * contains only symbol references.
294 #define kOSBundleIsInterfaceKey "OSBundleIsInterface"
297 * @define kOSBundleLibrariesKey
298 * @abstract A dictionary listing link dependencies for this kext.
299 * Keys are bundle identifiers, values are version strings.
301 #define kOSBundleLibrariesKey "OSBundleLibraries"
304 * @define kOSBundleRequiredKey
305 * @abstract A string indicating in which kinds of startup this kext
306 * may need to load during early startup (before
307 * <code>@link //apple_ref/doc/man/8/kextd kextcache(8)@/link</code>).
309 * The value is one of:
311 * <li>@link kOSBundleRequiredRoot "OSBundleRequiredRoot"@/link</li>
312 * <li>@link kOSBundleRequiredLocalRoot "OSBundleRequiredLocalRoot"@/link</li>
313 * <li>@link kOSBundleRequiredNetworkRoot "OSBundleRequiredNetworkRoot"@/link</li>
314 * <li>@link kOSBundleRequiredSafeBoot "OSBundleRequiredSafeBoot"@/link</li>
315 * <li>@link kOSBundleRequiredConsole "OSBundleRequiredConsole"@/link</li>
318 * Use this property judiciously.
319 * Every kext that declares a value other than "OSBundleRequiredSafeBoot"
320 * increases startup time, as the booter must read it into memory,
321 * or startup kext caches must include it.
323 #define kOSBundleRequiredKey "OSBundleRequired"
326 * @define kOSBundleAllowUserLoadKey
327 * @abstract A boolean value indicating whether
328 * <code>@link //apple_ref/doc/man/8/kextd kextcache(8)@/link</code>
329 * will honor a non-root process's request to load a kext.
331 * See <code>@link //apple_ref/doc/compositePage/c/func/KextManagerLoadKextWithURL
332 * KextManagerLoadKextWithURL@/link</code>
333 * and <code>@link //apple_ref/doc/compositePage/c/func/KextManagerLoadKextWithIdentifier
334 * KextManagerLoadKextWithIdentifier@/link</code>.
336 #define kOSBundleAllowUserLoadKey "OSBundleAllowUserLoad"
339 * @define kOSKernelResourceKey
340 * @abstract A boolean value indicating whether the kext represents a built-in
341 * component of the kernel.
343 #define kOSKernelResourceKey "OSKernelResource"
346 * @define kOSKextVariantOverrideKey
347 * @abstract A dictionary with target names as key and a target-specific variant
350 #define kOSKextVariantOverrideKey "OSKextVariantOverride"
353 * @define kIOKitPersonalitiesKey
354 * @abstract A dictionary of dictionaries used in matching for I/O Kit drivers.
356 #define kIOKitPersonalitiesKey "IOKitPersonalities"
359 * @define kIOPersonalityPublisherKey
360 * @abstract Used in personalities sent to the I/O Kit,
361 * contains the CFBundleIdentifier of the kext
362 * that the personality originated in.
364 #define kIOPersonalityPublisherKey "IOPersonalityPublisher"
368 * @define kAppleTextHashesKey
369 * @abstract A dictionary conataining hashes for corecrypto kext.
371 #define kAppleTextHashesKey "AppleTextHashes"
377 /********************************************************************/
378 #pragma mark Kext/OSBundle Property Deprecated Keys
379 /********************************************************************/
382 * @define kOSBundleDebugLevelKey
384 * Deprecated (used on some releases of Mac OS X prior to 10.6 Snow Leopard).
385 * Value is an integer from 1-6, corresponding to the verbose levels
386 * of kext tools on those releases.
387 * On 10.6 Snow Leopard, use <code>@link OSKextEnableKextLogging
388 * OSKextEnableKextLogging@/link</code>.
390 #define kOSBundleDebugLevelKey "OSBundleDebugLevel"
393 * @define kOSBundleSharedExecutableIdentifierKey
394 * @abstract Deprecated (used on some releases of Mac OS X
395 * prior to 10.6 Snow Leopard).
396 * Value is the bundle identifier of the pseudokext
397 * that contains an executable shared by this kext.
399 #define kOSBundleSharedExecutableIdentifierKey "OSBundleSharedExecutableIdentifier"
403 /********************************************************************/
404 #pragma mark Kext/OSBundle Property List Values
405 /********************************************************************/
409 * @group Kext Property List Values
410 * These constants encompass established values
411 * for kernel extension bundle properties.
415 * @define kOSKextKernelIdentifier
417 * This is the CFBundleIdentifier user for the kernel itself.
419 #define kOSKextKernelIdentifier "__kernel__"
422 * @define kOSKextBundlePackageTypeKext
424 * The bundle type value for Kernel Extensions.
426 #define kOSKextBundlePackageTypeKext "KEXT"
429 * @define kOSKextBundlePackageTypeDriverKit
431 * The bundle type value for Driver Extensions.
433 #define kOSKextBundlePackageTypeDriverKit "DEXT"
436 * @define kOSBundleRequiredRoot
438 * This <code>@link kOSBundleRequiredKey OSBundleRequired@/link</code>
439 * value indicates that the kext may be needed to mount the root filesystem
440 * whether starting from a local or a network volume.
442 #define kOSBundleRequiredRoot "Root"
445 * @define kOSBundleRequiredLocalRoot
447 * This <code>@link kOSBundleRequiredKey OSBundleRequired@/link</code>
448 * value indicates that the kext may be needed to mount the root filesystem
449 * when starting from a local disk.
451 #define kOSBundleRequiredLocalRoot "Local-Root"
454 * @define kOSBundleRequiredNetworkRoot
456 * This <code>@link kOSBundleRequiredKey OSBundleRequired@/link</code>
457 * value indicates that the kext may be needed to mount the root filesystem
458 * when starting over a network connection.
460 #define kOSBundleRequiredNetworkRoot "Network-Root"
463 * @define kOSBundleRequiredSafeBoot
465 * This <code>@link kOSBundleRequiredKey OSBundleRequired@/link</code>
466 * value indicates that the kext can be loaded during a safe startup.
467 * This value does not normally cause the kext to be read by the booter
468 * or included in startup kext caches.
470 #define kOSBundleRequiredSafeBoot "Safe Boot"
473 * @define kOSBundleRequiredConsole
475 * This <code>@link kOSBundleRequiredKey OSBundleRequired@/link</code>
476 * value indicates that the kext may be needed for console access
477 * (specifically in a single-user startup when
478 * <code>@link //apple_ref/doc/man/8/kextd kextd(8)@/link</code>.
480 * and should be loaded during early startup.
482 #define kOSBundleRequiredConsole "Console"
485 * @define kOSBundleRequiredDriverKit
487 * This <code>@link kOSBundleRequiredKey OSBundleRequired@/link</code>
488 * value indicates that the driver extension's (DriverKit driver's)
489 * personalities must be present in the kernel at early boot (specifically
490 * before <code>@link //apple_ref/doc/man/8/kextd kextd(8)@/link</code> starts)
491 * in order to compete with kexts built into the prelinkedkernel. Note that
492 * kextd is still required to launch the user space driver binary. The IOKit
493 * matching will happen during early boot, and the actual driver launch
494 * will happen after kextd starts.
496 #define kOSBundleRequiredDriverKit "DriverKit"
500 /********************************************************************/
501 #pragma mark Kext Information
502 /********************************************************************/
505 * @group Kext Information
506 * Types, constants, and macros providing a kext with information
512 * @typedef OSKextLoadTag
515 * A unique identifier assigned to a loaded instanace of a kext.
518 * If a kext is unloaded and later reloaded, the new instance
519 * has a different load tag.
521 * A kext can get its own load tag in the <code>kmod_info_t</code>
522 * structure passed into its module start routine, as the
523 * <code>id</code> field (cast to this type).
524 * You can use the load tag with the functions
525 * <code>@link OSKextRetainKextWithLoadTag
526 * OSKextRetainKextWithLoadTag@/link</code> and
527 * <code>@link OSKextReleaseKextWithLoadTag
528 * OSKextReleaseKextWithLoadTag@/link</code>.
532 * @typedef OSKextLoadTag
535 * A unique identifier assigned to a loaded instanace of a kext.
538 * If a kext is unloaded and later reloaded, the new instance
539 * has a different load tag.
541 * A kext can get its own load tag in the <code>kmod_info_t</code>
542 * structure passed into its module start routine, as the
543 * <code>id</code> field (cast to this type).
546 typedef uint32_t OSKextLoadTag
;
549 * @define kOSKextInvalidLoadTag
552 * A load tag value that will never be used for a loaded kext;
553 * indicates kext not found.
555 #define kOSKextInvalidLoadTag ((OSKextLoadTag)(-1))
559 /* Make these visible to kexts only and *not* the kernel.
561 #if !XNU_KERNEL_PRIVATE
564 * @function OSKextGetCurrentLoadTag
567 * Returns the run-time load tag for the calling kext as an
568 * <code>@link OSKextLoadTag OSKextLoadTag@/link</code>.
571 * The run-time load tag for the calling kext as an
572 * <code>@link OSKextLoadTag@/link</code>.
575 * The load tag identifies this loaded instance of the kext to the kernel
576 * and to kernel functions that operate on kexts.
578 OSKextLoadTag
OSKextGetCurrentLoadTag(void);
581 * @function OSKextGetCurrentIdentifier
584 * Returns the CFBundleIdentifier for the calling kext as a C string.
587 * The CFBundleIdentifier for the calling kext as a C string.
589 const char * OSKextGetCurrentIdentifier(void);
592 * @function OSKextGetCurrentVersionString
595 * Returns the CFBundleVersion for the calling kext as a C string.
598 * The CFBundleVersion for the calling kext as a C string.
600 const char * OSKextGetCurrentVersionString(void);
602 #endif /* !XNU_KERNEL_PRIVATE */
606 /********************************************************************/
607 #pragma mark Kext Loading C Functions
608 /********************************************************************/
611 * @group Kext Loading C Functions
612 * Functions for loading and tracking kexts in the kernel.
616 * @function OSKextLoadKextWithIdentifier
619 * Request that a kext be loaded.
621 * @param kextIdentifier The bundle identifier of the kext to be loaded.
624 * <code>@link //apple_ref/c/macro/kOSReturnSuccess kOSReturnSuccess@/link</code>
625 * if the kext was loaded (or was already loaded).
626 * <code>@link //apple_ref/c/macro/kOSKextReturnDeferred kOSKextReturnDeferred@/link</code>
627 * if the kext was not found and a request
628 * was queued to <code>@link //apple_ref/doc/man/8/kextd kextd(8)@/link</code>.
629 * Other return values indicate a failure to load the kext.
632 * If a kext is already in the kernel but not loaded, it is loaded immediately.
633 * If it isn't found, an asynchronous load request is
634 * made to <code>@link //apple_ref/doc/man/8/kextd kextd(8)@/link</code>
635 * and <code>@link //apple_ref/c/macro/kOSKextReturnDeferred kOSKextReturnDeferred@/link</code> is returned.
636 * There is no general notification or callback mechanism for load requests.
638 OSReturn
OSKextLoadKextWithIdentifier(const char * kextIdentifier
);
642 * @function OSKextRetainKextWithLoadTag
645 * Retain a loaded kext based on its load tag,
646 * and enable autounload for that kext.
648 * @param loadTag The load tag of the kext to be retained.
649 * See <code>@link OSKextGetCurrentLoadTag@/link</code>.
652 * <code>@link //apple_ref/c/macro/kOSReturnSuccess kOSReturnSuccess@/link</code>
653 * if the kext was retained.
654 * <code>@link //apple_ref/c/macro/kOSKextReturnNotFound kOSKextReturnNotFound@/link</code>
655 * if the kext was not found.
656 * <code>@link //apple_ref/c/macro/kOSKextReturnInvalidArgument
657 * kOSKextReturnInvalidArgument@/link</code>
658 * if <code>loadTag</code> is
659 * <code>@link kOSKextInvalidLoadTag kOSKextInvalidLoadTag@/link</code>.
662 * Retaining a kext prevents it from being unloaded,
663 * either explicitly or automatically, and enables autounload for the kext.
664 * When autounload is enabled, then shortly after the kext's last reference
665 * is dropped, it will be unloaded if there are no outstanding references to it
666 * and there are no instances of its Libkern C++ subclasses (if any).
668 * Kexts that define subclasses of
669 * <code>@link //apple_ref/doc/class/IOService IOService@/link</code>
670 * have autounload enabled automatically.
671 * Other kexts can use the reference count to manage automatic unload
672 * without having to define and create Libkern C++ objects.
673 * For example, a filesystem kext can retain itself whenever a new mount
674 * is created, and release itself when a mount is removed.
675 * When the last mount is removed, the kext will be unloaded after a brief delay.
677 * A kext can get its own load tag using the
678 * <code>@link OSKextGetCurrentLoadTag@/link</code>.
680 * Kexts should not retain and release other kexts; linkage references
681 * are accounted for internally.
683 OSReturn
OSKextRetainKextWithLoadTag(OSKextLoadTag loadTag
);
687 * @function OSKextReleaseKextWithLoadTag
690 * Release a loaded kext based on its load tag.
692 * @param loadTag The load tag of the kext to be released.
693 * See <code>@link OSKextGetCurrentLoadTag@/link</code>.
696 * <code>@link //apple_ref/c/macro/kOSReturnSuccess kOSReturnSuccess@/link</code>
697 * if the kext was released.
698 * <code>@link //apple_ref/c/macro/kOSKextReturnNotFound
699 * kOSKextReturnNotFound@/link</code>
700 * if the kext was not found.
701 * <code>@link //apple_ref/c/macro/kOSKextReturnInvalidArgument
702 * kOSKextReturnInvalidArgument@/link</code>
703 * if <code>loadTag</code> is
704 * <code>@link kOSKextInvalidLoadTag kOSKextInvalidLoadTag@/link</code>.
707 * The kext should have been retained previously via
708 * <code>@link OSKextRetainKextWithLoadTag@/link</code>.
710 * This function schedules an autounload scan for all kexts.
711 * When that scan occurs, if a kext has autounload enabled,
712 * it will be unloaded if there are no outstanding references to it
713 * and there are no instances of its Libkern C++ classes (if any).
715 * Kexts that define subclasses of
716 * <code>@link //apple_ref/doc/class/IOService IOService@/link</code>
717 * have autounload enabled automatically.
718 * Other kexts can use the reference count to manage automatic unload
719 * without having to define and create Libkern C++ objects.
720 * For example, a filesystem kext can be retained whenever a new mount
721 * is created, and released when a mount is removed.
722 * When the last mount is removed, the kext will be unloaded after a brief delay.
724 * While the autounload scan takes place after a delay of at least a minute,
725 * a kext that manages its own reference counts for autounload should
726 * be prepared to have its module stop function called even while the function
727 * calling this function is still running.
729 * A kext can get its own load tag using the
730 * <code>@link OSKextGetCurrentLoadTag@/link</code>.
732 * Kexts should not retain and release other kexts; linkage references
733 * are accounted for internally.
735 OSReturn
OSKextReleaseKextWithLoadTag(OSKextLoadTag loadTag
);
738 /********************************************************************/
740 #pragma mark Kext Requests
741 /********************************************************************/
744 * @group Kext Requests to User Space
745 * Functions for making requests to kextd in user space.
749 * @typedef OSKextRequestTag
752 * Identifies a kext request made to user space.
754 typedef uint32_t OSKextRequestTag
;
757 * @define kOSKextRequestTagInvalid
760 * A request tag value that will never be used for a kext request;
761 * indicates failure to create/queue the request.
763 #define kOSKextRequestTagInvalid ((OSKextRequestTag)-1)
766 * @typedef OSKextRequestResourceCallback
769 * Invoked to provide results for a kext resource request.
771 * @param requestTag The tag of the request that the callback pertains to.
772 * @param result The result of the request:
773 * <code>@link kOSReturnSuccess
774 * kOSReturnSuccess@/link</code>
775 * if the request was fulfilled;
776 * <code>@link kOSKextReturnTimeout
777 * kOSKextReturnTimeout@/link</code>
778 * if the request has timed out;
779 * <code>@link kOSKextReturnStopping
780 * kOSKextReturnStopping@/link</code>
781 * if the kext containing the callback
782 * address for the kext is being unloaded;
783 * or other values on error.
784 * @param resourceData A pointer to the requested resource data.
785 * Owned by the system; the kext should make a copy
786 * if it needs to keep the data past the callback.
787 * @param resourceDataLength The length of <code>resourceData</code>.
788 * @param context The context pointer originally passed to
789 * <code>@link OSKextRequestResource
790 * OSKextRequestResource@/link</code>.
792 typedef void (* OSKextRequestResourceCallback
)(
793 OSKextRequestTag requestTag
,
795 const void * resourceData
,
796 uint32_t resourceDataLength
,
800 * @function OSKextRequestResource
803 * Requests data from a nonlocalized resource file in a kext bundle on disk.
805 * @param kextIdentifier The CFBundleIdentifier of the kext
806 * from which to read the file.
807 * @param resourceName The name of the resource file to read.
808 * @param callback A pointer to a callback function; the address
809 * must be within a currently-loaded kext.
810 * @param context A pointer to arbitrary run-time data
811 * that will be passed to the callback
812 * when it is invoked. May be <code>NULL</code>.
813 * @param requestTagOut If non-<code>NULL</code>,
814 * filled on success with a tag identifying the
816 * (or on failure with <code>@link kOSKextRequestTagInvalid
817 * kOSKextRequestTagInvalid@/link</code>;
819 * <code>@link OSKextCancelRequest
820 * OSKextCancelRequest@/link</code>.
823 * <code>@link kOSReturnSuccess kOSReturnSuccess@/link</code>
824 * if the request is successfully queued.
825 * <code>@link kOSKextReturnInvalidArgument kOSKextReturnInvalidArgument@/link</code>
826 * if <code>kextIdentifier</code> or <code>resourceName</code> or if
827 * <code>callback</code> is not an address within a loaded kext executable.
828 * <code>@link kOSKextReturnStopping kOSKextReturnStopping@/link</code>
829 * if an unload attempt is being made
830 * on the kext containing <code>callback</code>.
831 * Other <code>OSKextReturn...</code> errors are possible.
834 * This function queues an asynchronous request to the user-space kext daemon
835 * <code>@link //apple_ref/doc/man/8/kextd kextd(8)@/link</code>;
836 * requests for resources early in system startup
837 * will not be fulfilled until that daemon starts.
838 * Requests made by a kext while that kext is loading
839 * (specifically in the kext's module start routine)
840 * will not be fulfilled until after the start routine returns and
841 * the kext is completely loaded.
842 * Kexts requesting resources should be sure to perform appropriate locking
843 * in the callback function.
845 * Kext resources are stored in the kext's on-disk bundle under the
846 * Resources subdirectory.
847 * See {@linkdoc //apple_ref/doc/uid/10000123i Bundle Programming Guide}
848 * for an overview of bundle structure.
849 * The localization context of the kext daemon
850 * (namely that of the superuser)
851 * will be used in retrieving resources;
852 * kext resources intended for use in the kernel
853 * should generally not be localized.
855 * <code>callback</code> is guaranteed to be invoked except when:
857 * <li>@link OSKextCancelRequest <code>OSKextCancelRequest</code>@/link
858 * is used to cancel the request.
859 * In this case the kext gets the <code>context</code> pointer
860 * and can clean it up.</li>
861 * <li>The request is made during a kext's module start routine
862 * and the start routine returns an error.
863 * In this case, callbacks cannot be safely invoked, so
864 * the kext should clean up all request contexts
865 * when returning the error from the start routine.</li>
868 * Kexts with pending requests are not subject to autounload,
869 * but requests are subject to timeout after a few minutes.
870 * If that amount of time passes with no response from user space,
871 * <code>callback</code> is invoked with a result of.
872 * <code>@link kOSKextReturnTimeout kOSKextReturnTimeout@/link</code>.
874 * Kexts that are explicitly unloaded have all pending request callbacks
875 * invoked with a result of
876 * <code>@link kOSKextReturnStopping kOSKextReturnStopping@/link</code>.
877 * The kext must handle these callbacks,
878 * even if its stop routine will prevent unloading.
879 * If the kext does prevent unloading, it can reissue resource requests
880 * outside of the stop function.
882 OSReturn
OSKextRequestResource(
883 const char * kextIdentifier
,
884 const char * resourceName
,
885 OSKextRequestResourceCallback callback
,
887 OSKextRequestTag
* requestTagOut
);
890 * @function OSKextCancelRequest
893 * Cancels a pending user-space kext request without invoking the callback.
895 * @param requestTag A tag identifying a pending request.
896 * @param contextOut If non-<code>NULL</code>, filled with the context pointer
897 * originally passed with the request.
900 * <code>@link kOSReturnSuccess kOSReturnSuccess@/link</code>
901 * if the request is successfully canceled.
902 * <code>@link kOSKextReturnNotFound kOSKextReturnNotFound@/link</code>
903 * if <code>requestTag</code> does not identify any pending request.
904 * Other <code>OSKextReturn...</code> errors are possible.
907 * This function cancels a pending request if it exists,
908 * so that its callback will not be invoked.
909 * It returns in <code>contextOut</code>
910 * the context pointer used to create the request
911 * so that any resources allocated for the request can be cleaned up.
913 * Kexts do not need to cancel outstanding requests
914 * in their module stop functions;
915 * when a kext is unloaded, all pending request callbacks
916 * are invoked with a result of
917 * <code>@link kOSKextReturnTimeout kOSKextReturnTimeout@/link</code>
918 * before the stop function is called.
920 OSReturn
OSKextCancelRequest(
921 OSKextRequestTag requestTag
,
926 * @function OSKextGrabPgoData
929 * Grab a LLVM profile data buffer from a loaded kext.
931 * @param uuid the uuid identifying the kext to retrieve data from
932 * @param pSize pointer of where to store the size of the buffer. May be NULL.
933 * @param pBuffer pointer to the output buffer. May be NULL.
934 * @param bufferSize size of the buffer pointed to by pBuffer
935 * @param wait_for_unload (boolean) sleep until the kext is unloaded
936 * @param metadata (boolean) include metadata footer
940 * ENOTSUP if the kext does not have profile data to retrieve.
941 * ENOTSUP if no kext with the given UUID is found
942 * ERRORS if the provided buffer is too small
943 * EIO internal error, such as if __llvm_profile_write_buffer_internal fails
946 OSKextGrabPgoData(uuid_t uuid
,
954 * @function OSKextResetPgoCountersLock
957 * Call this function before trapping into the debugger to call OSKextResetPgoCounters.
960 OSKextResetPgoCountersLock(void);
963 * @function OSKextResetPgoCountersUnlock
966 * Call this function after trapping into the debugger to call OSKextResetPgoCounters.
969 OSKextResetPgoCountersUnlock(void);
972 * @function OSKextResetPgoCounters
974 * @abstract Reset the PGO counters for all kexts. Call only from debugger
975 * context, while holding OSKextResetPgoCountersLock().
978 OSKextResetPgoCounters(void);
983 /********************************************************************/
984 #pragma mark Weak linking
985 /********************************************************************/
988 * @group Weak Linking
989 * Support for weak references to symbols in kexts.
993 * @var gOSKextUnresolved
996 * The value to which a kext's unresolved, weakly-referenced symbols are bound.
999 * A kext must test a weak symbol before using it. A weak symbol
1000 * is only safe to use if it is not equal to <code>gOSKextUnresolved</code>.
1002 * Example for a weak symbol named <code>foo</code>:
1005 * if (&foo != gOSKextUnresolved) {
1008 * printf("foo() is not supported\n");
1013 extern const void * const gOSKextUnresolved
;
1016 * @define OSKextSymbolIsResolved
1019 * Checks whether a weakly-referenced symbol has been resolved.
1021 * @param weak_sym The weak symbol to be tested for resolution.
1024 * <code>TRUE</code> if weak_sym is resolved, or <code>FALSE</code>
1025 * if weak_sym is unresolved.
1028 * This is a convenience macro for testing if weak symbols are resolved.
1030 * Example for a weak symbol named <code>foo</code>:
1033 * if (OSKextSymbolIsResolved(foo)) {
1036 * printf("foo() is not resolved\n");
1041 #define OSKextSymbolIsResolved(weak_sym) \
1042 (&(weak_sym) != gOSKextUnresolved)
1049 /********************************************************************/
1050 #pragma mark Kernel External Components for FIPS compliance
1051 /********************************************************************/
1054 // Kernel External Components for FIPS compliance (KEC_FIPS)
1055 // WARNING - ath_hash is owned by the kernel, do not free
1056 typedef struct AppleTEXTHash
{
1057 int ath_version
;// version of this structure (value is 1 or 2)
1058 int ath_length
; // length of hash data
1059 void * ath_hash
; // hash extracted from AppleTextHashes dict
1061 #endif // CONFIG_KEC_FIPS
1067 #endif /* _LIBKERN_OSKEXTLIB_H */