1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
; $Id: CPUMR0A.asm $
;; @file
; CPUM - Guest Context Assembly Routines.
;
;
; Copyright (C) 2006-2013 Oracle Corporation
;
; This file is part of VirtualBox Open Source Edition (OSE), as
; available from http://www.virtualbox.org. This file is free software;
; you can redistribute it and/or modify it under the terms of the GNU
; General Public License (GPL) as published by the Free Software
; Foundation, in version 2 as it comes in the "COPYING" file of the
; VirtualBox OSE distribution. VirtualBox OSE is distributed in the
; hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
;
;*******************************************************************************
;* Header Files *
;*******************************************************************************
%include "VBox/asmdefs.mac"
%include "VBox/vmm/vm.mac"
%include "VBox/err.mac"
%include "VBox/vmm/stam.mac"
%include "CPUMInternal.mac"
%include "iprt/x86.mac"
%include "VBox/vmm/cpum.mac"
%ifdef IN_RING3
%error "The jump table doesn't link on leopard."
%endif
;*******************************************************************************
;* Defined Constants And Macros *
;*******************************************************************************
;; The offset of the XMM registers in X86FXSTATE.
; Use define because I'm too lazy to convert the struct.
%define XMM_OFF_IN_X86FXSTATE 160
;*******************************************************************************
;* External Symbols *
;*******************************************************************************
%ifdef VBOX_WITH_HYBRID_32BIT_KERNEL
extern NAME(SUPR0AbsIs64bit)
extern NAME(SUPR0Abs64bitKernelCS)
extern NAME(SUPR0Abs64bitKernelSS)
extern NAME(SUPR0Abs64bitKernelDS)
extern NAME(SUPR0AbsKernelCS)
%endif
;*******************************************************************************
;* Global Variables *
;*******************************************************************************
%ifdef VBOX_WITH_HYBRID_32BIT_KERNEL
BEGINDATA
;;
; Store the SUPR0AbsIs64bit absolute value here so we can cmp/test without
; needing to clobber a register. (This trick doesn't quite work for PE btw.
; but that's not relevant atm.)
GLOBALNAME g_fCPUMIs64bitHost
dd NAME(SUPR0AbsIs64bit)
%endif
BEGINCODE
;; Macro for FXSAVE/FXRSTOR leaky behaviour on AMD CPUs, see cpumR3CheckLeakyFpu().
; Cleans the FPU state, if necessary, before restoring the FPU.
;
; This macro ASSUMES CR0.TS is not set!
; @remarks Trashes xAX!!
; Changes here should also be reflected in CPUMRCA.asm's copy!
%macro CLEANFPU 0
test dword [xDX + CPUMCPU.fUseFlags], CPUM_USE_FFXSR_LEAKY
jz .nothing_to_clean
xor eax, eax
fnstsw ax ; Get FSW
test eax, RT_BIT(7) ; If FSW.ES (bit 7) is set, clear it to not cause FPU exceptions
; while clearing & loading the FPU bits in 'clean_fpu'
jz .clean_fpu
fnclex
.clean_fpu:
ffree st7 ; Clear FPU stack register(7)'s tag entry to prevent overflow if a wraparound occurs
; for the upcoming push (load)
fild dword [xDX + CPUMCPU.Guest.fpu] ; Explicit FPU load to overwrite FIP, FOP, FDP registers in the FPU.
.nothing_to_clean:
%endmacro
;; Macro to save and modify CR0 (if necessary) before touching the FPU state
; so as to not cause any FPU exceptions.
;
; @remarks Uses xCX for backing-up CR0 (if CR0 needs to be modified) otherwise clears xCX.
; @remarks Trashes xAX.
%macro SAVE_CR0_CLEAR_FPU_TRAPS 0
xor ecx, ecx
mov xAX, cr0
test eax, X86_CR0_TS | X86_CR0_EM ; Make sure its safe to access the FPU state.
jz %%skip_cr0_write
mov xCX, xAX ; Save old CR0
and xAX, ~(X86_CR0_TS | X86_CR0_EM)
mov cr0, xAX
%%skip_cr0_write:
%endmacro
;; Macro to restore CR0 from xCX if necessary.
;
; @remarks xCX should contain the CR0 value to restore or 0 if no restoration is needed.
%macro RESTORE_CR0 0
cmp ecx, 0
je %%skip_cr0_restore
mov cr0, xCX
%%skip_cr0_restore:
%endmacro
;;
; Saves the host FPU/XMM state and restores the guest state.
;
; @returns 0
; @param pCPUMCPU x86:[esp+4] GCC:rdi MSC:rcx CPUMCPU pointer
;
align 16
BEGINPROC cpumR0SaveHostRestoreGuestFPUState
%ifdef RT_ARCH_AMD64
%ifdef RT_OS_WINDOWS
mov xDX, rcx
%else
mov xDX, rdi
%endif
%else
mov xDX, dword [esp + 4]
%endif
pushf ; The darwin kernel can get upset or upset things if an
cli ; interrupt occurs while we're doing fxsave/fxrstor/cr0.
; Switch the state.
or dword [xDX + CPUMCPU.fUseFlags], (CPUM_USED_FPU | CPUM_USED_FPU_SINCE_REM)
; Clear CR0 FPU bits to not cause exceptions, uses xCX
SAVE_CR0_CLEAR_FPU_TRAPS
; Do NOT use xCX from this point!
%ifdef VBOX_WITH_HYBRID_32BIT_KERNEL
cmp byte [NAME(g_fCPUMIs64bitHost)], 0
jz .legacy_mode
db 0xea ; jmp far .sixtyfourbit_mode
dd .sixtyfourbit_mode, NAME(SUPR0Abs64bitKernelCS)
.legacy_mode:
%endif ; VBOX_WITH_HYBRID_32BIT_KERNEL
%ifdef RT_ARCH_AMD64
; Use explicit REX prefix. See @bugref{6398}.
o64 fxsave [xDX + CPUMCPU.Host.fpu] ; ASSUMES that all VT-x/AMD-V boxes sports fxsave/fxrstor (safe assumption)
o64 fxrstor [xDX + CPUMCPU.Guest.fpu]
%else
fxsave [xDX + CPUMCPU.Host.fpu] ; ASSUMES that all VT-x/AMD-V boxes sports fxsave/fxrstor (safe assumption)
fxrstor [xDX + CPUMCPU.Guest.fpu]
%endif
%ifdef VBOX_WITH_KERNEL_USING_XMM
; Restore the non-volatile xmm registers. ASSUMING 64-bit windows
lea r11, [xDX + CPUMCPU.Host.fpu + XMM_OFF_IN_X86FXSTATE]
movdqa xmm6, [r11 + 060h]
movdqa xmm7, [r11 + 070h]
movdqa xmm8, [r11 + 080h]
movdqa xmm9, [r11 + 090h]
movdqa xmm10, [r11 + 0a0h]
movdqa xmm11, [r11 + 0b0h]
movdqa xmm12, [r11 + 0c0h]
movdqa xmm13, [r11 + 0d0h]
movdqa xmm14, [r11 + 0e0h]
movdqa xmm15, [r11 + 0f0h]
%endif
.done:
; Restore CR0 from xCX if it was previously saved.
RESTORE_CR0
popf
xor eax, eax
ret
%ifdef VBOX_WITH_HYBRID_32BIT_KERNEL_IN_R0
ALIGNCODE(16)
BITS 64
.sixtyfourbit_mode:
and edx, 0ffffffffh
o64 fxsave [rdx + CPUMCPU.Host.fpu]
o64 fxrstor [rdx + CPUMCPU.Guest.fpu]
jmp far [.fpret wrt rip]
.fpret: ; 16:32 Pointer to .the_end.
dd .done, NAME(SUPR0AbsKernelCS)
BITS 32
%endif
ENDPROC cpumR0SaveHostRestoreGuestFPUState
%ifndef RT_ARCH_AMD64
%ifdef VBOX_WITH_64_BITS_GUESTS
%ifndef VBOX_WITH_HYBRID_32BIT_KERNEL
;;
; Saves the host FPU/XMM state
;
; @returns 0
; @param pCPUMCPU x86:[esp+4] GCC:rdi MSC:rcx CPUMCPU pointer
;
align 16
BEGINPROC cpumR0SaveHostFPUState
mov xDX, dword [esp + 4]
pushf ; The darwin kernel can get upset or upset things if an
cli ; interrupt occurs while we're doing fxsave/fxrstor/cr0.
; Switch the state.
or dword [xDX + CPUMCPU.fUseFlags], (CPUM_USED_FPU | CPUM_USED_FPU_SINCE_REM)
; Clear CR0 FPU bits to not cause exceptions, uses xCX
SAVE_CR0_CLEAR_FPU_TRAPS
; Do NOT use xCX from this point!
fxsave [xDX + CPUMCPU.Host.fpu] ; ASSUMES that all VT-x/AMD-V boxes support fxsave/fxrstor (safe assumption)
; Restore CR0 from xCX if it was saved previously.
RESTORE_CR0
popf
xor eax, eax
ret
ENDPROC cpumR0SaveHostFPUState
%endif
%endif
%endif
;;
; Saves the guest FPU/XMM state and restores the host state.
;
; @returns 0
; @param pCPUMCPU x86:[esp+4] GCC:rdi MSC:rcx CPUMCPU pointer
;
align 16
BEGINPROC cpumR0SaveGuestRestoreHostFPUState
%ifdef RT_ARCH_AMD64
%ifdef RT_OS_WINDOWS
mov xDX, rcx
%else
mov xDX, rdi
%endif
%else
mov xDX, dword [esp + 4]
%endif
; Only restore FPU if guest has used it.
; Using fxrstor should ensure that we're not causing unwanted exception on the host.
test dword [xDX + CPUMCPU.fUseFlags], CPUM_USED_FPU
jz short .fpu_not_used
pushf ; The darwin kernel can get upset or upset things if an
cli ; interrupt occurs while we're doing fxsave/fxrstor/cr0.
; Clear CR0 FPU bits to not cause exceptions, uses xCX
SAVE_CR0_CLEAR_FPU_TRAPS
; Do NOT use xCX from this point!
%ifdef VBOX_WITH_HYBRID_32BIT_KERNEL
cmp byte [NAME(g_fCPUMIs64bitHost)], 0
jz .legacy_mode
db 0xea ; jmp far .sixtyfourbit_mode
dd .sixtyfourbit_mode, NAME(SUPR0Abs64bitKernelCS)
.legacy_mode:
%endif ; VBOX_WITH_HYBRID_32BIT_KERNEL
%ifdef RT_ARCH_AMD64
; Use explicit REX prefix. See @bugref{6398}.
o64 fxsave [xDX + CPUMCPU.Guest.fpu] ; ASSUMES that all VT-x/AMD-V boxes support fxsave/fxrstor (safe assumption)
o64 fxrstor [xDX + CPUMCPU.Host.fpu]
%else
fxsave [xDX + CPUMCPU.Guest.fpu] ; ASSUMES that all VT-x/AMD-V boxes support fxsave/fxrstor (safe assumption)
fxrstor [xDX + CPUMCPU.Host.fpu]
%endif
.done:
; Restore CR0 from xCX if it was previously saved.
RESTORE_CR0
and dword [xDX + CPUMCPU.fUseFlags], ~CPUM_USED_FPU
popf
.fpu_not_used:
xor eax, eax
ret
%ifdef VBOX_WITH_HYBRID_32BIT_KERNEL_IN_R0
ALIGNCODE(16)
BITS 64
.sixtyfourbit_mode:
and edx, 0ffffffffh
o64 fxsave [rdx + CPUMCPU.Guest.fpu]
o64 fxrstor [rdx + CPUMCPU.Host.fpu]
jmp far [.fpret wrt rip]
.fpret: ; 16:32 Pointer to .the_end.
dd .done, NAME(SUPR0AbsKernelCS)
BITS 32
%endif
ENDPROC cpumR0SaveGuestRestoreHostFPUState
;;
; Sets the host's FPU/XMM state
;
; @returns 0
; @param pCPUMCPU x86:[esp+4] GCC:rdi MSC:rcx CPUMCPU pointer
;
align 16
BEGINPROC cpumR0RestoreHostFPUState
%ifdef RT_ARCH_AMD64
%ifdef RT_OS_WINDOWS
mov xDX, rcx
%else
mov xDX, rdi
%endif
%else
mov xDX, dword [esp + 4]
%endif
; Restore FPU if guest has used it.
; Using fxrstor should ensure that we're not causing unwanted exception on the host.
test dword [xDX + CPUMCPU.fUseFlags], CPUM_USED_FPU
jz short .fpu_not_used
pushf ; The darwin kernel can get upset or upset things if an
cli ; interrupt occurs while we're doing fxsave/fxrstor/cr0.
; Clear CR0 FPU bits to not cause exceptions, uses xCX
SAVE_CR0_CLEAR_FPU_TRAPS
; Do NOT use xCX from this point!
%ifdef VBOX_WITH_HYBRID_32BIT_KERNEL
cmp byte [NAME(g_fCPUMIs64bitHost)], 0
jz .legacy_mode
db 0xea ; jmp far .sixtyfourbit_mode
dd .sixtyfourbit_mode, NAME(SUPR0Abs64bitKernelCS)
.legacy_mode:
%endif ; VBOX_WITH_HYBRID_32BIT_KERNEL
%ifdef RT_ARCH_AMD64
o64 fxrstor [xDX + CPUMCPU.Host.fpu]
%else
fxrstor [xDX + CPUMCPU.Host.fpu]
%endif
.done:
; Restore CR0 from xCX if it was previously saved.
RESTORE_CR0
and dword [xDX + CPUMCPU.fUseFlags], ~CPUM_USED_FPU
popf
.fpu_not_used:
xor eax, eax
ret
%ifdef VBOX_WITH_HYBRID_32BIT_KERNEL_IN_R0
ALIGNCODE(16)
BITS 64
.sixtyfourbit_mode:
and edx, 0ffffffffh
o64 fxrstor [rdx + CPUMCPU.Host.fpu]
jmp far [.fpret wrt rip]
.fpret: ; 16:32 Pointer to .the_end.
dd .done, NAME(SUPR0AbsKernelCS)
BITS 32
%endif
ENDPROC cpumR0RestoreHostFPUState
%ifdef VBOX_WITH_HYBRID_32BIT_KERNEL_IN_R0
;;
; DECLASM(void) cpumR0SaveDRx(uint64_t *pa4Regs);
;
ALIGNCODE(16)
BEGINPROC cpumR0SaveDRx
%ifdef RT_ARCH_AMD64
%ifdef ASM_CALL64_GCC
mov xCX, rdi
%endif
%else
mov xCX, dword [esp + 4]
%endif
pushf ; Just to be on the safe side.
cli
%ifdef VBOX_WITH_HYBRID_32BIT_KERNEL
cmp byte [NAME(g_fCPUMIs64bitHost)], 0
jz .legacy_mode
db 0xea ; jmp far .sixtyfourbit_mode
dd .sixtyfourbit_mode, NAME(SUPR0Abs64bitKernelCS)
.legacy_mode:
%endif ; VBOX_WITH_HYBRID_32BIT_KERNEL
;
; Do the job.
;
mov xAX, dr0
mov xDX, dr1
mov [xCX], xAX
mov [xCX + 8 * 1], xDX
mov xAX, dr2
mov xDX, dr3
mov [xCX + 8 * 2], xAX
mov [xCX + 8 * 3], xDX
.done:
popf
ret
%ifdef VBOX_WITH_HYBRID_32BIT_KERNEL_IN_R0
ALIGNCODE(16)
BITS 64
.sixtyfourbit_mode:
and ecx, 0ffffffffh
mov rax, dr0
mov rdx, dr1
mov r8, dr2
mov r9, dr3
mov [rcx], rax
mov [rcx + 8 * 1], rdx
mov [rcx + 8 * 2], r8
mov [rcx + 8 * 3], r9
jmp far [.fpret wrt rip]
.fpret: ; 16:32 Pointer to .the_end.
dd .done, NAME(SUPR0AbsKernelCS)
BITS 32
%endif
ENDPROC cpumR0SaveDRx
;;
; DECLASM(void) cpumR0LoadDRx(uint64_t const *pa4Regs);
;
ALIGNCODE(16)
BEGINPROC cpumR0LoadDRx
%ifdef RT_ARCH_AMD64
%ifdef ASM_CALL64_GCC
mov xCX, rdi
%endif
%else
mov xCX, dword [esp + 4]
%endif
pushf ; Just to be on the safe side.
cli
%ifdef VBOX_WITH_HYBRID_32BIT_KERNEL
cmp byte [NAME(g_fCPUMIs64bitHost)], 0
jz .legacy_mode
db 0xea ; jmp far .sixtyfourbit_mode
dd .sixtyfourbit_mode, NAME(SUPR0Abs64bitKernelCS)
.legacy_mode:
%endif ; VBOX_WITH_HYBRID_32BIT_KERNEL
;
; Do the job.
;
mov xAX, [xCX]
mov xDX, [xCX + 8 * 1]
mov dr0, xAX
mov dr1, xDX
mov xAX, [xCX + 8 * 2]
mov xDX, [xCX + 8 * 3]
mov dr2, xAX
mov dr3, xDX
.done:
popf
ret
%ifdef VBOX_WITH_HYBRID_32BIT_KERNEL_IN_R0
ALIGNCODE(16)
BITS 64
.sixtyfourbit_mode:
and ecx, 0ffffffffh
mov rax, [rcx]
mov rdx, [rcx + 8 * 1]
mov r8, [rcx + 8 * 2]
mov r9, [rcx + 8 * 3]
mov dr0, rax
mov dr1, rdx
mov dr2, r8
mov dr3, r9
jmp far [.fpret wrt rip]
.fpret: ; 16:32 Pointer to .the_end.
dd .done, NAME(SUPR0AbsKernelCS)
BITS 32
%endif
ENDPROC cpumR0LoadDRx
%endif ; VBOX_WITH_HYBRID_32BIT_KERNEL_IN_R0
|