2 * Copyright (c) 2000,2008-2009 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@
29 * Copyright (c) 1997 Apple Inc.
32 #include <libkern/c++/OSMetaClass.h>
33 #include <libkern/c++/OSKext.h>
34 #include <libkern/c++/OSLib.h>
35 #include <libkern/c++/OSSymbol.h>
36 #include <IOKit/IOKitDebug.h>
38 #include <sys/cdefs.h>
43 #include <mach/mach_types.h>
44 #include <libkern/kernel_mach_header.h>
45 #include <libkern/prelink.h>
49 #pragma mark Constants &c.
50 #endif /* PRAGMA_MARK */
51 OSKextLogSpec kOSRuntimeLogSpec
=
52 kOSKextLogErrorLevel
|
54 kOSKextLogKextBookkeepingFlag
;
57 #pragma mark Logging Bootstrap
58 #endif /* PRAGMA_MARK */
59 /*********************************************************************
60 * kern_os Logging Bootstrap
62 * We can't call in to OSKext until the kernel's C++ environment is up
63 * and running, so let's mask those references with a check variable.
64 * We print unconditionally if C++ isn't up, but if that's the case
65 * we've generally hit a serious error in kernel init!
66 *********************************************************************/
67 static bool gKernelCPPInitialized
= false;
69 #define OSRuntimeLog(kext, flags, format, args...) \
71 if (gKernelCPPInitialized) { \
72 OSKextLog((kext), (flags), (format), ## args); \
74 printf((format), ## args); \
79 #pragma mark kern_os Allocator Package
80 #endif /* PRAGMA_MARK */
81 /*********************************************************************
82 * kern_os Allocator Package
83 *********************************************************************/
85 /*********************************************************************
86 *********************************************************************/
88 extern int debug_iomalloc_size
;
91 /*********************************************************************
92 *********************************************************************/
94 kern_os_malloc(size_t size
)
101 mem
= kallocp_tag_bt((vm_size_t
*)&size
, VM_KERN_MEMORY_LIBKERN
);
107 OSAddAtomic(size
, &debug_iomalloc_size
);
115 /*********************************************************************
116 *********************************************************************/
118 kern_os_free(void * addr
)
121 size
= kalloc_size(addr
);
123 OSAddAtomic(-size
, &debug_iomalloc_size
);
129 /*********************************************************************
130 *********************************************************************/
140 return kern_os_malloc(nsize
);
143 osize
= kalloc_size(addr
);
144 if (nsize
== osize
) {
153 nmem
= kallocp_tag_bt((vm_size_t
*)&nsize
, VM_KERN_MEMORY_LIBKERN
);
160 OSAddAtomic((nsize
- osize
), &debug_iomalloc_size
);
164 (void)memset((char *)nmem
+ osize
, 0, nsize
- osize
);
166 (void)memcpy(nmem
, addr
, (nsize
> osize
) ? osize
: nsize
);
173 #pragma mark Libkern Init
174 #endif /* PRAGMA_MARK */
175 /*********************************************************************
177 *********************************************************************/
181 __cxa_pure_virtual( void )
183 panic("%s", __FUNCTION__
);
187 __pure_virtual( void )
189 panic("%s", __FUNCTION__
);
193 extern lck_grp_t
* IOLockGroup
;
194 extern kmod_info_t g_kernel_kmod_info
;
197 kOSSectionNamesDefault
= 0,
198 kOSSectionNamesBuiltinKext
= 1,
199 kOSSectionNamesCount
= 2,
202 kOSSectionNameInitializer
= 0,
203 kOSSectionNameFinalizer
= 1,
204 kOSSectionNameCount
= 2
208 gOSStructorSectionNames
[kOSSectionNamesCount
][kOSSectionNameCount
] = {
209 { SECT_MODINITFUNC
, SECT_MODTERMFUNC
},
210 { kBuiltinInitSection
, kBuiltinTermSection
}
216 // This must be called before calling OSRuntimeInitializeCPP.
217 OSMetaClassBase::initialize();
219 g_kernel_kmod_info
.address
= (vm_address_t
) &_mh_execute_header
;
220 if (kOSReturnSuccess
!= OSRuntimeInitializeCPP(NULL
)) {
221 // &g_kernel_kmod_info, gOSSectionNamesStandard, 0, 0)) {
222 panic("OSRuntime: C++ runtime failed to initialize.");
225 gKernelCPPInitialized
= true;
233 #pragma mark C++ Runtime Load/Unload
234 #endif /* PRAGMA_MARK */
235 /*********************************************************************
236 * kern_os C++ Runtime Load/Unload
237 *********************************************************************/
240 typedef void (*structor_t
)(void);
243 OSRuntimeCallStructorsInSection(
245 kmod_info_t
* kmodInfo
,
247 kernel_segment_command_t
* segment
,
248 const char * sectionName
,
252 kernel_section_t
* section
;
255 for (section
= firstsect(segment
);
257 section
= nextsect(segment
, section
)) {
258 if (strncmp(section
->sectname
, sectionName
, sizeof(section
->sectname
) - 1)) {
262 structor_t
* structors
= (structor_t
*)section
->addr
;
268 unsigned int num_structors
= section
->size
/ sizeof(structor_t
);
269 unsigned int hit_null_structor
= 0;
270 unsigned int firstIndex
= 0;
273 // bsearch for any in range
274 unsigned int baseIdx
;
277 firstIndex
= num_structors
;
278 for (lim
= num_structors
, baseIdx
= 0; lim
; lim
>>= 1) {
279 value
= (uintptr_t) structors
[baseIdx
+ (lim
>> 1)];
281 panic("%s: null structor", kmodInfo
->name
);
283 if ((value
>= textStart
) && (value
< textEnd
)) {
284 firstIndex
= (baseIdx
+ (lim
>> 1));
285 // scan back for the first in range
286 for (; firstIndex
; firstIndex
--) {
287 value
= (uintptr_t) structors
[firstIndex
- 1];
288 if ((value
< textStart
) || (value
>= textEnd
)) {
294 if (textStart
> value
) {
296 baseIdx
+= (lim
>> 1) + 1;
301 baseIdx
= (baseIdx
+ (lim
>> 1));
304 (firstIndex
< num_structors
)
305 && (!metaHandle
|| OSMetaClass::checkModLoad(metaHandle
));
307 if ((structor
= structors
[firstIndex
])) {
308 if ((textStart
&& ((uintptr_t) structor
< textStart
))
309 || (textEnd
&& ((uintptr_t) structor
>= textEnd
))) {
314 } else if (!hit_null_structor
) {
315 hit_null_structor
= 1;
316 OSRuntimeLog(theKext
, kOSRuntimeLogSpec
,
317 "Null structor in kext %s segment %s!",
318 kmodInfo
->name
, section
->segname
);
322 result
= OSMetaClass::checkModLoad(metaHandle
);
325 } /* for (section...) */
329 /*********************************************************************
330 *********************************************************************/
332 OSRuntimeFinalizeCPP(
335 kern_return_t result
= KMOD_RETURN_FAILURE
;
336 void * metaHandle
= NULL
;// do not free
337 kernel_mach_header_t
* header
;
338 kernel_segment_command_t
* segment
;
339 kmod_info_t
* kmodInfo
;
340 const char ** sectionNames
;
346 sectionNames
= gOSStructorSectionNames
[kOSSectionNamesDefault
];
348 if (!theKext
->isCPPInitialized()) {
349 result
= KMOD_RETURN_SUCCESS
;
352 kmodInfo
= theKext
->kmod_info
;
353 if (!kmodInfo
|| !kmodInfo
->address
) {
354 result
= kOSKextReturnInvalidArgument
;
357 header
= (kernel_mach_header_t
*)kmodInfo
->address
;
358 if (theKext
->flags
.builtin
) {
359 header
= (kernel_mach_header_t
*)g_kernel_kmod_info
.address
;
360 textStart
= kmodInfo
->address
;
361 textEnd
= textStart
+ kmodInfo
->size
;
362 sectionNames
= gOSStructorSectionNames
[kOSSectionNamesBuiltinKext
];
365 kmodInfo
= &g_kernel_kmod_info
;
366 header
= (kernel_mach_header_t
*)kmodInfo
->address
;
369 /* OSKext checks for this condition now, but somebody might call
370 * this function directly (the symbol is exported....).
372 if (OSMetaClass::modHasInstance(kmodInfo
->name
)) {
373 // xxx - Don't log under errors? this is more of an info thing
374 OSRuntimeLog(theKext
, kOSRuntimeLogSpec
,
375 "Can't tear down kext %s C++; classes have instances:",
377 OSKext::reportOSMetaClassInstances(kmodInfo
->name
, kOSRuntimeLogSpec
);
378 result
= kOSMetaClassHasInstances
;
382 /* Tell the meta class system that we are starting to unload.
383 * metaHandle isn't actually needed on the finalize path,
384 * so we don't check it here, even though OSMetaClass::postModLoad() will
385 * return a failure (it only does actual work on the init path anyhow).
387 metaHandle
= OSMetaClass::preModLoad(kmodInfo
->name
);
389 OSSymbol::checkForPageUnload((void *)kmodInfo
->address
,
390 (void *)(kmodInfo
->address
+ kmodInfo
->size
));
392 header
= (kernel_mach_header_t
*)kmodInfo
->address
;
393 segment
= firstsegfromheader(header
);
395 for (segment
= firstsegfromheader(header
);
397 segment
= nextsegfromheader(header
, segment
)) {
398 OSRuntimeCallStructorsInSection(theKext
, kmodInfo
, NULL
, segment
,
399 sectionNames
[kOSSectionNameFinalizer
], textStart
, textEnd
);
402 (void)OSMetaClass::postModLoad(metaHandle
);
405 theKext
->setCPPInitialized(false);
407 result
= KMOD_RETURN_SUCCESS
;
412 /*********************************************************************
413 *********************************************************************/
415 OSRuntimeInitializeCPP(
418 kern_return_t result
= KMOD_RETURN_FAILURE
;
419 kernel_mach_header_t
* header
= NULL
;
420 void * metaHandle
= NULL
;// do not free
421 bool load_success
= true;
422 kernel_segment_command_t
* segment
= NULL
;// do not free
423 kernel_segment_command_t
* failure_segment
= NULL
; // do not free
424 kmod_info_t
* kmodInfo
;
425 const char ** sectionNames
;
431 sectionNames
= gOSStructorSectionNames
[kOSSectionNamesDefault
];
433 if (theKext
->isCPPInitialized()) {
434 result
= KMOD_RETURN_SUCCESS
;
438 kmodInfo
= theKext
->kmod_info
;
439 if (!kmodInfo
|| !kmodInfo
->address
) {
440 result
= kOSKextReturnInvalidArgument
;
443 header
= (kernel_mach_header_t
*)kmodInfo
->address
;
445 if (theKext
->flags
.builtin
) {
446 header
= (kernel_mach_header_t
*)g_kernel_kmod_info
.address
;
447 textStart
= kmodInfo
->address
;
448 textEnd
= textStart
+ kmodInfo
->size
;
449 sectionNames
= gOSStructorSectionNames
[kOSSectionNamesBuiltinKext
];
452 kmodInfo
= &g_kernel_kmod_info
;
453 header
= (kernel_mach_header_t
*)kmodInfo
->address
;
456 /* Tell the meta class system that we are starting the load
458 metaHandle
= OSMetaClass::preModLoad(kmodInfo
->name
);
464 /* NO GOTO PAST HERE. */
466 /* Scan the header for all constructor sections, in any
467 * segment, and invoke the constructors within those sections.
469 for (segment
= firstsegfromheader(header
);
470 segment
!= NULL
&& load_success
;
471 segment
= nextsegfromheader(header
, segment
)) {
472 /* Record the current segment in the event of a failure.
474 failure_segment
= segment
;
475 load_success
= OSRuntimeCallStructorsInSection(
476 theKext
, kmodInfo
, metaHandle
, segment
,
477 sectionNames
[kOSSectionNameInitializer
],
479 } /* for (segment...) */
481 /* We failed so call all of the destructors. We must do this before
482 * calling OSMetaClass::postModLoad() as the OSMetaClass destructors
483 * will alter state (in the metaHandle) used by that function.
486 /* Scan the header for all destructor sections, in any
487 * segment, and invoke the constructors within those sections.
489 for (segment
= firstsegfromheader(header
);
490 segment
!= failure_segment
&& segment
!= 0;
491 segment
= nextsegfromheader(header
, segment
)) {
492 OSRuntimeCallStructorsInSection(theKext
, kmodInfo
, NULL
, segment
,
493 sectionNames
[kOSSectionNameFinalizer
], textStart
, textEnd
);
494 } /* for (segment...) */
497 /* Now, regardless of success so far, do the post-init registration
498 * and cleanup. If we had to call the unloadCPP function, static
499 * destructors have removed classes from the stalled list so no
500 * metaclasses will actually be registered.
502 result
= OSMetaClass::postModLoad(metaHandle
);
504 /* If we've otherwise been fine up to now, but OSMetaClass::postModLoad()
505 * fails (typically due to a duplicate class), tear down all the C++
506 * stuff from the kext. This isn't necessary for libkern/OSMetaClass stuff,
507 * but may be necessary for other C++ code. We ignore the return value
508 * because it's only a fail when there are existing instances of libkern
509 * classes, and there had better not be any created on the C++ init path.
511 if (load_success
&& result
!= KMOD_RETURN_SUCCESS
) {
512 (void)OSRuntimeFinalizeCPP(theKext
); //kmodInfo, sectionNames, textStart, textEnd);
515 if (theKext
&& load_success
&& result
== KMOD_RETURN_SUCCESS
) {
516 theKext
->setCPPInitialized(true);
522 /*********************************************************************
523 * Unload a kernel segment.
524 *********************************************************************/
527 OSRuntimeUnloadCPPForSegment(
528 kernel_segment_command_t
* segment
)
530 OSRuntimeCallStructorsInSection(NULL
, &g_kernel_kmod_info
, NULL
, segment
,
531 gOSStructorSectionNames
[kOSSectionNamesDefault
][kOSSectionNameFinalizer
], 0, 0);
535 #pragma mark C++ Allocators & Deallocators
536 #endif /* PRAGMA_MARK */
537 /*********************************************************************
538 * C++ Allocators & Deallocators
539 *********************************************************************/
541 operator new(size_t size
)
545 result
= (void *) kern_os_malloc(size
);
550 operator delete(void * addr
)
551 #if __cplusplus >= 201103L
560 operator new[](unsigned long sz
)
565 return kern_os_malloc(sz
);
569 operator delete[](void * ptr
)
570 #if __cplusplus >= 201103L
580 /* PR-6481964 - The compiler is going to check for size overflows in calls to
581 * new[], and if there is an overflow, it will call __throw_length_error.
582 * This is an unrecoverable error by the C++ standard, so we must panic here.
584 * We have to put the function inside the std namespace because of how the
585 * compiler expects the name to be mangled.
589 __throw_length_error(const char *msg __unused
)
591 panic("Size of array created by new[] has overflowed");