HWACCM.cpp revision 1870b02d10275ce54e2b8a4da8d031f0318d93f0
/* $Id$ */
/** @file
*/
/*
* Copyright (C) 2006 InnoTek Systemberatung GmbH
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License 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.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_HWACCM
#include <VBox/hwacc_vmx.h>
#include <VBox/hwacc_svm.h>
#include "HWACCMInternal.h"
#include "x86context.h"
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/**
* Initializes the HWACCM.
*
* @returns VBox status code.
* @param pVM The VM to operate on.
*/
{
LogFlow(("HWACCMR3Init\n"));
/*
* Assert alignment and sizes.
*/
/* Some structure checks. */
AssertMsg(RT_OFFSETOF(SVM_VMCB, u8Reserved3) == 0xC0, ("u8Reserved3 offset = %x\n", RT_OFFSETOF(SVM_VMCB, u8Reserved3)));
AssertMsg(RT_OFFSETOF(SVM_VMCB, ctrl.EventInject) == 0xA8, ("ctrl.EventInject offset = %x\n", RT_OFFSETOF(SVM_VMCB, ctrl.EventInject)));
AssertMsg(RT_OFFSETOF(SVM_VMCB, ctrl.ExitIntInfo) == 0x88, ("ctrl.ExitIntInfo offset = %x\n", RT_OFFSETOF(SVM_VMCB, ctrl.ExitIntInfo)));
AssertMsg(RT_OFFSETOF(SVM_VMCB, ctrl.TLBCtrl) == 0x58, ("ctrl.TLBCtrl offset = %x\n", RT_OFFSETOF(SVM_VMCB, ctrl.TLBCtrl)));
AssertMsg(RT_OFFSETOF(SVM_VMCB, guest) == 0x400, ("guest offset = %x\n", RT_OFFSETOF(SVM_VMCB, guest)));
AssertMsg(RT_OFFSETOF(SVM_VMCB, guest.u8Reserved4) == 0x4A0, ("guest.u8Reserved4 offset = %x\n", RT_OFFSETOF(SVM_VMCB, guest.u8Reserved4)));
AssertMsg(RT_OFFSETOF(SVM_VMCB, guest.u8Reserved6) == 0x4D8, ("guest.u8Reserved6 offset = %x\n", RT_OFFSETOF(SVM_VMCB, guest.u8Reserved6)));
AssertMsg(RT_OFFSETOF(SVM_VMCB, guest.u8Reserved7) == 0x580, ("guest.u8Reserved7 offset = %x\n", RT_OFFSETOF(SVM_VMCB, guest.u8Reserved7)));
AssertMsg(RT_OFFSETOF(SVM_VMCB, guest.u8Reserved9) == 0x648, ("guest.u8Reserved9 offset = %x\n", RT_OFFSETOF(SVM_VMCB, guest.u8Reserved9)));
AssertMsg(RT_OFFSETOF(SVM_VMCB, u8Reserved10) == 0x698, ("u8Reserved3 offset = %x\n", RT_OFFSETOF(SVM_VMCB, u8Reserved10)));
/*
* Register the saved state data unit.
*/
if (VBOX_FAILURE(rc))
return rc;
/** @todo Make sure both pages are either not accessible or readonly! */
/* Allocate one page for VMXON. */
{
AssertMsgFailed(("SUPContAlloc failed!!\n"));
return VERR_NO_MEMORY;
}
/* Allocate one page for the VM control structure (VMCS). */
{
AssertMsgFailed(("SUPContAlloc failed!!\n"));
return VERR_NO_MEMORY;
}
/* Reuse those two pages for AMD SVM. (one is active; never both) */
{
AssertMsgFailed(("SUPContAlloc failed!!\n"));
return VERR_NO_MEMORY;
}
/* Allocate 12 KB for the IO bitmap (doesn't seem to be a way to convince SVM not to use it) */
{
AssertMsgFailed(("SUPContAlloc failed!!\n"));
return VERR_NO_MEMORY;
}
/* Set all bits to intercept all IO accesses. */
/* Allocate 8 KB for the MSR bitmap (doesn't seem to be a way to convince SVM not to use it) */
{
AssertMsgFailed(("SUPContAlloc failed!!\n"));
return VERR_NO_MEMORY;
}
/* Set all bits to intercept all MSR accesses. */
/* Misc initialisation. */
/* On first entry we'll sync everything. */
/*
* Statistics.
*/
STAM_REG(pVM, &pVM->hwaccm.s.StatEntry, STAMTYPE_PROFILE, "/PROF/HWACCM/SwitchToGC", STAMUNIT_TICKS_PER_CALL, "Profiling of VMXR0RunGuestCode entry");
STAM_REG(pVM, &pVM->hwaccm.s.StatExit, STAMTYPE_PROFILE, "/PROF/HWACCM/SwitchFromGC", STAMUNIT_TICKS_PER_CALL, "Profiling of VMXR0RunGuestCode exit");
STAM_REG(pVM, &pVM->hwaccm.s.StatInGC, STAMTYPE_PROFILE, "/PROF/HWACCM/InGC", STAMUNIT_TICKS_PER_CALL, "Profiling of vmlaunch");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitShadowNM, STAMTYPE_COUNTER, "/HWACCM/Exit/Trap/Shadow/#NM", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitGuestNM, STAMTYPE_COUNTER, "/HWACCM/Exit/Trap/Guest/#NM", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitShadowPF, STAMTYPE_COUNTER, "/HWACCM/Exit/Trap/Shadow/#PF", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitGuestPF, STAMTYPE_COUNTER, "/HWACCM/Exit/Trap/Guest/#PF", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitGuestUD, STAMTYPE_COUNTER, "/HWACCM/Exit/Trap/Guest/#UD", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitGuestSS, STAMTYPE_COUNTER, "/HWACCM/Exit/Trap/Guest/#SS", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitGuestNP, STAMTYPE_COUNTER, "/HWACCM/Exit/Trap/Guest/#NP", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitGuestGP, STAMTYPE_COUNTER, "/HWACCM/Exit/Trap/Guest/#GP", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitGuestMF, STAMTYPE_COUNTER, "/HWACCM/Exit/Trap/Guest/#MF", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitGuestDE, STAMTYPE_COUNTER, "/HWACCM/Exit/Trap/Guest/#DE", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitInvpg, STAMTYPE_COUNTER, "/HWACCM/Exit/Instr/Invlpg", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitInvd, STAMTYPE_COUNTER, "/HWACCM/Exit/Instr/Invd", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitCpuid, STAMTYPE_COUNTER, "/HWACCM/Exit/Instr/Cpuid", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitCRxWrite, STAMTYPE_COUNTER, "/HWACCM/Exit/Instr/CRx/Write", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitCRxRead, STAMTYPE_COUNTER, "/HWACCM/Exit/Instr/CRx/Read", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitDRxWrite, STAMTYPE_COUNTER, "/HWACCM/Exit/Instr/DRx/Write", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitDRxRead, STAMTYPE_COUNTER, "/HWACCM/Exit/Instr/DRx/Read", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitCLTS, STAMTYPE_COUNTER, "/HWACCM/Exit/Instr/CLTS", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitLMSW, STAMTYPE_COUNTER, "/HWACCM/Exit/Instr/LMSW", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitIOWrite, STAMTYPE_COUNTER, "/HWACCM/Exit/IO/Write", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitIORead, STAMTYPE_COUNTER, "/HWACCM/Exit/IO/Read", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitIOStringWrite, STAMTYPE_COUNTER, "/HWACCM/Exit/IO/WriteString", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitIOStringRead, STAMTYPE_COUNTER, "/HWACCM/Exit/IO/ReadString", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatExitIrqWindow, STAMTYPE_COUNTER, "/HWACCM/Exit/GuestIrq/Pending", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatSwitchGuestIrq,STAMTYPE_COUNTER, "/HWACCM/Switch/IrqPending", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatSwitchToR3, STAMTYPE_COUNTER, "/HWACCM/Switch/ToR3", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatIntInject, STAMTYPE_COUNTER, "/HWACCM/Irq/Inject", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatIntReinject, STAMTYPE_COUNTER, "/HWACCM/Irq/Reinject", STAMUNIT_OCCURENCES, "Nr of occurances");
STAM_REG(pVM, &pVM->hwaccm.s.StatPendingHostIrq,STAMTYPE_COUNTER, "/HWACCM/Irq/PendingOnHost", STAMUNIT_OCCURENCES, "Nr of occurances");
#ifdef VBOX_WITH_STATISTICS
rc = MMHyperAlloc(pVM, MAX_EXITREASON_STAT*sizeof(*pVM->hwaccm.s.pStatExitReason), 0, MM_TAG_HWACCM, (void **)&pVM->hwaccm.s.pStatExitReason);
if (VBOX_SUCCESS(rc))
{
for (int i=0;i<MAX_EXITREASON_STAT;i++)
{
char szName[64];
int rc = STAMR3Register(pVM, &pVM->hwaccm.s.pStatExitReason[i], STAMTYPE_COUNTER, STAMVISIBILITY_USED, szName, STAMUNIT_OCCURENCES, "Exit reason");
}
}
#endif
/* Disabled by default. */
pVM->fHWACCMEnabled = false;
/* HWACCM support must be explicitely enabled in the configuration file. */
CFGMR3QueryBool(CFGMR3GetChild(CFGMR3GetRoot(pVM), "HWVirtExt/"), "Enabled", &pVM->hwaccm.s.fAllowed);
return VINF_SUCCESS;
}
/**
* Turns off normal raw mode features
*
* @param pVM The VM to operate on.
*/
{
/* Disable PATM & CSAM. */
PATMR3AllowPatching(pVM, false);
/* The hidden selector registers are now valid. */
CPUMSetHiddenSelRegsValid(pVM, true);
/* Disable the switcher code (safety precaution). */
/* Disable mapping of the hypervisor into the shadow page table. */
PGMR3ChangeShwPDMappings(pVM, false);
/* Disable the switcher */
}
/**
* Applies relocations to data and code managed by this
* component. This function will be called at init and
* whenever the VMM need to relocate it self inside the GC.
*
* @param pVM The VM.
*/
{
#ifdef LOG_ENABLED
#endif
return ;
{
{
/* Only try once. */
if (rc == VINF_SUCCESS)
{
pVM->fHWACCMEnabled = true;
LogRel(("HWACCM: VMX enabled!\n"));
}
else
{
pVM->fHWACCMEnabled = false;
}
}
}
else
{
LogRel(("HWACMM: cpuid 0x80000001.u32AMDFeatureECX = %VX32\n", pVM->hwaccm.s.cpuid.u32AMDFeatureECX));
LogRel(("HWACMM: cpuid 0x80000001.u32AMDFeatureEDX = %VX32\n", pVM->hwaccm.s.cpuid.u32AMDFeatureEDX));
{
/* Only try once. */
if (rc == VINF_SUCCESS)
{
pVM->fHWACCMEnabled = true;
}
else
{
pVM->fHWACCMEnabled = false;
}
}
}
}
/**
* Checks hardware accelerated raw mode is allowed.
*
* @returns boolean
* @param pVM The VM to operate on.
*/
{
}
/**
* Notification callback which is called whenever there is a chance that a CR3
* value might have changed.
* This is called by PGM.
*
* @param pVM The VM to operate on.
* @param enmShadowMode New paging mode.
*/
{
}
/**
* Terminates the HWACCM.
*
* Termination means cleaning up and freeing all resources,
* the VM it self is at this point powered off or suspended.
*
* @returns VBox status code.
* @param pVM The VM to operate on.
*/
{
{
}
{
}
{
}
{
}
{
}
{
}
return 0;
}
/**
* The VM is being reset.
*
* needs to be removed.
*
* @param pVM VM handle.
*/
{
LogFlow(("HWACCMR3Reset:\n"));
if (pVM->fHWACCMEnabled)
/* On first entry we'll sync everything. */
}
/**
* Checks if we can currently use hardware accelerated raw mode.
*
* @returns boolean
* @param pVM The VM to operate on.
* @param pCtx Partial VM execution context
*/
{
#if 1
/* AMD SVM supports real & protected mode with or without paging. */
{
return true;
}
#endif
/* @todo we can support real-mode by using v86 and protected mode without paging with identity mapped pages.
* (but do we really care?)
*/
/** @note The context supplied by REM is partial. If we add more checks here, be sure to verify that REM provides this info! */
/* Too early for VMX. */
return false;
/* The guest is about to complete the switch to protected mode. Wait a bit longer. */
return false;
return false;
/** @todo if we remove this check, then Windows XP install fails during the textmode phase */
return false;
{
/* if bit N is set in cr0_fixed0, then it must be set in the guest's cr0. */
/** @note We ignore the NE bit here on purpose; see vmmr0\hwaccmr0.cpp for details. */
mask &= ~X86_CR0_NE;
return false;
/* if bit N is cleared in cr0_fixed1, then it must be zero in the guest's cr0. */
return false;
/* if bit N is set in cr4_fixed0, then it must be set in the guest's cr4. */
mask &= ~X86_CR4_VMXE;
return false;
/* if bit N is cleared in cr4_fixed1, then it must be zero in the guest's cr4. */
return false;
return true;
}
else
{
return true;
}
return false;
}
/**
* Checks if we are currently using hardware accelerated raw mode.
*
* @returns boolean
* @param pVM The VM to operate on.
*/
{
}
/**
* Checks if internal events are pending. In that case we are not allowed to dispatch interrupts.
*
* @returns boolean
* @param pVM The VM to operate on.
*/
{
}
/**
* Execute state save operation.
*
* @returns VBox status code.
* @param pVM VM Handle.
* @param pSSM SSM operation handle.
*/
{
int rc;
Log(("hwaccmR3Save:\n"));
/*
* Save the basic bits - fortunately all the other things can be resynced on load.
*/
return VINF_SUCCESS;
}
/**
* Execute state load operation.
*
* @returns VBox status code.
* @param pVM VM Handle.
* @param pSSM SSM operation handle.
* @param u32Version Data layout version.
*/
{
int rc;
Log(("hwaccmR3Load:\n"));
/*
* Validate version.
*/
if (u32Version != HWACCM_SSM_VERSION)
{
}
return VINF_SUCCESS;
}