Commit Graph
100 Commits
Author SHA1 Message Date
Anees Iqbal ce6fa476d4 🆕 Write the degradation counts when a running VM's report asks
The library wrote its counts line only at a device's reset and at the
process's exit, so a reims-ctl report made while the VM ran had none of
it in its qemu.log.

A host now asks for them with rvg_write_counts (the Rust API's
GraphicsDevice::write_counts), which writes the line before it returns,
where it changed since it was last written. QEMU's reims-graphics-pci
calls it when its new x-write-counts property is set to true over QMP
(vendor/qemu b057a987), and reims-ctl report sets it on a running VM
before it zips the logs, with a line in state.txt saying whether the
library wrote them. A trigger rather than a timer: a timer would touch
only the library, but write a line a minute into every log and still be
up to a minute old at the report; QEMU forwards no signal the library
could take, and Windows has none.

For the host: a new C API function and QOM property; QEMU's log gets the
counts line at each ask. api.md, conventions.md, qemu.md and the report's
help say so.
2026-11-26 12:48:35 -08:00
Anees Iqbal c897a0f78f ⬆️ Build on metal2vulkan development efd7dbcd4d
Moves the runtime to metal2vulkan development efd7dbcd4d, which holds
development db3a88526b and the Shared memory coherence form the owner chose
(A-narrow, Telegram 2963). SEMANTICS moves from 2302 to 2304 and ARTIFACT from
596 to 597 (RUNTIME stays 2550), so every host translates its guests' shaders
again once, and rt's stored artifacts and pipeline caches re-key.

