Skip to content

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

C

xnu.ko: don't reload a stale TTBR0 when a run ends from an upcall (#38)

kk19879468251ce727
diff --git a/xnu-ko/world.S b/xnu-ko/world.S
index bb0f054..c6678ef 100644
--- a/xnu-ko/world.S
+++ b/xnu-ko/world.S
@@ -81,11 +81,20 @@ xnu_world_call:
W(x9)
mov x19, #0
b 2f
-xnu_world_abort: /* from the upcall: Linux keys and TPIDR are live */
+/*
+ * From the upcall: Linux keys, TPIDR and user half are live. The upcall's TO_LINUX_MM already switched, and Linux may
+ * have scheduled during the host call since, so the TTBR0 saved at the last TO_XNU_MM is stale: loading it again would
+ * leave this CPU on an mm Linux has moved off (the next task with the same mm faults on its user addresses for ever).
+ */
+xnu_world_abort:
+ W(x9)
+ mov x19, #1
+ b 3f
+xnu_world_abort_mm: /* from a fault in XNU: XNU's user half is live */
W(x9)
mov x19, #1
2: TO_LINUX_MM
- ldr x18, [x9, #464]
+3: ldr x18, [x9, #464]
ldr x10, [x9, #216]
msr tpidr_el0, x10
ldr x10, [x9, #224]
@@ -113,7 +122,7 @@ xnu_world_fault_abort: /* exception return here: XNU keys live, XNU stack */
add x0, x9, #240 /* load Linux keys */
add x1, x9, #328 /* save XNU keys */
bl fki_pac_swap
- b xnu_world_abort
+ b xnu_world_abort_mm
.size xnu_world_fault_abort, . - xnu_world_fault_abort
.globl xnu_world_upcall
diff --git a/xnu-ko/world.h b/xnu-ko/world.h
index 13a2359..6a9a838 100644
--- a/xnu-ko/world.h
+++ b/xnu-ko/world.h
@@ -29,7 +29,7 @@ void xnu_world_upcall(void);
/* Pure EL0 host variants (FKI_CAP_EL0): no EL1 system instructions; slot from TPIDRRO_EL0. */
long xnu_world_call_el0(unsigned long fn, unsigned long arg);
void xnu_world_upcall_el0(void);
-/* Where an XNU fault that cannot be handled returns to (still with XNU's keys): back to Linux's, then xnu_world_abort. */
+/* Where an XNU fault that cannot be handled returns to (still with XNU's keys and user half): back to Linux's, then out. */
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.c
index e2ceea1..a1596e7 100644
--- a/xnu-ko/xnu_main.c
+++ b/xnu-ko/xnu_main.c
@@ -411,7 +411,7 @@ static int xnu_fault(unsigned long far, unsigned long esr, void *regs)
/*
* 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.
+ * xnu_world_fault_abort, which puts Linux's keys back and leaves through xnu_world_abort_mm.
*/
unsigned long pc = fki_regs_pc(regs);
if (pc < XNU_VA_LO || pc >= XNU_VA_HI)