From 603ca0e21b2eeb9a4bd35b958f8ebc043bcc0d0a Mon Sep 17 00:00:00 2001 From: Akira Moroo Date: Sun, 20 Feb 2022 11:44:25 +0900 Subject: [PATCH] hypervisor: Add `translate_gva` for x86/KVM This commit adds `translate_gva` for x86/KVM. The same name function is already implemented for MSHV, but the implementation differs as KVM_TRANSLATE does not take the flag argument and does not return status code. This change requires the newer version of kvm-ioctls [1]. [1] https://github.com/rust-vmm/kvm-ioctls/commit/97ff779b6ea96ae451308149b0ae38f13b271c19 Signed-off-by: Akira Moroo --- hypervisor/src/cpu.rs | 2 +- hypervisor/src/kvm/mod.rs | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/hypervisor/src/cpu.rs b/hypervisor/src/cpu.rs index c1e255c5c..abee886a3 100644 --- a/hypervisor/src/cpu.rs +++ b/hypervisor/src/cpu.rs @@ -457,7 +457,7 @@ pub trait Vcpu: Send + Sync { /// Triggers the running of the current virtual CPU returning an exit reason. /// fn run(&self) -> std::result::Result; - #[cfg(all(feature = "mshv", target_arch = "x86_64"))] + #[cfg(target_arch = "x86_64")] /// /// Translate guest virtual address to guest physical address /// diff --git a/hypervisor/src/kvm/mod.rs b/hypervisor/src/kvm/mod.rs index 6959439e1..6ed9696d2 100644 --- a/hypervisor/src/kvm/mod.rs +++ b/hypervisor/src/kvm/mod.rs @@ -981,6 +981,24 @@ impl cpu::Vcpu for KvmVcpu { .set_xcrs(xcrs) .map_err(|e| cpu::HypervisorCpuError::SetXcsr(e.into())) } + #[cfg(target_arch = "x86_64")] + /// + /// Translates guest virtual address to guest physical address using the `KVM_TRANSLATE` ioctl. + /// + fn translate_gva(&self, gva: u64, _flags: u64) -> cpu::Result<(u64, u32)> { + let tr = self + .fd + .translate_gva(gva) + .map_err(|e| cpu::HypervisorCpuError::TranslateVirtualAddress(e.into()))?; + // tr.valid is set if the GVA is mapped to valid GPA. + match tr.valid { + 0 => Err(cpu::HypervisorCpuError::TranslateVirtualAddress(anyhow!( + "Invalid GVA: {:#x}", + gva + ))), + _ => Ok((tr.physical_address, 0)), + } + } /// /// Triggers the running of the current virtual CPU returning an exit reason. ///