Skip to content

Commit 1b0c76b

Browse files
committed
Update hyperv driver so that:
When the driver is created a handle to the FileMapping for the memory for this sandbox. Uses that handle as an argument to the get_surrogate_process function on the SurrogateProcessManager Removes the code that copies the Sandbox memory to and from the surrogate process memory each time the VM is run. Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
1 parent 0571129 commit 1b0c76b

File tree

1 file changed

+7
-54
lines changed

1 file changed

+7
-54
lines changed

src/hyperlight_host/src/hypervisor/hyperv_windows.rs

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use std::string::String;
2121

2222
use hyperlight_common::mem::PAGE_SIZE_USIZE;
2323
use tracing::{instrument, Span};
24-
use windows::Win32::Foundation::HANDLE;
2524
use windows::Win32::System::Hypervisor::{
2625
WHvX64RegisterCr0, WHvX64RegisterCr3, WHvX64RegisterCr4, WHvX64RegisterCs, WHvX64RegisterEfer,
2726
WHV_MEMORY_ACCESS_TYPE, WHV_PARTITION_HANDLE, WHV_REGISTER_VALUE, WHV_RUN_VP_EXIT_CONTEXT,
@@ -33,7 +32,7 @@ use super::handlers::{MemAccessHandlerWrapper, OutBHandlerWrapper};
3332
use super::surrogate_process::SurrogateProcess;
3433
use super::surrogate_process_manager::*;
3534
use super::windows_hypervisor_platform::{VMPartition, VMProcessor};
36-
use super::wrappers::WHvFPURegisters;
35+
use super::wrappers::{HandleWrapper, WHvFPURegisters};
3736
use super::{
3837
HyperlightExit, Hypervisor, VirtualCPU, CR0_AM, CR0_ET, CR0_MP, CR0_NE, CR0_PE, CR0_PG, CR0_WP,
3938
CR4_OSFXSR, CR4_OSXMMEXCPT, CR4_PAE, EFER_LMA, EFER_LME, EFER_NX, EFER_SCE,
@@ -43,15 +42,14 @@ use crate::hypervisor::hypervisor_handler::HypervisorHandler;
4342
use crate::hypervisor::wrappers::WHvGeneralRegisters;
4443
use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags};
4544
use crate::mem::ptr::{GuestPtr, RawPtr};
46-
use crate::HyperlightError::WindowsAPIError;
47-
use crate::{debug, log_then_return, new_error, Result};
45+
use crate::{debug, new_error, Result};
4846

4947
/// A Hypervisor driver for HyperV-on-Windows.
5048
pub(crate) struct HypervWindowsDriver {
5149
size: usize, // this is the size of the memory region, excluding the 2 surrounding guard pages
5250
processor: VMProcessor,
53-
surrogate_process: SurrogateProcess,
54-
source_address: *mut c_void, // this points into the first guard page
51+
_surrogate_process: SurrogateProcess, // we need to keep a reference to the SurrogateProcess for the duration of the driver since otherwise it will dropped and the memory mapping will be unmapped and the surrogate process will be returned to the pool
52+
source_address: *mut c_void, // this points into the first guard page
5553
entrypoint: u64,
5654
orig_rsp: GuestPtr,
5755
mem_regions: Vec<MemoryRegion>,
@@ -73,6 +71,7 @@ impl HypervWindowsDriver {
7371
pml4_address: u64,
7472
entrypoint: u64,
7573
rsp: u64,
74+
mmap_file_handle: HandleWrapper,
7675
) -> Result<Self> {
7776
// create and setup hypervisor partition
7877
let mut partition = VMPartition::new(1)?;
@@ -81,7 +80,7 @@ impl HypervWindowsDriver {
8180
// with guard pages setup
8281
let surrogate_process = {
8382
let mgr = get_surrogate_process_manager()?;
84-
mgr.get_surrogate_process(raw_size, raw_source_address)
83+
mgr.get_surrogate_process(raw_size, raw_source_address, mmap_file_handle)
8584
}?;
8685

8786
partition.map_gpa_range(&mem_regions, surrogate_process.process_handle)?;
@@ -95,7 +94,7 @@ impl HypervWindowsDriver {
9594
Ok(Self {
9695
size: mem_size,
9796
processor: proc,
98-
surrogate_process,
97+
_surrogate_process: surrogate_process,
9998
source_address: raw_source_address,
10099
entrypoint,
101100
orig_rsp: GuestPtr::try_from(RawPtr::from(rsp))?,
@@ -402,54 +401,8 @@ impl Hypervisor for HypervWindowsDriver {
402401

403402
#[instrument(err(Debug), skip_all, parent = Span::current(), level = "Trace")]
404403
fn run(&mut self) -> Result<super::HyperlightExit> {
405-
let bytes_written: Option<*mut usize> = None;
406-
let bytes_read: Option<*mut usize> = None;
407-
let handle: HANDLE = self.surrogate_process.process_handle.into();
408-
409-
// TODO optimise this
410-
// the following write to and read from process memory is required as we need to use
411-
// surrogate processes to allow more than one WHP Partition per process
412-
// see HyperVSurrogateProcessManager
413-
// this needs updating so that
414-
// 1. it only writes to memory that changes between usage
415-
// 2. memory is allocated in the process once and then only freed and reallocated if the
416-
// memory needs to grow.
417-
418-
// - copy stuff to surrogate process
419-
420-
if let Err(e) = unsafe {
421-
windows::Win32::System::Diagnostics::Debug::WriteProcessMemory(
422-
handle,
423-
self.surrogate_process
424-
.allocated_address
425-
.add(PAGE_SIZE_USIZE),
426-
self.source_address.add(PAGE_SIZE_USIZE),
427-
self.size,
428-
bytes_written,
429-
)
430-
} {
431-
log_then_return!(WindowsAPIError(e.clone()));
432-
}
433-
434-
// - call WHvRunVirtualProcessor
435404
let exit_context: WHV_RUN_VP_EXIT_CONTEXT = self.processor.run()?;
436405

437-
// - call read-process memory
438-
439-
if let Err(e) = unsafe {
440-
windows::Win32::System::Diagnostics::Debug::ReadProcessMemory(
441-
handle,
442-
self.surrogate_process
443-
.allocated_address
444-
.add(PAGE_SIZE_USIZE),
445-
self.source_address.add(PAGE_SIZE_USIZE),
446-
self.size,
447-
bytes_read,
448-
)
449-
} {
450-
log_then_return!(WindowsAPIError(e.clone()));
451-
}
452-
453406
let result = match exit_context.ExitReason {
454407
// WHvRunVpExitReasonX64IoPortAccess
455408
WHV_RUN_VP_EXIT_REASON(2i32) => {

0 commit comments

Comments
 (0)