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