Skip to content

Commit 9401bb2

Browse files
committed
[host/hypervisor/hyperv_windows] brought back whp driver
Changed whp driver to use the new SandboxMemorySections API. Signed-off-by: danbugs <[email protected]>
1 parent 9ef5013 commit 9401bb2

File tree

8 files changed

+80
-76
lines changed

8 files changed

+80
-76
lines changed

src/hyperlight_host/src/hypervisor/hyperv_windows.rs

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::fmt;
1919
use std::fmt::{Debug, Formatter};
2020
use std::string::String;
2121

22-
use hyperlight_common::mem::PAGE_SIZE;
22+
use hyperlight_common::PAGE_SIZE;
2323
use log::LevelFilter;
2424
use tracing::{instrument, Span};
2525
use windows::Win32::System::Hypervisor::{
@@ -43,8 +43,8 @@ use super::{
4343
use crate::hypervisor::fpu::FP_CONTROL_WORD_DEFAULT;
4444
use crate::hypervisor::hypervisor_handler::HypervisorHandler;
4545
use crate::hypervisor::wrappers::WHvGeneralRegisters;
46-
use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags};
4746
use crate::mem::ptr::{GuestPtr, RawPtr};
47+
use crate::sandbox::sandbox_builder::{MemoryRegionFlags, SandboxMemorySections};
4848
use crate::{debug, new_error, Result};
4949

5050
/// A Hypervisor driver for HyperV-on-Windows.
@@ -55,7 +55,7 @@ pub(crate) struct HypervWindowsDriver {
5555
source_address: *mut c_void, // this points into the first guard page
5656
entrypoint: u64,
5757
orig_rsp: GuestPtr,
58-
mem_regions: Vec<MemoryRegion>,
58+
mem_sections: SandboxMemorySections,
5959
}
6060
/* This does not automatically impl Send/Sync because the host
6161
* address of the shared memory region is a raw pointer, which are
@@ -68,7 +68,7 @@ unsafe impl Sync for HypervWindowsDriver {}
6868
impl HypervWindowsDriver {
6969
#[instrument(err(Debug), skip_all, parent = Span::current(), level = "Trace")]
7070
pub(crate) fn new(
71-
mem_regions: Vec<MemoryRegion>,
71+
mem_sections: SandboxMemorySections,
7272
raw_size: usize,
7373
raw_source_address: *mut c_void,
7474
pml4_address: u64,
@@ -86,7 +86,7 @@ impl HypervWindowsDriver {
8686
mgr.get_surrogate_process(raw_size, raw_source_address, mmap_file_handle)
8787
}?;
8888

89-
partition.map_gpa_range(&mem_regions, surrogate_process.process_handle)?;
89+
partition.map_gpa_range(&mem_sections, surrogate_process.process_handle)?;
9090

9191
let mut proc = VMProcessor::new(partition)?;
9292
Self::setup_initial_sregs(&mut proc, pml4_address)?;
@@ -101,7 +101,7 @@ impl HypervWindowsDriver {
101101
source_address: raw_source_address,
102102
entrypoint,
103103
orig_rsp: GuestPtr::try_from(RawPtr::from(rsp))?,
104-
mem_regions,
104+
mem_sections,
105105
})
106106
}
107107

@@ -167,7 +167,7 @@ impl Debug for HypervWindowsDriver {
167167
.field("Entrypoint", &self.entrypoint)
168168
.field("Original RSP", &self.orig_rsp);
169169

170-
for region in &self.mem_regions {
170+
for (_, region) in self.mem_sections.iter() {
171171
fs.field("Memory Region", &region);
172172
}
173173

@@ -302,9 +302,8 @@ impl Hypervisor for HypervWindowsDriver {
302302
#[instrument(err(Debug), skip_all, parent = Span::current(), level = "Trace")]
303303
fn initialise(
304304
&mut self,
305-
peb_address: RawPtr,
305+
hyperlight_peb_guest_memory_region_address: u64,
306306
seed: u64,
307-
page_size: u32,
308307
outb_hdl: OutBHandlerWrapper,
309308
mem_access_hdl: MemAccessHandlerWrapper,
310309
hv_handler: Option<HypervisorHandler>,
@@ -321,9 +320,8 @@ impl Hypervisor for HypervWindowsDriver {
321320
rsp: self.orig_rsp.absolute()?,
322321

323322
// function args
324-
rcx: peb_address.into(),
323+
rcx: hyperlight_peb_guest_memory_region_address,
325324
rdx: seed,
326-
r8: page_size.into(),
327325
r9: max_guest_log_level,
328326
rflags: 1 << 1, // eflags bit index 1 is reserved and always needs to be 1
329327

@@ -449,8 +447,11 @@ impl Hypervisor for HypervWindowsDriver {
449447
gpa, access_info, &self
450448
);
451449

452-
match self.get_memory_access_violation(gpa as usize, &self.mem_regions, access_info)
453-
{
450+
match self.get_memory_access_violation(
451+
gpa as usize,
452+
&self.mem_sections,
453+
access_info,
454+
) {
454455
Some(access_info) => access_info,
455456
None => HyperlightExit::Mmio(gpa),
456457
}
@@ -488,32 +489,33 @@ impl Hypervisor for HypervWindowsDriver {
488489

489490
#[cfg(crashdump)]
490491
fn get_memory_regions(&self) -> &[MemoryRegion] {
491-
&self.mem_regions
492+
&self.mem_sections
492493
}
493494
}
494495

495-
#[cfg(test)]
496-
pub mod tests {
497-
use std::sync::{Arc, Mutex};
498-
499-
use serial_test::serial;
500-
501-
use crate::hypervisor::handlers::{MemAccessHandler, OutBHandler};
502-
use crate::hypervisor::tests::test_initialise;
503-
use crate::Result;
504-
505-
#[test]
506-
#[serial]
507-
fn test_init() {
508-
let outb_handler = {
509-
let func: Box<dyn FnMut(u16, u64) -> Result<()> + Send> =
510-
Box::new(|_, _| -> Result<()> { Ok(()) });
511-
Arc::new(Mutex::new(OutBHandler::from(func)))
512-
};
513-
let mem_access_handler = {
514-
let func: Box<dyn FnMut() -> Result<()> + Send> = Box::new(|| -> Result<()> { Ok(()) });
515-
Arc::new(Mutex::new(MemAccessHandler::from(func)))
516-
};
517-
test_initialise(outb_handler, mem_access_handler).unwrap();
518-
}
519-
}
496+
// TODO(danbugs:297): bring back
497+
// #[cfg(test)]
498+
// pub mod tests {
499+
// use std::sync::{Arc, Mutex};
500+
//
501+
// use serial_test::serial;
502+
//
503+
// use crate::hypervisor::handlers::{MemAccessHandler, OutBHandler};
504+
// use crate::hypervisor::tests::test_initialise;
505+
// use crate::Result;
506+
//
507+
// #[test]
508+
// #[serial]
509+
// fn test_init() {
510+
// let outb_handler = {
511+
// let func: Box<dyn FnMut(u16, u64) -> Result<()> + Send> =
512+
// Box::new(|_, _| -> Result<()> { Ok(()) });
513+
// Arc::new(Mutex::new(OutBHandler::from(func)))
514+
// };
515+
// let mem_access_handler = {
516+
// let func: Box<dyn FnMut() -> Result<()> + Send> = Box::new(|| -> Result<()> { Ok(()) });
517+
// Arc::new(Mutex::new(MemAccessHandler::from(func)))
518+
// };
519+
// test_initialise(outb_handler, mem_access_handler).unwrap();
520+
// }
521+
// }

src/hyperlight_host/src/hypervisor/hypervisor_handler.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ use crate::hypervisor::Hypervisor;
4646
use crate::mem::mgr::SandboxMemoryManager;
4747
use crate::mem::ptr::{GuestPtr, RawPtr};
4848
use crate::mem::ptr_offset::Offset;
49+
#[cfg(target_os = "windows")]
50+
use crate::mem::shared_mem::SharedMemory;
4951
use crate::mem::shared_mem::{GuestSharedMemory, HostSharedMemory};
5052
#[cfg(gdb)]
5153
use crate::sandbox::config::DebugInfo;
@@ -920,7 +922,7 @@ fn set_up_hypervisor_partition(
920922
mgr.shared_mem.raw_ptr() as *mut c_void, // and instead convert it to base_addr where needed in the driver itself
921923
pml4_ptr.absolute()?,
922924
entrypoint_ptr.absolute()?,
923-
rsp_ptr,
925+
rsp_ptr.absolute()?,
924926
HandleWrapper::from(mmap_file_handle),
925927
)?;
926928
Ok(Box::new(hv))

src/hyperlight_host/src/hypervisor/surrogate_process_manager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::mem::size_of;
2121
use std::path::{Path, PathBuf};
2222

2323
use crossbeam_channel::{unbounded, Receiver, Sender};
24-
use hyperlight_common::mem::PAGE_SIZE;
24+
use hyperlight_common::PAGE_SIZE;
2525
use rust_embed::RustEmbed;
2626
use tracing::{error, info, instrument, Span};
2727
use windows::core::{s, PCSTR};
@@ -429,7 +429,7 @@ mod tests {
429429
use std::thread;
430430
use std::time::{Duration, Instant};
431431

432-
use hyperlight_common::mem::PAGE_SIZE;
432+
use hyperlight_common::PAGE_SIZE;
433433
use rand::{rng, Rng};
434434
use serial_test::serial;
435435
use windows::Win32::Foundation::{CloseHandle, HANDLE, INVALID_HANDLE_VALUE};

src/hyperlight_host/src/hypervisor/windows_hypervisor_platform.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use windows_result::HRESULT;
2525

2626
use super::wrappers::HandleWrapper;
2727
use crate::hypervisor::wrappers::{WHvFPURegisters, WHvGeneralRegisters, WHvSpecialRegisters};
28-
use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags};
28+
use crate::sandbox::sandbox_builder::{MemoryRegionFlags, SandboxMemorySections};
2929
use crate::{new_error, Result};
3030

3131
// We need to pass in a primitive array of register names/values
@@ -92,7 +92,7 @@ impl VMPartition {
9292
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
9393
pub(super) fn map_gpa_range(
9494
&mut self,
95-
regions: &[MemoryRegion],
95+
regions: &SandboxMemorySections,
9696
process_handle: HandleWrapper,
9797
) -> Result<()> {
9898
let process_handle: HANDLE = process_handle.into();
@@ -106,7 +106,7 @@ impl VMPartition {
106106
}
107107
};
108108

109-
regions.iter().try_for_each(|region| unsafe {
109+
regions.iter().try_for_each(|(_, region)| unsafe {
110110
let flags = region
111111
.flags
112112
.iter()
@@ -125,9 +125,9 @@ impl VMPartition {
125125
let res = whvmapgparange2_func(
126126
self.0,
127127
process_handle,
128-
region.host_region.start as *const c_void,
129-
region.guest_region.start as u64,
130-
(region.guest_region.end - region.guest_region.start) as u64,
128+
region.host_address.ok_or("No host address mapping")? as *const c_void,
129+
region.page_aligned_guest_offset as u64,
130+
region.page_aligned_size as u64,
131131
flags,
132132
);
133133
if res.is_err() {

src/hyperlight_host/src/mem/loaded_lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl LoadedLib {
6666
}
6767

6868
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
69-
pub(super) fn base_addr(&self) -> RawPtr {
69+
pub(crate) fn base_addr(&self) -> RawPtr {
7070
self.inner.base_addr()
7171
}
7272
}

src/hyperlight_host/src/mem/shared_mem.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ impl ExclusiveSharedMemory {
407407
if total_size % PAGE_SIZE != 0 {
408408
return Err(new_error!(
409409
"shared memory must be a multiple of {}",
410-
PAGE_SIZE_USIZE
410+
PAGE_SIZE
411411
));
412412
}
413413

src/hyperlight_host/src/sandbox/sandbox_builder.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ impl SandboxBuilder {
610610

611611
SandboxMemoryManager::new(
612612
exclusive_shared_memory,
613-
RawPtr(lib.base_addr() as u64),
613+
lib.base_addr(),
614614
guest_binary_exe_info.entrypoint(),
615615
memory_sections,
616616
init_rsp,
@@ -858,6 +858,31 @@ impl From<SandboxMemorySection> for mshv_bindings::mshv_user_mem_region {
858858
}
859859
}
860860

861+
// Check to see if the current version of Windows is supported
862+
// Hyperlight is only supported on Windows 11 and Windows Server 2022 and later
863+
#[cfg(target_os = "windows")]
864+
fn check_windows_version() -> Result<()> {
865+
use windows_version::{is_server, OsVersion};
866+
const WINDOWS_MAJOR: u32 = 10;
867+
const WINDOWS_MINOR: u32 = 0;
868+
const WINDOWS_PACK: u32 = 0;
869+
870+
// Windows Server 2022 has version numbers 10.0.20348 or greater
871+
if is_server() {
872+
if OsVersion::current() < OsVersion::new(WINDOWS_MAJOR, WINDOWS_MINOR, WINDOWS_PACK, 20348)
873+
{
874+
return Err(crate::new_error!(
875+
"Hyperlight Requires Windows Server 2022 or newer"
876+
));
877+
}
878+
} else if OsVersion::current()
879+
< OsVersion::new(WINDOWS_MAJOR, WINDOWS_MINOR, WINDOWS_PACK, 22000)
880+
{
881+
return Err(crate::new_error!("Hyperlight Requires Windows 11 or newer"));
882+
}
883+
Ok(())
884+
}
885+
861886
#[cfg(test)]
862887
mod tests {
863888
use std::sync::{Arc, Mutex};

src/hyperlight_host/src/sandbox/uninitialized.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -102,31 +102,6 @@ impl UninitializedSandbox {
102102
}
103103
}
104104

105-
// Check to see if the current version of Windows is supported
106-
// Hyperlight is only supported on Windows 11 and Windows Server 2022 and later
107-
#[cfg(target_os = "windows")]
108-
fn check_windows_version() -> Result<()> {
109-
use windows_version::{is_server, OsVersion};
110-
const WINDOWS_MAJOR: u32 = 10;
111-
const WINDOWS_MINOR: u32 = 0;
112-
const WINDOWS_PACK: u32 = 0;
113-
114-
// Windows Server 2022 has version numbers 10.0.20348 or greater
115-
if is_server() {
116-
if OsVersion::current() < OsVersion::new(WINDOWS_MAJOR, WINDOWS_MINOR, WINDOWS_PACK, 20348)
117-
{
118-
return Err(new_error!(
119-
"Hyperlight Requires Windows Server 2022 or newer"
120-
));
121-
}
122-
} else if OsVersion::current()
123-
< OsVersion::new(WINDOWS_MAJOR, WINDOWS_MINOR, WINDOWS_PACK, 22000)
124-
{
125-
return Err(new_error!("Hyperlight Requires Windows 11 or newer"));
126-
}
127-
Ok(())
128-
}
129-
130105
// TODO(danbugs:297): bring back tests
131106
// #[cfg(test)]
132107
// mod tests {

0 commit comments

Comments
 (0)