@@ -19,9 +19,10 @@ use std::collections::HashMap;
19
19
use windows:: Win32 :: System :: Hypervisor :: WHV_VP_EXCEPTION_CONTEXT ;
20
20
21
21
use 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 } ;
23
24
use crate :: hypervisor:: windows_hypervisor_platform:: VMProcessor ;
24
- use crate :: hypervisor:: wrappers:: { WHvDebugRegisters , WHvGeneralRegisters } ;
25
+ use crate :: hypervisor:: wrappers:: WHvDebugRegisters ;
25
26
use crate :: { HyperlightError , Result , new_error} ;
26
27
27
28
/// KVM Debug struct
@@ -54,7 +55,7 @@ impl HypervDebug {
54
55
/// Returns the instruction pointer from the stopped vCPU
55
56
fn get_instruction_pointer ( & self , vcpu_fd : & VMProcessor ) -> Result < u64 > {
56
57
let regs = vcpu_fd
57
- . get_regs ( )
58
+ . regs ( )
58
59
. map_err ( |e| new_error ! ( "Could not retrieve registers from vCPU: {:?}" , e) ) ?;
59
60
60
61
Ok ( regs. rip )
@@ -103,7 +104,7 @@ impl HypervDebug {
103
104
self . single_step = step;
104
105
105
106
let mut regs = vcpu_fd
106
- . get_regs ( )
107
+ . regs ( )
107
108
. map_err ( |e| new_error ! ( "Could not get registers: {:?}" , e) ) ?;
108
109
109
110
// Set TF Flag to enable Traps
@@ -114,7 +115,7 @@ impl HypervDebug {
114
115
}
115
116
116
117
vcpu_fd
117
- . set_general_purpose_registers ( & regs)
118
+ . set_regs ( & regs)
118
119
. map_err ( |e| new_error ! ( "Could not set guest registers: {:?}" , e) ) ?;
119
120
120
121
Ok ( ( ) )
@@ -185,45 +186,17 @@ impl GuestDebug for HypervDebug {
185
186
self . sw_breakpoints . remove ( addr)
186
187
}
187
188
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 ) > {
189
190
log:: debug!( "Read registers" ) ;
190
- let vcpu_regs = vcpu_fd
191
- . get_regs ( )
191
+ let regs = vcpu_fd
192
+ . regs ( )
192
193
. map_err ( |e| new_error ! ( "Could not read guest registers: {:?}" , e) ) ?;
193
194
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) ) ?;
225
198
226
- Ok ( ( ) )
199
+ Ok ( ( regs , fpu ) )
227
200
}
228
201
229
202
fn set_single_step ( & mut self , vcpu_fd : & Self :: Vcpu , enable : bool ) -> Result < ( ) > {
@@ -236,62 +209,28 @@ impl GuestDebug for HypervDebug {
236
209
. map_err ( |_| HyperlightError :: TranslateGuestAddress ( gva) )
237
210
}
238
211
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 < ( ) > {
240
218
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
- } ;
262
219
263
220
vcpu_fd
264
- . set_general_purpose_registers ( & gprs )
221
+ . set_regs ( regs )
265
222
. map_err ( |e| new_error ! ( "Could not write guest registers: {:?}" , e) ) ?;
266
223
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 ;
292
230
293
231
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 ( ( ) )
296
235
}
297
236
}
0 commit comments