VirtualBox

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

Last change on this file since 71290 was 70948, checked in by vboxsync, 7 years ago

VMM: Added a bMainExecutionEngine member to the VM structure for use instead of fHMEnabled and fNEMEnabled. Changed a lot of HMIsEnabled invocations to use the new macros VM_IS_RAW_MODE_ENABLED and VM_IS_HM_OR_NEM_ENABLED. Eliminated fHMEnabledFixed. Fixed inverted test for raw-mode debug register sanity checking. Some other minor cleanups.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 78.7 KB
Line 
1/* $Id: CPUMAllRegs.cpp 70948 2018-02-10 15:38:12Z vboxsync $ */
2/** @file
3 * CPUM - CPU Monitor(/Manager) - Getters and Setters.
4 */
5
6/*
7 * Copyright (C) 2006-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_CPUM
23#include <VBox/vmm/cpum.h>
24#include <VBox/vmm/patm.h>
25#include <VBox/vmm/dbgf.h>
26#include <VBox/vmm/apic.h>
27#include <VBox/vmm/pgm.h>
28#include <VBox/vmm/mm.h>
29#include <VBox/vmm/em.h>
30#if defined(VBOX_WITH_RAW_MODE) && !defined(IN_RING0)
31# include <VBox/vmm/selm.h>
32#endif
33#include "CPUMInternal.h"
34#include <VBox/vmm/vm.h>
35#include <VBox/err.h>
36#include <VBox/dis.h>
37#include <VBox/log.h>
38#include <VBox/vmm/hm.h>
39#include <VBox/vmm/tm.h>
40#include <iprt/assert.h>
41#include <iprt/asm.h>
42#include <iprt/asm-amd64-x86.h>
43#ifdef IN_RING3
44#include <iprt/thread.h>
45#endif
46
47/** Disable stack frame pointer generation here. */
48#if defined(_MSC_VER) && !defined(DEBUG) && defined(RT_ARCH_X86)
49# pragma optimize("y", off)
50#endif
51
52AssertCompile2MemberOffsets(VM, cpum.s.HostFeatures, cpum.ro.HostFeatures);
53AssertCompile2MemberOffsets(VM, cpum.s.GuestFeatures, cpum.ro.GuestFeatures);
54
55
56/*********************************************************************************************************************************
57* Defined Constants And Macros *
58*********************************************************************************************************************************/
59/**
60 * Converts a CPUMCPU::Guest pointer into a VMCPU pointer.
61 *
62 * @returns Pointer to the Virtual CPU.
63 * @param a_pGuestCtx Pointer to the guest context.
64 */
65#define CPUM_GUEST_CTX_TO_VMCPU(a_pGuestCtx) RT_FROM_MEMBER(a_pGuestCtx, VMCPU, cpum.s.Guest)
66
67/**
68 * Lazily loads the hidden parts of a selector register when using raw-mode.
69 */
70#if defined(VBOX_WITH_RAW_MODE) && !defined(IN_RING0)
71# define CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(a_pVCpu, a_pSReg) \
72 do \
73 { \
74 if (!CPUMSELREG_ARE_HIDDEN_PARTS_VALID(a_pVCpu, a_pSReg)) \
75 cpumGuestLazyLoadHiddenSelectorReg(a_pVCpu, a_pSReg); \
76 } while (0)
77#else
78# define CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(a_pVCpu, a_pSReg) \
79 Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(a_pVCpu, a_pSReg));
80#endif
81
82
83
84#ifdef VBOX_WITH_RAW_MODE_NOT_R0
85
86/**
87 * Does the lazy hidden selector register loading.
88 *
89 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
90 * @param pSReg The selector register to lazily load hidden parts of.
91 */
92static void cpumGuestLazyLoadHiddenSelectorReg(PVMCPU pVCpu, PCPUMSELREG pSReg)
93{
94 Assert(!CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, pSReg));
95 Assert(VM_IS_RAW_MODE_ENABLED(pVCpu->CTX_SUFF(pVM)));
96 Assert((uintptr_t)(pSReg - &pVCpu->cpum.s.Guest.es) < X86_SREG_COUNT);
97
98 if (pVCpu->cpum.s.Guest.eflags.Bits.u1VM)
99 {
100 /* V8086 mode - Tightly controlled environment, no question about the limit or flags. */
101 pSReg->Attr.u = 0;
102 pSReg->Attr.n.u4Type = pSReg == &pVCpu->cpum.s.Guest.cs ? X86_SEL_TYPE_ER_ACC : X86_SEL_TYPE_RW_ACC;
103 pSReg->Attr.n.u1DescType = 1; /* code/data segment */
104 pSReg->Attr.n.u2Dpl = 3;
105 pSReg->Attr.n.u1Present = 1;
106 pSReg->u32Limit = 0x0000ffff;
107 pSReg->u64Base = (uint32_t)pSReg->Sel << 4;
108 pSReg->ValidSel = pSReg->Sel;
109 pSReg->fFlags = CPUMSELREG_FLAGS_VALID;
110 /** @todo Check what the accessed bit should be (VT-x and AMD-V). */
111 }
112 else if (!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE))
113 {
114 /* Real mode - leave the limit and flags alone here, at least for now. */
115 pSReg->u64Base = (uint32_t)pSReg->Sel << 4;
116 pSReg->ValidSel = pSReg->Sel;
117 pSReg->fFlags = CPUMSELREG_FLAGS_VALID;
118 }
119 else
120 {
121 /* Protected mode - get it from the selector descriptor tables. */
122 if (!(pSReg->Sel & X86_SEL_MASK_OFF_RPL))
123 {
124 Assert(!CPUMIsGuestInLongMode(pVCpu));
125 pSReg->Sel = 0;
126 pSReg->u64Base = 0;
127 pSReg->u32Limit = 0;
128 pSReg->Attr.u = 0;
129 pSReg->ValidSel = 0;
130 pSReg->fFlags = CPUMSELREG_FLAGS_VALID;
131 /** @todo see todo in iemHlpLoadNullDataSelectorProt. */
132 }
133 else
134 SELMLoadHiddenSelectorReg(pVCpu, &pVCpu->cpum.s.Guest, pSReg);
135 }
136}
137
138
139/**
140 * Makes sure the hidden CS and SS selector registers are valid, loading them if
141 * necessary.
142 *
143 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
144 */
145VMM_INT_DECL(void) CPUMGuestLazyLoadHiddenCsAndSs(PVMCPU pVCpu)
146{
147 CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(pVCpu, &pVCpu->cpum.s.Guest.cs);
148 CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(pVCpu, &pVCpu->cpum.s.Guest.ss);
149}
150
151
152/**
153 * Loads a the hidden parts of a selector register.
154 *
155 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
156 * @param pSReg The selector register to lazily load hidden parts of.
157 */
158VMM_INT_DECL(void) CPUMGuestLazyLoadHiddenSelectorReg(PVMCPU pVCpu, PCPUMSELREG pSReg)
159{
160 CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(pVCpu, pSReg);
161}
162
163#endif /* VBOX_WITH_RAW_MODE_NOT_R0 */
164
165
166/**
167 * Obsolete.
168 *
169 * We don't support nested hypervisor context interrupts or traps. Life is much
170 * simpler when we don't. It's also slightly faster at times.
171 *
172 * @param pVCpu The cross context virtual CPU structure.
173 */
174VMMDECL(PCCPUMCTXCORE) CPUMGetHyperCtxCore(PVMCPU pVCpu)
175{
176 return CPUMCTX2CORE(&pVCpu->cpum.s.Hyper);
177}
178
179
180/**
181 * Gets the pointer to the hypervisor CPU context structure of a virtual CPU.
182 *
183 * @param pVCpu The cross context virtual CPU structure.
184 */
185VMMDECL(PCPUMCTX) CPUMGetHyperCtxPtr(PVMCPU pVCpu)
186{
187 return &pVCpu->cpum.s.Hyper;
188}
189
190
191VMMDECL(void) CPUMSetHyperGDTR(PVMCPU pVCpu, uint32_t addr, uint16_t limit)
192{
193 pVCpu->cpum.s.Hyper.gdtr.cbGdt = limit;
194 pVCpu->cpum.s.Hyper.gdtr.pGdt = addr;
195}
196
197
198VMMDECL(void) CPUMSetHyperIDTR(PVMCPU pVCpu, uint32_t addr, uint16_t limit)
199{
200 pVCpu->cpum.s.Hyper.idtr.cbIdt = limit;
201 pVCpu->cpum.s.Hyper.idtr.pIdt = addr;
202}
203
204
205VMMDECL(void) CPUMSetHyperCR3(PVMCPU pVCpu, uint32_t cr3)
206{
207 pVCpu->cpum.s.Hyper.cr3 = cr3;
208
209#ifdef IN_RC
210 /* Update the current CR3. */
211 ASMSetCR3(cr3);
212#endif
213}
214
215VMMDECL(uint32_t) CPUMGetHyperCR3(PVMCPU pVCpu)
216{
217 return pVCpu->cpum.s.Hyper.cr3;
218}
219
220
221VMMDECL(void) CPUMSetHyperCS(PVMCPU pVCpu, RTSEL SelCS)
222{
223 pVCpu->cpum.s.Hyper.cs.Sel = SelCS;
224}
225
226
227VMMDECL(void) CPUMSetHyperDS(PVMCPU pVCpu, RTSEL SelDS)
228{
229 pVCpu->cpum.s.Hyper.ds.Sel = SelDS;
230}
231
232
233VMMDECL(void) CPUMSetHyperES(PVMCPU pVCpu, RTSEL SelES)
234{
235 pVCpu->cpum.s.Hyper.es.Sel = SelES;
236}
237
238
239VMMDECL(void) CPUMSetHyperFS(PVMCPU pVCpu, RTSEL SelFS)
240{
241 pVCpu->cpum.s.Hyper.fs.Sel = SelFS;
242}
243
244
245VMMDECL(void) CPUMSetHyperGS(PVMCPU pVCpu, RTSEL SelGS)
246{
247 pVCpu->cpum.s.Hyper.gs.Sel = SelGS;
248}
249
250
251VMMDECL(void) CPUMSetHyperSS(PVMCPU pVCpu, RTSEL SelSS)
252{
253 pVCpu->cpum.s.Hyper.ss.Sel = SelSS;
254}
255
256
257VMMDECL(void) CPUMSetHyperESP(PVMCPU pVCpu, uint32_t u32ESP)
258{
259 pVCpu->cpum.s.Hyper.esp = u32ESP;
260}
261
262
263VMMDECL(void) CPUMSetHyperEDX(PVMCPU pVCpu, uint32_t u32ESP)
264{
265 pVCpu->cpum.s.Hyper.esp = u32ESP;
266}
267
268
269VMMDECL(int) CPUMSetHyperEFlags(PVMCPU pVCpu, uint32_t Efl)
270{
271 pVCpu->cpum.s.Hyper.eflags.u32 = Efl;
272 return VINF_SUCCESS;
273}
274
275
276VMMDECL(void) CPUMSetHyperEIP(PVMCPU pVCpu, uint32_t u32EIP)
277{
278 pVCpu->cpum.s.Hyper.eip = u32EIP;
279}
280
281
282/**
283 * Used by VMMR3RawRunGC to reinitialize the general raw-mode context registers,
284 * EFLAGS and EIP prior to resuming guest execution.
285 *
286 * All general register not given as a parameter will be set to 0. The EFLAGS
287 * register will be set to sane values for C/C++ code execution with interrupts
288 * disabled and IOPL 0.
289 *
290 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
291 * @param u32EIP The EIP value.
292 * @param u32ESP The ESP value.
293 * @param u32EAX The EAX value.
294 * @param u32EDX The EDX value.
295 */
296VMM_INT_DECL(void) CPUMSetHyperState(PVMCPU pVCpu, uint32_t u32EIP, uint32_t u32ESP, uint32_t u32EAX, uint32_t u32EDX)
297{
298 pVCpu->cpum.s.Hyper.eip = u32EIP;
299 pVCpu->cpum.s.Hyper.esp = u32ESP;
300 pVCpu->cpum.s.Hyper.eax = u32EAX;
301 pVCpu->cpum.s.Hyper.edx = u32EDX;
302 pVCpu->cpum.s.Hyper.ecx = 0;
303 pVCpu->cpum.s.Hyper.ebx = 0;
304 pVCpu->cpum.s.Hyper.ebp = 0;
305 pVCpu->cpum.s.Hyper.esi = 0;
306 pVCpu->cpum.s.Hyper.edi = 0;
307 pVCpu->cpum.s.Hyper.eflags.u = X86_EFL_1;
308}
309
310
311VMMDECL(void) CPUMSetHyperTR(PVMCPU pVCpu, RTSEL SelTR)
312{
313 pVCpu->cpum.s.Hyper.tr.Sel = SelTR;
314}
315
316
317VMMDECL(void) CPUMSetHyperLDTR(PVMCPU pVCpu, RTSEL SelLDTR)
318{
319 pVCpu->cpum.s.Hyper.ldtr.Sel = SelLDTR;
320}
321
322
323/** @def MAYBE_LOAD_DRx
324 * Macro for updating DRx values in raw-mode and ring-0 contexts.
325 */
326#ifdef IN_RING0
327# if HC_ARCH_BITS == 32 && defined(VBOX_WITH_64_BITS_GUESTS)
328# define MAYBE_LOAD_DRx(a_pVCpu, a_fnLoad, a_uValue) \
329 do { \
330 if (!CPUMIsGuestInLongModeEx(&(a_pVCpu)->cpum.s.Guest)) \
331 a_fnLoad(a_uValue); \
332 else \
333 (a_pVCpu)->cpum.s.fUseFlags |= CPUM_SYNC_DEBUG_REGS_HYPER; \
334 } while (0)
335# else
336# define MAYBE_LOAD_DRx(a_pVCpu, a_fnLoad, a_uValue) \
337 do { \
338 a_fnLoad(a_uValue); \
339 } while (0)
340# endif
341
342#elif defined(IN_RC)
343# define MAYBE_LOAD_DRx(a_pVCpu, a_fnLoad, a_uValue) \
344 do { \
345 if ((a_pVCpu)->cpum.s.fUseFlags & CPUM_USED_DEBUG_REGS_HYPER) \
346 { a_fnLoad(a_uValue); } \
347 } while (0)
348
349#else
350# define MAYBE_LOAD_DRx(a_pVCpu, a_fnLoad, a_uValue) do { } while (0)
351#endif
352
353VMMDECL(void) CPUMSetHyperDR0(PVMCPU pVCpu, RTGCUINTREG uDr0)
354{
355 pVCpu->cpum.s.Hyper.dr[0] = uDr0;
356 MAYBE_LOAD_DRx(pVCpu, ASMSetDR0, uDr0);
357}
358
359
360VMMDECL(void) CPUMSetHyperDR1(PVMCPU pVCpu, RTGCUINTREG uDr1)
361{
362 pVCpu->cpum.s.Hyper.dr[1] = uDr1;
363 MAYBE_LOAD_DRx(pVCpu, ASMSetDR1, uDr1);
364}
365
366
367VMMDECL(void) CPUMSetHyperDR2(PVMCPU pVCpu, RTGCUINTREG uDr2)
368{
369 pVCpu->cpum.s.Hyper.dr[2] = uDr2;
370 MAYBE_LOAD_DRx(pVCpu, ASMSetDR2, uDr2);
371}
372
373
374VMMDECL(void) CPUMSetHyperDR3(PVMCPU pVCpu, RTGCUINTREG uDr3)
375{
376 pVCpu->cpum.s.Hyper.dr[3] = uDr3;
377 MAYBE_LOAD_DRx(pVCpu, ASMSetDR3, uDr3);
378}
379
380
381VMMDECL(void) CPUMSetHyperDR6(PVMCPU pVCpu, RTGCUINTREG uDr6)
382{
383 pVCpu->cpum.s.Hyper.dr[6] = uDr6;
384}
385
386
387VMMDECL(void) CPUMSetHyperDR7(PVMCPU pVCpu, RTGCUINTREG uDr7)
388{
389 pVCpu->cpum.s.Hyper.dr[7] = uDr7;
390#ifdef IN_RC
391 MAYBE_LOAD_DRx(pVCpu, ASMSetDR7, uDr7);
392#endif
393}
394
395
396VMMDECL(RTSEL) CPUMGetHyperCS(PVMCPU pVCpu)
397{
398 return pVCpu->cpum.s.Hyper.cs.Sel;
399}
400
401
402VMMDECL(RTSEL) CPUMGetHyperDS(PVMCPU pVCpu)
403{
404 return pVCpu->cpum.s.Hyper.ds.Sel;
405}
406
407
408VMMDECL(RTSEL) CPUMGetHyperES(PVMCPU pVCpu)
409{
410 return pVCpu->cpum.s.Hyper.es.Sel;
411}
412
413
414VMMDECL(RTSEL) CPUMGetHyperFS(PVMCPU pVCpu)
415{
416 return pVCpu->cpum.s.Hyper.fs.Sel;
417}
418
419
420VMMDECL(RTSEL) CPUMGetHyperGS(PVMCPU pVCpu)
421{
422 return pVCpu->cpum.s.Hyper.gs.Sel;
423}
424
425
426VMMDECL(RTSEL) CPUMGetHyperSS(PVMCPU pVCpu)
427{
428 return pVCpu->cpum.s.Hyper.ss.Sel;
429}
430
431
432VMMDECL(uint32_t) CPUMGetHyperEAX(PVMCPU pVCpu)
433{
434 return pVCpu->cpum.s.Hyper.eax;
435}
436
437
438VMMDECL(uint32_t) CPUMGetHyperEBX(PVMCPU pVCpu)
439{
440 return pVCpu->cpum.s.Hyper.ebx;
441}
442
443
444VMMDECL(uint32_t) CPUMGetHyperECX(PVMCPU pVCpu)
445{
446 return pVCpu->cpum.s.Hyper.ecx;
447}
448
449
450VMMDECL(uint32_t) CPUMGetHyperEDX(PVMCPU pVCpu)
451{
452 return pVCpu->cpum.s.Hyper.edx;
453}
454
455
456VMMDECL(uint32_t) CPUMGetHyperESI(PVMCPU pVCpu)
457{
458 return pVCpu->cpum.s.Hyper.esi;
459}
460
461
462VMMDECL(uint32_t) CPUMGetHyperEDI(PVMCPU pVCpu)
463{
464 return pVCpu->cpum.s.Hyper.edi;
465}
466
467
468VMMDECL(uint32_t) CPUMGetHyperEBP(PVMCPU pVCpu)
469{
470 return pVCpu->cpum.s.Hyper.ebp;
471}
472
473
474VMMDECL(uint32_t) CPUMGetHyperESP(PVMCPU pVCpu)
475{
476 return pVCpu->cpum.s.Hyper.esp;
477}
478
479
480VMMDECL(uint32_t) CPUMGetHyperEFlags(PVMCPU pVCpu)
481{
482 return pVCpu->cpum.s.Hyper.eflags.u32;
483}
484
485
486VMMDECL(uint32_t) CPUMGetHyperEIP(PVMCPU pVCpu)
487{
488 return pVCpu->cpum.s.Hyper.eip;
489}
490
491
492VMMDECL(uint64_t) CPUMGetHyperRIP(PVMCPU pVCpu)
493{
494 return pVCpu->cpum.s.Hyper.rip;
495}
496
497
498VMMDECL(uint32_t) CPUMGetHyperIDTR(PVMCPU pVCpu, uint16_t *pcbLimit)
499{
500 if (pcbLimit)
501 *pcbLimit = pVCpu->cpum.s.Hyper.idtr.cbIdt;
502 return pVCpu->cpum.s.Hyper.idtr.pIdt;
503}
504
505
506VMMDECL(uint32_t) CPUMGetHyperGDTR(PVMCPU pVCpu, uint16_t *pcbLimit)
507{
508 if (pcbLimit)
509 *pcbLimit = pVCpu->cpum.s.Hyper.gdtr.cbGdt;
510 return pVCpu->cpum.s.Hyper.gdtr.pGdt;
511}
512
513
514VMMDECL(RTSEL) CPUMGetHyperLDTR(PVMCPU pVCpu)
515{
516 return pVCpu->cpum.s.Hyper.ldtr.Sel;
517}
518
519
520VMMDECL(RTGCUINTREG) CPUMGetHyperDR0(PVMCPU pVCpu)
521{
522 return pVCpu->cpum.s.Hyper.dr[0];
523}
524
525
526VMMDECL(RTGCUINTREG) CPUMGetHyperDR1(PVMCPU pVCpu)
527{
528 return pVCpu->cpum.s.Hyper.dr[1];
529}
530
531
532VMMDECL(RTGCUINTREG) CPUMGetHyperDR2(PVMCPU pVCpu)
533{
534 return pVCpu->cpum.s.Hyper.dr[2];
535}
536
537
538VMMDECL(RTGCUINTREG) CPUMGetHyperDR3(PVMCPU pVCpu)
539{
540 return pVCpu->cpum.s.Hyper.dr[3];
541}
542
543
544VMMDECL(RTGCUINTREG) CPUMGetHyperDR6(PVMCPU pVCpu)
545{
546 return pVCpu->cpum.s.Hyper.dr[6];
547}
548
549
550VMMDECL(RTGCUINTREG) CPUMGetHyperDR7(PVMCPU pVCpu)
551{
552 return pVCpu->cpum.s.Hyper.dr[7];
553}
554
555
556/**
557 * Gets the pointer to the internal CPUMCTXCORE structure.
558 * This is only for reading in order to save a few calls.
559 *
560 * @param pVCpu The cross context virtual CPU structure.
561 */
562VMMDECL(PCCPUMCTXCORE) CPUMGetGuestCtxCore(PVMCPU pVCpu)
563{
564 return CPUMCTX2CORE(&pVCpu->cpum.s.Guest);
565}
566
567
568/**
569 * Queries the pointer to the internal CPUMCTX structure.
570 *
571 * @returns The CPUMCTX pointer.
572 * @param pVCpu The cross context virtual CPU structure.
573 */
574VMMDECL(PCPUMCTX) CPUMQueryGuestCtxPtr(PVMCPU pVCpu)
575{
576 return &pVCpu->cpum.s.Guest;
577}
578
579VMMDECL(int) CPUMSetGuestGDTR(PVMCPU pVCpu, uint64_t GCPtrBase, uint16_t cbLimit)
580{
581#ifdef VBOX_WITH_RAW_MODE_NOT_R0
582 if (VM_IS_RAW_MODE_ENABLED(pVCpu->CTX_SUFF(pVM)))
583 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_GDT);
584#endif
585 pVCpu->cpum.s.Guest.gdtr.cbGdt = cbLimit;
586 pVCpu->cpum.s.Guest.gdtr.pGdt = GCPtrBase;
587 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_GDTR;
588 return VINF_SUCCESS; /* formality, consider it void. */
589}
590
591VMMDECL(int) CPUMSetGuestIDTR(PVMCPU pVCpu, uint64_t GCPtrBase, uint16_t cbLimit)
592{
593#ifdef VBOX_WITH_RAW_MODE_NOT_R0
594 if (VM_IS_RAW_MODE_ENABLED(pVCpu->CTX_SUFF(pVM)))
595 VMCPU_FF_SET(pVCpu, VMCPU_FF_TRPM_SYNC_IDT);
596#endif
597 pVCpu->cpum.s.Guest.idtr.cbIdt = cbLimit;
598 pVCpu->cpum.s.Guest.idtr.pIdt = GCPtrBase;
599 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_IDTR;
600 return VINF_SUCCESS; /* formality, consider it void. */
601}
602
603VMMDECL(int) CPUMSetGuestTR(PVMCPU pVCpu, uint16_t tr)
604{
605#ifdef VBOX_WITH_RAW_MODE_NOT_R0
606 if (VM_IS_RAW_MODE_ENABLED(pVCpu->CTX_SUFF(pVM)))
607 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_TSS);
608#endif
609 pVCpu->cpum.s.Guest.tr.Sel = tr;
610 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_TR;
611 return VINF_SUCCESS; /* formality, consider it void. */
612}
613
614VMMDECL(int) CPUMSetGuestLDTR(PVMCPU pVCpu, uint16_t ldtr)
615{
616#ifdef VBOX_WITH_RAW_MODE_NOT_R0
617 if ( ( ldtr != 0
618 || pVCpu->cpum.s.Guest.ldtr.Sel != 0)
619 && VM_IS_RAW_MODE_ENABLED(pVCpu->CTX_SUFF(pVM)))
620 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_LDT);
621#endif
622 pVCpu->cpum.s.Guest.ldtr.Sel = ldtr;
623 /* The caller will set more hidden bits if it has them. */
624 pVCpu->cpum.s.Guest.ldtr.ValidSel = 0;
625 pVCpu->cpum.s.Guest.ldtr.fFlags = 0;
626 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_LDTR;
627 return VINF_SUCCESS; /* formality, consider it void. */
628}
629
630
631/**
632 * Set the guest CR0.
633 *
634 * When called in GC, the hyper CR0 may be updated if that is
635 * required. The caller only has to take special action if AM,
636 * WP, PG or PE changes.
637 *
638 * @returns VINF_SUCCESS (consider it void).
639 * @param pVCpu The cross context virtual CPU structure.
640 * @param cr0 The new CR0 value.
641 */
642VMMDECL(int) CPUMSetGuestCR0(PVMCPU pVCpu, uint64_t cr0)
643{
644#ifdef IN_RC
645 /*
646 * Check if we need to change hypervisor CR0 because
647 * of math stuff.
648 */
649 if ( (cr0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP))
650 != (pVCpu->cpum.s.Guest.cr0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP)))
651 {
652 if (!(pVCpu->cpum.s.fUseFlags & CPUM_USED_FPU_GUEST))
653 {
654 /*
655 * We haven't loaded the guest FPU state yet, so TS and MT are both set
656 * and EM should be reflecting the guest EM (it always does this).
657 */
658 if ((cr0 & X86_CR0_EM) != (pVCpu->cpum.s.Guest.cr0 & X86_CR0_EM))
659 {
660 uint32_t HyperCR0 = ASMGetCR0();
661 AssertMsg((HyperCR0 & (X86_CR0_TS | X86_CR0_MP)) == (X86_CR0_TS | X86_CR0_MP), ("%#x\n", HyperCR0));
662 AssertMsg((HyperCR0 & X86_CR0_EM) == (pVCpu->cpum.s.Guest.cr0 & X86_CR0_EM), ("%#x\n", HyperCR0));
663 HyperCR0 &= ~X86_CR0_EM;
664 HyperCR0 |= cr0 & X86_CR0_EM;
665 Log(("CPUM: New HyperCR0=%#x\n", HyperCR0));
666 ASMSetCR0(HyperCR0);
667 }
668# ifdef VBOX_STRICT
669 else
670 {
671 uint32_t HyperCR0 = ASMGetCR0();
672 AssertMsg((HyperCR0 & (X86_CR0_TS | X86_CR0_MP)) == (X86_CR0_TS | X86_CR0_MP), ("%#x\n", HyperCR0));
673 AssertMsg((HyperCR0 & X86_CR0_EM) == (pVCpu->cpum.s.Guest.cr0 & X86_CR0_EM), ("%#x\n", HyperCR0));
674 }
675# endif
676 }
677 else
678 {
679 /*
680 * Already loaded the guest FPU state, so we're just mirroring
681 * the guest flags.
682 */
683 uint32_t HyperCR0 = ASMGetCR0();
684 AssertMsg( (HyperCR0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP))
685 == (pVCpu->cpum.s.Guest.cr0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP)),
686 ("%#x %#x\n", HyperCR0, pVCpu->cpum.s.Guest.cr0));
687 HyperCR0 &= ~(X86_CR0_TS | X86_CR0_EM | X86_CR0_MP);
688 HyperCR0 |= cr0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP);
689 Log(("CPUM: New HyperCR0=%#x\n", HyperCR0));
690 ASMSetCR0(HyperCR0);
691 }
692 }
693#endif /* IN_RC */
694
695 /*
696 * Check for changes causing TLB flushes (for REM).
697 * The caller is responsible for calling PGM when appropriate.
698 */
699 if ( (cr0 & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE))
700 != (pVCpu->cpum.s.Guest.cr0 & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE)))
701 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_GLOBAL_TLB_FLUSH;
702 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_CR0;
703
704 /*
705 * Let PGM know if the WP goes from 0 to 1 (netware WP0+RO+US hack)
706 */
707 if (((cr0 ^ pVCpu->cpum.s.Guest.cr0) & X86_CR0_WP) && (cr0 & X86_CR0_WP))
708 PGMCr0WpEnabled(pVCpu);
709
710 /* The ET flag is settable on a 386 and hardwired on 486+. */
711 if ( !(cr0 & X86_CR0_ET)
712 && pVCpu->CTX_SUFF(pVM)->cpum.s.GuestFeatures.enmMicroarch != kCpumMicroarch_Intel_80386)
713 cr0 |= X86_CR0_ET;
714
715 pVCpu->cpum.s.Guest.cr0 = cr0;
716 return VINF_SUCCESS;
717}
718
719
720VMMDECL(int) CPUMSetGuestCR2(PVMCPU pVCpu, uint64_t cr2)
721{
722 pVCpu->cpum.s.Guest.cr2 = cr2;
723 return VINF_SUCCESS;
724}
725
726
727VMMDECL(int) CPUMSetGuestCR3(PVMCPU pVCpu, uint64_t cr3)
728{
729 pVCpu->cpum.s.Guest.cr3 = cr3;
730 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_CR3;
731 return VINF_SUCCESS;
732}
733
734
735VMMDECL(int) CPUMSetGuestCR4(PVMCPU pVCpu, uint64_t cr4)
736{
737 /* Note! We don't bother with OSXSAVE and legacy CPUID patches. */
738
739 if ( (cr4 & (X86_CR4_PGE | X86_CR4_PAE | X86_CR4_PSE))
740 != (pVCpu->cpum.s.Guest.cr4 & (X86_CR4_PGE | X86_CR4_PAE | X86_CR4_PSE)))
741 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_GLOBAL_TLB_FLUSH;
742
743 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_CR4;
744 pVCpu->cpum.s.Guest.cr4 = cr4;
745 return VINF_SUCCESS;
746}
747
748
749VMMDECL(int) CPUMSetGuestEFlags(PVMCPU pVCpu, uint32_t eflags)
750{
751 pVCpu->cpum.s.Guest.eflags.u32 = eflags;
752 return VINF_SUCCESS;
753}
754
755
756VMMDECL(int) CPUMSetGuestEIP(PVMCPU pVCpu, uint32_t eip)
757{
758 pVCpu->cpum.s.Guest.eip = eip;
759 return VINF_SUCCESS;
760}
761
762
763VMMDECL(int) CPUMSetGuestEAX(PVMCPU pVCpu, uint32_t eax)
764{
765 pVCpu->cpum.s.Guest.eax = eax;
766 return VINF_SUCCESS;
767}
768
769
770VMMDECL(int) CPUMSetGuestEBX(PVMCPU pVCpu, uint32_t ebx)
771{
772 pVCpu->cpum.s.Guest.ebx = ebx;
773 return VINF_SUCCESS;
774}
775
776
777VMMDECL(int) CPUMSetGuestECX(PVMCPU pVCpu, uint32_t ecx)
778{
779 pVCpu->cpum.s.Guest.ecx = ecx;
780 return VINF_SUCCESS;
781}
782
783
784VMMDECL(int) CPUMSetGuestEDX(PVMCPU pVCpu, uint32_t edx)
785{
786 pVCpu->cpum.s.Guest.edx = edx;
787 return VINF_SUCCESS;
788}
789
790
791VMMDECL(int) CPUMSetGuestESP(PVMCPU pVCpu, uint32_t esp)
792{
793 pVCpu->cpum.s.Guest.esp = esp;
794 return VINF_SUCCESS;
795}
796
797
798VMMDECL(int) CPUMSetGuestEBP(PVMCPU pVCpu, uint32_t ebp)
799{
800 pVCpu->cpum.s.Guest.ebp = ebp;
801 return VINF_SUCCESS;
802}
803
804
805VMMDECL(int) CPUMSetGuestESI(PVMCPU pVCpu, uint32_t esi)
806{
807 pVCpu->cpum.s.Guest.esi = esi;
808 return VINF_SUCCESS;
809}
810
811
812VMMDECL(int) CPUMSetGuestEDI(PVMCPU pVCpu, uint32_t edi)
813{
814 pVCpu->cpum.s.Guest.edi = edi;
815 return VINF_SUCCESS;
816}
817
818
819VMMDECL(int) CPUMSetGuestSS(PVMCPU pVCpu, uint16_t ss)
820{
821 pVCpu->cpum.s.Guest.ss.Sel = ss;
822 return VINF_SUCCESS;
823}
824
825
826VMMDECL(int) CPUMSetGuestCS(PVMCPU pVCpu, uint16_t cs)
827{
828 pVCpu->cpum.s.Guest.cs.Sel = cs;
829 return VINF_SUCCESS;
830}
831
832
833VMMDECL(int) CPUMSetGuestDS(PVMCPU pVCpu, uint16_t ds)
834{
835 pVCpu->cpum.s.Guest.ds.Sel = ds;
836 return VINF_SUCCESS;
837}
838
839
840VMMDECL(int) CPUMSetGuestES(PVMCPU pVCpu, uint16_t es)
841{
842 pVCpu->cpum.s.Guest.es.Sel = es;
843 return VINF_SUCCESS;
844}
845
846
847VMMDECL(int) CPUMSetGuestFS(PVMCPU pVCpu, uint16_t fs)
848{
849 pVCpu->cpum.s.Guest.fs.Sel = fs;
850 return VINF_SUCCESS;
851}
852
853
854VMMDECL(int) CPUMSetGuestGS(PVMCPU pVCpu, uint16_t gs)
855{
856 pVCpu->cpum.s.Guest.gs.Sel = gs;
857 return VINF_SUCCESS;
858}
859
860
861VMMDECL(void) CPUMSetGuestEFER(PVMCPU pVCpu, uint64_t val)
862{
863 pVCpu->cpum.s.Guest.msrEFER = val;
864}
865
866
867VMMDECL(RTGCPTR) CPUMGetGuestIDTR(PVMCPU pVCpu, uint16_t *pcbLimit)
868{
869 if (pcbLimit)
870 *pcbLimit = pVCpu->cpum.s.Guest.idtr.cbIdt;
871 return pVCpu->cpum.s.Guest.idtr.pIdt;
872}
873
874
875VMMDECL(RTSEL) CPUMGetGuestTR(PVMCPU pVCpu, PCPUMSELREGHID pHidden)
876{
877 if (pHidden)
878 *pHidden = pVCpu->cpum.s.Guest.tr;
879 return pVCpu->cpum.s.Guest.tr.Sel;
880}
881
882
883VMMDECL(RTSEL) CPUMGetGuestCS(PVMCPU pVCpu)
884{
885 return pVCpu->cpum.s.Guest.cs.Sel;
886}
887
888
889VMMDECL(RTSEL) CPUMGetGuestDS(PVMCPU pVCpu)
890{
891 return pVCpu->cpum.s.Guest.ds.Sel;
892}
893
894
895VMMDECL(RTSEL) CPUMGetGuestES(PVMCPU pVCpu)
896{
897 return pVCpu->cpum.s.Guest.es.Sel;
898}
899
900
901VMMDECL(RTSEL) CPUMGetGuestFS(PVMCPU pVCpu)
902{
903 return pVCpu->cpum.s.Guest.fs.Sel;
904}
905
906
907VMMDECL(RTSEL) CPUMGetGuestGS(PVMCPU pVCpu)
908{
909 return pVCpu->cpum.s.Guest.gs.Sel;
910}
911
912
913VMMDECL(RTSEL) CPUMGetGuestSS(PVMCPU pVCpu)
914{
915 return pVCpu->cpum.s.Guest.ss.Sel;
916}
917
918
919VMMDECL(uint64_t) CPUMGetGuestFlatPC(PVMCPU pVCpu)
920{
921 CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(pVCpu, &pVCpu->cpum.s.Guest.cs);
922 if ( !CPUMIsGuestInLongMode(pVCpu)
923 || pVCpu->cpum.s.Guest.cs.Attr.n.u1Long)
924 return pVCpu->cpum.s.Guest.eip + (uint32_t)pVCpu->cpum.s.Guest.cs.u64Base;
925 return pVCpu->cpum.s.Guest.rip + pVCpu->cpum.s.Guest.cs.u64Base;
926}
927
928
929VMMDECL(uint64_t) CPUMGetGuestFlatSP(PVMCPU pVCpu)
930{
931 CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(pVCpu, &pVCpu->cpum.s.Guest.ss);
932 if ( !CPUMIsGuestInLongMode(pVCpu)
933 || pVCpu->cpum.s.Guest.ss.Attr.n.u1Long)
934 return pVCpu->cpum.s.Guest.eip + (uint32_t)pVCpu->cpum.s.Guest.ss.u64Base;
935 return pVCpu->cpum.s.Guest.rip + pVCpu->cpum.s.Guest.ss.u64Base;
936}
937
938
939VMMDECL(RTSEL) CPUMGetGuestLDTR(PVMCPU pVCpu)
940{
941 return pVCpu->cpum.s.Guest.ldtr.Sel;
942}
943
944
945VMMDECL(RTSEL) CPUMGetGuestLdtrEx(PVMCPU pVCpu, uint64_t *pGCPtrBase, uint32_t *pcbLimit)
946{
947 *pGCPtrBase = pVCpu->cpum.s.Guest.ldtr.u64Base;
948 *pcbLimit = pVCpu->cpum.s.Guest.ldtr.u32Limit;
949 return pVCpu->cpum.s.Guest.ldtr.Sel;
950}
951
952
953VMMDECL(uint64_t) CPUMGetGuestCR0(PVMCPU pVCpu)
954{
955 return pVCpu->cpum.s.Guest.cr0;
956}
957
958
959VMMDECL(uint64_t) CPUMGetGuestCR2(PVMCPU pVCpu)
960{
961 return pVCpu->cpum.s.Guest.cr2;
962}
963
964
965VMMDECL(uint64_t) CPUMGetGuestCR3(PVMCPU pVCpu)
966{
967 return pVCpu->cpum.s.Guest.cr3;
968}
969
970
971VMMDECL(uint64_t) CPUMGetGuestCR4(PVMCPU pVCpu)
972{
973 return pVCpu->cpum.s.Guest.cr4;
974}
975
976
977VMMDECL(uint64_t) CPUMGetGuestCR8(PVMCPU pVCpu)
978{
979 uint64_t u64;
980 int rc = CPUMGetGuestCRx(pVCpu, DISCREG_CR8, &u64);
981 if (RT_FAILURE(rc))
982 u64 = 0;
983 return u64;
984}
985
986
987VMMDECL(void) CPUMGetGuestGDTR(PVMCPU pVCpu, PVBOXGDTR pGDTR)
988{
989 *pGDTR = pVCpu->cpum.s.Guest.gdtr;
990}
991
992
993VMMDECL(uint32_t) CPUMGetGuestEIP(PVMCPU pVCpu)
994{
995 return pVCpu->cpum.s.Guest.eip;
996}
997
998
999VMMDECL(uint64_t) CPUMGetGuestRIP(PVMCPU pVCpu)
1000{
1001 return pVCpu->cpum.s.Guest.rip;
1002}
1003
1004
1005VMMDECL(uint32_t) CPUMGetGuestEAX(PVMCPU pVCpu)
1006{
1007 return pVCpu->cpum.s.Guest.eax;
1008}
1009
1010
1011VMMDECL(uint32_t) CPUMGetGuestEBX(PVMCPU pVCpu)
1012{
1013 return pVCpu->cpum.s.Guest.ebx;
1014}
1015
1016
1017VMMDECL(uint32_t) CPUMGetGuestECX(PVMCPU pVCpu)
1018{
1019 return pVCpu->cpum.s.Guest.ecx;
1020}
1021
1022
1023VMMDECL(uint32_t) CPUMGetGuestEDX(PVMCPU pVCpu)
1024{
1025 return pVCpu->cpum.s.Guest.edx;
1026}
1027
1028
1029VMMDECL(uint32_t) CPUMGetGuestESI(PVMCPU pVCpu)
1030{
1031 return pVCpu->cpum.s.Guest.esi;
1032}
1033
1034
1035VMMDECL(uint32_t) CPUMGetGuestEDI(PVMCPU pVCpu)
1036{
1037 return pVCpu->cpum.s.Guest.edi;
1038}
1039
1040
1041VMMDECL(uint32_t) CPUMGetGuestESP(PVMCPU pVCpu)
1042{
1043 return pVCpu->cpum.s.Guest.esp;
1044}
1045
1046
1047VMMDECL(uint32_t) CPUMGetGuestEBP(PVMCPU pVCpu)
1048{
1049 return pVCpu->cpum.s.Guest.ebp;
1050}
1051
1052
1053VMMDECL(uint32_t) CPUMGetGuestEFlags(PVMCPU pVCpu)
1054{
1055 return pVCpu->cpum.s.Guest.eflags.u32;
1056}
1057
1058
1059VMMDECL(int) CPUMGetGuestCRx(PVMCPU pVCpu, unsigned iReg, uint64_t *pValue)
1060{
1061 switch (iReg)
1062 {
1063 case DISCREG_CR0:
1064 *pValue = pVCpu->cpum.s.Guest.cr0;
1065 break;
1066
1067 case DISCREG_CR2:
1068 *pValue = pVCpu->cpum.s.Guest.cr2;
1069 break;
1070
1071 case DISCREG_CR3:
1072 *pValue = pVCpu->cpum.s.Guest.cr3;
1073 break;
1074
1075 case DISCREG_CR4:
1076 *pValue = pVCpu->cpum.s.Guest.cr4;
1077 break;
1078
1079 case DISCREG_CR8:
1080 {
1081 uint8_t u8Tpr;
1082 int rc = APICGetTpr(pVCpu, &u8Tpr, NULL /* pfPending */, NULL /* pu8PendingIrq */);
1083 if (RT_FAILURE(rc))
1084 {
1085 AssertMsg(rc == VERR_PDM_NO_APIC_INSTANCE, ("%Rrc\n", rc));
1086 *pValue = 0;
1087 return rc;
1088 }
1089 *pValue = u8Tpr >> 4; /* bits 7-4 contain the task priority that go in cr8, bits 3-0 */
1090 break;
1091 }
1092
1093 default:
1094 return VERR_INVALID_PARAMETER;
1095 }
1096 return VINF_SUCCESS;
1097}
1098
1099
1100VMMDECL(uint64_t) CPUMGetGuestDR0(PVMCPU pVCpu)
1101{
1102 return pVCpu->cpum.s.Guest.dr[0];
1103}
1104
1105
1106VMMDECL(uint64_t) CPUMGetGuestDR1(PVMCPU pVCpu)
1107{
1108 return pVCpu->cpum.s.Guest.dr[1];
1109}
1110
1111
1112VMMDECL(uint64_t) CPUMGetGuestDR2(PVMCPU pVCpu)
1113{
1114 return pVCpu->cpum.s.Guest.dr[2];
1115}
1116
1117
1118VMMDECL(uint64_t) CPUMGetGuestDR3(PVMCPU pVCpu)
1119{
1120 return pVCpu->cpum.s.Guest.dr[3];
1121}
1122
1123
1124VMMDECL(uint64_t) CPUMGetGuestDR6(PVMCPU pVCpu)
1125{
1126 return pVCpu->cpum.s.Guest.dr[6];
1127}
1128
1129
1130VMMDECL(uint64_t) CPUMGetGuestDR7(PVMCPU pVCpu)
1131{
1132 return pVCpu->cpum.s.Guest.dr[7];
1133}
1134
1135
1136VMMDECL(int) CPUMGetGuestDRx(PVMCPU pVCpu, uint32_t iReg, uint64_t *pValue)
1137{
1138 AssertReturn(iReg <= DISDREG_DR7, VERR_INVALID_PARAMETER);
1139 /* DR4 is an alias for DR6, and DR5 is an alias for DR7. */
1140 if (iReg == 4 || iReg == 5)
1141 iReg += 2;
1142 *pValue = pVCpu->cpum.s.Guest.dr[iReg];
1143 return VINF_SUCCESS;
1144}
1145
1146
1147VMMDECL(uint64_t) CPUMGetGuestEFER(PVMCPU pVCpu)
1148{
1149 return pVCpu->cpum.s.Guest.msrEFER;
1150}
1151
1152
1153/**
1154 * Looks up a CPUID leaf in the CPUID leaf array, no subleaf.
1155 *
1156 * @returns Pointer to the leaf if found, NULL if not.
1157 *
1158 * @param pVM The cross context VM structure.
1159 * @param uLeaf The leaf to get.
1160 */
1161PCPUMCPUIDLEAF cpumCpuIdGetLeaf(PVM pVM, uint32_t uLeaf)
1162{
1163 unsigned iEnd = pVM->cpum.s.GuestInfo.cCpuIdLeaves;
1164 if (iEnd)
1165 {
1166 unsigned iStart = 0;
1167 PCPUMCPUIDLEAF paLeaves = pVM->cpum.s.GuestInfo.CTX_SUFF(paCpuIdLeaves);
1168 for (;;)
1169 {
1170 unsigned i = iStart + (iEnd - iStart) / 2U;
1171 if (uLeaf < paLeaves[i].uLeaf)
1172 {
1173 if (i <= iStart)
1174 return NULL;
1175 iEnd = i;
1176 }
1177 else if (uLeaf > paLeaves[i].uLeaf)
1178 {
1179 i += 1;
1180 if (i >= iEnd)
1181 return NULL;
1182 iStart = i;
1183 }
1184 else
1185 {
1186 if (RT_LIKELY(paLeaves[i].fSubLeafMask == 0 && paLeaves[i].uSubLeaf == 0))
1187 return &paLeaves[i];
1188
1189 /* This shouldn't normally happen. But in case the it does due
1190 to user configuration overrids or something, just return the
1191 first sub-leaf. */
1192 AssertMsgFailed(("uLeaf=%#x fSubLeafMask=%#x uSubLeaf=%#x\n",
1193 uLeaf, paLeaves[i].fSubLeafMask, paLeaves[i].uSubLeaf));
1194 while ( paLeaves[i].uSubLeaf != 0
1195 && i > 0
1196 && uLeaf == paLeaves[i - 1].uLeaf)
1197 i--;
1198 return &paLeaves[i];
1199 }
1200 }
1201 }
1202
1203 return NULL;
1204}
1205
1206
1207/**
1208 * Looks up a CPUID leaf in the CPUID leaf array.
1209 *
1210 * @returns Pointer to the leaf if found, NULL if not.
1211 *
1212 * @param pVM The cross context VM structure.
1213 * @param uLeaf The leaf to get.
1214 * @param uSubLeaf The subleaf, if applicable. Just pass 0 if it
1215 * isn't.
1216 * @param pfExactSubLeafHit Whether we've got an exact subleaf hit or not.
1217 */
1218PCPUMCPUIDLEAF cpumCpuIdGetLeafEx(PVM pVM, uint32_t uLeaf, uint32_t uSubLeaf, bool *pfExactSubLeafHit)
1219{
1220 unsigned iEnd = pVM->cpum.s.GuestInfo.cCpuIdLeaves;
1221 if (iEnd)
1222 {
1223 unsigned iStart = 0;
1224 PCPUMCPUIDLEAF paLeaves = pVM->cpum.s.GuestInfo.CTX_SUFF(paCpuIdLeaves);
1225 for (;;)
1226 {
1227 unsigned i = iStart + (iEnd - iStart) / 2U;
1228 if (uLeaf < paLeaves[i].uLeaf)
1229 {
1230 if (i <= iStart)
1231 return NULL;
1232 iEnd = i;
1233 }
1234 else if (uLeaf > paLeaves[i].uLeaf)
1235 {
1236 i += 1;
1237 if (i >= iEnd)
1238 return NULL;
1239 iStart = i;
1240 }
1241 else
1242 {
1243 uSubLeaf &= paLeaves[i].fSubLeafMask;
1244 if (uSubLeaf == paLeaves[i].uSubLeaf)
1245 *pfExactSubLeafHit = true;
1246 else
1247 {
1248 /* Find the right subleaf. We return the last one before
1249 uSubLeaf if we don't find an exact match. */
1250 if (uSubLeaf < paLeaves[i].uSubLeaf)
1251 while ( i > 0
1252 && uLeaf == paLeaves[i - 1].uLeaf
1253 && uSubLeaf <= paLeaves[i - 1].uSubLeaf)
1254 i--;
1255 else
1256 while ( i + 1 < pVM->cpum.s.GuestInfo.cCpuIdLeaves
1257 && uLeaf == paLeaves[i + 1].uLeaf
1258 && uSubLeaf >= paLeaves[i + 1].uSubLeaf)
1259 i++;
1260 *pfExactSubLeafHit = uSubLeaf == paLeaves[i].uSubLeaf;
1261 }
1262 return &paLeaves[i];
1263 }
1264 }
1265 }
1266
1267 *pfExactSubLeafHit = false;
1268 return NULL;
1269}
1270
1271
1272/**
1273 * Gets a CPUID leaf.
1274 *
1275 * @param pVCpu The cross context virtual CPU structure.
1276 * @param uLeaf The CPUID leaf to get.
1277 * @param uSubLeaf The CPUID sub-leaf to get, if applicable.
1278 * @param pEax Where to store the EAX value.
1279 * @param pEbx Where to store the EBX value.
1280 * @param pEcx Where to store the ECX value.
1281 * @param pEdx Where to store the EDX value.
1282 */
1283VMMDECL(void) CPUMGetGuestCpuId(PVMCPU pVCpu, uint32_t uLeaf, uint32_t uSubLeaf,
1284 uint32_t *pEax, uint32_t *pEbx, uint32_t *pEcx, uint32_t *pEdx)
1285{
1286 bool fExactSubLeafHit;
1287 PVM pVM = pVCpu->CTX_SUFF(pVM);
1288 PCCPUMCPUIDLEAF pLeaf = cpumCpuIdGetLeafEx(pVM, uLeaf, uSubLeaf, &fExactSubLeafHit);
1289 if (pLeaf)
1290 {
1291 AssertMsg(pLeaf->uLeaf == uLeaf, ("%#x %#x\n", pLeaf->uLeaf, uLeaf));
1292 if (fExactSubLeafHit)
1293 {
1294 *pEax = pLeaf->uEax;
1295 *pEbx = pLeaf->uEbx;
1296 *pEcx = pLeaf->uEcx;
1297 *pEdx = pLeaf->uEdx;
1298
1299 /*
1300 * Deal with CPU specific information.
1301 */
1302 if (pLeaf->fFlags & ( CPUMCPUIDLEAF_F_CONTAINS_APIC_ID
1303 | CPUMCPUIDLEAF_F_CONTAINS_OSXSAVE
1304 | CPUMCPUIDLEAF_F_CONTAINS_APIC ))
1305 {
1306 if (uLeaf == 1)
1307 {
1308 /* EBX: Bits 31-24: Initial APIC ID. */
1309 Assert(pVCpu->idCpu <= 255);
1310 AssertMsg((pLeaf->uEbx >> 24) == 0, ("%#x\n", pLeaf->uEbx)); /* raw-mode assumption */
1311 *pEbx = (pLeaf->uEbx & UINT32_C(0x00ffffff)) | (pVCpu->idCpu << 24);
1312
1313 /* EDX: Bit 9: AND with APICBASE.EN. */
1314 if (!pVCpu->cpum.s.fCpuIdApicFeatureVisible && (pLeaf->fFlags & CPUMCPUIDLEAF_F_CONTAINS_APIC))
1315 *pEdx &= ~X86_CPUID_FEATURE_EDX_APIC;
1316
1317 /* ECX: Bit 27: CR4.OSXSAVE mirror. */
1318 *pEcx = (pLeaf->uEcx & ~X86_CPUID_FEATURE_ECX_OSXSAVE)
1319 | (pVCpu->cpum.s.Guest.cr4 & X86_CR4_OSXSAVE ? X86_CPUID_FEATURE_ECX_OSXSAVE : 0);
1320 }
1321 else if (uLeaf == 0xb)
1322 {
1323 /* EDX: Initial extended APIC ID. */
1324 AssertMsg(pLeaf->uEdx == 0, ("%#x\n", pLeaf->uEdx)); /* raw-mode assumption */
1325 *pEdx = pVCpu->idCpu;
1326 Assert(!(pLeaf->fFlags & ~(CPUMCPUIDLEAF_F_CONTAINS_APIC_ID | CPUMCPUIDLEAF_F_INTEL_TOPOLOGY_SUBLEAVES)));
1327 }
1328 else if (uLeaf == UINT32_C(0x8000001e))
1329 {
1330 /* EAX: Initial extended APIC ID. */
1331 AssertMsg(pLeaf->uEax == 0, ("%#x\n", pLeaf->uEax)); /* raw-mode assumption */
1332 *pEax = pVCpu->idCpu;
1333 Assert(!(pLeaf->fFlags & ~CPUMCPUIDLEAF_F_CONTAINS_APIC_ID));
1334 }
1335 else if (uLeaf == UINT32_C(0x80000001))
1336 {
1337 /* EDX: Bit 9: AND with APICBASE.EN. */
1338 if (!pVCpu->cpum.s.fCpuIdApicFeatureVisible)
1339 *pEdx &= ~X86_CPUID_AMD_FEATURE_EDX_APIC;
1340 Assert(!(pLeaf->fFlags & ~CPUMCPUIDLEAF_F_CONTAINS_APIC));
1341 }
1342 else
1343 AssertMsgFailed(("uLeaf=%#x\n", uLeaf));
1344 }
1345 }
1346 /*
1347 * Out of range sub-leaves aren't quite as easy and pretty as we emulate
1348 * them here, but we do the best we can here...
1349 */
1350 else
1351 {
1352 *pEax = *pEbx = *pEcx = *pEdx = 0;
1353 if (pLeaf->fFlags & CPUMCPUIDLEAF_F_INTEL_TOPOLOGY_SUBLEAVES)
1354 {
1355 *pEcx = uSubLeaf & 0xff;
1356 *pEdx = pVCpu->idCpu;
1357 }
1358 }
1359 }
1360 else
1361 {
1362 /*
1363 * Different CPUs have different ways of dealing with unknown CPUID leaves.
1364 */
1365 switch (pVM->cpum.s.GuestInfo.enmUnknownCpuIdMethod)
1366 {
1367 default:
1368 AssertFailed();
1369 RT_FALL_THRU();
1370 case CPUMUNKNOWNCPUID_DEFAULTS:
1371 case CPUMUNKNOWNCPUID_LAST_STD_LEAF: /* ASSUME this is executed */
1372 case CPUMUNKNOWNCPUID_LAST_STD_LEAF_WITH_ECX: /** @todo Implement CPUMUNKNOWNCPUID_LAST_STD_LEAF_WITH_ECX */
1373 *pEax = pVM->cpum.s.GuestInfo.DefCpuId.uEax;
1374 *pEbx = pVM->cpum.s.GuestInfo.DefCpuId.uEbx;
1375 *pEcx = pVM->cpum.s.GuestInfo.DefCpuId.uEcx;
1376 *pEdx = pVM->cpum.s.GuestInfo.DefCpuId.uEdx;
1377 break;
1378 case CPUMUNKNOWNCPUID_PASSTHRU:
1379 *pEax = uLeaf;
1380 *pEbx = 0;
1381 *pEcx = uSubLeaf;
1382 *pEdx = 0;
1383 break;
1384 }
1385 }
1386 Log2(("CPUMGetGuestCpuId: uLeaf=%#010x/%#010x %RX32 %RX32 %RX32 %RX32\n", uLeaf, uSubLeaf, *pEax, *pEbx, *pEcx, *pEdx));
1387}
1388
1389
1390/**
1391 * Sets the visibility of the X86_CPUID_FEATURE_EDX_APIC and
1392 * X86_CPUID_AMD_FEATURE_EDX_APIC CPUID bits.
1393 *
1394 * @returns Previous value.
1395 * @param pVCpu The cross context virtual CPU structure to make the
1396 * change on. Usually the calling EMT.
1397 * @param fVisible Whether to make it visible (true) or hide it (false).
1398 *
1399 * @remarks This is "VMMDECL" so that it still links with
1400 * the old APIC code which is in VBoxDD2 and not in
1401 * the VMM module.
1402 */
1403VMMDECL(bool) CPUMSetGuestCpuIdPerCpuApicFeature(PVMCPU pVCpu, bool fVisible)
1404{
1405 bool fOld = pVCpu->cpum.s.fCpuIdApicFeatureVisible;
1406 pVCpu->cpum.s.fCpuIdApicFeatureVisible = fVisible;
1407
1408#ifdef VBOX_WITH_RAW_MODE_NOT_R0
1409 /*
1410 * Patch manager saved state legacy pain.
1411 */
1412 PVM pVM = pVCpu->CTX_SUFF(pVM);
1413 PCPUMCPUIDLEAF pLeaf = cpumCpuIdGetLeaf(pVM, UINT32_C(0x00000001));
1414 if (pLeaf)
1415 {
1416 if (fVisible || (pLeaf->fFlags & CPUMCPUIDLEAF_F_CONTAINS_APIC))
1417 pVM->cpum.s.aGuestCpuIdPatmStd[1].uEdx = pLeaf->uEdx;
1418 else
1419 pVM->cpum.s.aGuestCpuIdPatmStd[1].uEdx = pLeaf->uEdx & ~X86_CPUID_FEATURE_EDX_APIC;
1420 }
1421
1422 pLeaf = cpumCpuIdGetLeaf(pVM, UINT32_C(0x80000001));
1423 if (pLeaf)
1424 {
1425 if (fVisible || (pLeaf->fFlags & CPUMCPUIDLEAF_F_CONTAINS_APIC))
1426 pVM->cpum.s.aGuestCpuIdPatmExt[1].uEdx = pLeaf->uEdx;
1427 else
1428 pVM->cpum.s.aGuestCpuIdPatmExt[1].uEdx = pLeaf->uEdx & ~X86_CPUID_AMD_FEATURE_EDX_APIC;
1429 }
1430#endif
1431
1432 return fOld;
1433}
1434
1435
1436/**
1437 * Gets the host CPU vendor.
1438 *
1439 * @returns CPU vendor.
1440 * @param pVM The cross context VM structure.
1441 */
1442VMMDECL(CPUMCPUVENDOR) CPUMGetHostCpuVendor(PVM pVM)
1443{
1444 return (CPUMCPUVENDOR)pVM->cpum.s.HostFeatures.enmCpuVendor;
1445}
1446
1447
1448/**
1449 * Gets the CPU vendor.
1450 *
1451 * @returns CPU vendor.
1452 * @param pVM The cross context VM structure.
1453 */
1454VMMDECL(CPUMCPUVENDOR) CPUMGetGuestCpuVendor(PVM pVM)
1455{
1456 return (CPUMCPUVENDOR)pVM->cpum.s.GuestFeatures.enmCpuVendor;
1457}
1458
1459
1460VMMDECL(int) CPUMSetGuestDR0(PVMCPU pVCpu, uint64_t uDr0)
1461{
1462 pVCpu->cpum.s.Guest.dr[0] = uDr0;
1463 return CPUMRecalcHyperDRx(pVCpu, 0, false);
1464}
1465
1466
1467VMMDECL(int) CPUMSetGuestDR1(PVMCPU pVCpu, uint64_t uDr1)
1468{
1469 pVCpu->cpum.s.Guest.dr[1] = uDr1;
1470 return CPUMRecalcHyperDRx(pVCpu, 1, false);
1471}
1472
1473
1474VMMDECL(int) CPUMSetGuestDR2(PVMCPU pVCpu, uint64_t uDr2)
1475{
1476 pVCpu->cpum.s.Guest.dr[2] = uDr2;
1477 return CPUMRecalcHyperDRx(pVCpu, 2, false);
1478}
1479
1480
1481VMMDECL(int) CPUMSetGuestDR3(PVMCPU pVCpu, uint64_t uDr3)
1482{
1483 pVCpu->cpum.s.Guest.dr[3] = uDr3;
1484 return CPUMRecalcHyperDRx(pVCpu, 3, false);
1485}
1486
1487
1488VMMDECL(int) CPUMSetGuestDR6(PVMCPU pVCpu, uint64_t uDr6)
1489{
1490 pVCpu->cpum.s.Guest.dr[6] = uDr6;
1491 return VINF_SUCCESS; /* No need to recalc. */
1492}
1493
1494
1495VMMDECL(int) CPUMSetGuestDR7(PVMCPU pVCpu, uint64_t uDr7)
1496{
1497 pVCpu->cpum.s.Guest.dr[7] = uDr7;
1498 return CPUMRecalcHyperDRx(pVCpu, 7, false);
1499}
1500
1501
1502VMMDECL(int) CPUMSetGuestDRx(PVMCPU pVCpu, uint32_t iReg, uint64_t Value)
1503{
1504 AssertReturn(iReg <= DISDREG_DR7, VERR_INVALID_PARAMETER);
1505 /* DR4 is an alias for DR6, and DR5 is an alias for DR7. */
1506 if (iReg == 4 || iReg == 5)
1507 iReg += 2;
1508 pVCpu->cpum.s.Guest.dr[iReg] = Value;
1509 return CPUMRecalcHyperDRx(pVCpu, iReg, false);
1510}
1511
1512
1513/**
1514 * Recalculates the hypervisor DRx register values based on current guest
1515 * registers and DBGF breakpoints, updating changed registers depending on the
1516 * context.
1517 *
1518 * This is called whenever a guest DRx register is modified (any context) and
1519 * when DBGF sets a hardware breakpoint (ring-3 only, rendezvous).
1520 *
1521 * In raw-mode context this function will reload any (hyper) DRx registers which
1522 * comes out with a different value. It may also have to save the host debug
1523 * registers if that haven't been done already. In this context though, we'll
1524 * be intercepting and emulating all DRx accesses, so the hypervisor DRx values
1525 * are only important when breakpoints are actually enabled.
1526 *
1527 * In ring-0 (HM) context DR0-3 will be relocated by us, while DR7 will be
1528 * reloaded by the HM code if it changes. Further more, we will only use the
1529 * combined register set when the VBox debugger is actually using hardware BPs,
1530 * when it isn't we'll keep the guest DR0-3 + (maybe) DR6 loaded (DR6 doesn't
1531 * concern us here).
1532 *
1533 * In ring-3 we won't be loading anything, so well calculate hypervisor values
1534 * all the time.
1535 *
1536 * @returns VINF_SUCCESS.
1537 * @param pVCpu The cross context virtual CPU structure.
1538 * @param iGstReg The guest debug register number that was modified.
1539 * UINT8_MAX if not guest register.
1540 * @param fForceHyper Used in HM to force hyper registers because of single
1541 * stepping.
1542 */
1543VMMDECL(int) CPUMRecalcHyperDRx(PVMCPU pVCpu, uint8_t iGstReg, bool fForceHyper)
1544{
1545 PVM pVM = pVCpu->CTX_SUFF(pVM);
1546#ifndef IN_RING0
1547 RT_NOREF_PV(iGstReg);
1548#endif
1549
1550 /*
1551 * Compare the DR7s first.
1552 *
1553 * We only care about the enabled flags. GD is virtualized when we
1554 * dispatch the #DB, we never enable it. The DBGF DR7 value is will
1555 * always have the LE and GE bits set, so no need to check and disable
1556 * stuff if they're cleared like we have to for the guest DR7.
1557 */
1558 RTGCUINTREG uGstDr7 = CPUMGetGuestDR7(pVCpu);
1559 if (!(uGstDr7 & (X86_DR7_LE | X86_DR7_GE)))
1560 uGstDr7 = 0;
1561 else if (!(uGstDr7 & X86_DR7_LE))
1562 uGstDr7 &= ~X86_DR7_LE_ALL;
1563 else if (!(uGstDr7 & X86_DR7_GE))
1564 uGstDr7 &= ~X86_DR7_GE_ALL;
1565
1566 const RTGCUINTREG uDbgfDr7 = DBGFBpGetDR7(pVM);
1567
1568#ifdef IN_RING0
1569 if (!fForceHyper && (pVCpu->cpum.s.fUseFlags & CPUM_USED_DEBUG_REGS_HYPER))
1570 fForceHyper = true;
1571#endif
1572 if ( (!VM_IS_RAW_MODE_ENABLED(pVCpu->CTX_SUFF(pVM)) && !fForceHyper ? uDbgfDr7 : (uGstDr7 | uDbgfDr7))
1573 & X86_DR7_ENABLED_MASK)
1574 {
1575 Assert(!CPUMIsGuestDebugStateActive(pVCpu));
1576#ifdef IN_RC
1577 bool const fRawModeEnabled = true;
1578#elif defined(IN_RING3)
1579 bool const fRawModeEnabled = VM_IS_RAW_MODE_ENABLED(pVM);
1580#endif
1581
1582 /*
1583 * Ok, something is enabled. Recalc each of the breakpoints, taking
1584 * the VM debugger ones of the guest ones. In raw-mode context we will
1585 * not allow breakpoints with values inside the hypervisor area.
1586 */
1587 RTGCUINTREG uNewDr7 = X86_DR7_GE | X86_DR7_LE | X86_DR7_RA1_MASK;
1588
1589 /* bp 0 */
1590 RTGCUINTREG uNewDr0;
1591 if (uDbgfDr7 & (X86_DR7_L0 | X86_DR7_G0))
1592 {
1593 uNewDr7 |= uDbgfDr7 & (X86_DR7_L0 | X86_DR7_G0 | X86_DR7_RW0_MASK | X86_DR7_LEN0_MASK);
1594 uNewDr0 = DBGFBpGetDR0(pVM);
1595 }
1596 else if (uGstDr7 & (X86_DR7_L0 | X86_DR7_G0))
1597 {
1598 uNewDr0 = CPUMGetGuestDR0(pVCpu);
1599#ifndef IN_RING0
1600 if (fRawModeEnabled && MMHyperIsInsideArea(pVM, uNewDr0))
1601 uNewDr0 = 0;
1602 else
1603#endif
1604 uNewDr7 |= uGstDr7 & (X86_DR7_L0 | X86_DR7_G0 | X86_DR7_RW0_MASK | X86_DR7_LEN0_MASK);
1605 }
1606 else
1607 uNewDr0 = 0;
1608
1609 /* bp 1 */
1610 RTGCUINTREG uNewDr1;
1611 if (uDbgfDr7 & (X86_DR7_L1 | X86_DR7_G1))
1612 {
1613 uNewDr7 |= uDbgfDr7 & (X86_DR7_L1 | X86_DR7_G1 | X86_DR7_RW1_MASK | X86_DR7_LEN1_MASK);
1614 uNewDr1 = DBGFBpGetDR1(pVM);
1615 }
1616 else if (uGstDr7 & (X86_DR7_L1 | X86_DR7_G1))
1617 {
1618 uNewDr1 = CPUMGetGuestDR1(pVCpu);
1619#ifndef IN_RING0
1620 if (fRawModeEnabled && MMHyperIsInsideArea(pVM, uNewDr1))
1621 uNewDr1 = 0;
1622 else
1623#endif
1624 uNewDr7 |= uGstDr7 & (X86_DR7_L1 | X86_DR7_G1 | X86_DR7_RW1_MASK | X86_DR7_LEN1_MASK);
1625 }
1626 else
1627 uNewDr1 = 0;
1628
1629 /* bp 2 */
1630 RTGCUINTREG uNewDr2;
1631 if (uDbgfDr7 & (X86_DR7_L2 | X86_DR7_G2))
1632 {
1633 uNewDr7 |= uDbgfDr7 & (X86_DR7_L2 | X86_DR7_G2 | X86_DR7_RW2_MASK | X86_DR7_LEN2_MASK);
1634 uNewDr2 = DBGFBpGetDR2(pVM);
1635 }
1636 else if (uGstDr7 & (X86_DR7_L2 | X86_DR7_G2))
1637 {
1638 uNewDr2 = CPUMGetGuestDR2(pVCpu);
1639#ifndef IN_RING0
1640 if (fRawModeEnabled && MMHyperIsInsideArea(pVM, uNewDr2))
1641 uNewDr2 = 0;
1642 else
1643#endif
1644 uNewDr7 |= uGstDr7 & (X86_DR7_L2 | X86_DR7_G2 | X86_DR7_RW2_MASK | X86_DR7_LEN2_MASK);
1645 }
1646 else
1647 uNewDr2 = 0;
1648
1649 /* bp 3 */
1650 RTGCUINTREG uNewDr3;
1651 if (uDbgfDr7 & (X86_DR7_L3 | X86_DR7_G3))
1652 {
1653 uNewDr7 |= uDbgfDr7 & (X86_DR7_L3 | X86_DR7_G3 | X86_DR7_RW3_MASK | X86_DR7_LEN3_MASK);
1654 uNewDr3 = DBGFBpGetDR3(pVM);
1655 }
1656 else if (uGstDr7 & (X86_DR7_L3 | X86_DR7_G3))
1657 {
1658 uNewDr3 = CPUMGetGuestDR3(pVCpu);
1659#ifndef IN_RING0
1660 if (fRawModeEnabled && MMHyperIsInsideArea(pVM, uNewDr3))
1661 uNewDr3 = 0;
1662 else
1663#endif
1664 uNewDr7 |= uGstDr7 & (X86_DR7_L3 | X86_DR7_G3 | X86_DR7_RW3_MASK | X86_DR7_LEN3_MASK);
1665 }
1666 else
1667 uNewDr3 = 0;
1668
1669 /*
1670 * Apply the updates.
1671 */
1672#ifdef IN_RC
1673 /* Make sure to save host registers first. */
1674 if (!(pVCpu->cpum.s.fUseFlags & CPUM_USED_DEBUG_REGS_HOST))
1675 {
1676 if (!(pVCpu->cpum.s.fUseFlags & CPUM_USE_DEBUG_REGS_HOST))
1677 {
1678 pVCpu->cpum.s.Host.dr6 = ASMGetDR6();
1679 pVCpu->cpum.s.Host.dr7 = ASMGetDR7();
1680 }
1681 pVCpu->cpum.s.Host.dr0 = ASMGetDR0();
1682 pVCpu->cpum.s.Host.dr1 = ASMGetDR1();
1683 pVCpu->cpum.s.Host.dr2 = ASMGetDR2();
1684 pVCpu->cpum.s.Host.dr3 = ASMGetDR3();
1685 pVCpu->cpum.s.fUseFlags |= CPUM_USED_DEBUG_REGS_HOST | CPUM_USE_DEBUG_REGS_HYPER | CPUM_USED_DEBUG_REGS_HYPER;
1686
1687 /* We haven't loaded any hyper DRxes yet, so we'll have to load them all now. */
1688 pVCpu->cpum.s.Hyper.dr[0] = uNewDr0;
1689 ASMSetDR0(uNewDr0);
1690 pVCpu->cpum.s.Hyper.dr[1] = uNewDr1;
1691 ASMSetDR1(uNewDr1);
1692 pVCpu->cpum.s.Hyper.dr[2] = uNewDr2;
1693 ASMSetDR2(uNewDr2);
1694 pVCpu->cpum.s.Hyper.dr[3] = uNewDr3;
1695 ASMSetDR3(uNewDr3);
1696 ASMSetDR6(X86_DR6_INIT_VAL);
1697 pVCpu->cpum.s.Hyper.dr[7] = uNewDr7;
1698 ASMSetDR7(uNewDr7);
1699 }
1700 else
1701#endif
1702 {
1703 pVCpu->cpum.s.fUseFlags |= CPUM_USE_DEBUG_REGS_HYPER;
1704 if (uNewDr3 != pVCpu->cpum.s.Hyper.dr[3])
1705 CPUMSetHyperDR3(pVCpu, uNewDr3);
1706 if (uNewDr2 != pVCpu->cpum.s.Hyper.dr[2])
1707 CPUMSetHyperDR2(pVCpu, uNewDr2);
1708 if (uNewDr1 != pVCpu->cpum.s.Hyper.dr[1])
1709 CPUMSetHyperDR1(pVCpu, uNewDr1);
1710 if (uNewDr0 != pVCpu->cpum.s.Hyper.dr[0])
1711 CPUMSetHyperDR0(pVCpu, uNewDr0);
1712 if (uNewDr7 != pVCpu->cpum.s.Hyper.dr[7])
1713 CPUMSetHyperDR7(pVCpu, uNewDr7);
1714 }
1715 }
1716#ifdef IN_RING0
1717 else if (CPUMIsGuestDebugStateActive(pVCpu))
1718 {
1719 /*
1720 * Reload the register that was modified. Normally this won't happen
1721 * as we won't intercept DRx writes when not having the hyper debug
1722 * state loaded, but in case we do for some reason we'll simply deal
1723 * with it.
1724 */
1725 switch (iGstReg)
1726 {
1727 case 0: ASMSetDR0(CPUMGetGuestDR0(pVCpu)); break;
1728 case 1: ASMSetDR1(CPUMGetGuestDR1(pVCpu)); break;
1729 case 2: ASMSetDR2(CPUMGetGuestDR2(pVCpu)); break;
1730 case 3: ASMSetDR3(CPUMGetGuestDR3(pVCpu)); break;
1731 default:
1732 AssertReturn(iGstReg != UINT8_MAX, VERR_INTERNAL_ERROR_3);
1733 }
1734 }
1735#endif
1736 else
1737 {
1738 /*
1739 * No active debug state any more. In raw-mode this means we have to
1740 * make sure DR7 has everything disabled now, if we armed it already.
1741 * In ring-0 we might end up here when just single stepping.
1742 */
1743#if defined(IN_RC) || defined(IN_RING0)
1744 if (pVCpu->cpum.s.fUseFlags & CPUM_USED_DEBUG_REGS_HYPER)
1745 {
1746# ifdef IN_RC
1747 ASMSetDR7(X86_DR7_INIT_VAL);
1748# endif
1749 if (pVCpu->cpum.s.Hyper.dr[0])
1750 ASMSetDR0(0);
1751 if (pVCpu->cpum.s.Hyper.dr[1])
1752 ASMSetDR1(0);
1753 if (pVCpu->cpum.s.Hyper.dr[2])
1754 ASMSetDR2(0);
1755 if (pVCpu->cpum.s.Hyper.dr[3])
1756 ASMSetDR3(0);
1757 pVCpu->cpum.s.fUseFlags &= ~CPUM_USED_DEBUG_REGS_HYPER;
1758 }
1759#endif
1760 pVCpu->cpum.s.fUseFlags &= ~CPUM_USE_DEBUG_REGS_HYPER;
1761
1762 /* Clear all the registers. */
1763 pVCpu->cpum.s.Hyper.dr[7] = X86_DR7_RA1_MASK;
1764 pVCpu->cpum.s.Hyper.dr[3] = 0;
1765 pVCpu->cpum.s.Hyper.dr[2] = 0;
1766 pVCpu->cpum.s.Hyper.dr[1] = 0;
1767 pVCpu->cpum.s.Hyper.dr[0] = 0;
1768
1769 }
1770 Log2(("CPUMRecalcHyperDRx: fUseFlags=%#x %RGr %RGr %RGr %RGr %RGr %RGr\n",
1771 pVCpu->cpum.s.fUseFlags, pVCpu->cpum.s.Hyper.dr[0], pVCpu->cpum.s.Hyper.dr[1],
1772 pVCpu->cpum.s.Hyper.dr[2], pVCpu->cpum.s.Hyper.dr[3], pVCpu->cpum.s.Hyper.dr[6],
1773 pVCpu->cpum.s.Hyper.dr[7]));
1774
1775 return VINF_SUCCESS;
1776}
1777
1778
1779/**
1780 * Set the guest XCR0 register.
1781 *
1782 * Will load additional state if the FPU state is already loaded (in ring-0 &
1783 * raw-mode context).
1784 *
1785 * @returns VINF_SUCCESS on success, VERR_CPUM_RAISE_GP_0 on invalid input
1786 * value.
1787 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1788 * @param uNewValue The new value.
1789 * @thread EMT(pVCpu)
1790 */
1791VMM_INT_DECL(int) CPUMSetGuestXcr0(PVMCPU pVCpu, uint64_t uNewValue)
1792{
1793 if ( (uNewValue & ~pVCpu->CTX_SUFF(pVM)->cpum.s.fXStateGuestMask) == 0
1794 /* The X87 bit cannot be cleared. */
1795 && (uNewValue & XSAVE_C_X87)
1796 /* AVX requires SSE. */
1797 && (uNewValue & (XSAVE_C_SSE | XSAVE_C_YMM)) != XSAVE_C_YMM
1798 /* AVX-512 requires YMM, SSE and all of its three components to be enabled. */
1799 && ( (uNewValue & (XSAVE_C_OPMASK | XSAVE_C_ZMM_HI256 | XSAVE_C_ZMM_16HI)) == 0
1800 || (uNewValue & (XSAVE_C_SSE | XSAVE_C_YMM | XSAVE_C_OPMASK | XSAVE_C_ZMM_HI256 | XSAVE_C_ZMM_16HI))
1801 == (XSAVE_C_SSE | XSAVE_C_YMM | XSAVE_C_OPMASK | XSAVE_C_ZMM_HI256 | XSAVE_C_ZMM_16HI) )
1802 )
1803 {
1804 pVCpu->cpum.s.Guest.aXcr[0] = uNewValue;
1805
1806 /* If more state components are enabled, we need to take care to load
1807 them if the FPU/SSE state is already loaded. May otherwise leak
1808 host state to the guest. */
1809 uint64_t fNewComponents = ~pVCpu->cpum.s.Guest.fXStateMask & uNewValue;
1810 if (fNewComponents)
1811 {
1812#if defined(IN_RING0) || defined(IN_RC)
1813 if (pVCpu->cpum.s.fUseFlags & CPUM_USED_FPU_GUEST)
1814 {
1815 if (pVCpu->cpum.s.Guest.fXStateMask != 0)
1816 /* Adding more components. */
1817 ASMXRstor(pVCpu->cpum.s.Guest.CTX_SUFF(pXState), fNewComponents);
1818 else
1819 {
1820 /* We're switching from FXSAVE/FXRSTOR to XSAVE/XRSTOR. */
1821 pVCpu->cpum.s.Guest.fXStateMask |= XSAVE_C_X87 | XSAVE_C_SSE;
1822 if (uNewValue & ~(XSAVE_C_X87 | XSAVE_C_SSE))
1823 ASMXRstor(pVCpu->cpum.s.Guest.CTX_SUFF(pXState), uNewValue & ~(XSAVE_C_X87 | XSAVE_C_SSE));
1824 }
1825 }
1826#endif
1827 pVCpu->cpum.s.Guest.fXStateMask |= uNewValue;
1828 }
1829 return VINF_SUCCESS;
1830 }
1831 return VERR_CPUM_RAISE_GP_0;
1832}
1833
1834
1835/**
1836 * Tests if the guest has No-Execute Page Protection Enabled (NXE).
1837 *
1838 * @returns true if in real mode, otherwise false.
1839 * @param pVCpu The cross context virtual CPU structure.
1840 */
1841VMMDECL(bool) CPUMIsGuestNXEnabled(PVMCPU pVCpu)
1842{
1843 return !!(pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_NXE);
1844}
1845
1846
1847/**
1848 * Tests if the guest has the Page Size Extension enabled (PSE).
1849 *
1850 * @returns true if in real mode, otherwise false.
1851 * @param pVCpu The cross context virtual CPU structure.
1852 */
1853VMMDECL(bool) CPUMIsGuestPageSizeExtEnabled(PVMCPU pVCpu)
1854{
1855 /* PAE or AMD64 implies support for big pages regardless of CR4.PSE */
1856 return !!(pVCpu->cpum.s.Guest.cr4 & (X86_CR4_PSE | X86_CR4_PAE));
1857}
1858
1859
1860/**
1861 * Tests if the guest has the paging enabled (PG).
1862 *
1863 * @returns true if in real mode, otherwise false.
1864 * @param pVCpu The cross context virtual CPU structure.
1865 */
1866VMMDECL(bool) CPUMIsGuestPagingEnabled(PVMCPU pVCpu)
1867{
1868 return !!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PG);
1869}
1870
1871
1872/**
1873 * Tests if the guest has the paging enabled (PG).
1874 *
1875 * @returns true if in real mode, otherwise false.
1876 * @param pVCpu The cross context virtual CPU structure.
1877 */
1878VMMDECL(bool) CPUMIsGuestR0WriteProtEnabled(PVMCPU pVCpu)
1879{
1880 return !!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_WP);
1881}
1882
1883
1884/**
1885 * Tests if the guest is running in real mode or not.
1886 *
1887 * @returns true if in real mode, otherwise false.
1888 * @param pVCpu The cross context virtual CPU structure.
1889 */
1890VMMDECL(bool) CPUMIsGuestInRealMode(PVMCPU pVCpu)
1891{
1892 return !(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE);
1893}
1894
1895
1896/**
1897 * Tests if the guest is running in real or virtual 8086 mode.
1898 *
1899 * @returns @c true if it is, @c false if not.
1900 * @param pVCpu The cross context virtual CPU structure.
1901 */
1902VMMDECL(bool) CPUMIsGuestInRealOrV86Mode(PVMCPU pVCpu)
1903{
1904 return !(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE)
1905 || pVCpu->cpum.s.Guest.eflags.Bits.u1VM; /** @todo verify that this cannot be set in long mode. */
1906}
1907
1908
1909/**
1910 * Tests if the guest is running in protected or not.
1911 *
1912 * @returns true if in protected mode, otherwise false.
1913 * @param pVCpu The cross context virtual CPU structure.
1914 */
1915VMMDECL(bool) CPUMIsGuestInProtectedMode(PVMCPU pVCpu)
1916{
1917 return !!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE);
1918}
1919
1920
1921/**
1922 * Tests if the guest is running in paged protected or not.
1923 *
1924 * @returns true if in paged protected mode, otherwise false.
1925 * @param pVCpu The cross context virtual CPU structure.
1926 */
1927VMMDECL(bool) CPUMIsGuestInPagedProtectedMode(PVMCPU pVCpu)
1928{
1929 return (pVCpu->cpum.s.Guest.cr0 & (X86_CR0_PE | X86_CR0_PG)) == (X86_CR0_PE | X86_CR0_PG);
1930}
1931
1932
1933/**
1934 * Tests if the guest is running in long mode or not.
1935 *
1936 * @returns true if in long mode, otherwise false.
1937 * @param pVCpu The cross context virtual CPU structure.
1938 */
1939VMMDECL(bool) CPUMIsGuestInLongMode(PVMCPU pVCpu)
1940{
1941 return (pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA) == MSR_K6_EFER_LMA;
1942}
1943
1944
1945/**
1946 * Tests if the guest is running in PAE mode or not.
1947 *
1948 * @returns true if in PAE mode, otherwise false.
1949 * @param pVCpu The cross context virtual CPU structure.
1950 */
1951VMMDECL(bool) CPUMIsGuestInPAEMode(PVMCPU pVCpu)
1952{
1953 /* Intel mentions EFER.LMA and EFER.LME in different parts of their spec. We shall use EFER.LMA rather
1954 than EFER.LME as it reflects if the CPU has entered paging with EFER.LME set. */
1955 return (pVCpu->cpum.s.Guest.cr4 & X86_CR4_PAE)
1956 && (pVCpu->cpum.s.Guest.cr0 & X86_CR0_PG)
1957 && !(pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA);
1958}
1959
1960
1961/**
1962 * Tests if the guest is running in 64 bits mode or not.
1963 *
1964 * @returns true if in 64 bits protected mode, otherwise false.
1965 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1966 */
1967VMMDECL(bool) CPUMIsGuestIn64BitCode(PVMCPU pVCpu)
1968{
1969 if (!CPUMIsGuestInLongMode(pVCpu))
1970 return false;
1971 CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(pVCpu, &pVCpu->cpum.s.Guest.cs);
1972 return pVCpu->cpum.s.Guest.cs.Attr.n.u1Long;
1973}
1974
1975
1976/**
1977 * Helper for CPUMIsGuestIn64BitCodeEx that handles lazy resolving of hidden CS
1978 * registers.
1979 *
1980 * @returns true if in 64 bits protected mode, otherwise false.
1981 * @param pCtx Pointer to the current guest CPU context.
1982 */
1983VMM_INT_DECL(bool) CPUMIsGuestIn64BitCodeSlow(PCPUMCTX pCtx)
1984{
1985 return CPUMIsGuestIn64BitCode(CPUM_GUEST_CTX_TO_VMCPU(pCtx));
1986}
1987
1988#ifdef VBOX_WITH_RAW_MODE_NOT_R0
1989
1990/**
1991 *
1992 * @returns @c true if we've entered raw-mode and selectors with RPL=1 are
1993 * really RPL=0, @c false if we've not (RPL=1 really is RPL=1).
1994 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1995 */
1996VMM_INT_DECL(bool) CPUMIsGuestInRawMode(PVMCPU pVCpu)
1997{
1998 return pVCpu->cpum.s.fRawEntered;
1999}
2000
2001/**
2002 * Transforms the guest CPU state to raw-ring mode.
2003 *
2004 * This function will change the any of the cs and ss register with DPL=0 to DPL=1.
2005 *
2006 * @returns VBox status code. (recompiler failure)
2007 * @param pVCpu The cross context virtual CPU structure.
2008 * @see @ref pg_raw
2009 */
2010VMM_INT_DECL(int) CPUMRawEnter(PVMCPU pVCpu)
2011{
2012 PVM pVM = pVCpu->CTX_SUFF(pVM);
2013
2014 Assert(!pVCpu->cpum.s.fRawEntered);
2015 Assert(!pVCpu->cpum.s.fRemEntered);
2016 PCPUMCTX pCtx = &pVCpu->cpum.s.Guest;
2017
2018 /*
2019 * Are we in Ring-0?
2020 */
2021 if ( pCtx->ss.Sel
2022 && (pCtx->ss.Sel & X86_SEL_RPL) == 0
2023 && !pCtx->eflags.Bits.u1VM)
2024 {
2025 /*
2026 * Enter execution mode.
2027 */
2028 PATMRawEnter(pVM, pCtx);
2029
2030 /*
2031 * Set CPL to Ring-1.
2032 */
2033 pCtx->ss.Sel |= 1;
2034 if ( pCtx->cs.Sel
2035 && (pCtx->cs.Sel & X86_SEL_RPL) == 0)
2036 pCtx->cs.Sel |= 1;
2037 }
2038 else
2039 {
2040# ifdef VBOX_WITH_RAW_RING1
2041 if ( EMIsRawRing1Enabled(pVM)
2042 && !pCtx->eflags.Bits.u1VM
2043 && (pCtx->ss.Sel & X86_SEL_RPL) == 1)
2044 {
2045 /* Set CPL to Ring-2. */
2046 pCtx->ss.Sel = (pCtx->ss.Sel & ~X86_SEL_RPL) | 2;
2047 if (pCtx->cs.Sel && (pCtx->cs.Sel & X86_SEL_RPL) == 1)
2048 pCtx->cs.Sel = (pCtx->cs.Sel & ~X86_SEL_RPL) | 2;
2049 }
2050# else
2051 AssertMsg((pCtx->ss.Sel & X86_SEL_RPL) >= 2 || pCtx->eflags.Bits.u1VM,
2052 ("ring-1 code not supported\n"));
2053# endif
2054 /*
2055 * PATM takes care of IOPL and IF flags for Ring-3 and Ring-2 code as well.
2056 */
2057 PATMRawEnter(pVM, pCtx);
2058 }
2059
2060 /*
2061 * Assert sanity.
2062 */
2063 AssertMsg((pCtx->eflags.u32 & X86_EFL_IF), ("X86_EFL_IF is clear\n"));
2064 AssertReleaseMsg(pCtx->eflags.Bits.u2IOPL == 0,
2065 ("X86_EFL_IOPL=%d CPL=%d\n", pCtx->eflags.Bits.u2IOPL, pCtx->ss.Sel & X86_SEL_RPL));
2066 Assert((pVCpu->cpum.s.Guest.cr0 & (X86_CR0_PG | X86_CR0_PE)) == (X86_CR0_PG | X86_CR0_PE));
2067
2068 pCtx->eflags.u32 |= X86_EFL_IF; /* paranoia */
2069
2070 pVCpu->cpum.s.fRawEntered = true;
2071 return VINF_SUCCESS;
2072}
2073
2074
2075/**
2076 * Transforms the guest CPU state from raw-ring mode to correct values.
2077 *
2078 * This function will change any selector registers with DPL=1 to DPL=0.
2079 *
2080 * @returns Adjusted rc.
2081 * @param pVCpu The cross context virtual CPU structure.
2082 * @param rc Raw mode return code
2083 * @see @ref pg_raw
2084 */
2085VMM_INT_DECL(int) CPUMRawLeave(PVMCPU pVCpu, int rc)
2086{
2087 PVM pVM = pVCpu->CTX_SUFF(pVM);
2088
2089 /*
2090 * Don't leave if we've already left (in RC).
2091 */
2092 Assert(!pVCpu->cpum.s.fRemEntered);
2093 if (!pVCpu->cpum.s.fRawEntered)
2094 return rc;
2095 pVCpu->cpum.s.fRawEntered = false;
2096
2097 PCPUMCTX pCtx = &pVCpu->cpum.s.Guest;
2098 Assert(pCtx->eflags.Bits.u1VM || (pCtx->ss.Sel & X86_SEL_RPL));
2099 AssertMsg(pCtx->eflags.Bits.u1VM || pCtx->eflags.Bits.u2IOPL < (unsigned)(pCtx->ss.Sel & X86_SEL_RPL),
2100 ("X86_EFL_IOPL=%d CPL=%d\n", pCtx->eflags.Bits.u2IOPL, pCtx->ss.Sel & X86_SEL_RPL));
2101
2102 /*
2103 * Are we executing in raw ring-1?
2104 */
2105 if ( (pCtx->ss.Sel & X86_SEL_RPL) == 1
2106 && !pCtx->eflags.Bits.u1VM)
2107 {
2108 /*
2109 * Leave execution mode.
2110 */
2111 PATMRawLeave(pVM, pCtx, rc);
2112 /* Not quite sure if this is really required, but shouldn't harm (too much anyways). */
2113 /** @todo See what happens if we remove this. */
2114 if ((pCtx->ds.Sel & X86_SEL_RPL) == 1)
2115 pCtx->ds.Sel &= ~X86_SEL_RPL;
2116 if ((pCtx->es.Sel & X86_SEL_RPL) == 1)
2117 pCtx->es.Sel &= ~X86_SEL_RPL;
2118 if ((pCtx->fs.Sel & X86_SEL_RPL) == 1)
2119 pCtx->fs.Sel &= ~X86_SEL_RPL;
2120 if ((pCtx->gs.Sel & X86_SEL_RPL) == 1)
2121 pCtx->gs.Sel &= ~X86_SEL_RPL;
2122
2123 /*
2124 * Ring-1 selector => Ring-0.
2125 */
2126 pCtx->ss.Sel &= ~X86_SEL_RPL;
2127 if ((pCtx->cs.Sel & X86_SEL_RPL) == 1)
2128 pCtx->cs.Sel &= ~X86_SEL_RPL;
2129 }
2130 else
2131 {
2132 /*
2133 * PATM is taking care of the IOPL and IF flags for us.
2134 */
2135 PATMRawLeave(pVM, pCtx, rc);
2136 if (!pCtx->eflags.Bits.u1VM)
2137 {
2138# ifdef VBOX_WITH_RAW_RING1
2139 if ( EMIsRawRing1Enabled(pVM)
2140 && (pCtx->ss.Sel & X86_SEL_RPL) == 2)
2141 {
2142 /* Not quite sure if this is really required, but shouldn't harm (too much anyways). */
2143 /** @todo See what happens if we remove this. */
2144 if ((pCtx->ds.Sel & X86_SEL_RPL) == 2)
2145 pCtx->ds.Sel = (pCtx->ds.Sel & ~X86_SEL_RPL) | 1;
2146 if ((pCtx->es.Sel & X86_SEL_RPL) == 2)
2147 pCtx->es.Sel = (pCtx->es.Sel & ~X86_SEL_RPL) | 1;
2148 if ((pCtx->fs.Sel & X86_SEL_RPL) == 2)
2149 pCtx->fs.Sel = (pCtx->fs.Sel & ~X86_SEL_RPL) | 1;
2150 if ((pCtx->gs.Sel & X86_SEL_RPL) == 2)
2151 pCtx->gs.Sel = (pCtx->gs.Sel & ~X86_SEL_RPL) | 1;
2152
2153 /*
2154 * Ring-2 selector => Ring-1.
2155 */
2156 pCtx->ss.Sel = (pCtx->ss.Sel & ~X86_SEL_RPL) | 1;
2157 if ((pCtx->cs.Sel & X86_SEL_RPL) == 2)
2158 pCtx->cs.Sel = (pCtx->cs.Sel & ~X86_SEL_RPL) | 1;
2159 }
2160 else
2161 {
2162# endif
2163 /** @todo See what happens if we remove this. */
2164 if ((pCtx->ds.Sel & X86_SEL_RPL) == 1)
2165 pCtx->ds.Sel &= ~X86_SEL_RPL;
2166 if ((pCtx->es.Sel & X86_SEL_RPL) == 1)
2167 pCtx->es.Sel &= ~X86_SEL_RPL;
2168 if ((pCtx->fs.Sel & X86_SEL_RPL) == 1)
2169 pCtx->fs.Sel &= ~X86_SEL_RPL;
2170 if ((pCtx->gs.Sel & X86_SEL_RPL) == 1)
2171 pCtx->gs.Sel &= ~X86_SEL_RPL;
2172# ifdef VBOX_WITH_RAW_RING1
2173 }
2174# endif
2175 }
2176 }
2177
2178 return rc;
2179}
2180
2181#endif /* VBOX_WITH_RAW_MODE_NOT_R0 */
2182
2183/**
2184 * Updates the EFLAGS while we're in raw-mode.
2185 *
2186 * @param pVCpu The cross context virtual CPU structure.
2187 * @param fEfl The new EFLAGS value.
2188 */
2189VMMDECL(void) CPUMRawSetEFlags(PVMCPU pVCpu, uint32_t fEfl)
2190{
2191#ifdef VBOX_WITH_RAW_MODE_NOT_R0
2192 if (pVCpu->cpum.s.fRawEntered)
2193 PATMRawSetEFlags(pVCpu->CTX_SUFF(pVM), &pVCpu->cpum.s.Guest, fEfl);
2194 else
2195#endif
2196 pVCpu->cpum.s.Guest.eflags.u32 = fEfl;
2197}
2198
2199
2200/**
2201 * Gets the EFLAGS while we're in raw-mode.
2202 *
2203 * @returns The eflags.
2204 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2205 */
2206VMMDECL(uint32_t) CPUMRawGetEFlags(PVMCPU pVCpu)
2207{
2208#ifdef VBOX_WITH_RAW_MODE_NOT_R0
2209 if (pVCpu->cpum.s.fRawEntered)
2210 return PATMRawGetEFlags(pVCpu->CTX_SUFF(pVM), &pVCpu->cpum.s.Guest);
2211#endif
2212 return pVCpu->cpum.s.Guest.eflags.u32;
2213}
2214
2215
2216/**
2217 * Sets the specified changed flags (CPUM_CHANGED_*).
2218 *
2219 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2220 * @param fChangedAdd The changed flags to add.
2221 */
2222VMMDECL(void) CPUMSetChangedFlags(PVMCPU pVCpu, uint32_t fChangedAdd)
2223{
2224 pVCpu->cpum.s.fChanged |= fChangedAdd;
2225}
2226
2227
2228/**
2229 * Checks if the CPU supports the XSAVE and XRSTOR instruction.
2230 *
2231 * @returns true if supported.
2232 * @returns false if not supported.
2233 * @param pVM The cross context VM structure.
2234 */
2235VMMDECL(bool) CPUMSupportsXSave(PVM pVM)
2236{
2237 return pVM->cpum.s.HostFeatures.fXSaveRstor != 0;
2238}
2239
2240
2241/**
2242 * Checks if the host OS uses the SYSENTER / SYSEXIT instructions.
2243 * @returns true if used.
2244 * @returns false if not used.
2245 * @param pVM The cross context VM structure.
2246 */
2247VMMDECL(bool) CPUMIsHostUsingSysEnter(PVM pVM)
2248{
2249 return RT_BOOL(pVM->cpum.s.fHostUseFlags & CPUM_USE_SYSENTER);
2250}
2251
2252
2253/**
2254 * Checks if the host OS uses the SYSCALL / SYSRET instructions.
2255 * @returns true if used.
2256 * @returns false if not used.
2257 * @param pVM The cross context VM structure.
2258 */
2259VMMDECL(bool) CPUMIsHostUsingSysCall(PVM pVM)
2260{
2261 return RT_BOOL(pVM->cpum.s.fHostUseFlags & CPUM_USE_SYSCALL);
2262}
2263
2264#ifdef IN_RC
2265
2266/**
2267 * Lazily sync in the FPU/XMM state.
2268 *
2269 * @returns VBox status code.
2270 * @param pVCpu The cross context virtual CPU structure.
2271 */
2272VMMDECL(int) CPUMHandleLazyFPU(PVMCPU pVCpu)
2273{
2274 return cpumHandleLazyFPUAsm(&pVCpu->cpum.s);
2275}
2276
2277#endif /* !IN_RC */
2278
2279/**
2280 * Checks if we activated the FPU/XMM state of the guest OS.
2281 *
2282 * This differs from CPUMIsGuestFPUStateLoaded() in that it refers to the next
2283 * time we'll be executing guest code, so it may return true for 64-on-32 when
2284 * we still haven't actually loaded the FPU status, just scheduled it to be
2285 * loaded the next time we go thru the world switcher (CPUM_SYNC_FPU_STATE).
2286 *
2287 * @returns true / false.
2288 * @param pVCpu The cross context virtual CPU structure.
2289 */
2290VMMDECL(bool) CPUMIsGuestFPUStateActive(PVMCPU pVCpu)
2291{
2292 return RT_BOOL(pVCpu->cpum.s.fUseFlags & (CPUM_USED_FPU_GUEST | CPUM_SYNC_FPU_STATE));
2293}
2294
2295
2296/**
2297 * Checks if we've really loaded the FPU/XMM state of the guest OS.
2298 *
2299 * @returns true / false.
2300 * @param pVCpu The cross context virtual CPU structure.
2301 */
2302VMMDECL(bool) CPUMIsGuestFPUStateLoaded(PVMCPU pVCpu)
2303{
2304 return RT_BOOL(pVCpu->cpum.s.fUseFlags & CPUM_USED_FPU_GUEST);
2305}
2306
2307
2308/**
2309 * Checks if we saved the FPU/XMM state of the host OS.
2310 *
2311 * @returns true / false.
2312 * @param pVCpu The cross context virtual CPU structure.
2313 */
2314VMMDECL(bool) CPUMIsHostFPUStateSaved(PVMCPU pVCpu)
2315{
2316 return RT_BOOL(pVCpu->cpum.s.fUseFlags & CPUM_USED_FPU_HOST);
2317}
2318
2319
2320/**
2321 * Checks if the guest debug state is active.
2322 *
2323 * @returns boolean
2324 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2325 */
2326VMMDECL(bool) CPUMIsGuestDebugStateActive(PVMCPU pVCpu)
2327{
2328 return RT_BOOL(pVCpu->cpum.s.fUseFlags & CPUM_USED_DEBUG_REGS_GUEST);
2329}
2330
2331
2332/**
2333 * Checks if the guest debug state is to be made active during the world-switch
2334 * (currently only used for the 32->64 switcher case).
2335 *
2336 * @returns boolean
2337 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2338 */
2339VMMDECL(bool) CPUMIsGuestDebugStateActivePending(PVMCPU pVCpu)
2340{
2341 return RT_BOOL(pVCpu->cpum.s.fUseFlags & CPUM_SYNC_DEBUG_REGS_GUEST);
2342}
2343
2344
2345/**
2346 * Checks if the hyper debug state is active.
2347 *
2348 * @returns boolean
2349 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2350 */
2351VMMDECL(bool) CPUMIsHyperDebugStateActive(PVMCPU pVCpu)
2352{
2353 return RT_BOOL(pVCpu->cpum.s.fUseFlags & CPUM_USED_DEBUG_REGS_HYPER);
2354}
2355
2356
2357/**
2358 * Checks if the hyper debug state is to be made active during the world-switch
2359 * (currently only used for the 32->64 switcher case).
2360 *
2361 * @returns boolean
2362 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2363 */
2364VMMDECL(bool) CPUMIsHyperDebugStateActivePending(PVMCPU pVCpu)
2365{
2366 return RT_BOOL(pVCpu->cpum.s.fUseFlags & CPUM_SYNC_DEBUG_REGS_HYPER);
2367}
2368
2369
2370/**
2371 * Mark the guest's debug state as inactive.
2372 *
2373 * @returns boolean
2374 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2375 * @todo This API doesn't make sense any more.
2376 */
2377VMMDECL(void) CPUMDeactivateGuestDebugState(PVMCPU pVCpu)
2378{
2379 Assert(!(pVCpu->cpum.s.fUseFlags & (CPUM_USED_DEBUG_REGS_GUEST | CPUM_USED_DEBUG_REGS_HYPER | CPUM_USED_DEBUG_REGS_HOST)));
2380 NOREF(pVCpu);
2381}
2382
2383
2384/**
2385 * Get the current privilege level of the guest.
2386 *
2387 * @returns CPL
2388 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2389 */
2390VMMDECL(uint32_t) CPUMGetGuestCPL(PVMCPU pVCpu)
2391{
2392 /*
2393 * CPL can reliably be found in SS.DPL (hidden regs valid) or SS if not.
2394 *
2395 * Note! We used to check CS.DPL here, assuming it was always equal to
2396 * CPL even if a conforming segment was loaded. But this turned out to
2397 * only apply to older AMD-V. With VT-x we had an ACP2 regression
2398 * during install after a far call to ring 2 with VT-x. Then on newer
2399 * AMD-V CPUs we have to move the VMCB.guest.u8CPL into cs.Attr.n.u2Dpl
2400 * as well as ss.Attr.n.u2Dpl to make this (and other) code work right.
2401 *
2402 * So, forget CS.DPL, always use SS.DPL.
2403 *
2404 * Note! The SS RPL is always equal to the CPL, while the CS RPL
2405 * isn't necessarily equal if the segment is conforming.
2406 * See section 4.11.1 in the AMD manual.
2407 *
2408 * Update: Where the heck does it say CS.RPL can differ from CPL other than
2409 * right after real->prot mode switch and when in V8086 mode? That
2410 * section says the RPL specified in a direct transfere (call, jmp,
2411 * ret) is not the one loaded into CS. Besides, if CS.RPL != CPL
2412 * it would be impossible for an exception handle or the iret
2413 * instruction to figure out whether SS:ESP are part of the frame
2414 * or not. VBox or qemu bug must've lead to this misconception.
2415 *
2416 * Update2: On an AMD bulldozer system here, I've no trouble loading a null
2417 * selector into SS with an RPL other than the CPL when CPL != 3 and
2418 * we're in 64-bit mode. The intel dev box doesn't allow this, on
2419 * RPL = CPL. Weird.
2420 */
2421 uint32_t uCpl;
2422 if (pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE)
2423 {
2424 if (!pVCpu->cpum.s.Guest.eflags.Bits.u1VM)
2425 {
2426 if (CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, &pVCpu->cpum.s.Guest.ss))
2427 uCpl = pVCpu->cpum.s.Guest.ss.Attr.n.u2Dpl;
2428 else
2429 {
2430 uCpl = (pVCpu->cpum.s.Guest.ss.Sel & X86_SEL_RPL);
2431#ifdef VBOX_WITH_RAW_MODE_NOT_R0
2432# ifdef VBOX_WITH_RAW_RING1
2433 if (pVCpu->cpum.s.fRawEntered)
2434 {
2435 if ( uCpl == 2
2436 && EMIsRawRing1Enabled(pVCpu->CTX_SUFF(pVM)))
2437 uCpl = 1;
2438 else if (uCpl == 1)
2439 uCpl = 0;
2440 }
2441 Assert(uCpl != 2); /* ring 2 support not allowed anymore. */
2442# else
2443 if (uCpl == 1)
2444 uCpl = 0;
2445# endif
2446#endif
2447 }
2448 }
2449 else
2450 uCpl = 3; /* V86 has CPL=3; REM doesn't set DPL=3 in V8086 mode. See @bugref{5130}. */
2451 }
2452 else
2453 uCpl = 0; /* Real mode is zero; CPL set to 3 for VT-x real-mode emulation. */
2454 return uCpl;
2455}
2456
2457
2458/**
2459 * Gets the current guest CPU mode.
2460 *
2461 * If paging mode is what you need, check out PGMGetGuestMode().
2462 *
2463 * @returns The CPU mode.
2464 * @param pVCpu The cross context virtual CPU structure.
2465 */
2466VMMDECL(CPUMMODE) CPUMGetGuestMode(PVMCPU pVCpu)
2467{
2468 CPUMMODE enmMode;
2469 if (!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE))
2470 enmMode = CPUMMODE_REAL;
2471 else if (!(pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA))
2472 enmMode = CPUMMODE_PROTECTED;
2473 else
2474 enmMode = CPUMMODE_LONG;
2475
2476 return enmMode;
2477}
2478
2479
2480/**
2481 * Figure whether the CPU is currently executing 16, 32 or 64 bit code.
2482 *
2483 * @returns 16, 32 or 64.
2484 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2485 */
2486VMMDECL(uint32_t) CPUMGetGuestCodeBits(PVMCPU pVCpu)
2487{
2488 if (!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE))
2489 return 16;
2490
2491 if (pVCpu->cpum.s.Guest.eflags.Bits.u1VM)
2492 {
2493 Assert(!(pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA));
2494 return 16;
2495 }
2496
2497 CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(pVCpu, &pVCpu->cpum.s.Guest.cs);
2498 if ( pVCpu->cpum.s.Guest.cs.Attr.n.u1Long
2499 && (pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA))
2500 return 64;
2501
2502 if (pVCpu->cpum.s.Guest.cs.Attr.n.u1DefBig)
2503 return 32;
2504
2505 return 16;
2506}
2507
2508
2509VMMDECL(DISCPUMODE) CPUMGetGuestDisMode(PVMCPU pVCpu)
2510{
2511 if (!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE))
2512 return DISCPUMODE_16BIT;
2513
2514 if (pVCpu->cpum.s.Guest.eflags.Bits.u1VM)
2515 {
2516 Assert(!(pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA));
2517 return DISCPUMODE_16BIT;
2518 }
2519
2520 CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(pVCpu, &pVCpu->cpum.s.Guest.cs);
2521 if ( pVCpu->cpum.s.Guest.cs.Attr.n.u1Long
2522 && (pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA))
2523 return DISCPUMODE_64BIT;
2524
2525 if (pVCpu->cpum.s.Guest.cs.Attr.n.u1DefBig)
2526 return DISCPUMODE_32BIT;
2527
2528 return DISCPUMODE_16BIT;
2529}
2530
2531
2532/**
2533 * Gets the guest MXCSR_MASK value.
2534 *
2535 * This does not access the x87 state, but the value we determined at VM
2536 * initialization.
2537 *
2538 * @returns MXCSR mask.
2539 * @param pVM The cross context VM structure.
2540 */
2541VMMDECL(uint32_t) CPUMGetGuestMxCsrMask(PVM pVM)
2542{
2543 return pVM->cpum.s.GuestInfo.fMxCsrMask;
2544}
2545
2546
2547/**
2548 * Checks whether the SVM nested-guest is in a state to receive physical (APIC)
2549 * interrupts.
2550 *
2551 * @returns VBox status code.
2552 * @retval true if it's ready, false otherwise.
2553 *
2554 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2555 * @param pCtx The guest-CPU context.
2556 *
2557 * @sa hmR0SvmCanNstGstTakePhysIntr.
2558 */
2559VMM_INT_DECL(bool) CPUMCanSvmNstGstTakePhysIntr(PVMCPU pVCpu, PCCPUMCTX pCtx)
2560{
2561 /** @todo Optimization: Avoid this function call and use a pointer to the
2562 * relevant eflags instead (setup during VMRUN instruction emulation). */
2563#ifdef IN_RC
2564 RT_NOREF2(pVCpu, pCtx);
2565 AssertReleaseFailedReturn(false);
2566#else
2567 Assert(CPUMIsGuestInSvmNestedHwVirtMode(pCtx));
2568 Assert(pCtx->hwvirt.fGif);
2569
2570 if (!pCtx->hwvirt.svm.fHMCachedVmcb)
2571 {
2572 PCSVMVMCBCTRL pVmcbCtrl = &pCtx->hwvirt.svm.CTX_SUFF(pVmcb)->ctrl;
2573 X86EFLAGS fEFlags;
2574 if (pVmcbCtrl->IntCtrl.n.u1VIntrMasking)
2575 fEFlags.u = pCtx->hwvirt.svm.HostState.rflags.u;
2576 else
2577 fEFlags.u = pCtx->eflags.u;
2578
2579 return fEFlags.Bits.u1IF;
2580 }
2581
2582 return HMCanSvmNstGstTakePhysIntr(pVCpu, pCtx);
2583#endif
2584}
2585
2586
2587/**
2588 * Checks whether the SVM nested-guest is in a state to receive virtual (setup
2589 * for injection by VMRUN instruction) interrupts.
2590 *
2591 * @returns VBox status code.
2592 * @retval true if it's ready, false otherwise.
2593 *
2594 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2595 * @param pCtx The guest-CPU context.
2596 */
2597VMM_INT_DECL(bool) CPUMCanSvmNstGstTakeVirtIntr(PVMCPU pVCpu, PCCPUMCTX pCtx)
2598{
2599#ifdef IN_RC
2600 RT_NOREF2(pVCpu, pCtx);
2601 AssertReleaseFailedReturn(false);
2602#else
2603 Assert(CPUMIsGuestInSvmNestedHwVirtMode(pCtx));
2604 Assert(pCtx->hwvirt.fGif);
2605
2606 /*
2607 * Although at present, the V_TPR and V_INTR_PRIO fields are not modified
2608 * by SVM R0 code and we could inspect them directly here, we play it
2609 * safe and ask HM if it has cached the VMCB.
2610 */
2611 if (!pCtx->hwvirt.svm.fHMCachedVmcb)
2612 {
2613 PCSVMVMCBCTRL pVmcbCtrl = &pCtx->hwvirt.svm.CTX_SUFF(pVmcb)->ctrl;
2614 if ( !pVmcbCtrl->IntCtrl.n.u1IgnoreTPR
2615 && pVmcbCtrl->IntCtrl.n.u4VIntrPrio <= pVmcbCtrl->IntCtrl.n.u8VTPR)
2616 return false;
2617
2618 X86EFLAGS fEFlags;
2619 if (pVmcbCtrl->IntCtrl.n.u1VIntrMasking)
2620 fEFlags.u = pCtx->eflags.u;
2621 else
2622 fEFlags.u = pCtx->hwvirt.svm.HostState.rflags.u;
2623
2624 return fEFlags.Bits.u1IF;
2625 }
2626
2627 return HMCanSvmNstGstTakeVirtIntr(pVCpu, pCtx);
2628#endif
2629}
2630
2631
2632/**
2633 * Gets the pending SVM nested-guest interrupt.
2634 *
2635 * @returns The nested-guest interrupt to inject.
2636 * @param pCtx The guest-CPU context.
2637 */
2638VMM_INT_DECL(uint8_t) CPUMGetSvmNstGstInterrupt(PCCPUMCTX pCtx)
2639{
2640#ifdef IN_RC
2641 RT_NOREF(pCtx);
2642 AssertReleaseFailedReturn(0);
2643#else
2644 PCSVMVMCBCTRL pVmcbCtrl = &pCtx->hwvirt.svm.CTX_SUFF(pVmcb)->ctrl;
2645 return pVmcbCtrl->IntCtrl.n.u8VIntrVector;
2646#endif
2647}
2648
2649
2650/**
2651 * Restores the host-state from the host-state save area as part of a \#VMEXIT.
2652 *
2653 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2654 * @param pCtx The guest-CPU context.
2655 */
2656VMM_INT_DECL(void) CPUMSvmVmExitRestoreHostState(PVMCPU pVCpu, PCPUMCTX pCtx)
2657{
2658 /*
2659 * Reload the guest's "host state".
2660 */
2661 PSVMHOSTSTATE pHostState = &pCtx->hwvirt.svm.HostState;
2662 pCtx->es = pHostState->es;
2663 pCtx->cs = pHostState->cs;
2664 pCtx->ss = pHostState->ss;
2665 pCtx->ds = pHostState->ds;
2666 pCtx->gdtr = pHostState->gdtr;
2667 pCtx->idtr = pHostState->idtr;
2668 CPUMSetGuestMsrEferNoCheck(pVCpu, pCtx->msrEFER, pHostState->uEferMsr);
2669 CPUMSetGuestCR0(pVCpu, pHostState->uCr0 | X86_CR0_PE);
2670 pCtx->cr3 = pHostState->uCr3;
2671 CPUMSetGuestCR4(pVCpu, pHostState->uCr4);
2672 pCtx->rflags = pHostState->rflags;
2673 pCtx->rflags.Bits.u1VM = 0;
2674 pCtx->rip = pHostState->uRip;
2675 pCtx->rsp = pHostState->uRsp;
2676 pCtx->rax = pHostState->uRax;
2677 pCtx->dr[7] &= ~(X86_DR7_ENABLED_MASK | X86_DR7_RAZ_MASK | X86_DR7_MBZ_MASK);
2678 pCtx->dr[7] |= X86_DR7_RA1_MASK;
2679 Assert(pCtx->ss.Attr.n.u2Dpl == 0);
2680
2681 /** @todo if RIP is not canonical or outside the CS segment limit, we need to
2682 * raise \#GP(0) in the guest. */
2683
2684 /** @todo check the loaded host-state for consistency. Figure out what
2685 * exactly this involves? */
2686}
2687
2688
2689/**
2690 * Saves the host-state to the host-state save area as part of a VMRUN.
2691 *
2692 * @param pCtx The guest-CPU context.
2693 * @param cbInstr The length of the VMRUN instruction in bytes.
2694 */
2695VMM_INT_DECL(void) CPUMSvmVmRunSaveHostState(PCPUMCTX pCtx, uint8_t cbInstr)
2696{
2697 PSVMHOSTSTATE pHostState = &pCtx->hwvirt.svm.HostState;
2698 pHostState->es = pCtx->es;
2699 pHostState->cs = pCtx->cs;
2700 pHostState->ss = pCtx->ss;
2701 pHostState->ds = pCtx->ds;
2702 pHostState->gdtr = pCtx->gdtr;
2703 pHostState->idtr = pCtx->idtr;
2704 pHostState->uEferMsr = pCtx->msrEFER;
2705 pHostState->uCr0 = pCtx->cr0;
2706 pHostState->uCr3 = pCtx->cr3;
2707 pHostState->uCr4 = pCtx->cr4;
2708 pHostState->rflags = pCtx->rflags;
2709 pHostState->uRip = pCtx->rip + cbInstr;
2710 pHostState->uRsp = pCtx->rsp;
2711 pHostState->uRax = pCtx->rax;
2712}
2713
2714
2715/**
2716 * Applies the TSC offset of a nested-guest if any and returns the new TSC
2717 * value for the guest (or nested-guest).
2718 *
2719 * @returns The TSC offset after applying any nested-guest TSC offset.
2720 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2721 * @param uTicks The guest TSC.
2722 *
2723 * @sa HMSvmNstGstApplyTscOffset.
2724 */
2725VMM_INT_DECL(uint64_t) CPUMApplyNestedGuestTscOffset(PVMCPU pVCpu, uint64_t uTicks)
2726{
2727#ifndef IN_RC
2728 PCCPUMCTX pCtx = &pVCpu->cpum.s.Guest;
2729 if (CPUMIsGuestInSvmNestedHwVirtMode(pCtx))
2730 {
2731 if (!pCtx->hwvirt.svm.fHMCachedVmcb)
2732 {
2733 PCSVMVMCB pVmcb = pCtx->hwvirt.svm.CTX_SUFF(pVmcb);
2734 return uTicks + pVmcb->ctrl.u64TSCOffset;
2735 }
2736 return HMSvmNstGstApplyTscOffset(pVCpu, uTicks);
2737 }
2738
2739 /** @todo Intel. */
2740#else
2741 RT_NOREF(pVCpu);
2742#endif
2743 return uTicks;
2744}
2745
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