VirtualBox

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

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

VMM + VBox/cdefs.h: consolidated all the XYZ*DECLS of the VMM into VMM*DECL. Removed dead DECL and IN_XYZ* macros.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 50.5 KB
Line 
1/* $Id: CPUMAllRegs.cpp 12989 2008-10-06 02:15:39Z 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 VBox status code.
455 * @param pVM Handle to the virtual machine.
456 * @param ppCtx Receives the CPUMCTX pointer when successful.
457 */
458VMMDECL(int) CPUMQueryGuestCtxPtr(PVM pVM, PCPUMCTX *ppCtx)
459{
460 *ppCtx = &pVM->cpum.s.Guest;
461 return VINF_SUCCESS;
462}
463
464
465VMMDECL(int) CPUMSetGuestGDTR(PVM pVM, uint32_t addr, uint16_t limit)
466{
467 pVM->cpum.s.Guest.gdtr.cbGdt = limit;
468 pVM->cpum.s.Guest.gdtr.pGdt = addr;
469 pVM->cpum.s.fChanged |= CPUM_CHANGED_GDTR;
470 return VINF_SUCCESS;
471}
472
473VMMDECL(int) CPUMSetGuestIDTR(PVM pVM, uint32_t addr, uint16_t limit)
474{
475 pVM->cpum.s.Guest.idtr.cbIdt = limit;
476 pVM->cpum.s.Guest.idtr.pIdt = addr;
477 pVM->cpum.s.fChanged |= CPUM_CHANGED_IDTR;
478 return VINF_SUCCESS;
479}
480
481VMMDECL(int) CPUMSetGuestTR(PVM pVM, uint16_t tr)
482{
483 pVM->cpum.s.Guest.tr = tr;
484 pVM->cpum.s.fChanged |= CPUM_CHANGED_TR;
485 return VINF_SUCCESS;
486}
487
488VMMDECL(int) CPUMSetGuestLDTR(PVM pVM, uint16_t ldtr)
489{
490 pVM->cpum.s.Guest.ldtr = ldtr;
491 pVM->cpum.s.fChanged |= CPUM_CHANGED_LDTR;
492 return VINF_SUCCESS;
493}
494
495
496/**
497 * Set the guest CR0.
498 *
499 * When called in GC, the hyper CR0 may be updated if that is
500 * required. The caller only has to take special action if AM,
501 * WP, PG or PE changes.
502 *
503 * @returns VINF_SUCCESS (consider it void).
504 * @param pVM Pointer to the shared VM structure.
505 * @param cr0 The new CR0 value.
506 */
507VMMDECL(int) CPUMSetGuestCR0(PVM pVM, uint64_t cr0)
508{
509#ifdef IN_GC
510 /*
511 * Check if we need to change hypervisor CR0 because
512 * of math stuff.
513 */
514 if ( (cr0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP))
515 != (pVM->cpum.s.Guest.cr0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP)))
516 {
517 if (!(pVM->cpum.s.fUseFlags & CPUM_USED_FPU))
518 {
519 /*
520 * We haven't saved the host FPU state yet, so TS and MT are both set
521 * and EM should be reflecting the guest EM (it always does this).
522 */
523 if ((cr0 & X86_CR0_EM) != (pVM->cpum.s.Guest.cr0 & X86_CR0_EM))
524 {
525 uint32_t HyperCR0 = ASMGetCR0();
526 AssertMsg((HyperCR0 & (X86_CR0_TS | X86_CR0_MP)) == (X86_CR0_TS | X86_CR0_MP), ("%#x\n", HyperCR0));
527 AssertMsg((HyperCR0 & X86_CR0_EM) == (pVM->cpum.s.Guest.cr0 & X86_CR0_EM), ("%#x\n", HyperCR0));
528 HyperCR0 &= ~X86_CR0_EM;
529 HyperCR0 |= cr0 & X86_CR0_EM;
530 Log(("CPUM New HyperCR0=%#x\n", HyperCR0));
531 ASMSetCR0(HyperCR0);
532 }
533# ifdef VBOX_STRICT
534 else
535 {
536 uint32_t HyperCR0 = ASMGetCR0();
537 AssertMsg((HyperCR0 & (X86_CR0_TS | X86_CR0_MP)) == (X86_CR0_TS | X86_CR0_MP), ("%#x\n", HyperCR0));
538 AssertMsg((HyperCR0 & X86_CR0_EM) == (pVM->cpum.s.Guest.cr0 & X86_CR0_EM), ("%#x\n", HyperCR0));
539 }
540# endif
541 }
542 else
543 {
544 /*
545 * Already saved the state, so we're just mirroring
546 * the guest flags.
547 */
548 uint32_t HyperCR0 = ASMGetCR0();
549 AssertMsg( (HyperCR0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP))
550 == (pVM->cpum.s.Guest.cr0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP)),
551 ("%#x %#x\n", HyperCR0, pVM->cpum.s.Guest.cr0));
552 HyperCR0 &= ~(X86_CR0_TS | X86_CR0_EM | X86_CR0_MP);
553 HyperCR0 |= cr0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP);
554 Log(("CPUM New HyperCR0=%#x\n", HyperCR0));
555 ASMSetCR0(HyperCR0);
556 }
557 }
558#endif /* IN_GC */
559
560 /*
561 * Check for changes causing TLB flushes (for REM).
562 * The caller is responsible for calling PGM when appropriate.
563 */
564 if ( (cr0 & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE))
565 != (pVM->cpum.s.Guest.cr0 & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE)))
566 pVM->cpum.s.fChanged |= CPUM_CHANGED_GLOBAL_TLB_FLUSH;
567 pVM->cpum.s.fChanged |= CPUM_CHANGED_CR0;
568
569 pVM->cpum.s.Guest.cr0 = cr0 | X86_CR0_ET;
570 return VINF_SUCCESS;
571}
572
573
574VMMDECL(int) CPUMSetGuestCR2(PVM pVM, uint64_t cr2)
575{
576 pVM->cpum.s.Guest.cr2 = cr2;
577 return VINF_SUCCESS;
578}
579
580
581VMMDECL(int) CPUMSetGuestCR3(PVM pVM, uint64_t cr3)
582{
583 pVM->cpum.s.Guest.cr3 = cr3;
584 pVM->cpum.s.fChanged |= CPUM_CHANGED_CR3;
585 return VINF_SUCCESS;
586}
587
588
589VMMDECL(int) CPUMSetGuestCR4(PVM pVM, uint64_t cr4)
590{
591 if ( (cr4 & (X86_CR4_PGE | X86_CR4_PAE | X86_CR4_PSE))
592 != (pVM->cpum.s.Guest.cr4 & (X86_CR4_PGE | X86_CR4_PAE | X86_CR4_PSE)))
593 pVM->cpum.s.fChanged |= CPUM_CHANGED_GLOBAL_TLB_FLUSH;
594 pVM->cpum.s.fChanged |= CPUM_CHANGED_CR4;
595 if (!CPUMSupportsFXSR(pVM))
596 cr4 &= ~X86_CR4_OSFSXR;
597 pVM->cpum.s.Guest.cr4 = cr4;
598 return VINF_SUCCESS;
599}
600
601
602VMMDECL(int) CPUMSetGuestEFlags(PVM pVM, uint32_t eflags)
603{
604 pVM->cpum.s.Guest.eflags.u32 = eflags;
605 return VINF_SUCCESS;
606}
607
608
609VMMDECL(int) CPUMSetGuestEIP(PVM pVM, uint32_t eip)
610{
611 pVM->cpum.s.Guest.eip = eip;
612 return VINF_SUCCESS;
613}
614
615
616VMMDECL(int) CPUMSetGuestEAX(PVM pVM, uint32_t eax)
617{
618 pVM->cpum.s.Guest.eax = eax;
619 return VINF_SUCCESS;
620}
621
622
623VMMDECL(int) CPUMSetGuestEBX(PVM pVM, uint32_t ebx)
624{
625 pVM->cpum.s.Guest.ebx = ebx;
626 return VINF_SUCCESS;
627}
628
629
630VMMDECL(int) CPUMSetGuestECX(PVM pVM, uint32_t ecx)
631{
632 pVM->cpum.s.Guest.ecx = ecx;
633 return VINF_SUCCESS;
634}
635
636
637VMMDECL(int) CPUMSetGuestEDX(PVM pVM, uint32_t edx)
638{
639 pVM->cpum.s.Guest.edx = edx;
640 return VINF_SUCCESS;
641}
642
643
644VMMDECL(int) CPUMSetGuestESP(PVM pVM, uint32_t esp)
645{
646 pVM->cpum.s.Guest.esp = esp;
647 return VINF_SUCCESS;
648}
649
650
651VMMDECL(int) CPUMSetGuestEBP(PVM pVM, uint32_t ebp)
652{
653 pVM->cpum.s.Guest.ebp = ebp;
654 return VINF_SUCCESS;
655}
656
657
658VMMDECL(int) CPUMSetGuestESI(PVM pVM, uint32_t esi)
659{
660 pVM->cpum.s.Guest.esi = esi;
661 return VINF_SUCCESS;
662}
663
664
665VMMDECL(int) CPUMSetGuestEDI(PVM pVM, uint32_t edi)
666{
667 pVM->cpum.s.Guest.edi = edi;
668 return VINF_SUCCESS;
669}
670
671
672VMMDECL(int) CPUMSetGuestSS(PVM pVM, uint16_t ss)
673{
674 pVM->cpum.s.Guest.ss = ss;
675 return VINF_SUCCESS;
676}
677
678
679VMMDECL(int) CPUMSetGuestCS(PVM pVM, uint16_t cs)
680{
681 pVM->cpum.s.Guest.cs = cs;
682 return VINF_SUCCESS;
683}
684
685
686VMMDECL(int) CPUMSetGuestDS(PVM pVM, uint16_t ds)
687{
688 pVM->cpum.s.Guest.ds = ds;
689 return VINF_SUCCESS;
690}
691
692
693VMMDECL(int) CPUMSetGuestES(PVM pVM, uint16_t es)
694{
695 pVM->cpum.s.Guest.es = es;
696 return VINF_SUCCESS;
697}
698
699
700VMMDECL(int) CPUMSetGuestFS(PVM pVM, uint16_t fs)
701{
702 pVM->cpum.s.Guest.fs = fs;
703 return VINF_SUCCESS;
704}
705
706
707VMMDECL(int) CPUMSetGuestGS(PVM pVM, uint16_t gs)
708{
709 pVM->cpum.s.Guest.gs = gs;
710 return VINF_SUCCESS;
711}
712
713
714VMMDECL(void) CPUMSetGuestEFER(PVM pVM, uint64_t val)
715{
716 pVM->cpum.s.Guest.msrEFER = val;
717}
718
719
720VMMDECL(uint64_t) CPUMGetGuestMsr(PVM pVM, unsigned idMsr)
721{
722 uint64_t u64 = 0;
723
724 switch (idMsr)
725 {
726 case MSR_IA32_CR_PAT:
727 u64 = pVM->cpum.s.Guest.msrPAT;
728 break;
729
730 case MSR_IA32_SYSENTER_CS:
731 u64 = pVM->cpum.s.Guest.SysEnter.cs;
732 break;
733
734 case MSR_IA32_SYSENTER_EIP:
735 u64 = pVM->cpum.s.Guest.SysEnter.eip;
736 break;
737
738 case MSR_IA32_SYSENTER_ESP:
739 u64 = pVM->cpum.s.Guest.SysEnter.esp;
740 break;
741
742 case MSR_K6_EFER:
743 u64 = pVM->cpum.s.Guest.msrEFER;
744 break;
745
746 case MSR_K8_SF_MASK:
747 u64 = pVM->cpum.s.Guest.msrSFMASK;
748 break;
749
750 case MSR_K6_STAR:
751 u64 = pVM->cpum.s.Guest.msrSTAR;
752 break;
753
754 case MSR_K8_LSTAR:
755 u64 = pVM->cpum.s.Guest.msrLSTAR;
756 break;
757
758 case MSR_K8_CSTAR:
759 u64 = pVM->cpum.s.Guest.msrCSTAR;
760 break;
761
762 case MSR_K8_KERNEL_GS_BASE:
763 u64 = pVM->cpum.s.Guest.msrKERNELGSBASE;
764 break;
765
766 /* fs & gs base skipped on purpose as the current context might not be up-to-date. */
767 default:
768 AssertFailed();
769 break;
770 }
771 return u64;
772}
773
774
775VMMDECL(RTGCPTR) CPUMGetGuestIDTR(PVM pVM, uint16_t *pcbLimit)
776{
777 if (pcbLimit)
778 *pcbLimit = pVM->cpum.s.Guest.idtr.cbIdt;
779 return pVM->cpum.s.Guest.idtr.pIdt;
780}
781
782
783VMMDECL(RTSEL) CPUMGetGuestTR(PVM pVM)
784{
785 return pVM->cpum.s.Guest.tr;
786}
787
788
789VMMDECL(RTSEL) CPUMGetGuestCS(PVM pVM)
790{
791 return pVM->cpum.s.Guest.cs;
792}
793
794
795VMMDECL(RTSEL) CPUMGetGuestDS(PVM pVM)
796{
797 return pVM->cpum.s.Guest.ds;
798}
799
800
801VMMDECL(RTSEL) CPUMGetGuestES(PVM pVM)
802{
803 return pVM->cpum.s.Guest.es;
804}
805
806
807VMMDECL(RTSEL) CPUMGetGuestFS(PVM pVM)
808{
809 return pVM->cpum.s.Guest.fs;
810}
811
812
813VMMDECL(RTSEL) CPUMGetGuestGS(PVM pVM)
814{
815 return pVM->cpum.s.Guest.gs;
816}
817
818
819VMMDECL(RTSEL) CPUMGetGuestSS(PVM pVM)
820{
821 return pVM->cpum.s.Guest.ss;
822}
823
824
825VMMDECL(RTSEL) CPUMGetGuestLDTR(PVM pVM)
826{
827 return pVM->cpum.s.Guest.ldtr;
828}
829
830
831VMMDECL(uint64_t) CPUMGetGuestCR0(PVM pVM)
832{
833 return pVM->cpum.s.Guest.cr0;
834}
835
836
837VMMDECL(uint64_t) CPUMGetGuestCR2(PVM pVM)
838{
839 return pVM->cpum.s.Guest.cr2;
840}
841
842
843VMMDECL(uint64_t) CPUMGetGuestCR3(PVM pVM)
844{
845 return pVM->cpum.s.Guest.cr3;
846}
847
848
849VMMDECL(uint64_t) CPUMGetGuestCR4(PVM pVM)
850{
851 return pVM->cpum.s.Guest.cr4;
852}
853
854
855VMMDECL(void) CPUMGetGuestGDTR(PVM pVM, PVBOXGDTR pGDTR)
856{
857 *pGDTR = pVM->cpum.s.Guest.gdtr;
858}
859
860
861VMMDECL(uint32_t) CPUMGetGuestEIP(PVM pVM)
862{
863 return pVM->cpum.s.Guest.eip;
864}
865
866
867VMMDECL(uint64_t) CPUMGetGuestRIP(PVM pVM)
868{
869 return pVM->cpum.s.Guest.rip;
870}
871
872
873VMMDECL(uint32_t) CPUMGetGuestEAX(PVM pVM)
874{
875 return pVM->cpum.s.Guest.eax;
876}
877
878
879VMMDECL(uint32_t) CPUMGetGuestEBX(PVM pVM)
880{
881 return pVM->cpum.s.Guest.ebx;
882}
883
884
885VMMDECL(uint32_t) CPUMGetGuestECX(PVM pVM)
886{
887 return pVM->cpum.s.Guest.ecx;
888}
889
890
891VMMDECL(uint32_t) CPUMGetGuestEDX(PVM pVM)
892{
893 return pVM->cpum.s.Guest.edx;
894}
895
896
897VMMDECL(uint32_t) CPUMGetGuestESI(PVM pVM)
898{
899 return pVM->cpum.s.Guest.esi;
900}
901
902
903VMMDECL(uint32_t) CPUMGetGuestEDI(PVM pVM)
904{
905 return pVM->cpum.s.Guest.edi;
906}
907
908
909VMMDECL(uint32_t) CPUMGetGuestESP(PVM pVM)
910{
911 return pVM->cpum.s.Guest.esp;
912}
913
914
915VMMDECL(uint32_t) CPUMGetGuestEBP(PVM pVM)
916{
917 return pVM->cpum.s.Guest.ebp;
918}
919
920
921VMMDECL(uint32_t) CPUMGetGuestEFlags(PVM pVM)
922{
923 return pVM->cpum.s.Guest.eflags.u32;
924}
925
926
927VMMDECL(CPUMSELREGHID *) CPUMGetGuestTRHid(PVM pVM)
928{
929 return &pVM->cpum.s.Guest.trHid;
930}
931
932
933///@todo: crx should be an array
934VMMDECL(int) CPUMGetGuestCRx(PVM pVM, unsigned iReg, uint64_t *pValue)
935{
936 switch (iReg)
937 {
938 case USE_REG_CR0:
939 *pValue = pVM->cpum.s.Guest.cr0;
940 break;
941 case USE_REG_CR2:
942 *pValue = pVM->cpum.s.Guest.cr2;
943 break;
944 case USE_REG_CR3:
945 *pValue = pVM->cpum.s.Guest.cr3;
946 break;
947 case USE_REG_CR4:
948 *pValue = pVM->cpum.s.Guest.cr4;
949 break;
950 default:
951 return VERR_INVALID_PARAMETER;
952 }
953 return VINF_SUCCESS;
954}
955
956
957VMMDECL(uint64_t) CPUMGetGuestDR0(PVM pVM)
958{
959 return pVM->cpum.s.Guest.dr[0];
960}
961
962
963VMMDECL(uint64_t) CPUMGetGuestDR1(PVM pVM)
964{
965 return pVM->cpum.s.Guest.dr[1];
966}
967
968
969VMMDECL(uint64_t) CPUMGetGuestDR2(PVM pVM)
970{
971 return pVM->cpum.s.Guest.dr[2];
972}
973
974
975VMMDECL(uint64_t) CPUMGetGuestDR3(PVM pVM)
976{
977 return pVM->cpum.s.Guest.dr[3];
978}
979
980
981VMMDECL(uint64_t) CPUMGetGuestDR6(PVM pVM)
982{
983 return pVM->cpum.s.Guest.dr[6];
984}
985
986
987VMMDECL(uint64_t) CPUMGetGuestDR7(PVM pVM)
988{
989 return pVM->cpum.s.Guest.dr[7];
990}
991
992
993VMMDECL(int) CPUMGetGuestDRx(PVM pVM, uint32_t iReg, uint64_t *pValue)
994{
995 AssertReturn(iReg <= USE_REG_DR7, VERR_INVALID_PARAMETER);
996 /* DR4 is an alias for DR6, and DR5 is an alias for DR7. */
997 if (iReg == 4 || iReg == 5)
998 iReg += 2;
999 *pValue = pVM->cpum.s.Guest.dr[iReg];
1000 return VINF_SUCCESS;
1001}
1002
1003
1004VMMDECL(uint64_t) CPUMGetGuestEFER(PVM pVM)
1005{
1006 return pVM->cpum.s.Guest.msrEFER;
1007}
1008
1009
1010/**
1011 * Gets a CpuId leaf.
1012 *
1013 * @param pVM The VM handle.
1014 * @param iLeaf The CPUID leaf to get.
1015 * @param pEax Where to store the EAX value.
1016 * @param pEbx Where to store the EBX value.
1017 * @param pEcx Where to store the ECX value.
1018 * @param pEdx Where to store the EDX value.
1019 */
1020VMMDECL(void) CPUMGetGuestCpuId(PVM pVM, uint32_t iLeaf, uint32_t *pEax, uint32_t *pEbx, uint32_t *pEcx, uint32_t *pEdx)
1021{
1022 PCCPUMCPUID pCpuId;
1023 if (iLeaf < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdStd))
1024 pCpuId = &pVM->cpum.s.aGuestCpuIdStd[iLeaf];
1025 else if (iLeaf - UINT32_C(0x80000000) < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdExt))
1026 pCpuId = &pVM->cpum.s.aGuestCpuIdExt[iLeaf - UINT32_C(0x80000000)];
1027 else if (iLeaf - UINT32_C(0xc0000000) < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdCentaur))
1028 pCpuId = &pVM->cpum.s.aGuestCpuIdCentaur[iLeaf - UINT32_C(0xc0000000)];
1029 else
1030 pCpuId = &pVM->cpum.s.GuestCpuIdDef;
1031
1032 *pEax = pCpuId->eax;
1033 *pEbx = pCpuId->ebx;
1034 *pEcx = pCpuId->ecx;
1035 *pEdx = pCpuId->edx;
1036 Log2(("CPUMGetGuestCpuId: iLeaf=%#010x %RX32 %RX32 %RX32 %RX32\n", iLeaf, *pEax, *pEbx, *pEcx, *pEdx));
1037}
1038
1039
1040/**
1041 * Gets a pointer to the array of standard CPUID leafs.
1042 *
1043 * CPUMGetGuestCpuIdStdMax() give the size of the array.
1044 *
1045 * @returns Pointer to the standard CPUID leafs (read-only).
1046 * @param pVM The VM handle.
1047 * @remark Intended for PATM.
1048 */
1049VMMDECL(RCPTRTYPE(PCCPUMCPUID)) CPUMGetGuestCpuIdStdGCPtr(PVM pVM)
1050{
1051 return RCPTRTYPE(PCCPUMCPUID)VM_GUEST_ADDR(pVM, &pVM->cpum.s.aGuestCpuIdStd[0]);
1052}
1053
1054
1055/**
1056 * Gets a pointer to the array of extended CPUID leafs.
1057 *
1058 * CPUMGetGuestCpuIdExtMax() give the size of the array.
1059 *
1060 * @returns Pointer to the extended CPUID leafs (read-only).
1061 * @param pVM The VM handle.
1062 * @remark Intended for PATM.
1063 */
1064VMMDECL(RCPTRTYPE(PCCPUMCPUID)) CPUMGetGuestCpuIdExtGCPtr(PVM pVM)
1065{
1066 return (RCPTRTYPE(PCCPUMCPUID))VM_GUEST_ADDR(pVM, &pVM->cpum.s.aGuestCpuIdExt[0]);
1067}
1068
1069
1070/**
1071 * Gets a pointer to the array of centaur CPUID leafs.
1072 *
1073 * CPUMGetGuestCpuIdCentaurMax() give the size of the array.
1074 *
1075 * @returns Pointer to the centaur CPUID leafs (read-only).
1076 * @param pVM The VM handle.
1077 * @remark Intended for PATM.
1078 */
1079VMMDECL(RCPTRTYPE(PCCPUMCPUID)) CPUMGetGuestCpuIdCentaurGCPtr(PVM pVM)
1080{
1081 return (RCPTRTYPE(PCCPUMCPUID))VM_GUEST_ADDR(pVM, &pVM->cpum.s.aGuestCpuIdCentaur[0]);
1082}
1083
1084
1085/**
1086 * Gets a pointer to the default CPUID leaf.
1087 *
1088 * @returns Pointer to the default CPUID leaf (read-only).
1089 * @param pVM The VM handle.
1090 * @remark Intended for PATM.
1091 */
1092VMMDECL(RCPTRTYPE(PCCPUMCPUID)) CPUMGetGuestCpuIdDefGCPtr(PVM pVM)
1093{
1094 return (RCPTRTYPE(PCCPUMCPUID))VM_GUEST_ADDR(pVM, &pVM->cpum.s.GuestCpuIdDef);
1095}
1096
1097
1098/**
1099 * Gets a number of standard CPUID leafs.
1100 *
1101 * @returns Number of leafs.
1102 * @param pVM The VM handle.
1103 * @remark Intended for PATM.
1104 */
1105VMMDECL(uint32_t) CPUMGetGuestCpuIdStdMax(PVM pVM)
1106{
1107 return RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdStd);
1108}
1109
1110
1111/**
1112 * Gets a number of extended CPUID leafs.
1113 *
1114 * @returns Number of leafs.
1115 * @param pVM The VM handle.
1116 * @remark Intended for PATM.
1117 */
1118VMMDECL(uint32_t) CPUMGetGuestCpuIdExtMax(PVM pVM)
1119{
1120 return RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdExt);
1121}
1122
1123
1124/**
1125 * Gets a number of centaur CPUID leafs.
1126 *
1127 * @returns Number of leafs.
1128 * @param pVM The VM handle.
1129 * @remark Intended for PATM.
1130 */
1131VMMDECL(uint32_t) CPUMGetGuestCpuIdCentaurMax(PVM pVM)
1132{
1133 return RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdCentaur);
1134}
1135
1136
1137/**
1138 * Sets a CPUID feature bit.
1139 *
1140 * @param pVM The VM Handle.
1141 * @param enmFeature The feature to set.
1142 */
1143VMMDECL(void) CPUMSetGuestCpuIdFeature(PVM pVM, CPUMCPUIDFEATURE enmFeature)
1144{
1145 switch (enmFeature)
1146 {
1147 /*
1148 * Set the APIC bit in both feature masks.
1149 */
1150 case CPUMCPUIDFEATURE_APIC:
1151 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1152 pVM->cpum.s.aGuestCpuIdStd[1].edx |= X86_CPUID_FEATURE_EDX_APIC;
1153 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1154 && pVM->cpum.s.enmCPUVendor == CPUMCPUVENDOR_AMD)
1155 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_APIC;
1156 LogRel(("CPUMSetGuestCpuIdFeature: Enabled APIC\n"));
1157 break;
1158
1159 /*
1160 * Set the x2APIC bit in the standard feature mask.
1161 */
1162 case CPUMCPUIDFEATURE_X2APIC:
1163 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1164 pVM->cpum.s.aGuestCpuIdStd[1].ecx |= X86_CPUID_FEATURE_ECX_X2APIC;
1165 LogRel(("CPUMSetGuestCpuIdFeature: Enabled x2APIC\n"));
1166 break;
1167
1168 /*
1169 * Set the sysenter/sysexit bit in the standard feature mask.
1170 * Assumes the caller knows what it's doing! (host must support these)
1171 */
1172 case CPUMCPUIDFEATURE_SEP:
1173 {
1174 if (!(ASMCpuId_EDX(1) & X86_CPUID_FEATURE_EDX_SEP))
1175 {
1176 AssertMsgFailed(("ERROR: Can't turn on SEP when the host doesn't support it!!\n"));
1177 return;
1178 }
1179
1180 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1181 pVM->cpum.s.aGuestCpuIdStd[1].edx |= X86_CPUID_FEATURE_EDX_SEP;
1182 LogRel(("CPUMSetGuestCpuIdFeature: Enabled sysenter/exit\n"));
1183 break;
1184 }
1185
1186 /*
1187 * Set the syscall/sysret bit in the extended feature mask.
1188 * Assumes the caller knows what it's doing! (host must support these)
1189 */
1190 case CPUMCPUIDFEATURE_SYSCALL:
1191 {
1192 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax < 0x80000001
1193 || !(ASMCpuId_EDX(0x80000001) & X86_CPUID_AMD_FEATURE_EDX_SEP))
1194 {
1195 LogRel(("WARNING: Can't turn on SYSCALL/SYSRET when the host doesn't support it!!\n"));
1196 return;
1197 }
1198 /* Valid for both Intel and AMD CPUs, although only in 64 bits mode for Intel. */
1199 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_SEP;
1200 LogRel(("CPUMSetGuestCpuIdFeature: Enabled syscall/ret\n"));
1201 break;
1202 }
1203
1204 /*
1205 * Set the PAE bit in both feature masks.
1206 * Assumes the caller knows what it's doing! (host must support these)
1207 */
1208 case CPUMCPUIDFEATURE_PAE:
1209 {
1210 if (!(ASMCpuId_EDX(1) & X86_CPUID_FEATURE_EDX_PAE))
1211 {
1212 LogRel(("WARNING: Can't turn on PAE when the host doesn't support it!!\n"));
1213 return;
1214 }
1215
1216 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1217 pVM->cpum.s.aGuestCpuIdStd[1].edx |= X86_CPUID_FEATURE_EDX_PAE;
1218 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1219 && pVM->cpum.s.enmCPUVendor == CPUMCPUVENDOR_AMD)
1220 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_PAE;
1221 LogRel(("CPUMSetGuestCpuIdFeature: Enabled PAE\n"));
1222 break;
1223 }
1224
1225 /*
1226 * Set the LONG MODE bit in the extended feature mask.
1227 * Assumes the caller knows what it's doing! (host must support these)
1228 */
1229 case CPUMCPUIDFEATURE_LONG_MODE:
1230 {
1231 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax < 0x80000001
1232 || !(ASMCpuId_EDX(0x80000001) & X86_CPUID_AMD_FEATURE_EDX_LONG_MODE))
1233 {
1234 LogRel(("WARNING: Can't turn on LONG MODE when the host doesn't support it!!\n"));
1235 return;
1236 }
1237
1238 /* Valid for both Intel and AMD. */
1239 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_LONG_MODE;
1240 LogRel(("CPUMSetGuestCpuIdFeature: Enabled LONG MODE\n"));
1241 break;
1242 }
1243
1244 /*
1245 * Set the NXE bit in the extended feature mask.
1246 * Assumes the caller knows what it's doing! (host must support these)
1247 */
1248 case CPUMCPUIDFEATURE_NXE:
1249 {
1250 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax < 0x80000001
1251 || !(ASMCpuId_EDX(0x80000001) & X86_CPUID_AMD_FEATURE_EDX_NX))
1252 {
1253 LogRel(("WARNING: Can't turn on NXE when the host doesn't support it!!\n"));
1254 return;
1255 }
1256
1257 /* Valid for both Intel and AMD. */
1258 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_NX;
1259 LogRel(("CPUMSetGuestCpuIdFeature: Enabled NXE\n"));
1260 break;
1261 }
1262
1263 case CPUMCPUIDFEATURE_LAHF:
1264 {
1265 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax < 0x80000001
1266 || !(ASMCpuId_ECX(0x80000001) & X86_CPUID_AMD_FEATURE_ECX_LAHF_SAHF))
1267 {
1268 LogRel(("WARNING: Can't turn on LAHF/SAHF when the host doesn't support it!!\n"));
1269 return;
1270 }
1271
1272 pVM->cpum.s.aGuestCpuIdExt[1].ecx |= X86_CPUID_AMD_FEATURE_ECX_LAHF_SAHF;
1273 LogRel(("CPUMSetGuestCpuIdFeature: Enabled LAHF/SAHF\n"));
1274 break;
1275 }
1276
1277 case CPUMCPUIDFEATURE_PAT:
1278 {
1279 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1280 pVM->cpum.s.aGuestCpuIdStd[1].edx |= X86_CPUID_FEATURE_EDX_PAT;
1281 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1282 && pVM->cpum.s.enmCPUVendor == CPUMCPUVENDOR_AMD)
1283 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_PAT;
1284 LogRel(("CPUMClearGuestCpuIdFeature: Enabled PAT\n"));
1285 break;
1286 }
1287
1288 default:
1289 AssertMsgFailed(("enmFeature=%d\n", enmFeature));
1290 break;
1291 }
1292 pVM->cpum.s.fChanged |= CPUM_CHANGED_CPUID;
1293}
1294
1295
1296/**
1297 * Queries a CPUID feature bit.
1298 *
1299 * @returns boolean for feature presence
1300 * @param pVM The VM Handle.
1301 * @param enmFeature The feature to query.
1302 */
1303VMMDECL(bool) CPUMGetGuestCpuIdFeature(PVM pVM, CPUMCPUIDFEATURE enmFeature)
1304{
1305 switch (enmFeature)
1306 {
1307 case CPUMCPUIDFEATURE_PAE:
1308 {
1309 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1310 return !!(pVM->cpum.s.aGuestCpuIdStd[1].edx & X86_CPUID_FEATURE_EDX_PAE);
1311 break;
1312 }
1313
1314 default:
1315 AssertMsgFailed(("enmFeature=%d\n", enmFeature));
1316 break;
1317 }
1318 return false;
1319}
1320
1321
1322/**
1323 * Clears a CPUID feature bit.
1324 *
1325 * @param pVM The VM Handle.
1326 * @param enmFeature The feature to clear.
1327 */
1328VMMDECL(void) CPUMClearGuestCpuIdFeature(PVM pVM, CPUMCPUIDFEATURE enmFeature)
1329{
1330 switch (enmFeature)
1331 {
1332 /*
1333 * Set the APIC bit in both feature masks.
1334 */
1335 case CPUMCPUIDFEATURE_APIC:
1336 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1337 pVM->cpum.s.aGuestCpuIdStd[1].edx &= ~X86_CPUID_FEATURE_EDX_APIC;
1338 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1339 && pVM->cpum.s.enmCPUVendor == CPUMCPUVENDOR_AMD)
1340 pVM->cpum.s.aGuestCpuIdExt[1].edx &= ~X86_CPUID_AMD_FEATURE_EDX_APIC;
1341 Log(("CPUMSetGuestCpuIdFeature: Disabled APIC\n"));
1342 break;
1343
1344 /*
1345 * Clear the x2APIC bit in the standard feature mask.
1346 */
1347 case CPUMCPUIDFEATURE_X2APIC:
1348 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1349 pVM->cpum.s.aGuestCpuIdStd[1].ecx &= ~X86_CPUID_FEATURE_ECX_X2APIC;
1350 LogRel(("CPUMSetGuestCpuIdFeature: Disabled x2APIC\n"));
1351 break;
1352
1353 case CPUMCPUIDFEATURE_PAE:
1354 {
1355 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1356 pVM->cpum.s.aGuestCpuIdStd[1].edx &= ~X86_CPUID_FEATURE_EDX_PAE;
1357 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1358 && pVM->cpum.s.enmCPUVendor == CPUMCPUVENDOR_AMD)
1359 pVM->cpum.s.aGuestCpuIdExt[1].edx &= ~X86_CPUID_AMD_FEATURE_EDX_PAE;
1360 LogRel(("CPUMClearGuestCpuIdFeature: Disabled PAE!\n"));
1361 break;
1362 }
1363
1364 case CPUMCPUIDFEATURE_PAT:
1365 {
1366 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1367 pVM->cpum.s.aGuestCpuIdStd[1].edx &= ~X86_CPUID_FEATURE_EDX_PAT;
1368 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1369 && pVM->cpum.s.enmCPUVendor == CPUMCPUVENDOR_AMD)
1370 pVM->cpum.s.aGuestCpuIdExt[1].edx &= ~X86_CPUID_AMD_FEATURE_EDX_PAT;
1371 LogRel(("CPUMClearGuestCpuIdFeature: Disabled PAT!\n"));
1372 break;
1373 }
1374
1375 default:
1376 AssertMsgFailed(("enmFeature=%d\n", enmFeature));
1377 break;
1378 }
1379 pVM->cpum.s.fChanged |= CPUM_CHANGED_CPUID;
1380}
1381
1382
1383/**
1384 * Gets the CPU vendor
1385 *
1386 * @returns CPU vendor
1387 * @param pVM The VM handle.
1388 */
1389VMMDECL(CPUMCPUVENDOR) CPUMGetCPUVendor(PVM pVM)
1390{
1391 return pVM->cpum.s.enmCPUVendor;
1392}
1393
1394
1395VMMDECL(int) CPUMSetGuestDR0(PVM pVM, uint64_t uDr0)
1396{
1397 pVM->cpum.s.Guest.dr[0] = uDr0;
1398 return CPUMRecalcHyperDRx(pVM);
1399}
1400
1401
1402VMMDECL(int) CPUMSetGuestDR1(PVM pVM, uint64_t uDr1)
1403{
1404 pVM->cpum.s.Guest.dr[1] = uDr1;
1405 return CPUMRecalcHyperDRx(pVM);
1406}
1407
1408
1409VMMDECL(int) CPUMSetGuestDR2(PVM pVM, uint64_t uDr2)
1410{
1411 pVM->cpum.s.Guest.dr[2] = uDr2;
1412 return CPUMRecalcHyperDRx(pVM);
1413}
1414
1415
1416VMMDECL(int) CPUMSetGuestDR3(PVM pVM, uint64_t uDr3)
1417{
1418 pVM->cpum.s.Guest.dr[3] = uDr3;
1419 return CPUMRecalcHyperDRx(pVM);
1420}
1421
1422
1423VMMDECL(int) CPUMSetGuestDR6(PVM pVM, uint64_t uDr6)
1424{
1425 pVM->cpum.s.Guest.dr[6] = uDr6;
1426 return CPUMRecalcHyperDRx(pVM);
1427}
1428
1429
1430VMMDECL(int) CPUMSetGuestDR7(PVM pVM, uint64_t uDr7)
1431{
1432 pVM->cpum.s.Guest.dr[7] = uDr7;
1433 return CPUMRecalcHyperDRx(pVM);
1434}
1435
1436
1437VMMDECL(int) CPUMSetGuestDRx(PVM pVM, uint32_t iReg, uint64_t Value)
1438{
1439 AssertReturn(iReg <= USE_REG_DR7, VERR_INVALID_PARAMETER);
1440 /* DR4 is an alias for DR6, and DR5 is an alias for DR7. */
1441 if (iReg == 4 || iReg == 5)
1442 iReg += 2;
1443 pVM->cpum.s.Guest.dr[iReg] = Value;
1444 return CPUMRecalcHyperDRx(pVM);
1445}
1446
1447
1448/**
1449 * Recalculates the hypvervisor DRx register values based on
1450 * current guest registers and DBGF breakpoints.
1451 *
1452 * This is called whenever a guest DRx register is modified and when DBGF
1453 * sets a hardware breakpoint. In guest context this function will reload
1454 * any (hyper) DRx registers which comes out with a different value.
1455 *
1456 * @returns VINF_SUCCESS.
1457 * @param pVM The VM handle.
1458 */
1459VMMDECL(int) CPUMRecalcHyperDRx(PVM pVM)
1460{
1461 /*
1462 * Compare the DR7s first.
1463 *
1464 * We only care about the enabled flags. The GE and LE flags are always
1465 * set and we don't care if the guest doesn't set them. GD is virtualized
1466 * when we dispatch #DB, we never enable it.
1467 */
1468 const RTGCUINTREG uDbgfDr7 = DBGFBpGetDR7(pVM);
1469#ifdef CPUM_VIRTUALIZE_DRX
1470 const RTGCUINTREG uGstDr7 = CPUMGetGuestDR7(pVM);
1471#else
1472 const RTGCUINTREG uGstDr7 = 0;
1473#endif
1474 if ((uGstDr7 | uDbgfDr7) & X86_DR7_ENABLED_MASK)
1475 {
1476 /*
1477 * Ok, something is enabled. Recalc each of the breakpoints.
1478 * Straight forward code, not optimized/minimized in any way.
1479 */
1480 RTGCUINTREG uNewDr7 = X86_DR7_GE | X86_DR7_LE | X86_DR7_MB1_MASK;
1481
1482 /* bp 0 */
1483 RTGCUINTREG uNewDr0;
1484 if (uDbgfDr7 & (X86_DR7_L0 | X86_DR7_G0))
1485 {
1486 uNewDr7 |= uDbgfDr7 & (X86_DR7_L0 | X86_DR7_G0 | X86_DR7_RW0_MASK | X86_DR7_LEN0_MASK);
1487 uNewDr0 = DBGFBpGetDR0(pVM);
1488 }
1489 else if (uGstDr7 & (X86_DR7_L0 | X86_DR7_G0))
1490 {
1491 uNewDr7 |= uGstDr7 & (X86_DR7_L0 | X86_DR7_G0 | X86_DR7_RW0_MASK | X86_DR7_LEN0_MASK);
1492 uNewDr0 = CPUMGetGuestDR0(pVM);
1493 }
1494 else
1495 uNewDr0 = pVM->cpum.s.Hyper.dr[0];
1496
1497 /* bp 1 */
1498 RTGCUINTREG uNewDr1;
1499 if (uDbgfDr7 & (X86_DR7_L1 | X86_DR7_G1))
1500 {
1501 uNewDr7 |= uDbgfDr7 & (X86_DR7_L1 | X86_DR7_G1 | X86_DR7_RW1_MASK | X86_DR7_LEN1_MASK);
1502 uNewDr1 = DBGFBpGetDR1(pVM);
1503 }
1504 else if (uGstDr7 & (X86_DR7_L1 | X86_DR7_G1))
1505 {
1506 uNewDr7 |= uGstDr7 & (X86_DR7_L1 | X86_DR7_G1 | X86_DR7_RW1_MASK | X86_DR7_LEN1_MASK);
1507 uNewDr1 = CPUMGetGuestDR1(pVM);
1508 }
1509 else
1510 uNewDr1 = pVM->cpum.s.Hyper.dr[1];
1511
1512 /* bp 2 */
1513 RTGCUINTREG uNewDr2;
1514 if (uDbgfDr7 & (X86_DR7_L2 | X86_DR7_G2))
1515 {
1516 uNewDr7 |= uDbgfDr7 & (X86_DR7_L2 | X86_DR7_G2 | X86_DR7_RW2_MASK | X86_DR7_LEN2_MASK);
1517 uNewDr2 = DBGFBpGetDR2(pVM);
1518 }
1519 else if (uGstDr7 & (X86_DR7_L2 | X86_DR7_G2))
1520 {
1521 uNewDr7 |= uGstDr7 & (X86_DR7_L2 | X86_DR7_G2 | X86_DR7_RW2_MASK | X86_DR7_LEN2_MASK);
1522 uNewDr2 = CPUMGetGuestDR2(pVM);
1523 }
1524 else
1525 uNewDr2 = pVM->cpum.s.Hyper.dr[2];
1526
1527 /* bp 3 */
1528 RTGCUINTREG uNewDr3;
1529 if (uDbgfDr7 & (X86_DR7_L3 | X86_DR7_G3))
1530 {
1531 uNewDr7 |= uDbgfDr7 & (X86_DR7_L3 | X86_DR7_G3 | X86_DR7_RW3_MASK | X86_DR7_LEN3_MASK);
1532 uNewDr3 = DBGFBpGetDR3(pVM);
1533 }
1534 else if (uGstDr7 & (X86_DR7_L3 | X86_DR7_G3))
1535 {
1536 uNewDr7 |= uGstDr7 & (X86_DR7_L3 | X86_DR7_G3 | X86_DR7_RW3_MASK | X86_DR7_LEN3_MASK);
1537 uNewDr3 = CPUMGetGuestDR3(pVM);
1538 }
1539 else
1540 uNewDr3 = pVM->cpum.s.Hyper.dr[3];
1541
1542 /*
1543 * Apply the updates.
1544 */
1545#ifdef IN_GC
1546 if (!(pVM->cpum.s.fUseFlags & CPUM_USE_DEBUG_REGS))
1547 {
1548 /** @todo save host DBx registers. */
1549 }
1550#endif
1551 pVM->cpum.s.fUseFlags |= CPUM_USE_DEBUG_REGS;
1552 if (uNewDr3 != pVM->cpum.s.Hyper.dr[3])
1553 CPUMSetHyperDR3(pVM, uNewDr3);
1554 if (uNewDr2 != pVM->cpum.s.Hyper.dr[2])
1555 CPUMSetHyperDR2(pVM, uNewDr2);
1556 if (uNewDr1 != pVM->cpum.s.Hyper.dr[1])
1557 CPUMSetHyperDR1(pVM, uNewDr1);
1558 if (uNewDr0 != pVM->cpum.s.Hyper.dr[0])
1559 CPUMSetHyperDR0(pVM, uNewDr0);
1560 if (uNewDr7 != pVM->cpum.s.Hyper.dr[7])
1561 CPUMSetHyperDR7(pVM, uNewDr7);
1562 }
1563 else
1564 {
1565#ifdef IN_GC
1566 if (pVM->cpum.s.fUseFlags & CPUM_USE_DEBUG_REGS)
1567 {
1568 /** @todo restore host DBx registers. */
1569 }
1570#endif
1571 pVM->cpum.s.fUseFlags &= ~CPUM_USE_DEBUG_REGS;
1572 }
1573 Log2(("CPUMRecalcHyperDRx: fUseFlags=%#x %RGr %RGr %RGr %RGr %RGr %RGr\n",
1574 pVM->cpum.s.fUseFlags, pVM->cpum.s.Hyper.dr[0], pVM->cpum.s.Hyper.dr[1],
1575 pVM->cpum.s.Hyper.dr[2], pVM->cpum.s.Hyper.dr[3], pVM->cpum.s.Hyper.dr[6],
1576 pVM->cpum.s.Hyper.dr[7]));
1577
1578 return VINF_SUCCESS;
1579}
1580
1581#ifndef IN_RING0 /** @todo I don't think we need this in R0, so move it to CPUMAll.cpp? */
1582
1583/**
1584 * Transforms the guest CPU state to raw-ring mode.
1585 *
1586 * This function will change the any of the cs and ss register with DPL=0 to DPL=1.
1587 *
1588 * @returns VBox status. (recompiler failure)
1589 * @param pVM VM handle.
1590 * @param pCtxCore The context core (for trap usage).
1591 * @see @ref pg_raw
1592 */
1593VMMDECL(int) CPUMRawEnter(PVM pVM, PCPUMCTXCORE pCtxCore)
1594{
1595 Assert(!pVM->cpum.s.fRawEntered);
1596 if (!pCtxCore)
1597 pCtxCore = CPUMCTX2CORE(&pVM->cpum.s.Guest);
1598
1599 /*
1600 * Are we in Ring-0?
1601 */
1602 if ( pCtxCore->ss && (pCtxCore->ss & X86_SEL_RPL) == 0
1603 && !pCtxCore->eflags.Bits.u1VM)
1604 {
1605 /*
1606 * Enter execution mode.
1607 */
1608 PATMRawEnter(pVM, pCtxCore);
1609
1610 /*
1611 * Set CPL to Ring-1.
1612 */
1613 pCtxCore->ss |= 1;
1614 if (pCtxCore->cs && (pCtxCore->cs & X86_SEL_RPL) == 0)
1615 pCtxCore->cs |= 1;
1616 }
1617 else
1618 {
1619 AssertMsg((pCtxCore->ss & X86_SEL_RPL) >= 2 || pCtxCore->eflags.Bits.u1VM,
1620 ("ring-1 code not supported\n"));
1621 /*
1622 * PATM takes care of IOPL and IF flags for Ring-3 and Ring-2 code as well.
1623 */
1624 PATMRawEnter(pVM, pCtxCore);
1625 }
1626
1627 /*
1628 * Assert sanity.
1629 */
1630 AssertMsg((pCtxCore->eflags.u32 & X86_EFL_IF), ("X86_EFL_IF is clear\n"));
1631 AssertReleaseMsg( pCtxCore->eflags.Bits.u2IOPL < (unsigned)(pCtxCore->ss & X86_SEL_RPL)
1632 || pCtxCore->eflags.Bits.u1VM,
1633 ("X86_EFL_IOPL=%d CPL=%d\n", pCtxCore->eflags.Bits.u2IOPL, pCtxCore->ss & X86_SEL_RPL));
1634 Assert((pVM->cpum.s.Guest.cr0 & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE)) == (X86_CR0_PG | X86_CR0_PE | X86_CR0_WP));
1635 pCtxCore->eflags.u32 |= X86_EFL_IF; /* paranoia */
1636
1637 pVM->cpum.s.fRawEntered = true;
1638 return VINF_SUCCESS;
1639}
1640
1641
1642/**
1643 * Transforms the guest CPU state from raw-ring mode to correct values.
1644 *
1645 * This function will change any selector registers with DPL=1 to DPL=0.
1646 *
1647 * @returns Adjusted rc.
1648 * @param pVM VM handle.
1649 * @param rc Raw mode return code
1650 * @param pCtxCore The context core (for trap usage).
1651 * @see @ref pg_raw
1652 */
1653VMMDECL(int) CPUMRawLeave(PVM pVM, PCPUMCTXCORE pCtxCore, int rc)
1654{
1655 /*
1656 * Don't leave if we've already left (in GC).
1657 */
1658 Assert(pVM->cpum.s.fRawEntered);
1659 if (!pVM->cpum.s.fRawEntered)
1660 return rc;
1661 pVM->cpum.s.fRawEntered = false;
1662
1663 PCPUMCTX pCtx = &pVM->cpum.s.Guest;
1664 if (!pCtxCore)
1665 pCtxCore = CPUMCTX2CORE(pCtx);
1666 Assert(pCtxCore->eflags.Bits.u1VM || (pCtxCore->ss & X86_SEL_RPL));
1667 AssertMsg(pCtxCore->eflags.Bits.u1VM || pCtxCore->eflags.Bits.u2IOPL < (unsigned)(pCtxCore->ss & X86_SEL_RPL),
1668 ("X86_EFL_IOPL=%d CPL=%d\n", pCtxCore->eflags.Bits.u2IOPL, pCtxCore->ss & X86_SEL_RPL));
1669
1670 /*
1671 * Are we executing in raw ring-1?
1672 */
1673 if ( (pCtxCore->ss & X86_SEL_RPL) == 1
1674 && !pCtxCore->eflags.Bits.u1VM)
1675 {
1676 /*
1677 * Leave execution mode.
1678 */
1679 PATMRawLeave(pVM, pCtxCore, rc);
1680 /* Not quite sure if this is really required, but shouldn't harm (too much anyways). */
1681 /** @todo See what happens if we remove this. */
1682 if ((pCtxCore->ds & X86_SEL_RPL) == 1)
1683 pCtxCore->ds &= ~X86_SEL_RPL;
1684 if ((pCtxCore->es & X86_SEL_RPL) == 1)
1685 pCtxCore->es &= ~X86_SEL_RPL;
1686 if ((pCtxCore->fs & X86_SEL_RPL) == 1)
1687 pCtxCore->fs &= ~X86_SEL_RPL;
1688 if ((pCtxCore->gs & X86_SEL_RPL) == 1)
1689 pCtxCore->gs &= ~X86_SEL_RPL;
1690
1691 /*
1692 * Ring-1 selector => Ring-0.
1693 */
1694 pCtxCore->ss &= ~X86_SEL_RPL;
1695 if ((pCtxCore->cs & X86_SEL_RPL) == 1)
1696 pCtxCore->cs &= ~X86_SEL_RPL;
1697 }
1698 else
1699 {
1700 /*
1701 * PATM is taking care of the IOPL and IF flags for us.
1702 */
1703 PATMRawLeave(pVM, pCtxCore, rc);
1704 if (!pCtxCore->eflags.Bits.u1VM)
1705 {
1706 /** @todo See what happens if we remove this. */
1707 if ((pCtxCore->ds & X86_SEL_RPL) == 1)
1708 pCtxCore->ds &= ~X86_SEL_RPL;
1709 if ((pCtxCore->es & X86_SEL_RPL) == 1)
1710 pCtxCore->es &= ~X86_SEL_RPL;
1711 if ((pCtxCore->fs & X86_SEL_RPL) == 1)
1712 pCtxCore->fs &= ~X86_SEL_RPL;
1713 if ((pCtxCore->gs & X86_SEL_RPL) == 1)
1714 pCtxCore->gs &= ~X86_SEL_RPL;
1715 }
1716 }
1717
1718 return rc;
1719}
1720
1721/**
1722 * Updates the EFLAGS while we're in raw-mode.
1723 *
1724 * @param pVM The VM handle.
1725 * @param pCtxCore The context core.
1726 * @param eflags The new EFLAGS value.
1727 */
1728VMMDECL(void) CPUMRawSetEFlags(PVM pVM, PCPUMCTXCORE pCtxCore, uint32_t eflags)
1729{
1730 if (!pVM->cpum.s.fRawEntered)
1731 {
1732 pCtxCore->eflags.u32 = eflags;
1733 return;
1734 }
1735 PATMRawSetEFlags(pVM, pCtxCore, eflags);
1736}
1737
1738#endif /* !IN_RING0 */
1739
1740/**
1741 * Gets the EFLAGS while we're in raw-mode.
1742 *
1743 * @returns The eflags.
1744 * @param pVM The VM handle.
1745 * @param pCtxCore The context core.
1746 */
1747VMMDECL(uint32_t) CPUMRawGetEFlags(PVM pVM, PCPUMCTXCORE pCtxCore)
1748{
1749#ifdef IN_RING0
1750 return pCtxCore->eflags.u32;
1751#else
1752 if (!pVM->cpum.s.fRawEntered)
1753 return pCtxCore->eflags.u32;
1754 return PATMRawGetEFlags(pVM, pCtxCore);
1755#endif
1756}
1757
1758
1759/**
1760 * Gets and resets the changed flags (CPUM_CHANGED_*).
1761 * Only REM should call this function.
1762 *
1763 * @returns The changed flags.
1764 * @param pVM The VM handle.
1765 */
1766VMMDECL(unsigned) CPUMGetAndClearChangedFlagsREM(PVM pVM)
1767{
1768 unsigned fFlags = pVM->cpum.s.fChanged;
1769 pVM->cpum.s.fChanged = 0;
1770 /** @todo change the switcher to use the fChanged flags. */
1771 if (pVM->cpum.s.fUseFlags & CPUM_USED_FPU_SINCE_REM)
1772 {
1773 fFlags |= CPUM_CHANGED_FPU_REM;
1774 pVM->cpum.s.fUseFlags &= ~CPUM_USED_FPU_SINCE_REM;
1775 }
1776 return fFlags;
1777}
1778
1779
1780/**
1781 * Sets the specified changed flags (CPUM_CHANGED_*).
1782 *
1783 * @param pVM The VM handle.
1784 */
1785VMMDECL(void) CPUMSetChangedFlags(PVM pVM, uint32_t fChangedFlags)
1786{
1787 pVM->cpum.s.fChanged |= fChangedFlags;
1788}
1789
1790
1791/**
1792 * Checks if the CPU supports the FXSAVE and FXRSTOR instruction.
1793 * @returns true if supported.
1794 * @returns false if not supported.
1795 * @param pVM The VM handle.
1796 */
1797VMMDECL(bool) CPUMSupportsFXSR(PVM pVM)
1798{
1799 return pVM->cpum.s.CPUFeatures.edx.u1FXSR != 0;
1800}
1801
1802
1803/**
1804 * Checks if the host OS uses the SYSENTER / SYSEXIT instructions.
1805 * @returns true if used.
1806 * @returns false if not used.
1807 * @param pVM The VM handle.
1808 */
1809VMMDECL(bool) CPUMIsHostUsingSysEnter(PVM pVM)
1810{
1811 return (pVM->cpum.s.fUseFlags & CPUM_USE_SYSENTER) != 0;
1812}
1813
1814
1815/**
1816 * Checks if the host OS uses the SYSCALL / SYSRET instructions.
1817 * @returns true if used.
1818 * @returns false if not used.
1819 * @param pVM The VM handle.
1820 */
1821VMMDECL(bool) CPUMIsHostUsingSysCall(PVM pVM)
1822{
1823 return (pVM->cpum.s.fUseFlags & CPUM_USE_SYSCALL) != 0;
1824}
1825
1826#ifndef IN_RING3
1827
1828/**
1829 * Lazily sync in the FPU/XMM state
1830 *
1831 * @returns VBox status code.
1832 * @param pVM VM handle.
1833 */
1834VMMDECL(int) CPUMHandleLazyFPU(PVM pVM)
1835{
1836 return CPUMHandleLazyFPUAsm(&pVM->cpum.s);
1837}
1838
1839
1840/**
1841 * Restore host FPU/XMM state
1842 *
1843 * @returns VBox status code.
1844 * @param pVM VM handle.
1845 */
1846VMMDECL(int) CPUMRestoreHostFPUState(PVM pVM)
1847{
1848 Assert(pVM->cpum.s.CPUFeatures.edx.u1FXSR);
1849 return CPUMRestoreHostFPUStateAsm(&pVM->cpum.s);
1850}
1851
1852#endif /* !IN_RING3 */
1853
1854/**
1855 * Checks if we activated the FPU/XMM state of the guest OS
1856 * @returns true if we did.
1857 * @returns false if not.
1858 * @param pVM The VM handle.
1859 */
1860VMMDECL(bool) CPUMIsGuestFPUStateActive(PVM pVM)
1861{
1862 return (pVM->cpum.s.fUseFlags & CPUM_USED_FPU) != 0;
1863}
1864
1865
1866/**
1867 * Deactivate the FPU/XMM state of the guest OS
1868 * @param pVM The VM handle.
1869 */
1870VMMDECL(void) CPUMDeactivateGuestFPUState(PVM pVM)
1871{
1872 pVM->cpum.s.fUseFlags &= ~CPUM_USED_FPU;
1873}
1874
1875
1876/**
1877 * Checks if the guest debug state is active
1878 *
1879 * @returns boolean
1880 * @param pVM VM handle.
1881 */
1882VMMDECL(bool) CPUMIsGuestDebugStateActive(PVM pVM)
1883{
1884 return (pVM->cpum.s.fUseFlags & CPUM_USE_DEBUG_REGS) != 0;
1885}
1886
1887
1888/**
1889 * Mark the guest's debug state as inactive
1890 *
1891 * @returns boolean
1892 * @param pVM VM handle.
1893 */
1894VMMDECL(void) CPUMDeactivateGuestDebugState(PVM pVM)
1895{
1896 pVM->cpum.s.fUseFlags &= ~CPUM_USE_DEBUG_REGS;
1897}
1898
1899
1900/**
1901 * Checks if the hidden selector registers are valid
1902 * @returns true if they are.
1903 * @returns false if not.
1904 * @param pVM The VM handle.
1905 */
1906VMMDECL(bool) CPUMAreHiddenSelRegsValid(PVM pVM)
1907{
1908 return !!pVM->cpum.s.fValidHiddenSelRegs; /** @todo change fValidHiddenSelRegs to bool! */
1909}
1910
1911
1912/**
1913 * Checks if the hidden selector registers are valid
1914 * @param pVM The VM handle.
1915 * @param fValid Valid or not
1916 */
1917VMMDECL(void) CPUMSetHiddenSelRegsValid(PVM pVM, bool fValid)
1918{
1919 pVM->cpum.s.fValidHiddenSelRegs = fValid;
1920}
1921
1922
1923/**
1924 * Get the current privilege level of the guest.
1925 *
1926 * @returns cpl
1927 * @param pVM VM Handle.
1928 * @param pRegFrame Trap register frame.
1929 */
1930VMMDECL(uint32_t) CPUMGetGuestCPL(PVM pVM, PCPUMCTXCORE pCtxCore)
1931{
1932 uint32_t cpl;
1933
1934 if (CPUMAreHiddenSelRegsValid(pVM))
1935 {
1936 /*
1937 * The hidden CS.DPL register is always equal to the CPL, it is
1938 * not affected by loading a conforming coding segment.
1939 *
1940 * This only seems to apply to AMD-V; in the VT-x case we *do* need to look
1941 * at SS. (ACP2 regression during install after a far call to ring 2)
1942 */
1943 if (RT_LIKELY(pVM->cpum.s.Guest.cr0 & X86_CR0_PE))
1944 cpl = pCtxCore->ssHid.Attr.n.u2Dpl;
1945 else
1946 cpl = 0; /* CPL set to 3 for VT-x real-mode emulation. */
1947 }
1948 else if (RT_LIKELY(pVM->cpum.s.Guest.cr0 & X86_CR0_PE))
1949 {
1950 if (RT_LIKELY(!pCtxCore->eflags.Bits.u1VM))
1951 {
1952 /*
1953 * The SS RPL is always equal to the CPL, while the CS RPL
1954 * isn't necessarily equal if the segment is conforming.
1955 * See section 4.11.1 in the AMD manual.
1956 */
1957 cpl = (pCtxCore->ss & X86_SEL_RPL);
1958#ifndef IN_RING0
1959 if (cpl == 1)
1960 cpl = 0;
1961#endif
1962 }
1963 else
1964 cpl = 3;
1965 }
1966 else
1967 cpl = 0; /* real mode; cpl is zero */
1968
1969 return cpl;
1970}
1971
1972
1973/**
1974 * Gets the current guest CPU mode.
1975 *
1976 * If paging mode is what you need, check out PGMGetGuestMode().
1977 *
1978 * @returns The CPU mode.
1979 * @param pVM The VM handle.
1980 */
1981VMMDECL(CPUMMODE) CPUMGetGuestMode(PVM pVM)
1982{
1983 CPUMMODE enmMode;
1984 if (!(pVM->cpum.s.Guest.cr0 & X86_CR0_PE))
1985 enmMode = CPUMMODE_REAL;
1986 else if (!(pVM->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA))
1987 enmMode = CPUMMODE_PROTECTED;
1988 else
1989 enmMode = CPUMMODE_LONG;
1990
1991 return enmMode;
1992}
1993
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