VirtualBox

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

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

Backed out 33399; must save the host context on entry due to long jumps to ring 3.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 83.8 KB
Line 
1/* $Id: HWSVMR0.cpp 10683 2008-07-16 07:45:39Z vboxsync $ */
2/** @file
3 * HWACCM SVM - Host Context Ring 0.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP LOG_GROUP_HWACCM
27#include <VBox/hwaccm.h>
28#include "HWACCMInternal.h"
29#include <VBox/vm.h>
30#include <VBox/x86.h>
31#include <VBox/hwacc_svm.h>
32#include <VBox/pgm.h>
33#include <VBox/pdm.h>
34#include <VBox/err.h>
35#include <VBox/log.h>
36#include <VBox/selm.h>
37#include <VBox/iom.h>
38#include <VBox/dis.h>
39#include <VBox/dbgf.h>
40#include <VBox/disopcode.h>
41#include <iprt/param.h>
42#include <iprt/assert.h>
43#include <iprt/asm.h>
44#include <iprt/cpuset.h>
45#include <iprt/mp.h>
46#include "HWSVMR0.h"
47
48static int SVMR0InterpretInvpg(PVM pVM, PCPUMCTXCORE pRegFrame, uint32_t uASID);
49
50/**
51 * Sets up and activates AMD-V on the current CPU
52 *
53 * @returns VBox status code.
54 * @param pCpu CPU info struct
55 * @param pVM The VM to operate on.
56 * @param pvPageCpu Pointer to the global cpu page
57 * @param pPageCpuPhys Physical address of the global cpu page
58 */
59HWACCMR0DECL(int) SVMR0EnableCpu(PHWACCM_CPUINFO pCpu, PVM pVM, void *pvPageCpu, RTHCPHYS pPageCpuPhys)
60{
61 AssertReturn(pPageCpuPhys, VERR_INVALID_PARAMETER);
62 AssertReturn(pVM, VERR_INVALID_PARAMETER);
63 AssertReturn(pvPageCpu, VERR_INVALID_PARAMETER);
64
65 /* We must turn on AMD-V and setup the host state physical address, as those MSRs are per-cpu/core. */
66
67#ifdef LOG_ENABLED
68 SUPR0Printf("SVMR0EnableCpu cpu %d page (%x) %x\n", pCpu->idCpu, pvPageCpu, (uint32_t)pPageCpuPhys);
69#endif
70
71 /* Turn on AMD-V in the EFER MSR. */
72 uint64_t val = ASMRdMsr(MSR_K6_EFER);
73 if (!(val & MSR_K6_EFER_SVME))
74 ASMWrMsr(MSR_K6_EFER, val | MSR_K6_EFER_SVME);
75
76 /* Write the physical page address where the CPU will store the host state while executing the VM. */
77 ASMWrMsr(MSR_K8_VM_HSAVE_PA, pPageCpuPhys);
78
79 pCpu->uCurrentASID = 0; /* we'll aways increment this the first time (host uses ASID 0) */
80 pCpu->cTLBFlushes = 0;
81 return VINF_SUCCESS;
82}
83
84/**
85 * Deactivates AMD-V on the current CPU
86 *
87 * @returns VBox status code.
88 * @param pCpu CPU info struct
89 * @param pvPageCpu Pointer to the global cpu page
90 * @param pPageCpuPhys Physical address of the global cpu page
91 */
92HWACCMR0DECL(int) SVMR0DisableCpu(PHWACCM_CPUINFO pCpu, void *pvPageCpu, RTHCPHYS pPageCpuPhys)
93{
94 AssertReturn(pPageCpuPhys, VERR_INVALID_PARAMETER);
95 AssertReturn(pvPageCpu, VERR_INVALID_PARAMETER);
96
97#ifdef LOG_ENABLED
98 SUPR0Printf("SVMR0DisableCpu cpu %d\n", pCpu->idCpu);
99#endif
100
101 /* Turn off AMD-V in the EFER MSR. */
102 uint64_t val = ASMRdMsr(MSR_K6_EFER);
103 ASMWrMsr(MSR_K6_EFER, val & ~MSR_K6_EFER_SVME);
104
105 /* Invalidate host state physical address. */
106 ASMWrMsr(MSR_K8_VM_HSAVE_PA, 0);
107 pCpu->uCurrentASID = 0;
108
109 return VINF_SUCCESS;
110}
111
112/**
113 * Does Ring-0 per VM AMD-V init.
114 *
115 * @returns VBox status code.
116 * @param pVM The VM to operate on.
117 */
118HWACCMR0DECL(int) SVMR0InitVM(PVM pVM)
119{
120 int rc;
121
122 pVM->hwaccm.s.svm.pMemObjVMCB = NIL_RTR0MEMOBJ;
123 pVM->hwaccm.s.svm.pMemObjVMCBHost = NIL_RTR0MEMOBJ;
124 pVM->hwaccm.s.svm.pMemObjIOBitmap = NIL_RTR0MEMOBJ;
125 pVM->hwaccm.s.svm.pMemObjMSRBitmap = NIL_RTR0MEMOBJ;
126
127
128 /* Allocate one page for the VM control block (VMCB). */
129 rc = RTR0MemObjAllocCont(&pVM->hwaccm.s.svm.pMemObjVMCB, 1 << PAGE_SHIFT, true /* executable R0 mapping */);
130 if (RT_FAILURE(rc))
131 return rc;
132
133 pVM->hwaccm.s.svm.pVMCB = RTR0MemObjAddress(pVM->hwaccm.s.svm.pMemObjVMCB);
134 pVM->hwaccm.s.svm.pVMCBPhys = RTR0MemObjGetPagePhysAddr(pVM->hwaccm.s.svm.pMemObjVMCB, 0);
135 ASMMemZeroPage(pVM->hwaccm.s.svm.pVMCB);
136
137 /* Allocate one page for the host context */
138 rc = RTR0MemObjAllocCont(&pVM->hwaccm.s.svm.pMemObjVMCBHost, 1 << PAGE_SHIFT, true /* executable R0 mapping */);
139 if (RT_FAILURE(rc))
140 return rc;
141
142 pVM->hwaccm.s.svm.pVMCBHost = RTR0MemObjAddress(pVM->hwaccm.s.svm.pMemObjVMCBHost);
143 pVM->hwaccm.s.svm.pVMCBHostPhys = RTR0MemObjGetPagePhysAddr(pVM->hwaccm.s.svm.pMemObjVMCBHost, 0);
144 ASMMemZeroPage(pVM->hwaccm.s.svm.pVMCBHost);
145
146 /* Allocate 12 KB for the IO bitmap (doesn't seem to be a way to convince SVM not to use it) */
147 rc = RTR0MemObjAllocCont(&pVM->hwaccm.s.svm.pMemObjIOBitmap, 3 << PAGE_SHIFT, true /* executable R0 mapping */);
148 if (RT_FAILURE(rc))
149 return rc;
150
151 pVM->hwaccm.s.svm.pIOBitmap = RTR0MemObjAddress(pVM->hwaccm.s.svm.pMemObjIOBitmap);
152 pVM->hwaccm.s.svm.pIOBitmapPhys = RTR0MemObjGetPagePhysAddr(pVM->hwaccm.s.svm.pMemObjIOBitmap, 0);
153 /* Set all bits to intercept all IO accesses. */
154 ASMMemFill32(pVM->hwaccm.s.svm.pIOBitmap, PAGE_SIZE*3, 0xffffffff);
155
156 /* Allocate 8 KB for the MSR bitmap (doesn't seem to be a way to convince SVM not to use it) */
157 rc = RTR0MemObjAllocCont(&pVM->hwaccm.s.svm.pMemObjMSRBitmap, 2 << PAGE_SHIFT, true /* executable R0 mapping */);
158 if (RT_FAILURE(rc))
159 return rc;
160
161 pVM->hwaccm.s.svm.pMSRBitmap = RTR0MemObjAddress(pVM->hwaccm.s.svm.pMemObjMSRBitmap);
162 pVM->hwaccm.s.svm.pMSRBitmapPhys = RTR0MemObjGetPagePhysAddr(pVM->hwaccm.s.svm.pMemObjMSRBitmap, 0);
163 /* Set all bits to intercept all MSR accesses. */
164 ASMMemFill32(pVM->hwaccm.s.svm.pMSRBitmap, PAGE_SIZE*2, 0xffffffff);
165
166 /* Erratum 170 which requires a forced TLB flush for each world switch:
167 * See http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/33610.pdf
168 *
169 * All BH-G1/2 and DH-G1/2 models include a fix:
170 * Athlon X2: 0x6b 1/2
171 * 0x68 1/2
172 * Athlon 64: 0x7f 1
173 * 0x6f 2
174 * Sempron: 0x7f 1/2
175 * 0x6f 2
176 * 0x6c 2
177 * 0x7c 2
178 * Turion 64: 0x68 2
179 *
180 */
181 uint32_t u32Dummy;
182 uint32_t u32Version, u32Family, u32Model, u32Stepping, u32BaseFamily;
183 ASMCpuId(1, &u32Version, &u32Dummy, &u32Dummy, &u32Dummy);
184 u32BaseFamily= (u32Version >> 8) & 0xf;
185 u32Family = u32BaseFamily + (u32BaseFamily == 0xf ? ((u32Version >> 20) & 0x7f) : 0);
186 u32Model = ((u32Version >> 4) & 0xf);
187 u32Model = u32Model | ((u32BaseFamily == 0xf ? (u32Version >> 16) & 0x0f : 0) << 4);
188 u32Stepping = u32Version & 0xf;
189 if ( u32Family == 0xf
190 && !((u32Model == 0x68 || u32Model == 0x6b || u32Model == 0x7f) && u32Stepping >= 1)
191 && !((u32Model == 0x6f || u32Model == 0x6c || u32Model == 0x7c) && u32Stepping >= 2))
192 {
193 Log(("SVMR0InitVM: AMD cpu with erratum 170 family %x model %x stepping %x\n", u32Family, u32Model, u32Stepping));
194 pVM->hwaccm.s.svm.fAlwaysFlushTLB = true;
195 }
196
197 /* Invalidate the last cpu we were running on. */
198 pVM->hwaccm.s.svm.idLastCpu = NIL_RTCPUID;
199 return VINF_SUCCESS;
200}
201
202/**
203 * Does Ring-0 per VM AMD-V termination.
204 *
205 * @returns VBox status code.
206 * @param pVM The VM to operate on.
207 */
208HWACCMR0DECL(int) SVMR0TermVM(PVM pVM)
209{
210 if (pVM->hwaccm.s.svm.pMemObjVMCB != NIL_RTR0MEMOBJ)
211 {
212 RTR0MemObjFree(pVM->hwaccm.s.svm.pMemObjVMCB, false);
213 pVM->hwaccm.s.svm.pVMCB = 0;
214 pVM->hwaccm.s.svm.pVMCBPhys = 0;
215 pVM->hwaccm.s.svm.pMemObjVMCB = NIL_RTR0MEMOBJ;
216 }
217 if (pVM->hwaccm.s.svm.pMemObjVMCBHost != NIL_RTR0MEMOBJ)
218 {
219 RTR0MemObjFree(pVM->hwaccm.s.svm.pMemObjVMCBHost, false);
220 pVM->hwaccm.s.svm.pVMCBHost = 0;
221 pVM->hwaccm.s.svm.pVMCBHostPhys = 0;
222 pVM->hwaccm.s.svm.pMemObjVMCBHost = NIL_RTR0MEMOBJ;
223 }
224 if (pVM->hwaccm.s.svm.pMemObjIOBitmap != NIL_RTR0MEMOBJ)
225 {
226 RTR0MemObjFree(pVM->hwaccm.s.svm.pMemObjIOBitmap, false);
227 pVM->hwaccm.s.svm.pIOBitmap = 0;
228 pVM->hwaccm.s.svm.pIOBitmapPhys = 0;
229 pVM->hwaccm.s.svm.pMemObjIOBitmap = NIL_RTR0MEMOBJ;
230 }
231 if (pVM->hwaccm.s.svm.pMemObjMSRBitmap != NIL_RTR0MEMOBJ)
232 {
233 RTR0MemObjFree(pVM->hwaccm.s.svm.pMemObjMSRBitmap, false);
234 pVM->hwaccm.s.svm.pMSRBitmap = 0;
235 pVM->hwaccm.s.svm.pMSRBitmapPhys = 0;
236 pVM->hwaccm.s.svm.pMemObjMSRBitmap = NIL_RTR0MEMOBJ;
237 }
238 return VINF_SUCCESS;
239}
240
241/**
242 * Sets up AMD-V for the specified VM
243 *
244 * @returns VBox status code.
245 * @param pVM The VM to operate on.
246 */
247HWACCMR0DECL(int) SVMR0SetupVM(PVM pVM)
248{
249 int rc = VINF_SUCCESS;
250 SVM_VMCB *pVMCB;
251
252 AssertReturn(pVM, VERR_INVALID_PARAMETER);
253
254 Assert(pVM->hwaccm.s.svm.fSupported);
255
256 pVMCB = (SVM_VMCB *)pVM->hwaccm.s.svm.pVMCB;
257 AssertMsgReturn(pVMCB, ("Invalid pVMCB\n"), VERR_EM_INTERNAL_ERROR);
258
259 /* Program the control fields. Most of them never have to be changed again. */
260 /* CR0/3/4 reads must be intercepted, our shadow values are not necessarily the same as the guest's. */
261 /** @note CR0 & CR4 can be safely read when guest and shadow copies are identical. */
262 if (!pVM->hwaccm.s.fNestedPaging)
263 pVMCB->ctrl.u16InterceptRdCRx = RT_BIT(0) | RT_BIT(3) | RT_BIT(4);
264 else
265 pVMCB->ctrl.u16InterceptRdCRx = RT_BIT(0) | RT_BIT(4);
266
267 /*
268 * CR0/3/4 writes must be intercepted for obvious reasons.
269 */
270 if (!pVM->hwaccm.s.fNestedPaging)
271 pVMCB->ctrl.u16InterceptWrCRx = RT_BIT(0) | RT_BIT(3) | RT_BIT(4);
272 else
273 pVMCB->ctrl.u16InterceptWrCRx = RT_BIT(0) | RT_BIT(4) | RT_BIT(8);
274
275 /* Intercept all DRx reads and writes. */
276 pVMCB->ctrl.u16InterceptRdDRx = RT_BIT(0) | RT_BIT(1) | RT_BIT(2) | RT_BIT(3) | RT_BIT(4) | RT_BIT(5) | RT_BIT(6) | RT_BIT(7);
277 pVMCB->ctrl.u16InterceptWrDRx = RT_BIT(0) | RT_BIT(1) | RT_BIT(2) | RT_BIT(3) | RT_BIT(4) | RT_BIT(5) | RT_BIT(6) | RT_BIT(7);
278
279 /* Currently we don't care about DRx reads or writes. DRx registers are trashed.
280 * All breakpoints are automatically cleared when the VM exits.
281 */
282
283 pVMCB->ctrl.u32InterceptException = HWACCM_SVM_TRAP_MASK;
284#ifndef DEBUG
285 if (pVM->hwaccm.s.fNestedPaging)
286 pVMCB->ctrl.u32InterceptException &= ~RT_BIT(14); /* no longer need to intercept #PF. */
287#endif
288
289 pVMCB->ctrl.u32InterceptCtrl1 = SVM_CTRL1_INTERCEPT_INTR
290 | SVM_CTRL1_INTERCEPT_VINTR
291 | SVM_CTRL1_INTERCEPT_NMI
292 | SVM_CTRL1_INTERCEPT_SMI
293 | SVM_CTRL1_INTERCEPT_INIT
294 | SVM_CTRL1_INTERCEPT_RDPMC
295 | SVM_CTRL1_INTERCEPT_CPUID
296 | SVM_CTRL1_INTERCEPT_RSM
297 | SVM_CTRL1_INTERCEPT_HLT
298 | SVM_CTRL1_INTERCEPT_INOUT_BITMAP
299 | SVM_CTRL1_INTERCEPT_MSR_SHADOW
300 | SVM_CTRL1_INTERCEPT_INVLPG
301 | SVM_CTRL1_INTERCEPT_INVLPGA /* AMD only */
302 | SVM_CTRL1_INTERCEPT_TASK_SWITCH
303 | SVM_CTRL1_INTERCEPT_SHUTDOWN /* fatal */
304 | SVM_CTRL1_INTERCEPT_FERR_FREEZE; /* Legacy FPU FERR handling. */
305 ;
306 /* With nested paging we don't care about invlpg anymore. */
307 if (pVM->hwaccm.s.fNestedPaging)
308 pVMCB->ctrl.u32InterceptCtrl1 &= ~SVM_CTRL1_INTERCEPT_INVLPG;
309
310 pVMCB->ctrl.u32InterceptCtrl2 = SVM_CTRL2_INTERCEPT_VMRUN /* required */
311 | SVM_CTRL2_INTERCEPT_VMMCALL
312 | SVM_CTRL2_INTERCEPT_VMLOAD
313 | SVM_CTRL2_INTERCEPT_VMSAVE
314 | SVM_CTRL2_INTERCEPT_STGI
315 | SVM_CTRL2_INTERCEPT_CLGI
316 | SVM_CTRL2_INTERCEPT_SKINIT
317 | SVM_CTRL2_INTERCEPT_RDTSCP /* AMD only; we don't support this one */
318 | SVM_CTRL2_INTERCEPT_WBINVD
319 | SVM_CTRL2_INTERCEPT_MWAIT_UNCOND; /* don't execute mwait or else we'll idle inside the guest (host thinks the cpu load is high) */
320 ;
321 Log(("pVMCB->ctrl.u32InterceptException = %x\n", pVMCB->ctrl.u32InterceptException));
322 Log(("pVMCB->ctrl.u32InterceptCtrl1 = %x\n", pVMCB->ctrl.u32InterceptCtrl1));
323 Log(("pVMCB->ctrl.u32InterceptCtrl2 = %x\n", pVMCB->ctrl.u32InterceptCtrl2));
324
325 /* Virtualize masking of INTR interrupts. (reads/writes from/to CR8 go to the V_TPR register) */
326 pVMCB->ctrl.IntCtrl.n.u1VIrqMasking = 1;
327 /* Ignore the priority in the TPR; just deliver it when we tell it to. */
328 pVMCB->ctrl.IntCtrl.n.u1IgnoreTPR = 1;
329
330 /* Set IO and MSR bitmap addresses. */
331 pVMCB->ctrl.u64IOPMPhysAddr = pVM->hwaccm.s.svm.pIOBitmapPhys;
332 pVMCB->ctrl.u64MSRPMPhysAddr = pVM->hwaccm.s.svm.pMSRBitmapPhys;
333
334 /* No LBR virtualization. */
335 pVMCB->ctrl.u64LBRVirt = 0;
336
337 /** The ASID must start at 1; the host uses 0. */
338 pVMCB->ctrl.TLBCtrl.n.u32ASID = 1;
339
340 /** Setup the PAT msr (nested paging only) */
341 pVMCB->guest.u64GPAT = 0x0007040600070406ULL;
342 return rc;
343}
344
345
346/**
347 * Injects an event (trap or external interrupt)
348 *
349 * @param pVM The VM to operate on.
350 * @param pVMCB SVM control block
351 * @param pCtx CPU Context
352 * @param pIntInfo SVM interrupt info
353 */
354inline void SVMR0InjectEvent(PVM pVM, SVM_VMCB *pVMCB, CPUMCTX *pCtx, SVM_EVENT* pEvent)
355{
356#ifdef VBOX_STRICT
357 if (pEvent->n.u8Vector == 0xE)
358 Log(("SVM: Inject int %d at %VGv error code=%02x CR2=%VGv intInfo=%08x\n", pEvent->n.u8Vector, pCtx->rip, pEvent->n.u32ErrorCode, pCtx->cr2, pEvent->au64[0]));
359 else
360 if (pEvent->n.u8Vector < 0x20)
361 Log(("SVM: Inject int %d at %VGv error code=%08x\n", pEvent->n.u8Vector, pCtx->rip, pEvent->n.u32ErrorCode));
362 else
363 {
364 Log(("INJ-EI: %x at %VGv\n", pEvent->n.u8Vector, pCtx->rip));
365 Assert(!VM_FF_ISSET(pVM, VM_FF_INHIBIT_INTERRUPTS));
366 Assert(pCtx->eflags.u32 & X86_EFL_IF);
367 }
368#endif
369
370 /* Set event injection state. */
371 pVMCB->ctrl.EventInject.au64[0] = pEvent->au64[0];
372}
373
374
375/**
376 * Checks for pending guest interrupts and injects them
377 *
378 * @returns VBox status code.
379 * @param pVM The VM to operate on.
380 * @param pVMCB SVM control block
381 * @param pCtx CPU Context
382 */
383static int SVMR0CheckPendingInterrupt(PVM pVM, SVM_VMCB *pVMCB, CPUMCTX *pCtx)
384{
385 int rc;
386
387 /* Dispatch any pending interrupts. (injected before, but a VM exit occurred prematurely) */
388 if (pVM->hwaccm.s.Event.fPending)
389 {
390 SVM_EVENT Event;
391
392 Log(("Reinjecting event %08x %08x at %VGv\n", pVM->hwaccm.s.Event.intInfo, pVM->hwaccm.s.Event.errCode, pCtx->rip));
393 STAM_COUNTER_INC(&pVM->hwaccm.s.StatIntReinject);
394 Event.au64[0] = pVM->hwaccm.s.Event.intInfo;
395 SVMR0InjectEvent(pVM, pVMCB, pCtx, &Event);
396
397 pVM->hwaccm.s.Event.fPending = false;
398 return VINF_SUCCESS;
399 }
400
401 /* When external interrupts are pending, we should exit the VM when IF is set. */
402 if ( !TRPMHasTrap(pVM)
403 && VM_FF_ISPENDING(pVM, (VM_FF_INTERRUPT_APIC|VM_FF_INTERRUPT_PIC)))
404 {
405 if ( !(pCtx->eflags.u32 & X86_EFL_IF)
406 || VM_FF_ISSET(pVM, VM_FF_INHIBIT_INTERRUPTS))
407 {
408 if (!pVMCB->ctrl.IntCtrl.n.u1VIrqValid)
409 {
410 if (!VM_FF_ISSET(pVM, VM_FF_INHIBIT_INTERRUPTS))
411 LogFlow(("Enable irq window exit!\n"));
412 else
413 Log(("Pending interrupt blocked at %VGv by VM_FF_INHIBIT_INTERRUPTS -> irq window exit\n", pCtx->rip));
414
415 /** @todo use virtual interrupt method to inject a pending irq; dispatched as soon as guest.IF is set. */
416 pVMCB->ctrl.u32InterceptCtrl1 |= SVM_CTRL1_INTERCEPT_VINTR;
417 pVMCB->ctrl.IntCtrl.n.u1VIrqValid = 1;
418 pVMCB->ctrl.IntCtrl.n.u8VIrqVector = 0; /* don't care */
419 }
420 }
421 else
422 {
423 uint8_t u8Interrupt;
424
425 rc = PDMGetInterrupt(pVM, &u8Interrupt);
426 Log(("Dispatch interrupt: u8Interrupt=%x (%d) rc=%Vrc\n", u8Interrupt, u8Interrupt, rc));
427 if (VBOX_SUCCESS(rc))
428 {
429 rc = TRPMAssertTrap(pVM, u8Interrupt, TRPM_HARDWARE_INT);
430 AssertRC(rc);
431 }
432 else
433 {
434 /* Can only happen in rare cases where a pending interrupt is cleared behind our back */
435 Assert(!VM_FF_ISPENDING(pVM, (VM_FF_INTERRUPT_APIC|VM_FF_INTERRUPT_PIC)));
436 STAM_COUNTER_INC(&pVM->hwaccm.s.StatSwitchGuestIrq);
437 /* Just continue */
438 }
439 }
440 }
441
442#ifdef VBOX_STRICT
443 if (TRPMHasTrap(pVM))
444 {
445 uint8_t u8Vector;
446 rc = TRPMQueryTrapAll(pVM, &u8Vector, 0, 0, 0);
447 AssertRC(rc);
448 }
449#endif
450
451 if ( pCtx->eflags.u32 & X86_EFL_IF
452 && (!VM_FF_ISSET(pVM, VM_FF_INHIBIT_INTERRUPTS))
453 && TRPMHasTrap(pVM)
454 )
455 {
456 uint8_t u8Vector;
457 int rc;
458 TRPMEVENT enmType;
459 SVM_EVENT Event;
460 RTGCUINT u32ErrorCode;
461
462 Event.au64[0] = 0;
463
464 /* If a new event is pending, then dispatch it now. */
465 rc = TRPMQueryTrapAll(pVM, &u8Vector, &enmType, &u32ErrorCode, 0);
466 AssertRC(rc);
467 Assert(pCtx->eflags.Bits.u1IF == 1 || enmType == TRPM_TRAP);
468 Assert(enmType != TRPM_SOFTWARE_INT);
469
470 /* Clear the pending trap. */
471 rc = TRPMResetTrap(pVM);
472 AssertRC(rc);
473
474 Event.n.u8Vector = u8Vector;
475 Event.n.u1Valid = 1;
476 Event.n.u32ErrorCode = u32ErrorCode;
477
478 if (enmType == TRPM_TRAP)
479 {
480 switch (u8Vector) {
481 case 8:
482 case 10:
483 case 11:
484 case 12:
485 case 13:
486 case 14:
487 case 17:
488 /* Valid error codes. */
489 Event.n.u1ErrorCodeValid = 1;
490 break;
491 default:
492 break;
493 }
494 if (u8Vector == X86_XCPT_NMI)
495 Event.n.u3Type = SVM_EVENT_NMI;
496 else
497 Event.n.u3Type = SVM_EVENT_EXCEPTION;
498 }
499 else
500 Event.n.u3Type = SVM_EVENT_EXTERNAL_IRQ;
501
502 STAM_COUNTER_INC(&pVM->hwaccm.s.StatIntInject);
503 SVMR0InjectEvent(pVM, pVMCB, pCtx, &Event);
504 } /* if (interrupts can be dispatched) */
505
506 return VINF_SUCCESS;
507}
508
509/**
510 * Save the host state
511 *
512 * @returns VBox status code.
513 * @param pVM The VM to operate on.
514 */
515HWACCMR0DECL(int) SVMR0SaveHostState(PVM pVM)
516{
517 /* Nothing to do here. */
518 return VINF_SUCCESS;
519}
520
521/**
522 * Loads the guest state
523 *
524 * NOTE: Don't do anything here that can cause a jump back to ring 3!!!!!
525 *
526 * @returns VBox status code.
527 * @param pVM The VM to operate on.
528 * @param pCtx Guest context
529 */
530HWACCMR0DECL(int) SVMR0LoadGuestState(PVM pVM, CPUMCTX *pCtx)
531{
532 RTGCUINTPTR val;
533 SVM_VMCB *pVMCB;
534
535 if (pVM == NULL)
536 return VERR_INVALID_PARAMETER;
537
538 /* Setup AMD SVM. */
539 Assert(pVM->hwaccm.s.svm.fSupported);
540
541 pVMCB = (SVM_VMCB *)pVM->hwaccm.s.svm.pVMCB;
542 AssertMsgReturn(pVMCB, ("Invalid pVMCB\n"), VERR_EM_INTERNAL_ERROR);
543
544 /* Guest CPU context: ES, CS, SS, DS, FS, GS. */
545 if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_SEGMENT_REGS)
546 {
547 SVM_WRITE_SELREG(CS, cs);
548 SVM_WRITE_SELREG(SS, ss);
549 SVM_WRITE_SELREG(DS, ds);
550 SVM_WRITE_SELREG(ES, es);
551 SVM_WRITE_SELREG(FS, fs);
552 SVM_WRITE_SELREG(GS, gs);
553 }
554
555 /* Guest CPU context: LDTR. */
556 if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_LDTR)
557 {
558 SVM_WRITE_SELREG(LDTR, ldtr);
559 }
560
561 /* Guest CPU context: TR. */
562 if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_TR)
563 {
564 SVM_WRITE_SELREG(TR, tr);
565 }
566
567 /* Guest CPU context: GDTR. */
568 if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_GDTR)
569 {
570 pVMCB->guest.GDTR.u32Limit = pCtx->gdtr.cbGdt;
571 pVMCB->guest.GDTR.u64Base = pCtx->gdtr.pGdt;
572 }
573
574 /* Guest CPU context: IDTR. */
575 if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_IDTR)
576 {
577 pVMCB->guest.IDTR.u32Limit = pCtx->idtr.cbIdt;
578 pVMCB->guest.IDTR.u64Base = pCtx->idtr.pIdt;
579 }
580
581 /*
582 * Sysenter MSRs (unconditional)
583 */
584 pVMCB->guest.u64SysEnterCS = pCtx->SysEnter.cs;
585 pVMCB->guest.u64SysEnterEIP = pCtx->SysEnter.eip;
586 pVMCB->guest.u64SysEnterESP = pCtx->SysEnter.esp;
587
588 /* Control registers */
589 if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_CR0)
590 {
591 val = pCtx->cr0;
592 if (!CPUMIsGuestFPUStateActive(pVM))
593 {
594 /* Always use #NM exceptions to load the FPU/XMM state on demand. */
595 val |= X86_CR0_TS | X86_CR0_ET | X86_CR0_NE | X86_CR0_MP;
596 }
597 else
598 {
599 /** @todo check if we support the old style mess correctly. */
600 if (!(val & X86_CR0_NE))
601 {
602 Log(("Forcing X86_CR0_NE!!!\n"));
603
604 /* Also catch floating point exceptions as we need to report them to the guest in a different way. */
605 if (!pVM->hwaccm.s.fFPUOldStyleOverride)
606 {
607 pVMCB->ctrl.u32InterceptException |= RT_BIT(16);
608 pVM->hwaccm.s.fFPUOldStyleOverride = true;
609 }
610 }
611 val |= X86_CR0_NE; /* always turn on the native mechanism to report FPU errors (old style uses interrupts) */
612 }
613 /* Always enable caching. */
614 val &= ~(X86_CR0_CD|X86_CR0_NW);
615
616 /* Note: WP is not relevant in nested paging mode as we catch accesses on the (guest) physical level. */
617 /* Note: In nested paging mode the guest is allowed to run with paging disabled; the guest physical to host physical translation will remain active. */
618 if (!pVM->hwaccm.s.fNestedPaging)
619 {
620 val |= X86_CR0_PG; /* Paging is always enabled; even when the guest is running in real mode or PE without paging. */
621 val |= X86_CR0_WP; /* Must set this as we rely on protect various pages and supervisor writes must be caught. */
622 }
623 pVMCB->guest.u64CR0 = val;
624 }
625 /* CR2 as well */
626 pVMCB->guest.u64CR2 = pCtx->cr2;
627
628 if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_CR3)
629 {
630 /* Save our shadow CR3 register. */
631 if (pVM->hwaccm.s.fNestedPaging)
632 {
633 pVMCB->ctrl.u64NestedPagingCR3 = PGMGetNestedCR3(pVM, PGMGetHostMode(pVM));
634 Assert(pVMCB->ctrl.u64NestedPagingCR3);
635 pVMCB->guest.u64CR3 = pCtx->cr3;
636 }
637 else
638 {
639 pVMCB->guest.u64CR3 = PGMGetHyperCR3(pVM);
640 Assert(pVMCB->guest.u64CR3);
641 }
642 }
643
644 if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_CR4)
645 {
646 val = pCtx->cr4;
647 if (!pVM->hwaccm.s.fNestedPaging)
648 {
649 switch(pVM->hwaccm.s.enmShadowMode)
650 {
651 case PGMMODE_REAL:
652 case PGMMODE_PROTECTED: /* Protected mode, no paging. */
653 AssertFailed();
654 return VERR_PGM_UNSUPPORTED_SHADOW_PAGING_MODE;
655
656 case PGMMODE_32_BIT: /* 32-bit paging. */
657 break;
658
659 case PGMMODE_PAE: /* PAE paging. */
660 case PGMMODE_PAE_NX: /* PAE paging with NX enabled. */
661 /** @todo use normal 32 bits paging */
662 val |= X86_CR4_PAE;
663 break;
664
665 case PGMMODE_AMD64: /* 64-bit AMD paging (long mode). */
666 case PGMMODE_AMD64_NX: /* 64-bit AMD paging (long mode) with NX enabled. */
667#ifdef VBOX_ENABLE_64_BITS_GUESTS
668 break;
669#else
670 AssertFailed();
671 return VERR_PGM_UNSUPPORTED_SHADOW_PAGING_MODE;
672#endif
673
674 default: /* shut up gcc */
675 AssertFailed();
676 return VERR_PGM_UNSUPPORTED_SHADOW_PAGING_MODE;
677 }
678 }
679 pVMCB->guest.u64CR4 = val;
680 }
681
682 /* Debug registers. */
683 if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_DEBUG)
684 {
685 /** @todo DR0-6 */
686 val = pCtx->dr7;
687 val &= ~(RT_BIT(11) | RT_BIT(12) | RT_BIT(14) | RT_BIT(15)); /* must be zero */
688 val |= 0x400; /* must be one */
689#ifdef VBOX_STRICT
690 val = 0x400;
691#endif
692 pVMCB->guest.u64DR7 = val;
693
694 pVMCB->guest.u64DR6 = pCtx->dr6;
695 }
696
697 /* EIP, ESP and EFLAGS */
698 pVMCB->guest.u64RIP = pCtx->rip;
699 pVMCB->guest.u64RSP = pCtx->rsp;
700 pVMCB->guest.u64RFlags = pCtx->eflags.u32;
701
702 /* Set CPL */
703 pVMCB->guest.u8CPL = pCtx->csHid.Attr.n.u2Dpl;
704
705 /* RAX/EAX too, as VMRUN uses RAX as an implicit parameter. */
706 pVMCB->guest.u64RAX = pCtx->rax;
707
708 /* vmrun will fail without MSR_K6_EFER_SVME. */
709 pVMCB->guest.u64EFER = pCtx->msrEFER | MSR_K6_EFER_SVME;
710
711 /* 64 bits guest mode? */
712 if (pCtx->msrEFER & MSR_K6_EFER_LMA)
713 {
714#if !defined(VBOX_WITH_64_BITS_GUESTS) || HC_ARCH_BITS != 64
715 return VERR_PGM_UNSUPPORTED_SHADOW_PAGING_MODE;
716#else
717 pVM->hwaccm.s.svm.pfnVMRun = SVMVMRun64;
718#endif
719 /* Unconditionally update these as wrmsr might have changed them. (HWACCM_CHANGED_GUEST_SEGMENT_REGS will not be set) */
720 pVMCB->guest.FS.u64Base = pCtx->fsHid.u64Base;
721 pVMCB->guest.GS.u64Base = pCtx->gsHid.u64Base;
722 }
723 else
724 {
725 /* Filter out the MSR_K6_LME bit or else AMD-V expects amd64 shadow paging. */
726 pVMCB->guest.u64EFER &= ~MSR_K6_EFER_LME;
727
728 pVM->hwaccm.s.svm.pfnVMRun = SVMVMRun;
729 }
730
731 /** TSC offset. */
732 if (TMCpuTickCanUseRealTSC(pVM, &pVMCB->ctrl.u64TSCOffset))
733 {
734 pVMCB->ctrl.u32InterceptCtrl1 &= ~SVM_CTRL1_INTERCEPT_RDTSC;
735 STAM_COUNTER_INC(&pVM->hwaccm.s.StatTSCOffset);
736 }
737 else
738 {
739 pVMCB->ctrl.u32InterceptCtrl1 |= SVM_CTRL1_INTERCEPT_RDTSC;
740 STAM_COUNTER_INC(&pVM->hwaccm.s.StatTSCIntercept);
741 }
742
743 /* Sync the various msrs for 64 bits mode. */
744 pVMCB->guest.u64STAR = pCtx->msrSTAR; /* legacy syscall eip, cs & ss */
745 pVMCB->guest.u64LSTAR = pCtx->msrLSTAR; /* 64 bits mode syscall rip */
746 pVMCB->guest.u64CSTAR = pCtx->msrCSTAR; /* compatibility mode syscall rip */
747 pVMCB->guest.u64SFMASK = pCtx->msrSFMASK; /* syscall flag mask */
748 pVMCB->guest.u64KernelGSBase = pCtx->msrKERNELGSBASE; /* swapgs exchange value */
749
750#ifdef DEBUG
751 /* Intercept X86_XCPT_DB if stepping is enabled */
752 if (DBGFIsStepping(pVM))
753 pVMCB->ctrl.u32InterceptException |= RT_BIT(1);
754 else
755 pVMCB->ctrl.u32InterceptException &= ~RT_BIT(1);
756#endif
757
758 /* Done. */
759 pVM->hwaccm.s.fContextUseFlags &= ~HWACCM_CHANGED_ALL_GUEST;
760
761 return VINF_SUCCESS;
762}
763
764
765/**
766 * Runs guest code in an SVM VM.
767 *
768 * @todo This can be much more efficient, when we only sync that which has actually changed. (this is the first attempt only)
769 *
770 * @returns VBox status code.
771 * @param pVM The VM to operate on.
772 * @param pCtx Guest context
773 */
774HWACCMR0DECL(int) SVMR0RunGuestCode(PVM pVM, CPUMCTX *pCtx)
775{
776 int rc = VINF_SUCCESS;
777 uint64_t exitCode = (uint64_t)SVM_EXIT_INVALID;
778 SVM_VMCB *pVMCB;
779 bool fGuestStateSynced = false;
780 bool fSyncTPR = false;
781 unsigned cResume = 0;
782 uint8_t u8LastVTPR;
783 PHWACCM_CPUINFO pCpu = 0;
784#ifdef VBOX_STRICT
785 RTCPUID idCpuCheck;
786#endif
787
788 STAM_PROFILE_ADV_START(&pVM->hwaccm.s.StatEntry, x);
789
790 pVMCB = (SVM_VMCB *)pVM->hwaccm.s.svm.pVMCB;
791 AssertMsgReturn(pVMCB, ("Invalid pVMCB\n"), VERR_EM_INTERNAL_ERROR);
792
793 /* We can jump to this point to resume execution after determining that a VM-exit is innocent.
794 */
795ResumeExecution:
796 /* Safety precaution; looping for too long here can have a very bad effect on the host */
797 if (++cResume > HWACCM_MAX_RESUME_LOOPS)
798 {
799 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitMaxResume);
800 rc = VINF_EM_RAW_INTERRUPT;
801 goto end;
802 }
803
804 /* Check for irq inhibition due to instruction fusing (sti, mov ss). */
805 if (VM_FF_ISSET(pVM, VM_FF_INHIBIT_INTERRUPTS))
806 {
807 Log(("VM_FF_INHIBIT_INTERRUPTS at %VGv successor %VGv\n", pCtx->rip, EMGetInhibitInterruptsPC(pVM)));
808 if (pCtx->rip != EMGetInhibitInterruptsPC(pVM))
809 {
810 /** @note we intentionally don't clear VM_FF_INHIBIT_INTERRUPTS here.
811 * Before we are able to execute this instruction in raw mode (iret to guest code) an external interrupt might
812 * force a world switch again. Possibly allowing a guest interrupt to be dispatched in the process. This could
813 * break the guest. Sounds very unlikely, but such timing sensitive problem are not as rare as you might think.
814 */
815 VM_FF_CLEAR(pVM, VM_FF_INHIBIT_INTERRUPTS);
816 /* Irq inhibition is no longer active; clear the corresponding SVM state. */
817 pVMCB->ctrl.u64IntShadow = 0;
818 }
819 }
820 else
821 {
822 /* Irq inhibition is no longer active; clear the corresponding SVM state. */
823 pVMCB->ctrl.u64IntShadow = 0;
824 }
825
826 /* Check for pending actions that force us to go back to ring 3. */
827#ifdef DEBUG
828 /* Intercept X86_XCPT_DB if stepping is enabled */
829 if (!DBGFIsStepping(pVM))
830#endif
831 {
832 if (VM_FF_ISPENDING(pVM, VM_FF_TO_R3 | VM_FF_TIMER))
833 {
834 VM_FF_CLEAR(pVM, VM_FF_TO_R3);
835 STAM_COUNTER_INC(&pVM->hwaccm.s.StatSwitchToR3);
836 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatEntry, x);
837 rc = VINF_EM_RAW_TO_R3;
838 goto end;
839 }
840 }
841
842 /* Pending request packets might contain actions that need immediate attention, such as pending hardware interrupts. */
843 if (VM_FF_ISPENDING(pVM, VM_FF_REQUEST))
844 {
845 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatEntry, x);
846 rc = VINF_EM_PENDING_REQUEST;
847 goto end;
848 }
849
850 /* When external interrupts are pending, we should exit the VM when IF is set. */
851 /** @note *after* VM_FF_INHIBIT_INTERRUPTS check!!! */
852 rc = SVMR0CheckPendingInterrupt(pVM, pVMCB, pCtx);
853 if (VBOX_FAILURE(rc))
854 {
855 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatEntry, x);
856 goto end;
857 }
858
859 /* TPR caching using CR8 is only available in 64 bits mode */
860 /* Note the 32 bits exception for AMD (X86_CPUID_AMD_FEATURE_ECX_CR8L), but that appears missing in Intel CPUs */
861 /* Note: we can't do this in LoadGuestState as PDMApicGetTPR can jump back to ring 3 (lock)!!!!!!!! */
862 if (pCtx->msrEFER & MSR_K6_EFER_LMA)
863 {
864 bool fPending;
865
866 /* TPR caching in CR8 */
867 int rc = PDMApicGetTPR(pVM, &u8LastVTPR, &fPending);
868 AssertRC(rc);
869 pVMCB->ctrl.IntCtrl.n.u8VTPR = u8LastVTPR;
870
871 if (fPending)
872 {
873 /* A TPR change could activate a pending interrupt, so catch cr8 writes. */
874 pVMCB->ctrl.u16InterceptWrCRx |= RT_BIT(8);
875 }
876 else
877 /* No interrupts are pending, so we don't need to be explicitely notified.
878 * There are enough world switches for detecting pending interrupts.
879 */
880 pVMCB->ctrl.u16InterceptWrCRx &= ~RT_BIT(8);
881
882 fSyncTPR = !fPending;
883 }
884
885 /* All done! Let's start VM execution. */
886 STAM_PROFILE_ADV_START(&pVM->hwaccm.s.StatInGC, x);
887
888 /* Enable nested paging if necessary (disabled each time after #VMEXIT). */
889 pVMCB->ctrl.NestedPaging.n.u1NestedPaging = pVM->hwaccm.s.fNestedPaging;
890
891#ifdef LOG_ENABLED
892 pCpu = HWACCMR0GetCurrentCpu();
893 if ( pVM->hwaccm.s.svm.idLastCpu != pCpu->idCpu
894 || pVM->hwaccm.s.svm.cTLBFlushes != pCpu->cTLBFlushes)
895 {
896 if (pVM->hwaccm.s.svm.idLastCpu != pCpu->idCpu)
897 Log(("Force TLB flush due to rescheduling to a different cpu (%d vs %d)\n", pVM->hwaccm.s.svm.idLastCpu, pCpu->idCpu));
898 else
899 Log(("Force TLB flush due to changed TLB flush count (%x vs %x)\n", pVM->hwaccm.s.svm.cTLBFlushes, pCpu->cTLBFlushes));
900 }
901 if (pCpu->fFlushTLB)
902 Log(("Force TLB flush: first time cpu %d is used -> flush\n", pCpu->idCpu));
903#endif
904
905 /*
906 * NOTE: DO NOT DO ANYTHING AFTER THIS POINT THAT MIGHT JUMP BACK TO RING 3!
907 * (until the actual world switch)
908 */
909
910#ifdef VBOX_STRICT
911 idCpuCheck = RTMpCpuId();
912#endif
913
914 /* Load the guest state; *must* be here as it sets up the shadow cr0 for lazy fpu syncing! */
915 rc = SVMR0LoadGuestState(pVM, pCtx);
916 if (rc != VINF_SUCCESS)
917 {
918 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatEntry, x);
919 goto end;
920 }
921 fGuestStateSynced = true;
922
923 pCpu = HWACCMR0GetCurrentCpu();
924 /* Force a TLB flush for the first world switch if the current cpu differs from the one we ran on last. */
925 /* Note that this can happen both for start and resume due to long jumps back to ring 3. */
926 if ( pVM->hwaccm.s.svm.idLastCpu != pCpu->idCpu
927 /* if the tlb flush count has changed, another VM has flushed the TLB of this cpu, so we can't use our current ASID anymore. */
928 || pVM->hwaccm.s.svm.cTLBFlushes != pCpu->cTLBFlushes)
929 {
930 /* Force a TLB flush on VM entry. */
931 pVM->hwaccm.s.svm.fForceTLBFlush = true;
932 }
933 else
934 Assert(!pCpu->fFlushTLB);
935
936 pVM->hwaccm.s.svm.idLastCpu = pCpu->idCpu;
937
938 /* Make sure we flush the TLB when required. Switch ASID to achieve the same thing, but without actually flushing the whole TLB (which is expensive). */
939 if ( pVM->hwaccm.s.svm.fForceTLBFlush
940 && !pVM->hwaccm.s.svm.fAlwaysFlushTLB)
941 {
942 if ( ++pCpu->uCurrentASID >= pVM->hwaccm.s.svm.u32MaxASID
943 || pCpu->fFlushTLB)
944 {
945 pCpu->fFlushTLB = false;
946 pCpu->uCurrentASID = 1; /* start at 1; host uses 0 */
947 pVMCB->ctrl.TLBCtrl.n.u1TLBFlush = 1; /* wrap around; flush TLB */
948 pCpu->cTLBFlushes++;
949 }
950 else
951 STAM_COUNTER_INC(&pVM->hwaccm.s.StatFlushASID);
952
953 pVM->hwaccm.s.svm.cTLBFlushes = pCpu->cTLBFlushes;
954 }
955 else
956 {
957 Assert(!pCpu->fFlushTLB || pVM->hwaccm.s.svm.fAlwaysFlushTLB);
958
959 /* We never increase uCurrentASID in the fAlwaysFlushTLB (erratum 170) case. */
960 if (!pCpu->uCurrentASID)
961 pCpu->uCurrentASID = 1;
962
963 pVMCB->ctrl.TLBCtrl.n.u1TLBFlush = pVM->hwaccm.s.svm.fForceTLBFlush;
964 }
965 AssertMsg(pVM->hwaccm.s.svm.cTLBFlushes == pCpu->cTLBFlushes, ("Flush count mismatch for cpu %d (%x vs %x)\n", pCpu->idCpu, pVM->hwaccm.s.svm.cTLBFlushes, pCpu->cTLBFlushes));
966 AssertMsg(pCpu->uCurrentASID >= 1 && pCpu->uCurrentASID < pVM->hwaccm.s.svm.u32MaxASID, ("cpu%d uCurrentASID = %x\n", pCpu->idCpu, pCpu->uCurrentASID));
967 pVMCB->ctrl.TLBCtrl.n.u32ASID = pCpu->uCurrentASID;
968
969#ifdef VBOX_WITH_STATISTICS
970 if (pVMCB->ctrl.TLBCtrl.n.u1TLBFlush)
971 STAM_COUNTER_INC(&pVM->hwaccm.s.StatFlushTLBWorldSwitch);
972 else
973 STAM_COUNTER_INC(&pVM->hwaccm.s.StatNoFlushTLBWorldSwitch);
974#endif
975
976 /* In case we execute a goto ResumeExecution later on. */
977 pVM->hwaccm.s.svm.fResumeVM = true;
978 pVM->hwaccm.s.svm.fForceTLBFlush = pVM->hwaccm.s.svm.fAlwaysFlushTLB;
979
980 Assert(sizeof(pVM->hwaccm.s.svm.pVMCBPhys) == 8);
981 Assert(pVMCB->ctrl.u32InterceptCtrl2 == ( SVM_CTRL2_INTERCEPT_VMRUN /* required */
982 | SVM_CTRL2_INTERCEPT_VMMCALL
983 | SVM_CTRL2_INTERCEPT_VMLOAD
984 | SVM_CTRL2_INTERCEPT_VMSAVE
985 | SVM_CTRL2_INTERCEPT_STGI
986 | SVM_CTRL2_INTERCEPT_CLGI
987 | SVM_CTRL2_INTERCEPT_SKINIT
988 | SVM_CTRL2_INTERCEPT_RDTSCP /* AMD only; we don't support this one */
989 | SVM_CTRL2_INTERCEPT_WBINVD
990 | SVM_CTRL2_INTERCEPT_MWAIT_UNCOND /* don't execute mwait or else we'll idle inside the guest (host thinks the cpu load is high) */
991 ));
992 Assert(pVMCB->ctrl.IntCtrl.n.u1VIrqMasking);
993 Assert(pVMCB->ctrl.u64IOPMPhysAddr == pVM->hwaccm.s.svm.pIOBitmapPhys);
994 Assert(pVMCB->ctrl.u64MSRPMPhysAddr == pVM->hwaccm.s.svm.pMSRBitmapPhys);
995 Assert(pVMCB->ctrl.u64LBRVirt == 0);
996
997#ifdef VBOX_STRICT
998 Assert(idCpuCheck == RTMpCpuId());
999#endif
1000 pVM->hwaccm.s.svm.pfnVMRun(pVM->hwaccm.s.svm.pVMCBHostPhys, pVM->hwaccm.s.svm.pVMCBPhys, pCtx);
1001 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatInGC, x);
1002
1003 /**
1004 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1005 * 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
1006 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1007 */
1008
1009 STAM_PROFILE_ADV_START(&pVM->hwaccm.s.StatExit, x);
1010
1011 /* Reason for the VM exit */
1012 exitCode = pVMCB->ctrl.u64ExitCode;
1013
1014 if (exitCode == (uint64_t)SVM_EXIT_INVALID) /* Invalid guest state. */
1015 {
1016 HWACCMDumpRegs(pVM, pCtx);
1017#ifdef DEBUG
1018 Log(("ctrl.u16InterceptRdCRx %x\n", pVMCB->ctrl.u16InterceptRdCRx));
1019 Log(("ctrl.u16InterceptWrCRx %x\n", pVMCB->ctrl.u16InterceptWrCRx));
1020 Log(("ctrl.u16InterceptRdDRx %x\n", pVMCB->ctrl.u16InterceptRdDRx));
1021 Log(("ctrl.u16InterceptWrDRx %x\n", pVMCB->ctrl.u16InterceptWrDRx));
1022 Log(("ctrl.u32InterceptException %x\n", pVMCB->ctrl.u32InterceptException));
1023 Log(("ctrl.u32InterceptCtrl1 %x\n", pVMCB->ctrl.u32InterceptCtrl1));
1024 Log(("ctrl.u32InterceptCtrl2 %x\n", pVMCB->ctrl.u32InterceptCtrl2));
1025 Log(("ctrl.u64IOPMPhysAddr %VX64\n", pVMCB->ctrl.u64IOPMPhysAddr));
1026 Log(("ctrl.u64MSRPMPhysAddr %VX64\n", pVMCB->ctrl.u64MSRPMPhysAddr));
1027 Log(("ctrl.u64TSCOffset %VX64\n", pVMCB->ctrl.u64TSCOffset));
1028
1029 Log(("ctrl.TLBCtrl.u32ASID %x\n", pVMCB->ctrl.TLBCtrl.n.u32ASID));
1030 Log(("ctrl.TLBCtrl.u1TLBFlush %x\n", pVMCB->ctrl.TLBCtrl.n.u1TLBFlush));
1031 Log(("ctrl.TLBCtrl.u7Reserved %x\n", pVMCB->ctrl.TLBCtrl.n.u7Reserved));
1032 Log(("ctrl.TLBCtrl.u24Reserved %x\n", pVMCB->ctrl.TLBCtrl.n.u24Reserved));
1033
1034 Log(("ctrl.IntCtrl.u8VTPR %x\n", pVMCB->ctrl.IntCtrl.n.u8VTPR));
1035 Log(("ctrl.IntCtrl.u1VIrqValid %x\n", pVMCB->ctrl.IntCtrl.n.u1VIrqValid));
1036 Log(("ctrl.IntCtrl.u7Reserved %x\n", pVMCB->ctrl.IntCtrl.n.u7Reserved));
1037 Log(("ctrl.IntCtrl.u4VIrqPriority %x\n", pVMCB->ctrl.IntCtrl.n.u4VIrqPriority));
1038 Log(("ctrl.IntCtrl.u1IgnoreTPR %x\n", pVMCB->ctrl.IntCtrl.n.u1IgnoreTPR));
1039 Log(("ctrl.IntCtrl.u3Reserved %x\n", pVMCB->ctrl.IntCtrl.n.u3Reserved));
1040 Log(("ctrl.IntCtrl.u1VIrqMasking %x\n", pVMCB->ctrl.IntCtrl.n.u1VIrqMasking));
1041 Log(("ctrl.IntCtrl.u7Reserved2 %x\n", pVMCB->ctrl.IntCtrl.n.u7Reserved2));
1042 Log(("ctrl.IntCtrl.u8VIrqVector %x\n", pVMCB->ctrl.IntCtrl.n.u8VIrqVector));
1043 Log(("ctrl.IntCtrl.u24Reserved %x\n", pVMCB->ctrl.IntCtrl.n.u24Reserved));
1044
1045 Log(("ctrl.u64IntShadow %VX64\n", pVMCB->ctrl.u64IntShadow));
1046 Log(("ctrl.u64ExitCode %VX64\n", pVMCB->ctrl.u64ExitCode));
1047 Log(("ctrl.u64ExitInfo1 %VX64\n", pVMCB->ctrl.u64ExitInfo1));
1048 Log(("ctrl.u64ExitInfo2 %VX64\n", pVMCB->ctrl.u64ExitInfo2));
1049 Log(("ctrl.ExitIntInfo.u8Vector %x\n", pVMCB->ctrl.ExitIntInfo.n.u8Vector));
1050 Log(("ctrl.ExitIntInfo.u3Type %x\n", pVMCB->ctrl.ExitIntInfo.n.u3Type));
1051 Log(("ctrl.ExitIntInfo.u1ErrorCodeValid %x\n", pVMCB->ctrl.ExitIntInfo.n.u1ErrorCodeValid));
1052 Log(("ctrl.ExitIntInfo.u19Reserved %x\n", pVMCB->ctrl.ExitIntInfo.n.u19Reserved));
1053 Log(("ctrl.ExitIntInfo.u1Valid %x\n", pVMCB->ctrl.ExitIntInfo.n.u1Valid));
1054 Log(("ctrl.ExitIntInfo.u32ErrorCode %x\n", pVMCB->ctrl.ExitIntInfo.n.u32ErrorCode));
1055 Log(("ctrl.NestedPaging %VX64\n", pVMCB->ctrl.NestedPaging.au64));
1056 Log(("ctrl.EventInject.u8Vector %x\n", pVMCB->ctrl.EventInject.n.u8Vector));
1057 Log(("ctrl.EventInject.u3Type %x\n", pVMCB->ctrl.EventInject.n.u3Type));
1058 Log(("ctrl.EventInject.u1ErrorCodeValid %x\n", pVMCB->ctrl.EventInject.n.u1ErrorCodeValid));
1059 Log(("ctrl.EventInject.u19Reserved %x\n", pVMCB->ctrl.EventInject.n.u19Reserved));
1060 Log(("ctrl.EventInject.u1Valid %x\n", pVMCB->ctrl.EventInject.n.u1Valid));
1061 Log(("ctrl.EventInject.u32ErrorCode %x\n", pVMCB->ctrl.EventInject.n.u32ErrorCode));
1062
1063 Log(("ctrl.u64NestedPagingCR3 %VX64\n", pVMCB->ctrl.u64NestedPagingCR3));
1064 Log(("ctrl.u64LBRVirt %VX64\n", pVMCB->ctrl.u64LBRVirt));
1065
1066 Log(("guest.CS.u16Sel %04X\n", pVMCB->guest.CS.u16Sel));
1067 Log(("guest.CS.u16Attr %04X\n", pVMCB->guest.CS.u16Attr));
1068 Log(("guest.CS.u32Limit %X\n", pVMCB->guest.CS.u32Limit));
1069 Log(("guest.CS.u64Base %VX64\n", pVMCB->guest.CS.u64Base));
1070 Log(("guest.DS.u16Sel %04X\n", pVMCB->guest.DS.u16Sel));
1071 Log(("guest.DS.u16Attr %04X\n", pVMCB->guest.DS.u16Attr));
1072 Log(("guest.DS.u32Limit %X\n", pVMCB->guest.DS.u32Limit));
1073 Log(("guest.DS.u64Base %VX64\n", pVMCB->guest.DS.u64Base));
1074 Log(("guest.ES.u16Sel %04X\n", pVMCB->guest.ES.u16Sel));
1075 Log(("guest.ES.u16Attr %04X\n", pVMCB->guest.ES.u16Attr));
1076 Log(("guest.ES.u32Limit %X\n", pVMCB->guest.ES.u32Limit));
1077 Log(("guest.ES.u64Base %VX64\n", pVMCB->guest.ES.u64Base));
1078 Log(("guest.FS.u16Sel %04X\n", pVMCB->guest.FS.u16Sel));
1079 Log(("guest.FS.u16Attr %04X\n", pVMCB->guest.FS.u16Attr));
1080 Log(("guest.FS.u32Limit %X\n", pVMCB->guest.FS.u32Limit));
1081 Log(("guest.FS.u64Base %VX64\n", pVMCB->guest.FS.u64Base));
1082 Log(("guest.GS.u16Sel %04X\n", pVMCB->guest.GS.u16Sel));
1083 Log(("guest.GS.u16Attr %04X\n", pVMCB->guest.GS.u16Attr));
1084 Log(("guest.GS.u32Limit %X\n", pVMCB->guest.GS.u32Limit));
1085 Log(("guest.GS.u64Base %VX64\n", pVMCB->guest.GS.u64Base));
1086
1087 Log(("guest.GDTR.u32Limit %X\n", pVMCB->guest.GDTR.u32Limit));
1088 Log(("guest.GDTR.u64Base %VX64\n", pVMCB->guest.GDTR.u64Base));
1089
1090 Log(("guest.LDTR.u16Sel %04X\n", pVMCB->guest.LDTR.u16Sel));
1091 Log(("guest.LDTR.u16Attr %04X\n", pVMCB->guest.LDTR.u16Attr));
1092 Log(("guest.LDTR.u32Limit %X\n", pVMCB->guest.LDTR.u32Limit));
1093 Log(("guest.LDTR.u64Base %VX64\n", pVMCB->guest.LDTR.u64Base));
1094
1095 Log(("guest.IDTR.u32Limit %X\n", pVMCB->guest.IDTR.u32Limit));
1096 Log(("guest.IDTR.u64Base %VX64\n", pVMCB->guest.IDTR.u64Base));
1097
1098 Log(("guest.TR.u16Sel %04X\n", pVMCB->guest.TR.u16Sel));
1099 Log(("guest.TR.u16Attr %04X\n", pVMCB->guest.TR.u16Attr));
1100 Log(("guest.TR.u32Limit %X\n", pVMCB->guest.TR.u32Limit));
1101 Log(("guest.TR.u64Base %VX64\n", pVMCB->guest.TR.u64Base));
1102
1103 Log(("guest.u8CPL %X\n", pVMCB->guest.u8CPL));
1104 Log(("guest.u64CR0 %VX64\n", pVMCB->guest.u64CR0));
1105 Log(("guest.u64CR2 %VX64\n", pVMCB->guest.u64CR2));
1106 Log(("guest.u64CR3 %VX64\n", pVMCB->guest.u64CR3));
1107 Log(("guest.u64CR4 %VX64\n", pVMCB->guest.u64CR4));
1108 Log(("guest.u64DR6 %VX64\n", pVMCB->guest.u64DR6));
1109 Log(("guest.u64DR7 %VX64\n", pVMCB->guest.u64DR7));
1110
1111 Log(("guest.u64RIP %VX64\n", pVMCB->guest.u64RIP));
1112 Log(("guest.u64RSP %VX64\n", pVMCB->guest.u64RSP));
1113 Log(("guest.u64RAX %VX64\n", pVMCB->guest.u64RAX));
1114 Log(("guest.u64RFlags %VX64\n", pVMCB->guest.u64RFlags));
1115
1116 Log(("guest.u64SysEnterCS %VX64\n", pVMCB->guest.u64SysEnterCS));
1117 Log(("guest.u64SysEnterEIP %VX64\n", pVMCB->guest.u64SysEnterEIP));
1118 Log(("guest.u64SysEnterESP %VX64\n", pVMCB->guest.u64SysEnterESP));
1119
1120 Log(("guest.u64EFER %VX64\n", pVMCB->guest.u64EFER));
1121 Log(("guest.u64STAR %VX64\n", pVMCB->guest.u64STAR));
1122 Log(("guest.u64LSTAR %VX64\n", pVMCB->guest.u64LSTAR));
1123 Log(("guest.u64CSTAR %VX64\n", pVMCB->guest.u64CSTAR));
1124 Log(("guest.u64SFMASK %VX64\n", pVMCB->guest.u64SFMASK));
1125 Log(("guest.u64KernelGSBase %VX64\n", pVMCB->guest.u64KernelGSBase));
1126 Log(("guest.u64GPAT %VX64\n", pVMCB->guest.u64GPAT));
1127 Log(("guest.u64DBGCTL %VX64\n", pVMCB->guest.u64DBGCTL));
1128 Log(("guest.u64BR_FROM %VX64\n", pVMCB->guest.u64BR_FROM));
1129 Log(("guest.u64BR_TO %VX64\n", pVMCB->guest.u64BR_TO));
1130 Log(("guest.u64LASTEXCPFROM %VX64\n", pVMCB->guest.u64LASTEXCPFROM));
1131 Log(("guest.u64LASTEXCPTO %VX64\n", pVMCB->guest.u64LASTEXCPTO));
1132
1133#endif
1134 rc = VERR_SVM_UNABLE_TO_START_VM;
1135 goto end;
1136 }
1137
1138 /* Let's first sync back eip, esp, and eflags. */
1139 pCtx->rip = pVMCB->guest.u64RIP;
1140 pCtx->rsp = pVMCB->guest.u64RSP;
1141 pCtx->eflags.u32 = pVMCB->guest.u64RFlags;
1142 /* eax is saved/restore across the vmrun instruction */
1143 pCtx->rax = pVMCB->guest.u64RAX;
1144
1145 pCtx->msrKERNELGSBASE = pVMCB->guest.u64KernelGSBase; /* swapgs exchange value */
1146
1147 /* Guest CPU context: ES, CS, SS, DS, FS, GS. */
1148 SVM_READ_SELREG(SS, ss);
1149 SVM_READ_SELREG(CS, cs);
1150 SVM_READ_SELREG(DS, ds);
1151 SVM_READ_SELREG(ES, es);
1152 SVM_READ_SELREG(FS, fs);
1153 SVM_READ_SELREG(GS, gs);
1154
1155 /* Note: no reason to sync back the CRx and DRx registers. They can't be changed by the guest. */
1156 /* Note: only in the nested paging case can CR3 & CR4 be changed by the guest. */
1157 if ( pVM->hwaccm.s.fNestedPaging
1158 && pCtx->cr3 != pVMCB->guest.u64CR3)
1159 {
1160 CPUMSetGuestCR3(pVM, pVMCB->guest.u64CR3);
1161 PGMUpdateCR3(pVM, pVMCB->guest.u64CR3);
1162 }
1163
1164 /** @note NOW IT'S SAFE FOR LOGGING! */
1165
1166 /* Take care of instruction fusing (sti, mov ss) (see 15.20.5 Interrupt Shadows) */
1167 if (pVMCB->ctrl.u64IntShadow & SVM_INTERRUPT_SHADOW_ACTIVE)
1168 {
1169 Log(("uInterruptState %x eip=%VGv\n", pVMCB->ctrl.u64IntShadow, pCtx->rip));
1170 EMSetInhibitInterruptsPC(pVM, pCtx->rip);
1171 }
1172 else
1173 VM_FF_CLEAR(pVM, VM_FF_INHIBIT_INTERRUPTS);
1174
1175 Log2(("exitCode = %x\n", exitCode));
1176
1177 /* Sync back the debug registers. */
1178 /** @todo Implement debug registers correctly. */
1179 pCtx->dr6 = pVMCB->guest.u64DR6;
1180 pCtx->dr7 = pVMCB->guest.u64DR7;
1181
1182 /* Check if an injected event was interrupted prematurely. */
1183 pVM->hwaccm.s.Event.intInfo = pVMCB->ctrl.ExitIntInfo.au64[0];
1184 if ( pVMCB->ctrl.ExitIntInfo.n.u1Valid
1185 && pVMCB->ctrl.ExitIntInfo.n.u3Type != SVM_EVENT_SOFTWARE_INT /* we don't care about 'int xx' as the instruction will be restarted. */)
1186 {
1187 Log(("Pending inject %VX64 at %VGv exit=%08x\n", pVM->hwaccm.s.Event.intInfo, pCtx->rip, exitCode));
1188
1189#ifdef LOG_ENABLED
1190 SVM_EVENT Event;
1191 Event.au64[0] = pVM->hwaccm.s.Event.intInfo;
1192
1193 if ( exitCode == SVM_EXIT_EXCEPTION_E
1194 && Event.n.u8Vector == 0xE)
1195 {
1196 Log(("Double fault!\n"));
1197 }
1198#endif
1199
1200 pVM->hwaccm.s.Event.fPending = true;
1201 /* Error code present? (redundant) */
1202 if (pVMCB->ctrl.ExitIntInfo.n.u1ErrorCodeValid)
1203 {
1204 pVM->hwaccm.s.Event.errCode = pVMCB->ctrl.ExitIntInfo.n.u32ErrorCode;
1205 }
1206 else
1207 pVM->hwaccm.s.Event.errCode = 0;
1208 }
1209#ifdef VBOX_WITH_STATISTICS
1210 if (exitCode == SVM_EXIT_NPF)
1211 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitReasonNPF);
1212 else
1213 STAM_COUNTER_INC(&pVM->hwaccm.s.pStatExitReasonR0[exitCode & MASK_EXITREASON_STAT]);
1214#endif
1215
1216 if (fSyncTPR)
1217 {
1218 rc = PDMApicSetTPR(pVM, pVMCB->ctrl.IntCtrl.n.u8VTPR);
1219 AssertRC(rc);
1220 }
1221
1222 /* Deal with the reason of the VM-exit. */
1223 switch (exitCode)
1224 {
1225 case SVM_EXIT_EXCEPTION_0: case SVM_EXIT_EXCEPTION_1: case SVM_EXIT_EXCEPTION_2: case SVM_EXIT_EXCEPTION_3:
1226 case SVM_EXIT_EXCEPTION_4: case SVM_EXIT_EXCEPTION_5: case SVM_EXIT_EXCEPTION_6: case SVM_EXIT_EXCEPTION_7:
1227 case SVM_EXIT_EXCEPTION_8: case SVM_EXIT_EXCEPTION_9: case SVM_EXIT_EXCEPTION_A: case SVM_EXIT_EXCEPTION_B:
1228 case SVM_EXIT_EXCEPTION_C: case SVM_EXIT_EXCEPTION_D: case SVM_EXIT_EXCEPTION_E: case SVM_EXIT_EXCEPTION_F:
1229 case SVM_EXIT_EXCEPTION_10: case SVM_EXIT_EXCEPTION_11: case SVM_EXIT_EXCEPTION_12: case SVM_EXIT_EXCEPTION_13:
1230 case SVM_EXIT_EXCEPTION_14: case SVM_EXIT_EXCEPTION_15: case SVM_EXIT_EXCEPTION_16: case SVM_EXIT_EXCEPTION_17:
1231 case SVM_EXIT_EXCEPTION_18: case SVM_EXIT_EXCEPTION_19: case SVM_EXIT_EXCEPTION_1A: case SVM_EXIT_EXCEPTION_1B:
1232 case SVM_EXIT_EXCEPTION_1C: case SVM_EXIT_EXCEPTION_1D: case SVM_EXIT_EXCEPTION_1E: case SVM_EXIT_EXCEPTION_1F:
1233 {
1234 /* Pending trap. */
1235 SVM_EVENT Event;
1236 uint32_t vector = exitCode - SVM_EXIT_EXCEPTION_0;
1237
1238 Log2(("Hardware/software interrupt %d\n", vector));
1239 switch (vector)
1240 {
1241#ifdef DEBUG
1242 case X86_XCPT_DB:
1243 rc = DBGFR0Trap01Handler(pVM, CPUMCTX2CORE(pCtx), pVMCB->guest.u64DR6);
1244 Assert(rc != VINF_EM_RAW_GUEST_TRAP);
1245 break;
1246#endif
1247
1248 case X86_XCPT_NM:
1249 {
1250 Log(("#NM fault at %VGv\n", pCtx->rip));
1251
1252 /** @todo don't intercept #NM exceptions anymore when we've activated the guest FPU state. */
1253 /* If we sync the FPU/XMM state on-demand, then we can continue execution as if nothing has happened. */
1254 rc = CPUMR0LoadGuestFPU(pVM, pCtx);
1255 if (rc == VINF_SUCCESS)
1256 {
1257 Assert(CPUMIsGuestFPUStateActive(pVM));
1258 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitShadowNM);
1259
1260 /* Continue execution. */
1261 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1262 pVM->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_CR0;
1263
1264 goto ResumeExecution;
1265 }
1266
1267 Log(("Forward #NM fault to the guest\n"));
1268 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestNM);
1269
1270 Event.au64[0] = 0;
1271 Event.n.u3Type = SVM_EVENT_EXCEPTION;
1272 Event.n.u1Valid = 1;
1273 Event.n.u8Vector = X86_XCPT_NM;
1274
1275 SVMR0InjectEvent(pVM, pVMCB, pCtx, &Event);
1276 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1277 goto ResumeExecution;
1278 }
1279
1280 case X86_XCPT_PF: /* Page fault */
1281 {
1282 uint32_t errCode = pVMCB->ctrl.u64ExitInfo1; /* EXITINFO1 = error code */
1283 RTGCUINTPTR uFaultAddress = pVMCB->ctrl.u64ExitInfo2; /* EXITINFO2 = fault address */
1284
1285#ifdef DEBUG
1286 if (pVM->hwaccm.s.fNestedPaging)
1287 { /* A genuine pagefault.
1288 * Forward the trap to the guest by injecting the exception and resuming execution.
1289 */
1290 Log(("Guest page fault at %VGv cr2=%VGv error code %x rsp=%VGv\n", (RTGCPTR)pCtx->rip, uFaultAddress, errCode, (RTGCPTR)pCtx->rsp));
1291 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestPF);
1292
1293 /* Now we must update CR2. */
1294 pCtx->cr2 = uFaultAddress;
1295
1296 Event.au64[0] = 0;
1297 Event.n.u3Type = SVM_EVENT_EXCEPTION;
1298 Event.n.u1Valid = 1;
1299 Event.n.u8Vector = X86_XCPT_PF;
1300 Event.n.u1ErrorCodeValid = 1;
1301 Event.n.u32ErrorCode = errCode;
1302
1303 SVMR0InjectEvent(pVM, pVMCB, pCtx, &Event);
1304
1305 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1306 goto ResumeExecution;
1307 }
1308#endif
1309 Assert(!pVM->hwaccm.s.fNestedPaging);
1310
1311 Log2(("Page fault at %VGv cr2=%VGv error code %x\n", pCtx->rip, uFaultAddress, errCode));
1312 /* Exit qualification contains the linear address of the page fault. */
1313 TRPMAssertTrap(pVM, X86_XCPT_PF, TRPM_TRAP);
1314 TRPMSetErrorCode(pVM, errCode);
1315 TRPMSetFaultAddress(pVM, uFaultAddress);
1316
1317 /* Forward it to our trap handler first, in case our shadow pages are out of sync. */
1318 rc = PGMTrap0eHandler(pVM, errCode, CPUMCTX2CORE(pCtx), (RTGCPTR)uFaultAddress);
1319 Log2(("PGMTrap0eHandler %VGv returned %Vrc\n", pCtx->rip, rc));
1320 if (rc == VINF_SUCCESS)
1321 { /* We've successfully synced our shadow pages, so let's just continue execution. */
1322 Log2(("Shadow page fault at %VGv cr2=%VGv error code %x\n", pCtx->rip, uFaultAddress, errCode));
1323 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitShadowPF);
1324
1325 TRPMResetTrap(pVM);
1326
1327 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1328 goto ResumeExecution;
1329 }
1330 else
1331 if (rc == VINF_EM_RAW_GUEST_TRAP)
1332 { /* A genuine pagefault.
1333 * Forward the trap to the guest by injecting the exception and resuming execution.
1334 */
1335 Log2(("Forward page fault to the guest\n"));
1336 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestPF);
1337 /* The error code might have been changed. */
1338 errCode = TRPMGetErrorCode(pVM);
1339
1340 TRPMResetTrap(pVM);
1341
1342 /* Now we must update CR2. */
1343 pCtx->cr2 = uFaultAddress;
1344
1345 Event.au64[0] = 0;
1346 Event.n.u3Type = SVM_EVENT_EXCEPTION;
1347 Event.n.u1Valid = 1;
1348 Event.n.u8Vector = X86_XCPT_PF;
1349 Event.n.u1ErrorCodeValid = 1;
1350 Event.n.u32ErrorCode = errCode;
1351
1352 SVMR0InjectEvent(pVM, pVMCB, pCtx, &Event);
1353
1354 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1355 goto ResumeExecution;
1356 }
1357#ifdef VBOX_STRICT
1358 if (rc != VINF_EM_RAW_EMULATE_INSTR)
1359 LogFlow(("PGMTrap0eHandler failed with %d\n", rc));
1360#endif
1361 /* Need to go back to the recompiler to emulate the instruction. */
1362 TRPMResetTrap(pVM);
1363 break;
1364 }
1365
1366 case X86_XCPT_MF: /* Floating point exception. */
1367 {
1368 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestMF);
1369 if (!(pCtx->cr0 & X86_CR0_NE))
1370 {
1371 /* old style FPU error reporting needs some extra work. */
1372 /** @todo don't fall back to the recompiler, but do it manually. */
1373 rc = VINF_EM_RAW_EMULATE_INSTR;
1374 break;
1375 }
1376 Log(("Trap %x at %VGv\n", vector, pCtx->rip));
1377
1378 Event.au64[0] = 0;
1379 Event.n.u3Type = SVM_EVENT_EXCEPTION;
1380 Event.n.u1Valid = 1;
1381 Event.n.u8Vector = X86_XCPT_MF;
1382
1383 SVMR0InjectEvent(pVM, pVMCB, pCtx, &Event);
1384
1385 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1386 goto ResumeExecution;
1387 }
1388
1389#ifdef VBOX_STRICT
1390 case X86_XCPT_GP: /* General protection failure exception.*/
1391 case X86_XCPT_UD: /* Unknown opcode exception. */
1392 case X86_XCPT_DE: /* Debug exception. */
1393 case X86_XCPT_SS: /* Stack segment exception. */
1394 case X86_XCPT_NP: /* Segment not present exception. */
1395 {
1396 Event.au64[0] = 0;
1397 Event.n.u3Type = SVM_EVENT_EXCEPTION;
1398 Event.n.u1Valid = 1;
1399 Event.n.u8Vector = vector;
1400
1401 switch(vector)
1402 {
1403 case X86_XCPT_GP:
1404 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestGP);
1405 Event.n.u1ErrorCodeValid = 1;
1406 Event.n.u32ErrorCode = pVMCB->ctrl.u64ExitInfo1; /* EXITINFO1 = error code */
1407 break;
1408 case X86_XCPT_DE:
1409 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestDE);
1410 break;
1411 case X86_XCPT_UD:
1412 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestUD);
1413 break;
1414 case X86_XCPT_SS:
1415 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestSS);
1416 Event.n.u1ErrorCodeValid = 1;
1417 Event.n.u32ErrorCode = pVMCB->ctrl.u64ExitInfo1; /* EXITINFO1 = error code */
1418 break;
1419 case X86_XCPT_NP:
1420 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestNP);
1421 Event.n.u1ErrorCodeValid = 1;
1422 Event.n.u32ErrorCode = pVMCB->ctrl.u64ExitInfo1; /* EXITINFO1 = error code */
1423 break;
1424 }
1425 Log(("Trap %x at %VGv esi=%x\n", vector, pCtx->rip, pCtx->esi));
1426 SVMR0InjectEvent(pVM, pVMCB, pCtx, &Event);
1427
1428 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1429 goto ResumeExecution;
1430 }
1431#endif
1432 default:
1433 AssertMsgFailed(("Unexpected vm-exit caused by exception %x\n", vector));
1434 rc = VERR_EM_INTERNAL_ERROR;
1435 break;
1436
1437 } /* switch (vector) */
1438 break;
1439 }
1440
1441 case SVM_EXIT_NPF:
1442 {
1443 /* EXITINFO1 contains fault errorcode; EXITINFO2 contains the guest physical address causing the fault. */
1444 uint32_t errCode = pVMCB->ctrl.u64ExitInfo1; /* EXITINFO1 = error code */
1445 RTGCPHYS uFaultAddress = pVMCB->ctrl.u64ExitInfo2; /* EXITINFO2 = fault address */
1446
1447 Assert(pVM->hwaccm.s.fNestedPaging);
1448 Log(("Nested page fault at %VGv cr2=%VGp error code %x\n", pCtx->rip, uFaultAddress, errCode));
1449 /* Exit qualification contains the linear address of the page fault. */
1450 TRPMAssertTrap(pVM, X86_XCPT_PF, TRPM_TRAP);
1451 TRPMSetErrorCode(pVM, errCode);
1452 TRPMSetFaultAddress(pVM, uFaultAddress);
1453
1454 /* Handle the pagefault trap for the nested shadow table. */
1455 rc = PGMR0Trap0eHandlerNestedPaging(pVM, PGMGetHostMode(pVM), errCode, CPUMCTX2CORE(pCtx), uFaultAddress);
1456 Log2(("PGMR0Trap0eHandlerNestedPaging %VGv returned %Vrc\n", pCtx->rip, rc));
1457 if (rc == VINF_SUCCESS)
1458 { /* We've successfully synced our shadow pages, so let's just continue execution. */
1459 Log2(("Shadow page fault at %VGv cr2=%VGp error code %x\n", pCtx->rip, uFaultAddress, errCode));
1460 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitShadowPF);
1461
1462 TRPMResetTrap(pVM);
1463
1464 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1465 goto ResumeExecution;
1466 }
1467
1468#ifdef VBOX_STRICT
1469 if (rc != VINF_EM_RAW_EMULATE_INSTR)
1470 LogFlow(("PGMTrap0eHandlerNestedPaging failed with %d\n", rc));
1471#endif
1472 /* Need to go back to the recompiler to emulate the instruction. */
1473 TRPMResetTrap(pVM);
1474 break;
1475 }
1476
1477 case SVM_EXIT_VINTR:
1478 /* A virtual interrupt is about to be delivered, which means IF=1. */
1479 Log(("SVM_EXIT_VINTR IF=%d\n", pCtx->eflags.Bits.u1IF));
1480 pVMCB->ctrl.IntCtrl.n.u1VIrqValid = 0;
1481 pVMCB->ctrl.IntCtrl.n.u8VIrqVector = 0;
1482 goto ResumeExecution;
1483
1484 case SVM_EXIT_FERR_FREEZE:
1485 case SVM_EXIT_INTR:
1486 case SVM_EXIT_NMI:
1487 case SVM_EXIT_SMI:
1488 case SVM_EXIT_INIT:
1489 /* External interrupt; leave to allow it to be dispatched again. */
1490 rc = VINF_EM_RAW_INTERRUPT;
1491 break;
1492
1493 case SVM_EXIT_WBINVD:
1494 case SVM_EXIT_INVD: /* Guest software attempted to execute INVD. */
1495 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitInvd);
1496 /* Skip instruction and continue directly. */
1497 pCtx->rip += 2; /** @note hardcoded opcode size! */
1498 /* Continue execution.*/
1499 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1500 goto ResumeExecution;
1501
1502 case SVM_EXIT_CPUID: /* Guest software attempted to execute CPUID. */
1503 {
1504 Log2(("SVM: Cpuid at %VGv for %x\n", pCtx->rip, pCtx->eax));
1505 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitCpuid);
1506 rc = EMInterpretCpuId(pVM, CPUMCTX2CORE(pCtx));
1507 if (rc == VINF_SUCCESS)
1508 {
1509 /* Update EIP and continue execution. */
1510 pCtx->rip += 2; /** @note hardcoded opcode size! */
1511 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1512 goto ResumeExecution;
1513 }
1514 AssertMsgFailed(("EMU: cpuid failed with %Vrc\n", rc));
1515 rc = VINF_EM_RAW_EMULATE_INSTR;
1516 break;
1517 }
1518
1519 case SVM_EXIT_RDTSC: /* Guest software attempted to execute RDTSC. */
1520 {
1521 Log2(("SVM: Rdtsc\n"));
1522 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitRdtsc);
1523 rc = EMInterpretRdtsc(pVM, CPUMCTX2CORE(pCtx));
1524 if (rc == VINF_SUCCESS)
1525 {
1526 /* Update EIP and continue execution. */
1527 pCtx->rip += 2; /** @note hardcoded opcode size! */
1528 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1529 goto ResumeExecution;
1530 }
1531 AssertMsgFailed(("EMU: rdtsc failed with %Vrc\n", rc));
1532 rc = VINF_EM_RAW_EMULATE_INSTR;
1533 break;
1534 }
1535
1536 case SVM_EXIT_INVLPG: /* Guest software attempted to execute INVPG. */
1537 {
1538 Log2(("SVM: invlpg\n"));
1539 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitInvpg);
1540
1541 Assert(!pVM->hwaccm.s.fNestedPaging);
1542
1543 /* Truly a pita. Why can't SVM give the same information as VT-x? */
1544 rc = SVMR0InterpretInvpg(pVM, CPUMCTX2CORE(pCtx), pVMCB->ctrl.TLBCtrl.n.u32ASID);
1545 if (rc == VINF_SUCCESS)
1546 {
1547 STAM_COUNTER_INC(&pVM->hwaccm.s.StatFlushPageInvlpg);
1548 goto ResumeExecution; /* eip already updated */
1549 }
1550 break;
1551 }
1552
1553 case SVM_EXIT_WRITE_CR0: case SVM_EXIT_WRITE_CR1: case SVM_EXIT_WRITE_CR2: case SVM_EXIT_WRITE_CR3:
1554 case SVM_EXIT_WRITE_CR4: case SVM_EXIT_WRITE_CR5: case SVM_EXIT_WRITE_CR6: case SVM_EXIT_WRITE_CR7:
1555 case SVM_EXIT_WRITE_CR8: case SVM_EXIT_WRITE_CR9: case SVM_EXIT_WRITE_CR10: case SVM_EXIT_WRITE_CR11:
1556 case SVM_EXIT_WRITE_CR12: case SVM_EXIT_WRITE_CR13: case SVM_EXIT_WRITE_CR14: case SVM_EXIT_WRITE_CR15:
1557 {
1558 uint32_t cbSize;
1559
1560 Log2(("SVM: %VGv mov cr%d, \n", pCtx->rip, exitCode - SVM_EXIT_WRITE_CR0));
1561 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitCRxWrite);
1562 rc = EMInterpretInstruction(pVM, CPUMCTX2CORE(pCtx), 0, &cbSize);
1563
1564 switch (exitCode - SVM_EXIT_WRITE_CR0)
1565 {
1566 case 0:
1567 pVM->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_CR0;
1568 break;
1569 case 2:
1570 break;
1571 case 3:
1572 Assert(!pVM->hwaccm.s.fNestedPaging);
1573 pVM->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_CR3;
1574 break;
1575 case 4:
1576 pVM->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_CR4;
1577 break;
1578 case 8:
1579 break;
1580 default:
1581 AssertFailed();
1582 }
1583 /* Check if a sync operation is pending. */
1584 if ( rc == VINF_SUCCESS /* don't bother if we are going to ring 3 anyway */
1585 && VM_FF_ISPENDING(pVM, VM_FF_PGM_SYNC_CR3 | VM_FF_PGM_SYNC_CR3_NON_GLOBAL))
1586 {
1587 rc = PGMSyncCR3(pVM, CPUMGetGuestCR0(pVM), CPUMGetGuestCR3(pVM), CPUMGetGuestCR4(pVM), VM_FF_ISSET(pVM, VM_FF_PGM_SYNC_CR3));
1588 AssertRC(rc);
1589
1590 STAM_COUNTER_INC(&pVM->hwaccm.s.StatFlushTLBCRxChange);
1591
1592 /* Must be set by PGMSyncCR3 */
1593 Assert(PGMGetGuestMode(pVM) <= PGMMODE_PROTECTED || pVM->hwaccm.s.svm.fForceTLBFlush);
1594 }
1595 if (rc == VINF_SUCCESS)
1596 {
1597 /* EIP has been updated already. */
1598
1599 /* Only resume if successful. */
1600 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1601 goto ResumeExecution;
1602 }
1603 Assert(rc == VERR_EM_INTERPRETER || rc == VINF_PGM_CHANGE_MODE || rc == VINF_PGM_SYNC_CR3);
1604 break;
1605 }
1606
1607 case SVM_EXIT_READ_CR0: case SVM_EXIT_READ_CR1: case SVM_EXIT_READ_CR2: case SVM_EXIT_READ_CR3:
1608 case SVM_EXIT_READ_CR4: case SVM_EXIT_READ_CR5: case SVM_EXIT_READ_CR6: case SVM_EXIT_READ_CR7:
1609 case SVM_EXIT_READ_CR8: case SVM_EXIT_READ_CR9: case SVM_EXIT_READ_CR10: case SVM_EXIT_READ_CR11:
1610 case SVM_EXIT_READ_CR12: case SVM_EXIT_READ_CR13: case SVM_EXIT_READ_CR14: case SVM_EXIT_READ_CR15:
1611 {
1612 uint32_t cbSize;
1613
1614 Log2(("SVM: %VGv mov x, cr%d\n", pCtx->rip, exitCode - SVM_EXIT_READ_CR0));
1615 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitCRxRead);
1616 rc = EMInterpretInstruction(pVM, CPUMCTX2CORE(pCtx), 0, &cbSize);
1617 if (rc == VINF_SUCCESS)
1618 {
1619 /* EIP has been updated already. */
1620
1621 /* Only resume if successful. */
1622 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1623 goto ResumeExecution;
1624 }
1625 Assert(rc == VERR_EM_INTERPRETER || rc == VINF_PGM_CHANGE_MODE || rc == VINF_PGM_SYNC_CR3);
1626 break;
1627 }
1628
1629 case SVM_EXIT_WRITE_DR0: case SVM_EXIT_WRITE_DR1: case SVM_EXIT_WRITE_DR2: case SVM_EXIT_WRITE_DR3:
1630 case SVM_EXIT_WRITE_DR4: case SVM_EXIT_WRITE_DR5: case SVM_EXIT_WRITE_DR6: case SVM_EXIT_WRITE_DR7:
1631 case SVM_EXIT_WRITE_DR8: case SVM_EXIT_WRITE_DR9: case SVM_EXIT_WRITE_DR10: case SVM_EXIT_WRITE_DR11:
1632 case SVM_EXIT_WRITE_DR12: case SVM_EXIT_WRITE_DR13: case SVM_EXIT_WRITE_DR14: case SVM_EXIT_WRITE_DR15:
1633 {
1634 uint32_t cbSize;
1635
1636 Log2(("SVM: %VGv mov dr%d, x\n", pCtx->rip, exitCode - SVM_EXIT_WRITE_DR0));
1637 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitDRxRead);
1638 rc = EMInterpretInstruction(pVM, CPUMCTX2CORE(pCtx), 0, &cbSize);
1639 if (rc == VINF_SUCCESS)
1640 {
1641 /* EIP has been updated already. */
1642
1643 /* Only resume if successful. */
1644 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1645 goto ResumeExecution;
1646 }
1647 Assert(rc == VERR_EM_INTERPRETER || rc == VINF_PGM_CHANGE_MODE || rc == VINF_PGM_SYNC_CR3);
1648 break;
1649 }
1650
1651 case SVM_EXIT_READ_DR0: case SVM_EXIT_READ_DR1: case SVM_EXIT_READ_DR2: case SVM_EXIT_READ_DR3:
1652 case SVM_EXIT_READ_DR4: case SVM_EXIT_READ_DR5: case SVM_EXIT_READ_DR6: case SVM_EXIT_READ_DR7:
1653 case SVM_EXIT_READ_DR8: case SVM_EXIT_READ_DR9: case SVM_EXIT_READ_DR10: case SVM_EXIT_READ_DR11:
1654 case SVM_EXIT_READ_DR12: case SVM_EXIT_READ_DR13: case SVM_EXIT_READ_DR14: case SVM_EXIT_READ_DR15:
1655 {
1656 uint32_t cbSize;
1657
1658 Log2(("SVM: %VGv mov dr%d, x\n", pCtx->rip, exitCode - SVM_EXIT_READ_DR0));
1659 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitDRxRead);
1660 rc = EMInterpretInstruction(pVM, CPUMCTX2CORE(pCtx), 0, &cbSize);
1661 if (rc == VINF_SUCCESS)
1662 {
1663 /* EIP has been updated already. */
1664
1665 /* Only resume if successful. */
1666 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1667 goto ResumeExecution;
1668 }
1669 Assert(rc == VERR_EM_INTERPRETER || rc == VINF_PGM_CHANGE_MODE || rc == VINF_PGM_SYNC_CR3);
1670 break;
1671 }
1672
1673 /* Note: We'll get a #GP if the IO instruction isn't allowed (IOPL or TSS bitmap); no need to double check. */
1674 case SVM_EXIT_IOIO: /* I/O instruction. */
1675 {
1676 SVM_IOIO_EXIT IoExitInfo;
1677 uint32_t uIOSize, uAndVal;
1678
1679 IoExitInfo.au32[0] = pVMCB->ctrl.u64ExitInfo1;
1680
1681 /** @todo could use a lookup table here */
1682 if (IoExitInfo.n.u1OP8)
1683 {
1684 uIOSize = 1;
1685 uAndVal = 0xff;
1686 }
1687 else
1688 if (IoExitInfo.n.u1OP16)
1689 {
1690 uIOSize = 2;
1691 uAndVal = 0xffff;
1692 }
1693 else
1694 if (IoExitInfo.n.u1OP32)
1695 {
1696 uIOSize = 4;
1697 uAndVal = 0xffffffff;
1698 }
1699 else
1700 {
1701 AssertFailed(); /* should be fatal. */
1702 rc = VINF_EM_RAW_EMULATE_INSTR;
1703 break;
1704 }
1705
1706 if (IoExitInfo.n.u1STR)
1707 {
1708 /* ins/outs */
1709 uint32_t prefix = 0;
1710 if (IoExitInfo.n.u1REP)
1711 prefix |= PREFIX_REP;
1712
1713 if (IoExitInfo.n.u1Type == 0)
1714 {
1715 Log2(("IOMInterpretOUTSEx %VGv %x size=%d\n", pCtx->rip, IoExitInfo.n.u16Port, uIOSize));
1716 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitIOStringWrite);
1717 rc = IOMInterpretOUTSEx(pVM, CPUMCTX2CORE(pCtx), IoExitInfo.n.u16Port, prefix, uIOSize);
1718 }
1719 else
1720 {
1721 Log2(("IOMInterpretINSEx %VGv %x size=%d\n", pCtx->rip, IoExitInfo.n.u16Port, uIOSize));
1722 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitIOStringRead);
1723 rc = IOMInterpretINSEx(pVM, CPUMCTX2CORE(pCtx), IoExitInfo.n.u16Port, prefix, uIOSize);
1724 }
1725 }
1726 else
1727 {
1728 /* normal in/out */
1729 Assert(!IoExitInfo.n.u1REP);
1730
1731 if (IoExitInfo.n.u1Type == 0)
1732 {
1733 Log2(("IOMIOPortWrite %VGv %x %x size=%d\n", pCtx->rip, IoExitInfo.n.u16Port, pCtx->eax & uAndVal, uIOSize));
1734 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitIOWrite);
1735 rc = IOMIOPortWrite(pVM, IoExitInfo.n.u16Port, pCtx->eax & uAndVal, uIOSize);
1736 }
1737 else
1738 {
1739 uint32_t u32Val = 0;
1740
1741 STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitIORead);
1742 rc = IOMIOPortRead(pVM, IoExitInfo.n.u16Port, &u32Val, uIOSize);
1743 if (IOM_SUCCESS(rc))
1744 {
1745 /* Write back to the EAX register. */
1746 pCtx->eax = (pCtx->eax & ~uAndVal) | (u32Val & uAndVal);
1747 Log2(("IOMIOPortRead %VGv %x %x size=%d\n", pCtx->rip, IoExitInfo.n.u16Port, u32Val & uAndVal, uIOSize));
1748 }
1749 }
1750 }
1751 /*
1752 * Handled the I/O return codes.
1753 * (The unhandled cases end up with rc == VINF_EM_RAW_EMULATE_INSTR.)
1754 */
1755 if (IOM_SUCCESS(rc))
1756 {
1757 /* Update EIP and continue execution. */
1758 pCtx->rip = pVMCB->ctrl.u64ExitInfo2; /* RIP/EIP of the next instruction is saved in EXITINFO2. */
1759 if (RT_LIKELY(rc == VINF_SUCCESS))
1760 {
1761 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1762 goto ResumeExecution;
1763 }
1764 Log2(("EM status from IO at %VGv %x size %d: %Vrc\n", pCtx->rip, IoExitInfo.n.u16Port, uIOSize, rc));
1765 break;
1766 }
1767
1768#ifdef VBOX_STRICT
1769 if (rc == VINF_IOM_HC_IOPORT_READ)
1770 Assert(IoExitInfo.n.u1Type != 0);
1771 else if (rc == VINF_IOM_HC_IOPORT_WRITE)
1772 Assert(IoExitInfo.n.u1Type == 0);
1773 else
1774 AssertMsg(VBOX_FAILURE(rc) || rc == VINF_EM_RAW_EMULATE_INSTR || rc == VINF_EM_RAW_GUEST_TRAP || rc == VINF_TRPM_XCPT_DISPATCHED, ("%Vrc\n", rc));
1775#endif
1776 Log2(("Failed IO at %VGv %x size %d\n", pCtx->rip, IoExitInfo.n.u16Port, uIOSize));
1777 break;
1778 }
1779
1780 case SVM_EXIT_HLT:
1781 /** Check if external interrupts are pending; if so, don't switch back. */
1782 pCtx->rip++; /* skip hlt */
1783 if ( pCtx->eflags.Bits.u1IF
1784 && VM_FF_ISPENDING(pVM, (VM_FF_INTERRUPT_APIC|VM_FF_INTERRUPT_PIC)))
1785 goto ResumeExecution;
1786
1787 rc = VINF_EM_HALT;
1788 break;
1789
1790 case SVM_EXIT_RSM:
1791 case SVM_EXIT_INVLPGA:
1792 case SVM_EXIT_VMRUN:
1793 case SVM_EXIT_VMMCALL:
1794 case SVM_EXIT_VMLOAD:
1795 case SVM_EXIT_VMSAVE:
1796 case SVM_EXIT_STGI:
1797 case SVM_EXIT_CLGI:
1798 case SVM_EXIT_SKINIT:
1799 case SVM_EXIT_RDTSCP:
1800 {
1801 /* Unsupported instructions. */
1802 SVM_EVENT Event;
1803
1804 Event.au64[0] = 0;
1805 Event.n.u3Type = SVM_EVENT_EXCEPTION;
1806 Event.n.u1Valid = 1;
1807 Event.n.u8Vector = X86_XCPT_UD;
1808
1809 Log(("Forced #UD trap at %VGv\n", pCtx->rip));
1810 SVMR0InjectEvent(pVM, pVMCB, pCtx, &Event);
1811
1812 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1813 goto ResumeExecution;
1814 }
1815
1816 /* Emulate in ring 3. */
1817 case SVM_EXIT_MSR:
1818 {
1819 uint32_t cbSize;
1820
1821 /* Note: the intel manual claims there's a REX version of RDMSR that's slightly different, so we play safe by completely disassembling the instruction. */
1822 Log(("SVM: %s\n", (pVMCB->ctrl.u64ExitInfo1 == 0) ? "rdmsr" : "wrmsr"));
1823 rc = EMInterpretInstruction(pVM, CPUMCTX2CORE(pCtx), 0, &cbSize);
1824 if (rc == VINF_SUCCESS)
1825 {
1826 /* EIP has been updated already. */
1827
1828 /* Only resume if successful. */
1829 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1830 goto ResumeExecution;
1831 }
1832 AssertMsg(rc == VERR_EM_INTERPRETER, ("EMU: %s failed with %Vrc\n", (pVMCB->ctrl.u64ExitInfo1 == 0) ? "rdmsr" : "wrmsr", rc));
1833 break;
1834 }
1835
1836 case SVM_EXIT_MONITOR:
1837 case SVM_EXIT_RDPMC:
1838 case SVM_EXIT_PAUSE:
1839 case SVM_EXIT_MWAIT_UNCOND:
1840 case SVM_EXIT_MWAIT_ARMED:
1841 case SVM_EXIT_TASK_SWITCH: /* can change CR3; emulate */
1842 rc = VINF_EM_RAW_EXCEPTION_PRIVILEGED;
1843 break;
1844
1845 case SVM_EXIT_SHUTDOWN:
1846 rc = VINF_EM_RESET; /* Triple fault equals a reset. */
1847 break;
1848
1849 case SVM_EXIT_IDTR_READ:
1850 case SVM_EXIT_GDTR_READ:
1851 case SVM_EXIT_LDTR_READ:
1852 case SVM_EXIT_TR_READ:
1853 case SVM_EXIT_IDTR_WRITE:
1854 case SVM_EXIT_GDTR_WRITE:
1855 case SVM_EXIT_LDTR_WRITE:
1856 case SVM_EXIT_TR_WRITE:
1857 case SVM_EXIT_CR0_SEL_WRITE:
1858 default:
1859 /* Unexpected exit codes. */
1860 rc = VERR_EM_INTERNAL_ERROR;
1861 AssertMsgFailed(("Unexpected exit code %x\n", exitCode)); /* Can't happen. */
1862 break;
1863 }
1864
1865end:
1866 if (fGuestStateSynced)
1867 {
1868 /* Remaining guest CPU context: TR, IDTR, GDTR, LDTR. */
1869 SVM_READ_SELREG(LDTR, ldtr);
1870 SVM_READ_SELREG(TR, tr);
1871
1872 pCtx->gdtr.cbGdt = pVMCB->guest.GDTR.u32Limit;
1873 pCtx->gdtr.pGdt = pVMCB->guest.GDTR.u64Base;
1874
1875 pCtx->idtr.cbIdt = pVMCB->guest.IDTR.u32Limit;
1876 pCtx->idtr.pIdt = pVMCB->guest.IDTR.u64Base;
1877
1878 /*
1879 * System MSRs
1880 */
1881 pCtx->SysEnter.cs = pVMCB->guest.u64SysEnterCS;
1882 pCtx->SysEnter.eip = pVMCB->guest.u64SysEnterEIP;
1883 pCtx->SysEnter.esp = pVMCB->guest.u64SysEnterESP;
1884 }
1885
1886 /* Signal changes for the recompiler. */
1887 CPUMSetChangedFlags(pVM, CPUM_CHANGED_SYSENTER_MSR | CPUM_CHANGED_LDTR | CPUM_CHANGED_GDTR | CPUM_CHANGED_IDTR | CPUM_CHANGED_TR | CPUM_CHANGED_HIDDEN_SEL_REGS);
1888
1889 /* If we executed vmrun and an external irq was pending, then we don't have to do a full sync the next time. */
1890 if (exitCode == SVM_EXIT_INTR)
1891 {
1892 STAM_COUNTER_INC(&pVM->hwaccm.s.StatPendingHostIrq);
1893 /* On the next entry we'll only sync the host context. */
1894 pVM->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_HOST_CONTEXT;
1895 }
1896 else
1897 {
1898 /* On the next entry we'll sync everything. */
1899 /** @todo we can do better than this */
1900 pVM->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_ALL;
1901 }
1902
1903 /* translate into a less severe return code */
1904 if (rc == VERR_EM_INTERPRETER)
1905 rc = VINF_EM_RAW_EMULATE_INSTR;
1906
1907 STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
1908 return rc;
1909}
1910
1911/**
1912 * Enters the AMD-V session
1913 *
1914 * @returns VBox status code.
1915 * @param pVM The VM to operate on.
1916 * @param pCpu CPU info struct
1917 */
1918HWACCMR0DECL(int) SVMR0Enter(PVM pVM, PHWACCM_CPUINFO pCpu)
1919{
1920 Assert(pVM->hwaccm.s.svm.fSupported);
1921
1922 LogFlow(("SVMR0Enter cpu%d last=%d asid=%d\n", pCpu->idCpu, pVM->hwaccm.s.svm.idLastCpu, pCpu->uCurrentASID));
1923 pVM->hwaccm.s.svm.fResumeVM = false;
1924
1925 /* Force to reload LDTR, so we'll execute VMLoad to load additional guest state. */
1926 pVM->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_LDTR;
1927
1928 return VINF_SUCCESS;
1929}
1930
1931
1932/**
1933 * Leaves the AMD-V session
1934 *
1935 * @returns VBox status code.
1936 * @param pVM The VM to operate on.
1937 */
1938HWACCMR0DECL(int) SVMR0Leave(PVM pVM)
1939{
1940 Assert(pVM->hwaccm.s.svm.fSupported);
1941 return VINF_SUCCESS;
1942}
1943
1944
1945static int svmInterpretInvlPg(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, uint32_t uASID)
1946{
1947 OP_PARAMVAL param1;
1948 RTGCPTR addr;
1949
1950 int rc = DISQueryParamVal(pRegFrame, pCpu, &pCpu->param1, &param1, PARAM_SOURCE);
1951 if(VBOX_FAILURE(rc))
1952 return VERR_EM_INTERPRETER;
1953
1954 switch(param1.type)
1955 {
1956 case PARMTYPE_IMMEDIATE:
1957 case PARMTYPE_ADDRESS:
1958 if(!(param1.flags & (PARAM_VAL32|PARAM_VAL64)))
1959 return VERR_EM_INTERPRETER;
1960 addr = param1.val.val64;
1961 break;
1962
1963 default:
1964 return VERR_EM_INTERPRETER;
1965 }
1966
1967 /** @todo is addr always a flat linear address or ds based
1968 * (in absence of segment override prefixes)????
1969 */
1970 rc = PGMInvalidatePage(pVM, addr);
1971 if (VBOX_SUCCESS(rc))
1972 {
1973 /* Manually invalidate the page for the VM's TLB. */
1974 Log(("SVMInvlpgA %VGv ASID=%d\n", addr, uASID));
1975 SVMInvlpgA(addr, uASID);
1976 return VINF_SUCCESS;
1977 }
1978 Assert(rc == VERR_REM_FLUSHED_PAGES_OVERFLOW);
1979 return rc;
1980}
1981
1982/**
1983 * Interprets INVLPG
1984 *
1985 * @returns VBox status code.
1986 * @retval VINF_* Scheduling instructions.
1987 * @retval VERR_EM_INTERPRETER Something we can't cope with.
1988 * @retval VERR_* Fatal errors.
1989 *
1990 * @param pVM The VM handle.
1991 * @param pRegFrame The register frame.
1992 * @param ASID Tagged TLB id for the guest
1993 *
1994 * Updates the EIP if an instruction was executed successfully.
1995 */
1996static int SVMR0InterpretInvpg(PVM pVM, PCPUMCTXCORE pRegFrame, uint32_t uASID)
1997{
1998 /*
1999 * Only allow 32 & 64 bits code.
2000 */
2001 DISCPUMODE enmMode = SELMGetCpuModeFromSelector(pVM, pRegFrame->eflags, pRegFrame->cs, &pRegFrame->csHid);
2002 if (enmMode != CPUMODE_16BIT)
2003 {
2004 RTGCPTR pbCode;
2005 int rc = SELMValidateAndConvertCSAddr(pVM, pRegFrame->eflags, pRegFrame->ss, pRegFrame->cs, &pRegFrame->csHid, (RTGCPTR)pRegFrame->rip, &pbCode);
2006 if (VBOX_SUCCESS(rc))
2007 {
2008 uint32_t cbOp;
2009 DISCPUSTATE Cpu;
2010
2011 Cpu.mode = enmMode;
2012 rc = EMInterpretDisasOneEx(pVM, pbCode, pRegFrame, &Cpu, &cbOp);
2013 Assert(VBOX_FAILURE(rc) || Cpu.pCurInstr->opcode == OP_INVLPG);
2014 if (VBOX_SUCCESS(rc) && Cpu.pCurInstr->opcode == OP_INVLPG)
2015 {
2016 Assert(cbOp == Cpu.opsize);
2017 rc = svmInterpretInvlPg(pVM, &Cpu, pRegFrame, uASID);
2018 if (VBOX_SUCCESS(rc))
2019 {
2020 pRegFrame->rip += cbOp; /* Move on to the next instruction. */
2021 }
2022 return rc;
2023 }
2024 }
2025 }
2026 return VERR_EM_INTERPRETER;
2027}
2028
2029
2030/**
2031 * Invalidates a guest page
2032 *
2033 * @returns VBox status code.
2034 * @param pVM The VM to operate on.
2035 * @param GCVirt Page to invalidate
2036 */
2037HWACCMR0DECL(int) SVMR0InvalidatePage(PVM pVM, RTGCPTR GCVirt)
2038{
2039 bool fFlushPending = pVM->hwaccm.s.svm.fAlwaysFlushTLB | pVM->hwaccm.s.svm.fForceTLBFlush;
2040
2041 /* Skip it if a TLB flush is already pending. */
2042 if (!fFlushPending)
2043 {
2044 SVM_VMCB *pVMCB;
2045
2046 Log2(("SVMR0InvalidatePage %VGv\n", GCVirt));
2047 AssertReturn(pVM, VERR_INVALID_PARAMETER);
2048 Assert(pVM->hwaccm.s.svm.fSupported);
2049
2050 pVMCB = (SVM_VMCB *)pVM->hwaccm.s.svm.pVMCB;
2051 AssertMsgReturn(pVMCB, ("Invalid pVMCB\n"), VERR_EM_INTERNAL_ERROR);
2052
2053 STAM_COUNTER_INC(&pVM->hwaccm.s.StatFlushPageManual);
2054 SVMInvlpgA(GCVirt, pVMCB->ctrl.TLBCtrl.n.u32ASID);
2055 }
2056 return VINF_SUCCESS;
2057}
2058
2059
2060/**
2061 * Invalidates a guest page by physical address
2062 *
2063 * NOTE: Assumes the current instruction references this physical page though a virtual address!!
2064 *
2065 * @returns VBox status code.
2066 * @param pVM The VM to operate on.
2067 * @param GCPhys Page to invalidate
2068 */
2069HWACCMR0DECL(int) SVMR0InvalidatePhysPage(PVM pVM, RTGCPHYS GCPhys)
2070{
2071 bool fFlushPending = pVM->hwaccm.s.svm.fAlwaysFlushTLB | pVM->hwaccm.s.svm.fForceTLBFlush;
2072
2073 Assert(pVM->hwaccm.s.fNestedPaging);
2074
2075 /* Skip it if a TLB flush is already pending. */
2076 if (!fFlushPending)
2077 {
2078 CPUMCTX *pCtx;
2079 int rc;
2080 SVM_VMCB *pVMCB;
2081
2082 rc = CPUMQueryGuestCtxPtr(pVM, &pCtx);
2083 AssertRCReturn(rc, rc);
2084
2085 Log2(("SVMR0InvalidatePhysPage %VGp\n", GCPhys));
2086 AssertReturn(pVM, VERR_INVALID_PARAMETER);
2087 Assert(pVM->hwaccm.s.svm.fSupported);
2088
2089 pVMCB = (SVM_VMCB *)pVM->hwaccm.s.svm.pVMCB;
2090 AssertMsgReturn(pVMCB, ("Invalid pVMCB\n"), VERR_EM_INTERNAL_ERROR);
2091
2092 /*
2093 * Only allow 32 & 64 bits code.
2094 */
2095 DISCPUMODE enmMode = SELMGetCpuModeFromSelector(pVM, pCtx->eflags, pCtx->cs, &pCtx->csHid);
2096 if (enmMode != CPUMODE_16BIT)
2097 {
2098 RTGCPTR pbCode;
2099 int rc = SELMValidateAndConvertCSAddr(pVM, pCtx->eflags, pCtx->ss, pCtx->cs, &pCtx->csHid, (RTGCPTR)pCtx->rip, &pbCode);
2100 if (VBOX_SUCCESS(rc))
2101 {
2102 uint32_t cbOp;
2103 DISCPUSTATE Cpu;
2104 OP_PARAMVAL param1;
2105 RTGCPTR addr;
2106
2107 Cpu.mode = enmMode;
2108 rc = EMInterpretDisasOneEx(pVM, pbCode, CPUMCTX2CORE(pCtx), &Cpu, &cbOp);
2109 AssertRCReturn(rc, rc);
2110 Assert(cbOp == Cpu.opsize);
2111
2112 int rc = DISQueryParamVal(CPUMCTX2CORE(pCtx), &Cpu, &Cpu.param1, &param1, PARAM_SOURCE);
2113 AssertRCReturn(rc, VERR_EM_INTERPRETER);
2114
2115 switch(param1.type)
2116 {
2117 case PARMTYPE_IMMEDIATE:
2118 case PARMTYPE_ADDRESS:
2119 AssertReturn((param1.flags & (PARAM_VAL32|PARAM_VAL64)), VERR_EM_INTERPRETER);
2120
2121 addr = param1.val.val64;
2122 break;
2123
2124 default:
2125 AssertFailed();
2126 return VERR_EM_INTERPRETER;
2127 }
2128
2129 /* Manually invalidate the page for the VM's TLB. */
2130 Log(("SVMR0InvalidatePhysPage Phys=%VGp Virt=%VGv ASID=%d\n", GCPhys, addr, pVMCB->ctrl.TLBCtrl.n.u32ASID));
2131 SVMInvlpgA(addr, pVMCB->ctrl.TLBCtrl.n.u32ASID);
2132 STAM_COUNTER_INC(&pVM->hwaccm.s.StatFlushPhysPageManual);
2133
2134 return VINF_SUCCESS;
2135 }
2136 }
2137 AssertFailed();
2138 return VERR_EM_INTERPRETER;
2139 }
2140 return VINF_SUCCESS;
2141}
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