How darwin-on-arm/xnu works
Step 1: Identify the Project Type and Purpose
• This is the XNU kernel—Apple’s hybrid operating-system kernel combining the Mach microkernel (osfmk), the BSD network/UNIX layer (bsd), the I/O Kit driver/plug-in architecture (iokit), and supporting libraries (libkern, libsyscall, libsa, libkmod).
• Its purpose is to bootstrap hardware (bootloader/libsa/pexpert), provide low-level kernel services (Mach IPC, scheduler, virtual memory, device I/O), expose UNIX/POSIX APIs (BSD), and load third-party kernel extensions (kexts via I/O Kit).
Step 2: Analyze the File Structure
• Top-level build scripts and Makefiles coordinate building everything for multiple architectures.
• SETUP and config directories define build setups, target configs, and export headers.
• osfmk contains the Mach microkernel core: IPC, scheduling, VM, low-level platform code per architecture.
• bsd implements the POSIX layer: VFS, networking stack, syscalls, device pseudo-file systems, man pages.
• iokit holds the driver framework: user-space headers (IOKit/IOKit/*.h), kernel-space classes (iokit/Kernel/*.cpp), built-in platform drivers, examples, and tests.
• libkern is the common kernel support library (C++ STL-like classes, crypto, zlib).
• libsyscall generates and wraps user-space syscall stubs.
• libsa is the standalone boot library used by the XNU bootloader (early startup code).
• pexpert provides platform expert code (board initialization, bootargs).
• tools, tests, and man/* directories supply auxiliary utilities, test suites, and documentation.
Step 3: Key Insights from README
• Building XNU is a multi-step make process that targets OS components (osfmk, mach_kernel, BSD, IOKit).
• Environment variables (SRCROOT, OBJROOT, DSTROOT, SYMROOT) control where artifacts go.
• Multiple “projects” (xnu, xnu_debug, libkmod, Libsyscall) support building different deliverables (boot image, debug kernel, kext libraries).
1. Bootloader Layer: libsa + pexpert
2. Mach Microkernel (osfmk): Scheduler, IPC, VM, kernel primitives
3. BSD UNIX Layer (bsd): VFS, Networking stack, System Calls
4. I/O Kit Framework (iokit): Driver base classes, user-kernel bridge, power management
5. Kernel Libraries (libkern, libkmod, libsyscall): supporting code for allocators, data structures, system-call stubs
6. Kernel Extensions (kexts): dynamically loadable drivers and file-systems
7. User-Space Interfaces: syscall wrappers, headers installed under /usr/include & frameworks
– Bootloader → Mach Microkernel: loads and transfers control to mach_kernel
– Microkernel ↔ Device Drivers (I/O Kit, built-ins): hardware I/O mediated via IOService, I/O Kit user-client interfaces
– Microkernel ↔ BSD Layer: Mach ports used for implementing syscalls and networking protocols
– BSD Layer → User Applications: syscalls and POSIX API (libsyscall wrappers)
– I/O Kit → Kernel Extensions: driver plug-in discovery via IOKit registry
– Kernel Libraries ← all layers: common services, data structures, crypto
c. Architectural Patterns and Principles:
– Layered hybrid kernel: Mach microkernel at core, layered UNIX subsystem on top, driver framework as separate plug-in architecture
– In-kernel C++ (I/O Kit, libkern) combined with C for low-level code
– Makefile-driven componentization enabling multi-architecture builds and selective component building
d. Key Technologies and Frameworks:
– Mach IPC and VM primitives (osfmk)
– POSIX/VFS/Networking in BSD subsystem
– I/O Kit driver model (IOService, IOCatalogue, IOPower)
– Kext loading/unloading mechanisms
– Build system: recursive Makefiles, SETUP scripts, config exports
• Boot path: libsa → pexpert → mach_kernel.
• Microkernel boundaries: separate the Mach core from the BSD and IOKit subsystems.
• Dynamic loading: how kexts plug into I/O Kit.
• User-space boundary: system call stubs (libsyscall) connecting user applications to kernel.
• Clear labels: name each block (e.g., “Mach Microkernel (osfmk)”), each subsystem, each layer.
– Another for OS services (bsd)
– Another for driver framework (iokit, kexts)
– Another for user-space interfaces (libsyscall, headers)
Step 7: Detail and Completeness
• Be exhaustive in capturing components—don’t collapse BSD or I/O Kit internals into “misc.”
• Per-architecture configuration and build scripts as side artifacts, if relevant to deployment.
• Both static (compiled-in drivers) and dynamic (kext) plugin models.
Summary