Skip to content

openvmm: RTC 12-hour conversion reports noon as AM and rejects 12 PM writes #1143

Closed

Description

@tangruize

In 12-hour mode, the RTC reports midnight as hour zero and noon as 12 AM. Writing 12 PM decodes to hour 24 and is rejected; alarm hour decoding has the same error.

In vm/devices/chipset/src/cmos_rtc.rs, canonical_hms adds twelve to the decoded PM hour without first mapping twelve to zero:

            let hour_ampm = from_bcd(hour & 0x7F);
            if hour & 0x80 != 0 {
                hour = hour_ampm + 12;
            } else {
                hour = hour_ampm
            }

BCD 0x92 becomes 12 + 12 = 24, while 0x12 becomes noon rather than midnight. The binary branch applies the same PM addition. In the reverse conversion, Rtc::sync_clock_to_cmos subtracts twelve and sets the PM bit only when clock_time.hour() > 12, leaving noon marked AM and midnight encoded as zero.

Reproduction

Through ports 0x70/0x71, clear status B's 24-hour and binary bits to select 12-hour BCD mode. At noon, HOUR reads 0x12 instead of 0x92; at midnight, it reads 0x00 instead of 0x12. Writing 0x92 to HOUR should set noon, but the decoded hour 24 is rejected.

Binary mode has the same semantic error: noon and midnight should be 0x8c and 0x0c.

Fix

