VirtualBox

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

Last change on this file since 5393 was 5389, checked in by vboxsync, 17 years ago

Fully deal with CR0.EM/TS/MP changes in GC.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 41.7 KB
Line 
1/* $Id: CPUMAllRegs.cpp 5389 2007-10-19 16:45:07Z vboxsync $ */
2/** @file
3 * CPUM - CPU Monitor(/Manager) - Gets and Sets.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_CPUM
23#include <VBox/cpum.h>
24#include <VBox/patm.h>
25#include <VBox/dbgf.h>
26#include <VBox/mm.h>
27#include "CPUMInternal.h"
28#include <VBox/vm.h>
29#include <VBox/err.h>
30#include <VBox/dis.h>
31#include <VBox/log.h>
32#include <iprt/assert.h>
33#include <iprt/asm.h>
34
35
36
37/** Disable stack frame pointer generation here. */
38#if defined(_MSC_VER) && !defined(DEBUG)
39# pragma optimize("y", off)
40#endif
41
42
43/**
44 * Sets or resets an alternative hypervisor context core.
45 *
46 * This is called when we get a hypervisor trap set switch the context
47 * core with the trap frame on the stack. It is called again to reset
48 * back to the default context core when resuming hypervisor execution.
49 *
50 * @param pVM The VM handle.
51 * @param pCtxCore Pointer to the alternative context core or NULL
52 * to go back to the default context core.
53 */
54CPUMDECL(void) CPUMHyperSetCtxCore(PVM pVM, PCPUMCTXCORE pCtxCore)
55{
56 LogFlow(("CPUMHyperSetCtxCore: %p/%p/%p -> %p\n", pVM->cpum.s.CTXALLSUFF(pHyperCore), pCtxCore));
57 if (!pCtxCore)
58 {
59 pCtxCore = CPUMCTX2CORE(&pVM->cpum.s.Hyper);
60 pVM->cpum.s.pHyperCoreR3 = (R3PTRTYPE(PCPUMCTXCORE))VM_R3_ADDR(pVM, pCtxCore);
61 pVM->cpum.s.pHyperCoreR0 = (R0PTRTYPE(PCPUMCTXCORE))VM_R0_ADDR(pVM, pCtxCore);
62 pVM->cpum.s.pHyperCoreGC = (GCPTRTYPE(PCPUMCTXCORE))VM_GUEST_ADDR(pVM, pCtxCore);
63 }
64 else
65 {
66 pVM->cpum.s.pHyperCoreR3 = (R3PTRTYPE(PCPUMCTXCORE))MMHyperCCToR3(pVM, pCtxCore);
67 pVM->cpum.s.pHyperCoreR0 = (R0PTRTYPE(PCPUMCTXCORE))MMHyperCCToR0(pVM, pCtxCore);
68 pVM->cpum.s.pHyperCoreGC = (GCPTRTYPE(PCPUMCTXCORE))MMHyperCCToGC(pVM, pCtxCore);
69 }
70}
71
72
73/**
74 * Gets the pointer to the internal CPUMCTXCORE structure for the hypervisor.
75 * This is only for reading in order to save a few calls.
76 *
77 * @param pVM Handle to the virtual machine.
78 */
79CPUMDECL(PCCPUMCTXCORE) CPUMGetHyperCtxCore(PVM pVM)
80{
81 return pVM->cpum.s.CTXALLSUFF(pHyperCore);
82}
83
84
85/**
86 * Queries the pointer to the internal CPUMCTX structure for the hypervisor.
87 *
88 * @returns VBox status code.
89 * @param pVM Handle to the virtual machine.
90 * @param ppCtx Receives the hyper CPUMCTX pointer when successful.
91 *
92 * @deprecated This will *not* (and has never) given the right picture of the
93 * hypervisor register state. With CPUMHyperSetCtxCore() this is
94 * getting much worse. So, use the individual functions for getting
95 * and esp. setting the hypervisor registers.
96 */
97CPUMDECL(int) CPUMQueryHyperCtxPtr(PVM pVM, PCPUMCTX *ppCtx)
98{
99 *ppCtx = &pVM->cpum.s.Hyper;
100 return VINF_SUCCESS;
101}
102
103CPUMDECL(void) CPUMSetHyperGDTR(PVM pVM, uint32_t addr, uint16_t limit)
104{
105 pVM->cpum.s.Hyper.gdtr.cbGdt = limit;
106 pVM->cpum.s.Hyper.gdtr.pGdt = addr;
107 pVM->cpum.s.Hyper.gdtrPadding = 0;
108 pVM->cpum.s.Hyper.gdtrPadding64 = 0;
109}
110
111CPUMDECL(void) CPUMSetHyperIDTR(PVM pVM, uint32_t addr, uint16_t limit)
112{
113 pVM->cpum.s.Hyper.idtr.cbIdt = limit;
114 pVM->cpum.s.Hyper.idtr.pIdt = addr;
115 pVM->cpum.s.Hyper.idtrPadding = 0;
116 pVM->cpum.s.Hyper.idtrPadding64 = 0;
117}
118
119CPUMDECL(void) CPUMSetHyperCR3(PVM pVM, uint32_t cr3)
120{
121 pVM->cpum.s.Hyper.cr3 = cr3;
122}
123
124CPUMDECL(void) CPUMSetHyperCS(PVM pVM, RTSEL SelCS)
125{
126 pVM->cpum.s.CTXALLSUFF(pHyperCore)->cs = SelCS;
127}
128
129CPUMDECL(void) CPUMSetHyperDS(PVM pVM, RTSEL SelDS)
130{
131 pVM->cpum.s.CTXALLSUFF(pHyperCore)->ds = SelDS;
132}
133
134CPUMDECL(void) CPUMSetHyperES(PVM pVM, RTSEL SelES)
135{
136 pVM->cpum.s.CTXALLSUFF(pHyperCore)->es = SelES;
137}
138
139CPUMDECL(void) CPUMSetHyperFS(PVM pVM, RTSEL SelFS)
140{
141 pVM->cpum.s.CTXALLSUFF(pHyperCore)->fs = SelFS;
142}
143
144CPUMDECL(void) CPUMSetHyperGS(PVM pVM, RTSEL SelGS)
145{
146 pVM->cpum.s.CTXALLSUFF(pHyperCore)->gs = SelGS;
147}
148
149CPUMDECL(void) CPUMSetHyperSS(PVM pVM, RTSEL SelSS)
150{
151 pVM->cpum.s.CTXALLSUFF(pHyperCore)->ss = SelSS;
152}
153
154CPUMDECL(void) CPUMSetHyperESP(PVM pVM, uint32_t u32ESP)
155{
156 pVM->cpum.s.CTXALLSUFF(pHyperCore)->esp = u32ESP;
157}
158
159CPUMDECL(int) CPUMSetHyperEFlags(PVM pVM, uint32_t Efl)
160{
161 pVM->cpum.s.CTXALLSUFF(pHyperCore)->eflags.u32 = Efl;
162 return VINF_SUCCESS;
163}
164
165CPUMDECL(void) CPUMSetHyperEIP(PVM pVM, uint32_t u32EIP)
166{
167 pVM->cpum.s.CTXALLSUFF(pHyperCore)->eip = u32EIP;
168}
169
170CPUMDECL(void) CPUMSetHyperTR(PVM pVM, RTSEL SelTR)
171{
172 pVM->cpum.s.Hyper.tr = SelTR;
173}
174
175CPUMDECL(void) CPUMSetHyperLDTR(PVM pVM, RTSEL SelLDTR)
176{
177 pVM->cpum.s.Hyper.ldtr = SelLDTR;
178}
179
180CPUMDECL(void) CPUMSetHyperDR0(PVM pVM, RTGCUINTREG uDr0)
181{
182 pVM->cpum.s.Hyper.dr0 = uDr0;
183 /** @todo in GC we must load it! */
184}
185
186CPUMDECL(void) CPUMSetHyperDR1(PVM pVM, RTGCUINTREG uDr1)
187{
188 pVM->cpum.s.Hyper.dr1 = uDr1;
189 /** @todo in GC we must load it! */
190}
191
192CPUMDECL(void) CPUMSetHyperDR2(PVM pVM, RTGCUINTREG uDr2)
193{
194 pVM->cpum.s.Hyper.dr2 = uDr2;
195 /** @todo in GC we must load it! */
196}
197
198CPUMDECL(void) CPUMSetHyperDR3(PVM pVM, RTGCUINTREG uDr3)
199{
200 pVM->cpum.s.Hyper.dr3 = uDr3;
201 /** @todo in GC we must load it! */
202}
203
204CPUMDECL(void) CPUMSetHyperDR6(PVM pVM, RTGCUINTREG uDr6)
205{
206 pVM->cpum.s.Hyper.dr6 = uDr6;
207 /** @todo in GC we must load it! */
208}
209
210CPUMDECL(void) CPUMSetHyperDR7(PVM pVM, RTGCUINTREG uDr7)
211{
212 pVM->cpum.s.Hyper.dr7 = uDr7;
213 /** @todo in GC we must load it! */
214}
215
216
217CPUMDECL(RTSEL) CPUMGetHyperCS(PVM pVM)
218{
219 return pVM->cpum.s.CTXALLSUFF(pHyperCore)->cs;
220}
221
222CPUMDECL(RTSEL) CPUMGetHyperDS(PVM pVM)
223{
224 return pVM->cpum.s.CTXALLSUFF(pHyperCore)->ds;
225}
226
227CPUMDECL(RTSEL) CPUMGetHyperES(PVM pVM)
228{
229 return pVM->cpum.s.CTXALLSUFF(pHyperCore)->es;
230}
231
232CPUMDECL(RTSEL) CPUMGetHyperFS(PVM pVM)
233{
234 return pVM->cpum.s.CTXALLSUFF(pHyperCore)->fs;
235}
236
237CPUMDECL(RTSEL) CPUMGetHyperGS(PVM pVM)
238{
239 return pVM->cpum.s.CTXALLSUFF(pHyperCore)->gs;
240}
241
242CPUMDECL(RTSEL) CPUMGetHyperSS(PVM pVM)
243{
244 return pVM->cpum.s.CTXALLSUFF(pHyperCore)->ss;
245}
246
247#if 0 /* these are not correct. */
248
249CPUMDECL(uint32_t) CPUMGetHyperCR0(PVM pVM)
250{
251 return pVM->cpum.s.Hyper.cr0;
252}
253
254CPUMDECL(uint32_t) CPUMGetHyperCR2(PVM pVM)
255{
256 return pVM->cpum.s.Hyper.cr2;
257}
258
259CPUMDECL(uint32_t) CPUMGetHyperCR3(PVM pVM)
260{
261 return pVM->cpum.s.Hyper.cr3;
262}
263
264CPUMDECL(uint32_t) CPUMGetHyperCR4(PVM pVM)
265{
266 return pVM->cpum.s.Hyper.cr4;
267}
268
269#endif /* not correct */
270
271CPUMDECL(uint32_t) CPUMGetHyperEAX(PVM pVM)
272{
273 return pVM->cpum.s.CTXALLSUFF(pHyperCore)->eax;
274}
275
276CPUMDECL(uint32_t) CPUMGetHyperEBX(PVM pVM)
277{
278 return pVM->cpum.s.CTXALLSUFF(pHyperCore)->ebx;
279}
280
281CPUMDECL(uint32_t) CPUMGetHyperECX(PVM pVM)
282{
283 return pVM->cpum.s.CTXALLSUFF(pHyperCore)->ecx;
284}
285
286CPUMDECL(uint32_t) CPUMGetHyperEDX(PVM pVM)
287{
288 return pVM->cpum.s.CTXALLSUFF(pHyperCore)->edx;
289}
290
291CPUMDECL(uint32_t) CPUMGetHyperESI(PVM pVM)
292{
293 return pVM->cpum.s.CTXALLSUFF(pHyperCore)->esi;
294}
295
296CPUMDECL(uint32_t) CPUMGetHyperEDI(PVM pVM)
297{
298 return pVM->cpum.s.CTXALLSUFF(pHyperCore)->edi;
299}
300
301CPUMDECL(uint32_t) CPUMGetHyperEBP(PVM pVM)
302{
303 return pVM->cpum.s.CTXALLSUFF(pHyperCore)->ebp;
304}
305
306CPUMDECL(uint32_t) CPUMGetHyperESP(PVM pVM)
307{
308 return pVM->cpum.s.CTXALLSUFF(pHyperCore)->esp;
309}
310
311CPUMDECL(uint32_t) CPUMGetHyperEFlags(PVM pVM)
312{
313 return pVM->cpum.s.CTXALLSUFF(pHyperCore)->eflags.u32;
314}
315
316CPUMDECL(uint32_t) CPUMGetHyperEIP(PVM pVM)
317{
318 return pVM->cpum.s.CTXALLSUFF(pHyperCore)->eip;
319}
320
321CPUMDECL(uint32_t) CPUMGetHyperIDTR(PVM pVM, uint16_t *pcbLimit)
322{
323 if (pcbLimit)
324 *pcbLimit = pVM->cpum.s.Hyper.idtr.cbIdt;
325 return pVM->cpum.s.Hyper.idtr.pIdt;
326}
327
328CPUMDECL(uint32_t) CPUMGetHyperGDTR(PVM pVM, uint16_t *pcbLimit)
329{
330 if (pcbLimit)
331 *pcbLimit = pVM->cpum.s.Hyper.gdtr.cbGdt;
332 return pVM->cpum.s.Hyper.gdtr.pGdt;
333}
334
335CPUMDECL(RTSEL) CPUMGetHyperLDTR(PVM pVM)
336{
337 return pVM->cpum.s.Hyper.ldtr;
338}
339
340CPUMDECL(RTGCUINTREG) CPUMGetHyperDR0(PVM pVM)
341{
342 return pVM->cpum.s.Hyper.dr0;
343}
344
345CPUMDECL(RTGCUINTREG) CPUMGetHyperDR1(PVM pVM)
346{
347 return pVM->cpum.s.Hyper.dr1;
348}
349
350CPUMDECL(RTGCUINTREG) CPUMGetHyperDR2(PVM pVM)
351{
352 return pVM->cpum.s.Hyper.dr2;
353}
354
355CPUMDECL(RTGCUINTREG) CPUMGetHyperDR3(PVM pVM)
356{
357 return pVM->cpum.s.Hyper.dr3;
358}
359
360CPUMDECL(RTGCUINTREG) CPUMGetHyperDR6(PVM pVM)
361{
362 return pVM->cpum.s.Hyper.dr6;
363}
364
365CPUMDECL(RTGCUINTREG) CPUMGetHyperDR7(PVM pVM)
366{
367 return pVM->cpum.s.Hyper.dr7;
368}
369
370
371/**
372 * Gets the pointer to the internal CPUMCTXCORE structure.
373 * This is only for reading in order to save a few calls.
374 *
375 * @param pVM Handle to the virtual machine.
376 */
377CPUMDECL(PCCPUMCTXCORE) CPUMGetGuestCtxCore(PVM pVM)
378{
379 return CPUMCTX2CORE(&pVM->cpum.s.Guest);
380}
381
382
383/**
384 * Sets the guest context core registers.
385 *
386 * @param pVM Handle to the virtual machine.
387 * @param pCtxCore The new context core values.
388 */
389CPUMDECL(void) CPUMSetGuestCtxCore(PVM pVM, PCCPUMCTXCORE pCtxCore)
390{
391 /** @todo #1410 requires selectors to be checked. */
392
393 PCPUMCTXCORE pCtxCoreDst CPUMCTX2CORE(&pVM->cpum.s.Guest);
394 *pCtxCoreDst = *pCtxCore;
395}
396
397
398/**
399 * Queries the pointer to the internal CPUMCTX structure
400 *
401 * @returns VBox status code.
402 * @param pVM Handle to the virtual machine.
403 * @param ppCtx Receives the CPUMCTX pointer when successful.
404 */
405CPUMDECL(int) CPUMQueryGuestCtxPtr(PVM pVM, PCPUMCTX *ppCtx)
406{
407 *ppCtx = &pVM->cpum.s.Guest;
408 return VINF_SUCCESS;
409}
410
411
412CPUMDECL(int) CPUMSetGuestGDTR(PVM pVM, uint32_t addr, uint16_t limit)
413{
414 pVM->cpum.s.Guest.gdtr.cbGdt = limit;
415 pVM->cpum.s.Guest.gdtr.pGdt = addr;
416 pVM->cpum.s.fChanged |= CPUM_CHANGED_GDTR;
417 return VINF_SUCCESS;
418}
419
420CPUMDECL(int) CPUMSetGuestIDTR(PVM pVM, uint32_t addr, uint16_t limit)
421{
422 pVM->cpum.s.Guest.idtr.cbIdt = limit;
423 pVM->cpum.s.Guest.idtr.pIdt = addr;
424 pVM->cpum.s.fChanged |= CPUM_CHANGED_IDTR;
425 return VINF_SUCCESS;
426}
427
428CPUMDECL(int) CPUMSetGuestTR(PVM pVM, uint16_t tr)
429{
430 pVM->cpum.s.Guest.tr = tr;
431 pVM->cpum.s.fChanged |= CPUM_CHANGED_TR;
432 return VINF_SUCCESS;
433}
434
435CPUMDECL(int) CPUMSetGuestLDTR(PVM pVM, uint16_t ldtr)
436{
437 pVM->cpum.s.Guest.ldtr = ldtr;
438 pVM->cpum.s.fChanged |= CPUM_CHANGED_LDTR;
439 return VINF_SUCCESS;
440}
441
442
443/**
444 * Set the guest CR0.
445 *
446 * When called in GC, the hyper CR0 may be updated if that is
447 * required. The caller only has to take special action if AM,
448 * WP, PG or PE changes.
449 *
450 * @returns VINF_SUCCESS (consider it void).
451 * @param pVM Pointer to the shared VM structure.
452 * @param cr0 The new CR0 value.
453 */
454CPUMDECL(int) CPUMSetGuestCR0(PVM pVM, uint32_t cr0)
455{
456#ifdef IN_GC
457 /*
458 * Check if we need to change hypervisor CR0 because
459 * of math stuff.
460 */
461 if ( (cr0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP))
462 != (pVM->cpum.s.Guest.cr0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP)))
463 {
464 if (!(pVM->cpum.s.fUseFlags & CPUM_USED_FPU))
465 {
466 /*
467 * We haven't saved the host FPU state yet, so TS and MT are both set
468 * and EM should be reflecting the guest EM (it always does this).
469 */
470 if ((cr0 & X86_CR0_EM) != (pVM->cpum.s.Guest.cr0 & X86_CR0_EM))
471 {
472 uint32_t HyperCR0 = ASMGetCR0();
473 AssertMsg((HyperCR0 & (X86_CR0_TS | X86_CR0_MP)) == (X86_CR0_TS | X86_CR0_MP), ("%#x\n", HyperCR0));
474 AssertMsg((HyperCR0 & X86_CR0_EM) == (pVM->cpum.s.Guest.cr0 & X86_CR0_EM), ("%#x\n", HyperCR0));
475 HyperCR0 &= ~X86_CR0_EM;
476 HyperCR0 |= cr0 & X86_CR0_EM;
477 Log(("CPUM New HyperCR0=%#x\n", HyperCR0));
478 ASMSetCR0(HyperCR0);
479 }
480#ifdef VBOX_STRICT
481 else
482 {
483 uint32_t HyperCR0 = ASMGetCR0();
484 AssertMsg((HyperCR0 & (X86_CR0_TS | X86_CR0_MP)) == (X86_CR0_TS | X86_CR0_MP), ("%#x\n", HyperCR0));
485 AssertMsg((HyperCR0 & X86_CR0_EM) == (pVM->cpum.s.Guest.cr0 & X86_CR0_EM), ("%#x\n", HyperCR0));
486 }
487#endif
488 }
489 else
490 {
491 /*
492 * Already saved the state, so we're just mirroring
493 * the guest flags.
494 */
495 uint32_t HyperCR0 = ASMGetCR0();
496 AssertMsg( (HyperCR0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP))
497 == (pVM->cpum.s.Guest.cr0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP)),
498 ("%#x %#x\n", HyperCR0, pVM->cpum.s.Guest.cr0));
499 HyperCR0 &= ~(X86_CR0_TS | X86_CR0_EM | X86_CR0_MP);
500 HyperCR0 |= cr0 & (X86_CR0_TS | X86_CR0_EM | X86_CR0_MP);
501 Log(("CPUM New HyperCR0=%#x\n", HyperCR0));
502 ASMSetCR0(HyperCR0);
503 }
504 }
505#endif
506
507 /*
508 * Check for changes causing TLB flushes (for REM).
509 * The caller is responsible for calling PGM when appropriate.
510 */
511 if ( (cr0 & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE))
512 != (pVM->cpum.s.Guest.cr0 & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE)))
513 pVM->cpum.s.fChanged |= CPUM_CHANGED_GLOBAL_TLB_FLUSH;
514 pVM->cpum.s.fChanged |= CPUM_CHANGED_CR0;
515
516 pVM->cpum.s.Guest.cr0 = cr0 | X86_CR0_ET;
517 return VINF_SUCCESS;
518}
519
520CPUMDECL(int) CPUMSetGuestCR2(PVM pVM, uint32_t cr2)
521{
522 pVM->cpum.s.Guest.cr2 = cr2;
523 return VINF_SUCCESS;
524}
525
526CPUMDECL(int) CPUMSetGuestCR3(PVM pVM, uint32_t cr3)
527{
528 pVM->cpum.s.Guest.cr3 = cr3;
529 pVM->cpum.s.fChanged |= CPUM_CHANGED_CR3;
530 return VINF_SUCCESS;
531}
532
533CPUMDECL(int) CPUMSetGuestCR4(PVM pVM, uint32_t cr4)
534{
535 if ( (cr4 & (X86_CR4_PGE | X86_CR4_PAE | X86_CR4_PSE))
536 != (pVM->cpum.s.Guest.cr4 & (X86_CR4_PGE | X86_CR4_PAE | X86_CR4_PSE)))
537 pVM->cpum.s.fChanged |= CPUM_CHANGED_GLOBAL_TLB_FLUSH;
538 pVM->cpum.s.fChanged |= CPUM_CHANGED_CR4;
539 if (!CPUMSupportsFXSR(pVM))
540 cr4 &= ~X86_CR4_OSFSXR;
541 pVM->cpum.s.Guest.cr4 = cr4;
542 return VINF_SUCCESS;
543}
544
545CPUMDECL(int) CPUMSetGuestEFlags(PVM pVM, uint32_t eflags)
546{
547 pVM->cpum.s.Guest.eflags.u32 = eflags;
548 return VINF_SUCCESS;
549}
550
551CPUMDECL(int) CPUMSetGuestEIP(PVM pVM, uint32_t eip)
552{
553 pVM->cpum.s.Guest.eip = eip;
554 return VINF_SUCCESS;
555}
556
557CPUMDECL(int) CPUMSetGuestEAX(PVM pVM, uint32_t eax)
558{
559 pVM->cpum.s.Guest.eax = eax;
560 return VINF_SUCCESS;
561}
562
563CPUMDECL(int) CPUMSetGuestEBX(PVM pVM, uint32_t ebx)
564{
565 pVM->cpum.s.Guest.ebx = ebx;
566 return VINF_SUCCESS;
567}
568
569CPUMDECL(int) CPUMSetGuestECX(PVM pVM, uint32_t ecx)
570{
571 pVM->cpum.s.Guest.ecx = ecx;
572 return VINF_SUCCESS;
573}
574
575CPUMDECL(int) CPUMSetGuestEDX(PVM pVM, uint32_t edx)
576{
577 pVM->cpum.s.Guest.edx = edx;
578 return VINF_SUCCESS;
579}
580
581CPUMDECL(int) CPUMSetGuestESP(PVM pVM, uint32_t esp)
582{
583 pVM->cpum.s.Guest.esp = esp;
584 return VINF_SUCCESS;
585}
586
587CPUMDECL(int) CPUMSetGuestEBP(PVM pVM, uint32_t ebp)
588{
589 pVM->cpum.s.Guest.ebp = ebp;
590 return VINF_SUCCESS;
591}
592
593CPUMDECL(int) CPUMSetGuestESI(PVM pVM, uint32_t esi)
594{
595 pVM->cpum.s.Guest.esi = esi;
596 return VINF_SUCCESS;
597}
598
599CPUMDECL(int) CPUMSetGuestEDI(PVM pVM, uint32_t edi)
600{
601 pVM->cpum.s.Guest.edi = edi;
602 return VINF_SUCCESS;
603}
604
605CPUMDECL(int) CPUMSetGuestSS(PVM pVM, uint16_t ss)
606{
607 pVM->cpum.s.Guest.ss = ss;
608 return VINF_SUCCESS;
609}
610
611CPUMDECL(int) CPUMSetGuestCS(PVM pVM, uint16_t cs)
612{
613 pVM->cpum.s.Guest.cs = cs;
614 return VINF_SUCCESS;
615}
616
617CPUMDECL(int) CPUMSetGuestDS(PVM pVM, uint16_t ds)
618{
619 pVM->cpum.s.Guest.ds = ds;
620 return VINF_SUCCESS;
621}
622
623CPUMDECL(int) CPUMSetGuestES(PVM pVM, uint16_t es)
624{
625 pVM->cpum.s.Guest.es = es;
626 return VINF_SUCCESS;
627}
628
629CPUMDECL(int) CPUMSetGuestFS(PVM pVM, uint16_t fs)
630{
631 pVM->cpum.s.Guest.fs = fs;
632 return VINF_SUCCESS;
633}
634
635CPUMDECL(int) CPUMSetGuestGS(PVM pVM, uint16_t gs)
636{
637 pVM->cpum.s.Guest.gs = gs;
638 return VINF_SUCCESS;
639}
640
641
642CPUMDECL(uint32_t) CPUMGetGuestIDTR(PVM pVM, uint16_t *pcbLimit)
643{
644 if (pcbLimit)
645 *pcbLimit = pVM->cpum.s.Guest.idtr.cbIdt;
646 return pVM->cpum.s.Guest.idtr.pIdt;
647}
648
649CPUMDECL(RTSEL) CPUMGetGuestTR(PVM pVM)
650{
651 return pVM->cpum.s.Guest.tr;
652}
653
654CPUMDECL(RTSEL) CPUMGetGuestCS(PVM pVM)
655{
656 return pVM->cpum.s.Guest.cs;
657}
658
659CPUMDECL(RTSEL) CPUMGetGuestDS(PVM pVM)
660{
661 return pVM->cpum.s.Guest.ds;
662}
663
664CPUMDECL(RTSEL) CPUMGetGuestES(PVM pVM)
665{
666 return pVM->cpum.s.Guest.es;
667}
668
669CPUMDECL(RTSEL) CPUMGetGuestFS(PVM pVM)
670{
671 return pVM->cpum.s.Guest.fs;
672}
673
674CPUMDECL(RTSEL) CPUMGetGuestGS(PVM pVM)
675{
676 return pVM->cpum.s.Guest.gs;
677}
678
679CPUMDECL(RTSEL) CPUMGetGuestSS(PVM pVM)
680{
681 return pVM->cpum.s.Guest.ss;
682}
683
684CPUMDECL(RTSEL) CPUMGetGuestLDTR(PVM pVM)
685{
686 return pVM->cpum.s.Guest.ldtr;
687}
688
689
690CPUMDECL(uint32_t) CPUMGetGuestCR0(PVM pVM)
691{
692 return pVM->cpum.s.Guest.cr0;
693}
694
695CPUMDECL(uint32_t) CPUMGetGuestCR2(PVM pVM)
696{
697 return pVM->cpum.s.Guest.cr2;
698}
699
700CPUMDECL(uint32_t) CPUMGetGuestCR3(PVM pVM)
701{
702 return pVM->cpum.s.Guest.cr3;
703}
704
705CPUMDECL(uint32_t) CPUMGetGuestCR4(PVM pVM)
706{
707 return pVM->cpum.s.Guest.cr4;
708}
709
710CPUMDECL(void) CPUMGetGuestGDTR(PVM pVM, PVBOXGDTR pGDTR)
711{
712 *pGDTR = pVM->cpum.s.Guest.gdtr;
713}
714
715CPUMDECL(uint32_t) CPUMGetGuestEIP(PVM pVM)
716{
717 return pVM->cpum.s.Guest.eip;
718}
719
720CPUMDECL(uint32_t) CPUMGetGuestEAX(PVM pVM)
721{
722 return pVM->cpum.s.Guest.eax;
723}
724
725CPUMDECL(uint32_t) CPUMGetGuestEBX(PVM pVM)
726{
727 return pVM->cpum.s.Guest.ebx;
728}
729
730CPUMDECL(uint32_t) CPUMGetGuestECX(PVM pVM)
731{
732 return pVM->cpum.s.Guest.ecx;
733}
734
735CPUMDECL(uint32_t) CPUMGetGuestEDX(PVM pVM)
736{
737 return pVM->cpum.s.Guest.edx;
738}
739
740CPUMDECL(uint32_t) CPUMGetGuestESI(PVM pVM)
741{
742 return pVM->cpum.s.Guest.esi;
743}
744
745CPUMDECL(uint32_t) CPUMGetGuestEDI(PVM pVM)
746{
747 return pVM->cpum.s.Guest.edi;
748}
749
750CPUMDECL(uint32_t) CPUMGetGuestESP(PVM pVM)
751{
752 return pVM->cpum.s.Guest.esp;
753}
754
755CPUMDECL(uint32_t) CPUMGetGuestEBP(PVM pVM)
756{
757 return pVM->cpum.s.Guest.ebp;
758}
759
760CPUMDECL(uint32_t) CPUMGetGuestEFlags(PVM pVM)
761{
762 return pVM->cpum.s.Guest.eflags.u32;
763}
764
765CPUMDECL(CPUMSELREGHID *) CPUMGetGuestTRHid(PVM pVM)
766{
767 return &pVM->cpum.s.Guest.trHid;
768}
769
770//@todo: crx should be an array
771CPUMDECL(int) CPUMGetGuestCRx(PVM pVM, uint32_t iReg, uint32_t *pValue)
772{
773 switch (iReg)
774 {
775 case USE_REG_CR0:
776 *pValue = pVM->cpum.s.Guest.cr0;
777 break;
778 case USE_REG_CR2:
779 *pValue = pVM->cpum.s.Guest.cr2;
780 break;
781 case USE_REG_CR3:
782 *pValue = pVM->cpum.s.Guest.cr3;
783 break;
784 case USE_REG_CR4:
785 *pValue = pVM->cpum.s.Guest.cr4;
786 break;
787 default:
788 return VERR_INVALID_PARAMETER;
789 }
790 return VINF_SUCCESS;
791}
792
793CPUMDECL(RTUINTREG) CPUMGetGuestDR0(PVM pVM)
794{
795 return pVM->cpum.s.Guest.dr0;
796}
797
798CPUMDECL(RTUINTREG) CPUMGetGuestDR1(PVM pVM)
799{
800 return pVM->cpum.s.Guest.dr1;
801}
802
803CPUMDECL(RTUINTREG) CPUMGetGuestDR2(PVM pVM)
804{
805 return pVM->cpum.s.Guest.dr2;
806}
807
808CPUMDECL(RTUINTREG) CPUMGetGuestDR3(PVM pVM)
809{
810 return pVM->cpum.s.Guest.dr3;
811}
812
813CPUMDECL(RTUINTREG) CPUMGetGuestDR6(PVM pVM)
814{
815 return pVM->cpum.s.Guest.dr6;
816}
817
818CPUMDECL(RTUINTREG) CPUMGetGuestDR7(PVM pVM)
819{
820 return pVM->cpum.s.Guest.dr7;
821}
822
823/** @todo drx should be an array */
824CPUMDECL(int) CPUMGetGuestDRx(PVM pVM, uint32_t iReg, uint32_t *pValue)
825{
826 switch (iReg)
827 {
828 case USE_REG_DR0:
829 *pValue = pVM->cpum.s.Guest.dr0;
830 break;
831 case USE_REG_DR1:
832 *pValue = pVM->cpum.s.Guest.dr1;
833 break;
834 case USE_REG_DR2:
835 *pValue = pVM->cpum.s.Guest.dr2;
836 break;
837 case USE_REG_DR3:
838 *pValue = pVM->cpum.s.Guest.dr3;
839 break;
840 case USE_REG_DR4:
841 case USE_REG_DR6:
842 *pValue = pVM->cpum.s.Guest.dr6;
843 break;
844 case USE_REG_DR5:
845 case USE_REG_DR7:
846 *pValue = pVM->cpum.s.Guest.dr7;
847 break;
848
849 default:
850 return VERR_INVALID_PARAMETER;
851 }
852 return VINF_SUCCESS;
853}
854
855/**
856 * Gets a CpuId leaf.
857 *
858 * @param pVM The VM handle.
859 * @param iLeaf The CPUID leaf to get.
860 * @param pEax Where to store the EAX value.
861 * @param pEbx Where to store the EBX value.
862 * @param pEcx Where to store the ECX value.
863 * @param pEdx Where to store the EDX value.
864 */
865CPUMDECL(void) CPUMGetGuestCpuId(PVM pVM, uint32_t iLeaf, uint32_t *pEax, uint32_t *pEbx, uint32_t *pEcx, uint32_t *pEdx)
866{
867 PCCPUMCPUID pCpuId;
868 if (iLeaf < ELEMENTS(pVM->cpum.s.aGuestCpuIdStd))
869 pCpuId = &pVM->cpum.s.aGuestCpuIdStd[iLeaf];
870 else if (iLeaf - UINT32_C(0x80000000) < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdExt))
871 pCpuId = &pVM->cpum.s.aGuestCpuIdExt[iLeaf - UINT32_C(0x80000000)];
872 else if (iLeaf - UINT32_C(0xc0000000) < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdCentaur))
873 pCpuId = &pVM->cpum.s.aGuestCpuIdCentaur[iLeaf - UINT32_C(0xc0000000)];
874 else
875 pCpuId = &pVM->cpum.s.GuestCpuIdDef;
876
877 *pEax = pCpuId->eax;
878 *pEbx = pCpuId->ebx;
879 *pEcx = pCpuId->ecx;
880 *pEdx = pCpuId->edx;
881 Log2(("CPUMGetGuestCpuId: iLeaf=%#010x %RX32 %RX32 %RX32 %RX32\n", iLeaf, *pEax, *pEbx, *pEcx, *pEdx));
882}
883
884/**
885 * Gets a pointer to the array of standard CPUID leafs.
886 *
887 * CPUMGetGuestCpuIdStdMax() give the size of the array.
888 *
889 * @returns Pointer to the standard CPUID leafs (read-only).
890 * @param pVM The VM handle.
891 * @remark Intended for PATM.
892 */
893CPUMDECL(GCPTRTYPE(PCCPUMCPUID)) CPUMGetGuestCpuIdStdGCPtr(PVM pVM)
894{
895 return GCPTRTYPE(PCCPUMCPUID)VM_GUEST_ADDR(pVM, &pVM->cpum.s.aGuestCpuIdStd[0]);
896}
897
898/**
899 * Gets a pointer to the array of extended CPUID leafs.
900 *
901 * CPUMGetGuestCpuIdExtMax() give the size of the array.
902 *
903 * @returns Pointer to the extended CPUID leafs (read-only).
904 * @param pVM The VM handle.
905 * @remark Intended for PATM.
906 */
907CPUMDECL(GCPTRTYPE(PCCPUMCPUID)) CPUMGetGuestCpuIdExtGCPtr(PVM pVM)
908{
909 return GCPTRTYPE(PCCPUMCPUID)VM_GUEST_ADDR(pVM, &pVM->cpum.s.aGuestCpuIdExt[0]);
910}
911
912/**
913 * Gets a pointer to the array of centaur CPUID leafs.
914 *
915 * CPUMGetGuestCpuIdCentaurMax() give the size of the array.
916 *
917 * @returns Pointer to the centaur CPUID leafs (read-only).
918 * @param pVM The VM handle.
919 * @remark Intended for PATM.
920 */
921CPUMDECL(GCPTRTYPE(PCCPUMCPUID)) CPUMGetGuestCpuIdCentaurGCPtr(PVM pVM)
922{
923 return GCPTRTYPE(PCCPUMCPUID)VM_GUEST_ADDR(pVM, &pVM->cpum.s.aGuestCpuIdCentaur[0]);
924}
925
926/**
927 * Gets a pointer to the default CPUID leaf.
928 *
929 * @returns Pointer to the default CPUID leaf (read-only).
930 * @param pVM The VM handle.
931 * @remark Intended for PATM.
932 */
933CPUMDECL(GCPTRTYPE(PCCPUMCPUID)) CPUMGetGuestCpuIdDefGCPtr(PVM pVM)
934{
935 return GCPTRTYPE(PCCPUMCPUID)VM_GUEST_ADDR(pVM, &pVM->cpum.s.GuestCpuIdDef);
936}
937
938/**
939 * Gets a number of standard CPUID leafs.
940 *
941 * @returns Number of leafs.
942 * @param pVM The VM handle.
943 * @remark Intended for PATM.
944 */
945CPUMDECL(uint32_t) CPUMGetGuestCpuIdStdMax(PVM pVM)
946{
947 return RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdStd);
948}
949
950/**
951 * Gets a number of extended CPUID leafs.
952 *
953 * @returns Number of leafs.
954 * @param pVM The VM handle.
955 * @remark Intended for PATM.
956 */
957CPUMDECL(uint32_t) CPUMGetGuestCpuIdExtMax(PVM pVM)
958{
959 return RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdExt);
960}
961
962/**
963 * Gets a number of centaur CPUID leafs.
964 *
965 * @returns Number of leafs.
966 * @param pVM The VM handle.
967 * @remark Intended for PATM.
968 */
969CPUMDECL(uint32_t) CPUMGetGuestCpuIdCentaurMax(PVM pVM)
970{
971 return RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdCentaur);
972}
973
974/**
975 * Sets a CPUID feature bit.
976 *
977 * @param pVM The VM Handle.
978 * @param enmFeature The feature to set.
979 */
980CPUMDECL(void) CPUMSetGuestCpuIdFeature(PVM pVM, CPUMCPUIDFEATURE enmFeature)
981{
982 switch (enmFeature)
983 {
984 /*
985 * Set the APIC bit in both feature masks.
986 */
987 case CPUMCPUIDFEATURE_APIC:
988 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
989 pVM->cpum.s.aGuestCpuIdStd[1].edx |= X86_CPUID_FEATURE_EDX_APIC;
990 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
991 && pVM->cpum.s.aGuestCpuIdExt[1].edx)
992 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_APIC;
993 Log(("CPUMSetGuestCpuIdFeature: Enabled APIC\n"));
994 break;
995
996 /*
997 * Set the sysenter/sysexit bit in both feature masks.
998 * Assumes the caller knows what it's doing! (host must support these)
999 */
1000 case CPUMCPUIDFEATURE_SEP:
1001 {
1002 uint32_t ulEdx, ulDummy;
1003
1004 ASMCpuId(1, &ulDummy, &ulDummy, &ulDummy, &ulEdx);
1005 if (!(ulEdx & X86_CPUID_FEATURE_EDX_SEP))
1006 {
1007 AssertMsgFailed(("ERROR: Can't turn on SEP when the host doesn't support it!!\n"));
1008 return;
1009 }
1010
1011 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1012 pVM->cpum.s.aGuestCpuIdStd[1].edx |= X86_CPUID_FEATURE_EDX_SEP;
1013 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1014 && pVM->cpum.s.aGuestCpuIdExt[1].edx)
1015 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_SEP;
1016 Log(("CPUMSetGuestCpuIdFeature: Enabled sysenter/exit\n"));
1017 break;
1018 }
1019
1020 default:
1021 AssertMsgFailed(("enmFeature=%d\n", enmFeature));
1022 break;
1023 }
1024}
1025
1026/**
1027 * Clears a CPUID feature bit.
1028 *
1029 * @param pVM The VM Handle.
1030 * @param enmFeature The feature to clear.
1031 */
1032CPUMDECL(void) CPUMClearGuestCpuIdFeature(PVM pVM, CPUMCPUIDFEATURE enmFeature)
1033{
1034 switch (enmFeature)
1035 {
1036 /*
1037 * Set the APIC bit in both feature masks.
1038 */
1039 case CPUMCPUIDFEATURE_APIC:
1040 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1041 pVM->cpum.s.aGuestCpuIdStd[1].edx &= ~X86_CPUID_FEATURE_EDX_APIC;
1042 if (pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001)
1043 pVM->cpum.s.aGuestCpuIdExt[1].edx &= ~X86_CPUID_AMD_FEATURE_EDX_APIC;
1044 Log(("CPUMSetGuestCpuIdFeature: Disabled APIC\n"));
1045 break;
1046
1047 default:
1048 AssertMsgFailed(("enmFeature=%d\n", enmFeature));
1049 break;
1050 }
1051}
1052
1053
1054
1055CPUMDECL(int) CPUMSetGuestDR0(PVM pVM, RTGCUINTREG uDr0)
1056{
1057 pVM->cpum.s.Guest.dr0 = uDr0;
1058 return CPUMRecalcHyperDRx(pVM);
1059}
1060
1061CPUMDECL(int) CPUMSetGuestDR1(PVM pVM, RTGCUINTREG uDr1)
1062{
1063 pVM->cpum.s.Guest.dr1 = uDr1;
1064 return CPUMRecalcHyperDRx(pVM);
1065}
1066
1067CPUMDECL(int) CPUMSetGuestDR2(PVM pVM, RTGCUINTREG uDr2)
1068{
1069 pVM->cpum.s.Guest.dr2 = uDr2;
1070 return CPUMRecalcHyperDRx(pVM);
1071}
1072
1073CPUMDECL(int) CPUMSetGuestDR3(PVM pVM, RTGCUINTREG uDr3)
1074{
1075 pVM->cpum.s.Guest.dr3 = uDr3;
1076 return CPUMRecalcHyperDRx(pVM);
1077}
1078
1079CPUMDECL(int) CPUMSetGuestDR6(PVM pVM, RTGCUINTREG uDr6)
1080{
1081 pVM->cpum.s.Guest.dr6 = uDr6;
1082 return CPUMRecalcHyperDRx(pVM);
1083}
1084
1085CPUMDECL(int) CPUMSetGuestDR7(PVM pVM, RTGCUINTREG uDr7)
1086{
1087 pVM->cpum.s.Guest.dr7 = uDr7;
1088 return CPUMRecalcHyperDRx(pVM);
1089}
1090
1091/** @todo drx should be an array */
1092CPUMDECL(int) CPUMSetGuestDRx(PVM pVM, uint32_t iReg, uint32_t Value)
1093{
1094 switch (iReg)
1095 {
1096 case USE_REG_DR0:
1097 pVM->cpum.s.Guest.dr0 = Value;
1098 break;
1099 case USE_REG_DR1:
1100 pVM->cpum.s.Guest.dr1 = Value;
1101 break;
1102 case USE_REG_DR2:
1103 pVM->cpum.s.Guest.dr2 = Value;
1104 break;
1105 case USE_REG_DR3:
1106 pVM->cpum.s.Guest.dr3 = Value;
1107 break;
1108 case USE_REG_DR4:
1109 case USE_REG_DR6:
1110 pVM->cpum.s.Guest.dr6 = Value;
1111 break;
1112 case USE_REG_DR5:
1113 case USE_REG_DR7:
1114 pVM->cpum.s.Guest.dr7 = Value;
1115 break;
1116
1117 default:
1118 return VERR_INVALID_PARAMETER;
1119 }
1120 return CPUMRecalcHyperDRx(pVM);
1121}
1122
1123
1124/**
1125 * Recalculates the hypvervisor DRx register values based on
1126 * current guest registers and DBGF breakpoints.
1127 *
1128 * This is called whenever a guest DRx register is modified and when DBGF
1129 * sets a hardware breakpoint. In guest context this function will reload
1130 * any (hyper) DRx registers which comes out with a different value.
1131 *
1132 * @returns VINF_SUCCESS.
1133 * @param pVM The VM handle.
1134 */
1135CPUMDECL(int) CPUMRecalcHyperDRx(PVM pVM)
1136{
1137 /*
1138 * Compare the DR7s first.
1139 *
1140 * We only care about the enabled flags. The GE and LE flags are always
1141 * set and we don't care if the guest doesn't set them. GD is virtualized
1142 * when we dispatch #DB, we never enable it.
1143 */
1144 const RTGCUINTREG uDbgfDr7 = DBGFBpGetDR7(pVM);
1145#ifdef CPUM_VIRTUALIZE_DRX
1146 const RTGCUINTREG uGstDr7 = CPUMGetGuestDR7(pVM);
1147#else
1148 const RTGCUINTREG uGstDr7 = 0;
1149#endif
1150 if ((uGstDr7 | uDbgfDr7) & X86_DR7_ENABLED_MASK)
1151 {
1152 /*
1153 * Ok, something is enabled. Recalc each of the breakpoints.
1154 * Straight forward code, not optimized/minimized in any way.
1155 */
1156 RTGCUINTREG uNewDr7 = X86_DR7_GE | X86_DR7_LE | X86_DR7_MB1_MASK;
1157
1158 /* bp 0 */
1159 RTGCUINTREG uNewDr0;
1160 if (uDbgfDr7 & (X86_DR7_L0 | X86_DR7_G0))
1161 {
1162 uNewDr7 |= uDbgfDr7 & (X86_DR7_L0 | X86_DR7_G0 | X86_DR7_RW0_MASK | X86_DR7_LEN0_MASK);
1163 uNewDr0 = DBGFBpGetDR0(pVM);
1164 }
1165 else if (uGstDr7 & (X86_DR7_L0 | X86_DR7_G0))
1166 {
1167 uNewDr7 |= uGstDr7 & (X86_DR7_L0 | X86_DR7_G0 | X86_DR7_RW0_MASK | X86_DR7_LEN0_MASK);
1168 uNewDr0 = CPUMGetGuestDR0(pVM);
1169 }
1170 else
1171 uNewDr0 = pVM->cpum.s.Hyper.dr0;
1172
1173 /* bp 1 */
1174 RTGCUINTREG uNewDr1;
1175 if (uDbgfDr7 & (X86_DR7_L1 | X86_DR7_G1))
1176 {
1177 uNewDr7 |= uDbgfDr7 & (X86_DR7_L1 | X86_DR7_G1 | X86_DR7_RW1_MASK | X86_DR7_LEN1_MASK);
1178 uNewDr1 = DBGFBpGetDR1(pVM);
1179 }
1180 else if (uGstDr7 & (X86_DR7_L1 | X86_DR7_G1))
1181 {
1182 uNewDr7 |= uGstDr7 & (X86_DR7_L1 | X86_DR7_G1 | X86_DR7_RW1_MASK | X86_DR7_LEN1_MASK);
1183 uNewDr1 = CPUMGetGuestDR1(pVM);
1184 }
1185 else
1186 uNewDr1 = pVM->cpum.s.Hyper.dr1;
1187
1188 /* bp 2 */
1189 RTGCUINTREG uNewDr2;
1190 if (uDbgfDr7 & (X86_DR7_L2 | X86_DR7_G2))
1191 {
1192 uNewDr7 |= uDbgfDr7 & (X86_DR7_L2 | X86_DR7_G2 | X86_DR7_RW2_MASK | X86_DR7_LEN2_MASK);
1193 uNewDr2 = DBGFBpGetDR2(pVM);
1194 }
1195 else if (uGstDr7 & (X86_DR7_L2 | X86_DR7_G2))
1196 {
1197 uNewDr7 |= uGstDr7 & (X86_DR7_L2 | X86_DR7_G2 | X86_DR7_RW2_MASK | X86_DR7_LEN2_MASK);
1198 uNewDr2 = CPUMGetGuestDR2(pVM);
1199 }
1200 else
1201 uNewDr2 = pVM->cpum.s.Hyper.dr2;
1202
1203 /* bp 3 */
1204 RTGCUINTREG uNewDr3;
1205 if (uDbgfDr7 & (X86_DR7_L3 | X86_DR7_G3))
1206 {
1207 uNewDr7 |= uDbgfDr7 & (X86_DR7_L3 | X86_DR7_G3 | X86_DR7_RW3_MASK | X86_DR7_LEN3_MASK);
1208 uNewDr3 = DBGFBpGetDR3(pVM);
1209 }
1210 else if (uGstDr7 & (X86_DR7_L3 | X86_DR7_G3))
1211 {
1212 uNewDr7 |= uGstDr7 & (X86_DR7_L3 | X86_DR7_G3 | X86_DR7_RW3_MASK | X86_DR7_LEN3_MASK);
1213 uNewDr3 = CPUMGetGuestDR3(pVM);
1214 }
1215 else
1216 uNewDr3 = pVM->cpum.s.Hyper.dr3;
1217
1218 /*
1219 * Apply the updates.
1220 */
1221#ifdef IN_GC
1222 if (!(pVM->cpum.s.fUseFlags & CPUM_USE_DEBUG_REGS))
1223 {
1224 /** @todo save host DBx registers. */
1225 }
1226#endif
1227 pVM->cpum.s.fUseFlags |= CPUM_USE_DEBUG_REGS;
1228 if (uNewDr3 != pVM->cpum.s.Hyper.dr3)
1229 CPUMSetHyperDR3(pVM, uNewDr3);
1230 if (uNewDr2 != pVM->cpum.s.Hyper.dr2)
1231 CPUMSetHyperDR2(pVM, uNewDr2);
1232 if (uNewDr1 != pVM->cpum.s.Hyper.dr1)
1233 CPUMSetHyperDR1(pVM, uNewDr1);
1234 if (uNewDr0 != pVM->cpum.s.Hyper.dr0)
1235 CPUMSetHyperDR0(pVM, uNewDr0);
1236 if (uNewDr7 != pVM->cpum.s.Hyper.dr7)
1237 CPUMSetHyperDR7(pVM, uNewDr7);
1238 }
1239 else
1240 {
1241#ifdef IN_GC
1242 if (pVM->cpum.s.fUseFlags & CPUM_USE_DEBUG_REGS)
1243 {
1244 /** @todo restore host DBx registers. */
1245 }
1246#endif
1247 pVM->cpum.s.fUseFlags &= ~CPUM_USE_DEBUG_REGS;
1248 }
1249 Log2(("CPUMRecalcHyperDRx: fUseFlags=%#x %RGr %RGr %RGr %RGr %RGr %RGr\n",
1250 pVM->cpum.s.fUseFlags, pVM->cpum.s.Hyper.dr0, pVM->cpum.s.Hyper.dr1,
1251 pVM->cpum.s.Hyper.dr2, pVM->cpum.s.Hyper.dr3, pVM->cpum.s.Hyper.dr6,
1252 pVM->cpum.s.Hyper.dr7));
1253
1254 return VINF_SUCCESS;
1255}
1256
1257#ifndef IN_RING0 /** @todo I don't think we need this in R0, so move it to CPUMAll.cpp? */
1258
1259/**
1260 * Transforms the guest CPU state to raw-ring mode.
1261 *
1262 * This function will change the any of the cs and ss register with DPL=0 to DPL=1.
1263 *
1264 * @returns VBox status. (recompiler failure)
1265 * @param pVM VM handle.
1266 * @param pCtxCore The context core (for trap usage).
1267 * @see @ref pg_raw
1268 */
1269CPUMDECL(int) CPUMRawEnter(PVM pVM, PCPUMCTXCORE pCtxCore)
1270{
1271 Assert(!pVM->cpum.s.fRawEntered);
1272 if (!pCtxCore)
1273 pCtxCore = CPUMCTX2CORE(&pVM->cpum.s.Guest);
1274
1275 /*
1276 * Are we in Ring-0?
1277 */
1278 if ( pCtxCore->ss && (pCtxCore->ss & X86_SEL_RPL) == 0
1279 && !pCtxCore->eflags.Bits.u1VM)
1280 {
1281 /*
1282 * Enter execution mode.
1283 */
1284 PATMRawEnter(pVM, pCtxCore);
1285
1286 /*
1287 * Set CPL to Ring-1.
1288 */
1289 pCtxCore->ss |= 1;
1290 if (pCtxCore->cs && (pCtxCore->cs & X86_SEL_RPL) == 0)
1291 pCtxCore->cs |= 1;
1292 }
1293 else
1294 {
1295 AssertMsg((pCtxCore->ss & X86_SEL_RPL) >= 2 || pCtxCore->eflags.Bits.u1VM,
1296 ("ring-1 code not supported\n"));
1297 /*
1298 * PATM takes care of IOPL and IF flags for Ring-3 and Ring-2 code as well.
1299 */
1300 PATMRawEnter(pVM, pCtxCore);
1301 }
1302
1303 /*
1304 * Assert sanity.
1305 */
1306 AssertMsg((pCtxCore->eflags.u32 & X86_EFL_IF), ("X86_EFL_IF is clear\n"));
1307 AssertReleaseMsg( pCtxCore->eflags.Bits.u2IOPL < (unsigned)(pCtxCore->ss & X86_SEL_RPL)
1308 || pCtxCore->eflags.Bits.u1VM,
1309 ("X86_EFL_IOPL=%d CPL=%d\n", pCtxCore->eflags.Bits.u2IOPL, pCtxCore->ss & X86_SEL_RPL));
1310 Assert((pVM->cpum.s.Guest.cr0 & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE)) == (X86_CR0_PG | X86_CR0_PE | X86_CR0_WP));
1311 pCtxCore->eflags.u32 |= X86_EFL_IF; /* paranoia */
1312
1313 pVM->cpum.s.fRawEntered = true;
1314 return VINF_SUCCESS;
1315}
1316
1317
1318/**
1319 * Transforms the guest CPU state from raw-ring mode to correct values.
1320 *
1321 * This function will change any selector registers with DPL=1 to DPL=0.
1322 *
1323 * @returns Adjusted rc.
1324 * @param pVM VM handle.
1325 * @param rc Raw mode return code
1326 * @param pCtxCore The context core (for trap usage).
1327 * @see @ref pg_raw
1328 */
1329CPUMDECL(int) CPUMRawLeave(PVM pVM, PCPUMCTXCORE pCtxCore, int rc)
1330{
1331 /*
1332 * Don't leave if we've already left (in GC).
1333 */
1334 Assert(pVM->cpum.s.fRawEntered);
1335 if (!pVM->cpum.s.fRawEntered)
1336 return rc;
1337 pVM->cpum.s.fRawEntered = false;
1338
1339 PCPUMCTX pCtx = &pVM->cpum.s.Guest;
1340 if (!pCtxCore)
1341 pCtxCore = CPUMCTX2CORE(pCtx);
1342 Assert(pCtxCore->eflags.Bits.u1VM || (pCtxCore->ss & X86_SEL_RPL));
1343 AssertMsg(pCtxCore->eflags.Bits.u1VM || pCtxCore->eflags.Bits.u2IOPL < (unsigned)(pCtxCore->ss & X86_SEL_RPL),
1344 ("X86_EFL_IOPL=%d CPL=%d\n", pCtxCore->eflags.Bits.u2IOPL, pCtxCore->ss & X86_SEL_RPL));
1345
1346 /*
1347 * Are we executing in raw ring-1?
1348 */
1349 if ( (pCtxCore->ss & X86_SEL_RPL) == 1
1350 && !pCtxCore->eflags.Bits.u1VM)
1351 {
1352 /*
1353 * Leave execution mode.
1354 */
1355 PATMRawLeave(pVM, pCtxCore, rc);
1356 /* Not quite sure if this is really required, but shouldn't harm (too much anyways). */
1357 /** @todo See what happens if we remove this. */
1358 if ((pCtxCore->ds & X86_SEL_RPL) == 1)
1359 pCtxCore->ds &= ~X86_SEL_RPL;
1360 if ((pCtxCore->es & X86_SEL_RPL) == 1)
1361 pCtxCore->es &= ~X86_SEL_RPL;
1362 if ((pCtxCore->fs & X86_SEL_RPL) == 1)
1363 pCtxCore->fs &= ~X86_SEL_RPL;
1364 if ((pCtxCore->gs & X86_SEL_RPL) == 1)
1365 pCtxCore->gs &= ~X86_SEL_RPL;
1366
1367 /*
1368 * Ring-1 selector => Ring-0.
1369 */
1370 pCtxCore->ss &= ~X86_SEL_RPL;
1371 if ((pCtxCore->cs & X86_SEL_RPL) == 1)
1372 pCtxCore->cs &= ~X86_SEL_RPL;
1373 }
1374 else
1375 {
1376 /*
1377 * PATM is taking care of the IOPL and IF flags for us.
1378 */
1379 PATMRawLeave(pVM, pCtxCore, rc);
1380 if (!pCtxCore->eflags.Bits.u1VM)
1381 {
1382 /** @todo See what happens if we remove this. */
1383 if ((pCtxCore->ds & X86_SEL_RPL) == 1)
1384 pCtxCore->ds &= ~X86_SEL_RPL;
1385 if ((pCtxCore->es & X86_SEL_RPL) == 1)
1386 pCtxCore->es &= ~X86_SEL_RPL;
1387 if ((pCtxCore->fs & X86_SEL_RPL) == 1)
1388 pCtxCore->fs &= ~X86_SEL_RPL;
1389 if ((pCtxCore->gs & X86_SEL_RPL) == 1)
1390 pCtxCore->gs &= ~X86_SEL_RPL;
1391 }
1392 }
1393
1394 return rc;
1395}
1396
1397/**
1398 * Updates the EFLAGS while we're in raw-mode.
1399 *
1400 * @param pVM The VM handle.
1401 * @param pCtxCore The context core.
1402 * @param eflags The new EFLAGS value.
1403 */
1404CPUMDECL(void) CPUMRawSetEFlags(PVM pVM, PCPUMCTXCORE pCtxCore, uint32_t eflags)
1405{
1406 if (!pVM->cpum.s.fRawEntered)
1407 {
1408 pCtxCore->eflags.u32 = eflags;
1409 return;
1410 }
1411 PATMRawSetEFlags(pVM, pCtxCore, eflags);
1412}
1413
1414#endif /* !IN_RING0 */
1415
1416/**
1417 * Gets the EFLAGS while we're in raw-mode.
1418 *
1419 * @returns The eflags.
1420 * @param pVM The VM handle.
1421 * @param pCtxCore The context core.
1422 */
1423CPUMDECL(uint32_t) CPUMRawGetEFlags(PVM pVM, PCPUMCTXCORE pCtxCore)
1424{
1425#ifdef IN_RING0
1426 return pCtxCore->eflags.u32;
1427#else
1428 if (!pVM->cpum.s.fRawEntered)
1429 return pCtxCore->eflags.u32;
1430 return PATMRawGetEFlags(pVM, pCtxCore);
1431#endif
1432}
1433
1434
1435
1436
1437/**
1438 * Gets and resets the changed flags (CPUM_CHANGED_*).
1439 * Only REM should call this function.
1440 *
1441 * @returns The changed flags.
1442 * @param pVM The VM handle.
1443 */
1444CPUMDECL(unsigned) CPUMGetAndClearChangedFlagsREM(PVM pVM)
1445{
1446 unsigned fFlags = pVM->cpum.s.fChanged;
1447 pVM->cpum.s.fChanged = 0;
1448 /** @todo change the switcher to use the fChanged flags. */
1449 if (pVM->cpum.s.fUseFlags & CPUM_USED_FPU_SINCE_REM)
1450 {
1451 fFlags |= CPUM_CHANGED_FPU_REM;
1452 pVM->cpum.s.fUseFlags &= ~CPUM_USED_FPU_SINCE_REM;
1453 }
1454 return fFlags;
1455}
1456
1457/**
1458 * Sets the specified changed flags (CPUM_CHANGED_*).
1459 *
1460 * @param pVM The VM handle.
1461 */
1462CPUMDECL(void) CPUMSetChangedFlags(PVM pVM, uint32_t fChangedFlags)
1463{
1464 pVM->cpum.s.fChanged |= fChangedFlags;
1465}
1466
1467/**
1468 * Checks if the CPU supports the FXSAVE and FXRSTOR instruction.
1469 * @returns true if supported.
1470 * @returns false if not supported.
1471 * @param pVM The VM handle.
1472 */
1473CPUMDECL(bool) CPUMSupportsFXSR(PVM pVM)
1474{
1475 return pVM->cpum.s.CPUFeatures.edx.u1FXSR != 0;
1476}
1477
1478
1479/**
1480 * Checks if the host OS uses the SYSENTER / SYSEXIT instructions.
1481 * @returns true if used.
1482 * @returns false if not used.
1483 * @param pVM The VM handle.
1484 */
1485CPUMDECL(bool) CPUMIsHostUsingSysEnter(PVM pVM)
1486{
1487 return (pVM->cpum.s.fUseFlags & CPUM_USE_SYSENTER) != 0;
1488}
1489
1490
1491/**
1492 * Checks if the host OS uses the SYSCALL / SYSRET instructions.
1493 * @returns true if used.
1494 * @returns false if not used.
1495 * @param pVM The VM handle.
1496 */
1497CPUMDECL(bool) CPUMIsHostUsingSysCall(PVM pVM)
1498{
1499 return (pVM->cpum.s.fUseFlags & CPUM_USE_SYSCALL) != 0;
1500}
1501
1502/**
1503 * Lazily sync in the FPU/XMM state
1504 *
1505 * @returns VBox status code.
1506 * @param pVM VM handle.
1507 */
1508CPUMDECL(int) CPUMHandleLazyFPU(PVM pVM)
1509{
1510 return CPUMHandleLazyFPUAsm(&pVM->cpum.s);
1511}
1512
1513/**
1514 * Restore host FPU/XMM state
1515 *
1516 * @returns VBox status code.
1517 * @param pVM VM handle.
1518 */
1519CPUMDECL(int) CPUMRestoreHostFPUState(PVM pVM)
1520{
1521 Assert(pVM->cpum.s.CPUFeatures.edx.u1FXSR);
1522 return CPUMRestoreHostFPUStateAsm(&pVM->cpum.s);
1523}
1524
1525/**
1526 * Checks if we activated the FPU/XMM state of the guest OS
1527 * @returns true if we did.
1528 * @returns false if not.
1529 * @param pVM The VM handle.
1530 */
1531CPUMDECL(bool) CPUMIsGuestFPUStateActive(PVM pVM)
1532{
1533 return (pVM->cpum.s.fUseFlags & CPUM_USED_FPU) != 0;
1534}
1535
1536/**
1537 * Deactivate the FPU/XMM state of the guest OS
1538 * @param pVM The VM handle.
1539 */
1540CPUMDECL(void) CPUMDeactivateGuestFPUState(PVM pVM)
1541{
1542 pVM->cpum.s.fUseFlags &= ~CPUM_USED_FPU;
1543}
1544
1545/**
1546 * Checks if the hidden selector registers are valid
1547 * @returns true if they are.
1548 * @returns false if not.
1549 * @param pVM The VM handle.
1550 */
1551CPUMDECL(bool) CPUMAreHiddenSelRegsValid(PVM pVM)
1552{
1553 return !!pVM->cpum.s.fValidHiddenSelRegs; /** @todo change fValidHiddenSelRegs to bool! */
1554}
1555
1556/**
1557 * Checks if the hidden selector registers are valid
1558 * @param pVM The VM handle.
1559 * @param fValid Valid or not
1560 */
1561CPUMDECL(void) CPUMSetHiddenSelRegsValid(PVM pVM, bool fValid)
1562{
1563 pVM->cpum.s.fValidHiddenSelRegs = fValid;
1564}
1565
1566/**
1567 * Get the current privilege level of the guest.
1568 *
1569 * @returns cpl
1570 * @param pVM VM Handle.
1571 * @param pRegFrame Trap register frame.
1572 */
1573CPUMDECL(uint32_t) CPUMGetGuestCPL(PVM pVM, PCPUMCTXCORE pCtxCore)
1574{
1575 uint32_t cpl;
1576
1577 if (CPUMAreHiddenSelRegsValid(pVM))
1578 cpl = pCtxCore->ssHid.Attr.n.u2Dpl;
1579 else if (RT_LIKELY(pVM->cpum.s.Guest.cr0 & X86_CR0_PE))
1580 {
1581 if (RT_LIKELY(!pCtxCore->eflags.Bits.u1VM))
1582 {
1583 cpl = (pCtxCore->ss & X86_SEL_RPL);
1584#ifndef IN_RING0
1585 if (cpl == 1)
1586 cpl = 0;
1587#endif
1588 }
1589 else
1590 cpl = 3;
1591 }
1592 else
1593 cpl = 0; /* real mode; cpl is zero */
1594
1595 return cpl;
1596}
1597
1598
1599/**
1600 * Gets the current guest CPU mode.
1601 *
1602 * If paging mode is what you need, check out PGMGetGuestMode().
1603 *
1604 * @returns The CPU mode.
1605 * @param pVM The VM handle.
1606 */
1607CPUMDECL(CPUMMODE) CPUMGetGuestMode(PVM pVM)
1608{
1609 CPUMMODE enmMode;
1610 if (!(pVM->cpum.s.Guest.cr0 & X86_CR0_PE))
1611 enmMode = CPUMMODE_REAL;
1612 else //GUEST64 if (!(pVM->cpum.s.Guest.efer & MSR_K6_EFER_LMA)
1613 enmMode = CPUMMODE_PROTECTED;
1614//GUEST64 else
1615//GUEST64 enmMode = CPUMMODE_LONG;
1616
1617 return enmMode;
1618}
1619
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