Skip to content

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

C

XNU patch: ml_wait_max_cpus does not wait for the CPU kext while hosted (thread dump: kernel_bootstrap_thread blocked in kperf_init_early -> ml_wait_max_cpus)

Heyang Zhou5858716026bb
diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patch
index 935646b..15a5fe1 100644
--- a/xnu-patches/0001-linux64-stage-hook.patch
+++ b/xnu-patches/0001-linux64-stage-hook.patch
@@ -498,7 +498,7 @@ index 743117b..9dc298b 100644
"msr SPSel, #1 \n"
"mov %0, sp \n"
diff --git a/osfmk/arm/machine_routines_common.c b/osfmk/arm/machine_routines_common.c
-index b65f76c..bc3207b 100644
+index b65f76c..11b510a 100644
--- a/osfmk/arm/machine_routines_common.c
+++ b/osfmk/arm/machine_routines_common.c
@@ -1094,6 +1094,30 @@ ml_set_interrupts_enabled_with_debug(boolean_t enable, boolean_t __unused debug)
@@ -532,6 +532,20 @@ index b65f76c..bc3207b 100644
state = __builtin_arm_rsr("DAIF");
if (__improbable(!(state & DAIF_DEBUGF))) {
+@@ -1384,6 +1408,13 @@ ml_wait_max_cpus(void)
+ {
+ assert(lockdown_done);
+ lck_mtx_lock(&max_cpus_lock);
++ {
++ /* LINUX64 (xnulinux): hosted without the CPU kext that calls ml_set_max_cpus(); one CPU, known now. */
++ extern uintptr_t linux64_hook;
++ if (linux64_hook) {
++ max_cpus_initialized = MAX_CPUS_SET;
++ }
++ }
+ while (max_cpus_initialized != MAX_CPUS_SET) {
+ max_cpus_initialized = MAX_CPUS_WAIT;
+ lck_mtx_sleep(&max_cpus_lock, LCK_SLEEP_DEFAULT, &max_cpus_initialized, THREAD_UNINT);
diff --git a/osfmk/arm/pmap/pmap.c b/osfmk/arm/pmap/pmap.c
index 79d0bc1..ac5b387 100644
--- a/osfmk/arm/pmap/pmap.c