XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
EL0 path keeps the host's x18 (Linux's shadow call stack pointer): saved at EL0 entry, restored on EL0 exceptions; kprintf per EL0 exception back
Heyang Zhou2a84cf941d2e
diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patchindex 10d2f63..807872e 100644--- a/xnu-patches/0001-linux64-stage-hook.patch+++ b/xnu-patches/0001-linux64-stage-hook.patch@@ -1094,7 +1094,7 @@ index d10d7a1..15e2fb4 100644cpc_cpu_transition(CPC_CPU_EARLY_INIT, cdp);#endif /* CONFIG_CPU_COUNTERS */diff --git a/osfmk/arm64/cswitch.s b/osfmk/arm64/cswitch.s-index a191eb3..127d057 100644+index a191eb3..9e745b5 100644--- a/osfmk/arm64/cswitch.s+++ b/osfmk/arm64/cswitch.s@@ -185,7 +185,8 @@@@ -1157,7 +1157,7 @@ index a191eb3..127d057 100644LOAD_KERN_STACK_TOP dst=x1, src=x0, tmp=x2 // Get the top of the kernel stackload_general_registers x1, 2set_process_dependent_keys_and_sync_context x0, x1, x2, x3, w4-@@ -444,3 +450,126 @@ LEXT(machine_set_current_thread)+@@ -444,3 +450,128 @@ LEXT(machine_set_current_thread)/* vim: set ts=4: */@@ -1173,7 +1173,7 @@ index a191eb3..127d057 100644+ .align 3+ .globl EXT(linux64_el0_save)+LEXT(linux64_el0_save)-+ .quad 0, 0++ .quad 0, 0, 0 // Linux's SP_EL0 (current), VBAR_EL1, x18 (its shadow call stack)++ .text+ .align 2@@ -1186,6 +1186,7 @@ index a191eb3..127d057 100644+ str x10, [x9, #0]+ mrs x10, VBAR_EL1+ str x10, [x9, #8]++ str x18, [x9, #16]+ adrp x10, EXT(linux64_el0_vectors)@page+ add x10, x10, EXT(linux64_el0_vectors)@pageoff+ msr VBAR_EL1, x10@@ -1279,6 +1280,7 @@ index a191eb3..127d057 100644+ msr SP_EL0, x10+ ldr x10, [x9, #8]+ msr VBAR_EL1, x10++ ldr x18, [x9, #16] // the host's x18 again (user code owns x18 at EL0)+ isb+ mov x0, sp+ mov x29, xzr@@ -1762,10 +1764,10 @@ index d211433..dc247da 100644// No need for atomic - These are per CPU and interrupts are disableddiff --git a/osfmk/arm64/sleh.c b/osfmk/arm64/sleh.c-index d8f1b19..d340172 100644+index d8f1b19..3baaa28 100644--- a/osfmk/arm64/sleh.c+++ b/osfmk/arm64/sleh.c-@@ -3646,3 +3646,115 @@ sleh_panic_lockdown_should_initiate_el1_sp0_sync(uint64_t esr, uint64_t elr,+@@ -3646,3 +3646,116 @@ sleh_panic_lockdown_should_initiate_el1_sp0_sync(uint64_t esr, uint64_t elr,}__SECURITY_STACK_DISALLOWED_POP#endif /* CONFIG_SPTM */@@ -1867,6 +1869,7 @@ index d8f1b19..d340172 100644+ linux64_dbg[12] = f->far;+ linux64_dbg[13] = f->pc;+ linux64_dbg[15] = 1;++ kprintf("linux64: EL0 exception esr 0x%llx far 0x%llx pc 0x%llx x16 %lld\n", f->esr, f->far, f->pc, f->x[16]);+ for (unsigned i = 0; i < 29; i++) {+ set_saved_state_reg(ss, i, f->x[i]);+ }