From c452471c4e9b7727b1732ec8374f0a9da730b066 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Sat, 4 Dec 2021 14:15:41 +0100 Subject: [PATCH] hypervisor: Add support for setting KVM identity map Extending the Vm trait with set_identity_map_address() in order to expose this ioctl to the VMM. Signed-off-by: Sebastien Boeuf --- hypervisor/src/kvm/mod.rs | 9 +++++++++ hypervisor/src/kvm/x86_64/mod.rs | 3 +++ hypervisor/src/mshv/mod.rs | 7 +++++++ hypervisor/src/vm.rs | 8 ++++++++ 4 files changed, 27 insertions(+) diff --git a/hypervisor/src/kvm/mod.rs b/hypervisor/src/kvm/mod.rs index 3c6d5912e..07191b74a 100644 --- a/hypervisor/src/kvm/mod.rs +++ b/hypervisor/src/kvm/mod.rs @@ -135,6 +135,15 @@ pub struct KvmVm { /// vm.set/get().unwrap() /// impl vm::Vm for KvmVm { + #[cfg(target_arch = "x86_64")] + /// + /// Sets the address of the one-page region in the VM's address space. + /// + fn set_identity_map_address(&self, address: u64) -> vm::Result<()> { + self.fd + .set_identity_map_address(address) + .map_err(|e| vm::HypervisorVmError::SetIdentityMapAddress(e.into())) + } #[cfg(target_arch = "x86_64")] /// /// Sets the address of the three-page region in the VM's address space. diff --git a/hypervisor/src/kvm/x86_64/mod.rs b/hypervisor/src/kvm/x86_64/mod.rs index 3b9973ef3..a2daa09c7 100644 --- a/hypervisor/src/kvm/x86_64/mod.rs +++ b/hypervisor/src/kvm/x86_64/mod.rs @@ -124,6 +124,9 @@ pub fn check_required_kvm_extensions(kvm: &Kvm) -> KvmResult<()> { if !kvm.check_extension(Cap::SplitIrqchip) { return Err(KvmError::CapabilityMissing(Cap::SplitIrqchip)); } + if !kvm.check_extension(Cap::SetIdentityMapAddr) { + return Err(KvmError::CapabilityMissing(Cap::SetIdentityMapAddr)); + } if !kvm.check_extension(Cap::SetTssAddr) { return Err(KvmError::CapabilityMissing(Cap::SetTssAddr)); } diff --git a/hypervisor/src/mshv/mod.rs b/hypervisor/src/mshv/mod.rs index 8647a4290..fe963f9a6 100644 --- a/hypervisor/src/mshv/mod.rs +++ b/hypervisor/src/mshv/mod.rs @@ -764,6 +764,13 @@ fn hv_state_init() -> Arc> { /// vm.set/get().unwrap() /// impl vm::Vm for MshvVm { + #[cfg(target_arch = "x86_64")] + /// + /// Sets the address of the one-page region in the VM's address space. + /// + fn set_identity_map_address(&self, _address: u64) -> vm::Result<()> { + Ok(()) + } #[cfg(target_arch = "x86_64")] /// /// Sets the address of the three-page region in the VM's address space. diff --git a/hypervisor/src/vm.rs b/hypervisor/src/vm.rs index 7837dd4cd..6b30a99d1 100644 --- a/hypervisor/src/vm.rs +++ b/hypervisor/src/vm.rs @@ -58,6 +58,11 @@ pub enum HypervisorVmError { #[error("Failed to create Vcpu: {0}")] CreateVcpu(#[source] anyhow::Error), /// + /// Identity map address error + /// + #[error("Failed to set identity map address: {0}")] + SetIdentityMapAddress(#[source] anyhow::Error), + /// /// TSS address error /// #[error("Failed to set TSS address: {0}")] @@ -217,6 +222,9 @@ pub type Result = std::result::Result; /// This crate provides a hypervisor-agnostic interfaces for Vm /// pub trait Vm: Send + Sync { + #[cfg(target_arch = "x86_64")] + /// Sets the address of the one-page region in the VM's address space. + fn set_identity_map_address(&self, address: u64) -> Result<()>; #[cfg(target_arch = "x86_64")] /// Sets the address of the three-page region in the VM's address space. fn set_tss_address(&self, offset: usize) -> Result<()>;