VirtualBox

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

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

Fixed up and enabled Netware WP0+RO+US hack.

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