What changes for the guest at this pin:
- rt's floor takes a device whose host-cached memory types are none of them
  host-coherent. Shared storage lives in the first host-visible, host-cached
  type a buffer can use, a host-coherent one first (2c4680acea, 31ca7f6779).
  Such a device is one where Mesa's Intel driver over i915 runs a GPU with no
  cache it shares with the CPU, as on a user's Intel Arc (Meteor Lake) laptop,
  which rt refused with rt-refused-below-floor (Missing::MemoryType). It is
  now made. On it, rt flushes the Shared memory a submission's command buffers
  reach before it submits them. Before the host can see a command buffer's
  progress (its status, a wait, its completed handlers, a shared event's
  value), rt flushes then invalidates what the command buffer may write. It
  also flushes around replaceRegion, getBytes and the named bytes of an
  upload. A range a command buffer reaches only through an address or a
  residency set, with no binding or declaration naming it, is neither
  flushed nor invalidated there (rt's known gap). The commit after this one
  forwards a render encoder's useHeaps, so a used heap's memory is among what
  rt flushes. On a device with a host-coherent host-cached type, every GPU our
  tests and boots run on, rt takes the same type as before, keeps no ranges
  and makes neither call. metal2vulkan measured what that costs there
  (efd7dbcd4d's message): +43.1 user-space instructions per command buffer
  on a Radeon RX 7900 XTX's burst_2000 (job 74045), and 1.0062 on burst_2000
  and 1.0115 on frame_5cb on a Radeon 780M (job 74046).
- A metallib of format 2.2, whose function entries carry no MDSZ and which has
  no extended header (an older toolchain's precompiled library, such as the
  one a game launcher's SDL2 renderer makes), is read (4bcb58dbb1, SEMANTICS
  2303). rt refused it as Container(MissingTag), and every pipeline made from
  it was dropped. Its functions, AIR 1.8 with the pre-LLVM-4 metadata and
  typed-pointer records (METADATA_STRING_OLD, METADATA_KIND in the METADATA
  block, METADATA_OLD_NODE, INBOUNDS_GEP_OLD, LOAD and CALL without their
  types), now decode as LLVM 23.1.2's reader reads them (31b3470a3e,
  a68c6633c2, SEMANTICS 2304), where each stopped at its first such record.
  Lane steamfix's boot of the launcher's disk on these fixes (job
  m3-max-73971, um870's RTX 3090) made all 61 of its libraries, the SDL2
  renderer's among them, and all 25 render pipelines made from its functions,
  with rt-refused-translation 0. A METADATA_KIND record with fewer than 2
  operands, or with a kind id stated twice, is now malformed, as it is for
  LLVM's reader.
- A compressed copy of whole blocks that ends past a level's edge, inside the
  level's last block (the one 4 x 4 block of a level of 4 x 2 texels), copies
  that block (9ddd0be2a4). That covers texture to texture, buffer to texture,
  texture to buffer, replaceRegion and getBytes, and matches what Metal does
  (measured on an M3 Max by metal2vulkan's probe
  compressed_copies_of_small_levels). rt refused those copies as
  Refusal::TextureRegion (rt-refused-ours), encoding nothing. A copy between
  two textures whose regions stop at different edges is now Gap::TextureCopy
  (rt-refused-gap), still encoding nothing. A size past the rounded edge stays
  refused as TextureRegion.
- The rest of the range is metal2vulkan's validation, probes, conformance
  cases, censuses and docs. Its deadbind deletions (the indirect-command
  plumbing only its deleted runtime drove, and the Y'CbCr planes chain nothing
  has made since its producer went) remove 51 records from api_signatures.tsv,
  and this tree names none of them.

For the host: rt's Metrics gain five fields, the Shared memory flushes and
invalidates, their bytes and the calls that failed, all 0 on a host-coherent
type. The counts line carries them from the commit after next. No other API
this tree uses changed, no rt kind of refusal, gap or note is new, and no
dependency changed: the lock, in its stripped form, is unchanged. Our docs
that named the Shared type's flags follow (gpu/identity.rs, pool.rs,
docs/api.md), and rt_platform/copied.rs says which copies rt flushes on a type
that is not host-coherent.

Checks the move cannot reach: QEMU, the option ROM, the guest's kexts and
bundle and reims-ctl build nothing of the runtime. The video crate and its arms
call nothing the move changes, so their checks do not run. The table's row owes
no boot: the move changes no death the platform ends the process on, and on
every GPU we run, rt's floor answers as before and takes the same Shared type,
with no device info, GPU family or feature query changed. Only a device with no
host-coherent host-cached type sees the floor change, and no lab GPU is one.
The Windows tests on the mini run after the merge.
2026-11-25 14:35:47 -08:00
Anees Iqbal df1eeb4e8c 🆕 Let a VM's windows never capture input
The owner asked for a VM whose window does not grab the mouse (owner 2893,
2895, 2900): capture stays the default, and a VM's settings can turn it
off.

With REIMS_VGPU_WINDOW_CAPTURE=0, which reims-ctl sets for such a VM, no
window captures, on macOS (AppKit's window and SDL3's) or on Linux and
Windows (SDL3's): a click into the image is sent to the guest but captures
nothing, so the window never takes the host's system keys, never grabs the
keyboard, never holds or locks the pointer and never holds its own size.
The pointer reaches the guest over the image alone, and outside it only in
a drag that began there, which ends in the guest. Keys go to the guest
while the window has the keyboard; on macOS that takes in ⌘ shortcuts, as
while captured, since nothing else could give the guest one, and the
window's menu stays in the pointer's reach. A game gets the absolute
pointer alone, which stops at the image's edge, since the guest's mouse is
driven only while captured. The title ends " — input capture off".

Unset, or any other value, capture is on as before. features.md gets the
register's "Capture off" entry and the switch, and the Capture entries say
where it applies; docs/api.md lists the variable.
2026-11-23 22:22:17 -08:00
Anees Iqbal d7fec652de 🐛 Let a decoder's state go off rt's completion thread at a loss
At a device's loss the table walk (fault_walk) upgrades each decoder's
Weak<Shared> on the thread that read the first failed command buffer, rt's
completion thread among them. Where the decoder went during the walk, the
walk's hold was the last one, and the decoder's objects were dropped there: a
drop that waited for a command buffer's completion would hang the thread that
completes it.

TableWalk::each now hands a last hold to a thread of its own,
reims-vgpu.graphics-tables-let-go, through the platform's threads, so a
refused spawn ends the process as theirs do. The test drops the decoder in
the walk's callback and checks that its state went on that thread, and that a
walk of a decoder that outlives it spawns nothing. The host sees only a new
library thread name; nothing the guest sees changes.
2026-11-21 19:14:23 -08:00
Anees Iqbal b955213d27 📝 Keep the guest's RAM mapped until the process exits [ci skip]
The device's imports of the guest's pages into the host GPU can outlive its
release: rt frees them on a thread of its own once no GPU work uses them, and
the release of the last device keeps the GPU device for the process when its
wait runs out. So the memory map's host memory stays mapped, and is not
unmapped, mapped anew or discarded, until the process exits (the header's
RvgMemoryRange and memory_map, docs/api.md, and reims-vgpu's MemoryMap).

QEMU's side does not change: the machine holds its RAM backend until QEMU
exits (hw/core/machine.c:1216), and the VMs reims-ctl makes discard no guest
RAM: no balloon, free page reporting, virtio-mem or memory hot-unplug, and
their RAM is a shared mapping, which KSM does not merge.
2026-11-20 16:33:34 -08:00
Anees Iqbal 42d93ec745 🎨 Rewrap the lines the runtime's rename ran past 100 columns
The rename of metal2vulkan-rt to metal2vulkan-runtime lengthened each mention
by five columns, and 43 lines of comments, docs and string literals ran past
the tree's 100 (the C API's two paragraphs were rewrapped in a16076ff37). Each
moves its last words to the next line of its paragraph, cascading where that
line runs over in turn; no word changes. A string literal moves words only
across its backslash-newline continuations, so every string reads as before:
each file's comment words, code tokens and decoded strings were compared
before and after, all equal. No log line, message or test changes.
2026-11-20 10:03:54 -08:00
Anees Iqbal 11055c78d5 ⬆️ Build on metal2vulkan development 82e7c39216
Moves both metal2vulkan revs from d83fb05624 to 82e7c39216, 65 commits on its
development branch, named green by its checks (m3-max-59311). The runtime's
crate takes its published name there (9a1c8fe6dc): metal2vulkan-rt 0.0.0 is
now metal2vulkan-runtime 0.2.0, its library metal2vulkan_runtime. The
dependency key, the crate's paths (each still `as rt`), the lock's entry and
the docs, comments, log lines and scripts that named the crate follow it. The
rename changes no API.

On rt:
- A command buffer waiting for a shared event's value whose signal is
  submitted is Scheduled as it is submitted (4730af16e7): its scheduled
  handlers run on the submit thread then, and its queue's later command
  buffers are no longer held behind it. A wait for a value that neither the
  host set nor a submitted signal promises stays held, Committed, as before
  (counted in Metrics::event_holds, fb4b77048c). GPU order and completion are
  unchanged. The docs that said rt's completion thread runs a held command
  buffer's scheduled handlers now say its submit thread does.
- Copies over buffers are tracked by the memory a buffer lies in, never its
  id (7e38a29b69): a texture over one buffer sees a draw through a texture
  over another buffer over the same bytes, in one command buffer and across
  a held group.
- A kernel that reads buffers through an argument buffer's pointer members,
  or through a gpuAddress the CPU wrote into a constant buffer, now makes
  (3cdca62567, c0403a1c98); the translator refused it (IdentityDomains).
- A refused kernel input names its kind (98895eb6d2, 1c26e1dc54):
  Gap::KernelInput { kind }, so the refusal's text, in the log and in the
  NSError's description, carries the kind. It is still counted as
  KernelInput.
- Memory goes back sooner. The release of a texture or block that an unsent
  upload holds sends the upload at once (0277c68696). A texture longer than
  a 16 MiB block has its block to itself and gives it back at its release
  (61949e8ba4). Every free of device memory on a normal path is made by the
  block-maker thread, and counted against the submits it meets (4c808ffa6d,
  09735ad792). New gauges tell texture blocks apart from the consumer's
  Private takes (b3f6c859dd).
- ComputePipelineState::dispatch_variant, the key of the VkPipeline a dispatch
  runs (82e7c39216), which this tree does not call yet.
- The translator's old path is deleted (0b94d919d9 to 16c831165d): its
  profile comes from Vulkan queries alone, with no policy switches and no
  driver-fact tables. SEMANTICS_VERSION goes from 2201 to 2203 and
  RUNTIME_VERSION from 2540 to 2541, so rt's shader and pipeline caches miss
  once: the first run on each host translates its pipelines again.

The rest is metal2vulkan's own: its conformance declarations and ledger, its
generated documents and the tests of each change. The NSError texts the guest
receives, and the comments of the wire header and the guest's sources that the
prebuilt kext sets and the option ROM record, keep the old name here; commits
of their own change them.
2026-11-20 09:41:45 -08:00
Anees Iqbal 8c40add9ac 📝 Say what the tree runs on now the old platform is gone [ci skip]
The cutdel branch deletes VulkanPlatform, metal2vulkan-runtime and the rt
feature, so RtPlatform over metal2vulkan-rt is the only platform. These
docs still described the old one: its answer policies, its exact
switches, MoltenVK on the Mac, its in-place guest memory, its records
cache, its device recovery and its Metal-answer tests.

- conventions.md: the guest-memory-in-place rule is replaced by the rt
  rule (rt allocates GPU memory, the device maps its blocks into the
  guest, textures sync at the guest's events, and there is one VkDevice
  per process). The degradation codes are now rt-unbuilt and rt-refused,
  and the deleted codes are dropped.
- accuracy-ledger.md: restated for rt, which has no switches. It keeps
  the device's own arithmetic notes (tagged as measured before rt), the
  19 unbuilt pieces and rt's refusal kinds, and device loss.
- api.md, README.md: REIMS_VGPU_EXACT, _CENSUS_DIR and _TRACE_DEGRADE
  are gone, and STRICT now covers running out of memory only. The let-go
  and the quiesce describe one platform.
- architecture.md, development.md, testing.md, qemu.md: KosmicKrisp in
  place of MoltenVK; RtPlatform, rt_window and RtBlocks in place of
  VulkanPlatform, the old window sink and HostBlocks; no Metal-answer
  comparisons and no opt-in GPU-fault tests.
- lessons.md: the four lessons about deleted code only (the Metal
  comparison after MoltenVK, the runtime's shadows, device recovery and
  the in-place check) are dropped. The lessons that still bind mark
  their deleted names.
- video/vaapi.md, graphics-kext/design.md: the GPU plane copy and the
  compositor mirror are marked as gone.
- AGENTS.md: only the Vulkan platform's "Where things are" row changes.
2026-11-20 07:08:46 -08:00
Anees Iqbal 3a550ab291 🐎 Let go of released device caches off the surface worker
A released block's device cache was dropped on the surface worker under its
block's key, and so were the views and the SURFACE_PAGES and BUFFER_OVER_PAGES
jobs, on the worker's four threads. A video session's caches go together at its
end: on Windows each is a Direct3D 11 target pair whose drop flushes both
immediate contexts under the plane draws' lock, 6 ms at the median and up to
100 ms under that contention. On the UHD 630 the held pass's 240 pairs kept all
four threads busy, and the next session's first SURFACE_PAGES, taken 15-19 ms
after its open and done in 0.02 ms, waited 512-586 ms for a thread to post its
answer; the guest's first copy took 516-600 ms.

The caches now go on a worker of their own, reims-vgpu.graphics-let-go-<i>,
each block's in its order: at a BLOCK_RELEASE, and once a block is named again
without a cache, whose drop ran inline on its ring-0 lane before. Their drops
answer nothing and no job waits for them, so a pages job no longer waits behind
them. The Linux arms' caches, VA-API's dma-buf imports and NVDEC's, go the same
way through the same let-go. A view's drop stays on the surface worker under
its block's key, as its mapping may go back to the idle pool the block's next
SURFACE_PAGES takes.
2026-11-20 06:39:52 -08:00
Anees Iqbal 7c74421e0e 🆕 Make the rt platform the library's default
The owner ordered the cutover: the library's platform is RtPlatform, over
metal2vulkan-rt, from this commit on. The rt feature of reims-vgpu-vulkan,
reims-vgpu and reims-vgpu-c is now on by default, so `cargo xtask dist`,
`make`, the installers' builds and QEMU's link all make every handle on
RtPlatform. Every test set that builds those crates with their default
features now runs rt's tests where it ran the old platform's.

For the guest everything the device answers and draws now comes from rt:
pipelines through rt's translator, memory from rt's pool, surfaces and imports
through rt's paths, on the host's Vulkan driver on Linux and Windows and on
Mesa's KosmicKrisp on a Mac. The old platform, VulkanPlatform over
metal2vulkan-runtime, still builds without the feature until its code is
deleted, which comes next. Anything wrong on rt is fixed forward.
2026-11-20 05:16:29 -08:00
Anees Iqbal 2e40e0b52c 🆕 Record refused pipelines' constants and descriptor fields
A refused pipeline's AIR alone does not reproduce its refusal when the guest's
call adds what refuses it: a kernel that compiles with default function
constants can be refused for the values or descriptor fields the guest passed.
Under the same off-by-default switch, REIMS_VGPU_REFUSED_AIR_DIR, each refused
compute or render state now also writes a record into records.txt next to
refused.txt, once per distinct record. The record holds the creator and the
refusal; for each stage, its function, whether it was made specialized, each
function-constant value with its index, declared name, type and bytes, and each
declared constant left undefined; the same for each linked function; and each
field of the descriptor rt was given. The records are the guest's data: they
are written only to that directory on the host, never to stderr or the log.
The rt-diag refused-air line now also counts the records.
2026-11-19 20:59:00 -08:00
Anees Iqbal bb34b2e0dd 🆕 Count rt's fetched-attachment draws, refusals and border samplers
The arm64 macOS 26 boot on rt refused 5685 draws, the first of them as
reading a colour attachment through its texture (FetchedAttachment), and
nothing said which attachments those were: rt refuses one that is no 2D
texture, renders another level or slice, has no index, shares its image
with another target, or that the device reads no attachment through. The
boot diagnostics now count each such draw by each colour target's class
(how its texture was made, type, samples, level and slice, index, whether
its image's usage reads and writes, its usages, a shared image), with a
first line per pipeline state, as they do the attachment reads.

They also count rt's refusals of Metal's and its limits by variant, where
the counts line has one rt-refused-metal and one rt-refused-limit, and
the sampler states made, with those that clamp to their border colour by
the colour.

And a diagnostic switch, off by default: REIMS_VGPU_REFUSED_AIR_DIR keeps
the metallib of each function of a pipeline state the translator refused,
once per library, with a line per refusal, in air-<pid> in the directory
it names, so the refusal can be read on the host that met it. It is the
guest's code: it stays on that host, and nothing commits or copies it.
2026-11-19 19:53:22 -08:00
Anees Iqbal 359ac7817e 🐎 Complete a decode alone into caches at its decode
A command buffer that holds only a video decode whose output lies in device
caches, where the VA-API arm copies the picture into the caches by its video
processing on the GPU, now posts its CB_COMPLETE as the session says the
decode is done and the copy is issued, instead of at its platform end after
the copy. The guest's decoder hands the picture on while the copy runs: the
start of a 4K stream decodes one picture at a time into new surfaces, and
each picture's chain waited for its copy.

What changed for the guest:
- such a CB_COMPLETE comes before the copy into the output's caches ends,
  in its queue's order of records (a queue's turns of completion posts), and
  carries GPU times of 0;
- the guest's writer slots, which it empties at the CB_COMPLETE, then name
  the decode no more, so the host keeps each such decode by its blocks until
  its platform end: a later command buffer of another queue that uses the
  output waits for that end, on the GPU, or on the CPU for a video op; a
  present waits for it as for a writer named; a lock's download waits on the
  end value as before. Counted as buffers and presents held;
- the buffer's later work, its surface timeline, its caches' copy into the
  pages, its context's drain and its place among its queue's buffers in
  flight all stay at its platform end, so a reset, a close and a quiesce see
  the decode in flight as before.

What changed for the host: raise_interrupt may now be called on a video
session's picture landing thread, reims-vgpu.graphics-video-land, inside the
landing's video processing run (reims_vgpu.h, docs/api.md section 5). QEMU's
reims-graphics-pci raises its interrupt from any thread, and is unchanged.
2026-11-19 16:59:44 -08:00
Anees Iqbal 59e376bd05 🐛 Answer false to the quiesce once the platform made a GPU device
On the Vulkan platform (metal2vulkan's old runtime) rvg_quiesce_for_exit
answered true once the release's let-go took the GPU devices, so QEMU called
exit(). The let-go does not end the runtime's work: a pipeline making of the
warm pass can stay in the driver past the device's last drop, and the
runtime's exit hook, registered as the device was made, then waits for it and
on Linux ends the process at once with _exit. QEMU's exit notifiers never ran:
on an RTX 2080 (620.70.06) a quit 24 s into a boot, its let-go 315 ms after
the quit, ended 9.6 s after it with QEMU's pidfile left (job m3-max-53092).

Now the quiesce answers false once the platform asked the runtime for a device
in the process, kept or let go of since: each of the platform's makes (the
default device, a window's device, MTLCopyAllDevices's, the device choice's
timed devices) and each adoption mark it (let_go::asked_the_runtime_for_devices).
What changes for the host: on the Vulkan platform, a quit after the release
that let go of the device now gets false, and QEMU runs its own exit work (its
exit notifiers, the terminal's restore, the streams' flush) and ends with
_exit and the cleanup's status, as it did with a device kept. The runtime's
exit hook and the driver's exit handlers no longer run then, and the library
writes its degradation counts in the quiesce. A process that never opened a
GPU device still answers true. The platform over metal2vulkan-rt is unchanged.

The let-go's child-process test now expects false after the let-go, then
_exit with status 0; a new one holds true and exit() for a process that asked
for no device. The C harness ends as QEMU does on the answer, and its test
expects false on the Vulkan platform and true over metal2vulkan-rt. The API
docs, the header, docs/api.md and docs/lessons.md say the same.
2026-11-19 11:15:34 -08:00
Anees Iqbal bbeb3d39b3 🐎 Land staged downloads on threads of the caches' own
On the rt platform every cache's copies are staged, and a download's command
buffer ended in a completed handler that copied the staging buffer into the
pages on the host's CPU. The platform sees every queue's completions on one
thread, so a large surface's copy held all of them: 144 MiB at 3 GB/s is
48 ms, and every completion of every queue behind it waited that long.

The handler now hands the copy into the pages, the copy's landing and the
answers that wait for it to a worker of the caches' own
(reims-vgpu.graphics-into-pages-<i>), keyed by the block, so a block's copies
land in the order their command buffers completed, and returns. Only what
waits for the pages waits for that thread: the download's or release's
answer and the threads that wait for the copy to land.

For the host: raise_interrupt is now also called on those threads, which the
header, docs/api.md and the raise list now name. QEMU's callback already
takes any of the library's threads, so its side needs no change. The tests
that check a staged download's answer now wait for the worker, and a new
test shows another queue's completion going through while the copy waits.
2026-11-19 10:12:58 -08:00
Anees Iqbal 96dbe24887 🐛 Answer false to the quiesce while a Vulkan GPU device lives
The Vulkan platform's quiesce ahead of the exit (rvg_quiesce_for_exit) did
nothing and answered true, so a quit with a GPU device kept to the exit went
on through exit(). On Linux metal2vulkan's exit hook, which runs before QEMU's
exit notifiers, waits 2 s for the runtime's work in progress and then ends the
process with _exit itself: with a device kept to a paused VM's quit, 5 of 9
quits on an RTX 3090 and 5 of 9 on an RTX 2080 left QEMU's pidfile behind,
and usb-host's return of host devices to their drivers is skipped the same
way.

Now it closes the exit gate ahead of the exit, waits for the library's steps
in progress within its bound, and answers false while the platform keeps a GPU
device (let_go::gpus_kept), as the platform over metal2vulkan-rt does. What
changes for the host, where QEMU's quit finds a device kept (a paused VM, a
shutdown action that pauses, a display read that kept it): QEMU's exit work
runs (its exit notifiers, the terminal's restore, the streams' flush), then
_exit with the cleanup's status. metal2vulkan's exit hook and the driver's
exit handlers no longer run then, and the degradation counts are written in
the quiesce rather than by the exit gate's handler. A quit whose release let
go of the device answers true and exits as before.

New tests hold both answers in a child process: false with a device kept,
true once the let-go took it, then exit() with status 0. The C tests that
quiesce with nothing on a GPU still answer true. The API docs, the header
and docs/api.md say the same for both platforms; docs/lessons.md has the trap.
2026-11-19 09:52:07 -08:00
Anees Iqbal c997d16fe0 🐛 End an exit that skips QEMU's cleanup with _exit and no quiesce
metal2vulkan-rt supports no quiesce inside an exit handler. QEMU's exit
handler for an exit() that skips its cleanup (a later device's refused
realize, a failed incoming migration) called rvg_quiesce_for_exit there and
let the exit go on where it answered true, though no release had let go of
the GPU device on that path. vendor/qemu ba447463 makes the handler call no
quiesce: it runs QEMU's own exit work and ends with _exit(EXIT_FAILURE), and
after the cleanup's true answer it does nothing.

For the host: rvg_quiesce_for_exit's doc says it is not for an exit handler,
and on that path QEMU now ends with _exit, the library writing no
degradation counts. QEMU's new trace event reims_graphics_end names each end
through _exit.
2026-11-19 08:04:09 -08:00
Anees Iqbal df1172fa25 🐛 Answer false to the exit's quiesce while a GPU device lives
metal2vulkan-rt's exit contract (its README, "Quiesce and exit", at
d48664f6d2) supports exit() with a device live on no device: a process that
still holds one quiesces it and ends with _exit, whatever the quiesce
answered. On an RTX 3090 (620.70.06), 3 of 9 power-offs that called exit()
after the quiesce stopped every rt thread in time ended in SIGSEGV in the
driver's exit handler.

