Anees IqbalandClaude 6783a94576 ✨ A drain of one channel stamps one slot, so it submits one command buffer and not eleven
Pathway: x86 macOS / Linux Vulkan, drain worker. Behavior change on the child
FIFO's completion-stamp path, switched by `REIMS_VGPU_STAMP_COALESCE`.

THE MEASUREMENT THAT ASKED FOR THIS

`cb718e5f` split the drain residue's `regs` span three ways. Two driven macos-13
`blur=40` boots (`pin-split`), medians over 74 and 75 census windows:

    tailrd   0.49 / 0.48 ms/s      59 /  58 ns per op
    headwr   1.00 / 0.99 ms/s     128 / 127 ns per op
    stamp   92.29 / 98.59 ms/s   12266 / 13334 ns per op

    sum vs regs_us = 1.00 on every window

The two accesses that really are four-byte guest word accesses cost **1.5 ms a
second between them**. The stamp is **98.4 % of the span at 12.3 microseconds a
packet**, because `write_stamp` is not a word write: on the Vulkan arm it takes
the engine lock, binds the guest RAM reference, records a barrier and a
`cmd_update_buffer`, reserves a timeline value, and **submits a command buffer**.

`engine_delta` says the same thing from the other side, and this is the part that
generalizes past the stamp:

    batch_opens=6812  batch_joins=3934  batch_flushes=6812  batch_flush_draws=10746

**`batch_flushes` equals `batch_opens` exactly, at 1.58 draws a flush.** The draw
batch exists to amortize submits across draws and was never allowed to
accumulate any, because `write_stamp_after_guest_writes` appends to the open
batch and then flushes it. Every packet's stamp flushed whatever draws had
gathered.

WHAT THE CONTRACT LICENSES

Two facts, neither of them a heuristic:

1. **A stamp wait is `>=`, not `==`.** `StampWait::satisfied_by` is
   `current.wrapping_sub(self.value) as i32 >= 0`, so a wait on V is discharged
   by any value at or past V in wrapping-signed order. Intermediate values need
   never appear.
2. **A drain of one channel stamps one slot.** `stamp_index` is read once from
   the channel's register block *before* the loop, so every packet in a
   `drain_child_fifo` call writes the same slot. A run of writes to one slot is
   observable only through its greatest value unless the guest samples between
   them.

So `drain_child_fifo` now latches each packet's completion stamp into a
`PendingStamp` and writes once, after the loop. A driven boot averages **11.6
packets per drain call** (`packets` ~6400/s over `setup_calls` ~550-600/s), so
this removes roughly ten submissions in eleven.

THE TWO HAZARDS IT HAD TO DISPOSE OF, AND HOW

**A wait on the slot the drain is holding.** `note_packet_stamp_waits` reads the
word out of guest RAM, which is now stale by exactly the stamps this drain has
latched. A later packet in the same drain waiting on that slot would read the old
value, return `Hold`, and park the channel against a stamp this device is itself
sitting on — a deadlock introduced by the coalescing rather than by the guest. It
now consults the latch first, and answering from it is not a shortcut: packets
are processed in order on this thread, so a latched stamp is work that finished
before the waiting packet was decoded. Counted as `packet_stamp_wait_met_pending`
so the reliance is measurable rather than assumed.

**A latched stamp that never lands.** Every `break` in that loop falls through to
one site after it, which is why one flush is enough — a drain that stops on a
held stamp, a deferred translation, a decode fault or a host-action yield owes
what it latched exactly as one that ran the ring dry does. Leaving one unwritten
is a guest waiting on a word that never arrives, and its own deadline is a
second, so it would present as a stall rather than a hang.

`PendingStamp::latch` keeps the **maximum in wrapping-signed order**, not the
last value seen. For a well-formed guest those are the same value; taking the
maximum means this device cannot publish a slot going backwards even if a
regressing stamp arrives, which would unsatisfy a wait the guest had already been
told was met.

The main FIFO is unchanged and passes `None`: it stamps per packet and its slot
is not fixed across a drain, so neither fact above holds there. It also executed
zero times on this workload.

SWITCH

