VirtualBox

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

Last change on this file since 7689 was 7650, checked in by vboxsync, 17 years ago

Missed one

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 43.3 KB
Line 
1/* $Id: CPUMAllRegs.cpp 7650 2008-03-31 11:33:29Z 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 (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_CPUM
23#include <VBox/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 if (!(ASMCpuId_EDX(1) & X86_CPUID_FEATURE_EDX_SEP))
1003 {
1004 AssertMsgFailed(("ERROR: Can't turn on SEP when the host doesn't support it!!\n"));
1005 return;
1006 }
1007
1008 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1009 pVM->cpum.s.aGuestCpuIdStd[1].edx |= X86_CPUID_FEATURE_EDX_SEP;
1010 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1011 && pVM->cpum.s.aGuestCpuIdExt[1].edx)
1012 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_SEP;
1013 Log(("CPUMSetGuestCpuIdFeature: Enabled sysenter/exit\n"));
1014 break;
1015 }
1016
1017 /*
1018 * Set the PAE bit in both feature masks.
1019 * Assumes the caller knows what it's doing! (host must support these)
1020 */
1021 case CPUMCPUIDFEATURE_PAE:
1022 {
1023 if (!(ASMCpuId_EDX(1) & X86_CPUID_FEATURE_EDX_PAE))
1024 {
1025 AssertMsgFailed(("ERROR: Can't turn on PAE when the host doesn't support it!!\n"));
1026 return;
1027 }
1028
1029 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1030 pVM->cpum.s.aGuestCpuIdStd[1].edx |= X86_CPUID_FEATURE_EDX_PAE;
1031 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1032 && pVM->cpum.s.aGuestCpuIdExt[1].edx)
1033 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_PAE;
1034 Log(("CPUMSetGuestCpuIdFeature: Enabled PAE\n"));
1035 break;
1036 }
1037
1038 /*
1039 * Set the LONG MODE bit in the extended feature mask.
1040 * Assumes the caller knows what it's doing! (host must support these)
1041 */
1042 case CPUMCPUIDFEATURE_LONG_MODE:
1043 {
1044 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax < 0x80000001
1045 || !(ASMCpuId_EDX(0x80000001) & X86_CPUID_AMD_FEATURE_EDX_LONG_MODE))
1046 {
1047 AssertMsgFailed(("ERROR: Can't turn on LONG MODE when the host doesn't support it!!\n"));
1048 return;
1049 }
1050
1051 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
1052 && pVM->cpum.s.aGuestCpuIdExt[1].edx)
1053 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_LONG_MODE;
1054 Log(("CPUMSetGuestCpuIdFeature: Enabled LONG MODE\n"));
1055 break;
1056 }
1057
1058 default:
1059 AssertMsgFailed(("enmFeature=%d\n", enmFeature));
1060 break;
1061 }
1062}
1063
1064/**
1065 * Clears a CPUID feature bit.
1066 *
1067 * @param pVM The VM Handle.
1068 * @param enmFeature The feature to clear.
1069 */
1070CPUMDECL(void) CPUMClearGuestCpuIdFeature(PVM pVM, CPUMCPUIDFEATURE enmFeature)
1071{
1072 switch (enmFeature)
1073 {
1074 /*
1075 * Set the APIC bit in both feature masks.
1076 */
1077 case CPUMCPUIDFEATURE_APIC:
1078 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
1079 pVM->cpum.s.aGuestCpuIdStd[1].edx &= ~X86_CPUID_FEATURE_EDX_APIC;
1080 if (pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001)
1081 pVM->cpum.s.aGuestCpuIdExt[1].edx &= ~X86_CPUID_AMD_FEATURE_EDX_APIC;
1082 Log(("CPUMSetGuestCpuIdFeature: Disabled APIC\n"));
1083 break;
1084
1085 default:
1086 AssertMsgFailed(("enmFeature=%d\n", enmFeature));
1087 break;
1088 }
1089}
1090
1091
1092
1093CPUMDECL(int) CPUMSetGuestDR0(PVM pVM, RTGCUINTREG uDr0)
1094{
1095 pVM->cpum.s.Guest.dr0 = uDr0;
1096 return CPUMRecalcHyperDRx(pVM);
1097}
1098
1099CPUMDECL(int) CPUMSetGuestDR1(PVM pVM, RTGCUINTREG uDr1)
1100{
1101 pVM->cpum.s.Guest.dr1 = uDr1;
1102 return CPUMRecalcHyperDRx(pVM);
1103}
1104
1105CPUMDECL(int) CPUMSetGuestDR2(PVM pVM, RTGCUINTREG uDr2)
1106{
1107 pVM->cpum.s.Guest.dr2 = uDr2;
1108 return CPUMRecalcHyperDRx(pVM);
1109}
1110
1111CPUMDECL(int) CPUMSetGuestDR3(PVM pVM, RTGCUINTREG uDr3)
1112{
1113 pVM->cpum.s.Guest.dr3 = uDr3;
1114 return CPUMRecalcHyperDRx(pVM);
1115}
1116
1117CPUMDECL(int) CPUMSetGuestDR6(PVM pVM, RTGCUINTREG uDr6)
1118{
1119 pVM->cpum.s.Guest.dr6 = uDr6;
1120 return CPUMRecalcHyperDRx(pVM);
1121}
1122
1123CPUMDECL(int) CPUMSetGuestDR7(PVM pVM, RTGCUINTREG uDr7)
1124{
1125 pVM->cpum.s.Guest.dr7 = uDr7;
1126 return CPUMRecalcHyperDRx(pVM);
1127}
1128
1129/** @todo drx should be an array */
1130CPUMDECL(int) CPUMSetGuestDRx(PVM pVM, uint32_t iReg, uint32_t Value)
1131{
1132 switch (iReg)
1133 {
1134 case USE_REG_DR0:
1135 pVM->cpum.s.Guest.dr0 = Value;
1136 break;
1137 case USE_REG_DR1:
1138 pVM->cpum.s.Guest.dr1 = Value;
1139 break;
1140 case USE_REG_DR2:
1141 pVM->cpum.s.Guest.dr2 = Value;
1142 break;
1143 case USE_REG_DR3:
1144 pVM->cpum.s.Guest.dr3 = Value;
1145 break;
1146 case USE_REG_DR4:
1147 case USE_REG_DR6:
1148 pVM->cpum.s.Guest.dr6 = Value;
1149 break;
1150 case USE_REG_DR5:
1151 case USE_REG_DR7:
1152 pVM->cpum.s.Guest.dr7 = Value;
1153 break;
1154
1155 default:
1156 return VERR_INVALID_PARAMETER;
1157 }
1158 return CPUMRecalcHyperDRx(pVM);
1159}
1160
1161
1162/**
1163 * Recalculates the hypvervisor DRx register values based on
1164 * current guest registers and DBGF breakpoints.
1165 *
1166 * This is called whenever a guest DRx register is modified and when DBGF
1167 * sets a hardware breakpoint. In guest context this function will reload
1168 * any (hyper) DRx registers which comes out with a different value.
1169 *
1170 * @returns VINF_SUCCESS.
1171 * @param pVM The VM handle.
1172 */
1173CPUMDECL(int) CPUMRecalcHyperDRx(PVM pVM)
1174{
1175 /*
1176 * Compare the DR7s first.
1177 *
1178 * We only care about the enabled flags. The GE and LE flags are always
1179 * set and we don't care if the guest doesn't set them. GD is virtualized
1180 * when we dispatch #DB, we never enable it.
1181 */
1182 const RTGCUINTREG uDbgfDr7 = DBGFBpGetDR7(pVM);
1183#ifdef CPUM_VIRTUALIZE_DRX
1184 const RTGCUINTREG uGstDr7 = CPUMGetGuestDR7(pVM);
1185#else
1186 const RTGCUINTREG uGstDr7 = 0;
1187#endif
1188 if ((uGstDr7 | uDbgfDr7) & X86_DR7_ENABLED_MASK)
1189 {
1190 /*
1191 * Ok, something is enabled. Recalc each of the breakpoints.
1192 * Straight forward code, not optimized/minimized in any way.
1193 */
1194 RTGCUINTREG uNewDr7 = X86_DR7_GE | X86_DR7_LE | X86_DR7_MB1_MASK;
1195
1196 /* bp 0 */
1197 RTGCUINTREG uNewDr0;
1198 if (uDbgfDr7 & (X86_DR7_L0 | X86_DR7_G0))
1199 {
1200 uNewDr7 |= uDbgfDr7 & (X86_DR7_L0 | X86_DR7_G0 | X86_DR7_RW0_MASK | X86_DR7_LEN0_MASK);
1201 uNewDr0 = DBGFBpGetDR0(pVM);
1202 }
1203 else if (uGstDr7 & (X86_DR7_L0 | X86_DR7_G0))
1204 {
1205 uNewDr7 |= uGstDr7 & (X86_DR7_L0 | X86_DR7_G0 | X86_DR7_RW0_MASK | X86_DR7_LEN0_MASK);
1206 uNewDr0 = CPUMGetGuestDR0(pVM);
1207 }
1208 else
1209 uNewDr0 = pVM->cpum.s.Hyper.dr0;
1210
1211 /* bp 1 */
1212 RTGCUINTREG uNewDr1;
1213 if (uDbgfDr7 & (X86_DR7_L1 | X86_DR7_G1))
1214 {
1215 uNewDr7 |= uDbgfDr7 & (X86_DR7_L1 | X86_DR7_G1 | X86_DR7_RW1_MASK | X86_DR7_LEN1_MASK);
1216 uNewDr1 = DBGFBpGetDR1(pVM);
1217 }
1218 else if (uGstDr7 & (X86_DR7_L1 | X86_DR7_G1))
1219 {
1220 uNewDr7 |= uGstDr7 & (X86_DR7_L1 | X86_DR7_G1 | X86_DR7_RW1_MASK | X86_DR7_LEN1_MASK);
1221 uNewDr1 = CPUMGetGuestDR1(pVM);
1222 }
1223 else
1224 uNewDr1 = pVM->cpum.s.Hyper.dr1;
1225
1226 /* bp 2 */
1227 RTGCUINTREG uNewDr2;
1228 if (uDbgfDr7 & (X86_DR7_L2 | X86_DR7_G2))
1229 {
1230 uNewDr7 |= uDbgfDr7 & (X86_DR7_L2 | X86_DR7_G2 | X86_DR7_RW2_MASK | X86_DR7_LEN2_MASK);
1231 uNewDr2 = DBGFBpGetDR2(pVM);
1232 }
1233 else if (uGstDr7 & (X86_DR7_L2 | X86_DR7_G2))
1234 {
1235 uNewDr7 |= uGstDr7 & (X86_DR7_L2 | X86_DR7_G2 | X86_DR7_RW2_MASK | X86_DR7_LEN2_MASK);
1236 uNewDr2 = CPUMGetGuestDR2(pVM);
1237 }
1238 else
1239 uNewDr2 = pVM->cpum.s.Hyper.dr2;
1240
1241 /* bp 3 */
1242 RTGCUINTREG uNewDr3;
1243 if (uDbgfDr7 & (X86_DR7_L3 | X86_DR7_G3))
1244 {
1245 uNewDr7 |= uDbgfDr7 & (X86_DR7_L3 | X86_DR7_G3 | X86_DR7_RW3_MASK | X86_DR7_LEN3_MASK);
1246 uNewDr3 = DBGFBpGetDR3(pVM);
1247 }
1248 else if (uGstDr7 & (X86_DR7_L3 | X86_DR7_G3))
1249 {
1250 uNewDr7 |= uGstDr7 & (X86_DR7_L3 | X86_DR7_G3 | X86_DR7_RW3_MASK | X86_DR7_LEN3_MASK);
1251 uNewDr3 = CPUMGetGuestDR3(pVM);
1252 }
1253 else
1254 uNewDr3 = pVM->cpum.s.Hyper.dr3;
1255
1256 /*
1257 * Apply the updates.
1258 */
1259#ifdef IN_GC
1260 if (!(pVM->cpum.s.fUseFlags & CPUM_USE_DEBUG_REGS))
1261 {
1262 /** @todo save host DBx registers. */
1263 }
1264#endif
1265 pVM->cpum.s.fUseFlags |= CPUM_USE_DEBUG_REGS;
1266 if (uNewDr3 != pVM->cpum.s.Hyper.dr3)
1267 CPUMSetHyperDR3(pVM, uNewDr3);
1268 if (uNewDr2 != pVM->cpum.s.Hyper.dr2)
1269 CPUMSetHyperDR2(pVM, uNewDr2);
1270 if (uNewDr1 != pVM->cpum.s.Hyper.dr1)
1271 CPUMSetHyperDR1(pVM, uNewDr1);
1272 if (uNewDr0 != pVM->cpum.s.Hyper.dr0)
1273 CPUMSetHyperDR0(pVM, uNewDr0);
1274 if (uNewDr7 != pVM->cpum.s.Hyper.dr7)
1275 CPUMSetHyperDR7(pVM, uNewDr7);
1276 }
1277 else
1278 {
1279#ifdef IN_GC
1280 if (pVM->cpum.s.fUseFlags & CPUM_USE_DEBUG_REGS)
1281 {
1282 /** @todo restore host DBx registers. */
1283 }
1284#endif
1285 pVM->cpum.s.fUseFlags &= ~CPUM_USE_DEBUG_REGS;
1286 }
1287 Log2(("CPUMRecalcHyperDRx: fUseFlags=%#x %RGr %RGr %RGr %RGr %RGr %RGr\n",
1288 pVM->cpum.s.fUseFlags, pVM->cpum.s.Hyper.dr0, pVM->cpum.s.Hyper.dr1,
1289 pVM->cpum.s.Hyper.dr2, pVM->cpum.s.Hyper.dr3, pVM->cpum.s.Hyper.dr6,
1290 pVM->cpum.s.Hyper.dr7));
1291
1292 return VINF_SUCCESS;
1293}
1294
1295#ifndef IN_RING0 /** @todo I don't think we need this in R0, so move it to CPUMAll.cpp? */
1296
1297/**
1298 * Transforms the guest CPU state to raw-ring mode.
1299 *
1300 * This function will change the any of the cs and ss register with DPL=0 to DPL=1.
1301 *
1302 * @returns VBox status. (recompiler failure)
1303 * @param pVM VM handle.
1304 * @param pCtxCore The context core (for trap usage).
1305 * @see @ref pg_raw
1306 */
1307CPUMDECL(int) CPUMRawEnter(PVM pVM, PCPUMCTXCORE pCtxCore)
1308{
1309 Assert(!pVM->cpum.s.fRawEntered);
1310 if (!pCtxCore)
1311 pCtxCore = CPUMCTX2CORE(&pVM->cpum.s.Guest);
1312
1313 /*
1314 * Are we in Ring-0?
1315 */
1316 if ( pCtxCore->ss && (pCtxCore->ss & X86_SEL_RPL) == 0
1317 && !pCtxCore->eflags.Bits.u1VM)
1318 {
1319 /*
1320 * Enter execution mode.
1321 */
1322 PATMRawEnter(pVM, pCtxCore);
1323
1324 /*
1325 * Set CPL to Ring-1.
1326 */
1327 pCtxCore->ss |= 1;
1328 if (pCtxCore->cs && (pCtxCore->cs & X86_SEL_RPL) == 0)
1329 pCtxCore->cs |= 1;
1330 }
1331 else
1332 {
1333 AssertMsg((pCtxCore->ss & X86_SEL_RPL) >= 2 || pCtxCore->eflags.Bits.u1VM,
1334 ("ring-1 code not supported\n"));
1335 /*
1336 * PATM takes care of IOPL and IF flags for Ring-3 and Ring-2 code as well.
1337 */
1338 PATMRawEnter(pVM, pCtxCore);
1339 }
1340
1341 /*
1342 * Assert sanity.
1343 */
1344 AssertMsg((pCtxCore->eflags.u32 & X86_EFL_IF), ("X86_EFL_IF is clear\n"));
1345 AssertReleaseMsg( pCtxCore->eflags.Bits.u2IOPL < (unsigned)(pCtxCore->ss & X86_SEL_RPL)
1346 || pCtxCore->eflags.Bits.u1VM,
1347 ("X86_EFL_IOPL=%d CPL=%d\n", pCtxCore->eflags.Bits.u2IOPL, pCtxCore->ss & X86_SEL_RPL));
1348 Assert((pVM->cpum.s.Guest.cr0 & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE)) == (X86_CR0_PG | X86_CR0_PE | X86_CR0_WP));
1349 pCtxCore->eflags.u32 |= X86_EFL_IF; /* paranoia */
1350
1351 pVM->cpum.s.fRawEntered = true;
1352 return VINF_SUCCESS;
1353}
1354
1355
1356/**
1357 * Transforms the guest CPU state from raw-ring mode to correct values.
1358 *
1359 * This function will change any selector registers with DPL=1 to DPL=0.
1360 *
1361 * @returns Adjusted rc.
1362 * @param pVM VM handle.
1363 * @param rc Raw mode return code
1364 * @param pCtxCore The context core (for trap usage).
1365 * @see @ref pg_raw
1366 */
1367CPUMDECL(int) CPUMRawLeave(PVM pVM, PCPUMCTXCORE pCtxCore, int rc)
1368{
1369 /*
1370 * Don't leave if we've already left (in GC).
1371 */
1372 Assert(pVM->cpum.s.fRawEntered);
1373 if (!pVM->cpum.s.fRawEntered)
1374 return rc;
1375 pVM->cpum.s.fRawEntered = false;
1376
1377 PCPUMCTX pCtx = &pVM->cpum.s.Guest;
1378 if (!pCtxCore)
1379 pCtxCore = CPUMCTX2CORE(pCtx);
1380 Assert(pCtxCore->eflags.Bits.u1VM || (pCtxCore->ss & X86_SEL_RPL));
1381 AssertMsg(pCtxCore->eflags.Bits.u1VM || pCtxCore->eflags.Bits.u2IOPL < (unsigned)(pCtxCore->ss & X86_SEL_RPL),
1382 ("X86_EFL_IOPL=%d CPL=%d\n", pCtxCore->eflags.Bits.u2IOPL, pCtxCore->ss & X86_SEL_RPL));
1383
1384 /*
1385 * Are we executing in raw ring-1?
1386 */
1387 if ( (pCtxCore->ss & X86_SEL_RPL) == 1
1388 && !pCtxCore->eflags.Bits.u1VM)
1389 {
1390 /*
1391 * Leave execution mode.
1392 */
1393 PATMRawLeave(pVM, pCtxCore, rc);
1394 /* Not quite sure if this is really required, but shouldn't harm (too much anyways). */
1395 /** @todo See what happens if we remove this. */
1396 if ((pCtxCore->ds & X86_SEL_RPL) == 1)
1397 pCtxCore->ds &= ~X86_SEL_RPL;
1398 if ((pCtxCore->es & X86_SEL_RPL) == 1)
1399 pCtxCore->es &= ~X86_SEL_RPL;
1400 if ((pCtxCore->fs & X86_SEL_RPL) == 1)
1401 pCtxCore->fs &= ~X86_SEL_RPL;
1402 if ((pCtxCore->gs & X86_SEL_RPL) == 1)
1403 pCtxCore->gs &= ~X86_SEL_RPL;
1404
1405 /*
1406 * Ring-1 selector => Ring-0.
1407 */
1408 pCtxCore->ss &= ~X86_SEL_RPL;
1409 if ((pCtxCore->cs & X86_SEL_RPL) == 1)
1410 pCtxCore->cs &= ~X86_SEL_RPL;
1411 }
1412 else
1413 {
1414 /*
1415 * PATM is taking care of the IOPL and IF flags for us.
1416 */
1417 PATMRawLeave(pVM, pCtxCore, rc);
1418 if (!pCtxCore->eflags.Bits.u1VM)
1419 {
1420 /** @todo See what happens if we remove this. */
1421 if ((pCtxCore->ds & X86_SEL_RPL) == 1)
1422 pCtxCore->ds &= ~X86_SEL_RPL;
1423 if ((pCtxCore->es & X86_SEL_RPL) == 1)
1424 pCtxCore->es &= ~X86_SEL_RPL;
1425 if ((pCtxCore->fs & X86_SEL_RPL) == 1)
1426 pCtxCore->fs &= ~X86_SEL_RPL;
1427 if ((pCtxCore->gs & X86_SEL_RPL) == 1)
1428 pCtxCore->gs &= ~X86_SEL_RPL;
1429 }
1430 }
1431
1432 return rc;
1433}
1434
1435/**
1436 * Updates the EFLAGS while we're in raw-mode.
1437 *
1438 * @param pVM The VM handle.
1439 * @param pCtxCore The context core.
1440 * @param eflags The new EFLAGS value.
1441 */
1442CPUMDECL(void) CPUMRawSetEFlags(PVM pVM, PCPUMCTXCORE pCtxCore, uint32_t eflags)
1443{
1444 if (!pVM->cpum.s.fRawEntered)
1445 {
1446 pCtxCore->eflags.u32 = eflags;
1447 return;
1448 }
1449 PATMRawSetEFlags(pVM, pCtxCore, eflags);
1450}
1451
1452#endif /* !IN_RING0 */
1453
1454/**
1455 * Gets the EFLAGS while we're in raw-mode.
1456 *
1457 * @returns The eflags.
1458 * @param pVM The VM handle.
1459 * @param pCtxCore The context core.
1460 */
1461CPUMDECL(uint32_t) CPUMRawGetEFlags(PVM pVM, PCPUMCTXCORE pCtxCore)
1462{
1463#ifdef IN_RING0
1464 return pCtxCore->eflags.u32;
1465#else
1466 if (!pVM->cpum.s.fRawEntered)
1467 return pCtxCore->eflags.u32;
1468 return PATMRawGetEFlags(pVM, pCtxCore);
1469#endif
1470}
1471
1472
1473
1474
1475/**
1476 * Gets and resets the changed flags (CPUM_CHANGED_*).
1477 * Only REM should call this function.
1478 *
1479 * @returns The changed flags.
1480 * @param pVM The VM handle.
1481 */
1482CPUMDECL(unsigned) CPUMGetAndClearChangedFlagsREM(PVM pVM)
1483{
1484 unsigned fFlags = pVM->cpum.s.fChanged;
1485 pVM->cpum.s.fChanged = 0;
1486 /** @todo change the switcher to use the fChanged flags. */
1487 if (pVM->cpum.s.fUseFlags & CPUM_USED_FPU_SINCE_REM)
1488 {
1489 fFlags |= CPUM_CHANGED_FPU_REM;
1490 pVM->cpum.s.fUseFlags &= ~CPUM_USED_FPU_SINCE_REM;
1491 }
1492 return fFlags;
1493}
1494
1495/**
1496 * Sets the specified changed flags (CPUM_CHANGED_*).
1497 *
1498 * @param pVM The VM handle.
1499 */
1500CPUMDECL(void) CPUMSetChangedFlags(PVM pVM, uint32_t fChangedFlags)
1501{
1502 pVM->cpum.s.fChanged |= fChangedFlags;
1503}
1504
1505/**
1506 * Checks if the CPU supports the FXSAVE and FXRSTOR instruction.
1507 * @returns true if supported.
1508 * @returns false if not supported.
1509 * @param pVM The VM handle.
1510 */
1511CPUMDECL(bool) CPUMSupportsFXSR(PVM pVM)
1512{
1513 return pVM->cpum.s.CPUFeatures.edx.u1FXSR != 0;
1514}
1515
1516
1517/**
1518 * Checks if the host OS uses the SYSENTER / SYSEXIT instructions.
1519 * @returns true if used.
1520 * @returns false if not used.
1521 * @param pVM The VM handle.
1522 */
1523CPUMDECL(bool) CPUMIsHostUsingSysEnter(PVM pVM)
1524{
1525 return (pVM->cpum.s.fUseFlags & CPUM_USE_SYSENTER) != 0;
1526}
1527
1528
1529/**
1530 * Checks if the host OS uses the SYSCALL / SYSRET instructions.
1531 * @returns true if used.
1532 * @returns false if not used.
1533 * @param pVM The VM handle.
1534 */
1535CPUMDECL(bool) CPUMIsHostUsingSysCall(PVM pVM)
1536{
1537 return (pVM->cpum.s.fUseFlags & CPUM_USE_SYSCALL) != 0;
1538}
1539
1540
1541#ifndef IN_RING3
1542/**
1543 * Lazily sync in the FPU/XMM state
1544 *
1545 * @returns VBox status code.
1546 * @param pVM VM handle.
1547 */
1548CPUMDECL(int) CPUMHandleLazyFPU(PVM pVM)
1549{
1550 return CPUMHandleLazyFPUAsm(&pVM->cpum.s);
1551}
1552
1553
1554/**
1555 * Restore host FPU/XMM state
1556 *
1557 * @returns VBox status code.
1558 * @param pVM VM handle.
1559 */
1560CPUMDECL(int) CPUMRestoreHostFPUState(PVM pVM)
1561{
1562 Assert(pVM->cpum.s.CPUFeatures.edx.u1FXSR);
1563 return CPUMRestoreHostFPUStateAsm(&pVM->cpum.s);
1564}
1565#endif /* !IN_RING3 */
1566
1567
1568/**
1569 * Checks if we activated the FPU/XMM state of the guest OS
1570 * @returns true if we did.
1571 * @returns false if not.
1572 * @param pVM The VM handle.
1573 */
1574CPUMDECL(bool) CPUMIsGuestFPUStateActive(PVM pVM)
1575{
1576 return (pVM->cpum.s.fUseFlags & CPUM_USED_FPU) != 0;
1577}
1578
1579
1580/**
1581 * Deactivate the FPU/XMM state of the guest OS
1582 * @param pVM The VM handle.
1583 */
1584CPUMDECL(void) CPUMDeactivateGuestFPUState(PVM pVM)
1585{
1586 pVM->cpum.s.fUseFlags &= ~CPUM_USED_FPU;
1587}
1588
1589
1590/**
1591 * Checks if the hidden selector registers are valid
1592 * @returns true if they are.
1593 * @returns false if not.
1594 * @param pVM The VM handle.
1595 */
1596CPUMDECL(bool) CPUMAreHiddenSelRegsValid(PVM pVM)
1597{
1598 return !!pVM->cpum.s.fValidHiddenSelRegs; /** @todo change fValidHiddenSelRegs to bool! */
1599}
1600
1601
1602/**
1603 * Checks if the hidden selector registers are valid
1604 * @param pVM The VM handle.
1605 * @param fValid Valid or not
1606 */
1607CPUMDECL(void) CPUMSetHiddenSelRegsValid(PVM pVM, bool fValid)
1608{
1609 pVM->cpum.s.fValidHiddenSelRegs = fValid;
1610}
1611
1612
1613/**
1614 * Get the current privilege level of the guest.
1615 *
1616 * @returns cpl
1617 * @param pVM VM Handle.
1618 * @param pRegFrame Trap register frame.
1619 */
1620CPUMDECL(uint32_t) CPUMGetGuestCPL(PVM pVM, PCPUMCTXCORE pCtxCore)
1621{
1622 uint32_t cpl;
1623
1624 if (CPUMAreHiddenSelRegsValid(pVM))
1625 cpl = pCtxCore->ssHid.Attr.n.u2Dpl;
1626 else if (RT_LIKELY(pVM->cpum.s.Guest.cr0 & X86_CR0_PE))
1627 {
1628 if (RT_LIKELY(!pCtxCore->eflags.Bits.u1VM))
1629 {
1630 cpl = (pCtxCore->ss & X86_SEL_RPL);
1631#ifndef IN_RING0
1632 if (cpl == 1)
1633 cpl = 0;
1634#endif
1635 }
1636 else
1637 cpl = 3;
1638 }
1639 else
1640 cpl = 0; /* real mode; cpl is zero */
1641
1642 return cpl;
1643}
1644
1645
1646/**
1647 * Gets the current guest CPU mode.
1648 *
1649 * If paging mode is what you need, check out PGMGetGuestMode().
1650 *
1651 * @returns The CPU mode.
1652 * @param pVM The VM handle.
1653 */
1654CPUMDECL(CPUMMODE) CPUMGetGuestMode(PVM pVM)
1655{
1656 CPUMMODE enmMode;
1657 if (!(pVM->cpum.s.Guest.cr0 & X86_CR0_PE))
1658 enmMode = CPUMMODE_REAL;
1659 else //GUEST64 if (!(pVM->cpum.s.Guest.efer & MSR_K6_EFER_LMA)
1660 enmMode = CPUMMODE_PROTECTED;
1661//GUEST64 else
1662//GUEST64 enmMode = CPUMMODE_LONG;
1663
1664 return enmMode;
1665}
1666
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