From feb0a36067bc930360cd110d50308539906dfbe6 Mon Sep 17 00:00:00 2001 From: Jinank Jain Date: Thu, 1 Aug 2024 12:00:59 +0000 Subject: [PATCH] hypervisor: Implement StandardRegisters as union of KVM & MSHV Currently we are redefining StandardRegisters instead of using the ones coming from bindings. With this we can remove the unnecessary construction of global structure which contains fields from different hypervisor dependent structs. Signed-off-by: Jinank Jain --- hypervisor/src/kvm/mod.rs | 17 +++++++++++++++++ hypervisor/src/lib.rs | 8 ++++++++ hypervisor/src/mshv/mod.rs | 17 +++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/hypervisor/src/kvm/mod.rs b/hypervisor/src/kvm/mod.rs index 3f8fc4469..ac6d1472f 100644 --- a/hypervisor/src/kvm/mod.rs +++ b/hypervisor/src/kvm/mod.rs @@ -337,6 +337,23 @@ impl From for kvm_clock_data { } } +impl From for crate::StandardRegisters { + fn from(s: kvm_bindings::kvm_regs) -> Self { + crate::StandardRegisters::Kvm(s) + } +} + +impl From for kvm_bindings::kvm_regs { + fn from(e: crate::StandardRegisters) -> Self { + match e { + crate::StandardRegisters::Kvm(e) => e, + /* Needed in case other hypervisors are enabled */ + #[allow(unreachable_patterns)] + _ => panic!("StandardRegisters are not valid"), + } + } +} + impl From for IrqRoutingEntry { fn from(s: kvm_irq_routing_entry) -> Self { IrqRoutingEntry::Kvm(s) diff --git a/hypervisor/src/lib.rs b/hypervisor/src/lib.rs index 0c4d78d0a..f43f5264b 100644 --- a/hypervisor/src/lib.rs +++ b/hypervisor/src/lib.rs @@ -187,3 +187,11 @@ pub enum IrqRoutingEntry { #[cfg(feature = "mshv")] Mshv(mshv_bindings::mshv_msi_routing_entry), } + +#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)] +pub enum StandardRegisters { + #[cfg(feature = "kvm")] + Kvm(kvm_bindings::kvm_regs), + #[cfg(all(feature = "mshv", target_arch = "x86_64"))] + Mshv(mshv_bindings::StandardRegisters), +} diff --git a/hypervisor/src/mshv/mod.rs b/hypervisor/src/mshv/mod.rs index 19dfe0a9a..54329d382 100644 --- a/hypervisor/src/mshv/mod.rs +++ b/hypervisor/src/mshv/mod.rs @@ -165,6 +165,23 @@ impl From for VcpuMshvState { } } +impl From for crate::StandardRegisters { + fn from(s: mshv_bindings::StandardRegisters) -> Self { + crate::StandardRegisters::Mshv(s) + } +} + +impl From for mshv_bindings::StandardRegisters { + fn from(e: crate::StandardRegisters) -> Self { + match e { + crate::StandardRegisters::Mshv(e) => e, + /* Needed in case other hypervisors are enabled */ + #[allow(unreachable_patterns)] + _ => panic!("StandardRegisters are not valid"), + } + } +} + impl From for IrqRoutingEntry { fn from(s: mshv_msi_routing_entry) -> Self { IrqRoutingEntry::Mshv(s)