Skip to content

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

C

xnu.ko: install XNU's kernel L1 entries from a copy of its table

Heyang Zhou8f343317bab8
diff --git a/scripts/xnulinux-boot.sh b/scripts/xnulinux-boot.sh
index 00568a4..3ec088c 100755
--- a/scripts/xnulinux-boot.sh
+++ b/scripts/xnulinux-boot.sh
@@ -104,6 +104,7 @@ if [ "$GPU" != off ] && [ -e /dev/dri/renderD128 ]; then
fi
# mode=3: the kernel collection; the run lasts until the hosted system halts.
# insmod returns when the run ends: it runs detached, its output in /var/tmp/xnu-insmod.log.
+START=$(date +%s) # the run's log (--wait) starts here, so a failure while xnu.ko loads is in it
for try in 1 2 3 4 5 6 7 8 9; do
sync; echo 3 | sudo tee /proc/sys/vm/drop_caches >/dev/null; echo 1 | sudo tee /proc/sys/vm/compact_memory >/dev/null
sudo dmesg -C
@@ -130,7 +131,6 @@ echo "booting: console scripts/xnu-console.sh, screen tools/hosted/fb2png.py $FB
# --wait: until the run ends (insmod returns), then keep its kernel log and say how it ended: 0 a halt or a stop asked for,
# 2 an XNU panic, 3 the run ended otherwise
-START=$(date +%s)
collect() { # the run's kernel log, kept; the status says how it ended
local LOGDIR=/var/log/xnulinux LOG
# insmod has returned but xnu.ko stays loaded after a run that ended by itself (a panic, a halt): unload it, or the
diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.c
index 0c62269..1ba1462 100644
--- a/xnu-ko/xnu_main.c
+++ b/xnu-ko/xnu_main.c
@@ -583,7 +583,14 @@ static long xnu_hook_body(unsigned long call, unsigned long a1, unsigned long a2
return 0;
}
if (call == LINUX64_CALL_KERNEL_TABLES && window_l1) { /* install XNU's kernel L1 entries (the kernel VA range only) */
- const unsigned long long *tte = (const unsigned long long *)a1;
+ /*
+ * XNU's L1 table (cpu_tte) is read through the bootstrap window, and installing XNU's entry for the window's
+ * own slot replaces that window: XNU's tables need not map cpu_tte at this stage (L3 0 here), so the rest of
+ * the table is read from a copy taken first, never through the slot being replaced (it faulted in Linux's
+ * context, an oops, whenever the TLB no longer held the bootstrap entry).
+ */
+ static unsigned long long tte[512];
+ x_memcpy(tte, (const void *)a1, sizeof(tte));
walk_log("xnu: XNU table walk for arm_init (text)", diag_va[0], tte);
walk_log("xnu: XNU table walk for the Mach-O header", diag_va[1], tte);
walk_log("xnu: XNU table walk for boot args", diag_va[2], tte);