VirtualBox

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

Last change on this file since 66227 was 66227, checked in by vboxsync, 8 years ago

VMM: Nested Hw.virt: Implement SVM VMRUN and #VMEXIT in IEM.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 72.2 KB
Line 
1/* $Id: CPUMAllRegs.cpp 66227 2017-03-23 14:50:07Z vboxsync $ */
2/** @file
3 * CPUM - CPU Monitor(/Manager) - Getters and Setters.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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(!HMIsEnabled(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 (!HMIsEnabled(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 (!HMIsEnabled(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 (!HMIsEnabled(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 && !HMIsEnabled(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 /* 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 (( HMIsEnabled(pVCpu->CTX_SUFF(pVM)) && !fForceHyper ? uDbgfDr7 : (uGstDr7 | uDbgfDr7)) & X86_DR7_ENABLED_MASK)
1573 {
1574 Assert(!CPUMIsGuestDebugStateActive(pVCpu));
1575#ifdef IN_RC
1576 bool const fHmEnabled = false;
1577#elif defined(IN_RING3)
1578 bool const fHmEnabled = HMIsEnabled(pVM);
1579#endif
1580
1581 /*
1582 * Ok, something is enabled. Recalc each of the breakpoints, taking
1583 * the VM debugger ones of the guest ones. In raw-mode context we will
1584 * not allow breakpoints with values inside the hypervisor area.
1585 */
1586 RTGCUINTREG uNewDr7 = X86_DR7_GE | X86_DR7_LE | X86_DR7_RA1_MASK;
1587
1588 /* bp 0 */
1589 RTGCUINTREG uNewDr0;
1590 if (uDbgfDr7 & (X86_DR7_L0 | X86_DR7_G0))
1591 {
1592 uNewDr7 |= uDbgfDr7 & (X86_DR7_L0 | X86_DR7_G0 | X86_DR7_RW0_MASK | X86_DR7_LEN0_MASK);
1593 uNewDr0 = DBGFBpGetDR0(pVM);
1594 }
1595 else if (uGstDr7 & (X86_DR7_L0 | X86_DR7_G0))
1596 {
1597 uNewDr0 = CPUMGetGuestDR0(pVCpu);
1598#ifndef IN_RING0
1599 if (fHmEnabled && MMHyperIsInsideArea(pVM, uNewDr0))
1600 uNewDr0 = 0;
1601 else
1602#endif
1603 uNewDr7 |= uGstDr7 & (X86_DR7_L0 | X86_DR7_G0 | X86_DR7_RW0_MASK | X86_DR7_LEN0_MASK);
1604 }
1605 else
1606 uNewDr0 = 0;
1607
1608 /* bp 1 */
1609 RTGCUINTREG uNewDr1;
1610 if (uDbgfDr7 & (X86_DR7_L1 | X86_DR7_G1))
1611 {
1612 uNewDr7 |= uDbgfDr7 & (X86_DR7_L1 | X86_DR7_G1 | X86_DR7_RW1_MASK | X86_DR7_LEN1_MASK);
1613 uNewDr1 = DBGFBpGetDR1(pVM);
1614 }
1615 else if (uGstDr7 & (X86_DR7_L1 | X86_DR7_G1))
1616 {
1617 uNewDr1 = CPUMGetGuestDR1(pVCpu);
1618#ifndef IN_RING0
1619 if (fHmEnabled && MMHyperIsInsideArea(pVM, uNewDr1))
1620 uNewDr1 = 0;
1621 else
1622#endif
1623 uNewDr7 |= uGstDr7 & (X86_DR7_L1 | X86_DR7_G1 | X86_DR7_RW1_MASK | X86_DR7_LEN1_MASK);
1624 }
1625 else
1626 uNewDr1 = 0;
1627
1628 /* bp 2 */
1629 RTGCUINTREG uNewDr2;
1630 if (uDbgfDr7 & (X86_DR7_L2 | X86_DR7_G2))
1631 {
1632 uNewDr7 |= uDbgfDr7 & (X86_DR7_L2 | X86_DR7_G2 | X86_DR7_RW2_MASK | X86_DR7_LEN2_MASK);
1633 uNewDr2 = DBGFBpGetDR2(pVM);
1634 }
1635 else if (uGstDr7 & (X86_DR7_L2 | X86_DR7_G2))
1636 {
1637 uNewDr2 = CPUMGetGuestDR2(pVCpu);
1638#ifndef IN_RING0
1639 if (fHmEnabled && MMHyperIsInsideArea(pVM, uNewDr2))
1640 uNewDr2 = 0;
1641 else
1642#endif
1643 uNewDr7 |= uGstDr7 & (X86_DR7_L2 | X86_DR7_G2 | X86_DR7_RW2_MASK | X86_DR7_LEN2_MASK);
1644 }
1645 else
1646 uNewDr2 = 0;
1647
1648 /* bp 3 */
1649 RTGCUINTREG uNewDr3;
1650 if (uDbgfDr7 & (X86_DR7_L3 | X86_DR7_G3))
1651 {
1652 uNewDr7 |= uDbgfDr7 & (X86_DR7_L3 | X86_DR7_G3 | X86_DR7_RW3_MASK | X86_DR7_LEN3_MASK);
1653 uNewDr3 = DBGFBpGetDR3(pVM);
1654 }
1655 else if (uGstDr7 & (X86_DR7_L3 | X86_DR7_G3))
1656 {
1657 uNewDr3 = CPUMGetGuestDR3(pVCpu);
1658#ifndef IN_RING0
1659 if (fHmEnabled && MMHyperIsInsideArea(pVM, uNewDr3))
1660 uNewDr3 = 0;
1661 else
1662#endif
1663 uNewDr7 |= uGstDr7 & (X86_DR7_L3 | X86_DR7_G3 | X86_DR7_RW3_MASK | X86_DR7_LEN3_MASK);
1664 }
1665 else
1666 uNewDr3 = 0;
1667
1668 /*
1669 * Apply the updates.
1670 */
1671#ifdef IN_RC
1672 /* Make sure to save host registers first. */
1673 if (!(pVCpu->cpum.s.fUseFlags & CPUM_USED_DEBUG_REGS_HOST))
1674 {
1675 if (!(pVCpu->cpum.s.fUseFlags & CPUM_USE_DEBUG_REGS_HOST))
1676 {
1677 pVCpu->cpum.s.Host.dr6 = ASMGetDR6();
1678 pVCpu->cpum.s.Host.dr7 = ASMGetDR7();
1679 }
1680 pVCpu->cpum.s.Host.dr0 = ASMGetDR0();
1681 pVCpu->cpum.s.Host.dr1 = ASMGetDR1();
1682 pVCpu->cpum.s.Host.dr2 = ASMGetDR2();
1683 pVCpu->cpum.s.Host.dr3 = ASMGetDR3();
1684 pVCpu->cpum.s.fUseFlags |= CPUM_USED_DEBUG_REGS_HOST | CPUM_USE_DEBUG_REGS_HYPER | CPUM_USED_DEBUG_REGS_HYPER;
1685
1686 /* We haven't loaded any hyper DRxes yet, so we'll have to load them all now. */
1687 pVCpu->cpum.s.Hyper.dr[0] = uNewDr0;
1688 ASMSetDR0(uNewDr0);
1689 pVCpu->cpum.s.Hyper.dr[1] = uNewDr1;
1690 ASMSetDR1(uNewDr1);
1691 pVCpu->cpum.s.Hyper.dr[2] = uNewDr2;
1692 ASMSetDR2(uNewDr2);
1693 pVCpu->cpum.s.Hyper.dr[3] = uNewDr3;
1694 ASMSetDR3(uNewDr3);
1695 ASMSetDR6(X86_DR6_INIT_VAL);
1696 pVCpu->cpum.s.Hyper.dr[7] = uNewDr7;
1697 ASMSetDR7(uNewDr7);
1698 }
1699 else
1700#endif
1701 {
1702 pVCpu->cpum.s.fUseFlags |= CPUM_USE_DEBUG_REGS_HYPER;
1703 if (uNewDr3 != pVCpu->cpum.s.Hyper.dr[3])
1704 CPUMSetHyperDR3(pVCpu, uNewDr3);
1705 if (uNewDr2 != pVCpu->cpum.s.Hyper.dr[2])
1706 CPUMSetHyperDR2(pVCpu, uNewDr2);
1707 if (uNewDr1 != pVCpu->cpum.s.Hyper.dr[1])
1708 CPUMSetHyperDR1(pVCpu, uNewDr1);
1709 if (uNewDr0 != pVCpu->cpum.s.Hyper.dr[0])
1710 CPUMSetHyperDR0(pVCpu, uNewDr0);
1711 if (uNewDr7 != pVCpu->cpum.s.Hyper.dr[7])
1712 CPUMSetHyperDR7(pVCpu, uNewDr7);
1713 }
1714 }
1715#ifdef IN_RING0
1716 else if (CPUMIsGuestDebugStateActive(pVCpu))
1717 {
1718 /*
1719 * Reload the register that was modified. Normally this won't happen
1720 * as we won't intercept DRx writes when not having the hyper debug
1721 * state loaded, but in case we do for some reason we'll simply deal
1722 * with it.
1723 */
1724 switch (iGstReg)
1725 {
1726 case 0: ASMSetDR0(CPUMGetGuestDR0(pVCpu)); break;
1727 case 1: ASMSetDR1(CPUMGetGuestDR1(pVCpu)); break;
1728 case 2: ASMSetDR2(CPUMGetGuestDR2(pVCpu)); break;
1729 case 3: ASMSetDR3(CPUMGetGuestDR3(pVCpu)); break;
1730 default:
1731 AssertReturn(iGstReg != UINT8_MAX, VERR_INTERNAL_ERROR_3);
1732 }
1733 }
1734#endif
1735 else
1736 {
1737 /*
1738 * No active debug state any more. In raw-mode this means we have to
1739 * make sure DR7 has everything disabled now, if we armed it already.
1740 * In ring-0 we might end up here when just single stepping.
1741 */
1742#if defined(IN_RC) || defined(IN_RING0)
1743 if (pVCpu->cpum.s.fUseFlags & CPUM_USED_DEBUG_REGS_HYPER)
1744 {
1745# ifdef IN_RC
1746 ASMSetDR7(X86_DR7_INIT_VAL);
1747# endif
1748 if (pVCpu->cpum.s.Hyper.dr[0])
1749 ASMSetDR0(0);
1750 if (pVCpu->cpum.s.Hyper.dr[1])
1751 ASMSetDR1(0);
1752 if (pVCpu->cpum.s.Hyper.dr[2])
1753 ASMSetDR2(0);
1754 if (pVCpu->cpum.s.Hyper.dr[3])
1755 ASMSetDR3(0);
1756 pVCpu->cpum.s.fUseFlags &= ~CPUM_USED_DEBUG_REGS_HYPER;
1757 }
1758#endif
1759 pVCpu->cpum.s.fUseFlags &= ~CPUM_USE_DEBUG_REGS_HYPER;
1760
1761 /* Clear all the registers. */
1762 pVCpu->cpum.s.Hyper.dr[7] = X86_DR7_RA1_MASK;
1763 pVCpu->cpum.s.Hyper.dr[3] = 0;
1764 pVCpu->cpum.s.Hyper.dr[2] = 0;
1765 pVCpu->cpum.s.Hyper.dr[1] = 0;
1766 pVCpu->cpum.s.Hyper.dr[0] = 0;
1767
1768 }
1769 Log2(("CPUMRecalcHyperDRx: fUseFlags=%#x %RGr %RGr %RGr %RGr %RGr %RGr\n",
1770 pVCpu->cpum.s.fUseFlags, pVCpu->cpum.s.Hyper.dr[0], pVCpu->cpum.s.Hyper.dr[1],
1771 pVCpu->cpum.s.Hyper.dr[2], pVCpu->cpum.s.Hyper.dr[3], pVCpu->cpum.s.Hyper.dr[6],
1772 pVCpu->cpum.s.Hyper.dr[7]));
1773
1774 return VINF_SUCCESS;
1775}
1776
1777
1778/**
1779 * Set the guest XCR0 register.
1780 *
1781 * Will load additional state if the FPU state is already loaded (in ring-0 &
1782 * raw-mode context).
1783 *
1784 * @returns VINF_SUCCESS on success, VERR_CPUM_RAISE_GP_0 on invalid input
1785 * value.
1786 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1787 * @param uNewValue The new value.
1788 * @thread EMT(pVCpu)
1789 */
1790VMM_INT_DECL(int) CPUMSetGuestXcr0(PVMCPU pVCpu, uint64_t uNewValue)
1791{
1792 if ( (uNewValue & ~pVCpu->CTX_SUFF(pVM)->cpum.s.fXStateGuestMask) == 0
1793 /* The X87 bit cannot be cleared. */
1794 && (uNewValue & XSAVE_C_X87)
1795 /* AVX requires SSE. */
1796 && (uNewValue & (XSAVE_C_SSE | XSAVE_C_YMM)) != XSAVE_C_YMM
1797 /* AVX-512 requires YMM, SSE and all of its three components to be enabled. */
1798 && ( (uNewValue & (XSAVE_C_OPMASK | XSAVE_C_ZMM_HI256 | XSAVE_C_ZMM_16HI)) == 0
1799 || (uNewValue & (XSAVE_C_SSE | XSAVE_C_YMM | XSAVE_C_OPMASK | XSAVE_C_ZMM_HI256 | XSAVE_C_ZMM_16HI))
1800 == (XSAVE_C_SSE | XSAVE_C_YMM | XSAVE_C_OPMASK | XSAVE_C_ZMM_HI256 | XSAVE_C_ZMM_16HI) )
1801 )
1802 {
1803 pVCpu->cpum.s.Guest.aXcr[0] = uNewValue;
1804
1805 /* If more state components are enabled, we need to take care to load
1806 them if the FPU/SSE state is already loaded. May otherwise leak
1807 host state to the guest. */
1808 uint64_t fNewComponents = ~pVCpu->cpum.s.Guest.fXStateMask & uNewValue;
1809 if (fNewComponents)
1810 {
1811#if defined(IN_RING0) || defined(IN_RC)
1812 if (pVCpu->cpum.s.fUseFlags & CPUM_USED_FPU_GUEST)
1813 {
1814 if (pVCpu->cpum.s.Guest.fXStateMask != 0)
1815 /* Adding more components. */
1816 ASMXRstor(pVCpu->cpum.s.Guest.CTX_SUFF(pXState), fNewComponents);
1817 else
1818 {
1819 /* We're switching from FXSAVE/FXRSTOR to XSAVE/XRSTOR. */
1820 pVCpu->cpum.s.Guest.fXStateMask |= XSAVE_C_X87 | XSAVE_C_SSE;
1821 if (uNewValue & ~(XSAVE_C_X87 | XSAVE_C_SSE))
1822 ASMXRstor(pVCpu->cpum.s.Guest.CTX_SUFF(pXState), uNewValue & ~(XSAVE_C_X87 | XSAVE_C_SSE));
1823 }
1824 }
1825#endif
1826 pVCpu->cpum.s.Guest.fXStateMask |= uNewValue;
1827 }
1828 return VINF_SUCCESS;
1829 }
1830 return VERR_CPUM_RAISE_GP_0;
1831}
1832
1833
1834/**
1835 * Tests if the guest has No-Execute Page Protection Enabled (NXE).
1836 *
1837 * @returns true if in real mode, otherwise false.
1838 * @param pVCpu The cross context virtual CPU structure.
1839 */
1840VMMDECL(bool) CPUMIsGuestNXEnabled(PVMCPU pVCpu)
1841{
1842 return !!(pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_NXE);
1843}
1844
1845
1846/**
1847 * Tests if the guest has the Page Size Extension enabled (PSE).
1848 *
1849 * @returns true if in real mode, otherwise false.
1850 * @param pVCpu The cross context virtual CPU structure.
1851 */
1852VMMDECL(bool) CPUMIsGuestPageSizeExtEnabled(PVMCPU pVCpu)
1853{
1854 /* PAE or AMD64 implies support for big pages regardless of CR4.PSE */
1855 return !!(pVCpu->cpum.s.Guest.cr4 & (X86_CR4_PSE | X86_CR4_PAE));
1856}
1857
1858
1859/**
1860 * Tests if the guest has the paging enabled (PG).
1861 *
1862 * @returns true if in real mode, otherwise false.
1863 * @param pVCpu The cross context virtual CPU structure.
1864 */
1865VMMDECL(bool) CPUMIsGuestPagingEnabled(PVMCPU pVCpu)
1866{
1867 return !!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PG);
1868}
1869
1870
1871/**
1872 * Tests if the guest has the paging enabled (PG).
1873 *
1874 * @returns true if in real mode, otherwise false.
1875 * @param pVCpu The cross context virtual CPU structure.
1876 */
1877VMMDECL(bool) CPUMIsGuestR0WriteProtEnabled(PVMCPU pVCpu)
1878{
1879 return !!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_WP);
1880}
1881
1882
1883/**
1884 * Tests if the guest is running in real mode or not.
1885 *
1886 * @returns true if in real mode, otherwise false.
1887 * @param pVCpu The cross context virtual CPU structure.
1888 */
1889VMMDECL(bool) CPUMIsGuestInRealMode(PVMCPU pVCpu)
1890{
1891 return !(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE);
1892}
1893
1894
1895/**
1896 * Tests if the guest is running in real or virtual 8086 mode.
1897 *
1898 * @returns @c true if it is, @c false if not.
1899 * @param pVCpu The cross context virtual CPU structure.
1900 */
1901VMMDECL(bool) CPUMIsGuestInRealOrV86Mode(PVMCPU pVCpu)
1902{
1903 return !(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE)
1904 || pVCpu->cpum.s.Guest.eflags.Bits.u1VM; /** @todo verify that this cannot be set in long mode. */
1905}
1906
1907
1908/**
1909 * Tests if the guest is running in protected or not.
1910 *
1911 * @returns true if in protected mode, otherwise false.
1912 * @param pVCpu The cross context virtual CPU structure.
1913 */
1914VMMDECL(bool) CPUMIsGuestInProtectedMode(PVMCPU pVCpu)
1915{
1916 return !!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE);
1917}
1918
1919
1920/**
1921 * Tests if the guest is running in paged protected or not.
1922 *
1923 * @returns true if in paged protected mode, otherwise false.
1924 * @param pVCpu The cross context virtual CPU structure.
1925 */
1926VMMDECL(bool) CPUMIsGuestInPagedProtectedMode(PVMCPU pVCpu)
1927{
1928 return (pVCpu->cpum.s.Guest.cr0 & (X86_CR0_PE | X86_CR0_PG)) == (X86_CR0_PE | X86_CR0_PG);
1929}
1930
1931
1932/**
1933 * Tests if the guest is running in long mode or not.
1934 *
1935 * @returns true if in long mode, otherwise false.
1936 * @param pVCpu The cross context virtual CPU structure.
1937 */
1938VMMDECL(bool) CPUMIsGuestInLongMode(PVMCPU pVCpu)
1939{
1940 return (pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA) == MSR_K6_EFER_LMA;
1941}
1942
1943
1944/**
1945 * Tests if the guest is running in PAE mode or not.
1946 *
1947 * @returns true if in PAE mode, otherwise false.
1948 * @param pVCpu The cross context virtual CPU structure.
1949 */
1950VMMDECL(bool) CPUMIsGuestInPAEMode(PVMCPU pVCpu)
1951{
1952 /* Intel mentions EFER.LMA and EFER.LME in different parts of their spec. We shall use EFER.LMA rather
1953 than EFER.LME as it reflects if the CPU has entered paging with EFER.LME set. */
1954 return (pVCpu->cpum.s.Guest.cr4 & X86_CR4_PAE)
1955 && (pVCpu->cpum.s.Guest.cr0 & X86_CR0_PG)
1956 && !(pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA);
1957}
1958
1959
1960/**
1961 * Tests if the guest is running in 64 bits mode or not.
1962 *
1963 * @returns true if in 64 bits protected mode, otherwise false.
1964 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1965 */
1966VMMDECL(bool) CPUMIsGuestIn64BitCode(PVMCPU pVCpu)
1967{
1968 if (!CPUMIsGuestInLongMode(pVCpu))
1969 return false;
1970 CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(pVCpu, &pVCpu->cpum.s.Guest.cs);
1971 return pVCpu->cpum.s.Guest.cs.Attr.n.u1Long;
1972}
1973
1974
1975/**
1976 * Helper for CPUMIsGuestIn64BitCodeEx that handles lazy resolving of hidden CS
1977 * registers.
1978 *
1979 * @returns true if in 64 bits protected mode, otherwise false.
1980 * @param pCtx Pointer to the current guest CPU context.
1981 */
1982VMM_INT_DECL(bool) CPUMIsGuestIn64BitCodeSlow(PCPUMCTX pCtx)
1983{
1984 return CPUMIsGuestIn64BitCode(CPUM_GUEST_CTX_TO_VMCPU(pCtx));
1985}
1986
1987#ifdef VBOX_WITH_RAW_MODE_NOT_R0
1988
1989/**
1990 *
1991 * @returns @c true if we've entered raw-mode and selectors with RPL=1 are
1992 * really RPL=0, @c false if we've not (RPL=1 really is RPL=1).
1993 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1994 */
1995VMM_INT_DECL(bool) CPUMIsGuestInRawMode(PVMCPU pVCpu)
1996{
1997 return pVCpu->cpum.s.fRawEntered;
1998}
1999
2000/**
2001 * Transforms the guest CPU state to raw-ring mode.
2002 *
2003 * This function will change the any of the cs and ss register with DPL=0 to DPL=1.
2004 *
2005 * @returns VBox status code. (recompiler failure)
2006 * @param pVCpu The cross context virtual CPU structure.
2007 * @see @ref pg_raw
2008 */
2009VMM_INT_DECL(int) CPUMRawEnter(PVMCPU pVCpu)
2010{
2011 PVM pVM = pVCpu->CTX_SUFF(pVM);
2012
2013 Assert(!pVCpu->cpum.s.fRawEntered);
2014 Assert(!pVCpu->cpum.s.fRemEntered);
2015 PCPUMCTX pCtx = &pVCpu->cpum.s.Guest;
2016
2017 /*
2018 * Are we in Ring-0?
2019 */
2020 if ( pCtx->ss.Sel
2021 && (pCtx->ss.Sel & X86_SEL_RPL) == 0
2022 && !pCtx->eflags.Bits.u1VM)
2023 {
2024 /*
2025 * Enter execution mode.
2026 */
2027 PATMRawEnter(pVM, pCtx);
2028
2029 /*
2030 * Set CPL to Ring-1.
2031 */
2032 pCtx->ss.Sel |= 1;
2033 if ( pCtx->cs.Sel
2034 && (pCtx->cs.Sel & X86_SEL_RPL) == 0)
2035 pCtx->cs.Sel |= 1;
2036 }
2037 else
2038 {
2039# ifdef VBOX_WITH_RAW_RING1
2040 if ( EMIsRawRing1Enabled(pVM)
2041 && !pCtx->eflags.Bits.u1VM
2042 && (pCtx->ss.Sel & X86_SEL_RPL) == 1)
2043 {
2044 /* Set CPL to Ring-2. */
2045 pCtx->ss.Sel = (pCtx->ss.Sel & ~X86_SEL_RPL) | 2;
2046 if (pCtx->cs.Sel && (pCtx->cs.Sel & X86_SEL_RPL) == 1)
2047 pCtx->cs.Sel = (pCtx->cs.Sel & ~X86_SEL_RPL) | 2;
2048 }
2049# else
2050 AssertMsg((pCtx->ss.Sel & X86_SEL_RPL) >= 2 || pCtx->eflags.Bits.u1VM,
2051 ("ring-1 code not supported\n"));
2052# endif
2053 /*
2054 * PATM takes care of IOPL and IF flags for Ring-3 and Ring-2 code as well.
2055 */
2056 PATMRawEnter(pVM, pCtx);
2057 }
2058
2059 /*
2060 * Assert sanity.
2061 */
2062 AssertMsg((pCtx->eflags.u32 & X86_EFL_IF), ("X86_EFL_IF is clear\n"));
2063 AssertReleaseMsg(pCtx->eflags.Bits.u2IOPL == 0,
2064 ("X86_EFL_IOPL=%d CPL=%d\n", pCtx->eflags.Bits.u2IOPL, pCtx->ss.Sel & X86_SEL_RPL));
2065 Assert((pVCpu->cpum.s.Guest.cr0 & (X86_CR0_PG | X86_CR0_PE)) == (X86_CR0_PG | X86_CR0_PE));
2066
2067 pCtx->eflags.u32 |= X86_EFL_IF; /* paranoia */
2068
2069 pVCpu->cpum.s.fRawEntered = true;
2070 return VINF_SUCCESS;
2071}
2072
2073
2074/**
2075 * Transforms the guest CPU state from raw-ring mode to correct values.
2076 *
2077 * This function will change any selector registers with DPL=1 to DPL=0.
2078 *
2079 * @returns Adjusted rc.
2080 * @param pVCpu The cross context virtual CPU structure.
2081 * @param rc Raw mode return code
2082 * @see @ref pg_raw
2083 */
2084VMM_INT_DECL(int) CPUMRawLeave(PVMCPU pVCpu, int rc)
2085{
2086 PVM pVM = pVCpu->CTX_SUFF(pVM);
2087
2088 /*
2089 * Don't leave if we've already left (in RC).
2090 */
2091 Assert(!pVCpu->cpum.s.fRemEntered);
2092 if (!pVCpu->cpum.s.fRawEntered)
2093 return rc;
2094 pVCpu->cpum.s.fRawEntered = false;
2095
2096 PCPUMCTX pCtx = &pVCpu->cpum.s.Guest;
2097 Assert(pCtx->eflags.Bits.u1VM || (pCtx->ss.Sel & X86_SEL_RPL));
2098 AssertMsg(pCtx->eflags.Bits.u1VM || pCtx->eflags.Bits.u2IOPL < (unsigned)(pCtx->ss.Sel & X86_SEL_RPL),
2099 ("X86_EFL_IOPL=%d CPL=%d\n", pCtx->eflags.Bits.u2IOPL, pCtx->ss.Sel & X86_SEL_RPL));
2100
2101 /*
2102 * Are we executing in raw ring-1?
2103 */
2104 if ( (pCtx->ss.Sel & X86_SEL_RPL) == 1
2105 && !pCtx->eflags.Bits.u1VM)
2106 {
2107 /*
2108 * Leave execution mode.
2109 */
2110 PATMRawLeave(pVM, pCtx, rc);
2111 /* Not quite sure if this is really required, but shouldn't harm (too much anyways). */
2112 /** @todo See what happens if we remove this. */
2113 if ((pCtx->ds.Sel & X86_SEL_RPL) == 1)
2114 pCtx->ds.Sel &= ~X86_SEL_RPL;
2115 if ((pCtx->es.Sel & X86_SEL_RPL) == 1)
2116 pCtx->es.Sel &= ~X86_SEL_RPL;
2117 if ((pCtx->fs.Sel & X86_SEL_RPL) == 1)
2118 pCtx->fs.Sel &= ~X86_SEL_RPL;
2119 if ((pCtx->gs.Sel & X86_SEL_RPL) == 1)
2120 pCtx->gs.Sel &= ~X86_SEL_RPL;
2121
2122 /*
2123 * Ring-1 selector => Ring-0.
2124 */
2125 pCtx->ss.Sel &= ~X86_SEL_RPL;
2126 if ((pCtx->cs.Sel & X86_SEL_RPL) == 1)
2127 pCtx->cs.Sel &= ~X86_SEL_RPL;
2128 }
2129 else
2130 {
2131 /*
2132 * PATM is taking care of the IOPL and IF flags for us.
2133 */
2134 PATMRawLeave(pVM, pCtx, rc);
2135 if (!pCtx->eflags.Bits.u1VM)
2136 {
2137# ifdef VBOX_WITH_RAW_RING1
2138 if ( EMIsRawRing1Enabled(pVM)
2139 && (pCtx->ss.Sel & X86_SEL_RPL) == 2)
2140 {
2141 /* Not quite sure if this is really required, but shouldn't harm (too much anyways). */
2142 /** @todo See what happens if we remove this. */
2143 if ((pCtx->ds.Sel & X86_SEL_RPL) == 2)
2144 pCtx->ds.Sel = (pCtx->ds.Sel & ~X86_SEL_RPL) | 1;
2145 if ((pCtx->es.Sel & X86_SEL_RPL) == 2)
2146 pCtx->es.Sel = (pCtx->es.Sel & ~X86_SEL_RPL) | 1;
2147 if ((pCtx->fs.Sel & X86_SEL_RPL) == 2)
2148 pCtx->fs.Sel = (pCtx->fs.Sel & ~X86_SEL_RPL) | 1;
2149 if ((pCtx->gs.Sel & X86_SEL_RPL) == 2)
2150 pCtx->gs.Sel = (pCtx->gs.Sel & ~X86_SEL_RPL) | 1;
2151
2152 /*
2153 * Ring-2 selector => Ring-1.
2154 */
2155 pCtx->ss.Sel = (pCtx->ss.Sel & ~X86_SEL_RPL) | 1;
2156 if ((pCtx->cs.Sel & X86_SEL_RPL) == 2)
2157 pCtx->cs.Sel = (pCtx->cs.Sel & ~X86_SEL_RPL) | 1;
2158 }
2159 else
2160 {
2161# endif
2162 /** @todo See what happens if we remove this. */
2163 if ((pCtx->ds.Sel & X86_SEL_RPL) == 1)
2164 pCtx->ds.Sel &= ~X86_SEL_RPL;
2165 if ((pCtx->es.Sel & X86_SEL_RPL) == 1)
2166 pCtx->es.Sel &= ~X86_SEL_RPL;
2167 if ((pCtx->fs.Sel & X86_SEL_RPL) == 1)
2168 pCtx->fs.Sel &= ~X86_SEL_RPL;
2169 if ((pCtx->gs.Sel & X86_SEL_RPL) == 1)
2170 pCtx->gs.Sel &= ~X86_SEL_RPL;
2171# ifdef VBOX_WITH_RAW_RING1
2172 }
2173# endif
2174 }
2175 }
2176
2177 return rc;
2178}
2179
2180#endif /* VBOX_WITH_RAW_MODE_NOT_R0 */
2181
2182/**
2183 * Updates the EFLAGS while we're in raw-mode.
2184 *
2185 * @param pVCpu The cross context virtual CPU structure.
2186 * @param fEfl The new EFLAGS value.
2187 */
2188VMMDECL(void) CPUMRawSetEFlags(PVMCPU pVCpu, uint32_t fEfl)
2189{
2190#ifdef VBOX_WITH_RAW_MODE_NOT_R0
2191 if (pVCpu->cpum.s.fRawEntered)
2192 PATMRawSetEFlags(pVCpu->CTX_SUFF(pVM), &pVCpu->cpum.s.Guest, fEfl);
2193 else
2194#endif
2195 pVCpu->cpum.s.Guest.eflags.u32 = fEfl;
2196}
2197
2198
2199/**
2200 * Gets the EFLAGS while we're in raw-mode.
2201 *
2202 * @returns The eflags.
2203 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2204 */
2205VMMDECL(uint32_t) CPUMRawGetEFlags(PVMCPU pVCpu)
2206{
2207#ifdef VBOX_WITH_RAW_MODE_NOT_R0
2208 if (pVCpu->cpum.s.fRawEntered)
2209 return PATMRawGetEFlags(pVCpu->CTX_SUFF(pVM), &pVCpu->cpum.s.Guest);
2210#endif
2211 return pVCpu->cpum.s.Guest.eflags.u32;
2212}
2213
2214
2215/**
2216 * Sets the specified changed flags (CPUM_CHANGED_*).
2217 *
2218 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2219 * @param fChangedAdd The changed flags to add.
2220 */
2221VMMDECL(void) CPUMSetChangedFlags(PVMCPU pVCpu, uint32_t fChangedAdd)
2222{
2223 pVCpu->cpum.s.fChanged |= fChangedAdd;
2224}
2225
2226
2227/**
2228 * Checks if the CPU supports the XSAVE and XRSTOR instruction.
2229 *
2230 * @returns true if supported.
2231 * @returns false if not supported.
2232 * @param pVM The cross context VM structure.
2233 */
2234VMMDECL(bool) CPUMSupportsXSave(PVM pVM)
2235{
2236 return pVM->cpum.s.HostFeatures.fXSaveRstor != 0;
2237}
2238
2239
2240/**
2241 * Checks if the host OS uses the SYSENTER / SYSEXIT instructions.
2242 * @returns true if used.
2243 * @returns false if not used.
2244 * @param pVM The cross context VM structure.
2245 */
2246VMMDECL(bool) CPUMIsHostUsingSysEnter(PVM pVM)
2247{
2248 return RT_BOOL(pVM->cpum.s.fHostUseFlags & CPUM_USE_SYSENTER);
2249}
2250
2251
2252/**
2253 * Checks if the host OS uses the SYSCALL / SYSRET instructions.
2254 * @returns true if used.
2255 * @returns false if not used.
2256 * @param pVM The cross context VM structure.
2257 */
2258VMMDECL(bool) CPUMIsHostUsingSysCall(PVM pVM)
2259{
2260 return RT_BOOL(pVM->cpum.s.fHostUseFlags & CPUM_USE_SYSCALL);
2261}
2262
2263#ifdef IN_RC
2264
2265/**
2266 * Lazily sync in the FPU/XMM state.
2267 *
2268 * @returns VBox status code.
2269 * @param pVCpu The cross context virtual CPU structure.
2270 */
2271VMMDECL(int) CPUMHandleLazyFPU(PVMCPU pVCpu)
2272{
2273 return cpumHandleLazyFPUAsm(&pVCpu->cpum.s);
2274}
2275
2276#endif /* !IN_RC */
2277
2278/**
2279 * Checks if we activated the FPU/XMM state of the guest OS.
2280 *
2281 * This differs from CPUMIsGuestFPUStateLoaded() in that it refers to the next
2282 * time we'll be executing guest code, so it may return true for 64-on-32 when
2283 * we still haven't actually loaded the FPU status, just scheduled it to be
2284 * loaded the next time we go thru the world switcher (CPUM_SYNC_FPU_STATE).
2285 *
2286 * @returns true / false.
2287 * @param pVCpu The cross context virtual CPU structure.
2288 */
2289VMMDECL(bool) CPUMIsGuestFPUStateActive(PVMCPU pVCpu)
2290{
2291 return RT_BOOL(pVCpu->cpum.s.fUseFlags & (CPUM_USED_FPU_GUEST | CPUM_SYNC_FPU_STATE));
2292}
2293
2294
2295/**
2296 * Checks if we've really loaded the FPU/XMM state of the guest OS.
2297 *
2298 * @returns true / false.
2299 * @param pVCpu The cross context virtual CPU structure.
2300 */
2301VMMDECL(bool) CPUMIsGuestFPUStateLoaded(PVMCPU pVCpu)
2302{
2303 return RT_BOOL(pVCpu->cpum.s.fUseFlags & CPUM_USED_FPU_GUEST);
2304}
2305
2306
2307/**
2308 * Checks if we saved the FPU/XMM state of the host OS.
2309 *
2310 * @returns true / false.
2311 * @param pVCpu The cross context virtual CPU structure.
2312 */
2313VMMDECL(bool) CPUMIsHostFPUStateSaved(PVMCPU pVCpu)
2314{
2315 return RT_BOOL(pVCpu->cpum.s.fUseFlags & CPUM_USED_FPU_HOST);
2316}
2317
2318
2319/**
2320 * Checks if the guest debug state is active.
2321 *
2322 * @returns boolean
2323 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2324 */
2325VMMDECL(bool) CPUMIsGuestDebugStateActive(PVMCPU pVCpu)
2326{
2327 return RT_BOOL(pVCpu->cpum.s.fUseFlags & CPUM_USED_DEBUG_REGS_GUEST);
2328}
2329
2330
2331/**
2332 * Checks if the guest debug state is to be made active during the world-switch
2333 * (currently only used for the 32->64 switcher case).
2334 *
2335 * @returns boolean
2336 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2337 */
2338VMMDECL(bool) CPUMIsGuestDebugStateActivePending(PVMCPU pVCpu)
2339{
2340 return RT_BOOL(pVCpu->cpum.s.fUseFlags & CPUM_SYNC_DEBUG_REGS_GUEST);
2341}
2342
2343
2344/**
2345 * Checks if the hyper debug state is active.
2346 *
2347 * @returns boolean
2348 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2349 */
2350VMMDECL(bool) CPUMIsHyperDebugStateActive(PVMCPU pVCpu)
2351{
2352 return RT_BOOL(pVCpu->cpum.s.fUseFlags & CPUM_USED_DEBUG_REGS_HYPER);
2353}
2354
2355
2356/**
2357 * Checks if the hyper debug state is to be made active during the world-switch
2358 * (currently only used for the 32->64 switcher case).
2359 *
2360 * @returns boolean
2361 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2362 */
2363VMMDECL(bool) CPUMIsHyperDebugStateActivePending(PVMCPU pVCpu)
2364{
2365 return RT_BOOL(pVCpu->cpum.s.fUseFlags & CPUM_SYNC_DEBUG_REGS_HYPER);
2366}
2367
2368
2369/**
2370 * Mark the guest's debug state as inactive.
2371 *
2372 * @returns boolean
2373 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2374 * @todo This API doesn't make sense any more.
2375 */
2376VMMDECL(void) CPUMDeactivateGuestDebugState(PVMCPU pVCpu)
2377{
2378 Assert(!(pVCpu->cpum.s.fUseFlags & (CPUM_USED_DEBUG_REGS_GUEST | CPUM_USED_DEBUG_REGS_HYPER | CPUM_USED_DEBUG_REGS_HOST)));
2379 NOREF(pVCpu);
2380}
2381
2382
2383/**
2384 * Get the current privilege level of the guest.
2385 *
2386 * @returns CPL
2387 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2388 */
2389VMMDECL(uint32_t) CPUMGetGuestCPL(PVMCPU pVCpu)
2390{
2391 /*
2392 * CPL can reliably be found in SS.DPL (hidden regs valid) or SS if not.
2393 *
2394 * Note! We used to check CS.DPL here, assuming it was always equal to
2395 * CPL even if a conforming segment was loaded. But this turned out to
2396 * only apply to older AMD-V. With VT-x we had an ACP2 regression
2397 * during install after a far call to ring 2 with VT-x. Then on newer
2398 * AMD-V CPUs we have to move the VMCB.guest.u8CPL into cs.Attr.n.u2Dpl
2399 * as well as ss.Attr.n.u2Dpl to make this (and other) code work right.
2400 *
2401 * So, forget CS.DPL, always use SS.DPL.
2402 *
2403 * Note! The SS RPL is always equal to the CPL, while the CS RPL
2404 * isn't necessarily equal if the segment is conforming.
2405 * See section 4.11.1 in the AMD manual.
2406 *
2407 * Update: Where the heck does it say CS.RPL can differ from CPL other than
2408 * right after real->prot mode switch and when in V8086 mode? That
2409 * section says the RPL specified in a direct transfere (call, jmp,
2410 * ret) is not the one loaded into CS. Besides, if CS.RPL != CPL
2411 * it would be impossible for an exception handle or the iret
2412 * instruction to figure out whether SS:ESP are part of the frame
2413 * or not. VBox or qemu bug must've lead to this misconception.
2414 *
2415 * Update2: On an AMD bulldozer system here, I've no trouble loading a null
2416 * selector into SS with an RPL other than the CPL when CPL != 3 and
2417 * we're in 64-bit mode. The intel dev box doesn't allow this, on
2418 * RPL = CPL. Weird.
2419 */
2420 uint32_t uCpl;
2421 if (pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE)
2422 {
2423 if (!pVCpu->cpum.s.Guest.eflags.Bits.u1VM)
2424 {
2425 if (CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, &pVCpu->cpum.s.Guest.ss))
2426 uCpl = pVCpu->cpum.s.Guest.ss.Attr.n.u2Dpl;
2427 else
2428 {
2429 uCpl = (pVCpu->cpum.s.Guest.ss.Sel & X86_SEL_RPL);
2430#ifdef VBOX_WITH_RAW_MODE_NOT_R0
2431# ifdef VBOX_WITH_RAW_RING1
2432 if (pVCpu->cpum.s.fRawEntered)
2433 {
2434 if ( uCpl == 2
2435 && EMIsRawRing1Enabled(pVCpu->CTX_SUFF(pVM)))
2436 uCpl = 1;
2437 else if (uCpl == 1)
2438 uCpl = 0;
2439 }
2440 Assert(uCpl != 2); /* ring 2 support not allowed anymore. */
2441# else
2442 if (uCpl == 1)
2443 uCpl = 0;
2444# endif
2445#endif
2446 }
2447 }
2448 else
2449 uCpl = 3; /* V86 has CPL=3; REM doesn't set DPL=3 in V8086 mode. See @bugref{5130}. */
2450 }
2451 else
2452 uCpl = 0; /* Real mode is zero; CPL set to 3 for VT-x real-mode emulation. */
2453 return uCpl;
2454}
2455
2456
2457/**
2458 * Gets the current guest CPU mode.
2459 *
2460 * If paging mode is what you need, check out PGMGetGuestMode().
2461 *
2462 * @returns The CPU mode.
2463 * @param pVCpu The cross context virtual CPU structure.
2464 */
2465VMMDECL(CPUMMODE) CPUMGetGuestMode(PVMCPU pVCpu)
2466{
2467 CPUMMODE enmMode;
2468 if (!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE))
2469 enmMode = CPUMMODE_REAL;
2470 else if (!(pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA))
2471 enmMode = CPUMMODE_PROTECTED;
2472 else
2473 enmMode = CPUMMODE_LONG;
2474
2475 return enmMode;
2476}
2477
2478
2479/**
2480 * Figure whether the CPU is currently executing 16, 32 or 64 bit code.
2481 *
2482 * @returns 16, 32 or 64.
2483 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2484 */
2485VMMDECL(uint32_t) CPUMGetGuestCodeBits(PVMCPU pVCpu)
2486{
2487 if (!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE))
2488 return 16;
2489
2490 if (pVCpu->cpum.s.Guest.eflags.Bits.u1VM)
2491 {
2492 Assert(!(pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA));
2493 return 16;
2494 }
2495
2496 CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(pVCpu, &pVCpu->cpum.s.Guest.cs);
2497 if ( pVCpu->cpum.s.Guest.cs.Attr.n.u1Long
2498 && (pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA))
2499 return 64;
2500
2501 if (pVCpu->cpum.s.Guest.cs.Attr.n.u1DefBig)
2502 return 32;
2503
2504 return 16;
2505}
2506
2507
2508VMMDECL(DISCPUMODE) CPUMGetGuestDisMode(PVMCPU pVCpu)
2509{
2510 if (!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE))
2511 return DISCPUMODE_16BIT;
2512
2513 if (pVCpu->cpum.s.Guest.eflags.Bits.u1VM)
2514 {
2515 Assert(!(pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA));
2516 return DISCPUMODE_16BIT;
2517 }
2518
2519 CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(pVCpu, &pVCpu->cpum.s.Guest.cs);
2520 if ( pVCpu->cpum.s.Guest.cs.Attr.n.u1Long
2521 && (pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA))
2522 return DISCPUMODE_64BIT;
2523
2524 if (pVCpu->cpum.s.Guest.cs.Attr.n.u1DefBig)
2525 return DISCPUMODE_32BIT;
2526
2527 return DISCPUMODE_16BIT;
2528}
2529
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