XNU Kernel Architecture
macOS & BSD Internals · Operating Systems track
8 min read
In this module
The Hybrid Heart of macOS
XNU ("X is Not Unix") is the kernel underneath macOS, iOS, iPadOS, watchOS, and tvOS. There's no other production kernel quite like it: a Mach microkernel core, a BSD subsystem on top, and Apple's IOKit driver framework alongside — all stuffed into one shared address space. Get a handle on XNU and you'll see why macOS security research feels different, how driver development works on the platform, and how Apple sidesteps the perennial microkernel-vs-monolithic debate by simply doing both.
The Three Components
The three parts aren't stacked in a neat chain. They're siblings in one address space, and user space reaches the kernel through two different trap tables: BSD syscalls and Mach traps. Trace the two paths below: a read() enters on the BSD side, a mach_msg() on the Mach side, and both end up leaning on Mach's VM.
Mach: The Microkernel Foundation
Mach hands you the bedrock primitives.
Tasks and Threads: A Mach task is a container — address space, ports, and the like. BSD processes are wrappers built on top of Mach tasks. Every BSD process has a Mach task hiding inside it, and every BSD thread maps to a Mach thread.
// Get the current task's Mach portmach_port_t task = mach_task_self();
// Get information about the taskstruct task_basic_info info;mach_msg_type_number_t count = TASK_BASIC_INFO_COUNT;task_info(task, TASK_BASIC_INFO, (task_info_t)&info, &count);// info.resident_size - physical memory used// info.virtual_size - virtual memory used// info.suspend_count - suspension levelMach Ports: the fundamental IPC primitive. A port is a kernel-managed message queue you address by capability:
Port Rights:
| Right Type | Allows |
|---|---|
| Receive right | Receive messages (one holder) |
| Send right | Send messages (many holders) |
| Send-once right | Send one message then invalid |
| Port set | Wait on multiple ports |
| Dead name | Port was destroyed |
Predict: after the allocate-then-insert code below runs, how many port names does your task hold for the new port, and which rights?
One name, two rights. mach_port_allocate creates the port and puts a receive right under a new name in your task's port namespace. mach_port_insert_right with MACH_MSG_TYPE_MAKE_SEND adds a send right under that same name, so the entry's type becomes send-plus-receive. On a Mac, lsmp -p <pid> lists a process's names and the rights it holds on each.
// Allocate a new Mach port with receive rightmach_port_t port;mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port);
// Insert a send rightmach_port_insert_right(mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND);
// Send a messagetypedef struct { mach_msg_header_t header; char data[256];} MyMessage;
MyMessage msg = {0};msg.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0);msg.header.msgh_size = sizeof(msg);msg.header.msgh_remote_port = destination_port;msg.header.msgh_local_port = MACH_PORT_NULL;strcpy(msg.data, "Hello from Mach!");
mach_msg(&msg.header, MACH_SEND_MSG, sizeof(msg), 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);Unlike simple byte-oriented IPC (pipes, sockets), Mach messages can transfer port rights between tasks. When you send a message containing a send right, the kernel transfers that right to the receiver's port namespace. This capability-based model is how macOS services grant access to each other: a service can send a port right to a client, giving it the ability to communicate with another service.
Virtual Memory: Mach owns the virtual address space for each task. The BSD layer doesn't reinvent any of this — it goes straight to Mach VM under the hood:
// Allocate memory in the current taskmach_vm_address_t addr = 0;mach_vm_allocate(mach_task_self(), &addr, 4096, VM_FLAGS_ANYWHERE);
// Map memory with specific protectionmach_vm_protect(mach_task_self(), addr, 4096, FALSE, VM_PROT_READ | VM_PROT_WRITE);
// Read memory from another task (requires task port)mach_vm_read(target_task, remote_addr, size, &local_addr, &local_size);BSD: The POSIX Interface
The BSD layer is what application code actually talks to.
- Process Model: fork(), exec(), wait(), signals — POSIX through and through.
- File Systems: a VFS layer with APFS, HFS+, NFS, SMB, FAT and exFAT. There's no in-kernel ext2/3/4; reading Linux disks takes a third-party FUSE driver.
- Networking: BSD sockets, the full TCP/IP stack, routing, and the
pfpacket filter. - Security: POSIX permissions, file flags, and the MAC Framework that SIP and the sandbox lean on.
BSD sits squarely on top of Mach, even though most application programmers never touch the lower layer. A BSD process is a wrapper round a Mach task. A BSD thread is a wrapper round a Mach thread. The page cache, networking buffers, the lot — all of it ultimately speaks Mach VM.
Linux has no task port to ask. The numbers task_info() returns above (resident and virtual size) come from text files under /proc/self instead, which is the comparison the runner makes. On a Mac, vmmap <pid> and lsmp -p <pid> are the tools that read the Mach side.
IOKit: The Driver Framework
IOKit is Apple's driver framework, written in a restricted subset of C++:
IOKit drives device binding through matching and probing. When new hardware shows up, IOKit hunts for a driver whose matching dictionary lines up with the device's properties. If something fits, the driver's probe() method runs to confirm it really can drive that hardware.
Kernel Extensions vs System Extensions
For most of macOS's life, third-party kernel code shipped as kexts (kernel extensions). Catalina (10.15) marked the beginning of the end: Apple deprecated kexts and pushed everything toward System Extensions that live entirely in user space.
System Extensions come in a few flavours:
- Network Extensions — where VPN clients, content filters, and DNS proxies live now
- Endpoint Security Extensions — anything resembling AV or EDR
- DriverKit Extensions — USB, HID, PCIe, audio, SCSI; basically everything that used to be a kext
DriverKit is Apple's user-space driver framework, providing IOKit-like APIs that run outside the kernel. DriverKit drivers communicate with the kernel through a serialised IPC mechanism. If a DriverKit driver crashes, it does not panic the kernel - it simply restarts. Apple is systematically moving driver categories from kexts to DriverKit, reducing the kernel attack surface.
Hardening on Apple Silicon
Moving drivers out is only half of it. On Apple's own chips, the kernel also defends itself against an attacker who already has a kernel memory-write bug. KTRR locks the kernel's code and read-only data at boot, enforced by the memory controller, so even the kernel can't patch its own text afterwards. Kernel PAC signs return addresses and many function and data pointers, so a corrupted pointer fails authentication instead of being followed (the mechanism is covered in Pointer Authentication and Memory Tagging). PPL, and its successor SPTM on newer chips, moves page-table updates and code-signing state behind a small, more privileged monitor, so a kernel write primitive can't simply map new executable pages. Each one turns "I can write kernel memory" from the end of an exploit into the start of a much harder one.
How Mach and BSD Interact
Mach and BSD are tightly woven together, and the seams are surprisingly clean once you trace them:
One read() call touches all three: BSD owns the POSIX surface, Mach handles VM and the page cache, IOKit drives the disk. None of the layers know all that much about the others, which is the whole trick.
XNU's hybrid shape is proof that decades-old design decisions don't retire quietly. Apple's slow migration of drivers out of the kernel (DriverKit) and the tightening security perimeter (SIP, the signed system volume) show the kernel is still being reshaped — just at glacier-speed Apple pace.