xnulinux: Two Kernels, One CPU

Apple's XNU runs as a loadable Linux module, swapping page tables, PAC keys, and registers on every world switch. No hypervisor required.

~10 min read • View on GitHub • More from AFK-surf

A wide editorial illustration of a single large mechanical loom in a sparse workshop, with two weavers standing on opposite sides. Each weaver has their own warp threads, shuttle, and pattern card, but they share one shuttle race. One shuttle is mid-flight while the other rests in a weaver's hand. The image explains how two kernels can share one CPU core by taking turns owning it, one shuttle crossing at a time.
One machine, two operators, one shuttle at a time. That is the entire architecture of xnulinux in a single image.
Key Takeaways

The World Switch

On every call between the two kernels, the CPU saves one world's registers, swaps its page tables, swaps its pointer authentication keys, swaps its thread-local pointers, flushes the entire translation lookaside buffer, and runs the other kernel. Then it reverses the whole sequence.

There is no hypervisor in that sentence. There is no guest boundary. There is a very disciplined handoff, written in hand-tuned assembly, executed on every core.

This is the world switch, and it is the most surprising thing in xnulinux. The project runs Apple's open-source XNU kernel as a loadable Linux module. XNU owns the CPU and the MMU while it runs. Linux owns them the rest of the time. Neither one is virtualized. They just take turns.

The README frames the goal plainly: "Run macOS userspace on Linux by loading the macOS kernel into it."

Run macOS userspace on Linux by loading the macOS kernel into it.

AFK-surf / project README, Project description · AFK-surf/xnulinux README

What xnulinux Actually Is

Apple publishes XNU's source. xnulinux builds it, patches it, and loads the result as xnu.ko into a stock arm64 Linux kernel. A second module, fki.ko, is the Foreign Kernel Interface. It gives hosted XNU the things a kernel normally expects from hardware: memory, timers, files, networking, and a path to the GPU.

The hosted XNU is not sandboxed. It drives the CPUs and the MMU directly, which is exactly why macOS and Linux processes can share the same cores without a hypervisor in between. The README reports booting a macOS 27.2.1 desktop on an Apple Silicon Linux VM and on a bare-metal NVIDIA DGX Spark. The repository snapshot shows 2,312 stars and 89 forks.

# Load the hosted macOS kernel into a stock arm64 Linux kernel
insmod xnu.ko cpus=4 ram_mb=8192 hostdisk=/dev/nvme0n1p3 \
    fb_width=1920 fb_height=1080 fb_dump=/tmp/fb.raw

# Bring up the Foreign Kernel Interface that supplies
# memory, timers, files, networking, and GPU access
insmod fki.ko

Who Built It and Why

xnulinux lives in the AFK-surf organization, from AFK Inc. The repository's top contributor is losfair, with 262 commits. The announcement was blunt about what had been done.

We loaded XNU as a kernel module inside stock arm64 Linux and ran its code in kernel context. No VM, no compat layer, open source.

AFK Inc. (@afk_surf), Project-linked social account · AFK Inc. social post

Then came the milestone post. Heyang Zhou, associated with the project announcement, put it simply.

We can now run full macOS GUI userspace on Linux.

Heyang Zhou, Developer associated with the project announcement · Heyang Zhou social post

The DGX Spark detail is the one that reframes the whole project. A DGX Spark is an arm64 Linux box with no Apple silicon inside it. The README reports running a macOS desktop on it, with screen sharing and SSH, using CPU rendering rather than a hardware GPU.

A black ink hedcut-style editorial portrait of losfair, the top contributor to xnulinux, rendered with stipple dots for skin texture and fine hatching for hair and clothing on a pure white background.

How Two Kernels Share One Core

The ledger for the whole arrangement is struct xnu_world. Each CPU gets one. It holds the callee-saved registers and stack pointer for both worlds, both sets of TLS registers, both page-table base registers, both PAC key sets, and Linux's shadow call stack pointer. The layout is fixed and enforced with _Static_assert checks on the exact byte offsets.

struct xnu_world {
    u64 linux_regs[14];   /* x19-x30, SP */
    u64 xnu_regs[13];     /* x19-x30, SP */
    u64 linux_tpidr0, linux_tpidrro0;
    u64 xnu_tpidr, xnu_tpidrro;
    u64 linux_ttbr0, linux_tcr;
    u64 xnu_ttbr0, xnu_tcr;
    u64 linux_scs;        /* x18, shadow call stack */
    struct fki_pac_keys linux_keys;
    struct fki_pac_keys xnu_keys;
};

