Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

fix(hypervisor): accept every 1-8 byte MMIO fragment from KVM (#3145)

Abishekcs3ed68b111e1f
diff --git a/src/hypervisor/src/kvm/vcpu.rs b/src/hypervisor/src/kvm/vcpu.rs
index a8df3a920..5f480cbe8 100644
--- a/src/hypervisor/src/kvm/vcpu.rs
+++ b/src/hypervisor/src/kvm/vcpu.rs
@@ -547,7 +547,10 @@ unsafe fn decode_exit(id: u32, run: &mut kvm_run, run_size: usize) -> Result<Vcp
KVM_EXIT_MMIO => {
// SAFETY: exit_reason identifies the active union field.
let access = unsafe { &mut run.__bindgen_anon_1.mmio };
- if !matches!(access.len, 1 | 2 | 4 | 8) || access.is_write > 1 {
+ // KVM splits an access that crosses a page boundary into fragments
+ // at each page, so a fragment can be any length from 1 to 8 bytes,
+ // not only the naturally aligned widths.
+ if !(1..=8).contains(&access.len) || access.is_write > 1 {
return Err(unsupported());
}
let bytes = &mut access.data[..access.len as usize];
@@ -1259,11 +1262,32 @@ mod tests {
}
}
+ #[test]
+ fn split_mmio_fragments_decode_at_their_own_addresses() {
+ let mut buffer = ExitBuffer::default();
+ buffer.run.exit_reason = KVM_EXIT_MMIO;
+
+ for (addr, len) in [(0x0fff_u64, 1_u32), (0x1000, 3)] {
+ buffer.run.__bindgen_anon_1.mmio = kvm_bindings::kvm_run__bindgen_ty_1__bindgen_ty_6 {
+ phys_addr: addr,
+ data: [0; 8],
+ len,
+ is_write: 0,
+ };
+
+ let VcpuExit::MmioRead { guest_addr, bytes } = buffer.decode().unwrap() else {
+ panic!("expected read");
+ };
+ assert_eq!(guest_addr, addr);
+ assert_eq!(bytes.len(), len as usize);
+ }
+ }
+
#[test]
fn mmio_exits_preserve_address_width_and_read_responses() {
let mut buffer = ExitBuffer::default();
buffer.run.exit_reason = KVM_EXIT_MMIO;
- for len in [1, 2, 4, 8] {
+ for len in 1..=8 {
buffer.run.__bindgen_anon_1.mmio = kvm_bindings::kvm_run__bindgen_ty_1__bindgen_ty_6 {
phys_addr: 0x1000_0000,
data: [0; 8],
@@ -1283,7 +1307,7 @@ mod tests {
assert_eq!(guest_addr, 0x1000_0000);
assert_eq!(bytes, vec![0x42; len as usize]);
}
- for (len, is_write) in [(0, 0), (3, 0), (9, 0), (1, 2)] {
+ for (len, is_write) in [(0, 0), (9, 0), (1, 2)] {
buffer.run.__bindgen_anon_1.mmio.len = len;
buffer.run.__bindgen_anon_1.mmio.is_write = is_write;
assert!(buffer.decode().is_err());
diff --git a/src/vmm/tests/bus/kvm.rs b/src/vmm/tests/bus/kvm.rs
index 77ced1ee8..4a02131b2 100644
--- a/src/vmm/tests/bus/kvm.rs
+++ b/src/vmm/tests/bus/kvm.rs
@@ -7,7 +7,7 @@
use std::sync::{Arc, Mutex};
-use boxlite_hypervisor::KvmVm;
+use boxlite_hypervisor::{KvmVm, VcpuExit};
use super::{BusDevice, BusRange, Buses, accesses, recorder};
use crate::{
@@ -70,6 +70,56 @@ fn a_flat_guest_reaches_a_port_device_at_its_register_offset() {
assert_eq!(buses.pio.unclaimed(), (0, 0));
}
+/// A 4-byte read at the last byte of a page reaches KVM as two fragments: one
+/// byte at the page boundary and three bytes at the next page. Each fragment
+/// must decode at its own address and length.
+#[test]
+#[ignore = "requires Linux x86_64 with access to /dev/kvm"]
+fn a_cross_page_mmio_read_is_split_into_one_and_three_bytes() {
+ /// The device window, one page past the 2 MiB of guest RAM.
+ const MMIO_BASE: u64 = 0x20_0000;
+ /// The first byte of the 4-byte read: the last byte of the first page.
+ const CROSS_PAGE: u64 = MMIO_BASE + 0x0fff;
+
+ // mov eax, [0x200fff]; hlt; jmp hlt
+ #[rustfmt::skip]
+ const PROGRAM: &[u8] = &[
+ 0xa1, 0xff, 0x0f, 0x20, 0x00,
+ 0xf4, 0xeb, 0xfd,
+ ];
+
+ let mut guest = FlatGuest::new(PROGRAM);
+ let device = recorder(0x42);
+ let mut buses = Buses::default();
+ let shared: Arc<Mutex<dyn BusDevice>> = device.clone();
+ buses
+ .mmio
+ .insert(
+ BusRange::new(MMIO_BASE, 0x2000).unwrap(),
+ "test",
+ Some((shared, 0)),
+ )
+ .unwrap();
+
+ let mut reads = Vec::new();
+ guest.run(|exit| {
+ if let VcpuExit::MmioRead { guest_addr, bytes } = &exit {
+ println!(
+ " fragment {}: {} byte(s) at {guest_addr:#x}",
+ reads.len() + 1,
+ bytes.len()
+ );
+ reads.push((*guest_addr, bytes.len()));
+ }
+ if let Some(other) = buses.dispatch(exit) {
+ panic!("{other:?} is not an I/O exit");
+ }
+ reads.len() < 2
+ });
+
+ assert_eq!(reads, [(CROSS_PAGE, 1), (CROSS_PAGE + 1, 3)]);
+}
+
/// KVM's interrupt controller, recording every host call that succeeded.
struct Recording {
vm: Arc<KvmVm>,