@@ -21,7 +21,6 @@ use std::string::String;
21
21
22
22
use hyperlight_common:: mem:: PAGE_SIZE_USIZE ;
23
23
use tracing:: { instrument, Span } ;
24
- use windows:: Win32 :: Foundation :: HANDLE ;
25
24
use windows:: Win32 :: System :: Hypervisor :: {
26
25
WHvX64RegisterCr0 , WHvX64RegisterCr3 , WHvX64RegisterCr4 , WHvX64RegisterCs , WHvX64RegisterEfer ,
27
26
WHV_MEMORY_ACCESS_TYPE , WHV_PARTITION_HANDLE , WHV_REGISTER_VALUE , WHV_RUN_VP_EXIT_CONTEXT ,
@@ -33,7 +32,7 @@ use super::handlers::{MemAccessHandlerWrapper, OutBHandlerWrapper};
33
32
use super :: surrogate_process:: SurrogateProcess ;
34
33
use super :: surrogate_process_manager:: * ;
35
34
use super :: windows_hypervisor_platform:: { VMPartition , VMProcessor } ;
36
- use super :: wrappers:: WHvFPURegisters ;
35
+ use super :: wrappers:: { HandleWrapper , WHvFPURegisters } ;
37
36
use super :: {
38
37
HyperlightExit , Hypervisor , VirtualCPU , CR0_AM , CR0_ET , CR0_MP , CR0_NE , CR0_PE , CR0_PG , CR0_WP ,
39
38
CR4_OSFXSR , CR4_OSXMMEXCPT , CR4_PAE , EFER_LMA , EFER_LME , EFER_NX , EFER_SCE ,
@@ -43,15 +42,14 @@ use crate::hypervisor::hypervisor_handler::HypervisorHandler;
43
42
use crate :: hypervisor:: wrappers:: WHvGeneralRegisters ;
44
43
use crate :: mem:: memory_region:: { MemoryRegion , MemoryRegionFlags } ;
45
44
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 } ;
48
46
49
47
/// A Hypervisor driver for HyperV-on-Windows.
50
48
pub ( crate ) struct HypervWindowsDriver {
51
49
size : usize , // this is the size of the memory region, excluding the 2 surrounding guard pages
52
50
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
55
53
entrypoint : u64 ,
56
54
orig_rsp : GuestPtr ,
57
55
mem_regions : Vec < MemoryRegion > ,
@@ -73,6 +71,7 @@ impl HypervWindowsDriver {
73
71
pml4_address : u64 ,
74
72
entrypoint : u64 ,
75
73
rsp : u64 ,
74
+ mmap_file_handle : HandleWrapper ,
76
75
) -> Result < Self > {
77
76
// create and setup hypervisor partition
78
77
let mut partition = VMPartition :: new ( 1 ) ?;
@@ -81,7 +80,7 @@ impl HypervWindowsDriver {
81
80
// with guard pages setup
82
81
let surrogate_process = {
83
82
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 )
85
84
} ?;
86
85
87
86
partition. map_gpa_range ( & mem_regions, surrogate_process. process_handle ) ?;
@@ -95,7 +94,7 @@ impl HypervWindowsDriver {
95
94
Ok ( Self {
96
95
size : mem_size,
97
96
processor : proc,
98
- surrogate_process,
97
+ _surrogate_process : surrogate_process,
99
98
source_address : raw_source_address,
100
99
entrypoint,
101
100
orig_rsp : GuestPtr :: try_from ( RawPtr :: from ( rsp) ) ?,
@@ -402,54 +401,8 @@ impl Hypervisor for HypervWindowsDriver {
402
401
403
402
#[ instrument( err( Debug ) , skip_all, parent = Span :: current( ) , level = "Trace" ) ]
404
403
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
435
404
let exit_context: WHV_RUN_VP_EXIT_CONTEXT = self . processor . run ( ) ?;
436
405
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
-
453
406
let result = match exit_context. ExitReason {
454
407
// WHvRunVpExitReasonX64IoPortAccess
455
408
WHV_RUN_VP_EXIT_REASON ( 2i32 ) => {
0 commit comments