← All posts

Darwin & XNU: Inside the macOS Kernel

Date: 2025-11-24 Tags: macOS, Kernel, Mach, BSD, C Author: Wissam Ztaoui


Introduction

macOS is not Linux. It’s not purely Unix. It’s Darwin. At its heart beats the XNU kernel (X is Not Unix). XNU is a hybrid kernel, a Frankenstein monster stitching together the microkernel philosophy of Mach with the monolithic robustness of BSD.


1. The Hybrid Architecture

Mach (The Microkernel)

Mach handles the lowest-level primitives:

BSD (The Monolith)

Wrapped around Mach is the BSD layer (derived from FreeBSD). It provides:

Interaction: When you call fork() (BSD), XNU internally calls task_create() (Mach).


2. Mach Ports: The Nervous System

In Linux, “everything is a file”. In Mach, “everything is a port”. A port is a secure, kernel-managed communication channel.

Example: When you launch an app, launchd gives it a set of bootstrap ports. If the app wants to talk to the Window Server, it looks up the service name, gets a send right to the Window Server’s port, and sends a message.


3. IOKit: Object-Oriented Drivers

Most kernels (Linux, Windows) write drivers in C. Apple created IOKit, a subset of C++ for writing drivers.

Power Management: IOKit handles power states (sleep/wake) automatically through the object hierarchy.


4. The Boot Process

  1. BootROM: Hardware root of trust. Loads iBoot.
  2. iBoot: The second-stage loader. Verifies and loads the kernel cache.
  3. XNU Kernel: Initializes Mach, then BSD, then IOKit.
  4. launchd: The first user-mode process (PID 1). It replaces the traditional init and systemd. It manages daemons, agents, and XPC services.

5. Security: Kexts vs. System Extensions

Kernel Extensions (Kexts)

Traditionally, drivers ran in Ring 0 (.kext). A bug in a kext panicked the entire OS.

System Extensions (The Future)

Apple is deprecating Kexts. New extensions (Network, Endpoint Security) run in User Space.


Conclusion

Darwin is a fascinating study in OS history. It combines the academic purity of Mach’s IPC with the industrial strength of BSD, wrapped in a modern C++ driver framework. It is arguably the most complex mainstream kernel in existence.


← Back to all posts