SUPDRVShared.c revision 37eb780874007e6c73f493edcfd7a1e498a6a2ef
/** @file
*
* VBox host drivers - Ring-0 support drivers - Shared code:
* Driver code for all host platforms
*/
/*
* 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 *
*******************************************************************************/
#include "SUPDRV.h"
#ifndef PAGE_SHIFT
#endif
#include <iprt/semaphore.h>
#include <iprt/spinlock.h>
#ifdef VBOX_WITHOUT_IDT_PATCHING
#endif
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/* from x86.h - clashes with linux thus this duplication */
#define X86_CR0_PE BIT(0)
#define MSR_K6_EFER 0xc0000080
/** The frequency by which we recalculate the u32UpdateHz and
* u32UpdateIntervalNS GIP members. The value must be a power of 2. */
#define GIP_UPDATEHZ_RECALC_FREQ 0x800
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/**
* Array of the R0 SUP API.
*/
static SUPFUNC g_aFunctions[] =
{
/* name function */
{ "SUPR0ObjRegister", (void *)SUPR0ObjRegister },
{ "SUPR0ObjAddRef", (void *)SUPR0ObjAddRef },
{ "SUPR0ObjRelease", (void *)SUPR0ObjRelease },
{ "SUPR0ObjVerifyAccess", (void *)SUPR0ObjVerifyAccess },
{ "SUPR0LockMem", (void *)SUPR0LockMem },
{ "SUPR0UnlockMem", (void *)SUPR0UnlockMem },
{ "SUPR0ContAlloc", (void *)SUPR0ContAlloc },
{ "SUPR0ContFree", (void *)SUPR0ContFree },
{ "SUPR0MemAlloc", (void *)SUPR0MemAlloc },
{ "SUPR0MemGetPhys", (void *)SUPR0MemGetPhys },
{ "SUPR0MemFree", (void *)SUPR0MemFree },
{ "SUPR0Printf", (void *)SUPR0Printf },
{ "RTMemAlloc", (void *)RTMemAlloc },
{ "RTMemAllocZ", (void *)RTMemAllocZ },
{ "RTMemFree", (void *)RTMemFree },
/* These doesn't work yet on linux - use fast mutexes!
{ "RTSemMutexCreate", (void *)RTSemMutexCreate },
{ "RTSemMutexRequest", (void *)RTSemMutexRequest },
{ "RTSemMutexRelease", (void *)RTSemMutexRelease },
{ "RTSemMutexDestroy", (void *)RTSemMutexDestroy },
*/
{ "RTSemFastMutexCreate", (void *)RTSemFastMutexCreate },
{ "RTSemFastMutexDestroy", (void *)RTSemFastMutexDestroy },
{ "RTSemFastMutexRequest", (void *)RTSemFastMutexRequest },
{ "RTSemFastMutexRelease", (void *)RTSemFastMutexRelease },
{ "RTSemEventCreate", (void *)RTSemEventCreate },
{ "RTSemEventSignal", (void *)RTSemEventSignal },
{ "RTSemEventWait", (void *)RTSemEventWait },
{ "RTSemEventDestroy", (void *)RTSemEventDestroy },
{ "RTSpinlockCreate", (void *)RTSpinlockCreate },
{ "RTSpinlockDestroy", (void *)RTSpinlockDestroy },
{ "RTSpinlockAcquire", (void *)RTSpinlockAcquire },
{ "RTSpinlockRelease", (void *)RTSpinlockRelease },
{ "RTSpinlockAcquireNoInts", (void *)RTSpinlockAcquireNoInts },
{ "RTSpinlockReleaseNoInts", (void *)RTSpinlockReleaseNoInts },
{ "RTThreadNativeSelf", (void *)RTThreadNativeSelf },
{ "RTThreadSleep", (void *)RTThreadSleep },
{ "RTThreadYield", (void *)RTThreadYield },
#if 0 /* Thread APIs, Part 2. */
{ "RTThreadSelf", (void *)RTThreadSelf },
{ "RTThreadCreate", (void *)RTThreadCreate },
{ "RTThreadGetNative", (void *)RTThreadGetNative },
{ "RTThreadWait", (void *)RTThreadWait },
{ "RTThreadWaitNoResume", (void *)RTThreadWaitNoResume },
{ "RTThreadGetName", (void *)RTThreadGetName },
{ "RTThreadSelfName", (void *)RTThreadSelfName },
{ "RTThreadGetType", (void *)RTThreadGetType },
{ "RTThreadUserSignal", (void *)RTThreadUserSignal },
{ "RTThreadUserReset", (void *)RTThreadUserReset },
{ "RTThreadUserWait", (void *)RTThreadUserWait },
{ "RTThreadUserWaitNoResume", (void *)RTThreadUserWaitNoResume },
#endif
{ "RTLogDefaultInstance", (void *)RTLogDefaultInstance },
{ "RTLogRelDefaultInstance", (void *)RTLogRelDefaultInstance },
{ "RTLogSetDefaultInstanceThread", (void *)RTLogSetDefaultInstanceThread },
{ "RTLogLogger", (void *)RTLogLogger },
{ "RTLogLoggerEx", (void *)RTLogLoggerEx },
{ "RTLogLoggerExV", (void *)RTLogLoggerExV },
{ "AssertMsg1", (void *)AssertMsg1 },
{ "AssertMsg2", (void *)AssertMsg2 },
};
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
#ifndef VBOX_WITHOUT_IDT_PATCHING
static int supdrvIOCtl_IdtInstall(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPIDTINSTALL_IN pIn, PSUPIDTINSTALL_OUT pOut);
#endif /* !VBOX_WITHOUT_IDT_PATCHING */
static int supdrvIOCtl_LdrOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDROPEN_IN pIn, PSUPLDROPEN_OUT pOut);
static int supdrvIOCtl_LdrGetSymbol(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDRGETSYMBOL_IN pIn, PSUPLDRGETSYMBOL_OUT pOut);
static SUPGIPMODE supdrvGipDeterminTscMode(void);
#ifdef USE_NEW_OS_INTERFACE
#endif
/**
* Initializes the device extentsion structure.
*
* @returns 0 on success.
* @returns SUPDRV_ERR_ on failure.
* @param pDevExt The device extension to initialize.
*/
{
/*
* Initialize it.
*/
int rc;
if (!rc)
{
if (!rc)
{
if (!rc)
{
#ifdef USE_NEW_OS_INTERFACE
if (RT_SUCCESS(rc))
{
return 0;
}
#else
return 0;
#endif
}
}
}
return rc;
}
/**
* Delete the device extension (e.g. cleanup members).
*
* @returns 0.
* @param pDevExt The device extension to delete.
*/
{
#ifndef VBOX_WITHOUT_IDT_PATCHING
#endif
/*
* Kill mutexes and spinlocks.
*/
/*
* Free lists.
*/
#ifndef VBOX_WITHOUT_IDT_PATCHING
/* patches */
/** @todo make sure we don't uninstall patches which has been patched by someone else. */
while (pPatch)
{
}
#endif /* !VBOX_WITHOUT_IDT_PATCHING */
/* objects. */
#endif
while (pObj)
{
}
/* usage records. */
while (pUsage)
{
}
#ifdef USE_NEW_OS_INTERFACE
/* kill the GIP */
#endif
return 0;
}
/**
* Create session.
*
* @returns 0 on success.
* @returns SUPDRV_ERR_ on failure.
* @param pDevExt Device extension.
* @param ppSession Where to store the pointer to the session data.
*/
{
/*
* Allocate memory for the session data.
*/
int rc = SUPDRV_ERR_NO_MEMORY;
if (pSession)
{
/* Initialize session data. */
if (!rc)
{
/*pSession->pLdrUsage = NULL;
pSession->pPatchUsage = NULL;
pSession->pUsage = NULL;
pSession->pGip = NULL;
pSession->fGipReferenced = false;
pSession->Bundle.cUsed = 0 */
return 0;
}
}
return rc;
}
/**
* Shared code for cleaning up a session.
*
* @param pDevExt Device extension.
* @param pSession Session data.
* This data will be freed by this routine.
*/
{
/*
* Cleanup the session first.
*/
/*
* Free the rest of the session stuff.
*/
dprintf2(("supdrvCloseSession: returns\n"));
}
/**
* Shared code for cleaning up a session (but not quite freeing it).
*
* This is primarily intended for MAC OS X where we have to clean up the memory
* stuff before the file handle is closed.
*
* @param pDevExt Device extension.
* @param pSession Session data.
* This data will be freed by this routine.
*/
{
/*
* Remove logger instances related to this session.
* (This assumes the dprintf and dprintf2 macros doesn't use the normal logging.)
*/
#ifndef VBOX_WITHOUT_IDT_PATCHING
/*
* Uninstall any IDT patches installed for this session.
*/
#endif
/*
* Release object references made in this session.
* In theory there should be noone racing us in this session.
*/
dprintf2(("release objects - start\n"));
{
{
AssertMsg(pUsage->cUsage >= 1 && pObj->cUsage >= pUsage->cUsage, ("glob %d; sess %d\n", pObj->cUsage, pUsage->cUsage));
{
}
else
{
/* Destroy the object and free the record. */
else
{
{
break;
}
}
}
/* free it and continue. */
}
}
dprintf2(("release objects - done\n"));
/*
* Release memory allocated in the session.
*
* We do not serialize this as we assume that the application will
* not allocated memory while closing the file handle object.
*/
dprintf2(("freeing memory:\n"));
while (pBundle)
{
unsigned i;
/*
* Check and unlock all entries in the bundle.
*/
{
#ifdef USE_NEW_OS_INTERFACE
{
int rc;
{
}
}
#else /* !USE_NEW_OS_INTERFACE */
{
{
case MEMREF_TYPE_LOCKED:
break;
case MEMREF_TYPE_CONT:
break;
case MEMREF_TYPE_LOW:
break;
case MEMREF_TYPE_MEM:
break;
default:
break;
}
}
#endif /* !USE_NEW_OS_INTERFACE */
}
/*
* Advance and free previous bundle.
*/
}
dprintf2(("freeing memory - done\n"));
/*
* Loaded images needs to be dereferenced and possibly freed up.
*/
dprintf2(("freeing images:\n"));
{
while (pUsage)
{
else
}
}
dprintf2(("freeing images - done\n"));
/*
* Unmap the GIP.
*/
dprintf2(("umapping GIP:\n"));
#ifdef USE_NEW_OS_INTERFACE
#else
#endif
{
#ifndef USE_NEW_OS_INTERFACE
#endif
pSession->fGipReferenced = 0;
}
dprintf2(("umapping GIP - done\n"));
}
#ifdef VBOX_WITHOUT_IDT_PATCHING
/**
* Fast path I/O Control worker.
*
* @returns 0 on success.
* @returns One of the SUPDRV_ERR_* on failure.
* @param uIOCtl Function number.
* @param pDevExt Device extention.
* @param pSession Session data.
*/
{
/*
* Disable interrupts before invoking VMMR0Entry() because it ASSUMES
* that interrupts are disabled. (We check the two prereqs after doing
* this only to allow the compiler to optimize things better.)
*/
int rc;
{
switch (uIOCtl)
{
break;
break;
case SUP_IOCTL_FAST_DO_NOP:
break;
default:
break;
}
}
else
return rc;
}
#endif /* VBOX_WITHOUT_IDT_PATCHING */
/**
* I/O Control worker.
*
* @returns 0 on success.
* @returns One of the SUPDRV_ERR_* on failure.
* @param uIOCtl Function number.
* @param pDevExt Device extention.
* @param pSession Session data.
* @param pvIn Input data.
* @param cbIn Size of input data.
* @param pvOut Output data.
* IMPORTANT! This buffer may be shared with the input
* data, thus no writing before done reading
* input data!!!
* @param cbOut Size of output data.
* @param pcbReturned Size of the returned data.
*/
{
*pcbReturned = 0;
switch (uIOCtl)
{
case SUP_IOCTL_COOKIE:
{
/*
* Validate.
*/
{
dprintf(("SUP_IOCTL_COOKIE: Invalid input/output sizes. cbIn=%ld expected %ld. cbOut=%ld expected %ld.\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
return SUPDRV_ERR_INVALID_MAGIC;
}
{
dprintf(("SUP_IOCTL_COOKIE: Version mismatch. Requested: %#x Current: %#x\n", pIn->u32Version, SUPDRVIOC_VERSION));
return SUPDRV_ERR_VERSION_MISMATCH;
}
/*
* Fill in return data and be gone.
*/
/** @todo secure cookie negotiation? */
*pcbReturned = sizeof(*pOut);
return 0;
}
case SUP_IOCTL_QUERY_FUNCS:
{
unsigned cFunctions;
/*
* Validate.
*/
{
dprintf(("SUP_IOCTL_QUERY_FUNCS: Invalid input/output sizes. cbIn=%ld expected %ld. cbOut=%ld expected %ld.\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_QUERY_FUNCS: Cookie mismatch {%#x,%#x} != {%#x,%#x}!\n",
return SUPDRV_ERR_INVALID_MAGIC;
}
/*
* Copy the functions.
*/
("Why aren't R3 querying all the functions!?! cFunctions=%d while there are %d available\n",
return 0;
}
case SUP_IOCTL_IDT_INSTALL:
{
/*
* Validate.
*/
{
dprintf(("SUP_IOCTL_INSTALL: Invalid input/output sizes. cbIn=%ld expected %ld. cbOut=%ld expected %ld.\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_INSTALL: Cookie mismatch {%#x,%#x} != {%#x,%#x}!\n",
return SUPDRV_ERR_INVALID_MAGIC;
}
*pcbReturned = sizeof(*pOut);
#ifndef VBOX_WITHOUT_IDT_PATCHING
#else
return 0;
#endif
}
case SUP_IOCTL_IDT_REMOVE:
{
/*
* Validate.
*/
|| cbOut != 0)
{
dprintf(("SUP_IOCTL_REMOVE: Invalid input/output sizes. cbIn=%ld expected %ld. cbOut=%ld expected %ld.\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_REMOVE: Cookie mismatch {%#x,%#x} != {%#x,%#x}!\n",
return SUPDRV_ERR_INVALID_MAGIC;
}
#ifndef VBOX_WITHOUT_IDT_PATCHING
#else
return 0;
#endif
}
case SUP_IOCTL_PINPAGES:
{
int rc;
/*
* Validate.
*/
{
dprintf(("SUP_IOCTL_PINPAGES: Invalid input/output sizes. cbIn=%ld expected %ld. cbOut=%ld expected %ld.\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_PINPAGES: Cookie mismatch {%#x,%#x} != {%#x,%#x}!\n",
return SUPDRV_ERR_INVALID_MAGIC;
}
{
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_PINPAGES: Output buffer is too small! %d required %d passed in.\n",
return SUPDRV_ERR_INVALID_PARAM;
}
/*
* Execute.
*/
if (rc)
*pcbReturned = 0;
return rc;
}
case SUP_IOCTL_UNPINPAGES:
{
/*
* Validate.
*/
|| cbOut != 0)
{
dprintf(("SUP_IOCTL_UNPINPAGES: Invalid input/output sizes. cbIn=%ld expected %ld. cbOut=%ld expected %ld.\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_UNPINPAGES: Cookie mismatch {%#x,%#x} != {%#x,%#x}!\n",
return SUPDRV_ERR_INVALID_MAGIC;
}
/*
* Execute.
*/
}
case SUP_IOCTL_CONT_ALLOC:
{
int rc;
/*
* Validate.
*/
{
dprintf(("SUP_IOCTL_CONT_ALLOC: Invalid input/output sizes. cbIn=%ld expected %ld. cbOut=%ld expected %ld.\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_CONT_ALLOC: Cookie mismatch {%#x,%#x} != {%#x,%#x}!\n",
return SUPDRV_ERR_INVALID_MAGIC;
}
/*
* Execute.
*/
if (!rc)
*pcbReturned = sizeof(*pOut);
return rc;
}
case SUP_IOCTL_CONT_FREE:
{
/*
* Validate.
*/
|| cbOut != 0)
{
dprintf(("SUP_IOCTL_CONT_FREE: Invalid input/output sizes. cbIn=%ld expected %ld. cbOut=%ld expected %ld.\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_CONT_FREE: Cookie mismatch {%#x,%#x} != {%#x,%#x}!\n",
return SUPDRV_ERR_INVALID_MAGIC;
}
/*
* Execute.
*/
}
case SUP_IOCTL_LDR_OPEN:
{
/*
* Validate.
*/
{
dprintf(("SUP_IOCTL_LDR_OPEN: Invalid input/output sizes. cbIn=%ld expected %ld. cbOut=%ld expected %ld.\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_LDR_OPEN: Cookie mismatch {%#x,%#x} != {%#x,%#x}!\n",
return SUPDRV_ERR_INVALID_MAGIC;
}
{
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_LDR_GET_SYMBOL: The image name isn't terminated!\n"));
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_LDR_OPEN: The image name is too short\n"));
return SUPDRV_ERR_INVALID_PARAM;
}
{
return SUPDRV_ERR_INVALID_PARAM;
}
*pcbReturned = sizeof(*pOut);
}
case SUP_IOCTL_LDR_LOAD:
{
/*
* Validate.
*/
|| cbOut != 0)
{
dprintf(("SUP_IOCTL_LDR_LOAD: Invalid input/output sizes. cbIn=%ld expected greater than %ld. cbOut=%ld expected %ld.\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_LDR_LOAD: Cookie mismatch {%#x,%#x} != {%#x,%#x}!\n",
return SUPDRV_ERR_INVALID_MAGIC;
}
{
dprintf(("SUP_IOCTL_LDR_LOAD: Invalid size %d. InputBufferLength=%d\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
return SUPDRV_ERR_INVALID_PARAM;
}
)
{
dprintf(("SUP_IOCTL_LDR_LOAD: symbol table is outside the image bits! offSymbols=%u cSymbols=%d cbImage=%d\n",
return SUPDRV_ERR_INVALID_PARAM;
}
)
{
dprintf(("SUP_IOCTL_LDR_LOAD: string table is outside the image bits! offStrTab=%u cbStrTab=%d cbImage=%d\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
uint32_t i;
{
{
dprintf(("SUP_IOCTL_LDR_LOAD: symbol i=%d has an invalid symbol offset: %#x (max=%#x)\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_LDR_LOAD: symbol i=%d has an invalid name offset: %#x (max=%#x)\n",
return SUPDRV_ERR_INVALID_PARAM;
}
if (!memchr(&pIn->achImage[pIn->offStrTab + paSyms[i].offName], '\0', pIn->cbStrTab - paSyms[i].offName))
{
dprintf(("SUP_IOCTL_LDR_LOAD: symbol i=%d has an unterminated name! offName=%#x (max=%#x)\n",
return SUPDRV_ERR_INVALID_PARAM;
}
}
}
}
case SUP_IOCTL_LDR_FREE:
{
/*
* Validate.
*/
|| cbOut != 0)
{
dprintf(("SUP_IOCTL_LDR_FREE: Invalid input/output sizes. cbIn=%ld expected %ld. cbOut=%ld expected %ld.\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_LDR_FREE: Cookie mismatch {%#x,%#x} != {%#x,%#x}!\n",
return SUPDRV_ERR_INVALID_MAGIC;
}
}
case SUP_IOCTL_LDR_GET_SYMBOL:
{
char *pszEnd;
/*
* Validate.
*/
{
dprintf(("SUP_IOCTL_LDR_GET_SYMBOL: Invalid input/output sizes. cbIn=%d expected >=%d. cbOut=%d expected at%d.\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_LDR_GET_SYMBOL: Cookie mismatch {%#x,%#x} != {%#x,%#x}!\n",
return SUPDRV_ERR_INVALID_MAGIC;
}
if (!pszEnd)
{
dprintf(("SUP_IOCTL_LDR_GET_SYMBOL: The symbol name isn't terminated!\n"));
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_LDR_GET_SYMBOL: The symbol name too long (%ld chars, max is %d)!\n",
return SUPDRV_ERR_INVALID_PARAM;
}
*pcbReturned = sizeof(*pOut);
}
/** @todo this interface needs re-doing, we're accessing Ring-3 buffers directly here! */
case SUP_IOCTL_CALL_VMMR0:
{
/*
* Validate.
*/
{
dprintf(("SUP_IOCTL_CALL_VMMR0: Invalid input/output sizes. cbIn=%ld expected %ld. cbOut=%ld expected %ld.\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_CALL_VMMR0: Cookie mismatch {%#x,%#x} != {%#x,%#x}!\n",
return SUPDRV_ERR_INVALID_MAGIC;
}
/*
* Do we have an entrypoint?
*/
if (!pDevExt->pfnVMMR0Entry)
return SUPDRV_ERR_GENERAL_FAILURE;
/*
* Execute.
*/
*pcbReturned = sizeof(*pOut);
return 0;
}
{
int rc;
/*
* Validate.
*/
{
dprintf(("SUP_IOCTL_GET_PAGING_MODE: Invalid input/output sizes. cbIn=%ld expected %ld. cbOut=%ld expected %ld.\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_GET_PAGING_MODE: Cookie mismatch {%#x,%#x} != {%#x,%#x}!\n",
return SUPDRV_ERR_INVALID_MAGIC;
}
/*
* Execute.
*/
*pcbReturned = sizeof(*pOut);
if (rc)
*pcbReturned = 0;
return rc;
}
case SUP_IOCTL_LOW_ALLOC:
{
int rc;
/*
* Validate.
*/
{
dprintf(("SUP_IOCTL_LOW_ALLOC: Invalid input/output sizes. cbIn=%ld expected %ld. cbOut=%ld expected %ld.\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_LOW_ALLOC: Cookie mismatch {%#x,%#x} != {%#x,%#x}!\n",
return SUPDRV_ERR_INVALID_MAGIC;
}
{
dprintf(("SUP_IOCTL_LOW_ALLOC: Output buffer is too small! %d required %d passed in.\n",
return SUPDRV_ERR_INVALID_PARAM;
}
/*
* Execute.
*/
if (rc)
*pcbReturned = 0;
return rc;
}
case SUP_IOCTL_LOW_FREE:
{
/*
* Validate.
*/
|| cbOut != 0)
{
dprintf(("SUP_IOCTL_LOW_FREE: Invalid input/output sizes. cbIn=%ld expected %ld. cbOut=%ld expected %ld.\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_LOW_FREE: Cookie mismatch {%#x,%#x} != {%#x,%#x}!\n",
return SUPDRV_ERR_INVALID_MAGIC;
}
/*
* Execute.
*/
}
case SUP_IOCTL_GIP_MAP:
{
int rc;
/*
* Validate.
*/
{
dprintf(("SUP_IOCTL_GIP_MAP: Invalid input/output sizes. cbIn=%ld expected %ld. cbOut=%ld expected %ld.\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_GIP_MAP: Cookie mismatch {%#x,%#x} != {%#x,%#x}!\n",
return SUPDRV_ERR_INVALID_MAGIC;
}
/*
* Execute.
*/
if (!rc)
{
*pcbReturned = sizeof(*pOut);
}
return rc;
}
case SUP_IOCTL_GIP_UNMAP:
{
/*
* Validate.
*/
|| cbOut != 0)
{
dprintf(("SUP_IOCTL_GIP_UNMAP: Invalid input/output sizes. cbIn=%ld expected %ld. cbOut=%ld expected %ld.\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_GIP_UNMAP: Cookie mismatch {%#x,%#x} != {%#x,%#x}!\n",
return SUPDRV_ERR_INVALID_MAGIC;
}
/*
* Execute.
*/
return SUPR0GipUnmap(pSession);
}
{
/*
* Validate.
*/
|| cbOut != 0)
{
dprintf(("SUP_IOCTL_SET_VM_FOR_FAST: Invalid input/output sizes. cbIn=%ld expected %ld. cbOut=%ld expected %ld.\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_SET_VM_FOR_FAST: Cookie mismatch {%#x,%#x} != {%#x,%#x}!\n",
return SUPDRV_ERR_INVALID_MAGIC;
}
)
)
{
dprintf(("SUP_IOCTL_SET_VM_FOR_FAST: pVMR0=%p! Must be a valid, page aligned, pointer.\n", pIn->pVMR0));
return SUPDRV_ERR_INVALID_POINTER;
}
/*
* Execute.
*/
#ifndef VBOX_WITHOUT_IDT_PATCHING
OSDBGPRINT(("SUP_IOCTL_SET_VM_FOR_FAST: !VBOX_WITHOUT_IDT_PATCHING\n"));
return SUPDRV_ERR_GENERAL_FAILURE;
#else
return 0;
#endif
}
default:
break;
}
return SUPDRV_ERR_GENERAL_FAILURE;
}
/**
* Register a object for reference counting.
* The object is registered with one reference in the specified session.
*
* @returns Unique identifier on success (pointer).
* All future reference must use this identifier.
* @returns NULL on failure.
* @param pfnDestructor The destructore function which will be called when the reference count reaches 0.
* @param pvUser1 The first user argument.
* @param pvUser2 The second user argument.
*/
SUPR0DECL(void *) SUPR0ObjRegister(PSUPDRVSESSION pSession, SUPDRVOBJTYPE enmType, PFNSUPDRVDESTRUCTOR pfnDestructor, void *pvUser1, void *pvUser2)
{
/*
* Validate the input.
*/
if (!pSession)
{
return NULL;
}
if ( enmType <= SUPDRVOBJTYPE_INVALID
|| enmType >= SUPDRVOBJTYPE_END)
{
return NULL;
}
if (!pfnDestructor)
{
return NULL;
}
/*
* Allocate and initialize the object.
*/
if (!pObj)
return NULL;
/*
* Allocate the usage record.
* (We keep freed usage records around to simplity SUPR0ObjAddRef().)
*/
if (pUsage)
else
{
if (!pUsage)
{
return NULL;
}
}
/*
* Insert the object and create the session usage record.
*/
/* The object. */
/* The session record. */
dprintf(("SUPR0ObjRegister: pUsage=%p:{.pObj=%p, .pNext=%p}\n", pUsage, pUsage->pObj, pUsage->pNext));
return pObj;
}
/**
* Increment the reference counter for the object associating the reference
* with the specified session.
*
* @returns 0 on success.
* @returns SUPDRV_ERR_* on failure.
* @param pvObj The identifier returned by SUPR0ObjRegister().
* @param pSession The session which is referencing the object.
*/
{
/*
* Validate the input.
*/
if (!pSession)
{
return SUPDRV_ERR_INVALID_PARAM;
}
{
AssertMsgFailed(("Invalid pvObj=%p magic=%#x (exepcted %#x)\n",
return SUPDRV_ERR_INVALID_PARAM;
}
/*
* Preallocate the usage record.
*/
if (pUsagePre)
else
{
if (!pUsagePre)
return SUPDRV_ERR_NO_MEMORY;
}
/*
* Reference the object.
*/
/*
* Look for the session record.
*/
{
break;
}
if (pUsage)
else
{
/* create a new session record. */
dprintf(("SUPR0ObjRelease: pUsagePre=%p:{.pObj=%p, .pNext=%p}\n", pUsagePre, pUsagePre->pObj, pUsagePre->pNext));
}
/*
* Put any unused usage record into the free list..
*/
if (pUsagePre)
{
}
return 0;
}
/**
* Decrement / destroy a reference counter record for an object.
*
* The object is uniquely identified by pfnDestructor+pvUser1+pvUser2.
*
* @returns 0 on success.
* @returns SUPDRV_ERR_* on failure.
* @param pvObj The identifier returned by SUPR0ObjRegister().
* @param pSession The session which is referencing the object.
*/
{
bool fDestroy = false;
/*
* Validate the input.
*/
if (!pSession)
{
return SUPDRV_ERR_INVALID_PARAM;
}
{
AssertMsgFailed(("Invalid pvObj=%p magic=%#x (exepcted %#x)\n",
return SUPDRV_ERR_INVALID_PARAM;
}
/*
* Acquire the spinlock and look for the usage record.
*/
{
dprintf(("SUPR0ObjRelease: pUsage=%p:{.pObj=%p, .pNext=%p}\n", pUsage, pUsage->pObj, pUsage->pNext));
{
AssertMsg(pUsage->cUsage >= 1 && pObj->cUsage >= pUsage->cUsage, ("glob %d; sess %d\n", pObj->cUsage, pUsage->cUsage));
{
}
else
{
/*
* Free the session record.
*/
if (pUsagePrev)
else
/* What about the object? */
else
{
/*
* Object is to be destroyed, unlink it.
*/
fDestroy = true;
else
{
{
break;
}
}
}
}
break;
}
}
/*
* Call the destructor and free the object if required.
*/
if (fDestroy)
{
}
return pUsage ? 0 : SUPDRV_ERR_INVALID_PARAM;
}
/**
* Verifies that the current process can access the specified object.
*
* @returns 0 if access is granted.
* @returns SUPDRV_ERR_PERMISSION_DENIED if denied access.
* @returns SUPDRV_ERR_INVALID_PARAM if invalid parameter.
*
* @param pvObj The identifier returned by SUPR0ObjRegister().
* @param pSession The session which wishes to access the object.
* @param pszObjName Object string name. This is optional and depends on the object type.
*
* @remark The caller is responsible for making sure the object isn't removed while
* we're inside this function. If uncertain about this, just call AddRef before calling us.
*/
{
int rc = SUPDRV_ERR_GENERAL_FAILURE;
/*
* Validate the input.
*/
if (!pSession)
{
return SUPDRV_ERR_INVALID_PARAM;
}
{
AssertMsgFailed(("Invalid pvObj=%p magic=%#x (exepcted %#x)\n",
return SUPDRV_ERR_INVALID_PARAM;
}
/*
* Check access. (returns true if a decision has been made.)
*/
return rc;
/*
* Default policy is to allow the user to access his own
* stuff but nothing else.
*/
return 0;
return SUPDRV_ERR_PERMISSION_DENIED;
}
/**
* Lock pages.
*
* @param pSession Session to which the locked memory should be associated.
* @param pvR3 Start of the memory range to lock.
* This must be page aligned.
* @param cb Size of the memory range to lock.
* This must be page aligned.
*/
{
int rc;
SUPDRVMEMREF Mem = {0};
dprintf(("SUPR0LockMem: pSession=%p pvR3=%p cb=%d paPages=%p\n",
/*
* Verify input.
*/
{
return SUPDRV_ERR_INVALID_PARAM;
}
{
return SUPDRV_ERR_INVALID_PARAM;
}
if (!paPages)
{
dprintf(("paPages is NULL!\n"));
return SUPDRV_ERR_INVALID_PARAM;
}
#ifdef USE_NEW_OS_INTERFACE
/*
* Let IPRT do the job.
*/
if (RT_SUCCESS(rc))
{
AssertMsg(RTR0MemObjAddress(Mem.MemObj) == pvR3, ("%p == %p\n", RTR0MemObjAddress(Mem.MemObj), pvR3));
while (iPage-- > 0)
{
{
break;
}
}
if (RT_SUCCESS(rc))
if (RT_FAILURE(rc))
{
}
}
#else /* !USE_NEW_OS_INTERFACE */
/*
* Let the OS specific code have a go.
*/
if (rc)
return rc;
/*
* Everything when fine, add the memory reference to the session.
*/
if (rc)
#endif /* !USE_NEW_OS_INTERFACE */
return rc;
}
/**
* Unlocks the memory pointed to by pv.
*
* @returns 0 on success.
* @returns SUPDRV_ERR_* on failure
* @param pSession Session to which the memory was locked.
* @param pvR3 Memory to unlock.
*/
{
}
/**
* Allocates a chunk of page aligned memory with contiguous and fixed physical
* backing.
*
* @returns 0 on success.
* @returns SUPDRV_ERR_* on failure.
* @param pSession Session data.
* @param cb Number of bytes to allocate.
* @param ppvR0 Where to put the address of Ring-0 mapping the allocated memory.
* @param ppvR3 Where to put the address of Ring-3 mapping the allocated memory.
* @param pHCPhys Where to put the physical address of allocated memory.
*/
SUPR0DECL(int) SUPR0ContAlloc(PSUPDRVSESSION pSession, uint32_t cb, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PRTHCPHYS pHCPhys)
{
int rc;
SUPDRVMEMREF Mem = {0};
dprintf(("SUPR0ContAlloc: pSession=%p cb=%d ppvR0=%p ppvR3=%p pHCPhys=%p\n", pSession, cb, ppvR0, ppvR3, pHCPhys));
/*
* Validate input.
*/
{
dprintf(("Null pointer. All of these should be set: pSession=%p ppvR0=%p ppvR3=%p pHCPhys=%p\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
return SUPDRV_ERR_INVALID_PARAM;
}
#ifdef USE_NEW_OS_INTERFACE
/*
* Let IPRT do the job.
*/
if (RT_SUCCESS(rc))
{
int rc2;
if (RT_SUCCESS(rc))
{
if (!rc)
{
return 0;
}
}
}
#else /* !USE_NEW_OS_INTERFACE */
/*
* Let the OS specific code have a go.
*/
if (rc)
return rc;
("Memory is not page aligned! *ppvR0=%p *ppvR3=%p phys=%VHp\n", ppvR0 ? *ppvR0 : NULL, *ppvR3, *pHCPhys));
/*
* Everything when fine, add the memory reference to the session.
*/
if (rc)
#endif /* !USE_NEW_OS_INTERFACE */
return rc;
}
/**
* Frees memory allocated using SUPR0ContAlloc().
*
* @returns 0 on success.
* @returns SUPDRV_ERR_* on failure.
* @param pSession The session to which the memory was allocated.
* @param uPtr Pointer to the memory (ring-3 or ring-0).
*/
{
}
/**
* Allocates a chunk of page aligned memory with fixed physical backing below 4GB.
*
* @returns 0 on success.
* @returns SUPDRV_ERR_* on failure.
* @param pSession Session data.
* @param cPages Number of pages to allocate.
* @param ppvR0 Where to put the address of Ring-0 mapping of the allocated memory.
* @param ppvR3 Where to put the address of Ring-3 mapping of the allocated memory.
* @param paPages Where to put the physical addresses of allocated memory.
*/
SUPR0DECL(int) SUPR0LowAlloc(PSUPDRVSESSION pSession, uint32_t cPages, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PSUPPAGE paPages)
{
unsigned iPage;
int rc;
SUPDRVMEMREF Mem = {0};
dprintf(("SUPR0LowAlloc: pSession=%p cPages=%d ppvR3=%p ppvR0=%p paPages=%p\n", pSession, cPages, ppvR3, ppvR0, paPages));
/*
* Validate input.
*/
{
dprintf(("Null pointer. All of these should be set: pSession=%p ppvR3=%p ppvR0=%p paPages=%p\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
return SUPDRV_ERR_INVALID_PARAM;
}
#ifdef USE_NEW_OS_INTERFACE
/*
* Let IPRT do the work.
*/
if (RT_SUCCESS(rc))
{
int rc2;
if (RT_SUCCESS(rc))
{
if (!rc)
{
{
}
return 0;
}
}
}
#else /* !USE_NEW_OS_INTERFACE */
/*
* Let the OS specific code have a go.
*/
if (rc)
return rc;
AssertMsg(!((uintptr_t)*ppvR3 & (PAGE_SIZE - 1)), ("Memory is not page aligned! virt=%p\n", *ppvR3));
AssertMsg(!((uintptr_t)*ppvR0 & (PAGE_SIZE - 1)), ("Memory is not page aligned! virt=%p\n", *ppvR0));
/*
* Everything when fine, add the memory reference to the session.
*/
if (rc)
#endif /* !USE_NEW_OS_INTERFACE */
return rc;
}
/**
* Frees memory allocated using SUPR0LowAlloc().
*
* @returns 0 on success.
* @returns SUPDRV_ERR_* on failure.
* @param pSession The session to which the memory was allocated.
* @param uPtr Pointer to the memory (ring-3 or ring-0).
*/
{
}
/**
* Allocates a chunk of memory with both R0 and R3 mappings.
* The memory is fixed and it's possible to query the physical addresses using SUPR0MemGetPhys().
*
* @returns 0 on success.
* @returns SUPDRV_ERR_* on failure.
* @param pSession The session to associated the allocation with.
* @param cb Number of bytes to allocate.
* @param ppvR0 Where to store the address of the Ring-0 mapping.
* @param ppvR3 Where to store the address of the Ring-3 mapping.
*/
{
int rc;
SUPDRVMEMREF Mem = {0};
/*
* Validate input.
*/
{
dprintf(("Null pointer. All of these should be set: pSession=%p ppvR0=%p ppvR3=%p\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
return SUPDRV_ERR_INVALID_PARAM;
}
#ifdef USE_NEW_OS_INTERFACE
/*
* Let IPRT do the work.
*/
if (RT_SUCCESS(rc))
{
int rc2;
if (RT_SUCCESS(rc))
{
if (!rc)
{
return 0;
}
}
}
#else /* !USE_NEW_OS_INTERFACE */
/*
* Let the OS specific code have a go.
*/
if (rc)
return rc;
AssertMsg(!((uintptr_t)*ppvR0 & (PAGE_SIZE - 1)), ("Memory is not page aligned! pvR0=%p\n", *ppvR0));
AssertMsg(!((uintptr_t)*ppvR3 & (PAGE_SIZE - 1)), ("Memory is not page aligned! pvR3=%p\n", *ppvR3));
/*
* Everything when fine, add the memory reference to the session.
*/
if (rc)
#endif /* !USE_NEW_OS_INTERFACE */
return rc;
}
/**
* Get the physical addresses of memory allocated using SUPR0MemAlloc().
*
* @returns 0 on success.
* @returns SUPDRV_ERR_* on failure.
* @param pSession The session to which the memory was allocated.
* @param uPtr The Ring-0 or Ring-3 address returned by SUPR0MemAlloc().
* @param paPages Where to store the physical addresses.
*/
{
/*
* Validate input.
*/
if (!pSession)
{
dprintf(("pSession must not be NULL!"));
return SUPDRV_ERR_INVALID_PARAM;
}
{
return SUPDRV_ERR_INVALID_PARAM;
}
/*
* Search for the address.
*/
{
{
unsigned i;
{
#ifdef USE_NEW_OS_INTERFACE
)
)
{
unsigned iPage;
{
}
return 0;
}
#else /* !USE_NEW_OS_INTERFACE */
{
return 0;
}
#endif
}
}
}
return SUPDRV_ERR_INVALID_PARAM;
}
/**
* Free memory allocated by SUPR0MemAlloc().
*
* @returns 0 on success.
* @returns SUPDRV_ERR_* on failure.
* @param pSession The session owning the allocation.
* @param uPtr The Ring-0 or Ring-3 address returned by SUPR0MemAlloc().
*/
{
}
/**
*
* @returns 0 on success.
* @returns SUPDRV_ERR_* on failure.
* @param pSession Session to which the GIP mapping should belong.
* @param ppGip Where to store the address of the mapping. (optional)
* @param pHCPhysGip Where to store the physical address. (optional)
*
* @remark There is no reference counting on the mapping, so one call to this function
* count globally as one reference. One call to SUPR0GipUnmap() is will unmap GIP
* and remove the session as a GIP user.
*/
SUPR0DECL(int) SUPR0GipMap(PSUPDRVSESSION pSession, PCSUPGLOBALINFOPAGE *ppGip, PRTHCPHYS pHCPhysGid)
{
int rc = 0;
/*
* Validate
*/
if (!ppGip && !pHCPhysGid)
return 0;
{
/*
* Map it?
*/
if (ppGip)
{
#ifdef USE_NEW_OS_INTERFACE
if (RT_SUCCESS(rc))
{
}
#else /* !USE_NEW_OS_INTERFACE */
if (!rc)
#endif /* !USE_NEW_OS_INTERFACE */
}
/*
* Get physical address.
*/
if (pHCPhysGid && !rc)
/*
* Reference globally.
*/
{
{
unsigned i;
dprintf(("SUPR0GipMap: Resumes GIP updating\n"));
ASMAtomicXchgU32(&pGip->aCPUs[i].u32TransactionId, pGip->aCPUs[i].u32TransactionId & ~(GIP_UPDATEHZ_RECALC_FREQ * 2 - 1));
#ifdef USE_NEW_OS_INTERFACE
#else
#endif
}
}
}
else
{
dprintf(("SUPR0GipMap: GIP is not available!\n"));
}
/*
* Write returns.
*/
if (pHCPhysGid)
*pHCPhysGid = HCPhys;
if (ppGip)
return rc;
}
/**
* Unmaps any user mapping of the GIP and terminates all GIP access
* from this session.
*
* @returns 0 on success.
* @returns SUPDRV_ERR_* on failure.
* @param pSession Session to which the GIP mapping should belong.
*/
{
int rc = 0;
/*
* Unmap anything?
*/
#ifdef USE_NEW_OS_INTERFACE
{
if (RT_SUCCESS(rc))
}
#else
{
if (!rc)
}
#endif
/*
* Dereference global GIP.
*/
{
pSession->fGipReferenced = 0;
{
dprintf(("SUPR0GipUnmap: Suspends GIP updating\n"));
#ifdef USE_NEW_OS_INTERFACE
#else
#endif
}
}
return rc;
}
/**
* Adds a memory object to the session.
*
* @returns 0 on success.
* @returns SUPDRV_ERR_* on failure.
* @param pMem Memory tracking structure containing the
* information to track.
* @param pSession The session.
*/
{
/*
* Find free entry and record the allocation.
*/
{
{
unsigned i;
{
#ifdef USE_NEW_OS_INTERFACE
#else /* !USE_NEW_OS_INTERFACE */
#endif /* !USE_NEW_OS_INTERFACE */
{
return 0;
}
}
AssertFailed(); /* !!this can't be happening!!! */
}
}
/*
* Need to allocate a new bundle.
* Insert into the last entry in the bundle.
*/
if (!pBundle)
return SUPDRV_ERR_NO_MEMORY;
/* take last entry. */
/* insert into list. */
return 0;
}
/**
* Releases a memory object referenced by pointer and type.
*
* @returns 0 on success.
* @returns SUPDRV_ERR_INVALID_PARAM on failure.
* @param pSession Session data.
* @param uPtr Pointer to memory. This is matched against both the R0 and R3 addresses.
* @param eType Memory type.
*/
{
/*
* Validate input.
*/
if (!pSession)
{
dprintf(("pSession must not be NULL!"));
return SUPDRV_ERR_INVALID_PARAM;
}
if (!uPtr)
{
return SUPDRV_ERR_INVALID_PARAM;
}
/*
* Search for the address.
*/
{
{
unsigned i;
{
#ifdef USE_NEW_OS_INTERFACE
)
{
/* Make a copy of it and release it outside the spinlock. */
{
}
{
}
return 0;
}
#else /* !USE_NEW_OS_INTERFACE */
{
/* Make a copy of it and release it outside the spinlock. */
/* Type specific free operation. */
{
case MEMREF_TYPE_LOCKED:
break;
case MEMREF_TYPE_CONT:
break;
case MEMREF_TYPE_LOW:
break;
case MEMREF_TYPE_MEM:
break;
default:
case MEMREF_TYPE_UNUSED:
break;
}
return 0;
}
#endif /* !USE_NEW_OS_INTERFACE */
}
}
}
return SUPDRV_ERR_INVALID_PARAM;
}
#ifndef VBOX_WITHOUT_IDT_PATCHING
/**
* Install IDT for the current CPU.
*
* @returns 0 on success.
* @returns SUPDRV_ERR_NO_MEMORY or SUPDRV_ERROR_IDT_FAILED on failure.
* @param pIn Input data.
* @param pOut Output data.
*/
static int supdrvIOCtl_IdtInstall(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPIDTINSTALL_IN pIn, PSUPIDTINSTALL_OUT pOut)
{
dprintf(("supdrvIOCtl_IdtInstall\n"));
/*
* Preallocate entry for this CPU cause we don't wanna do
* that inside the spinlock!
*/
if (!pUsagePre)
return SUPDRV_ERR_NO_MEMORY;
/*
* Take the spinlock and see what we need to do.
*/
/* check if we already got a free patch. */
if (!pDevExt->pIdtPatchesFree)
{
/*
* Allocate a patch - outside the spinlock of course.
*/
if (!pPatchPre)
return SUPDRV_ERR_NO_MEMORY;
}
else
{
}
/* look for matching patch entry */
ASMGetIDTR(&Idtr);
if (!pPatch)
{
/*
* Create patch.
*/
if (pPatch)
}
else
{
/*
* Simply increment patch usage.
*/
}
if (pPatch)
{
/*
* Increment and add if need be the session usage record for this patch.
*/
if (!pUsage)
{
/*
* Add usage record.
*/
}
else
{
/*
* Increment usage count.
*/
}
}
/* free patch - we accumulate them for paranoid saftly reasons. */
if (pPatchPre)
{
}
/*
* Free unused preallocated buffers.
*/
if (pUsagePre)
return pPatch ? 0 : SUPDRV_ERR_IDT_FAILED;
}
/**
* This creates a IDT patch entry.
* If the first patch being installed it'll also determin the IDT entry
* to use.
*
* @returns pPatch on success.
* @returns NULL on failure.
* @param pDevExt Pointer to globals.
* @param pPatch Patch entry to use.
* This will be linked into SUPDRVDEVEXT::pIdtPatches on
* successful return.
* @remark Call must be owning the SUPDRVDEVEXT::Spinlock!
*/
{
/*
* Get IDT.
*/
ASMGetIDTR(&Idtr);
{
return NULL;
}
{
/*
* Test out the alternatives.
*
* At the moment we do not support chaining thus we ASSUME that one of
* these 48 entries is unused (which is not a problem on Win32 and
* Linux to my knowledge).
*/
/** @todo we MUST change this detection to try grab an entry which is NOT in use. This can be
* combined with gathering info about which guest system call gates we can hook up directly. */
unsigned i;
{
#ifdef __WIN__ /* We don't use 0xef and above because they are system stuff on linux (ef is IPI,
* local apic timer, or some other frequently fireing thing). */
0xef, 0xee, 0xed, 0xec,
#endif
0xeb, 0xea, 0xe9, 0xe8,
0xdf, 0xde, 0xdd, 0xdc,
0x7b, 0x7a, 0x79, 0x78,
0xbf, 0xbe, 0xbd, 0xbc,
};
static int s_iWobble = 0;
{
dprintf(("%#x: %04x:%08x%04x%04x P=%d DPL=%d IST=%d Type1=%#x u32Reserved=%#x u5Reserved=%#x\n",
}
#endif
/* look for entries which are not present or otherwise unused. */
{
break;
u8Idt = 0;
}
if (!u8Idt)
{
/* try again, look for a compatible entry .*/
{
break;
u8Idt = 0;
}
if (!u8Idt)
{
dprintf(("Failed to find appropirate IDT entry!!\n"));
return NULL;
}
}
}
/*
* Prepare the patch
*/
#ifdef __AMD64__
#endif
#ifdef __AMD64__
#else /* x86 */
#endif /* x86 */
/*
* Generate the patch code.
*/
{
#ifdef __AMD64__
union
{
/* check the cookie */
*u.pb++ = 2;
/* jump to forwarder code. */
*u.pb++ = 0xeb;
uFixJmp = u;
*u.pb++ = 0xfe;
// @VBoxCall:
*u.pb++ = 0x01;
*u.pb++ = 0xf8;
/*
* Call VMMR0Entry
* We don't have to push the arguments here, but we have to
* reserve some stack space for the interrupt forwarding.
*/
# ifdef __WIN__
*u.pb++ = 0x50;
# else
# endif
*u.pb++ = 0x15;
uFixCall = u;
*u.pu32++ = 0;
*u.pb++ = 0x81;
*u.pb++ = 0xc4;
*u.pu32++ = 0x20;
*u.pb++ = 0x01;
*u.pb++ = 0xf8;
/* Return to R3. */
uNotNested = u;
*u.pb++ = 0xcf;
*u.pb++ = 0xcc;
/* Pointer to the VMMR0Entry. */ // pfnVMMR0Entry dq StubVMMR0Entry
/* stub entry. */ // StubVMMR0Entry:
*u.pb++ = 0xc0;
*u.pb++ = 0xff;
*u.pb++ = 0xc8;
/* forward to the original handler using a retf. */
*u.pb++ = 0x44;
*u.pb++ = 0x24;
*u.pb++ = 0x04;
*u.pb++ = 0xcb;
#else /* __X86__ */
union
{
/* check the cookie */
*u.pb++ = 0xfe;
uFixJmp = u;
*u.pb++ = 0;
/* jump (far) to the original handler / not-nested-stub. */
uFixJmpNotNested = u;
*u.pu32++ = 0;
*u.pu16++ = 0;
/* save selector registers. */ // VBoxCall:
*u.pb++ = 0xa0;
/* call frame */
/* load ds, es and perhaps fs before call. */
*u.pb++ = 0xd8;
*u.pb++ = 0xc0;
#ifdef __WIN__
*u.pb++ = 0xe0;
#endif
/* do the call. */
uFixCall = u;
*u.pu32++ = 0xfffffffb;
*u.pb++ = 0xc4;
*u.pb++ = 0x0c;
/* restore selector registers. */
//
*u.pb++ = 0xa1;
uNotNested = u; // NotNested:
/* the stub VMMR0Entry. */ // StubVMMR0Entry:
*u.pb++ = 0xc0;
/* Fixup the VMMR0Entry call. */
else
/* Fixup the forward / nested far jump. */
{
}
else
{
*uFixJmpNotNested.pu32++ = ((uint32_t)pPatch->SavedIdt.u16OffsetHigh << 16) | pPatch->SavedIdt.u16OffsetLow;
}
#endif /* __X86__ */
#if 0
/* dump the patch code */
#endif
}
/*
* Install the patch.
*/
AssertMsg(!memcmp((void *)pPatch->pIdtEntry, &pPatch->ChangedIdt, sizeof(pPatch->ChangedIdt)), ("The stupid change code didn't work!!!!!\n"));
/*
* Link in the patch.
*/
return pPatch;
}
/**
* Removes the sessions IDT references.
* This will uninstall our IDT patch if we left unreferenced.
*
* @returns 0 indicating success.
* @param pDevExt Device globals.
* @param pSession Session data.
*/
{
/*
* Take the spinlock.
*/
/*
* Walk usage list.
*/
while (pUsage)
{
else
/* next */
}
/*
* Empty the usage chain and we're done inside the spinlock.
*/
/*
* Free usage entries.
*/
while (pUsage)
{
}
return 0;
}
/**
* Remove one patch.
*
* @param pDevExt Device globals.
* @param pPatch Patch entry to remove.
* @remark Caller must own SUPDRVDEVEXT::Spinlock!
*/
{
/*
* If the IDT entry was changed it have to kick around for ever!
* This will be attempted freed again, perhaps next time we'll succeed :-)
*/
{
AssertMsgFailed(("The hijacked IDT entry has CHANGED!!!\n"));
return;
}
/*
* Unlink it.
*/
{
while (pPatchPrev)
{
{
break;
}
}
Assert(!pPatchPrev);
}
else
/*
* Verify and restore the IDT.
*/
AssertMsg(!memcmp((void *)pPatch->pIdtEntry, &pPatch->ChangedIdt, sizeof(pPatch->ChangedIdt)), ("The hijacked IDT entry has CHANGED!!!\n"));
AssertMsg(!memcmp((void *)pPatch->pIdtEntry, &pPatch->SavedIdt, sizeof(pPatch->SavedIdt)), ("The hijacked IDT entry has CHANGED!!!\n"));
/*
* Put it in the free list.
* (This free list stuff is to calm my paranoia.)
*/
}
/**
* Write to an IDT entry.
*
* @param pvIdtEntry Where to write.
* @param pNewIDTEntry What to write.
*/
{
/*
* On SMP machines (P4 hyperthreading included) we must preform a
* 64-bit locked write when updating the IDT entry.
*
* The F00F bugfix for linux (and probably other OSes) causes
* the IDT to be pointing to an readonly mapping. We get around that
* by temporarily turning of WP. Since we're inside a spinlock at this
* point, interrupts are disabled and there isn't any way the WP bit
* flipping can cause any trouble.
*/
/* Save & Clear interrupt flag; Save & clear WP. */
uFlags = ASMGetFlags();
/* Update IDT Entry */
#ifdef __AMD64__
#else
#endif
/* Restore CR0 & Flags */
}
#endif /* !VBOX_WITHOUT_IDT_PATCHING */
/**
* Opens an image. If it's the first time it's opened the call must upload
* the bits using the supdrvIOCtl_LdrLoad() / SUPDRV_IOCTL_LDR_LOAD function.
*
* This is the 1st step of the loading.
*
* @returns 0 on success.
* @returns SUPDRV_ERR_* on failure.
* @param pDevExt Device globals.
* @param pSession Session data.
* @param pIn Input.
* @param pOut Output. (May overlap pIn.)
*/
static int supdrvIOCtl_LdrOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDROPEN_IN pIn, PSUPLDROPEN_OUT pOut)
{
unsigned cb;
void *pv;
/*
* Check if we got an instance of the image already.
*/
{
{
return 0;
}
}
/* (not found - add it!) */
/*
* Allocate memory.
*/
if (!pv)
{
return SUPDRV_ERR_NO_MEMORY;
}
/*
* Setup and link in the LDR stuff.
*/
return 0;
}
/**
* Loads the image bits.
*
* This is the 2nd step of the loading.
*
* @returns 0 on success.
* @returns SUPDRV_ERR_* on failure.
* @param pDevExt Device globals.
* @param pSession Session data.
* @param pIn Input.
*/
{
int rc;
/*
* Find the ldr image.
*/
if (!pUsage)
{
dprintf(("SUP_IOCTL_LDR_LOAD: couldn't find image!\n"));
return SUPDRV_ERR_INVALID_HANDLE;
}
{
dprintf(("SUP_IOCTL_LDR_LOAD: image size mismatch!! %d(prep) != %d(load)\n", pImage->cbImage, pIn->cbImage));
return SUPDRV_ERR_INVALID_HANDLE;
}
{
if (uState != SUP_IOCTL_LDR_LOAD)
return SUPDRV_ERR_ALREADY_LOADED;
}
{
case EP_NOTHING:
break;
case EP_VMMR0:
{
dprintf(("pvVMMR0=%p or pIn->EP.VMMR0.pvVMMR0Entry=%p is NULL!\n",
return SUPDRV_ERR_INVALID_PARAM;
}
{
dprintf(("SUP_IOCTL_LDR_LOAD: pvVMMR0Entry=%p is outside the image (%p %d bytes)\n",
return SUPDRV_ERR_INVALID_PARAM;
}
break;
default:
return SUPDRV_ERR_INVALID_PARAM;
}
if ( pIn->pfnModuleInit
{
dprintf(("SUP_IOCTL_LDR_LOAD: pfnModuleInit=%p is outside the image (%p %d bytes)\n",
return SUPDRV_ERR_INVALID_PARAM;
}
if ( pIn->pfnModuleTerm
{
dprintf(("SUP_IOCTL_LDR_LOAD: pfnModuleTerm=%p is outside the image (%p %d bytes)\n",
return SUPDRV_ERR_INVALID_PARAM;
}
/*
* Copy the memory.
*/
/*
* Update any entry points.
*/
{
default:
case EP_NOTHING:
rc = 0;
break;
case EP_VMMR0:
break;
}
/*
* On success call the module initialization.
*/
{
}
if (rc)
return rc;
}
/**
* Frees a previously loaded (prep'ed) image.
*
* @returns 0 on success.
* @returns SUPDRV_ERR_* on failure.
* @param pDevExt Device globals.
* @param pSession Session data.
* @param pIn Input.
*/
{
/*
* Find the ldr image.
*/
pUsagePrev = NULL;
{
pUsagePrev = pUsage;
}
if (!pUsage)
{
dprintf(("SUP_IOCTL_LDR_FREE: couldn't find image!\n"));
return SUPDRV_ERR_INVALID_HANDLE;
}
/*
* Check if we can remove anything.
*/
{
/* unlink it */
if (pUsagePrev)
else
/* free it */
/*
* Derefrence the image.
*/
else
}
else
{
/*
* Dereference both image and usage.
*/
}
return 0;
}
/**
* Gets the address of a symbol in an open image.
*
* @returns 0 on success.
* @returns SUPDRV_ERR_* on failure.
* @param pDevExt Device globals.
* @param pSession Session data.
* @param pIn Input.
* @param pOut Output. (May overlap pIn.)
*/
static int supdrvIOCtl_LdrGetSymbol(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDRGETSYMBOL_IN pIn, PSUPLDRGETSYMBOL_OUT pOut)
{
uint32_t i;
const char *pchStrings;
dprintf2(("supdrvIOCtl_LdrGetSymbol: pvImageBase=%p szSymbol=\"%s\"\n", pIn->pvImageBase, pIn->szSymbol));
/*
* Find the ldr image.
*/
if (!pUsage)
{
dprintf(("SUP_IOCTL_LDR_GET_SYMBOL: couldn't find image!\n"));
return SUPDRV_ERR_INVALID_HANDLE;
}
{
dprintf(("SUP_IOCTL_LDR_GET_SYMBOL: invalid image state %d (%#x)!\n", uState, uState)); NOREF(uState);
return SUPDRV_ERR_ALREADY_LOADED;
}
/*
* Search the symbol string.
*/
{
{
rc = 0;
break;
}
}
return rc;
}
/**
* Updates the IDT patches to point to the specified VMM R0 entry
* point (i.e. VMMR0Enter()).
*
* @returns 0 on success.
* @returns SUPDRV_ERR_* on failure.
* @param pDevExt Device globals.
* @param pSession Session data.
* @param pVMMR0 VMMR0 image handle.
* @param pVMMR0Entry VMMR0Entry address.
* @remark Caller must own the loader mutex.
*/
{
int rc;
/*
* Check if not yet set.
*/
rc = 0;
{
#ifndef VBOX_WITHOUT_IDT_PATCHING
#endif
/*
* Set it and update IDT patch code.
*/
#ifndef VBOX_WITHOUT_IDT_PATCHING
{
# ifdef __AMD64__
ASMAtomicXchgU64((volatile uint64_t *)&pPatch->auCode[pPatch->offVMMR0EntryFixup], (uint64_t)pvVMMR0);
# else /* __X86__ */
# endif
}
#endif /* !VBOX_WITHOUT_IDT_PATCHING */
}
else
{
/*
* Return failure or success depending on whether the
* values match or not.
*/
{
AssertMsgFailed(("SUP_IOCTL_LDR_SETR0EP: Already set pointing to a different module!\n"));
}
}
return rc;
}
/**
* Unsets the R0 entry point installed by supdrvLdrSetR0EP.
*
* @param pDevExt Device globals.
*/
{
#ifndef VBOX_WITHOUT_IDT_PATCHING
#endif
#ifndef VBOX_WITHOUT_IDT_PATCHING
{
# ifdef __AMD64__
# else /* __X86__ */
(uint32_t)&pPatch->auCode[pPatch->offStub] - (uint32_t)&pPatch->auCode[pPatch->offVMMR0EntryFixup + 4]);
# endif
}
#endif /* !VBOX_WITHOUT_IDT_PATCHING */
}
/**
* Adds a usage reference in the specified session of an image.
*
* @param pSession Session in question.
* @param pImage Image which the session is using.
*/
{
/*
* Referenced it already?
*/
while (pUsage)
{
{
return;
}
}
/*
* Allocate new usage record.
*/
if (pUsage)
{
}
/* ignore errors... */
}
/**
* Frees a load image.
*
* @param pDevExt Pointer to device extension.
* @param pImage Pointer to the image we're gonna free.
* This image must exit!
* @remark The caller MUST own SUPDRVDEVEXT::mtxLdr!
*/
{
/* find it - arg. should've used doubly linked list. */
pImagePrev = NULL;
{
}
/* unlink */
if (pImagePrev)
else
/* check if this is VMMR0.r0 and fix the Idt patches if it is. */
/* call termination function if fully loaded. */
if ( pImage->pfnModuleTerm
{
pImage->pfnModuleTerm();
}
/* free the image */
}
/**
* Gets the current paging mode of the CPU and stores in in pOut.
*/
{
else
{
uint32_t fNXEPlusLMA = 0;
if (cr4 & X86_CR4_PAE)
{
{
fNXEPlusLMA |= BIT(0);
}
}
{
case 0:
break;
case X86_CR4_PGE:
break;
case X86_CR4_PAE:
break;
case X86_CR4_PAE | BIT(0):
break;
case X86_CR4_PAE | X86_CR4_PGE:
break;
break;
break;
break;
break;
break;
default:
break;
}
}
return 0;
}
#if !defined(SUPDRV_OS_HAVE_LOW) && !defined(USE_NEW_OS_INTERFACE) /* Use same backend as the contiguous stuff */
/**
* OS Specific code for allocating page aligned memory with fixed
* physical backing below 4GB.
*
* @returns 0 on success.
* @returns SUPDRV_ERR_* on failure.
* @param pMem Memory reference record of the memory to be allocated.
* (This is not linked in anywhere.)
* @param ppvR3 Where to store the Ring-0 mapping of the allocated memory.
* @param ppvR3 Where to store the Ring-3 mapping of the allocated memory.
* @param paPagesOut Where to store the physical addresss.
*/
int VBOXCALL supdrvOSLowAllocOne(PSUPDRVMEMREF pMem, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PSUPPAGE paPagesOut)
{
if (!rc)
{
while (iPage-- > 0)
{
}
}
return rc;
}
/**
* Frees low memory.
*
* @param pMem Memory reference record of the memory to be freed.
*/
{
}
#endif /* !SUPDRV_OS_HAVE_LOW */
#ifdef USE_NEW_OS_INTERFACE
/**
* Creates the GIP.
*
* @returns negative errno.
* @param pDevExt Instance data. GIP stuff may be updated.
*/
{
int rc;
dprintf(("supdrvGipCreate:\n"));
/* assert order */
/*
* Allocate a suitable page with a default kernel mapping.
*/
if (RT_FAILURE(rc))
{
return rc;
}
/*
* Try bump up the system timer resolution.
* The more interrupts the better...
*/
)
{
}
/*
* Find a reasonable update interval, something close to 10ms would be nice,
* and create a recurring timer.
*/
if (RT_FAILURE(rc))
{
OSDBGPRINT(("supdrvGipCreate: failed create GIP timer at %RU32 ns interval. rc=%d\n", u32Interval, rc));
return rc;
}
/*
* We're good.
*/
return 0;
}
/**
* Terminates the GIP.
*
* @returns negative errno.
* @param pDevExt Instance data. GIP stuff may be updated.
*/
{
int rc;
/*
* Invalid the GIP data.
*/
{
}
/*
* Destroy the timer and free the GIP memory object.
*/
{
}
{
}
/*
* Finally, release the system timer resolution request if one succeeded.
*/
{
}
return 0;
}
/**
* Timer callback function.
* @param pTimer The timer.
* @param pvUser The device extension.
*/
{
}
#endif /* USE_NEW_OS_INTERFACE */
/**
* Initializes the GIP data.
*
* @returns VBox status code.
* @param pDevExt Pointer to the device instance data.
* @param pGip Pointer to the read-write kernel mapping of the GIP.
* @param HCPhys The physical address of the GIP.
* @param u64NanoTS The current nanosecond timestamp.
* @param uUpdateHz The update freqence.
*/
int VBOXCALL supdrvGipInit(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE pGip, RTHCPHYS HCPhys, uint64_t u64NanoTS, unsigned uUpdateHz)
{
unsigned i;
dprintf(("supdrvGipInit: pGip=%p HCPhys=%lx u64NanoTS=%llu uUpdateHz=%d\n", pGip, (long)HCPhys, u64NanoTS, uUpdateHz));
/*
* Initialize the structure.
*/
{
/*
* We don't know the following values until we've executed updates.
* So, we'll just insert very high values.
*/
}
/*
* Link it to the device extension.
*/
return 0;
}
/**
* Determin the GIP TSC mode.
*
* @returns The most suitable TSC mode.
*/
static SUPGIPMODE supdrvGipDeterminTscMode(void)
{
#ifndef USE_NEW_OS_INTERFACE
if (supdrvOSGetCPUCount() > 1)
{
/* Check for "AuthenticAMD" */
{
/* Check for family 15 and the RDTSCP feature - hope that's is sufficient. */
/* r=frank: The test for TscInvariant should be sufficient */
{
/* Check the power specs for Advanced Power Management Information */
if (uEAX < 0x80000007)
return SUPGIPMODE_ASYNC_TSC;
/* TscInvariant 1=The TSC rate is ensured to be invariant across all P-States,
* C-States, and stop-grant transitions (such as STPCLK Throttling); therefore
* the TSC is suitable for use as a source of time. 0=No such guarantee is made
* and software should avoid attempting to use the TSC as a source of time. */
return SUPGIPMODE_ASYNC_TSC;
}
}
}
#endif
return SUPGIPMODE_SYNC_TSC;
}
/**
* Invalidates the GIP data upon termination.
*
* @param pGip Pointer to the read-write kernel mapping of the GIP.
*/
{
unsigned i;
{
}
}
/**
* Worker routine for supdrvGipUpdate and supdrvGipUpdatePerCpu that
* updates all the per cpu data except the transaction id.
*
* @param pGip The GIP.
* @param pGipCpu Pointer to the per cpu data.
* @param u64NanoTS The current time stamp.
*/
{
unsigned iTSCHistoryHead;
/*
* Update the NanoTS.
*/
/*
* Calc TSC delta.
*/
/** @todo validate the NanoTS delta, don't trust the OS to call us when it should... */
u64TSC = ASMReadTSC();
if (u64TSCDelta >> 32)
{
}
/*
* TSC History.
*/
/*
* UpdateIntervalTSC = average of last 8,2,1 intervals depending on update HZ.
*/
{
u32 >>= 2;
u32UpdateIntervalTSC >>= 2;
u32UpdateIntervalTSC >>= 1;
/* Value choosen for a 2GHz Athlon64 running linux 2.6.10/11, . */
}
{
u32UpdateIntervalTSC >>= 1;
/* value choosen on a 2GHz thinkpad running windows */
}
else
{
/* This value hasn't be checked yet.. waiting for OS/2 and 33Hz timers.. :-) */
}
/*
* CpuHz.
*/
}
/**
* Updates the GIP.
*
* @param pGip Pointer to the GIP.
* @param u64NanoTS The current nanosecond timesamp.
*/
{
/*
* Determin the relevant CPU data.
*/
else
{
unsigned iCpu = ASMGetApicId();
return;
}
/*
* Start update transaction.
*/
{
/* this can happen on win32 if we're taking to long and there are more CPUs around. shouldn't happen though. */
return;
}
/*
* Recalc the update frequency every 0x800th time.
*/
{
if (pGip->u64NanoTSLastUpdateHz)
{
#ifdef __AMD64__ /** @todo fix 64-bit div here to work on x86 linux. */
{
}
#endif
}
}
/*
* Update the data.
*/
/*
* Complete transaction.
*/
}
/**
* Updates the per cpu GIP data for the calling cpu.
*
* @param pGip Pointer to the GIP.
* @param u64NanoTS The current nanosecond timesamp.
* @param iCpu The CPU index.
*/
{
{
/*
* Start update transaction.
*/
{
return;
}
/*
* Update the data.
*/
/*
* Complete transaction.
*/
}
}
#ifndef DEBUG /** @todo change #ifndef DEBUG -> #ifdef LOG_ENABLED */
/**
* Stub function for non-debug builds.
*/
{
return NULL;
}
{
return NULL;
}
/**
* Stub function for non-debug builds.
*/
{
return 0;
}
/**
* Stub function for non-debug builds.
*/
{
}
/**
* Stub function for non-debug builds.
*/
RTDECL(void) RTLogLoggerEx(PRTLOGGER pLogger, unsigned fFlags, unsigned iGroup, const char *pszFormat, ...)
{
}
/**
* Stub function for non-debug builds.
*/
RTDECL(void) RTLogLoggerExV(PRTLOGGER pLogger, unsigned fFlags, unsigned iGroup, const char *pszFormat, va_list args)
{
}
#endif /* !DEBUG */