1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions * Copyright (c) 2008-2016 Apple Inc. All rights reserved.
3*c54f35caSApple OSS Distributions *
4*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions *
6*c54f35caSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions *
15*c54f35caSApple OSS Distributions * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions *
18*c54f35caSApple OSS Distributions * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions * limitations under the License.
25*c54f35caSApple OSS Distributions *
26*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions */
28*c54f35caSApple OSS Distributions
29*c54f35caSApple OSS Distributions extern "C" {
30*c54f35caSApple OSS Distributions #include <libkern/OSKextLibPrivate.h>
31*c54f35caSApple OSS Distributions #include <libkern/mkext.h>
32*c54f35caSApple OSS Distributions };
33*c54f35caSApple OSS Distributions
34*c54f35caSApple OSS Distributions #include <libkern/c++/OSContainers.h>
35*c54f35caSApple OSS Distributions #include <libkern/c++/OSKext.h>
36*c54f35caSApple OSS Distributions #include <libkern/OSKextLib.h>
37*c54f35caSApple OSS Distributions #include <libkern/OSKextLibPrivate.h>
38*c54f35caSApple OSS Distributions
39*c54f35caSApple OSS Distributions extern "C" {
40*c54f35caSApple OSS Distributions #if PRAGMA_MARK
41*c54f35caSApple OSS Distributions #pragma mark C-based kext interface (loading/loaded kexts only)
42*c54f35caSApple OSS Distributions #endif
43*c54f35caSApple OSS Distributions /*********************************************************************
44*c54f35caSApple OSS Distributions *********************************************************************/
45*c54f35caSApple OSS Distributions kern_return_t
OSKextLoadKextWithIdentifier(const char * bundle_id)46*c54f35caSApple OSS Distributions OSKextLoadKextWithIdentifier(const char * bundle_id)
47*c54f35caSApple OSS Distributions {
48*c54f35caSApple OSS Distributions return OSKext::loadKextWithIdentifier(bundle_id);
49*c54f35caSApple OSS Distributions }
50*c54f35caSApple OSS Distributions
51*c54f35caSApple OSS Distributions uint32_t OSKextGetLoadTagForIdentifier(const char * kextIdentifier);
52*c54f35caSApple OSS Distributions /*********************************************************************
53*c54f35caSApple OSS Distributions *********************************************************************/
54*c54f35caSApple OSS Distributions uint32_t
OSKextGetLoadTagForIdentifier(const char * kextIdentifier)55*c54f35caSApple OSS Distributions OSKextGetLoadTagForIdentifier(const char * kextIdentifier)
56*c54f35caSApple OSS Distributions {
57*c54f35caSApple OSS Distributions uint32_t result = kOSKextInvalidLoadTag;
58*c54f35caSApple OSS Distributions OSKext * theKext = NULL; // must release
59*c54f35caSApple OSS Distributions
60*c54f35caSApple OSS Distributions if (!kextIdentifier) {
61*c54f35caSApple OSS Distributions goto finish;
62*c54f35caSApple OSS Distributions }
63*c54f35caSApple OSS Distributions
64*c54f35caSApple OSS Distributions theKext = OSKext::lookupKextWithIdentifier(kextIdentifier);
65*c54f35caSApple OSS Distributions if (theKext && theKext->isLoaded()) {
66*c54f35caSApple OSS Distributions result = theKext->getLoadTag();
67*c54f35caSApple OSS Distributions }
68*c54f35caSApple OSS Distributions finish:
69*c54f35caSApple OSS Distributions if (theKext) {
70*c54f35caSApple OSS Distributions theKext->release();
71*c54f35caSApple OSS Distributions }
72*c54f35caSApple OSS Distributions return result;
73*c54f35caSApple OSS Distributions }
74*c54f35caSApple OSS Distributions
75*c54f35caSApple OSS Distributions /*********************************************************************
76*c54f35caSApple OSS Distributions *********************************************************************/
77*c54f35caSApple OSS Distributions
78*c54f35caSApple OSS Distributions // FIXME: Implementation of this function is hidden from the static analyzer.
79*c54f35caSApple OSS Distributions // The analyzer is worried about the lack of release and suggests
80*c54f35caSApple OSS Distributions // refactoring the code into the typical non-owning container pattern.
81*c54f35caSApple OSS Distributions // Feel free to remove the #ifndef and address the warning!
82*c54f35caSApple OSS Distributions #ifndef __clang_analyzer__
83*c54f35caSApple OSS Distributions OSReturn
OSKextRetainKextWithLoadTag(uint32_t loadTag)84*c54f35caSApple OSS Distributions OSKextRetainKextWithLoadTag(uint32_t loadTag)
85*c54f35caSApple OSS Distributions {
86*c54f35caSApple OSS Distributions OSReturn result = kOSKextReturnNotFound;
87*c54f35caSApple OSS Distributions OSKext * theKext = NULL;// do not release; as this function is a retain
88*c54f35caSApple OSS Distributions
89*c54f35caSApple OSS Distributions if (loadTag == kOSKextInvalidLoadTag) {
90*c54f35caSApple OSS Distributions result = kOSKextReturnInvalidArgument;
91*c54f35caSApple OSS Distributions goto finish;
92*c54f35caSApple OSS Distributions }
93*c54f35caSApple OSS Distributions theKext = OSKext::lookupKextWithLoadTag(loadTag);
94*c54f35caSApple OSS Distributions if (theKext) {
95*c54f35caSApple OSS Distributions result = kOSReturnSuccess;
96*c54f35caSApple OSS Distributions
97*c54f35caSApple OSS Distributions OSKextLog(theKext,
98*c54f35caSApple OSS Distributions kOSKextLogDebugLevel |
99*c54f35caSApple OSS Distributions kOSKextLogKextBookkeepingFlag,
100*c54f35caSApple OSS Distributions "Kext %s (load tag %d) has been retained.",
101*c54f35caSApple OSS Distributions theKext->getIdentifierCString(),
102*c54f35caSApple OSS Distributions loadTag);
103*c54f35caSApple OSS Distributions
104*c54f35caSApple OSS Distributions /* Call this after so a log message about autounload comes second.
105*c54f35caSApple OSS Distributions */
106*c54f35caSApple OSS Distributions theKext->setAutounloadEnabled(true);
107*c54f35caSApple OSS Distributions } else {
108*c54f35caSApple OSS Distributions OSKextLog(theKext,
109*c54f35caSApple OSS Distributions kOSKextLogErrorLevel |
110*c54f35caSApple OSS Distributions kOSKextLogKextBookkeepingFlag,
111*c54f35caSApple OSS Distributions "Can't retain kext with load tag %d - no such kext is loaded.",
112*c54f35caSApple OSS Distributions loadTag);
113*c54f35caSApple OSS Distributions }
114*c54f35caSApple OSS Distributions finish:
115*c54f35caSApple OSS Distributions return result;
116*c54f35caSApple OSS Distributions }
117*c54f35caSApple OSS Distributions #endif // __clang_analyzer__
118*c54f35caSApple OSS Distributions
119*c54f35caSApple OSS Distributions /*********************************************************************
120*c54f35caSApple OSS Distributions *********************************************************************/
121*c54f35caSApple OSS Distributions
122*c54f35caSApple OSS Distributions // FIXME: Implementation of this function is hidden from the static analyzer.
123*c54f35caSApple OSS Distributions // The analyzer is worried about the double release and suggests
124*c54f35caSApple OSS Distributions // refactoring the code into the typical non-owning container pattern.
125*c54f35caSApple OSS Distributions // Feel free to remove the #ifndef and address the warning!
126*c54f35caSApple OSS Distributions #ifndef __clang_analyzer__
127*c54f35caSApple OSS Distributions OSReturn
OSKextReleaseKextWithLoadTag(uint32_t loadTag)128*c54f35caSApple OSS Distributions OSKextReleaseKextWithLoadTag(uint32_t loadTag)
129*c54f35caSApple OSS Distributions {
130*c54f35caSApple OSS Distributions OSReturn result = kOSKextReturnNotFound;
131*c54f35caSApple OSS Distributions OSKext * theKext = NULL; // must release twice!
132*c54f35caSApple OSS Distributions
133*c54f35caSApple OSS Distributions if (loadTag == kOSKextInvalidLoadTag) {
134*c54f35caSApple OSS Distributions result = kOSKextReturnInvalidArgument;
135*c54f35caSApple OSS Distributions goto finish;
136*c54f35caSApple OSS Distributions }
137*c54f35caSApple OSS Distributions theKext = OSKext::lookupKextWithLoadTag(loadTag);
138*c54f35caSApple OSS Distributions if (theKext) {
139*c54f35caSApple OSS Distributions result = kOSReturnSuccess;
140*c54f35caSApple OSS Distributions OSKext::considerUnloads(); // schedule autounload pass
141*c54f35caSApple OSS Distributions theKext->release(); // do the release the caller wants
142*c54f35caSApple OSS Distributions theKext->release(); // now do the release on the lookup
143*c54f35caSApple OSS Distributions OSKextLog(theKext,
144*c54f35caSApple OSS Distributions kOSKextLogDebugLevel |
145*c54f35caSApple OSS Distributions kOSKextLogKextBookkeepingFlag,
146*c54f35caSApple OSS Distributions "Kext %s (load tag %d) has been released.",
147*c54f35caSApple OSS Distributions theKext->getIdentifierCString(),
148*c54f35caSApple OSS Distributions loadTag);
149*c54f35caSApple OSS Distributions } else {
150*c54f35caSApple OSS Distributions OSKextLog(theKext,
151*c54f35caSApple OSS Distributions kOSKextLogErrorLevel |
152*c54f35caSApple OSS Distributions kOSKextLogKextBookkeepingFlag,
153*c54f35caSApple OSS Distributions "Can't release kext with load tag %d - no such kext is loaded.",
154*c54f35caSApple OSS Distributions loadTag);
155*c54f35caSApple OSS Distributions }
156*c54f35caSApple OSS Distributions
157*c54f35caSApple OSS Distributions // xxx - should I check that the refcount of the OSKext is above the lower bound?
158*c54f35caSApple OSS Distributions // xxx - do we want a OSKextGetRetainCountOfKextWithLoadTag()?
159*c54f35caSApple OSS Distributions finish:
160*c54f35caSApple OSS Distributions return result;
161*c54f35caSApple OSS Distributions }
162*c54f35caSApple OSS Distributions #endif // __clang_analyzer__
163*c54f35caSApple OSS Distributions
164*c54f35caSApple OSS Distributions /*********************************************************************
165*c54f35caSApple OSS Distributions * Not to be called by the kext being unloaded!
166*c54f35caSApple OSS Distributions *********************************************************************/
167*c54f35caSApple OSS Distributions OSReturn
OSKextUnloadKextWithLoadTag(uint32_t loadTag)168*c54f35caSApple OSS Distributions OSKextUnloadKextWithLoadTag(uint32_t loadTag)
169*c54f35caSApple OSS Distributions {
170*c54f35caSApple OSS Distributions return OSKext::removeKextWithLoadTag(loadTag,
171*c54f35caSApple OSS Distributions /* terminateServicesAndRemovePersonalitiesFlag */ false);
172*c54f35caSApple OSS Distributions }
173*c54f35caSApple OSS Distributions
174*c54f35caSApple OSS Distributions
175*c54f35caSApple OSS Distributions #if PRAGMA_MARK
176*c54f35caSApple OSS Distributions #pragma mark Kext Requests
177*c54f35caSApple OSS Distributions #endif
178*c54f35caSApple OSS Distributions /*********************************************************************
179*c54f35caSApple OSS Distributions * Kext Requests
180*c54f35caSApple OSS Distributions *********************************************************************/
181*c54f35caSApple OSS Distributions OSReturn
OSKextRequestResource(const char * kextIdentifier,const char * resourceName,OSKextRequestResourceCallback callback,void * context,OSKextRequestTag * requestTagOut)182*c54f35caSApple OSS Distributions OSKextRequestResource(
183*c54f35caSApple OSS Distributions const char * kextIdentifier,
184*c54f35caSApple OSS Distributions const char * resourceName,
185*c54f35caSApple OSS Distributions OSKextRequestResourceCallback callback,
186*c54f35caSApple OSS Distributions void * context,
187*c54f35caSApple OSS Distributions OSKextRequestTag * requestTagOut)
188*c54f35caSApple OSS Distributions {
189*c54f35caSApple OSS Distributions return OSKext::requestResource(kextIdentifier, resourceName,
190*c54f35caSApple OSS Distributions callback, context, requestTagOut);
191*c54f35caSApple OSS Distributions }
192*c54f35caSApple OSS Distributions
193*c54f35caSApple OSS Distributions /*********************************************************************
194*c54f35caSApple OSS Distributions *********************************************************************/
195*c54f35caSApple OSS Distributions OSReturn
OSKextCancelRequest(OSKextRequestTag requestTag,void ** contextOut)196*c54f35caSApple OSS Distributions OSKextCancelRequest(
197*c54f35caSApple OSS Distributions OSKextRequestTag requestTag,
198*c54f35caSApple OSS Distributions void ** contextOut)
199*c54f35caSApple OSS Distributions {
200*c54f35caSApple OSS Distributions return OSKext::cancelRequest(requestTag, contextOut);
201*c54f35caSApple OSS Distributions }
202*c54f35caSApple OSS Distributions
203*c54f35caSApple OSS Distributions #if PRAGMA_MARK
204*c54f35caSApple OSS Distributions #pragma mark MIG Functions & Wrappers
205*c54f35caSApple OSS Distributions #endif
206*c54f35caSApple OSS Distributions /*********************************************************************
207*c54f35caSApple OSS Distributions * IMPORTANT: vm_map_copyout_size() consumes the requestIn copy
208*c54f35caSApple OSS Distributions * object on success. Therefore once it has been invoked successfully,
209*c54f35caSApple OSS Distributions * this routine *must* return KERN_SUCCESS, regardless of our actual
210*c54f35caSApple OSS Distributions * result. Our contract with the caller is that requestIn must be
211*c54f35caSApple OSS Distributions * caller-deallocated if we return an error. We use op_result to return
212*c54f35caSApple OSS Distributions * the real result of our work.
213*c54f35caSApple OSS Distributions *********************************************************************/
214*c54f35caSApple OSS Distributions kern_return_t
kext_request(host_priv_t hostPriv,uint32_t clientLogSpec,vm_offset_t requestIn,mach_msg_type_number_t requestLengthIn,vm_offset_t * responseOut,mach_msg_type_number_t * responseLengthOut,vm_offset_t * logDataOut,mach_msg_type_number_t * logDataLengthOut,kern_return_t * op_result)215*c54f35caSApple OSS Distributions kext_request(
216*c54f35caSApple OSS Distributions host_priv_t hostPriv,
217*c54f35caSApple OSS Distributions /* in only */ uint32_t clientLogSpec,
218*c54f35caSApple OSS Distributions /* in only */ vm_offset_t requestIn,
219*c54f35caSApple OSS Distributions /* in only */ mach_msg_type_number_t requestLengthIn,
220*c54f35caSApple OSS Distributions /* out only */ vm_offset_t * responseOut,
221*c54f35caSApple OSS Distributions /* out only */ mach_msg_type_number_t * responseLengthOut,
222*c54f35caSApple OSS Distributions /* out only */ vm_offset_t * logDataOut,
223*c54f35caSApple OSS Distributions /* out only */ mach_msg_type_number_t * logDataLengthOut,
224*c54f35caSApple OSS Distributions /* out only */ kern_return_t * op_result)
225*c54f35caSApple OSS Distributions {
226*c54f35caSApple OSS Distributions kern_return_t result = KERN_FAILURE;
227*c54f35caSApple OSS Distributions vm_map_address_t map_addr = 0; // do not free/deallocate
228*c54f35caSApple OSS Distributions char * request = NULL;// must vm_deallocate
229*c54f35caSApple OSS Distributions
230*c54f35caSApple OSS Distributions mkext2_header * mkextHeader = NULL;// do not release
231*c54f35caSApple OSS Distributions bool isMkext = false;
232*c54f35caSApple OSS Distributions
233*c54f35caSApple OSS Distributions char * response = NULL;// must kmem_free
234*c54f35caSApple OSS Distributions uint32_t responseLength = 0;
235*c54f35caSApple OSS Distributions char * logData = NULL;// must kmem_free
236*c54f35caSApple OSS Distributions uint32_t logDataLength = 0;
237*c54f35caSApple OSS Distributions
238*c54f35caSApple OSS Distributions /* MIG doesn't pass "out" parameters as empty, so clear them immediately
239*c54f35caSApple OSS Distributions * just in case, or MIG will try to copy out bogus data.
240*c54f35caSApple OSS Distributions */
241*c54f35caSApple OSS Distributions *op_result = KERN_FAILURE;
242*c54f35caSApple OSS Distributions *responseOut = 0;
243*c54f35caSApple OSS Distributions *responseLengthOut = 0;
244*c54f35caSApple OSS Distributions *logDataOut = 0;
245*c54f35caSApple OSS Distributions *logDataLengthOut = 0;
246*c54f35caSApple OSS Distributions
247*c54f35caSApple OSS Distributions /* Check for input. Don't discard what isn't there, though.
248*c54f35caSApple OSS Distributions */
249*c54f35caSApple OSS Distributions if (!requestLengthIn || !requestIn) {
250*c54f35caSApple OSS Distributions OSKextLog(/* kext */ NULL,
251*c54f35caSApple OSS Distributions kOSKextLogErrorLevel |
252*c54f35caSApple OSS Distributions kOSKextLogIPCFlag,
253*c54f35caSApple OSS Distributions "Invalid request from user space (no data).");
254*c54f35caSApple OSS Distributions *op_result = KERN_INVALID_ARGUMENT;
255*c54f35caSApple OSS Distributions goto finish;
256*c54f35caSApple OSS Distributions }
257*c54f35caSApple OSS Distributions
258*c54f35caSApple OSS Distributions result = vm_map_copyout_size(kernel_map, &map_addr, (vm_map_copy_t)requestIn, requestLengthIn);
259*c54f35caSApple OSS Distributions if (result != KERN_SUCCESS) {
260*c54f35caSApple OSS Distributions OSKextLog(/* kext */ NULL,
261*c54f35caSApple OSS Distributions kOSKextLogErrorLevel |
262*c54f35caSApple OSS Distributions kOSKextLogIPCFlag,
263*c54f35caSApple OSS Distributions "vm_map_copyout() failed for request from user space.");
264*c54f35caSApple OSS Distributions /*
265*c54f35caSApple OSS Distributions * If we return an error it is our caller's responsibility to
266*c54f35caSApple OSS Distributions * deallocate the requestIn copy object, so do not deallocate it
267*c54f35caSApple OSS Distributions * here. See comment above.
268*c54f35caSApple OSS Distributions */
269*c54f35caSApple OSS Distributions goto finish;
270*c54f35caSApple OSS Distributions }
271*c54f35caSApple OSS Distributions request = CAST_DOWN(char *, map_addr);
272*c54f35caSApple OSS Distributions
273*c54f35caSApple OSS Distributions /* Check if request is an mkext; this is always a load request
274*c54f35caSApple OSS Distributions * and requires root access. If it isn't an mkext, see if it's
275*c54f35caSApple OSS Distributions * an XML request, and check the request to see if that requires
276*c54f35caSApple OSS Distributions * root access.
277*c54f35caSApple OSS Distributions */
278*c54f35caSApple OSS Distributions if (requestLengthIn > sizeof(mkext2_header)) {
279*c54f35caSApple OSS Distributions mkextHeader = (mkext2_header *)request;
280*c54f35caSApple OSS Distributions if (MKEXT_GET_MAGIC(mkextHeader) == MKEXT_MAGIC &&
281*c54f35caSApple OSS Distributions MKEXT_GET_SIGNATURE(mkextHeader) == MKEXT_SIGN) {
282*c54f35caSApple OSS Distributions isMkext = true;
283*c54f35caSApple OSS Distributions }
284*c54f35caSApple OSS Distributions }
285*c54f35caSApple OSS Distributions
286*c54f35caSApple OSS Distributions if (isMkext) {
287*c54f35caSApple OSS Distributions #if defined(SECURE_KERNEL) || !CONFIG_KXLD
288*c54f35caSApple OSS Distributions // xxx - something tells me if we have a secure kernel we don't even
289*c54f35caSApple OSS Distributions // xxx - want to log a message here. :-)
290*c54f35caSApple OSS Distributions *op_result = KERN_NOT_SUPPORTED;
291*c54f35caSApple OSS Distributions goto finish;
292*c54f35caSApple OSS Distributions #else
293*c54f35caSApple OSS Distributions // xxx - can we find out if calling task is kextd?
294*c54f35caSApple OSS Distributions // xxx - can we find the name of the calling task?
295*c54f35caSApple OSS Distributions if (hostPriv == HOST_PRIV_NULL) {
296*c54f35caSApple OSS Distributions OSKextLog(/* kext */ NULL,
297*c54f35caSApple OSS Distributions kOSKextLogErrorLevel |
298*c54f35caSApple OSS Distributions kOSKextLogLoadFlag | kOSKextLogIPCFlag,
299*c54f35caSApple OSS Distributions "Attempt by non-root process to load a kext.");
300*c54f35caSApple OSS Distributions *op_result = kOSKextReturnNotPrivileged;
301*c54f35caSApple OSS Distributions goto finish;
302*c54f35caSApple OSS Distributions }
303*c54f35caSApple OSS Distributions
304*c54f35caSApple OSS Distributions *op_result = OSKext::loadFromMkext((OSKextLogSpec)clientLogSpec,
305*c54f35caSApple OSS Distributions request, requestLengthIn,
306*c54f35caSApple OSS Distributions &logData, &logDataLength);
307*c54f35caSApple OSS Distributions
308*c54f35caSApple OSS Distributions #endif /* defined(SECURE_KERNEL) */
309*c54f35caSApple OSS Distributions } else {
310*c54f35caSApple OSS Distributions /* If the request isn't an mkext, then is should be XML. Parse it
311*c54f35caSApple OSS Distributions * if possible and hand the request over to OSKext.
312*c54f35caSApple OSS Distributions */
313*c54f35caSApple OSS Distributions *op_result = OSKext::handleRequest(hostPriv,
314*c54f35caSApple OSS Distributions (OSKextLogSpec)clientLogSpec,
315*c54f35caSApple OSS Distributions request, requestLengthIn,
316*c54f35caSApple OSS Distributions &response, &responseLength,
317*c54f35caSApple OSS Distributions &logData, &logDataLength);
318*c54f35caSApple OSS Distributions }
319*c54f35caSApple OSS Distributions
320*c54f35caSApple OSS Distributions if (response && responseLength > 0) {
321*c54f35caSApple OSS Distributions kern_return_t copyin_result;
322*c54f35caSApple OSS Distributions
323*c54f35caSApple OSS Distributions copyin_result = vm_map_copyin(kernel_map,
324*c54f35caSApple OSS Distributions CAST_USER_ADDR_T(response), responseLength,
325*c54f35caSApple OSS Distributions /* src_destroy */ false, (vm_map_copy_t *)responseOut);
326*c54f35caSApple OSS Distributions if (copyin_result == KERN_SUCCESS) {
327*c54f35caSApple OSS Distributions *responseLengthOut = responseLength;
328*c54f35caSApple OSS Distributions } else {
329*c54f35caSApple OSS Distributions OSKextLog(/* kext */ NULL,
330*c54f35caSApple OSS Distributions kOSKextLogErrorLevel |
331*c54f35caSApple OSS Distributions kOSKextLogIPCFlag,
332*c54f35caSApple OSS Distributions "Failed to copy response to request from user space.");
333*c54f35caSApple OSS Distributions *op_result = copyin_result; // xxx - should we map to our own code?
334*c54f35caSApple OSS Distributions *responseOut = 0;
335*c54f35caSApple OSS Distributions *responseLengthOut = 0;
336*c54f35caSApple OSS Distributions goto finish;
337*c54f35caSApple OSS Distributions }
338*c54f35caSApple OSS Distributions }
339*c54f35caSApple OSS Distributions
340*c54f35caSApple OSS Distributions if (logData && logDataLength > 0) {
341*c54f35caSApple OSS Distributions kern_return_t copyin_result;
342*c54f35caSApple OSS Distributions
343*c54f35caSApple OSS Distributions copyin_result = vm_map_copyin(kernel_map,
344*c54f35caSApple OSS Distributions CAST_USER_ADDR_T(logData), logDataLength,
345*c54f35caSApple OSS Distributions /* src_destroy */ false, (vm_map_copy_t *)logDataOut);
346*c54f35caSApple OSS Distributions if (copyin_result == KERN_SUCCESS) {
347*c54f35caSApple OSS Distributions *logDataLengthOut = logDataLength;
348*c54f35caSApple OSS Distributions } else {
349*c54f35caSApple OSS Distributions OSKextLog(/* kext */ NULL,
350*c54f35caSApple OSS Distributions kOSKextLogErrorLevel |
351*c54f35caSApple OSS Distributions kOSKextLogIPCFlag,
352*c54f35caSApple OSS Distributions "Failed to copy log data for request from user space.");
353*c54f35caSApple OSS Distributions *op_result = copyin_result; // xxx - should we map to our own code?
354*c54f35caSApple OSS Distributions *logDataOut = 0;
355*c54f35caSApple OSS Distributions *logDataLengthOut = 0;
356*c54f35caSApple OSS Distributions goto finish;
357*c54f35caSApple OSS Distributions }
358*c54f35caSApple OSS Distributions }
359*c54f35caSApple OSS Distributions
360*c54f35caSApple OSS Distributions finish:
361*c54f35caSApple OSS Distributions if (request) {
362*c54f35caSApple OSS Distributions (void)vm_deallocate(kernel_map, (vm_offset_t)request, requestLengthIn);
363*c54f35caSApple OSS Distributions }
364*c54f35caSApple OSS Distributions if (response) {
365*c54f35caSApple OSS Distributions /* 11981737 - clear uninitialized data in last page */
366*c54f35caSApple OSS Distributions kmem_free(kernel_map, (vm_offset_t)response, round_page(responseLength));
367*c54f35caSApple OSS Distributions }
368*c54f35caSApple OSS Distributions if (logData) {
369*c54f35caSApple OSS Distributions /* 11981737 - clear uninitialized data in last page */
370*c54f35caSApple OSS Distributions kmem_free(kernel_map, (vm_offset_t)logData, round_page(logDataLength));
371*c54f35caSApple OSS Distributions }
372*c54f35caSApple OSS Distributions
373*c54f35caSApple OSS Distributions return result;
374*c54f35caSApple OSS Distributions }
375*c54f35caSApple OSS Distributions
376*c54f35caSApple OSS Distributions /*********************************************************************
377*c54f35caSApple OSS Distributions * Gets the vm_map for the current kext
378*c54f35caSApple OSS Distributions *********************************************************************/
379*c54f35caSApple OSS Distributions extern vm_offset_t segPRELINKTEXTB;
380*c54f35caSApple OSS Distributions extern vm_offset_t segLINKB;
381*c54f35caSApple OSS Distributions extern unsigned long segSizePRELINKTEXT;
382*c54f35caSApple OSS Distributions extern vm_map_t g_kext_map;
383*c54f35caSApple OSS Distributions
384*c54f35caSApple OSS Distributions vm_map_t
kext_get_vm_map(kmod_info_t * info)385*c54f35caSApple OSS Distributions kext_get_vm_map(kmod_info_t *info)
386*c54f35caSApple OSS Distributions {
387*c54f35caSApple OSS Distributions vm_map_t kext_map = NULL;
388*c54f35caSApple OSS Distributions kc_format_t kcformat;
389*c54f35caSApple OSS Distributions
390*c54f35caSApple OSS Distributions if (PE_get_primary_kc_format(&kcformat) && kcformat == KCFormatFileset) {
391*c54f35caSApple OSS Distributions /* Check if the kext is from the boot KC */
392*c54f35caSApple OSS Distributions assert(segLINKB >= (segPRELINKTEXTB + segSizePRELINKTEXT));
393*c54f35caSApple OSS Distributions if ((info->address >= segPRELINKTEXTB) &&
394*c54f35caSApple OSS Distributions (info->address < segLINKB)) {
395*c54f35caSApple OSS Distributions kext_map = kernel_map;
396*c54f35caSApple OSS Distributions } else {
397*c54f35caSApple OSS Distributions kext_map = g_kext_map;
398*c54f35caSApple OSS Distributions }
399*c54f35caSApple OSS Distributions } else {
400*c54f35caSApple OSS Distributions if ((info->address >= segPRELINKTEXTB) &&
401*c54f35caSApple OSS Distributions (info->address < (segPRELINKTEXTB + segSizePRELINKTEXT))) {
402*c54f35caSApple OSS Distributions kext_map = kernel_map;
403*c54f35caSApple OSS Distributions } else {
404*c54f35caSApple OSS Distributions kext_map = g_kext_map;
405*c54f35caSApple OSS Distributions }
406*c54f35caSApple OSS Distributions }
407*c54f35caSApple OSS Distributions
408*c54f35caSApple OSS Distributions return kext_map;
409*c54f35caSApple OSS Distributions }
410*c54f35caSApple OSS Distributions
411*c54f35caSApple OSS Distributions
412*c54f35caSApple OSS Distributions #if PRAGMA_MARK
413*c54f35caSApple OSS Distributions /********************************************************************/
414*c54f35caSApple OSS Distributions #pragma mark Weak linking support
415*c54f35caSApple OSS Distributions /********************************************************************/
416*c54f35caSApple OSS Distributions #endif
417*c54f35caSApple OSS Distributions void
kext_weak_symbol_referenced(void)418*c54f35caSApple OSS Distributions kext_weak_symbol_referenced(void)
419*c54f35caSApple OSS Distributions {
420*c54f35caSApple OSS Distributions panic("A kext referenced an unresolved weak symbol");
421*c54f35caSApple OSS Distributions }
422*c54f35caSApple OSS Distributions
423*c54f35caSApple OSS Distributions const void * const gOSKextUnresolved = (const void *)&kext_weak_symbol_referenced;
424*c54f35caSApple OSS Distributions
425*c54f35caSApple OSS Distributions #if PRAGMA_MARK
426*c54f35caSApple OSS Distributions #pragma mark Kernel-Internal C Functions
427*c54f35caSApple OSS Distributions #endif
428*c54f35caSApple OSS Distributions /*********************************************************************
429*c54f35caSApple OSS Distributions * Called from startup.c.
430*c54f35caSApple OSS Distributions *********************************************************************/
431*c54f35caSApple OSS Distributions void
OSKextRemoveKextBootstrap(void)432*c54f35caSApple OSS Distributions OSKextRemoveKextBootstrap(void)
433*c54f35caSApple OSS Distributions {
434*c54f35caSApple OSS Distributions OSKext::removeKextBootstrap();
435*c54f35caSApple OSS Distributions return;
436*c54f35caSApple OSS Distributions }
437*c54f35caSApple OSS Distributions
438*c54f35caSApple OSS Distributions #if CONFIG_DTRACE
439*c54f35caSApple OSS Distributions /*********************************************************************
440*c54f35caSApple OSS Distributions *********************************************************************/
441*c54f35caSApple OSS Distributions void
OSKextRegisterKextsWithDTrace(void)442*c54f35caSApple OSS Distributions OSKextRegisterKextsWithDTrace(void)
443*c54f35caSApple OSS Distributions {
444*c54f35caSApple OSS Distributions OSKext::registerKextsWithDTrace();
445*c54f35caSApple OSS Distributions return;
446*c54f35caSApple OSS Distributions }
447*c54f35caSApple OSS Distributions #endif /* CONFIG_DTRACE */
448*c54f35caSApple OSS Distributions
449*c54f35caSApple OSS Distributions /*********************************************************************
450*c54f35caSApple OSS Distributions *********************************************************************/
451*c54f35caSApple OSS Distributions void
kext_dump_panic_lists(int (* printf_func)(const char * fmt,...))452*c54f35caSApple OSS Distributions kext_dump_panic_lists(int (*printf_func)(const char * fmt, ...))
453*c54f35caSApple OSS Distributions {
454*c54f35caSApple OSS Distributions OSKext::printKextPanicLists(printf_func);
455*c54f35caSApple OSS Distributions return;
456*c54f35caSApple OSS Distributions }
457*c54f35caSApple OSS Distributions
458*c54f35caSApple OSS Distributions #if PRAGMA_MARK
459*c54f35caSApple OSS Distributions #pragma mark Kmod Compatibility Functions
460*c54f35caSApple OSS Distributions #endif
461*c54f35caSApple OSS Distributions /*********************************************************************
462*c54f35caSApple OSS Distributions **********************************************************************
463*c54f35caSApple OSS Distributions * KMOD COMPATIBILITY FUNCTIONS *
464*c54f35caSApple OSS Distributions * (formerly in kmod.c, or C++ bridges from) *
465*c54f35caSApple OSS Distributions **********************************************************************
466*c54f35caSApple OSS Distributions **********************************************************************
467*c54f35caSApple OSS Distributions * These two functions are used in various places in the kernel, but
468*c54f35caSApple OSS Distributions * are not exported. We might rename them at some point to start with
469*c54f35caSApple OSS Distributions * kext_ or OSKext.
470*c54f35caSApple OSS Distributions *
471*c54f35caSApple OSS Distributions * kmod_panic_dump() must not be called outside of a panic context.
472*c54f35caSApple OSS Distributions * kmod_dump_log() must not be called in a panic context.
473*c54f35caSApple OSS Distributions *********************************************************************/
474*c54f35caSApple OSS Distributions void
kmod_panic_dump(vm_offset_t * addr,unsigned int cnt)475*c54f35caSApple OSS Distributions kmod_panic_dump(vm_offset_t * addr, unsigned int cnt)
476*c54f35caSApple OSS Distributions {
477*c54f35caSApple OSS Distributions extern int paniclog_append_noflush(const char *format, ...) __printflike(1, 2);
478*c54f35caSApple OSS Distributions
479*c54f35caSApple OSS Distributions OSKext::printKextsInBacktrace(addr, cnt, &paniclog_append_noflush, 0);
480*c54f35caSApple OSS Distributions
481*c54f35caSApple OSS Distributions return;
482*c54f35caSApple OSS Distributions }
483*c54f35caSApple OSS Distributions
484*c54f35caSApple OSS Distributions /********************************************************************/
485*c54f35caSApple OSS Distributions void kmod_dump_log(vm_offset_t *addr, unsigned int cnt, boolean_t doUnslide);
486*c54f35caSApple OSS Distributions
487*c54f35caSApple OSS Distributions void
kmod_dump_log(vm_offset_t * addr,unsigned int cnt,boolean_t doUnslide)488*c54f35caSApple OSS Distributions kmod_dump_log(
489*c54f35caSApple OSS Distributions vm_offset_t * addr,
490*c54f35caSApple OSS Distributions unsigned int cnt,
491*c54f35caSApple OSS Distributions boolean_t doUnslide)
492*c54f35caSApple OSS Distributions {
493*c54f35caSApple OSS Distributions uint32_t flags = OSKext::kPrintKextsLock;
494*c54f35caSApple OSS Distributions if (doUnslide) {
495*c54f35caSApple OSS Distributions flags |= OSKext::kPrintKextsUnslide;
496*c54f35caSApple OSS Distributions }
497*c54f35caSApple OSS Distributions OSKext::printKextsInBacktrace(addr, cnt, &printf, flags);
498*c54f35caSApple OSS Distributions }
499*c54f35caSApple OSS Distributions
500*c54f35caSApple OSS Distributions void *
OSKextKextForAddress(const void * addr)501*c54f35caSApple OSS Distributions OSKextKextForAddress(const void *addr)
502*c54f35caSApple OSS Distributions {
503*c54f35caSApple OSS Distributions return OSKext::kextForAddress(addr);
504*c54f35caSApple OSS Distributions }
505*c54f35caSApple OSS Distributions
506*c54f35caSApple OSS Distributions
507*c54f35caSApple OSS Distributions /*********************************************************************
508*c54f35caSApple OSS Distributions * Compatibility implementation for kmod_get_info() host_priv routine.
509*c54f35caSApple OSS Distributions * Only supported on old 32-bit architectures.
510*c54f35caSApple OSS Distributions *********************************************************************/
511*c54f35caSApple OSS Distributions
512*c54f35caSApple OSS Distributions #if PRAGMA_MARK
513*c54f35caSApple OSS Distributions #pragma mark Loaded Kext Summary
514*c54f35caSApple OSS Distributions #endif
515*c54f35caSApple OSS Distributions
516*c54f35caSApple OSS Distributions void
OSKextLoadedKextSummariesUpdated(void)517*c54f35caSApple OSS Distributions OSKextLoadedKextSummariesUpdated(void)
518*c54f35caSApple OSS Distributions {
519*c54f35caSApple OSS Distributions // Do nothing.
520*c54f35caSApple OSS Distributions }
521*c54f35caSApple OSS Distributions };
522