VirtualBox

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

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

deal with the centaur cpuid stuff.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 39.1 KB
Line 
1/* $Id: CPUMAllRegs.cpp 5285 2007-10-13 23:55:58Z 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
443CPUMDECL(int) CPUMSetGuestCR0(PVM pVM, uint32_t cr0)
444{
445 if ( (cr0 & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE))
446 != (pVM->cpum.s.Guest.cr0 & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE)))
447 pVM->cpum.s.fChanged |= CPUM_CHANGED_GLOBAL_TLB_FLUSH;
448 pVM->cpum.s.fChanged |= CPUM_CHANGED_CR0;
449 pVM->cpum.s.Guest.cr0 = cr0 | X86_CR0_ET;
450 return VINF_SUCCESS;
451}
452
453CPUMDECL(int) CPUMSetGuestCR2(PVM pVM, uint32_t cr2)
454{
455 pVM->cpum.s.Guest.cr2 = cr2;
456 return VINF_SUCCESS;
457}
458
459CPUMDECL(int) CPUMSetGuestCR3(PVM pVM, uint32_t cr3)
460{
461 pVM->cpum.s.Guest.cr3 = cr3;
462 pVM->cpum.s.fChanged |= CPUM_CHANGED_CR3;
463 return VINF_SUCCESS;
464}
465
466CPUMDECL(int) CPUMSetGuestCR4(PVM pVM, uint32_t cr4)
467{
468 if ( (cr4 & (X86_CR4_PGE | X86_CR4_PAE | X86_CR4_PSE))
469 != (pVM->cpum.s.Guest.cr4 & (X86_CR4_PGE | X86_CR4_PAE | X86_CR4_PSE)))
470 pVM->cpum.s.fChanged |= CPUM_CHANGED_GLOBAL_TLB_FLUSH;
471 pVM->cpum.s.fChanged |= CPUM_CHANGED_CR4;
472 if (!CPUMSupportsFXSR(pVM))
473 cr4 &= ~X86_CR4_OSFSXR;
474 pVM->cpum.s.Guest.cr4 = cr4;
475 return VINF_SUCCESS;
476}
477
478CPUMDECL(int) CPUMSetGuestEFlags(PVM pVM, uint32_t eflags)
479{
480 pVM->cpum.s.Guest.eflags.u32 = eflags;
481 return VINF_SUCCESS;
482}
483
484CPUMDECL(int) CPUMSetGuestEIP(PVM pVM, uint32_t eip)
485{
486 pVM->cpum.s.Guest.eip = eip;
487 return VINF_SUCCESS;
488}
489
490CPUMDECL(int) CPUMSetGuestEAX(PVM pVM, uint32_t eax)
491{
492 pVM->cpum.s.Guest.eax = eax;
493 return VINF_SUCCESS;
494}
495
496CPUMDECL(int) CPUMSetGuestEBX(PVM pVM, uint32_t ebx)
497{
498 pVM->cpum.s.Guest.ebx = ebx;
499 return VINF_SUCCESS;
500}
501
502CPUMDECL(int) CPUMSetGuestECX(PVM pVM, uint32_t ecx)
503{
504 pVM->cpum.s.Guest.ecx = ecx;
505 return VINF_SUCCESS;
506}
507
508CPUMDECL(int) CPUMSetGuestEDX(PVM pVM, uint32_t edx)
509{
510 pVM->cpum.s.Guest.edx = edx;
511 return VINF_SUCCESS;
512}
513
514CPUMDECL(int) CPUMSetGuestESP(PVM pVM, uint32_t esp)
515{
516 pVM->cpum.s.Guest.esp = esp;
517 return VINF_SUCCESS;
518}
519
520CPUMDECL(int) CPUMSetGuestEBP(PVM pVM, uint32_t ebp)
521{
522 pVM->cpum.s.Guest.ebp = ebp;
523 return VINF_SUCCESS;
524}
525
526CPUMDECL(int) CPUMSetGuestESI(PVM pVM, uint32_t esi)
527{
528 pVM->cpum.s.Guest.esi = esi;
529 return VINF_SUCCESS;
530}
531
532CPUMDECL(int) CPUMSetGuestEDI(PVM pVM, uint32_t edi)
533{
534 pVM->cpum.s.Guest.edi = edi;
535 return VINF_SUCCESS;
536}
537
538CPUMDECL(int) CPUMSetGuestSS(PVM pVM, uint16_t ss)
539{
540 pVM->cpum.s.Guest.ss = ss;
541 return VINF_SUCCESS;
542}
543
544CPUMDECL(int) CPUMSetGuestCS(PVM pVM, uint16_t cs)
545{
546 pVM->cpum.s.Guest.cs = cs;
547 return VINF_SUCCESS;
548}
549
550CPUMDECL(int) CPUMSetGuestDS(PVM pVM, uint16_t ds)
551{
552 pVM->cpum.s.Guest.ds = ds;
553 return VINF_SUCCESS;
554}
555
556CPUMDECL(int) CPUMSetGuestES(PVM pVM, uint16_t es)
557{
558 pVM->cpum.s.Guest.es = es;
559 return VINF_SUCCESS;
560}
561
562CPUMDECL(int) CPUMSetGuestFS(PVM pVM, uint16_t fs)
563{
564 pVM->cpum.s.Guest.fs = fs;
565 return VINF_SUCCESS;
566}
567
568CPUMDECL(int) CPUMSetGuestGS(PVM pVM, uint16_t gs)
569{
570 pVM->cpum.s.Guest.gs = gs;
571 return VINF_SUCCESS;
572}
573
574
575CPUMDECL(uint32_t) CPUMGetGuestIDTR(PVM pVM, uint16_t *pcbLimit)
576{
577 if (pcbLimit)
578 *pcbLimit = pVM->cpum.s.Guest.idtr.cbIdt;
579 return pVM->cpum.s.Guest.idtr.pIdt;
580}
581
582CPUMDECL(RTSEL) CPUMGetGuestTR(PVM pVM)
583{
584 return pVM->cpum.s.Guest.tr;
585}
586
587CPUMDECL(RTSEL) CPUMGetGuestCS(PVM pVM)
588{
589 return pVM->cpum.s.Guest.cs;
590}
591
592CPUMDECL(RTSEL) CPUMGetGuestDS(PVM pVM)
593{
594 return pVM->cpum.s.Guest.ds;
595}
596
597CPUMDECL(RTSEL) CPUMGetGuestES(PVM pVM)
598{
599 return pVM->cpum.s.Guest.es;
600}
601
602CPUMDECL(RTSEL) CPUMGetGuestFS(PVM pVM)
603{
604 return pVM->cpum.s.Guest.fs;
605}
606
607CPUMDECL(RTSEL) CPUMGetGuestGS(PVM pVM)
608{
609 return pVM->cpum.s.Guest.gs;
610}
611
612CPUMDECL(RTSEL) CPUMGetGuestSS(PVM pVM)
613{
614 return pVM->cpum.s.Guest.ss;
615}
616
617CPUMDECL(RTSEL) CPUMGetGuestLDTR(PVM pVM)
618{
619 return pVM->cpum.s.Guest.ldtr;
620}
621
622
623CPUMDECL(uint32_t) CPUMGetGuestCR0(PVM pVM)
624{
625 return pVM->cpum.s.Guest.cr0;
626}
627
628CPUMDECL(uint32_t) CPUMGetGuestCR2(PVM pVM)
629{
630 return pVM->cpum.s.Guest.cr2;
631}
632
633CPUMDECL(uint32_t) CPUMGetGuestCR3(PVM pVM)
634{
635 return pVM->cpum.s.Guest.cr3;
636}
637
638CPUMDECL(uint32_t) CPUMGetGuestCR4(PVM pVM)
639{
640 return pVM->cpum.s.Guest.cr4;
641}
642
643CPUMDECL(void) CPUMGetGuestGDTR(PVM pVM, PVBOXGDTR pGDTR)
644{
645 *pGDTR = pVM->cpum.s.Guest.gdtr;
646}
647
648CPUMDECL(uint32_t) CPUMGetGuestEIP(PVM pVM)
649{
650 return pVM->cpum.s.Guest.eip;
651}
652
653CPUMDECL(uint32_t) CPUMGetGuestEAX(PVM pVM)
654{
655 return pVM->cpum.s.Guest.eax;
656}
657
658CPUMDECL(uint32_t) CPUMGetGuestEBX(PVM pVM)
659{
660 return pVM->cpum.s.Guest.ebx;
661}
662
663CPUMDECL(uint32_t) CPUMGetGuestECX(PVM pVM)
664{
665 return pVM->cpum.s.Guest.ecx;
666}
667
668CPUMDECL(uint32_t) CPUMGetGuestEDX(PVM pVM)
669{
670 return pVM->cpum.s.Guest.edx;
671}
672
673CPUMDECL(uint32_t) CPUMGetGuestESI(PVM pVM)
674{
675 return pVM->cpum.s.Guest.esi;
676}
677
678CPUMDECL(uint32_t) CPUMGetGuestEDI(PVM pVM)
679{
680 return pVM->cpum.s.Guest.edi;
681}
682
683CPUMDECL(uint32_t) CPUMGetGuestESP(PVM pVM)
684{
685 return pVM->cpum.s.Guest.esp;
686}
687
688CPUMDECL(uint32_t) CPUMGetGuestEBP(PVM pVM)
689{
690 return pVM->cpum.s.Guest.ebp;
691}
692
693CPUMDECL(uint32_t) CPUMGetGuestEFlags(PVM pVM)
694{
695 return pVM->cpum.s.Guest.eflags.u32;
696}
697
698CPUMDECL(CPUMSELREGHID *) CPUMGetGuestTRHid(PVM pVM)
699{
700 return &pVM->cpum.s.Guest.trHid;
701}
702
703//@todo: crx should be an array
704CPUMDECL(int) CPUMGetGuestCRx(PVM pVM, uint32_t iReg, uint32_t *pValue)
705{
706 switch (iReg)
707 {
708 case USE_REG_CR0:
709 *pValue = pVM->cpum.s.Guest.cr0;
710 break;
711 case USE_REG_CR2:
712 *pValue = pVM->cpum.s.Guest.cr2;
713 break;
714 case USE_REG_CR3:
715 *pValue = pVM->cpum.s.Guest.cr3;
716 break;
717 case USE_REG_CR4:
718 *pValue = pVM->cpum.s.Guest.cr4;
719 break;
720 default:
721 return VERR_INVALID_PARAMETER;
722 }
723 return VINF_SUCCESS;
724}
725
726CPUMDECL(RTUINTREG) CPUMGetGuestDR0(PVM pVM)
727{
728 return pVM->cpum.s.Guest.dr0;
729}
730
731CPUMDECL(RTUINTREG) CPUMGetGuestDR1(PVM pVM)
732{
733 return pVM->cpum.s.Guest.dr1;
734}
735
736CPUMDECL(RTUINTREG) CPUMGetGuestDR2(PVM pVM)
737{
738 return pVM->cpum.s.Guest.dr2;
739}
740
741CPUMDECL(RTUINTREG) CPUMGetGuestDR3(PVM pVM)
742{
743 return pVM->cpum.s.Guest.dr3;
744}
745
746CPUMDECL(RTUINTREG) CPUMGetGuestDR6(PVM pVM)
747{
748 return pVM->cpum.s.Guest.dr6;
749}
750
751CPUMDECL(RTUINTREG) CPUMGetGuestDR7(PVM pVM)
752{
753 return pVM->cpum.s.Guest.dr7;
754}
755
756/** @todo drx should be an array */
757CPUMDECL(int) CPUMGetGuestDRx(PVM pVM, uint32_t iReg, uint32_t *pValue)
758{
759 switch (iReg)
760 {
761 case USE_REG_DR0:
762 *pValue = pVM->cpum.s.Guest.dr0;
763 break;
764 case USE_REG_DR1:
765 *pValue = pVM->cpum.s.Guest.dr1;
766 break;
767 case USE_REG_DR2:
768 *pValue = pVM->cpum.s.Guest.dr2;
769 break;
770 case USE_REG_DR3:
771 *pValue = pVM->cpum.s.Guest.dr3;
772 break;
773 case USE_REG_DR4:
774 case USE_REG_DR6:
775 *pValue = pVM->cpum.s.Guest.dr6;
776 break;
777 case USE_REG_DR5:
778 case USE_REG_DR7:
779 *pValue = pVM->cpum.s.Guest.dr7;
780 break;
781
782 default:
783 return VERR_INVALID_PARAMETER;
784 }
785 return VINF_SUCCESS;
786}
787
788/**
789 * Gets a CpuId leaf.
790 *
791 * @param pVM The VM handle.
792 * @param iLeaf The CPUID leaf to get.
793 * @param pEax Where to store the EAX value.
794 * @param pEbx Where to store the EBX value.
795 * @param pEcx Where to store the ECX value.
796 * @param pEdx Where to store the EDX value.
797 */
798CPUMDECL(void) CPUMGetGuestCpuId(PVM pVM, uint32_t iLeaf, uint32_t *pEax, uint32_t *pEbx, uint32_t *pEcx, uint32_t *pEdx)
799{
800 PCCPUMCPUID pCpuId;
801 if (iLeaf < ELEMENTS(pVM->cpum.s.aGuestCpuIdStd))
802 pCpuId = &pVM->cpum.s.aGuestCpuIdStd[iLeaf];
803 else if (iLeaf - UINT32_C(0x80000000) < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdExt))
804 pCpuId = &pVM->cpum.s.aGuestCpuIdExt[iLeaf - UINT32_C(0x80000000)];
805 else if (iLeaf - UINT32_C(0xc0000000) < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdCentaur))
806 pCpuId = &pVM->cpum.s.aGuestCpuIdCentaur[iLeaf - UINT32_C(0xc0000000)];
807 else
808 pCpuId = &pVM->cpum.s.GuestCpuIdDef;
809
810 *pEax = pCpuId->eax;
811 *pEbx = pCpuId->ebx;
812 *pEcx = pCpuId->ecx;
813 *pEdx = pCpuId->edx;
814 Log2(("CPUMGetGuestCpuId: iLeaf=%#010x %RX32 %RX32 %RX32 %RX32\n", iLeaf, *pEax, *pEbx, *pEcx, *pEdx));
815}
816
817/**
818 * Gets a pointer to the array of standard CPUID leafs.
819 *
820 * CPUMGetGuestCpuIdStdMax() give the size of the array.
821 *
822 * @returns Pointer to the standard CPUID leafs (read-only).
823 * @param pVM The VM handle.
824 * @remark Intended for PATM.
825 */
826CPUMDECL(GCPTRTYPE(PCCPUMCPUID)) CPUMGetGuestCpuIdStdGCPtr(PVM pVM)
827{
828 return GCPTRTYPE(PCCPUMCPUID)VM_GUEST_ADDR(pVM, &pVM->cpum.s.aGuestCpuIdStd[0]);
829}
830
831/**
832 * Gets a pointer to the array of extended CPUID leafs.
833 *
834 * CPUMGetGuestCpuIdExtMax() give the size of the array.
835 *
836 * @returns Pointer to the extended CPUID leafs (read-only).
837 * @param pVM The VM handle.
838 * @remark Intended for PATM.
839 */
840CPUMDECL(GCPTRTYPE(PCCPUMCPUID)) CPUMGetGuestCpuIdExtGCPtr(PVM pVM)
841{
842 return GCPTRTYPE(PCCPUMCPUID)VM_GUEST_ADDR(pVM, &pVM->cpum.s.aGuestCpuIdExt[0]);
843}
844
845/**
846 * Gets a pointer to the array of centaur CPUID leafs.
847 *
848 * CPUMGetGuestCpuIdCentaurMax() give the size of the array.
849 *
850 * @returns Pointer to the centaur CPUID leafs (read-only).
851 * @param pVM The VM handle.
852 * @remark Intended for PATM.
853 */
854CPUMDECL(GCPTRTYPE(PCCPUMCPUID)) CPUMGetGuestCpuIdCentaurGCPtr(PVM pVM)
855{
856 return GCPTRTYPE(PCCPUMCPUID)VM_GUEST_ADDR(pVM, &pVM->cpum.s.aGuestCpuIdCentaur[0]);
857}
858
859/**
860 * Gets a pointer to the default CPUID leaf.
861 *
862 * @returns Pointer to the default CPUID leaf (read-only).
863 * @param pVM The VM handle.
864 * @remark Intended for PATM.
865 */
866CPUMDECL(GCPTRTYPE(PCCPUMCPUID)) CPUMGetGuestCpuIdDefGCPtr(PVM pVM)
867{
868 return GCPTRTYPE(PCCPUMCPUID)VM_GUEST_ADDR(pVM, &pVM->cpum.s.GuestCpuIdDef);
869}
870
871/**
872 * Gets a number of standard CPUID leafs.
873 *
874 * @returns Number of leafs.
875 * @param pVM The VM handle.
876 * @remark Intended for PATM.
877 */
878CPUMDECL(uint32_t) CPUMGetGuestCpuIdStdMax(PVM pVM)
879{
880 return RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdStd);
881}
882
883/**
884 * Gets a number of extended CPUID leafs.
885 *
886 * @returns Number of leafs.
887 * @param pVM The VM handle.
888 * @remark Intended for PATM.
889 */
890CPUMDECL(uint32_t) CPUMGetGuestCpuIdExtMax(PVM pVM)
891{
892 return RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdExt);
893}
894
895/**
896 * Gets a number of centaur CPUID leafs.
897 *
898 * @returns Number of leafs.
899 * @param pVM The VM handle.
900 * @remark Intended for PATM.
901 */
902CPUMDECL(uint32_t) CPUMGetGuestCpuIdCentaurMax(PVM pVM)
903{
904 return RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdCentaur);
905}
906
907/**
908 * Sets a CPUID feature bit.
909 *
910 * @param pVM The VM Handle.
911 * @param enmFeature The feature to set.
912 */
913CPUMDECL(void) CPUMSetGuestCpuIdFeature(PVM pVM, CPUMCPUIDFEATURE enmFeature)
914{
915 switch (enmFeature)
916 {
917 /*
918 * Set the APIC bit in both feature masks.
919 */
920 case CPUMCPUIDFEATURE_APIC:
921 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
922 pVM->cpum.s.aGuestCpuIdStd[1].edx |= X86_CPUID_FEATURE_EDX_APIC;
923 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
924 && pVM->cpum.s.aGuestCpuIdExt[1].edx)
925 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_APIC;
926 Log(("CPUMSetGuestCpuIdFeature: Enabled APIC\n"));
927 break;
928
929 /*
930 * Set the sysenter/sysexit bit in both feature masks.
931 * Assumes the caller knows what it's doing! (host must support these)
932 */
933 case CPUMCPUIDFEATURE_SEP:
934 {
935 uint32_t ulEdx, ulDummy;
936
937 ASMCpuId(1, &ulDummy, &ulDummy, &ulDummy, &ulEdx);
938 if (!(ulEdx & X86_CPUID_FEATURE_EDX_SEP))
939 {
940 AssertMsgFailed(("ERROR: Can't turn on SEP when the host doesn't support it!!\n"));
941 return;
942 }
943
944 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
945 pVM->cpum.s.aGuestCpuIdStd[1].edx |= X86_CPUID_FEATURE_EDX_SEP;
946 if ( pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001
947 && pVM->cpum.s.aGuestCpuIdExt[1].edx)
948 pVM->cpum.s.aGuestCpuIdExt[1].edx |= X86_CPUID_AMD_FEATURE_EDX_SEP;
949 Log(("CPUMSetGuestCpuIdFeature: Enabled sysenter/exit\n"));
950 break;
951 }
952
953 default:
954 AssertMsgFailed(("enmFeature=%d\n", enmFeature));
955 break;
956 }
957}
958
959/**
960 * Clears a CPUID feature bit.
961 *
962 * @param pVM The VM Handle.
963 * @param enmFeature The feature to clear.
964 */
965CPUMDECL(void) CPUMClearGuestCpuIdFeature(PVM pVM, CPUMCPUIDFEATURE enmFeature)
966{
967 switch (enmFeature)
968 {
969 /*
970 * Set the APIC bit in both feature masks.
971 */
972 case CPUMCPUIDFEATURE_APIC:
973 if (pVM->cpum.s.aGuestCpuIdStd[0].eax >= 1)
974 pVM->cpum.s.aGuestCpuIdStd[1].edx &= ~X86_CPUID_FEATURE_EDX_APIC;
975 if (pVM->cpum.s.aGuestCpuIdExt[0].eax >= 0x80000001)
976 pVM->cpum.s.aGuestCpuIdExt[1].edx &= ~X86_CPUID_AMD_FEATURE_EDX_APIC;
977 Log(("CPUMSetGuestCpuIdFeature: Disabled APIC\n"));
978 break;
979
980 default:
981 AssertMsgFailed(("enmFeature=%d\n", enmFeature));
982 break;
983 }
984}
985
986
987
988CPUMDECL(int) CPUMSetGuestDR0(PVM pVM, RTGCUINTREG uDr0)
989{
990 pVM->cpum.s.Guest.dr0 = uDr0;
991 return CPUMRecalcHyperDRx(pVM);
992}
993
994CPUMDECL(int) CPUMSetGuestDR1(PVM pVM, RTGCUINTREG uDr1)
995{
996 pVM->cpum.s.Guest.dr1 = uDr1;
997 return CPUMRecalcHyperDRx(pVM);
998}
999
1000CPUMDECL(int) CPUMSetGuestDR2(PVM pVM, RTGCUINTREG uDr2)
1001{
1002 pVM->cpum.s.Guest.dr2 = uDr2;
1003 return CPUMRecalcHyperDRx(pVM);
1004}
1005
1006CPUMDECL(int) CPUMSetGuestDR3(PVM pVM, RTGCUINTREG uDr3)
1007{
1008 pVM->cpum.s.Guest.dr3 = uDr3;
1009 return CPUMRecalcHyperDRx(pVM);
1010}
1011
1012CPUMDECL(int) CPUMSetGuestDR6(PVM pVM, RTGCUINTREG uDr6)
1013{
1014 pVM->cpum.s.Guest.dr6 = uDr6;
1015 return CPUMRecalcHyperDRx(pVM);
1016}
1017
1018CPUMDECL(int) CPUMSetGuestDR7(PVM pVM, RTGCUINTREG uDr7)
1019{
1020 pVM->cpum.s.Guest.dr7 = uDr7;
1021 return CPUMRecalcHyperDRx(pVM);
1022}
1023
1024/** @todo drx should be an array */
1025CPUMDECL(int) CPUMSetGuestDRx(PVM pVM, uint32_t iReg, uint32_t Value)
1026{
1027 switch (iReg)
1028 {
1029 case USE_REG_DR0:
1030 pVM->cpum.s.Guest.dr0 = Value;
1031 break;
1032 case USE_REG_DR1:
1033 pVM->cpum.s.Guest.dr1 = Value;
1034 break;
1035 case USE_REG_DR2:
1036 pVM->cpum.s.Guest.dr2 = Value;
1037 break;
1038 case USE_REG_DR3:
1039 pVM->cpum.s.Guest.dr3 = Value;
1040 break;
1041 case USE_REG_DR4:
1042 case USE_REG_DR6:
1043 pVM->cpum.s.Guest.dr6 = Value;
1044 break;
1045 case USE_REG_DR5:
1046 case USE_REG_DR7:
1047 pVM->cpum.s.Guest.dr7 = Value;
1048 break;
1049
1050 default:
1051 return VERR_INVALID_PARAMETER;
1052 }
1053 return CPUMRecalcHyperDRx(pVM);
1054}
1055
1056
1057/**
1058 * Recalculates the hypvervisor DRx register values based on
1059 * current guest registers and DBGF breakpoints.
1060 *
1061 * This is called whenever a guest DRx register is modified and when DBGF
1062 * sets a hardware breakpoint. In guest context this function will reload
1063 * any (hyper) DRx registers which comes out with a different value.
1064 *
1065 * @returns VINF_SUCCESS.
1066 * @param pVM The VM handle.
1067 */
1068CPUMDECL(int) CPUMRecalcHyperDRx(PVM pVM)
1069{
1070 /*
1071 * Compare the DR7s first.
1072 *
1073 * We only care about the enabled flags. The GE and LE flags are always
1074 * set and we don't care if the guest doesn't set them. GD is virtualized
1075 * when we dispatch #DB, we never enable it.
1076 */
1077 const RTGCUINTREG uDbgfDr7 = DBGFBpGetDR7(pVM);
1078#ifdef CPUM_VIRTUALIZE_DRX
1079 const RTGCUINTREG uGstDr7 = CPUMGetGuestDR7(pVM);
1080#else
1081 const RTGCUINTREG uGstDr7 = 0;
1082#endif
1083 if ((uGstDr7 | uDbgfDr7) & X86_DR7_ENABLED_MASK)
1084 {
1085 /*
1086 * Ok, something is enabled. Recalc each of the breakpoints.
1087 * Straight forward code, not optimized/minimized in any way.
1088 */
1089 RTGCUINTREG uNewDr7 = X86_DR7_GE | X86_DR7_LE | X86_DR7_MB1_MASK;
1090
1091 /* bp 0 */
1092 RTGCUINTREG uNewDr0;
1093 if (uDbgfDr7 & (X86_DR7_L0 | X86_DR7_G0))
1094 {
1095 uNewDr7 |= uDbgfDr7 & (X86_DR7_L0 | X86_DR7_G0 | X86_DR7_RW0_MASK | X86_DR7_LEN0_MASK);
1096 uNewDr0 = DBGFBpGetDR0(pVM);
1097 }
1098 else if (uGstDr7 & (X86_DR7_L0 | X86_DR7_G0))
1099 {
1100 uNewDr7 |= uGstDr7 & (X86_DR7_L0 | X86_DR7_G0 | X86_DR7_RW0_MASK | X86_DR7_LEN0_MASK);
1101 uNewDr0 = CPUMGetGuestDR0(pVM);
1102 }
1103 else
1104 uNewDr0 = pVM->cpum.s.Hyper.dr0;
1105
1106 /* bp 1 */
1107 RTGCUINTREG uNewDr1;
1108 if (uDbgfDr7 & (X86_DR7_L1 | X86_DR7_G1))
1109 {
1110 uNewDr7 |= uDbgfDr7 & (X86_DR7_L1 | X86_DR7_G1 | X86_DR7_RW1_MASK | X86_DR7_LEN1_MASK);
1111 uNewDr1 = DBGFBpGetDR1(pVM);
1112 }
1113 else if (uGstDr7 & (X86_DR7_L1 | X86_DR7_G1))
1114 {
1115 uNewDr7 |= uGstDr7 & (X86_DR7_L1 | X86_DR7_G1 | X86_DR7_RW1_MASK | X86_DR7_LEN1_MASK);
1116 uNewDr1 = CPUMGetGuestDR1(pVM);
1117 }
1118 else
1119 uNewDr1 = pVM->cpum.s.Hyper.dr1;
1120
1121 /* bp 2 */
1122 RTGCUINTREG uNewDr2;
1123 if (uDbgfDr7 & (X86_DR7_L2 | X86_DR7_G2))
1124 {
1125 uNewDr7 |= uDbgfDr7 & (X86_DR7_L2 | X86_DR7_G2 | X86_DR7_RW2_MASK | X86_DR7_LEN2_MASK);
1126 uNewDr2 = DBGFBpGetDR2(pVM);
1127 }
1128 else if (uGstDr7 & (X86_DR7_L2 | X86_DR7_G2))
1129 {
1130 uNewDr7 |= uGstDr7 & (X86_DR7_L2 | X86_DR7_G2 | X86_DR7_RW2_MASK | X86_DR7_LEN2_MASK);
1131 uNewDr2 = CPUMGetGuestDR2(pVM);
1132 }
1133 else
1134 uNewDr2 = pVM->cpum.s.Hyper.dr2;
1135
1136 /* bp 3 */
1137 RTGCUINTREG uNewDr3;
1138 if (uDbgfDr7 & (X86_DR7_L3 | X86_DR7_G3))
1139 {
1140 uNewDr7 |= uDbgfDr7 & (X86_DR7_L3 | X86_DR7_G3 | X86_DR7_RW3_MASK | X86_DR7_LEN3_MASK);
1141 uNewDr3 = DBGFBpGetDR3(pVM);
1142 }
1143 else if (uGstDr7 & (X86_DR7_L3 | X86_DR7_G3))
1144 {
1145 uNewDr7 |= uGstDr7 & (X86_DR7_L3 | X86_DR7_G3 | X86_DR7_RW3_MASK | X86_DR7_LEN3_MASK);
1146 uNewDr3 = CPUMGetGuestDR3(pVM);
1147 }
1148 else
1149 uNewDr3 = pVM->cpum.s.Hyper.dr3;
1150
1151 /*
1152 * Apply the updates.
1153 */
1154#ifdef IN_GC
1155 if (!(pVM->cpum.s.fUseFlags & CPUM_USE_DEBUG_REGS))
1156 {
1157 /** @todo save host DBx registers. */
1158 }
1159#endif
1160 pVM->cpum.s.fUseFlags |= CPUM_USE_DEBUG_REGS;
1161 if (uNewDr3 != pVM->cpum.s.Hyper.dr3)
1162 CPUMSetHyperDR3(pVM, uNewDr3);
1163 if (uNewDr2 != pVM->cpum.s.Hyper.dr2)
1164 CPUMSetHyperDR2(pVM, uNewDr2);
1165 if (uNewDr1 != pVM->cpum.s.Hyper.dr1)
1166 CPUMSetHyperDR1(pVM, uNewDr1);
1167 if (uNewDr0 != pVM->cpum.s.Hyper.dr0)
1168 CPUMSetHyperDR0(pVM, uNewDr0);
1169 if (uNewDr7 != pVM->cpum.s.Hyper.dr7)
1170 CPUMSetHyperDR7(pVM, uNewDr7);
1171 }
1172 else
1173 {
1174#ifdef IN_GC
1175 if (pVM->cpum.s.fUseFlags & CPUM_USE_DEBUG_REGS)
1176 {
1177 /** @todo restore host DBx registers. */
1178 }
1179#endif
1180 pVM->cpum.s.fUseFlags &= ~CPUM_USE_DEBUG_REGS;
1181 }
1182 Log2(("CPUMRecalcHyperDRx: fUseFlags=%#x %RGr %RGr %RGr %RGr %RGr %RGr\n",
1183 pVM->cpum.s.fUseFlags, pVM->cpum.s.Hyper.dr0, pVM->cpum.s.Hyper.dr1,
1184 pVM->cpum.s.Hyper.dr2, pVM->cpum.s.Hyper.dr3, pVM->cpum.s.Hyper.dr6,
1185 pVM->cpum.s.Hyper.dr7));
1186
1187 return VINF_SUCCESS;
1188}
1189
1190#ifndef IN_RING0 /** @todo I don't think we need this in R0, so move it to CPUMAll.cpp? */
1191
1192/**
1193 * Transforms the guest CPU state to raw-ring mode.
1194 *
1195 * This function will change the any of the cs and ss register with DPL=0 to DPL=1.
1196 *
1197 * @returns VBox status. (recompiler failure)
1198 * @param pVM VM handle.
1199 * @param pCtxCore The context core (for trap usage).
1200 * @see @ref pg_raw
1201 */
1202CPUMDECL(int) CPUMRawEnter(PVM pVM, PCPUMCTXCORE pCtxCore)
1203{
1204 Assert(!pVM->cpum.s.fRawEntered);
1205 if (!pCtxCore)
1206 pCtxCore = CPUMCTX2CORE(&pVM->cpum.s.Guest);
1207
1208 /*
1209 * Are we in Ring-0?
1210 */
1211 if ( pCtxCore->ss && (pCtxCore->ss & X86_SEL_RPL) == 0
1212 && !pCtxCore->eflags.Bits.u1VM)
1213 {
1214 /*
1215 * Enter execution mode.
1216 */
1217 PATMRawEnter(pVM, pCtxCore);
1218
1219 /*
1220 * Set CPL to Ring-1.
1221 */
1222 pCtxCore->ss |= 1;
1223 if (pCtxCore->cs && (pCtxCore->cs & X86_SEL_RPL) == 0)
1224 pCtxCore->cs |= 1;
1225 }
1226 else
1227 {
1228 AssertMsg((pCtxCore->ss & X86_SEL_RPL) >= 2 || pCtxCore->eflags.Bits.u1VM,
1229 ("ring-1 code not supported\n"));
1230 /*
1231 * PATM takes care of IOPL and IF flags for Ring-3 and Ring-2 code as well.
1232 */
1233 PATMRawEnter(pVM, pCtxCore);
1234 }
1235
1236 /*
1237 * Assert sanity.
1238 */
1239 AssertMsg((pCtxCore->eflags.u32 & X86_EFL_IF), ("X86_EFL_IF is clear\n"));
1240 AssertReleaseMsg( pCtxCore->eflags.Bits.u2IOPL < (unsigned)(pCtxCore->ss & X86_SEL_RPL)
1241 || pCtxCore->eflags.Bits.u1VM,
1242 ("X86_EFL_IOPL=%d CPL=%d\n", pCtxCore->eflags.Bits.u2IOPL, pCtxCore->ss & X86_SEL_RPL));
1243 Assert((pVM->cpum.s.Guest.cr0 & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE)) == (X86_CR0_PG | X86_CR0_PE | X86_CR0_WP));
1244 pCtxCore->eflags.u32 |= X86_EFL_IF; /* paranoia */
1245
1246 pVM->cpum.s.fRawEntered = true;
1247 return VINF_SUCCESS;
1248}
1249
1250
1251/**
1252 * Transforms the guest CPU state from raw-ring mode to correct values.
1253 *
1254 * This function will change any selector registers with DPL=1 to DPL=0.
1255 *
1256 * @returns Adjusted rc.
1257 * @param pVM VM handle.
1258 * @param rc Raw mode return code
1259 * @param pCtxCore The context core (for trap usage).
1260 * @see @ref pg_raw
1261 */
1262CPUMDECL(int) CPUMRawLeave(PVM pVM, PCPUMCTXCORE pCtxCore, int rc)
1263{
1264 /*
1265 * Don't leave if we've already left (in GC).
1266 */
1267 Assert(pVM->cpum.s.fRawEntered);
1268 if (!pVM->cpum.s.fRawEntered)
1269 return rc;
1270 pVM->cpum.s.fRawEntered = false;
1271
1272 PCPUMCTX pCtx = &pVM->cpum.s.Guest;
1273 if (!pCtxCore)
1274 pCtxCore = CPUMCTX2CORE(pCtx);
1275 Assert(pCtxCore->eflags.Bits.u1VM || (pCtxCore->ss & X86_SEL_RPL));
1276 AssertMsg(pCtxCore->eflags.Bits.u1VM || pCtxCore->eflags.Bits.u2IOPL < (unsigned)(pCtxCore->ss & X86_SEL_RPL),
1277 ("X86_EFL_IOPL=%d CPL=%d\n", pCtxCore->eflags.Bits.u2IOPL, pCtxCore->ss & X86_SEL_RPL));
1278
1279 /*
1280 * Are we executing in raw ring-1?
1281 */
1282 if ( (pCtxCore->ss & X86_SEL_RPL) == 1
1283 && !pCtxCore->eflags.Bits.u1VM)
1284 {
1285 /*
1286 * Leave execution mode.
1287 */
1288 PATMRawLeave(pVM, pCtxCore, rc);
1289 /* Not quite sure if this is really required, but shouldn't harm (too much anyways). */
1290 /** @todo See what happens if we remove this. */
1291 if ((pCtxCore->ds & X86_SEL_RPL) == 1)
1292 pCtxCore->ds &= ~X86_SEL_RPL;
1293 if ((pCtxCore->es & X86_SEL_RPL) == 1)
1294 pCtxCore->es &= ~X86_SEL_RPL;
1295 if ((pCtxCore->fs & X86_SEL_RPL) == 1)
1296 pCtxCore->fs &= ~X86_SEL_RPL;
1297 if ((pCtxCore->gs & X86_SEL_RPL) == 1)
1298 pCtxCore->gs &= ~X86_SEL_RPL;
1299
1300 /*
1301 * Ring-1 selector => Ring-0.
1302 */
1303 pCtxCore->ss &= ~X86_SEL_RPL;
1304 if ((pCtxCore->cs & X86_SEL_RPL) == 1)
1305 pCtxCore->cs &= ~X86_SEL_RPL;
1306 }
1307 else
1308 {
1309 /*
1310 * PATM is taking care of the IOPL and IF flags for us.
1311 */
1312 PATMRawLeave(pVM, pCtxCore, rc);
1313 if (!pCtxCore->eflags.Bits.u1VM)
1314 {
1315 /** @todo See what happens if we remove this. */
1316 if ((pCtxCore->ds & X86_SEL_RPL) == 1)
1317 pCtxCore->ds &= ~X86_SEL_RPL;
1318 if ((pCtxCore->es & X86_SEL_RPL) == 1)
1319 pCtxCore->es &= ~X86_SEL_RPL;
1320 if ((pCtxCore->fs & X86_SEL_RPL) == 1)
1321 pCtxCore->fs &= ~X86_SEL_RPL;
1322 if ((pCtxCore->gs & X86_SEL_RPL) == 1)
1323 pCtxCore->gs &= ~X86_SEL_RPL;
1324 }
1325 }
1326
1327 return rc;
1328}
1329
1330/**
1331 * Updates the EFLAGS while we're in raw-mode.
1332 *
1333 * @param pVM The VM handle.
1334 * @param pCtxCore The context core.
1335 * @param eflags The new EFLAGS value.
1336 */
1337CPUMDECL(void) CPUMRawSetEFlags(PVM pVM, PCPUMCTXCORE pCtxCore, uint32_t eflags)
1338{
1339 if (!pVM->cpum.s.fRawEntered)
1340 {
1341 pCtxCore->eflags.u32 = eflags;
1342 return;
1343 }
1344 PATMRawSetEFlags(pVM, pCtxCore, eflags);
1345}
1346
1347#endif /* !IN_RING0 */
1348
1349/**
1350 * Gets the EFLAGS while we're in raw-mode.
1351 *
1352 * @returns The eflags.
1353 * @param pVM The VM handle.
1354 * @param pCtxCore The context core.
1355 */
1356CPUMDECL(uint32_t) CPUMRawGetEFlags(PVM pVM, PCPUMCTXCORE pCtxCore)
1357{
1358#ifdef IN_RING0
1359 return pCtxCore->eflags.u32;
1360#else
1361 if (!pVM->cpum.s.fRawEntered)
1362 return pCtxCore->eflags.u32;
1363 return PATMRawGetEFlags(pVM, pCtxCore);
1364#endif
1365}
1366
1367
1368
1369
1370/**
1371 * Gets and resets the changed flags (CPUM_CHANGED_*).
1372 * Only REM should call this function.
1373 *
1374 * @returns The changed flags.
1375 * @param pVM The VM handle.
1376 */
1377CPUMDECL(unsigned) CPUMGetAndClearChangedFlagsREM(PVM pVM)
1378{
1379 unsigned fFlags = pVM->cpum.s.fChanged;
1380 pVM->cpum.s.fChanged = 0;
1381 /** @todo change the switcher to use the fChanged flags. */
1382 if (pVM->cpum.s.fUseFlags & CPUM_USED_FPU_SINCE_REM)
1383 {
1384 fFlags |= CPUM_CHANGED_FPU_REM;
1385 pVM->cpum.s.fUseFlags &= ~CPUM_USED_FPU_SINCE_REM;
1386 }
1387 return fFlags;
1388}
1389
1390/**
1391 * Sets the specified changed flags (CPUM_CHANGED_*).
1392 *
1393 * @param pVM The VM handle.
1394 */
1395CPUMDECL(void) CPUMSetChangedFlags(PVM pVM, uint32_t fChangedFlags)
1396{
1397 pVM->cpum.s.fChanged |= fChangedFlags;
1398}
1399
1400/**
1401 * Checks if the CPU supports the FXSAVE and FXRSTOR instruction.
1402 * @returns true if supported.
1403 * @returns false if not supported.
1404 * @param pVM The VM handle.
1405 */
1406CPUMDECL(bool) CPUMSupportsFXSR(PVM pVM)
1407{
1408 return pVM->cpum.s.CPUFeatures.edx.u1FXSR != 0;
1409}
1410
1411
1412/**
1413 * Checks if the host OS uses the SYSENTER / SYSEXIT instructions.
1414 * @returns true if used.
1415 * @returns false if not used.
1416 * @param pVM The VM handle.
1417 */
1418CPUMDECL(bool) CPUMIsHostUsingSysEnter(PVM pVM)
1419{
1420 return (pVM->cpum.s.fUseFlags & CPUM_USE_SYSENTER) != 0;
1421}
1422
1423
1424/**
1425 * Checks if the host OS uses the SYSCALL / SYSRET instructions.
1426 * @returns true if used.
1427 * @returns false if not used.
1428 * @param pVM The VM handle.
1429 */
1430CPUMDECL(bool) CPUMIsHostUsingSysCall(PVM pVM)
1431{
1432 return (pVM->cpum.s.fUseFlags & CPUM_USE_SYSCALL) != 0;
1433}
1434
1435/**
1436 * Lazily sync in the FPU/XMM state
1437 *
1438 * @returns VBox status code.
1439 * @param pVM VM handle.
1440 */
1441CPUMDECL(int) CPUMHandleLazyFPU(PVM pVM)
1442{
1443 return CPUMHandleLazyFPUAsm(&pVM->cpum.s);
1444}
1445
1446/**
1447 * Restore host FPU/XMM state
1448 *
1449 * @returns VBox status code.
1450 * @param pVM VM handle.
1451 */
1452CPUMDECL(int) CPUMRestoreHostFPUState(PVM pVM)
1453{
1454 Assert(pVM->cpum.s.CPUFeatures.edx.u1FXSR);
1455 return CPUMRestoreHostFPUStateAsm(&pVM->cpum.s);
1456}
1457
1458/**
1459 * Checks if we activated the FPU/XMM state of the guest OS
1460 * @returns true if we did.
1461 * @returns false if not.
1462 * @param pVM The VM handle.
1463 */
1464CPUMDECL(bool) CPUMIsGuestFPUStateActive(PVM pVM)
1465{
1466 return (pVM->cpum.s.fUseFlags & CPUM_USED_FPU) != 0;
1467}
1468
1469/**
1470 * Deactivate the FPU/XMM state of the guest OS
1471 * @param pVM The VM handle.
1472 */
1473CPUMDECL(void) CPUMDeactivateGuestFPUState(PVM pVM)
1474{
1475 pVM->cpum.s.fUseFlags &= ~CPUM_USED_FPU;
1476}
1477
1478/**
1479 * Checks if the hidden selector registers are valid
1480 * @returns true if they are.
1481 * @returns false if not.
1482 * @param pVM The VM handle.
1483 */
1484CPUMDECL(bool) CPUMAreHiddenSelRegsValid(PVM pVM)
1485{
1486 return !!pVM->cpum.s.fValidHiddenSelRegs; /** @todo change fValidHiddenSelRegs to bool! */
1487}
1488
1489/**
1490 * Checks if the hidden selector registers are valid
1491 * @param pVM The VM handle.
1492 * @param fValid Valid or not
1493 */
1494CPUMDECL(void) CPUMSetHiddenSelRegsValid(PVM pVM, bool fValid)
1495{
1496 pVM->cpum.s.fValidHiddenSelRegs = fValid;
1497}
1498
1499/**
1500 * Get the current privilege level of the guest.
1501 *
1502 * @returns cpl
1503 * @param pVM VM Handle.
1504 * @param pRegFrame Trap register frame.
1505 */
1506CPUMDECL(uint32_t) CPUMGetGuestCPL(PVM pVM, PCPUMCTXCORE pCtxCore)
1507{
1508 uint32_t cpl;
1509
1510 if (CPUMAreHiddenSelRegsValid(pVM))
1511 cpl = pCtxCore->ssHid.Attr.n.u2Dpl;
1512 else if (RT_LIKELY(pVM->cpum.s.Guest.cr0 & X86_CR0_PE))
1513 {
1514 if (RT_LIKELY(!pCtxCore->eflags.Bits.u1VM))
1515 {
1516 cpl = (pCtxCore->ss & X86_SEL_RPL);
1517#ifndef IN_RING0
1518 if (cpl == 1)
1519 cpl = 0;
1520#endif
1521 }
1522 else
1523 cpl = 3;
1524 }
1525 else
1526 cpl = 0; /* real mode; cpl is zero */
1527
1528 return cpl;
1529}
1530
1531
1532/**
1533 * Gets the current guest CPU mode.
1534 *
1535 * If paging mode is what you need, check out PGMGetGuestMode().
1536 *
1537 * @returns The CPU mode.
1538 * @param pVM The VM handle.
1539 */
1540CPUMDECL(CPUMMODE) CPUMGetGuestMode(PVM pVM)
1541{
1542 CPUMMODE enmMode;
1543 if (!(pVM->cpum.s.Guest.cr0 & X86_CR0_PE))
1544 enmMode = CPUMMODE_REAL;
1545 else //GUEST64 if (!(pVM->cpum.s.Guest.efer & MSR_K6_EFER_LMA)
1546 enmMode = CPUMMODE_PROTECTED;
1547//GUEST64 else
1548//GUEST64 enmMode = CPUMMODE_LONG;
1549
1550 return enmMode;
1551}
1552
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