Skip to content

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

C

Hosted XNU: macOS 27 system calls (pipe2, dup3, fchflagsat, getumask implemented; aio_readv/writev, fsctlat, guarded_ftruncate_np return ENOSYS), nosys calls logged with pc/lr, dlopen of ad-hoc signed libraries under amfi_get_out_of_my_way (F_CHECK_LV, exec mmap); vm-xnu-rebuild.sh stops on a failed kernel build; xprotectd parked. CPython 3.13.1 builds in the hosted macOS: 74 extension modules load, 1744 tests in json/re/os/subprocess/sqlite3/threading with 2 failures

Heyang Zhou91e8f9dcf336
diff --git a/scripts/vm-park.sh b/scripts/vm-park.sh
index a9d05b9..5de9b7a 100755
--- a/scripts/vm-park.sh
+++ b/scripts/vm-park.sh
@@ -6,7 +6,7 @@ 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.bluetoothd com.apple.mobileactivationd com.apple.xprotectd com.apple.security.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
diff --git a/scripts/vm-xnu-rebuild.sh b/scripts/vm-xnu-rebuild.sh
index eeba7b5..a2113aa 100755
--- a/scripts/vm-xnu-rebuild.sh
+++ b/scripts/vm-xnu-rebuild.sh
@@ -2,7 +2,7 @@
# Inside the VM: apply xnulinux's XNU patches to the k27 tree, rebuild the kernel incrementally, and (when the macOS 27.2
# kernelcache is there) relink the kernel collection with Apple's kexts. The kernel's segment slots inside the collection
# (k27/segaddrs.sh) follow the kernel's size: when kc27-layout.py moves them, the kernel is linked again.
-set -e
+set -e -o pipefail # a failed kernel build stops here: never repackage the previous kernel
cd ~/w/dvm
export PATH=$PATH:/usr/lib/llvm-21/bin
T=out/k27/xnu
diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patch
index ee66d68..4de6b69 100644
--- a/xnu-patches/0001-linux64-stage-hook.patch
+++ b/xnu-patches/0001-linux64-stage-hook.patch
@@ -1,3 +1,18 @@
+diff --git a/bsd/dev/arm/systemcalls.c b/bsd/dev/arm/systemcalls.c
+index 5a7b717..5c12c8f 100644
+--- a/bsd/dev/arm/systemcalls.c
++++ b/bsd/dev/arm/systemcalls.c
+@@ -132,6 +132,10 @@ unix_syscall(
+
+ uthread->uu_flag |= UT_NOTCANCELPT;
+ uthread->syscall_code = code;
++ if (__improbable(callp->sy_call == (sy_call_t *)nosys)) { /* LINUX64: who calls a system call this kernel does not have */
++ kprintf("linux64: nosys %d from pid %d %s pc 0x%llx lr 0x%llx\n", code, proc_pid(proc), proc->p_comm,
++ (unsigned long long)get_saved_state_pc(state), (unsigned long long)get_saved_state_lr(state));
++ }
+
+ uthread->uu_rval[0] = 0;
+
diff --git a/bsd/dev/memdev.c b/bsd/dev/memdev.c
index d24535d..5c2aafb 100644
--- a/bsd/dev/memdev.c
@@ -239,6 +254,87 @@ index 5362b32..cd43d8e 100644
#if DEBUG_BSDINIT
#define bsd_init_kprintf(x, ...) kprintf("bsd_init: " x, ## __VA_ARGS__)
+diff --git a/bsd/kern/kern_descrip.c b/bsd/kern/kern_descrip.c
+index fac307f..23e1d06 100644
+--- a/bsd/kern/kern_descrip.c
++++ b/bsd/kern/kern_descrip.c
+@@ -113,11 +113,25 @@
+ #include <kern/waitq.h>
+ #include <kern/ipc_kobject.h>
+ #include <kern/ipc_misc.h>
++#include <pexpert/pexpert.h>
+ #include <kern/ast.h>
+
+ #include <vm/vm_protos.h>
+ #include <mach/mach_port.h>
+
++/* LINUX64: hosted with the amfi_get_out_of_my_way boot-arg, code signing lets any locally built code load (kern_mman.c too) */
++int linux64_amfi_off(void);
++int
++linux64_amfi_off(void)
++{
++ static int off = -1;
++ if (off < 0) {
++ int v = 0;
++ off = PE_parse_boot_argn("amfi_get_out_of_my_way", &v, sizeof(v)) && v != 0;
++ }
++ return off;
++}
++
+ #include <security/audit/audit.h>
+ #if CONFIG_MACF
+ #include <security/mac_framework.h>
+@@ -2525,6 +2539,32 @@ sys_dup2(proc_t p, struct dup2_args *uap, int32_t *retval)
+ return dup2(p, p_cred, uap->from, uap->to, retval);
+ }
+
++/*
++ * LINUX64: dup3(from, to, flags), system call 149 in macOS 27's libsystem_kernel: dup2 that fails when from == to and
++ * takes O_CLOEXEC for the new descriptor.
++ */
++int
++sys_dup3(proc_t p, struct dup3_args *uap, int32_t *retval)
++{
++ kauth_cred_t p_cred = current_cached_proc_cred(p);
++ int error;
++
++ if ((uap->flags & ~O_CLOEXEC) || uap->from == uap->to) {
++ return EINVAL;
++ }
++ error = dup2(p, p_cred, uap->from, uap->to, retval);
++ if (error == 0 && (uap->flags & O_CLOEXEC)) {
++ struct fileproc *fp;
++
++ proc_fdlock(p);
++ if ((fp = fp_get_noref_locked(p, uap->to)) != NULL) {
++ fp->fp_flags |= FP_CLOEXEC;
++ }
++ proc_fdunlock(p);
++ }
++ return error;
++}
++
+ int
+ dup2(proc_t p, kauth_cred_t p_cred, int old, int new, int *retval)
+ {
+@@ -4412,8 +4452,15 @@ dropboth:
+ }
+
+ #if CONFIG_MACF
+- error = mac_file_check_library_validation(p, fg, lv.lv_file_start,
+- (user_long_t)lv.lv_error_message, lv.lv_error_message_size);
++ /*
++ * LINUX64: hosted with amfi_get_out_of_my_way, a process may load any library, as it may run any binary:
++ * AMFI otherwise rejects the locally built (ad-hoc, linker-signed) Python extension modules a locally built
++ * python dlopens ("mapping process and mapped file (non-platform) have different Team IDs").
++ */
++ if (!linux64_amfi_off()) {
++ error = mac_file_check_library_validation(p, fg, lv.lv_file_start,
++ (user_long_t)lv.lv_error_message, lv.lv_error_message_size);
++ }
+ #endif
+
+ break;
diff --git a/bsd/kern/kern_exec.c b/bsd/kern/kern_exec.c
index 2bf34ab..e7bf32a 100644
--- a/bsd/kern/kern_exec.c
@@ -296,6 +392,21 @@ index 6e740c2..bfbdd29 100644
/* consumes reason */
return exit_with_reason(p, W_EXITCODE(0, SIGKILL), NULL,
+diff --git a/bsd/kern/kern_mman.c b/bsd/kern/kern_mman.c
+index 32d27c9..8f9bc0c 100644
+--- a/bsd/kern/kern_mman.c
++++ b/bsd/kern/kern_mman.c
+@@ -669,7 +669,9 @@ mmap(proc_t p, struct mmap_args *uap, user_addr_t *retval)
+
+ handle = (void *)vp;
+ #if CONFIG_MACF
+- error = mac_file_check_mmap(vfs_context_ucred(ctx),
++ extern int linux64_amfi_off(void);
++ /* LINUX64: with amfi_get_out_of_my_way, executable mappings of locally built (ad-hoc signed) libraries too */
++ error = linux64_amfi_off() ? 0 : mac_file_check_mmap(vfs_context_ucred(ctx),
+ fp->fp_glob, prot, flags, file_pos, &maxprot);
+ if (error) {
+ (void)vnode_put(vp);
diff --git a/bsd/kern/kern_trustcache.c b/bsd/kern/kern_trustcache.c
index 4b1e8a9..f188eed 100644
--- a/bsd/kern/kern_trustcache.c
@@ -373,6 +484,119 @@ index 4b1e8a9..f188eed 100644
/* Nothing to do when the runtime isn't set */
if (trust_cache_rt == NULL) {
return;
+diff --git a/bsd/kern/sys_pipe.c b/bsd/kern/sys_pipe.c
+index bd682ab..64d7fa9 100644
+--- a/bsd/kern/sys_pipe.c
++++ b/bsd/kern/sys_pipe.c
+@@ -388,8 +388,29 @@ expand_pipespace(struct pipe *p, int target_size)
+ */
+
+ /* ARGSUSED */
++static int pipe_flags(proc_t p, int32_t *retval, int flags);
++
+ int
+ pipe(proc_t p, __unused struct pipe_args *uap, int32_t *retval)
++{
++ return pipe_flags(p, retval, 0);
++}
++
++/*
++ * LINUX64: pipe2(fildes, flags), system call 148 in macOS 27's libsystem_kernel (__pipe2 returns the two descriptors in
++ * x0/x1 as __pipe does, then stores them). Flags: O_CLOEXEC and O_NONBLOCK, on both ends.
++ */
++int
++pipe2(proc_t p, struct pipe2_args *uap, int32_t *retval)
++{
++ if (uap->flags & ~(O_CLOEXEC | O_NONBLOCK)) {
++ return EINVAL;
++ }
++ return pipe_flags(p, retval, uap->flags);
++}
++
++static int
++pipe_flags(proc_t p, int32_t *retval, int flags)
+ {
+ struct fileproc *rf, *wf;
+ struct pipe *rpipe, *wpipe;
+@@ -409,7 +430,10 @@ pipe(proc_t p, __unused struct pipe_args *uap, int32_t *retval)
+ if (error) {
+ goto freepipes;
+ }
+- rf->f_flag = FREAD;
++ rf->f_flag = FREAD | ((flags & O_NONBLOCK) ? FNONBLOCK : 0);
++ if (flags & O_CLOEXEC) {
++ rf->fp_flags |= FP_CLOEXEC;
++ }
+ rf->f_ops = &pipeops;
+ fp_set_data(rf, rpipe);
+
+@@ -418,7 +442,10 @@ pipe(proc_t p, __unused struct pipe_args *uap, int32_t *retval)
+ fp_free(p, retval[0], rf);
+ goto freepipes;
+ }
+- wf->f_flag = FWRITE;
++ wf->f_flag = FWRITE | ((flags & O_NONBLOCK) ? FNONBLOCK : 0);
++ if (flags & O_CLOEXEC) {
++ wf->fp_flags |= FP_CLOEXEC;
++ }
+ wf->f_ops = &pipeops;
+ fp_set_data(wf, wpipe);
+
+diff --git a/bsd/kern/syscalls.master b/bsd/kern/syscalls.master
+index c08a74c..1e26f75 100644
+--- a/bsd/kern/syscalls.master
++++ b/bsd/kern/syscalls.master
+@@ -229,8 +229,8 @@
+ 145 AUE_NULL ALL { int nosys(void); } { old setrlimit }
+ 146 AUE_NULL ALL { int nosys(void); } { old killpg }
+ 147 AUE_SETSID ALL { int setsid(void); }
+-148 AUE_NULL ALL { int nosys(void); } { old setquota }
+-149 AUE_NULL ALL { int nosys(void); } { old qquota }
++148 AUE_PIPE ALL { int pipe2(user_addr_t fildes, int flags) NO_SYSCALL_STUB; } { LINUX64 macOS 27 pipe2 }
++149 AUE_DUP2 ALL { int sys_dup3(u_int from, u_int to, int flags); } { LINUX64 macOS 27 dup3 }
+ 150 AUE_NULL ALL { int nosys(void); } { old getsockname }
+ 151 AUE_GETPGID ALL { int getpgid(pid_t pid); }
+ 152 AUE_SETPRIVEXEC ALL { int setprivexec(int flag); }
+@@ -309,8 +309,8 @@
+ 212 AUE_NULL ALL { int nosys(void); } { old ATgetrsp }
+ 213 AUE_NULL ALL { int nosys(void); } { Reserved for AppleTalk }
+
+-214 AUE_NULL ALL { int nosys(void); }
+-215 AUE_NULL ALL { int nosys(void); }
++214 AUE_CHFLAGS ALL { int fchflagsat(int fd, user_addr_t path, int flags, int atflag); } { LINUX64 macOS 27 fchflagsat }
++215 AUE_UMASK ALL { int getumask(void); } { LINUX64 macOS 27 getumask }
+
+ ; System Calls 216 - 230 are reserved for calls to support HFS/HFS Plus
+ ; file system semantics. Currently, we only use 215-227. The rest is
+@@ -351,8 +351,8 @@
+ 243 AUE_INITGROUPS ALL { int initgroups(u_int gidsetsize, gid_t *gidset, int gmuid) NO_SYSCALL_STUB; }
+ 244 AUE_POSIX_SPAWN ALL { int posix_spawn(pid_t *pid, const char *path, const struct _posix_spawn_args_desc *adesc, char **argv, char **envp) NO_SYSCALL_STUB; }
+ 245 AUE_FFSCTL ALL { int ffsctl(int fd, u_long cmd, caddr_t data, u_int options); }
+-246 AUE_NULL ALL { int nosys(void); }
+-247 AUE_NULL ALL { int nosys(void); } { old nfsclnt }
++246 AUE_NULL ALL { int enosys(void); } { LINUX64 macOS 27 aio_readv ENOSYS without SIGSYS }
++247 AUE_NULL ALL { int enosys(void); } { LINUX64 macOS 27 aio_writev ENOSYS without SIGSYS }
+
+ #if NFSSERVER /* XXX */
+ 248 AUE_FHOPEN ALL { int fhopen(const struct fhandle *u_fhp, int flags); }
+@@ -360,7 +360,7 @@
+ 248 AUE_NULL ALL { int nosys(void); }
+ #endif
+
+-249 AUE_NULL ALL { int nosys(void); }
++249 AUE_NULL ALL { int enosys(void); } { LINUX64 macOS 27 fsctlat ENOSYS without SIGSYS }
+ 250 AUE_MINHERIT ALL { int minherit(caddr_ut addr, size_ut len, int inherit); }
+ #if SYSV_SEM
+ 251 AUE_SEMSYS ALL { int semsys(u_int which, int a2, int a3, int a4, int a5) NO_SYSCALL_STUB; }
+@@ -530,7 +530,7 @@
+ 349 AUE_NULL ALL { int __pthread_fchdir(int fd); }
+ 350 AUE_AUDIT ALL { int audit(void *record, int length); }
+ 351 AUE_AUDITON ALL { int auditon(int cmd, void *data, int length); }
+-352 AUE_NULL ALL { int nosys(void); }
++352 AUE_NULL ALL { int enosys(void); } { LINUX64 macOS 27 guarded_ftruncate_np ENOSYS without SIGSYS }
+ 353 AUE_GETAUID ALL { int getauid(au_id_t *auid); }
+ 354 AUE_SETAUID ALL { int setauid(au_id_t *auid); }
+ 355 AUE_NULL ALL { int nosys(void); } { old getaudit }
diff --git a/bsd/pthread/pthread_shims.c b/bsd/pthread/pthread_shims.c
index e5e58b5..6e6596d 100644
--- a/bsd/pthread/pthread_shims.c
@@ -446,6 +670,55 @@ index 229d6a1..e610b22 100644
mp->mnt_vnodecovered = NULLVP;
//mp->mnt_stat.f_type = vfsp->vfc_typenum;
mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
+diff --git a/bsd/vfs/vfs_syscalls.c b/bsd/vfs/vfs_syscalls.c
+index 873eb8f..a15ea6b 100644
+--- a/bsd/vfs/vfs_syscalls.c
++++ b/bsd/vfs/vfs_syscalls.c
+@@ -8018,6 +8018,44 @@ chflags(__unused proc_t p, struct chflags_args *uap, __unused int32_t *retval)
+ return error;
+ }
+
++/*
++ * LINUX64: fchflagsat(fd, path, flags, atflag), system call 214 in macOS 27's libsystem_kernel: chflags relative to fd
++ * (AT_FDCWD for the working directory), not following a final symlink with AT_SYMLINK_NOFOLLOW.
++ */
++int
++fchflagsat(__unused proc_t p, struct fchflagsat_args *uap, __unused int32_t *retval)
++{
++ vnode_t vp;
++ vfs_context_t ctx = vfs_context_current();
++ int error;
++ struct nameidata nd;
++
++ if (uap->atflag & ~AT_SYMLINK_NOFOLLOW) {
++ return EINVAL;
++ }
++ AUDIT_ARG(fflags, uap->flags);
++ NDINIT(&nd, LOOKUP, OP_SETATTR, ((uap->atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW) | AUDITVNPATH1,
++ UIO_USERSPACE, uap->path, ctx);
++ error = nameiat(&nd, uap->fd);
++ if (error) {
++ return error;
++ }
++ vp = nd.ni_vp;
++ nameidone(&nd);
++ /* chflags1 drops the vnode reference */
++ return chflags1(vp, uap->flags, ctx);
++}
++
++/*
++ * LINUX64: getumask(), system call 215 in macOS 27's libsystem_kernel: the file creation mask, unchanged.
++ */
++int
++getumask(proc_t p, __unused struct getumask_args *uap, int32_t *retval)
++{
++ *retval = p->p_fd.fd_cmask;
++ return 0;
++}
++
+ /*
+ * Change flags of a file given a file descriptor.
+ */
diff --git a/bsd/vm/vm_unix.c b/bsd/vm/vm_unix.c
index d8fd4af..815051f 100644
--- a/bsd/vm/vm_unix.c