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