跳到主内容
@wquguru
精选90Hacker News Best(web_list)技巧与观点

XNU Linux:在Linux内核中以模块形式运行macOS用户态

XNU Linux: Full OS X GUI Userspace on Linux

原文
发到 X
推荐理由

极具工程价值的底层技术实践,展示了如何在Linux中直接运行macOS内核,对研究跨平台虚拟化或系统架构的同学是极佳的参考案例。

xnulinux

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

通过将 macOS 内核加载到 Linux 中,在 Linux 上运行 macOS 用户空间。

xnulinux builds XNU, the open-source kernel of macOS, from Apple's published sources and runs it as a loadable kernel module (xnu.ko) inside an unmodified arm64 Linux kernel. The hosted XNU drives the CPU and MMU directly, so macOS processes run natively next to Linux processes on the same CPUs, with no hypervisor between them. Linux lends XNU its memory, timers, files, network and GPU through a small interface module (fki.ko).

xnulinux 从 Apple 发布的源代码构建 XNU(macOS 的开源内核),并将其作为可加载内核模块(xnu.ko)在未修改的 arm64 Linux 内核内运行。托管的 XNU 直接驱动 CPU 和 MMU,因此 macOS 进程与 Linux 进程在同一组 CPU 上原生并行运行,两者之间没有虚拟机监控程序(hypervisor)。Linux 通过一个小型接口模块(fki.ko)向 XNU 提供内存、定时器、文件、网络和 GPU 支持。

With a stock Ubuntu kernel, insmod xnu.ko boots a macOS 27 system disk made by Apple's own installer to the desktop: loginwindow, WindowServer, Dock, Finder, Safari and Chrome.

使用默认的 Ubuntu 内核,执行 insmod xnu.ko 即可将 Apple 官方安装程序制作的 macOS 27 系统磁盘引导至桌面:包括 loginwindow、WindowServer、Dock、Finder、Safari 和 Chrome。

One system, two userspaces. Left: a Terminal in the hosted macOS 27.2.1 (sw_vers, the Darwin kernel, launchd, WindowServer, Dock and Finder running). Right: from that macOS, an SSH session into the Linux it runs inside (Ubuntu, Linux 7.0, the xnu and fki modules loaded, and the insmod xnu.ko process the macOS kernel runs in). Straight from the hosted framebuffer, 1280×800.

一个系统,两个用户空间。左侧:托管的 macOS 27.2.1 中的终端窗口(显示 sw_vers 命令输出,Darwin 内核、launchd、WindowServer、Dock 和 Finder 正在运行)。右侧:从该 macOS 环境通过 SSH 连接到其所在的 Linux 系统(Ubuntu,Linux 7.0,已加载 xnu 和 fki 模块,insmod xnu.ko 进程即为 macOS 内核运行的进程)。画面直接来自托管帧缓冲,分辨率为 1280×800。

The hosted desktop at the end of a 30-minute stress run, rendered on the CPU (no GPU bridge).

经过 30 分钟压力测试后,托管桌面由 CPU 渲染(无 GPU 桥接)。

Safari in the hosted macOS: Wikipedia's article on XNU, kernel.org and the Linux repository on GitHub, over the hosted system's network link through Linux, with the GPU bridge on (GPU=auto).

托管 macOS 中的 Safari 浏览器:正在浏览维基百科上关于 XNU 的文章、kernel.org 以及 GitHub 上的 Linux 仓库,网络链路通过 Linux 宿主系统建立,GPU 桥接已启用(GPU=auto)。

How it works

工作原理

