XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
M2: boot XNU inside the module
Goal: insmod xnu.ko runs XNU's own boot path (arm_init → kernel_bootstrap → kernel_bootstrap_thread → bsd_init), with XNU's kprintf/printf output reaching dmesg through FKI.
Base tree: the macOS-DroidVM k27 tree (xnu-12377.121.6 plus its patch series). Its 4K BCM2837 machine layer is already non-Apple: GIC, generic timer, no monitor (PPL/SPTM).
What we add:
- a
LINUX64machine config, as patches in this repository on top of that series; - the APSL-side runtime in
xnu-ko/.
How XNU and Linux share a CPU
These facts come from reading the k27 tree.
| Register / state | XNU expects | Linux uses it for | Resolution |
|---|---|---|---|
TPIDR_EL1 |
current thread (cpu_data.h current_thread_fast, asm.h, pac_asm.h, locore.s, cswitch.s) |
per-cpu offset (non-VHE, which includes every guest) | Early boot: the world switch swaps it. Later: linux64 reads the thread from an FKI per-task slot reached through Linux's current (SP_EL0), and TPIDR_EL1 is never written. |
SP_EL0 / SPSel |
kernel runs on SP_EL0, exception stack on SP_EL1 (start.s: "Set SP_EL0 to interrupt stack") |
SP_EL0 = current task; kernel on SP_EL1 |
XNU C code runs on whatever sp is selected. Only XNU's asm (locore/cswitch/start) relies on SPSel, and linux64 replaces that asm. |
VBAR_EL1 |
XNU's vectors | Linux's vectors | Linux keeps the vectors. FKI forwards faults whose PC is in XNU text, and user exceptions of XNU tasks, to xnu.ko, which enters XNU's sleh with a synthesized arm_saved_state. |
TTBR1/TCR/MAIR/SCTLR |
arm_vm_init builds and installs its own tables |
Linux owns them | Never written (see the VM section). |
| PAC keys | XNU kernel keys | Linux kernel keys (Ubuntu has kernel PAC) | Swapped by the world switch on every entry and exit. FKI functions that switch keys are built without pac-ret. |
DAIF |
ml_set_interrupts_enabled |
the same bits | Shared directly: XNU masking interrupts masks Linux interrupts, which is correct. |
| Generic timer / GIC | rtclock programs CNTV_*; GIC driver |
Linux owns both | linux64 rtclock: deadlines become FKI hrtimers and IPIs become FKI calls. XNU never touches the GIC. |
World switch (xnu-ko/world.S)
xnu_call(fn, args...)enters the XNU world:- disable preemption and IRQs (early boot only);
- save Linux
TPIDR_EL1and the PAC keys; - install XNU's
TPIDR_EL1and keys; - call
fn; - restore everything on return.
- Upcalls from XNU into FKI (for example
fki_logfrom XNU's console) go through a stub that does the reverse switch around the FKI call, so Linux code always runs with Linux state.
Boot handoff (xnu-ko/boot.c)
One "DRAM" region of N MiB, vzalloc'd and executable, laid out as the XNU image followed by free memory. XNU sees
physBaseas a synthetic PA, andvirtBaseis the region's VA, so XNU'sphystokv/kvtophysarithmetic holds across the region. A synthetic PA only matters where hardware walks it, and with the VM design below hardware never sees XNU page tables.boot_argsv2:virtBase,physBase,memSize,topOfKernelData, and a command line (debug=…,-v,serial=0,vm_compression_limit=…).ADT (Apple device tree) built by
xnu-ko:/chosen:dram-base,dram-size,random-seed(256 bytes, fromfki_random),firmware-version,lock-owner;/cpuswith onecpu0;- an
arm-ionode with no devices; - the properties
PE_init_platformandml_parse_cpu_topologyread.
The ADT writer can follow UniXNU's DarwinBoot FDT→ADT converter (BSD-2-Clause-Patent).
Rebase:
arm_slide_rebase_and_sign_imagemust not re-apply the threaded fixups thatxnu.kohas already applied.linux64sets the slide globals and skips the rebase.
VM (osfmk/arm/pmap under LINUX64)
- Kernel VA window: a fixed 512 GiB window inside Linux's vmalloc range (
0xfffff00000000000), reserved by FKI. In-tree this is__get_vm_area_caller; the out-of-tree form resolves it through kallsyms. XNU'sVM_MIN/MAX_KERNEL_ADDRESSforLINUX64are set to that window. - XNU's pmap stays as the bookkeeping layer: pv lists, ledgers and
pmap_entersemantics are unchanged. Its tables are never installed in hardware.- Kernel mappings: the PTE write path (
write_pte_fast/write_pte_strong) forkernel_pmapadditionally callsfki_kmap(va, page_of(pa), prot), which maps the backing Linux page at the same VA, followed byfki_kunmap/TLB invalidation. - User mappings (M3): the same hook writes into the Linux mm of the task (the single-VMA design in #201).
- Kernel mappings: the PTE write path (
arm_vm_init(LINUX64): no table building and noTTBR/TCRwrites. It setsgVirtBase,gPhysBase,mem_size,first_availandavail_end, and the static kernel ranges, then callspmap_bootstrap.
Order of work (each step ends with dmesg proof)
- M2a: world switch,
boot_argsand ADT; callarm_initwith crumbs reported through FKI; reachkernel_startup_bootstrap. - M2b:
LINUX64arm_vm_initand the pmap hook;kprintf initializedappears in dmesg. - M2c:
rtclockon FKI timers;machine_startup→kernel_bootstrap; zalloc and kalloc running. - M2d: threads.
Switch_context/thread_blockrun on Linux kthreads, one kthread per XNU thread, with the XNU thread pointer in the FKI task slot.kernel_bootstrap_threadruns. - M2e:
bsd_initwith a RAM-disk root, plus the trust cache and AMFI stubs.
M2b plan: XNU's own page tables are walked by hardware
A finding that simplifies the VM layer: the 4K LINUX64 kernel VA range is 0xffffffe000000000–0xfffffffbffffffff (T1SZ=25, a 512 GiB root).
- One L0 slot. That range lies inside the top L0 slot of arm64 Linux's 48-bit layout (
L0[511]). - Free L1 entries. Within that slot, Linux uses only
L1[511](the last GiB: fixmap and PCI I/O). XNU's range isL1[384..495], which Linux leaves empty.
So instead of mirroring XNU's PTEs into Linux:
- Shared root. XNU's root table (
cpu_tte) is Linux's own L1 page forL0[511]. XNU writes only entries 384–495, never zeroes the page, and never touchesTTBR1/TCR/MAIR/SCTLR. The hardware then walks XNU's L2/L3 tables directly, and XNU's pmap works unchanged for kernel mappings. - Real physical addresses. XNU's "DRAM" must have real PAs: one physically contiguous region (in-tree FKI uses
alloc_contig_pages; the out-of-tree PoC resolves it through kallsyms).gPhysBaseis then the real PA of that region. - Boot window.
xnu.komaps the region at XNU's link window (image atVM_KERNEL_LINK_ADDRESS+ slide) with a bootstrap L2 table plugged into one of those L1 entries, and runs XNU there. This is what iBoot's bootstrap tables do.arm_vm_initlater replaces that entry with its own tables for the same VA→PA mapping. - Memory attributes. XNU's attribute indices are checked against Linux's MAIR: WB=0 matches; DISABLE=3 is nGnRnE in both; WRITECOMB and WRITETHRU are remapped in the LINUX64 config.