Commit Graph
98 Commits
Author SHA1 Message Date
Anees Iqbal 5260a14032 🔥 Copy read-only memory whole at each commit, with no compare
A copy of read-only memory kept a base, compared every page with it at each
commit that used it, and at completion compared again to drop the GPU's
writes. The owner's rule is no compare-and-skip on read-only copies: memory
that is not provably unchanging is copied whole at each commit that uses it.

What the guest sees: the GPU's writes to read-only memory are still dropped,
as Metal's are, but now at the next commit that uses the memory, when its
bytes are written into the range again, not at the completion of the command
buffer that wrote them. A command buffer that writes read-only memory still
in flight when another commit uses it has those writes overwritten. The
writable memory and the writable shadows keep their merge, unchanged.

On the host (rt, copied.rs): a dropping copy keeps no base, copies all of its
pages at each commit, does nothing at completion, and no longer holds its
command buffer's handlers back. Counted for each kind (writable, read-only,
unchanging): rt-copied-*-commits, the bytes (writable-compared-bytes,
read-only-copied-bytes, and restored-bytes, as before) and rt-copied-*-sync-ns,
and the read-only commits and bytes by the block's why
(rt-copied-read-only-why-N-*), which the surface pages' import hands the
copy.

In the bundle (RgfxMemoryShadow): a read-only shadow keeps no agreed copy and
is copied whole at each sync. The process's commits, write-backs, bytes and
time for each kind are logged as "RGFX: memory shadows, totals" within a
minute of a change.
2026-11-20 10:45:23 -08:00
Anees Iqbal 318864dca5 🆕 Read once the file pages no guest process can ever write
Read-only memory under a no-copy buffer lies over a copy on rt wherever the
GPU cannot import it as it is, and every commit that uses the copy compared
all of its pages with their base. Much of that memory is a file's pages on
the guest's read-only system volume, which nothing can change while the
buffer lives, so the compare buys nothing there.

What the guest sees: the device offers feature bit 31, UNCHANGING_PAGES, with
READ_ONLY_PAGES, and the kext takes it (kext feature 1 << 19). At a no-copy
buffer's make over read-only memory, the bundle asks the guest's kernel once
whether the range is one file-backed region whose maximum protection has no
write (a shared mapping, or a private one whose maximum was lowered), whose
every page is the file's own, on a volume mounted read-only (proc_pidinfo's
fsid against getfsstat; no path is opened). Such a block is the app's own
memory, as before, marked RGFX_SURFACE_PAGES_UNCHANGING (and
_COPY_ON_WRITE); the kext's wire of a private mapping makes the process's
copy, as a Mac's does. Any other read-only block carries why not in its
flags' bits 8 to 11, which the host only counts.

What the host does: an unchanging block's copy is filled once, as its view is
made, never compared at a commit, and written again whole at the completions
of the command buffers that can write it, which drops the GPU's writes as
before. The views' summary counts the other read-only blocks by why, and
each texture over one (read-only-pages-textures-why-N, with one line per
format, size and why).

A private mapping whose maximum protection still has write is not provably
unchanging, since its process could raise its protection and write its copy:
it stays a plain read-only block, why "writable max protection".
2026-11-20 10:45:23 -08:00
Anees Iqbal 0f8bba3c2c 🆕 Make no-copy buffers at the host's address where blocks answer first
Where the device took IMPORT_ADDRESSES_LATER, a pages block is answered
before its view is imported, with no GPU address, so the bundle cannot place
the buffers over it at the view's address as it made them. It now lets the
host place them:
- a buffer over such a block goes in BUFFER_OVER_PAGES at gpu_address 0, which
  the host places at the view's address plus the buffer's offset; no address
  of the process's is picked for it;
- a Shared one asks its address of the host (PAGES_BUFFER_ADDRESS) at its
  first gpuAddress read, the argument encoder's and a texture over it
  included, and keeps the answer; a read with no answer is 0 and asks again;
- a Managed one, where the host makes Managed buffers at their copy's address
  (MANAGED_AT_COPY), asks its copy's address as it is made.

What the guest sees: on metal2vulkan-rt's platform, newBufferWithBytesNoCopy:
returns without waiting for the block's import, and the first read of the
buffer's gpuAddress waits for it instead. The addresses are the host's, as
they were where the block's answer carried one; on the default platform
nothing changes.
2026-11-20 07:15:38 -08:00
Anees Iqbal 7fff9ed966 ✅ Check only live Private picks for a shared span
The threaded pick test's 2000 picks of 32 KiB spans are 62.5 MiB, which go
round several of the guest's new 16 MiB blocks, so a span freed by one thread
is picked again by another. The test held every pick, freed ones included,
and failed on the re-pick. It now checks that no two live picks share a span,
that every pick lies in a block taken, and that the picks took more than one
block.
2026-11-19 16:25:31 -08:00
Anees Iqbal ac8fd0a663 🆕 Take 16 MiB Private blocks, and one of its own for a longer pick
The bundle asked the kext for Private blocks of 256 MiB. On metal2vulkan-rt each
was an allocation of the driver's in the take: on NVIDIA's driver about 0.08 ms
a MiB with the device's other calls held, 21 ms for 256 MiB. It now asks for
16 MiB (kPrivateBlockBytes), rt's allocation unit, which rt's block maker makes
ahead of need, so a take is a range of an allocation already made.

A pick whose span and alignment are more than 16 MiB takes a block of its own:
their length rounded up to 64 KiB, holding that pick alone, at the block's first
aligned byte. It goes back to the host with the pick's free. On a 780M desktop
boot, 4 of 77 Private objects were longer than 16 MiB (32 MiB each), 250 MiB in
all.

What the host observes: PRIVATE_TAKE asks for 16 MiB or one object's span rather
than 256 MiB, and a context holds more, shorter blocks, an own block given back
at its object's free. What the guest observes: its Private objects' gpuAddress
values lie in those blocks. Picks, refusals and gives are otherwise as before.
The tests that held 256 MiB blocks now hold 16 MiB ones and the own block.
docs/graphics-kext/design.md says so.
2026-11-19 16:25:31 -08:00
Anees Iqbal f13f992e49 🆕 Let the guest place a Managed buffer at its CPU copy's address
On a GPU that shares the CPU's memory a Managed buffer is its bytes, so its
GPU address is its CPU copy's. The guest's bundle picked it an address of its
own, in a Private block, which metal2vulkan-rt cannot keep for a buffer that
lies over the copy's bytes: rt made the buffer at the copy's address, while
the guest's pointers held the pick, and each pick held a Private take.

Feature bit 63, MANAGED_AT_COPY, names the other form: where the guest took
it, a MANAGED_BUFFER_CREATE's gpu_address is its CPU copy's, the guest takes
no address for the buffer, and the address goes back with the copy. Both
kexts carry the bit, the bundle's serializer, slabs and no-copy buffers post
the copy's address, and the device tells its host the features the driver
took as it becomes ready (Host::driver_ready), before any message, so the
host's decoder can read the creates in that form.

No host offers the bit in this commit, so nothing changes for a guest. The
wire header changes, so the ROM's record and the guest's prebuilt sets are
rebuilt in their own commit.
2026-11-19 15:43:47 -08:00
Anees Iqbal f4ae62647f 🐛 Pick a heap outside Private blocks at its own alignment, as before
Guest-visible against f180bde6f only: a device heap picked in the context's
own range (no Private block source, or a take refused) lies where any pick
there does again, at the alignment asked and the 32 KiB granule, not at a
multiple of 256 KiB. Against development, only heaps in Private blocks move:
they start at a multiple of 256 KiB from their block's first byte.

The 256 KiB serves rt's binding of a heap's textures at their offset in a
Private block's memory; a heap outside the blocks is a block of its own on rt
and binds at its own base, so the pad bought nothing there, and it broke
test_addresses_reused_after_destroy: a freed heap's span joined the pad's free
bytes below it, so the next pick of that span lay below the heap's old
address. allocPrivateAddress takes the block-only alignment as `blockAlign`
in place of a flag, and falls back to allocAddress at the caller's alignment.
2026-11-19 07:52:55 -08:00
Anees Iqbal 97808c8997 🐛 Pin Private heaps at 256 KiB from their block's first byte
Guest-visible: a device heap the bundle pins in a Private block now starts
at a multiple of 256 KiB from that block's first byte, on both platforms,
where it started at the next 32 KiB granule. Heaps' gpuAddress values move;
no other pick does, and no wire field changes.

