Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/hyperlight_host/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 0 additions & 5 deletions src/hyperlight_host/src/hypervisor/gdb/mshv_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down
6 changes: 2 additions & 4 deletions src/hyperlight_host/src/hypervisor/hyperv_linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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};
Expand Down
76 changes: 3 additions & 73 deletions src/hyperlight_host/src/hypervisor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(())
Expand Down
4 changes: 0 additions & 4 deletions src/hyperlight_host/src/hypervisor/regs/fpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
95 changes: 90 additions & 5 deletions src/hyperlight_host/src/hypervisor/regs/special_regs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Expand All @@ -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 {
Expand Down
5 changes: 0 additions & 5 deletions src/hyperlight_host/src/hypervisor/regs/standard_regs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
5 changes: 0 additions & 5 deletions src/hyperlight_host/src/mem/memory_region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading