hypervisor, vmm: introduce trait functions for aarch64 PMU

The original code uses kvm_device_attr directly outside of the
hyeprvisor crate. That leaks hypervisor details.

No functional change intended.

Signed-off-by: Wei Liu <liuwe@microsoft.com>
This commit is contained in:
Wei Liu 2022-07-20 11:42:24 +00:00 committed by Liu Wei
parent b5d10eb28b
commit f84ddedb1a
3 changed files with 49 additions and 30 deletions

View File

@ -236,6 +236,12 @@ pub enum HypervisorCpuError {
#[cfg(feature = "tdx")] #[cfg(feature = "tdx")]
#[error("Unknown TDX VM call")] #[error("Unknown TDX VM call")]
UnknownTdxVmCall, UnknownTdxVmCall,
#[cfg(target_arch = "aarch64")]
///
/// Failed to intialize PMU
///
#[error("Failed to initialize PMU")]
InitializePmu,
} }
#[derive(Debug)] #[derive(Debug)]
@ -400,6 +406,16 @@ pub trait Vcpu: Send + Sync {
#[cfg(target_arch = "aarch64")] #[cfg(target_arch = "aarch64")]
fn setup_regs(&self, cpu_id: u8, boot_ip: u64, fdt_start: u64) -> Result<()>; fn setup_regs(&self, cpu_id: u8, boot_ip: u64, fdt_start: u64) -> Result<()>;
/// ///
/// Check if the CPU supports PMU
///
#[cfg(target_arch = "aarch64")]
fn has_pmu_support(&self) -> bool;
///
/// Initialize PMU
///
#[cfg(target_arch = "aarch64")]
fn init_pmu(&self, irq: u32) -> Result<()>;
///
/// Retrieve the vCPU state. /// Retrieve the vCPU state.
/// This function is necessary to snapshot the VM /// This function is necessary to snapshot the VM
/// ///

View File

@ -2063,6 +2063,35 @@ impl cpu::Vcpu for KvmVcpu {
] ]
.to_vec() .to_vec()
} }
#[cfg(target_arch = "aarch64")]
fn has_pmu_support(&self) -> bool {
let cpu_attr = kvm_bindings::kvm_device_attr {
group: kvm_bindings::KVM_ARM_VCPU_PMU_V3_CTRL,
attr: u64::from(kvm_bindings::KVM_ARM_VCPU_PMU_V3_INIT),
addr: 0x0,
flags: 0,
};
self.has_vcpu_attr(&cpu_attr).is_ok()
}
#[cfg(target_arch = "aarch64")]
fn init_pmu(&self, irq: u32) -> cpu::Result<()> {
let cpu_attr = kvm_bindings::kvm_device_attr {
group: kvm_bindings::KVM_ARM_VCPU_PMU_V3_CTRL,
attr: u64::from(kvm_bindings::KVM_ARM_VCPU_PMU_V3_INIT),
addr: 0x0,
flags: 0,
};
let cpu_attr_irq = kvm_bindings::kvm_device_attr {
group: kvm_bindings::KVM_ARM_VCPU_PMU_V3_CTRL,
attr: u64::from(kvm_bindings::KVM_ARM_VCPU_PMU_V3_IRQ),
addr: &irq as *const u32 as u64,
flags: 0,
};
self.set_vcpu_attr(&cpu_attr_irq)
.map_err(|_| cpu::HypervisorCpuError::InitializePmu)?;
self.set_vcpu_attr(&cpu_attr)
.map_err(|_| cpu::HypervisorCpuError::InitializePmu)
}
} }
impl KvmVcpu { impl KvmVcpu {

View File

@ -787,41 +787,15 @@ impl CpuManager {
#[cfg(target_arch = "aarch64")] #[cfg(target_arch = "aarch64")]
pub fn init_pmu(&self, irq: u32) -> Result<bool> { pub fn init_pmu(&self, irq: u32) -> Result<bool> {
let cpu_attr = kvm_bindings::kvm_device_attr {
group: kvm_bindings::KVM_ARM_VCPU_PMU_V3_CTRL,
attr: u64::from(kvm_bindings::KVM_ARM_VCPU_PMU_V3_INIT),
addr: 0x0,
flags: 0,
};
for cpu in self.vcpus.iter() { for cpu in self.vcpus.iter() {
let tmp = irq; let cpu = cpu.lock().unwrap();
let cpu_attr_irq = kvm_bindings::kvm_device_attr {
group: kvm_bindings::KVM_ARM_VCPU_PMU_V3_CTRL,
attr: u64::from(kvm_bindings::KVM_ARM_VCPU_PMU_V3_IRQ),
addr: &tmp as *const u32 as u64,
flags: 0,
};
// Check if PMU attr is available, if not, log the information. // Check if PMU attr is available, if not, log the information.
if cpu.lock().unwrap().vcpu.has_vcpu_attr(&cpu_attr).is_ok() { if cpu.vcpu.has_pmu_support() {
// Set irq for PMU cpu.vcpu.init_pmu(irq).map_err(Error::InitPmu)?;
cpu.lock()
.unwrap()
.vcpu
.set_vcpu_attr(&cpu_attr_irq)
.map_err(Error::InitPmu)?;
// Init PMU
cpu.lock()
.unwrap()
.vcpu
.set_vcpu_attr(&cpu_attr)
.map_err(Error::InitPmu)?;
} else { } else {
debug!( debug!(
"PMU attribute is not supported in vCPU{}, skip PMU init!", "PMU attribute is not supported in vCPU{}, skip PMU init!",
cpu.lock().unwrap().id cpu.id
); );
return Ok(false); return Ok(false);
} }