fix.patch encodes hours as 1–12 with bit 7 marking PM. When decoding, it maps twelve to zero before adding twelve for PM, giving midnight hour zero and noon hour twelve. It corrects both calendar and alarm conversion while retaining 24-hour behavior.

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..985152d80 100644
    --- a/vm/devices/chipset/src/cmos_rtc.rs
    +++ b/vm/devices/chipset/src/cmos_rtc.rs
    @@ -273,6 +273,14 @@ fn from_bcd(n: u8) -> u8 {
         (n >> 4) * 10 + (n & 0xf)
     }
     
    +/// The hour of day of a 12-hour-mode hour `h` (MC146818: 1-12, `pm` from bit
    +/// 7): 12 AM is midnight (0), 12 PM is noon (12), any other hour is `h`, plus
    +/// 12 for PM.
    +fn twelve_hour(h: u8, pm: bool) -> u8 {
    +    let h = if h == 12 { 0 } else { h };
    +    if pm { h + 12 } else { h }
    +}
    +
     /// Applies BCD + 24H time calculations on-top of HMS values fetched from CMOS.
     fn canonical_hms(status_b: StatusRegB, mut hour: u8, mut min: u8, mut sec: u8) -> (u8, u8, u8) {
         if !status_b.disable_bcd() {
    @@ -282,18 +290,11 @@ fn canonical_hms(status_b: StatusRegB, mut hour: u8, mut min: u8, mut sec: u8) -
             if status_b.h24_mode() {
                 hour = from_bcd(hour);
             } else {
    -            let hour_ampm = from_bcd(hour & 0x7F);
    -            if hour & 0x80 != 0 {
    -                hour = hour_ampm + 12;
    -            } else {
    -                hour = hour_ampm
    -            }
    +            hour = twelve_hour(from_bcd(hour & 0x7F), hour & 0x80 != 0);
             }
         } else {
             if !status_b.h24_mode() {
    -            if hour & 0x80 != 0 {
    -                hour = (hour & 0x7F) + 12;
    -            }
    +            hour = twelve_hour(hour & 0x7F, hour & 0x80 != 0);
             }
         }
     
    @@ -822,10 +823,11 @@ fn sync_clock_to_cmos(&mut self) {
                 if status_b.h24_mode() {
                     hour
                 } else {
    -                if clock_time.hour() > 12 {
    -                    hour - 12
    -                } else {
    -                    hour
    +                // 12-hour mode (MC146818): 1-12, with bit 7 set for PM (below):
    +                // midnight is 12 AM, noon is 12 PM.
    +                match hour % 12 {
    +                    0 => 12,
    +                    h => h,
                     }
                 }
             };
    @@ -854,7 +856,7 @@ fn sync_clock_to_cmos(&mut self) {
             }
     
             if !status_b.h24_mode() {
    -            if clock_time.hour() > 12 {
    +            if clock_time.hour() >= 12 {
                     self.state.cmos[CmosReg::HOUR] |= 0x80;
                 }
             }
    @@ -1073,13 +1075,17 @@ fn get_rtc_data(rtc: &mut Rtc, addr: CmosReg, bcd: bool, hour24: bool) -> u8 {
             rtc.io_read(RtcIoPort::DATA.0, &mut temp).unwrap();
             let mut data = temp[0];
             if addr == CmosReg::HOUR {
    +            // 12-hour mode (MC146818): 1-12 with bit 7 for PM; 12 AM is hour 0.
                 let pm = if hour24 { false } else { (data & 0x80) != 0 };
    -            if pm {
    +            if !hour24 {
                     data &= 0x7f
                 };
                 if bcd {
                     data = from_bcd(data)
                 };
    +            if !hour24 && data == 12 {
    +                data = 0;
    +            }
                 if pm {
                     data += 12;
                 }
    @@ -1098,14 +1104,19 @@ fn set_rtc_data(rtc: &mut Rtc, addr: CmosReg, data: u8, bcd: bool, hour24: bool)
             rtc.io_write(RtcIoPort::ADDR.0, &temp).unwrap();
             let mut new_data = data;
             if addr == CmosReg::HOUR {
    -            let pm = if hour24 { false } else { new_data > 12 };
    +            // 12-hour mode (MC146818): 1-12 with bit 7 for PM; hour 0 is 12 AM.
    +            let pm = if hour24 { false } else { new_data >= 12 };
    +            if !hour24 {
    +                new_data = match new_data % 12 {
    +                    0 => 12,
    +                    h => h,
    +                };
    +            }
                 if bcd {
    -                new_data =
    -                    to_bcd(if pm { new_data - 12 } else { new_data }) | if pm { 0x80 } else { 0 };
    -            } else {
    -                if pm {
    -                    new_data = (new_data - 12) | 0x80;
    -                }
    +                new_data = to_bcd(new_data);
    +            }
    +            if pm {
    +                new_data |= 0x80;
                 }
             } else {
                 if bcd {
    @@ -1564,4 +1575,133 @@ fn hms_to_duration(h: u8, m: u8, s: u8) -> Duration {
     
             // TODO: test some more alarm scenarios
         }
    +
    +    /// Sets the RTC's mock UTC clock to `ms` milliseconds after the Unix
    +    /// epoch.
    +    fn set_clock_ms(time: &MockLocalClockAccessor, ms: i64) {
    +        let now = time.get_time().as_millis_since_unix_epoch();
    +        if ms >= now {
    +            time.tick(Duration::from_millis((ms - now) as u64));
    +        } else {
    +            time.tick_backwards(Duration::from_millis((now - ms) as u64));
    +        }
    +    }
    +
    +    /// Steps the stopped VM time and the RTC's clock by `d` together, as a
    +    /// running VM would, then polls the RTC.
    +    fn run_rtc(
    +        pool: &mut pal_async::DefaultPool,
    +        keeper: &mut vmcore::vmtime::VmTimeKeeper,
    +        time: &MockLocalClockAccessor,
    +        rtc: &mut Rtc,
    +        d: Duration,
    +    ) {
    +        pool.run_until(keeper.advance(d));
    +        time.tick(d);
    +        rtc.poll_device(&mut std::task::Context::from_waker(std::task::Waker::noop()));
    +    }
    +
    +    /// The seconds from VM time 0 at which the alarm is armed to fire.
    +    fn alarm_due_secs(rtc: &Rtc) -> u64 {
    +        rtc.vmtime_alarm.get_timeout().unwrap().as_100ns() / 10_000_000
    +    }
    +
    +    /// The hours byte of hour of day `h` in status B's format, by the MC146818
    +    /// datasheet, written independently of the RTC: 24-hour mode the hour;
    +    /// 12-hour mode 1-12 with bit 7 set for PM (midnight 12 AM, noon 12 PM);
    +    /// BCD unless status B's DM bit (0x04) is set.
    +    fn datasheet_hour_byte(status_b: u8, h: u8) -> u8 {
    +        let binary = status_b & 0x04 != 0;
    +        let enc = |v: u8| if binary { v } else { to_bcd(v) };
    +        if status_b & 0x02 != 0 {
    +            enc(h)
    +        } else {
    +            let h12 = match h % 12 {
    +                0 => 12,
    +                h => h,
    +            };
    +            enc(h12) | if h >= 12 { 0x80 } else { 0 }
    +        }
    +    }
    +
    +    /// Regression test of BUG-11: the hours register follows the MC146818
    +    /// format in every mode. Before the fix, 12-hour mode read noon as 0x12
    +    /// (12 AM) and midnight as 0x00, and a guest write of 12 PM (0x92) decoded
    +    /// as hour 24 and was rejected while 12 AM (0x12) set noon. Now every hour
    +    /// of the day reads as the datasheet encodes it, in 12- and 24-hour mode,
    +    /// BCD and binary; and a guest write of every hours byte (in 12-hour mode
    +    /// 12 AM, 1-11 AM, 12 PM and 1-11 PM) sets the clock to that hour.
    +    #[test]
    +    fn rtc_hours_register_follows_the_datasheet() {
    +        for status_b in [0x02u8, 0x06, 0x00, 0x04] {
    +            for hour in 0..24u8 {
    +                let case = format!("hour {hour}, status B {status_b:#x}");
    +                let at = |h: u8| h as i64 * 3_600_000 + 30 * 60_000;
    +                let (_, _, time, mut rtc) = new_test_rtc();
    +                set_clock_ms(&time, at(hour));
    +                set_cmos_data(&mut rtc, CmosReg::STATUS_B, status_b);
    +                assert_eq!(
    +                    get_cmos_data(&mut rtc, CmosReg::HOUR),
    +                    datasheet_hour_byte(status_b, hour),
    +                    "{case}"
    +                );
    +                // Writing the byte from another hour sets the clock to `hour`
    +                // (the read first syncs the stored byte, so that the write
    +                // changes it).
    +                let (_, _, time, mut rtc) = new_test_rtc();
    +                set_clock_ms(&time, at((hour + 5) % 24));
    +                set_cmos_data(&mut rtc, CmosReg::STATUS_B, status_b);
    +                get_cmos_data(&mut rtc, CmosReg::HOUR);
    +                set_cmos_data(&mut rtc, CmosReg::HOUR, datasheet_hour_byte(status_b, hour));
    +                assert_eq!(
    +                    time.get_time().as_millis_since_unix_epoch(),
    +                    at(hour),
    +                    "{case}"
    +                );
    +            }
    +        }
    +        // The cases by name: noon is 12 PM, midnight 12 AM.
    +        let (_, _, time, mut rtc) = new_test_rtc();
    +        set_clock_ms(&time, 12 * 3_600_000);
    +        set_cmos_data(&mut rtc, CmosReg::STATUS_B, 0x00);
    +        assert_eq!(get_cmos_data(&mut rtc, CmosReg::HOUR), 0x92);
    +        set_cmos_data(&mut rtc, CmosReg::STATUS_B, 0x04);
    +        assert_eq!(get_cmos_data(&mut rtc, CmosReg::HOUR), 0x8c);
    +        let (_, _, time, mut rtc) = new_test_rtc();
    +        set_clock_ms(&time, 0);
    +        set_cmos_data(&mut rtc, CmosReg::STATUS_B, 0x00);
    +        assert_eq!(get_cmos_data(&mut rtc, CmosReg::HOUR), 0x12);
    +        set_cmos_data(&mut rtc, CmosReg::STATUS_B, 0x04);
    +        assert_eq!(get_cmos_data(&mut rtc, CmosReg::HOUR), 0x0c);
    +    }
    +
    +    /// BUG-11 and the alarm: in 12-hour mode, BCD and binary, an alarm for
    +    /// 12:00:00 AM fires at midnight and one for 12:00:00 PM at noon, two
    +    /// seconds after 23:59:58 and 11:59:58, and not at the other twelve
    +    /// o'clock.
    +    #[test]
    +    fn rtc_twelve_hour_alarm_at_midnight_and_noon_fires_on_time() {
    +        for status_b in [0x20u8, 0x24] {
    +            for (start_h, alarm_h, other_start_h) in [(23u8, 0u8, 11u8), (11, 12, 23)] {
    +                for (start, fires) in [(start_h, true), (other_start_h, false)] {
    +                    let case =
    +                        format!("status B {status_b:#x}, alarm hour {alarm_h}, start {start}");
    +                    let (mut pool, mut keeper, time, mut rtc) = new_test_rtc();
    +                    set_clock_ms(&time, start as i64 * 3_600_000 + 59 * 60_000 + 58_000);
    +                    set_cmos_data(&mut rtc, CmosReg::STATUS_B, status_b & !0x20);
    +                    let alarm_hour = datasheet_hour_byte(status_b, alarm_h);
    +                    set_cmos_data(&mut rtc, CmosReg::HOUR_ALARM, alarm_hour);
    +                    set_cmos_data(&mut rtc, CmosReg::MINUTE_ALARM, 0);
    +                    set_cmos_data(&mut rtc, CmosReg::SECOND_ALARM, 0);
    +                    set_cmos_data(&mut rtc, CmosReg::STATUS_B, status_b);
    +                    let due = if fires { 2 } else { 12 * 3600 + 2 };
    +                    assert_eq!(alarm_due_secs(&rtc), due, "{case}");
    +                    let d = Duration::from_millis(2001);
    +                    run_rtc(&mut pool, &mut keeper, &time, &mut rtc, d);
    +                    let status_c = get_cmos_data(&mut rtc, CmosReg::STATUS_C);
    +                    assert_eq!(status_c & 0x20 != 0, fires, "{case}");
    +                }
    +            }
    +        }
    +    }
     }
  2. ppenna commented on Nov 11, 2026

    @ppenna
    Contributor

    Closing: this is an OpenVMM bug outside NVX, and upstream fixed it in microsoft/openvmm#4959, chipset: fix RTC noon and midnight conversion in 12-hour mode, merged on 2026-11-06. NVX's OpenVMM pin, 58d3c3b1d, includes it.

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