diff --git a/hypervisor/src/mshv/mod.rs b/hypervisor/src/mshv/mod.rs index 32ef68bc5..502c36f10 100644 --- a/hypervisor/src/mshv/mod.rs +++ b/hypervisor/src/mshv/mod.rs @@ -737,11 +737,7 @@ impl cpu::Vcpu for MshvVcpu { let mut context = MshvEmulatorContext { vcpu: self, - map: if gva_gpa_valid { - (gva, gpa) - } else { - (u64::MAX, 0) - }, + mapping: gva_gpa_valid.then_some((gva, gpa)), }; let old_state = context diff --git a/hypervisor/src/mshv/x86_64/emulator.rs b/hypervisor/src/mshv/x86_64/emulator.rs index eb2be3d2f..211d0cb8e 100644 --- a/hypervisor/src/mshv/x86_64/emulator.rs +++ b/hypervisor/src/mshv/x86_64/emulator.rs @@ -15,20 +15,20 @@ use crate::mshv::MshvVcpu; pub struct MshvEmulatorContext<'a> { pub vcpu: &'a MshvVcpu, - pub map: (u64, u64), // Initial GVA to GPA mapping provided by the hypervisor + /// Initial (GVA, GPA) mapping provided by the hypervisor if the hypervisor provided a + /// valid mapping. Used as a fast path in [`MshvEmulatorContext::translate`] to avoid a + /// translate hypercall. `None` when the hypervisor did not provide a valid mapping. + pub mapping: Option<(u64, u64)>, } impl MshvEmulatorContext<'_> { - // Do the actual gva -> gpa translation. - // - // When the hypervisor sets GvaGpaValid in the intercept message, `map` - // caches the (gva, gpa) pair as a fast path that avoids a translate - // hypercall. When the flag is clear, `map` is set to a sentinel - // (u64::MAX, 0) so this shortcut never fires. + // Do the actual gva -> gpa translation #[allow(non_upper_case_globals)] fn translate(&self, gva: u64, flags: u32) -> Result { - if self.map.0 == gva { - return Ok(self.map.1); + if let Some((cached_gva, cached_gpa)) = self.mapping + && cached_gva == gva + { + return Ok(cached_gpa); } let (gpa, result_code) = self