VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/CPUM.cpp@ 76827

Last change on this file since 76827 was 76827, checked in by vboxsync, 6 years ago

VMM: Nested VMX: bugref:9180 Nits.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 218.7 KB
Line 
1/* $Id: CPUM.cpp 76827 2019-01-16 05:39:39Z vboxsync $ */
2/** @file
3 * CPUM - CPU Monitor / Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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/** @page pg_cpum CPUM - CPU Monitor / Manager
19 *
20 * The CPU Monitor / Manager keeps track of all the CPU registers. It is
21 * also responsible for lazy FPU handling and some of the context loading
22 * in raw mode.
23 *
24 * There are three CPU contexts, the most important one is the guest one (GC).
25 * When running in raw-mode (RC) there is a special hyper context for the VMM
26 * part that floats around inside the guest address space. When running in
27 * raw-mode, CPUM also maintains a host context for saving and restoring
28 * registers across world switches. This latter is done in cooperation with the
29 * world switcher (@see pg_vmm).
30 *
31 * @see grp_cpum
32 *
33 * @section sec_cpum_fpu FPU / SSE / AVX / ++ state.
34 *
35 * TODO: proper write up, currently just some notes.
36 *
37 * The ring-0 FPU handling per OS:
38 *
39 * - 64-bit Windows uses XMM registers in the kernel as part of the calling
40 * convention (Visual C++ doesn't seem to have a way to disable
41 * generating such code either), so CR0.TS/EM are always zero from what I
42 * can tell. We are also forced to always load/save the guest XMM0-XMM15
43 * registers when entering/leaving guest context. Interrupt handlers
44 * using FPU/SSE will offically have call save and restore functions
45 * exported by the kernel, if the really really have to use the state.
46 *
47 * - 32-bit windows does lazy FPU handling, I think, probably including
48 * lazying saving. The Windows Internals book states that it's a bad
49 * idea to use the FPU in kernel space. However, it looks like it will
50 * restore the FPU state of the current thread in case of a kernel \#NM.
51 * Interrupt handlers should be same as for 64-bit.
52 *
53 * - Darwin allows taking \#NM in kernel space, restoring current thread's
54 * state if I read the code correctly. It saves the FPU state of the
55 * outgoing thread, and uses CR0.TS to lazily load the state of the
56 * incoming one. No idea yet how the FPU is treated by interrupt
57 * handlers, i.e. whether they are allowed to disable the state or
58 * something.
59 *
60 * - Linux also allows \#NM in kernel space (don't know since when), and
61 * uses CR0.TS for lazy loading. Saves outgoing thread's state, lazy
62 * loads the incoming unless configured to agressivly load it. Interrupt
63 * handlers can ask whether they're allowed to use the FPU, and may
64 * freely trash the state if Linux thinks it has saved the thread's state
65 * already. This is a problem.
66 *
67 * - Solaris will, from what I can tell, panic if it gets an \#NM in kernel
68 * context. When switching threads, the kernel will save the state of
69 * the outgoing thread and lazy load the incoming one using CR0.TS.
70 * There are a few routines in seeblk.s which uses the SSE unit in ring-0
71 * to do stuff, HAT are among the users. The routines there will
72 * manually clear CR0.TS and save the XMM registers they use only if
73 * CR0.TS was zero upon entry. They will skip it when not, because as
74 * mentioned above, the FPU state is saved when switching away from a
75 * thread and CR0.TS set to 1, so when CR0.TS is 1 there is nothing to
76 * preserve. This is a problem if we restore CR0.TS to 1 after loading
77 * the guest state.
78 *
79 * - FreeBSD - no idea yet.
80 *
81 * - OS/2 does not allow \#NMs in kernel space IIRC. Does lazy loading,
82 * possibly also lazy saving. Interrupts must preserve the CR0.TS+EM &
83 * FPU states.
84 *
85 * Up to r107425 (2016-05-24) we would only temporarily modify CR0.TS/EM while
86 * saving and restoring the host and guest states. The motivation for this
87 * change is that we want to be able to emulate SSE instruction in ring-0 (IEM).
88 *
89 * Starting with that change, we will leave CR0.TS=EM=0 after saving the host
90 * state and only restore it once we've restore the host FPU state. This has the
91 * accidental side effect of triggering Solaris to preserve XMM registers in
92 * sseblk.s. When CR0 was changed by saving the FPU state, CPUM must now inform
93 * the VT-x (HMVMX) code about it as it caches the CR0 value in the VMCS.
94 *
95 *
96 * @section sec_cpum_logging Logging Level Assignments.
97 *
98 * Following log level assignments:
99 * - Log6 is used for FPU state management.
100 * - Log7 is used for FPU state actualization.
101 *
102 */
103
104
105/*********************************************************************************************************************************
106* Header Files *
107*********************************************************************************************************************************/
108#define LOG_GROUP LOG_GROUP_CPUM
109#include <VBox/vmm/cpum.h>
110#include <VBox/vmm/cpumdis.h>
111#include <VBox/vmm/cpumctx-v1_6.h>
112#include <VBox/vmm/pgm.h>
113#include <VBox/vmm/apic.h>
114#include <VBox/vmm/mm.h>
115#include <VBox/vmm/em.h>
116#include <VBox/vmm/iem.h>
117#include <VBox/vmm/selm.h>
118#include <VBox/vmm/dbgf.h>
119#include <VBox/vmm/patm.h>
120#include <VBox/vmm/hm.h>
121#include <VBox/vmm/ssm.h>
122#include "CPUMInternal.h"
123#include <VBox/vmm/vm.h>
124
125#include <VBox/param.h>
126#include <VBox/dis.h>
127#include <VBox/err.h>
128#include <VBox/log.h>
129#include <iprt/asm-amd64-x86.h>
130#include <iprt/assert.h>
131#include <iprt/cpuset.h>
132#include <iprt/mem.h>
133#include <iprt/mp.h>
134#include <iprt/string.h>
135
136
137/*********************************************************************************************************************************
138* Defined Constants And Macros *
139*********************************************************************************************************************************/
140/**
141 * This was used in the saved state up to the early life of version 14.
142 *
143 * It indicates that we may have some out-of-sync hidden segement registers.
144 * It is only relevant for raw-mode.
145 */
146#define CPUM_CHANGED_HIDDEN_SEL_REGS_INVALID RT_BIT(12)
147
148
149/*********************************************************************************************************************************
150* Structures and Typedefs *
151*********************************************************************************************************************************/
152
153/**
154 * What kind of cpu info dump to perform.
155 */
156typedef enum CPUMDUMPTYPE
157{
158 CPUMDUMPTYPE_TERSE,
159 CPUMDUMPTYPE_DEFAULT,
160 CPUMDUMPTYPE_VERBOSE
161} CPUMDUMPTYPE;
162/** Pointer to a cpu info dump type. */
163typedef CPUMDUMPTYPE *PCPUMDUMPTYPE;
164
165
166/*********************************************************************************************************************************
167* Internal Functions *
168*********************************************************************************************************************************/
169static DECLCALLBACK(int) cpumR3LiveExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uPass);
170static DECLCALLBACK(int) cpumR3SaveExec(PVM pVM, PSSMHANDLE pSSM);
171static DECLCALLBACK(int) cpumR3LoadPrep(PVM pVM, PSSMHANDLE pSSM);
172static DECLCALLBACK(int) cpumR3LoadExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
173static DECLCALLBACK(int) cpumR3LoadDone(PVM pVM, PSSMHANDLE pSSM);
174static DECLCALLBACK(void) cpumR3InfoAll(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
175static DECLCALLBACK(void) cpumR3InfoGuest(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
176static DECLCALLBACK(void) cpumR3InfoGuestHwvirt(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
177static DECLCALLBACK(void) cpumR3InfoGuestInstr(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
178static DECLCALLBACK(void) cpumR3InfoHyper(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
179static DECLCALLBACK(void) cpumR3InfoHost(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
180
181
182/*********************************************************************************************************************************
183* Global Variables *
184*********************************************************************************************************************************/
185/** Saved state field descriptors for CPUMCTX. */
186static const SSMFIELD g_aCpumCtxFields[] =
187{
188 SSMFIELD_ENTRY( CPUMCTX, rdi),
189 SSMFIELD_ENTRY( CPUMCTX, rsi),
190 SSMFIELD_ENTRY( CPUMCTX, rbp),
191 SSMFIELD_ENTRY( CPUMCTX, rax),
192 SSMFIELD_ENTRY( CPUMCTX, rbx),
193 SSMFIELD_ENTRY( CPUMCTX, rdx),
194 SSMFIELD_ENTRY( CPUMCTX, rcx),
195 SSMFIELD_ENTRY( CPUMCTX, rsp),
196 SSMFIELD_ENTRY( CPUMCTX, rflags),
197 SSMFIELD_ENTRY( CPUMCTX, rip),
198 SSMFIELD_ENTRY( CPUMCTX, r8),
199 SSMFIELD_ENTRY( CPUMCTX, r9),
200 SSMFIELD_ENTRY( CPUMCTX, r10),
201 SSMFIELD_ENTRY( CPUMCTX, r11),
202 SSMFIELD_ENTRY( CPUMCTX, r12),
203 SSMFIELD_ENTRY( CPUMCTX, r13),
204 SSMFIELD_ENTRY( CPUMCTX, r14),
205 SSMFIELD_ENTRY( CPUMCTX, r15),
206 SSMFIELD_ENTRY( CPUMCTX, es.Sel),
207 SSMFIELD_ENTRY( CPUMCTX, es.ValidSel),
208 SSMFIELD_ENTRY( CPUMCTX, es.fFlags),
209 SSMFIELD_ENTRY( CPUMCTX, es.u64Base),
210 SSMFIELD_ENTRY( CPUMCTX, es.u32Limit),
211 SSMFIELD_ENTRY( CPUMCTX, es.Attr),
212 SSMFIELD_ENTRY( CPUMCTX, cs.Sel),
213 SSMFIELD_ENTRY( CPUMCTX, cs.ValidSel),
214 SSMFIELD_ENTRY( CPUMCTX, cs.fFlags),
215 SSMFIELD_ENTRY( CPUMCTX, cs.u64Base),
216 SSMFIELD_ENTRY( CPUMCTX, cs.u32Limit),
217 SSMFIELD_ENTRY( CPUMCTX, cs.Attr),
218 SSMFIELD_ENTRY( CPUMCTX, ss.Sel),
219 SSMFIELD_ENTRY( CPUMCTX, ss.ValidSel),
220 SSMFIELD_ENTRY( CPUMCTX, ss.fFlags),
221 SSMFIELD_ENTRY( CPUMCTX, ss.u64Base),
222 SSMFIELD_ENTRY( CPUMCTX, ss.u32Limit),
223 SSMFIELD_ENTRY( CPUMCTX, ss.Attr),
224 SSMFIELD_ENTRY( CPUMCTX, ds.Sel),
225 SSMFIELD_ENTRY( CPUMCTX, ds.ValidSel),
226 SSMFIELD_ENTRY( CPUMCTX, ds.fFlags),
227 SSMFIELD_ENTRY( CPUMCTX, ds.u64Base),
228 SSMFIELD_ENTRY( CPUMCTX, ds.u32Limit),
229 SSMFIELD_ENTRY( CPUMCTX, ds.Attr),
230 SSMFIELD_ENTRY( CPUMCTX, fs.Sel),
231 SSMFIELD_ENTRY( CPUMCTX, fs.ValidSel),
232 SSMFIELD_ENTRY( CPUMCTX, fs.fFlags),
233 SSMFIELD_ENTRY( CPUMCTX, fs.u64Base),
234 SSMFIELD_ENTRY( CPUMCTX, fs.u32Limit),
235 SSMFIELD_ENTRY( CPUMCTX, fs.Attr),
236 SSMFIELD_ENTRY( CPUMCTX, gs.Sel),
237 SSMFIELD_ENTRY( CPUMCTX, gs.ValidSel),
238 SSMFIELD_ENTRY( CPUMCTX, gs.fFlags),
239 SSMFIELD_ENTRY( CPUMCTX, gs.u64Base),
240 SSMFIELD_ENTRY( CPUMCTX, gs.u32Limit),
241 SSMFIELD_ENTRY( CPUMCTX, gs.Attr),
242 SSMFIELD_ENTRY( CPUMCTX, cr0),
243 SSMFIELD_ENTRY( CPUMCTX, cr2),
244 SSMFIELD_ENTRY( CPUMCTX, cr3),
245 SSMFIELD_ENTRY( CPUMCTX, cr4),
246 SSMFIELD_ENTRY( CPUMCTX, dr[0]),
247 SSMFIELD_ENTRY( CPUMCTX, dr[1]),
248 SSMFIELD_ENTRY( CPUMCTX, dr[2]),
249 SSMFIELD_ENTRY( CPUMCTX, dr[3]),
250 SSMFIELD_ENTRY( CPUMCTX, dr[6]),
251 SSMFIELD_ENTRY( CPUMCTX, dr[7]),
252 SSMFIELD_ENTRY( CPUMCTX, gdtr.cbGdt),
253 SSMFIELD_ENTRY( CPUMCTX, gdtr.pGdt),
254 SSMFIELD_ENTRY( CPUMCTX, idtr.cbIdt),
255 SSMFIELD_ENTRY( CPUMCTX, idtr.pIdt),
256 SSMFIELD_ENTRY( CPUMCTX, SysEnter.cs),
257 SSMFIELD_ENTRY( CPUMCTX, SysEnter.eip),
258 SSMFIELD_ENTRY( CPUMCTX, SysEnter.esp),
259 SSMFIELD_ENTRY( CPUMCTX, msrEFER),
260 SSMFIELD_ENTRY( CPUMCTX, msrSTAR),
261 SSMFIELD_ENTRY( CPUMCTX, msrPAT),
262 SSMFIELD_ENTRY( CPUMCTX, msrLSTAR),
263 SSMFIELD_ENTRY( CPUMCTX, msrCSTAR),
264 SSMFIELD_ENTRY( CPUMCTX, msrSFMASK),
265 SSMFIELD_ENTRY( CPUMCTX, msrKERNELGSBASE),
266 SSMFIELD_ENTRY( CPUMCTX, ldtr.Sel),
267 SSMFIELD_ENTRY( CPUMCTX, ldtr.ValidSel),
268 SSMFIELD_ENTRY( CPUMCTX, ldtr.fFlags),
269 SSMFIELD_ENTRY( CPUMCTX, ldtr.u64Base),
270 SSMFIELD_ENTRY( CPUMCTX, ldtr.u32Limit),
271 SSMFIELD_ENTRY( CPUMCTX, ldtr.Attr),
272 SSMFIELD_ENTRY( CPUMCTX, tr.Sel),
273 SSMFIELD_ENTRY( CPUMCTX, tr.ValidSel),
274 SSMFIELD_ENTRY( CPUMCTX, tr.fFlags),
275 SSMFIELD_ENTRY( CPUMCTX, tr.u64Base),
276 SSMFIELD_ENTRY( CPUMCTX, tr.u32Limit),
277 SSMFIELD_ENTRY( CPUMCTX, tr.Attr),
278 SSMFIELD_ENTRY_VER( CPUMCTX, aXcr[0], CPUM_SAVED_STATE_VERSION_XSAVE),
279 SSMFIELD_ENTRY_VER( CPUMCTX, aXcr[1], CPUM_SAVED_STATE_VERSION_XSAVE),
280 SSMFIELD_ENTRY_VER( CPUMCTX, fXStateMask, CPUM_SAVED_STATE_VERSION_XSAVE),
281 SSMFIELD_ENTRY_TERM()
282};
283
284/** Saved state field descriptors for SVM nested hardware-virtualization
285 * Host State. */
286static const SSMFIELD g_aSvmHwvirtHostState[] =
287{
288 SSMFIELD_ENTRY( SVMHOSTSTATE, uEferMsr),
289 SSMFIELD_ENTRY( SVMHOSTSTATE, uCr0),
290 SSMFIELD_ENTRY( SVMHOSTSTATE, uCr4),
291 SSMFIELD_ENTRY( SVMHOSTSTATE, uCr3),
292 SSMFIELD_ENTRY( SVMHOSTSTATE, uRip),
293 SSMFIELD_ENTRY( SVMHOSTSTATE, uRsp),
294 SSMFIELD_ENTRY( SVMHOSTSTATE, uRax),
295 SSMFIELD_ENTRY( SVMHOSTSTATE, rflags),
296 SSMFIELD_ENTRY( SVMHOSTSTATE, es.Sel),
297 SSMFIELD_ENTRY( SVMHOSTSTATE, es.ValidSel),
298 SSMFIELD_ENTRY( SVMHOSTSTATE, es.fFlags),
299 SSMFIELD_ENTRY( SVMHOSTSTATE, es.u64Base),
300 SSMFIELD_ENTRY( SVMHOSTSTATE, es.u32Limit),
301 SSMFIELD_ENTRY( SVMHOSTSTATE, es.Attr),
302 SSMFIELD_ENTRY( SVMHOSTSTATE, cs.Sel),
303 SSMFIELD_ENTRY( SVMHOSTSTATE, cs.ValidSel),
304 SSMFIELD_ENTRY( SVMHOSTSTATE, cs.fFlags),
305 SSMFIELD_ENTRY( SVMHOSTSTATE, cs.u64Base),
306 SSMFIELD_ENTRY( SVMHOSTSTATE, cs.u32Limit),
307 SSMFIELD_ENTRY( SVMHOSTSTATE, cs.Attr),
308 SSMFIELD_ENTRY( SVMHOSTSTATE, ss.Sel),
309 SSMFIELD_ENTRY( SVMHOSTSTATE, ss.ValidSel),
310 SSMFIELD_ENTRY( SVMHOSTSTATE, ss.fFlags),
311 SSMFIELD_ENTRY( SVMHOSTSTATE, ss.u64Base),
312 SSMFIELD_ENTRY( SVMHOSTSTATE, ss.u32Limit),
313 SSMFIELD_ENTRY( SVMHOSTSTATE, ss.Attr),
314 SSMFIELD_ENTRY( SVMHOSTSTATE, ds.Sel),
315 SSMFIELD_ENTRY( SVMHOSTSTATE, ds.ValidSel),
316 SSMFIELD_ENTRY( SVMHOSTSTATE, ds.fFlags),
317 SSMFIELD_ENTRY( SVMHOSTSTATE, ds.u64Base),
318 SSMFIELD_ENTRY( SVMHOSTSTATE, ds.u32Limit),
319 SSMFIELD_ENTRY( SVMHOSTSTATE, ds.Attr),
320 SSMFIELD_ENTRY( SVMHOSTSTATE, gdtr.cbGdt),
321 SSMFIELD_ENTRY( SVMHOSTSTATE, gdtr.pGdt),
322 SSMFIELD_ENTRY( SVMHOSTSTATE, idtr.cbIdt),
323 SSMFIELD_ENTRY( SVMHOSTSTATE, idtr.pIdt),
324 SSMFIELD_ENTRY_IGNORE(SVMHOSTSTATE, abPadding),
325 SSMFIELD_ENTRY_TERM()
326};
327
328/** Saved state field descriptors for CPUMCTX. */
329static const SSMFIELD g_aCpumX87Fields[] =
330{
331 SSMFIELD_ENTRY( X86FXSTATE, FCW),
332 SSMFIELD_ENTRY( X86FXSTATE, FSW),
333 SSMFIELD_ENTRY( X86FXSTATE, FTW),
334 SSMFIELD_ENTRY( X86FXSTATE, FOP),
335 SSMFIELD_ENTRY( X86FXSTATE, FPUIP),
336 SSMFIELD_ENTRY( X86FXSTATE, CS),
337 SSMFIELD_ENTRY( X86FXSTATE, Rsrvd1),
338 SSMFIELD_ENTRY( X86FXSTATE, FPUDP),
339 SSMFIELD_ENTRY( X86FXSTATE, DS),
340 SSMFIELD_ENTRY( X86FXSTATE, Rsrvd2),
341 SSMFIELD_ENTRY( X86FXSTATE, MXCSR),
342 SSMFIELD_ENTRY( X86FXSTATE, MXCSR_MASK),
343 SSMFIELD_ENTRY( X86FXSTATE, aRegs[0]),
344 SSMFIELD_ENTRY( X86FXSTATE, aRegs[1]),
345 SSMFIELD_ENTRY( X86FXSTATE, aRegs[2]),
346 SSMFIELD_ENTRY( X86FXSTATE, aRegs[3]),
347 SSMFIELD_ENTRY( X86FXSTATE, aRegs[4]),
348 SSMFIELD_ENTRY( X86FXSTATE, aRegs[5]),
349 SSMFIELD_ENTRY( X86FXSTATE, aRegs[6]),
350 SSMFIELD_ENTRY( X86FXSTATE, aRegs[7]),
351 SSMFIELD_ENTRY( X86FXSTATE, aXMM[0]),
352 SSMFIELD_ENTRY( X86FXSTATE, aXMM[1]),
353 SSMFIELD_ENTRY( X86FXSTATE, aXMM[2]),
354 SSMFIELD_ENTRY( X86FXSTATE, aXMM[3]),
355 SSMFIELD_ENTRY( X86FXSTATE, aXMM[4]),
356 SSMFIELD_ENTRY( X86FXSTATE, aXMM[5]),
357 SSMFIELD_ENTRY( X86FXSTATE, aXMM[6]),
358 SSMFIELD_ENTRY( X86FXSTATE, aXMM[7]),
359 SSMFIELD_ENTRY( X86FXSTATE, aXMM[8]),
360 SSMFIELD_ENTRY( X86FXSTATE, aXMM[9]),
361 SSMFIELD_ENTRY( X86FXSTATE, aXMM[10]),
362 SSMFIELD_ENTRY( X86FXSTATE, aXMM[11]),
363 SSMFIELD_ENTRY( X86FXSTATE, aXMM[12]),
364 SSMFIELD_ENTRY( X86FXSTATE, aXMM[13]),
365 SSMFIELD_ENTRY( X86FXSTATE, aXMM[14]),
366 SSMFIELD_ENTRY( X86FXSTATE, aXMM[15]),
367 SSMFIELD_ENTRY_VER( X86FXSTATE, au32RsrvdForSoftware[0], CPUM_SAVED_STATE_VERSION_XSAVE), /* 32-bit/64-bit hack */
368 SSMFIELD_ENTRY_TERM()
369};
370
371/** Saved state field descriptors for X86XSAVEHDR. */
372static const SSMFIELD g_aCpumXSaveHdrFields[] =
373{
374 SSMFIELD_ENTRY( X86XSAVEHDR, bmXState),
375 SSMFIELD_ENTRY_TERM()
376};
377
378/** Saved state field descriptors for X86XSAVEYMMHI. */
379static const SSMFIELD g_aCpumYmmHiFields[] =
380{
381 SSMFIELD_ENTRY( X86XSAVEYMMHI, aYmmHi[0]),
382 SSMFIELD_ENTRY( X86XSAVEYMMHI, aYmmHi[1]),
383 SSMFIELD_ENTRY( X86XSAVEYMMHI, aYmmHi[2]),
384 SSMFIELD_ENTRY( X86XSAVEYMMHI, aYmmHi[3]),
385 SSMFIELD_ENTRY( X86XSAVEYMMHI, aYmmHi[4]),
386 SSMFIELD_ENTRY( X86XSAVEYMMHI, aYmmHi[5]),
387 SSMFIELD_ENTRY( X86XSAVEYMMHI, aYmmHi[6]),
388 SSMFIELD_ENTRY( X86XSAVEYMMHI, aYmmHi[7]),
389 SSMFIELD_ENTRY( X86XSAVEYMMHI, aYmmHi[8]),
390 SSMFIELD_ENTRY( X86XSAVEYMMHI, aYmmHi[9]),
391 SSMFIELD_ENTRY( X86XSAVEYMMHI, aYmmHi[10]),
392 SSMFIELD_ENTRY( X86XSAVEYMMHI, aYmmHi[11]),
393 SSMFIELD_ENTRY( X86XSAVEYMMHI, aYmmHi[12]),
394 SSMFIELD_ENTRY( X86XSAVEYMMHI, aYmmHi[13]),
395 SSMFIELD_ENTRY( X86XSAVEYMMHI, aYmmHi[14]),
396 SSMFIELD_ENTRY( X86XSAVEYMMHI, aYmmHi[15]),
397 SSMFIELD_ENTRY_TERM()
398};
399
400/** Saved state field descriptors for X86XSAVEBNDREGS. */
401static const SSMFIELD g_aCpumBndRegsFields[] =
402{
403 SSMFIELD_ENTRY( X86XSAVEBNDREGS, aRegs[0]),
404 SSMFIELD_ENTRY( X86XSAVEBNDREGS, aRegs[1]),
405 SSMFIELD_ENTRY( X86XSAVEBNDREGS, aRegs[2]),
406 SSMFIELD_ENTRY( X86XSAVEBNDREGS, aRegs[3]),
407 SSMFIELD_ENTRY_TERM()
408};
409
410/** Saved state field descriptors for X86XSAVEBNDCFG. */
411static const SSMFIELD g_aCpumBndCfgFields[] =
412{
413 SSMFIELD_ENTRY( X86XSAVEBNDCFG, fConfig),
414 SSMFIELD_ENTRY( X86XSAVEBNDCFG, fStatus),
415 SSMFIELD_ENTRY_TERM()
416};
417
418#if 0 /** @todo */
419/** Saved state field descriptors for X86XSAVEOPMASK. */
420static const SSMFIELD g_aCpumOpmaskFields[] =
421{
422 SSMFIELD_ENTRY( X86XSAVEOPMASK, aKRegs[0]),
423 SSMFIELD_ENTRY( X86XSAVEOPMASK, aKRegs[1]),
424 SSMFIELD_ENTRY( X86XSAVEOPMASK, aKRegs[2]),
425 SSMFIELD_ENTRY( X86XSAVEOPMASK, aKRegs[3]),
426 SSMFIELD_ENTRY( X86XSAVEOPMASK, aKRegs[4]),
427 SSMFIELD_ENTRY( X86XSAVEOPMASK, aKRegs[5]),
428 SSMFIELD_ENTRY( X86XSAVEOPMASK, aKRegs[6]),
429 SSMFIELD_ENTRY( X86XSAVEOPMASK, aKRegs[7]),
430 SSMFIELD_ENTRY_TERM()
431};
432#endif
433
434/** Saved state field descriptors for X86XSAVEZMMHI256. */
435static const SSMFIELD g_aCpumZmmHi256Fields[] =
436{
437 SSMFIELD_ENTRY( X86XSAVEZMMHI256, aHi256Regs[0]),
438 SSMFIELD_ENTRY( X86XSAVEZMMHI256, aHi256Regs[1]),
439 SSMFIELD_ENTRY( X86XSAVEZMMHI256, aHi256Regs[2]),
440 SSMFIELD_ENTRY( X86XSAVEZMMHI256, aHi256Regs[3]),
441 SSMFIELD_ENTRY( X86XSAVEZMMHI256, aHi256Regs[4]),
442 SSMFIELD_ENTRY( X86XSAVEZMMHI256, aHi256Regs[5]),
443 SSMFIELD_ENTRY( X86XSAVEZMMHI256, aHi256Regs[6]),
444 SSMFIELD_ENTRY( X86XSAVEZMMHI256, aHi256Regs[7]),
445 SSMFIELD_ENTRY( X86XSAVEZMMHI256, aHi256Regs[8]),
446 SSMFIELD_ENTRY( X86XSAVEZMMHI256, aHi256Regs[9]),
447 SSMFIELD_ENTRY( X86XSAVEZMMHI256, aHi256Regs[10]),
448 SSMFIELD_ENTRY( X86XSAVEZMMHI256, aHi256Regs[11]),
449 SSMFIELD_ENTRY( X86XSAVEZMMHI256, aHi256Regs[12]),
450 SSMFIELD_ENTRY( X86XSAVEZMMHI256, aHi256Regs[13]),
451 SSMFIELD_ENTRY( X86XSAVEZMMHI256, aHi256Regs[14]),
452 SSMFIELD_ENTRY( X86XSAVEZMMHI256, aHi256Regs[15]),
453 SSMFIELD_ENTRY_TERM()
454};
455
456/** Saved state field descriptors for X86XSAVEZMM16HI. */
457static const SSMFIELD g_aCpumZmm16HiFields[] =
458{
459 SSMFIELD_ENTRY( X86XSAVEZMM16HI, aRegs[0]),
460 SSMFIELD_ENTRY( X86XSAVEZMM16HI, aRegs[1]),
461 SSMFIELD_ENTRY( X86XSAVEZMM16HI, aRegs[2]),
462 SSMFIELD_ENTRY( X86XSAVEZMM16HI, aRegs[3]),
463 SSMFIELD_ENTRY( X86XSAVEZMM16HI, aRegs[4]),
464 SSMFIELD_ENTRY( X86XSAVEZMM16HI, aRegs[5]),
465 SSMFIELD_ENTRY( X86XSAVEZMM16HI, aRegs[6]),
466 SSMFIELD_ENTRY( X86XSAVEZMM16HI, aRegs[7]),
467 SSMFIELD_ENTRY( X86XSAVEZMM16HI, aRegs[8]),
468 SSMFIELD_ENTRY( X86XSAVEZMM16HI, aRegs[9]),
469 SSMFIELD_ENTRY( X86XSAVEZMM16HI, aRegs[10]),
470 SSMFIELD_ENTRY( X86XSAVEZMM16HI, aRegs[11]),
471 SSMFIELD_ENTRY( X86XSAVEZMM16HI, aRegs[12]),
472 SSMFIELD_ENTRY( X86XSAVEZMM16HI, aRegs[13]),
473 SSMFIELD_ENTRY( X86XSAVEZMM16HI, aRegs[14]),
474 SSMFIELD_ENTRY( X86XSAVEZMM16HI, aRegs[15]),
475 SSMFIELD_ENTRY_TERM()
476};
477
478
479
480/** Saved state field descriptors for CPUMCTX in V4.1 before the hidden selector
481 * registeres changed. */
482static const SSMFIELD g_aCpumX87FieldsMem[] =
483{
484 SSMFIELD_ENTRY( X86FXSTATE, FCW),
485 SSMFIELD_ENTRY( X86FXSTATE, FSW),
486 SSMFIELD_ENTRY( X86FXSTATE, FTW),
487 SSMFIELD_ENTRY( X86FXSTATE, FOP),
488 SSMFIELD_ENTRY( X86FXSTATE, FPUIP),
489 SSMFIELD_ENTRY( X86FXSTATE, CS),
490 SSMFIELD_ENTRY( X86FXSTATE, Rsrvd1),
491 SSMFIELD_ENTRY( X86FXSTATE, FPUDP),
492 SSMFIELD_ENTRY( X86FXSTATE, DS),
493 SSMFIELD_ENTRY( X86FXSTATE, Rsrvd2),
494 SSMFIELD_ENTRY( X86FXSTATE, MXCSR),
495 SSMFIELD_ENTRY( X86FXSTATE, MXCSR_MASK),
496 SSMFIELD_ENTRY( X86FXSTATE, aRegs[0]),
497 SSMFIELD_ENTRY( X86FXSTATE, aRegs[1]),
498 SSMFIELD_ENTRY( X86FXSTATE, aRegs[2]),
499 SSMFIELD_ENTRY( X86FXSTATE, aRegs[3]),
500 SSMFIELD_ENTRY( X86FXSTATE, aRegs[4]),
501 SSMFIELD_ENTRY( X86FXSTATE, aRegs[5]),
502 SSMFIELD_ENTRY( X86FXSTATE, aRegs[6]),
503 SSMFIELD_ENTRY( X86FXSTATE, aRegs[7]),
504 SSMFIELD_ENTRY( X86FXSTATE, aXMM[0]),
505 SSMFIELD_ENTRY( X86FXSTATE, aXMM[1]),
506 SSMFIELD_ENTRY( X86FXSTATE, aXMM[2]),
507 SSMFIELD_ENTRY( X86FXSTATE, aXMM[3]),
508 SSMFIELD_ENTRY( X86FXSTATE, aXMM[4]),
509 SSMFIELD_ENTRY( X86FXSTATE, aXMM[5]),
510 SSMFIELD_ENTRY( X86FXSTATE, aXMM[6]),
511 SSMFIELD_ENTRY( X86FXSTATE, aXMM[7]),
512 SSMFIELD_ENTRY( X86FXSTATE, aXMM[8]),
513 SSMFIELD_ENTRY( X86FXSTATE, aXMM[9]),
514 SSMFIELD_ENTRY( X86FXSTATE, aXMM[10]),
515 SSMFIELD_ENTRY( X86FXSTATE, aXMM[11]),
516 SSMFIELD_ENTRY( X86FXSTATE, aXMM[12]),
517 SSMFIELD_ENTRY( X86FXSTATE, aXMM[13]),
518 SSMFIELD_ENTRY( X86FXSTATE, aXMM[14]),
519 SSMFIELD_ENTRY( X86FXSTATE, aXMM[15]),
520 SSMFIELD_ENTRY_IGNORE( X86FXSTATE, au32RsrvdRest),
521 SSMFIELD_ENTRY_IGNORE( X86FXSTATE, au32RsrvdForSoftware),
522};
523
524/** Saved state field descriptors for CPUMCTX in V4.1 before the hidden selector
525 * registeres changed. */
526static const SSMFIELD g_aCpumCtxFieldsMem[] =
527{
528 SSMFIELD_ENTRY( CPUMCTX, rdi),
529 SSMFIELD_ENTRY( CPUMCTX, rsi),
530 SSMFIELD_ENTRY( CPUMCTX, rbp),
531 SSMFIELD_ENTRY( CPUMCTX, rax),
532 SSMFIELD_ENTRY( CPUMCTX, rbx),
533 SSMFIELD_ENTRY( CPUMCTX, rdx),
534 SSMFIELD_ENTRY( CPUMCTX, rcx),
535 SSMFIELD_ENTRY( CPUMCTX, rsp),
536 SSMFIELD_ENTRY_OLD( lss_esp, sizeof(uint32_t)),
537 SSMFIELD_ENTRY( CPUMCTX, ss.Sel),
538 SSMFIELD_ENTRY_OLD( ssPadding, sizeof(uint16_t)),
539 SSMFIELD_ENTRY( CPUMCTX, gs.Sel),
540 SSMFIELD_ENTRY_OLD( gsPadding, sizeof(uint16_t)),
541 SSMFIELD_ENTRY( CPUMCTX, fs.Sel),
542 SSMFIELD_ENTRY_OLD( fsPadding, sizeof(uint16_t)),
543 SSMFIELD_ENTRY( CPUMCTX, es.Sel),
544 SSMFIELD_ENTRY_OLD( esPadding, sizeof(uint16_t)),
545 SSMFIELD_ENTRY( CPUMCTX, ds.Sel),
546 SSMFIELD_ENTRY_OLD( dsPadding, sizeof(uint16_t)),
547 SSMFIELD_ENTRY( CPUMCTX, cs.Sel),
548 SSMFIELD_ENTRY_OLD( csPadding, sizeof(uint16_t)*3),
549 SSMFIELD_ENTRY( CPUMCTX, rflags),
550 SSMFIELD_ENTRY( CPUMCTX, rip),
551 SSMFIELD_ENTRY( CPUMCTX, r8),
552 SSMFIELD_ENTRY( CPUMCTX, r9),
553 SSMFIELD_ENTRY( CPUMCTX, r10),
554 SSMFIELD_ENTRY( CPUMCTX, r11),
555 SSMFIELD_ENTRY( CPUMCTX, r12),
556 SSMFIELD_ENTRY( CPUMCTX, r13),
557 SSMFIELD_ENTRY( CPUMCTX, r14),
558 SSMFIELD_ENTRY( CPUMCTX, r15),
559 SSMFIELD_ENTRY( CPUMCTX, es.u64Base),
560 SSMFIELD_ENTRY( CPUMCTX, es.u32Limit),
561 SSMFIELD_ENTRY( CPUMCTX, es.Attr),
562 SSMFIELD_ENTRY( CPUMCTX, cs.u64Base),
563 SSMFIELD_ENTRY( CPUMCTX, cs.u32Limit),
564 SSMFIELD_ENTRY( CPUMCTX, cs.Attr),
565 SSMFIELD_ENTRY( CPUMCTX, ss.u64Base),
566 SSMFIELD_ENTRY( CPUMCTX, ss.u32Limit),
567 SSMFIELD_ENTRY( CPUMCTX, ss.Attr),
568 SSMFIELD_ENTRY( CPUMCTX, ds.u64Base),
569 SSMFIELD_ENTRY( CPUMCTX, ds.u32Limit),
570 SSMFIELD_ENTRY( CPUMCTX, ds.Attr),
571 SSMFIELD_ENTRY( CPUMCTX, fs.u64Base),
572 SSMFIELD_ENTRY( CPUMCTX, fs.u32Limit),
573 SSMFIELD_ENTRY( CPUMCTX, fs.Attr),
574 SSMFIELD_ENTRY( CPUMCTX, gs.u64Base),
575 SSMFIELD_ENTRY( CPUMCTX, gs.u32Limit),
576 SSMFIELD_ENTRY( CPUMCTX, gs.Attr),
577 SSMFIELD_ENTRY( CPUMCTX, cr0),
578 SSMFIELD_ENTRY( CPUMCTX, cr2),
579 SSMFIELD_ENTRY( CPUMCTX, cr3),
580 SSMFIELD_ENTRY( CPUMCTX, cr4),
581 SSMFIELD_ENTRY( CPUMCTX, dr[0]),
582 SSMFIELD_ENTRY( CPUMCTX, dr[1]),
583 SSMFIELD_ENTRY( CPUMCTX, dr[2]),
584 SSMFIELD_ENTRY( CPUMCTX, dr[3]),
585 SSMFIELD_ENTRY_OLD( dr[4], sizeof(uint64_t)),
586 SSMFIELD_ENTRY_OLD( dr[5], sizeof(uint64_t)),
587 SSMFIELD_ENTRY( CPUMCTX, dr[6]),
588 SSMFIELD_ENTRY( CPUMCTX, dr[7]),
589 SSMFIELD_ENTRY( CPUMCTX, gdtr.cbGdt),
590 SSMFIELD_ENTRY( CPUMCTX, gdtr.pGdt),
591 SSMFIELD_ENTRY_OLD( gdtrPadding, sizeof(uint16_t)),
592 SSMFIELD_ENTRY( CPUMCTX, idtr.cbIdt),
593 SSMFIELD_ENTRY( CPUMCTX, idtr.pIdt),
594 SSMFIELD_ENTRY_OLD( idtrPadding, sizeof(uint16_t)),
595 SSMFIELD_ENTRY( CPUMCTX, ldtr.Sel),
596 SSMFIELD_ENTRY_OLD( ldtrPadding, sizeof(uint16_t)),
597 SSMFIELD_ENTRY( CPUMCTX, tr.Sel),
598 SSMFIELD_ENTRY_OLD( trPadding, sizeof(uint16_t)),
599 SSMFIELD_ENTRY( CPUMCTX, SysEnter.cs),
600 SSMFIELD_ENTRY( CPUMCTX, SysEnter.eip),
601 SSMFIELD_ENTRY( CPUMCTX, SysEnter.esp),
602 SSMFIELD_ENTRY( CPUMCTX, msrEFER),
603 SSMFIELD_ENTRY( CPUMCTX, msrSTAR),
604 SSMFIELD_ENTRY( CPUMCTX, msrPAT),
605 SSMFIELD_ENTRY( CPUMCTX, msrLSTAR),
606 SSMFIELD_ENTRY( CPUMCTX, msrCSTAR),
607 SSMFIELD_ENTRY( CPUMCTX, msrSFMASK),
608 SSMFIELD_ENTRY( CPUMCTX, msrKERNELGSBASE),
609 SSMFIELD_ENTRY( CPUMCTX, ldtr.u64Base),
610 SSMFIELD_ENTRY( CPUMCTX, ldtr.u32Limit),
611 SSMFIELD_ENTRY( CPUMCTX, ldtr.Attr),
612 SSMFIELD_ENTRY( CPUMCTX, tr.u64Base),
613 SSMFIELD_ENTRY( CPUMCTX, tr.u32Limit),
614 SSMFIELD_ENTRY( CPUMCTX, tr.Attr),
615 SSMFIELD_ENTRY_TERM()
616};
617
618/** Saved state field descriptors for CPUMCTX_VER1_6. */
619static const SSMFIELD g_aCpumX87FieldsV16[] =
620{
621 SSMFIELD_ENTRY( X86FXSTATE, FCW),
622 SSMFIELD_ENTRY( X86FXSTATE, FSW),
623 SSMFIELD_ENTRY( X86FXSTATE, FTW),
624 SSMFIELD_ENTRY( X86FXSTATE, FOP),
625 SSMFIELD_ENTRY( X86FXSTATE, FPUIP),
626 SSMFIELD_ENTRY( X86FXSTATE, CS),
627 SSMFIELD_ENTRY( X86FXSTATE, Rsrvd1),
628 SSMFIELD_ENTRY( X86FXSTATE, FPUDP),
629 SSMFIELD_ENTRY( X86FXSTATE, DS),
630 SSMFIELD_ENTRY( X86FXSTATE, Rsrvd2),
631 SSMFIELD_ENTRY( X86FXSTATE, MXCSR),
632 SSMFIELD_ENTRY( X86FXSTATE, MXCSR_MASK),
633 SSMFIELD_ENTRY( X86FXSTATE, aRegs[0]),
634 SSMFIELD_ENTRY( X86FXSTATE, aRegs[1]),
635 SSMFIELD_ENTRY( X86FXSTATE, aRegs[2]),
636 SSMFIELD_ENTRY( X86FXSTATE, aRegs[3]),
637 SSMFIELD_ENTRY( X86FXSTATE, aRegs[4]),
638 SSMFIELD_ENTRY( X86FXSTATE, aRegs[5]),
639 SSMFIELD_ENTRY( X86FXSTATE, aRegs[6]),
640 SSMFIELD_ENTRY( X86FXSTATE, aRegs[7]),
641 SSMFIELD_ENTRY( X86FXSTATE, aXMM[0]),
642 SSMFIELD_ENTRY( X86FXSTATE, aXMM[1]),
643 SSMFIELD_ENTRY( X86FXSTATE, aXMM[2]),
644 SSMFIELD_ENTRY( X86FXSTATE, aXMM[3]),
645 SSMFIELD_ENTRY( X86FXSTATE, aXMM[4]),
646 SSMFIELD_ENTRY( X86FXSTATE, aXMM[5]),
647 SSMFIELD_ENTRY( X86FXSTATE, aXMM[6]),
648 SSMFIELD_ENTRY( X86FXSTATE, aXMM[7]),
649 SSMFIELD_ENTRY( X86FXSTATE, aXMM[8]),
650 SSMFIELD_ENTRY( X86FXSTATE, aXMM[9]),
651 SSMFIELD_ENTRY( X86FXSTATE, aXMM[10]),
652 SSMFIELD_ENTRY( X86FXSTATE, aXMM[11]),
653 SSMFIELD_ENTRY( X86FXSTATE, aXMM[12]),
654 SSMFIELD_ENTRY( X86FXSTATE, aXMM[13]),
655 SSMFIELD_ENTRY( X86FXSTATE, aXMM[14]),
656 SSMFIELD_ENTRY( X86FXSTATE, aXMM[15]),
657 SSMFIELD_ENTRY_IGNORE( X86FXSTATE, au32RsrvdRest),
658 SSMFIELD_ENTRY_IGNORE( X86FXSTATE, au32RsrvdForSoftware),
659 SSMFIELD_ENTRY_TERM()
660};
661
662/** Saved state field descriptors for CPUMCTX_VER1_6. */
663static const SSMFIELD g_aCpumCtxFieldsV16[] =
664{
665 SSMFIELD_ENTRY( CPUMCTX, rdi),
666 SSMFIELD_ENTRY( CPUMCTX, rsi),
667 SSMFIELD_ENTRY( CPUMCTX, rbp),
668 SSMFIELD_ENTRY( CPUMCTX, rax),
669 SSMFIELD_ENTRY( CPUMCTX, rbx),
670 SSMFIELD_ENTRY( CPUMCTX, rdx),
671 SSMFIELD_ENTRY( CPUMCTX, rcx),
672 SSMFIELD_ENTRY_U32_ZX_U64( CPUMCTX, rsp),
673 SSMFIELD_ENTRY( CPUMCTX, ss.Sel),
674 SSMFIELD_ENTRY_OLD( ssPadding, sizeof(uint16_t)),
675 SSMFIELD_ENTRY_OLD( CPUMCTX, sizeof(uint64_t) /*rsp_notused*/),
676 SSMFIELD_ENTRY( CPUMCTX, gs.Sel),
677 SSMFIELD_ENTRY_OLD( gsPadding, sizeof(uint16_t)),
678 SSMFIELD_ENTRY( CPUMCTX, fs.Sel),
679 SSMFIELD_ENTRY_OLD( fsPadding, sizeof(uint16_t)),
680 SSMFIELD_ENTRY( CPUMCTX, es.Sel),
681 SSMFIELD_ENTRY_OLD( esPadding, sizeof(uint16_t)),
682 SSMFIELD_ENTRY( CPUMCTX, ds.Sel),
683 SSMFIELD_ENTRY_OLD( dsPadding, sizeof(uint16_t)),
684 SSMFIELD_ENTRY( CPUMCTX, cs.Sel),
685 SSMFIELD_ENTRY_OLD( csPadding, sizeof(uint16_t)*3),
686 SSMFIELD_ENTRY( CPUMCTX, rflags),
687 SSMFIELD_ENTRY( CPUMCTX, rip),
688 SSMFIELD_ENTRY( CPUMCTX, r8),
689 SSMFIELD_ENTRY( CPUMCTX, r9),
690 SSMFIELD_ENTRY( CPUMCTX, r10),
691 SSMFIELD_ENTRY( CPUMCTX, r11),
692 SSMFIELD_ENTRY( CPUMCTX, r12),
693 SSMFIELD_ENTRY( CPUMCTX, r13),
694 SSMFIELD_ENTRY( CPUMCTX, r14),
695 SSMFIELD_ENTRY( CPUMCTX, r15),
696 SSMFIELD_ENTRY_U32_ZX_U64( CPUMCTX, es.u64Base),
697 SSMFIELD_ENTRY( CPUMCTX, es.u32Limit),
698 SSMFIELD_ENTRY( CPUMCTX, es.Attr),
699 SSMFIELD_ENTRY_U32_ZX_U64( CPUMCTX, cs.u64Base),
700 SSMFIELD_ENTRY( CPUMCTX, cs.u32Limit),
701 SSMFIELD_ENTRY( CPUMCTX, cs.Attr),
702 SSMFIELD_ENTRY_U32_ZX_U64( CPUMCTX, ss.u64Base),
703 SSMFIELD_ENTRY( CPUMCTX, ss.u32Limit),
704 SSMFIELD_ENTRY( CPUMCTX, ss.Attr),
705 SSMFIELD_ENTRY_U32_ZX_U64( CPUMCTX, ds.u64Base),
706 SSMFIELD_ENTRY( CPUMCTX, ds.u32Limit),
707 SSMFIELD_ENTRY( CPUMCTX, ds.Attr),
708 SSMFIELD_ENTRY_U32_ZX_U64( CPUMCTX, fs.u64Base),
709 SSMFIELD_ENTRY( CPUMCTX, fs.u32Limit),
710 SSMFIELD_ENTRY( CPUMCTX, fs.Attr),
711 SSMFIELD_ENTRY_U32_ZX_U64( CPUMCTX, gs.u64Base),
712 SSMFIELD_ENTRY( CPUMCTX, gs.u32Limit),
713 SSMFIELD_ENTRY( CPUMCTX, gs.Attr),
714 SSMFIELD_ENTRY( CPUMCTX, cr0),
715 SSMFIELD_ENTRY( CPUMCTX, cr2),
716 SSMFIELD_ENTRY( CPUMCTX, cr3),
717 SSMFIELD_ENTRY( CPUMCTX, cr4),
718 SSMFIELD_ENTRY_OLD( cr8, sizeof(uint64_t)),
719 SSMFIELD_ENTRY( CPUMCTX, dr[0]),
720 SSMFIELD_ENTRY( CPUMCTX, dr[1]),
721 SSMFIELD_ENTRY( CPUMCTX, dr[2]),
722 SSMFIELD_ENTRY( CPUMCTX, dr[3]),
723 SSMFIELD_ENTRY_OLD( dr[4], sizeof(uint64_t)),
724 SSMFIELD_ENTRY_OLD( dr[5], sizeof(uint64_t)),
725 SSMFIELD_ENTRY( CPUMCTX, dr[6]),
726 SSMFIELD_ENTRY( CPUMCTX, dr[7]),
727 SSMFIELD_ENTRY( CPUMCTX, gdtr.cbGdt),
728 SSMFIELD_ENTRY_U32_ZX_U64( CPUMCTX, gdtr.pGdt),
729 SSMFIELD_ENTRY_OLD( gdtrPadding, sizeof(uint16_t)),
730 SSMFIELD_ENTRY_OLD( gdtrPadding64, sizeof(uint64_t)),
731 SSMFIELD_ENTRY( CPUMCTX, idtr.cbIdt),
732 SSMFIELD_ENTRY_U32_ZX_U64( CPUMCTX, idtr.pIdt),
733 SSMFIELD_ENTRY_OLD( idtrPadding, sizeof(uint16_t)),
734 SSMFIELD_ENTRY_OLD( idtrPadding64, sizeof(uint64_t)),
735 SSMFIELD_ENTRY( CPUMCTX, ldtr.Sel),
736 SSMFIELD_ENTRY_OLD( ldtrPadding, sizeof(uint16_t)),
737 SSMFIELD_ENTRY( CPUMCTX, tr.Sel),
738 SSMFIELD_ENTRY_OLD( trPadding, sizeof(uint16_t)),
739 SSMFIELD_ENTRY( CPUMCTX, SysEnter.cs),
740 SSMFIELD_ENTRY( CPUMCTX, SysEnter.eip),
741 SSMFIELD_ENTRY( CPUMCTX, SysEnter.esp),
742 SSMFIELD_ENTRY( CPUMCTX, msrEFER),
743 SSMFIELD_ENTRY( CPUMCTX, msrSTAR),
744 SSMFIELD_ENTRY( CPUMCTX, msrPAT),
745 SSMFIELD_ENTRY( CPUMCTX, msrLSTAR),
746 SSMFIELD_ENTRY( CPUMCTX, msrCSTAR),
747 SSMFIELD_ENTRY( CPUMCTX, msrSFMASK),
748 SSMFIELD_ENTRY_OLD( msrFSBASE, sizeof(uint64_t)),
749 SSMFIELD_ENTRY_OLD( msrGSBASE, sizeof(uint64_t)),
750 SSMFIELD_ENTRY( CPUMCTX, msrKERNELGSBASE),
751 SSMFIELD_ENTRY_U32_ZX_U64( CPUMCTX, ldtr.u64Base),
752 SSMFIELD_ENTRY( CPUMCTX, ldtr.u32Limit),
753 SSMFIELD_ENTRY( CPUMCTX, ldtr.Attr),
754 SSMFIELD_ENTRY_U32_ZX_U64( CPUMCTX, tr.u64Base),
755 SSMFIELD_ENTRY( CPUMCTX, tr.u32Limit),
756 SSMFIELD_ENTRY( CPUMCTX, tr.Attr),
757 SSMFIELD_ENTRY_OLD( padding, sizeof(uint32_t)*2),
758 SSMFIELD_ENTRY_TERM()
759};
760
761
762/**
763 * Checks for partial/leaky FXSAVE/FXRSTOR handling on AMD CPUs.
764 *
765 * AMD K7, K8 and newer AMD CPUs do not save/restore the x87 error pointers
766 * (last instruction pointer, last data pointer, last opcode) except when the ES
767 * bit (Exception Summary) in x87 FSW (FPU Status Word) is set. Thus if we don't
768 * clear these registers there is potential, local FPU leakage from a process
769 * using the FPU to another.
770 *
771 * See AMD Instruction Reference for FXSAVE, FXRSTOR.
772 *
773 * @param pVM The cross context VM structure.
774 */
775static void cpumR3CheckLeakyFpu(PVM pVM)
776{
777 uint32_t u32CpuVersion = ASMCpuId_EAX(1);
778 uint32_t const u32Family = u32CpuVersion >> 8;
779 if ( u32Family >= 6 /* K7 and higher */
780 && ASMIsAmdCpu())
781 {
782 uint32_t cExt = ASMCpuId_EAX(0x80000000);
783 if (ASMIsValidExtRange(cExt))
784 {
785 uint32_t fExtFeaturesEDX = ASMCpuId_EDX(0x80000001);
786 if (fExtFeaturesEDX & X86_CPUID_AMD_FEATURE_EDX_FFXSR)
787 {
788 for (VMCPUID i = 0; i < pVM->cCpus; i++)
789 pVM->aCpus[i].cpum.s.fUseFlags |= CPUM_USE_FFXSR_LEAKY;
790 Log(("CPUM: Host CPU has leaky fxsave/fxrstor behaviour\n"));
791 }
792 }
793 }
794}
795
796
797/**
798 * Frees memory allocated for the SVM hardware virtualization state.
799 *
800 * @param pVM The cross context VM structure.
801 */
802static void cpumR3FreeSvmHwVirtState(PVM pVM)
803{
804 Assert(pVM->cpum.s.GuestFeatures.fSvm);
805 for (VMCPUID i = 0; i < pVM->cCpus; i++)
806 {
807 PVMCPU pVCpu = &pVM->aCpus[i];
808 if (pVCpu->cpum.s.Guest.hwvirt.svm.pVmcbR3)
809 {
810 SUPR3PageFreeEx(pVCpu->cpum.s.Guest.hwvirt.svm.pVmcbR3, SVM_VMCB_PAGES);
811 pVCpu->cpum.s.Guest.hwvirt.svm.pVmcbR3 = NULL;
812 }
813 pVCpu->cpum.s.Guest.hwvirt.svm.HCPhysVmcb = NIL_RTHCPHYS;
814
815 if (pVCpu->cpum.s.Guest.hwvirt.svm.pvMsrBitmapR3)
816 {
817 SUPR3PageFreeEx(pVCpu->cpum.s.Guest.hwvirt.svm.pvMsrBitmapR3, SVM_MSRPM_PAGES);
818 pVCpu->cpum.s.Guest.hwvirt.svm.pvMsrBitmapR3 = NULL;
819 }
820
821 if (pVCpu->cpum.s.Guest.hwvirt.svm.pvIoBitmapR3)
822 {
823 SUPR3PageFreeEx(pVCpu->cpum.s.Guest.hwvirt.svm.pvIoBitmapR3, SVM_IOPM_PAGES);
824 pVCpu->cpum.s.Guest.hwvirt.svm.pvIoBitmapR3 = NULL;
825 }
826 }
827}
828
829
830/**
831 * Allocates memory for the SVM hardware virtualization state.
832 *
833 * @returns VBox status code.
834 * @param pVM The cross context VM structure.
835 */
836static int cpumR3AllocSvmHwVirtState(PVM pVM)
837{
838 Assert(pVM->cpum.s.GuestFeatures.fSvm);
839
840 int rc = VINF_SUCCESS;
841 LogRel(("CPUM: Allocating %u pages for the nested-guest SVM MSR and IO permission bitmaps\n",
842 pVM->cCpus * (SVM_MSRPM_PAGES + SVM_IOPM_PAGES)));
843 for (VMCPUID i = 0; i < pVM->cCpus; i++)
844 {
845 PVMCPU pVCpu = &pVM->aCpus[i];
846 pVCpu->cpum.s.Guest.hwvirt.enmHwvirt = CPUMHWVIRT_SVM;
847
848 /*
849 * Allocate the nested-guest VMCB.
850 */
851 SUPPAGE SupNstGstVmcbPage;
852 RT_ZERO(SupNstGstVmcbPage);
853 SupNstGstVmcbPage.Phys = NIL_RTHCPHYS;
854 Assert(SVM_VMCB_PAGES == 1);
855 Assert(!pVCpu->cpum.s.Guest.hwvirt.svm.pVmcbR3);
856 rc = SUPR3PageAllocEx(SVM_VMCB_PAGES, 0 /* fFlags */, (void **)&pVCpu->cpum.s.Guest.hwvirt.svm.pVmcbR3,
857 &pVCpu->cpum.s.Guest.hwvirt.svm.pVmcbR0, &SupNstGstVmcbPage);
858 if (RT_FAILURE(rc))
859 {
860 Assert(!pVCpu->cpum.s.Guest.hwvirt.svm.pVmcbR3);
861 LogRel(("CPUM%u: Failed to alloc %u pages for the nested-guest's VMCB\n", pVCpu->idCpu, SVM_VMCB_PAGES));
862 break;
863 }
864 pVCpu->cpum.s.Guest.hwvirt.svm.HCPhysVmcb = SupNstGstVmcbPage.Phys;
865
866 /*
867 * Allocate the MSRPM (MSR Permission bitmap).
868 */
869 Assert(!pVCpu->cpum.s.Guest.hwvirt.svm.pvMsrBitmapR3);
870 rc = SUPR3PageAllocEx(SVM_MSRPM_PAGES, 0 /* fFlags */, &pVCpu->cpum.s.Guest.hwvirt.svm.pvMsrBitmapR3,
871 &pVCpu->cpum.s.Guest.hwvirt.svm.pvMsrBitmapR0, NULL /* paPages */);
872 if (RT_FAILURE(rc))
873 {
874 Assert(!pVCpu->cpum.s.Guest.hwvirt.svm.pvMsrBitmapR3);
875 LogRel(("CPUM%u: Failed to alloc %u pages for the nested-guest's MSR permission bitmap\n", pVCpu->idCpu,
876 SVM_MSRPM_PAGES));
877 break;
878 }
879
880 /*
881 * Allocate the IOPM (IO Permission bitmap).
882 */
883 Assert(!pVCpu->cpum.s.Guest.hwvirt.svm.pvIoBitmapR3);
884 rc = SUPR3PageAllocEx(SVM_IOPM_PAGES, 0 /* fFlags */, &pVCpu->cpum.s.Guest.hwvirt.svm.pvIoBitmapR3,
885 &pVCpu->cpum.s.Guest.hwvirt.svm.pvIoBitmapR0, NULL /* paPages */);
886 if (RT_FAILURE(rc))
887 {
888 Assert(!pVCpu->cpum.s.Guest.hwvirt.svm.pvIoBitmapR3);
889 LogRel(("CPUM%u: Failed to alloc %u pages for the nested-guest's IO permission bitmap\n", pVCpu->idCpu,
890 SVM_IOPM_PAGES));
891 break;
892 }
893 }
894
895 /* On any failure, cleanup. */
896 if (RT_FAILURE(rc))
897 cpumR3FreeSvmHwVirtState(pVM);
898
899 return rc;
900}
901
902
903/**
904 * Resets per-VCPU SVM hardware virtualization state.
905 *
906 * @param pVCpu The cross context virtual CPU structure.
907 */
908DECLINLINE(void) cpumR3ResetSvmHwVirtState(PVMCPU pVCpu)
909{
910 PCPUMCTX pCtx = &pVCpu->cpum.s.Guest;
911 Assert(pCtx->hwvirt.enmHwvirt == CPUMHWVIRT_SVM);
912 Assert(pCtx->hwvirt.svm.CTX_SUFF(pVmcb));
913
914 memset(pCtx->hwvirt.svm.CTX_SUFF(pVmcb), 0, SVM_VMCB_PAGES << PAGE_SHIFT);
915 pCtx->hwvirt.svm.uMsrHSavePa = 0;
916 pCtx->hwvirt.svm.uPrevPauseTick = 0;
917}
918
919
920/**
921 * Frees memory allocated for the VMX hardware virtualization state.
922 *
923 * @param pVM The cross context VM structure.
924 */
925static void cpumR3FreeVmxHwVirtState(PVM pVM)
926{
927 Assert(pVM->cpum.s.GuestFeatures.fVmx);
928 for (VMCPUID i = 0; i < pVM->cCpus; i++)
929 {
930 PVMCPU pVCpu = &pVM->aCpus[i];
931 if (pVCpu->cpum.s.Guest.hwvirt.vmx.pVmcsR3)
932 {
933 SUPR3PageFreeEx(pVCpu->cpum.s.Guest.hwvirt.vmx.pVmcsR3, VMX_V_VMCS_PAGES);
934 pVCpu->cpum.s.Guest.hwvirt.vmx.pVmcsR3 = NULL;
935 }
936 if (pVCpu->cpum.s.Guest.hwvirt.vmx.pShadowVmcsR3)
937 {
938 SUPR3PageFreeEx(pVCpu->cpum.s.Guest.hwvirt.vmx.pShadowVmcsR3, VMX_V_VMCS_PAGES);
939 pVCpu->cpum.s.Guest.hwvirt.vmx.pShadowVmcsR3 = NULL;
940 }
941 if (pVCpu->cpum.s.Guest.hwvirt.vmx.pvVirtApicPageR3)
942 {
943 SUPR3PageFreeEx(pVCpu->cpum.s.Guest.hwvirt.vmx.pvVirtApicPageR3, VMX_V_VIRT_APIC_PAGES);
944 pVCpu->cpum.s.Guest.hwvirt.vmx.pvVirtApicPageR3 = NULL;
945 }
946 if (pVCpu->cpum.s.Guest.hwvirt.vmx.pvVmreadBitmapR3)
947 {
948 SUPR3PageFreeEx(pVCpu->cpum.s.Guest.hwvirt.vmx.pvVmreadBitmapR3, VMX_V_VMREAD_VMWRITE_BITMAP_PAGES);
949 pVCpu->cpum.s.Guest.hwvirt.vmx.pvVmreadBitmapR3 = NULL;
950 }
951 if (pVCpu->cpum.s.Guest.hwvirt.vmx.pvVmwriteBitmapR3)
952 {
953 SUPR3PageFreeEx(pVCpu->cpum.s.Guest.hwvirt.vmx.pvVmwriteBitmapR3, VMX_V_VMREAD_VMWRITE_BITMAP_PAGES);
954 pVCpu->cpum.s.Guest.hwvirt.vmx.pvVmwriteBitmapR3 = NULL;
955 }
956 if (pVCpu->cpum.s.Guest.hwvirt.vmx.pAutoMsrAreaR3)
957 {
958 SUPR3PageFreeEx(pVCpu->cpum.s.Guest.hwvirt.vmx.pAutoMsrAreaR3, VMX_V_AUTOMSR_AREA_PAGES);
959 pVCpu->cpum.s.Guest.hwvirt.vmx.pAutoMsrAreaR3 = NULL;
960 }
961 if (pVCpu->cpum.s.Guest.hwvirt.vmx.pvMsrBitmapR3)
962 {
963 SUPR3PageFreeEx(pVCpu->cpum.s.Guest.hwvirt.vmx.pvMsrBitmapR3, VMX_V_MSR_BITMAP_PAGES);
964 pVCpu->cpum.s.Guest.hwvirt.vmx.pvMsrBitmapR3 = NULL;
965 }
966 if (pVCpu->cpum.s.Guest.hwvirt.vmx.pvIoBitmapR3)
967 {
968 SUPR3PageFreeEx(pVCpu->cpum.s.Guest.hwvirt.vmx.pvIoBitmapR3, VMX_V_IO_BITMAP_A_PAGES + VMX_V_IO_BITMAP_B_PAGES);
969 pVCpu->cpum.s.Guest.hwvirt.vmx.pvIoBitmapR3 = NULL;
970 }
971 }
972}
973
974
975/**
976 * Allocates memory for the VMX hardware virtualization state.
977 *
978 * @returns VBox status code.
979 * @param pVM The cross context VM structure.
980 */
981static int cpumR3AllocVmxHwVirtState(PVM pVM)
982{
983 int rc = VINF_SUCCESS;
984 LogRel(("CPUM: Allocating %u pages for the nested-guest VMCS and related structures\n",
985 pVM->cCpus * ( VMX_V_VMCS_PAGES + VMX_V_VIRT_APIC_PAGES + VMX_V_VMREAD_VMWRITE_BITMAP_PAGES * 2
986 + VMX_V_AUTOMSR_AREA_PAGES)));
987 for (VMCPUID i = 0; i < pVM->cCpus; i++)
988 {
989 PVMCPU pVCpu = &pVM->aCpus[i];
990 pVCpu->cpum.s.Guest.hwvirt.enmHwvirt = CPUMHWVIRT_VMX;
991
992 /*
993 * Allocate the nested-guest current VMCS.
994 */
995 Assert(VMX_V_VMCS_PAGES == 1);
996 Assert(!pVCpu->cpum.s.Guest.hwvirt.vmx.pVmcsR3);
997 rc = SUPR3PageAllocEx(VMX_V_VMCS_PAGES, 0 /* fFlags */, (void **)&pVCpu->cpum.s.Guest.hwvirt.vmx.pVmcsR3,
998 &pVCpu->cpum.s.Guest.hwvirt.vmx.pVmcsR0, NULL /* paPages */);
999 if (RT_FAILURE(rc))
1000 {
1001 Assert(!pVCpu->cpum.s.Guest.hwvirt.vmx.pVmcsR3);
1002 LogRel(("CPUM%u: Failed to alloc %u pages for the nested-guest's VMCS\n", pVCpu->idCpu, VMX_V_VMCS_PAGES));
1003 break;
1004 }
1005
1006 /*
1007 * Allocate the nested-guest shadow VMCS.
1008 */
1009 Assert(VMX_V_VMCS_PAGES == 1);
1010 Assert(!pVCpu->cpum.s.Guest.hwvirt.vmx.pShadowVmcsR3);
1011 rc = SUPR3PageAllocEx(VMX_V_VMCS_PAGES, 0 /* fFlags */, (void **)&pVCpu->cpum.s.Guest.hwvirt.vmx.pShadowVmcsR3,
1012 &pVCpu->cpum.s.Guest.hwvirt.vmx.pShadowVmcsR0, NULL /* paPages */);
1013 if (RT_FAILURE(rc))
1014 {
1015 Assert(!pVCpu->cpum.s.Guest.hwvirt.vmx.pShadowVmcsR3);
1016 LogRel(("CPUM%u: Failed to alloc %u pages for the nested-guest's shadow VMCS\n", pVCpu->idCpu, VMX_V_VMCS_PAGES));
1017 break;
1018 }
1019
1020 /*
1021 * Allocate the Virtual-APIC page.
1022 */
1023 Assert(!pVCpu->cpum.s.Guest.hwvirt.vmx.pvVirtApicPageR3);
1024 rc = SUPR3PageAllocEx(VMX_V_VIRT_APIC_PAGES, 0 /* fFlags */, &pVCpu->cpum.s.Guest.hwvirt.vmx.pvVirtApicPageR3,
1025 &pVCpu->cpum.s.Guest.hwvirt.vmx.pvVirtApicPageR0, NULL /* paPages */);
1026 if (RT_FAILURE(rc))
1027 {
1028 Assert(!pVCpu->cpum.s.Guest.hwvirt.vmx.pvVirtApicPageR3);
1029 LogRel(("CPUM%u: Failed to alloc %u pages for the nested-guest's Virtual-APIC page\n", pVCpu->idCpu,
1030 VMX_V_VIRT_APIC_PAGES));
1031 break;
1032 }
1033
1034 /*
1035 * Allocate the VMREAD-bitmap.
1036 */
1037 Assert(!pVCpu->cpum.s.Guest.hwvirt.vmx.pvVmreadBitmapR3);
1038 rc = SUPR3PageAllocEx(VMX_V_VMREAD_VMWRITE_BITMAP_PAGES, 0 /* fFlags */, &pVCpu->cpum.s.Guest.hwvirt.vmx.pvVmreadBitmapR3,
1039 &pVCpu->cpum.s.Guest.hwvirt.vmx.pvVmreadBitmapR0, NULL /* paPages */);
1040 if (RT_FAILURE(rc))
1041 {
1042 Assert(!pVCpu->cpum.s.Guest.hwvirt.vmx.pvVmreadBitmapR3);
1043 LogRel(("CPUM%u: Failed to alloc %u pages for the nested-guest's VMREAD-bitmap\n", pVCpu->idCpu,
1044 VMX_V_VMREAD_VMWRITE_BITMAP_PAGES));
1045 break;
1046 }
1047
1048 /*
1049 * Allocatge the VMWRITE-bitmap.
1050 */
1051 Assert(!pVCpu->cpum.s.Guest.hwvirt.vmx.pvVmwriteBitmapR3);
1052 rc = SUPR3PageAllocEx(VMX_V_VMREAD_VMWRITE_BITMAP_PAGES, 0 /* fFlags */,
1053 &pVCpu->cpum.s.Guest.hwvirt.vmx.pvVmwriteBitmapR3,
1054 &pVCpu->cpum.s.Guest.hwvirt.vmx.pvVmwriteBitmapR0, NULL /* paPages */);
1055 if (RT_FAILURE(rc))
1056 {
1057 Assert(!pVCpu->cpum.s.Guest.hwvirt.vmx.pvVmwriteBitmapR3);
1058 LogRel(("CPUM%u: Failed to alloc %u pages for the nested-guest's VMWRITE-bitmap\n", pVCpu->idCpu,
1059 VMX_V_VMREAD_VMWRITE_BITMAP_PAGES));
1060 break;
1061 }
1062
1063 /*
1064 * Allocate the MSR auto-load/store area.
1065 */
1066 Assert(!pVCpu->cpum.s.Guest.hwvirt.vmx.pAutoMsrAreaR3);
1067 rc = SUPR3PageAllocEx(VMX_V_AUTOMSR_AREA_PAGES, 0 /* fFlags */, (void **)&pVCpu->cpum.s.Guest.hwvirt.vmx.pAutoMsrAreaR3,
1068 &pVCpu->cpum.s.Guest.hwvirt.vmx.pAutoMsrAreaR0, NULL /* paPages */);
1069 if (RT_FAILURE(rc))
1070 {
1071 Assert(!pVCpu->cpum.s.Guest.hwvirt.vmx.pAutoMsrAreaR3);
1072 LogRel(("CPUM%u: Failed to alloc %u pages for the nested-guest's auto-load/store MSR area\n", pVCpu->idCpu,
1073 VMX_V_AUTOMSR_AREA_PAGES));
1074 break;
1075 }
1076
1077 /*
1078 * Allocate the MSR bitmap.
1079 */
1080 Assert(!pVCpu->cpum.s.Guest.hwvirt.vmx.pvMsrBitmapR3);
1081 rc = SUPR3PageAllocEx(VMX_V_MSR_BITMAP_PAGES, 0 /* fFlags */, (void **)&pVCpu->cpum.s.Guest.hwvirt.vmx.pvMsrBitmapR3,
1082 &pVCpu->cpum.s.Guest.hwvirt.vmx.pvMsrBitmapR0, NULL /* paPages */);
1083 if (RT_FAILURE(rc))
1084 {
1085 Assert(!pVCpu->cpum.s.Guest.hwvirt.vmx.pvMsrBitmapR3);
1086 LogRel(("CPUM%u: Failed to alloc %u pages for the nested-guest's MSR bitmap\n", pVCpu->idCpu,
1087 VMX_V_MSR_BITMAP_PAGES));
1088 break;
1089 }
1090
1091 /*
1092 * Allocate the I/O bitmaps (A and B).
1093 */
1094 Assert(!pVCpu->cpum.s.Guest.hwvirt.vmx.pvIoBitmapR3);
1095 rc = SUPR3PageAllocEx(VMX_V_IO_BITMAP_A_PAGES + VMX_V_IO_BITMAP_B_PAGES, 0 /* fFlags */,
1096 (void **)&pVCpu->cpum.s.Guest.hwvirt.vmx.pvIoBitmapR3,
1097 &pVCpu->cpum.s.Guest.hwvirt.vmx.pvIoBitmapR0, NULL /* paPages */);
1098 if (RT_FAILURE(rc))
1099 {
1100 Assert(!pVCpu->cpum.s.Guest.hwvirt.vmx.pvIoBitmapR3);
1101 LogRel(("CPUM%u: Failed to alloc %u pages for the nested-guest's I/O bitmaps\n", pVCpu->idCpu,
1102 VMX_V_IO_BITMAP_A_PAGES + VMX_V_IO_BITMAP_B_PAGES));
1103 break;
1104 }
1105 }
1106
1107 /* On any failure, cleanup. */
1108 if (RT_FAILURE(rc))
1109 cpumR3FreeVmxHwVirtState(pVM);
1110
1111 return rc;
1112}
1113
1114
1115/**
1116 * Resets per-VCPU VMX hardware virtualization state.
1117 *
1118 * @param pVCpu The cross context virtual CPU structure.
1119 */
1120DECLINLINE(void) cpumR3ResetVmxHwVirtState(PVMCPU pVCpu)
1121{
1122 PCPUMCTX pCtx = &pVCpu->cpum.s.Guest;
1123 Assert(pCtx->hwvirt.enmHwvirt == CPUMHWVIRT_VMX);
1124 Assert(pCtx->hwvirt.vmx.CTX_SUFF(pVmcs));
1125 Assert(pCtx->hwvirt.vmx.CTX_SUFF(pShadowVmcs));
1126
1127 memset(pCtx->hwvirt.vmx.CTX_SUFF(pVmcs), 0, VMX_V_VMCS_SIZE);
1128 memset(pCtx->hwvirt.vmx.CTX_SUFF(pShadowVmcs), 0, VMX_V_VMCS_SIZE);
1129 pCtx->hwvirt.vmx.GCPhysVmxon = NIL_RTGCPHYS;
1130 pCtx->hwvirt.vmx.GCPhysShadowVmcs = NIL_RTGCPHYS;
1131 pCtx->hwvirt.vmx.GCPhysVmxon = NIL_RTGCPHYS;
1132 pCtx->hwvirt.vmx.fInVmxRootMode = false;
1133 pCtx->hwvirt.vmx.fInVmxNonRootMode = false;
1134 /* Don't reset diagnostics here. */
1135}
1136
1137
1138/**
1139 * Displays the host and guest VMX features.
1140 *
1141 * @param pVM The cross context VM structure.
1142 * @param pHlp The info helper functions.
1143 * @param pszArgs "terse", "default" or "verbose".
1144 */
1145DECLCALLBACK(void) cpumR3InfoVmxFeatures(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
1146{
1147 RT_NOREF(pszArgs);
1148 PCCPUMFEATURES pHostFeatures = &pVM->cpum.s.HostFeatures;
1149 PCCPUMFEATURES pGuestFeatures = &pVM->cpum.s.GuestFeatures;
1150 if ( pHostFeatures->enmCpuVendor == CPUMCPUVENDOR_INTEL
1151 || pHostFeatures->enmCpuVendor == CPUMCPUVENDOR_VIA)
1152 {
1153#define VMXFEATDUMP(a_szDesc, a_Var) \
1154 pHlp->pfnPrintf(pHlp, " %s = %u (%u)\n", a_szDesc, pGuestFeatures->a_Var, pHostFeatures->a_Var)
1155
1156 pHlp->pfnPrintf(pHlp, "Nested hardware virtualization - VMX features\n");
1157 pHlp->pfnPrintf(pHlp, " Mnemonic - Description = guest (host)\n");
1158 VMXFEATDUMP("VMX - Virtual-Machine Extensions ", fVmx);
1159 /* Basic. */
1160 VMXFEATDUMP("InsOutInfo - INS/OUTS instruction info. ", fVmxInsOutInfo);
1161 /* Pin-based controls. */
1162 VMXFEATDUMP("ExtIntExit - External interrupt VM-exit ", fVmxExtIntExit);
1163 VMXFEATDUMP("NmiExit - NMI VM-exit ", fVmxNmiExit);
1164 VMXFEATDUMP("VirtNmi - Virtual NMIs ", fVmxVirtNmi);
1165 VMXFEATDUMP("PreemptTimer - VMX preemption timer ", fVmxPreemptTimer);
1166 VMXFEATDUMP("PostedInt - Posted interrupts ", fVmxPostedInt);
1167 /* Processor-based controls. */
1168 VMXFEATDUMP("IntWindowExit - Interrupt-window exiting ", fVmxIntWindowExit);
1169 VMXFEATDUMP("TscOffsetting - TSC offsetting ", fVmxTscOffsetting);
1170 VMXFEATDUMP("HltExit - HLT exiting ", fVmxHltExit);
1171 VMXFEATDUMP("InvlpgExit - INVLPG exiting ", fVmxInvlpgExit);
1172 VMXFEATDUMP("MwaitExit - MWAIT exiting ", fVmxMwaitExit);
1173 VMXFEATDUMP("RdpmcExit - RDPMC exiting ", fVmxRdpmcExit);
1174 VMXFEATDUMP("RdtscExit - RDTSC exiting ", fVmxRdtscExit);
1175 VMXFEATDUMP("Cr3LoadExit - CR3-load exiting ", fVmxCr3LoadExit);
1176 VMXFEATDUMP("Cr3StoreExit - CR3-store exiting ", fVmxCr3StoreExit);
1177 VMXFEATDUMP("Cr8LoadExit - CR8-load exiting ", fVmxCr8LoadExit);
1178 VMXFEATDUMP("Cr8StoreExit - CR8-store exiting ", fVmxCr8StoreExit);
1179 VMXFEATDUMP("UseTprShadow - Use TPR shadow ", fVmxUseTprShadow);
1180 VMXFEATDUMP("NmiWindowExit - NMI-window exiting ", fVmxNmiWindowExit);
1181 VMXFEATDUMP("MovDRxExit - Mov-DR exiting ", fVmxMovDRxExit);
1182 VMXFEATDUMP("UncondIoExit - Unconditional I/O exiting ", fVmxUncondIoExit);
1183 VMXFEATDUMP("UseIoBitmaps - Use I/O bitmaps ", fVmxUseIoBitmaps);
1184 VMXFEATDUMP("MonitorTrapFlag - Monitor trap flag ", fVmxMonitorTrapFlag);
1185 VMXFEATDUMP("UseMsrBitmaps - MSR bitmaps ", fVmxUseMsrBitmaps);
1186 VMXFEATDUMP("MonitorExit - MONITOR exiting ", fVmxMonitorExit);
1187 VMXFEATDUMP("PauseExit - PAUSE exiting ", fVmxPauseExit);
1188 VMXFEATDUMP("SecondaryExecCtl - Activate secondary controls ", fVmxSecondaryExecCtls);
1189 /* Secondary processor-based controls. */
1190 VMXFEATDUMP("VirtApic - Virtualize-APIC accesses ", fVmxVirtApicAccess);
1191 VMXFEATDUMP("Ept - Extended Page Tables ", fVmxEpt);
1192 VMXFEATDUMP("DescTableExit - Descriptor-table exiting ", fVmxDescTableExit);
1193 VMXFEATDUMP("Rdtscp - Enable RDTSCP ", fVmxRdtscp);
1194 VMXFEATDUMP("VirtX2ApicMode - Virtualize-x2APIC mode ", fVmxVirtX2ApicMode);
1195 VMXFEATDUMP("Vpid - Enable VPID ", fVmxVpid);
1196 VMXFEATDUMP("WbinvdExit - WBINVD exiting ", fVmxWbinvdExit);
1197 VMXFEATDUMP("UnrestrictedGuest - Unrestricted guest ", fVmxUnrestrictedGuest);
1198 VMXFEATDUMP("ApicRegVirt - APIC-register virtualization ", fVmxApicRegVirt);
1199 VMXFEATDUMP("VirtIntDelivery - Virtual-interrupt delivery ", fVmxVirtIntDelivery);
1200 VMXFEATDUMP("PauseLoopExit - PAUSE-loop exiting ", fVmxPauseLoopExit);
1201 VMXFEATDUMP("RdrandExit - RDRAND exiting ", fVmxRdrandExit);
1202 VMXFEATDUMP("Invpcid - Enable INVPCID ", fVmxInvpcid);
1203 VMXFEATDUMP("VmFuncs - Enable VM Functions ", fVmxVmFunc);
1204 VMXFEATDUMP("VmcsShadowing - VMCS shadowing ", fVmxVmcsShadowing);
1205 VMXFEATDUMP("RdseedExiting - RDSEED exiting ", fVmxRdseedExit);
1206 VMXFEATDUMP("PML - Supports Page-Modification Log (PML) ", fVmxPml);
1207 VMXFEATDUMP("EptVe - EPT violations can cause #VE ", fVmxEptXcptVe);
1208 VMXFEATDUMP("XsavesXRstors - Enable XSAVES/XRSTORS ", fVmxXsavesXrstors);
1209 /* VM-entry controls. */
1210 VMXFEATDUMP("EntryLoadDebugCtls - Load debug controls on VM-entry ", fVmxEntryLoadDebugCtls);
1211 VMXFEATDUMP("Ia32eModeGuest - IA-32e mode guest ", fVmxIa32eModeGuest);
1212 VMXFEATDUMP("EntryLoadEferMsr - Load IA32_EFER on VM-entry ", fVmxEntryLoadEferMsr);
1213 VMXFEATDUMP("EntryLoadPatMsr - Load IA32_PAT on VM-entry ", fVmxEntryLoadPatMsr);
1214 /* VM-exit controls. */
1215 VMXFEATDUMP("ExitSaveDebugCtls - Save debug controls on VM-exit ", fVmxExitSaveDebugCtls);
1216 VMXFEATDUMP("HostAddrSpaceSize - Host address-space size ", fVmxHostAddrSpaceSize);
1217 VMXFEATDUMP("ExitAckExtInt - Acknowledge interrupt on VM-exit ", fVmxExitAckExtInt);
1218 VMXFEATDUMP("ExitSavePatMsr - Save IA32_PAT on VM-exit ", fVmxExitSavePatMsr);
1219 VMXFEATDUMP("ExitLoadPatMsr - Load IA32_PAT on VM-exit ", fVmxExitLoadPatMsr);
1220 VMXFEATDUMP("ExitSaveEferMsr - Save IA32_EFER on VM-exit ", fVmxExitSaveEferMsr);
1221 VMXFEATDUMP("ExitLoadEferMsr - Load IA32_EFER on VM-exit ", fVmxExitLoadEferMsr);
1222 VMXFEATDUMP("SavePreemptTimer - Save VMX-preemption timer ", fVmxSavePreemptTimer);
1223 /* Miscellaneous data. */
1224 VMXFEATDUMP("ExitSaveEferLma - Save EFER.LMA on VM-exit ", fVmxExitSaveEferLma);
1225 VMXFEATDUMP("IntelPt - Intel PT (Processor Trace) in VMX operation ", fVmxIntelPt);
1226 VMXFEATDUMP("VmwriteAll - Write allowed to read-only VMCS fields ", fVmxVmwriteAll);
1227 VMXFEATDUMP("EntryInjectSoftInt - Inject softint. with 0-len instr. ", fVmxEntryInjectSoftInt);
1228#undef VMXFEATDUMP
1229 }
1230 else
1231 pHlp->pfnPrintf(pHlp, "No VMX features present - requires an Intel or compatible CPU.\n");
1232}
1233
1234
1235/**
1236 * Checks whether nested-guest execution using hardware-assisted VMX (e.g, using HM
1237 * or NEM) is allowed.
1238 *
1239 * @returns @c true if hardware-assisted nested-guest execution is allowed, @c false
1240 * otherwise.
1241 * @param pVM The cross context VM structure.
1242 */
1243static bool cpumR3IsHwAssistNstGstExecAllowed(PVM pVM)
1244{
1245 AssertMsg(pVM->bMainExecutionEngine != VM_EXEC_ENGINE_NOT_SET, ("Calling this function too early!\n"));
1246#ifndef VBOX_WITH_NESTED_HWVIRT_ONLY_IN_IEM
1247 if ( pVM->bMainExecutionEngine == VM_EXEC_ENGINE_HW_VIRT
1248 || pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
1249 return true;
1250#else
1251 NOREF(pVM);
1252#endif
1253 return false;
1254}
1255
1256
1257/**
1258 * Initializes the VMX guest MSRs from guest CPU features based on the host MSRs.
1259 *
1260 * @param pVM The cross context VM structure.
1261 * @param pHostVmxMsrs The host VMX MSRs. Pass NULL when fully emulating VMX
1262 * and no hardware-assisted nested-guest execution is
1263 * possible for this VM.
1264 * @param pGuestFeatures The guest features to use (only VMX features are
1265 * accessed).
1266 * @param pGuestVmxMsrs Where to store the initialized guest VMX MSRs.
1267 *
1268 * @remarks This function ASSUMES the VMX guest-features are already exploded!
1269 */
1270static void cpumR3InitVmxGuestMsrs(PVM pVM, PCVMXMSRS pHostVmxMsrs, PCCPUMFEATURES pGuestFeatures, PVMXMSRS pGuestVmxMsrs)
1271{
1272 bool const fIsNstGstHwExecAllowed = cpumR3IsHwAssistNstGstExecAllowed(pVM);
1273
1274 Assert(!fIsNstGstHwExecAllowed || pHostVmxMsrs);
1275 Assert(pGuestFeatures->fVmx);
1276
1277 /*
1278 * We don't support the following MSRs yet:
1279 * - True Pin-based VM-execution controls.
1280 * - True Processor-based VM-execution controls.
1281 * - True VM-entry VM-execution controls.
1282 * - True VM-exit VM-execution controls.
1283 * - EPT/VPID capabilities.
1284 */
1285
1286 /* Feature control. */
1287 pGuestVmxMsrs->u64FeatCtrl = MSR_IA32_FEATURE_CONTROL_LOCK | MSR_IA32_FEATURE_CONTROL_VMXON;
1288
1289 /* Basic information. */
1290 {
1291 uint64_t const u64Basic = RT_BF_MAKE(VMX_BF_BASIC_VMCS_ID, VMX_V_VMCS_REVISION_ID )
1292 | RT_BF_MAKE(VMX_BF_BASIC_VMCS_SIZE, VMX_V_VMCS_SIZE )
1293 | RT_BF_MAKE(VMX_BF_BASIC_PHYSADDR_WIDTH, !pGuestFeatures->fLongMode )
1294 | RT_BF_MAKE(VMX_BF_BASIC_DUAL_MON, 0 )
1295 | RT_BF_MAKE(VMX_BF_BASIC_VMCS_MEM_TYPE, VMX_BASIC_MEM_TYPE_WB )
1296 | RT_BF_MAKE(VMX_BF_BASIC_VMCS_INS_OUTS, pGuestFeatures->fVmxInsOutInfo)
1297 | RT_BF_MAKE(VMX_BF_BASIC_TRUE_CTLS, 0 );
1298 pGuestVmxMsrs->u64Basic = u64Basic;
1299 }
1300
1301 /* Pin-based VM-execution controls. */
1302 {
1303 uint32_t const fFeatures = (pGuestFeatures->fVmxExtIntExit << VMX_BF_PIN_CTLS_EXT_INT_EXIT_SHIFT )
1304 | (pGuestFeatures->fVmxNmiExit << VMX_BF_PIN_CTLS_NMI_EXIT_SHIFT )
1305 | (pGuestFeatures->fVmxVirtNmi << VMX_BF_PIN_CTLS_VIRT_NMI_SHIFT )
1306 | (pGuestFeatures->fVmxPreemptTimer << VMX_BF_PIN_CTLS_PREEMPT_TIMER_SHIFT)
1307 | (pGuestFeatures->fVmxPostedInt << VMX_BF_PIN_CTLS_POSTED_INT_SHIFT );
1308 uint32_t const fAllowed0 = VMX_PIN_CTLS_DEFAULT1;
1309 uint32_t const fAllowed1 = fFeatures | VMX_PIN_CTLS_DEFAULT1;
1310 AssertMsg((fAllowed0 & fAllowed1) == fAllowed0, ("fAllowed0=%#RX32 fAllowed1=%#RX32 fFeatures=%#RX32\n",
1311 fAllowed0, fAllowed1, fFeatures));
1312 pGuestVmxMsrs->PinCtls.u = RT_MAKE_U64(fAllowed0, fAllowed1);
1313 }
1314
1315 /* Processor-based VM-execution controls. */
1316 {
1317 uint32_t const fFeatures = (pGuestFeatures->fVmxIntWindowExit << VMX_BF_PROC_CTLS_INT_WINDOW_EXIT_SHIFT )
1318 | (pGuestFeatures->fVmxTscOffsetting << VMX_BF_PROC_CTLS_USE_TSC_OFFSETTING_SHIFT)
1319 | (pGuestFeatures->fVmxHltExit << VMX_BF_PROC_CTLS_HLT_EXIT_SHIFT )
1320 | (pGuestFeatures->fVmxInvlpgExit << VMX_BF_PROC_CTLS_INVLPG_EXIT_SHIFT )
1321 | (pGuestFeatures->fVmxMwaitExit << VMX_BF_PROC_CTLS_MWAIT_EXIT_SHIFT )
1322 | (pGuestFeatures->fVmxRdpmcExit << VMX_BF_PROC_CTLS_RDPMC_EXIT_SHIFT )
1323 | (pGuestFeatures->fVmxRdtscExit << VMX_BF_PROC_CTLS_RDTSC_EXIT_SHIFT )
1324 | (pGuestFeatures->fVmxCr3LoadExit << VMX_BF_PROC_CTLS_CR3_LOAD_EXIT_SHIFT )
1325 | (pGuestFeatures->fVmxCr3StoreExit << VMX_BF_PROC_CTLS_CR3_STORE_EXIT_SHIFT )
1326 | (pGuestFeatures->fVmxCr8LoadExit << VMX_BF_PROC_CTLS_CR8_LOAD_EXIT_SHIFT )
1327 | (pGuestFeatures->fVmxCr8StoreExit << VMX_BF_PROC_CTLS_CR8_STORE_EXIT_SHIFT )
1328 | (pGuestFeatures->fVmxUseTprShadow << VMX_BF_PROC_CTLS_USE_TPR_SHADOW_SHIFT )
1329 | (pGuestFeatures->fVmxNmiWindowExit << VMX_BF_PROC_CTLS_NMI_WINDOW_EXIT_SHIFT )
1330 | (pGuestFeatures->fVmxMovDRxExit << VMX_BF_PROC_CTLS_MOV_DR_EXIT_SHIFT )
1331 | (pGuestFeatures->fVmxUncondIoExit << VMX_BF_PROC_CTLS_UNCOND_IO_EXIT_SHIFT )
1332 | (pGuestFeatures->fVmxUseIoBitmaps << VMX_BF_PROC_CTLS_USE_IO_BITMAPS_SHIFT )
1333 | (pGuestFeatures->fVmxMonitorTrapFlag << VMX_BF_PROC_CTLS_MONITOR_TRAP_FLAG_SHIFT )
1334 | (pGuestFeatures->fVmxUseMsrBitmaps << VMX_BF_PROC_CTLS_USE_MSR_BITMAPS_SHIFT )
1335 | (pGuestFeatures->fVmxMonitorExit << VMX_BF_PROC_CTLS_MONITOR_EXIT_SHIFT )
1336 | (pGuestFeatures->fVmxPauseExit << VMX_BF_PROC_CTLS_PAUSE_EXIT_SHIFT )
1337 | (pGuestFeatures->fVmxSecondaryExecCtls << VMX_BF_PROC_CTLS_USE_SECONDARY_CTLS_SHIFT);
1338 uint32_t const fAllowed0 = VMX_PROC_CTLS_DEFAULT1;
1339 uint32_t const fAllowed1 = fFeatures | VMX_PROC_CTLS_DEFAULT1;
1340 AssertMsg((fAllowed0 & fAllowed1) == fAllowed0, ("fAllowed0=%#RX32 fAllowed1=%#RX32 fFeatures=%#RX32\n", fAllowed0,
1341 fAllowed1, fFeatures));
1342 pGuestVmxMsrs->ProcCtls.u = RT_MAKE_U64(fAllowed0, fAllowed1);
1343 }
1344
1345 /* Secondary processor-based VM-execution controls. */
1346 if (pGuestFeatures->fVmxSecondaryExecCtls)
1347 {
1348 uint32_t const fFeatures = (pGuestFeatures->fVmxVirtApicAccess << VMX_BF_PROC_CTLS2_VIRT_APIC_ACCESS_SHIFT )
1349 | (pGuestFeatures->fVmxEpt << VMX_BF_PROC_CTLS2_EPT_SHIFT )
1350 | (pGuestFeatures->fVmxDescTableExit << VMX_BF_PROC_CTLS2_DESC_TABLE_EXIT_SHIFT )
1351 | (pGuestFeatures->fVmxRdtscp << VMX_BF_PROC_CTLS2_RDTSCP_SHIFT )
1352 | (pGuestFeatures->fVmxVirtX2ApicMode << VMX_BF_PROC_CTLS2_VIRT_X2APIC_MODE_SHIFT )
1353 | (pGuestFeatures->fVmxVpid << VMX_BF_PROC_CTLS2_VPID_SHIFT )
1354 | (pGuestFeatures->fVmxWbinvdExit << VMX_BF_PROC_CTLS2_WBINVD_EXIT_SHIFT )
1355 | (pGuestFeatures->fVmxUnrestrictedGuest << VMX_BF_PROC_CTLS2_UNRESTRICTED_GUEST_SHIFT)
1356 | (pGuestFeatures->fVmxApicRegVirt << VMX_BF_PROC_CTLS2_APIC_REG_VIRT_SHIFT )
1357 | (pGuestFeatures->fVmxVirtIntDelivery << VMX_BF_PROC_CTLS2_VIRT_INT_DELIVERY_SHIFT )
1358 | (pGuestFeatures->fVmxPauseLoopExit << VMX_BF_PROC_CTLS2_PAUSE_LOOP_EXIT_SHIFT )
1359 | (pGuestFeatures->fVmxRdrandExit << VMX_BF_PROC_CTLS2_RDRAND_EXIT_SHIFT )
1360 | (pGuestFeatures->fVmxInvpcid << VMX_BF_PROC_CTLS2_INVPCID_SHIFT )
1361 | (pGuestFeatures->fVmxVmFunc << VMX_BF_PROC_CTLS2_VMFUNC_SHIFT )
1362 | (pGuestFeatures->fVmxVmcsShadowing << VMX_BF_PROC_CTLS2_VMCS_SHADOWING_SHIFT )
1363 | (pGuestFeatures->fVmxRdseedExit << VMX_BF_PROC_CTLS2_RDSEED_EXIT_SHIFT )
1364 | (pGuestFeatures->fVmxPml << VMX_BF_PROC_CTLS2_PML_SHIFT )
1365 | (pGuestFeatures->fVmxEptXcptVe << VMX_BF_PROC_CTLS2_EPT_VE_SHIFT )
1366 | (pGuestFeatures->fVmxXsavesXrstors << VMX_BF_PROC_CTLS2_XSAVES_XRSTORS_SHIFT )
1367 | (pGuestFeatures->fVmxUseTscScaling << VMX_BF_PROC_CTLS2_TSC_SCALING_SHIFT );
1368 uint32_t const fAllowed0 = 0;
1369 uint32_t const fAllowed1 = fFeatures;
1370 pGuestVmxMsrs->ProcCtls2.u = RT_MAKE_U64(fAllowed0, fAllowed1);
1371 }
1372
1373 /* VM-exit controls. */
1374 {
1375 uint32_t const fFeatures = (pGuestFeatures->fVmxExitSaveDebugCtls << VMX_BF_EXIT_CTLS_SAVE_DEBUG_SHIFT )
1376 | (pGuestFeatures->fVmxHostAddrSpaceSize << VMX_BF_EXIT_CTLS_HOST_ADDR_SPACE_SIZE_SHIFT)
1377 | (pGuestFeatures->fVmxExitAckExtInt << VMX_BF_EXIT_CTLS_ACK_EXT_INT_SHIFT )
1378 | (pGuestFeatures->fVmxExitSavePatMsr << VMX_BF_EXIT_CTLS_SAVE_PAT_MSR_SHIFT )
1379 | (pGuestFeatures->fVmxExitLoadPatMsr << VMX_BF_EXIT_CTLS_LOAD_PAT_MSR_SHIFT )
1380 | (pGuestFeatures->fVmxExitSaveEferMsr << VMX_BF_EXIT_CTLS_SAVE_EFER_MSR_SHIFT )
1381 | (pGuestFeatures->fVmxExitLoadEferMsr << VMX_BF_EXIT_CTLS_LOAD_EFER_MSR_SHIFT )
1382 | (pGuestFeatures->fVmxSavePreemptTimer << VMX_BF_EXIT_CTLS_SAVE_PREEMPT_TIMER_SHIFT );
1383 /* Set the default1 class bits. See Intel spec. A.4 "VM-exit Controls". */
1384 uint32_t const fAllowed0 = VMX_EXIT_CTLS_DEFAULT1;
1385 uint32_t const fAllowed1 = fFeatures | VMX_EXIT_CTLS_DEFAULT1;
1386 AssertMsg((fAllowed0 & fAllowed1) == fAllowed0, ("fAllowed0=%#RX32 fAllowed1=%#RX32 fFeatures=%#RX32\n", fAllowed0,
1387 fAllowed1, fFeatures));
1388 pGuestVmxMsrs->ExitCtls.u = RT_MAKE_U64(fAllowed0, fAllowed1);
1389 }
1390
1391 /* VM-entry controls. */
1392 {
1393 uint32_t const fFeatures = (pGuestFeatures->fVmxEntryLoadDebugCtls << VMX_BF_ENTRY_CTLS_LOAD_DEBUG_SHIFT )
1394 | (pGuestFeatures->fVmxIa32eModeGuest << VMX_BF_ENTRY_CTLS_IA32E_MODE_GUEST_SHIFT)
1395 | (pGuestFeatures->fVmxEntryLoadEferMsr << VMX_BF_ENTRY_CTLS_LOAD_EFER_MSR_SHIFT )
1396 | (pGuestFeatures->fVmxEntryLoadPatMsr << VMX_BF_ENTRY_CTLS_LOAD_PAT_MSR_SHIFT );
1397 uint32_t const fAllowed0 = VMX_ENTRY_CTLS_DEFAULT1;
1398 uint32_t const fAllowed1 = fFeatures | VMX_ENTRY_CTLS_DEFAULT1;
1399 AssertMsg((fAllowed0 & fAllowed1) == fAllowed0, ("fAllowed0=%#RX32 fAllowed0=%#RX32 fFeatures=%#RX32\n", fAllowed0,
1400 fAllowed1, fFeatures));
1401 pGuestVmxMsrs->EntryCtls.u = RT_MAKE_U64(fAllowed0, fAllowed1);
1402 }
1403
1404 /* Miscellaneous data. */
1405 {
1406 uint64_t const uHostMsr = fIsNstGstHwExecAllowed ? pHostVmxMsrs->u64Misc : 0;
1407
1408 uint8_t const cMaxMsrs = RT_MIN(RT_BF_GET(uHostMsr, VMX_BF_MISC_MAX_MSRS), VMX_V_AUTOMSR_COUNT_MAX);
1409 uint8_t const fActivityState = RT_BF_GET(uHostMsr, VMX_BF_MISC_ACTIVITY_STATES) & VMX_V_GUEST_ACTIVITY_STATE_MASK;
1410 pGuestVmxMsrs->u64Misc = RT_BF_MAKE(VMX_BF_MISC_PREEMPT_TIMER_TSC, VMX_V_PREEMPT_TIMER_SHIFT )
1411 | RT_BF_MAKE(VMX_BF_MISC_EXIT_SAVE_EFER_LMA, pGuestFeatures->fVmxExitSaveEferLma )
1412 | RT_BF_MAKE(VMX_BF_MISC_ACTIVITY_STATES, fActivityState )
1413 | RT_BF_MAKE(VMX_BF_MISC_INTEL_PT, pGuestFeatures->fVmxIntelPt )
1414 | RT_BF_MAKE(VMX_BF_MISC_SMM_READ_SMBASE_MSR, 0 )
1415 | RT_BF_MAKE(VMX_BF_MISC_CR3_TARGET, VMX_V_CR3_TARGET_COUNT )
1416 | RT_BF_MAKE(VMX_BF_MISC_MAX_MSRS, cMaxMsrs )
1417 | RT_BF_MAKE(VMX_BF_MISC_VMXOFF_BLOCK_SMI, 0 )
1418 | RT_BF_MAKE(VMX_BF_MISC_VMWRITE_ALL, pGuestFeatures->fVmxVmwriteAll )
1419 | RT_BF_MAKE(VMX_BF_MISC_ENTRY_INJECT_SOFT_INT, pGuestFeatures->fVmxEntryInjectSoftInt)
1420 | RT_BF_MAKE(VMX_BF_MISC_MSEG_ID, VMX_V_MSEG_REV_ID );
1421 }
1422
1423 /* CR0 Fixed-0. */
1424 pGuestVmxMsrs->u64Cr0Fixed0 = pGuestFeatures->fVmxUnrestrictedGuest ? VMX_V_CR0_FIXED0_UX: VMX_V_CR0_FIXED0;
1425
1426 /* CR0 Fixed-1. */
1427 {
1428 /*
1429 * All CPUs I've looked at so far report CR0 fixed-1 bits as 0xffffffff.
1430 * This is different from CR4 fixed-1 bits which are reported as per the
1431 * CPU features and/or micro-architecture/generation. Why? Ask Intel.
1432 */
1433 uint64_t const uHostMsr = fIsNstGstHwExecAllowed ? pHostVmxMsrs->u64Cr0Fixed1 : 0xffffffff;
1434 pGuestVmxMsrs->u64Cr0Fixed1 = uHostMsr | VMX_V_CR0_FIXED0; /* Make sure the CR0 MB1 bits are not clear. */
1435 }
1436
1437 /* CR4 Fixed-0. */
1438 pGuestVmxMsrs->u64Cr4Fixed0 = VMX_V_CR4_FIXED0;
1439
1440 /* CR4 Fixed-1. */
1441 {
1442 uint64_t const uHostMsr = fIsNstGstHwExecAllowed ? pHostVmxMsrs->u64Cr4Fixed1 : CPUMGetGuestCR4ValidMask(pVM);
1443 pGuestVmxMsrs->u64Cr4Fixed1 = uHostMsr | VMX_V_CR4_FIXED0; /* Make sure the CR4 MB1 bits are not clear. */
1444 }
1445
1446 /* VMCS Enumeration. */
1447 pGuestVmxMsrs->u64VmcsEnum = VMX_V_VMCS_MAX_INDEX << VMX_BF_VMCS_ENUM_HIGHEST_IDX_SHIFT;
1448
1449 /* VM Functions. */
1450 if (pGuestFeatures->fVmxVmFunc)
1451 pGuestVmxMsrs->u64VmFunc = RT_BF_MAKE(VMX_BF_VMFUNC_EPTP_SWITCHING, 1);
1452}
1453
1454
1455#if 0
1456/**
1457 * Checks whether the given guest CPU VMX features are compatible with the provided
1458 * base features.
1459 *
1460 * @returns @c true if compatible, @c false otherwise.
1461 * @param pVM The cross context VM structure.
1462 * @param pBase The base VMX CPU features.
1463 * @param pGst The guest VMX CPU features.
1464 *
1465 * @remarks Only VMX feature bits are examined.
1466 */
1467static bool cpumR3AreVmxCpuFeaturesCompatible(PVM pVM, PCCPUMFEATURES pBase, PCCPUMFEATURES pGst)
1468{
1469 if (cpumR3IsHwAssistVmxNstGstExecAllowed(pVM))
1470 {
1471 uint64_t const fBase = ((uint64_t)pBase->fVmxInsOutInfo << 0) | ((uint64_t)pBase->fVmxExtIntExit << 1)
1472 | ((uint64_t)pBase->fVmxNmiExit << 2) | ((uint64_t)pBase->fVmxVirtNmi << 3)
1473 | ((uint64_t)pBase->fVmxPreemptTimer << 4) | ((uint64_t)pBase->fVmxPostedInt << 5)
1474 | ((uint64_t)pBase->fVmxIntWindowExit << 6) | ((uint64_t)pBase->fVmxTscOffsetting << 7)
1475 | ((uint64_t)pBase->fVmxHltExit << 8) | ((uint64_t)pBase->fVmxInvlpgExit << 9)
1476 | ((uint64_t)pBase->fVmxMwaitExit << 10) | ((uint64_t)pBase->fVmxRdpmcExit << 11)
1477 | ((uint64_t)pBase->fVmxRdtscExit << 12) | ((uint64_t)pBase->fVmxCr3LoadExit << 13)
1478 | ((uint64_t)pBase->fVmxCr3StoreExit << 14) | ((uint64_t)pBase->fVmxCr8LoadExit << 15)
1479 | ((uint64_t)pBase->fVmxCr8StoreExit << 16) | ((uint64_t)pBase->fVmxUseTprShadow << 17)
1480 | ((uint64_t)pBase->fVmxNmiWindowExit << 18) | ((uint64_t)pBase->fVmxMovDRxExit << 19)
1481 | ((uint64_t)pBase->fVmxUncondIoExit << 20) | ((uint64_t)pBase->fVmxUseIoBitmaps << 21)
1482 | ((uint64_t)pBase->fVmxMonitorTrapFlag << 22) | ((uint64_t)pBase->fVmxUseMsrBitmaps << 23)
1483 | ((uint64_t)pBase->fVmxMonitorExit << 24) | ((uint64_t)pBase->fVmxPauseExit << 25)
1484 | ((uint64_t)pBase->fVmxSecondaryExecCtls << 26) | ((uint64_t)pBase->fVmxVirtApicAccess << 27)
1485 | ((uint64_t)pBase->fVmxEpt << 28) | ((uint64_t)pBase->fVmxDescTableExit << 29)
1486 | ((uint64_t)pBase->fVmxRdtscp << 30) | ((uint64_t)pBase->fVmxVirtX2ApicMode << 31)
1487 | ((uint64_t)pBase->fVmxVpid << 32) | ((uint64_t)pBase->fVmxWbinvdExit << 33)
1488 | ((uint64_t)pBase->fVmxUnrestrictedGuest << 34) | ((uint64_t)pBase->fVmxApicRegVirt << 35)
1489 | ((uint64_t)pBase->fVmxVirtIntDelivery << 36) | ((uint64_t)pBase->fVmxPauseLoopExit << 37)
1490 | ((uint64_t)pBase->fVmxRdrandExit << 38) | ((uint64_t)pBase->fVmxInvpcid << 39)
1491 | ((uint64_t)pBase->fVmxVmFunc << 40) | ((uint64_t)pBase->fVmxVmcsShadowing << 41)
1492 | ((uint64_t)pBase->fVmxRdseedExit << 42) | ((uint64_t)pBase->fVmxPml << 43)
1493 | ((uint64_t)pBase->fVmxEptXcptVe << 44) | ((uint64_t)pBase->fVmxXsavesXrstors << 45)
1494 | ((uint64_t)pBase->fVmxUseTscScaling << 46) | ((uint64_t)pBase->fVmxEntryLoadDebugCtls << 47)
1495 | ((uint64_t)pBase->fVmxIa32eModeGuest << 48) | ((uint64_t)pBase->fVmxEntryLoadEferMsr << 49)
1496 | ((uint64_t)pBase->fVmxEntryLoadPatMsr << 50) | ((uint64_t)pBase->fVmxExitSaveDebugCtls << 51)
1497 | ((uint64_t)pBase->fVmxHostAddrSpaceSize << 52) | ((uint64_t)pBase->fVmxExitAckExtInt << 53)
1498 | ((uint64_t)pBase->fVmxExitSavePatMsr << 54) | ((uint64_t)pBase->fVmxExitLoadPatMsr << 55)
1499 | ((uint64_t)pBase->fVmxExitSaveEferMsr << 56) | ((uint64_t)pBase->fVmxExitLoadEferMsr << 57)
1500 | ((uint64_t)pBase->fVmxSavePreemptTimer << 58) | ((uint64_t)pBase->fVmxExitSaveEferLma << 59)
1501 | ((uint64_t)pBase->fVmxIntelPt << 60) | ((uint64_t)pBase->fVmxVmwriteAll << 61)
1502 | ((uint64_t)pBase->fVmxEntryInjectSoftInt << 62);
1503
1504 uint64_t const fGst = ((uint64_t)pGst->fVmxInsOutInfo << 0) | ((uint64_t)pGst->fVmxExtIntExit << 1)
1505 | ((uint64_t)pGst->fVmxNmiExit << 2) | ((uint64_t)pGst->fVmxVirtNmi << 3)
1506 | ((uint64_t)pGst->fVmxPreemptTimer << 4) | ((uint64_t)pGst->fVmxPostedInt << 5)
1507 | ((uint64_t)pGst->fVmxIntWindowExit << 6) | ((uint64_t)pGst->fVmxTscOffsetting << 7)
1508 | ((uint64_t)pGst->fVmxHltExit << 8) | ((uint64_t)pGst->fVmxInvlpgExit << 9)
1509 | ((uint64_t)pGst->fVmxMwaitExit << 10) | ((uint64_t)pGst->fVmxRdpmcExit << 11)
1510 | ((uint64_t)pGst->fVmxRdtscExit << 12) | ((uint64_t)pGst->fVmxCr3LoadExit << 13)
1511 | ((uint64_t)pGst->fVmxCr3StoreExit << 14) | ((uint64_t)pGst->fVmxCr8LoadExit << 15)
1512 | ((uint64_t)pGst->fVmxCr8StoreExit << 16) | ((uint64_t)pGst->fVmxUseTprShadow << 17)
1513 | ((uint64_t)pGst->fVmxNmiWindowExit << 18) | ((uint64_t)pGst->fVmxMovDRxExit << 19)
1514 | ((uint64_t)pGst->fVmxUncondIoExit << 20) | ((uint64_t)pGst->fVmxUseIoBitmaps << 21)
1515 | ((uint64_t)pGst->fVmxMonitorTrapFlag << 22) | ((uint64_t)pGst->fVmxUseMsrBitmaps << 23)
1516 | ((uint64_t)pGst->fVmxMonitorExit << 24) | ((uint64_t)pGst->fVmxPauseExit << 25)
1517 | ((uint64_t)pGst->fVmxSecondaryExecCtls << 26) | ((uint64_t)pGst->fVmxVirtApicAccess << 27)
1518 | ((uint64_t)pGst->fVmxEpt << 28) | ((uint64_t)pGst->fVmxDescTableExit << 29)
1519 | ((uint64_t)pGst->fVmxRdtscp << 30) | ((uint64_t)pGst->fVmxVirtX2ApicMode << 31)
1520 | ((uint64_t)pGst->fVmxVpid << 32) | ((uint64_t)pGst->fVmxWbinvdExit << 33)
1521 | ((uint64_t)pGst->fVmxUnrestrictedGuest << 34) | ((uint64_t)pGst->fVmxApicRegVirt << 35)
1522 | ((uint64_t)pGst->fVmxVirtIntDelivery << 36) | ((uint64_t)pGst->fVmxPauseLoopExit << 37)
1523 | ((uint64_t)pGst->fVmxRdrandExit << 38) | ((uint64_t)pGst->fVmxInvpcid << 39)
1524 | ((uint64_t)pGst->fVmxVmFunc << 40) | ((uint64_t)pGst->fVmxVmcsShadowing << 41)
1525 | ((uint64_t)pGst->fVmxRdseedExit << 42) | ((uint64_t)pGst->fVmxPml << 43)
1526 | ((uint64_t)pGst->fVmxEptXcptVe << 44) | ((uint64_t)pGst->fVmxXsavesXrstors << 45)
1527 | ((uint64_t)pGst->fVmxUseTscScaling << 46) | ((uint64_t)pGst->fVmxEntryLoadDebugCtls << 47)
1528 | ((uint64_t)pGst->fVmxIa32eModeGuest << 48) | ((uint64_t)pGst->fVmxEntryLoadEferMsr << 49)
1529 | ((uint64_t)pGst->fVmxEntryLoadPatMsr << 50) | ((uint64_t)pGst->fVmxExitSaveDebugCtls << 51)
1530 | ((uint64_t)pGst->fVmxHostAddrSpaceSize << 52) | ((uint64_t)pGst->fVmxExitAckExtInt << 53)
1531 | ((uint64_t)pGst->fVmxExitSavePatMsr << 54) | ((uint64_t)pGst->fVmxExitLoadPatMsr << 55)
1532 | ((uint64_t)pGst->fVmxExitSaveEferMsr << 56) | ((uint64_t)pGst->fVmxExitLoadEferMsr << 57)
1533 | ((uint64_t)pGst->fVmxSavePreemptTimer << 58) | ((uint64_t)pGst->fVmxExitSaveEferLma << 59)
1534 | ((uint64_t)pGst->fVmxIntelPt << 60) | ((uint64_t)pGst->fVmxVmwriteAll << 61)
1535 | ((uint64_t)pGst->fVmxEntryInjectSoftInt << 62);
1536
1537 if ((fBase | fGst) != fBase)
1538 return false;
1539 return true;
1540 }
1541 return true;
1542}
1543#endif
1544
1545
1546/**
1547 * Initializes VMX guest features and MSRs.
1548 *
1549 * @param pVM The cross context VM structure.
1550 * @param pHostVmxMsrs The host VMX MSRs. Pass NULL when fully emulating VMX
1551 * and no hardware-assisted nested-guest execution is
1552 * possible for this VM.
1553 * @param pGuestVmxMsrs Where to store the initialized guest VMX MSRs.
1554 */
1555void cpumR3InitVmxGuestFeaturesAndMsrs(PVM pVM, PCVMXMSRS pHostVmxMsrs, PVMXMSRS pGuestVmxMsrs)
1556{
1557 Assert(pVM);
1558 Assert(pGuestVmxMsrs);
1559
1560 /*
1561 * Initialize the set of VMX features we emulate.
1562 *
1563 * Note! Some bits might be reported as 1 always if they fall under the
1564 * default1 class bits (e.g. fVmxEntryLoadDebugCtls), see @bugref{9180#c5}.
1565 */
1566 CPUMFEATURES EmuFeat;
1567 RT_ZERO(EmuFeat);
1568 EmuFeat.fVmx = 1;
1569 EmuFeat.fVmxInsOutInfo = 0;
1570 EmuFeat.fVmxExtIntExit = 1;
1571 EmuFeat.fVmxNmiExit = 1;
1572 EmuFeat.fVmxVirtNmi = 0;
1573 EmuFeat.fVmxPreemptTimer = 0; /** @todo NSTVMX: enable this. */
1574 EmuFeat.fVmxPostedInt = 0;
1575 EmuFeat.fVmxIntWindowExit = 1;
1576 EmuFeat.fVmxTscOffsetting = 1;
1577 EmuFeat.fVmxHltExit = 1;
1578 EmuFeat.fVmxInvlpgExit = 1;
1579 EmuFeat.fVmxMwaitExit = 1;
1580 EmuFeat.fVmxRdpmcExit = 1;
1581 EmuFeat.fVmxRdtscExit = 1;
1582 EmuFeat.fVmxCr3LoadExit = 1;
1583 EmuFeat.fVmxCr3StoreExit = 1;
1584 EmuFeat.fVmxCr8LoadExit = 1;
1585 EmuFeat.fVmxCr8StoreExit = 1;
1586 EmuFeat.fVmxUseTprShadow = 0;
1587 EmuFeat.fVmxNmiWindowExit = 0;
1588 EmuFeat.fVmxMovDRxExit = 1;
1589 EmuFeat.fVmxUncondIoExit = 1;
1590 EmuFeat.fVmxUseIoBitmaps = 1;
1591 EmuFeat.fVmxMonitorTrapFlag = 0;
1592 EmuFeat.fVmxUseMsrBitmaps = 0;
1593 EmuFeat.fVmxMonitorExit = 1;
1594 EmuFeat.fVmxPauseExit = 1;
1595 EmuFeat.fVmxSecondaryExecCtls = 1;
1596 EmuFeat.fVmxVirtApicAccess = 0;
1597 EmuFeat.fVmxEpt = 0;
1598 EmuFeat.fVmxDescTableExit = 1;
1599 EmuFeat.fVmxRdtscp = 1;
1600 EmuFeat.fVmxVirtX2ApicMode = 0;
1601 EmuFeat.fVmxVpid = 0;
1602 EmuFeat.fVmxWbinvdExit = 1;
1603 EmuFeat.fVmxUnrestrictedGuest = 0;
1604 EmuFeat.fVmxApicRegVirt = 0;
1605 EmuFeat.fVmxVirtIntDelivery = 0;
1606 EmuFeat.fVmxPauseLoopExit = 0;
1607 EmuFeat.fVmxRdrandExit = 0;
1608 EmuFeat.fVmxInvpcid = 1;
1609 EmuFeat.fVmxVmFunc = 0;
1610 EmuFeat.fVmxVmcsShadowing = 0;
1611 EmuFeat.fVmxRdseedExit = 0;
1612 EmuFeat.fVmxPml = 0;
1613 EmuFeat.fVmxEptXcptVe = 0;
1614 EmuFeat.fVmxXsavesXrstors = 0;
1615 EmuFeat.fVmxUseTscScaling = 0;
1616 EmuFeat.fVmxEntryLoadDebugCtls = 1;
1617 EmuFeat.fVmxIa32eModeGuest = 1;
1618 EmuFeat.fVmxEntryLoadEferMsr = 1;
1619 EmuFeat.fVmxEntryLoadPatMsr = 0;
1620 EmuFeat.fVmxExitSaveDebugCtls = 1;
1621 EmuFeat.fVmxHostAddrSpaceSize = 1;
1622 EmuFeat.fVmxExitAckExtInt = 0;
1623 EmuFeat.fVmxExitSavePatMsr = 0;
1624 EmuFeat.fVmxExitLoadPatMsr = 0;
1625 EmuFeat.fVmxExitSaveEferMsr = 1;
1626 EmuFeat.fVmxExitLoadEferMsr = 1;
1627 EmuFeat.fVmxSavePreemptTimer = 0;
1628 EmuFeat.fVmxExitSaveEferLma = 1;
1629 EmuFeat.fVmxIntelPt = 0;
1630 EmuFeat.fVmxVmwriteAll = 0;
1631 EmuFeat.fVmxEntryInjectSoftInt = 0;
1632
1633 /*
1634 * Merge guest features.
1635 *
1636 * When hardware-assisted VMX may be used, any feature we emulate must also be supported
1637 * by the hardware, hence we merge our emulated features with the host features below.
1638 */
1639 PCCPUMFEATURES pBaseFeat = cpumR3IsHwAssistNstGstExecAllowed(pVM) ? &pVM->cpum.s.HostFeatures : &EmuFeat;
1640 PCPUMFEATURES pGuestFeat = &pVM->cpum.s.GuestFeatures;
1641 Assert(pBaseFeat->fVmx);
1642 pGuestFeat->fVmxInsOutInfo = (pBaseFeat->fVmxInsOutInfo & EmuFeat.fVmxInsOutInfo );
1643 pGuestFeat->fVmxExtIntExit = (pBaseFeat->fVmxExtIntExit & EmuFeat.fVmxExtIntExit );
1644 pGuestFeat->fVmxNmiExit = (pBaseFeat->fVmxNmiExit & EmuFeat.fVmxNmiExit );
1645 pGuestFeat->fVmxVirtNmi = (pBaseFeat->fVmxVirtNmi & EmuFeat.fVmxVirtNmi );
1646 pGuestFeat->fVmxPreemptTimer = (pBaseFeat->fVmxPreemptTimer & EmuFeat.fVmxPreemptTimer );
1647 pGuestFeat->fVmxPostedInt = (pBaseFeat->fVmxPostedInt & EmuFeat.fVmxPostedInt );
1648 pGuestFeat->fVmxIntWindowExit = (pBaseFeat->fVmxIntWindowExit & EmuFeat.fVmxIntWindowExit );
1649 pGuestFeat->fVmxTscOffsetting = (pBaseFeat->fVmxTscOffsetting & EmuFeat.fVmxTscOffsetting );
1650 pGuestFeat->fVmxHltExit = (pBaseFeat->fVmxHltExit & EmuFeat.fVmxHltExit );
1651 pGuestFeat->fVmxInvlpgExit = (pBaseFeat->fVmxInvlpgExit & EmuFeat.fVmxInvlpgExit );
1652 pGuestFeat->fVmxMwaitExit = (pBaseFeat->fVmxMwaitExit & EmuFeat.fVmxMwaitExit );
1653 pGuestFeat->fVmxRdpmcExit = (pBaseFeat->fVmxRdpmcExit & EmuFeat.fVmxRdpmcExit );
1654 pGuestFeat->fVmxRdtscExit = (pBaseFeat->fVmxRdtscExit & EmuFeat.fVmxRdtscExit );
1655 pGuestFeat->fVmxCr3LoadExit = (pBaseFeat->fVmxCr3LoadExit & EmuFeat.fVmxCr3LoadExit );
1656 pGuestFeat->fVmxCr3StoreExit = (pBaseFeat->fVmxCr3StoreExit & EmuFeat.fVmxCr3StoreExit );
1657 pGuestFeat->fVmxCr8LoadExit = (pBaseFeat->fVmxCr8LoadExit & EmuFeat.fVmxCr8LoadExit );
1658 pGuestFeat->fVmxCr8StoreExit = (pBaseFeat->fVmxCr8StoreExit & EmuFeat.fVmxCr8StoreExit );
1659 pGuestFeat->fVmxUseTprShadow = (pBaseFeat->fVmxUseTprShadow & EmuFeat.fVmxUseTprShadow );
1660 pGuestFeat->fVmxNmiWindowExit = (pBaseFeat->fVmxNmiWindowExit & EmuFeat.fVmxNmiWindowExit );
1661 pGuestFeat->fVmxMovDRxExit = (pBaseFeat->fVmxMovDRxExit & EmuFeat.fVmxMovDRxExit );
1662 pGuestFeat->fVmxUncondIoExit = (pBaseFeat->fVmxUncondIoExit & EmuFeat.fVmxUncondIoExit );
1663 pGuestFeat->fVmxUseIoBitmaps = (pBaseFeat->fVmxUseIoBitmaps & EmuFeat.fVmxUseIoBitmaps );
1664 pGuestFeat->fVmxMonitorTrapFlag = (pBaseFeat->fVmxMonitorTrapFlag & EmuFeat.fVmxMonitorTrapFlag );
1665 pGuestFeat->fVmxUseMsrBitmaps = (pBaseFeat->fVmxUseMsrBitmaps & EmuFeat.fVmxUseMsrBitmaps );
1666 pGuestFeat->fVmxMonitorExit = (pBaseFeat->fVmxMonitorExit & EmuFeat.fVmxMonitorExit );
1667 pGuestFeat->fVmxPauseExit = (pBaseFeat->fVmxPauseExit & EmuFeat.fVmxPauseExit );
1668 pGuestFeat->fVmxSecondaryExecCtls = (pBaseFeat->fVmxSecondaryExecCtls & EmuFeat.fVmxSecondaryExecCtls );
1669 pGuestFeat->fVmxVirtApicAccess = (pBaseFeat->fVmxVirtApicAccess & EmuFeat.fVmxVirtApicAccess );
1670 pGuestFeat->fVmxEpt = (pBaseFeat->fVmxEpt & EmuFeat.fVmxEpt );
1671 pGuestFeat->fVmxDescTableExit = (pBaseFeat->fVmxDescTableExit & EmuFeat.fVmxDescTableExit );
1672 pGuestFeat->fVmxRdtscp = (pBaseFeat->fVmxRdtscp & EmuFeat.fVmxRdtscp );
1673 pGuestFeat->fVmxVirtX2ApicMode = (pBaseFeat->fVmxVirtX2ApicMode & EmuFeat.fVmxVirtX2ApicMode );
1674 pGuestFeat->fVmxVpid = (pBaseFeat->fVmxVpid & EmuFeat.fVmxVpid );
1675 pGuestFeat->fVmxWbinvdExit = (pBaseFeat->fVmxWbinvdExit & EmuFeat.fVmxWbinvdExit );
1676 pGuestFeat->fVmxUnrestrictedGuest = (pBaseFeat->fVmxUnrestrictedGuest & EmuFeat.fVmxUnrestrictedGuest );
1677 pGuestFeat->fVmxApicRegVirt = (pBaseFeat->fVmxApicRegVirt & EmuFeat.fVmxApicRegVirt );
1678 pGuestFeat->fVmxVirtIntDelivery = (pBaseFeat->fVmxVirtIntDelivery & EmuFeat.fVmxVirtIntDelivery );
1679 pGuestFeat->fVmxPauseLoopExit = (pBaseFeat->fVmxPauseLoopExit & EmuFeat.fVmxPauseLoopExit );
1680 pGuestFeat->fVmxRdrandExit = (pBaseFeat->fVmxRdrandExit & EmuFeat.fVmxRdrandExit );
1681 pGuestFeat->fVmxInvpcid = (pBaseFeat->fVmxInvpcid & EmuFeat.fVmxInvpcid );
1682 pGuestFeat->fVmxVmFunc = (pBaseFeat->fVmxVmFunc & EmuFeat.fVmxVmFunc );
1683 pGuestFeat->fVmxVmcsShadowing = (pBaseFeat->fVmxVmcsShadowing & EmuFeat.fVmxVmcsShadowing );
1684 pGuestFeat->fVmxRdseedExit = (pBaseFeat->fVmxRdseedExit & EmuFeat.fVmxRdseedExit );
1685 pGuestFeat->fVmxPml = (pBaseFeat->fVmxPml & EmuFeat.fVmxPml );
1686 pGuestFeat->fVmxEptXcptVe = (pBaseFeat->fVmxEptXcptVe & EmuFeat.fVmxEptXcptVe );
1687 pGuestFeat->fVmxXsavesXrstors = (pBaseFeat->fVmxXsavesXrstors & EmuFeat.fVmxXsavesXrstors );
1688 pGuestFeat->fVmxUseTscScaling = (pBaseFeat->fVmxUseTscScaling & EmuFeat.fVmxUseTscScaling );
1689 pGuestFeat->fVmxEntryLoadDebugCtls = (pBaseFeat->fVmxEntryLoadDebugCtls & EmuFeat.fVmxEntryLoadDebugCtls );
1690 pGuestFeat->fVmxIa32eModeGuest = (pBaseFeat->fVmxIa32eModeGuest & EmuFeat.fVmxIa32eModeGuest );
1691 pGuestFeat->fVmxEntryLoadEferMsr = (pBaseFeat->fVmxEntryLoadEferMsr & EmuFeat.fVmxEntryLoadEferMsr );
1692 pGuestFeat->fVmxEntryLoadPatMsr = (pBaseFeat->fVmxEntryLoadPatMsr & EmuFeat.fVmxEntryLoadPatMsr );
1693 pGuestFeat->fVmxExitSaveDebugCtls = (pBaseFeat->fVmxExitSaveDebugCtls & EmuFeat.fVmxExitSaveDebugCtls );
1694 pGuestFeat->fVmxHostAddrSpaceSize = (pBaseFeat->fVmxHostAddrSpaceSize & EmuFeat.fVmxHostAddrSpaceSize );
1695 pGuestFeat->fVmxExitAckExtInt = (pBaseFeat->fVmxExitAckExtInt & EmuFeat.fVmxExitAckExtInt );
1696 pGuestFeat->fVmxExitSavePatMsr = (pBaseFeat->fVmxExitSavePatMsr & EmuFeat.fVmxExitSavePatMsr );
1697 pGuestFeat->fVmxExitLoadPatMsr = (pBaseFeat->fVmxExitLoadPatMsr & EmuFeat.fVmxExitLoadPatMsr );
1698 pGuestFeat->fVmxExitSaveEferMsr = (pBaseFeat->fVmxExitSaveEferMsr & EmuFeat.fVmxExitSaveEferMsr );
1699 pGuestFeat->fVmxExitLoadEferMsr = (pBaseFeat->fVmxExitLoadEferMsr & EmuFeat.fVmxExitLoadEferMsr );
1700 pGuestFeat->fVmxSavePreemptTimer = (pBaseFeat->fVmxSavePreemptTimer & EmuFeat.fVmxSavePreemptTimer );
1701 pGuestFeat->fVmxExitSaveEferLma = (pBaseFeat->fVmxExitSaveEferLma & EmuFeat.fVmxExitSaveEferLma );
1702 pGuestFeat->fVmxIntelPt = (pBaseFeat->fVmxIntelPt & EmuFeat.fVmxIntelPt );
1703 pGuestFeat->fVmxVmwriteAll = (pBaseFeat->fVmxVmwriteAll & EmuFeat.fVmxVmwriteAll );
1704 pGuestFeat->fVmxEntryInjectSoftInt = (pBaseFeat->fVmxEntryInjectSoftInt & EmuFeat.fVmxEntryInjectSoftInt );
1705
1706 /* Paranoia. */
1707 if (!pGuestFeat->fVmxSecondaryExecCtls)
1708 {
1709 Assert(!pGuestFeat->fVmxVirtApicAccess);
1710 Assert(!pGuestFeat->fVmxEpt);
1711 Assert(!pGuestFeat->fVmxDescTableExit);
1712 Assert(!pGuestFeat->fVmxRdtscp);
1713 Assert(!pGuestFeat->fVmxVirtX2ApicMode);
1714 Assert(!pGuestFeat->fVmxVpid);
1715 Assert(!pGuestFeat->fVmxWbinvdExit);
1716 Assert(!pGuestFeat->fVmxUnrestrictedGuest);
1717 Assert(!pGuestFeat->fVmxApicRegVirt);
1718 Assert(!pGuestFeat->fVmxVirtIntDelivery);
1719 Assert(!pGuestFeat->fVmxPauseLoopExit);
1720 Assert(!pGuestFeat->fVmxRdrandExit);
1721 Assert(!pGuestFeat->fVmxInvpcid);
1722 Assert(!pGuestFeat->fVmxVmFunc);
1723 Assert(!pGuestFeat->fVmxVmcsShadowing);
1724 Assert(!pGuestFeat->fVmxRdseedExit);
1725 Assert(!pGuestFeat->fVmxPml);
1726 Assert(!pGuestFeat->fVmxEptXcptVe);
1727 Assert(!pGuestFeat->fVmxXsavesXrstors);
1728 Assert(!pGuestFeat->fVmxUseTscScaling);
1729 }
1730
1731 /*
1732 * Finally initialize the VMX guest MSRs.
1733 */
1734 cpumR3InitVmxGuestMsrs(pVM, pHostVmxMsrs, pGuestFeat, pGuestVmxMsrs);
1735}
1736
1737
1738/**
1739 * Gets the host hardware-virtualization MSRs.
1740 *
1741 * @returns VBox status code.
1742 * @param pMsrs Where to store the MSRs.
1743 */
1744static int cpumR3GetHostHwvirtMsrs(PCPUMMSRS pMsrs)
1745{
1746 Assert(pMsrs);
1747
1748 uint32_t fCaps = 0;
1749 int rc = SUPR3QueryVTCaps(&fCaps);
1750 if (RT_SUCCESS(rc))
1751 {
1752 if (fCaps & (SUPVTCAPS_VT_X | SUPVTCAPS_AMD_V))
1753 {
1754 SUPHWVIRTMSRS HwvirtMsrs;
1755 rc = SUPR3GetHwvirtMsrs(&HwvirtMsrs, false /* fForceRequery */);
1756 if (RT_SUCCESS(rc))
1757 {
1758 if (fCaps & SUPVTCAPS_VT_X)
1759 HMVmxGetVmxMsrsFromHwvirtMsrs(&HwvirtMsrs, &pMsrs->hwvirt.vmx);
1760 else
1761 HMVmxGetSvmMsrsFromHwvirtMsrs(&HwvirtMsrs, &pMsrs->hwvirt.svm);
1762 return VINF_SUCCESS;
1763 }
1764
1765 LogRel(("CPUM: Querying hardware-virtualization MSRs failed. rc=%Rrc\n", rc));
1766 return rc;
1767 }
1768 else
1769 {
1770 LogRel(("CPUM: Querying hardware-virtualization capability succeeded but did not find VT-x or AMD-V\n"));
1771 return VERR_INTERNAL_ERROR_5;
1772 }
1773 }
1774 else
1775 LogRel(("CPUM: No hardware-virtualization capability detected\n"));
1776
1777 return VINF_SUCCESS;
1778}
1779
1780
1781/**
1782 * Initializes the CPUM.
1783 *
1784 * @returns VBox status code.
1785 * @param pVM The cross context VM structure.
1786 */
1787VMMR3DECL(int) CPUMR3Init(PVM pVM)
1788{
1789 LogFlow(("CPUMR3Init\n"));
1790
1791 /*
1792 * Assert alignment, sizes and tables.
1793 */
1794 AssertCompileMemberAlignment(VM, cpum.s, 32);
1795 AssertCompile(sizeof(pVM->cpum.s) <= sizeof(pVM->cpum.padding));
1796 AssertCompileSizeAlignment(CPUMCTX, 64);
1797 AssertCompileSizeAlignment(CPUMCTXMSRS, 64);
1798 AssertCompileSizeAlignment(CPUMHOSTCTX, 64);
1799 AssertCompileMemberAlignment(VM, cpum, 64);
1800 AssertCompileMemberAlignment(VM, aCpus, 64);
1801 AssertCompileMemberAlignment(VMCPU, cpum.s, 64);
1802 AssertCompileMemberSizeAlignment(VM, aCpus[0].cpum.s, 64);
1803#ifdef VBOX_STRICT
1804 int rc2 = cpumR3MsrStrictInitChecks();
1805 AssertRCReturn(rc2, rc2);
1806#endif
1807
1808 /*
1809 * Initialize offsets.
1810 */
1811
1812 /* Calculate the offset from CPUM to CPUMCPU for the first CPU. */
1813 pVM->cpum.s.offCPUMCPU0 = RT_UOFFSETOF(VM, aCpus[0].cpum) - RT_UOFFSETOF(VM, cpum);
1814 Assert((uintptr_t)&pVM->cpum + pVM->cpum.s.offCPUMCPU0 == (uintptr_t)&pVM->aCpus[0].cpum);
1815
1816
1817 /* Calculate the offset from CPUMCPU to CPUM. */
1818 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1819 {
1820 PVMCPU pVCpu = &pVM->aCpus[i];
1821
1822 pVCpu->cpum.s.offCPUM = RT_UOFFSETOF_DYN(VM, aCpus[i].cpum) - RT_UOFFSETOF(VM, cpum);
1823 Assert((uintptr_t)&pVCpu->cpum - pVCpu->cpum.s.offCPUM == (uintptr_t)&pVM->cpum);
1824 }
1825
1826 /*
1827 * Gather info about the host CPU.
1828 */
1829 if (!ASMHasCpuId())
1830 {
1831 LogRel(("The CPU doesn't support CPUID!\n"));
1832 return VERR_UNSUPPORTED_CPU;
1833 }
1834
1835 pVM->cpum.s.fHostMxCsrMask = CPUMR3DeterminHostMxCsrMask();
1836
1837 CPUMMSRS HostMsrs;
1838 RT_ZERO(HostMsrs);
1839 int rc = cpumR3GetHostHwvirtMsrs(&HostMsrs);
1840 AssertLogRelRCReturn(rc, rc);
1841
1842 PCPUMCPUIDLEAF paLeaves;
1843 uint32_t cLeaves;
1844 rc = CPUMR3CpuIdCollectLeaves(&paLeaves, &cLeaves);
1845 AssertLogRelRCReturn(rc, rc);
1846
1847 rc = cpumR3CpuIdExplodeFeatures(paLeaves, cLeaves, &HostMsrs, &pVM->cpum.s.HostFeatures);
1848 RTMemFree(paLeaves);
1849 AssertLogRelRCReturn(rc, rc);
1850 pVM->cpum.s.GuestFeatures.enmCpuVendor = pVM->cpum.s.HostFeatures.enmCpuVendor;
1851
1852 /*
1853 * Check that the CPU supports the minimum features we require.
1854 */
1855 if (!pVM->cpum.s.HostFeatures.fFxSaveRstor)
1856 return VMSetError(pVM, VERR_UNSUPPORTED_CPU, RT_SRC_POS, "Host CPU does not support the FXSAVE/FXRSTOR instruction.");
1857 if (!pVM->cpum.s.HostFeatures.fMmx)
1858 return VMSetError(pVM, VERR_UNSUPPORTED_CPU, RT_SRC_POS, "Host CPU does not support MMX.");
1859 if (!pVM->cpum.s.HostFeatures.fTsc)
1860 return VMSetError(pVM, VERR_UNSUPPORTED_CPU, RT_SRC_POS, "Host CPU does not support RDTSC.");
1861
1862 /*
1863 * Setup the CR4 AND and OR masks used in the raw-mode switcher.
1864 */
1865 pVM->cpum.s.CR4.AndMask = X86_CR4_OSXMMEEXCPT | X86_CR4_PVI | X86_CR4_VME;
1866 pVM->cpum.s.CR4.OrMask = X86_CR4_OSFXSR;
1867
1868 /*
1869 * Figure out which XSAVE/XRSTOR features are available on the host.
1870 */
1871 uint64_t fXcr0Host = 0;
1872 uint64_t fXStateHostMask = 0;
1873 if ( pVM->cpum.s.HostFeatures.fXSaveRstor
1874 && pVM->cpum.s.HostFeatures.fOpSysXSaveRstor)
1875 {
1876 fXStateHostMask = fXcr0Host = ASMGetXcr0();
1877 fXStateHostMask &= XSAVE_C_X87 | XSAVE_C_SSE | XSAVE_C_YMM | XSAVE_C_OPMASK | XSAVE_C_ZMM_HI256 | XSAVE_C_ZMM_16HI;
1878 AssertLogRelMsgStmt((fXStateHostMask & (XSAVE_C_X87 | XSAVE_C_SSE)) == (XSAVE_C_X87 | XSAVE_C_SSE),
1879 ("%#llx\n", fXStateHostMask), fXStateHostMask = 0);
1880 }
1881 pVM->cpum.s.fXStateHostMask = fXStateHostMask;
1882 if (VM_IS_RAW_MODE_ENABLED(pVM)) /* For raw-mode, we only use XSAVE/XRSTOR when the guest starts using it (CPUID/CR4 visibility). */
1883 fXStateHostMask = 0;
1884 LogRel(("CPUM: fXStateHostMask=%#llx; initial: %#llx; host XCR0=%#llx\n",
1885 pVM->cpum.s.fXStateHostMask, fXStateHostMask, fXcr0Host));
1886
1887 /*
1888 * Allocate memory for the extended CPU state and initialize the host XSAVE/XRSTOR mask.
1889 */
1890 uint32_t cbMaxXState = pVM->cpum.s.HostFeatures.cbMaxExtendedState;
1891 cbMaxXState = RT_ALIGN(cbMaxXState, 128);
1892 AssertLogRelReturn(cbMaxXState >= sizeof(X86FXSTATE) && cbMaxXState <= _8K, VERR_CPUM_IPE_2);
1893
1894 uint8_t *pbXStates;
1895 rc = MMR3HyperAllocOnceNoRelEx(pVM, cbMaxXState * 3 * pVM->cCpus, PAGE_SIZE, MM_TAG_CPUM_CTX,
1896 MMHYPER_AONR_FLAGS_KERNEL_MAPPING, (void **)&pbXStates);
1897 AssertLogRelRCReturn(rc, rc);
1898
1899 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1900 {
1901 PVMCPU pVCpu = &pVM->aCpus[i];
1902
1903 pVCpu->cpum.s.Guest.pXStateR3 = (PX86XSAVEAREA)pbXStates;
1904 pVCpu->cpum.s.Guest.pXStateR0 = MMHyperR3ToR0(pVM, pbXStates);
1905 pVCpu->cpum.s.Guest.pXStateRC = MMHyperR3ToR0(pVM, pbXStates);
1906 pbXStates += cbMaxXState;
1907
1908 pVCpu->cpum.s.Host.pXStateR3 = (PX86XSAVEAREA)pbXStates;
1909 pVCpu->cpum.s.Host.pXStateR0 = MMHyperR3ToR0(pVM, pbXStates);
1910 pVCpu->cpum.s.Host.pXStateRC = MMHyperR3ToR0(pVM, pbXStates);
1911 pbXStates += cbMaxXState;
1912
1913 pVCpu->cpum.s.Hyper.pXStateR3 = (PX86XSAVEAREA)pbXStates;
1914 pVCpu->cpum.s.Hyper.pXStateR0 = MMHyperR3ToR0(pVM, pbXStates);
1915 pVCpu->cpum.s.Hyper.pXStateRC = MMHyperR3ToR0(pVM, pbXStates);
1916 pbXStates += cbMaxXState;
1917
1918 pVCpu->cpum.s.Host.fXStateMask = fXStateHostMask;
1919 }
1920
1921 /*
1922 * Register saved state data item.
1923 */
1924 rc = SSMR3RegisterInternal(pVM, "cpum", 1, CPUM_SAVED_STATE_VERSION, sizeof(CPUM),
1925 NULL, cpumR3LiveExec, NULL,
1926 NULL, cpumR3SaveExec, NULL,
1927 cpumR3LoadPrep, cpumR3LoadExec, cpumR3LoadDone);
1928 if (RT_FAILURE(rc))
1929 return rc;
1930
1931 /*
1932 * Register info handlers and registers with the debugger facility.
1933 */
1934 DBGFR3InfoRegisterInternalEx(pVM, "cpum", "Displays the all the cpu states.",
1935 &cpumR3InfoAll, DBGFINFO_FLAGS_ALL_EMTS);
1936 DBGFR3InfoRegisterInternalEx(pVM, "cpumguest", "Displays the guest cpu state.",
1937 &cpumR3InfoGuest, DBGFINFO_FLAGS_ALL_EMTS);
1938 DBGFR3InfoRegisterInternalEx(pVM, "cpumguesthwvirt", "Displays the guest hwvirt. cpu state.",
1939 &cpumR3InfoGuestHwvirt, DBGFINFO_FLAGS_ALL_EMTS);
1940 DBGFR3InfoRegisterInternalEx(pVM, "cpumhyper", "Displays the hypervisor cpu state.",
1941 &cpumR3InfoHyper, DBGFINFO_FLAGS_ALL_EMTS);
1942 DBGFR3InfoRegisterInternalEx(pVM, "cpumhost", "Displays the host cpu state.",
1943 &cpumR3InfoHost, DBGFINFO_FLAGS_ALL_EMTS);
1944 DBGFR3InfoRegisterInternalEx(pVM, "cpumguestinstr", "Displays the current guest instruction.",
1945 &cpumR3InfoGuestInstr, DBGFINFO_FLAGS_ALL_EMTS);
1946 DBGFR3InfoRegisterInternal( pVM, "cpuid", "Displays the guest cpuid leaves.", &cpumR3CpuIdInfo);
1947 DBGFR3InfoRegisterInternal( pVM, "cpumvmxfeat", "Displays the host and guest VMX hwvirt. features.",
1948 &cpumR3InfoVmxFeatures);
1949
1950 rc = cpumR3DbgInit(pVM);
1951 if (RT_FAILURE(rc))
1952 return rc;
1953
1954 /*
1955 * Check if we need to workaround partial/leaky FPU handling.
1956 */
1957 cpumR3CheckLeakyFpu(pVM);
1958
1959 /*
1960 * Initialize the Guest CPUID and MSR states.
1961 */
1962 rc = cpumR3InitCpuIdAndMsrs(pVM, &HostMsrs);
1963 if (RT_FAILURE(rc))
1964 return rc;
1965
1966 /*
1967 * Allocate memory required by the guest hardware-virtualization structures.
1968 * This must be done after initializing CPUID/MSR features as we access the
1969 * the VMX/SVM guest features below.
1970 */
1971 if (pVM->cpum.s.GuestFeatures.fVmx)
1972 rc = cpumR3AllocVmxHwVirtState(pVM);
1973 else if (pVM->cpum.s.GuestFeatures.fSvm)
1974 rc = cpumR3AllocSvmHwVirtState(pVM);
1975 else
1976 Assert(pVM->aCpus[0].cpum.s.Guest.hwvirt.enmHwvirt == CPUMHWVIRT_NONE);
1977 if (RT_FAILURE(rc))
1978 return rc;
1979
1980 /*
1981 * Workaround for missing cpuid(0) patches when leaf 4 returns GuestInfo.DefCpuId:
1982 * If we miss to patch a cpuid(0).eax then Linux tries to determine the number
1983 * of processors from (cpuid(4).eax >> 26) + 1.
1984 *
1985 * Note: this code is obsolete, but let's keep it here for reference.
1986 * Purpose is valid when we artificially cap the max std id to less than 4.
1987 *
1988 * Note: This used to be a separate function CPUMR3SetHwVirt that was called
1989 * after VMINITCOMPLETED_HM.
1990 */
1991 if (VM_IS_RAW_MODE_ENABLED(pVM))
1992 {
1993 Assert( (pVM->cpum.s.aGuestCpuIdPatmStd[4].uEax & UINT32_C(0xffffc000)) == 0
1994 || pVM->cpum.s.aGuestCpuIdPatmStd[0].uEax < 0x4);
1995 pVM->cpum.s.aGuestCpuIdPatmStd[4].uEax &= UINT32_C(0x00003fff);
1996 }
1997
1998 CPUMR3Reset(pVM);
1999 return VINF_SUCCESS;
2000}
2001
2002
2003/**
2004 * Applies relocations to data and code managed by this
2005 * component. This function will be called at init and
2006 * whenever the VMM need to relocate it self inside the GC.
2007 *
2008 * The CPUM will update the addresses used by the switcher.
2009 *
2010 * @param pVM The cross context VM structure.
2011 */
2012VMMR3DECL(void) CPUMR3Relocate(PVM pVM)
2013{
2014 LogFlow(("CPUMR3Relocate\n"));
2015
2016 pVM->cpum.s.GuestInfo.paMsrRangesRC = MMHyperR3ToRC(pVM, pVM->cpum.s.GuestInfo.paMsrRangesR3);
2017 pVM->cpum.s.GuestInfo.paCpuIdLeavesRC = MMHyperR3ToRC(pVM, pVM->cpum.s.GuestInfo.paCpuIdLeavesR3);
2018
2019 for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
2020 {
2021 PVMCPU pVCpu = &pVM->aCpus[iCpu];
2022 pVCpu->cpum.s.Guest.pXStateRC = MMHyperR3ToRC(pVM, pVCpu->cpum.s.Guest.pXStateR3);
2023 pVCpu->cpum.s.Host.pXStateRC = MMHyperR3ToRC(pVM, pVCpu->cpum.s.Host.pXStateR3);
2024 pVCpu->cpum.s.Hyper.pXStateRC = MMHyperR3ToRC(pVM, pVCpu->cpum.s.Hyper.pXStateR3); /** @todo remove me */
2025
2026 /* Recheck the guest DRx values in raw-mode. */
2027 CPUMRecalcHyperDRx(pVCpu, UINT8_MAX, false);
2028 }
2029}
2030
2031
2032/**
2033 * Terminates the CPUM.
2034 *
2035 * Termination means cleaning up and freeing all resources,
2036 * the VM it self is at this point powered off or suspended.
2037 *
2038 * @returns VBox status code.
2039 * @param pVM The cross context VM structure.
2040 */
2041VMMR3DECL(int) CPUMR3Term(PVM pVM)
2042{
2043#ifdef VBOX_WITH_CRASHDUMP_MAGIC
2044 for (VMCPUID i = 0; i < pVM->cCpus; i++)
2045 {
2046 PVMCPU pVCpu = &pVM->aCpus[i];
2047 PCPUMCTX pCtx = CPUMQueryGuestCtxPtr(pVCpu);
2048
2049 memset(pVCpu->cpum.s.aMagic, 0, sizeof(pVCpu->cpum.s.aMagic));
2050 pVCpu->cpum.s.uMagic = 0;
2051 pCtx->dr[5] = 0;
2052 }
2053#endif
2054
2055 if (pVM->cpum.s.GuestFeatures.fVmx)
2056 cpumR3FreeVmxHwVirtState(pVM);
2057 else if (pVM->cpum.s.GuestFeatures.fSvm)
2058 cpumR3FreeSvmHwVirtState(pVM);
2059 return VINF_SUCCESS;
2060}
2061
2062
2063/**
2064 * Resets a virtual CPU.
2065 *
2066 * Used by CPUMR3Reset and CPU hot plugging.
2067 *
2068 * @param pVM The cross context VM structure.
2069 * @param pVCpu The cross context virtual CPU structure of the CPU that is
2070 * being reset. This may differ from the current EMT.
2071 */
2072VMMR3DECL(void) CPUMR3ResetCpu(PVM pVM, PVMCPU pVCpu)
2073{
2074 /** @todo anything different for VCPU > 0? */
2075 PCPUMCTX pCtx = &pVCpu->cpum.s.Guest;
2076
2077 /*
2078 * Initialize everything to ZERO first.
2079 */
2080 uint32_t fUseFlags = pVCpu->cpum.s.fUseFlags & ~CPUM_USED_FPU_SINCE_REM;
2081
2082 AssertCompile(RTASSERT_OFFSET_OF(CPUMCTX, pXStateR0) < RTASSERT_OFFSET_OF(CPUMCTX, pXStateR3));
2083 AssertCompile(RTASSERT_OFFSET_OF(CPUMCTX, pXStateR0) < RTASSERT_OFFSET_OF(CPUMCTX, pXStateRC));
2084 memset(pCtx, 0, RT_UOFFSETOF(CPUMCTX, pXStateR0));
2085
2086 pVCpu->cpum.s.fUseFlags = fUseFlags;
2087
2088 pCtx->cr0 = X86_CR0_CD | X86_CR0_NW | X86_CR0_ET; //0x60000010
2089 pCtx->eip = 0x0000fff0;
2090 pCtx->edx = 0x00000600; /* P6 processor */
2091 pCtx->eflags.Bits.u1Reserved0 = 1;
2092
2093 pCtx->cs.Sel = 0xf000;
2094 pCtx->cs.ValidSel = 0xf000;
2095 pCtx->cs.fFlags = CPUMSELREG_FLAGS_VALID;
2096 pCtx->cs.u64Base = UINT64_C(0xffff0000);
2097 pCtx->cs.u32Limit = 0x0000ffff;
2098 pCtx->cs.Attr.n.u1DescType = 1; /* code/data segment */
2099 pCtx->cs.Attr.n.u1Present = 1;
2100 pCtx->cs.Attr.n.u4Type = X86_SEL_TYPE_ER_ACC;
2101
2102 pCtx->ds.fFlags = CPUMSELREG_FLAGS_VALID;
2103 pCtx->ds.u32Limit = 0x0000ffff;
2104 pCtx->ds.Attr.n.u1DescType = 1; /* code/data segment */
2105 pCtx->ds.Attr.n.u1Present = 1;
2106 pCtx->ds.Attr.n.u4Type = X86_SEL_TYPE_RW_ACC;
2107
2108 pCtx->es.fFlags = CPUMSELREG_FLAGS_VALID;
2109 pCtx->es.u32Limit = 0x0000ffff;
2110 pCtx->es.Attr.n.u1DescType = 1; /* code/data segment */
2111 pCtx->es.Attr.n.u1Present = 1;
2112 pCtx->es.Attr.n.u4Type = X86_SEL_TYPE_RW_ACC;
2113
2114 pCtx->fs.fFlags = CPUMSELREG_FLAGS_VALID;
2115 pCtx->fs.u32Limit = 0x0000ffff;
2116 pCtx->fs.Attr.n.u1DescType = 1; /* code/data segment */
2117 pCtx->fs.Attr.n.u1Present = 1;
2118 pCtx->fs.Attr.n.u4Type = X86_SEL_TYPE_RW_ACC;
2119
2120 pCtx->gs.fFlags = CPUMSELREG_FLAGS_VALID;
2121 pCtx->gs.u32Limit = 0x0000ffff;
2122 pCtx->gs.Attr.n.u1DescType = 1; /* code/data segment */
2123 pCtx->gs.Attr.n.u1Present = 1;
2124 pCtx->gs.Attr.n.u4Type = X86_SEL_TYPE_RW_ACC;
2125
2126 pCtx->ss.fFlags = CPUMSELREG_FLAGS_VALID;
2127 pCtx->ss.u32Limit = 0x0000ffff;
2128 pCtx->ss.Attr.n.u1Present = 1;
2129 pCtx->ss.Attr.n.u1DescType = 1; /* code/data segment */
2130 pCtx->ss.Attr.n.u4Type = X86_SEL_TYPE_RW_ACC;
2131
2132 pCtx->idtr.cbIdt = 0xffff;
2133 pCtx->gdtr.cbGdt = 0xffff;
2134
2135 pCtx->ldtr.fFlags = CPUMSELREG_FLAGS_VALID;
2136 pCtx->ldtr.u32Limit = 0xffff;
2137 pCtx->ldtr.Attr.n.u1Present = 1;
2138 pCtx->ldtr.Attr.n.u4Type = X86_SEL_TYPE_SYS_LDT;
2139
2140 pCtx->tr.fFlags = CPUMSELREG_FLAGS_VALID;
2141 pCtx->tr.u32Limit = 0xffff;
2142 pCtx->tr.Attr.n.u1Present = 1;
2143 pCtx->tr.Attr.n.u4Type = X86_SEL_TYPE_SYS_386_TSS_BUSY; /* Deduction, not properly documented by Intel. */
2144
2145 pCtx->dr[6] = X86_DR6_INIT_VAL;
2146 pCtx->dr[7] = X86_DR7_INIT_VAL;
2147
2148 PX86FXSTATE pFpuCtx = &pCtx->pXStateR3->x87; AssertReleaseMsg(RT_VALID_PTR(pFpuCtx), ("%p\n", pFpuCtx));
2149 pFpuCtx->FTW = 0x00; /* All empty (abbridged tag reg edition). */
2150 pFpuCtx->FCW = 0x37f;
2151
2152 /* Intel 64 and IA-32 Architectures Software Developer's Manual Volume 3A, Table 8-1.
2153 IA-32 Processor States Following Power-up, Reset, or INIT */
2154 pFpuCtx->MXCSR = 0x1F80;
2155 pFpuCtx->MXCSR_MASK = pVM->cpum.s.GuestInfo.fMxCsrMask; /** @todo check if REM messes this up... */
2156
2157 pCtx->aXcr[0] = XSAVE_C_X87;
2158 if (pVM->cpum.s.HostFeatures.cbMaxExtendedState >= RT_UOFFSETOF(X86XSAVEAREA, Hdr))
2159 {
2160 /* The entire FXSAVE state needs loading when we switch to XSAVE/XRSTOR
2161 as we don't know what happened before. (Bother optimize later?) */
2162 pCtx->pXStateR3->Hdr.bmXState = XSAVE_C_X87 | XSAVE_C_SSE;
2163 }
2164
2165 /*
2166 * MSRs.
2167 */
2168 /* Init PAT MSR */
2169 pCtx->msrPAT = MSR_IA32_CR_PAT_INIT_VAL;
2170
2171 /* EFER MBZ; see AMD64 Architecture Programmer's Manual Volume 2: Table 14-1. Initial Processor State.
2172 * The Intel docs don't mention it. */
2173 Assert(!pCtx->msrEFER);
2174
2175 /* IA32_MISC_ENABLE - not entirely sure what the init/reset state really
2176 is supposed to be here, just trying provide useful/sensible values. */
2177 PCPUMMSRRANGE pRange = cpumLookupMsrRange(pVM, MSR_IA32_MISC_ENABLE);
2178 if (pRange)
2179 {
2180 pVCpu->cpum.s.GuestMsrs.msr.MiscEnable = MSR_IA32_MISC_ENABLE_BTS_UNAVAIL
2181 | MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL
2182 | (pVM->cpum.s.GuestFeatures.fMonitorMWait ? MSR_IA32_MISC_ENABLE_MONITOR : 0)
2183 | MSR_IA32_MISC_ENABLE_FAST_STRINGS;
2184 pRange->fWrIgnMask |= MSR_IA32_MISC_ENABLE_BTS_UNAVAIL
2185 | MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL;
2186 pRange->fWrGpMask &= ~pVCpu->cpum.s.GuestMsrs.msr.MiscEnable;
2187 }
2188
2189 /** @todo Wire IA32_MISC_ENABLE bit 22 to our NT 4 CPUID trick. */
2190
2191 /** @todo r=ramshankar: Currently broken for SMP as TMCpuTickSet() expects to be
2192 * called from each EMT while we're getting called by CPUMR3Reset()
2193 * iteratively on the same thread. Fix later. */
2194#if 0 /** @todo r=bird: This we will do in TM, not here. */
2195 /* TSC must be 0. Intel spec. Table 9-1. "IA-32 Processor States Following Power-up, Reset, or INIT." */
2196 CPUMSetGuestMsr(pVCpu, MSR_IA32_TSC, 0);
2197#endif
2198
2199
2200 /* C-state control. Guesses. */
2201 pVCpu->cpum.s.GuestMsrs.msr.PkgCStateCfgCtrl = 1 /*C1*/ | RT_BIT_32(25) | RT_BIT_32(26) | RT_BIT_32(27) | RT_BIT_32(28);
2202 /* For Nehalem+ and Atoms, the 0xE2 MSR (MSR_PKG_CST_CONFIG_CONTROL) is documented. For Core 2,
2203 * it's undocumented but exists as MSR_PMG_CST_CONFIG_CONTROL and has similar but not identical
2204 * functionality. The default value must be different due to incompatible write mask.
2205 */
2206 if (CPUMMICROARCH_IS_INTEL_CORE2(pVM->cpum.s.GuestFeatures.enmMicroarch))
2207 pVCpu->cpum.s.GuestMsrs.msr.PkgCStateCfgCtrl = 0x202a01; /* From Mac Pro Harpertown, unlocked. */
2208 else if (pVM->cpum.s.GuestFeatures.enmMicroarch == kCpumMicroarch_Intel_Core_Yonah)
2209 pVCpu->cpum.s.GuestMsrs.msr.PkgCStateCfgCtrl = 0x26740c; /* From MacBookPro1,1. */
2210
2211 /*
2212 * Hardware virtualization state.
2213 */
2214 CPUMSetGuestGif(pCtx, true);
2215 Assert(!pVM->cpum.s.GuestFeatures.fVmx || !pVM->cpum.s.GuestFeatures.fSvm); /* Paranoia. */
2216 if (pVM->cpum.s.GuestFeatures.fVmx)
2217 cpumR3ResetVmxHwVirtState(pVCpu);
2218 else if (pVM->cpum.s.GuestFeatures.fSvm)
2219 cpumR3ResetSvmHwVirtState(pVCpu);
2220}
2221
2222
2223/**
2224 * Resets the CPU.
2225 *
2226 * @returns VINF_SUCCESS.
2227 * @param pVM The cross context VM structure.
2228 */
2229VMMR3DECL(void) CPUMR3Reset(PVM pVM)
2230{
2231 for (VMCPUID i = 0; i < pVM->cCpus; i++)
2232 {
2233 CPUMR3ResetCpu(pVM, &pVM->aCpus[i]);
2234
2235#ifdef VBOX_WITH_CRASHDUMP_MAGIC
2236 PCPUMCTX pCtx = &pVM->aCpus[i].cpum.s.Guest;
2237
2238 /* Magic marker for searching in crash dumps. */
2239 strcpy((char *)pVM->aCpus[i].cpum.s.aMagic, "CPUMCPU Magic");
2240 pVM->aCpus[i].cpum.s.uMagic = UINT64_C(0xDEADBEEFDEADBEEF);
2241 pCtx->dr[5] = UINT64_C(0xDEADBEEFDEADBEEF);
2242#endif
2243 }
2244}
2245
2246
2247
2248
2249/**
2250 * Pass 0 live exec callback.
2251 *
2252 * @returns VINF_SSM_DONT_CALL_AGAIN.
2253 * @param pVM The cross context VM structure.
2254 * @param pSSM The saved state handle.
2255 * @param uPass The pass (0).
2256 */
2257static DECLCALLBACK(int) cpumR3LiveExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uPass)
2258{
2259 AssertReturn(uPass == 0, VERR_SSM_UNEXPECTED_PASS);
2260 cpumR3SaveCpuId(pVM, pSSM);
2261 return VINF_SSM_DONT_CALL_AGAIN;
2262}
2263
2264
2265/**
2266 * Execute state save operation.
2267 *
2268 * @returns VBox status code.
2269 * @param pVM The cross context VM structure.
2270 * @param pSSM SSM operation handle.
2271 */
2272static DECLCALLBACK(int) cpumR3SaveExec(PVM pVM, PSSMHANDLE pSSM)
2273{
2274 /*
2275 * Save.
2276 */
2277 SSMR3PutU32(pSSM, pVM->cCpus);
2278 SSMR3PutU32(pSSM, sizeof(pVM->aCpus[0].cpum.s.GuestMsrs.msr));
2279 for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
2280 {
2281 PVMCPU pVCpu = &pVM->aCpus[iCpu];
2282
2283 SSMR3PutStructEx(pSSM, &pVCpu->cpum.s.Hyper, sizeof(pVCpu->cpum.s.Hyper), 0, g_aCpumCtxFields, NULL);
2284
2285 PCPUMCTX pGstCtx = &pVCpu->cpum.s.Guest;
2286 SSMR3PutStructEx(pSSM, pGstCtx, sizeof(*pGstCtx), 0, g_aCpumCtxFields, NULL);
2287 SSMR3PutStructEx(pSSM, &pGstCtx->pXStateR3->x87, sizeof(pGstCtx->pXStateR3->x87), 0, g_aCpumX87Fields, NULL);
2288 if (pGstCtx->fXStateMask != 0)
2289 SSMR3PutStructEx(pSSM, &pGstCtx->pXStateR3->Hdr, sizeof(pGstCtx->pXStateR3->Hdr), 0, g_aCpumXSaveHdrFields, NULL);
2290 if (pGstCtx->fXStateMask & XSAVE_C_YMM)
2291 {
2292 PCX86XSAVEYMMHI pYmmHiCtx = CPUMCTX_XSAVE_C_PTR(pGstCtx, XSAVE_C_YMM_BIT, PCX86XSAVEYMMHI);
2293 SSMR3PutStructEx(pSSM, pYmmHiCtx, sizeof(*pYmmHiCtx), SSMSTRUCT_FLAGS_FULL_STRUCT, g_aCpumYmmHiFields, NULL);
2294 }
2295 if (pGstCtx->fXStateMask & XSAVE_C_BNDREGS)
2296 {
2297 PCX86XSAVEBNDREGS pBndRegs = CPUMCTX_XSAVE_C_PTR(pGstCtx, XSAVE_C_BNDREGS_BIT, PCX86XSAVEBNDREGS);
2298 SSMR3PutStructEx(pSSM, pBndRegs, sizeof(*pBndRegs), SSMSTRUCT_FLAGS_FULL_STRUCT, g_aCpumBndRegsFields, NULL);
2299 }
2300 if (pGstCtx->fXStateMask & XSAVE_C_BNDCSR)
2301 {
2302 PCX86XSAVEBNDCFG pBndCfg = CPUMCTX_XSAVE_C_PTR(pGstCtx, XSAVE_C_BNDCSR_BIT, PCX86XSAVEBNDCFG);
2303 SSMR3PutStructEx(pSSM, pBndCfg, sizeof(*pBndCfg), SSMSTRUCT_FLAGS_FULL_STRUCT, g_aCpumBndCfgFields, NULL);
2304 }
2305 if (pGstCtx->fXStateMask & XSAVE_C_ZMM_HI256)
2306 {
2307 PCX86XSAVEZMMHI256 pZmmHi256 = CPUMCTX_XSAVE_C_PTR(pGstCtx, XSAVE_C_ZMM_HI256_BIT, PCX86XSAVEZMMHI256);
2308 SSMR3PutStructEx(pSSM, pZmmHi256, sizeof(*pZmmHi256), SSMSTRUCT_FLAGS_FULL_STRUCT, g_aCpumZmmHi256Fields, NULL);
2309 }
2310 if (pGstCtx->fXStateMask & XSAVE_C_ZMM_16HI)
2311 {
2312 PCX86XSAVEZMM16HI pZmm16Hi = CPUMCTX_XSAVE_C_PTR(pGstCtx, XSAVE_C_ZMM_16HI_BIT, PCX86XSAVEZMM16HI);
2313 SSMR3PutStructEx(pSSM, pZmm16Hi, sizeof(*pZmm16Hi), SSMSTRUCT_FLAGS_FULL_STRUCT, g_aCpumZmm16HiFields, NULL);
2314 }
2315 if (pVM->cpum.s.GuestFeatures.fSvm)
2316 {
2317 Assert(pGstCtx->hwvirt.svm.CTX_SUFF(pVmcb));
2318 SSMR3PutU64(pSSM, pGstCtx->hwvirt.svm.uMsrHSavePa);
2319 SSMR3PutGCPhys(pSSM, pGstCtx->hwvirt.svm.GCPhysVmcb);
2320 SSMR3PutU64(pSSM, pGstCtx->hwvirt.svm.uPrevPauseTick);
2321 SSMR3PutU16(pSSM, pGstCtx->hwvirt.svm.cPauseFilter);
2322 SSMR3PutU16(pSSM, pGstCtx->hwvirt.svm.cPauseFilterThreshold);
2323 SSMR3PutBool(pSSM, pGstCtx->hwvirt.svm.fInterceptEvents);
2324 SSMR3PutStructEx(pSSM, &pGstCtx->hwvirt.svm.HostState, sizeof(pGstCtx->hwvirt.svm.HostState), 0 /* fFlags */,
2325 g_aSvmHwvirtHostState, NULL /* pvUser */);
2326 SSMR3PutMem(pSSM, pGstCtx->hwvirt.svm.pVmcbR3, SVM_VMCB_PAGES << X86_PAGE_4K_SHIFT);
2327 SSMR3PutMem(pSSM, pGstCtx->hwvirt.svm.pvMsrBitmapR3, SVM_MSRPM_PAGES << X86_PAGE_4K_SHIFT);
2328 SSMR3PutMem(pSSM, pGstCtx->hwvirt.svm.pvIoBitmapR3, SVM_IOPM_PAGES << X86_PAGE_4K_SHIFT);
2329 SSMR3PutU32(pSSM, pGstCtx->hwvirt.fLocalForcedActions);
2330 SSMR3PutBool(pSSM, pGstCtx->hwvirt.fGif);
2331 }
2332 SSMR3PutU32(pSSM, pVCpu->cpum.s.fUseFlags);
2333 SSMR3PutU32(pSSM, pVCpu->cpum.s.fChanged);
2334 AssertCompileSizeAlignment(pVCpu->cpum.s.GuestMsrs.msr, sizeof(uint64_t));
2335 SSMR3PutMem(pSSM, &pVCpu->cpum.s.GuestMsrs, sizeof(pVCpu->cpum.s.GuestMsrs.msr));
2336 }
2337
2338 cpumR3SaveCpuId(pVM, pSSM);
2339 return VINF_SUCCESS;
2340}
2341
2342
2343/**
2344 * @callback_method_impl{FNSSMINTLOADPREP}
2345 */
2346static DECLCALLBACK(int) cpumR3LoadPrep(PVM pVM, PSSMHANDLE pSSM)
2347{
2348 NOREF(pSSM);
2349 pVM->cpum.s.fPendingRestore = true;
2350 return VINF_SUCCESS;
2351}
2352
2353
2354/**
2355 * @callback_method_impl{FNSSMINTLOADEXEC}
2356 */
2357static DECLCALLBACK(int) cpumR3LoadExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
2358{
2359 int rc; /* Only for AssertRCReturn use. */
2360
2361 /*
2362 * Validate version.
2363 */
2364 if ( uVersion != CPUM_SAVED_STATE_VERSION_HWVIRT_SVM
2365 && uVersion != CPUM_SAVED_STATE_VERSION_XSAVE
2366 && uVersion != CPUM_SAVED_STATE_VERSION_GOOD_CPUID_COUNT
2367 && uVersion != CPUM_SAVED_STATE_VERSION_BAD_CPUID_COUNT
2368 && uVersion != CPUM_SAVED_STATE_VERSION_PUT_STRUCT
2369 && uVersion != CPUM_SAVED_STATE_VERSION_MEM
2370 && uVersion != CPUM_SAVED_STATE_VERSION_NO_MSR_SIZE
2371 && uVersion != CPUM_SAVED_STATE_VERSION_VER3_2
2372 && uVersion != CPUM_SAVED_STATE_VERSION_VER3_0
2373 && uVersion != CPUM_SAVED_STATE_VERSION_VER2_1_NOMSR
2374 && uVersion != CPUM_SAVED_STATE_VERSION_VER2_0
2375 && uVersion != CPUM_SAVED_STATE_VERSION_VER1_6)
2376 {
2377 AssertMsgFailed(("cpumR3LoadExec: Invalid version uVersion=%d!\n", uVersion));
2378 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
2379 }
2380
2381 if (uPass == SSM_PASS_FINAL)
2382 {
2383 /*
2384 * Set the size of RTGCPTR for SSMR3GetGCPtr. (Only necessary for
2385 * really old SSM file versions.)
2386 */
2387 if (uVersion == CPUM_SAVED_STATE_VERSION_VER1_6)
2388 SSMR3HandleSetGCPtrSize(pSSM, sizeof(RTGCPTR32));
2389 else if (uVersion <= CPUM_SAVED_STATE_VERSION_VER3_0)
2390 SSMR3HandleSetGCPtrSize(pSSM, HC_ARCH_BITS == 32 ? sizeof(RTGCPTR32) : sizeof(RTGCPTR));
2391
2392 /*
2393 * Figure x86 and ctx field definitions to use for older states.
2394 */
2395 uint32_t const fLoad = uVersion > CPUM_SAVED_STATE_VERSION_MEM ? 0 : SSMSTRUCT_FLAGS_MEM_BAND_AID_RELAXED;
2396 PCSSMFIELD paCpumCtx1Fields = g_aCpumX87Fields;
2397 PCSSMFIELD paCpumCtx2Fields = g_aCpumCtxFields;
2398 if (uVersion == CPUM_SAVED_STATE_VERSION_VER1_6)
2399 {
2400 paCpumCtx1Fields = g_aCpumX87FieldsV16;
2401 paCpumCtx2Fields = g_aCpumCtxFieldsV16;
2402 }
2403 else if (uVersion <= CPUM_SAVED_STATE_VERSION_MEM)
2404 {
2405 paCpumCtx1Fields = g_aCpumX87FieldsMem;
2406 paCpumCtx2Fields = g_aCpumCtxFieldsMem;
2407 }
2408
2409 /*
2410 * The hyper state used to preceed the CPU count. Starting with
2411 * XSAVE it was moved down till after we've got the count.
2412 */
2413 if (uVersion < CPUM_SAVED_STATE_VERSION_XSAVE)
2414 {
2415 for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
2416 {
2417 PVMCPU pVCpu = &pVM->aCpus[iCpu];
2418 X86FXSTATE Ign;
2419 SSMR3GetStructEx(pSSM, &Ign, sizeof(Ign), fLoad | SSMSTRUCT_FLAGS_NO_TAIL_MARKER, paCpumCtx1Fields, NULL);
2420 uint64_t uCR3 = pVCpu->cpum.s.Hyper.cr3;
2421 uint64_t uRSP = pVCpu->cpum.s.Hyper.rsp; /* see VMMR3Relocate(). */
2422 SSMR3GetStructEx(pSSM, &pVCpu->cpum.s.Hyper, sizeof(pVCpu->cpum.s.Hyper),
2423 fLoad | SSMSTRUCT_FLAGS_NO_LEAD_MARKER, paCpumCtx2Fields, NULL);
2424 pVCpu->cpum.s.Hyper.cr3 = uCR3;
2425 pVCpu->cpum.s.Hyper.rsp = uRSP;
2426 }
2427 }
2428
2429 if (uVersion >= CPUM_SAVED_STATE_VERSION_VER2_1_NOMSR)
2430 {
2431 uint32_t cCpus;
2432 rc = SSMR3GetU32(pSSM, &cCpus); AssertRCReturn(rc, rc);
2433 AssertLogRelMsgReturn(cCpus == pVM->cCpus, ("Mismatching CPU counts: saved: %u; configured: %u \n", cCpus, pVM->cCpus),
2434 VERR_SSM_UNEXPECTED_DATA);
2435 }
2436 AssertLogRelMsgReturn( uVersion > CPUM_SAVED_STATE_VERSION_VER2_0
2437 || pVM->cCpus == 1,
2438 ("cCpus=%u\n", pVM->cCpus),
2439 VERR_SSM_UNEXPECTED_DATA);
2440
2441 uint32_t cbMsrs = 0;
2442 if (uVersion > CPUM_SAVED_STATE_VERSION_NO_MSR_SIZE)
2443 {
2444 rc = SSMR3GetU32(pSSM, &cbMsrs); AssertRCReturn(rc, rc);
2445 AssertLogRelMsgReturn(RT_ALIGN(cbMsrs, sizeof(uint64_t)) == cbMsrs, ("Size of MSRs is misaligned: %#x\n", cbMsrs),
2446 VERR_SSM_UNEXPECTED_DATA);
2447 AssertLogRelMsgReturn(cbMsrs <= sizeof(CPUMCTXMSRS) && cbMsrs > 0, ("Size of MSRs is out of range: %#x\n", cbMsrs),
2448 VERR_SSM_UNEXPECTED_DATA);
2449 }
2450
2451 /*
2452 * Do the per-CPU restoring.
2453 */
2454 for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
2455 {
2456 PVMCPU pVCpu = &pVM->aCpus[iCpu];
2457 PCPUMCTX pGstCtx = &pVCpu->cpum.s.Guest;
2458
2459 if (uVersion >= CPUM_SAVED_STATE_VERSION_XSAVE)
2460 {
2461 /*
2462 * The XSAVE saved state layout moved the hyper state down here.
2463 */
2464 uint64_t uCR3 = pVCpu->cpum.s.Hyper.cr3;
2465 uint64_t uRSP = pVCpu->cpum.s.Hyper.rsp; /* see VMMR3Relocate(). */
2466 rc = SSMR3GetStructEx(pSSM, &pVCpu->cpum.s.Hyper, sizeof(pVCpu->cpum.s.Hyper), 0, g_aCpumCtxFields, NULL);
2467 pVCpu->cpum.s.Hyper.cr3 = uCR3;
2468 pVCpu->cpum.s.Hyper.rsp = uRSP;
2469 AssertRCReturn(rc, rc);
2470
2471 /*
2472 * Start by restoring the CPUMCTX structure and the X86FXSAVE bits of the extended state.
2473 */
2474 rc = SSMR3GetStructEx(pSSM, pGstCtx, sizeof(*pGstCtx), 0, g_aCpumCtxFields, NULL);
2475 rc = SSMR3GetStructEx(pSSM, &pGstCtx->pXStateR3->x87, sizeof(pGstCtx->pXStateR3->x87), 0, g_aCpumX87Fields, NULL);
2476 AssertRCReturn(rc, rc);
2477
2478 /* Check that the xsave/xrstor mask is valid (invalid results in #GP). */
2479 if (pGstCtx->fXStateMask != 0)
2480 {
2481 AssertLogRelMsgReturn(!(pGstCtx->fXStateMask & ~pVM->cpum.s.fXStateGuestMask),
2482 ("fXStateMask=%#RX64 fXStateGuestMask=%#RX64\n",
2483 pGstCtx->fXStateMask, pVM->cpum.s.fXStateGuestMask),
2484 VERR_CPUM_INCOMPATIBLE_XSAVE_COMP_MASK);
2485 AssertLogRelMsgReturn(pGstCtx->fXStateMask & XSAVE_C_X87,
2486 ("fXStateMask=%#RX64\n", pGstCtx->fXStateMask), VERR_CPUM_INVALID_XSAVE_COMP_MASK);
2487 AssertLogRelMsgReturn((pGstCtx->fXStateMask & (XSAVE_C_SSE | XSAVE_C_YMM)) != XSAVE_C_YMM,
2488 ("fXStateMask=%#RX64\n", pGstCtx->fXStateMask), VERR_CPUM_INVALID_XSAVE_COMP_MASK);
2489 AssertLogRelMsgReturn( (pGstCtx->fXStateMask & (XSAVE_C_OPMASK | XSAVE_C_ZMM_HI256 | XSAVE_C_ZMM_16HI)) == 0
2490 || (pGstCtx->fXStateMask & (XSAVE_C_SSE | XSAVE_C_YMM | XSAVE_C_OPMASK | XSAVE_C_ZMM_HI256 | XSAVE_C_ZMM_16HI))
2491 == (XSAVE_C_SSE | XSAVE_C_YMM | XSAVE_C_OPMASK | XSAVE_C_ZMM_HI256 | XSAVE_C_ZMM_16HI),
2492 ("fXStateMask=%#RX64\n", pGstCtx->fXStateMask), VERR_CPUM_INVALID_XSAVE_COMP_MASK);
2493 }
2494
2495 /* Check that the XCR0 mask is valid (invalid results in #GP). */
2496 AssertLogRelMsgReturn(pGstCtx->aXcr[0] & XSAVE_C_X87, ("xcr0=%#RX64\n", pGstCtx->aXcr[0]), VERR_CPUM_INVALID_XCR0);
2497 if (pGstCtx->aXcr[0] != XSAVE_C_X87)
2498 {
2499 AssertLogRelMsgReturn(!(pGstCtx->aXcr[0] & ~(pGstCtx->fXStateMask | XSAVE_C_X87)),
2500 ("xcr0=%#RX64 fXStateMask=%#RX64\n", pGstCtx->aXcr[0], pGstCtx->fXStateMask),
2501 VERR_CPUM_INVALID_XCR0);
2502 AssertLogRelMsgReturn(pGstCtx->aXcr[0] & XSAVE_C_X87,
2503 ("xcr0=%#RX64\n", pGstCtx->aXcr[0]), VERR_CPUM_INVALID_XSAVE_COMP_MASK);
2504 AssertLogRelMsgReturn((pGstCtx->aXcr[0] & (XSAVE_C_SSE | XSAVE_C_YMM)) != XSAVE_C_YMM,
2505 ("xcr0=%#RX64\n", pGstCtx->aXcr[0]), VERR_CPUM_INVALID_XSAVE_COMP_MASK);
2506 AssertLogRelMsgReturn( (pGstCtx->aXcr[0] & (XSAVE_C_OPMASK | XSAVE_C_ZMM_HI256 | XSAVE_C_ZMM_16HI)) == 0
2507 || (pGstCtx->aXcr[0] & (XSAVE_C_SSE | XSAVE_C_YMM | XSAVE_C_OPMASK | XSAVE_C_ZMM_HI256 | XSAVE_C_ZMM_16HI))
2508 == (XSAVE_C_SSE | XSAVE_C_YMM | XSAVE_C_OPMASK | XSAVE_C_ZMM_HI256 | XSAVE_C_ZMM_16HI),
2509 ("xcr0=%#RX64\n", pGstCtx->aXcr[0]), VERR_CPUM_INVALID_XSAVE_COMP_MASK);
2510 }
2511
2512 /* Check that the XCR1 is zero, as we don't implement it yet. */
2513 AssertLogRelMsgReturn(!pGstCtx->aXcr[1], ("xcr1=%#RX64\n", pGstCtx->aXcr[1]), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2514
2515 /*
2516 * Restore the individual extended state components we support.
2517 */
2518 if (pGstCtx->fXStateMask != 0)
2519 {
2520 rc = SSMR3GetStructEx(pSSM, &pGstCtx->pXStateR3->Hdr, sizeof(pGstCtx->pXStateR3->Hdr),
2521 0, g_aCpumXSaveHdrFields, NULL);
2522 AssertRCReturn(rc, rc);
2523 AssertLogRelMsgReturn(!(pGstCtx->pXStateR3->Hdr.bmXState & ~pGstCtx->fXStateMask),
2524 ("bmXState=%#RX64 fXStateMask=%#RX64\n",
2525 pGstCtx->pXStateR3->Hdr.bmXState, pGstCtx->fXStateMask),
2526 VERR_CPUM_INVALID_XSAVE_HDR);
2527 }
2528 if (pGstCtx->fXStateMask & XSAVE_C_YMM)
2529 {
2530 PX86XSAVEYMMHI pYmmHiCtx = CPUMCTX_XSAVE_C_PTR(pGstCtx, XSAVE_C_YMM_BIT, PX86XSAVEYMMHI);
2531 SSMR3GetStructEx(pSSM, pYmmHiCtx, sizeof(*pYmmHiCtx), SSMSTRUCT_FLAGS_FULL_STRUCT, g_aCpumYmmHiFields, NULL);
2532 }
2533 if (pGstCtx->fXStateMask & XSAVE_C_BNDREGS)
2534 {
2535 PX86XSAVEBNDREGS pBndRegs = CPUMCTX_XSAVE_C_PTR(pGstCtx, XSAVE_C_BNDREGS_BIT, PX86XSAVEBNDREGS);
2536 SSMR3GetStructEx(pSSM, pBndRegs, sizeof(*pBndRegs), SSMSTRUCT_FLAGS_FULL_STRUCT, g_aCpumBndRegsFields, NULL);
2537 }
2538 if (pGstCtx->fXStateMask & XSAVE_C_BNDCSR)
2539 {
2540 PX86XSAVEBNDCFG pBndCfg = CPUMCTX_XSAVE_C_PTR(pGstCtx, XSAVE_C_BNDCSR_BIT, PX86XSAVEBNDCFG);
2541 SSMR3GetStructEx(pSSM, pBndCfg, sizeof(*pBndCfg), SSMSTRUCT_FLAGS_FULL_STRUCT, g_aCpumBndCfgFields, NULL);
2542 }
2543 if (pGstCtx->fXStateMask & XSAVE_C_ZMM_HI256)
2544 {
2545 PX86XSAVEZMMHI256 pZmmHi256 = CPUMCTX_XSAVE_C_PTR(pGstCtx, XSAVE_C_ZMM_HI256_BIT, PX86XSAVEZMMHI256);
2546 SSMR3GetStructEx(pSSM, pZmmHi256, sizeof(*pZmmHi256), SSMSTRUCT_FLAGS_FULL_STRUCT, g_aCpumZmmHi256Fields, NULL);
2547 }
2548 if (pGstCtx->fXStateMask & XSAVE_C_ZMM_16HI)
2549 {
2550 PX86XSAVEZMM16HI pZmm16Hi = CPUMCTX_XSAVE_C_PTR(pGstCtx, XSAVE_C_ZMM_16HI_BIT, PX86XSAVEZMM16HI);
2551 SSMR3GetStructEx(pSSM, pZmm16Hi, sizeof(*pZmm16Hi), SSMSTRUCT_FLAGS_FULL_STRUCT, g_aCpumZmm16HiFields, NULL);
2552 }
2553 if (uVersion >= CPUM_SAVED_STATE_VERSION_HWVIRT_SVM)
2554 {
2555 if (pVM->cpum.s.GuestFeatures.fSvm)
2556 {
2557 Assert(pGstCtx->hwvirt.svm.CTX_SUFF(pVmcb));
2558 SSMR3GetU64(pSSM, &pGstCtx->hwvirt.svm.uMsrHSavePa);
2559 SSMR3GetGCPhys(pSSM, &pGstCtx->hwvirt.svm.GCPhysVmcb);
2560 SSMR3GetU64(pSSM, &pGstCtx->hwvirt.svm.uPrevPauseTick);
2561 SSMR3GetU16(pSSM, &pGstCtx->hwvirt.svm.cPauseFilter);
2562 SSMR3GetU16(pSSM, &pGstCtx->hwvirt.svm.cPauseFilterThreshold);
2563 SSMR3GetBool(pSSM, &pGstCtx->hwvirt.svm.fInterceptEvents);
2564 SSMR3GetStructEx(pSSM, &pGstCtx->hwvirt.svm.HostState, sizeof(pGstCtx->hwvirt.svm.HostState),
2565 0 /* fFlags */, g_aSvmHwvirtHostState, NULL /* pvUser */);
2566 SSMR3GetMem(pSSM, pGstCtx->hwvirt.svm.pVmcbR3, SVM_VMCB_PAGES << X86_PAGE_4K_SHIFT);
2567 SSMR3GetMem(pSSM, pGstCtx->hwvirt.svm.pvMsrBitmapR3, SVM_MSRPM_PAGES << X86_PAGE_4K_SHIFT);
2568 SSMR3GetMem(pSSM, pGstCtx->hwvirt.svm.pvIoBitmapR3, SVM_IOPM_PAGES << X86_PAGE_4K_SHIFT);
2569 SSMR3GetU32(pSSM, &pGstCtx->hwvirt.fLocalForcedActions);
2570 SSMR3GetBool(pSSM, &pGstCtx->hwvirt.fGif);
2571 }
2572 }
2573 /** @todo NSTVMX: Load VMX state. */
2574 }
2575 else
2576 {
2577 /*
2578 * Pre XSAVE saved state.
2579 */
2580 SSMR3GetStructEx(pSSM, &pGstCtx->pXStateR3->x87, sizeof(pGstCtx->pXStateR3->x87),
2581 fLoad | SSMSTRUCT_FLAGS_NO_TAIL_MARKER, paCpumCtx1Fields, NULL);
2582 SSMR3GetStructEx(pSSM, pGstCtx, sizeof(*pGstCtx), fLoad | SSMSTRUCT_FLAGS_NO_LEAD_MARKER, paCpumCtx2Fields, NULL);
2583 }
2584
2585 /*
2586 * Restore a couple of flags and the MSRs.
2587 */
2588 SSMR3GetU32(pSSM, &pVCpu->cpum.s.fUseFlags);
2589 SSMR3GetU32(pSSM, &pVCpu->cpum.s.fChanged);
2590
2591 rc = VINF_SUCCESS;
2592 if (uVersion > CPUM_SAVED_STATE_VERSION_NO_MSR_SIZE)
2593 rc = SSMR3GetMem(pSSM, &pVCpu->cpum.s.GuestMsrs.au64[0], cbMsrs);
2594 else if (uVersion >= CPUM_SAVED_STATE_VERSION_VER3_0)
2595 {
2596 SSMR3GetMem(pSSM, &pVCpu->cpum.s.GuestMsrs.au64[0], 2 * sizeof(uint64_t)); /* Restore two MSRs. */
2597 rc = SSMR3Skip(pSSM, 62 * sizeof(uint64_t));
2598 }
2599 AssertRCReturn(rc, rc);
2600
2601 /* REM and other may have cleared must-be-one fields in DR6 and
2602 DR7, fix these. */
2603 pGstCtx->dr[6] &= ~(X86_DR6_RAZ_MASK | X86_DR6_MBZ_MASK);
2604 pGstCtx->dr[6] |= X86_DR6_RA1_MASK;
2605 pGstCtx->dr[7] &= ~(X86_DR7_RAZ_MASK | X86_DR7_MBZ_MASK);
2606 pGstCtx->dr[7] |= X86_DR7_RA1_MASK;
2607 }
2608
2609 /* Older states does not have the internal selector register flags
2610 and valid selector value. Supply those. */
2611 if (uVersion <= CPUM_SAVED_STATE_VERSION_MEM)
2612 {
2613 for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
2614 {
2615 PVMCPU pVCpu = &pVM->aCpus[iCpu];
2616 bool const fValid = !VM_IS_RAW_MODE_ENABLED(pVM)
2617 || ( uVersion > CPUM_SAVED_STATE_VERSION_VER3_2
2618 && !(pVCpu->cpum.s.fChanged & CPUM_CHANGED_HIDDEN_SEL_REGS_INVALID));
2619 PCPUMSELREG paSelReg = CPUMCTX_FIRST_SREG(&pVCpu->cpum.s.Guest);
2620 if (fValid)
2621 {
2622 for (uint32_t iSelReg = 0; iSelReg < X86_SREG_COUNT; iSelReg++)
2623 {
2624 paSelReg[iSelReg].fFlags = CPUMSELREG_FLAGS_VALID;
2625 paSelReg[iSelReg].ValidSel = paSelReg[iSelReg].Sel;
2626 }
2627
2628 pVCpu->cpum.s.Guest.ldtr.fFlags = CPUMSELREG_FLAGS_VALID;
2629 pVCpu->cpum.s.Guest.ldtr.ValidSel = pVCpu->cpum.s.Guest.ldtr.Sel;
2630 }
2631 else
2632 {
2633 for (uint32_t iSelReg = 0; iSelReg < X86_SREG_COUNT; iSelReg++)
2634 {
2635 paSelReg[iSelReg].fFlags = 0;
2636 paSelReg[iSelReg].ValidSel = 0;
2637 }
2638
2639 /* This might not be 104% correct, but I think it's close
2640 enough for all practical purposes... (REM always loaded
2641 LDTR registers.) */
2642 pVCpu->cpum.s.Guest.ldtr.fFlags = CPUMSELREG_FLAGS_VALID;
2643 pVCpu->cpum.s.Guest.ldtr.ValidSel = pVCpu->cpum.s.Guest.ldtr.Sel;
2644 }
2645 pVCpu->cpum.s.Guest.tr.fFlags = CPUMSELREG_FLAGS_VALID;
2646 pVCpu->cpum.s.Guest.tr.ValidSel = pVCpu->cpum.s.Guest.tr.Sel;
2647 }
2648 }
2649
2650 /* Clear CPUM_CHANGED_HIDDEN_SEL_REGS_INVALID. */
2651 if ( uVersion > CPUM_SAVED_STATE_VERSION_VER3_2
2652 && uVersion <= CPUM_SAVED_STATE_VERSION_MEM)
2653 for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
2654 pVM->aCpus[iCpu].cpum.s.fChanged &= CPUM_CHANGED_HIDDEN_SEL_REGS_INVALID;
2655
2656 /*
2657 * A quick sanity check.
2658 */
2659 for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
2660 {
2661 PVMCPU pVCpu = &pVM->aCpus[iCpu];
2662 AssertLogRelReturn(!(pVCpu->cpum.s.Guest.es.fFlags & ~CPUMSELREG_FLAGS_VALID_MASK), VERR_SSM_UNEXPECTED_DATA);
2663 AssertLogRelReturn(!(pVCpu->cpum.s.Guest.cs.fFlags & ~CPUMSELREG_FLAGS_VALID_MASK), VERR_SSM_UNEXPECTED_DATA);
2664 AssertLogRelReturn(!(pVCpu->cpum.s.Guest.ss.fFlags & ~CPUMSELREG_FLAGS_VALID_MASK), VERR_SSM_UNEXPECTED_DATA);
2665 AssertLogRelReturn(!(pVCpu->cpum.s.Guest.ds.fFlags & ~CPUMSELREG_FLAGS_VALID_MASK), VERR_SSM_UNEXPECTED_DATA);
2666 AssertLogRelReturn(!(pVCpu->cpum.s.Guest.fs.fFlags & ~CPUMSELREG_FLAGS_VALID_MASK), VERR_SSM_UNEXPECTED_DATA);
2667 AssertLogRelReturn(!(pVCpu->cpum.s.Guest.gs.fFlags & ~CPUMSELREG_FLAGS_VALID_MASK), VERR_SSM_UNEXPECTED_DATA);
2668 }
2669 }
2670
2671 pVM->cpum.s.fPendingRestore = false;
2672
2673 /*
2674 * Guest CPUIDs.
2675 */
2676 if (uVersion >= CPUM_SAVED_STATE_VERSION_VER3_2)
2677 {
2678 CPUMMSRS GuestMsrs;
2679 RT_ZERO(GuestMsrs);
2680 if (pVM->cpum.s.GuestFeatures.fVmx)
2681 GuestMsrs.hwvirt.vmx = pVM->aCpus[0].cpum.s.Guest.hwvirt.vmx.Msrs;
2682 return cpumR3LoadCpuId(pVM, pSSM, uVersion, &GuestMsrs);
2683 }
2684 return cpumR3LoadCpuIdPre32(pVM, pSSM, uVersion);
2685}
2686
2687
2688/**
2689 * @callback_method_impl{FNSSMINTLOADDONE}
2690 */
2691static DECLCALLBACK(int) cpumR3LoadDone(PVM pVM, PSSMHANDLE pSSM)
2692{
2693 if (RT_FAILURE(SSMR3HandleGetStatus(pSSM)))
2694 return VINF_SUCCESS;
2695
2696 /* just check this since we can. */ /** @todo Add a SSM unit flag for indicating that it's mandatory during a restore. */
2697 if (pVM->cpum.s.fPendingRestore)
2698 {
2699 LogRel(("CPUM: Missing state!\n"));
2700 return VERR_INTERNAL_ERROR_2;
2701 }
2702
2703 bool const fSupportsLongMode = VMR3IsLongModeAllowed(pVM);
2704 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
2705 {
2706 PVMCPU pVCpu = &pVM->aCpus[idCpu];
2707
2708 /* Notify PGM of the NXE states in case they've changed. */
2709 PGMNotifyNxeChanged(pVCpu, RT_BOOL(pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_NXE));
2710
2711 /* During init. this is done in CPUMR3InitCompleted(). */
2712 if (fSupportsLongMode)
2713 pVCpu->cpum.s.fUseFlags |= CPUM_USE_SUPPORTS_LONGMODE;
2714 }
2715 return VINF_SUCCESS;
2716}
2717
2718
2719/**
2720 * Checks if the CPUM state restore is still pending.
2721 *
2722 * @returns true / false.
2723 * @param pVM The cross context VM structure.
2724 */
2725VMMDECL(bool) CPUMR3IsStateRestorePending(PVM pVM)
2726{
2727 return pVM->cpum.s.fPendingRestore;
2728}
2729
2730
2731/**
2732 * Formats the EFLAGS value into mnemonics.
2733 *
2734 * @param pszEFlags Where to write the mnemonics. (Assumes sufficient buffer space.)
2735 * @param efl The EFLAGS value.
2736 */
2737static void cpumR3InfoFormatFlags(char *pszEFlags, uint32_t efl)
2738{
2739 /*
2740 * Format the flags.
2741 */
2742 static const struct
2743 {
2744 const char *pszSet; const char *pszClear; uint32_t fFlag;
2745 } s_aFlags[] =
2746 {
2747 { "vip",NULL, X86_EFL_VIP },
2748 { "vif",NULL, X86_EFL_VIF },
2749 { "ac", NULL, X86_EFL_AC },
2750 { "vm", NULL, X86_EFL_VM },
2751 { "rf", NULL, X86_EFL_RF },
2752 { "nt", NULL, X86_EFL_NT },
2753 { "ov", "nv", X86_EFL_OF },
2754 { "dn", "up", X86_EFL_DF },
2755 { "ei", "di", X86_EFL_IF },
2756 { "tf", NULL, X86_EFL_TF },
2757 { "nt", "pl", X86_EFL_SF },
2758 { "nz", "zr", X86_EFL_ZF },
2759 { "ac", "na", X86_EFL_AF },
2760 { "po", "pe", X86_EFL_PF },
2761 { "cy", "nc", X86_EFL_CF },
2762 };
2763 char *psz = pszEFlags;
2764 for (unsigned i = 0; i < RT_ELEMENTS(s_aFlags); i++)
2765 {
2766 const char *pszAdd = s_aFlags[i].fFlag & efl ? s_aFlags[i].pszSet : s_aFlags[i].pszClear;
2767 if (pszAdd)
2768 {
2769 strcpy(psz, pszAdd);
2770 psz += strlen(pszAdd);
2771 *psz++ = ' ';
2772 }
2773 }
2774 psz[-1] = '\0';
2775}
2776
2777
2778/**
2779 * Formats a full register dump.
2780 *
2781 * @param pVM The cross context VM structure.
2782 * @param pCtx The context to format.
2783 * @param pCtxCore The context core to format.
2784 * @param pHlp Output functions.
2785 * @param enmType The dump type.
2786 * @param pszPrefix Register name prefix.
2787 */
2788static void cpumR3InfoOne(PVM pVM, PCPUMCTX pCtx, PCCPUMCTXCORE pCtxCore, PCDBGFINFOHLP pHlp, CPUMDUMPTYPE enmType,
2789 const char *pszPrefix)
2790{
2791 NOREF(pVM);
2792
2793 /*
2794 * Format the EFLAGS.
2795 */
2796 uint32_t efl = pCtxCore->eflags.u32;
2797 char szEFlags[80];
2798 cpumR3InfoFormatFlags(&szEFlags[0], efl);
2799
2800 /*
2801 * Format the registers.
2802 */
2803 switch (enmType)
2804 {
2805 case CPUMDUMPTYPE_TERSE:
2806 if (CPUMIsGuestIn64BitCodeEx(pCtx))
2807 pHlp->pfnPrintf(pHlp,
2808 "%srax=%016RX64 %srbx=%016RX64 %srcx=%016RX64 %srdx=%016RX64\n"
2809 "%srsi=%016RX64 %srdi=%016RX64 %sr8 =%016RX64 %sr9 =%016RX64\n"
2810 "%sr10=%016RX64 %sr11=%016RX64 %sr12=%016RX64 %sr13=%016RX64\n"
2811 "%sr14=%016RX64 %sr15=%016RX64\n"
2812 "%srip=%016RX64 %srsp=%016RX64 %srbp=%016RX64 %siopl=%d %*s\n"
2813 "%scs=%04x %sss=%04x %sds=%04x %ses=%04x %sfs=%04x %sgs=%04x %seflags=%08x\n",
2814 pszPrefix, pCtxCore->rax, pszPrefix, pCtxCore->rbx, pszPrefix, pCtxCore->rcx, pszPrefix, pCtxCore->rdx, pszPrefix, pCtxCore->rsi, pszPrefix, pCtxCore->rdi,
2815 pszPrefix, pCtxCore->r8, pszPrefix, pCtxCore->r9, pszPrefix, pCtxCore->r10, pszPrefix, pCtxCore->r11, pszPrefix, pCtxCore->r12, pszPrefix, pCtxCore->r13,
2816 pszPrefix, pCtxCore->r14, pszPrefix, pCtxCore->r15,
2817 pszPrefix, pCtxCore->rip, pszPrefix, pCtxCore->rsp, pszPrefix, pCtxCore->rbp, pszPrefix, X86_EFL_GET_IOPL(efl), *pszPrefix ? 33 : 31, szEFlags,
2818 pszPrefix, pCtxCore->cs.Sel, pszPrefix, pCtxCore->ss.Sel, pszPrefix, pCtxCore->ds.Sel, pszPrefix, pCtxCore->es.Sel,
2819 pszPrefix, pCtxCore->fs.Sel, pszPrefix, pCtxCore->gs.Sel, pszPrefix, efl);
2820 else
2821 pHlp->pfnPrintf(pHlp,
2822 "%seax=%08x %sebx=%08x %secx=%08x %sedx=%08x %sesi=%08x %sedi=%08x\n"
2823 "%seip=%08x %sesp=%08x %sebp=%08x %siopl=%d %*s\n"
2824 "%scs=%04x %sss=%04x %sds=%04x %ses=%04x %sfs=%04x %sgs=%04x %seflags=%08x\n",
2825 pszPrefix, pCtxCore->eax, pszPrefix, pCtxCore->ebx, pszPrefix, pCtxCore->ecx, pszPrefix, pCtxCore->edx, pszPrefix, pCtxCore->esi, pszPrefix, pCtxCore->edi,
2826 pszPrefix, pCtxCore->eip, pszPrefix, pCtxCore->esp, pszPrefix, pCtxCore->ebp, pszPrefix, X86_EFL_GET_IOPL(efl), *pszPrefix ? 33 : 31, szEFlags,
2827 pszPrefix, pCtxCore->cs.Sel, pszPrefix, pCtxCore->ss.Sel, pszPrefix, pCtxCore->ds.Sel, pszPrefix, pCtxCore->es.Sel,
2828 pszPrefix, pCtxCore->fs.Sel, pszPrefix, pCtxCore->gs.Sel, pszPrefix, efl);
2829 break;
2830
2831 case CPUMDUMPTYPE_DEFAULT:
2832 if (CPUMIsGuestIn64BitCodeEx(pCtx))
2833 pHlp->pfnPrintf(pHlp,
2834 "%srax=%016RX64 %srbx=%016RX64 %srcx=%016RX64 %srdx=%016RX64\n"
2835 "%srsi=%016RX64 %srdi=%016RX64 %sr8 =%016RX64 %sr9 =%016RX64\n"
2836 "%sr10=%016RX64 %sr11=%016RX64 %sr12=%016RX64 %sr13=%016RX64\n"
2837 "%sr14=%016RX64 %sr15=%016RX64\n"
2838 "%srip=%016RX64 %srsp=%016RX64 %srbp=%016RX64 %siopl=%d %*s\n"
2839 "%scs=%04x %sss=%04x %sds=%04x %ses=%04x %sfs=%04x %sgs=%04x %str=%04x %seflags=%08x\n"
2840 "%scr0=%08RX64 %scr2=%08RX64 %scr3=%08RX64 %scr4=%08RX64 %sgdtr=%016RX64:%04x %sldtr=%04x\n"
2841 ,
2842 pszPrefix, pCtxCore->rax, pszPrefix, pCtxCore->rbx, pszPrefix, pCtxCore->rcx, pszPrefix, pCtxCore->rdx, pszPrefix, pCtxCore->rsi, pszPrefix, pCtxCore->rdi,
2843 pszPrefix, pCtxCore->r8, pszPrefix, pCtxCore->r9, pszPrefix, pCtxCore->r10, pszPrefix, pCtxCore->r11, pszPrefix, pCtxCore->r12, pszPrefix, pCtxCore->r13,
2844 pszPrefix, pCtxCore->r14, pszPrefix, pCtxCore->r15,
2845 pszPrefix, pCtxCore->rip, pszPrefix, pCtxCore->rsp, pszPrefix, pCtxCore->rbp, pszPrefix, X86_EFL_GET_IOPL(efl), *pszPrefix ? 33 : 31, szEFlags,
2846 pszPrefix, pCtxCore->cs.Sel, pszPrefix, pCtxCore->ss.Sel, pszPrefix, pCtxCore->ds.Sel, pszPrefix, pCtxCore->es.Sel,
2847 pszPrefix, pCtxCore->fs.Sel, pszPrefix, pCtxCore->gs.Sel, pszPrefix, pCtx->tr.Sel, pszPrefix, efl,
2848 pszPrefix, pCtx->cr0, pszPrefix, pCtx->cr2, pszPrefix, pCtx->cr3, pszPrefix, pCtx->cr4,
2849 pszPrefix, pCtx->gdtr.pGdt, pCtx->gdtr.cbGdt, pszPrefix, pCtx->ldtr.Sel);
2850 else
2851 pHlp->pfnPrintf(pHlp,
2852 "%seax=%08x %sebx=%08x %secx=%08x %sedx=%08x %sesi=%08x %sedi=%08x\n"
2853 "%seip=%08x %sesp=%08x %sebp=%08x %siopl=%d %*s\n"
2854 "%scs=%04x %sss=%04x %sds=%04x %ses=%04x %sfs=%04x %sgs=%04x %str=%04x %seflags=%08x\n"
2855 "%scr0=%08RX64 %scr2=%08RX64 %scr3=%08RX64 %scr4=%08RX64 %sgdtr=%08RX64:%04x %sldtr=%04x\n"
2856 ,
2857 pszPrefix, pCtxCore->eax, pszPrefix, pCtxCore->ebx, pszPrefix, pCtxCore->ecx, pszPrefix, pCtxCore->edx, pszPrefix, pCtxCore->esi, pszPrefix, pCtxCore->edi,
2858 pszPrefix, pCtxCore->eip, pszPrefix, pCtxCore->esp, pszPrefix, pCtxCore->ebp, pszPrefix, X86_EFL_GET_IOPL(efl), *pszPrefix ? 33 : 31, szEFlags,
2859 pszPrefix, pCtxCore->cs.Sel, pszPrefix, pCtxCore->ss.Sel, pszPrefix, pCtxCore->ds.Sel, pszPrefix, pCtxCore->es.Sel,
2860 pszPrefix, pCtxCore->fs.Sel, pszPrefix, pCtxCore->gs.Sel, pszPrefix, pCtx->tr.Sel, pszPrefix, efl,
2861 pszPrefix, pCtx->cr0, pszPrefix, pCtx->cr2, pszPrefix, pCtx->cr3, pszPrefix, pCtx->cr4,
2862 pszPrefix, pCtx->gdtr.pGdt, pCtx->gdtr.cbGdt, pszPrefix, pCtx->ldtr.Sel);
2863 break;
2864
2865 case CPUMDUMPTYPE_VERBOSE:
2866 if (CPUMIsGuestIn64BitCodeEx(pCtx))
2867 pHlp->pfnPrintf(pHlp,
2868 "%srax=%016RX64 %srbx=%016RX64 %srcx=%016RX64 %srdx=%016RX64\n"
2869 "%srsi=%016RX64 %srdi=%016RX64 %sr8 =%016RX64 %sr9 =%016RX64\n"
2870 "%sr10=%016RX64 %sr11=%016RX64 %sr12=%016RX64 %sr13=%016RX64\n"
2871 "%sr14=%016RX64 %sr15=%016RX64\n"
2872 "%srip=%016RX64 %srsp=%016RX64 %srbp=%016RX64 %siopl=%d %*s\n"
2873 "%scs={%04x base=%016RX64 limit=%08x flags=%08x}\n"
2874 "%sds={%04x base=%016RX64 limit=%08x flags=%08x}\n"
2875 "%ses={%04x base=%016RX64 limit=%08x flags=%08x}\n"
2876 "%sfs={%04x base=%016RX64 limit=%08x flags=%08x}\n"
2877 "%sgs={%04x base=%016RX64 limit=%08x flags=%08x}\n"
2878 "%sss={%04x base=%016RX64 limit=%08x flags=%08x}\n"
2879 "%scr0=%016RX64 %scr2=%016RX64 %scr3=%016RX64 %scr4=%016RX64\n"
2880 "%sdr0=%016RX64 %sdr1=%016RX64 %sdr2=%016RX64 %sdr3=%016RX64\n"
2881 "%sdr4=%016RX64 %sdr5=%016RX64 %sdr6=%016RX64 %sdr7=%016RX64\n"
2882 "%sgdtr=%016RX64:%04x %sidtr=%016RX64:%04x %seflags=%08x\n"
2883 "%sldtr={%04x base=%08RX64 limit=%08x flags=%08x}\n"
2884 "%str ={%04x base=%08RX64 limit=%08x flags=%08x}\n"
2885 "%sSysEnter={cs=%04llx eip=%016RX64 esp=%016RX64}\n"
2886 ,
2887 pszPrefix, pCtxCore->rax, pszPrefix, pCtxCore->rbx, pszPrefix, pCtxCore->rcx, pszPrefix, pCtxCore->rdx, pszPrefix, pCtxCore->rsi, pszPrefix, pCtxCore->rdi,
2888 pszPrefix, pCtxCore->r8, pszPrefix, pCtxCore->r9, pszPrefix, pCtxCore->r10, pszPrefix, pCtxCore->r11, pszPrefix, pCtxCore->r12, pszPrefix, pCtxCore->r13,
2889 pszPrefix, pCtxCore->r14, pszPrefix, pCtxCore->r15,
2890 pszPrefix, pCtxCore->rip, pszPrefix, pCtxCore->rsp, pszPrefix, pCtxCore->rbp, pszPrefix, X86_EFL_GET_IOPL(efl), *pszPrefix ? 33 : 31, szEFlags,
2891 pszPrefix, pCtxCore->cs.Sel, pCtx->cs.u64Base, pCtx->cs.u32Limit, pCtx->cs.Attr.u,
2892 pszPrefix, pCtxCore->ds.Sel, pCtx->ds.u64Base, pCtx->ds.u32Limit, pCtx->ds.Attr.u,
2893 pszPrefix, pCtxCore->es.Sel, pCtx->es.u64Base, pCtx->es.u32Limit, pCtx->es.Attr.u,
2894 pszPrefix, pCtxCore->fs.Sel, pCtx->fs.u64Base, pCtx->fs.u32Limit, pCtx->fs.Attr.u,
2895 pszPrefix, pCtxCore->gs.Sel, pCtx->gs.u64Base, pCtx->gs.u32Limit, pCtx->gs.Attr.u,
2896 pszPrefix, pCtxCore->ss.Sel, pCtx->ss.u64Base, pCtx->ss.u32Limit, pCtx->ss.Attr.u,
2897 pszPrefix, pCtx->cr0, pszPrefix, pCtx->cr2, pszPrefix, pCtx->cr3, pszPrefix, pCtx->cr4,
2898 pszPrefix, pCtx->dr[0], pszPrefix, pCtx->dr[1], pszPrefix, pCtx->dr[2], pszPrefix, pCtx->dr[3],
2899 pszPrefix, pCtx->dr[4], pszPrefix, pCtx->dr[5], pszPrefix, pCtx->dr[6], pszPrefix, pCtx->dr[7],
2900 pszPrefix, pCtx->gdtr.pGdt, pCtx->gdtr.cbGdt, pszPrefix, pCtx->idtr.pIdt, pCtx->idtr.cbIdt, pszPrefix, efl,
2901 pszPrefix, pCtx->ldtr.Sel, pCtx->ldtr.u64Base, pCtx->ldtr.u32Limit, pCtx->ldtr.Attr.u,
2902 pszPrefix, pCtx->tr.Sel, pCtx->tr.u64Base, pCtx->tr.u32Limit, pCtx->tr.Attr.u,
2903 pszPrefix, pCtx->SysEnter.cs, pCtx->SysEnter.eip, pCtx->SysEnter.esp);
2904 else
2905 pHlp->pfnPrintf(pHlp,
2906 "%seax=%08x %sebx=%08x %secx=%08x %sedx=%08x %sesi=%08x %sedi=%08x\n"
2907 "%seip=%08x %sesp=%08x %sebp=%08x %siopl=%d %*s\n"
2908 "%scs={%04x base=%016RX64 limit=%08x flags=%08x} %sdr0=%08RX64 %sdr1=%08RX64\n"
2909 "%sds={%04x base=%016RX64 limit=%08x flags=%08x} %sdr2=%08RX64 %sdr3=%08RX64\n"
2910 "%ses={%04x base=%016RX64 limit=%08x flags=%08x} %sdr4=%08RX64 %sdr5=%08RX64\n"
2911 "%sfs={%04x base=%016RX64 limit=%08x flags=%08x} %sdr6=%08RX64 %sdr7=%08RX64\n"
2912 "%sgs={%04x base=%016RX64 limit=%08x flags=%08x} %scr0=%08RX64 %scr2=%08RX64\n"
2913 "%sss={%04x base=%016RX64 limit=%08x flags=%08x} %scr3=%08RX64 %scr4=%08RX64\n"
2914 "%sgdtr=%016RX64:%04x %sidtr=%016RX64:%04x %seflags=%08x\n"
2915 "%sldtr={%04x base=%08RX64 limit=%08x flags=%08x}\n"
2916 "%str ={%04x base=%08RX64 limit=%08x flags=%08x}\n"
2917 "%sSysEnter={cs=%04llx eip=%08llx esp=%08llx}\n"
2918 ,
2919 pszPrefix, pCtxCore->eax, pszPrefix, pCtxCore->ebx, pszPrefix, pCtxCore->ecx, pszPrefix, pCtxCore->edx, pszPrefix, pCtxCore->esi, pszPrefix, pCtxCore->edi,
2920 pszPrefix, pCtxCore->eip, pszPrefix, pCtxCore->esp, pszPrefix, pCtxCore->ebp, pszPrefix, X86_EFL_GET_IOPL(efl), *pszPrefix ? 33 : 31, szEFlags,
2921 pszPrefix, pCtxCore->cs.Sel, pCtx->cs.u64Base, pCtx->cs.u32Limit, pCtx->cs.Attr.u, pszPrefix, pCtx->dr[0], pszPrefix, pCtx->dr[1],
2922 pszPrefix, pCtxCore->ds.Sel, pCtx->ds.u64Base, pCtx->ds.u32Limit, pCtx->ds.Attr.u, pszPrefix, pCtx->dr[2], pszPrefix, pCtx->dr[3],
2923 pszPrefix, pCtxCore->es.Sel, pCtx->es.u64Base, pCtx->es.u32Limit, pCtx->es.Attr.u, pszPrefix, pCtx->dr[4], pszPrefix, pCtx->dr[5],
2924 pszPrefix, pCtxCore->fs.Sel, pCtx->fs.u64Base, pCtx->fs.u32Limit, pCtx->fs.Attr.u, pszPrefix, pCtx->dr[6], pszPrefix, pCtx->dr[7],
2925 pszPrefix, pCtxCore->gs.Sel, pCtx->gs.u64Base, pCtx->gs.u32Limit, pCtx->gs.Attr.u, pszPrefix, pCtx->cr0, pszPrefix, pCtx->cr2,
2926 pszPrefix, pCtxCore->ss.Sel, pCtx->ss.u64Base, pCtx->ss.u32Limit, pCtx->ss.Attr.u, pszPrefix, pCtx->cr3, pszPrefix, pCtx->cr4,
2927 pszPrefix, pCtx->gdtr.pGdt, pCtx->gdtr.cbGdt, pszPrefix, pCtx->idtr.pIdt, pCtx->idtr.cbIdt, pszPrefix, efl,
2928 pszPrefix, pCtx->ldtr.Sel, pCtx->ldtr.u64Base, pCtx->ldtr.u32Limit, pCtx->ldtr.Attr.u,
2929 pszPrefix, pCtx->tr.Sel, pCtx->tr.u64Base, pCtx->tr.u32Limit, pCtx->tr.Attr.u,
2930 pszPrefix, pCtx->SysEnter.cs, pCtx->SysEnter.eip, pCtx->SysEnter.esp);
2931
2932 pHlp->pfnPrintf(pHlp, "%sxcr=%016RX64 %sxcr1=%016RX64 %sxss=%016RX64 (fXStateMask=%016RX64)\n",
2933 pszPrefix, pCtx->aXcr[0], pszPrefix, pCtx->aXcr[1],
2934 pszPrefix, UINT64_C(0) /** @todo XSS */, pCtx->fXStateMask);
2935 if (pCtx->CTX_SUFF(pXState))
2936 {
2937 PX86FXSTATE pFpuCtx = &pCtx->CTX_SUFF(pXState)->x87;
2938 pHlp->pfnPrintf(pHlp,
2939 "%sFCW=%04x %sFSW=%04x %sFTW=%04x %sFOP=%04x %sMXCSR=%08x %sMXCSR_MASK=%08x\n"
2940 "%sFPUIP=%08x %sCS=%04x %sRsrvd1=%04x %sFPUDP=%08x %sDS=%04x %sRsvrd2=%04x\n"
2941 ,
2942 pszPrefix, pFpuCtx->FCW, pszPrefix, pFpuCtx->FSW, pszPrefix, pFpuCtx->FTW, pszPrefix, pFpuCtx->FOP,
2943 pszPrefix, pFpuCtx->MXCSR, pszPrefix, pFpuCtx->MXCSR_MASK,
2944 pszPrefix, pFpuCtx->FPUIP, pszPrefix, pFpuCtx->CS, pszPrefix, pFpuCtx->Rsrvd1,
2945 pszPrefix, pFpuCtx->FPUDP, pszPrefix, pFpuCtx->DS, pszPrefix, pFpuCtx->Rsrvd2
2946 );
2947 /*
2948 * The FSAVE style memory image contains ST(0)-ST(7) at increasing addresses,
2949 * not (FP)R0-7 as Intel SDM suggests.
2950 */
2951 unsigned iShift = (pFpuCtx->FSW >> 11) & 7;
2952 for (unsigned iST = 0; iST < RT_ELEMENTS(pFpuCtx->aRegs); iST++)
2953 {
2954 unsigned iFPR = (iST + iShift) % RT_ELEMENTS(pFpuCtx->aRegs);
2955 unsigned uTag = (pFpuCtx->FTW >> (2 * iFPR)) & 3;
2956 char chSign = pFpuCtx->aRegs[iST].au16[4] & 0x8000 ? '-' : '+';
2957 unsigned iInteger = (unsigned)(pFpuCtx->aRegs[iST].au64[0] >> 63);
2958 uint64_t u64Fraction = pFpuCtx->aRegs[iST].au64[0] & UINT64_C(0x7fffffffffffffff);
2959 int iExponent = pFpuCtx->aRegs[iST].au16[4] & 0x7fff;
2960 iExponent -= 16383; /* subtract bias */
2961 /** @todo This isn't entirenly correct and needs more work! */
2962 pHlp->pfnPrintf(pHlp,
2963 "%sST(%u)=%sFPR%u={%04RX16'%08RX32'%08RX32} t%d %c%u.%022llu * 2 ^ %d (*)",
2964 pszPrefix, iST, pszPrefix, iFPR,
2965 pFpuCtx->aRegs[iST].au16[4], pFpuCtx->aRegs[iST].au32[1], pFpuCtx->aRegs[iST].au32[0],
2966 uTag, chSign, iInteger, u64Fraction, iExponent);
2967 if (pFpuCtx->aRegs[iST].au16[5] || pFpuCtx->aRegs[iST].au16[6] || pFpuCtx->aRegs[iST].au16[7])
2968 pHlp->pfnPrintf(pHlp, " res={%04RX16,%04RX16,%04RX16}\n",
2969 pFpuCtx->aRegs[iST].au16[5], pFpuCtx->aRegs[iST].au16[6], pFpuCtx->aRegs[iST].au16[7]);
2970 else
2971 pHlp->pfnPrintf(pHlp, "\n");
2972 }
2973
2974 /* XMM/YMM/ZMM registers. */
2975 if (pCtx->fXStateMask & XSAVE_C_YMM)
2976 {
2977 PCX86XSAVEYMMHI pYmmHiCtx = CPUMCTX_XSAVE_C_PTR(pCtx, XSAVE_C_YMM_BIT, PCX86XSAVEYMMHI);
2978 if (!(pCtx->fXStateMask & XSAVE_C_ZMM_HI256))
2979 for (unsigned i = 0; i < RT_ELEMENTS(pFpuCtx->aXMM); i++)
2980 pHlp->pfnPrintf(pHlp, "%sYMM%u%s=%08RX32'%08RX32'%08RX32'%08RX32'%08RX32'%08RX32'%08RX32'%08RX32\n",
2981 pszPrefix, i, i < 10 ? " " : "",
2982 pYmmHiCtx->aYmmHi[i].au32[3],
2983 pYmmHiCtx->aYmmHi[i].au32[2],
2984 pYmmHiCtx->aYmmHi[i].au32[1],
2985 pYmmHiCtx->aYmmHi[i].au32[0],
2986 pFpuCtx->aXMM[i].au32[3],
2987 pFpuCtx->aXMM[i].au32[2],
2988 pFpuCtx->aXMM[i].au32[1],
2989 pFpuCtx->aXMM[i].au32[0]);
2990 else
2991 {
2992 PCX86XSAVEZMMHI256 pZmmHi256 = CPUMCTX_XSAVE_C_PTR(pCtx, XSAVE_C_ZMM_HI256_BIT, PCX86XSAVEZMMHI256);
2993 for (unsigned i = 0; i < RT_ELEMENTS(pFpuCtx->aXMM); i++)
2994 pHlp->pfnPrintf(pHlp,
2995 "%sZMM%u%s=%08RX32'%08RX32'%08RX32'%08RX32'%08RX32'%08RX32'%08RX32'%08RX32''%08RX32'%08RX32'%08RX32'%08RX32'%08RX32'%08RX32'%08RX32'%08RX32\n",
2996 pszPrefix, i, i < 10 ? " " : "",
2997 pZmmHi256->aHi256Regs[i].au32[7],
2998 pZmmHi256->aHi256Regs[i].au32[6],
2999 pZmmHi256->aHi256Regs[i].au32[5],
3000 pZmmHi256->aHi256Regs[i].au32[4],
3001 pZmmHi256->aHi256Regs[i].au32[3],
3002 pZmmHi256->aHi256Regs[i].au32[2],
3003 pZmmHi256->aHi256Regs[i].au32[1],
3004 pZmmHi256->aHi256Regs[i].au32[0],
3005 pYmmHiCtx->aYmmHi[i].au32[3],
3006 pYmmHiCtx->aYmmHi[i].au32[2],
3007 pYmmHiCtx->aYmmHi[i].au32[1],
3008 pYmmHiCtx->aYmmHi[i].au32[0],
3009 pFpuCtx->aXMM[i].au32[3],
3010 pFpuCtx->aXMM[i].au32[2],
3011 pFpuCtx->aXMM[i].au32[1],
3012 pFpuCtx->aXMM[i].au32[0]);
3013
3014 PCX86XSAVEZMM16HI pZmm16Hi = CPUMCTX_XSAVE_C_PTR(pCtx, XSAVE_C_ZMM_16HI_BIT, PCX86XSAVEZMM16HI);
3015 for (unsigned i = 0; i < RT_ELEMENTS(pZmm16Hi->aRegs); i++)
3016 pHlp->pfnPrintf(pHlp,
3017 "%sZMM%u=%08RX32'%08RX32'%08RX32'%08RX32'%08RX32'%08RX32'%08RX32'%08RX32''%08RX32'%08RX32'%08RX32'%08RX32'%08RX32'%08RX32'%08RX32'%08RX32\n",
3018 pszPrefix, i + 16,
3019 pZmm16Hi->aRegs[i].au32[15],
3020 pZmm16Hi->aRegs[i].au32[14],
3021 pZmm16Hi->aRegs[i].au32[13],
3022 pZmm16Hi->aRegs[i].au32[12],
3023 pZmm16Hi->aRegs[i].au32[11],
3024 pZmm16Hi->aRegs[i].au32[10],
3025 pZmm16Hi->aRegs[i].au32[9],
3026 pZmm16Hi->aRegs[i].au32[8],
3027 pZmm16Hi->aRegs[i].au32[7],
3028 pZmm16Hi->aRegs[i].au32[6],
3029 pZmm16Hi->aRegs[i].au32[5],
3030 pZmm16Hi->aRegs[i].au32[4],
3031 pZmm16Hi->aRegs[i].au32[3],
3032 pZmm16Hi->aRegs[i].au32[2],
3033 pZmm16Hi->aRegs[i].au32[1],
3034 pZmm16Hi->aRegs[i].au32[0]);
3035 }
3036 }
3037 else
3038 for (unsigned i = 0; i < RT_ELEMENTS(pFpuCtx->aXMM); i++)
3039 pHlp->pfnPrintf(pHlp,
3040 i & 1
3041 ? "%sXMM%u%s=%08RX32'%08RX32'%08RX32'%08RX32\n"
3042 : "%sXMM%u%s=%08RX32'%08RX32'%08RX32'%08RX32 ",
3043 pszPrefix, i, i < 10 ? " " : "",
3044 pFpuCtx->aXMM[i].au32[3],
3045 pFpuCtx->aXMM[i].au32[2],
3046 pFpuCtx->aXMM[i].au32[1],
3047 pFpuCtx->aXMM[i].au32[0]);
3048
3049 if (pCtx->fXStateMask & XSAVE_C_OPMASK)
3050 {
3051 PCX86XSAVEOPMASK pOpMask = CPUMCTX_XSAVE_C_PTR(pCtx, XSAVE_C_OPMASK_BIT, PCX86XSAVEOPMASK);
3052 for (unsigned i = 0; i < RT_ELEMENTS(pOpMask->aKRegs); i += 4)
3053 pHlp->pfnPrintf(pHlp, "%sK%u=%016RX64 %sK%u=%016RX64 %sK%u=%016RX64 %sK%u=%016RX64\n",
3054 pszPrefix, i + 0, pOpMask->aKRegs[i + 0],
3055 pszPrefix, i + 1, pOpMask->aKRegs[i + 1],
3056 pszPrefix, i + 2, pOpMask->aKRegs[i + 2],
3057 pszPrefix, i + 3, pOpMask->aKRegs[i + 3]);
3058 }
3059
3060 if (pCtx->fXStateMask & XSAVE_C_BNDREGS)
3061 {
3062 PCX86XSAVEBNDREGS pBndRegs = CPUMCTX_XSAVE_C_PTR(pCtx, XSAVE_C_BNDREGS_BIT, PCX86XSAVEBNDREGS);
3063 for (unsigned i = 0; i < RT_ELEMENTS(pBndRegs->aRegs); i += 2)
3064 pHlp->pfnPrintf(pHlp, "%sBNDREG%u=%016RX64/%016RX64 %sBNDREG%u=%016RX64/%016RX64\n",
3065 pszPrefix, i, pBndRegs->aRegs[i].uLowerBound, pBndRegs->aRegs[i].uUpperBound,
3066 pszPrefix, i + 1, pBndRegs->aRegs[i + 1].uLowerBound, pBndRegs->aRegs[i + 1].uUpperBound);
3067 }
3068
3069 if (pCtx->fXStateMask & XSAVE_C_BNDCSR)
3070 {
3071 PCX86XSAVEBNDCFG pBndCfg = CPUMCTX_XSAVE_C_PTR(pCtx, XSAVE_C_BNDCSR_BIT, PCX86XSAVEBNDCFG);
3072 pHlp->pfnPrintf(pHlp, "%sBNDCFG.CONFIG=%016RX64 %sBNDCFG.STATUS=%016RX64\n",
3073 pszPrefix, pBndCfg->fConfig, pszPrefix, pBndCfg->fStatus);
3074 }
3075
3076 for (unsigned i = 0; i < RT_ELEMENTS(pFpuCtx->au32RsrvdRest); i++)
3077 if (pFpuCtx->au32RsrvdRest[i])
3078 pHlp->pfnPrintf(pHlp, "%sRsrvdRest[%u]=%RX32 (offset=%#x)\n",
3079 pszPrefix, i, pFpuCtx->au32RsrvdRest[i], RT_UOFFSETOF_DYN(X86FXSTATE, au32RsrvdRest[i]) );
3080 }
3081
3082 pHlp->pfnPrintf(pHlp,
3083 "%sEFER =%016RX64\n"
3084 "%sPAT =%016RX64\n"
3085 "%sSTAR =%016RX64\n"
3086 "%sCSTAR =%016RX64\n"
3087 "%sLSTAR =%016RX64\n"
3088 "%sSFMASK =%016RX64\n"
3089 "%sKERNELGSBASE =%016RX64\n",
3090 pszPrefix, pCtx->msrEFER,
3091 pszPrefix, pCtx->msrPAT,
3092 pszPrefix, pCtx->msrSTAR,
3093 pszPrefix, pCtx->msrCSTAR,
3094 pszPrefix, pCtx->msrLSTAR,
3095 pszPrefix, pCtx->msrSFMASK,
3096 pszPrefix, pCtx->msrKERNELGSBASE);
3097 break;
3098 }
3099}
3100
3101
3102/**
3103 * Display all cpu states and any other cpum info.
3104 *
3105 * @param pVM The cross context VM structure.
3106 * @param pHlp The info helper functions.
3107 * @param pszArgs Arguments, ignored.
3108 */
3109static DECLCALLBACK(void) cpumR3InfoAll(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
3110{
3111 cpumR3InfoGuest(pVM, pHlp, pszArgs);
3112 cpumR3InfoGuestInstr(pVM, pHlp, pszArgs);
3113 cpumR3InfoGuestHwvirt(pVM, pHlp, pszArgs);
3114 cpumR3InfoHyper(pVM, pHlp, pszArgs);
3115 cpumR3InfoHost(pVM, pHlp, pszArgs);
3116}
3117
3118
3119/**
3120 * Parses the info argument.
3121 *
3122 * The argument starts with 'verbose', 'terse' or 'default' and then
3123 * continues with the comment string.
3124 *
3125 * @param pszArgs The pointer to the argument string.
3126 * @param penmType Where to store the dump type request.
3127 * @param ppszComment Where to store the pointer to the comment string.
3128 */
3129static void cpumR3InfoParseArg(const char *pszArgs, CPUMDUMPTYPE *penmType, const char **ppszComment)
3130{
3131 if (!pszArgs)
3132 {
3133 *penmType = CPUMDUMPTYPE_DEFAULT;
3134 *ppszComment = "";
3135 }
3136 else
3137 {
3138 if (!strncmp(pszArgs, RT_STR_TUPLE("verbose")))
3139 {
3140 pszArgs += 7;
3141 *penmType = CPUMDUMPTYPE_VERBOSE;
3142 }
3143 else if (!strncmp(pszArgs, RT_STR_TUPLE("terse")))
3144 {
3145 pszArgs += 5;
3146 *penmType = CPUMDUMPTYPE_TERSE;
3147 }
3148 else if (!strncmp(pszArgs, RT_STR_TUPLE("default")))
3149 {
3150 pszArgs += 7;
3151 *penmType = CPUMDUMPTYPE_DEFAULT;
3152 }
3153 else
3154 *penmType = CPUMDUMPTYPE_DEFAULT;
3155 *ppszComment = RTStrStripL(pszArgs);
3156 }
3157}
3158
3159
3160/**
3161 * Display the guest cpu state.
3162 *
3163 * @param pVM The cross context VM structure.
3164 * @param pHlp The info helper functions.
3165 * @param pszArgs Arguments.
3166 */
3167static DECLCALLBACK(void) cpumR3InfoGuest(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
3168{
3169 CPUMDUMPTYPE enmType;
3170 const char *pszComment;
3171 cpumR3InfoParseArg(pszArgs, &enmType, &pszComment);
3172
3173 PVMCPU pVCpu = VMMGetCpu(pVM);
3174 if (!pVCpu)
3175 pVCpu = &pVM->aCpus[0];
3176
3177 pHlp->pfnPrintf(pHlp, "Guest CPUM (VCPU %d) state: %s\n", pVCpu->idCpu, pszComment);
3178
3179 PCPUMCTX pCtx = &pVCpu->cpum.s.Guest;
3180 cpumR3InfoOne(pVM, pCtx, CPUMCTX2CORE(pCtx), pHlp, enmType, "");
3181}
3182
3183
3184/**
3185 * Displays an SVM VMCB control area.
3186 *
3187 * @param pHlp The info helper functions.
3188 * @param pVmcbCtrl Pointer to a SVM VMCB controls area.
3189 * @param pszPrefix Caller specified string prefix.
3190 */
3191static void cpumR3InfoSvmVmcbCtrl(PCDBGFINFOHLP pHlp, PCSVMVMCBCTRL pVmcbCtrl, const char *pszPrefix)
3192{
3193 AssertReturnVoid(pHlp);
3194 AssertReturnVoid(pVmcbCtrl);
3195
3196 pHlp->pfnPrintf(pHlp, "%sCRX-read intercepts = %#RX16\n", pszPrefix, pVmcbCtrl->u16InterceptRdCRx);
3197 pHlp->pfnPrintf(pHlp, "%sCRX-write intercepts = %#RX16\n", pszPrefix, pVmcbCtrl->u16InterceptWrCRx);
3198 pHlp->pfnPrintf(pHlp, "%sDRX-read intercepts = %#RX16\n", pszPrefix, pVmcbCtrl->u16InterceptRdDRx);
3199 pHlp->pfnPrintf(pHlp, "%sDRX-write intercepts = %#RX16\n", pszPrefix, pVmcbCtrl->u16InterceptWrDRx);
3200 pHlp->pfnPrintf(pHlp, "%sException intercepts = %#RX32\n", pszPrefix, pVmcbCtrl->u32InterceptXcpt);
3201 pHlp->pfnPrintf(pHlp, "%sControl intercepts = %#RX64\n", pszPrefix, pVmcbCtrl->u64InterceptCtrl);
3202 pHlp->pfnPrintf(pHlp, "%sPause-filter threshold = %#RX16\n", pszPrefix, pVmcbCtrl->u16PauseFilterThreshold);
3203 pHlp->pfnPrintf(pHlp, "%sPause-filter count = %#RX16\n", pszPrefix, pVmcbCtrl->u16PauseFilterCount);
3204 pHlp->pfnPrintf(pHlp, "%sIOPM bitmap physaddr = %#RX64\n", pszPrefix, pVmcbCtrl->u64IOPMPhysAddr);
3205 pHlp->pfnPrintf(pHlp, "%sMSRPM bitmap physaddr = %#RX64\n", pszPrefix, pVmcbCtrl->u64MSRPMPhysAddr);
3206 pHlp->pfnPrintf(pHlp, "%sTSC offset = %#RX64\n", pszPrefix, pVmcbCtrl->u64TSCOffset);
3207 pHlp->pfnPrintf(pHlp, "%sTLB Control\n", pszPrefix);
3208 pHlp->pfnPrintf(pHlp, " %sASID = %#RX32\n", pszPrefix, pVmcbCtrl->TLBCtrl.n.u32ASID);
3209 pHlp->pfnPrintf(pHlp, " %sTLB-flush type = %u\n", pszPrefix, pVmcbCtrl->TLBCtrl.n.u8TLBFlush);
3210 pHlp->pfnPrintf(pHlp, "%sInterrupt Control\n", pszPrefix);
3211 pHlp->pfnPrintf(pHlp, " %sVTPR = %#RX8 (%u)\n", pszPrefix, pVmcbCtrl->IntCtrl.n.u8VTPR, pVmcbCtrl->IntCtrl.n.u8VTPR);
3212 pHlp->pfnPrintf(pHlp, " %sVIRQ (Pending) = %RTbool\n", pszPrefix, pVmcbCtrl->IntCtrl.n.u1VIrqPending);
3213 pHlp->pfnPrintf(pHlp, " %sVINTR vector = %#RX8\n", pszPrefix, pVmcbCtrl->IntCtrl.n.u8VIntrVector);
3214 pHlp->pfnPrintf(pHlp, " %sVGIF = %u\n", pszPrefix, pVmcbCtrl->IntCtrl.n.u1VGif);
3215 pHlp->pfnPrintf(pHlp, " %sVINTR priority = %#RX8\n", pszPrefix, pVmcbCtrl->IntCtrl.n.u4VIntrPrio);
3216 pHlp->pfnPrintf(pHlp, " %sIgnore TPR = %RTbool\n", pszPrefix, pVmcbCtrl->IntCtrl.n.u1IgnoreTPR);
3217 pHlp->pfnPrintf(pHlp, " %sVINTR masking = %RTbool\n", pszPrefix, pVmcbCtrl->IntCtrl.n.u1VIntrMasking);
3218 pHlp->pfnPrintf(pHlp, " %sVGIF enable = %RTbool\n", pszPrefix, pVmcbCtrl->IntCtrl.n.u1VGifEnable);
3219 pHlp->pfnPrintf(pHlp, " %sAVIC enable = %RTbool\n", pszPrefix, pVmcbCtrl->IntCtrl.n.u1AvicEnable);
3220 pHlp->pfnPrintf(pHlp, "%sInterrupt Shadow\n", pszPrefix);
3221 pHlp->pfnPrintf(pHlp, " %sInterrupt shadow = %RTbool\n", pszPrefix, pVmcbCtrl->IntShadow.n.u1IntShadow);
3222 pHlp->pfnPrintf(pHlp, " %sGuest-interrupt Mask = %RTbool\n", pszPrefix, pVmcbCtrl->IntShadow.n.u1GuestIntMask);
3223 pHlp->pfnPrintf(pHlp, "%sExit Code = %#RX64\n", pszPrefix, pVmcbCtrl->u64ExitCode);
3224 pHlp->pfnPrintf(pHlp, "%sEXITINFO1 = %#RX64\n", pszPrefix, pVmcbCtrl->u64ExitInfo1);
3225 pHlp->pfnPrintf(pHlp, "%sEXITINFO2 = %#RX64\n", pszPrefix, pVmcbCtrl->u64ExitInfo2);
3226 pHlp->pfnPrintf(pHlp, "%sExit Interrupt Info\n", pszPrefix);
3227 pHlp->pfnPrintf(pHlp, " %sValid = %RTbool\n", pszPrefix, pVmcbCtrl->ExitIntInfo.n.u1Valid);
3228 pHlp->pfnPrintf(pHlp, " %sVector = %#RX8 (%u)\n", pszPrefix, pVmcbCtrl->ExitIntInfo.n.u8Vector, pVmcbCtrl->ExitIntInfo.n.u8Vector);
3229 pHlp->pfnPrintf(pHlp, " %sType = %u\n", pszPrefix, pVmcbCtrl->ExitIntInfo.n.u3Type);
3230 pHlp->pfnPrintf(pHlp, " %sError-code valid = %RTbool\n", pszPrefix, pVmcbCtrl->ExitIntInfo.n.u1ErrorCodeValid);
3231 pHlp->pfnPrintf(pHlp, " %sError-code = %#RX32\n", pszPrefix, pVmcbCtrl->ExitIntInfo.n.u32ErrorCode);
3232 pHlp->pfnPrintf(pHlp, "%sNested paging and SEV\n", pszPrefix);
3233 pHlp->pfnPrintf(pHlp, " %sNested paging = %RTbool\n", pszPrefix, pVmcbCtrl->NestedPagingCtrl.n.u1NestedPaging);
3234 pHlp->pfnPrintf(pHlp, " %sSEV (Secure Encrypted VM) = %RTbool\n", pszPrefix, pVmcbCtrl->NestedPagingCtrl.n.u1Sev);
3235 pHlp->pfnPrintf(pHlp, " %sSEV-ES (Encrypted State) = %RTbool\n", pszPrefix, pVmcbCtrl->NestedPagingCtrl.n.u1SevEs);
3236 pHlp->pfnPrintf(pHlp, "%sEvent Inject\n", pszPrefix);
3237 pHlp->pfnPrintf(pHlp, " %sValid = %RTbool\n", pszPrefix, pVmcbCtrl->EventInject.n.u1Valid);
3238 pHlp->pfnPrintf(pHlp, " %sVector = %#RX32 (%u)\n", pszPrefix, pVmcbCtrl->EventInject.n.u8Vector, pVmcbCtrl->EventInject.n.u8Vector);
3239 pHlp->pfnPrintf(pHlp, " %sType = %u\n", pszPrefix, pVmcbCtrl->EventInject.n.u3Type);
3240 pHlp->pfnPrintf(pHlp, " %sError-code valid = %RTbool\n", pszPrefix, pVmcbCtrl->EventInject.n.u1ErrorCodeValid);
3241 pHlp->pfnPrintf(pHlp, " %sError-code = %#RX32\n", pszPrefix, pVmcbCtrl->EventInject.n.u32ErrorCode);
3242 pHlp->pfnPrintf(pHlp, "%sNested-paging CR3 = %#RX64\n", pszPrefix, pVmcbCtrl->u64NestedPagingCR3);
3243 pHlp->pfnPrintf(pHlp, "%sLBR Virtualization\n", pszPrefix);
3244 pHlp->pfnPrintf(pHlp, " %sLBR virt = %RTbool\n", pszPrefix, pVmcbCtrl->LbrVirt.n.u1LbrVirt);
3245 pHlp->pfnPrintf(pHlp, " %sVirt. VMSAVE/VMLOAD = %RTbool\n", pszPrefix, pVmcbCtrl->LbrVirt.n.u1VirtVmsaveVmload);
3246 pHlp->pfnPrintf(pHlp, "%sVMCB Clean Bits = %#RX32\n", pszPrefix, pVmcbCtrl->u32VmcbCleanBits);
3247 pHlp->pfnPrintf(pHlp, "%sNext-RIP = %#RX64\n", pszPrefix, pVmcbCtrl->u64NextRIP);
3248 pHlp->pfnPrintf(pHlp, "%sInstruction bytes fetched = %u\n", pszPrefix, pVmcbCtrl->cbInstrFetched);
3249 pHlp->pfnPrintf(pHlp, "%sInstruction bytes = %.*Rhxs\n", pszPrefix, sizeof(pVmcbCtrl->abInstr), pVmcbCtrl->abInstr);
3250 pHlp->pfnPrintf(pHlp, "%sAVIC\n", pszPrefix);
3251 pHlp->pfnPrintf(pHlp, " %sBar addr = %#RX64\n", pszPrefix, pVmcbCtrl->AvicBar.n.u40Addr);
3252 pHlp->pfnPrintf(pHlp, " %sBacking page addr = %#RX64\n", pszPrefix, pVmcbCtrl->AvicBackingPagePtr.n.u40Addr);
3253 pHlp->pfnPrintf(pHlp, " %sLogical table addr = %#RX64\n", pszPrefix, pVmcbCtrl->AvicLogicalTablePtr.n.u40Addr);
3254 pHlp->pfnPrintf(pHlp, " %sPhysical table addr = %#RX64\n", pszPrefix, pVmcbCtrl->AvicPhysicalTablePtr.n.u40Addr);
3255 pHlp->pfnPrintf(pHlp, " %sLast guest core Id = %u\n", pszPrefix, pVmcbCtrl->AvicPhysicalTablePtr.n.u8LastGuestCoreId);
3256}
3257
3258
3259/**
3260 * Helper for dumping the SVM VMCB selector registers.
3261 *
3262 * @param pHlp The info helper functions.
3263 * @param pSel Pointer to the SVM selector register.
3264 * @param pszName Name of the selector.
3265 * @param pszPrefix Caller specified string prefix.
3266 */
3267DECLINLINE(void) cpumR3InfoSvmVmcbSelReg(PCDBGFINFOHLP pHlp, PCSVMSELREG pSel, const char *pszName, const char *pszPrefix)
3268{
3269 /* The string width of 4 used below is to handle 'LDTR'. Change later if longer register names are used. */
3270 pHlp->pfnPrintf(pHlp, "%s%-4s = {%04x base=%016RX64 limit=%08x flags=%04x}\n", pszPrefix,
3271 pszName, pSel->u16Sel, pSel->u64Base, pSel->u32Limit, pSel->u16Attr);
3272}
3273
3274
3275/**
3276 * Helper for dumping the SVM VMCB GDTR/IDTR registers.
3277 *
3278 * @param pHlp The info helper functions.
3279 * @param pXdtr Pointer to the descriptor table register.
3280 * @param pszName Name of the descriptor table register.
3281 * @param pszPrefix Caller specified string prefix.
3282 */
3283DECLINLINE(void) cpumR3InfoSvmVmcbXdtr(PCDBGFINFOHLP pHlp, PCSVMXDTR pXdtr, const char *pszName, const char *pszPrefix)
3284{
3285 /* The string width of 4 used below is to cover 'GDTR', 'IDTR'. Change later if longer register names are used. */
3286 pHlp->pfnPrintf(pHlp, "%s%-4s = %016RX64:%04x\n", pszPrefix, pszName, pXdtr->u64Base, pXdtr->u32Limit);
3287}
3288
3289
3290/**
3291 * Displays an SVM VMCB state-save area.
3292 *
3293 * @param pHlp The info helper functions.
3294 * @param pVmcbStateSave Pointer to a SVM VMCB controls area.
3295 * @param pszPrefix Caller specified string prefix.
3296 */
3297static void cpumR3InfoSvmVmcbStateSave(PCDBGFINFOHLP pHlp, PCSVMVMCBSTATESAVE pVmcbStateSave, const char *pszPrefix)
3298{
3299 AssertReturnVoid(pHlp);
3300 AssertReturnVoid(pVmcbStateSave);
3301
3302 char szEFlags[80];
3303 cpumR3InfoFormatFlags(&szEFlags[0], pVmcbStateSave->u64RFlags);
3304
3305 cpumR3InfoSvmVmcbSelReg(pHlp, &pVmcbStateSave->CS, "CS", pszPrefix);
3306 cpumR3InfoSvmVmcbSelReg(pHlp, &pVmcbStateSave->SS, "SS", pszPrefix);
3307 cpumR3InfoSvmVmcbSelReg(pHlp, &pVmcbStateSave->ES, "ES", pszPrefix);
3308 cpumR3InfoSvmVmcbSelReg(pHlp, &pVmcbStateSave->DS, "DS", pszPrefix);
3309 cpumR3InfoSvmVmcbSelReg(pHlp, &pVmcbStateSave->FS, "FS", pszPrefix);
3310 cpumR3InfoSvmVmcbSelReg(pHlp, &pVmcbStateSave->GS, "GS", pszPrefix);
3311 cpumR3InfoSvmVmcbSelReg(pHlp, &pVmcbStateSave->LDTR, "LDTR", pszPrefix);
3312 cpumR3InfoSvmVmcbSelReg(pHlp, &pVmcbStateSave->TR, "TR", pszPrefix);
3313 cpumR3InfoSvmVmcbXdtr(pHlp, &pVmcbStateSave->GDTR, "GDTR", pszPrefix);
3314 cpumR3InfoSvmVmcbXdtr(pHlp, &pVmcbStateSave->IDTR, "IDTR", pszPrefix);
3315 pHlp->pfnPrintf(pHlp, "%sCPL = %u\n", pszPrefix, pVmcbStateSave->u8CPL);
3316 pHlp->pfnPrintf(pHlp, "%sEFER = %#RX64\n", pszPrefix, pVmcbStateSave->u64EFER);
3317 pHlp->pfnPrintf(pHlp, "%sCR4 = %#RX64\n", pszPrefix, pVmcbStateSave->u64CR4);
3318 pHlp->pfnPrintf(pHlp, "%sCR3 = %#RX64\n", pszPrefix, pVmcbStateSave->u64CR3);
3319 pHlp->pfnPrintf(pHlp, "%sCR0 = %#RX64\n", pszPrefix, pVmcbStateSave->u64CR0);
3320 pHlp->pfnPrintf(pHlp, "%sDR7 = %#RX64\n", pszPrefix, pVmcbStateSave->u64DR7);
3321 pHlp->pfnPrintf(pHlp, "%sDR6 = %#RX64\n", pszPrefix, pVmcbStateSave->u64DR6);
3322 pHlp->pfnPrintf(pHlp, "%sRFLAGS = %#RX64 %31s\n", pszPrefix, pVmcbStateSave->u64RFlags, szEFlags);
3323 pHlp->pfnPrintf(pHlp, "%sRIP = %#RX64\n", pszPrefix, pVmcbStateSave->u64RIP);
3324 pHlp->pfnPrintf(pHlp, "%sRSP = %#RX64\n", pszPrefix, pVmcbStateSave->u64RSP);
3325 pHlp->pfnPrintf(pHlp, "%sRAX = %#RX64\n", pszPrefix, pVmcbStateSave->u64RAX);
3326 pHlp->pfnPrintf(pHlp, "%sSTAR = %#RX64\n", pszPrefix, pVmcbStateSave->u64STAR);
3327 pHlp->pfnPrintf(pHlp, "%sLSTAR = %#RX64\n", pszPrefix, pVmcbStateSave->u64LSTAR);
3328 pHlp->pfnPrintf(pHlp, "%sCSTAR = %#RX64\n", pszPrefix, pVmcbStateSave->u64CSTAR);
3329 pHlp->pfnPrintf(pHlp, "%sSFMASK = %#RX64\n", pszPrefix, pVmcbStateSave->u64SFMASK);
3330 pHlp->pfnPrintf(pHlp, "%sKERNELGSBASE = %#RX64\n", pszPrefix, pVmcbStateSave->u64KernelGSBase);
3331 pHlp->pfnPrintf(pHlp, "%sSysEnter CS = %#RX64\n", pszPrefix, pVmcbStateSave->u64SysEnterCS);
3332 pHlp->pfnPrintf(pHlp, "%sSysEnter EIP = %#RX64\n", pszPrefix, pVmcbStateSave->u64SysEnterEIP);
3333 pHlp->pfnPrintf(pHlp, "%sSysEnter ESP = %#RX64\n", pszPrefix, pVmcbStateSave->u64SysEnterESP);
3334 pHlp->pfnPrintf(pHlp, "%sCR2 = %#RX64\n", pszPrefix, pVmcbStateSave->u64CR2);
3335 pHlp->pfnPrintf(pHlp, "%sPAT = %#RX64\n", pszPrefix, pVmcbStateSave->u64PAT);
3336 pHlp->pfnPrintf(pHlp, "%sDBGCTL = %#RX64\n", pszPrefix, pVmcbStateSave->u64DBGCTL);
3337 pHlp->pfnPrintf(pHlp, "%sBR_FROM = %#RX64\n", pszPrefix, pVmcbStateSave->u64BR_FROM);
3338 pHlp->pfnPrintf(pHlp, "%sBR_TO = %#RX64\n", pszPrefix, pVmcbStateSave->u64BR_TO);
3339 pHlp->pfnPrintf(pHlp, "%sLASTXCPT_FROM = %#RX64\n", pszPrefix, pVmcbStateSave->u64LASTEXCPFROM);
3340 pHlp->pfnPrintf(pHlp, "%sLASTXCPT_TO = %#RX64\n", pszPrefix, pVmcbStateSave->u64LASTEXCPTO);
3341}
3342
3343
3344/**
3345 * Displays a virtual-VMCS.
3346 *
3347 * @param pHlp The info helper functions.
3348 * @param pVmcs Pointer to a virtual VMCS.
3349 * @param pszPrefix Caller specified string prefix.
3350 */
3351static void cpumR3InfoVmxVmcs(PCDBGFINFOHLP pHlp, PCVMXVVMCS pVmcs, const char *pszPrefix)
3352{
3353 AssertReturnVoid(pHlp);
3354 AssertReturnVoid(pVmcs);
3355
3356 /* The string width of -4 used in the macros below to cover 'LDTR', 'GDTR', 'IDTR. */
3357#define CPUMVMX_DUMP_HOST_XDTR(a_pHlp, a_pVmcs, a_Seg, a_SegName, a_pszPrefix) \
3358 do { \
3359 (a_pHlp)->pfnPrintf((a_pHlp), " %s%-4s = {base=%016RX64}\n", \
3360 (a_pszPrefix), (a_SegName), (a_pVmcs)->u64Host##a_Seg##Base.u); \
3361 } while (0)
3362
3363#define CPUMVMX_DUMP_HOST_FS_GS_TR(a_pHlp, a_pVmcs, a_Seg, a_SegName, a_pszPrefix) \
3364 do { \
3365 (a_pHlp)->pfnPrintf((a_pHlp), " %s%-4s = {%04x base=%016RX64}\n", \
3366 (a_pszPrefix), (a_SegName), (a_pVmcs)->Host##a_Seg, (a_pVmcs)->u64Host##a_Seg##Base.u); \
3367 } while (0)
3368
3369#define CPUMVMX_DUMP_GUEST_SEGREG(a_pHlp, a_pVmcs, a_Seg, a_SegName, a_pszPrefix) \
3370 do { \
3371 (a_pHlp)->pfnPrintf((a_pHlp), " %s%-4s = {%04x base=%016RX64 limit=%08x flags=%04x}\n", \
3372 (a_pszPrefix), (a_SegName), (a_pVmcs)->Guest##a_Seg, (a_pVmcs)->u64Guest##a_Seg##Base.u, \
3373 (a_pVmcs)->u32Guest##a_Seg##Limit, (a_pVmcs)->u32Guest##a_Seg##Attr); \
3374 } while (0)
3375
3376#define CPUMVMX_DUMP_GUEST_XDTR(a_pHlp, a_pVmcs, a_Seg, a_SegName, a_pszPrefix) \
3377 do { \
3378 (a_pHlp)->pfnPrintf((a_pHlp), " %s%-4s = {base=%016RX64 limit=%08x}\n", \
3379 (a_pszPrefix), (a_SegName), (a_pVmcs)->u64Guest##a_Seg##Base.u, (a_pVmcs)->u32Guest##a_Seg##Limit); \
3380 } while (0)
3381
3382 /* Header. */
3383 {
3384 pHlp->pfnPrintf(pHlp, "%sHeader:\n", pszPrefix);
3385 pHlp->pfnPrintf(pHlp, " %sVMCS revision id = %#RX32\n", pszPrefix, pVmcs->u32VmcsRevId);
3386 pHlp->pfnPrintf(pHlp, " %sVMX-abort id = %#RX32 (%s)\n", pszPrefix, pVmcs->enmVmxAbort, HMVmxGetAbortDesc(pVmcs->enmVmxAbort));
3387 pHlp->pfnPrintf(pHlp, " %sVMCS state = %#x (%s)\n", pszPrefix, pVmcs->fVmcsState, HMVmxGetVmcsStateDesc(pVmcs->fVmcsState));
3388 }
3389
3390 /* Control fields. */
3391 {
3392 /* 16-bit. */
3393 pHlp->pfnPrintf(pHlp, "%sControl:\n", pszPrefix);
3394 pHlp->pfnPrintf(pHlp, " %sVPID = %#RX16\n", pszPrefix, pVmcs->u16Vpid);
3395 pHlp->pfnPrintf(pHlp, " %sPosted intr notify vector = %#RX16\n", pszPrefix, pVmcs->u16PostIntNotifyVector);
3396 pHlp->pfnPrintf(pHlp, " %sEPTP index = %#RX16\n", pszPrefix, pVmcs->u16EptpIndex);
3397
3398 /* 32-bit. */
3399 pHlp->pfnPrintf(pHlp, " %sPinCtls = %#RX32\n", pszPrefix, pVmcs->u32PinCtls);
3400 pHlp->pfnPrintf(pHlp, " %sProcCtls = %#RX32\n", pszPrefix, pVmcs->u32ProcCtls);
3401 pHlp->pfnPrintf(pHlp, " %sProcCtls2 = %#RX32\n", pszPrefix, pVmcs->u32ProcCtls2);
3402 pHlp->pfnPrintf(pHlp, " %sExitCtls = %#RX32\n", pszPrefix, pVmcs->u32ExitCtls);
3403 pHlp->pfnPrintf(pHlp, " %sEntryCtls = %#RX32\n", pszPrefix, pVmcs->u32EntryCtls);
3404 pHlp->pfnPrintf(pHlp, " %sException bitmap = %#RX32\n", pszPrefix, pVmcs->u32XcptBitmap);
3405 pHlp->pfnPrintf(pHlp, " %sPage-fault mask = %#RX32\n", pszPrefix, pVmcs->u32XcptPFMask);
3406 pHlp->pfnPrintf(pHlp, " %sPage-fault match = %#RX32\n", pszPrefix, pVmcs->u32XcptPFMatch);
3407 pHlp->pfnPrintf(pHlp, " %sCR3-target count = %RU32\n", pszPrefix, pVmcs->u32Cr3TargetCount);
3408 pHlp->pfnPrintf(pHlp, " %sVM-exit MSR store count = %RU32\n", pszPrefix, pVmcs->u32ExitMsrStoreCount);
3409 pHlp->pfnPrintf(pHlp, " %sVM-exit MSR load count = %RU32\n", pszPrefix, pVmcs->u32ExitMsrLoadCount);
3410 pHlp->pfnPrintf(pHlp, " %sVM-entry MSR load count = %RU32\n", pszPrefix, pVmcs->u32EntryMsrLoadCount);
3411 pHlp->pfnPrintf(pHlp, " %sVM-Entry interruption info = %#RX32\n", pszPrefix, pVmcs->u32EntryIntInfo);
3412 {
3413 uint32_t const fInfo = pVmcs->u32EntryIntInfo;
3414 uint8_t const uType = VMX_ENTRY_INT_INFO_TYPE(fInfo);
3415 pHlp->pfnPrintf(pHlp, " %sValid = %RTbool\n", pszPrefix, VMX_ENTRY_INT_INFO_IS_VALID(fInfo));
3416 pHlp->pfnPrintf(pHlp, " %sType = %#x (%s)\n", pszPrefix, uType, HMVmxGetEntryIntInfoTypeDesc(uType));
3417 pHlp->pfnPrintf(pHlp, " %sVector = %#x\n", pszPrefix, VMX_ENTRY_INT_INFO_VECTOR(fInfo));
3418 pHlp->pfnPrintf(pHlp, " %sNMI-unblocking-IRET = %RTbool\n", pszPrefix, VMX_ENTRY_INT_INFO_IS_NMI_UNBLOCK_IRET(fInfo));
3419 pHlp->pfnPrintf(pHlp, " %sError-code valid = %RTbool\n", pszPrefix, VMX_ENTRY_INT_INFO_IS_ERROR_CODE_VALID(fInfo));
3420 }
3421 pHlp->pfnPrintf(pHlp, " %sVM-Entry xcpt error-code = %#RX32\n", pszPrefix, pVmcs->u32EntryXcptErrCode);
3422 pHlp->pfnPrintf(pHlp, " %sVM-Entry instruction len = %u bytes\n", pszPrefix, pVmcs->u32EntryInstrLen);
3423 pHlp->pfnPrintf(pHlp, " %sTPR threshold = %#RX32\n", pszPrefix, pVmcs->u32TprThreshold);
3424 pHlp->pfnPrintf(pHlp, " %sPLE gap = %#RX32\n", pszPrefix, pVmcs->u32PleGap);
3425 pHlp->pfnPrintf(pHlp, " %sPLE window = %#RX32\n", pszPrefix, pVmcs->u32PleWindow);
3426
3427 /* 64-bit. */
3428 pHlp->pfnPrintf(pHlp, " %sIO-bitmap A addr = %#RX64\n", pszPrefix, pVmcs->u64AddrIoBitmapA.u);
3429 pHlp->pfnPrintf(pHlp, " %sIO-bitmap B addr = %#RX64\n", pszPrefix, pVmcs->u64AddrIoBitmapB.u);
3430 pHlp->pfnPrintf(pHlp, " %sMSR-bitmap addr = %#RX64\n", pszPrefix, pVmcs->u64AddrMsrBitmap.u);
3431 pHlp->pfnPrintf(pHlp, " %sVM-exit MSR store addr = %#RX64\n", pszPrefix, pVmcs->u64AddrExitMsrStore.u);
3432 pHlp->pfnPrintf(pHlp, " %sVM-exit MSR load addr = %#RX64\n", pszPrefix, pVmcs->u64AddrExitMsrLoad.u);
3433 pHlp->pfnPrintf(pHlp, " %sVM-entry MSR load addr = %#RX64\n", pszPrefix, pVmcs->u64AddrEntryMsrLoad.u);
3434 pHlp->pfnPrintf(pHlp, " %sExecutive VMCS ptr = %#RX64\n", pszPrefix, pVmcs->u64ExecVmcsPtr.u);
3435 pHlp->pfnPrintf(pHlp, " %sPML addr = %#RX64\n", pszPrefix, pVmcs->u64AddrPml.u);
3436 pHlp->pfnPrintf(pHlp, " %sTSC offset = %#RX64\n", pszPrefix, pVmcs->u64TscOffset.u);
3437 pHlp->pfnPrintf(pHlp, " %sVirtual-APIC addr = %#RX64\n", pszPrefix, pVmcs->u64AddrVirtApic.u);
3438 pHlp->pfnPrintf(pHlp, " %sAPIC-access addr = %#RX64\n", pszPrefix, pVmcs->u64AddrApicAccess.u);
3439 pHlp->pfnPrintf(pHlp, " %sPosted-intr desc addr = %#RX64\n", pszPrefix, pVmcs->u64AddrPostedIntDesc.u);
3440 pHlp->pfnPrintf(pHlp, " %sVM-functions control = %#RX64\n", pszPrefix, pVmcs->u64VmFuncCtls.u);
3441 pHlp->pfnPrintf(pHlp, " %sEPTP ptr = %#RX64\n", pszPrefix, pVmcs->u64EptpPtr.u);
3442 pHlp->pfnPrintf(pHlp, " %sEOI-exit bitmap 0 addr = %#RX64\n", pszPrefix, pVmcs->u64EoiExitBitmap0.u);
3443 pHlp->pfnPrintf(pHlp, " %sEOI-exit bitmap 1 addr = %#RX64\n", pszPrefix, pVmcs->u64EoiExitBitmap1.u);
3444 pHlp->pfnPrintf(pHlp, " %sEOI-exit bitmap 2 addr = %#RX64\n", pszPrefix, pVmcs->u64EoiExitBitmap2.u);
3445 pHlp->pfnPrintf(pHlp, " %sEOI-exit bitmap 3 addr = %#RX64\n", pszPrefix, pVmcs->u64EoiExitBitmap3.u);
3446 pHlp->pfnPrintf(pHlp, " %sEPTP-list addr = %#RX64\n", pszPrefix, pVmcs->u64AddrEptpList.u);
3447 pHlp->pfnPrintf(pHlp, " %sVMREAD-bitmap addr = %#RX64\n", pszPrefix, pVmcs->u64AddrVmreadBitmap.u);
3448 pHlp->pfnPrintf(pHlp, " %sVMWRITE-bitmap addr = %#RX64\n", pszPrefix, pVmcs->u64AddrVmwriteBitmap.u);
3449 pHlp->pfnPrintf(pHlp, " %sVirt-Xcpt info addr = %#RX64\n", pszPrefix, pVmcs->u64AddrXcptVeInfo.u);
3450 pHlp->pfnPrintf(pHlp, " %sXSS-bitmap = %#RX64\n", pszPrefix, pVmcs->u64XssBitmap.u);
3451 pHlp->pfnPrintf(pHlp, " %sENCLS-exiting bitmap addr = %#RX64\n", pszPrefix, pVmcs->u64AddrEnclsBitmap.u);
3452 pHlp->pfnPrintf(pHlp, " %sTSC multiplier = %#RX64\n", pszPrefix, pVmcs->u64TscMultiplier.u);
3453
3454 /* Natural width. */
3455 pHlp->pfnPrintf(pHlp, " %sCR0 guest/host mask = %#RX64\n", pszPrefix, pVmcs->u64Cr0Mask.u);
3456 pHlp->pfnPrintf(pHlp, " %sCR4 guest/host mask = %#RX64\n", pszPrefix, pVmcs->u64Cr4Mask.u);
3457 pHlp->pfnPrintf(pHlp, " %sCR0 read shadow = %#RX64\n", pszPrefix, pVmcs->u64Cr0ReadShadow.u);
3458 pHlp->pfnPrintf(pHlp, " %sCR4 read shadow = %#RX64\n", pszPrefix, pVmcs->u64Cr4ReadShadow.u);
3459 pHlp->pfnPrintf(pHlp, " %sCR3-target 0 = %#RX64\n", pszPrefix, pVmcs->u64Cr3Target0.u);
3460 pHlp->pfnPrintf(pHlp, " %sCR3-target 1 = %#RX64\n", pszPrefix, pVmcs->u64Cr3Target1.u);
3461 pHlp->pfnPrintf(pHlp, " %sCR3-target 2 = %#RX64\n", pszPrefix, pVmcs->u64Cr3Target2.u);
3462 pHlp->pfnPrintf(pHlp, " %sCR3-target 3 = %#RX64\n", pszPrefix, pVmcs->u64Cr3Target3.u);
3463 }
3464
3465 /* Guest state. */
3466 {
3467 char szEFlags[80];
3468 cpumR3InfoFormatFlags(&szEFlags[0], pVmcs->u64GuestRFlags.u);
3469 pHlp->pfnPrintf(pHlp, "%sGuest state:\n", pszPrefix);
3470
3471 /* 16-bit. */
3472 CPUMVMX_DUMP_GUEST_SEGREG(pHlp, pVmcs, Cs, "cs", pszPrefix);
3473 CPUMVMX_DUMP_GUEST_SEGREG(pHlp, pVmcs, Ss, "ss", pszPrefix);
3474 CPUMVMX_DUMP_GUEST_SEGREG(pHlp, pVmcs, Es, "es", pszPrefix);
3475 CPUMVMX_DUMP_GUEST_SEGREG(pHlp, pVmcs, Ds, "ds", pszPrefix);
3476 CPUMVMX_DUMP_GUEST_SEGREG(pHlp, pVmcs, Fs, "fs", pszPrefix);
3477 CPUMVMX_DUMP_GUEST_SEGREG(pHlp, pVmcs, Gs, "gs", pszPrefix);
3478 CPUMVMX_DUMP_GUEST_SEGREG(pHlp, pVmcs, Ldtr, "ldtr", pszPrefix);
3479 CPUMVMX_DUMP_GUEST_SEGREG(pHlp, pVmcs, Tr, "tr", pszPrefix);
3480 CPUMVMX_DUMP_GUEST_XDTR(pHlp, pVmcs, Gdtr, "gdtr", pszPrefix);
3481 CPUMVMX_DUMP_GUEST_XDTR(pHlp, pVmcs, Idtr, "idtr", pszPrefix);
3482 pHlp->pfnPrintf(pHlp, " %sInterrupt status = %#RX16\n", pszPrefix, pVmcs->u16GuestIntStatus);
3483 pHlp->pfnPrintf(pHlp, " %sPML index = %#RX16\n", pszPrefix, pVmcs->u16PmlIndex);
3484
3485 /* 32-bit. */
3486 pHlp->pfnPrintf(pHlp, " %sInterruptibility state = %#RX32\n", pszPrefix, pVmcs->u32GuestIntrState);
3487 pHlp->pfnPrintf(pHlp, " %sActivity state = %#RX32\n", pszPrefix, pVmcs->u32GuestActivityState);
3488 pHlp->pfnPrintf(pHlp, " %sSMBASE = %#RX32\n", pszPrefix, pVmcs->u32GuestSmBase);
3489 pHlp->pfnPrintf(pHlp, " %sSysEnter CS = %#RX32\n", pszPrefix, pVmcs->u32GuestSysenterCS);
3490 pHlp->pfnPrintf(pHlp, " %sVMX-preemption timer value = %#RX32\n", pszPrefix, pVmcs->u32PreemptTimer);
3491
3492 /* 64-bit. */
3493 pHlp->pfnPrintf(pHlp, " %sVMCS link ptr = %#RX64\n", pszPrefix, pVmcs->u64VmcsLinkPtr.u);
3494 pHlp->pfnPrintf(pHlp, " %sDBGCTL = %#RX64\n", pszPrefix, pVmcs->u64GuestDebugCtlMsr.u);
3495 pHlp->pfnPrintf(pHlp, " %sPAT = %#RX64\n", pszPrefix, pVmcs->u64GuestPatMsr.u);
3496 pHlp->pfnPrintf(pHlp, " %sEFER = %#RX64\n", pszPrefix, pVmcs->u64GuestEferMsr.u);
3497 pHlp->pfnPrintf(pHlp, " %sPERFGLOBALCTRL = %#RX64\n", pszPrefix, pVmcs->u64GuestPerfGlobalCtlMsr.u);
3498 pHlp->pfnPrintf(pHlp, " %sPDPTE 0 = %#RX64\n", pszPrefix, pVmcs->u64GuestPdpte0.u);
3499 pHlp->pfnPrintf(pHlp, " %sPDPTE 1 = %#RX64\n", pszPrefix, pVmcs->u64GuestPdpte1.u);
3500 pHlp->pfnPrintf(pHlp, " %sPDPTE 2 = %#RX64\n", pszPrefix, pVmcs->u64GuestPdpte2.u);
3501 pHlp->pfnPrintf(pHlp, " %sPDPTE 3 = %#RX64\n", pszPrefix, pVmcs->u64GuestPdpte3.u);
3502 pHlp->pfnPrintf(pHlp, " %sBNDCFGS = %#RX64\n", pszPrefix, pVmcs->u64GuestBndcfgsMsr.u);
3503
3504 /* Natural width. */
3505 pHlp->pfnPrintf(pHlp, " %scr0 = %#RX64\n", pszPrefix, pVmcs->u64GuestCr0.u);
3506 pHlp->pfnPrintf(pHlp, " %scr3 = %#RX64\n", pszPrefix, pVmcs->u64GuestCr3.u);
3507 pHlp->pfnPrintf(pHlp, " %scr4 = %#RX64\n", pszPrefix, pVmcs->u64GuestCr4.u);
3508 pHlp->pfnPrintf(pHlp, " %sdr7 = %#RX64\n", pszPrefix, pVmcs->u64GuestDr7.u);
3509 pHlp->pfnPrintf(pHlp, " %srsp = %#RX64\n", pszPrefix, pVmcs->u64GuestRsp.u);
3510 pHlp->pfnPrintf(pHlp, " %srip = %#RX64\n", pszPrefix, pVmcs->u64GuestRip.u);
3511 pHlp->pfnPrintf(pHlp, " %srflags = %#RX64 %31s\n",pszPrefix, pVmcs->u64GuestRFlags.u, szEFlags);
3512 pHlp->pfnPrintf(pHlp, " %sPending debug xcpts = %#RX64\n", pszPrefix, pVmcs->u64GuestPendingDbgXcpt.u);
3513 pHlp->pfnPrintf(pHlp, " %sSysEnter ESP = %#RX64\n", pszPrefix, pVmcs->u64GuestSysenterEsp.u);
3514 pHlp->pfnPrintf(pHlp, " %sSysEnter EIP = %#RX64\n", pszPrefix, pVmcs->u64GuestSysenterEip.u);
3515 }
3516
3517 /* Host state. */
3518 {
3519 pHlp->pfnPrintf(pHlp, "%sHost state:\n", pszPrefix);
3520
3521 /* 16-bit. */
3522 pHlp->pfnPrintf(pHlp, " %scs = %#RX16\n", pszPrefix, pVmcs->HostCs);
3523 pHlp->pfnPrintf(pHlp, " %sss = %#RX16\n", pszPrefix, pVmcs->HostSs);
3524 pHlp->pfnPrintf(pHlp, " %sds = %#RX16\n", pszPrefix, pVmcs->HostDs);
3525 pHlp->pfnPrintf(pHlp, " %ses = %#RX16\n", pszPrefix, pVmcs->HostEs);
3526 CPUMVMX_DUMP_HOST_FS_GS_TR(pHlp, pVmcs, Fs, "fs", pszPrefix);
3527 CPUMVMX_DUMP_HOST_FS_GS_TR(pHlp, pVmcs, Gs, "gs", pszPrefix);
3528 CPUMVMX_DUMP_HOST_FS_GS_TR(pHlp, pVmcs, Tr, "tr", pszPrefix);
3529 CPUMVMX_DUMP_HOST_XDTR(pHlp, pVmcs, Gdtr, "gdtr", pszPrefix);
3530 CPUMVMX_DUMP_HOST_XDTR(pHlp, pVmcs, Idtr, "idtr", pszPrefix);
3531
3532 /* 32-bit. */
3533 pHlp->pfnPrintf(pHlp, " %sSysEnter CS = %#RX32\n", pszPrefix, pVmcs->u32HostSysenterCs);
3534
3535 /* 64-bit. */
3536 pHlp->pfnPrintf(pHlp, " %sEFER = %#RX64\n", pszPrefix, pVmcs->u64HostEferMsr.u);
3537 pHlp->pfnPrintf(pHlp, " %sPAT = %#RX64\n", pszPrefix, pVmcs->u64HostPatMsr.u);
3538 pHlp->pfnPrintf(pHlp, " %sPERFGLOBALCTRL = %#RX64\n", pszPrefix, pVmcs->u64HostPerfGlobalCtlMsr.u);
3539
3540 /* Natural width. */
3541 pHlp->pfnPrintf(pHlp, " %scr0 = %#RX64\n", pszPrefix, pVmcs->u64HostCr0.u);
3542 pHlp->pfnPrintf(pHlp, " %scr3 = %#RX64\n", pszPrefix, pVmcs->u64HostCr3.u);
3543 pHlp->pfnPrintf(pHlp, " %scr4 = %#RX64\n", pszPrefix, pVmcs->u64HostCr4.u);
3544 pHlp->pfnPrintf(pHlp, " %sSysEnter ESP = %#RX64\n", pszPrefix, pVmcs->u64HostSysenterEsp.u);
3545 pHlp->pfnPrintf(pHlp, " %sSysEnter EIP = %#RX64\n", pszPrefix, pVmcs->u64HostSysenterEip.u);
3546 pHlp->pfnPrintf(pHlp, " %srsp = %#RX64\n", pszPrefix, pVmcs->u64HostRsp.u);
3547 pHlp->pfnPrintf(pHlp, " %srip = %#RX64\n", pszPrefix, pVmcs->u64HostRip.u);
3548 }
3549
3550 /* Read-only fields. */
3551 {
3552 pHlp->pfnPrintf(pHlp, "%sRead-only data fields:\n", pszPrefix);
3553
3554 /* 16-bit (none currently). */
3555
3556 /* 32-bit. */
3557 pHlp->pfnPrintf(pHlp, " %sExit reason = %u (%s)\n", pszPrefix, pVmcs->u32RoExitReason, HMR3GetVmxExitName(pVmcs->u32RoExitReason));
3558 pHlp->pfnPrintf(pHlp, " %sExit qualification = %#RX64\n", pszPrefix, pVmcs->u64RoExitQual.u);
3559 pHlp->pfnPrintf(pHlp, " %sVM-instruction error = %#RX32\n", pszPrefix, pVmcs->u32RoVmInstrError);
3560 pHlp->pfnPrintf(pHlp, " %sVM-exit intr info = %#RX32\n", pszPrefix, pVmcs->u32RoExitIntInfo);
3561 {
3562 uint32_t const fInfo = pVmcs->u32RoExitIntInfo;
3563 uint8_t const uType = VMX_EXIT_INT_INFO_TYPE(fInfo);
3564 pHlp->pfnPrintf(pHlp, " %sValid = %RTbool\n", pszPrefix, VMX_EXIT_INT_INFO_IS_VALID(fInfo));
3565 pHlp->pfnPrintf(pHlp, " %sType = %#x (%s)\n", pszPrefix, uType, HMVmxGetExitIntInfoTypeDesc(uType));
3566 pHlp->pfnPrintf(pHlp, " %sVector = %#x\n", pszPrefix, VMX_EXIT_INT_INFO_VECTOR(fInfo));
3567 pHlp->pfnPrintf(pHlp, " %sNMI-unblocking-IRET = %RTbool\n", pszPrefix, VMX_EXIT_INT_INFO_IS_NMI_UNBLOCK_IRET(fInfo));
3568 pHlp->pfnPrintf(pHlp, " %sError-code valid = %RTbool\n", pszPrefix, VMX_EXIT_INT_INFO_IS_ERROR_CODE_VALID(fInfo));
3569 }
3570 pHlp->pfnPrintf(pHlp, " %sVM-exit intr error-code = %#RX32\n", pszPrefix, pVmcs->u32RoExitIntErrCode);
3571 pHlp->pfnPrintf(pHlp, " %sIDT-vectoring info = %#RX32\n", pszPrefix, pVmcs->u32RoIdtVectoringInfo);
3572 {
3573 uint32_t const fInfo = pVmcs->u32RoIdtVectoringInfo;
3574 uint8_t const uType = VMX_IDT_VECTORING_INFO_TYPE(fInfo);
3575 pHlp->pfnPrintf(pHlp, " %sValid = %RTbool\n", pszPrefix, VMX_IDT_VECTORING_INFO_IS_VALID(fInfo));
3576 pHlp->pfnPrintf(pHlp, " %sType = %#x (%s)\n", pszPrefix, uType, HMVmxGetIdtVectoringInfoTypeDesc(uType));
3577 pHlp->pfnPrintf(pHlp, " %sVector = %#x\n", pszPrefix, VMX_IDT_VECTORING_INFO_VECTOR(fInfo));
3578 pHlp->pfnPrintf(pHlp, " %sError-code valid = %RTbool\n", pszPrefix, VMX_IDT_VECTORING_INFO_IS_ERROR_CODE_VALID(fInfo));
3579 }
3580 pHlp->pfnPrintf(pHlp, " %sIDT-vectoring error-code = %#RX32\n", pszPrefix, pVmcs->u32RoIdtVectoringErrCode);
3581 pHlp->pfnPrintf(pHlp, " %sVM-exit instruction length = %u bytes\n", pszPrefix, pVmcs->u32RoExitInstrLen);
3582 pHlp->pfnPrintf(pHlp, " %sVM-exit instruction info = %#RX64\n", pszPrefix, pVmcs->u32RoExitInstrInfo);
3583
3584 /* 64-bit. */
3585 pHlp->pfnPrintf(pHlp, " %sGuest-physical addr = %#RX64\n", pszPrefix, pVmcs->u64RoGuestPhysAddr.u);
3586
3587 /* Natural width. */
3588 pHlp->pfnPrintf(pHlp, " %sI/O RCX = %#RX64\n", pszPrefix, pVmcs->u64RoIoRcx.u);
3589 pHlp->pfnPrintf(pHlp, " %sI/O RSI = %#RX64\n", pszPrefix, pVmcs->u64RoIoRsi.u);
3590 pHlp->pfnPrintf(pHlp, " %sI/O RDI = %#RX64\n", pszPrefix, pVmcs->u64RoIoRdi.u);
3591 pHlp->pfnPrintf(pHlp, " %sI/O RIP = %#RX64\n", pszPrefix, pVmcs->u64RoIoRip.u);
3592 pHlp->pfnPrintf(pHlp, " %sGuest-linear addr = %#RX64\n", pszPrefix, pVmcs->u64RoGuestLinearAddr.u);
3593 }
3594
3595#undef CPUMVMX_DUMP_HOST_XDTR
3596#undef CPUMVMX_DUMP_HOST_FS_GS_TR
3597#undef CPUMVMX_DUMP_GUEST_SEGREG
3598#undef CPUMVMX_DUMP_GUEST_XDTR
3599}
3600
3601
3602/**
3603 * Display the guest's hardware-virtualization cpu state.
3604 *
3605 * @param pVM The cross context VM structure.
3606 * @param pHlp The info helper functions.
3607 * @param pszArgs Arguments, ignored.
3608 */
3609static DECLCALLBACK(void) cpumR3InfoGuestHwvirt(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
3610{
3611 RT_NOREF(pszArgs);
3612
3613 PVMCPU pVCpu = VMMGetCpu(pVM);
3614 if (!pVCpu)
3615 pVCpu = &pVM->aCpus[0];
3616
3617 /*
3618 * Figure out what to dump.
3619 *
3620 * In the future we may need to dump everything whether or not we're actively in nested-guest mode
3621 * or not, hence the reason why we use a mask to determine what needs dumping. Currently, we only
3622 * dump hwvirt. state when the guest CPU is executing a nested-guest.
3623 */
3624 /** @todo perhaps make this configurable through pszArgs, depending on how much
3625 * noise we wish to accept when nested hwvirt. isn't used. */
3626#define CPUMHWVIRTDUMP_NONE (0)
3627#define CPUMHWVIRTDUMP_SVM RT_BIT(0)
3628#define CPUMHWVIRTDUMP_VMX RT_BIT(1)
3629#define CPUMHWVIRTDUMP_COMMON RT_BIT(2)
3630#define CPUMHWVIRTDUMP_LAST CPUMHWVIRTDUMP_VMX
3631
3632 PCPUMCTX pCtx = &pVCpu->cpum.s.Guest;
3633 static const char *const s_aHwvirtModes[] = { "No/inactive", "SVM", "VMX", "Common" };
3634 bool const fSvm = pVM->cpum.s.GuestFeatures.fSvm;
3635 bool const fVmx = pVM->cpum.s.GuestFeatures.fVmx;
3636 uint8_t const idxHwvirtState = fSvm ? CPUMHWVIRTDUMP_SVM : (fVmx ? CPUMHWVIRTDUMP_VMX : CPUMHWVIRTDUMP_NONE);
3637 AssertCompile(CPUMHWVIRTDUMP_LAST <= RT_ELEMENTS(s_aHwvirtModes));
3638 Assert(idxHwvirtState < RT_ELEMENTS(s_aHwvirtModes));
3639 const char *pcszHwvirtMode = s_aHwvirtModes[idxHwvirtState];
3640 uint32_t fDumpState = idxHwvirtState | CPUMHWVIRTDUMP_COMMON;
3641
3642 /*
3643 * Dump it.
3644 */
3645 pHlp->pfnPrintf(pHlp, "VCPU[%u] hardware virtualization state:\n", pVCpu->idCpu);
3646
3647 if (fDumpState & CPUMHWVIRTDUMP_COMMON)
3648 pHlp->pfnPrintf(pHlp, "fLocalForcedActions = %#RX32\n", pCtx->hwvirt.fLocalForcedActions);
3649
3650 pHlp->pfnPrintf(pHlp, "%s hwvirt state%s\n", pcszHwvirtMode, (fDumpState & (CPUMHWVIRTDUMP_SVM | CPUMHWVIRTDUMP_VMX)) ?
3651 ":" : "");
3652 if (fDumpState & CPUMHWVIRTDUMP_SVM)
3653 {
3654 pHlp->pfnPrintf(pHlp, " fGif = %RTbool\n", pCtx->hwvirt.fGif);
3655
3656 char szEFlags[80];
3657 cpumR3InfoFormatFlags(&szEFlags[0], pCtx->hwvirt.svm.HostState.rflags.u);
3658 pHlp->pfnPrintf(pHlp, " uMsrHSavePa = %#RX64\n", pCtx->hwvirt.svm.uMsrHSavePa);
3659 pHlp->pfnPrintf(pHlp, " GCPhysVmcb = %#RGp\n", pCtx->hwvirt.svm.GCPhysVmcb);
3660 pHlp->pfnPrintf(pHlp, " VmcbCtrl:\n");
3661 cpumR3InfoSvmVmcbCtrl(pHlp, &pCtx->hwvirt.svm.pVmcbR3->ctrl, " " /* pszPrefix */);
3662 pHlp->pfnPrintf(pHlp, " VmcbStateSave:\n");
3663 cpumR3InfoSvmVmcbStateSave(pHlp, &pCtx->hwvirt.svm.pVmcbR3->guest, " " /* pszPrefix */);
3664 pHlp->pfnPrintf(pHlp, " HostState:\n");
3665 pHlp->pfnPrintf(pHlp, " uEferMsr = %#RX64\n", pCtx->hwvirt.svm.HostState.uEferMsr);
3666 pHlp->pfnPrintf(pHlp, " uCr0 = %#RX64\n", pCtx->hwvirt.svm.HostState.uCr0);
3667 pHlp->pfnPrintf(pHlp, " uCr4 = %#RX64\n", pCtx->hwvirt.svm.HostState.uCr4);
3668 pHlp->pfnPrintf(pHlp, " uCr3 = %#RX64\n", pCtx->hwvirt.svm.HostState.uCr3);
3669 pHlp->pfnPrintf(pHlp, " uRip = %#RX64\n", pCtx->hwvirt.svm.HostState.uRip);
3670 pHlp->pfnPrintf(pHlp, " uRsp = %#RX64\n", pCtx->hwvirt.svm.HostState.uRsp);
3671 pHlp->pfnPrintf(pHlp, " uRax = %#RX64\n", pCtx->hwvirt.svm.HostState.uRax);
3672 pHlp->pfnPrintf(pHlp, " rflags = %#RX64 %31s\n", pCtx->hwvirt.svm.HostState.rflags.u64, szEFlags);
3673 PCPUMSELREG pSel = &pCtx->hwvirt.svm.HostState.es;
3674 pHlp->pfnPrintf(pHlp, " es = {%04x base=%016RX64 limit=%08x flags=%08x}\n",
3675 pSel->Sel, pSel->u64Base, pSel->u32Limit, pSel->Attr.u);
3676 pSel = &pCtx->hwvirt.svm.HostState.cs;
3677 pHlp->pfnPrintf(pHlp, " cs = {%04x base=%016RX64 limit=%08x flags=%08x}\n",
3678 pSel->Sel, pSel->u64Base, pSel->u32Limit, pSel->Attr.u);
3679 pSel = &pCtx->hwvirt.svm.HostState.ss;
3680 pHlp->pfnPrintf(pHlp, " ss = {%04x base=%016RX64 limit=%08x flags=%08x}\n",
3681 pSel->Sel, pSel->u64Base, pSel->u32Limit, pSel->Attr.u);
3682 pSel = &pCtx->hwvirt.svm.HostState.ds;
3683 pHlp->pfnPrintf(pHlp, " ds = {%04x base=%016RX64 limit=%08x flags=%08x}\n",
3684 pSel->Sel, pSel->u64Base, pSel->u32Limit, pSel->Attr.u);
3685 pHlp->pfnPrintf(pHlp, " gdtr = %016RX64:%04x\n", pCtx->hwvirt.svm.HostState.gdtr.pGdt,
3686 pCtx->hwvirt.svm.HostState.gdtr.cbGdt);
3687 pHlp->pfnPrintf(pHlp, " idtr = %016RX64:%04x\n", pCtx->hwvirt.svm.HostState.idtr.pIdt,
3688 pCtx->hwvirt.svm.HostState.idtr.cbIdt);
3689 pHlp->pfnPrintf(pHlp, " cPauseFilter = %RU16\n", pCtx->hwvirt.svm.cPauseFilter);
3690 pHlp->pfnPrintf(pHlp, " cPauseFilterThreshold = %RU32\n", pCtx->hwvirt.svm.cPauseFilterThreshold);
3691 pHlp->pfnPrintf(pHlp, " fInterceptEvents = %u\n", pCtx->hwvirt.svm.fInterceptEvents);
3692 pHlp->pfnPrintf(pHlp, " pvMsrBitmapR3 = %p\n", pCtx->hwvirt.svm.pvMsrBitmapR3);
3693 pHlp->pfnPrintf(pHlp, " pvMsrBitmapR0 = %RKv\n", pCtx->hwvirt.svm.pvMsrBitmapR0);
3694 pHlp->pfnPrintf(pHlp, " pvIoBitmapR3 = %p\n", pCtx->hwvirt.svm.pvIoBitmapR3);
3695 pHlp->pfnPrintf(pHlp, " pvIoBitmapR0 = %RKv\n", pCtx->hwvirt.svm.pvIoBitmapR0);
3696 }
3697
3698 if (fDumpState & CPUMHWVIRTDUMP_VMX)
3699 {
3700 pHlp->pfnPrintf(pHlp, " GCPhysVmxon = %#RGp\n", pCtx->hwvirt.vmx.GCPhysVmxon);
3701 pHlp->pfnPrintf(pHlp, " GCPhysVmcs = %#RGp\n", pCtx->hwvirt.vmx.GCPhysVmcs);
3702 pHlp->pfnPrintf(pHlp, " GCPhysShadowVmcs = %#RGp\n", pCtx->hwvirt.vmx.GCPhysShadowVmcs);
3703 pHlp->pfnPrintf(pHlp, " enmDiag = %u (%s)\n", pCtx->hwvirt.vmx.enmDiag, HMVmxGetDiagDesc(pCtx->hwvirt.vmx.enmDiag));
3704 pHlp->pfnPrintf(pHlp, " enmAbort = %u (%s)\n", pCtx->hwvirt.vmx.enmAbort, HMVmxGetAbortDesc(pCtx->hwvirt.vmx.enmAbort));
3705 pHlp->pfnPrintf(pHlp, " uAbortAux = %u (%#x)\n", pCtx->hwvirt.vmx.uAbortAux, pCtx->hwvirt.vmx.uAbortAux);
3706 pHlp->pfnPrintf(pHlp, " fInVmxRootMode = %RTbool\n", pCtx->hwvirt.vmx.fInVmxRootMode);
3707 pHlp->pfnPrintf(pHlp, " fInVmxNonRootMode = %RTbool\n", pCtx->hwvirt.vmx.fInVmxNonRootMode);
3708 pHlp->pfnPrintf(pHlp, " fInterceptEvents = %RTbool\n", pCtx->hwvirt.vmx.fInterceptEvents);
3709 pHlp->pfnPrintf(pHlp, " fNmiUnblockingIret = %RTbool\n", pCtx->hwvirt.vmx.fNmiUnblockingIret);
3710 pHlp->pfnPrintf(pHlp, " uFirstPauseLoopTick = %RX64\n", pCtx->hwvirt.vmx.uFirstPauseLoopTick);
3711 pHlp->pfnPrintf(pHlp, " uPrevPauseTick = %RX64\n", pCtx->hwvirt.vmx.uPrevPauseTick);
3712 pHlp->pfnPrintf(pHlp, " uVmentryTick = %RX64\n", pCtx->hwvirt.vmx.uVmentryTick);
3713 pHlp->pfnPrintf(pHlp, " offVirtApicWrite = %#RX16\n", pCtx->hwvirt.vmx.offVirtApicWrite);
3714 pHlp->pfnPrintf(pHlp, " VMCS cache:\n");
3715 cpumR3InfoVmxVmcs(pHlp, pCtx->hwvirt.vmx.pVmcsR3, " " /* pszPrefix */);
3716 }
3717
3718#undef CPUMHWVIRTDUMP_NONE
3719#undef CPUMHWVIRTDUMP_COMMON
3720#undef CPUMHWVIRTDUMP_SVM
3721#undef CPUMHWVIRTDUMP_VMX
3722#undef CPUMHWVIRTDUMP_LAST
3723#undef CPUMHWVIRTDUMP_ALL
3724}
3725
3726/**
3727 * Display the current guest instruction
3728 *
3729 * @param pVM The cross context VM structure.
3730 * @param pHlp The info helper functions.
3731 * @param pszArgs Arguments, ignored.
3732 */
3733static DECLCALLBACK(void) cpumR3InfoGuestInstr(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
3734{
3735 NOREF(pszArgs);
3736
3737 PVMCPU pVCpu = VMMGetCpu(pVM);
3738 if (!pVCpu)
3739 pVCpu = &pVM->aCpus[0];
3740
3741 char szInstruction[256];
3742 szInstruction[0] = '\0';
3743 DBGFR3DisasInstrCurrent(pVCpu, szInstruction, sizeof(szInstruction));
3744 pHlp->pfnPrintf(pHlp, "\nCPUM%u: %s\n\n", pVCpu->idCpu, szInstruction);
3745}
3746
3747
3748/**
3749 * Display the hypervisor cpu state.
3750 *
3751 * @param pVM The cross context VM structure.
3752 * @param pHlp The info helper functions.
3753 * @param pszArgs Arguments, ignored.
3754 */
3755static DECLCALLBACK(void) cpumR3InfoHyper(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
3756{
3757 PVMCPU pVCpu = VMMGetCpu(pVM);
3758 if (!pVCpu)
3759 pVCpu = &pVM->aCpus[0];
3760
3761 CPUMDUMPTYPE enmType;
3762 const char *pszComment;
3763 cpumR3InfoParseArg(pszArgs, &enmType, &pszComment);
3764 pHlp->pfnPrintf(pHlp, "Hypervisor CPUM state: %s\n", pszComment);
3765 cpumR3InfoOne(pVM, &pVCpu->cpum.s.Hyper, CPUMCTX2CORE(&pVCpu->cpum.s.Hyper), pHlp, enmType, ".");
3766 pHlp->pfnPrintf(pHlp, "CR4OrMask=%#x CR4AndMask=%#x\n", pVM->cpum.s.CR4.OrMask, pVM->cpum.s.CR4.AndMask);
3767}
3768
3769
3770/**
3771 * Display the host cpu state.
3772 *
3773 * @param pVM The cross context VM structure.
3774 * @param pHlp The info helper functions.
3775 * @param pszArgs Arguments, ignored.
3776 */
3777static DECLCALLBACK(void) cpumR3InfoHost(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
3778{
3779 CPUMDUMPTYPE enmType;
3780 const char *pszComment;
3781 cpumR3InfoParseArg(pszArgs, &enmType, &pszComment);
3782 pHlp->pfnPrintf(pHlp, "Host CPUM state: %s\n", pszComment);
3783
3784 PVMCPU pVCpu = VMMGetCpu(pVM);
3785 if (!pVCpu)
3786 pVCpu = &pVM->aCpus[0];
3787 PCPUMHOSTCTX pCtx = &pVCpu->cpum.s.Host;
3788
3789 /*
3790 * Format the EFLAGS.
3791 */
3792#if HC_ARCH_BITS == 32
3793 uint32_t efl = pCtx->eflags.u32;
3794#else
3795 uint64_t efl = pCtx->rflags;
3796#endif
3797 char szEFlags[80];
3798 cpumR3InfoFormatFlags(&szEFlags[0], efl);
3799
3800 /*
3801 * Format the registers.
3802 */
3803#if HC_ARCH_BITS == 32
3804 pHlp->pfnPrintf(pHlp,
3805 "eax=xxxxxxxx ebx=%08x ecx=xxxxxxxx edx=xxxxxxxx esi=%08x edi=%08x\n"
3806 "eip=xxxxxxxx esp=%08x ebp=%08x iopl=%d %31s\n"
3807 "cs=%04x ds=%04x es=%04x fs=%04x gs=%04x eflags=%08x\n"
3808 "cr0=%08RX64 cr2=xxxxxxxx cr3=%08RX64 cr4=%08RX64 gdtr=%08x:%04x ldtr=%04x\n"
3809 "dr[0]=%08RX64 dr[1]=%08RX64x dr[2]=%08RX64 dr[3]=%08RX64x dr[6]=%08RX64 dr[7]=%08RX64\n"
3810 "SysEnter={cs=%04x eip=%08x esp=%08x}\n"
3811 ,
3812 /*pCtx->eax,*/ pCtx->ebx, /*pCtx->ecx, pCtx->edx,*/ pCtx->esi, pCtx->edi,
3813 /*pCtx->eip,*/ pCtx->esp, pCtx->ebp, X86_EFL_GET_IOPL(efl), szEFlags,
3814 pCtx->cs, pCtx->ds, pCtx->es, pCtx->fs, pCtx->gs, efl,
3815 pCtx->cr0, /*pCtx->cr2,*/ pCtx->cr3, pCtx->cr4,
3816 pCtx->dr0, pCtx->dr1, pCtx->dr2, pCtx->dr3, pCtx->dr6, pCtx->dr7,
3817 (uint32_t)pCtx->gdtr.uAddr, pCtx->gdtr.cb, pCtx->ldtr,
3818 pCtx->SysEnter.cs, pCtx->SysEnter.eip, pCtx->SysEnter.esp);
3819#else
3820 pHlp->pfnPrintf(pHlp,
3821 "rax=xxxxxxxxxxxxxxxx rbx=%016RX64 rcx=xxxxxxxxxxxxxxxx\n"
3822 "rdx=xxxxxxxxxxxxxxxx rsi=%016RX64 rdi=%016RX64\n"
3823 "rip=xxxxxxxxxxxxxxxx rsp=%016RX64 rbp=%016RX64\n"
3824 " r8=xxxxxxxxxxxxxxxx r9=xxxxxxxxxxxxxxxx r10=%016RX64\n"
3825 "r11=%016RX64 r12=%016RX64 r13=%016RX64\n"
3826 "r14=%016RX64 r15=%016RX64\n"
3827 "iopl=%d %31s\n"
3828 "cs=%04x ds=%04x es=%04x fs=%04x gs=%04x eflags=%08RX64\n"
3829 "cr0=%016RX64 cr2=xxxxxxxxxxxxxxxx cr3=%016RX64\n"
3830 "cr4=%016RX64 ldtr=%04x tr=%04x\n"
3831 "dr[0]=%016RX64 dr[1]=%016RX64 dr[2]=%016RX64\n"
3832 "dr[3]=%016RX64 dr[6]=%016RX64 dr[7]=%016RX64\n"
3833 "gdtr=%016RX64:%04x idtr=%016RX64:%04x\n"
3834 "SysEnter={cs=%04x eip=%08x esp=%08x}\n"
3835 "FSbase=%016RX64 GSbase=%016RX64 efer=%08RX64\n"
3836 ,
3837 /*pCtx->rax,*/ pCtx->rbx, /*pCtx->rcx,
3838 pCtx->rdx,*/ pCtx->rsi, pCtx->rdi,
3839 /*pCtx->rip,*/ pCtx->rsp, pCtx->rbp,
3840 /*pCtx->r8, pCtx->r9,*/ pCtx->r10,
3841 pCtx->r11, pCtx->r12, pCtx->r13,
3842 pCtx->r14, pCtx->r15,
3843 X86_EFL_GET_IOPL(efl), szEFlags,
3844 pCtx->cs, pCtx->ds, pCtx->es, pCtx->fs, pCtx->gs, efl,
3845 pCtx->cr0, /*pCtx->cr2,*/ pCtx->cr3,
3846 pCtx->cr4, pCtx->ldtr, pCtx->tr,
3847 pCtx->dr0, pCtx->dr1, pCtx->dr2,
3848 pCtx->dr3, pCtx->dr6, pCtx->dr7,
3849 pCtx->gdtr.uAddr, pCtx->gdtr.cb, pCtx->idtr.uAddr, pCtx->idtr.cb,
3850 pCtx->SysEnter.cs, pCtx->SysEnter.eip, pCtx->SysEnter.esp,
3851 pCtx->FSbase, pCtx->GSbase, pCtx->efer);
3852#endif
3853}
3854
3855/**
3856 * Structure used when disassembling and instructions in DBGF.
3857 * This is used so the reader function can get the stuff it needs.
3858 */
3859typedef struct CPUMDISASSTATE
3860{
3861 /** Pointer to the CPU structure. */
3862 PDISCPUSTATE pCpu;
3863 /** Pointer to the VM. */
3864 PVM pVM;
3865 /** Pointer to the VMCPU. */
3866 PVMCPU pVCpu;
3867 /** Pointer to the first byte in the segment. */
3868 RTGCUINTPTR GCPtrSegBase;
3869 /** Pointer to the byte after the end of the segment. (might have wrapped!) */
3870 RTGCUINTPTR GCPtrSegEnd;
3871 /** The size of the segment minus 1. */
3872 RTGCUINTPTR cbSegLimit;
3873 /** Pointer to the current page - R3 Ptr. */
3874 void const *pvPageR3;
3875 /** Pointer to the current page - GC Ptr. */
3876 RTGCPTR pvPageGC;
3877 /** The lock information that PGMPhysReleasePageMappingLock needs. */
3878 PGMPAGEMAPLOCK PageMapLock;
3879 /** Whether the PageMapLock is valid or not. */
3880 bool fLocked;
3881 /** 64 bits mode or not. */
3882 bool f64Bits;
3883} CPUMDISASSTATE, *PCPUMDISASSTATE;
3884
3885
3886/**
3887 * @callback_method_impl{FNDISREADBYTES}
3888 */
3889static DECLCALLBACK(int) cpumR3DisasInstrRead(PDISCPUSTATE pDis, uint8_t offInstr, uint8_t cbMinRead, uint8_t cbMaxRead)
3890{
3891 PCPUMDISASSTATE pState = (PCPUMDISASSTATE)pDis->pvUser;
3892 for (;;)
3893 {
3894 RTGCUINTPTR GCPtr = pDis->uInstrAddr + offInstr + pState->GCPtrSegBase;
3895
3896 /*
3897 * Need to update the page translation?
3898 */
3899 if ( !pState->pvPageR3
3900 || (GCPtr >> PAGE_SHIFT) != (pState->pvPageGC >> PAGE_SHIFT))
3901 {
3902 int rc = VINF_SUCCESS;
3903
3904 /* translate the address */
3905 pState->pvPageGC = GCPtr & PAGE_BASE_GC_MASK;
3906 if ( VM_IS_RAW_MODE_ENABLED(pState->pVM)
3907 && MMHyperIsInsideArea(pState->pVM, pState->pvPageGC))
3908 {
3909 pState->pvPageR3 = MMHyperRCToR3(pState->pVM, (RTRCPTR)pState->pvPageGC);
3910 if (!pState->pvPageR3)
3911 rc = VERR_INVALID_POINTER;
3912 }
3913 else
3914 {
3915 /* Release mapping lock previously acquired. */
3916 if (pState->fLocked)
3917 PGMPhysReleasePageMappingLock(pState->pVM, &pState->PageMapLock);
3918 rc = PGMPhysGCPtr2CCPtrReadOnly(pState->pVCpu, pState->pvPageGC, &pState->pvPageR3, &pState->PageMapLock);
3919 pState->fLocked = RT_SUCCESS_NP(rc);
3920 }
3921 if (RT_FAILURE(rc))
3922 {
3923 pState->pvPageR3 = NULL;
3924 return rc;
3925 }
3926 }
3927
3928 /*
3929 * Check the segment limit.
3930 */
3931 if (!pState->f64Bits && pDis->uInstrAddr + offInstr > pState->cbSegLimit)
3932 return VERR_OUT_OF_SELECTOR_BOUNDS;
3933
3934 /*
3935 * Calc how much we can read.
3936 */
3937 uint32_t cb = PAGE_SIZE - (GCPtr & PAGE_OFFSET_MASK);
3938 if (!pState->f64Bits)
3939 {
3940 RTGCUINTPTR cbSeg = pState->GCPtrSegEnd - GCPtr;
3941 if (cb > cbSeg && cbSeg)
3942 cb = cbSeg;
3943 }
3944 if (cb > cbMaxRead)
3945 cb = cbMaxRead;
3946
3947 /*
3948 * Read and advance or exit.
3949 */
3950 memcpy(&pDis->abInstr[offInstr], (uint8_t *)pState->pvPageR3 + (GCPtr & PAGE_OFFSET_MASK), cb);
3951 offInstr += (uint8_t)cb;
3952 if (cb >= cbMinRead)
3953 {
3954 pDis->cbCachedInstr = offInstr;
3955 return VINF_SUCCESS;
3956 }
3957 cbMinRead -= (uint8_t)cb;
3958 cbMaxRead -= (uint8_t)cb;
3959 }
3960}
3961
3962
3963/**
3964 * Disassemble an instruction and return the information in the provided structure.
3965 *
3966 * @returns VBox status code.
3967 * @param pVM The cross context VM structure.
3968 * @param pVCpu The cross context virtual CPU structure.
3969 * @param pCtx Pointer to the guest CPU context.
3970 * @param GCPtrPC Program counter (relative to CS) to disassemble from.
3971 * @param pCpu Disassembly state.
3972 * @param pszPrefix String prefix for logging (debug only).
3973 *
3974 */
3975VMMR3DECL(int) CPUMR3DisasmInstrCPU(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, RTGCPTR GCPtrPC, PDISCPUSTATE pCpu,
3976 const char *pszPrefix)
3977{
3978 CPUMDISASSTATE State;
3979 int rc;
3980
3981 const PGMMODE enmMode = PGMGetGuestMode(pVCpu);
3982 State.pCpu = pCpu;
3983 State.pvPageGC = 0;
3984 State.pvPageR3 = NULL;
3985 State.pVM = pVM;
3986 State.pVCpu = pVCpu;
3987 State.fLocked = false;
3988 State.f64Bits = false;
3989
3990 /*
3991 * Get selector information.
3992 */
3993 DISCPUMODE enmDisCpuMode;
3994 if ( (pCtx->cr0 & X86_CR0_PE)
3995 && pCtx->eflags.Bits.u1VM == 0)
3996 {
3997 if (!CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, &pCtx->cs))
3998 {
3999# ifdef VBOX_WITH_RAW_MODE_NOT_R0
4000 CPUMGuestLazyLoadHiddenSelectorReg(pVCpu, &pCtx->cs);
4001# endif
4002 if (!CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, &pCtx->cs))
4003 return VERR_CPUM_HIDDEN_CS_LOAD_ERROR;
4004 }
4005 State.f64Bits = enmMode >= PGMMODE_AMD64 && pCtx->cs.Attr.n.u1Long;
4006 State.GCPtrSegBase = pCtx->cs.u64Base;
4007 State.GCPtrSegEnd = pCtx->cs.u32Limit + 1 + (RTGCUINTPTR)pCtx->cs.u64Base;
4008 State.cbSegLimit = pCtx->cs.u32Limit;
4009 enmDisCpuMode = (State.f64Bits)
4010 ? DISCPUMODE_64BIT
4011 : pCtx->cs.Attr.n.u1DefBig
4012 ? DISCPUMODE_32BIT
4013 : DISCPUMODE_16BIT;
4014 }
4015 else
4016 {
4017 /* real or V86 mode */
4018 enmDisCpuMode = DISCPUMODE_16BIT;
4019 State.GCPtrSegBase = pCtx->cs.Sel * 16;
4020 State.GCPtrSegEnd = 0xFFFFFFFF;
4021 State.cbSegLimit = 0xFFFFFFFF;
4022 }
4023
4024 /*
4025 * Disassemble the instruction.
4026 */
4027 uint32_t cbInstr;
4028#ifndef LOG_ENABLED
4029 RT_NOREF_PV(pszPrefix);
4030 rc = DISInstrWithReader(GCPtrPC, enmDisCpuMode, cpumR3DisasInstrRead, &State, pCpu, &cbInstr);
4031 if (RT_SUCCESS(rc))
4032 {
4033#else
4034 char szOutput[160];
4035 rc = DISInstrToStrWithReader(GCPtrPC, enmDisCpuMode, cpumR3DisasInstrRead, &State,
4036 pCpu, &cbInstr, szOutput, sizeof(szOutput));
4037 if (RT_SUCCESS(rc))
4038 {
4039 /* log it */
4040 if (pszPrefix)
4041 Log(("%s-CPU%d: %s", pszPrefix, pVCpu->idCpu, szOutput));
4042 else
4043 Log(("%s", szOutput));
4044#endif
4045 rc = VINF_SUCCESS;
4046 }
4047 else
4048 Log(("CPUMR3DisasmInstrCPU: DISInstr failed for %04X:%RGv rc=%Rrc\n", pCtx->cs.Sel, GCPtrPC, rc));
4049
4050 /* Release mapping lock acquired in cpumR3DisasInstrRead. */
4051 if (State.fLocked)
4052 PGMPhysReleasePageMappingLock(pVM, &State.PageMapLock);
4053
4054 return rc;
4055}
4056
4057
4058
4059/**
4060 * API for controlling a few of the CPU features found in CR4.
4061 *
4062 * Currently only X86_CR4_TSD is accepted as input.
4063 *
4064 * @returns VBox status code.
4065 *
4066 * @param pVM The cross context VM structure.
4067 * @param fOr The CR4 OR mask.
4068 * @param fAnd The CR4 AND mask.
4069 */
4070VMMR3DECL(int) CPUMR3SetCR4Feature(PVM pVM, RTHCUINTREG fOr, RTHCUINTREG fAnd)
4071{
4072 AssertMsgReturn(!(fOr & ~(X86_CR4_TSD)), ("%#x\n", fOr), VERR_INVALID_PARAMETER);
4073 AssertMsgReturn((fAnd & ~(X86_CR4_TSD)) == ~(X86_CR4_TSD), ("%#x\n", fAnd), VERR_INVALID_PARAMETER);
4074
4075 pVM->cpum.s.CR4.OrMask &= fAnd;
4076 pVM->cpum.s.CR4.OrMask |= fOr;
4077
4078 return VINF_SUCCESS;
4079}
4080
4081
4082/**
4083 * Enters REM, gets and resets the changed flags (CPUM_CHANGED_*).
4084 *
4085 * Only REM should ever call this function!
4086 *
4087 * @returns The changed flags.
4088 * @param pVCpu The cross context virtual CPU structure.
4089 * @param puCpl Where to return the current privilege level (CPL).
4090 */
4091VMMR3DECL(uint32_t) CPUMR3RemEnter(PVMCPU pVCpu, uint32_t *puCpl)
4092{
4093 Assert(!pVCpu->cpum.s.fRawEntered);
4094 Assert(!pVCpu->cpum.s.fRemEntered);
4095
4096 /*
4097 * Get the CPL first.
4098 */
4099 *puCpl = CPUMGetGuestCPL(pVCpu);
4100
4101 /*
4102 * Get and reset the flags.
4103 */
4104 uint32_t fFlags = pVCpu->cpum.s.fChanged;
4105 pVCpu->cpum.s.fChanged = 0;
4106
4107 /** @todo change the switcher to use the fChanged flags. */
4108 if (pVCpu->cpum.s.fUseFlags & CPUM_USED_FPU_SINCE_REM)
4109 {
4110 fFlags |= CPUM_CHANGED_FPU_REM;
4111 pVCpu->cpum.s.fUseFlags &= ~CPUM_USED_FPU_SINCE_REM;
4112 }
4113
4114 pVCpu->cpum.s.fRemEntered = true;
4115 return fFlags;
4116}
4117
4118
4119/**
4120 * Leaves REM.
4121 *
4122 * @param pVCpu The cross context virtual CPU structure.
4123 * @param fNoOutOfSyncSels This is @c false if there are out of sync
4124 * registers.
4125 */
4126VMMR3DECL(void) CPUMR3RemLeave(PVMCPU pVCpu, bool fNoOutOfSyncSels)
4127{
4128 Assert(!pVCpu->cpum.s.fRawEntered);
4129 Assert(pVCpu->cpum.s.fRemEntered);
4130
4131 RT_NOREF_PV(fNoOutOfSyncSels);
4132
4133 pVCpu->cpum.s.fRemEntered = false;
4134}
4135
4136
4137/**
4138 * Called when the ring-3 init phase completes.
4139 *
4140 * @returns VBox status code.
4141 * @param pVM The cross context VM structure.
4142 * @param enmWhat Which init phase.
4143 */
4144VMMR3DECL(int) CPUMR3InitCompleted(PVM pVM, VMINITCOMPLETED enmWhat)
4145{
4146 switch (enmWhat)
4147 {
4148 case VMINITCOMPLETED_RING3:
4149 {
4150 /*
4151 * Figure out if the guest uses 32-bit or 64-bit FPU state at runtime for 64-bit capable VMs.
4152 * Only applicable/used on 64-bit hosts, refer CPUMR0A.asm. See @bugref{7138}.
4153 */
4154 bool const fSupportsLongMode = VMR3IsLongModeAllowed(pVM);
4155 for (VMCPUID i = 0; i < pVM->cCpus; i++)
4156 {
4157 PVMCPU pVCpu = &pVM->aCpus[i];
4158 /* While loading a saved-state we fix it up in, cpumR3LoadDone(). */
4159 if (fSupportsLongMode)
4160 pVCpu->cpum.s.fUseFlags |= CPUM_USE_SUPPORTS_LONGMODE;
4161 }
4162
4163 /* Register statistic counters for MSRs. */
4164 cpumR3MsrRegStats(pVM);
4165 break;
4166 }
4167
4168 default:
4169 break;
4170 }
4171 return VINF_SUCCESS;
4172}
4173
4174
4175/**
4176 * Called when the ring-0 init phases completed.
4177 *
4178 * @param pVM The cross context VM structure.
4179 */
4180VMMR3DECL(void) CPUMR3LogCpuIdAndMsrFeatures(PVM pVM)
4181{
4182 /*
4183 * Enable log buffering as we're going to log a lot of lines.
4184 */
4185 bool const fOldBuffered = RTLogRelSetBuffering(true /*fBuffered*/);
4186
4187 /*
4188 * Log the cpuid.
4189 */
4190 RTCPUSET OnlineSet;
4191 LogRel(("CPUM: Logical host processors: %u present, %u max, %u online, online mask: %016RX64\n",
4192 (unsigned)RTMpGetPresentCount(), (unsigned)RTMpGetCount(), (unsigned)RTMpGetOnlineCount(),
4193 RTCpuSetToU64(RTMpGetOnlineSet(&OnlineSet)) ));
4194 RTCPUID cCores = RTMpGetCoreCount();
4195 if (cCores)
4196 LogRel(("CPUM: Physical host cores: %u\n", (unsigned)cCores));
4197 LogRel(("************************* CPUID dump ************************\n"));
4198 DBGFR3Info(pVM->pUVM, "cpuid", "verbose", DBGFR3InfoLogRelHlp());
4199 LogRel(("\n"));
4200 DBGFR3_INFO_LOG_SAFE(pVM, "cpuid", "verbose"); /* macro */
4201 LogRel(("******************** End of CPUID dump **********************\n"));
4202
4203 /*
4204 * Log VT-x extended features.
4205 *
4206 * SVM features are currently all covered under CPUID so there is nothing
4207 * to do here for SVM.
4208 */
4209 if (pVM->cpum.s.HostFeatures.fVmx)
4210 {
4211 LogRel(("*********************** VT-x features ***********************\n"));
4212 DBGFR3Info(pVM->pUVM, "cpumvmxfeat", "default", DBGFR3InfoLogRelHlp());
4213 LogRel(("\n"));
4214 LogRel(("******************* End of VT-x features ********************\n"));
4215 }
4216
4217 /*
4218 * Restore the log buffering state to what it was previously.
4219 */
4220 RTLogRelSetBuffering(fOldBuffered);
4221}
4222
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