XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
Hosted XNU: vfork for other Darwin userlands, and a halt or restart from inside ends the run (#13)
CommaBot68dfae0650ab
diff --git a/scripts/xnulinux-boot.sh b/scripts/xnulinux-boot.shindex 3ec088c..e9848e7 100755--- a/scripts/xnulinux-boot.sh+++ b/scripts/xnulinux-boot.sh@@ -129,8 +129,8 @@ doneecho "booting: console scripts/xnu-console.sh, screen tools/hosted/fb2png.py $FB_DUMP"[ $WAIT = 1 ] || exit 0-# --wait: until the run ends (insmod returns), then keep its kernel log and say how it ended: 0 a halt or a stop asked for,-# 2 an XNU panic, 3 the run ended otherwise+# --wait: until the run ends (insmod returns), then keep its kernel log and say how it ended: 0 a halt (from inside, or+# a stop asked for), 2 an XNU panic, 4 a restart the hosted system asked for, 3 the run ended otherwisecollect() { # the run's kernel log, kept; the status says how it endedlocal LOGDIR=/var/log/xnulinux LOG# insmod has returned but xnu.ko stays loaded after a run that ended by itself (a panic, a halt): unload it, or the@@ -143,6 +143,10 @@ collect() { # the run's kernel log, kept; the status says how it endedsudo tee "$LOG" >/dev/nullls -1t "$LOGDIR"/run-*.log | tail -n +21 | xargs -r sudo rm -f # the last 20 runsif grep -aq "XNU panic" "$LOG"; then echo "the hosted system panicked: $LOG" >&2; return 2; fi+ # shut down from inside (shutdown -h, the Apple menu): a halt is a clean end; a restart ends with 4 for systemd to start it again+ # (checked before the stop: the other CPUs log "run stopped by the host" as they leave after it)+ if grep -aq "xnu: the hosted system halted" "$LOG"; then echo "the hosted system shut down: $LOG"; return 0; fi+ if grep -aq "xnu: the hosted system asked for a restart" "$LOG"; then echo "the hosted system restarts: $LOG"; return 4; fiif [ "${1:-}" = stopped ] || grep -aq "run stopped by the host" "$LOG"; then echo "the hosted system stopped: $LOG"; return 0; fiecho "the run ended: $LOG" >&2; return 3}diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.cindex 4c67b55..9d28e9a 100644--- a/xnu-ko/xnu_main.c+++ b/xnu-ko/xnu_main.c@@ -646,6 +646,10 @@ static long xnu_hook_body(unsigned long call, unsigned long a1, unsigned long a2logkv("xnu: XNU enters user space (EL0); user pc", a1);return 0;}+ if (call == LINUX64_CALL_HALT) { /* the hosted system shut down: XNU would spin with this CPU's interrupts masked */+ logs(a1 ? "xnu: the hosted system asked for a restart: run ended" : "xnu: the hosted system halted: run ended");+ return 1;+ }if (call == LINUX64_CALL_PANIC) {char m[500]; const char *f = (const char *)a1; unsigned i = 0;while (f && f[i] && i < sizeof(m) - 1) { m[i] = f[i]; i++; }diff --git a/xnu-patches/0001-linux64-machine-layer.patch b/xnu-patches/0001-linux64-machine-layer.patchindex 089b211..b405d4f 100644--- a/xnu-patches/0001-linux64-machine-layer.patch+++ b/xnu-patches/0001-linux64-machine-layer.patch@@ -3,8 +3,8 @@ The LINUX64 machine layer: XNU hosted inside a Linux kernel by xnu.ko.The CPU, MMU and exceptions are Linux's: XNU runs at EL1 on Linux's page tables (its kernel L1 entries installed by thehost), with hardware interrupts masked and a virtual interrupt flag; exceptions are taken in Linux's vectors and handedback (kernel faults to linux64_kernel_fault, user space through linux64_el0_run/linux64_el0_exception). Idle, timers,-the console, printf/kprintf, panics and failed asserts go to the host; entropy comes from the host until corecrypto-registers. Every hosted path is behind `if (linux64_hook)`: the same kernel still boots on hardware.+the console, printf/kprintf, panics, failed asserts and a halt or restart go to the host; entropy comes from the host+until corecrypto registers. Every hosted path is behind `if (linux64_hook)`: the same kernel still boots on hardware.The interface to the host is linux64/linux64_abi.h (shared with xnu.ko) and linux64/linux64.h, copied intoEXTERNAL_HEADERS/linux64 from xnu-patches/include by scripts/vm-xnu-rebuild.sh.@@ -1381,7 +1381,7 @@ index 13d2819..1837c6a 100644mrs x1, MPIDR_EL1diff --git a/osfmk/arm64/machine_routines.c b/osfmk/arm64/machine_routines.c-index f456377..b66afca 100644+index f456377..3fdc51c 100644--- a/osfmk/arm64/machine_routines.c+++ b/osfmk/arm64/machine_routines.c@@ -27,6 +27,7 @@@@ -1427,7 +1427,21 @@ index f456377..b66afca 100644MRS(value, "DAIF");if ((value & DAIF_STANDARD_DISABLE) == DAIF_STANDARD_DISABLE) {return FALSE;-@@ -1367,7 +1382,9 @@ ml_parse_cpu_topology(void)+@@ -934,6 +949,13 @@ halt_all_cpus(boolean_t reboot)+ printf("CPU halted\n");+ PEHaltRestart(kPEHaltCPU);+ }++ if (linux64_hook) {++ /*++ * LINUX64 (xnulinux): hosted, spinning here keeps this Linux CPU with its interrupts masked for good (Linux++ * soft-locks). The host ends the run instead and says whether the system asked for a restart.++ */++ linux64_call2(LINUX64_CALL_HALT, reboot ? 1 : 0, 0);++ }+ while (1) {+ ;+ }+@@ -1367,7 +1389,9 @@ ml_parse_cpu_topology(void)assert(((tpidr_el0 & MACHDEP_TPIDR_CLUSTERID_MASK) >> MACHDEP_TPIDR_CLUSTERID_SHIFT) == boot_cpu->cluster_id);__builtin_arm_wsr64("TPIDR_EL0", tpidr_el0);@@ -1438,7 +1452,7 @@ index f456377..b66afca 100644}const ml_topology_info_t *-@@ -2529,6 +2546,12 @@ ml_set_decrementer(uint32_t dec_value)+@@ -2529,6 +2553,12 @@ ml_set_decrementer(uint32_t dec_value)}}diff --git a/xnu-patches/0004-linux64-macos27-userland.patch b/xnu-patches/0004-linux64-macos27-userland.patchindex 512d096..6ab2c2e 100644--- a/xnu-patches/0004-linux64-macos27-userland.patch+++ b/xnu-patches/0004-linux64-macos27-userland.patch@@ -1,6 +1,7 @@What macOS 27's userland needs of xnu-12377: the system calls its libsystem_kernel calls (pipe2, dup3, fchflagsat,getumask; ENOSYS without SIGSYS for the ones it probes), a vnode table and workqueue limits for a full userland on one-CPU, and a log line for fatal exceptions and missing system calls.+CPU, and a log line for fatal exceptions and missing system calls. Other Darwin userlands (PureDarwin) also call vfork,+which Apple's arm64 configuration leaves out: it is a fork here.diff --git a/bsd/dev/arm/systemcalls.c b/bsd/dev/arm/systemcalls.cindex 5a7b717..5c12c8f 100644@@ -64,6 +65,30 @@ index 6e740c2..bfbdd29 100644/* consumes reason */return exit_with_reason(p, W_EXITCODE(0, SIGKILL), NULL,+diff --git a/bsd/kern/kern_fork.c b/bsd/kern/kern_fork.c+index 1d8db27..06c3a8d 100644+--- a/bsd/kern/kern_fork.c++++ b/bsd/kern/kern_fork.c+@@ -631,6 +631,19 @@ fork(proc_t parent_proc, __unused struct fork_args *uap, int32_t *retval)+ return err;+ }+++#if !CONFIG_VFORK++/*++ * LINUX64 (xnulinux): vfork, system call 66, which Apple's arm64 kernels leave out (CONFIG_VFORK) but other Darwin++ * userlands still call (PureDarwin's launchd and launchctl). It is a fork: POSIX lets vfork be one, and libsyscall's++ * vfork stub reads the same parent/child flag (x1) as fork's.++ */++int++sys_vfork(proc_t parent_proc, __unused struct vfork_args *uap, int32_t *retval)++{++ return fork(parent_proc, NULL, retval);++}++#endif++++ /*+ * cloneprocdiff --git a/bsd/kern/sys_pipe.c b/bsd/kern/sys_pipe.cindex bd682ab..64d7fa9 100644--- a/bsd/kern/sys_pipe.c@@ -123,9 +148,18 @@ index bd682ab..64d7fa9 100644fp_set_data(wf, wpipe);diff --git a/bsd/kern/syscalls.master b/bsd/kern/syscalls.master-index c08a74c..1e26f75 100644+index c08a74c..9bdafba 100644--- a/bsd/kern/syscalls.master+++ b/bsd/kern/syscalls.master+@@ -120,7 +120,7 @@+ #if CONFIG_VFORK+ 66 AUE_VFORK ALL { int vfork(void) NO_SYSCALL_STUB; }+ #else+-66 AUE_NULL ALL { int nosys(void); } { old vfork }++66 AUE_FORK ALL { int sys_vfork(void); } { LINUX64 vfork as fork for userlands that call it such as PureDarwin }+ #endif+ 67 AUE_NULL ALL { int oslog_coproc_reg(const char *uuid, const char *file_path, size_t file_path_len) NO_SYSCALL_STUB; }+ 68 AUE_NULL ALL { int oslog_coproc(void *buff, uint64_t buff_len, uint32_t type, const char *uuid, uint64_t timestamp, uint32_t offset, uint32_t stream_log) NO_SYSCALL_STUB; }@@ -229,8 +229,8 @@145 AUE_NULL ALL { int nosys(void); } { old setrlimit }146 AUE_NULL ALL { int nosys(void); } { old killpg }diff --git a/xnu-patches/include/linux64_abi.h b/xnu-patches/include/linux64_abi.hindex 72dbdd2..5d0db6d 100644--- a/xnu-patches/include/linux64_abi.h+++ b/xnu-patches/include/linux64_abi.h@@ -41,6 +41,7 @@ enum linux64_call {LINUX64_CALL_HOSTCALL = 830, /* a1 = struct linux64_hostcall_req: a message to a Linux service and its reply */LINUX64_CALL_CPU_START = 840, /* a1 = a CPU's MPIDR, a2 = its cpu_data_t: the host enters linux64_cpu_entry(a2) there */LINUX64_CALL_IPI = 841, /* a1 = the target CPU's MPIDR: its linux64_ipi_pending slot is set; make it look */+ LINUX64_CALL_HALT = 992, /* a1 = 1 restart, 0 halt: the hosted system shut down (halt_all_cpus); the run ends */LINUX64_CALL_CONSOLE_READ = 993, /* a1 = struct linux64_buf_req: console input bytes */LINUX64_CALL_RAMDISK_RW = 994, /* a1 = struct linux64_rw_req on the host-file md disk */LINUX64_CALL_RAMDISK_SIZE = 995, /* a1 = u64 *: the host-file md disk's size, 0 when there is none */