Skip to content

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

C

M2a: world switch, ADT and boot args, arm_init stage hook (WIP)

Heyang Zhou9e1c1f75ea71
diff --git a/fki-ko/fki.h b/fki-ko/fki.h
index d4243c9..217a6ad 100644
--- a/fki-ko/fki.h
+++ b/fki-ko/fki.h
@@ -47,6 +47,8 @@ void fki_blob_put(void *handle);
/* Enter and leave a foreign context on this CPU: no preemption, no interrupts, foreign PAC keys. */
unsigned long fki_enter(const struct fki_pac_keys *foreign, struct fki_pac_keys *saved);
void fki_leave(unsigned long cookie, const struct fki_pac_keys *saved);
+/* Swap PAC keys only (caller already in a foreign context): save current into *save, load *load. */
+void fki_pac_swap(const struct fki_pac_keys *load, struct fki_pac_keys *save);
fki_u64 fki_counter_frequency(void);
fki_u64 fki_counter(void);
void fki_random(void *buf, unsigned long len);
diff --git a/fki-ko/fki_mod.c b/fki-ko/fki_mod.c
index fb46b0a..82b1555 100644
--- a/fki-ko/fki_mod.c
+++ b/fki-ko/fki_mod.c
@@ -156,6 +156,13 @@ __attribute__((target("branch-protection=none"))) void fki_leave(unsigned long c
}
EXPORT_SYMBOL(fki_leave);
+__attribute__((target("branch-protection=none"))) void fki_pac_swap(const struct fki_pac_keys *load, struct fki_pac_keys *save)
+{
+ keys_read(save);
+ keys_write(load);
+}
+EXPORT_SYMBOL(fki_pac_swap);
+
fki_u64 fki_counter_frequency(void)
{
return arch_timer_get_cntfrq();
diff --git a/linux/drivers/fki/fki.c b/linux/drivers/fki/fki.c
index 3b4f3e4..49444b5 100644
--- a/linux/drivers/fki/fki.c
+++ b/linux/drivers/fki/fki.c
@@ -134,6 +134,13 @@ void fki_leave(unsigned long cookie, const struct fki_pac_keys *saved)
}
EXPORT_SYMBOL(fki_leave);
+void fki_pac_swap(const struct fki_pac_keys *load, struct fki_pac_keys *save)
+{
+ keys_read(save);
+ keys_write(load);
+}
+EXPORT_SYMBOL(fki_pac_swap);
+
fki_u64 fki_counter_frequency(void)
{
return arch_timer_get_cntfrq();
diff --git a/linux/include/linux/fki.h b/linux/include/linux/fki.h
index d4243c9..217a6ad 100644
--- a/linux/include/linux/fki.h
+++ b/linux/include/linux/fki.h
@@ -47,6 +47,8 @@ void fki_blob_put(void *handle);
/* Enter and leave a foreign context on this CPU: no preemption, no interrupts, foreign PAC keys. */
unsigned long fki_enter(const struct fki_pac_keys *foreign, struct fki_pac_keys *saved);
void fki_leave(unsigned long cookie, const struct fki_pac_keys *saved);
+/* Swap PAC keys only (caller already in a foreign context): save current into *save, load *load. */
+void fki_pac_swap(const struct fki_pac_keys *load, struct fki_pac_keys *save);
fki_u64 fki_counter_frequency(void);
fki_u64 fki_counter(void);
void fki_random(void *buf, unsigned long len);
diff --git a/scripts/vm-m2.sh b/scripts/vm-m2.sh
new file mode 100755
index 0000000..157c1fb
--- /dev/null
+++ b/scripts/vm-m2.sh
@@ -0,0 +1,11 @@
+#!/bin/bash
+# Inside the VM: build fki.ko and xnu.ko, run M2a (XNU's arm_init in the XNU world, stages reported through FKI).
+set -x
+cd ~/w/poc
+make -s -C /lib/modules/$(uname -r)/build M=$PWD/fki-ko modules 2>&1 | grep -v "compiler differs\|built by\|You are using\|pahole\|BTF\|built with" || true
+make -s -C /lib/modules/$(uname -r)/build M=$PWD/xnu-ko KBUILD_EXTRA_SYMBOLS=$PWD/fki-ko/Module.symvers modules 2>&1 | grep -v "compiler differs\|built by\|You are using\|pahole\|BTF\|built with" || true
+ls xnu-ko/xnu.ko fki-ko/fki.ko || exit 1
+sudo dmesg -C
+sudo rmmod xnu 2>/dev/null; sudo rmmod fki 2>/dev/null
+sudo insmod fki-ko/fki.ko && sudo insmod xnu-ko/xnu.ko mode=2 stop=${STOP:-199}; echo "insmod rc=$?"
+sudo dmesg | grep -v "^\[.*\] *\(x[0-9]\|Modules\)" | head -80
diff --git a/scripts/vm-xnu-rebuild.sh b/scripts/vm-xnu-rebuild.sh
new file mode 100755
index 0000000..677e6ac
--- /dev/null
+++ b/scripts/vm-xnu-rebuild.sh
@@ -0,0 +1,12 @@
+#!/bin/bash
+# Inside the VM: apply xnulinux's XNU patches to the k27 tree and rebuild the kernel incrementally.
+set -e
+cd ~/w/dvm
+T=out/k27/xnu
+for p in ~/w/poc/xnu-patches/*.patch; do
+ for f in $(grep '^+++ b/' $p | sed 's|^+++ b/||'); do git -C $T checkout -q -- $f; done
+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
+ls -la out/k27/kernel-k27.macho
+sudo cp out/k27/kernel-k27.macho /lib/firmware/xnu/kernel.macho
diff --git a/xnu-ko/Makefile b/xnu-ko/Makefile
index 7880efa..f521bc6 100644
--- a/xnu-ko/Makefile
+++ b/xnu-ko/Makefile
@@ -1,4 +1,4 @@
-# xnu.ko: the APSL side. Out-of-tree, non-GPL module that only uses <linux/fki.h>.
+# xnu.ko: the APSL side. Out-of-tree, non-GPL module that only uses fki.h.
obj-m += xnu.o
-xnu-y := xnu_main.o macho.o
+xnu-y := xnu_main.o macho.o adt.o world.o
ccflags-y += -Wno-declaration-after-statement
diff --git a/xnu-ko/adt.c b/xnu-ko/adt.c
new file mode 100644
index 0000000..08d0bd7
--- /dev/null
+++ b/xnu-ko/adt.c
@@ -0,0 +1,37 @@
+// ADT: node = {u32 nProperties, u32 nChildren}, then properties
+// {char name[32]; u32 length; value padded to 4}, then children.
+#include "xrt.h"
+#include "adt.h"
+
+void adt_init(struct adt *a, void *buf, unsigned long cap)
+{
+ a->buf = a->p = buf; a->end = a->buf + cap; a->err = 0;
+}
+
+static void put(struct adt *a, const void *v, unsigned long n)
+{
+ if (a->p + n > a->end) { a->err = 1; return; }
+ x_memcpy(a->p, v, n); a->p += n;
+}
+
+void adt_node(struct adt *a, unsigned nprops, unsigned nchildren)
+{
+ unsigned h[2] = { nprops, nchildren };
+ put(a, h, sizeof(h));
+}
+
+void adt_prop(struct adt *a, const char *name, const void *val, unsigned len)
+{
+ char n[32] = { 0 };
+ unsigned long l = x_strlen(name);
+ static const unsigned char zero[4];
+ x_memcpy(n, name, l < 31 ? l : 31);
+ put(a, n, 32);
+ put(a, &len, 4);
+ put(a, val, len);
+ put(a, zero, (4 - (len & 3)) & 3);
+}
+
+void adt_prop_str(struct adt *a, const char *name, const char *s) { adt_prop(a, name, s, x_strlen(s) + 1); }
+void adt_prop_u32(struct adt *a, const char *name, unsigned v) { adt_prop(a, name, &v, 4); }
+void adt_prop_u64(struct adt *a, const char *name, unsigned long long v) { adt_prop(a, name, &v, 8); }
diff --git a/xnu-ko/adt.h b/xnu-ko/adt.h
new file mode 100644
index 0000000..f878c9e
--- /dev/null
+++ b/xnu-ko/adt.h
@@ -0,0 +1,10 @@
+/* Apple device tree (ADT) writer, written from the documented flattened format. */
+#pragma once
+struct adt { unsigned char *buf, *p, *end; int err; };
+void adt_init(struct adt *a, void *buf, unsigned long cap);
+void adt_node(struct adt *a, unsigned nprops, unsigned nchildren);
+void adt_prop(struct adt *a, const char *name, const void *val, unsigned len);
+void adt_prop_str(struct adt *a, const char *name, const char *s);
+void adt_prop_u32(struct adt *a, const char *name, unsigned v);
+void adt_prop_u64(struct adt *a, const char *name, unsigned long long v);
+static inline unsigned long adt_size(struct adt *a) { return a->p - a->buf; }
diff --git a/xnu-ko/macho.c b/xnu-ko/macho.c
index 62dd818..37f2d35 100644
--- a/xnu-ko/macho.c
+++ b/xnu-ko/macho.c
@@ -8,7 +8,7 @@
for (const struct lc *l = (const void *)((img)->file + sizeof(struct mh64)), *_e = (const void *)((img)->file + sizeof(struct mh64) + ((const struct mh64 *)(img)->file)->sizeofcmds); \
l < _e; l = (const void *)((const char *)l + l->cmdsize))
-int macho_load(struct xnu_image *img, const void *file, unsigned long size)
+int macho_load(struct xnu_image *img, const void *file, unsigned long size, unsigned long extra)
{
const struct mh64 *mh = file;
int err;
@@ -40,7 +40,7 @@ int macho_load(struct xnu_image *img, const void *file, unsigned long size)
}
if (img->vmhigh <= img->vmlow)
return -ENOEXEC;
- err = fki_region_alloc(img->vmhigh - img->vmlow, &img->region);
+ err = fki_region_alloc(img->vmhigh - img->vmlow + extra, &img->region);
if (err)
return err;
img->slide = (u64_t)img->region.va - img->vmlow;
diff --git a/xnu-ko/macho.h b/xnu-ko/macho.h
index c452bff..e4777be 100644
--- a/xnu-ko/macho.h
+++ b/xnu-ko/macho.h
@@ -26,6 +26,6 @@ struct xnu_image {
u64_t entry; /* unslid */
};
-int macho_load(struct xnu_image *img, const void *file, unsigned long size);
+int macho_load(struct xnu_image *img, const void *file, unsigned long size, unsigned long extra);
int macho_rebase(struct xnu_image *img);
u64_t macho_sym(struct xnu_image *img, const char *name); /* runtime address or 0 */
diff --git a/xnu-ko/world.S b/xnu-ko/world.S
new file mode 100644
index 0000000..f8fa09b
--- /dev/null
+++ b/xnu-ko/world.S
@@ -0,0 +1,103 @@
+/* World switch, see world.h. No PAC instructions here: keys change under our feet. */
+#define W(reg) adrp reg, xnu_W; add reg, reg, :lo12:xnu_W
+
+ .text
+ .globl xnu_world_call
+ .type xnu_world_call, %function
+xnu_world_call:
+ W(x9)
+ stp x19, x20, [x9, #0]
+ stp x21, x22, [x9, #16]
+ stp x23, x24, [x9, #32]
+ stp x25, x26, [x9, #48]
+ stp x27, x28, [x9, #64]
+ stp x29, x30, [x9, #80]
+ mov x10, sp
+ str x10, [x9, #96]
+ mov x19, x0
+ mov x20, x1
+ add x0, x9, #328 /* load XNU keys */
+ add x1, x9, #240 /* save Linux keys */
+ bl fki_enter /* no preemption, no IRQs */
+ W(x9)
+ str x0, [x9, #104]
+ mrs x10, tpidr_el1
+ str x10, [x9, #216]
+ ldr x10, [x9, #224]
+ msr tpidr_el1, x10
+ ldr x10, [x9, #232]
+ mov sp, x10
+ mov x0, x20
+ blr x19
+ /* fn returned: XNU keys and thread pointer are live */
+ W(x9)
+ mrs x10, tpidr_el1
+ str x10, [x9, #224]
+ ldr x10, [x9, #216]
+ msr tpidr_el1, x10
+ mov x19, #0
+ b 2f
+xnu_world_abort: /* from the upcall: Linux keys and TPIDR are live */
+ W(x9)
+ mov x19, #1
+2: ldr x10, [x9, #96]
+ mov sp, x10
+ ldr x0, [x9, #104]
+ add x1, x9, #240
+ bl fki_leave /* Linux keys, IRQs, preemption back */
+ mov x0, x19
+ W(x9)
+ ldp x19, x20, [x9, #0]
+ ldp x21, x22, [x9, #16]
+ ldp x23, x24, [x9, #32]
+ ldp x25, x26, [x9, #48]
+ ldp x27, x28, [x9, #64]
+ ldp x29, x30, [x9, #80]
+ ret
+ .size xnu_world_call, . - xnu_world_call
+
+ .globl xnu_world_upcall
+ .type xnu_world_upcall, %function
+xnu_world_upcall: /* x0 = stage, called by XNU with blr */
+ hint #34 /* bti c: module text may be guarded */
+ W(x9)
+ stp x19, x20, [x9, #112]
+ stp x21, x22, [x9, #128]
+ stp x23, x24, [x9, #144]
+ stp x25, x26, [x9, #160]
+ stp x27, x28, [x9, #176]
+ stp x29, x30, [x9, #192]
+ mov x10, sp
+ str x10, [x9, #208]
+ mov x19, x0
+ mov x20, x1
+ mov x21, x2
+ mrs x10, tpidr_el1
+ str x10, [x9, #224]
+ ldr x10, [x9, #216]
+ msr tpidr_el1, x10
+ add x0, x9, #240 /* load Linux keys */
+ add x1, x9, #328 /* save XNU keys */
+ bl fki_pac_swap
+ mov x0, x19
+ mov x1, x20
+ mov x2, x21
+ bl xnu_hook
+ cbnz x0, xnu_world_abort
+ W(x9)
+ add x0, x9, #328 /* load XNU keys */
+ add x1, x9, #240 /* save Linux keys */
+ bl fki_pac_swap
+ W(x9)
+ ldr x10, [x9, #224]
+ msr tpidr_el1, x10
+ ldr x10, [x9, #208]
+ mov sp, x10
+ ldp x19, x20, [x9, #112]
+ ldp x21, x22, [x9, #128]
+ ldp x23, x24, [x9, #144]
+ ldp x25, x26, [x9, #160]
+ ldp x27, x28, [x9, #176]
+ ldp x29, x30, [x9, #192]
+ ret
+ .size xnu_world_upcall, . - xnu_world_upcall
diff --git a/xnu-ko/world.h b/xnu-ko/world.h
new file mode 100644
index 0000000..6e1a2d2
--- /dev/null
+++ b/xnu-ko/world.h
@@ -0,0 +1,21 @@
+/* World switch between Linux and the hosted XNU (see world.S for the layout). */
+#pragma once
+#include "../fki-ko/fki.h"
+struct xnu_world {
+ unsigned long long linux_regs[14]; /* 0: x19..x28, x29, x30, sp, irq cookie */
+ unsigned long long xnu_regs[13]; /* 112: x19..x28, x29, x30, sp */
+ unsigned long long linux_tpidr; /* 216 */
+ unsigned long long xnu_tpidr; /* 224 */
+ unsigned long long xnu_stack_top; /* 232 */
+ struct fki_pac_keys linux_keys; /* 240 */
+ struct fki_pac_keys xnu_keys; /* 328 */
+};
+_Static_assert(sizeof(struct fki_pac_keys) == 88, "fki_pac_keys layout");
+_Static_assert(__builtin_offsetof(struct xnu_world, xnu_keys) == 328, "xnu_world layout");
+extern struct xnu_world xnu_W;
+/* Run fn(arg) in the XNU world. 0: fn returned; 1: the hook ended the run. */
+long xnu_world_call(unsigned long fn, unsigned long arg);
+/* The address XNU calls (linux64_hook); it calls xnu_hook(stage) in the Linux world. */
+void xnu_world_upcall(void);
+/* Linux-world handler: return 0 to resume XNU, nonzero to end the run. */
+long xnu_hook(unsigned long stage, unsigned long a1, unsigned long a2);
diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.c
index 24aee1e..9afd20d 100644
--- a/xnu-ko/xnu_main.c
+++ b/xnu-ko/xnu_main.c
@@ -1,84 +1,205 @@
// SPDX-License-Identifier: APSL-2.0
/*
* xnu.ko: hosts an XNU kernel image inside Linux. Talks to Linux only
- * through <linux/fki.h>; <linux/module.h> provides the module ABI.
+ * through FKI (fki.h); <linux/module.h> provides the module ABI.
*
- * PoC milestone M1: load the source-built XNU Mach-O, slide and PAC-sign it
- * with XNU's own kernel keys, and run XNU code inside the Linux kernel.
+ * mode=1 (M1): load, slide and sign the image, call two XNU functions.
+ * mode=2 (M2a, default): hand XNU boot args and an ADT and run arm_init in
+ * the XNU world; XNU reports stages through linux64_hook, and the run
+ * ends at stage `stop`.
*/
#include <linux/module.h>
#include "../fki-ko/fki.h"
#include "xrt.h"
#include "macho.h"
+#include "adt.h"
+#include "world.h"
static char *image = "xnu/kernel.macho";
module_param(image, charp, 0444);
+static int mode = 2;
+module_param(mode, int, 0444);
+static int stop = 199;
+module_param(stop, int, 0444);
+static int ram_mb = 256;
+module_param(ram_mb, int, 0444);
static struct xnu_image img;
static void *blob;
-static struct fki_pac_keys xnu_keys;
+struct xnu_world xnu_W;
static void logs(const char *s) { fki_log(s, x_strlen(s)); }
-
-static char *fmt_hex(char *p, u64_t v)
-{
- *p++ = '0'; *p++ = 'x';
- for (int i = 60; i >= 0; i -= 4) *p++ = "0123456789abcdef"[(v >> i) & 15];
- *p = 0;
- return p;
-}
-
static void logkv(const char *k, u64_t v)
{
char buf[96]; char *p = buf;
while (*k) *p++ = *k++;
- *p++ = ' ';
- fmt_hex(p, v);
+ *p++ = ' '; *p++ = '0'; *p++ = 'x';
+ for (int i = 60; i >= 0; i -= 4) *p++ = "0123456789abcdef"[(v >> i) & 15];
+ *p = 0;
logs(buf);
}
+/* ---- M1 ---- */
typedef unsigned long (*strlen_fn)(const char *);
typedef unsigned int (*crc32_fn)(unsigned int, const void *, unsigned long);
+static int run_m1(void)
+{
+ struct fki_pac_keys saved; unsigned long c;
+ c = fki_enter(&xnu_W.xnu_keys, &saved);
+ int n = macho_rebase(&img);
+ fki_leave(c, &saved);
+ logkv("xnu: threaded fixups applied", (u64_t)n);
+ if (n < 0) return n;
+ u64_t v = macho_sym(&img, "_version"), sl = macho_sym(&img, "_strlen"), cr = macho_sym(&img, "_crc32");
+ if (!v || !sl || !cr) { logs("xnu: symbols missing"); return 0; }
+ c = fki_enter(&xnu_W.xnu_keys, &saved);
+ unsigned long len = ((strlen_fn)sl)((const char *)v);
+ unsigned int crc = ((crc32_fn)cr)(0, (const void *)v, len);
+ fki_leave(c, &saved);
+ logs("xnu: XNU code ran inside Linux; XNU's version string:");
+ fki_log((const char *)v, len);
+ logkv("xnu: strlen(version) computed by XNU", len);
+ logkv("xnu: crc32(version) computed by XNU", crc);
+ return 0;
+}
+
+/* ---- M2a ---- */
+#define XNU_FAKE_PHYS 0x800000000ULL /* XNU's view of the region's first byte */
+#define BOOT_LINE 1024
+
+struct boot_args_v2 { /* pexpert/arm64/boot.h layout */
+ unsigned short Revision, Version;
+ u64_t virtBase, physBase, memSize, topOfKernelData;
+ struct { u64_t v_baseAddr, v_display, v_rowBytes, v_width, v_height, v_depth; } Video;
+ unsigned int machineType;
+ void *deviceTreeP;
+ unsigned int deviceTreeLength;
+ char CommandLine[BOOT_LINE];
+ u64_t bootFlags, memSizeActual;
+};
+
+static const char *cmdline = "debug=0x8 -v serial=0 startup_debug=1 vm_compression_limit=1572864";
+
+long xnu_hook(unsigned long stage, unsigned long a1, unsigned long a2)
+{
+ if (stage == 999) {
+ char m[200]; const char *f = (const char *)a1; unsigned i = 0;
+ while (f && f[i] && i < sizeof(m) - 1) { m[i] = f[i]; i++; }
+ m[i] = 0;
+ logs("xnu: XNU panic, format string:");
+ logs(m);
+ logkv("xnu: panic caller", a2);
+ return 1;
+ }
+ static const struct { unsigned long s; const char *what; } names[] = {
+ { 101, "image slid, structors signed" }, { 110, "PE_init_platform: ADT parsed" },
+ { 120, "ml_parse_cpu_topology" }, { 130, "kernel_startup_bootstrap returned" },
+ { 199, "arm_init reached arm_vm_init" },
+ };
+ const char *what = "?";
+ for (unsigned i = 0; i < sizeof(names) / sizeof(names[0]); i++)
+ if (names[i].s == stage) what = names[i].what;
+ char buf[96]; char *p = buf; const char *q = "xnu: XNU stage ";
+ while (*q) *p++ = *q++;
+ p[0] = '0' + stage / 100; p[1] = '0' + stage / 10 % 10; p[2] = '0' + stage % 10; p += 3;
+ *p++ = ':'; *p++ = ' ';
+ while (*what && p < buf + 94) *p++ = *what++;
+ *p = 0;
+ logs(buf);
+ return (long)stage >= stop;
+}
+
+static int run_m2a(void)
+{
+ u64_t image_size = img.vmhigh - img.vmlow;
+ unsigned char *base = img.region.va;
+ unsigned long region = img.region.size;
+ /* region: [image][boot args][ADT][free "DRAM" ...] */
+ u64_t off_args = (image_size + 0x3fff) & ~0x3fffULL;
+ struct boot_args_v2 *ba = (void *)(base + off_args);
+ u64_t off_dt = off_args + 0x4000;
+ struct adt a;
+ static unsigned char seed[256];
+
+ fki_random(seed, sizeof(seed));
+ adt_init(&a, base + off_dt, 0x10000);
+ adt_node(&a, 3, 2); /* / */
+ adt_prop_str(&a, "name", "device-tree");
+ adt_prop_str(&a, "compatible", "xnulinux,linux64");
+ adt_prop_str(&a, "model", "xnulinux");
+ adt_node(&a, 6, 0); /* /chosen */
+ adt_prop_str(&a, "name", "chosen");
+ adt_prop_u64(&a, "dram-base", XNU_FAKE_PHYS);
+ adt_prop_u64(&a, "dram-size", region);
+ adt_prop(&a, "random-seed", seed, sizeof(seed));
+ adt_prop_str(&a, "firmware-version", "xnulinux-m2a");
+ adt_prop_u32(&a, "debug-enabled", 1);
+ adt_node(&a, 1, 1); /* /cpus */
+ adt_prop_str(&a, "name", "cpus");
+ adt_node(&a, 5, 0); /* /cpus/cpu0 */
+ adt_prop_str(&a, "name", "cpu0");
+ adt_prop_str(&a, "device_type", "cpu");
+ adt_prop_str(&a, "state", "running");
+ adt_prop_u32(&a, "reg", 0);
+ adt_prop_u64(&a, "timebase-frequency", fki_counter_frequency());
+ if (a.err) { logs("xnu: ADT overflow"); return -12; }
+ u64_t dt_len = adt_size(&a);
+
+ ba->Revision = 2; ba->Version = 2;
+ ba->virtBase = (u64_t)base;
+ ba->physBase = XNU_FAKE_PHYS;
+ ba->memSize = region;
+ ba->memSizeActual = region;
+ ba->topOfKernelData = XNU_FAKE_PHYS + ((off_dt + dt_len + 0x3fff) & ~0x3fffULL);
+ ba->deviceTreeP = base + off_dt;
+ ba->deviceTreeLength = (unsigned)dt_len;
+ x_memcpy(ba->CommandLine, cmdline, x_strlen(cmdline) + 1);
+ logkv("xnu: boot args at", (u64_t)ba);
+ logkv("xnu: ADT bytes", dt_len);
+ logkv("xnu: topOfKernelData (XNU phys)", ba->topOfKernelData);
+
+ /* Slide and sign here (XNU skips its own rebase when hosted). */
+ struct fki_pac_keys saved; unsigned long c = fki_enter(&xnu_W.xnu_keys, &saved);
+ int n = macho_rebase(&img);
+ fki_leave(c, &saved);
+ logkv("xnu: threaded fixups applied", (u64_t)n);
+ if (n < 0) return n;
+
+ u64_t a_init = macho_sym(&img, "_arm_init"), a_hook = macho_sym(&img, "_linux64_hook");
+ logkv("xnu: arm_init at", a_init);
+ if (!a_init || !a_hook) { logs("xnu: arm_init or linux64_hook missing (kernel without the LINUX64 patch?)"); return -2; }
+ *(u64_t *)a_hook = (u64_t)xnu_world_upcall;
+
+ /* XNU's boot stack: the last 64 KiB of the region */
+ xnu_W.xnu_stack_top = (u64_t)(base + region - 64);
+ xnu_W.xnu_tpidr = 0;
+ logs("xnu: entering arm_init");
+ long r = xnu_world_call(a_init, (unsigned long)ba);
+ logkv(r ? "xnu: run ended by stage hook, r =" : "xnu: arm_init returned (unexpected), r =", (u64_t)r);
+ return 0;
+}
+
static int __init xnu_init(void)
{
- const void *data; unsigned long size; unsigned long c; struct fki_pac_keys saved;
+ const void *data; unsigned long size;
int err;
if (fki_version() != 0)
return -22;
err = fki_blob_get(image, &data, &size, &blob);
if (err) { logkv("xnu: cannot read image, err", (u64_t)err); return err; }
- err = macho_load(&img, data, size);
- if (err) { logkv("xnu: load failed", (u64_t)err); fki_blob_put(blob); return err; }
+ err = macho_load(&img, data, size, mode == 2 ? (unsigned long)ram_mb << 20 : 0);
+ if (err) { logkv("xnu: load failed", (u64_t)err); fki_blob_put(blob); blob = 0; return err; }
logkv("xnu: image vmlow", img.vmlow);
logkv("xnu: image size", img.vmhigh - img.vmlow);
- logkv("xnu: loaded at", (u64_t)img.region.va);
- logkv("xnu: phys", img.region.pa);
- logkv("xnu: entry (unslid)", img.entry);
+ logkv("xnu: region at", (u64_t)img.region.va);
+ logkv("xnu: region size", img.region.size);
- fki_random(&xnu_keys, sizeof(xnu_keys));
- xnu_keys.sctlr_en = ~0ULL; /* all four address keys on */
+ fki_random(&xnu_W.xnu_keys, sizeof(xnu_W.xnu_keys));
+ xnu_W.xnu_keys.sctlr_en = ~0ULL;
- c = fki_enter(&xnu_keys, &saved);
- int n = macho_rebase(&img);
- fki_leave(c, &saved);
- logkv("xnu: threaded fixups applied", (u64_t)n);
- if (n < 0)
- return n;
-
- u64_t a_version = macho_sym(&img, "_version"), a_strlen = macho_sym(&img, "_strlen"), a_crc = macho_sym(&img, "_crc32");
- logkv("xnu: _version at", a_version);
- if (!a_version || !a_strlen || !a_crc) { logs("xnu: symbols missing"); return 0; }
- c = fki_enter(&xnu_keys, &saved);
- unsigned long len = ((strlen_fn)a_strlen)((const char *)a_version);
- unsigned int crc = ((crc32_fn)a_crc)(0, (const void *)a_version, len);
- fki_leave(c, &saved);
- logs("xnu: XNU code ran inside Linux; XNU's version string:");
- fki_log((const char *)a_version, len);
- logkv("xnu: strlen(version) computed by XNU", len);
- logkv("xnu: crc32(version) computed by XNU", crc);
- return 0;
+ return mode == 1 ? run_m1() : run_m2a();
}
static void __exit xnu_exit(void)
@@ -89,5 +210,5 @@ static void __exit xnu_exit(void)
module_init(xnu_init);
module_exit(xnu_exit);
-MODULE_LICENSE("Proprietary"); /* APSL-2.0: not GPL-compatible; taints the kernel like ZFS */
+MODULE_LICENSE("Proprietary"); /* APSL-2.0 side: not GPL-compatible; taints the kernel like ZFS */
MODULE_DESCRIPTION("XNU personality (PoC)");
diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patch
new file mode 100644
index 0000000..cb79cc4
--- /dev/null
+++ b/xnu-patches/0001-linux64-stage-hook.patch
@@ -0,0 +1,125 @@
+diff --git a/osfmk/arm/arm_init.c b/osfmk/arm/arm_init.c
+index ac1713e..ade9738 100644
+--- a/osfmk/arm/arm_init.c
++++ b/osfmk/arm/arm_init.c
+@@ -228,6 +228,9 @@ extern vm_offset_t segLOWESTKC, segHIGHESTKC, segLOWESTROKC, segHIGHESTROKC;
+ extern vm_offset_t segLOWESTAuxKC, segHIGHESTAuxKC, segLOWESTROAuxKC, segHIGHESTROAuxKC;
+ extern vm_offset_t segLOWESTRXAuxKC, segHIGHESTRXAuxKC, segHIGHESTNLEAuxKC;
+
++/* LINUX64 (xnulinux): set by xnu.ko; see linux64_stage(). */
++uintptr_t linux64_hook;
++
+ static void
+ arm_slide_rebase_and_sign_image(void)
+ {
+@@ -285,10 +288,15 @@ arm_slide_rebase_and_sign_image(void)
+ * using hardcoded kernel link address
+ */
+ slide = (uintptr_t)k_mh - VM_KERNEL_LINK_ADDRESS;
++ if (linux64_hook) {
++ /* LINUX64: xnu.ko loaded, slid and signed the image already. */
++ seg = (kernel_segment_command_t *)((uintptr_t)k_mh + sizeof(*k_mh));
++ slide = (uintptr_t)k_mh - seg->vmaddr;
++ }
+
+ /* rebase and sign jops */
+ static_kernelcache = &__thread_starts_sect_end[0] != &__thread_starts_sect_start[0];
+- if (static_kernelcache) {
++ if (static_kernelcache && !linux64_hook) {
+ rebase_threaded_starts( &__thread_starts_sect_start[0],
+ &__thread_starts_sect_end[0],
+ (uintptr_t)k_mh, (uintptr_t)k_mh - slide, slide);
+@@ -341,6 +349,27 @@ arm_auxkc_init(void *mh, void *base)
+ #endif /* defined(HAS_APPLE_PAC) */
+ }
+
++/*
++ * LINUX64 (xnulinux): when XNU is hosted inside Linux by xnu.ko, xnu.ko sets
++ * linux64_hook before calling arm_init. Each stage calls into the host, which
++ * may resume XNU or end the run. The call is a plain blr: the hook is host
++ * code, not an XNU function pointer.
++ */
++extern uintptr_t linux64_hook;
++
++static inline void
++linux64_stage(unsigned long stage)
++{
++ if (linux64_hook) {
++ register unsigned long x0 __asm__("x0") = stage;
++ __asm__ volatile ("blr %1"
++ : "+r"(x0)
++ : "r"(linux64_hook)
++ : "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11",
++ "x12", "x13", "x14", "x15", "x16", "x17", "x30", "cc", "memory");
++ }
++}
++
+ /*
+ * Routine: arm_init
+ * Function: Runs on the boot CPU, once, on entry from iBoot.
+@@ -361,6 +390,7 @@ arm_init(
+ port_crumb = 100;
+ arm_slide_rebase_and_sign_image();
+ port_crumb = 101;
++ linux64_stage(101);
+
+ arm_static_if_init(args);
+ port_crumb = 102;
+@@ -382,6 +412,7 @@ arm_init(
+
+ PE_init_platform(FALSE, args); /* Get platform expert set up */
+ boot_progress_init();
++ linux64_stage(110);
+
+ #if __arm64__
+ configure_timer_apple_regs();
+@@ -475,6 +506,7 @@ arm_init(
+ #endif
+
+ ml_parse_cpu_topology();
++ linux64_stage(120);
+
+
+ boot_cpu_id = ml_get_boot_cpu_number();
+@@ -511,6 +543,7 @@ arm_init(
+
+ kernel_debug_string_early("kernel_startup_bootstrap");
+ kernel_startup_bootstrap();
++ linux64_stage(130);
+
+ /*
+ * Initialize the timer callout world
+@@ -585,6 +618,7 @@ arm_init(
+ gDramSize = *dram_size;
+ pmap_first_pnum = (ppnum_t)atop(gDramBase);
+
++ linux64_stage(199);
+ boot_progress_stage("arm_vm_init enter");
+ port_crumb = 1;
+ arm_vm_init(xmaxmem, args);
+diff --git a/osfmk/kern/debug.c b/osfmk/kern/debug.c
+index 0eae1f6..9a4e240 100644
+--- a/osfmk/kern/debug.c
++++ b/osfmk/kern/debug.c
+@@ -1253,6 +1253,21 @@ panic_trap_to_debugger(const char *panic_format_str, va_list *panic_args, unsign
+ {
+ #pragma clang diagnostic pop
+
++#if defined(__arm64__)
++ {
++ /* LINUX64 (xnulinux): report the panic to the host (see arm_init.c). */
++ extern uintptr_t linux64_hook;
++ if (linux64_hook) {
++ register unsigned long x0 __asm__("x0") = 999;
++ register unsigned long x1 __asm__("x1") = (unsigned long)panic_format_str;
++ register unsigned long x2 __asm__("x2") = panic_caller;
++ __asm__ volatile ("blr %3" : "+r"(x0), "+r"(x1), "+r"(x2) : "r"(linux64_hook)
++ : "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12", "x13",
++ "x14", "x15", "x16", "x17", "x30", "cc", "memory");
++ }
++ }
++#endif
++
+ #if defined(__x86_64__) && (DEVELOPMENT || DEBUG)
+ read_lbr();
+ #endif