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 kOSBundleAllowUserTerminateKey
340 * @abstract A boolean value indicating whether the kextunload tool
341 * is allowed to issue IOService terminate to classes defined in this kext.
342 * @discussion A boolean value indicating whether the kextunload tool
343 * is allowed to issue IOService terminate to classes defined in this kext.
345 #define kOSBundleAllowUserTerminateKey "OSBundleAllowUserTerminate"
348 * @define kOSKernelResourceKey
349 * @abstract A boolean value indicating whether the kext represents a built-in
350 * component of the kernel.
352 #define kOSKernelResourceKey "OSKernelResource"
355 * @define kOSKextVariantOverrideKey
356 * @abstract A dictionary with target names as key and a target-specific variant
359 #define kOSKextVariantOverrideKey "OSKextVariantOverride"
362 * @define kIOKitPersonalitiesKey
363 * @abstract A dictionary of dictionaries used in matching for I/O Kit drivers.
365 #define kIOKitPersonalitiesKey "IOKitPersonalities"
368 * @define kIOPersonalityPublisherKey
369 * @abstract Used in personalities sent to the I/O Kit,
370 * contains the CFBundleIdentifier of the kext
371 * that the personality originated in.
373 #define kIOPersonalityPublisherKey "IOPersonalityPublisher"
377 * @define kAppleTextHashesKey
378 * @abstract A dictionary conataining hashes for corecrypto kext.
380 #define kAppleTextHashesKey "AppleTextHashes"
386 /********************************************************************/
387 #pragma mark Kext/OSBundle Property Deprecated Keys
388 /********************************************************************/
391 * @define kOSBundleDebugLevelKey
393 * Deprecated (used on some releases of Mac OS X prior to 10.6 Snow Leopard).
394 * Value is an integer from 1-6, corresponding to the verbose levels
395 * of kext tools on those releases.
396 * On 10.6 Snow Leopard, use <code>@link OSKextEnableKextLogging
397 * OSKextEnableKextLogging@/link</code>.
399 #define kOSBundleDebugLevelKey "OSBundleDebugLevel"
402 * @define kOSBundleSharedExecutableIdentifierKey
403 * @abstract Deprecated (used on some releases of Mac OS X
404 * prior to 10.6 Snow Leopard).
405 * Value is the bundle identifier of the pseudokext
406 * that contains an executable shared by this kext.
408 #define kOSBundleSharedExecutableIdentifierKey "OSBundleSharedExecutableIdentifier"
412 /********************************************************************/
413 #pragma mark Kext/OSBundle Property List Values
414 /********************************************************************/
418 * @group Kext Property List Values
419 * These constants encompass established values
420 * for kernel extension bundle properties.
424 * @define kOSKextKernelIdentifier
426 * This is the CFBundleIdentifier user for the kernel itself.
428 #define kOSKextKernelIdentifier "__kernel__"
431 * @define kOSKextBundlePackageTypeKext
433 * The bundle type value for Kernel Extensions.
435 #define kOSKextBundlePackageTypeKext "KEXT"
438 * @define kOSKextBundlePackageTypeDriverKit
440 * The bundle type value for Driver Extensions.
442 #define kOSKextBundlePackageTypeDriverKit "DEXT"
445 * @define kOSBundleRequiredRoot
447 * This <code>@link kOSBundleRequiredKey OSBundleRequired@/link</code>
448 * value indicates that the kext may be needed to mount the root filesystem
449 * whether starting from a local or a network volume.
451 #define kOSBundleRequiredRoot "Root"
454 * @define kOSBundleRequiredLocalRoot
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 from a local disk.
460 #define kOSBundleRequiredLocalRoot "Local-Root"
463 * @define kOSBundleRequiredNetworkRoot
465 * This <code>@link kOSBundleRequiredKey OSBundleRequired@/link</code>
466 * value indicates that the kext may be needed to mount the root filesystem
467 * when starting over a network connection.
469 #define kOSBundleRequiredNetworkRoot "Network-Root"
472 * @define kOSBundleRequiredSafeBoot
474 * This <code>@link kOSBundleRequiredKey OSBundleRequired@/link</code>
475 * value indicates that the kext can be loaded during a safe startup.
476 * This value does not normally cause the kext to be read by the booter
477 * or included in startup kext caches.
479 #define kOSBundleRequiredSafeBoot "Safe Boot"
482 * @define kOSBundleRequiredConsole
484 * This <code>@link kOSBundleRequiredKey OSBundleRequired@/link</code>
485 * value indicates that the kext may be needed for console access
486 * (specifically in a single-user startup when
487 * <code>@link //apple_ref/doc/man/8/kextd kextd(8)@/link</code>.
489 * and should be loaded during early startup.
491 #define kOSBundleRequiredConsole "Console"
494 * @define kOSBundleRequiredDriverKit
496 * This <code>@link kOSBundleRequiredKey OSBundleRequired@/link</code>
497 * value indicates that the driver extension's (DriverKit driver's)
498 * personalities must be present in the kernel at early boot (specifically
499 * before <code>@link //apple_ref/doc/man/8/kextd kextd(8)@/link</code> starts)
500 * in order to compete with kexts built into the prelinkedkernel. Note that
501 * kextd is still required to launch the user space driver binary. The IOKit
502 * matching will happen during early boot, and the actual driver launch
503 * will happen after kextd starts.
505 #define kOSBundleRequiredDriverKit "DriverKit"
509 /********************************************************************/
510 #pragma mark Kext Information
511 /********************************************************************/
514 * @group Kext Information
515 * Types, constants, and macros providing a kext with information
521 * @typedef OSKextLoadTag
524 * A unique identifier assigned to a loaded instanace of a kext.
527 * If a kext is unloaded and later reloaded, the new instance
528 * has a different load tag.
530 * A kext can get its own load tag in the <code>kmod_info_t</code>
531 * structure passed into its module start routine, as the
532 * <code>id</code> field (cast to this type).
533 * You can use the load tag with the functions
534 * <code>@link OSKextRetainKextWithLoadTag
535 * OSKextRetainKextWithLoadTag@/link</code> and
536 * <code>@link OSKextReleaseKextWithLoadTag
537 * OSKextReleaseKextWithLoadTag@/link</code>.
541 * @typedef OSKextLoadTag
544 * A unique identifier assigned to a loaded instanace of a kext.
547 * If a kext is unloaded and later reloaded, the new instance
548 * has a different load tag.
550 * A kext can get its own load tag in the <code>kmod_info_t</code>
551 * structure passed into its module start routine, as the
552 * <code>id</code> field (cast to this type).
555 typedef uint32_t OSKextLoadTag
;
558 * @define kOSKextInvalidLoadTag
561 * A load tag value that will never be used for a loaded kext;
562 * indicates kext not found.
564 #define kOSKextInvalidLoadTag ((OSKextLoadTag)(-1))
568 /* Make these visible to kexts only and *not* the kernel.
570 #if !XNU_KERNEL_PRIVATE
573 * @function OSKextGetCurrentLoadTag
576 * Returns the run-time load tag for the calling kext as an
577 * <code>@link OSKextLoadTag OSKextLoadTag@/link</code>.
580 * The run-time load tag for the calling kext as an
581 * <code>@link OSKextLoadTag@/link</code>.
584 * The load tag identifies this loaded instance of the kext to the kernel
585 * and to kernel functions that operate on kexts.
587 OSKextLoadTag
OSKextGetCurrentLoadTag(void);
590 * @function OSKextGetCurrentIdentifier
593 * Returns the CFBundleIdentifier for the calling kext as a C string.
596 * The CFBundleIdentifier for the calling kext as a C string.
598 const char * OSKextGetCurrentIdentifier(void);
601 * @function OSKextGetCurrentVersionString
604 * Returns the CFBundleVersion for the calling kext as a C string.
607 * The CFBundleVersion for the calling kext as a C string.
609 const char * OSKextGetCurrentVersionString(void);
611 #endif /* !XNU_KERNEL_PRIVATE */
615 /********************************************************************/
616 #pragma mark Kext Loading C Functions
617 /********************************************************************/
620 * @group Kext Loading C Functions
621 * Functions for loading and tracking kexts in the kernel.
625 * @function OSKextLoadKextWithIdentifier
628 * Request that a kext be loaded.
630 * @param kextIdentifier The bundle identifier of the kext to be loaded.
633 * <code>@link //apple_ref/c/macro/kOSReturnSuccess kOSReturnSuccess@/link</code>
634 * if the kext was loaded (or was already loaded).
635 * <code>@link //apple_ref/c/macro/kOSKextReturnDeferred kOSKextReturnDeferred@/link</code>
636 * if the kext was not found and a request
637 * was queued to <code>@link //apple_ref/doc/man/8/kextd kextd(8)@/link</code>.
638 * Other return values indicate a failure to load the kext.
641 * If a kext is already in the kernel but not loaded, it is loaded immediately.
642 * If it isn't found, an asynchronous load request is
643 * made to <code>@link //apple_ref/doc/man/8/kextd kextd(8)@/link</code>
644 * and <code>@link //apple_ref/c/macro/kOSKextReturnDeferred kOSKextReturnDeferred@/link</code> is returned.
645 * There is no general notification or callback mechanism for load requests.
647 OSReturn
OSKextLoadKextWithIdentifier(const char * kextIdentifier
);
651 * @function OSKextRetainKextWithLoadTag
654 * Retain a loaded kext based on its load tag,
655 * and enable autounload for that kext.
657 * @param loadTag The load tag of the kext to be retained.
658 * See <code>@link OSKextGetCurrentLoadTag@/link</code>.
661 * <code>@link //apple_ref/c/macro/kOSReturnSuccess kOSReturnSuccess@/link</code>
662 * if the kext was retained.
663 * <code>@link //apple_ref/c/macro/kOSKextReturnNotFound kOSKextReturnNotFound@/link</code>
664 * if the kext was not found.
665 * <code>@link //apple_ref/c/macro/kOSKextReturnInvalidArgument
666 * kOSKextReturnInvalidArgument@/link</code>
667 * if <code>loadTag</code> is
668 * <code>@link kOSKextInvalidLoadTag kOSKextInvalidLoadTag@/link</code>.
671 * Retaining a kext prevents it from being unloaded,
672 * either explicitly or automatically, and enables autounload for the kext.
673 * When autounload is enabled, then shortly after the kext's last reference
674 * is dropped, it will be unloaded if there are no outstanding references to it
675 * and there are no instances of its Libkern C++ subclasses (if any).
677 * Kexts that define subclasses of
678 * <code>@link //apple_ref/doc/class/IOService IOService@/link</code>
679 * have autounload enabled automatically.
680 * Other kexts can use the reference count to manage automatic unload
681 * without having to define and create Libkern C++ objects.
682 * For example, a filesystem kext can retain itself whenever a new mount
683 * is created, and release itself when a mount is removed.
684 * When the last mount is removed, the kext will be unloaded after a brief delay.
686 * A kext can get its own load tag using the
687 * <code>@link OSKextGetCurrentLoadTag@/link</code>.
689 * Kexts should not retain and release other kexts; linkage references
690 * are accounted for internally.
692 OSReturn
OSKextRetainKextWithLoadTag(OSKextLoadTag loadTag
);
696 * @function OSKextReleaseKextWithLoadTag
699 * Release a loaded kext based on its load tag.
701 * @param loadTag The load tag of the kext to be released.
702 * See <code>@link OSKextGetCurrentLoadTag@/link</code>.
705 * <code>@link //apple_ref/c/macro/kOSReturnSuccess kOSReturnSuccess@/link</code>
706 * if the kext was released.
707 * <code>@link //apple_ref/c/macro/kOSKextReturnNotFound
708 * kOSKextReturnNotFound@/link</code>
709 * if the kext was not found.
710 * <code>@link //apple_ref/c/macro/kOSKextReturnInvalidArgument
711 * kOSKextReturnInvalidArgument@/link</code>
712 * if <code>loadTag</code> is
713 * <code>@link kOSKextInvalidLoadTag kOSKextInvalidLoadTag@/link</code>.
716 * The kext should have been retained previously via
717 * <code>@link OSKextRetainKextWithLoadTag@/link</code>.
719 * This function schedules an autounload scan for all kexts.
720 * When that scan occurs, if a kext has autounload enabled,
721 * it will be unloaded if there are no outstanding references to it
722 * and there are no instances of its Libkern C++ classes (if any).
724 * Kexts that define subclasses of
725 * <code>@link //apple_ref/doc/class/IOService IOService@/link</code>
726 * have autounload enabled automatically.
727 * Other kexts can use the reference count to manage automatic unload
728 * without having to define and create Libkern C++ objects.
729 * For example, a filesystem kext can be retained whenever a new mount
730 * is created, and released when a mount is removed.
731 * When the last mount is removed, the kext will be unloaded after a brief delay.
733 * While the autounload scan takes place after a delay of at least a minute,
734 * a kext that manages its own reference counts for autounload should
735 * be prepared to have its module stop function called even while the function
736 * calling this function is still running.
738 * A kext can get its own load tag using the
739 * <code>@link OSKextGetCurrentLoadTag@/link</code>.
741 * Kexts should not retain and release other kexts; linkage references
742 * are accounted for internally.
744 OSReturn
OSKextReleaseKextWithLoadTag(OSKextLoadTag loadTag
);
747 /********************************************************************/
749 #pragma mark Kext Requests
750 /********************************************************************/
753 * @group Kext Requests to User Space
754 * Functions for making requests to kextd in user space.
758 * @typedef OSKextRequestTag
761 * Identifies a kext request made to user space.
763 typedef uint32_t OSKextRequestTag
;
766 * @define kOSKextRequestTagInvalid
769 * A request tag value that will never be used for a kext request;
770 * indicates failure to create/queue the request.
772 #define kOSKextRequestTagInvalid ((OSKextRequestTag)-1)
775 * @typedef OSKextRequestResourceCallback
778 * Invoked to provide results for a kext resource request.
780 * @param requestTag The tag of the request that the callback pertains to.
781 * @param result The result of the request:
782 * <code>@link kOSReturnSuccess
783 * kOSReturnSuccess@/link</code>
784 * if the request was fulfilled;
785 * <code>@link kOSKextReturnTimeout
786 * kOSKextReturnTimeout@/link</code>
787 * if the request has timed out;
788 * <code>@link kOSKextReturnStopping
789 * kOSKextReturnStopping@/link</code>
790 * if the kext containing the callback
791 * address for the kext is being unloaded;
792 * or other values on error.
793 * @param resourceData A pointer to the requested resource data.
794 * Owned by the system; the kext should make a copy
795 * if it needs to keep the data past the callback.
796 * @param resourceDataLength The length of <code>resourceData</code>.
797 * @param context The context pointer originally passed to
798 * <code>@link OSKextRequestResource
799 * OSKextRequestResource@/link</code>.
801 typedef void (* OSKextRequestResourceCallback
)(
802 OSKextRequestTag requestTag
,
804 const void * resourceData
,
805 uint32_t resourceDataLength
,
809 * @function OSKextRequestResource
812 * Requests data from a nonlocalized resource file in a kext bundle on disk.
814 * @param kextIdentifier The CFBundleIdentifier of the kext
815 * from which to read the file.
816 * @param resourceName The name of the resource file to read.
817 * @param callback A pointer to a callback function; the address
818 * must be within a currently-loaded kext.
819 * @param context A pointer to arbitrary run-time data
820 * that will be passed to the callback
821 * when it is invoked. May be <code>NULL</code>.
822 * @param requestTagOut If non-<code>NULL</code>,
823 * filled on success with a tag identifying the
825 * (or on failure with <code>@link kOSKextRequestTagInvalid
826 * kOSKextRequestTagInvalid@/link</code>;
828 * <code>@link OSKextCancelRequest
829 * OSKextCancelRequest@/link</code>.
832 * <code>@link kOSReturnSuccess kOSReturnSuccess@/link</code>
833 * if the request is successfully queued.
834 * <code>@link kOSKextReturnInvalidArgument kOSKextReturnInvalidArgument@/link</code>
835 * if <code>kextIdentifier</code> or <code>resourceName</code> or if
836 * <code>callback</code> is not an address within a loaded kext executable.
837 * <code>@link kOSKextReturnStopping kOSKextReturnStopping@/link</code>
838 * if an unload attempt is being made
839 * on the kext containing <code>callback</code>.
840 * Other <code>OSKextReturn...</code> errors are possible.
843 * This function queues an asynchronous request to the user-space kext daemon
844 * <code>@link //apple_ref/doc/man/8/kextd kextd(8)@/link</code>;
845 * requests for resources early in system startup
846 * will not be fulfilled until that daemon starts.
847 * Requests made by a kext while that kext is loading
848 * (specifically in the kext's module start routine)
849 * will not be fulfilled until after the start routine returns and
850 * the kext is completely loaded.
851 * Kexts requesting resources should be sure to perform appropriate locking
852 * in the callback function.
854 * Kext resources are stored in the kext's on-disk bundle under the
855 * Resources subdirectory.
856 * See {@linkdoc //apple_ref/doc/uid/10000123i Bundle Programming Guide}
857 * for an overview of bundle structure.
858 * The localization context of the kext daemon
859 * (namely that of the superuser)
860 * will be used in retrieving resources;
861 * kext resources intended for use in the kernel
862 * should generally not be localized.
864 * <code>callback</code> is guaranteed to be invoked except when:
866 * <li>@link OSKextCancelRequest <code>OSKextCancelRequest</code>@/link
867 * is used to cancel the request.
868 * In this case the kext gets the <code>context</code> pointer
869 * and can clean it up.</li>
870 * <li>The request is made during a kext's module start routine
871 * and the start routine returns an error.
872 * In this case, callbacks cannot be safely invoked, so
873 * the kext should clean up all request contexts
874 * when returning the error from the start routine.</li>
877 * Kexts with pending requests are not subject to autounload,
878 * but requests are subject to timeout after a few minutes.
879 * If that amount of time passes with no response from user space,
880 * <code>callback</code> is invoked with a result of.
881 * <code>@link kOSKextReturnTimeout kOSKextReturnTimeout@/link</code>.
883 * Kexts that are explicitly unloaded have all pending request callbacks
884 * invoked with a result of
885 * <code>@link kOSKextReturnStopping kOSKextReturnStopping@/link</code>.
886 * The kext must handle these callbacks,
887 * even if its stop routine will prevent unloading.
888 * If the kext does prevent unloading, it can reissue resource requests
889 * outside of the stop function.
891 OSReturn
OSKextRequestResource(
892 const char * kextIdentifier
,
893 const char * resourceName
,
894 OSKextRequestResourceCallback callback
,
896 OSKextRequestTag
* requestTagOut
);
899 * @function OSKextCancelRequest
902 * Cancels a pending user-space kext request without invoking the callback.
904 * @param requestTag A tag identifying a pending request.
905 * @param contextOut If non-<code>NULL</code>, filled with the context pointer
906 * originally passed with the request.
909 * <code>@link kOSReturnSuccess kOSReturnSuccess@/link</code>
910 * if the request is successfully canceled.
911 * <code>@link kOSKextReturnNotFound kOSKextReturnNotFound@/link</code>
912 * if <code>requestTag</code> does not identify any pending request.
913 * Other <code>OSKextReturn...</code> errors are possible.
916 * This function cancels a pending request if it exists,
917 * so that its callback will not be invoked.
918 * It returns in <code>contextOut</code>
919 * the context pointer used to create the request
920 * so that any resources allocated for the request can be cleaned up.
922 * Kexts do not need to cancel outstanding requests
923 * in their module stop functions;
924 * when a kext is unloaded, all pending request callbacks
925 * are invoked with a result of
926 * <code>@link kOSKextReturnTimeout kOSKextReturnTimeout@/link</code>
927 * before the stop function is called.
929 OSReturn
OSKextCancelRequest(
930 OSKextRequestTag requestTag
,
935 * @function OSKextGrabPgoData
938 * Grab a LLVM profile data buffer from a loaded kext.
940 * @param uuid the uuid identifying the kext to retrieve data from
941 * @param pSize pointer of where to store the size of the buffer. May be NULL.
942 * @param pBuffer pointer to the output buffer. May be NULL.
943 * @param bufferSize size of the buffer pointed to by pBuffer
944 * @param wait_for_unload (boolean) sleep until the kext is unloaded
945 * @param metadata (boolean) include metadata footer
949 * ENOTSUP if the kext does not have profile data to retrieve.
950 * ENOTSUP if no kext with the given UUID is found
951 * ERRORS if the provided buffer is too small
952 * EIO internal error, such as if __llvm_profile_write_buffer_internal fails
955 OSKextGrabPgoData(uuid_t uuid
,
963 * @function OSKextResetPgoCountersLock
966 * Call this function before trapping into the debugger to call OSKextResetPgoCounters.
969 OSKextResetPgoCountersLock(void);
972 * @function OSKextResetPgoCountersUnlock
975 * Call this function after trapping into the debugger to call OSKextResetPgoCounters.
978 OSKextResetPgoCountersUnlock(void);
981 * @function OSKextResetPgoCounters
983 * @abstract Reset the PGO counters for all kexts. Call only from debugger
984 * context, while holding OSKextResetPgoCountersLock().
987 OSKextResetPgoCounters(void);
992 /********************************************************************/
993 #pragma mark Weak linking
994 /********************************************************************/
997 * @group Weak Linking
998 * Support for weak references to symbols in kexts.
1002 * @var gOSKextUnresolved
1005 * The value to which a kext's unresolved, weakly-referenced symbols are bound.
1008 * A kext must test a weak symbol before using it. A weak symbol
1009 * is only safe to use if it is not equal to <code>gOSKextUnresolved</code>.
1011 * Example for a weak symbol named <code>foo</code>:
1014 * if (&foo != gOSKextUnresolved) {
1017 * printf("foo() is not supported\n");
1022 extern const void * const gOSKextUnresolved
;
1025 * @define OSKextSymbolIsResolved
1028 * Checks whether a weakly-referenced symbol has been resolved.
1030 * @param weak_sym The weak symbol to be tested for resolution.
1033 * <code>TRUE</code> if weak_sym is resolved, or <code>FALSE</code>
1034 * if weak_sym is unresolved.
1037 * This is a convenience macro for testing if weak symbols are resolved.
1039 * Example for a weak symbol named <code>foo</code>:
1042 * if (OSKextSymbolIsResolved(foo)) {
1045 * printf("foo() is not resolved\n");
1050 #define OSKextSymbolIsResolved(weak_sym) \
1051 (&(weak_sym) != gOSKextUnresolved)
1058 /********************************************************************/
1059 #pragma mark Kernel External Components for FIPS compliance
1060 /********************************************************************/
1063 // Kernel External Components for FIPS compliance (KEC_FIPS)
1064 // WARNING - ath_hash is owned by the kernel, do not free
1065 typedef struct AppleTEXTHash
{
1066 int ath_version
;// version of this structure (value is 1 or 2)
1067 int ath_length
; // length of hash data
1068 void * ath_hash
; // hash extracted from AppleTextHashes dict
1070 #endif // CONFIG_KEC_FIPS
1076 #endif /* _LIBKERN_OSKEXTLIB_H */