hypervisor: aarch64: capture the guest counter for snapshot/restore

Unlike x86, ARM64 has no kvmclock support to sync guest time upon
required. However, the guest reads the architected virtual timer
(CNTVCT_EL0) directly which can be modified by the VMM to update the
time after snapshot restore. Since the CNTVCT is in ticks, we also need
to read CNTFRQ (via mrs due to lack of ONEREG interface) to compute the
ticks from wall clock difference.

Because the counter is a vCPU register, the capture must run with the
vCPUs quiesced, so the VMM now captures the clock just after
cpu_manager.pause() through the boot vCPU. This is behaviorally
identical for x86, whose clock is VM-wide. There is no restore/advance
yet, so aarch64 guests still resume behind real time until the following
commit.

Signed-off-by: Atish Patra <atishp@meta.com>
This commit is contained in:
Atish Patra
2026-06-11 15:25:08 -07:00
committed by Rob Bradford
parent ad909a3d71
commit 69637dde69
8 changed files with 144 additions and 28 deletions

View File

@@ -23,7 +23,8 @@ use thiserror::Error;
use vmm_sys_util::eventfd::EventFd;
#[cfg(target_arch = "x86_64")]
use crate::{ClockData, ClockState};
use crate::ClockData;
use crate::ClockState;
#[cfg(target_arch = "aarch64")]
use crate::arch::aarch64::gic::{Vgic, VgicConfig};
#[cfg(target_arch = "riscv64")]
@@ -140,6 +141,12 @@ pub enum HypervisorVmError {
#[error("Failed to set clock")]
SetClock(#[source] anyhow::Error),
///
/// Capture guest timer state error (aarch64)
///
#[cfg(all(target_arch = "aarch64", feature = "kvm"))]
#[error("Failed to capture the guest timer state")]
CaptureTimerState(#[source] anyhow::Error),
///
/// Create passthrough device
///
#[error("Failed to create passthrough device")]
@@ -385,9 +392,10 @@ pub trait Vm: Send + Sync + Any {
#[cfg(target_arch = "x86_64")]
fn set_clock(&self, data: &ClockData) -> Result<()>;
/// Capture the guest clock for snapshot/migration while the VM is paused.
/// `Ok(None)` means this backend has no clock to preserve.
#[cfg(target_arch = "x86_64")]
fn snapshot_clock(&self) -> Result<Option<ClockState>> {
/// `Ok(None)` means this backend has no clock to preserve. `boot_vcpu` is the
/// boot vCPU, used by backends whose clock is per-vCPU state (e.g the aarch64
/// counter);
fn snapshot_clock(&self, _boot_vcpu: &dyn Vcpu) -> Result<Option<ClockState>> {
Ok(None)
}
/// Re-establish the guest clock before the vCPUs resume.