HMSVMR0.cpp revision fde7c950706837227b4e26388edfc154de1b5530
/* $Id$ */
/** @file
* HM SVM (AMD-V) - Host Context Ring-0.
*/
/*
* Copyright (C) 2013 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** Ring-0 memory object for the IO bitmap. */
RTR0MEMOBJ g_hMemObjIOBitmap = NIL_RTR0MEMOBJ;
/** Physical address of the IO bitmap. */
RTHCPHYS g_HCPhysIOBitmap = 0;
/** Virtual address of the IO bitmap. */
R0PTRTYPE(void *) g_pvIOBitmap = NULL;
/**
* Sets up and activates AMD-V on the current CPU.
*
* @returns VBox status code.
* @param pCpu Pointer to the CPU info struct.
* @param pVM Pointer to the VM (can be NULL after a resume!).
* @param pvCpuPage Pointer to the global CPU page.
* @param HCPhysCpuPage Physical address of the global CPU page.
*/
VMMR0DECL(int) SVMR0EnableCpu(PHMGLOBLCPUINFO pCpu, PVM pVM, void *pvCpuPage, RTHCPHYS HCPhysCpuPage, bool fEnabledByHost)
{
AssertReturn(!fEnabledByHost, VERR_INVALID_PARAMETER);
AssertReturn( HCPhysCpuPage
&& HCPhysCpuPage != NIL_RTHCPHYS, VERR_INVALID_PARAMETER);
AssertReturn(pvCpuPage, VERR_INVALID_PARAMETER);
/*
* We must turn on AMD-V and setup the host state physical address, as those MSRs are per CPU.
*/
uint64_t u64HostEfer = ASMRdMsr(MSR_K6_EFER);
if (u64HostEfer & MSR_K6_EFER_SVME)
{
/* If the VBOX_HWVIRTEX_IGNORE_SVM_IN_USE is active, then we blindly use AMD-V. */
if ( pVM
&& pVM->hm.s.svm.fIgnoreInUseError)
{
pCpu->fIgnoreAMDVInUseError = true;
}
if (!pCpu->fIgnoreAMDVInUseError)
return VERR_SVM_IN_USE;
}
/* Turn on AMD-V in the EFER MSR. */
ASMWrMsr(MSR_K6_EFER, u64HostEfer | MSR_K6_EFER_SVME);
/* Write the physical page address where the CPU will store the host state while executing the VM. */
ASMWrMsr(MSR_K8_VM_HSAVE_PA, HCPhysCpuPage);
/*
* Theoretically, other hypervisors may have used ASIDs, ideally we should flush all non-zero ASIDs
* when enabling SVM. AMD doesn't have an SVM instruction to flush all ASIDs (flushing is done
* upon VMRUN). Therefore, just set the fFlushAsidBeforeUse flag which instructs hmR0SvmSetupTLB()
* to flush the TLB with before using a new ASID.
*/
pCpu->fFlushAsidBeforeUse = true;
/*
* Ensure each VCPU scheduled on this CPU gets a new VPID on resume. See @bugref{6255}.
*/
++pCpu->cTlbFlushes;
return VINF_SUCCESS;
}
/**
* Deactivates AMD-V on the current CPU.
*
* @returns VBox status code.
* @param pCpu Pointer to the CPU info struct.
* @param pvCpuPage Pointer to the global CPU page.
* @param HCPhysCpuPage Physical address of the global CPU page.
*/
VMMR0DECL(int) SVMR0DisableCpu(PHMGLOBLCPUINFO pCpu, void *pvCpuPage, RTHCPHYS HCPhysCpuPage)
{
AssertReturn( HCPhysCpuPage
&& HCPhysCpuPage != NIL_RTHCPHYS, VERR_INVALID_PARAMETER);
AssertReturn(pvCpuPage, VERR_INVALID_PARAMETER);
NOREF(pCpu);
/* Turn off AMD-V in the EFER MSR if AMD-V is active. */
uint64_t u64HostEfer = ASMRdMsr(MSR_K6_EFER);
if (u64HostEfer & MSR_K6_EFER_SVME)
{
ASMWrMsr(MSR_K6_EFER, u64HostEfer & ~MSR_K6_EFER_SVME);
/* Invalidate host state physical address. */
ASMWrMsr(MSR_K8_VM_HSAVE_PA, 0);
}
return VINF_SUCCESS;
}
/**
* Does global AMD-V initialization (called during module initialization).
*
* @returns VBox status code.
*/
VMMR0DECL(int) SVMR0GlobalInit(void)
{
/*
* Allocate 12 KB for the IO bitmap. Since this is non-optional and we always intercept all IO accesses, it's done
* once globally here instead of per-VM.
*/
int rc = RTR0MemObjAllocCont(&g_hMemObjIOBitmap, 3 << PAGE_SHIFT, false /* fExecutable */);
if (RT_FAILURE(rc))
return rc;
g_pvIOBitmap = RTR0MemObjAddress(g_hMemObjIOBitmap);
g_HCPhysIOBitmap = RTR0MemObjGetPagePhysAddr(g_hMemObjIOBitmap, 0 /* iPage */);
/* Set all bits to intercept all IO accesses. */
ASMMemFill32(pVM->hm.s.svm.pvIOBitmap, 3 << PAGE_SHIFT, UINT32_C(0xffffffff));
}
/**
* Does global VT-x termination (called during module termination).
*/
VMMR0DECL(void) SVMR0GlobalTerm(void)
{
if (g_hMemObjIOBitmap != NIL_RTR0MEMOBJ)
{
RTR0MemObjFree(pVM->hm.s.svm.hMemObjIOBitmap, false /* fFreeMappings */);
g_pvIOBitmap = NULL;
g_HCPhysIOBitmap = 0;
g_hMemObjIOBitmap = NIL_RTR0MEMOBJ;
}
}
/**
* Frees any allocated per-VCPU structures for a VM.
*
* @param pVM Pointer to the VM.
*/
DECLINLINE(void) hmR0SvmFreeStructs(PVM pVM)
{
for (uint32_t i = 0; i < pVM->cCpus; i++)
{
PVMCPU pVCpu = &pVM->aCpus[i];
if (pVCpu->hm.s.svm.hMemObjVMCBHost != NIL_RTR0MEMOBJ)
{
RTR0MemObjFree(pVCpu->hm.s.svm.hMemObjVMCBHost, false);
pVCpu->hm.s.svm.pvVMCBHost = 0;
pVCpu->hm.s.svm.HCPhysVMCBHost = 0;
pVCpu->hm.s.svm.hMemObjVMCBHost = NIL_RTR0MEMOBJ;
}
if (pVCpu->hm.s.svm.hMemObjVMCB != NIL_RTR0MEMOBJ)
{
RTR0MemObjFree(pVCpu->hm.s.svm.hMemObjVMCB, false);
pVCpu->hm.s.svm.pvVMCB = 0;
pVCpu->hm.s.svm.HCPhysVMCB = 0;
pVCpu->hm.s.svm.hMemObjVMCB = NIL_RTR0MEMOBJ;
}
if (pVCpu->hm.s.svm.hMemObjMsrBitmap != NIL_RTR0MEMOBJ)
{
RTR0MemObjFree(pVCpu->hm.s.svm.hMemObjMsrBitmap, false);
pVCpu->hm.s.svm.pvMsrBitmap = 0;
pVCpu->hm.s.svm.HCPhysMsrBitmap = 0;
pVCpu->hm.s.svm.hMemObjMsrBitmap = NIL_RTR0MEMOBJ;
}
}
}
/**
* Does per-VM AMD-V initialization.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
*/
VMMR0DECL(int) SVMR0InitVM(PVM pVM)
{
int rc;
/* Check for an AMD CPU erratum which requires us to flush the TLB before every world-switch. */
uint32_t u32Family;
uint32_t u32Model;
uint32_t u32Stepping;
if (HMAmdIsSubjectToErratum170(&u32Family, &u32Model, &u32Stepping))
{
Log(("SVMR0InitVM: AMD cpu with erratum 170 family %#x model %#x stepping %#x\n", u32Family, u32Model, u32Stepping));
pVM->hm.s.svm.fAlwaysFlushTLB = true;
}
/* Initialize the memory objects up-front so we can cleanup on allocation failures properly. */
for (uint32_t i = 0; i < pVM->cCpus; i++)
{
PVMCPU pVCpu = &pVM->aCpus[i];
pVCpu->hm.s.svm.hMemObjVMCBHost = NIL_RTR0MEMOBJ;
pVCpu->hm.s.svm.hMemObjVMCB = NIL_RTR0MEMOBJ;
pVCpu->hm.s.svm.hMemObjMsrBitmap = NIL_RTR0MEMOBJ;
}
/* Allocate a VMCB for each VCPU. */
for (uint32_t i = 0; i < pVM->cCpus; i++)
{
/* Allocate one page for the host context */
rc = RTR0MemObjAllocCont(&pVCpu->hm.s.svm.hMemObjVMCBHost, 1 << PAGE_SHIFT, false /* fExecutable */);
if (RT_FAILURE(rc))
goto failure_cleanup;
pVCpu->hm.s.svm.pvVMCBHost = RTR0MemObjAddress(pVCpu->hm.s.svm.hMemObjVMCBHost);
pVCpu->hm.s.svm.HCPhysVMCBHost = RTR0MemObjGetPagePhysAddr(pVCpu->hm.s.svm.hMemObjVMCBHost, 0);
Assert(pVCpu->hm.s.svm.HCPhysVMCBHost < _4G);
ASMMemZeroPage(pVCpu->hm.s.svm.pvVMCBHost);
/* Allocate one page for the VM control block (VMCB). */
rc = RTR0MemObjAllocCont(&pVCpu->hm.s.svm.hMemObjVMCB, 1 << PAGE_SHIFT, false /* fExecutable */);
if (RT_FAILURE(rc))
goto failure_cleanup;
pVCpu->hm.s.svm.pvVMCB = RTR0MemObjAddress(pVCpu->hm.s.svm.hMemObjVMCB);
pVCpu->hm.s.svm.HCPhysVMCB = RTR0MemObjGetPagePhysAddr(pVCpu->hm.s.svm.hMemObjVMCB, 0);
Assert(pVCpu->hm.s.svm.HCPhysVMCB < _4G);
ASMMemZeroPage(pVCpu->hm.s.svm.pvVMCB);
/* Allocate 8 KB for the MSR bitmap (doesn't seem to be a way to convince SVM not to use it) */
rc = RTR0MemObjAllocCont(&pVCpu->hm.s.svm.hMemObjMsrBitmap, 2 << PAGE_SHIFT, false /* fExecutable */);
if (RT_FAILURE(rc))
failure_cleanup;
pVCpu->hm.s.svm.pvMsrBitmap = RTR0MemObjAddress(pVCpu->hm.s.svm.hMemObjMsrBitmap);
pVCpu->hm.s.svm.HCPhysMsrBitmap = RTR0MemObjGetPagePhysAddr(pVCpu->hm.s.svm.hMemObjMsrBitmap, 0);
/* Set all bits to intercept all MSR accesses. */
ASMMemFill32(pVCpu->hm.s.svm.pvMsrBitmap, 2 << PAGE_SHIFT, 0xffffffff);
}
return VINF_SUCCESS;
failure_cleanup:
hmR0SvmFreeVMStructs(pVM);
return rc;
}
/**
* Does per-VM AMD-V termination.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
*/
VMMR0DECL(int) SVMR0TermVM(PVM pVM)
{
hmR0SvmFreeVMStructs(pVM);
return VINF_SUCCESS;
}