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