metal2vulkan-rt binds a heap's texture at the heap's offset in its block's
memory plus the texture's offset, and refuses one whose place there is off
the image's alignment, though Metal's rule (the offset a multiple of
heapTextureSizeAndAlign's align) places it. With heaps on the 32 KiB granule
every texture whose image asks 64 KiB or more (the 780M, Iris Xe, the XTX)
could land off it: pl8rt counted 85 such refusals in one boot, and the
window overview's thumbnails stayed grey.

256 KiB is the largest image alignment measured on the hosts' GPUs (a
multisample target on an RX 7900 XTX) and the current runtime's own heap
base alignment. The pick counts from the block's first byte, not from 0,
because the block's address is aligned only to the host's 64 KiB while its
memory starts at a multiple of 2 MiB on rt. Where no block is held the heap
lies at a multiple of 256 KiB in the context's range. The leading gap stays
in the block's free list, so later smaller picks reuse it once picks wrap.
2026-11-19 07:52:55 -08:00
Anees Iqbal 66370e18be 🆕 Place no-copy buffers at the address the host gives their block
On metal2vulkan-rt a no-copy buffer's GPU address is the device address of the
host's import of its pages, which only the host knows, so a guest pick cannot
match it. Where the kext and the device both took IMPORT_ADDRESSES, the bundle
asks PagesBlock for its third output, the GPU address the host gave the block's
view, and makes each BUFFER_OVER_PAGES over the block there plus the pointer's
offset in its page, with no pick of its own and none given back at its destroy.

What the guest sees changes: a no-copy Shared buffer over writable memory the
kext wires answers a gpuAddress in [2^43, 2^44) on the Vulkan platform (at its
pages' import on rt) instead of a pick in the context's range, and two no-copy
buffers over the same bytes, which join one block, answer the same gpuAddress.
A Managed no-copy buffer's own address stays its GPU copy's pick; only its CPU
copy, the Shared buffer over the pages, moves. Read-only memory, copied memory
and blocks given no address keep their picks, and a guest without the bit sees
nothing change.

design.md gains the "Import addresses" paragraph, PagesBlock's third output and
the kext's bit 18.
2026-11-19 02:54:55 -08:00
Anees Iqbal a75f372539 🆕 Place Private objects in the Private blocks the host gives
This change is visible to the guest. Where the kext and the device both took
PRIVATE_BLOCKS, these objects now get their gpuAddress inside blocks the
host gives the context, rather than in the context's own range in
[2^46, 2^47):
- Private buffers;
- heaps that are not Shared, and the buffers in them;
- Managed buffers, whose GPU copies are what the address names.

On the Vulkan platform those addresses lie in [2^44, 2^45). On
metal2vulkan-rt's device they are the Vulkan device addresses of the host's
memory, which is the reason for the move. Shared buffers, the pool's heaps
and no-copy buffers keep their addresses.

How the bundle picks inside its blocks:
- Picks are next fit through the blocks, on the same span and granule
  rules as the context's range.
- A block is taken from the kext when none has room: 256 MiB, or the pick's
  span and alignment rounded up to 64 KiB where that is more. 256 MiB is
  rt's pooled block, and it keeps a freed span from being picked again soon
  after its destroy.
- Free ranges never join across blocks, so no pick spans two of them.
- One empty block is kept for the next picks. Any further empty block is
  given back after the last object's destroy has been posted.
- Where the host refuses a take, the bundle picks in the context's range as
  before and logs the first refusal.

The serializer tests cover picks in blocks, a block of its own for a large
pick, giving back and keeping, blocks side by side, refusal and threads.
docs/graphics-kext/design.md gains "Private blocks" and the two selectors.
2026-11-19 02:30:57 -08:00
Anees Iqbal 8447199fee 🆕 Note a no-copy buffer's identity the first time it escapes
The host reads a no-copy buffer's bytes in place. The guest's Metal can hand
that buffer's identity to the app, or write it where the GPU reads, with no
message naming the buffer, and from then on the GPU may reach the buffer
through memory alone. The host had no way to know which buffers that happened
to. This commit adds a note for it, so the host can tell them apart.

The note is RGFX_CMD_BUFFER_ESCAPED (0x23b), RgfxBufferEscaped of 32 bytes:
the buffer's object id and the act, one of GPU_ADDRESS, ARGUMENT_ENCODER,
RESOURCE_ID and RESIDENCY_SET. The device offers it as
RGFX_FEATURE_BUFFER_ESCAPED (bit 56), and both kexts carry it. The submit gate
takes it in a batch, like a Managed range's write: unwaited, a fixed size,
naming no block and making no object.

The bundle marks each covered buffer as it is made (_rgfxEscape, set by
rgfxOwesEscapeNote) where the host took the feature. The first of these acts
in the buffer's life posts the note, before the act returns:
- a read of the buffer's gpuAddress;
- an argument encoder's setBuffer(s) of it, or setTexture(s) of a texture or
  view over it;
- a read of such a texture's or view's gpuResourceID;
- the buffer, or such a texture, joining a residency set.
Every later act is one relaxed load.

Wire order holds the note ahead of any command buffer that could use the
identity. The note goes on ring 0, posted or held in the serialiser to go
before the next commit's messages, and the device carries out a command buffer
only after every ring-0 message its process posted before it. Across threads,
the act's slow path posts the note under the buffer's lock and then clears the
mark with a release store. Every commit takes an acquire fence before it posts,
so a commit after an act that found the mark clear posts after the note.

The covered buffers are the app's no-copy buffers over its own pages
(BUFFER_OVER_PAGES, writable or read-only, bit 24) and over bytes already in
a block of the process (BUFFER_OVER_HOST_MEMORY). Managed buffers, heaps over
pages, IOSurface shadows and buffers placed in a slab's heap
(BUFFER_AT_OFFSET) are not covered.

A slab buffer made with an address of its own is covered too: a Shared buffer
past 2 MiB over a slab of the process's own pages, a device buffer, or a
no-copy buffer's shadow (RgfxSlabSource::Made's ownsAddress). It reaches the
host as the same BUFFER_OVER_PAGES or BUFFER_OVER_HOST_MEMORY message as an
app's no-copy buffer, and the host takes every such message alike. Leaving
slab buffers out would leave the host unable to read the mark as "this
buffer's identity never left".

The host marks the buffer's entry escaped, once for its life, and a later
note changes nothing. A note for an object that is not such a buffer is a bad
reference. A note for an object whose create the host refused is dropped, as
any naming of one is. Escapes are counted in the degradation counts line and
in the recorder's stats (`counted <code> <n>` lines), with no new stream
records:
- buffer-escaped-gpu-address, -argument-encoder, -resource-id and
  -residency-set, by the act that noted the escape;
- buffer-escaped-read-only-pages, the escaped buffers over a copy of their
  pages;
- no-copy-buffers, every no-copy buffer made, of which the escapes are a
  share.
Each act's first escape is also logged at info.

This changes nothing the guest observes. No one waits for the note, it has no
reply, and the mark changes no answer, object, address or command. Both
prebuilt sets and the option ROM's record are rebuilt from these sources. The
ROM's bytes do not change: its record changes only by the wire header's hash.
2026-11-18 19:52:34 -08:00
Anees Iqbal 191daab32a 🐛 Send a render pipeline's tessellation state and call stack depths
A guest's render pipeline drew its patches with Metal's default tessellation
state, whatever its descriptor set: power-of-two partitions, a maximum factor
of 16, no factor scale, constant factors per patch and clockwise output. The
bundle never sent the seven tessellation fields, so the device never saw them.
For the same reason the device never saw maxVertexCallStackDepth or
maxFragmentCallStackDepth, and created every pipeline with a depth of 1.

The bundle now reads each of these properties and sends it. Each tessellation
field travels as Metal's raw value, and tessellationFactorScaleEnabled as a new
flag, RGFX_RENDER_PIPELINE_TESSELLATION_FACTOR_SCALE (bit 8). The depths are
read only where the descriptor answers their selectors (macOS 12 and later);
older guests send 1.

The decoder passes every value through FullRenderPipelineDesc to the runtime's
RenderPipelineDescriptor. A state equal to Metal's defaults becomes the
runtime's `None`, so a pipeline that sets nothing is created exactly as
before. A raw value the runtime has no enumerator for dies at the send, as
every other enumerated field does. The recorder's tag-13 map carries the
values as three keys after privateState: `tessellation`,
`maxVertexCallStackDepth` and `maxFragmentCallStackDepth`.

Guests now see patches tessellated with the partition mode, maximum factor,
factor scale, step function, winding and control point index type that their
pipeline sets. They also see pipelines created with the call stack depths
they asked for.

The tessellation fields and the call stack depths share this commit because
they share one wire record. RgfxRenderPipelineCreate grows from 464 to 496
bytes, with eight u32s appended at offset 464, and both prebuilt sets and the
option ROM's record are rebuilt from these sources. The ROM's bytes do not
change.
2026-11-18 18:12:19 -08:00
Anees Iqbal 61084eab14 🆕 Tell the host which bytes of a new Managed buffer read zero
On a GPU without unified memory the runtime makes a Managed buffer's GPU copy
by copying all of its CPU copy over the link as the buffer is made. Most of
those bytes are zeros the guest never wrote: on the RX 7900 XTX, Geekbench 7.1
Metal's making copies carried ~4.5 GB beyond what the guest marked written, up
to ~0.94 s of GPU time (one 131 ms job copied six new buffers' 631 MB, 12 MB
of it written). The guest knows which bytes read zero; the host cannot.

