Skip to content

Commit bfbd008

Browse files
authored
Move sregs to its own file (#1043)
Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent 55055dd commit bfbd008

File tree

8 files changed

+95
-104
lines changed

8 files changed

+95
-104
lines changed

src/hyperlight_host/src/error.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
#[cfg(mshv3)]
18-
extern crate mshv_ioctls;
19-
2017
use std::array::TryFromSliceError;
2118
use std::cell::{BorrowError, BorrowMutError};
2219
use std::convert::Infallible;

src/hyperlight_host/src/hypervisor/gdb/mshv_debug.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
#[cfg(mshv3)]
18-
extern crate mshv_bindings;
19-
#[cfg(mshv3)]
20-
extern crate mshv_ioctls;
21-
2217
use std::collections::HashMap;
2318

2419
use mshv_bindings::{

src/hyperlight_host/src/hypervisor/hyperv_linux.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
extern crate mshv_bindings;
18-
extern crate mshv_ioctls;
19-
2017
use std::fmt::{Debug, Formatter};
2118
use std::sync::atomic::{AtomicBool, AtomicU8, AtomicU64};
2219
use std::sync::{Arc, Mutex};
@@ -68,7 +65,8 @@ use crate::{Result, log_then_return, new_error};
6865

6966
#[cfg(gdb)]
7067
mod debug {
71-
use super::mshv_bindings::hv_x64_exception_intercept_message;
68+
use mshv_bindings::hv_x64_exception_intercept_message;
69+
7270
use super::{HypervLinuxDriver, *};
7371
use crate::hypervisor::gdb::{DebugMemoryAccess, DebugMsg, DebugResponse, VcpuStopReason};
7472
use crate::{Result, new_error};

src/hyperlight_host/src/hypervisor/mod.rs

Lines changed: 3 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ use tracing::{Span, instrument};
1919

2020
use crate::HyperlightError::StackOverflow;
2121
use crate::error::HyperlightError::ExecutionCanceledByHost;
22-
use crate::hypervisor::regs::{
23-
CommonFpu, CommonRegisters, CommonSegmentRegister, CommonSpecialRegisters,
24-
};
22+
use crate::hypervisor::regs::{CommonFpu, CommonRegisters, CommonSpecialRegisters};
2523
use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags};
2624
use crate::metrics::METRIC_GUEST_CANCELLATION;
2725
#[cfg(feature = "mem_profile")]
@@ -78,25 +76,6 @@ use crate::mem::ptr::RawPtr;
7876
use crate::mem::shared_mem::HostSharedMemory;
7977
use crate::sandbox::host_funcs::FunctionRegistry;
8078

81-
cfg_if::cfg_if! {
82-
if #[cfg(feature = "init-paging")] {
83-
pub(crate) const CR4_PAE: u64 = 1 << 5;
84-
pub(crate) const CR4_OSFXSR: u64 = 1 << 9;
85-
pub(crate) const CR4_OSXMMEXCPT: u64 = 1 << 10;
86-
pub(crate) const CR0_PE: u64 = 1;
87-
pub(crate) const CR0_MP: u64 = 1 << 1;
88-
pub(crate) const CR0_ET: u64 = 1 << 4;
89-
pub(crate) const CR0_NE: u64 = 1 << 5;
90-
pub(crate) const CR0_WP: u64 = 1 << 16;
91-
pub(crate) const CR0_AM: u64 = 1 << 18;
92-
pub(crate) const CR0_PG: u64 = 1 << 31;
93-
pub(crate) const EFER_LME: u64 = 1 << 8;
94-
pub(crate) const EFER_LMA: u64 = 1 << 10;
95-
pub(crate) const EFER_SCE: u64 = 1;
96-
pub(crate) const EFER_NX: u64 = 1 << 11;
97-
}
98-
}
99-
10079
/// These are the generic exit reasons that we can handle from a Hypervisor the Hypervisors run method is responsible for mapping from
10180
/// the hypervisor specific exit reasons to these generic ones
10281
pub enum HyperlightExit {
@@ -212,59 +191,10 @@ pub(crate) trait Hypervisor: Debug + Send {
212191
/// This is a default implementation that works for all hypervisors
213192
fn setup_initial_sregs(&mut self, _pml4_addr: u64) -> Result<()> {
214193
#[cfg(feature = "init-paging")]
215-
let sregs = CommonSpecialRegisters {
216-
cr0: CR0_PE | CR0_MP | CR0_ET | CR0_NE | CR0_AM | CR0_PG | CR0_WP,
217-
cr4: CR4_PAE | CR4_OSFXSR | CR4_OSXMMEXCPT,
218-
cr3: _pml4_addr,
219-
efer: EFER_LME | EFER_LMA | EFER_SCE | EFER_NX,
220-
cs: CommonSegmentRegister {
221-
type_: 11,
222-
present: 1,
223-
s: 1,
224-
l: 1,
225-
..Default::default()
226-
},
227-
tr: CommonSegmentRegister {
228-
limit: 65535,
229-
type_: 11,
230-
present: 1,
231-
s: 0,
232-
..Default::default()
233-
},
234-
..Default::default()
235-
};
194+
let sregs = CommonSpecialRegisters::standard_64bit_defaults(_pml4_addr);
236195

237196
#[cfg(not(feature = "init-paging"))]
238-
let sregs = CommonSpecialRegisters {
239-
cs: CommonSegmentRegister {
240-
base: 0,
241-
selector: 0,
242-
limit: 0xFFFF,
243-
type_: 11,
244-
present: 1,
245-
s: 1,
246-
..Default::default()
247-
},
248-
ds: CommonSegmentRegister {
249-
base: 0,
250-
selector: 0,
251-
limit: 0xFFFF,
252-
type_: 3,
253-
present: 1,
254-
s: 1,
255-
..Default::default()
256-
},
257-
tr: CommonSegmentRegister {
258-
base: 0,
259-
selector: 0,
260-
limit: 0xFFFF,
261-
type_: 11,
262-
present: 1,
263-
s: 0,
264-
..Default::default()
265-
},
266-
..Default::default()
267-
};
197+
let sregs = CommonSpecialRegisters::standard_real_mode_defaults();
268198

269199
self.set_sregs(&sregs)?;
270200
Ok(())

src/hyperlight_host/src/hypervisor/regs/fpu.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16-
#[cfg(mshv3)]
17-
extern crate mshv_bindings;
18-
#[cfg(mshv3)]
19-
extern crate mshv_ioctls;
2016

2117
#[cfg(target_os = "windows")]
2218
use std::collections::HashSet;

src/hyperlight_host/src/hypervisor/regs/special_regs.rs

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
#[cfg(mshv3)]
18-
extern crate mshv_bindings;
19-
#[cfg(mshv3)]
20-
extern crate mshv_ioctls;
21-
2217
#[cfg(target_os = "windows")]
2318
use std::collections::HashSet;
2419

@@ -32,6 +27,25 @@ use windows::Win32::System::Hypervisor::*;
3227
#[cfg(target_os = "windows")]
3328
use super::FromWhpRegisterError;
3429

30+
cfg_if::cfg_if! {
31+
if #[cfg(feature = "init-paging")] {
32+
pub(crate) const CR4_PAE: u64 = 1 << 5;
33+
pub(crate) const CR4_OSFXSR: u64 = 1 << 9;
34+
pub(crate) const CR4_OSXMMEXCPT: u64 = 1 << 10;
35+
pub(crate) const CR0_PE: u64 = 1;
36+
pub(crate) const CR0_MP: u64 = 1 << 1;
37+
pub(crate) const CR0_ET: u64 = 1 << 4;
38+
pub(crate) const CR0_NE: u64 = 1 << 5;
39+
pub(crate) const CR0_WP: u64 = 1 << 16;
40+
pub(crate) const CR0_AM: u64 = 1 << 18;
41+
pub(crate) const CR0_PG: u64 = 1 << 31;
42+
pub(crate) const EFER_LME: u64 = 1 << 8;
43+
pub(crate) const EFER_LMA: u64 = 1 << 10;
44+
pub(crate) const EFER_SCE: u64 = 1;
45+
pub(crate) const EFER_NX: u64 = 1 << 11;
46+
}
47+
}
48+
3549
#[derive(Debug, Default, Copy, Clone, PartialEq)]
3650
pub(crate) struct CommonSpecialRegisters {
3751
pub cs: CommonSegmentRegister,
@@ -54,6 +68,77 @@ pub(crate) struct CommonSpecialRegisters {
5468
pub interrupt_bitmap: [u64; 4],
5569
}
5670

71+
impl CommonSpecialRegisters {
72+
#[cfg(feature = "init-paging")]
73+
pub(crate) fn standard_64bit_defaults(pml4_addr: u64) -> Self {
74+
CommonSpecialRegisters {
75+
cs: CommonSegmentRegister {
76+
l: 1, // 64-bit
77+
type_: 0b1011, // Code, Readable, Accessed
78+
present: 1, // Present
79+
s: 1, // Non-system
80+
..Default::default()
81+
},
82+
tr: CommonSegmentRegister {
83+
limit: 0xFFFF,
84+
type_: 0b1011,
85+
present: 1,
86+
..Default::default()
87+
},
88+
efer: EFER_LME | EFER_LMA | EFER_SCE | EFER_NX,
89+
ds: Default::default(),
90+
es: Default::default(),
91+
fs: Default::default(),
92+
gs: Default::default(),
93+
ss: Default::default(),
94+
ldt: Default::default(),
95+
gdt: Default::default(),
96+
idt: Default::default(),
97+
cr0: CR0_PE | CR0_MP | CR0_ET | CR0_NE | CR0_AM | CR0_WP | CR0_PG,
98+
cr2: 0,
99+
cr4: CR4_PAE | CR4_OSFXSR | CR4_OSXMMEXCPT,
100+
cr3: pml4_addr,
101+
cr8: 0,
102+
apic_base: 0,
103+
interrupt_bitmap: [0; 4],
104+
}
105+
}
106+
107+
#[cfg(not(feature = "init-paging"))]
108+
pub(crate) fn standard_real_mode_defaults() -> Self {
109+
CommonSpecialRegisters {
110+
cs: CommonSegmentRegister {
111+
base: 0,
112+
selector: 0,
113+
limit: 0xFFFF,
114+
type_: 11,
115+
present: 1,
116+
s: 1,
117+
..Default::default()
118+
},
119+
ds: CommonSegmentRegister {
120+
base: 0,
121+
selector: 0,
122+
limit: 0xFFFF,
123+
type_: 3,
124+
present: 1,
125+
s: 1,
126+
..Default::default()
127+
},
128+
tr: CommonSegmentRegister {
129+
base: 0,
130+
selector: 0,
131+
limit: 0xFFFF,
132+
type_: 11,
133+
present: 1,
134+
s: 0,
135+
..Default::default()
136+
},
137+
..Default::default()
138+
}
139+
}
140+
}
141+
57142
#[cfg(mshv3)]
58143
impl From<&SpecialRegisters> for CommonSpecialRegisters {
59144
fn from(value: &SpecialRegisters) -> Self {

src/hyperlight_host/src/hypervisor/regs/standard_regs.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
#[cfg(mshv3)]
18-
extern crate mshv_bindings;
19-
#[cfg(mshv3)]
20-
extern crate mshv_ioctls;
21-
2217
#[cfg(kvm)]
2318
use kvm_bindings::kvm_regs;
2419
#[cfg(mshv3)]

src/hyperlight_host/src/mem/memory_region.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
#[cfg(mshv3)]
18-
extern crate mshv_bindings;
19-
#[cfg(mshv3)]
20-
extern crate mshv_ioctls;
21-
2217
use std::ops::Range;
2318

2419
use bitflags::bitflags;

0 commit comments

Comments
 (0)