What changes for the host: rvg_quiesce_for_exit now answers whether the host
may call exit(). On the rt platform it still parks the library's threads and
quiesces a device that lives, but answers false wherever one lives: a device
kept to the exit (a quit of a stopped or shutdown-paused VM, a display read
that kept it) or one a let-go could not tear down. It answers true where the
release of the last graphics device let go of it. On false the library writes
its degradation counts then, since the host's _exit runs no exit handler.
The warning on false now comes from the platform and only where a wait
outlasted its bound. The Vulkan platform still answers true.

QEMU (vendor/qemu e8f0178b): on false it runs its exit notifiers, the
terminal's restore and the flush of stdio, then ends with _exit.

Tests: the rt child test with the device live now expects false, with rt's
own quiesce answering true, and ends with _exit.
2026-11-19 08:04:09 -08:00
Anees Iqbal c000ca0981 🐛 Let go of rt's device at the last release
The rt platform kept the process's one rt device in a OnceLock until the
exit. On an RTX 3090 (NVIDIA 620.70.06), 3 of 9 power-offs of an x86-64 macOS
guest then ended in SIGSEGV inside exit(): the driver's exit handler waited
on a lock while a thread of the driver's own read near a null pointer, with
the VkDevice live and every rt thread already stopped by the quiesce.
metal2vulkan-rt's contract asks the consumer to drop the device's last
handle before the process exits.

