1 /* 2 * Copyright (c) 2000-2020 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 * @OSF_COPYRIGHT@ 30 */ 31 32 #ifdef XNU_KERNEL_PRIVATE 33 34 #ifndef _KERN_STARTUP_H_ 35 #define _KERN_STARTUP_H_ 36 37 #include <stdbool.h> 38 #include <stddef.h> 39 #include <stdint.h> 40 41 #include <libkern/section_keywords.h> 42 43 __BEGIN_DECLS 44 45 #pragma GCC visibility push(hidden) 46 47 /*! 48 * @enum startup_subsystem_id_t 49 * 50 * @abstract 51 * Represents a stage of kernel intialization, ubnd allows for subsystems 52 * to register initializers for a specific stage. 53 * 54 * @discussion 55 * Documentation of each subsystem initialization sequence exists in 56 * @file doc/startup.md. 57 */ 58 __enum_decl(startup_subsystem_id_t, uint32_t, { 59 STARTUP_SUB_NONE = 0, /**< reserved for the startup subsystem */ 60 61 STARTUP_SUB_TUNABLES, /**< support for the tunables subsystem */ 62 STARTUP_SUB_TIMEOUTS, /**< configurable machine timeouts */ 63 STARTUP_SUB_LOCKS, /**< various subsystem locks */ 64 STARTUP_SUB_KPRINTF, /**< kprintf initialization */ 65 66 STARTUP_SUB_PMAP_STEAL, /**< to perform various pmap carveouts */ 67 STARTUP_SUB_KMEM, /**< once kmem_alloc is ready */ 68 STARTUP_SUB_ZALLOC, /**< initialize zalloc and kalloc */ 69 STARTUP_SUB_KTRACE, /**< initialize kernel trace */ 70 STARTUP_SUB_PERCPU, /**< initialize the percpu subsystem */ 71 STARTUP_SUB_EVENT, /**< initiailze the event subsystem */ 72 73 STARTUP_SUB_CODESIGNING, /**< codesigning subsystem */ 74 STARTUP_SUB_OSLOG, /**< oslog and kernel logging */ 75 STARTUP_SUB_MACH_IPC, /**< Mach IPC */ 76 STARTUP_SUB_THREAD_CALL, /**< Thread calls */ 77 STARTUP_SUB_SYSCTL, /**< registers sysctls */ 78 STARTUP_SUB_EARLY_BOOT, /**< interrupts/preemption are turned on */ 79 80 STARTUP_SUB_LOCKDOWN = ~0u, /**< reserved for the startup subsystem */ 81 }); 82 83 /*! 84 * Stores the last subsystem to have been fully initialized; 85 */ 86 extern startup_subsystem_id_t startup_phase; 87 88 /*! 89 * @enum startup_debug_t 90 * 91 * @abstract 92 * Flags set in the @c startup_debug global to configure startup debugging. 93 */ 94 __options_decl(startup_debug_t, uint32_t, { 95 STARTUP_DEBUG_NONE = 0x00000000, 96 STARTUP_DEBUG_VERBOSE = 0x00000001, 97 }); 98 99 extern startup_debug_t startup_debug; 100 101 /*! 102 * @enum startup_rank 103 * 104 * @abstract 105 * Specifies in which rank a given initializer runs within a given section 106 * to register initializers for a specific rank within the subsystem. 107 * 108 * @description 109 * A startup function, declared with @c STARTUP or @c STARTUP_ARG, can specify 110 * a rank within the subsystem they initialize. 111 * 112 * @c STARTUP_RANK_NTH(n) will let callbacks be run at stage @c n (0-based). 113 * 114 * @c STARTUP_RANK_FIRST, @c STARTUP_RANK_SECOND, @c STARTUP_RANK_THIRD and 115 * @c STARTUP_RANK_FOURTH are given as convenient names for these. 116 * 117 * @c STARTUP_RANK_MIDDLE is a reserved value that will let startup functions 118 * run after all the @c STARTUP_RANK_NTH(n) ones have. 119 * 120 * @c STARTUP_RANK_NTH_LATE_NTH(n) will let callbacks be run then in @c n rank 121 * after the @c STARTUP_RANK_MIDDLE ones (0-based). 122 * 123 * @c STARTUP_RANK_LAST callbacks will run absolutely last after everything 124 * else did for this subsystem. 125 */ 126 __enum_decl(startup_rank_t, uint32_t, { 127 #define STARTUP_RANK_NTH(n) ((startup_rank_t)(n - 1)) 128 STARTUP_RANK_FIRST = 0, 129 STARTUP_RANK_SECOND = 1, 130 STARTUP_RANK_THIRD = 2, 131 STARTUP_RANK_FOURTH = 3, 132 133 STARTUP_RANK_MIDDLE = 0x7fffffff, 134 135 #define STARTUP_RANK_LATE_NTH(n) \ 136 ((startup_rank_t)(STARTUP_RANK_MIDDLE + 1 + (n))) 137 138 STARTUP_RANK_LAST = 0xffffffff, 139 }); 140 141 #if KASAN 142 /* 143 * The use of weird sections that get unmapped confuse the hell out of kasan, 144 * so for KASAN leave things in regular __TEXT/__DATA segments 145 */ 146 #define STARTUP_CODE_SEGSECT "__TEXT,__text" 147 #define STARTUP_DATA_SEGSECT "__DATA,__init" 148 #define STARTUP_HOOK_SEGMENT "__DATA" 149 #define STARTUP_HOOK_SECTION "__init_entry_set" 150 #elif defined(__x86_64__) 151 /* Intel doesn't have a __BOOTDATA but doesn't protect __KLD */ 152 #define STARTUP_CODE_SEGSECT "__TEXT,__text" 153 #define STARTUP_DATA_SEGSECT "__KLDDATA,__init" 154 #define STARTUP_HOOK_SEGMENT "__KLDDATA" 155 #define STARTUP_HOOK_SECTION "__init_entry_set" 156 #else 157 /* arm protects __KLD early, so use __BOOTDATA for data */ 158 #define STARTUP_CODE_SEGSECT "__TEXT,__text" 159 #define STARTUP_DATA_SEGSECT "__BOOTDATA,__init" 160 #define STARTUP_HOOK_SEGMENT "__BOOTDATA" 161 #define STARTUP_HOOK_SECTION "__init_entry_set" 162 #endif 163 164 /*! 165 * @macro __startup_func 166 * 167 * @abstract 168 * Attribute to place on functions used only during the kernel startup phase. 169 * 170 * @description 171 * Code marked with this attribute will be unmapped after kernel lockdown. 172 */ 173 #define __startup_func \ 174 __PLACE_IN_SECTION(STARTUP_CODE_SEGSECT) \ 175 __attribute__((cold, visibility("hidden"))) 176 177 /*! 178 * @macro __startup_data 179 * 180 * @abstract 181 * Attribute to place on globals used during the kernel startup phase. 182 * 183 * @description 184 * Data marked with this attribute will be unmapped after kernel lockdown. 185 */ 186 #define __startup_data \ 187 __PLACE_IN_SECTION(STARTUP_DATA_SEGSECT) 188 189 /*! 190 * @macro STARTUP 191 * 192 * @abstract 193 * Declares a kernel startup callback. 194 */ 195 #define STARTUP(subsystem, rank, func) \ 196 __STARTUP(func, __LINE__, subsystem, rank, func) 197 198 /*! 199 * @macro STARTUP_ARG 200 * 201 * @abstract 202 * Declares a kernel startup callback that takes an argument. 203 */ 204 #define STARTUP_ARG(subsystem, rank, func, arg) \ 205 __STARTUP_ARG(func, __LINE__, subsystem, rank, func, arg) 206 207 /*! 208 * @macro TUNABLE 209 * 210 * @abstract 211 * Declares a read-only kernel tunable that is read from a boot-arg with 212 * a default value, without further processing. 213 * 214 * @param type_t 215 * Should be an integer type or bool. 216 * 217 * @param var 218 * The name of the C variable to use for storage. 219 * 220 * @param boot_arg 221 * The name of the boot-arg to parse for initialization 222 * 223 * @param default_value 224 * The default value for the tunable if the boot-arg is absent. 225 */ 226 #define TUNABLE(type_t, var, boot_arg, default_value) \ 227 SECURITY_READ_ONLY_LATE(type_t) var = default_value; \ 228 __TUNABLE(type_t, var, boot_arg) 229 230 /*! 231 * @macro TUNABLE_WRITEABLE 232 * 233 * @abstract 234 * Declares a writeable kernel tunable that is read from a boot-arg with 235 * a default value, without further processing. 236 * 237 * @param type_t 238 * Should be an integer type or bool. 239 * 240 * @param var 241 * The name of the C variable to use for storage. 242 * 243 * @param boot_arg 244 * The name of the boot-arg to parse for initialization 245 * 246 * @param default_value 247 * The default value for the tunable if the boot-arg is absent. 248 */ 249 #define TUNABLE_WRITEABLE(type_t, var, boot_arg, default_value) \ 250 type_t var = default_value; \ 251 __TUNABLE(type_t, var, boot_arg) 252 253 #if DEBUG || DEVELOPMENT 254 #define TUNABLE_DEV_WRITEABLE(type_t, var, boot_arg, default_value) \ 255 TUNABLE_WRITEABLE(type_t, var, boot_arg, default_value) 256 #else 257 #define TUNABLE_DEV_WRITEABLE(type_t, var, boot_arg, default_value) \ 258 TUNABLE(type_t, var, boot_arg, default_value) 259 #endif 260 261 /*! 262 * @macro TUNABLE_STR 263 * 264 * @abstract 265 * Declares a read-only kernel tunable that is read from a boot-arg with 266 * a default value, without further processing. 267 * 268 * @param var 269 * The name of the C variable to use for storage. 270 * 271 * @param count 272 * The number of bytes in the buffer. 273 * 274 * @param boot_arg 275 * The name of the boot-arg to parse for initialization 276 * 277 * @param default_value 278 * The default value for the tunable if the boot-arg is absent. 279 */ 280 #define TUNABLE_STR(var, count, boot_arg, default_value) \ 281 char __security_const_late var[count] = default_value; \ 282 __TUNABLE_STR(var, boot_arg) 283 284 /*! 285 * @enum tunable_dt_flags_t 286 * 287 * @abstract 288 * Flags used with the @c TUNABLE_DT* macros. 289 * 290 * @description 291 * If TUNABLE_DT_CHECK_CHOSEN is set, a value in 292 * /chosen/<dt_base>/<dt_name> takes precedence over any value in 293 * /<dt_base>/<dt_name>. /chosen is by convention the area where 294 * synthesized values not coming from the serialized device tree are 295 * being added, so this provides a way for e.g. the boot-loader to 296 * set/override tunables. 297 */ 298 __options_decl(tunable_dt_flags_t, uint32_t, { 299 TUNABLE_DT_NONE = 0x00000000, 300 TUNABLE_DT_CHECK_CHOSEN = 0x00000001, 301 }); 302 303 /*! 304 * @macro TUNABLE_DT 305 * 306 * @abstract 307 * Like TUNABLE, but gets the initial value from both Device Tree and 308 * boot-args. The order in which the initial value is resolved is as 309 * follows, with later steps overriding previous ones (if they are 310 * specified): 311 * 312 * 1. Device Tree Entry "/<dt_base>/<dt_name>", 313 * 2. If TUNABLE_DT_CHECK_CHOSEN is set, Device Tree Entry 314 * "/chosen/<dt_base>/<dt_name>" (see the description for 315 * @c tunable_dt_flags_t), 316 * 3. boot-args. 317 * 318 * @param type_t 319 * Should be an integer type or bool. 320 * 321 * @param var 322 * The name of the C variable to use for storage. 323 * 324 * @param dt_base 325 * The name of the DT node containing the property. 326 * 327 * @param dt_name 328 * The name of the DT property containing the default value. 329 * 330 * @param boot_arg 331 * The name of the boot-arg overriding the initial value from the DT. 332 * 333 * @param default_value 334 * The default value for the tunable if both DT entry and boot-arg are 335 * absent. 336 * 337 * @param flags 338 * See the description for @c tunable_dt_flags_t. 339 */ 340 #define TUNABLE_DT(type_t, var, dt_base, dt_name, boot_arg, default_value, flags) \ 341 SECURITY_READ_ONLY_LATE(type_t) var = default_value; \ 342 __TUNABLE_DT(type_t, var, dt_base, dt_name, boot_arg, flags) 343 344 /*! 345 * @macro TUNABLE_DT_WRITEABLE 346 * 347 * @abstract 348 * Like TUNABLE_WRITEABLE, but gets the initial value from both Device 349 * Tree and boot-args. The order in which the initial value is 350 * resolved is as follows, with later steps overriding previous ones 351 * (if they are specified): 352 * 353 * 1. Device Tree Entry "/<dt_base>/<dt_name>", 354 * 2. If TUNABLE_DT_CHECK_CHOSEN is set, Device Tree Entry 355 * "/chosen/<dt_base>/<dt_name>" (see the description for 356 * @c tunable_dt_flags_t), 357 * 3. boot-args. 358 * 359 * @param type_t 360 * Should be an integer type or bool. 361 * 362 * @param var 363 * The name of the C variable to use for storage. 364 * 365 * @param dt_base 366 * The name of the DT node containing the property. 367 * 368 * @param dt_name 369 * The name of the DT property containing the default value. 370 * 371 * @param boot_arg 372 * The name of the boot-arg overriding the initial value from the DT. 373 * 374 * @param default_value 375 * The default value for the tunable if both DT entry and boot-arg are 376 * absent. 377 * 378 * @param flags 379 * See the description for @c tunable_dt_flags_t. 380 */ 381 #define TUNABLE_DT_WRITEABLE(type_t, var, dt_base, dt_name, boot_arg, default_value, flags) \ 382 type_t var = default_value; \ 383 __TUNABLE_DT(type_t, var, dt_base, dt_name, boot_arg, flags) 384 385 #if DEBUG || DEVELOPMENT 386 #define TUNABLE_DT_DEV_WRITEABLE(type_t, var, dt_base, dt_name, boot_arg, default_value, flags) \ 387 TUNABLE_DT_WRITEABLE(type_t, var, dt_base, dt_name, boot_arg, default_value, flags) 388 #else 389 #define TUNABLE_DT_DEV_WRITEABLE(type_t, var, dt_base, dt_name, boot_arg, default_value, flags) \ 390 TUNABLE_DT(type_t, var, dt_base, dt_name, boot_arg, default_value, flags) 391 #endif 392 393 /* 394 * Machine Timeouts 395 * 396 * Machine Timeouts are timeouts for low level kernel code manifesting 397 * as _Atomic uint64_t variables, whose default value can be 398 * overridden and scaled via the device tree and boot-args. 399 * 400 * Each timeout has a name, looked up directly as the property name in 401 * the device tree in both the "/machine-timeouts" and 402 * "/chosen/machine-timeouts" nodes. The "chosen" property always 403 * overrides the other one. This allows fixed per-device timeouts in 404 * the device tree to be overridden by iBoot in "chosen". 405 * 406 * Additionally, the same name with "-scale" appended is looked up as 407 * properties for optional scale factors. Scale factors are not 408 * overridden by chosen, instead all scale factors (including global 409 * and/or boot-arg scale factors) combine by multiplication. 410 * 411 * The special name "global-scale" provides a scale that applies to 412 * every timeout. 413 * 414 * All property names can be used as boot-args by prefixing 415 * "ml-timeout-", e.g. th global scale is available as the 416 * "ml-timeout-global-scale" boot-arg. 417 * 418 * By convention, if the timeout value resolves to 0, the timeout 419 * should be disabled. 420 */ 421 422 /* 423 * Machine Timeouts types. See the next section for what unit 424 * they are in. 425 * 426 * We use _Atomic, but only with relaxed ordering: This is just to 427 * make sure all devices see consistent values all the time. Since 428 * the actual timeout value will be seen as 0 before initializaton, 429 * relaxed ordering means that code that runs concurrently with 430 * initialization only risks to see a disabled timeout during early 431 * boot. 432 */ 433 typedef _Atomic uint64_t machine_timeout_t; 434 435 /* 436 * Units 437 * 438 * Machine Timeouts are ALWAYS in picoseconds in the device tree or 439 * boot-args, to avoid confusion when changing or comparing timeouts 440 * as a user, but the actual storage value might contain the same 441 * duration in another unit, calculated by the initialization code. 442 * 443 * This is done because otherwise we would likely introduce another 444 * multiplication in potentially hot code paths, given that code that 445 * actually uses the timeout storage variable is unlikely to work with 446 * picosecond values when comparing against the timeout deadline. 447 * 448 * This unit scale is *only* applied during initialization at early 449 * boot, and only if the timeout's default value was overridden 450 * through the device tree or a boot-arg. 451 */ 452 #define MACHINE_TIMEOUT_UNIT_PSEC 1 453 #define MACHINE_TIMEOUT_UNIT_NSEC 1000 454 #define MACHINE_TIMEOUT_UNIT_USEC (1000*1000) 455 #define MACHINE_TIMEOUT_UNIT_MSEC (1000*1000*1000) 456 // Special unit for timebase ticks (usually 1/24MHz) 457 #define MACHINE_TIMEOUT_UNIT_TIMEBASE 0 458 459 // DT property names are limited to 31 chars, minus "-global" suffix 460 #define MACHINE_TIMEOUT_MAX_NAME_LEN 25 461 struct machine_timeout_spec { 462 void *ptr; 463 uint64_t default_value; 464 uint64_t unit_scale; 465 char name[MACHINE_TIMEOUT_MAX_NAME_LEN + 1]; 466 bool (*skip_predicate)(struct machine_timeout_spec const *); 467 }; 468 469 extern void 470 machine_timeout_init_with_suffix(const struct machine_timeout_spec *spec, char const *phase_suffix); 471 472 extern void 473 machine_timeout_init(const struct machine_timeout_spec *spec); 474 475 #if DEVELOPMENT || DEBUG 476 // Late timeout (re-)initialization, at the end of bsd_init() 477 extern void 478 machine_timeout_bsd_init(void); 479 #endif /* DEVELOPMENT || DEBUG */ 480 481 /*! 482 * @macro MACHINE_TIMEOUT and MACHINE_TIMEOUT_DEV_WRITEABLE 483 * 484 * @abstract 485 * Defines a Machine Timeout that can be overridden and 486 * scaled through the device tree and boot-args. 487 * 488 * The variant with the _DEV_WRITEABLE suffix does not mark the timeout as 489 * SECURITY_READ_ONLY_LATE on DEVELOPMENT kernels, so that e.g. 490 * machine_timeout_init_with_suffix or sysctls can change it after lockdown. 491 * 492 * @param var 493 * The name of the C variable to use for storage. If the storage value 494 * contains 0, the timeout is considered disabled by convention. 495 * 496 * @param timeout_name 497 * The name of the timeout, used for property and boot-arg names. See 498 * the general description of Machine Timeouts above for how this name 499 * ends up being used. 500 * 501 * @param timeout_default 502 * The default value for the timeout if not specified through device 503 * tree or boot-arg. Will still be scaled if a scale factor exists. 504 * 505 * @param var_unit 506 * The unit that the storage variable is in. Note that timeout values 507 * must always be specified as picoseconds in the device tree and 508 * boot-args, but timeout initialization will convert the value to the 509 * unit specified here before writing it to the storage variable. 510 * 511 * @param skip_predicate 512 * Optionally, a function to call to decide whether the timeout should 513 * be set or not. If NULL, the timeout will always be set (if 514 * specified anywhere). A predicate has the following signature: 515 * bool skip_predicate (struct machine_timeout_spec const *) 516 */ 517 518 #define _MACHINE_TIMEOUT(var, timeout_name, timeout_default, var_unit, skip_pred) \ 519 struct machine_timeout_spec \ 520 __machine_timeout_spec_ ## var = { \ 521 .ptr = &var, \ 522 .default_value = timeout_default, \ 523 .unit_scale = var_unit, \ 524 .name = timeout_name, \ 525 .skip_predicate = skip_pred, \ 526 }; \ 527 __STARTUP_ARG(var, __LINE__, TIMEOUTS, STARTUP_RANK_FIRST, \ 528 machine_timeout_init, &__machine_timeout_spec_ ## var) 529 530 #define MACHINE_TIMEOUT(var, name, default, unit, skip_predicate) \ 531 SECURITY_READ_ONLY_LATE(machine_timeout_t) var = 0; \ 532 _MACHINE_TIMEOUT(var, name, default, unit, skip_predicate) 533 534 #if DEVELOPMENT || DEBUG 535 #define MACHINE_TIMEOUT_DEV_WRITEABLE(var, name, default, unit, skip_predicate) \ 536 machine_timeout_t var = 0; \ 537 _MACHINE_TIMEOUT(var, name, default, unit, skip_predicate) 538 #else 539 #define MACHINE_TIMEOUT_DEV_WRITEABLE(var, name, default, unit, skip_predicate) \ 540 MACHINE_TIMEOUT(var, name, default, unit, skip_predicate) 541 #endif /* DEVELOPMENT || DEBUG */ 542 543 /*! 544 * @macro MACHINE_TIMEOUT_SPEC_REF 545 * 546 * @abstract 547 * References a previously defined MACHINE_TIMEOUT. 548 * 549 * This is primarily useful for overriding individual timeouts 550 * at arbitrary times (even after boot), by manually calling 551 * machine_timeout_init_with_suffix() with this macro 552 * as first argument, and a suffix to apply to both device tree and 553 * boot-arg as second argument. 554 * 555 * @param var 556 * The name of the C variable used for storage, as it was specified 557 * in MACHINE_TIMEOUT. 558 */ 559 #define MACHINE_TIMEOUT_SPEC_REF(var) (&__machine_timeout_spec_ ## var) 560 561 /*! 562 * @macro MACHINE_TIMEOUT_SPEC_DECL 563 * 564 * @abstract 565 * Declaration of machine timeout spec, mostly useful to make it known 566 * for MACHINE_TIMEOUT_SPEC_REF. 567 * 568 * @param var 569 * The name of the C variable used for storage, as it was specified 570 * in MACHINE_TIMEOUT. 571 */ 572 #define MACHINE_TIMEOUT_SPEC_DECL(var) extern struct machine_timeout_spec __machine_timeout_spec_ ## var 573 574 /* 575 * Event subsystem 576 * 577 * This allows to define a few system-wide events that allow for loose coupling 578 * between subsystems interested in those events and the emitter. 579 */ 580 581 /*! 582 * @macro EVENT_DECLARE() 583 * 584 * @brief 585 * Declares an event namespace with a given callback type. 586 * 587 * @param name The name for the event (typically in all caps). 588 * @param cb_type_t A function type for the callbacks. 589 */ 590 #define EVENT_DECLARE(name, cb_type_t) \ 591 struct name##_event { \ 592 struct event_hdr evt_link; \ 593 cb_type_t *evt_cb; \ 594 }; \ 595 extern struct event_hdr name##_HEAD 596 597 /*! 598 * @macro EVENT_DEFINE() 599 * 600 * @brief 601 * Defines the head for the event corresponding to an EVENT_DECLARE() 602 */ 603 #define EVENT_DEFINE(name) \ 604 __security_const_late struct event_hdr name##_HEAD 605 606 /*! 607 * @macro EVENT_REGISTER_HANDLER() 608 * 609 * @brief 610 * Registers a handler for a given event. 611 * 612 * @param name The name for the event as declared in EVENT_DECLARE() 613 * @param handler The handler to register for this event. 614 */ 615 #define EVENT_REGISTER_HANDLER(name, handler) \ 616 __EVENT_REGISTER(name, __LINE__, handler) 617 618 619 /*! 620 * @macro EVENT_INVOKE() 621 * 622 * @brief 623 * Call all the events handlers with the specified arguments. 624 * 625 * @param name The name for the event as declared in EVENT_DECLARE() 626 * @param handler The handler to register for this event. 627 */ 628 #define EVENT_INVOKE(name, ...) \ 629 for (struct event_hdr *__e = &name##_HEAD; (__e = __e->next);) { \ 630 __container_of(__e, struct name##_event, \ 631 evt_link)->evt_cb(__VA_ARGS__); \ 632 } 633 634 635 #if DEBUG || DEVELOPMENT 636 637 /*! 638 * @macro SYSCTL_TEST_REGISTER 639 * 640 * @abstract 641 * Declares a test that will appear under @c debug.test.${name}. 642 * 643 * @param name 644 * An identifier that will be stringified to form the sysctl test name. 645 * 646 * @param cb 647 * The callback to run, of type: 648 * <code> 649 * int (callback *)(int64_t value, int64_t *); 650 * </code> 651 */ 652 #define SYSCTL_TEST_REGISTER(name, cb) \ 653 static __startup_data struct sysctl_test_setup_spec \ 654 __startup_SYSCTL_TEST_ ## name = { \ 655 .st_name = #name, \ 656 .st_func = &cb, \ 657 }; \ 658 STARTUP_ARG(SYSCTL, STARTUP_RANK_MIDDLE, \ 659 sysctl_register_test_startup, &__startup_SYSCTL_TEST_ ## name) 660 661 #endif /* DEBUG || DEVELOPMENT */ 662 #pragma mark - internals 663 664 __END_DECLS 665 666 #ifdef __cplusplus 667 template <typename T> 668 struct __startup_tunable { 669 static const bool value = false; 670 }; 671 672 template <> 673 struct __startup_tunable <bool>{ 674 static const bool value = true; 675 }; 676 #define __startup_type_is_bool(type_t) __startup_tunable<type_t>::value 677 #else 678 #define __startup_type_is_bool(type_t) __builtin_types_compatible_p(bool, type_t) 679 #endif 680 681 __BEGIN_DECLS 682 683 #define __TUNABLE(type_t, var, key) \ 684 static __startup_data char __startup_TUNABLES_name_ ## var[] = key; \ 685 static __startup_data struct startup_tunable_spec \ 686 __startup_TUNABLES_spec_ ## var = { \ 687 .name = __startup_TUNABLES_name_ ## var, \ 688 .var_addr = (void *)&var, \ 689 .var_len = sizeof(type_t), \ 690 .var_is_bool = __startup_type_is_bool(type_t), \ 691 }; \ 692 __STARTUP_ARG(var, __LINE__, TUNABLES, STARTUP_RANK_FIRST, \ 693 kernel_startup_tunable_init, &__startup_TUNABLES_spec_ ## var) 694 695 #define __TUNABLE_STR(var, key) \ 696 static __startup_data char __startup_TUNABLES_name_ ## var[] = key; \ 697 static __startup_data struct startup_tunable_spec \ 698 __startup_TUNABLES_spec_ ## var = { \ 699 .name = __startup_TUNABLES_name_ ## var, \ 700 .var_addr = (void *)&var, \ 701 .var_len = sizeof(var), \ 702 .var_is_str = true, \ 703 }; \ 704 __STARTUP_ARG(var, __LINE__, TUNABLES, STARTUP_RANK_FIRST, \ 705 kernel_startup_tunable_init, &__startup_TUNABLES_spec_ ## var) 706 707 #define __TUNABLE_DT(type_t, var, dt_base_key, dt_name_key, boot_arg_key, flags) \ 708 static __startup_data char __startup_TUNABLES_dt_base_ ## var[] = dt_base_key; \ 709 static __startup_data char __startup_TUNABLES_dt_name_ ## var[] = dt_name_key; \ 710 static __startup_data char __startup_TUNABLES_name_ ## var[] = boot_arg_key; \ 711 static __startup_data struct startup_tunable_dt_spec \ 712 __startup_TUNABLES_DT_spec_ ## var = { \ 713 .dt_base = __startup_TUNABLES_dt_base_ ## var, \ 714 .dt_name = __startup_TUNABLES_dt_name_ ## var, \ 715 .dt_chosen_override = (bool)((flags) & TUNABLE_DT_CHECK_CHOSEN), \ 716 .boot_arg_name = __startup_TUNABLES_name_ ## var, \ 717 .var_addr = (void *)&var, \ 718 .var_len = sizeof(type_t), \ 719 .var_is_bool = __startup_type_is_bool(type_t), \ 720 }; \ 721 __STARTUP_ARG(var, __LINE__, TUNABLES, STARTUP_RANK_FIRST, \ 722 kernel_startup_tunable_dt_init, &__startup_TUNABLES_DT_spec_ ## var) 723 724 #ifdef __cplusplus 725 #define __STARTUP_FUNC_CAST(func, a) \ 726 (void(*)(const void *))func 727 #else 728 #define __STARTUP_FUNC_CAST(func, a) \ 729 (typeof(func(a))(*)(const void *))func 730 #endif 731 732 733 #define __STARTUP1(name, line, subsystem, rank, func, a, b) \ 734 __PLACE_IN_SECTION(STARTUP_HOOK_SEGMENT "," STARTUP_HOOK_SECTION) \ 735 static const struct startup_entry \ 736 __startup_ ## subsystem ## _entry_ ## name ## _ ## line = { \ 737 STARTUP_SUB_ ## subsystem, \ 738 rank, __STARTUP_FUNC_CAST(func, a), b, \ 739 } 740 741 #define __STARTUP(name, line, subsystem, rank, func) \ 742 __STARTUP1(name, line, subsystem, rank, func, , NULL) 743 744 #define __STARTUP_ARG(name, line, subsystem, rank, func, arg) \ 745 __STARTUP1(name, line, subsystem, rank, func, arg, arg) 746 747 struct startup_entry { 748 startup_subsystem_id_t subsystem; 749 startup_rank_t rank; 750 void (*func)(const void *); 751 const void *arg; 752 }; 753 754 struct startup_tunable_spec { 755 const char *name; 756 void *var_addr; 757 int var_len; 758 bool var_is_bool; 759 bool var_is_str; 760 }; 761 762 struct startup_tunable_dt_spec { 763 const char *dt_base; 764 const char *dt_name; 765 bool dt_chosen_override; 766 const char *boot_arg_name; 767 void *var_addr; 768 int var_len; 769 bool var_is_bool; 770 }; 771 772 #if DEBUG || DEVELOPMENT 773 struct sysctl_test_setup_spec { 774 const char *st_name; 775 int (*st_func)(int64_t, int64_t *); 776 }; 777 778 extern void sysctl_register_test_startup( 779 struct sysctl_test_setup_spec *spec); 780 #endif /* DEBUG || DEVELOPMENT */ 781 782 /* 783 * Kernel and machine startup declarations 784 */ 785 786 /* Initialize kernel */ 787 extern void kernel_startup_bootstrap(void); 788 extern void kernel_startup_initialize_upto(startup_subsystem_id_t upto); 789 extern void kernel_startup_tunable_init(const struct startup_tunable_spec *); 790 extern void kernel_startup_tunable_dt_init(const struct startup_tunable_dt_spec *); 791 extern void kernel_bootstrap(void); 792 793 /* Initialize machine dependent stuff */ 794 extern void machine_init(void); 795 796 extern void slave_main(void *machine_param); 797 798 /* 799 * The following must be implemented in machine dependent code. 800 */ 801 802 /* Slave cpu initialization */ 803 extern void slave_machine_init(void *machine_param); 804 805 /* Device subsystem initialization */ 806 extern void device_service_create(void); 807 808 struct event_hdr { 809 struct event_hdr *next; 810 }; 811 812 extern void event_register_handler(struct event_hdr *event_hdr); 813 814 #define __EVENT_REGISTER(name, lno, handler) \ 815 static __security_const_late struct name##_event name##_event_##lno = { \ 816 .evt_link.next = &name##_HEAD, \ 817 .evt_cb = (handler), \ 818 }; \ 819 __STARTUP_ARG(name, lno, EVENT, STARTUP_RANK_FIRST, \ 820 event_register_handler, &name##_event_##lno.evt_link) 821 822 #ifdef MACH_BSD 823 824 /* BSD subsystem initialization */ 825 extern void bsd_init(void); 826 827 #endif /* MACH_BSD */ 828 829 #pragma GCC visibility pop 830 831 __END_DECLS 832 833 #endif /* _KERN_STARTUP_H_ */ 834 835 #endif /* XNU_KERNEL_PRIVATE */ 836