XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
docs: M2b plan (shared top-level L1, hardware walks XNU's tables)
Heyang Zhoua7d655dbb839
diff --git a/docs/m2.md b/docs/m2.mdindex 3e78c60..ccfed8c 100644--- a/docs/m2.md+++ b/docs/m2.md@@ -60,3 +60,17 @@ These facts come from reading the k27 tree.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.