代码 · 8 行
 macOS processes (launchd, WindowServer, Safari...)    Linux processes (systemd, sshd, xnudrmd...)
 ──────────────────────────────────────────────────    ────────────────────────────────────────────
 XNU (source-built, LINUX64 machine layer, kexts)  │
   ↕ linux64 host calls (xnu-patches/include/linux64_abi.h)
 xnu.ko   (APSL side: loads and runs the kernel collection)
   ↕ FKI, the foreign kernel interface (fki_*)
 fki.ko   (GPL side: CPUs, memory, faults, IPIs, files, network, host calls)
 Linux kernel (stock distro kernel, 4K pages, PAC)
  • CPUs: each hosted CPU is a Linux kernel thread pinned to a Linux CPU. XNU runs on it until it calls the host (idle, I/O, a timer); Linux keeps its own exception vectors, and fki.ko forwards the faults and user exceptions of XNU code to XNU's handlers.
  • Memory: XNU gets one physically contiguous block from Linux (CMA) and manages it with its own pmap; XNU and Linux share the user half of the address space layout (4K pages).
  • Devices: kexts in kexts/ give macOS a disk (a file on the Linux side), a display (a framebuffer Linux reads), a keyboard and pointer, an Ethernet link (a Linux network interface), a keystore and a GPU bridge.
  • GPU: Metal in the hosted macOS runs on gpu/xnu-cpu-metal (metal2vulkan over Vulkan). Vulkan reaches the Linux host's GPU through the DRM bridge (kexts/XnuDrm → host call → gpu/xnu-drm/xnudrmd → the Linux render node), using Mesa's Venus driver; without a GPU it falls back to Mesa lavapipe on the CPU.
  • CPU:每个托管 CPU 是一个绑定到特定 Linux CPU 核心的 Linux 内核线程。XNU 在该线程上运行,直到调用宿主机(空闲、I/O 或定时器事件);Linux 保留自己的异常向量表,fki.ko 将 XNU 代码引发的故障和用户异常转发给 XNU 的处理程序。
  • 内存:XNU 从 Linux 获取一块物理连续的内存区域(CMA),并使用自身的 pmap 进行管理;XNU 和 Linux 共享地址布局的用户空间部分(4K 页面)。
  • 设备:kexts/ 目录下的 kext 为 macOS 提供磁盘(Linux 侧的一个文件)、显示器(Linux 读取的帧缓冲)、键盘和指针设备、以太网连接(Linux 网络接口)、密钥库以及 GPU 桥接。
  • GPU:托管 macOS 中的 Metal 框架运行于 gpu/xnu-cpu-metal(基于 Vulkan 的 metal2vulkan 转换层)。Vulkan 通过 DRM 桥接访问 Linux 宿主机的 GPU(路径为 kexts/XnuDrm → 主机调用 → gpu/xnu-drm/xnudrmd → Linux 渲染节点),使用 Mesa 的 Venus 驱动;若无 GPU,则回退至 CPU 上的 Mesa lavapipe。

Status

状态

Working, on an Apple Silicon Mac running an arm64 Linux VM (krunkit) with 1 to 6 hosted CPUs:

已在运行 arm64 Linux 虚拟机(krunkit)的 Apple Silicon Mac 上验证可用,支持 1 至 6 个托管 CPU:

  • macOS 27.2.1 boots to the desktop with auto-login in 31–48 s (an Apple Virtualization macOS VM on the same Mac: 49 s).
  • Apps open and quit, Spotlight and the file system work; a 30-minute stress run (40 rounds of Safari, Notes, Calculator, TextEdit, System Settings, Spotlight and a disk walk) finishes with no panic and no crash report.
  • Runs as a systemd service: started at boot, stopped cleanly with the VM, restarted after a hosted-kernel panic, each run's log kept in /var/log/xnulinux/.
  • The GPU bridge gives hosted apps the host GPU (Virtio-GPU Venus (Apple M5 Max)) and is enabled by default (GPU=auto) when a Linux render node is present, falling back to CPU rendering (GPU=off) otherwise.
  • macOS 27.2.1 可在 31–48 秒内启动至桌面并自动登录(同一台 Mac 上运行的 Apple Virtualization macOS 虚拟机需 49 秒)。
  • 应用可正常打开和退出,Spotlight 和文件系统工作正常;30 分钟压力测试(40 轮 Safari、备忘录、计算器、文本编辑、系统设置、Spotlight 以及磁盘遍历)结束后无内核恐慌且无崩溃报告。
  • 作为 systemd 服务运行:开机时启动,随虚拟机干净停止,在托管内核发生恐慌后重启,每次运行的日志保留在 /var/log/xnulinux/ 中。
  • GPU 桥接为托管应用提供主机 GPU(Virtio-GPU Venus (Apple M5 Max)),并在存在 Linux 渲染节点时默认启用(GPU=auto),否则回退到 CPU 渲染(GPU=off)。

What is still a stopgap is listed in docs/production.md. Notably: the OS cryptex is merged into the system volume, AMFI is patched and SIP relaxed, and memory access is slower than a native VM (4K pages vs 16K).

docs/production.md 中列出了仍属于权宜之计的部分。 notably:OS cryptex 已合并到系统卷中,AMFI 已打补丁且 SIP 限制已放宽,内存访问速度比原生虚拟机慢(4K 页面 vs 16K)。

Requirements

