VirtualBox

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

Last change on this file since 42640 was 42640, checked in by vboxsync, 12 years ago

CPUM: Stubbed MSR_IA32_BIOS_SIGN_ID and MSR_IA32_BIOS_UPDT_TRIG.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 75.6 KB
Line 
1/* $Id: CPUMAllRegs.cpp 42640 2012-08-06 23:16:14Z vboxsync $ */
2/** @file
3 * CPUM - CPU Monitor(/Manager) - Getters and Setters.
4 */
5
6/*
7 * Copyright (C) 2006-2012 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/pdm.h>
27#include <VBox/vmm/pgm.h>
28#include <VBox/vmm/mm.h>
29#if defined(VBOX_WITH_RAW_MODE) && !defined(IN_RING0)
30# include <VBox/vmm/selm.h>
31#endif
32#include "CPUMInternal.h"
33#include <VBox/vmm/vm.h>
34#include <VBox/err.h>
35#include <VBox/dis.h>
36#include <VBox/log.h>
37#include <VBox/vmm/hwaccm.h>
38#include <VBox/vmm/tm.h>
39#include <iprt/assert.h>
40#include <iprt/asm.h>
41#include <iprt/asm-amd64-x86.h>
42#ifdef IN_RING3
43#include <iprt/thread.h>
44#endif
45
46/** Disable stack frame pointer generation here. */
47#if defined(_MSC_VER) && !defined(DEBUG)
48# pragma optimize("y", off)
49#endif
50
51
52/*******************************************************************************
53* Defined Constants And Macros *
54*******************************************************************************/
55/**
56 * Converts a CPUMCPU::Guest pointer into a VMCPU pointer.
57 *
58 * @returns Pointer to the Virtual CPU.
59 * @param a_pGuestCtx Pointer to the guest context.
60 */
61#define CPUM_GUEST_CTX_TO_VMCPU(a_pGuestCtx) RT_FROM_MEMBER(a_pGuestCtx, VMCPU, cpum.s.Guest)
62
63/**
64 * Lazily loads the hidden parts of a selector register when using raw-mode.
65 */
66#if defined(VBOX_WITH_RAW_MODE) && !defined(IN_RING0)
67# define CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(a_pVCpu, a_pSReg) \
68 do \
69 { \
70 if (!CPUMSELREG_ARE_HIDDEN_PARTS_VALID(a_pVCpu, a_pSReg)) \
71 cpumGuestLazyLoadHiddenSelectorReg(a_pVCpu, a_pSReg); \
72 } while (0)
73#else
74# define CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(a_pVCpu, a_pSReg) \
75 Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(a_pVCpu, a_pSReg));
76#endif
77
78
79
80#ifdef VBOX_WITH_RAW_MODE_NOT_R0
81
82/**
83 * Does the lazy hidden selector register loading.
84 *
85 * @param pVCpu The current Virtual CPU.
86 * @param pSReg The selector register to lazily load hidden parts of.
87 */
88static void cpumGuestLazyLoadHiddenSelectorReg(PVMCPU pVCpu, PCPUMSELREG pSReg)
89{
90 Assert(!CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, pSReg));
91 Assert(!HWACCMIsEnabled(pVCpu->CTX_SUFF(pVM)));
92 Assert((uintptr_t)(pSReg - &pVCpu->cpum.s.Guest.es) < X86_SREG_COUNT);
93
94 if (pVCpu->cpum.s.Guest.eflags.Bits.u1VM)
95 {
96 /* V8086 mode - Tightly controlled environment, no question about the limit or flags. */
97 pSReg->Attr.u = 0;
98 pSReg->Attr.n.u4Type = pSReg == &pVCpu->cpum.s.Guest.cs ? X86_SEL_TYPE_ER_ACC : X86_SEL_TYPE_RW_ACC;
99 pSReg->Attr.n.u1DescType = 1; /* code/data segment */
100 pSReg->Attr.n.u2Dpl = 3;
101 pSReg->Attr.n.u1Present = 1;
102 pSReg->u32Limit = 0x0000ffff;
103 pSReg->u64Base = (uint32_t)pSReg->Sel << 4;
104 pSReg->ValidSel = pSReg->Sel;
105 pSReg->fFlags = CPUMSELREG_FLAGS_VALID;
106 /** @todo Check what the accessed bit should be (VT-x and AMD-V). */
107 }
108 else if (!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE))
109 {
110 /* Real mode - leave the limit and flags alone here, at least for now. */
111 pSReg->u64Base = (uint32_t)pSReg->Sel << 4;
112 pSReg->ValidSel = pSReg->Sel;
113 pSReg->fFlags = CPUMSELREG_FLAGS_VALID;
114 }
115 else
116 {
117 /* Protected mode - get it from the selector descriptor tables. */
118 if (!(pSReg->Sel & X86_SEL_MASK_OFF_RPL))
119 {
120 Assert(!CPUMIsGuestInLongMode(pVCpu));
121 pSReg->Sel = 0;
122 pSReg->u64Base = 0;
123 pSReg->u32Limit = 0;
124 pSReg->Attr.u = 0;
125 pSReg->ValidSel = 0;
126 pSReg->fFlags = CPUMSELREG_FLAGS_VALID;
127 /** @todo see todo in iemHlpLoadNullDataSelectorProt. */
128 }
129 else
130 SELMLoadHiddenSelectorReg(pVCpu, &pVCpu->cpum.s.Guest, pSReg);
131 }
132}
133
134
135/**
136 * Makes sure the hidden CS and SS selector registers are valid, loading them if
137 * necessary.
138 *
139 * @param pVCpu The current virtual CPU.
140 */
141VMM_INT_DECL(void) CPUMGuestLazyLoadHiddenCsAndSs(PVMCPU pVCpu)
142{
143 CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(pVCpu, &pVCpu->cpum.s.Guest.cs);
144 CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(pVCpu, &pVCpu->cpum.s.Guest.ss);
145}
146
147
148/**
149 * Loads a the hidden parts of a selector register.
150 *
151 * @param pVCpu The current virtual CPU.
152 */
153VMM_INT_DECL(void) CPUMGuestLazyLoadHiddenSelectorReg(PVMCPU pVCpu, PCPUMSELREG pSReg)
154{
155 CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(pVCpu, pSReg);
156}
157
158#endif /* VBOX_WITH_RAW_MODE_NOT_R0 */
159
160
161/**
162 * Obsolete.
163 *
164 * We don't support nested hypervisor context interrupts or traps. Life is much
165 * simpler when we don't. It's also slightly faster at times.
166 *
167 * @param pVM Handle to the virtual machine.
168 */
169VMMDECL(PCCPUMCTXCORE) CPUMGetHyperCtxCore(PVMCPU pVCpu)
170{
171 return CPUMCTX2CORE(&pVCpu->cpum.s.Hyper);
172}
173
174
175/**
176 * Gets the pointer to the hypervisor CPU context structure of a virtual CPU.
177 *
178 * @param pVCpu Pointer to the VMCPU.
179 */
180VMMDECL(PCPUMCTX) CPUMGetHyperCtxPtr(PVMCPU pVCpu)
181{
182 return &pVCpu->cpum.s.Hyper;
183}
184
185
186VMMDECL(void) CPUMSetHyperGDTR(PVMCPU pVCpu, uint32_t addr, uint16_t limit)
187{
188 pVCpu->cpum.s.Hyper.gdtr.cbGdt = limit;
189 pVCpu->cpum.s.Hyper.gdtr.pGdt = addr;
190}
191
192
193VMMDECL(void) CPUMSetHyperIDTR(PVMCPU pVCpu, uint32_t addr, uint16_t limit)
194{
195 pVCpu->cpum.s.Hyper.idtr.cbIdt = limit;
196 pVCpu->cpum.s.Hyper.idtr.pIdt = addr;
197}
198
199
200VMMDECL(void) CPUMSetHyperCR3(PVMCPU pVCpu, uint32_t cr3)
201{
202 pVCpu->cpum.s.Hyper.cr3 = cr3;
203
204#ifdef IN_RC
205 /* Update the current CR3. */
206 ASMSetCR3(cr3);
207#endif
208}
209
210VMMDECL(uint32_t) CPUMGetHyperCR3(PVMCPU pVCpu)
211{
212 return pVCpu->cpum.s.Hyper.cr3;
213}
214
215
216VMMDECL(void) CPUMSetHyperCS(PVMCPU pVCpu, RTSEL SelCS)
217{
218 pVCpu->cpum.s.Hyper.cs.Sel = SelCS;
219}
220
221
222VMMDECL(void) CPUMSetHyperDS(PVMCPU pVCpu, RTSEL SelDS)
223{
224 pVCpu->cpum.s.Hyper.ds.Sel = SelDS;
225}
226
227
228VMMDECL(void) CPUMSetHyperES(PVMCPU pVCpu, RTSEL SelES)
229{
230 pVCpu->cpum.s.Hyper.es.Sel = SelES;
231}
232
233
234VMMDECL(void) CPUMSetHyperFS(PVMCPU pVCpu, RTSEL SelFS)
235{
236 pVCpu->cpum.s.Hyper.fs.Sel = SelFS;
237}
238
239
240VMMDECL(void) CPUMSetHyperGS(PVMCPU pVCpu, RTSEL SelGS)
241{
242 pVCpu->cpum.s.Hyper.gs.Sel = SelGS;
243}
244
245
246VMMDECL(void) CPUMSetHyperSS(PVMCPU pVCpu, RTSEL SelSS)
247{
248 pVCpu->cpum.s.Hyper.ss.Sel = SelSS;
249}
250
251
252VMMDECL(void) CPUMSetHyperESP(PVMCPU pVCpu, uint32_t u32ESP)
253{
254 pVCpu->cpum.s.Hyper.esp = u32ESP;
255}
256
257
258VMMDECL(void) CPUMSetHyperEDX(PVMCPU pVCpu, uint32_t u32ESP)
259{
260 pVCpu->cpum.s.Hyper.esp = u32ESP;
261}
262
263
264VMMDECL(int) CPUMSetHyperEFlags(PVMCPU pVCpu, uint32_t Efl)
265{
266 pVCpu->cpum.s.Hyper.eflags.u32 = Efl;
267 return VINF_SUCCESS;
268}
269
270
271VMMDECL(void) CPUMSetHyperEIP(PVMCPU pVCpu, uint32_t u32EIP)
272{
273 pVCpu->cpum.s.Hyper.eip = u32EIP;
274}
275
276
277/**
278 * Used by VMMR3RawRunGC to reinitialize the general raw-mode context registers,
279 * EFLAGS and EIP prior to resuming guest execution.
280 *
281 * All general register not given as a parameter will be set to 0. The EFLAGS
282 * register will be set to sane values for C/C++ code execution with interrupts
283 * disabled and IOPL 0.
284 *
285 * @param pVCpu The current virtual CPU.
286 * @param u32EIP The EIP value.
287 * @param u32ESP The ESP value.
288 * @param u32EAX The EAX value.
289 * @param u32EDX The EDX value.
290 */
291VMM_INT_DECL(void) CPUMSetHyperState(PVMCPU pVCpu, uint32_t u32EIP, uint32_t u32ESP, uint32_t u32EAX, uint32_t u32EDX)
292{
293 pVCpu->cpum.s.Hyper.eip = u32EIP;
294 pVCpu->cpum.s.Hyper.esp = u32ESP;
295 pVCpu->cpum.s.Hyper.eax = u32EAX;
296 pVCpu->cpum.s.Hyper.edx = u32EDX;
297 pVCpu->cpum.s.Hyper.ecx = 0;
298 pVCpu->cpum.s.Hyper.ebx = 0;
299 pVCpu->cpum.s.Hyper.ebp = 0;
300 pVCpu->cpum.s.Hyper.esi = 0;
301 pVCpu->cpum.s.Hyper.edi = 0;
302 pVCpu->cpum.s.Hyper.eflags.u = X86_EFL_1;
303}
304
305
306VMMDECL(void) CPUMSetHyperTR(PVMCPU pVCpu, RTSEL SelTR)
307{
308 pVCpu->cpum.s.Hyper.tr.Sel = SelTR;
309}
310
311
312VMMDECL(void) CPUMSetHyperLDTR(PVMCPU pVCpu, RTSEL SelLDTR)
313{
314 pVCpu->cpum.s.Hyper.ldtr.Sel = SelLDTR;
315}
316
317
318VMMDECL(void) CPUMSetHyperDR0(PVMCPU pVCpu, RTGCUINTREG uDr0)
319{
320 pVCpu->cpum.s.Hyper.dr[0] = uDr0;
321 /** @todo in GC we must load it! */
322}
323
324
325VMMDECL(void) CPUMSetHyperDR1(PVMCPU pVCpu, RTGCUINTREG uDr1)
326{
327 pVCpu->cpum.s.Hyper.dr[1] = uDr1;
328 /** @todo in GC we must load it! */
329}
330
331
332VMMDECL(void) CPUMSetHyperDR2(PVMCPU pVCpu, RTGCUINTREG uDr2)
333{
334 pVCpu->cpum.s.Hyper.dr[2] = uDr2;
335 /** @todo in GC we must load it! */
336}
337
338
339VMMDECL(void) CPUMSetHyperDR3(PVMCPU pVCpu, RTGCUINTREG uDr3)
340{
341 pVCpu->cpum.s.Hyper.dr[3] = uDr3;
342 /** @todo in GC we must load it! */
343}
344
345
346VMMDECL(void) CPUMSetHyperDR6(PVMCPU pVCpu, RTGCUINTREG uDr6)
347{
348 pVCpu->cpum.s.Hyper.dr[6] = uDr6;
349 /** @todo in GC we must load it! */
350}
351
352
353VMMDECL(void) CPUMSetHyperDR7(PVMCPU pVCpu, RTGCUINTREG uDr7)
354{
355 pVCpu->cpum.s.Hyper.dr[7] = uDr7;
356 /** @todo in GC we must load it! */
357}
358
359
360VMMDECL(RTSEL) CPUMGetHyperCS(PVMCPU pVCpu)
361{
362 return pVCpu->cpum.s.Hyper.cs.Sel;
363}
364
365
366VMMDECL(RTSEL) CPUMGetHyperDS(PVMCPU pVCpu)
367{
368 return pVCpu->cpum.s.Hyper.ds.Sel;
369}
370
371
372VMMDECL(RTSEL) CPUMGetHyperES(PVMCPU pVCpu)
373{
374 return pVCpu->cpum.s.Hyper.es.Sel;
375}
376
377
378VMMDECL(RTSEL) CPUMGetHyperFS(PVMCPU pVCpu)
379{
380 return pVCpu->cpum.s.Hyper.fs.Sel;
381}
382
383
384VMMDECL(RTSEL) CPUMGetHyperGS(PVMCPU pVCpu)
385{
386 return pVCpu->cpum.s.Hyper.gs.Sel;
387}
388
389
390VMMDECL(RTSEL) CPUMGetHyperSS(PVMCPU pVCpu)
391{
392 return pVCpu->cpum.s.Hyper.ss.Sel;
393}
394
395
396VMMDECL(uint32_t) CPUMGetHyperEAX(PVMCPU pVCpu)
397{
398 return pVCpu->cpum.s.Hyper.eax;
399}
400
401
402VMMDECL(uint32_t) CPUMGetHyperEBX(PVMCPU pVCpu)
403{
404 return pVCpu->cpum.s.Hyper.ebx;
405}
406
407
408VMMDECL(uint32_t) CPUMGetHyperECX(PVMCPU pVCpu)
409{
410 return pVCpu->cpum.s.Hyper.ecx;
411}
412
413
414VMMDECL(uint32_t) CPUMGetHyperEDX(PVMCPU pVCpu)
415{
416 return pVCpu->cpum.s.Hyper.edx;
417}
418
419
420VMMDECL(uint32_t) CPUMGetHyperESI(PVMCPU pVCpu)
421{
422 return pVCpu->cpum.s.Hyper.esi;
423}
424
425
426VMMDECL(uint32_t) CPUMGetHyperEDI(PVMCPU pVCpu)
427{
428 return pVCpu->cpum.s.Hyper.edi;
429}
430
431
432VMMDECL(uint32_t) CPUMGetHyperEBP(PVMCPU pVCpu)
433{
434 return pVCpu->cpum.s.Hyper.ebp;
435}
436
437
438VMMDECL(uint32_t) CPUMGetHyperESP(PVMCPU pVCpu)
439{
440 return pVCpu->cpum.s.Hyper.esp;
441}
442
443
444VMMDECL(uint32_t) CPUMGetHyperEFlags(PVMCPU pVCpu)
445{
446 return pVCpu->cpum.s.Hyper.eflags.u32;
447}
448
449
450VMMDECL(uint32_t) CPUMGetHyperEIP(PVMCPU pVCpu)
451{
452 return pVCpu->cpum.s.Hyper.eip;
453}
454
455
456VMMDECL(uint64_t) CPUMGetHyperRIP(PVMCPU pVCpu)
457{
458 return pVCpu->cpum.s.Hyper.rip;
459}
460
461
462VMMDECL(uint32_t) CPUMGetHyperIDTR(PVMCPU pVCpu, uint16_t *pcbLimit)
463{
464 if (pcbLimit)
465 *pcbLimit = pVCpu->cpum.s.Hyper.idtr.cbIdt;
466 return pVCpu->cpum.s.Hyper.idtr.pIdt;
467}
468
469
470VMMDECL(uint32_t) CPUMGetHyperGDTR(PVMCPU pVCpu, uint16_t *pcbLimit)
471{
472 if (pcbLimit)
473 *pcbLimit = pVCpu->cpum.s.Hyper.gdtr.cbGdt;
474 return pVCpu->cpum.s.Hyper.gdtr.pGdt;
475}
476
477
478VMMDECL(RTSEL) CPUMGetHyperLDTR(PVMCPU pVCpu)
479{
480 return pVCpu->cpum.s.Hyper.ldtr.Sel;
481}
482
483
484VMMDECL(RTGCUINTREG) CPUMGetHyperDR0(PVMCPU pVCpu)
485{
486 return pVCpu->cpum.s.Hyper.dr[0];
487}
488
489
490VMMDECL(RTGCUINTREG) CPUMGetHyperDR1(PVMCPU pVCpu)
491{
492 return pVCpu->cpum.s.Hyper.dr[1];
493}
494
495
496VMMDECL(RTGCUINTREG) CPUMGetHyperDR2(PVMCPU pVCpu)
497{
498 return pVCpu->cpum.s.Hyper.dr[2];
499}
500
501
502VMMDECL(RTGCUINTREG) CPUMGetHyperDR3(PVMCPU pVCpu)
503{
504 return pVCpu->cpum.s.Hyper.dr[3];
505}
506
507
508VMMDECL(RTGCUINTREG) CPUMGetHyperDR6(PVMCPU pVCpu)
509{
510 return pVCpu->cpum.s.Hyper.dr[6];
511}
512
513
514VMMDECL(RTGCUINTREG) CPUMGetHyperDR7(PVMCPU pVCpu)
515{
516 return pVCpu->cpum.s.Hyper.dr[7];
517}
518
519
520/**
521 * Gets the pointer to the internal CPUMCTXCORE structure.
522 * This is only for reading in order to save a few calls.
523 *
524 * @param pVCpu Handle to the virtual cpu.
525 */
526VMMDECL(PCCPUMCTXCORE) CPUMGetGuestCtxCore(PVMCPU pVCpu)
527{
528 return CPUMCTX2CORE(&pVCpu->cpum.s.Guest);
529}
530
531
532/**
533 * Queries the pointer to the internal CPUMCTX structure.
534 *
535 * @returns The CPUMCTX pointer.
536 * @param pVCpu Handle to the virtual cpu.
537 */
538VMMDECL(PCPUMCTX) CPUMQueryGuestCtxPtr(PVMCPU pVCpu)
539{
540 return &pVCpu->cpum.s.Guest;
541}
542
543VMMDECL(int) CPUMSetGuestGDTR(PVMCPU pVCpu, uint64_t GCPtrBase, uint16_t cbLimit)
544{
545 pVCpu->cpum.s.Guest.gdtr.cbGdt = cbLimit;
546 pVCpu->cpum.s.Guest.gdtr.pGdt = GCPtrBase;
547 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_GDTR;
548 return VINF_SUCCESS; /* formality, consider it void. */
549}
550
551VMMDECL(int) CPUMSetGuestIDTR(PVMCPU pVCpu, uint64_t GCPtrBase, uint16_t cbLimit)
552{
553 pVCpu->cpum.s.Guest.idtr.cbIdt = cbLimit;
554 pVCpu->cpum.s.Guest.idtr.pIdt = GCPtrBase;
555 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_IDTR;
556 return VINF_SUCCESS; /* formality, consider it void. */
557}
558
559VMMDECL(int) CPUMSetGuestTR(PVMCPU pVCpu, uint16_t tr)
560{
561 pVCpu->cpum.s.Guest.tr.Sel = tr;
562 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_TR;
563 return VINF_SUCCESS; /* formality, consider it void. */
564}
565
566VMMDECL(int) CPUMSetGuestLDTR(PVMCPU pVCpu, uint16_t ldtr)
567{
568 pVCpu->cpum.s.Guest.ldtr.Sel = ldtr;
569 /* The caller will set more hidden bits if it has them. */
570 pVCpu->cpum.s.Guest.ldtr.ValidSel = 0;
571 pVCpu->cpum.s.Guest.ldtr.fFlags = 0;
572 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_LDTR;
573 return VINF_SUCCESS; /* formality, consider it void. */
574}
575
576
577/**
578 * Set the guest CR0.
579 *
580 * When called in GC, the hyper CR0 may be updated if that is
581 * required. The caller only has to take special action if AM,
582 * WP, PG or PE changes.
583 *
584 * @returns VINF_SUCCESS (consider it void).
585 * @param pVCpu Handle to the virtual cpu.
586 * @param cr0 The new CR0 value.
587 */
588VMMDECL(int) CPUMSetGuestCR0(PVMCPU pVCpu, uint64_t cr0)
589{
590#ifdef IN_RC
591 /*
592 * Check if we need to change hypervisor CR0 because
593 * of math stuff.
594 */
595 if ( (cr0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP))
596 != (pVCpu->cpum.s.Guest.cr0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP)))
597 {
598 if (!(pVCpu->cpum.s.fUseFlags & CPUM_USED_FPU))
599 {
600 /*
601 * We haven't saved the host FPU state yet, so TS and MT are both set
602 * and EM should be reflecting the guest EM (it always does this).
603 */
604 if ((cr0 & X86_CR0_EM) != (pVCpu->cpum.s.Guest.cr0 & X86_CR0_EM))
605 {
606 uint32_t HyperCR0 = ASMGetCR0();
607 AssertMsg((HyperCR0 & (X86_CR0_TS | X86_CR0_MP)) == (X86_CR0_TS | X86_CR0_MP), ("%#x\n", HyperCR0));
608 AssertMsg((HyperCR0 & X86_CR0_EM) == (pVCpu->cpum.s.Guest.cr0 & X86_CR0_EM), ("%#x\n", HyperCR0));
609 HyperCR0 &= ~X86_CR0_EM;
610 HyperCR0 |= cr0 & X86_CR0_EM;
611 Log(("CPUM New HyperCR0=%#x\n", HyperCR0));
612 ASMSetCR0(HyperCR0);
613 }
614# ifdef VBOX_STRICT
615 else
616 {
617 uint32_t HyperCR0 = ASMGetCR0();
618 AssertMsg((HyperCR0 & (X86_CR0_TS | X86_CR0_MP)) == (X86_CR0_TS | X86_CR0_MP), ("%#x\n", HyperCR0));
619 AssertMsg((HyperCR0 & X86_CR0_EM) == (pVCpu->cpum.s.Guest.cr0 & X86_CR0_EM), ("%#x\n", HyperCR0));
620 }
621# endif
622 }
623 else
624 {
625 /*
626 * Already saved the state, so we're just mirroring
627 * the guest flags.
628 */
629 uint32_t HyperCR0 = ASMGetCR0();
630 AssertMsg( (HyperCR0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP))
631 == (pVCpu->cpum.s.Guest.cr0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP)),
632 ("%#x %#x\n", HyperCR0, pVCpu->cpum.s.Guest.cr0));
633 HyperCR0 &= ~(X86_CR0_TS | X86_CR0_EM | X86_CR0_MP);
634 HyperCR0 |= cr0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP);
635 Log(("CPUM New HyperCR0=%#x\n", HyperCR0));
636 ASMSetCR0(HyperCR0);
637 }
638 }
639#endif /* IN_RC */
640
641 /*
642 * Check for changes causing TLB flushes (for REM).
643 * The caller is responsible for calling PGM when appropriate.
644 */
645 if ( (cr0 & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE))
646 != (pVCpu->cpum.s.Guest.cr0 & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE)))
647 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_GLOBAL_TLB_FLUSH;
648 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_CR0;
649
650 pVCpu->cpum.s.Guest.cr0 = cr0 | X86_CR0_ET;
651 return VINF_SUCCESS;
652}
653
654
655VMMDECL(int) CPUMSetGuestCR2(PVMCPU pVCpu, uint64_t cr2)
656{
657 pVCpu->cpum.s.Guest.cr2 = cr2;
658 return VINF_SUCCESS;
659}
660
661
662VMMDECL(int) CPUMSetGuestCR3(PVMCPU pVCpu, uint64_t cr3)
663{
664 pVCpu->cpum.s.Guest.cr3 = cr3;
665 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_CR3;
666 return VINF_SUCCESS;
667}
668
669
670VMMDECL(int) CPUMSetGuestCR4(PVMCPU pVCpu, uint64_t cr4)
671{
672 if ( (cr4 & (X86_CR4_PGE | X86_CR4_PAE | X86_CR4_PSE))
673 != (pVCpu->cpum.s.Guest.cr4 & (X86_CR4_PGE | X86_CR4_PAE | X86_CR4_PSE)))
674 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_GLOBAL_TLB_FLUSH;
675 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_CR4;
676 if (!CPUMSupportsFXSR(pVCpu->CTX_SUFF(pVM)))
677 cr4 &= ~X86_CR4_OSFSXR;
678 pVCpu->cpum.s.Guest.cr4 = cr4;
679 return VINF_SUCCESS;
680}
681
682
683VMMDECL(int) CPUMSetGuestEFlags(PVMCPU pVCpu, uint32_t eflags)
684{
685 pVCpu->cpum.s.Guest.eflags.u32 = eflags;
686 return VINF_SUCCESS;
687}
688
689
690VMMDECL(int) CPUMSetGuestEIP(PVMCPU pVCpu, uint32_t eip)
691{
692 pVCpu->cpum.s.Guest.eip = eip;
693 return VINF_SUCCESS;
694}
695
696
697VMMDECL(int) CPUMSetGuestEAX(PVMCPU pVCpu, uint32_t eax)
698{
699 pVCpu->cpum.s.Guest.eax = eax;
700 return VINF_SUCCESS;
701}
702
703
704VMMDECL(int) CPUMSetGuestEBX(PVMCPU pVCpu, uint32_t ebx)
705{
706 pVCpu->cpum.s.Guest.ebx = ebx;
707 return VINF_SUCCESS;
708}
709
710
711VMMDECL(int) CPUMSetGuestECX(PVMCPU pVCpu, uint32_t ecx)
712{
713 pVCpu->cpum.s.Guest.ecx = ecx;
714 return VINF_SUCCESS;
715}
716
717
718VMMDECL(int) CPUMSetGuestEDX(PVMCPU pVCpu, uint32_t edx)
719{
720 pVCpu->cpum.s.Guest.edx = edx;
721 return VINF_SUCCESS;
722}
723
724
725VMMDECL(int) CPUMSetGuestESP(PVMCPU pVCpu, uint32_t esp)
726{
727 pVCpu->cpum.s.Guest.esp = esp;
728 return VINF_SUCCESS;
729}
730
731
732VMMDECL(int) CPUMSetGuestEBP(PVMCPU pVCpu, uint32_t ebp)
733{
734 pVCpu->cpum.s.Guest.ebp = ebp;
735 return VINF_SUCCESS;
736}
737
738
739VMMDECL(int) CPUMSetGuestESI(PVMCPU pVCpu, uint32_t esi)
740{
741 pVCpu->cpum.s.Guest.esi = esi;
742 return VINF_SUCCESS;
743}
744
745
746VMMDECL(int) CPUMSetGuestEDI(PVMCPU pVCpu, uint32_t edi)
747{
748 pVCpu->cpum.s.Guest.edi = edi;
749 return VINF_SUCCESS;
750}
751
752
753VMMDECL(int) CPUMSetGuestSS(PVMCPU pVCpu, uint16_t ss)
754{
755 pVCpu->cpum.s.Guest.ss.Sel = ss;
756 return VINF_SUCCESS;
757}
758
759
760VMMDECL(int) CPUMSetGuestCS(PVMCPU pVCpu, uint16_t cs)
761{
762 pVCpu->cpum.s.Guest.cs.Sel = cs;
763 return VINF_SUCCESS;
764}
765
766
767VMMDECL(int) CPUMSetGuestDS(PVMCPU pVCpu, uint16_t ds)
768{
769 pVCpu->cpum.s.Guest.ds.Sel = ds;
770 return VINF_SUCCESS;
771}
772
773
774VMMDECL(int) CPUMSetGuestES(PVMCPU pVCpu, uint16_t es)
775{
776 pVCpu->cpum.s.Guest.es.Sel = es;
777 return VINF_SUCCESS;
778}
779
780
781VMMDECL(int) CPUMSetGuestFS(PVMCPU pVCpu, uint16_t fs)
782{
783 pVCpu->cpum.s.Guest.fs.Sel = fs;
784 return VINF_SUCCESS;
785}
786
787
788VMMDECL(int) CPUMSetGuestGS(PVMCPU pVCpu, uint16_t gs)
789{
790 pVCpu->cpum.s.Guest.gs.Sel = gs;
791 return VINF_SUCCESS;
792}
793
794
795VMMDECL(void) CPUMSetGuestEFER(PVMCPU pVCpu, uint64_t val)
796{
797 pVCpu->cpum.s.Guest.msrEFER = val;
798}
799
800
801/**
802 * Query an MSR.
803 *
804 * The caller is responsible for checking privilege if the call is the result
805 * of a RDMSR instruction. We'll do the rest.
806 *
807 * @retval VINF_SUCCESS on success.
808 * @retval VERR_CPUM_RAISE_GP_0 on failure (invalid MSR), the caller is
809 * expected to take the appropriate actions. @a *puValue is set to 0.
810 * @param pVCpu Pointer to the VMCPU.
811 * @param idMsr The MSR.
812 * @param puValue Where to return the value.
813 *
814 * @remarks This will always return the right values, even when we're in the
815 * recompiler.
816 */
817VMMDECL(int) CPUMQueryGuestMsr(PVMCPU pVCpu, uint32_t idMsr, uint64_t *puValue)
818{
819 /*
820 * If we don't indicate MSR support in the CPUID feature bits, indicate
821 * that a #GP(0) should be raised.
822 */
823 if (!(pVCpu->CTX_SUFF(pVM)->cpum.s.aGuestCpuIdStd[1].edx & X86_CPUID_FEATURE_EDX_MSR))
824 {
825 *puValue = 0;
826 return VERR_CPUM_RAISE_GP_0; /** @todo isn't \#UD more correct if not supported? */
827 }
828
829 int rc = VINF_SUCCESS;
830 uint8_t const u8Multiplier = 4;
831 switch (idMsr)
832 {
833 case MSR_IA32_TSC:
834 *puValue = TMCpuTickGet(pVCpu);
835 break;
836
837 case MSR_IA32_APICBASE:
838 rc = PDMApicGetBase(pVCpu->CTX_SUFF(pVM), puValue);
839 if (RT_SUCCESS(rc))
840 rc = VINF_SUCCESS;
841 else
842 {
843 *puValue = 0;
844 rc = VERR_CPUM_RAISE_GP_0;
845 }
846 break;
847
848 case MSR_IA32_CR_PAT:
849 *puValue = pVCpu->cpum.s.Guest.msrPAT;
850 break;
851
852 case MSR_IA32_SYSENTER_CS:
853 *puValue = pVCpu->cpum.s.Guest.SysEnter.cs;
854 break;
855
856 case MSR_IA32_SYSENTER_EIP:
857 *puValue = pVCpu->cpum.s.Guest.SysEnter.eip;
858 break;
859
860 case MSR_IA32_SYSENTER_ESP:
861 *puValue = pVCpu->cpum.s.Guest.SysEnter.esp;
862 break;
863
864 case MSR_IA32_MTRR_CAP:
865 {
866 /* This is currently a bit weird. :-) */
867 uint8_t const cVariableRangeRegs = 0;
868 bool const fSystemManagementRangeRegisters = false;
869 bool const fFixedRangeRegisters = false;
870 bool const fWriteCombiningType = false;
871 *puValue = cVariableRangeRegs
872 | (fFixedRangeRegisters ? RT_BIT_64(8) : 0)
873 | (fWriteCombiningType ? RT_BIT_64(10) : 0)
874 | (fSystemManagementRangeRegisters ? RT_BIT_64(11) : 0);
875 break;
876 }
877
878 case MSR_IA32_MTRR_DEF_TYPE:
879 *puValue = pVCpu->cpum.s.GuestMsrs.msr.MtrrDefType;
880 break;
881
882 case IA32_MTRR_FIX64K_00000:
883 *puValue = pVCpu->cpum.s.GuestMsrs.msr.MtrrFix64K_00000;
884 break;
885 case IA32_MTRR_FIX16K_80000:
886 *puValue = pVCpu->cpum.s.GuestMsrs.msr.MtrrFix16K_80000;
887 break;
888 case IA32_MTRR_FIX16K_A0000:
889 *puValue = pVCpu->cpum.s.GuestMsrs.msr.MtrrFix16K_A0000;
890 break;
891 case IA32_MTRR_FIX4K_C0000:
892 *puValue = pVCpu->cpum.s.GuestMsrs.msr.MtrrFix4K_C0000;
893 break;
894 case IA32_MTRR_FIX4K_C8000:
895 *puValue = pVCpu->cpum.s.GuestMsrs.msr.MtrrFix4K_C8000;
896 break;
897 case IA32_MTRR_FIX4K_D0000:
898 *puValue = pVCpu->cpum.s.GuestMsrs.msr.MtrrFix4K_D0000;
899 break;
900 case IA32_MTRR_FIX4K_D8000:
901 *puValue = pVCpu->cpum.s.GuestMsrs.msr.MtrrFix4K_D8000;
902 break;
903 case IA32_MTRR_FIX4K_E0000:
904 *puValue = pVCpu->cpum.s.GuestMsrs.msr.MtrrFix4K_E0000;
905 break;
906 case IA32_MTRR_FIX4K_E8000:
907 *puValue = pVCpu->cpum.s.GuestMsrs.msr.MtrrFix4K_E8000;
908 break;
909 case IA32_MTRR_FIX4K_F0000:
910 *puValue = pVCpu->cpum.s.GuestMsrs.msr.MtrrFix4K_F0000;
911 break;
912 case IA32_MTRR_FIX4K_F8000:
913 *puValue = pVCpu->cpum.s.GuestMsrs.msr.MtrrFix4K_F8000;
914 break;
915
916 case MSR_K6_EFER:
917 *puValue = pVCpu->cpum.s.Guest.msrEFER;
918 break;
919
920 case MSR_K8_SF_MASK:
921 *puValue = pVCpu->cpum.s.Guest.msrSFMASK;
922 break;
923
924 case MSR_K6_STAR:
925 *puValue = pVCpu->cpum.s.Guest.msrSTAR;
926 break;
927
928 case MSR_K8_LSTAR:
929 *puValue = pVCpu->cpum.s.Guest.msrLSTAR;
930 break;
931
932 case MSR_K8_CSTAR:
933 *puValue = pVCpu->cpum.s.Guest.msrCSTAR;
934 break;
935
936 case MSR_K8_FS_BASE:
937 *puValue = pVCpu->cpum.s.Guest.fs.u64Base;
938 break;
939
940 case MSR_K8_GS_BASE:
941 *puValue = pVCpu->cpum.s.Guest.gs.u64Base;
942 break;
943
944 case MSR_K8_KERNEL_GS_BASE:
945 *puValue = pVCpu->cpum.s.Guest.msrKERNELGSBASE;
946 break;
947
948 case MSR_K8_TSC_AUX:
949 *puValue = pVCpu->cpum.s.GuestMsrs.msr.TscAux;
950 break;
951
952 case MSR_IA32_PERF_STATUS:
953 /** @todo could really be not exactly correct, maybe use host's values */
954 *puValue = UINT64_C(1000) /* TSC increment by tick */
955 | ((uint64_t)u8Multiplier << 24) /* CPU multiplier (aka bus ratio) min */
956 | ((uint64_t)u8Multiplier << 40) /* CPU multiplier (aka bus ratio) max */;
957 break;
958
959 case MSR_IA32_FSB_CLOCK_STS:
960 /*
961 * Encoded as:
962 * 0 - 266
963 * 1 - 133
964 * 2 - 200
965 * 3 - return 166
966 * 5 - return 100
967 */
968 *puValue = (2 << 4);
969 break;
970
971 case MSR_IA32_PLATFORM_INFO:
972 *puValue = (u8Multiplier << 8) /* Flex ratio max */
973 | ((uint64_t)u8Multiplier << 40) /* Flex ratio min */;
974 break;
975
976 case MSR_IA32_THERM_STATUS:
977 /* CPU temperature relative to TCC, to actually activate, CPUID leaf 6 EAX[0] must be set */
978 *puValue = RT_BIT(31) /* validity bit */
979 | (UINT64_C(20) << 16) /* degrees till TCC */;
980 break;
981
982 case MSR_IA32_MISC_ENABLE:
983#if 0
984 /* Needs to be tested more before enabling. */
985 *puValue = pVCpu->cpum.s.GuestMsr.msr.miscEnable;
986#else
987 /* Currenty we don't allow guests to modify enable MSRs. */
988 *puValue = MSR_IA32_MISC_ENABLE_FAST_STRINGS /* by default */;
989
990 if ((pVCpu->CTX_SUFF(pVM)->cpum.s.aGuestCpuIdStd[1].ecx & X86_CPUID_FEATURE_ECX_MONITOR) != 0)
991
992 *puValue |= MSR_IA32_MISC_ENABLE_MONITOR /* if mwait/monitor available */;
993 /** @todo: add more cpuid-controlled features this way. */
994#endif
995 break;
996
997#if 0 /*def IN_RING0 */
998 case MSR_IA32_PLATFORM_ID:
999 case MSR_IA32_BIOS_SIGN_ID:
1000 if (CPUMGetCPUVendor(pVM) == CPUMCPUVENDOR_INTEL)
1001 {
1002 /* Available since the P6 family. VT-x implies that this feature is present. */
1003 if (idMsr == MSR_IA32_PLATFORM_ID)
1004 *puValue = ASMRdMsr(MSR_IA32_PLATFORM_ID);
1005 else if (idMsr == MSR_IA32_BIOS_SIGN_ID)
1006 *puValue = ASMRdMsr(MSR_IA32_BIOS_SIGN_ID);
1007 break;
1008 }
1009 /* no break */
1010#endif
1011
1012 /*
1013 * Intel specifics MSRs:
1014 */
1015 case MSR_IA32_BIOS_SIGN_ID: /* fam/mod >= 6_01 */
1016 case MSR_IA32_BIOS_UPDT_TRIG: /* fam/mod >= 6_01 */
1017 *puValue = 0;
1018 if (CPUMGetGuestCpuVendor(pVCpu->CTX_SUFF(pVM)) != CPUMCPUVENDOR_INTEL)
1019 {
1020 Log(("MSR %#x is Intel, the virtual CPU isn't an Intel one -> #GP\n", idMsr));
1021 rc = VERR_CPUM_RAISE_GP_0;
1022 }
1023 break;
1024
1025 default:
1026 /*
1027 * Hand the X2APIC range to PDM and the APIC.
1028 */
1029 if ( idMsr >= MSR_IA32_APIC_START
1030 && idMsr < MSR_IA32_APIC_END)
1031 {
1032 rc = PDMApicReadMSR(pVCpu->CTX_SUFF(pVM), pVCpu->idCpu, idMsr, puValue);
1033 if (RT_SUCCESS(rc))
1034 rc = VINF_SUCCESS;
1035 else
1036 {
1037 *puValue = 0;
1038 rc = VERR_CPUM_RAISE_GP_0;
1039 }
1040 }
1041 else
1042 {
1043 *puValue = 0;
1044 rc = VERR_CPUM_RAISE_GP_0;
1045 }
1046 break;
1047 }
1048
1049 return rc;
1050}
1051
1052
1053/**
1054 * Sets the MSR.
1055 *
1056 * The caller is responsible for checking privilege if the call is the result
1057 * of a WRMSR instruction. We'll do the rest.
1058 *
1059 * @retval VINF_SUCCESS on success.
1060 * @retval VERR_CPUM_RAISE_GP_0 on failure, the caller is expected to take the
1061 * appropriate actions.
1062 *
1063 * @param pVCpu Pointer to the VMCPU.
1064 * @param idMsr The MSR id.
1065 * @param uValue The value to set.
1066 *
1067 * @remarks Everyone changing MSR values, including the recompiler, shall do it
1068 * by calling this method. This makes sure we have current values and
1069 * that we trigger all the right actions when something changes.
1070 */
1071VMMDECL(int) CPUMSetGuestMsr(PVMCPU pVCpu, uint32_t idMsr, uint64_t uValue)
1072{
1073 /*
1074 * If we don't indicate MSR support in the CPUID feature bits, indicate
1075 * that a #GP(0) should be raised.
1076 */
1077 if (!(pVCpu->CTX_SUFF(pVM)->cpum.s.aGuestCpuIdStd[1].edx & X86_CPUID_FEATURE_EDX_MSR))
1078 return VERR_CPUM_RAISE_GP_0; /** @todo isn't \#UD more correct if not supported? */
1079
1080 int rc = VINF_SUCCESS;
1081 switch (idMsr)
1082 {
1083 case MSR_IA32_MISC_ENABLE:
1084 pVCpu->cpum.s.GuestMsrs.msr.MiscEnable = uValue;
1085 break;
1086
1087 case MSR_IA32_TSC:
1088 TMCpuTickSet(pVCpu->CTX_SUFF(pVM), pVCpu, uValue);
1089 break;
1090
1091 case MSR_IA32_APICBASE:
1092 rc = PDMApicSetBase(pVCpu->CTX_SUFF(pVM), uValue);
1093 if (rc != VINF_SUCCESS)
1094 rc = VERR_CPUM_RAISE_GP_0;
1095 break;
1096
1097 case MSR_IA32_CR_PAT:
1098 pVCpu->cpum.s.Guest.msrPAT = uValue;
1099 break;
1100
1101 case MSR_IA32_SYSENTER_CS:
1102 pVCpu->cpum.s.Guest.SysEnter.cs = uValue & 0xffff; /* 16 bits selector */
1103 break;
1104
1105 case MSR_IA32_SYSENTER_EIP:
1106 pVCpu->cpum.s.Guest.SysEnter.eip = uValue;
1107 break;
1108
1109 case MSR_IA32_SYSENTER_ESP:
1110 pVCpu->cpum.s.Guest.SysEnter.esp = uValue;
1111 break;
1112
1113 case MSR_IA32_MTRR_CAP:
1114 return VERR_CPUM_RAISE_GP_0;
1115
1116 case MSR_IA32_MTRR_DEF_TYPE:
1117 if ( (uValue & UINT64_C(0xfffffffffffff300))
1118 || ( (uValue & 0xff) != 0
1119 && (uValue & 0xff) != 1
1120 && (uValue & 0xff) != 4
1121 && (uValue & 0xff) != 5
1122 && (uValue & 0xff) != 6) )
1123 {
1124 Log(("MSR_IA32_MTRR_DEF_TYPE: #GP(0) - writing reserved value (%#llx)\n", uValue));
1125 return VERR_CPUM_RAISE_GP_0;
1126 }
1127 pVCpu->cpum.s.GuestMsrs.msr.MtrrDefType = uValue;
1128 break;
1129
1130 case IA32_MTRR_FIX64K_00000:
1131 pVCpu->cpum.s.GuestMsrs.msr.MtrrFix64K_00000 = uValue;
1132 break;
1133 case IA32_MTRR_FIX16K_80000:
1134 pVCpu->cpum.s.GuestMsrs.msr.MtrrFix16K_80000 = uValue;
1135 break;
1136 case IA32_MTRR_FIX16K_A0000:
1137 pVCpu->cpum.s.GuestMsrs.msr.MtrrFix16K_A0000 = uValue;
1138 break;
1139 case IA32_MTRR_FIX4K_C0000:
1140 pVCpu->cpum.s.GuestMsrs.msr.MtrrFix4K_C0000 = uValue;
1141 break;
1142 case IA32_MTRR_FIX4K_C8000:
1143 pVCpu->cpum.s.GuestMsrs.msr.MtrrFix4K_C8000 = uValue;
1144 break;
1145 case IA32_MTRR_FIX4K_D0000:
1146 pVCpu->cpum.s.GuestMsrs.msr.MtrrFix4K_D0000 = uValue;
1147 break;
1148 case IA32_MTRR_FIX4K_D8000:
1149 pVCpu->cpum.s.GuestMsrs.msr.MtrrFix4K_D8000 = uValue;
1150 break;
1151 case IA32_MTRR_FIX4K_E0000:
1152 pVCpu->cpum.s.GuestMsrs.msr.MtrrFix4K_E0000 = uValue;
1153 break;
1154 case IA32_MTRR_FIX4K_E8000:
1155 pVCpu->cpum.s.GuestMsrs.msr.MtrrFix4K_E8000 = uValue;
1156 break;
1157 case IA32_MTRR_FIX4K_F0000:
1158 pVCpu->cpum.s.GuestMsrs.msr.MtrrFix4K_F0000 = uValue;
1159 break;
1160 case IA32_MTRR_FIX4K_F8000:
1161 pVCpu->cpum.s.GuestMsrs.msr.MtrrFix4K_F8000 = uValue;
1162 break;
1163
1164 /*
1165 * AMD64 MSRs.
1166 */
1167 case MSR_K6_EFER:
1168 {
1169 PVM pVM = pVCpu->CTX_SUFF(pVM);
1170 uint64_t const uOldEFER = pVCpu->cpum.s.Guest.msrEFER;
1171 uint32_t const fExtFeatures = pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1172 ? pVM->cpum.s.aGuestCpuIdExt[1].edx
1173 : 0;
1174 uint64_t fMask = 0;
1175
1176 /* Filter out those bits the guest is allowed to change. (e.g. LMA is read-only) */
1177 if (fExtFeatures & X86_CPUID_EXT_FEATURE_EDX_NX)
1178 fMask |= MSR_K6_EFER_NXE;
1179 if (fExtFeatures & X86_CPUID_EXT_FEATURE_EDX_LONG_MODE)
1180 fMask |= MSR_K6_EFER_LME;
1181 if (fExtFeatures & X86_CPUID_EXT_FEATURE_EDX_SYSCALL)
1182 fMask |= MSR_K6_EFER_SCE;
1183 if (fExtFeatures & X86_CPUID_AMD_FEATURE_EDX_FFXSR)
1184 fMask |= MSR_K6_EFER_FFXSR;
1185
1186 /* Check for illegal MSR_K6_EFER_LME transitions: not allowed to change LME if
1187 paging is enabled. (AMD Arch. Programmer's Manual Volume 2: Table 14-5) */
1188 if ( (uOldEFER & MSR_K6_EFER_LME) != (uValue & fMask & MSR_K6_EFER_LME)
1189 && (pVCpu->cpum.s.Guest.cr0 & X86_CR0_PG))
1190 {
1191 Log(("Illegal MSR_K6_EFER_LME change: paging is enabled!!\n"));
1192 return VERR_CPUM_RAISE_GP_0;
1193 }
1194
1195 /* There are a few more: e.g. MSR_K6_EFER_LMSLE */
1196 AssertMsg(!(uValue & ~(MSR_K6_EFER_NXE | MSR_K6_EFER_LME | MSR_K6_EFER_LMA /* ignored anyway */ | MSR_K6_EFER_SCE | MSR_K6_EFER_FFXSR)),
1197 ("Unexpected value %RX64\n", uValue));
1198 pVCpu->cpum.s.Guest.msrEFER = (uOldEFER & ~fMask) | (uValue & fMask);
1199
1200 /* AMD64 Architecture Programmer's Manual: 15.15 TLB Control; flush the TLB
1201 if MSR_K6_EFER_NXE, MSR_K6_EFER_LME or MSR_K6_EFER_LMA are changed. */
1202 if ( (uOldEFER & (MSR_K6_EFER_NXE | MSR_K6_EFER_LME | MSR_K6_EFER_LMA))
1203 != (pVCpu->cpum.s.Guest.msrEFER & (MSR_K6_EFER_NXE | MSR_K6_EFER_LME | MSR_K6_EFER_LMA)))
1204 {
1205 /// @todo PGMFlushTLB(pVCpu, cr3, true /*fGlobal*/);
1206 HWACCMFlushTLB(pVCpu);
1207
1208 /* Notify PGM about NXE changes. */
1209 if ( (uOldEFER & MSR_K6_EFER_NXE)
1210 != (pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_NXE))
1211 PGMNotifyNxeChanged(pVCpu, !(uOldEFER & MSR_K6_EFER_NXE));
1212 }
1213 break;
1214 }
1215
1216 case MSR_K8_SF_MASK:
1217 pVCpu->cpum.s.Guest.msrSFMASK = uValue;
1218 break;
1219
1220 case MSR_K6_STAR:
1221 pVCpu->cpum.s.Guest.msrSTAR = uValue;
1222 break;
1223
1224 case MSR_K8_LSTAR:
1225 pVCpu->cpum.s.Guest.msrLSTAR = uValue;
1226 break;
1227
1228 case MSR_K8_CSTAR:
1229 pVCpu->cpum.s.Guest.msrCSTAR = uValue;
1230 break;
1231
1232 case MSR_K8_FS_BASE:
1233 pVCpu->cpum.s.Guest.fs.u64Base = uValue;
1234 break;
1235
1236 case MSR_K8_GS_BASE:
1237 pVCpu->cpum.s.Guest.gs.u64Base = uValue;
1238 break;
1239
1240 case MSR_K8_KERNEL_GS_BASE:
1241 pVCpu->cpum.s.Guest.msrKERNELGSBASE = uValue;
1242 break;
1243
1244 case MSR_K8_TSC_AUX:
1245 pVCpu->cpum.s.GuestMsrs.msr.TscAux = uValue;
1246 break;
1247
1248 /*
1249 * Intel specifics MSRs:
1250 */
1251 case MSR_IA32_BIOS_SIGN_ID: /* fam/mod >= 6_01 */
1252 case MSR_IA32_BIOS_UPDT_TRIG: /* fam/mod >= 6_01 */
1253 if (CPUMGetGuestCpuVendor(pVCpu->CTX_SUFF(pVM)) != CPUMCPUVENDOR_INTEL)
1254 {
1255 Log(("MSR %#x is Intel, the virtual CPU isn't an Intel one -> #GP\n", idMsr));
1256 return VERR_CPUM_RAISE_GP_0;
1257 }
1258 /* ignored */
1259 break;
1260
1261 default:
1262 /*
1263 * Hand the X2APIC range to PDM and the APIC.
1264 */
1265 if ( idMsr >= MSR_IA32_APIC_START
1266 && idMsr < MSR_IA32_APIC_END)
1267 {
1268 rc = PDMApicWriteMSR(pVCpu->CTX_SUFF(pVM), pVCpu->idCpu, idMsr, uValue);
1269 if (rc != VINF_SUCCESS)
1270 rc = VERR_CPUM_RAISE_GP_0;
1271 }
1272 else
1273 {
1274 /* We should actually trigger a #GP here, but don't as that might cause more trouble. */
1275 /** @todo rc = VERR_CPUM_RAISE_GP_0 */
1276 Log(("CPUMSetGuestMsr: Unknown MSR %#x attempted set to %#llx\n", idMsr, uValue));
1277 }
1278 break;
1279 }
1280 return rc;
1281}
1282
1283
1284VMMDECL(RTGCPTR) CPUMGetGuestIDTR(PVMCPU pVCpu, uint16_t *pcbLimit)
1285{
1286 if (pcbLimit)
1287 *pcbLimit = pVCpu->cpum.s.Guest.idtr.cbIdt;
1288 return pVCpu->cpum.s.Guest.idtr.pIdt;
1289}
1290
1291
1292VMMDECL(RTSEL) CPUMGetGuestTR(PVMCPU pVCpu, PCPUMSELREGHID pHidden)
1293{
1294 if (pHidden)
1295 *pHidden = pVCpu->cpum.s.Guest.tr;
1296 return pVCpu->cpum.s.Guest.tr.Sel;
1297}
1298
1299
1300VMMDECL(RTSEL) CPUMGetGuestCS(PVMCPU pVCpu)
1301{
1302 return pVCpu->cpum.s.Guest.cs.Sel;
1303}
1304
1305
1306VMMDECL(RTSEL) CPUMGetGuestDS(PVMCPU pVCpu)
1307{
1308 return pVCpu->cpum.s.Guest.ds.Sel;
1309}
1310
1311
1312VMMDECL(RTSEL) CPUMGetGuestES(PVMCPU pVCpu)
1313{
1314 return pVCpu->cpum.s.Guest.es.Sel;
1315}
1316
1317
1318VMMDECL(RTSEL) CPUMGetGuestFS(PVMCPU pVCpu)
1319{
1320 return pVCpu->cpum.s.Guest.fs.Sel;
1321}
1322
1323
1324VMMDECL(RTSEL) CPUMGetGuestGS(PVMCPU pVCpu)
1325{
1326 return pVCpu->cpum.s.Guest.gs.Sel;
1327}
1328
1329
1330VMMDECL(RTSEL) CPUMGetGuestSS(PVMCPU pVCpu)
1331{
1332 return pVCpu->cpum.s.Guest.ss.Sel;
1333}
1334
1335
1336VMMDECL(RTSEL) CPUMGetGuestLDTR(PVMCPU pVCpu)
1337{
1338 return pVCpu->cpum.s.Guest.ldtr.Sel;
1339}
1340
1341
1342VMMDECL(RTSEL) CPUMGetGuestLdtrEx(PVMCPU pVCpu, uint64_t *pGCPtrBase, uint32_t *pcbLimit)
1343{
1344 *pGCPtrBase = pVCpu->cpum.s.Guest.ldtr.u64Base;
1345 *pcbLimit = pVCpu->cpum.s.Guest.ldtr.u32Limit;
1346 return pVCpu->cpum.s.Guest.ldtr.Sel;
1347}
1348
1349
1350VMMDECL(uint64_t) CPUMGetGuestCR0(PVMCPU pVCpu)
1351{
1352 return pVCpu->cpum.s.Guest.cr0;
1353}
1354
1355
1356VMMDECL(uint64_t) CPUMGetGuestCR2(PVMCPU pVCpu)
1357{
1358 return pVCpu->cpum.s.Guest.cr2;
1359}
1360
1361
1362VMMDECL(uint64_t) CPUMGetGuestCR3(PVMCPU pVCpu)
1363{
1364 return pVCpu->cpum.s.Guest.cr3;
1365}
1366
1367
1368VMMDECL(uint64_t) CPUMGetGuestCR4(PVMCPU pVCpu)
1369{
1370 return pVCpu->cpum.s.Guest.cr4;
1371}
1372
1373
1374VMMDECL(uint64_t) CPUMGetGuestCR8(PVMCPU pVCpu)
1375{
1376 uint64_t u64;
1377 int rc = CPUMGetGuestCRx(pVCpu, DISCREG_CR8, &u64);
1378 if (RT_FAILURE(rc))
1379 u64 = 0;
1380 return u64;
1381}
1382
1383
1384VMMDECL(void) CPUMGetGuestGDTR(PVMCPU pVCpu, PVBOXGDTR pGDTR)
1385{
1386 *pGDTR = pVCpu->cpum.s.Guest.gdtr;
1387}
1388
1389
1390VMMDECL(uint32_t) CPUMGetGuestEIP(PVMCPU pVCpu)
1391{
1392 return pVCpu->cpum.s.Guest.eip;
1393}
1394
1395
1396VMMDECL(uint64_t) CPUMGetGuestRIP(PVMCPU pVCpu)
1397{
1398 return pVCpu->cpum.s.Guest.rip;
1399}
1400
1401
1402VMMDECL(uint32_t) CPUMGetGuestEAX(PVMCPU pVCpu)
1403{
1404 return pVCpu->cpum.s.Guest.eax;
1405}
1406
1407
1408VMMDECL(uint32_t) CPUMGetGuestEBX(PVMCPU pVCpu)
1409{
1410 return pVCpu->cpum.s.Guest.ebx;
1411}
1412
1413
1414VMMDECL(uint32_t) CPUMGetGuestECX(PVMCPU pVCpu)
1415{
1416 return pVCpu->cpum.s.Guest.ecx;
1417}
1418
1419
1420VMMDECL(uint32_t) CPUMGetGuestEDX(PVMCPU pVCpu)
1421{
1422 return pVCpu->cpum.s.Guest.edx;
1423}
1424
1425
1426VMMDECL(uint32_t) CPUMGetGuestESI(PVMCPU pVCpu)
1427{
1428 return pVCpu->cpum.s.Guest.esi;
1429}
1430
1431
1432VMMDECL(uint32_t) CPUMGetGuestEDI(PVMCPU pVCpu)
1433{
1434 return pVCpu->cpum.s.Guest.edi;
1435}
1436
1437
1438VMMDECL(uint32_t) CPUMGetGuestESP(PVMCPU pVCpu)
1439{
1440 return pVCpu->cpum.s.Guest.esp;
1441}
1442
1443
1444VMMDECL(uint32_t) CPUMGetGuestEBP(PVMCPU pVCpu)
1445{
1446 return pVCpu->cpum.s.Guest.ebp;
1447}
1448
1449
1450VMMDECL(uint32_t) CPUMGetGuestEFlags(PVMCPU pVCpu)
1451{
1452 return pVCpu->cpum.s.Guest.eflags.u32;
1453}
1454
1455
1456VMMDECL(int) CPUMGetGuestCRx(PVMCPU pVCpu, unsigned iReg, uint64_t *pValue)
1457{
1458 switch (iReg)
1459 {
1460 case DISCREG_CR0:
1461 *pValue = pVCpu->cpum.s.Guest.cr0;
1462 break;
1463
1464 case DISCREG_CR2:
1465 *pValue = pVCpu->cpum.s.Guest.cr2;
1466 break;
1467
1468 case DISCREG_CR3:
1469 *pValue = pVCpu->cpum.s.Guest.cr3;
1470 break;
1471
1472 case DISCREG_CR4:
1473 *pValue = pVCpu->cpum.s.Guest.cr4;
1474 break;
1475
1476 case DISCREG_CR8:
1477 {
1478 uint8_t u8Tpr;
1479 int rc = PDMApicGetTPR(pVCpu, &u8Tpr, NULL /*pfPending*/);
1480 if (RT_FAILURE(rc))
1481 {
1482 AssertMsg(rc == VERR_PDM_NO_APIC_INSTANCE, ("%Rrc\n", rc));
1483 *pValue = 0;
1484 return rc;
1485 }
1486 *pValue = u8Tpr >> 4; /* bits 7-4 contain the task priority that go in cr8, bits 3-0*/
1487 break;
1488 }
1489
1490 default:
1491 return VERR_INVALID_PARAMETER;
1492 }
1493 return VINF_SUCCESS;
1494}
1495
1496
1497VMMDECL(uint64_t) CPUMGetGuestDR0(PVMCPU pVCpu)
1498{
1499 return pVCpu->cpum.s.Guest.dr[0];
1500}
1501
1502
1503VMMDECL(uint64_t) CPUMGetGuestDR1(PVMCPU pVCpu)
1504{
1505 return pVCpu->cpum.s.Guest.dr[1];
1506}
1507
1508
1509VMMDECL(uint64_t) CPUMGetGuestDR2(PVMCPU pVCpu)
1510{
1511 return pVCpu->cpum.s.Guest.dr[2];
1512}
1513
1514
1515VMMDECL(uint64_t) CPUMGetGuestDR3(PVMCPU pVCpu)
1516{
1517 return pVCpu->cpum.s.Guest.dr[3];
1518}
1519
1520
1521VMMDECL(uint64_t) CPUMGetGuestDR6(PVMCPU pVCpu)
1522{
1523 return pVCpu->cpum.s.Guest.dr[6];
1524}
1525
1526
1527VMMDECL(uint64_t) CPUMGetGuestDR7(PVMCPU pVCpu)
1528{
1529 return pVCpu->cpum.s.Guest.dr[7];
1530}
1531
1532
1533VMMDECL(int) CPUMGetGuestDRx(PVMCPU pVCpu, uint32_t iReg, uint64_t *pValue)
1534{
1535 AssertReturn(iReg <= DISDREG_DR7, VERR_INVALID_PARAMETER);
1536 /* DR4 is an alias for DR6, and DR5 is an alias for DR7. */
1537 if (iReg == 4 || iReg == 5)
1538 iReg += 2;
1539 *pValue = pVCpu->cpum.s.Guest.dr[iReg];
1540 return VINF_SUCCESS;
1541}
1542
1543
1544VMMDECL(uint64_t) CPUMGetGuestEFER(PVMCPU pVCpu)
1545{
1546 return pVCpu->cpum.s.Guest.msrEFER;
1547}
1548
1549
1550/**
1551 * Gets a CPUID leaf.
1552 *
1553 * @param pVCpu Pointer to the VMCPU.
1554 * @param iLeaf The CPUID leaf to get.
1555 * @param pEax Where to store the EAX value.
1556 * @param pEbx Where to store the EBX value.
1557 * @param pEcx Where to store the ECX value.
1558 * @param pEdx Where to store the EDX value.
1559 */
1560VMMDECL(void) CPUMGetGuestCpuId(PVMCPU pVCpu, uint32_t iLeaf, uint32_t *pEax, uint32_t *pEbx, uint32_t *pEcx, uint32_t *pEdx)
1561{
1562 PVM pVM = pVCpu->CTX_SUFF(pVM);
1563
1564 PCCPUMCPUID pCpuId;
1565 if (iLeaf < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdStd))
1566 pCpuId = &pVM->cpum.s.aGuestCpuIdStd[iLeaf];
1567 else if (iLeaf - UINT32_C(0x80000000) < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdExt))
1568 pCpuId = &pVM->cpum.s.aGuestCpuIdExt[iLeaf - UINT32_C(0x80000000)];
1569 else if ( iLeaf - UINT32_C(0x40000000) < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdHyper)
1570 && (pVCpu->CTX_SUFF(pVM)->cpum.s.aGuestCpuIdStd[1].ecx & X86_CPUID_FEATURE_ECX_HVP))
1571 pCpuId = &pVM->cpum.s.aGuestCpuIdHyper[iLeaf - UINT32_C(0x40000000)]; /* Only report if HVP bit set. */
1572 else if (iLeaf - UINT32_C(0xc0000000) < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdCentaur))
1573 pCpuId = &pVM->cpum.s.aGuestCpuIdCentaur[iLeaf - UINT32_C(0xc0000000)];
1574 else
1575 pCpuId = &pVM->cpum.s.GuestCpuIdDef;
1576
1577 uint32_t cCurrentCacheIndex = *pEcx;
1578
1579 *pEax = pCpuId->eax;
1580 *pEbx = pCpuId->ebx;
1581 *pEcx = pCpuId->ecx;
1582 *pEdx = pCpuId->edx;
1583
1584 if ( iLeaf == 1)
1585 {
1586 /* Bits 31-24: Initial APIC ID */
1587 Assert(pVCpu->idCpu <= 255);
1588 *pEbx |= (pVCpu->idCpu << 24);
1589 }
1590
1591 if ( iLeaf == 4
1592 && cCurrentCacheIndex < 3
1593 && pVM->cpum.s.enmGuestCpuVendor == CPUMCPUVENDOR_INTEL)
1594 {
1595 uint32_t type, level, sharing, linesize,
1596 partitions, associativity, sets, cores;
1597
1598 /* For type: 1 - data cache, 2 - i-cache, 3 - unified */
1599 partitions = 1;
1600 /* Those are only to shut up compiler, as they will always
1601 get overwritten, and compiler should be able to figure that out */
1602 sets = associativity = sharing = level = 1;
1603 cores = pVM->cCpus > 32 ? 32 : pVM->cCpus;
1604 switch (cCurrentCacheIndex)
1605 {
1606 case 0:
1607 type = 1;
1608 level = 1;
1609 sharing = 1;
1610 linesize = 64;
1611 associativity = 8;
1612 sets = 64;
1613 break;
1614 case 1:
1615 level = 1;
1616 type = 2;
1617 sharing = 1;
1618 linesize = 64;
1619 associativity = 8;
1620 sets = 64;
1621 break;
1622 default: /* shut up gcc.*/
1623 AssertFailed();
1624 case 2:
1625 level = 2;
1626 type = 3;
1627 sharing = cores; /* our L2 cache is modelled as shared between all cores */
1628 linesize = 64;
1629 associativity = 24;
1630 sets = 4096;
1631 break;
1632 }
1633
1634 *pEax |= ((cores - 1) << 26) |
1635 ((sharing - 1) << 14) |
1636 (level << 5) |
1637 1;
1638 *pEbx = (linesize - 1) |
1639 ((partitions - 1) << 12) |
1640 ((associativity - 1) << 22); /* -1 encoding */
1641 *pEcx = sets - 1;
1642 }
1643
1644 Log2(("CPUMGetGuestCpuId: iLeaf=%#010x %RX32 %RX32 %RX32 %RX32\n", iLeaf, *pEax, *pEbx, *pEcx, *pEdx));
1645}
1646
1647/**
1648 * Gets a number of standard CPUID leafs.
1649 *
1650 * @returns Number of leafs.
1651 * @param pVM Pointer to the VM.
1652 * @remark Intended for PATM.
1653 */
1654VMMDECL(uint32_t) CPUMGetGuestCpuIdStdMax(PVM pVM)
1655{
1656 return RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdStd);
1657}
1658
1659
1660/**
1661 * Gets a number of extended CPUID leafs.
1662 *
1663 * @returns Number of leafs.
1664 * @param pVM Pointer to the VM.
1665 * @remark Intended for PATM.
1666 */
1667VMMDECL(uint32_t) CPUMGetGuestCpuIdExtMax(PVM pVM)
1668{
1669 return RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdExt);
1670}
1671
1672
1673/**
1674 * Gets a number of centaur CPUID leafs.
1675 *
1676 * @returns Number of leafs.
1677 * @param pVM Pointer to the VM.
1678 * @remark Intended for PATM.
1679 */
1680VMMDECL(uint32_t) CPUMGetGuestCpuIdCentaurMax(PVM pVM)
1681{
1682 return RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdCentaur);
1683}
1684
1685
1686/**
1687 * Sets a CPUID feature bit.
1688 *
1689 * @param pVM Pointer to the VM.
1690 * @param enmFeature The feature to set.
1691 */
1692VMMDECL(void) CPUMSetGuestCpuIdFeature(PVM pVM, CPUMCPUIDFEATURE enmFeature)
1693{
1694 switch (enmFeature)
1695 {
1696 /*
1697 * Set the APIC bit in both feature masks.
1698 */
1699 case CPUMCPUIDFEATURE_APIC:
1700 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1701 pVM->cpum.s.aGuestCpuIdStd[1].edx |= X86_CPUID_FEATURE_EDX_APIC;
1702 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1703 && pVM->cpum.s.enmGuestCpuVendor == CPUMCPUVENDOR_AMD)
1704 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_APIC;
1705 LogRel(("CPUMSetGuestCpuIdFeature: Enabled APIC\n"));
1706 break;
1707
1708 /*
1709 * Set the x2APIC bit in the standard feature mask.
1710 */
1711 case CPUMCPUIDFEATURE_X2APIC:
1712 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1713 pVM->cpum.s.aGuestCpuIdStd[1].ecx |= X86_CPUID_FEATURE_ECX_X2APIC;
1714 LogRel(("CPUMSetGuestCpuIdFeature: Enabled x2APIC\n"));
1715 break;
1716
1717 /*
1718 * Set the sysenter/sysexit bit in the standard feature mask.
1719 * Assumes the caller knows what it's doing! (host must support these)
1720 */
1721 case CPUMCPUIDFEATURE_SEP:
1722 {
1723 if (!(ASMCpuId_EDX(1) & X86_CPUID_FEATURE_EDX_SEP))
1724 {
1725 AssertMsgFailed(("ERROR: Can't turn on SEP when the host doesn't support it!!\n"));
1726 return;
1727 }
1728
1729 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1730 pVM->cpum.s.aGuestCpuIdStd[1].edx |= X86_CPUID_FEATURE_EDX_SEP;
1731 LogRel(("CPUMSetGuestCpuIdFeature: Enabled sysenter/exit\n"));
1732 break;
1733 }
1734
1735 /*
1736 * Set the syscall/sysret bit in the extended feature mask.
1737 * Assumes the caller knows what it's doing! (host must support these)
1738 */
1739 case CPUMCPUIDFEATURE_SYSCALL:
1740 {
1741 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax < 0x80000001
1742 || !(ASMCpuId_EDX(0x80000001) & X86_CPUID_EXT_FEATURE_EDX_SYSCALL))
1743 {
1744#if HC_ARCH_BITS == 32
1745 /* X86_CPUID_EXT_FEATURE_EDX_SYSCALL not set it seems in 32 bits mode.
1746 * Even when the cpu is capable of doing so in 64 bits mode.
1747 */
1748 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax < 0x80000001
1749 || !(ASMCpuId_EDX(0x80000001) & X86_CPUID_EXT_FEATURE_EDX_LONG_MODE)
1750 || !(ASMCpuId_EDX(1) & X86_CPUID_EXT_FEATURE_EDX_SYSCALL))
1751#endif
1752 {
1753 LogRel(("WARNING: Can't turn on SYSCALL/SYSRET when the host doesn't support it!!\n"));
1754 return;
1755 }
1756 }
1757 /* Valid for both Intel and AMD CPUs, although only in 64 bits mode for Intel. */
1758 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_EXT_FEATURE_EDX_SYSCALL;
1759 LogRel(("CPUMSetGuestCpuIdFeature: Enabled syscall/ret\n"));
1760 break;
1761 }
1762
1763 /*
1764 * Set the PAE bit in both feature masks.
1765 * Assumes the caller knows what it's doing! (host must support these)
1766 */
1767 case CPUMCPUIDFEATURE_PAE:
1768 {
1769 if (!(ASMCpuId_EDX(1) & X86_CPUID_FEATURE_EDX_PAE))
1770 {
1771 LogRel(("WARNING: Can't turn on PAE when the host doesn't support it!!\n"));
1772 return;
1773 }
1774
1775 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1776 pVM->cpum.s.aGuestCpuIdStd[1].edx |= X86_CPUID_FEATURE_EDX_PAE;
1777 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1778 && pVM->cpum.s.enmGuestCpuVendor == CPUMCPUVENDOR_AMD)
1779 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_PAE;
1780 LogRel(("CPUMSetGuestCpuIdFeature: Enabled PAE\n"));
1781 break;
1782 }
1783
1784 /*
1785 * Set the LONG MODE bit in the extended feature mask.
1786 * Assumes the caller knows what it's doing! (host must support these)
1787 */
1788 case CPUMCPUIDFEATURE_LONG_MODE:
1789 {
1790 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax < 0x80000001
1791 || !(ASMCpuId_EDX(0x80000001) & X86_CPUID_EXT_FEATURE_EDX_LONG_MODE))
1792 {
1793 LogRel(("WARNING: Can't turn on LONG MODE when the host doesn't support it!!\n"));
1794 return;
1795 }
1796
1797 /* Valid for both Intel and AMD. */
1798 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_EXT_FEATURE_EDX_LONG_MODE;
1799 LogRel(("CPUMSetGuestCpuIdFeature: Enabled LONG MODE\n"));
1800 break;
1801 }
1802
1803 /*
1804 * Set the NX/XD bit in the extended feature mask.
1805 * Assumes the caller knows what it's doing! (host must support these)
1806 */
1807 case CPUMCPUIDFEATURE_NX:
1808 {
1809 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax < 0x80000001
1810 || !(ASMCpuId_EDX(0x80000001) & X86_CPUID_EXT_FEATURE_EDX_NX))
1811 {
1812 LogRel(("WARNING: Can't turn on NX/XD when the host doesn't support it!!\n"));
1813 return;
1814 }
1815
1816 /* Valid for both Intel and AMD. */
1817 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_EXT_FEATURE_EDX_NX;
1818 LogRel(("CPUMSetGuestCpuIdFeature: Enabled NX\n"));
1819 break;
1820 }
1821
1822 /*
1823 * Set the LAHF/SAHF support in 64-bit mode.
1824 * Assumes the caller knows what it's doing! (host must support this)
1825 */
1826 case CPUMCPUIDFEATURE_LAHF:
1827 {
1828 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax < 0x80000001
1829 || !(ASMCpuId_ECX(0x80000001) & X86_CPUID_EXT_FEATURE_ECX_LAHF_SAHF))
1830 {
1831 LogRel(("WARNING: Can't turn on LAHF/SAHF when the host doesn't support it!!\n"));
1832 return;
1833 }
1834
1835 /* Valid for both Intel and AMD. */
1836 pVM->cpum.s.aGuestCpuIdExt[1].ecx |= X86_CPUID_EXT_FEATURE_ECX_LAHF_SAHF;
1837 LogRel(("CPUMSetGuestCpuIdFeature: Enabled LAHF/SAHF\n"));
1838 break;
1839 }
1840
1841 case CPUMCPUIDFEATURE_PAT:
1842 {
1843 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1844 pVM->cpum.s.aGuestCpuIdStd[1].edx |= X86_CPUID_FEATURE_EDX_PAT;
1845 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1846 && pVM->cpum.s.enmGuestCpuVendor == CPUMCPUVENDOR_AMD)
1847 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_PAT;
1848 LogRel(("CPUMClearGuestCpuIdFeature: Enabled PAT\n"));
1849 break;
1850 }
1851
1852 /*
1853 * Set the RDTSCP support bit.
1854 * Assumes the caller knows what it's doing! (host must support this)
1855 */
1856 case CPUMCPUIDFEATURE_RDTSCP:
1857 {
1858 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax < 0x80000001
1859 || !(ASMCpuId_EDX(0x80000001) & X86_CPUID_EXT_FEATURE_EDX_RDTSCP)
1860 || pVM->cpum.s.u8PortableCpuIdLevel > 0)
1861 {
1862 if (!pVM->cpum.s.u8PortableCpuIdLevel)
1863 LogRel(("WARNING: Can't turn on RDTSCP when the host doesn't support it!!\n"));
1864 return;
1865 }
1866
1867 /* Valid for both Intel and AMD. */
1868 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_EXT_FEATURE_EDX_RDTSCP;
1869 LogRel(("CPUMSetGuestCpuIdFeature: Enabled RDTSCP.\n"));
1870 break;
1871 }
1872
1873 /*
1874 * Set the Hypervisor Present bit in the standard feature mask.
1875 */
1876 case CPUMCPUIDFEATURE_HVP:
1877 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1878 pVM->cpum.s.aGuestCpuIdStd[1].ecx |= X86_CPUID_FEATURE_ECX_HVP;
1879 LogRel(("CPUMSetGuestCpuIdFeature: Enabled Hypervisor Present bit\n"));
1880 break;
1881
1882 default:
1883 AssertMsgFailed(("enmFeature=%d\n", enmFeature));
1884 break;
1885 }
1886 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1887 {
1888 PVMCPU pVCpu = &pVM->aCpus[i];
1889 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_CPUID;
1890 }
1891}
1892
1893
1894/**
1895 * Queries a CPUID feature bit.
1896 *
1897 * @returns boolean for feature presence
1898 * @param pVM Pointer to the VM.
1899 * @param enmFeature The feature to query.
1900 */
1901VMMDECL(bool) CPUMGetGuestCpuIdFeature(PVM pVM, CPUMCPUIDFEATURE enmFeature)
1902{
1903 switch (enmFeature)
1904 {
1905 case CPUMCPUIDFEATURE_PAE:
1906 {
1907 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1908 return !!(pVM->cpum.s.aGuestCpuIdStd[1].edx & X86_CPUID_FEATURE_EDX_PAE);
1909 break;
1910 }
1911
1912 case CPUMCPUIDFEATURE_NX:
1913 {
1914 if (pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001)
1915 return !!(pVM->cpum.s.aGuestCpuIdExt[1].edx & X86_CPUID_EXT_FEATURE_EDX_NX);
1916 }
1917
1918 case CPUMCPUIDFEATURE_RDTSCP:
1919 {
1920 if (pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001)
1921 return !!(pVM->cpum.s.aGuestCpuIdExt[1].edx & X86_CPUID_EXT_FEATURE_EDX_RDTSCP);
1922 break;
1923 }
1924
1925 case CPUMCPUIDFEATURE_LONG_MODE:
1926 {
1927 if (pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001)
1928 return !!(pVM->cpum.s.aGuestCpuIdExt[1].edx & X86_CPUID_EXT_FEATURE_EDX_LONG_MODE);
1929 break;
1930 }
1931
1932 default:
1933 AssertMsgFailed(("enmFeature=%d\n", enmFeature));
1934 break;
1935 }
1936 return false;
1937}
1938
1939
1940/**
1941 * Clears a CPUID feature bit.
1942 *
1943 * @param pVM Pointer to the VM.
1944 * @param enmFeature The feature to clear.
1945 */
1946VMMDECL(void) CPUMClearGuestCpuIdFeature(PVM pVM, CPUMCPUIDFEATURE enmFeature)
1947{
1948 switch (enmFeature)
1949 {
1950 /*
1951 * Set the APIC bit in both feature masks.
1952 */
1953 case CPUMCPUIDFEATURE_APIC:
1954 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1955 pVM->cpum.s.aGuestCpuIdStd[1].edx &= ~X86_CPUID_FEATURE_EDX_APIC;
1956 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1957 && pVM->cpum.s.enmGuestCpuVendor == CPUMCPUVENDOR_AMD)
1958 pVM->cpum.s.aGuestCpuIdExt[1].edx &= ~X86_CPUID_AMD_FEATURE_EDX_APIC;
1959 Log(("CPUMSetGuestCpuIdFeature: Disabled APIC\n"));
1960 break;
1961
1962 /*
1963 * Clear the x2APIC bit in the standard feature mask.
1964 */
1965 case CPUMCPUIDFEATURE_X2APIC:
1966 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1967 pVM->cpum.s.aGuestCpuIdStd[1].ecx &= ~X86_CPUID_FEATURE_ECX_X2APIC;
1968 LogRel(("CPUMSetGuestCpuIdFeature: Disabled x2APIC\n"));
1969 break;
1970
1971 case CPUMCPUIDFEATURE_PAE:
1972 {
1973 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1974 pVM->cpum.s.aGuestCpuIdStd[1].edx &= ~X86_CPUID_FEATURE_EDX_PAE;
1975 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1976 && pVM->cpum.s.enmGuestCpuVendor == CPUMCPUVENDOR_AMD)
1977 pVM->cpum.s.aGuestCpuIdExt[1].edx &= ~X86_CPUID_AMD_FEATURE_EDX_PAE;
1978 LogRel(("CPUMClearGuestCpuIdFeature: Disabled PAE!\n"));
1979 break;
1980 }
1981
1982 case CPUMCPUIDFEATURE_PAT:
1983 {
1984 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1985 pVM->cpum.s.aGuestCpuIdStd[1].edx &= ~X86_CPUID_FEATURE_EDX_PAT;
1986 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1987 && pVM->cpum.s.enmGuestCpuVendor == CPUMCPUVENDOR_AMD)
1988 pVM->cpum.s.aGuestCpuIdExt[1].edx &= ~X86_CPUID_AMD_FEATURE_EDX_PAT;
1989 LogRel(("CPUMClearGuestCpuIdFeature: Disabled PAT!\n"));
1990 break;
1991 }
1992
1993 case CPUMCPUIDFEATURE_LONG_MODE:
1994 {
1995 if (pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001)
1996 pVM->cpum.s.aGuestCpuIdExt[1].edx &= ~X86_CPUID_EXT_FEATURE_EDX_LONG_MODE;
1997 break;
1998 }
1999
2000 case CPUMCPUIDFEATURE_LAHF:
2001 {
2002 if (pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001)
2003 pVM->cpum.s.aGuestCpuIdExt[1].ecx &= ~X86_CPUID_EXT_FEATURE_ECX_LAHF_SAHF;
2004 break;
2005 }
2006
2007 case CPUMCPUIDFEATURE_RDTSCP:
2008 {
2009 if (pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001)
2010 pVM->cpum.s.aGuestCpuIdExt[1].edx &= ~X86_CPUID_EXT_FEATURE_EDX_RDTSCP;
2011 LogRel(("CPUMClearGuestCpuIdFeature: Disabled RDTSCP!\n"));
2012 break;
2013 }
2014
2015 case CPUMCPUIDFEATURE_HVP:
2016 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
2017 pVM->cpum.s.aGuestCpuIdStd[1].ecx &= ~X86_CPUID_FEATURE_ECX_HVP;
2018 break;
2019
2020 default:
2021 AssertMsgFailed(("enmFeature=%d\n", enmFeature));
2022 break;
2023 }
2024 for (VMCPUID i = 0; i < pVM->cCpus; i++)
2025 {
2026 PVMCPU pVCpu = &pVM->aCpus[i];
2027 pVCpu->cpum.s.fChanged |= CPUM_CHANGED_CPUID;
2028 }
2029}
2030
2031
2032/**
2033 * Gets the host CPU vendor.
2034 *
2035 * @returns CPU vendor.
2036 * @param pVM Pointer to the VM.
2037 */
2038VMMDECL(CPUMCPUVENDOR) CPUMGetHostCpuVendor(PVM pVM)
2039{
2040 return pVM->cpum.s.enmHostCpuVendor;
2041}
2042
2043
2044/**
2045 * Gets the CPU vendor.
2046 *
2047 * @returns CPU vendor.
2048 * @param pVM Pointer to the VM.
2049 */
2050VMMDECL(CPUMCPUVENDOR) CPUMGetGuestCpuVendor(PVM pVM)
2051{
2052 return pVM->cpum.s.enmGuestCpuVendor;
2053}
2054
2055
2056VMMDECL(int) CPUMSetGuestDR0(PVMCPU pVCpu, uint64_t uDr0)
2057{
2058 pVCpu->cpum.s.Guest.dr[0] = uDr0;
2059 return CPUMRecalcHyperDRx(pVCpu);
2060}
2061
2062
2063VMMDECL(int) CPUMSetGuestDR1(PVMCPU pVCpu, uint64_t uDr1)
2064{
2065 pVCpu->cpum.s.Guest.dr[1] = uDr1;
2066 return CPUMRecalcHyperDRx(pVCpu);
2067}
2068
2069
2070VMMDECL(int) CPUMSetGuestDR2(PVMCPU pVCpu, uint64_t uDr2)
2071{
2072 pVCpu->cpum.s.Guest.dr[2] = uDr2;
2073 return CPUMRecalcHyperDRx(pVCpu);
2074}
2075
2076
2077VMMDECL(int) CPUMSetGuestDR3(PVMCPU pVCpu, uint64_t uDr3)
2078{
2079 pVCpu->cpum.s.Guest.dr[3] = uDr3;
2080 return CPUMRecalcHyperDRx(pVCpu);
2081}
2082
2083
2084VMMDECL(int) CPUMSetGuestDR6(PVMCPU pVCpu, uint64_t uDr6)
2085{
2086 pVCpu->cpum.s.Guest.dr[6] = uDr6;
2087 return CPUMRecalcHyperDRx(pVCpu);
2088}
2089
2090
2091VMMDECL(int) CPUMSetGuestDR7(PVMCPU pVCpu, uint64_t uDr7)
2092{
2093 pVCpu->cpum.s.Guest.dr[7] = uDr7;
2094 return CPUMRecalcHyperDRx(pVCpu);
2095}
2096
2097
2098VMMDECL(int) CPUMSetGuestDRx(PVMCPU pVCpu, uint32_t iReg, uint64_t Value)
2099{
2100 AssertReturn(iReg <= DISDREG_DR7, VERR_INVALID_PARAMETER);
2101 /* DR4 is an alias for DR6, and DR5 is an alias for DR7. */
2102 if (iReg == 4 || iReg == 5)
2103 iReg += 2;
2104 pVCpu->cpum.s.Guest.dr[iReg] = Value;
2105 return CPUMRecalcHyperDRx(pVCpu);
2106}
2107
2108
2109/**
2110 * Recalculates the hypervisor DRx register values based on
2111 * current guest registers and DBGF breakpoints.
2112 *
2113 * This is called whenever a guest DRx register is modified and when DBGF
2114 * sets a hardware breakpoint. In guest context this function will reload
2115 * any (hyper) DRx registers which comes out with a different value.
2116 *
2117 * @returns VINF_SUCCESS.
2118 * @param pVCpu Pointer to the VMCPU.
2119 */
2120VMMDECL(int) CPUMRecalcHyperDRx(PVMCPU pVCpu)
2121{
2122 PVM pVM = pVCpu->CTX_SUFF(pVM);
2123
2124 /*
2125 * Compare the DR7s first.
2126 *
2127 * We only care about the enabled flags. The GE and LE flags are always
2128 * set and we don't care if the guest doesn't set them. GD is virtualized
2129 * when we dispatch #DB, we never enable it.
2130 */
2131 const RTGCUINTREG uDbgfDr7 = DBGFBpGetDR7(pVM);
2132#ifdef CPUM_VIRTUALIZE_DRX
2133 const RTGCUINTREG uGstDr7 = CPUMGetGuestDR7(pVCpu);
2134#else
2135 const RTGCUINTREG uGstDr7 = 0;
2136#endif
2137 if ((uGstDr7 | uDbgfDr7) & X86_DR7_ENABLED_MASK)
2138 {
2139 /*
2140 * Ok, something is enabled. Recalc each of the breakpoints.
2141 * Straight forward code, not optimized/minimized in any way.
2142 */
2143 RTGCUINTREG uNewDr7 = X86_DR7_GE | X86_DR7_LE | X86_DR7_MB1_MASK;
2144
2145 /* bp 0 */
2146 RTGCUINTREG uNewDr0;
2147 if (uDbgfDr7 & (X86_DR7_L0 | X86_DR7_G0))
2148 {
2149 uNewDr7 |= uDbgfDr7 & (X86_DR7_L0 | X86_DR7_G0 | X86_DR7_RW0_MASK | X86_DR7_LEN0_MASK);
2150 uNewDr0 = DBGFBpGetDR0(pVM);
2151 }
2152 else if (uGstDr7 & (X86_DR7_L0 | X86_DR7_G0))
2153 {
2154 uNewDr7 |= uGstDr7 & (X86_DR7_L0 | X86_DR7_G0 | X86_DR7_RW0_MASK | X86_DR7_LEN0_MASK);
2155 uNewDr0 = CPUMGetGuestDR0(pVCpu);
2156 }
2157 else
2158 uNewDr0 = pVCpu->cpum.s.Hyper.dr[0];
2159
2160 /* bp 1 */
2161 RTGCUINTREG uNewDr1;
2162 if (uDbgfDr7 & (X86_DR7_L1 | X86_DR7_G1))
2163 {
2164 uNewDr7 |= uDbgfDr7 & (X86_DR7_L1 | X86_DR7_G1 | X86_DR7_RW1_MASK | X86_DR7_LEN1_MASK);
2165 uNewDr1 = DBGFBpGetDR1(pVM);
2166 }
2167 else if (uGstDr7 & (X86_DR7_L1 | X86_DR7_G1))
2168 {
2169 uNewDr7 |= uGstDr7 & (X86_DR7_L1 | X86_DR7_G1 | X86_DR7_RW1_MASK | X86_DR7_LEN1_MASK);
2170 uNewDr1 = CPUMGetGuestDR1(pVCpu);
2171 }
2172 else
2173 uNewDr1 = pVCpu->cpum.s.Hyper.dr[1];
2174
2175 /* bp 2 */
2176 RTGCUINTREG uNewDr2;
2177 if (uDbgfDr7 & (X86_DR7_L2 | X86_DR7_G2))
2178 {
2179 uNewDr7 |= uDbgfDr7 & (X86_DR7_L2 | X86_DR7_G2 | X86_DR7_RW2_MASK | X86_DR7_LEN2_MASK);
2180 uNewDr2 = DBGFBpGetDR2(pVM);
2181 }
2182 else if (uGstDr7 & (X86_DR7_L2 | X86_DR7_G2))
2183 {
2184 uNewDr7 |= uGstDr7 & (X86_DR7_L2 | X86_DR7_G2 | X86_DR7_RW2_MASK | X86_DR7_LEN2_MASK);
2185 uNewDr2 = CPUMGetGuestDR2(pVCpu);
2186 }
2187 else
2188 uNewDr2 = pVCpu->cpum.s.Hyper.dr[2];
2189
2190 /* bp 3 */
2191 RTGCUINTREG uNewDr3;
2192 if (uDbgfDr7 & (X86_DR7_L3 | X86_DR7_G3))
2193 {
2194 uNewDr7 |= uDbgfDr7 & (X86_DR7_L3 | X86_DR7_G3 | X86_DR7_RW3_MASK | X86_DR7_LEN3_MASK);
2195 uNewDr3 = DBGFBpGetDR3(pVM);
2196 }
2197 else if (uGstDr7 & (X86_DR7_L3 | X86_DR7_G3))
2198 {
2199 uNewDr7 |= uGstDr7 & (X86_DR7_L3 | X86_DR7_G3 | X86_DR7_RW3_MASK | X86_DR7_LEN3_MASK);
2200 uNewDr3 = CPUMGetGuestDR3(pVCpu);
2201 }
2202 else
2203 uNewDr3 = pVCpu->cpum.s.Hyper.dr[3];
2204
2205 /*
2206 * Apply the updates.
2207 */
2208#ifdef IN_RC
2209 if (!(pVCpu->cpum.s.fUseFlags & CPUM_USE_DEBUG_REGS))
2210 {
2211 /** @todo save host DBx registers. */
2212 }
2213#endif
2214 pVCpu->cpum.s.fUseFlags |= CPUM_USE_DEBUG_REGS;
2215 if (uNewDr3 != pVCpu->cpum.s.Hyper.dr[3])
2216 CPUMSetHyperDR3(pVCpu, uNewDr3);
2217 if (uNewDr2 != pVCpu->cpum.s.Hyper.dr[2])
2218 CPUMSetHyperDR2(pVCpu, uNewDr2);
2219 if (uNewDr1 != pVCpu->cpum.s.Hyper.dr[1])
2220 CPUMSetHyperDR1(pVCpu, uNewDr1);
2221 if (uNewDr0 != pVCpu->cpum.s.Hyper.dr[0])
2222 CPUMSetHyperDR0(pVCpu, uNewDr0);
2223 if (uNewDr7 != pVCpu->cpum.s.Hyper.dr[7])
2224 CPUMSetHyperDR7(pVCpu, uNewDr7);
2225 }
2226 else
2227 {
2228#ifdef IN_RC
2229 if (pVCpu->cpum.s.fUseFlags & CPUM_USE_DEBUG_REGS)
2230 {
2231 /** @todo restore host DBx registers. */
2232 }
2233#endif
2234 pVCpu->cpum.s.fUseFlags &= ~CPUM_USE_DEBUG_REGS;
2235 }
2236 Log2(("CPUMRecalcHyperDRx: fUseFlags=%#x %RGr %RGr %RGr %RGr %RGr %RGr\n",
2237 pVCpu->cpum.s.fUseFlags, pVCpu->cpum.s.Hyper.dr[0], pVCpu->cpum.s.Hyper.dr[1],
2238 pVCpu->cpum.s.Hyper.dr[2], pVCpu->cpum.s.Hyper.dr[3], pVCpu->cpum.s.Hyper.dr[6],
2239 pVCpu->cpum.s.Hyper.dr[7]));
2240
2241 return VINF_SUCCESS;
2242}
2243
2244
2245/**
2246 * Tests if the guest has No-Execute Page Protection Enabled (NXE).
2247 *
2248 * @returns true if in real mode, otherwise false.
2249 * @param pVCpu Pointer to the VMCPU.
2250 */
2251VMMDECL(bool) CPUMIsGuestNXEnabled(PVMCPU pVCpu)
2252{
2253 return !!(pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_NXE);
2254}
2255
2256
2257/**
2258 * Tests if the guest has the Page Size Extension enabled (PSE).
2259 *
2260 * @returns true if in real mode, otherwise false.
2261 * @param pVCpu Pointer to the VMCPU.
2262 */
2263VMMDECL(bool) CPUMIsGuestPageSizeExtEnabled(PVMCPU pVCpu)
2264{
2265 /* PAE or AMD64 implies support for big pages regardless of CR4.PSE */
2266 return !!(pVCpu->cpum.s.Guest.cr4 & (X86_CR4_PSE | X86_CR4_PAE));
2267}
2268
2269
2270/**
2271 * Tests if the guest has the paging enabled (PG).
2272 *
2273 * @returns true if in real mode, otherwise false.
2274 * @param pVCpu Pointer to the VMCPU.
2275 */
2276VMMDECL(bool) CPUMIsGuestPagingEnabled(PVMCPU pVCpu)
2277{
2278 return !!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PG);
2279}
2280
2281
2282/**
2283 * Tests if the guest has the paging enabled (PG).
2284 *
2285 * @returns true if in real mode, otherwise false.
2286 * @param pVCpu Pointer to the VMCPU.
2287 */
2288VMMDECL(bool) CPUMIsGuestR0WriteProtEnabled(PVMCPU pVCpu)
2289{
2290 return !!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_WP);
2291}
2292
2293
2294/**
2295 * Tests if the guest is running in real mode or not.
2296 *
2297 * @returns true if in real mode, otherwise false.
2298 * @param pVCpu Pointer to the VMCPU.
2299 */
2300VMMDECL(bool) CPUMIsGuestInRealMode(PVMCPU pVCpu)
2301{
2302 return !(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE);
2303}
2304
2305
2306/**
2307 * Tests if the guest is running in real or virtual 8086 mode.
2308 *
2309 * @returns @c true if it is, @c false if not.
2310 * @param pVCpu Pointer to the VMCPU.
2311 */
2312VMMDECL(bool) CPUMIsGuestInRealOrV86Mode(PVMCPU pVCpu)
2313{
2314 return !(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE)
2315 || pVCpu->cpum.s.Guest.eflags.Bits.u1VM; /** @todo verify that this cannot be set in long mode. */
2316}
2317
2318
2319/**
2320 * Tests if the guest is running in protected or not.
2321 *
2322 * @returns true if in protected mode, otherwise false.
2323 * @param pVCpu Pointer to the VMCPU.
2324 */
2325VMMDECL(bool) CPUMIsGuestInProtectedMode(PVMCPU pVCpu)
2326{
2327 return !!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE);
2328}
2329
2330
2331/**
2332 * Tests if the guest is running in paged protected or not.
2333 *
2334 * @returns true if in paged protected mode, otherwise false.
2335 * @param pVCpu Pointer to the VMCPU.
2336 */
2337VMMDECL(bool) CPUMIsGuestInPagedProtectedMode(PVMCPU pVCpu)
2338{
2339 return (pVCpu->cpum.s.Guest.cr0 & (X86_CR0_PE | X86_CR0_PG)) == (X86_CR0_PE | X86_CR0_PG);
2340}
2341
2342
2343/**
2344 * Tests if the guest is running in long mode or not.
2345 *
2346 * @returns true if in long mode, otherwise false.
2347 * @param pVCpu Pointer to the VMCPU.
2348 */
2349VMMDECL(bool) CPUMIsGuestInLongMode(PVMCPU pVCpu)
2350{
2351 return (pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA) == MSR_K6_EFER_LMA;
2352}
2353
2354
2355/**
2356 * Tests if the guest is running in PAE mode or not.
2357 *
2358 * @returns true if in PAE mode, otherwise false.
2359 * @param pVCpu Pointer to the VMCPU.
2360 */
2361VMMDECL(bool) CPUMIsGuestInPAEMode(PVMCPU pVCpu)
2362{
2363 return (pVCpu->cpum.s.Guest.cr4 & X86_CR4_PAE)
2364 && (pVCpu->cpum.s.Guest.cr0 & (X86_CR0_PE | X86_CR0_PG)) == (X86_CR0_PE | X86_CR0_PG)
2365 && !(pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA);
2366}
2367
2368
2369/**
2370 * Tests if the guest is running in 64 bits mode or not.
2371 *
2372 * @returns true if in 64 bits protected mode, otherwise false.
2373 * @param pVCpu The current virtual CPU.
2374 */
2375VMMDECL(bool) CPUMIsGuestIn64BitCode(PVMCPU pVCpu)
2376{
2377 if (!CPUMIsGuestInLongMode(pVCpu))
2378 return false;
2379 CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(pVCpu, &pVCpu->cpum.s.Guest.cs);
2380 return pVCpu->cpum.s.Guest.cs.Attr.n.u1Long;
2381}
2382
2383
2384/**
2385 * Helper for CPUMIsGuestIn64BitCodeEx that handles lazy resolving of hidden CS
2386 * registers.
2387 *
2388 * @returns true if in 64 bits protected mode, otherwise false.
2389 * @param pCtx Pointer to the current guest CPU context.
2390 */
2391VMM_INT_DECL(bool) CPUMIsGuestIn64BitCodeSlow(PCPUMCTX pCtx)
2392{
2393 return CPUMIsGuestIn64BitCode(CPUM_GUEST_CTX_TO_VMCPU(pCtx));
2394}
2395
2396#ifdef VBOX_WITH_RAW_MODE_NOT_R0
2397/**
2398 *
2399 * @returns @c true if we've entered raw-mode and selectors with RPL=1 are
2400 * really RPL=0, @c false if we've not (RPL=1 really is RPL=1).
2401 * @param pVCpu The current virtual CPU.
2402 */
2403VMM_INT_DECL(bool) CPUMIsGuestInRawMode(PVMCPU pVCpu)
2404{
2405 return pVCpu->cpum.s.fRawEntered;
2406}
2407#endif
2408
2409#ifdef VBOX_WITH_RAW_MODE_NOT_R0
2410/**
2411 * Updates the EFLAGS while we're in raw-mode.
2412 *
2413 * @param pVCpu Pointer to the VMCPU.
2414 * @param fEfl The new EFLAGS value.
2415 */
2416VMMDECL(void) CPUMRawSetEFlags(PVMCPU pVCpu, uint32_t fEfl)
2417{
2418 if (!pVCpu->cpum.s.fRawEntered)
2419 pVCpu->cpum.s.Guest.eflags.u32 = fEfl;
2420 else
2421 PATMRawSetEFlags(pVCpu->CTX_SUFF(pVM), CPUMCTX2CORE(&pVCpu->cpum.s.Guest), fEfl);
2422}
2423#endif /* VBOX_WITH_RAW_MODE_NOT_R0 */
2424
2425
2426/**
2427 * Gets the EFLAGS while we're in raw-mode.
2428 *
2429 * @returns The eflags.
2430 * @param pVCpu Pointer to the current virtual CPU.
2431 */
2432VMMDECL(uint32_t) CPUMRawGetEFlags(PVMCPU pVCpu)
2433{
2434#ifdef IN_RING0
2435 return pVCpu->cpum.s.Guest.eflags.u32;
2436#else
2437
2438 if (!pVCpu->cpum.s.fRawEntered)
2439 return pVCpu->cpum.s.Guest.eflags.u32;
2440 return PATMRawGetEFlags(pVCpu->CTX_SUFF(pVM), CPUMCTX2CORE(&pVCpu->cpum.s.Guest));
2441#endif
2442}
2443
2444
2445/**
2446 * Sets the specified changed flags (CPUM_CHANGED_*).
2447 *
2448 * @param pVCpu Pointer to the current virtual CPU.
2449 */
2450VMMDECL(void) CPUMSetChangedFlags(PVMCPU pVCpu, uint32_t fChangedFlags)
2451{
2452 pVCpu->cpum.s.fChanged |= fChangedFlags;
2453}
2454
2455
2456/**
2457 * Checks if the CPU supports the FXSAVE and FXRSTOR instruction.
2458 * @returns true if supported.
2459 * @returns false if not supported.
2460 * @param pVM Pointer to the VM.
2461 */
2462VMMDECL(bool) CPUMSupportsFXSR(PVM pVM)
2463{
2464 return pVM->cpum.s.CPUFeatures.edx.u1FXSR != 0;
2465}
2466
2467
2468/**
2469 * Checks if the host OS uses the SYSENTER / SYSEXIT instructions.
2470 * @returns true if used.
2471 * @returns false if not used.
2472 * @param pVM Pointer to the VM.
2473 */
2474VMMDECL(bool) CPUMIsHostUsingSysEnter(PVM pVM)
2475{
2476 return (pVM->cpum.s.fHostUseFlags & CPUM_USE_SYSENTER) != 0;
2477}
2478
2479
2480/**
2481 * Checks if the host OS uses the SYSCALL / SYSRET instructions.
2482 * @returns true if used.
2483 * @returns false if not used.
2484 * @param pVM Pointer to the VM.
2485 */
2486VMMDECL(bool) CPUMIsHostUsingSysCall(PVM pVM)
2487{
2488 return (pVM->cpum.s.fHostUseFlags & CPUM_USE_SYSCALL) != 0;
2489}
2490
2491#ifndef IN_RING3
2492
2493/**
2494 * Lazily sync in the FPU/XMM state.
2495 *
2496 * @returns VBox status code.
2497 * @param pVCpu Pointer to the VMCPU.
2498 */
2499VMMDECL(int) CPUMHandleLazyFPU(PVMCPU pVCpu)
2500{
2501 return cpumHandleLazyFPUAsm(&pVCpu->cpum.s);
2502}
2503
2504#endif /* !IN_RING3 */
2505
2506/**
2507 * Checks if we activated the FPU/XMM state of the guest OS.
2508 * @returns true if we did.
2509 * @returns false if not.
2510 * @param pVCpu Pointer to the VMCPU.
2511 */
2512VMMDECL(bool) CPUMIsGuestFPUStateActive(PVMCPU pVCpu)
2513{
2514 return (pVCpu->cpum.s.fUseFlags & CPUM_USED_FPU) != 0;
2515}
2516
2517
2518/**
2519 * Deactivate the FPU/XMM state of the guest OS.
2520 * @param pVCpu Pointer to the VMCPU.
2521 */
2522VMMDECL(void) CPUMDeactivateGuestFPUState(PVMCPU pVCpu)
2523{
2524 pVCpu->cpum.s.fUseFlags &= ~CPUM_USED_FPU;
2525}
2526
2527
2528/**
2529 * Checks if the guest debug state is active.
2530 *
2531 * @returns boolean
2532 * @param pVM Pointer to the VM.
2533 */
2534VMMDECL(bool) CPUMIsGuestDebugStateActive(PVMCPU pVCpu)
2535{
2536 return (pVCpu->cpum.s.fUseFlags & CPUM_USE_DEBUG_REGS) != 0;
2537}
2538
2539/**
2540 * Checks if the hyper debug state is active.
2541 *
2542 * @returns boolean
2543 * @param pVM Pointer to the VM.
2544 */
2545VMMDECL(bool) CPUMIsHyperDebugStateActive(PVMCPU pVCpu)
2546{
2547 return (pVCpu->cpum.s.fUseFlags & CPUM_USE_DEBUG_REGS_HYPER) != 0;
2548}
2549
2550
2551/**
2552 * Mark the guest's debug state as inactive.
2553 *
2554 * @returns boolean
2555 * @param pVM Pointer to the VM.
2556 */
2557VMMDECL(void) CPUMDeactivateGuestDebugState(PVMCPU pVCpu)
2558{
2559 pVCpu->cpum.s.fUseFlags &= ~CPUM_USE_DEBUG_REGS;
2560}
2561
2562
2563/**
2564 * Mark the hypervisor's debug state as inactive.
2565 *
2566 * @returns boolean
2567 * @param pVM Pointer to the VM.
2568 */
2569VMMDECL(void) CPUMDeactivateHyperDebugState(PVMCPU pVCpu)
2570{
2571 pVCpu->cpum.s.fUseFlags &= ~CPUM_USE_DEBUG_REGS_HYPER;
2572}
2573
2574
2575/**
2576 * Get the current privilege level of the guest.
2577 *
2578 * @returns CPL
2579 * @param pVCpu Pointer to the current virtual CPU.
2580 */
2581VMMDECL(uint32_t) CPUMGetGuestCPL(PVMCPU pVCpu)
2582{
2583 /*
2584 * CPL can reliably be found in SS.DPL (hidden regs valid) or SS if not.
2585 *
2586 * Note! We used to check CS.DPL here, assuming it was always equal to
2587 * CPL even if a conforming segment was loaded. But this truned out to
2588 * only apply to older AMD-V. With VT-x we had an ACP2 regression
2589 * during install after a far call to ring 2 with VT-x. Then on newer
2590 * AMD-V CPUs we have to move the VMCB.guest.u8CPL into cs.Attr.n.u2Dpl
2591 * as well as ss.Attr.n.u2Dpl to make this (and other) code work right.
2592 *
2593 * So, forget CS.DPL, always use SS.DPL.
2594 *
2595 * Note! The SS RPL is always equal to the CPL, while the CS RPL
2596 * isn't necessarily equal if the segment is conforming.
2597 * See section 4.11.1 in the AMD manual.
2598 */
2599 uint32_t uCpl;
2600 if (pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE)
2601 {
2602 if (!pVCpu->cpum.s.Guest.eflags.Bits.u1VM)
2603 {
2604 if (CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, &pVCpu->cpum.s.Guest.ss))
2605 uCpl = pVCpu->cpum.s.Guest.ss.Attr.n.u2Dpl;
2606 else
2607 {
2608 uCpl = (pVCpu->cpum.s.Guest.ss.Sel & X86_SEL_RPL);
2609#ifdef VBOX_WITH_RAW_MODE_NOT_R0
2610 if (uCpl == 1)
2611 uCpl = 0;
2612#endif
2613 }
2614 }
2615 else
2616 uCpl = 3; /* V86 has CPL=3; REM doesn't set DPL=3 in V8086 mode. See @bugref{5130}. */
2617 }
2618 else
2619 uCpl = 0; /* Real mode is zero; CPL set to 3 for VT-x real-mode emulation. */
2620 return uCpl;
2621}
2622
2623
2624/**
2625 * Gets the current guest CPU mode.
2626 *
2627 * If paging mode is what you need, check out PGMGetGuestMode().
2628 *
2629 * @returns The CPU mode.
2630 * @param pVCpu Pointer to the VMCPU.
2631 */
2632VMMDECL(CPUMMODE) CPUMGetGuestMode(PVMCPU pVCpu)
2633{
2634 CPUMMODE enmMode;
2635 if (!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE))
2636 enmMode = CPUMMODE_REAL;
2637 else if (!(pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA))
2638 enmMode = CPUMMODE_PROTECTED;
2639 else
2640 enmMode = CPUMMODE_LONG;
2641
2642 return enmMode;
2643}
2644
2645
2646/**
2647 * Figure whether the CPU is currently executing 16, 32 or 64 bit code.
2648 *
2649 * @returns 16, 32 or 64.
2650 * @param pVCpu The current virtual CPU.
2651 */
2652VMMDECL(uint32_t) CPUMGetGuestCodeBits(PVMCPU pVCpu)
2653{
2654 if (!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE))
2655 return 16;
2656
2657 if (pVCpu->cpum.s.Guest.eflags.Bits.u1VM)
2658 {
2659 Assert(!(pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA));
2660 return 16;
2661 }
2662
2663 CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(pVCpu, &pVCpu->cpum.s.Guest.cs);
2664 if ( pVCpu->cpum.s.Guest.cs.Attr.n.u1Long
2665 && (pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA))
2666 return 64;
2667
2668 if (pVCpu->cpum.s.Guest.cs.Attr.n.u1DefBig)
2669 return 32;
2670
2671 return 16;
2672}
2673
2674
2675VMMDECL(DISCPUMODE) CPUMGetGuestDisMode(PVMCPU pVCpu)
2676{
2677 if (!(pVCpu->cpum.s.Guest.cr0 & X86_CR0_PE))
2678 return DISCPUMODE_16BIT;
2679
2680 if (pVCpu->cpum.s.Guest.eflags.Bits.u1VM)
2681 {
2682 Assert(!(pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA));
2683 return DISCPUMODE_16BIT;
2684 }
2685
2686 CPUMSELREG_LAZY_LOAD_HIDDEN_PARTS(pVCpu, &pVCpu->cpum.s.Guest.cs);
2687 if ( pVCpu->cpum.s.Guest.cs.Attr.n.u1Long
2688 && (pVCpu->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA))
2689 return DISCPUMODE_64BIT;
2690
2691 if (pVCpu->cpum.s.Guest.cs.Attr.n.u1DefBig)
2692 return DISCPUMODE_32BIT;
2693
2694 return DISCPUMODE_16BIT;
2695}
2696
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