Skip to content

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

C

scripts: xnulinux-boot.sh boots the hosted system; the disk's UUIDs in mac.conf

Heyang Zhou842daaba5ebe
diff --git a/README.md b/README.md
index 89cccbd..9f21cf5 100644
--- a/README.md
+++ b/README.md
@@ -43,10 +43,10 @@ The host is an arm64 Linux VM with PAC on an Apple Silicon Mac (`scripts/krun-vm
1. `scripts/vm-xnu-build.sh` (once) sets up the macOS-DroidVM Linux toolchain and builds XNU natively on arm64.
2. `scripts/vm-xnu-rebuild.sh` applies `xnu-patches/`, rebuilds the kernel, builds `kexts/` and links the kernel collection.
3. `scripts/mk-macvm-disk.sh` (on the Mac) makes the system disk with Apple's installer and puts it in the VM.
-4. `scripts/vm-m2-kbisect.sh` builds `fki.ko` and `xnu.ko` and boots the hosted system; its console is
+4. `scripts/xnulinux-boot.sh` (in the VM) builds `fki.ko` and `xnu.ko`, brings up the network and, with a render node, the
+ GPU bridge (`xnudrmd`), and boots the hosted system from the disk and its `/var/tmp/mac.conf`; its console is
`scripts/xnu-console.sh`, its screen `tools/hosted/fb2png.py`.
-5. For the GPU: `gpu/xnu-drm/xnudrmd` runs as root once `/dev/xnu-hostcall` is there; `gpu/venus/build-darwin.sh` builds
- the hosted Vulkan driver.
+5. `gpu/venus/build-darwin.sh` (on the Mac) builds the hosted Vulkan driver that the Metal plugin uses on a real GPU.
Debugging: the VM console is captured on the host (`scripts/serial-pty.py`), a monitor thread on another CPU prints XNU's
counters every 2 s, and XNU dumps its threads with their saved frames after 20000 idle rounds.
diff --git a/docs/production.md b/docs/production.md
index c251a8d..831df14 100644
--- a/docs/production.md
+++ b/docs/production.md
@@ -26,13 +26,13 @@ What the PoC did to get each step running, and what replaces it. Ordered by what
| `mk-macvm-disk.sh` merges the OS cryptex into the sealed System volume (breaks the seal) | Graft the OS and App cryptexes at boot as macOS does (`graftdmg`), so the System volume stays sealed and an OS update replaces only the cryptex |
| `kc-amfi-platform.py` patches AMFI's expected platform version in the kernel collection | Build the collection from the same macOS build as the disk (one version source), no byte patching |
| `amfi_get_out_of_my_way=1`, `lp-sip0=0x802`, `kern.memorystatus_highwater_enabled=0` | Keep SIP/AMFI on; sign our plugin and kexts (ad-hoc trust cache of our own), only the unauthenticated-root bit stays |
-| `/chosen` UUIDs passed by hand (`root_uuid=`, `preboot_uuid=`) | Read from the disk's APFS container at load (xnu.ko or a host tool), no manual parameters |
+| `/chosen` UUIDs passed by hand (`root_uuid=`, `preboot_uuid=`) | Read from the disk's APFS container at load (xnu.ko or a host tool), no manual parameters (step one done: `mk-macvm-disk.sh` writes them to `mac.conf`, `xnulinux-boot.sh` reads it) |
## 4. Scripts and tests
| Now | Replace with |
|---|---|
-| ~30 scripts; the boot itself runs from `vm-m2-kbisect.sh` (`mode=3 stop=899 kmax=…`, a bisecting tool's knobs); several have session paths (`/Users/admin/...`), tty names, fixed `sleep`s | One driver (`xnulinux` CLI): `build`, `disk`, `boot`, `console`, `screenshot`, `test`; configuration in one file |
+| ~30 scripts; the boot itself ran from `vm-m2-kbisect.sh` (now `xnulinux-boot.sh`: builds, checks, retries, GPU bridge) (`mode=3 stop=899 kmax=…`, a bisecting tool's knobs); several have session paths (`/Users/admin/...`), tty names, fixed `sleep`s | One driver (`xnulinux` CLI): `build`, `disk`, `boot`, `console`, `screenshot`, `test`; configuration in one file |
| HFS+ route scripts (`mk-fulldisk.sh`, `vm-sysdisk-*`, `vm-mk-sysdisk.sh`, `vm-m2-*`, `run-poc.sh`) | Removed (done: APFS route only) |
| `tools/hosted/tests/t*.sh`, `af*.sh`: one-off probes (removed) | A test suite with expected results: boot to desktop, Safari/Chrome page load (screenshot compare), `xcodebuild` of a pinned project, Metal unit tests |
| No CI | CI: fki.ko/xnu.ko build against a pinned kernel, XNU + collection build (x86_64 Linux works with the DroidVM toolchain), GPU plugin host tests on macOS |
diff --git a/scripts/mk-macvm-disk.sh b/scripts/mk-macvm-disk.sh
index e814634..6bcca20 100755
--- a/scripts/mk-macvm-disk.sh
+++ b/scripts/mk-macvm-disk.sh
@@ -129,10 +129,17 @@ if [ "${MERGE_OS_CRYPTEX:-1}" = 1 ]; then
diskutil mount readOnly -mountPoint "$W/sys" "$SYS" >/dev/null
fi
sudo -n python3 scripts/extract-trustcache.py "$W/trustcache.bin" "$W/preboot"
+# what the boot needs to know of this disk (scripts/xnulinux-boot.sh): the system volume's APFS UUID (/chosen
+# root-matching) and its volume group's (boot-uuid; Preboot/<group> holds the boot files)
+{ echo "ROOT_UUID=$(diskutil info "$SYS" | awk -F': *' '/Volume UUID/{print $2; exit}')"
+ echo "BOOT_UUID=$(basename "$(dirname "$(dirname "$CX")")")"
+ grep -h -A1 ProductVersion "$W/sys/System/Library/CoreServices/SystemVersion.plist" | tr -d '\t\n' | sed 's/.*<string>\(.*\)<\/string>.*/MACOS=\1\n/'
+} > "$W/mac.conf"; cat "$W/mac.conf"
grep -h -A1 ProductVersion "$W/sys/System/Library/CoreServices/SystemVersion.plist" | tr -d '\t\n'; echo
cleanup; trap - EXIT
t0=$(date +%s)
$V "rm -f /var/tmp/apfs.img /var/tmp/apfs.cow"
tar -C "$W" -cSf - mac.img | $V "rm -f /var/tmp/mac.img; tar -C /var/tmp -xSf -; ls -ls /var/tmp/mac.img; df -h /var/tmp | tail -1"
+$V -put "$W/mac.conf" /var/tmp/mac.conf
$V -put "$W/trustcache.bin" /var/tmp/trustcache-mac.bin; $V "sudo mv /var/tmp/trustcache-mac.bin /lib/firmware/xnu/trustcache-mac.bin; ls -la /lib/firmware/xnu/trustcache-mac.bin"
echo "sent in $(( $(date +%s) - t0 ))s"
diff --git a/scripts/xnulinux-boot.sh b/scripts/xnulinux-boot.sh
new file mode 100755
index 0000000..fff16b2
--- /dev/null
+++ b/scripts/xnulinux-boot.sh
@@ -0,0 +1,58 @@
+#!/bin/bash
+# In the VM: boot the hosted macOS. Builds fki.ko and xnu.ko against the running kernel (stops on a compile error, never
+# boots stale modules), brings up the hosted system's network (xnu0) and GPU bridge (xnudrmd, when there is a render node),
+# and loads xnu.ko on the disk mk-macvm-disk.sh made. Returns once XNU is running; xnu.ko stays loaded until the hosted
+# system halts (or `sudo rmmod xnu`).
+#
+# Configuration: /var/tmp/mac.conf (written by mk-macvm-disk.sh: ROOT_UUID, BOOT_UUID), then the environment:
+# DISK (/var/tmp/mac.img) RAM_MB (7900) BOOTARGS CONSOLE_IN (/var/tmp/console-in) INPUT_IN (/var/tmp/xnu-input)
+# FB_DUMP (/var/tmp/xnu-fb.raw) KEYSTORE (/var/tmp/xnu-keystore.bin) TRUSTCACHE (xnu/trustcache-mac.bin)
+# IMAGE (xnu/kc.bin: the kernel collection under /lib/firmware) GPU (auto|off)
+set -euo pipefail
+cd "$(dirname "$0")/.."
+[ -f /var/tmp/mac.conf ] && . /var/tmp/mac.conf
+DISK=${DISK:-/var/tmp/mac.img}; RAM_MB=${RAM_MB:-7900}; GPU=${GPU:-auto}
+CONSOLE_IN=${CONSOLE_IN:-/var/tmp/console-in}; INPUT_IN=${INPUT_IN:-/var/tmp/xnu-input}; FB_DUMP=${FB_DUMP:-/var/tmp/xnu-fb.raw}
+KEYSTORE=${KEYSTORE:-/var/tmp/xnu-keystore.bin}; TRUSTCACHE=${TRUSTCACHE:-xnu/trustcache-mac.bin}; IMAGE=${IMAGE:-xnu/kc.bin}
+BOOTARGS=${BOOTARGS:-amfi_get_out_of_my_way=1 lp-sip0=2050}
+: "${ROOT_UUID:?no ROOT_UUID (/var/tmp/mac.conf from mk-macvm-disk.sh)}" "${BOOT_UUID:?no BOOT_UUID}"
+lsmod | grep -q '^xnu ' && { echo "xnu.ko is loaded: a hosted system is running (sudo rmmod xnu first)" >&2; exit 1; }
+
+K=/lib/modules/$(uname -r)/build
+build() { # a module; its compiler errors on failure
+ local log; log=$(make -s -C "$K" M="$PWD/$1" ${2:-} modules 2>&1) || { echo "$log" | grep -E "error|Error" | head -20 >&2; exit 1; }
+}
+build fki-ko
+build xnu-ko "KBUILD_EXTRA_SYMBOLS=$PWD/fki-ko/Module.symvers"
+sudo dmesg -n "${CONLEVEL:-8}"
+sudo systemctl stop xnudrmd 2>/dev/null || true # it holds fki's /dev/xnu-hostcall
+sudo rmmod fki 2>/dev/null || true
+sudo insmod fki-ko/fki.ko
+bash scripts/vm-net-up.sh
+
+# the hosted system's files on the host: console and input queues, the framebuffer xnu.ko writes, the keystore
+: > "$CONSOLE_IN"; : > "$INPUT_IN"; : > "$FB_DUMP"
+rm -f "$KEYSTORE"; truncate -s 1M "$KEYSTORE" # fresh each boot, as the disk was made (persisting it: production.md)
+# XNU's DRAM is one physically contiguous block (GBs, from CMA): free the page cache and compact first. Right after the VM
+# boots the allocation can still fail with the area free (pages not yet migratable): wait and try again.
+# mode=3: the kernel collection; the run lasts until the hosted system halts (no stage stop, no line or idle budget).
+# insmod returns when the run ends: it runs detached, its output in /var/tmp/xnu-insmod.log.
+for try in 1 2 3 4; 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
+ sudo setsid nohup insmod xnu-ko/xnu.ko mode=3 stop=899 kmax=1000000000 idle_max=2000000000 ram_mb="$RAM_MB" image="$IMAGE" \
+ trustcache="$TRUSTCACHE" disk="$DISK" root_uuid="$ROOT_UUID" boot_uuid="$BOOT_UUID" preboot_uuid="$BOOT_UUID" \
+ console_in="$CONSOLE_IN" input_in="$INPUT_IN" fb_dump="$FB_DUMP" keystore="$KEYSTORE" bootargs="\"$BOOTARGS\"" \
+ > /var/tmp/xnu-insmod.log 2>&1 < /dev/null &
+ sleep 5
+ sudo dmesg | grep -aq "contiguous allocation failed" || break
+ [ $try = 4 ] && { echo "no contiguous memory for XNU (CMA: $(grep CmaFree /proc/meminfo))" >&2; exit 1; }
+ echo "no contiguous memory for XNU yet: trying again in 20 s"; sleep 20
+done
+if [ "$GPU" != off ] && [ -e /dev/dri/renderD128 ]; then
+ make -s -C gpu/xnu-drm >/dev/null
+ sudo systemctl stop xnudrmd 2>/dev/null || true
+ sudo systemd-run -q --unit xnudrmd --collect "$PWD/gpu/xnu-drm/xnudrmd"
+ echo "gpu: xnudrmd on /dev/dri/renderD128 (journalctl -u xnudrmd)"
+fi
+echo "booting: console scripts/xnu-console.sh, screen tools/hosted/fb2png.py $FB_DUMP"