VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/CPUMAllRegs.cpp@ 100061

Last change on this file since 100061 was 99739, checked in by vboxsync, 19 months ago

*: doxygen corrections (mostly about removing @returns from functions returning void).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 99.3 KB
Line 
1/* $Id: CPUMAllRegs.cpp 99739 2023-05-11 01:01:08Z vboxsync $ */
2/** @file
3 * CPUM - CPU Monitor(/Manager) - Getters and Setters.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_CPUM
33#include <VBox/vmm/cpum.h>
34#include <VBox/vmm/dbgf.h>
35#include <VBox/vmm/apic.h>
36#include <VBox/vmm/pgm.h>
37#include <VBox/vmm/mm.h>
38#include <VBox/vmm/em.h>
39#include <VBox/vmm/nem.h>
40#include <VBox/vmm/hm.h>
41#include "CPUMInternal.h"
42#include <VBox/vmm/vmcc.h>
43#include <VBox/err.h>
44#include <VBox/dis.h>
45#include <VBox/log.h>
46#include <VBox/vmm/hm.h>
47#include <VBox/vmm/tm.h>
48#include <iprt/assert.h>
49#include <iprt/asm.h>
50#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
51# include <iprt/asm-amd64-x86.h>
52#endif
53#ifdef IN_RING3
54# include <iprt/thread.h>
55#endif
56
57/** Disable stack frame pointer generation here. */
58#if defined(_MSC_VER) && !defined(DEBUG) && defined(RT_ARCH_X86)
59# pragma optimize("y", off)
60#endif
61
62AssertCompile2MemberOffsets(VM, cpum.s.GuestFeatures, cpum.ro.GuestFeatures);
63
64
65/*********************************************************************************************************************************
66* Defined Constants And Macros *
67*********************************************************************************************************************************/
68/**
69 * Converts a CPUMCPU::Guest pointer into a VMCPU pointer.
70 *
71 * @returns Pointer to the Virtual CPU.
72 * @param a_pGuestCtx Pointer to the guest context.
73 */
74#define CPUM_GUEST_CTX_TO_VMCPU(a_pGuestCtx) RT_FROM_MEMBER(a_pGuestCtx, VMCPU, cpum.s.Guest)
75
76/**
77 * Lazily loads the hidden parts of a selector register when using raw-mode.
78 */
79#define CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(a_pVCpu, a_pSReg) \
80 Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(a_pVCpu, a_pSReg))
81
82/** @def CPUM_INT_ASSERT_NOT_EXTRN
83 * Macro for asserting that @a a_fNotExtrn are present.
84 *
85 * @param a_pVCpu The cross context virtual CPU structure of the calling EMT.
86 * @param a_fNotExtrn Mask of CPUMCTX_EXTRN_XXX bits to check.
87 */
88#define CPUM_INT_ASSERT_NOT_EXTRN(a_pVCpu, a_fNotExtrn) \
89 AssertMsg(!((a_pVCpu)->cpum.s.Guest.fExtrn & (a_fNotExtrn)), \
90 ("%#RX64; a_fNotExtrn=%#RX64\n", (a_pVCpu)->cpum.s.Guest.fExtrn, (a_fNotExtrn)))
91
92
93VMMDECL(void) CPUMSetHyperCR3(PVMCPU pVCpu, uint32_t cr3)
94{
95 pVCpu->cpum.s.Hyper.cr3 = cr3;
96}
97
98VMMDECL(uint32_t) CPUMGetHyperCR3(PVMCPU pVCpu)
99{
100 return pVCpu->cpum.s.Hyper.cr3;
101}
102
103
104/** @def MAYBE_LOAD_DRx
105 * Macro for updating DRx values in raw-mode and ring-0 contexts.
106 */
107#ifdef IN_RING0
108# define MAYBE_LOAD_DRx(a_pVCpu, a_fnLoad, a_uValue) do { a_fnLoad(a_uValue); } while (0)
109#else
110# define MAYBE_LOAD_DRx(a_pVCpu, a_fnLoad, a_uValue) do { } while (0)
111#endif
112
113VMMDECL(void) CPUMSetHyperDR0(PVMCPU pVCpu, RTGCUINTREG uDr0)
114{
115 pVCpu->cpum.s.Hyper.dr[0] = uDr0;
116 MAYBE_LOAD_DRx(pVCpu, ASMSetDR0, uDr0);
117}
118
119
120VMMDECL(void) CPUMSetHyperDR1(PVMCPU pVCpu, RTGCUINTREG uDr1)
121{
122 pVCpu->cpum.s.Hyper.dr[1] = uDr1;
123 MAYBE_LOAD_DRx(pVCpu, ASMSetDR1, uDr1);
124}
125
126
127VMMDECL(void) CPUMSetHyperDR2(PVMCPU pVCpu, RTGCUINTREG uDr2)
128{
129 pVCpu->cpum.s.Hyper.dr[2] = uDr2;
130 MAYBE_LOAD_DRx(pVCpu, ASMSetDR2, uDr2);
131}
132
133
134VMMDECL(void) CPUMSetHyperDR3(PVMCPU pVCpu, RTGCUINTREG uDr3)
135{
136 pVCpu->cpum.s.Hyper.dr[3] = uDr3;
137 MAYBE_LOAD_DRx(pVCpu, ASMSetDR3, uDr3);
138}
139
140
141VMMDECL(void) CPUMSetHyperDR6(PVMCPU pVCpu, RTGCUINTREG uDr6)
142{
143 pVCpu->cpum.s.Hyper.dr[6] = uDr6;
144}
145
146
147VMMDECL(void) CPUMSetHyperDR7(PVMCPU pVCpu, RTGCUINTREG uDr7)
148{
149 pVCpu->cpum.s.Hyper.dr[7] = uDr7;
150}
151
152
153VMMDECL(RTGCUINTREG) CPUMGetHyperDR0(PVMCPU pVCpu)
154{
155 return pVCpu->cpum.s.Hyper.dr[0];
156}
157
158
159VMMDECL(RTGCUINTREG) CPUMGetHyperDR1(PVMCPU pVCpu)
160{
161 return pVCpu->cpum.s.Hyper.dr[1];
162}
163
164
165VMMDECL(RTGCUINTREG) CPUMGetHyperDR2(PVMCPU pVCpu)
166{
167 return pVCpu->cpum.s.Hyper.dr[2];
168}
169
170
171VMMDECL(RTGCUINTREG) CPUMGetHyperDR3(PVMCPU pVCpu)
172{
173 return pVCpu->cpum.s.Hyper.dr[3];
174}
175
176
177VMMDECL(RTGCUINTREG) CPUMGetHyperDR6(PVMCPU pVCpu)
178{
179 return pVCpu->cpum.s.Hyper.dr[6];
180}
181
182
183VMMDECL(RTGCUINTREG) CPUMGetHyperDR7(PVMCPU pVCpu)
184{
185 return pVCpu->cpum.s.Hyper.dr[7];
186}
187
188
189/**
190 * Checks that the special cookie stored in unused reserved RFLAGS bits
191 *
192 * @retval true if cookie is ok.
193 * @retval false if cookie is not ok.
194 * @param pVM The cross context VM structure.
195 * @param pVCpu The cross context virtual CPU structure.
196 */
197VMM_INT_DECL(bool) CPUMAssertGuestRFlagsCookie(PVM pVM, PVMCPU pVCpu)
198{
199 AssertLogRelMsgReturn( ( pVCpu->cpum.s.Guest.rflags.uBoth
200 & ~(uint64_t)(CPUMX86EFLAGS_HW_MASK_64 | CPUMX86EFLAGS_INT_MASK_64))
201 == pVM->cpum.s.fReservedRFlagsCookie
202 && (pVCpu->cpum.s.Guest.rflags.uBoth & X86_EFL_RA1_MASK) == X86_EFL_RA1_MASK
203 && (pVCpu->cpum.s.Guest.rflags.uBoth & X86_EFL_RAZ_MASK & CPUMX86EFLAGS_HW_MASK_64) == 0,
204 ("rflags=%#RX64 vs fReservedRFlagsCookie=%#RX64\n",
205 pVCpu->cpum.s.Guest.rflags.uBoth, pVM->cpum.s.fReservedRFlagsCookie),
206 false);
207 return true;
208}
209
210
211/**
212 * Queries the pointer to the internal CPUMCTX structure.
213 *
214 * @returns The CPUMCTX pointer.
215 * @param pVCpu The cross context virtual CPU structure.
216 */
217VMMDECL(PCPUMCTX) CPUMQueryGuestCtxPtr(PVMCPU pVCpu)
218{
219 return &pVCpu->cpum.s.Guest;
220}
221
222
223/**
224 * Queries the pointer to the internal CPUMCTXMSRS structure.
225 *
226 * This is for NEM only.
227 *
228 * @returns The CPUMCTX pointer.
229 * @param pVCpu The cross context virtual CPU structure.
230 */
231VMM_INT_DECL(PCPUMCTXMSRS) CPUMQueryGuestCtxMsrsPtr(PVMCPU pVCpu)
232{
233 return &pVCpu->cpum.s.GuestMsrs;
234}
235
236
237VMMDECL(int) CPUMSetGuestGDTR(PVMCPU pVCpu, uint64_t GCPtrBase, uint16_t cbLimit)
238{
239 pVCpu->cpum.s.Guest.gdtr.cbGdt = cbLimit;
240 pVCpu->cpum.s.Guest.gdtr.pGdt = GCPtrBase;
241 pVCpu->cpum.s.Guest.fExtrn &= ~CPUMCTX_EXTRN_GDTR;
242 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_GDTR;
243 return VINF_SUCCESS; /* formality, consider it void. */
244}
245
246
247VMMDECL(int) CPUMSetGuestIDTR(PVMCPU pVCpu, uint64_t GCPtrBase, uint16_t cbLimit)
248{
249 pVCpu->cpum.s.Guest.idtr.cbIdt = cbLimit;
250 pVCpu->cpum.s.Guest.idtr.pIdt = GCPtrBase;
251 pVCpu->cpum.s.Guest.fExtrn &= ~CPUMCTX_EXTRN_IDTR;
252 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_IDTR;
253 return VINF_SUCCESS; /* formality, consider it void. */
254}
255
256
257VMMDECL(int) CPUMSetGuestTR(PVMCPU pVCpu, uint16_t tr)
258{
259 pVCpu->cpum.s.Guest.tr.Sel = tr;
260 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_TR;
261 return VINF_SUCCESS; /* formality, consider it void. */
262}
263
264
265VMMDECL(int) CPUMSetGuestLDTR(PVMCPU pVCpu, uint16_t ldtr)
266{
267 pVCpu->cpum.s.Guest.ldtr.Sel = ldtr;
268 /* The caller will set more hidden bits if it has them. */
269 pVCpu->cpum.s.Guest.ldtr.ValidSel = 0;
270 pVCpu->cpum.s.Guest.ldtr.fFlags = 0;
271 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_LDTR;
272 return VINF_SUCCESS; /* formality, consider it void. */
273}
274
275
276/**
277 * Set the guest CR0.
278 *
279 * When called in GC, the hyper CR0 may be updated if that is
280 * required. The caller only has to take special action if AM,
281 * WP, PG or PE changes.
282 *
283 * @returns VINF_SUCCESS (consider it void).
284 * @param pVCpu The cross context virtual CPU structure.
285 * @param cr0 The new CR0 value.
286 */
287VMMDECL(int) CPUMSetGuestCR0(PVMCPUCC pVCpu, uint64_t cr0)
288{
289 /*
290 * Check for changes causing TLB flushes (for REM).
291 * The caller is responsible for calling PGM when appropriate.
292 */
293 if ( (cr0 & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE))
294 != (pVCpu->cpum.s.Guest.cr0 & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE)))
295 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_GLOBAL_TLB_FLUSH;
296 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_CR0;
297
298 /*
299 * Let PGM know if the WP goes from 0 to 1 (netware WP0+RO+US hack)
300 */
301 if (((cr0 ^ pVCpu->cpum.s.Guest.cr0) & X86_CR0_WP) && (cr0 & X86_CR0_WP))
302 PGMCr0WpEnabled(pVCpu);
303
304 /* The ET flag is settable on a 386 and hardwired on 486+. */
305 if ( !(cr0 & X86_CR0_ET)
306 && pVCpu->CTX_SUFF(pVM)->cpum.s.GuestFeatures.enmMicroarch != kCpumMicroarch_Intel_80386)
307 cr0 |= X86_CR0_ET;
308
309 pVCpu->cpum.s.Guest.cr0 = cr0;
310 pVCpu->cpum.s.Guest.fExtrn &= ~CPUMCTX_EXTRN_CR0;
311 return VINF_SUCCESS;
312}
313
314
315VMMDECL(int) CPUMSetGuestCR2(PVMCPU pVCpu, uint64_t cr2)
316{
317 pVCpu->cpum.s.Guest.cr2 = cr2;
318 pVCpu->cpum.s.Guest.fExtrn &= ~CPUMCTX_EXTRN_CR2;
319 return VINF_SUCCESS;
320}
321
322
323VMMDECL(int) CPUMSetGuestCR3(PVMCPU pVCpu, uint64_t cr3)
324{
325 pVCpu->cpum.s.Guest.cr3 = cr3;
326 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_CR3;
327 pVCpu->cpum.s.Guest.fExtrn &= ~CPUMCTX_EXTRN_CR3;
328 return VINF_SUCCESS;
329}
330
331
332VMMDECL(int) CPUMSetGuestCR4(PVMCPU pVCpu, uint64_t cr4)
333{
334 /* Note! We don't bother with OSXSAVE and legacy CPUID patches. */
335
336 if ( (cr4 & (X86_CR4_PGE | X86_CR4_PAE | X86_CR4_PSE))
337 != (pVCpu->cpum.s.Guest.cr4 & (X86_CR4_PGE | X86_CR4_PAE | X86_CR4_PSE)))
338 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_GLOBAL_TLB_FLUSH;
339
340 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_CR4;
341 pVCpu->cpum.s.Guest.cr4 = cr4;
342 pVCpu->cpum.s.Guest.fExtrn &= ~CPUMCTX_EXTRN_CR4;
343 return VINF_SUCCESS;
344}
345
346
347VMMDECL(int) CPUMSetGuestEFlags(PVMCPU pVCpu, uint32_t eflags)
348{
349 pVCpu->cpum.s.Guest.eflags.u = eflags;
350 pVCpu->cpum.s.Guest.fExtrn &= ~CPUMCTX_EXTRN_RFLAGS;
351 return VINF_SUCCESS;
352}
353
354
355VMMDECL(int) CPUMSetGuestEIP(PVMCPU pVCpu, uint32_t eip)
356{
357 pVCpu->cpum.s.Guest.eip = eip;
358 return VINF_SUCCESS;
359}
360
361
362VMMDECL(int) CPUMSetGuestEAX(PVMCPU pVCpu, uint32_t eax)
363{
364 pVCpu->cpum.s.Guest.eax = eax;
365 return VINF_SUCCESS;
366}
367
368
369VMMDECL(int) CPUMSetGuestEBX(PVMCPU pVCpu, uint32_t ebx)
370{
371 pVCpu->cpum.s.Guest.ebx = ebx;
372 return VINF_SUCCESS;
373}
374
375
376VMMDECL(int) CPUMSetGuestECX(PVMCPU pVCpu, uint32_t ecx)
377{
378 pVCpu->cpum.s.Guest.ecx = ecx;
379 return VINF_SUCCESS;
380}
381
382
383VMMDECL(int) CPUMSetGuestEDX(PVMCPU pVCpu, uint32_t edx)
384{
385 pVCpu->cpum.s.Guest.edx = edx;
386 return VINF_SUCCESS;
387}
388
389
390VMMDECL(int) CPUMSetGuestESP(PVMCPU pVCpu, uint32_t esp)
391{
392 pVCpu->cpum.s.Guest.esp = esp;
393 return VINF_SUCCESS;
394}
395
396
397VMMDECL(int) CPUMSetGuestEBP(PVMCPU pVCpu, uint32_t ebp)
398{
399 pVCpu->cpum.s.Guest.ebp = ebp;
400 return VINF_SUCCESS;
401}
402
403
404VMMDECL(int) CPUMSetGuestESI(PVMCPU pVCpu, uint32_t esi)
405{
406 pVCpu->cpum.s.Guest.esi = esi;
407 return VINF_SUCCESS;
408}
409
410
411VMMDECL(int) CPUMSetGuestEDI(PVMCPU pVCpu, uint32_t edi)
412{
413 pVCpu->cpum.s.Guest.edi = edi;
414 return VINF_SUCCESS;
415}
416
417
418VMMDECL(int) CPUMSetGuestSS(PVMCPU pVCpu, uint16_t ss)
419{
420 pVCpu->cpum.s.Guest.ss.Sel = ss;
421 return VINF_SUCCESS;
422}
423
424
425VMMDECL(int) CPUMSetGuestCS(PVMCPU pVCpu, uint16_t cs)
426{
427 pVCpu->cpum.s.Guest.cs.Sel = cs;
428 return VINF_SUCCESS;
429}
430
431
432VMMDECL(int) CPUMSetGuestDS(PVMCPU pVCpu, uint16_t ds)
433{
434 pVCpu->cpum.s.Guest.ds.Sel = ds;
435 return VINF_SUCCESS;
436}
437
438
439VMMDECL(int) CPUMSetGuestES(PVMCPU pVCpu, uint16_t es)
440{
441 pVCpu->cpum.s.Guest.es.Sel = es;
442 return VINF_SUCCESS;
443}
444
445
446VMMDECL(int) CPUMSetGuestFS(PVMCPU pVCpu, uint16_t fs)
447{
448 pVCpu->cpum.s.Guest.fs.Sel = fs;
449 return VINF_SUCCESS;
450}
451
452
453VMMDECL(int) CPUMSetGuestGS(PVMCPU pVCpu, uint16_t gs)
454{
455 pVCpu->cpum.s.Guest.gs.Sel = gs;
456 return VINF_SUCCESS;
457}
458
459
460VMMDECL(void) CPUMSetGuestEFER(PVMCPU pVCpu, uint64_t val)
461{
462 pVCpu->cpum.s.Guest.msrEFER = val;
463 pVCpu->cpum.s.Guest.fExtrn &= ~CPUMCTX_EXTRN_EFER;
464}
465
466
467VMMDECL(RTGCPTR) CPUMGetGuestIDTR(PCVMCPU pVCpu, uint16_t *pcbLimit)
468{
469 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_IDTR);
470 if (pcbLimit)
471 *pcbLimit = pVCpu->cpum.s.Guest.idtr.cbIdt;
472 return pVCpu->cpum.s.Guest.idtr.pIdt;
473}
474
475
476VMMDECL(RTSEL) CPUMGetGuestTR(PCVMCPU pVCpu, PCPUMSELREGHID pHidden)
477{
478 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_TR);
479 if (pHidden)
480 *pHidden = pVCpu->cpum.s.Guest.tr;
481 return pVCpu->cpum.s.Guest.tr.Sel;
482}
483
484
485VMMDECL(RTSEL) CPUMGetGuestCS(PCVMCPU pVCpu)
486{
487 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_CS);
488 return pVCpu->cpum.s.Guest.cs.Sel;
489}
490
491
492VMMDECL(RTSEL) CPUMGetGuestDS(PCVMCPU pVCpu)
493{
494 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_DS);
495 return pVCpu->cpum.s.Guest.ds.Sel;
496}
497
498
499VMMDECL(RTSEL) CPUMGetGuestES(PCVMCPU pVCpu)
500{
501 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_ES);
502 return pVCpu->cpum.s.Guest.es.Sel;
503}
504
505
506VMMDECL(RTSEL) CPUMGetGuestFS(PCVMCPU pVCpu)
507{
508 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_FS);
509 return pVCpu->cpum.s.Guest.fs.Sel;
510}
511
512
513VMMDECL(RTSEL) CPUMGetGuestGS(PCVMCPU pVCpu)
514{
515 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_GS);
516 return pVCpu->cpum.s.Guest.gs.Sel;
517}
518
519
520VMMDECL(RTSEL) CPUMGetGuestSS(PCVMCPU pVCpu)
521{
522 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_SS);
523 return pVCpu->cpum.s.Guest.ss.Sel;
524}
525
526
527VMMDECL(uint64_t) CPUMGetGuestFlatPC(PVMCPU pVCpu)
528{
529 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_RIP | CPUMCTX_EXTRN_CS | CPUMCTX_EXTRN_EFER);
530 CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(pVCpu, &pVCpu->cpum.s.Guest.cs);
531 if ( !CPUMIsGuestInLongMode(pVCpu)
532 || !pVCpu->cpum.s.Guest.cs.Attr.n.u1Long)
533 return pVCpu->cpum.s.Guest.eip + (uint32_t)pVCpu->cpum.s.Guest.cs.u64Base;
534 return pVCpu->cpum.s.Guest.rip + pVCpu->cpum.s.Guest.cs.u64Base;
535}
536
537
538VMMDECL(uint64_t) CPUMGetGuestFlatSP(PVMCPU pVCpu)
539{
540 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_RSP | CPUMCTX_EXTRN_SS | CPUMCTX_EXTRN_CS | CPUMCTX_EXTRN_EFER);
541 CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(pVCpu, &pVCpu->cpum.s.Guest.ss);
542 if ( !CPUMIsGuestInLongMode(pVCpu)
543 || !pVCpu->cpum.s.Guest.cs.Attr.n.u1Long)
544 return pVCpu->cpum.s.Guest.eip + (uint32_t)pVCpu->cpum.s.Guest.ss.u64Base;
545 return pVCpu->cpum.s.Guest.rip + pVCpu->cpum.s.Guest.ss.u64Base;
546}
547
548
549VMMDECL(RTSEL) CPUMGetGuestLDTR(PCVMCPU pVCpu)
550{
551 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_LDTR);
552 return pVCpu->cpum.s.Guest.ldtr.Sel;
553}
554
555
556VMMDECL(RTSEL) CPUMGetGuestLdtrEx(PCVMCPU pVCpu, uint64_t *pGCPtrBase, uint32_t *pcbLimit)
557{
558 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_LDTR);
559 *pGCPtrBase = pVCpu->cpum.s.Guest.ldtr.u64Base;
560 *pcbLimit = pVCpu->cpum.s.Guest.ldtr.u32Limit;
561 return pVCpu->cpum.s.Guest.ldtr.Sel;
562}
563
564
565VMMDECL(uint64_t) CPUMGetGuestCR0(PCVMCPU pVCpu)
566{
567 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_CR0);
568 return pVCpu->cpum.s.Guest.cr0;
569}
570
571
572VMMDECL(uint64_t) CPUMGetGuestCR2(PCVMCPU pVCpu)
573{
574 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_CR2);
575 return pVCpu->cpum.s.Guest.cr2;
576}
577
578
579VMMDECL(uint64_t) CPUMGetGuestCR3(PCVMCPU pVCpu)
580{
581 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_CR3);
582 return pVCpu->cpum.s.Guest.cr3;
583}
584
585
586VMMDECL(uint64_t) CPUMGetGuestCR4(PCVMCPU pVCpu)
587{
588 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_CR4);
589 return pVCpu->cpum.s.Guest.cr4;
590}
591
592
593VMMDECL(uint64_t) CPUMGetGuestCR8(PCVMCPUCC pVCpu)
594{
595 uint64_t u64;
596 int rc = CPUMGetGuestCRx(pVCpu, DISCREG_CR8, &u64);
597 if (RT_FAILURE(rc))
598 u64 = 0;
599 return u64;
600}
601
602
603VMMDECL(void) CPUMGetGuestGDTR(PCVMCPU pVCpu, PVBOXGDTR pGDTR)
604{
605 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_GDTR);
606 *pGDTR = pVCpu->cpum.s.Guest.gdtr;
607}
608
609
610VMMDECL(uint32_t) CPUMGetGuestEIP(PCVMCPU pVCpu)
611{
612 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_RIP);
613 return pVCpu->cpum.s.Guest.eip;
614}
615
616
617VMMDECL(uint64_t) CPUMGetGuestRIP(PCVMCPU pVCpu)
618{
619 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_RIP);
620 return pVCpu->cpum.s.Guest.rip;
621}
622
623
624VMMDECL(uint32_t) CPUMGetGuestEAX(PCVMCPU pVCpu)
625{
626 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_RAX);
627 return pVCpu->cpum.s.Guest.eax;
628}
629
630
631VMMDECL(uint32_t) CPUMGetGuestEBX(PCVMCPU pVCpu)
632{
633 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_RBX);
634 return pVCpu->cpum.s.Guest.ebx;
635}
636
637
638VMMDECL(uint32_t) CPUMGetGuestECX(PCVMCPU pVCpu)
639{
640 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_RCX);
641 return pVCpu->cpum.s.Guest.ecx;
642}
643
644
645VMMDECL(uint32_t) CPUMGetGuestEDX(PCVMCPU pVCpu)
646{
647 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_RDX);
648 return pVCpu->cpum.s.Guest.edx;
649}
650
651
652VMMDECL(uint32_t) CPUMGetGuestESI(PCVMCPU pVCpu)
653{
654 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_RSI);
655 return pVCpu->cpum.s.Guest.esi;
656}
657
658
659VMMDECL(uint32_t) CPUMGetGuestEDI(PCVMCPU pVCpu)
660{
661 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_RDI);
662 return pVCpu->cpum.s.Guest.edi;
663}
664
665
666VMMDECL(uint32_t) CPUMGetGuestESP(PCVMCPU pVCpu)
667{
668 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_RSP);
669 return pVCpu->cpum.s.Guest.esp;
670}
671
672
673VMMDECL(uint32_t) CPUMGetGuestEBP(PCVMCPU pVCpu)
674{
675 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_RBP);
676 return pVCpu->cpum.s.Guest.ebp;
677}
678
679
680VMMDECL(uint32_t) CPUMGetGuestEFlags(PCVMCPU pVCpu)
681{
682 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_RFLAGS);
683 return pVCpu->cpum.s.Guest.eflags.u;
684}
685
686
687VMMDECL(int) CPUMGetGuestCRx(PCVMCPUCC pVCpu, unsigned iReg, uint64_t *pValue)
688{
689 switch (iReg)
690 {
691 case DISCREG_CR0:
692 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_CR0);
693 *pValue = pVCpu->cpum.s.Guest.cr0;
694 break;
695
696 case DISCREG_CR2:
697 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_CR2);
698 *pValue = pVCpu->cpum.s.Guest.cr2;
699 break;
700
701 case DISCREG_CR3:
702 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_CR3);
703 *pValue = pVCpu->cpum.s.Guest.cr3;
704 break;
705
706 case DISCREG_CR4:
707 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_CR4);
708 *pValue = pVCpu->cpum.s.Guest.cr4;
709 break;
710
711 case DISCREG_CR8:
712 {
713 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_APIC_TPR);
714 uint8_t u8Tpr;
715 int rc = APICGetTpr(pVCpu, &u8Tpr, NULL /* pfPending */, NULL /* pu8PendingIrq */);
716 if (RT_FAILURE(rc))
717 {
718 AssertMsg(rc == VERR_PDM_NO_APIC_INSTANCE, ("%Rrc\n", rc));
719 *pValue = 0;
720 return rc;
721 }
722 *pValue = u8Tpr >> 4; /* bits 7-4 contain the task priority that go in cr8, bits 3-0 */
723 break;
724 }
725
726 default:
727 return VERR_INVALID_PARAMETER;
728 }
729 return VINF_SUCCESS;
730}
731
732
733VMMDECL(uint64_t) CPUMGetGuestDR0(PCVMCPU pVCpu)
734{
735 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
736 return pVCpu->cpum.s.Guest.dr[0];
737}
738
739
740VMMDECL(uint64_t) CPUMGetGuestDR1(PCVMCPU pVCpu)
741{
742 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
743 return pVCpu->cpum.s.Guest.dr[1];
744}
745
746
747VMMDECL(uint64_t) CPUMGetGuestDR2(PCVMCPU pVCpu)
748{
749 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
750 return pVCpu->cpum.s.Guest.dr[2];
751}
752
753
754VMMDECL(uint64_t) CPUMGetGuestDR3(PCVMCPU pVCpu)
755{
756 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
757 return pVCpu->cpum.s.Guest.dr[3];
758}
759
760
761VMMDECL(uint64_t) CPUMGetGuestDR6(PCVMCPU pVCpu)
762{
763 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_DR6);
764 return pVCpu->cpum.s.Guest.dr[6];
765}
766
767
768VMMDECL(uint64_t) CPUMGetGuestDR7(PCVMCPU pVCpu)
769{
770 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_DR7);
771 return pVCpu->cpum.s.Guest.dr[7];
772}
773
774
775VMMDECL(int) CPUMGetGuestDRx(PCVMCPU pVCpu, uint32_t iReg, uint64_t *pValue)
776{
777 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_DR_MASK);
778 AssertReturn(iReg <= DISDREG_DR7, VERR_INVALID_PARAMETER);
779 /* DR4 is an alias for DR6, and DR5 is an alias for DR7. */
780 if (iReg == 4 || iReg == 5)
781 iReg += 2;
782 *pValue = pVCpu->cpum.s.Guest.dr[iReg];
783 return VINF_SUCCESS;
784}
785
786
787VMMDECL(uint64_t) CPUMGetGuestEFER(PCVMCPU pVCpu)
788{
789 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_EFER);
790 return pVCpu->cpum.s.Guest.msrEFER;
791}
792
793
794/**
795 * Looks up a CPUID leaf in the CPUID leaf array, no subleaf.
796 *
797 * @returns Pointer to the leaf if found, NULL if not.
798 *
799 * @param pVM The cross context VM structure.
800 * @param uLeaf The leaf to get.
801 */
802PCPUMCPUIDLEAF cpumCpuIdGetLeaf(PVM pVM, uint32_t uLeaf)
803{
804 unsigned iEnd = RT_MIN(pVM->cpum.s.GuestInfo.cCpuIdLeaves, RT_ELEMENTS(pVM->cpum.s.GuestInfo.aCpuIdLeaves));
805 if (iEnd)
806 {
807 unsigned iStart = 0;
808 PCPUMCPUIDLEAF paLeaves = pVM->cpum.s.GuestInfo.aCpuIdLeaves;
809 for (;;)
810 {
811 unsigned i = iStart + (iEnd - iStart) / 2U;
812 if (uLeaf < paLeaves[i].uLeaf)
813 {
814 if (i <= iStart)
815 return NULL;
816 iEnd = i;
817 }
818 else if (uLeaf > paLeaves[i].uLeaf)
819 {
820 i += 1;
821 if (i >= iEnd)
822 return NULL;
823 iStart = i;
824 }
825 else
826 {
827 if (RT_LIKELY(paLeaves[i].fSubLeafMask == 0 && paLeaves[i].uSubLeaf == 0))
828 return &paLeaves[i];
829
830 /* This shouldn't normally happen. But in case the it does due
831 to user configuration overrids or something, just return the
832 first sub-leaf. */
833 AssertMsgFailed(("uLeaf=%#x fSubLeafMask=%#x uSubLeaf=%#x\n",
834 uLeaf, paLeaves[i].fSubLeafMask, paLeaves[i].uSubLeaf));
835 while ( paLeaves[i].uSubLeaf != 0
836 && i > 0
837 && uLeaf == paLeaves[i - 1].uLeaf)
838 i--;
839 return &paLeaves[i];
840 }
841 }
842 }
843
844 return NULL;
845}
846
847
848/**
849 * Looks up a CPUID leaf in the CPUID leaf array.
850 *
851 * @returns Pointer to the leaf if found, NULL if not.
852 *
853 * @param pVM The cross context VM structure.
854 * @param uLeaf The leaf to get.
855 * @param uSubLeaf The subleaf, if applicable. Just pass 0 if it
856 * isn't.
857 * @param pfExactSubLeafHit Whether we've got an exact subleaf hit or not.
858 */
859PCPUMCPUIDLEAF cpumCpuIdGetLeafEx(PVM pVM, uint32_t uLeaf, uint32_t uSubLeaf, bool *pfExactSubLeafHit)
860{
861 unsigned iEnd = RT_MIN(pVM->cpum.s.GuestInfo.cCpuIdLeaves, RT_ELEMENTS(pVM->cpum.s.GuestInfo.aCpuIdLeaves));
862 if (iEnd)
863 {
864 unsigned iStart = 0;
865 PCPUMCPUIDLEAF paLeaves = pVM->cpum.s.GuestInfo.aCpuIdLeaves;
866 for (;;)
867 {
868 unsigned i = iStart + (iEnd - iStart) / 2U;
869 if (uLeaf < paLeaves[i].uLeaf)
870 {
871 if (i <= iStart)
872 return NULL;
873 iEnd = i;
874 }
875 else if (uLeaf > paLeaves[i].uLeaf)
876 {
877 i += 1;
878 if (i >= iEnd)
879 return NULL;
880 iStart = i;
881 }
882 else
883 {
884 uSubLeaf &= paLeaves[i].fSubLeafMask;
885 if (uSubLeaf == paLeaves[i].uSubLeaf)
886 *pfExactSubLeafHit = true;
887 else
888 {
889 /* Find the right subleaf. We return the last one before
890 uSubLeaf if we don't find an exact match. */
891 if (uSubLeaf < paLeaves[i].uSubLeaf)
892 while ( i > 0
893 && uLeaf == paLeaves[i - 1].uLeaf
894 && uSubLeaf <= paLeaves[i - 1].uSubLeaf)
895 i--;
896 else
897 while ( i + 1 < pVM->cpum.s.GuestInfo.cCpuIdLeaves
898 && uLeaf == paLeaves[i + 1].uLeaf
899 && uSubLeaf >= paLeaves[i + 1].uSubLeaf)
900 i++;
901 *pfExactSubLeafHit = uSubLeaf == paLeaves[i].uSubLeaf;
902 }
903 return &paLeaves[i];
904 }
905 }
906 }
907
908 *pfExactSubLeafHit = false;
909 return NULL;
910}
911
912
913/**
914 * Gets a CPUID leaf.
915 *
916 * @param pVCpu The cross context virtual CPU structure.
917 * @param uLeaf The CPUID leaf to get.
918 * @param uSubLeaf The CPUID sub-leaf to get, if applicable.
919 * @param f64BitMode A tristate indicate if the caller is in 64-bit mode or
920 * not: 1=true, 0=false, 1=whatever. This affect how the
921 * X86_CPUID_EXT_FEATURE_EDX_SYSCALL flag is returned on
922 * Intel CPUs, where it's only returned in 64-bit mode.
923 * @param pEax Where to store the EAX value.
924 * @param pEbx Where to store the EBX value.
925 * @param pEcx Where to store the ECX value.
926 * @param pEdx Where to store the EDX value.
927 */
928VMMDECL(void) CPUMGetGuestCpuId(PVMCPUCC pVCpu, uint32_t uLeaf, uint32_t uSubLeaf, int f64BitMode,
929 uint32_t *pEax, uint32_t *pEbx, uint32_t *pEcx, uint32_t *pEdx)
930{
931 bool fExactSubLeafHit;
932 PVM pVM = pVCpu->CTX_SUFF(pVM);
933 PCCPUMCPUIDLEAF pLeaf = cpumCpuIdGetLeafEx(pVM, uLeaf, uSubLeaf, &fExactSubLeafHit);
934 if (pLeaf)
935 {
936 AssertMsg(pLeaf->uLeaf == uLeaf, ("%#x %#x\n", pLeaf->uLeaf, uLeaf));
937 if (fExactSubLeafHit)
938 {
939 *pEax = pLeaf->uEax;
940 *pEbx = pLeaf->uEbx;
941 *pEcx = pLeaf->uEcx;
942 *pEdx = pLeaf->uEdx;
943
944 /*
945 * Deal with CPU specific information.
946 */
947 if (pLeaf->fFlags & ( CPUMCPUIDLEAF_F_CONTAINS_APIC_ID
948 | CPUMCPUIDLEAF_F_CONTAINS_OSXSAVE
949 | CPUMCPUIDLEAF_F_CONTAINS_APIC ))
950 {
951 if (uLeaf == 1)
952 {
953 /* EBX: Bits 31-24: Initial APIC ID. */
954 Assert(pVCpu->idCpu <= 255);
955 AssertMsg((pLeaf->uEbx >> 24) == 0, ("%#x\n", pLeaf->uEbx)); /* raw-mode assumption */
956 *pEbx = (pLeaf->uEbx & UINT32_C(0x00ffffff)) | (pVCpu->idCpu << 24);
957
958 /* EDX: Bit 9: AND with APICBASE.EN. */
959 if (!pVCpu->cpum.s.fCpuIdApicFeatureVisible && (pLeaf->fFlags & CPUMCPUIDLEAF_F_CONTAINS_APIC))
960 *pEdx &= ~X86_CPUID_FEATURE_EDX_APIC;
961
962 /* ECX: Bit 27: CR4.OSXSAVE mirror. */
963 *pEcx = (pLeaf->uEcx & ~X86_CPUID_FEATURE_ECX_OSXSAVE)
964 | (pVCpu->cpum.s.Guest.cr4 & X86_CR4_OSXSAVE ? X86_CPUID_FEATURE_ECX_OSXSAVE : 0);
965 }
966 else if (uLeaf == 0xb)
967 {
968 /* EDX: Initial extended APIC ID. */
969 AssertMsg(pLeaf->uEdx == 0, ("%#x\n", pLeaf->uEdx)); /* raw-mode assumption */
970 *pEdx = pVCpu->idCpu;
971 Assert(!(pLeaf->fFlags & ~(CPUMCPUIDLEAF_F_CONTAINS_APIC_ID | CPUMCPUIDLEAF_F_INTEL_TOPOLOGY_SUBLEAVES)));
972 }
973 else if (uLeaf == UINT32_C(0x8000001e))
974 {
975 /* EAX: Initial extended APIC ID. */
976 AssertMsg(pLeaf->uEax == 0, ("%#x\n", pLeaf->uEax)); /* raw-mode assumption */
977 *pEax = pVCpu->idCpu;
978 Assert(!(pLeaf->fFlags & ~CPUMCPUIDLEAF_F_CONTAINS_APIC_ID));
979 }
980 else if (uLeaf == UINT32_C(0x80000001))
981 {
982 /* EDX: Bit 9: AND with APICBASE.EN. */
983 if (!pVCpu->cpum.s.fCpuIdApicFeatureVisible)
984 *pEdx &= ~X86_CPUID_AMD_FEATURE_EDX_APIC;
985 Assert(!(pLeaf->fFlags & ~CPUMCPUIDLEAF_F_CONTAINS_APIC));
986 }
987 else
988 AssertMsgFailed(("uLeaf=%#x\n", uLeaf));
989 }
990
991 /* Intel CPUs supresses the SYSCALL bit when not executing in 64-bit mode: */
992 if ( uLeaf == UINT32_C(0x80000001)
993 && f64BitMode == false
994 && (*pEdx & X86_CPUID_EXT_FEATURE_EDX_SYSCALL)
995 && ( pVM->cpum.s.GuestFeatures.enmCpuVendor == CPUMCPUVENDOR_INTEL
996 || pVM->cpum.s.GuestFeatures.enmCpuVendor == CPUMCPUVENDOR_VIA /*?*/
997 || pVM->cpum.s.GuestFeatures.enmCpuVendor == CPUMCPUVENDOR_SHANGHAI /*?*/ ) )
998 *pEdx &= ~X86_CPUID_EXT_FEATURE_EDX_SYSCALL;
999
1000 }
1001 /*
1002 * Out of range sub-leaves aren't quite as easy and pretty as we emulate
1003 * them here, but we do the best we can here...
1004 */
1005 else
1006 {
1007 *pEax = *pEbx = *pEcx = *pEdx = 0;
1008 if (pLeaf->fFlags & CPUMCPUIDLEAF_F_INTEL_TOPOLOGY_SUBLEAVES)
1009 {
1010 *pEcx = uSubLeaf & 0xff;
1011 *pEdx = pVCpu->idCpu;
1012 }
1013 }
1014 }
1015 else
1016 {
1017 /*
1018 * Different CPUs have different ways of dealing with unknown CPUID leaves.
1019 */
1020 switch (pVM->cpum.s.GuestInfo.enmUnknownCpuIdMethod)
1021 {
1022 default:
1023 AssertFailed();
1024 RT_FALL_THRU();
1025 case CPUMUNKNOWNCPUID_DEFAULTS:
1026 case CPUMUNKNOWNCPUID_LAST_STD_LEAF: /* ASSUME this is executed */
1027 case CPUMUNKNOWNCPUID_LAST_STD_LEAF_WITH_ECX: /** @todo Implement CPUMUNKNOWNCPUID_LAST_STD_LEAF_WITH_ECX */
1028 *pEax = pVM->cpum.s.GuestInfo.DefCpuId.uEax;
1029 *pEbx = pVM->cpum.s.GuestInfo.DefCpuId.uEbx;
1030 *pEcx = pVM->cpum.s.GuestInfo.DefCpuId.uEcx;
1031 *pEdx = pVM->cpum.s.GuestInfo.DefCpuId.uEdx;
1032 break;
1033 case CPUMUNKNOWNCPUID_PASSTHRU:
1034 *pEax = uLeaf;
1035 *pEbx = 0;
1036 *pEcx = uSubLeaf;
1037 *pEdx = 0;
1038 break;
1039 }
1040 }
1041 Log2(("CPUMGetGuestCpuId: uLeaf=%#010x/%#010x %RX32 %RX32 %RX32 %RX32\n", uLeaf, uSubLeaf, *pEax, *pEbx, *pEcx, *pEdx));
1042}
1043
1044
1045/**
1046 * Sets the visibility of the X86_CPUID_FEATURE_EDX_APIC and
1047 * X86_CPUID_AMD_FEATURE_EDX_APIC CPUID bits.
1048 *
1049 * @returns Previous value.
1050 * @param pVCpu The cross context virtual CPU structure to make the
1051 * change on. Usually the calling EMT.
1052 * @param fVisible Whether to make it visible (true) or hide it (false).
1053 *
1054 * @remarks This is "VMMDECL" so that it still links with
1055 * the old APIC code which is in VBoxDD2 and not in
1056 * the VMM module.
1057 */
1058VMMDECL(bool) CPUMSetGuestCpuIdPerCpuApicFeature(PVMCPU pVCpu, bool fVisible)
1059{
1060 bool fOld = pVCpu->cpum.s.fCpuIdApicFeatureVisible;
1061 pVCpu->cpum.s.fCpuIdApicFeatureVisible = fVisible;
1062 return fOld;
1063}
1064
1065
1066/**
1067 * Gets the host CPU vendor.
1068 *
1069 * @returns CPU vendor.
1070 * @param pVM The cross context VM structure.
1071 */
1072VMMDECL(CPUMCPUVENDOR) CPUMGetHostCpuVendor(PVM pVM)
1073{
1074 return (CPUMCPUVENDOR)pVM->cpum.s.HostFeatures.enmCpuVendor;
1075}
1076
1077
1078/**
1079 * Gets the host CPU microarchitecture.
1080 *
1081 * @returns CPU microarchitecture.
1082 * @param pVM The cross context VM structure.
1083 */
1084VMMDECL(CPUMMICROARCH) CPUMGetHostMicroarch(PCVM pVM)
1085{
1086 return pVM->cpum.s.HostFeatures.enmMicroarch;
1087}
1088
1089
1090/**
1091 * Gets the guest CPU vendor.
1092 *
1093 * @returns CPU vendor.
1094 * @param pVM The cross context VM structure.
1095 */
1096VMMDECL(CPUMCPUVENDOR) CPUMGetGuestCpuVendor(PVM pVM)
1097{
1098 return (CPUMCPUVENDOR)pVM->cpum.s.GuestFeatures.enmCpuVendor;
1099}
1100
1101
1102/**
1103 * Gets the guest CPU microarchitecture.
1104 *
1105 * @returns CPU microarchitecture.
1106 * @param pVM The cross context VM structure.
1107 */
1108VMMDECL(CPUMMICROARCH) CPUMGetGuestMicroarch(PCVM pVM)
1109{
1110 return pVM->cpum.s.GuestFeatures.enmMicroarch;
1111}
1112
1113
1114/**
1115 * Gets the maximum number of physical and linear address bits supported by the
1116 * guest.
1117 *
1118 * @param pVM The cross context VM structure.
1119 * @param pcPhysAddrWidth Where to store the physical address width.
1120 * @param pcLinearAddrWidth Where to store the linear address width.
1121 */
1122VMMDECL(void) CPUMGetGuestAddrWidths(PCVM pVM, uint8_t *pcPhysAddrWidth, uint8_t *pcLinearAddrWidth)
1123{
1124 AssertPtr(pVM);
1125 AssertReturnVoid(pcPhysAddrWidth);
1126 AssertReturnVoid(pcLinearAddrWidth);
1127 *pcPhysAddrWidth = pVM->cpum.s.GuestFeatures.cMaxPhysAddrWidth;
1128 *pcLinearAddrWidth = pVM->cpum.s.GuestFeatures.cMaxLinearAddrWidth;
1129}
1130
1131
1132VMMDECL(int) CPUMSetGuestDR0(PVMCPUCC pVCpu, uint64_t uDr0)
1133{
1134 pVCpu->cpum.s.Guest.dr[0] = uDr0;
1135 return CPUMRecalcHyperDRx(pVCpu, 0);
1136}
1137
1138
1139VMMDECL(int) CPUMSetGuestDR1(PVMCPUCC pVCpu, uint64_t uDr1)
1140{
1141 pVCpu->cpum.s.Guest.dr[1] = uDr1;
1142 return CPUMRecalcHyperDRx(pVCpu, 1);
1143}
1144
1145
1146VMMDECL(int) CPUMSetGuestDR2(PVMCPUCC pVCpu, uint64_t uDr2)
1147{
1148 pVCpu->cpum.s.Guest.dr[2] = uDr2;
1149 return CPUMRecalcHyperDRx(pVCpu, 2);
1150}
1151
1152
1153VMMDECL(int) CPUMSetGuestDR3(PVMCPUCC pVCpu, uint64_t uDr3)
1154{
1155 pVCpu->cpum.s.Guest.dr[3] = uDr3;
1156 return CPUMRecalcHyperDRx(pVCpu, 3);
1157}
1158
1159
1160VMMDECL(int) CPUMSetGuestDR6(PVMCPU pVCpu, uint64_t uDr6)
1161{
1162 pVCpu->cpum.s.Guest.dr[6] = uDr6;
1163 pVCpu->cpum.s.Guest.fExtrn &= ~CPUMCTX_EXTRN_DR6;
1164 return VINF_SUCCESS; /* No need to recalc. */
1165}
1166
1167
1168VMMDECL(int) CPUMSetGuestDR7(PVMCPUCC pVCpu, uint64_t uDr7)
1169{
1170 pVCpu->cpum.s.Guest.dr[7] = uDr7;
1171 pVCpu->cpum.s.Guest.fExtrn &= ~CPUMCTX_EXTRN_DR7;
1172 return CPUMRecalcHyperDRx(pVCpu, 7);
1173}
1174
1175
1176VMMDECL(int) CPUMSetGuestDRx(PVMCPUCC pVCpu, uint32_t iReg, uint64_t Value)
1177{
1178 AssertReturn(iReg <= DISDREG_DR7, VERR_INVALID_PARAMETER);
1179 /* DR4 is an alias for DR6, and DR5 is an alias for DR7. */
1180 if (iReg == 4 || iReg == 5)
1181 iReg += 2;
1182 pVCpu->cpum.s.Guest.dr[iReg] = Value;
1183 return CPUMRecalcHyperDRx(pVCpu, iReg);
1184}
1185
1186
1187/**
1188 * Recalculates the hypervisor DRx register values based on current guest
1189 * registers and DBGF breakpoints, updating changed registers depending on the
1190 * context.
1191 *
1192 * This is called whenever a guest DRx register is modified (any context) and
1193 * when DBGF sets a hardware breakpoint (ring-3 only, rendezvous).
1194 *
1195 * In raw-mode context this function will reload any (hyper) DRx registers which
1196 * comes out with a different value. It may also have to save the host debug
1197 * registers if that haven't been done already. In this context though, we'll
1198 * be intercepting and emulating all DRx accesses, so the hypervisor DRx values
1199 * are only important when breakpoints are actually enabled.
1200 *
1201 * In ring-0 (HM) context DR0-3 will be relocated by us, while DR7 will be
1202 * reloaded by the HM code if it changes. Further more, we will only use the
1203 * combined register set when the VBox debugger is actually using hardware BPs,
1204 * when it isn't we'll keep the guest DR0-3 + (maybe) DR6 loaded (DR6 doesn't
1205 * concern us here).
1206 *
1207 * In ring-3 we won't be loading anything, so well calculate hypervisor values
1208 * all the time.
1209 *
1210 * @returns VINF_SUCCESS.
1211 * @param pVCpu The cross context virtual CPU structure.
1212 * @param iGstReg The guest debug register number that was modified.
1213 * UINT8_MAX if not guest register.
1214 */
1215VMMDECL(int) CPUMRecalcHyperDRx(PVMCPUCC pVCpu, uint8_t iGstReg)
1216{
1217 PVM pVM = pVCpu->CTX_SUFF(pVM);
1218#ifndef IN_RING0
1219 RT_NOREF_PV(iGstReg);
1220#endif
1221
1222 /*
1223 * Compare the DR7s first.
1224 *
1225 * We only care about the enabled flags. GD is virtualized when we
1226 * dispatch the #DB, we never enable it. The DBGF DR7 value is will
1227 * always have the LE and GE bits set, so no need to check and disable
1228 * stuff if they're cleared like we have to for the guest DR7.
1229 */
1230 RTGCUINTREG uGstDr7 = CPUMGetGuestDR7(pVCpu);
1231 /** @todo This isn't correct. BPs work without setting LE and GE under AMD-V. They are also documented as unsupported by P6+. */
1232 if (!(uGstDr7 & (X86_DR7_LE | X86_DR7_GE)))
1233 uGstDr7 = 0;
1234 else if (!(uGstDr7 & X86_DR7_LE))
1235 uGstDr7 &= ~X86_DR7_LE_ALL;
1236 else if (!(uGstDr7 & X86_DR7_GE))
1237 uGstDr7 &= ~X86_DR7_GE_ALL;
1238
1239 const RTGCUINTREG uDbgfDr7 = DBGFBpGetDR7(pVM);
1240 if ((uGstDr7 | uDbgfDr7) & X86_DR7_ENABLED_MASK)
1241 {
1242 Assert(!CPUMIsGuestDebugStateActive(pVCpu));
1243
1244 /*
1245 * Ok, something is enabled. Recalc each of the breakpoints, taking
1246 * the VM debugger ones of the guest ones. In raw-mode context we will
1247 * not allow breakpoints with values inside the hypervisor area.
1248 */
1249 RTGCUINTREG uNewDr7 = X86_DR7_GE | X86_DR7_LE | X86_DR7_RA1_MASK;
1250
1251 /* bp 0 */
1252 RTGCUINTREG uNewDr0;
1253 if (uDbgfDr7 & (X86_DR7_L0 | X86_DR7_G0))
1254 {
1255 uNewDr7 |= uDbgfDr7 & (X86_DR7_L0 | X86_DR7_G0 | X86_DR7_RW0_MASK | X86_DR7_LEN0_MASK);
1256 uNewDr0 = DBGFBpGetDR0(pVM);
1257 }
1258 else if (uGstDr7 & (X86_DR7_L0 | X86_DR7_G0))
1259 {
1260 uNewDr0 = CPUMGetGuestDR0(pVCpu);
1261 uNewDr7 |= uGstDr7 & (X86_DR7_L0 | X86_DR7_G0 | X86_DR7_RW0_MASK | X86_DR7_LEN0_MASK);
1262 }
1263 else
1264 uNewDr0 = 0;
1265
1266 /* bp 1 */
1267 RTGCUINTREG uNewDr1;
1268 if (uDbgfDr7 & (X86_DR7_L1 | X86_DR7_G1))
1269 {
1270 uNewDr7 |= uDbgfDr7 & (X86_DR7_L1 | X86_DR7_G1 | X86_DR7_RW1_MASK | X86_DR7_LEN1_MASK);
1271 uNewDr1 = DBGFBpGetDR1(pVM);
1272 }
1273 else if (uGstDr7 & (X86_DR7_L1 | X86_DR7_G1))
1274 {
1275 uNewDr1 = CPUMGetGuestDR1(pVCpu);
1276 uNewDr7 |= uGstDr7 & (X86_DR7_L1 | X86_DR7_G1 | X86_DR7_RW1_MASK | X86_DR7_LEN1_MASK);
1277 }
1278 else
1279 uNewDr1 = 0;
1280
1281 /* bp 2 */
1282 RTGCUINTREG uNewDr2;
1283 if (uDbgfDr7 & (X86_DR7_L2 | X86_DR7_G2))
1284 {
1285 uNewDr7 |= uDbgfDr7 & (X86_DR7_L2 | X86_DR7_G2 | X86_DR7_RW2_MASK | X86_DR7_LEN2_MASK);
1286 uNewDr2 = DBGFBpGetDR2(pVM);
1287 }
1288 else if (uGstDr7 & (X86_DR7_L2 | X86_DR7_G2))
1289 {
1290 uNewDr2 = CPUMGetGuestDR2(pVCpu);
1291 uNewDr7 |= uGstDr7 & (X86_DR7_L2 | X86_DR7_G2 | X86_DR7_RW2_MASK | X86_DR7_LEN2_MASK);
1292 }
1293 else
1294 uNewDr2 = 0;
1295
1296 /* bp 3 */
1297 RTGCUINTREG uNewDr3;
1298 if (uDbgfDr7 & (X86_DR7_L3 | X86_DR7_G3))
1299 {
1300 uNewDr7 |= uDbgfDr7 & (X86_DR7_L3 | X86_DR7_G3 | X86_DR7_RW3_MASK | X86_DR7_LEN3_MASK);
1301 uNewDr3 = DBGFBpGetDR3(pVM);
1302 }
1303 else if (uGstDr7 & (X86_DR7_L3 | X86_DR7_G3))
1304 {
1305 uNewDr3 = CPUMGetGuestDR3(pVCpu);
1306 uNewDr7 |= uGstDr7 & (X86_DR7_L3 | X86_DR7_G3 | X86_DR7_RW3_MASK | X86_DR7_LEN3_MASK);
1307 }
1308 else
1309 uNewDr3 = 0;
1310
1311 /*
1312 * Apply the updates.
1313 */
1314 pVCpu->cpum.s.fUseFlags |= CPUM_USE_DEBUG_REGS_HYPER;
1315 if (uNewDr3 != pVCpu->cpum.s.Hyper.dr[3])
1316 CPUMSetHyperDR3(pVCpu, uNewDr3);
1317 if (uNewDr2 != pVCpu->cpum.s.Hyper.dr[2])
1318 CPUMSetHyperDR2(pVCpu, uNewDr2);
1319 if (uNewDr1 != pVCpu->cpum.s.Hyper.dr[1])
1320 CPUMSetHyperDR1(pVCpu, uNewDr1);
1321 if (uNewDr0 != pVCpu->cpum.s.Hyper.dr[0])
1322 CPUMSetHyperDR0(pVCpu, uNewDr0);
1323 if (uNewDr7 != pVCpu->cpum.s.Hyper.dr[7])
1324 CPUMSetHyperDR7(pVCpu, uNewDr7);
1325 }
1326#ifdef IN_RING0
1327 else if (CPUMIsGuestDebugStateActive(pVCpu))
1328 {
1329 /*
1330 * Reload the register that was modified. Normally this won't happen
1331 * as we won't intercept DRx writes when not having the hyper debug
1332 * state loaded, but in case we do for some reason we'll simply deal
1333 * with it.
1334 */
1335 switch (iGstReg)
1336 {
1337 case 0: ASMSetDR0(CPUMGetGuestDR0(pVCpu)); break;
1338 case 1: ASMSetDR1(CPUMGetGuestDR1(pVCpu)); break;
1339 case 2: ASMSetDR2(CPUMGetGuestDR2(pVCpu)); break;
1340 case 3: ASMSetDR3(CPUMGetGuestDR3(pVCpu)); break;
1341 default:
1342 AssertReturn(iGstReg != UINT8_MAX, VERR_INTERNAL_ERROR_3);
1343 }
1344 }
1345#endif
1346 else
1347 {
1348 /*
1349 * No active debug state any more. In raw-mode this means we have to
1350 * make sure DR7 has everything disabled now, if we armed it already.
1351 * In ring-0 we might end up here when just single stepping.
1352 */
1353#ifdef IN_RING0
1354 if (pVCpu->cpum.s.fUseFlags & CPUM_USED_DEBUG_REGS_HYPER)
1355 {
1356 if (pVCpu->cpum.s.Hyper.dr[0])
1357 ASMSetDR0(0);
1358 if (pVCpu->cpum.s.Hyper.dr[1])
1359 ASMSetDR1(0);
1360 if (pVCpu->cpum.s.Hyper.dr[2])
1361 ASMSetDR2(0);
1362 if (pVCpu->cpum.s.Hyper.dr[3])
1363 ASMSetDR3(0);
1364 pVCpu->cpum.s.fUseFlags &= ~CPUM_USED_DEBUG_REGS_HYPER;
1365 }
1366#endif
1367 pVCpu->cpum.s.fUseFlags &= ~CPUM_USE_DEBUG_REGS_HYPER;
1368
1369 /* Clear all the registers. */
1370 pVCpu->cpum.s.Hyper.dr[7] = X86_DR7_RA1_MASK;
1371 pVCpu->cpum.s.Hyper.dr[3] = 0;
1372 pVCpu->cpum.s.Hyper.dr[2] = 0;
1373 pVCpu->cpum.s.Hyper.dr[1] = 0;
1374 pVCpu->cpum.s.Hyper.dr[0] = 0;
1375
1376 }
1377 Log2(("CPUMRecalcHyperDRx: fUseFlags=%#x %RGr %RGr %RGr %RGr %RGr %RGr\n",
1378 pVCpu->cpum.s.fUseFlags, pVCpu->cpum.s.Hyper.dr[0], pVCpu->cpum.s.Hyper.dr[1],
1379 pVCpu->cpum.s.Hyper.dr[2], pVCpu->cpum.s.Hyper.dr[3], pVCpu->cpum.s.Hyper.dr[6],
1380 pVCpu->cpum.s.Hyper.dr[7]));
1381
1382 return VINF_SUCCESS;
1383}
1384
1385
1386/**
1387 * Set the guest XCR0 register.
1388 *
1389 * Will load additional state if the FPU state is already loaded (in ring-0 &
1390 * raw-mode context).
1391 *
1392 * @returns VINF_SUCCESS on success, VERR_CPUM_RAISE_GP_0 on invalid input
1393 * value.
1394 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1395 * @param uNewValue The new value.
1396 * @thread EMT(pVCpu)
1397 */
1398VMM_INT_DECL(int) CPUMSetGuestXcr0(PVMCPUCC pVCpu, uint64_t uNewValue)
1399{
1400 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_XCRx);
1401 if ( (uNewValue & ~pVCpu->CTX_SUFF(pVM)->cpum.s.fXStateGuestMask) == 0
1402 /* The X87 bit cannot be cleared. */
1403 && (uNewValue & XSAVE_C_X87)
1404 /* AVX requires SSE. */
1405 && (uNewValue & (XSAVE_C_SSE | XSAVE_C_YMM)) != XSAVE_C_YMM
1406 /* AVX-512 requires YMM, SSE and all of its three components to be enabled. */
1407 && ( (uNewValue & (XSAVE_C_OPMASK | XSAVE_C_ZMM_HI256 | XSAVE_C_ZMM_16HI)) == 0
1408 || (uNewValue & (XSAVE_C_SSE | XSAVE_C_YMM | XSAVE_C_OPMASK | XSAVE_C_ZMM_HI256 | XSAVE_C_ZMM_16HI))
1409 == (XSAVE_C_SSE | XSAVE_C_YMM | XSAVE_C_OPMASK | XSAVE_C_ZMM_HI256 | XSAVE_C_ZMM_16HI) )
1410 )
1411 {
1412 pVCpu->cpum.s.Guest.aXcr[0] = uNewValue;
1413
1414 /* If more state components are enabled, we need to take care to load
1415 them if the FPU/SSE state is already loaded. May otherwise leak
1416 host state to the guest. */
1417 uint64_t fNewComponents = ~pVCpu->cpum.s.Guest.fXStateMask & uNewValue;
1418 if (fNewComponents)
1419 {
1420#ifdef IN_RING0
1421 if (pVCpu->cpum.s.fUseFlags & CPUM_USED_FPU_GUEST)
1422 {
1423 if (pVCpu->cpum.s.Guest.fXStateMask != 0)
1424 /* Adding more components. */
1425 ASMXRstor(&pVCpu->cpum.s.Guest.XState, fNewComponents);
1426 else
1427 {
1428 /* We're switching from FXSAVE/FXRSTOR to XSAVE/XRSTOR. */
1429 pVCpu->cpum.s.Guest.fXStateMask |= XSAVE_C_X87 | XSAVE_C_SSE;
1430 if (uNewValue & ~(XSAVE_C_X87 | XSAVE_C_SSE))
1431 ASMXRstor(&pVCpu->cpum.s.Guest.XState, uNewValue & ~(XSAVE_C_X87 | XSAVE_C_SSE));
1432 }
1433 }
1434#endif
1435 pVCpu->cpum.s.Guest.fXStateMask |= uNewValue;
1436 }
1437 return VINF_SUCCESS;
1438 }
1439 return VERR_CPUM_RAISE_GP_0;
1440}
1441
1442
1443/**
1444 * Tests if the guest has No-Execute Page Protection Enabled (NXE).
1445 *
1446 * @returns true if in real mode, otherwise false.
1447 * @param pVCpu The cross context virtual CPU structure.
1448 */
1449VMMDECL(bool) CPUMIsGuestNXEnabled(PCVMCPU pVCpu)
1450{
1451 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_EFER);
1452 return !!(pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_NXE);
1453}
1454
1455
1456/**
1457 * Tests if the guest has the Page Size Extension enabled (PSE).
1458 *
1459 * @returns true if in real mode, otherwise false.
1460 * @param pVCpu The cross context virtual CPU structure.
1461 */
1462VMMDECL(bool) CPUMIsGuestPageSizeExtEnabled(PCVMCPU pVCpu)
1463{
1464 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_CR4);
1465 /* PAE or AMD64 implies support for big pages regardless of CR4.PSE */
1466 return !!(pVCpu->cpum.s.Guest.cr4 & (X86_CR4_PSE | X86_CR4_PAE));
1467}
1468
1469
1470/**
1471 * Tests if the guest has the paging enabled (PG).
1472 *
1473 * @returns true if in real mode, otherwise false.
1474 * @param pVCpu The cross context virtual CPU structure.
1475 */
1476VMMDECL(bool) CPUMIsGuestPagingEnabled(PCVMCPU pVCpu)
1477{
1478 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_CR0);
1479 return !!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PG);
1480}
1481
1482
1483/**
1484 * Tests if the guest has the paging enabled (PG).
1485 *
1486 * @returns true if in real mode, otherwise false.
1487 * @param pVCpu The cross context virtual CPU structure.
1488 */
1489VMMDECL(bool) CPUMIsGuestR0WriteProtEnabled(PCVMCPU pVCpu)
1490{
1491 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_CR0);
1492 return !!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_WP);
1493}
1494
1495
1496/**
1497 * Tests if the guest is running in real mode or not.
1498 *
1499 * @returns true if in real mode, otherwise false.
1500 * @param pVCpu The cross context virtual CPU structure.
1501 */
1502VMMDECL(bool) CPUMIsGuestInRealMode(PCVMCPU pVCpu)
1503{
1504 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_CR0);
1505 return !(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE);
1506}
1507
1508
1509/**
1510 * Tests if the guest is running in real or virtual 8086 mode.
1511 *
1512 * @returns @c true if it is, @c false if not.
1513 * @param pVCpu The cross context virtual CPU structure.
1514 */
1515VMMDECL(bool) CPUMIsGuestInRealOrV86Mode(PCVMCPU pVCpu)
1516{
1517 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_RFLAGS);
1518 return !(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE)
1519 || pVCpu->cpum.s.Guest.eflags.Bits.u1VM; /** @todo verify that this cannot be set in long mode. */
1520}
1521
1522
1523/**
1524 * Tests if the guest is running in protected or not.
1525 *
1526 * @returns true if in protected mode, otherwise false.
1527 * @param pVCpu The cross context virtual CPU structure.
1528 */
1529VMMDECL(bool) CPUMIsGuestInProtectedMode(PCVMCPU pVCpu)
1530{
1531 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_CR0);
1532 return !!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE);
1533}
1534
1535
1536/**
1537 * Tests if the guest is running in paged protected or not.
1538 *
1539 * @returns true if in paged protected mode, otherwise false.
1540 * @param pVCpu The cross context virtual CPU structure.
1541 */
1542VMMDECL(bool) CPUMIsGuestInPagedProtectedMode(PCVMCPU pVCpu)
1543{
1544 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_CR0);
1545 return (pVCpu->cpum.s.Guest.cr0 & (X86_CR0_PE | X86_CR0_PG)) == (X86_CR0_PE | X86_CR0_PG);
1546}
1547
1548
1549/**
1550 * Tests if the guest is running in long mode or not.
1551 *
1552 * @returns true if in long mode, otherwise false.
1553 * @param pVCpu The cross context virtual CPU structure.
1554 */
1555VMMDECL(bool) CPUMIsGuestInLongMode(PCVMCPU pVCpu)
1556{
1557 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_EFER);
1558 return (pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA) == MSR_K6_EFER_LMA;
1559}
1560
1561
1562/**
1563 * Tests if the guest is running in PAE mode or not.
1564 *
1565 * @returns true if in PAE mode, otherwise false.
1566 * @param pVCpu The cross context virtual CPU structure.
1567 */
1568VMMDECL(bool) CPUMIsGuestInPAEMode(PCVMCPU pVCpu)
1569{
1570 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_CR4 | CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_EFER);
1571 /* Intel mentions EFER.LMA and EFER.LME in different parts of their spec. We shall use EFER.LMA rather
1572 than EFER.LME as it reflects if the CPU has entered paging with EFER.LME set. */
1573 return (pVCpu->cpum.s.Guest.cr4 & X86_CR4_PAE)
1574 && (pVCpu->cpum.s.Guest.cr0 & X86_CR0_PG)
1575 && !(pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA);
1576}
1577
1578
1579/**
1580 * Tests if the guest is running in 64 bits mode or not.
1581 *
1582 * @returns true if in 64 bits protected mode, otherwise false.
1583 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1584 */
1585VMMDECL(bool) CPUMIsGuestIn64BitCode(PVMCPU pVCpu)
1586{
1587 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_CS | CPUMCTX_EXTRN_EFER);
1588 if (!CPUMIsGuestInLongMode(pVCpu))
1589 return false;
1590 CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(pVCpu, &pVCpu->cpum.s.Guest.cs);
1591 return pVCpu->cpum.s.Guest.cs.Attr.n.u1Long;
1592}
1593
1594
1595/**
1596 * Helper for CPUMIsGuestIn64BitCodeEx that handles lazy resolving of hidden CS
1597 * registers.
1598 *
1599 * @returns true if in 64 bits protected mode, otherwise false.
1600 * @param pCtx Pointer to the current guest CPU context.
1601 */
1602VMM_INT_DECL(bool) CPUMIsGuestIn64BitCodeSlow(PCPUMCTX pCtx)
1603{
1604 return CPUMIsGuestIn64BitCode(CPUM_GUEST_CTX_TO_VMCPU(pCtx));
1605}
1606
1607
1608/**
1609 * Sets the specified changed flags (CPUM_CHANGED_*).
1610 *
1611 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1612 * @param fChangedAdd The changed flags to add.
1613 */
1614VMMDECL(void) CPUMSetChangedFlags(PVMCPU pVCpu, uint32_t fChangedAdd)
1615{
1616 pVCpu->cpum.s.fChanged |= fChangedAdd;
1617}
1618
1619
1620/**
1621 * Checks if the CPU supports the XSAVE and XRSTOR instruction.
1622 *
1623 * @returns true if supported.
1624 * @returns false if not supported.
1625 * @param pVM The cross context VM structure.
1626 */
1627VMMDECL(bool) CPUMSupportsXSave(PVM pVM)
1628{
1629 return pVM->cpum.s.HostFeatures.fXSaveRstor != 0;
1630}
1631
1632
1633/**
1634 * Checks if the host OS uses the SYSENTER / SYSEXIT instructions.
1635 * @returns true if used.
1636 * @returns false if not used.
1637 * @param pVM The cross context VM structure.
1638 */
1639VMMDECL(bool) CPUMIsHostUsingSysEnter(PVM pVM)
1640{
1641 return RT_BOOL(pVM->cpum.s.fHostUseFlags & CPUM_USE_SYSENTER);
1642}
1643
1644
1645/**
1646 * Checks if the host OS uses the SYSCALL / SYSRET instructions.
1647 * @returns true if used.
1648 * @returns false if not used.
1649 * @param pVM The cross context VM structure.
1650 */
1651VMMDECL(bool) CPUMIsHostUsingSysCall(PVM pVM)
1652{
1653 return RT_BOOL(pVM->cpum.s.fHostUseFlags & CPUM_USE_SYSCALL);
1654}
1655
1656
1657/**
1658 * Checks if we activated the FPU/XMM state of the guest OS.
1659 *
1660 * Obsolete: This differs from CPUMIsGuestFPUStateLoaded() in that it refers to
1661 * the next time we'll be executing guest code, so it may return true for
1662 * 64-on-32 when we still haven't actually loaded the FPU status, just scheduled
1663 * it to be loaded the next time we go thru the world switcher
1664 * (CPUM_SYNC_FPU_STATE).
1665 *
1666 * @returns true / false.
1667 * @param pVCpu The cross context virtual CPU structure.
1668 */
1669VMMDECL(bool) CPUMIsGuestFPUStateActive(PVMCPU pVCpu)
1670{
1671 bool fRet = RT_BOOL(pVCpu->cpum.s.fUseFlags & CPUM_USED_FPU_GUEST);
1672 AssertMsg(fRet == pVCpu->cpum.s.Guest.fUsedFpuGuest, ("fRet=%d\n", fRet));
1673 return fRet;
1674}
1675
1676
1677/**
1678 * Checks if we've really loaded the FPU/XMM state of the guest OS.
1679 *
1680 * @returns true / false.
1681 * @param pVCpu The cross context virtual CPU structure.
1682 */
1683VMMDECL(bool) CPUMIsGuestFPUStateLoaded(PVMCPU pVCpu)
1684{
1685 bool fRet = RT_BOOL(pVCpu->cpum.s.fUseFlags & CPUM_USED_FPU_GUEST);
1686 AssertMsg(fRet == pVCpu->cpum.s.Guest.fUsedFpuGuest, ("fRet=%d\n", fRet));
1687 return fRet;
1688}
1689
1690
1691/**
1692 * Checks if we saved the FPU/XMM state of the host OS.
1693 *
1694 * @returns true / false.
1695 * @param pVCpu The cross context virtual CPU structure.
1696 */
1697VMMDECL(bool) CPUMIsHostFPUStateSaved(PVMCPU pVCpu)
1698{
1699 return RT_BOOL(pVCpu->cpum.s.fUseFlags & CPUM_USED_FPU_HOST);
1700}
1701
1702
1703/**
1704 * Checks if the guest debug state is active.
1705 *
1706 * @returns boolean
1707 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1708 */
1709VMMDECL(bool) CPUMIsGuestDebugStateActive(PVMCPU pVCpu)
1710{
1711 return RT_BOOL(pVCpu->cpum.s.fUseFlags & CPUM_USED_DEBUG_REGS_GUEST);
1712}
1713
1714
1715/**
1716 * Checks if the hyper debug state is active.
1717 *
1718 * @returns boolean
1719 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1720 */
1721VMMDECL(bool) CPUMIsHyperDebugStateActive(PVMCPU pVCpu)
1722{
1723 return RT_BOOL(pVCpu->cpum.s.fUseFlags & CPUM_USED_DEBUG_REGS_HYPER);
1724}
1725
1726
1727/**
1728 * Mark the guest's debug state as inactive.
1729 *
1730 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1731 * @todo This API doesn't make sense any more.
1732 */
1733VMMDECL(void) CPUMDeactivateGuestDebugState(PVMCPU pVCpu)
1734{
1735 Assert(!(pVCpu->cpum.s.fUseFlags & (CPUM_USED_DEBUG_REGS_GUEST | CPUM_USED_DEBUG_REGS_HYPER | CPUM_USED_DEBUG_REGS_HOST)));
1736 NOREF(pVCpu);
1737}
1738
1739
1740/**
1741 * Get the current privilege level of the guest.
1742 *
1743 * @returns CPL
1744 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1745 */
1746VMMDECL(uint32_t) CPUMGetGuestCPL(PVMCPU pVCpu)
1747{
1748 /*
1749 * CPL can reliably be found in SS.DPL (hidden regs valid) or SS if not.
1750 *
1751 * Note! We used to check CS.DPL here, assuming it was always equal to
1752 * CPL even if a conforming segment was loaded. But this turned out to
1753 * only apply to older AMD-V. With VT-x we had an ACP2 regression
1754 * during install after a far call to ring 2 with VT-x. Then on newer
1755 * AMD-V CPUs we have to move the VMCB.guest.u8CPL into cs.Attr.n.u2Dpl
1756 * as well as ss.Attr.n.u2Dpl to make this (and other) code work right.
1757 *
1758 * So, forget CS.DPL, always use SS.DPL.
1759 *
1760 * Note! The SS RPL is always equal to the CPL, while the CS RPL
1761 * isn't necessarily equal if the segment is conforming.
1762 * See section 4.11.1 in the AMD manual.
1763 *
1764 * Update: Where the heck does it say CS.RPL can differ from CPL other than
1765 * right after real->prot mode switch and when in V8086 mode? That
1766 * section says the RPL specified in a direct transfere (call, jmp,
1767 * ret) is not the one loaded into CS. Besides, if CS.RPL != CPL
1768 * it would be impossible for an exception handle or the iret
1769 * instruction to figure out whether SS:ESP are part of the frame
1770 * or not. VBox or qemu bug must've lead to this misconception.
1771 *
1772 * Update2: On an AMD bulldozer system here, I've no trouble loading a null
1773 * selector into SS with an RPL other than the CPL when CPL != 3 and
1774 * we're in 64-bit mode. The intel dev box doesn't allow this, on
1775 * RPL = CPL. Weird.
1776 */
1777 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_RFLAGS | CPUMCTX_EXTRN_SS);
1778 uint32_t uCpl;
1779 if (pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE)
1780 {
1781 if (!pVCpu->cpum.s.Guest.eflags.Bits.u1VM)
1782 {
1783 if (CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, &pVCpu->cpum.s.Guest.ss))
1784 uCpl = pVCpu->cpum.s.Guest.ss.Attr.n.u2Dpl;
1785 else
1786 uCpl = (pVCpu->cpum.s.Guest.ss.Sel & X86_SEL_RPL);
1787 }
1788 else
1789 uCpl = 3; /* V86 has CPL=3; REM doesn't set DPL=3 in V8086 mode. See @bugref{5130}. */
1790 }
1791 else
1792 uCpl = 0; /* Real mode is zero; CPL set to 3 for VT-x real-mode emulation. */
1793 return uCpl;
1794}
1795
1796
1797/**
1798 * Gets the current guest CPU mode.
1799 *
1800 * If paging mode is what you need, check out PGMGetGuestMode().
1801 *
1802 * @returns The CPU mode.
1803 * @param pVCpu The cross context virtual CPU structure.
1804 */
1805VMMDECL(CPUMMODE) CPUMGetGuestMode(PVMCPU pVCpu)
1806{
1807 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_EFER);
1808 CPUMMODE enmMode;
1809 if (!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE))
1810 enmMode = CPUMMODE_REAL;
1811 else if (!(pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA))
1812 enmMode = CPUMMODE_PROTECTED;
1813 else
1814 enmMode = CPUMMODE_LONG;
1815
1816 return enmMode;
1817}
1818
1819
1820/**
1821 * Figure whether the CPU is currently executing 16, 32 or 64 bit code.
1822 *
1823 * @returns 16, 32 or 64.
1824 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1825 */
1826VMMDECL(uint32_t) CPUMGetGuestCodeBits(PVMCPU pVCpu)
1827{
1828 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_EFER | CPUMCTX_EXTRN_RFLAGS | CPUMCTX_EXTRN_CS);
1829
1830 if (!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE))
1831 return 16;
1832
1833 if (pVCpu->cpum.s.Guest.eflags.Bits.u1VM)
1834 {
1835 Assert(!(pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA));
1836 return 16;
1837 }
1838
1839 CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(pVCpu, &pVCpu->cpum.s.Guest.cs);
1840 if ( pVCpu->cpum.s.Guest.cs.Attr.n.u1Long
1841 && (pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA))
1842 return 64;
1843
1844 if (pVCpu->cpum.s.Guest.cs.Attr.n.u1DefBig)
1845 return 32;
1846
1847 return 16;
1848}
1849
1850
1851VMMDECL(DISCPUMODE) CPUMGetGuestDisMode(PVMCPU pVCpu)
1852{
1853 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_EFER | CPUMCTX_EXTRN_RFLAGS | CPUMCTX_EXTRN_CS);
1854
1855 if (!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE))
1856 return DISCPUMODE_16BIT;
1857
1858 if (pVCpu->cpum.s.Guest.eflags.Bits.u1VM)
1859 {
1860 Assert(!(pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA));
1861 return DISCPUMODE_16BIT;
1862 }
1863
1864 CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(pVCpu, &pVCpu->cpum.s.Guest.cs);
1865 if ( pVCpu->cpum.s.Guest.cs.Attr.n.u1Long
1866 && (pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA))
1867 return DISCPUMODE_64BIT;
1868
1869 if (pVCpu->cpum.s.Guest.cs.Attr.n.u1DefBig)
1870 return DISCPUMODE_32BIT;
1871
1872 return DISCPUMODE_16BIT;
1873}
1874
1875
1876/**
1877 * Gets the guest MXCSR_MASK value.
1878 *
1879 * This does not access the x87 state, but the value we determined at VM
1880 * initialization.
1881 *
1882 * @returns MXCSR mask.
1883 * @param pVM The cross context VM structure.
1884 */
1885VMMDECL(uint32_t) CPUMGetGuestMxCsrMask(PVM pVM)
1886{
1887 return pVM->cpum.s.GuestInfo.fMxCsrMask;
1888}
1889
1890
1891/**
1892 * Returns whether the guest has physical interrupts enabled.
1893 *
1894 * @returns @c true if interrupts are enabled, @c false otherwise.
1895 * @param pVCpu The cross context virtual CPU structure.
1896 *
1897 * @remarks Warning! This function does -not- take into account the global-interrupt
1898 * flag (GIF).
1899 */
1900VMM_INT_DECL(bool) CPUMIsGuestPhysIntrEnabled(PVMCPU pVCpu)
1901{
1902 switch (CPUMGetGuestInNestedHwvirtMode(&pVCpu->cpum.s.Guest))
1903 {
1904 case CPUMHWVIRT_NONE:
1905 default:
1906 return pVCpu->cpum.s.Guest.eflags.Bits.u1IF;
1907 case CPUMHWVIRT_VMX:
1908 return CPUMIsGuestVmxPhysIntrEnabled(&pVCpu->cpum.s.Guest);
1909 case CPUMHWVIRT_SVM:
1910 return CPUMIsGuestSvmPhysIntrEnabled(pVCpu, &pVCpu->cpum.s.Guest);
1911 }
1912}
1913
1914
1915/**
1916 * Returns whether the nested-guest has virtual interrupts enabled.
1917 *
1918 * @returns @c true if interrupts are enabled, @c false otherwise.
1919 * @param pVCpu The cross context virtual CPU structure.
1920 *
1921 * @remarks Warning! This function does -not- take into account the global-interrupt
1922 * flag (GIF).
1923 */
1924VMM_INT_DECL(bool) CPUMIsGuestVirtIntrEnabled(PVMCPU pVCpu)
1925{
1926 PCCPUMCTX pCtx = &pVCpu->cpum.s.Guest;
1927 Assert(CPUMIsGuestInNestedHwvirtMode(pCtx));
1928
1929 if (CPUMIsGuestInVmxNonRootMode(pCtx))
1930 return CPUMIsGuestVmxVirtIntrEnabled(pCtx);
1931
1932 Assert(CPUMIsGuestInSvmNestedHwVirtMode(pCtx));
1933 return CPUMIsGuestSvmVirtIntrEnabled(pVCpu, pCtx);
1934}
1935
1936
1937/**
1938 * Calculates the interruptiblity of the guest.
1939 *
1940 * @returns Interruptibility level.
1941 * @param pVCpu The cross context virtual CPU structure.
1942 */
1943VMM_INT_DECL(CPUMINTERRUPTIBILITY) CPUMGetGuestInterruptibility(PVMCPU pVCpu)
1944{
1945#if 1
1946 /* Global-interrupt flag blocks pretty much everything we care about here. */
1947 if (CPUMGetGuestGif(&pVCpu->cpum.s.Guest))
1948 {
1949 /*
1950 * Physical interrupts are primarily blocked using EFLAGS. However, we cannot access
1951 * it directly here. If and how EFLAGS are used depends on the context (nested-guest
1952 * or raw-mode). Hence we use the function below which handles the details.
1953 */
1954 if ( !(pVCpu->cpum.s.Guest.eflags.uBoth & CPUMCTX_INHIBIT_ALL_MASK)
1955 || ( !(pVCpu->cpum.s.Guest.eflags.uBoth & CPUMCTX_INHIBIT_NMI)
1956 && pVCpu->cpum.s.Guest.uRipInhibitInt != pVCpu->cpum.s.Guest.rip))
1957 {
1958 /** @todo OPT: this next call should be inlined! */
1959 if (CPUMIsGuestPhysIntrEnabled(pVCpu))
1960 {
1961 /** @todo OPT: type this out as it repeats tests. */
1962 if ( !CPUMIsGuestInNestedHwvirtMode(&pVCpu->cpum.s.Guest)
1963 || CPUMIsGuestVirtIntrEnabled(pVCpu))
1964 return CPUMINTERRUPTIBILITY_UNRESTRAINED;
1965
1966 /* Physical interrupts are enabled, but nested-guest virtual interrupts are disabled. */
1967 return CPUMINTERRUPTIBILITY_VIRT_INT_DISABLED;
1968 }
1969 return CPUMINTERRUPTIBILITY_INT_DISABLED;
1970 }
1971
1972 /*
1973 * Blocking the delivery of NMIs during an interrupt shadow is CPU implementation
1974 * specific. Therefore, in practice, we can't deliver an NMI in an interrupt shadow.
1975 * However, there is some uncertainity regarding the converse, i.e. whether
1976 * NMI-blocking until IRET blocks delivery of physical interrupts.
1977 *
1978 * See Intel spec. 25.4.1 "Event Blocking".
1979 */
1980 /** @todo r=bird: The above comment mixes up VMX root-mode and non-root. Section
1981 * 25.4.1 is only applicable to VMX non-root mode. In root mode /
1982 * non-VMX mode, I have not see any evidence in the intel manuals that
1983 * NMIs are not blocked when in an interrupt shadow. Section "6.7
1984 * NONMASKABLE INTERRUPT (NMI)" in SDM 3A seems pretty clear to me.
1985 */
1986 if (!(pVCpu->cpum.s.Guest.eflags.uBoth & CPUMCTX_INHIBIT_NMI))
1987 return CPUMINTERRUPTIBILITY_INT_INHIBITED;
1988 return CPUMINTERRUPTIBILITY_NMI_INHIBIT;
1989 }
1990 return CPUMINTERRUPTIBILITY_GLOBAL_INHIBIT;
1991#else
1992 if (pVCpu->cpum.s.Guest.rflags.Bits.u1IF)
1993 {
1994 if (pVCpu->cpum.s.Guest.hwvirt.fGif)
1995 {
1996 if (!VMCPU_FF_IS_ANY_SET(pVCpu, VMCPU_FF_BLOCK_NMIS | VMCPU_FF_INHIBIT_INTERRUPTS))
1997 return CPUMINTERRUPTIBILITY_UNRESTRAINED;
1998
1999 /** @todo does blocking NMIs mean interrupts are also inhibited? */
2000 if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS))
2001 {
2002 if (!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_BLOCK_NMIS))
2003 return CPUMINTERRUPTIBILITY_INT_INHIBITED;
2004 return CPUMINTERRUPTIBILITY_NMI_INHIBIT;
2005 }
2006 AssertFailed();
2007 return CPUMINTERRUPTIBILITY_NMI_INHIBIT;
2008 }
2009 return CPUMINTERRUPTIBILITY_GLOBAL_INHIBIT;
2010 }
2011 else
2012 {
2013 if (pVCpu->cpum.s.Guest.hwvirt.fGif)
2014 {
2015 if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_BLOCK_NMIS))
2016 return CPUMINTERRUPTIBILITY_NMI_INHIBIT;
2017 return CPUMINTERRUPTIBILITY_INT_DISABLED;
2018 }
2019 return CPUMINTERRUPTIBILITY_GLOBAL_INHIBIT;
2020 }
2021#endif
2022}
2023
2024
2025/**
2026 * Checks whether the SVM nested-guest has physical interrupts enabled.
2027 *
2028 * @returns true if interrupts are enabled, false otherwise.
2029 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2030 * @param pCtx The guest-CPU context.
2031 *
2032 * @remarks This does -not- take into account the global-interrupt flag.
2033 */
2034VMM_INT_DECL(bool) CPUMIsGuestSvmPhysIntrEnabled(PCVMCPU pVCpu, PCCPUMCTX pCtx)
2035{
2036 /** @todo Optimization: Avoid this function call and use a pointer to the
2037 * relevant eflags instead (setup during VMRUN instruction emulation). */
2038 Assert(CPUMIsGuestInSvmNestedHwVirtMode(pCtx));
2039
2040 X86EFLAGS fEFlags;
2041 if (CPUMIsGuestSvmVirtIntrMasking(pVCpu, pCtx))
2042 fEFlags.u = pCtx->hwvirt.svm.HostState.rflags.u;
2043 else
2044 fEFlags.u = pCtx->eflags.u;
2045
2046 return fEFlags.Bits.u1IF;
2047}
2048
2049
2050/**
2051 * Checks whether the SVM nested-guest is in a state to receive virtual (setup
2052 * for injection by VMRUN instruction) interrupts.
2053 *
2054 * @returns VBox status code.
2055 * @retval true if it's ready, false otherwise.
2056 *
2057 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2058 * @param pCtx The guest-CPU context.
2059 */
2060VMM_INT_DECL(bool) CPUMIsGuestSvmVirtIntrEnabled(PCVMCPU pVCpu, PCCPUMCTX pCtx)
2061{
2062 RT_NOREF(pVCpu);
2063 Assert(CPUMIsGuestInSvmNestedHwVirtMode(pCtx));
2064
2065 PCSVMVMCBCTRL pVmcbCtrl = &pCtx->hwvirt.svm.Vmcb.ctrl;
2066 PCSVMINTCTRL pVmcbIntCtrl = &pVmcbCtrl->IntCtrl;
2067 Assert(!pVmcbIntCtrl->n.u1VGifEnable); /* We don't support passing virtual-GIF feature to the guest yet. */
2068 if ( !pVmcbIntCtrl->n.u1IgnoreTPR
2069 && pVmcbIntCtrl->n.u4VIntrPrio <= pVmcbIntCtrl->n.u8VTPR)
2070 return false;
2071
2072 return RT_BOOL(pCtx->eflags.u & X86_EFL_IF);
2073}
2074
2075
2076/**
2077 * Gets the pending SVM nested-guest interruptvector.
2078 *
2079 * @returns The nested-guest interrupt to inject.
2080 * @param pCtx The guest-CPU context.
2081 */
2082VMM_INT_DECL(uint8_t) CPUMGetGuestSvmVirtIntrVector(PCCPUMCTX pCtx)
2083{
2084 return pCtx->hwvirt.svm.Vmcb.ctrl.IntCtrl.n.u8VIntrVector;
2085}
2086
2087
2088/**
2089 * Restores the host-state from the host-state save area as part of a \#VMEXIT.
2090 *
2091 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2092 * @param pCtx The guest-CPU context.
2093 */
2094VMM_INT_DECL(void) CPUMSvmVmExitRestoreHostState(PVMCPUCC pVCpu, PCPUMCTX pCtx)
2095{
2096 /*
2097 * Reload the guest's "host state".
2098 */
2099 PSVMHOSTSTATE pHostState = &pCtx->hwvirt.svm.HostState;
2100 pCtx->es = pHostState->es;
2101 pCtx->cs = pHostState->cs;
2102 pCtx->ss = pHostState->ss;
2103 pCtx->ds = pHostState->ds;
2104 pCtx->gdtr = pHostState->gdtr;
2105 pCtx->idtr = pHostState->idtr;
2106 CPUMSetGuestEferMsrNoChecks(pVCpu, pCtx->msrEFER, pHostState->uEferMsr);
2107 CPUMSetGuestCR0(pVCpu, pHostState->uCr0 | X86_CR0_PE);
2108 pCtx->cr3 = pHostState->uCr3;
2109 CPUMSetGuestCR4(pVCpu, pHostState->uCr4);
2110 pCtx->rflags.u = pHostState->rflags.u;
2111 pCtx->rflags.Bits.u1VM = 0;
2112 pCtx->rip = pHostState->uRip;
2113 pCtx->rsp = pHostState->uRsp;
2114 pCtx->rax = pHostState->uRax;
2115 pCtx->dr[7] &= ~(X86_DR7_ENABLED_MASK | X86_DR7_RAZ_MASK | X86_DR7_MBZ_MASK);
2116 pCtx->dr[7] |= X86_DR7_RA1_MASK;
2117 Assert(pCtx->ss.Attr.n.u2Dpl == 0);
2118
2119 /** @todo if RIP is not canonical or outside the CS segment limit, we need to
2120 * raise \#GP(0) in the guest. */
2121
2122 /** @todo check the loaded host-state for consistency. Figure out what
2123 * exactly this involves? */
2124}
2125
2126
2127/**
2128 * Saves the host-state to the host-state save area as part of a VMRUN.
2129 *
2130 * @param pCtx The guest-CPU context.
2131 * @param cbInstr The length of the VMRUN instruction in bytes.
2132 */
2133VMM_INT_DECL(void) CPUMSvmVmRunSaveHostState(PCPUMCTX pCtx, uint8_t cbInstr)
2134{
2135 PSVMHOSTSTATE pHostState = &pCtx->hwvirt.svm.HostState;
2136 pHostState->es = pCtx->es;
2137 pHostState->cs = pCtx->cs;
2138 pHostState->ss = pCtx->ss;
2139 pHostState->ds = pCtx->ds;
2140 pHostState->gdtr = pCtx->gdtr;
2141 pHostState->idtr = pCtx->idtr;
2142 pHostState->uEferMsr = pCtx->msrEFER;
2143 pHostState->uCr0 = pCtx->cr0;
2144 pHostState->uCr3 = pCtx->cr3;
2145 pHostState->uCr4 = pCtx->cr4;
2146 pHostState->rflags.u = pCtx->rflags.u;
2147 pHostState->uRip = pCtx->rip + cbInstr;
2148 pHostState->uRsp = pCtx->rsp;
2149 pHostState->uRax = pCtx->rax;
2150}
2151
2152
2153/**
2154 * Applies the TSC offset of a nested-guest if any and returns the TSC value for the
2155 * nested-guest.
2156 *
2157 * @returns The TSC offset after applying any nested-guest TSC offset.
2158 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2159 * @param uTscValue The guest TSC.
2160 *
2161 * @sa CPUMRemoveNestedGuestTscOffset.
2162 */
2163VMM_INT_DECL(uint64_t) CPUMApplyNestedGuestTscOffset(PCVMCPU pVCpu, uint64_t uTscValue)
2164{
2165 PCCPUMCTX pCtx = &pVCpu->cpum.s.Guest;
2166 if (CPUMIsGuestInVmxNonRootMode(pCtx))
2167 {
2168 if (CPUMIsGuestVmxProcCtlsSet(pCtx, VMX_PROC_CTLS_USE_TSC_OFFSETTING))
2169 return uTscValue + pCtx->hwvirt.vmx.Vmcs.u64TscOffset.u;
2170 return uTscValue;
2171 }
2172
2173 if (CPUMIsGuestInSvmNestedHwVirtMode(pCtx))
2174 {
2175 uint64_t offTsc;
2176 if (!HMGetGuestSvmTscOffset(pVCpu, &offTsc))
2177 offTsc = pCtx->hwvirt.svm.Vmcb.ctrl.u64TSCOffset;
2178 return uTscValue + offTsc;
2179 }
2180 return uTscValue;
2181}
2182
2183
2184/**
2185 * Removes the TSC offset of a nested-guest if any and returns the TSC value for the
2186 * guest.
2187 *
2188 * @returns The TSC offset after removing any nested-guest TSC offset.
2189 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2190 * @param uTscValue The nested-guest TSC.
2191 *
2192 * @sa CPUMApplyNestedGuestTscOffset.
2193 */
2194VMM_INT_DECL(uint64_t) CPUMRemoveNestedGuestTscOffset(PCVMCPU pVCpu, uint64_t uTscValue)
2195{
2196 PCCPUMCTX pCtx = &pVCpu->cpum.s.Guest;
2197 if (CPUMIsGuestInVmxNonRootMode(pCtx))
2198 {
2199 if (CPUMIsGuestVmxProcCtlsSet(pCtx, VMX_PROC_CTLS_USE_TSC_OFFSETTING))
2200 return uTscValue - pCtx->hwvirt.vmx.Vmcs.u64TscOffset.u;
2201 return uTscValue;
2202 }
2203
2204 if (CPUMIsGuestInSvmNestedHwVirtMode(pCtx))
2205 {
2206 uint64_t offTsc;
2207 if (!HMGetGuestSvmTscOffset(pVCpu, &offTsc))
2208 offTsc = pCtx->hwvirt.svm.Vmcb.ctrl.u64TSCOffset;
2209 return uTscValue - offTsc;
2210 }
2211 return uTscValue;
2212}
2213
2214
2215/**
2216 * Used to dynamically imports state residing in NEM or HM.
2217 *
2218 * This is a worker for the CPUM_IMPORT_EXTRN_RET() macro and various IEM ones.
2219 *
2220 * @returns VBox status code.
2221 * @param pVCpu The cross context virtual CPU structure of the calling thread.
2222 * @param fExtrnImport The fields to import.
2223 * @thread EMT(pVCpu)
2224 */
2225VMM_INT_DECL(int) CPUMImportGuestStateOnDemand(PVMCPUCC pVCpu, uint64_t fExtrnImport)
2226{
2227 VMCPU_ASSERT_EMT(pVCpu);
2228 if (pVCpu->cpum.s.Guest.fExtrn & fExtrnImport)
2229 {
2230 switch (pVCpu->cpum.s.Guest.fExtrn & CPUMCTX_EXTRN_KEEPER_MASK)
2231 {
2232 case CPUMCTX_EXTRN_KEEPER_NEM:
2233 {
2234 int rc = NEMImportStateOnDemand(pVCpu, fExtrnImport);
2235 Assert(rc == VINF_SUCCESS || RT_FAILURE_NP(rc));
2236 return rc;
2237 }
2238
2239 case CPUMCTX_EXTRN_KEEPER_HM:
2240 {
2241#ifdef IN_RING0
2242 int rc = HMR0ImportStateOnDemand(pVCpu, fExtrnImport);
2243 Assert(rc == VINF_SUCCESS || RT_FAILURE_NP(rc));
2244 return rc;
2245#else
2246 AssertLogRelMsgFailed(("TODO Fetch HM state: %#RX64 vs %#RX64\n", pVCpu->cpum.s.Guest.fExtrn, fExtrnImport));
2247 return VINF_SUCCESS;
2248#endif
2249 }
2250 default:
2251 AssertLogRelMsgFailedReturn(("%#RX64 vs %#RX64\n", pVCpu->cpum.s.Guest.fExtrn, fExtrnImport), VERR_CPUM_IPE_2);
2252 }
2253 }
2254 return VINF_SUCCESS;
2255}
2256
2257
2258/**
2259 * Gets valid CR4 bits for the guest.
2260 *
2261 * @returns Valid CR4 bits.
2262 * @param pVM The cross context VM structure.
2263 */
2264VMM_INT_DECL(uint64_t) CPUMGetGuestCR4ValidMask(PVM pVM)
2265{
2266 PCCPUMFEATURES pGuestFeatures = &pVM->cpum.s.GuestFeatures;
2267 uint64_t fMask = X86_CR4_VME | X86_CR4_PVI
2268 | X86_CR4_TSD | X86_CR4_DE
2269 | X86_CR4_MCE | X86_CR4_PCE;
2270 if (pGuestFeatures->fPae)
2271 fMask |= X86_CR4_PAE;
2272 if (pGuestFeatures->fPge)
2273 fMask |= X86_CR4_PGE;
2274 if (pGuestFeatures->fPse)
2275 fMask |= X86_CR4_PSE;
2276 if (pGuestFeatures->fFxSaveRstor)
2277 fMask |= X86_CR4_OSFXSR;
2278 if (pGuestFeatures->fVmx)
2279 fMask |= X86_CR4_VMXE;
2280 if (pGuestFeatures->fXSaveRstor)
2281 fMask |= X86_CR4_OSXSAVE;
2282 if (pGuestFeatures->fPcid)
2283 fMask |= X86_CR4_PCIDE;
2284 if (pGuestFeatures->fFsGsBase)
2285 fMask |= X86_CR4_FSGSBASE;
2286 if (pGuestFeatures->fSse)
2287 fMask |= X86_CR4_OSXMMEEXCPT;
2288 return fMask;
2289}
2290
2291
2292/**
2293 * Sets the PAE PDPEs for the guest.
2294 *
2295 * @param pVCpu The cross context virtual CPU structure of the calling thread.
2296 * @param paPaePdpes The PAE PDPEs to set.
2297 */
2298VMM_INT_DECL(void) CPUMSetGuestPaePdpes(PVMCPU pVCpu, PCX86PDPE paPaePdpes)
2299{
2300 Assert(paPaePdpes);
2301 for (unsigned i = 0; i < RT_ELEMENTS(pVCpu->cpum.s.Guest.aPaePdpes); i++)
2302 pVCpu->cpum.s.Guest.aPaePdpes[i].u = paPaePdpes[i].u;
2303 pVCpu->cpum.s.Guest.fExtrn &= ~CPUMCTX_EXTRN_CR3;
2304}
2305
2306
2307/**
2308 * Gets the PAE PDPTEs for the guest.
2309 *
2310 * @param pVCpu The cross context virtual CPU structure of the calling thread.
2311 * @param paPaePdpes Where to store the PAE PDPEs.
2312 */
2313VMM_INT_DECL(void) CPUMGetGuestPaePdpes(PVMCPU pVCpu, PX86PDPE paPaePdpes)
2314{
2315 Assert(paPaePdpes);
2316 CPUM_INT_ASSERT_NOT_EXTRN(pVCpu, CPUMCTX_EXTRN_CR3);
2317 for (unsigned i = 0; i < RT_ELEMENTS(pVCpu->cpum.s.Guest.aPaePdpes); i++)
2318 paPaePdpes[i].u = pVCpu->cpum.s.Guest.aPaePdpes[i].u;
2319}
2320
2321
2322/**
2323 * Starts a VMX-preemption timer to expire as specified by the nested hypervisor.
2324 *
2325 * @returns VBox status code.
2326 * @param pVCpu The cross context virtual CPU structure of the calling thread.
2327 * @param uTimer The VMCS preemption timer value.
2328 * @param cShift The VMX-preemption timer shift (usually based on guest
2329 * VMX MSR rate).
2330 * @param pu64EntryTick Where to store the current tick when the timer is
2331 * programmed.
2332 * @thread EMT(pVCpu)
2333 */
2334VMM_INT_DECL(int) CPUMStartGuestVmxPremptTimer(PVMCPUCC pVCpu, uint32_t uTimer, uint8_t cShift, uint64_t *pu64EntryTick)
2335{
2336 Assert(uTimer);
2337 Assert(cShift <= 31);
2338 Assert(pu64EntryTick);
2339 VMCPU_ASSERT_EMT(pVCpu);
2340 uint64_t const cTicksToNext = uTimer << cShift;
2341 return TMTimerSetRelative(pVCpu->CTX_SUFF(pVM), pVCpu->cpum.s.hNestedVmxPreemptTimer, cTicksToNext, pu64EntryTick);
2342}
2343
2344
2345/**
2346 * Stops the VMX-preemption timer from firing.
2347 *
2348 * @returns VBox status code.
2349 * @param pVCpu The cross context virtual CPU structure of the calling thread.
2350 * @thread EMT.
2351 *
2352 * @remarks This can be called during VM reset, so we cannot assume it will be on
2353 * the EMT corresponding to @c pVCpu.
2354 */
2355VMM_INT_DECL(int) CPUMStopGuestVmxPremptTimer(PVMCPUCC pVCpu)
2356{
2357 /*
2358 * CPUM gets initialized before TM, so we defer creation of timers till CPUMR3InitCompleted().
2359 * However, we still get called during CPUMR3Init() and hence we need to check if we have
2360 * a valid timer object before trying to stop it.
2361 */
2362 int rc;
2363 TMTIMERHANDLE hTimer = pVCpu->cpum.s.hNestedVmxPreemptTimer;
2364 if (hTimer != NIL_TMTIMERHANDLE)
2365 {
2366 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
2367 rc = TMTimerLock(pVM, hTimer, VERR_IGNORED);
2368 if (rc == VINF_SUCCESS)
2369 {
2370 if (TMTimerIsActive(pVM, hTimer))
2371 TMTimerStop(pVM, hTimer);
2372 TMTimerUnlock(pVM, hTimer);
2373 }
2374 }
2375 else
2376 rc = VERR_NOT_FOUND;
2377 return rc;
2378}
2379
2380
2381/**
2382 * Gets the read and write permission bits for an MSR in an MSR bitmap.
2383 *
2384 * @returns VMXMSRPM_XXX - the MSR permission.
2385 * @param pvMsrBitmap Pointer to the MSR bitmap.
2386 * @param idMsr The MSR to get permissions for.
2387 *
2388 * @sa hmR0VmxSetMsrPermission.
2389 */
2390VMM_INT_DECL(uint32_t) CPUMGetVmxMsrPermission(void const *pvMsrBitmap, uint32_t idMsr)
2391{
2392 AssertPtrReturn(pvMsrBitmap, VMXMSRPM_EXIT_RD | VMXMSRPM_EXIT_WR);
2393
2394 uint8_t const * const pbMsrBitmap = (uint8_t const * const)pvMsrBitmap;
2395
2396 /*
2397 * MSR Layout:
2398 * Byte index MSR range Interpreted as
2399 * 0x000 - 0x3ff 0x00000000 - 0x00001fff Low MSR read bits.
2400 * 0x400 - 0x7ff 0xc0000000 - 0xc0001fff High MSR read bits.
2401 * 0x800 - 0xbff 0x00000000 - 0x00001fff Low MSR write bits.
2402 * 0xc00 - 0xfff 0xc0000000 - 0xc0001fff High MSR write bits.
2403 *
2404 * A bit corresponding to an MSR within the above range causes a VM-exit
2405 * if the bit is 1 on executions of RDMSR/WRMSR. If an MSR falls out of
2406 * the MSR range, it always cause a VM-exit.
2407 *
2408 * See Intel spec. 24.6.9 "MSR-Bitmap Address".
2409 */
2410 uint32_t const offBitmapRead = 0;
2411 uint32_t const offBitmapWrite = 0x800;
2412 uint32_t offMsr;
2413 uint32_t iBit;
2414 if (idMsr <= UINT32_C(0x00001fff))
2415 {
2416 offMsr = 0;
2417 iBit = idMsr;
2418 }
2419 else if (idMsr - UINT32_C(0xc0000000) <= UINT32_C(0x00001fff))
2420 {
2421 offMsr = 0x400;
2422 iBit = idMsr - UINT32_C(0xc0000000);
2423 }
2424 else
2425 {
2426 LogFunc(("Warning! Out of range MSR %#RX32\n", idMsr));
2427 return VMXMSRPM_EXIT_RD | VMXMSRPM_EXIT_WR;
2428 }
2429
2430 /*
2431 * Get the MSR read permissions.
2432 */
2433 uint32_t fRet;
2434 uint32_t const offMsrRead = offBitmapRead + offMsr;
2435 Assert(offMsrRead + (iBit >> 3) < offBitmapWrite);
2436 if (ASMBitTest(pbMsrBitmap, (offMsrRead << 3) + iBit))
2437 fRet = VMXMSRPM_EXIT_RD;
2438 else
2439 fRet = VMXMSRPM_ALLOW_RD;
2440
2441 /*
2442 * Get the MSR write permissions.
2443 */
2444 uint32_t const offMsrWrite = offBitmapWrite + offMsr;
2445 Assert(offMsrWrite + (iBit >> 3) < X86_PAGE_4K_SIZE);
2446 if (ASMBitTest(pbMsrBitmap, (offMsrWrite << 3) + iBit))
2447 fRet |= VMXMSRPM_EXIT_WR;
2448 else
2449 fRet |= VMXMSRPM_ALLOW_WR;
2450
2451 Assert(VMXMSRPM_IS_FLAG_VALID(fRet));
2452 return fRet;
2453}
2454
2455
2456/**
2457 * Checks the permission bits for the specified I/O port from the given I/O bitmap
2458 * to see if causes a VM-exit.
2459 *
2460 * @returns @c true if the I/O port access must cause a VM-exit, @c false otherwise.
2461 * @param pbIoBitmap Pointer to I/O bitmap.
2462 * @param uPort The I/O port being accessed.
2463 * @param cbAccess e size of the I/O access in bytes (1, 2 or 4 bytes).
2464 */
2465static bool cpumGetVmxIoBitmapPermission(uint8_t const *pbIoBitmap, uint16_t uPort, uint8_t cbAccess)
2466{
2467 Assert(cbAccess == 1 || cbAccess == 2 || cbAccess == 4);
2468
2469 /*
2470 * If the I/O port access wraps around the 16-bit port I/O space, we must cause a
2471 * VM-exit.
2472 *
2473 * Reading 1, 2, 4 bytes at ports 0xffff, 0xfffe and 0xfffc are valid and do not
2474 * constitute a wrap around. However, reading 2 bytes at port 0xffff or 4 bytes
2475 * from port 0xffff/0xfffe/0xfffd constitute a wrap around. In other words, any
2476 * access to -both- ports 0xffff and port 0 is a wrap around.
2477 *
2478 * See Intel spec. 25.1.3 "Instructions That Cause VM Exits Conditionally".
2479 */
2480 uint32_t const uPortLast = uPort + cbAccess;
2481 if (uPortLast > 0x10000)
2482 return true;
2483
2484 /*
2485 * If any bit corresponding to the I/O access is set, we must cause a VM-exit.
2486 */
2487 uint16_t const offPerm = uPort >> 3; /* Byte offset of the port. */
2488 uint16_t const idxPermBit = uPort - (offPerm << 3); /* Bit offset within byte. */
2489 Assert(idxPermBit < 8);
2490 static const uint8_t s_afMask[] = { 0x0, 0x1, 0x3, 0x7, 0xf }; /* Bit-mask for all access sizes. */
2491 uint16_t const fMask = s_afMask[cbAccess] << idxPermBit; /* Bit-mask of the access. */
2492
2493 /* Fetch 8 or 16-bits depending on whether the access spans 8-bit boundary. */
2494 RTUINT16U uPerm;
2495 uPerm.s.Lo = pbIoBitmap[offPerm];
2496 if (idxPermBit + cbAccess > 8)
2497 uPerm.s.Hi = pbIoBitmap[offPerm + 1];
2498 else
2499 uPerm.s.Hi = 0;
2500
2501 /* If any bit for the access is 1, we must cause a VM-exit. */
2502 if (uPerm.u & fMask)
2503 return true;
2504
2505 return false;
2506}
2507
2508
2509/**
2510 * Returns whether the given VMCS field is valid and supported for the guest.
2511 *
2512 * @param pVM The cross context VM structure.
2513 * @param u64VmcsField The VMCS field.
2514 *
2515 * @remarks This takes into account the CPU features exposed to the guest.
2516 */
2517VMM_INT_DECL(bool) CPUMIsGuestVmxVmcsFieldValid(PVMCC pVM, uint64_t u64VmcsField)
2518{
2519 uint32_t const uFieldEncHi = RT_HI_U32(u64VmcsField);
2520 uint32_t const uFieldEncLo = RT_LO_U32(u64VmcsField);
2521 if (!uFieldEncHi)
2522 { /* likely */ }
2523 else
2524 return false;
2525
2526 PCCPUMFEATURES pFeat = &pVM->cpum.s.GuestFeatures;
2527 switch (uFieldEncLo)
2528 {
2529 /*
2530 * 16-bit fields.
2531 */
2532 /* Control fields. */
2533 case VMX_VMCS16_VPID: return pFeat->fVmxVpid;
2534 case VMX_VMCS16_POSTED_INT_NOTIFY_VECTOR: return pFeat->fVmxPostedInt;
2535 case VMX_VMCS16_EPTP_INDEX: return pFeat->fVmxEptXcptVe;
2536
2537 /* Guest-state fields. */
2538 case VMX_VMCS16_GUEST_ES_SEL:
2539 case VMX_VMCS16_GUEST_CS_SEL:
2540 case VMX_VMCS16_GUEST_SS_SEL:
2541 case VMX_VMCS16_GUEST_DS_SEL:
2542 case VMX_VMCS16_GUEST_FS_SEL:
2543 case VMX_VMCS16_GUEST_GS_SEL:
2544 case VMX_VMCS16_GUEST_LDTR_SEL:
2545 case VMX_VMCS16_GUEST_TR_SEL: return true;
2546 case VMX_VMCS16_GUEST_INTR_STATUS: return pFeat->fVmxVirtIntDelivery;
2547 case VMX_VMCS16_GUEST_PML_INDEX: return pFeat->fVmxPml;
2548
2549 /* Host-state fields. */
2550 case VMX_VMCS16_HOST_ES_SEL:
2551 case VMX_VMCS16_HOST_CS_SEL:
2552 case VMX_VMCS16_HOST_SS_SEL:
2553 case VMX_VMCS16_HOST_DS_SEL:
2554 case VMX_VMCS16_HOST_FS_SEL:
2555 case VMX_VMCS16_HOST_GS_SEL:
2556 case VMX_VMCS16_HOST_TR_SEL: return true;
2557
2558 /*
2559 * 64-bit fields.
2560 */
2561 /* Control fields. */
2562 case VMX_VMCS64_CTRL_IO_BITMAP_A_FULL:
2563 case VMX_VMCS64_CTRL_IO_BITMAP_A_HIGH:
2564 case VMX_VMCS64_CTRL_IO_BITMAP_B_FULL:
2565 case VMX_VMCS64_CTRL_IO_BITMAP_B_HIGH: return pFeat->fVmxUseIoBitmaps;
2566 case VMX_VMCS64_CTRL_MSR_BITMAP_FULL:
2567 case VMX_VMCS64_CTRL_MSR_BITMAP_HIGH: return pFeat->fVmxUseMsrBitmaps;
2568 case VMX_VMCS64_CTRL_EXIT_MSR_STORE_FULL:
2569 case VMX_VMCS64_CTRL_EXIT_MSR_STORE_HIGH:
2570 case VMX_VMCS64_CTRL_EXIT_MSR_LOAD_FULL:
2571 case VMX_VMCS64_CTRL_EXIT_MSR_LOAD_HIGH:
2572 case VMX_VMCS64_CTRL_ENTRY_MSR_LOAD_FULL:
2573 case VMX_VMCS64_CTRL_ENTRY_MSR_LOAD_HIGH:
2574 case VMX_VMCS64_CTRL_EXEC_VMCS_PTR_FULL:
2575 case VMX_VMCS64_CTRL_EXEC_VMCS_PTR_HIGH: return true;
2576 case VMX_VMCS64_CTRL_EXEC_PML_ADDR_FULL:
2577 case VMX_VMCS64_CTRL_EXEC_PML_ADDR_HIGH: return pFeat->fVmxPml;
2578 case VMX_VMCS64_CTRL_TSC_OFFSET_FULL:
2579 case VMX_VMCS64_CTRL_TSC_OFFSET_HIGH: return true;
2580 case VMX_VMCS64_CTRL_VIRT_APIC_PAGEADDR_FULL:
2581 case VMX_VMCS64_CTRL_VIRT_APIC_PAGEADDR_HIGH: return pFeat->fVmxUseTprShadow;
2582 case VMX_VMCS64_CTRL_APIC_ACCESSADDR_FULL:
2583 case VMX_VMCS64_CTRL_APIC_ACCESSADDR_HIGH: return pFeat->fVmxVirtApicAccess;
2584 case VMX_VMCS64_CTRL_POSTED_INTR_DESC_FULL:
2585 case VMX_VMCS64_CTRL_POSTED_INTR_DESC_HIGH: return pFeat->fVmxPostedInt;
2586 case VMX_VMCS64_CTRL_VMFUNC_CTRLS_FULL:
2587 case VMX_VMCS64_CTRL_VMFUNC_CTRLS_HIGH: return pFeat->fVmxVmFunc;
2588 case VMX_VMCS64_CTRL_EPTP_FULL:
2589 case VMX_VMCS64_CTRL_EPTP_HIGH: return pFeat->fVmxEpt;
2590 case VMX_VMCS64_CTRL_EOI_BITMAP_0_FULL:
2591 case VMX_VMCS64_CTRL_EOI_BITMAP_0_HIGH:
2592 case VMX_VMCS64_CTRL_EOI_BITMAP_1_FULL:
2593 case VMX_VMCS64_CTRL_EOI_BITMAP_1_HIGH:
2594 case VMX_VMCS64_CTRL_EOI_BITMAP_2_FULL:
2595 case VMX_VMCS64_CTRL_EOI_BITMAP_2_HIGH:
2596 case VMX_VMCS64_CTRL_EOI_BITMAP_3_FULL:
2597 case VMX_VMCS64_CTRL_EOI_BITMAP_3_HIGH: return pFeat->fVmxVirtIntDelivery;
2598 case VMX_VMCS64_CTRL_EPTP_LIST_FULL:
2599 case VMX_VMCS64_CTRL_EPTP_LIST_HIGH:
2600 {
2601 PCVMCPU pVCpu = pVM->CTX_SUFF(apCpus)[0];
2602 uint64_t const uVmFuncMsr = pVCpu->cpum.s.Guest.hwvirt.vmx.Msrs.u64VmFunc;
2603 return RT_BOOL(RT_BF_GET(uVmFuncMsr, VMX_BF_VMFUNC_EPTP_SWITCHING));
2604 }
2605 case VMX_VMCS64_CTRL_VMREAD_BITMAP_FULL:
2606 case VMX_VMCS64_CTRL_VMREAD_BITMAP_HIGH:
2607 case VMX_VMCS64_CTRL_VMWRITE_BITMAP_FULL:
2608 case VMX_VMCS64_CTRL_VMWRITE_BITMAP_HIGH: return pFeat->fVmxVmcsShadowing;
2609 case VMX_VMCS64_CTRL_VE_XCPT_INFO_ADDR_FULL:
2610 case VMX_VMCS64_CTRL_VE_XCPT_INFO_ADDR_HIGH: return pFeat->fVmxEptXcptVe;
2611 case VMX_VMCS64_CTRL_XSS_EXITING_BITMAP_FULL:
2612 case VMX_VMCS64_CTRL_XSS_EXITING_BITMAP_HIGH: return pFeat->fVmxXsavesXrstors;
2613 case VMX_VMCS64_CTRL_TSC_MULTIPLIER_FULL:
2614 case VMX_VMCS64_CTRL_TSC_MULTIPLIER_HIGH: return pFeat->fVmxUseTscScaling;
2615 case VMX_VMCS64_CTRL_PROC_EXEC3_FULL:
2616 case VMX_VMCS64_CTRL_PROC_EXEC3_HIGH: return pFeat->fVmxTertiaryExecCtls;
2617
2618 /* Read-only data fields. */
2619 case VMX_VMCS64_RO_GUEST_PHYS_ADDR_FULL:
2620 case VMX_VMCS64_RO_GUEST_PHYS_ADDR_HIGH: return pFeat->fVmxEpt;
2621
2622 /* Guest-state fields. */
2623 case VMX_VMCS64_GUEST_VMCS_LINK_PTR_FULL:
2624 case VMX_VMCS64_GUEST_VMCS_LINK_PTR_HIGH:
2625 case VMX_VMCS64_GUEST_DEBUGCTL_FULL:
2626 case VMX_VMCS64_GUEST_DEBUGCTL_HIGH: return true;
2627 case VMX_VMCS64_GUEST_PAT_FULL:
2628 case VMX_VMCS64_GUEST_PAT_HIGH: return pFeat->fVmxEntryLoadPatMsr || pFeat->fVmxExitSavePatMsr;
2629 case VMX_VMCS64_GUEST_EFER_FULL:
2630 case VMX_VMCS64_GUEST_EFER_HIGH: return pFeat->fVmxEntryLoadEferMsr || pFeat->fVmxExitSaveEferMsr;
2631 case VMX_VMCS64_GUEST_PDPTE0_FULL:
2632 case VMX_VMCS64_GUEST_PDPTE0_HIGH:
2633 case VMX_VMCS64_GUEST_PDPTE1_FULL:
2634 case VMX_VMCS64_GUEST_PDPTE1_HIGH:
2635 case VMX_VMCS64_GUEST_PDPTE2_FULL:
2636 case VMX_VMCS64_GUEST_PDPTE2_HIGH:
2637 case VMX_VMCS64_GUEST_PDPTE3_FULL:
2638 case VMX_VMCS64_GUEST_PDPTE3_HIGH: return pFeat->fVmxEpt;
2639
2640 /* Host-state fields. */
2641 case VMX_VMCS64_HOST_PAT_FULL:
2642 case VMX_VMCS64_HOST_PAT_HIGH: return pFeat->fVmxExitLoadPatMsr;
2643 case VMX_VMCS64_HOST_EFER_FULL:
2644 case VMX_VMCS64_HOST_EFER_HIGH: return pFeat->fVmxExitLoadEferMsr;
2645
2646 /*
2647 * 32-bit fields.
2648 */
2649 /* Control fields. */
2650 case VMX_VMCS32_CTRL_PIN_EXEC:
2651 case VMX_VMCS32_CTRL_PROC_EXEC:
2652 case VMX_VMCS32_CTRL_EXCEPTION_BITMAP:
2653 case VMX_VMCS32_CTRL_PAGEFAULT_ERROR_MASK:
2654 case VMX_VMCS32_CTRL_PAGEFAULT_ERROR_MATCH:
2655 case VMX_VMCS32_CTRL_CR3_TARGET_COUNT:
2656 case VMX_VMCS32_CTRL_EXIT:
2657 case VMX_VMCS32_CTRL_EXIT_MSR_STORE_COUNT:
2658 case VMX_VMCS32_CTRL_EXIT_MSR_LOAD_COUNT:
2659 case VMX_VMCS32_CTRL_ENTRY:
2660 case VMX_VMCS32_CTRL_ENTRY_MSR_LOAD_COUNT:
2661 case VMX_VMCS32_CTRL_ENTRY_INTERRUPTION_INFO:
2662 case VMX_VMCS32_CTRL_ENTRY_EXCEPTION_ERRCODE:
2663 case VMX_VMCS32_CTRL_ENTRY_INSTR_LENGTH: return true;
2664 case VMX_VMCS32_CTRL_TPR_THRESHOLD: return pFeat->fVmxUseTprShadow;
2665 case VMX_VMCS32_CTRL_PROC_EXEC2: return pFeat->fVmxSecondaryExecCtls;
2666 case VMX_VMCS32_CTRL_PLE_GAP:
2667 case VMX_VMCS32_CTRL_PLE_WINDOW: return pFeat->fVmxPauseLoopExit;
2668
2669 /* Read-only data fields. */
2670 case VMX_VMCS32_RO_VM_INSTR_ERROR:
2671 case VMX_VMCS32_RO_EXIT_REASON:
2672 case VMX_VMCS32_RO_EXIT_INTERRUPTION_INFO:
2673 case VMX_VMCS32_RO_EXIT_INTERRUPTION_ERROR_CODE:
2674 case VMX_VMCS32_RO_IDT_VECTORING_INFO:
2675 case VMX_VMCS32_RO_IDT_VECTORING_ERROR_CODE:
2676 case VMX_VMCS32_RO_EXIT_INSTR_LENGTH:
2677 case VMX_VMCS32_RO_EXIT_INSTR_INFO: return true;
2678
2679 /* Guest-state fields. */
2680 case VMX_VMCS32_GUEST_ES_LIMIT:
2681 case VMX_VMCS32_GUEST_CS_LIMIT:
2682 case VMX_VMCS32_GUEST_SS_LIMIT:
2683 case VMX_VMCS32_GUEST_DS_LIMIT:
2684 case VMX_VMCS32_GUEST_FS_LIMIT:
2685 case VMX_VMCS32_GUEST_GS_LIMIT:
2686 case VMX_VMCS32_GUEST_LDTR_LIMIT:
2687 case VMX_VMCS32_GUEST_TR_LIMIT:
2688 case VMX_VMCS32_GUEST_GDTR_LIMIT:
2689 case VMX_VMCS32_GUEST_IDTR_LIMIT:
2690 case VMX_VMCS32_GUEST_ES_ACCESS_RIGHTS:
2691 case VMX_VMCS32_GUEST_CS_ACCESS_RIGHTS:
2692 case VMX_VMCS32_GUEST_SS_ACCESS_RIGHTS:
2693 case VMX_VMCS32_GUEST_DS_ACCESS_RIGHTS:
2694 case VMX_VMCS32_GUEST_FS_ACCESS_RIGHTS:
2695 case VMX_VMCS32_GUEST_GS_ACCESS_RIGHTS:
2696 case VMX_VMCS32_GUEST_LDTR_ACCESS_RIGHTS:
2697 case VMX_VMCS32_GUEST_TR_ACCESS_RIGHTS:
2698 case VMX_VMCS32_GUEST_INT_STATE:
2699 case VMX_VMCS32_GUEST_ACTIVITY_STATE:
2700 case VMX_VMCS32_GUEST_SMBASE:
2701 case VMX_VMCS32_GUEST_SYSENTER_CS: return true;
2702 case VMX_VMCS32_PREEMPT_TIMER_VALUE: return pFeat->fVmxPreemptTimer;
2703
2704 /* Host-state fields. */
2705 case VMX_VMCS32_HOST_SYSENTER_CS: return true;
2706
2707 /*
2708 * Natural-width fields.
2709 */
2710 /* Control fields. */
2711 case VMX_VMCS_CTRL_CR0_MASK:
2712 case VMX_VMCS_CTRL_CR4_MASK:
2713 case VMX_VMCS_CTRL_CR0_READ_SHADOW:
2714 case VMX_VMCS_CTRL_CR4_READ_SHADOW:
2715 case VMX_VMCS_CTRL_CR3_TARGET_VAL0:
2716 case VMX_VMCS_CTRL_CR3_TARGET_VAL1:
2717 case VMX_VMCS_CTRL_CR3_TARGET_VAL2:
2718 case VMX_VMCS_CTRL_CR3_TARGET_VAL3: return true;
2719
2720 /* Read-only data fields. */
2721 case VMX_VMCS_RO_EXIT_QUALIFICATION:
2722 case VMX_VMCS_RO_IO_RCX:
2723 case VMX_VMCS_RO_IO_RSI:
2724 case VMX_VMCS_RO_IO_RDI:
2725 case VMX_VMCS_RO_IO_RIP:
2726 case VMX_VMCS_RO_GUEST_LINEAR_ADDR: return true;
2727
2728 /* Guest-state fields. */
2729 case VMX_VMCS_GUEST_CR0:
2730 case VMX_VMCS_GUEST_CR3:
2731 case VMX_VMCS_GUEST_CR4:
2732 case VMX_VMCS_GUEST_ES_BASE:
2733 case VMX_VMCS_GUEST_CS_BASE:
2734 case VMX_VMCS_GUEST_SS_BASE:
2735 case VMX_VMCS_GUEST_DS_BASE:
2736 case VMX_VMCS_GUEST_FS_BASE:
2737 case VMX_VMCS_GUEST_GS_BASE:
2738 case VMX_VMCS_GUEST_LDTR_BASE:
2739 case VMX_VMCS_GUEST_TR_BASE:
2740 case VMX_VMCS_GUEST_GDTR_BASE:
2741 case VMX_VMCS_GUEST_IDTR_BASE:
2742 case VMX_VMCS_GUEST_DR7:
2743 case VMX_VMCS_GUEST_RSP:
2744 case VMX_VMCS_GUEST_RIP:
2745 case VMX_VMCS_GUEST_RFLAGS:
2746 case VMX_VMCS_GUEST_PENDING_DEBUG_XCPTS:
2747 case VMX_VMCS_GUEST_SYSENTER_ESP:
2748 case VMX_VMCS_GUEST_SYSENTER_EIP: return true;
2749
2750 /* Host-state fields. */
2751 case VMX_VMCS_HOST_CR0:
2752 case VMX_VMCS_HOST_CR3:
2753 case VMX_VMCS_HOST_CR4:
2754 case VMX_VMCS_HOST_FS_BASE:
2755 case VMX_VMCS_HOST_GS_BASE:
2756 case VMX_VMCS_HOST_TR_BASE:
2757 case VMX_VMCS_HOST_GDTR_BASE:
2758 case VMX_VMCS_HOST_IDTR_BASE:
2759 case VMX_VMCS_HOST_SYSENTER_ESP:
2760 case VMX_VMCS_HOST_SYSENTER_EIP:
2761 case VMX_VMCS_HOST_RSP:
2762 case VMX_VMCS_HOST_RIP: return true;
2763 }
2764
2765 return false;
2766}
2767
2768
2769/**
2770 * Checks whether the given I/O access should cause a nested-guest VM-exit.
2771 *
2772 * @returns @c true if it causes a VM-exit, @c false otherwise.
2773 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2774 * @param u16Port The I/O port being accessed.
2775 * @param cbAccess The size of the I/O access in bytes (1, 2 or 4 bytes).
2776 */
2777VMM_INT_DECL(bool) CPUMIsGuestVmxIoInterceptSet(PCVMCPU pVCpu, uint16_t u16Port, uint8_t cbAccess)
2778{
2779 PCCPUMCTX pCtx = &pVCpu->cpum.s.Guest;
2780 if (CPUMIsGuestVmxProcCtlsSet(pCtx, VMX_PROC_CTLS_UNCOND_IO_EXIT))
2781 return true;
2782
2783 if (CPUMIsGuestVmxProcCtlsSet(pCtx, VMX_PROC_CTLS_USE_IO_BITMAPS))
2784 return cpumGetVmxIoBitmapPermission(pCtx->hwvirt.vmx.abIoBitmap, u16Port, cbAccess);
2785
2786 return false;
2787}
2788
2789
2790/**
2791 * Checks whether the Mov-to-CR3 instruction causes a nested-guest VM-exit.
2792 *
2793 * @returns @c true if it causes a VM-exit, @c false otherwise.
2794 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2795 * @param uNewCr3 The CR3 value being written.
2796 */
2797VMM_INT_DECL(bool) CPUMIsGuestVmxMovToCr3InterceptSet(PVMCPU pVCpu, uint64_t uNewCr3)
2798{
2799 /*
2800 * If the CR3-load exiting control is set and the new CR3 value does not
2801 * match any of the CR3-target values in the VMCS, we must cause a VM-exit.
2802 *
2803 * See Intel spec. 25.1.3 "Instructions That Cause VM Exits Conditionally".
2804 */
2805 PCCPUMCTX const pCtx = &pVCpu->cpum.s.Guest;
2806 if (CPUMIsGuestVmxProcCtlsSet(pCtx, VMX_PROC_CTLS_CR3_LOAD_EXIT))
2807 {
2808 uint32_t const uCr3TargetCount = pCtx->hwvirt.vmx.Vmcs.u32Cr3TargetCount;
2809 Assert(uCr3TargetCount <= VMX_V_CR3_TARGET_COUNT);
2810
2811 /* If the CR3-target count is 0, cause a VM-exit. */
2812 if (uCr3TargetCount == 0)
2813 return true;
2814
2815 /* If the CR3 being written doesn't match any of the target values, cause a VM-exit. */
2816 AssertCompile(VMX_V_CR3_TARGET_COUNT == 4);
2817 if ( uNewCr3 != pCtx->hwvirt.vmx.Vmcs.u64Cr3Target0.u
2818 && uNewCr3 != pCtx->hwvirt.vmx.Vmcs.u64Cr3Target1.u
2819 && uNewCr3 != pCtx->hwvirt.vmx.Vmcs.u64Cr3Target2.u
2820 && uNewCr3 != pCtx->hwvirt.vmx.Vmcs.u64Cr3Target3.u)
2821 return true;
2822 }
2823 return false;
2824}
2825
2826
2827/**
2828 * Checks whether a VMREAD or VMWRITE instruction for the given VMCS field causes a
2829 * VM-exit or not.
2830 *
2831 * @returns @c true if the VMREAD/VMWRITE is intercepted, @c false otherwise.
2832 * @param pVCpu The cross context virtual CPU structure.
2833 * @param uExitReason The VM-exit reason (VMX_EXIT_VMREAD or
2834 * VMX_EXIT_VMREAD).
2835 * @param u64VmcsField The VMCS field.
2836 */
2837VMM_INT_DECL(bool) CPUMIsGuestVmxVmreadVmwriteInterceptSet(PCVMCPU pVCpu, uint32_t uExitReason, uint64_t u64VmcsField)
2838{
2839 Assert(CPUMIsGuestInVmxNonRootMode(&pVCpu->cpum.s.Guest));
2840 Assert( uExitReason == VMX_EXIT_VMREAD
2841 || uExitReason == VMX_EXIT_VMWRITE);
2842
2843 /*
2844 * Without VMCS shadowing, all VMREAD and VMWRITE instructions are intercepted.
2845 */
2846 if (!CPUMIsGuestVmxProcCtls2Set(&pVCpu->cpum.s.Guest, VMX_PROC_CTLS2_VMCS_SHADOWING))
2847 return true;
2848
2849 /*
2850 * If any reserved bit in the 64-bit VMCS field encoding is set, the VMREAD/VMWRITE
2851 * is intercepted. This excludes any reserved bits in the valid parts of the field
2852 * encoding (i.e. bit 12).
2853 */
2854 if (u64VmcsField & VMX_VMCSFIELD_RSVD_MASK)
2855 return true;
2856
2857 /*
2858 * Finally, consult the VMREAD/VMWRITE bitmap whether to intercept the instruction or not.
2859 */
2860 uint32_t const u32VmcsField = RT_LO_U32(u64VmcsField);
2861 uint8_t const * const pbBitmap = uExitReason == VMX_EXIT_VMREAD
2862 ? &pVCpu->cpum.s.Guest.hwvirt.vmx.abVmreadBitmap[0]
2863 : &pVCpu->cpum.s.Guest.hwvirt.vmx.abVmwriteBitmap[0];
2864 Assert(pbBitmap);
2865 Assert(u32VmcsField >> 3 < VMX_V_VMREAD_VMWRITE_BITMAP_SIZE);
2866 return ASMBitTest(pbBitmap, (u32VmcsField << 3) + (u32VmcsField & 7));
2867}
2868
2869
2870
2871/**
2872 * Determines whether the given I/O access should cause a nested-guest \#VMEXIT.
2873 *
2874 * @param pvIoBitmap Pointer to the nested-guest IO bitmap.
2875 * @param u16Port The IO port being accessed.
2876 * @param enmIoType The type of IO access.
2877 * @param cbReg The IO operand size in bytes.
2878 * @param cAddrSizeBits The address size bits (for 16, 32 or 64).
2879 * @param iEffSeg The effective segment number.
2880 * @param fRep Whether this is a repeating IO instruction (REP prefix).
2881 * @param fStrIo Whether this is a string IO instruction.
2882 * @param pIoExitInfo Pointer to the SVMIOIOEXITINFO struct to be filled.
2883 * Optional, can be NULL.
2884 */
2885VMM_INT_DECL(bool) CPUMIsSvmIoInterceptSet(void *pvIoBitmap, uint16_t u16Port, SVMIOIOTYPE enmIoType, uint8_t cbReg,
2886 uint8_t cAddrSizeBits, uint8_t iEffSeg, bool fRep, bool fStrIo,
2887 PSVMIOIOEXITINFO pIoExitInfo)
2888{
2889 Assert(cAddrSizeBits == 16 || cAddrSizeBits == 32 || cAddrSizeBits == 64);
2890 Assert(cbReg == 1 || cbReg == 2 || cbReg == 4 || cbReg == 8);
2891
2892 /*
2893 * The IOPM layout:
2894 * Each bit represents one 8-bit port. That makes a total of 0..65535 bits or
2895 * two 4K pages.
2896 *
2897 * For IO instructions that access more than a single byte, the permission bits
2898 * for all bytes are checked; if any bit is set to 1, the IO access is intercepted.
2899 *
2900 * Since it's possible to do a 32-bit IO access at port 65534 (accessing 4 bytes),
2901 * we need 3 extra bits beyond the second 4K page.
2902 */
2903 static const uint16_t s_auSizeMasks[] = { 0, 1, 3, 0, 0xf, 0, 0, 0 };
2904
2905 uint16_t const offIopm = u16Port >> 3;
2906 uint16_t const fSizeMask = s_auSizeMasks[(cAddrSizeBits >> SVM_IOIO_OP_SIZE_SHIFT) & 7];
2907 uint8_t const cShift = u16Port - (offIopm << 3);
2908 uint16_t const fIopmMask = (1 << cShift) | (fSizeMask << cShift);
2909
2910 uint8_t const *pbIopm = (uint8_t *)pvIoBitmap;
2911 Assert(pbIopm);
2912 pbIopm += offIopm;
2913 uint16_t const u16Iopm = *(uint16_t *)pbIopm;
2914 if (u16Iopm & fIopmMask)
2915 {
2916 if (pIoExitInfo)
2917 {
2918 static const uint32_t s_auIoOpSize[] =
2919 { SVM_IOIO_32_BIT_OP, SVM_IOIO_8_BIT_OP, SVM_IOIO_16_BIT_OP, 0, SVM_IOIO_32_BIT_OP, 0, 0, 0 };
2920
2921 static const uint32_t s_auIoAddrSize[] =
2922 { 0, SVM_IOIO_16_BIT_ADDR, SVM_IOIO_32_BIT_ADDR, 0, SVM_IOIO_64_BIT_ADDR, 0, 0, 0 };
2923
2924 pIoExitInfo->u = s_auIoOpSize[cbReg & 7];
2925 pIoExitInfo->u |= s_auIoAddrSize[(cAddrSizeBits >> 4) & 7];
2926 pIoExitInfo->n.u1Str = fStrIo;
2927 pIoExitInfo->n.u1Rep = fRep;
2928 pIoExitInfo->n.u3Seg = iEffSeg & 7;
2929 pIoExitInfo->n.u1Type = enmIoType;
2930 pIoExitInfo->n.u16Port = u16Port;
2931 }
2932 return true;
2933 }
2934
2935 /** @todo remove later (for debugging as VirtualBox always traps all IO
2936 * intercepts). */
2937 AssertMsgFailed(("CPUMSvmIsIOInterceptActive: We expect an IO intercept here!\n"));
2938 return false;
2939}
2940
2941
2942/**
2943 * Gets the MSR permission bitmap byte and bit offset for the specified MSR.
2944 *
2945 * @returns VBox status code.
2946 * @param idMsr The MSR being requested.
2947 * @param pbOffMsrpm Where to store the byte offset in the MSR permission
2948 * bitmap for @a idMsr.
2949 * @param puMsrpmBit Where to store the bit offset starting at the byte
2950 * returned in @a pbOffMsrpm.
2951 */
2952VMM_INT_DECL(int) CPUMGetSvmMsrpmOffsetAndBit(uint32_t idMsr, uint16_t *pbOffMsrpm, uint8_t *puMsrpmBit)
2953{
2954 Assert(pbOffMsrpm);
2955 Assert(puMsrpmBit);
2956
2957 /*
2958 * MSRPM Layout:
2959 * Byte offset MSR range
2960 * 0x000 - 0x7ff 0x00000000 - 0x00001fff
2961 * 0x800 - 0xfff 0xc0000000 - 0xc0001fff
2962 * 0x1000 - 0x17ff 0xc0010000 - 0xc0011fff
2963 * 0x1800 - 0x1fff Reserved
2964 *
2965 * Each MSR is represented by 2 permission bits (read and write).
2966 */
2967 if (idMsr <= 0x00001fff)
2968 {
2969 /* Pentium-compatible MSRs. */
2970 uint32_t const bitoffMsr = idMsr << 1;
2971 *pbOffMsrpm = bitoffMsr >> 3;
2972 *puMsrpmBit = bitoffMsr & 7;
2973 return VINF_SUCCESS;
2974 }
2975
2976 if ( idMsr >= 0xc0000000
2977 && idMsr <= 0xc0001fff)
2978 {
2979 /* AMD Sixth Generation x86 Processor MSRs. */
2980 uint32_t const bitoffMsr = (idMsr - 0xc0000000) << 1;
2981 *pbOffMsrpm = 0x800 + (bitoffMsr >> 3);
2982 *puMsrpmBit = bitoffMsr & 7;
2983 return VINF_SUCCESS;
2984 }
2985
2986 if ( idMsr >= 0xc0010000
2987 && idMsr <= 0xc0011fff)
2988 {
2989 /* AMD Seventh and Eighth Generation Processor MSRs. */
2990 uint32_t const bitoffMsr = (idMsr - 0xc0010000) << 1;
2991 *pbOffMsrpm = 0x1000 + (bitoffMsr >> 3);
2992 *puMsrpmBit = bitoffMsr & 7;
2993 return VINF_SUCCESS;
2994 }
2995
2996 *pbOffMsrpm = 0;
2997 *puMsrpmBit = 0;
2998 return VERR_OUT_OF_RANGE;
2999}
3000
3001
3002/**
3003 * Checks whether the guest is in VMX non-root mode and using EPT paging.
3004 *
3005 * @returns @c true if in VMX non-root operation with EPT, @c false otherwise.
3006 * @param pVCpu The cross context virtual CPU structure.
3007 */
3008VMM_INT_DECL(bool) CPUMIsGuestVmxEptPagingEnabled(PCVMCPUCC pVCpu)
3009{
3010 return CPUMIsGuestVmxEptPagingEnabledEx(&pVCpu->cpum.s.Guest);
3011}
3012
3013
3014/**
3015 * Checks whether the guest is in VMX non-root mode and using EPT paging and the
3016 * nested-guest is in PAE mode.
3017 *
3018 * @returns @c true if in VMX non-root operation with EPT, @c false otherwise.
3019 * @param pVCpu The cross context virtual CPU structure.
3020 */
3021VMM_INT_DECL(bool) CPUMIsGuestVmxEptPaePagingEnabled(PCVMCPUCC pVCpu)
3022{
3023 return CPUMIsGuestVmxEptPagingEnabledEx(&pVCpu->cpum.s.Guest)
3024 && CPUMIsGuestInPAEModeEx(&pVCpu->cpum.s.Guest);
3025}
3026
3027
3028/**
3029 * Returns the guest-physical address of the APIC-access page when executing a
3030 * nested-guest.
3031 *
3032 * @returns The APIC-access page guest-physical address.
3033 * @param pVCpu The cross context virtual CPU structure.
3034 */
3035VMM_INT_DECL(uint64_t) CPUMGetGuestVmxApicAccessPageAddr(PCVMCPUCC pVCpu)
3036{
3037 return CPUMGetGuestVmxApicAccessPageAddrEx(&pVCpu->cpum.s.Guest);
3038}
3039
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