要求

  • An arm64 Linux host with PAC (Apple Silicon M1 or later in a VM: krunkit for GPU access, or tart), 4K pages, 16 GB of RAM (8 GB of it reserved for the hosted kernel).
  • A Mac with Xcode to make the system disk and build the GPU parts.
  • A macOS 27 installation (an Apple Virtualization VM disk) of your own. No Apple binaries are in this repository or distributed by it: the kexts Apple ships, the macOS system disk and its trust cache come from your own installation.
  • 具有 PAC 的 arm64 Linux 主机(VM 中的 Apple Silicon M1 或更高版本:krunkit 用于 GPU 访问,或 tart),4K 页面,16 GB 内存(其中 8 GB 预留供托管内核使用)。
  • 一台装有 Xcode 的 Mac,用于制作系统磁盘并构建 GPU 相关部分。
  • 您自己的 macOS 27 安装文件(一个 Apple Virtualization VM 磁盘)。本仓库不包含也不分发任何 Apple 二进制文件:Apple 提供的 kexts、macOS 系统磁盘及其信任缓存均来自您自己的安装。

Building and running

构建与运行

  • scripts/vm-xnu-build.sh (in the Linux VM, once): sets up the macOS-DroidVM Linux toolchain and builds XNU (xnu-12377.121.6) natively on arm64.
  • 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/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.
  • scripts/xnulinux-boot.sh (in the Linux VM): 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.
  • scripts/install-service.sh (in the Linux VM): 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.
  • GPU (optional, on the Mac): gpu/venus/build-darwin.sh builds the hosted Vulkan driver, gpu/xnu-cpu-metal/ the Metal plugin (see its README).
  • scripts/vm-xnu-build.sh(在 Linux VM 中,仅执行一次):设置 macOS-DroidVM Linux 工具链,并在 arm64 上原生构建 XNU (xnu-12377.121.6)。
  • scripts/vm-xnu-rebuild.sh:应用 xnu-patches/ 中的补丁,构建内核和 kexts/,并将内核集合与您 macOS 安装中的 Apple kexts 链接。
  • scripts/mk-macvm-disk.sh(在 Mac 上):将 Apple 安装程序创建的 macOS VM 磁盘转换为托管系统的磁盘,并将其复制到 Linux VM 中。
  • scripts/xnulinux-boot.sh(在 Linux VM 中):针对当前运行的内核构建 fki.ko 和 xnu.ko,启动网络(当存在渲染节点时启动 GPU 桥接)并引导托管系统。其控制台为 scripts/xnu-console.sh,屏幕工具为 tools/hosted/fb2png.py,输入工具为 tools/hosted/xnu-input.py。
  • scripts/install-service.sh(在 Linux VM 中):以 xnulinux.service 运行,配置位于 /etc/xnulinux.conf(CPU、内存、磁盘、GPU)。在 Mac 上,scripts/install-krun-agent.sh 保持虚拟机本身持续运行。
  • GPU(可选,在 Mac 上):gpu/venus/build-darwin.sh 构建托管 Vulkan 驱动,gpu/xnu-cpu-metal/ 构建 Metal 插件(参见其 README)。

Repository layout and licenses

仓库结构与许可证

The Linux side and the XNU side are kept apart by license and meet only at FKI. Every source file carries an SPDX-License-Identifier; the license texts are in LICENSES/.

Linux 侧与 XNU 侧因许可证而保持分离,仅在 FKI 处交汇。每个源文件都带有 SPDX-License-Identifier;许可证文本位于 LICENSES/ 目录下。

