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>
37 #include <sys/cdefs.h>
42 #include <mach/mach_types.h>
43 #include <libkern/kernel_mach_header.h>
47 #pragma mark Constants &c.
48 #endif /* PRAGMA_MARK */
49 OSKextLogSpec kOSRuntimeLogSpec
=
50 kOSKextLogErrorLevel
|
52 kOSKextLogKextBookkeepingFlag
;
55 #pragma mark Logging Bootstrap
56 #endif /* PRAGMA_MARK */
57 /*********************************************************************
58 * kern_os Logging Bootstrap
60 * We can't call in to OSKext until the kernel's C++ environment is up
61 * and running, so let's mask those references with a check variable.
62 * We print unconditionally if C++ isn't up, but if that's the case
63 * we've generally hit a serious error in kernel init!
64 *********************************************************************/
65 static bool gKernelCPPInitialized
= false;
67 #define OSRuntimeLog(kext, flags, format, args...) \
69 if (gKernelCPPInitialized) { \
70 OSKextLog((kext), (flags), (format), ## args); \
72 printf((format), ## args); \
78 #pragma mark kern_os Allocator Package
79 #endif /* PRAGMA_MARK */
80 /*********************************************************************
81 * kern_os Allocator Package
82 *********************************************************************/
84 /*********************************************************************
85 *********************************************************************/
87 extern int debug_iomalloc_size
;
95 /*********************************************************************
96 *********************************************************************/
98 kern_os_malloc(size_t size
)
101 size_t memsize
= sizeof (*mem
) + size
;
107 mem
= (struct _mhead
*)kalloc(memsize
);
113 debug_iomalloc_size
+= memsize
;
117 bzero(mem
->dat
, size
);
122 /*********************************************************************
123 *********************************************************************/
125 kern_os_free(void * addr
)
133 hdr
= (struct _mhead
*)addr
;
137 debug_iomalloc_size
-= hdr
->mlen
;
141 memset((vm_offset_t
)hdr
, 0xbb, hdr
->mlen
);
143 kfree(hdr
, hdr
->mlen
);
147 /*********************************************************************
148 *********************************************************************/
154 struct _mhead
* ohdr
;
155 struct _mhead
* nmem
;
156 size_t nmemsize
, osize
;
159 return (kern_os_malloc(nsize
));
162 ohdr
= (struct _mhead
*)addr
;
164 osize
= ohdr
->mlen
- sizeof(*ohdr
);
165 if (nsize
== osize
) {
174 nmemsize
= sizeof (*nmem
) + nsize
;
175 nmem
= (struct _mhead
*) kalloc(nmemsize
);
182 debug_iomalloc_size
+= (nmemsize
- ohdr
->mlen
);
185 nmem
->mlen
= nmemsize
;
187 (void) memset(&nmem
->dat
[osize
], 0, nsize
- osize
);
189 (void)memcpy(nmem
->dat
, ohdr
->dat
, (nsize
> osize
) ? osize
: nsize
);
190 kfree(ohdr
, ohdr
->mlen
);
195 /*********************************************************************
196 *********************************************************************/
198 kern_os_malloc_size(void * addr
)
206 hdr
= (struct _mhead
*) addr
; hdr
--;
207 return hdr
->mlen
- sizeof (struct _mhead
);
211 #pragma mark C++ Runtime Load/Unload
212 #endif /* PRAGMA_MARK */
213 /*********************************************************************
214 * kern_os C++ Runtime Load/Unload
215 *********************************************************************/
217 /*********************************************************************
218 *********************************************************************/
220 void __cxa_pure_virtual( void ) { panic("%s", __FUNCTION__
); }
222 void __pure_virtual( void ) { panic("%s", __FUNCTION__
); }
225 typedef void (*structor_t
)(void);
227 /*********************************************************************
228 * OSRuntimeUnloadCPPForSegment()
230 * Given a pointer to a mach object segment, iterate the segment to
231 * obtain a destructor section for C++ objects, and call each of the
233 *********************************************************************/
236 OSRuntimeUnloadCPPForSegmentInKmod(
237 kernel_segment_command_t
* segment
,
238 kmod_info_t
* kmodInfo
)
241 kernel_section_t
* section
= NULL
; // do not free
242 OSKext
* theKext
= NULL
; // must release
244 if (gKernelCPPInitialized
&& kmodInfo
) {
245 theKext
= OSKext::lookupKextWithIdentifier(kmodInfo
->name
);
248 for (section
= firstsect(segment
);
250 section
= nextsect(segment
, section
)) {
252 if (strncmp(section
->sectname
, SECT_DESTRUCTOR
,
253 sizeof(SECT_DESTRUCTOR
)) == 0) {
255 structor_t
* destructors
= (structor_t
*)section
->addr
;
258 int num_destructors
= section
->size
/ sizeof(structor_t
);
259 int hit_null_destructor
= 0;
261 for (int i
= 0; i
< num_destructors
; i
++) {
262 if (destructors
[i
]) {
264 } else if (!hit_null_destructor
) {
265 hit_null_destructor
= 1;
266 OSRuntimeLog(theKext
, kOSRuntimeLogSpec
,
267 "Null destructor in kext %s segment %s!",
268 kmodInfo
? kmodInfo
->name
: "(unknown)",
272 } /* if (destructors) */
273 } /* if (strncmp...) */
274 } /* for (section...) */
276 OSSafeRelease(theKext
);
281 OSRuntimeUnloadCPPForSegment(kernel_segment_command_t
* segment
) {
282 OSRuntimeUnloadCPPForSegmentInKmod(segment
, NULL
);
285 /*********************************************************************
286 *********************************************************************/
289 kmod_info_t
* kmodInfo
,
290 void * data __unused
)
292 if (kmodInfo
&& kmodInfo
->address
) {
294 kernel_segment_command_t
* segment
;
295 kernel_mach_header_t
* header
;
297 OSSymbol::checkForPageUnload((void *)kmodInfo
->address
,
298 (void *)(kmodInfo
->address
+ kmodInfo
->size
));
300 header
= (kernel_mach_header_t
*)kmodInfo
->address
;
301 segment
= firstsegfromheader(header
);
303 for (segment
= firstsegfromheader(header
);
305 segment
= nextsegfromheader(header
, segment
)) {
307 OSRuntimeUnloadCPPForSegmentInKmod(segment
, kmodInfo
);
314 /*********************************************************************
315 *********************************************************************/
317 OSRuntimeFinalizeCPP(
318 kmod_info_t
* kmodInfo
,
319 void * data __unused
)
321 kern_return_t result
= KMOD_RETURN_FAILURE
;
322 void * metaHandle
= NULL
; // do not free
323 OSKext
* theKext
= NULL
; // must release
325 if (gKernelCPPInitialized
) {
326 theKext
= OSKext::lookupKextWithIdentifier(kmodInfo
->name
);
329 if (theKext
&& !theKext
->isCPPInitialized()) {
330 result
= KMOD_RETURN_SUCCESS
;
334 /* OSKext checks for this condition now, but somebody might call
335 * this function directly (the symbol is exported....).
337 if (OSMetaClass::modHasInstance(kmodInfo
->name
)) {
338 // xxx - Don't log under errors? this is more of an info thing
339 OSRuntimeLog(theKext
, kOSRuntimeLogSpec
,
340 "Can't tear down kext %s C++; classes have instances:",
342 OSKext::reportOSMetaClassInstances(kmodInfo
->name
, kOSRuntimeLogSpec
);
343 result
= kOSMetaClassHasInstances
;
347 /* Tell the meta class system that we are starting to unload.
348 * metaHandle isn't actually needed on the finalize path,
349 * so we don't check it here, even though OSMetaClass::postModLoad() will
350 * return a failure (it only does actual work on the init path anyhow).
352 metaHandle
= OSMetaClass::preModLoad(kmodInfo
->name
);
353 OSRuntimeUnloadCPP(kmodInfo
, 0);
354 (void)OSMetaClass::postModLoad(metaHandle
);
357 theKext
->setCPPInitialized(false);
359 result
= KMOD_RETURN_SUCCESS
;
361 OSSafeRelease(theKext
);
365 // Functions used by the extenTools/kmod library project
367 /*********************************************************************
368 *********************************************************************/
370 OSRuntimeInitializeCPP(
371 kmod_info_t
* kmodInfo
,
372 void * data __unused
)
374 kern_return_t result
= KMOD_RETURN_FAILURE
;
375 OSKext
* theKext
= NULL
; // must release
376 kernel_mach_header_t
* header
= NULL
;
377 void * metaHandle
= NULL
; // do not free
378 bool load_success
= true;
379 kernel_segment_command_t
* segment
= NULL
; // do not free
380 kernel_segment_command_t
* failure_segment
= NULL
; // do not free
382 if (!kmodInfo
|| !kmodInfo
->address
|| !kmodInfo
->name
) {
383 result
= kOSKextReturnInvalidArgument
;
387 if (gKernelCPPInitialized
) {
388 theKext
= OSKext::lookupKextWithIdentifier(kmodInfo
->name
);
391 if (theKext
&& theKext
->isCPPInitialized()) {
392 result
= KMOD_RETURN_SUCCESS
;
396 header
= (kernel_mach_header_t
*)kmodInfo
->address
;
398 /* Tell the meta class system that we are starting the load
400 metaHandle
= OSMetaClass::preModLoad(kmodInfo
->name
);
406 /* NO GOTO PAST HERE. */
408 /* Scan the header for all constructor sections, in any
409 * segment, and invoke the constructors within those sections.
411 for (segment
= firstsegfromheader(header
);
412 segment
!= NULL
&& load_success
;
413 segment
= nextsegfromheader(header
, segment
)) {
415 kernel_section_t
* section
;
417 /* Record the current segment in the event of a failure.
419 failure_segment
= segment
;
421 for (section
= firstsect(segment
);
423 section
= nextsect(segment
, section
)) {
425 if (strncmp(section
->sectname
, SECT_CONSTRUCTOR
,
426 sizeof(SECT_CONSTRUCTOR
)) == 0) {
428 structor_t
* constructors
= (structor_t
*)section
->addr
;
431 int num_constructors
= section
->size
/ sizeof(structor_t
);
432 int hit_null_constructor
= 0;
435 i
< num_constructors
&&
436 OSMetaClass::checkModLoad(metaHandle
);
439 if (constructors
[i
]) {
440 (*constructors
[i
])();
441 } else if (!hit_null_constructor
) {
442 hit_null_constructor
= 1;
443 OSRuntimeLog(theKext
, kOSRuntimeLogSpec
,
444 "Null constructor in kext %s segment %s!",
445 kmodInfo
->name
, section
->segname
);
448 load_success
= OSMetaClass::checkModLoad(metaHandle
);
451 } /* if (constructors) */
452 } /* if (strncmp...) */
453 } /* for (section...) */
454 } /* for (segment...) */
456 /* We failed so call all of the destructors. We must do this before
457 * calling OSMetaClass::postModLoad() as the OSMetaClass destructors
458 * will alter state (in the metaHandle) used by that function.
462 /* Scan the header for all destructor sections, in any
463 * segment, and invoke the constructors within those sections.
465 for (segment
= firstsegfromheader(header
);
466 segment
!= failure_segment
&& segment
!= 0;
467 segment
= nextsegfromheader(header
, segment
)) {
469 OSRuntimeUnloadCPPForSegment(segment
);
471 } /* for (segment...) */
474 /* Now, regardless of success so far, do the post-init registration
475 * and cleanup. If we had to call the unloadCPP function, static
476 * destructors have removed classes from the stalled list so no
477 * metaclasses will actually be registered.
479 result
= OSMetaClass::postModLoad(metaHandle
);
481 /* If we've otherwise been fine up to now, but OSMetaClass::postModLoad()
482 * fails (typically due to a duplicate class), tear down all the C++
483 * stuff from the kext. This isn't necessary for libkern/OSMetaClass stuff,
484 * but may be necessary for other C++ code. We ignore the return value
485 * because it's only a fail when there are existing instances of libkern
486 * classes, and there had better not be any created on the C++ init path.
488 if (load_success
&& result
!= KMOD_RETURN_SUCCESS
) {
489 (void)OSRuntimeFinalizeCPP(kmodInfo
, NULL
);
492 if (theKext
&& load_success
&& result
== KMOD_RETURN_SUCCESS
) {
493 theKext
->setCPPInitialized(true);
496 OSSafeRelease(theKext
);
501 #pragma mark Libkern Init
502 #endif /* PRAGMA_MARK */
503 /*********************************************************************
505 *********************************************************************/
507 /*********************************************************************
508 *********************************************************************/
509 extern lck_spin_t gOSObjectTrackLock
;
510 extern lck_grp_t
* IOLockGroup
;
511 extern kmod_info_t g_kernel_kmod_info
;
513 void OSlibkernInit(void)
515 lck_spin_init(&gOSObjectTrackLock
, IOLockGroup
, LCK_ATTR_NULL
);
517 // This must be called before calling OSRuntimeInitializeCPP.
518 OSMetaClassBase::initialize();
520 if (kOSReturnSuccess
!= OSRuntimeInitializeCPP(&g_kernel_kmod_info
, 0)) {
521 panic("OSRuntime: C++ runtime failed to initialize.");
524 gKernelCPPInitialized
= true;
532 #pragma mark C++ Allocators & Deallocators
533 #endif /* PRAGMA_MARK */
534 /*********************************************************************
535 * C++ Allocators & Deallocators
536 *********************************************************************/
538 operator new(size_t size
)
542 result
= (void *) kern_os_malloc(size
);
547 operator delete(void * addr
)
554 operator new[](unsigned long sz
)
557 return kern_os_malloc(sz
);
561 operator delete[](void * ptr
)
569 /* PR-6481964 - The compiler is going to check for size overflows in calls to
570 * new[], and if there is an overflow, it will call __throw_length_error.
571 * This is an unrecoverable error by the C++ standard, so we must panic here.
573 * We have to put the function inside the std namespace because of how the
574 * compiler expects the name to be mangled.
579 __throw_length_error(const char *msg __unused
)
581 panic("Size of array created by new[] has overflowed");