Skip to content

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

C

Upcalls keep XNU's user TLS registers: Linux's context switch (an upcall that sleeps: idle, host disk I/O) zeroes TPIDRRO_EL0, and dyld's __error read the TSD through it (null + 8)

Heyang Zhou0f795b3032bf
diff --git a/xnu-ko/world.S b/xnu-ko/world.S
index f1da815..d5dce23 100644
--- a/xnu-ko/world.S
+++ b/xnu-ko/world.S
@@ -120,6 +120,14 @@ xnu_world_upcall: /* x0 = stage, called by XNU with blr */
mov x19, x0
mov x20, x1
mov x21, x2
+ /*
+ * The current XNU thread's user TLS registers: Linux may schedule during the upcall (idle, file I/O), and its
+ * context switch rewrites TPIDR_EL0 and zeroes TPIDRRO_EL0 (where Darwin user code keeps its TSD pointer).
+ */
+ mrs x10, tpidr_el0
+ str x10, [x9, #448]
+ mrs x10, tpidrro_el0
+ str x10, [x9, #456]
TO_LINUX_MM
add x0, x9, #240 /* load Linux keys */
add x1, x9, #328 /* save XNU keys */
@@ -130,6 +138,10 @@ xnu_world_upcall: /* x0 = stage, called by XNU with blr */
bl xnu_hook
cbnz x0, xnu_world_abort
W(x9)
+ ldr x10, [x9, #448]
+ msr tpidr_el0, x10
+ ldr x10, [x9, #456]
+ msr tpidrro_el0, x10
TO_XNU_MM
add x0, x9, #328 /* load XNU keys */
add x1, x9, #240 /* save Linux keys */
diff --git a/xnu-ko/world.h b/xnu-ko/world.h
index d7744af..f13a5ae 100644
--- a/xnu-ko/world.h
+++ b/xnu-ko/world.h
@@ -11,7 +11,9 @@ struct xnu_world {
struct fki_pac_keys xnu_keys; /* 328 */
unsigned long long linux_ttbr0, linux_tcr; /* 416, 424: Linux's user address space while XNU runs */
unsigned long long xnu_ttbr0, xnu_tcr; /* 432, 440: XNU's (its user pmaps, its T0SZ) */
+ unsigned long long xnu_tpidr, xnu_tpidrro; /* 448, 456: XNU's user TLS registers across an upcall */
};
+_Static_assert(__builtin_offsetof(struct xnu_world, xnu_tpidrro) == 456, "xnu_world layout");
_Static_assert(__builtin_offsetof(struct xnu_world, linux_ttbr0) == 416, "xnu_world layout");
_Static_assert(sizeof(struct fki_pac_keys) == 88, "fki_pac_keys layout");
_Static_assert(__builtin_offsetof(struct xnu_world, xnu_keys) == 328, "xnu_world layout");