XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
Golden system disk + per-run dm snapshot (scripts/vm-snap.sh); fki_file_open sizes block devices; JOP-off SCTLR write moved after the last kernel pointer authentication (a brk at EL1 hung the host); fault-loop host yields; monitor shows kernel faults and keeps printing
Heyang Zhou78871d96df3d
diff --git a/fki-ko/fki_mod.c b/fki-ko/fki_mod.cindex 80f2299..1c46164 100644--- a/fki-ko/fki_mod.c+++ b/fki-ko/fki_mod.c@@ -551,8 +551,8 @@ void *fki_file_open(const char *path, int writable, fki_u64 *size)if (IS_ERR(f))return NULL;- if (size)- *size = i_size_read(file_inode(f));+ if (size) /* the mapping's host: a block device's own size (a device node's inode says 0) */+ *size = i_size_read(f->f_mapping->host);return f;}EXPORT_SYMBOL(fki_file_open);diff --git a/scripts/vm-m2-kbisect.sh b/scripts/vm-m2-kbisect.shindex f0a0340..d029ccd 100755--- a/scripts/vm-m2-kbisect.sh+++ b/scripts/vm-m2-kbisect.sh@@ -7,7 +7,7 @@ make -s -C /lib/modules/$(uname -r)/build M=$PWD/xnu-ko KBUILD_EXTRA_SYMBOLS=$PWsudo dmesg -n ${CONLEVEL:-8}sudo rmmod xnu 2>/dev/null; sudo rmmod fki 2>/dev/null; sudo insmod fki-ko/fki.ko# a hosted XNU never unmounts its read-write root: check the host disk before each boot-if [ -n "$HOSTDISK" ]; then+if [ -n "$HOSTDISK" ] && [ "${HOSTDISK#/dev/}" = "$HOSTDISK" ]; thenfor t in 1 2 3; do r=$(sudo fsck.hfsplus -fy "$HOSTDISK" 2>&1 | tail -1); echo "fsck: $r"; case "$r" in *"appears to be OK"*) break;; esac; donefi# XNU's DRAM is one physically contiguous block (GBs): free the page cache and compact first, or the allocation failsdiff --git a/scripts/vm-snap.sh b/scripts/vm-snap.shnew file mode 100755index 0000000..25d727d--- /dev/null+++ b/scripts/vm-snap.sh@@ -0,0 +1,12 @@+#!/bin/bash+# In the VM: a fresh copy-on-write view of the golden system disk for one hosted run, /dev/mapper/xnusys.+# A hosted XNU that dies mid-write (or takes the host with it) leaves HFS+ corrupt; the golden image stays clean.+set -e+IMG=${IMG:-/var/tmp/full.img}; COW=${COW:-/var/tmp/full.cow}+sudo dmsetup remove xnusys 2>/dev/null || true+for l in $(losetup -j "$IMG" -O NAME -n) $(losetup -j "$COW" -O NAME -n 2>/dev/null); do sudo losetup -d "$l"; done+rm -f "$COW"; truncate -s 24G "$COW"+B=$(sudo losetup -f -r --show "$IMG"); C=$(sudo losetup -f --show "$COW")+sudo dmsetup create xnusys --table "0 $(sudo blockdev --getsz "$B") snapshot $B $C N 16"+sudo chmod 666 /dev/mapper/xnusys 2>/dev/null || true+echo "xnusys over $B (cow $C): $(sudo blockdev --getsize64 /dev/mapper/xnusys) bytes"diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.cindex 9f02045..f889a3f 100644--- a/xnu-ko/xnu_main.c+++ b/xnu-ko/xnu_main.c@@ -194,8 +194,16 @@ unsigned long xnu_fault_deferred(unsigned long *frame)* modified state, so each reuse takes one fault). Only report it, rarely, when it looks endless.*/if (xnu_kfault(far, esr) == 0) {- if (repeats == 100000)- logkv("xnu: one address took 100000 kernel faults in a row (each resolved), far", far);+ if (repeats && (repeats & (repeats - 1)) == 0 && repeats >= 1024) {+ logkv("xnu: one address keeps taking resolved kernel faults, repeats", repeats);+ logkv("xnu: far", far);+ logkv("xnu: esr", esr);+ logkv("xnu: pc ", pc);+ logkv("xnu: lr ", frame[30]);+ }+ /* a fault loop runs with interrupts masked: let Linux take its interrupts now and then (RCU, network) */+ if ((repeats & 63) == 63)+ ((void (*)(unsigned long, unsigned long, unsigned long))xnu_world_upcall)(811, 0, 0);return pc;}/* inside copyin/copyout: the routine's recovery path returns EFAULT to its caller, as XNU's own abort handler does */@@ -588,7 +596,9 @@ static int monitor_fn(void *arg)(void)arg;while (!fki_thread_should_stop()) {fki_msleep(2000);- if (!xnu_dbg || lines >= 90)+ /* every 2 s for the first 3 minutes, then every 20 s */+ static unsigned long ticks;+ if (!xnu_dbg || (lines >= 90 && (++ticks % 10) != 0))continue;char buf[420]; char *p = buf;const char *h = "xnu: monitor";@@ -603,6 +613,9 @@ static int monitor_fn(void *arg)h = " el0exc=";while (*h) *p++ = *h++;p = put_dec(p, xnu_dbg[10]);+ h = " kfaults=";+ while (*h) *p++ = *h++;+ p = put_dec(p, kfaults);{static const char *const k[] = { " esr=0x", " far=0x", " pc=0x", " bad_esr=0x", " step=0x" };for (int j = 0; j < 5; j++) {diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patchindex ab1512b..7298b4a 100644--- a/xnu-patches/0001-linux64-stage-hook.patch+++ b/xnu-patches/0001-linux64-stage-hook.patch@@ -1638,7 +1638,7 @@ index d10d7a1..15e2fb4 100644cpc_cpu_transition(CPC_CPU_EARLY_INIT, cdp);#endif /* CONFIG_CPU_COUNTERS */diff --git a/osfmk/arm64/cswitch.s b/osfmk/arm64/cswitch.s-index a191eb3..78c2de4 100644+index a191eb3..062cd5f 100644--- a/osfmk/arm64/cswitch.s+++ b/osfmk/arm64/cswitch.s@@ -185,7 +185,8 @@@@ -1701,7 +1701,7 @@ index a191eb3..78c2de4 100644LOAD_KERN_STACK_TOP dst=x1, src=x0, tmp=x2 // Get the top of the kernel stackload_general_registers x1, 2set_process_dependent_keys_and_sync_context x0, x1, x2, x3, w4-@@ -444,3 +450,194 @@ LEXT(machine_set_current_thread)+@@ -444,3 +450,199 @@ LEXT(machine_set_current_thread)/* vim: set ts=4: */@@ -1733,11 +1733,6 @@ index a191eb3..78c2de4 100644+ str x18, [x9, #16]+ mrs x10, SCTLR_EL1+ str x10, [x9, #24]-+ cbz x1, 1f // x1: JOP off for this thread (arm64, not arm64e): EnIA (31) and EnDA (27) off at EL0-+ mov x11, #((1 << 31) | (1 << 27))-+ bic x10, x10, x11-+ msr SCTLR_EL1, x10-+1:+ adrp x10, EXT(linux64_el0_vectors)@page+ add x10, x10, EXT(linux64_el0_vectors)@pageoff+ msr VBAR_EL1, x10@@ -1775,6 +1770,16 @@ index a191eb3..78c2de4 100644+ ldr x11, [x11, _linux64_tpidr@PAGEOFF]+ LOAD_KERN_STACK_TOP dst=x12, src=x11, tmp=x13+ mov sp, x12++ /*++ * x1: JOP off for this thread (arm64, not arm64e): EnIA (31) and EnDA (27) off at EL0. Only now, after the last++ * kernel pointer authentication above (LOAD_KERN_STACK_TOP authenticates; with EnDA off it would trap to brk).++ */++ cbz x1, 1f++ mrs x10, SCTLR_EL1++ mov x11, #((1 << 31) | (1 << 27))++ bic x10, x10, x11++ msr SCTLR_EL1, x10++1:+ isb+ add x30, x0, SS64_X0+ ldp x2, x3, [x30, #16]