indicogniBuild a VMM

Visual tour

How macvmm works

Five pictures of the VMM you build in this course, each with a few plain paragraphs. You need no virtualization or ARM background to read them.

A virtual machine is a computer made of software, running on your Mac. The program that builds it and keeps it running is a virtual machine monitor, or VMM. macvmm is the VMM this course builds, in Rust. The operating system that runs inside the virtual machine is the guest. Your Mac and macOS are the host. Chapter 00 builds this picture slowly. This page shows it in five diagrams.

The whole picture

The whole picture Four layers, top to bottom. 1. The guest: Linux, U-Boot then FreeBSD, or a test guest. It believes it runs on a QEMU virt board. 2. The macvmm process, one macOS process that sandboxes itself. It holds the command line (crates/macvmm), the VMM library (crates/vmm: vCPU threads, devices, virtio, loaders and the device tree) and the FFI bindings (crates/hvf). Beside it, on the host, are the input files (kernel, initramfs, disk image), which macvmm reads, and the terminal, which carries the serial console both ways. 3. Hypervisor.framework and the macOS kernel, called through the FFI bindings: the stage-2 memory map, vCPUs, the in-kernel GIC interrupt controller and the virtual timer. 4. The Apple Silicon CPU, which runs the guest's instructions directly, at full speed. macvmm: one macOS process, sandboxedHypervisor.framework + macOS kernelThe guestLinux, U-Boot then FreeBSD, or a testguest. It believes it runs on a QEMUvirt board.Command linecrates/macvmmVMM librarycrates/vmmvCPU threads, devices, virtio,loaders and the device treeFFI bindingscrates/hvfHost fileskernel,initramfs,disk imageTerminalthe serialconsoleStage-2 memoryvCPUsIn-kernel GICVirtual timerApple Silicon CPUruns the guest's instructionsdirectly, at full speed
The four layers: the guest, the macvmm process, Hypervisor.framework with the macOS kernel, and the CPU.

Read it from the top. The guest is Linux, the U-Boot firmware that then boots FreeBSD, or one of the small test programs in the repository. The guest believes it runs on QEMU's virt board, a standard ARM board that stock kernels and firmware already support. macvmm copies that board's layout, so they run unchanged.

Below the guest is macvmm, one ordinary macOS process built from three Rust crates. The command line, crates/macvmm, reads your flags and sets up your terminal for the guest's serial console. The VMM library, crates/vmm, does the real work: it runs one thread per virtual CPU, or vCPU, and holds the device models, virtio, the loaders and the device tree. The FFI bindings, crates/hvf, are the hand-written calls from Rust into Apple's C API. After it opens its input files, the process locks itself in a macOS sandbox. From then on, a guest that takes over macvmm still cannot open a new file or reach the network.

Below macvmm is Hypervisor.framework, or HVF, Apple's API for running virtual machines. It and the macOS kernel map guest memory, run the vCPUs, and provide an interrupt controller, the in-kernel GIC, and a timer. At the bottom is the Apple Silicon CPU. The guest's instructions run on it directly, at full speed, with nothing translating them. Chapter 01 shows where each layer sits.

The heart of a VMM: run, exit, emulate, resume

The heart of a VMM: run, exit, emulate, resume A loop of five steps. 1. macvmm calls hv_vcpu_run and the guest runs at full speed. 2. Exit: the guest touched a device address or made a hypercall, so the CPU stops it and hv_vcpu_run returns. 3. macvmm decodes the syndrome register, ESR_EL2, which says why the guest stopped. 4. A device model, or the PSCI code for a hypercall, emulates the access. 5. macvmm advances the program counter to the next instruction (after a hypercall the CPU has already done this), then calls hv_vcpu_run again. Measured on 2026-11-22 on an Apple M5 Pro with scripts/run-linux.sh --cpus 4 --stats, booting Linux to a shell and powering it off: 35,271 exits in total, the same in each of 4 runs. 35,235 were device accesses (MMIO data aborts), 25 were hypercalls and 11 were other kinds. There were no timer or WFI exits, because the in-kernel GIC handles those inside Hypervisor.framework. hv_vcpu_runthe guest runs atfull speedExitdevice accessor hypercallDecode the ESRwhy it stoppedEmulatea device modelor PSCI answersAdvance the PCto the nextinstructionMeasured: one Linux run, 4 vCPUs35,271 exits in total.35,235 were device accesses (MMIO),25 were hypercalls, 11 other kinds.No timer or WFI exits at all:the in-kernel GIC handles those.
The exit loop: run the guest, take an exit, decode why, emulate, advance the PC, run again.

