VirtualBox

source: vbox/trunk/src/VBox/VMM/include/CPUMInternal.mac@ 61775

Last change on this file since 61775 was 61348, checked in by vboxsync, 9 years ago

CPUM,VMM: Touch the FPU state before doing HM on all platforms which allows us do (VMM_R0_TOUCH_FPU, see Makefile.kmk). No special treatment of win.amd64 (could save a CR0 read, maybe). Cleaned up the fix from this morning.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 27.9 KB
Line 
1; $Id: CPUMInternal.mac 61348 2016-05-31 17:59:34Z vboxsync $
2;; @file
3; CPUM - Internal header file (asm).
4;
5
6;
7; Copyright (C) 2006-2015 Oracle Corporation
8;
9; This file is part of VirtualBox Open Source Edition (OSE), as
10; available from http://www.virtualbox.org. This file is free software;
11; you can redistribute it and/or modify it under the terms of the GNU
12; General Public License (GPL) as published by the Free Software
13; Foundation, in version 2 as it comes in the "COPYING" file of the
14; VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15; hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16;
17
18%include "VBox/asmdefs.mac"
19%include "VBox/vmm/cpum.mac"
20
21;; Check sanity.
22%ifdef VBOX_WITH_KERNEL_USING_XMM
23 %ifndef IN_RING0
24 %error "What? We've got code assuming VBOX_WITH_KERNEL_USING_XMM is only defined in ring-0!"
25 %endif
26%endif
27
28;; For numeric expressions
29%ifdef RT_ARCH_AMD64
30 %define CPUM_IS_AMD64 1
31%else
32 %define CPUM_IS_AMD64 0
33%endif
34
35
36;;
37; CPU info
38struc CPUMINFO
39 .cMsrRanges resd 1 ; uint32_t
40 .fMsrMask resd 1 ; uint32_t
41 .cCpuIdLeaves resd 1 ; uint32_t
42 .iFirstExtCpuIdLeaf resd 1 ; uint32_t
43 .uPadding resd 1 ; uint32_t
44 .enmUnknownCpuIdMethod resd 1 ; CPUMUNKNOWNCPUID
45 .DefCpuId resb CPUMCPUID_size ; CPUMCPUID
46 .uScalableBusFreq resq 1 ; uint64_t
47 .paMsrRangesR0 RTR0PTR_RES 1 ; R0PTRTYPE(PCPUMMSRRANGE)
48 .paCpuIdLeavesR0 RTR0PTR_RES 1 ; R0PTRTYPE(PCPUMCPUIDLEAF)
49 .paMsrRangesR3 RTR3PTR_RES 1 ; R3PTRTYPE(PCPUMMSRRANGE)
50 .paCpuIdLeavesR3 RTR3PTR_RES 1 ; R3PTRTYPE(PCPUMCPUIDLEAF)
51 .paMsrRangesRC RTRCPTR_RES 1 ; RCPTRTYPE(PCPUMMSRRANGE)
52 .paCpuIdLeavesRC RTRCPTR_RES 1 ; RCPTRTYPE(PCPUMCPUIDLEAF)
53endstruc
54
55
56%define CPUM_USED_FPU_HOST RT_BIT(0)
57%define CPUM_USED_FPU_GUEST RT_BIT(10)
58%define CPUM_USED_FPU_SINCE_REM RT_BIT(1)
59%define CPUM_USED_MANUAL_XMM_RESTORE RT_BIT(2)
60%define CPUM_USE_SYSENTER RT_BIT(3)
61%define CPUM_USE_SYSCALL RT_BIT(4)
62%define CPUM_USE_DEBUG_REGS_HOST RT_BIT(5)
63%define CPUM_USED_DEBUG_REGS_HOST RT_BIT(6)
64%define CPUM_USE_DEBUG_REGS_HYPER RT_BIT(7)
65%define CPUM_USED_DEBUG_REGS_HYPER RT_BIT(8)
66%define CPUM_USED_DEBUG_REGS_GUEST RT_BIT(9)
67%define CPUM_SYNC_FPU_STATE RT_BIT(16)
68%define CPUM_SYNC_DEBUG_REGS_GUEST RT_BIT(17)
69%define CPUM_SYNC_DEBUG_REGS_HYPER RT_BIT(18)
70%define CPUM_USE_FFXSR_LEAKY RT_BIT(19)
71%define CPUM_USE_SUPPORTS_LONGMODE RT_BIT(20)
72
73%define CPUM_HANDLER_DS 1
74%define CPUM_HANDLER_ES 2
75%define CPUM_HANDLER_FS 3
76%define CPUM_HANDLER_GS 4
77%define CPUM_HANDLER_IRET 5
78%define CPUM_HANDLER_TYPEMASK 0ffh
79%define CPUM_HANDLER_CTXCORE_IN_EBP RT_BIT(31)
80
81
82struc CPUM
83 ;...
84 .offCPUMCPU0 resd 1
85 .fHostUseFlags resd 1
86
87 ; CR4 masks
88 .CR4.AndMask resd 1
89 .CR4.OrMask resd 1
90 ; entered rawmode?
91 .u8PortableCpuIdLevel resb 1
92 .fPendingRestore resb 1
93
94 alignb 8
95 .fXStateGuestMask resq 1
96 .fXStateHostMask resq 1
97
98 alignb 64
99 .HostFeatures resb 32
100 .GuestFeatures resb 32
101 .GuestInfo resb RTHCPTR_CB*4 + RTRCPTR_CB*2 + 4*12
102
103 ; Patch manager saved state compatability CPUID leaf arrays
104 .aGuestCpuIdPatmStd resb 16*6
105 .aGuestCpuIdPatmExt resb 16*10
106 .aGuestCpuIdPatmCentaur resb 16*4
107
108 alignb 8
109 .cMsrWrites resq 1
110 .cMsrWritesToIgnoredBits resq 1
111 .cMsrWritesRaiseGp resq 1
112 .cMsrWritesUnknown resq 1
113 .cMsrReads resq 1
114 .cMsrReadsRaiseGp resq 1
115 .cMsrReadsUnknown resq 1
116endstruc
117
118struc CPUMCPU
119 ;
120 ; Guest context state
121 ; (Identical to the .Hyper chunk below.)
122 ;
123 .Guest resq 0
124 .Guest.eax resq 1
125 .Guest.ecx resq 1
126 .Guest.edx resq 1
127 .Guest.ebx resq 1
128 .Guest.esp resq 1
129 .Guest.ebp resq 1
130 .Guest.esi resq 1
131 .Guest.edi resq 1
132 .Guest.r8 resq 1
133 .Guest.r9 resq 1
134 .Guest.r10 resq 1
135 .Guest.r11 resq 1
136 .Guest.r12 resq 1
137 .Guest.r13 resq 1
138 .Guest.r14 resq 1
139 .Guest.r15 resq 1
140 .Guest.es.Sel resw 1
141 .Guest.es.PaddingSel resw 1
142 .Guest.es.ValidSel resw 1
143 .Guest.es.fFlags resw 1
144 .Guest.es.u64Base resq 1
145 .Guest.es.u32Limit resd 1
146 .Guest.es.Attr resd 1
147 .Guest.cs.Sel resw 1
148 .Guest.cs.PaddingSel resw 1
149 .Guest.cs.ValidSel resw 1
150 .Guest.cs.fFlags resw 1
151 .Guest.cs.u64Base resq 1
152 .Guest.cs.u32Limit resd 1
153 .Guest.cs.Attr resd 1
154 .Guest.ss.Sel resw 1
155 .Guest.ss.PaddingSel resw 1
156 .Guest.ss.ValidSel resw 1
157 .Guest.ss.fFlags resw 1
158 .Guest.ss.u64Base resq 1
159 .Guest.ss.u32Limit resd 1
160 .Guest.ss.Attr resd 1
161 .Guest.ds.Sel resw 1
162 .Guest.ds.PaddingSel resw 1
163 .Guest.ds.ValidSel resw 1
164 .Guest.ds.fFlags resw 1
165 .Guest.ds.u64Base resq 1
166 .Guest.ds.u32Limit resd 1
167 .Guest.ds.Attr resd 1
168 .Guest.fs.Sel resw 1
169 .Guest.fs.PaddingSel resw 1
170 .Guest.fs.ValidSel resw 1
171 .Guest.fs.fFlags resw 1
172 .Guest.fs.u64Base resq 1
173 .Guest.fs.u32Limit resd 1
174 .Guest.fs.Attr resd 1
175 .Guest.gs.Sel resw 1
176 .Guest.gs.PaddingSel resw 1
177 .Guest.gs.ValidSel resw 1
178 .Guest.gs.fFlags resw 1
179 .Guest.gs.u64Base resq 1
180 .Guest.gs.u32Limit resd 1
181 .Guest.gs.Attr resd 1
182 .Guest.eip resq 1
183 .Guest.eflags resq 1
184 .Guest.cr0 resq 1
185 .Guest.cr2 resq 1
186 .Guest.cr3 resq 1
187 .Guest.cr4 resq 1
188 .Guest.dr resq 8
189 .Guest.gdtrPadding resw 3
190 .Guest.gdtr resw 0
191 .Guest.gdtr.cbGdt resw 1
192 .Guest.gdtr.pGdt resq 1
193 .Guest.idtrPadding resw 3
194 .Guest.idtr resw 0
195 .Guest.idtr.cbIdt resw 1
196 .Guest.idtr.pIdt resq 1
197 .Guest.ldtr.Sel resw 1
198 .Guest.ldtr.PaddingSel resw 1
199 .Guest.ldtr.ValidSel resw 1
200 .Guest.ldtr.fFlags resw 1
201 .Guest.ldtr.u64Base resq 1
202 .Guest.ldtr.u32Limit resd 1
203 .Guest.ldtr.Attr resd 1
204 .Guest.tr.Sel resw 1
205 .Guest.tr.PaddingSel resw 1
206 .Guest.tr.ValidSel resw 1
207 .Guest.tr.fFlags resw 1
208 .Guest.tr.u64Base resq 1
209 .Guest.tr.u32Limit resd 1
210 .Guest.tr.Attr resd 1
211 .Guest.SysEnter.cs resb 8
212 .Guest.SysEnter.eip resb 8
213 .Guest.SysEnter.esp resb 8
214 .Guest.msrEFER resb 8
215 .Guest.msrSTAR resb 8
216 .Guest.msrPAT resb 8
217 .Guest.msrLSTAR resb 8
218 .Guest.msrCSTAR resb 8
219 .Guest.msrSFMASK resb 8
220 .Guest.msrKERNELGSBASE resb 8
221 .Guest.msrApicBase resb 8
222 .Guest.aXcr resq 2
223 .Guest.fXStateMask resq 1
224 .Guest.pXStateR0 RTR0PTR_RES 1
225 .Guest.pXStateR3 RTR3PTR_RES 1
226 .Guest.pXStateRC RTRCPTR_RES 1
227 .Guest.aoffXState resw 64
228
229 alignb 64
230 .GuestMsrs resq 0
231 .GuestMsrs.au64 resq 64
232
233 ;
234 ; Other stuff.
235 ;
236 .fUseFlags resd 1
237 .fChanged resd 1
238 .offCPUM resd 1
239 .u32RetCode resd 1
240
241%ifdef VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
242 .pvApicBase RTR0PTR_RES 1
243 .fApicDisVectors resd 1
244 .fX2Apic resb 1
245%else
246 .abPadding3 resb (RTR0PTR_CB + 4 + 1)
247%endif
248
249 .fRawEntered resb 1
250 .fRemEntered resb 1
251
252 .abPadding2 resb (64 - 16 - RTR0PTR_CB - 4 - 1 - 2)
253
254 ;
255 ; Host context state
256 ;
257 alignb 64
258 .Host resb 0
259%if HC_ARCH_BITS == 64
260 ;.Host.rax resq 1 - scratch
261 .Host.rbx resq 1
262 ;.Host.rcx resq 1 - scratch
263 ;.Host.rdx resq 1 - scratch
264 .Host.rdi resq 1
265 .Host.rsi resq 1
266 .Host.rbp resq 1
267 .Host.rsp resq 1
268 ;.Host.r8 resq 1 - scratch
269 ;.Host.r9 resq 1 - scratch
270 .Host.r10 resq 1
271 .Host.r11 resq 1
272 .Host.r12 resq 1
273 .Host.r13 resq 1
274 .Host.r14 resq 1
275 .Host.r15 resq 1
276 ;.Host.rip resd 1 - scratch
277 .Host.rflags resq 1
278%endif
279%if HC_ARCH_BITS == 32
280 ;.Host.eax resd 1 - scratch
281 .Host.ebx resd 1
282 ;.Host.edx resd 1 - scratch
283 ;.Host.ecx resd 1 - scratch
284 .Host.edi resd 1
285 .Host.esi resd 1
286 .Host.ebp resd 1
287 .Host.eflags resd 1
288 ;.Host.eip resd 1 - scratch
289 ; lss pair!
290 .Host.esp resd 1
291%endif
292 .Host.ss resw 1
293 .Host.ssPadding resw 1
294 .Host.gs resw 1
295 .Host.gsPadding resw 1
296 .Host.fs resw 1
297 .Host.fsPadding resw 1
298 .Host.es resw 1
299 .Host.esPadding resw 1
300 .Host.ds resw 1
301 .Host.dsPadding resw 1
302 .Host.cs resw 1
303 .Host.csPadding resw 1
304
305%if HC_ARCH_BITS == 32
306 .Host.cr0 resd 1
307 ;.Host.cr2 resd 1 - scratch
308 .Host.cr3 resd 1
309 .Host.cr4 resd 1
310 .Host.cr0Fpu resd 1
311
312 .Host.dr0 resd 1
313 .Host.dr1 resd 1
314 .Host.dr2 resd 1
315 .Host.dr3 resd 1
316 .Host.dr6 resd 1
317 .Host.dr7 resd 1
318
319 .Host.gdtr resb 6 ; GDT limit + linear address
320 .Host.gdtrPadding resw 1
321 .Host.idtr resb 6 ; IDT limit + linear address
322 .Host.idtrPadding resw 1
323 .Host.ldtr resw 1
324 .Host.ldtrPadding resw 1
325 .Host.tr resw 1
326 .Host.trPadding resw 1
327
328 alignb 8
329 .Host.SysEnter.cs resq 1
330 .Host.SysEnter.eip resq 1
331 .Host.SysEnter.esp resq 1
332 .Host.efer resq 1
333 .Host.auPadding resb (20)
334
335%else ; 64-bit
336
337 .Host.cr0Fpu:
338 .Host.cr0 resq 1
339 ;.Host.cr2 resq 1 - scratch
340 .Host.cr3 resq 1
341 .Host.cr4 resq 1
342 .Host.cr8 resq 1
343
344 .Host.dr0 resq 1
345 .Host.dr1 resq 1
346 .Host.dr2 resq 1
347 .Host.dr3 resq 1
348 .Host.dr6 resq 1
349 .Host.dr7 resq 1
350
351 .Host.gdtr resb 10 ; GDT limit + linear address
352 .Host.gdtrPadding resw 1
353 .Host.idtr resb 10 ; IDT limit + linear address
354 .Host.idtrPadding resw 1
355 .Host.ldtr resw 1
356 .Host.ldtrPadding resw 1
357 .Host.tr resw 1
358 .Host.trPadding resw 1
359
360 .Host.SysEnter.cs resq 1
361 .Host.SysEnter.eip resq 1
362 .Host.SysEnter.esp resq 1
363 .Host.FSbase resq 1
364 .Host.GSbase resq 1
365 .Host.efer resq 1
366 .Host.auPadding resb 4
367%endif ; 64-bit
368 .Host.pXStateRC RTRCPTR_RES 1
369 alignb RTR0PTR_CB
370 .Host.pXStateR0 RTR0PTR_RES 1
371 .Host.pXStateR3 RTR3PTR_RES 1
372 alignb 8
373 .Host.xcr0 resq 1
374 .Host.fXStateMask resq 1
375
376 ;
377 ; Hypervisor Context (same as .Guest above).
378 ;
379 alignb 64
380 .Hyper resq 0
381 .Hyper.eax resq 1
382 .Hyper.ecx resq 1
383 .Hyper.edx resq 1
384 .Hyper.ebx resq 1
385 .Hyper.esp resq 1
386 .Hyper.ebp resq 1
387 .Hyper.esi resq 1
388 .Hyper.edi resq 1
389 .Hyper.r8 resq 1
390 .Hyper.r9 resq 1
391 .Hyper.r10 resq 1
392 .Hyper.r11 resq 1
393 .Hyper.r12 resq 1
394 .Hyper.r13 resq 1
395 .Hyper.r14 resq 1
396 .Hyper.r15 resq 1
397 .Hyper.es.Sel resw 1
398 .Hyper.es.PaddingSel resw 1
399 .Hyper.es.ValidSel resw 1
400 .Hyper.es.fFlags resw 1
401 .Hyper.es.u64Base resq 1
402 .Hyper.es.u32Limit resd 1
403 .Hyper.es.Attr resd 1
404 .Hyper.cs.Sel resw 1
405 .Hyper.cs.PaddingSel resw 1
406 .Hyper.cs.ValidSel resw 1
407 .Hyper.cs.fFlags resw 1
408 .Hyper.cs.u64Base resq 1
409 .Hyper.cs.u32Limit resd 1
410 .Hyper.cs.Attr resd 1
411 .Hyper.ss.Sel resw 1
412 .Hyper.ss.PaddingSel resw 1
413 .Hyper.ss.ValidSel resw 1
414 .Hyper.ss.fFlags resw 1
415 .Hyper.ss.u64Base resq 1
416 .Hyper.ss.u32Limit resd 1
417 .Hyper.ss.Attr resd 1
418 .Hyper.ds.Sel resw 1
419 .Hyper.ds.PaddingSel resw 1
420 .Hyper.ds.ValidSel resw 1
421 .Hyper.ds.fFlags resw 1
422 .Hyper.ds.u64Base resq 1
423 .Hyper.ds.u32Limit resd 1
424 .Hyper.ds.Attr resd 1
425 .Hyper.fs.Sel resw 1
426 .Hyper.fs.PaddingSel resw 1
427 .Hyper.fs.ValidSel resw 1
428 .Hyper.fs.fFlags resw 1
429 .Hyper.fs.u64Base resq 1
430 .Hyper.fs.u32Limit resd 1
431 .Hyper.fs.Attr resd 1
432 .Hyper.gs.Sel resw 1
433 .Hyper.gs.PaddingSel resw 1
434 .Hyper.gs.ValidSel resw 1
435 .Hyper.gs.fFlags resw 1
436 .Hyper.gs.u64Base resq 1
437 .Hyper.gs.u32Limit resd 1
438 .Hyper.gs.Attr resd 1
439 .Hyper.eip resq 1
440 .Hyper.eflags resq 1
441 .Hyper.cr0 resq 1
442 .Hyper.cr2 resq 1
443 .Hyper.cr3 resq 1
444 .Hyper.cr4 resq 1
445 .Hyper.dr resq 8
446 .Hyper.gdtrPadding resw 3
447 .Hyper.gdtr resw 0
448 .Hyper.gdtr.cbGdt resw 1
449 .Hyper.gdtr.pGdt resq 1
450 .Hyper.idtrPadding resw 3
451 .Hyper.idtr resw 0
452 .Hyper.idtr.cbIdt resw 1
453 .Hyper.idtr.pIdt resq 1
454 .Hyper.ldtr.Sel resw 1
455 .Hyper.ldtr.PaddingSel resw 1
456 .Hyper.ldtr.ValidSel resw 1
457 .Hyper.ldtr.fFlags resw 1
458 .Hyper.ldtr.u64Base resq 1
459 .Hyper.ldtr.u32Limit resd 1
460 .Hyper.ldtr.Attr resd 1
461 .Hyper.tr.Sel resw 1
462 .Hyper.tr.PaddingSel resw 1
463 .Hyper.tr.ValidSel resw 1
464 .Hyper.tr.fFlags resw 1
465 .Hyper.tr.u64Base resq 1
466 .Hyper.tr.u32Limit resd 1
467 .Hyper.tr.Attr resd 1
468 .Hyper.SysEnter.cs resb 8
469 .Hyper.SysEnter.eip resb 8
470 .Hyper.SysEnter.esp resb 8
471 .Hyper.msrEFER resb 8
472 .Hyper.msrSTAR resb 8
473 .Hyper.msrPAT resb 8
474 .Hyper.msrLSTAR resb 8
475 .Hyper.msrCSTAR resb 8
476 .Hyper.msrSFMASK resb 8
477 .Hyper.msrKERNELGSBASE resb 8
478 .Hyper.msrApicBase resb 8
479 .Hyper.aXcr resq 2
480 .Hyper.fXStateMask resq 1
481 .Hyper.pXStateR0 RTR0PTR_RES 1
482 .Hyper.pXStateR3 RTR3PTR_RES 1
483 .Hyper.pXStateRC RTRCPTR_RES 1
484 .Hyper.aoffXState resw 64
485 alignb 64
486
487%ifdef VBOX_WITH_CRASHDUMP_MAGIC
488 .aMagic resb 56
489 .uMagic resq 1
490%endif
491endstruc
492
493
494;;
495; Converts the CPUM pointer to CPUMCPU
496; @param %1 register name
497%macro CPUMCPU_FROM_CPUM 1
498 add %1, dword [%1 + CPUM.offCPUMCPU0]
499%endmacro
500
501;;
502; Converts the CPUM pointer to CPUMCPU
503; @param %1 register name (CPUM)
504; @param %2 register name (CPUMCPU offset)
505%macro CPUMCPU_FROM_CPUM_WITH_OFFSET 2
506 add %1, %2
507%endmacro
508
509;;
510; Converts the CPUMCPU pointer to CPUM
511; @param %1 register name
512%macro CPUM_FROM_CPUMCPU 1
513 sub %1, dword [%1 + CPUMCPU.offCPUM]
514%endmacro
515
516;;
517; Converts the CPUMCPU pointer to CPUM
518; @param %1 register name (CPUM)
519; @param %2 register name (CPUMCPU offset)
520%macro CPUM_FROM_CPUMCPU_WITH_OFFSET 2
521 sub %1, %2
522%endmacro
523
524
525
526%if 0 ; Currently not used anywhere.
527;;
528; Macro for FXSAVE/FXRSTOR leaky behaviour on AMD CPUs, see cpumR3CheckLeakyFpu().
529;
530; Cleans the FPU state, if necessary, before restoring the FPU.
531;
532; This macro ASSUMES CR0.TS is not set!
533;
534; @param xDX Pointer to CPUMCPU.
535; @uses xAX, EFLAGS
536;
537; Changes here should also be reflected in CPUMRCA.asm's copy!
538;
539%macro CLEANFPU 0
540 test dword [xDX + CPUMCPU.fUseFlags], CPUM_USE_FFXSR_LEAKY
541 jz .nothing_to_clean
542
543 xor eax, eax
544 fnstsw ax ; FSW -> AX.
545 test eax, RT_BIT(7) ; If FSW.ES (bit 7) is set, clear it to not cause FPU exceptions
546 ; while clearing & loading the FPU bits in 'clean_fpu' below.
547 jz .clean_fpu
548 fnclex
549
550.clean_fpu:
551 ffree st7 ; Clear FPU stack register(7)'s tag entry to prevent overflow if a wraparound occurs.
552 ; for the upcoming push (load)
553 fild dword [g_r32_Zero xWrtRIP] ; Explicit FPU load to overwrite FIP, FOP, FDP registers in the FPU.
554.nothing_to_clean:
555%endmacro
556%endif ; Unused.
557
558
559;;
560; Makes sure we don't trap (#NM) accessing the FPU.
561;
562; In ring-0 this is a bit of work since we may have try convince the host kernel
563; to do the work for us, also, we must report any CR0 changes back to HMR0VMX
564; via the VINF_CPUM_HOST_CR0_MODIFIED status code.
565;
566; If we end up clearing CR0.TS/EM ourselves in ring-0, we'll save the original
567; value in CPUMCPU.Host.cr0Fpu. If we don't, we'll store zero there. (See also
568; CPUMRZ_RESTORE_CR0_IF_TS_OR_EM_SET.)
569;
570; In raw-mode we will always have to clear TS and it will be recalculated
571; elsewhere and thus needs no saving.
572;
573; @param %1 Register to return the return status code in.
574; @param %2 Temporary scratch register.
575; @param %3 Ring-0 only, register pointing to the CPUMCPU structure
576; of the EMT we're on.
577; @uses EFLAGS, CR0, %1, %2
578;
579%macro CPUMRZ_TOUCH_FPU_CLEAR_CR0_FPU_TRAPS_SET_RC 3
580 %ifdef IN_RC
581 ;
582 ; raw-mode - always clear it. We won't be here otherwise.
583 ;
584 mov %2, cr0
585 and %2, ~(X86_CR0_TS | X86_CR0_EM)
586 mov cr0, %2
587
588 %else
589 ;
590 ; ring-0 - slightly complicated.
591 ;
592 xor %1, %1 ; 0 / VINF_SUCCESS. Wishing for no CR0 changes.
593 mov [%3 + CPUMCPU.Host.cr0Fpu], %1
594
595 mov %2, cr0
596 test %2, X86_CR0_TS | X86_CR0_EM ; Make sure its safe to access the FPU state.
597 jz %%no_cr0_change
598
599 %ifdef VMM_R0_TOUCH_FPU
600 ; Touch the state and check that the kernel updated CR0 for us.
601 movdqa xmm0, xmm0
602 mov %2, cr0
603 test %2, X86_CR0_TS | X86_CR0_EM
604 jz %%cr0_changed
605 %endif
606
607 ; Save CR0 and clear them flags ourselves.
608 mov [%3 + CPUMCPU.Host.cr0Fpu], %2
609 and %2, ~(X86_CR0_TS | X86_CR0_EM)
610 mov cr0, %2
611 %endif ; IN_RING0
612
613%%cr0_changed:
614 mov %1, VINF_CPUM_HOST_CR0_MODIFIED
615%%no_cr0_change:
616%endmacro
617
618
619;;
620; Restore CR0 if CR0.TS or CR0.EM were non-zero in the original state.
621;
622; @param %1 The original state to restore (or zero).
623;
624%macro CPUMRZ_RESTORE_CR0_IF_TS_OR_EM_SET 1
625 test %1, X86_CR0_TS | X86_CR0_EM
626 jz %%skip_cr0_restore
627 mov cr0, %1
628%%skip_cr0_restore:
629%endmacro
630
631
632;;
633; Saves the host state.
634;
635; @uses rax, rdx
636; @param pCpumCpu Define for the register containing the CPUMCPU pointer.
637; @param pXState Define for the register containing the extended state pointer.
638;
639%macro CPUMR0_SAVE_HOST 0
640 ;
641 ; Load a couple of registers we'll use later in all branches.
642 ;
643 %ifdef IN_RING0
644 mov pXState, [pCpumCpu + CPUMCPU.Host.pXStateR0]
645 %elifdef IN_RC
646 mov pXState, [pCpumCpu + CPUMCPU.Host.pXStateRC]
647 %else
648 %error "Unsupported context!"
649 %endif
650 mov eax, [pCpumCpu + CPUMCPU.Host.fXStateMask]
651
652 ;
653 ; XSAVE or FXSAVE?
654 ;
655 or eax, eax
656 jz %%host_fxsave
657
658 ; XSAVE
659 mov edx, [pCpumCpu + CPUMCPU.Host.fXStateMask + 4]
660 %ifdef RT_ARCH_AMD64
661 o64 xsave [pXState]
662 %else
663 xsave [pXState]
664 %endif
665 jmp %%host_done
666
667 ; FXSAVE
668%%host_fxsave:
669 %ifdef RT_ARCH_AMD64
670 o64 fxsave [pXState] ; Use explicit REX prefix. See @bugref{6398}.
671 %else
672 fxsave [pXState]
673 %endif
674
675%%host_done:
676%endmacro ; CPUMR0_SAVE_HOST
677
678
679;;
680; Loads the host state.
681;
682; @uses rax, rdx
683; @param pCpumCpu Define for the register containing the CPUMCPU pointer.
684; @param pXState Define for the register containing the extended state pointer.
685;
686%macro CPUMR0_LOAD_HOST 0
687 ;
688 ; Load a couple of registers we'll use later in all branches.
689 ;
690 %ifdef IN_RING0
691 mov pXState, [pCpumCpu + CPUMCPU.Host.pXStateR0]
692 %elifdef IN_RC
693 mov pXState, [pCpumCpu + CPUMCPU.Host.pXStateRC]
694 %else
695 %error "Unsupported context!"
696 %endif
697 mov eax, [pCpumCpu + CPUMCPU.Host.fXStateMask]
698
699 ;
700 ; XRSTOR or FXRSTOR?
701 ;
702 or eax, eax
703 jz %%host_fxrstor
704
705 ; XRSTOR
706 mov edx, [pCpumCpu + CPUMCPU.Host.fXStateMask + 4]
707 %ifdef RT_ARCH_AMD64
708 o64 xrstor [pXState]
709 %else
710 xrstor [pXState]
711 %endif
712 jmp %%host_done
713
714 ; FXRSTOR
715%%host_fxrstor:
716 %ifdef RT_ARCH_AMD64
717 o64 fxrstor [pXState] ; Use explicit REX prefix. See @bugref{6398}.
718 %else
719 fxrstor [pXState]
720 %endif
721
722%%host_done:
723%endmacro ; CPUMR0_LOAD_HOST
724
725
726
727;; Macro for XSAVE/FXSAVE for the guest FPU but tries to figure out whether to
728; save the 32-bit FPU state or 64-bit FPU state.
729;
730; @param %1 Pointer to CPUMCPU.
731; @param %2 Pointer to XState.
732; @param %3 Force AMD64
733; @param %4 The instruction to use (xsave or fxsave)
734; @uses xAX, xDX, EFLAGS, 20h of stack.
735;
736%macro SAVE_32_OR_64_FPU 4
737%if CPUM_IS_AMD64 || %3
738 ; Save the guest FPU (32-bit or 64-bit), preserves existing broken state. See @bugref{7138}.
739 test dword [pCpumCpu + CPUMCPU.fUseFlags], CPUM_USE_SUPPORTS_LONGMODE
740 jnz short %%save_long_mode_guest
741%endif
742 %4 [pXState]
743%if CPUM_IS_AMD64 || %3
744 jmp %%save_done_32bit_cs_ds
745
746%%save_long_mode_guest:
747 o64 %4 [pXState]
748
749 xor edx, edx
750 cmp dword [pXState + X86FXSTATE.FPUCS], 0
751 jne short %%save_done
752
753 sub rsp, 20h ; Only need 1ch bytes but keep stack aligned otherwise we #GP(0).
754 fnstenv [rsp]
755 movzx eax, word [rsp + 10h]
756 mov [pXState + X86FXSTATE.FPUCS], eax
757 movzx eax, word [rsp + 18h]
758 add rsp, 20h
759 mov [pXState + X86FXSTATE.FPUDS], eax
760%endif
761%%save_done_32bit_cs_ds:
762 mov edx, X86_FXSTATE_RSVD_32BIT_MAGIC
763%%save_done:
764 mov dword [pXState + X86_OFF_FXSTATE_RSVD], edx
765%endmacro ; SAVE_32_OR_64_FPU
766
767
768;;
769; Save the guest state.
770;
771; @uses rax, rdx
772; @param pCpumCpu Define for the register containing the CPUMCPU pointer.
773; @param pXState Define for the register containing the extended state pointer.
774;
775%macro CPUMR0_SAVE_GUEST 0
776 ;
777 ; Load a couple of registers we'll use later in all branches.
778 ;
779 %ifdef IN_RING0
780 mov pXState, [pCpumCpu + CPUMCPU.Guest.pXStateR0]
781 %elifdef IN_RC
782 mov pXState, [pCpumCpu + CPUMCPU.Guest.pXStateRC]
783 %else
784 %error "Unsupported context!"
785 %endif
786 mov eax, [pCpumCpu + CPUMCPU.Guest.fXStateMask]
787
788 ;
789 ; XSAVE or FXSAVE?
790 ;
791 or eax, eax
792 jz %%guest_fxsave
793
794 ; XSAVE
795 mov edx, [pCpumCpu + CPUMCPU.Guest.fXStateMask + 4]
796 %ifdef VBOX_WITH_KERNEL_USING_XMM
797 and eax, ~CPUM_VOLATILE_XSAVE_GUEST_COMPONENTS ; Already saved in HMR0A.asm.
798 %endif
799 SAVE_32_OR_64_FPU pCpumCpu, pXState, 0, xsave
800 jmp %%guest_done
801
802 ; FXSAVE
803%%guest_fxsave:
804 SAVE_32_OR_64_FPU pCpumCpu, pXState, 0, fxsave
805
806%%guest_done:
807%endmacro ; CPUMR0_SAVE_GUEST
808
809
810;;
811; Wrapper for selecting 32-bit or 64-bit XRSTOR/FXRSTOR according to what SAVE_32_OR_64_FPU did.
812;
813; @param %1 Pointer to CPUMCPU.
814; @param %2 Pointer to XState.
815; @param %3 Force AMD64.
816; @param %4 The instruction to use (xrstor or fxrstor).
817; @uses xAX, xDX, EFLAGS
818;
819%macro RESTORE_32_OR_64_FPU 4
820%if CPUM_IS_AMD64 || %3
821 ; Restore the guest FPU (32-bit or 64-bit), preserves existing broken state. See @bugref{7138}.
822 test dword [pCpumCpu + CPUMCPU.fUseFlags], CPUM_USE_SUPPORTS_LONGMODE
823 jz %%restore_32bit_fpu
824 cmp dword [pXState + X86_OFF_FXSTATE_RSVD], X86_FXSTATE_RSVD_32BIT_MAGIC
825 jne short %%restore_64bit_fpu
826%%restore_32bit_fpu:
827%endif
828 %4 [pXState]
829%if CPUM_IS_AMD64 || %3
830 ; TODO: Restore XMM8-XMM15!
831 jmp short %%restore_fpu_done
832%%restore_64bit_fpu:
833 o64 %4 [pXState]
834%%restore_fpu_done:
835%endif
836%endmacro ; RESTORE_32_OR_64_FPU
837
838
839;;
840; Loads the guest state.
841;
842; @uses rax, rdx
843; @param pCpumCpu Define for the register containing the CPUMCPU pointer.
844; @param pXState Define for the register containing the extended state pointer.
845;
846%macro CPUMR0_LOAD_GUEST 0
847 ;
848 ; Load a couple of registers we'll use later in all branches.
849 ;
850 %ifdef IN_RING0
851 mov pXState, [pCpumCpu + CPUMCPU.Guest.pXStateR0]
852 %elifdef IN_RC
853 mov pXState, [pCpumCpu + CPUMCPU.Guest.pXStateRC]
854 %else
855 %error "Unsupported context!"
856 %endif
857 mov eax, [pCpumCpu + CPUMCPU.Guest.fXStateMask]
858
859 ;
860 ; XRSTOR or FXRSTOR?
861 ;
862 or eax, eax
863 jz %%guest_fxrstor
864
865 ; XRSTOR
866 mov edx, [pCpumCpu + CPUMCPU.Guest.fXStateMask + 4]
867 %ifdef VBOX_WITH_KERNEL_USING_XMM
868 and eax, ~CPUM_VOLATILE_XSAVE_GUEST_COMPONENTS ; Will be loaded by HMR0A.asm.
869 %endif
870 RESTORE_32_OR_64_FPU pCpumCpu, pXState, 0, xrstor
871 jmp %%guest_done
872
873 ; FXRSTOR
874%%guest_fxrstor:
875 RESTORE_32_OR_64_FPU pCpumCpu, pXState, 0, fxrstor
876
877%%guest_done:
878%endmacro ; CPUMR0_LOAD_GUEST
879
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette