xref: /xnu-11417.140.69/doc/lifecycle/startup.md (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS DistributionsXNU startup sequence
2*43a90889SApple OSS Distributions====================
3*43a90889SApple OSS Distributions
4*43a90889SApple OSS DistributionsAdding code to run during early boot.
5*43a90889SApple OSS Distributions
6*43a90889SApple OSS Distributions### General Principles
7*43a90889SApple OSS Distributions
8*43a90889SApple OSS DistributionsXNU Startup sequence is driven by the `<kern/startup.h>` module.
9*43a90889SApple OSS Distributions
10*43a90889SApple OSS DistributionsThe startup sequence is made of individual subsystems (the `STARTUP_SUB_*`
11*43a90889SApple OSS Distributionsvalues of the `startup_subsystem_id_t` type) that get initialized in sequence.
12*43a90889SApple OSS Distributions
13*43a90889SApple OSS DistributionsA subsystem can use ranks to order the various initializers that make up its
14*43a90889SApple OSS Distributionsinitialization sequence. Usage of ranks is custom to each subsystem and must be
15*43a90889SApple OSS Distributionsdocumented in this file.
16*43a90889SApple OSS Distributions
17*43a90889SApple OSS DistributionsThe subsystem module will basically run hooks in that order:
18*43a90889SApple OSS Distributions
19*43a90889SApple OSS Distributions```
20*43a90889SApple OSS Distributionsfor (subsystem 0 -> N) {
21*43a90889SApple OSS Distributions  for (rank 0 -> N) {
22*43a90889SApple OSS Distributions    // run in no particular order for a given rank in the given subsystem
23*43a90889SApple OSS Distributions    init(subsystem, rank);
24*43a90889SApple OSS Distributions  }
25*43a90889SApple OSS Distributions}
26*43a90889SApple OSS Distributions```
27*43a90889SApple OSS Distributions
28*43a90889SApple OSS Distributions### Extending the startup sequence
29*43a90889SApple OSS Distributions
30*43a90889SApple OSS DistributionsWhen extending the startup sequence:
31*43a90889SApple OSS Distributions
32*43a90889SApple OSS Distributions1. add a new value to the `startup_subsystem_id_t` enum in the right order
33*43a90889SApple OSS Distributions2. document what services this phase provides, and how it uses ranks in this
34*43a90889SApple OSS Distributions   file.
35*43a90889SApple OSS Distributions
36*43a90889SApple OSS Distributions
37*43a90889SApple OSS DistributionsWhen hooking with a given subsystem, consult this documentation to use the
38*43a90889SApple OSS Distributionsproper rank for your callback.
39*43a90889SApple OSS Distributions
40*43a90889SApple OSS DistributionsIf a new rank needs to be used, update this documentation in the proper section.
41*43a90889SApple OSS Distributions
42*43a90889SApple OSS Distributions---------------------------------------------------------------------------------
43*43a90889SApple OSS Distributions
44*43a90889SApple OSS Distributions
45*43a90889SApple OSS Distributions`STARTUP_SUB_TUNABLES`
46*43a90889SApple OSS Distributions----------------------
47*43a90889SApple OSS Distributions
48*43a90889SApple OSS Distributions### Description
49*43a90889SApple OSS Distributions
50*43a90889SApple OSS DistributionsInitializes various globals that alter the behavior of the kernel, lookup
51*43a90889SApple OSS Distributionstables, ... Available hooks are:
52*43a90889SApple OSS Distributions
53*43a90889SApple OSS Distributions- `TUNABLES`: parses a boot arg into a global that will become read-only at
54*43a90889SApple OSS Distributions  lockdown time,
55*43a90889SApple OSS Distributions- `TUNABLE_WRITEABLE`: same as `TUNABLE` but the global will not be locked down.
56*43a90889SApple OSS Distributions
57*43a90889SApple OSS Distributions### Rank usage
58*43a90889SApple OSS Distributions
59*43a90889SApple OSS Distributions- Rank 1: `TUNABLE`, `TUNABLE_WRITEABLE`
60*43a90889SApple OSS Distributions- Middle: globals that require complex initialization (e.g. SFI classes).
61*43a90889SApple OSS Distributions
62*43a90889SApple OSS Distributions
63*43a90889SApple OSS Distributions`STARTUP_SUB_TIMEOUTS`
64*43a90889SApple OSS Distributions----------------------
65*43a90889SApple OSS Distributions
66*43a90889SApple OSS Distributions## Description
67*43a90889SApple OSS Distributions
68*43a90889SApple OSS DistributionsInitializes machine timeouts, which are device-tree/boot-args
69*43a90889SApple OSS Distributionsconfigurable timeouts for low level machine code.
70*43a90889SApple OSS Distributions
71*43a90889SApple OSS DistributionsSee the comments for the MACHINE_TIMEOUT macro on how they are used in
72*43a90889SApple OSS Distributionsdetail.
73*43a90889SApple OSS Distributions
74*43a90889SApple OSS Distributions- Rank 1: `MACHINE_TIMEOUT` specifications.
75*43a90889SApple OSS Distributions- Rank 2: `ml_io_timeouts_init` for scheduler hygiene.
76*43a90889SApple OSS Distributions- Middle: Global lock timeouts that are derived from machine timeouts.
77*43a90889SApple OSS Distributions
78*43a90889SApple OSS Distributions`STARTUP_SUB_LOCKS`
79*43a90889SApple OSS Distributions-------------------
80*43a90889SApple OSS Distributions
81*43a90889SApple OSS Distributions### Description
82*43a90889SApple OSS Distributions
83*43a90889SApple OSS DistributionsInitializes early locks that do not require any memory allocations to be
84*43a90889SApple OSS Distributionsinitialized. Available hooks are:
85*43a90889SApple OSS Distributions
86*43a90889SApple OSS Distributions- `LCK_GRP_DECLARE*`: automatically initialized lock groups,
87*43a90889SApple OSS Distributions- `LCK_ATTR_DECLARE`: automatically initialized lock attributes,
88*43a90889SApple OSS Distributions- `LCK_SPIN_DECLARE*`: automatically initialized spinlocks,
89*43a90889SApple OSS Distributions- `LCK_RW_DECLARE`: automatically initialized reader/writer lock,
90*43a90889SApple OSS Distributions- `LCK_MTX_DECLARE`: automatically initialized mutex,
91*43a90889SApple OSS Distributions- `SIMPLE_LOCK_DECLARE*`: automatically initialized simple locks.
92*43a90889SApple OSS Distributions
93*43a90889SApple OSS Distributions### Rank usage
94*43a90889SApple OSS Distributions
95*43a90889SApple OSS Distributions- Rank 1: Initializes the module (`lck_mod_init`),
96*43a90889SApple OSS Distributions- Rank 2: `LCK_ATTR_DECLARE`, `LCK_GRP_DECLARE*`
97*43a90889SApple OSS Distributions- Rank 3: compact lock group table init
98*43a90889SApple OSS Distributions- Rank 4: `LCK_SPIN_DECLARE*`, `LCK_MTX_DECLARE*`,
99*43a90889SApple OSS Distributions  `LCK_RW_DECLARE`, `SIMPLE_LOCK_DECLARE*`.
100*43a90889SApple OSS Distributions
101*43a90889SApple OSS Distributions
102*43a90889SApple OSS Distributions`STARTUP_SUB_KPRINTF`
103*43a90889SApple OSS Distributions---------------------
104*43a90889SApple OSS Distributions
105*43a90889SApple OSS Distributions### Description
106*43a90889SApple OSS Distributions
107*43a90889SApple OSS DistributionsInitializes the kprintf subsystem.
108*43a90889SApple OSS Distributions
109*43a90889SApple OSS Distributions### Rank usage
110*43a90889SApple OSS Distributions
111*43a90889SApple OSS Distributions- Rank 1: calls the module initializer (`PE_init_kprintf`).
112*43a90889SApple OSS Distributions
113*43a90889SApple OSS Distributions
114*43a90889SApple OSS Distributions`STARTUP_SUB_PMAP_STEAL`
115*43a90889SApple OSS Distributions------------------------
116*43a90889SApple OSS Distributions
117*43a90889SApple OSS Distributions### Description
118*43a90889SApple OSS Distributions
119*43a90889SApple OSS DistributionsAllows for subsystems to steal early memory.
120*43a90889SApple OSS Distributions
121*43a90889SApple OSS Distributions### Rank usage
122*43a90889SApple OSS Distributions
123*43a90889SApple OSS Distributions- First rank:
124*43a90889SApple OSS Distributions  - `cpu_data_startup_init`: Allocate per-CPU memory that needs to be accessible with MMU disabled
125*43a90889SApple OSS Distributions  - `socd_client_init`: Steal memory for SoC diagnostics
126*43a90889SApple OSS Distributions  - `vm_map_steal_memory`: Allocate bootstrap VM maps prior to the zone allocator coming up
127*43a90889SApple OSS Distributions
128*43a90889SApple OSS Distributions- Last rank:
129*43a90889SApple OSS Distributions  - `init_ecc_bad_pages`: Exclude frames detected as bad from frame allocator
130*43a90889SApple OSS Distributions
131*43a90889SApple OSS Distributions`STARTUP_SUB_KMEM`
132*43a90889SApple OSS Distributions------------------
133*43a90889SApple OSS Distributions
134*43a90889SApple OSS Distributions### Description
135*43a90889SApple OSS Distributions
136*43a90889SApple OSS DistributionsDenotes that `kmem_alloc` is now usable.
137*43a90889SApple OSS Distributions
138*43a90889SApple OSS Distributions### Rank usage
139*43a90889SApple OSS Distributions
140*43a90889SApple OSS Distributions- First rank:
141*43a90889SApple OSS Distributions  - `zone_set_map_sizes`: Select physical limits for zone map
142*43a90889SApple OSS Distributions  - `vm_compressor_set_size`: Reserve VA for the compressor submap
143*43a90889SApple OSS Distributions
144*43a90889SApple OSS Distributions- Rank 2:
145*43a90889SApple OSS Distributions  - `kmem_range_startup_init`: Initialize data structures associated wiht ranges registered via
146*43a90889SApple OSS Distributions    the `KMEM_RANGE_REGISTER_[STATIC|DYNAMIC]` mechanisms.
147*43a90889SApple OSS Distributions
148*43a90889SApple OSS Distributions- Rank 3:
149*43a90889SApple OSS Distributions  - `kmem_range_init`: Shuffle and initialize ranges that have been registered up to now
150*43a90889SApple OSS Distributions
151*43a90889SApple OSS Distributions- Last rank:
152*43a90889SApple OSS Distributions  - `io_map_init`: Creates an early `kernel_map` carve-out for mapping memory shared with devices
153*43a90889SApple OSS Distributions
154*43a90889SApple OSS Distributions`STARTUP_SUB_ZALLOC`
155*43a90889SApple OSS Distributions--------------------
156*43a90889SApple OSS Distributions
157*43a90889SApple OSS Distributions### Description
158*43a90889SApple OSS Distributions
159*43a90889SApple OSS DistributionsInitializes the zone allocator.
160*43a90889SApple OSS Distributions
161*43a90889SApple OSS Distributions- `ZONE_DEFINE`, `ZONE_INIT`: automatically initialized permanent zones.
162*43a90889SApple OSS Distributions- `ZONE_VIEW_DEFINE`, `KALLOC_HEAP_DEFINE`: zone and kalloc heap views.
163*43a90889SApple OSS Distributions
164*43a90889SApple OSS Distributions
165*43a90889SApple OSS Distributions### Rank usage
166*43a90889SApple OSS Distributions
167*43a90889SApple OSS Distributions- Rank 1: `zone_init`: setup the zone subsystem, this allows for the already
168*43a90889SApple OSS Distributions  created VM/pmap zones to become dynamic.
169*43a90889SApple OSS Distributions
170*43a90889SApple OSS Distributions- Rank 2: `vm_page_module_init`: create the "vm pages" zone.
171*43a90889SApple OSS Distributions  The `vm_page_zone` must be created prior to `kalloc_init`; that routine can
172*43a90889SApple OSS Distributions  trigger `zalloc()`s (for e.g. mutex statistic structure initialization).
173*43a90889SApple OSS Distributions
174*43a90889SApple OSS Distributions  The `vm_page_zone` must exist to satisfy fictitious page allocations
175*43a90889SApple OSS Distributions  (which are used for guard pages by the guard mode zone allocator).
176*43a90889SApple OSS Distributions
177*43a90889SApple OSS Distributions- Rank 3: Initialize kalloc.
178*43a90889SApple OSS Distributions
179*43a90889SApple OSS Distributions- Rank 4: Handle `ZONE_DEFINE` and `ZONE_INIT`.
180*43a90889SApple OSS Distributions
181*43a90889SApple OSS Distributions- Middle:   zone and kalloc heaps (`ZONE_VIEW_DEFINE`, `KALLOC_HEAP_DEFINE`).
182*43a90889SApple OSS Distributions
183*43a90889SApple OSS Distributions`STARTUP_SUB_KTRACE`
184*43a90889SApple OSS Distributions--------------------
185*43a90889SApple OSS Distributions
186*43a90889SApple OSS Distributions### Description
187*43a90889SApple OSS Distributions
188*43a90889SApple OSS DistributionsInitializes kdebug and kperf and starts tracing if requested with boot-args.
189*43a90889SApple OSS Distributions
190*43a90889SApple OSS Distributions### Rank usage
191*43a90889SApple OSS Distributions
192*43a90889SApple OSS DistributionsN/A.
193*43a90889SApple OSS Distributions
194*43a90889SApple OSS Distributions`STARTUP_SUB_PERCPU`
195*43a90889SApple OSS Distributions--------------------
196*43a90889SApple OSS Distributions
197*43a90889SApple OSS Distributions### Description
198*43a90889SApple OSS Distributions
199*43a90889SApple OSS DistributionsInitializes the percpu subsystem.
200*43a90889SApple OSS Distributions
201*43a90889SApple OSS Distributions### Rank usage
202*43a90889SApple OSS Distributions
203*43a90889SApple OSS DistributionsRank 1: allocates the percpu memory, `percpu_foreach_base` and `percpu_foreach`
204*43a90889SApple OSS Distributions        become usable.
205*43a90889SApple OSS Distributions
206*43a90889SApple OSS DistributionsRank 2: sets up static percpu counters.
207*43a90889SApple OSS Distributions
208*43a90889SApple OSS Distributions
209*43a90889SApple OSS Distributions### Rank usage
210*43a90889SApple OSS Distributions
211*43a90889SApple OSS Distributions- Rank 1: `LCK_MTX_DECLARE`.
212*43a90889SApple OSS Distributions
213*43a90889SApple OSS Distributions`STARTUP_SUB_CODESIGNING`
214*43a90889SApple OSS Distributions-------------------------
215*43a90889SApple OSS Distributions
216*43a90889SApple OSS Distributions### Description
217*43a90889SApple OSS Distributions
218*43a90889SApple OSS DistributionsInitializes the codesigning subsystem.
219*43a90889SApple OSS Distributions
220*43a90889SApple OSS Distributions### Rank usage
221*43a90889SApple OSS Distributions
222*43a90889SApple OSS Distributions- Rank 1: calls the module initializer (`cs_init`).
223*43a90889SApple OSS Distributions
224*43a90889SApple OSS Distributions`STARTUP_SUB_OSLOG`
225*43a90889SApple OSS Distributions-------------------
226*43a90889SApple OSS Distributions
227*43a90889SApple OSS Distributions### Description
228*43a90889SApple OSS Distributions
229*43a90889SApple OSS DistributionsInitializes the `os_log` facilities.
230*43a90889SApple OSS Distributions
231*43a90889SApple OSS Distributions### Rank usage
232*43a90889SApple OSS Distributions
233*43a90889SApple OSS Distributions- Rank 1: Calls the module initializer (`oslog_init`).
234*43a90889SApple OSS Distributions
235*43a90889SApple OSS Distributions
236*43a90889SApple OSS Distributions`STARTUP_SUB_MACH_IPC`
237*43a90889SApple OSS Distributions----------------------
238*43a90889SApple OSS Distributions
239*43a90889SApple OSS Distributions### Description
240*43a90889SApple OSS Distributions
241*43a90889SApple OSS DistributionsInitializes the Mach IPC subsystem.
242*43a90889SApple OSS Distributions
243*43a90889SApple OSS Distributions### Rank usage
244*43a90889SApple OSS Distributions
245*43a90889SApple OSS Distributions- Rank 1: Initializes IPC submodule globals (ipc tables, voucher hashes, ...)
246*43a90889SApple OSS Distributions- Rank last: Final IPC initialization.
247*43a90889SApple OSS Distributions
248*43a90889SApple OSS Distributions
249*43a90889SApple OSS Distributions`STARTUP_SUB_THREAD_CALL`
250*43a90889SApple OSS Distributions-------------------------
251*43a90889SApple OSS Distributions
252*43a90889SApple OSS Distributions### Description
253*43a90889SApple OSS Distributions
254*43a90889SApple OSS DistributionsInitializes the Thread call subsystem (and dependent subsystems).
255*43a90889SApple OSS Distributions
256*43a90889SApple OSS Distributions### Rank usage
257*43a90889SApple OSS Distributions
258*43a90889SApple OSS Distributions- Rank 1: Initiailizes the thread call subsystem
259*43a90889SApple OSS Distributions- Rank Middle: Initialize modules needing thread calls
260*43a90889SApple OSS Distributions
261*43a90889SApple OSS Distributions
262*43a90889SApple OSS Distributions`STARTUP_SUB_SYSCTL`
263*43a90889SApple OSS Distributions--------------------
264*43a90889SApple OSS Distributions
265*43a90889SApple OSS Distributions### Description
266*43a90889SApple OSS Distributions
267*43a90889SApple OSS DistributionsInitializes the sysctl kernel subsystem
268*43a90889SApple OSS Distributions
269*43a90889SApple OSS Distributions### Rank usage
270*43a90889SApple OSS Distributions
271*43a90889SApple OSS Distributions- Rank 1: automatic `SYSCTL_NODE` registration.
272*43a90889SApple OSS Distributions- Rank 2: automatic `SYSCTL_OID` registration.
273*43a90889SApple OSS Distributions- Middle: other manual early registrations.
274*43a90889SApple OSS Distributions- Last: registrations of dummy nodes in the constant nodes to allow extension.
275*43a90889SApple OSS Distributions
276*43a90889SApple OSS Distributions
277*43a90889SApple OSS Distributions`STARTUP_SUB_EARLY_BOOT`
278*43a90889SApple OSS Distributions------------------------
279*43a90889SApple OSS Distributions
280*43a90889SApple OSS Distributions### Description
281*43a90889SApple OSS Distributions
282*43a90889SApple OSS DistributionsDenotes that subsystems that expect to operate with
283*43a90889SApple OSS Distributionsinterrupts or preemption enabled may begin enforcement.
284*43a90889SApple OSS Distributions
285*43a90889SApple OSS Distributions### Rank usage
286*43a90889SApple OSS Distributions
287*43a90889SApple OSS Distributions- Rank 1: Initialize some BSD globals
288*43a90889SApple OSS Distributions- Middle: Initialize some early BSD subsystems
289*43a90889SApple OSS Distributions
290*43a90889SApple OSS Distributions
291*43a90889SApple OSS Distributions`STARTUP_SUB_EXCLAVES`
292*43a90889SApple OSS Distributions------------------------
293*43a90889SApple OSS Distributions
294*43a90889SApple OSS Distributions### Description
295*43a90889SApple OSS Distributions
296*43a90889SApple OSS DistributionsEarly exclaves initialization.
297*43a90889SApple OSS Distributions
298*43a90889SApple OSS Distributions### Rank usage
299*43a90889SApple OSS Distributions
300*43a90889SApple OSS Distributions- Rank 1: Determine run-time support for exclaves
301*43a90889SApple OSS Distributions- Middle: Initialize tightbeam runtime
302*43a90889SApple OSS Distributions
303*43a90889SApple OSS Distributions
304*43a90889SApple OSS Distributions`STARTUP_SUB_LOCKDOWN`
305*43a90889SApple OSS Distributions----------------------
306*43a90889SApple OSS Distributions
307*43a90889SApple OSS Distributions### Description
308*43a90889SApple OSS Distributions
309*43a90889SApple OSS DistributionsDenotes that the kernel is locking down, this phase should never be hooked.
310*43a90889SApple OSS DistributionsWhen the kernel locks down:
311*43a90889SApple OSS Distributions
312*43a90889SApple OSS Distributions- data marked `__startup_data` or `__startup_const`, and code marked
313*43a90889SApple OSS Distributions  `__startup_func`, is unmapped;
314*43a90889SApple OSS Distributions- data marked `__security_const_late` or `SECURITY_READ_ONLY_LATE` becomes
315*43a90889SApple OSS Distributions  read-only.
316*43a90889SApple OSS Distributions
317*43a90889SApple OSS Distributions### Rank usage
318*43a90889SApple OSS Distributions
319*43a90889SApple OSS DistributionsN/A.
320