]> git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSRuntime.cpp
122acda60a68b5254cbeac64150f05dd9e983487
[apple/xnu.git] / libkern / c++ / OSRuntime.cpp
1 /*
2 * Copyright (c) 2000,2008-2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (c) 1997 Apple Inc.
30 *
31 */
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>
37
38 #include <sys/cdefs.h>
39
40 __BEGIN_DECLS
41
42 #include <string.h>
43 #include <mach/mach_types.h>
44 #include <libkern/kernel_mach_header.h>
45 #include <libkern/prelink.h>
46 #include <stdarg.h>
47
48 #if PRAGMA_MARK
49 #pragma mark Constants &c.
50 #endif /* PRAGMA_MARK */
51 OSKextLogSpec kOSRuntimeLogSpec =
52 kOSKextLogErrorLevel |
53 kOSKextLogLoadFlag |
54 kOSKextLogKextBookkeepingFlag;
55
56 #if PRAGMA_MARK
57 #pragma mark Logging Bootstrap
58 #endif /* PRAGMA_MARK */
59 /*********************************************************************
60 * kern_os Logging Bootstrap
61 *
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;
68
69 #define OSRuntimeLog(kext, flags, format, args...) \
70 do { \
71 if (gKernelCPPInitialized) { \
72 OSKextLog((kext), (flags), (format), ## args); \
73 } else { \
74 printf((format), ## args); \
75 } \
76 } while (0)
77
78 #if PRAGMA_MARK
79 #pragma mark kern_os Allocator Package
80 #endif /* PRAGMA_MARK */
81 /*********************************************************************
82 * kern_os Allocator Package
83 *********************************************************************/
84
85 /*********************************************************************
86 *********************************************************************/
87 #if OSALLOCDEBUG
88 extern int debug_iomalloc_size;
89 #endif
90
91 /*********************************************************************
92 *********************************************************************/
93 void *
94 kern_os_malloc(size_t size)
95 {
96 void *mem;
97 if (size == 0) {
98 return 0;
99 }
100
101 mem = kallocp_tag_bt((vm_size_t *)&size, VM_KERN_MEMORY_LIBKERN);
102 if (!mem) {
103 return 0;
104 }
105
106 #if OSALLOCDEBUG
107 OSAddAtomic(size, &debug_iomalloc_size);
108 #endif
109
110 bzero(mem, size);
111
112 return mem;
113 }
114
115 /*********************************************************************
116 *********************************************************************/
117 void
118 kern_os_free(void * addr)
119 {
120 size_t size;
121 size = kalloc_size(addr);
122 #if OSALLOCDEBUG
123 OSAddAtomic(-size, &debug_iomalloc_size);
124 #endif
125
126 kfree_addr(addr);
127 }
128
129 /*********************************************************************
130 *********************************************************************/
131 void *
132 kern_os_realloc(
133 void * addr,
134 size_t nsize)
135 {
136 void *nmem;
137 size_t osize;
138
139 if (!addr) {
140 return kern_os_malloc(nsize);
141 }
142
143 osize = kalloc_size(addr);
144 if (nsize == osize) {
145 return addr;
146 }
147
148 if (nsize == 0) {
149 kfree_addr(addr);
150 return 0;
151 }
152
153 nmem = kallocp_tag_bt((vm_size_t *)&nsize, VM_KERN_MEMORY_LIBKERN);
154 if (!nmem) {
155 kfree_addr(addr);
156 return 0;
157 }
158
159 #if OSALLOCDEBUG
160 OSAddAtomic((nsize - osize), &debug_iomalloc_size);
161 #endif
162
163 if (nsize > osize) {
164 (void)memset((char *)nmem + osize, 0, nsize - osize);
165 }
166 (void)memcpy(nmem, addr, (nsize > osize) ? osize : nsize);
167 kfree_addr(addr);
168
169 return nmem;
170 }
171
172 #if PRAGMA_MARK
173 #pragma mark Libkern Init
174 #endif /* PRAGMA_MARK */
175 /*********************************************************************
176 * Libkern Init
177 *********************************************************************/
178
179 #if __GNUC__ >= 3
180 void
181 __cxa_pure_virtual( void )
182 {
183 panic("%s", __FUNCTION__);
184 }
185 #else
186 void
187 __pure_virtual( void )
188 {
189 panic("%s", __FUNCTION__);
190 }
191 #endif
192
193 extern lck_grp_t * IOLockGroup;
194 extern kmod_info_t g_kernel_kmod_info;
195
196 enum {
197 kOSSectionNamesDefault = 0,
198 kOSSectionNamesBuiltinKext = 1,
199 kOSSectionNamesCount = 2,
200 };
201 enum {
202 kOSSectionNameInitializer = 0,
203 kOSSectionNameFinalizer = 1,
204 kOSSectionNameCount = 2
205 };
206
207 static const char *
208 gOSStructorSectionNames[kOSSectionNamesCount][kOSSectionNameCount] = {
209 { SECT_MODINITFUNC, SECT_MODTERMFUNC },
210 { kBuiltinInitSection, kBuiltinTermSection }
211 };
212
213 void
214 OSlibkernInit(void)
215 {
216 // This must be called before calling OSRuntimeInitializeCPP.
217 OSMetaClassBase::initialize();
218
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.");
223 }
224
225 gKernelCPPInitialized = true;
226
227 return;
228 }
229
230 __END_DECLS
231
232 #if PRAGMA_MARK
233 #pragma mark C++ Runtime Load/Unload
234 #endif /* PRAGMA_MARK */
235 /*********************************************************************
236 * kern_os C++ Runtime Load/Unload
237 *********************************************************************/
238
239
240 typedef void (*structor_t)(void);
241
242 static bool
243 OSRuntimeCallStructorsInSection(
244 OSKext * theKext,
245 kmod_info_t * kmodInfo,
246 void * metaHandle,
247 kernel_segment_command_t * segment,
248 const char * sectionName,
249 uintptr_t textStart,
250 uintptr_t textEnd)
251 {
252 kernel_section_t * section;
253 bool result = TRUE;
254
255 for (section = firstsect(segment);
256 section != NULL;
257 section = nextsect(segment, section)) {
258 if (strncmp(section->sectname, sectionName, sizeof(section->sectname) - 1)) {
259 continue;
260 }
261
262 structor_t * structors = (structor_t *)section->addr;
263 if (!structors) {
264 continue;
265 }
266
267 structor_t structor;
268 unsigned int num_structors = section->size / sizeof(structor_t);
269 unsigned int hit_null_structor = 0;
270 unsigned int firstIndex = 0;
271
272 if (textStart) {
273 // bsearch for any in range
274 unsigned int baseIdx;
275 unsigned int lim;
276 uintptr_t value;
277 firstIndex = num_structors;
278 for (lim = num_structors, baseIdx = 0; lim; lim >>= 1) {
279 value = (uintptr_t) structors[baseIdx + (lim >> 1)];
280 if (!value) {
281 panic("%s: null structor", kmodInfo->name);
282 }
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)) {
289 break;
290 }
291 }
292 break;
293 }
294 if (textStart > value) {
295 // move right
296 baseIdx += (lim >> 1) + 1;
297 lim--;
298 }
299 // else move left
300 }
301 baseIdx = (baseIdx + (lim >> 1));
302 }
303 for (;
304 (firstIndex < num_structors)
305 && (!metaHandle || OSMetaClass::checkModLoad(metaHandle));
306 firstIndex++) {
307 if ((structor = structors[firstIndex])) {
308 if ((textStart && ((uintptr_t) structor < textStart))
309 || (textEnd && ((uintptr_t) structor >= textEnd))) {
310 break;
311 }
312
313 (*structor)();
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);
319 }
320 }
321 if (metaHandle) {
322 result = OSMetaClass::checkModLoad(metaHandle);
323 }
324 break;
325 } /* for (section...) */
326 return result;
327 }
328
329 /*********************************************************************
330 *********************************************************************/
331 kern_return_t
332 OSRuntimeFinalizeCPP(
333 OSKext * theKext)
334 {
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;
341 uintptr_t textStart;
342 uintptr_t textEnd;
343
344 textStart = 0;
345 textEnd = 0;
346 sectionNames = gOSStructorSectionNames[kOSSectionNamesDefault];
347 if (theKext) {
348 if (!theKext->isCPPInitialized()) {
349 result = KMOD_RETURN_SUCCESS;
350 goto finish;
351 }
352 kmodInfo = theKext->kmod_info;
353 if (!kmodInfo || !kmodInfo->address) {
354 result = kOSKextReturnInvalidArgument;
355 goto finish;
356 }
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];
363 }
364 } else {
365 kmodInfo = &g_kernel_kmod_info;
366 header = (kernel_mach_header_t *)kmodInfo->address;
367 }
368
369 /* OSKext checks for this condition now, but somebody might call
370 * this function directly (the symbol is exported....).
371 */
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:",
376 kmodInfo->name);
377 OSKext::reportOSMetaClassInstances(kmodInfo->name, kOSRuntimeLogSpec);
378 result = kOSMetaClassHasInstances;
379 goto finish;
380 }
381
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).
386 */
387 metaHandle = OSMetaClass::preModLoad(kmodInfo->name);
388
389 OSSymbol::checkForPageUnload((void *)kmodInfo->address,
390 (void *)(kmodInfo->address + kmodInfo->size));
391
392 header = (kernel_mach_header_t *)kmodInfo->address;
393 segment = firstsegfromheader(header);
394
395 for (segment = firstsegfromheader(header);
396 segment != 0;
397 segment = nextsegfromheader(header, segment)) {
398 OSRuntimeCallStructorsInSection(theKext, kmodInfo, NULL, segment,
399 sectionNames[kOSSectionNameFinalizer], textStart, textEnd);
400 }
401
402 (void)OSMetaClass::postModLoad(metaHandle);
403
404 if (theKext) {
405 theKext->setCPPInitialized(false);
406 }
407 result = KMOD_RETURN_SUCCESS;
408 finish:
409 return result;
410 }
411
412 /*********************************************************************
413 *********************************************************************/
414 kern_return_t
415 OSRuntimeInitializeCPP(
416 OSKext * theKext)
417 {
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;
426 uintptr_t textStart;
427 uintptr_t textEnd;
428
429 textStart = 0;
430 textEnd = 0;
431 sectionNames = gOSStructorSectionNames[kOSSectionNamesDefault];
432 if (theKext) {
433 if (theKext->isCPPInitialized()) {
434 result = KMOD_RETURN_SUCCESS;
435 goto finish;
436 }
437
438 kmodInfo = theKext->kmod_info;
439 if (!kmodInfo || !kmodInfo->address) {
440 result = kOSKextReturnInvalidArgument;
441 goto finish;
442 }
443 header = (kernel_mach_header_t *)kmodInfo->address;
444
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];
450 }
451 } else {
452 kmodInfo = &g_kernel_kmod_info;
453 header = (kernel_mach_header_t *)kmodInfo->address;
454 }
455
456 /* Tell the meta class system that we are starting the load
457 */
458 metaHandle = OSMetaClass::preModLoad(kmodInfo->name);
459 assert(metaHandle);
460 if (!metaHandle) {
461 goto finish;
462 }
463
464 /* NO GOTO PAST HERE. */
465
466 /* Scan the header for all constructor sections, in any
467 * segment, and invoke the constructors within those sections.
468 */
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.
473 */
474 failure_segment = segment;
475 load_success = OSRuntimeCallStructorsInSection(
476 theKext, kmodInfo, metaHandle, segment,
477 sectionNames[kOSSectionNameInitializer],
478 textStart, textEnd);
479 } /* for (segment...) */
480
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.
484 */
485 if (!load_success) {
486 /* Scan the header for all destructor sections, in any
487 * segment, and invoke the constructors within those sections.
488 */
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...) */
495 }
496
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.
501 */
502 result = OSMetaClass::postModLoad(metaHandle);
503
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.
510 */
511 if (load_success && result != KMOD_RETURN_SUCCESS) {
512 (void)OSRuntimeFinalizeCPP(theKext); //kmodInfo, sectionNames, textStart, textEnd);
513 }
514
515 if (theKext && load_success && result == KMOD_RETURN_SUCCESS) {
516 theKext->setCPPInitialized(true);
517 }
518 finish:
519 return result;
520 }
521
522 /*********************************************************************
523 * Unload a kernel segment.
524 *********************************************************************/
525
526 void
527 OSRuntimeUnloadCPPForSegment(
528 kernel_segment_command_t * segment)
529 {
530 OSRuntimeCallStructorsInSection(NULL, &g_kernel_kmod_info, NULL, segment,
531 gOSStructorSectionNames[kOSSectionNamesDefault][kOSSectionNameFinalizer], 0, 0);
532 }
533
534 #if PRAGMA_MARK
535 #pragma mark C++ Allocators & Deallocators
536 #endif /* PRAGMA_MARK */
537 /*********************************************************************
538 * C++ Allocators & Deallocators
539 *********************************************************************/
540 void *
541 operator new(size_t size)
542 {
543 void * result;
544
545 result = (void *) kern_os_malloc(size);
546 return result;
547 }
548
549 void
550 operator delete(void * addr)
551 #if __cplusplus >= 201103L
552 noexcept
553 #endif
554 {
555 kern_os_free(addr);
556 return;
557 }
558
559 void *
560 operator new[](unsigned long sz)
561 {
562 if (sz == 0) {
563 sz = 1;
564 }
565 return kern_os_malloc(sz);
566 }
567
568 void
569 operator delete[](void * ptr)
570 #if __cplusplus >= 201103L
571 noexcept
572 #endif
573 {
574 if (ptr) {
575 kern_os_free(ptr);
576 }
577 return;
578 }
579
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.
583 *
584 * We have to put the function inside the std namespace because of how the
585 * compiler expects the name to be mangled.
586 */
587 namespace std {
588 void
589 __throw_length_error(const char *msg __unused)
590 {
591 panic("Size of array created by new[] has overflowed");
592 }
593 };