2 * Copyright (c) 2008-2012 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@
30 #include <libkern/OSKextLibPrivate.h>
31 #include <libkern/mkext.h>
34 #include <libkern/c++/OSContainers.h>
35 #include <libkern/c++/OSKext.h>
36 #include <libkern/OSKextLib.h>
37 #include <libkern/OSKextLibPrivate.h>
42 #pragma mark C-based kext interface (loading/loaded kexts only)
44 /*********************************************************************
45 *********************************************************************/
46 kern_return_t
OSKextLoadKextWithIdentifier(const char * bundle_id
)
48 return OSKext::loadKextWithIdentifier(bundle_id
);
51 /*********************************************************************
52 *********************************************************************/
54 OSKextGetLoadTagForIdentifier(const char * kextIdentifier
)
56 uint32_t result
= kOSKextInvalidLoadTag
;
57 OSKext
* theKext
= NULL
; // must release
59 if (!kextIdentifier
) {
63 theKext
= OSKext::lookupKextWithIdentifier(kextIdentifier
);
64 if (theKext
&& theKext
->isLoaded()) {
65 result
= theKext
->getLoadTag();
68 if (theKext
) theKext
->release();
72 /*********************************************************************
73 *********************************************************************/
74 OSReturn
OSKextRetainKextWithLoadTag(uint32_t loadTag
)
76 OSReturn result
= kOSKextReturnNotFound
;
77 OSKext
* theKext
= NULL
; // do not release; as this function is a retain
79 if (loadTag
== kOSKextInvalidLoadTag
) {
80 result
= kOSKextReturnInvalidArgument
;
83 theKext
= OSKext::lookupKextWithLoadTag(loadTag
);
85 result
= kOSReturnSuccess
;
88 kOSKextLogDebugLevel
|
89 kOSKextLogKextBookkeepingFlag
,
90 "Kext %s (load tag %d) has been retained.",
91 theKext
->getIdentifierCString(),
94 /* Call this after so a log message about autounload comes second.
96 theKext
->setAutounloadEnabled(true);
99 kOSKextLogErrorLevel
|
100 kOSKextLogKextBookkeepingFlag
,
101 "Can't retain kext with load tag %d - no such kext is loaded.",
108 /*********************************************************************
109 *********************************************************************/
110 OSReturn
OSKextReleaseKextWithLoadTag(uint32_t loadTag
)
112 OSReturn result
= kOSKextReturnNotFound
;
113 OSKext
* theKext
= NULL
; // must release twice!
115 if (loadTag
== kOSKextInvalidLoadTag
) {
116 result
= kOSKextReturnInvalidArgument
;
119 theKext
= OSKext::lookupKextWithLoadTag(loadTag
);
121 result
= kOSReturnSuccess
;
122 OSKext::considerUnloads(); // schedule autounload pass
123 theKext
->release(); // do the release the caller wants
124 theKext
->release(); // now do the release on the lookup
126 kOSKextLogDebugLevel
|
127 kOSKextLogKextBookkeepingFlag
,
128 "Kext %s (load tag %d) has been released.",
129 theKext
->getIdentifierCString(),
133 kOSKextLogErrorLevel
|
134 kOSKextLogKextBookkeepingFlag
,
135 "Can't release kext with load tag %d - no such kext is loaded.",
139 // xxx - should I check that the refcount of the OSKext is above the lower bound?
140 // xxx - do we want a OSKextGetRetainCountOfKextWithLoadTag()?
145 /*********************************************************************
146 * Not to be called by the kext being unloaded!
147 *********************************************************************/
148 OSReturn
OSKextUnloadKextWithLoadTag(uint32_t loadTag
)
150 return OSKext::removeKextWithLoadTag(loadTag
,
151 /* terminateServicesAndRemovePersonalitiesFlag */ false);
156 #pragma mark Kext Requests
158 /*********************************************************************
160 *********************************************************************/
161 OSReturn
OSKextRequestResource(
162 const char * kextIdentifier
,
163 const char * resourceName
,
164 OSKextRequestResourceCallback callback
,
166 OSKextRequestTag
* requestTagOut
)
168 return OSKext::requestResource(kextIdentifier
, resourceName
,
169 callback
, context
, requestTagOut
);
172 /*********************************************************************
173 *********************************************************************/
174 OSReturn
OSKextCancelRequest(
175 OSKextRequestTag requestTag
,
178 return OSKext::cancelRequest(requestTag
, contextOut
);
182 #pragma mark MIG Functions & Wrappers
184 /*********************************************************************
185 * IMPORTANT: Once we have done the vm_map_copyout(), we *must* return
186 * KERN_SUCCESS or the kernel map gets messed up (reason as yet
187 * unknown). We use op_result to return the real result of our work.
188 *********************************************************************/
189 kern_return_t
kext_request(
190 host_priv_t hostPriv
,
191 /* in only */ uint32_t clientLogSpec
,
192 /* in only */ vm_offset_t requestIn
,
193 /* in only */ mach_msg_type_number_t requestLengthIn
,
194 /* out only */ vm_offset_t
* responseOut
,
195 /* out only */ mach_msg_type_number_t
* responseLengthOut
,
196 /* out only */ vm_offset_t
* logDataOut
,
197 /* out only */ mach_msg_type_number_t
* logDataLengthOut
,
198 /* out only */ kern_return_t
* op_result
)
200 kern_return_t result
= KERN_FAILURE
;
201 vm_map_address_t map_addr
= 0; // do not free/deallocate
202 char * request
= NULL
; // must vm_deallocate
204 mkext2_header
* mkextHeader
= NULL
; // do not release
205 bool isMkext
= false;
207 char * response
= NULL
; // must kmem_free
208 uint32_t responseLength
= 0;
209 char * logData
= NULL
; // must kmem_free
210 uint32_t logDataLength
= 0;
212 /* MIG doesn't pass "out" parameters as empty, so clear them immediately
213 * just in case, or MIG will try to copy out bogus data.
215 *op_result
= KERN_FAILURE
;
217 *responseLengthOut
= 0;
219 *logDataLengthOut
= 0;
221 /* Check for input. Don't discard what isn't there, though.
223 if (!requestLengthIn
|| !requestIn
) {
224 OSKextLog(/* kext */ NULL
,
225 kOSKextLogErrorLevel
|
227 "Invalid request from user space (no data).");
228 *op_result
= KERN_INVALID_ARGUMENT
;
232 /* Once we have done the vm_map_copyout(), we *must* return KERN_SUCCESS
233 * or the kernel map gets messed up (reason as yet unknown). We will use
234 * op_result to return the real result of our work.
236 result
= vm_map_copyout(kernel_map
, &map_addr
, (vm_map_copy_t
)requestIn
);
237 if (result
!= KERN_SUCCESS
) {
238 OSKextLog(/* kext */ NULL
,
239 kOSKextLogErrorLevel
|
241 "vm_map_copyout() failed for request from user space.");
242 vm_map_copy_discard((vm_map_copy_t
)requestIn
);
245 request
= CAST_DOWN(char *, map_addr
);
247 /* Check if request is an mkext; this is always a load request
248 * and requires root access. If it isn't an mkext, see if it's
249 * an XML request, and check the request to see if that requires
252 if (requestLengthIn
> sizeof(mkext2_header
)) {
253 mkextHeader
= (mkext2_header
*)request
;
254 if (MKEXT_GET_MAGIC(mkextHeader
) == MKEXT_MAGIC
&&
255 MKEXT_GET_SIGNATURE(mkextHeader
) == MKEXT_SIGN
) {
263 // xxx - something tells me if we have a secure kernel we don't even
264 // xxx - want to log a message here. :-)
265 *op_result
= KERN_NOT_SUPPORTED
;
268 // xxx - can we find out if calling task is kextd?
269 // xxx - can we find the name of the calling task?
270 if (hostPriv
== HOST_PRIV_NULL
) {
271 OSKextLog(/* kext */ NULL
,
272 kOSKextLogErrorLevel
|
273 kOSKextLogLoadFlag
| kOSKextLogIPCFlag
,
274 "Attempt by non-root process to load a kext.");
275 *op_result
= kOSKextReturnNotPrivileged
;
279 *op_result
= OSKext::loadFromMkext((OSKextLogSpec
)clientLogSpec
,
280 request
, requestLengthIn
,
281 &logData
, &logDataLength
);
283 #endif /* defined(SECURE_KERNEL) */
287 /* If the request isn't an mkext, then is should be XML. Parse it
288 * if possible and hand the request over to OSKext.
290 *op_result
= OSKext::handleRequest(hostPriv
,
291 (OSKextLogSpec
)clientLogSpec
,
292 request
, requestLengthIn
,
293 &response
, &responseLength
,
294 &logData
, &logDataLength
);
297 if (response
&& responseLength
> 0) {
298 kern_return_t copyin_result
;
300 copyin_result
= vm_map_copyin(kernel_map
,
301 CAST_USER_ADDR_T(response
), responseLength
,
302 /* src_destroy */ false, (vm_map_copy_t
*)responseOut
);
303 if (copyin_result
== KERN_SUCCESS
) {
304 *responseLengthOut
= responseLength
;
306 OSKextLog(/* kext */ NULL
,
307 kOSKextLogErrorLevel
|
309 "Failed to copy response to request from user space.");
310 *op_result
= copyin_result
; // xxx - should we map to our own code?
312 *responseLengthOut
= 0;
317 if (logData
&& logDataLength
> 0) {
318 kern_return_t copyin_result
;
320 copyin_result
= vm_map_copyin(kernel_map
,
321 CAST_USER_ADDR_T(logData
), logDataLength
,
322 /* src_destroy */ false, (vm_map_copy_t
*)logDataOut
);
323 if (copyin_result
== KERN_SUCCESS
) {
324 *logDataLengthOut
= logDataLength
;
326 OSKextLog(/* kext */ NULL
,
327 kOSKextLogErrorLevel
|
329 "Failed to copy log data for request from user space.");
330 *op_result
= copyin_result
; // xxx - should we map to our own code?
332 *logDataLengthOut
= 0;
339 (void)vm_deallocate(kernel_map
, (vm_offset_t
)request
, requestLengthIn
);
342 /* 11981737 - clear uninitialized data in last page */
343 kmem_free(kernel_map
, (vm_offset_t
)response
, round_page(responseLength
));
346 /* 11981737 - clear uninitialized data in last page */
347 kmem_free(kernel_map
, (vm_offset_t
)logData
, round_page(logDataLength
));
353 /*********************************************************************
354 * Gets the vm_map for the current kext
355 *********************************************************************/
356 extern vm_offset_t segPRELINKB
;
357 extern unsigned long segSizePRELINK
;
358 extern int kth_started
;
359 extern vm_map_t g_kext_map
;
362 kext_get_vm_map(kmod_info_t
*info
)
364 vm_map_t kext_map
= NULL
;
367 if ((info
->address
>= segPRELINKB
) &&
368 (info
->address
< (segPRELINKB
+ segSizePRELINK
)))
370 kext_map
= kernel_map
;
372 kext_map
= g_kext_map
;
380 /********************************************************************/
381 #pragma mark Weak linking support
382 /********************************************************************/
385 kext_weak_symbol_referenced(void)
387 panic("A kext referenced an unresolved weak symbol\n");
390 const void *gOSKextUnresolved
= (const void *)&kext_weak_symbol_referenced
;
393 #pragma mark Kernel-Internal C Functions
395 /*********************************************************************
396 * Called from startup.c.
397 *********************************************************************/
398 void OSKextRemoveKextBootstrap(void)
400 OSKext::removeKextBootstrap();
405 /*********************************************************************
406 *********************************************************************/
407 void OSKextRegisterKextsWithDTrace(void)
409 OSKext::registerKextsWithDTrace();
412 #endif /* CONFIG_DTRACE */
414 /*********************************************************************
415 *********************************************************************/
416 void kext_dump_panic_lists(int (*printf_func
)(const char * fmt
, ...))
418 OSKext::printKextPanicLists(printf_func
);
423 #pragma mark Kmod Compatibility Functions
425 /*********************************************************************
426 **********************************************************************
427 * KMOD COMPATIBILITY FUNCTIONS *
428 * (formerly in kmod.c, or C++ bridges from) *
429 **********************************************************************
430 **********************************************************************
431 * These two functions are used in various places in the kernel, but
432 * are not exported. We might rename them at some point to start with
435 * kmod_panic_dump() must not be called outside of a panic context.
436 * kmod_dump_log() must not be called in a panic context.
437 *********************************************************************/
439 kmod_panic_dump(vm_offset_t
* addr
, unsigned int cnt
)
441 extern int kdb_printf(const char *format
, ...) __printflike(1,2);
443 OSKext::printKextsInBacktrace(addr
, cnt
, &kdb_printf
,
444 /* takeLock? */ false);
448 /********************************************************************/
449 void kmod_dump_log(vm_offset_t
*addr
, unsigned int cnt
);
456 OSKext::printKextsInBacktrace(addr
, cnt
, &printf
, /* lock? */ true);
459 /*********************************************************************
460 * Compatibility implementation for kmod_get_info() host_priv routine.
461 * Only supported on old 32-bit architectures.
462 *********************************************************************/
465 #pragma mark Loaded Kext Summary
469 OSKextLoadedKextSummariesUpdated(void)