Skip to content

XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)

C
7.0 KB
Raw Blame

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 LINUX64 machine 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:
    1. disable preemption and IRQs (early boot only);
    2. save Linux TPIDR_EL1 and the PAC keys;
    3. install XNU's TPIDR_EL1 and keys;
    4. call fn;
    5. restore everything on return.
  • Upcalls from XNU into FKI (for example fki_log from 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 physBase as a synthetic PA, and virtBase is the region's VA, so XNU's phystokv/kvtophys arithmetic 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_args v2: 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, from fki_random), firmware-version, lock-owner;
    • /cpus with one cpu0;
    • an arm-io node with no devices;
    • the properties PE_init_platform and ml_parse_cpu_topology read.

    The ADT writer can follow UniXNU's DarwinBoot FDT→ADT converter (BSD-2-Clause-Patent).

  • Rebase: arm_slide_rebase_and_sign_image must not re-apply the threaded fixups that xnu.ko has already applied. linux64 sets 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's VM_MIN/MAX_KERNEL_ADDRESS for LINUX64 are set to that window.
  • XNU's pmap stays as the bookkeeping layer: pv lists, ledgers and pmap_enter semantics are unchanged. Its tables are never installed in hardware.
    • Kernel mappings: the PTE write path (write_pte_fast/write_pte_strong) for kernel_pmap additionally calls fki_kmap(va, page_of(pa), prot), which maps the backing Linux page at the same VA, followed by fki_kunmap/TLB invalidation.
    • User mappings (M3): the same hook writes into the Linux mm of the task (the single-VMA design in #201).
  • arm_vm_init (LINUX64): no table building and no TTBR/TCR writes. It sets gVirtBase, gPhysBase, mem_size, first_avail and avail_end, and the static kernel ranges, then calls pmap_bootstrap.

Order of work (each step ends with dmesg proof)

  1. M2a: world switch, boot_args and ADT; call arm_init with crumbs reported through FKI; reach kernel_startup_bootstrap.
  2. M2b: LINUX64 arm_vm_init and the pmap hook; kprintf initialized appears in dmesg.
  3. M2c: rtclock on FKI timers; machine_startup → kernel_bootstrap; zalloc and kalloc running.
  4. M2d: threads. Switch_context/thread_block run on Linux kthreads, one kthread per XNU thread, with the XNU thread pointer in the FKI task slot. kernel_bootstrap_thread runs.
  5. M2e: bsd_init with 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 is L1[384..495], which Linux leaves empty.

So instead of mirroring XNU's PTEs into Linux:

  1. Shared root. XNU's root table (cpu_tte) is Linux's own L1 page for L0[511]. XNU writes only entries 384–495, never zeroes the page, and never touches TTBR1/TCR/MAIR/SCTLR. The hardware then walks XNU's L2/L3 tables directly, and XNU's pmap works unchanged for kernel mappings.
  2. 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). gPhysBase is then the real PA of that region.
  3. Boot window. xnu.ko maps the region at XNU's link window (image at VM_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_init later replaces that entry with its own tables for the same VA→PA mapping.
  4. 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.