🎨 Write arm64 device accesses out as plain loads and stores

A volatile access fixes an access's size and count, not its instruction, and
clang picks writeback forms: the Ambrosia module's BarSize read ECAM with a
pre-indexed LDR. A hypervisor decodes an access to emulated device memory from
its abort's syndrome, which an arm64 CPU gives only for a single load or store
without writeback. A pre- or post-indexed, pair or SIMD&FP access aborts with
ISV 0, and KVM hands it to QEMU, which decodes it only with its own instruction
emulator (vendor/qemu 89e32906, 3892a92f). Ambrosia's own firmware made the same
change.

The module's Read16, Write16, Read32, Write32, Read64 and Write64 become inline
asm: one LDRH, STRH, LDR or STR at [Xn], with a "memory" clobber; the barriers
stay as they are. In the arm64e kext, the only accesses to BAR 0 are
ReimsGraphics' and RgfxAccelerator's readRegister and writeRegister. They now
go through rgfxRegisterRead32 and rgfxRegisterWrite32, which on arm64 are one
LDR or STR of a W register at [Xn]. RgfxGate makes no device access. BAR 2, the
region, is RAM to QEMU (memory_region_init_ram_ptr), so the rings' accesses
never reach it. x86-64 has no such limit and keeps the volatile accesses.

No behaviour changes for the device: each access keeps its size, its count and
its order.

Checked on m3-max (m3-max-14564, Apple clang 21.0.0), before and after:
- `clang -S` of each translation unit with its Makefile's flags, listing every
  inline-asm instruction (between `//APP` and `//NO_APP`, or `; InlineAsm
  Start` and `End`);
- `llvm-objdump -d` of the module's ELF, listing its loads and stores whose
  base is not sp that write back, pair or move a SIMD&FP register;
