hypervisor: add a generic guest-clock save/restore abstraction

Preserving the guest clock across pause/resume and snapshot/restore is
currently open-coded in the VMM against the x86-only
get_clock/set_clock. aarch64 needs the same correction but via a
different mechanism (i.e. the architected counter, CNTVCT). Having a
common backend-agnostic interface that VMM can drive uniformly allows us
to keep the architecture details behind the Hypervisor abstraction.

This commit only introduces the abstraction while the future commits
will actually move the implementation to use it.

Use this opportunity to fix the full path to get SystemTime as well.

Suggested-by: Sebastien Boeuf <sboeuf@meta.com>
Signed-off-by: Atish Patra <atishp@meta.com>
This commit is contained in:
Atish Patra
2026-06-11 14:51:01 -07:00
committed by Rob Bradford
parent 00edf5b34d
commit d68b93ea93
4 changed files with 56 additions and 4 deletions

View File

@@ -49,6 +49,8 @@ mod cpu;
mod device;
use std::sync::Arc;
#[cfg(target_arch = "x86_64")]
use std::time::SystemTime;
use anyhow::anyhow;
use concat_idents::concat_idents;
@@ -196,7 +198,7 @@ impl ClockData {
}
}
pub fn set_realtime(&mut self, realtime: std::time::SystemTime) {
pub fn set_realtime(&mut self, realtime: SystemTime) {
match self {
#[cfg(feature = "kvm")]
ClockData::Kvm(s) => {
@@ -211,8 +213,23 @@ impl ClockData {
}
}
}
/// Returns the clock with `CLOCK_REALTIME` filled from the host wall clock
/// when absent, so a later restore can advance the guest to current wall
/// time. No-op for backends without a realtime field (e.g. MSHV).
pub fn with_realtime_filled(mut self) -> Self {
if !self.has_realtime() {
self.set_realtime(SystemTime::now());
}
self
}
}
/// Guest clock state preserved across pause/resume and snapshot/restore
/// (`ClockData` on x86).
#[cfg(target_arch = "x86_64")]
pub type ClockState = ClockData;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct HypervisorVmConfig {
#[cfg(feature = "tdx")]