What changes for the host: the release of the last graphics device
(rvg_graphics_device_release) now lets go of rt's device, as it does of the
Vulkan platform's. Once nothing beyond the platform holds it (at most 5 s),
rt's quiesce runs with what is left of the bound, then the drop of the last
handle tears the device down on the releasing thread, and the let-go waits
for rt's gauges of it to read zero. A device whose quiesce answered false is
kept to the exit. A let-go that waits out its bound logs a warning and keeps
the device, as on the Vulkan platform. A graphics device created after it
runs on a new rt device.

Tests: the let-go test runs on the default platform, so on rt too, and the
test that pinned rt keeping its device (9df71bca5) is removed. A child
process test checks the order of a quit: the let-go, then the quiesce, then
exit().
2026-11-19 08:04:09 -08:00
Anees Iqbal f7892bf0e8 🆕 Quiesce the library ahead of the process's exit
Where QEMU keeps the reims-graphics device to its exit (a shutdown action
that pauses, a quit of a stopped VM, a display read past its 5 s), and on
the rt platform even after the release of the last graphics device, rt's
submit, completion and block-maker threads can still be in the GPU's driver
as the driver's exit handler tears it down: on NVIDIA's driver, SIGSEGV
inside that handler.

rvg_quiesce_for_exit(timeout_ms) is the host's last call before its exit.
It goes to a new method of core's platform, quiesce: on the rt platform it
closes the exit gate ahead of the exit, so the library's threads park at
their next step, waits for the steps in progress, then runs rt's
Device::quiesce, all within the bound; it frees nothing, answers false where
a thread may still be in the driver, and a second call gives the first
answer at once. The exit gate's hook still runs at the exit and writes the
counts. On the Vulkan platform and the testkit it does nothing and answers
true, so their exits stay as they were.

QEMU calls it at the end of its cleanup, before exit() runs any handler, and
from an exit handler registered as the device is made for exits that skip
the cleanup; it ends with _exit on a false answer (vendor/qemu 88337859).
The C harness and a unit test call it with nothing to stop; an rt GPU test
checks the device's handlers ran, a second call, the closed gate and the
refusal after it; an exit-gate test checks the wait, its deadline and the
counts written at the exit.
2026-11-19 08:04:09 -08:00
Anees Iqbal 2fe5515d85 🆕 Record each acquire of another API's writes into a buffer
metal2vulkan's replay player plays the host video recordings, whose surface
caches the hardware writes outside the runtime: VA-API's decode or video
processing into a dma-buf cache, NVDEC's copy through CUDA's import of an
exportable one. It must refill what lies over such a cache where those writes
become visible to the device: the platform's external_write_done, the
runtime's acquire. The platform made that call unrecorded, so no stream held
the point, and a player had to infer it.

external_write_done now records m2v.externalWriteDone on the buffer, with no
arguments, as the call returns, as rebindNoCopy and every other hooked call
are recorded. The host's video makes the call on the session's landing thread
once the picture is in the cache, right before the session's
setSignaledValue: that releases the decode's command buffer past its wait, so
the record lies after the hardware's writes and before every command that
reads them. A refused acquire is recorded as any refusal is. The record is
counted by its selector in the stats file, and the recorder_fix
"external-write-done" names the change.

Nothing changes for the guest or the C API's host. With REIMS_VGPU_RECORD
unset the call costs one relaxed load more.
2026-11-19 03:25:44 -08:00
Anees Iqbal c6ab041d68 🆕 Record a no-copy buffer's escape in the replay stream
metal2vulkan's replay player must stop, typed, where the GPU may reach a
no-copy buffer through an address in memory alone. The guest says so with
BUFFER_ESCAPED, but the note ended in the decoder: no stream carried it, and
only the stats file's counted totals showed it.

The decoder's first note for each buffer now also records it, where the
process records (REIMS_VGPU_RECORD): an m2v.bufferEscaped call on the
buffer's stream id, [kind], the note's RGFX_BUFFER_ESCAPED_* act (0
gpuAddress, 1 argument encoder, 2 resource id, 3 residency set), of the whole
buffer. It is queued on the decoder's thread at the note, so it lies after
the m2v.write records of each commit made before the note and before those of
each commit made after it. A later note, a note for a refused create and a
bad reference write none. Each record is counted by its selector in the stats
file, and the recorder_fix "buffer-escaped" names the change.

Nothing changes for the guest or the C API's host. With REIMS_VGPU_RECORD
unset the note costs one more relaxed load (recorder::on).
2026-11-18 22:08:45 -08:00
Anees Iqbal 5273e0eeb9 🆕 Give arm64 guests' device the Shared pool's room past RAM
QEMU gave the library a pool range only on a PC machine, and an arm64
guest's device on vmapple had none. vmapple now answers the room past its
RAM, from the end of RAM rounded up to 1 GiB to the end of the guest's
physical addresses (the CPU's PARange, at most 40 bits), as read-only
machine properties, and reims-graphics-pci gives the library that room
where no PC host bridge answers one, reserving nothing where the machine
has no fw_cfg. Ambrosia lists RAM alone, so the guest places nothing
there; it is told of no piece yet. x86-64 guests' pool is unchanged.
2026-11-18 19:15:08 -08:00
Anees Iqbal a39e43e99f 🐛 Cap the pool's room at the end of the host's physical addresses
QEMU offered the library the pool's room up to three quarters of the
guest's physical address space only. A guest may have more physical
address bits than its host (QEMU allows it with a warning; the Windows
mini's Coffee Lake has 39 against the guest's default 40), and a piece
placed past the host's width makes KVM refuse its slot (x86 KVM's
memslot check, read from code); WHPX gets the same cap. Under KVM and
WHPX the room now ends at the host's width too, read from CPUID
0x80000008: the device file builds for every target, where
target/i386's host_cpu_phys_bits is not linked.

