@@ -21,7 +21,8 @@ use std::sync::LazyLock;
2121#[ cfg( gdb) ]
2222use mshv_bindings:: { DebugRegisters , hv_message_type_HVMSG_X64_EXCEPTION_INTERCEPT} ;
2323use mshv_bindings:: {
24- hv_message_type, hv_message_type_HVMSG_GPA_INTERCEPT, hv_message_type_HVMSG_UNMAPPED_GPA,
24+ FloatingPointUnit , SpecialRegisters , StandardRegisters , hv_message_type,
25+ hv_message_type_HVMSG_GPA_INTERCEPT, hv_message_type_HVMSG_UNMAPPED_GPA,
2526 hv_message_type_HVMSG_X64_HALT, hv_message_type_HVMSG_X64_IO_PORT_INTERCEPT,
2627 hv_partition_property_code_HV_PARTITION_PROPERTY_SYNTHETIC_PROC_FEATURES,
2728 hv_partition_synthetic_processor_features, hv_register_assoc,
@@ -32,7 +33,6 @@ use tracing::{Span, instrument};
3233
3334#[ cfg( gdb) ]
3435use crate :: hypervisor:: gdb:: DebuggableVm ;
35- use crate :: hypervisor:: regs:: { CommonFpu , CommonRegisters , CommonSpecialRegisters } ;
3636use crate :: hypervisor:: { HyperlightExit , Hypervisor } ;
3737use crate :: mem:: memory_region:: { MemoryRegion , MemoryRegionFlags } ;
3838use crate :: { Result , new_error} ;
@@ -90,41 +90,6 @@ impl MshvVm {
9090}
9191
9292impl Hypervisor for MshvVm {
93- fn regs ( & self ) -> Result < CommonRegisters > {
94- Ok ( ( & self . vcpu_fd . get_regs ( ) ?) . into ( ) )
95- }
96-
97- fn set_regs ( & self , regs : & CommonRegisters ) -> Result < ( ) > {
98- Ok ( self . vcpu_fd . set_regs ( & regs. into ( ) ) ?)
99- }
100-
101- fn sregs ( & self ) -> Result < CommonSpecialRegisters > {
102- Ok ( ( & self . vcpu_fd . get_sregs ( ) ?) . into ( ) )
103- }
104-
105- fn set_sregs ( & self , sregs : & CommonSpecialRegisters ) -> Result < ( ) > {
106- self . vcpu_fd . set_sregs ( & sregs. into ( ) ) ?;
107- Ok ( ( ) )
108- }
109-
110- fn fpu ( & self ) -> Result < CommonFpu > {
111- Ok ( ( & self . vcpu_fd . get_fpu ( ) ?) . into ( ) )
112- }
113-
114- fn set_fpu ( & self , fpu : & CommonFpu ) -> Result < ( ) > {
115- self . vcpu_fd . set_fpu ( & fpu. into ( ) ) ?;
116- Ok ( ( ) )
117- }
118-
119- #[ cfg( crashdump) ]
120- fn xsave ( & self ) -> Result < Vec < u8 > > {
121- let xsave = self . vcpu_fd . get_xsave ( ) ?;
122- Ok ( xsave. buffer . to_vec ( ) )
123- }
124-
125- /// # Safety
126- /// The caller must ensure that the memory region is valid and points to valid memory,
127- /// and lives long enough for the VM to use it.
12893 unsafe fn map_memory ( & mut self , ( _slot, region) : ( u32 , & MemoryRegion ) ) -> Result < ( ) > {
12994 let mshv_region: mshv_user_mem_region = region. into ( ) ;
13095 self . vm_fd . map_user_memory ( mshv_region) ?;
@@ -209,6 +174,45 @@ impl Hypervisor for MshvVm {
209174 } ;
210175 Ok ( result)
211176 }
177+
178+ fn regs ( & self ) -> Result < super :: regs:: CommonRegisters > {
179+ let mshv_regs = self . vcpu_fd . get_regs ( ) ?;
180+ Ok ( ( & mshv_regs) . into ( ) )
181+ }
182+
183+ fn set_regs ( & self , regs : & super :: regs:: CommonRegisters ) -> Result < ( ) > {
184+ let mshv_regs: StandardRegisters = regs. into ( ) ;
185+ self . vcpu_fd . set_regs ( & mshv_regs) ?;
186+ Ok ( ( ) )
187+ }
188+
189+ fn fpu ( & self ) -> Result < super :: regs:: CommonFpu > {
190+ let mshv_fpu = self . vcpu_fd . get_fpu ( ) ?;
191+ Ok ( ( & mshv_fpu) . into ( ) )
192+ }
193+
194+ fn set_fpu ( & self , fpu : & super :: regs:: CommonFpu ) -> Result < ( ) > {
195+ let mshv_fpu: FloatingPointUnit = fpu. into ( ) ;
196+ self . vcpu_fd . set_fpu ( & mshv_fpu) ?;
197+ Ok ( ( ) )
198+ }
199+
200+ fn sregs ( & self ) -> Result < super :: regs:: CommonSpecialRegisters > {
201+ let mshv_sregs = self . vcpu_fd . get_sregs ( ) ?;
202+ Ok ( ( & mshv_sregs) . into ( ) )
203+ }
204+
205+ fn set_sregs ( & self , sregs : & super :: regs:: CommonSpecialRegisters ) -> Result < ( ) > {
206+ let mshv_sregs: SpecialRegisters = sregs. into ( ) ;
207+ self . vcpu_fd . set_sregs ( & mshv_sregs) ?;
208+ Ok ( ( ) )
209+ }
210+
211+ #[ cfg( crashdump) ]
212+ fn xsave ( & self ) -> Result < Vec < u8 > > {
213+ let xsave = self . vcpu_fd . get_xsave ( ) ?;
214+ Ok ( xsave. buffer . to_vec ( ) )
215+ }
212216}
213217
214218#[ cfg( gdb) ]
@@ -229,37 +233,36 @@ impl DebuggableVm for MshvVm {
229233
230234 fn set_debug ( & mut self , enabled : bool ) -> Result < ( ) > {
231235 use mshv_bindings:: {
232- HV_INTERCEPT_ACCESS_MASK_EXECUTE , hv_intercept_parameters,
233- hv_intercept_type_HV_INTERCEPT_TYPE_EXCEPTION, mshv_install_intercept,
236+ HV_INTERCEPT_ACCESS_MASK_EXECUTE , HV_INTERCEPT_ACCESS_MASK_NONE ,
237+ hv_intercept_parameters, hv_intercept_type_HV_INTERCEPT_TYPE_EXCEPTION,
238+ mshv_install_intercept,
234239 } ;
235240
236241 use crate :: hypervisor:: gdb:: arch:: { BP_EX_ID , DB_EX_ID } ;
237242
238- if enabled {
239- self . vm_fd
240- . install_intercept ( mshv_install_intercept {
241- access_type_mask : HV_INTERCEPT_ACCESS_MASK_EXECUTE ,
242- intercept_type : hv_intercept_type_HV_INTERCEPT_TYPE_EXCEPTION,
243- // Exception handler #DB (1)
244- intercept_parameter : hv_intercept_parameters {
245- exception_vector : DB_EX_ID as u16 ,
246- } ,
247- } )
248- . map_err ( |e| new_error ! ( "Cannot install debug exception intercept: {}" , e) ) ?;
243+ let access_type_mask = if enabled {
244+ HV_INTERCEPT_ACCESS_MASK_EXECUTE
245+ } else {
246+ HV_INTERCEPT_ACCESS_MASK_NONE
247+ } ;
249248
250- // Install intercept for #BP (3) exception
249+ for vector in [ DB_EX_ID , BP_EX_ID ] {
251250 self . vm_fd
252251 . install_intercept ( mshv_install_intercept {
253- access_type_mask : HV_INTERCEPT_ACCESS_MASK_EXECUTE ,
252+ access_type_mask,
254253 intercept_type : hv_intercept_type_HV_INTERCEPT_TYPE_EXCEPTION,
255- // Exception handler #BP (3)
256254 intercept_parameter : hv_intercept_parameters {
257- exception_vector : BP_EX_ID as u16 ,
255+ exception_vector : vector as u16 ,
258256 } ,
259257 } )
260- . map_err ( |e| new_error ! ( "Cannot install breakpoint exception intercept: {}" , e) ) ?;
261- } else {
262- // There doesn't seem to be any way to remove installed intercepts. But that's okay.
258+ . map_err ( |e| {
259+ new_error ! (
260+ "Cannot {} exception intercept for vector {}: {}" ,
261+ if enabled { "install" } else { "remove" } ,
262+ vector,
263+ e
264+ )
265+ } ) ?;
263266 }
264267 Ok ( ( ) )
265268 }
0 commit comments