hypervisor: implement clock data for MSHV

This PR implement time reference for Microsoft
Hypervisor based partition/VM.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
This commit is contained in:
Muminul Islam 2024-04-17 11:42:55 -07:00 committed by Bo Chen
parent bf6c9e6447
commit 4847f5c4f6
3 changed files with 38 additions and 4 deletions

View File

@ -165,7 +165,7 @@ pub enum ClockData {
#[cfg(feature = "kvm")] #[cfg(feature = "kvm")]
Kvm(kvm_bindings::kvm_clock_data), Kvm(kvm_bindings::kvm_clock_data),
#[cfg(feature = "mshv")] #[cfg(feature = "mshv")]
Mshv, /* MSHV does not support ClockData yet */ Mshv(mshv::MshvClockData),
} }
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]

View File

@ -92,6 +92,25 @@ impl From<mshv_user_mem_region> for UserMemoryRegion {
} }
} }
#[cfg(target_arch = "x86_64")]
impl From<MshvClockData> for ClockData {
fn from(d: MshvClockData) -> Self {
ClockData::Mshv(d)
}
}
#[cfg(target_arch = "x86_64")]
impl From<ClockData> for MshvClockData {
fn from(ms: ClockData) -> Self {
match ms {
ClockData::Mshv(s) => s,
/* Needed in case other hypervisors are enabled */
#[allow(unreachable_patterns)]
_ => unreachable!("MSHV clock data is not valid"),
}
}
}
impl From<UserMemoryRegion> for mshv_user_mem_region { impl From<UserMemoryRegion> for mshv_user_mem_region {
fn from(region: UserMemoryRegion) -> Self { fn from(region: UserMemoryRegion) -> Self {
let mut flags: u32 = 0; let mut flags: u32 = 0;
@ -1930,13 +1949,23 @@ impl vm::Vm for MshvVm {
/// Retrieve guest clock. /// Retrieve guest clock.
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
fn get_clock(&self) -> vm::Result<ClockData> { fn get_clock(&self) -> vm::Result<ClockData> {
Ok(ClockData::Mshv) let val = self
.fd
.get_partition_property(hv_partition_property_code_HV_PARTITION_PROPERTY_REFERENCE_TIME)
.map_err(|e| vm::HypervisorVmError::GetClock(e.into()))?;
Ok(MshvClockData { ref_time: val }.into())
} }
/// Set guest clock. /// Set guest clock.
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
fn set_clock(&self, _data: &ClockData) -> vm::Result<()> { fn set_clock(&self, data: &ClockData) -> vm::Result<()> {
Ok(()) let data: MshvClockData = (*data).into();
self.fd
.set_partition_property(
hv_partition_property_code_HV_PARTITION_PROPERTY_REFERENCE_TIME,
data.ref_time,
)
.map_err(|e| vm::HypervisorVmError::SetClock(e.into()))
} }
/// Downcast to the underlying MshvVm type /// Downcast to the underlying MshvVm type

View File

@ -43,6 +43,11 @@ pub struct VcpuMshvState {
pub vp_states: AllVpStateComponents, pub vp_states: AllVpStateComponents,
} }
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize)]
pub struct MshvClockData {
pub ref_time: u64,
}
impl fmt::Display for VcpuMshvState { impl fmt::Display for VcpuMshvState {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let expected_num_msrs = self.msrs.len(); let expected_num_msrs = self.msrs.len();