What changes for the guest and the host:
- MANAGED_BUFFER_CREATE takes a tail, RgfxManagedBufferZero at byte 56
  (RGFX_MANAGED_BUFFER_ZERO_OFFSET): its CPU copy reads zero from zero_from to
  the length as the create is posted. The bundle sends it with every create:
  0 for a buffer made with no bytes, whose slab range take() zeroed; the
  length, which says nothing, for one made with bytes or over the app's own
  memory. The kext's SubmitBatch takes the create at its new size, 64 bytes.
- The host passes a zero_from below the length to the platform
  (BufferDesc::zero_from), and the Vulkan platform to the runtime
  (HostMemory::with_zero_from): the GPU copy starts with zeros the GPU writes
  there, rather than with those bytes copied in. A guest write there reaches
  the GPU copy by a didModifyRange:, as on a Mac's discrete GPU, whose GPU
  copy is made at the buffer's making; before, a write made before the host
  took the create reached it too. A GPU that shares the CPU's memory keeps one
  copy, unchanged, and a create without the tail (an older guest's) is copied
  in whole, as before. An older host reads the create and ignores the tail.

Tests: the decoder passes the tail's offset and none without it or at the
length; the platform's zero bytes are the device's on a GPU of its own memory;
the serialiser, the slabs and the device buffers send each make path's value.
Both guest sets and the ROM's record are rebuilt in the next commit.
2026-11-15 18:44:04 -08:00
Anees Iqbal 15df01ce67 🐎 Arm the held destroys' timer after a delivery's waiters go
A command buffer's delivery releases what the buffer kept before it lets
its waiters go, as Metal does, and the destroys those releases hold armed
their 1 ms timer there: a dispatch_after, whose kevent wakes the dispatch
manager's thread. In a guest ktrace of Geekbench's Metal Super Resolution
on the 780M host that arm took the release 16.9 us p50 (30.2 p90) of each
delivery on the completion queue, between the app's handler and the wait's
return. The delivery now holds the arm (RgfxRingLaterArms) until its
waiters are let go: the destroys go as before, the timer from the end of
the delivery.
2026-11-14 07:50:59 -08:00
Anees Iqbal 14c2c18e8c 🆕 Stamp each command buffer with the guest's clock (GUEST_TIMES)
The host sees a command buffer only once a queue's thread takes it off its
ring, so the app's encode and the buffer's way to the host show as one span
with nothing pending on the host. This adds the guest's stamps that split it,
a diagnostic.

What changes for the guest:
- The device offers a new feature bit, RGFX_FEATURE_GUEST_TIMES (bit 44,
  FEATURES_HI bit 12), and DRIVER_FEATURES takes it. The kext carries it.
- Where the device's word holds it, the bundle starts each command buffer's
  stream with a new op, RGFX_OP_GUEST_TIMES (0x000e, 32 bytes): the guest's
  clock as the buffer was made, as its commit was entered, and as its message
  went on its ring. The last is written under the ring's lock, after the copy
  and before producer is published; for a buffer in a block, into the block.
- After each VBLANK's reading of HOST_TIME the kext writes ring 0's new header
  fields: guest_clock_offset_ns (0x18), then guest_clock_bound_ns (0x20) with
  release, split out of reserved bytes. The host zeroes both as it initialises
  a ring.
- The decoder walks the op as a no-op, so it changes nothing a buffer does,
  and a host that did not offer the bit never sees it. Old guest sets carry
  no bit, send no op and leave the fields 0.

The header is regenerated, both guest sets are rebuilt from the same sources,
and the option ROM's record takes the header's new hash; the ROM's bytes are
unchanged. The tests cover the op's and fields' layout (the header test, the
kext's vblank clock test), the bit's negotiation (the kext's features test,
the peer and memory-map tests), the stamps (the bundle's serializer and shell
tests) and the walk (a cmdbuf test, and the fuzz's streams). The host's
reading of the stamps is the next commit.
2026-11-13 21:25:12 -08:00
Anees Iqbal 15801990cd 🐎 Ring an asking buffer's doorbell apart from the rest
The host can take a queue's doorbell with no exit to its process only
where the write needs nothing of it but a wake, and a write of the ring's
id alone could be a buffer that asks for its CB_SUBMITTED, which the host
answers inside the write. Where the device offers DOORBELL_ASKS, the kext
takes it and the bundle writes an asking buffer's doorbell with the
ring's id and RGFX_DOORBELL_ASKS (bit 0), every other doorbell with the id
alone. On a host that does not offer it the bundle writes the id alone, as
before.

The kext writes what the bundle hands its doorbell call, so it needs
nothing more than the bit in its carried features. Tested by the kext's
features test and the bundle's
test_ring_doorbell_asks_where_the_device_takes_them.
2026-11-13 09:50:12 -08:00
Anees Iqbal 7c9c37a4f6 ✅ Hold the guest tests' timing checks under load
Batch 42's guest test (job m3-max-25789) failed under the M3's load at
serializer_tests.cpp's CHECK(ending.calls.empty()): with setBatch's timer at
1 ms, a test thread held off a CPU for 1 ms after postLater let the timer
post before the writer's end. It is the class of the room-record test's
failure: a check that holds only while a thread runs within a time window.
The other guest test sites of it, found by reading every sleep, clock read
and timer delay in the bundle's and the kext's tests:

- serializer_tests, test_held_destroys, the writer's end: a run counts only
  where the look at the calls comes within the timer's 1 ms from before the
  post; runs repeat, up to 50, until one does. The calls are read under the
  log's lock, as the timer's thread writes them.
- serializer_tests, test_ring_post_waits_for_room: the post's time, checked
  to be at least 15 ms, was counted from after the host's thread started its
  20 ms; it is now counted from before, so a late start of this thread no
  longer shortens it.
- queue_tests, test_submitted_waits_for_the_buffers_before: the same for the
  40 ms the wait must last, against the thread that commits after 50 ms.
- shell_tests, test_submitted_at_the_doorbell_wakes_no_thread: the
  completion thread's late wake came 400 ms after the doorbell, and the test
  checked the call took under 200 ms and the record was still untaken. The
  wake now waits for the test to let it go (or 5 s), so the test checks the
  call returned before the wake and the record untaken, with no window.

Each keeps what it checks. The rest hold under any delay: lower bounds
counted from before what they wait for, negative checks of work nothing
lets run, and bounded waits of seconds.
2026-11-13 09:49:33 -08:00
Anees Iqbal ba1cf087e8 ✅ Count the room-record test's runs only inside the backstop
test_ring_post_waits_for_the_room_record checked that a post still waited
3 ms after the host took room with no record, and that the record ended
the wait within 15 ms. The wait's backstop ends it 20 ms after it began
(RgfxRingWriter.cpp's kRoomBackstopUs), and under the load of batch 41's
check (job m3-max-25671) the test's threads were held off a CPU long enough
for the backstop to come first: CHECK(!posted) failed, where the test
passed alone (25724). The post's own look after it stores the flag could
also find the host's take when its thread was held between the two, as the
test took room once it saw the flag.

The test now takes room only after the post rang the doorbell, which it
does once that look found no room, so from then on only the record or the
backstop ends the wait. A run counts only where it ends inside the backstop
from before its post began: the record alone can then have ended its post,
which was still waiting when looked at 3 ms after the take. Runs repeat, up
to 50, until one counts. A post the record does not wake never counts, and
one that does not wait fails the run that counts.
2026-11-13 09:49:33 -08:00
Anees Iqbal 8430562098 🐎 Hold textures, views and Managed creates with the destroys
A texture's create (TEXTURE_CREATE), a view's (TEXTURE_VIEW), a Managed
texture's and buffer's (MANAGED_TEXTURE_CREATE, MANAGED_BUFFER_CREATE) and a
Managed buffer's modified range (BUFFER_DID_MODIFY_RANGE) are messages no one
waits for, each its own Submit call: on the RX 7900 XTX host 8 to 13 us each,
812 of them and 8.8 ms a Vision process. SubmitBatch now takes them too, each
of its one size, checked as Submit checks it, a create for a context closed
since its check destroyed behind it (rgfxSubmitBatchSize). None names a block,
which spans_test keeps. The bundle holds them with the destroys and the
heap-placed buffers (RgfxSerializer::postUnwaited), in their order, posted
before anything else the process posts or asks of the kext and before any
command buffer it commits. A texture over an IOSurface still goes at once, as
a present of the surface waits for it, and so do the creates that name a block.

This changes what the guest sees under transport exhaustion only (the control
ring full for 5 s, or the device stopped), as 628b03fb3 did for heap-placed
buffers: such an object made then is answered as made, held, where its refused
post answered nil. Its refusal reaches the app where Metal's caller sees a
lost GPU's: the next post retries what is held and is refused in that same
call where the held are refused again; a create that needs the held gone to
make room is refused at once; and a commit that finds anything but destroys
held fails its command buffer, unposted, with MTLCommandBufferErrorInternal.
postHeldCreates already answered so for every held type; serializer_tests'
test_held_creates checks each kind on its own, and protocol_tests'
test_commit_behind_an_untaken_buffer_fails a texture through a commit. A
create the host refuses was answered by no one before, and is not now.
2026-11-13 09:01:40 -08:00
Anees Iqbal 88b34b304a 🐎 Hash library keys in 256 KiB pieces on the guest's cores
A library's content key was one SHA-256 over its bytes past the bitcode
section: 2.9 to 6.2 MB for the MPS and Espresso libraries a Vision request
loads, hashed by CommonCrypto at 567 MB/s on a guest core (the RTX 3090 host's
guest, with or without the CPU's SHA extensions, which CommonCrypto does not
use), 30 ms of every process's newLibraryWithURL. The key is now
"rgfx-library-v2": SHA-256 of the prefix and of each 256 KiB piece's SHA-256,
in order, and the bundle hashes the pieces at once (dispatch_apply_f); the key
of a library's whole bytes, new on this branch, takes the same form
("rgfx-library-whole-v2"). The host computes both the same way, and both
sides' tests pin the same digests, a multi-piece library's among them.

wire.h is regenerated from wire.rs's new definition, so the ROM's record moves
(its bytes stay the same). An older guest set on this host keys its libraries
the old way: the host then vouches for none of them, so they are made in each
process, uncached, as before the cache. The host's persisted pipeline records,
keyed by their libraries' keys, go cold once.


Measured with the previous commit (rts2 against rts4, RTX 3090 host, load 3.4
and 3.0): newLibraryWithURL, 18 loads, 49.0 -> 18.3 ms in the first process,
44.5 -> 18.0 ms in the second; the three default.metallib loads that hash MPS's
and Espresso's libraries 7.3, 15.5 and 12.0 ms -> 2.8, 4.9 and 3.8 ms.
2026-11-13 04:02:31 -08:00
Anees Iqbal a6210a27d7 🐎 Key the libraries the host cannot vouch for by their bytes
The host keeps a library under its content key only where it checked every
function's HASH tag against its bitcode. Core Image's libraries fail that check:
all 232 functions of ci_stdlib share one module whose HASH is no SHA-256 of
it. So ci_stdlib and ci_filters were made again in every process, twice each
in a Vision process (1.5 and 2.5 to 3.7 ms a load on the RTX 3090 host), and
their bytes crossed each time. The host now keeps such a library of at most
4 MiB under the key of its whole bytes, SHA-256 of "rgfx-library-whole-v1" and
them, which it computes from the bytes it stores, so no guest can bind other
bytes to it; the bundle asks for that key after a miss on the content key.
No wire change: LIBRARY_LOOKUP takes either key.


Measured with the next commit (rts2 against rts4, RTX 3090 host, load 3.4 and
3.0): the libraries' uploads (LIBRARY with a region) 5 calls and 8.0 ms ->
1 and 0.2 ms in the first process, 4 and 4.7 ms -> none in the second; the
first process's second loads of ci_stdlib and ci_filters 1.53 and 3.82 ms ->
0.24 and 0.20 ms.
2026-11-13 04:02:30 -08:00
Anees Iqbal 628b03fb37 🐎 Hold heap-placed buffers with the destroys around them
A heap's buffer at an offset (BUFFER_AT_OFFSET, each small buffer of a slab) is
a create no one waits for, and cost its own kext call: 3079 a Vision process,
12 to 15 us each, 40 to 48 ms a process on the RTX 3090 host, between the
destroys, which broke each held run of destroys into one call of about 11.
SubmitBatch now takes BUFFER_AT_OFFSET too, of its one size, checked as Submit
checks it, and a buffer made for a context that closed since its check is
destroyed behind it, as Submit does (RgfxSubmitGate's rgfxSubmitBatchSize;
spans_test checks no message the batch takes names a block). The bundle holds
each buffer with the destroys, in their order, and a command buffer's commit
posts what is held before the buffer goes on its ring, as the host reads a
command buffer after the control ring's messages posted before it.

This changes what the guest sees under transport exhaustion only (the control
ring full for 5 s, or the device stopped):
- before, the buffer's create was refused there and the buffer was nil; now
  it is answered as made, held, as Metal makes objects on a GPU that is gone;
- before, a command buffer committed on a stopped device was never answered,
  and the run hung at its wait; now a commit that finds a buffer still held
  fails the command buffer, unposted, with MTLCommandBufferErrorInternal
  ("Discarded (victim of GPU error/recovery)"), the host's own recovery error,
  as the buffer may name it;
- the next post retries what is held and is refused where it is refused
  again, and a buffer that needs the held gone to make room is refused at
  once, holding nothing more.
Tested by serializer_tests' test_held_buffers and protocol_tests'
test_commit_behind_an_untaken_buffer_fails.


Measured, Vision cold request, RTX 3090 host (rts2, the destroys held,
against rts4, this commit and the next two; load 3.4 and 3.0): destroys and
heap-placed buffers together 62.0 -> 17.6 ms in the first process (3079
buffers and 294 destroy calls become 688 SubmitBatch calls), 51.6 -> 16.1 ms
in the second.
2026-11-13 04:02:30 -08:00
Anees Iqbal 7e8ea3c46f 🐎 Post a process's destroys in one kext call
Each destroy cost the guest a trip into the kext, and a post that found the
host's control thread asleep a doorbell's exit too: 14 us a destroy in a Vision
request on the RTX 3090 host, 3415 destroys and 68 to 70 ms a process, in runs
an app lets go back to back. The kext now carries SubmitBatch (selector 5,
feature bit 1 << 1): the bundle holds each destroy (RgfxRingWriter::postLater)
and posts what it holds in one call of at most 4096 bytes, before anything
else the process posts or asks of the kext (every IOConnectCall goes through
rgfxKextConn, which posts the held first), once the held fill a call, or 1 ms
after the first held. The kext checks each as Submit checks one, all before it
posts any, puts the runs on the ring under one hold of its lock with one
doorbell at most, and ends a queue's as Submit does. A texture over an
IOSurface goes at once: a present of the surface waits for its destroy.

What an object lay over goes back as its destroy is held, as it went back once
the destroy was posted: whatever takes it again is posted after the destroy,
so the host drops the object first, and every answer the guest reads (a heap's
usedSize, a new object's GPU address and resource id) is the one it read
before (shell_tests' test_held_destroys_change_nothing_seen). Where the kext
stops taking them (control ring full 5 s, or the device stopped), the rest stay
held; the next post retries them under its own bound and is refused only where
they are refused again. The one difference, under that exhaustion only: what
the held destroys lay over has gone back, so a heap's usedSize read during a 5 s
stall of ring 0 reads lower than before (design.md, "Destroys go together").

Measured, Vision cold request, RTX 3090 host (rts1 against rts2, load 3.6 and
3.4): destroys 69.6 -> 18.6 ms in the first process, 67.7 -> 14.7 ms in the
second; all kext calls 216 -> 162 and 188 -> 135 ms.
2026-11-13 04:02:30 -08:00
Anees Iqbal 34efdc631a 🐎 Ring a walking queue's doorbell only for a buffer that asks
While a queue's service thread walked a buffer on its own thread, it marked
the ring as it marks it before it sleeps, so the guest rang the queue's
doorbell for every buffer it published meanwhile: a VM exit that answered
nothing for a buffer that does not ask for its CB_SUBMITTED, in two of three
commits of a back-to-back run on a 26.7.2 guest (11.9 µs a doorbell on a
780M host). The thread now marks the ring walking (consumer_idle 2,
RGFX_CONSUMER_WALKING), for which the bundle rings only for a command buffer
whose last op is its ask, which the doorbell's write may answer; the thread
takes the rest after the walk, as before. A bundle that rings only for 1
rings for none during a walk, and its asking buffer is answered at that take.

What the guest sees: consumer_idle reads 2 during a walk, where it read 1.

Measured with commit_split, before and after in one job each, cold boots of
26.7.2 on um870 (m3-max-23492, m3-max-23493):
- RTX 3090: a back-to-back commit 15.8 -> 2.2 µs (median), doorbells 0.57 ->
  0.30 a commit, 22.5 -> 20.2 µs a buffer over 100,000 buffers; a commit's
  CPU time in encode_probe's cb.commit 19.6-21.2 -> 15.3-15.7 µs.
- Radeon 780M: 44.5-46.5 -> 44.7-45.3 µs a buffer, bound by the host's
  submits; doorbells 0.66 -> 0.54 a commit, the rest of them posts that wait
  for room; cb.commit's CPU time 19.6-20.2 -> 18.4-18.9 µs.
An idle queue's single commit rings as before (16.4-17.4 µs on both).
2026-11-12 19:06:13 -08:00
Anees Iqbal ecf693f1bf 🐎 Build a large command buffer's message off its block
A message too large for half its queue's ring was built in a block of the
queue's pool once it outgrew the ring, and grew from block to block. The
block is write-combined where the kext maps it so, and on an AMD host, which
applies the guest's memory type, a write-combined store still pending
stalls the encoding thread's next locked instruction until it drains. Every
retain and release on that thread, the app's and ours, paid it: on a
26.7.2 guest of a Ryzen host (RTX 3090), encode_probe's
setRenderPipelineState took 108 ns a call in a 10000-call buffer against
21.6 ns in a 400-call one, which stays on the builder's heap, and a
setVertexBuffer between a CFRetain and a CFRelease 103 ns against 17.1 ns.
Growing from block to block also read the old block back, uncached.

The message is now built in the builder's own buffer whatever its size,
which grows to the queue's guess as it outgrows the ring, and the commit
copies it once, whole, into a block from the block's start. The host reads
the same COMMAND_BUFFER bytes; only where the message lies in its block
changes. The tests that held the old placement change with it.
2026-11-12 16:14:00 -08:00
Anees Iqbal a5c9010efc 🆕 Answer each format's own minimum linear texture alignment
The device answered minimumLinearTextureAlignmentForPixelFormat: with
linearTextureAlignmentBytes, the largest of every format's, for each format. On
a host GPU whose linear images ask more of larger texels that is too much for
the smaller ones: a Radeon RX 550 answers 256 bytes for texels of 1 to 4 bytes,
512 for 8 and 1024 for 16, and Geekbench's MPSImageGaussianBlur source, an
RGBA8Unorm texture over a buffer with rows of 7680 bytes, was nil against 1024.

What the guest observes changes:
- each format answer of the device info carries the runtime's alignment of
  that format in its last two bytes (0x3e, `linear_alignment`, which were
  zero): 0 where the runtime has none (compressed, depth and stencil formats)
  or one past 65535;
- the bundle answers minimumLinearTextureAlignmentForPixelFormat: with the
  format's own, and with linearTextureAlignmentBytes where the blob has none,
  so the video output planes align to R8/RG8/R16/RG16's own answers too;
- linearTextureAlignmentBytes and deviceLinearTextureAlignmentBytes stay the
  largest of every format's.
2026-11-12 09:19:28 -08:00
Anees Iqbal 50be7d4a9a 🆕 Carry compute dynamic libraries to the host
An x86-64 guest's device made no dynamic library: newDynamicLibrary:error:
and newDynamicLibraryWithURL:error: were RGFX_UNSUPPORTED, so a kernel linked
against a dynamic library could not be made. The platform makes them and
links them (the commit before), and nothing carried them from the guest.

What changes for the guest: the device offers feature bit 40,
DYNAMIC_LIBRARIES (FEATURES_HI bit 8), on every host, and the kext takes it.
Where taken, ring 0 carries DYNAMIC_LIBRARY_CREATE (0x239: the dynamic
library's ObjId and its library's), answered by COMPLETE with its status, and
a COMPUTE_PIPELINE_CREATE ends with a run of RgfxPipelineLibrary records
(library_count, where reserved_5c was): each a dynamic library, preloaded
where it has no install name, else the library that install name loads. The
bundle's RgfxDynamicLibrary answers its library's install name and writes its
bytes to a file URL; newDynamicLibraryWithURL: reads the file in the guest's
process with the AMD Radeon Pro 5300M's and the Intel UHD Graphics 630's
answers (measured, macOS 26.7.1, oracle_urls and oracle_fnptr: a nil URL,
bytes that are no metallib, a library that is not dynamic, a missing file).

Metal reads the file each install name of a library made from a file names
when it creates a pipeline, and none of a library made of bytes or of source
(measured, both Macs, oracle_fnptr: the linked kernel's file makes its
pipeline, and the same bytes through newLibraryWithData: are refused). The
bundle reads them then too: every name the kernel's library, its linked
functions' libraries and these dynamic libraries name, from the executable's
directory, the loading library's or an absolute path, each sent by name. A
name that loads nothing, or that a library of no file names, refuses the
pipeline with the 5300M's CompilerError 2, beside a preloaded library or not,
so the host is never left to read a file of its own. A dynamic library's
install name is read from its metallib's HDYN NAME tag. Render stages carry
none.

A compile of source whose options name dynamic libraries reads each through
the private member airData, the one message Metal sends it (measured, both
Macs): the bundle's dynamic library answers its library's bytes, as Metal's
answers its file's, where it ended the guest's process with an unrecognized
selector before. The caller owns the answer, as it owns Metal's, which is a
new object at each call: the compile releases it, and an unowned answer ended
the guest's process with SIGSEGV after the compile.

supportsDynamicLibraries stays no in this commit; a later one answers it once
the guest's sets carry this bundle. supportsRenderDynamicLibraries stays no,
as the 5300M answers it.

The host knows bit 40 (KNOWN_FEATURES); the tests that probed an unknown bit
at 40 probe 41. The decoder's tests make dynamic libraries on the fake
platform and pass a run's libraries to the creator; the serializer's, the
shell's (the linkage reader, the routing and airData, a pipeline's loads and
refusals), the kext's and the members register's tests cover the guest's
side, and design.md describes the messages.
2026-11-12 08:14:05 -08:00
Anees Iqbal 149fc57c85 ✅ Hold the join test's first memory as the kext does
test_region_pages_join_over_remapped_memory failed now and then at its check
that new memory mapped over the first (VM_FLAGS_OVERWRITE) has another
identity than the first (rgfxPagesIdentity). The identity is the memory's
vm_object's id, which a new object can take once the old one is freed, and
the test's fake block source holds nothing of the first memory, so the
overwrite freed it.

Production does not: the kext wires a pages block's memory (its
IOMemoryDescriptor's prepare), which keeps the first memory's object alive
while the block is held. Measured in a macOS 26.7.2 guest of our device on an
Iris Xe, 200 overwrites each: with a no-copy buffer over the first memory
alive, the new memory's identity was the old one's in 0; with no buffer, in
198. So the join cannot be fooled by a recycled id while the block it would
join is held, and the identity needs no change.

The test now holds a memory entry of the first memory to the end, as the
kext's wiring does, with a comment saying why. Measured on macOS 27.0.1 (the
M3 Max), 5000 overwrites each: the identity was the old one's in 0 with the
entry held, and in 717 without it.
2026-11-12 08:14:05 -08:00
Anees Iqbal 019d941b54 🆕 Carry compute function pointers to the host
An x86-64 guest's device made no function handle or visible function table:
the bundle's members were RGFX_UNSUPPORTED and the device info held
supportsFunctionPointers at no, so an app took its fallback. The platform
makes them for a compute pipeline (the commit before), and nothing carried
them from the guest to it.

What changes for the guest: the device offers feature bit 39,
FUNCTION_POINTERS (FEATURES_HI bit 7), on every host, and the kext takes it.
Where taken, ring 0 carries FUNCTION_HANDLE_CREATE (0x236: the handle's
ObjId, the pipeline state's, and its function as an RgfxLinkedFunction) and
FUNCTION_TABLE_CREATE (0x237: the table's ObjId, the state's and its entry
count), each answered by FUNCTION_POINTER_STATE (0x40c: the status, and a
table's gpuResourceID, allocatedSize and resourceOptions), and
FUNCTION_TABLE_SET (0x238: a range of a table's entries, 0 a nil entry),
which is not answered and is carried out before any command buffer the
process posts after it. A compute pass binds a table at a buffer index
(RGFX_OP_COMPUTE_SET_VISIBLE_FUNCTION_TABLE, 0x201e) or a range of them
(0x201f), and an argument buffer holds a table by its gpuResourceID. The
bundle's RgfxFunctionHandle and RgfxVisibleFunctionTable answer from the
host's state; a handle of a function the state does not link is nil, as
Metal's is (RGFX_STATUS_NOT_FOUND), and gpuResourceID of a handle and
functionHandleWithName: raise the unrecognized selector the AMD Radeon Pro
5300M and the Intel UHD Graphics 630 raise (measured, macOS 26.7.1). A
table's allocatedSize and resourceOptions are the host's table's, inside the
two Macs' answers. A render pipeline's handles and tables stay unsupported,
with Metal's error. supportsFunctionPointers stays no in this commit: the
next one answers it.

The host knows bit 39 (KNOWN_FEATURES), so a driver's write of it is taken.
The decoder's tests make handles and tables on the fake platform and call
through them on the Vulkan platform; the serializer's, the shell's, the
kext's and the members register's tests cover the guest's side, and
design.md describes the messages.
2026-11-12 08:14:05 -08:00
Anees Iqbal f5030a5f78 🆕 Carry rasterization rate maps to the host
The x86-64 guest's device made no rate map:
newRasterizationRateMapWithDescriptor: was RGFX_UNSUPPORTED, and the device
info held the layer count at 0, so an app that asks for a rate map (Apple's
Modern Rendering sample among them) took its path without one. The platform
makes rate maps (metal2vulkan's runtime compiles Apple's map, rate_map.rs
there), and nothing carried one from the guest to it.

What changes for the guest: the device offers feature bit 38, RATE_MAPS
(FEATURES_HI bit 6), on every host, and the kext takes it. Where taken, ring 0
carries RATE_MAP_CREATE (0x233) and RATE_MAP_RESET (0x234), both
RgfxRateMapCreate (the map's obj_id, the screen size, the private mutability
where set, and the layers' RgfxRateMapLayer records with their samples),
answered by RATE_MAP_STATE (0x40a: the map's screen size, granularity, layer
count, mutability and parameter data's size and alignment), and
RATE_MAP_QUERY (0x235), answered by RATE_MAP_ANSWER (0x40b): a layer's
physical size, a coordinate mapped either way, or a copy of the parameter
data, which the platform writes where a buffer of the sender's lies before it
answers. A render pass names its map after its other records
(RGFX_PASS_FLAG_RATE_MAP, RgfxRenderPassRateMap). The kext admits the three
messages, waits for their answers, and destroys a create it timed out on. The
bundle's map (RgfxRasterizationRateMap) answers the host's state, asks the
host each answer that takes an argument, copies into a Shared buffer of ours
in place and into any other through a scratch buffer of its own, answers the
SPI's mutability, minFactor, resetUsingDescriptor: and formattedDescription:
(the words of Metal's base map, measured on the AMD Radeon Pro 5300M), and
ends the process where Metal's map does: at a copy into a Private buffer, and
at a coordinate map the host refuses (a map of no layers or of an empty
screen, a Mutable map not yet reset). The layer counts stay 0 in this commit:
supportsRasterizationRateMapWithLayerCount: answers no until the device info
answers the platform's.

The core platform's map gains screenSize, which the state answers.

New tests: the decoder's create, reset, queries and copy (decode/rate_map.rs,
on the fake and on Vulkan), a pass's map (cmdbuf.rs), the header's checks of
the new names, the kext's feature, gate and reply types, and the bundle's
messages (serializer_tests), map (shell_tests, its two deaths among them) and
pass record (protocol_tests).
2026-11-12 08:13:36 -08:00
Anees Iqbal 276e4ea3d3 🐛 Join a pages block only over the memory it was given for
00268441a lets a no-copy buffer join a live pages block when its address,
length and writability match. Matching values do not make it the same memory.
An object can outlive the app's hold of the memory under it, through a texture
over the buffer or a command buffer that holds it. In that time the app can
unmap the memory and map other memory at the same address. WindowServer does
this with CoreAnimation's shmem images. The new buffer then joined the old
block, so the GPU read the old pages, which the kext still had wired, while the
app wrote the new ones. In a macOS 26.7.2 guest, System Settings' "About" row
drew the Energy pane's title, clipped, in 3 of 8 pane switches on a Radeon 780M
and 3 of 8 on an RTX 3090. Setup Assistant's labels showed earlier panes'
strings in the same way at a379a5a45; that was not run again.

A block is now joined only where the memory at the bytes is the memory the
block was given for. rgfxPagesIdentity reads, for each VM region the bytes
span, the region's VM object (mach_vm_region_recurse's object_id_full) and the
offset in it. It is read once the kext has wired the block's pages, since
the wire gives a copy-on-write mapping an object of its own. A joinable block
whose bytes now hold other memory is joinable no longer, and the new buffer
takes a block of its own. So do bytes whose memory cannot be identified
(no object yet, a hole). The first refusal in a process logs one info line,
and each refusal logs a debug line.

With the check, no label drew stale in 16 switches on each GPU. In each run,
WindowServer refused joins over one-page images: 28 on the 780M and 15 on the
3090.

For the host, a buffer over remapped memory now names a pages block of its
own in BUFFER_OVER_PAGES, and its GPU reads and writes reach the app's current
memory. The guest's objects, GPU addresses and contents are unchanged.

Tests: the region joins over the same memory mapped again at the same
address and offset, and never over new memory at the same address with other
contents, over the same memory at another offset, or over memory with no pages
yet; a no-copy buffer over memory remapped while the first buffer lives takes
its own block, and a third buffer joins it.
2026-11-12 04:21:35 -08:00
Anees Iqbal fe9a056b7b 🐛 Carry each sampler's LOD bias and reduction mode to the host
A guest's samplers lost lodBias and reductionMode (macOS 26) on the way to
the host: SAMPLER_CREATE's extension, RgfxSamplerCreateExt, was on the wire,
but the bundle never sent it and new_sampler never read it, so every sampler
was made at bias 0 with weighted-average reduction. The guest's state cache
already keyed on both, so two descriptors that differed only there were two
guest samplers of one host shape.

The bundle now sends the extension with every create, on the posted and the
asked route. new_sampler sets the platform's reduction mode and its new LOD
bias from it, and leaves both unset for a create without one, which keeps
Metal's defaults. What the guest sees changes: a sampler of minimum or
maximum reduction samples that way, as Metal's does. The runtime's sampler
descriptor holds no bias yet, so a non-zero bias still samples at 0, and is
now relayed as inexact-sampler-lod-bias, with an accuracy-ledger row, until
the runtime takes one.

The header follows wire.rs's doc, the ROM's record that header (its bytes
are unchanged), and both guest sets are built again from these sources.
2026-11-12 00:04:50 -08:00
Anees Iqbal b22535736b 🐛 Answer Metal's 16 for both IOSurface alignments again
Since 18b642dab the guest's Metal device answered iosurfaceTextureAlignmentBytes
and iosurfaceReadOnlyTextureAlignmentBytes from the host's runtime, 128 on an
NVIDIA GPU and 256 on RADV, so that CoreAnimation would round its backing
stores' rows to what the host takes in place. CoreAnimation also refuses a
Metal texture of any surface whose rows or planes are not at a multiple of
that answer, and the surfaces other code makes keep their own rows. In macOS
26.7.2 guests on an RTX 3090 and an RTX 2080, WindowServer logged "Failed to
allocate MTLTexture from IOSurface ... Row bytes (5312) not 128 aligned" for
rows of 192 to 14272 bytes, and Safari drew pages blank and WebGL black on
every load (appwalk2 walks m3-max-16925, -17008). On a Radeon 780M, at 256, it
found Safari's 1080p and 2160p video frames without the proper alignment.

What the guest observes: the device answers 16 for both again, as Metal's
devices and the IOGPU base do, whatever the host's GPU answers. On RADV the
backing stores' rows go back to 64-byte multiples, so the runtime shadows
those 18b642dab moved in place (1399 textures over a cold boot and a Safari
scroll of macOS 11.7.11 on a 780M).

test_iosurface_alignments_divide_other_codes_surface_rows holds the answer to
the rows WindowServer refused; the member register lists both as Metal's. The
trap is in docs/lessons.md.
2026-11-11 13:09:30 -08:00
Anees Iqbal fedd9e8410 🐛 Make no-copy Managed buffers over app pages Managed on the host
A no-copy Managed buffer over writable app pages was one copy on every host:
the bundle sent its BUFFER_OVER_PAGES with Shared storage, and its
didModifyRange: and a blit's synchronize of it, or of a texture over it, sent
nothing (e828db91b). Apple's OpenCL makes each default cl_mem such a buffer and
binds it to every kernel, so on a host GPU with memory of its own the kernels
read and wrote it across the bus in system memory. CL makes the calls Metal
requires of a Managed buffer: synchronizeResource: before a map, and
didModifyRange: after a map for writing (measured with cl_bw on a macOS 26.6.1
guest on the RTX 5080 host).

The buffer is now a Managed buffer over a Shared one, as the pages slabs'
large Managed buffers are: the BUFFER_OVER_PAGES buffer, still Shared, is its
CPU copy, and a MANAGED_BUFFER_CREATE over it, at an address of its own that
keeps the pointer's offset in its page, is the buffer the app gets. At its
release the Managed buffer goes first, then its CPU copy, then the pages
block. A device that makes no Managed buffer over a Shared one gives such
memory the shadow, as read-only memory has.

What the guest sees: the host's object for the buffer has storageMode Managed,
and the buffer's didModifyRange:, a replaceRegion: of a texture over it, and a
blit's synchronizeResource: or synchronizeTexture: of it or of such a texture
now reach the host. On a host GPU of its own memory the buffer is two copies,
as the AMD 5300M's is: the GPU sees a CPU write only after a didModifyRange:,
and the CPU a GPU write only after a synchronize. On a GPU that shares the
CPU's memory it stays one copy, as the Intel 630's is.

The shell tests that held one copy now hold the two messages, the ranges and
synchronizes sent, the release order, and the shadow where the device makes
no Managed buffer over a Shared one; the serializer tests hold the Managed
create's offset.
2026-11-11 12:20:36 -08:00
Anees Iqbal 00268441aa 🐛 Lay no-copy buffers over the same bytes in one pages block
Each no-copy buffer over the app's own pages took a pages block of its own, so
two buffers over the same bytes had two host views of the same pages, and where
the views' gate refuses their import (a buffer of scattered guest pages at the
stock vm.max_map_count on NVIDIA), the runtime made a copy and a snapshot for
each. Apple's OpenCL makes such buffers: a Shared no-copy buffer over its
staging pages at each clEnqueueWriteBuffer, the same pages each time, while the
one before still lives. On the RTX 5080 host, each 256 MiB of them cost QEMU
512 MiB more Rss per write.

The region now gives a block asked for the same bytes, writability and join
again while it is joinable, so such buffers lie in one view and one copy, as on
a Mac they lie in the same pages. A block stays joinable until each buffer over
it has gone for the app, so a block is never joined over memory the app freed
and took again at the same address, and it goes back to the kext once the host
has dropped every buffer over it. A buffer of Metal's private bit 24, which the
host's runtime takes as read-only memory, joins only its own kind. Nothing
changes for the guest: each buffer keeps its object, GPU address and contents.

Tests: the region's joins and their release order, two no-copy buffers over
one block in the bundle, and the host's one view under two buffers.
2026-11-11 11:38:41 -08:00
Anees Iqbal 18b642dab8 🐛 Pass the host's IOSurface texture alignments to the guest
The guest's Metal device left iosurfaceTextureAlignmentBytes and
iosurfaceReadOnlyTextureAlignmentBytes to the IOGPU base, which answers 16 from
its own state, so the host's answer in the device info never reached the guest.
CoreAnimation rounds the rows of its backing stores' IOSurfaces to
iosurfaceTextureAlignmentBytes (to no less than 64 bytes) and names them to
IOSurface, which keeps them. On RADV, which takes a linear image in place only
at rows a multiple of 256 bytes, those 64- and 128-byte rows are the runtime's
shadows. In a macOS 11.7.11 guest on a Radeon 780M, 1399 textures were shadows
for their pitch over a cold boot and a 15 s Safari scroll, and 11 with the
device answering 512.

The device now answers both from the device info, which carries the Vulkan
platform's answers. What the guest observes: since pin 37 the runtime answers
the device's linear row alignment there, so on RADV (GFX9 to 11) the guest
now sees 256 where it saw the base's 16, and CoreAnimation's backing stores
take rows the host's GPU takes in place; on other GPUs it sees that GPU's
linear row alignment. The device-info doc said an IOSurface's rows follow
none of the device's answers; it now says which answers they follow.
2026-11-11 09:47:42 -08:00
Anees Iqbal 93a1c790d0 🐛 Number the user client's calls where every web sandbox allows them
On macOS 12, Safari's web content process may call only the external
methods its sandbox lists on an IOAccelerator connection: 0-13, 15-18,
22-24, 28-31, 40, 42, 48, 256-265, 267, 268 and 512 in every Safari from
15.1 to 17.6. Its sandbox refused Features (0x10f) before the kext saw it,
and the bundle went on without the kext's features: it took 64-id chunks
where the kext granted 16, and the host refused the textures past each
chunk's first 16 ids (BadResourceId), so pages drew without their body and
text. Under Safari 15 and 16, SurfaceBlockRelease (0x10a) and
SurfaceRetire (0x10d) were refused too. macOS 11, and 13 and later, list
no numbers.

What the guest sees:
- New selector numbers, selector map 1: Features 3, PagesBlock 4,
  TakeAnswers 7, SurfaceBlockRelease 8, SurfaceRetire 9, and a shared
  event port's Export 10 and Import 11. 1, 5 and 6 stay reserved, 2 is
  given to none, and the rest keep their numbers. No number of map 0
  takes a new meaning, so a bundle or kext of map 0 is refused, not
  misrouted.
- Features answers a third word, the selector map, and keeps its number
  in every map. A bundle that meets another map has no Metal device.
- ResourceIds answers the chunk's size after its first id, and the bundle
  takes its chunks at that size.
- Where the bundle's setup left a stubbed device (no features, the device
  info unread or of another version, the context or the user client not
  opened) or returned silently (the IOGPU base declined), the process has
  no Metal device, with a log line that says why. A host's video caps of
  another version still leave the device without video codecs, now
  logged.

user_client_words_test holds the sandbox's set, checks every selector and
the kext's routing against it, and checks that every IOConnectCall*Method
in the bundle's sources names one of them. design.md's registry gives map
1 and what map 0 was. The kext set is built again.
2026-11-08 11:13:13 -08:00
Anees Iqbal a5988ff35e ✨ Answer the x86 Macs' feature profile, 10002, from macOS 13 on
What changes for the guest: the device's featureProfile is 10002 where it was
the host's, 10 on every host so far, wherever the guest's Metal has the
MTLArchitecture class (macOS 13 and later), on every host. Where it lacks the
class (macOS 11 and 12) the device still answers 10001. Both x86 Macs of the
oracle answer 10002 under macOS 26.7.1 (the Radeon Pro 5300M and the UHD 630),
and the guest's Metal keys its limits, families and feature queries on the
profile. 10 was the M3 Max's, kept only because macOS 26's WindowServer, under
10002, asks for tiled textures of 6 and 7 levels over a buffer, which the
device answered nil; the host makes those now (the two commits before), and
26.7.2 composes its desktop under 10002 with them.

The answer is made in one place, RgfxFeatureProfile.hpp, which no longer takes
the host's profile: the bundle stops reading it from the device info, whose
field the host still fills with its Metal device's or the M3 Max's.

The profile's unit test, which held the host's answer from macOS 13 on, holds
10002 there and 10001 below; the guest's kext set is built again with it.
2026-11-08 03:29:59 -08:00
Anees Iqbal d507d7f650 🐛 Answer a feature profile macOS 12's Metal has limits for
macOS 12's Metal keys every limit of a device on its feature profile and has
none for the M3 Max's 10, which the device answered on every guest: it left
the limits 0 and ended WindowServer at device creation, so 12.x guests never
reached a desktop (launchd gave up after 14 runs).

The device now answers the host's profile only where the guest's Metal knows
it, told by what that Metal has: the MTLArchitecture class, which macOS 13
and later have (13.7.8, 14.8.9 and 26.7.2 boot with 10). Elsewhere (macOS 11
and 12) it answers 10001, as 12's drivers for the 5300M and the UHD 630 do.
26 keeps 10 rather than the x86 Macs' 10002: under 10002 its WindowServer
still asks for a mipmapped tiled texture over a buffer, which the device
refuses (dgpu-oracle.md, Q11, rechecked).

What changes for the guest: featureProfile on macOS 11 and 12 is 10001; on
13 to 26 it is unchanged.
2026-11-08 01:57:20 -08:00
Anees Iqbal 6fb33ecd27 🎨 Rename the reims-graphics crate to reims-vgpu-graphics
The crate that holds the reims-graphics device's wire and transport now
carries the library's prefix, as reims-vgpu-video does: its directory is
crates/reims-vgpu-graphics, its package reims-vgpu-graphics, and its Rust
path and log target reims_vgpu_graphics. The guest's, the option ROM's and
Ambrosia's Makefiles include the wire header from the new directory, and the
prebuilt records name it there.

Nothing changes for the guest or the host. The device keeps its name
(reims-graphics, QEMU's reims-graphics-pci, PCI 1234:5247), and the wire
header keeps its file name and RGFX_ symbols. The C API header is unchanged,
and the prebuilt kexts and option ROM are byte-identical; only the records'
paths and source hashes move. A log filter names the crate's lines by its
new target (REIMS_VGPU_LOG=info,reims_vgpu_graphics=trace), a diagnostic.
2026-11-08 01:37:50 -08:00
Anees Iqbal 7936a73a63 🐛 Answer the host GPU's vendor as the device's vendorName
The device answered the IOGPU base's vendorName, "Apple", beside the Mac2
family alone, a pair no Mac has. The system's GL renderer
(AppleMetalOpenGLRenderer) picks its settings by finding "AMD" or "ATI",
"NVIDIA" or "Intel" in the vendor name, else by the Apple GPU families 7 and
5 (read from its code). On any other device it ends every process that makes
a GL context ("Unknown GPU!!", measured: macOS 26.6.1, RTX 5080 host).

The answer is now the vendor the device name's first word names, in the
words the Macs answer: "NVIDIA", "AMD" (the AMD 5300M's) or "Intel(R)" (the
Intel 630's, measured: macOS 26.7.1). A name of any other vendor answers
"AMD", the x86 Macs' discrete GPUs' vendor, since the renderer knows no
other. The Macs diverge here (class 2); the register says so.
2026-11-07 21:36:20 -08:00
Anees Iqbal ef9140c2d8 🐛 Send the decode's output_rows the guest's encoder was clearing
RgfxEncoder zeroed the field the decode ops used to keep reserved, which
is output_rows now: the rows the output surface holds go to the host as
the video driver set them. The serializer test checks they arrive.
2026-11-07 15:06:55 -08:00
Anees Iqbal a227a9bad6 ✅ Check the commit order's cycles only on interleaved posts
shell_tests' check count changed between runs of one tree (262756 to
296306) because of test_commit_order_names_no_cycle. Its two threads often
ran one after the other, so no post named the other queue, and its cycle
check checked nothing (40002 checks in a run, against 72352 in another,
m3-max-5134). The threads now start together, and a run where either queue
names nothing runs again, up to 50 times, before the test fails.

The check counter is atomic: CHECK runs on the test's threads, and the
plain int could lose counts there.
2026-11-07 13:26:52 -08:00
Anees Iqbal 402b7463a9 🐎 Restore the write-combined spans: they take, and the encode was theirs
Reverts 0825b1ea2, restoring 0afc30f37, e8d04b187 and ea13ccbca. A write-combined
mapping of the region does take: the region's descriptor is physically
contiguous, and xnu's fault path maps such an object's pages in its own cache
mode (vm_fault's pmap_map_block_addr, read from the public xnu source), which
the earlier reading missed. The page-table probe that seemed to agree saw only
the processes on the CPUs at its instant, not the bench.

Measured on the Radeon 780M host, draws.10k's encode: 9.2-9.5 ms at the base,
7.7 ms with the slabs in the process's own pages alone (936e3968d's set), and
1.05-1.3 ms with the spans write-combined as well (0afc30f37's and ea13ccbca's
sets). The kext's comment and the design doc say why, and docs/lessons.md the
probe's trap.

For the guest, as 0afc30f37: a queue's submission ring starts 3840 bytes into
its block, the kext's Alloc takes a third input and its feature word holds
1 << 7.
2026-11-07 08:27:28 -08:00
Anees Iqbal c08c19f377 🔥 Drop the write-combined spans: no user mapping of the region can be
Reverts 0afc30f37, e8d04b187 and ea13ccbca. On x86-64 xnu every mapping of
the region in a guest process is uncached, whatever cache mode the kext asks:
vm_fault enters each page with no cache flags, and the pmap gives a page it
does not manage PCD alone, the guest PAT's UC- (read from the public xnu
source: pmap_get_cache_attributes and vm_fault's pmap_enter wrappers). Measured
on the Radeon 780M host with both the region's own descriptor and one of the
write-combined spans' own: every page of a process's over BAR 2 was PCD alone,
none PAT and PCD, and draws.10k encoded as fast as with the spans write-back.

For the guest, a queue's submission ring starts its block again, the kext's
Alloc takes two inputs and its feature word drops 1 << 7; the slabs in the
process's own pages (72c09eddc) carry the measured gains. The design doc says
why the region is uncached, as measured.
2026-11-07 08:27:28 -08:00
Anees Iqbal b396a9b33a ✅ Refill the write-combined test's memory before its second region
The test's first region zeroed its rings in the same memory, so the second region's
check that its ring block's head is untouched read those zeros.
2026-11-07 08:27:28 -08:00
Anees Iqbal fac9e06ca7 🐎 Map the bytes the guest only writes write-combined
The guest's kernel maps the device region uncached whatever the kext asks:
xnu's pmap sets PCD on every page it does not manage, and the region is no RAM
of the guest's. An AMD host applies the guest's memory type (Intel's EPT
ignores it), so on the Radeon 780M host the guest's stores there ran at
13 MB/s and draws.10k spent 9.1 ms encoding against 0.5 ms on the RTX 5080.

The kext's Alloc now takes a third input, the byte from which the process's
mapping of the block is write-combined (kIOMapWriteCombineCache, PAT index 6),
behind a kext feature bit. The bundle puts there what it only writes: a
submission ring's data, which now starts its block's second page with the
ring's header ending the first, write-back as before, since the host writes
it too and the writer reads it; command buffers' bodies; and a library's
staging chunks, past the page of acks it reads. The ring writer fences its
stores (sfence) before it publishes a message, so write-combined stores reach
the host before the producer index that names them, as a release store
orders write-back ones. ICB slots stay write-back: their setters read fields
back and OR bits in with locked operations, which write-combined memory does
not keep atomic.

For the guest, a queue's submission ring now starts 3840 bytes into its block
where the kext has the feature; the wire is unchanged.
2026-11-07 08:27:28 -08:00
Anees Iqbal 11672a2a5f 🐛 Carry only the options the host takes on heap and buffer creates
The GL renderer makes its no-copy buffers with resource option bit 20
(0x100000), a hint for the device's driver. The bundle passed every bit
to the host, and the host's runtime refuses a buffer with a bit Metal
gives no meaning, so each such buffer was refused (measured: macOS 26.6.1
on the RTX 5080 host).

Heap and buffer creates now carry only the CPU cache, storage and hazard
tracking modes, and Metal's private bit 24, which the host takes as Metal
does.

A buffer outside a heap now answers resourceOptions with its three modes
alone. On macOS 26.7.1 the AMD 5300M and the Intel 630 drop each of bits
10 to 31 from the answer, for length and no-copy buffers alike: they give
0x200, 0x210 and 0x220 for Shared, Managed and Private.
2026-11-07 06:57:51 -08:00
Anees Iqbal 77f12120d4 🐛 Answer nil for a texture with no GPU address left
A device-placed texture and a Managed texture's GPU copy take a GPU address
of the context's range, which runs out at address-space exhaustion. Their
creates took the address unchecked and posted 0, where a heap, a buffer and
every other create that picks an address answer no object.

What changes for the guest: at exhaustion such a texture is nil, as Metal
answers one it cannot place, and nothing is posted; its resource id goes back
for the next texture. A texture in a heap takes no address and is unchanged.

Test: test_creates_with_no_address_post_nothing fails at the base, where both
creates post with address 0.
2026-11-05 21:59:36 -08:00
Anees Iqbal 7defb7f533 🆕 Make mesh render pipelines and draw them, as an x86 Mac's Metal
For the guest: where the device takes MESH_PIPELINES, every
newRenderPipelineStateWithMeshDescriptor: form makes a mesh render pipeline. It
used to answer nil and an unsupported error. The render encoders send the object
and mesh stages' bindings and drawMeshThreadgroups:, drawMeshThreads: and the
indirect form, and the SPI's supportsMeshShaders answers YES. Without the
feature they answer as before. The state answers its object and mesh limits as
the host's platform answers them (maxTotalThreadgroupsPerMeshGrid NSUIntegerMax
past 32 bits) and the threadgroup sizes its descriptor required.

Measured with guest/bundle/tools/oracle/oracle_mesh, plain Metal, on the AMD
Radeon Pro 5300M and the Intel UHD Graphics 630 (macOS 26.7.1, 25G241) and the
M3 Max (macOS 27.0.1, 26A434), the creators now end the process where the x86
Macs' Metal does, with their words:
- supportIndirectCommandBuffers YES, on the 5300M and the 630 (the M3 Max makes
  it): "validateWithDevice:1884: failed assertion `Mesh Render Pipeline
  Descriptor Validation\nUse of mesh shader pipelines in indirect command
  buffers is not supported by this device.\n'"
- no mesh function, on all three: "...\nmeshFunction must not be nil.\n'"
- no fragment function while rasterizing, on all three: "...\nfragmentFunction
  must not be nil.\n'"
So no mesh pipeline supports indirect command buffers, and indirect commands
carry no mesh draw. This matches the families: supportsFamily: answers no for
Apple7 to Apple9 on the x86 Macs and on the device, and the SPI's
supportsMeshShadersInICB answers NO on the x86 Macs and the device (YES on the
M3 Max).

They refuse before the host is asked, in the x86 Macs' compiler's domain
(CompilerError):
- a payload past 16384 bytes, code 2, "payloadMemoryLength property of
  MTLMeshRenderPipelineDescriptor (16385 bytes) exceeds maximum supported by
  this device (16384 bytes)";
- a stage maximum past the device's 1024 threads, code 2, the 5300M's
  "maxThreadsPerThreadgroup pipeline descriptor value (2048) exceeds maximum
  supported (1024)\n" (the 630 makes it, the M3 Max refuses it);
- a vertex function as the mesh function, code 2, "RasterizationEnabled is
  false but the vertex shader's return type is not void";
- any other function of another stage, code 1. The x86 Macs answer the
  crashed compiler's XPC words; ours are the validation layer's;
- a mesh function as the object function, code 1, which the x86 Macs make
  and the M3 Max refuses;
- an archive miss under MTLPipelineOptionFailOnBinaryArchiveMiss, code 2,
  "Unable to find function \"f_col\" in binary archives. \n ". It names the
  fragment function, else the mesh function. An x86 archive holds no mesh
  pipeline, so every create naming an archive misses.

The creators with a reflection answer RgfxMeshPipelineReflectionOf's, the
reflection of the pipeline's functions, as the render pipeline creators answer
theirs.
2026-11-05 21:59:35 -08:00