Skip to content

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

C

Hosted macOS: IOSurface kept in the kernel collection (xnu-patches/kc-keep-extra.txt; actool needs it), fki_linux_runs restores the caller's DAIF (Linux leaves SError unmasked; XNU's idle loop asserted). xcodebuild builds exelban/Stats in the hosted macOS (226 compile commands, BUILD SUCCEEDED with only its GPU icon-render script phase removed: no Metal device)

Heyang Zhoua8e27a62727b
diff --git a/fki-ko/fki_mod.c b/fki-ko/fki_mod.c
index 31cb9ee..12f31bf 100644
--- a/fki-ko/fki_mod.c
+++ b/fki-ko/fki_mod.c
@@ -475,7 +475,21 @@ asm(
" .popsection\n");
void fki_run_on_stack(void (*fn)(void *), void *arg, void *top); /* x0 = fn, x1 = arg, x2 = stack top */
+static void fki_linux_runs_body(void (*fn)(void *), void *arg);
+
+/*
+ * Linux runs with SError (PSTATE.A) unmasked; the foreign kernel keeps D, A, I and F masked and asserts so in its
+ * idle loop. Give it back exactly the exception mask it called with.
+ */
static void fki_linux_runs(void (*fn)(void *), void *arg)
+{
+ unsigned long daif = read_sysreg(daif);
+
+ fki_linux_runs_body(fn, arg);
+ write_sysreg(daif, daif);
+}
+
+static void fki_linux_runs_body(void (*fn)(void *), void *arg)
{
unsigned long sp = current_stack_pointer;
int cpu = smp_processor_id();
diff --git a/scripts/vm-xnu-rebuild.sh b/scripts/vm-xnu-rebuild.sh
index a2113aa..2744450 100755
--- a/scripts/vm-xnu-rebuild.sh
+++ b/scripts/vm-xnu-rebuild.sh
@@ -12,6 +12,8 @@ done
for p in ~/w/poc/xnu-patches/*.patch; do git -C $T apply $p; echo "applied $(basename $p)"; done
JOBS=12 bash k27/build-kernel27.sh 2>&1 | tail -15
if [ -e out/kc/kc27.bin ]; then
+ # the hosted system's extra Apple kexts (in kc27, not kept by default): IOSurface for actool, CoreImage and friends
+ for id in $(grep -v '^#' ~/w/poc/xnu-patches/kc-keep-extra.txt); do grep -qx "$id" configs/kc-keep.txt || echo "$id" >> configs/kc-keep.txt; done
cp k27/segaddrs.sh /tmp/segaddrs.before
python3 k27/kc27-layout.py out/kc/kc27.bin out/k27/kernel-k27.macho --write k27/segaddrs.sh | tail -2
if ! cmp -s /tmp/segaddrs.before k27/segaddrs.sh; then
diff --git a/xnu-patches/kc-keep-extra.txt b/xnu-patches/kc-keep-extra.txt
new file mode 100644
index 0000000..fafe806
--- /dev/null
+++ b/xnu-patches/kc-keep-extra.txt
@@ -0,0 +1,2 @@
+# Apple kexts the hosted macOS needs beyond configs/kc-keep.txt (bundle IDs, one per line)
+com.apple.iokit.IOSurface