🆕 Give arm64 VMs audio, on virtio sound devices

reims-ctl gave an arm64 VM no sound device, whatever its [audio] setting,
so a macOS guest found no output device. It now gives an arm64 VM with
audio on the same two virtio-sound-pci devices on the VM's backend as an
x86-64 VM (an output stream alone and an input stream alone), after the
GPU where QEMU puts them, and no HDA. The q35 devices keep their slots;
both machines make them through one function.

macOS 27's virtio PCI transport never started such a device: it spins on a
capability list QEMU links from high offsets to low, and a boot could stop
there. vendor/qemu moves to 5c42f3fce4, which links virtio-sound-pci's
capabilities in address order on vmapple, as 4a39ef8d54 does for
virtio-net-pci. That commit is steelbrain/qemu-reims-vgpu#16, which must
merge first.

On a macOS 27.2.1 guest under KVM on an M1 Pro, both devices reach
DRIVER_OK, Core Audio lists the default output and input, and afplay plays
on the host's speakers (docs/qemu.md, "arm64 guests: audio"). Refs #167.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vintowareandClaude Opus 5.5 authored and Anees Iqbal committed 2026-11-23 21:06:12 -08:00
1 parent 9c91714b60
commit 78c2202380
3 files changed
+123 -18

No files matched your search

