hypervisor: add has/set trait for vcpu

Like devicefd, vcpufd also has ability to set/has attribute through kvm
ioctl. These traits are used when enable PMU on arm64, so add it here.

Signed-off-by: Jianyong Wu <jianyong.wu@arm.com>
This commit is contained in:
Jianyong Wu 2022-01-11 14:39:42 +08:00 committed by Xin Wang
parent ae68c2f31a
commit 9bcb984962
3 changed files with 44 additions and 2 deletions

2
Cargo.lock generated
View File

@ -403,7 +403,7 @@ dependencies = [
[[package]] [[package]]
name = "kvm-ioctls" name = "kvm-ioctls"
version = "0.11.0" version = "0.11.0"
source = "git+https://github.com/rust-vmm/kvm-ioctls?branch=main#d22ef1f51852dfb055da38004e1a4fed81246f81" source = "git+https://github.com/rust-vmm/kvm-ioctls?branch=main#8ab2e374e262612020fb0b9f7fbdec839ee7a76f"
dependencies = [ dependencies = [
"kvm-bindings", "kvm-bindings",
"libc", "libc",

View File

@ -19,6 +19,8 @@ use crate::x86_64::{
ExtendedControlRegisters, FpuState, MsrEntries, SpecialRegisters, StandardRegisters, VcpuEvents, ExtendedControlRegisters, FpuState, MsrEntries, SpecialRegisters, StandardRegisters, VcpuEvents,
}; };
use crate::CpuState; use crate::CpuState;
#[cfg(target_arch = "aarch64")]
use crate::DeviceAttr;
#[cfg(feature = "kvm")] #[cfg(feature = "kvm")]
use crate::MpState; use crate::MpState;
#[cfg(all(feature = "mshv", target_arch = "x86_64"))] #[cfg(all(feature = "mshv", target_arch = "x86_64"))]
@ -223,6 +225,16 @@ pub enum HypervisorCpuError {
#[error("Failed to translate GVA: {0}")] #[error("Failed to translate GVA: {0}")]
TranslateVirtualAddress(#[source] anyhow::Error), TranslateVirtualAddress(#[source] anyhow::Error),
/// ///
/// Set cpu attribute error
///
#[error("Failed to set vcpu attribute: {0}")]
SetVcpuAttribute(#[source] anyhow::Error),
///
/// Check if cpu has a certain attribute error
///
#[error("Failed to check if vcpu has attribute: {0}")]
HasVcpuAttribute(#[source] anyhow::Error),
///
/// Failed to initialize TDX on CPU /// Failed to initialize TDX on CPU
/// ///
#[cfg(feature = "tdx")] #[cfg(feature = "tdx")]
@ -259,7 +271,16 @@ pub trait Vcpu: Send + Sync {
/// Returns the vCPU general purpose registers. /// Returns the vCPU general purpose registers.
/// ///
fn get_regs(&self) -> Result<StandardRegisters>; fn get_regs(&self) -> Result<StandardRegisters>;
#[cfg(target_arch = "aarch64")]
///
/// Sets vcpu attribute
///
fn set_vcpu_attr(&self, attr: &DeviceAttr) -> Result<()>;
#[cfg(target_arch = "aarch64")]
///
/// Check if vcpu has attribute.
///
fn has_vcpu_attr(&self, attr: &DeviceAttr) -> Result<()>;
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
/// ///
/// Sets the vCPU general purpose registers. /// Sets the vCPU general purpose registers.

View File

@ -777,6 +777,27 @@ impl cpu::Vcpu for KvmVcpu {
.set_regs(regs) .set_regs(regs)
.map_err(|e| cpu::HypervisorCpuError::SetStandardRegs(e.into())) .map_err(|e| cpu::HypervisorCpuError::SetStandardRegs(e.into()))
} }
#[cfg(target_arch = "aarch64")]
///
/// Set attribute for vcpu.
///
fn set_vcpu_attr(&self, attr: &DeviceAttr) -> cpu::Result<()> {
self.fd
.set_device_attr(attr)
.map_err(|e| cpu::HypervisorCpuError::SetVcpuAttribute(e.into()))
}
#[cfg(target_arch = "aarch64")]
///
/// Check if vcpu has a certain attribute.
///
fn has_vcpu_attr(&self, attr: &DeviceAttr) -> cpu::Result<()> {
self.fd
.has_device_attr(attr)
.map_err(|e| cpu::HypervisorCpuError::HasVcpuAttribute(e.into()))
}
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
/// ///
/// Returns the vCPU special registers. /// Returns the vCPU special registers.