@@ -19,9 +19,10 @@ use std::collections::HashMap;
1919use  windows:: Win32 :: System :: Hypervisor :: WHV_VP_EXCEPTION_CONTEXT ; 
2020
2121use  super :: arch:: { MAX_NO_OF_HW_BP ,  vcpu_stop_reason} ; 
22- use  super :: { GuestDebug ,  SW_BP_SIZE ,  VcpuStopReason ,  X86_64Regs } ; 
22+ use  super :: { GuestDebug ,  SW_BP_SIZE ,  VcpuStopReason } ; 
23+ use  crate :: hypervisor:: regs:: { CommonFpu ,  CommonRegisters } ; 
2324use  crate :: hypervisor:: windows_hypervisor_platform:: VMProcessor ; 
24- use  crate :: hypervisor:: wrappers:: { WHvDebugRegisters ,   WHvGeneralRegisters } ; 
25+ use  crate :: hypervisor:: wrappers:: WHvDebugRegisters ; 
2526use  crate :: { HyperlightError ,  Result ,  new_error} ; 
2627
2728/// KVM Debug struct 
@@ -54,7 +55,7 @@ impl HypervDebug {
5455    /// Returns the instruction pointer from the stopped vCPU 
5556fn  get_instruction_pointer ( & self ,  vcpu_fd :  & VMProcessor )  -> Result < u64 >  { 
5657        let  regs = vcpu_fd
57-             . get_regs ( ) 
58+             . regs ( ) 
5859            . map_err ( |e| new_error ! ( "Could not retrieve registers from vCPU: {:?}" ,  e) ) ?; 
5960
6061        Ok ( regs. rip ) 
@@ -103,7 +104,7 @@ impl HypervDebug {
103104        self . single_step  = step; 
104105
105106        let  mut  regs = vcpu_fd
106-             . get_regs ( ) 
107+             . regs ( ) 
107108            . map_err ( |e| new_error ! ( "Could not get registers: {:?}" ,  e) ) ?; 
108109
109110        // Set TF Flag to enable Traps 
@@ -114,7 +115,7 @@ impl HypervDebug {
114115        } 
115116
116117        vcpu_fd
117-             . set_general_purpose_registers ( & regs) 
118+             . set_regs ( & regs) 
118119            . map_err ( |e| new_error ! ( "Could not set guest registers: {:?}" ,  e) ) ?; 
119120
120121        Ok ( ( ) ) 
@@ -185,45 +186,17 @@ impl GuestDebug for HypervDebug {
185186        self . sw_breakpoints . remove ( addr) 
186187    } 
187188
188-     fn  read_regs ( & self ,  vcpu_fd :  & Self :: Vcpu ,   regs :   & mut   X86_64Regs )  -> Result < ( ) >  { 
189+     fn  read_regs ( & self ,  vcpu_fd :  & Self :: Vcpu )  -> Result < ( CommonRegisters ,   CommonFpu ) >  { 
189190        log:: debug!( "Read registers" ) ; 
190-         let  vcpu_regs  = vcpu_fd
191-             . get_regs ( ) 
191+         let  regs  = vcpu_fd
192+             . regs ( ) 
192193            . map_err ( |e| new_error ! ( "Could not read guest registers: {:?}" ,  e) ) ?; 
193194
194-         regs. rax  = vcpu_regs. rax ; 
195-         regs. rbx  = vcpu_regs. rbx ; 
196-         regs. rcx  = vcpu_regs. rcx ; 
197-         regs. rdx  = vcpu_regs. rdx ; 
198-         regs. rsi  = vcpu_regs. rsi ; 
199-         regs. rdi  = vcpu_regs. rdi ; 
200-         regs. rbp  = vcpu_regs. rbp ; 
201-         regs. rsp  = vcpu_regs. rsp ; 
202-         regs. r8  = vcpu_regs. r8 ; 
203-         regs. r9  = vcpu_regs. r9 ; 
204-         regs. r10  = vcpu_regs. r10 ; 
205-         regs. r11  = vcpu_regs. r11 ; 
206-         regs. r12  = vcpu_regs. r12 ; 
207-         regs. r13  = vcpu_regs. r13 ; 
208-         regs. r14  = vcpu_regs. r14 ; 
209-         regs. r15  = vcpu_regs. r15 ; 
210- 
211-         regs. rip  = vcpu_regs. rip ; 
212-         regs. rflags  = vcpu_regs. rflags ; 
213- 
214-         // Fetch XMM from WHVP 
215-         if  let  Ok ( fpu)  = vcpu_fd. get_fpu ( )  { 
216-             regs. xmm  = [ 
217-                 fpu. xmm0 ,  fpu. xmm1 ,  fpu. xmm2 ,  fpu. xmm3 ,  fpu. xmm4 ,  fpu. xmm5 ,  fpu. xmm6 ,  fpu. xmm7 , 
218-                 fpu. xmm8 ,  fpu. xmm9 ,  fpu. xmm10 ,  fpu. xmm11 ,  fpu. xmm12 ,  fpu. xmm13 ,  fpu. xmm14 , 
219-                 fpu. xmm15 , 
220-             ] ; 
221-             regs. mxcsr  = fpu. mxcsr ; 
222-         }  else  { 
223-             log:: warn!( "Failed to read FPU/XMM via WHVP for debug registers" ) ; 
224-         } 
195+         let  fpu = vcpu_fd
196+             . fpu ( ) 
197+             . map_err ( |e| new_error ! ( "Could not read guest FPU registers: {:?}" ,  e) ) ?; 
225198
226-         Ok ( ( ) ) 
199+         Ok ( ( regs ,  fpu ) ) 
227200    } 
228201
229202    fn  set_single_step ( & mut  self ,  vcpu_fd :  & Self :: Vcpu ,  enable :  bool )  -> Result < ( ) >  { 
@@ -236,62 +209,28 @@ impl GuestDebug for HypervDebug {
236209            . map_err ( |_| HyperlightError :: TranslateGuestAddress ( gva) ) 
237210    } 
238211
239-     fn  write_regs ( & self ,  vcpu_fd :  & Self :: Vcpu ,  regs :  & X86_64Regs )  -> Result < ( ) >  { 
212+     fn  write_regs ( 
213+         & self , 
214+         vcpu_fd :  & Self :: Vcpu , 
215+         regs :  & CommonRegisters , 
216+         fpu :  & CommonFpu , 
217+     )  -> Result < ( ) >  { 
240218        log:: debug!( "Write registers" ) ; 
241-         let  gprs = WHvGeneralRegisters  { 
242-             rax :  regs. rax , 
243-             rbx :  regs. rbx , 
244-             rcx :  regs. rcx , 
245-             rdx :  regs. rdx , 
246-             rsi :  regs. rsi , 
247-             rdi :  regs. rdi , 
248-             rbp :  regs. rbp , 
249-             rsp :  regs. rsp , 
250-             r8 :  regs. r8 , 
251-             r9 :  regs. r9 , 
252-             r10 :  regs. r10 , 
253-             r11 :  regs. r11 , 
254-             r12 :  regs. r12 , 
255-             r13 :  regs. r13 , 
256-             r14 :  regs. r14 , 
257-             r15 :  regs. r15 , 
258- 
259-             rip :  regs. rip , 
260-             rflags :  regs. rflags , 
261-         } ; 
262219
263220        vcpu_fd
264-             . set_general_purpose_registers ( & gprs ) 
221+             . set_regs ( regs ) 
265222            . map_err ( |e| new_error ! ( "Could not write guest registers: {:?}" ,  e) ) ?; 
266223
267-         // Load existing FPU state, replace XMM and MXCSR, and write it back. 
268-         let  mut  fpu = match  vcpu_fd. get_fpu ( )  { 
269-             Ok ( f)  => f, 
270-             Err ( e)  => { 
271-                 return  Err ( new_error ! ( "Could not write guest registers: {:?}" ,  e) ) ; 
272-             } 
273-         } ; 
274- 
275-         fpu. xmm0  = regs. xmm [ 0 ] ; 
276-         fpu. xmm1  = regs. xmm [ 1 ] ; 
277-         fpu. xmm2  = regs. xmm [ 2 ] ; 
278-         fpu. xmm3  = regs. xmm [ 3 ] ; 
279-         fpu. xmm4  = regs. xmm [ 4 ] ; 
280-         fpu. xmm5  = regs. xmm [ 5 ] ; 
281-         fpu. xmm6  = regs. xmm [ 6 ] ; 
282-         fpu. xmm7  = regs. xmm [ 7 ] ; 
283-         fpu. xmm8  = regs. xmm [ 8 ] ; 
284-         fpu. xmm9  = regs. xmm [ 9 ] ; 
285-         fpu. xmm10  = regs. xmm [ 10 ] ; 
286-         fpu. xmm11  = regs. xmm [ 11 ] ; 
287-         fpu. xmm12  = regs. xmm [ 12 ] ; 
288-         fpu. xmm13  = regs. xmm [ 13 ] ; 
289-         fpu. xmm14  = regs. xmm [ 14 ] ; 
290-         fpu. xmm15  = regs. xmm [ 15 ] ; 
291-         fpu. mxcsr  = regs. mxcsr ; 
224+         // Only xmm and mxcsr is piped though in the given fpu, so only set those 
225+         let  mut  current_fpu:  CommonFpu  = vcpu_fd
226+             . fpu ( ) 
227+             . map_err ( |e| new_error ! ( "Could not read guest FPU registers: {:?}" ,  e) ) ?; 
228+         current_fpu. mxcsr  = fpu. mxcsr ; 
229+         current_fpu. xmm  = fpu. xmm ; 
292230
293231        vcpu_fd
294-             . set_fpu ( & fpu) 
295-             . map_err ( |e| new_error ! ( "Could not write guest registers: {:?}" ,  e) ) 
232+             . set_fpu ( & current_fpu) 
233+             . map_err ( |e| new_error ! ( "Could not write guest FPU registers: {:?}" ,  e) ) ?; 
234+         Ok ( ( ) ) 
296235    } 
297236} 
0 commit comments