doc/startup.md
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);
}
}
When extending the startup sequence:
startup_subsystem_id_t enum in the right orderWhen 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_TUNABLESInitializes 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.TUNABLE, TUNABLE_WRITEABLESTARTUP_SUB_LOCKS_EARLYInitializes early locks that do not require any memory allocations to be initialized. Available hooks are:
LCK_GRP_DECLARE*: automatically initialized lock groups,LCK_GRP_ATTR_DECLARE: automatically initialized lock group attributes,LCK_ATTR_DECLARE: automatically initialized lock attributes,LCK_SPIN_DECLARE*: automatically initialized spinlocks,LCK_RW_DECLARE: automatically initialized reader/writer lock,LCK_MTX_EARLY_DECLARE*: automatically initialized mutexes, with statically
allocated buffers for statistics/tracing,SIMPLE_LOCK_DECLARE*: automatically initialized simple locks.lck_mod_init),LCK_GRP_ATTR_DECLARE, LCK_ATTR_DECLARE,LCK_GRP_DECLARE*LCK_SPIN_DECLARE*, LCK_MTX_EARLY_DECLARE*,
LCK_RW_DECLARE, SIMPLE_LOCK_DECLARE*.STARTUP_SUB_KPRINTFInitializes the kprintf subsystem.
PE_init_kprintf).STARTUP_SUB_PMAP_STEALAllows for subsystems to steal early memory.
N/A.
STARTUP_SUB_VM_KERNELDenotes that the early kernel VM is initialized.
N/A.
STARTUP_SUB_KMEMDenotes that kernel_memory_allocate is now usable.
N/A.
STARTUP_SUB_KMEM_ALLOCDenotes that kmem_alloc is now usable.
N/A.
STARTUP_SUB_ZALLOCInitializes the zone allocator.
ZONE_DECLARE, ZONE_INIT: automatically initialized permanent zones.ZONE_VIEW_DEFINE, KALLOC_HEAP_DEFINE: zone and kalloc heap views.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: Enable zone caching (uses kalloc)
Middle: for any initialization that only requires kalloc/zalloc
runs ZONE_DECLARE and ZONE_INIT.
Last: zone and kalloc heaps (ZONE_VIEW_DEFINE, KALLOC_HEAP_DEFINE).
STARTUP_SUB_PERCPUInitializes the percpu subsystem.
Rank 1: allocates the percpu memory, percpu_foreach_base and percpu_foreach
become usable.
Rank 2: sets up static percpu counters.
STARTUP_SUB_LOCKSInitializes kernel locks that might require allocations (due to statistics and tracing features). Available hooks are:
LCK_MTX_DECLARE: automatically initialized mutex,LCK_MTX_DECLARE.STARTUP_SUB_CODESIGNINGInitializes the codesigning subsystem.
cs_init).STARTUP_SUB_OSLOGInitializes the os_log facilities.
oslog_init).STARTUP_SUB_MACH_IPCInitializes the Mach IPC subsystem.
STARTUP_SUB_SYSCTLInitializes the sysctl kernel subsystem
SYSCTL_NODE registration.SYSCTL_OID registration.STARTUP_SUB_EARLY_BOOTDenotes that subsystems that expect to operate with interrupts or preemption enabled may begin enforcement.
N/A.
STARTUP_SUB_LOCKDOWNDenotes that the kernel is locking down, this phase should never be hooked. When the kernel locks down:
__startup_data and code marked __startup_func is unmapped,__security_const_late or SECURITY_READ_ONLY_LATE becomes
read-only.N/A.