VirtualBox

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

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

CPUMGetGuestCPL fix for real mode in VT-x.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 50.0 KB
Line 
1/* $Id: CPUMAllRegs.cpp 12735 2008-09-25 14:07:53Z 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 */
57CPUMDECL(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 */
82CPUMDECL(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 */
100CPUMDECL(int) CPUMQueryHyperCtxPtr(PVM pVM, PCPUMCTX *ppCtx)
101{
102 *ppCtx = &pVM->cpum.s.Hyper;
103 return VINF_SUCCESS;
104}
105
106
107CPUMDECL(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
115CPUMDECL(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
123CPUMDECL(void) CPUMSetHyperCR3(PVM pVM, uint32_t cr3)
124{
125 pVM->cpum.s.Hyper.cr3 = cr3;
126}
127
128
129CPUMDECL(void) CPUMSetHyperCS(PVM pVM, RTSEL SelCS)
130{
131 pVM->cpum.s.CTX_SUFF(pHyperCore)->cs = SelCS;
132}
133
134
135CPUMDECL(void) CPUMSetHyperDS(PVM pVM, RTSEL SelDS)
136{
137 pVM->cpum.s.CTX_SUFF(pHyperCore)->ds = SelDS;
138}
139
140
141CPUMDECL(void) CPUMSetHyperES(PVM pVM, RTSEL SelES)
142{
143 pVM->cpum.s.CTX_SUFF(pHyperCore)->es = SelES;
144}
145
146
147CPUMDECL(void) CPUMSetHyperFS(PVM pVM, RTSEL SelFS)
148{
149 pVM->cpum.s.CTX_SUFF(pHyperCore)->fs = SelFS;
150}
151
152
153CPUMDECL(void) CPUMSetHyperGS(PVM pVM, RTSEL SelGS)
154{
155 pVM->cpum.s.CTX_SUFF(pHyperCore)->gs = SelGS;
156}
157
158
159CPUMDECL(void) CPUMSetHyperSS(PVM pVM, RTSEL SelSS)
160{
161 pVM->cpum.s.CTX_SUFF(pHyperCore)->ss = SelSS;
162}
163
164
165CPUMDECL(void) CPUMSetHyperESP(PVM pVM, uint32_t u32ESP)
166{
167 pVM->cpum.s.CTX_SUFF(pHyperCore)->esp = u32ESP;
168}
169
170
171CPUMDECL(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
178CPUMDECL(void) CPUMSetHyperEIP(PVM pVM, uint32_t u32EIP)
179{
180 pVM->cpum.s.CTX_SUFF(pHyperCore)->eip = u32EIP;
181}
182
183
184CPUMDECL(void) CPUMSetHyperTR(PVM pVM, RTSEL SelTR)
185{
186 pVM->cpum.s.Hyper.tr = SelTR;
187}
188
189
190CPUMDECL(void) CPUMSetHyperLDTR(PVM pVM, RTSEL SelLDTR)
191{
192 pVM->cpum.s.Hyper.ldtr = SelLDTR;
193}
194
195
196CPUMDECL(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
203CPUMDECL(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
210CPUMDECL(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
217CPUMDECL(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
224CPUMDECL(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
231CPUMDECL(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
238CPUMDECL(RTSEL) CPUMGetHyperCS(PVM pVM)
239{
240 return pVM->cpum.s.CTX_SUFF(pHyperCore)->cs;
241}
242
243
244CPUMDECL(RTSEL) CPUMGetHyperDS(PVM pVM)
245{
246 return pVM->cpum.s.CTX_SUFF(pHyperCore)->ds;
247}
248
249
250CPUMDECL(RTSEL) CPUMGetHyperES(PVM pVM)
251{
252 return pVM->cpum.s.CTX_SUFF(pHyperCore)->es;
253}
254
255
256CPUMDECL(RTSEL) CPUMGetHyperFS(PVM pVM)
257{
258 return pVM->cpum.s.CTX_SUFF(pHyperCore)->fs;
259}
260
261
262CPUMDECL(RTSEL) CPUMGetHyperGS(PVM pVM)
263{
264 return pVM->cpum.s.CTX_SUFF(pHyperCore)->gs;
265}
266
267
268CPUMDECL(RTSEL) CPUMGetHyperSS(PVM pVM)
269{
270 return pVM->cpum.s.CTX_SUFF(pHyperCore)->ss;
271}
272
273
274CPUMDECL(uint32_t) CPUMGetHyperEAX(PVM pVM)
275{
276 return pVM->cpum.s.CTX_SUFF(pHyperCore)->eax;
277}
278
279
280CPUMDECL(uint32_t) CPUMGetHyperEBX(PVM pVM)
281{
282 return pVM->cpum.s.CTX_SUFF(pHyperCore)->ebx;
283}
284
285
286CPUMDECL(uint32_t) CPUMGetHyperECX(PVM pVM)
287{
288 return pVM->cpum.s.CTX_SUFF(pHyperCore)->ecx;
289}
290
291
292CPUMDECL(uint32_t) CPUMGetHyperEDX(PVM pVM)
293{
294 return pVM->cpum.s.CTX_SUFF(pHyperCore)->edx;
295}
296
297
298CPUMDECL(uint32_t) CPUMGetHyperESI(PVM pVM)
299{
300 return pVM->cpum.s.CTX_SUFF(pHyperCore)->esi;
301}
302
303
304CPUMDECL(uint32_t) CPUMGetHyperEDI(PVM pVM)
305{
306 return pVM->cpum.s.CTX_SUFF(pHyperCore)->edi;
307}
308
309
310CPUMDECL(uint32_t) CPUMGetHyperEBP(PVM pVM)
311{
312 return pVM->cpum.s.CTX_SUFF(pHyperCore)->ebp;
313}
314
315
316CPUMDECL(uint32_t) CPUMGetHyperESP(PVM pVM)
317{
318 return pVM->cpum.s.CTX_SUFF(pHyperCore)->esp;
319}
320
321
322CPUMDECL(uint32_t) CPUMGetHyperEFlags(PVM pVM)
323{
324 return pVM->cpum.s.CTX_SUFF(pHyperCore)->eflags.u32;
325}
326
327
328CPUMDECL(uint32_t) CPUMGetHyperEIP(PVM pVM)
329{
330 return pVM->cpum.s.CTX_SUFF(pHyperCore)->eip;
331}
332
333
334CPUMDECL(uint64_t) CPUMGetHyperRIP(PVM pVM)
335{
336 return pVM->cpum.s.CTX_SUFF(pHyperCore)->rip;
337}
338
339
340CPUMDECL(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
348CPUMDECL(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
356CPUMDECL(RTSEL) CPUMGetHyperLDTR(PVM pVM)
357{
358 return pVM->cpum.s.Hyper.ldtr;
359}
360
361
362CPUMDECL(RTGCUINTREG) CPUMGetHyperDR0(PVM pVM)
363{
364 return pVM->cpum.s.Hyper.dr[0];
365}
366
367
368CPUMDECL(RTGCUINTREG) CPUMGetHyperDR1(PVM pVM)
369{
370 return pVM->cpum.s.Hyper.dr[1];
371}
372
373
374CPUMDECL(RTGCUINTREG) CPUMGetHyperDR2(PVM pVM)
375{
376 return pVM->cpum.s.Hyper.dr[2];
377}
378
379
380CPUMDECL(RTGCUINTREG) CPUMGetHyperDR3(PVM pVM)
381{
382 return pVM->cpum.s.Hyper.dr[3];
383}
384
385
386CPUMDECL(RTGCUINTREG) CPUMGetHyperDR6(PVM pVM)
387{
388 return pVM->cpum.s.Hyper.dr[6];
389}
390
391
392CPUMDECL(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 */
404CPUMDECL(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 */
416CPUMDECL(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 */
458CPUMDECL(int) CPUMQueryGuestCtxPtr(PVM pVM, PCPUMCTX *ppCtx)
459{
460 *ppCtx = &pVM->cpum.s.Guest;
461 return VINF_SUCCESS;
462}
463
464
465CPUMDECL(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
473CPUMDECL(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
481CPUMDECL(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
488CPUMDECL(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 */
507CPUMDECL(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
574CPUMDECL(int) CPUMSetGuestCR2(PVM pVM, uint64_t cr2)
575{
576 pVM->cpum.s.Guest.cr2 = cr2;
577 return VINF_SUCCESS;
578}
579
580
581CPUMDECL(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
589CPUMDECL(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
602CPUMDECL(int) CPUMSetGuestEFlags(PVM pVM, uint32_t eflags)
603{
604 pVM->cpum.s.Guest.eflags.u32 = eflags;
605 return VINF_SUCCESS;
606}
607
608
609CPUMDECL(int) CPUMSetGuestEIP(PVM pVM, uint32_t eip)
610{
611 pVM->cpum.s.Guest.eip = eip;
612 return VINF_SUCCESS;
613}
614
615
616CPUMDECL(int) CPUMSetGuestEAX(PVM pVM, uint32_t eax)
617{
618 pVM->cpum.s.Guest.eax = eax;
619 return VINF_SUCCESS;
620}
621
622
623CPUMDECL(int) CPUMSetGuestEBX(PVM pVM, uint32_t ebx)
624{
625 pVM->cpum.s.Guest.ebx = ebx;
626 return VINF_SUCCESS;
627}
628
629
630CPUMDECL(int) CPUMSetGuestECX(PVM pVM, uint32_t ecx)
631{
632 pVM->cpum.s.Guest.ecx = ecx;
633 return VINF_SUCCESS;
634}
635
636
637CPUMDECL(int) CPUMSetGuestEDX(PVM pVM, uint32_t edx)
638{
639 pVM->cpum.s.Guest.edx = edx;
640 return VINF_SUCCESS;
641}
642
643
644CPUMDECL(int) CPUMSetGuestESP(PVM pVM, uint32_t esp)
645{
646 pVM->cpum.s.Guest.esp = esp;
647 return VINF_SUCCESS;
648}
649
650
651CPUMDECL(int) CPUMSetGuestEBP(PVM pVM, uint32_t ebp)
652{
653 pVM->cpum.s.Guest.ebp = ebp;
654 return VINF_SUCCESS;
655}
656
657
658CPUMDECL(int) CPUMSetGuestESI(PVM pVM, uint32_t esi)
659{
660 pVM->cpum.s.Guest.esi = esi;
661 return VINF_SUCCESS;
662}
663
664
665CPUMDECL(int) CPUMSetGuestEDI(PVM pVM, uint32_t edi)
666{
667 pVM->cpum.s.Guest.edi = edi;
668 return VINF_SUCCESS;
669}
670
671
672CPUMDECL(int) CPUMSetGuestSS(PVM pVM, uint16_t ss)
673{
674 pVM->cpum.s.Guest.ss = ss;
675 return VINF_SUCCESS;
676}
677
678
679CPUMDECL(int) CPUMSetGuestCS(PVM pVM, uint16_t cs)
680{
681 pVM->cpum.s.Guest.cs = cs;
682 return VINF_SUCCESS;
683}
684
685
686CPUMDECL(int) CPUMSetGuestDS(PVM pVM, uint16_t ds)
687{
688 pVM->cpum.s.Guest.ds = ds;
689 return VINF_SUCCESS;
690}
691
692
693CPUMDECL(int) CPUMSetGuestES(PVM pVM, uint16_t es)
694{
695 pVM->cpum.s.Guest.es = es;
696 return VINF_SUCCESS;
697}
698
699
700CPUMDECL(int) CPUMSetGuestFS(PVM pVM, uint16_t fs)
701{
702 pVM->cpum.s.Guest.fs = fs;
703 return VINF_SUCCESS;
704}
705
706
707CPUMDECL(int) CPUMSetGuestGS(PVM pVM, uint16_t gs)
708{
709 pVM->cpum.s.Guest.gs = gs;
710 return VINF_SUCCESS;
711}
712
713
714CPUMDECL(void) CPUMSetGuestEFER(PVM pVM, uint64_t val)
715{
716 pVM->cpum.s.Guest.msrEFER = val;
717}
718
719
720CPUMDECL(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
775CPUMDECL(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
783CPUMDECL(RTSEL) CPUMGetGuestTR(PVM pVM)
784{
785 return pVM->cpum.s.Guest.tr;
786}
787
788
789CPUMDECL(RTSEL) CPUMGetGuestCS(PVM pVM)
790{
791 return pVM->cpum.s.Guest.cs;
792}
793
794
795CPUMDECL(RTSEL) CPUMGetGuestDS(PVM pVM)
796{
797 return pVM->cpum.s.Guest.ds;
798}
799
800
801CPUMDECL(RTSEL) CPUMGetGuestES(PVM pVM)
802{
803 return pVM->cpum.s.Guest.es;
804}
805
806
807CPUMDECL(RTSEL) CPUMGetGuestFS(PVM pVM)
808{
809 return pVM->cpum.s.Guest.fs;
810}
811
812
813CPUMDECL(RTSEL) CPUMGetGuestGS(PVM pVM)
814{
815 return pVM->cpum.s.Guest.gs;
816}
817
818
819CPUMDECL(RTSEL) CPUMGetGuestSS(PVM pVM)
820{
821 return pVM->cpum.s.Guest.ss;
822}
823
824
825CPUMDECL(RTSEL) CPUMGetGuestLDTR(PVM pVM)
826{
827 return pVM->cpum.s.Guest.ldtr;
828}
829
830
831CPUMDECL(uint64_t) CPUMGetGuestCR0(PVM pVM)
832{
833 return pVM->cpum.s.Guest.cr0;
834}
835
836
837CPUMDECL(uint64_t) CPUMGetGuestCR2(PVM pVM)
838{
839 return pVM->cpum.s.Guest.cr2;
840}
841
842
843CPUMDECL(uint64_t) CPUMGetGuestCR3(PVM pVM)
844{
845 return pVM->cpum.s.Guest.cr3;
846}
847
848
849CPUMDECL(uint64_t) CPUMGetGuestCR4(PVM pVM)
850{
851 return pVM->cpum.s.Guest.cr4;
852}
853
854
855CPUMDECL(void) CPUMGetGuestGDTR(PVM pVM, PVBOXGDTR pGDTR)
856{
857 *pGDTR = pVM->cpum.s.Guest.gdtr;
858}
859
860
861CPUMDECL(uint32_t) CPUMGetGuestEIP(PVM pVM)
862{
863 return pVM->cpum.s.Guest.eip;
864}
865
866
867CPUMDECL(uint64_t) CPUMGetGuestRIP(PVM pVM)
868{
869 return pVM->cpum.s.Guest.rip;
870}
871
872
873CPUMDECL(uint32_t) CPUMGetGuestEAX(PVM pVM)
874{
875 return pVM->cpum.s.Guest.eax;
876}
877
878
879CPUMDECL(uint32_t) CPUMGetGuestEBX(PVM pVM)
880{
881 return pVM->cpum.s.Guest.ebx;
882}
883
884
885CPUMDECL(uint32_t) CPUMGetGuestECX(PVM pVM)
886{
887 return pVM->cpum.s.Guest.ecx;
888}
889
890
891CPUMDECL(uint32_t) CPUMGetGuestEDX(PVM pVM)
892{
893 return pVM->cpum.s.Guest.edx;
894}
895
896
897CPUMDECL(uint32_t) CPUMGetGuestESI(PVM pVM)
898{
899 return pVM->cpum.s.Guest.esi;
900}
901
902
903CPUMDECL(uint32_t) CPUMGetGuestEDI(PVM pVM)
904{
905 return pVM->cpum.s.Guest.edi;
906}
907
908
909CPUMDECL(uint32_t) CPUMGetGuestESP(PVM pVM)
910{
911 return pVM->cpum.s.Guest.esp;
912}
913
914
915CPUMDECL(uint32_t) CPUMGetGuestEBP(PVM pVM)
916{
917 return pVM->cpum.s.Guest.ebp;
918}
919
920
921CPUMDECL(uint32_t) CPUMGetGuestEFlags(PVM pVM)
922{
923 return pVM->cpum.s.Guest.eflags.u32;
924}
925
926
927CPUMDECL(CPUMSELREGHID *) CPUMGetGuestTRHid(PVM pVM)
928{
929 return &pVM->cpum.s.Guest.trHid;
930}
931
932
933///@todo: crx should be an array
934CPUMDECL(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
957CPUMDECL(uint64_t) CPUMGetGuestDR0(PVM pVM)
958{
959 return pVM->cpum.s.Guest.dr[0];
960}
961
962
963CPUMDECL(uint64_t) CPUMGetGuestDR1(PVM pVM)
964{
965 return pVM->cpum.s.Guest.dr[1];
966}
967
968
969CPUMDECL(uint64_t) CPUMGetGuestDR2(PVM pVM)
970{
971 return pVM->cpum.s.Guest.dr[2];
972}
973
974
975CPUMDECL(uint64_t) CPUMGetGuestDR3(PVM pVM)
976{
977 return pVM->cpum.s.Guest.dr[3];
978}
979
980
981CPUMDECL(uint64_t) CPUMGetGuestDR6(PVM pVM)
982{
983 return pVM->cpum.s.Guest.dr[6];
984}
985
986
987CPUMDECL(uint64_t) CPUMGetGuestDR7(PVM pVM)
988{
989 return pVM->cpum.s.Guest.dr[7];
990}
991
992
993CPUMDECL(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
1004CPUMDECL(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 */
1020CPUMDECL(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 */
1049CPUMDECL(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 */
1064CPUMDECL(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 */
1079CPUMDECL(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 */
1092CPUMDECL(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 */
1105CPUMDECL(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 */
1118CPUMDECL(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 */
1131CPUMDECL(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 */
1143CPUMDECL(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 sysenter/sysexit bit in the standard feature mask.
1161 * Assumes the caller knows what it's doing! (host must support these)
1162 */
1163 case CPUMCPUIDFEATURE_SEP:
1164 {
1165 if (!(ASMCpuId_EDX(1) & X86_CPUID_FEATURE_EDX_SEP))
1166 {
1167 AssertMsgFailed(("ERROR: Can't turn on SEP when the host doesn't support it!!\n"));
1168 return;
1169 }
1170
1171 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1172 pVM->cpum.s.aGuestCpuIdStd[1].edx |= X86_CPUID_FEATURE_EDX_SEP;
1173 LogRel(("CPUMSetGuestCpuIdFeature: Enabled sysenter/exit\n"));
1174 break;
1175 }
1176
1177 /*
1178 * Set the syscall/sysret bit in the extended feature mask.
1179 * Assumes the caller knows what it's doing! (host must support these)
1180 */
1181 case CPUMCPUIDFEATURE_SYSCALL:
1182 {
1183 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax < 0x80000001
1184 || !(ASMCpuId_EDX(0x80000001) & X86_CPUID_AMD_FEATURE_EDX_SEP))
1185 {
1186 LogRel(("WARNING: Can't turn on SYSCALL/SYSRET when the host doesn't support it!!\n"));
1187 return;
1188 }
1189 /* Valid for both Intel and AMD CPUs, although only in 64 bits mode for Intel. */
1190 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_SEP;
1191 LogRel(("CPUMSetGuestCpuIdFeature: Enabled syscall/ret\n"));
1192 break;
1193 }
1194
1195 /*
1196 * Set the PAE bit in both feature masks.
1197 * Assumes the caller knows what it's doing! (host must support these)
1198 */
1199 case CPUMCPUIDFEATURE_PAE:
1200 {
1201 if (!(ASMCpuId_EDX(1) & X86_CPUID_FEATURE_EDX_PAE))
1202 {
1203 LogRel(("WARNING: Can't turn on PAE when the host doesn't support it!!\n"));
1204 return;
1205 }
1206
1207 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1208 pVM->cpum.s.aGuestCpuIdStd[1].edx |= X86_CPUID_FEATURE_EDX_PAE;
1209 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1210 && pVM->cpum.s.enmCPUVendor == CPUMCPUVENDOR_AMD)
1211 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_PAE;
1212 LogRel(("CPUMSetGuestCpuIdFeature: Enabled PAE\n"));
1213 break;
1214 }
1215
1216 /*
1217 * Set the LONG MODE bit in the extended feature mask.
1218 * Assumes the caller knows what it's doing! (host must support these)
1219 */
1220 case CPUMCPUIDFEATURE_LONG_MODE:
1221 {
1222 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax < 0x80000001
1223 || !(ASMCpuId_EDX(0x80000001) & X86_CPUID_AMD_FEATURE_EDX_LONG_MODE))
1224 {
1225 LogRel(("WARNING: Can't turn on LONG MODE when the host doesn't support it!!\n"));
1226 return;
1227 }
1228
1229 /* Valid for both Intel and AMD. */
1230 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_LONG_MODE;
1231 LogRel(("CPUMSetGuestCpuIdFeature: Enabled LONG MODE\n"));
1232 break;
1233 }
1234
1235 /*
1236 * Set the NXE bit in the extended feature mask.
1237 * Assumes the caller knows what it's doing! (host must support these)
1238 */
1239 case CPUMCPUIDFEATURE_NXE:
1240 {
1241 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax < 0x80000001
1242 || !(ASMCpuId_EDX(0x80000001) & X86_CPUID_AMD_FEATURE_EDX_NX))
1243 {
1244 LogRel(("WARNING: Can't turn on NXE when the host doesn't support it!!\n"));
1245 return;
1246 }
1247
1248 /* Valid for both Intel and AMD. */
1249 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_NX;
1250 LogRel(("CPUMSetGuestCpuIdFeature: Enabled NXE\n"));
1251 break;
1252 }
1253
1254 case CPUMCPUIDFEATURE_LAHF:
1255 {
1256 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax < 0x80000001
1257 || !(ASMCpuId_ECX(0x80000001) & X86_CPUID_AMD_FEATURE_ECX_LAHF_SAHF))
1258 {
1259 LogRel(("WARNING: Can't turn on LAHF/SAHF when the host doesn't support it!!\n"));
1260 return;
1261 }
1262
1263 pVM->cpum.s.aGuestCpuIdExt[1].ecx |= X86_CPUID_AMD_FEATURE_ECX_LAHF_SAHF;
1264 LogRel(("CPUMSetGuestCpuIdFeature: Enabled LAHF/SAHF\n"));
1265 break;
1266 }
1267
1268 case CPUMCPUIDFEATURE_PAT:
1269 {
1270 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1271 pVM->cpum.s.aGuestCpuIdStd[1].edx |= X86_CPUID_FEATURE_EDX_PAT;
1272 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1273 && pVM->cpum.s.enmCPUVendor == CPUMCPUVENDOR_AMD)
1274 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_PAT;
1275 LogRel(("CPUMClearGuestCpuIdFeature: Enabled PAT\n"));
1276 break;
1277 }
1278
1279 default:
1280 AssertMsgFailed(("enmFeature=%d\n", enmFeature));
1281 break;
1282 }
1283 pVM->cpum.s.fChanged |= CPUM_CHANGED_CPUID;
1284}
1285
1286
1287/**
1288 * Queries a CPUID feature bit.
1289 *
1290 * @returns boolean for feature presence
1291 * @param pVM The VM Handle.
1292 * @param enmFeature The feature to query.
1293 */
1294CPUMDECL(bool) CPUMGetGuestCpuIdFeature(PVM pVM, CPUMCPUIDFEATURE enmFeature)
1295{
1296 switch (enmFeature)
1297 {
1298 case CPUMCPUIDFEATURE_PAE:
1299 {
1300 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1301 return !!(pVM->cpum.s.aGuestCpuIdStd[1].edx & X86_CPUID_FEATURE_EDX_PAE);
1302 break;
1303 }
1304
1305 default:
1306 AssertMsgFailed(("enmFeature=%d\n", enmFeature));
1307 break;
1308 }
1309 return false;
1310}
1311
1312
1313/**
1314 * Clears a CPUID feature bit.
1315 *
1316 * @param pVM The VM Handle.
1317 * @param enmFeature The feature to clear.
1318 */
1319CPUMDECL(void) CPUMClearGuestCpuIdFeature(PVM pVM, CPUMCPUIDFEATURE enmFeature)
1320{
1321 switch (enmFeature)
1322 {
1323 /*
1324 * Set the APIC bit in both feature masks.
1325 */
1326 case CPUMCPUIDFEATURE_APIC:
1327 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1328 pVM->cpum.s.aGuestCpuIdStd[1].edx &= ~X86_CPUID_FEATURE_EDX_APIC;
1329 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1330 && pVM->cpum.s.enmCPUVendor == CPUMCPUVENDOR_AMD)
1331 pVM->cpum.s.aGuestCpuIdExt[1].edx &= ~X86_CPUID_AMD_FEATURE_EDX_APIC;
1332 Log(("CPUMSetGuestCpuIdFeature: Disabled APIC\n"));
1333 break;
1334
1335 case CPUMCPUIDFEATURE_PAE:
1336 {
1337 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1338 pVM->cpum.s.aGuestCpuIdStd[1].edx &= ~X86_CPUID_FEATURE_EDX_PAE;
1339 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1340 && pVM->cpum.s.enmCPUVendor == CPUMCPUVENDOR_AMD)
1341 pVM->cpum.s.aGuestCpuIdExt[1].edx &= ~X86_CPUID_AMD_FEATURE_EDX_PAE;
1342 LogRel(("CPUMClearGuestCpuIdFeature: Disabled PAE!\n"));
1343 break;
1344 }
1345
1346 case CPUMCPUIDFEATURE_PAT:
1347 {
1348 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1349 pVM->cpum.s.aGuestCpuIdStd[1].edx &= ~X86_CPUID_FEATURE_EDX_PAT;
1350 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1351 && pVM->cpum.s.enmCPUVendor == CPUMCPUVENDOR_AMD)
1352 pVM->cpum.s.aGuestCpuIdExt[1].edx &= ~X86_CPUID_AMD_FEATURE_EDX_PAT;
1353 LogRel(("CPUMClearGuestCpuIdFeature: Disabled PAT!\n"));
1354 break;
1355 }
1356
1357 default:
1358 AssertMsgFailed(("enmFeature=%d\n", enmFeature));
1359 break;
1360 }
1361 pVM->cpum.s.fChanged |= CPUM_CHANGED_CPUID;
1362}
1363
1364
1365/**
1366 * Gets the CPU vendor
1367 *
1368 * @returns CPU vendor
1369 * @param pVM The VM handle.
1370 */
1371CPUMDECL(CPUMCPUVENDOR) CPUMGetCPUVendor(PVM pVM)
1372{
1373 return pVM->cpum.s.enmCPUVendor;
1374}
1375
1376
1377CPUMDECL(int) CPUMSetGuestDR0(PVM pVM, uint64_t uDr0)
1378{
1379 pVM->cpum.s.Guest.dr[0] = uDr0;
1380 return CPUMRecalcHyperDRx(pVM);
1381}
1382
1383
1384CPUMDECL(int) CPUMSetGuestDR1(PVM pVM, uint64_t uDr1)
1385{
1386 pVM->cpum.s.Guest.dr[1] = uDr1;
1387 return CPUMRecalcHyperDRx(pVM);
1388}
1389
1390
1391CPUMDECL(int) CPUMSetGuestDR2(PVM pVM, uint64_t uDr2)
1392{
1393 pVM->cpum.s.Guest.dr[2] = uDr2;
1394 return CPUMRecalcHyperDRx(pVM);
1395}
1396
1397
1398CPUMDECL(int) CPUMSetGuestDR3(PVM pVM, uint64_t uDr3)
1399{
1400 pVM->cpum.s.Guest.dr[3] = uDr3;
1401 return CPUMRecalcHyperDRx(pVM);
1402}
1403
1404
1405CPUMDECL(int) CPUMSetGuestDR6(PVM pVM, uint64_t uDr6)
1406{
1407 pVM->cpum.s.Guest.dr[6] = uDr6;
1408 return CPUMRecalcHyperDRx(pVM);
1409}
1410
1411
1412CPUMDECL(int) CPUMSetGuestDR7(PVM pVM, uint64_t uDr7)
1413{
1414 pVM->cpum.s.Guest.dr[7] = uDr7;
1415 return CPUMRecalcHyperDRx(pVM);
1416}
1417
1418
1419CPUMDECL(int) CPUMSetGuestDRx(PVM pVM, uint32_t iReg, uint64_t Value)
1420{
1421 AssertReturn(iReg <= USE_REG_DR7, VERR_INVALID_PARAMETER);
1422 /* DR4 is an alias for DR6, and DR5 is an alias for DR7. */
1423 if (iReg == 4 || iReg == 5)
1424 iReg += 2;
1425 pVM->cpum.s.Guest.dr[iReg] = Value;
1426 return CPUMRecalcHyperDRx(pVM);
1427}
1428
1429
1430/**
1431 * Recalculates the hypvervisor DRx register values based on
1432 * current guest registers and DBGF breakpoints.
1433 *
1434 * This is called whenever a guest DRx register is modified and when DBGF
1435 * sets a hardware breakpoint. In guest context this function will reload
1436 * any (hyper) DRx registers which comes out with a different value.
1437 *
1438 * @returns VINF_SUCCESS.
1439 * @param pVM The VM handle.
1440 */
1441CPUMDECL(int) CPUMRecalcHyperDRx(PVM pVM)
1442{
1443 /*
1444 * Compare the DR7s first.
1445 *
1446 * We only care about the enabled flags. The GE and LE flags are always
1447 * set and we don't care if the guest doesn't set them. GD is virtualized
1448 * when we dispatch #DB, we never enable it.
1449 */
1450 const RTGCUINTREG uDbgfDr7 = DBGFBpGetDR7(pVM);
1451#ifdef CPUM_VIRTUALIZE_DRX
1452 const RTGCUINTREG uGstDr7 = CPUMGetGuestDR7(pVM);
1453#else
1454 const RTGCUINTREG uGstDr7 = 0;
1455#endif
1456 if ((uGstDr7 | uDbgfDr7) & X86_DR7_ENABLED_MASK)
1457 {
1458 /*
1459 * Ok, something is enabled. Recalc each of the breakpoints.
1460 * Straight forward code, not optimized/minimized in any way.
1461 */
1462 RTGCUINTREG uNewDr7 = X86_DR7_GE | X86_DR7_LE | X86_DR7_MB1_MASK;
1463
1464 /* bp 0 */
1465 RTGCUINTREG uNewDr0;
1466 if (uDbgfDr7 & (X86_DR7_L0 | X86_DR7_G0))
1467 {
1468 uNewDr7 |= uDbgfDr7 & (X86_DR7_L0 | X86_DR7_G0 | X86_DR7_RW0_MASK | X86_DR7_LEN0_MASK);
1469 uNewDr0 = DBGFBpGetDR0(pVM);
1470 }
1471 else if (uGstDr7 & (X86_DR7_L0 | X86_DR7_G0))
1472 {
1473 uNewDr7 |= uGstDr7 & (X86_DR7_L0 | X86_DR7_G0 | X86_DR7_RW0_MASK | X86_DR7_LEN0_MASK);
1474 uNewDr0 = CPUMGetGuestDR0(pVM);
1475 }
1476 else
1477 uNewDr0 = pVM->cpum.s.Hyper.dr[0];
1478
1479 /* bp 1 */
1480 RTGCUINTREG uNewDr1;
1481 if (uDbgfDr7 & (X86_DR7_L1 | X86_DR7_G1))
1482 {
1483 uNewDr7 |= uDbgfDr7 & (X86_DR7_L1 | X86_DR7_G1 | X86_DR7_RW1_MASK | X86_DR7_LEN1_MASK);
1484 uNewDr1 = DBGFBpGetDR1(pVM);
1485 }
1486 else if (uGstDr7 & (X86_DR7_L1 | X86_DR7_G1))
1487 {
1488 uNewDr7 |= uGstDr7 & (X86_DR7_L1 | X86_DR7_G1 | X86_DR7_RW1_MASK | X86_DR7_LEN1_MASK);
1489 uNewDr1 = CPUMGetGuestDR1(pVM);
1490 }
1491 else
1492 uNewDr1 = pVM->cpum.s.Hyper.dr[1];
1493
1494 /* bp 2 */
1495 RTGCUINTREG uNewDr2;
1496 if (uDbgfDr7 & (X86_DR7_L2 | X86_DR7_G2))
1497 {
1498 uNewDr7 |= uDbgfDr7 & (X86_DR7_L2 | X86_DR7_G2 | X86_DR7_RW2_MASK | X86_DR7_LEN2_MASK);
1499 uNewDr2 = DBGFBpGetDR2(pVM);
1500 }
1501 else if (uGstDr7 & (X86_DR7_L2 | X86_DR7_G2))
1502 {
1503 uNewDr7 |= uGstDr7 & (X86_DR7_L2 | X86_DR7_G2 | X86_DR7_RW2_MASK | X86_DR7_LEN2_MASK);
1504 uNewDr2 = CPUMGetGuestDR2(pVM);
1505 }
1506 else
1507 uNewDr2 = pVM->cpum.s.Hyper.dr[2];
1508
1509 /* bp 3 */
1510 RTGCUINTREG uNewDr3;
1511 if (uDbgfDr7 & (X86_DR7_L3 | X86_DR7_G3))
1512 {
1513 uNewDr7 |= uDbgfDr7 & (X86_DR7_L3 | X86_DR7_G3 | X86_DR7_RW3_MASK | X86_DR7_LEN3_MASK);
1514 uNewDr3 = DBGFBpGetDR3(pVM);
1515 }
1516 else if (uGstDr7 & (X86_DR7_L3 | X86_DR7_G3))
1517 {
1518 uNewDr7 |= uGstDr7 & (X86_DR7_L3 | X86_DR7_G3 | X86_DR7_RW3_MASK | X86_DR7_LEN3_MASK);
1519 uNewDr3 = CPUMGetGuestDR3(pVM);
1520 }
1521 else
1522 uNewDr3 = pVM->cpum.s.Hyper.dr[3];
1523
1524 /*
1525 * Apply the updates.
1526 */
1527#ifdef IN_GC
1528 if (!(pVM->cpum.s.fUseFlags & CPUM_USE_DEBUG_REGS))
1529 {
1530 /** @todo save host DBx registers. */
1531 }
1532#endif
1533 pVM->cpum.s.fUseFlags |= CPUM_USE_DEBUG_REGS;
1534 if (uNewDr3 != pVM->cpum.s.Hyper.dr[3])
1535 CPUMSetHyperDR3(pVM, uNewDr3);
1536 if (uNewDr2 != pVM->cpum.s.Hyper.dr[2])
1537 CPUMSetHyperDR2(pVM, uNewDr2);
1538 if (uNewDr1 != pVM->cpum.s.Hyper.dr[1])
1539 CPUMSetHyperDR1(pVM, uNewDr1);
1540 if (uNewDr0 != pVM->cpum.s.Hyper.dr[0])
1541 CPUMSetHyperDR0(pVM, uNewDr0);
1542 if (uNewDr7 != pVM->cpum.s.Hyper.dr[7])
1543 CPUMSetHyperDR7(pVM, uNewDr7);
1544 }
1545 else
1546 {
1547#ifdef IN_GC
1548 if (pVM->cpum.s.fUseFlags & CPUM_USE_DEBUG_REGS)
1549 {
1550 /** @todo restore host DBx registers. */
1551 }
1552#endif
1553 pVM->cpum.s.fUseFlags &= ~CPUM_USE_DEBUG_REGS;
1554 }
1555 Log2(("CPUMRecalcHyperDRx: fUseFlags=%#x %RGr %RGr %RGr %RGr %RGr %RGr\n",
1556 pVM->cpum.s.fUseFlags, pVM->cpum.s.Hyper.dr[0], pVM->cpum.s.Hyper.dr[1],
1557 pVM->cpum.s.Hyper.dr[2], pVM->cpum.s.Hyper.dr[3], pVM->cpum.s.Hyper.dr[6],
1558 pVM->cpum.s.Hyper.dr[7]));
1559
1560 return VINF_SUCCESS;
1561}
1562
1563#ifndef IN_RING0 /** @todo I don't think we need this in R0, so move it to CPUMAll.cpp? */
1564
1565/**
1566 * Transforms the guest CPU state to raw-ring mode.
1567 *
1568 * This function will change the any of the cs and ss register with DPL=0 to DPL=1.
1569 *
1570 * @returns VBox status. (recompiler failure)
1571 * @param pVM VM handle.
1572 * @param pCtxCore The context core (for trap usage).
1573 * @see @ref pg_raw
1574 */
1575CPUMDECL(int) CPUMRawEnter(PVM pVM, PCPUMCTXCORE pCtxCore)
1576{
1577 Assert(!pVM->cpum.s.fRawEntered);
1578 if (!pCtxCore)
1579 pCtxCore = CPUMCTX2CORE(&pVM->cpum.s.Guest);
1580
1581 /*
1582 * Are we in Ring-0?
1583 */
1584 if ( pCtxCore->ss && (pCtxCore->ss & X86_SEL_RPL) == 0
1585 && !pCtxCore->eflags.Bits.u1VM)
1586 {
1587 /*
1588 * Enter execution mode.
1589 */
1590 PATMRawEnter(pVM, pCtxCore);
1591
1592 /*
1593 * Set CPL to Ring-1.
1594 */
1595 pCtxCore->ss |= 1;
1596 if (pCtxCore->cs && (pCtxCore->cs & X86_SEL_RPL) == 0)
1597 pCtxCore->cs |= 1;
1598 }
1599 else
1600 {
1601 AssertMsg((pCtxCore->ss & X86_SEL_RPL) >= 2 || pCtxCore->eflags.Bits.u1VM,
1602 ("ring-1 code not supported\n"));
1603 /*
1604 * PATM takes care of IOPL and IF flags for Ring-3 and Ring-2 code as well.
1605 */
1606 PATMRawEnter(pVM, pCtxCore);
1607 }
1608
1609 /*
1610 * Assert sanity.
1611 */
1612 AssertMsg((pCtxCore->eflags.u32 & X86_EFL_IF), ("X86_EFL_IF is clear\n"));
1613 AssertReleaseMsg( pCtxCore->eflags.Bits.u2IOPL < (unsigned)(pCtxCore->ss & X86_SEL_RPL)
1614 || pCtxCore->eflags.Bits.u1VM,
1615 ("X86_EFL_IOPL=%d CPL=%d\n", pCtxCore->eflags.Bits.u2IOPL, pCtxCore->ss & X86_SEL_RPL));
1616 Assert((pVM->cpum.s.Guest.cr0 & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE)) == (X86_CR0_PG | X86_CR0_PE | X86_CR0_WP));
1617 pCtxCore->eflags.u32 |= X86_EFL_IF; /* paranoia */
1618
1619 pVM->cpum.s.fRawEntered = true;
1620 return VINF_SUCCESS;
1621}
1622
1623
1624/**
1625 * Transforms the guest CPU state from raw-ring mode to correct values.
1626 *
1627 * This function will change any selector registers with DPL=1 to DPL=0.
1628 *
1629 * @returns Adjusted rc.
1630 * @param pVM VM handle.
1631 * @param rc Raw mode return code
1632 * @param pCtxCore The context core (for trap usage).
1633 * @see @ref pg_raw
1634 */
1635CPUMDECL(int) CPUMRawLeave(PVM pVM, PCPUMCTXCORE pCtxCore, int rc)
1636{
1637 /*
1638 * Don't leave if we've already left (in GC).
1639 */
1640 Assert(pVM->cpum.s.fRawEntered);
1641 if (!pVM->cpum.s.fRawEntered)
1642 return rc;
1643 pVM->cpum.s.fRawEntered = false;
1644
1645 PCPUMCTX pCtx = &pVM->cpum.s.Guest;
1646 if (!pCtxCore)
1647 pCtxCore = CPUMCTX2CORE(pCtx);
1648 Assert(pCtxCore->eflags.Bits.u1VM || (pCtxCore->ss & X86_SEL_RPL));
1649 AssertMsg(pCtxCore->eflags.Bits.u1VM || pCtxCore->eflags.Bits.u2IOPL < (unsigned)(pCtxCore->ss & X86_SEL_RPL),
1650 ("X86_EFL_IOPL=%d CPL=%d\n", pCtxCore->eflags.Bits.u2IOPL, pCtxCore->ss & X86_SEL_RPL));
1651
1652 /*
1653 * Are we executing in raw ring-1?
1654 */
1655 if ( (pCtxCore->ss & X86_SEL_RPL) == 1
1656 && !pCtxCore->eflags.Bits.u1VM)
1657 {
1658 /*
1659 * Leave execution mode.
1660 */
1661 PATMRawLeave(pVM, pCtxCore, rc);
1662 /* Not quite sure if this is really required, but shouldn't harm (too much anyways). */
1663 /** @todo See what happens if we remove this. */
1664 if ((pCtxCore->ds & X86_SEL_RPL) == 1)
1665 pCtxCore->ds &= ~X86_SEL_RPL;
1666 if ((pCtxCore->es & X86_SEL_RPL) == 1)
1667 pCtxCore->es &= ~X86_SEL_RPL;
1668 if ((pCtxCore->fs & X86_SEL_RPL) == 1)
1669 pCtxCore->fs &= ~X86_SEL_RPL;
1670 if ((pCtxCore->gs & X86_SEL_RPL) == 1)
1671 pCtxCore->gs &= ~X86_SEL_RPL;
1672
1673 /*
1674 * Ring-1 selector => Ring-0.
1675 */
1676 pCtxCore->ss &= ~X86_SEL_RPL;
1677 if ((pCtxCore->cs & X86_SEL_RPL) == 1)
1678 pCtxCore->cs &= ~X86_SEL_RPL;
1679 }
1680 else
1681 {
1682 /*
1683 * PATM is taking care of the IOPL and IF flags for us.
1684 */
1685 PATMRawLeave(pVM, pCtxCore, rc);
1686 if (!pCtxCore->eflags.Bits.u1VM)
1687 {
1688 /** @todo See what happens if we remove this. */
1689 if ((pCtxCore->ds & X86_SEL_RPL) == 1)
1690 pCtxCore->ds &= ~X86_SEL_RPL;
1691 if ((pCtxCore->es & X86_SEL_RPL) == 1)
1692 pCtxCore->es &= ~X86_SEL_RPL;
1693 if ((pCtxCore->fs & X86_SEL_RPL) == 1)
1694 pCtxCore->fs &= ~X86_SEL_RPL;
1695 if ((pCtxCore->gs & X86_SEL_RPL) == 1)
1696 pCtxCore->gs &= ~X86_SEL_RPL;
1697 }
1698 }
1699
1700 return rc;
1701}
1702
1703/**
1704 * Updates the EFLAGS while we're in raw-mode.
1705 *
1706 * @param pVM The VM handle.
1707 * @param pCtxCore The context core.
1708 * @param eflags The new EFLAGS value.
1709 */
1710CPUMDECL(void) CPUMRawSetEFlags(PVM pVM, PCPUMCTXCORE pCtxCore, uint32_t eflags)
1711{
1712 if (!pVM->cpum.s.fRawEntered)
1713 {
1714 pCtxCore->eflags.u32 = eflags;
1715 return;
1716 }
1717 PATMRawSetEFlags(pVM, pCtxCore, eflags);
1718}
1719
1720#endif /* !IN_RING0 */
1721
1722/**
1723 * Gets the EFLAGS while we're in raw-mode.
1724 *
1725 * @returns The eflags.
1726 * @param pVM The VM handle.
1727 * @param pCtxCore The context core.
1728 */
1729CPUMDECL(uint32_t) CPUMRawGetEFlags(PVM pVM, PCPUMCTXCORE pCtxCore)
1730{
1731#ifdef IN_RING0
1732 return pCtxCore->eflags.u32;
1733#else
1734 if (!pVM->cpum.s.fRawEntered)
1735 return pCtxCore->eflags.u32;
1736 return PATMRawGetEFlags(pVM, pCtxCore);
1737#endif
1738}
1739
1740
1741/**
1742 * Gets and resets the changed flags (CPUM_CHANGED_*).
1743 * Only REM should call this function.
1744 *
1745 * @returns The changed flags.
1746 * @param pVM The VM handle.
1747 */
1748CPUMDECL(unsigned) CPUMGetAndClearChangedFlagsREM(PVM pVM)
1749{
1750 unsigned fFlags = pVM->cpum.s.fChanged;
1751 pVM->cpum.s.fChanged = 0;
1752 /** @todo change the switcher to use the fChanged flags. */
1753 if (pVM->cpum.s.fUseFlags & CPUM_USED_FPU_SINCE_REM)
1754 {
1755 fFlags |= CPUM_CHANGED_FPU_REM;
1756 pVM->cpum.s.fUseFlags &= ~CPUM_USED_FPU_SINCE_REM;
1757 }
1758 return fFlags;
1759}
1760
1761
1762/**
1763 * Sets the specified changed flags (CPUM_CHANGED_*).
1764 *
1765 * @param pVM The VM handle.
1766 */
1767CPUMDECL(void) CPUMSetChangedFlags(PVM pVM, uint32_t fChangedFlags)
1768{
1769 pVM->cpum.s.fChanged |= fChangedFlags;
1770}
1771
1772
1773/**
1774 * Checks if the CPU supports the FXSAVE and FXRSTOR instruction.
1775 * @returns true if supported.
1776 * @returns false if not supported.
1777 * @param pVM The VM handle.
1778 */
1779CPUMDECL(bool) CPUMSupportsFXSR(PVM pVM)
1780{
1781 return pVM->cpum.s.CPUFeatures.edx.u1FXSR != 0;
1782}
1783
1784
1785/**
1786 * Checks if the host OS uses the SYSENTER / SYSEXIT instructions.
1787 * @returns true if used.
1788 * @returns false if not used.
1789 * @param pVM The VM handle.
1790 */
1791CPUMDECL(bool) CPUMIsHostUsingSysEnter(PVM pVM)
1792{
1793 return (pVM->cpum.s.fUseFlags & CPUM_USE_SYSENTER) != 0;
1794}
1795
1796
1797/**
1798 * Checks if the host OS uses the SYSCALL / SYSRET instructions.
1799 * @returns true if used.
1800 * @returns false if not used.
1801 * @param pVM The VM handle.
1802 */
1803CPUMDECL(bool) CPUMIsHostUsingSysCall(PVM pVM)
1804{
1805 return (pVM->cpum.s.fUseFlags & CPUM_USE_SYSCALL) != 0;
1806}
1807
1808#ifndef IN_RING3
1809
1810/**
1811 * Lazily sync in the FPU/XMM state
1812 *
1813 * @returns VBox status code.
1814 * @param pVM VM handle.
1815 */
1816CPUMDECL(int) CPUMHandleLazyFPU(PVM pVM)
1817{
1818 return CPUMHandleLazyFPUAsm(&pVM->cpum.s);
1819}
1820
1821
1822/**
1823 * Restore host FPU/XMM state
1824 *
1825 * @returns VBox status code.
1826 * @param pVM VM handle.
1827 */
1828CPUMDECL(int) CPUMRestoreHostFPUState(PVM pVM)
1829{
1830 Assert(pVM->cpum.s.CPUFeatures.edx.u1FXSR);
1831 return CPUMRestoreHostFPUStateAsm(&pVM->cpum.s);
1832}
1833
1834#endif /* !IN_RING3 */
1835
1836/**
1837 * Checks if we activated the FPU/XMM state of the guest OS
1838 * @returns true if we did.
1839 * @returns false if not.
1840 * @param pVM The VM handle.
1841 */
1842CPUMDECL(bool) CPUMIsGuestFPUStateActive(PVM pVM)
1843{
1844 return (pVM->cpum.s.fUseFlags & CPUM_USED_FPU) != 0;
1845}
1846
1847
1848/**
1849 * Deactivate the FPU/XMM state of the guest OS
1850 * @param pVM The VM handle.
1851 */
1852CPUMDECL(void) CPUMDeactivateGuestFPUState(PVM pVM)
1853{
1854 pVM->cpum.s.fUseFlags &= ~CPUM_USED_FPU;
1855}
1856
1857
1858/**
1859 * Checks if the guest debug state is active
1860 *
1861 * @returns boolean
1862 * @param pVM VM handle.
1863 */
1864CPUMDECL(bool) CPUMIsGuestDebugStateActive(PVM pVM)
1865{
1866 return (pVM->cpum.s.fUseFlags & CPUM_USE_DEBUG_REGS) != 0;
1867}
1868
1869
1870/**
1871 * Mark the guest's debug state as inactive
1872 *
1873 * @returns boolean
1874 * @param pVM VM handle.
1875 */
1876CPUMDECL(void) CPUMDeactivateGuestDebugState(PVM pVM)
1877{
1878 pVM->cpum.s.fUseFlags &= ~CPUM_USE_DEBUG_REGS;
1879}
1880
1881
1882/**
1883 * Checks if the hidden selector registers are valid
1884 * @returns true if they are.
1885 * @returns false if not.
1886 * @param pVM The VM handle.
1887 */
1888CPUMDECL(bool) CPUMAreHiddenSelRegsValid(PVM pVM)
1889{
1890 return !!pVM->cpum.s.fValidHiddenSelRegs; /** @todo change fValidHiddenSelRegs to bool! */
1891}
1892
1893
1894/**
1895 * Checks if the hidden selector registers are valid
1896 * @param pVM The VM handle.
1897 * @param fValid Valid or not
1898 */
1899CPUMDECL(void) CPUMSetHiddenSelRegsValid(PVM pVM, bool fValid)
1900{
1901 pVM->cpum.s.fValidHiddenSelRegs = fValid;
1902}
1903
1904
1905/**
1906 * Get the current privilege level of the guest.
1907 *
1908 * @returns cpl
1909 * @param pVM VM Handle.
1910 * @param pRegFrame Trap register frame.
1911 */
1912CPUMDECL(uint32_t) CPUMGetGuestCPL(PVM pVM, PCPUMCTXCORE pCtxCore)
1913{
1914 uint32_t cpl;
1915
1916 if (CPUMAreHiddenSelRegsValid(pVM))
1917 {
1918 /*
1919 * The hidden CS.DPL register is always equal to the CPL, it is
1920 * not affected by loading a conforming coding segment.
1921 *
1922 * This only seems to apply to AMD-V; in the VT-x case we *do* need to look
1923 * at SS. (ACP2 regression during install after a far call to ring 2)
1924 */
1925 if (RT_LIKELY(pVM->cpum.s.Guest.cr0 & X86_CR0_PE))
1926 cpl = pCtxCore->ssHid.Attr.n.u2Dpl;
1927 else
1928 cpl = 0; /* CPL set to 3 for VT-x real-mode emulation. */
1929 }
1930 else if (RT_LIKELY(pVM->cpum.s.Guest.cr0 & X86_CR0_PE))
1931 {
1932 if (RT_LIKELY(!pCtxCore->eflags.Bits.u1VM))
1933 {
1934 /*
1935 * The SS RPL is always equal to the CPL, while the CS RPL
1936 * isn't necessarily equal if the segment is conforming.
1937 * See section 4.11.1 in the AMD manual.
1938 */
1939 cpl = (pCtxCore->ss & X86_SEL_RPL);
1940#ifndef IN_RING0
1941 if (cpl == 1)
1942 cpl = 0;
1943#endif
1944 }
1945 else
1946 cpl = 3;
1947 }
1948 else
1949 cpl = 0; /* real mode; cpl is zero */
1950
1951 return cpl;
1952}
1953
1954
1955/**
1956 * Gets the current guest CPU mode.
1957 *
1958 * If paging mode is what you need, check out PGMGetGuestMode().
1959 *
1960 * @returns The CPU mode.
1961 * @param pVM The VM handle.
1962 */
1963CPUMDECL(CPUMMODE) CPUMGetGuestMode(PVM pVM)
1964{
1965 CPUMMODE enmMode;
1966 if (!(pVM->cpum.s.Guest.cr0 & X86_CR0_PE))
1967 enmMode = CPUMMODE_REAL;
1968 else if (!(pVM->cpum.s.Guest.msrEFER & MSR_K6_EFER_LMA))
1969 enmMode = CPUMMODE_PROTECTED;
1970 else
1971 enmMode = CPUMMODE_LONG;
1972
1973 return enmMode;
1974}
1975
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