Skip to content

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

C

Hosted XNU's console_write (/dev/console, km tty) goes to the host log through stage 996, line-buffered as 'darwin console:'

Heyang Zhouc94e5f5a3e53
diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.c
index 7e500b5..2e77cd7 100644
--- a/xnu-ko/xnu_main.c
+++ b/xnu-ko/xnu_main.c
@@ -200,6 +200,24 @@ 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 == 996) { /* XNU console bytes (/dev/console): line-buffered into the host log */
+ static char line[200]; static unsigned n;
+ const char *b = (const char *)a1; unsigned long i;
+ for (i = 0; b && i < a2; i++) {
+ char c = b[i];
+ if (c == '\r') continue;
+ if (c != '\n') line[n++] = c;
+ if (c == '\n' || n == sizeof(line) - 1) {
+ char m[220]; unsigned j = 0, k;
+ 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);
+ }
+ }
+ return 0;
+ }
if (stage == 998) { /* XNU kprintf */
char m[260]; const char *f = (const char *)a1; unsigned i = 0, j = 0;
m[j++] = 'x'; m[j++] = 'n'; m[j++] = 'u'; m[j++] = ']'; m[j++] = ' ';
diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patch
index 807872e..f09ba9d 100644
--- a/xnu-patches/0001-linux64-stage-hook.patch
+++ b/xnu-patches/0001-linux64-stage-hook.patch
@@ -1908,6 +1908,30 @@ index c615764..72ec4a7 100644
);
ml_pac_safe_interrupts_restore(intr);
}
+diff --git a/osfmk/console/serial_console.c b/osfmk/console/serial_console.c
+index 11d9eb4..92f42d3 100644
+--- a/osfmk/console/serial_console.c
++++ b/osfmk/console/serial_console.c
+@@ -532,6 +532,19 @@ console_resume()
+ void
+ console_write(char *str, int size)
+ {
++ {
++ /* LINUX64 (xnulinux): hosted XNU's console (/dev/console, km) goes to the host (stage 996: bytes, length). */
++ extern uintptr_t linux64_hook;
++ if (linux64_hook && str && size > 0) {
++ register unsigned long x0 __asm__("x0") = 996;
++ register unsigned long x1 __asm__("x1") = (unsigned long)str;
++ register unsigned long x2 __asm__("x2") = (unsigned long)size;
++ __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");
++ return;
++ }
++ }
+ if (disableConsoleOutput) {
+ return;
+ }
diff --git a/osfmk/kern/assert.h b/osfmk/kern/assert.h
index 4224c9d..4a1fe94 100644
--- a/osfmk/kern/assert.h