From f2b1fbf5607bce8442e75e5bbedd2252527019ec Mon Sep 17 00:00:00 2001 From: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com> Date: Tue, 25 Nov 2025 10:30:04 -0800 Subject: [PATCH] Move sregs to its own file Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com> --- src/hyperlight_host/src/error.rs | 3 - .../src/hypervisor/gdb/mshv_debug.rs | 5 - .../src/hypervisor/hyperv_linux.rs | 6 +- src/hyperlight_host/src/hypervisor/mod.rs | 76 +-------------- .../src/hypervisor/regs/fpu.rs | 4 - .../src/hypervisor/regs/special_regs.rs | 95 ++++++++++++++++++- .../src/hypervisor/regs/standard_regs.rs | 5 - src/hyperlight_host/src/mem/memory_region.rs | 5 - 8 files changed, 95 insertions(+), 104 deletions(-) diff --git a/src/hyperlight_host/src/error.rs b/src/hyperlight_host/src/error.rs index b46dd5a6c..dea5ca587 100644 --- a/src/hyperlight_host/src/error.rs +++ b/src/hyperlight_host/src/error.rs @@ -14,9 +14,6 @@ See the License for the specific language governing permissions and limitations under the License. */ -#[cfg(mshv3)] -extern crate mshv_ioctls; - use std::array::TryFromSliceError; use std::cell::{BorrowError, BorrowMutError}; use std::convert::Infallible; diff --git a/src/hyperlight_host/src/hypervisor/gdb/mshv_debug.rs b/src/hyperlight_host/src/hypervisor/gdb/mshv_debug.rs index 0b5aba47c..f4d30d1d9 100644 --- a/src/hyperlight_host/src/hypervisor/gdb/mshv_debug.rs +++ b/src/hyperlight_host/src/hypervisor/gdb/mshv_debug.rs @@ -14,11 +14,6 @@ See the License for the specific language governing permissions and limitations under the License. */ -#[cfg(mshv3)] -extern crate mshv_bindings; -#[cfg(mshv3)] -extern crate mshv_ioctls; - use std::collections::HashMap; use mshv_bindings::{ diff --git a/src/hyperlight_host/src/hypervisor/hyperv_linux.rs b/src/hyperlight_host/src/hypervisor/hyperv_linux.rs index 88c27edc3..0dd7ad240 100644 --- a/src/hyperlight_host/src/hypervisor/hyperv_linux.rs +++ b/src/hyperlight_host/src/hypervisor/hyperv_linux.rs @@ -14,9 +14,6 @@ See the License for the specific language governing permissions and limitations under the License. */ -extern crate mshv_bindings; -extern crate mshv_ioctls; - use std::fmt::{Debug, Formatter}; use std::sync::atomic::{AtomicBool, AtomicU8, AtomicU64}; use std::sync::{Arc, Mutex}; @@ -68,7 +65,8 @@ use crate::{Result, log_then_return, new_error}; #[cfg(gdb)] mod debug { - use super::mshv_bindings::hv_x64_exception_intercept_message; + use mshv_bindings::hv_x64_exception_intercept_message; + use super::{HypervLinuxDriver, *}; use crate::hypervisor::gdb::{DebugMemoryAccess, DebugMsg, DebugResponse, VcpuStopReason}; use crate::{Result, new_error}; diff --git a/src/hyperlight_host/src/hypervisor/mod.rs b/src/hyperlight_host/src/hypervisor/mod.rs index d4eeeef75..609c01c0f 100644 --- a/src/hyperlight_host/src/hypervisor/mod.rs +++ b/src/hyperlight_host/src/hypervisor/mod.rs @@ -19,9 +19,7 @@ use tracing::{Span, instrument}; use crate::HyperlightError::StackOverflow; use crate::error::HyperlightError::ExecutionCanceledByHost; -use crate::hypervisor::regs::{ - CommonFpu, CommonRegisters, CommonSegmentRegister, CommonSpecialRegisters, -}; +use crate::hypervisor::regs::{CommonFpu, CommonRegisters, CommonSpecialRegisters}; use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags}; use crate::metrics::METRIC_GUEST_CANCELLATION; #[cfg(feature = "mem_profile")] @@ -78,25 +76,6 @@ use crate::mem::ptr::RawPtr; use crate::mem::shared_mem::HostSharedMemory; use crate::sandbox::host_funcs::FunctionRegistry; -cfg_if::cfg_if! { - if #[cfg(feature = "init-paging")] { - pub(crate) const CR4_PAE: u64 = 1 << 5; - pub(crate) const CR4_OSFXSR: u64 = 1 << 9; - pub(crate) const CR4_OSXMMEXCPT: u64 = 1 << 10; - pub(crate) const CR0_PE: u64 = 1; - pub(crate) const CR0_MP: u64 = 1 << 1; - pub(crate) const CR0_ET: u64 = 1 << 4; - pub(crate) const CR0_NE: u64 = 1 << 5; - pub(crate) const CR0_WP: u64 = 1 << 16; - pub(crate) const CR0_AM: u64 = 1 << 18; - pub(crate) const CR0_PG: u64 = 1 << 31; - pub(crate) const EFER_LME: u64 = 1 << 8; - pub(crate) const EFER_LMA: u64 = 1 << 10; - pub(crate) const EFER_SCE: u64 = 1; - pub(crate) const EFER_NX: u64 = 1 << 11; - } -} - /// These are the generic exit reasons that we can handle from a Hypervisor the Hypervisors run method is responsible for mapping from /// the hypervisor specific exit reasons to these generic ones pub enum HyperlightExit { @@ -212,59 +191,10 @@ pub(crate) trait Hypervisor: Debug + Send { /// This is a default implementation that works for all hypervisors fn setup_initial_sregs(&mut self, _pml4_addr: u64) -> Result<()> { #[cfg(feature = "init-paging")] - let sregs = CommonSpecialRegisters { - cr0: CR0_PE | CR0_MP | CR0_ET | CR0_NE | CR0_AM | CR0_PG | CR0_WP, - cr4: CR4_PAE | CR4_OSFXSR | CR4_OSXMMEXCPT, - cr3: _pml4_addr, - efer: EFER_LME | EFER_LMA | EFER_SCE | EFER_NX, - cs: CommonSegmentRegister { - type_: 11, - present: 1, - s: 1, - l: 1, - ..Default::default() - }, - tr: CommonSegmentRegister { - limit: 65535, - type_: 11, - present: 1, - s: 0, - ..Default::default() - }, - ..Default::default() - }; + let sregs = CommonSpecialRegisters::standard_64bit_defaults(_pml4_addr); #[cfg(not(feature = "init-paging"))] - let sregs = CommonSpecialRegisters { - cs: CommonSegmentRegister { - base: 0, - selector: 0, - limit: 0xFFFF, - type_: 11, - present: 1, - s: 1, - ..Default::default() - }, - ds: CommonSegmentRegister { - base: 0, - selector: 0, - limit: 0xFFFF, - type_: 3, - present: 1, - s: 1, - ..Default::default() - }, - tr: CommonSegmentRegister { - base: 0, - selector: 0, - limit: 0xFFFF, - type_: 11, - present: 1, - s: 0, - ..Default::default() - }, - ..Default::default() - }; + let sregs = CommonSpecialRegisters::standard_real_mode_defaults(); self.set_sregs(&sregs)?; Ok(()) diff --git a/src/hyperlight_host/src/hypervisor/regs/fpu.rs b/src/hyperlight_host/src/hypervisor/regs/fpu.rs index 0fdbde31d..47ce8a853 100644 --- a/src/hyperlight_host/src/hypervisor/regs/fpu.rs +++ b/src/hyperlight_host/src/hypervisor/regs/fpu.rs @@ -13,10 +13,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -#[cfg(mshv3)] -extern crate mshv_bindings; -#[cfg(mshv3)] -extern crate mshv_ioctls; #[cfg(target_os = "windows")] use std::collections::HashSet; diff --git a/src/hyperlight_host/src/hypervisor/regs/special_regs.rs b/src/hyperlight_host/src/hypervisor/regs/special_regs.rs index 7d4267c2d..37c0b94da 100644 --- a/src/hyperlight_host/src/hypervisor/regs/special_regs.rs +++ b/src/hyperlight_host/src/hypervisor/regs/special_regs.rs @@ -14,11 +14,6 @@ See the License for the specific language governing permissions and limitations under the License. */ -#[cfg(mshv3)] -extern crate mshv_bindings; -#[cfg(mshv3)] -extern crate mshv_ioctls; - #[cfg(target_os = "windows")] use std::collections::HashSet; @@ -32,6 +27,25 @@ use windows::Win32::System::Hypervisor::*; #[cfg(target_os = "windows")] use super::FromWhpRegisterError; +cfg_if::cfg_if! { + if #[cfg(feature = "init-paging")] { + pub(crate) const CR4_PAE: u64 = 1 << 5; + pub(crate) const CR4_OSFXSR: u64 = 1 << 9; + pub(crate) const CR4_OSXMMEXCPT: u64 = 1 << 10; + pub(crate) const CR0_PE: u64 = 1; + pub(crate) const CR0_MP: u64 = 1 << 1; + pub(crate) const CR0_ET: u64 = 1 << 4; + pub(crate) const CR0_NE: u64 = 1 << 5; + pub(crate) const CR0_WP: u64 = 1 << 16; + pub(crate) const CR0_AM: u64 = 1 << 18; + pub(crate) const CR0_PG: u64 = 1 << 31; + pub(crate) const EFER_LME: u64 = 1 << 8; + pub(crate) const EFER_LMA: u64 = 1 << 10; + pub(crate) const EFER_SCE: u64 = 1; + pub(crate) const EFER_NX: u64 = 1 << 11; + } +} + #[derive(Debug, Default, Copy, Clone, PartialEq)] pub(crate) struct CommonSpecialRegisters { pub cs: CommonSegmentRegister, @@ -54,6 +68,77 @@ pub(crate) struct CommonSpecialRegisters { pub interrupt_bitmap: [u64; 4], } +impl CommonSpecialRegisters { + #[cfg(feature = "init-paging")] + pub(crate) fn standard_64bit_defaults(pml4_addr: u64) -> Self { + CommonSpecialRegisters { + cs: CommonSegmentRegister { + l: 1, // 64-bit + type_: 0b1011, // Code, Readable, Accessed + present: 1, // Present + s: 1, // Non-system + ..Default::default() + }, + tr: CommonSegmentRegister { + limit: 0xFFFF, + type_: 0b1011, + present: 1, + ..Default::default() + }, + efer: EFER_LME | EFER_LMA | EFER_SCE | EFER_NX, + ds: Default::default(), + es: Default::default(), + fs: Default::default(), + gs: Default::default(), + ss: Default::default(), + ldt: Default::default(), + gdt: Default::default(), + idt: Default::default(), + cr0: CR0_PE | CR0_MP | CR0_ET | CR0_NE | CR0_AM | CR0_WP | CR0_PG, + cr2: 0, + cr4: CR4_PAE | CR4_OSFXSR | CR4_OSXMMEXCPT, + cr3: pml4_addr, + cr8: 0, + apic_base: 0, + interrupt_bitmap: [0; 4], + } + } + + #[cfg(not(feature = "init-paging"))] + pub(crate) fn standard_real_mode_defaults() -> Self { + CommonSpecialRegisters { + cs: CommonSegmentRegister { + base: 0, + selector: 0, + limit: 0xFFFF, + type_: 11, + present: 1, + s: 1, + ..Default::default() + }, + ds: CommonSegmentRegister { + base: 0, + selector: 0, + limit: 0xFFFF, + type_: 3, + present: 1, + s: 1, + ..Default::default() + }, + tr: CommonSegmentRegister { + base: 0, + selector: 0, + limit: 0xFFFF, + type_: 11, + present: 1, + s: 0, + ..Default::default() + }, + ..Default::default() + } + } +} + #[cfg(mshv3)] impl From<&SpecialRegisters> for CommonSpecialRegisters { fn from(value: &SpecialRegisters) -> Self { diff --git a/src/hyperlight_host/src/hypervisor/regs/standard_regs.rs b/src/hyperlight_host/src/hypervisor/regs/standard_regs.rs index d97f4cb68..03dcf4b93 100644 --- a/src/hyperlight_host/src/hypervisor/regs/standard_regs.rs +++ b/src/hyperlight_host/src/hypervisor/regs/standard_regs.rs @@ -14,11 +14,6 @@ See the License for the specific language governing permissions and limitations under the License. */ -#[cfg(mshv3)] -extern crate mshv_bindings; -#[cfg(mshv3)] -extern crate mshv_ioctls; - #[cfg(kvm)] use kvm_bindings::kvm_regs; #[cfg(mshv3)] diff --git a/src/hyperlight_host/src/mem/memory_region.rs b/src/hyperlight_host/src/mem/memory_region.rs index 192298aaa..bc3c9d9b6 100644 --- a/src/hyperlight_host/src/mem/memory_region.rs +++ b/src/hyperlight_host/src/mem/memory_region.rs @@ -14,11 +14,6 @@ See the License for the specific language governing permissions and limitations under the License. */ -#[cfg(mshv3)] -extern crate mshv_bindings; -#[cfg(mshv3)] -extern crate mshv_ioctls; - use std::ops::Range; use bitflags::bitflags;