VirtualBox

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

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

Fixed wrong call to TRPMResetTrap

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette