Files
geramyloveless 4968adb4de VRAM only through the kernel: no CPU access through a BAR mapping
Unplugging the GPU with the display live panicked the Mac on 239 and 240
(LLC bus error, NC write, in the dext). On 240 the panicked thread was in
an atomic commit powering the display down after the agent's framebuffers
were removed: dmub_srv_fb_cmd_execute, with dmub_rb_push_front having
stored the command into DMUB's inbox in VRAM one byte at a time through
the dext's user-space mapping of BAR0. The removal was already under way
(Stop, removal_main, the aperture gate set); the error is reported only
after the store has retired, so no check before a store can prevent it.
Register accesses, through the kernel's IOPCIDevice MemoryRead/Write, ran
in the same commit after the link went down and never faulted.

So every access to the VRAM aperture goes through the kernel:
rt/aperture.h's read/write/copy/fill route through ops the dext installs
with the PCI device (BAR0 MemoryRead/MemoryWrite 8 to 64 bits, BAR0's
index and size read once). linux/io.h's readX/writeX, memcpy_toio,
memcpy_fromio and memset_io, the 64-bit MMIO atomics and the string
functions (memcpy, memset, memmove, memcmp) route aperture addresses
through them; the gate still skips writes and reads all ones once the
device is known gone. The GART page table in VRAM, written with writeq on
every GTT bind, now goes this way too.

dmub_cmd.h's inline dmub_rb_push_front (a byte loop) and
dmub_rb_flush_pending (READ_ONCE) reach the inbox with plain pointer
accesses no shim can route. A declared compile intervention for
dmub_srv.c (rt/dmub_inbox.h) includes dmub_cmd.h first and redirects its
two calls to the same operations written with dmub_memcpy, as upstream's
dmub_rb_out_push_front copies. Upstream sources stay unchanged.

Offline proof: the CS fixture gives the driver VRAM only as a no-access
alias of its pages, reached through the aperture's ops; the host
library's upstream objects alias the string functions as the dext's do.
Any direct load or store through a kernel VRAM pointer now crashes the
offline tests. With the two DMUB helpers redirected, every test passes:
the display pipeline (with its removal check now on the fixture's real
aperture), scanout, surface import, CS selftest, KFD and the rest.
2026-10-04 17:48:52 -07:00

46 lines
1.5 KiB
C

/* Kernel buffers may be uncached PCI mappings. Darwin's libc string routines
* assume normal memory (large bzero uses DC ZVA), unlike these mappings.
* Include before SDK string macros in every DriverKit KMD translation unit. */
#ifndef LINUXU_DEVICE_STRING_H
#define LINUXU_DEVICE_STRING_H
#include <string.h>
#include <stddef.h>
#include <stdint.h>
#include <rt/aperture.h>
#ifdef __cplusplus
extern "C" {
#endif
void *linuxu_device_memset(void *, int, size_t);
void *linuxu_device_memcpy(void *, const void *, size_t);
void *linuxu_device_memmove(void *, const void *, size_t);
int linuxu_device_memcmp(const void *, const void *, size_t);
void linuxu_device_bzero(void *, size_t);
/* The VRAM aperture: rt/aperture.h. */
#ifdef __cplusplus
}
#endif
/* Object-like aliases also cover function pointers. Explicit builtins occur
* in upstream SMU headers; they must obey the same device-memory contract.
* Host builds of the driver alias them too (LINUXU_DEVICE_STRING_ALIASES),
* so the offline tests route aperture accesses the way the dext does. */
#if defined(LINUXU_DEXT_DK) || defined(LINUXU_DEVICE_STRING_ALIASES)
#undef memset
#undef memcpy
#undef memmove
#undef memcmp
#undef bzero
#define memset linuxu_device_memset
#define memcpy linuxu_device_memcpy
#define memmove linuxu_device_memmove
#define memcmp linuxu_device_memcmp
#define bzero linuxu_device_bzero
#define __builtin_memset linuxu_device_memset
#define __builtin_memcpy linuxu_device_memcpy
#define __builtin_memmove linuxu_device_memmove
#define __builtin_memcmp linuxu_device_memcmp
#endif
#endif