If the guest runs directly on the CPU, how does macvmm ever get a say? The CPU stops the guest whenever it does something that only the VMM may handle. That stop is a VM exit: hv_vcpu_run, the call that runs the guest, returns to macvmm. Each vCPU thread spends its life in this loop.

Most exits are device accesses. The guest reads or writes an address where a device should be, finds no memory there, and the CPU reports a data abort. The next diagram shows why there is no memory. The other common exit is a hypercall: the guest runs the hvc instruction to ask the VMM for a service on purpose, such as starting another CPU.

macvmm then reads the syndrome register, ESR_EL2. It says why the guest stopped: for a data abort, which register, how many bytes, and whether it was a read or a write. A device model does what the real hardware would do, or the PSCI code answers the hypercall. PSCI is ARM's standard interface for turning CPUs on and off. macvmm moves the program counter past the instruction and runs the guest again. Chapter 07 builds this loop, and chapter 08 adds hypercalls and PSCI.

The numbers in the box are a real count. On 2026-11-22 on an Apple M5 Pro, scripts/run-linux.sh --cpus 4 --stats booted Linux to a shell, then powered it off. The four vCPUs took 35,271 exits, and the total was the same in each of 4 runs. Only the split between the vCPUs changed. Linux printed 12,883 bytes to the serial console in that run, and each byte costs at least one device register write, so at least one exit. There were no timer exits and no WFI exits. WFI, "wait for interrupt", is the instruction an idle CPU runs. The in-kernel GIC handles both inside Hypervisor.framework.

The board the guest sees

The board the guest sees The guest-physical address map of macvmm's virt board, lowest address at the top, not to scale. 0x0000_0000: flash, for firmware, mapped only with --bios. 0x0800_0000: the GIC interrupt controller's distributor, then its redistributors from 0x080A_0000; Hypervisor.framework answers these accesses itself, with no exit. Then the trapped devices, which are deliberately left unmapped so that every access exits to macvmm: 0x0900_0000 the PL011 UART, the serial console; 0x0901_0000 the PL031 RTC, the clock; 0x0a00_0000 the virtio-mmio slots, 32 of them 0x200 bytes apart. Unused holes follow, also unmapped. 0x4000_0000: RAM, 512 MiB by default, mapped, so the guest reads and writes it with no exit. Trapped: every accessexits to macvmm0x0000_0000Flashfirmware code,mapped only with --bios0x0800_0000GICinterrupt controller.Handled in HVF: no exit0x0900_0000PL011 UARTthe serial console0x0901_0000PL031 RTCthe clock0x0a00_0000virtio-mmio32 slots, 0x200 apartunused holes, alsounmapped0x4000_0000RAM512 MiB by default.Mapped, so accessesnever exit.
The guest-physical memory map: flash, the GIC, three trapped devices and RAM, with their base addresses.

A guest finds its devices by address. Every byte it reads or writes has a guest-physical address, or IPA, and the board's memory map says what lives at each one. macvmm's map copies QEMU's virt board. One file, crates/vmm/src/arch/layout.rs, is the single source of truth, and the reference card shows it.

What matters is what macvmm leaves out. RAM is mapped: macvmm asks Hypervisor.framework to enter guest RAM in the CPU's second stage of address translation, stage 2, which turns each IPA into an address in macvmm's own memory. The guest then reads and writes RAM with no exit at all. The UART, the clock and the virtio slots are deliberately not mapped. An access there has nowhere to go, so the CPU stops the guest and macvmm's device model answers. A device in macvmm is code that runs when an exit arrives. Chapter 05 explains these holes.

Two regions behave differently. The GIC's addresses belong to Hypervisor.framework's in-kernel GIC, which answers those accesses itself, again with no exit (chapter 12). Flash holds firmware, and macvmm maps it only when you boot firmware with --bios (chapter 21).

From macvmm run to a shell prompt

