2 * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* OSMetaClass.cpp created by gvdl on Fri 1998-11-17 */
32 #include <libkern/OSReturn.h>
34 #include <libkern/c++/OSMetaClass.h>
35 #include <libkern/c++/OSObject.h>
36 #include <libkern/c++/OSKext.h>
38 #include <libkern/c++/OSCollectionIterator.h>
39 #include <libkern/c++/OSDictionary.h>
40 #include <libkern/c++/OSArray.h>
41 #include <libkern/c++/OSSet.h>
42 #include <libkern/c++/OSSymbol.h>
43 #include <libkern/c++/OSNumber.h>
44 #include <libkern/c++/OSSerialize.h>
46 #include <libkern/c++/OSLib.h>
47 #include <libkern/OSAtomic.h>
49 #include <IOKit/IOLib.h>
51 #include <IOKit/IOKitDebug.h>
56 #include <sys/systm.h>
57 #include <mach/mach_types.h>
58 #include <kern/locks.h>
59 #include <kern/clock.h>
60 #include <kern/thread_call.h>
61 #include <kern/host.h>
62 #include <mach/mach_interface.h>
67 #endif /* PRAGMA_MARK */
68 /*********************************************************************
70 *********************************************************************/
74 #pragma mark Internal constants & data structs
75 #endif /* PRAGMA_MARK */
76 /*********************************************************************
77 * Internal constants & data structs
78 *********************************************************************/
79 OSKextLogSpec kOSMetaClassLogSpec
=
80 kOSKextLogErrorLevel
|
82 kOSKextLogKextBookkeepingFlag
;
85 kCompletedBootstrap
= 0,
87 kMakingDictionaries
= 2
88 } sBootstrapState
= kNoDictionaries
;
90 static const int kClassCapacityIncrement
= 40;
91 static const int kKModCapacityIncrement
= 10;
92 static OSDictionary
* sAllClassesDict
;
93 static unsigned int sDeepestClass
;
94 IOLock
* sAllClassesLock
= NULL
;
95 IOLock
* sInstancesLock
= NULL
;
98 * While loading a kext and running all its constructors to register
99 * all OSMetaClass classes, the classes are queued up here. Only one
100 * kext can be in flight at a time, guarded by sStalledClassesLock
102 static struct StalledData
{
103 const char * kextIdentifier
;
105 unsigned int capacity
;
107 OSMetaClass
** classes
;
109 IOLock
* sStalledClassesLock
= NULL
;
111 struct ExpansionData
{
112 OSOrderedSet
* instances
;
116 IOTrackingQueue
* tracking
;
122 #pragma mark OSMetaClassBase
123 #endif /* PRAGMA_MARK */
124 /*********************************************************************
126 *********************************************************************/
128 #if APPLE_KEXT_VTABLE_PADDING
129 /*********************************************************************
130 * Reserved vtable functions.
131 *********************************************************************/
134 OSMetaClassBase::_RESERVEDOSMetaClassBase0()
136 panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 0);
139 OSMetaClassBase::_RESERVEDOSMetaClassBase1()
141 panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 1);
144 OSMetaClassBase::_RESERVEDOSMetaClassBase2()
146 panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 2);
149 OSMetaClassBase::_RESERVEDOSMetaClassBase3()
151 panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 3);
153 #endif /* SLOT_USED */
155 // As these slots are used move them up inside the #if above
157 OSMetaClassBase::_RESERVEDOSMetaClassBase4()
159 panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 4);
162 OSMetaClassBase::_RESERVEDOSMetaClassBase5()
164 panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 5);
167 OSMetaClassBase::_RESERVEDOSMetaClassBase6()
169 panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 6);
173 /*********************************************************************
174 *********************************************************************/
176 #if defined(__arm__) || defined(__arm64__)
178 #if defined(HAS_APPLE_PAC)
180 #endif /* defined(HAS_APPLE_PAC) */
183 * IHI0059A "C++ Application Binary Interface Standard for the ARM 64 - bit Architecture":
185 * 3.2.1 Representation of pointer to member function The generic C++ ABI [GC++ABI]
186 * specifies that a pointer to member function is a pair of words <ptr, adj>. The
187 * least significant bit of ptr discriminates between (0) the address of a non-
188 * virtual member function and (1) the offset in the class's virtual table of the
189 * address of a virtual function. This encoding cannot work for the AArch64
190 * instruction set where the architecture reserves all bits of code addresses. This
191 * ABI specifies that adj contains twice the this adjustment, plus 1 if the member
192 * function is virtual. The least significant bit of adj then makes exactly the
193 * same discrimination as the least significant bit of ptr does for Itanium. A
194 * pointer to member function is NULL when ptr = 0 and the least significant bit of
198 OSMetaClassBase::_ptf_t
199 #if defined(HAS_APPLE_PAC) && __has_feature(ptrauth_type_discriminator)
200 OSMetaClassBase::_ptmf2ptf(const OSMetaClassBase
*self
__attribute__((unused
)),
201 void (OSMetaClassBase::*func
)(void), uintptr_t typeDisc
)
203 OSMetaClassBase::_ptmf2ptf(const OSMetaClassBase
*self
,
204 void (OSMetaClassBase::*func
)(void),
206 __attribute__((unused
)))
214 void (OSMetaClassBase::*fIn
)(void);
222 #if defined(HAS_APPLE_PAC) && __has_feature(ptrauth_type_discriminator)
223 // Authenticate 'pfn' using the member function pointer type discriminator
224 // and resign it as a C function pointer. 'pfn' can point to either a
225 // non-virtual function or a virtual member function thunk.
226 pfn
= ptrauth_auth_function(pfn
, ptrauth_key_function_pointer
, typeDisc
);
229 if (map
.pTMF
.delta
& 1) {
232 const OSMetaClassBase
*fObj
;
237 // Virtual member function so dereference table
238 #if defined(HAS_APPLE_PAC)
239 // The entity hash is stored in the top 32-bits of the vtable offset of a
240 // member function pointer.
241 uint32_t entity_hash
= ((uintptr_t)pfn
) >> 32;
242 pfn
= (_ptf_t
)(((uintptr_t) pfn
) & 0xFFFFFFFF);
244 // Authenticate the vtable pointer.
245 _ptf_t
*vtablep
= ptrauth_auth_data(*u
.vtablep
,
246 ptrauth_key_cxx_vtable_pointer
, 0);
247 // Calculate the address of the vtable entry.
248 _ptf_t
*vtentryp
= (_ptf_t
*)(((uintptr_t)vtablep
) + (uintptr_t)pfn
);
249 // Load the pointer from the vtable entry.
252 // Finally, resign the vtable entry as a function pointer.
253 uintptr_t auth_data
= ptrauth_blend_discriminator(vtentryp
, entity_hash
);
254 pfn
= ptrauth_auth_and_resign(pfn
, ptrauth_key_function_pointer
,
255 auth_data
, ptrauth_key_function_pointer
, 0);
256 #else /* defined(HAS_APPLE_PAC) */
257 pfn
= *(_ptf_t
*)(((uintptr_t)*u
.vtablep
) + (uintptr_t)pfn
);
258 #endif /* !defined(HAS_APPLE_PAC) */
261 // Not virtual, i.e. plain member func
267 #endif /* defined(__arm__) || defined(__arm64__) */
268 /*********************************************************************
269 * These used to be inline in the header but gcc didn't believe us
270 * Now we MUST pull the inline out at least until the compiler is
273 * Helper inlines for runtime type preprocessor macros
274 *********************************************************************/
276 /*********************************************************************
277 *********************************************************************/
279 OSMetaClassBase::safeMetaCast(
280 const OSMetaClassBase
* me
,
281 const OSMetaClass
* toType
)
283 return (me
)? me
->metaCast(toType
) : NULL
;
286 /// A helper function to crash with a kernel panic.
287 __attribute__((cold
, not_tail_called
, noreturn
))
289 panic_crash_fail_cast(const OSMetaClassBase
*me
,
290 const OSMetaClass
*toType
)
292 panic("Unexpected cast fail: from %p to %p", me
, toType
);
293 __builtin_unreachable();
297 OSMetaClassBase::requiredMetaCast(
298 const OSMetaClassBase
* me
,
299 const OSMetaClass
* toType
)
304 OSMetaClassBase
*tmp
= safeMetaCast(me
, toType
);
306 panic_crash_fail_cast(me
, toType
);
311 /*********************************************************************
312 *********************************************************************/
314 OSMetaClassBase::checkTypeInst(
315 const OSMetaClassBase
* inst
,
316 const OSMetaClassBase
* typeinst
)
318 const OSMetaClass
* toType
= OSTypeIDInst(typeinst
);
319 return typeinst
&& inst
&& (NULL
!= inst
->metaCast(toType
));
322 /*********************************************************************
323 *********************************************************************/
328 sAllClassesLock
= IOLockAlloc();
329 sStalledClassesLock
= IOLockAlloc();
330 sInstancesLock
= IOLockAlloc();
333 #if APPLE_KEXT_VTABLE_PADDING
334 /*********************************************************************
335 * If you need this slot you had better setup an IOCTL style interface.
336 * 'Cause the whole kernel world depends on OSMetaClassBase and YOU
337 * CANT change the VTABLE size ever.
338 *********************************************************************/
340 OSMetaClassBase::_RESERVEDOSMetaClassBase7()
342 panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 7);
346 /*********************************************************************
347 *********************************************************************/
348 OSMetaClassBase::OSMetaClassBase()
352 /*********************************************************************
353 *********************************************************************/
354 OSMetaClassBase::~OSMetaClassBase()
358 thisVTable
= (void **) this;
359 *thisVTable
= (void *) -1UL;
362 /*********************************************************************
363 *********************************************************************/
365 OSMetaClassBase::isEqualTo(const OSMetaClassBase
* anObj
) const
367 return this == anObj
;
370 /*********************************************************************
371 *********************************************************************/
373 OSMetaClassBase::metaCast(const OSMetaClass
* toMeta
) const
375 return toMeta
->checkMetaCast(this);
378 /*********************************************************************
379 *********************************************************************/
381 OSMetaClassBase::metaCast(const OSSymbol
* toMetaSymb
) const
383 return OSMetaClass::checkMetaCastWithName(toMetaSymb
, this);
386 /*********************************************************************
387 *********************************************************************/
389 OSMetaClassBase::metaCast(const OSString
* toMetaStr
) const
391 const OSSymbol
* tempSymb
= OSSymbol::withString(toMetaStr
);
392 OSMetaClassBase
* ret
= NULL
;
394 ret
= metaCast(tempSymb
);
400 /*********************************************************************
401 *********************************************************************/
403 OSMetaClassBase::metaCast(const char * toMetaCStr
) const
405 const OSSymbol
* tempSymb
= OSSymbol::withCString(toMetaCStr
);
406 OSMetaClassBase
* ret
= NULL
;
408 ret
= metaCast(tempSymb
);
415 #pragma mark OSMetaClassMeta
416 #endif /* PRAGMA_MARK */
417 /*********************************************************************
418 * OSMetaClassMeta - the bootstrap metaclass of OSMetaClass
419 *********************************************************************/
420 class OSMetaClassMeta
: public OSMetaClass
424 OSObject
* alloc() const;
426 OSMetaClassMeta::OSMetaClassMeta()
427 : OSMetaClass("OSMetaClass", NULL
, sizeof(OSMetaClass
))
431 OSMetaClassMeta::alloc() const
436 static OSMetaClassMeta sOSMetaClassMeta
;
438 const OSMetaClass
* const OSMetaClass::metaClass
= &sOSMetaClassMeta
;
440 OSMetaClass::getMetaClass() const
442 return &sOSMetaClassMeta
;
446 #pragma mark OSMetaClass
447 #endif /* PRAGMA_MARK */
448 /*********************************************************************
450 *********************************************************************/
452 #if APPLE_KEXT_VTABLE_PADDING
453 /*********************************************************************
454 * Reserved functions.
455 *********************************************************************/
457 OSMetaClass::_RESERVEDOSMetaClass0()
459 panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 0);
462 OSMetaClass::_RESERVEDOSMetaClass1()
464 panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 1);
467 OSMetaClass::_RESERVEDOSMetaClass2()
469 panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 2);
472 OSMetaClass::_RESERVEDOSMetaClass3()
474 panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 3);
477 OSMetaClass::_RESERVEDOSMetaClass4()
479 panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 4);
482 OSMetaClass::_RESERVEDOSMetaClass5()
484 panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 5);
487 OSMetaClass::_RESERVEDOSMetaClass6()
489 panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 6);
492 OSMetaClass::_RESERVEDOSMetaClass7()
494 panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 7);
498 /*********************************************************************
499 *********************************************************************/
501 OSMetaClassLogErrorForKext(
505 const char * message
= NULL
;
508 case kOSReturnSuccess
:
510 case kOSMetaClassNoInit
: // xxx - never returned; logged at fail site
511 message
= "OSMetaClass: preModLoad() wasn't called (runtime internal error).";
513 case kOSMetaClassNoDicts
:
514 message
= "OSMetaClass: Allocation failure for OSMetaClass internal dictionaries.";
516 case kOSMetaClassNoKModSet
:
517 message
= "OSMetaClass: Allocation failure for internal kext recording set/set missing.";
519 case kOSMetaClassNoInsKModSet
:
520 message
= "OSMetaClass: Failed to record class in kext.";
522 case kOSMetaClassDuplicateClass
:
523 message
= "OSMetaClass: Duplicate class encountered.";
525 case kOSMetaClassNoSuper
: // xxx - never returned
526 message
= "OSMetaClass: Can't associate a class with its superclass.";
528 case kOSMetaClassInstNoSuper
: // xxx - never returned
529 message
= "OSMetaClass: Instance construction error; unknown superclass.";
531 case kOSMetaClassNoKext
:
532 message
= "OSMetaClass: Kext not found for metaclass.";
534 case kOSMetaClassInternal
:
536 message
= "OSMetaClass: Runtime internal error.";
541 OSKextLog(aKext
, kOSMetaClassLogSpec
, "%s", message
);
547 OSMetaClass::logError(OSReturn error
)
549 OSMetaClassLogErrorForKext(error
, NULL
);
552 /*********************************************************************
553 * The core constructor for a MetaClass (defined with this name always
554 * but within the scope of its represented class).
556 * MetaClass constructors are invoked in OSRuntimeInitializeCPP(),
557 * in between calls to OSMetaClass::preModLoad(), which sets up for
558 * registration, and OSMetaClass::postModLoad(), which actually
559 * records all the class/kext relationships of the new MetaClasses.
560 *********************************************************************/
562 OSMetaClass::OSMetaClass(
563 const char * inClassName
,
564 const OSMetaClass
* inSuperClass
,
565 unsigned int inClassSize
)
568 classSize
= inClassSize
;
569 superClassLink
= inSuperClass
;
571 reserved
= IONew(ExpansionData
, 1);
572 bzero(reserved
, sizeof(ExpansionData
));
574 uint32_t numSiteQs
= 0;
575 if ((this == &OSSymbol ::gMetaClass
)
576 || (this == &OSString ::gMetaClass
)
577 || (this == &OSNumber ::gMetaClass
)
578 || (this == &OSString ::gMetaClass
)
579 || (this == &OSData ::gMetaClass
)
580 || (this == &OSDictionary::gMetaClass
)
581 || (this == &OSArray ::gMetaClass
)
582 || (this == &OSSet ::gMetaClass
)) {
586 reserved
->tracking
= IOTrackingQueueAlloc(inClassName
, (uintptr_t) this,
587 inClassSize
, 0, kIOTrackingQueueTypeAlloc
,
591 /* Hack alert: We are just casting inClassName and storing it in
592 * an OSString * instance variable. This may be because you can't
593 * create C++ objects in static constructors, but I really don't know!
595 className
= (const OSSymbol
*)inClassName
;
597 // sStalledClassesLock taken in preModLoad
599 /* There's no way we can look up the kext here, unfortunately.
601 OSKextLog(/* kext */ NULL
, kOSMetaClassLogSpec
,
602 "OSMetaClass: preModLoad() wasn't called for class %s "
603 "(runtime internal error).",
605 } else if (!sStalled
->result
) {
606 // Grow stalled array if neccessary
607 if (sStalled
->count
>= sStalled
->capacity
) {
608 OSMetaClass
**oldStalled
= sStalled
->classes
;
609 int oldSize
= sStalled
->capacity
* sizeof(OSMetaClass
*);
610 int newSize
= oldSize
611 + kKModCapacityIncrement
* sizeof(OSMetaClass
*);
613 sStalled
->classes
= (OSMetaClass
**)kalloc_tag(newSize
, VM_KERN_MEMORY_OSKEXT
);
614 if (!sStalled
->classes
) {
615 sStalled
->classes
= oldStalled
;
616 sStalled
->result
= kOSMetaClassNoTempData
;
620 sStalled
->capacity
+= kKModCapacityIncrement
;
621 memmove(sStalled
->classes
, oldStalled
, oldSize
);
622 kfree(oldStalled
, oldSize
);
623 OSMETA_ACCUMSIZE(((size_t)newSize
) - ((size_t)oldSize
));
626 sStalled
->classes
[sStalled
->count
++] = this;
630 /*********************************************************************
631 *********************************************************************/
632 OSMetaClass::~OSMetaClass()
634 OSKext
* myKext
= reserved
? reserved
->kext
: NULL
; // do not release
636 /* Hack alert: 'className' is a C string during early C++ init, and
637 * is converted to a real OSSymbol only when we record the OSKext in
638 * OSMetaClass::postModLoad(). So only do this bit if we have an OSKext.
639 * We can't safely cast or check 'className'.
641 * Also, release className *after* calling into the kext,
642 * as removeClass() may access className.
644 IOLockLock(sAllClassesLock
);
645 if (sAllClassesDict
) {
647 sAllClassesDict
->removeObject(className
);
649 sAllClassesDict
->removeObject((const char *)className
);
652 IOLockUnlock(sAllClassesLock
);
655 if (myKext
->removeClass(this) != kOSReturnSuccess
) {
656 // xxx - what can we do?
658 className
->release();
661 // sStalledClassesLock taken in preModLoad
665 /* First pass find class in stalled list. If we find it that means
666 * we started C++ init with constructors but now we're tearing down
667 * because of some failure.
669 for (i
= 0; i
< sStalled
->count
; i
++) {
670 if (this == sStalled
->classes
[i
]) {
675 /* Remove this metaclass from the stalled list so postModLoad() doesn't
676 * try to register it.
678 if (i
< sStalled
->count
) {
680 if (i
< sStalled
->count
) {
681 memmove(&sStalled
->classes
[i
], &sStalled
->classes
[i
+ 1],
682 (sStalled
->count
- i
) * sizeof(OSMetaClass
*));
687 IOTrackingQueueFree(reserved
->tracking
);
689 IODelete(reserved
, ExpansionData
, 1);
692 /*********************************************************************
694 *********************************************************************/
696 OSMetaClass::retain() const
700 OSMetaClass::release() const
704 OSMetaClass::release(__unused
int when
) const
708 OSMetaClass::taggedRetain(__unused
const void * tag
) const
712 OSMetaClass::taggedRelease(__unused
const void * tag
) const
716 OSMetaClass::taggedRelease(__unused
const void * tag
, __unused
const int when
) const
720 OSMetaClass::getRetainCount() const
725 /*********************************************************************
726 *********************************************************************/
728 OSMetaClass::getClassName() const
733 return className
->getCStringNoCopy();
735 /*********************************************************************
736 *********************************************************************/
738 OSMetaClass::getClassNameSymbol() const
742 /*********************************************************************
743 *********************************************************************/
745 OSMetaClass::getClassSize() const
750 /*********************************************************************
751 *********************************************************************/
753 OSMetaClass::preModLoad(const char * kextIdentifier
)
755 IOLockLock(sStalledClassesLock
);
757 assert(sStalled
== NULL
);
758 sStalled
= (StalledData
*)kalloc_tag(sizeof(*sStalled
), VM_KERN_MEMORY_OSKEXT
);
760 sStalled
->classes
= (OSMetaClass
**)
761 kalloc_tag(kKModCapacityIncrement
* sizeof(OSMetaClass
*), VM_KERN_MEMORY_OSKEXT
);
762 if (!sStalled
->classes
) {
763 kfree(sStalled
, sizeof(*sStalled
));
766 OSMETA_ACCUMSIZE((kKModCapacityIncrement
* sizeof(OSMetaClass
*)) +
769 sStalled
->result
= kOSReturnSuccess
;
770 sStalled
->capacity
= kKModCapacityIncrement
;
772 sStalled
->kextIdentifier
= kextIdentifier
;
773 bzero(sStalled
->classes
, kKModCapacityIncrement
* sizeof(OSMetaClass
*));
776 // keep sStalledClassesLock locked until postModLoad
781 /*********************************************************************
782 *********************************************************************/
784 OSMetaClass::checkModLoad(void * loadHandle
)
786 return sStalled
&& loadHandle
== sStalled
&&
787 sStalled
->result
== kOSReturnSuccess
;
790 /*********************************************************************
791 *********************************************************************/
793 OSMetaClass::postModLoad(void * loadHandle
)
795 OSReturn result
= kOSReturnSuccess
;
796 OSSymbol
* myKextName
= NULL
;// must release
797 OSKext
* myKext
= NULL
;// must release
799 if (!sStalled
|| loadHandle
!= sStalled
) {
800 result
= kOSMetaClassInternal
;
804 if (sStalled
->result
) {
805 result
= sStalled
->result
;
807 switch (sBootstrapState
) {
808 case kNoDictionaries
:
809 sBootstrapState
= kMakingDictionaries
;
810 // No break; fall through
811 [[clang::fallthrough]];
813 case kMakingDictionaries
:
814 sAllClassesDict
= OSDictionary::withCapacity(kClassCapacityIncrement
);
815 if (!sAllClassesDict
) {
816 result
= kOSMetaClassNoDicts
;
819 sAllClassesDict
->setOptions(OSCollection::kSort
, OSCollection::kSort
);
821 // No break; fall through
822 [[clang::fallthrough]];
824 case kCompletedBootstrap
:
827 myKextName
= const_cast<OSSymbol
*>(OSSymbol::withCStringNoCopy(
828 sStalled
->kextIdentifier
));
830 if (!sStalled
->count
) {
831 break; // Nothing to do so just get out
834 myKext
= OSKext::lookupKextWithIdentifier(myKextName
);
836 result
= kOSMetaClassNoKext
;
838 /* Log this error here so we can include the kext name.
840 OSKextLog(/* kext */ NULL
, kOSMetaClassLogSpec
,
841 "OSMetaClass: Can't record classes for kext %s - kext not found.",
842 sStalled
->kextIdentifier
);
846 /* First pass checking classes aren't already loaded. If any already
847 * exist, we don't register any, and so we don't technically have
848 * to do any C++ teardown.
850 * Hack alert: me->className has been a C string until now.
851 * We only release the OSSymbol if we store the kext.
853 IOLockLock(sAllClassesLock
);
854 for (i
= 0; i
< sStalled
->count
; i
++) {
855 const OSMetaClass
* me
= sStalled
->classes
[i
];
856 OSMetaClass
* orig
= OSDynamicCast(OSMetaClass
,
857 sAllClassesDict
->getObject((const char *)me
->className
));
860 /* Log this error here so we can include the class name.
861 * xxx - we should look up the other kext that defines the class
866 OSKextLog(myKext
, kOSMetaClassLogSpec
,
867 #endif /* CONFIG_EMBEDDED */
868 "OSMetaClass: Kext %s class %s is a duplicate;"
869 "kext %s already has a class by that name.",
870 sStalled
->kextIdentifier
, (const char *)me
->className
,
871 ((OSKext
*)orig
->reserved
->kext
)->getIdentifierCString());
872 result
= kOSMetaClassDuplicateClass
;
875 unsigned int depth
= 1;
876 while ((me
= me
->superClassLink
)) {
879 if (depth
> sDeepestClass
) {
880 sDeepestClass
= depth
;
883 IOLockUnlock(sAllClassesLock
);
885 /* Bail if we didn't go through the entire list of new classes
886 * (if we hit a duplicate).
888 if (i
!= sStalled
->count
) {
892 // Second pass symbolling strings and inserting classes in dictionary
893 IOLockLock(sAllClassesLock
);
894 for (i
= 0; i
< sStalled
->count
; i
++) {
895 OSMetaClass
* me
= sStalled
->classes
[i
];
897 /* Hack alert: me->className has been a C string until now.
898 * We only release the OSSymbol in ~OSMetaClass()
899 * if we set the reference to the kext.
902 OSSymbol::withCStringNoCopy((const char *)me
->className
);
904 // xxx - I suppose if these fail we're going to panic soon....
905 sAllClassesDict
->setObject(me
->className
, me
);
907 /* Do not retain the kext object here.
909 me
->reserved
->kext
= myKext
;
911 result
= myKext
->addClass(me
, sStalled
->count
);
912 if (result
!= kOSReturnSuccess
) {
913 /* OSKext::addClass() logs with kOSMetaClassNoInsKModSet. */
918 IOLockUnlock(sAllClassesLock
);
919 sBootstrapState
= kCompletedBootstrap
;
924 result
= kOSMetaClassInternal
;
930 /* Don't call logError() for success or the conditions logged above
931 * or by called function.
933 if (result
!= kOSReturnSuccess
&&
934 result
!= kOSMetaClassNoInsKModSet
&&
935 result
!= kOSMetaClassDuplicateClass
&&
936 result
!= kOSMetaClassNoKext
) {
937 OSMetaClassLogErrorForKext(result
, myKext
);
940 OSSafeReleaseNULL(myKextName
);
941 OSSafeReleaseNULL(myKext
);
944 OSMETA_ACCUMSIZE(-(sStalled
->capacity
* sizeof(OSMetaClass
*) +
946 kfree(sStalled
->classes
, sStalled
->capacity
* sizeof(OSMetaClass
*));
947 kfree(sStalled
, sizeof(*sStalled
));
951 IOLockUnlock(sStalledClassesLock
);
957 /*********************************************************************
958 *********************************************************************/
960 OSMetaClass::instanceConstructed() const
962 // if ((0 == OSIncrementAtomic(&(((OSMetaClass *) this)->instanceCount))) && superClassLink)
963 if ((0 == OSIncrementAtomic(&instanceCount
)) && superClassLink
) {
964 superClassLink
->instanceConstructed();
968 /*********************************************************************
969 *********************************************************************/
971 OSMetaClass::instanceDestructed() const
973 if ((1 == OSDecrementAtomic(&instanceCount
)) && superClassLink
) {
974 superClassLink
->instanceDestructed();
977 if (((int)instanceCount
) < 0) {
978 OSKext
* myKext
= reserved
->kext
;
980 OSKextLog(myKext
, kOSMetaClassLogSpec
,
981 // xxx - this phrasing is rather cryptic
982 "OSMetaClass: Class %s - bad retain (%d)",
983 getClassName(), instanceCount
);
987 /*********************************************************************
988 *********************************************************************/
990 OSMetaClass::modHasInstance(const char * kextIdentifier
)
993 OSKext
* theKext
= NULL
; // must release
995 theKext
= OSKext::lookupKextWithIdentifier(kextIdentifier
);
1000 result
= theKext
->hasOSMetaClassInstances();
1003 OSSafeReleaseNULL(theKext
);
1007 /*********************************************************************
1008 *********************************************************************/
1010 OSMetaClass::reportModInstances(const char * kextIdentifier
)
1012 OSKext::reportOSMetaClassInstances(kextIdentifier
,
1013 kOSKextLogExplicitLevel
);
1016 /*********************************************************************
1017 *********************************************************************/
1020 OSMetaClass::addInstance(const OSObject
* instance
, bool super
) const
1023 IOLockLock(sInstancesLock
);
1026 if (!reserved
->instances
) {
1027 reserved
->instances
= OSOrderedSet::withCapacity(16);
1028 if (superClassLink
) {
1029 superClassLink
->addInstance(reserved
->instances
, true);
1032 reserved
->instances
->setLastObject(instance
);
1035 IOLockUnlock(sInstancesLock
);
1040 OSMetaClass::removeInstance(const OSObject
* instance
, bool super
) const
1043 IOLockLock(sInstancesLock
);
1046 if (reserved
->instances
) {
1047 reserved
->instances
->removeObject(instance
);
1048 if (0 == reserved
->instances
->getCount()) {
1049 if (superClassLink
) {
1050 superClassLink
->removeInstance(reserved
->instances
, true);
1052 IOLockLock(sAllClassesLock
);
1053 reserved
->instances
->release();
1054 reserved
->instances
= NULL
;
1055 IOLockUnlock(sAllClassesLock
);
1060 IOLockUnlock(sInstancesLock
);
1065 OSMetaClass::applyToInstances(OSOrderedSet
* set
,
1066 OSMetaClassInstanceApplierFunction applier
,
1069 enum { kLocalDepth
= 24 };
1070 unsigned int _nextIndex
[kLocalDepth
];
1071 OSOrderedSet
* _sets
[kLocalDepth
];
1072 unsigned int * nextIndex
= &_nextIndex
[0];
1073 OSOrderedSet
** sets
= &_sets
[0];
1075 OSOrderedSet
* childSet
;
1076 unsigned int maxDepth
;
1081 maxDepth
= sDeepestClass
;
1082 if (maxDepth
> kLocalDepth
) {
1083 nextIndex
= IONew(typeof(nextIndex
[0]), maxDepth
);
1084 sets
= IONew(typeof(sets
[0]), maxDepth
);
1090 while (!done
&& (obj
= set
->getObject(idx
++))) {
1091 if ((childSet
= OSDynamicCast(OSOrderedSet
, obj
))) {
1092 if (level
>= maxDepth
) {
1096 nextIndex
[level
] = idx
;
1102 done
= (*applier
)(obj
, context
);
1105 if (!done
&& level
) {
1108 idx
= nextIndex
[level
];
1114 if (maxDepth
> kLocalDepth
) {
1115 IODelete(nextIndex
, typeof(nextIndex
[0]), maxDepth
);
1116 IODelete(sets
, typeof(sets
[0]), maxDepth
);
1121 OSMetaClass::applyToInstances(OSMetaClassInstanceApplierFunction applier
,
1122 void * context
) const
1124 IOLockLock(sInstancesLock
);
1125 if (reserved
->instances
) {
1126 applyToInstances(reserved
->instances
, applier
, context
);
1128 IOLockUnlock(sInstancesLock
);
1132 OSMetaClass::applyToInstancesOfClassName(
1133 const OSSymbol
* name
,
1134 OSMetaClassInstanceApplierFunction applier
,
1138 OSOrderedSet
* set
= NULL
;
1140 IOLockLock(sAllClassesLock
);
1142 && (meta
= (OSMetaClass
*) sAllClassesDict
->getObject(name
))
1143 && (set
= meta
->reserved
->instances
)) {
1146 IOLockUnlock(sAllClassesLock
);
1152 IOLockLock(sInstancesLock
);
1153 applyToInstances(set
, applier
, context
);
1154 IOLockUnlock(sInstancesLock
);
1158 /*********************************************************************
1159 *********************************************************************/
1161 OSMetaClass::considerUnloads()
1163 OSKext::considerUnloads();
1166 /*********************************************************************
1167 *********************************************************************/
1169 OSMetaClass::removeClasses(OSCollection
* metaClasses
)
1171 OSCollectionIterator
* classIterator
;
1172 OSMetaClass
* checkClass
;
1175 classIterator
= OSCollectionIterator::withCollection(metaClasses
);
1176 if (!classIterator
) {
1180 IOLockLock(sAllClassesLock
);
1184 while ((checkClass
= (OSMetaClass
*)classIterator
->getNextObject())
1185 && !checkClass
->getInstanceCount()
1186 && !checkClass
->reserved
->retain
) {
1191 classIterator
->reset();
1192 while ((checkClass
= (OSMetaClass
*)classIterator
->getNextObject())) {
1193 sAllClassesDict
->removeObject(checkClass
->className
);
1198 IOLockUnlock(sAllClassesLock
);
1199 OSSafeReleaseNULL(classIterator
);
1205 /*********************************************************************
1206 *********************************************************************/
1208 OSMetaClass::getMetaClassWithName(const OSSymbol
* name
)
1210 OSMetaClass
* retMeta
= NULL
;
1216 IOLockLock(sAllClassesLock
);
1217 if (sAllClassesDict
) {
1218 retMeta
= (OSMetaClass
*) sAllClassesDict
->getObject(name
);
1220 IOLockUnlock(sAllClassesLock
);
1225 /*********************************************************************
1226 *********************************************************************/
1228 OSMetaClass::copyMetaClassWithName(const OSSymbol
* name
)
1230 const OSMetaClass
* meta
;
1237 IOLockLock(sAllClassesLock
);
1238 if (sAllClassesDict
) {
1239 meta
= (OSMetaClass
*) sAllClassesDict
->getObject(name
);
1241 OSIncrementAtomic(&meta
->reserved
->retain
);
1244 IOLockUnlock(sAllClassesLock
);
1249 /*********************************************************************
1250 *********************************************************************/
1252 OSMetaClass::releaseMetaClass() const
1254 OSDecrementAtomic(&reserved
->retain
);
1257 /*********************************************************************
1258 *********************************************************************/
1260 OSMetaClass::allocClassWithName(const OSSymbol
* name
)
1262 const OSMetaClass
* meta
;
1266 meta
= copyMetaClassWithName(name
);
1268 result
= meta
->alloc();
1269 meta
->releaseMetaClass();
1275 /*********************************************************************
1276 *********************************************************************/
1278 OSMetaClass::allocClassWithName(const OSString
* name
)
1280 const OSSymbol
* tmpKey
= OSSymbol::withString(name
);
1281 OSObject
* result
= allocClassWithName(tmpKey
);
1286 /*********************************************************************
1287 *********************************************************************/
1289 OSMetaClass::allocClassWithName(const char * name
)
1291 const OSSymbol
* tmpKey
= OSSymbol::withCStringNoCopy(name
);
1292 OSObject
* result
= allocClassWithName(tmpKey
);
1298 /*********************************************************************
1299 *********************************************************************/
1301 OSMetaClass::checkMetaCastWithName(
1302 const OSSymbol
* name
,
1303 const OSMetaClassBase
* in
)
1305 OSMetaClassBase
* result
= NULL
;
1307 const OSMetaClass
* const meta
= getMetaClassWithName(name
);
1310 result
= meta
->checkMetaCast(in
);
1316 /*********************************************************************
1317 *********************************************************************/
1320 checkMetaCastWithName(
1321 const OSString
* name
,
1322 const OSMetaClassBase
* in
)
1324 const OSSymbol
* tmpKey
= OSSymbol::withString(name
);
1325 OSMetaClassBase
* result
= checkMetaCastWithName(tmpKey
, in
);
1331 /*********************************************************************
1332 *********************************************************************/
1334 OSMetaClass::checkMetaCastWithName(
1336 const OSMetaClassBase
* in
)
1338 const OSSymbol
* tmpKey
= OSSymbol::withCStringNoCopy(name
);
1339 OSMetaClassBase
* result
= checkMetaCastWithName(tmpKey
, in
);
1345 /*********************************************************************
1346 * OSMetaClass::checkMetaCast()
1347 * Check to see if the 'check' object has this object in its metaclass chain.
1348 * Returns check if it is indeed a kind of the current meta class, 0 otherwise.
1350 * Generally this method is not invoked directly but is used to implement
1351 * the OSMetaClassBase::metaCast member function.
1353 * See also OSMetaClassBase::metaCast
1354 *********************************************************************/
1356 OSMetaClass::checkMetaCast(
1357 const OSMetaClassBase
* check
) const
1359 const OSMetaClass
* const toMeta
= this;
1360 const OSMetaClass
* fromMeta
;
1362 for (fromMeta
= check
->getMetaClass();; fromMeta
= fromMeta
->superClassLink
) {
1363 if (toMeta
== fromMeta
) {
1364 return const_cast<OSMetaClassBase
*>(check
); // Discard const
1366 if (!fromMeta
->superClassLink
) {
1374 /*********************************************************************
1375 *********************************************************************/
1378 OSMetaClass::reservedCalled(int ind
) const
1380 const char * cname
= className
->getCStringNoCopy();
1381 panic("%s::_RESERVED%s%d called.", cname
, cname
, ind
);
1384 /*********************************************************************
1385 *********************************************************************/
1388 OSMetaClass::getSuperClass() const
1390 return superClassLink
;
1393 /*********************************************************************
1394 *********************************************************************/
1396 OSMetaClass::getKmodName() const
1398 OSKext
* myKext
= reserved
? reserved
->kext
: NULL
;
1400 return myKext
->getIdentifier();
1402 return OSSymbol::withCStringNoCopy("unknown");
1405 /*********************************************************************
1406 *********************************************************************/
1408 OSMetaClass::getKext() const
1410 return reserved
? reserved
->kext
: NULL
;
1413 /*********************************************************************
1414 *********************************************************************/
1416 OSMetaClass::getInstanceCount() const
1418 return instanceCount
;
1421 /*********************************************************************
1422 *********************************************************************/
1425 OSMetaClass::printInstanceCounts()
1427 OSCollectionIterator
* classes
;
1428 OSSymbol
* className
;
1431 IOLockLock(sAllClassesLock
);
1432 classes
= OSCollectionIterator::withCollection(sAllClassesDict
);
1435 while ((className
= (OSSymbol
*)classes
->getNextObject())) {
1436 meta
= (OSMetaClass
*)sAllClassesDict
->getObject(className
);
1439 printf("%24s count: %03d x 0x%03x = 0x%06x\n",
1440 className
->getCStringNoCopy(),
1441 meta
->getInstanceCount(),
1442 meta
->getClassSize(),
1443 meta
->getInstanceCount() * meta
->getClassSize());
1447 IOLockUnlock(sAllClassesLock
);
1451 /*********************************************************************
1452 *********************************************************************/
1454 OSMetaClass::getClassDictionary()
1456 panic("OSMetaClass::getClassDictionary() is obsoleted.\n");
1460 /*********************************************************************
1461 *********************************************************************/
1463 OSMetaClass::serialize(__unused OSSerialize
* s
) const
1465 panic("OSMetaClass::serialize(): Obsoleted\n");
1469 /*********************************************************************
1470 *********************************************************************/
1473 OSMetaClass::serializeClassDictionary(OSDictionary
* serializeDictionary
)
1475 OSDictionary
* classDict
= NULL
;
1477 IOLockLock(sAllClassesLock
);
1479 classDict
= OSDictionary::withCapacity(sAllClassesDict
->getCount());
1485 OSCollectionIterator
* classes
;
1486 const OSSymbol
* className
;
1488 classes
= OSCollectionIterator::withCollection(sAllClassesDict
);
1493 while ((className
= (const OSSymbol
*)classes
->getNextObject())) {
1494 const OSMetaClass
* meta
;
1497 meta
= (OSMetaClass
*)sAllClassesDict
->getObject(className
);
1498 count
= OSNumber::withNumber(meta
->getInstanceCount(), 32);
1500 classDict
->setObject(className
, count
);
1506 serializeDictionary
->setObject("Classes", classDict
);
1510 OSSafeReleaseNULL(classDict
);
1512 IOLockUnlock(sAllClassesLock
);
1518 /*********************************************************************
1519 *********************************************************************/
1524 OSMetaClass::trackedNew(size_t size
)
1528 mem
= (typeof(mem
))kalloc_tag_bt(size
+ sizeof(IOTracking
), VM_KERN_MEMORY_LIBKERN
);
1534 memset(mem
, 0, size
+ sizeof(IOTracking
));
1537 OSIVAR_ACCUMSIZE(size
);
1543 OSMetaClass::trackedDelete(void * instance
, size_t size
)
1545 IOTracking
* mem
= (typeof(mem
))instance
; mem
--;
1547 kfree(mem
, size
+ sizeof(IOTracking
));
1548 OSIVAR_ACCUMSIZE(-size
);
1552 OSMetaClass::trackedInstance(OSObject
* instance
) const
1554 IOTracking
* mem
= (typeof(mem
))instance
; mem
--;
1556 return IOTrackingAdd(reserved
->tracking
, mem
, classSize
, false, VM_KERN_MEMORY_NONE
);
1560 OSMetaClass::trackedFree(OSObject
* instance
) const
1562 IOTracking
* mem
= (typeof(mem
))instance
; mem
--;
1564 return IOTrackingRemove(reserved
->tracking
, mem
, classSize
);
1568 OSMetaClass::trackedAccumSize(OSObject
* instance
, size_t size
) const
1570 IOTracking
* mem
= (typeof(mem
))instance
; mem
--;
1572 return IOTrackingAccumSize(reserved
->tracking
, mem
, size
);
1576 OSMetaClass::getTracking() const
1578 return reserved
->tracking
;
1581 #endif /* IOTRACKING */