# XNU

**XNU** 是苹果操作系统核心的[混合内核](https://www.edgepedia.org/zh/hybrid-kernel)，属于 macOS 与 iOS 所用的 [Darwin](https://www.edgepedia.org/zh/darwin-operating-system) 操作系统的一部分。这个名字是“X is Not Unix”（X 不是 Unix）的首字母缩写。XNU 是一种混合内核，它将[卡内基梅隆大学](https://www.edgepedia.org/zh/carnegie-mellon-university)（Carnegie Mellon University）的 Mach 内核与 FreeBSD 的部分内容，再加上 IOKit——一套用于驱动开发的 C++ API——融合在一起，并在 x86_64 与 ARM64 上同时支持单处理器和多处理器配置。<sup>[1](https://github.com/apple-oss-distributions/xnu/blob/main/README.md?plain=1)</sup>

| 关键事实 | 详情 |
|---|---|
| 类型 | 混合内核：Mach、BSD（主要是 FreeBSD）与 I/O Kit 链接进单一内核地址空间<sup>[1](https://github.com/apple-oss-distributions/xnu/blob/main/README.md?plain=1)</sup><sup> • </sup><sup>[2](https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/Mach/Mach.html)</sup> |
| 名称 | “X is Not Unix”的缩写<sup>[1](https://github.com/apple-oss-distributions/xnu/blob/main/README.md?plain=1)</sup> |
| 平台 | macOS 与 iOS（经由 Darwin）；x86_64 与 ARM64，单处理器与多处理器<sup>[1](https://github.com/apple-oss-distributions/xnu/blob/main/README.md?plain=1)</sup> |
| 开源 | 源码由苹果发布；Darwin 1.0 于 2000 年 4 月 5 日在开源模式下发布<sup>[3](https://www.apple.com/newsroom/2000/04/05Apple-Releases-Darwin-1-0-Open-Source/)</sup> |
| 安全职责 | 被视为可信；负责执行代码签名、沙盒、权限凭证检查与地址空间布局随机化（ASLR）<sup>[4](https://help.apple.com/pdf/security/en_US/apple-platform-security-guide.pdf)</sup> |
| 构建变体 | debug、development、release、profile；默认配置为 DEVELOPMENT<sup>[1](https://github.com/apple-oss-distributions/xnu/blob/main/README.md?plain=1)</sup> |
| 扩展模型 | 内核扩展（kext）在内核空间运行；自 macOS 11 起，DriverKit 与系统扩展在可能时于用户空间运行同样的服务<sup>[5](https://developer.apple.com/documentation/kernel/implementing_drivers_system_extensions_and_kexts?changes=l__3)</sup> |

## 起源与 Darwin

XNU 是 Darwin 的内核，后者是构成 macOS、iOS 及苹果其他操作系统基础的开源核心。苹果于 2000 年 4 月 5 日发布 Darwin 1.0，将新的 Mac OS X 内核描述为基于 FreeBSD 与 Mach 3.0 技术、面向 PowerPC 与 Intel 平台构建。<sup>[3](https://www.apple.com/newsroom/2000/04/05Apple-Releases-Darwin-1-0-Open-Source/)</sup> Mach 部分可追溯至卡内基梅隆大学设计的 Mach 3.0 微内核，后来由苹果与开放软件基金会研究院（Open Software Foundation Research Institute）将其适配到 [Power Macintosh](https://www.edgepedia.org/zh/power-macintosh)，OSF 商业开发工作产生的代码也被并入 Darwin 内核；OS X 中所用的 Mach API 在许多方面已与最初的 CMU Mach 3 API 不同。<sup>[6](https://developer-rno.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/About/About.html)</sup>

Darwin 与 macOS 构建在同一内核之上，但 macOS 还增加了 Quartz、Cocoa、Carbon 和 [QuickTime](https://www.edgepedia.org/zh/quicktime) 等 Darwin 所不含的专有层。<sup>[7](https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/Architecture/Architecture.html)</sup>

## 架构

内核环境包含的不只是 Mach 内核本身：它包括 Mach、BSD、I/O Kit、文件系统和网络组件，这些合起来被称为内核。<sup>[7](https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/Architecture/Architecture.html)</sup>

**Mach。** Mach 3.0 最初被设想为一个能够独立运行的通信微内核，把 I/O、文件系统和网络等服务放在用户态的服务器中。而在 OS X 中，Mach 则与其他内核组件链接进一个单一的内核地址空间，主要是出于性能考虑：已链接组件之间的直接调用远快于独立任务之间的消息传递或远程过程调用。这样得到的系统比宏内核更具模块化和可扩展性，又没有纯微内核的性能代价。<sup>[2](https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/Mach/Mach.html)</sup> Mach 管理 CPU 占用与内存等处理器资源，负责调度，实施内存保护，并提供以消息传递为中心的进程间通信基础设施。其抽象包括任务（拥有虚拟地址空间与端口权限的资源所有权单位）、线程、端口、内存对象和 IPC 原语。<sup>[7](https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/Architecture/Architecture.html)</sup><sup> • </sup><sup>[2](https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/Mach/Mach.html)</sup> OS X 的进程和 [POSIX 线程](https://www.edgepedia.org/zh/posix-threads)都建立在 Mach 任务和线程之上。<sup>[2](https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/Mach/Mach.html)</sup>

**BSD。** 在 Mach 之上，BSD 层提供“操作系统个性”（OS personality）的 API 与服务，主要源自 FreeBSD，即 4.4BSD 的一个版本。它提供文件系统、网络（硬件设备层除外）、UNIX 安全模型、系统调用支持、带进程 ID 与信号的 BSD 进程模型、大量 POSIX API，以及内核对 pthreads 的支持。<sup>[7](https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/Architecture/Architecture.html)</sup> 苹果的实现增加了 FreeBSD 所没有的功能，包括对文件系统缓冲区缓存和文件 I/O 聚集的改进、针对 ISO-9660 文件系统的苹果扩展、NFS 的多线程异步 I/O，以及支持 HFS+ 语义的系统调用。<sup>[8](https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/BSD/BSD.html)</sup>

**I/O Kit。** I/O Kit 是一个面向对象的驱动框架，用 C++ 的一个受限子集实现。它提供[即插即用](https://www.edgepedia.org/zh/plug-and-play)、动态设备管理、驱动按需加载和电源管理。<sup>[7](https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/Architecture/Architecture.html)</sup>

从技术上讲，XNU 并非微内核实现，但它保留了微内核的优点，例如 Mach IPC 机制，以及内核各部分之间相对清晰的 API 分离。之所以采用混合设计，是因为纯 Mach 把核心操作系统服务放在用户态进程中，通过消息传递拖慢了系统；把 BSD 功能与 Mach 一并纳入内核，则重新赢回了那部分性能。<sup>[9](https://developer.apple.com/library/archive/documentation/Porting/Conceptual/PortingUnix/additionalfeatures/additionalfeatures.html)</sup>

## 源码树与构建

源码发布在苹果位于 GitHub 的 `apple-oss-distributions/xnu` 仓库中。<sup>[10](https://github.com/apple-oss-distributions/xnu)</sup> 主要目录包括 `osfmk`（基于 Mach 内核的子系统）、`bsd`（BSD 子系统）、`iokit` 与 `libkern`（用于驱动和 kext 的 C++ IOKit 库代码）、`pexpert`（中断处理和原子操作等平台相关代码）、`security`（强制访问检查策略接口）、`libsyscall`（面向用户程序的系统调用库接口）、`libsa`（引导代码）和 `libkdd`（用于解析内核分块数据的用户库）。<sup>[1](https://github.com/apple-oss-distributions/xnu/blob/main/README.md?plain=1)</sup>

构建系统由 make 变量驱动：`KERNEL_CONFIGS` 在 debug、development、release 和 profile 变体之间选择，这些变体配置编译标志和断言；`ARCH_CONFIGS` 选择架构，例如 `X86_64`。默认情况下，构建以构建机自身的架构和 DEVELOPMENT 配置为目标，产出可启动映像 `kernel.[config]` 以及带符号的未剥离二进制文件。<sup>[1](https://github.com/apple-oss-distributions/xnu/blob/main/README.md?plain=1)</sup> `config/MASTER` 中的配置选项包括 `MACH_BSD`（Mach 之上的 BSD 子系统）、`IOKIT`、`CONFIG_MACF`（强制访问控制框架）、`CONFIG_KXLD`（kext 的运行时链接），以及 `MACH_ASSERT`、`KDEBUG` 等调试选项。<sup>[11](https://github.com/apple-oss-distributions/xnu/blob/main/config/MASTER)</sup>

头文件会安装到多个位置：`Kernel.framework` 的头文件供内核扩展使用，`IOKit.framework` 的头文件供 IOKit 用户态客户端使用，`/usr/include` 与 `System.framework` 供用户级应用使用，`/System/DriverKit/usr/include` 供用户态驱动使用。各框架 `PrivateHeaders` 目录中的头文件仅供苹果内部开发使用。<sup>[1](https://github.com/apple-oss-distributions/xnu/blob/main/README.md?plain=1)</sup>

## 启动与调试

启动序列由 `kern/startup.h` 模块驱动，被组织成带等级（rank）的具名子系统，包括早期引导、Mach IPC、代码签名子系统、sysctl 注册以及飞地（exclaves）初始化。最后的锁定阶段（文档指出绝不应挂接该阶段）会取消映射标记为 `__startup_data` 或 `__startup_const` 的数据，以及标记为 `__startup_func` 的代码，并将标记为 `__security_const_late` 或 `SECURITY_READ_ONLY_LATE` 的数据设为只读。<sup>[12](https://github.com/apple-oss-distributions/xnu/blob/main/doc/lifecycle/startup.md)</sup>

默认情况下，内核在发生 panic 时重启；`debug=0x14e` 启动参数则让 panic 改为等待调试器挂接。XNU 支持通过远程内核调试协议（kdp）经以太网、Thunderbolt 或串口（视硬件而定）进行调试，使用 LLDB 对未剥离的内核映像进行调试；dSYM 调试包中随附了用于内核调试的 LLDB 宏，位于 `tools/lldbmacros`。<sup>[1](https://github.com/apple-oss-distributions/xnu/blob/main/README.md?plain=1)</sup>

## 安全职责

苹果的平台安全文档将 XNU 描述为苹果操作系统核心的内核，被视为可信，并指出它负责执行代码签名、沙盒、权限凭证检查和地址空间布局随机化（ASLR）。<sup>[4](https://help.apple.com/pdf/security/en_US/apple-platform-security-guide.pdf)</sup> 在被标记为既可写又可执行的内存页可以使用之前，内核会检查仅限苹果使用的动态代码签名权限凭证。<sup>[13](https://help.apple.com/pdf/security/en_CA/apple-platform-security-guide-v.pdf)</sup> 沙盒是内核级的安全层，对系统调用施加严格约束，以限制恶意软件能造成的破坏。<sup>[14](https://arxiv.org/abs/1608.04303)</sup>

近来，内核的安全职责开始向外迁移。在近期的苹果平台上，涉及 SPTM、TXM 与飞地（exclaves）的架构变化将关键与敏感组件——包括负责代码签名和权限凭证验证的可信执行监视器（Trusted Execution Monitor）——移出了 XNU 的直接触及范围，形成即使在内核遭攻陷时也能守住的信任边界。<sup>[15](https://arxiv.org/abs/2510.09272)</sup>

## 内核扩展与现代驱动模型

XNU 支持内核扩展（kext），即无需重新编译内核即可动态加载进内核空间的代码包；I/O Kit 中的设备驱动和设备族都以 kext 形式实现，网络内核扩展（NKE）和 VFS 文件系统扩展也是如此。<sup>[7](https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/Architecture/Architecture.html)</sup>

自 macOS 11 起，苹果已将其中大部分工作移出内核。DriverKit 扩展（dext）和系统扩展在用户空间运行，在那里它们无法危及 macOS 的安全或稳定性；若已存在等效的 DriverKit 或系统扩展方案，内核就不再加载对应的 kext。<sup>[5](https://developer.apple.com/documentation/kernel/implementing_drivers_system_extensions_and_kexts?changes=l__3)</sup><sup> • </sup><sup>[16](https://developer.apple.com/documentation/DriverKit)</sup> 保留下来的 kext 面临更严格的要求：在[苹果芯片](https://www.edgepedia.org/zh/apple-silicon)上，它们必须支持带指针认证码的 `arm64e` 架构，在内核完整性保护（Kernel Integrity Protection）下运行（该机制在初始化后锁定内核内存页），并且安装或卸载 kext 需要用户批准并重启。<sup>[5](https://developer.apple.com/documentation/kernel/implementing_drivers_system_extensions_and_kexts?changes=l__3)</sup>

## References

1. [XNU README, apple-oss-distributions/xnu, GitHub](https://github.com/apple-oss-distributions/xnu/blob/main/README.md?plain=1)
2. [Mach Overview, Kernel Programming Guide, Apple Developer Archive](https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/Mach/Mach.html)
3. [Apple Releases Darwin 1.0 Open Source, Apple Newsroom, April 5, 2000](https://www.apple.com/newsroom/2000/04/05Apple-Releases-Darwin-1-0-Open-Source/)
4. [Apple Platform Security Guide (glossary, XNU entry)](https://help.apple.com/pdf/security/en_US/apple-platform-security-guide.pdf)
5. [Implementing drivers, system extensions, and kexts, Apple Developer Documentation](https://developer.apple.com/documentation/kernel/implementing_drivers_system_extensions_and_kexts?changes=l__3)
6. [About This Document, Kernel Programming Guide, Apple Developer Archive](https://developer-rno.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/About/About.html)
7. [Kernel Architecture Overview, Kernel Programming Guide, Apple Developer Archive](https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/Architecture/Architecture.html)
8. [BSD Overview, Kernel Programming Guide, Apple Developer Archive](https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/BSD/BSD.html)
9. [Additional Features, Porting UNIX Applications, Apple Developer Archive](https://developer.apple.com/library/archive/documentation/Porting/Conceptual/PortingUnix/additionalfeatures/additionalfeatures.html)
10. [apple-oss-distributions/xnu repository, GitHub](https://github.com/apple-oss-distributions/xnu)
11. [xnu/config/MASTER, apple-oss-distributions/xnu, GitHub](https://github.com/apple-oss-distributions/xnu/blob/main/config/MASTER)
12. [XNU startup sequence, xnu/doc/lifecycle/startup.md, GitHub](https://github.com/apple-oss-distributions/xnu/blob/main/doc/lifecycle/startup.md)
13. [Apple Platform Security Guide (code signing entitlements)](https://help.apple.com/pdf/security/en_CA/apple-platform-security-guide-v.pdf)
14. [SandBlaster: Reversing the Apple Sandbox, arXiv](https://arxiv.org/abs/1608.04303)
15. [Modern iOS Security Features: A Deep Dive into SPTM, TXM, and Exclaves, arXiv](https://arxiv.org/abs/2510.09272)
16. [DriverKit, Apple Developer Documentation](https://developer.apple.com/documentation/DriverKit)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Operating systems › Apple operating systems

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/xnu

English original: https://www.edgepedia.org/xnu
