SUPDrv.c revision f5dd1b19e27ce8cd10e3646cc919d74e433c8df9
/* $Revision$ */
/** @file
* VBoxDrv - The VirtualBox Support Driver - Common code.
*/
/*
* 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.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* 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_SUP_DRV
#include "SUPDrvInternal.h"
#ifndef PAGE_SHIFT
#endif
#include <iprt/semaphore.h>
#include <iprt/spinlock.h>
#if defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS)
#endif
#ifdef RT_OS_LINUX
# define X86_CPUID_VENDOR_AMD_EBX 0x68747541
# define X86_CPUID_VENDOR_AMD_ECX 0x444d4163
# define X86_CPUID_VENDOR_AMD_EDX 0x69746e65
#else
#endif
/*
* Logging assignments:
* Log - useful stuff, like failures.
* LogFlow - program flow, except the really noisy bits.
* Log2 - Cleanup.
* Log3 - Loader flow noise.
* Log4 - Call VMMR0 flow noise.
* Log5 - Native yet-to-be-defined noise.
* Log6 - Native ioctl flow noise.
*
* Logging requires BUILD_TYPE=debug and possibly changes to the logger
* instanciation in log-vbox.c(pp).
*/
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/* from x86.h - clashes with linux thus this duplication */
#define X86_CR0_PE RT_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
/**
* Validates a session pointer.
*
* @param pSession The session.
*/
#define SUP_IS_SESSION_VALID(pSession) \
/** @def VBOX_SVN_REV
* The makefile should define this if it can. */
#ifndef VBOX_SVN_REV
# define VBOX_SVN_REV 0
#endif
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static int supdrvIOCtl_LdrGetSymbol(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDRGETSYMBOL pReq);
static int supdrvIDC_LdrGetSymbol(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVIDCREQGETSYM pReq);
static int supdrvLdrSetVMMR0EPs(PSUPDRVDEVEXT pDevExt, void *pvVMMR0, void *pvVMMR0EntryInt, void *pvVMMR0EntryFast, void *pvVMMR0EntryEx);
static int supdrvIOCtl_CallServiceModule(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPCALLSERVICE pReq);
#ifdef RT_OS_WINDOWS
static int supdrvPageGetPhys(PSUPDRVSESSION pSession, RTR3PTR pvR3, uint32_t cPages, PRTHCPHYS paPages);
#endif /* RT_OS_WINDOWS */
#ifdef RT_WITH_W64_UNWIND_HACK
DECLASM(int) supdrvNtWrapVMMR0EntryEx(PFNRT pfnVMMR0EntryEx, PVM pVM, unsigned uOperation, PSUPVMMR0REQHDR pReq, uint64_t u64Arg, PSUPDRVSESSION pSession);
DECLASM(int) supdrvNtWrapVMMR0EntryFast(PFNRT pfnVMMR0EntryFast, PVM pVM, unsigned idCpu, unsigned uOperation);
DECLASM(void) supdrvNtWrapObjDestructor(PFNRT pfnDestruction, void *pvObj, void *pvUser1, void *pvUser2);
DECLASM(void *) supdrvNtWrapQueryFactoryInterface(PFNRT pfnQueryFactoryInterface, struct SUPDRVFACTORY const *pSupDrvFactory, PSUPDRVSESSION pSession, const char *pszInterfaceUuid);
DECLASM(int) supdrvNtWrapServiceReqHandler(PFNRT pfnServiceReqHandler, PSUPDRVSESSION pSession, uint32_t uOperation, uint64_t u64Arg, PSUPR0SERVICEREQHDR pReqHdr);
DECLASM(int) UNWIND_WRAP(SUPR0ComponentRegisterFactory)(PSUPDRVSESSION pSession, PCSUPDRVFACTORY pFactory);
DECLASM(int) UNWIND_WRAP(SUPR0ComponentDeregisterFactory)(PSUPDRVSESSION pSession, PCSUPDRVFACTORY pFactory);
DECLASM(int) UNWIND_WRAP(SUPR0ComponentQueryFactory)(PSUPDRVSESSION pSession, const char *pszName, const char *pszInterfaceUuid, void **ppvFactoryIf);
DECLASM(void *) UNWIND_WRAP(SUPR0ObjRegister)(PSUPDRVSESSION pSession, SUPDRVOBJTYPE enmType, PFNSUPDRVDESTRUCTOR pfnDestructor, void *pvUser1, void *pvUser2);
DECLASM(int) UNWIND_WRAP(SUPR0ObjVerifyAccess)(void *pvObj, PSUPDRVSESSION pSession, const char *pszObjName);
DECLASM(int) UNWIND_WRAP(SUPR0LockMem)(PSUPDRVSESSION pSession, RTR3PTR pvR3, uint32_t cPages, PRTHCPHYS paPages);
DECLASM(int) UNWIND_WRAP(SUPR0ContAlloc)(PSUPDRVSESSION pSession, uint32_t cPages, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PRTHCPHYS pHCPhys);
DECLASM(int) UNWIND_WRAP(SUPR0LowAlloc)(PSUPDRVSESSION pSession, uint32_t cPages, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PRTHCPHYS paPages);
DECLASM(int) UNWIND_WRAP(SUPR0MemAlloc)(PSUPDRVSESSION pSession, uint32_t cb, PRTR0PTR ppvR0, PRTR3PTR ppvR3);
DECLASM(int) UNWIND_WRAP(SUPR0MemGetPhys)(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr, PSUPPAGE paPages);
DECLASM(int) UNWIND_WRAP(SUPR0PageAlloc)(PSUPDRVSESSION pSession, uint32_t cPages, PRTR3PTR ppvR3, PRTHCPHYS paPages);
//DECLASM(int) UNWIND_WRAP(SUPR0Printf)(const char *pszFormat, ...);
DECLASM(void *) UNWIND_WRAP(RTMemDupEx)(const void *pvSrc, size_t cbSrc, size_t cbExtra) RT_NO_THROW;
DECLASM(int) UNWIND_WRAP(RTR0MemObjAllocPhys)(PRTR0MEMOBJ pMemObj, size_t cb, RTHCPHYS PhysHighest);
DECLASM(int) UNWIND_WRAP(RTR0MemObjAllocPhysNC)(PRTR0MEMOBJ pMemObj, size_t cb, RTHCPHYS PhysHighest);
DECLASM(int) UNWIND_WRAP(RTR0MemObjLockUser)(PRTR0MEMOBJ pMemObj, RTR3PTR R3Ptr, size_t cb, RTR0PROCESS R0Process);
DECLASM(int) UNWIND_WRAP(RTR0MemObjMapKernel)(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, void *pvFixed, size_t uAlignment, unsigned fProt);
DECLASM(int) UNWIND_WRAP(RTR0MemObjMapKernelEx)(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, void *pvFixed, size_t uAlignment, unsigned fProt, size_t offSub, size_t cbSub);
DECLASM(int) UNWIND_WRAP(RTR0MemObjMapUser)(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process);
/*DECLASM(void *) UNWIND_WRAP(RTR0MemObjAddress)(RTR0MEMOBJ MemObj); - not necessary */
/*DECLASM(RTR3PTR) UNWIND_WRAP(RTR0MemObjAddressR3)(RTR0MEMOBJ MemObj); - not necessary */
/*DECLASM(size_t) UNWIND_WRAP(RTR0MemObjSize)(RTR0MEMOBJ MemObj); - not necessary */
/*DECLASM(bool) UNWIND_WRAP(RTR0MemObjIsMapping)(RTR0MEMOBJ MemObj); - not necessary */
/*DECLASM(RTHCPHYS) UNWIND_WRAP(RTR0MemObjGetPagePhysAddr)(RTR0MEMOBJ MemObj, size_t iPage); - not necessary */
/* RTProcSelf - not necessary */
/* RTR0ProcHandleSelf - not necessary */
DECLASM(int) UNWIND_WRAP(RTSemEventMultiWaitNoResume)(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies);
/* RTTimeNanoTS - not necessary */
/* RTTimeMilliTS - not necessary */
/* RTTimeSystemNanoTS - not necessary */
/* RTTimeSystemMilliTS - not necessary */
/* RTThreadNativeSelf - not necessary */
#if 0
/* RTThreadSelf - not necessary */
DECLASM(int) UNWIND_WRAP(RTThreadCreate)(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
#endif
/* RTLogDefaultInstance - a bit of a gamble, but we do not want the overhead! */
/* RTMpCpuId - not necessary */
/* RTMpCpuIdFromSetIndex - not necessary */
/* RTMpCpuIdToSetIndex - not necessary */
/* RTMpIsCpuPossible - not necessary */
/* RTMpGetCount - not necessary */
/* RTMpGetMaxCpuId - not necessary */
/* RTMpGetOnlineCount - not necessary */
/* RTMpGetOnlineSet - not necessary */
/* RTMpGetSet - not necessary */
/* RTMpIsCpuOnline - not necessary */
DECLASM(int) UNWIND_WRAP(RTMpOnSpecific)(RTCPUID idCpu, PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2);
/* RTLogRelDefaultInstance - not necessary. */
/* RTLogLogger - can't wrap this buster. */
/* RTLogLoggerEx - can't wrap this buster. */
DECLASM(void) UNWIND_WRAP(RTLogLoggerExV)(PRTLOGGER pLogger, unsigned fFlags, unsigned iGroup, const char *pszFormat, va_list args);
/* RTLogPrintf - can't wrap this buster. */ /** @todo provide va_list log wrappers in RuntimeR0. */
DECLASM(void) UNWIND_WRAP(AssertMsg1)(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction);
/* AssertMsg2 - can't wrap this buster. */
#endif /* RT_WITH_W64_UNWIND_HACK */
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/**
* Array of the R0 SUP API.
*/
static SUPFUNC g_aFunctions[] =
{
/* name function */
/* Entries with absolute addresses determined at runtime, fixup
code makes ugly ASSUMPTIONS about the order here: */
{ "SUPR0AbsIs64bit", (void *)0 },
{ "SUPR0Abs64bitKernelCS", (void *)0 },
{ "SUPR0Abs64bitKernelSS", (void *)0 },
{ "SUPR0Abs64bitKernelDS", (void *)0 },
{ "SUPR0AbsKernelCS", (void *)0 },
{ "SUPR0AbsKernelSS", (void *)0 },
{ "SUPR0AbsKernelDS", (void *)0 },
{ "SUPR0AbsKernelES", (void *)0 },
{ "SUPR0AbsKernelFS", (void *)0 },
{ "SUPR0AbsKernelGS", (void *)0 },
/* Normal function pointers: */
{ "SUPR0EnableVTx", (void *)SUPR0EnableVTx },
/*{ "RTMemDup", (void *)UNWIND_WRAP(RTMemDup) },
{ "RTMemDupEx", (void *)UNWIND_WRAP(RTMemDupEx) },*/
{ "RTR0MemObjAddress", (void *)RTR0MemObjAddress },
{ "RTR0MemObjAddressR3", (void *)RTR0MemObjAddressR3 },
{ "RTR0MemObjSize", (void *)RTR0MemObjSize },
{ "RTR0MemObjIsMapping", (void *)RTR0MemObjIsMapping },
{ "RTR0MemObjGetPagePhysAddr", (void *)RTR0MemObjGetPagePhysAddr },
/* These don't work yet on linux - use fast mutexes!
{ "RTSemMutexCreate", (void *)RTSemMutexCreate },
{ "RTSemMutexRequest", (void *)RTSemMutexRequest },
{ "RTSemMutexRelease", (void *)RTSemMutexRelease },
{ "RTSemMutexDestroy", (void *)RTSemMutexDestroy },
*/
{ "RTProcSelf", (void *)RTProcSelf },
{ "RTR0ProcHandleSelf", (void *)RTR0ProcHandleSelf },
{ "RTTimeNanoTS", (void *)RTTimeNanoTS },
{ "RTTimeMillieTS", (void *)RTTimeMilliTS },
{ "RTTimeSystemNanoTS", (void *)RTTimeSystemNanoTS },
{ "RTTimeSystemMillieTS", (void *)RTTimeSystemMilliTS },
{ "RTThreadNativeSelf", (void *)RTThreadNativeSelf },
#if 0 /* Thread APIs, Part 2. */
#endif
{ "RTLogDefaultInstance", (void *)RTLogDefaultInstance },
{ "RTMpCpuId", (void *)RTMpCpuId },
{ "RTMpCpuIdFromSetIndex", (void *)RTMpCpuIdFromSetIndex },
{ "RTMpCpuIdToSetIndex", (void *)RTMpCpuIdToSetIndex },
{ "RTMpIsCpuPossible", (void *)RTMpIsCpuPossible },
{ "RTMpGetCount", (void *)RTMpGetCount },
{ "RTMpGetMaxCpuId", (void *)RTMpGetMaxCpuId },
{ "RTMpGetOnlineCount", (void *)RTMpGetOnlineCount },
{ "RTMpGetOnlineSet", (void *)RTMpGetOnlineSet },
{ "RTMpGetSet", (void *)RTMpGetSet },
{ "RTMpIsCpuOnline", (void *)RTMpIsCpuOnline },
{ "RTPowerNotificationRegister", (void *)RTPowerNotificationRegister },
{ "RTPowerNotificationDeregister", (void *)RTPowerNotificationDeregister },
{ "RTLogRelDefaultInstance", (void *)RTLogRelDefaultInstance },
#if defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS)
{ "RTR0AssertPanicSystem", (void *)RTR0AssertPanicSystem },
#endif
#if defined(RT_OS_DARWIN)
{ "RTAssertMsg1", (void *)RTAssertMsg1 },
{ "RTAssertMsg2", (void *)RTAssertMsg2 },
{ "RTAssertMsg2V", (void *)RTAssertMsg2V },
#endif
};
#if defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS)
/**
* Drag in the rest of IRPT since we share it with the
* rest of the kernel modules on darwin.
*/
{
};
#endif /* RT_OS_DARWIN || RT_OS_SOLARIS */
/**
* Initializes the device extentsion structure.
*
* @returns IPRT status code.
* @param pDevExt The device extension to initialize.
*/
{
int rc;
#ifdef SUPDRV_WITH_RELEASE_LOGGER
/*
* Create the release log.
*/
static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
if (RT_SUCCESS(rc))
#endif
/*
* Initialize it.
*/
if (!rc)
{
if (!rc)
{
if (!rc)
{
if (!rc)
{
if (RT_SUCCESS(rc))
{
/*
* Fixup the absolute symbols.
*
* Because of the table indexing assumptions we'll do #ifdef orgy here rather
* than distributing this to OS specific files. At least for now.
*/
#ifdef RT_OS_DARWIN
if (SUPR0GetPagingMode() >= SUPPAGINGMODE_AMD64)
{
}
else
#else
# if ARCH_BITS == 64
# endif
#endif
return VINF_SUCCESS;
}
}
}
}
}
#ifdef SUPDRV_WITH_RELEASE_LOGGER
#endif
return rc;
}
/**
* Delete the device extension (e.g. cleanup members).
*
* @param pDevExt The device extension to delete.
*/
{
/*
* Kill mutexes and spinlocks.
*/
/*
* Free lists.
*/
/* objects. */
#endif
while (pObj)
{
}
/* usage records. */
while (pUsage)
{
}
/* kill the GIP. */
#ifdef SUPDRV_WITH_RELEASE_LOGGER
/* destroy the loggers. */
#endif
}
/**
* Create session.
*
* @returns IPRT status code.
* @param pDevExt Device extension.
* @param fUser Flag indicating whether this is a user or kernel session.
* @param ppSession Where to store the pointer to the session data.
*/
{
/*
* Allocate memory for the session data.
*/
int rc = VERR_NO_MEMORY;
if (pSession)
{
/* Initialize session data. */
if (!rc)
{
/*pSession->pLdrUsage = NULL;
pSession->pVM = NULL;
pSession->pUsage = NULL;
pSession->pGip = NULL;
pSession->fGipReferenced = false;
pSession->Bundle.cUsed = 0; */
if (fUser)
{
}
else
{
}
return VINF_SUCCESS;
}
}
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.
*/
LogFlow(("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.
*/
/*
* Release object references made in this session.
* In theory there should be noone racing us in this session.
*/
Log2(("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;
}
}
Log(("supdrvCleanupSession: destroying %p/%d (%p/%p) cpid=%RTproc pid=%RTproc dtor=%p\n",
pObj, pObj->enmType, pObj->pvUser1, pObj->pvUser2, pObj->CreatorProcess, RTProcSelf(), pObj->pfnDestructor));
if (pObj->pfnDestructor)
#ifdef RT_WITH_W64_UNWIND_HACK
#else
#endif
}
/* free it and continue. */
}
}
Log2(("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.
*/
Log2(("freeing memory:\n"));
while (pBundle)
{
unsigned i;
/*
* Check and unlock all entries in the bundle.
*/
{
{
int rc;
Log2(("eType=%d pvR0=%p pvR3=%p cb=%ld\n", pBundle->aMem[i].eType, RTR0MemObjAddress(pBundle->aMem[i].MemObj),
(void *)RTR0MemObjAddressR3(pBundle->aMem[i].MapObjR3), (long)RTR0MemObjSize(pBundle->aMem[i].MemObj)));
{
}
}
}
/*
* Advance and free previous bundle.
*/
}
Log2(("freeing memory - done\n"));
/*
* Deregister component factories.
*/
Log2(("deregistering component factories:\n"));
if (pDevExt->pComponentFactoryHead)
{
while (pCur)
{
{
/* unlink it */
if (pPrev)
else
/* free it */
/* next */
}
else
{
/* next */
}
}
}
Log2(("deregistering component factories - done\n"));
/*
* Loaded images needs to be dereferenced and possibly freed up.
*/
Log2(("freeing images:\n"));
{
while (pUsage)
{
else
}
}
Log2(("freeing images - done\n"));
/*
* Unmap the GIP.
*/
Log2(("umapping GIP:\n"));
{
pSession->fGipReferenced = 0;
}
Log2(("umapping GIP - done\n"));
}
/**
* Fast path I/O Control worker.
*
* @returns VBox status code that should be passed down to ring-3 unchanged.
* @param uIOCtl Function number.
* @param idCpu VMCPU id.
* @param pDevExt Device extention.
* @param pSession Session data.
*/
int VBOXCALL supdrvIOCtlFast(uintptr_t uIOCtl, unsigned idCpu, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession)
{
/*
* We check the two prereqs after doing this only to allow the compiler to optimize things better.
*/
{
switch (uIOCtl)
{
#ifdef RT_WITH_W64_UNWIND_HACK
supdrvNtWrapVMMR0EntryFast((PFNRT)pDevExt->pfnVMMR0EntryFast, pSession->pVM, idCpu, SUP_VMMR0_DO_RAW_RUN);
#else
#endif
break;
#ifdef RT_WITH_W64_UNWIND_HACK
supdrvNtWrapVMMR0EntryFast((PFNRT)pDevExt->pfnVMMR0EntryFast, pSession->pVM, idCpu, SUP_VMMR0_DO_HWACC_RUN);
#else
#endif
break;
case SUP_IOCTL_FAST_DO_NOP:
#ifdef RT_WITH_W64_UNWIND_HACK
supdrvNtWrapVMMR0EntryFast((PFNRT)pDevExt->pfnVMMR0EntryFast, pSession->pVM, idCpu, SUP_VMMR0_DO_NOP);
#else
#endif
break;
default:
return VERR_INTERNAL_ERROR;
}
return VINF_SUCCESS;
}
return VERR_INTERNAL_ERROR;
}
/**
* Helper for supdrvIOCtl. Check if pszStr contains any character of pszChars.
* We would use strpbrk here if this function would be contained in the RedHat kABI white
* list, see http://www.kerneldrivers.org/RHEL5.
*
* @return 1 if pszStr does contain any character of pszChars, 0 otherwise.
* @param pszStr String to check
* @param pszChars Character set
*/
{
int chCur;
{
int ch;
return 1;
}
return 0;
}
/**
* I/O Control worker.
*
* @returns 0 on success.
* @returns VERR_INVALID_PARAMETER if the request is invalid.
*
* @param uIOCtl Function number.
* @param pDevExt Device extention.
* @param pSession Session data.
* @param pReqHdr The request header.
*/
int VBOXCALL supdrvIOCtl(uintptr_t uIOCtl, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPREQHDR pReqHdr)
{
/*
* Validate the request.
*/
/* this first check could probably be omitted as its also done by the OS specific code... */
{
OSDBGPRINT(("vboxdrv: Bad ioctl request header; cbIn=%#lx cbOut=%#lx fFlags=%#lx\n",
return VERR_INVALID_PARAMETER;
}
{
{
return VERR_INVALID_PARAMETER;
}
}
{
OSDBGPRINT(("vboxdrv: bad cookie %#lx / %#lx.\n", (long)pReqHdr->u32Cookie, (long)pReqHdr->u32SessionCookie));
return VERR_INVALID_PARAMETER;
}
/*
* Validation macros
*/
do { \
{ \
OSDBGPRINT(( #Name ": Invalid input/output sizes. cbIn=%ld expected %ld. cbOut=%ld expected %ld.\n", \
} \
} while (0)
do { \
{ \
} \
} while (0)
do { \
{ \
} \
} while (0)
do { \
if (RT_UNLIKELY(!(expr))) \
{ \
} \
} while (0)
do { \
if (RT_UNLIKELY(!(expr))) \
{ \
OSDBGPRINT( fmt ); \
} \
} while (0)
/*
* The switch.
*/
switch (SUP_CTL_CODE_NO_SIZE(uIOCtl))
{
{
{
return 0;
}
#if 0
/*
* Call out to the OS specific code and let it do permission checks on the
* client process.
*/
{
return 0;
}
#endif
/*
* Match the version.
* The current logic is very simple, match the major interface version.
*/
{
OSDBGPRINT(("SUP_IOCTL_COOKIE: Version mismatch. Requested: %#x Min: %#x Current: %#x\n",
return 0;
}
/*
* Fill in return data and be gone.
* N.B. The first one to change SUPDRV_IOC_VERSION shall makes sure that
* u32SessionVersion <= u32ReqVersion!
*/
/** @todo Somehow validate the client and negotiate a secure cookie... */
return 0;
}
case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_QUERY_FUNCS(0)):
{
/* validate */
REQ_CHECK_SIZES_EX(SUP_IOCTL_QUERY_FUNCS, SUP_IOCTL_QUERY_FUNCS_SIZE_IN, SUP_IOCTL_QUERY_FUNCS_SIZE_OUT(RT_ELEMENTS(g_aFunctions)));
/* execute */
return 0;
}
{
/* validate */
/* execute */
return 0;
}
{
/* validate */
/* execute */
return 0;
}
{
/* validate */
/* execute */
return 0;
}
{
/* validate */
/* execute */
return 0;
}
{
/* validate */
/* execute */
pReq->Hdr.rc = SUPR0ContAlloc(pSession, pReq->u.In.cPages, &pReq->u.Out.pvR0, &pReq->u.Out.pvR3, &pReq->u.Out.HCPhys);
return 0;
}
{
/* validate */
/* execute */
return 0;
}
{
/* validate */
REQ_CHECK_EXPR(SUP_IOCTL_LDR_OPEN, !supdrvCheckInvalidChar(pReq->u.In.szName, ";:()[]{}/\\|&*%#@!~`\"'"));
/* execute */
return 0;
}
{
/* validate */
REQ_CHECK_SIZES_EX(SUP_IOCTL_LDR_LOAD, SUP_IOCTL_LDR_LOAD_SIZE_IN(pReq->u.In.cbImage), SUP_IOCTL_LDR_LOAD_SIZE_OUT);
{
uint32_t i;
{
("SUP_IOCTL_LDR_LOAD: sym #%ld: symb off %#lx (max=%#lx)\n", (long)i, (long)paSyms[i].offSymbol, (long)pReq->u.In.cbImage));
("SUP_IOCTL_LDR_LOAD: sym #%ld: name off %#lx (max=%#lx)\n", (long)i, (long)paSyms[i].offName, (long)pReq->u.In.cbImage));
REQ_CHECK_EXPR_FMT(memchr(&pReq->u.In.achImage[pReq->u.In.offStrTab + paSyms[i].offName], '\0', pReq->u.In.cbStrTab - paSyms[i].offName),
("SUP_IOCTL_LDR_LOAD: sym #%ld: unterminated name! (%#lx / %#lx)\n", (long)i, (long)paSyms[i].offName, (long)pReq->u.In.cbImage));
}
}
/* execute */
return 0;
}
{
/* validate */
/* execute */
return 0;
}
{
/* validate */
REQ_CHECK_EXPR(SUP_IOCTL_LDR_GET_SYMBOL, memchr(pReq->u.In.szSymbol, '\0', sizeof(pReq->u.In.szSymbol)));
/* execute */
return 0;
}
case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_CALL_VMMR0(0)):
{
/* validate */
Log4(("SUP_IOCTL_CALL_VMMR0: op=%u in=%u arg=%RX64 p/t=%RTproc/%RTthrd\n",
{
REQ_CHECK_SIZES_EX(SUP_IOCTL_CALL_VMMR0, SUP_IOCTL_CALL_VMMR0_SIZE_IN(0), SUP_IOCTL_CALL_VMMR0_SIZE_OUT(0));
/* execute */
#ifdef RT_WITH_W64_UNWIND_HACK
pReq->Hdr.rc = supdrvNtWrapVMMR0EntryEx((PFNRT)pDevExt->pfnVMMR0EntryEx, pReq->u.In.pVMR0, pReq->u.In.uOperation, NULL, pReq->u.In.u64Arg, pSession);
#else
pReq->Hdr.rc = pDevExt->pfnVMMR0EntryEx(pReq->u.In.pVMR0, pReq->u.In.uOperation, NULL, pReq->u.In.u64Arg, pSession);
#endif
else
}
else
{
("SUP_IOCTL_CALL_VMMR0: cbIn=%#x < %#lx\n", pReq->Hdr.cbIn, SUP_IOCTL_CALL_VMMR0_SIZE(sizeof(SUPVMMR0REQHDR))));
REQ_CHECK_SIZES_EX(SUP_IOCTL_CALL_VMMR0, SUP_IOCTL_CALL_VMMR0_SIZE_IN(pVMMReq->cbReq), SUP_IOCTL_CALL_VMMR0_SIZE_OUT(pVMMReq->cbReq));
/* execute */
#ifdef RT_WITH_W64_UNWIND_HACK
pReq->Hdr.rc = supdrvNtWrapVMMR0EntryEx((PFNRT)pDevExt->pfnVMMR0EntryEx, pReq->u.In.pVMR0, pReq->u.In.uOperation, pVMMReq, pReq->u.In.u64Arg, pSession);
#else
pReq->Hdr.rc = pDevExt->pfnVMMR0EntryEx(pReq->u.In.pVMR0, pReq->u.In.uOperation, pVMMReq, pReq->u.In.u64Arg, pSession);
#endif
else
}
Log(("SUP_IOCTL_CALL_VMMR0: rc=%Rrc op=%u out=%u arg=%RX64 p/t=%RTproc/%RTthrd\n",
pReq->Hdr.rc, pReq->u.In.uOperation, pReq->Hdr.cbOut, pReq->u.In.u64Arg, RTProcSelf(), RTThreadNativeSelf()));
else
Log4(("SUP_IOCTL_CALL_VMMR0: rc=%Rrc op=%u out=%u arg=%RX64 p/t=%RTproc/%RTthrd\n",
pReq->Hdr.rc, pReq->u.In.uOperation, pReq->Hdr.cbOut, pReq->u.In.u64Arg, RTProcSelf(), RTThreadNativeSelf()));
return 0;
}
{
/* validate */
/* execute */
return 0;
}
{
/* validate */
REQ_CHECK_SIZES_EX(SUP_IOCTL_LOW_ALLOC, SUP_IOCTL_LOW_ALLOC_SIZE_IN, SUP_IOCTL_LOW_ALLOC_SIZE_OUT(pReq->u.In.cPages));
/* execute */
pReq->Hdr.rc = SUPR0LowAlloc(pSession, pReq->u.In.cPages, &pReq->u.Out.pvR0, &pReq->u.Out.pvR3, &pReq->u.Out.aPages[0]);
return 0;
}
{
/* validate */
/* execute */
return 0;
}
{
/* validate */
/* execute */
return 0;
}
{
/* validate */
/* execute */
return 0;
}
{
/* validate */
/* execute */
return 0;
}
{
/* validate */
REQ_CHECK_SIZES_EX(SUP_IOCTL_PAGE_ALLOC, SUP_IOCTL_PAGE_ALLOC_SIZE_IN, SUP_IOCTL_PAGE_ALLOC_SIZE_OUT(pReq->u.In.cPages));
/* execute */
pReq->Hdr.rc = SUPR0PageAlloc(pSession, pReq->u.In.cPages, &pReq->u.Out.pvR3, &pReq->u.Out.aPages[0]);
return 0;
}
{
/* validate */
REQ_CHECK_SIZES_EX(SUP_IOCTL_PAGE_ALLOC_EX, SUP_IOCTL_PAGE_ALLOC_EX_SIZE_IN, SUP_IOCTL_PAGE_ALLOC_EX_SIZE_OUT(pReq->u.In.cPages));
("SUP_IOCTL_PAGE_ALLOC_EX: No mapping requested!\n"));
("SUP_IOCTL_PAGE_ALLOC_EX: Must have user mapping!\n"));
("SUP_IOCTL_PAGE_ALLOC_EX: fReserved0=%d fReserved1=%d\n", pReq->u.In.fReserved0, pReq->u.In.fReserved1));
/* execute */
return 0;
}
{
/* validate */
REQ_CHECK_EXPR_FMT(!pReq->u.In.fFlags, ("SUP_IOCTL_PAGE_MAP_KERNEL: fFlags=%#x! MBZ\n", pReq->u.In.fFlags));
REQ_CHECK_EXPR_FMT(!(pReq->u.In.offSub & PAGE_OFFSET_MASK), ("SUP_IOCTL_PAGE_MAP_KERNEL: offSub=%#x\n", pReq->u.In.offSub));
/* execute */
return 0;
}
{
/* validate */
/* execute */
return 0;
}
case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_CALL_SERVICE(0)):
{
/* validate */
Log4(("SUP_IOCTL_CALL_SERVICE: op=%u in=%u arg=%RX64 p/t=%RTproc/%RTthrd\n",
REQ_CHECK_SIZES_EX(SUP_IOCTL_CALL_SERVICE, SUP_IOCTL_CALL_SERVICE_SIZE_IN(0), SUP_IOCTL_CALL_SERVICE_SIZE_OUT(0));
else
{
("SUP_IOCTL_CALL_SERVICE: cbIn=%#x < %#lx\n", pReq->Hdr.cbIn, SUP_IOCTL_CALL_SERVICE_SIZE(sizeof(SUPR0SERVICEREQHDR))));
REQ_CHECK_SIZES_EX(SUP_IOCTL_CALL_SERVICE, SUP_IOCTL_CALL_SERVICE_SIZE_IN(pSrvReq->cbReq), SUP_IOCTL_CALL_SERVICE_SIZE_OUT(pSrvReq->cbReq));
}
/* execute */
return 0;
}
default:
break;
}
return SUPDRV_ERR_GENERAL_FAILURE;
}
/**
* Inter-Driver Communcation (IDC) worker.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @retval VERR_INVALID_PARAMETER if the request is invalid.
* @retval VERR_NOT_SUPPORTED if the request isn't supported.
*
* @param uReq The request (function) code.
* @param pDevExt Device extention.
* @param pSession Session data.
* @param pReqHdr The request header.
*/
int VBOXCALL supdrvIDC(uintptr_t uReq, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVIDCREQHDR pReqHdr)
{
/*
* The OS specific code has already validated the pSession
* pointer, and the request size being greater or equal to
* size of the header.
*
* So, just check that pSession is a kernel context session.
*/
if (RT_UNLIKELY( pSession
return VERR_INVALID_PARAMETER;
/*
* Validation macro.
*/
do { \
{ \
} \
} while (0)
switch (uReq)
{
case SUPDRV_IDC_REQ_CONNECT:
{
/*
* Validate the cookie and other input.
*/
{
}
{
OSDBGPRINT(("SUPDRV_IDC_REQ_CONNECT: u32MagicCookie=%#x expected %#x!\n",
}
|| (pReq->u.In.uMinVersion & UINT32_C(0xffff0000)) != (pReq->u.In.uReqVersion & UINT32_C(0xffff0000)))
{
OSDBGPRINT(("SUPDRV_IDC_REQ_CONNECT: uMinVersion=%#x uMaxVersion=%#x doesn't match!\n",
}
/*
* Match the version.
* The current logic is very simple, match the major interface version.
*/
{
OSDBGPRINT(("SUPDRV_IDC_REQ_CONNECT: Version mismatch. Requested: %#x Min: %#x Current: %#x\n",
return VINF_SUCCESS;
}
/*
* On NT we will already have a session associated with the
* client, just like with the SUP_IOCTL_COOKIE request, while
* the other doesn't.
*/
#ifdef RT_OS_WINDOWS
#else
{
return VINF_SUCCESS;
}
#endif
return VINF_SUCCESS;
}
{
#ifdef RT_OS_WINDOWS
/* Windows will destroy the session when the file object is destroyed. */
#else
#endif
}
{
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
}
default:
break;
}
return VERR_NOT_SUPPORTED;
}
/**
* 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.
*/
/*
* Allocate and initialize the object.
*/
if (!pObj)
return NULL;
/*
* Allocate the usage record.
* (We keep freed usage records around to simplify SUPR0ObjAddRefEx().)
*/
if (pUsage)
else
{
if (!pUsage)
{
return NULL;
}
}
/*
* Insert the object and create the session usage record.
*/
/* The object. */
/* The session record. */
/* Log2(("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 IPRT status code.
* @param pvObj The identifier returned by SUPR0ObjRegister().
* @param pSession The session which is referencing the object.
*
* @remarks The caller should not own any spinlocks and must carefully protect
* itself against potential race with the destructor so freed memory
* isn't accessed here.
*/
{
}
/**
* Increment the reference counter for the object associating the reference
* with the specified session.
*
* @returns IPRT status code.
* @retval VERR_TRY_AGAIN if fNoBlocking was set and a new usage record
* couldn't be allocated. (If you see this you're not doing the right
* thing and it won't ever work reliably.)
*
* @param pvObj The identifier returned by SUPR0ObjRegister().
* @param pSession The session which is referencing the object.
* @param fNoBlocking Set if it's not OK to block. Never try to make the
* first reference to an object in a session with this
* argument set.
*
* @remarks The caller should not own any spinlocks and must carefully protect
* itself against potential race with the destructor so freed memory
* isn't accessed here.
*/
{
int rc = VINF_SUCCESS;
/*
* Validate the input.
* Be ready for the destruction race (someone might be stuck in the
* destructor waiting a lock we own).
*/
("Invalid pvObj=%p magic=%#x (expected %#x or %#x)\n", pvObj, pObj->u32Magic, SUPDRVOBJ_MAGIC, SUPDRVOBJ_MAGIC_DEAD),
{
return VERR_WRONG_ORDER;
}
/*
* Preallocate the usage record if we can.
*/
if (pUsagePre)
else if (!fNoBlocking)
{
if (!pUsagePre)
return VERR_NO_MEMORY;
{
return VERR_WRONG_ORDER;
}
}
/*
* Reference the object.
*/
/*
* Look for the session record.
*/
{
/*Log(("SUPR0AddRef: pUsage=%p:{.pObj=%p, .pNext=%p}\n", pUsage, pUsage->pObj, pUsage->pNext));*/
break;
}
if (pUsage)
else if (pUsagePre)
{
/* create a new session record. */
/*Log(("SUPR0AddRef: pUsagePre=%p:{.pObj=%p, .pNext=%p}\n", pUsagePre, pUsagePre->pObj, pUsagePre->pNext));*/
}
else
{
rc = VERR_TRY_AGAIN;
}
/*
* Put any unused usage record into the free list..
*/
if (pUsagePre)
{
}
return rc;
}
/**
* Decrement / destroy a reference counter record for an object.
*
* The object is uniquely identified by pfnDestructor+pvUser1+pvUser2.
*
* @returns IPRT status code.
* @retval VINF_SUCCESS if not destroyed.
* @retval VINF_OBJECT_DESTROYED if it's destroyed by this release call.
* @retval VERR_INVALID_PARAMETER if the object isn't valid. Will assert in
* string builds.
*
* @param pvObj The identifier returned by SUPR0ObjRegister().
* @param pSession The session which is referencing the object.
*/
{
int rc = VERR_INVALID_PARAMETER;
/*
* Validate the input.
*/
/*
* Acquire the spinlock and look for the usage record.
*/
{
/*Log2(("SUPR0ObjRelease: pUsage=%p:{.pObj=%p, .pNext=%p}\n", pUsage, pUsage->pObj, pUsage->pNext));*/
{
rc = VINF_SUCCESS;
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.
*/
else
{
{
break;
}
}
}
}
break;
}
}
/*
* Call the destructor and free the object if required.
*/
if (rc == VINF_OBJECT_DESTROYED)
{
Log(("SUPR0ObjRelease: destroying %p/%d (%p/%p) cpid=%RTproc pid=%RTproc dtor=%p\n",
pObj, pObj->enmType, pObj->pvUser1, pObj->pvUser2, pObj->CreatorProcess, RTProcSelf(), pObj->pfnDestructor));
if (pObj->pfnDestructor)
#ifdef RT_WITH_W64_UNWIND_HACK
#else
#endif
}
return rc;
}
/**
* Verifies that the current process can access the specified object.
*
* @returns The following IPRT status code:
* @retval VINF_SUCCESS if access was granted.
* @retval VERR_PERMISSION_DENIED if denied access.
* @retval VERR_INVALID_PARAMETER 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;
/*
* Validate the input.
*/
/*
* 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 VINF_SUCCESS;
return VERR_PERMISSION_DENIED;
}
/**
* Lock pages.
*
* @returns IPRT status code.
* @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 cPages Number of pages to lock.
* @param paPages Where to put the physical addresses of allocated memory.
*/
SUPR0DECL(int) SUPR0LockMem(PSUPDRVSESSION pSession, RTR3PTR pvR3, uint32_t cPages, PRTHCPHYS paPages)
{
int rc;
LogFlow(("SUPR0LockMem: pSession=%p pvR3=%p cPages=%d paPages=%p\n", pSession, (void *)pvR3, cPages, paPages));
/*
* Verify input.
*/
|| !pvR3)
{
return VERR_INVALID_PARAMETER;
}
#ifdef RT_OS_WINDOWS /* A temporary hack for windows, will be removed once all ring-3 code has been cleaned up. */
/* First check if we allocated it using SUPPageAlloc; if so then we don't need to lock it again */
if (RT_SUCCESS(rc))
return rc;
#endif
/*
* Let IPRT do the job.
*/
if (RT_SUCCESS(rc))
{
AssertMsg(RTR0MemObjAddressR3(Mem.MemObj) == pvR3, ("%p == %p\n", RTR0MemObjAddressR3(Mem.MemObj), pvR3));
while (iPage-- > 0)
{
{
break;
}
}
if (RT_SUCCESS(rc))
if (RT_FAILURE(rc))
{
}
}
return rc;
}
/**
* Unlocks the memory pointed to by pv.
*
* @returns IPRT status code.
* @param pSession Session to which the memory was locked.
* @param pvR3 Memory to unlock.
*/
{
#ifdef RT_OS_WINDOWS
/*
* Temporary hack for windows - SUPR0PageFree will unlock SUPR0PageAlloc
* allocations; ignore this call.
*/
{
LogFlow(("Page will be unlocked in SUPR0PageFree -> ignore\n"));
return VINF_SUCCESS;
}
#endif
}
/**
* Allocates a chunk of page aligned memory with contiguous and fixed physical
* backing.
*
* @returns IPRT status code.
* @param pSession Session data.
* @param cPages Number of pages 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 cPages, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PRTHCPHYS pHCPhys)
{
int rc;
LogFlow(("SUPR0ContAlloc: pSession=%p cPages=%d ppvR0=%p ppvR3=%p pHCPhys=%p\n", pSession, cPages, ppvR0, ppvR3, pHCPhys));
/*
* Validate input.
*/
{
Log(("Null pointer. All of these should be set: pSession=%p ppvR0=%p ppvR3=%p pHCPhys=%p\n",
return VERR_INVALID_PARAMETER;
}
{
return VERR_PAGE_COUNT_OUT_OF_RANGE;
}
/*
* Let IPRT do the job.
*/
if (RT_SUCCESS(rc))
{
int rc2;
if (RT_SUCCESS(rc))
{
if (!rc)
{
return 0;
}
}
}
return rc;
}
/**
* Frees memory allocated using SUPR0ContAlloc().
*
* @returns IPRT status code.
* @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.
*
* The memory isn't zeroed.
*
* @returns IPRT status code.
* @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, PRTHCPHYS paPages)
{
unsigned iPage;
int rc;
LogFlow(("SUPR0LowAlloc: pSession=%p cPages=%d ppvR3=%p ppvR0=%p paPages=%p\n", pSession, cPages, ppvR3, ppvR0, paPages));
/*
* Validate input.
*/
{
Log(("Null pointer. All of these should be set: pSession=%p ppvR3=%p ppvR0=%p paPages=%p\n",
return VERR_INVALID_PARAMETER;
}
{
return VERR_PAGE_COUNT_OUT_OF_RANGE;
}
/*
* Let IPRT do the work.
*/
if (RT_SUCCESS(rc))
{
int rc2;
if (RT_SUCCESS(rc))
{
if (!rc)
{
{
}
return 0;
}
}
}
return rc;
}
/**
* Frees memory allocated using SUPR0LowAlloc().
*
* @returns IPRT status code.
* @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 IPRT status code.
* @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;
/*
* Validate input.
*/
{
return VERR_INVALID_PARAMETER;
}
/*
* Let IPRT do the work.
*/
if (RT_SUCCESS(rc))
{
int rc2;
if (RT_SUCCESS(rc))
{
if (!rc)
{
return VINF_SUCCESS;
}
}
}
return rc;
}
/**
* Get the physical addresses of memory allocated using SUPR0MemAlloc().
*
* @returns IPRT status code.
* @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.
*/
SUPR0DECL(int) SUPR0MemGetPhys(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr, PSUPPAGE paPages) /** @todo switch this bugger to RTHCPHYS */
{
/*
* Validate input.
*/
/*
* Search for the address.
*/
{
{
unsigned i;
{
)
)
{
{
}
return VINF_SUCCESS;
}
}
}
}
return VERR_INVALID_PARAMETER;
}
/**
* Free memory allocated by SUPR0MemAlloc().
*
* @returns IPRT status code.
* @param pSession The session owning the allocation.
* @param uPtr The Ring-0 or Ring-3 address returned by SUPR0MemAlloc().
*/
{
}
/**
* Allocates a chunk of memory with only a R3 mappings.
*
* The memory is fixed and it's possible to query the physical addresses using
* SUPR0MemGetPhys().
*
* @returns IPRT status code.
* @param pSession The session to associated the allocation with.
* @param cPages The number of pages to allocate.
* @param ppvR3 Where to store the address of the Ring-3 mapping.
* @param paPages Where to store the addresses of the pages. Optional.
*/
SUPR0DECL(int) SUPR0PageAlloc(PSUPDRVSESSION pSession, uint32_t cPages, PRTR3PTR ppvR3, PRTHCPHYS paPages)
{
}
/**
*
* The memory is fixed and it's possible to query the physical addresses using
* SUPR0MemGetPhys().
*
* @returns IPRT status code.
* @param pSession The session to associated the allocation with.
* @param cPages The number of pages to allocate.
* @param fFlags Flags, reserved for the future. Must be zero.
* @param ppvR3 Where to store the address of the Ring-3 mapping.
* NULL if no ring-3 mapping.
* @param ppvR3 Where to store the address of the Ring-0 mapping.
* NULL if no ring-0 mapping.
* @param paPages Where to store the addresses of the pages. Optional.
*/
SUPR0DECL(int) SUPR0PageAllocEx(PSUPDRVSESSION pSession, uint32_t cPages, uint32_t fFlags, PRTR3PTR ppvR3, PRTR0PTR ppvR0, PRTHCPHYS paPages)
{
int rc;
/*
* Validate input. The allowed allocation size must be at least equal to the maximum guest VRAM size.
*/
{
Log(("SUPR0PageAlloc: Illegal request cb=%u; must be greater than 0 and smaller than 128MB.\n", cPages));
return VERR_PAGE_COUNT_OUT_OF_RANGE;
}
/*
* Let IPRT do the work.
*/
if (ppvR0)
else
if (RT_SUCCESS(rc))
{
int rc2;
if (ppvR3)
else
if (RT_SUCCESS(rc))
{
if (!rc)
{
if (ppvR3)
if (ppvR0)
if (paPages)
{
while (iPage-- > 0)
{
}
}
return VINF_SUCCESS;
}
}
}
return rc;
}
/**
*
* The memory is fixed and it's possible to query the physical addresses using
* SUPR0MemGetPhys().
*
* @returns IPRT status code.
* @param pSession The session to associated the allocation with.
* @param cPages The number of pages to allocate.
* @param fFlags Flags, reserved for the future. Must be zero.
* @param ppvR3 Where to store the address of the Ring-3 mapping.
* NULL if no ring-3 mapping.
* @param ppvR3 Where to store the address of the Ring-0 mapping.
* NULL if no ring-0 mapping.
* @param paPages Where to store the addresses of the pages. Optional.
*/
SUPR0DECL(int) SUPR0PageMapKernel(PSUPDRVSESSION pSession, RTR3PTR pvR3, uint32_t offSub, uint32_t cbSub,
{
int rc;
LogFlow(("SUPR0PageMapKernel: pSession=%p pvR3=%p offSub=%#x cbSub=%#x\n", pSession, pvR3, offSub, cbSub));
/*
* Validate input. The allowed allocation size must be at least equal to the maximum guest VRAM size.
*/
/*
* Find the memory object.
*/
{
{
unsigned i;
{
{
break;
}
}
}
}
if (hMemObj != NIL_RTR0MEMOBJ)
{
/*
* Do some furter input validations before calling IPRT.
* (Cleanup is done indirectly by telling RTR0MemObjFree to include mappings.)
*/
{
if (RT_SUCCESS(rc))
}
else
}
return rc;
}
#ifdef RT_OS_WINDOWS
/**
* Check if the pages were locked by SUPR0PageAlloc
*
* we've cleaned up the ring-3 code properly.
*
* @returns boolean
* @param pSession The session to which the memory was allocated.
* @param pvR3 The Ring-3 address returned by SUPR0PageAlloc().
*/
{
/*
* Search for the address.
*/
{
{
unsigned i;
{
{
return true;
}
}
}
}
return false;
}
/**
* Get the physical addresses of memory allocated using SUPR0PageAllocEx().
*
* we've cleaned up the ring-3 code properly.
*
* @returns IPRT status code.
* @param pSession The session to which the memory was allocated.
* @param pvR3 The Ring-3 address returned by SUPR0PageAlloc().
* @param cPages Number of pages in paPages
* @param paPages Where to store the physical addresses.
*/
static int supdrvPageGetPhys(PSUPDRVSESSION pSession, RTR3PTR pvR3, uint32_t cPages, PRTHCPHYS paPages)
{
LogFlow(("supdrvPageGetPhys: pSession=%p pvR3=%p cPages=%#lx paPages=%p\n", pSession, (void *)pvR3, (long)cPages, paPages));
/*
* Search for the address.
*/
{
{
unsigned i;
{
{
return VINF_SUCCESS;
}
}
}
}
return VERR_INVALID_PARAMETER;
}
#endif /* RT_OS_WINDOWS */
/**
* Free memory allocated by SUPR0PageAlloc() and SUPR0PageAllocEx().
*
* @returns IPRT status code.
* @param pSession The session owning the allocation.
* @param pvR3 The Ring-3 address returned by SUPR0PageAlloc() or
* SUPR0PageAllocEx().
*/
{
}
/**
*
* @returns IPRT status code.
* @param pSession Session to which the GIP mapping should belong.
* @param ppGipR3 Where to store the address of the ring-3 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.
*/
{
int rc = 0;
/*
* Validate
*/
{
/*
* Map it?
*/
if (ppGipR3)
{
if (RT_SUCCESS(rc))
{
}
}
/*
* Get physical address.
*/
if (pHCPhysGip && !rc)
/*
* Reference globally.
*/
{
{
unsigned i;
LogFlow(("SUPR0GipMap: Resumes GIP updating\n"));
ASMAtomicXchgU32(&pGip->aCPUs[i].u32TransactionId, pGip->aCPUs[i].u32TransactionId & ~(GIP_UPDATEHZ_RECALC_FREQ * 2 - 1));
}
}
}
else
{
Log(("SUPR0GipMap: GIP is not available!\n"));
}
/*
* Write returns.
*/
if (pHCPhysGip)
*pHCPhysGip = HCPhys;
if (ppGipR3)
#ifdef DEBUG_DARWIN_GIP
OSDBGPRINT(("SUPR0GipMap: returns %d *pHCPhysGip=%lx pGip=%p\n", rc, (unsigned long)HCPhys, (void *)pGip));
#else
LogFlow(( "SUPR0GipMap: returns %d *pHCPhysGip=%lx pGip=%p\n", rc, (unsigned long)HCPhys, (void *)pGip));
#endif
return rc;
}
/**
* Unmaps any user mapping of the GIP and terminates all GIP access
* from this session.
*
* @returns IPRT status code.
* @param pSession Session to which the GIP mapping should belong.
*/
{
int rc = VINF_SUCCESS;
#ifdef DEBUG_DARWIN_GIP
OSDBGPRINT(("SUPR0GipUnmap: pSession=%p pGip=%p GipMapObjR3=%p\n",
pSession->GipMapObjR3));
#else
#endif
/*
* Unmap anything?
*/
{
if (RT_SUCCESS(rc))
}
/*
* Dereference global GIP.
*/
{
pSession->fGipReferenced = 0;
{
LogFlow(("SUPR0GipUnmap: Suspends GIP updating\n"));
}
}
return rc;
}
/**
* Register a component factory with the support driver.
*
* This is currently restricted to kernel sessions only.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @retval VERR_NO_MEMORY if we're out of memory.
* @retval VERR_ALREADY_EXISTS if the factory has already been registered.
* @retval VERR_ACCESS_DENIED if it isn't a kernel session.
* @retval VERR_INVALID_PARAMETER on invalid parameter.
* @retval VERR_INVALID_POINTER on invalid pointer parameter.
*
* @param pSession The SUPDRV session (must be a ring-0 session).
* @param pFactory Pointer to the component factory registration structure.
*
* @remarks This interface is also available via SUPR0IdcComponentRegisterFactory.
*/
{
const char *psz;
int rc;
/*
* Validate parameters.
*/
/*
* Allocate and initialize a new registration structure.
*/
if (pNewReg)
{
/*
* Add it to the tail of the list after checking for prior registration.
*/
if (RT_SUCCESS(rc))
{
{
}
if (!pCur)
{
if (pPrev)
else
rc = VINF_SUCCESS;
}
else
}
if (RT_FAILURE(rc))
}
else
rc = VERR_NO_MEMORY;
return rc;
}
/**
* Deregister a component factory.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @retval VERR_NOT_FOUND if the factory wasn't registered.
* @retval VERR_ACCESS_DENIED if it isn't a kernel session.
* @retval VERR_INVALID_PARAMETER on invalid parameter.
* @retval VERR_INVALID_POINTER on invalid pointer parameter.
*
* @param pSession The SUPDRV session (must be a ring-0 session).
* @param pFactory Pointer to the component factory registration structure
* previously passed SUPR0ComponentRegisterFactory().
*
* @remarks This interface is also available via SUPR0IdcComponentDeregisterFactory.
*/
{
int rc;
/*
* Validate parameters.
*/
/*
* Take the lock and look for the registration record.
*/
if (RT_SUCCESS(rc))
{
{
}
if (pCur)
{
if (!pPrev)
else
rc = VINF_SUCCESS;
}
else
rc = VERR_NOT_FOUND;
}
return rc;
}
/**
* Queries a component factory.
*
* @returns VBox status code.
* @retval VERR_INVALID_PARAMETER on invalid parameter.
* @retval VERR_INVALID_POINTER on invalid pointer parameter.
* @retval VERR_SUPDRV_COMPONENT_NOT_FOUND if the component factory wasn't found.
* @retval VERR_SUPDRV_INTERFACE_NOT_SUPPORTED if the interface wasn't supported.
*
* @param pSession The SUPDRV session.
* @param pszName The name of the component factory.
* @param pszInterfaceUuid The UUID of the factory interface (stringified).
* @param ppvFactoryIf Where to store the factory interface.
*/
SUPR0DECL(int) SUPR0ComponentQueryFactory(PSUPDRVSESSION pSession, const char *pszName, const char *pszInterfaceUuid, void **ppvFactoryIf)
{
const char *pszEnd;
int rc;
/*
* Validate parameters.
*/
*ppvFactoryIf = NULL;
/*
* Take the lock and try all factories by this name.
*/
if (RT_SUCCESS(rc))
{
while (pCur)
{
{
#ifdef RT_WITH_W64_UNWIND_HACK
void *pvFactory = supdrvNtWrapQueryFactoryInterface((PFNRT)pCur->pFactory->pfnQueryFactoryInterface, pCur->pFactory, pSession, pszInterfaceUuid);
#else
void *pvFactory = pCur->pFactory->pfnQueryFactoryInterface(pCur->pFactory, pSession, pszInterfaceUuid);
#endif
if (pvFactory)
{
rc = VINF_SUCCESS;
break;
}
}
/* next */
}
}
return rc;
}
/**
* Adds a memory object to the session.
*
* @returns IPRT status code.
* @param pMem Memory tracking structure containing the
* information to track.
* @param pSession The session.
*/
{
/*
* Find free entry and record the allocation.
*/
{
{
unsigned i;
{
{
return VINF_SUCCESS;
}
}
AssertFailed(); /* !!this can't be happening!!! */
}
}
/*
* Need to allocate a new bundle.
* Insert into the last entry in the bundle.
*/
if (!pBundle)
return VERR_NO_MEMORY;
/* take last entry. */
/* insert into list. */
return VINF_SUCCESS;
}
/**
* Releases a memory object referenced by pointer and type.
*
* @returns IPRT status code.
* @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 (!uPtr)
{
return VERR_INVALID_PARAMETER;
}
/*
* Search for the address.
*/
{
{
unsigned i;
{
)
{
/* Make a copy of it and release it outside the spinlock. */
{
}
{
}
return VINF_SUCCESS;
}
}
}
}
return VERR_INVALID_PARAMETER;
}
/**
* 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 IPRT status code.
* @param pDevExt Device globals.
* @param pSession Session data.
* @param pReq The open request.
*/
{
unsigned cb;
void *pv;
/*
* Check if we got an instance of the image already.
*/
{
{
return VINF_SUCCESS;
}
}
/* (not found - add it!) */
/*
* Allocate memory.
*/
if (!pv)
{
return VERR_NO_MEMORY;
}
/*
* Setup and link in the LDR stuff.
*/
#if defined(RT_OS_WINDOWS) && defined(DEBUG)
#endif
return VINF_SUCCESS;
}
/**
* Loads the image bits.
*
* This is the 2nd step of the loading.
*
* @returns IPRT status code.
* @param pDevExt Device globals.
* @param pSession Session data.
* @param pReq The request.
*/
{
int rc;
LogFlow(("supdrvIOCtl_LdrLoad: pvImageBase=%p cbImage=%d\n", pReq->u.In.pvImageBase, pReq->u.In.cbImage));
/*
* Find the ldr image.
*/
if (!pUsage)
{
Log(("SUP_IOCTL_LDR_LOAD: couldn't find image!\n"));
return VERR_INVALID_HANDLE;
}
{
Log(("SUP_IOCTL_LDR_LOAD: image size mismatch!! %d(prep) != %d(load)\n", pImage->cbImage, pReq->u.In.cbImage));
return VERR_INVALID_HANDLE;
}
{
if (uState != SUP_IOCTL_LDR_LOAD)
return SUPDRV_ERR_ALREADY_LOADED;
}
{
case SUPLDRLOADEP_NOTHING:
break;
case SUPLDRLOADEP_VMMR0:
{
Log(("NULL pointer: pvVMMR0=%p pvVMMR0EntryInt=%p pvVMMR0EntryFast=%p pvVMMR0EntryEx=%p!\n",
return VERR_INVALID_PARAMETER;
}
/** @todo validate pReq->u.In.EP.VMMR0.pvVMMR0 against pvImage! */
if ( (uintptr_t)pReq->u.In.EP.VMMR0.pvVMMR0EntryInt - (uintptr_t)pImage->pvImage >= pReq->u.In.cbImage
|| (uintptr_t)pReq->u.In.EP.VMMR0.pvVMMR0EntryFast - (uintptr_t)pImage->pvImage >= pReq->u.In.cbImage
|| (uintptr_t)pReq->u.In.EP.VMMR0.pvVMMR0EntryEx - (uintptr_t)pImage->pvImage >= pReq->u.In.cbImage)
{
Log(("Out of range (%p LB %#x): pvVMMR0EntryInt=%p, pvVMMR0EntryFast=%p or pvVMMR0EntryEx=%p is NULL!\n",
return VERR_INVALID_PARAMETER;
}
break;
case SUPLDRLOADEP_SERVICE:
{
return VERR_INVALID_PARAMETER;
}
if ((uintptr_t)pReq->u.In.EP.Service.pfnServiceReq - (uintptr_t)pImage->pvImage >= pReq->u.In.cbImage)
{
Log(("Out of range (%p LB %#x): pfnServiceReq=%p, pvVMMR0EntryFast=%p or pvVMMR0EntryEx=%p is NULL!\n",
return VERR_INVALID_PARAMETER;
}
{
Log(("Out of range (%p LB %#x): apvReserved={%p,%p,%p} MBZ!\n",
return VERR_INVALID_PARAMETER;
}
break;
default:
return VERR_INVALID_PARAMETER;
}
{
Log(("SUP_IOCTL_LDR_LOAD: pfnModuleInit=%p is outside the image (%p %d bytes)\n",
return VERR_INVALID_PARAMETER;
}
{
Log(("SUP_IOCTL_LDR_LOAD: pfnModuleTerm=%p is outside the image (%p %d bytes)\n",
return VERR_INVALID_PARAMETER;
}
/*
* Copy the memory.
*/
/*
* Update any entry points.
*/
{
default:
case SUPLDRLOADEP_NOTHING:
rc = VINF_SUCCESS;
break;
case SUPLDRLOADEP_VMMR0:
rc = supdrvLdrSetVMMR0EPs(pDevExt, pReq->u.In.EP.VMMR0.pvVMMR0, pReq->u.In.EP.VMMR0.pvVMMR0EntryInt,
break;
case SUPLDRLOADEP_SERVICE:
rc = VINF_SUCCESS;
break;
}
/*
* On success call the module initialization.
*/
{
#ifdef RT_WITH_W64_UNWIND_HACK
#else
#endif
}
if (rc)
return rc;
}
/**
* Frees a previously loaded (prep'ed) image.
*
* @returns IPRT status code.
* @param pDevExt Device globals.
* @param pSession Session data.
* @param pReq The request.
*/
{
int rc;
/*
* Find the ldr image.
*/
pUsagePrev = NULL;
{
pUsagePrev = pUsage;
}
if (!pUsage)
{
Log(("SUP_IOCTL_LDR_FREE: couldn't find image!\n"));
return VERR_INVALID_HANDLE;
}
/*
* Check if we can remove anything.
*/
rc = VINF_SUCCESS;
{
/*
* Check if there are any objects with destructors in the image, if
* so leave it for the session cleanup routine so we get a chance to
* clean things up in the right order and not leave them all dangling.
*/
{
{
break;
}
}
else
{
if (RT_UNLIKELY((uintptr_t)pGenUsage->pObj->pfnDestructor - (uintptr_t)pImage->pvImage < pImage->cbImage))
{
break;
}
}
if (rc == VINF_SUCCESS)
{
/* unlink it */
if (pUsagePrev)
else
/* free it */
/*
* Derefrence the image.
*/
else
}
else
{
}
}
else
{
/*
* Dereference both image and usage.
*/
}
return rc;
}
/**
* 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 pReq The request buffer.
*/
static int supdrvIOCtl_LdrGetSymbol(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDRGETSYMBOL pReq)
{
uint32_t i;
const char *pchStrings;
int rc = VERR_GENERAL_FAILURE;
Log3(("supdrvIOCtl_LdrGetSymbol: pvImageBase=%p szSymbol=\"%s\"\n", pReq->u.In.pvImageBase, pReq->u.In.szSymbol));
/*
* Find the ldr image.
*/
if (!pUsage)
{
Log(("SUP_IOCTL_LDR_GET_SYMBOL: couldn't find image!\n"));
return VERR_INVALID_HANDLE;
}
{
return VERR_ALREADY_LOADED;
}
/*
* Search the symbol strings.
*/
{
{
rc = VINF_SUCCESS;
break;
}
}
return rc;
}
/**
* Gets the address of a symbol in an open image or the support driver.
*
* @returns VINF_SUCCESS on success.
* @returns
* @param pDevExt Device globals.
* @param pSession Session data.
* @param pReq The request buffer.
*/
static int supdrvIDC_LdrGetSymbol(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVIDCREQGETSYM pReq)
{
int rc = VINF_SUCCESS;
char const *pszEnd;
uint32_t i;
/*
* Input validation.
*/
if (pszModule)
{
}
Log3(("supdrvIDC_LdrGetSymbol: pszModule=%p:{%s} pszSymbol=%p:{%s}\n", pszModule, pszModule, pszSymbol, pszSymbol));
if ( !pszModule
{
/*
* Search the support driver export table.
*/
for (i = 0; i < RT_ELEMENTS(g_aFunctions); i++)
{
break;
}
}
else
{
/*
* Find the loader image.
*/
break;
{
/*
* Search the symbol strings.
*/
{
{
/*
* Found it! Calc the symbol address and add a reference to the module.
*/
break;
}
}
}
else
}
return rc;
}
/**
* Updates the VMMR0 entry point pointers.
*
* @returns IPRT status code.
* @param pDevExt Device globals.
* @param pSession Session data.
* @param pVMMR0 VMMR0 image handle.
* @param pvVMMR0EntryInt VMMR0EntryInt address.
* @param pvVMMR0EntryFast VMMR0EntryFast address.
* @param pvVMMR0EntryEx VMMR0EntryEx address.
* @remark Caller must own the loader mutex.
*/
static int supdrvLdrSetVMMR0EPs(PSUPDRVDEVEXT pDevExt, void *pvVMMR0, void *pvVMMR0EntryInt, void *pvVMMR0EntryFast, void *pvVMMR0EntryEx)
{
int rc = VINF_SUCCESS;
/*
* Check if not yet set.
*/
{
}
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 VMMR0 entry point installed by supdrvLdrSetR0EP.
*
* @param pDevExt Device globals.
*/
{
}
/**
* Adds a usage reference in the specified session of an image.
*
* Called while owning the loader semaphore.
*
* @returns VINF_SUCCESS on success and VERR_NO_MEMORY on failure.
* @param pSession Session in question.
* @param pImage Image which the session is using.
*/
{
/*
* Referenced it already?
*/
while (pUsage)
{
{
return VINF_SUCCESS;
}
}
/*
* Allocate new usage record.
*/
return VINF_SUCCESS;
}
/**
* 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 unset its entry point pointers. */
/* check for objects with destructors in this image. (Shouldn't happen.) */
{
unsigned cObjs = 0;
{
cObjs++;
}
if (cObjs)
}
/* call termination function if fully loaded. */
if ( pImage->pfnModuleTerm
{
#ifdef RT_WITH_W64_UNWIND_HACK
#else
pImage->pfnModuleTerm();
#endif
}
/* free the image */
}
/**
* Implements the service call request.
*
* @returns VBox status code.
* @param pDevExt The device extension.
* @param pSession The calling session.
* @param pReq The request packet, valid.
*/
static int supdrvIOCtl_CallServiceModule(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPCALLSERVICE pReq)
{
#if !defined(RT_OS_WINDOWS) || defined(DEBUG)
int rc;
/*
* Find the module first in the module referenced by the calling session.
*/
if (RT_SUCCESS(rc))
{
{
break;
}
if (pfnServiceReqHandler)
{
/*
* Call it.
*/
#ifdef RT_WITH_W64_UNWIND_HACK
rc = supdrvNtWrapServiceReqHandler((PFNRT)pfnServiceReqHandler, pSession, pReq->u.In.uOperation, pReq->u.In.u64Arg, NULL);
#else
#endif
else
#ifdef RT_WITH_W64_UNWIND_HACK
#else
rc = pfnServiceReqHandler(pSession, pReq->u.In.uOperation, pReq->u.In.u64Arg, (PSUPR0SERVICEREQHDR)&pReq->abReqPkt[0]);
#endif
}
else
}
/* log it */
if ( RT_FAILURE(rc)
&& rc != VERR_INTERRUPTED
&& rc != VERR_TIMEOUT)
Log(("SUP_IOCTL_CALL_SERVICE: rc=%Rrc op=%u out=%u arg=%RX64 p/t=%RTproc/%RTthrd\n",
rc, pReq->u.In.uOperation, pReq->Hdr.cbOut, pReq->u.In.u64Arg, RTProcSelf(), RTThreadNativeSelf()));
else
Log4(("SUP_IOCTL_CALL_SERVICE: rc=%Rrc op=%u out=%u arg=%RX64 p/t=%RTproc/%RTthrd\n",
rc, pReq->u.In.uOperation, pReq->Hdr.cbOut, pReq->u.In.u64Arg, RTProcSelf(), RTThreadNativeSelf()));
return rc;
#else /* RT_OS_WINDOWS && !DEBUG */
return VERR_NOT_IMPLEMENTED;
#endif /* RT_OS_WINDOWS && !DEBUG */
}
/**
* Gets the paging mode of the current CPU.
*
* @returns Paging mode, SUPPAGEINGMODE_INVALID on error.
*/
{
else
{
uint32_t fNXEPlusLMA = 0;
if (cr4 & X86_CR4_PAE)
{
{
fNXEPlusLMA |= RT_BIT(0);
}
}
{
case 0:
break;
case X86_CR4_PGE:
break;
case X86_CR4_PAE:
break;
case X86_CR4_PAE | RT_BIT(0):
break;
case X86_CR4_PAE | X86_CR4_PGE:
break;
break;
break;
break;
break;
break;
default:
break;
}
}
return enmMode;
}
/**
* Enables or disabled hardware virtualization extensions using native OS APIs.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @retval VERR_NOT_SUPPORTED if not supported by the native OS.
*
* @param fEnable Whether to enable or disable.
*/
{
#ifdef RT_OS_DARWIN
return supdrvOSEnableVTx(fEnable);
#else
return VERR_NOT_SUPPORTED;
#endif
}
/**
* Creates the GIP.
*
* @returns VBox status code.
* @param pDevExt Instance data. GIP stuff may be updated.
*/
{
int rc;
LogFlow(("supdrvGipCreate:\n"));
/* assert order */
/*
* Allocate a suitable page with a default kernel mapping.
*/
if (RT_FAILURE(rc))
{
return rc;
}
#if 0 /** @todo Disabled this as we didn't used to do it before and causes unnecessary stress on laptops.
* It only applies to Windows and should probably revisited later, if possible made part of the
* timer code (return min granularity in RTTimerGetSystemGranularity and set it in RTTimerStart). */
/*
* Try bump up the system timer resolution.
* The more interrupts the better...
*/
)
{
}
#endif
/*
* Find a reasonable update interval and initialize the structure.
*/
/*
* Create the timer.
* If CPU_ALL isn't supported we'll have to fall back to synchronous mode.
*/
{
rc = RTTimerCreateEx(&pDevExt->pGipTimer, u32Interval, RTTIMER_FLAGS_CPU_ALL, supdrvGipAsyncTimer, pDevExt);
if (rc == VERR_NOT_SUPPORTED)
{
OSDBGPRINT(("supdrvGipCreate: omni timer not supported, falling back to synchronous mode\n"));
}
}
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
/*
* We're good.
*/
return VINF_SUCCESS;
}
}
else
{
OSDBGPRINT(("supdrvGipCreate: failed create GIP timer at %ld ns interval. rc=%d\n", (long)u32Interval, rc));
}
return rc;
}
/**
* Terminates the GIP.
*
* @param pDevExt Instance data. GIP stuff may be updated.
*/
{
int rc;
#ifdef DEBUG_DARWIN_GIP
#endif
/*
* Invalid the GIP data.
*/
{
}
/*
* Destroy the timer and free the GIP memory object.
*/
{
}
{
}
/*
* Finally, release the system timer resolution request if one succeeded.
*/
{
}
}
/**
* Timer callback function sync GIP mode.
* @param pTimer The timer.
* @param pvUser The device extension.
*/
{
}
/**
* Timer callback function for async GIP mode.
* @param pTimer The timer.
* @param pvUser The device extension.
*/
{
/** @todo reset the transaction number and whatnot when iTick == 1. */
else
}
/**
* Multiprocessor event notification callback.
*
* This is used to make sue that the GIP master gets passed on to
* another CPU.
*
* @param enmEvent The event.
* @param idCpu The cpu it applies to.
* @param pvUser Pointer to the device extension.
*/
{
if (enmEvent == RTMPEVENT_OFFLINE)
{
if (idGipMaster == idCpu)
{
/*
* Find a new GIP master.
*/
bool fIgnored;
unsigned i;
for (i = 0; i < RTCPUSET_MAX_CPUS; i++)
{
&& idCurCpu != idGipMaster)
{
break;
}
}
}
}
}
/**
* Initializes the GIP data.
*
* @returns IPRT 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;
#ifdef DEBUG_DARWIN_GIP
OSDBGPRINT(("supdrvGipInit: pGip=%p HCPhys=%lx u64NanoTS=%llu uUpdateHz=%d\n", pGip, (long)HCPhys, u64NanoTS, uUpdateHz));
#else
LogFlow(("supdrvGipInit: pGip=%p HCPhys=%lx u64NanoTS=%llu uUpdateHz=%d\n", pGip, (long)HCPhys, u64NanoTS, uUpdateHz));
#endif
/*
* 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 VINF_SUCCESS;
}
/**
* Callback used by supdrvDetermineAsyncTSC to read the TSC on a CPU.
*
* @param idCpu Ignored.
* @param pvUser1 Where to put the TSC.
* @param pvUser2 Ignored.
*/
static DECLCALLBACK(void) supdrvDetermineAsyncTscWorker(RTCPUID idCpu, void *pvUser1, void *pvUser2)
{
#if 1
#else
#endif
}
/**
* Determine if Async GIP mode is required because of TSC drift.
*
* (TSC) runs never backwards, that is, a read operation to the counter should return
* a bigger value than any previous read operation. This is guaranteed by the latest
* AMD CPUs and by newer Intel CPUs which never enter the C2 state (P4). In any other
* case we have to choose the asynchronous timer mode.
*
* @param poffMin Pointer to the determined difference between different cores.
* @return false if the time stamp counters appear to be synchron, true otherwise.
*/
{
/*
* Just iterate all the cpus 8 times and make sure that the TSC is
* ever increasing. We don't bother taking TSC rollover into account.
*/
int iCpu;
int cLoops = 8;
bool fAsync = false;
int rc = VINF_SUCCESS;
while (cLoops-- > 0)
{
{
if (RT_SUCCESS(rc))
{
{
fAsync = true;
dprintf(("supdrvDetermineAsyncTsc: iCpu=%d cLoops=%d CurTsc=%llx PrevTsc=%llx\n",
break;
}
/* Gather statistics (except the first time). */
{
}
/* Next */
}
else if (rc == VERR_NOT_SUPPORTED)
break;
else
}
/* broke out of the loop. */
break;
}
dprintf(("supdrvDetermineAsyncTsc: returns %d; iLastCpu=%d rc=%d offMin=%llx offMax=%llx\n",
#endif
return fAsync;
}
/**
* Determin the GIP TSC mode.
*
* @returns The most suitable TSC mode.
* @param pDevExt Pointer to the device instance data.
*/
{
/*
* On SMP we're faced with two problems:
* (1) There might be a skew between the CPU, so that cpu0
* returns a TSC that is sligtly different from cpu1.
* (2) Power management (and other things) may cause the TSC
* to run at a non-constant speed, and cause the speed
* to be different on the cpus. This will result in (1).
*
* So, on SMP systems we'll have to select the ASYNC update method
* if there are symphoms of these problems.
*/
if (RTMpGetCount() > 1)
{
return SUPGIPMODE_ASYNC_TSC;
/* Try check for current differences between the cpus. */
return SUPGIPMODE_ASYNC_TSC;
/*
* If the CPU supports power management and is an AMD one we
* won't trust it unless it has the TscInvariant bit is set.
*/
/* Check for "AuthenticAMD" */
if ( uEAX >= 1
&& uEBX == X86_CPUID_VENDOR_AMD_EBX
&& uECX == X86_CPUID_VENDOR_AMD_ECX
&& uEDX == X86_CPUID_VENDOR_AMD_EDX)
{
/* Check for APM support and that TscInvariant is cleared. */
if (uEAX >= 0x80000007)
{
return SUPGIPMODE_ASYNC_TSC;
}
}
}
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 RT_ARCH_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.
*/
}
}