From 63f356b75bbad85bacb76053d333601e8c16afdf Mon Sep 17 00:00:00 2001 From: Muminul Islam Date: Fri, 4 Dec 2020 11:41:06 -0800 Subject: [PATCH] hypervisor: mhsv: Define software emulated TLB A software emulated TLB. This is mostly used by the instruction emulator to cache gva to gpa translations passed from the hypervisor. Co-Developed-by: Nuno Das Neves Signed-off-by: Nuno Das Neves Co-Developed-by: Praveen Paladugu Signed-off-by: Praveen Paladugu Co-Developed-by: Samuel Ortiz Signed-off-by: Samuel Ortiz Co-Developed-by: Wei Liu Signed-off-by: Wei Liu Signed-off-by: Muminul Islam --- hypervisor/src/mshv/mod.rs | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/hypervisor/src/mshv/mod.rs b/hypervisor/src/mshv/mod.rs index 0a2f134fd..196e76095 100644 --- a/hypervisor/src/mshv/mod.rs +++ b/hypervisor/src/mshv/mod.rs @@ -327,6 +327,46 @@ impl hypervisor::Hypervisor for MshvHypervisor { } } +#[derive(Clone)] +// A software emulated TLB. +// This is mostly used by the instruction emulator to cache gva to gpa translations +// passed from the hypervisor. +struct SoftTLB { + addr_map: HashMap, +} + +impl SoftTLB { + fn new() -> SoftTLB { + SoftTLB { + addr_map: HashMap::new(), + } + } + + // Adds a gva -> gpa mapping into the TLB. + fn add_mapping(&mut self, gva: u64, gpa: u64) -> Result<(), PlatformError> { + *self.addr_map.entry(gva).or_insert(gpa) = gpa; + Ok(()) + } + + // Do the actual gva -> gpa translation + fn translate(&self, gva: u64) -> Result { + self.addr_map + .get(&gva) + .ok_or_else(|| PlatformError::UnmappedGVA(anyhow!("{:#?}", gva))) + .map(|v| *v) + + // TODO Check if we could fallback to e.g. an hypercall for doing + // the translation for us. + } + + // FLush the TLB, all mappings are removed. + fn flush(&mut self) -> Result<(), PlatformError> { + self.addr_map.clear(); + + Ok(()) + } +} + #[allow(clippy::type_complexity)] /// Vcpu struct for Microsoft Hypervisor pub struct MshvVcpu {