PathLicenseWhat
fki-ko/, linux/GPL-2.0-onlyFKI: the out-of-tree module for stock kernels, and the same as an in-tree driver
xnu-ko/APSL-2.0xnu.ko: loads XNU's kernel collection and runs it; calls Linux only through fki_*
xnu-patches/APSL-2.0The LINUX64 machine layer and the other changes to XNU's sources, and their shared headers
kexts/APSL-2.0The hosted system's kexts (CPU, disk, keystore, Metal accelerator, DRM bridge)
kexts/XnuDisplay/, kexts/XnuEthernet/, kexts/XnuInput/BSD-2-Clause-PatentDerived from macOS-DroidVM's kexts
gpu/xnu-cpu-metal/GPL-2.0-or-laterThe Metal plugin over metal2vulkan (GPL-2.0-or-later); XnuCpuMetalShim.h is BSD-2-Clause-Patent (from AdrenoMetal)
gpu/xnu-drm/GPL-2.0-only, MITxnudrmd (the Linux end of the DRM bridge) is GPL; the protocol header and darwin/ (built into Mesa) are MIT
gpu/venus/GPL-2.0-only; patches MITBuild scripts and tests; the patches to Mesa and virglrenderer are under their upstream license (MIT)
gpu/xnu-cpu-metal/patches/upstream'sPatches to Mesa (MIT) and metal2vulkan (GPL-2.0-or-later)
scripts/, tools/, tests/, docs/GPL-2.0-onlyBuild, disk, boot, debugging and test tools; documentation
路径许可证说明
fki-ko/, linux/GPL-2.0-onlyFKI:面向标准内核的树外模块,与树内驱动相同
xnu-ko/APSL-2.0xnu.ko:加载 XNU 的内核集合并运行它;仅通过 fki_* 调用 Linux
xnu-patches/APSL-2.0LINUX64 机器层及对 XNU 源码的其他更改,以及它们的共享头文件
kexts/APSL-2.0宿主系统的 kext(CPU、磁盘、密钥库、Metal 加速器、DRM 桥接)
kexts/XnuDisplay/, kexts/XnuEthernet/, kexts/XnuInput/BSD-2-Clause-Patent源自 macOS-DroidVM 的 kext
gpu/xnu-cpu-metal/GPL-2.0-or-latermetal2vulkan(GPL-2.0-or-later)之上的 Metal 插件;XnuCpuMetalShim.h 为 BSD-2-Clause-Patent(源自 AdrenoMetal)
gpu/xnu-drm/GPL-2.0-only, MITxnudrmd(DRM 桥接的 Linux 端)采用 GPL;协议头文件和 darwin/(内置于 Mesa)采用 MIT
gpu/venus/GPL-2.0-only; patches MIT构建脚本和测试;对 Mesa 和 virglrenderer 的补丁遵循其上游许可证(MIT)
gpu/xnu-cpu-metal/patches/upstream's对 Mesa(MIT)和 metal2vulkan(GPL-2.0-or-later)的补丁
scripts/, tools/, tests/, docs/GPL-2.0-only构建、磁盘、启动、调试和测试工具;文档

xnu.ko is on the XNU side of the line: it runs XNU's code in its own address space, so it is APSL-2.0 like XNU, and Linux reports it as a non-GPL module (MODULE_LICENSE("Proprietary"), which taints the kernel as ZFS does). It must not include Linux headers beyond the module ABI, call Linux functions other than fki_*, or carry Linux code. Upstream code (XNU, macOS-DroidVM's toolchain, Mesa, metal2vulkan, the Vulkan loader, LLVM) is fetched at pinned revisions and patched at build time; it is never committed.

xnu.ko 位于 XNU 侧:它在自己的地址空间中运行 XNU 的代码,因此属于 APSL-2.0,与 XNU 相同,且 Linux 将其报告为非 GPL 模块(MODULE_LICENSE("Proprietary"),这会使内核被标记为 ZFS 那样的状态)。它不得包含超出模块 ABI 范围的 Linux 头文件,不得调用除 fki_* 之外的 Linux 函数,也不得携带 Linux 代码。上游代码(XNU、macOS-DroidVM 的工具链、Mesa、metal2vulkan、Vulkan 加载器、LLVM)在固定的修订版本上获取,并在构建时打补丁;这些代码永远不会提交。

Acknowledgements

致谢

  • macOS-DroidVM and UniXNU: the 4K-page XNU base, the Linux-hosted XNU toolchain, and the display, Ethernet and input kexts these are derived from.
  • metal2vulkan, Mesa (Venus, lavapipe), virglrenderer and krunkit.
  • Apple's open-source XNU.
  • macOS-DroidVM 和 UniXNU:4K 页大小的 XNU 基础、Linux 宿主的 XNU 工具链,以及由此衍生出的显示、以太网和输入 kext。
  • metal2vulkan、Mesa(Venus、lavapipe)、virglrenderer 和 krunkit。
  • Apple 的开源 XNU。

macOS, Metal and XNU are trademarks of Apple Inc. This project is not affiliated with or endorsed by Apple.

macOS、Metal 和 XNU 是 Apple Inc. 的商标。本项目未获得 Apple 的关联或认可。

Copyright © 2026 Heyang Zhou and contributors.

版权所有 © 2026 Heyang Zhou 及贡献者。

更进一步:量化金融体系

看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力

进入量化体系 →

相似阅读

关联信息,但可能不是同一事件