Skip to content

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

C

kprintf-budget bisect

Heyang Zhoubddbc5d4de82
diff --git a/scripts/vm-m2-kbisect.sh b/scripts/vm-m2-kbisect.sh
new file mode 100755
index 0000000..60c44a2
--- /dev/null
+++ b/scripts/vm-m2-kbisect.sh
@@ -0,0 +1,14 @@
+#!/bin/bash
+# Inside the VM: run M2 with stop=999 (no stage stop) and kmax=K XNU kprintf lines, for each K.
+cd ~/w/poc
+make -s -C /lib/modules/$(uname -r)/build M=$PWD/fki-ko modules >/dev/null 2>&1
+make -s -C /lib/modules/$(uname -r)/build M=$PWD/xnu-ko clean >/dev/null 2>&1
+make -s -C /lib/modules/$(uname -r)/build M=$PWD/xnu-ko KBUILD_EXTRA_SYMBOLS=$PWD/fki-ko/Module.symvers modules >/dev/null 2>&1
+sudo rmmod xnu 2>/dev/null; sudo rmmod fki 2>/dev/null; sudo insmod fki-ko/fki.ko
+for K in "$@"; do
+ sudo dmesg -C
+ echo ">>> kmax=$K"; sync
+ sudo insmod xnu-ko/xnu.ko mode=3 stop=899 kmax=$K ram_mb=${RAM_MB:-128}
+ sudo dmesg | grep -a "xnu\]\|XNU stage 4\|budget\|panic\|run ended" | tail -${SHOW:-4}
+ sudo rmmod xnu
+done
diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.c
index 6fdc866..b2e4331 100644
--- a/xnu-ko/xnu_main.c
+++ b/xnu-ko/xnu_main.c
@@ -23,6 +23,9 @@ static int stop = 199;
module_param(stop, int, 0444);
static int ram_mb = 256;
module_param(ram_mb, int, 0444);
+static int kmax; /* end the run after this many XNU kprintf lines (0: no limit) */
+module_param(kmax, int, 0444);
+static int kcount;
static struct xnu_image img;
static struct fki_region l2page;
@@ -124,6 +127,7 @@ long xnu_hook(unsigned long stage, unsigned long a1, unsigned long a2)
while (f && f[i] && j < sizeof(m) - 1) { if (f[i] != '\n') m[j++] = f[i]; i++; }
m[j] = 0;
logs(m);
+ if (kmax && ++kcount >= kmax) { logs("xnu: kprintf budget reached"); return 1; }
return 0;
}
if (stage == 210 && window_l1) { /* install XNU's kernel L1 entries (the kernel VA range only) */