diff --git a/AGENTS.md b/AGENTS.md index dd9853ed5..d57f92475 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -9,7 +9,9 @@ drivers drive in x86-64 macOS guests: which reims-ctl adds to an x86-64 VM's OpenCore image as a prebuilt set. They are to work on every x86-64 macOS release from 11.0 to 26's last; - the option ROM (`firmware/`), the device's UEFI driver in C, which an - x86-64 guest's firmware runs; + x86-64 guest's firmware runs, and its driver module for Ambrosia + (`firmware/ambrosia`), which gives an arm64 guest's firmware an early + console on the device; - QEMU's `reims-graphics-pci` (`vendor/qemu`), in C on that API, which finds the library through pkg-config and links it statically; - `reims-ctl` (`crates/reims-ctl`), the VM manager users run. @@ -62,6 +64,7 @@ The Makefile's header lists every target. README.md lists the requirements. | the guest's kexts, loader and Metal bundle | `guest/`, and the prebuilt set reims-ctl carries, `guest/prebuilt` | | the dist tree QEMU links | `xtask/src/dist.rs` (`cargo xtask dist`) | | the option ROM reims-ctl gives the device | `firmware/`, and the prebuilt ROM, `firmware/prebuilt/ReimsGraphics.rom`, described in [docs/architecture.md](docs/architecture.md) section 11.4 | +| the device's driver module for Ambrosia, the arm64 guests' firmware | `firmware/ambrosia`, described in [docs/architecture.md](docs/architecture.md) section 11.5; how a guest boots with it is [docs/qemu.md](docs/qemu.md), "arm64 guests" | | `cargo xtask` | `xtask/` | | the tests' metallibs and their sources | `tests/shaders` (`tests/suite/fixtures/shaders` links to it, for `guest/bundle`'s build and tools) | | QEMU and VM boots | `vendor/qemu`, `vendor/qemu/hw/display/reims-graphics-pci.c`, [docs/qemu.md](docs/qemu.md) | @@ -125,7 +128,8 @@ passes run again alone. | `xtask/` | `cargo test -p xtask`, rustfmt and clippy on it | | `crates/reims-ctl` | `cargo test`, `cargo fmt --check` and `cargo clippy --all-targets` in it (`cargo xtask check` does not reach it); a change to the QEMU command it builds, or to `scripts/install-ctl.sh`, also needs an install and a VM boot on each host it touches | | `guest/` (kext, bundle, rgfxgate, loader) | `make -C guest test` on a Mac; a change to a source the set is built from also needs `make -C guest prebuilt KDK=`, committing `guest/prebuilt` with it (`make -C guest prebuilt-check` fails otherwise) | -| `firmware/` | build it (`make -C firmware`); a change not meant to alter what the guest's firmware sees keeps the ROM's bytes; a change to what `ReimsGraphics.rom` is built from (its sources, the Makefile, `crates/reims-graphics/include/reims_graphics_wire.h`) also runs `make -C firmware prebuilt` on a Mac, committing the ROM and record it writes | +| `firmware/`, the option ROM | build it (`make -C firmware`); a change not meant to alter what the guest's firmware sees keeps the ROM's bytes; a change to what `ReimsGraphics.rom` is built from (its sources, the Makefile, `crates/reims-graphics/include/reims_graphics_wire.h`) also runs `make -C firmware prebuilt` on a Mac, committing the ROM and record it writes | +| `firmware/ambrosia` | build it (`make -C firmware/ambrosia bundle`); a change to what it does with the device also needs an arm64 guest's boot on Ambrosia with the device and the module ([docs/qemu.md](docs/qemu.md), "arm64 guests") | | `vendor/qemu` | build it (`make qemu`) and realize the device paused (`-S`); a change to `hw/display/reims-graphics-pci.c`, or to what QEMU calls, also needs a VM boot ([docs/lessons.md](docs/lessons.md), [docs/qemu.md](docs/qemu.md)) | | docs and comments | nothing | diff --git a/README.md b/README.md index 87ffbab79..dbe6e2de2 100644 --- a/README.md +++ b/README.md @@ -152,7 +152,9 @@ resize it. [docs/api.md](docs/api.md) section 2 lists every setting and variable `reims-graphics-pci` in C on the C API, which finds the library through pkg-config and links it statically. - The option ROM: `firmware/`, the device's UEFI driver, in C, which the guest's firmware runs. - `firmware/prebuilt` holds the built ROM, which reims-ctl gives the device. + `firmware/prebuilt` holds the built ROM, which reims-ctl gives the device. `firmware/ambrosia` + is the device's driver module for Ambrosia, which gives an arm64 guest's firmware an early + console on the device. - The Vulkan platform's runtime: metal2vulkan's public repository, at the commit the workspace's `Cargo.toml` names. - VMs: `reims-ctl` (`crates/reims-ctl`) creates, boots and manages them. @@ -188,7 +190,8 @@ it needs itself; working on the code needs the following. (`xcodebuild -downloadComponent MetalToolchain`), and Homebrew's `vulkan-loader` and `molten-vk`, the Vulkan the library runs on there. The guest's kexts build with Xcode and a macOS 26 KDK (`guest/Makefile`). -- clang and lld for the option ROM (`firmware/`; on a Mac, Homebrew's `lld`). +- clang and lld for the option ROM and the Ambrosia module (`firmware/`; on a Mac, Homebrew's + `lld`). - For `make qemu`: the `vendor/qemu` submodule (`git submodule update --init vendor/qemu`) and QEMU's build dependencies (`meson`, `ninja`, `pkg-config`, `glib`, `pixman` and so on). @@ -205,6 +208,7 @@ make qemu # qemu-system-x86_64 with reims-graphics-pci on the library, i make clean # remove build/ make -C guest test # the guest side's tests, on a Mac make -C firmware # the option ROM, in firmware/build +make -C firmware/ambrosia bundle # the Ambrosia module and a bundle that starts it, in firmware/ambrosia/build ``` The Makefile's header describes every target and its variables. `make` runs `cargo`, which you can diff --git a/docs/architecture.md b/docs/architecture.md index d7769df87..06c996726 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -124,7 +124,8 @@ crates/ reims-vgpu-video-*/ the host's video decoders and encoders (docs/video) reims-ctl/ the VM manager, a workspace of its own xtask/ testkit, header, dist, metallibs, check -firmware/ the device's option ROM: its UEFI driver, in C, and the prebuilt ROM (11.4) +firmware/ the device's option ROM: its UEFI driver, in C, and the prebuilt ROM (11.4); + firmware/ambrosia, its driver module for Ambrosia, the arm64 guests' firmware (11.5) guest/ the guest's kexts, loader and Metal bundle, and the prebuilt kext set tests/shaders/ the tests' metallibs and their sources scripts/ install-ctl.sh: installs reims-ctl and its QEMU @@ -708,6 +709,44 @@ run, fails where a file the record names has changed. driver calls, and keeps every table's offsets by declaring the services it does not call as opaque pointers. +### 11.5 The driver module for Ambrosia + +`firmware/ambrosia/` is the device's driver module for Ambrosia, the boot firmware of arm64 macOS +guests on QEMU's `vmapple` machine: what the option ROM is to an x86-64 guest's firmware. Ambrosia +loads the module from its configuration bundle, whose `config.plist` names it (`Graphics` > +`Driver`), and calls its entry point once, before it reads the guest's disks. It draws its log on +the framebuffer the module returns, as an early console, and hands the framebuffer to the kernel in +the boot arguments, whose console draws there in turn. The interface is revision 1 of Ambrosia's +graphics driver interface, which `AmbrosiaGraphics.h` declares; Ambrosia's own repository +documents it. + +``` +Makefile clang and lld: the module, build/ReimsGraphics.elf, and a bundle that starts it, + build/ReimsGraphics.cpio (make bundle) +ReimsGraphicsModule.c the entry point: the device's BARs, its control ring and its mode +AmbrosiaGraphics.h Ambrosia's graphics driver interface, revision 1 +config.plist the bundle's configuration, which names the module +``` + +**What the module does.** It finds the device on bus 0, gives BAR 0 an address at the start of the +PCIe window Ambrosia names and BAR 2 the first address after it aligned to its size, where the +guest's kernel keeps them, and turns memory decoding on. Then it does what the option ROM's `Start` +and `SetMode` do: it resets the device, adopts the control rings' counters, asks for the displays, +takes the preferred mode of the first connected display, or 1920 × 1080, with rows a multiple of +256 bytes, and points the display at the region's memory from `RGFX_ALLOCATIONS_OFFSET` with +`SET_SCANOUT`, polling the completion ring for each answer. That memory is the framebuffer it +returns. It turns on neither bus mastering nor MSI-X, and leaves the scanout set, for the kernel. +It returns 1 where bus 0 has no device, and 2, saying why in Ambrosia's log, where the device can't +be driven; Ambrosia then boots with no console but the UART. + +**The build.** The module is an ELF64 aarch64 static PIE, the format Ambrosia loads: clang compiles +the C freestanding for `aarch64-unknown-none-elf`, position-independent and with strict alignment, +and `ld.lld -pie --no-dynamic-linker -z text` links it with no `PT_INTERP`, no relocations but +`R_AARCH64_RELATIVE` ones, and the entry point as its `e_entry`. The PCIe window is device memory +to the module, so it reads and writes the rings 8 bytes at a time with plain loads and stores +ordered by `dmb sy`, where the option ROM uses acquire and release atomics. Its C follows the option +ROM's style, on `stdint.h`'s types, since it has no UEFI. + --- ## 12. QEMU diff --git a/docs/development.md b/docs/development.md index 37c9f073d..1f387b576 100644 --- a/docs/development.md +++ b/docs/development.md @@ -16,7 +16,9 @@ crates/ its eight crates (below), and reims-ctl, a xtask/ cargo xtask (below) firmware/ the reims-graphics device's option ROM: its UEFI driver, in C, and the prebuilt ROM reims-ctl - carries + carries; in firmware/ambrosia, the device's + driver module for Ambrosia, arm64 guests' + firmware guest/ the guest's kexts, loader and Metal bundle, and the prebuilt kext set reims-ctl carries tests/ diff --git a/docs/qemu.md b/docs/qemu.md index 34caab3c2..f520fd2dc 100644 --- a/docs/qemu.md +++ b/docs/qemu.md @@ -830,3 +830,56 @@ or TCG on Ambrosia, our own arm64 firmware, which enters macOS's kernel itself. Virtualization.framework's firmware, AVPBooter, does not boot on the machine's layout now. The branch's commits for it ("What vendor/qemu carries", above) say what each one changes and why. + +### arm64 guests: the device on Ambrosia + +`firmware/ambrosia` is the device's driver module for Ambrosia +([architecture.md](architecture.md) 11.5), which gives an arm64 guest an +early console on the device. `make qemu` builds no `qemu-system-aarch64`, +and runs configure again when its build directory holds an `aarch64-softmmu` +build, so one with the device is configured by hand in a directory of its +own, on the same dist tree, with `make qemu`'s options and `vmapple`'s: + +```sh +make # cargo xtask dist: target/dist-vulkan +mkdir -p build/qemu-aarch64 && cd build/qemu-aarch64 +PKG_CONFIG_PATH=$PWD/../../target/dist-vulkan/lib/pkgconfig ../../vendor/qemu/configure \ + --target-list=aarch64-softmmu --enable-hvf --enable-reims-vgpu --enable-gnutls \ + --disable-docs --disable-bsd-user --disable-linux-user --disable-tools +make qemu-system-aarch64 +``` + +configure's summary says `reims-vgpu support: YES` and `HVF support: YES`, +and QEMU's build signs the binary with the hypervisor entitlement. A guest +boots on Ambrosia as `vendor/qemu`'s `docs/system/arm/vmapple.rst` says, +with the device and a configuration bundle that names the module +(`make -C firmware/ambrosia bundle`) added; `REIMS_VGPU_WINDOW=0` keeps the +library's window shut, and QMP's `screendump` shows the display: + +``` +-device reims-graphics-pci +-device loader,file=firmware/ambrosia/build/ReimsGraphics.cpio,addr=0x120000000,force-raw=on +``` + +**Measured** on the M3 Max under HVF on 2026-11-08, with `vendor/qemu` at +`aef8a7cf` configured as above on the library of `61b1aac9e`, Ambrosia's +`44f0dac` and APFS clones of a macOS 26.6.2 (25G83) guest's disks: +- With the module, in each of two boots, Ambrosia logged `reims-graphics + at 00:05.0: 1920x1080, framebuffer 0x80400000, registers 0x50000000` and + started its console there. The screendumps showed its log from the + console's start to the kernel's entry, and the kernel's progress bar + beneath it, advancing; the guest has no driver for the device (our kexts + are x86-64), so that is all the display showed. The guest answered SSH + 12 s after QEMU started, with System Integrity Protection enabled and + `kern.bootargs` empty, and QEMU logged no `reims-vgpu: fatal` line. Once + the second boot answered SSH, QEMU's `info pci` had BAR 0 at 0x50000000 + and BAR 2 at 0x80000000, where the module put them, and the other + devices' BARs from 0x50001000 up, where the kernel put them. +- With the device and no module, the guest answered SSH 11 s after QEMU + started, with SIP enabled and no boot arguments; the display stayed + black, and `info pci` listed both of the device's BARs as not mapped. +- The device as it was before `7aee20f4`, with the scanout's dirty log + (`c7909d60`'s `reims-graphics-pci.c`, on the library of `a851db6a7`), + stopped QEMU under HVF as soon as the module set the scanout: + `accel/hvf/hvf-all.c:74:do_hv_vm_protect: assertion failed: + (!(size & ~page_mask))`.