virtio-devices: Implement virtio-device rtc

This change will allow us to get accurate time over ptp in guests
started from a MSHV-virtualized Linux host. Implementing it as a
virtio device is preferable to using the existing kvm_ptp because:

kvm_ptp relies on hypercalls that only exist on host kernels running
kvm. Virtio-rtc gives us more flexibility in what clock types we want
to provide. We can later extend the device to implement multiple clocks
(smeared UTC, TAI, monotonic, etc.). Virtio-rtc protocol supports
alarms. Alarms may later enable usecases where the guests can do their
own VM lifecycle management without relying on a host-side
orchestrator.

Implement device backend for virtio-rtc. Currently this implementation
encompasses:

1. CONFIG, CAP, READ, CROSSCAP (returns false)
2. One PTP clock is presented of type
VIRTIO_RTC_CLOCK_UTC_MAYBE_SMEARED with leap_second_smearing
VIRTIO_RTC_SMEAR_UNSPECIFIED

The device is disabled by default, requiring --rtc to be passed

Not implemented but theoretically supported by virtio-rtc is:

1. Cross-timestamping support
2. The alarm queue

Fixes #7730

Signed-off-by: Cameron Baird <cameronbaird@microsoft.com>
This commit is contained in:
Cameron Baird
2026-03-03 23:49:38 +00:00
committed by Wei Liu
parent 1e18716fbd
commit b452440f6c
11 changed files with 858 additions and 3 deletions

View File

@@ -139,6 +139,7 @@ const DEBUGCON_DEVICE_NAME: &str = "__debug_console";
#[cfg(target_arch = "aarch64")]
const GPIO_DEVICE_NAME: &str = "__gpio";
const RNG_DEVICE_NAME: &str = "__rng";
const RTC_DEVICE_NAME: &str = "__rtc";
const IOMMU_DEVICE_NAME: &str = "__iommu";
#[cfg(feature = "pvmemcontrol")]
const PVMEMCONTROL_DEVICE_NAME: &str = "__pvmemcontrol";
@@ -193,6 +194,10 @@ pub enum DeviceManagerError {
#[error("Cannot create virtio-rng device")]
CreateVirtioRng(#[source] io::Error),
/// Cannot create virtio-rtc device
#[error("Cannot create virtio-rtc device")]
CreateVirtioRtc(#[source] io::Error),
/// Cannot create generic vhost-user device
#[error("Cannot create generic vhost-user device")]
CreateGenericVhostUser(#[source] virtio_devices::vhost_user::Error),
@@ -2654,6 +2659,9 @@ impl DeviceManager {
// Add vDPA devices if required
self.make_vdpa_devices(snapshot)?;
// Add virtio-rtc device
self.make_virtio_rtc_devices(snapshot)?;
Ok(())
}
/// Creates a [`MetaVirtioDevice`] from the provided [`DiskConfig`].
@@ -3116,6 +3124,53 @@ impl DeviceManager {
Ok(())
}
fn make_virtio_rtc_devices(&mut self, snapshot: Option<&Snapshot>) -> DeviceManagerResult<()> {
let Some(mut rtc_config) = self.config.lock().unwrap().rtc.clone() else {
return Ok(());
};
info!("Creating virtio-rtc device: {rtc_config:?}");
let id = match rtc_config.pci_common.id.as_ref() {
Some(id) => id.clone(),
None => rtc_config
.pci_common
.id
.insert(RTC_DEVICE_NAME.to_string())
.clone(),
};
let virtio_rtc_device = Arc::new(Mutex::new(
virtio_devices::Rtc::new(
id.clone(),
self.force_access_platform | rtc_config.pci_common.iommu,
self.seccomp_action.clone(),
self.exit_evt
.try_clone()
.map_err(DeviceManagerError::EventFd)?,
state_from_id(snapshot, id.as_str())
.map_err(DeviceManagerError::RestoreGetState)?,
)
.map_err(DeviceManagerError::CreateVirtioRtc)?,
));
self.virtio_devices.push(MetaVirtioDevice {
virtio_device: Arc::clone(&virtio_rtc_device)
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
pci_common: rtc_config.pci_common.clone(),
dma_handler: None,
});
// Fill the device tree with a new node. In case of restore, we
// know there is nothing to do, so we can simply override the
// existing entry.
self.device_tree
.lock()
.unwrap()
.insert(id.clone(), device_node!(id, virtio_rtc_device));
Ok(())
}
fn make_generic_vhost_user_device(
&mut self,
generic_vhost_user_cfg: &mut GenericVhostUserConfig,