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