XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
Kernel page faults on XNU addresses go to XNU's vm_fault: FKI fault hook (fki_fault_register; out of tree a kprobe on do_bad_area), XNU linux64_kernel_fault, xnu.ko forwards while XNU runs; hosted XNU kernel stacks 32 KiB aligned for Linux's overflow check
Heyang Zhou6fb2541e47f5
diff --git a/fki-ko/fki.h b/fki-ko/fki.hindex cf53f5b..e077d5b 100644--- a/fki-ko/fki.h+++ b/fki-ko/fki.h@@ -79,6 +79,13 @@ 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);+/*+ * 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.+ */+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 __cplusplus}#endifdiff --git a/fki-ko/fki_mod.c b/fki-ko/fki_mod.cindex fc3c39a..f4cd2e8 100644--- a/fki-ko/fki_mod.c+++ b/fki-ko/fki_mod.c@@ -243,6 +243,66 @@ void fki_msleep(unsigned int ms)}EXPORT_SYMBOL(fki_msleep);+/*+ * Foreign kernel faults. Out of tree, the hook is a kprobe on do_bad_area(far, esr, regs), where Linux's abort path+ * goes for a kernel address it has no mapping for (do_mem_abort and do_translation_fault cannot be probed). When+ * the handler resolves the fault, do_bad_area is skipped (return to its caller), and the exception returns to retry+ * the access. The in-tree patch calls the hook from do_translation_fault/do_page_fault directly.+ */+static unsigned long fki_fault_lo, fki_fault_hi;+static int (*fki_fault_handler)(unsigned long far, unsigned long esr, void *regs);++static int fki_fault_pre(struct kprobe *p, struct pt_regs *kregs)+{+ unsigned long far = kregs->regs[0], esr = kregs->regs[1];+ 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))+ return 0;+ if (h(far, esr, regs))+ return 0;+ instruction_pointer_set(kregs, kregs->regs[30]); /* do_bad_area returns at once */+ return 1;+}+NOKPROBE_SYMBOL(fki_fault_pre);++static struct kprobe fki_fault_kp = { .symbol_name = "do_bad_area", .pre_handler = fki_fault_pre };+static int fki_fault_registered;++int fki_fault_register(unsigned long lo, unsigned long hi, int (*handler)(unsigned long far, unsigned long esr, void *regs))+{+ int err;++ if (fki_fault_registered)+ return -EBUSY;+ fki_fault_lo = lo;+ fki_fault_hi = hi;+ WRITE_ONCE(fki_fault_handler, handler);+ err = register_kprobe(&fki_fault_kp);+ if (err) {+ WRITE_ONCE(fki_fault_handler, NULL);+ return err;+ }+ fki_fault_registered = 1;+ return 0;+}+EXPORT_SYMBOL(fki_fault_register);++void fki_fault_unregister(void)+{+ if (!fki_fault_registered)+ return;+ unregister_kprobe(&fki_fault_kp);+ WRITE_ONCE(fki_fault_handler, NULL);+ fki_fault_registered = 0;+ /* a fresh kprobe struct for the next registration */+ memset(&fki_fault_kp, 0, sizeof(fki_fault_kp));+ fki_fault_kp.symbol_name = "do_bad_area";+ fki_fault_kp.pre_handler = fki_fault_pre;+}+EXPORT_SYMBOL(fki_fault_unregister);+/* ---- 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 cf53f5b..e077d5b 100644--- a/linux/include/linux/fki.h+++ b/linux/include/linux/fki.h@@ -79,6 +79,13 @@ 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);+/*+ * 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.+ */+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 __cplusplus}#endifdiff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.cindex 934f90f..86ae6d2 100644--- a/xnu-ko/xnu_main.c+++ b/xnu-ko/xnu_main.c@@ -123,7 +123,33 @@ static void walk_log(const char *what, u64_t va, const unsigned long long *l1)}}+/*+ * Kernel page faults on XNU addresses, taken in Linux's vectors while XNU runs (XNU's keys and stack live), go to+ * XNU's own linux64_kernel_fault (vm_fault on the kernel map). in_xnu is 0 while host code runs (upcalls, monitor).+ */+static volatile int in_xnu;+static unsigned long (*xnu_kfault)(unsigned long far, unsigned long esr);+static unsigned long kfaults;++static int xnu_fault(unsigned long far, unsigned long esr, void *regs)+{+ (void)regs;+ if (!in_xnu || !xnu_kfault)+ return 1;+ kfaults++;+ return xnu_kfault(far, esr) == 0 ? 0 : 1;+}++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)+{+ in_xnu = 0;+ long r = xnu_hook_body(stage, a1, a2);+ if (!r) in_xnu = 1;+ return r;+}++static long xnu_hook_body(unsigned long stage, unsigned long a1, unsigned long a2){if (stage == 998) { /* XNU kprintf */char m[260]; const char *f = (const char *)a1; unsigned i = 0, j = 0;@@ -368,8 +394,15 @@ static int enter_arm_init(struct boot_args_v2 *ba)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");+ xnu_kfault = (void *)macho_sym(&img, "_linux64_kernel_fault");+ if (xnu_kfault && fki_fault_register(0xffffffe000000000ULL, 0xfffffffc00000000ULL, xnu_fault) == 0)+ logs("xnu: kernel faults on XNU addresses go to XNU's vm_fault");logs("xnu: entering arm_init");+ in_xnu = 1;long r = xnu_world_call(a_init, (unsigned long)ba);+ in_xnu = 0;+ fki_fault_unregister();+ logkv("xnu: kernel faults forwarded to XNU", kfaults);fki_thread_stop(monitor);monitor = 0;logkv(r ? "xnu: run ended by stage hook, r =" : "xnu: arm_init returned (unexpected), r =", (u64_t)r);diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patchindex 7df9bd9..431a073 100644--- a/xnu-patches/0001-linux64-stage-hook.patch+++ b/xnu-patches/0001-linux64-stage-hook.patch@@ -1581,6 +1581,49 @@ index d211433..dc247da 100644ldr \x, [\x, ACT_CPUDATAP]// No need for atomic - These are per CPU and interrupts are disabled+diff --git a/osfmk/arm64/sleh.c b/osfmk/arm64/sleh.c+index d8f1b19..29daae4 100644+--- a/osfmk/arm64/sleh.c++++ b/osfmk/arm64/sleh.c+@@ -3646,3 +3646,38 @@ sleh_panic_lockdown_should_initiate_el1_sp0_sync(uint64_t esr, uint64_t elr,+ }+ __SECURITY_STACK_DISALLOWED_POP+ #endif /* CONFIG_SPTM */++++/*++ * LINUX64 (xnulinux): a data or instruction abort on an XNU kernel address, taken in Linux's vectors and forwarded by++ * the host (xnu.ko through FKI). Does what handle_kernel_abort does for a kernel-map fault: the pmap fast path for++ * permission/access-flag faults, then vm_fault. Returns KERN_SUCCESS when the access can be retried.++ */++kern_return_t linux64_kernel_fault(uint64_t far, uint64_t esr);++kern_return_t++linux64_kernel_fault(uint64_t far, uint64_t esr)++{++ uint32_t ec = (uint32_t)((esr >> 26) & 0x3f);++ fault_status_t fault_code = (fault_status_t)(esr & 0x3f);++ vm_prot_t fault_type = VM_PROT_READ;++ kern_return_t result = KERN_FAILURE;++++ if (ec == 0x21) { /* instruction abort, current EL */++ fault_type = VM_PROT_READ | VM_PROT_EXECUTE;++ } else if (ec == 0x25) { /* data abort, current EL */++ if (esr & (1u << 6)) { /* WnR */++ fault_type = VM_PROT_READ | VM_PROT_WRITE;++ }++ } else {++ return KERN_FAILURE;++ }++ if (!is_vm_fault(fault_code) || !VM_KERNEL_ADDRESS(far)) {++ return KERN_FAILURE;++ }++ if (!is_translation_fault(fault_code)) {++ result = arm_fast_fault(kernel_map->pmap, far, fault_type, (fault_code == FSC_ACCESS_FLAG_FAULT_L3), FALSE);++ if (result == KERN_SUCCESS) {++ return result;++ }++ }++ return vm_fault(kernel_map, far, fault_type, FALSE, VM_KERN_MEMORY_NONE, THREAD_UNINT, NULL, 0);++}diff --git a/osfmk/kern/assert.h b/osfmk/kern/assert.hindex 4224c9d..4a1fe94 100644--- a/osfmk/kern/assert.h@@ -1776,6 +1819,64 @@ index b2aae03..3b2058f 100644+ }+ }+}+diff --git a/osfmk/kern/stack.c b/osfmk/kern/stack.c+index 1111ccf..7a3c00b 100644+--- a/osfmk/kern/stack.c++++ b/osfmk/kern/stack.c+@@ -185,6 +185,24 @@ STARTUP(TUNABLES, STARTUP_RANK_MIDDLE, stack_init);+ * block.+ */+++/*++ * LINUX64 (xnulinux): an exception taken on an XNU kernel stack enters Linux's vectors, whose overflow check wants++ * bit THREAD_SHIFT (14) of sp clear, as on Linux's 2*THREAD_SIZE-aligned stacks. Hosted, each stack starts on a++ * 32 KiB boundary: the allocation is 32 KiB aligned and the stack sits 32 KiB above its start (guard pages first,++ * then unused wired pages, then the stack, then the upper guard page).++ */++extern uintptr_t linux64_hook;++static inline vm_offset_t++linux64_stack_lead(void)++{++ return linux64_hook ? 0x8000 : ptoa(2);++}++static inline vm_offset_t++linux64_stack_mask(void)++{++ return linux64_hook ? (stack_addr_mask | 0x7fff) : stack_addr_mask;++}+++ static vm_offset_t+ stack_alloc_internal(void)+ {+@@ -218,7 +236,7 @@ stack_alloc_internal(void)+ */++ kernel_memory_allocate(kernel_map, &stack,+- kernel_stack_size + ptoa(3), stack_addr_mask,++ kernel_stack_size + linux64_stack_lead() + ptoa(1), linux64_stack_mask(),+ flags, VM_KERN_MEMORY_STACK);++ /*+@@ -226,7 +244,7 @@ stack_alloc_internal(void)+ * guard pages. Skip past them to get the actual stack base address.+ */++- stack += ptoa(2);++ stack += linux64_stack_lead();+ }+ return stack;+ }+@@ -389,8 +407,8 @@ stack_collect(void)+ stack = (vm_offset_t)vm_map_trunc_page(+ stack,+ VM_MAP_PAGE_MASK(kernel_map));+- stack -= ptoa(2);+- kmem_free(kernel_map, stack, kernel_stack_size + ptoa(3),++ stack -= linux64_stack_lead();++ kmem_free(kernel_map, stack, kernel_stack_size + linux64_stack_lead() + ptoa(1),+ KMF_GUARD_FIRST | KMF_GUARD_LAST | KMF_GUARD_STACK);+ stack = 0;+diff --git a/osfmk/kern/startup.c b/osfmk/kern/startup.cindex a004862..8d4f8a1 100644--- a/osfmk/kern/startup.c