What changed for the host: where the host's addresses end before three
quarters of the guest's, so does the room. Measured with the device
realized paused and the 64-bit window pushed so the room starts 4 GiB
below the host's end, the range the library took ends there: at
0x1000000000000 under KVM on a 48-bit Ryzen with phys-bits=52
(m3-max-49283), and at 0x8000000000 under WHPX on the mini
(m3-max-49316); under TCG it runs past both. On the mini's default q35
machine the room is now 487424 MiB, its 39 bits' end less the window's;
the range, the UHD 630's 31 GiB of heaps, is as it was.
2026-11-18 18:42:35 -08:00
Anees Iqbal f4692c7179 🐛 Take a pool piece out of the guest before its block goes back
The pool gave an empty piece's block back to its source and left the
guest's mapping to the source's unmap hook (BlockMappings::unmap), which
the source called before it freed the block, on the pool's thread or its
own. The metal2vulkan runtime deletes that hook, so the pool keeps the
promise itself: it gives a block back only once no thread of the device
holds it and the host has said its piece is out of the guest's address
space, and its close gives every block back only after every hold has
ended. The source may then free any block it gets back at once.
HostBlocks loses its mappings field, and Pool::new takes the source
itself.

What changed for the host: pool_remove comes on the device's pool thread
as the pool gives a block back (an empty piece past RELEASE_DELAY), never
on the source's thread; and a block the source hands out again is placed
and gets a new pool_add, where it used to keep its piece and mapping.
QEMU's x-pool-grow boot (8 asks of 256 MiB) now shows 8 adds and 7
removals (m3-max-49284), where it showed 5 removals before
(m3-max-49031). The release waits for the device's own holds before it
frees the blocks. The guest sees nothing new.

The tests that held "a block taken again keeps its piece" now hold that
it is mapped anew. New tests hold that a piece goes back only once the
host removed it and its hold ended, that one given back before the
host's add answered still waits for its remove, that ready blocks past
two are freed with no word to the host, and that the release gives a
held piece's block back only once the hold ends and asks the host
nothing. All ten fail on the code before this commit. QEMU's comment now
says pool_remove comes on the pool thread.
2026-11-18 18:42:29 -08:00
Anees Iqbal 16ad40298e 🆕 Size the guest's Shared pool's range from all the GPU's heaps
The guest's Shared pool's range of guest physical addresses, which QEMU
reserves and gives the firmware as etc/reserved-memory-end, is now the sum of
the host GPU's memory heaps, every heap, rounded up to 2 MiB and at most the
room the host gives, where it was the heap of the GPU's host-visible,
host-coherent and host-cached memory type alone. A heap's size is how the
driver accounts for memory, not the most it allocates: on a 780M, RADV puts
that type on a 5.52 GiB heap beside an 11.05 GiB device-local one over the same
system memory, so the guest's pool stopped at about 5.5 GiB, while the runtime
takes Shared blocks until the driver refuses one. A guest on a 780M now gets
about 16.6 GiB of pool addresses, on an RTX 3090 about 46 to 47 GiB, on an
RX 7900 XTX about 38.6 GiB; on an Iris Xe, with one heap, 23.24 GiB as before.

GpuDevice::shared_heap_size becomes memory_heaps_total, which sums the heaps
of VkPhysicalDeviceMemoryProperties and needs no buffer to ask which types a
buffer can use. A GPU that answers no heap still gives no pool. The library's
pool line now also logs the room the host gave, so a boot's log shows whether
the room clamps the range. QEMU's code is unchanged: vendor/qemu moves to a
commit that rewords its comments.
2026-11-18 17:54:51 -08:00
Anees Iqbal e74f40cc65 🆕 Name the display library's hash in each recording's stats file
A recording makes the device's own display library with
newLibraryWithSource:options:error: and no arguments, so a stream does not hold
its bytes. metal2vulkan's players now make it from a copy of the committed
shaders.metallib kept beside each stream (NAME.display.metallib), and asked that
each stream's stats file name that library by its hash, so a play can say which
display library it ran. The stats file now writes a display_library line after
recorder_commit: the SHA-256 of the bytes the device loads, in the hex the blobs
directory names libraries by, and their length.
2026-11-18 17:54:04 -08:00
Anees Iqbal 706353df2c 🆕 Size the guest's Shared pool's range from the GPU's Shared heap
The pool's range was a fixed 64 GiB of guest physical addresses, whatever the
host GPU: more than a GPU whose Shared heap is 24 GiB can ever fill, and less
than one whose heap is larger. The range now follows the heap the pool's
blocks come from: metal2vulkan-rt's Shared memory type, the first type with
HOST_VISIBLE, HOST_COHERENT and HOST_CACHED that a buffer of the device's
usage can use, on the GPU device the library opens. The bound is the heap's
size, not VK_EXT_memory_budget's budget: the range is address space held for
the VM's life, while a budget is momentary, moved by other processes, and on
Intel's Windows driver answered past its heap's size; RAM is bounded at each
block's take.

The config's pool range is now the room the host can give. The create opens
the GPU device, takes the heap rounded up to 2 MiB of that room (warning once
where the room is smaller, and giving no pool where no type is host-cached),
and rvg_graphics_device_pool_range answers what it took. The range is read
after the create rather than asked of the library before it, which would open
the GPU device twice or keep one alive without a device. QEMU offers the room
from pci-hole64-end to three quarters of the guest's physical addresses, then
reserves the range the library took, rounded up to 1 GiB, in
etc/reserved-memory-end, still at realize before the firmware runs. The
x-pool-size property goes; 0 and 0 still mean no pool in the C API.

What the guest sees changes: the firmware's reserved-memory-end, and so where
OVMF may put its 64-bit PCI window, follows the host GPU's Shared heap instead
of a fixed 64 GiB.
2026-11-18 16:52:12 -08:00
Anees Iqbal 5d38c7e825 📝 Say the library's hosts are Linux, Windows and macOS [ci skip]
The owner's word (Telegram 1924, relayed by metal2vulkan): the hosts are
Linux, Windows and macOS, and on macOS the library runs on Vulkan, not Metal.
README and the crates' and docs' notes said VMs run on Linux and that the Mac
build was for development and tests. They now say x86-64 guests run on Linux
and Windows hosts and arm64 guests on Apple silicon Macs, and that the Vulkan
platform runs natively on Linux and Windows and over MoltenVK on macOS.
rust-toolchain.toml names Windows' target as shipping too, and a reims-ctl
test's note says the test, not the installer, is Linux-only. AGENTS.md waits
for the owner's own word.
2026-11-18 14:28:11 -08:00
Anees Iqbal cb7f52c9a6 🆕 Name the recorder's build in each recording's stats file
metal2vulkan's stream table reads which recorder made a recording off its
files. The stats file now starts with recorder_commit, the commit the library
was built from, and a recorder_fix line for each change to what a stream holds
since format version 2, in landing order (recorder::FIXES, which each such
change extends in its own commit). cargo xtask dist stamps the commit for its
Cargo steps in RVG_BUILD_COMMIT (git rev-parse HEAD, -dirty where a tracked
file differs); other builds write unknown. Cargo and kache key the Vulkan
crate on the variable, so a dist at another commit compiles it again. The
stream itself and the device's behaviour are unchanged.
2026-11-18 13:56:40 -08:00
Anees Iqbal 285f0bdd69 🆕 Carry a no-copy Managed buffer's create-time bytes on two copies
On a device that keeps two copies of a Managed buffer (hasUnifiedMemory NO),
a no-copy Managed buffer's GPU copy takes the pages' bytes at its create, but
from zeroFrom on, and only didModifyRange: changes it afterwards. The stream
carried a no-copy buffer's bytes only as the writes before its first use, so a
player could not rebuild what the GPU copy held when the guest's CPU writes
since the create were never flushed to it.

Such a buffer's create record now carries the bytes the create copied, read
while the call holds them, a blob where long, as a copied buffer's contents
are: newBufferWithDescriptor:'s contents (the bytes before zeroFrom, nil where
that is 0) and newBufferWithBytesNoCopy:length:options:deallocator:'s first
argument (all of them). Shared and Private no-copy buffers, and any on a device
that keeps one copy, are recorded as before, and the writes before first use
are unchanged.
2026-11-18 00:45:55 -08:00
Anees Iqbal 0ba37884af 🆕 Record each device's identity at each segment's start
A player makes a stream's resources in the forms the recording device made
them, and the device's answers decide some of those forms: a Managed buffer is
one copy where the GPU shares the CPU's memory and two where it does not. The
stream named neither, so a player on another GPU could only guess them.

