]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/OSKextLib.h
b9011d07225ec4bf4506849d66bc3081b8d9ae1d
[apple/xnu.git] / libkern / libkern / OSKextLib.h
1 /*
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #ifndef _LIBKERN_OSKEXTLIB_H
30 #define _LIBKERN_OSKEXTLIB_H
31
32 #include <sys/cdefs.h>
33 __BEGIN_DECLS
34
35 #include <stdint.h>
36 #include <mach/kmod.h>
37 #include <mach/vm_types.h>
38 #include <uuid/uuid.h>
39
40 #ifdef KERNEL
41 #include <libkern/OSTypes.h>
42 #include <libkern/OSReturn.h>
43 #else
44 #include <CoreFoundation/CoreFoundation.h>
45 #include <libkern/OSReturn.h>
46 #endif /* KERNEL */
47
48 /*!
49 * @header
50 *
51 * Declares functions, basic return values, and other constants
52 * related to kernel extensions (kexts).
53 */
54
55 #if PRAGMA_MARK
56 #pragma mark -
57 /********************************************************************/
58 #pragma mark OSReturn Values for Kernel Extensions
59 /********************************************************************/
60 #endif
61 /*!
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>.
67 */
68
69 #ifdef XNU_KERNEL_PRIVATE
70 /*********************************************************************
71 * Check libsyscall/mach/err_libkern.sub when editing or adding
72 * result codes!
73 *********************************************************************/
74 #endif /* XNU_KERNEL_PRIVATE */
75
76 #define sub_libkern_kext err_sub(2)
77 #define libkern_kext_err(code) (sys_libkern|sub_libkern_kext|(code))
78
79
80 /*!
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>.
85 */
86 #define kOSKextReturnInternalError libkern_kext_err(0x1)
87
88 /*!
89 * @define kOSKextReturnNoMemory
90 * @abstract Memory allocation failed.
91 */
92 #define kOSKextReturnNoMemory libkern_kext_err(0x2)
93
94 /*!
95 * @define kOSKextReturnNoResources
96 * @abstract Some resource other than memory (such as available load tags)
97 * is exhausted.
98 */
99 #define kOSKextReturnNoResources libkern_kext_err(0x3)
100
101 /*!
102 * @define kOSKextReturnNotPrivileged
103 * @abstract The caller lacks privileges to perform the requested operation.
104 */
105 #define kOSKextReturnNotPrivileged libkern_kext_err(0x4)
106
107 /*!
108 * @define kOSKextReturnInvalidArgument
109 * @abstract Invalid argument.
110 */
111 #define kOSKextReturnInvalidArgument libkern_kext_err(0x5)
112
113 /*!
114 * @define kOSKextReturnNotFound
115 * @abstract Search item not found.
116 */
117 #define kOSKextReturnNotFound libkern_kext_err(0x6)
118
119 /*!
120 * @define kOSKextReturnBadData
121 * @abstract Malformed data (not used for XML).
122 */
123 #define kOSKextReturnBadData libkern_kext_err(0x7)
124
125 /*!
126 * @define kOSKextReturnSerialization
127 * @abstract Error converting or (un)serializing URL, string, or XML.
128 */
129 #define kOSKextReturnSerialization libkern_kext_err(0x8)
130
131 /*!
132 * @define kOSKextReturnUnsupported
133 * @abstract Operation is no longer or not yet supported.
134 */
135 #define kOSKextReturnUnsupported libkern_kext_err(0x9)
136
137 /*!
138 * @define kOSKextReturnDisabled
139 * @abstract Operation is currently disabled.
140 */
141 #define kOSKextReturnDisabled libkern_kext_err(0xa)
142
143 /*!
144 * @define kOSKextReturnNotAKext
145 * @abstract Bundle is not a kernel extension.
146 */
147 #define kOSKextReturnNotAKext libkern_kext_err(0xb)
148
149 /*!
150 * @define kOSKextReturnValidation
151 * @abstract Validation failures encountered; check diagnostics for details.
152 */
153 #define kOSKextReturnValidation libkern_kext_err(0xc)
154
155 /*!
156 * @define kOSKextReturnAuthentication
157 * @abstract Authetication failures encountered; check diagnostics for details.
158 */
159 #define kOSKextReturnAuthentication libkern_kext_err(0xd)
160
161 /*!
162 * @define kOSKextReturnDependencies
163 * @abstract Dependency resolution failures encountered; check diagnostics for details.
164 */
165 #define kOSKextReturnDependencies libkern_kext_err(0xe)
166
167 /*!
168 * @define kOSKextReturnArchNotFound
169 * @abstract Kext does not contain code for the requested architecture.
170 */
171 #define kOSKextReturnArchNotFound libkern_kext_err(0xf)
172
173 /*!
174 * @define kOSKextReturnCache
175 * @abstract An error occurred processing a system kext cache.
176 */
177 #define kOSKextReturnCache libkern_kext_err(0x10)
178
179 /*!
180 * @define kOSKextReturnDeferred
181 * @abstract Operation has been posted asynchronously to user space (kernel only).
182 */
183 #define kOSKextReturnDeferred libkern_kext_err(0x11)
184
185 /*!
186 * @define kOSKextReturnBootLevel
187 * @abstract Kext not loadable or operation not allowed at current boot level.
188 */
189 #define kOSKextReturnBootLevel libkern_kext_err(0x12)
190
191 /*!
192 * @define kOSKextReturnNotLoadable
193 * @abstract Kext cannot be loaded; check diagnostics for details.
194 */
195 #define kOSKextReturnNotLoadable libkern_kext_err(0x13)
196
197 /*!
198 * @define kOSKextReturnLoadedVersionDiffers
199 * @abstract A different version (or executable UUID, or executable by checksum)
200 * of the requested kext is already loaded.
201 */
202 #define kOSKextReturnLoadedVersionDiffers libkern_kext_err(0x14)
203
204 /*!
205 * @define kOSKextReturnDependencyLoadError
206 * @abstract A load error occurred on a dependency of the kext being loaded.
207 */
208 #define kOSKextReturnDependencyLoadError libkern_kext_err(0x15)
209
210 /*!
211 * @define kOSKextReturnLinkError
212 * @abstract A link failure occured with this kext or a dependency.
213 */
214 #define kOSKextReturnLinkError libkern_kext_err(0x16)
215
216 /*!
217 * @define kOSKextReturnStartStopError
218 * @abstract The kext start or stop routine returned an error.
219 */
220 #define kOSKextReturnStartStopError libkern_kext_err(0x17)
221
222 /*!
223 * @define kOSKextReturnInUse
224 * @abstract The kext is currently in use or has outstanding references,
225 * and cannot be unloaded.
226 */
227 #define kOSKextReturnInUse libkern_kext_err(0x18)
228
229 /*!
230 * @define kOSKextReturnTimeout
231 * @abstract A kext request has timed out.
232 */
233 #define kOSKextReturnTimeout libkern_kext_err(0x19)
234
235 /*!
236 * @define kOSKextReturnStopping
237 * @abstract The kext is in the process of stopping; requests cannot be made.
238 */
239 #define kOSKextReturnStopping libkern_kext_err(0x1a)
240
241 /*!
242 * @define kOSKextReturnSystemPolicy
243 * @abstract The kext was prevented from loading due to system policy.
244 */
245 #define kOSKextReturnSystemPolicy libkern_kext_err(0x1b)
246
247 #if PRAGMA_MARK
248 #pragma mark -
249 /********************************************************************/
250 #pragma mark Kext/OSBundle Property List Keys
251 /********************************************************************/
252 #endif
253 /*!
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.
258 */
259
260 #ifdef KERNEL
261 /* Define C-string versions of the CFBundle keys for use in the kernel.
262 */
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"
270 #endif /* KERNEL */
271
272 /*!
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').
277 */
278 #define kOSBundleCompatibleVersionKey "OSBundleCompatibleVersion"
279
280 /*!
281 * @define kOSBundleEnableKextLoggingKey
282 * @abstract Set to true to have the kernel kext logging spec applied
283 * to the kext.
284 * See <code>@link //apple_ref/c/econst/OSKextLogSpec
285 * OSKextLogSpec@/link</code>.
286 */
287 #define kOSBundleEnableKextLoggingKey "OSBundleEnableKextLogging"
288
289 /*!
290 * @define kOSBundleIsInterfaceKey
291 * @abstract A boolean value indicating whether the kext executable
292 * contains only symbol references.
293 */
294 #define kOSBundleIsInterfaceKey "OSBundleIsInterface"
295
296 /*!
297 * @define kOSBundleLibrariesKey
298 * @abstract A dictionary listing link dependencies for this kext.
299 * Keys are bundle identifiers, values are version strings.
300 */
301 #define kOSBundleLibrariesKey "OSBundleLibraries"
302
303 /*!
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>).
308 * @discussion
309 * The value is one of:
310 * <ul>
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>
316 * </ul>
317 *
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.
322 */
323 #define kOSBundleRequiredKey "OSBundleRequired"
324
325 /*!
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.
330 * @discussion
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>.
335 */
336 #define kOSBundleAllowUserLoadKey "OSBundleAllowUserLoad"
337
338 /*!
339 * @define kOSKernelResourceKey
340 * @abstract A boolean value indicating whether the kext represents a built-in
341 * component of the kernel.
342 */
343 #define kOSKernelResourceKey "OSKernelResource"
344
345 /*!
346 * @define kOSKextVariantOverrideKey
347 * @abstract A dictionary with target names as key and a target-specific variant
348 * name as value.
349 */
350 #define kOSKextVariantOverrideKey "OSKextVariantOverride"
351
352 /*!
353 * @define kIOKitPersonalitiesKey
354 * @abstract A dictionary of dictionaries used in matching for I/O Kit drivers.
355 */
356 #define kIOKitPersonalitiesKey "IOKitPersonalities"
357
358 /*
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.
363 */
364 #define kIOPersonalityPublisherKey "IOPersonalityPublisher"
365
366 #if CONFIG_KEC_FIPS
367 /*
368 * @define kAppleTextHashesKey
369 * @abstract A dictionary conataining hashes for corecrypto kext.
370 */
371 #define kAppleTextHashesKey "AppleTextHashes"
372 #endif
373
374
375
376 #if PRAGMA_MARK
377 /********************************************************************/
378 #pragma mark Kext/OSBundle Property Deprecated Keys
379 /********************************************************************/
380 #endif
381 /*
382 * @define kOSBundleDebugLevelKey
383 * @abstract
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>.
389 */
390 #define kOSBundleDebugLevelKey "OSBundleDebugLevel"
391
392 /*!
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.
398 */
399 #define kOSBundleSharedExecutableIdentifierKey "OSBundleSharedExecutableIdentifier"
400
401
402 #if PRAGMA_MARK
403 /********************************************************************/
404 #pragma mark Kext/OSBundle Property List Values
405 /********************************************************************/
406 #endif
407
408 /*!
409 * @group Kext Property List Values
410 * These constants encompass established values
411 * for kernel extension bundle properties.
412 */
413
414 /*!
415 * @define kOSKextKernelIdentifier
416 * @abstract
417 * This is the CFBundleIdentifier user for the kernel itself.
418 */
419 #define kOSKextKernelIdentifier "__kernel__"
420
421 /*!
422 * @define kOSKextBundlePackageTypeKext
423 * @abstract
424 * The bundle type value for Kernel Extensions.
425 */
426 #define kOSKextBundlePackageTypeKext "KEXT"
427
428 /*!
429 * @define kOSKextBundlePackageTypeDriverKit
430 * @abstract
431 * The bundle type value for Driver Extensions.
432 */
433 #define kOSKextBundlePackageTypeDriverKit "DEXT"
434
435 /*!
436 * @define kOSBundleRequiredRoot
437 * @abstract
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.
441 */
442 #define kOSBundleRequiredRoot "Root"
443
444 /*!
445 * @define kOSBundleRequiredLocalRoot
446 * @abstract
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.
450 */
451 #define kOSBundleRequiredLocalRoot "Local-Root"
452
453 /*!
454 * @define kOSBundleRequiredNetworkRoot
455 * @abstract
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.
459 */
460 #define kOSBundleRequiredNetworkRoot "Network-Root"
461
462 /*!
463 * @define kOSBundleRequiredSafeBoot
464 * @abstract
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.
469 */
470 #define kOSBundleRequiredSafeBoot "Safe Boot"
471
472 /*!
473 * @define kOSBundleRequiredConsole
474 * @abstract
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>.
479 * does not run)
480 * and should be loaded during early startup.
481 */
482 #define kOSBundleRequiredConsole "Console"
483
484 /*!
485 * @define kOSBundleRequiredDriverKit
486 * @abstract
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.
495 */
496 #define kOSBundleRequiredDriverKit "DriverKit"
497
498 #if PRAGMA_MARK
499 #pragma mark -
500 /********************************************************************/
501 #pragma mark Kext Information
502 /********************************************************************/
503 #endif
504 /*!
505 * @group Kext Information
506 * Types, constants, and macros providing a kext with information
507 * about itself.
508 */
509
510 #ifdef KERNEL
511 /*!
512 * @typedef OSKextLoadTag
513 *
514 * @abstract
515 * A unique identifier assigned to a loaded instanace of a kext.
516 *
517 * @discussion
518 * If a kext is unloaded and later reloaded, the new instance
519 * has a different load tag.
520 *
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>.
529 */
530 #else
531 /*!
532 * @typedef OSKextLoadTag
533 *
534 * @abstract
535 * A unique identifier assigned to a loaded instanace of a kext.
536 *
537 * @discussion
538 * If a kext is unloaded and later reloaded, the new instance
539 * has a different load tag.
540 *
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).
544 */
545 #endif
546 typedef uint32_t OSKextLoadTag;
547
548 /*!
549 * @define kOSKextInvalidLoadTag
550 *
551 * @abstract
552 * A load tag value that will never be used for a loaded kext;
553 * indicates kext not found.
554 */
555 #define kOSKextInvalidLoadTag ((OSKextLoadTag)(-1))
556
557 #ifdef KERNEL
558
559 /* Make these visible to kexts only and *not* the kernel.
560 */
561 #if !XNU_KERNEL_PRIVATE
562
563 /*!
564 * @function OSKextGetCurrentLoadTag
565 *
566 * @abstract
567 * Returns the run-time load tag for the calling kext as an
568 * <code>@link OSKextLoadTag OSKextLoadTag@/link</code>.
569 *
570 * @result
571 * The run-time load tag for the calling kext as an
572 * <code>@link OSKextLoadTag@/link</code>.
573 *
574 * @discussion
575 * The load tag identifies this loaded instance of the kext to the kernel
576 * and to kernel functions that operate on kexts.
577 */
578 OSKextLoadTag OSKextGetCurrentLoadTag(void);
579
580 /*!
581 * @function OSKextGetCurrentIdentifier
582 *
583 * @abstract
584 * Returns the CFBundleIdentifier for the calling kext as a C string.
585 *
586 * @result
587 * The CFBundleIdentifier for the calling kext as a C string.
588 */
589 const char * OSKextGetCurrentIdentifier(void);
590
591 /*!
592 * @function OSKextGetCurrentVersionString
593 *
594 * @abstract
595 * Returns the CFBundleVersion for the calling kext as a C string.
596 *
597 * @result
598 * The CFBundleVersion for the calling kext as a C string.
599 */
600 const char * OSKextGetCurrentVersionString(void);
601
602 #endif /* !XNU_KERNEL_PRIVATE */
603
604 #if PRAGMA_MARK
605 #pragma mark -
606 /********************************************************************/
607 #pragma mark Kext Loading C Functions
608 /********************************************************************/
609 #endif
610 /*!
611 * @group Kext Loading C Functions
612 * Functions for loading and tracking kexts in the kernel.
613 */
614
615 /*!
616 * @function OSKextLoadKextWithIdentifier
617 *
618 * @abstract
619 * Request that a kext be loaded.
620 *
621 * @param kextIdentifier The bundle identifier of the kext to be loaded.
622 *
623 * @result
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.
630 *
631 * @discussion
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.
637 */
638 OSReturn OSKextLoadKextWithIdentifier(const char * kextIdentifier);
639
640
641 /*!
642 * @function OSKextRetainKextWithLoadTag
643 *
644 * @abstract
645 * Retain a loaded kext based on its load tag,
646 * and enable autounload for that kext.
647 *
648 * @param loadTag The load tag of the kext to be retained.
649 * See <code>@link OSKextGetCurrentLoadTag@/link</code>.
650 *
651 * @result
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>.
660 *
661 * @discussion
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).
667 *
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.
676 *
677 * A kext can get its own load tag using the
678 * <code>@link OSKextGetCurrentLoadTag@/link</code>.
679 *
680 * Kexts should not retain and release other kexts; linkage references
681 * are accounted for internally.
682 */
683 OSReturn OSKextRetainKextWithLoadTag(OSKextLoadTag loadTag);
684
685
686 /*!
687 * @function OSKextReleaseKextWithLoadTag
688 *
689 * @abstract
690 * Release a loaded kext based on its load tag.
691 *
692 * @param loadTag The load tag of the kext to be released.
693 * See <code>@link OSKextGetCurrentLoadTag@/link</code>.
694 *
695 * @result
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>.
705 *
706 * @discussion
707 * The kext should have been retained previously via
708 * <code>@link OSKextRetainKextWithLoadTag@/link</code>.
709 *
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).
714 *
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.
723 *
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.
728 *
729 * A kext can get its own load tag using the
730 * <code>@link OSKextGetCurrentLoadTag@/link</code>.
731 *
732 * Kexts should not retain and release other kexts; linkage references
733 * are accounted for internally.
734 */
735 OSReturn OSKextReleaseKextWithLoadTag(OSKextLoadTag loadTag);
736
737 #if PRAGMA_MARK
738 /********************************************************************/
739 #pragma mark -
740 #pragma mark Kext Requests
741 /********************************************************************/
742 #endif
743 /*!
744 * @group Kext Requests to User Space
745 * Functions for making requests to kextd in user space.
746 */
747
748 /*!
749 * @typedef OSKextRequestTag
750 *
751 * @abstract
752 * Identifies a kext request made to user space.
753 */
754 typedef uint32_t OSKextRequestTag;
755
756 /*!
757 * @define kOSKextRequestTagInvalid
758 *
759 * @abstract
760 * A request tag value that will never be used for a kext request;
761 * indicates failure to create/queue the request.
762 */
763 #define kOSKextRequestTagInvalid ((OSKextRequestTag)-1)
764
765 /*!
766 * @typedef OSKextRequestResourceCallback
767 *
768 * @abstract
769 * Invoked to provide results for a kext resource request.
770 *
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>.
791 */
792 typedef void (* OSKextRequestResourceCallback)(
793 OSKextRequestTag requestTag,
794 OSReturn result,
795 const void * resourceData,
796 uint32_t resourceDataLength,
797 void * context);
798
799 /*!
800 * @function OSKextRequestResource
801 *
802 * @abstract
803 * Requests data from a nonlocalized resource file in a kext bundle on disk.
804 *
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
815 * pending request
816 * (or on failure with <code>@link kOSKextRequestTagInvalid
817 * kOSKextRequestTagInvalid@/link</code>;
818 * can be used with
819 * <code>@link OSKextCancelRequest
820 * OSKextCancelRequest@/link</code>.
821 *
822 * @result
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.
832 *
833 * @discussion
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.
844 *
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.
854 *
855 * <code>callback</code> is guaranteed to be invoked except when:
856 * <ul>
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>
866 * </ul>
867 *
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>.
873 *
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.
881 */
882 OSReturn OSKextRequestResource(
883 const char * kextIdentifier,
884 const char * resourceName,
885 OSKextRequestResourceCallback callback,
886 void * context,
887 OSKextRequestTag * requestTagOut);
888
889 /*!
890 * @function OSKextCancelRequest
891 *
892 * @abstract
893 * Cancels a pending user-space kext request without invoking the callback.
894 *
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.
898 *
899 * @result
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.
905 *
906 * @discussion
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.
912 *
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.
919 */
920 OSReturn OSKextCancelRequest(
921 OSKextRequestTag requestTag,
922 void ** contextOut);
923
924
925 /*!
926 * @function OSKextGrabPgoData
927 *
928 * @abstract
929 * Grab a LLVM profile data buffer from a loaded kext.
930 *
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
937 *
938 * @result
939 * 0 on success
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
944 */
945 int
946 OSKextGrabPgoData(uuid_t uuid,
947 uint64_t *pSize,
948 char *pBuffer,
949 uint64_t bufferSize,
950 int wait_for_unload,
951 int metadata);
952
953 /*!
954 * @function OSKextResetPgoCountersLock
955 *
956 * @abstract
957 * Call this function before trapping into the debugger to call OSKextResetPgoCounters.
958 */
959 void
960 OSKextResetPgoCountersLock(void);
961
962 /*!
963 * @function OSKextResetPgoCountersUnlock
964 *
965 * @abstract
966 * Call this function after trapping into the debugger to call OSKextResetPgoCounters.
967 */
968 void
969 OSKextResetPgoCountersUnlock(void);
970
971 /*!
972 * @function OSKextResetPgoCounters
973 *
974 * @abstract Reset the PGO counters for all kexts. Call only from debugger
975 * context, while holding OSKextResetPgoCountersLock().
976 */
977 void
978 OSKextResetPgoCounters(void);
979
980
981 #if PRAGMA_MARK
982 #pragma mark -
983 /********************************************************************/
984 #pragma mark Weak linking
985 /********************************************************************/
986 #endif
987 /*!
988 * @group Weak Linking
989 * Support for weak references to symbols in kexts.
990 */
991
992 /*!
993 * @var gOSKextUnresolved
994 *
995 * @abstract
996 * The value to which a kext's unresolved, weakly-referenced symbols are bound.
997 *
998 * @discussion
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>.
1001 *
1002 * Example for a weak symbol named <code>foo</code>:
1003 * <pre>
1004 * @textblock
1005 * if (&foo != gOSKextUnresolved) {
1006 * foo();
1007 * } else {
1008 * printf("foo() is not supported\n");
1009 * }
1010 * @/textblock
1011 * </pre>
1012 */
1013 extern const void * const gOSKextUnresolved;
1014
1015 /*!
1016 * @define OSKextSymbolIsResolved
1017 *
1018 * @abstract
1019 * Checks whether a weakly-referenced symbol has been resolved.
1020 *
1021 * @param weak_sym The weak symbol to be tested for resolution.
1022 *
1023 * @result
1024 * <code>TRUE</code> if weak_sym is resolved, or <code>FALSE</code>
1025 * if weak_sym is unresolved.
1026 *
1027 * @discussion
1028 * This is a convenience macro for testing if weak symbols are resolved.
1029 *
1030 * Example for a weak symbol named <code>foo</code>:
1031 * <pre>
1032 * @textblock
1033 * if (OSKextSymbolIsResolved(foo)) {
1034 * foo();
1035 * } else {
1036 * printf("foo() is not resolved\n");
1037 * }
1038 * @/textblock
1039 * </pre>
1040 */
1041 #define OSKextSymbolIsResolved(weak_sym) \
1042 (&(weak_sym) != gOSKextUnresolved)
1043
1044
1045 #if CONFIG_KEC_FIPS
1046
1047 #if PRAGMA_MARK
1048 #pragma mark -
1049 /********************************************************************/
1050 #pragma mark Kernel External Components for FIPS compliance
1051 /********************************************************************/
1052 #endif
1053
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
1060 } AppleTEXTHash_t;
1061 #endif // CONFIG_KEC_FIPS
1062
1063 #endif /* KERNEL */
1064
1065 __END_DECLS
1066
1067 #endif /* _LIBKERN_OSKEXTLIB_H */