Skip to content

openvmm: wide RTC port reads leave stale bytes in the upper portion of the result #1151

Closed

Description

@tangruize

RTC PIO reads wider than one byte return the register byte together with stale upper bytes from the destination buffer, despite the intercept's documented zero-extension behavior.

In vm/devices/chipset/src/cmos_rtc.rs, Rtc::io_read writes only data[0]:

        // 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) {
            RtcIoPort::ADDR => self.state.addr,
            RtcIoPort::DATA => self.get_cmos_byte(self.state.addr),
            _ => return IoResult::Err(IoError::InvalidRegister),
        };

        IoResult::Ok

For a two- or four-byte destination, the remaining bytes are never overwritten. On a reused PIO exit buffer, those bytes can come from an earlier operation rather than the current RTC read.

Reproduction

Write 0x0d to address port 0x70 to select RTC status D, then read data port 0x71 with a 16- or 32-bit PIO access. The low byte is status D's 0x80, but the upper bytes retain the read buffer's previous contents instead of producing 0x0080 or 0x00000080.

A wide read of address port 0x70 has the same problem: only the selected CMOS index replaces the previous buffer contents.

Fix

fix.patch determines the register value, clears the entire destination, then writes the value to its first byte. Invalid-register handling and one-byte reads remain unchanged.

Activity

  1. tangruize commented on Nov 1, 2026

    @tangruize
    ContributorAuthor

    Proposed patch

    Patch against OpenVMM 998b9f2402050cd16d1faa897bc831ef1434be48. Save the contents below as fix.patch and run git apply fix.patch.

    fix.patch
    diff --git a/vm/devices/chipset/src/cmos_rtc.rs b/vm/devices/chipset/src/cmos_rtc.rs
    index ae091aec8..44fb21532 100644
    --- a/vm/devices/chipset/src/cmos_rtc.rs
    +++ b/vm/devices/chipset/src/cmos_rtc.rs
    @@ -897,11 +897,13 @@ fn io_read(&mut self, io_port: u16, data: &mut [u8]) -> IoResult {
             // 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
         }
    @@ -1564,4 +1566,59 @@ fn hms_to_duration(h: u8, m: u8, s: u8) -> Duration {
     
             // TODO: test some more alarm scenarios
         }
    +
    +    /// Regression test of BUG-07: a read of port 0x70 or 0x71 wider than a
    +    /// byte returns the byte zero-extended to the access size, as `io_read`'s
    +    /// comment says. Before the fix only the first byte was written and the
    +    /// others kept the buffer's content (KVM's shared `kvm_run` PIO area,
    +    /// which vmotherboard passes as is), so a 16- or 32-bit `in` could return
    +    /// bytes left by an earlier exit.
    +    #[test]
    +    fn rtc_wide_read_is_zero_extended() {
    +        let (_, _, _, mut rtc) = new_test_rtc();
    +        let (addr, data_port) = (RtcIoPort::ADDR.0, RtcIoPort::DATA.0);
    +        for (port, len) in [
    +            (addr, 2usize),
    +            (data_port, 2),
    +            (addr, 4),
    +            (data_port, 4),
    +            (addr, 8),
    +            (data_port, 8),
    +        ] {
    +            rtc.io_write(addr, &[CmosReg::STATUS_D.0]).unwrap();
    +            let mut data = vec![0xa5; len];
    +            rtc.io_read(port, &mut data).unwrap();
    +            // The register (the index 0x0d, or status D's VRT bit), then zeros.
    +            assert_eq!(data[0], if port == addr { 0x0d } else { 0x80 });
    +            assert!(
    +                data[1..].iter().all(|&b| b == 0),
    +                "{port:#x} x{len}: {data:x?}"
    +            );
    +        }
    +    }
    +
    +    /// BUG-07's fix keeps one-byte reads: port 0x70 returns the index
    +    /// register, port 0x71 the selected register (RAM bytes as written, status
    +    /// B as written, status D's VRT bit, status C's flags), as before.
    +    #[test]
    +    fn rtc_one_byte_reads_are_unchanged() {
    +        let (_, _, _, mut rtc) = new_test_rtc();
    +        for (index, value) in [(0x0eu8, 0x5au8), (0x40, 0xa7), (0x7f, 0x01), (0x0b, 0x06)] {
    +            set_cmos_data(&mut rtc, CmosReg(index), value);
    +            let mut data = [0xa5];
    +            rtc.io_read(RtcIoPort::ADDR.0, &mut data).unwrap();
    +            assert_eq!(data, [index]);
    +            rtc.io_read(RtcIoPort::DATA.0, &mut data).unwrap();
    +            assert_eq!(data, [value], "index {index:#x}");
    +        }
    +        rtc.io_write(RtcIoPort::ADDR.0, &[CmosReg::STATUS_D.0])
    +            .unwrap();
    +        let mut data = [0xa5];
    +        rtc.io_read(RtcIoPort::DATA.0, &mut data).unwrap();
    +        assert_eq!(data, [0x80]);
    +        rtc.io_write(RtcIoPort::ADDR.0, &[CmosReg::STATUS_C.0])
    +            .unwrap();
    +        rtc.io_read(RtcIoPort::DATA.0, &mut data).unwrap();
    +        assert_eq!(data, [0]);
    +    }
     }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions