Skip to content

openvmm: CMOS RTC snapshot save/restore omits sub-second timer deadlines, UIP phase, and stopped-boundary alignment #1163

Closed

Description

@tangruize

vm/devices/chipset/src/cmos_rtc.rs saves the CMOS register array (state.cmos), clock_time_millis, and time_valid, but omits runtime sub-second timer deadlines and phase state. Restoring an RTC therefore alters guest-observable timing in five related ways:

  1. Lost pending alarm within its matching second (run-1-rtc-3): Rtc::set_alarm_timer reconstructs the alarm timeout from whole-second CMOS calendar fields:
    fn set_alarm_timer(&mut self, now: VmTime) {
        self.sync_clock_to_cmos();
        let alarm_duration = self.calculate_alarm_duration();

        self.vmtime_alarm
            .set_timeout(now.wrapping_add(alarm_duration));
    }

If an alarm is armed for 00:01:00 (at VM time 60.200s) and the snapshot is taken at 00:01:00.100 (60.100s) before the alarm fires, the restored calendar already matches 00:01:00, so calculate_alarm_duration schedules the next alarm 24 hours later instead of 100 ms later.

  1. Periodic and update-ended timer phase reset (run-1-rtc-1): During Rtc::restore, update_timers calls set_periodic_timer() and set_update_timer(), which start fresh full periods from the current VmTime instead of preserving the remaining sub-period deadline.

  2. Synthetic Status Register A UIP transition on first read (run-1-rtc-2): Rtc::get_status_byte tracks the 1-second update-in-progress (UIP) blip window using a runtime-only field self.last_update_bit_blip, which defaults to 0 on a fresh Rtc instance and is not saved in SavedState. The first Status A read after restore therefore sees elapsed_millis > 1000 and triggers a synthetic UIP blip and sync_clock_to_cmos().

  3. Cached CMOS calendar byte refresh during restore (run-1-rtc-4): When an alarm is enabled, Rtc::restore -> update_timers -> set_alarm_timer calls sync_clock_to_cmos(), overwriting the saved state.cmos calendar cache. Because Rtc::set_cmos_byte ignores writes where data == self.state.cmos[addr], refreshing the cache during restore changes whether a subsequent guest byte write is treated as a no-op or a clock update.

  4. Wall-clock drift while VM time is stopped (run-1-rtc-7): SystemTimeClock continues advancing with host wall time between VmTime stop and Rtc::save, and again between Rtc::restore_clock and Rtc::advance_clock, so applying downtime adds intervals already elapsed on the host clock.

Proposed Fix

  • Decide which sub-second RTC invariants the snapshot contract guarantees. To preserve pending alarms, periodic/update timer phases, and UIP behavior without mutating cached CMOS bytes on restore, include optional deadline/phase fields in cmos_rtc::save_restore::state::SavedState (with a fallback to current rearming behavior for older snapshots).
  • Sample and restore the RTC epoch at the same stopped boundary as VmTime so host save/restore work is not double-counted alongside explicit downtime advancement.

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