Skip to content

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

C

README and docs: the set-up steps as they work; LICENSES/Linux-syscall-note.txt; the hosted tools are executable

Fred Nixf7a1c9e6c7ae
diff --git a/LICENSES/Linux-syscall-note.txt b/LICENSES/Linux-syscall-note.txt
new file mode 100644
index 0000000..fcd0563
--- /dev/null
+++ b/LICENSES/Linux-syscall-note.txt
@@ -0,0 +1,12 @@
+ NOTE! This copyright does *not* cover user programs that use kernel
+ services by normal system calls - this is merely considered normal use
+ of the kernel, and does *not* fall under the heading of "derived work".
+ Also note that the GPL below is copyrighted by the Free Software
+ Foundation, but the instance of code that it refers to (the Linux
+ kernel) is copyrighted by me and others who actually wrote it.
+
+ Also note that the only valid version of the GPL as far as the kernel
+ is concerned is _this_ particular version of the license (ie v2, not
+ v2.2 or v3.x or whatever), unless explicitly otherwise stated.
+
+ Linus Torvalds
diff --git a/README.md b/README.md
index 57639d6..629b00f 100644
--- a/README.md
+++ b/README.md
@@ -80,8 +80,9 @@ Working on two arm64 Linux setups:
- **Bare-metal NVIDIA DGX Spark (Ubuntu 24.04.5 LTS aarch64, Linux `7.0.0-1025-nvidia`)**:
- Boots to the full macOS desktop (Screen Sharing and SSH working) directly on bare-metal arm64 Linux with `CPUS=4`
and 8 GiB of RAM reserved for XNU.
- - Currently renders on the CPU (Mesa lavapipe, `GPU=off`): `xnudrmd` only forwards `virtio-gpu` (Venus) ioctls today,
- so hardware GPU pass-through on the GB10 GPU requires a new DRM bridge backend.
+ - Renders on the GB10 (`Virtio-GPU Venus (NVIDIA GB10)`, `GPU=auto`) through `xnudrmd`'s virglrenderer backend for a
+ render node that is not virtio-gpu (`gpu/xnu-drm/xnudrmd_virgl.c`, virglrenderer 1.2.0: `tools/dgx-spark/README.md`),
+ or on the CPU (Mesa lavapipe, `GPU=off`).
Shipping it as a hosted service (what is distributed, what a user installs to run Apple's macOS) is in
[`docs/hosted-service.md`](docs/hosted-service.md).
@@ -105,16 +106,23 @@ the system volume, AMFI is patched and SIP relaxed, and memory access is slower
1. `scripts/vm-xnu-build.sh` (on the arm64 Linux host, once): sets up the macOS-DroidVM Linux toolchain and builds XNU
(`xnu-12377.121.6`) natively on arm64.
2. `scripts/vm-xnu-rebuild.sh`: applies `xnu-patches/`, builds the kernel and `kexts/`, and links the kernel collection
- with Apple's kexts from your macOS installation (`scripts/kclink`). To link that collection without building
- anything, `scripts/apple-kc/link.sh`, on the machine that runs `xnu.ko`, takes a kit of the built kernel and kexts
- and macOS 27.2's kernelcache, read from Apple's IPSW ([docs/free-kc.md](docs/free-kc.md)).
+ (`/lib/firmware/xnu/kc.bin`) with Apple's kexts from macOS 27.2's kernelcache (`scripts/kclink`), when that is at
+ `~/w/dvm/out/kc/kc27.bin`; without it no collection is linked. It comes from Apple's IPSW:
+ `python3 scripts/apple-kc/im4p.py URL ~/w/dvm/out/kc/kc27.bin`, the URL in `~/w/dvm/configs/macos-27.2-26B123.json`.
+ To link that collection without building anything, `scripts/apple-kc/link.sh`, on the machine that runs `xnu.ko`,
+ takes a kit of the built kernel and kexts and macOS 27.2's kernelcache, read from Apple's IPSW
+ ([docs/free-kc.md](docs/free-kc.md)).
3. `scripts/mk-macvm-disk.sh` (on the Mac): turns a macOS VM disk made by Apple's installer into the hosted system's disk
and copies it into the Linux VM.
-4. `scripts/xnulinux-boot.sh` (on the arm64 Linux host): builds `fki.ko` and `xnu.ko` against the running kernel, brings
+4. `scripts/vm-install-fki-service.sh` (on the arm64 Linux host, once, then reboot): Linux's `mem=`, and `fki.ko` loaded
+ at boot with the carve-out that is the hosted kernel's DRAM (without it the boot stops: `fki: carve-out ... overlaps
+ IORESOURCE_SYSTEM_RAM`). Then
+ `scripts/xnulinux-boot.sh` (on the arm64 Linux host): builds `fki.ko` and `xnu.ko` against the running kernel, brings
up the network (and the GPU bridge when there is a render node) and boots the hosted system. Its console is
`scripts/xnu-console.sh`, its screen `tools/hosted/fb2png.py`, input `tools/hosted/xnu-input.py`. On a bare-metal
host (such as a DGX Spark), set the carve-out (`mem=`, `carve_base=`, `carve_mb=`) to fit the machine's physical
- memory layout, and set `GPU=off` (`xnudrmd` supports only virtio-gpu for now).
+ memory layout; its GPU needs virglrenderer installed before `xnudrmd` is built (`tools/dgx-spark/README.md`), or
+ `GPU=off`.
5. `scripts/install-service.sh` (on the arm64 Linux host): runs it as `xnulinux.service`, configured in
`/etc/xnulinux.conf` (CPUs, memory, disk, GPU). On the Mac, `scripts/install-krun-agent.sh` keeps the VM itself
running.
diff --git a/docs/hosted-service.md b/docs/hosted-service.md
index 6585718..d61fada 100644
--- a/docs/hosted-service.md
+++ b/docs/hosted-service.md
@@ -27,13 +27,14 @@ artifact comes with its source.
|---|---|---|---|
| `fki.ko` | `fki-ko/` | GPL-2.0-only | the foreign kernel interface: CPUs, memory carve-out, faults, IPIs, host calls |
| `xnu.ko` | `xnu-ko/` | APSL-2.0 | loads and runs the kernel collection. It is a non-GPL module that taints the kernel, like ZFS: ship its **source** and build it on the host against the running kernel (`xnulinux-boot.sh` does this on every boot, and a DKMS package would do it on kernel updates). Do not ship it prebuilt inside a kernel package |
-| `xnudrmd` | `gpu/xnu-drm/xnudrmd.c` | GPL-2.0-only | the GPU bridge's Linux service (virtio-gpu Venus render nodes only so far) |
+| `xnudrmd` | `gpu/xnu-drm/xnudrmd.c` | GPL-2.0-only | the GPU bridge's Linux service (a virtio-gpu Venus render node, or another through virglrenderer: `xnudrmd_virgl.c`) |
| boot and service scripts | `scripts/xnulinux-boot.sh`, `install-service.sh`, `vm-install-fki-service.sh`, `vm-net-up.sh` | GPL-2.0-only | `xnulinux.service`, `xnulinux-fki.service`, `/etc/xnulinux.conf` |
| kernel command line | `vm-install-fki-service.sh` | | `mem=`, `nokaslr`, `log_buf_len=`; the carve-out (`carve_base`, `carve_mb`) sized for each host model |
| on Mac hosts | `scripts/krun-vm.sh`, `install-krun-agent.sh`, krunkit (Apache-2.0) | | the Linux VM with virtio-gpu Venus; a launchd agent keeps it running |
The host Linux is a stock arm64 distribution kernel (Ubuntu 24.04 is tested) with 4K pages. The CPU needs PAC
-(`FEAT_PAuth`), and Linux must run at EL1, not as a bare-metal VHE kernel at EL2. `fki.ko` refuses to load otherwise.
+(`FEAT_PAuth`), and Linux must run at EL1, not as a bare-metal VHE kernel at EL2 (`kvm-arm.mode=nvhe` on a DGX Spark).
+`fki.ko` refuses to load otherwise.
### 2.2 The kernel kit
@@ -45,7 +46,7 @@ The host Linux is a stock arm64 distribution kernel (Ubuntu 24.04 is tested) wit
| `kexts/NAME/` | our kexts as arm64e objects plus the description `kclink` links them from (`XnuCPU`, `XnuDisk`, `XnuDrm`, display, input, ethernet...) |
| `kc-free.bin` | the PureDarwin kernel collection (`scripts/free-kc/build.sh`). It is the same byte for byte whether or not any Apple kernelcache is visible to the build. |
| `kit.json` | for `link.sh`: the sha256 of the one kernelcache it links against and Apple's IPSW URL for it, the Apple kexts to keep (`scripts/apple-kc/keep.txt`), the pointers to rebind, AMFI's platform version |
-| `scripts/kclink`, `scripts/apple-kc` | the linker and the user-side scripts (GPL-2.0-only), shipped with the kit |
+| `scripts/kclink`, `scripts/apple-kc`, `scripts/kc-amfi-platform.py` | the linker and the user-side scripts (GPL-2.0-only), shipped with the kit but not in it (`package.sh` writes the four files above): unpacked as `/opt/xnulinux/scripts` (§4) |
The kernel's segment slots are laid out for one macOS build (today 27.2, `26B123`, `kernelcache.release.vma2`). Each
macOS build we support gets its own kit, and `link.sh` refuses any other kernelcache.
@@ -97,6 +98,8 @@ We ship these. The user's disk script installs them into their own macOS disk:
CPUS=2
RAM_MB=7900
```
+ Run it as the user the service is to run as. The service has no terminal, so that user needs sudo without a
+ password (a `NOPASSWD` rule), or run the installer as root.
5. **Isolation.** The hosted kernel runs at EL1 inside the Linux kernel, so a hosted system that is compromised owns
that Linux. **The tenant boundary is the VM (or the machine), never the Linux.** Run one Linux VM per tenant, with one
hosted system in it. Never run two users' hosted systems in one Linux.
@@ -117,7 +120,7 @@ Apple's installer (`tart` or another Virtualization.framework installer).
the IM4P or an unpacked kernelcache can be given as a second argument instead. The output is the same byte for byte
on every machine.
2. **System disk: Apple's installer, on a Mac.** Install macOS from Apple's IPSW into a Virtualization.framework
- disk (`tart create NAME --from-ipsw …`), then run `scripts/mk-macvm-disk.sh` on an APFS clone of it. It merges the
+ disk (`tart create NAME --from-ipsw …`), then run `SRC=~/.tart/vms/NAME/disk.img scripts/mk-macvm-disk.sh` (it works on an APFS clone of it). It merges the
OS cryptex into the system volume (`docs/production.md` §3), adds to the Data volume: the local user and automatic login, the Metal plugin (§2.4), the
console shell, and a list of disabled hardware daemons. It also takes the static trust cache from the disk's own
Preboot volume. It sends `mac.img`, `mac.conf` and `trustcache-mac.bin` to the Linux host.
@@ -146,6 +149,6 @@ Apple's installer (`tart` or another Virtualization.framework installer).
| One kit per macOS build, fixed segment slots | each macOS release needs a new kit before users can update |
| AMFI relaxed (`amfi_get_out_of_my_way=1`) and SIP partly off (`lp-sip0`); the OS cryptex merged into the system volume | the hosted macOS is less locked down than a real Mac. State this in the service's documentation (`docs/production.md` §3) |
| The keystore is fresh at every boot | FileVault-style data protection does not persist across reboots |
-| GPU bridge only for virtio-gpu (Venus) | Mac hosts get the host GPU. Bare-metal hosts render on the CPU |
+| GPU bridge: virtio-gpu (Venus), or virglrenderer on the host's own Vulkan driver (tested on a DGX Spark) | Mac hosts and the DGX Spark get the host GPU; other bare-metal hosts are untested |
| Bare-metal hosts need their own carve-out layout (`mem=`, `carve_base`) | one layout per host model, set up when it is provisioned |
| `xnu.ko` built on the host at boot | needs kernel headers and a compiler on the host. Package it as DKMS |
diff --git a/docs/smp.md b/docs/smp.md
index cfd2954..2e1cf67 100644
--- a/docs/smp.md
+++ b/docs/smp.md
@@ -65,7 +65,7 @@ spinning lets the host take the spinning CPU's interrupts (`linux64_spin_relax`,
Done: `hw.ncpu` and `hw.activecpu` 2; two `yes` plus the login's own work reach about 170% (more than one CPU can);
the desktop comes up; minutes of running with a hundred cross-CPU fault resumes and no fault. Kernel fault counts
are of one CPU's order (7.6 and 5.6 million in the first 8 minutes).
-3. N CPUs (`cpus=` defaults to the Linux CPUs less one for Linux), then the workloads: build a project, 4K playback.
+3. N CPUs (`cpus=`; its default is still 1, see Open), then the workloads: build a project, 4K playback.
Done for `cpus=` up to 6 (2026-11-22): 1, 2, 4 and 6 CPUs each boot to the desktop (Dock 48, 42, 38, 39 s from
`kern.boottime`) with no panic, silent CPU or soft lockup. What 3 and more needed:
- fki's kick: one csd per target CPU shared by every sender; `smp_call_function_single_async`'s busy check is not
diff --git a/scripts/extract-trustcache.py b/scripts/extract-trustcache.py
index 6b652ff..7d90f41 100755
--- a/scripts/extract-trustcache.py
+++ b/scripts/extract-trustcache.py
@@ -6,7 +6,7 @@
Run on the Mac whose OS the hosted system disk was made from, or give the mounted Preboot volume of that disk (an
installed macOS VM disk, scripts/mk-macvm-disk.sh). The output is Apple data: it stays local (the VM's
-/lib/firmware/xnu/trustcache.bin, passed to xnu.ko as trustcache=), never in the repository.
+/lib/firmware/xnu/trustcache-mac.bin, xnulinux-boot.sh's TRUSTCACHE, passed to xnu.ko as trustcache=), never in the repository.
"""
import glob, struct, sys
diff --git a/scripts/free-kc/build.sh b/scripts/free-kc/build.sh
index 2c280f2..5c81293 100755
--- a/scripts/free-kc/build.sh
+++ b/scripts/free-kc/build.sh
@@ -4,8 +4,8 @@
# own kexts (XnuCPU, XnuDisk, XnuDrm), plus the kexts a Darwin userland needs, built from PureDarwin's sources as arm64e with
# this kernel's own compile flags and headers (scripts/free-kc/kcc.py): a permissive AMFI, pthread, IOStorageFamily,
# HFSEncodings and hfs; and xnulinux's own corecrypto kext (kexts/free/corecrypto: Mbed TLS behind the kernel's corecrypto
-# interface). It boots PureDarwin's userland from an HFS+ disk (scripts/mk-puredarwin-disk.sh):
-# IMAGE=xnu/kc-free.bin DISK=... ROOT_UUID=... scripts/xnulinux-boot.sh
+# interface). It boots PureDarwin's userland from an HFS+ disk (scripts/mk-puredarwin-disk.sh, which prints its pd.conf):
+# MAC_CONF=pd.conf DISK=pd.img IMAGE=xnu/kc-free.bin TRUSTCACHE= GPU=off scripts/xnulinux-boot.sh
# Sources are fetched at pinned revisions (Mbed TLS: kexts/free/corecrypto/mbedtls.rev).
#
# Nothing of Apple's is read: the collection is linked into an empty one that mk-template.py makes from layout numbers
diff --git a/scripts/mk-macvm-disk.sh b/scripts/mk-macvm-disk.sh
index 1973dce..957a2fe 100755
--- a/scripts/mk-macvm-disk.sh
+++ b/scripts/mk-macvm-disk.sh
@@ -3,7 +3,7 @@
# On the macOS host: the hosted system's disk from a macOS VM disk installed by Apple's own installer, then sent to
# the xnupoc VM as /var/tmp/mac.img (xnu.ko disk=, kexts/XnuDisk; boot-arg rd=<its system volume>).
#
-# `tart create NAME --from-ipsw latest` installs macOS with Virtualization.framework, as Apple's own VMs are installed: the
+# `tart create xnumac --from-ipsw latest` installs macOS with Virtualization.framework, as Apple's own VMs are installed: the
# whole Apple Silicon layout (iBootSystemContainer with xART and Hardware, the main container with the System and Data
# volume group and Preboot with the cryptexes, the Recovery container). The sealed System volume stays as installed; what the
# hosted system needs is added to the Data volume only:
@@ -13,6 +13,7 @@
# - a root shell on /dev/console (xnulinux.shell), the hosted system's console
# - launchd's disabled list for daemons of hardware the hosted system has not (watchdog, audio, ...) and Rosetta's
# and the static trust cache of that macOS build, for xnu.ko trustcache=. Apple's files stay on this host and in the VM.
+# SRC: the installed disk (default ~/.tart/vms/xnumac/disk.img: a VM of another NAME needs SRC=~/.tart/vms/NAME/disk.img).
set -euo pipefail
cd "$(dirname "$0")/.."
V="bash scripts/vm.sh"
diff --git a/scripts/mk-puredarwin-disk.sh b/scripts/mk-puredarwin-disk.sh
index cd0a554..1d5bda4 100755
--- a/scripts/mk-puredarwin-disk.sh
+++ b/scripts/mk-puredarwin-disk.sh
@@ -8,7 +8,7 @@
# Linux writes to a journaled volume only with -o force, which is safe on a new one: its journal is empty and stays so.
# The kernel mounts the root read-only; PureDarwin's own images use ext4, whose kext clears that, and its mount(8) has no
# -uw for launchctl's remount, so /sbin/fsck (which launchctl runs first, on a read-only root) remounts it read-write as
-# PureDarwin's HFS images do. The disk is checked on the Linux side before each boot (fsck.hfsplus), not here.
+# PureDarwin's HFS images do. Nothing checks the disk, here or before a boot (xnulinux-boot.sh runs no fsck.hfsplus).
# mk-puredarwin-disk.sh ROOT.tar.gz OUT.img [SIZE (1g)] prints the disk's configuration for xnulinux-boot.sh (MAC_CONF):
# ROOT_UUID and BOOT_UUID, the partition's GUID
set -euo pipefail
@@ -42,7 +42,7 @@ sudo rm -rf "$M/.fseventsd" "$M/.Spotlight-V100"
F=$(mktemp)
cat > "$F" <<'FSCK'
#!/bin/zsh
-# xnulinux: the disk was checked on the Linux host before boot (fsck.hfsplus); remount the HFS+ root read-write
+# xnulinux: no check (nothing checks this disk before a boot); remount the HFS+ root read-write
dev=$(/bin/df / | /bin/awk 'NR==2 {print $1}')
/sbin/mount -t hfs -o update,rw "$dev" / && echo "xnulinux: root $dev remounted read-write"
exit 0
diff --git a/scripts/vm-install-fki-service.sh b/scripts/vm-install-fki-service.sh
index 8331915..b197782 100755
--- a/scripts/vm-install-fki-service.sh
+++ b/scripts/vm-install-fki-service.sh
@@ -11,6 +11,9 @@
set -euo pipefail
P=$(cd "$(dirname "$0")/.." && pwd)
LINUX_MEM=${LINUX_MEM:-7G}; CARVE_BASE=${CARVE_BASE:-0x200000000}; CARVE_MB=${CARVE_MB:-8156}
+# the unit loads fki-ko/fki.ko at every boot, before anything else builds it (xnulinux-boot.sh, which runs from xnulinux.service,
+# and that requires this unit): build it now
+make -s -C /lib/modules/$(uname -r)/build M="$P/fki-ko" modules
G=/etc/default/grub.d/99-xnulinux.cfg
if [ "${1:-}" = remove ]; then
[ ! -d /sys/module/xnu ] || { echo "a hosted system is running: scripts/install-service.sh remove (or scripts/xnulinux-boot.sh stop) first" >&2; exit 1; }
diff --git a/tools/hosted/fb2png.py b/tools/hosted/fb2png.py
old mode 100644
new mode 100755
diff --git a/tools/hosted/xnu-input.py b/tools/hosted/xnu-input.py
old mode 100644
new mode 100755