`REIMS_VGPU_STAMP_COALESCE=off` restores the per-packet arm on the same binary,
because a change to *when* a completion becomes visible fails as a stall or a
stutter rather than as a decline, and both arms have to be reachable for one A/B.
Off is a refusal and never a permission — one write per packet is strictly more
work than one per drain, and the coalesced arm needs no capability the per-packet
arm does not.

VERIFIED

- 1881 tests pass (+2), `--test-threads=1`, backend-vulkan,host-window.
- Both new tests fail without the change. `a_coalesced_stamp_keeps_the_latest_
  value_across_the_u32_wrap` was probed by stubbing `latch` to last-write-wins
  and fails on the regressing-stamp assertion; the file was restored by copying
  it back, not with git.
- Clippy `-D warnings` clean on all three arms.
- `scripts/feature-matrix`: all supported arms compile, warnings=0, lib count
  1881 (was 1879).

NOT VERIFIED

- **No boot.** Nothing here is measured on a guest yet: not the saving, not the
  frame rate, and not that the desktop still composites. That is the next step
  and it is an A/B against `=off` on one binary.
- Whether `batch_flushes` actually falls and whether the draw batch then
  accumulates. The prediction is that it does and that the win is larger than the
  92 ms/s the stamp span itself carries, but a prediction is not a reading.
- The arm64/Metal pathway. The coalescing is backend-independent but `write_stamp`'s
  GPU rail is `backend-vulkan`-gated, so the saving is expected to differ there
  and no Apple host has booted it.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-11 16:38:49 +03:00
2026-07-26 15:24:53 +03:00
2026-07-26 15:24:53 +03:00
2026-07-26 15:24:53 +03:00
2026-07-26 15:24:53 +03:00
2026-07-26 15:24:53 +03:00
2026-07-26 15:24:53 +03:00

reims-vgpu

License: LGPL-3.0-or-later

Alpha. This project is early and under active development. The QEMU device ABI, boot scripts, crate layout, backend behavior, and supported host/guest pathways may change without a stable compatibility guarantee. Treat it as research-quality: useful for experimentation and bring-up, not a frozen virtualization product.

reims-vgpu is an experimental virtual GPU for macOS guests. It aims to let macOS running inside a VM use accelerated graphics instead of a basic framebuffer, while keeping the guest operating system unchanged.

macOS already includes a paravirtual GPU driver named AppleParavirtGPU.kext. reims-vgpu provides the QEMU device that driver attaches to, then decodes the guest's GPU command stream on the host and executes it through Metal (TODO) or Vulkan, with Vulkan translation handled by metal2vulkan. There is no custom macOS kext and no guest driver to install.

Contributions are welcome. I am especially interested in collaborating with developers who want to work on correctness, visual glitches, synchronization bugs, command-stream decoding, Metal/Vulkan translation, and making more host/guest combinations reliable.

reims-vgpu running an arm64 macOS 13 Ventura guest desktop on an Apple Silicon host

arm64 macOS 13 Ventura guest on an Apple Silicon host.

reims-vgpu running an x86_64 macOS 13 Ventura guest desktop on a Linux host

x86_64 macOS 13 Ventura guest on a Linux host.

Three pathways

crates/reims-vgpu targets the following host/guest/backend combinations. Agents pick the pathway their unit of work is on.

Pathway Host Guest Device attach Backend Boot
x86 macOS / Linux Vulkan Linux x86_64 (KVM) x86_64 macOS Metal guest PCI reims-vgpu-pci host Vulkan via metal2vulkan vm/boot-x86.sh
arm64 macOS / macOS Metal Apple Silicon macOS (HVF) arm64 macOS Metal guest (vmapple) sysbus MMIO reims-vgpu-mmio host Metal vm/boot-arm64.sh
arm64 macOS / macOS Vulkan Apple Silicon macOS (HVF) arm64 macOS Metal guest (vmapple) sysbus MMIO reims-vgpu-mmio host Vulkan via metal2vulkan through MoltenVK vm/boot-arm64.sh
  • QEMU device shims: vendor/qemu tracks steelbrain/qemu-reims-vgpu@host-reims-vgpu-vmapple (thin C — QOM/MMIO/IRQ/console/HostOps only)
  • Product logic: crates/reims-vgpu (decode + device model + Metal/Vulkan backends)
  • Wire layouts: crates/reims-vgpu-wire (derived serializer views/parsers; decode uses these as the layout authority for covered records)
  • Vulkan translator dependency: public steelbrain/metal2vulkan Git crate. On macOS, the Vulkan host backend runs through MoltenVK.
  • VM lifecycle: vm/ (snapshot-revert; arm and x86 guest boot scripts)

