1 | ; $Id: CPUMInternal.mac 91306 2021-09-17 21:11:01Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; CPUM - Internal header file (asm).
|
---|
4 | ;
|
---|
5 |
|
---|
6 | ;
|
---|
7 | ; Copyright (C) 2006-2020 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
|
---|
38 | struc CPUMINFO
|
---|
39 | .cMsrRanges resd 1 ; uint32_t
|
---|
40 | .fMsrMask resd 1 ; uint32_t
|
---|
41 | .fMxCsrMask resd 1 ; uint32_t
|
---|
42 | .cCpuIdLeaves resd 1 ; uint32_t
|
---|
43 | .iFirstExtCpuIdLeaf resd 1 ; uint32_t
|
---|
44 | .enmUnknownCpuIdMethod resd 1 ; CPUMUNKNOWNCPUID
|
---|
45 | .DefCpuId resb CPUMCPUID_size ; CPUMCPUID
|
---|
46 | .uScalableBusFreq resq 1 ; uint64_t
|
---|
47 | .paMsrRangesR3 RTR3PTR_RES 1 ; R3PTRTYPE(PCPUMMSRRANGE)
|
---|
48 | .paCpuIdLeavesR3 RTR3PTR_RES 1 ; R3PTRTYPE(PCPUMCPUIDLEAF)
|
---|
49 | .aCpuIdLeaves resb 256*32
|
---|
50 | .aMsrRanges resb 8192*128
|
---|
51 | endstruc
|
---|
52 |
|
---|
53 |
|
---|
54 | %define CPUM_USED_FPU_HOST RT_BIT(0)
|
---|
55 | %define CPUM_USED_FPU_GUEST RT_BIT(10)
|
---|
56 | %define CPUM_USED_FPU_SINCE_REM RT_BIT(1)
|
---|
57 | %define CPUM_USED_MANUAL_XMM_RESTORE RT_BIT(2)
|
---|
58 | %define CPUM_USE_SYSENTER RT_BIT(3)
|
---|
59 | %define CPUM_USE_SYSCALL RT_BIT(4)
|
---|
60 | %define CPUM_USE_DEBUG_REGS_HOST RT_BIT(5)
|
---|
61 | %define CPUM_USED_DEBUG_REGS_HOST RT_BIT(6)
|
---|
62 | %define CPUM_USE_DEBUG_REGS_HYPER RT_BIT(7)
|
---|
63 | %define CPUM_USED_DEBUG_REGS_HYPER RT_BIT(8)
|
---|
64 | %define CPUM_USED_DEBUG_REGS_GUEST RT_BIT(9)
|
---|
65 | %define CPUM_USE_FFXSR_LEAKY RT_BIT(19)
|
---|
66 | %define CPUM_USE_SUPPORTS_LONGMODE RT_BIT(20)
|
---|
67 |
|
---|
68 | %define CPUM_HANDLER_DS 1
|
---|
69 | %define CPUM_HANDLER_ES 2
|
---|
70 | %define CPUM_HANDLER_FS 3
|
---|
71 | %define CPUM_HANDLER_GS 4
|
---|
72 | %define CPUM_HANDLER_IRET 5
|
---|
73 | %define CPUM_HANDLER_TYPEMASK 0ffh
|
---|
74 | %define CPUM_HANDLER_CTXCORE_IN_EBP RT_BIT(31)
|
---|
75 |
|
---|
76 |
|
---|
77 | struc CPUM
|
---|
78 | ;...
|
---|
79 | .fHostUseFlags resd 1
|
---|
80 |
|
---|
81 | ; CR4 masks
|
---|
82 | .CR4.AndMask resd 1
|
---|
83 | .CR4.OrMask resd 1
|
---|
84 | .u8PortableCpuIdLevel resb 1
|
---|
85 | .fPendingRestore resb 1
|
---|
86 |
|
---|
87 | alignb 8
|
---|
88 | .fXStateGuestMask resq 1
|
---|
89 | .fXStateHostMask resq 1
|
---|
90 |
|
---|
91 | alignb 64
|
---|
92 | .HostFeatures resb 48
|
---|
93 | .GuestFeatures resb 48
|
---|
94 | .GuestInfo resb CPUMINFO_size
|
---|
95 |
|
---|
96 | ; Patch manager saved state compatability CPUID leaf arrays
|
---|
97 | .aGuestCpuIdPatmStd resb 16*6
|
---|
98 | .aGuestCpuIdPatmExt resb 16*10
|
---|
99 | .aGuestCpuIdPatmCentaur resb 16*4
|
---|
100 |
|
---|
101 | alignb 8
|
---|
102 | .cMsrWrites resq 1
|
---|
103 | .cMsrWritesToIgnoredBits resq 1
|
---|
104 | .cMsrWritesRaiseGp resq 1
|
---|
105 | .cMsrWritesUnknown resq 1
|
---|
106 | .cMsrReads resq 1
|
---|
107 | .cMsrReadsRaiseGp resq 1
|
---|
108 | .cMsrReadsUnknown resq 1
|
---|
109 | endstruc
|
---|
110 |
|
---|
111 | struc CPUMCPU
|
---|
112 | ;
|
---|
113 | ; Guest context state
|
---|
114 | ; (Identical to the .Hyper chunk below and to CPUMCTX in cpum.mac.)
|
---|
115 | ;
|
---|
116 | .Guest resq 0
|
---|
117 | .Guest.eax resq 1
|
---|
118 | .Guest.ecx resq 1
|
---|
119 | .Guest.edx resq 1
|
---|
120 | .Guest.ebx resq 1
|
---|
121 | .Guest.esp resq 1
|
---|
122 | .Guest.ebp resq 1
|
---|
123 | .Guest.esi resq 1
|
---|
124 | .Guest.edi resq 1
|
---|
125 | .Guest.r8 resq 1
|
---|
126 | .Guest.r9 resq 1
|
---|
127 | .Guest.r10 resq 1
|
---|
128 | .Guest.r11 resq 1
|
---|
129 | .Guest.r12 resq 1
|
---|
130 | .Guest.r13 resq 1
|
---|
131 | .Guest.r14 resq 1
|
---|
132 | .Guest.r15 resq 1
|
---|
133 | .Guest.es.Sel resw 1
|
---|
134 | .Guest.es.PaddingSel resw 1
|
---|
135 | .Guest.es.ValidSel resw 1
|
---|
136 | .Guest.es.fFlags resw 1
|
---|
137 | .Guest.es.u64Base resq 1
|
---|
138 | .Guest.es.u32Limit resd 1
|
---|
139 | .Guest.es.Attr resd 1
|
---|
140 | .Guest.cs.Sel resw 1
|
---|
141 | .Guest.cs.PaddingSel resw 1
|
---|
142 | .Guest.cs.ValidSel resw 1
|
---|
143 | .Guest.cs.fFlags resw 1
|
---|
144 | .Guest.cs.u64Base resq 1
|
---|
145 | .Guest.cs.u32Limit resd 1
|
---|
146 | .Guest.cs.Attr resd 1
|
---|
147 | .Guest.ss.Sel resw 1
|
---|
148 | .Guest.ss.PaddingSel resw 1
|
---|
149 | .Guest.ss.ValidSel resw 1
|
---|
150 | .Guest.ss.fFlags resw 1
|
---|
151 | .Guest.ss.u64Base resq 1
|
---|
152 | .Guest.ss.u32Limit resd 1
|
---|
153 | .Guest.ss.Attr resd 1
|
---|
154 | .Guest.ds.Sel resw 1
|
---|
155 | .Guest.ds.PaddingSel resw 1
|
---|
156 | .Guest.ds.ValidSel resw 1
|
---|
157 | .Guest.ds.fFlags resw 1
|
---|
158 | .Guest.ds.u64Base resq 1
|
---|
159 | .Guest.ds.u32Limit resd 1
|
---|
160 | .Guest.ds.Attr resd 1
|
---|
161 | .Guest.fs.Sel resw 1
|
---|
162 | .Guest.fs.PaddingSel resw 1
|
---|
163 | .Guest.fs.ValidSel resw 1
|
---|
164 | .Guest.fs.fFlags resw 1
|
---|
165 | .Guest.fs.u64Base resq 1
|
---|
166 | .Guest.fs.u32Limit resd 1
|
---|
167 | .Guest.fs.Attr resd 1
|
---|
168 | .Guest.gs.Sel resw 1
|
---|
169 | .Guest.gs.PaddingSel resw 1
|
---|
170 | .Guest.gs.ValidSel resw 1
|
---|
171 | .Guest.gs.fFlags resw 1
|
---|
172 | .Guest.gs.u64Base resq 1
|
---|
173 | .Guest.gs.u32Limit resd 1
|
---|
174 | .Guest.gs.Attr resd 1
|
---|
175 | .Guest.eip resq 1
|
---|
176 | .Guest.eflags resq 1
|
---|
177 | .Guest.cr0 resq 1
|
---|
178 | .Guest.cr2 resq 1
|
---|
179 | .Guest.cr3 resq 1
|
---|
180 | .Guest.cr4 resq 1
|
---|
181 | .Guest.dr resq 8
|
---|
182 | .Guest.gdtrPadding resw 3
|
---|
183 | .Guest.gdtr resw 0
|
---|
184 | .Guest.gdtr.cbGdt resw 1
|
---|
185 | .Guest.gdtr.pGdt resq 1
|
---|
186 | .Guest.idtrPadding resw 3
|
---|
187 | .Guest.idtr resw 0
|
---|
188 | .Guest.idtr.cbIdt resw 1
|
---|
189 | .Guest.idtr.pIdt resq 1
|
---|
190 | .Guest.ldtr.Sel resw 1
|
---|
191 | .Guest.ldtr.PaddingSel resw 1
|
---|
192 | .Guest.ldtr.ValidSel resw 1
|
---|
193 | .Guest.ldtr.fFlags resw 1
|
---|
194 | .Guest.ldtr.u64Base resq 1
|
---|
195 | .Guest.ldtr.u32Limit resd 1
|
---|
196 | .Guest.ldtr.Attr resd 1
|
---|
197 | .Guest.tr.Sel resw 1
|
---|
198 | .Guest.tr.PaddingSel resw 1
|
---|
199 | .Guest.tr.ValidSel resw 1
|
---|
200 | .Guest.tr.fFlags resw 1
|
---|
201 | .Guest.tr.u64Base resq 1
|
---|
202 | .Guest.tr.u32Limit resd 1
|
---|
203 | .Guest.tr.Attr resd 1
|
---|
204 | .Guest.SysEnter.cs resb 8
|
---|
205 | .Guest.SysEnter.eip resb 8
|
---|
206 | .Guest.SysEnter.esp resb 8
|
---|
207 | .Guest.msrEFER resb 8
|
---|
208 | .Guest.msrSTAR resb 8
|
---|
209 | .Guest.msrPAT resb 8
|
---|
210 | .Guest.msrLSTAR resb 8
|
---|
211 | .Guest.msrCSTAR resb 8
|
---|
212 | .Guest.msrSFMASK resb 8
|
---|
213 | .Guest.msrKERNELGSBASE resb 8
|
---|
214 | .Guest.uMsrPadding0 resb 8
|
---|
215 |
|
---|
216 | alignb 8
|
---|
217 | .Guest.fExtrn resq 1
|
---|
218 |
|
---|
219 | alignb 32
|
---|
220 | .Guest.aPaePdpes resq 4
|
---|
221 |
|
---|
222 | alignb 8
|
---|
223 | .Guest.aXcr resq 2
|
---|
224 | .Guest.fXStateMask resq 1
|
---|
225 | .Guest.fUsedFpuGuest resb 1
|
---|
226 | alignb 8
|
---|
227 | .Guest.aoffXState resw 64
|
---|
228 | alignb 256
|
---|
229 | .Guest.abXState resb 0x4000-0x300
|
---|
230 | .Guest.XState EQU .Guest.abXState
|
---|
231 |
|
---|
232 | ;;
|
---|
233 | alignb 4096
|
---|
234 | .Guest.hwvirt resb 0
|
---|
235 | .Guest.hwvirt.svm resb 0
|
---|
236 | .Guest.hwvirt.vmx resb 0
|
---|
237 |
|
---|
238 | .Guest.hwvirt.svm.Vmcb EQU .Guest.hwvirt.svm
|
---|
239 | .Guest.hwvirt.svm.abMsrBitmap EQU (.Guest.hwvirt.svm.Vmcb + 0x1000)
|
---|
240 | .Guest.hwvirt.svm.abIoBitmap EQU (.Guest.hwvirt.svm.abMsrBitmap + 0x2000)
|
---|
241 | .Guest.hwvirt.svm.uMsrHSavePa EQU (.Guest.hwvirt.svm.abIoBitmap + 0x3000) ; resq 1
|
---|
242 | .Guest.hwvirt.svm.GCPhysVmcb EQU (.Guest.hwvirt.svm.uMsrHSavePa + 8) ; resq 1
|
---|
243 | alignb 8
|
---|
244 | .Guest.hwvirt.svm.HostState EQU (.Guest.hwvirt.svm.GCPhysVmcb + 8) ; resb 184
|
---|
245 | .Guest.hwvirt.svm.uPrevPauseTick EQU (.Guest.hwvirt.svm.HostState + 184) ; resq 1
|
---|
246 | .Guest.hwvirt.svm.cPauseFilter EQU (.Guest.hwvirt.svm.uPrevPauseTick + 8) ; resw 1
|
---|
247 | .Guest.hwvirt.svm.cPauseFilterThreshold EQU (.Guest.hwvirt.svm.cPauseFilter + 2) ; resw 1
|
---|
248 | .Guest.hwvirt.svm.fInterceptEvents EQU (.Guest.hwvirt.svm.cPauseFilterThreshold + 2) ; resb 1
|
---|
249 |
|
---|
250 | .Guest.hwvirt.vmx.Vmcs resb 0x1000
|
---|
251 | .Guest.hwvirt.vmx.ShadowVmcs resb 0x1000
|
---|
252 | .Guest.hwvirt.vmx.abVmreadBitmap resb 0x1000
|
---|
253 | .Guest.hwvirt.vmx.abVmwriteBitmap resb 0x1000
|
---|
254 | .Guest.hwvirt.vmx.aEntryMsrLoadArea resb 0x2000
|
---|
255 | .Guest.hwvirt.vmx.aExitMsrStoreArea resb 0x2000
|
---|
256 | .Guest.hwvirt.vmx.aExitMsrLoadArea resb 0x2000
|
---|
257 | .Guest.hwvirt.vmx.abMsrBitmap resb 0x1000
|
---|
258 | .Guest.hwvirt.vmx.abIoBitmap resb 0x1000+0x1000
|
---|
259 | .Guest.hwvirt.vmx.abVirtApicPage resb 0x1000
|
---|
260 | alignb 8
|
---|
261 | .Guest.hwvirt.vmx.GCPhysVmxon resq 1
|
---|
262 | .Guest.hwvirt.vmx.GCPhysVmcs resq 1
|
---|
263 | .Guest.hwvirt.vmx.GCPhysShadowVmcs resq 1
|
---|
264 | .Guest.hwvirt.vmx.enmDiag resd 1
|
---|
265 | .Guest.hwvirt.vmx.enmAbort resd 1
|
---|
266 | .Guest.hwvirt.vmx.uDiagAux resq 1
|
---|
267 | .Guest.hwvirt.vmx.uAbortAux resd 1
|
---|
268 | .Guest.hwvirt.vmx.fInVmxRootMode resb 1
|
---|
269 | .Guest.hwvirt.vmx.fInVmxNonRootMode resb 1
|
---|
270 | .Guest.hwvirt.vmx.fInterceptEvents resb 1
|
---|
271 | .Guest.hwvirt.vmx.fNmiUnblockingIret resb 1
|
---|
272 | .Guest.hwvirt.vmx.uFirstPauseLoopTick resq 1
|
---|
273 | .Guest.hwvirt.vmx.uPrevPauseTick resq 1
|
---|
274 | .Guest.hwvirt.vmx.uEntryTick resq 1
|
---|
275 | .Guest.hwvirt.vmx.offVirtApicWrite resw 1
|
---|
276 | .Guest.hwvirt.vmx.fVirtNmiBlocking resb 1
|
---|
277 | alignb 8
|
---|
278 | .Guest.hwvirt.vmx.Msrs resb 224
|
---|
279 |
|
---|
280 | alignb 8
|
---|
281 | .Guest.hwvirt.enmHwvirt resd 1
|
---|
282 | .Guest.hwvirt.fGif resb 1
|
---|
283 | alignb 8
|
---|
284 | .Guest.hwvirt.fLocalForcedActions resd 1
|
---|
285 | alignb 64
|
---|
286 |
|
---|
287 | .GuestMsrs resq 0
|
---|
288 | .GuestMsrs.au64 resq 64
|
---|
289 |
|
---|
290 | ;
|
---|
291 | ; Other stuff.
|
---|
292 | ;
|
---|
293 | .hNestedVmxPreemptTimer resq 1
|
---|
294 |
|
---|
295 | .fUseFlags resd 1
|
---|
296 | .fChanged resd 1
|
---|
297 | .u32RetCode resd 1
|
---|
298 |
|
---|
299 | %ifdef VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
|
---|
300 | .fApicDisVectors resd 1
|
---|
301 | .pvApicBase RTR0PTR_RES 1
|
---|
302 | .fX2Apic resb 1
|
---|
303 | %else
|
---|
304 | .abPadding3 resb (4 + RTR0PTR_CB + 1)
|
---|
305 | %endif
|
---|
306 |
|
---|
307 | .fCpuIdApicFeatureVisible resb 1
|
---|
308 |
|
---|
309 | ;
|
---|
310 | ; Host context state
|
---|
311 | ;
|
---|
312 | alignb 64
|
---|
313 | .Host resb 0
|
---|
314 | .Host.abXState resb 0x4000-0x300
|
---|
315 | .Host.XState EQU .Host.abXState
|
---|
316 | ;.Host.rax resq 1 - scratch
|
---|
317 | .Host.rbx resq 1
|
---|
318 | ;.Host.rcx resq 1 - scratch
|
---|
319 | ;.Host.rdx resq 1 - scratch
|
---|
320 | .Host.rdi resq 1
|
---|
321 | .Host.rsi resq 1
|
---|
322 | .Host.rbp resq 1
|
---|
323 | .Host.rsp resq 1
|
---|
324 | ;.Host.r8 resq 1 - scratch
|
---|
325 | ;.Host.r9 resq 1 - scratch
|
---|
326 | .Host.r10 resq 1
|
---|
327 | .Host.r11 resq 1
|
---|
328 | .Host.r12 resq 1
|
---|
329 | .Host.r13 resq 1
|
---|
330 | .Host.r14 resq 1
|
---|
331 | .Host.r15 resq 1
|
---|
332 | ;.Host.rip resd 1 - scratch
|
---|
333 | .Host.rflags resq 1
|
---|
334 | .Host.ss resw 1
|
---|
335 | .Host.ssPadding resw 1
|
---|
336 | .Host.gs resw 1
|
---|
337 | .Host.gsPadding resw 1
|
---|
338 | .Host.fs resw 1
|
---|
339 | .Host.fsPadding resw 1
|
---|
340 | .Host.es resw 1
|
---|
341 | .Host.esPadding resw 1
|
---|
342 | .Host.ds resw 1
|
---|
343 | .Host.dsPadding resw 1
|
---|
344 | .Host.cs resw 1
|
---|
345 | .Host.csPadding resw 1
|
---|
346 |
|
---|
347 | .Host.cr0Fpu:
|
---|
348 | .Host.cr0 resq 1
|
---|
349 | ;.Host.cr2 resq 1 - scratch
|
---|
350 | .Host.cr3 resq 1
|
---|
351 | .Host.cr4 resq 1
|
---|
352 | .Host.cr8 resq 1
|
---|
353 |
|
---|
354 | .Host.dr0 resq 1
|
---|
355 | .Host.dr1 resq 1
|
---|
356 | .Host.dr2 resq 1
|
---|
357 | .Host.dr3 resq 1
|
---|
358 | .Host.dr6 resq 1
|
---|
359 | .Host.dr7 resq 1
|
---|
360 |
|
---|
361 | .Host.gdtr resb 10 ; GDT limit + linear address
|
---|
362 | .Host.gdtrPadding resw 1
|
---|
363 | .Host.idtr resb 10 ; IDT limit + linear address
|
---|
364 | .Host.idtrPadding resw 1
|
---|
365 | .Host.ldtr resw 1
|
---|
366 | .Host.ldtrPadding resw 1
|
---|
367 | .Host.tr resw 1
|
---|
368 | .Host.trPadding resw 1
|
---|
369 |
|
---|
370 | .Host.SysEnter.cs resq 1
|
---|
371 | .Host.SysEnter.eip resq 1
|
---|
372 | .Host.SysEnter.esp resq 1
|
---|
373 | .Host.FSbase resq 1
|
---|
374 | .Host.GSbase resq 1
|
---|
375 | .Host.efer resq 1
|
---|
376 | alignb 8
|
---|
377 | .Host.xcr0 resq 1
|
---|
378 | .Host.fXStateMask resq 1
|
---|
379 |
|
---|
380 | ;
|
---|
381 | ; Hypervisor Context.
|
---|
382 | ;
|
---|
383 | alignb 64
|
---|
384 | .Hyper resq 0
|
---|
385 | .Hyper.dr resq 8
|
---|
386 | .Hyper.cr3 resq 1
|
---|
387 | alignb 64
|
---|
388 |
|
---|
389 | %ifdef VBOX_WITH_CRASHDUMP_MAGIC
|
---|
390 | .aMagic resb 56
|
---|
391 | .uMagic resq 1
|
---|
392 | %endif
|
---|
393 | endstruc
|
---|
394 |
|
---|
395 |
|
---|
396 |
|
---|
397 | %if 0 ; Currently not used anywhere.
|
---|
398 | ;;
|
---|
399 | ; Macro for FXSAVE/FXRSTOR leaky behaviour on AMD CPUs, see cpumR3CheckLeakyFpu().
|
---|
400 | ;
|
---|
401 | ; Cleans the FPU state, if necessary, before restoring the FPU.
|
---|
402 | ;
|
---|
403 | ; This macro ASSUMES CR0.TS is not set!
|
---|
404 | ;
|
---|
405 | ; @param xDX Pointer to CPUMCPU.
|
---|
406 | ; @uses xAX, EFLAGS
|
---|
407 | ;
|
---|
408 | ; Changes here should also be reflected in CPUMRCA.asm's copy!
|
---|
409 | ;
|
---|
410 | %macro CLEANFPU 0
|
---|
411 | test dword [xDX + CPUMCPU.fUseFlags], CPUM_USE_FFXSR_LEAKY
|
---|
412 | jz .nothing_to_clean
|
---|
413 |
|
---|
414 | xor eax, eax
|
---|
415 | fnstsw ax ; FSW -> AX.
|
---|
416 | test eax, RT_BIT(7) ; If FSW.ES (bit 7) is set, clear it to not cause FPU exceptions
|
---|
417 | ; while clearing & loading the FPU bits in 'clean_fpu' below.
|
---|
418 | jz .clean_fpu
|
---|
419 | fnclex
|
---|
420 |
|
---|
421 | .clean_fpu:
|
---|
422 | ffree st7 ; Clear FPU stack register(7)'s tag entry to prevent overflow if a wraparound occurs.
|
---|
423 | ; for the upcoming push (load)
|
---|
424 | fild dword [g_r32_Zero xWrtRIP] ; Explicit FPU load to overwrite FIP, FOP, FDP registers in the FPU.
|
---|
425 | .nothing_to_clean:
|
---|
426 | %endmacro
|
---|
427 | %endif ; Unused.
|
---|
428 |
|
---|
429 |
|
---|
430 | ;;
|
---|
431 | ; Makes sure we don't trap (#NM) accessing the FPU.
|
---|
432 | ;
|
---|
433 | ; In ring-0 this is a bit of work since we may have try convince the host kernel
|
---|
434 | ; to do the work for us, also, we must report any CR0 changes back to HMR0VMX
|
---|
435 | ; via the VINF_CPUM_HOST_CR0_MODIFIED status code.
|
---|
436 | ;
|
---|
437 | ; If we end up clearing CR0.TS/EM ourselves in ring-0, we'll save the original
|
---|
438 | ; value in CPUMCPU.Host.cr0Fpu. If we don't, we'll store zero there. (See also
|
---|
439 | ; CPUMRZ_RESTORE_CR0_IF_TS_OR_EM_SET.)
|
---|
440 | ;
|
---|
441 | ; In raw-mode we will always have to clear TS and it will be recalculated
|
---|
442 | ; elsewhere and thus needs no saving.
|
---|
443 | ;
|
---|
444 | ; @param %1 Register to return the return status code in.
|
---|
445 | ; @param %2 Temporary scratch register.
|
---|
446 | ; @param %3 Ring-0 only, register pointing to the CPUMCPU structure
|
---|
447 | ; of the EMT we're on.
|
---|
448 | ; @uses EFLAGS, CR0, %1, %2
|
---|
449 | ;
|
---|
450 | %macro CPUMRZ_TOUCH_FPU_CLEAR_CR0_FPU_TRAPS_SET_RC 3
|
---|
451 | ;
|
---|
452 | ; ring-0 - slightly complicated (than old raw-mode).
|
---|
453 | ;
|
---|
454 | xor %1, %1 ; 0 / VINF_SUCCESS. Wishing for no CR0 changes.
|
---|
455 | mov [%3 + CPUMCPU.Host.cr0Fpu], %1
|
---|
456 |
|
---|
457 | mov %2, cr0
|
---|
458 | test %2, X86_CR0_TS | X86_CR0_EM ; Make sure its safe to access the FPU state.
|
---|
459 | jz %%no_cr0_change
|
---|
460 |
|
---|
461 | %ifdef VMM_R0_TOUCH_FPU
|
---|
462 | ; Touch the state and check that the kernel updated CR0 for us.
|
---|
463 | movdqa xmm0, xmm0
|
---|
464 | mov %2, cr0
|
---|
465 | test %2, X86_CR0_TS | X86_CR0_EM
|
---|
466 | jz %%cr0_changed
|
---|
467 | %endif
|
---|
468 |
|
---|
469 | ; Save CR0 and clear them flags ourselves.
|
---|
470 | mov [%3 + CPUMCPU.Host.cr0Fpu], %2
|
---|
471 | and %2, ~(X86_CR0_TS | X86_CR0_EM)
|
---|
472 | mov cr0, %2
|
---|
473 |
|
---|
474 | %%cr0_changed:
|
---|
475 | mov %1, VINF_CPUM_HOST_CR0_MODIFIED
|
---|
476 | %%no_cr0_change:
|
---|
477 | %endmacro
|
---|
478 |
|
---|
479 |
|
---|
480 | ;;
|
---|
481 | ; Restore CR0 if CR0.TS or CR0.EM were non-zero in the original state.
|
---|
482 | ;
|
---|
483 | ; @param %1 The original state to restore (or zero).
|
---|
484 | ;
|
---|
485 | %macro CPUMRZ_RESTORE_CR0_IF_TS_OR_EM_SET 1
|
---|
486 | test %1, X86_CR0_TS | X86_CR0_EM
|
---|
487 | jz %%skip_cr0_restore
|
---|
488 | mov cr0, %1
|
---|
489 | %%skip_cr0_restore:
|
---|
490 | %endmacro
|
---|
491 |
|
---|
492 |
|
---|
493 | ;;
|
---|
494 | ; Saves the host state.
|
---|
495 | ;
|
---|
496 | ; @uses rax, rdx
|
---|
497 | ; @param pCpumCpu Define for the register containing the CPUMCPU pointer.
|
---|
498 | ; @param pXState Define for the register containing the extended state pointer.
|
---|
499 | ;
|
---|
500 | %macro CPUMR0_SAVE_HOST 0
|
---|
501 | ;
|
---|
502 | ; Load a couple of registers we'll use later in all branches.
|
---|
503 | ;
|
---|
504 | lea pXState, [pCpumCpu + CPUMCPU.Host.XState]
|
---|
505 | mov eax, [pCpumCpu + CPUMCPU.Host.fXStateMask]
|
---|
506 |
|
---|
507 | ;
|
---|
508 | ; XSAVE or FXSAVE?
|
---|
509 | ;
|
---|
510 | or eax, eax
|
---|
511 | jz %%host_fxsave
|
---|
512 |
|
---|
513 | ; XSAVE
|
---|
514 | mov edx, [pCpumCpu + CPUMCPU.Host.fXStateMask + 4]
|
---|
515 | %ifdef RT_ARCH_AMD64
|
---|
516 | o64 xsave [pXState]
|
---|
517 | %else
|
---|
518 | xsave [pXState]
|
---|
519 | %endif
|
---|
520 | jmp %%host_done
|
---|
521 |
|
---|
522 | ; FXSAVE
|
---|
523 | %%host_fxsave:
|
---|
524 | %ifdef RT_ARCH_AMD64
|
---|
525 | o64 fxsave [pXState] ; Use explicit REX prefix. See @bugref{6398}.
|
---|
526 | %else
|
---|
527 | fxsave [pXState]
|
---|
528 | %endif
|
---|
529 |
|
---|
530 | %%host_done:
|
---|
531 | %endmacro ; CPUMR0_SAVE_HOST
|
---|
532 |
|
---|
533 |
|
---|
534 | ;;
|
---|
535 | ; Loads the host state.
|
---|
536 | ;
|
---|
537 | ; @uses rax, rdx
|
---|
538 | ; @param pCpumCpu Define for the register containing the CPUMCPU pointer.
|
---|
539 | ; @param pXState Define for the register containing the extended state pointer.
|
---|
540 | ;
|
---|
541 | %macro CPUMR0_LOAD_HOST 0
|
---|
542 | ;
|
---|
543 | ; Load a couple of registers we'll use later in all branches.
|
---|
544 | ;
|
---|
545 | lea pXState, [pCpumCpu + CPUMCPU.Host.XState]
|
---|
546 | mov eax, [pCpumCpu + CPUMCPU.Host.fXStateMask]
|
---|
547 |
|
---|
548 | ;
|
---|
549 | ; XRSTOR or FXRSTOR?
|
---|
550 | ;
|
---|
551 | or eax, eax
|
---|
552 | jz %%host_fxrstor
|
---|
553 |
|
---|
554 | ; XRSTOR
|
---|
555 | mov edx, [pCpumCpu + CPUMCPU.Host.fXStateMask + 4]
|
---|
556 | %ifdef RT_ARCH_AMD64
|
---|
557 | o64 xrstor [pXState]
|
---|
558 | %else
|
---|
559 | xrstor [pXState]
|
---|
560 | %endif
|
---|
561 | jmp %%host_done
|
---|
562 |
|
---|
563 | ; FXRSTOR
|
---|
564 | %%host_fxrstor:
|
---|
565 | %ifdef RT_ARCH_AMD64
|
---|
566 | o64 fxrstor [pXState] ; Use explicit REX prefix. See @bugref{6398}.
|
---|
567 | %else
|
---|
568 | fxrstor [pXState]
|
---|
569 | %endif
|
---|
570 |
|
---|
571 | %%host_done:
|
---|
572 | %endmacro ; CPUMR0_LOAD_HOST
|
---|
573 |
|
---|
574 |
|
---|
575 |
|
---|
576 | ;; Macro for XSAVE/FXSAVE for the guest FPU but tries to figure out whether to
|
---|
577 | ; save the 32-bit FPU state or 64-bit FPU state.
|
---|
578 | ;
|
---|
579 | ; @param %1 Pointer to CPUMCPU.
|
---|
580 | ; @param %2 Pointer to XState.
|
---|
581 | ; @param %3 Force AMD64
|
---|
582 | ; @param %4 The instruction to use (xsave or fxsave)
|
---|
583 | ; @uses xAX, xDX, EFLAGS, 20h of stack.
|
---|
584 | ;
|
---|
585 | %macro SAVE_32_OR_64_FPU 4
|
---|
586 | %if CPUM_IS_AMD64 || %3
|
---|
587 | ; Save the guest FPU (32-bit or 64-bit), preserves existing broken state. See @bugref{7138}.
|
---|
588 | test dword [pCpumCpu + CPUMCPU.fUseFlags], CPUM_USE_SUPPORTS_LONGMODE
|
---|
589 | jnz short %%save_long_mode_guest
|
---|
590 | %endif
|
---|
591 | %4 [pXState]
|
---|
592 | %if CPUM_IS_AMD64 || %3
|
---|
593 | jmp %%save_done_32bit_cs_ds
|
---|
594 |
|
---|
595 | %%save_long_mode_guest:
|
---|
596 | o64 %4 [pXState]
|
---|
597 |
|
---|
598 | xor edx, edx
|
---|
599 | cmp dword [pXState + X86FXSTATE.FPUCS], 0
|
---|
600 | jne short %%save_done
|
---|
601 |
|
---|
602 | sub rsp, 20h ; Only need 1ch bytes but keep stack aligned otherwise we #GP(0).
|
---|
603 | fnstenv [rsp]
|
---|
604 | movzx eax, word [rsp + 10h]
|
---|
605 | mov [pXState + X86FXSTATE.FPUCS], eax
|
---|
606 | movzx eax, word [rsp + 18h]
|
---|
607 | add rsp, 20h
|
---|
608 | mov [pXState + X86FXSTATE.FPUDS], eax
|
---|
609 | %endif
|
---|
610 | %%save_done_32bit_cs_ds:
|
---|
611 | mov edx, X86_FXSTATE_RSVD_32BIT_MAGIC
|
---|
612 | %%save_done:
|
---|
613 | mov dword [pXState + X86_OFF_FXSTATE_RSVD], edx
|
---|
614 | %endmacro ; SAVE_32_OR_64_FPU
|
---|
615 |
|
---|
616 |
|
---|
617 | ;;
|
---|
618 | ; Save the guest state.
|
---|
619 | ;
|
---|
620 | ; @uses rax, rdx
|
---|
621 | ; @param pCpumCpu Define for the register containing the CPUMCPU pointer.
|
---|
622 | ; @param pXState Define for the register containing the extended state pointer.
|
---|
623 | ;
|
---|
624 | %macro CPUMR0_SAVE_GUEST 0
|
---|
625 | ;
|
---|
626 | ; Load a couple of registers we'll use later in all branches.
|
---|
627 | ;
|
---|
628 | %ifdef IN_RING0
|
---|
629 | lea pXState, [pCpumCpu + CPUMCPU.Guest.XState]
|
---|
630 | %else
|
---|
631 | %error "Unsupported context!"
|
---|
632 | %endif
|
---|
633 | mov eax, [pCpumCpu + CPUMCPU.Guest.fXStateMask]
|
---|
634 |
|
---|
635 | ;
|
---|
636 | ; XSAVE or FXSAVE?
|
---|
637 | ;
|
---|
638 | or eax, eax
|
---|
639 | jz %%guest_fxsave
|
---|
640 |
|
---|
641 | ; XSAVE
|
---|
642 | mov edx, [pCpumCpu + CPUMCPU.Guest.fXStateMask + 4]
|
---|
643 | %ifdef VBOX_WITH_KERNEL_USING_XMM
|
---|
644 | and eax, ~CPUM_VOLATILE_XSAVE_GUEST_COMPONENTS ; Already saved in HMR0A.asm.
|
---|
645 | %endif
|
---|
646 | SAVE_32_OR_64_FPU pCpumCpu, pXState, 0, xsave
|
---|
647 | jmp %%guest_done
|
---|
648 |
|
---|
649 | ; FXSAVE
|
---|
650 | %%guest_fxsave:
|
---|
651 | SAVE_32_OR_64_FPU pCpumCpu, pXState, 0, fxsave
|
---|
652 |
|
---|
653 | %%guest_done:
|
---|
654 | %endmacro ; CPUMR0_SAVE_GUEST
|
---|
655 |
|
---|
656 |
|
---|
657 | ;;
|
---|
658 | ; Wrapper for selecting 32-bit or 64-bit XRSTOR/FXRSTOR according to what SAVE_32_OR_64_FPU did.
|
---|
659 | ;
|
---|
660 | ; @param %1 Pointer to CPUMCPU.
|
---|
661 | ; @param %2 Pointer to XState.
|
---|
662 | ; @param %3 Force AMD64.
|
---|
663 | ; @param %4 The instruction to use (xrstor or fxrstor).
|
---|
664 | ; @uses xAX, xDX, EFLAGS
|
---|
665 | ;
|
---|
666 | %macro RESTORE_32_OR_64_FPU 4
|
---|
667 | %if CPUM_IS_AMD64 || %3
|
---|
668 | ; Restore the guest FPU (32-bit or 64-bit), preserves existing broken state. See @bugref{7138}.
|
---|
669 | test dword [pCpumCpu + CPUMCPU.fUseFlags], CPUM_USE_SUPPORTS_LONGMODE
|
---|
670 | jz %%restore_32bit_fpu
|
---|
671 | cmp dword [pXState + X86_OFF_FXSTATE_RSVD], X86_FXSTATE_RSVD_32BIT_MAGIC
|
---|
672 | jne short %%restore_64bit_fpu
|
---|
673 | %%restore_32bit_fpu:
|
---|
674 | %endif
|
---|
675 | %4 [pXState]
|
---|
676 | %if CPUM_IS_AMD64 || %3
|
---|
677 | ; TODO: Restore XMM8-XMM15!
|
---|
678 | jmp short %%restore_fpu_done
|
---|
679 | %%restore_64bit_fpu:
|
---|
680 | o64 %4 [pXState]
|
---|
681 | %%restore_fpu_done:
|
---|
682 | %endif
|
---|
683 | %endmacro ; RESTORE_32_OR_64_FPU
|
---|
684 |
|
---|
685 |
|
---|
686 | ;;
|
---|
687 | ; Loads the guest state.
|
---|
688 | ;
|
---|
689 | ; @uses rax, rdx
|
---|
690 | ; @param pCpumCpu Define for the register containing the CPUMCPU pointer.
|
---|
691 | ; @param pXState Define for the register containing the extended state pointer.
|
---|
692 | ;
|
---|
693 | %macro CPUMR0_LOAD_GUEST 0
|
---|
694 | ;
|
---|
695 | ; Load a couple of registers we'll use later in all branches.
|
---|
696 | ;
|
---|
697 | lea pXState, [pCpumCpu + CPUMCPU.Guest.XState]
|
---|
698 | mov eax, [pCpumCpu + CPUMCPU.Guest.fXStateMask]
|
---|
699 |
|
---|
700 | ;
|
---|
701 | ; XRSTOR or FXRSTOR?
|
---|
702 | ;
|
---|
703 | or eax, eax
|
---|
704 | jz %%guest_fxrstor
|
---|
705 |
|
---|
706 | ; XRSTOR
|
---|
707 | mov edx, [pCpumCpu + CPUMCPU.Guest.fXStateMask + 4]
|
---|
708 | %ifdef VBOX_WITH_KERNEL_USING_XMM
|
---|
709 | and eax, ~CPUM_VOLATILE_XSAVE_GUEST_COMPONENTS ; Will be loaded by HMR0A.asm.
|
---|
710 | %endif
|
---|
711 | RESTORE_32_OR_64_FPU pCpumCpu, pXState, 0, xrstor
|
---|
712 | jmp %%guest_done
|
---|
713 |
|
---|
714 | ; FXRSTOR
|
---|
715 | %%guest_fxrstor:
|
---|
716 | RESTORE_32_OR_64_FPU pCpumCpu, pXState, 0, fxrstor
|
---|
717 |
|
---|
718 | %%guest_done:
|
---|
719 | %endmacro ; CPUMR0_LOAD_GUEST
|
---|
720 |
|
---|