XNU Kernel Architecture: Mach, BSD and IOKit
How XNU combines Mach, a BSD layer and IOKit in one kernel: what each part owns, how a process maps to a task, and how a read call crosses all three.

The usual one-line description of XNU is "a hybrid kernel", and it explains almost nothing until you see which responsibility sits in which part. Mach owns the abstractions that look strange to a Unix programmer: tasks, threads, ports and virtual memory objects. The BSD layer owns the abstractions that look familiar: processes, file descriptors, signals and sockets. IOKit owns the abstraction that looks like neither: a registry of C++ objects representing hardware. They are compiled into one binary and call each other directly, with no message passing between them in the common path. This post draws the boundaries on macOS 15, using the source tree layout, one object that spans two layers and one system call followed all the way down. It is a closer look at a section of the complete guide to macOS kernel internals.
One kernel, three personalities
In the original Mach 3 design, the kernel was a small core and the operating system personality ran as a server in user space. XNU went the other way. It took the Mach core and BSD, and linked them into the same address space at the same privilege level, so a BSD system call reaches a Mach function with an ordinary C call instead of a message. The reason was cost: crossing an address space boundary for every file operation was too slow in practice. The structure that remains is conceptual and organizational, and it matters for reading the code, because the three parts follow different conventions.
| Mach | BSD | IOKit | |
|---|---|---|---|
| Source directory | osfmk/ | bsd/ | iokit/, libkern/ |
| Language | C | C | C++ (restricted) |
| Main objects | task_t, thread_t, ipc_port_t, vm_map_t | struct proc, struct vnode, struct socket | IOService, IORegistryEntry, OSObject |
| Error type | kern_return_t | errno integers | IOReturn |
| User-space interface | Mach traps and messages | System calls | IOKit.framework, user clients |
| Typical naming | thread_create, vm_map_enter | fork1, vn_open | IOService::start |
You can often tell which part you are in from the identifiers alone. A function that returns KERN_SUCCESS is Mach, one that returns 0 or EINVAL is BSD, and one that returns kIOReturnSuccess is IOKit.
What Mach owns
Mach is responsible for the resources every program needs, regardless of its Unix identity.
Tasks and threads. A task_t is an address space plus a set of port rights and a group of threads. A thread_t is a schedulable execution context, with its own register state, priority and a kernel stack. The scheduler, which picks the next thread for each processor, works in terms of threads and knows nothing about processes.
Virtual memory. vm_map_t is an address space, a sorted set of entries each mapping a range of addresses to a vm_object_t at an offset. A vm_object holds pages and a pager. The page fault handler, copy-on-write, the memory compressor and mmap are all built on these. The Mach VM calls are vm_allocate, vm_protect, vm_map and their 64-bit forms mach_vm_*.
IPC. An ipc_port_t is a kernel message queue, and a task refers to it by a name in its ipc_space_t. Rights to ports are the capabilities by which a task controls other kernel objects. mach_msg_trap is the single trap that sends and receives.
Time and synchronization. Timers, wait queues, locks (lck_mtx_t, lck_spin_t) and the thread blocking primitive thread_block are Mach. BSD code that sleeps, such as msleep, ends up in Mach's wait queue code.
What BSD owns
The BSD layer implements the interfaces that Unix programs are written against.
Processes and credentials. struct proc holds the PID, parent, process group, credentials (kauth_cred_t), file descriptor table and signal state. Its p_pid is what getpid() returns. Each BSD process has a task and a main thread in Mach, and the code that creates a process allocates both.
Files. A file descriptor indexes a table of struct fileproc entries, each pointing at a struct fileglob that points at a vnode, a socket or another object type. Vnodes represent files in a mounted file system, with an operations vector that file system drivers fill in. The path lookup function is namei.
Networking. Sockets, protocols, interfaces and the routing table: struct socket, struct protosw, struct ifnet. Code in bsd/netinet implements IP, TCP and UDP.
**System calls and sysctl.** The table of BSD system calls is generated from bsd/kern/syscalls.master, and the sysctl tree you can browse from user space is defined across bsd/.
Because the BSD layer is derived from FreeBSD, the structure of a FreeBSD kernel is a useful map, but Apple's version differs in many places, and the file system (APFS and the layers above it), the security framework and the process model have diverged. Treat resemblance as a hint, not a guarantee.
What IOKit owns
IOKit is the device layer, and the only part of XNU written in C++. It uses a restricted subset: no exceptions, no multiple inheritance in drivers, no standard templates, and a custom runtime type system provided by libkern. A driver class derives from IOService, and objects are reference-counted through OSObject.
The central structure is the IORegistry, a tree of IORegistryEntry objects. A driver (the client) attaches to the object that supplies the device (its provider). Matching decides what attaches to what: each driver declares personalities, dictionaries describing what it matches, and the system tests candidates and calls probe on them, picking the best score, then start. Families, such as IOUSBHostFamily and IONetworkingFamily, provide shared behavior for a bus or device type, so a specific driver contains only what is particular to its hardware.
IOKit is also where kernel and user space meet for devices. A kernel driver can publish a user client, a subclass of IOUserClient, and a process opens it through IOServiceOpen. The process then calls into the driver with IOConnectCallMethod, which becomes a method dispatch on the user client object, usually through a table of selectors the driver defines. That mechanism, and not a device file in /dev, is how most macOS drivers expose themselves.
A single object that spans two layers
The process is the clearest example of two layers sharing one entity. A running program has all of these at once:
struct proc (bsd/sys/proc_internal.h)
p_pid, p_ucred, p_fd (file descriptor table), p_sigacts
| p_task (via proc_task())
v
task_t (osfmk/kern/task.h)
map (vm_map_t), itk_space (port rights), threads list
|
v
thread_t (osfmk/kern/thread.h)
one per user thread; its ->uthread is a struct uthread (BSD per-thread state)The BSD process points to the Mach task, and each Mach thread carries a BSD uthread structure with the per-thread state that Unix needs, such as the signal mask and the current system call's arguments. The two layers each keep what they need and link to each other, and either can be found from the other. When kill(pid, SIGTERM) is called, BSD finds the proc by PID, decides which thread should handle the signal and asks Mach to interrupt it. When the Mach task's last thread is destroyed, the BSD side runs the exit path to clean up files and notify the parent.
You can see this pairing from user space. Every PID has a task port, which a privileged and entitled process can obtain with task_for_pid, and a debugger uses it to read another process's memory through mach_vm_read. The restrictions on that call, enforced by the security layers described in the guide, exist because the task port is the right to control the process.
Following a read call through all three
A request that reads a file shows each layer doing its part. The program calls read(fd, buf, n), and the C library traps into the kernel.
BSD. The trap dispatches to
read_nocancelorreadinbsd/kern/sys_generic.c, which looks up the descriptor in the process'sp_fdtable, gets thefileproc, and calls itsfo_readoperation. For a regular file that leads tovn_readand then to the file system's read operation through the vnode's operations vector.File system and Mach VM. The file system, such as APFS, resolves the file offset to cached pages. File data is cached as pages of the vnode's memory object, a Mach
vm_object, so the read first checks whether the pages are resident. If they are, the kernel copies the data to the user's buffer withcopyout, and the call returns.Mach pager. If the page is not resident, a page-in request goes to the object's pager, which for a file is the vnode pager, and the file system issues a read.
IOKit. The file system hands a block request to the storage stack, which is a chain of IOKit objects: a partition or container (
IOMedia), a block storage driver and finally the controller driver for the NVMe or other device. The controller driver programs the hardware, and an interrupt later completes the request, wakes the waiting thread through a Mach wait queue and the page becomes resident.
So one read touches BSD for the descriptor and file semantics, Mach for the caching and the blocking, and IOKit for the hardware, and all of it is function calls inside the one kernel. A message is involved only where the design says so: with a user client, or a Mach service in user space.
What lives outside the kernel
Much of what you might expect in the kernel is not in XNU. launchd, the init process, is user space and is also the Mach bootstrap server, handing out send rights to named services. Name resolution, power management policy, window management, Bluetooth host stack pieces and the configuration database run in daemons. Even many device drivers have moved out under DriverKit. The kernel keeps the mechanisms that must be privileged, and policy moves to processes that can crash and restart. That policy split is why a macOS system often recovers from a failing service where a monolithic kernel would panic.
Where this does not fit
The three-part description is a teaching model. In the real code, boundaries blur: Mach code calls into BSD for credentials, BSD code manipulates Mach structures directly, and IOKit uses both. Do not assume a clean layering when reading source, and do not assume a rule such as "BSD never calls IOKit" holds. The model also describes the open-source xnu tree. Apple ships additional closed components in its kernel binary, including parts of the platform and security code, so you cannot reproduce the shipping kernel by building the public source. Finally, the details move with releases, so confirm any structure or function name against the tag matching kern.version before you rely on it.