Getting started

This tree ships boot scripts and the device, not a ready-made macOS disk image. Guest disks, firmware vars, and OpenCore blobs are private/gitignored under vm/. Pick a pathway, provision a guest once, freeze a golden snapshot, then use the snapshot-revert boots for day-to-day work. macOS 13 Ventura is the recommended guest release for bring-up.

x86_64 guest on Linux (KVM)

  1. Host prep. You need KVM (/dev/kvm), a working NVIDIA (or other) Vulkan stack for the product backend, and build deps for the in-tree QEMU (scripts/qemu-build/qemu-build.sh --target x86_64 --backend vulkan). KVM must ignore unhandled MSRs or macOS will not boot — e.g. a modprobe conf with options kvm ignore_msrs=1 (reboot or reload the module after).

  2. Generate OpenCore, OVMF, and a guest disk with OSX-KVM. macOS 13 is recommended.Follow that project’s docs to fetch recovery media, build OpenCore, and install macOS under QEMU+KVM. The point of this step is only to produce a working, post-Setup-Assistant guest plus the usual OpenCore/OVMF pieces — not to stay on OSX-KVM’s long-term launcher.

  3. Drop the artifacts where this repo expects them (paths are the defaults in vm/boot-x86.sh; override with env if you prefer):

    Artifact Default location
    Guest system disk vm/disks/macos.img
    OpenCore boot disk vm/disks/OpenCore.qcow2
    OVMF code vm/ovmf/OVMF_CODE_4M.fd
    OVMF vars template vm/ovmf/OVMF_VARS-1920x1080.fd

    Finish install in the guest: enable Remote Login, install your SSH key, turn off sleep/screensaver as you like. Host SSH is typically localhost:2222 → guest :22 (see vm/boot-x86.sh).

  4. Capture the first immutable snapshot. Guests are organised into rails — one rail per guest OS line (macos-11 … macos-26), each with a snapshot history of its own under vm/disks/rails/<rail>/snapshots/. Create the rail's directory, then from a clean guest state (logged in, network/SSH known-good) shut down cleanly while booting in capture mode:

    mkdir -p vm/disks/rails/macos-15
    vm/boot-x86.sh --rail macos-15 --capture --device vmware-svga
    # clean shutdown from inside the guest → new label under
    # vm/disks/rails/macos-15/snapshots/, and that rail's snapshots/current points at it
    

    Every later boot clones the selected rail's snapshots/current (COW when possible) and throws the clone away on exit, so wedges and hard kills never poison the golden image.

    Importing a guest built elsewhere is the same shape without the boot — drop {macos.img,OpenCore.qcow2,OVMF_VARS.fd} (plus OVMF_CODE.fd if that guest was installed under a different OVMF build) into vm/disks/rails/<rail>/snapshots/base/, chmod 444 them, and ln -sfn base vm/disks/rails/<rail>/snapshots/current. Use cp --reflink=auto on btrfs and the import costs no disk.

  5. Day-to-day boots.

    vm/boot-x86.sh --list-rails                  # what guest lines exist (* = default)
    vm/boot-x86.sh --rail macos-15 --list-snapshots
    
    # Console only (mainstream OSX-KVM-style VGA) while you debug the host stack
    vm/boot-x86.sh --testing --device vmware-svga
    
    # Product Reims VGPU device (needs in-tree QEMU + reims-vgpu Vulkan)
    REIMS_VGPU_BACKEND=vulkan scripts/qemu-build/qemu-build.sh --target x86_64
    vm/boot-x86.sh --testing --device reims-vgpu-pci --rail macos-15
    
    # Host-window screenshot on the Linux/Plasma host
    scripts/screenshot-when-kde-plasma-host/screenshot-when-kde-plasma-host.sh -o /tmp/screen.png
    

    Without --rail a boot follows vm/disks/rails/current; change it with ln -sfn <rail> vm/disks/rails/current. Neither --rail nor --snapshot repoints anything.

