XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
M2c: cooperative idle — XNU's idle hands its timer deadline to the host, which sleeps in Linux
Heyang Zhou68bd8450fe67
diff --git a/fki-ko/fki.h b/fki-ko/fki.hindex 5fe0bf8..9a6d514 100644--- a/fki-ko/fki.h+++ b/fki-ko/fki.h@@ -64,6 +64,11 @@ void fki_contig_free(struct fki_region *r);*/int fki_window_l1(unsigned long va, unsigned long long **l1_va, fki_u64 *l1_pa);void fki_window_set(unsigned long long *slot, fki_u64 desc);+/*+ * Called from inside fki_enter() (preemption and IRQs off): let Linux run (IRQs and preemption on)+ * until the counter reaches deadline or max_ns pass, then return with them off again.+ */+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);#ifdef __cplusplusdiff --git a/fki-ko/fki_mod.c b/fki-ko/fki_mod.cindex 07cc3de..a9e8deb 100644--- a/fki-ko/fki_mod.c+++ b/fki-ko/fki_mod.c@@ -24,6 +24,8 @@#include <asm/tlbflush.h>#include <asm/barrier.h>#include <linux/kprobes.h>+#include <linux/hrtimer.h>+#include <linux/sched.h>int fki_version(void){@@ -258,5 +260,26 @@ void fki_window_set(unsigned long long *slot, fki_u64 desc)}EXPORT_SYMBOL(fki_window_set);+void fki_idle_until(fki_u64 counter_deadline, fki_u64 max_ns)+{+ u64 now = __arch_counter_get_cntvct(), freq = arch_timer_get_cntfrq(), ns;++ if (counter_deadline <= now)+ return;+ ns = div64_u64((counter_deadline - now) * 1000000ULL, freq) * 1000ULL;+ if (ns > max_ns)+ ns = max_ns;+ local_irq_enable();+ preempt_enable();+ {+ ktime_t t = ns_to_ktime(ns);+ set_current_state(TASK_UNINTERRUPTIBLE);+ schedule_hrtimeout_range(&t, 50000, HRTIMER_MODE_REL);+ }+ preempt_disable();+ local_irq_disable();+}+EXPORT_SYMBOL(fki_idle_until);+MODULE_LICENSE("GPL");MODULE_DESCRIPTION("Foreign kernel interface v0 (out-of-tree form for stock kernels)");diff --git a/linux/include/linux/fki.h b/linux/include/linux/fki.hindex 5fe0bf8..9a6d514 100644--- a/linux/include/linux/fki.h+++ b/linux/include/linux/fki.h@@ -64,6 +64,11 @@ void fki_contig_free(struct fki_region *r);*/int fki_window_l1(unsigned long va, unsigned long long **l1_va, fki_u64 *l1_pa);void fki_window_set(unsigned long long *slot, fki_u64 desc);+/*+ * Called from inside fki_enter() (preemption and IRQs off): let Linux run (IRQs and preemption on)+ * until the counter reaches deadline or max_ns pass, then return with them off again.+ */+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);#ifdef __cplusplusdiff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.cindex 05a0a6c..4b26bfa 100644--- a/xnu-ko/xnu_main.c+++ b/xnu-ko/xnu_main.c@@ -155,6 +155,12 @@ long xnu_hook(unsigned long stage, unsigned long a1, unsigned long a2)if (diag_va[5]) { unsigned long l = ((unsigned long (*)(const char *))diag_va[5])("xnulinux"); logkv("xnu: probe exec strlen through XNU tables", l); }logs("xnu: probes done");}+ if (stage == 810) { /* XNU idle: let Linux run until XNU's next timer deadline */+ static unsigned long idles;+ if ((++idles & (idles - 1)) == 0) logkv("xnu: XNU idle, count", idles);+ fki_idle_until(a1, 20000000ULL); /* at most 20 ms per round */+ return (long)stop == 810 && idles >= (unsigned long)(kmax ? kmax : 1);+ }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++; }diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patchindex a308077..1ac9b23 100644--- a/xnu-patches/0001-linux64-stage-hook.patch+++ b/xnu-patches/0001-linux64-stage-hook.patch@@ -587,10 +587,46 @@ index 84deb3f..767b8f8 100644.macro BRANCH_EXTERNdiff --git a/osfmk/arm64/cpu.c b/osfmk/arm64/cpu.c-index d10d7a1..edece60 100644+index d10d7a1..5f33c6d 100644--- a/osfmk/arm64/cpu.c+++ b/osfmk/arm64/cpu.c-@@ -735,20 +735,27 @@ cpu_idle_exit(boolean_t from_reset)+@@ -100,6 +100,7 @@ extern void __attribute__((noreturn)) arm64_prepare_for_sleep(boolean_t deep_sle+ extern void __attribute__((noreturn)) arm64_prepare_for_sleep(boolean_t deep_sleep);+ #endif+ extern void arm64_force_wfi_clock_gate(void);++extern void rtclock_intr(unsigned int);+ #if defined(APPLETYPHOON)+ // <rdar://problem/15827409>+ extern void typhoon_prepare_for_wfi(void);+@@ -614,6 +615,27 @@ cpu_idle(void)+ cpc_cpu_transition(CPC_CPU_IDLE, cpu_data_ptr);+ #endif /* CONFIG_CPU_COUNTERS */+++ {++ /*++ * LINUX64 (xnulinux): no WFI and no timer interrupt of our own. The host sleeps (in Linux, with++ * Linux interrupts on) until XNU's next deadline; then the timer "interrupt" is delivered here.++ */++ extern uintptr_t linux64_hook;++ extern void linux64_stage_arg(unsigned long, unsigned long);++ if (linux64_hook) {++ uint64_t now = mach_absolute_time();++ uint64_t deadline = cpu_data_ptr->rtcPop;++ if (deadline == 0 || deadline == EndOfAllTime || deadline < now) {++ deadline = now + 24000; /* about 1 ms: re-check the run queue */++ }++ linux64_stage_arg(810, deadline);++ cpu_data_ptr->cpu_decrementer = -1;++ rtclock_intr(FALSE);++ ClearIdlePop(TRUE);++ cpu_idle_exit(FALSE);++ }++ }+++ if (wfi) {+ #if !defined(APPLE_ARM64_ARCH_FAMILY)+ platform_cache_idle_enter();+@@ -735,20 +757,27 @@ cpu_idle_exit(boolean_t from_reset)voidcpu_init(void){@@ -618,7 +654,7 @@ index d10d7a1..edece60 100644} else {/** We initialize non-boot CPUs here; the boot CPU is-@@ -758,9 +765,11 @@ cpu_init(void)+@@ -758,9 +787,11 @@ cpu_init(void)}do_cacheid();@@ -630,7 +666,7 @@ index d10d7a1..edece60 100644cpu_info_p = cpuid_info();-@@ -780,6 +789,7 @@ cpu_init(void)+@@ -780,6 +811,7 @@ cpu_init(void)}cdp->cpu_threadtype = CPU_THREADTYPE_NONE;@@ -1042,6 +1078,24 @@ index 13d2819..3ba4ba7 100644str x1, [x0, #PANIC_LOCKDOWN_INITIATOR_STATE_INITIATOR_TPIDR]mrs x1, MPIDR_EL1+diff --git a/osfmk/arm64/machine_routines.c b/osfmk/arm64/machine_routines.c+index f456377..5e1f0b1 100644+--- a/osfmk/arm64/machine_routines.c++++ b/osfmk/arm64/machine_routines.c+@@ -2529,6 +2529,13 @@ ml_set_decrementer(uint32_t dec_value)+ }+ }+++ {++ /* LINUX64 (xnulinux): the generic timer is Linux's; cpu_idle hands the deadline to the host. */++ extern uintptr_t linux64_hook;++ if (linux64_hook) {++ return;++ }++ }+ if (cdp->cpu_set_decrementer_func) {+ cdp->cpu_set_decrementer_func((uint32_t)counter_value);+ } else {diff --git a/osfmk/arm64/machine_routines_asm.s b/osfmk/arm64/machine_routines_asm.sindex 96650a0..54d80e5 100644--- a/osfmk/arm64/machine_routines_asm.s