+91 -16
View File
@@ -95,7 +95,11 @@
//! the tickets of its macOS are bound to. It runs on HVF on a Mac, with the GIC in the kernel, on
//! KVM on arm64 Linux, and on TCG, a thread for each vCPU, on x86-64 ([`Profile::accel_arg`]). On
//! Linux its RAM is a shared memfd, as the library there needs, not allocated as QEMU starts.
//! It takes no host USB device or disk, no VMware SVGA and no sound device.
//! It takes no host USB device or disk and no VMware SVGA. With audio on, it has the same two
//! virtio sound devices as an x86-64 VM, after the GPU, where QEMU puts them, and no HDA: arm64
//! macOS's AppleVirtIOSound drives them, on vendor/qemu's `virtio-sound-pci` whose capabilities
//! vmapple links in address order, without which macOS 27's virtio transport never starts the
//! device (docs/qemu.md, "arm64 guests: audio").
//!
//! The xHCI controller has seven ports (`p2=7,p3=7`), each taking a USB 2 and a USB 3 device:
//! the keyboard, the tablet, and up to [`crate::vm::USB_DEVICES_MAX`] host devices, at their
@@ -439,21 +443,8 @@ fn q35(vm: &Vm, ctx: &Ctx, a: &mut Vec<String>) -> Result<()> {
push(&["-device", &crate::usb::device_arg(u)?]);
}
if c.audio.enabled {
let (backend, input_apart) = sound_routing(audio_backend(vm, ctx), ctx.sound);
push(&["-audiodev", &format!("{},id=snd0", opt(backend))]);
if input_apart {
push(&["-audiodev", "none,id=sndin"]);
}
push(&[
"-device",
"virtio-sound-pci,id=audio,audiodev=snd0,addr=0x7,streams=1",
"-device",
if input_apart {
"virtio-sound-pci,id=audioin,audiodev=sndin,addr=0x8,streams=1,input-streams=1"
} else {
"virtio-sound-pci,id=audioin,audiodev=snd0,addr=0x8,streams=1,input-streams=1"
},
]);
let sound = virtio_sound(vm, ctx, Some(("0x7", "0x8")));
push(&sound.iter().map(String::as_str).collect::<Vec<_>>());
// At ICH9's own slot for it.
push(&["-device", "ich9-intel-hda,id=hda,addr=0x1b"]);
push(&["-device", "hda-output,bus=hda.0,audiodev=snd0"]);
@@ -572,6 +563,10 @@ fn vmapple(vm: &Vm, ctx: &Ctx, a: &mut Vec<String>) -> Result<()> {
.map_or(String::new(), |(w, h)| format!(",resolution={w}x{h}"))
),
]);
if c.audio.enabled {
let sound = virtio_sound(vm, ctx, None);
push(&sound.iter().map(String::as_str).collect::<Vec<_>>());
}
push(&[
"-device",
&format!(
@@ -612,6 +607,33 @@ pub fn sound_routing(backend: &str, devices: crate::os::SoundDevices) -> (&str,
}
}
/// The VM's audio backend and its two virtio sound devices (the module doc): one with an output
/// stream alone and one with an input stream alone, which records from an audiodev of its own,
/// `none`, where [`sound_routing`] keeps the input apart. At `slots`, the output's and the
/// input's, where given (q35's); where QEMU puts them otherwise (vmapple's).
fn virtio_sound(vm: &Vm, ctx: &Ctx, slots: Option<(&str, &str)>) -> Vec<String> {
let (backend, input_apart) = sound_routing(audio_backend(vm, ctx), ctx.sound);
let mut a = vec!["-audiodev".to_string(), format!("{},id=snd0", opt(backend))];
if input_apart {
a.extend(["-audiodev".to_string(), "none,id=sndin".to_string()]);
}
let at = |slot: Option<&str>| slot.map_or(String::new(), |s| format!(",addr={s}"));
let input = if input_apart { "sndin" } else { "snd0" };
a.extend([
"-device".to_string(),
format!(
"virtio-sound-pci,id=audio,audiodev=snd0{},streams=1",
at(slots.map(|s| s.0))
),
"-device".to_string(),
format!(
"virtio-sound-pci,id=audioin,audiodev={input}{},streams=1,input-streams=1",
at(slots.map(|s| s.1))
),
]);
a
}
/// What a start says of the VM's sound where [`sound_routing`] changes it.
pub fn sound_note(backend: &str, devices: crate::os::SoundDevices) -> Option<String> {
match sound_routing(backend, devices) {
@@ -800,6 +822,9 @@ mod tests {
aux: "aux.img".into(),
ecid: 0x9e35_f01e_dc0c_5536,
});
// The command lines below are a VM's without audio; arm64_with_audio_has_virtio_sound
// adds it.
c.audio.enabled = false;
vm
}
@@ -880,6 +905,56 @@ mod tests {
assert_eq!(unix_paths(&inv.args), want);
}
/// An arm64 VM with audio on gets an x86-64 VM's two virtio sound devices on its backend,
/// after the GPU and before the bundle, where QEMU puts them, and no HDA; with audio off, none.
#[test]
fn arm64_with_audio_has_virtio_sound() {
let arm_linux = Host {
os: Os::Linux,
arch: Arch::Aarch64,
};
let mut vm = arm64_vm();
vm.config.audio.enabled = true;
let a = unix_paths(&invocation(&vm, &arm64_ctx(arm_linux)).unwrap().args);
assert!(
has(
&a,
&[
"reims-graphics-pci,id=gfx",
"-audiodev",
"pipewire,id=snd0",
"-device",
"virtio-sound-pci,id=audio,audiodev=snd0,streams=1",
"-device",
"virtio-sound-pci,id=audioin,audiodev=snd0,streams=1,input-streams=1",
"-device",
]
),
"{a:?}"
);
assert!(
a.iter()
.skip_while(|x| !x.starts_with("virtio-sound-pci,id=audioin"))
.nth(2)
.is_some_and(|x| x.starts_with("loader,")),
"{a:?}"
);
assert!(
!a.iter()
.any(|x| x.contains("hda")
|| (x.starts_with("virtio-sound-pci") && x.contains("addr="))),
"{a:?}"
);
vm.config.audio.enabled = false;
let a = invocation(&vm, &arm64_ctx(arm_linux)).unwrap().args;
assert!(
!a.iter()
.any(|x| x == "-audiodev" || x.contains("virtio-sound")),
"{a:?}"
);
}
/// On arm64 Linux the same machine runs on KVM.
#[test]
fn arm64_on_arm64_linux_runs_on_kvm() {
+31 -1
View File
@@ -149,7 +149,7 @@ virtio device (`d151bcd9`, upstream's `b501f21312`).
| QMP's `screendump` in PPM, its default format, writes rows of width × 3 bytes. Upstream's wrote each row with its line buffer's stride, which pixman rounds up to 4 bytes, so a display whose width is not a multiple of 4 (a window of 3662 × 2039 pixels) read back sheared, its colours rotating from row to row; PNG's rows were right. `tests/qtest/display-screendump-test.c` dumps `bochs-display` at widths of 64, 66 and 67 | `e1360002ad` |
| QMP's and HMP's `screendump` copy the console's surface under the BQL, then encode and write the copy on a thread of the pool with the BQL let go (`thread_pool_submit_co`), so a vCPU that touches a device the BQL guards no longer waits out the encode: a PNG of 2880x1620 takes about 120 ms on a Mac mini 2018 ("The reims-graphics device", below) | `500d30fb` |
| the input layer's smooth `scroll` events (`qemu_input_queue_scroll`, `qemu_input_scrolls_smoothly`, QMP `input-send-event`); a device without them (`usb-tablet`, which every x86-64 VM has) gets the event's wheel notches | `9017d7e47e` |
| `vmapple` ("arm64 guests", below): patches 1 to 6 of the RFC v7 series "vmapple: make it work on the latest macOS release" (Mohamed Mediouni), and the branch's commits for HVF's ISV=0 data aborts and in-kernel vGIC, the secondary CPUs' pointer authentication keys, macOS 14 and later and macOS 27 guests, the AES engine's refusal without a crypto library, and `usb-vmapple-tablet`, Virtualization.framework's USB pointing device | `00eb197889`, `4d4daab1a0`, `1161969f05`, `d24cde8e6e`, `ab4c3018f4`, `d10ee1dd7b`, `1d0776ac53`, `3deaf0f735`, `74d5134fb9`, `29043c9b34`, `eb67b81577`, `743bbba8f9`, `ffca79f534`, `6a54a097bd`, `eae5dd4a5c`, `4a39ef8d54`, `d01f28c9ed`, `93f9f40cdc` |
| `vmapple` ("arm64 guests", below): patches 1 to 6 of the RFC v7 series "vmapple: make it work on the latest macOS release" (Mohamed Mediouni), and the branch's commits for HVF's ISV=0 data aborts and in-kernel vGIC, the secondary CPUs' pointer authentication keys, macOS 14 and later and macOS 27 guests, the AES engine's refusal without a crypto library, and `usb-vmapple-tablet`, Virtualization.framework's USB pointing device | `00eb197889`, `4d4daab1a0`, `1161969f05`, `d24cde8e6e`, `ab4c3018f4`, `d10ee1dd7b`, `1d0776ac53`, `3deaf0f735`, `74d5134fb9`, `29043c9b34`, `eb67b81577`, `743bbba8f9`, `ffca79f534`, `6a54a097bd`, `eae5dd4a5c`, `4a39ef8d54`, `d01f28c9ed`, `93f9f40cdc`, `5c42f3fce4` |
| apple-gfx, Apple's paravirtualized GPU device on the library, with its windows, its option ROM, its IOSurface device and the VideoToolbox and M2 scaler devices, removed: `vmapple`, whose display was `apple-gfx-mmio`, stayed in the tree unbuilt until the next row's commits; the option that finds the library is `reims_vgpu` (`--enable-reims-vgpu`) | `96e1b470` |
| `vmapple` for Ambrosia, built by default in an `aarch64-softmmu` build again: the machine without apple-gfx's display; no kernel-entry hook (the firmware makes macOS's kernel-entry changes, so AVPBooter boots only macOS 12 and 13 guests); RAM at 4 GiB, with the PCIe MMIO window from 0x50000000 below it for 1 GiB BARs (AVPBooter does not boot on this layout); TCG with the `max` CPU and a 24 MHz generic timer; the guest's write to its DMA configuration doorbell at 0x4ffff8 ignored, which panicked a macOS 27.0.1 kernel under TCG; and `virtio-net-pci` without its control queue in the machine's defaults, since one boot in four to six of a macOS 26 or 27 guest missed the completion of its first control command and never started DHCP | `15989559`, `b6821b78`, `981994e8`, `c629d147`, `42e7766d`, `1b3509f3` |
| BAR 2 is one RAM region with no dirty log and no `scanout_written` callback: the library's window draws the scanout again at each refresh while it shows no presented surface | `7365f20e` |
@@ -2631,6 +2631,36 @@ Text uses:
text at both levels, and so does `ImageAnalyzer`: x86-64 macOS's
TextRecognition runs Espresso models on the GPU (`eng=5`), our device.
### arm64 guests: audio
An arm64 VM with audio on gets an x86-64 VM's two virtio sound devices on
the same backend ("x86-64 macOS guests: audio", above): one with an output
stream alone and one with an input stream alone. They follow the GPU, at the
slots QEMU picks, and there is no HDA, since Ambrosia plays no boot chime.
arm64 macOS's `AppleVirtIO` kext has `AppleVirtIOSound`, matching virtio
device type 0x19 (`IOVirtIOPrimaryMatch` `0x00191af4`), and its
`AppleVirtIOPCITransport` matches every `0x1af4` PCI device (**read**, from
the VirtualMac2,1 kernelcaches of 14.0 and 27.2.1).
macOS 27's virtio PCI transport never starts a device whose capability list
runs from high offsets to low, as QEMU links it, so on `vmapple` the
`virtio-sound-pci` devices take `x-ascending-capabilities`, as
`virtio-net-pci` does (`4a39ef8d54`, `5c42f3fce4`). **Measured** 2026-11-23
under KVM on an M1 Pro, with a macOS 27.2.1 (26B131) guest:
- Without the relink, each sound device's `IOConfigThread` spun in
`AppleVirtIO` ("triggered fail-safe by spinning for at least 1000000us at
fixed priority"). Sampled over QMP's `info registers`, it re-read the
device's PCI_CFG capability at 0x84 and notify capability at 0x70 through
IOPCIFamily's capability walk, forever. The transport never registered,
the device's status stayed 0, and `kernel_task` took two of four vCPUs.
With one sound device, and with two at `vectors=5`, the boot stopped
there, before the disks' drivers reached `DRIVER_OK`.
- With it, both devices reach `DRIVER_OK`, and Core Audio lists two "Apple
Virtual Sound Device"s: 2 output channels at 48 kHz as the default output,
and 2 input channels as the default input. `afplay` in the guest plays
through a `qemu-system-aarch64` PipeWire stream on the host's speakers.
- Recording, and arm64 guests under HVF or TCG, are **unknown**.
### arm64 guests: the boot check of our configuration
Ambrosia's done list has 10 boots of our configuration: macOS 13.6, 14.6.1,
Vendored
+1 -1
Submodule vendor/qemu updated: 53e766304f...5c42f3fce4.