The recorder now writes an m2v.deviceIdentity call record on each device,
answering its name (the one the guest's device info gives) and
hasUnifiedMemory, right after the record that made the device, and again as the
first records of each later segment, one per device then live, so a segment
read on its own names them too. The format stays version 2: a reader that does
not use the record skips it, and its keys are answers.
2026-11-18 00:45:55 -08:00
Anees Iqbal b27a603cb4 🆕 Record the commands a CPU encodes into indirect command buffers
With REIMS_VGPU_RECORD set, the commands the guest's CPU encodes into an
indirect command buffer never reached the stream: an ICB_WRITE's went through
IndirectRenderCommand::encode and IndirectComputeCommand::encode, which had no
hook, and an ICB_SLOTS slot's call was recorded with its commands as nil (its
iterator prints nothing). The buffer's records live in the runtime, which no
memory write carries, so a replay executed commands it never saw.

Each send of commands to a buffer is now a call record on it,
indirectRenderCommandAtIndex: or indirectComputeCommandAtIndex:, with the
arguments [index, commands] that metal2vulkan's format names (section 8): the
commands in the order the platform carried them out, each a map of one pair,
the command's Metal selector and the list of its arguments read from its fields
(objects as stream ids, sizes and regions as maps). A command sent on its own
is a list of one; a slot's call lists the commands it took, read as it took
them, out of line over one iterator type. No m2v. selector is added.

With the variable unset nothing changes: one relaxed load and a branch, and the
platform's sends keep their shape.
2026-11-17 09:57:46 -08:00
Anees Iqbal 00d59542bc 🆕 Record Private buffers around dispatches of linked kernels
metal2vulkan replays the benchmark's lockstep kernels to check its forms against
Metal, and 28 of them link functions and bind Private buffers, whose bytes no CPU
can read, so no recording held them and those replays could not be checked.

With REIMS_VGPU_RECORD set, each recorded dispatch of a compute pipeline made
with linked functions (outside a block of command predication) now splits its
compute encoder around a blit before and after it, which copies each bound
Private buffer range (from its offset to the buffer's end, 256 MiB a dispatch at
most) into a Shared buffer of the recorder's own. At the command buffer's
completion the recorder writes both byte runs into the blobs directory and an
m2v.privateBytes record per range: receiver the buffer, arguments [the
dispatch's position among the stream's call records, {location, length}, the
before blob, the after blob]. A fresh compute encoder takes the guest's bindings
and residency again, as predication's refresh gives them; a recording process
keeps those bindings in a command buffer that holds no predication. Only these
dispatches: one per dispatch binding a Private buffer would split and copy far
more than the stream costs. stats.txt counts private_ranges, private_bytes and
private_skipped.

With the variable unset nothing changes: the hooks read the recorded call,
which is None there.
2026-11-17 09:24:15 -08:00
Anees Iqbal 426724ff52 🆕 Map the guest's Shared pool's blocks into a range the host reserves
metal2vulkan's new runtime allocates the GPU memory the guest's Shared
buffers live in, and the device maps those blocks into the guest (plan D2,
route 1). This is the host side of that pool: the guest's side, and the
wire it reads the range and its pieces from, come with the D2 guest work.

The library: a pool per device (crates/reims-vgpu/src/pool.rs) over the
range the host reserved. Its thread takes Shared blocks from a block source
and asks the host to map each at the lowest free 2 MiB-aligned address
(pool_add); the host says when the guest's address space has it
(rvg_graphics_device_pool_added). The source's unmap hook takes a piece out
of the lookups, waits for every hold on it, asks the host to unmap it
(pool_remove) and returns once the host says it is gone
(rvg_graphics_device_pool_removed); only then may the source free the block.
The pool keeps one empty piece as headroom, asks for a block more when the
guest uses the last empty one, and gives back a piece that stayed empty for
5 s above the lowest empty one. The source's calls and hook have
metal2vulkan-rt's shape (take_block, release_block, BlockMappings::unmap,
BlockInfo); until the device runs on that runtime the source is the
process's own memory (HostBlocks), so only the source changes then.

