HWSVMR0.cpp revision 9112ff4daa3099506cfe23af46efaeacd4082988
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * HWACCM SVM - Host Context Ring 0.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * available from http://www.virtualbox.org. This file is free software;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * you can redistribute it and/or modify it under the terms of the GNU
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * General Public License (GPL) as published by the Free Software
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * additional information or have any questions.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/*******************************************************************************
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync* Header Files *
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync*******************************************************************************/
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/*******************************************************************************
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync* Internal Functions *
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync*******************************************************************************/
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic int svmR0InterpretInvpg(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, uint32_t uASID);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic int svmR0EmulateTprVMMCall(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic void svmR0SetMSRPermission(PVMCPU pVCpu, unsigned ulMSR, bool fRead, bool fWrite);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/*******************************************************************************
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync* Global Variables *
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync*******************************************************************************/
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/* IO operation lookup arrays. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Sets up and activates AMD-V on the current CPU
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @returns VBox status code.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pCpu CPU info struct
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pVM The VM to operate on. (can be NULL after a resume!!)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pvPageCpu Pointer to the global cpu page
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pPageCpuPhys Physical address of the global cpu page
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncVMMR0DECL(int) SVMR0EnableCpu(PHWACCM_CPUINFO pCpu, PVM pVM, void *pvPageCpu, RTHCPHYS pPageCpuPhys)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync AssertReturn(pPageCpuPhys, VERR_INVALID_PARAMETER);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* We must turn on AMD-V and setup the host state physical address, as those MSRs are per-cpu/core. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SUPR0Printf("SVMR0EnableCpu cpu %d page (%x) %x\n", pCpu->idCpu, pvPageCpu, (uint32_t)pPageCpuPhys);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Turn on AMD-V in the EFER MSR. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Write the physical page address where the CPU will store the host state while executing the VM. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Deactivates AMD-V on the current CPU
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @returns VBox status code.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pCpu CPU info struct
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pvPageCpu Pointer to the global cpu page
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pPageCpuPhys Physical address of the global cpu page
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncVMMR0DECL(int) SVMR0DisableCpu(PHWACCM_CPUINFO pCpu, void *pvPageCpu, RTHCPHYS pPageCpuPhys)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync AssertReturn(pPageCpuPhys, VERR_INVALID_PARAMETER);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SUPR0Printf("SVMR0DisableCpu cpu %d\n", pCpu->idCpu);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Turn off AMD-V in the EFER MSR. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Invalidate host state physical address. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Does Ring-0 per VM AMD-V init.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @returns VBox status code.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pVM The VM to operate on.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVM->hwaccm.s.svm.pMemObjIOBitmap = NIL_RTR0MEMOBJ;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Allocate 12 KB for the IO bitmap (doesn't seem to be a way to convince SVM not to use it) */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rc = RTR0MemObjAllocCont(&pVM->hwaccm.s.svm.pMemObjIOBitmap, 3 << PAGE_SHIFT, true /* executable R0 mapping */);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVM->hwaccm.s.svm.pIOBitmap = RTR0MemObjAddress(pVM->hwaccm.s.svm.pMemObjIOBitmap);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVM->hwaccm.s.svm.pIOBitmapPhys = RTR0MemObjGetPagePhysAddr(pVM->hwaccm.s.svm.pMemObjIOBitmap, 0);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Set all bits to intercept all IO accesses. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync ASMMemFill32(pVM->hwaccm.s.svm.pIOBitmap, PAGE_SIZE*3, 0xffffffff);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Erratum 170 which requires a forced TLB flush for each world switch:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * See http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/33610.pdf
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * All BH-G1/2 and DH-G1/2 models include a fix:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Athlon X2: 0x6b 1/2
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Athlon 64: 0x7f 1
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Sempron: 0x7f 1/2
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Turion 64: 0x68 2
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync uint32_t u32Version, u32Family, u32Model, u32Stepping, u32BaseFamily;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync ASMCpuId(1, &u32Version, &u32Dummy, &u32Dummy, &u32Dummy);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync u32Family = u32BaseFamily + (u32BaseFamily == 0xf ? ((u32Version >> 20) & 0x7f) : 0);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync u32Model = u32Model | ((u32BaseFamily == 0xf ? (u32Version >> 16) & 0x0f : 0) << 4);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync && !((u32Model == 0x68 || u32Model == 0x6b || u32Model == 0x7f) && u32Stepping >= 1)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync && !((u32Model == 0x6f || u32Model == 0x6c || u32Model == 0x7c) && u32Stepping >= 2))
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync Log(("SVMR0InitVM: AMD cpu with erratum 170 family %x model %x stepping %x\n", u32Family, u32Model, u32Stepping));
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Allocate VMCBs for all guest CPUs. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVCpu->hwaccm.s.svm.pMemObjVMCBHost = NIL_RTR0MEMOBJ;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVCpu->hwaccm.s.svm.pMemObjMSRBitmap = NIL_RTR0MEMOBJ;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Allocate one page for the host context */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rc = RTR0MemObjAllocCont(&pVCpu->hwaccm.s.svm.pMemObjVMCBHost, 1 << PAGE_SHIFT, true /* executable R0 mapping */);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVCpu->hwaccm.s.svm.pVMCBHost = RTR0MemObjAddress(pVCpu->hwaccm.s.svm.pMemObjVMCBHost);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVCpu->hwaccm.s.svm.pVMCBHostPhys = RTR0MemObjGetPagePhysAddr(pVCpu->hwaccm.s.svm.pMemObjVMCBHost, 0);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Allocate one page for the VM control block (VMCB). */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rc = RTR0MemObjAllocCont(&pVCpu->hwaccm.s.svm.pMemObjVMCB, 1 << PAGE_SHIFT, true /* executable R0 mapping */);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVCpu->hwaccm.s.svm.pVMCB = RTR0MemObjAddress(pVCpu->hwaccm.s.svm.pMemObjVMCB);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVCpu->hwaccm.s.svm.pVMCBPhys = RTR0MemObjGetPagePhysAddr(pVCpu->hwaccm.s.svm.pMemObjVMCB, 0);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Allocate 8 KB for the MSR bitmap (doesn't seem to be a way to convince SVM not to use it) */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rc = RTR0MemObjAllocCont(&pVCpu->hwaccm.s.svm.pMemObjMSRBitmap, 2 << PAGE_SHIFT, true /* executable R0 mapping */);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVCpu->hwaccm.s.svm.pMSRBitmap = RTR0MemObjAddress(pVCpu->hwaccm.s.svm.pMemObjMSRBitmap);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVCpu->hwaccm.s.svm.pMSRBitmapPhys = RTR0MemObjGetPagePhysAddr(pVCpu->hwaccm.s.svm.pMemObjMSRBitmap, 0);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Set all bits to intercept all MSR accesses. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync ASMMemFill32(pVCpu->hwaccm.s.svm.pMSRBitmap, PAGE_SIZE*2, 0xffffffff);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Does Ring-0 per VM AMD-V termination.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @returns VBox status code.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pVM The VM to operate on.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (pVCpu->hwaccm.s.svm.pMemObjVMCBHost != NIL_RTR0MEMOBJ)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync RTR0MemObjFree(pVCpu->hwaccm.s.svm.pMemObjVMCBHost, false);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVCpu->hwaccm.s.svm.pMemObjVMCBHost = NIL_RTR0MEMOBJ;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (pVCpu->hwaccm.s.svm.pMemObjVMCB != NIL_RTR0MEMOBJ)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync RTR0MemObjFree(pVCpu->hwaccm.s.svm.pMemObjVMCB, false);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (pVCpu->hwaccm.s.svm.pMemObjMSRBitmap != NIL_RTR0MEMOBJ)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync RTR0MemObjFree(pVCpu->hwaccm.s.svm.pMemObjMSRBitmap, false);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVCpu->hwaccm.s.svm.pMemObjMSRBitmap = NIL_RTR0MEMOBJ;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (pVM->hwaccm.s.svm.pMemObjIOBitmap != NIL_RTR0MEMOBJ)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync RTR0MemObjFree(pVM->hwaccm.s.svm.pMemObjIOBitmap, false);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVM->hwaccm.s.svm.pMemObjIOBitmap = NIL_RTR0MEMOBJ;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Sets up AMD-V for the specified VM
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @returns VBox status code.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pVM The VM to operate on.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SVM_VMCB *pVMCB = (SVM_VMCB *)pVM->aCpus[i].hwaccm.s.svm.pVMCB;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync AssertMsgReturn(pVMCB, ("Invalid pVMCB\n"), VERR_EM_INTERNAL_ERROR);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Program the control fields. Most of them never have to be changed again. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* CR0/3/4 reads must be intercepted, our shadow values are not necessarily the same as the guest's. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Note: CR0 & CR4 can be safely read when guest and shadow copies are identical. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVMCB->ctrl.u16InterceptRdCRx = RT_BIT(0) | RT_BIT(3) | RT_BIT(4);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVMCB->ctrl.u16InterceptRdCRx = RT_BIT(0) | RT_BIT(4);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * CR0/3/4 writes must be intercepted for obvious reasons.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVMCB->ctrl.u16InterceptWrCRx = RT_BIT(0) | RT_BIT(3) | RT_BIT(4);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVMCB->ctrl.u16InterceptWrCRx = RT_BIT(0) | RT_BIT(4) | RT_BIT(8);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Intercept all DRx reads and writes by default. Changed later on. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Currently we don't care about DRx reads or writes. DRx registers are trashed.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * All breakpoints are automatically cleared when the VM exits.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVMCB->ctrl.u32InterceptException = HWACCM_SVM_TRAP_MASK;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVMCB->ctrl.u32InterceptException &= ~RT_BIT(X86_XCPT_PF); /* no longer need to intercept #PF. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVMCB->ctrl.u32InterceptCtrl1 = SVM_CTRL1_INTERCEPT_INTR
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync | SVM_CTRL1_INTERCEPT_FERR_FREEZE; /* Legacy FPU FERR handling. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* With nested paging we don't care about invlpg anymore. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVMCB->ctrl.u32InterceptCtrl1 &= ~SVM_CTRL1_INTERCEPT_INVLPG;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVMCB->ctrl.u32InterceptCtrl2 = SVM_CTRL2_INTERCEPT_VMRUN /* required */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync | SVM_CTRL2_INTERCEPT_MWAIT_UNCOND; /* don't execute mwait or else we'll idle inside the guest (host thinks the cpu load is high) */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync Log(("pVMCB->ctrl.u32InterceptException = %x\n", pVMCB->ctrl.u32InterceptException));
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync Log(("pVMCB->ctrl.u32InterceptCtrl1 = %x\n", pVMCB->ctrl.u32InterceptCtrl1));
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync Log(("pVMCB->ctrl.u32InterceptCtrl2 = %x\n", pVMCB->ctrl.u32InterceptCtrl2));
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Virtualize masking of INTR interrupts. (reads/writes from/to CR8 go to the V_TPR register) */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Ignore the priority in the TPR; just deliver it when we tell it to. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Set IO and MSR bitmap addresses. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVMCB->ctrl.u64IOPMPhysAddr = pVM->hwaccm.s.svm.pIOBitmapPhys;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVMCB->ctrl.u64MSRPMPhysAddr = pVCpu->hwaccm.s.svm.pMSRBitmapPhys;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* No LBR virtualization. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /** The ASID must start at 1; the host uses 0. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /** Setup the PAT msr (nested paging only) */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* The following MSRs are saved automatically by vmload/vmsave, so we allow the guest
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * to modify them directly.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync svmR0SetMSRPermission(pVCpu, MSR_K8_LSTAR, true, true);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync svmR0SetMSRPermission(pVCpu, MSR_K8_CSTAR, true, true);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync svmR0SetMSRPermission(pVCpu, MSR_K6_STAR, true, true);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync svmR0SetMSRPermission(pVCpu, MSR_K8_SF_MASK, true, true);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync svmR0SetMSRPermission(pVCpu, MSR_K8_FS_BASE, true, true);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync svmR0SetMSRPermission(pVCpu, MSR_K8_GS_BASE, true, true);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync svmR0SetMSRPermission(pVCpu, MSR_K8_KERNEL_GS_BASE, true, true);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync svmR0SetMSRPermission(pVCpu, MSR_IA32_SYSENTER_CS, true, true);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync svmR0SetMSRPermission(pVCpu, MSR_IA32_SYSENTER_ESP, true, true);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync svmR0SetMSRPermission(pVCpu, MSR_IA32_SYSENTER_EIP, true, true);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Sets the permission bits for the specified MSR
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pVCpu The VMCPU to operate on.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param ulMSR MSR value
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param fRead Reading allowed/disallowed
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param fWrite Writing allowed/disallowed
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic void svmR0SetMSRPermission(PVMCPU pVCpu, unsigned ulMSR, bool fRead, bool fWrite)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync uint8_t *pMSRBitmap = (uint8_t *)pVCpu->hwaccm.s.svm.pMSRBitmap;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Pentium-compatible MSRs */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* AMD Sixth Generation x86 Processor MSRs and SYSCALL */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* AMD Seventh and Eighth Generation Processor MSRs */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Injects an event (trap or external interrupt)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pVCpu The VMCPU to operate on.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pVMCB SVM control block
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pCtx CPU Context
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pIntInfo SVM interrupt info
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncinline void SVMR0InjectEvent(PVMCPU pVCpu, SVM_VMCB *pVMCB, CPUMCTX *pCtx, SVM_EVENT* pEvent)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync STAM_COUNTER_INC(&pVCpu->hwaccm.s.paStatInjectedIrqsR0[pEvent->n.u8Vector & MASK_INJECT_IRQ_STAT]);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync Log(("SVM: Inject int %d at %RGv error code=%02x CR2=%RGv intInfo=%08x\n", pEvent->n.u8Vector, (RTGCPTR)pCtx->rip, pEvent->n.u32ErrorCode, (RTGCPTR)pCtx->cr2, pEvent->au64[0]));
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync Log(("SVM: Inject int %d at %RGv error code=%08x\n", pEvent->n.u8Vector, (RTGCPTR)pCtx->rip, pEvent->n.u32ErrorCode));
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync Log(("INJ-EI: %x at %RGv\n", pEvent->n.u8Vector, (RTGCPTR)pCtx->rip));
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync Assert(!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS));
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Set event injection state. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Checks for pending guest interrupts and injects them
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @returns VBox status code.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pVM The VM to operate on.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pVCpu The VM CPU to operate on.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pVMCB SVM control block
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pCtx CPU Context
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic int SVMR0CheckPendingInterrupt(PVM pVM, PVMCPU pVCpu, SVM_VMCB *pVMCB, CPUMCTX *pCtx)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Dispatch any pending interrupts. (injected before, but a VM exit occurred prematurely) */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync Log(("Reinjecting event %08x %08x at %RGv\n", pVCpu->hwaccm.s.Event.intInfo, pVCpu->hwaccm.s.Event.errCode, (RTGCPTR)pCtx->rip));
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatIntReinject);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* If an active trap is already pending, then we must forward it first! */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (VMCPU_FF_TESTANDCLEAR(pVCpu, VMCPU_FF_INTERRUPT_NMI))
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* @todo SMI interrupts. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* When external interrupts are pending, we should exit the VM when IF is set. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (VMCPU_FF_ISPENDING(pVCpu, (VMCPU_FF_INTERRUPT_APIC|VMCPU_FF_INTERRUPT_PIC)))
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync || VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS))
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS))
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync Log(("Pending interrupt blocked at %RGv by VM_FF_INHIBIT_INTERRUPTS -> irq window exit\n", (RTGCPTR)pCtx->rip));
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /** @todo use virtual interrupt method to inject a pending irq; dispatched as soon as guest.IF is set. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVMCB->ctrl.u32InterceptCtrl1 |= SVM_CTRL1_INTERCEPT_VINTR;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVMCB->ctrl.IntCtrl.n.u8VIrqVector = 0; /* don't care */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync Log(("Dispatch interrupt: u8Interrupt=%x (%d) rc=%Rrc\n", u8Interrupt, u8Interrupt, rc));
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rc = TRPMAssertTrap(pVCpu, u8Interrupt, TRPM_HARDWARE_INT);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Can only happen in rare cases where a pending interrupt is cleared behind our back */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync Assert(!VMCPU_FF_ISPENDING(pVCpu, (VMCPU_FF_INTERRUPT_APIC|VMCPU_FF_INTERRUPT_PIC)));
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatSwitchGuestIrq);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Just continue */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync && (!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS))
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* If a new event is pending, then dispatch it now. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rc = TRPMQueryTrapAll(pVCpu, &u8Vector, &enmType, &u32ErrorCode, 0);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync Assert(pCtx->eflags.Bits.u1IF == 1 || enmType == TRPM_TRAP);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Clear the pending trap. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Valid error codes. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync } /* if (interrupts can be dispatched) */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Save the host state
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @returns VBox status code.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pVM The VM to operate on.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pVCpu The VM CPU to operate on.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncVMMR0DECL(int) SVMR0SaveHostState(PVM pVM, PVMCPU pVCpu)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Nothing to do here. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Loads the guest state
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * NOTE: Don't do anything here that can cause a jump back to ring 3!!!!!
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @returns VBox status code.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pVM The VM to operate on.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pVCpu The VM CPU to operate on.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pCtx Guest context
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncVMMR0DECL(int) SVMR0LoadGuestState(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Setup AMD SVM. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync AssertMsgReturn(pVMCB, ("Invalid pVMCB\n"), VERR_EM_INTERNAL_ERROR);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Guest CPU context: ES, CS, SS, DS, FS, GS. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (pVCpu->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_SEGMENT_REGS)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Guest CPU context: LDTR. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (pVCpu->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_LDTR)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Guest CPU context: TR. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (pVCpu->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_TR)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Guest CPU context: GDTR. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (pVCpu->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_GDTR)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Guest CPU context: IDTR. */
67c73271505adae0686ddbc74eaeef778ef16792vboxsync if (pVCpu->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_IDTR)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Sysenter MSRs (unconditional)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Control registers */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (pVCpu->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_CR0)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Always use #NM exceptions to load the FPU/XMM state on demand. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync val |= X86_CR0_TS | X86_CR0_ET | X86_CR0_NE | X86_CR0_MP;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /** @todo check if we support the old style mess correctly. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Also catch floating point exceptions as we need to report them to the guest in a different way. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVMCB->ctrl.u32InterceptException |= RT_BIT(X86_XCPT_MF);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync val |= X86_CR0_NE; /* always turn on the native mechanism to report FPU errors (old style uses interrupts) */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Always enable caching. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Note: WP is not relevant in nested paging mode as we catch accesses on the (guest) physical level. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Note: In nested paging mode the guest is allowed to run with paging disabled; the guest physical to host physical translation will remain active. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync val |= X86_CR0_PG; /* Paging is always enabled; even when the guest is running in real mode or PE without paging. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync val |= X86_CR0_WP; /* Must set this as we rely on protect various pages and supervisor writes must be caught. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* CR2 as well */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (pVCpu->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_CR3)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Save our shadow CR3 register. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVMCB->ctrl.u64NestedPagingCR3 = PGMGetNestedCR3(pVCpu, enmShwPagingMode);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync Assert(pVMCB->guest.u64CR3 || VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL));
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (pVCpu->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_CR4)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case PGMMODE_PROTECTED: /* Protected mode, no paging. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case PGMMODE_PAE_NX: /* PAE paging with NX enabled. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /** @todo use normal 32 bits paging */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case PGMMODE_AMD64: /* 64-bit AMD paging (long mode). */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case PGMMODE_AMD64_NX: /* 64-bit AMD paging (long mode) with NX enabled. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync default: /* shut up gcc */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Debug registers. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (pVCpu->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_DEBUG)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pCtx->dr[6] |= X86_DR6_INIT_VAL; /* set all reserved bits to 1. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pCtx->dr[7] &= 0xffffffff; /* upper 32 bits reserved */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pCtx->dr[7] &= ~(RT_BIT(11) | RT_BIT(12) | RT_BIT(14) | RT_BIT(15)); /* must be zero */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Sync the hypervisor debug state now if any breakpoint is armed. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if ( CPUMGetHyperDR7(pVCpu) & (X86_DR7_ENABLED_MASK|X86_DR7_GD)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Save the host and load the hypervisor debug state. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync int rc = CPUMR0LoadHyperDebugState(pVM, pVCpu, pCtx, false /* exclude DR6 */);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* DRx intercepts remain enabled. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Override dr6 & dr7 with the hypervisor values. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Sync the debug state now if any breakpoint is armed. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if ( (pCtx->dr[7] & (X86_DR7_ENABLED_MASK|X86_DR7_GD))
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Disable drx move intercepts. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Save the host and load the guest debug state. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync int rc = CPUMR0LoadGuestDebugState(pVM, pVCpu, pCtx, false /* exclude DR6 */);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* EIP, ESP and EFLAGS */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Set CPL */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* RAX/EAX too, as VMRUN uses RAX as an implicit parameter. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* vmrun will fail without MSR_K6_EFER_SVME. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVMCB->guest.u64EFER = pCtx->msrEFER | MSR_K6_EFER_SVME;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* 64 bits guest mode? */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync#elif HC_ARCH_BITS == 32 && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pVCpu->hwaccm.s.svm.pfnVMRun = SVMR0VMSwitcherRun64;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Unconditionally update these as wrmsr might have changed them. (HWACCM_CHANGED_GUEST_SEGMENT_REGS will not be set) */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* Filter out the MSR_K6_LME bit or else AMD-V expects amd64 shadow paging. */
LogFlow(("TSC %RX64 offset %RX64 time=%RX64 last=%RX64 (diff=%RX64, virt_tsc=%RX64)\n", u64CurTSC, pVMCB->ctrl.u64TSCOffset, u64CurTSC + pVMCB->ctrl.u64TSCOffset, TMCpuTickGetLastSeen(pVCpu), TMCpuTickGetLastSeen(pVCpu) - u64CurTSC - pVMCB->ctrl.u64TSCOffset, TMCpuTickGet(pVCpu)));
#ifdef DEBUG
return VINF_SUCCESS;
bool fSyncTPR = false;
unsigned cResume = 0;
#ifdef VBOX_STRICT
goto end;
Log(("VM_FF_INHIBIT_INTERRUPTS at %RGv successor %RGv\n", (RTGCPTR)pCtx->rip, EMGetInhibitInterruptsPC(pVCpu)));
* Before we are able to execute this instruction in raw mode (iret to guest code) an external interrupt might
* force a world switch again. Possibly allowing a guest interrupt to be dispatched in the process. This could
* break the guest. Sounds very unlikely, but such timing sensitive problems are not as rare as you might think.
#ifdef DEBUG
rc = RT_UNLIKELY(VM_FF_ISPENDING(pVM, VM_FF_PGM_NO_MEMORY)) ? VINF_EM_NO_MEMORY : VINF_EM_RAW_TO_R3;
goto end;
/* Pending request packets might contain actions that need immediate attention, such as pending hardware interrupts. */
goto end;
* Exit to ring-3 preemption/work is pending.
goto end;
goto end;
/* TPR caching using CR8 is only available in 64 bits mode or with 32 bits guests when X86_CPUID_AMD_FEATURE_ECX_CR8L is supported. */
/* Note: we can't do this in LoadGuestState as PDMApicGetTPR can jump back to ring 3 (lock)!!!!!!!! (no longer true)
bool fPending;
if (fPending)
pVMCB->ctrl.IntCtrl.n.u8VTPR = (u8LastTPR >> 4); /* cr8 bits 3-0 correspond to bits 7-4 of the task priority mmio register. */
if (fPending)
#ifdef LOG_ENABLED
LogFlow(("Force TLB flush due to rescheduling to a different cpu (%d vs %d)\n", pVCpu->hwaccm.s.idLastCpu, pCpu->idCpu));
LogFlow(("Force TLB flush due to changed TLB flush count (%x vs %x)\n", pVCpu->hwaccm.s.cTLBFlushes, pCpu->cTLBFlushes));
#ifdef VBOX_STRICT
goto end;
/* Force a TLB flush for the first world switch if the current cpu differs from the one we ran on last. */
/* 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. */
/* 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). */
AssertMsg(pVCpu->hwaccm.s.cTLBFlushes == pCpu->cTLBFlushes, ("Flush count mismatch for cpu %d (%x vs %x)\n", pCpu->idCpu, pVCpu->hwaccm.s.cTLBFlushes, pCpu->cTLBFlushes));
AssertMsg(pCpu->uCurrentASID >= 1 && pCpu->uCurrentASID < pVM->hwaccm.s.uMaxASID, ("cpu%d uCurrentASID = %x\n", pCpu->idCpu, pCpu->uCurrentASID));
AssertMsg(pVCpu->hwaccm.s.uCurrentASID >= 1 && pVCpu->hwaccm.s.uCurrentASID < pVM->hwaccm.s.uMaxASID, ("cpu%d VM uCurrentASID = %x\n", pCpu->idCpu, pVCpu->hwaccm.s.uCurrentASID));
#ifdef VBOX_WITH_STATISTICS
#ifdef VBOX_STRICT
#ifdef VBOX_WITH_KERNEL_USING_XMM
hwaccmR0SVMRunWrapXMM(pVCpu->hwaccm.s.svm.pVMCBHostPhys, pVCpu->hwaccm.s.svm.pVMCBPhys, pCtx, pVM, pVCpu, pVCpu->hwaccm.s.svm.pfnVMRun);
pVCpu->hwaccm.s.svm.pfnVMRun(pVCpu->hwaccm.s.svm.pVMCBHostPhys, pVCpu->hwaccm.s.svm.pVMCBPhys, pCtx, pVM, pVCpu);
/* Possibly the last TSC value seen by the guest (too high) (only when we're in tsc offset mode). */
TMCpuTickSetLastSeen(pVCpu, ASMReadTSC() + pVMCB->ctrl.u64TSCOffset - 0x400 /* guestimate of world switch overhead in clock ticks */);
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* 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
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#ifdef DEBUG
goto end;
/* Save all the MSRs that can be changed by the guest without causing a world switch. (fs & gs base are saved with SVM_READ_SELREG) */
/* Remaining guest CPU context: TR, IDTR, GDTR, LDTR; must sync everything otherwise we can get out of sync when jumping to ring 3. */
&& pVMCB->ctrl.ExitIntInfo.n.u3Type != SVM_EVENT_SOFTWARE_INT /* we don't care about 'int xx' as the instruction will be restarted. */)
Log(("Pending inject %RX64 at %RGv exit=%08x\n", pVCpu->hwaccm.s.Event.intInfo, (RTGCPTR)pCtx->rip, exitCode));
#ifdef LOG_ENABLED
#ifdef VBOX_WITH_STATISTICS
if (fSyncTPR)
rc = PDMApicSetTPR(pVCpu, pVMCB->ctrl.IntCtrl.n.u8VTPR << 4); /* cr8 bits 3-0 correspond to bits 7-4 of the task priority mmio register. */
switch (exitCode)
case SVM_EXIT_EXCEPTION_0: case SVM_EXIT_EXCEPTION_1: case SVM_EXIT_EXCEPTION_2: case SVM_EXIT_EXCEPTION_3:
case SVM_EXIT_EXCEPTION_4: case SVM_EXIT_EXCEPTION_5: case SVM_EXIT_EXCEPTION_6: case SVM_EXIT_EXCEPTION_7:
case SVM_EXIT_EXCEPTION_8: case SVM_EXIT_EXCEPTION_9: case SVM_EXIT_EXCEPTION_A: case SVM_EXIT_EXCEPTION_B:
case SVM_EXIT_EXCEPTION_C: case SVM_EXIT_EXCEPTION_D: case SVM_EXIT_EXCEPTION_E: case SVM_EXIT_EXCEPTION_F:
case SVM_EXIT_EXCEPTION_10: case SVM_EXIT_EXCEPTION_11: case SVM_EXIT_EXCEPTION_12: case SVM_EXIT_EXCEPTION_13:
case SVM_EXIT_EXCEPTION_14: case SVM_EXIT_EXCEPTION_15: case SVM_EXIT_EXCEPTION_16: case SVM_EXIT_EXCEPTION_17:
case SVM_EXIT_EXCEPTION_18: case SVM_EXIT_EXCEPTION_19: case SVM_EXIT_EXCEPTION_1A: case SVM_EXIT_EXCEPTION_1B:
case SVM_EXIT_EXCEPTION_1C: case SVM_EXIT_EXCEPTION_1D: case SVM_EXIT_EXCEPTION_1E: case SVM_EXIT_EXCEPTION_1F:
switch (vector)
case X86_XCPT_DB:
goto ResumeExecution;
case X86_XCPT_NM:
/* If we sync the FPU/XMM state on-demand, then we can continue execution as if nothing has happened. */
goto ResumeExecution;
goto ResumeExecution;
#ifdef DEBUG
Log(("Guest page fault at %04X:%RGv cr2=%RGv error code %x rsp=%RGv\n", pCtx->cs, (RTGCPTR)pCtx->rip, uFaultAddress, errCode, (RTGCPTR)pCtx->rsp));
goto ResumeExecution;
PHWACCMTPRPATCH pPatch = (PHWACCMTPRPATCH)RTAvloU32Get(&pVM->hwaccm.s.svm.PatchTree, (AVLOU32KEY)pCtx->eip);
if (!pPatch)
Log2(("Shadow page fault at %RGv cr2=%RGv error code %x\n", (RTGCPTR)pCtx->rip, uFaultAddress, errCode));
goto ResumeExecution;
goto ResumeExecution;
#ifdef VBOX_STRICT
goto ResumeExecution;
#ifdef VBOX_STRICT
switch(vector)
case X86_XCPT_GP:
case X86_XCPT_DE:
case X86_XCPT_UD:
case X86_XCPT_SS:
case X86_XCPT_NP:
goto ResumeExecution;
case SVM_EXIT_NPF:
/* EXITINFO1 contains fault errorcode; EXITINFO2 contains the guest physical address causing the fault. */
LogFlow(("Nested page fault at %RGv cr2=%RGp error code %x\n", (RTGCPTR)pCtx->rip, uFaultAddress, errCode));
PHWACCMTPRPATCH pPatch = (PHWACCMTPRPATCH)RTAvloU32Get(&pVM->hwaccm.s.svm.PatchTree, (AVLOU32KEY)pCtx->eip);
if (!pPatch)
rc = PGMR0Trap0eHandlerNestedPaging(pVM, pVCpu, enmShwPagingMode, errCode, CPUMCTX2CORE(pCtx), uFaultAddress);
Log2(("Shadow page fault at %RGv cr2=%RGp error code %x\n", (RTGCPTR)pCtx->rip, uFaultAddress, errCode));
goto ResumeExecution;
#ifdef VBOX_STRICT
case SVM_EXIT_VINTR:
goto ResumeExecution;
case SVM_EXIT_FERR_FREEZE:
case SVM_EXIT_INTR:
case SVM_EXIT_NMI:
case SVM_EXIT_SMI:
case SVM_EXIT_INIT:
case SVM_EXIT_WBINVD:
goto ResumeExecution;
goto ResumeExecution;
goto ResumeExecution;
goto ResumeExecution;
goto ResumeExecution;
case SVM_EXIT_WRITE_CR8: case SVM_EXIT_WRITE_CR9: case SVM_EXIT_WRITE_CR10: case SVM_EXIT_WRITE_CR11:
case SVM_EXIT_WRITE_CR12: case SVM_EXIT_WRITE_CR13: case SVM_EXIT_WRITE_CR14: case SVM_EXIT_WRITE_CR15:
AssertFailed();
rc = PGMSyncCR3(pVCpu, pCtx->cr0, pCtx->cr3, pCtx->cr4, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3));
AssertMsg(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3 || PGMGetGuestMode(pVCpu) <= PGMMODE_PROTECTED || pVCpu->hwaccm.s.fForceTLBFlush,
("rc=%Rrc mode=%d fForceTLBFlush=%RTbool\n", rc, PGMGetGuestMode(pVCpu), pVCpu->hwaccm.s.fForceTLBFlush));
goto ResumeExecution;
goto ResumeExecution;
case SVM_EXIT_WRITE_DR8: case SVM_EXIT_WRITE_DR9: case SVM_EXIT_WRITE_DR10: case SVM_EXIT_WRITE_DR11:
case SVM_EXIT_WRITE_DR12: case SVM_EXIT_WRITE_DR13: case SVM_EXIT_WRITE_DR14: case SVM_EXIT_WRITE_DR15:
goto ResumeExecution;
goto ResumeExecution;
goto ResumeExecution;
goto ResumeExecution;
/* Note: We'll get a #GP if the IO instruction isn't allowed (IOPL or TSS bitmap); no need to double check. */
rc = VBOXSTRICTRC_TODO(IOMInterpretOUTSEx(pVM, CPUMCTX2CORE(pCtx), IoExitInfo.n.u16Port, pDis->prefix, uIOSize));
rc = VBOXSTRICTRC_TODO(IOMInterpretINSEx(pVM, CPUMCTX2CORE(pCtx), IoExitInfo.n.u16Port, pDis->prefix, uIOSize));
Log2(("IOMIOPortWrite %RGv %x %x size=%d\n", (RTGCPTR)pCtx->rip, IoExitInfo.n.u16Port, pCtx->eax & uAndVal, uIOSize));
HWACCMR0SavePendingIOPortWrite(pVCpu, pCtx->rip, pVMCB->ctrl.u64ExitInfo2, IoExitInfo.n.u16Port, uAndVal, uIOSize);
Log2(("IOMIOPortRead %RGv %x %x size=%d\n", (RTGCPTR)pCtx->rip, IoExitInfo.n.u16Port, u32Val & uAndVal, uIOSize));
HWACCMR0SavePendingIOPortRead(pVCpu, pCtx->rip, pVMCB->ctrl.u64ExitInfo2, IoExitInfo.n.u16Port, uAndVal, uIOSize);
* Bits 15:13 of the DR6 register is never cleared by the processor and must be cleared by software after
goto ResumeExecution;
goto ResumeExecution;
Log2(("EM status from IO at %RGv %x size %d: %Rrc\n", (RTGCPTR)pCtx->rip, IoExitInfo.n.u16Port, uIOSize, rc));
#ifdef VBOX_STRICT
AssertMsg(RT_FAILURE(rc) || rc == VINF_EM_RAW_EMULATE_INSTR || rc == VINF_EM_RAW_GUEST_TRAP || rc == VINF_TRPM_XCPT_DISPATCHED, ("%Rrc\n", rc));
case SVM_EXIT_HLT:
goto ResumeExecution;
case SVM_EXIT_MWAIT_UNCOND:
goto ResumeExecution;
case SVM_EXIT_VMMCALL:
case SVM_EXIT_RSM:
case SVM_EXIT_INVLPGA:
case SVM_EXIT_VMRUN:
case SVM_EXIT_VMLOAD:
case SVM_EXIT_VMSAVE:
case SVM_EXIT_STGI:
case SVM_EXIT_CLGI:
case SVM_EXIT_SKINIT:
goto ResumeExecution;
case SVM_EXIT_MSR:
goto ResumeExecution;
/* 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. */
STAM_COUNTER_INC((pVMCB->ctrl.u64ExitInfo1 == 0) ? &pVCpu->hwaccm.s.StatExitRdmsr : &pVCpu->hwaccm.s.StatExitWrmsr);
goto ResumeExecution;
AssertMsg(rc == VERR_EM_INTERPRETER, ("EMU: %s failed with %Rrc\n", (pVMCB->ctrl.u64ExitInfo1 == 0) ? "rdmsr" : "wrmsr", rc));
case SVM_EVENT_EXTERNAL_IRQ:
case SVM_EVENT_NMI:
case SVM_EXIT_MONITOR:
case SVM_EXIT_PAUSE:
case SVM_EXIT_MWAIT_ARMED:
case SVM_EXIT_SHUTDOWN:
case SVM_EXIT_IDTR_READ:
case SVM_EXIT_GDTR_READ:
case SVM_EXIT_LDTR_READ:
case SVM_EXIT_TR_READ:
case SVM_EXIT_IDTR_WRITE:
case SVM_EXIT_GDTR_WRITE:
case SVM_EXIT_LDTR_WRITE:
case SVM_EXIT_TR_WRITE:
case SVM_EXIT_CR0_SEL_WRITE:
end:
CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_SYSENTER_MSR | CPUM_CHANGED_LDTR | CPUM_CHANGED_GDTR | CPUM_CHANGED_IDTR | CPUM_CHANGED_TR | CPUM_CHANGED_HIDDEN_SEL_REGS);
/* If we executed vmrun and an external irq was pending, then we don't have to do a full sync the next time. */
return rc;
int rc;
bool fPending;
PHWACCMTPRPATCH pPatch = (PHWACCMTPRPATCH)RTAvloU32Get(&pVM->hwaccm.s.svm.PatchTree, (AVLOU32KEY)pCtx->eip);
if (!pPatch)
case HWACCMTPRINSTR_READ:
case HWACCMTPRINSTR_WRITE_REG:
case HWACCMTPRINSTR_WRITE_IMM:
return VINF_SUCCESS;
LogFlow(("SVMR0Enter cpu%d last=%d asid=%d\n", pCpu->idCpu, pVCpu->hwaccm.s.idLastCpu, pVCpu->hwaccm.s.uCurrentASID));
return VINF_SUCCESS;
#ifdef DEBUG
return VINF_SUCCESS;
static int svmR0InterpretInvlPg(PVMCPU pVCpu, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, uint32_t uASID)
return VERR_EM_INTERPRETER;
case PARMTYPE_IMMEDIATE:
case PARMTYPE_ADDRESS:
return VERR_EM_INTERPRETER;
return VERR_EM_INTERPRETER;
return VINF_SUCCESS;
return rc;
DISCPUMODE enmMode = SELMGetCpuModeFromSelector(pVM, pRegFrame->eflags, pRegFrame->cs, &pRegFrame->csHid);
int rc = SELMValidateAndConvertCSAddr(pVM, pRegFrame->eflags, pRegFrame->ss, pRegFrame->cs, &pRegFrame->csHid, (RTGCPTR)pRegFrame->rip, &pbCode);
return rc;
return VERR_EM_INTERPRETER;
if (!fFlushPending)
/* If we get a flush in 64 bits guest mode, then force a full TLB flush. Invlpga takes only 32 bits addresses. */
return VINF_SUCCESS;
/* invlpga only invalidates TLB entries for guest virtual addresses; we have no choice but to force a TLB flush here. */
return VINF_SUCCESS;
#if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
DECLASM(int) SVMR0VMSwitcherRun64(RTHCPHYS pVMCBHostPhys, RTHCPHYS pVMCBPhys, PCPUMCTX pCtx, PVM pVM, PVMCPU pVCpu)
VMMR0DECL(int) SVMR0Execute64BitsHandler(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, RTRCPTR pfnHandler, uint32_t cbParam, uint32_t *paParam)
int rc;
return rc;