Skip to content

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

C

world switch: preserve the user TLS registers XNU writes

Heyang Zhou82eb1f1335d8
diff --git a/xnu-ko/world.S b/xnu-ko/world.S
index 6cd7268..24bd4ab 100644
--- a/xnu-ko/world.S
+++ b/xnu-ko/world.S
@@ -23,6 +23,10 @@ xnu_world_call:
bl fki_enter /* no preemption, no IRQs */
W(x9)
str x0, [x9, #104]
+ mrs x10, tpidr_el0 /* XNU writes the user TLS registers of the thread it believes current */
+ str x10, [x9, #216]
+ mrs x10, tpidrro_el0
+ str x10, [x9, #224]
/* XNU runs on the Linux task stack: Linux's entry code treats any other stack as overflow. */
mov x0, x20
blr x19
@@ -33,7 +37,11 @@ xnu_world_call:
xnu_world_abort: /* from the upcall: Linux keys and TPIDR are live */
W(x9)
mov x19, #1
-2: ldr x10, [x9, #96]
+2: ldr x10, [x9, #216]
+ msr tpidr_el0, x10
+ ldr x10, [x9, #224]
+ msr tpidrro_el0, x10
+ ldr x10, [x9, #96]
mov sp, x10
ldr x0, [x9, #104]
add x1, x9, #240
diff --git a/xnu-ko/world.h b/xnu-ko/world.h
index 6e1a2d2..ac36c4e 100644
--- a/xnu-ko/world.h
+++ b/xnu-ko/world.h
@@ -4,8 +4,8 @@
struct xnu_world {
unsigned long long linux_regs[14]; /* 0: x19..x28, x29, x30, sp, irq cookie */
unsigned long long xnu_regs[13]; /* 112: x19..x28, x29, x30, sp */
- unsigned long long linux_tpidr; /* 216 */
- unsigned long long xnu_tpidr; /* 224 */
+ unsigned long long linux_tpidr0; /* 216: TPIDR_EL0 */
+ unsigned long long linux_tpidrro0; /* 224: TPIDRRO_EL0 */
unsigned long long xnu_stack_top; /* 232 */
struct fki_pac_keys linux_keys; /* 240 */
struct fki_pac_keys xnu_keys; /* 328 */
diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.c
index 0adbe31..03f7766 100644
--- a/xnu-ko/xnu_main.c
+++ b/xnu-ko/xnu_main.c
@@ -190,7 +190,6 @@ static int run_m2a(void)
*(u64_t *)a_hook = (u64_t)xnu_world_upcall;
*(u64_t *)a_slide = img.slide;
- xnu_W.xnu_tpidr = 0;
logs("xnu: entering arm_init");
long r = xnu_world_call(a_init, (unsigned long)ba);
logkv(r ? "xnu: run ended by stage hook, r =" : "xnu: arm_init returned (unexpected), r =", (u64_t)r);