arm64 guest on Apple Silicon (HVF / vmapple)

Arm bring-up is in-tree: Virtualization.framework via Homebrew macosvm, then QEMU’s vmapple machine under HVF. There is no OSX-KVM step.

  1. Install macosvm, and build the vendored QEMU:

    scripts/qemu-build/qemu-build.sh --target aarch64 --backend metal
    
  2. Provision a guest from a UniversalMac IPSW with the project helpers in scripts/vmapple-provision/. The live bundle lives under vm/guest/ (disk, aux, vm.json / ECID).

  3. Configure the guest once: enable Remote Login, run scripts/vmapple-guest-config/ for no-sleep settings, and optionally enable auto-login by hand in System Settings. Capture a golden under vm/guest/rails/<rail>/snapshots/ with the snapshot helpers (scripts/vmapple-snapshot/, or vm/boot-arm64.sh --rail <rail> --capture once the disk is ready).

  4. Boot:

    vm/boot-arm64.sh --testing --device reims-vgpu-mmio    # product
    vm/boot-arm64.sh --testing --device apple-gfx-mmio   # Apple ParavirtualizedGraphics A/B
    scripts/screenshot-when-macos-host/screenshot-when-macos-host.sh /tmp/screen.png
    

    Optional performance ceiling reference: the same guest under native VZ via macosvm --gui.

After the first snapshot

  • Prefer --testing for agent/measurement boots (time-bounded, always reverts).
  • Use --interactive when you need an open-ended GUI session (still reverts unless you are in --capture mode).
  • Say which rail a result came from. A number from macos-11 and a number from macos-26 are two measurements, not one — that separation is the whole reason snapshots are per-rail.
  • Never commit disks, IPSWs, or OpenCore/OVMF runtime under vm/.
  • Device/backend work lives in crates/reims-vgpu + the thin shims in vendor/qemu; rebuild QEMU after product changes before claiming a live boot result.

Environment overrides

Set on the boot command; every one is optional and every default is "let the device decide". The full list, with the parse, is crates/reims-vgpu/src/env.rs. Each accepts 1/on/true/yes and 0/off/false/no, case-insensitively.

Variable Effect
REIMS_VGPU_DMABUF=off Stop reaching guest pages through a dma-buf, even where the host can. Every guest-memory rail takes the copying path instead — which is what runs on any host without VK_EXT_external_memory_dma_buf, so this is how that half is exercised on a machine that has it.
REIMS_VGPU_DRAW_LOG=on Verbose per-draw detail on top of the always-on failure log.

An override can only narrow what the device does. There is no way to switch a rail on that the host reported it cannot run: capability is measured from the device at startup, and asking a driver for an extension it does not advertise fails device creation rather than degrading. REIMS_VGPU_DMABUF has no on direction for that reason — on a host without the extension it is already off, and the vk_caps line in /tmp/reims-vgpu-fail.log names which check said so.

Repo layout

AGENTS.md           - repo operating guide for agents
crates/             - Rust crates (`reims-vgpu`, `reims-vgpu-wire`, `reims-vgpu-efi`)
scripts/            - host setup, VM lifecycle, screenshot, and diagnostic helpers
vendor/             - vendored QEMU submodule and patch record
vm/                 - VM launch/configuration glue; images are private/untracked

crates/reims-vgpu-wire holds zero-copy views and parsers for the Apple paravirtualized GPU serializer format, derived from Apple's own encoder rather than inferred from captures. crates/reims-vgpu's runtime::decode uses those exports for opcodes, record framing, and field layouts on wire-covered families (encoder blit/compute/render binds and state, and the create records above); decode remains the mapping layer into the device's Command / Kind model and decline naming. Gaps without a wire export (FIFO, event opcodes, unobserved compute residency, pipeline TLV) stay local to decode.

License

Licensed under the GNU Lesser General Public License v3.0 or later (LGPL-3.0-or-later).

Metal, macOS are trademarks of Apple Inc. reims-vgpu is an independent project and is not affiliated with, sponsored by, or endorsed by Apple Inc.

Languages
Rust 61%
C++ 17%
Objective-C 7.9%
Objective-C++ 7.4%
C 2.6%
Other 4%