VirtualBox

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

Last change on this file since 12077 was 12077, checked in by vboxsync, 16 years ago

Adjusted assertions.

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