Skip to content

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

C

Hosted XNU: host yield where kernel code re-enables interrupts (long kernel paths), quiet_console filter for high-volume console/kprintf lines, dmesg-based shell driver (log_buf_len=128M, console level 1)

Heyang Zhou5d232b6892c3
diff --git a/scripts/vm-dmesg-shell.sh b/scripts/vm-dmesg-shell.sh
new file mode 100755
index 0000000..bc02587
--- /dev/null
+++ b/scripts/vm-dmesg-shell.sh
@@ -0,0 +1,10 @@
+#!/bin/bash
+# On tokyo: talk to the hosted macOS shell through the VM. XNU's output is read from the kernel ring buffer (dmesg,
+# log_buf_len=128M) instead of the serial console: a flood of console lines through a 115200-baud console stalled
+# printk with interrupts off on XNU's CPU (RCU stall). Usage: vm-dmesg-shell.sh mark | send 'cmd' [secs] | since MARK
+cd "$(dirname "$0")/.."
+case "$1" in
+ mark) bash scripts/vm.sh 'sudo dmesg | tail -1 | sed "s/^\[ *\([0-9.]*\)\].*/\1/"';;
+ send) printf '%s\n' "$2" | timeout 30 bash scripts/vm.sh 'cat >> /var/tmp/console-in'; sleep "${3:-10}";;
+ since) bash scripts/vm.sh 'sudo dmesg' | awk -v m="$2" 'substr($0,2,12)+0 > m+0';;
+esac
diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.c
index cfa5b0d..9f02045 100644
--- a/xnu-ko/xnu_main.c
+++ b/xnu-ko/xnu_main.c
@@ -28,6 +28,21 @@ module_param(kmax, int, 0444);
static int kcount;
static int idle_max = 200; /* end the run after this many XNU idle rounds (0: never) */
module_param(idle_max, int, 0444);
+static int quiet_console = 1; /* drop the known high-volume console and kprintf lines (see quiet_line) */
+module_param(quiet_console, int, 0444);
+static int quiet_line(const char *l, unsigned n)
+{
+ static const char *const pfx[] = { "com.apple.xpc.launchd|", "kmutil: AppleImage4", "cryptexd: AppleImage4", "ACMTRM", "ACM:",
+ "AppleCredentialManagerUserClient", "hfs: set hardlink bit", "Couldn't alloc class", " dynamic: platformVersion" };
+ for (unsigned i = 0; i < sizeof(pfx) / sizeof(pfx[0]); i++) {
+ unsigned k = 0;
+ while (pfx[i][k] && k < n && l[k] == pfx[i][k]) k++;
+ if (!pfx[i][k]) return 1;
+ }
+ for (unsigned k = 0; k + 18 <= n; k++) /* "... triggered unnest of range ..." */
+ if (!x_strncmp(l + k, "triggered unnest", 16)) return 1;
+ return 0;
+}
static int assert_fatal; /* 1: a failed XNU assert() ends the run; 0: report it and let XNU continue */
module_param(assert_fatal, int, 0444);
@@ -298,8 +313,11 @@ static long xnu_hook_body(unsigned long stage, unsigned long a1, unsigned long a
const char *p = "darwin console: ";
while (*p) m[j++] = *p++;
for (k = 0; k < n; k++) m[j++] = line[k];
- m[j] = 0; n = 0;
- logs(m);
+ m[j] = 0;
+ /* quiet_console: launchd's and AppleImage4's chatter (thousands of lines a second) stays out */
+ if (!(quiet_console && quiet_line(line, n)))
+ logs(m);
+ n = 0;
}
}
/* a prompt (a write that ends in "# ", "$ " or "> " without a newline): show it now */
@@ -319,7 +337,7 @@ static long xnu_hook_body(unsigned long stage, unsigned long a1, unsigned long a
while (f && f[i] && j < sizeof(m) - 1) { if (f[i] != '\n') m[j++] = f[i]; i++; }
m[j] = 0;
/* the per-entry startup trace is thousands of lines: count it, do not print it */
- if (!(f && !x_strncmp(f, "kernel_startup_initialize_upto[", 31)))
+ if (!(f && !x_strncmp(f, "kernel_startup_initialize_upto[", 31)) && !(quiet_console && quiet_line(m + 5, j - 5)))
logs(m);
if (kmax && ++kcount >= kmax) { logs("xnu: kprintf budget reached"); return 1; }
return 0;
diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patch
index a24828c..ab1512b 100644
--- a/xnu-patches/0001-linux64-stage-hook.patch
+++ b/xnu-patches/0001-linux64-stage-hook.patch
@@ -1052,10 +1052,10 @@ index 743117b..9dc298b 100644
"msr SPSel, #1 \n"
"mov %0, sp \n"
diff --git a/osfmk/arm/machine_routines_common.c b/osfmk/arm/machine_routines_common.c
-index b65f76c..11b510a 100644
+index b65f76c..b0692dd 100644
--- a/osfmk/arm/machine_routines_common.c
+++ b/osfmk/arm/machine_routines_common.c
-@@ -1094,6 +1094,30 @@ ml_set_interrupts_enabled_with_debug(boolean_t enable, boolean_t __unused debug)
+@@ -1094,6 +1094,49 @@ ml_set_interrupts_enabled_with_debug(boolean_t enable, boolean_t __unused debug)
thread = current_thread();
@@ -1070,6 +1070,25 @@ index b65f76c..11b510a 100644
+ if (linux64_hook) {
+ boolean_t was = linux64_vint ? TRUE : FALSE;
+ if (enable && !was) {
++ /*
++ * Kernel code that runs long without going to user space or idle never sees a host interrupt
++ * (hardware IRQs stay masked in XNU's kernel), which stalls Linux (RCU) and stops XNU's own
++ * clock. Where XNU would take a pending interrupt, re-enabling interrupts, let Linux run its
++ * interrupts every 2 ms and deliver XNU's timer if it is due, as the IRQ path does.
++ */
++ static uint64_t linux64_last_kyield;
++ uint64_t now = mach_absolute_time();
++ if (now - linux64_last_kyield > 48000 && thread->machine.CpuDatap->cpu_int_state == NULL) {
++ extern void linux64_stage_arg(unsigned long, unsigned long);
++ extern void rtclock_intr(unsigned int);
++ cpu_data_t *cdp = thread->machine.CpuDatap;
++ linux64_last_kyield = now;
++ linux64_stage_arg(811, 1);
++ if (cdp->rtcPop != 0 && cdp->rtcPop != 0xFFFFFFFFFFFFFFFFULL && mach_absolute_time() >= cdp->rtcPop) {
++ cdp->cpu_decrementer = -1;
++ rtclock_intr(FALSE);
++ }
++ }
+ if (get_preemption_level() == 0) {
+ while (thread->machine.CpuDatap->cpu_pending_ast & AST_URGENT) {
+ ast_taken_kernel();
@@ -1086,7 +1105,7 @@ index b65f76c..11b510a 100644
state = __builtin_arm_rsr("DAIF");
if (__improbable(!(state & DAIF_DEBUGF))) {
-@@ -1384,6 +1408,13 @@ ml_wait_max_cpus(void)
+@@ -1384,6 +1427,13 @@ ml_wait_max_cpus(void)
{
assert(lockdown_done);
lck_mtx_lock(&max_cpus_lock);