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 *********************************************************************/
180 void __cxa_pure_virtual( void ) { panic("%s", __FUNCTION__
); }
182 void __pure_virtual( void ) { panic("%s", __FUNCTION__
); }
185 extern lck_grp_t
* IOLockGroup
;
186 extern kmod_info_t g_kernel_kmod_info
;
189 kOSSectionNamesDefault
= 0,
190 kOSSectionNamesBuiltinKext
= 1,
191 kOSSectionNamesCount
= 2,
194 kOSSectionNameInitializer
= 0,
195 kOSSectionNameFinalizer
= 1,
196 kOSSectionNameCount
= 2
200 gOSStructorSectionNames
[kOSSectionNamesCount
][kOSSectionNameCount
] = {
201 { SECT_MODINITFUNC
, SECT_MODTERMFUNC
},
202 { kBuiltinInitSection
, kBuiltinTermSection
}
205 void OSlibkernInit(void)
207 // This must be called before calling OSRuntimeInitializeCPP.
208 OSMetaClassBase::initialize();
210 g_kernel_kmod_info
.address
= (vm_address_t
) &_mh_execute_header
;
211 if (kOSReturnSuccess
!= OSRuntimeInitializeCPP(NULL
)) {
212 // &g_kernel_kmod_info, gOSSectionNamesStandard, 0, 0)) {
213 panic("OSRuntime: C++ runtime failed to initialize.");
216 gKernelCPPInitialized
= true;
224 #pragma mark C++ Runtime Load/Unload
225 #endif /* PRAGMA_MARK */
226 /*********************************************************************
227 * kern_os C++ Runtime Load/Unload
228 *********************************************************************/
231 typedef void (*structor_t
)(void);
234 OSRuntimeCallStructorsInSection(
236 kmod_info_t
* kmodInfo
,
238 kernel_segment_command_t
* segment
,
239 const char * sectionName
,
243 kernel_section_t
* section
;
246 for (section
= firstsect(segment
);
248 section
= nextsect(segment
, section
))
250 if (strncmp(section
->sectname
, sectionName
, sizeof(section
->sectname
) - 1)) continue;
252 structor_t
* structors
= (structor_t
*)section
->addr
;
253 if (!structors
) continue;
256 unsigned int num_structors
= section
->size
/ sizeof(structor_t
);
257 unsigned int hit_null_structor
= 0;
258 unsigned int firstIndex
= 0;
262 // bsearch for any in range
263 unsigned int baseIdx
;
266 firstIndex
= num_structors
;
267 for (lim
= num_structors
, baseIdx
= 0; lim
; lim
>>= 1)
269 value
= (uintptr_t) structors
[baseIdx
+ (lim
>> 1)];
270 if (!value
) panic("%s: null structor", kmodInfo
->name
);
271 if ((value
>= textStart
) && (value
< textEnd
))
273 firstIndex
= (baseIdx
+ (lim
>> 1));
274 // scan back for the first in range
275 for (; firstIndex
; firstIndex
--)
277 value
= (uintptr_t) structors
[firstIndex
- 1];
278 if ((value
< textStart
) || (value
>= textEnd
)) break;
282 if (textStart
> value
)
285 baseIdx
+= (lim
>> 1) + 1;
290 baseIdx
= (baseIdx
+ (lim
>> 1));
293 (firstIndex
< num_structors
)
294 && (!metaHandle
|| OSMetaClass::checkModLoad(metaHandle
));
297 if ((structor
= structors
[firstIndex
]))
299 if ((textStart
&& ((uintptr_t) structor
< textStart
))
300 || (textEnd
&& ((uintptr_t) structor
>= textEnd
))) break;
304 else if (!hit_null_structor
)
306 hit_null_structor
= 1;
307 OSRuntimeLog(theKext
, kOSRuntimeLogSpec
,
308 "Null structor in kext %s segment %s!",
309 kmodInfo
->name
, section
->segname
);
312 if (metaHandle
) result
= OSMetaClass::checkModLoad(metaHandle
);
314 } /* for (section...) */
318 /*********************************************************************
319 *********************************************************************/
321 OSRuntimeFinalizeCPP(
324 kern_return_t result
= KMOD_RETURN_FAILURE
;
325 void * metaHandle
= NULL
; // do not free
326 kernel_mach_header_t
* header
;
327 kernel_segment_command_t
* segment
;
328 kmod_info_t
* kmodInfo
;
329 const char ** sectionNames
;
335 sectionNames
= gOSStructorSectionNames
[kOSSectionNamesDefault
];
337 if (!theKext
->isCPPInitialized()) {
338 result
= KMOD_RETURN_SUCCESS
;
341 kmodInfo
= theKext
->kmod_info
;
342 if (!kmodInfo
|| !kmodInfo
->address
) {
343 result
= kOSKextReturnInvalidArgument
;
346 header
= (kernel_mach_header_t
*)kmodInfo
->address
;
347 if (theKext
->flags
.builtin
) {
348 header
= (kernel_mach_header_t
*)g_kernel_kmod_info
.address
;
349 textStart
= kmodInfo
->address
;
350 textEnd
= textStart
+ kmodInfo
->size
;
351 sectionNames
= gOSStructorSectionNames
[kOSSectionNamesBuiltinKext
];
354 kmodInfo
= &g_kernel_kmod_info
;
355 header
= (kernel_mach_header_t
*)kmodInfo
->address
;
358 /* OSKext checks for this condition now, but somebody might call
359 * this function directly (the symbol is exported....).
361 if (OSMetaClass::modHasInstance(kmodInfo
->name
)) {
362 // xxx - Don't log under errors? this is more of an info thing
363 OSRuntimeLog(theKext
, kOSRuntimeLogSpec
,
364 "Can't tear down kext %s C++; classes have instances:",
366 OSKext::reportOSMetaClassInstances(kmodInfo
->name
, kOSRuntimeLogSpec
);
367 result
= kOSMetaClassHasInstances
;
371 /* Tell the meta class system that we are starting to unload.
372 * metaHandle isn't actually needed on the finalize path,
373 * so we don't check it here, even though OSMetaClass::postModLoad() will
374 * return a failure (it only does actual work on the init path anyhow).
376 metaHandle
= OSMetaClass::preModLoad(kmodInfo
->name
);
378 OSSymbol::checkForPageUnload((void *)kmodInfo
->address
,
379 (void *)(kmodInfo
->address
+ kmodInfo
->size
));
381 header
= (kernel_mach_header_t
*)kmodInfo
->address
;
382 segment
= firstsegfromheader(header
);
384 for (segment
= firstsegfromheader(header
);
386 segment
= nextsegfromheader(header
, segment
)) {
388 OSRuntimeCallStructorsInSection(theKext
, kmodInfo
, NULL
, segment
,
389 sectionNames
[kOSSectionNameFinalizer
], textStart
, textEnd
);
392 (void)OSMetaClass::postModLoad(metaHandle
);
395 theKext
->setCPPInitialized(false);
397 result
= KMOD_RETURN_SUCCESS
;
402 /*********************************************************************
403 *********************************************************************/
405 OSRuntimeInitializeCPP(
408 kern_return_t result
= KMOD_RETURN_FAILURE
;
409 kernel_mach_header_t
* header
= NULL
;
410 void * metaHandle
= NULL
; // do not free
411 bool load_success
= true;
412 kernel_segment_command_t
* segment
= NULL
; // do not free
413 kernel_segment_command_t
* failure_segment
= NULL
; // do not free
414 kmod_info_t
* kmodInfo
;
415 const char ** sectionNames
;
421 sectionNames
= gOSStructorSectionNames
[kOSSectionNamesDefault
];
423 if (theKext
->isCPPInitialized()) {
424 result
= KMOD_RETURN_SUCCESS
;
428 kmodInfo
= theKext
->kmod_info
;
429 if (!kmodInfo
|| !kmodInfo
->address
) {
430 result
= kOSKextReturnInvalidArgument
;
433 header
= (kernel_mach_header_t
*)kmodInfo
->address
;
435 if (theKext
->flags
.builtin
) {
436 header
= (kernel_mach_header_t
*)g_kernel_kmod_info
.address
;
437 textStart
= kmodInfo
->address
;
438 textEnd
= textStart
+ kmodInfo
->size
;
439 sectionNames
= gOSStructorSectionNames
[kOSSectionNamesBuiltinKext
];
442 kmodInfo
= &g_kernel_kmod_info
;
443 header
= (kernel_mach_header_t
*)kmodInfo
->address
;
446 /* Tell the meta class system that we are starting the load
448 metaHandle
= OSMetaClass::preModLoad(kmodInfo
->name
);
454 /* NO GOTO PAST HERE. */
456 /* Scan the header for all constructor sections, in any
457 * segment, and invoke the constructors within those sections.
459 for (segment
= firstsegfromheader(header
);
460 segment
!= NULL
&& load_success
;
461 segment
= nextsegfromheader(header
, segment
))
463 /* Record the current segment in the event of a failure.
465 failure_segment
= segment
;
466 load_success
= OSRuntimeCallStructorsInSection(
467 theKext
, kmodInfo
, metaHandle
, segment
,
468 sectionNames
[kOSSectionNameInitializer
],
470 } /* for (segment...) */
472 /* We failed so call all of the destructors. We must do this before
473 * calling OSMetaClass::postModLoad() as the OSMetaClass destructors
474 * will alter state (in the metaHandle) used by that function.
478 /* Scan the header for all destructor sections, in any
479 * segment, and invoke the constructors within those sections.
481 for (segment
= firstsegfromheader(header
);
482 segment
!= failure_segment
&& segment
!= 0;
483 segment
= nextsegfromheader(header
, segment
)) {
485 OSRuntimeCallStructorsInSection(theKext
, kmodInfo
, NULL
, segment
,
486 sectionNames
[kOSSectionNameFinalizer
], textStart
, textEnd
);
488 } /* for (segment...) */
491 /* Now, regardless of success so far, do the post-init registration
492 * and cleanup. If we had to call the unloadCPP function, static
493 * destructors have removed classes from the stalled list so no
494 * metaclasses will actually be registered.
496 result
= OSMetaClass::postModLoad(metaHandle
);
498 /* If we've otherwise been fine up to now, but OSMetaClass::postModLoad()
499 * fails (typically due to a duplicate class), tear down all the C++
500 * stuff from the kext. This isn't necessary for libkern/OSMetaClass stuff,
501 * but may be necessary for other C++ code. We ignore the return value
502 * because it's only a fail when there are existing instances of libkern
503 * classes, and there had better not be any created on the C++ init path.
505 if (load_success
&& result
!= KMOD_RETURN_SUCCESS
) {
506 (void)OSRuntimeFinalizeCPP(theKext
); //kmodInfo, sectionNames, textStart, textEnd);
509 if (theKext
&& load_success
&& result
== KMOD_RETURN_SUCCESS
) {
510 theKext
->setCPPInitialized(true);
516 /*********************************************************************
517 Unload a kernel segment.
518 *********************************************************************/
521 OSRuntimeUnloadCPPForSegment(
522 kernel_segment_command_t
* segment
)
524 OSRuntimeCallStructorsInSection(NULL
, &g_kernel_kmod_info
, NULL
, segment
,
525 gOSStructorSectionNames
[kOSSectionNamesDefault
][kOSSectionNameFinalizer
], 0, 0);
529 #pragma mark C++ Allocators & Deallocators
530 #endif /* PRAGMA_MARK */
531 /*********************************************************************
532 * C++ Allocators & Deallocators
533 *********************************************************************/
535 operator new(size_t size
)
539 result
= (void *) kern_os_malloc(size
);
544 operator delete(void * addr
)
545 #if __cplusplus >= 201103L
554 operator new[](unsigned long sz
)
557 return kern_os_malloc(sz
);
561 operator delete[](void * ptr
)
562 #if __cplusplus >= 201103L
572 /* PR-6481964 - The compiler is going to check for size overflows in calls to
573 * new[], and if there is an overflow, it will call __throw_length_error.
574 * This is an unrecoverable error by the C++ standard, so we must panic here.
576 * We have to put the function inside the std namespace because of how the
577 * compiler expects the name to be mangled.
582 __throw_length_error(const char *msg __unused
)
584 panic("Size of array created by new[] has overflowed");