XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
Full userland fixes: vnode table sized from DRAM (k27's CONFIG_VNODES is 1024), idle with a passed deadline still yields to Linux, fsck until clean, journaled HFS+ system disk
Heyang Zhoua312b3ed743c
diff --git a/fki-ko/fki_mod.c b/fki-ko/fki_mod.cindex 75dfb79..80f2299 100644--- a/fki-ko/fki_mod.c+++ b/fki-ko/fki_mod.c@@ -509,13 +509,25 @@ static void fki_idle_body(void *p)local_irq_disable();}+static void fki_yield_body(void *p)+{+ local_irq_enable();+ preempt_enable();+ cond_resched();+ preempt_disable();+ local_irq_disable();+}+void fki_idle_until(fki_u64 counter_deadline, fki_u64 max_ns){u64 now = __arch_counter_get_cntvct(), freq = arch_timer_get_cntfrq(), ns;struct fki_idle_args a;- if (counter_deadline <= now)+ if (counter_deadline <= now) {+ /* already due: still let Linux take its interrupts and reschedule (an idle loop that never sleeps starves it) */+ fki_linux_runs(fki_yield_body, NULL);return;+ }ns = div64_u64((counter_deadline - now) * 1000000ULL, freq) * 1000ULL;if (ns > max_ns)ns = max_ns;@@ -524,15 +536,6 @@ void fki_idle_until(fki_u64 counter_deadline, fki_u64 max_ns)}EXPORT_SYMBOL(fki_idle_until);-static void fki_yield_body(void *p)-{- local_irq_enable();- preempt_enable();- cond_resched();- preempt_disable();- local_irq_disable();-}-void fki_yield(void){fki_linux_runs(fki_yield_body, NULL);diff --git a/scripts/mk-fulldisk.sh b/scripts/mk-fulldisk.shindex 9e224fe..69c1561 100644--- a/scripts/mk-fulldisk.sh+++ b/scripts/mk-fulldisk.sh@@ -10,8 +10,8 @@ XCODE=${XCODE:-/Applications/Xcode_27.app}V="bash scripts/vm.sh"$V "set -e; sudo umount /mnt/full 2>/dev/null || true; rm -f /var/tmp/full.img; truncate -s $SIZE /var/tmp/full.img- mkfs.hfsplus -v xnulinux-full /var/tmp/full.img >/dev/null; sudo mkdir -p /mnt/full- sudo mount -o loop,rw -t hfsplus /var/tmp/full.img /mnt/full; df -h /mnt/full | tail -1"+ mkfs.hfsplus -J -v xnulinux-full /var/tmp/full.img >/dev/null; sudo mkdir -p /mnt/full+ sudo mount -o loop,rw,force -t hfsplus /var/tmp/full.img /mnt/full; df -h /mnt/full | tail -1"# bsdtar --exclude patterns are not anchored ("Library/Preferences" also drops System/Library/Preferences), so the# top-level choices are made by listing what goes in; the exclusions left are unique paths.diff --git a/scripts/vm-m2-kbisect.sh b/scripts/vm-m2-kbisect.shindex b8e1339..f0a0340 100755--- a/scripts/vm-m2-kbisect.sh+++ b/scripts/vm-m2-kbisect.sh@@ -7,7 +7,9 @@ make -s -C /lib/modules/$(uname -r)/build M=$PWD/xnu-ko KBUILD_EXTRA_SYMBOLS=$PWsudo dmesg -n ${CONLEVEL:-8}sudo rmmod xnu 2>/dev/null; sudo rmmod fki 2>/dev/null; sudo insmod fki-ko/fki.ko# a hosted XNU never unmounts its read-write root: check the host disk before each boot-[ -n "$HOSTDISK" ] && sudo fsck.hfsplus -fy "$HOSTDISK" 2>&1 | tail -1+if [ -n "$HOSTDISK" ]; then+ for t in 1 2 3; do r=$(sudo fsck.hfsplus -fy "$HOSTDISK" 2>&1 | tail -1); echo "fsck: $r"; case "$r" in *"appears to be OK"*) break;; esac; done+fi# XNU's DRAM is one physically contiguous block (GBs): free the page cache and compact first, or the allocation failssync; echo 3 | sudo tee /proc/sys/vm/drop_caches >/dev/null; echo 1 | sudo tee /proc/sys/vm/compact_memory >/dev/nullfor K in "$@"; dodiff --git a/scripts/vm-sysdisk-launchd.sh b/scripts/vm-sysdisk-launchd.shindex a84e244..9158dc3 100755--- a/scripts/vm-sysdisk-launchd.sh+++ b/scripts/vm-sysdisk-launchd.sh@@ -4,7 +4,7 @@set -euo pipefailIMG=${IMG:-/var/tmp/sys.img}sudo fsck.hfsplus -fy $IMG 2>&1 | tail -1-sudo mkdir -p /mnt/sys; sudo mount -o loop,rw -t hfsplus $IMG /mnt/sys+sudo mkdir -p /mnt/sys; sudo mount -o loop,rw,force -t hfsplus $IMG /mnt/sysif [ "${MODE:-launchd}" = launchd ]; then# FULL=1: a full userland disk (scripts/mk-fulldisk.sh) already has launchd and every daemon; /private/var comes# from Apple's pristine data-volume template, the root symlinks are made herediff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patchindex e7cc8c8..49d2c96 100644--- a/xnu-patches/0001-linux64-stage-hook.patch+++ b/xnu-patches/0001-linux64-stage-hook.patch@@ -200,6 +200,32 @@ index d24535d..5c2aafb 100644/* make sure (base, size) is a valid range and will not overflow */assert(*size < (UINT64_MAX - *base));+diff --git a/bsd/dev/unix_startup.c b/bsd/dev/unix_startup.c+index ec19ee7..6baf626 100644+--- a/bsd/dev/unix_startup.c++++ b/bsd/dev/unix_startup.c+@@ -186,6 +186,21 @@ bsd_startupearly(void)+ }+ #endif+ }++ {++ /* LINUX64 (xnulinux): hosted, size the vnode table from the DRAM xnu.ko gave us (sane_size may not be set yet) */++ extern uintptr_t linux64_hook;++ extern uint64_t max_mem;++ if (linux64_hook) {++ kprintf("linux64: desiredvnodes %d (sane_size 0x%llx max_mem 0x%llx)\n", desiredvnodes, (uint64_t)sane_size, (uint64_t)max_mem);++ /* the k27 config's CONFIG_VNODES (1024) is for a small device; a full macOS userland needs far more */++ if (desiredvnodes < (int)(max_mem / 65536) + 1024) {++ desiredvnodes = (int)(max_mem / 65536) + 1024;++ if (desiredvnodes > 263168) {++ desiredvnodes = 263168;++ }++ }++ }++ }+ vnodes_sized = 1;+ }+ }diff --git a/bsd/kern/bsd_init.c b/bsd/kern/bsd_init.cindex 5362b32..cd43d8e 100644--- a/bsd/kern/bsd_init.c