Skip to content

XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)

C

Kernel faults inside copyin/copyout that vm_fault cannot resolve return through XNU's copyio recovery (x0 = EFAULT) instead of ending the run (linux64_kernel_fault_recover, fki_regs_set_reg)

Heyang Zhoud36a59d58378
diff --git a/fki-ko/fki.h b/fki-ko/fki.h
index 73b13ec..fb5e563 100644
--- a/fki-ko/fki.h
+++ b/fki-ko/fki.h
@@ -88,6 +88,7 @@ void fki_msleep(unsigned int ms);
unsigned long fki_regs_pc(void *regs);
unsigned long fki_regs_lr(void *regs);
void fki_regs_set_pc(void *regs, unsigned long pc);
+void fki_regs_set_reg(void *regs, unsigned int n, unsigned long v); /* x0..x30 */
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);
/* Read 8 bytes of RAM by physical address (debugging: walking a foreign page table). */
diff --git a/fki-ko/fki_mod.c b/fki-ko/fki_mod.c
index b49ea83..3ddb1c1 100644
--- a/fki-ko/fki_mod.c
+++ b/fki-ko/fki_mod.c
@@ -277,6 +277,8 @@ 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);
+void fki_regs_set_reg(void *regs, unsigned int n, unsigned long v) { if (n < 31) ((struct pt_regs *)regs)->regs[n] = v; }
+EXPORT_SYMBOL(fki_regs_set_reg);
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.h
index 73b13ec..fb5e563 100644
--- a/linux/include/linux/fki.h
+++ b/linux/include/linux/fki.h
@@ -88,6 +88,7 @@ void fki_msleep(unsigned int ms);
unsigned long fki_regs_pc(void *regs);
unsigned long fki_regs_lr(void *regs);
void fki_regs_set_pc(void *regs, unsigned long pc);
+void fki_regs_set_reg(void *regs, unsigned int n, unsigned long v); /* x0..x30 */
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);
/* Read 8 bytes of RAM by physical address (debugging: walking a foreign page table). */
diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.c
index b039e17..3a58f57 100644
--- a/xnu-ko/xnu_main.c
+++ b/xnu-ko/xnu_main.c
@@ -144,6 +144,7 @@ static void walk_log(const char *what, u64_t va, const unsigned long long *l1)
*/
static volatile int in_xnu;
static unsigned long (*xnu_kfault)(unsigned long far, unsigned long esr);
+static unsigned long (*xnu_kfault_recover)(unsigned long pc, unsigned long far, unsigned long esr);
static unsigned long kfaults;
#define XNU_VA_LO 0xffffffe000000000ULL
@@ -187,6 +188,15 @@ static int xnu_fault(unsigned long far, unsigned long esr, void *regs)
unsigned long pc = fki_regs_pc(regs);
if (pc < XNU_VA_LO || pc >= XNU_VA_HI)
return 1;
+ /* inside copyin/copyout: the routine's recovery path returns EFAULT to its caller, as XNU's own abort handler does */
+ if (xnu_kfault_recover) {
+ unsigned long rpc = xnu_kfault_recover(pc, far, esr);
+ if (rpc) {
+ fki_regs_set_reg(regs, 0, 14); /* EFAULT */
+ fki_regs_set_pc(regs, rpc);
+ return 0;
+ }
+ }
in_xnu = 0;
logs("xnu: XNU took a fault it cannot handle; ending the run");
logkv("xnu: pc ", pc);
@@ -554,6 +564,7 @@ static int enter_arm_init(struct boot_args_v2 *ba)
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");
+ xnu_kfault_recover = (void *)macho_sym(&img, "_linux64_kernel_fault_recover");
if (xnu_kfault && fki_fault_register(0xffffffe000000000ULL, 0xfffffffc00000000ULL, xnu_fault) == 0)
logs("xnu: kernel faults on XNU addresses go to XNU's vm_fault");
{
diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patch
index ddcdde6..ff4f38a 100644
--- a/xnu-patches/0001-linux64-stage-hook.patch
+++ b/xnu-patches/0001-linux64-stage-hook.patch
@@ -2014,10 +2014,10 @@ index d211433..dc247da 100644
// 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..a97c679 100644
+index d8f1b19..be334a5 100644
--- a/osfmk/arm64/sleh.c
+++ b/osfmk/arm64/sleh.c
-@@ -3646,3 +3646,140 @@ sleh_panic_lockdown_should_initiate_el1_sp0_sync(uint64_t esr, uint64_t elr,
+@@ -3646,3 +3646,161 @@ sleh_panic_lockdown_should_initiate_el1_sp0_sync(uint64_t esr, uint64_t elr,
}
__SECURITY_STACK_DISALLOWED_POP
#endif /* CONFIG_SPTM */
@@ -2078,6 +2078,27 @@ index d8f1b19..a97c679 100644
+}
+
+/*
++ * LINUX64 (xnulinux): a kernel fault vm_fault could not resolve, taken inside a copyin/copyout routine: the pc its
++ * copyio recovery entry returns through (the routine then returns an error; x0 = EFAULT), 0 when pc has none.
++ */
++uint64_t linux64_kernel_fault_recover(uint64_t pc, uint64_t far, uint64_t esr);
++uint64_t
++linux64_kernel_fault_recover(uint64_t pc, uint64_t far, uint64_t esr)
++{
++ struct copyio_recovery_entry *e = find_copyio_recovery_entry(pc);
++ thread_t thread = current_thread();
++
++ if (e == NULL) {
++ return 0;
++ }
++ if (thread != THREAD_NULL) {
++ thread->machine.recover_esr = esr;
++ thread->machine.recover_far = far;
++ }
++ return (uint64_t)copyio_recover_table + (uint64_t)e->cre_recovery;
++}
++
++/*
+ * LINUX64 (xnulinux): the first return of a new user thread (thread_bootstrap_return) while hosted. The pending ASTs
+ * run as on the way to user space (for the first BSD thread: bsdinit_task, which execs launchd); then the host is told
+ * where user space would start. Entering EL0 is M3.