From macvmm run to a shell prompt Eight steps, top to bottom. 1. macvmm opens the input files, reading each one once. 2. It enters its sandbox: no new files or network after this. 3. It builds the machine: the VM, the GIC, RAM, the devices and the device tree. 4. It loads the guest: the device tree, kernel and initramfs go into guest RAM. 5. vCPU 0 starts at the kernel's entry point with register x0 holding the device tree's address. 6. Linux boots, and PSCI CPU_ON wakes the other vCPUs. 7. A shell prompt, / #, appears on the serial console. 8. poweroff -f makes Linux call PSCI SYSTEM_OFF, and macvmm exits with code 0. A reboot instead calls PSCI SYSTEM_RESET, and macvmm goes back to step 3 and builds a fresh machine from the files it already read. Open the input filesread each one onceEnter the sandboxno new files or networkafter thisBuild the machineVM, GIC, RAM, devices,device treeLoad the guestdevice tree, kernel,initramfs into RAMStart vCPU 0kernel entry point,x0 = device tree addressLinux bootsPSCI CPU_ON wakes theother vCPUs/ #a shell promptmacvmm exitsexit code 0poweroff -f: PSCISYSTEM_OFFreboot:PSCISYSTEM_RESET
The boot sequence, from opening the input files to a shell prompt, with power-off and reboot.

macvmm first opens every file it needs, once: the kernel, the initramfs and any disk images. The initramfs is a small archive that Linux unpacks as its first filesystem. Then macvmm enters its sandbox, and from that point it cannot open a file by path at all.

Next it builds the machine. It creates the virtual machine and its GIC, maps RAM, adds the devices, and writes a device tree, a small binary description of the board that tells Linux what lives where. It copies the device tree, the kernel and the initramfs into guest RAM. Then it points vCPU 0 at the kernel's first instruction, with register x0 holding the device tree's address. The arm64 boot protocol primer lists the rest of what Linux expects at that moment. Chapter 10 writes the device tree, and chapter 13 loads the kernel.

Linux does the rest. It asks PSCI to start the other vCPUs with CPU_ON, finds its devices in the device tree, and starts a shell. When you type poweroff -f, Linux calls PSCI SYSTEM_OFF and macvmm exits with code 0. A reboot calls SYSTEM_RESET instead, and macvmm builds a fresh machine from the bytes it already read. Chapter 14 boots Linux to that shell.

How a disk write travels

How a disk write travels Sequence between the Linux virtio-blk driver in the guest, a vCPU thread in macvmm, the virtio-blk worker thread in macvmm, and the disk image, a file on the host. 1. The driver puts the request's descriptors in guest RAM and adds them to the available ring. 2. The driver writes the QueueNotify register, which is an MMIO exit to the vCPU thread. 3. The vCPU thread wakes the worker. 4. The vCPU thread returns to the guest at once. 5. The worker walks the descriptor chain, checking every address against RAM and stopping a chain that loops. 6. The worker calls pwrite on the disk image. 7. The worker writes the used ring in guest RAM. 8. The worker raises the device's interrupt through the in-kernel GIC. 9. The driver sees the used ring and completes the request. Linux driver(in the guest)vCPU thread(macvmm)virtio-blk worker(macvmm)Disk image(host file)puts descriptorsin guest RAMwrites QueueNotify:an MMIO exitwakes the workerreturns to theguest at oncewalks the chain:bounds-checked,loop-proofpwritewrites the usedringraises the interrupt through thein-kernel GICcompletes therequest
One virtio-blk write, from the Linux driver through the vCPU thread and the worker thread to the disk image, and the interrupt back.

An emulated disk controller would cost an exit for every register it touches. Virtio is a family of devices designed for virtual machines instead. The driver and the device share virtqueues: rings of descriptors in guest RAM, each one pointing at a buffer. Most of the work then happens in shared memory, with no exits. Chapter 15 builds the transport.

To write to the disk, the Linux driver fills in descriptors for the request, then writes the QueueNotify register. That one write is the only exit needed to start the request. The vCPU thread wakes the disk's worker thread and returns to the guest at once, so the guest keeps running while the disk works.

The worker treats everything in the ring as hostile. It walks the descriptor chain, checks every address against RAM, and stops a chain that loops back on itself. Then it calls pwrite on the disk image file, records the result in the used ring, and raises the device's interrupt through the in-kernel GIC. The driver sees the used ring and completes the request. Chapter 16 builds the hardened virtqueue, and chapter 18 the disk.

Build it yourself

Everything inside the macvmm process on this page is code you write in the course. Chapter 00 begins from zero. The glossary defines every bold term in one line.