The entry path is short and deliberate. Locate the save area for this CPU from its MPIDR affinity bits. Save Linux's callee-saved registers, stack pointer, and x18. Call fki_enter to disable IRQs and preemption and load XNU's PAC keys. Save and set the TLS registers. Mask all exceptions, including debug exceptions. Swap TTBR0 and TCR_EL1 to XNU's user pmap. Flush the TLB. Branch.

Three choices inside that path are worth naming.

The full TLB flush is the expensive part. TO_XNU_MM and TO_LINUX_MM each end with tlbi vmalle1. The comment justifies it by noting that the two worlds' ASIDs are unrelated. Correct, and costly. It is probably a major contributor to the performance gap the project documents.

A single upcall from XNU into Linux, one stage at a time. Step through to see what gets saved, what gets swapped, and where the TLB flush lands.

Faking an Apple SoC

XNU expects its hardware description from iBoot. There is no iBoot. So xnulinux builds the description itself.

adt.c synthesizes an Apple Device Tree, the same {u32 nProperties, u32 nChildren} structure XNU parses on real hardware. It is a synthetic description of a virtual Apple SoC, and its existence is one of the clearest statements of what the project has to fake. macho.c applies chained fixups and re-signs authenticated pointers inside XNU's key context, because those pointers must be signed with the keys XNU will later authenticate with.

A close-up editorial illustration of two hands exchanging two shallow trays on a single workbench under one overhead lamp. The left tray holds a brass keyring, a folded map, a pair of spectacles, and a closed notebook. The right tray holds a different keyring, map, spectacles, and notebook. The image explains the world switch as a swap of a specific, finite set of state: keys are PAC keys, the map is the page table, spectacles are TLS, and the notebook is the saved registers.
The world switch is a swap of a specific, countable set of state. Keys are PAC keys, the map is the page table, spectacles are TLS, the notebook is the saved registers.

A set of IOKit kexts give hosted macOS its devices: disk, display, input, Ethernet, DRM, and a Metal CPU device. The README credits some of these derived kexts to macOS-DroidVM. Together with the ADT and the Mach-O loader, they are what make this a faithful boot rather than a loader that merely runs code.

Not a VM

The distinction that matters is categorical, not visual. xnulinux is not a virtual machine, not a compatibility layer, and not a reimplementation. The boundary between the two kernels is a module boundary, and XNU is the real kernel, not a guest.

ApproachWhat runsBoundaryarm64 Linux hostFull macOS userspace
xnulinuxApple's XNU kernel, loaded as xnu.ko plus fki.koModule boundary, no VMYesGoal
DarlingReimplemented macOS interfacesUserspace layerYesPartial
GNUstepOpenStep/Cocoa API implementationUserspace layerYesNo
Apple Virtualization.frameworkmacOS guest kernel behind a hypervisorVM boundaryNo, needs Apple hardwareYes, isolated guest
QEMU/KVMGuest operating systemVM boundaryYes, host-dependentPartial

A Hacker News commenter drew the line cleanly against GNUstep: "The goal of GNUstep was/is to provide an open-source implementation of the OPENSTEP API. This is not what this project here is doing. It's taking the already open-source XNU kernel and gets it to run under Linux." That is the whole differentiator in one sentence.

The Fine Print

The project's own production document lists the stopgaps: a merged OS cryptex, patched AMFI, relaxed SIP, and slower memory access than a native VM. On performance, it reports Dock startup at 31 seconds for the hosted setup versus 49 seconds for a two-CPU Apple Virtualization VM on the same host. These are project-reported measurements.

The licensing split is deliberate. xnu.ko is APSL-2.0, matching XNU itself. fki.ko and the Linux-side code are GPL-2.0-only. The README describes FKI as "a narrow, stable, semantic interface," and xnu.ko "only uses fki.h." Whether that boundary holds up legally is a question for a licensing expert, not this article.

Nice, I like this, though, it still needs Apple hardware. I think I would prefer if someone was to improve GNUstep instead.

gen2brain, Hacker News commenter · Hacker News discussion

That comment reflects the state of the project at announcement: the tested environments were an Apple Silicon Linux VM and a bare-metal DGX Spark, and those results do not establish broad hardware compatibility. The EULA questions raised in the same thread are community discussion, not legal conclusions.