2 * Copyright (c) 2008-2016 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 uint32_t OSKextGetLoadTagForIdentifier(const char * kextIdentifier
);
52 /*********************************************************************
53 *********************************************************************/
55 OSKextGetLoadTagForIdentifier(const char * kextIdentifier
)
57 uint32_t result
= kOSKextInvalidLoadTag
;
58 OSKext
* theKext
= NULL
; // must release
60 if (!kextIdentifier
) {
64 theKext
= OSKext::lookupKextWithIdentifier(kextIdentifier
);
65 if (theKext
&& theKext
->isLoaded()) {
66 result
= theKext
->getLoadTag();
69 if (theKext
) theKext
->release();
73 /*********************************************************************
74 *********************************************************************/
75 OSReturn
OSKextRetainKextWithLoadTag(uint32_t loadTag
)
77 OSReturn result
= kOSKextReturnNotFound
;
78 OSKext
* theKext
= NULL
; // do not release; as this function is a retain
80 if (loadTag
== kOSKextInvalidLoadTag
) {
81 result
= kOSKextReturnInvalidArgument
;
84 theKext
= OSKext::lookupKextWithLoadTag(loadTag
);
86 result
= kOSReturnSuccess
;
89 kOSKextLogDebugLevel
|
90 kOSKextLogKextBookkeepingFlag
,
91 "Kext %s (load tag %d) has been retained.",
92 theKext
->getIdentifierCString(),
95 /* Call this after so a log message about autounload comes second.
97 theKext
->setAutounloadEnabled(true);
100 kOSKextLogErrorLevel
|
101 kOSKextLogKextBookkeepingFlag
,
102 "Can't retain kext with load tag %d - no such kext is loaded.",
109 /*********************************************************************
110 *********************************************************************/
111 OSReturn
OSKextReleaseKextWithLoadTag(uint32_t loadTag
)
113 OSReturn result
= kOSKextReturnNotFound
;
114 OSKext
* theKext
= NULL
; // must release twice!
116 if (loadTag
== kOSKextInvalidLoadTag
) {
117 result
= kOSKextReturnInvalidArgument
;
120 theKext
= OSKext::lookupKextWithLoadTag(loadTag
);
122 result
= kOSReturnSuccess
;
123 OSKext::considerUnloads(); // schedule autounload pass
124 theKext
->release(); // do the release the caller wants
125 theKext
->release(); // now do the release on the lookup
127 kOSKextLogDebugLevel
|
128 kOSKextLogKextBookkeepingFlag
,
129 "Kext %s (load tag %d) has been released.",
130 theKext
->getIdentifierCString(),
134 kOSKextLogErrorLevel
|
135 kOSKextLogKextBookkeepingFlag
,
136 "Can't release kext with load tag %d - no such kext is loaded.",
140 // xxx - should I check that the refcount of the OSKext is above the lower bound?
141 // xxx - do we want a OSKextGetRetainCountOfKextWithLoadTag()?
146 /*********************************************************************
147 * Not to be called by the kext being unloaded!
148 *********************************************************************/
149 OSReturn
OSKextUnloadKextWithLoadTag(uint32_t loadTag
)
151 return OSKext::removeKextWithLoadTag(loadTag
,
152 /* terminateServicesAndRemovePersonalitiesFlag */ false);
157 #pragma mark Kext Requests
159 /*********************************************************************
161 *********************************************************************/
162 OSReturn
OSKextRequestResource(
163 const char * kextIdentifier
,
164 const char * resourceName
,
165 OSKextRequestResourceCallback callback
,
167 OSKextRequestTag
* requestTagOut
)
169 return OSKext::requestResource(kextIdentifier
, resourceName
,
170 callback
, context
, requestTagOut
);
173 /*********************************************************************
174 *********************************************************************/
175 OSReturn
OSKextCancelRequest(
176 OSKextRequestTag requestTag
,
179 return OSKext::cancelRequest(requestTag
, contextOut
);
183 #pragma mark MIG Functions & Wrappers
185 /*********************************************************************
186 * IMPORTANT: Once we have done the vm_map_copyout(), we *must* return
187 * KERN_SUCCESS or the kernel map gets messed up (reason as yet
188 * unknown). We use op_result to return the real result of our work.
189 *********************************************************************/
190 kern_return_t
kext_request(
191 host_priv_t hostPriv
,
192 /* in only */ uint32_t clientLogSpec
,
193 /* in only */ vm_offset_t requestIn
,
194 /* in only */ mach_msg_type_number_t requestLengthIn
,
195 /* out only */ vm_offset_t
* responseOut
,
196 /* out only */ mach_msg_type_number_t
* responseLengthOut
,
197 /* out only */ vm_offset_t
* logDataOut
,
198 /* out only */ mach_msg_type_number_t
* logDataLengthOut
,
199 /* out only */ kern_return_t
* op_result
)
201 kern_return_t result
= KERN_FAILURE
;
202 vm_map_address_t map_addr
= 0; // do not free/deallocate
203 char * request
= NULL
; // must vm_deallocate
205 mkext2_header
* mkextHeader
= NULL
; // do not release
206 bool isMkext
= false;
208 char * response
= NULL
; // must kmem_free
209 uint32_t responseLength
= 0;
210 char * logData
= NULL
; // must kmem_free
211 uint32_t logDataLength
= 0;
213 /* MIG doesn't pass "out" parameters as empty, so clear them immediately
214 * just in case, or MIG will try to copy out bogus data.
216 *op_result
= KERN_FAILURE
;
218 *responseLengthOut
= 0;
220 *logDataLengthOut
= 0;
222 /* Check for input. Don't discard what isn't there, though.
224 if (!requestLengthIn
|| !requestIn
) {
225 OSKextLog(/* kext */ NULL
,
226 kOSKextLogErrorLevel
|
228 "Invalid request from user space (no data).");
229 *op_result
= KERN_INVALID_ARGUMENT
;
233 /* Once we have done the vm_map_copyout(), we *must* return KERN_SUCCESS
234 * or the kernel map gets messed up (reason as yet unknown). We will use
235 * op_result to return the real result of our work.
237 result
= vm_map_copyout(kernel_map
, &map_addr
, (vm_map_copy_t
)requestIn
);
238 if (result
!= KERN_SUCCESS
) {
239 OSKextLog(/* kext */ NULL
,
240 kOSKextLogErrorLevel
|
242 "vm_map_copyout() failed for request from user space.");
243 vm_map_copy_discard((vm_map_copy_t
)requestIn
);
246 request
= CAST_DOWN(char *, map_addr
);
248 /* Check if request is an mkext; this is always a load request
249 * and requires root access. If it isn't an mkext, see if it's
250 * an XML request, and check the request to see if that requires
253 if (requestLengthIn
> sizeof(mkext2_header
)) {
254 mkextHeader
= (mkext2_header
*)request
;
255 if (MKEXT_GET_MAGIC(mkextHeader
) == MKEXT_MAGIC
&&
256 MKEXT_GET_SIGNATURE(mkextHeader
) == MKEXT_SIGN
) {
264 // xxx - something tells me if we have a secure kernel we don't even
265 // xxx - want to log a message here. :-)
266 *op_result
= KERN_NOT_SUPPORTED
;
269 // xxx - can we find out if calling task is kextd?
270 // xxx - can we find the name of the calling task?
271 if (hostPriv
== HOST_PRIV_NULL
) {
272 OSKextLog(/* kext */ NULL
,
273 kOSKextLogErrorLevel
|
274 kOSKextLogLoadFlag
| kOSKextLogIPCFlag
,
275 "Attempt by non-root process to load a kext.");
276 *op_result
= kOSKextReturnNotPrivileged
;
280 *op_result
= OSKext::loadFromMkext((OSKextLogSpec
)clientLogSpec
,
281 request
, requestLengthIn
,
282 &logData
, &logDataLength
);
284 #endif /* defined(SECURE_KERNEL) */
288 /* If the request isn't an mkext, then is should be XML. Parse it
289 * if possible and hand the request over to OSKext.
291 *op_result
= OSKext::handleRequest(hostPriv
,
292 (OSKextLogSpec
)clientLogSpec
,
293 request
, requestLengthIn
,
294 &response
, &responseLength
,
295 &logData
, &logDataLength
);
298 if (response
&& responseLength
> 0) {
299 kern_return_t copyin_result
;
301 copyin_result
= vm_map_copyin(kernel_map
,
302 CAST_USER_ADDR_T(response
), responseLength
,
303 /* src_destroy */ false, (vm_map_copy_t
*)responseOut
);
304 if (copyin_result
== KERN_SUCCESS
) {
305 *responseLengthOut
= responseLength
;
307 OSKextLog(/* kext */ NULL
,
308 kOSKextLogErrorLevel
|
310 "Failed to copy response to request from user space.");
311 *op_result
= copyin_result
; // xxx - should we map to our own code?
313 *responseLengthOut
= 0;
318 if (logData
&& logDataLength
> 0) {
319 kern_return_t copyin_result
;
321 copyin_result
= vm_map_copyin(kernel_map
,
322 CAST_USER_ADDR_T(logData
), logDataLength
,
323 /* src_destroy */ false, (vm_map_copy_t
*)logDataOut
);
324 if (copyin_result
== KERN_SUCCESS
) {
325 *logDataLengthOut
= logDataLength
;
327 OSKextLog(/* kext */ NULL
,
328 kOSKextLogErrorLevel
|
330 "Failed to copy log data for request from user space.");
331 *op_result
= copyin_result
; // xxx - should we map to our own code?
333 *logDataLengthOut
= 0;
340 (void)vm_deallocate(kernel_map
, (vm_offset_t
)request
, requestLengthIn
);
343 /* 11981737 - clear uninitialized data in last page */
344 kmem_free(kernel_map
, (vm_offset_t
)response
, round_page(responseLength
));
347 /* 11981737 - clear uninitialized data in last page */
348 kmem_free(kernel_map
, (vm_offset_t
)logData
, round_page(logDataLength
));
354 /*********************************************************************
355 * Gets the vm_map for the current kext
356 *********************************************************************/
357 extern vm_offset_t segPRELINKTEXTB
;
358 extern unsigned long segSizePRELINKTEXT
;
359 extern int kth_started
;
360 extern vm_map_t g_kext_map
;
363 kext_get_vm_map(kmod_info_t
*info
)
365 vm_map_t kext_map
= NULL
;
368 if ((info
->address
>= segPRELINKTEXTB
) &&
369 (info
->address
< (segPRELINKTEXTB
+ segSizePRELINKTEXT
)))
371 kext_map
= kernel_map
;
373 kext_map
= g_kext_map
;
381 /********************************************************************/
382 #pragma mark Weak linking support
383 /********************************************************************/
386 kext_weak_symbol_referenced(void)
388 panic("A kext referenced an unresolved weak symbol\n");
391 const void *gOSKextUnresolved
= (const void *)&kext_weak_symbol_referenced
;
394 #pragma mark Kernel-Internal C Functions
396 /*********************************************************************
397 * Called from startup.c.
398 *********************************************************************/
399 void OSKextRemoveKextBootstrap(void)
401 OSKext::removeKextBootstrap();
406 /*********************************************************************
407 *********************************************************************/
408 void OSKextRegisterKextsWithDTrace(void)
410 OSKext::registerKextsWithDTrace();
413 #endif /* CONFIG_DTRACE */
415 /*********************************************************************
416 *********************************************************************/
417 void kext_dump_panic_lists(int (*printf_func
)(const char * fmt
, ...))
419 OSKext::printKextPanicLists(printf_func
);
424 #pragma mark Kmod Compatibility Functions
426 /*********************************************************************
427 **********************************************************************
428 * KMOD COMPATIBILITY FUNCTIONS *
429 * (formerly in kmod.c, or C++ bridges from) *
430 **********************************************************************
431 **********************************************************************
432 * These two functions are used in various places in the kernel, but
433 * are not exported. We might rename them at some point to start with
436 * kmod_panic_dump() must not be called outside of a panic context.
437 * kmod_dump_log() must not be called in a panic context.
438 *********************************************************************/
440 kmod_panic_dump(vm_offset_t
* addr
, unsigned int cnt
)
442 extern int kdb_printf(const char *format
, ...) __printflike(1,2);
444 OSKext::printKextsInBacktrace(addr
, cnt
, &kdb_printf
,
445 /* takeLock? */ false, false);
449 /********************************************************************/
450 void kmod_dump_log(vm_offset_t
*addr
, unsigned int cnt
, boolean_t doUnslide
);
458 OSKext::printKextsInBacktrace(addr
, cnt
, &printf
, /* lock? */ true, doUnslide
);
462 OSKextKextForAddress(const void *addr
)
464 return OSKext::kextForAddress(addr
);
468 /*********************************************************************
469 * Compatibility implementation for kmod_get_info() host_priv routine.
470 * Only supported on old 32-bit architectures.
471 *********************************************************************/
474 #pragma mark Loaded Kext Summary
478 OSKextLoadedKextSummariesUpdated(void)