XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
XNU faults nothing resolves end the run cleanly: FKI offers faults by pc too and exposes fki_regs_pc/lr/set_pc; xnu.ko returns the exception into xnu_world_fault_abort (Linux keys back, then xnu_world_abort)
Heyang Zhou56a26c12dbeb
diff --git a/fki-ko/fki.h b/fki-ko/fki.hindex e077d5b..97df2ca 100644--- a/fki-ko/fki.h+++ b/fki-ko/fki.h@@ -80,10 +80,14 @@ void fki_thread_stop(void *thread);int fki_thread_should_stop(void);void fki_msleep(unsigned int ms);/*- * Foreign kernel faults: a kernel-mode data or instruction abort whose address is in [lo, hi) is offered to handler- * (far, esr, the faulting pt_regs) before Linux's own handling; handler returns 0 when it made the access retryable- * (the faulting instruction then runs again), nonzero to let Linux handle it as usual. One registration at a time.+ * Foreign kernel faults: a kernel-mode data or instruction abort whose address or faulting pc is in [lo, hi) is offered+ * to handler (far, esr, the faulting registers) before Linux's own handling; handler returns 0 when the exception should+ * return (the faulting instruction runs again, or wherever fki_regs_set_pc points it), nonzero to let Linux handle it as+ * usual. One registration at a time.*/+unsigned long fki_regs_pc(void *regs);+unsigned long fki_regs_lr(void *regs);+void fki_regs_set_pc(void *regs, unsigned long pc);int fki_fault_register(unsigned long lo, unsigned long hi, int (*handler)(unsigned long far, unsigned long esr, void *regs));void fki_fault_unregister(void);#ifdef __cplusplusdiff --git a/fki-ko/fki_mod.c b/fki-ko/fki_mod.cindex 558e7ab..49162c4 100644--- a/fki-ko/fki_mod.c+++ b/fki-ko/fki_mod.c@@ -258,7 +258,10 @@ static int fki_fault_pre(struct kprobe *p, struct pt_regs *kregs)struct pt_regs *regs = (struct pt_regs *)kregs->regs[2];int (*h)(unsigned long, unsigned long, void *) = READ_ONCE(fki_fault_handler);- if (!h || far < fki_fault_lo || far >= fki_fault_hi || user_mode(regs))+ if (!h || user_mode(regs))+ return 0;+ if ((far < fki_fault_lo || far >= fki_fault_hi) &&+ (instruction_pointer(regs) < fki_fault_lo || instruction_pointer(regs) >= fki_fault_hi))return 0;if (h(far, esr, regs))return 0;@@ -267,6 +270,13 @@ static int fki_fault_pre(struct kprobe *p, struct pt_regs *kregs)}NOKPROBE_SYMBOL(fki_fault_pre);+unsigned long fki_regs_pc(void *regs) { return instruction_pointer((struct pt_regs *)regs); }+EXPORT_SYMBOL(fki_regs_pc);+unsigned long fki_regs_lr(void *regs) { return ((struct pt_regs *)regs)->regs[30]; }+EXPORT_SYMBOL(fki_regs_lr);+void fki_regs_set_pc(void *regs, unsigned long pc) { instruction_pointer_set((struct pt_regs *)regs, pc); }+EXPORT_SYMBOL(fki_regs_set_pc);+static struct kprobe fki_fault_kp = { .symbol_name = "__do_kernel_fault", .pre_handler = fki_fault_pre };static int fki_fault_registered;diff --git a/linux/include/linux/fki.h b/linux/include/linux/fki.hindex e077d5b..97df2ca 100644--- a/linux/include/linux/fki.h+++ b/linux/include/linux/fki.h@@ -80,10 +80,14 @@ void fki_thread_stop(void *thread);int fki_thread_should_stop(void);void fki_msleep(unsigned int ms);/*- * Foreign kernel faults: a kernel-mode data or instruction abort whose address is in [lo, hi) is offered to handler- * (far, esr, the faulting pt_regs) before Linux's own handling; handler returns 0 when it made the access retryable- * (the faulting instruction then runs again), nonzero to let Linux handle it as usual. One registration at a time.+ * Foreign kernel faults: a kernel-mode data or instruction abort whose address or faulting pc is in [lo, hi) is offered+ * to handler (far, esr, the faulting registers) before Linux's own handling; handler returns 0 when the exception should+ * return (the faulting instruction runs again, or wherever fki_regs_set_pc points it), nonzero to let Linux handle it as+ * usual. One registration at a time.*/+unsigned long fki_regs_pc(void *regs);+unsigned long fki_regs_lr(void *regs);+void fki_regs_set_pc(void *regs, unsigned long pc);int fki_fault_register(unsigned long lo, unsigned long hi, int (*handler)(unsigned long far, unsigned long esr, void *regs));void fki_fault_unregister(void);#ifdef __cplusplusdiff --git a/xnu-ko/world.S b/xnu-ko/world.Sindex 4a95162..3102bba 100644--- a/xnu-ko/world.S+++ b/xnu-ko/world.S@@ -58,6 +58,16 @@ xnu_world_abort: /* from the upcall: Linux keys and TPIDR are live */ret.size xnu_world_call, . - xnu_world_call+ .globl xnu_world_fault_abort+ .type xnu_world_fault_abort, %function+xnu_world_fault_abort: /* exception return here: XNU keys live, XNU stack */+ W(x9)+ add x0, x9, #240 /* load Linux keys */+ add x1, x9, #328 /* save XNU keys */+ bl fki_pac_swap+ b xnu_world_abort+ .size xnu_world_fault_abort, . - xnu_world_fault_abort+.globl xnu_world_upcall.type xnu_world_upcall, %functionxnu_world_upcall: /* x0 = stage, called by XNU with blr */diff --git a/xnu-ko/world.h b/xnu-ko/world.hindex ac36c4e..29e74a6 100644--- a/xnu-ko/world.h+++ b/xnu-ko/world.h@@ -17,5 +17,7 @@ extern struct xnu_world xnu_W;long xnu_world_call(unsigned long fn, unsigned long arg);/* The address XNU calls (linux64_hook); it calls xnu_hook(stage) in the Linux world. */void xnu_world_upcall(void);+/* Where an XNU fault that cannot be handled returns to (still with XNU's keys): back to Linux's, then xnu_world_abort. */+void xnu_world_fault_abort(void);/* Linux-world handler: return 0 to resume XNU, nonzero to end the run. */long xnu_hook(unsigned long stage, unsigned long a1, unsigned long a2);diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.cindex d43bd35..4418d23 100644--- a/xnu-ko/xnu_main.c+++ b/xnu-ko/xnu_main.c@@ -131,13 +131,33 @@ static volatile int in_xnu;static unsigned long (*xnu_kfault)(unsigned long far, unsigned long esr);static unsigned long kfaults;+#define XNU_VA_LO 0xffffffe000000000ULL+#define XNU_VA_HI 0xfffffffc00000000ULLstatic int xnu_fault(unsigned long far, unsigned long esr, void *regs){- (void)regs;- if (!in_xnu || !xnu_kfault)+ if (!in_xnu)return 1;- kfaults++;- return xnu_kfault(far, esr) == 0 ? 0 : 1;+ if (far >= XNU_VA_LO && far < XNU_VA_HI && xnu_kfault) {+ kfaults++;+ if (xnu_kfault(far, esr) == 0)+ return 0;+ }+ /*+ * An XNU fault nothing resolves (a bug, or a NULL dereference): report it and end the run instead of letting Linux+ * oops with XNU's keys and stack live (which takes the machine with it). The exception returns into+ * xnu_world_fault_abort, which puts Linux's keys back and leaves through xnu_world_abort.+ */+ unsigned long pc = fki_regs_pc(regs);+ if (pc < XNU_VA_LO || pc >= XNU_VA_HI)+ return 1;+ in_xnu = 0;+ logs("xnu: XNU took a fault it cannot handle; ending the run");+ logkv("xnu: pc ", pc);+ logkv("xnu: lr ", fki_regs_lr(regs));+ logkv("xnu: far", far);+ logkv("xnu: esr", esr);+ fki_regs_set_pc(regs, (unsigned long)xnu_world_fault_abort);+ return 0;}static long xnu_hook_body(unsigned long stage, unsigned long a1, unsigned long a2);