Skip to content

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

C

Hosted XNU: constrained workqueue parallelism >= 4 on the one hosted CPU (ld's dispatch_group_wait got no worker); vm-park.sh parks hardware/crash-looping daemons on the golden disk. Xcode clang compiles, links and runs hello world

Heyang Zhoue294ca38b489
diff --git a/scripts/vm-park.sh b/scripts/vm-park.sh
new file mode 100755
index 0000000..a9d05b9
--- /dev/null
+++ b/scripts/vm-park.sh
@@ -0,0 +1,18 @@
+#!/bin/bash
+# In the VM: park (move aside) launchd jobs on the golden full disk that need hardware this hosted system does not
+# have, or crash-loop and eat its one CPU; then mark the volume clean for XNU (fsck -fy). Names are plist basenames.
+set -u
+IMG=${IMG:-/var/tmp/full.img}
+PARK="com.apple.audiomxd com.apple.audio.coreaudiod com.apple.audio.AudioComponentRegistrar.daemon com.apple.audio.micactivityd
+ com.apple.audio.systemsoundserverd com.apple.audioanalyticsd com.apple.historicalaudiod com.apple.usbaudiod
+ com.apple.PerfPowerServices com.apple.PerfPowerServicesExtended com.apple.perfpowermetricd
+ com.apple.bluetoothd com.apple.mobileactivationd com.apple.xprotectd com.apple.XProtect.daemon.scan com.apple.MRTd
+ com.apple.systemstats.daily com.apple.systemstats.analysis com.apple.systemstats.microstackshot_periodic
+ com.apple.endpointsecurity.endpointsecurityd com.apple.MobileSoftwareUpdate.CleanupPreparePathService ${PARK_MORE:-}"
+sudo mount -o loop,rw,force -t hfsplus "$IMG" /mnt/sys || exit 1
+D=/mnt/sys/System/Library/LaunchDaemons; P=/mnt/sys/private/var/xnulinux-parked
+sudo mkdir -p $P
+n=0; for j in $PARK; do if [ -e $D/$j.plist ]; then sudo mv $D/$j.plist $P/ && n=$((n+1)); fi; done
+echo "parked $n now; $(ls $P | wc -l) parked in all; $(ls $D | wc -l) jobs left"
+sudo umount /mnt/sys
+sudo fsck.hfsplus -fy "$IMG" 2>&1 | tail -1; sync
diff --git a/scripts/vm-sysdisk-launchd.sh b/scripts/vm-sysdisk-launchd.sh
index 9158dc3..9f75cd4 100755
--- a/scripts/vm-sysdisk-launchd.sh
+++ b/scripts/vm-sysdisk-launchd.sh
@@ -24,7 +24,7 @@ if [ "${MODE:-launchd}" = launchd ]; then
sudo rm -rf /mnt/sys/usr/local
sudo find /mnt/sys/Library/LaunchDaemons /mnt/sys/Library/LaunchAgents -name '*.plist' ! -name 'com.apple.*' -delete 2>/dev/null || true
sudo mkdir -p /mnt/sys/private/var/xnulinux-parked
- for d in com.apple.loginwindow com.apple.WindowServer com.apple.watchdogd com.apple.iomfb_bics_daemon ${DISABLE:-}; do
+ for d in com.apple.loginwindow com.apple.WindowServer com.apple.watchdogd com.apple.iomfb_bics_daemon com.apple.audiomxd com.apple.audio.coreaudiod com.apple.PerfPowerServices com.apple.PerfPowerServicesExtended ${DISABLE:-}; do
[ -e /mnt/sys/System/Library/LaunchDaemons/$d.plist ] && sudo mv /mnt/sys/System/Library/LaunchDaemons/$d.plist /mnt/sys/private/var/xnulinux-parked/
done
else
diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patch
index 7298b4a..c65626f 100644
--- a/xnu-patches/0001-linux64-stage-hook.patch
+++ b/xnu-patches/0001-linux64-stage-hook.patch
@@ -400,6 +400,32 @@ index e5e58b5..6e6596d 100644
pthread_functions->pth_proc_hashinit(p);
}
+diff --git a/bsd/pthread/pthread_workqueue.c b/bsd/pthread/pthread_workqueue.c
+index cb27db6..2a73564 100644
+--- a/bsd/pthread/pthread_workqueue.c
++++ b/bsd/pthread/pthread_workqueue.c
+@@ -2117,6 +2117,21 @@ workq_open(struct proc *p, __unused struct workq_open_args *uap,
+ }
+
+ wq_max_cooperative_threads = num_cpus;
++ {
++ /*
++ * LINUX64 (xnulinux): hosted XNU has one CPU. With a parallelism of 1 a process whose main thread waits
++ * on work it queued (ld's layout: dispatch_group_wait) never got a worker admitted; allow a few
++ * constrained threads per QoS bucket (the cooperative pool stays at the CPU count), as a small multi-core machine would.
++ */
++ extern uintptr_t linux64_hook;
++ if (linux64_hook) {
++ for (int b = 0; b < WORKQ_NUM_QOS_BUCKETS; b++) {
++ if (wq_max_parallelism[b] < 4) {
++ wq_max_parallelism[b] = 4;
++ }
++ }
++ }
++ }
+
+ wq_init_constrained_limit = 0;
+ }
diff --git a/bsd/vfs/vfs_subr.c b/bsd/vfs/vfs_subr.c
index 229d6a1..e610b22 100644
--- a/bsd/vfs/vfs_subr.c