VMMR0.cpp revision 3dc55354143de06ead357136a00f175dada2e549
/* $Id$ */
/** @file
* VMM - 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_VMM
#include "VMMInternal.h"
# pragma intrinsic(_AddressOfReturnAddress)
#endif
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
VMMR0DECL(int) ModuleInit(void);
VMMR0DECL(void) ModuleTerm(void);
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** Pointer to the internal networking service instance. */
/**
* Initialize the module.
* This is called when we're first loaded.
*
* @returns 0 on success.
* @returns VBox status on failure.
*/
VMMR0DECL(int) ModuleInit(void)
{
LogFlow(("ModuleInit:\n"));
/*
* Initialize the GVMM, GMM.& HWACCM
*/
int rc = GVMMR0Init();
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
rc = HWACCMR0Init();
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
}
}
}
return rc;
}
/**
* Terminate the module.
* This is called when we're finally unloaded.
*/
VMMR0DECL(void) ModuleTerm(void)
{
LogFlow(("ModuleTerm:\n"));
/*
* Destroy the internal networking instance.
*/
if (g_pIntNet)
{
}
/* Global HWACCM cleanup */
HWACCMR0Term();
/*
* Destroy the GMM and GVMM instances.
*/
GMMR0Term();
GVMMR0Term();
LogFlow(("ModuleTerm: returns\n"));
}
/**
* Initaties the R0 driver for a particular VM instance.
*
* @returns VBox status code.
*
* @param pVM The VM instance in question.
* @param uSvnRev The SVN revision of the ring-3 part.
* @thread EMT.
*/
{
/*
* Match the SVN revisions.
*/
if (uSvnRev != VMMGetSvnRev())
return VERR_VERSION_MISMATCH;
return VERR_INVALID_PARAMETER;
/*
* Register the EMT R0 logger instance.
*/
if (pR0Logger)
{
#if 0 /* testing of the logger. */
LogCom(("VMMR0Init: offScratch=%d fFlags=%#x fDestFlags=%#x\n", pR0Logger->Logger.offScratch, pR0Logger->Logger.fFlags, pR0Logger->Logger.fDestFlags));
LogCom(("VMMR0Init: returned succesfully from direct logger call.\n"));
LogCom(("VMMR0Init: returned succesfully from direct flush call.\n"));
LogCom(("VMMR0Init: returned succesfully from direct logger call (2). offScratch=%d\n", pR0Logger->Logger.offScratch));
RTLogPrintf("hello ring-0 logger (RTLogPrintf)\n");
#endif
}
/*
* Initialize the per VM data for GVMM and GMM.
*/
// if (RT_SUCCESS(rc))
// rc = GMMR0InitPerVMData(pVM);
if (RT_SUCCESS(rc))
{
/*
* Init HWACCM.
*/
if (RT_SUCCESS(rc))
{
/*
* Init CPUM.
*/
if (RT_SUCCESS(rc))
return rc;
}
}
/* failed */
return rc;
}
/**
* Terminates the R0 driver for a particular VM instance.
*
* @returns VBox status code.
*
* @param pVM The VM instance in question.
* @thread EMT.
*/
{
/*
* Deregister the logger.
*/
return VINF_SUCCESS;
}
/**
* Calls the ring-3 host code.
*
* @returns VBox status code of the ring-3 call.
* @param pVM The VM handle.
* @param enmOperation The operation.
* @param uArg The argument to the operation.
*/
{
/** @todo profile this! */
if (rc == VINF_SUCCESS)
return rc;
}
#ifdef VBOX_WITH_STATISTICS
/**
* Record return code statistics
* @param pVM The VM handle.
* @param rc The status code.
*/
{
/*
* Collect statistics.
*/
switch (rc)
{
case VINF_SUCCESS:
break;
case VINF_EM_RAW_INTERRUPT:
break;
break;
case VINF_EM_RAW_GUEST_TRAP:
break;
case VINF_EM_RAW_RING_SWITCH:
break;
break;
break;
break;
case VINF_EM_RAW_IRET_TRAP:
break;
case VINF_IOM_HC_IOPORT_READ:
break;
case VINF_IOM_HC_IOPORT_WRITE:
break;
case VINF_IOM_HC_MMIO_READ:
break;
case VINF_IOM_HC_MMIO_WRITE:
break;
break;
break;
break;
break;
case VINF_PATCH_EMULATE_INSTR:
break;
break;
break;
break;
break;
break;
case VINF_CSAM_PENDING_ACTION:
break;
case VINF_PGM_SYNC_CR3:
break;
case VINF_PATM_PATCH_INT3:
break;
case VINF_PATM_PATCH_TRAP_PF:
break;
case VINF_PATM_PATCH_TRAP_GP:
break;
break;
break;
case VINF_EM_RESCHEDULE_REM:
break;
case VINF_EM_RAW_TO_R3:
break;
break;
break;
case VINF_VMM_CALL_HOST:
{
case VMMCALLHOST_PDM_LOCK:
break;
break;
break;
case VMMCALLHOST_PGM_LOCK:
break;
break;
break;
#ifndef VBOX_WITH_NEW_PHYS_CODE
break;
#endif
break;
break;
case VMMCALLHOST_VM_SET_ERROR:
break;
break;
default:
break;
}
break;
break;
case VINF_PGM_CHANGE_MODE:
break;
break;
case VINF_EM_PENDING_REQUEST:
break;
default:
break;
}
}
#endif /* VBOX_WITH_STATISTICS */
/**
* The Ring 0 entry point, called by the interrupt gate.
*
* @returns VBox status code.
* @param pVM The VM to operate on.
* @param enmOperation Which operation to execute.
* @param pvArg Argument to the operation.
* @remarks Assume called with interrupts disabled.
*/
{
switch (enmOperation)
{
#ifdef VBOX_WITH_IDT_PATCHING
/*
* Switch to GC.
* These calls return whatever the GC returns.
*/
case VMMR0_DO_RAW_RUN:
{
/* Safety precaution as VMX disables the switcher. */
return VERR_NOT_SUPPORTED;
register int rc;
#ifdef VBOX_WITH_STATISTICS
#endif
/*
* We'll let TRPM change the stack frame so our return is different.
* Just keep in mind that after the call, things have changed!
*/
if ( rc == VINF_EM_RAW_INTERRUPT
|| rc == VINF_EM_RAW_INTERRUPT_HYPER)
{
/*
* Don't trust the compiler to get this right.
* gcc -fomit-frame-pointer screws up big time here. This works fine in 64-bit
* mode too because we push the arguments on the stack in the IDT patch code.
*/
# if defined(__GNUC__)
# elif defined(RT_ARCH_X86)
# else
# error "huh?"
# endif
else
{
# if defined(DEBUG) || defined(LOG_ENABLED)
static bool s_fHaveWarned = false;
if (!s_fHaveWarned)
{
s_fHaveWarned = true;
RTLogPrintf("VMMR0.r0: The compiler can't find the stack frame!\n");
RTLogComPrintf("VMMR0.r0: The compiler can't find the stack frame!\n");
}
# endif
}
}
return rc;
}
/*
* Switch to GC to execute Hypervisor function.
*/
case VMMR0_DO_CALL_HYPERVISOR:
{
/* Safety precaution as VMX disables the switcher. */
return VERR_NOT_SUPPORTED;
/** @todo dispatch interrupts? */
return rc;
}
/*
* For profiling.
*/
case VMMR0_DO_NOP:
return VINF_SUCCESS;
#endif /* VBOX_WITH_IDT_PATCHING */
default:
/*
* We're returning VERR_NOT_SUPPORT here so we've got something else
* than -1 which the interrupt gate glue code might return.
*/
return VERR_NOT_SUPPORTED;
}
}
/**
* The Ring 0 entry point, called by the fast-ioctl path.
*
* @param pVM The VM to operate on.
* The return code is stored in pVM->vmm.s.iLastGZRc.
* @param idCPU VMCPU id.
* @param enmOperation Which operation to execute.
* @remarks Assume called with interrupts _enabled_.
*/
{
switch (enmOperation)
{
/*
* Switch to GC and run guest raw mode code.
* Disable interrupts before doing the world switch.
*/
case VMMR0_DO_RAW_RUN:
{
/* Safety precaution as hwaccm disables the switcher. */
{
if ( rc == VINF_EM_RAW_INTERRUPT
|| rc == VINF_EM_RAW_INTERRUPT_HYPER)
#ifdef VBOX_WITH_STATISTICS
#endif
}
else
{
}
break;
}
/*
* Run guest code using the available hardware acceleration technology.
*
* Disable interrupts before we do anything interesting. On Windows we avoid
* this by having the support driver raise the IRQL before calling us, this way
* we hope to get away with page faults and later calling into the kernel.
*/
case VMMR0_DO_HWACC_RUN:
{
int rc;
{
return;
}
#ifndef RT_OS_WINDOWS /** @todo check other hosts */
#endif
if (!HWACCMR0SuspendPending())
{
if (RT_SUCCESS(rc))
{
rc = vmmR0CallHostSetJmp(&pVM->vmm.s.CallHostR0JmpBuf, HWACCMR0RunGuestCode, pVM); /* this may resume code. */
}
}
else
{
/* System is about to go into suspend mode; go back to ring 3. */
}
#ifndef RT_OS_WINDOWS /** @todo check other hosts */
#endif
#ifdef VBOX_WITH_STATISTICS
#endif
/* No special action required for external interrupts, just return. */
break;
}
/*
* For profiling.
*/
case VMMR0_DO_NOP:
break;
/*
* Impossible.
*/
default:
break;
}
}
/**
* Validates a session or VM session argument.
*
* @returns true / false accordingly.
* @param pVM The VM argument.
* @param pSession The session argument.
*/
DECLINLINE(bool) vmmR0IsValidSession(PVM pVM, PSUPDRVSESSION pClaimedSession, PSUPDRVSESSION pSession)
{
/* This must be set! */
if (!pSession)
return false;
/* Only one out of the two. */
if (pVM && pClaimedSession)
return false;
if (pVM)
return pClaimedSession == pSession;
}
/**
* VMMR0EntryEx worker function, either called directly or when ever possible
* called thru a longjmp so we can exit safely on failure.
*
* @returns VBox status code.
* @param pVM The VM to operate on.
* @param enmOperation Which operation to execute.
* @param pReqHdr This points to a SUPVMMR0REQHDR packet. Optional.
* The support driver validates this if it's present.
* @param u64Arg Some simple constant argument.
* @param pSession The session of the caller.
* @remarks Assume called with interrupts _enabled_.
*/
static int vmmR0EntryExWorker(PVM pVM, VMMR0OPERATION enmOperation, PSUPVMMR0REQHDR pReqHdr, uint64_t u64Arg, PSUPDRVSESSION pSession)
{
/*
* Common VM pointer validation.
*/
if (pVM)
{
{
return VERR_INVALID_POINTER;
}
{
SUPR0Printf("vmmR0EntryExWorker: Invalid pVM=%p:{enmVMState=%d, .pVMR0=%p}! (op=%d)\n",
return VERR_INVALID_POINTER;
}
}
switch (enmOperation)
{
/*
* GVM requests
*/
case VMMR0_DO_GVMM_CREATE_VM:
return VERR_INVALID_PARAMETER;
case VMMR0_DO_GVMM_DESTROY_VM:
return VERR_INVALID_PARAMETER;
return GVMMR0DestroyVM(pVM);
case VMMR0_DO_GVMM_SCHED_HALT:
if (pReqHdr)
return VERR_INVALID_PARAMETER;
return VERR_INVALID_PARAMETER;
return GVMMR0SchedWakeUp(pVM);
case VMMR0_DO_GVMM_SCHED_POLL:
return VERR_INVALID_PARAMETER;
if (u64Arg)
return VERR_INVALID_PARAMETER;
if (u64Arg)
return VERR_INVALID_PARAMETER;
/*
* Initialize the R0 part of a VM instance.
*/
case VMMR0_DO_VMMR0_INIT:
/*
* Terminate the R0 part of a VM instance.
*/
case VMMR0_DO_VMMR0_TERM:
/*
* Attempt to enable hwacc mode and check the current setting.
*
*/
case VMMR0_DO_HWACC_ENABLE:
/*
* Setup the hardware accelerated raw-mode session.
*/
case VMMR0_DO_HWACC_SETUP_VM:
{
return rc;
}
/*
* Switch to GC to execute Hypervisor function.
*/
case VMMR0_DO_CALL_HYPERVISOR:
{
/* Safety precaution as HWACCM can disable the switcher. */
return VERR_NOT_SUPPORTED;
/** @todo dispatch interrupts? */
return rc;
}
/*
* PGM wrappers.
*/
return PGMR0PhysAllocateHandyPages(pVM);
/*
* GMM wrappers.
*/
if (u64Arg)
return VERR_INVALID_PARAMETER;
if (u64Arg)
return VERR_INVALID_PARAMETER;
if (u64Arg)
return VERR_INVALID_PARAMETER;
case VMMR0_DO_GMM_FREE_PAGES:
if (u64Arg)
return VERR_INVALID_PARAMETER;
if (u64Arg)
return VERR_INVALID_PARAMETER;
if (pReqHdr)
return VERR_INVALID_PARAMETER;
if (u64Arg)
return VERR_INVALID_PARAMETER;
case VMMR0_DO_GMM_SEED_CHUNK:
if (pReqHdr)
return VERR_INVALID_PARAMETER;
/*
* A quick GCFGM mock-up.
*/
/** @todo GCFGM with proper access control, ring-3 management interface and all that. */
case VMMR0_DO_GCFGM_SET_VALUE:
{
return VERR_INVALID_PARAMETER;
return VERR_INVALID_PARAMETER;
int rc;
if (enmOperation == VMMR0_DO_GCFGM_SET_VALUE)
{
//if (rc == VERR_CFGM_VALUE_NOT_FOUND)
// rc = GMMR0SetConfig(pReq->pSession, &pReq->szName[0], pReq->u64Value);
}
else
{
//if (rc == VERR_CFGM_VALUE_NOT_FOUND)
// rc = GMMR0QueryConfig(pReq->pSession, &pReq->szName[0], &pReq->u64Value);
}
return rc;
}
/*
* Requests to the internal networking service.
*/
case VMMR0_DO_INTNET_OPEN:
{
return VERR_INVALID_PARAMETER;
if (!g_pIntNet)
return VERR_NOT_SUPPORTED;
}
case VMMR0_DO_INTNET_IF_CLOSE:
if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFCLOSEREQ)pReqHdr)->pSession, pSession))
return VERR_INVALID_PARAMETER;
if (!g_pIntNet)
return VERR_NOT_SUPPORTED;
if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFGETRING3BUFFERREQ)pReqHdr)->pSession, pSession))
return VERR_INVALID_PARAMETER;
if (!g_pIntNet)
return VERR_NOT_SUPPORTED;
if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETPROMISCUOUSMODEREQ)pReqHdr)->pSession, pSession))
return VERR_INVALID_PARAMETER;
if (!g_pIntNet)
return VERR_NOT_SUPPORTED;
return INTNETR0IfSetPromiscuousModeReq(g_pIntNet, pSession, (PINTNETIFSETPROMISCUOUSMODEREQ)pReqHdr);
if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETMACADDRESSREQ)pReqHdr)->pSession, pSession))
return VERR_INVALID_PARAMETER;
if (!g_pIntNet)
return VERR_NOT_SUPPORTED;
if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETACTIVEREQ)pReqHdr)->pSession, pSession))
return VERR_INVALID_PARAMETER;
if (!g_pIntNet)
return VERR_NOT_SUPPORTED;
case VMMR0_DO_INTNET_IF_SEND:
if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSENDREQ)pReqHdr)->pSession, pSession))
return VERR_INVALID_PARAMETER;
if (!g_pIntNet)
return VERR_NOT_SUPPORTED;
case VMMR0_DO_INTNET_IF_WAIT:
if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFWAITREQ)pReqHdr)->pSession, pSession))
return VERR_INVALID_PARAMETER;
if (!g_pIntNet)
return VERR_NOT_SUPPORTED;
/*
* For profiling.
*/
case VMMR0_DO_NOP:
case VMMR0_DO_SLOW_NOP:
return VINF_SUCCESS;
/*
* For testing Ring-0 APIs invoked in this environment.
*/
case VMMR0_DO_TESTS:
/** @todo make new test */
return VINF_SUCCESS;
default:
/*
* We're returning VERR_NOT_SUPPORT here so we've got something else
* than -1 which the interrupt gate glue code might return.
*/
return VERR_NOT_SUPPORTED;
}
}
/**
* Argument for vmmR0EntryExWrapper containing the argument s ofr VMMR0EntryEx.
*/
typedef struct VMMR0ENTRYEXARGS
{
/** Pointer to a vmmR0EntryExWrapper argument package. */
typedef VMMR0ENTRYEXARGS *PVMMR0ENTRYEXARGS;
/**
* This is just a longjmp wrapper function for VMMR0EntryEx calls.
*
* @returns VBox status code.
* @param pvArgs The argument package
*/
static int vmmR0EntryExWrapper(void *pvArgs)
{
}
/**
* The Ring 0 entry point, called by the support library (SUP).
*
* @returns VBox status code.
* @param pVM The VM to operate on.
* @param enmOperation Which operation to execute.
* @param pReq This points to a SUPVMMR0REQHDR packet. Optional.
* @param u64Arg Some simple constant argument.
* @param pSession The session of the caller.
* @remarks Assume called with interrupts _enabled_.
*/
VMMR0DECL(int) VMMR0EntryEx(PVM pVM, VMMR0OPERATION enmOperation, PSUPVMMR0REQHDR pReq, uint64_t u64Arg, PSUPDRVSESSION pSession)
{
/*
* Requests that should only happen on the EMT thread will be
* wrapped in a setjmp so we can assert without causing trouble.
*/
{
switch (enmOperation)
{
case VMMR0_DO_VMMR0_INIT:
case VMMR0_DO_VMMR0_TERM:
case VMMR0_DO_GMM_FREE_PAGES:
case VMMR0_DO_GMM_SEED_CHUNK:
{
/** @todo validate this EMT claim... GVM knows. */
}
default:
break;
}
}
}
/**
* Internal R0 logger worker: Flush logger.
*
* @param pLogger The logger instance to flush.
* @remark This function must be exported!
*/
{
/*
* Convert the pLogger into a VM handle and 'call' back to Ring-3.
* (This is a bit paranoid code.)
*/
{
#ifdef DEBUG
#endif
return;
}
{
#ifdef DEBUG
#endif
return;
}
/*
* Check that the jump buffer is armed.
*/
#ifdef RT_ARCH_X86
#else
#endif
{
#ifdef DEBUG
SUPR0Printf("vmmR0LoggerFlush: Jump buffer isn't armed!\n");
#endif
pLogger->offScratch = 0;
return;
}
}
/**
* Jump back to ring-3 if we're the EMT and the longjmp is armed.
*
* @returns true if the breakpoint should be hit, false if it should be ignored.
*/
{
if (pVM)
{
#ifdef RT_ARCH_X86
#else
#endif
{
return RT_FAILURE_NP(rc);
}
}
#ifdef RT_OS_LINUX
return true;
#else
return false;
#endif
}
/**
* Override this so we can push it up to ring-3.
*
* @param pszExpr Expression. Can be NULL.
* @param uLine Location line number.
* @param pszFile Location file name.
* @param pszFunction Location function name.
*/
DECLEXPORT(void) RTCALL AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
{
#ifndef DEBUG_sandervl
SUPR0Printf("\n!!R0-Assertion Failed!!\n"
"Expression: %s\n"
"Location : %s(%d) %s\n",
#endif
LogAlways(("\n!!R0-Assertion Failed!!\n"
"Expression: %s\n"
"Location : %s(%d) %s\n",
if (pVM)
"\n!!R0-Assertion Failed!!\n"
"Expression: %s\n"
"Location : %s(%d) %s\n",
}
/**
* Callback for RTLogFormatV which writes to the ring-3 log port.
* See PFNLOGOUTPUT() for details.
*/
{
{
#ifndef DEBUG_sandervl
#endif
}
return cbChars;
}
{
if (pLog)
{
if (pVM)
{
}
}
}