Skip to content

Commit 30e1ff2

Browse files
authored
[host] adds init-paging feature (#639)
This is an extra feature flag that allows disabling our paging setup. This is needed to further enable custom guests and should not impact any of our current workloads. init-paging is on by default. Signed-off-by: danbugs <danilochiarlone@gmail.com>
1 parent bd5c4a9 commit 30e1ff2

File tree

11 files changed

+154
-83
lines changed

11 files changed

+154
-83
lines changed

Justfile

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ test-like-ci config=default-target hypervisor="kvm":
7575
@# with default features
7676
just test {{config}} {{ if hypervisor == "mshv3" {"mshv3"} else {""} }}
7777

78-
@# with only one driver enabled + seccomp
79-
just test {{config}} seccomp,build-metadata,{{ if hypervisor == "mshv" {"mshv2"} else if hypervisor == "mshv3" {"mshv3"} else {"kvm"} }}
78+
@# with only one driver enabled + seccomp + build-metadata + init-paging
79+
just test {{config}} seccomp,build-metadata,init-paging,{{ if hypervisor == "mshv" {"mshv2"} else if hypervisor == "mshv3" {"mshv3"} else {"kvm"} }}
8080

8181
@# make sure certain cargo features compile
8282
cargo check -p hyperlight-host --features crashdump
@@ -94,32 +94,32 @@ test target=default-target features="": (test-unit target features) (test-isolat
9494

9595
# runs unit tests
9696
test-unit target=default-target features="":
97-
cargo test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} --lib
97+
cargo test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F init-paging," + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} --lib
9898

9999
# runs tests that requires being run separately, for example due to global state
100100
test-isolated target=default-target features="":
101-
cargo test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host --lib -- sandbox::uninitialized::tests::test_trace_trace --exact --ignored
102-
cargo test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host --lib -- sandbox::uninitialized::tests::test_log_trace --exact --ignored
103-
cargo test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host --lib -- sandbox::initialized_multi_use::tests::create_1000_sandboxes --exact --ignored
104-
cargo test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host --lib -- sandbox::outb::tests::test_log_outb_log --exact --ignored
105-
cargo test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host --lib -- mem::shared_mem::tests::test_drop --exact --ignored
106-
cargo test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host --test integration_test -- log_message --exact --ignored
101+
cargo test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F init-paging," + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host --lib -- sandbox::uninitialized::tests::test_trace_trace --exact --ignored
102+
cargo test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F init-paging," + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host --lib -- sandbox::uninitialized::tests::test_log_trace --exact --ignored
103+
cargo test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F init-paging," + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host --lib -- sandbox::initialized_multi_use::tests::create_1000_sandboxes --exact --ignored
104+
cargo test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F init-paging," + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host --lib -- sandbox::outb::tests::test_log_outb_log --exact --ignored
105+
cargo test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F init-paging," + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host --lib -- mem::shared_mem::tests::test_drop --exact --ignored
106+
cargo test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F init-paging," + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host --test integration_test -- log_message --exact --ignored
107107
@# metrics tests
108-
cargo test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F function_call_metrics," + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host --lib -- metrics::tests::test_metrics_are_emitted --exact
108+
cargo test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F function_call_metrics,init-paging," + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host --lib -- metrics::tests::test_metrics_are_emitted --exact
109109
# runs integration tests. Guest can either be "rust" or "c"
110110
test-integration guest target=default-target features="":
111111
@# run execute_on_heap test with feature "executable_heap" on and off
112112
{{if os() == "windows" { "$env:" } else { "" } }}GUEST="{{guest}}"{{if os() == "windows" { ";" } else { "" } }} cargo test --profile={{ if target == "debug" { "dev" } else { target } }} --test integration_test execute_on_heap {{ if features =="" {" --features executable_heap"} else {"--features executable_heap," + features} }} -- --ignored
113113
{{if os() == "windows" { "$env:" } else { "" } }}GUEST="{{guest}}"{{if os() == "windows" { ";" } else { "" } }} cargo test --profile={{ if target == "debug" { "dev" } else { target } }} --test integration_test execute_on_heap {{ if features =="" {""} else {"--features " + features} }} -- --ignored
114114

115115
@# run the rest of the integration tests
116-
{{if os() == "windows" { "$env:" } else { "" } }}GUEST="{{guest}}"{{if os() == "windows" { ";" } else { "" } }} cargo test -p hyperlight-host {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} --test '*'
116+
{{if os() == "windows" { "$env:" } else { "" } }}GUEST="{{guest}}"{{if os() == "windows" { ";" } else { "" } }} cargo test -p hyperlight-host {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F init-paging," + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} --test '*'
117117

118118
# runs seccomp tests
119119
test-seccomp target=default-target features="":
120120
@# run seccomp test with feature "seccomp" on and off
121121
cargo test --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host test_violate_seccomp_filters --lib {{ if features =="" {''} else { "--features " + features } }} -- --ignored
122-
cargo test --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host test_violate_seccomp_filters --no-default-features {{ if features =~"mshv3" {"--features mshv3"} else {"--features mshv2,kvm" } }} --lib -- --ignored
122+
cargo test --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host test_violate_seccomp_filters --no-default-features {{ if features =~"mshv3" {"--features init-paging,mshv3"} else {"--features mshv2,init-paging,kvm" } }} --lib -- --ignored
123123

124124
# runs tests that ensure compilation fails when it should
125125
test-compilation-fail target=default-target:

src/hyperlight_host/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ cfg_aliases = "0.2.1"
119119
built = { version = "0.8.0", optional = true, features = ["chrono", "git2"] }
120120

121121
[features]
122-
default = ["kvm", "mshv2", "seccomp", "build-metadata"]
122+
default = ["kvm", "mshv2", "seccomp", "build-metadata", "init-paging"]
123123
seccomp = ["dep:seccompiler"]
124124
function_call_metrics = []
125125
executable_heap = []
@@ -134,6 +134,7 @@ mshv3 = ["dep:mshv-bindings3", "dep:mshv-ioctls3"]
134134
gdb = ["dep:gdbstub", "dep:gdbstub_arch"]
135135
fuzzing = ["hyperlight-common/fuzzing"]
136136
build-metadata = ["dep:built"]
137+
init-paging = []
137138

138139
[[bench]]
139140
name = "benchmarks"

src/hyperlight_host/src/hypervisor/hyperv_linux.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ use super::gdb::{
6161
#[cfg(gdb)]
6262
use super::handlers::DbgMemAccessHandlerWrapper;
6363
use super::handlers::{MemAccessHandlerWrapper, OutBHandlerWrapper};
64+
#[cfg(feature = "init-paging")]
6465
use super::{
6566
CR0_AM, CR0_ET, CR0_MP, CR0_NE, CR0_PE, CR0_PG, CR0_WP, CR4_OSFXSR, CR4_OSXMMEXCPT, CR4_PAE,
66-
EFER_LMA, EFER_LME, EFER_NX, EFER_SCE, Hypervisor, InterruptHandle, LinuxInterruptHandle,
67-
VirtualCPU,
67+
EFER_LMA, EFER_LME, EFER_NX, EFER_SCE,
6868
};
69+
use super::{HyperlightExit, Hypervisor, InterruptHandle, LinuxInterruptHandle, VirtualCPU};
6970
#[cfg(gdb)]
7071
use crate::HyperlightError;
71-
use crate::hypervisor::HyperlightExit;
7272
use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags};
7373
use crate::mem::ptr::{GuestPtr, RawPtr};
7474
use crate::sandbox::SandboxConfiguration;
@@ -436,11 +436,12 @@ impl HypervLinuxDriver {
436436
}
437437

438438
#[instrument(err(Debug), skip_all, parent = Span::current(), level = "Trace")]
439-
fn setup_initial_sregs(vcpu: &mut VcpuFd, pml4_addr: u64) -> Result<()> {
439+
fn setup_initial_sregs(vcpu: &mut VcpuFd, _pml4_addr: u64) -> Result<()> {
440+
#[cfg(feature = "init-paging")]
440441
let sregs = SpecialRegisters {
441442
cr0: CR0_PE | CR0_MP | CR0_ET | CR0_NE | CR0_AM | CR0_PG | CR0_WP,
442443
cr4: CR4_PAE | CR4_OSFXSR | CR4_OSXMMEXCPT,
443-
cr3: pml4_addr,
444+
cr3: _pml4_addr,
444445
efer: EFER_LME | EFER_LMA | EFER_SCE | EFER_NX,
445446
cs: SegmentRegister {
446447
type_: 11,
@@ -457,6 +458,16 @@ impl HypervLinuxDriver {
457458
},
458459
..Default::default()
459460
};
461+
462+
#[cfg(not(feature = "init-paging"))]
463+
let sregs = SpecialRegisters {
464+
cs: SegmentRegister {
465+
base: 0,
466+
selector: 0,
467+
..Default::default()
468+
},
469+
..Default::default()
470+
};
460471
vcpu.set_sregs(&sregs)?;
461472
Ok(())
462473
}

src/hyperlight_host/src/hypervisor/hyperv_windows.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ use super::surrogate_process::SurrogateProcess;
4141
use super::surrogate_process_manager::*;
4242
use super::windows_hypervisor_platform::{VMPartition, VMProcessor};
4343
use super::wrappers::{HandleWrapper, WHvFPURegisters};
44+
#[cfg(feature = "init-paging")]
4445
use super::{
4546
CR0_AM, CR0_ET, CR0_MP, CR0_NE, CR0_PE, CR0_PG, CR0_WP, CR4_OSFXSR, CR4_OSXMMEXCPT, CR4_PAE,
46-
EFER_LMA, EFER_LME, EFER_NX, EFER_SCE, HyperlightExit, Hypervisor, InterruptHandle, VirtualCPU,
47+
EFER_LMA, EFER_LME, EFER_NX, EFER_SCE,
4748
};
49+
use super::{HyperlightExit, Hypervisor, InterruptHandle, VirtualCPU};
4850
use crate::hypervisor::fpu::FP_CONTROL_WORD_DEFAULT;
4951
use crate::hypervisor::wrappers::WHvGeneralRegisters;
5052
use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags};
@@ -125,9 +127,10 @@ impl HypervWindowsDriver {
125127
})
126128
}
127129

128-
fn setup_initial_sregs(proc: &mut VMProcessor, pml4_addr: u64) -> Result<()> {
130+
fn setup_initial_sregs(proc: &mut VMProcessor, _pml4_addr: u64) -> Result<()> {
131+
#[cfg(feature = "init-paging")]
129132
proc.set_registers(&[
130-
(WHvX64RegisterCr3, WHV_REGISTER_VALUE { Reg64: pml4_addr }),
133+
(WHvX64RegisterCr3, WHV_REGISTER_VALUE { Reg64: _pml4_addr }),
131134
(
132135
WHvX64RegisterCr4,
133136
WHV_REGISTER_VALUE {
@@ -158,6 +161,19 @@ impl HypervWindowsDriver {
158161
},
159162
),
160163
])?;
164+
165+
#[cfg(not(feature = "init-paging"))]
166+
proc.set_registers(&[(
167+
WHvX64RegisterCs,
168+
WHV_REGISTER_VALUE {
169+
Segment: WHV_X64_SEGMENT_REGISTER {
170+
Base: 0,
171+
Selector: 0,
172+
..Default::default()
173+
},
174+
},
175+
)])?;
176+
161177
Ok(())
162178
}
163179

src/hyperlight_host/src/hypervisor/kvm.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ use super::gdb::{DebugCommChannel, DebugMsg, DebugResponse, GuestDebug, KvmDebug
3535
#[cfg(gdb)]
3636
use super::handlers::DbgMemAccessHandlerWrapper;
3737
use super::handlers::{MemAccessHandlerWrapper, OutBHandlerWrapper};
38+
#[cfg(feature = "init-paging")]
3839
use super::{
3940
CR0_AM, CR0_ET, CR0_MP, CR0_NE, CR0_PE, CR0_PG, CR0_WP, CR4_OSFXSR, CR4_OSXMMEXCPT, CR4_PAE,
40-
EFER_LMA, EFER_LME, EFER_NX, EFER_SCE, HyperlightExit, Hypervisor, InterruptHandle,
41-
LinuxInterruptHandle, VirtualCPU,
41+
EFER_LMA, EFER_LME, EFER_NX, EFER_SCE,
4242
};
43+
use super::{HyperlightExit, Hypervisor, InterruptHandle, LinuxInterruptHandle, VirtualCPU};
4344
#[cfg(gdb)]
4445
use crate::HyperlightError;
4546
use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags};
@@ -390,14 +391,21 @@ impl KVMDriver {
390391
}
391392

392393
#[instrument(err(Debug), skip_all, parent = Span::current(), level = "Trace")]
393-
fn setup_initial_sregs(vcpu_fd: &mut VcpuFd, pml4_addr: u64) -> Result<()> {
394+
fn setup_initial_sregs(vcpu_fd: &mut VcpuFd, _pml4_addr: u64) -> Result<()> {
394395
// setup paging and IA-32e (64-bit) mode
395396
let mut sregs = vcpu_fd.get_sregs()?;
396-
sregs.cr3 = pml4_addr;
397-
sregs.cr4 = CR4_PAE | CR4_OSFXSR | CR4_OSXMMEXCPT;
398-
sregs.cr0 = CR0_PE | CR0_MP | CR0_ET | CR0_NE | CR0_AM | CR0_PG | CR0_WP;
399-
sregs.efer = EFER_LME | EFER_LMA | EFER_SCE | EFER_NX;
400-
sregs.cs.l = 1; // required for 64-bit mode
397+
cfg_if::cfg_if! {
398+
if #[cfg(feature = "init-paging")] {
399+
sregs.cr3 = _pml4_addr;
400+
sregs.cr4 = CR4_PAE | CR4_OSFXSR | CR4_OSXMMEXCPT;
401+
sregs.cr0 = CR0_PE | CR0_MP | CR0_ET | CR0_NE | CR0_AM | CR0_PG | CR0_WP;
402+
sregs.efer = EFER_LME | EFER_LMA | EFER_SCE | EFER_NX;
403+
sregs.cs.l = 1; // required for 64-bit mode
404+
} else {
405+
sregs.cs.base = 0;
406+
sregs.cs.selector = 0;
407+
}
408+
}
401409
vcpu_fd.set_sregs(&sregs)?;
402410
Ok(())
403411
}

src/hyperlight_host/src/hypervisor/mod.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,24 @@ use self::handlers::{
7575
};
7676
use crate::mem::ptr::RawPtr;
7777

78-
pub(crate) const CR4_PAE: u64 = 1 << 5;
79-
pub(crate) const CR4_OSFXSR: u64 = 1 << 9;
80-
pub(crate) const CR4_OSXMMEXCPT: u64 = 1 << 10;
81-
pub(crate) const CR0_PE: u64 = 1;
82-
pub(crate) const CR0_MP: u64 = 1 << 1;
83-
pub(crate) const CR0_ET: u64 = 1 << 4;
84-
pub(crate) const CR0_NE: u64 = 1 << 5;
85-
pub(crate) const CR0_WP: u64 = 1 << 16;
86-
pub(crate) const CR0_AM: u64 = 1 << 18;
87-
pub(crate) const CR0_PG: u64 = 1 << 31;
88-
pub(crate) const EFER_LME: u64 = 1 << 8;
89-
pub(crate) const EFER_LMA: u64 = 1 << 10;
90-
pub(crate) const EFER_SCE: u64 = 1;
91-
pub(crate) const EFER_NX: u64 = 1 << 11;
78+
cfg_if::cfg_if! {
79+
if #[cfg(feature = "init-paging")] {
80+
pub(crate) const CR4_PAE: u64 = 1 << 5;
81+
pub(crate) const CR4_OSFXSR: u64 = 1 << 9;
82+
pub(crate) const CR4_OSXMMEXCPT: u64 = 1 << 10;
83+
pub(crate) const CR0_PE: u64 = 1;
84+
pub(crate) const CR0_MP: u64 = 1 << 1;
85+
pub(crate) const CR0_ET: u64 = 1 << 4;
86+
pub(crate) const CR0_NE: u64 = 1 << 5;
87+
pub(crate) const CR0_WP: u64 = 1 << 16;
88+
pub(crate) const CR0_AM: u64 = 1 << 18;
89+
pub(crate) const CR0_PG: u64 = 1 << 31;
90+
pub(crate) const EFER_LME: u64 = 1 << 8;
91+
pub(crate) const EFER_LMA: u64 = 1 << 10;
92+
pub(crate) const EFER_SCE: u64 = 1;
93+
pub(crate) const EFER_NX: u64 = 1 << 11;
94+
}
95+
}
9296

9397
/// These are the generic exit reasons that we can handle from a Hypervisor the Hypervisors run method is responsible for mapping from
9498
/// the hypervisor specific exit reasons to these generic ones

src/hyperlight_host/src/mem/elf.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ use goblin::elf::reloc::{R_AARCH64_NONE, R_AARCH64_RELATIVE};
1919
#[cfg(target_arch = "x86_64")]
2020
use goblin::elf::reloc::{R_X86_64_NONE, R_X86_64_RELATIVE};
2121
use goblin::elf::{Elf, ProgramHeaders, Reloc};
22+
#[cfg(not(feature = "init-paging"))]
23+
use goblin::elf32::program_header::PT_LOAD;
24+
#[cfg(feature = "init-paging")]
2225
use goblin::elf64::program_header::PT_LOAD;
2326

2427
use crate::{Result, log_then_return, new_error};

0 commit comments

Comments
 (0)