Skip to content

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

C

EL0 exceptions hand XNU the user's PSTATE without the DAIF bits we mask at EL0 (a user abort otherwise panics as non-interruptible); log TTBR0/ttep per EL0 exception

Heyang Zhoubba3ef3570b9
diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patch
index f09ba9d..9397acd 100644
--- a/xnu-patches/0001-linux64-stage-hook.patch
+++ b/xnu-patches/0001-linux64-stage-hook.patch
@@ -1764,10 +1764,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..3baaa28 100644
+index d8f1b19..c977b9c 100644
--- a/osfmk/arm64/sleh.c
+++ b/osfmk/arm64/sleh.c
-@@ -3646,3 +3646,116 @@ sleh_panic_lockdown_should_initiate_el1_sp0_sync(uint64_t esr, uint64_t elr,
+@@ -3646,3 +3646,119 @@ sleh_panic_lockdown_should_initiate_el1_sp0_sync(uint64_t esr, uint64_t elr,
}
__SECURITY_STACK_DISALLOWED_POP
#endif /* CONFIG_SPTM */
@@ -1869,7 +1869,8 @@ index d8f1b19..3baaa28 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]);
++ kprintf("linux64: EL0 exception esr 0x%llx far 0x%llx pc 0x%llx x16 %lld ttbr0 0x%llx ttep 0x%llx\n", f->esr, f->far, f->pc, f->x[16],
++ __builtin_arm_rsr64("TTBR0_EL1"), (uint64_t)thread->map->pmap->ttep);
+ for (unsigned i = 0; i < 29; i++) {
+ set_saved_state_reg(ss, i, f->x[i]);
+ }
@@ -1878,7 +1879,9 @@ index d8f1b19..3baaa28 100644
+ set_saved_state_lr(ss, f->x[30]);
+ set_saved_state_sp(ss, f->sp);
+ set_saved_state_pc(ss, f->pc);
-+ set_saved_state_cpsr(ss, (uint32_t)f->cpsr);
++ /* EL0 runs with IRQ/FIQ masked while hosted (no XNU interrupts at EL0 yet); XNU must see the user's own PSTATE,
++ * or its abort path takes the user fault for one from a non-interruptible context */
++ set_saved_state_cpsr(ss, (uint32_t)f->cpsr & ~(uint32_t)0x3c0);
+ linux64_dbg[15] = 3;
+ sleh_synchronous(thread->machine.contextData, f->esr, (vm_offset_t)f->far, false);
+ linux64_dbg[15] = 4;