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