HM.cpp revision 6ccf50f343c9e0f53c53d3a996a5e02c3cda5043
/* $Id$ */
/** @file
*/
/*
* Copyright (C) 2006-2012 Oracle Corporation
*
* 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 (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 *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_HM
#ifdef VBOX_WITH_REM
#endif
#include "HMInternal.h"
#include <iprt/asm-amd64-x86.h>
/*******************************************************************************
* Global Variables *
*******************************************************************************/
#ifdef VBOX_WITH_STATISTICS
# define EXIT_REASON_NIL() NULL
/** Exit reason descriptions for VT-x, used to describe statistics. */
static const char * const g_apszVTxExitReasons[MAX_EXITREASON_STAT] =
{
EXIT_REASON(VMX_EXIT_APIC_ACCESS , 44, "APIC access. Guest software attempted to access memory at a physical address on the APIC-access page."),
EXIT_REASON(VMX_EXIT_XDTR_ACCESS , 46, "Access to GDTR or IDTR. Guest software attempted to execute LGDT, LIDT, SGDT, or SIDT."),
EXIT_REASON(VMX_EXIT_TR_ACCESS , 47, "Access to LDTR or TR. Guest software attempted to execute LLDT, LTR, SLDT, or STR."),
EXIT_REASON(VMX_EXIT_EPT_VIOLATION , 48, "EPT violation. An attempt to access memory with a guest-physical address was disallowed by the configuration of the EPT paging structures."),
EXIT_REASON(VMX_EXIT_EPT_MISCONFIG , 49, "EPT misconfiguration. An attempt to access memory with a guest-physical address encountered a misconfigured EPT paging-structure entry."),
EXIT_REASON(VMX_EXIT_PREEMPTION_TIMER , 52, "VMX-preemption timer expired. The preemption timer counted down to zero."),
};
/** Exit reason descriptions for AMD-V, used to describe statistics. */
static const char * const g_apszAmdVExitReasons[MAX_EXITREASON_STAT] =
{
EXIT_REASON(SVM_EXIT_CR0_SEL_WRITE ,101, "Write to CR0 that changed any bits other than CR0.TS or CR0.MP."),
EXIT_REASON(SVM_EXIT_IOIO ,123, "IN/OUT accessing protected port (EXITINFO1 field provides more information)."),
EXIT_REASON(SVM_EXIT_FERR_FREEZE ,126, "FP legacy handling enabled, and processor is frozen in an x87/mmx instruction waiting for an interrupt"),
EXIT_REASON(SVM_EXIT_NPF ,1024, "Nested paging: host-level page fault occurred (EXITINFO1 contains fault errorcode; EXITINFO2 contains the guest physical address causing the fault)."),
};
#endif /* VBOX_WITH_STATISTICS */
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/**
* Initializes the HM.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
*/
{
LogFlow(("HMR3Init\n"));
/*
* Assert alignment and sizes.
*/
/* Some structure checks. */
AssertReleaseMsg(RT_OFFSETOF(SVM_VMCB, ctrl.EventInject) == 0xA8, ("ctrl.EventInject offset = %x\n", RT_OFFSETOF(SVM_VMCB, ctrl.EventInject)));
AssertReleaseMsg(RT_OFFSETOF(SVM_VMCB, ctrl.ExitIntInfo) == 0x88, ("ctrl.ExitIntInfo offset = %x\n", RT_OFFSETOF(SVM_VMCB, ctrl.ExitIntInfo)));
AssertReleaseMsg(RT_OFFSETOF(SVM_VMCB, ctrl.TLBCtrl) == 0x58, ("ctrl.TLBCtrl offset = %x\n", RT_OFFSETOF(SVM_VMCB, ctrl.TLBCtrl)));
AssertReleaseMsg(RT_OFFSETOF(SVM_VMCB, guest) == 0x400, ("guest offset = %x\n", RT_OFFSETOF(SVM_VMCB, guest)));
AssertReleaseMsg(RT_OFFSETOF(SVM_VMCB, guest.TR) == 0x490, ("guest.TR offset = %x\n", RT_OFFSETOF(SVM_VMCB, guest.TR)));
AssertReleaseMsg(RT_OFFSETOF(SVM_VMCB, guest.u8CPL) == 0x4CB, ("guest.u8CPL offset = %x\n", RT_OFFSETOF(SVM_VMCB, guest.u8CPL)));
AssertReleaseMsg(RT_OFFSETOF(SVM_VMCB, guest.u64EFER) == 0x4D0, ("guest.u64EFER offset = %x\n", RT_OFFSETOF(SVM_VMCB, guest.u64EFER)));
AssertReleaseMsg(RT_OFFSETOF(SVM_VMCB, guest.u64CR4) == 0x548, ("guest.u64CR4 offset = %x\n", RT_OFFSETOF(SVM_VMCB, guest.u64CR4)));
AssertReleaseMsg(RT_OFFSETOF(SVM_VMCB, guest.u64RIP) == 0x578, ("guest.u64RIP offset = %x\n", RT_OFFSETOF(SVM_VMCB, guest.u64RIP)));
AssertReleaseMsg(RT_OFFSETOF(SVM_VMCB, guest.u64RSP) == 0x5D8, ("guest.u64RSP offset = %x\n", RT_OFFSETOF(SVM_VMCB, guest.u64RSP)));
AssertReleaseMsg(RT_OFFSETOF(SVM_VMCB, guest.u64CR2) == 0x640, ("guest.u64CR2 offset = %x\n", RT_OFFSETOF(SVM_VMCB, guest.u64CR2)));
AssertReleaseMsg(RT_OFFSETOF(SVM_VMCB, guest.u64GPAT) == 0x668, ("guest.u64GPAT offset = %x\n", RT_OFFSETOF(SVM_VMCB, guest.u64GPAT)));
AssertReleaseMsg(RT_OFFSETOF(SVM_VMCB, guest.u64LASTEXCPTO) == 0x690, ("guest.u64LASTEXCPTO offset = %x\n", RT_OFFSETOF(SVM_VMCB, guest.u64LASTEXCPTO)));
/*
* Register the saved state data unit.
*/
if (RT_FAILURE(rc))
return rc;
/* Misc initialisation. */
/* Disabled by default. */
pVM->fHMEnabled = false;
/*
* Check CFGM options.
*/
/* Nested paging: disabled by default. */
/* Large pages: disabled by default. */
/* VT-x VPID: disabled by default. */
/* HM support must be explicitely enabled in the configuration file. */
/* TPR patching for 32 bits (Windows) guests with IO-APIC: disabled by default. */
#ifdef RT_OS_DARWIN
#else
#endif
{
AssertLogRelMsgFailed(("VMMIsHwVirtExtForced=%RTbool fAllowed=%RTbool\n",
return VERR_HM_CONFIG_MISMATCH;
}
if (VMMIsHwVirtExtForced(pVM))
pVM->fHMEnabled = true;
#if HC_ARCH_BITS == 32
/*
* 64-bit mode is configurable and it depends on both the kernel mode and VT-x.
* (To use the default, don't set 64bitEnabled in CFGM.)
*/
{
# ifdef RT_OS_DARWIN
if (!VMMIsHwVirtExtForced(pVM))
# else
# endif
return VM_SET_ERROR(pVM, VERR_INVALID_PARAMETER, "64-bit guest support was requested without also enabling HWVirtEx (VT-x/AMD-V).");
}
#else
/*
* On 64-bit hosts 64-bit guest support is enabled by default, but allow this to be overridden
* via VBoxInternal/HWVirtExt/64bitEnabled=0. (ConsoleImpl2.cpp doesn't set this to false for 64-bit.)*
*/
#endif
/*
* Determine the init method for AMD-V and VT-x; either one global init for each host CPU
* or local init each time we wish to execute guest code.
*
* Default false for Mac OS X and Windows due to the higher risk of conflicts with other hypervisors.
*/
#if defined(RT_OS_DARWIN) || defined(RT_OS_WINDOWS)
false
#else
true
#endif
);
/* Max number of resume loops. */
rc = CFGMR3QueryU32Def(pHWVirtExt, "MaxResumeLoops", &pVM->hm.s.cMaxResumeLoops, 0 /* set by R0 later */);
return rc;
}
/**
* Initializes the per-VCPU HM.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
*/
{
LogFlow(("HMR3InitCPU\n"));
{
}
#ifdef VBOX_WITH_STATISTICS
STAM_REG(pVM, &pVM->hm.s.StatTprPatchSuccess, STAMTYPE_COUNTER, "/HM/TPR/Patch/Success", STAMUNIT_OCCURENCES, "Number of times an instruction was successfully patched.");
STAM_REG(pVM, &pVM->hm.s.StatTprPatchFailure, STAMTYPE_COUNTER, "/HM/TPR/Patch/Failed", STAMUNIT_OCCURENCES, "Number of unsuccessful patch attempts.");
STAM_REG(pVM, &pVM->hm.s.StatTprReplaceSuccess, STAMTYPE_COUNTER, "/HM/TPR/Replace/Success",STAMUNIT_OCCURENCES, "Number of times an instruction was successfully patched.");
STAM_REG(pVM, &pVM->hm.s.StatTprReplaceFailure, STAMTYPE_COUNTER, "/HM/TPR/Replace/Failed", STAMUNIT_OCCURENCES, "Number of unsuccessful patch attempts.");
/*
* Statistics.
*/
{
int rc;
rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatPoke, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
"Profiling of RTMpPokeCpu",
rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatSpinPoke, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
"Profiling of poke wait",
rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatSpinPokeFailed, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
"Profiling of poke wait when RTMpPokeCpu fails",
"/PROF/HM/CPU%d/PokeWaitFailed", i);
rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatEntry, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
"Profiling of VMXR0RunGuestCode entry",
"/PROF/HM/CPU%d/SwitchToGC", i);
rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatExit1, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
"Profiling of VMXR0RunGuestCode exit part 1",
"/PROF/HM/CPU%d/SwitchFromGC_1", i);
rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatExit2, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
"Profiling of VMXR0RunGuestCode exit part 2",
"/PROF/HM/CPU%d/SwitchFromGC_2", i);
# if 1 /* temporary for tracking down darwin holdup. */
rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatExit2Sub1, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
"Temporary - I/O",
rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatExit2Sub2, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
"Temporary - CRx RWs",
rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatExit2Sub3, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
"Temporary - Exceptions",
# endif
rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatInGC, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
"Profiling of vmlaunch",
# if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
STAMUNIT_TICKS_PER_CALL, "Profiling of the 32/64 switcher",
"/PROF/HM/CPU%d/Switcher3264", i);
# endif
# define HM_REG_COUNTER(a, b) \
rc = STAMR3RegisterF(pVM, a, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Profiling of vmlaunch", b, i); \
#if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
#endif
{
STAMUNIT_OCCURENCES, "Profiling of CRx writes",
STAMUNIT_OCCURENCES, "Profiling of CRx reads",
}
if (RT_SUCCESS(rc))
{
const char * const *papszDesc = ASMIsIntelCpu() ? &g_apszVTxExitReasons[0] : &g_apszAmdVExitReasons[0];
for (int j = 0; j < MAX_EXITREASON_STAT; j++)
{
if (papszDesc[j])
{
}
}
rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatExitReasonNpf, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_OCCURENCES,
}
# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
# else
# endif
rc = MMHyperAlloc(pVM, sizeof(STAMCOUNTER) * 256, 8, MM_TAG_HM, (void **)&pVCpu->hm.s.paStatInjectedIrqs);
# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
# else
# endif
for (unsigned j = 0; j < 255; j++)
{
STAMR3RegisterF(pVM, &pVCpu->hm.s.paStatInjectedIrqs[j], STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_OCCURENCES,
"Forwarded interrupts.",
}
}
#endif /* VBOX_WITH_STATISTICS */
#ifdef VBOX_WITH_CRASHDUMP_MAGIC
/* Magic marker for searching in crash dumps. */
{
}
#endif
return VINF_SUCCESS;
}
/**
* Called when a init phase has completed.
*
* @returns VBox status code.
* @param pVM The VM.
* @param enmWhat The phase that completed.
*/
{
switch (enmWhat)
{
case VMINITCOMPLETED_RING3:
return hmR3InitCPU(pVM);
case VMINITCOMPLETED_RING0:
return hmR3InitFinalizeR0(pVM);
default:
return VINF_SUCCESS;
}
}
/**
* Turns off normal raw mode features.
*
* @param pVM Pointer to the VM.
*/
{
/* Disable PATM & CSAM. */
PATMR3AllowPatching(pVM, false);
/* Disable the switcher code (safety precaution). */
/* Disable mapping of the hypervisor into the shadow page table. */
/* Disable the switcher */
/* Reinit the paging mode to force the new shadow mode. */
{
}
}
/**
* Initialize VT-x or AMD-V.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
*/
{
int rc;
/*
* Hack to allow users to work around broken BIOSes that incorrectly set EFER.SVME, which makes us believe somebody else
* is already using AMD-V.
*/
&& RTEnvExist("VBOX_HWVIRTEX_IGNORE_SVM_IN_USE"))
{
LogRel(("HM: VBOX_HWVIRTEX_IGNORE_SVM_IN_USE active!\n"));
}
else
{
if (VMMIsHwVirtExtForced(pVM))
{
{
case VERR_VMX_NO_VMX:
case VERR_SVM_IN_USE:
case VERR_SVM_NO_SVM:
case VERR_SVM_DISABLED:
default:
}
}
return VINF_SUCCESS;
}
{
rc = SUPR3QueryVTxSupported();
if (RT_FAILURE(rc))
{
#ifdef RT_OS_LINUX
LogRel(("HM: The host kernel does not support VT-x -- Linux 2.6.13 or newer required!\n"));
#else
LogRel(("HM: The host kernel does not support VT-x!\n"));
#endif
|| VMMIsHwVirtExtForced(pVM))
return rc;
/* silently fall back to raw mode */
return VINF_SUCCESS;
}
}
return VINF_SUCCESS; /* nothing to do */
/* Enable VT-x or AMD-V on all host CPUs. */
if (RT_FAILURE(rc))
{
return rc;
}
/* No TPR patching is required when the IO-APIC is not enabled for this VM. (Main should have taken care of this already) */
{
}
{
{
LogRel(("HM: VMCS size = %x\n", MSR_IA32_VMX_BASIC_INFO_VMCS_SIZE(pVM->hm.s.vmx.msr.vmx_basic_info)));
LogRel(("HM: VMCS physical address limit = %s\n", MSR_IA32_VMX_BASIC_INFO_VMCS_PHYS_WIDTH(pVM->hm.s.vmx.msr.vmx_basic_info) ? "< 4 GB" : "None"));
LogRel(("HM: VMCS memory type = %x\n", MSR_IA32_VMX_BASIC_INFO_VMCS_MEM_TYPE(pVM->hm.s.vmx.msr.vmx_basic_info)));
LogRel(("HM: Dual monitor treatment = %d\n", MSR_IA32_VMX_BASIC_INFO_VMCS_DUAL_MON(pVM->hm.s.vmx.msr.vmx_basic_info)));
LogRel(("HM: VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_EXT_INT_EXIT\n"));
LogRel(("HM: VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_NMI_EXIT\n"));
LogRel(("HM: VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_VIRTUAL_NMI\n"));
LogRel(("HM: VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_PREEMPT_TIMER\n"));
LogRel(("HM: VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_EXT_INT_EXIT *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_NMI_EXIT *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_VIRTUAL_NMI *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_PREEMPT_TIMER *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_INT_WINDOW_EXIT\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_TSC_OFFSET\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_HLT_EXIT\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_INVLPG_EXIT\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MWAIT_EXIT\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_RDPMC_EXIT\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_RDTSC_EXIT\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR3_LOAD_EXIT\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR3_STORE_EXIT\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR8_LOAD_EXIT\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR8_STORE_EXIT\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_TPR_SHADOW\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_NMI_WINDOW_EXIT\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MOV_DR_EXIT\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_UNCOND_IO_EXIT\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_IO_BITMAPS\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MONITOR_TRAP_FLAG\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_MSR_BITMAPS\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MONITOR_EXIT\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_PAUSE_EXIT\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_USE_SECONDARY_EXEC_CTRL\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_INT_WINDOW_EXIT *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_TSC_OFFSET *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_HLT_EXIT *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_INVLPG_EXIT *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MWAIT_EXIT *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_RDPMC_EXIT *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_RDTSC_EXIT *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR3_LOAD_EXIT *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR3_STORE_EXIT *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR8_LOAD_EXIT *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR8_STORE_EXIT *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_TPR_SHADOW *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_NMI_WINDOW_EXIT *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MOV_DR_EXIT *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_UNCOND_IO_EXIT *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_IO_BITMAPS *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MONITOR_TRAP_FLAG *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_MSR_BITMAPS *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MONITOR_EXIT *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_PAUSE_EXIT *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC_USE_SECONDARY_EXEC_CTRL *must* be set\n"));
{
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC2_VIRT_APIC\n"));
if (val & VMX_VMCS_CTRL_PROC_EXEC2_EPT)
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC2_EPT\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC2_DESCRIPTOR_TABLE_EXIT\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC2_RDTSCP\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC2_X2APIC\n"));
if (val & VMX_VMCS_CTRL_PROC_EXEC2_VPID)
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC2_VPID\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC2_WBINVD_EXIT\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC2_REAL_MODE\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC2_PAUSE_LOOP_EXIT\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC2_VIRT_APIC *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC2_DESCRIPTOR_TABLE_EXIT *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC2_RDTSCP *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC2_X2APIC *must* be set\n"));
if (val & VMX_VMCS_CTRL_PROC_EXEC2_EPT)
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC2_EPT *must* be set\n"));
if (val & VMX_VMCS_CTRL_PROC_EXEC2_VPID)
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC2_VPID *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC2_WBINVD_EXIT *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC2_REAL_MODE *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_PROC_EXEC2_PAUSE_LOOP_EXIT *must* be set\n"));
}
LogRel(("HM: VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_DEBUG\n"));
LogRel(("HM: VMX_VMCS_CTRL_ENTRY_CONTROLS_IA32E_MODE_GUEST\n"));
LogRel(("HM: VMX_VMCS_CTRL_ENTRY_CONTROLS_ENTRY_SMM\n"));
LogRel(("HM: VMX_VMCS_CTRL_ENTRY_CONTROLS_DEACTIVATE_DUALMON\n"));
LogRel(("HM: VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_GUEST_PERF_MSR\n"));
LogRel(("HM: VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_GUEST_PAT_MSR\n"));
LogRel(("HM: VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_GUEST_EFER_MSR\n"));
LogRel(("HM: VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_DEBUG *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_ENTRY_CONTROLS_IA32E_MODE_GUEST *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_ENTRY_CONTROLS_ENTRY_SMM *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_ENTRY_CONTROLS_DEACTIVATE_DUALMON *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_GUEST_PERF_MSR *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_GUEST_PAT_MSR *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_GUEST_EFER_MSR *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_DEBUG\n"));
LogRel(("HM: VMX_VMCS_CTRL_EXIT_CONTROLS_HOST_ADDR_SPACE_SIZE\n"));
LogRel(("HM: VMX_VMCS_CTRL_EXIT_CONTROLS_ACK_EXT_INT\n"));
LogRel(("HM: VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_GUEST_PAT_MSR\n"));
LogRel(("HM: VMX_VMCS_CTRL_EXIT_CONTROLS_LOAD_HOST_PAT_MSR\n"));
LogRel(("HM: VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_GUEST_EFER_MSR\n"));
LogRel(("HM: VMX_VMCS_CTRL_EXIT_CONTROLS_LOAD_HOST_EFER_MSR\n"));
LogRel(("HM: VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_VMX_PREEMPT_TIMER\n"));
LogRel(("HM: VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_DEBUG *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_EXIT_CONTROLS_HOST_ADDR_SPACE_SIZE *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_EXIT_CONTROLS_ACK_EXT_INT *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_GUEST_PAT_MSR *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_EXIT_CONTROLS_LOAD_HOST_PAT_MSR *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_GUEST_EFER_MSR *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_EXIT_CONTROLS_LOAD_HOST_EFER_MSR *must* be set\n"));
LogRel(("HM: VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_VMX_PREEMPT_TIMER *must* be set\n"));
{
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_RWX_X_ONLY\n"));
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_RWX_W_ONLY\n"));
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_RWX_WX_ONLY\n"));
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_GAW_21_BITS\n"));
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_GAW_30_BITS\n"));
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_GAW_39_BITS\n"));
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_GAW_48_BITS\n"));
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_GAW_57_BITS\n"));
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_EMT_UC\n"));
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_EMT_WC\n"));
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_EMT_WT\n"));
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_EMT_WP\n"));
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_EMT_WB\n"));
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_SP_21_BITS\n"));
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_SP_30_BITS\n"));
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_SP_39_BITS\n"));
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_SP_48_BITS\n"));
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_INVEPT\n"));
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_INVEPT_SINGLE_CONTEXT\n"));
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_INVEPT_ALL_CONTEXTS\n"));
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_INVVPID\n"));
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_INVVPID_INDIV_ADDR\n"));
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_INVVPID_SINGLE_CONTEXT\n"));
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_INVVPID_ALL_CONTEXTS\n"));
if (pVM->hm.s.vmx.msr.vmx_ept_vpid_caps & MSR_IA32_VMX_EPT_VPID_CAP_INVVPID_SINGLE_CONTEXT_RETAIN_GLOBALS)
LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP_INVVPID_SINGLE_CONTEXT_RETAIN_GLOBALS\n"));
}
if (MSR_IA32_VMX_MISC_PREEMPT_TSC_BIT(pVM->hm.s.vmx.msr.vmx_misc) == pVM->hm.s.vmx.cPreemptTimerShift)
LogRel(("HM: MSR_IA32_VMX_MISC_PREEMPT_TSC_BIT %x\n", MSR_IA32_VMX_MISC_PREEMPT_TSC_BIT(pVM->hm.s.vmx.msr.vmx_misc)));
else
{
LogRel(("HM: MSR_IA32_VMX_MISC_PREEMPT_TSC_BIT %x - erratum detected, using %x instead\n",
}
LogRel(("HM: MSR_IA32_VMX_MISC_ACTIVITY_STATES %x\n", MSR_IA32_VMX_MISC_ACTIVITY_STATES(pVM->hm.s.vmx.msr.vmx_misc)));
LogRel(("HM: MSR_IA32_VMX_MISC_CR3_TARGET %x\n", MSR_IA32_VMX_MISC_CR3_TARGET(pVM->hm.s.vmx.msr.vmx_misc)));
LogRel(("HM: MSR_IA32_VMX_MISC_MAX_MSR %x\n", MSR_IA32_VMX_MISC_MAX_MSR(pVM->hm.s.vmx.msr.vmx_misc)));
LogRel(("HM: MSR_IA32_VMX_MISC_MSEG_ID %x\n", MSR_IA32_VMX_MISC_MSEG_ID(pVM->hm.s.vmx.msr.vmx_misc)));
/* Paranoia */
{
}
/*
* Disallow RDTSCP in the guest if there is no secondary process-based VM execution controls as otherwise
* RDTSCP would cause a #UD. There might be no CPUs out there where this happens, as RDTSCP was introduced
* in Nehalems and secondary VM exec. controls should be supported in all of them, but nonetheless it's Intel...
*/
{
}
/* Unrestricted guest execution relies on EPT. */
{
}
/* Only try once. */
{
/* Allocate three pages for the TSS we need for real mode emulation. (2 pages for the IO bitmap) */
if (RT_SUCCESS(rc))
{
/* The I/O bitmap starts right after the virtual interrupt redirection bitmap. */
/* Refer Intel spec. 20.3.3 "Software Interrupt Handling in Virtual-8086 mode" esp. Figure 20-5.*/
/* Bit set to 0 means software interrupts are redirected to the 8086 program interrupt handler rather than
switching to protected-mode handler. */
memset(pVM->hm.s.vmx.pRealModeTSS->IntRedirBitmap, 0, sizeof(pVM->hm.s.vmx.pRealModeTSS->IntRedirBitmap));
/* Allow all port IO, so that port IO instructions do not cause exceptions and would instead
cause a VM-exit (based on VT-x's IO bitmap which we currently configure to always cause an exit). */
/*
* Construct a 1024 element page directory with 4 MB pages for the identity mapped page table used in
* real and protected mode without paging with EPT.
*/
pVM->hm.s.vmx.pNonPagingModeEPTPageTable = (PX86PD)((char *)pVM->hm.s.vmx.pRealModeTSS + PAGE_SIZE * 3);
for (unsigned i = 0; i < X86_PG_ENTRIES; i++)
{
| X86_PDE4M_G;
}
/* We convert it here every time as pci regions could be reconfigured. */
}
else
{
/** @todo This cannot possibly work, there are other places which assumes
* this allocation cannot fail (see HMR3CanExecuteGuest()). Make this
* a failure case. */
}
}
if (rc == VINF_SUCCESS)
{
pVM->fHMEnabled = true;
#ifdef VBOX_ENABLE_64_BITS_GUESTS
{
}
else
/* Turn on NXE if PAE has been enabled *and* the host has turned on NXE (we reuse the host EFER in the switcher) */
/* Todo: this needs to be fixed properly!! */
? "HM: 32-bit and 64-bit guests supported.\n"
: "HM: 32-bit guests supported.\n"));
#else
LogRel(("HM: 32-bit guests supported.\n"));
#endif
LogRel(("HM: VMX enabled!\n"));
{
LogRel(("HM: Enabled nested paging\n"));
LogRel(("HM: enmFlushEpt = VMX_FLUSH_EPT_SINGLE_CONTEXT\n"));
LogRel(("HM: enmFlushEpt = VMX_FLUSH_EPT_ALL_CONTEXTS\n"));
LogRel(("HM: enmFlushEpt = VMX_FLUSH_EPT_NOT_SUPPORTED\n"));
else
LogRel(("HM: Unrestricted guest execution enabled!\n"));
#if HC_ARCH_BITS == 64
{
/* Use large (2 MB) pages for our EPT PDEs where possible. */
PGMSetLargePageUsage(pVM, true);
LogRel(("HM: Large page support enabled!\n"));
}
#endif
}
else
{
LogRel(("HM: Enabled VPID\n"));
LogRel(("HM: enmFlushVpid = VMX_FLUSH_VPID_INDIV_ADDR\n"));
LogRel(("HM: enmFlushVpid = VMX_FLUSH_VPID_SINGLE_CONTEXT\n"));
LogRel(("HM: enmFlushVpid = VMX_FLUSH_VPID_ALL_CONTEXTS\n"));
LogRel(("HM: enmFlushVpid = VMX_FLUSH_VPID_SINGLE_CONTEXT_RETAIN_GLOBALS\n"));
else
}
LogRel(("HM: Ignoring VPID capabilities of CPU.\n"));
/* TPR patching status logging. */
{
{
LogRel(("HM: TPR Patching not required (VMX_VMCS_CTRL_PROC_EXEC2_VIRT_APIC).\n"));
}
else
{
/* TPR patching needs access to the MSR_K8_LSTAR msr. */
if ( u32Eax < 0x80000001
{
LogRel(("HM: TPR patching disabled (long mode not supported).\n"));
}
}
}
/*
* Check for preemption timer config override and log the state of it.
*/
{
}
LogRel(("HM: Using the VMX-preemption timer (cPreemptTimerShift=%u)\n", pVM->hm.s.vmx.cPreemptTimerShift));
}
else
{
LogRel(("HM: CPU[%ld] Last instruction error %x\n", i, pVM->aCpus[i].hm.s.vmx.lasterror.u32InstrError));
pVM->fHMEnabled = false;
}
}
}
else
{
{
/* Erratum 170 which requires a forced TLB flush for each world switch:
*
* All BH-G1/2 and DH-G1/2 models include a fix:
* Athlon X2: 0x6b 1/2
* 0x68 1/2
* Athlon 64: 0x7f 1
* 0x6f 2
* Sempron: 0x7f 1/2
* 0x6f 2
* 0x6c 2
* 0x7c 2
* Turion 64: 0x68 2
*
*/
if ( u32Family == 0xf
{
LogRel(("HM: AMD cpu with erratum 170 family %x model %x stepping %x\n", u32Family, u32Model, u32Stepping));
}
{
};
for (unsigned i = 0; i < RT_ELEMENTS(s_aSvmFeatures); i++)
{
}
if (fSvmFeatures)
/* Only try once. */
if (rc == VINF_SUCCESS)
{
pVM->fHMEnabled = true;
{
LogRel(("HM: Enabled nested paging\n"));
#if HC_ARCH_BITS == 64
{
/* Use large (2 MB) pages for our nested paging PDEs where possible. */
PGMSetLargePageUsage(pVM, true);
LogRel(("HM: Large page support enabled!\n"));
}
#endif
}
#ifdef VBOX_ENABLE_64_BITS_GUESTS
{
}
else
/* Turn on NXE if PAE has been enabled. */
#endif
? "HM: 32-bit and 64-bit guest supported.\n"
: "HM: 32-bit guest supported.\n"));
}
else
{
pVM->fHMEnabled = false;
}
}
}
if (pVM->fHMEnabled)
return VINF_SUCCESS;
}
/**
* 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.
*/
{
/* Fetch the current paging mode during the relocate callback during state loading. */
{
{
}
}
#if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
if (pVM->fHMEnabled)
{
int rc;
switch (PGMGetHostMode(pVM))
{
case PGMMODE_32_BIT:
break;
case PGMMODE_PAE:
case PGMMODE_PAE_NX:
break;
default:
AssertFailed();
break;
}
# ifdef DEBUG
# endif
}
#endif
return;
}
/**
* Checks if hardware accelerated raw mode is allowed.
*
* @returns true if hardware acceleration is allowed, otherwise false.
* @param pVM Pointer to the VM.
*/
{
}
/**
* Notification callback which is called whenever there is a chance that a CR3
* value might have changed.
*
* This is called by PGM.
*
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
* @param enmShadowMode New shadow paging mode.
* @param enmGuestMode New guest paging mode.
*/
VMMR3DECL(void) HMR3PagingModeChanged(PVM pVM, PVMCPU pVCpu, PGMMODE enmShadowMode, PGMMODE enmGuestMode)
{
/* Ignore page mode changes during state loading. */
return;
&& pVM->fHMEnabled)
{
&& enmGuestMode >= PGMMODE_PROTECTED)
{
/* After a real mode switch to protected mode we must force
CPL to 0. Our real mode emulation had to set it to 3. */
}
}
{
/* Keep track of paging mode changes. */
/* Did we miss a change, because all code was executed in the recompiler? */
{
Log(("HMR3PagingModeChanged missed %s->%s transition (prev %s)\n", PGMGetModeName(pVCpu->hm.s.vmx.enmPrevGuestMode),
PGMGetModeName(pVCpu->hm.s.vmx.enmCurrGuestMode), PGMGetModeName(pVCpu->hm.s.vmx.enmLastSeenGuestMode)));
}
}
/* Reset the contents of the read cache. */
}
/**
* Terminates the HM.
*
* Termination means cleaning up and freeing all resources,
* the VM itself is, at this point, powered off or suspended.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
*/
{
{
}
return 0;
}
/**
* Terminates the per-VCPU HM.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
*/
{
{
#ifdef VBOX_WITH_STATISTICS
{
}
{
}
#endif
#ifdef VBOX_WITH_CRASHDUMP_MAGIC
#endif
}
return 0;
}
/**
* Resets a virtual CPU.
*
* Used by HMR3Reset and CPU hot plugging.
*
* @param pVCpu The CPU to reset.
*/
{
/* On first entry we'll sync everything. */
/* Reset state information for real-mode emulation in VT-x. */
/* Reset the contents of the read cache. */
#ifdef VBOX_WITH_CRASHDUMP_MAGIC
/* Magic marker for searching in crash dumps. */
#endif
}
/**
* The VM is being reset.
*
* needs to be removed.
*
* @param pVM Pointer to the VM.
*/
{
LogFlow(("HMR3Reset:\n"));
if (pVM->fHMEnabled)
{
}
/* Clear all patch information. */
}
/**
* Callback to patch a TPR instruction (vmmcall or mov cr8).
*
* @returns VBox strict status code.
* @param pVM Pointer to the VM.
* @param pVCpu The VMCPU for the EMT we're being called on.
* @param pvUser Unused.
*/
{
/* Only execute the handler on the VCPU the original patch request was issued. */
return VINF_SUCCESS;
Log(("hmR3RemovePatches\n"));
{
int rc;
#ifdef LOG_ENABLED
char szOutput[256];
rc = DBGFR3DisasInstrEx(pVM, pVCpu->idCpu, CPUMGetGuestCS(pVCpu), pInstrGC, DBGF_DISAS_FLAGS_DEFAULT_MODE,
if (RT_SUCCESS(rc))
#endif
/* Check if the instruction is still the same. */
if (rc != VINF_SUCCESS)
{
continue; /* swapped out or otherwise removed; skip it. */
}
{
continue; /* skip it. */
}
#ifdef LOG_ENABLED
rc = DBGFR3DisasInstrEx(pVM, pVCpu->idCpu, CPUMGetGuestCS(pVCpu), pInstrGC, DBGF_DISAS_FLAGS_DEFAULT_MODE,
if (RT_SUCCESS(rc))
#endif
}
return VINF_SUCCESS;
}
/**
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param idCpu VCPU to execute hmR3RemovePatches on.
* @param pPatchMem Patch memory range.
* @param cbPatchMem Size of the memory range.
*/
{
int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE, hmR3RemovePatches, (void *)(uintptr_t)idCpu);
return VINF_SUCCESS;
}
/**
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pPatchMem Patch memory range.
* @param cbPatchMem Size of the memory range.
*/
{
{
/* We own the IOM lock here and could cause a deadlock by waiting for a VCPU that is blocking on the IOM lock. */
return rc;
}
}
/**
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pPatchMem Patch memory range.
* @param cbPatchMem Size of the memory range.
*/
{
/* @todo Potential deadlock when other VCPUs are waiting on the IOM lock (we own it)!! */
return VINF_SUCCESS;
}
/**
* Callback to patch a TPR instruction (vmmcall or mov cr8).
*
* @returns VBox strict status code.
* @param pVM Pointer to the VM.
* @param pVCpu The VMCPU for the EMT we're being called on.
* @param pvUser User specified CPU context.
*
*/
{
/*
* Only execute the handler on the VCPU the original patch request was
* issued. (The other CPU(s) might not yet have switched to protected
* mode, nor have the correct memory context.)
*/
return VINF_SUCCESS;
/*
* We're racing other VCPUs here, so don't try patch the instruction twice
* and make sure there is still room for our patch record.
*/
if (pPatch)
{
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
}
/*
* Disassembler the instruction and get cracking.
*/
if ( rc == VINF_SUCCESS
&& cbOp >= 3)
{
{
/* write. */
{
}
else
{
}
}
else
{
/*
* TPR Read.
*
* Found:
* mov eax, dword [fffe0080] (5 bytes)
* Check if next instruction is:
* shr eax, 4
*/
if ( rc == VINF_SUCCESS
{
/* Replacing two instructions now. */
/* 0xF0, 0x0F, 0x20, 0xC0 = mov eax, cr8 */
abInstr[0] = 0xF0;
}
else
{
}
}
return VINF_SUCCESS;
}
/*
* Save invalid patch, so we will not try again.
*/
Log(("hmR3ReplaceTprInstr: Failed to patch instr!\n"));
return VINF_SUCCESS;
}
/**
* Callback to patch a TPR instruction (jump to generated code).
*
* @returns VBox strict status code.
* @param pVM Pointer to the VM.
* @param pVCpu The VMCPU for the EMT we're being called on.
* @param pvUser User specified CPU context.
*
*/
{
/*
* Only execute the handler on the VCPU the original patch request was
* issued. (The other CPU(s) might not yet have switched to protected
* mode, nor have the correct memory context.)
*/
return VINF_SUCCESS;
/*
* We're racing other VCPUs here, so don't try patch the instruction twice
* and make sure there is still room for our patch record.
*/
if (pPatch)
{
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
}
/*
* Disassemble the instruction and get cracking.
*/
if ( rc == VINF_SUCCESS
&& cbOp >= 5)
{
{
/*
* TPR write:
*
* push ECX [51]
* push EDX [52]
* push EAX [50]
* xor EDX,EDX [31 D2]
* mov EAX,EAX [89 C0]
* or
* mov EAX,0000000CCh [B8 CC 00 00 00]
* mov ECX,0C0000082h [B9 82 00 00 C0]
* wrmsr [0F 30]
* pop EAX [58]
* pop EDX [5A]
* pop ECX [59]
* jmp return_address [E9 return_address]
*
*/
bool fUsesEax = (pDis->Param2.fUse == DISUSE_REG_GEN32 && pDis->Param2.Base.idxGenReg == DISGREG_EAX);
if (!fUsesEax)
{
if (!fUsesEax)
{
}
}
else
{
}
if (!fUsesEax)
}
else
{
/*
* TPR read:
*
* push ECX [51]
* push EDX [52]
* push EAX [50]
* mov ECX,0C0000082h [B9 82 00 00 C0]
* rdmsr [0F 32]
* mov EAX,EAX [89 C0]
* pop EAX [58]
* pop EDX [5A]
* pop ECX [59]
* jmp return_address [E9 return_address]
*
*/
{
}
}
*(RTRCUINTPTR *)&aPatch[off] = ((RTRCUINTPTR)pCtx->eip + cbOp) - ((RTRCUINTPTR)pVM->hm.s.pFreeGuestPatchMem + off + 4);
off += sizeof(RTRCUINTPTR);
{
/* Write new code to the patch buffer. */
#ifdef LOG_ENABLED
{
char szOutput[256];
if (RT_SUCCESS(rc))
else
}
#endif
*(RTRCUINTPTR *)&pPatch->aNewOpcode[1] = ((RTRCUINTPTR)pVM->hm.s.pFreeGuestPatchMem) - ((RTRCUINTPTR)pCtx->eip + 5);
/* Overwrite the TPR instruction with a jump. */
return VINF_SUCCESS;
}
Log(("Ran out of space in our patch buffer!\n"));
}
else
Log(("hmR3PatchTprInstr: Failed to patch instr!\n"));
/*
* Save invalid patch, so we will not try again.
*/
return VINF_SUCCESS;
}
/**
* Attempt to patch TPR mmio instructions.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
* @param pCtx Pointer to the guest CPU context.
*/
{
return rc;
}
/**
* Force execution of the current IO code in the recompiler.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pCtx Partial VM execution context.
*/
{
Log(("HMR3EmulateIoBlock\n"));
/* This is primarily intended to speed up Grub, so we don't care about paged protected mode. */
if (HMCanEmulateIoBlockEx(pCtx))
{
Log(("HMR3EmulateIoBlock -> enabled\n"));
return VINF_EM_RESCHEDULE_REM;
}
return VINF_SUCCESS;
}
/**
* Checks if we can currently use hardware accelerated raw mode.
*
* @returns true if we can currently use hardware acceleration, otherwise false.
* @param pVM Pointer to the VM.
* @param pCtx Partial VM execution context.
*/
{
/* If we're still executing the IO code, then return false. */
return false;
/* AMD-V supports real & protected mode with or without paging. */
{
return true;
}
/* Note! The context supplied by REM is partial. If we add more checks here, be sure to verify that REM provides this info! */
{
/*
* The VMM device heap is a requirement for emulating real mode or protected mode without paging with the unrestricted
* guest execution feature i missing (VT-x only).
*/
if (fSupportsRealMode)
{
if (CPUMIsGuestInRealModeEx(pCtx))
{
/* In V86 mode (VT-x or not), the CPU enforces real-mode compatible selector
* bases and limits, i.e. limit must be 64K and base must be selector * 16.
* If this is not true, we cannot execute real mode as V86 and have to fall
* back to emulation.
*/
{
return false;
}
}
else
{
/* Verify the requirements for executing code in protected
mode. VT-x can't handle the CPU state right after a switch
from real to protected mode. (all sorts of RPL & DPL assumptions) */
&& enmGuestMode >= PGMMODE_PROTECTED)
{
{
return false;
}
}
/* VT-x also chokes on invalid tr or ldtr selectors (minix) */
{
return false;
}
}
}
else
{
if ( !CPUMIsGuestInLongModeEx(pCtx)
{
/** @todo This should (probably) be set on every excursion to the REM,
* however it's too risky right now. So, only apply it when we go
* back to REM for real mode execution. (The XP hack below doesn't
* work reliably without this.)
* Update: Implemented in EM.cpp, see #ifdef EM_NOTIFY_HM. */
if ( !pVM->hm.s.fNestedPaging /* requires a fake PD for real *and* protected mode without paging - stored in the VMM device heap */
|| CPUMIsGuestInRealModeEx(pCtx)) /* requires a fake TSS for real mode - stored in the VMM device heap */
return false;
/* Too early for VT-x; Solaris guests will fail with a guru meditation otherwise; same for XP. */
return false;
/* The guest is about to complete the switch to protected mode. Wait a bit longer. */
/* Windows XP; switch to protected mode; all selectors are marked not present in the
* hidden registers (possible recompiler bug; see load_seg_vm) */
return false;
return false;
/* Windows XP: possible same as above, but new recompiler requires new heuristics?
VT-x doesn't seem to like something about the guest state and this stuff avoids it. */
/** @todo This check is actually wrong, it doesn't take the direction of the
* stack segment into account. But, it does the job for now. */
return false;
#if 0
return false;
#endif
}
}
}
{
/* 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\hmr0.cpp for details. */
mask &= ~X86_CR0_NE;
if (fSupportsRealMode)
{
/* Note: We ignore the PE & PG bits here on purpose; we emulate real and protected mode without paging. */
}
else
{
/* We support protected mode without paging using identity mapping. */
mask &= ~X86_CR0_PG;
}
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;
}
return false;
}
/**
* Checks if we need to reschedule due to VMM device heap changes.
*
* @returns true if a reschedule is required, otherwise false.
* @param pVM Pointer to the VM.
* @param pCtx VM execution context.
*/
{
/*
* The VMM device heap is a requirement for emulating real mode or protected mode without paging
* when the unrestricted guest execution feature is missing (VT-x only).
*/
return true;
return false;
}
/**
* Notification from EM about a rescheduling into hardware assisted execution
* mode.
*
* @param pVCpu Pointer to the current VMCPU.
*/
{
}
/**
* Notification from EM about returning from instruction emulation (REM / EM).
*
* @param pVCpu Pointer to the VMCPU.
*/
{
}
/**
* Checks if we are currently using hardware accelerated raw mode.
*
* @returns true if hardware acceleration is being used, otherwise false.
* @param pVCpu Pointer to the VMCPU.
*/
{
}
/**
* Checks if we are currently using nested paging.
*
* @returns true if nested paging is being used, otherwise false.
* @param pVM Pointer to the VM.
*/
{
}
/**
* Checks if we are currently using VPID in VT-x mode.
*
* @returns true if VPID is being used, otherwise false.
* @param pVM Pointer to the VM.
*/
{
}
/**
* Checks if internal events are pending. In that case we are not allowed to dispatch interrupts.
*
* @returns true if an internal event is pending, otherwise false.
* @param pVM Pointer to the VM.
*/
{
}
/**
* Checks if the VMX-preemption timer is being used.
*
* @returns true if the VMX-preemption timer is being used, otherwise false.
* @param pVM Pointer to the VM.
*/
{
return HMIsEnabled(pVM)
}
/**
* Restart an I/O instruction that was refused in ring-0
*
* @returns Strict VBox status code. Informational status codes other than the one documented
* here are to be treated as internal failure. Use IOM_SUCCESS() to check for success.
* @retval VINF_SUCCESS Success.
* @retval VINF_EM_FIRST-VINF_EM_LAST Success with some exceptions (see IOM_SUCCESS()), the
* status code must be passed on to EM.
* @retval VERR_NOT_FOUND if no pending I/O instruction.
*
* @param pVM Pointer to the VM.
* @param pVCpu Pointer to the VMCPU.
* @param pCtx Pointer to the guest CPU context.
*/
{
|| enmType == HMPENDINGIO_INVALID)
return VERR_NOT_FOUND;
switch (enmType)
{
case HMPENDINGIO_PORT_READ:
{
&u32Val,
if (IOM_SUCCESS(rcStrict))
{
/* Write back to the EAX register. */
}
break;
}
case HMPENDINGIO_PORT_WRITE:
if (IOM_SUCCESS(rcStrict))
break;
default:
}
return rcStrict;
}
/**
* Inject an NMI into a running VM (only VCPU 0!)
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
*/
{
return VINF_SUCCESS;
}
/**
* log release message.
*
* @param pVM Pointer to the VM.
* @param iStatusCode VBox status code.
*/
{
{
switch (iStatusCode)
{
break;
LogRel(("VERR_VMX_INVALID_VMCS_PTR: CPU%d Current pointer %RGp vs %RGp\n", i, pVM->aCpus[i].hm.s.vmx.lasterror.u64VMCSPhys, pVM->aCpus[i].hm.s.vmx.HCPhysVMCS));
LogRel(("VERR_VMX_INVALID_VMCS_PTR: CPU%d Current VMCS version %x\n", i, pVM->aCpus[i].hm.s.vmx.lasterror.u32VMCSRevision));
LogRel(("VERR_VMX_INVALID_VMCS_PTR: CPU%d Entered Cpu %d\n", i, pVM->aCpus[i].hm.s.vmx.lasterror.idEnteredCpu));
LogRel(("VERR_VMX_INVALID_VMCS_PTR: CPU%d Current Cpu %d\n", i, pVM->aCpus[i].hm.s.vmx.lasterror.idCurrentCpu));
break;
LogRel(("VERR_VMX_UNABLE_TO_START_VM: CPU%d instruction error %x\n", i, pVM->aCpus[i].hm.s.vmx.lasterror.u32InstrError));
LogRel(("VERR_VMX_UNABLE_TO_START_VM: CPU%d exit reason %x\n", i, pVM->aCpus[i].hm.s.vmx.lasterror.u32ExitReason));
{
LogRel(("VERR_VMX_UNABLE_TO_START_VM: Cpu%d MSRBitmapPhys %RHp\n", i, pVM->aCpus[i].hm.s.vmx.HCPhysMsrBitmap));
LogRel(("VERR_VMX_UNABLE_TO_START_VM: Cpu%d GuestMSRPhys %RHp\n", i, pVM->aCpus[i].hm.s.vmx.HCPhysGuestMsr));
LogRel(("VERR_VMX_UNABLE_TO_START_VM: Cpu%d HostMsrPhys %RHp\n", i, pVM->aCpus[i].hm.s.vmx.HCPhysHostMsr));
LogRel(("VERR_VMX_UNABLE_TO_START_VM: Cpu%d cGuestMSRs %x\n", i, pVM->aCpus[i].hm.s.vmx.cGuestMsrs));
#endif
}
/** @todo Log VM-entry event injection control fields
* VMX_VMCS_CTRL_ENTRY_IRQ_INFO, VMX_VMCS_CTRL_ENTRY_EXCEPTION_ERRCODE
* and VMX_VMCS_CTRL_ENTRY_INSTR_LENGTH from the VMCS. */
break;
LogRel(("VERR_VMX_UNABLE_TO_RESUME_VM: CPU%d instruction error %x\n", i, pVM->aCpus[i].hm.s.vmx.lasterror.u32InstrError));
LogRel(("VERR_VMX_UNABLE_TO_RESUME_VM: CPU%d exit reason %x\n", i, pVM->aCpus[i].hm.s.vmx.lasterror.u32ExitReason));
break;
break;
}
}
{
LogRel(("VERR_VMX_UNABLE_TO_START_VM: VM-entry allowed %x\n", pVM->hm.s.vmx.msr.vmx_entry.n.allowed1));
LogRel(("VERR_VMX_UNABLE_TO_START_VM: VM-entry disallowed %x\n", pVM->hm.s.vmx.msr.vmx_entry.n.disallowed0));
}
}
/**
* Execute state save operation.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pSSM SSM operation handle.
*/
{
int rc;
Log(("hmR3Save:\n"));
{
/*
* Save the basic bits - fortunately all the other things can be resynced on load.
*/
}
#ifdef VBOX_HM_WITH_GUEST_PATCHING
/* Store all the guest patch records too. */
{
}
#endif
return VINF_SUCCESS;
}
/**
* Execute state load operation.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pSSM SSM operation handle.
* @param uVersion Data layout version.
* @param uPass The data pass.
*/
{
int rc;
Log(("hmR3Load:\n"));
/*
* Validate version.
*/
if ( uVersion != HM_SSM_VERSION
&& uVersion != HM_SSM_VERSION_2_0_X)
{
}
{
if (uVersion >= HM_SSM_VERSION_NO_PATCHING)
{
}
}
#ifdef VBOX_HM_WITH_GUEST_PATCHING
{
/* Fetch all TPR patch records. */
{
Log(("hmR3Load: patch %d\n", i));
}
}
#endif
/* Recheck all VCPUs if we can go straight into hm execution mode. */
if (HMIsEnabled(pVM))
{
{
}
}
return VINF_SUCCESS;
}