- `llvm-objdump -d --disassemble-symbols=` of the kext's four accessors.
Module before: `ldr w9, [x8, #0x10]!` (BarSize's ECAM read) is among the
ELF's 20 such forms. After: its 34 device accesses are all inline asm, each
`ldrh`, `strh`, `ldr` or `str` at `[xN]`. None of the ELF's 16 remaining such
forms is one of them. Kext after: ReimsGraphics.cpp's 27 and
RgfxAccelerator.cpp's 13 register accesses are each `ldr wN, [xM]` or
`str wN, [xM]`. The accessors
built `ldr w0, [x8, w1, uxtw]` and `str w2, [x8, w1, uxtw]` before, and
`ldr w0, [x8]` and `str w2, [x8]` after. x86-64: ReimsGraphics.o and
RgfxAccelerator.o disassemble the same before and after, and the rebuilt
x86-64 set's three binaries are byte for byte the committed ones.
This commit is contained in:
Anees Iqbal committed 2026-11-11 03:01:04 -08:00
1 parent d73aec66cd
commit 8e7e0aa713
4 files changed
+60 -10

No files matched your search

+27 -6
View File
@@ -119,12 +119,27 @@ typedef struct {
/* ------ Device memory, barriers and the counter ------ */
//
// Each access to the device's memory is one instruction: a plain LDRH, STRH,
// LDR or STR at a base register's address, written out. A hypervisor that
// emulates the device's registers or its configuration space decodes a
// guest's access from its abort's syndrome, which the CPU gives only for a
// single load or store without writeback. A pre- or post-indexed, pair or
// SIMD&FP access aborts with no syndrome (ISV 0), and KVM hands that to QEMU,
// which decodes it only with its own instruction emulator. A volatile access
// fixes the access's size and count, not the instruction the compiler picks
// (BarSize's read of ECAM compiled to a pre-indexed LDR).
//
static inline uint16_t
Read16 (
uint64_t Address
)
{
return *(volatile uint16_t *)(uintptr_t)Address;
uint16_t Value;
__asm__ volatile ("ldrh %w0, [%1]" : "=r" (Value) : "r" (Address) : "memory");
return Value;
}
static inline void
@@ -133,7 +148,7 @@ Write16 (
uint16_t Value
)
{
*(volatile uint16_t *)(uintptr_t)Address = Value;
__asm__ volatile ("strh %w0, [%1]" : : "rZ" (Value), "r" (Address) : "memory");
}
static inline uint32_t
@@ -141,7 +156,10 @@ Read32 (
uint64_t Address
)
{
return *(volatile uint32_t *)(uintptr_t)Address;
uint32_t Value;
__asm__ volatile ("ldr %w0, [%1]" : "=r" (Value) : "r" (Address) : "memory");
return Value;
}
static inline void
@@ -150,7 +168,7 @@ Write32 (
uint32_t Value
)
{
*(volatile uint32_t *)(uintptr_t)Address = Value;
__asm__ volatile ("str %w0, [%1]" : : "rZ" (Value), "r" (Address) : "memory");
}
static inline uint64_t
@@ -158,7 +176,10 @@ Read64 (
uint64_t Address
)
{
return *(volatile uint64_t *)(uintptr_t)Address;
uint64_t Value;
__asm__ volatile ("ldr %x0, [%1]" : "=r" (Value) : "r" (Address) : "memory");
return Value;
}
static inline void
@@ -167,7 +188,7 @@ Write64 (
uint64_t Value
)
{
*(volatile uint64_t *)(uintptr_t)Address = Value;
__asm__ volatile ("str %x0, [%1]" : : "rZ" (Value), "r" (Address) : "memory");
}
/**
+2 -2
View File
@@ -190,13 +190,13 @@ inline UInt64 clockAbsolute(UInt64 ns)
UInt32 ReimsGraphics::readRegister(UInt32 offset) const
{
return *reinterpret_cast<const volatile UInt32 *>(registers + offset);
return rgfxRegisterRead32(registers + offset);
}
void ReimsGraphics::writeRegister(UInt32 offset, UInt32 value)
{
rgfxRegisterWriteBarrier();
*reinterpret_cast<volatile UInt32 *>(registers + offset) = value;
rgfxRegisterWrite32(registers + offset, value);
}
volatile UInt8 *ReimsGraphics::ring(UInt32 offset) const
+2 -2
View File
@@ -291,13 +291,13 @@ void RgfxAccelerator::settleBundle()
UInt32 RgfxAccelerator::readRegister(UInt32 offset) const
{
return *reinterpret_cast<const volatile UInt32 *>(registers + offset);
return rgfxRegisterRead32(registers + offset);
}
void RgfxAccelerator::writeRegister(UInt32 offset, UInt32 value)
{
rgfxRegisterWriteBarrier();
*reinterpret_cast<volatile UInt32 *>(registers + offset) = value;
rgfxRegisterWrite32(registers + offset, value);
}
uint64_t RgfxAccelerator::featureWord() const
+29
View File
@@ -50,4 +50,33 @@ inline void rgfxRegisterWriteBarrier()
#endif
}
// A 32-bit load or store of the device's registers (BAR 0), each one instruction. On arm64 a
// hypervisor that emulates the registers decodes a guest's access to them from its abort's
// syndrome, which the CPU gives only for a single load or store without writeback; a pre- or
// post-indexed, pair or SIMD&FP access aborts with no syndrome (ISV 0), and KVM hands that to
// QEMU, which decodes it only with its own instruction emulator. A volatile access fixes the
// access's size and count, not the instruction the compiler picks, so on arm64 each is written
// out: an LDR or STR of a W register at a base register's address, as Linux's readl and writel
// are. x86-64 has no such limit and keeps the volatile access. BAR 2, the region, is RAM to the
// hypervisor (QEMU's reims-graphics-pci maps it so), and its accesses do not reach it.
inline uint32_t rgfxRegisterRead32(const volatile uint8_t *at)
{
#if defined(__aarch64__)
uint32_t value;
__asm__ volatile("ldr %w0, [%1]" : "=r"(value) : "r"(at) : "memory");
return value;
#else
return *reinterpret_cast<const volatile uint32_t *>(at);
#endif
}
inline void rgfxRegisterWrite32(volatile uint8_t *at, uint32_t value)
{
#if defined(__aarch64__)
__asm__ volatile("str %w0, [%1]" : : "rZ"(value), "r"(at) : "memory");
#else
*reinterpret_cast<volatile uint32_t *>(at) = value;
#endif
}
#endif // RGFX_REGION_MAPPING_HPP_