XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
FKI: the parts that let Linux run (idle, host file I/O) move to a Linux-shaped stack of their own when called on a foreign stack (Linux's entry check panicked on an XNU stack with sp bit 14 set); xnu.ko notes such upcalls
Heyang Zhou27bc6e3e74ea
diff --git a/fki-ko/fki_mod.c b/fki-ko/fki_mod.cindex 3ddb1c1..4881a9f 100644--- a/fki-ko/fki_mod.c+++ b/fki-ko/fki_mod.c@@ -30,6 +30,7 @@#include <linux/err.h>#include <linux/sched.h>#include <linux/fs.h>+#include <asm/stacktrace.h>int fki_version(void){@@ -381,24 +382,82 @@ void fki_window_set(unsigned long long *slot, fki_u64 desc)}EXPORT_SYMBOL(fki_window_set);+/*+ * Letting Linux run (IRQs on, maybe a reschedule) from inside fki_enter() happens on whatever stack the foreign kernel+ * is on. Linux's exception entry takes a stack whose bit THREAD_SHIFT is set at sp for an overflow and panics, so+ * from a stack that is not this task's, the work runs on a stack of our own laid out like Linux's (the lower half of a+ * 2 * THREAD_SIZE aligned block, which the linear map gives an order-aligned page block).+ */+static void *fki_alt_stack[NR_CPUS];++asm(+" .pushsection .text\n"+" .type fki_run_on_stack, %function\n"+"fki_run_on_stack:\n"+" hint #34\n"+" stp x29, x30, [sp, #-16]!\n"+" mov x29, sp\n"+" mov x9, sp\n"+" mov sp, x2\n"+" stp x9, xzr, [sp, #-16]!\n"+" mov x9, x0\n"+" mov x0, x1\n"+" blr x9\n"+" ldp x9, xzr, [sp], #16\n"+" mov sp, x9\n"+" ldp x29, x30, [sp], #16\n"+" ret\n"+" .size fki_run_on_stack, . - fki_run_on_stack\n"+" .popsection\n");+void fki_run_on_stack(void (*fn)(void *), void *arg, void *top); /* x0 = fn, x1 = arg, x2 = stack top */++static void fki_linux_runs(void (*fn)(void *), void *arg)+{+ unsigned long sp = current_stack_pointer;+ int cpu = smp_processor_id();++ if (on_task_stack(current, sp, 1)) {+ fn(arg);+ return;+ }+ if (!fki_alt_stack[cpu]) {+ struct page *pg = alloc_pages(GFP_ATOMIC, get_order(2 * THREAD_SIZE));+ if (!pg) {+ fn(arg); /* no memory: take the chance */+ return;+ }+ fki_alt_stack[cpu] = page_address(pg);+ }+ fki_run_on_stack(fn, arg, (char *)fki_alt_stack[cpu] + THREAD_SIZE);+}++struct fki_idle_args { fki_u64 ns; };++static void fki_idle_body(void *p)+{+ struct fki_idle_args *a = p;+ ktime_t t = ns_to_ktime(a->ns);++ local_irq_enable();+ preempt_enable();+ set_current_state(TASK_UNINTERRUPTIBLE);+ schedule_hrtimeout_range(&t, 50000, HRTIMER_MODE_REL);+ preempt_disable();+ local_irq_disable();+}+void fki_idle_until(fki_u64 counter_deadline, fki_u64 max_ns){u64 now = __arch_counter_get_cntvct(), freq = arch_timer_get_cntfrq(), ns;+ struct fki_idle_args a;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();+ a.ns = ns;+ fki_linux_runs(fki_idle_body, &a);}EXPORT_SYMBOL(fki_idle_until);@@ -419,15 +478,32 @@ EXPORT_SYMBOL(fki_file_open);#define FKI_FILE_CHUNK (256UL << 10)+static long fki_file_rw_body(void *file, int write, void *buf, unsigned long len, fki_u64 off);+struct fki_file_args { void *file; int write; void *buf; unsigned long len; fki_u64 off; long ret; };+static void fki_file_rw_thunk(void *p)+{+ struct fki_file_args *a = p;+ a->ret = fki_file_rw_body(a->file, a->write, a->buf, a->len, a->off);+}+long fki_file_rw(void *file, int write, void *buf, unsigned long len, fki_u64 off)+{+ struct fki_file_args a = { file, write, buf, len, off, -EIO };++ if (!file)+ return -EBADF;+ fki_linux_runs(fki_file_rw_thunk, &a);+ return a.ret;+}+EXPORT_SYMBOL(fki_file_rw);++static long fki_file_rw_body(void *file, int write, void *buf, unsigned long len, fki_u64 off){struct file *f = file;unsigned long done = 0;long ret = 0;void *bounce;- if (!f)- return -EBADF;local_irq_enable();preempt_enable();bounce = kvmalloc(min(len, FKI_FILE_CHUNK), GFP_KERNEL);@@ -464,7 +540,7 @@ long fki_file_rw(void *file, int write, void *buf, unsigned long len, fki_u64 oflocal_irq_disable();return ret < 0 && !done ? ret : (long)done;}-EXPORT_SYMBOL(fki_file_rw);+void fki_file_close(void *file){diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.cindex 3a58f57..37bfd98 100644--- a/xnu-ko/xnu_main.c+++ b/xnu-ko/xnu_main.c@@ -210,6 +210,17 @@ static int xnu_fault(unsigned long far, unsigned long esr, void *regs)static long xnu_hook_body(unsigned long stage, unsigned long a1, unsigned long a2);long xnu_hook(unsigned long stage, unsigned long a1, unsigned long a2){+ {+ /* debugging: an XNU stack Linux's overflow check would reject (bit 14 of sp set) */+ static int told;+ unsigned long sp;+ __asm__ volatile("mov %0, sp" : "=r"(sp));+ if ((sp & 0x4000) && told < 3) {+ told++;+ logkv("xnu: upcall on an XNU stack with sp bit 14 set, sp", sp);+ logkv("xnu: stage", stage);+ }+ }in_xnu = 0;long r = xnu_hook_body(stage, a1, a2);if (!r) in_xnu = 1;