notes

The framebuffer contract

Milestone 29, rung one of the display ladder. The contract a client speaks to get pixels on a screen, written down so rung two (the compositor, milestone 33) implements against a contract instead of inventing one. The code half is crates/graphics_contract; this is the prose half, the same split notes/fs-server.md makes for the filesystem and notes/terminal-contract.md for the terminal. The evidence behind it is in framebuffer-contract/.

The shape

  virtio-gpu ──virtio (PCIe, behind the IOMMU)──► display driver ──display IPC──► client
       │                                              │  (display)                  (painter)
       └──── DMA: the whole region ──────────────────► │
                                   the surface ────── shared frames ──────────────┘

Three parties. The device, on the PCIe bus. The display driver (components/src/gpu_driver.rs), which owns the device and serves the contract. The client (fixtures/src/painter.rs), which owns the pixels and has never heard of virtio-gpu.

What each holds is the whole design:

display driver client
slot 0 report endpoint (status to its spawner) report endpoint
slot 1 Interrupt, the device's completion interrupt the display endpoint, WRITE (it CALLs)
slot 2 VirtualIo, the confined transport
slot 3 the display endpoint, READ (it serves)
mapped the whole DMA region (rings + surface) the surface frames only
knows its DMA region's physical base no physical address at all

The client cannot program a queue, cannot ring a doorbell, cannot see a descriptor ring (they are in a page it is not mapped), and cannot name a physical address. So the worst a hostile client can do is draw nonsense, which is exactly the authority a thing that draws should have.

That separation, not the picture, is why this increment exists. A compositor is the canonical multiplexer of one device among mutually distrusting clients, and it can only be that if the thing that draws is provably not the thing that talks to the hardware. Rung two takes the client's place here unchanged.

Control by message, pixels by shared frame

DECISIONS §10's division, third instance (after blk IPC and file IPC). A request is an endpoint CALL carrying an opcode and a rectangle; the pixels never ride in a message. They live in frames the kernel maps into both address spaces at spawn, which the client may write at any time without asking anyone.

Two verbs, and that is the whole surface today:

A rectangle outside the surface is refused, not clamped. A clamp hides a client's coordinate bug, and the client is the only party that can tell the difference. EINVAL (-22), the same negative-is-an-error convention file_system_contract sets.

Why two device commands per flush, in this order: TRANSFER_TO_HOST_2D is what reads the guest pixels (the device DMAs them out of the backing into its own host-side image), and RESOURCE_FLUSH is what makes the host show that image. A driver that only flushed would put stale pixels on the screen.

The memory story: a framebuffer does not fit in a page

Every other driver here gets one 4 KiB DMA page. A 128x64 surface at 4 bytes a pixel is 32 KiB, so the GPU needs eight more. The tempting shortcut is to let the framebuffer live outside the DMA region: it is "pixels", after all. That would put the one device that reads bulk memory outside the confinement everything else is inside, which is exactly backwards.

So the region is wider, not special:

  page 0        rings (queue 0 at 0x000) + control request (0x400) + response (0x600)   driver-private
  pages 1..8    the surface, 32 KiB of pixels                                           shared with the client

1 + graphics_contract::SURFACE_FRAMES contiguous frames, allocated with alloc_contiguous and registered whole as the driver's DMA region. Three things follow, and they are the reason this is the right shape:

  1. The shadow-ring validator bounds every descriptor to [direct_memory_access_base, direct_memory_access_base + direct_memory_access_size), and the surface is inside that. crates/direct_memory_access_validator needed no change at all, because it bounds addresses against a region whose size is a parameter; the region got nine times bigger and the proof still covers it. (This was the one place the increment could have tempted a change to a proved crate, and it did not.)
  2. iommu::confine maps exactly that region plus the kernel's shadow page, frame-granular. The device can reach the pixels and nothing else.
  3. The client maps only pages 1..8, so it cannot touch a descriptor ring even though it shares memory with a driver.

