From 60e160369b1f1f10543e0699d4ae1e95810a1e62 Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Thu, 12 Nov 2026 18:24:10 -0800 Subject: [PATCH] chipset: zero-extend wide reads of the RTC's ports Rtc::io_read wrote only the first byte of the read buffer, although its comment says that a read wider than a byte returns the register's byte zero-extended to the size of the access. The other bytes kept what the buffer held. On MSHV and WHP, emulate_io gives the device the guest's EAX as the buffer, so a 16- or 32-bit IN from port 0x70 or 0x71 kept the guest's upper bytes of AX or EAX, as an 8-bit IN does (microsoft/nvx#1151). KVM zeroes its PIO data before it exits to the VMM for an IN, which hid the defect there. io_read now reads the register first, so that a read of another port still leaves the buffer as it was, then zeroes the whole buffer and stores the register's byte in its first byte, as the DMA controller's io_read does. A one-byte read returns the same byte as before. Tests: wide_reads_are_zero_extended reads both ports with 2- and 4-byte buffers that hold 0xa5 bytes, and requires the register's byte followed by zeros. At 6f57a6d61, a 2-byte read of port 0x70 returns [0x40, 0xa5]. Evidence: in NVX's Alpine microVM, with OpenVMM built from 6f57a6d61, a guest program selected status D, 0x80, and read port 0x70 and 0x71 with 16- and 32-bit INs, each starting with EAX = 0xdeadbeef. On MSHV and WHP, every read returned 0xdeadbe0d from port 0x70 and 0xdeadbe80 from port 0x71. On KVM they returned the zero-extended values, 0xdead000d and 0x0000000d, and 0xdead0080 and 0x00000080, since a 16-bit IN keeps EAX[31:16]. With cargo test -p chipset on Windows x64, the new test fails at 6f57a6d61 and passes with this commit, with the crate's other 112 tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- vm/devices/chipset/src/cmos_rtc.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/vm/devices/chipset/src/cmos_rtc.rs b/vm/devices/chipset/src/cmos_rtc.rs index 816fdbe8ecd..5615511c93b 100644 --- a/vm/devices/chipset/src/cmos_rtc.rs +++ b/vm/devices/chipset/src/cmos_rtc.rs @@ -918,11 +918,13 @@ impl PortIoIntercept for Rtc { // We assume all accesses are one byte in size. Attempts to // access larger sizes will return a single byte of information // (zero-extended to the size of the access). - data[0] = match RtcIoPort(io_port) { + let value = match RtcIoPort(io_port) { RtcIoPort::ADDR => self.state.addr, RtcIoPort::DATA => self.get_cmos_byte(self.state.addr), _ => return IoResult::Err(IoError::InvalidRegister), }; + data.fill(0); + data[0] = value; IoResult::Ok } @@ -1354,6 +1356,24 @@ mod tests { } } + #[test] + fn wide_reads_are_zero_extended() { + let (_, _, _, mut rtc) = new_test_rtc(); + set_cmos_data(&mut rtc, CmosReg(0x40), 0x5a); + + // The buffer starts with stale bytes, as a backend's reused I/O buffer + // or the guest's register can provide. + for len in [2, 4] { + for (port, value) in [(RtcIoPort::ADDR.0, 0x40), (RtcIoPort::DATA.0, 0x5a)] { + let mut data = vec![0xa5; len]; + rtc.io_read(port, &mut data).unwrap(); + let mut expected = vec![0; len]; + expected[0] = value; + assert_eq!(data, expected, "port {port:#x}, {len} bytes"); + } + } + } + #[test] fn test_writeable() { let (_, _, _, mut rtc) = new_test_rtc();