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