VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR0/HWSVMR0.cpp@ 4756

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

Translate VERR_EM_INTERPRETER to VINF_EM_RAW_EMULATE_INSTR

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 60.0 KB
Line 
1/* $Id: HWSVMR0.cpp 4756 2007-09-13 08:27:24Z vboxsync $ */
2/** @file
3 * HWACCM SVM - Host Context Ring 0.
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_HWACCM
23#include <VBox/hwaccm.h>
24#include "HWACCMInternal.h"
25#include <VBox/vm.h>
26#include <VBox/x86.h>
27#include <VBox/hwacc_svm.h>
28#include <VBox/pgm.h>
29#include <VBox/pdm.h>
30#include <VBox/err.h>
31#include <VBox/log.h>
32#include <VBox/selm.h>
33#include <VBox/iom.h>
34#include <VBox/dis.h>
35#include <VBox/dbgf.h>
36#include <VBox/disopcode.h>
37#include <iprt/param.h>
38#include <iprt/assert.h>
39#include <iprt/asm.h>
40#include "HWSVMR0.h"
41
42static int SVMR0InterpretInvpg(PVM pVM, PCPUMCTXCORE pRegFrame, uint32_t uASID);
43
44/**
45 * Sets up and activates SVM
46 *
47 * @returns VBox status code.
48 * @param pVM The VM to operate on.
49 */
50HWACCMR0DECL(int) SVMR0Setup(PVM pVM)
51{
52 int rc = VINF_SUCCESS;
53 SVM_VMCB *pVMCB;
54
55 if (pVM == NULL)
56 return VERR_INVALID_PARAMETER;
57
58 /* Setup AMD SVM. */
59 Assert(pVM->hwaccm.s.svm.fSupported);
60
61 pVMCB = (SVM_VMCB *)pVM->hwaccm.s.svm.pVMCB;
62 AssertMsgReturn(pVMCB, ("Invalid pVMCB\n"), VERR_EM_INTERNAL_ERROR);
63
64 /* Program the control fields. Most of them never have to be changed again. */
65 /* CR0/3/4 reads must be intercepted, our shadow values are not necessarily the same as the guest's. */
66 /** @note CR0 & CR4 can be safely read when guest and shadow copies are identical. */
67 pVMCB->ctrl.u16InterceptRdCRx = BIT(0) | BIT(3) | BIT(4) | BIT(8);
68
69 /*
70 * CR0/3/4 writes must be intercepted for obvious reasons.
71 */
72 pVMCB->ctrl.u16InterceptWrCRx = BIT(0) | BIT(3) | BIT(4) | BIT(8);
73
74 /* Intercept all DRx reads and writes. */
75 pVMCB->ctrl.u16InterceptRdDRx = BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(5) | BIT(6) | BIT(7);
76 pVMCB->ctrl.u16InterceptWrDRx = BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(5) | BIT(6) | BIT(7);
77
78 /* Currently we don't care about DRx reads or writes. DRx registers are trashed.
79 * All breakpoints are automatically cleared when the VM exits.
80 */
81
82 /** @todo nested paging */
83 /* Intercept #NM only; #PF is not relevant due to nested paging (we get a seperate exit code (SVM_EXIT_NPF) for
84 * pagefaults that need our attention).
85 */
86 pVMCB->ctrl.u32InterceptException = HWACCM_SVM_TRAP_MASK;
87
88 pVMCB->ctrl.u32InterceptCtrl1 = SVM_CTRL1_INTERCEPT_INTR
89 | SVM_CTRL1_INTERCEPT_VINTR
90 | SVM_CTRL1_INTERCEPT_NMI
91 | SVM_CTRL1_INTERCEPT_SMI
92 | SVM_CTRL1_INTERCEPT_INIT
93 | SVM_CTRL1_INTERCEPT_CR0 /** @todo redundant? */
94 | SVM_CTRL1_INTERCEPT_RDPMC
95 | SVM_CTRL1_INTERCEPT_CPUID
96 | SVM_CTRL1_INTERCEPT_RSM
97 | SVM_CTRL1_INTERCEPT_HLT
98 | SVM_CTRL1_INTERCEPT_INOUT_BITMAP
99 | SVM_CTRL1_INTERCEPT_MSR_SHADOW
100 | SVM_CTRL1_INTERCEPT_INVLPG
101 | SVM_CTRL1_INTERCEPT_INVLPGA /* AMD only */
102 | SVM_CTRL1_INTERCEPT_SHUTDOWN /* fatal */
103 | SVM_CTRL1_INTERCEPT_FERR_FREEZE; /* Legacy FPU FERR handling. */
104 ;
105 pVMCB->ctrl.u32InterceptCtrl2 = SVM_CTRL2_INTERCEPT_VMRUN /* required */
106 | SVM_CTRL2_INTERCEPT_VMMCALL
107 | SVM_CTRL2_INTERCEPT_VMLOAD
108 | SVM_CTRL2_INTERCEPT_VMSAVE
109 | SVM_CTRL2_INTERCEPT_STGI
110 | SVM_CTRL2_INTERCEPT_CLGI
111 | SVM_CTRL2_INTERCEPT_SKINIT
112 | SVM_CTRL2_INTERCEPT_RDTSCP /* AMD only; we don't support this one */
113 ;
114 Log(("pVMCB->ctrl.u32InterceptException = %x\n", pVMCB->ctrl.u32InterceptException));
115 Log(("pVMCB->ctrl.u32InterceptCtrl1 = %x\n", pVMCB->ctrl.u32InterceptCtrl1));
116 Log(("pVMCB->ctrl.u32InterceptCtrl2 = %x\n", pVMCB->ctrl.u32InterceptCtrl2));
117
118 /* Virtualize masking of INTR interrupts. */
119 pVMCB->ctrl.IntCtrl.n.u1VIrqMasking = 1;
120
121 /* Set IO and MSR bitmap addresses. */
122 pVMCB->ctrl.u64IOPMPhysAddr = pVM->hwaccm.s.svm.pIOBitmapPhys;
123 pVMCB->ctrl.u64MSRPMPhysAddr = pVM->hwaccm.s.svm.pMSRBitmapPhys;
124
125 /* Enable nested paging. */
126 /** @todo how to detect support for this?? */
127 pVMCB->ctrl.u64NestedPaging = 0; /** @todo SVM_NESTED_PAGING_ENABLE; */
128
129 /* No LBR virtualization. */
130 pVMCB->ctrl.u64LBRVirt = 0;
131
132 return rc;
133}
134
135
136/**
137 * Injects an event (trap or external interrupt)
138 *
139 * @param pVM The VM to operate on.
140 * @param pVMCB SVM control block
141 * @param pCtx CPU Context
142 * @param pIntInfo SVM interrupt info
143 */
144inline void SVMR0InjectEvent(PVM pVM, SVM_VMCB *pVMCB, CPUMCTX *pCtx, SVM_EVENT* pEvent)
145{
146#ifdef VBOX_STRICT
147 if (pEvent->n.u8Vector == 0xE)
148 Log(("SVM: Inject int %d at %VGv error code=%08x CR2=%08x intInfo=%08x\n", pEvent->n.u8Vector, pCtx->eip, pEvent->n.u32ErrorCode, pCtx->cr2, pEvent->au64[0]));
149 else
150 if (pEvent->n.u8Vector < 0x20)
151 Log(("SVM: Inject int %d at %VGv error code=%08x\n", pEvent->n.u8Vector, pCtx->eip, pEvent->n.u32ErrorCode));
152 else
153 {
154 Log(("INJ-EI: %x at %VGv\n", pEvent->n.u8Vector, pCtx->eip));
155 Assert(!VM_FF_ISSET(pVM, VM_FF_INHIBIT_INTERRUPTS));
156 Assert(pCtx->eflags.u32 & X86_EFL_IF);
157 }
158#endif
159
160 /* Set event injection state. */
161 pVMCB->ctrl.EventInject.au64[0] = pEvent->au64[0];
162}
163
164
165/**
166 * Checks for pending guest interrupts and injects them
167 *
168 * @returns VBox status code.
169 * @param pVM The VM to operate on.
170 * @param pVMCB SVM control block
171 * @param pCtx CPU Context
172 */
173static int SVMR0CheckPendingInterrupt(PVM pVM, SVM_VMCB *pVMCB, CPUMCTX *pCtx)
174{
175 int rc;
176
177 /* Dispatch any pending interrupts. (injected before, but a VM exit occurred prematurely) */
178 if (pVM->hwaccm.s.Event.fPending)
179 {
180 SVM_EVENT Event;
181
182 Log(("Reinjecting event %08x %08x at %VGv\n", pVM->hwaccm.s.Event.intInfo, pVM->hwaccm.s.Event.errCode, pCtx->eip));
183 STAM_COUNTER_INC(&pVM->hwaccm.s.StatIntReinject);
184 Event.au64[0] = pVM->hwaccm.s.Event.intInfo;
185 SVMR0InjectEvent(pVM, pVMCB, pCtx, &Event);
186
187 pVM->hwaccm.s.Event.fPending = false;
188 return VINF_SUCCESS;
189 }
190
191 /* When external interrupts are pending, we should exit the VM when IF is set. */
192 if ( !TRPMHasTrap(pVM)
193 && VM_FF_ISPENDING(pVM, (VM_FF_INTERRUPT_APIC|VM_FF_INTERRUPT_PIC)))
194 {
195 if (!(pCtx->eflags.u32 & X86_EFL_IF))
196 {
197 Log2(("Enable irq window exit!\n"));
198 /** @todo use virtual interrupt method to inject a pending irq; dispatched as soon as guest.IF is set. */
199//// pVMCB->ctrl.u32InterceptCtrl1 |= SVM_CTRL1_INTERCEPT_VINTR;
200//// AssertRC(rc);
201 }
202 else
203 if (!VM_FF_ISSET(pVM, VM_FF_INHIBIT_INTERRUPTS))
204 {
205 uint8_t u8Interrupt;
206
207 rc = PDMGetInterrupt(pVM, &u8Interrupt);
208 Log(("Dispatch interrupt: u8Interrupt=%x (%d) rc=%Vrc\n", u8Interrupt, u8Interrupt, rc));
209 if (VBOX_SUCCESS(rc))
210 {
211 rc = TRPMAssertTrap(pVM, u8Interrupt, TRPM_HARDWARE_INT);
212 AssertRC(rc);
213 }
214 else
215 {
216 /* can't happen... */
217 AssertFailed();
218 STAM_COUNTER_INC(&pVM->hwaccm.s.StatSwitchGuestIrq);
219 return VINF_EM_RAW_INTERRUPT_PENDING;
220 }
221 }
222 else
223 Log(("Pending interrupt blocked at %VGv by VM_FF_INHIBIT_INTERRUPTS!!\n", pCtx->eip));
224 }
225
226#ifdef VBOX_STRICT
227 if (TRPMHasTrap(pVM))
228 {
229 uint8_t u8Vector;
230 rc = TRPMQueryTrapAll(pVM, &u8Vector, 0, 0, 0);
231 AssertRC(rc);
232 }
233#endif
234
235 if ( pCtx->eflags.u32 & X86_EFL_IF
236 && (!VM_FF_ISSET(pVM, VM_FF_INHIBIT_INTERRUPTS))
237 && TRPMHasTrap(pVM)
238 )
239 {
240 uint8_t u8Vector;
241 int rc;
242 TRPMEVENT enmType;
243 SVM_EVENT Event;
244 uint32_t u32ErrorCode;
245
246 Event.au64[0] = 0;
247
248 /* If a new event is pending, then dispatch it now. */
249 rc = TRPMQueryTrapAll(pVM, &u8Vector, &enmType, &u32ErrorCode, 0);
250 AssertRC(rc);
251 Assert(pCtx->eflags.Bits.u1IF == 1 || enmType == TRPM_TRAP);
252 Assert(enmType != TRPM_SOFTWARE_INT);
253
254 /* Clear the pending trap. */
255 rc = TRPMResetTrap(pVM);
256 AssertRC(rc);
257
258 Event.n.u8Vector = u8Vector;
259 Event.n.u1Valid = 1;
260 Event.n.u32ErrorCode = u32ErrorCode;
261
262 if (enmType == TRPM_TRAP)
263 {
264 switch (u8Vector) {
265 case 8:
266 case 10:
267 case 11:
268 case 12:
269 case 13:
270 case 14:
271 case 17:
272 /* Valid error codes. */
273 Event.n.u1ErrorCodeValid = 1;
274 break;
275 default:
276 break;
277 }
278 if (u8Vector == X86_XCPT_NMI)
279 Event.n.u3Type = SVM_EVENT_NMI;
280 else
281 Event.n.u3Type = SVM_EVENT_EXCEPTION;
282 }
283 else
284 Event.n.u3Type = SVM_EVENT_EXTERNAL_IRQ;
285
286 STAM_COUNTER_INC(&pVM->hwaccm.s.StatIntInject);
287 SVMR0InjectEvent(pVM, pVMCB, pCtx, &Event);
288 } /* if (interrupts can be dispatched) */
289
290 return VINF_SUCCESS;
291}
292
293
294/**
295 * Loads the guest state
296 *
297 * @returns VBox status code.
298 * @param pVM The VM to operate on.
299 * @param pCtx Guest context
300 */
301HWACCMR0DECL(int) SVMR0LoadGuestState(PVM pVM, CPUMCTX *pCtx)
302{
303 RTGCUINTPTR val;
304 SVM_VMCB *pVMCB;
305
306 if (pVM == NULL)
307 return VERR_INVALID_PARAMETER;
308
309 /* Setup AMD SVM. */
310 Assert(pVM->hwaccm.s.svm.fSupported);
311
312 pVMCB = (SVM_VMCB *)pVM->hwaccm.s.svm.pVMCB;
313 AssertMsgReturn(pVMCB, ("Invalid pVMCB\n"), VERR_EM_INTERNAL_ERROR);
314
315 /* Guest CPU context: ES, CS, SS, DS, FS, GS. */
316 if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_SEGMENT_REGS)
317 {
318 SVM_WRITE_SELREG(CS, cs);
319 SVM_WRITE_SELREG(SS, ss);
320 SVM_WRITE_SELREG(DS, ds);
321 SVM_WRITE_SELREG(ES, es);
322 SVM_WRITE_SELREG(FS, fs);
323 SVM_WRITE_SELREG(GS, gs);
324 }
325
326 /* Guest CPU context: LDTR. */
327 if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_LDTR)
328 {
329 SVM_WRITE_SELREG(LDTR, ldtr);
330 }
331
332 /* Guest CPU context: TR. */
333 if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_TR)
334 {
335 SVM_WRITE_SELREG(TR, tr);
336 }
337
338 /* Guest CPU context: GDTR. */
339 if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_GDTR)
340 {
341 pVMCB->guest.GDTR.u32Limit = pCtx->gdtr.cbGdt;
342 pVMCB->guest.GDTR.u64Base = pCtx->gdtr.pGdt;
343 }
344
345 /* Guest CPU context: IDTR. */
346 if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_IDTR)
347 {
348 pVMCB->guest.IDTR.u32Limit = pCtx->idtr.cbIdt;
349 pVMCB->guest.IDTR.u64Base = pCtx->idtr.pIdt;
350 }
351
352 /*
353 * Sysenter MSRs
354 */
355 if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_SYSENTER_MSR)
356 {
357 pVMCB->guest.u64SysEnterCS = pCtx->SysEnter.cs;
358 pVMCB->guest.u64SysEnterEIP = pCtx->SysEnter.eip;
359 pVMCB->guest.u64SysEnterESP = pCtx->SysEnter.esp;
360 }
361
362 /* Control registers */
363 if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_CR0)
364 {
365 val = pCtx->cr0;
366 if (CPUMIsGuestFPUStateActive(pVM) == false)
367 {
368 /* Always use #NM exceptions to load the FPU/XMM state on demand. */
369 val |= X86_CR0_TS | X86_CR0_ET | X86_CR0_NE | X86_CR0_MP;
370 }
371 else
372 {
373 Assert(pVM->hwaccm.s.svm.fResumeVM == true);
374 /** @todo check if we support the old style mess correctly. */
375 if (!(val & X86_CR0_NE))
376 {
377 Log(("Forcing X86_CR0_NE!!!\n"));
378
379 /* Also catch floating point exceptions as we need to report them to the guest in a different way. */
380 if (!pVM->hwaccm.s.fFPUOldStyleOverride)
381 {
382 pVMCB->ctrl.u32InterceptException |= BIT(16);
383 pVM->hwaccm.s.fFPUOldStyleOverride = true;
384 }
385 }
386 val |= X86_CR0_NE; /* always turn on the native mechanism to report FPU errors (old style uses interrupts) */
387 }
388 if (!(val & X86_CR0_CD))
389 val &= ~X86_CR0_NW; /* Illegal when cache is turned on. */
390
391 val |= X86_CR0_PG; /* Paging is always enabled; even when the guest is running in real mode or PE without paging. */
392 pVMCB->guest.u64CR0 = val;
393 }
394 /* CR2 as well */
395 pVMCB->guest.u64CR2 = pCtx->cr2;
396
397 if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_CR3)
398 {
399 /* Save our shadow CR3 register. */
400 pVMCB->guest.u64CR3 = PGMGetHyperCR3(pVM);
401 }
402
403 if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_CR4)
404 {
405 val = pCtx->cr4;
406 switch(pVM->hwaccm.s.enmShadowMode)
407 {
408 case PGMMODE_REAL:
409 case PGMMODE_PROTECTED: /* Protected mode, no paging. */
410 AssertFailed();
411 return VERR_PGM_UNSUPPORTED_HOST_PAGING_MODE;
412
413 case PGMMODE_32_BIT: /* 32-bit paging. */
414 break;
415
416 case PGMMODE_PAE: /* PAE paging. */
417 case PGMMODE_PAE_NX: /* PAE paging with NX enabled. */
418 /** @todo use normal 32 bits paging */
419 val |= X86_CR4_PAE;
420 break;
421
422 case PGMMODE_AMD64: /* 64-bit AMD paging (long mode). */
423 case PGMMODE_AMD64_NX: /* 64-bit AMD paging (long mode) with NX enabled. */
424 AssertFailed();
425 return VERR_PGM_UNSUPPORTED_HOST_PAGING_MODE;
426
427 default: /* shut up gcc */
428 AssertFailed();
429 return VERR_PGM_UNSUPPORTED_HOST_PAGING_MODE;
430 }
431 pVMCB->guest.u64CR4 = val;
432 }
433
434 /* Debug registers. */
435 if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_DEBUG)
436 {
437 /** @todo DR0-6 */
438 val = pCtx->dr7;
439 val &= ~(BIT(11) | BIT(12) | BIT(14) | BIT(15)); /* must be zero */
440 val |= 0x400; /* must be one */
441#ifdef VBOX_STRICT
442 val = 0x400;
443#endif
444 pVMCB->guest.u64DR7 = val;
445
446 pVMCB->guest.u64DR6 = pCtx->dr6;
447 }
448
449 /* EIP, ESP and EFLAGS */
450 pVMCB->guest.u64RIP = pCtx->eip;
451 pVMCB->guest.u64RSP = pCtx->esp;
452 pVMCB->guest.u64RFlags = pCtx->eflags.u32;
453
454 /* Set CPL */
455 pVMCB->guest.u8CPL = pCtx->ssHid.Attr.n.u2Dpl;
456
457 /* RAX/EAX too, as VMRUN uses RAX as an implicit parameter. */
458 pVMCB->guest.u64RAX = pCtx->eax;
459
460 /* vmrun will fail otherwise. */
461 pVMCB->guest.u64EFER = MSR_K6_EFER_SVME;
462
463 /** @note We can do more complex things with tagged TLBs. */
464 pVMCB->ctrl.TLBCtrl.n.u32ASID = 1;
465
466 /** TSC offset. */
467 if (TMCpuTickCanUseRealTSC(pVM, &pVMCB->ctrl.u64TSCOffset))
468 pVMCB->ctrl.u32InterceptCtrl1 &= ~SVM_CTRL1_INTERCEPT_RDTSC;
469 else
470 pVMCB->ctrl.u32InterceptCtrl1 |= SVM_CTRL1_INTERCEPT_RDTSC;
471
472 /** @todo 64 bits stuff (?):
473 * - STAR
474 * - LSTAR
475 * - CSTAR
476 * - SFMASK
477 * - KernelGSBase
478 */
479
480#ifdef DEBUG
481 /* Intercept X86_XCPT_DB if stepping is enabled */
482 if (DBGFIsStepping(pVM))
483 pVMCB->ctrl.u32InterceptException |= BIT(1);
484 else
485 pVMCB->ctrl.u32InterceptException &= ~BIT(1);
486#endif
487
488 /* Done. */
489 pVM->hwaccm.s.fContextUseFlags &= ~HWACCM_CHANGED_ALL_GUEST;
490
491 return VINF_SUCCESS;
492}
493
494
495/**
496 * Runs guest code in an SVM VM.
497 *
498 * @todo This can be much more efficient, when we only sync that which has actually changed. (this is the first attempt only)
499 *
500 * @returns VBox status code.
501 * @param pVM The VM to operate on.
502 * @param pCtx Guest context
503 */
504HWACCMR0DECL(int) SVMR0RunGuestCode(PVM pVM, CPUMCTX *pCtx)
505{
506 int rc = VINF_SUCCESS;
507 uint64_t exitCode = (uint64_t)SVM_EXIT_INVALID;
508 SVM_VMCB *pVMCB;
509 bool fForceTLBFlush = false;
510 bool fGuestStateSynced = false;
511
512 STAM_PROFILE_ADV_START(&pVM->hwaccm.s.StatEntry, x);
513
514 pVMCB = (SVM_VMCB *)pVM->hwaccm.s.svm.pVMCB;
515 AssertMsgReturn(pVMCB, ("Invalid pVMCB\n"), VERR_EM_INTERNAL_ERROR);
516
517 /* We can jump to this point to resume execution after determining that a VM-exit is innocent.
518 */
519ResumeExecution:
520
521 /* Check for irq inhibition due to instruction fusing (sti, mov ss). */
522 if (VM_FF_ISSET(pVM, VM_FF_INHIBIT_INTERRUPTS))
523 {
524 Log(("VM_FF_INHIBIT_INTERRUPTS at %VGv successor %VGv\n", pCtx->eip, EMGetInhibitInterruptsPC(pVM)));
525 if (pCtx->eip != EMGetInhibitInterruptsPC(pVM))
526 {
527 /** @note we intentionally don't clear VM_FF_INHIBIT_INTERRUPTS here.
528 * Before we are able to execute this instruction in raw mode (iret to guest code) an external interrupt might
529 * force a world switch again. Possibly allowing a guest interrupt to be dispatched in the process. This could
530 * break the guest. Sounds very unlikely, but such timing sensitive problem are not as rare as you might think.
531 */
532 VM_FF_CLEAR(pVM, VM_FF_INHIBIT_INTERRUPTS);
533 /* Irq inhibition is no longer active; clear the corresponding SVM state. */
534 pVMCB->ctrl.u64IntShadow = 0;
535 }
536 }
537 else
538 {
539 /* Irq inhibition is no longer active; clear the corresponding SVM state. */
540 pVMCB->ctrl.u64IntShadow = 0;
541 }
542
543 /* Check for pending actions that force us to go back to ring 3. */
544#ifdef DEBUG
545 /* Intercept X86_XCPT_DB if stepping is enabled */
546 if (!DBGFIsStepping(pVM))
547#endif
548 {
549 if (VM_FF_ISPENDING(pVM, VM_FF_TO_R3 | VM_FF_TIMER))
550 {
551 VM_FF_CLEAR(pVM, VM_FF_TO_R3);
552 STAM_COUNTER_INC(&pVM->hwaccm.s.StatSwitchToR3);
553 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatEntry, x);
554 rc = VINF_EM_RAW_TO_R3;
555 goto end;
556 }
557 }
558
559 /* Pending request packets might contain actions that need immediate attention, such as pending hardware interrupts. */
560 if (VM_FF_ISPENDING(pVM, VM_FF_REQUEST))
561 {
562 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatEntry, x);
563 rc = VINF_EM_PENDING_REQUEST;
564 goto end;
565 }
566
567 /* When external interrupts are pending, we should exit the VM when IF is set. */
568 /** @note *after* VM_FF_INHIBIT_INTERRUPTS check!!! */
569 rc = SVMR0CheckPendingInterrupt(pVM, pVMCB, pCtx);
570 if (VBOX_FAILURE(rc))
571 {
572 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatEntry, x);
573 goto end;
574 }
575
576 /* Load the guest state */
577 rc = SVMR0LoadGuestState(pVM, pCtx);
578 if (rc != VINF_SUCCESS)
579 {
580 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatEntry, x);
581 goto end;
582 }
583 fGuestStateSynced = true;
584
585 /* All done! Let's start VM execution. */
586 STAM_PROFILE_ADV_START(&pVM->hwaccm.s.StatInGC, x);
587
588 /** Erratum #170 -> must force a TLB flush */
589 /** @todo supposed to be fixed in future by AMD */
590 fForceTLBFlush = true;
591
592 if ( pVM->hwaccm.s.svm.fResumeVM == false
593 || fForceTLBFlush)
594 {
595 pVMCB->ctrl.TLBCtrl.n.u1TLBFlush = 1;
596 }
597 else
598 {
599 pVMCB->ctrl.TLBCtrl.n.u1TLBFlush = 0;
600 }
601 /* In case we execute a goto ResumeExecution later on. */
602 pVM->hwaccm.s.svm.fResumeVM = true;
603 fForceTLBFlush = false;
604
605 Assert(sizeof(pVM->hwaccm.s.svm.pVMCBPhys) == 8);
606 Assert(pVMCB->ctrl.u32InterceptCtrl2 == ( SVM_CTRL2_INTERCEPT_VMRUN /* required */
607 | SVM_CTRL2_INTERCEPT_VMMCALL
608 | SVM_CTRL2_INTERCEPT_VMLOAD
609 | SVM_CTRL2_INTERCEPT_VMSAVE
610 | SVM_CTRL2_INTERCEPT_STGI
611 | SVM_CTRL2_INTERCEPT_CLGI
612 | SVM_CTRL2_INTERCEPT_SKINIT
613 | SVM_CTRL2_INTERCEPT_RDTSCP /* AMD only; we don't support this one */
614 ));
615 Assert(pVMCB->ctrl.IntCtrl.n.u1VIrqMasking);
616 Assert(pVMCB->ctrl.u64IOPMPhysAddr == pVM->hwaccm.s.svm.pIOBitmapPhys);
617 Assert(pVMCB->ctrl.u64MSRPMPhysAddr == pVM->hwaccm.s.svm.pMSRBitmapPhys);
618 Assert(pVMCB->ctrl.u64NestedPaging == 0);
619 Assert(pVMCB->ctrl.u64LBRVirt == 0);
620
621 SVMVMRun(pVM->hwaccm.s.svm.pVMCBHostPhys, pVM->hwaccm.s.svm.pVMCBPhys, pCtx);
622 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatInGC, x);
623
624 /**
625 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
626 * IMPORTANT: WE CAN'T DO ANY LOGGING OR OPERATIONS THAT CAN DO A LONGJMP BACK TO RING 3 *BEFORE* WE'VE SYNCED BACK (MOST OF) THE GUEST STATE
627 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
628 */
629
630 STAM_PROFILE_ADV_START(&pVM->hwaccm.s.StatExit, x);
631
632 /* Reason for the VM exit */
633 exitCode = pVMCB->ctrl.u64ExitCode;
634
635 if (exitCode == (uint64_t)SVM_EXIT_INVALID) /* Invalid guest state. */
636 {
637 HWACCMDumpRegs(pCtx);
638#ifdef DEBUG
639 Log(("ctrl.u16InterceptRdCRx %x\n", pVMCB->ctrl.u16InterceptRdCRx));
640 Log(("ctrl.u16InterceptWrCRx %x\n", pVMCB->ctrl.u16InterceptWrCRx));
641 Log(("ctrl.u16InterceptRdDRx %x\n", pVMCB->ctrl.u16InterceptRdDRx));
642 Log(("ctrl.u16InterceptWrDRx %x\n", pVMCB->ctrl.u16InterceptWrDRx));
643 Log(("ctrl.u32InterceptException %x\n", pVMCB->ctrl.u32InterceptException));
644 Log(("ctrl.u32InterceptCtrl1 %x\n", pVMCB->ctrl.u32InterceptCtrl1));
645 Log(("ctrl.u32InterceptCtrl2 %x\n", pVMCB->ctrl.u32InterceptCtrl2));
646 Log(("ctrl.u64IOPMPhysAddr %VX64\n", pVMCB->ctrl.u64IOPMPhysAddr));
647 Log(("ctrl.u64MSRPMPhysAddr %VX64\n", pVMCB->ctrl.u64MSRPMPhysAddr));
648 Log(("ctrl.u64TSCOffset %VX64\n", pVMCB->ctrl.u64TSCOffset));
649
650 Log(("ctrl.TLBCtrl.u32ASID %x\n", pVMCB->ctrl.TLBCtrl.n.u32ASID));
651 Log(("ctrl.TLBCtrl.u1TLBFlush %x\n", pVMCB->ctrl.TLBCtrl.n.u1TLBFlush));
652 Log(("ctrl.TLBCtrl.u7Reserved %x\n", pVMCB->ctrl.TLBCtrl.n.u7Reserved));
653 Log(("ctrl.TLBCtrl.u24Reserved %x\n", pVMCB->ctrl.TLBCtrl.n.u24Reserved));
654
655 Log(("ctrl.IntCtrl.u8VTPR %x\n", pVMCB->ctrl.IntCtrl.n.u8VTPR));
656 Log(("ctrl.IntCtrl.u1VIrqValid %x\n", pVMCB->ctrl.IntCtrl.n.u1VIrqValid));
657 Log(("ctrl.IntCtrl.u7Reserved %x\n", pVMCB->ctrl.IntCtrl.n.u7Reserved));
658 Log(("ctrl.IntCtrl.u4VIrqPriority %x\n", pVMCB->ctrl.IntCtrl.n.u4VIrqPriority));
659 Log(("ctrl.IntCtrl.u1IgnoreTPR %x\n", pVMCB->ctrl.IntCtrl.n.u1IgnoreTPR));
660 Log(("ctrl.IntCtrl.u3Reserved %x\n", pVMCB->ctrl.IntCtrl.n.u3Reserved));
661 Log(("ctrl.IntCtrl.u1VIrqMasking %x\n", pVMCB->ctrl.IntCtrl.n.u1VIrqMasking));
662 Log(("ctrl.IntCtrl.u7Reserved2 %x\n", pVMCB->ctrl.IntCtrl.n.u7Reserved2));
663 Log(("ctrl.IntCtrl.u8VIrqVector %x\n", pVMCB->ctrl.IntCtrl.n.u8VIrqVector));
664 Log(("ctrl.IntCtrl.u24Reserved %x\n", pVMCB->ctrl.IntCtrl.n.u24Reserved));
665
666 Log(("ctrl.u64IntShadow %VX64\n", pVMCB->ctrl.u64IntShadow));
667 Log(("ctrl.u64ExitCode %VX64\n", pVMCB->ctrl.u64ExitCode));
668 Log(("ctrl.u64ExitInfo1 %VX64\n", pVMCB->ctrl.u64ExitInfo1));
669 Log(("ctrl.u64ExitInfo2 %VX64\n", pVMCB->ctrl.u64ExitInfo2));
670 Log(("ctrl.ExitIntInfo.u8Vector %x\n", pVMCB->ctrl.ExitIntInfo.n.u8Vector));
671 Log(("ctrl.ExitIntInfo.u3Type %x\n", pVMCB->ctrl.ExitIntInfo.n.u3Type));
672 Log(("ctrl.ExitIntInfo.u1ErrorCodeValid %x\n", pVMCB->ctrl.ExitIntInfo.n.u1ErrorCodeValid));
673 Log(("ctrl.ExitIntInfo.u19Reserved %x\n", pVMCB->ctrl.ExitIntInfo.n.u19Reserved));
674 Log(("ctrl.ExitIntInfo.u1Valid %x\n", pVMCB->ctrl.ExitIntInfo.n.u1Valid));
675 Log(("ctrl.ExitIntInfo.u32ErrorCode %x\n", pVMCB->ctrl.ExitIntInfo.n.u32ErrorCode));
676 Log(("ctrl.u64NestedPaging %VX64\n", pVMCB->ctrl.u64NestedPaging));
677 Log(("ctrl.EventInject.u8Vector %x\n", pVMCB->ctrl.EventInject.n.u8Vector));
678 Log(("ctrl.EventInject.u3Type %x\n", pVMCB->ctrl.EventInject.n.u3Type));
679 Log(("ctrl.EventInject.u1ErrorCodeValid %x\n", pVMCB->ctrl.EventInject.n.u1ErrorCodeValid));
680 Log(("ctrl.EventInject.u19Reserved %x\n", pVMCB->ctrl.EventInject.n.u19Reserved));
681 Log(("ctrl.EventInject.u1Valid %x\n", pVMCB->ctrl.EventInject.n.u1Valid));
682 Log(("ctrl.EventInject.u32ErrorCode %x\n", pVMCB->ctrl.EventInject.n.u32ErrorCode));
683
684 Log(("ctrl.u64HostCR3 %VX64\n", pVMCB->ctrl.u64HostCR3));
685 Log(("ctrl.u64LBRVirt %VX64\n", pVMCB->ctrl.u64LBRVirt));
686
687 Log(("guest.CS.u16Sel %04X\n", pVMCB->guest.CS.u16Sel));
688 Log(("guest.CS.u16Attr %04X\n", pVMCB->guest.CS.u16Attr));
689 Log(("guest.CS.u32Limit %X\n", pVMCB->guest.CS.u32Limit));
690 Log(("guest.CS.u64Base %VX64\n", pVMCB->guest.CS.u64Base));
691 Log(("guest.DS.u16Sel %04X\n", pVMCB->guest.DS.u16Sel));
692 Log(("guest.DS.u16Attr %04X\n", pVMCB->guest.DS.u16Attr));
693 Log(("guest.DS.u32Limit %X\n", pVMCB->guest.DS.u32Limit));
694 Log(("guest.DS.u64Base %VX64\n", pVMCB->guest.DS.u64Base));
695 Log(("guest.ES.u16Sel %04X\n", pVMCB->guest.ES.u16Sel));
696 Log(("guest.ES.u16Attr %04X\n", pVMCB->guest.ES.u16Attr));
697 Log(("guest.ES.u32Limit %X\n", pVMCB->guest.ES.u32Limit));
698 Log(("guest.ES.u64Base %VX64\n", pVMCB->guest.ES.u64Base));
699 Log(("guest.FS.u16Sel %04X\n", pVMCB->guest.FS.u16Sel));
700 Log(("guest.FS.u16Attr %04X\n", pVMCB->guest.FS.u16Attr));
701 Log(("guest.FS.u32Limit %X\n", pVMCB->guest.FS.u32Limit));
702 Log(("guest.FS.u64Base %VX64\n", pVMCB->guest.FS.u64Base));
703 Log(("guest.GS.u16Sel %04X\n", pVMCB->guest.GS.u16Sel));
704 Log(("guest.GS.u16Attr %04X\n", pVMCB->guest.GS.u16Attr));
705 Log(("guest.GS.u32Limit %X\n", pVMCB->guest.GS.u32Limit));
706 Log(("guest.GS.u64Base %VX64\n", pVMCB->guest.GS.u64Base));
707
708 Log(("guest.GDTR.u32Limit %X\n", pVMCB->guest.GDTR.u32Limit));
709 Log(("guest.GDTR.u64Base %VX64\n", pVMCB->guest.GDTR.u64Base));
710
711 Log(("guest.LDTR.u16Sel %04X\n", pVMCB->guest.LDTR.u16Sel));
712 Log(("guest.LDTR.u16Attr %04X\n", pVMCB->guest.LDTR.u16Attr));
713 Log(("guest.LDTR.u32Limit %X\n", pVMCB->guest.LDTR.u32Limit));
714 Log(("guest.LDTR.u64Base %VX64\n", pVMCB->guest.LDTR.u64Base));
715
716 Log(("guest.IDTR.u32Limit %X\n", pVMCB->guest.IDTR.u32Limit));
717 Log(("guest.IDTR.u64Base %VX64\n", pVMCB->guest.IDTR.u64Base));
718
719 Log(("guest.TR.u16Sel %04X\n", pVMCB->guest.TR.u16Sel));
720 Log(("guest.TR.u16Attr %04X\n", pVMCB->guest.TR.u16Attr));
721 Log(("guest.TR.u32Limit %X\n", pVMCB->guest.TR.u32Limit));
722 Log(("guest.TR.u64Base %VX64\n", pVMCB->guest.TR.u64Base));
723
724 Log(("guest.u8CPL %X\n", pVMCB->guest.u8CPL));
725 Log(("guest.u64CR0 %VX64\n", pVMCB->guest.u64CR0));
726 Log(("guest.u64CR2 %VX64\n", pVMCB->guest.u64CR2));
727 Log(("guest.u64CR3 %VX64\n", pVMCB->guest.u64CR3));
728 Log(("guest.u64CR4 %VX64\n", pVMCB->guest.u64CR4));
729 Log(("guest.u64DR6 %VX64\n", pVMCB->guest.u64DR6));
730 Log(("guest.u64DR7 %VX64\n", pVMCB->guest.u64DR7));
731
732 Log(("guest.u64RIP %VX64\n", pVMCB->guest.u64RIP));
733 Log(("guest.u64RSP %VX64\n", pVMCB->guest.u64RSP));
734 Log(("guest.u64RAX %VX64\n", pVMCB->guest.u64RAX));
735 Log(("guest.u64RFlags %VX64\n", pVMCB->guest.u64RFlags));
736
737 Log(("guest.u64SysEnterCS %VX64\n", pVMCB->guest.u64SysEnterCS));
738 Log(("guest.u64SysEnterEIP %VX64\n", pVMCB->guest.u64SysEnterEIP));
739 Log(("guest.u64SysEnterESP %VX64\n", pVMCB->guest.u64SysEnterESP));
740
741 Log(("guest.u64EFER %VX64\n", pVMCB->guest.u64EFER));
742 Log(("guest.u64STAR %VX64\n", pVMCB->guest.u64STAR));
743 Log(("guest.u64LSTAR %VX64\n", pVMCB->guest.u64LSTAR));
744 Log(("guest.u64CSTAR %VX64\n", pVMCB->guest.u64CSTAR));
745 Log(("guest.u64SFMASK %VX64\n", pVMCB->guest.u64SFMASK));
746 Log(("guest.u64KernelGSBase %VX64\n", pVMCB->guest.u64KernelGSBase));
747 Log(("guest.u64GPAT %VX64\n", pVMCB->guest.u64GPAT));
748 Log(("guest.u64DBGCTL %VX64\n", pVMCB->guest.u64DBGCTL));
749 Log(("guest.u64BR_FROM %VX64\n", pVMCB->guest.u64BR_FROM));
750 Log(("guest.u64BR_TO %VX64\n", pVMCB->guest.u64BR_TO));
751 Log(("guest.u64LASTEXCPFROM %VX64\n", pVMCB->guest.u64LASTEXCPFROM));
752 Log(("guest.u64LASTEXCPTO %VX64\n", pVMCB->guest.u64LASTEXCPTO));
753
754#endif
755 rc = VERR_SVM_UNABLE_TO_START_VM;
756 goto end;
757 }
758
759 /* Let's first sync back eip, esp, and eflags. */
760 pCtx->eip = pVMCB->guest.u64RIP;
761 pCtx->esp = pVMCB->guest.u64RSP;
762 pCtx->eflags.u32 = pVMCB->guest.u64RFlags;
763 /* eax is saved/restore across the vmrun instruction */
764 pCtx->eax = pVMCB->guest.u64RAX;
765
766 /* Guest CPU context: ES, CS, SS, DS, FS, GS. */
767 SVM_READ_SELREG(SS, ss);
768 SVM_READ_SELREG(CS, cs);
769 SVM_READ_SELREG(DS, ds);
770 SVM_READ_SELREG(ES, es);
771 SVM_READ_SELREG(FS, fs);
772 SVM_READ_SELREG(GS, gs);
773
774 /** @note no reason to sync back the CRx and DRx registers. They can't be changed by the guest. */
775
776 /** @note NOW IT'S SAFE FOR LOGGING! */
777
778 /* Take care of instruction fusing (sti, mov ss) */
779 if (pVMCB->ctrl.u64IntShadow & SVM_INTERRUPT_SHADOW_ACTIVE)
780 {
781 Log(("uInterruptState %x eip=%VGv\n", pVMCB->ctrl.u64IntShadow, pCtx->eip));
782 EMSetInhibitInterruptsPC(pVM, pCtx->eip);
783 }
784 else
785 VM_FF_CLEAR(pVM, VM_FF_INHIBIT_INTERRUPTS);
786
787 Log2(("exitCode = %x\n", exitCode));
788
789 /* Check if an injected event was interrupted prematurely. */
790 pVM->hwaccm.s.Event.intInfo = pVMCB->ctrl.ExitIntInfo.au64[0];
791 if ( pVMCB->ctrl.ExitIntInfo.n.u1Valid
792 && pVMCB->ctrl.ExitIntInfo.n.u3Type != SVM_EVENT_SOFTWARE_INT /* we don't care about 'int xx' as the instruction will be restarted. */)
793 {
794 Log(("Pending inject %VX64 at %08x exit=%08x\n", pVM->hwaccm.s.Event.intInfo, pCtx->eip, exitCode));
795 pVM->hwaccm.s.Event.fPending = true;
796 /* Error code present? (redundant) */
797 if (pVMCB->ctrl.ExitIntInfo.n.u1ErrorCodeValid)
798 {
799 pVM->hwaccm.s.Event.errCode = pVMCB->ctrl.ExitIntInfo.n.u32ErrorCode;
800 }
801 else
802 pVM->hwaccm.s.Event.errCode = 0;
803 }
804 STAM_COUNTER_INC(&pVM->hwaccm.s.pStatExitReasonR0[exitCode & MASK_EXITREASON_STAT]);
805
806 /* Deal with the reason of the VM-exit. */
807 switch (exitCode)
808 {
809 case SVM_EXIT_EXCEPTION_0: case SVM_EXIT_EXCEPTION_1: case SVM_EXIT_EXCEPTION_2: case SVM_EXIT_EXCEPTION_3:
810 case SVM_EXIT_EXCEPTION_4: case SVM_EXIT_EXCEPTION_5: case SVM_EXIT_EXCEPTION_6: case SVM_EXIT_EXCEPTION_7:
811 case SVM_EXIT_EXCEPTION_8: case SVM_EXIT_EXCEPTION_9: case SVM_EXIT_EXCEPTION_A: case SVM_EXIT_EXCEPTION_B:
812 case SVM_EXIT_EXCEPTION_C: case SVM_EXIT_EXCEPTION_D: case SVM_EXIT_EXCEPTION_E: case SVM_EXIT_EXCEPTION_F:
813 case SVM_EXIT_EXCEPTION_10: case SVM_EXIT_EXCEPTION_11: case SVM_EXIT_EXCEPTION_12: case SVM_EXIT_EXCEPTION_13:
814 case SVM_EXIT_EXCEPTION_14: case SVM_EXIT_EXCEPTION_15: case SVM_EXIT_EXCEPTION_16: case SVM_EXIT_EXCEPTION_17:
815 case SVM_EXIT_EXCEPTION_18: case SVM_EXIT_EXCEPTION_19: case SVM_EXIT_EXCEPTION_1A: case SVM_EXIT_EXCEPTION_1B:
816 case SVM_EXIT_EXCEPTION_1C: case SVM_EXIT_EXCEPTION_1D: case SVM_EXIT_EXCEPTION_1E: case SVM_EXIT_EXCEPTION_1F:
817 {
818 /* Pending trap. */
819 SVM_EVENT Event;
820 uint32_t vector = exitCode - SVM_EXIT_EXCEPTION_0;
821
822 Log2(("Hardware/software interrupt %d\n", vector));
823 switch (vector)
824 {
825#ifdef DEBUG
826 case X86_XCPT_DB:
827 rc = DBGFR0Trap01Handler(pVM, CPUMCTX2CORE(pCtx), pVMCB->guest.u64DR6);
828 Assert(rc != VINF_EM_RAW_GUEST_TRAP);
829 break;
830#endif
831
832 case X86_XCPT_NM:
833 {
834 uint32_t oldCR0;
835
836 Log(("#NM fault at %VGv\n", pCtx->eip));
837
838 /** @todo don't intercept #NM exceptions anymore when we've activated the guest FPU state. */
839 oldCR0 = ASMGetCR0();
840 /* If we sync the FPU/XMM state on-demand, then we can continue execution as if nothing has happened. */
841 rc = CPUMHandleLazyFPU(pVM);
842 if (rc == VINF_SUCCESS)
843 {
844 Assert(CPUMIsGuestFPUStateActive(pVM));
845
846 /* CPUMHandleLazyFPU could have changed CR0; restore it. */
847 ASMSetCR0(oldCR0);
848
849 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitShadowNM);
850
851 /* Continue execution. */
852 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
853 pVM->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_CR0;
854
855 goto ResumeExecution;
856 }
857
858 Log(("Forward #NM fault to the guest\n"));
859 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestNM);
860
861 Event.au64[0] = 0;
862 Event.n.u3Type = SVM_EVENT_EXCEPTION;
863 Event.n.u1Valid = 1;
864 Event.n.u8Vector = X86_XCPT_NM;
865
866 SVMR0InjectEvent(pVM, pVMCB, pCtx, &Event);
867 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
868 goto ResumeExecution;
869 }
870
871 case X86_XCPT_PF: /* Page fault */
872 {
873 uint32_t errCode = pVMCB->ctrl.u64ExitInfo1; /* EXITINFO1 = error code */
874 RTGCUINTPTR uFaultAddress = pVMCB->ctrl.u64ExitInfo2; /* EXITINFO2 = fault address */
875
876 Log2(("Page fault at %VGv cr2=%VGv error code %x\n", pCtx->eip, uFaultAddress, errCode));
877 /* Exit qualification contains the linear address of the page fault. */
878 TRPMAssertTrap(pVM, X86_XCPT_PF, TRPM_TRAP);
879 TRPMSetErrorCode(pVM, errCode);
880 TRPMSetFaultAddress(pVM, uFaultAddress);
881
882 /* Forward it to our trap handler first, in case our shadow pages are out of sync. */
883 rc = PGMTrap0eHandler(pVM, errCode, CPUMCTX2CORE(pCtx), (RTGCPTR)uFaultAddress);
884 Log2(("PGMTrap0eHandler %VGv returned %Vrc\n", pCtx->eip, rc));
885 if (rc == VINF_SUCCESS)
886 { /* We've successfully synced our shadow pages, so let's just continue execution. */
887 Log2(("Shadow page fault at %VGv cr2=%VGv error code %x\n", pCtx->eip, uFaultAddress, errCode));
888 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitShadowPF);
889
890 TRPMResetTrap(pVM);
891
892 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
893 goto ResumeExecution;
894 }
895 else
896 if (rc == VINF_EM_RAW_GUEST_TRAP)
897 { /* A genuine pagefault.
898 * Forward the trap to the guest by injecting the exception and resuming execution.
899 */
900 Log2(("Forward page fault to the guest\n"));
901 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestPF);
902 /* The error code might have been changed. */
903 errCode = TRPMGetErrorCode(pVM);
904
905 TRPMResetTrap(pVM);
906
907 /* Now we must update CR2. */
908 pCtx->cr2 = uFaultAddress;
909
910 Event.au64[0] = 0;
911 Event.n.u3Type = SVM_EVENT_EXCEPTION;
912 Event.n.u1Valid = 1;
913 Event.n.u8Vector = X86_XCPT_PF;
914 Event.n.u1ErrorCodeValid = 1;
915 Event.n.u32ErrorCode = errCode;
916
917 SVMR0InjectEvent(pVM, pVMCB, pCtx, &Event);
918
919 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
920 goto ResumeExecution;
921 }
922#ifdef VBOX_STRICT
923 if (rc != VINF_EM_RAW_EMULATE_INSTR)
924 Log(("PGMTrap0eHandler failed with %d\n", rc));
925#endif
926 /* Need to go back to the recompiler to emulate the instruction. */
927 TRPMResetTrap(pVM);
928 break;
929 }
930
931 case X86_XCPT_MF: /* Floating point exception. */
932 {
933 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestMF);
934 if (!(pCtx->cr0 & X86_CR0_NE))
935 {
936 /* old style FPU error reporting needs some extra work. */
937 /** @todo don't fall back to the recompiler, but do it manually. */
938 rc = VINF_EM_RAW_EMULATE_INSTR;
939 break;
940 }
941 Log(("Trap %x at %VGv\n", vector, pCtx->eip));
942
943 Event.au64[0] = 0;
944 Event.n.u3Type = SVM_EVENT_EXCEPTION;
945 Event.n.u1Valid = 1;
946 Event.n.u8Vector = X86_XCPT_MF;
947
948 SVMR0InjectEvent(pVM, pVMCB, pCtx, &Event);
949
950 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
951 goto ResumeExecution;
952 }
953
954#ifdef VBOX_STRICT
955 case X86_XCPT_GP: /* General protection failure exception.*/
956 case X86_XCPT_UD: /* Unknown opcode exception. */
957 case X86_XCPT_DE: /* Debug exception. */
958 case X86_XCPT_SS: /* Stack segment exception. */
959 case X86_XCPT_NP: /* Segment not present exception. */
960 {
961 Event.au64[0] = 0;
962 Event.n.u3Type = SVM_EVENT_EXCEPTION;
963 Event.n.u1Valid = 1;
964 Event.n.u8Vector = vector;
965
966 switch(vector)
967 {
968 case X86_XCPT_GP:
969 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestGP);
970 Event.n.u1ErrorCodeValid = 1;
971 Event.n.u32ErrorCode = pVMCB->ctrl.u64ExitInfo1; /* EXITINFO1 = error code */
972 break;
973 case X86_XCPT_DE:
974 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestDE);
975 break;
976 case X86_XCPT_UD:
977 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestUD);
978 break;
979 case X86_XCPT_SS:
980 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestSS);
981 Event.n.u1ErrorCodeValid = 1;
982 Event.n.u32ErrorCode = pVMCB->ctrl.u64ExitInfo1; /* EXITINFO1 = error code */
983 break;
984 case X86_XCPT_NP:
985 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestNP);
986 Event.n.u1ErrorCodeValid = 1;
987 Event.n.u32ErrorCode = pVMCB->ctrl.u64ExitInfo1; /* EXITINFO1 = error code */
988 break;
989 }
990 Log(("Trap %x at %VGv\n", vector, pCtx->eip));
991 SVMR0InjectEvent(pVM, pVMCB, pCtx, &Event);
992
993 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
994 goto ResumeExecution;
995 }
996#endif
997 default:
998 AssertMsgFailed(("Unexpected vm-exit caused by exception %x\n", vector));
999 rc = VERR_EM_INTERNAL_ERROR;
1000 break;
1001
1002 } /* switch (vector) */
1003 break;
1004 }
1005
1006 case SVM_EXIT_FERR_FREEZE:
1007 case SVM_EXIT_INTR:
1008 case SVM_EXIT_NMI:
1009 case SVM_EXIT_SMI:
1010 case SVM_EXIT_INIT:
1011 case SVM_EXIT_VINTR:
1012 /* External interrupt; leave to allow it to be dispatched again. */
1013 rc = VINF_EM_RAW_INTERRUPT;
1014 break;
1015
1016 case SVM_EXIT_INVD: /* Guest software attempted to execute INVD. */
1017 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitInvd);
1018 /* Skip instruction and continue directly. */
1019 pCtx->eip += 2; /** @note hardcoded opcode size! */
1020 /* Continue execution.*/
1021 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1022 goto ResumeExecution;
1023
1024 case SVM_EXIT_CPUID: /* Guest software attempted to execute CPUID. */
1025 {
1026 Log2(("SVM: Cpuid %x\n", pCtx->eax));
1027 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitCpuid);
1028 rc = EMInterpretCpuId(pVM, CPUMCTX2CORE(pCtx));
1029 if (rc == VINF_SUCCESS)
1030 {
1031 /* Update EIP and continue execution. */
1032 pCtx->eip += 2; /** @note hardcoded opcode size! */
1033 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1034 goto ResumeExecution;
1035 }
1036 AssertMsgFailed(("EMU: cpuid failed with %Vrc\n", rc));
1037 rc = VINF_EM_RAW_EMULATE_INSTR;
1038 break;
1039 }
1040
1041 case SVM_EXIT_RDTSC: /* Guest software attempted to execute RDTSC. */
1042 {
1043 Log2(("SVM: Rdtsc\n"));
1044 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitRdtsc);
1045 rc = EMInterpretRdtsc(pVM, CPUMCTX2CORE(pCtx));
1046 if (rc == VINF_SUCCESS)
1047 {
1048 /* Update EIP and continue execution. */
1049 pCtx->eip += 2; /** @note hardcoded opcode size! */
1050 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1051 goto ResumeExecution;
1052 }
1053 AssertMsgFailed(("EMU: rdtsc failed with %Vrc\n", rc));
1054 rc = VINF_EM_RAW_EMULATE_INSTR;
1055 break;
1056 }
1057
1058 case SVM_EXIT_INVLPG: /* Guest software attempted to execute INVPG. */
1059 {
1060 Log2(("SVM: invlpg\n"));
1061 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitInvpg);
1062
1063 /* Truly a pita. Why can't SVM give the same information as VMX? */
1064 rc = SVMR0InterpretInvpg(pVM, CPUMCTX2CORE(pCtx), pVMCB->ctrl.TLBCtrl.n.u32ASID);
1065 if (rc == VINF_SUCCESS)
1066 goto ResumeExecution; /* eip already updated */
1067 break;
1068 }
1069
1070 case SVM_EXIT_WRITE_CR0: case SVM_EXIT_WRITE_CR1: case SVM_EXIT_WRITE_CR2: case SVM_EXIT_WRITE_CR3:
1071 case SVM_EXIT_WRITE_CR4: case SVM_EXIT_WRITE_CR5: case SVM_EXIT_WRITE_CR6: case SVM_EXIT_WRITE_CR7:
1072 case SVM_EXIT_WRITE_CR8: case SVM_EXIT_WRITE_CR9: case SVM_EXIT_WRITE_CR10: case SVM_EXIT_WRITE_CR11:
1073 case SVM_EXIT_WRITE_CR12: case SVM_EXIT_WRITE_CR13: case SVM_EXIT_WRITE_CR14: case SVM_EXIT_WRITE_CR15:
1074 {
1075 uint32_t cbSize;
1076
1077 Log2(("SVM: %VGv mov cr%d, \n", pCtx->eip, exitCode - SVM_EXIT_WRITE_CR0));
1078 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitCRxWrite);
1079 rc = EMInterpretInstruction(pVM, CPUMCTX2CORE(pCtx), 0, &cbSize);
1080
1081 switch (exitCode - SVM_EXIT_WRITE_CR0)
1082 {
1083 case 0:
1084 pVM->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_CR0;
1085 break;
1086 case 2:
1087 break;
1088 case 3:
1089 pVM->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_CR3;
1090 break;
1091 case 4:
1092 pVM->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_CR4;
1093 break;
1094 default:
1095 AssertFailed();
1096 }
1097 /* Check if a sync operation is pending. */
1098 if ( rc == VINF_SUCCESS /* don't bother if we are going to ring 3 anyway */
1099 && VM_FF_ISPENDING(pVM, VM_FF_PGM_SYNC_CR3 | VM_FF_PGM_SYNC_CR3_NON_GLOBAL))
1100 {
1101 rc = PGMSyncCR3(pVM, CPUMGetGuestCR0(pVM), CPUMGetGuestCR3(pVM), CPUMGetGuestCR4(pVM), VM_FF_ISSET(pVM, VM_FF_PGM_SYNC_CR3));
1102 AssertRC(rc);
1103
1104 /** @note Force a TLB flush. SVM requires us to do it manually. */
1105 fForceTLBFlush = true;
1106 }
1107 if (rc == VINF_SUCCESS)
1108 {
1109 /* EIP has been updated already. */
1110
1111 /* Only resume if successful. */
1112 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1113 goto ResumeExecution;
1114 }
1115 Assert(rc == VERR_EM_INTERPRETER || rc == VINF_PGM_CHANGE_MODE || rc == VINF_PGM_SYNC_CR3);
1116 break;
1117 }
1118
1119 case SVM_EXIT_READ_CR0: case SVM_EXIT_READ_CR1: case SVM_EXIT_READ_CR2: case SVM_EXIT_READ_CR3:
1120 case SVM_EXIT_READ_CR4: case SVM_EXIT_READ_CR5: case SVM_EXIT_READ_CR6: case SVM_EXIT_READ_CR7:
1121 case SVM_EXIT_READ_CR8: case SVM_EXIT_READ_CR9: case SVM_EXIT_READ_CR10: case SVM_EXIT_READ_CR11:
1122 case SVM_EXIT_READ_CR12: case SVM_EXIT_READ_CR13: case SVM_EXIT_READ_CR14: case SVM_EXIT_READ_CR15:
1123 {
1124 uint32_t cbSize;
1125
1126 Log2(("SVM: %VGv mov x, cr%d\n", pCtx->eip, exitCode - SVM_EXIT_READ_CR0));
1127 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitCRxRead);
1128 rc = EMInterpretInstruction(pVM, CPUMCTX2CORE(pCtx), 0, &cbSize);
1129 if (rc == VINF_SUCCESS)
1130 {
1131 /* EIP has been updated already. */
1132
1133 /* Only resume if successful. */
1134 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1135 goto ResumeExecution;
1136 }
1137 Assert(rc == VERR_EM_INTERPRETER || rc == VINF_PGM_CHANGE_MODE || rc == VINF_PGM_SYNC_CR3);
1138 break;
1139 }
1140
1141 case SVM_EXIT_WRITE_DR0: case SVM_EXIT_WRITE_DR1: case SVM_EXIT_WRITE_DR2: case SVM_EXIT_WRITE_DR3:
1142 case SVM_EXIT_WRITE_DR4: case SVM_EXIT_WRITE_DR5: case SVM_EXIT_WRITE_DR6: case SVM_EXIT_WRITE_DR7:
1143 case SVM_EXIT_WRITE_DR8: case SVM_EXIT_WRITE_DR9: case SVM_EXIT_WRITE_DR10: case SVM_EXIT_WRITE_DR11:
1144 case SVM_EXIT_WRITE_DR12: case SVM_EXIT_WRITE_DR13: case SVM_EXIT_WRITE_DR14: case SVM_EXIT_WRITE_DR15:
1145 {
1146 uint32_t cbSize;
1147
1148 Log2(("SVM: %VGv mov dr%d, x\n", pCtx->eip, exitCode - SVM_EXIT_WRITE_DR0));
1149 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitDRxRead);
1150 rc = EMInterpretInstruction(pVM, CPUMCTX2CORE(pCtx), 0, &cbSize);
1151 if (rc == VINF_SUCCESS)
1152 {
1153 /* EIP has been updated already. */
1154
1155 /* Only resume if successful. */
1156 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1157 goto ResumeExecution;
1158 }
1159 Assert(rc == VERR_EM_INTERPRETER || rc == VINF_PGM_CHANGE_MODE || rc == VINF_PGM_SYNC_CR3);
1160 break;
1161 }
1162
1163 case SVM_EXIT_READ_DR0: case SVM_EXIT_READ_DR1: case SVM_EXIT_READ_DR2: case SVM_EXIT_READ_DR3:
1164 case SVM_EXIT_READ_DR4: case SVM_EXIT_READ_DR5: case SVM_EXIT_READ_DR6: case SVM_EXIT_READ_DR7:
1165 case SVM_EXIT_READ_DR8: case SVM_EXIT_READ_DR9: case SVM_EXIT_READ_DR10: case SVM_EXIT_READ_DR11:
1166 case SVM_EXIT_READ_DR12: case SVM_EXIT_READ_DR13: case SVM_EXIT_READ_DR14: case SVM_EXIT_READ_DR15:
1167 {
1168 uint32_t cbSize;
1169
1170 Log2(("SVM: %VGv mov dr%d, x\n", pCtx->eip, exitCode - SVM_EXIT_READ_DR0));
1171 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitDRxRead);
1172 rc = EMInterpretInstruction(pVM, CPUMCTX2CORE(pCtx), 0, &cbSize);
1173 if (rc == VINF_SUCCESS)
1174 {
1175 /* EIP has been updated already. */
1176
1177 /* Only resume if successful. */
1178 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1179 goto ResumeExecution;
1180 }
1181 Assert(rc == VERR_EM_INTERPRETER || rc == VINF_PGM_CHANGE_MODE || rc == VINF_PGM_SYNC_CR3);
1182 break;
1183 }
1184
1185 /* Note: We'll get a #GP if the IO instruction isn't allowed (IOPL or TSS bitmap); no need to double check. */
1186 case SVM_EXIT_IOIO: /* I/O instruction. */
1187 {
1188 SVM_IOIO_EXIT IoExitInfo;
1189 uint32_t uIOSize, uAndVal;
1190
1191 IoExitInfo.au32[0] = pVMCB->ctrl.u64ExitInfo1;
1192
1193 /** @todo could use a lookup table here */
1194 if (IoExitInfo.n.u1OP8)
1195 {
1196 uIOSize = 1;
1197 uAndVal = 0xff;
1198 }
1199 else
1200 if (IoExitInfo.n.u1OP16)
1201 {
1202 uIOSize = 2;
1203 uAndVal = 0xffff;
1204 }
1205 else
1206 if (IoExitInfo.n.u1OP32)
1207 {
1208 uIOSize = 4;
1209 uAndVal = 0xffffffff;
1210 }
1211 else
1212 {
1213 AssertFailed(); /* should be fatal. */
1214 rc = VINF_EM_RAW_EMULATE_INSTR;
1215 break;
1216 }
1217
1218 if (IoExitInfo.n.u1STR)
1219 {
1220#if 1
1221 /** @todo the ring 3 emulation somehow causes the host to hang during e.g. nt4 installation; fall back to the recompiler */
1222 rc = VINF_EM_RAW_EMULATE_INSTR;
1223 break;
1224#else
1225 /* ins/outs */
1226 uint32_t prefix = 0;
1227 if (IoExitInfo.n.u1REP)
1228 prefix |= PREFIX_REP;
1229
1230 if (IoExitInfo.n.u1Type == 0)
1231 {
1232 Log2(("IOMInterpretOUTSEx %VGv %x size=%d\n", pCtx->eip, IoExitInfo.n.u16Port, uIOSize));
1233 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitIOStringWrite);
1234 rc = IOMInterpretOUTSEx(pVM, CPUMCTX2CORE(pCtx), IoExitInfo.n.u16Port, prefix, uIOSize);
1235 }
1236 else
1237 {
1238 Log2(("IOMInterpretINSEx %VGv %x size=%d\n", pCtx->eip, IoExitInfo.n.u16Port, uIOSize));
1239 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitIOStringRead);
1240 rc = IOMInterpretINSEx(pVM, CPUMCTX2CORE(pCtx), IoExitInfo.n.u16Port, prefix, uIOSize);
1241 }
1242#endif
1243 }
1244 else
1245 {
1246 /* normal in/out */
1247 Assert(!IoExitInfo.n.u1REP);
1248
1249 if (IoExitInfo.n.u1Type == 0)
1250 {
1251 Log2(("IOMIOPortWrite %VGv %x %x size=%d\n", pCtx->eip, IoExitInfo.n.u16Port, pCtx->eax & uAndVal, uIOSize));
1252 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitIOWrite);
1253 rc = IOMIOPortWrite(pVM, IoExitInfo.n.u16Port, pCtx->eax & uAndVal, uIOSize);
1254 }
1255 else
1256 {
1257 uint32_t u32Val = 0;
1258
1259 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitIORead);
1260 rc = IOMIOPortRead(pVM, IoExitInfo.n.u16Port, &u32Val, uIOSize);
1261 if (IOM_SUCCESS(rc))
1262 {
1263 /* Write back to the EAX register. */
1264 pCtx->eax = (pCtx->eax & ~uAndVal) | (u32Val & uAndVal);
1265 Log2(("IOMIOPortRead %VGv %x %x size=%d\n", pCtx->eip, IoExitInfo.n.u16Port, u32Val & uAndVal, uIOSize));
1266 }
1267 }
1268 }
1269 /*
1270 * Handled the I/O return codes.
1271 * (The unhandled cases end up with rc == VINF_EM_RAW_EMULATE_INSTR.)
1272 */
1273 if (IOM_SUCCESS(rc))
1274 {
1275 /* Update EIP and continue execution. */
1276 pCtx->eip = pVMCB->ctrl.u64ExitInfo2; /* RIP/EIP of the next instruction is saved in EXITINFO2. */
1277 if (RT_LIKELY(rc == VINF_SUCCESS))
1278 {
1279 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1280 goto ResumeExecution;
1281 }
1282 Log2(("EM status from IO at %VGv %x size %d: %Vrc\n", pCtx->eip, IoExitInfo.n.u16Port, uIOSize, rc));
1283 break;
1284 }
1285
1286#ifdef VBOX_STRICT
1287 if (rc == VINF_IOM_HC_IOPORT_READ)
1288 Assert(IoExitInfo.n.u1Type != 0);
1289 else if (rc == VINF_IOM_HC_IOPORT_WRITE)
1290 Assert(IoExitInfo.n.u1Type == 0);
1291 else
1292 AssertMsg(VBOX_FAILURE(rc) || rc == VINF_EM_RAW_EMULATE_INSTR || rc == VINF_EM_RAW_GUEST_TRAP || rc == VINF_TRPM_XCPT_DISPATCHED, ("%Vrc\n", rc));
1293#endif
1294 Log2(("Failed IO at %VGv %x size %d\n", pCtx->eip, IoExitInfo.n.u16Port, uIOSize));
1295 break;
1296 }
1297
1298 case SVM_EXIT_HLT:
1299 /** Check if external interrupts are pending; if so, don't switch back. */
1300 if (VM_FF_ISPENDING(pVM, (VM_FF_INTERRUPT_APIC|VM_FF_INTERRUPT_PIC)))
1301 {
1302 pCtx->eip++; /* skip hlt */
1303 goto ResumeExecution;
1304 }
1305
1306 rc = VINF_EM_RAW_EMULATE_INSTR_HLT;
1307 break;
1308
1309 case SVM_EXIT_RDPMC:
1310 case SVM_EXIT_RSM:
1311 case SVM_EXIT_INVLPGA:
1312 case SVM_EXIT_VMRUN:
1313 case SVM_EXIT_VMMCALL:
1314 case SVM_EXIT_VMLOAD:
1315 case SVM_EXIT_VMSAVE:
1316 case SVM_EXIT_STGI:
1317 case SVM_EXIT_CLGI:
1318 case SVM_EXIT_SKINIT:
1319 case SVM_EXIT_RDTSCP:
1320 {
1321 /* Unsupported instructions. */
1322 SVM_EVENT Event;
1323
1324 Event.au64[0] = 0;
1325 Event.n.u3Type = SVM_EVENT_EXCEPTION;
1326 Event.n.u1Valid = 1;
1327 Event.n.u8Vector = X86_XCPT_UD;
1328
1329 Log(("Forced #UD trap at %VGv\n", pCtx->eip));
1330 SVMR0InjectEvent(pVM, pVMCB, pCtx, &Event);
1331
1332 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1333 goto ResumeExecution;
1334 }
1335
1336 /* Emulate RDMSR & WRMSR in ring 3. */
1337 case SVM_EXIT_MSR:
1338 rc = VINF_EM_RAW_EXCEPTION_PRIVILEGED;
1339 break;
1340
1341 case SVM_EXIT_NPF:
1342 AssertFailed(); /* unexpected */
1343 break;
1344
1345 case SVM_EXIT_SHUTDOWN:
1346 rc = VINF_EM_RESET; /* Triple fault equals a reset. */
1347 break;
1348
1349 case SVM_EXIT_PAUSE:
1350 case SVM_EXIT_IDTR_READ:
1351 case SVM_EXIT_GDTR_READ:
1352 case SVM_EXIT_LDTR_READ:
1353 case SVM_EXIT_TR_READ:
1354 case SVM_EXIT_IDTR_WRITE:
1355 case SVM_EXIT_GDTR_WRITE:
1356 case SVM_EXIT_LDTR_WRITE:
1357 case SVM_EXIT_TR_WRITE:
1358 case SVM_EXIT_CR0_SEL_WRITE:
1359 default:
1360 /* Unexpected exit codes. */
1361 rc = VERR_EM_INTERNAL_ERROR;
1362 AssertMsgFailed(("Unexpected exit code %x\n", exitCode)); /* Can't happen. */
1363 break;
1364 }
1365
1366end:
1367 if (fGuestStateSynced)
1368 {
1369 /* Remaining guest CPU context: TR, IDTR, GDTR, LDTR. */
1370 SVM_READ_SELREG(LDTR, ldtr);
1371 SVM_READ_SELREG(TR, tr);
1372
1373 pCtx->gdtr.cbGdt = pVMCB->guest.GDTR.u32Limit;
1374 pCtx->gdtr.pGdt = pVMCB->guest.GDTR.u64Base;
1375
1376 pCtx->idtr.cbIdt = pVMCB->guest.IDTR.u32Limit;
1377 pCtx->idtr.pIdt = pVMCB->guest.IDTR.u64Base;
1378
1379 /*
1380 * System MSRs
1381 */
1382 pCtx->SysEnter.cs = pVMCB->guest.u64SysEnterCS;
1383 pCtx->SysEnter.eip = pVMCB->guest.u64SysEnterEIP;
1384 pCtx->SysEnter.esp = pVMCB->guest.u64SysEnterESP;
1385 }
1386
1387 /* Signal changes for the recompiler. */
1388 CPUMSetChangedFlags(pVM, CPUM_CHANGED_SYSENTER_MSR | CPUM_CHANGED_LDTR | CPUM_CHANGED_GDTR | CPUM_CHANGED_IDTR | CPUM_CHANGED_TR | CPUM_CHANGED_HIDDEN_SEL_REGS);
1389
1390 /* If we executed vmrun and an external irq was pending, then we don't have to do a full sync the next time. */
1391 if (exitCode == SVM_EXIT_INTR)
1392 {
1393 STAM_COUNTER_INC(&pVM->hwaccm.s.StatPendingHostIrq);
1394 /* On the next entry we'll only sync the host context. */
1395 pVM->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_HOST_CONTEXT;
1396 }
1397 else
1398 {
1399 /* On the next entry we'll sync everything. */
1400 /** @todo we can do better than this */
1401 pVM->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_ALL;
1402 }
1403
1404 /* translate into a less severe return code */
1405 if (rc == VERR_EM_INTERPRETER)
1406 rc = VINF_EM_RAW_EMULATE_INSTR;
1407
1408 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1409 return rc;
1410}
1411
1412/**
1413 * Enable SVM
1414 *
1415 * @returns VBox status code.
1416 * @param pVM The VM to operate on.
1417 */
1418HWACCMR0DECL(int) SVMR0Enable(PVM pVM)
1419{
1420 uint64_t val;
1421
1422 Assert(pVM->hwaccm.s.svm.fSupported);
1423
1424 /* We must turn on SVM and setup the host state physical address, as those MSRs are per-cpu/core. */
1425
1426 /* Turn on SVM in the EFER MSR. */
1427 val = ASMRdMsr(MSR_K6_EFER);
1428 if (!(val & MSR_K6_EFER_SVME))
1429 ASMWrMsr(MSR_K6_EFER, val | MSR_K6_EFER_SVME);
1430
1431 /* Write the physical page address where the CPU will store the host state while executing the VM. */
1432 ASMWrMsr(MSR_K8_VM_HSAVE_PA, pVM->hwaccm.s.svm.pHStatePhys);
1433
1434 /* Force a TLB flush on VM entry. */
1435 pVM->hwaccm.s.svm.fResumeVM = false;
1436
1437 /* Force to reload LDTR, so we'll execute VMLoad to load additional guest state. */
1438 pVM->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_LDTR;
1439
1440 return VINF_SUCCESS;
1441}
1442
1443
1444/**
1445 * Disable SVM
1446 *
1447 * @returns VBox status code.
1448 * @param pVM The VM to operate on.
1449 */
1450HWACCMR0DECL(int) SVMR0Disable(PVM pVM)
1451{
1452 /** @todo hopefully this is not very expensive. */
1453
1454 /* Turn off SVM in the EFER MSR. */
1455 uint64_t val = ASMRdMsr(MSR_K6_EFER);
1456 ASMWrMsr(MSR_K6_EFER, val & ~MSR_K6_EFER_SVME);
1457
1458 /* Invalidate host state physical address. */
1459 ASMWrMsr(MSR_K8_VM_HSAVE_PA, 0);
1460
1461 Assert(pVM->hwaccm.s.svm.fSupported);
1462 return VINF_SUCCESS;
1463}
1464
1465
1466static int svmInterpretInvlPg(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, uint32_t uASID)
1467{
1468 OP_PARAMVAL param1;
1469 RTGCPTR addr;
1470
1471 int rc = DISQueryParamVal(pRegFrame, pCpu, &pCpu->param1, &param1, PARAM_SOURCE);
1472 if(VBOX_FAILURE(rc))
1473 return VERR_EM_INTERPRETER;
1474
1475 switch(param1.type)
1476 {
1477 case PARMTYPE_IMMEDIATE:
1478 case PARMTYPE_ADDRESS:
1479 if(!(param1.flags & PARAM_VAL32))
1480 return VERR_EM_INTERPRETER;
1481 addr = (RTGCPTR)param1.val.val32;
1482 break;
1483
1484 default:
1485 return VERR_EM_INTERPRETER;
1486 }
1487
1488 /** @todo is addr always a flat linear address or ds based
1489 * (in absence of segment override prefixes)????
1490 */
1491 rc = PGMInvalidatePage(pVM, addr);
1492 if (VBOX_SUCCESS(rc))
1493 {
1494 /* Manually invalidate the page for the VM's TLB. */
1495 SVMInvlpgA(addr, uASID);
1496 return VINF_SUCCESS;
1497 }
1498 /** @todo r=bird: we shouldn't ignore returns codes like this... I'm 99% sure the error is fatal. */
1499 return VERR_EM_INTERPRETER;
1500}
1501
1502/**
1503 * Interprets INVLPG
1504 *
1505 * @returns VBox status code.
1506 * @retval VINF_* Scheduling instructions.
1507 * @retval VERR_EM_INTERPRETER Something we can't cope with.
1508 * @retval VERR_* Fatal errors.
1509 *
1510 * @param pVM The VM handle.
1511 * @param pRegFrame The register frame.
1512 * @param ASID Tagged TLB id for the guest
1513 *
1514 * Updates the EIP if an instruction was executed successfully.
1515 */
1516static int SVMR0InterpretInvpg(PVM pVM, PCPUMCTXCORE pRegFrame, uint32_t uASID)
1517{
1518 /*
1519 * Only allow 32-bit code.
1520 */
1521 if (SELMIsSelector32Bit(pVM, pRegFrame->eflags, pRegFrame->cs, &pRegFrame->csHid))
1522 {
1523 RTGCPTR pbCode;
1524 int rc = SELMValidateAndConvertCSAddr(pVM, pRegFrame->eflags, pRegFrame->ss, pRegFrame->cs, &pRegFrame->csHid, (RTGCPTR)pRegFrame->eip, &pbCode);
1525 if (VBOX_SUCCESS(rc))
1526 {
1527 uint32_t cbOp;
1528 DISCPUSTATE Cpu;
1529
1530 Cpu.mode = CPUMODE_32BIT;
1531 rc = EMInterpretDisasOneEx(pVM, pbCode, pRegFrame, &Cpu, &cbOp);
1532 Assert(VBOX_FAILURE(rc) || Cpu.pCurInstr->opcode == OP_INVLPG);
1533 if (VBOX_SUCCESS(rc) && Cpu.pCurInstr->opcode == OP_INVLPG)
1534 {
1535 Assert(cbOp == Cpu.opsize);
1536 rc = svmInterpretInvlPg(pVM, &Cpu, pRegFrame, uASID);
1537 if (VBOX_SUCCESS(rc))
1538 {
1539 pRegFrame->eip += cbOp; /* Move on to the next instruction. */
1540 }
1541 return rc;
1542 }
1543 }
1544 }
1545 return VERR_EM_INTERPRETER;
1546}
1547
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