XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
Console input: hosted XNU's serial keyboard thread polls the host (stage 993); xnu.ko console_in= (bytes appended to a host file are typed into /dev/console)
Heyang Zhou5d58cd588c1f
diff --git a/scripts/vm-m2-kbisect.sh b/scripts/vm-m2-kbisect.shindex 2151901..27c4984 100755--- a/scripts/vm-m2-kbisect.sh+++ b/scripts/vm-m2-kbisect.sh@@ -9,7 +9,7 @@ sudo rmmod xnu 2>/dev/null; sudo rmmod fki 2>/dev/null; sudo insmod fki-ko/fki.kfor K in "$@"; dosudo dmesg -Cecho ">>> kmax=$K"; sync- sudo insmod xnu-ko/xnu.ko mode=3 stop=899 kmax=$K idle_max=${IDLE_MAX:-200} ram_mb=${RAM_MB:-128} ${IMAGE:+image=$IMAGE} ${RAMDISK:+ramdisk=$RAMDISK} ${HOSTDISK:+hostdisk=$HOSTDISK hostdisk_rw=1} ${BOOTARGS:+bootargs=\"$BOOTARGS\"}+ sudo insmod xnu-ko/xnu.ko mode=3 stop=899 kmax=$K idle_max=${IDLE_MAX:-200} ram_mb=${RAM_MB:-128} ${IMAGE:+image=$IMAGE} ${RAMDISK:+ramdisk=$RAMDISK} ${HOSTDISK:+hostdisk=$HOSTDISK hostdisk_rw=1} ${CONSOLE_IN:+console_in=$CONSOLE_IN} ${BOOTARGS:+bootargs=\"$BOOTARGS\"}echo "lines: $(sudo dmesg | grep -ac 'xnu\]')"sudo dmesg | grep -a "xnu\]\|XNU stage 4\|budget\|panic\|run ended" | tail -${SHOW:-4}sudo rmmod xnudiff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.cindex 781bc5d..ef6cd3c 100644--- a/xnu-ko/xnu_main.c+++ b/xnu-ko/xnu_main.c@@ -52,6 +52,11 @@ static int hostdisk_rw;module_param(hostdisk, charp, 0444);module_param(hostdisk_rw, int, 0444);static void *hd_file; static fki_u64 hd_size;+/* Console input for XNU's /dev/console: bytes appended to this host file are typed in (stage 993) */+static char *console_in;+module_param(console_in, charp, 0444);+static void *ci_file; static fki_u64 ci_off;+struct xnu_cons_req { void *buf; u64_t len; long result; };/* hook returns must be 0 (nonzero ends the run): results come back through the request */struct xnu_hostdisk_req { u64_t write, off, len; void *buf; long result; };struct xnu_world xnu_W;@@ -229,6 +234,16 @@ long xnu_hook(unsigned long stage, unsigned long a1, unsigned long a2)static long xnu_hook_body(unsigned long stage, unsigned long a1, unsigned long a2){+ if (stage == 993) { /* console input: new bytes of console_in */+ struct xnu_cons_req *q = (struct xnu_cons_req *)a1;+ if (!q) return 0;+ q->result = 0;+ if (ci_file) {+ long n = fki_file_rw(ci_file, 0, q->buf, (unsigned long)q->len, ci_off);+ if (n > 0) { ci_off += n; q->result = n; }+ }+ return 0;+ }if (stage == 995) { /* host disk size in bytes into *a1, 0 when there is none */if (a1) *(u64_t *)a1 = hd_file ? hd_size : 0;return 0;@@ -676,6 +691,12 @@ static int __init xnu_init(void)err = fki_blob_get(ramdisk, &rd_data, &rd_size, &rd_blob);if (err) { logkv("xnu: cannot read the RAM disk, err", (u64_t)err); return err; }}+ if (console_in) {+ fki_u64 sz = 0;+ ci_file = fki_file_open(console_in, 0, &sz);+ ci_off = sz; /* only what is written after the load */+ if (!ci_file) logs("xnu: cannot open console_in");+ }if (hostdisk) {hd_file = fki_file_open(hostdisk, hostdisk_rw, &hd_size);if (!hd_file) { logs("xnu: cannot open the host disk"); return -2; }@@ -712,6 +733,7 @@ static void __exit xnu_exit(void)if (blob) fki_blob_put(blob);if (rd_blob) fki_blob_put(rd_blob);if (hd_file) fki_file_close(hd_file);+ if (ci_file) fki_file_close(ci_file);}module_init(xnu_init);diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patchindex abeee9b..20b18da 100644--- a/xnu-patches/0001-linux64-stage-hook.patch+++ b/xnu-patches/0001-linux64-stage-hook.patch@@ -2462,6 +2462,61 @@ index 11d9eb4..92f42d3 100644if (disableConsoleOutput) {return;}+diff --git a/osfmk/console/serial_general.c b/osfmk/console/serial_general.c+index b1ca47a..406a431 100644+--- a/osfmk/console/serial_general.c++++ b/osfmk/console/serial_general.c+@@ -48,13 +48,40 @@ SECURITY_READ_ONLY_LATE(unsigned int) serialmode;+ * characters that have been typed.+ */+++/*++ * LINUX64 (xnulinux): a hosted XNU's console input comes from the host (stage 993: up to len bytes into buf, the count++ * in result), polled by the serial keyboard thread like a UART.++ */++extern uintptr_t linux64_hook;++struct linux64_cons_req { void *buf; uint64_t len; long result; };++static int++linux64_cons_getc(void)++{++ static char buf[64];++ static int n, i;++++ if (i < n) {++ return (unsigned char)buf[i++];++ }++ struct linux64_cons_req q = { .buf = buf, .len = sizeof(buf), .result = 0 };++ register unsigned long x0 __asm__("x0") = 993;++ register unsigned long x1 __asm__("x1") = (unsigned long)&q;++ register unsigned long x2 __asm__("x2") = 0;++ __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");++ n = q.result > 0 ? (int)q.result : 0;++ i = 0;++ return n ? (unsigned char)buf[i++] : -1;++}+++ void+ serial_keyboard_init(void)+ {+ kern_return_t result;+ thread_t thread;++- if (!(serialmode & SERIALMODE_INPUT)) { /* Leave if we do not want a serial console */++ if (!(serialmode & SERIALMODE_INPUT) && !linux64_hook) { /* Leave if we do not want a serial console */+ return;+ }++@@ -82,7 +109,7 @@ serial_keyboard_poll(void)+ uint64_t next;++ while (1) {+- chr = _serial_getc(false); /* Get a character if there is one */++ chr = linux64_hook ? linux64_cons_getc() : _serial_getc(false); /* Get a character if there is one */+ if (chr < 0) { /* The serial buffer is empty */+ break;+ }diff --git a/osfmk/kern/assert.h b/osfmk/kern/assert.hindex 4224c9d..4a1fe94 100644--- a/osfmk/kern/assert.h