1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions * Copyright (c) 2000-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 /* OSMetaClass.cpp created by gvdl on Fri 1998-11-17 */
29*c54f35caSApple OSS Distributions
30*c54f35caSApple OSS Distributions #include <string.h>
31*c54f35caSApple OSS Distributions
32*c54f35caSApple OSS Distributions #include <libkern/OSReturn.h>
33*c54f35caSApple OSS Distributions
34*c54f35caSApple OSS Distributions #include <libkern/c++/OSMetaClass.h>
35*c54f35caSApple OSS Distributions #include <libkern/c++/OSObject.h>
36*c54f35caSApple OSS Distributions #include <libkern/c++/OSKext.h>
37*c54f35caSApple OSS Distributions
38*c54f35caSApple OSS Distributions #include <libkern/c++/OSCollectionIterator.h>
39*c54f35caSApple OSS Distributions #include <libkern/c++/OSDictionary.h>
40*c54f35caSApple OSS Distributions #include <libkern/c++/OSArray.h>
41*c54f35caSApple OSS Distributions #include <libkern/c++/OSSet.h>
42*c54f35caSApple OSS Distributions #include <libkern/c++/OSSymbol.h>
43*c54f35caSApple OSS Distributions #include <libkern/c++/OSNumber.h>
44*c54f35caSApple OSS Distributions #include <libkern/c++/OSSerialize.h>
45*c54f35caSApple OSS Distributions
46*c54f35caSApple OSS Distributions #include <libkern/c++/OSLib.h>
47*c54f35caSApple OSS Distributions #include <libkern/OSAtomic.h>
48*c54f35caSApple OSS Distributions
49*c54f35caSApple OSS Distributions #include <IOKit/IOLib.h>
50*c54f35caSApple OSS Distributions
51*c54f35caSApple OSS Distributions #include <IOKit/IOKitDebug.h>
52*c54f35caSApple OSS Distributions
53*c54f35caSApple OSS Distributions
54*c54f35caSApple OSS Distributions __BEGIN_DECLS
55*c54f35caSApple OSS Distributions
56*c54f35caSApple OSS Distributions #include <sys/systm.h>
57*c54f35caSApple OSS Distributions #include <mach/mach_types.h>
58*c54f35caSApple OSS Distributions #include <kern/locks.h>
59*c54f35caSApple OSS Distributions #include <kern/clock.h>
60*c54f35caSApple OSS Distributions #include <kern/thread_call.h>
61*c54f35caSApple OSS Distributions #include <kern/host.h>
62*c54f35caSApple OSS Distributions #include <mach/mach_interface.h>
63*c54f35caSApple OSS Distributions #include <stddef.h>
64*c54f35caSApple OSS Distributions
65*c54f35caSApple OSS Distributions #if PRAGMA_MARK
66*c54f35caSApple OSS Distributions #pragma mark Macros
67*c54f35caSApple OSS Distributions #endif /* PRAGMA_MARK */
68*c54f35caSApple OSS Distributions /*********************************************************************
69*c54f35caSApple OSS Distributions * Macros
70*c54f35caSApple OSS Distributions *********************************************************************/
71*c54f35caSApple OSS Distributions __END_DECLS
72*c54f35caSApple OSS Distributions
73*c54f35caSApple OSS Distributions #if PRAGMA_MARK
74*c54f35caSApple OSS Distributions #pragma mark Internal constants & data structs
75*c54f35caSApple OSS Distributions #endif /* PRAGMA_MARK */
76*c54f35caSApple OSS Distributions /*********************************************************************
77*c54f35caSApple OSS Distributions * Internal constants & data structs
78*c54f35caSApple OSS Distributions *********************************************************************/
79*c54f35caSApple OSS Distributions OSKextLogSpec kOSMetaClassLogSpec =
80*c54f35caSApple OSS Distributions kOSKextLogErrorLevel |
81*c54f35caSApple OSS Distributions kOSKextLogLoadFlag |
82*c54f35caSApple OSS Distributions kOSKextLogKextBookkeepingFlag;
83*c54f35caSApple OSS Distributions
84*c54f35caSApple OSS Distributions static enum {
85*c54f35caSApple OSS Distributions kCompletedBootstrap = 0,
86*c54f35caSApple OSS Distributions kNoDictionaries = 1,
87*c54f35caSApple OSS Distributions kMakingDictionaries = 2
88*c54f35caSApple OSS Distributions } sBootstrapState = kNoDictionaries;
89*c54f35caSApple OSS Distributions
90*c54f35caSApple OSS Distributions static const int kClassCapacityIncrement = 40;
91*c54f35caSApple OSS Distributions static const int kKModCapacityIncrement = 10;
92*c54f35caSApple OSS Distributions static OSDictionary * sAllClassesDict;
93*c54f35caSApple OSS Distributions static unsigned int sDeepestClass;
94*c54f35caSApple OSS Distributions IOLock * sAllClassesLock = NULL;
95*c54f35caSApple OSS Distributions IOLock * sInstancesLock = NULL;
96*c54f35caSApple OSS Distributions
97*c54f35caSApple OSS Distributions /*
98*c54f35caSApple OSS Distributions * While loading a kext and running all its constructors to register
99*c54f35caSApple OSS Distributions * all OSMetaClass classes, the classes are queued up here. Only one
100*c54f35caSApple OSS Distributions * kext can be in flight at a time, guarded by sStalledClassesLock
101*c54f35caSApple OSS Distributions */
102*c54f35caSApple OSS Distributions static struct StalledData {
103*c54f35caSApple OSS Distributions const char * kextIdentifier;
104*c54f35caSApple OSS Distributions OSReturn result;
105*c54f35caSApple OSS Distributions unsigned int capacity;
106*c54f35caSApple OSS Distributions unsigned int count;
107*c54f35caSApple OSS Distributions OSMetaClass ** classes;
108*c54f35caSApple OSS Distributions } * sStalled;
109*c54f35caSApple OSS Distributions IOLock * sStalledClassesLock = NULL;
110*c54f35caSApple OSS Distributions
111*c54f35caSApple OSS Distributions struct ExpansionData {
112*c54f35caSApple OSS Distributions OSOrderedSet * instances;
113*c54f35caSApple OSS Distributions OSKext * kext;
114*c54f35caSApple OSS Distributions uint32_t retain;
115*c54f35caSApple OSS Distributions #if IOTRACKING
116*c54f35caSApple OSS Distributions IOTrackingQueue * tracking;
117*c54f35caSApple OSS Distributions #endif
118*c54f35caSApple OSS Distributions };
119*c54f35caSApple OSS Distributions
120*c54f35caSApple OSS Distributions
121*c54f35caSApple OSS Distributions #if PRAGMA_MARK
122*c54f35caSApple OSS Distributions #pragma mark OSMetaClassBase
123*c54f35caSApple OSS Distributions #endif /* PRAGMA_MARK */
124*c54f35caSApple OSS Distributions /*********************************************************************
125*c54f35caSApple OSS Distributions * OSMetaClassBase.
126*c54f35caSApple OSS Distributions *********************************************************************/
127*c54f35caSApple OSS Distributions
128*c54f35caSApple OSS Distributions #if APPLE_KEXT_VTABLE_PADDING
129*c54f35caSApple OSS Distributions /*********************************************************************
130*c54f35caSApple OSS Distributions * Reserved vtable functions.
131*c54f35caSApple OSS Distributions *********************************************************************/
132*c54f35caSApple OSS Distributions #if defined(__arm64__) || defined(__arm__)
133*c54f35caSApple OSS Distributions void
_RESERVEDOSMetaClassBase0()134*c54f35caSApple OSS Distributions OSMetaClassBase::_RESERVEDOSMetaClassBase0()
135*c54f35caSApple OSS Distributions {
136*c54f35caSApple OSS Distributions panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 0);
137*c54f35caSApple OSS Distributions }
138*c54f35caSApple OSS Distributions void
_RESERVEDOSMetaClassBase1()139*c54f35caSApple OSS Distributions OSMetaClassBase::_RESERVEDOSMetaClassBase1()
140*c54f35caSApple OSS Distributions {
141*c54f35caSApple OSS Distributions panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 1);
142*c54f35caSApple OSS Distributions }
143*c54f35caSApple OSS Distributions void
_RESERVEDOSMetaClassBase2()144*c54f35caSApple OSS Distributions OSMetaClassBase::_RESERVEDOSMetaClassBase2()
145*c54f35caSApple OSS Distributions {
146*c54f35caSApple OSS Distributions panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 2);
147*c54f35caSApple OSS Distributions }
148*c54f35caSApple OSS Distributions void
_RESERVEDOSMetaClassBase3()149*c54f35caSApple OSS Distributions OSMetaClassBase::_RESERVEDOSMetaClassBase3()
150*c54f35caSApple OSS Distributions {
151*c54f35caSApple OSS Distributions panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 3);
152*c54f35caSApple OSS Distributions }
153*c54f35caSApple OSS Distributions #endif /* defined(__arm64__) || defined(__arm__) */
154*c54f35caSApple OSS Distributions
155*c54f35caSApple OSS Distributions // As these slots are used move them up inside the #if above
156*c54f35caSApple OSS Distributions void
_RESERVEDOSMetaClassBase4()157*c54f35caSApple OSS Distributions OSMetaClassBase::_RESERVEDOSMetaClassBase4()
158*c54f35caSApple OSS Distributions {
159*c54f35caSApple OSS Distributions panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 4);
160*c54f35caSApple OSS Distributions }
161*c54f35caSApple OSS Distributions void
_RESERVEDOSMetaClassBase5()162*c54f35caSApple OSS Distributions OSMetaClassBase::_RESERVEDOSMetaClassBase5()
163*c54f35caSApple OSS Distributions {
164*c54f35caSApple OSS Distributions panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 5);
165*c54f35caSApple OSS Distributions }
166*c54f35caSApple OSS Distributions void
_RESERVEDOSMetaClassBase6()167*c54f35caSApple OSS Distributions OSMetaClassBase::_RESERVEDOSMetaClassBase6()
168*c54f35caSApple OSS Distributions {
169*c54f35caSApple OSS Distributions panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 6);
170*c54f35caSApple OSS Distributions }
171*c54f35caSApple OSS Distributions #endif
172*c54f35caSApple OSS Distributions
173*c54f35caSApple OSS Distributions /*********************************************************************
174*c54f35caSApple OSS Distributions *********************************************************************/
175*c54f35caSApple OSS Distributions
176*c54f35caSApple OSS Distributions #if defined(__arm__) || defined(__arm64__)
177*c54f35caSApple OSS Distributions
178*c54f35caSApple OSS Distributions #if defined(HAS_APPLE_PAC)
179*c54f35caSApple OSS Distributions #include <ptrauth.h>
180*c54f35caSApple OSS Distributions #endif /* defined(HAS_APPLE_PAC) */
181*c54f35caSApple OSS Distributions
182*c54f35caSApple OSS Distributions /*
183*c54f35caSApple OSS Distributions * IHI0059A "C++ Application Binary Interface Standard for the ARM 64 - bit Architecture":
184*c54f35caSApple OSS Distributions *
185*c54f35caSApple OSS Distributions * 3.2.1 Representation of pointer to member function The generic C++ ABI [GC++ABI]
186*c54f35caSApple OSS Distributions * specifies that a pointer to member function is a pair of words <ptr, adj>. The
187*c54f35caSApple OSS Distributions * least significant bit of ptr discriminates between (0) the address of a non-
188*c54f35caSApple OSS Distributions * virtual member function and (1) the offset in the class's virtual table of the
189*c54f35caSApple OSS Distributions * address of a virtual function. This encoding cannot work for the AArch64
190*c54f35caSApple OSS Distributions * instruction set where the architecture reserves all bits of code addresses. This
191*c54f35caSApple OSS Distributions * ABI specifies that adj contains twice the this adjustment, plus 1 if the member
192*c54f35caSApple OSS Distributions * function is virtual. The least significant bit of adj then makes exactly the
193*c54f35caSApple OSS Distributions * same discrimination as the least significant bit of ptr does for Itanium. A
194*c54f35caSApple OSS Distributions * pointer to member function is NULL when ptr = 0 and the least significant bit of
195*c54f35caSApple OSS Distributions * adj is zero.
196*c54f35caSApple OSS Distributions */
197*c54f35caSApple OSS Distributions
198*c54f35caSApple OSS Distributions OSMetaClassBase::_ptf_t
199*c54f35caSApple OSS Distributions #if defined(HAS_APPLE_PAC) && \
200*c54f35caSApple OSS Distributions __has_feature(ptrauth_member_function_pointer_type_discrimination)
_ptmf2ptf(const OSMetaClassBase * self,void (OSMetaClassBase::* func)(void))201*c54f35caSApple OSS Distributions OSMetaClassBase::_ptmf2ptf(const OSMetaClassBase *self __attribute__((unused)),
202*c54f35caSApple OSS Distributions void (OSMetaClassBase::*func)(void))
203*c54f35caSApple OSS Distributions #else
204*c54f35caSApple OSS Distributions OSMetaClassBase::_ptmf2ptf(const OSMetaClassBase *self,
205*c54f35caSApple OSS Distributions void (OSMetaClassBase::*func)(void))
206*c54f35caSApple OSS Distributions #endif
207*c54f35caSApple OSS Distributions {
208*c54f35caSApple OSS Distributions struct ptmf_t {
209*c54f35caSApple OSS Distributions _ptf_t fPFN;
210*c54f35caSApple OSS Distributions ptrdiff_t delta;
211*c54f35caSApple OSS Distributions };
212*c54f35caSApple OSS Distributions union {
213*c54f35caSApple OSS Distributions void (OSMetaClassBase::*fIn)(void);
214*c54f35caSApple OSS Distributions struct ptmf_t pTMF;
215*c54f35caSApple OSS Distributions } map;
216*c54f35caSApple OSS Distributions _ptf_t pfn;
217*c54f35caSApple OSS Distributions
218*c54f35caSApple OSS Distributions map.fIn = func;
219*c54f35caSApple OSS Distributions pfn = map.pTMF.fPFN;
220*c54f35caSApple OSS Distributions
221*c54f35caSApple OSS Distributions #if defined(HAS_APPLE_PAC) && \
222*c54f35caSApple OSS Distributions __has_feature(ptrauth_member_function_pointer_type_discrimination)
223*c54f35caSApple OSS Distributions // Authenticate 'pfn' using the member function pointer type discriminator
224*c54f35caSApple OSS Distributions // and resign it as a C function pointer. 'pfn' can point to either a
225*c54f35caSApple OSS Distributions // non-virtual function or a virtual member function thunk.
226*c54f35caSApple OSS Distributions // It can also be NULL.
227*c54f35caSApple OSS Distributions if (pfn) {
228*c54f35caSApple OSS Distributions pfn = ptrauth_auth_and_resign(pfn, ptrauth_key_function_pointer,
229*c54f35caSApple OSS Distributions ptrauth_type_discriminator(__typeof__(func)),
230*c54f35caSApple OSS Distributions ptrauth_key_function_pointer,
231*c54f35caSApple OSS Distributions ptrauth_function_pointer_type_discriminator(_ptf_t));
232*c54f35caSApple OSS Distributions }
233*c54f35caSApple OSS Distributions return pfn;
234*c54f35caSApple OSS Distributions #else
235*c54f35caSApple OSS Distributions if (map.pTMF.delta & 1) {
236*c54f35caSApple OSS Distributions // virtual
237*c54f35caSApple OSS Distributions union {
238*c54f35caSApple OSS Distributions const OSMetaClassBase *fObj;
239*c54f35caSApple OSS Distributions _ptf_t **vtablep;
240*c54f35caSApple OSS Distributions } u;
241*c54f35caSApple OSS Distributions u.fObj = self;
242*c54f35caSApple OSS Distributions
243*c54f35caSApple OSS Distributions // Virtual member function so dereference table
244*c54f35caSApple OSS Distributions #if defined(HAS_APPLE_PAC)
245*c54f35caSApple OSS Distributions // The entity hash is stored in the top 32-bits of the vtable offset of a
246*c54f35caSApple OSS Distributions // member function pointer.
247*c54f35caSApple OSS Distributions uint32_t entity_hash = ((uintptr_t)pfn) >> 32;
248*c54f35caSApple OSS Distributions pfn = (_ptf_t)(((uintptr_t) pfn) & 0xFFFFFFFF);
249*c54f35caSApple OSS Distributions
250*c54f35caSApple OSS Distributions #if __has_builtin(__builtin_get_vtable_pointer)
251*c54f35caSApple OSS Distributions const _ptf_t *vtablep =
252*c54f35caSApple OSS Distributions (const _ptf_t *)__builtin_get_vtable_pointer(u.fObj);
253*c54f35caSApple OSS Distributions #else
254*c54f35caSApple OSS Distributions // Authenticate the vtable pointer.
255*c54f35caSApple OSS Distributions const _ptf_t *vtablep = ptrauth_auth_data(*u.vtablep,
256*c54f35caSApple OSS Distributions ptrauth_key_cxx_vtable_pointer, 0);
257*c54f35caSApple OSS Distributions #endif
258*c54f35caSApple OSS Distributions // Calculate the address of the vtable entry.
259*c54f35caSApple OSS Distributions _ptf_t *vtentryp = (_ptf_t *)(((uintptr_t)vtablep) + (uintptr_t)pfn);
260*c54f35caSApple OSS Distributions // Load the pointer from the vtable entry.
261*c54f35caSApple OSS Distributions pfn = *vtentryp;
262*c54f35caSApple OSS Distributions
263*c54f35caSApple OSS Distributions // Finally, resign the vtable entry as a function pointer.
264*c54f35caSApple OSS Distributions uintptr_t auth_data = ptrauth_blend_discriminator(vtentryp, entity_hash);
265*c54f35caSApple OSS Distributions pfn = ptrauth_auth_and_resign(pfn, ptrauth_key_function_pointer,
266*c54f35caSApple OSS Distributions auth_data, ptrauth_key_function_pointer,
267*c54f35caSApple OSS Distributions ptrauth_function_pointer_type_discriminator(_ptf_t));
268*c54f35caSApple OSS Distributions #else /* defined(HAS_APPLE_PAC) */
269*c54f35caSApple OSS Distributions pfn = *(_ptf_t *)(((uintptr_t)*u.vtablep) + (uintptr_t)pfn);
270*c54f35caSApple OSS Distributions #endif /* !defined(HAS_APPLE_PAC) */
271*c54f35caSApple OSS Distributions return pfn;
272*c54f35caSApple OSS Distributions } else {
273*c54f35caSApple OSS Distributions // Not virtual, i.e. plain member func
274*c54f35caSApple OSS Distributions return pfn;
275*c54f35caSApple OSS Distributions }
276*c54f35caSApple OSS Distributions #endif
277*c54f35caSApple OSS Distributions }
278*c54f35caSApple OSS Distributions
279*c54f35caSApple OSS Distributions #endif /* defined(__arm__) || defined(__arm64__) */
280*c54f35caSApple OSS Distributions /*********************************************************************
281*c54f35caSApple OSS Distributions * These used to be inline in the header but gcc didn't believe us
282*c54f35caSApple OSS Distributions * Now we MUST pull the inline out at least until the compiler is
283*c54f35caSApple OSS Distributions * repaired.
284*c54f35caSApple OSS Distributions *
285*c54f35caSApple OSS Distributions * Helper inlines for runtime type preprocessor macros
286*c54f35caSApple OSS Distributions *********************************************************************/
287*c54f35caSApple OSS Distributions
288*c54f35caSApple OSS Distributions /*********************************************************************
289*c54f35caSApple OSS Distributions *********************************************************************/
290*c54f35caSApple OSS Distributions OSMetaClassBase *
safeMetaCast(const OSMetaClassBase * me,const OSMetaClass * toType)291*c54f35caSApple OSS Distributions OSMetaClassBase::safeMetaCast(
292*c54f35caSApple OSS Distributions const OSMetaClassBase * me,
293*c54f35caSApple OSS Distributions const OSMetaClass * toType)
294*c54f35caSApple OSS Distributions {
295*c54f35caSApple OSS Distributions return (me)? me->metaCast(toType) : NULL;
296*c54f35caSApple OSS Distributions }
297*c54f35caSApple OSS Distributions
298*c54f35caSApple OSS Distributions /// A helper function to crash with a kernel panic.
299*c54f35caSApple OSS Distributions __attribute__((cold, not_tail_called, noreturn))
300*c54f35caSApple OSS Distributions static inline void
panic_crash_fail_cast(const OSMetaClassBase * me,const OSMetaClass * toType)301*c54f35caSApple OSS Distributions panic_crash_fail_cast(const OSMetaClassBase *me,
302*c54f35caSApple OSS Distributions const OSMetaClass *toType)
303*c54f35caSApple OSS Distributions {
304*c54f35caSApple OSS Distributions panic("Unexpected cast fail: from %p to %p", me, toType);
305*c54f35caSApple OSS Distributions __builtin_unreachable();
306*c54f35caSApple OSS Distributions }
307*c54f35caSApple OSS Distributions
308*c54f35caSApple OSS Distributions OSMetaClassBase *
requiredMetaCast(const OSMetaClassBase * me,const OSMetaClass * toType)309*c54f35caSApple OSS Distributions OSMetaClassBase::requiredMetaCast(
310*c54f35caSApple OSS Distributions const OSMetaClassBase * me,
311*c54f35caSApple OSS Distributions const OSMetaClass * toType)
312*c54f35caSApple OSS Distributions {
313*c54f35caSApple OSS Distributions if (!me) {
314*c54f35caSApple OSS Distributions return NULL;
315*c54f35caSApple OSS Distributions }
316*c54f35caSApple OSS Distributions OSMetaClassBase *tmp = safeMetaCast(me, toType);
317*c54f35caSApple OSS Distributions if (!tmp) {
318*c54f35caSApple OSS Distributions panic_crash_fail_cast(me, toType);
319*c54f35caSApple OSS Distributions }
320*c54f35caSApple OSS Distributions return tmp;
321*c54f35caSApple OSS Distributions }
322*c54f35caSApple OSS Distributions
323*c54f35caSApple OSS Distributions /*********************************************************************
324*c54f35caSApple OSS Distributions *********************************************************************/
325*c54f35caSApple OSS Distributions bool
checkTypeInst(const OSMetaClassBase * inst,const OSMetaClassBase * typeinst)326*c54f35caSApple OSS Distributions OSMetaClassBase::checkTypeInst(
327*c54f35caSApple OSS Distributions const OSMetaClassBase * inst,
328*c54f35caSApple OSS Distributions const OSMetaClassBase * typeinst)
329*c54f35caSApple OSS Distributions {
330*c54f35caSApple OSS Distributions const OSMetaClass * toType = OSTypeIDInst(typeinst);
331*c54f35caSApple OSS Distributions return typeinst && inst && (NULL != inst->metaCast(toType));
332*c54f35caSApple OSS Distributions }
333*c54f35caSApple OSS Distributions
334*c54f35caSApple OSS Distributions /*********************************************************************
335*c54f35caSApple OSS Distributions *********************************************************************/
336*c54f35caSApple OSS Distributions void
337*c54f35caSApple OSS Distributions OSMetaClassBase::
initialize()338*c54f35caSApple OSS Distributions initialize()
339*c54f35caSApple OSS Distributions {
340*c54f35caSApple OSS Distributions sAllClassesLock = IOLockAlloc();
341*c54f35caSApple OSS Distributions sStalledClassesLock = IOLockAlloc();
342*c54f35caSApple OSS Distributions sInstancesLock = IOLockAlloc();
343*c54f35caSApple OSS Distributions }
344*c54f35caSApple OSS Distributions
345*c54f35caSApple OSS Distributions #if APPLE_KEXT_VTABLE_PADDING
346*c54f35caSApple OSS Distributions /*********************************************************************
347*c54f35caSApple OSS Distributions * If you need this slot you had better setup an IOCTL style interface.
348*c54f35caSApple OSS Distributions * 'Cause the whole kernel world depends on OSMetaClassBase and YOU
349*c54f35caSApple OSS Distributions * CANT change the VTABLE size ever.
350*c54f35caSApple OSS Distributions *********************************************************************/
351*c54f35caSApple OSS Distributions void
_RESERVEDOSMetaClassBase7()352*c54f35caSApple OSS Distributions OSMetaClassBase::_RESERVEDOSMetaClassBase7()
353*c54f35caSApple OSS Distributions {
354*c54f35caSApple OSS Distributions panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 7);
355*c54f35caSApple OSS Distributions }
356*c54f35caSApple OSS Distributions #endif
357*c54f35caSApple OSS Distributions
358*c54f35caSApple OSS Distributions /*********************************************************************
359*c54f35caSApple OSS Distributions *********************************************************************/
OSMetaClassBase()360*c54f35caSApple OSS Distributions OSMetaClassBase::OSMetaClassBase()
361*c54f35caSApple OSS Distributions {
362*c54f35caSApple OSS Distributions }
363*c54f35caSApple OSS Distributions
364*c54f35caSApple OSS Distributions /*********************************************************************
365*c54f35caSApple OSS Distributions *********************************************************************/
~OSMetaClassBase()366*c54f35caSApple OSS Distributions OSMetaClassBase::~OSMetaClassBase()
367*c54f35caSApple OSS Distributions {
368*c54f35caSApple OSS Distributions void ** thisVTable;
369*c54f35caSApple OSS Distributions
370*c54f35caSApple OSS Distributions thisVTable = (void **) this;
371*c54f35caSApple OSS Distributions *thisVTable = (void *) -1UL;
372*c54f35caSApple OSS Distributions }
373*c54f35caSApple OSS Distributions
374*c54f35caSApple OSS Distributions /*********************************************************************
375*c54f35caSApple OSS Distributions *********************************************************************/
376*c54f35caSApple OSS Distributions bool
isEqualTo(const OSMetaClassBase * anObj) const377*c54f35caSApple OSS Distributions OSMetaClassBase::isEqualTo(const OSMetaClassBase * anObj) const
378*c54f35caSApple OSS Distributions {
379*c54f35caSApple OSS Distributions return this == anObj;
380*c54f35caSApple OSS Distributions }
381*c54f35caSApple OSS Distributions
382*c54f35caSApple OSS Distributions /*********************************************************************
383*c54f35caSApple OSS Distributions *********************************************************************/
384*c54f35caSApple OSS Distributions OSMetaClassBase *
metaCast(const OSMetaClass * toMeta) const385*c54f35caSApple OSS Distributions OSMetaClassBase::metaCast(const OSMetaClass * toMeta) const
386*c54f35caSApple OSS Distributions {
387*c54f35caSApple OSS Distributions return toMeta->checkMetaCast(this);
388*c54f35caSApple OSS Distributions }
389*c54f35caSApple OSS Distributions
390*c54f35caSApple OSS Distributions /*********************************************************************
391*c54f35caSApple OSS Distributions *********************************************************************/
392*c54f35caSApple OSS Distributions OSMetaClassBase *
metaCast(const OSSymbol * toMetaSymb) const393*c54f35caSApple OSS Distributions OSMetaClassBase::metaCast(const OSSymbol * toMetaSymb) const
394*c54f35caSApple OSS Distributions {
395*c54f35caSApple OSS Distributions return OSMetaClass::checkMetaCastWithName(toMetaSymb, this);
396*c54f35caSApple OSS Distributions }
397*c54f35caSApple OSS Distributions
398*c54f35caSApple OSS Distributions /*********************************************************************
399*c54f35caSApple OSS Distributions *********************************************************************/
400*c54f35caSApple OSS Distributions OSMetaClassBase *
metaCast(const OSString * toMetaStr) const401*c54f35caSApple OSS Distributions OSMetaClassBase::metaCast(const OSString * toMetaStr) const
402*c54f35caSApple OSS Distributions {
403*c54f35caSApple OSS Distributions const OSSymbol * tempSymb = OSSymbol::withString(toMetaStr);
404*c54f35caSApple OSS Distributions OSMetaClassBase * ret = NULL;
405*c54f35caSApple OSS Distributions if (tempSymb) {
406*c54f35caSApple OSS Distributions ret = metaCast(tempSymb);
407*c54f35caSApple OSS Distributions tempSymb->release();
408*c54f35caSApple OSS Distributions }
409*c54f35caSApple OSS Distributions return ret;
410*c54f35caSApple OSS Distributions }
411*c54f35caSApple OSS Distributions
412*c54f35caSApple OSS Distributions /*********************************************************************
413*c54f35caSApple OSS Distributions *********************************************************************/
414*c54f35caSApple OSS Distributions OSMetaClassBase *
metaCast(const char * toMetaCStr) const415*c54f35caSApple OSS Distributions OSMetaClassBase::metaCast(const char * toMetaCStr) const
416*c54f35caSApple OSS Distributions {
417*c54f35caSApple OSS Distributions const OSSymbol * tempSymb = OSSymbol::withCString(toMetaCStr);
418*c54f35caSApple OSS Distributions OSMetaClassBase * ret = NULL;
419*c54f35caSApple OSS Distributions if (tempSymb) {
420*c54f35caSApple OSS Distributions ret = metaCast(tempSymb);
421*c54f35caSApple OSS Distributions tempSymb->release();
422*c54f35caSApple OSS Distributions }
423*c54f35caSApple OSS Distributions return ret;
424*c54f35caSApple OSS Distributions }
425*c54f35caSApple OSS Distributions
426*c54f35caSApple OSS Distributions #if PRAGMA_MARK
427*c54f35caSApple OSS Distributions #pragma mark OSMetaClassMeta
428*c54f35caSApple OSS Distributions #endif /* PRAGMA_MARK */
429*c54f35caSApple OSS Distributions /*********************************************************************
430*c54f35caSApple OSS Distributions * OSMetaClassMeta - the bootstrap metaclass of OSMetaClass
431*c54f35caSApple OSS Distributions *********************************************************************/
432*c54f35caSApple OSS Distributions class OSMetaClassMeta : public OSMetaClass
433*c54f35caSApple OSS Distributions {
434*c54f35caSApple OSS Distributions public:
435*c54f35caSApple OSS Distributions OSMetaClassMeta();
436*c54f35caSApple OSS Distributions OSObject * alloc() const;
437*c54f35caSApple OSS Distributions };
OSMetaClassMeta()438*c54f35caSApple OSS Distributions OSMetaClassMeta::OSMetaClassMeta()
439*c54f35caSApple OSS Distributions : OSMetaClass("OSMetaClass", NULL, sizeof(OSMetaClass))
440*c54f35caSApple OSS Distributions {
441*c54f35caSApple OSS Distributions }
442*c54f35caSApple OSS Distributions OSObject *
alloc() const443*c54f35caSApple OSS Distributions OSMetaClassMeta::alloc() const
444*c54f35caSApple OSS Distributions {
445*c54f35caSApple OSS Distributions return NULL;
446*c54f35caSApple OSS Distributions }
447*c54f35caSApple OSS Distributions
448*c54f35caSApple OSS Distributions static OSMetaClassMeta sOSMetaClassMeta;
449*c54f35caSApple OSS Distributions
450*c54f35caSApple OSS Distributions const OSMetaClass * const OSMetaClass::metaClass = &sOSMetaClassMeta;
451*c54f35caSApple OSS Distributions const OSMetaClass *
getMetaClass() const452*c54f35caSApple OSS Distributions OSMetaClass::getMetaClass() const
453*c54f35caSApple OSS Distributions {
454*c54f35caSApple OSS Distributions return &sOSMetaClassMeta;
455*c54f35caSApple OSS Distributions }
456*c54f35caSApple OSS Distributions
457*c54f35caSApple OSS Distributions #if PRAGMA_MARK
458*c54f35caSApple OSS Distributions #pragma mark OSMetaClass
459*c54f35caSApple OSS Distributions #endif /* PRAGMA_MARK */
460*c54f35caSApple OSS Distributions /*********************************************************************
461*c54f35caSApple OSS Distributions * OSMetaClass
462*c54f35caSApple OSS Distributions *********************************************************************/
463*c54f35caSApple OSS Distributions
464*c54f35caSApple OSS Distributions #if APPLE_KEXT_VTABLE_PADDING
465*c54f35caSApple OSS Distributions /*********************************************************************
466*c54f35caSApple OSS Distributions * Reserved functions.
467*c54f35caSApple OSS Distributions *********************************************************************/
468*c54f35caSApple OSS Distributions void
_RESERVEDOSMetaClass0()469*c54f35caSApple OSS Distributions OSMetaClass::_RESERVEDOSMetaClass0()
470*c54f35caSApple OSS Distributions {
471*c54f35caSApple OSS Distributions panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 0);
472*c54f35caSApple OSS Distributions }
473*c54f35caSApple OSS Distributions void
_RESERVEDOSMetaClass1()474*c54f35caSApple OSS Distributions OSMetaClass::_RESERVEDOSMetaClass1()
475*c54f35caSApple OSS Distributions {
476*c54f35caSApple OSS Distributions panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 1);
477*c54f35caSApple OSS Distributions }
478*c54f35caSApple OSS Distributions void
_RESERVEDOSMetaClass2()479*c54f35caSApple OSS Distributions OSMetaClass::_RESERVEDOSMetaClass2()
480*c54f35caSApple OSS Distributions {
481*c54f35caSApple OSS Distributions panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 2);
482*c54f35caSApple OSS Distributions }
483*c54f35caSApple OSS Distributions void
_RESERVEDOSMetaClass3()484*c54f35caSApple OSS Distributions OSMetaClass::_RESERVEDOSMetaClass3()
485*c54f35caSApple OSS Distributions {
486*c54f35caSApple OSS Distributions panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 3);
487*c54f35caSApple OSS Distributions }
488*c54f35caSApple OSS Distributions void
_RESERVEDOSMetaClass4()489*c54f35caSApple OSS Distributions OSMetaClass::_RESERVEDOSMetaClass4()
490*c54f35caSApple OSS Distributions {
491*c54f35caSApple OSS Distributions panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 4);
492*c54f35caSApple OSS Distributions }
493*c54f35caSApple OSS Distributions void
_RESERVEDOSMetaClass5()494*c54f35caSApple OSS Distributions OSMetaClass::_RESERVEDOSMetaClass5()
495*c54f35caSApple OSS Distributions {
496*c54f35caSApple OSS Distributions panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 5);
497*c54f35caSApple OSS Distributions }
498*c54f35caSApple OSS Distributions void
_RESERVEDOSMetaClass6()499*c54f35caSApple OSS Distributions OSMetaClass::_RESERVEDOSMetaClass6()
500*c54f35caSApple OSS Distributions {
501*c54f35caSApple OSS Distributions panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 6);
502*c54f35caSApple OSS Distributions }
503*c54f35caSApple OSS Distributions void
_RESERVEDOSMetaClass7()504*c54f35caSApple OSS Distributions OSMetaClass::_RESERVEDOSMetaClass7()
505*c54f35caSApple OSS Distributions {
506*c54f35caSApple OSS Distributions panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 7);
507*c54f35caSApple OSS Distributions }
508*c54f35caSApple OSS Distributions #endif
509*c54f35caSApple OSS Distributions
510*c54f35caSApple OSS Distributions /*********************************************************************
511*c54f35caSApple OSS Distributions *********************************************************************/
512*c54f35caSApple OSS Distributions static void
OSMetaClassLogErrorForKext(OSReturn error,OSKext * aKext)513*c54f35caSApple OSS Distributions OSMetaClassLogErrorForKext(
514*c54f35caSApple OSS Distributions OSReturn error,
515*c54f35caSApple OSS Distributions OSKext * aKext)
516*c54f35caSApple OSS Distributions {
517*c54f35caSApple OSS Distributions const char * message = NULL;
518*c54f35caSApple OSS Distributions
519*c54f35caSApple OSS Distributions switch (error) {
520*c54f35caSApple OSS Distributions case kOSReturnSuccess:
521*c54f35caSApple OSS Distributions return;
522*c54f35caSApple OSS Distributions case kOSMetaClassNoInit: // xxx - never returned; logged at fail site
523*c54f35caSApple OSS Distributions message = "OSMetaClass: preModLoad() wasn't called (runtime internal error).";
524*c54f35caSApple OSS Distributions break;
525*c54f35caSApple OSS Distributions case kOSMetaClassNoDicts:
526*c54f35caSApple OSS Distributions message = "OSMetaClass: Allocation failure for OSMetaClass internal dictionaries.";
527*c54f35caSApple OSS Distributions break;
528*c54f35caSApple OSS Distributions case kOSMetaClassNoKModSet:
529*c54f35caSApple OSS Distributions message = "OSMetaClass: Allocation failure for internal kext recording set/set missing.";
530*c54f35caSApple OSS Distributions break;
531*c54f35caSApple OSS Distributions case kOSMetaClassNoInsKModSet:
532*c54f35caSApple OSS Distributions message = "OSMetaClass: Failed to record class in kext.";
533*c54f35caSApple OSS Distributions break;
534*c54f35caSApple OSS Distributions case kOSMetaClassDuplicateClass:
535*c54f35caSApple OSS Distributions message = "OSMetaClass: Duplicate class encountered.";
536*c54f35caSApple OSS Distributions break;
537*c54f35caSApple OSS Distributions case kOSMetaClassNoSuper: // xxx - never returned
538*c54f35caSApple OSS Distributions message = "OSMetaClass: Can't associate a class with its superclass.";
539*c54f35caSApple OSS Distributions break;
540*c54f35caSApple OSS Distributions case kOSMetaClassInstNoSuper: // xxx - never returned
541*c54f35caSApple OSS Distributions message = "OSMetaClass: Instance construction error; unknown superclass.";
542*c54f35caSApple OSS Distributions break;
543*c54f35caSApple OSS Distributions case kOSMetaClassNoKext:
544*c54f35caSApple OSS Distributions message = "OSMetaClass: Kext not found for metaclass.";
545*c54f35caSApple OSS Distributions break;
546*c54f35caSApple OSS Distributions case kOSMetaClassInternal:
547*c54f35caSApple OSS Distributions default:
548*c54f35caSApple OSS Distributions message = "OSMetaClass: Runtime internal error.";
549*c54f35caSApple OSS Distributions break;
550*c54f35caSApple OSS Distributions }
551*c54f35caSApple OSS Distributions
552*c54f35caSApple OSS Distributions if (message) {
553*c54f35caSApple OSS Distributions OSKextLog(aKext, kOSMetaClassLogSpec, "%s", message);
554*c54f35caSApple OSS Distributions }
555*c54f35caSApple OSS Distributions return;
556*c54f35caSApple OSS Distributions }
557*c54f35caSApple OSS Distributions
558*c54f35caSApple OSS Distributions void
logError(OSReturn error)559*c54f35caSApple OSS Distributions OSMetaClass::logError(OSReturn error)
560*c54f35caSApple OSS Distributions {
561*c54f35caSApple OSS Distributions OSMetaClassLogErrorForKext(error, NULL);
562*c54f35caSApple OSS Distributions }
563*c54f35caSApple OSS Distributions
564*c54f35caSApple OSS Distributions /*********************************************************************
565*c54f35caSApple OSS Distributions * The core constructor for a MetaClass (defined with this name always
566*c54f35caSApple OSS Distributions * but within the scope of its represented class).
567*c54f35caSApple OSS Distributions *
568*c54f35caSApple OSS Distributions * MetaClass constructors are invoked in OSRuntimeInitializeCPP(),
569*c54f35caSApple OSS Distributions * in between calls to OSMetaClass::preModLoad(), which sets up for
570*c54f35caSApple OSS Distributions * registration, and OSMetaClass::postModLoad(), which actually
571*c54f35caSApple OSS Distributions * records all the class/kext relationships of the new MetaClasses.
572*c54f35caSApple OSS Distributions *********************************************************************/
573*c54f35caSApple OSS Distributions
OSMetaClass(const char * inClassName,const OSMetaClass * inSuperClass,unsigned int inClassSize)574*c54f35caSApple OSS Distributions OSMetaClass::OSMetaClass(
575*c54f35caSApple OSS Distributions const char * inClassName,
576*c54f35caSApple OSS Distributions const OSMetaClass * inSuperClass,
577*c54f35caSApple OSS Distributions unsigned int inClassSize)
578*c54f35caSApple OSS Distributions {
579*c54f35caSApple OSS Distributions instanceCount = 0;
580*c54f35caSApple OSS Distributions classSize = inClassSize;
581*c54f35caSApple OSS Distributions superClassLink = inSuperClass;
582*c54f35caSApple OSS Distributions
583*c54f35caSApple OSS Distributions reserved = IOMallocType(ExpansionData);
584*c54f35caSApple OSS Distributions #if IOTRACKING
585*c54f35caSApple OSS Distributions uint32_t numSiteQs = 0;
586*c54f35caSApple OSS Distributions if ((this == &OSSymbol ::gMetaClass)
587*c54f35caSApple OSS Distributions || (this == &OSString ::gMetaClass)
588*c54f35caSApple OSS Distributions || (this == &OSNumber ::gMetaClass)
589*c54f35caSApple OSS Distributions || (this == &OSString ::gMetaClass)
590*c54f35caSApple OSS Distributions || (this == &OSData ::gMetaClass)
591*c54f35caSApple OSS Distributions || (this == &OSDictionary::gMetaClass)
592*c54f35caSApple OSS Distributions || (this == &OSArray ::gMetaClass)
593*c54f35caSApple OSS Distributions || (this == &OSSet ::gMetaClass)) {
594*c54f35caSApple OSS Distributions numSiteQs = 27;
595*c54f35caSApple OSS Distributions }
596*c54f35caSApple OSS Distributions
597*c54f35caSApple OSS Distributions reserved->tracking = IOTrackingQueueAlloc(inClassName, (uintptr_t) this,
598*c54f35caSApple OSS Distributions inClassSize, 0, kIOTrackingQueueTypeAlloc,
599*c54f35caSApple OSS Distributions numSiteQs);
600*c54f35caSApple OSS Distributions #endif
601*c54f35caSApple OSS Distributions
602*c54f35caSApple OSS Distributions /* Hack alert: We are just casting inClassName and storing it in
603*c54f35caSApple OSS Distributions * an OSString * instance variable. This may be because you can't
604*c54f35caSApple OSS Distributions * create C++ objects in static constructors, but I really don't know!
605*c54f35caSApple OSS Distributions */
606*c54f35caSApple OSS Distributions className = (const OSSymbol *)inClassName;
607*c54f35caSApple OSS Distributions
608*c54f35caSApple OSS Distributions // sStalledClassesLock taken in preModLoad
609*c54f35caSApple OSS Distributions if (!sStalled) {
610*c54f35caSApple OSS Distributions /* There's no way we can look up the kext here, unfortunately.
611*c54f35caSApple OSS Distributions */
612*c54f35caSApple OSS Distributions OSKextLog(/* kext */ NULL, kOSMetaClassLogSpec,
613*c54f35caSApple OSS Distributions "OSMetaClass: preModLoad() wasn't called for class %s "
614*c54f35caSApple OSS Distributions "(runtime internal error).",
615*c54f35caSApple OSS Distributions inClassName);
616*c54f35caSApple OSS Distributions } else if (!sStalled->result) {
617*c54f35caSApple OSS Distributions // Grow stalled array if neccessary
618*c54f35caSApple OSS Distributions if (sStalled->count >= sStalled->capacity) {
619*c54f35caSApple OSS Distributions OSMetaClass **oldStalled = sStalled->classes;
620*c54f35caSApple OSS Distributions int oldCount = sStalled->capacity;
621*c54f35caSApple OSS Distributions int newCount = oldCount + kKModCapacityIncrement;
622*c54f35caSApple OSS Distributions
623*c54f35caSApple OSS Distributions sStalled->classes = kalloc_type_tag(OSMetaClass *, newCount,
624*c54f35caSApple OSS Distributions Z_WAITOK_ZERO, VM_KERN_MEMORY_OSKEXT);
625*c54f35caSApple OSS Distributions if (!sStalled->classes) {
626*c54f35caSApple OSS Distributions sStalled->classes = oldStalled;
627*c54f35caSApple OSS Distributions sStalled->result = kOSMetaClassNoTempData;
628*c54f35caSApple OSS Distributions return;
629*c54f35caSApple OSS Distributions }
630*c54f35caSApple OSS Distributions
631*c54f35caSApple OSS Distributions sStalled->capacity = newCount;
632*c54f35caSApple OSS Distributions memmove(sStalled->classes, oldStalled,
633*c54f35caSApple OSS Distributions sizeof(OSMetaClass *) * oldCount);
634*c54f35caSApple OSS Distributions kfree_type(OSMetaClass *, oldCount, oldStalled);
635*c54f35caSApple OSS Distributions OSMETA_ACCUMSIZE(sizeof(OSMetaClass *) * (newCount - oldCount));
636*c54f35caSApple OSS Distributions }
637*c54f35caSApple OSS Distributions
638*c54f35caSApple OSS Distributions sStalled->classes[sStalled->count++] = this;
639*c54f35caSApple OSS Distributions }
640*c54f35caSApple OSS Distributions }
641*c54f35caSApple OSS Distributions
OSMetaClass(const char * inClassName,const OSMetaClass * inSuperClass,unsigned int inClassSize,zone_t * inZone,const char * zone_name,zone_create_flags_t zflags)642*c54f35caSApple OSS Distributions OSMetaClass::OSMetaClass(
643*c54f35caSApple OSS Distributions const char * inClassName,
644*c54f35caSApple OSS Distributions const OSMetaClass * inSuperClass,
645*c54f35caSApple OSS Distributions unsigned int inClassSize,
646*c54f35caSApple OSS Distributions zone_t * inZone,
647*c54f35caSApple OSS Distributions const char * zone_name,
648*c54f35caSApple OSS Distributions zone_create_flags_t zflags) : OSMetaClass(inClassName, inSuperClass,
649*c54f35caSApple OSS Distributions inClassSize)
650*c54f35caSApple OSS Distributions {
651*c54f35caSApple OSS Distributions if (!(kIOTracking & gIOKitDebug)) {
652*c54f35caSApple OSS Distributions *inZone = zone_create(zone_name, inClassSize,
653*c54f35caSApple OSS Distributions (zone_create_flags_t) (ZC_ZFREE_CLEARMEM | zflags));
654*c54f35caSApple OSS Distributions }
655*c54f35caSApple OSS Distributions }
656*c54f35caSApple OSS Distributions
657*c54f35caSApple OSS Distributions /*********************************************************************
658*c54f35caSApple OSS Distributions *********************************************************************/
~OSMetaClass()659*c54f35caSApple OSS Distributions OSMetaClass::~OSMetaClass()
660*c54f35caSApple OSS Distributions {
661*c54f35caSApple OSS Distributions OSKext * myKext = reserved->kext; // do not release
662*c54f35caSApple OSS Distributions
663*c54f35caSApple OSS Distributions /* Hack alert: 'className' is a C string during early C++ init, and
664*c54f35caSApple OSS Distributions * is converted to a real OSSymbol only when we record the OSKext in
665*c54f35caSApple OSS Distributions * OSMetaClass::postModLoad(). So only do this bit if we have an OSKext.
666*c54f35caSApple OSS Distributions * We can't safely cast or check 'className'.
667*c54f35caSApple OSS Distributions *
668*c54f35caSApple OSS Distributions * Also, release className *after* calling into the kext,
669*c54f35caSApple OSS Distributions * as removeClass() may access className.
670*c54f35caSApple OSS Distributions */
671*c54f35caSApple OSS Distributions IOLockLock(sAllClassesLock);
672*c54f35caSApple OSS Distributions if (sAllClassesDict) {
673*c54f35caSApple OSS Distributions if (myKext) {
674*c54f35caSApple OSS Distributions sAllClassesDict->removeObject(className);
675*c54f35caSApple OSS Distributions } else {
676*c54f35caSApple OSS Distributions sAllClassesDict->removeObject((const char *)className);
677*c54f35caSApple OSS Distributions }
678*c54f35caSApple OSS Distributions }
679*c54f35caSApple OSS Distributions IOLockUnlock(sAllClassesLock);
680*c54f35caSApple OSS Distributions
681*c54f35caSApple OSS Distributions if (myKext) {
682*c54f35caSApple OSS Distributions if (myKext->removeClass(this) != kOSReturnSuccess) {
683*c54f35caSApple OSS Distributions // xxx - what can we do?
684*c54f35caSApple OSS Distributions }
685*c54f35caSApple OSS Distributions className->release();
686*c54f35caSApple OSS Distributions }
687*c54f35caSApple OSS Distributions
688*c54f35caSApple OSS Distributions // sStalledClassesLock taken in preModLoad
689*c54f35caSApple OSS Distributions if (sStalled) {
690*c54f35caSApple OSS Distributions unsigned int i;
691*c54f35caSApple OSS Distributions
692*c54f35caSApple OSS Distributions /* First pass find class in stalled list. If we find it that means
693*c54f35caSApple OSS Distributions * we started C++ init with constructors but now we're tearing down
694*c54f35caSApple OSS Distributions * because of some failure.
695*c54f35caSApple OSS Distributions */
696*c54f35caSApple OSS Distributions for (i = 0; i < sStalled->count; i++) {
697*c54f35caSApple OSS Distributions if (this == sStalled->classes[i]) {
698*c54f35caSApple OSS Distributions break;
699*c54f35caSApple OSS Distributions }
700*c54f35caSApple OSS Distributions }
701*c54f35caSApple OSS Distributions
702*c54f35caSApple OSS Distributions /* Remove this metaclass from the stalled list so postModLoad() doesn't
703*c54f35caSApple OSS Distributions * try to register it.
704*c54f35caSApple OSS Distributions */
705*c54f35caSApple OSS Distributions if (i < sStalled->count) {
706*c54f35caSApple OSS Distributions sStalled->count--;
707*c54f35caSApple OSS Distributions if (i < sStalled->count) {
708*c54f35caSApple OSS Distributions memmove(&sStalled->classes[i], &sStalled->classes[i + 1],
709*c54f35caSApple OSS Distributions (sStalled->count - i) * sizeof(OSMetaClass *));
710*c54f35caSApple OSS Distributions }
711*c54f35caSApple OSS Distributions }
712*c54f35caSApple OSS Distributions }
713*c54f35caSApple OSS Distributions #if IOTRACKING
714*c54f35caSApple OSS Distributions IOTrackingQueueFree(reserved->tracking);
715*c54f35caSApple OSS Distributions #endif
716*c54f35caSApple OSS Distributions IOFreeType(reserved, ExpansionData);
717*c54f35caSApple OSS Distributions }
718*c54f35caSApple OSS Distributions
719*c54f35caSApple OSS Distributions /*********************************************************************
720*c54f35caSApple OSS Distributions * Empty overrides.
721*c54f35caSApple OSS Distributions *********************************************************************/
722*c54f35caSApple OSS Distributions void
retain() const723*c54f35caSApple OSS Distributions OSMetaClass::retain() const
724*c54f35caSApple OSS Distributions {
725*c54f35caSApple OSS Distributions }
726*c54f35caSApple OSS Distributions void
release() const727*c54f35caSApple OSS Distributions OSMetaClass::release() const
728*c54f35caSApple OSS Distributions {
729*c54f35caSApple OSS Distributions }
730*c54f35caSApple OSS Distributions void
release(__unused int when) const731*c54f35caSApple OSS Distributions OSMetaClass::release(__unused int when) const
732*c54f35caSApple OSS Distributions {
733*c54f35caSApple OSS Distributions }
734*c54f35caSApple OSS Distributions void
taggedRetain(__unused const void * tag) const735*c54f35caSApple OSS Distributions OSMetaClass::taggedRetain(__unused const void * tag) const
736*c54f35caSApple OSS Distributions {
737*c54f35caSApple OSS Distributions }
738*c54f35caSApple OSS Distributions void
taggedRelease(__unused const void * tag) const739*c54f35caSApple OSS Distributions OSMetaClass::taggedRelease(__unused const void * tag) const
740*c54f35caSApple OSS Distributions {
741*c54f35caSApple OSS Distributions }
742*c54f35caSApple OSS Distributions void
taggedRelease(__unused const void * tag,__unused const int when) const743*c54f35caSApple OSS Distributions OSMetaClass::taggedRelease(__unused const void * tag, __unused const int when) const
744*c54f35caSApple OSS Distributions {
745*c54f35caSApple OSS Distributions }
746*c54f35caSApple OSS Distributions int
getRetainCount() const747*c54f35caSApple OSS Distributions OSMetaClass::getRetainCount() const
748*c54f35caSApple OSS Distributions {
749*c54f35caSApple OSS Distributions return 0;
750*c54f35caSApple OSS Distributions }
751*c54f35caSApple OSS Distributions
752*c54f35caSApple OSS Distributions /*********************************************************************
753*c54f35caSApple OSS Distributions *********************************************************************/
754*c54f35caSApple OSS Distributions const char *
getClassName() const755*c54f35caSApple OSS Distributions OSMetaClass::getClassName() const
756*c54f35caSApple OSS Distributions {
757*c54f35caSApple OSS Distributions if (!className) {
758*c54f35caSApple OSS Distributions return NULL;
759*c54f35caSApple OSS Distributions }
760*c54f35caSApple OSS Distributions return className->getCStringNoCopy();
761*c54f35caSApple OSS Distributions }
762*c54f35caSApple OSS Distributions /*********************************************************************
763*c54f35caSApple OSS Distributions *********************************************************************/
764*c54f35caSApple OSS Distributions const OSSymbol *
getClassNameSymbol() const765*c54f35caSApple OSS Distributions OSMetaClass::getClassNameSymbol() const
766*c54f35caSApple OSS Distributions {
767*c54f35caSApple OSS Distributions return className;
768*c54f35caSApple OSS Distributions }
769*c54f35caSApple OSS Distributions /*********************************************************************
770*c54f35caSApple OSS Distributions *********************************************************************/
771*c54f35caSApple OSS Distributions unsigned int
getClassSize() const772*c54f35caSApple OSS Distributions OSMetaClass::getClassSize() const
773*c54f35caSApple OSS Distributions {
774*c54f35caSApple OSS Distributions return classSize;
775*c54f35caSApple OSS Distributions }
776*c54f35caSApple OSS Distributions
777*c54f35caSApple OSS Distributions /*********************************************************************
778*c54f35caSApple OSS Distributions *********************************************************************/
779*c54f35caSApple OSS Distributions void *
preModLoad(const char * kextIdentifier)780*c54f35caSApple OSS Distributions OSMetaClass::preModLoad(const char * kextIdentifier)
781*c54f35caSApple OSS Distributions {
782*c54f35caSApple OSS Distributions IOLockLock(sStalledClassesLock);
783*c54f35caSApple OSS Distributions
784*c54f35caSApple OSS Distributions assert(sStalled == NULL);
785*c54f35caSApple OSS Distributions sStalled = kalloc_type(StalledData, Z_WAITOK_ZERO_NOFAIL);
786*c54f35caSApple OSS Distributions
787*c54f35caSApple OSS Distributions sStalled->classes = kalloc_type_tag(OSMetaClass *,
788*c54f35caSApple OSS Distributions kKModCapacityIncrement, Z_WAITOK_ZERO, VM_KERN_MEMORY_OSKEXT);
789*c54f35caSApple OSS Distributions if (!sStalled->classes) {
790*c54f35caSApple OSS Distributions kfree_type(StalledData, sStalled);
791*c54f35caSApple OSS Distributions return NULL;
792*c54f35caSApple OSS Distributions }
793*c54f35caSApple OSS Distributions OSMETA_ACCUMSIZE((kKModCapacityIncrement * sizeof(OSMetaClass *)) +
794*c54f35caSApple OSS Distributions sizeof(*sStalled));
795*c54f35caSApple OSS Distributions
796*c54f35caSApple OSS Distributions sStalled->result = kOSReturnSuccess;
797*c54f35caSApple OSS Distributions sStalled->capacity = kKModCapacityIncrement;
798*c54f35caSApple OSS Distributions sStalled->count = 0;
799*c54f35caSApple OSS Distributions sStalled->kextIdentifier = kextIdentifier;
800*c54f35caSApple OSS Distributions
801*c54f35caSApple OSS Distributions // keep sStalledClassesLock locked until postModLoad
802*c54f35caSApple OSS Distributions
803*c54f35caSApple OSS Distributions return sStalled;
804*c54f35caSApple OSS Distributions }
805*c54f35caSApple OSS Distributions
806*c54f35caSApple OSS Distributions /*********************************************************************
807*c54f35caSApple OSS Distributions *********************************************************************/
808*c54f35caSApple OSS Distributions bool
checkModLoad(void * loadHandle)809*c54f35caSApple OSS Distributions OSMetaClass::checkModLoad(void * loadHandle)
810*c54f35caSApple OSS Distributions {
811*c54f35caSApple OSS Distributions return sStalled && loadHandle == sStalled &&
812*c54f35caSApple OSS Distributions sStalled->result == kOSReturnSuccess;
813*c54f35caSApple OSS Distributions }
814*c54f35caSApple OSS Distributions
815*c54f35caSApple OSS Distributions /*********************************************************************
816*c54f35caSApple OSS Distributions *********************************************************************/
817*c54f35caSApple OSS Distributions OSReturn
postModLoad(void * loadHandle)818*c54f35caSApple OSS Distributions OSMetaClass::postModLoad(void * loadHandle)
819*c54f35caSApple OSS Distributions {
820*c54f35caSApple OSS Distributions OSReturn result = kOSReturnSuccess;
821*c54f35caSApple OSS Distributions OSSymbol * myKextName = NULL;// must release
822*c54f35caSApple OSS Distributions OSKext * myKext = NULL;// must release
823*c54f35caSApple OSS Distributions
824*c54f35caSApple OSS Distributions if (!sStalled || loadHandle != sStalled) {
825*c54f35caSApple OSS Distributions result = kOSMetaClassInternal;
826*c54f35caSApple OSS Distributions goto finish;
827*c54f35caSApple OSS Distributions }
828*c54f35caSApple OSS Distributions
829*c54f35caSApple OSS Distributions if (sStalled->result) {
830*c54f35caSApple OSS Distributions result = sStalled->result;
831*c54f35caSApple OSS Distributions } else {
832*c54f35caSApple OSS Distributions switch (sBootstrapState) {
833*c54f35caSApple OSS Distributions case kNoDictionaries:
834*c54f35caSApple OSS Distributions sBootstrapState = kMakingDictionaries;
835*c54f35caSApple OSS Distributions // No break; fall through
836*c54f35caSApple OSS Distributions [[clang::fallthrough]];
837*c54f35caSApple OSS Distributions
838*c54f35caSApple OSS Distributions case kMakingDictionaries:
839*c54f35caSApple OSS Distributions sAllClassesDict = OSDictionary::withCapacity(kClassCapacityIncrement);
840*c54f35caSApple OSS Distributions if (!sAllClassesDict) {
841*c54f35caSApple OSS Distributions result = kOSMetaClassNoDicts;
842*c54f35caSApple OSS Distributions break;
843*c54f35caSApple OSS Distributions }
844*c54f35caSApple OSS Distributions sAllClassesDict->setOptions(OSCollection::kSort, OSCollection::kSort);
845*c54f35caSApple OSS Distributions
846*c54f35caSApple OSS Distributions // No break; fall through
847*c54f35caSApple OSS Distributions [[clang::fallthrough]];
848*c54f35caSApple OSS Distributions
849*c54f35caSApple OSS Distributions case kCompletedBootstrap:
850*c54f35caSApple OSS Distributions {
851*c54f35caSApple OSS Distributions unsigned int i;
852*c54f35caSApple OSS Distributions myKextName = const_cast<OSSymbol *>(OSSymbol::withCStringNoCopy(
853*c54f35caSApple OSS Distributions sStalled->kextIdentifier));
854*c54f35caSApple OSS Distributions
855*c54f35caSApple OSS Distributions if (!sStalled->count) {
856*c54f35caSApple OSS Distributions break; // Nothing to do so just get out
857*c54f35caSApple OSS Distributions }
858*c54f35caSApple OSS Distributions
859*c54f35caSApple OSS Distributions myKext = OSKext::lookupKextWithIdentifier(myKextName);
860*c54f35caSApple OSS Distributions if (!myKext) {
861*c54f35caSApple OSS Distributions result = kOSMetaClassNoKext;
862*c54f35caSApple OSS Distributions
863*c54f35caSApple OSS Distributions /* Log this error here so we can include the kext name.
864*c54f35caSApple OSS Distributions */
865*c54f35caSApple OSS Distributions OSKextLog(/* kext */ NULL, kOSMetaClassLogSpec,
866*c54f35caSApple OSS Distributions "OSMetaClass: Can't record classes for kext %s - kext not found.",
867*c54f35caSApple OSS Distributions sStalled->kextIdentifier);
868*c54f35caSApple OSS Distributions break;
869*c54f35caSApple OSS Distributions }
870*c54f35caSApple OSS Distributions
871*c54f35caSApple OSS Distributions /* First pass checking classes aren't already loaded. If any already
872*c54f35caSApple OSS Distributions * exist, we don't register any, and so we don't technically have
873*c54f35caSApple OSS Distributions * to do any C++ teardown.
874*c54f35caSApple OSS Distributions *
875*c54f35caSApple OSS Distributions * Hack alert: me->className has been a C string until now.
876*c54f35caSApple OSS Distributions * We only release the OSSymbol if we store the kext.
877*c54f35caSApple OSS Distributions */
878*c54f35caSApple OSS Distributions IOLockLock(sAllClassesLock);
879*c54f35caSApple OSS Distributions for (i = 0; i < sStalled->count; i++) {
880*c54f35caSApple OSS Distributions const OSMetaClass * me = sStalled->classes[i];
881*c54f35caSApple OSS Distributions OSMetaClass * orig = OSDynamicCast(OSMetaClass,
882*c54f35caSApple OSS Distributions sAllClassesDict->getObject((const char *)me->className));
883*c54f35caSApple OSS Distributions
884*c54f35caSApple OSS Distributions if (orig) {
885*c54f35caSApple OSS Distributions /* Log this error here so we can include the class name.
886*c54f35caSApple OSS Distributions * xxx - we should look up the other kext that defines the class
887*c54f35caSApple OSS Distributions */
888*c54f35caSApple OSS Distributions #if defined(XNU_TARGET_OS_OSX)
889*c54f35caSApple OSS Distributions OSKextLog(myKext, kOSMetaClassLogSpec,
890*c54f35caSApple OSS Distributions #else
891*c54f35caSApple OSS Distributions panic(
892*c54f35caSApple OSS Distributions #endif /* defined(XNU_TARGET_OS_OSX) */
893*c54f35caSApple OSS Distributions "OSMetaClass: Kext %s class %s is a duplicate;"
894*c54f35caSApple OSS Distributions "kext %s already has a class by that name.",
895*c54f35caSApple OSS Distributions sStalled->kextIdentifier, (const char *)me->className,
896*c54f35caSApple OSS Distributions ((OSKext *)orig->reserved->kext)->getIdentifierCString());
897*c54f35caSApple OSS Distributions result = kOSMetaClassDuplicateClass;
898*c54f35caSApple OSS Distributions break;
899*c54f35caSApple OSS Distributions }
900*c54f35caSApple OSS Distributions unsigned int depth = 1;
901*c54f35caSApple OSS Distributions while ((me = me->superClassLink)) {
902*c54f35caSApple OSS Distributions depth++;
903*c54f35caSApple OSS Distributions }
904*c54f35caSApple OSS Distributions if (depth > sDeepestClass) {
905*c54f35caSApple OSS Distributions sDeepestClass = depth;
906*c54f35caSApple OSS Distributions }
907*c54f35caSApple OSS Distributions }
908*c54f35caSApple OSS Distributions IOLockUnlock(sAllClassesLock);
909*c54f35caSApple OSS Distributions
910*c54f35caSApple OSS Distributions /* Bail if we didn't go through the entire list of new classes
911*c54f35caSApple OSS Distributions * (if we hit a duplicate).
912*c54f35caSApple OSS Distributions */
913*c54f35caSApple OSS Distributions if (i != sStalled->count) {
914*c54f35caSApple OSS Distributions break;
915*c54f35caSApple OSS Distributions }
916*c54f35caSApple OSS Distributions
917*c54f35caSApple OSS Distributions // Second pass symbolling strings and inserting classes in dictionary
918*c54f35caSApple OSS Distributions IOLockLock(sAllClassesLock);
919*c54f35caSApple OSS Distributions for (i = 0; i < sStalled->count; i++) {
920*c54f35caSApple OSS Distributions OSMetaClass * me = sStalled->classes[i];
921*c54f35caSApple OSS Distributions
922*c54f35caSApple OSS Distributions /* Hack alert: me->className has been a C string until now.
923*c54f35caSApple OSS Distributions * We only release the OSSymbol in ~OSMetaClass()
924*c54f35caSApple OSS Distributions * if we set the reference to the kext.
925*c54f35caSApple OSS Distributions */
926*c54f35caSApple OSS Distributions me->className =
927*c54f35caSApple OSS Distributions OSSymbol::withCStringNoCopy((const char *)me->className);
928*c54f35caSApple OSS Distributions
929*c54f35caSApple OSS Distributions // xxx - I suppose if these fail we're going to panic soon....
930*c54f35caSApple OSS Distributions sAllClassesDict->setObject(me->className, me);
931*c54f35caSApple OSS Distributions
932*c54f35caSApple OSS Distributions /* Do not retain the kext object here.
933*c54f35caSApple OSS Distributions */
934*c54f35caSApple OSS Distributions me->reserved->kext = myKext;
935*c54f35caSApple OSS Distributions if (myKext) {
936*c54f35caSApple OSS Distributions result = myKext->addClass(me, sStalled->count);
937*c54f35caSApple OSS Distributions if (result != kOSReturnSuccess) {
938*c54f35caSApple OSS Distributions /* OSKext::addClass() logs with kOSMetaClassNoInsKModSet. */
939*c54f35caSApple OSS Distributions break;
940*c54f35caSApple OSS Distributions }
941*c54f35caSApple OSS Distributions }
942*c54f35caSApple OSS Distributions }
943*c54f35caSApple OSS Distributions IOLockUnlock(sAllClassesLock);
944*c54f35caSApple OSS Distributions sBootstrapState = kCompletedBootstrap;
945*c54f35caSApple OSS Distributions break;
946*c54f35caSApple OSS Distributions }
947*c54f35caSApple OSS Distributions
948*c54f35caSApple OSS Distributions default:
949*c54f35caSApple OSS Distributions result = kOSMetaClassInternal;
950*c54f35caSApple OSS Distributions break;
951*c54f35caSApple OSS Distributions }
952*c54f35caSApple OSS Distributions }
953*c54f35caSApple OSS Distributions
954*c54f35caSApple OSS Distributions finish:
955*c54f35caSApple OSS Distributions /* Don't call logError() for success or the conditions logged above
956*c54f35caSApple OSS Distributions * or by called function.
957*c54f35caSApple OSS Distributions */
958*c54f35caSApple OSS Distributions if (result != kOSReturnSuccess &&
959*c54f35caSApple OSS Distributions result != kOSMetaClassNoInsKModSet &&
960*c54f35caSApple OSS Distributions result != kOSMetaClassDuplicateClass &&
961*c54f35caSApple OSS Distributions result != kOSMetaClassNoKext) {
962*c54f35caSApple OSS Distributions OSMetaClassLogErrorForKext(result, myKext);
963*c54f35caSApple OSS Distributions }
964*c54f35caSApple OSS Distributions
965*c54f35caSApple OSS Distributions OSSafeReleaseNULL(myKextName);
966*c54f35caSApple OSS Distributions OSSafeReleaseNULL(myKext);
967*c54f35caSApple OSS Distributions
968*c54f35caSApple OSS Distributions if (sStalled) {
969*c54f35caSApple OSS Distributions OSMETA_ACCUMSIZE(-(sStalled->capacity * sizeof(OSMetaClass *) +
970*c54f35caSApple OSS Distributions sizeof(*sStalled)));
971*c54f35caSApple OSS Distributions kfree_type(OSMetaClass *, sStalled->capacity, sStalled->classes);
972*c54f35caSApple OSS Distributions kfree_type(StalledData, sStalled);
973*c54f35caSApple OSS Distributions sStalled = NULL;
974*c54f35caSApple OSS Distributions }
975*c54f35caSApple OSS Distributions
976*c54f35caSApple OSS Distributions IOLockUnlock(sStalledClassesLock);
977*c54f35caSApple OSS Distributions
978*c54f35caSApple OSS Distributions return result;
979*c54f35caSApple OSS Distributions }
980*c54f35caSApple OSS Distributions
981*c54f35caSApple OSS Distributions
982*c54f35caSApple OSS Distributions /*********************************************************************
983*c54f35caSApple OSS Distributions *********************************************************************/
984*c54f35caSApple OSS Distributions void
instanceConstructed() const985*c54f35caSApple OSS Distributions OSMetaClass::instanceConstructed() const
986*c54f35caSApple OSS Distributions {
987*c54f35caSApple OSS Distributions // if ((0 == OSIncrementAtomic(&(((OSMetaClass *) this)->instanceCount))) && superClassLink)
988*c54f35caSApple OSS Distributions if ((0 == OSIncrementAtomic(&instanceCount)) && superClassLink) {
989*c54f35caSApple OSS Distributions superClassLink->instanceConstructed();
990*c54f35caSApple OSS Distributions }
991*c54f35caSApple OSS Distributions }
992*c54f35caSApple OSS Distributions
993*c54f35caSApple OSS Distributions /*********************************************************************
994*c54f35caSApple OSS Distributions *********************************************************************/
995*c54f35caSApple OSS Distributions void
instanceDestructed() const996*c54f35caSApple OSS Distributions OSMetaClass::instanceDestructed() const
997*c54f35caSApple OSS Distributions {
998*c54f35caSApple OSS Distributions if ((1 == OSDecrementAtomic(&instanceCount)) && superClassLink) {
999*c54f35caSApple OSS Distributions superClassLink->instanceDestructed();
1000*c54f35caSApple OSS Distributions }
1001*c54f35caSApple OSS Distributions
1002*c54f35caSApple OSS Distributions if (((int)instanceCount) < 0) {
1003*c54f35caSApple OSS Distributions OSKext * myKext = reserved->kext;
1004*c54f35caSApple OSS Distributions
1005*c54f35caSApple OSS Distributions OSKextLog(myKext, kOSMetaClassLogSpec,
1006*c54f35caSApple OSS Distributions // xxx - this phrasing is rather cryptic
1007*c54f35caSApple OSS Distributions "OSMetaClass: Class %s - bad retain (%d)",
1008*c54f35caSApple OSS Distributions getClassName(), instanceCount);
1009*c54f35caSApple OSS Distributions }
1010*c54f35caSApple OSS Distributions }
1011*c54f35caSApple OSS Distributions
1012*c54f35caSApple OSS Distributions /*********************************************************************
1013*c54f35caSApple OSS Distributions *********************************************************************/
1014*c54f35caSApple OSS Distributions bool
modHasInstance(const char * kextIdentifier)1015*c54f35caSApple OSS Distributions OSMetaClass::modHasInstance(const char * kextIdentifier)
1016*c54f35caSApple OSS Distributions {
1017*c54f35caSApple OSS Distributions bool result = false;
1018*c54f35caSApple OSS Distributions OSKext * theKext = NULL; // must release
1019*c54f35caSApple OSS Distributions
1020*c54f35caSApple OSS Distributions theKext = OSKext::lookupKextWithIdentifier(kextIdentifier);
1021*c54f35caSApple OSS Distributions if (!theKext) {
1022*c54f35caSApple OSS Distributions goto finish;
1023*c54f35caSApple OSS Distributions }
1024*c54f35caSApple OSS Distributions
1025*c54f35caSApple OSS Distributions result = theKext->hasOSMetaClassInstances();
1026*c54f35caSApple OSS Distributions
1027*c54f35caSApple OSS Distributions finish:
1028*c54f35caSApple OSS Distributions OSSafeReleaseNULL(theKext);
1029*c54f35caSApple OSS Distributions return result;
1030*c54f35caSApple OSS Distributions }
1031*c54f35caSApple OSS Distributions
1032*c54f35caSApple OSS Distributions /*********************************************************************
1033*c54f35caSApple OSS Distributions *********************************************************************/
1034*c54f35caSApple OSS Distributions void
reportModInstances(const char * kextIdentifier)1035*c54f35caSApple OSS Distributions OSMetaClass::reportModInstances(const char * kextIdentifier)
1036*c54f35caSApple OSS Distributions {
1037*c54f35caSApple OSS Distributions OSKext::reportOSMetaClassInstances(kextIdentifier,
1038*c54f35caSApple OSS Distributions kOSKextLogExplicitLevel);
1039*c54f35caSApple OSS Distributions return;
1040*c54f35caSApple OSS Distributions }
1041*c54f35caSApple OSS Distributions /*********************************************************************
1042*c54f35caSApple OSS Distributions *********************************************************************/
1043*c54f35caSApple OSS Distributions
1044*c54f35caSApple OSS Distributions void
addInstance(const OSObject * instance,bool super) const1045*c54f35caSApple OSS Distributions OSMetaClass::addInstance(const OSObject * instance, bool super) const
1046*c54f35caSApple OSS Distributions {
1047*c54f35caSApple OSS Distributions if (!super) {
1048*c54f35caSApple OSS Distributions IOLockLock(sInstancesLock);
1049*c54f35caSApple OSS Distributions }
1050*c54f35caSApple OSS Distributions
1051*c54f35caSApple OSS Distributions if (!reserved->instances) {
1052*c54f35caSApple OSS Distributions reserved->instances = OSOrderedSet::withCapacity(16);
1053*c54f35caSApple OSS Distributions if (superClassLink) {
1054*c54f35caSApple OSS Distributions superClassLink->addInstance(reserved->instances, true);
1055*c54f35caSApple OSS Distributions }
1056*c54f35caSApple OSS Distributions }
1057*c54f35caSApple OSS Distributions reserved->instances->setLastObject(instance);
1058*c54f35caSApple OSS Distributions
1059*c54f35caSApple OSS Distributions if (!super) {
1060*c54f35caSApple OSS Distributions IOLockUnlock(sInstancesLock);
1061*c54f35caSApple OSS Distributions }
1062*c54f35caSApple OSS Distributions }
1063*c54f35caSApple OSS Distributions
1064*c54f35caSApple OSS Distributions void
removeInstance(const OSObject * instance,bool super) const1065*c54f35caSApple OSS Distributions OSMetaClass::removeInstance(const OSObject * instance, bool super) const
1066*c54f35caSApple OSS Distributions {
1067*c54f35caSApple OSS Distributions if (!super) {
1068*c54f35caSApple OSS Distributions IOLockLock(sInstancesLock);
1069*c54f35caSApple OSS Distributions }
1070*c54f35caSApple OSS Distributions
1071*c54f35caSApple OSS Distributions if (reserved->instances) {
1072*c54f35caSApple OSS Distributions reserved->instances->removeObject(instance);
1073*c54f35caSApple OSS Distributions if (0 == reserved->instances->getCount()) {
1074*c54f35caSApple OSS Distributions if (superClassLink) {
1075*c54f35caSApple OSS Distributions superClassLink->removeInstance(reserved->instances, true);
1076*c54f35caSApple OSS Distributions }
1077*c54f35caSApple OSS Distributions IOLockLock(sAllClassesLock);
1078*c54f35caSApple OSS Distributions reserved->instances->release();
1079*c54f35caSApple OSS Distributions reserved->instances = NULL;
1080*c54f35caSApple OSS Distributions IOLockUnlock(sAllClassesLock);
1081*c54f35caSApple OSS Distributions }
1082*c54f35caSApple OSS Distributions }
1083*c54f35caSApple OSS Distributions
1084*c54f35caSApple OSS Distributions if (!super) {
1085*c54f35caSApple OSS Distributions IOLockUnlock(sInstancesLock);
1086*c54f35caSApple OSS Distributions }
1087*c54f35caSApple OSS Distributions }
1088*c54f35caSApple OSS Distributions
1089*c54f35caSApple OSS Distributions void
applyToInstances(OSOrderedSet * set,OSMetaClassInstanceApplierFunction applier,void * context)1090*c54f35caSApple OSS Distributions OSMetaClass::applyToInstances(OSOrderedSet * set,
1091*c54f35caSApple OSS Distributions OSMetaClassInstanceApplierFunction applier,
1092*c54f35caSApple OSS Distributions void * context)
1093*c54f35caSApple OSS Distributions {
1094*c54f35caSApple OSS Distributions enum { kLocalDepth = 24 };
1095*c54f35caSApple OSS Distributions unsigned int _nextIndex[kLocalDepth];
1096*c54f35caSApple OSS Distributions OSOrderedSet * _sets[kLocalDepth];
1097*c54f35caSApple OSS Distributions unsigned int * nextIndex = &_nextIndex[0];
1098*c54f35caSApple OSS Distributions OSOrderedSet ** sets = &_sets[0];
1099*c54f35caSApple OSS Distributions OSObject * obj;
1100*c54f35caSApple OSS Distributions OSOrderedSet * childSet;
1101*c54f35caSApple OSS Distributions unsigned int maxDepth;
1102*c54f35caSApple OSS Distributions unsigned int idx;
1103*c54f35caSApple OSS Distributions unsigned int level;
1104*c54f35caSApple OSS Distributions bool done;
1105*c54f35caSApple OSS Distributions
1106*c54f35caSApple OSS Distributions maxDepth = sDeepestClass;
1107*c54f35caSApple OSS Distributions if (maxDepth > kLocalDepth) {
1108*c54f35caSApple OSS Distributions nextIndex = IONewData(typeof(nextIndex[0]), maxDepth);
1109*c54f35caSApple OSS Distributions sets = IONew(typeof(sets[0]), maxDepth);
1110*c54f35caSApple OSS Distributions }
1111*c54f35caSApple OSS Distributions done = false;
1112*c54f35caSApple OSS Distributions level = 0;
1113*c54f35caSApple OSS Distributions idx = 0;
1114*c54f35caSApple OSS Distributions do{
1115*c54f35caSApple OSS Distributions while (!done && (obj = set->getObject(idx++))) {
1116*c54f35caSApple OSS Distributions if ((childSet = OSDynamicCast(OSOrderedSet, obj))) {
1117*c54f35caSApple OSS Distributions if (level >= maxDepth) {
1118*c54f35caSApple OSS Distributions panic(">maxDepth");
1119*c54f35caSApple OSS Distributions }
1120*c54f35caSApple OSS Distributions sets[level] = set;
1121*c54f35caSApple OSS Distributions nextIndex[level] = idx;
1122*c54f35caSApple OSS Distributions level++;
1123*c54f35caSApple OSS Distributions set = childSet;
1124*c54f35caSApple OSS Distributions idx = 0;
1125*c54f35caSApple OSS Distributions break;
1126*c54f35caSApple OSS Distributions }
1127*c54f35caSApple OSS Distributions done = (*applier)(obj, context);
1128*c54f35caSApple OSS Distributions }
1129*c54f35caSApple OSS Distributions if (!obj) {
1130*c54f35caSApple OSS Distributions if (!done && level) {
1131*c54f35caSApple OSS Distributions level--;
1132*c54f35caSApple OSS Distributions set = sets[level];
1133*c54f35caSApple OSS Distributions idx = nextIndex[level];
1134*c54f35caSApple OSS Distributions } else {
1135*c54f35caSApple OSS Distributions done = true;
1136*c54f35caSApple OSS Distributions }
1137*c54f35caSApple OSS Distributions }
1138*c54f35caSApple OSS Distributions }while (!done);
1139*c54f35caSApple OSS Distributions if (maxDepth > kLocalDepth) {
1140*c54f35caSApple OSS Distributions IODeleteData(nextIndex, typeof(nextIndex[0]), maxDepth);
1141*c54f35caSApple OSS Distributions IODelete(sets, typeof(sets[0]), maxDepth);
1142*c54f35caSApple OSS Distributions }
1143*c54f35caSApple OSS Distributions }
1144*c54f35caSApple OSS Distributions
1145*c54f35caSApple OSS Distributions void
applyToInstances(OSMetaClassInstanceApplierFunction applier,void * context) const1146*c54f35caSApple OSS Distributions OSMetaClass::applyToInstances(OSMetaClassInstanceApplierFunction applier,
1147*c54f35caSApple OSS Distributions void * context) const
1148*c54f35caSApple OSS Distributions {
1149*c54f35caSApple OSS Distributions IOLockLock(sInstancesLock);
1150*c54f35caSApple OSS Distributions if (reserved->instances) {
1151*c54f35caSApple OSS Distributions applyToInstances(reserved->instances, applier, context);
1152*c54f35caSApple OSS Distributions }
1153*c54f35caSApple OSS Distributions IOLockUnlock(sInstancesLock);
1154*c54f35caSApple OSS Distributions }
1155*c54f35caSApple OSS Distributions
1156*c54f35caSApple OSS Distributions void
applyToInstancesOfClassName(const OSSymbol * name,OSMetaClassInstanceApplierFunction applier,void * context)1157*c54f35caSApple OSS Distributions OSMetaClass::applyToInstancesOfClassName(
1158*c54f35caSApple OSS Distributions const OSSymbol * name,
1159*c54f35caSApple OSS Distributions OSMetaClassInstanceApplierFunction applier,
1160*c54f35caSApple OSS Distributions void * context)
1161*c54f35caSApple OSS Distributions {
1162*c54f35caSApple OSS Distributions OSMetaClass * meta;
1163*c54f35caSApple OSS Distributions OSOrderedSet * set = NULL;
1164*c54f35caSApple OSS Distributions
1165*c54f35caSApple OSS Distributions IOLockLock(sAllClassesLock);
1166*c54f35caSApple OSS Distributions if (sAllClassesDict
1167*c54f35caSApple OSS Distributions && (meta = (OSMetaClass *) sAllClassesDict->getObject(name))
1168*c54f35caSApple OSS Distributions && (set = meta->reserved->instances)) {
1169*c54f35caSApple OSS Distributions set->retain();
1170*c54f35caSApple OSS Distributions }
1171*c54f35caSApple OSS Distributions IOLockUnlock(sAllClassesLock);
1172*c54f35caSApple OSS Distributions
1173*c54f35caSApple OSS Distributions if (!set) {
1174*c54f35caSApple OSS Distributions return;
1175*c54f35caSApple OSS Distributions }
1176*c54f35caSApple OSS Distributions
1177*c54f35caSApple OSS Distributions IOLockLock(sInstancesLock);
1178*c54f35caSApple OSS Distributions applyToInstances(set, applier, context);
1179*c54f35caSApple OSS Distributions IOLockUnlock(sInstancesLock);
1180*c54f35caSApple OSS Distributions set->release();
1181*c54f35caSApple OSS Distributions }
1182*c54f35caSApple OSS Distributions
1183*c54f35caSApple OSS Distributions /*********************************************************************
1184*c54f35caSApple OSS Distributions *********************************************************************/
1185*c54f35caSApple OSS Distributions void
considerUnloads()1186*c54f35caSApple OSS Distributions OSMetaClass::considerUnloads()
1187*c54f35caSApple OSS Distributions {
1188*c54f35caSApple OSS Distributions OSKext::considerUnloads();
1189*c54f35caSApple OSS Distributions }
1190*c54f35caSApple OSS Distributions
1191*c54f35caSApple OSS Distributions /*********************************************************************
1192*c54f35caSApple OSS Distributions *********************************************************************/
1193*c54f35caSApple OSS Distributions bool
removeClasses(OSCollection * metaClasses)1194*c54f35caSApple OSS Distributions OSMetaClass::removeClasses(OSCollection * metaClasses)
1195*c54f35caSApple OSS Distributions {
1196*c54f35caSApple OSS Distributions OSCollectionIterator * classIterator;
1197*c54f35caSApple OSS Distributions OSMetaClass * checkClass;
1198*c54f35caSApple OSS Distributions bool result;
1199*c54f35caSApple OSS Distributions
1200*c54f35caSApple OSS Distributions classIterator = OSCollectionIterator::withCollection(metaClasses);
1201*c54f35caSApple OSS Distributions if (!classIterator) {
1202*c54f35caSApple OSS Distributions return false;
1203*c54f35caSApple OSS Distributions }
1204*c54f35caSApple OSS Distributions
1205*c54f35caSApple OSS Distributions IOLockLock(sAllClassesLock);
1206*c54f35caSApple OSS Distributions
1207*c54f35caSApple OSS Distributions result = false;
1208*c54f35caSApple OSS Distributions do{
1209*c54f35caSApple OSS Distributions while ((checkClass = (OSMetaClass *)classIterator->getNextObject())
1210*c54f35caSApple OSS Distributions && !checkClass->getInstanceCount()
1211*c54f35caSApple OSS Distributions && !checkClass->reserved->retain) {
1212*c54f35caSApple OSS Distributions }
1213*c54f35caSApple OSS Distributions if (checkClass) {
1214*c54f35caSApple OSS Distributions break;
1215*c54f35caSApple OSS Distributions }
1216*c54f35caSApple OSS Distributions classIterator->reset();
1217*c54f35caSApple OSS Distributions while ((checkClass = (OSMetaClass *)classIterator->getNextObject())) {
1218*c54f35caSApple OSS Distributions sAllClassesDict->removeObject(checkClass->className);
1219*c54f35caSApple OSS Distributions }
1220*c54f35caSApple OSS Distributions result = true;
1221*c54f35caSApple OSS Distributions }while (false);
1222*c54f35caSApple OSS Distributions
1223*c54f35caSApple OSS Distributions IOLockUnlock(sAllClassesLock);
1224*c54f35caSApple OSS Distributions OSSafeReleaseNULL(classIterator);
1225*c54f35caSApple OSS Distributions
1226*c54f35caSApple OSS Distributions return result;
1227*c54f35caSApple OSS Distributions }
1228*c54f35caSApple OSS Distributions
1229*c54f35caSApple OSS Distributions
1230*c54f35caSApple OSS Distributions /*********************************************************************
1231*c54f35caSApple OSS Distributions *********************************************************************/
1232*c54f35caSApple OSS Distributions const OSMetaClass *
getMetaClassWithName(const OSSymbol * name)1233*c54f35caSApple OSS Distributions OSMetaClass::getMetaClassWithName(const OSSymbol * name)
1234*c54f35caSApple OSS Distributions {
1235*c54f35caSApple OSS Distributions OSMetaClass * retMeta = NULL;
1236*c54f35caSApple OSS Distributions
1237*c54f35caSApple OSS Distributions if (!name) {
1238*c54f35caSApple OSS Distributions return NULL;
1239*c54f35caSApple OSS Distributions }
1240*c54f35caSApple OSS Distributions
1241*c54f35caSApple OSS Distributions IOLockLock(sAllClassesLock);
1242*c54f35caSApple OSS Distributions if (sAllClassesDict) {
1243*c54f35caSApple OSS Distributions retMeta = (OSMetaClass *) sAllClassesDict->getObject(name);
1244*c54f35caSApple OSS Distributions }
1245*c54f35caSApple OSS Distributions IOLockUnlock(sAllClassesLock);
1246*c54f35caSApple OSS Distributions
1247*c54f35caSApple OSS Distributions return retMeta;
1248*c54f35caSApple OSS Distributions }
1249*c54f35caSApple OSS Distributions
1250*c54f35caSApple OSS Distributions /*********************************************************************
1251*c54f35caSApple OSS Distributions *********************************************************************/
1252*c54f35caSApple OSS Distributions const OSMetaClass *
copyMetaClassWithName(const OSSymbol * name)1253*c54f35caSApple OSS Distributions OSMetaClass::copyMetaClassWithName(const OSSymbol * name)
1254*c54f35caSApple OSS Distributions {
1255*c54f35caSApple OSS Distributions const OSMetaClass * meta;
1256*c54f35caSApple OSS Distributions
1257*c54f35caSApple OSS Distributions if (!name) {
1258*c54f35caSApple OSS Distributions return NULL;
1259*c54f35caSApple OSS Distributions }
1260*c54f35caSApple OSS Distributions
1261*c54f35caSApple OSS Distributions meta = NULL;
1262*c54f35caSApple OSS Distributions IOLockLock(sAllClassesLock);
1263*c54f35caSApple OSS Distributions if (sAllClassesDict) {
1264*c54f35caSApple OSS Distributions meta = (OSMetaClass *) sAllClassesDict->getObject(name);
1265*c54f35caSApple OSS Distributions if (meta) {
1266*c54f35caSApple OSS Distributions OSIncrementAtomic(&meta->reserved->retain);
1267*c54f35caSApple OSS Distributions }
1268*c54f35caSApple OSS Distributions }
1269*c54f35caSApple OSS Distributions IOLockUnlock(sAllClassesLock);
1270*c54f35caSApple OSS Distributions
1271*c54f35caSApple OSS Distributions return meta;
1272*c54f35caSApple OSS Distributions }
1273*c54f35caSApple OSS Distributions
1274*c54f35caSApple OSS Distributions /*********************************************************************
1275*c54f35caSApple OSS Distributions *********************************************************************/
1276*c54f35caSApple OSS Distributions void
releaseMetaClass() const1277*c54f35caSApple OSS Distributions OSMetaClass::releaseMetaClass() const
1278*c54f35caSApple OSS Distributions {
1279*c54f35caSApple OSS Distributions OSDecrementAtomic(&reserved->retain);
1280*c54f35caSApple OSS Distributions }
1281*c54f35caSApple OSS Distributions
1282*c54f35caSApple OSS Distributions /*********************************************************************
1283*c54f35caSApple OSS Distributions *********************************************************************/
1284*c54f35caSApple OSS Distributions OSObject *
allocClassWithName(const OSSymbol * name)1285*c54f35caSApple OSS Distributions OSMetaClass::allocClassWithName(const OSSymbol * name)
1286*c54f35caSApple OSS Distributions {
1287*c54f35caSApple OSS Distributions const OSMetaClass * meta;
1288*c54f35caSApple OSS Distributions OSObject * result;
1289*c54f35caSApple OSS Distributions
1290*c54f35caSApple OSS Distributions result = NULL;
1291*c54f35caSApple OSS Distributions meta = copyMetaClassWithName(name);
1292*c54f35caSApple OSS Distributions if (meta) {
1293*c54f35caSApple OSS Distributions result = meta->alloc();
1294*c54f35caSApple OSS Distributions meta->releaseMetaClass();
1295*c54f35caSApple OSS Distributions }
1296*c54f35caSApple OSS Distributions
1297*c54f35caSApple OSS Distributions return result;
1298*c54f35caSApple OSS Distributions }
1299*c54f35caSApple OSS Distributions
1300*c54f35caSApple OSS Distributions /*********************************************************************
1301*c54f35caSApple OSS Distributions *********************************************************************/
1302*c54f35caSApple OSS Distributions OSObject *
allocClassWithName(const OSString * name)1303*c54f35caSApple OSS Distributions OSMetaClass::allocClassWithName(const OSString * name)
1304*c54f35caSApple OSS Distributions {
1305*c54f35caSApple OSS Distributions const OSSymbol * tmpKey = OSSymbol::withString(name);
1306*c54f35caSApple OSS Distributions OSObject * result = allocClassWithName(tmpKey);
1307*c54f35caSApple OSS Distributions tmpKey->release();
1308*c54f35caSApple OSS Distributions return result;
1309*c54f35caSApple OSS Distributions }
1310*c54f35caSApple OSS Distributions
1311*c54f35caSApple OSS Distributions /*********************************************************************
1312*c54f35caSApple OSS Distributions *********************************************************************/
1313*c54f35caSApple OSS Distributions OSObject *
allocClassWithName(const char * name)1314*c54f35caSApple OSS Distributions OSMetaClass::allocClassWithName(const char * name)
1315*c54f35caSApple OSS Distributions {
1316*c54f35caSApple OSS Distributions const OSSymbol * tmpKey = OSSymbol::withCStringNoCopy(name);
1317*c54f35caSApple OSS Distributions OSObject * result = allocClassWithName(tmpKey);
1318*c54f35caSApple OSS Distributions tmpKey->release();
1319*c54f35caSApple OSS Distributions return result;
1320*c54f35caSApple OSS Distributions }
1321*c54f35caSApple OSS Distributions
1322*c54f35caSApple OSS Distributions
1323*c54f35caSApple OSS Distributions /*********************************************************************
1324*c54f35caSApple OSS Distributions *********************************************************************/
1325*c54f35caSApple OSS Distributions OSMetaClassBase *
checkMetaCastWithName(const OSSymbol * name,const OSMetaClassBase * in)1326*c54f35caSApple OSS Distributions OSMetaClass::checkMetaCastWithName(
1327*c54f35caSApple OSS Distributions const OSSymbol * name,
1328*c54f35caSApple OSS Distributions const OSMetaClassBase * in)
1329*c54f35caSApple OSS Distributions {
1330*c54f35caSApple OSS Distributions OSMetaClassBase * result = NULL;
1331*c54f35caSApple OSS Distributions
1332*c54f35caSApple OSS Distributions const OSMetaClass * const meta = getMetaClassWithName(name);
1333*c54f35caSApple OSS Distributions
1334*c54f35caSApple OSS Distributions if (meta) {
1335*c54f35caSApple OSS Distributions result = meta->checkMetaCast(in);
1336*c54f35caSApple OSS Distributions }
1337*c54f35caSApple OSS Distributions
1338*c54f35caSApple OSS Distributions return result;
1339*c54f35caSApple OSS Distributions }
1340*c54f35caSApple OSS Distributions
1341*c54f35caSApple OSS Distributions /*********************************************************************
1342*c54f35caSApple OSS Distributions *********************************************************************/
1343*c54f35caSApple OSS Distributions OSMetaClassBase *
1344*c54f35caSApple OSS Distributions OSMetaClass::
checkMetaCastWithName(const OSString * name,const OSMetaClassBase * in)1345*c54f35caSApple OSS Distributions checkMetaCastWithName(
1346*c54f35caSApple OSS Distributions const OSString * name,
1347*c54f35caSApple OSS Distributions const OSMetaClassBase * in)
1348*c54f35caSApple OSS Distributions {
1349*c54f35caSApple OSS Distributions const OSSymbol * tmpKey = OSSymbol::withString(name);
1350*c54f35caSApple OSS Distributions OSMetaClassBase * result = checkMetaCastWithName(tmpKey, in);
1351*c54f35caSApple OSS Distributions
1352*c54f35caSApple OSS Distributions tmpKey->release();
1353*c54f35caSApple OSS Distributions return result;
1354*c54f35caSApple OSS Distributions }
1355*c54f35caSApple OSS Distributions
1356*c54f35caSApple OSS Distributions /*********************************************************************
1357*c54f35caSApple OSS Distributions *********************************************************************/
1358*c54f35caSApple OSS Distributions OSMetaClassBase *
checkMetaCastWithName(const char * name,const OSMetaClassBase * in)1359*c54f35caSApple OSS Distributions OSMetaClass::checkMetaCastWithName(
1360*c54f35caSApple OSS Distributions const char * name,
1361*c54f35caSApple OSS Distributions const OSMetaClassBase * in)
1362*c54f35caSApple OSS Distributions {
1363*c54f35caSApple OSS Distributions const OSSymbol * tmpKey = OSSymbol::withCStringNoCopy(name);
1364*c54f35caSApple OSS Distributions OSMetaClassBase * result = checkMetaCastWithName(tmpKey, in);
1365*c54f35caSApple OSS Distributions
1366*c54f35caSApple OSS Distributions tmpKey->release();
1367*c54f35caSApple OSS Distributions return result;
1368*c54f35caSApple OSS Distributions }
1369*c54f35caSApple OSS Distributions
1370*c54f35caSApple OSS Distributions /*********************************************************************
1371*c54f35caSApple OSS Distributions * OSMetaClass::checkMetaCast()
1372*c54f35caSApple OSS Distributions * Check to see if the 'check' object has this object in its metaclass chain.
1373*c54f35caSApple OSS Distributions * Returns check if it is indeed a kind of the current meta class, 0 otherwise.
1374*c54f35caSApple OSS Distributions *
1375*c54f35caSApple OSS Distributions * Generally this method is not invoked directly but is used to implement
1376*c54f35caSApple OSS Distributions * the OSMetaClassBase::metaCast member function.
1377*c54f35caSApple OSS Distributions *
1378*c54f35caSApple OSS Distributions * See also OSMetaClassBase::metaCast
1379*c54f35caSApple OSS Distributions *********************************************************************/
1380*c54f35caSApple OSS Distributions OSMetaClassBase *
checkMetaCast(const OSMetaClassBase * check) const1381*c54f35caSApple OSS Distributions OSMetaClass::checkMetaCast(
1382*c54f35caSApple OSS Distributions const OSMetaClassBase * check) const
1383*c54f35caSApple OSS Distributions {
1384*c54f35caSApple OSS Distributions const OSMetaClass * const toMeta = this;
1385*c54f35caSApple OSS Distributions const OSMetaClass * fromMeta;
1386*c54f35caSApple OSS Distributions
1387*c54f35caSApple OSS Distributions for (fromMeta = check->getMetaClass();; fromMeta = fromMeta->superClassLink) {
1388*c54f35caSApple OSS Distributions if (toMeta == fromMeta) {
1389*c54f35caSApple OSS Distributions return const_cast<OSMetaClassBase *>(check); // Discard const
1390*c54f35caSApple OSS Distributions }
1391*c54f35caSApple OSS Distributions if (!fromMeta->superClassLink) {
1392*c54f35caSApple OSS Distributions break;
1393*c54f35caSApple OSS Distributions }
1394*c54f35caSApple OSS Distributions }
1395*c54f35caSApple OSS Distributions
1396*c54f35caSApple OSS Distributions return NULL;
1397*c54f35caSApple OSS Distributions }
1398*c54f35caSApple OSS Distributions
1399*c54f35caSApple OSS Distributions /*********************************************************************
1400*c54f35caSApple OSS Distributions *********************************************************************/
1401*c54f35caSApple OSS Distributions __dead2
1402*c54f35caSApple OSS Distributions void
reservedCalled(int ind) const1403*c54f35caSApple OSS Distributions OSMetaClass::reservedCalled(int ind) const
1404*c54f35caSApple OSS Distributions {
1405*c54f35caSApple OSS Distributions const char * cname = className->getCStringNoCopy();
1406*c54f35caSApple OSS Distributions panic("%s::_RESERVED%s%d called.", cname, cname, ind);
1407*c54f35caSApple OSS Distributions }
1408*c54f35caSApple OSS Distributions
1409*c54f35caSApple OSS Distributions /*********************************************************************
1410*c54f35caSApple OSS Distributions *********************************************************************/
1411*c54f35caSApple OSS Distributions const
1412*c54f35caSApple OSS Distributions OSMetaClass *
getSuperClass() const1413*c54f35caSApple OSS Distributions OSMetaClass::getSuperClass() const
1414*c54f35caSApple OSS Distributions {
1415*c54f35caSApple OSS Distributions return superClassLink;
1416*c54f35caSApple OSS Distributions }
1417*c54f35caSApple OSS Distributions
1418*c54f35caSApple OSS Distributions /*********************************************************************
1419*c54f35caSApple OSS Distributions *********************************************************************/
1420*c54f35caSApple OSS Distributions const OSSymbol *
getKmodName() const1421*c54f35caSApple OSS Distributions OSMetaClass::getKmodName() const
1422*c54f35caSApple OSS Distributions {
1423*c54f35caSApple OSS Distributions OSKext * myKext = reserved ? reserved->kext : NULL;
1424*c54f35caSApple OSS Distributions if (myKext) {
1425*c54f35caSApple OSS Distributions return myKext->getIdentifier();
1426*c54f35caSApple OSS Distributions }
1427*c54f35caSApple OSS Distributions return OSSymbol::withCStringNoCopy("unknown");
1428*c54f35caSApple OSS Distributions }
1429*c54f35caSApple OSS Distributions
1430*c54f35caSApple OSS Distributions /*********************************************************************
1431*c54f35caSApple OSS Distributions *********************************************************************/
1432*c54f35caSApple OSS Distributions OSKext *
getKext() const1433*c54f35caSApple OSS Distributions OSMetaClass::getKext() const
1434*c54f35caSApple OSS Distributions {
1435*c54f35caSApple OSS Distributions return reserved ? reserved->kext : NULL;
1436*c54f35caSApple OSS Distributions }
1437*c54f35caSApple OSS Distributions
1438*c54f35caSApple OSS Distributions /*********************************************************************
1439*c54f35caSApple OSS Distributions *********************************************************************/
1440*c54f35caSApple OSS Distributions unsigned int
getInstanceCount() const1441*c54f35caSApple OSS Distributions OSMetaClass::getInstanceCount() const
1442*c54f35caSApple OSS Distributions {
1443*c54f35caSApple OSS Distributions return instanceCount;
1444*c54f35caSApple OSS Distributions }
1445*c54f35caSApple OSS Distributions
1446*c54f35caSApple OSS Distributions /*********************************************************************
1447*c54f35caSApple OSS Distributions *********************************************************************/
1448*c54f35caSApple OSS Distributions /* static */
1449*c54f35caSApple OSS Distributions void
printInstanceCounts()1450*c54f35caSApple OSS Distributions OSMetaClass::printInstanceCounts()
1451*c54f35caSApple OSS Distributions {
1452*c54f35caSApple OSS Distributions OSCollectionIterator * classes;
1453*c54f35caSApple OSS Distributions OSSymbol * className;
1454*c54f35caSApple OSS Distributions OSMetaClass * meta;
1455*c54f35caSApple OSS Distributions
1456*c54f35caSApple OSS Distributions IOLockLock(sAllClassesLock);
1457*c54f35caSApple OSS Distributions classes = OSCollectionIterator::withCollection(sAllClassesDict);
1458*c54f35caSApple OSS Distributions assert(classes);
1459*c54f35caSApple OSS Distributions
1460*c54f35caSApple OSS Distributions while ((className = (OSSymbol *)classes->getNextObject())) {
1461*c54f35caSApple OSS Distributions meta = (OSMetaClass *)sAllClassesDict->getObject(className);
1462*c54f35caSApple OSS Distributions assert(meta);
1463*c54f35caSApple OSS Distributions
1464*c54f35caSApple OSS Distributions printf("%24s count: %03d x 0x%03x = 0x%06x\n",
1465*c54f35caSApple OSS Distributions className->getCStringNoCopy(),
1466*c54f35caSApple OSS Distributions meta->getInstanceCount(),
1467*c54f35caSApple OSS Distributions meta->getClassSize(),
1468*c54f35caSApple OSS Distributions meta->getInstanceCount() * meta->getClassSize());
1469*c54f35caSApple OSS Distributions }
1470*c54f35caSApple OSS Distributions printf("\n");
1471*c54f35caSApple OSS Distributions classes->release();
1472*c54f35caSApple OSS Distributions IOLockUnlock(sAllClassesLock);
1473*c54f35caSApple OSS Distributions return;
1474*c54f35caSApple OSS Distributions }
1475*c54f35caSApple OSS Distributions
1476*c54f35caSApple OSS Distributions /*********************************************************************
1477*c54f35caSApple OSS Distributions *********************************************************************/
1478*c54f35caSApple OSS Distributions OSDictionary *
getClassDictionary()1479*c54f35caSApple OSS Distributions OSMetaClass::getClassDictionary()
1480*c54f35caSApple OSS Distributions {
1481*c54f35caSApple OSS Distributions panic("OSMetaClass::getClassDictionary() is obsoleted.");
1482*c54f35caSApple OSS Distributions return NULL;
1483*c54f35caSApple OSS Distributions }
1484*c54f35caSApple OSS Distributions
1485*c54f35caSApple OSS Distributions /*********************************************************************
1486*c54f35caSApple OSS Distributions *********************************************************************/
1487*c54f35caSApple OSS Distributions bool
serialize(__unused OSSerialize * s) const1488*c54f35caSApple OSS Distributions OSMetaClass::serialize(__unused OSSerialize * s) const
1489*c54f35caSApple OSS Distributions {
1490*c54f35caSApple OSS Distributions panic("OSMetaClass::serialize(): Obsoleted");
1491*c54f35caSApple OSS Distributions return false;
1492*c54f35caSApple OSS Distributions }
1493*c54f35caSApple OSS Distributions
1494*c54f35caSApple OSS Distributions /*********************************************************************
1495*c54f35caSApple OSS Distributions *********************************************************************/
1496*c54f35caSApple OSS Distributions /* static */
1497*c54f35caSApple OSS Distributions void
serializeClassDictionary(OSDictionary * serializeDictionary)1498*c54f35caSApple OSS Distributions OSMetaClass::serializeClassDictionary(OSDictionary * serializeDictionary)
1499*c54f35caSApple OSS Distributions {
1500*c54f35caSApple OSS Distributions OSDictionary * classDict = NULL;
1501*c54f35caSApple OSS Distributions
1502*c54f35caSApple OSS Distributions IOLockLock(sAllClassesLock);
1503*c54f35caSApple OSS Distributions
1504*c54f35caSApple OSS Distributions classDict = OSDictionary::withCapacity(sAllClassesDict->getCount());
1505*c54f35caSApple OSS Distributions if (!classDict) {
1506*c54f35caSApple OSS Distributions goto finish;
1507*c54f35caSApple OSS Distributions }
1508*c54f35caSApple OSS Distributions
1509*c54f35caSApple OSS Distributions do {
1510*c54f35caSApple OSS Distributions OSCollectionIterator * classes;
1511*c54f35caSApple OSS Distributions const OSSymbol * className;
1512*c54f35caSApple OSS Distributions
1513*c54f35caSApple OSS Distributions classes = OSCollectionIterator::withCollection(sAllClassesDict);
1514*c54f35caSApple OSS Distributions if (!classes) {
1515*c54f35caSApple OSS Distributions break;
1516*c54f35caSApple OSS Distributions }
1517*c54f35caSApple OSS Distributions
1518*c54f35caSApple OSS Distributions while ((className = (const OSSymbol *)classes->getNextObject())) {
1519*c54f35caSApple OSS Distributions const OSMetaClass * meta;
1520*c54f35caSApple OSS Distributions OSNumber * count;
1521*c54f35caSApple OSS Distributions
1522*c54f35caSApple OSS Distributions meta = (OSMetaClass *)sAllClassesDict->getObject(className);
1523*c54f35caSApple OSS Distributions count = OSNumber::withNumber(meta->getInstanceCount(), 32);
1524*c54f35caSApple OSS Distributions if (count) {
1525*c54f35caSApple OSS Distributions classDict->setObject(className, count);
1526*c54f35caSApple OSS Distributions count->release();
1527*c54f35caSApple OSS Distributions }
1528*c54f35caSApple OSS Distributions }
1529*c54f35caSApple OSS Distributions classes->release();
1530*c54f35caSApple OSS Distributions
1531*c54f35caSApple OSS Distributions serializeDictionary->setObject("Classes", classDict);
1532*c54f35caSApple OSS Distributions } while (0);
1533*c54f35caSApple OSS Distributions
1534*c54f35caSApple OSS Distributions finish:
1535*c54f35caSApple OSS Distributions OSSafeReleaseNULL(classDict);
1536*c54f35caSApple OSS Distributions
1537*c54f35caSApple OSS Distributions IOLockUnlock(sAllClassesLock);
1538*c54f35caSApple OSS Distributions
1539*c54f35caSApple OSS Distributions return;
1540*c54f35caSApple OSS Distributions }
1541*c54f35caSApple OSS Distributions
1542*c54f35caSApple OSS Distributions
1543*c54f35caSApple OSS Distributions /*********************************************************************
1544*c54f35caSApple OSS Distributions *********************************************************************/
1545*c54f35caSApple OSS Distributions
1546*c54f35caSApple OSS Distributions #if IOTRACKING
1547*c54f35caSApple OSS Distributions
1548*c54f35caSApple OSS Distributions __typed_allocators_ignore_push
1549*c54f35caSApple OSS Distributions
1550*c54f35caSApple OSS Distributions void *
trackedNew(size_t size)1551*c54f35caSApple OSS Distributions OSMetaClass::trackedNew(size_t size)
1552*c54f35caSApple OSS Distributions {
1553*c54f35caSApple OSS Distributions IOTracking * mem;
1554*c54f35caSApple OSS Distributions
1555*c54f35caSApple OSS Distributions mem = (typeof(mem))kheap_alloc(KHEAP_DEFAULT, size + sizeof(IOTracking),
1556*c54f35caSApple OSS Distributions Z_VM_TAG_BT(Z_WAITOK, VM_KERN_MEMORY_LIBKERN));
1557*c54f35caSApple OSS Distributions assert(mem);
1558*c54f35caSApple OSS Distributions if (!mem) {
1559*c54f35caSApple OSS Distributions return mem;
1560*c54f35caSApple OSS Distributions }
1561*c54f35caSApple OSS Distributions
1562*c54f35caSApple OSS Distributions memset(mem, 0, size + sizeof(IOTracking));
1563*c54f35caSApple OSS Distributions mem++;
1564*c54f35caSApple OSS Distributions
1565*c54f35caSApple OSS Distributions OSIVAR_ACCUMSIZE(size);
1566*c54f35caSApple OSS Distributions
1567*c54f35caSApple OSS Distributions return mem;
1568*c54f35caSApple OSS Distributions }
1569*c54f35caSApple OSS Distributions
1570*c54f35caSApple OSS Distributions void
trackedDelete(void * instance,size_t size)1571*c54f35caSApple OSS Distributions OSMetaClass::trackedDelete(void * instance, size_t size)
1572*c54f35caSApple OSS Distributions {
1573*c54f35caSApple OSS Distributions IOTracking * mem = (typeof(mem))instance; mem--;
1574*c54f35caSApple OSS Distributions
1575*c54f35caSApple OSS Distributions kheap_free(KHEAP_DEFAULT, mem, size + sizeof(IOTracking));
1576*c54f35caSApple OSS Distributions OSIVAR_ACCUMSIZE(-size);
1577*c54f35caSApple OSS Distributions }
1578*c54f35caSApple OSS Distributions
1579*c54f35caSApple OSS Distributions __typed_allocators_ignore_pop
1580*c54f35caSApple OSS Distributions
1581*c54f35caSApple OSS Distributions void
trackedInstance(OSObject * instance) const1582*c54f35caSApple OSS Distributions OSMetaClass::trackedInstance(OSObject * instance) const
1583*c54f35caSApple OSS Distributions {
1584*c54f35caSApple OSS Distributions IOTracking * mem = (typeof(mem))instance; mem--;
1585*c54f35caSApple OSS Distributions
1586*c54f35caSApple OSS Distributions return IOTrackingAdd(reserved->tracking, mem, classSize, false, VM_KERN_MEMORY_NONE);
1587*c54f35caSApple OSS Distributions }
1588*c54f35caSApple OSS Distributions
1589*c54f35caSApple OSS Distributions void
trackedFree(OSObject * instance) const1590*c54f35caSApple OSS Distributions OSMetaClass::trackedFree(OSObject * instance) const
1591*c54f35caSApple OSS Distributions {
1592*c54f35caSApple OSS Distributions IOTracking * mem = (typeof(mem))instance; mem--;
1593*c54f35caSApple OSS Distributions
1594*c54f35caSApple OSS Distributions return IOTrackingRemove(reserved->tracking, mem, classSize);
1595*c54f35caSApple OSS Distributions }
1596*c54f35caSApple OSS Distributions
1597*c54f35caSApple OSS Distributions void
trackedAccumSize(OSObject * instance,size_t size) const1598*c54f35caSApple OSS Distributions OSMetaClass::trackedAccumSize(OSObject * instance, size_t size) const
1599*c54f35caSApple OSS Distributions {
1600*c54f35caSApple OSS Distributions IOTracking * mem = (typeof(mem))instance; mem--;
1601*c54f35caSApple OSS Distributions
1602*c54f35caSApple OSS Distributions return IOTrackingAccumSize(reserved->tracking, mem, size);
1603*c54f35caSApple OSS Distributions }
1604*c54f35caSApple OSS Distributions
1605*c54f35caSApple OSS Distributions IOTrackingQueue *
getTracking() const1606*c54f35caSApple OSS Distributions OSMetaClass::getTracking() const
1607*c54f35caSApple OSS Distributions {
1608*c54f35caSApple OSS Distributions return reserved->tracking;
1609*c54f35caSApple OSS Distributions }
1610*c54f35caSApple OSS Distributions
1611*c54f35caSApple OSS Distributions #endif /* IOTRACKING */
1612