The block server already took a two-page region this way for whole-block reads (milestone 32). This is the same move, wider, and it is worth stating as a general rule: a device that needs more memory gets a bigger grant, never an exemption.

The backing is one virtio-gpu memory entry rather than eight, because the frames are contiguous. That is not required (the command takes a list) but it keeps the domain a single range and the entry list trivial.

The confinement hazard a GPU adds, and what actually stops it

This is the part worth reading twice, because it is the first device here whose DMA addresses do not all arrive in descriptors.

Everywhere else, every address a device will touch arrives in a virtqueue descriptor. The kernel validates each one and copies it into a shadow ring the driver physically cannot write, so the bytes the device acts on are the bytes the kernel checked (notes/dma.md). A virtio-gpu's backing addresses arrive somewhere else: inside a RESOURCE_ATTACH_BACKING command payload. The kernel bounds the descriptor that carries that command, but the addresses inside it are bytes it does not parse.

And it should not start parsing them. Teaching the transport to read virtio-gpu commands would put device knowledge in the one place DECISIONS §18 (the PCIe transport) keeps device-neutral. It would be a per-device arms race: the next device class with addresses in a payload would need its own parser in the kernel.

So the IOMMU is the barrier for this class of address, and that is proven, not assumed. the_iommu_refuses_the_gpu_a_framebuffer_outside_the_drivers_grant gives a driver exactly the honest driver's authority. It points the resource's backing at a frame the kernel deliberately left out of its domain, asks the device to transfer from it, and asserts the IOMMU recorded a fault at that frame. Both ISAs.

Two consequences follow, both stated plainly because they matter:

Two things the escape test found on the way. The device's "OK" is not evidence: QEMU hands a device a bounce buffer on a translation failure, so the command succeeds while the confinement holds. The test therefore asserts on the IOMMU's fault queue. And the RISC-V IOMMU's fault queue (128 records, FQ_LOG2 = 7) overflows silently: the driver never clears the overflow bit, so a flood of faults stops all later fault recording. The escape now attaches four bytes and drains the queue after. Clearing the bit and routing faults is left for a fault-handling milestone (DECISIONS §20 (IOMMU-backed DMA isolation)). Both are in framebuffer-contract/iommu-escape-test.md.

What the test proves, and the one thing it does not

a_confined_userspace_driver_puts_a_known_pattern_in_a_framebuffer, one arch-neutral test that runs on both ISAs (everything under it is portable: same two binaries in both archives, same PCIe seam on both boards, one host-tested contract crate).

Proven:

Not proven by the in-guest test: the scanout. The suite runs -display none, and nothing inside the guest can read QEMU's host-side surface back, so "the bytes we handed the device are the bytes it read out of our frames" is as far as an in-guest test reaches. A wrong pixel format or a wrong scanout rectangle would pass it and show garbage on a real screen.

Proving the scanout, from the host

That gap is closed, and by the host rather than the guest, because only the host can see the pixels.

QEMU's monitor works headlessly: screendump FILE writes a PPM of the scanout even with -display none (verified against QEMU 11.0.2). So the runners take a monitor socket (NIFE_GPU_MON), and cargo xtask's cargo_test_with_scanout_check drives it while the ordinary test run is happening. It spawns the suite, and beside it polls the monitor every 100 ms, dumps the scanout, and compares the PPM against graphics_contract::pixel, the same definition the client painted from. The first match ends the polling. Both ISAs. Every command goes over one monitor connection and waits for QEMU's prompt, so a dump is whole when it is read. On success it prints:

scanout check (aarch64): the 128x64 pattern reached the DEVICE's scanout, verified pixel for pixel
against graphics_contract::pixel (target/gpu-scanout-aarch64.ppm)

So the pixels are proven all the way to the device, not just to our own frames. The guest's two witnesses agree about the framebuffer, and the host independently confirms that what QEMU is scanning out is the pattern, pixel for pixel, in the right format, at the right geometry.

