XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
XNU patch: hosted arm64_thread_exception_return also goes through C (task_wait_to_return and friends reach user space that way)
Heyang Zhou8771cabacc52
diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patchindex 325b072..48a0607 100644--- a/xnu-patches/0001-linux64-stage-hook.patch+++ b/xnu-patches/0001-linux64-stage-hook.patch@@ -1153,7 +1153,7 @@ index b0772a7..ea5e679 100644mov sp, x1 // Set the stack pointer to the interrupt stack.endmacrodiff --git a/osfmk/arm64/locore.s b/osfmk/arm64/locore.s-index 13d2819..5f4700f 100644+index 13d2819..d71ba86 100644--- a/osfmk/arm64/locore.s+++ b/osfmk/arm64/locore.s@@ -144,7 +144,8 @@@@ -1339,17 +1339,23 @@ index 13d2819..5f4700f 100644#if CONFIG_DTRACEbl EXT(dtrace_thread_bootstrap)#endif-@@ -1816,7 +1839,8 @@ LEXT(thread_bootstrap_return)+@@ -1816,7 +1839,14 @@ LEXT(thread_bootstrap_return).globl EXT(arm64_thread_exception_return)LEXT(arm64_thread_exception_return)ARM64_PROLOG- mrs x0, TPIDR_EL1++ // LINUX64 (xnulinux): hosted, a return to user space goes through C (user space is not hosted yet)++ adrp x9, _linux64_hook@PAGE++ ldr x9, [x9, _linux64_hook@PAGEOFF]++ cbz x9, 1f++ b EXT(linux64_thread_bootstrap_return)++1:+ adrp x0, _linux64_tpidr@PAGE+ ldr x0, [x0, _linux64_tpidr@PAGEOFF]LOAD_USER_PCB dst=x21, src=x0, tmp=x28mov x28, xzr-@@ -1835,7 +1859,8 @@ check_user_asts:+@@ -1835,7 +1865,8 @@ check_user_asts:bl EXT(__asan_handle_no_return)POP_FRAME_WITHOUT_LR#endif /* KASAN_TBI */@@ -1359,7 +1365,7 @@ index 13d2819..5f4700f 100644movn w2, #0str w2, [x3, TH_IOTIER_OVERRIDE] // Reset IO tier override to -1 before returning to user-@@ -1927,7 +1952,8 @@ no_asts:+@@ -1927,7 +1958,8 @@ no_asts:PUSH_FRAMEbl EXT(arm_debug_set) // Establish thread debug state in live regsPOP_FRAME_WITHOUT_LR@@ -1369,7 +1375,7 @@ index 13d2819..5f4700f 100644ldr x4, [x3, ACT_CPUDATAP] // Reload CPU data pointerL_skip_user_set_debug_state:-@@ -1937,7 +1963,8 @@ L_skip_user_set_debug_state:+@@ -1937,7 +1969,8 @@ L_skip_user_set_debug_state:exception_return:msr DAIFSet, #DAIFSC_ALL // Disable exceptionsexception_return_unint:@@ -1379,7 +1385,7 @@ index 13d2819..5f4700f 100644exception_return_unint_tpidr_x3:mov sp, x21 // Reload the pcb pointer-@@ -2006,7 +2033,9 @@ Lexception_return_restore_registers:+@@ -2006,7 +2039,9 @@ Lexception_return_restore_registers:// SPSR_EL1.M != EL0: no SME state to restorebne Lno_sme_saved_state@@ -1390,7 +1396,7 @@ index 13d2819..5f4700f 100644add x3, x3, ACT_UMATRIX_HDRldr x2, [x3]cbz x2, Lno_sme_saved_state-@@ -2071,7 +2100,8 @@ Lno_gcr_mask_el0_reset:+@@ -2071,7 +2106,8 @@ Lno_gcr_mask_el0_reset:#endif /* HAS_MTE */// If sync_on_cswitch and ERET is not a CSE, issue an ISB now. Unconditionally clear the// sync_on_cswitch flag.@@ -1400,7 +1406,7 @@ index 13d2819..5f4700f 100644ldr x1, [x1, ACT_CPUDATAP]// Redefined for backporting.-@@ -2095,7 +2125,8 @@ Lno_gcr_mask_el0_reset:+@@ -2095,7 +2131,8 @@ Lno_gcr_mask_el0_reset:mrs x2, SPSR_EL1tst x2, #(PSR64_MODE_EL_MASK)b.ne 1f@@ -1410,7 +1416,7 @@ index 13d2819..5f4700f 100644ldr w2, [x2, TH_ARM_MACHINE_FLAGS]tbz w2, #ARM_MACHINE_THREAD_DISABLE_USER_JOP_SHIFT, 1fmrs x2, SCTLR_EL1-@@ -2184,7 +2215,8 @@ user_take_ast:+@@ -2184,7 +2221,8 @@ user_take_ast:.text.align 2preempt_underflow:@@ -1420,7 +1426,7 @@ index 13d2819..5f4700f 100644str x0, [sp, #-16]! // We'll print thread pointeradr x0, L_underflow_str // Format stringCALL_EXTERN panic // Game over-@@ -2197,7 +2229,8 @@ L_underflow_str:+@@ -2197,7 +2235,8 @@ L_underflow_str:.text.align 2preempt_count_notzero:@@ -1430,7 +1436,7 @@ index 13d2819..5f4700f 100644str x0, [sp, #-16]! // We'll print thread pointerldr w0, [x0, ACT_PREEMPT_CNT]str w0, [sp, #8]-@@ -2457,7 +2490,8 @@ ppl_exit:+@@ -2457,7 +2496,8 @@ ppl_exit:add x15, x15, x10, lsl #32/* Load the preemption count. */@@ -1440,7 +1446,7 @@ index 13d2819..5f4700f 100644ldr w12, [x10, ACT_PREEMPT_CNT]/* Detect underflow */-@@ -2493,7 +2527,8 @@ Lno_preempt_underflow:+@@ -2493,7 +2533,8 @@ Lno_preempt_underflow:mov w12, #0/* Restore the thread pointer into x10. */@@ -1450,7 +1456,7 @@ index 13d2819..5f4700f 100644Lskip_collect_measurement:#endif /* SCHED_HYGIENE_DEBUG */-@@ -2743,7 +2778,8 @@ LEXT(_sptm_pre_entry_hook)+@@ -2743,7 +2784,8 @@ LEXT(_sptm_pre_entry_hook)stp x6, x7, [sp, #0x30]/* Increase the preemption count. */@@ -1460,7 +1466,7 @@ index 13d2819..5f4700f 100644cbz x9, Lskip_preemption_check_sptmhookldr w10, [x9, ACT_PREEMPT_CNT]add w10, w10, #1-@@ -2803,7 +2839,8 @@ LEXT(_sptm_post_exit_hook)+@@ -2803,7 +2845,8 @@ LEXT(_sptm_post_exit_hook)/* Load the preemption count. */@@ -1470,7 +1476,7 @@ index 13d2819..5f4700f 100644cbz x0, Lsptm_skip_ast_taken_sptmhookldr w12, [x0, ACT_PREEMPT_CNT]-@@ -2834,7 +2871,8 @@ Lno_preempt_underflow_sptmhook:+@@ -2834,7 +2877,8 @@ Lno_preempt_underflow_sptmhook:mov w12, #0/* Restore x0 as the thread pointer */@@ -1480,7 +1486,7 @@ index 13d2819..5f4700f 100644Lskip_collect_measurement_sptmhook:#endif /* SCHED_HYGIENE_DEBUG */-@@ -2916,7 +2954,9 @@ LEXT(panic_lockdown_record_debug_data)+@@ -2916,7 +2960,9 @@ LEXT(panic_lockdown_record_debug_data)mov x1, spstr x1, [x0, #PANIC_LOCKDOWN_INITIATOR_STATE_INITIATOR_SP]