HWACCMR0.cpp revision e73868a8e8291d31c5bd6bcf0fbd8037310f3e31
/* $Id$ */
/** @file
* HWACCM - Host Context Ring 0.
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* 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.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_HWACCM
#include "HWACCMInternal.h"
#include <VBox/hwacc_vmx.h>
#include <VBox/hwacc_svm.h>
#include "HWVMXR0.h"
#include "HWSVMR0.h"
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/*******************************************************************************
* Global Variables *
*******************************************************************************/
static struct
{
/** Ring 0 handlers for VT-x and AMD-V. */
DECLR0CALLBACKMEMBER(int, pfnEnableCpu, (PHWACCM_CPUINFO pCpu, PVM pVM, void *pvPageCpu, RTHCPHYS pPageCpuPhys));
DECLR0CALLBACKMEMBER(int, pfnDisableCpu, (PHWACCM_CPUINFO pCpu, void *pvPageCpu, RTHCPHYS pPageCpuPhys));
/** Maximum ASID allowed. */
struct
{
/** Set by the ring-0 driver to indicate VMX is supported by the CPU. */
bool fSupported;
/** Host CR4 value (set by ring-0 VMX init) */
/** VMX MSR values */
struct
{
} msr;
/* Last instruction error */
} vmx;
struct
{
/** Set by the ring-0 driver to indicate SVM is supported by the CPU. */
bool fSupported;
/** SVM revision. */
/** SVM feature bits from cpuid 0x8000000a */
} svm;
/** Saved error from detection */
struct
{
} cpuid;
volatile bool fSuspended;
/**
* Does global Ring-0 HWACCM initialization.
*
* @returns VBox status code.
*/
VMMR0DECL(int) HWACCMR0Init(void)
{
int rc;
/* Fill in all callbacks with placeholders. */
#ifndef VBOX_WITH_HYBIRD_32BIT_KERNEL /* paranoia */
/*
* Check for VT-x and AMD-V capabilities
*/
if (ASMHasCpuId())
{
/* Query AMD features. */
ASMCpuId(0x80000001, &u32Dummy, &u32Dummy, &HWACCMR0Globals.cpuid.u32AMDFeatureECX, &HWACCMR0Globals.cpuid.u32AMDFeatureEDX);
)
{
/*
*/
if ( (u32FeaturesECX & X86_CPUID_FEATURE_ECX_VMX)
)
{
int aRc[RTCPUSET_MAX_CPUS];
/* We need to check if VT-x has been properly initialized on all CPUs. Some BIOSes do a lousy job. */
/* Check the return code of all invocations. */
{
/* Reread in case we've changed it. */
if ( (HWACCMR0Globals.vmx.msr.feature_ctrl & (MSR_IA32_FEATURE_CONTROL_VMXON|MSR_IA32_FEATURE_CONTROL_LOCK))
{
void *pvScatchPage;
/* VPID 16 bits ASID. */
if (HWACCMR0Globals.vmx.msr.vmx_proc_ctls.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC_USE_SECONDARY_EXEC_CTRL)
{
if (HWACCMR0Globals.vmx.msr.vmx_proc_ctls2.n.allowed1 & (VMX_VMCS_CTRL_PROC_EXEC2_EPT|VMX_VMCS_CTRL_PROC_EXEC2_VPID))
}
if (RT_FAILURE(rc))
return rc;
/* Set revision dword at the beginning of the structure. */
*(uint32_t *)pvScatchPage = MSR_IA32_VMX_BASIC_INFO_VMCS_ID(HWACCMR0Globals.vmx.msr.vmx_basic_info);
/* Make sure we don't get rescheduled to another cpu during this probe. */
/*
* Check CR4.VMXE
*/
{
/* In theory this bit could be cleared behind our back. Which would cause #UD faults when we
* try to execute the VMX instructions...
*/
}
/* Enter VMX Root Mode */
if (RT_FAILURE(rc))
{
/* KVM leaves the CPU in VMX root mode. Not only is this not allowed, it will crash the host when we enter raw mode, because
* (a) clearing X86_CR4_VMXE in CR4 causes a #GP (we no longer modify this bit)
* (b) turning off paging causes a #GP (unavoidable when switching from long to 32 bits mode or 32 bits to PAE)
*
* They should fix their code, but until they do we simply refuse to run.
*/
}
else
{
VMXDisable();
}
/* Restore CR4 again; don't leave the X86_CR4_VMXE flag set if it wasn't so before (some software could incorrectly think it's in VMX mode) */
RTR0MemObjFree(pScatchMemObj, false);
return HWACCMR0Globals.lLastError;
}
else
{
AssertFailed(); /* can't hit this case anymore */
}
}
#ifdef LOG_ENABLED
else
#endif
}
else
}
else
if ( u32VendorEBX == X86_CPUID_VENDOR_AMD_EBX
)
{
/*
*/
)
{
int aRc[RTCPUSET_MAX_CPUS];
/* We need to check if AMD-V has been properly initialized on all CPUs. Some BIOSes might do a poor job. */
/* Check the return code of all invocations. */
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
{
/* Query AMD features. */
ASMCpuId(0x8000000A, &HWACCMR0Globals.svm.u32Rev, &HWACCMR0Globals.uMaxASID, &u32Dummy, &HWACCMR0Globals.svm.u32Features);
}
else
}
else
}
else
}
else
#endif /* !VBOX_WITH_HYBIRD_32BIT_KERNEL */
{
}
else
{
}
return VINF_SUCCESS;
}
/**
* Checks the error code array filled in for each cpu in the system.
*
* @returns VBox status code.
* @param paRc Error code array
* @param cErrorCodes Array size
* @param pidCpu Value of the first cpu that set an error (out)
*/
{
int rc = VINF_SUCCESS;
for (unsigned i=0;i<cErrorCodes;i++)
{
if (RTMpIsCpuOnline(i))
{
if (RT_FAILURE(paRc[i]))
{
*pidCpu = i;
break;
}
}
}
return rc;
}
/**
* Does global Ring-0 HWACCM termination.
*
* @returns VBox status code.
*/
VMMR0DECL(int) HWACCMR0Term(void)
{
int aRc[RTCPUSET_MAX_CPUS];
int rc;
/* Free the per-cpu pages used for VT-x and AMD-V */
{
{
}
}
return rc;
}
/**
* Worker function passed to RTMpOnAll, RTMpOnOthers and RTMpOnSpecific that
* is to be called on the target cpus.
*
* @param idCpu The identifier for the CPU the function is called on.
* @param pvUser1 The 1st user argument.
* @param pvUser2 The 2nd user argument.
*/
{
#ifdef LOG_ENABLED
#endif
Assert(idCpu == (RTCPUID)RTMpCpuIdToSetIndex(idCpu)); /// @todo fix idCpu == index assumption (rainy day)
{
/*
* Both the LOCK and VMXON bit must be set; otherwise VMXON will generate a #GP.
* Once the lock bit is set, this MSR can no longer be modified.
*/
{
/* MSR is not yet locked; we can change it ourselves here */
ASMWrMsr(MSR_IA32_FEATURE_CONTROL, HWACCMR0Globals.vmx.msr.feature_ctrl | MSR_IA32_FEATURE_CONTROL_VMXON | MSR_IA32_FEATURE_CONTROL_LOCK);
}
else
}
else
if (u32VendorEBX == X86_CPUID_VENDOR_AMD_EBX)
{
/* Check if SVM is disabled */
if (!(val & MSR_K8_VM_CR_SVM_DISABLE))
{
/* Turn on SVM in the EFER MSR. */
if (!(val & MSR_K6_EFER_SVME))
/* Paranoia. */
if (val & MSR_K6_EFER_SVME)
else
}
else
}
else
AssertFailed(); /* can't happen */
return;
}
/**
* Sets up HWACCM on all cpus.
*
* @returns VBox status code.
* @param pVM The VM to operate on.
* @param enmNewHwAccmState New hwaccm state
*
*/
{
/* Make sure we don't touch hwaccm after we've disabled hwaccm in preparation of a suspend. */
return VERR_HWACCM_SUSPEND_PENDING;
if (ASMAtomicCmpXchgU32((volatile uint32_t *)&HWACCMR0Globals.enmHwAccmState, enmNewHwAccmState, HWACCMSTATE_UNINITIALIZED))
{
int aRc[RTCPUSET_MAX_CPUS];
/* Don't setup hwaccm as that might not work (vt-x & 64 bits raw mode) */
return VINF_SUCCESS;
/* Allocate one page per cpu for the global vt-x and amd-v pages */
{
/** @todo this is rather dangerous if cpus can be taken offline; we don't care for now */
if (RTMpIsCpuOnline(i))
{
int rc = RTR0MemObjAllocCont(&HWACCMR0Globals.aCpuInfo[i].pMemObj, 1 << PAGE_SHIFT, true /* executable R0 mapping */);
if (RT_FAILURE(rc))
return rc;
#ifdef LOG_ENABLED
SUPR0Printf("address %x phys %x\n", pvR0, (uint32_t)RTR0MemObjGetPagePhysAddr(HWACCMR0Globals.aCpuInfo[i].pMemObj, 0));
#endif
}
}
/* Check the return code of all invocations. */
if (RT_SUCCESS(rc))
return rc;
}
return VINF_SUCCESS;
/* Request to change the mode is not allowed */
return VERR_ACCESS_DENIED;
}
/**
* Worker function passed to RTMpOnAll, RTMpOnOthers and RTMpOnSpecific that
* is to be called on the target cpus.
*
* @param idCpu The identifier for the CPU the function is called on.
* @param pvUser1 The 1st user argument.
* @param pvUser2 The 2nd user argument.
*/
{
void *pvPageCpu;
Assert(idCpu == (RTCPUID)RTMpCpuIdToSetIndex(idCpu)); /// @todo fix idCpu == index assumption (rainy day)
/* Make sure we start with a clean TLB. */
pCpu->cTLBFlushes = 0;
/* Should never happen */
{
AssertFailed();
return;
}
pCpu->fConfigured = true;
return;
}
/**
* Worker function passed to RTMpOnAll, RTMpOnOthers and RTMpOnSpecific that
* is to be called on the target cpus.
*
* @param idCpu The identifier for the CPU the function is called on.
* @param pvUser1 The 1st user argument.
* @param pvUser2 The 2nd user argument.
*/
{
void *pvPageCpu;
Assert(idCpu == (RTCPUID)RTMpCpuIdToSetIndex(idCpu)); /// @todo fix idCpu == index assumption (rainy day)
return;
if (pCpu->fConfigured)
{
pCpu->fConfigured = false;
}
else
pCpu->uCurrentASID = 0;
return;
}
/**
* Called whenever a system power state change occurs.
*
* @param enmEvent Power event
* @param pvUser User argument
*/
{
#ifdef LOG_ENABLED
if (enmEvent == RTPOWEREVENT_SUSPEND)
SUPR0Printf("hwaccmR0PowerCallback RTPOWEREVENT_SUSPEND\n");
else
SUPR0Printf("hwaccmR0PowerCallback RTPOWEREVENT_RESUME\n");
#endif
if (enmEvent == RTPOWEREVENT_SUSPEND)
{
int aRc[RTCPUSET_MAX_CPUS];
int rc;
if (enmEvent == RTPOWEREVENT_SUSPEND)
{
/* Turn off VT-x or AMD-V on all CPUs. */
}
else
{
/* Reinit the CPUs from scratch as the suspend state has messed with the MSRs. */
rc = RTMpOnAll(HWACCMR0InitCPU, (void *)((HWACCMR0Globals.vmx.fSupported) ? X86_CPUID_VENDOR_INTEL_EBX : X86_CPUID_VENDOR_AMD_EBX), aRc);
if (RT_SUCCESS(rc))
#ifdef LOG_ENABLED
if (RT_FAILURE(rc))
#endif
/* Turn VT-x or AMD-V back on on all CPUs. */
}
}
if (enmEvent == RTPOWEREVENT_RESUME)
}
/**
* Does Ring-0 per VM HWACCM initialization.
*
* This is mainly to check that the Host CPU mode is compatible
* with VMX.
*
* @returns VBox status code.
* @param pVM The VM to operate on.
*/
{
int rc;
#ifdef LOG_ENABLED
#endif
/* Make sure we don't touch hwaccm after we've disabled hwaccm in preparation of a suspend. */
return VERR_HWACCM_SUSPEND_PENDING;
#ifdef VBOX_STRICT
#endif
/* Invalidate the last cpu we were running on. */
/* we'll aways increment this the first time (host uses ASID 0) */
/* Init a VT-x or AMD-V VM. */
return rc;
}
/**
* Does Ring-0 per VM HWACCM termination.
*
* @returns VBox status code.
* @param pVM The VM to operate on.
*/
{
int rc;
#ifdef LOG_ENABLED
#endif
/* Make sure we don't touch hwaccm after we've disabled hwaccm in preparation of a suspend. */
/* Terminate a VT-x or AMD-V VM. */
return rc;
}
/**
* Sets up a VT-x or AMD-V session
*
* @returns VBox status code.
* @param pVM The VM to operate on.
*/
{
int rc;
/* Make sure we don't touch hwaccm after we've disabled hwaccm in preparation of a suspend. */
#ifdef LOG_ENABLED
#endif
/* Setup VT-x or AMD-V. */
return rc;
}
/**
* Enters the VT-x or AMD-V session
*
* @returns VBox status code.
* @param pVM The VM to operate on.
* @param idVCpu VMCPUD id.
*/
{
int rc;
/* Make sure we can't enter a session after we've disabled hwaccm in preparation of a suspend. */
/* Always load the guest's debug state on-demand. */
/* Always reload the host context and the guest's CR0 register. (!!!!) */
/* Setup the register and mask according to the current execution mode. */
else
/* We must save the host context here (VT-x) as we might be rescheduled on a different cpu after a long jump back to ring 3. */
#ifdef VBOX_STRICT
/* keep track of the CPU owning the VMCS for debugging scheduling weirdness and ring-3 calls. */
if (RT_SUCCESS(rc))
{
}
#endif
return rc;
}
/**
* Leaves the VT-x or AMD-V session
*
* @returns VBox status code.
* @param pVM The VM to operate on.
* @param idVCpu VMCPUD id.
*/
{
int rc;
/* Note: It's rather tricky with longjmps done by e.g. Log statements or the page fault handler.
* We must restore the host FPU here to make absolutely sure we don't leave the guest FPU state active
* or trash somebody else's FPU state.
*/
/* Save the guest FPU and XMM state if necessary. */
if (CPUMIsGuestFPUStateActive(pVM))
{
Log2(("CPUMR0SaveGuestFPU\n"));
}
#ifdef VBOX_STRICT
/* keep track of the CPU owning the VMCS for debugging scheduling weirdness and ring-3 calls. */
AssertMsg(pVM->hwaccm.s.idEnteredCpu == idCpu, ("owner is %d, I'm %d", (int)pVM->hwaccm.s.idEnteredCpu, (int)idCpu));
#endif
return rc;
}
/**
* Runs guest code in a hardware accelerated VM.
*
* @returns VBox status code.
* @param pVM The VM to operate on.
* @param idVCpu VMCPUD id.
*/
{
int rc;
#ifdef VBOX_STRICT
#endif
}
/**
* Returns suspend status of the host
*
* @returns Suspend pending or not
*/
VMMR0DECL(bool) HWACCMR0SuspendPending()
{
}
/**
* Returns the cpu structure for the current cpu.
* Keep in mind that there is no guarantee it will stay the same (long jumps to ring 3!!!).
*
* @returns cpu structure pointer
* @param pVM The VM to operate on.
*/
{
}
#ifdef VBOX_STRICT
/**
* Dumps a descriptor.
*
* @param pDesc Descriptor to dump.
* @param Sel Selector number.
* @param pszMsg Message to prepend the log entry with.
*/
{
/*
* Make variable description string.
*/
static struct
{
unsigned cch;
const char *psz;
} const aTypes[32] =
{
/* system */
# if HC_ARCH_BITS == 64
# else
# endif
/* non system */
};
char szMsg[128];
else
# if HC_ARCH_BITS == 64
else
# else
else
# endif
*psz = '\0';
/*
* Limit and Base and format the output.
*/
# if HC_ARCH_BITS == 64
# else
# endif
}
/**
* Formats a full register dump.
*
* @param pVM The VM to operate on.
* @param pCtx The context to format.
*/
{
/*
* Format the flags.
*/
static struct
{
} aFlags[] =
{
};
char szEFlags[80];
for (unsigned i = 0; i < RT_ELEMENTS(aFlags); i++)
{
if (pszAdd)
{
*psz++ = ' ';
}
}
/*
* Format the registers.
*/
{
Log(("rax=%016RX64 rbx=%016RX64 rcx=%016RX64 rdx=%016RX64\n"
"rsi=%016RX64 rdi=%016RX64 r8 =%016RX64 r9 =%016RX64\n"
"r10=%016RX64 r11=%016RX64 r12=%016RX64 r13=%016RX64\n"
"r14=%016RX64 r15=%016RX64\n"
"rip=%016RX64 rsp=%016RX64 rbp=%016RX64 iopl=%d %*s\n"
"cs={%04x base=%016RX64 limit=%08x flags=%08x}\n"
"ds={%04x base=%016RX64 limit=%08x flags=%08x}\n"
"es={%04x base=%016RX64 limit=%08x flags=%08x}\n"
"fs={%04x base=%016RX64 limit=%08x flags=%08x}\n"
"gs={%04x base=%016RX64 limit=%08x flags=%08x}\n"
"ss={%04x base=%016RX64 limit=%08x flags=%08x}\n"
"cr0=%016RX64 cr2=%016RX64 cr3=%016RX64 cr4=%016RX64\n"
"dr0=%016RX64 dr1=%016RX64 dr2=%016RX64 dr3=%016RX64\n"
"dr4=%016RX64 dr5=%016RX64 dr6=%016RX64 dr7=%016RX64\n"
"gdtr=%016RX64:%04x idtr=%016RX64:%04x eflags=%08x\n"
"ldtr={%04x base=%08RX64 limit=%08x flags=%08x}\n"
"tr ={%04x base=%08RX64 limit=%08x flags=%08x}\n"
"SysEnter={cs=%04llx eip=%08llx esp=%08llx}\n"
,
}
else
Log(("eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n"
"eip=%08x esp=%08x ebp=%08x iopl=%d %*s\n"
"cs={%04x base=%016RX64 limit=%08x flags=%08x} dr0=%08RX64 dr1=%08RX64\n"
"ds={%04x base=%016RX64 limit=%08x flags=%08x} dr2=%08RX64 dr3=%08RX64\n"
"es={%04x base=%016RX64 limit=%08x flags=%08x} dr4=%08RX64 dr5=%08RX64\n"
"fs={%04x base=%016RX64 limit=%08x flags=%08x} dr6=%08RX64 dr7=%08RX64\n"
"gs={%04x base=%016RX64 limit=%08x flags=%08x} cr0=%08RX64 cr2=%08RX64\n"
"ss={%04x base=%016RX64 limit=%08x flags=%08x} cr3=%08RX64 cr4=%08RX64\n"
"gdtr=%016RX64:%04x idtr=%016RX64:%04x eflags=%08x\n"
"ldtr={%04x base=%08RX64 limit=%08x flags=%08x}\n"
"tr ={%04x base=%08RX64 limit=%08x flags=%08x}\n"
"SysEnter={cs=%04llx eip=%08llx esp=%08llx}\n"
,
(RTSEL)pCtx->cs, pCtx->csHid.u64Base, pCtx->csHid.u32Limit, pCtx->csHid.Attr.u, pCtx->dr[0], pCtx->dr[1],
(RTSEL)pCtx->ds, pCtx->dsHid.u64Base, pCtx->dsHid.u32Limit, pCtx->dsHid.Attr.u, pCtx->dr[2], pCtx->dr[3],
(RTSEL)pCtx->es, pCtx->esHid.u64Base, pCtx->esHid.u32Limit, pCtx->esHid.Attr.u, pCtx->dr[4], pCtx->dr[5],
(RTSEL)pCtx->fs, pCtx->fsHid.u64Base, pCtx->fsHid.u32Limit, pCtx->fsHid.Attr.u, pCtx->dr[6], pCtx->dr[7],
(RTSEL)pCtx->gs, pCtx->gsHid.u64Base, pCtx->gsHid.u32Limit, pCtx->gsHid.Attr.u, pCtx->cr0, pCtx->cr2,
(RTSEL)pCtx->ss, pCtx->ssHid.u64Base, pCtx->ssHid.u32Limit, pCtx->ssHid.Attr.u, pCtx->cr3, pCtx->cr4,
Log(("FPU:\n"
"FCW=%04x FSW=%04x FTW=%02x\n"
"res1=%02x FOP=%04x FPUIP=%08x CS=%04x Rsvrd1=%04x\n"
"FPUDP=%04x DS=%04x Rsvrd2=%04x MXCSR=%08x MXCSR_MASK=%08x\n"
,
Log(("MSR:\n"
"EFER =%016RX64\n"
"PAT =%016RX64\n"
"STAR =%016RX64\n"
"CSTAR =%016RX64\n"
"LSTAR =%016RX64\n"
"SFMASK =%016RX64\n"
"KERNELGSBASE =%016RX64\n",
pCtx->msrKERNELGSBASE));
}
#endif /* VBOX_STRICT */
/* Dummy callback handlers. */
{
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
}
VMMR0DECL(int) HWACCMR0DummyEnableCpu(PHWACCM_CPUINFO pCpu, PVM pVM, void *pvPageCpu, RTHCPHYS pPageCpuPhys)
{
return VINF_SUCCESS;
}
VMMR0DECL(int) HWACCMR0DummyDisableCpu(PHWACCM_CPUINFO pCpu, void *pvPageCpu, RTHCPHYS pPageCpuPhys)
{
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
}