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