The C API: RvgGraphicsDeviceConfig and RvgGraphicsCallbacks go to their fifth
version (pool_physical_address and _length; pool_add and pool_remove), with
rvg_graphics_device_pool_added, _pool_removed and _pool_grow (the host's ask
for more pool now, which QEMU's x-pool-grow property drives).

QEMU (vendor/qemu): the device reserves 64 GiB past the RAM and the host
bridge's default 64-bit window, raises etc/reserved-memory-end past it so
OVMF's 64-bit window goes above it, turns KVM's zap-every-slot quirk off, maps
each piece as a RAM region of its own (a pure add) on its main loop, and under
WHPX runs the hypervisor's map outside the BQL. Its teardown takes every piece
out before the release, which frees the blocks before the process exits.

For the guest: the firmware's 64-bit PCI window starts past the reserved
range, 32 GiB or more higher than before; nothing else changes until the
guest's side maps the pool.
2026-11-17 06:35:09 -08:00
Anees Iqbal 14b69ece14 🆕 Record the platform's calls as a replay stream (format v2)
metal2vulkan's replay lane plays a guest's Metal work back on its own
runtime without a guest. It needs the calls our decoder makes on the
Vulkan platform, in order, with the guest memory the GPU reads, written in
its replay format v2.

The recorder is off until the host sets REIMS_VGPU_RECORD to a directory;
off, each hooked platform method costs one atomic load and a branch. On,
each process writes <dir>/<pid>.NNNN.m2vr segments (1 GiB each) from a
writer thread every 50 ms, libraries' bytes into the blobs directory beside
<dir>, and its counts into <dir>/<pid>.stats.txt.

- The hooks are in the Vulkan platform's trait impls (112 methods), and
  a thread-local depth records only the outermost call, the one core
  made. Arguments are written by their type: handles as object ids,
  bytes and strings as themselves, the rest from their Debug text.
- Before a commit, the recorder waits for the command buffers that ran
  before it and could still write what it reads, then writes the 4 KiB
  chunks of the memory it reaches that changed since the stream last held
  them. Only the ranges the GPU wrote are taken as the stream's once
  their command buffer completed, so CPU writes made meanwhile are kept.
- Blocks over guest memory and buffers in them are written as
  m2v.newBlockWithLength:storageMode: and m2v.newBufferInBlock:..., and
  each present the device's display shows, with a window or without one,
  as m2v.present of the surface's texture.
- format::summary reads a stream back; an ignored test prints it for a
  recording, on the host that made it.
- It never asks a buffer for its GPU address, which keeps a dma-buf
  import alive for good; addresses come from the calls that set them.

What the guest and the host observe is unchanged: the recorder only reads.
2026-11-17 05:56:10 -08:00
Anees Iqbal 64183b449e 🆕 Add several displays to the C API
RvgGraphicsDeviceConfig's fourth version adds display_count and
display_windows (the window configs of displays 1 and up, given exactly
where display 0 has a window). RvgGraphicsCallbacks' fourth version adds
cursor_glyph_at, cursor_show_at and cursor_move_at for displays 1 and up.
rvg_graphics_device_read_display_at and
rvg_graphics_device_cursor_position_at read any display, and
RVG_GRAPHICS_DISPLAYS_MAX names the limit. These are additions only: a host
built against the older structs, or one that gives a display_count of 0 or
1, gets one display and the same results and callbacks as before. The C
harness checks a device of two displays, and docs/api.md describes the
additions. QEMU's side follows in the next commit.
2026-11-15 15:16:04 -08:00
Anees Iqbal 9a4f391b23 📝 Say that Windows runs the window's callbacks on reims-vgpu.ui [ci skip]
With the window on Windows SDL3's on the library's own thread, as on Linux,
the C API's thread rules name Windows beside Linux: the header (from
window.rs, regenerated), docs/api.md and the feature's note in reims-vgpu's
manifest. docs/conventions.md says Windows takes Linux's host-call catch.
2026-11-15 07:44:23 -08:00
Anees Iqbal 7c672c67f8 🆕 Give the C API its Windows forms: a section's handle, an event
On Windows the host's guest RAM is a view of a pagefile-backed section and
QEMU's EventNotifier is a manual-reset event, so the two items that take the
host's OS handles get a form of Windows's own, under #if defined(_WIN32)
(cbindgen's [defines] maps cfg(windows) to it):
- RvgMemoryRangeFile is {void *handle; uint32_t flags; uint64_t offset;}, 24
  bytes; the create checks that a view the library may write maps the range's
  bytes from offset (MapViewOfFile, unmapped at once) and duplicates the
  handle (memory/windows.rs, with its tests);
- rvg_graphics_device_set_doorbell takes the event's handle.
A Windows device makes no bell, so it is handed neither route function.
Linux and macOS keep their forms and checks, under #if !defined(_WIN32); the
header's text changes there only by those guards and docs, and nothing the
guest or the host sees changes.
2026-11-15 06:43:52 -08:00
Anees Iqbal 5c1dd4f2e1 📝 Say where a rebound doorbell page drops its old routes
The device drops the routes on a doorbell page of its old context's queues
when a DOORBELL_PAGE_BIND write binds the page to another context, on the
host's thread that writes the register. The module doc still said such a
route stayed until its queue ended, and neither it, the host trait's
unroute_doorbell nor docs/api.md named the register write among the calls
that drop a route on the caller's thread, where the host must not wait for
a lock it holds around the call.
2026-11-14 18:01:16 -08:00
Anees Iqbal 18a87b6026 🆕 Give the device doorbell pages in BAR 4
A guest's single commit costs 5.4 us against 2.9-3.1 on a native Mac, and
about 1.5 us of it is the bundle's call into the kext to write DOORBELL.
The device now has a BAR 4 of 256 doorbell pages of 16 KiB, which a guest
binds to its contexts through DOORBELL_PAGE_BIND (0x040, `page | context <<
8`) where it took DOORBELL_PAGES (feature bit 46). A page is its context's
own DOORBELL: a write of a ring's id at its first byte wakes that ring
where it is one of the context's queues', and is answered at the doorbell
as there when it asks. Every other write is dropped and counted, logged
once per kind: at an unbound page or another offset (`unbound`), or of
ring 0 or another context's ring (`foreign`). Reads answer 0. A reset
unbinds every page.

A queue of a context with a page is routed on the page's doorbell
(`route_doorbell_page`), and on DOORBELL otherwise. A page bound to another
context drops the routes on it of its old context's queues, so the host
wakes no ring at a page but its context's.

What the host and guest see:
- the C API: `rvg_graphics_device_doorbell_write`, the config's
  `doorbell_pages` (third version) and the callbacks' `route_doorbell_page`
  (third version), whose thread docs/api.md section 5 names;
- FEATURES_HI bit 14 is offered only where the host sets `doorbell_pages`;
- QEMU's reims-graphics-pci (b4771167) maps BAR 4 and offers the pages;
- a guest that does not take the bit, or a host that does not offer it,
  has every doorbell on DOORBELL as before: the bind is ignored and every
  BAR 4 write dropped (`without_doorbell_pages_the_doorbells_are_the_register_s`).

The wire header's change rewrites the option ROM's record; its bytes are
the same.
2026-11-14 18:01:06 -08:00
Anees Iqbal 95843e7838 ✅ Prove a route waiting for the host's lock holds up nothing
QEMU applies a doorbell route under the BQL before route_doorbell returns,
so a ring-0 thread may wait for the BQL inside the device. That is safe
only if the device calls the route functions holding no lock of its own,
and no device function a host calls under the BQL waits for a thread
inside one. The device module's doc now names each call site and what it
holds: a QUEUE_CREATE, OBJECT_DESTROY or CONTEXT_CLOSE on the context's
lane (a ring-0 thread or the control thread) after the queues' table and
the ended rings are let go, and a reset on the resetting thread after it
let go of ring 0, the display, the completion ring, the table and the
ended rings. QEMU's registers are lockless, and its reset holds the BQL,
which its route function sees. What a host calls under its lock waits at
most for the control thread's display message, a ring's take or post, or
the table; the last device's release waits for the threads at most 5 s.

the_host_s_lock_held_around_its_calls_while_a_route_waits_for_it_holds_up_nothing
gives the test host a stand-in BQL its route functions take: while a
create's route waits for it, the thread holding it reads and writes the
registers, rings ring 0's and the queue's doorbells and resets the device,
and a watchdog ends the process if any call never returns. The route the
create took goes once the lock is let go. The C API and api.md name the
control thread among route_doorbell's threads, and design.md says which
hosts offer DOORBELL_ASKS.
2026-11-13 09:50:12 -08:00
Anees Iqbal 28250f0bed 🐎 Route plain queue doorbells to KVM where the guest asks apart
Every command queue doorbell write exited to QEMU and ran the library on
the guest's vCPU, though only a buffer that asks for its CB_SUBMITTED can
be answered there; the rest need only the queue's thread woken. Measured
on the Radeon 780M host, a doorbell QEMU takes as an ioeventfd costs the
guest 8.9 us and one run through the library 13.4 us, of a 17.1 us
commit.

The device offers DOORBELL_ASKS (feature bit 43, FEATURES_HI bit 11): a
guest that takes it writes an asking buffer's doorbell with the ring's
id and RGFX_DOORBELL_ASKS (bit 0), and every other doorbell with the id
alone. The device then answers at the doorbell only an asking write, and
gives each queue an eventfd its thread sleeps on, which it hands the host
with the ring's id (route_doorbell, a new C API callback at the end of
RvgGraphicsCallbacks, its second version). QEMU adds it as an ioeventfd
matching the id exactly, so KVM wakes the queue's thread in the kernel and
the vCPU goes back to the guest with no exit; the asking write matches no
route and reaches the library as before. A guest without the bit sees
nothing new: every doorbell is answered as before, and no queue is
routed.

The device routes at most 128 queues (KVM holds 1000 devices on its MMIO
bus, and QEMU ends the VM where KVM refuses one); a queue past them, or
one the host refuses, keeps its doorbell on the write, logged once per
kind and counted. A route goes as its queue ends, before the destroy,
close or reset is answered, and QEMU drops it under the BQL before the
callback returns, so a queue made again over the same ring gets every
write. The thread drains the eventfd and walks its ring on each wake, so
coalesced writes take every buffer in ring order.

QEMU's side is vendor/qemu d81bcaac. The guest's side, the ROM's record
and both guest sets follow.
2026-11-13 09:50:12 -08:00
Anees Iqbal 73b79f7e61 🐛 Quit a VM whose display read waits for a GPU that stopped answering
QEMU's quit hung for good after NVIDIA's Xid 109 (a context switch timeout)
in Apple's Modern Rendering sample, on an RTX 2080 and an RTX 3090: a
screendump's read of the display waited for its draw on the GPU, which never
finished, and the teardown at the quit waited for the read under the BQL
(thread_pool_free), so QMP's and HMP's quit both needed SIGKILL.

vendor/qemu 32a9f9a9 waits for the read at most 5 s, the bound the
library's release of its last device waits for its own threads, and past it
keeps the device to the exit, as a VM whose shutdown pauses it does. What the
host sees: such a quit now ends; a quit with no read under way, or one that
ends in time, is as before. docs/qemu.md says what was measured, including
that a pending screendump holds every QMP monitor while HMP, the vCPUs and the
guest go on, and docs/api.md the quit that keeps its device.
2026-11-12 16:38:38 -08:00
Anees Iqbal fab4be5201 🆕 Show black while the guest's display sleeps
A Mac's display goes dark when the OS puts it to sleep (Energy Saver's timer,
pmset displaysleepnow) and lights with the desktop at its wake. The device kept
showing the last frame instead, so a sleeping guest looked frozen.

What changes for the guest: the device offers DISPLAY_POWER (FEATURES bit 37)
on every host, and a guest that takes it sends SET_DISPLAY_POWER (command 8,
a 24-byte RgfxSetDisplayPower of display and on, answered COMPLETE), a display
command the control thread carries out. A reset turns the panel on. Nothing
else the guest sees changes: presents are taken and answered, VBLANKs go on,
and modes and the cursor's image stay.

What changes for the host: while the panel is off, the device's window and
rvg_graphics_device_read_display (QEMU's console and screendump) show black,
and cursor_show tells the host the cursor does not show; at the wake both show
what the guest shows again. The display callbacks' order now covers the
display power commands (docs/api.md). QEMU's reims-graphics-pci needs no
change: its console reads the display through read_display.

The tests that pinned the offered feature words and the backlog's display
command range take the new bit and command. The option ROM is built from the
wire header, so its record takes the header's new hash; the ROM's bytes are
unchanged (sha256 749434f8...).
2026-11-12 00:04:27 -08:00
Anees Iqbal 7f44053438 🐛 Answer a shared-memory GPU's working set from the guest's RAM
On a host GPU that shares the CPU's memory, the device answered
recommendedMaxWorkingSetSize with the platform's device-local memory, a share
of the host's RAM, which can be more than the guest's: guests of 8 GiB were
told 11864629248 bytes on a Radeon 780M and 24950464512 on an Iris Xe. No Mac
answers more than its RAM: a Mac's GPU that shares its memory answers a share
of it, from an Intel UHD 630's 0.023 (macOS 26.7.1) to an Apple M3 Max's 0.81
(macOS 27.0.1).

What the guest sees now:
- On a GPU that shares the CPU's memory (a Radeon 780M, an Iris Xe, an Apple
  GPU under MoltenVK), x86-64 and arm64 guests alike: the working set is three
  quarters of the RAM the host maps for the device, rounded down to 16 KiB, or
  the platform's answer where that is less; about 6 GiB for a guest of 8 GiB.
  maxBufferLength follows it by its rule (three quarters of the working set,
  about 4.5 GiB), and so does the kext's VRAM,totalMB (the working set in MiB).
