HWACCMR0.cpp revision cbeafe5b35cc2bf5ab239f08d3a7b53b31ef191d
/* $Id$ */
/** @file
* HWACCM - Host Context Ring 0.
*/
/*
* Copyright (C) 2006-2007 innotek 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 (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_HWACCM
#include "HWACCMInternal.h"
#include <VBox/hwacc_vmx.h>
#include <VBox/hwacc_svm.h>
#include "HWVMXR0.h"
#include "HWSVMR0.h"
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/*******************************************************************************
* Local Variables *
*******************************************************************************/
static struct
{
struct
{
bool fVMXConfigured;
bool fSVMConfigured;
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. */
/** Maximum ASID allowed. */
} svm;
/** Saved error from detection */
struct
{
} cpuid;
/**
* Does global Ring-0 HWACCM initialization.
*
* @returns VBox status code.
*/
HWACCMR0DECL(int) HWACCMR0Init()
{
int rc;
void *pvScatchPage;
if (RT_FAILURE(rc))
return rc;
/* Assume success */
rc = VINF_SUCCESS;
#ifndef VBOX_WITH_HYBIRD_32BIT_KERNEL /* paranoia */
/*
* Check for VT-x and AMD-V capabilities
*/
if (ASMHasCpuId())
{
/* Make sure we don't get rescheduled to another cpu during this probe. */
/* Query AMD features. */
ASMCpuId(0x80000001, &u32Dummy, &u32Dummy, &HWACCMR0Globals.cpuid.u32AMDFeatureECX, &HWACCMR0Globals.cpuid.u32AMDFeatureEDX);
)
{
/*
*/
if ( (u32FeaturesECX & X86_CPUID_FEATURE_ECX_VMX)
)
{
/*
* 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.
*/
if (!(HWACCMR0Globals.vmx.msr.feature_ctrl & (MSR_IA32_FEATURE_CONTROL_VMXON|MSR_IA32_FEATURE_CONTROL_LOCK)))
{
/* MSR is not yet locked; we can change it ourselves here */
HWACCMR0Globals.vmx.msr.feature_ctrl |= (MSR_IA32_FEATURE_CONTROL_VMXON|MSR_IA32_FEATURE_CONTROL_LOCK);
}
if ( (HWACCMR0Globals.vmx.msr.feature_ctrl & (MSR_IA32_FEATURE_CONTROL_VMXON|MSR_IA32_FEATURE_CONTROL_LOCK))
{
/*
* 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...
*/
}
/* 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);
#if HC_ARCH_BITS == 64
/* Enter VMX Root Mode */
if (VBOX_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)
*
* They should fix their code, but until they do we simply refuse to run.
*/
}
else
VMXDisable();
#endif
/* 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) */
}
else
}
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)
{
/* Query AMD features. */
ASMCpuId(0x8000000A, &HWACCMR0Globals.svm.u32Rev, &HWACCMR0Globals.svm.u32MaxASID, &u32Dummy, &u32Dummy);
}
else
{
AssertFailed();
}
}
else
}
else
}
else
}
else
#endif /* !VBOX_WITH_HYBIRD_32BIT_KERNEL */
RTR0MemObjFree(pScatchMemObj, false);
return rc;
}
/**
* Does global Ring-0 HWACCM termination.
*
* @returns VBox status code.
*/
HWACCMR0DECL(int) HWACCMR0Term()
{
int aRc[RTCPUSET_MAX_CPUS];
/* Free the per-cpu pages used for VT-x and AMD-V */
{
{
}
}
return rc;
}
/**
* Sets up HWACCM on all cpus.
*
* @returns VBox status code.
* @param pVM The VM to operate on.
* @param enmNewHwAccmState New hwaccm state
*
*/
{
if (ASMAtomicCmpXchgU32((volatile uint32_t *)&HWACCMR0Globals.enmHwAccmState, enmNewHwAccmState, HWACCMSTATE_UNINITIALIZED))
{
int aRc[RTCPUSET_MAX_CPUS];
/* 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;
}
}
if (VBOX_SUCCESS(rc))
{
for (unsigned i=0;i<RT_ELEMENTS(aRc);i++)
{
if (RTMpIsCpuOnline(i))
{
if (VBOX_FAILURE(aRc[i]))
{
break;
}
}
}
}
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;
/* Should never happen */
{
AssertFailed();
return;
}
{
}
else
{
}
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;
/* Should never happen */
{
AssertFailed();
return;
}
{
}
else
{
}
return;
}
/**
* 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.
*/
{
{
}
else
{
}
return VINF_SUCCESS;
}
/**
* Sets up a VT-x or AMD-V session
*
* @returns VBox status code.
* @param pVM The VM to operate on.
*/
{
int rc = VINF_SUCCESS;
return VERR_INVALID_PARAMETER;
/* Setup Intel VMX. */
else
return rc;
}
/**
* Enters the VT-x or AMD-V session
*
* @returns VBox status code.
* @param pVM The VM to operate on.
*/
{
int rc;
if (VBOX_FAILURE(rc))
return rc;
/* Always reload the host context and the guest's CR0 register. (!!!!) */
{
if (rc != VINF_SUCCESS)
return rc;
}
else
{
if (rc != VINF_SUCCESS)
return rc;
}
return VINF_SUCCESS;
}
/**
* Leaves the VT-x or AMD-V session
*
* @returns VBox status code.
* @param pVM The VM to operate on.
*/
{
int rc;
if (VBOX_FAILURE(rc))
return 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.
*/
/* Restore host FPU and XMM state if necessary. */
if (CPUMIsGuestFPUStateActive(pVM))
{
Log2(("CPUMRestoreHostFPUState\n"));
/** @note CPUMRestoreHostFPUState keeps the current CR0 intact. */
}
{
return VMXR0Leave(pVM);
}
else
{
return SVMR0Leave(pVM);
}
}
/**
* Runs guest code in a hardware accelerated VM.
*
* @returns VBox status code.
* @param pVM The VM to operate on.
*/
{
int rc;
if (VBOX_FAILURE(rc))
return rc;
{
}
else
{
}
}
#ifdef VBOX_STRICT
/**
* Dumps a descriptor.
*
* @param Desc 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
uint64_t u32Base = ((uintptr_t)Desc->Gen.u32BaseHigh3 << 32ULL) | Desc->Gen.u8BaseHigh2 << 24ULL | Desc->Gen.u8BaseHigh1 << 16ULL | Desc->Gen.u16BaseLow;
#else
uint32_t u32Base = Desc->Gen.u8BaseHigh2 << 24 | Desc->Gen.u8BaseHigh1 << 16 | Desc->Gen.u16BaseLow;
#endif
}
/**
* Formats a full register dump.
*
* @param pCtx The context to format.
*/
{
/*
* Format the flags.
*/
static struct
{
} aFlags[] =
{
};
char szEFlags[80];
{
if (pszAdd)
{
*psz++ = ' ';
}
}
/*
* Format the registers.
*/
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=%08x limit=%08x flags=%08x} dr0=%08RX64 dr1=%08RX64\n"
"ds={%04x base=%08x limit=%08x flags=%08x} dr2=%08RX64 dr3=%08RX64\n"
"es={%04x base=%08x limit=%08x flags=%08x} dr4=%08RX64 dr5=%08RX64\n"
"fs={%04x base=%08x limit=%08x flags=%08x} dr6=%08RX64 dr7=%08RX64\n"
,
(RTSEL)pCtx->cs, pCtx->csHid.u32Base, pCtx->csHid.u32Limit, pCtx->csHid.Attr.u, pCtx->dr0, pCtx->dr1,
(RTSEL)pCtx->ds, pCtx->dsHid.u32Base, pCtx->dsHid.u32Limit, pCtx->dsHid.Attr.u, pCtx->dr2, pCtx->dr3,
(RTSEL)pCtx->es, pCtx->esHid.u32Base, pCtx->esHid.u32Limit, pCtx->esHid.Attr.u, pCtx->dr4, pCtx->dr5,
(RTSEL)pCtx->fs, pCtx->fsHid.u32Base, pCtx->fsHid.u32Limit, pCtx->fsHid.Attr.u, pCtx->dr6, pCtx->dr7));
Log(("gs={%04x base=%08x limit=%08x flags=%08x} cr0=%08RX64 cr2=%08RX64\n"
"ss={%04x base=%08x limit=%08x flags=%08x} cr3=%08RX64 cr4=%08RX64\n"
"gdtr=%08x:%04x idtr=%08x:%04x eflags=%08x\n"
"ldtr={%04x base=%08x limit=%08x flags=%08x}\n"
"tr ={%04x base=%08x limit=%08x flags=%08x}\n"
"SysEnter={cs=%04llx eip=%08llx esp=%08llx}\n"
"FCW=%04x FSW=%04x FTW=%04x\n",
(RTSEL)pCtx->gs, pCtx->gsHid.u32Base, pCtx->gsHid.u32Limit, pCtx->gsHid.Attr.u, pCtx->cr0, pCtx->cr2,
(RTSEL)pCtx->ss, pCtx->ssHid.u32Base, pCtx->ssHid.u32Limit, pCtx->ssHid.Attr.u, pCtx->cr3, pCtx->cr4,
}
#endif