XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
Monitor thread: FKI helper threads (fki_thread_start/stop, fki_msleep); xnu.ko prints XNU's linux64_dbg counters from another CPU every 2 s while XNU runs
Heyang Zhou0b2330f3f574
diff --git a/fki-ko/fki.h b/fki-ko/fki.hindex 9a6d514..cf53f5b 100644--- a/fki-ko/fki.h+++ b/fki-ko/fki.h@@ -71,6 +71,14 @@ void fki_window_set(unsigned long long *slot, fki_u64 desc);void fki_idle_until(fki_u64 counter_deadline, fki_u64 max_ns);/* Keep the calling task on one CPU (the hosted kernel sees that CPU's MPIDR). */int fki_bind_cpu(int cpu);+/*+ * A helper kernel thread bound to one CPU: fn(arg) runs until it returns or fki_thread_stop() is called;+ * fn should loop on !fki_thread_should_stop() and sleep with fki_msleep().+ */+void *fki_thread_start(int (*fn)(void *), void *arg, int cpu, const char *name);+void fki_thread_stop(void *thread);+int fki_thread_should_stop(void);+void fki_msleep(unsigned int ms);#ifdef __cplusplus}#endifdiff --git a/fki-ko/fki_mod.c b/fki-ko/fki_mod.cindex a9e8deb..fc3c39a 100644--- a/fki-ko/fki_mod.c+++ b/fki-ko/fki_mod.c@@ -25,6 +25,9 @@#include <asm/barrier.h>#include <linux/kprobes.h>#include <linux/hrtimer.h>+#include <linux/kthread.h>+#include <linux/delay.h>+#include <linux/err.h>#include <linux/sched.h>int fki_version(void)@@ -203,6 +206,43 @@ int fki_bind_cpu(int cpu)}EXPORT_SYMBOL(fki_bind_cpu);+void *fki_thread_start(int (*fn)(void *), void *arg, int cpu, const char *name)+{+ struct task_struct *t;++ if (cpu < 0 || cpu >= nr_cpu_ids || !cpu_online(cpu))+ return NULL;+ t = kthread_create(fn, arg, "%s", name);+ if (IS_ERR(t))+ return NULL;+ kthread_bind(t, cpu);+ get_task_struct(t);+ wake_up_process(t);+ return t;+}+EXPORT_SYMBOL(fki_thread_start);++void fki_thread_stop(void *thread)+{+ if (thread) {+ kthread_stop(thread);+ put_task_struct(thread);+ }+}+EXPORT_SYMBOL(fki_thread_stop);++int fki_thread_should_stop(void)+{+ return kthread_should_stop();+}+EXPORT_SYMBOL(fki_thread_should_stop);++void fki_msleep(unsigned int ms)+{+ msleep(ms);+}+EXPORT_SYMBOL(fki_msleep);+/* ---- contiguous memory and the top foreign window ---- */int fki_contig_alloc(unsigned long size, struct fki_region *r)diff --git a/linux/include/linux/fki.h b/linux/include/linux/fki.hindex 9a6d514..cf53f5b 100644--- a/linux/include/linux/fki.h+++ b/linux/include/linux/fki.h@@ -71,6 +71,14 @@ void fki_window_set(unsigned long long *slot, fki_u64 desc);void fki_idle_until(fki_u64 counter_deadline, fki_u64 max_ns);/* Keep the calling task on one CPU (the hosted kernel sees that CPU's MPIDR). */int fki_bind_cpu(int cpu);+/*+ * A helper kernel thread bound to one CPU: fn(arg) runs until it returns or fki_thread_stop() is called;+ * fn should loop on !fki_thread_should_stop() and sleep with fki_msleep().+ */+void *fki_thread_start(int (*fn)(void *), void *arg, int cpu, const char *name);+void fki_thread_stop(void *thread);+int fki_thread_should_stop(void);+void fki_msleep(unsigned int ms);#ifdef __cplusplus}#endifdiff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.cindex daeb410..033d27f 100644--- a/xnu-ko/xnu_main.c+++ b/xnu-ko/xnu_main.c@@ -290,6 +290,54 @@ static struct boot_args_v2 *build_boot(unsigned char *base_va, u64_t phys, u64_treturn ba;}++/*+ * Monitor: XNU runs with interrupts masked on its CPU, so when it spins nothing reaches Linux. A helper+ * thread on another CPU prints XNU's debug counters (linux64_dbg, see the XNU patch) every 2 s.+ */+static volatile const unsigned long *xnu_dbg;+static void *monitor;+static int monitor_cpu = 1;+module_param(monitor_cpu, int, 0444);++static char *put_dec(char *p, unsigned long v)+{+ char t[24]; int n = 0;+ do { t[n++] = '0' + v % 10; v /= 10; } while (v);+ while (n) *p++ = t[--n];+ return p;+}++static int monitor_fn(void *arg)+{+ static const char *const names[] = { "idleloop", "machine_idle", "cpu_idle", "host_idle", "invoke",+ "-", "stage", "rtclock" };+ int lines = 0;+ (void)arg;+ while (!fki_thread_should_stop()) {+ fki_msleep(2000);+ if (!xnu_dbg || lines >= 90)+ continue;+ char buf[256]; char *p = buf;+ const char *h = "xnu: monitor";+ while (*h) *p++ = *h++;+ for (int i = 0; i < 8; i++) {+ if (i == 5) continue;+ *p++ = ' ';+ for (const char *n = names[i]; *n; n++) *p++ = *n;+ *p++ = '=';+ p = put_dec(p, xnu_dbg[i]);+ }+ h = " thread=0x";+ while (*h) *p++ = *h++;+ for (int i = 44; i >= 0; i -= 4) *p++ = "0123456789abcdef"[(xnu_dbg[9] >> i) & 15];+ *p = 0;+ logs(buf);+ lines++;+ }+ return 0;+}+static int enter_arm_init(struct boot_args_v2 *ba){struct fki_pac_keys saved; unsigned long c = fki_enter(&xnu_W.xnu_keys, &saved);@@ -311,8 +359,13 @@ static int enter_arm_init(struct boot_args_v2 *ba)diag_va[0] = a_init; diag_va[1] = img.vmlow; diag_va[2] = (u64_t)ba; diag_va[3] = (u64_t)ba->deviceTreeP;diag_va[4] = macho_sym(&img, "_port_crumb");diag_va[5] = macho_sym(&img, "_strlen");+ xnu_dbg = (const unsigned long *)macho_sym(&img, "_linux64_dbg");+ if (xnu_dbg && monitor_cpu >= 0)+ monitor = fki_thread_start(monitor_fn, 0, monitor_cpu, "xnu-monitor");logs("xnu: entering arm_init");long r = xnu_world_call(a_init, (unsigned long)ba);+ fki_thread_stop(monitor);+ monitor = 0;logkv(r ? "xnu: run ended by stage hook, r =" : "xnu: arm_init returned (unexpected), r =", (u64_t)r);return 0;}diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patchindex 2c884e8..9db8055 100644--- a/xnu-patches/0001-linux64-stage-hook.patch+++ b/xnu-patches/0001-linux64-stage-hook.patch@@ -209,10 +209,10 @@ index d46815b..ccd2d84 100644g_crypto_funcs->random_kmem_init_fn(ctx);}diff --git a/osfmk/arm/arm_init.c b/osfmk/arm/arm_init.c-index ac1713e..d65a903 100644+index ac1713e..45b4d94 100644--- a/osfmk/arm/arm_init.c+++ b/osfmk/arm/arm_init.c-@@ -228,6 +228,15 @@ extern vm_offset_t segLOWESTKC, segHIGHESTKC, segLOWESTROKC, segHIGHESTROKC;+@@ -228,6 +228,17 @@ extern vm_offset_t segLOWESTKC, segHIGHESTKC, segLOWESTROKC, segHIGHESTROKC;extern vm_offset_t segLOWESTAuxKC, segHIGHESTAuxKC, segLOWESTROAuxKC, segHIGHESTROAuxKC;extern vm_offset_t segLOWESTRXAuxKC, segHIGHESTRXAuxKC, segHIGHESTNLEAuxKC;@@ -224,11 +224,13 @@ index ac1713e..d65a903 100644+uintptr_t linux64_slide;+/* LINUX64: XNU's virtual interrupt-enable state (hardware IRQs stay masked while hosted). */+unsigned int linux64_vint;++/* LINUX64: debug counters, read by xnu.ko's monitor thread on another CPU while XNU runs. */++unsigned long linux64_dbg[16];+static voidarm_slide_rebase_and_sign_image(void){-@@ -285,10 +294,14 @@ arm_slide_rebase_and_sign_image(void)+@@ -285,10 +296,14 @@ arm_slide_rebase_and_sign_image(void)* using hardcoded kernel link address*/slide = (uintptr_t)k_mh - VM_KERNEL_LINK_ADDRESS;@@ -244,7 +246,7 @@ index ac1713e..d65a903 100644rebase_threaded_starts( &__thread_starts_sect_start[0],&__thread_starts_sect_end[0],(uintptr_t)k_mh, (uintptr_t)k_mh - slide, slide);-@@ -341,6 +354,58 @@ arm_auxkc_init(void *mh, void *base)+@@ -341,6 +356,60 @@ arm_auxkc_init(void *mh, void *base)#endif /* defined(HAS_APPLE_PAC) */}@@ -259,6 +261,7 @@ index ac1713e..d65a903 100644+static inline void+linux64_stage(unsigned long stage)+{++ linux64_dbg[6] = stage;+ if (linux64_hook) {+ register unsigned long x0 __asm__("x0") = stage;+ __asm__ volatile ("blr %1"@@ -281,6 +284,7 @@ index ac1713e..d65a903 100644+void+linux64_stage_arg(unsigned long stage, unsigned long arg)+{++ linux64_dbg[6] = stage;+ if (linux64_hook) {+ register unsigned long x0 __asm__("x0") = stage;+ register unsigned long x1 __asm__("x1") = arg;@@ -303,7 +307,7 @@ index ac1713e..d65a903 100644/** Routine: arm_init* Function: Runs on the boot CPU, once, on entry from iBoot.-@@ -361,6 +426,7 @@ arm_init(+@@ -361,6 +430,7 @@ arm_init(port_crumb = 100;arm_slide_rebase_and_sign_image();port_crumb = 101;@@ -311,7 +315,7 @@ index ac1713e..d65a903 100644arm_static_if_init(args);port_crumb = 102;-@@ -382,6 +448,7 @@ arm_init(+@@ -382,6 +452,7 @@ arm_init(PE_init_platform(FALSE, args); /* Get platform expert set up */boot_progress_init();@@ -319,7 +323,7 @@ index ac1713e..d65a903 100644#if __arm64__configure_timer_apple_regs();-@@ -475,6 +542,7 @@ arm_init(+@@ -475,6 +546,7 @@ arm_init(#endifml_parse_cpu_topology();@@ -327,7 +331,7 @@ index ac1713e..d65a903 100644boot_cpu_id = ml_get_boot_cpu_number();-@@ -511,15 +579,19 @@ arm_init(+@@ -511,15 +583,19 @@ arm_init(kernel_debug_string_early("kernel_startup_bootstrap");kernel_startup_bootstrap();@@ -347,7 +351,7 @@ index ac1713e..d65a903 100644if (PE_parse_boot_argn("maxmem", &maxmem, sizeof(maxmem))) {xmaxmem = (uint64_t) maxmem * (1024 * 1024);-@@ -544,7 +616,9 @@ arm_init(+@@ -544,7 +620,9 @@ arm_init(}#endif /* SCHED_HYGIENE_DEBUG */@@ -357,7 +361,7 @@ index ac1713e..d65a903 100644#if HAS_BP_RETPE_parse_boot_argn("bpret", &bp_ret, sizeof(bp_ret));-@@ -581,10 +655,12 @@ arm_init(+@@ -581,10 +659,12 @@ arm_init(panic("%s: Unable to find 'dram-size' entry in the 'chosen' DT node", __FUNCTION__);}@@ -370,7 +374,7 @@ index ac1713e..d65a903 100644boot_progress_stage("arm_vm_init enter");port_crumb = 1;arm_vm_init(xmaxmem, args);-@@ -607,6 +683,7 @@ arm_init(+@@ -607,6 +687,7 @@ arm_init(port_crumb = 5;kernel_startup_initialize_upto(STARTUP_SUB_KPRINTF);kprintf("kprintf initialized\n");@@ -378,7 +382,7 @@ index ac1713e..d65a903 100644serialmode = 0;if (PE_parse_boot_argn("serial", &serialmode, sizeof(serialmode))) {-@@ -648,10 +725,12 @@ arm_init(+@@ -648,10 +729,12 @@ arm_init(}PE_create_console();port_crumb = 6;@@ -391,7 +395,7 @@ index ac1713e..d65a903 100644#if __arm64__#if DEBUG-@@ -661,9 +740,11 @@ arm_init(+@@ -661,9 +744,11 @@ arm_init(cpu_machine_idle_init(TRUE);port_crumb = 8;@@ -403,7 +407,7 @@ index ac1713e..d65a903 100644/* Initialize the debug infrastructure system-wide and on the local core. */pe_arm_debug_init_early(&BootCpuData);-@@ -676,6 +757,7 @@ arm_init(+@@ -676,6 +761,7 @@ arm_init(ml_map_cpu_pio();port_crumb = 10;@@ -411,7 +415,7 @@ index ac1713e..d65a903 100644#if APPLE_ARM64_ARCH_FAMILYconfigure_late_apple_regs(true);-@@ -685,6 +767,7 @@ arm_init(+@@ -685,6 +771,7 @@ arm_init(cpu_timebase_init(TRUE);port_crumb = 11;@@ -419,7 +423,7 @@ index ac1713e..d65a903 100644#if KPERF/* kptimer_curcpu_up() must be called after cpu_timebase_init */-@@ -693,6 +776,7 @@ arm_init(+@@ -693,6 +780,7 @@ arm_init(PE_init_cpu();port_crumb = 12;@@ -427,7 +431,7 @@ index ac1713e..d65a903 100644#if __arm64__apt_msg_init();apt_msg_init_cpu();-@@ -717,6 +801,7 @@ arm_init(+@@ -717,6 +805,7 @@ arm_init(*/__stack_chk_guard &= ~(0xFFULL << 8);port_crumb = 13;@@ -562,6 +566,18 @@ index 79d0bc1..ac5b387 100644#if !HAS_16BIT_ASIDbitmap_full(&asid_plru_bitmap[0], MAX_HW_ASIDS);// Clear the highest-order bit, which corresponds to MAX_HW_ASIDS + 1+diff --git a/osfmk/arm/rtclock.c b/osfmk/arm/rtclock.c+index a17a07d..6136425 100644+--- a/osfmk/arm/rtclock.c++++ b/osfmk/arm/rtclock.c+@@ -327,6 +327,7 @@ rtclock_intr(__unused unsigned int is_user_context)+ struct arm_saved_state * regs;+ unsigned int user_mode;+ uintptr_t pc;++ { extern unsigned long linux64_dbg[16]; linux64_dbg[7]++; } /* LINUX64 debug */++ #if XNU_NATIVE_DWC3_CONSOLE+ extern void unixnu_dwc3_poll(void);diff --git a/osfmk/arm64/arm_vm_init.c b/osfmk/arm64/arm_vm_init.cindex f7f387e..c239600 100644--- a/osfmk/arm64/arm_vm_init.c@@ -842,7 +858,7 @@ index 84deb3f..767b8f8 100644.macro BRANCH_EXTERNdiff --git a/osfmk/arm64/cpu.c b/osfmk/arm64/cpu.c-index d10d7a1..6d2cc80 100644+index d10d7a1..0052898 100644--- a/osfmk/arm64/cpu.c+++ b/osfmk/arm64/cpu.c@@ -100,6 +100,7 @@ extern void __attribute__((noreturn)) arm64_prepare_for_sleep(boolean_t deep_sle@@ -866,7 +882,15 @@ index d10d7a1..6d2cc80 100644if (unmask) {/* Latch SW IRQ+FIQ counter prior to unmasking* interrupts.-@@ -570,7 +577,9 @@ cpu_idle(void)+@@ -506,6 +513,7 @@ cpu_idle(void)+ uint64_t new_idle_timeout_ticks = 0x0ULL, lastPop;+ bool idle_disallowed = false;+++ { extern unsigned long linux64_dbg[16]; linux64_dbg[2]++; } /* LINUX64 debug */+ if (__improbable((!idle_enable))) {+ idle_disallowed = true;+ os_atomic_inc(&virt_idle_disallowed[0], relaxed);+@@ -570,7 +578,9 @@ cpu_idle(void)}}@@ -877,7 +901,7 @@ index d10d7a1..6d2cc80 100644if (idle_wfe_poll && idle_proximate_timer_wfe == 1) {/* Poll issuing WFEs until the expected* timer FIQ arrives.-@@ -614,6 +623,27 @@ cpu_idle(void)+@@ -614,6 +624,28 @@ cpu_idle(void)cpc_cpu_transition(CPC_CPU_IDLE, cpu_data_ptr);#endif /* CONFIG_CPU_COUNTERS */@@ -894,6 +918,7 @@ index d10d7a1..6d2cc80 100644+ if (deadline == 0 || deadline == EndOfAllTime || deadline < now) {+ deadline = now + 24000; /* about 1 ms: re-check the run queue */+ }++ { extern unsigned long linux64_dbg[16]; linux64_dbg[3]++; } /* LINUX64 debug */+ linux64_stage_arg(810, deadline);+ cpu_data_ptr->cpu_decrementer = -1;+ rtclock_intr(FALSE);@@ -905,7 +930,7 @@ index d10d7a1..6d2cc80 100644if (wfi) {#if !defined(APPLE_ARM64_ARCH_FAMILY)platform_cache_idle_enter();-@@ -735,20 +765,27 @@ cpu_idle_exit(boolean_t from_reset)+@@ -735,20 +767,27 @@ cpu_idle_exit(boolean_t from_reset)voidcpu_init(void){@@ -933,7 +958,7 @@ index d10d7a1..6d2cc80 100644} else {/** We initialize non-boot CPUs here; the boot CPU is-@@ -758,9 +795,11 @@ cpu_init(void)+@@ -758,9 +797,11 @@ cpu_init(void)}do_cacheid();@@ -945,7 +970,7 @@ index d10d7a1..6d2cc80 100644cpu_info_p = cpuid_info();-@@ -780,6 +819,7 @@ cpu_init(void)+@@ -780,6 +821,7 @@ cpu_init(void)}cdp->cpu_threadtype = CPU_THREADTYPE_NONE;@@ -1358,10 +1383,18 @@ index 13d2819..3ba4ba7 100644mrs x1, MPIDR_EL1diff --git a/osfmk/arm64/machine_routines.c b/osfmk/arm64/machine_routines.c-index f456377..ef5a7f5 100644+index f456377..1cafc2c 100644--- a/osfmk/arm64/machine_routines.c+++ b/osfmk/arm64/machine_routines.c-@@ -400,6 +400,15 @@ machine_idle(void)+@@ -385,6 +385,7 @@ wfe_process_recommendation(void)+ void+ machine_idle(void)+ {++ { extern unsigned long linux64_dbg[16]; linux64_dbg[1]++; } /* LINUX64 debug */+ /* Interrupts are expected to be masked on entry or re-entry via+ * Idle_load_context()+ */+@@ -400,6 +401,15 @@ machine_idle(void)*/Idle_context();}@@ -1377,7 +1410,7 @@ index f456377..ef5a7f5 100644__builtin_arm_wsr("DAIFClr", DAIFSC_STANDARD_DISABLE);}-@@ -441,6 +450,14 @@ ml_get_interrupts_enabled(void)+@@ -441,6 +451,14 @@ ml_get_interrupts_enabled(void){uint64_t value;@@ -1392,7 +1425,7 @@ index f456377..ef5a7f5 100644MRS(value, "DAIF");if ((value & DAIF_STANDARD_DISABLE) == DAIF_STANDARD_DISABLE) {return FALSE;-@@ -585,8 +602,12 @@ machine_lockdown(void)+@@ -585,8 +603,12 @@ machine_lockdown(void)#else#endif@@ -1405,7 +1438,7 @@ index f456377..ef5a7f5 100644#if CONFIG_KERNEL_INTEGRITY#if KERNEL_INTEGRITY_WT-@@ -2529,6 +2550,13 @@ ml_set_decrementer(uint32_t dec_value)+@@ -2529,6 +2551,13 @@ ml_set_decrementer(uint32_t dec_value)}}@@ -1557,6 +1590,26 @@ index 2fc6931..dd59ae6 100644if (fmt) {struct console_printbuf_state info_data;+diff --git a/osfmk/kern/sched_prim.c b/osfmk/kern/sched_prim.c+index b2aae03..dcfaff8 100644+--- a/osfmk/kern/sched_prim.c++++ b/osfmk/kern/sched_prim.c+@@ -3010,6 +3010,7 @@ thread_invoke(++ thread_continue_t continuation = self->continuation;+ void *parameter = self->parameter;++ { extern unsigned long linux64_dbg[16]; linux64_dbg[4]++; linux64_dbg[9] = (unsigned long)thread; } /* LINUX64 debug */++ struct recount_snap snap = { 0 };+ recount_snapshot(&snap);+@@ -6452,6 +6453,7 @@ processor_idle(+ recount_processor_idle(&processor->pr_recount, &snap);++ while (1) {++ { extern unsigned long linux64_dbg[16]; linux64_dbg[0]++; } /* LINUX64 debug: idle loop */+ /*+ * Ensure that updates to my processor and pset state,+ * made by the IPI source processor before sending the IPI,diff --git a/osfmk/prng/prng_random.c b/osfmk/prng/prng_random.cindex 44b993c..d21911d 100644--- a/osfmk/prng/prng_random.c