- On a GPU of its own memory (an RTX 3090 or 5080, an RX 7900 XTX), and where
  the host passes no memory map: the platform's answer, as before, the card's
  own memory, as an AMD Radeon Pro 5300M answers its own 4 GiB less 16 MiB.

The RAM is the sum of the memory map's ranges, so no C API value is added:
q35's map leaves out the 384 KiB below 1 MiB, and vmapple's under HVF adds its
firmware's 1 MiB and config's 64 KiB, both well inside Apple's range.

On an Iris Xe, a macOS 14.8.9 guest told 24950464512, 5726617600, 4278190080
or 1610612736 bytes did the same work over a cold boot and a Safari scroll:
the runtime's blocks, the page's frames and the presents.
2026-11-11 13:16:00 -08:00
Anees Iqbal 56acf05064 🐛 Let go of the GPU device at the last graphics device's release
The C API's release ended a device's threads without waiting for them, and
the library kept its GPU device to the process's end; QEMU never released
its device at all, as it unrealizes nothing at its exit. A process that
exits with the runtime's device live can end with SIGSEGV inside NVIDIA's
driver (the commit before).

What the host now observes:
- rvg_graphics_device_release of the process's last graphics device waits,
  at most 5 s, for that device's threads, its objects and their GPU work to
  let go of the GPU device, then lets go of every GPU device the library
  keeps: the runtime's device is torn down on the releasing thread before
  the release returns (GraphicsDevice::let_go_of_gpu, LET_GO_BOUND).
- A device callback still running holds that wait up; one that waits for a
  lock the host holds around the release, for the whole 5 s. Past them the
  library keeps its GPU devices for the process, as before, and logs a
  warning naming what still held them.
- A device created after the let-go runs on a new GPU device, whose
  numbering starts again. A release while another device lives waits for
  nothing, as before.
- QEMU tears its device down as it quits, once vm_shutdown stopped the VM
  for good, with BAR 2 disabled first (vendor/qemu's reims-graphics-pci.c).
  None of its callbacks takes the BQL, which it holds there. A VM whose
  shutdown action pauses it, or that was stopped before the quit, keeps its
  device to the exit as before.

A new test (on_vulkan_the_let_go_waits_for_a_callback_blocked_on_the_...)
blocks raise_interrupt on a lock the releasing thread holds: the let-go
returns at its bound naming the ended decoder, and lets go once the lock goes.
The header, docs/api.md and QEMU's side change together.
2026-11-11 10:35:17 -08:00
Anees Iqbal df8f36bc9b 🐛 Answer a device the host's driver refuses as no device
A Vulkan device the host's driver refused at its make
(VK_ERROR_INITIALIZATION_FAILED and the other Vulkan failures, its memory run
out, or its device lost) ended the process at MTLCreateSystemDefaultDevice()
with a fatal line. It is the same class as a host with no Vulkan device, which
the library already answers: the platform makes none, and the graphics device's
create refuses.

Measured on an RTX 2080 behind a Thunderbolt 3 link (driver 620.70.06), whose
BAR1 is 256 MiB: each device maps 67 MiB of it, and 8 processes making a device
at once had 4 to 6 of them refused with -3 (NVRM: "dmaAllocMapping_GM107: can't
alloc VA space for mapping"). The refusals come from makes that race: with other
processes' live devices holding 245 of BAR1's 256 MiB, or seven devices of 16
queues each, a QEMU's device was still made, taking about 5 MiB. A QEMU whose
device the driver refused died at its realize.

What the host observes: rvg_graphics_device_create answers
RVG_ERR_HOST_RESOURCES where the driver refuses the device, as it does where the
host has no device, instead of the process dying; the library logs an error line
naming the refusal first. RVG_ERR_HOST_RESOURCES's message, which QEMU's realize
error prints, now reads "the host refused memory, a pipe, a thread or a GPU
device", and the header and docs/api.md name the GPU device among its causes.
The guest sees nothing: no device is realized either way. No degradation kind
changes.
2026-11-11 08:23:42 -08:00
Anees Iqbal cd6b7bb2bf 🐛 Answer framebuffer read to an arm64 guest as its host GPU can
arm64 guests drew WindowServer's menu bar, and some widgets, 100% black.
CoreAnimation compiles framebuffer fetch into its generic shader by
isFramebufferReadSupported, and that shader blends against the fetched
destination whatever the answer. The device answered no to every guest,
so the destination was the AIR's NaN. Metal on the M3 Max writes the same
NaN for that pipeline and its buffers, so the translation was right; the
answer was not one an Apple silicon Mac gives.

The x86 Macs split on it (AMD 5300M no, Intel UHD 630 yes, measured again
today), and x86 guests keep the AMD answer, which keeps their frameworks
off the platform's costly fetch. So the answer now follows the guest:

- The C API's RvgGraphicsDeviceConfig gains guest_arch (RvgGuestArch:
  RVG_GUEST_ARCH_X86_64 0, RVG_GUEST_ARCH_ARM64 1), its second version: a
  host built before it, whose struct_size ends before the field, serves an
  x86-64 guest; any other value is RVG_ERR_INVALID_ARGUMENT. The Rust API's
  GraphicsConfig gains GuestArch alike.
- The device-info blob answers isFramebufferReadSupported with the
  platform's answer for an arm64 guest (yes on MoltenVK, RADV, ANV and
  NVIDIA, which fetch through the pixel interlock), and no for an x86-64
  guest as before.

What the guest sees: an arm64 guest's MTLDevice answers
isFramebufferReadSupported yes where the host GPU fetches, once the host
names the guest arm64 (QEMU, next commit). An x86-64 guest's blob is byte
for byte what it was. On ANV and NVIDIA a pipeline that fetches an
attachment of a format the runtime does not fetch there (R8/RG8 sRGB, and
on NVIDIA the packed 16-bit formats and RGB9E5) is answered nil, relayed.
2026-11-09 18:15:57 -08:00
Anees Iqbal 031e0e8ca3 🐛 Hold the process's exit until the library's GPU steps end
A process exits on one thread, which runs the exit handlers and the loaded
libraries' destructors while every other thread goes on. A thread of the
library's inside a driver then ends the process: on the RTX 5080 test processes
ended with SIGSEGV inside libnvidia-glvkspirv after their tests passed, and the
awake thread's touches segfaulted 79 times in six minutes before it got a gate
of its own. The runtime's guard covers only the runtime's own threads, and the
platform keeps its devices for the process, so dropping them is no way out.

So the awake thread's gate becomes the library's: reims_vgpu_vulkan::exit_gate.
Each job of the library's threads that may call a driver is a step of it:
every reims-vgpu entry (the C API's calls, the device's callbacks, the jobs of
lanes, compile threads and surface workers), each compile (a making), a
window's frame, a video picture landed or frame encoded, and each call of the
video crate's threads, through a new Runtime::exit_step. An exit handler,
registered at each device's adoption, as a compile pool starts, after each
making and at each power of two of a thread's steps, closes the gate and
waits for the steps in progress on other threads, 2 s at most, or 60 s while
a compile is among them, as the runtime's guard waits for its makings. A step
begun after it ran parks its thread for good, except on the exiting thread.

What the host sees: an exit (QEMU's quit, a test process's end) now waits for
the library's GPU work in progress, bounded as above, where it could end the
process with a signal; a library thread or a call of the host's that would
begin work after the exit began parks. docs/api.md says so. Nothing the guest
sees changes. Tests: a child process that exits mid-compile with a second
compile queued (the first ends, the second never runs), the gate's own exit
children, and the awake thread's exit test on the shared gate.
2026-11-08 09:23:03 -08:00