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