]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/OSKextLib.h
8435d0c49382b6dea2e9c4f7596e6440667dce04
[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 #if PRAGMA_MARK
242 #pragma mark -
243 /********************************************************************/
244 #pragma mark Kext/OSBundle Property List Keys
245 /********************************************************************/
246 #endif
247 /*!
248 * @group Kext Property List Keys
249 * These constants cover CFBundle properties defined for kernel extensions.
250 * Because they are used in the kernel, if you want to use one with
251 * CFBundle APIs you'll need to wrap it in a <code>CFSTR()</code> macro.
252 */
253
254 #ifdef KERNEL
255 /* Define C-string versions of the CFBundle keys for use in the kernel.
256 */
257 #define kCFBundleIdentifierKey "CFBundleIdentifier"
258 #define kCFBundleVersionKey "CFBundleVersion"
259 #define kCFBundleNameKey "CFBundleName"
260 #define kCFBundleExecutableKey "CFBundleExecutable"
261 #endif /* KERNEL */
262
263 /*!
264 * @define kOSBundleCompatibleVersionKey
265 * @abstract A string giving the backwards-compatible version of a library kext
266 * in extended Mac OS 'vers' format (####.##.##s{1-255} where 's'
267 * is a build stage 'd', 'a', 'b', 'f' or 'fc').
268 */
269 #define kOSBundleCompatibleVersionKey "OSBundleCompatibleVersion"
270
271 /*!
272 * @define kOSBundleEnableKextLoggingKey
273 * @abstract Set to true to have the kernel kext logging spec applied
274 * to the kext.
275 * See <code>@link //apple_ref/c/econst/OSKextLogSpec
276 * OSKextLogSpec@/link</code>.
277 */
278 #define kOSBundleEnableKextLoggingKey "OSBundleEnableKextLogging"
279
280 /*!
281 * @define kOSBundleIsInterfaceKey
282 * @abstract A boolean value indicating whether the kext executable
283 * contains only symbol references.
284 */
285 #define kOSBundleIsInterfaceKey "OSBundleIsInterface"
286
287 /*!
288 * @define kOSBundleLibrariesKey
289 * @abstract A dictionary listing link dependencies for this kext.
290 * Keys are bundle identifiers, values are version strings.
291 */
292 #define kOSBundleLibrariesKey "OSBundleLibraries"
293
294 /*!
295 * @define kOSBundleRequiredKey
296 * @abstract A string indicating in which kinds of startup this kext
297 * may need to load during early startup (before
298 * <code>@link //apple_ref/doc/man/8/kextd kextcache(8)@/link</code>).
299 * @discussion
300 * The value is one of:
301 * <ul>
302 * <li>@link kOSBundleRequiredRoot "OSBundleRequiredRoot"@/link</li>
303 * <li>@link kOSBundleRequiredLocalRoot "OSBundleRequiredLocalRoot"@/link</li>
304 * <li>@link kOSBundleRequiredNetworkRoot "OSBundleRequiredNetworkRoot"@/link</li>
305 * <li>@link kOSBundleRequiredSafeBoot "OSBundleRequiredSafeBoot"@/link</li>
306 * <li>@link kOSBundleRequiredConsole "OSBundleRequiredConsole"@/link</li>
307 * </ul>
308 *
309 * Use this property judiciously.
310 * Every kext that declares a value other than "OSBundleRequiredSafeBoot"
311 * increases startup time, as the booter must read it into memory,
312 * or startup kext caches must include it.
313 */
314 #define kOSBundleRequiredKey "OSBundleRequired"
315
316 /*!
317 * @define kOSBundleAllowUserLoadKey
318 * @abstract A boolean value indicating whether
319 * <code>@link //apple_ref/doc/man/8/kextd kextcache(8)@/link</code>
320 * will honor a non-root process's request to load a kext.
321 * @discussion
322 * See <code>@link //apple_ref/doc/compositePage/c/func/KextManagerLoadKextWithURL
323 * KextManagerLoadKextWithURL@/link</code>
324 * and <code>@link //apple_ref/doc/compositePage/c/func/KextManagerLoadKextWithIdentifier
325 * KextManagerLoadKextWithIdentifier@/link</code>.
326 */
327 #define kOSBundleAllowUserLoadKey "OSBundleAllowUserLoad"
328
329 /*!
330 * @define kOSKernelResourceKey
331 * @abstract A boolean value indicating whether the kext represents a built-in
332 * component of the kernel.
333 */
334 #define kOSKernelResourceKey "OSKernelResource"
335
336 /*!
337 * @define kIOKitPersonalitiesKey
338 * @abstract A dictionary of dictionaries used in matching for I/O Kit drivers.
339 */
340 #define kIOKitPersonalitiesKey "IOKitPersonalities"
341
342 /*
343 * @define kIOPersonalityPublisherKey
344 * @abstract Used in personalities sent to the I/O Kit,
345 * contains the CFBundleIdentifier of the kext
346 * that the personality originated in.
347 */
348 #define kIOPersonalityPublisherKey "IOPersonalityPublisher"
349
350 #if CONFIG_KEC_FIPS
351 /*
352 * @define kAppleTextHashesKey
353 * @abstract A dictionary conataining hashes for corecrypto kext.
354 */
355 #define kAppleTextHashesKey "AppleTextHashes"
356 #endif
357
358
359
360 #if PRAGMA_MARK
361 /********************************************************************/
362 #pragma mark Kext/OSBundle Property Deprecated Keys
363 /********************************************************************/
364 #endif
365 /*
366 * @define kOSBundleDebugLevelKey
367 * @abstract
368 * Deprecated (used on some releases of Mac OS X prior to 10.6 Snow Leopard).
369 * Value is an integer from 1-6, corresponding to the verbose levels
370 * of kext tools on those releases.
371 * On 10.6 Snow Leopard, use <code>@link OSKextEnableKextLogging
372 * OSKextEnableKextLogging@/link</code>.
373 */
374 #define kOSBundleDebugLevelKey "OSBundleDebugLevel"
375
376 /*!
377 * @define kOSBundleSharedExecutableIdentifierKey
378 * @abstract Deprecated (used on some releases of Mac OS X
379 * prior to 10.6 Snow Leopard).
380 * Value is the bundle identifier of the pseudokext
381 * that contains an executable shared by this kext.
382 */
383 #define kOSBundleSharedExecutableIdentifierKey "OSBundleSharedExecutableIdentifier"
384
385
386 #if PRAGMA_MARK
387 /********************************************************************/
388 #pragma mark Kext/OSBundle Property List Values
389 /********************************************************************/
390 #endif
391
392 /*!
393 * @group Kext Property List Values
394 * These constants encompass established values
395 * for kernel extension bundle properties.
396 */
397
398 /*!
399 * @define kOSKextKernelIdentifier
400 * @abstract
401 * This is the CFBundleIdentifier user for the kernel itself.
402 */
403 #define kOSKextKernelIdentifier "__kernel__"
404
405 /*!
406 * @define kOSBundleRequiredRoot
407 * @abstract
408 * This <code>@link kOSBundleRequiredKey OSBundleRequired@/link</code>
409 * value indicates that the kext may be needed to mount the root filesystem
410 * whether starting from a local or a network volume.
411 */
412 #define kOSBundleRequiredRoot "Root"
413
414 /*!
415 * @define kOSBundleRequiredLocalRoot
416 * @abstract
417 * This <code>@link kOSBundleRequiredKey OSBundleRequired@/link</code>
418 * value indicates that the kext may be needed to mount the root filesystem
419 * when starting from a local disk.
420 */
421 #define kOSBundleRequiredLocalRoot "Local-Root"
422
423 /*!
424 * @define kOSBundleRequiredNetworkRoot
425 * @abstract
426 * This <code>@link kOSBundleRequiredKey OSBundleRequired@/link</code>
427 * value indicates that the kext may be needed to mount the root filesystem
428 * when starting over a network connection.
429 */
430 #define kOSBundleRequiredNetworkRoot "Network-Root"
431
432 /*!
433 * @define kOSBundleRequiredSafeBoot
434 * @abstract
435 * This <code>@link kOSBundleRequiredKey OSBundleRequired@/link</code>
436 * value indicates that the kext can be loaded during a safe startup.
437 * This value does not normally cause the kext to be read by the booter
438 * or included in startup kext caches.
439 */
440 #define kOSBundleRequiredSafeBoot "Safe Boot"
441
442 /*!
443 * @define kOSBundleRequiredConsole
444 * @abstract
445 * This <code>@link kOSBundleRequiredKey OSBundleRequired@/link</code>
446 * value indicates that the kext may be needed for console access
447 * (specifically in a single-user startup when
448 * <code>@link //apple_ref/doc/man/8/kextd kextd(8)@/link</code>.
449 * does not run)
450 * and should be loaded during early startup.
451 */
452 #define kOSBundleRequiredConsole "Console"
453
454
455 #if PRAGMA_MARK
456 #pragma mark -
457 /********************************************************************/
458 #pragma mark Kext Information
459 /********************************************************************/
460 #endif
461 /*!
462 * @group Kext Information
463 * Types, constants, and macros providing a kext with information
464 * about itself.
465 */
466
467 #ifdef KERNEL
468 /*!
469 * @typedef OSKextLoadTag
470 *
471 * @abstract
472 * A unique identifier assigned to a loaded instanace of a kext.
473 *
474 * @discussion
475 * If a kext is unloaded and later reloaded, the new instance
476 * has a different load tag.
477 *
478 * A kext can get its own load tag in the <code>kmod_info_t</code>
479 * structure passed into its module start routine, as the
480 * <code>id</code> field (cast to this type).
481 * You can use the load tag with the functions
482 * <code>@link OSKextRetainKextWithLoadTag
483 * OSKextRetainKextWithLoadTag@/link</code> and
484 * <code>@link OSKextReleaseKextWithLoadTag
485 * OSKextReleaseKextWithLoadTag@/link</code>.
486 */
487 #else
488 /*!
489 * @typedef OSKextLoadTag
490 *
491 * @abstract
492 * A unique identifier assigned to a loaded instanace of a kext.
493 *
494 * @discussion
495 * If a kext is unloaded and later reloaded, the new instance
496 * has a different load tag.
497 *
498 * A kext can get its own load tag in the <code>kmod_info_t</code>
499 * structure passed into its module start routine, as the
500 * <code>id</code> field (cast to this type).
501 */
502 #endif
503 typedef uint32_t OSKextLoadTag;
504
505 /*!
506 * @define kOSKextInvalidLoadTag
507 *
508 * @abstract
509 * A load tag value that will never be used for a loaded kext;
510 * indicates kext not found.
511 */
512 #define kOSKextInvalidLoadTag ((OSKextLoadTag)(-1))
513
514 #ifdef KERNEL
515
516 /* Make these visible to kexts only and *not* the kernel.
517 */
518 #if !XNU_KERNEL_PRIVATE
519
520 /*!
521 * @function OSKextGetCurrentLoadTag
522 *
523 * @abstract
524 * Returns the run-time load tag for the calling kext as an
525 * <code>@link OSKextLoadTag OSKextLoadTag@/link</code>.
526 *
527 * @result
528 * The run-time load tag for the calling kext as an
529 * <code>@link OSKextLoadTag@/link</code>.
530 *
531 * @discussion
532 * The load tag identifies this loaded instance of the kext to the kernel
533 * and to kernel functions that operate on kexts.
534 */
535 OSKextLoadTag OSKextGetCurrentLoadTag(void);
536
537 /*!
538 * @function OSKextGetCurrentIdentifier
539 *
540 * @abstract
541 * Returns the CFBundleIdentifier for the calling kext as a C string.
542 *
543 * @result
544 * The CFBundleIdentifier for the calling kext as a C string.
545 */
546 const char * OSKextGetCurrentIdentifier(void);
547
548 /*!
549 * @function OSKextGetCurrentVersionString
550 *
551 * @abstract
552 * Returns the CFBundleVersion for the calling kext as a C string.
553 *
554 * @result
555 * The CFBundleVersion for the calling kext as a C string.
556 */
557 const char * OSKextGetCurrentVersionString(void);
558
559 #endif /* !XNU_KERNEL_PRIVATE */
560
561 #if PRAGMA_MARK
562 #pragma mark -
563 /********************************************************************/
564 #pragma mark Kext Loading C Functions
565 /********************************************************************/
566 #endif
567 /*!
568 * @group Kext Loading C Functions
569 * Functions for loading and tracking kexts in the kernel.
570 */
571
572 /*!
573 * @function OSKextLoadKextWithIdentifier
574 *
575 * @abstract
576 * Request that a kext be loaded.
577 *
578 * @param kextIdentifier The bundle identifier of the kext to be loaded.
579 *
580 * @result
581 * <code>@link //apple_ref/c/macro/kOSReturnSuccess kOSReturnSuccess@/link</code>
582 * if the kext was loaded (or was already loaded).
583 * <code>@link //apple_ref/c/macro/kOSKextReturnDeferred kOSKextReturnDeferred@/link</code>
584 * if the kext was not found and a request
585 * was queued to <code>@link //apple_ref/doc/man/8/kextd kextd(8)@/link</code>.
586 * Other return values indicate a failure to load the kext.
587 *
588 * @discussion
589 * If a kext is already in the kernel but not loaded, it is loaded immediately.
590 * If it isn't found, an asynchronous load request is
591 * made to <code>@link //apple_ref/doc/man/8/kextd kextd(8)@/link</code>
592 * and <code>@link //apple_ref/c/macro/kOSKextReturnDeferred kOSKextReturnDeferred@/link</code> is returned.
593 * There is no general notification or callback mechanism for load requests.
594 */
595 OSReturn OSKextLoadKextWithIdentifier(const char * kextIdentifier);
596
597
598 /*!
599 * @function OSKextRetainKextWithLoadTag
600 *
601 * @abstract
602 * Retain a loaded kext based on its load tag,
603 * and enable autounload for that kext.
604 *
605 * @param loadTag The load tag of the kext to be retained.
606 * See <code>@link OSKextGetCurrentLoadTag@/link</code>.
607 *
608 * @result
609 * <code>@link //apple_ref/c/macro/kOSReturnSuccess kOSReturnSuccess@/link</code>
610 * if the kext was retained.
611 * <code>@link //apple_ref/c/macro/kOSKextReturnNotFound kOSKextReturnNotFound@/link</code>
612 * if the kext was not found.
613 * <code>@link //apple_ref/c/macro/kOSKextReturnInvalidArgument
614 * kOSKextReturnInvalidArgument@/link</code>
615 * if <code>loadTag</code> is
616 * <code>@link kOSKextInvalidLoadTag kOSKextInvalidLoadTag@/link</code>.
617 *
618 * @discussion
619 * Retaining a kext prevents it from being unloaded,
620 * either explicitly or automatically, and enables autounload for the kext.
621 * When autounload is enabled, then shortly after the kext's last reference
622 * is dropped, it will be unloaded if there are no outstanding references to it
623 * and there are no instances of its Libkern C++ subclasses (if any).
624 *
625 * Kexts that define subclasses of
626 * <code>@link //apple_ref/doc/class/IOService IOService@/link</code>
627 * have autounload enabled automatically.
628 * Other kexts can use the reference count to manage automatic unload
629 * without having to define and create Libkern C++ objects.
630 * For example, a filesystem kext can retain itself whenever a new mount
631 * is created, and release itself when a mount is removed.
632 * When the last mount is removed, the kext will be unloaded after a brief delay.
633 *
634 * A kext can get its own load tag using the
635 * <code>@link OSKextGetCurrentLoadTag@/link</code>.
636 *
637 * Kexts should not retain and release other kexts; linkage references
638 * are accounted for internally.
639 */
640 OSReturn OSKextRetainKextWithLoadTag(OSKextLoadTag loadTag);
641
642
643 /*!
644 * @function OSKextReleaseKextWithLoadTag
645 *
646 * @abstract
647 * Release a loaded kext based on its load tag.
648 *
649 * @param loadTag The load tag of the kext to be released.
650 * See <code>@link OSKextGetCurrentLoadTag@/link</code>.
651 *
652 * @result
653 * <code>@link //apple_ref/c/macro/kOSReturnSuccess kOSReturnSuccess@/link</code>
654 * if the kext was released.
655 * <code>@link //apple_ref/c/macro/kOSKextReturnNotFound
656 * kOSKextReturnNotFound@/link</code>
657 * if the kext was not found.
658 * <code>@link //apple_ref/c/macro/kOSKextReturnInvalidArgument
659 * kOSKextReturnInvalidArgument@/link</code>
660 * if <code>loadTag</code> is
661 * <code>@link kOSKextInvalidLoadTag kOSKextInvalidLoadTag@/link</code>.
662 *
663 * @discussion
664 * The kext should have been retained previously via
665 * <code>@link OSKextRetainKextWithLoadTag@/link</code>.
666 *
667 * This function schedules an autounload scan for all kexts.
668 * When that scan occurs, if a kext has autounload enabled,
669 * it will be unloaded if there are no outstanding references to it
670 * and there are no instances of its Libkern C++ classes (if any).
671 *
672 * Kexts that define subclasses of
673 * <code>@link //apple_ref/doc/class/IOService IOService@/link</code>
674 * have autounload enabled automatically.
675 * Other kexts can use the reference count to manage automatic unload
676 * without having to define and create Libkern C++ objects.
677 * For example, a filesystem kext can be retained whenever a new mount
678 * is created, and released when a mount is removed.
679 * When the last mount is removed, the kext will be unloaded after a brief delay.
680 *
681 * While the autounload scan takes place after a delay of at least a minute,
682 * a kext that manages its own reference counts for autounload should
683 * be prepared to have its module stop function called even while the function
684 * calling this function is still running.
685 *
686 * A kext can get its own load tag using the
687 * <code>@link OSKextGetCurrentLoadTag@/link</code>.
688 *
689 * Kexts should not retain and release other kexts; linkage references
690 * are accounted for internally.
691 */
692 OSReturn OSKextReleaseKextWithLoadTag(OSKextLoadTag loadTag);
693
694 #if PRAGMA_MARK
695 /********************************************************************/
696 #pragma mark -
697 #pragma mark Kext Requests
698 /********************************************************************/
699 #endif
700 /*!
701 * @group Kext Requests to User Space
702 * Functions for making requests to kextd in user space.
703 */
704
705 /*!
706 * @typedef OSKextRequestTag
707 *
708 * @abstract
709 * Identifies a kext request made to user space.
710 */
711 typedef uint32_t OSKextRequestTag;
712
713 /*!
714 * @define kOSKextRequestTagInvalid
715 *
716 * @abstract
717 * A request tag value that will never be used for a kext request;
718 * indicates failure to create/queue the request.
719 */
720 #define kOSKextRequestTagInvalid ((OSKextRequestTag)-1)
721
722 /*!
723 * @typedef OSKextRequestResourceCallback
724 *
725 * @abstract
726 * Invoked to provide results for a kext resource request.
727 *
728 * @param requestTag The tag of the request that the callback pertains to.
729 * @param result The result of the request:
730 * <code>@link kOSReturnSuccess
731 * kOSReturnSuccess@/link</code>
732 * if the request was fulfilled;
733 * <code>@link kOSKextReturnTimeout
734 * kOSKextReturnTimeout@/link</code>
735 * if the request has timed out;
736 * <code>@link kOSKextReturnStopping
737 * kOSKextReturnStopping@/link</code>
738 * if the kext containing the callback
739 * address for the kext is being unloaded;
740 * or other values on error.
741 * @param resourceData A pointer to the requested resource data.
742 * Owned by the system; the kext should make a copy
743 * if it needs to keep the data past the callback.
744 * @param resourceDataLength The length of <code>resourceData</code>.
745 * @param context The context pointer originally passed to
746 * <code>@link OSKextRequestResource
747 * OSKextRequestResource@/link</code>.
748 */
749 typedef void (* OSKextRequestResourceCallback)(
750 OSKextRequestTag requestTag,
751 OSReturn result,
752 const void * resourceData,
753 uint32_t resourceDataLength,
754 void * context);
755
756 /*!
757 * @function OSKextRequestResource
758 *
759 * @abstract
760 * Requests data from a nonlocalized resource file in a kext bundle on disk.
761 *
762 * @param kextIdentifier The CFBundleIdentifier of the kext
763 * from which to read the file.
764 * @param resourceName The name of the resource file to read.
765 * @param callback A pointer to a callback function; the address
766 * must be within a currently-loaded kext.
767 * @param context A pointer to arbitrary run-time data
768 * that will be passed to the callback
769 * when it is invoked. May be <code>NULL</code>.
770 * @param requestTagOut If non-<code>NULL</code>,
771 * filled on success with a tag identifying the
772 * pending request
773 * (or on failure with <code>@link kOSKextRequestTagInvalid
774 * kOSKextRequestTagInvalid@/link</code>;
775 * can be used with
776 * <code>@link OSKextCancelRequest
777 * OSKextCancelRequest@/link</code>.
778 *
779 * @result
780 * <code>@link kOSReturnSuccess kOSReturnSuccess@/link</code>
781 * if the request is successfully queued.
782 * <code>@link kOSKextReturnInvalidArgument kOSKextReturnInvalidArgument@/link</code>
783 * if <code>kextIdentifier</code> or <code>resourceName</code> or if
784 * <code>callback</code> is not an address within a loaded kext executable.
785 * <code>@link kOSKextReturnStopping kOSKextReturnStopping@/link</code>
786 * if an unload attempt is being made
787 * on the kext containing <code>callback</code>.
788 * Other <code>OSKextReturn...</code> errors are possible.
789 *
790 * @discussion
791 * This function queues an asynchronous request to the user-space kext daemon
792 * <code>@link //apple_ref/doc/man/8/kextd kextd(8)@/link</code>;
793 * requests for resources early in system startup
794 * will not be fulfilled until that daemon starts.
795 * Requests made by a kext while that kext is loading
796 * (specifically in the kext's module start routine)
797 * will not be fulfilled until after the start routine returns and
798 * the kext is completely loaded.
799 * Kexts requesting resources should be sure to perform appropriate locking
800 * in the callback function.
801 *
802 * Kext resources are stored in the kext's on-disk bundle under the
803 * Resources subdirectory.
804 * See {@linkdoc //apple_ref/doc/uid/10000123i Bundle Programming Guide}
805 * for an overview of bundle structure.
806 * The localization context of the kext daemon
807 * (namely that of the superuser)
808 * will be used in retrieving resources;
809 * kext resources intended for use in the kernel
810 * should generally not be localized.
811 *
812 * <code>callback</code> is guaranteed to be invoked except when:
813 * <ul>
814 * <li>@link OSKextCancelRequest <code>OSKextCancelRequest</code>@/link
815 * is used to cancel the request.
816 * In this case the kext gets the <code>context</code> pointer
817 * and can clean it up.</li>
818 * <li>The request is made during a kext's module start routine
819 * and the start routine returns an error.
820 * In this case, callbacks cannot be safely invoked, so
821 * the kext should clean up all request contexts
822 * when returning the error from the start routine.</li>
823 * </ul>
824 *
825 * Kexts with pending requests are not subject to autounload,
826 * but requests are subject to timeout after a few minutes.
827 * If that amount of time passes with no response from user space,
828 * <code>callback</code> is invoked with a result of.
829 * <code>@link kOSKextReturnTimeout kOSKextReturnTimeout@/link</code>.
830 *
831 * Kexts that are explicitly unloaded have all pending request callbacks
832 * invoked with a result of
833 * <code>@link kOSKextReturnStopping kOSKextReturnStopping@/link</code>.
834 * The kext must handle these callbacks,
835 * even if its stop routine will prevent unloading.
836 * If the kext does prevent unloading, it can reissue resource requests
837 * outside of the stop function.
838 */
839 OSReturn OSKextRequestResource(
840 const char * kextIdentifier,
841 const char * resourceName,
842 OSKextRequestResourceCallback callback,
843 void * context,
844 OSKextRequestTag * requestTagOut);
845
846 /*!
847 * @function OSKextCancelRequest
848 *
849 * @abstract
850 * Cancels a pending user-space kext request without invoking the callback.
851 *
852 * @param requestTag A tag identifying a pending request.
853 * @param contextOut If non-<code>NULL</code>, filled with the context pointer
854 * originally passed with the request.
855 *
856 * @result
857 * <code>@link kOSReturnSuccess kOSReturnSuccess@/link</code>
858 * if the request is successfully canceled.
859 * <code>@link kOSKextReturnNotFound kOSKextReturnNotFound@/link</code>
860 * if <code>requestTag</code> does not identify any pending request.
861 * Other <code>OSKextReturn...</code> errors are possible.
862 *
863 * @discussion
864 * This function cancels a pending request if it exists,
865 * so that its callback will not be invoked.
866 * It returns in <code>contextOut</code>
867 * the context pointer used to create the request
868 * so that any resources allocated for the request can be cleaned up.
869 *
870 * Kexts do not need to cancel outstanding requests
871 * in their module stop functions;
872 * when a kext is unloaded, all pending request callbacks
873 * are invoked with a result of
874 * <code>@link kOSKextReturnTimeout kOSKextReturnTimeout@/link</code>
875 * before the stop function is called.
876 */
877 OSReturn OSKextCancelRequest(
878 OSKextRequestTag requestTag,
879 void ** contextOut);
880
881
882 /*!
883 * @function OSKextGrabPgoData
884 *
885 * @abstract
886 * Grab a LLVM profile data buffer from a loaded kext.
887 *
888 * @param uuid the uuid identifying the kext to retrieve data from
889 * @param pSize pointer of where to store the size of the buffer. May be NULL.
890 * @param pBuffer pointer to the output buffer. May be NULL.
891 * @param bufferSize size of the buffer pointed to by pBuffer
892 * @param wait_for_unload (boolean) sleep until the kext is unloaded
893 * @param metadata (boolean) include metadata footer
894 *
895 * @result
896 * 0 on success
897 * ENOTSUP if the kext does not have profile data to retrieve.
898 * ENOTSUP if no kext with the given UUID is found
899 * ERRORS if the provided buffer is too small
900 * EIO internal error, such as if __llvm_profile_write_buffer_internal fails
901 */
902 int
903 OSKextGrabPgoData(uuid_t uuid,
904 uint64_t *pSize,
905 char *pBuffer,
906 uint64_t bufferSize,
907 int wait_for_unload,
908 int metadata);
909
910
911
912 #if PRAGMA_MARK
913 #pragma mark -
914 /********************************************************************/
915 #pragma mark Weak linking
916 /********************************************************************/
917 #endif
918 /*!
919 * @group Weak Linking
920 * Support for weak references to symbols in kexts.
921 */
922
923 /*!
924 * @var gOSKextUnresolved
925 *
926 * @abstract
927 * The value to which a kext's unresolved, weakly-referenced symbols are bound.
928 *
929 * @discussion
930 * A kext must test a weak symbol before using it. A weak symbol
931 * is only safe to use if it is not equal to <code>gOSKextUnresolved</code>.
932 *
933 * Example for a weak symbol named <code>foo</code>:
934 * <pre>
935 * @textblock
936 * if (&foo != gOSKextUnresolved) {
937 * foo();
938 * } else {
939 * printf("foo() is not supported\n");
940 * }
941 * @/textblock
942 * </pre>
943 */
944 extern const void * gOSKextUnresolved;
945
946 /*!
947 * @define OSKextSymbolIsResolved
948 *
949 * @abstract
950 * Checks whether a weakly-referenced symbol has been resolved.
951 *
952 * @param weak_sym The weak symbol to be tested for resolution.
953 *
954 * @result
955 * <code>TRUE</code> if weak_sym is resolved, or <code>FALSE</code>
956 * if weak_sym is unresolved.
957 *
958 * @discussion
959 * This is a convenience macro for testing if weak symbols are resolved.
960 *
961 * Example for a weak symbol named <code>foo</code>:
962 * <pre>
963 * @textblock
964 * if (OSKextSymbolIsResolved(foo)) {
965 * foo();
966 * } else {
967 * printf("foo() is not resolved\n");
968 * }
969 * @/textblock
970 * </pre>
971 */
972 #define OSKextSymbolIsResolved(weak_sym) \
973 (&(weak_sym) != gOSKextUnresolved)
974
975
976 #if CONFIG_KEC_FIPS
977
978 #if PRAGMA_MARK
979 #pragma mark -
980 /********************************************************************/
981 #pragma mark Kernel External Components for FIPS compliance
982 /********************************************************************/
983 #endif
984
985 // Kernel External Components for FIPS compliance (KEC_FIPS)
986 // WARNING - ath_hash is owned by the kernel, do not free
987 typedef struct AppleTEXTHash {
988 const int ath_version; // version of this structure (value is 1)
989 int ath_length; // length of hash data
990 void * ath_hash; // hash extracted from AppleTextHashes dict
991 } AppleTEXTHash_t;
992 #endif // CONFIG_KEC_FIPS
993
994 #endif /* KERNEL */
995
996 __END_DECLS
997
998 #endif /* _LIBKERN_OSKEXTLIB_H */