VirtualBox

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

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

VMM: HM cleanup.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use