The check runs inside the existing test run, the 128x64 geometry is part of the assertion, and the checker has a negative control that must reject a red/blue swap, a one-row shift and a single wrong pixel. Test order is load-bearing: the confinement test resets the device, so it runs first, and a changed order fails loudly. Those details and two practical constraints are in framebuffer-contract/scanout-referee.md.

What is still not proven, to be exact about it: that a physical panel would show this. QEMU's scanout is the last thing we can observe, and on real hardware there is a display controller past it. That is a silicon question (notes/target-hardware.md), not a QEMU one.

A false lead, resolved

On 2026-08-26 a grown 1280x720 scanout looked like a QEMU resize bug. It was a guest hang: three programs still looped over per-page capability slots that DECISIONS §102 (a Frame names a run of pages) had removed, so the driver died before its "UP" report. The investigation, a false lead, is in framebuffer-contract/scanout-referee.md.

What the next clients did with this contract

Rung two's compositor (milestone 33 (a compositor)) and the display terminal (milestone 29 (a display terminal)'s text increment) each took painter's place at this seam with exactly painter's authority. crates/graphics_contract and gpu_driver stayed byte-for-byte the same both times. A kernel stand-in for the driver made the damage rectangle observable. The scanout check grew to three pictures over one boot. How each prediction below came out is in framebuffer-contract/later-rungs.md.

The seams left open

Deliberately not in rung one, each with the seam it will use:

BUGS

Every spawner of gpu_driver must receive FLUSHED, or the driver serves exactly one flush. FLUSHED is a blocking SEND the driver makes from inside its serving loop, right after replying to its first flush, and while it waits there nothing is in RECEIVE on the display endpoint. The three test spawners read it because they want the digest; the boot now reads it because it must. Nothing enforces this: display_service::start_terminal's doc says so and names it a foot gun, because the tests need the digest and the wiring function therefore cannot swallow it for them.

RESOLVED 2026-10-12 (lane/scanout-referee-flake): the referee missed pictures because QEMU refused its dumps. The monitor socket serves one client with a listen backlog of one, and the referee opened a connection for sendkey and another for screendump microseconds apart without reading either reply. On macOS the second connect was refused while the first sat in the backlog, so most polls took no dump; one loaded riscv64 run went 25 polls (2.6 s) without one, against a picture held for 3 s. That is the failure seen on 2026-10-12 (HVF) and 2026-10-13 (riscv64, which PR #2047 recorded as an unconfirmed reading). The referee now holds one connection and waits for QEMU's prompt after each command, and an unmatched picture says whether it was ever looked for. framebuffer-contract/scanout-referee.md has the runs.

The red report still opens "the compositor test passed" for every unmatched picture, though the referee cannot know that: when the guest panicked first, that test may never have run. Read the guest's verdict above the scanout lines before the scanout lines.

RESOLVED 2026-09-19 (milestone 177): the second FLUSH on the real boot never returned because nobody received the driver's FLUSHED report. The terminal's first flush came before its TERM_UP, so the driver was already blocked sending FLUSHED when the boot stopped listening. The keyboard had the identical hang one step later, and keyboard_service::start_direct now takes its report itself. The boot evidence is in framebuffer-contract/flushed-hang.md.

Where the pieces are

piece file
the contract, host-tested crates/graphics_contract/src/lib.rs
the display driver components/src/gpu_driver.rs
the second driver: a screen the firmware already set up (milestone 400) components/src/framebuffer_driver.rs, its arithmetic in screen_console::Aperture
the client that draws fixtures/src/painter.rs
enumeration kernel/src/pci.rs (find_gpu_device)
the spawn wiring kernel/src/user/display_service.rs
the tests system_tests/src/user/display_tests.rs
the device lines helpers/qemu-runner-aarch64.sh, helpers/qemu-runner-riscv64.sh (NIFE_GPU)