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. ///