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>
66 #endif /* PRAGMA_MARK */
67 /*********************************************************************
69 *********************************************************************/
73 #pragma mark Internal constants & data structs
74 #endif /* PRAGMA_MARK */
75 /*********************************************************************
76 * Internal constants & data structs
77 *********************************************************************/
78 OSKextLogSpec kOSMetaClassLogSpec
=
79 kOSKextLogErrorLevel
|
81 kOSKextLogKextBookkeepingFlag
;
84 kCompletedBootstrap
= 0,
86 kMakingDictionaries
= 2
87 } sBootstrapState
= kNoDictionaries
;
89 static const int kClassCapacityIncrement
= 40;
90 static const int kKModCapacityIncrement
= 10;
91 static OSDictionary
* sAllClassesDict
;
92 static unsigned int sDeepestClass
;
93 IOLock
* sAllClassesLock
= NULL
;
94 IOLock
* sInstancesLock
= NULL
;
97 * While loading a kext and running all its constructors to register
98 * all OSMetaClass classes, the classes are queued up here. Only one
99 * kext can be in flight at a time, guarded by sStalledClassesLock
101 static struct StalledData
{
102 const char * kextIdentifier
;
104 unsigned int capacity
;
106 OSMetaClass
** classes
;
108 IOLock
* sStalledClassesLock
= NULL
;
110 struct ExpansionData
{
111 OSOrderedSet
* instances
;
115 IOTrackingQueue
* tracking
;
121 #pragma mark OSMetaClassBase
122 #endif /* PRAGMA_MARK */
123 /*********************************************************************
125 *********************************************************************/
127 #if APPLE_KEXT_VTABLE_PADDING
128 /*********************************************************************
129 * Reserved vtable functions.
130 *********************************************************************/
133 OSMetaClassBase::_RESERVEDOSMetaClassBase0()
135 panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 0);
138 OSMetaClassBase::_RESERVEDOSMetaClassBase1()
140 panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 1);
143 OSMetaClassBase::_RESERVEDOSMetaClassBase2()
145 panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 2);
147 #endif /* SLOT_USED */
149 // As these slots are used move them up inside the #if above
151 OSMetaClassBase::_RESERVEDOSMetaClassBase3()
153 panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 3);
156 OSMetaClassBase::_RESERVEDOSMetaClassBase4()
158 panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 4);
161 OSMetaClassBase::_RESERVEDOSMetaClassBase5()
163 panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 5);
166 OSMetaClassBase::_RESERVEDOSMetaClassBase6()
168 panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 6);
173 /*********************************************************************
174 *********************************************************************/
176 #if defined(__arm__) || defined(__arm64__)
181 * IHI0059A "C++ Application Binary Interface Standard for the ARM 64 - bit Architecture":
183 * 3.2.1 Representation of pointer to member function The generic C++ ABI [GC++ABI]
184 * specifies that a pointer to member function is a pair of words <ptr, adj>. The
185 * least significant bit of ptr discriminates between (0) the address of a non-
186 * virtual member function and (1) the offset in the class's virtual table of the
187 * address of a virtual function. This encoding cannot work for the AArch64
188 * instruction set where the architecture reserves all bits of code addresses. This
189 * ABI specifies that adj contains twice the this adjustment, plus 1 if the member
190 * function is virtual. The least significant bit of adj then makes exactly the
191 * same discrimination as the least significant bit of ptr does for Itanium. A
192 * pointer to member function is NULL when ptr = 0 and the least significant bit of
196 OSMetaClassBase::_ptf_t
197 OSMetaClassBase::_ptmf2ptf(const OSMetaClassBase
*self
, void (OSMetaClassBase::*func
)(void))
199 typedef long int ptrdiff_t;
205 void (OSMetaClassBase::*fIn
)(void);
213 if (map
.pTMF
.delta
& 1) {
216 const OSMetaClassBase
*fObj
;
221 // Virtual member function so dereference table
222 pfn
= *(_ptf_t
*)(((uintptr_t)*u
.vtablep
) + (uintptr_t)pfn
);
225 // Not virtual, i.e. plain member func
230 #endif /* defined(__arm__) || defined(__arm64__) */
231 /*********************************************************************
232 * These used to be inline in the header but gcc didn't believe us
233 * Now we MUST pull the inline out at least until the compiler is
236 * Helper inlines for runtime type preprocessor macros
237 *********************************************************************/
239 /*********************************************************************
240 *********************************************************************/
242 OSMetaClassBase::safeMetaCast(
243 const OSMetaClassBase
* me
,
244 const OSMetaClass
* toType
)
246 return (me
)? me
->metaCast(toType
) : 0;
249 /*********************************************************************
250 *********************************************************************/
252 OSMetaClassBase::checkTypeInst(
253 const OSMetaClassBase
* inst
,
254 const OSMetaClassBase
* typeinst
)
256 const OSMetaClass
* toType
= OSTypeIDInst(typeinst
);
257 return typeinst
&& inst
&& (0 != inst
->metaCast(toType
));
260 /*********************************************************************
261 *********************************************************************/
266 sAllClassesLock
= IOLockAlloc();
267 sStalledClassesLock
= IOLockAlloc();
268 sInstancesLock
= IOLockAlloc();
271 #if APPLE_KEXT_VTABLE_PADDING
272 /*********************************************************************
273 * If you need this slot you had better setup an IOCTL style interface.
274 * 'Cause the whole kernel world depends on OSMetaClassBase and YOU
275 * CANT change the VTABLE size ever.
276 *********************************************************************/
278 OSMetaClassBase::_RESERVEDOSMetaClassBase7()
280 panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 7);
284 /*********************************************************************
285 *********************************************************************/
286 OSMetaClassBase::OSMetaClassBase()
290 /*********************************************************************
291 *********************************************************************/
292 OSMetaClassBase::~OSMetaClassBase()
296 thisVTable
= (void **) this;
297 *thisVTable
= (void *) -1UL;
300 /*********************************************************************
301 *********************************************************************/
303 OSMetaClassBase::isEqualTo(const OSMetaClassBase
* anObj
) const
305 return this == anObj
;
308 /*********************************************************************
309 *********************************************************************/
311 OSMetaClassBase::metaCast(const OSMetaClass
* toMeta
) const
313 return toMeta
->checkMetaCast(this);
316 /*********************************************************************
317 *********************************************************************/
319 OSMetaClassBase::metaCast(const OSSymbol
* toMetaSymb
) const
321 return OSMetaClass::checkMetaCastWithName(toMetaSymb
, this);
324 /*********************************************************************
325 *********************************************************************/
327 OSMetaClassBase::metaCast(const OSString
* toMetaStr
) const
329 const OSSymbol
* tempSymb
= OSSymbol::withString(toMetaStr
);
330 OSMetaClassBase
* ret
= 0;
332 ret
= metaCast(tempSymb
);
338 /*********************************************************************
339 *********************************************************************/
341 OSMetaClassBase::metaCast(const char * toMetaCStr
) const
343 const OSSymbol
* tempSymb
= OSSymbol::withCString(toMetaCStr
);
344 OSMetaClassBase
* ret
= 0;
346 ret
= metaCast(tempSymb
);
353 #pragma mark OSMetaClassMeta
354 #endif /* PRAGMA_MARK */
355 /*********************************************************************
356 * OSMetaClassMeta - the bootstrap metaclass of OSMetaClass
357 *********************************************************************/
358 class OSMetaClassMeta
: public OSMetaClass
362 OSObject
* alloc() const;
364 OSMetaClassMeta::OSMetaClassMeta()
365 : OSMetaClass("OSMetaClass", 0, sizeof(OSMetaClass
))
369 OSMetaClassMeta::alloc() const
374 static OSMetaClassMeta sOSMetaClassMeta
;
376 const OSMetaClass
* const OSMetaClass::metaClass
= &sOSMetaClassMeta
;
378 OSMetaClass::getMetaClass() const
380 return &sOSMetaClassMeta
;
384 #pragma mark OSMetaClass
385 #endif /* PRAGMA_MARK */
386 /*********************************************************************
388 *********************************************************************/
390 #if APPLE_KEXT_VTABLE_PADDING
391 /*********************************************************************
392 * Reserved functions.
393 *********************************************************************/
395 OSMetaClass::_RESERVEDOSMetaClass0()
397 panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 0);
400 OSMetaClass::_RESERVEDOSMetaClass1()
402 panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 1);
405 OSMetaClass::_RESERVEDOSMetaClass2()
407 panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 2);
410 OSMetaClass::_RESERVEDOSMetaClass3()
412 panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 3);
415 OSMetaClass::_RESERVEDOSMetaClass4()
417 panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 4);
420 OSMetaClass::_RESERVEDOSMetaClass5()
422 panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 5);
425 OSMetaClass::_RESERVEDOSMetaClass6()
427 panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 6);
430 OSMetaClass::_RESERVEDOSMetaClass7()
432 panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 7);
436 /*********************************************************************
437 *********************************************************************/
439 OSMetaClassLogErrorForKext(
443 const char * message
= NULL
;
446 case kOSReturnSuccess
:
448 case kOSMetaClassNoInit
: // xxx - never returned; logged at fail site
449 message
= "OSMetaClass: preModLoad() wasn't called (runtime internal error).";
451 case kOSMetaClassNoDicts
:
452 message
= "OSMetaClass: Allocation failure for OSMetaClass internal dictionaries.";
454 case kOSMetaClassNoKModSet
:
455 message
= "OSMetaClass: Allocation failure for internal kext recording set/set missing.";
457 case kOSMetaClassNoInsKModSet
:
458 message
= "OSMetaClass: Failed to record class in kext.";
460 case kOSMetaClassDuplicateClass
:
461 message
= "OSMetaClass: Duplicate class encountered.";
463 case kOSMetaClassNoSuper
: // xxx - never returned
464 message
= "OSMetaClass: Can't associate a class with its superclass.";
466 case kOSMetaClassInstNoSuper
: // xxx - never returned
467 message
= "OSMetaClass: Instance construction error; unknown superclass.";
469 case kOSMetaClassNoKext
:
470 message
= "OSMetaClass: Kext not found for metaclass.";
472 case kOSMetaClassInternal
:
474 message
= "OSMetaClass: Runtime internal error.";
479 OSKextLog(aKext
, kOSMetaClassLogSpec
, "%s", message
);
485 OSMetaClass::logError(OSReturn error
)
487 OSMetaClassLogErrorForKext(error
, NULL
);
490 /*********************************************************************
491 * The core constructor for a MetaClass (defined with this name always
492 * but within the scope of its represented class).
494 * MetaClass constructors are invoked in OSRuntimeInitializeCPP(),
495 * in between calls to OSMetaClass::preModLoad(), which sets up for
496 * registration, and OSMetaClass::postModLoad(), which actually
497 * records all the class/kext relationships of the new MetaClasses.
498 *********************************************************************/
499 OSMetaClass::OSMetaClass(
500 const char * inClassName
,
501 const OSMetaClass
* inSuperClass
,
502 unsigned int inClassSize
)
505 classSize
= inClassSize
;
506 superClassLink
= inSuperClass
;
508 reserved
= IONew(ExpansionData
, 1);
509 bzero(reserved
, sizeof(ExpansionData
));
511 uint32_t numSiteQs
= 0;
512 if ((this == &OSSymbol ::gMetaClass
)
513 || (this == &OSString ::gMetaClass
)
514 || (this == &OSNumber ::gMetaClass
)
515 || (this == &OSString ::gMetaClass
)
516 || (this == &OSData ::gMetaClass
)
517 || (this == &OSDictionary::gMetaClass
)
518 || (this == &OSArray ::gMetaClass
)
519 || (this == &OSSet ::gMetaClass
)) {
523 reserved
->tracking
= IOTrackingQueueAlloc(inClassName
, (uintptr_t) this,
524 inClassSize
, 0, kIOTrackingQueueTypeAlloc
,
528 /* Hack alert: We are just casting inClassName and storing it in
529 * an OSString * instance variable. This may be because you can't
530 * create C++ objects in static constructors, but I really don't know!
532 className
= (const OSSymbol
*)inClassName
;
534 // sStalledClassesLock taken in preModLoad
536 /* There's no way we can look up the kext here, unfortunately.
538 OSKextLog(/* kext */ NULL
, kOSMetaClassLogSpec
,
539 "OSMetaClass: preModLoad() wasn't called for class %s "
540 "(runtime internal error).",
542 } else if (!sStalled
->result
) {
543 // Grow stalled array if neccessary
544 if (sStalled
->count
>= sStalled
->capacity
) {
545 OSMetaClass
**oldStalled
= sStalled
->classes
;
546 int oldSize
= sStalled
->capacity
* sizeof(OSMetaClass
*);
547 int newSize
= oldSize
548 + kKModCapacityIncrement
* sizeof(OSMetaClass
*);
550 sStalled
->classes
= (OSMetaClass
**)kalloc_tag(newSize
, VM_KERN_MEMORY_OSKEXT
);
551 if (!sStalled
->classes
) {
552 sStalled
->classes
= oldStalled
;
553 sStalled
->result
= kOSMetaClassNoTempData
;
557 sStalled
->capacity
+= kKModCapacityIncrement
;
558 memmove(sStalled
->classes
, oldStalled
, oldSize
);
559 kfree(oldStalled
, oldSize
);
560 OSMETA_ACCUMSIZE(((size_t)newSize
) - ((size_t)oldSize
));
563 sStalled
->classes
[sStalled
->count
++] = this;
567 /*********************************************************************
568 *********************************************************************/
569 OSMetaClass::~OSMetaClass()
571 OSKext
* myKext
= reserved
? reserved
->kext
: 0; // do not release
573 /* Hack alert: 'className' is a C string during early C++ init, and
574 * is converted to a real OSSymbol only when we record the OSKext in
575 * OSMetaClass::postModLoad(). So only do this bit if we have an OSKext.
576 * We can't safely cast or check 'className'.
578 * Also, release className *after* calling into the kext,
579 * as removeClass() may access className.
581 IOLockLock(sAllClassesLock
);
582 if (sAllClassesDict
) {
584 sAllClassesDict
->removeObject(className
);
586 sAllClassesDict
->removeObject((const char *)className
);
589 IOLockUnlock(sAllClassesLock
);
592 if (myKext
->removeClass(this) != kOSReturnSuccess
) {
593 // xxx - what can we do?
595 className
->release();
598 // sStalledClassesLock taken in preModLoad
602 /* First pass find class in stalled list. If we find it that means
603 * we started C++ init with constructors but now we're tearing down
604 * because of some failure.
606 for (i
= 0; i
< sStalled
->count
; i
++) {
607 if (this == sStalled
->classes
[i
]) {
612 /* Remove this metaclass from the stalled list so postModLoad() doesn't
613 * try to register it.
615 if (i
< sStalled
->count
) {
617 if (i
< sStalled
->count
) {
618 memmove(&sStalled
->classes
[i
], &sStalled
->classes
[i
+ 1],
619 (sStalled
->count
- i
) * sizeof(OSMetaClass
*));
624 IOTrackingQueueFree(reserved
->tracking
);
626 IODelete(reserved
, ExpansionData
, 1);
629 /*********************************************************************
631 *********************************************************************/
633 OSMetaClass::retain() const
637 OSMetaClass::release() const
641 OSMetaClass::release(__unused
int when
) const
645 OSMetaClass::taggedRetain(__unused
const void * tag
) const
649 OSMetaClass::taggedRelease(__unused
const void * tag
) const
653 OSMetaClass::taggedRelease(__unused
const void * tag
, __unused
const int when
) const
657 OSMetaClass::getRetainCount() const
662 /*********************************************************************
663 *********************************************************************/
665 OSMetaClass::getClassName() const
670 return className
->getCStringNoCopy();
672 /*********************************************************************
673 *********************************************************************/
675 OSMetaClass::getClassNameSymbol() const
679 /*********************************************************************
680 *********************************************************************/
682 OSMetaClass::getClassSize() const
687 /*********************************************************************
688 *********************************************************************/
690 OSMetaClass::preModLoad(const char * kextIdentifier
)
692 IOLockLock(sStalledClassesLock
);
694 assert(sStalled
== NULL
);
695 sStalled
= (StalledData
*)kalloc_tag(sizeof(*sStalled
), VM_KERN_MEMORY_OSKEXT
);
697 sStalled
->classes
= (OSMetaClass
**)
698 kalloc_tag(kKModCapacityIncrement
* sizeof(OSMetaClass
*), VM_KERN_MEMORY_OSKEXT
);
699 if (!sStalled
->classes
) {
700 kfree(sStalled
, sizeof(*sStalled
));
703 OSMETA_ACCUMSIZE((kKModCapacityIncrement
* sizeof(OSMetaClass
*)) +
706 sStalled
->result
= kOSReturnSuccess
;
707 sStalled
->capacity
= kKModCapacityIncrement
;
709 sStalled
->kextIdentifier
= kextIdentifier
;
710 bzero(sStalled
->classes
, kKModCapacityIncrement
* sizeof(OSMetaClass
*));
713 // keep sStalledClassesLock locked until postModLoad
718 /*********************************************************************
719 *********************************************************************/
721 OSMetaClass::checkModLoad(void * loadHandle
)
723 return sStalled
&& loadHandle
== sStalled
&&
724 sStalled
->result
== kOSReturnSuccess
;
727 /*********************************************************************
728 *********************************************************************/
730 OSMetaClass::postModLoad(void * loadHandle
)
732 OSReturn result
= kOSReturnSuccess
;
733 OSSymbol
* myKextName
= 0;// must release
734 OSKext
* myKext
= 0;// must release
736 if (!sStalled
|| loadHandle
!= sStalled
) {
737 result
= kOSMetaClassInternal
;
741 if (sStalled
->result
) {
742 result
= sStalled
->result
;
744 switch (sBootstrapState
) {
745 case kNoDictionaries
:
746 sBootstrapState
= kMakingDictionaries
;
747 // No break; fall through
748 [[clang::fallthrough]];
750 case kMakingDictionaries
:
751 sAllClassesDict
= OSDictionary::withCapacity(kClassCapacityIncrement
);
752 if (!sAllClassesDict
) {
753 result
= kOSMetaClassNoDicts
;
756 sAllClassesDict
->setOptions(OSCollection::kSort
, OSCollection::kSort
);
758 // No break; fall through
759 [[clang::fallthrough]];
761 case kCompletedBootstrap
:
764 myKextName
= const_cast<OSSymbol
*>(OSSymbol::withCStringNoCopy(
765 sStalled
->kextIdentifier
));
767 if (!sStalled
->count
) {
768 break; // Nothing to do so just get out
771 myKext
= OSKext::lookupKextWithIdentifier(myKextName
);
773 result
= kOSMetaClassNoKext
;
775 /* Log this error here so we can include the kext name.
777 OSKextLog(/* kext */ NULL
, kOSMetaClassLogSpec
,
778 "OSMetaClass: Can't record classes for kext %s - kext not found.",
779 sStalled
->kextIdentifier
);
783 /* First pass checking classes aren't already loaded. If any already
784 * exist, we don't register any, and so we don't technically have
785 * to do any C++ teardown.
787 * Hack alert: me->className has been a C string until now.
788 * We only release the OSSymbol if we store the kext.
790 IOLockLock(sAllClassesLock
);
791 for (i
= 0; i
< sStalled
->count
; i
++) {
792 const OSMetaClass
* me
= sStalled
->classes
[i
];
793 OSMetaClass
* orig
= OSDynamicCast(OSMetaClass
,
794 sAllClassesDict
->getObject((const char *)me
->className
));
797 /* Log this error here so we can include the class name.
798 * xxx - we should look up the other kext that defines the class
803 OSKextLog(myKext
, kOSMetaClassLogSpec
,
804 #endif /* CONFIG_EMBEDDED */
805 "OSMetaClass: Kext %s class %s is a duplicate;"
806 "kext %s already has a class by that name.",
807 sStalled
->kextIdentifier
, (const char *)me
->className
,
808 ((OSKext
*)orig
->reserved
->kext
)->getIdentifierCString());
809 result
= kOSMetaClassDuplicateClass
;
812 unsigned int depth
= 1;
813 while ((me
= me
->superClassLink
)) {
816 if (depth
> sDeepestClass
) {
817 sDeepestClass
= depth
;
820 IOLockUnlock(sAllClassesLock
);
822 /* Bail if we didn't go through the entire list of new classes
823 * (if we hit a duplicate).
825 if (i
!= sStalled
->count
) {
829 // Second pass symbolling strings and inserting classes in dictionary
830 IOLockLock(sAllClassesLock
);
831 for (i
= 0; i
< sStalled
->count
; i
++) {
832 OSMetaClass
* me
= sStalled
->classes
[i
];
834 /* Hack alert: me->className has been a C string until now.
835 * We only release the OSSymbol in ~OSMetaClass()
836 * if we set the reference to the kext.
839 OSSymbol::withCStringNoCopy((const char *)me
->className
);
841 // xxx - I suppose if these fail we're going to panic soon....
842 sAllClassesDict
->setObject(me
->className
, me
);
844 /* Do not retain the kext object here.
846 me
->reserved
->kext
= myKext
;
848 result
= myKext
->addClass(me
, sStalled
->count
);
849 if (result
!= kOSReturnSuccess
) {
850 /* OSKext::addClass() logs with kOSMetaClassNoInsKModSet. */
855 IOLockUnlock(sAllClassesLock
);
856 sBootstrapState
= kCompletedBootstrap
;
861 result
= kOSMetaClassInternal
;
867 /* Don't call logError() for success or the conditions logged above
868 * or by called function.
870 if (result
!= kOSReturnSuccess
&&
871 result
!= kOSMetaClassNoInsKModSet
&&
872 result
!= kOSMetaClassDuplicateClass
&&
873 result
!= kOSMetaClassNoKext
) {
874 OSMetaClassLogErrorForKext(result
, myKext
);
877 OSSafeReleaseNULL(myKextName
);
878 OSSafeReleaseNULL(myKext
);
881 OSMETA_ACCUMSIZE(-(sStalled
->capacity
* sizeof(OSMetaClass
*) +
883 kfree(sStalled
->classes
, sStalled
->capacity
* sizeof(OSMetaClass
*));
884 kfree(sStalled
, sizeof(*sStalled
));
888 IOLockUnlock(sStalledClassesLock
);
894 /*********************************************************************
895 *********************************************************************/
897 OSMetaClass::instanceConstructed() const
899 // if ((0 == OSIncrementAtomic(&(((OSMetaClass *) this)->instanceCount))) && superClassLink)
900 if ((0 == OSIncrementAtomic(&instanceCount
)) && superClassLink
) {
901 superClassLink
->instanceConstructed();
905 /*********************************************************************
906 *********************************************************************/
908 OSMetaClass::instanceDestructed() const
910 if ((1 == OSDecrementAtomic(&instanceCount
)) && superClassLink
) {
911 superClassLink
->instanceDestructed();
914 if (((int)instanceCount
) < 0) {
915 OSKext
* myKext
= reserved
->kext
;
917 OSKextLog(myKext
, kOSMetaClassLogSpec
,
918 // xxx - this phrasing is rather cryptic
919 "OSMetaClass: Class %s - bad retain (%d)",
920 getClassName(), instanceCount
);
924 /*********************************************************************
925 *********************************************************************/
927 OSMetaClass::modHasInstance(const char * kextIdentifier
)
930 OSKext
* theKext
= NULL
; // must release
932 theKext
= OSKext::lookupKextWithIdentifier(kextIdentifier
);
937 result
= theKext
->hasOSMetaClassInstances();
940 OSSafeReleaseNULL(theKext
);
944 /*********************************************************************
945 *********************************************************************/
947 OSMetaClass::reportModInstances(const char * kextIdentifier
)
949 OSKext::reportOSMetaClassInstances(kextIdentifier
,
950 kOSKextLogExplicitLevel
);
953 /*********************************************************************
954 *********************************************************************/
957 OSMetaClass::addInstance(const OSObject
* instance
, bool super
) const
960 IOLockLock(sInstancesLock
);
963 if (!reserved
->instances
) {
964 reserved
->instances
= OSOrderedSet::withCapacity(16);
965 if (superClassLink
) {
966 superClassLink
->addInstance(reserved
->instances
, true);
969 reserved
->instances
->setLastObject(instance
);
972 IOLockUnlock(sInstancesLock
);
977 OSMetaClass::removeInstance(const OSObject
* instance
, bool super
) const
980 IOLockLock(sInstancesLock
);
983 if (reserved
->instances
) {
984 reserved
->instances
->removeObject(instance
);
985 if (0 == reserved
->instances
->getCount()) {
986 if (superClassLink
) {
987 superClassLink
->removeInstance(reserved
->instances
, true);
989 IOLockLock(sAllClassesLock
);
990 reserved
->instances
->release();
991 reserved
->instances
= 0;
992 IOLockUnlock(sAllClassesLock
);
997 IOLockUnlock(sInstancesLock
);
1002 OSMetaClass::applyToInstances(OSOrderedSet
* set
,
1003 OSMetaClassInstanceApplierFunction applier
,
1006 enum { kLocalDepth
= 24 };
1007 unsigned int _nextIndex
[kLocalDepth
];
1008 OSOrderedSet
* _sets
[kLocalDepth
];
1009 unsigned int * nextIndex
= &_nextIndex
[0];
1010 OSOrderedSet
** sets
= &_sets
[0];
1012 OSOrderedSet
* childSet
;
1013 unsigned int maxDepth
;
1018 maxDepth
= sDeepestClass
;
1019 if (maxDepth
> kLocalDepth
) {
1020 nextIndex
= IONew(typeof(nextIndex
[0]), maxDepth
);
1021 sets
= IONew(typeof(sets
[0]), maxDepth
);
1027 while (!done
&& (obj
= set
->getObject(idx
++))) {
1028 if ((childSet
= OSDynamicCast(OSOrderedSet
, obj
))) {
1029 if (level
>= maxDepth
) {
1033 nextIndex
[level
] = idx
;
1039 done
= (*applier
)(obj
, context
);
1042 if (!done
&& level
) {
1045 idx
= nextIndex
[level
];
1051 if (maxDepth
> kLocalDepth
) {
1052 IODelete(nextIndex
, typeof(nextIndex
[0]), maxDepth
);
1053 IODelete(sets
, typeof(sets
[0]), maxDepth
);
1058 OSMetaClass::applyToInstances(OSMetaClassInstanceApplierFunction applier
,
1059 void * context
) const
1061 IOLockLock(sInstancesLock
);
1062 if (reserved
->instances
) {
1063 applyToInstances(reserved
->instances
, applier
, context
);
1065 IOLockUnlock(sInstancesLock
);
1069 OSMetaClass::applyToInstancesOfClassName(
1070 const OSSymbol
* name
,
1071 OSMetaClassInstanceApplierFunction applier
,
1075 OSOrderedSet
* set
= 0;
1077 IOLockLock(sAllClassesLock
);
1079 && (meta
= (OSMetaClass
*) sAllClassesDict
->getObject(name
))
1080 && (set
= meta
->reserved
->instances
)) {
1083 IOLockUnlock(sAllClassesLock
);
1089 IOLockLock(sInstancesLock
);
1090 applyToInstances(set
, applier
, context
);
1091 IOLockUnlock(sInstancesLock
);
1095 /*********************************************************************
1096 *********************************************************************/
1098 OSMetaClass::considerUnloads()
1100 OSKext::considerUnloads();
1103 /*********************************************************************
1104 *********************************************************************/
1106 OSMetaClass::removeClasses(OSCollection
* metaClasses
)
1108 OSCollectionIterator
* classIterator
;
1109 OSMetaClass
* checkClass
;
1112 classIterator
= OSCollectionIterator::withCollection(metaClasses
);
1113 if (!classIterator
) {
1117 IOLockLock(sAllClassesLock
);
1121 while ((checkClass
= (OSMetaClass
*)classIterator
->getNextObject())
1122 && !checkClass
->getInstanceCount()
1123 && !checkClass
->reserved
->retain
) {
1128 classIterator
->reset();
1129 while ((checkClass
= (OSMetaClass
*)classIterator
->getNextObject())) {
1130 sAllClassesDict
->removeObject(checkClass
->className
);
1135 IOLockUnlock(sAllClassesLock
);
1136 OSSafeReleaseNULL(classIterator
);
1142 /*********************************************************************
1143 *********************************************************************/
1145 OSMetaClass::getMetaClassWithName(const OSSymbol
* name
)
1147 OSMetaClass
* retMeta
= 0;
1153 IOLockLock(sAllClassesLock
);
1154 if (sAllClassesDict
) {
1155 retMeta
= (OSMetaClass
*) sAllClassesDict
->getObject(name
);
1157 IOLockUnlock(sAllClassesLock
);
1162 /*********************************************************************
1163 *********************************************************************/
1165 OSMetaClass::copyMetaClassWithName(const OSSymbol
* name
)
1167 const OSMetaClass
* meta
;
1174 IOLockLock(sAllClassesLock
);
1175 if (sAllClassesDict
) {
1176 meta
= (OSMetaClass
*) sAllClassesDict
->getObject(name
);
1178 OSIncrementAtomic(&meta
->reserved
->retain
);
1181 IOLockUnlock(sAllClassesLock
);
1186 /*********************************************************************
1187 *********************************************************************/
1189 OSMetaClass::releaseMetaClass() const
1191 OSDecrementAtomic(&reserved
->retain
);
1194 /*********************************************************************
1195 *********************************************************************/
1197 OSMetaClass::allocClassWithName(const OSSymbol
* name
)
1199 const OSMetaClass
* meta
;
1203 meta
= copyMetaClassWithName(name
);
1205 result
= meta
->alloc();
1206 meta
->releaseMetaClass();
1212 /*********************************************************************
1213 *********************************************************************/
1215 OSMetaClass::allocClassWithName(const OSString
* name
)
1217 const OSSymbol
* tmpKey
= OSSymbol::withString(name
);
1218 OSObject
* result
= allocClassWithName(tmpKey
);
1223 /*********************************************************************
1224 *********************************************************************/
1226 OSMetaClass::allocClassWithName(const char * name
)
1228 const OSSymbol
* tmpKey
= OSSymbol::withCStringNoCopy(name
);
1229 OSObject
* result
= allocClassWithName(tmpKey
);
1235 /*********************************************************************
1236 *********************************************************************/
1238 OSMetaClass::checkMetaCastWithName(
1239 const OSSymbol
* name
,
1240 const OSMetaClassBase
* in
)
1242 OSMetaClassBase
* result
= 0;
1244 const OSMetaClass
* const meta
= getMetaClassWithName(name
);
1247 result
= meta
->checkMetaCast(in
);
1253 /*********************************************************************
1254 *********************************************************************/
1257 checkMetaCastWithName(
1258 const OSString
* name
,
1259 const OSMetaClassBase
* in
)
1261 const OSSymbol
* tmpKey
= OSSymbol::withString(name
);
1262 OSMetaClassBase
* result
= checkMetaCastWithName(tmpKey
, in
);
1268 /*********************************************************************
1269 *********************************************************************/
1271 OSMetaClass::checkMetaCastWithName(
1273 const OSMetaClassBase
* in
)
1275 const OSSymbol
* tmpKey
= OSSymbol::withCStringNoCopy(name
);
1276 OSMetaClassBase
* result
= checkMetaCastWithName(tmpKey
, in
);
1282 /*********************************************************************
1283 * OSMetaClass::checkMetaCast()
1284 * Check to see if the 'check' object has this object in its metaclass chain.
1285 * Returns check if it is indeed a kind of the current meta class, 0 otherwise.
1287 * Generally this method is not invoked directly but is used to implement
1288 * the OSMetaClassBase::metaCast member function.
1290 * See also OSMetaClassBase::metaCast
1291 *********************************************************************/
1293 OSMetaClass::checkMetaCast(
1294 const OSMetaClassBase
* check
) const
1296 const OSMetaClass
* const toMeta
= this;
1297 const OSMetaClass
* fromMeta
;
1299 for (fromMeta
= check
->getMetaClass();; fromMeta
= fromMeta
->superClassLink
) {
1300 if (toMeta
== fromMeta
) {
1301 return const_cast<OSMetaClassBase
*>(check
); // Discard const
1303 if (!fromMeta
->superClassLink
) {
1311 /*********************************************************************
1312 *********************************************************************/
1314 OSMetaClass::reservedCalled(int ind
) const
1316 const char * cname
= className
->getCStringNoCopy();
1317 panic("%s::_RESERVED%s%d called.", cname
, cname
, ind
);
1320 /*********************************************************************
1321 *********************************************************************/
1324 OSMetaClass::getSuperClass() const
1326 return superClassLink
;
1329 /*********************************************************************
1330 * xxx - I want to rename this :-/
1331 *********************************************************************/
1333 OSMetaClass::getKmodName() const
1335 OSKext
* myKext
= reserved
? reserved
->kext
: 0;
1337 return myKext
->getIdentifier();
1339 return OSSymbol::withCStringNoCopy("unknown");
1342 /*********************************************************************
1343 *********************************************************************/
1345 OSMetaClass::getInstanceCount() const
1347 return instanceCount
;
1350 /*********************************************************************
1351 *********************************************************************/
1354 OSMetaClass::printInstanceCounts()
1356 OSCollectionIterator
* classes
;
1357 OSSymbol
* className
;
1360 IOLockLock(sAllClassesLock
);
1361 classes
= OSCollectionIterator::withCollection(sAllClassesDict
);
1364 while ((className
= (OSSymbol
*)classes
->getNextObject())) {
1365 meta
= (OSMetaClass
*)sAllClassesDict
->getObject(className
);
1368 printf("%24s count: %03d x 0x%03x = 0x%06x\n",
1369 className
->getCStringNoCopy(),
1370 meta
->getInstanceCount(),
1371 meta
->getClassSize(),
1372 meta
->getInstanceCount() * meta
->getClassSize());
1376 IOLockUnlock(sAllClassesLock
);
1380 /*********************************************************************
1381 *********************************************************************/
1383 OSMetaClass::getClassDictionary()
1385 panic("OSMetaClass::getClassDictionary() is obsoleted.\n");
1389 /*********************************************************************
1390 *********************************************************************/
1392 OSMetaClass::serialize(__unused OSSerialize
* s
) const
1394 panic("OSMetaClass::serialize(): Obsoleted\n");
1398 /*********************************************************************
1399 *********************************************************************/
1402 OSMetaClass::serializeClassDictionary(OSDictionary
* serializeDictionary
)
1404 OSDictionary
* classDict
= NULL
;
1406 IOLockLock(sAllClassesLock
);
1408 classDict
= OSDictionary::withCapacity(sAllClassesDict
->getCount());
1414 OSCollectionIterator
* classes
;
1415 const OSSymbol
* className
;
1417 classes
= OSCollectionIterator::withCollection(sAllClassesDict
);
1422 while ((className
= (const OSSymbol
*)classes
->getNextObject())) {
1423 const OSMetaClass
* meta
;
1426 meta
= (OSMetaClass
*)sAllClassesDict
->getObject(className
);
1427 count
= OSNumber::withNumber(meta
->getInstanceCount(), 32);
1429 classDict
->setObject(className
, count
);
1435 serializeDictionary
->setObject("Classes", classDict
);
1439 OSSafeReleaseNULL(classDict
);
1441 IOLockUnlock(sAllClassesLock
);
1447 /*********************************************************************
1448 *********************************************************************/
1453 OSMetaClass::trackedNew(size_t size
)
1457 mem
= (typeof(mem
))kalloc_tag_bt(size
+ sizeof(IOTracking
), VM_KERN_MEMORY_LIBKERN
);
1463 memset(mem
, 0, size
+ sizeof(IOTracking
));
1466 OSIVAR_ACCUMSIZE(size
);
1472 OSMetaClass::trackedDelete(void * instance
, size_t size
)
1474 IOTracking
* mem
= (typeof(mem
))instance
; mem
--;
1476 kfree(mem
, size
+ sizeof(IOTracking
));
1477 OSIVAR_ACCUMSIZE(-size
);
1481 OSMetaClass::trackedInstance(OSObject
* instance
) const
1483 IOTracking
* mem
= (typeof(mem
))instance
; mem
--;
1485 return IOTrackingAdd(reserved
->tracking
, mem
, classSize
, false, VM_KERN_MEMORY_NONE
);
1489 OSMetaClass::trackedFree(OSObject
* instance
) const
1491 IOTracking
* mem
= (typeof(mem
))instance
; mem
--;
1493 return IOTrackingRemove(reserved
->tracking
, mem
, classSize
);
1497 OSMetaClass::trackedAccumSize(OSObject
* instance
, size_t size
) const
1499 IOTracking
* mem
= (typeof(mem
))instance
; mem
--;
1501 return IOTrackingAccumSize(reserved
->tracking
, mem
, size
);
1505 OSMetaClass::getTracking() const
1507 return reserved
->tracking
;
1510 #endif /* IOTRACKING */