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