1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
use super::*;
impl DeviceManager {
/// Finds device by MMIO address.
#[must_use]
pub fn find_by_mmio(&self, addr: u64) -> Option<DeviceId> {
for (base, id) in &self.mmio_map {
if let Some(device) = self.devices.get(id) {
if addr >= *base && addr < *base + device.info.mmio_size {
return Some(*id);
}
}
}
None
}
/// Handles MMIO read.
///
/// # Errors
///
/// Returns an error if the read fails.
pub fn handle_mmio_read(&self, addr: u64, size: usize) -> Result<u64> {
let device_id = self
.find_by_mmio(addr)
.ok_or_else(|| VmmError::Device(format!("No device at MMIO address {addr:#x}")))?;
let device = self
.devices
.get(&device_id)
.ok_or_else(|| VmmError::Device(format!("Device {} not found", device_id.0)))?;
let base = device.info.mmio_base.unwrap_or(0);
let offset = addr - base;
if let Some(state) = &device.mmio_state {
let state = state
.read()
.map_err(|e| VmmError::Device(format!("Failed to lock device state: {e}")))?;
// Handle config space reads - forward to actual device
if offset >= virtio_mmio::regs::CONFIG {
let config_offset = offset - virtio_mmio::regs::CONFIG;
if let Some(virtio_dev) = &device.virtio_device {
let dev = virtio_dev.lock().map_err(|e| {
VmmError::Device(format!("Failed to lock virtio device: {e}"))
})?;
let mut data = vec![0u8; size];
dev.read_config(config_offset, &mut data);
tracing::trace!(
"Config read: device={} offset={:#x} size={} data={:?}",
device_id.0,
config_offset,
size,
&data[..size.min(8)]
);
return Ok(match size {
1 => u64::from(data[0]),
2 => u64::from(u16::from_le_bytes([data[0], data[1]])),
4 => u64::from(u32::from_le_bytes([data[0], data[1], data[2], data[3]])),
8 => u64::from_le_bytes([
data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
]),
_ => 0,
});
}
return Ok(0);
}
let value = state.read(offset);
let result = match size {
1 => u64::from(value as u8),
2 => u64::from(value as u16),
4 => u64::from(value),
_ => u64::from(value),
};
Ok(result)
} else {
Ok(0)
}
}
/// Handles MMIO write.
///
/// # Errors
///
/// Returns an error if the write fails.
pub fn handle_mmio_write(&self, addr: u64, size: usize, value: u64) -> Result<()> {
let device_id = self
.find_by_mmio(addr)
.ok_or_else(|| VmmError::Device(format!("No device at MMIO address {addr:#x}")))?;
let device = self
.devices
.get(&device_id)
.ok_or_else(|| VmmError::Device(format!("Device {} not found", device_id.0)))?;
let base = device.info.mmio_base.unwrap_or(0);
let offset = addr - base;
if let Some(state) = &device.mmio_state {
let old_status = {
let s = state
.read()
.map_err(|e| VmmError::Device(format!("Failed to lock device state: {e}")))?;
s.status
};
// Handle config space writes - forward to actual device
if offset >= virtio_mmio::regs::CONFIG {
let config_offset = offset - virtio_mmio::regs::CONFIG;
if let Some(virtio_dev) = &device.virtio_device {
let mut dev = virtio_dev.lock().map_err(|e| {
VmmError::Device(format!("Failed to lock virtio device: {e}"))
})?;
let data: Vec<u8> = match size {
1 => vec![value as u8],
2 => (value as u16).to_le_bytes().to_vec(),
4 => (value as u32).to_le_bytes().to_vec(),
8 => value.to_le_bytes().to_vec(),
_ => return Ok(()),
};
dev.write_config(config_offset, &data);
}
return Ok(());
}
let value32 = match size {
1 => value as u32 & 0xFF,
2 => value as u32 & 0xFFFF,
4 | 8 => value as u32,
_ => value as u32,
};
// Write to MMIO state
{
let mut state = state
.write()
.map_err(|e| VmmError::Device(format!("Failed to lock device state: {e}")))?;
state.write(offset, value32);
}
// Handle special cases after write
match offset {
virtio_mmio::regs::STATUS => {
let new_status = value32 as u8;
// Handle feature acknowledgment
if new_status & DeviceStatus::FEATURES_OK != 0
&& old_status & DeviceStatus::FEATURES_OK == 0
{
if let Some(virtio_dev) = &device.virtio_device {
let mmio_state = state.read().map_err(|e| {
VmmError::Device(format!("Failed to lock device state: {e}"))
})?;
let mut dev = virtio_dev.lock().map_err(|e| {
VmmError::Device(format!("Failed to lock virtio device: {e}"))
})?;
dev.ack_features(mmio_state.driver_features);
tracing::debug!(
"Device {} acknowledged features: {:#x}",
device_id.0,
mmio_state.driver_features
);
}
}
// Handle device activation
if new_status & DeviceStatus::DRIVER_OK != 0
&& old_status & DeviceStatus::DRIVER_OK == 0
{
if let Some(virtio_dev) = &device.virtio_device {
let mut dev = virtio_dev.lock().map_err(|e| {
VmmError::Device(format!("Failed to lock virtio device: {e}"))
})?;
dev.activate().map_err(|e| {
VmmError::Device(format!("Failed to activate device: {e}"))
})?;
tracing::info!("Device {} activated", device_id.0);
}
// Spawn the net-io worker for the primary VirtioNet device.
self.maybe_spawn_net_rx_worker(device_id, state);
}
// Handle device reset
if new_status == 0 {
if let Some(virtio_dev) = &device.virtio_device {
let mut dev = virtio_dev.lock().map_err(|e| {
VmmError::Device(format!("Failed to lock virtio device: {e}"))
})?;
dev.reset();
tracing::info!("Device {} reset", device_id.0);
}
}
}
virtio_mmio::regs::QUEUE_NOTIFY => {
let queue_idx = value32 as u16;
// Log vsock TX notifications at trace level (per-kick hot path).
if device.info.device_type == DeviceType::VirtioVsock && queue_idx == 1 {
tracing::trace!("QUEUE_NOTIFY: vsock TX queue 1 kicked by guest!",);
}
tracing::trace!(
"QUEUE_NOTIFY: device {} ({:?}) queue {}",
device_id.0,
device.info.device_type,
queue_idx,
);
if let Some(virtio_dev) = &device.virtio_device {
// Build QueueConfig from current MMIO state for the
// notified queue index.
let qcfg = {
let mmio_state = state.read().map_err(|e| {
VmmError::Device(format!("Failed to lock state: {e}"))
})?;
let qi = queue_idx as usize;
if qi < MAX_VIRTQUEUES {
QueueConfig {
desc_addr: mmio_state.queue_desc[qi],
avail_addr: mmio_state.queue_driver[qi],
used_addr: mmio_state.queue_device[qi],
size: mmio_state.queue_num[qi],
ready: mmio_state.queue_ready[qi],
gpa_base: self.guest_ram_gpa,
}
} else {
QueueConfig::default()
}
};
if let (Some(ram_base), ram_size) =
(self.guest_ram_base, self.guest_ram_size)
{
// Build a guest memory slice covering the guest RAM region.
// The host pointer `ram_base` maps to GPA `guest_ram_gpa`.
// All GPA-based indices must subtract `gpa_base` to obtain
// the correct offset within this slice.
//
// SAFETY: `ram_base` is the host mapping returned by
// Virtualization.framework and is valid for `ram_size` bytes.
let guest_mem =
unsafe { std::slice::from_raw_parts_mut(ram_base, ram_size) };
// VirtioBlock async path: dispatch to worker thread
// instead of blocking the vCPU with synchronous I/O.
if device.info.device_type == DeviceType::VirtioBlock {
let workers = self
.blk_workers
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if let Some(handle) = workers.get(&device_id) {
// Ring the queue's doorbell; the owning worker
// drains avail, does the I/O, and completes.
handle.ring(queue_idx);
}
}
// VirtioNet TX (queue 1): extract ethernet frames
// from guest memory and write to the network host fd.
// This bypasses the generic process_queue — the
// concrete `VirtioNet` owns its fd + TX cursor via
// `NetPort` and implements the hot path itself.
else if device.info.device_type == DeviceType::VirtioNet
&& queue_idx == 1
&& (self.primary_net.is_some() || self.bridge_net.is_some())
{
let is_bridge = self
.bridge_net_device_id
.is_some_and(|bid| bid == device_id);
let typed = if is_bridge {
self.bridge_net.as_ref()
} else {
self.primary_net.as_ref()
};
let net_notify = match typed {
Some(arc) => arc.lock().is_ok_and(|d| {
d.drain_tx_queue(&qcfg, finalize_virtio_net_checksum)
}),
None => false,
};
// `drain_tx_queue` now publishes the used ring
// (via SplitQueue, with the StoreLoad barrier) and
// avail_event itself; the VMM only raises the IRQ.
let _ = guest_mem;
if net_notify && device.info.irq.is_some() {
{
let mut s = state.write().map_err(|e| {
VmmError::Device(format!("Failed to lock state: {e}"))
})?;
s.trigger_interrupt(virtio_mmio::INT_VRING);
}
self.sync_irq_level(device_id);
}
} else {
// Generic process_queue for all other devices.
let mut dev = virtio_dev.lock().map_err(|e| {
VmmError::Device(format!("Failed to lock device: {e}"))
})?;
// Log vsock TX processing at trace level (per-kick hot path).
let is_vsock_tx = device.info.device_type
== DeviceType::VirtioVsock
&& queue_idx == 1;
match dev.process_queue(queue_idx, guest_mem, &qcfg) {
Ok(completions) if !completions.is_empty() => {
if is_vsock_tx {
tracing::trace!(
"Vsock QUEUE_NOTIFY TX: {} completions processed!",
completions.len(),
);
}
tracing::trace!(
"Device {} queue {} processed {} completions",
device_id.0,
queue_idx,
completions.len()
);
// Console TX completions don't need interrupts —
// the guest doesn't wait for host ACK on console output.
// Skipping avoids interrupt storms with level-triggered SPIs.
let skip_irq = device.info.device_type
== DeviceType::VirtioConsole
&& queue_idx == 1;
if !skip_irq {
{
let mut s = state.write().map_err(|e| {
VmmError::Device(format!(
"Failed to lock state: {e}"
))
})?;
s.trigger_interrupt(virtio_mmio::INT_VRING);
}
self.sync_irq_level(device_id);
}
}
Ok(_) => {
if is_vsock_tx {
tracing::trace!(
"Vsock QUEUE_NOTIFY TX: kicked but 0 completions \
(last_avail_idx_tx may already be current)",
);
}
}
Err(e) => {
tracing::warn!(
"Device {} queue {} error: {e}",
device_id.0,
queue_idx
);
}
}
} // end else (non-VirtioNet)
} else {
tracing::trace!(
"Device {} queue {} notified but no guest memory set",
device_id.0,
queue_idx
);
}
} else {
tracing::trace!(
"Device {} queue {} notified (no device impl)",
device_id.0,
queue_idx
);
}
}
virtio_mmio::regs::INTERRUPT_ACK => {
// Sync the GIC SPI level with the updated interrupt_status.
// If all bits are cleared, the SPI goes low; if bits remain
// (from a concurrent completion), the SPI stays high.
self.sync_irq_level(device_id);
}
_ => {}
}
}
Ok(())
}
}