VirtualBox

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

Last change on this file since 13532 was 13532, checked in by vboxsync, 16 years ago

CPUMQueryGuestCtxPtr doesn't need to return a status. It can never fail.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 50.4 KB
Line 
1/* $Id: CPUMAllRegs.cpp 13532 2008-10-23 12:39:48Z vboxsync $ */
2/** @file
3 * CPUM - CPU Monitor(/Manager) - Getters and Setters.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP LOG_GROUP_CPUM
27#include <VBox/cpum.h>
28#include <VBox/patm.h>
29#include <VBox/dbgf.h>
30#include <VBox/mm.h>
31#include "CPUMInternal.h"
32#include <VBox/vm.h>
33#include <VBox/err.h>
34#include <VBox/dis.h>
35#include <VBox/log.h>
36#include <iprt/assert.h>
37#include <iprt/asm.h>
38
39
40/** Disable stack frame pointer generation here. */
41#if defined(_MSC_VER) && !defined(DEBUG)
42# pragma optimize("y", off)
43#endif
44
45
46/**
47 * Sets or resets an alternative hypervisor context core.
48 *
49 * This is called when we get a hypervisor trap set switch the context
50 * core with the trap frame on the stack. It is called again to reset
51 * back to the default context core when resuming hypervisor execution.
52 *
53 * @param pVM The VM handle.
54 * @param pCtxCore Pointer to the alternative context core or NULL
55 * to go back to the default context core.
56 */
57VMMDECL(void) CPUMHyperSetCtxCore(PVM pVM, PCPUMCTXCORE pCtxCore)
58{
59 LogFlow(("CPUMHyperSetCtxCore: %p/%p/%p -> %p\n", pVM->cpum.s.CTX_SUFF(pHyperCore), pCtxCore));
60 if (!pCtxCore)
61 {
62 pCtxCore = CPUMCTX2CORE(&pVM->cpum.s.Hyper);
63 pVM->cpum.s.pHyperCoreR3 = (R3PTRTYPE(PCPUMCTXCORE))VM_R3_ADDR(pVM, pCtxCore);
64 pVM->cpum.s.pHyperCoreR0 = (R0PTRTYPE(PCPUMCTXCORE))VM_R0_ADDR(pVM, pCtxCore);
65 pVM->cpum.s.pHyperCoreRC = (RCPTRTYPE(PCPUMCTXCORE))VM_GUEST_ADDR(pVM, pCtxCore);
66 }
67 else
68 {
69 pVM->cpum.s.pHyperCoreR3 = (R3PTRTYPE(PCPUMCTXCORE))MMHyperCCToR3(pVM, pCtxCore);
70 pVM->cpum.s.pHyperCoreR0 = (R0PTRTYPE(PCPUMCTXCORE))MMHyperCCToR0(pVM, pCtxCore);
71 pVM->cpum.s.pHyperCoreRC = (RCPTRTYPE(PCPUMCTXCORE))MMHyperCCToRC(pVM, pCtxCore);
72 }
73}
74
75
76/**
77 * Gets the pointer to the internal CPUMCTXCORE structure for the hypervisor.
78 * This is only for reading in order to save a few calls.
79 *
80 * @param pVM Handle to the virtual machine.
81 */
82VMMDECL(PCCPUMCTXCORE) CPUMGetHyperCtxCore(PVM pVM)
83{
84 return pVM->cpum.s.CTX_SUFF(pHyperCore);
85}
86
87
88/**
89 * Queries the pointer to the internal CPUMCTX structure for the hypervisor.
90 *
91 * @returns VBox status code.
92 * @param pVM Handle to the virtual machine.
93 * @param ppCtx Receives the hyper CPUMCTX pointer when successful.
94 *
95 * @deprecated This will *not* (and has never) given the right picture of the
96 * hypervisor register state. With CPUMHyperSetCtxCore() this is
97 * getting much worse. So, use the individual functions for getting
98 * and esp. setting the hypervisor registers.
99 */
100VMMDECL(int) CPUMQueryHyperCtxPtr(PVM pVM, PCPUMCTX *ppCtx)
101{
102 *ppCtx = &pVM->cpum.s.Hyper;
103 return VINF_SUCCESS;
104}
105
106
107VMMDECL(void) CPUMSetHyperGDTR(PVM pVM, uint32_t addr, uint16_t limit)
108{
109 pVM->cpum.s.Hyper.gdtr.cbGdt = limit;
110 pVM->cpum.s.Hyper.gdtr.pGdt = addr;
111 pVM->cpum.s.Hyper.gdtrPadding = 0;
112}
113
114
115VMMDECL(void) CPUMSetHyperIDTR(PVM pVM, uint32_t addr, uint16_t limit)
116{
117 pVM->cpum.s.Hyper.idtr.cbIdt = limit;
118 pVM->cpum.s.Hyper.idtr.pIdt = addr;
119 pVM->cpum.s.Hyper.idtrPadding = 0;
120}
121
122
123VMMDECL(void) CPUMSetHyperCR3(PVM pVM, uint32_t cr3)
124{
125 pVM->cpum.s.Hyper.cr3 = cr3;
126}
127
128
129VMMDECL(void) CPUMSetHyperCS(PVM pVM, RTSEL SelCS)
130{
131 pVM->cpum.s.CTX_SUFF(pHyperCore)->cs = SelCS;
132}
133
134
135VMMDECL(void) CPUMSetHyperDS(PVM pVM, RTSEL SelDS)
136{
137 pVM->cpum.s.CTX_SUFF(pHyperCore)->ds = SelDS;
138}
139
140
141VMMDECL(void) CPUMSetHyperES(PVM pVM, RTSEL SelES)
142{
143 pVM->cpum.s.CTX_SUFF(pHyperCore)->es = SelES;
144}
145
146
147VMMDECL(void) CPUMSetHyperFS(PVM pVM, RTSEL SelFS)
148{
149 pVM->cpum.s.CTX_SUFF(pHyperCore)->fs = SelFS;
150}
151
152
153VMMDECL(void) CPUMSetHyperGS(PVM pVM, RTSEL SelGS)
154{
155 pVM->cpum.s.CTX_SUFF(pHyperCore)->gs = SelGS;
156}
157
158
159VMMDECL(void) CPUMSetHyperSS(PVM pVM, RTSEL SelSS)
160{
161 pVM->cpum.s.CTX_SUFF(pHyperCore)->ss = SelSS;
162}
163
164
165VMMDECL(void) CPUMSetHyperESP(PVM pVM, uint32_t u32ESP)
166{
167 pVM->cpum.s.CTX_SUFF(pHyperCore)->esp = u32ESP;
168}
169
170
171VMMDECL(int) CPUMSetHyperEFlags(PVM pVM, uint32_t Efl)
172{
173 pVM->cpum.s.CTX_SUFF(pHyperCore)->eflags.u32 = Efl;
174 return VINF_SUCCESS;
175}
176
177
178VMMDECL(void) CPUMSetHyperEIP(PVM pVM, uint32_t u32EIP)
179{
180 pVM->cpum.s.CTX_SUFF(pHyperCore)->eip = u32EIP;
181}
182
183
184VMMDECL(void) CPUMSetHyperTR(PVM pVM, RTSEL SelTR)
185{
186 pVM->cpum.s.Hyper.tr = SelTR;
187}
188
189
190VMMDECL(void) CPUMSetHyperLDTR(PVM pVM, RTSEL SelLDTR)
191{
192 pVM->cpum.s.Hyper.ldtr = SelLDTR;
193}
194
195
196VMMDECL(void) CPUMSetHyperDR0(PVM pVM, RTGCUINTREG uDr0)
197{
198 pVM->cpum.s.Hyper.dr[0] = uDr0;
199 /** @todo in GC we must load it! */
200}
201
202
203VMMDECL(void) CPUMSetHyperDR1(PVM pVM, RTGCUINTREG uDr1)
204{
205 pVM->cpum.s.Hyper.dr[1] = uDr1;
206 /** @todo in GC we must load it! */
207}
208
209
210VMMDECL(void) CPUMSetHyperDR2(PVM pVM, RTGCUINTREG uDr2)
211{
212 pVM->cpum.s.Hyper.dr[2] = uDr2;
213 /** @todo in GC we must load it! */
214}
215
216
217VMMDECL(void) CPUMSetHyperDR3(PVM pVM, RTGCUINTREG uDr3)
218{
219 pVM->cpum.s.Hyper.dr[3] = uDr3;
220 /** @todo in GC we must load it! */
221}
222
223
224VMMDECL(void) CPUMSetHyperDR6(PVM pVM, RTGCUINTREG uDr6)
225{
226 pVM->cpum.s.Hyper.dr[6] = uDr6;
227 /** @todo in GC we must load it! */
228}
229
230
231VMMDECL(void) CPUMSetHyperDR7(PVM pVM, RTGCUINTREG uDr7)
232{
233 pVM->cpum.s.Hyper.dr[7] = uDr7;
234 /** @todo in GC we must load it! */
235}
236
237
238VMMDECL(RTSEL) CPUMGetHyperCS(PVM pVM)
239{
240 return pVM->cpum.s.CTX_SUFF(pHyperCore)->cs;
241}
242
243
244VMMDECL(RTSEL) CPUMGetHyperDS(PVM pVM)
245{
246 return pVM->cpum.s.CTX_SUFF(pHyperCore)->ds;
247}
248
249
250VMMDECL(RTSEL) CPUMGetHyperES(PVM pVM)
251{
252 return pVM->cpum.s.CTX_SUFF(pHyperCore)->es;
253}
254
255
256VMMDECL(RTSEL) CPUMGetHyperFS(PVM pVM)
257{
258 return pVM->cpum.s.CTX_SUFF(pHyperCore)->fs;
259}
260
261
262VMMDECL(RTSEL) CPUMGetHyperGS(PVM pVM)
263{
264 return pVM->cpum.s.CTX_SUFF(pHyperCore)->gs;
265}
266
267
268VMMDECL(RTSEL) CPUMGetHyperSS(PVM pVM)
269{
270 return pVM->cpum.s.CTX_SUFF(pHyperCore)->ss;
271}
272
273
274VMMDECL(uint32_t) CPUMGetHyperEAX(PVM pVM)
275{
276 return pVM->cpum.s.CTX_SUFF(pHyperCore)->eax;
277}
278
279
280VMMDECL(uint32_t) CPUMGetHyperEBX(PVM pVM)
281{
282 return pVM->cpum.s.CTX_SUFF(pHyperCore)->ebx;
283}
284
285
286VMMDECL(uint32_t) CPUMGetHyperECX(PVM pVM)
287{
288 return pVM->cpum.s.CTX_SUFF(pHyperCore)->ecx;
289}
290
291
292VMMDECL(uint32_t) CPUMGetHyperEDX(PVM pVM)
293{
294 return pVM->cpum.s.CTX_SUFF(pHyperCore)->edx;
295}
296
297
298VMMDECL(uint32_t) CPUMGetHyperESI(PVM pVM)
299{
300 return pVM->cpum.s.CTX_SUFF(pHyperCore)->esi;
301}
302
303
304VMMDECL(uint32_t) CPUMGetHyperEDI(PVM pVM)
305{
306 return pVM->cpum.s.CTX_SUFF(pHyperCore)->edi;
307}
308
309
310VMMDECL(uint32_t) CPUMGetHyperEBP(PVM pVM)
311{
312 return pVM->cpum.s.CTX_SUFF(pHyperCore)->ebp;
313}
314
315
316VMMDECL(uint32_t) CPUMGetHyperESP(PVM pVM)
317{
318 return pVM->cpum.s.CTX_SUFF(pHyperCore)->esp;
319}
320
321
322VMMDECL(uint32_t) CPUMGetHyperEFlags(PVM pVM)
323{
324 return pVM->cpum.s.CTX_SUFF(pHyperCore)->eflags.u32;
325}
326
327
328VMMDECL(uint32_t) CPUMGetHyperEIP(PVM pVM)
329{
330 return pVM->cpum.s.CTX_SUFF(pHyperCore)->eip;
331}
332
333
334VMMDECL(uint64_t) CPUMGetHyperRIP(PVM pVM)
335{
336 return pVM->cpum.s.CTX_SUFF(pHyperCore)->rip;
337}
338
339
340VMMDECL(uint32_t) CPUMGetHyperIDTR(PVM pVM, uint16_t *pcbLimit)
341{
342 if (pcbLimit)
343 *pcbLimit = pVM->cpum.s.Hyper.idtr.cbIdt;
344 return pVM->cpum.s.Hyper.idtr.pIdt;
345}
346
347
348VMMDECL(uint32_t) CPUMGetHyperGDTR(PVM pVM, uint16_t *pcbLimit)
349{
350 if (pcbLimit)
351 *pcbLimit = pVM->cpum.s.Hyper.gdtr.cbGdt;
352 return pVM->cpum.s.Hyper.gdtr.pGdt;
353}
354
355
356VMMDECL(RTSEL) CPUMGetHyperLDTR(PVM pVM)
357{
358 return pVM->cpum.s.Hyper.ldtr;
359}
360
361
362VMMDECL(RTGCUINTREG) CPUMGetHyperDR0(PVM pVM)
363{
364 return pVM->cpum.s.Hyper.dr[0];
365}
366
367
368VMMDECL(RTGCUINTREG) CPUMGetHyperDR1(PVM pVM)
369{
370 return pVM->cpum.s.Hyper.dr[1];
371}
372
373
374VMMDECL(RTGCUINTREG) CPUMGetHyperDR2(PVM pVM)
375{
376 return pVM->cpum.s.Hyper.dr[2];
377}
378
379
380VMMDECL(RTGCUINTREG) CPUMGetHyperDR3(PVM pVM)
381{
382 return pVM->cpum.s.Hyper.dr[3];
383}
384
385
386VMMDECL(RTGCUINTREG) CPUMGetHyperDR6(PVM pVM)
387{
388 return pVM->cpum.s.Hyper.dr[6];
389}
390
391
392VMMDECL(RTGCUINTREG) CPUMGetHyperDR7(PVM pVM)
393{
394 return pVM->cpum.s.Hyper.dr[7];
395}
396
397
398/**
399 * Gets the pointer to the internal CPUMCTXCORE structure.
400 * This is only for reading in order to save a few calls.
401 *
402 * @param pVM Handle to the virtual machine.
403 */
404VMMDECL(PCCPUMCTXCORE) CPUMGetGuestCtxCore(PVM pVM)
405{
406 return CPUMCTX2CORE(&pVM->cpum.s.Guest);
407}
408
409
410/**
411 * Sets the guest context core registers.
412 *
413 * @param pVM Handle to the virtual machine.
414 * @param pCtxCore The new context core values.
415 */
416VMMDECL(void) CPUMSetGuestCtxCore(PVM pVM, PCCPUMCTXCORE pCtxCore)
417{
418 /** @todo #1410 requires selectors to be checked. (huh? 1410?) */
419
420 PCPUMCTXCORE pCtxCoreDst = CPUMCTX2CORE(&pVM->cpum.s.Guest);
421 *pCtxCoreDst = *pCtxCore;
422
423 /* Mask away invalid parts of the cpu context. */
424 if (!CPUMIsGuestInLongMode(pVM))
425 {
426 uint64_t u64Mask = UINT64_C(0xffffffff);
427
428 pCtxCoreDst->rip &= u64Mask;
429 pCtxCoreDst->rax &= u64Mask;
430 pCtxCoreDst->rbx &= u64Mask;
431 pCtxCoreDst->rcx &= u64Mask;
432 pCtxCoreDst->rdx &= u64Mask;
433 pCtxCoreDst->rsi &= u64Mask;
434 pCtxCoreDst->rdi &= u64Mask;
435 pCtxCoreDst->rbp &= u64Mask;
436 pCtxCoreDst->rsp &= u64Mask;
437 pCtxCoreDst->rflags.u &= u64Mask;
438
439 pCtxCoreDst->r8 = 0;
440 pCtxCoreDst->r9 = 0;
441 pCtxCoreDst->r10 = 0;
442 pCtxCoreDst->r11 = 0;
443 pCtxCoreDst->r12 = 0;
444 pCtxCoreDst->r13 = 0;
445 pCtxCoreDst->r14 = 0;
446 pCtxCoreDst->r15 = 0;
447 }
448}
449
450
451/**
452 * Queries the pointer to the internal CPUMCTX structure
453 *
454 * @returns The CPUMCTX pointer.
455 * @param pVM Handle to the virtual machine.
456 */
457VMMDECL(PCPUMCTX) CPUMQueryGuestCtxPtr(PVM pVM)
458{
459 return &pVM->cpum.s.Guest;
460}
461
462
463VMMDECL(int) CPUMSetGuestGDTR(PVM pVM, uint32_t addr, uint16_t limit)
464{
465 pVM->cpum.s.Guest.gdtr.cbGdt = limit;
466 pVM->cpum.s.Guest.gdtr.pGdt = addr;
467 pVM->cpum.s.fChanged |= CPUM_CHANGED_GDTR;
468 return VINF_SUCCESS;
469}
470
471VMMDECL(int) CPUMSetGuestIDTR(PVM pVM, uint32_t addr, uint16_t limit)
472{
473 pVM->cpum.s.Guest.idtr.cbIdt = limit;
474 pVM->cpum.s.Guest.idtr.pIdt = addr;
475 pVM->cpum.s.fChanged |= CPUM_CHANGED_IDTR;
476 return VINF_SUCCESS;
477}
478
479VMMDECL(int) CPUMSetGuestTR(PVM pVM, uint16_t tr)
480{
481 pVM->cpum.s.Guest.tr = tr;
482 pVM->cpum.s.fChanged |= CPUM_CHANGED_TR;
483 return VINF_SUCCESS;
484}
485
486VMMDECL(int) CPUMSetGuestLDTR(PVM pVM, uint16_t ldtr)
487{
488 pVM->cpum.s.Guest.ldtr = ldtr;
489 pVM->cpum.s.fChanged |= CPUM_CHANGED_LDTR;
490 return VINF_SUCCESS;
491}
492
493
494/**
495 * Set the guest CR0.
496 *
497 * When called in GC, the hyper CR0 may be updated if that is
498 * required. The caller only has to take special action if AM,
499 * WP, PG or PE changes.
500 *
501 * @returns VINF_SUCCESS (consider it void).
502 * @param pVM Pointer to the shared VM structure.
503 * @param cr0 The new CR0 value.
504 */
505VMMDECL(int) CPUMSetGuestCR0(PVM pVM, uint64_t cr0)
506{
507#ifdef IN_GC
508 /*
509 * Check if we need to change hypervisor CR0 because
510 * of math stuff.
511 */
512 if ( (cr0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP))
513 != (pVM->cpum.s.Guest.cr0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP)))
514 {
515 if (!(pVM->cpum.s.fUseFlags & CPUM_USED_FPU))
516 {
517 /*
518 * We haven't saved the host FPU state yet, so TS and MT are both set
519 * and EM should be reflecting the guest EM (it always does this).
520 */
521 if ((cr0 & X86_CR0_EM) != (pVM->cpum.s.Guest.cr0 & X86_CR0_EM))
522 {
523 uint32_t HyperCR0 = ASMGetCR0();
524 AssertMsg((HyperCR0 & (X86_CR0_TS | X86_CR0_MP)) == (X86_CR0_TS | X86_CR0_MP), ("%#x\n", HyperCR0));
525 AssertMsg((HyperCR0 & X86_CR0_EM) == (pVM->cpum.s.Guest.cr0 & X86_CR0_EM), ("%#x\n", HyperCR0));
526 HyperCR0 &= ~X86_CR0_EM;
527 HyperCR0 |= cr0 & X86_CR0_EM;
528 Log(("CPUM New HyperCR0=%#x\n", HyperCR0));
529 ASMSetCR0(HyperCR0);
530 }
531# ifdef VBOX_STRICT
532 else
533 {
534 uint32_t HyperCR0 = ASMGetCR0();
535 AssertMsg((HyperCR0 & (X86_CR0_TS | X86_CR0_MP)) == (X86_CR0_TS | X86_CR0_MP), ("%#x\n", HyperCR0));
536 AssertMsg((HyperCR0 & X86_CR0_EM) == (pVM->cpum.s.Guest.cr0 & X86_CR0_EM), ("%#x\n", HyperCR0));
537 }
538# endif
539 }
540 else
541 {
542 /*
543 * Already saved the state, so we're just mirroring
544 * the guest flags.
545 */
546 uint32_t HyperCR0 = ASMGetCR0();
547 AssertMsg( (HyperCR0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP))
548 == (pVM->cpum.s.Guest.cr0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP)),
549 ("%#x %#x\n", HyperCR0, pVM->cpum.s.Guest.cr0));
550 HyperCR0 &= ~(X86_CR0_TS | X86_CR0_EM | X86_CR0_MP);
551 HyperCR0 |= cr0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP);
552 Log(("CPUM New HyperCR0=%#x\n", HyperCR0));
553 ASMSetCR0(HyperCR0);
554 }
555 }
556#endif /* IN_GC */
557
558 /*
559 * Check for changes causing TLB flushes (for REM).
560 * The caller is responsible for calling PGM when appropriate.
561 */
562 if ( (cr0 & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE))
563 != (pVM->cpum.s.Guest.cr0 & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE)))
564 pVM->cpum.s.fChanged |= CPUM_CHANGED_GLOBAL_TLB_FLUSH;
565 pVM->cpum.s.fChanged |= CPUM_CHANGED_CR0;
566
567 pVM->cpum.s.Guest.cr0 = cr0 | X86_CR0_ET;
568 return VINF_SUCCESS;
569}
570
571
572VMMDECL(int) CPUMSetGuestCR2(PVM pVM, uint64_t cr2)
573{
574 pVM->cpum.s.Guest.cr2 = cr2;
575 return VINF_SUCCESS;
576}
577
578
579VMMDECL(int) CPUMSetGuestCR3(PVM pVM, uint64_t cr3)
580{
581 pVM->cpum.s.Guest.cr3 = cr3;
582 pVM->cpum.s.fChanged |= CPUM_CHANGED_CR3;
583 return VINF_SUCCESS;
584}
585
586
587VMMDECL(int) CPUMSetGuestCR4(PVM pVM, uint64_t cr4)
588{
589 if ( (cr4 & (X86_CR4_PGE | X86_CR4_PAE | X86_CR4_PSE))
590 != (pVM->cpum.s.Guest.cr4 & (X86_CR4_PGE | X86_CR4_PAE | X86_CR4_PSE)))
591 pVM->cpum.s.fChanged |= CPUM_CHANGED_GLOBAL_TLB_FLUSH;
592 pVM->cpum.s.fChanged |= CPUM_CHANGED_CR4;
593 if (!CPUMSupportsFXSR(pVM))
594 cr4 &= ~X86_CR4_OSFSXR;
595 pVM->cpum.s.Guest.cr4 = cr4;
596 return VINF_SUCCESS;
597}
598
599
600VMMDECL(int) CPUMSetGuestEFlags(PVM pVM, uint32_t eflags)
601{
602 pVM->cpum.s.Guest.eflags.u32 = eflags;
603 return VINF_SUCCESS;
604}
605
606
607VMMDECL(int) CPUMSetGuestEIP(PVM pVM, uint32_t eip)
608{
609 pVM->cpum.s.Guest.eip = eip;
610 return VINF_SUCCESS;
611}
612
613
614VMMDECL(int) CPUMSetGuestEAX(PVM pVM, uint32_t eax)
615{
616 pVM->cpum.s.Guest.eax = eax;
617 return VINF_SUCCESS;
618}
619
620
621VMMDECL(int) CPUMSetGuestEBX(PVM pVM, uint32_t ebx)
622{
623 pVM->cpum.s.Guest.ebx = ebx;
624 return VINF_SUCCESS;
625}
626
627
628VMMDECL(int) CPUMSetGuestECX(PVM pVM, uint32_t ecx)
629{
630 pVM->cpum.s.Guest.ecx = ecx;
631 return VINF_SUCCESS;
632}
633
634
635VMMDECL(int) CPUMSetGuestEDX(PVM pVM, uint32_t edx)
636{
637 pVM->cpum.s.Guest.edx = edx;
638 return VINF_SUCCESS;
639}
640
641
642VMMDECL(int) CPUMSetGuestESP(PVM pVM, uint32_t esp)
643{
644 pVM->cpum.s.Guest.esp = esp;
645 return VINF_SUCCESS;
646}
647
648
649VMMDECL(int) CPUMSetGuestEBP(PVM pVM, uint32_t ebp)
650{
651 pVM->cpum.s.Guest.ebp = ebp;
652 return VINF_SUCCESS;
653}
654
655
656VMMDECL(int) CPUMSetGuestESI(PVM pVM, uint32_t esi)
657{
658 pVM->cpum.s.Guest.esi = esi;
659 return VINF_SUCCESS;
660}
661
662
663VMMDECL(int) CPUMSetGuestEDI(PVM pVM, uint32_t edi)
664{
665 pVM->cpum.s.Guest.edi = edi;
666 return VINF_SUCCESS;
667}
668
669
670VMMDECL(int) CPUMSetGuestSS(PVM pVM, uint16_t ss)
671{
672 pVM->cpum.s.Guest.ss = ss;
673 return VINF_SUCCESS;
674}
675
676
677VMMDECL(int) CPUMSetGuestCS(PVM pVM, uint16_t cs)
678{
679 pVM->cpum.s.Guest.cs = cs;
680 return VINF_SUCCESS;
681}
682
683
684VMMDECL(int) CPUMSetGuestDS(PVM pVM, uint16_t ds)
685{
686 pVM->cpum.s.Guest.ds = ds;
687 return VINF_SUCCESS;
688}
689
690
691VMMDECL(int) CPUMSetGuestES(PVM pVM, uint16_t es)
692{
693 pVM->cpum.s.Guest.es = es;
694 return VINF_SUCCESS;
695}
696
697
698VMMDECL(int) CPUMSetGuestFS(PVM pVM, uint16_t fs)
699{
700 pVM->cpum.s.Guest.fs = fs;
701 return VINF_SUCCESS;
702}
703
704
705VMMDECL(int) CPUMSetGuestGS(PVM pVM, uint16_t gs)
706{
707 pVM->cpum.s.Guest.gs = gs;
708 return VINF_SUCCESS;
709}
710
711
712VMMDECL(void) CPUMSetGuestEFER(PVM pVM, uint64_t val)
713{
714 pVM->cpum.s.Guest.msrEFER = val;
715}
716
717
718VMMDECL(uint64_t) CPUMGetGuestMsr(PVM pVM, unsigned idMsr)
719{
720 uint64_t u64 = 0;
721
722 switch (idMsr)
723 {
724 case MSR_IA32_CR_PAT:
725 u64 = pVM->cpum.s.Guest.msrPAT;
726 break;
727
728 case MSR_IA32_SYSENTER_CS:
729 u64 = pVM->cpum.s.Guest.SysEnter.cs;
730 break;
731
732 case MSR_IA32_SYSENTER_EIP:
733 u64 = pVM->cpum.s.Guest.SysEnter.eip;
734 break;
735
736 case MSR_IA32_SYSENTER_ESP:
737 u64 = pVM->cpum.s.Guest.SysEnter.esp;
738 break;
739
740 case MSR_K6_EFER:
741 u64 = pVM->cpum.s.Guest.msrEFER;
742 break;
743
744 case MSR_K8_SF_MASK:
745 u64 = pVM->cpum.s.Guest.msrSFMASK;
746 break;
747
748 case MSR_K6_STAR:
749 u64 = pVM->cpum.s.Guest.msrSTAR;
750 break;
751
752 case MSR_K8_LSTAR:
753 u64 = pVM->cpum.s.Guest.msrLSTAR;
754 break;
755
756 case MSR_K8_CSTAR:
757 u64 = pVM->cpum.s.Guest.msrCSTAR;
758 break;
759
760 case MSR_K8_KERNEL_GS_BASE:
761 u64 = pVM->cpum.s.Guest.msrKERNELGSBASE;
762 break;
763
764 /* fs & gs base skipped on purpose as the current context might not be up-to-date. */
765 default:
766 AssertFailed();
767 break;
768 }
769 return u64;
770}
771
772
773VMMDECL(RTGCPTR) CPUMGetGuestIDTR(PVM pVM, uint16_t *pcbLimit)
774{
775 if (pcbLimit)
776 *pcbLimit = pVM->cpum.s.Guest.idtr.cbIdt;
777 return pVM->cpum.s.Guest.idtr.pIdt;
778}
779
780
781VMMDECL(RTSEL) CPUMGetGuestTR(PVM pVM)
782{
783 return pVM->cpum.s.Guest.tr;
784}
785
786
787VMMDECL(RTSEL) CPUMGetGuestCS(PVM pVM)
788{
789 return pVM->cpum.s.Guest.cs;
790}
791
792
793VMMDECL(RTSEL) CPUMGetGuestDS(PVM pVM)
794{
795 return pVM->cpum.s.Guest.ds;
796}
797
798
799VMMDECL(RTSEL) CPUMGetGuestES(PVM pVM)
800{
801 return pVM->cpum.s.Guest.es;
802}
803
804
805VMMDECL(RTSEL) CPUMGetGuestFS(PVM pVM)
806{
807 return pVM->cpum.s.Guest.fs;
808}
809
810
811VMMDECL(RTSEL) CPUMGetGuestGS(PVM pVM)
812{
813 return pVM->cpum.s.Guest.gs;
814}
815
816
817VMMDECL(RTSEL) CPUMGetGuestSS(PVM pVM)
818{
819 return pVM->cpum.s.Guest.ss;
820}
821
822
823VMMDECL(RTSEL) CPUMGetGuestLDTR(PVM pVM)
824{
825 return pVM->cpum.s.Guest.ldtr;
826}
827
828
829VMMDECL(uint64_t) CPUMGetGuestCR0(PVM pVM)
830{
831 return pVM->cpum.s.Guest.cr0;
832}
833
834
835VMMDECL(uint64_t) CPUMGetGuestCR2(PVM pVM)
836{
837 return pVM->cpum.s.Guest.cr2;
838}
839
840
841VMMDECL(uint64_t) CPUMGetGuestCR3(PVM pVM)
842{
843 return pVM->cpum.s.Guest.cr3;
844}
845
846
847VMMDECL(uint64_t) CPUMGetGuestCR4(PVM pVM)
848{
849 return pVM->cpum.s.Guest.cr4;
850}
851
852
853VMMDECL(void) CPUMGetGuestGDTR(PVM pVM, PVBOXGDTR pGDTR)
854{
855 *pGDTR = pVM->cpum.s.Guest.gdtr;
856}
857
858
859VMMDECL(uint32_t) CPUMGetGuestEIP(PVM pVM)
860{
861 return pVM->cpum.s.Guest.eip;
862}
863
864
865VMMDECL(uint64_t) CPUMGetGuestRIP(PVM pVM)
866{
867 return pVM->cpum.s.Guest.rip;
868}
869
870
871VMMDECL(uint32_t) CPUMGetGuestEAX(PVM pVM)
872{
873 return pVM->cpum.s.Guest.eax;
874}
875
876
877VMMDECL(uint32_t) CPUMGetGuestEBX(PVM pVM)
878{
879 return pVM->cpum.s.Guest.ebx;
880}
881
882
883VMMDECL(uint32_t) CPUMGetGuestECX(PVM pVM)
884{
885 return pVM->cpum.s.Guest.ecx;
886}
887
888
889VMMDECL(uint32_t) CPUMGetGuestEDX(PVM pVM)
890{
891 return pVM->cpum.s.Guest.edx;
892}
893
894
895VMMDECL(uint32_t) CPUMGetGuestESI(PVM pVM)
896{
897 return pVM->cpum.s.Guest.esi;
898}
899
900
901VMMDECL(uint32_t) CPUMGetGuestEDI(PVM pVM)
902{
903 return pVM->cpum.s.Guest.edi;
904}
905
906
907VMMDECL(uint32_t) CPUMGetGuestESP(PVM pVM)
908{
909 return pVM->cpum.s.Guest.esp;
910}
911
912
913VMMDECL(uint32_t) CPUMGetGuestEBP(PVM pVM)
914{
915 return pVM->cpum.s.Guest.ebp;
916}
917
918
919VMMDECL(uint32_t) CPUMGetGuestEFlags(PVM pVM)
920{
921 return pVM->cpum.s.Guest.eflags.u32;
922}
923
924
925VMMDECL(CPUMSELREGHID *) CPUMGetGuestTRHid(PVM pVM)
926{
927 return &pVM->cpum.s.Guest.trHid;
928}
929
930
931///@todo: crx should be an array
932VMMDECL(int) CPUMGetGuestCRx(PVM pVM, unsigned iReg, uint64_t *pValue)
933{
934 switch (iReg)
935 {
936 case USE_REG_CR0:
937 *pValue = pVM->cpum.s.Guest.cr0;
938 break;
939 case USE_REG_CR2:
940 *pValue = pVM->cpum.s.Guest.cr2;
941 break;
942 case USE_REG_CR3:
943 *pValue = pVM->cpum.s.Guest.cr3;
944 break;
945 case USE_REG_CR4:
946 *pValue = pVM->cpum.s.Guest.cr4;
947 break;
948 default:
949 return VERR_INVALID_PARAMETER;
950 }
951 return VINF_SUCCESS;
952}
953
954
955VMMDECL(uint64_t) CPUMGetGuestDR0(PVM pVM)
956{
957 return pVM->cpum.s.Guest.dr[0];
958}
959
960
961VMMDECL(uint64_t) CPUMGetGuestDR1(PVM pVM)
962{
963 return pVM->cpum.s.Guest.dr[1];
964}
965
966
967VMMDECL(uint64_t) CPUMGetGuestDR2(PVM pVM)
968{
969 return pVM->cpum.s.Guest.dr[2];
970}
971
972
973VMMDECL(uint64_t) CPUMGetGuestDR3(PVM pVM)
974{
975 return pVM->cpum.s.Guest.dr[3];
976}
977
978
979VMMDECL(uint64_t) CPUMGetGuestDR6(PVM pVM)
980{
981 return pVM->cpum.s.Guest.dr[6];
982}
983
984
985VMMDECL(uint64_t) CPUMGetGuestDR7(PVM pVM)
986{
987 return pVM->cpum.s.Guest.dr[7];
988}
989
990
991VMMDECL(int) CPUMGetGuestDRx(PVM pVM, uint32_t iReg, uint64_t *pValue)
992{
993 AssertReturn(iReg <= USE_REG_DR7, VERR_INVALID_PARAMETER);
994 /* DR4 is an alias for DR6, and DR5 is an alias for DR7. */
995 if (iReg == 4 || iReg == 5)
996 iReg += 2;
997 *pValue = pVM->cpum.s.Guest.dr[iReg];
998 return VINF_SUCCESS;
999}
1000
1001
1002VMMDECL(uint64_t) CPUMGetGuestEFER(PVM pVM)
1003{
1004 return pVM->cpum.s.Guest.msrEFER;
1005}
1006
1007
1008/**
1009 * Gets a CpuId leaf.
1010 *
1011 * @param pVM The VM handle.
1012 * @param iLeaf The CPUID leaf to get.
1013 * @param pEax Where to store the EAX value.
1014 * @param pEbx Where to store the EBX value.
1015 * @param pEcx Where to store the ECX value.
1016 * @param pEdx Where to store the EDX value.
1017 */
1018VMMDECL(void) CPUMGetGuestCpuId(PVM pVM, uint32_t iLeaf, uint32_t *pEax, uint32_t *pEbx, uint32_t *pEcx, uint32_t *pEdx)
1019{
1020 PCCPUMCPUID pCpuId;
1021 if (iLeaf < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdStd))
1022 pCpuId = &pVM->cpum.s.aGuestCpuIdStd[iLeaf];
1023 else if (iLeaf - UINT32_C(0x80000000) < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdExt))
1024 pCpuId = &pVM->cpum.s.aGuestCpuIdExt[iLeaf - UINT32_C(0x80000000)];
1025 else if (iLeaf - UINT32_C(0xc0000000) < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdCentaur))
1026 pCpuId = &pVM->cpum.s.aGuestCpuIdCentaur[iLeaf - UINT32_C(0xc0000000)];
1027 else
1028 pCpuId = &pVM->cpum.s.GuestCpuIdDef;
1029
1030 *pEax = pCpuId->eax;
1031 *pEbx = pCpuId->ebx;
1032 *pEcx = pCpuId->ecx;
1033 *pEdx = pCpuId->edx;
1034 Log2(("CPUMGetGuestCpuId: iLeaf=%#010x %RX32 %RX32 %RX32 %RX32\n", iLeaf, *pEax, *pEbx, *pEcx, *pEdx));
1035}
1036
1037
1038/**
1039 * Gets a pointer to the array of standard CPUID leafs.
1040 *
1041 * CPUMGetGuestCpuIdStdMax() give the size of the array.
1042 *
1043 * @returns Pointer to the standard CPUID leafs (read-only).
1044 * @param pVM The VM handle.
1045 * @remark Intended for PATM.
1046 */
1047VMMDECL(RCPTRTYPE(PCCPUMCPUID)) CPUMGetGuestCpuIdStdGCPtr(PVM pVM)
1048{
1049 return RCPTRTYPE(PCCPUMCPUID)VM_GUEST_ADDR(pVM, &pVM->cpum.s.aGuestCpuIdStd[0]);
1050}
1051
1052
1053/**
1054 * Gets a pointer to the array of extended CPUID leafs.
1055 *
1056 * CPUMGetGuestCpuIdExtMax() give the size of the array.
1057 *
1058 * @returns Pointer to the extended CPUID leafs (read-only).
1059 * @param pVM The VM handle.
1060 * @remark Intended for PATM.
1061 */
1062VMMDECL(RCPTRTYPE(PCCPUMCPUID)) CPUMGetGuestCpuIdExtGCPtr(PVM pVM)
1063{
1064 return (RCPTRTYPE(PCCPUMCPUID))VM_GUEST_ADDR(pVM, &pVM->cpum.s.aGuestCpuIdExt[0]);
1065}
1066
1067
1068/**
1069 * Gets a pointer to the array of centaur CPUID leafs.
1070 *
1071 * CPUMGetGuestCpuIdCentaurMax() give the size of the array.
1072 *
1073 * @returns Pointer to the centaur CPUID leafs (read-only).
1074 * @param pVM The VM handle.
1075 * @remark Intended for PATM.
1076 */
1077VMMDECL(RCPTRTYPE(PCCPUMCPUID)) CPUMGetGuestCpuIdCentaurGCPtr(PVM pVM)
1078{
1079 return (RCPTRTYPE(PCCPUMCPUID))VM_GUEST_ADDR(pVM, &pVM->cpum.s.aGuestCpuIdCentaur[0]);
1080}
1081
1082
1083/**
1084 * Gets a pointer to the default CPUID leaf.
1085 *
1086 * @returns Pointer to the default CPUID leaf (read-only).
1087 * @param pVM The VM handle.
1088 * @remark Intended for PATM.
1089 */
1090VMMDECL(RCPTRTYPE(PCCPUMCPUID)) CPUMGetGuestCpuIdDefGCPtr(PVM pVM)
1091{
1092 return (RCPTRTYPE(PCCPUMCPUID))VM_GUEST_ADDR(pVM, &pVM->cpum.s.GuestCpuIdDef);
1093}
1094
1095
1096/**
1097 * Gets a number of standard CPUID leafs.
1098 *
1099 * @returns Number of leafs.
1100 * @param pVM The VM handle.
1101 * @remark Intended for PATM.
1102 */
1103VMMDECL(uint32_t) CPUMGetGuestCpuIdStdMax(PVM pVM)
1104{
1105 return RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdStd);
1106}
1107
1108
1109/**
1110 * Gets a number of extended CPUID leafs.
1111 *
1112 * @returns Number of leafs.
1113 * @param pVM The VM handle.
1114 * @remark Intended for PATM.
1115 */
1116VMMDECL(uint32_t) CPUMGetGuestCpuIdExtMax(PVM pVM)
1117{
1118 return RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdExt);
1119}
1120
1121
1122/**
1123 * Gets a number of centaur CPUID leafs.
1124 *
1125 * @returns Number of leafs.
1126 * @param pVM The VM handle.
1127 * @remark Intended for PATM.
1128 */
1129VMMDECL(uint32_t) CPUMGetGuestCpuIdCentaurMax(PVM pVM)
1130{
1131 return RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdCentaur);
1132}
1133
1134
1135/**
1136 * Sets a CPUID feature bit.
1137 *
1138 * @param pVM The VM Handle.
1139 * @param enmFeature The feature to set.
1140 */
1141VMMDECL(void) CPUMSetGuestCpuIdFeature(PVM pVM, CPUMCPUIDFEATURE enmFeature)
1142{
1143 switch (enmFeature)
1144 {
1145 /*
1146 * Set the APIC bit in both feature masks.
1147 */
1148 case CPUMCPUIDFEATURE_APIC:
1149 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1150 pVM->cpum.s.aGuestCpuIdStd[1].edx |= X86_CPUID_FEATURE_EDX_APIC;
1151 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1152 && pVM->cpum.s.enmCPUVendor == CPUMCPUVENDOR_AMD)
1153 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_APIC;
1154 LogRel(("CPUMSetGuestCpuIdFeature: Enabled APIC\n"));
1155 break;
1156
1157 /*
1158 * Set the x2APIC bit in the standard feature mask.
1159 */
1160 case CPUMCPUIDFEATURE_X2APIC:
1161 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1162 pVM->cpum.s.aGuestCpuIdStd[1].ecx |= X86_CPUID_FEATURE_ECX_X2APIC;
1163 LogRel(("CPUMSetGuestCpuIdFeature: Enabled x2APIC\n"));
1164 break;
1165
1166 /*
1167 * Set the sysenter/sysexit bit in the standard feature mask.
1168 * Assumes the caller knows what it's doing! (host must support these)
1169 */
1170 case CPUMCPUIDFEATURE_SEP:
1171 {
1172 if (!(ASMCpuId_EDX(1) & X86_CPUID_FEATURE_EDX_SEP))
1173 {
1174 AssertMsgFailed(("ERROR: Can't turn on SEP when the host doesn't support it!!\n"));
1175 return;
1176 }
1177
1178 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1179 pVM->cpum.s.aGuestCpuIdStd[1].edx |= X86_CPUID_FEATURE_EDX_SEP;
1180 LogRel(("CPUMSetGuestCpuIdFeature: Enabled sysenter/exit\n"));
1181 break;
1182 }
1183
1184 /*
1185 * Set the syscall/sysret bit in the extended feature mask.
1186 * Assumes the caller knows what it's doing! (host must support these)
1187 */
1188 case CPUMCPUIDFEATURE_SYSCALL:
1189 {
1190 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax < 0x80000001
1191 || !(ASMCpuId_EDX(0x80000001) & X86_CPUID_AMD_FEATURE_EDX_SEP))
1192 {
1193 LogRel(("WARNING: Can't turn on SYSCALL/SYSRET when the host doesn't support it!!\n"));
1194 return;
1195 }
1196 /* Valid for both Intel and AMD CPUs, although only in 64 bits mode for Intel. */
1197 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_SEP;
1198 LogRel(("CPUMSetGuestCpuIdFeature: Enabled syscall/ret\n"));
1199 break;
1200 }
1201
1202 /*
1203 * Set the PAE bit in both feature masks.
1204 * Assumes the caller knows what it's doing! (host must support these)
1205 */
1206 case CPUMCPUIDFEATURE_PAE:
1207 {
1208 if (!(ASMCpuId_EDX(1) & X86_CPUID_FEATURE_EDX_PAE))
1209 {
1210 LogRel(("WARNING: Can't turn on PAE when the host doesn't support it!!\n"));
1211 return;
1212 }
1213
1214 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1215 pVM->cpum.s.aGuestCpuIdStd[1].edx |= X86_CPUID_FEATURE_EDX_PAE;
1216 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1217 && pVM->cpum.s.enmCPUVendor == CPUMCPUVENDOR_AMD)
1218 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_PAE;
1219 LogRel(("CPUMSetGuestCpuIdFeature: Enabled PAE\n"));
1220 break;
1221 }
1222
1223 /*
1224 * Set the LONG MODE bit in the extended feature mask.
1225 * Assumes the caller knows what it's doing! (host must support these)
1226 */
1227 case CPUMCPUIDFEATURE_LONG_MODE:
1228 {
1229 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax < 0x80000001
1230 || !(ASMCpuId_EDX(0x80000001) & X86_CPUID_AMD_FEATURE_EDX_LONG_MODE))
1231 {
1232 LogRel(("WARNING: Can't turn on LONG MODE when the host doesn't support it!!\n"));
1233 return;
1234 }
1235
1236 /* Valid for both Intel and AMD. */
1237 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_LONG_MODE;
1238 LogRel(("CPUMSetGuestCpuIdFeature: Enabled LONG MODE\n"));
1239 break;
1240 }
1241
1242 /*
1243 * Set the NXE bit in the extended feature mask.
1244 * Assumes the caller knows what it's doing! (host must support these)
1245 */
1246 case CPUMCPUIDFEATURE_NXE:
1247 {
1248 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax < 0x80000001
1249 || !(ASMCpuId_EDX(0x80000001) & X86_CPUID_AMD_FEATURE_EDX_NX))
1250 {
1251 LogRel(("WARNING: Can't turn on NXE when the host doesn't support it!!\n"));
1252 return;
1253 }
1254
1255 /* Valid for both Intel and AMD. */
1256 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_NX;
1257 LogRel(("CPUMSetGuestCpuIdFeature: Enabled NXE\n"));
1258 break;
1259 }
1260
1261 case CPUMCPUIDFEATURE_LAHF:
1262 {
1263 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax < 0x80000001
1264 || !(ASMCpuId_ECX(0x80000001) & X86_CPUID_AMD_FEATURE_ECX_LAHF_SAHF))
1265 {
1266 LogRel(("WARNING: Can't turn on LAHF/SAHF when the host doesn't support it!!\n"));
1267 return;
1268 }
1269
1270 pVM->cpum.s.aGuestCpuIdExt[1].ecx |= X86_CPUID_AMD_FEATURE_ECX_LAHF_SAHF;
1271 LogRel(("CPUMSetGuestCpuIdFeature: Enabled LAHF/SAHF\n"));
1272 break;
1273 }
1274
1275 case CPUMCPUIDFEATURE_PAT:
1276 {
1277 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1278 pVM->cpum.s.aGuestCpuIdStd[1].edx |= X86_CPUID_FEATURE_EDX_PAT;
1279 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1280 && pVM->cpum.s.enmCPUVendor == CPUMCPUVENDOR_AMD)
1281 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_PAT;
1282 LogRel(("CPUMClearGuestCpuIdFeature: Enabled PAT\n"));
1283 break;
1284 }
1285
1286 default:
1287 AssertMsgFailed(("enmFeature=%d\n", enmFeature));
1288 break;
1289 }
1290 pVM->cpum.s.fChanged |= CPUM_CHANGED_CPUID;
1291}
1292
1293
1294/**
1295 * Queries a CPUID feature bit.
1296 *
1297 * @returns boolean for feature presence
1298 * @param pVM The VM Handle.
1299 * @param enmFeature The feature to query.
1300 */
1301VMMDECL(bool) CPUMGetGuestCpuIdFeature(PVM pVM, CPUMCPUIDFEATURE enmFeature)
1302{
1303 switch (enmFeature)
1304 {
1305 case CPUMCPUIDFEATURE_PAE:
1306 {
1307 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1308 return !!(pVM->cpum.s.aGuestCpuIdStd[1].edx & X86_CPUID_FEATURE_EDX_PAE);
1309 break;
1310 }
1311
1312 default:
1313 AssertMsgFailed(("enmFeature=%d\n", enmFeature));
1314 break;
1315 }
1316 return false;
1317}
1318
1319
1320/**
1321 * Clears a CPUID feature bit.
1322 *
1323 * @param pVM The VM Handle.
1324 * @param enmFeature The feature to clear.
1325 */
1326VMMDECL(void) CPUMClearGuestCpuIdFeature(PVM pVM, CPUMCPUIDFEATURE enmFeature)
1327{
1328 switch (enmFeature)
1329 {
1330 /*
1331 * Set the APIC bit in both feature masks.
1332 */
1333 case CPUMCPUIDFEATURE_APIC:
1334 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1335 pVM->cpum.s.aGuestCpuIdStd[1].edx &= ~X86_CPUID_FEATURE_EDX_APIC;
1336 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1337 && pVM->cpum.s.enmCPUVendor == CPUMCPUVENDOR_AMD)
1338 pVM->cpum.s.aGuestCpuIdExt[1].edx &= ~X86_CPUID_AMD_FEATURE_EDX_APIC;
1339 Log(("CPUMSetGuestCpuIdFeature: Disabled APIC\n"));
1340 break;
1341
1342 /*
1343 * Clear the x2APIC bit in the standard feature mask.
1344 */
1345 case CPUMCPUIDFEATURE_X2APIC:
1346 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1347 pVM->cpum.s.aGuestCpuIdStd[1].ecx &= ~X86_CPUID_FEATURE_ECX_X2APIC;
1348 LogRel(("CPUMSetGuestCpuIdFeature: Disabled x2APIC\n"));
1349 break;
1350
1351 case CPUMCPUIDFEATURE_PAE:
1352 {
1353 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1354 pVM->cpum.s.aGuestCpuIdStd[1].edx &= ~X86_CPUID_FEATURE_EDX_PAE;
1355 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1356 && pVM->cpum.s.enmCPUVendor == CPUMCPUVENDOR_AMD)
1357 pVM->cpum.s.aGuestCpuIdExt[1].edx &= ~X86_CPUID_AMD_FEATURE_EDX_PAE;
1358 LogRel(("CPUMClearGuestCpuIdFeature: Disabled PAE!\n"));
1359 break;
1360 }
1361
1362 case CPUMCPUIDFEATURE_PAT:
1363 {
1364 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1365 pVM->cpum.s.aGuestCpuIdStd[1].edx &= ~X86_CPUID_FEATURE_EDX_PAT;
1366 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1367 && pVM->cpum.s.enmCPUVendor == CPUMCPUVENDOR_AMD)
1368 pVM->cpum.s.aGuestCpuIdExt[1].edx &= ~X86_CPUID_AMD_FEATURE_EDX_PAT;
1369 LogRel(("CPUMClearGuestCpuIdFeature: Disabled PAT!\n"));
1370 break;
1371 }
1372
1373 default:
1374 AssertMsgFailed(("enmFeature=%d\n", enmFeature));
1375 break;
1376 }
1377 pVM->cpum.s.fChanged |= CPUM_CHANGED_CPUID;
1378}
1379
1380
1381/**
1382 * Gets the CPU vendor
1383 *
1384 * @returns CPU vendor
1385 * @param pVM The VM handle.
1386 */
1387VMMDECL(CPUMCPUVENDOR) CPUMGetCPUVendor(PVM pVM)
1388{
1389 return pVM->cpum.s.enmCPUVendor;
1390}
1391
1392
1393VMMDECL(int) CPUMSetGuestDR0(PVM pVM, uint64_t uDr0)
1394{
1395 pVM->cpum.s.Guest.dr[0] = uDr0;
1396 return CPUMRecalcHyperDRx(pVM);
1397}
1398
1399
1400VMMDECL(int) CPUMSetGuestDR1(PVM pVM, uint64_t uDr1)
1401{
1402 pVM->cpum.s.Guest.dr[1] = uDr1;
1403 return CPUMRecalcHyperDRx(pVM);
1404}
1405
1406
1407VMMDECL(int) CPUMSetGuestDR2(PVM pVM, uint64_t uDr2)
1408{
1409 pVM->cpum.s.Guest.dr[2] = uDr2;
1410 return CPUMRecalcHyperDRx(pVM);
1411}
1412
1413
1414VMMDECL(int) CPUMSetGuestDR3(PVM pVM, uint64_t uDr3)
1415{
1416 pVM->cpum.s.Guest.dr[3] = uDr3;
1417 return CPUMRecalcHyperDRx(pVM);
1418}
1419
1420
1421VMMDECL(int) CPUMSetGuestDR6(PVM pVM, uint64_t uDr6)
1422{
1423 pVM->cpum.s.Guest.dr[6] = uDr6;
1424 return CPUMRecalcHyperDRx(pVM);
1425}
1426
1427
1428VMMDECL(int) CPUMSetGuestDR7(PVM pVM, uint64_t uDr7)
1429{
1430 pVM->cpum.s.Guest.dr[7] = uDr7;
1431 return CPUMRecalcHyperDRx(pVM);
1432}
1433
1434
1435VMMDECL(int) CPUMSetGuestDRx(PVM pVM, uint32_t iReg, uint64_t Value)
1436{
1437 AssertReturn(iReg <= USE_REG_DR7, VERR_INVALID_PARAMETER);
1438 /* DR4 is an alias for DR6, and DR5 is an alias for DR7. */
1439 if (iReg == 4 || iReg == 5)
1440 iReg += 2;
1441 pVM->cpum.s.Guest.dr[iReg] = Value;
1442 return CPUMRecalcHyperDRx(pVM);
1443}
1444
1445
1446/**
1447 * Recalculates the hypvervisor DRx register values based on
1448 * current guest registers and DBGF breakpoints.
1449 *
1450 * This is called whenever a guest DRx register is modified and when DBGF
1451 * sets a hardware breakpoint. In guest context this function will reload
1452 * any (hyper) DRx registers which comes out with a different value.
1453 *
1454 * @returns VINF_SUCCESS.
1455 * @param pVM The VM handle.
1456 */
1457VMMDECL(int) CPUMRecalcHyperDRx(PVM pVM)
1458{
1459 /*
1460 * Compare the DR7s first.
1461 *
1462 * We only care about the enabled flags. The GE and LE flags are always
1463 * set and we don't care if the guest doesn't set them. GD is virtualized
1464 * when we dispatch #DB, we never enable it.
1465 */
1466 const RTGCUINTREG uDbgfDr7 = DBGFBpGetDR7(pVM);
1467#ifdef CPUM_VIRTUALIZE_DRX
1468 const RTGCUINTREG uGstDr7 = CPUMGetGuestDR7(pVM);
1469#else
1470 const RTGCUINTREG uGstDr7 = 0;
1471#endif
1472 if ((uGstDr7 | uDbgfDr7) & X86_DR7_ENABLED_MASK)
1473 {
1474 /*
1475 * Ok, something is enabled. Recalc each of the breakpoints.
1476 * Straight forward code, not optimized/minimized in any way.
1477 */
1478 RTGCUINTREG uNewDr7 = X86_DR7_GE | X86_DR7_LE | X86_DR7_MB1_MASK;
1479
1480 /* bp 0 */
1481 RTGCUINTREG uNewDr0;
1482 if (uDbgfDr7 & (X86_DR7_L0 | X86_DR7_G0))
1483 {
1484 uNewDr7 |= uDbgfDr7 & (X86_DR7_L0 | X86_DR7_G0 | X86_DR7_RW0_MASK | X86_DR7_LEN0_MASK);
1485 uNewDr0 = DBGFBpGetDR0(pVM);
1486 }
1487 else if (uGstDr7 & (X86_DR7_L0 | X86_DR7_G0))
1488 {
1489 uNewDr7 |= uGstDr7 & (X86_DR7_L0 | X86_DR7_G0 | X86_DR7_RW0_MASK | X86_DR7_LEN0_MASK);
1490 uNewDr0 = CPUMGetGuestDR0(pVM);
1491 }
1492 else
1493 uNewDr0 = pVM->cpum.s.Hyper.dr[0];
1494
1495 /* bp 1 */
1496 RTGCUINTREG uNewDr1;
1497 if (uDbgfDr7 & (X86_DR7_L1 | X86_DR7_G1))
1498 {
1499 uNewDr7 |= uDbgfDr7 & (X86_DR7_L1 | X86_DR7_G1 | X86_DR7_RW1_MASK | X86_DR7_LEN1_MASK);
1500 uNewDr1 = DBGFBpGetDR1(pVM);
1501 }
1502 else if (uGstDr7 & (X86_DR7_L1 | X86_DR7_G1))
1503 {
1504 uNewDr7 |= uGstDr7 & (X86_DR7_L1 | X86_DR7_G1 | X86_DR7_RW1_MASK | X86_DR7_LEN1_MASK);
1505 uNewDr1 = CPUMGetGuestDR1(pVM);
1506 }
1507 else
1508 uNewDr1 = pVM->cpum.s.Hyper.dr[1];
1509
1510 /* bp 2 */
1511 RTGCUINTREG uNewDr2;
1512 if (uDbgfDr7 & (X86_DR7_L2 | X86_DR7_G2))
1513 {
1514 uNewDr7 |= uDbgfDr7 & (X86_DR7_L2 | X86_DR7_G2 | X86_DR7_RW2_MASK | X86_DR7_LEN2_MASK);
1515 uNewDr2 = DBGFBpGetDR2(pVM);
1516 }
1517 else if (uGstDr7 & (X86_DR7_L2 | X86_DR7_G2))
1518 {
1519 uNewDr7 |= uGstDr7 & (X86_DR7_L2 | X86_DR7_G2 | X86_DR7_RW2_MASK | X86_DR7_LEN2_MASK);
1520 uNewDr2 = CPUMGetGuestDR2(pVM);
1521 }
1522 else
1523 uNewDr2 = pVM->cpum.s.Hyper.dr[2];
1524
1525 /* bp 3 */
1526 RTGCUINTREG uNewDr3;
1527 if (uDbgfDr7 & (X86_DR7_L3 | X86_DR7_G3))
1528 {
1529 uNewDr7 |= uDbgfDr7 & (X86_DR7_L3 | X86_DR7_G3 | X86_DR7_RW3_MASK | X86_DR7_LEN3_MASK);
1530 uNewDr3 = DBGFBpGetDR3(pVM);
1531 }
1532 else if (uGstDr7 & (X86_DR7_L3 | X86_DR7_G3))
1533 {
1534 uNewDr7 |= uGstDr7 & (X86_DR7_L3 | X86_DR7_G3 | X86_DR7_RW3_MASK | X86_DR7_LEN3_MASK);
1535 uNewDr3 = CPUMGetGuestDR3(pVM);
1536 }
1537 else
1538 uNewDr3 = pVM->cpum.s.Hyper.dr[3];
1539
1540 /*
1541 * Apply the updates.
1542 */
1543#ifdef IN_GC
1544 if (!(pVM->cpum.s.fUseFlags & CPUM_USE_DEBUG_REGS))
1545 {
1546 /** @todo save host DBx registers. */
1547 }
1548#endif
1549 pVM->cpum.s.fUseFlags |= CPUM_USE_DEBUG_REGS;
1550 if (uNewDr3 != pVM->cpum.s.Hyper.dr[3])
1551 CPUMSetHyperDR3(pVM, uNewDr3);
1552 if (uNewDr2 != pVM->cpum.s.Hyper.dr[2])
1553 CPUMSetHyperDR2(pVM, uNewDr2);
1554 if (uNewDr1 != pVM->cpum.s.Hyper.dr[1])
1555 CPUMSetHyperDR1(pVM, uNewDr1);
1556 if (uNewDr0 != pVM->cpum.s.Hyper.dr[0])
1557 CPUMSetHyperDR0(pVM, uNewDr0);
1558 if (uNewDr7 != pVM->cpum.s.Hyper.dr[7])
1559 CPUMSetHyperDR7(pVM, uNewDr7);
1560 }
1561 else
1562 {
1563#ifdef IN_GC
1564 if (pVM->cpum.s.fUseFlags & CPUM_USE_DEBUG_REGS)
1565 {
1566 /** @todo restore host DBx registers. */
1567 }
1568#endif
1569 pVM->cpum.s.fUseFlags &= ~CPUM_USE_DEBUG_REGS;
1570 }
1571 Log2(("CPUMRecalcHyperDRx: fUseFlags=%#x %RGr %RGr %RGr %RGr %RGr %RGr\n",
1572 pVM->cpum.s.fUseFlags, pVM->cpum.s.Hyper.dr[0], pVM->cpum.s.Hyper.dr[1],
1573 pVM->cpum.s.Hyper.dr[2], pVM->cpum.s.Hyper.dr[3], pVM->cpum.s.Hyper.dr[6],
1574 pVM->cpum.s.Hyper.dr[7]));
1575
1576 return VINF_SUCCESS;
1577}
1578
1579#ifndef IN_RING0 /** @todo I don't think we need this in R0, so move it to CPUMAll.cpp? */
1580
1581/**
1582 * Transforms the guest CPU state to raw-ring mode.
1583 *
1584 * This function will change the any of the cs and ss register with DPL=0 to DPL=1.
1585 *
1586 * @returns VBox status. (recompiler failure)
1587 * @param pVM VM handle.
1588 * @param pCtxCore The context core (for trap usage).
1589 * @see @ref pg_raw
1590 */
1591VMMDECL(int) CPUMRawEnter(PVM pVM, PCPUMCTXCORE pCtxCore)
1592{
1593 Assert(!pVM->cpum.s.fRawEntered);
1594 if (!pCtxCore)
1595 pCtxCore = CPUMCTX2CORE(&pVM->cpum.s.Guest);
1596
1597 /*
1598 * Are we in Ring-0?
1599 */
1600 if ( pCtxCore->ss && (pCtxCore->ss & X86_SEL_RPL) == 0
1601 && !pCtxCore->eflags.Bits.u1VM)
1602 {
1603 /*
1604 * Enter execution mode.
1605 */
1606 PATMRawEnter(pVM, pCtxCore);
1607
1608 /*
1609 * Set CPL to Ring-1.
1610 */
1611 pCtxCore->ss |= 1;
1612 if (pCtxCore->cs && (pCtxCore->cs & X86_SEL_RPL) == 0)
1613 pCtxCore->cs |= 1;
1614 }
1615 else
1616 {
1617 AssertMsg((pCtxCore->ss & X86_SEL_RPL) >= 2 || pCtxCore->eflags.Bits.u1VM,
1618 ("ring-1 code not supported\n"));
1619 /*
1620 * PATM takes care of IOPL and IF flags for Ring-3 and Ring-2 code as well.
1621 */
1622 PATMRawEnter(pVM, pCtxCore);
1623 }
1624
1625 /*
1626 * Assert sanity.
1627 */
1628 AssertMsg((pCtxCore->eflags.u32 & X86_EFL_IF), ("X86_EFL_IF is clear\n"));
1629 AssertReleaseMsg( pCtxCore->eflags.Bits.u2IOPL < (unsigned)(pCtxCore->ss & X86_SEL_RPL)
1630 || pCtxCore->eflags.Bits.u1VM,
1631 ("X86_EFL_IOPL=%d CPL=%d\n", pCtxCore->eflags.Bits.u2IOPL, pCtxCore->ss & X86_SEL_RPL));
1632 Assert((pVM->cpum.s.Guest.cr0 & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE)) == (X86_CR0_PG | X86_CR0_PE | X86_CR0_WP));
1633 pCtxCore->eflags.u32 |= X86_EFL_IF; /* paranoia */
1634
1635 pVM->cpum.s.fRawEntered = true;
1636 return VINF_SUCCESS;
1637}
1638
1639
1640/**
1641 * Transforms the guest CPU state from raw-ring mode to correct values.
1642 *
1643 * This function will change any selector registers with DPL=1 to DPL=0.
1644 *
1645 * @returns Adjusted rc.
1646 * @param pVM VM handle.
1647 * @param rc Raw mode return code
1648 * @param pCtxCore The context core (for trap usage).
1649 * @see @ref pg_raw
1650 */
1651VMMDECL(int) CPUMRawLeave(PVM pVM, PCPUMCTXCORE pCtxCore, int rc)
1652{
1653 /*
1654 * Don't leave if we've already left (in GC).
1655 */
1656 Assert(pVM->cpum.s.fRawEntered);
1657 if (!pVM->cpum.s.fRawEntered)
1658 return rc;
1659 pVM->cpum.s.fRawEntered = false;
1660
1661 PCPUMCTX pCtx = &pVM->cpum.s.Guest;
1662 if (!pCtxCore)
1663 pCtxCore = CPUMCTX2CORE(pCtx);
1664 Assert(pCtxCore->eflags.Bits.u1VM || (pCtxCore->ss & X86_SEL_RPL));
1665 AssertMsg(pCtxCore->eflags.Bits.u1VM || pCtxCore->eflags.Bits.u2IOPL < (unsigned)(pCtxCore->ss & X86_SEL_RPL),
1666 ("X86_EFL_IOPL=%d CPL=%d\n", pCtxCore->eflags.Bits.u2IOPL, pCtxCore->ss & X86_SEL_RPL));
1667
1668 /*
1669 * Are we executing in raw ring-1?
1670 */
1671 if ( (pCtxCore->ss & X86_SEL_RPL) == 1
1672 && !pCtxCore->eflags.Bits.u1VM)
1673 {
1674 /*
1675 * Leave execution mode.
1676 */
1677 PATMRawLeave(pVM, pCtxCore, rc);
1678 /* Not quite sure if this is really required, but shouldn't harm (too much anyways). */
1679 /** @todo See what happens if we remove this. */
1680 if ((pCtxCore->ds & X86_SEL_RPL) == 1)
1681 pCtxCore->ds &= ~X86_SEL_RPL;
1682 if ((pCtxCore->es & X86_SEL_RPL) == 1)
1683 pCtxCore->es &= ~X86_SEL_RPL;
1684 if ((pCtxCore->fs & X86_SEL_RPL) == 1)
1685 pCtxCore->fs &= ~X86_SEL_RPL;
1686 if ((pCtxCore->gs & X86_SEL_RPL) == 1)
1687 pCtxCore->gs &= ~X86_SEL_RPL;
1688
1689 /*
1690 * Ring-1 selector => Ring-0.
1691 */
1692 pCtxCore->ss &= ~X86_SEL_RPL;
1693 if ((pCtxCore->cs & X86_SEL_RPL) == 1)
1694 pCtxCore->cs &= ~X86_SEL_RPL;
1695 }
1696 else
1697 {
1698 /*
1699 * PATM is taking care of the IOPL and IF flags for us.
1700 */
1701 PATMRawLeave(pVM, pCtxCore, rc);
1702 if (!pCtxCore->eflags.Bits.u1VM)
1703 {
1704 /** @todo See what happens if we remove this. */
1705 if ((pCtxCore->ds & X86_SEL_RPL) == 1)
1706 pCtxCore->ds &= ~X86_SEL_RPL;
1707 if ((pCtxCore->es & X86_SEL_RPL) == 1)
1708 pCtxCore->es &= ~X86_SEL_RPL;
1709 if ((pCtxCore->fs & X86_SEL_RPL) == 1)
1710 pCtxCore->fs &= ~X86_SEL_RPL;
1711 if ((pCtxCore->gs & X86_SEL_RPL) == 1)
1712 pCtxCore->gs &= ~X86_SEL_RPL;
1713 }
1714 }
1715
1716 return rc;
1717}
1718
1719/**
1720 * Updates the EFLAGS while we're in raw-mode.
1721 *
1722 * @param pVM The VM handle.
1723 * @param pCtxCore The context core.
1724 * @param eflags The new EFLAGS value.
1725 */
1726VMMDECL(void) CPUMRawSetEFlags(PVM pVM, PCPUMCTXCORE pCtxCore, uint32_t eflags)
1727{
1728 if (!pVM->cpum.s.fRawEntered)
1729 {
1730 pCtxCore->eflags.u32 = eflags;
1731 return;
1732 }
1733 PATMRawSetEFlags(pVM, pCtxCore, eflags);
1734}
1735
1736#endif /* !IN_RING0 */
1737
1738/**
1739 * Gets the EFLAGS while we're in raw-mode.
1740 *
1741 * @returns The eflags.
1742 * @param pVM The VM handle.
1743 * @param pCtxCore The context core.
1744 */
1745VMMDECL(uint32_t) CPUMRawGetEFlags(PVM pVM, PCPUMCTXCORE pCtxCore)
1746{
1747#ifdef IN_RING0
1748 return pCtxCore->eflags.u32;
1749#else
1750 if (!pVM->cpum.s.fRawEntered)
1751 return pCtxCore->eflags.u32;
1752 return PATMRawGetEFlags(pVM, pCtxCore);
1753#endif
1754}
1755
1756
1757/**
1758 * Gets and resets the changed flags (CPUM_CHANGED_*).
1759 * Only REM should call this function.
1760 *
1761 * @returns The changed flags.
1762 * @param pVM The VM handle.
1763 */
1764VMMDECL(unsigned) CPUMGetAndClearChangedFlagsREM(PVM pVM)
1765{
1766 unsigned fFlags = pVM->cpum.s.fChanged;
1767 pVM->cpum.s.fChanged = 0;
1768 /** @todo change the switcher to use the fChanged flags. */
1769 if (pVM->cpum.s.fUseFlags & CPUM_USED_FPU_SINCE_REM)
1770 {
1771 fFlags |= CPUM_CHANGED_FPU_REM;
1772 pVM->cpum.s.fUseFlags &= ~CPUM_USED_FPU_SINCE_REM;
1773 }
1774 return fFlags;
1775}
1776
1777
1778/**
1779 * Sets the specified changed flags (CPUM_CHANGED_*).
1780 *
1781 * @param pVM The VM handle.
1782 */
1783VMMDECL(void) CPUMSetChangedFlags(PVM pVM, uint32_t fChangedFlags)
1784{
1785 pVM->cpum.s.fChanged |= fChangedFlags;
1786}
1787
1788
1789/**
1790 * Checks if the CPU supports the FXSAVE and FXRSTOR instruction.
1791 * @returns true if supported.
1792 * @returns false if not supported.
1793 * @param pVM The VM handle.
1794 */
1795VMMDECL(bool) CPUMSupportsFXSR(PVM pVM)
1796{
1797 return pVM->cpum.s.CPUFeatures.edx.u1FXSR != 0;
1798}
1799
1800
1801/**
1802 * Checks if the host OS uses the SYSENTER / SYSEXIT instructions.
1803 * @returns true if used.
1804 * @returns false if not used.
1805 * @param pVM The VM handle.
1806 */
1807VMMDECL(bool) CPUMIsHostUsingSysEnter(PVM pVM)
1808{
1809 return (pVM->cpum.s.fUseFlags & CPUM_USE_SYSENTER) != 0;
1810}
1811
1812
1813/**
1814 * Checks if the host OS uses the SYSCALL / SYSRET instructions.
1815 * @returns true if used.
1816 * @returns false if not used.
1817 * @param pVM The VM handle.
1818 */
1819VMMDECL(bool) CPUMIsHostUsingSysCall(PVM pVM)
1820{
1821 return (pVM->cpum.s.fUseFlags & CPUM_USE_SYSCALL) != 0;
1822}
1823
1824#ifndef IN_RING3
1825
1826/**
1827 * Lazily sync in the FPU/XMM state
1828 *
1829 * @returns VBox status code.
1830 * @param pVM VM handle.
1831 */
1832VMMDECL(int) CPUMHandleLazyFPU(PVM pVM)
1833{
1834 return CPUMHandleLazyFPUAsm(&pVM->cpum.s);
1835}
1836
1837
1838/**
1839 * Restore host FPU/XMM state
1840 *
1841 * @returns VBox status code.
1842 * @param pVM VM handle.
1843 */
1844VMMDECL(int) CPUMRestoreHostFPUState(PVM pVM)
1845{
1846 Assert(pVM->cpum.s.CPUFeatures.edx.u1FXSR);
1847 return CPUMRestoreHostFPUStateAsm(&pVM->cpum.s);
1848}
1849
1850#endif /* !IN_RING3 */
1851
1852/**
1853 * Checks if we activated the FPU/XMM state of the guest OS
1854 * @returns true if we did.
1855 * @returns false if not.
1856 * @param pVM The VM handle.
1857 */
1858VMMDECL(bool) CPUMIsGuestFPUStateActive(PVM pVM)
1859{
1860 return (pVM->cpum.s.fUseFlags & CPUM_USED_FPU) != 0;
1861}
1862
1863
1864/**
1865 * Deactivate the FPU/XMM state of the guest OS
1866 * @param pVM The VM handle.
1867 */
1868VMMDECL(void) CPUMDeactivateGuestFPUState(PVM pVM)
1869{
1870 pVM->cpum.s.fUseFlags &= ~CPUM_USED_FPU;
1871}
1872
1873
1874/**
1875 * Checks if the guest debug state is active
1876 *
1877 * @returns boolean
1878 * @param pVM VM handle.
1879 */
1880VMMDECL(bool) CPUMIsGuestDebugStateActive(PVM pVM)
1881{
1882 return (pVM->cpum.s.fUseFlags & CPUM_USE_DEBUG_REGS) != 0;
1883}
1884
1885
1886/**
1887 * Mark the guest's debug state as inactive
1888 *
1889 * @returns boolean
1890 * @param pVM VM handle.
1891 */
1892VMMDECL(void) CPUMDeactivateGuestDebugState(PVM pVM)
1893{
1894 pVM->cpum.s.fUseFlags &= ~CPUM_USE_DEBUG_REGS;
1895}
1896
1897
1898/**
1899 * Checks if the hidden selector registers are valid
1900 * @returns true if they are.
1901 * @returns false if not.
1902 * @param pVM The VM handle.
1903 */
1904VMMDECL(bool) CPUMAreHiddenSelRegsValid(PVM pVM)
1905{
1906 return !!pVM->cpum.s.fValidHiddenSelRegs; /** @todo change fValidHiddenSelRegs to bool! */
1907}
1908
1909
1910/**
1911 * Checks if the hidden selector registers are valid
1912 * @param pVM The VM handle.
1913 * @param fValid Valid or not
1914 */
1915VMMDECL(void) CPUMSetHiddenSelRegsValid(PVM pVM, bool fValid)
1916{
1917 pVM->cpum.s.fValidHiddenSelRegs = fValid;
1918}
1919
1920
1921/**
1922 * Get the current privilege level of the guest.
1923 *
1924 * @returns cpl
1925 * @param pVM VM Handle.
1926 * @param pRegFrame Trap register frame.
1927 */
1928VMMDECL(uint32_t) CPUMGetGuestCPL(PVM pVM, PCPUMCTXCORE pCtxCore)
1929{
1930 uint32_t cpl;
1931
1932 if (CPUMAreHiddenSelRegsValid(pVM))
1933 {
1934 /*
1935 * The hidden CS.DPL register is always equal to the CPL, it is
1936 * not affected by loading a conforming coding segment.
1937 *
1938 * This only seems to apply to AMD-V; in the VT-x case we *do* need to look
1939 * at SS. (ACP2 regression during install after a far call to ring 2)
1940 */
1941 if (RT_LIKELY(pVM->cpum.s.Guest.cr0 & X86_CR0_PE))
1942 cpl = pCtxCore->ssHid.Attr.n.u2Dpl;
1943 else
1944 cpl = 0; /* CPL set to 3 for VT-x real-mode emulation. */
1945 }
1946 else if (RT_LIKELY(pVM->cpum.s.Guest.cr0 & X86_CR0_PE))
1947 {
1948 if (RT_LIKELY(!pCtxCore->eflags.Bits.u1VM))
1949 {
1950 /*
1951 * The SS RPL is always equal to the CPL, while the CS RPL
1952 * isn't necessarily equal if the segment is conforming.
1953 * See section 4.11.1 in the AMD manual.
1954 */
1955 cpl = (pCtxCore->ss & X86_SEL_RPL);
1956#ifndef IN_RING0
1957 if (cpl == 1)
1958 cpl = 0;
1959#endif
1960 }
1961 else
1962 cpl = 3;
1963 }
1964 else
1965 cpl = 0; /* real mode; cpl is zero */
1966
1967 return cpl;
1968}
1969
1970
1971/**
1972 * Gets the current guest CPU mode.
1973 *
1974 * If paging mode is what you need, check out PGMGetGuestMode().
1975 *
1976 * @returns The CPU mode.
1977 * @param pVM The VM handle.
1978 */
1979VMMDECL(CPUMMODE) CPUMGetGuestMode(PVM pVM)
1980{
1981 CPUMMODE enmMode;
1982 if (!(pVM->cpum.s.Guest.cr0 & X86_CR0_PE))
1983 enmMode = CPUMMODE_REAL;
1984 else if (!(pVM->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA))
1985 enmMode = CPUMMODE_PROTECTED;
1986 else
1987 enmMode = CPUMMODE_LONG;
1988
1989 return enmMode;
1990}
1991
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