hypervisor: mshv: Make the translation caching code idiomatic

Signed-off-by: Wei Liu <liuwe@microsoft.com>
This commit is contained in:
Wei Liu
2026-04-28 04:11:39 +00:00
committed by Wei Liu
parent b03e186270
commit 389257964b
2 changed files with 10 additions and 14 deletions

View File

@@ -737,11 +737,7 @@ impl cpu::Vcpu for MshvVcpu {
let mut context = MshvEmulatorContext { let mut context = MshvEmulatorContext {
vcpu: self, vcpu: self,
map: if gva_gpa_valid { mapping: gva_gpa_valid.then_some((gva, gpa)),
(gva, gpa)
} else {
(u64::MAX, 0)
},
}; };
let old_state = context let old_state = context

View File

@@ -15,20 +15,20 @@ use crate::mshv::MshvVcpu;
pub struct MshvEmulatorContext<'a> { pub struct MshvEmulatorContext<'a> {
pub vcpu: &'a MshvVcpu, 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<'_> { impl MshvEmulatorContext<'_> {
// Do the actual gva -> gpa translation. // 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.
#[allow(non_upper_case_globals)] #[allow(non_upper_case_globals)]
fn translate(&self, gva: u64, flags: u32) -> Result<u64, PlatformError> { fn translate(&self, gva: u64, flags: u32) -> Result<u64, PlatformError> {
if self.map.0 == gva { if let Some((cached_gva, cached_gpa)) = self.mapping
return Ok(self.map.1); && cached_gva == gva
{
return Ok(cached_gpa);
} }
let (gpa, result_code) = self let (gpa, result_code) = self