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