SUPDrv.c revision d8c8f5fbd010ad9ec23ff9f2c6d8c027a0ca8f01
/* $Id$ */
/** @file
* VBoxDrv - The VirtualBox Support Driver - Common code.
*/
/*
* Copyright (C) 2006-2014 Oracle Corporation
*
* 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_SUP_DRV
#define SUPDRV_AGNOSTIC
#include "SUPDrvInternal.h"
#ifndef PAGE_SHIFT
#endif
#include <iprt/asm-amd64-x86.h>
#include <iprt/asm-math.h>
#include <iprt/handletable.h>
#include <iprt/semaphore.h>
#include <iprt/spinlock.h>
#endif
#if defined(RT_OS_SOLARIS) || defined(RT_OS_DARWIN)
#else
# define VBOXDRV_SESSION_CLOSE(pvSession) do { } while (0)
#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
* instantiation in log-vbox.c(pp).
*/
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** The frequency by which we recalculate the u32UpdateHz and
* u32UpdateIntervalNS GIP members. The value must be a power of 2.
*
* Warning: Bumping this too high might overflow u32UpdateIntervalNS.
*/
#define GIP_UPDATEHZ_RECALC_FREQ 0x800
/** A reserved TSC value used for synchronization as well as measurement of
* TSC deltas. */
#define GIP_TSC_DELTA_RSVD UINT64_MAX
/** The number of TSC delta measurement loops in total (includes primer and
* read-time loops). */
#define GIP_TSC_DELTA_LOOPS 96
/** The number of cache primer loops. */
#define GIP_TSC_DELTA_PRIMER_LOOPS 4
/** The number of loops until we keep computing the minumum read time. */
#define GIP_TSC_DELTA_READ_TIME_LOOPS 24
/** Stop measurement of TSC delta. */
#define GIP_TSC_DELTA_SYNC_STOP 0
/** Start measurement of TSC delta. */
#define GIP_TSC_DELTA_SYNC_START 1
/** Worker thread is ready for reading the TSC. */
#define GIP_TSC_DELTA_SYNC_WORKER_READY 2
/** Worker thread is done updating TSC delta info. */
#define GIP_TSC_DELTA_SYNC_WORKER_DONE 3
/** When IPRT is isn't concurrent safe: Master is ready and will wait for worker
* with a timeout. */
#define GIP_TSC_DELTA_SYNC_PRESTART_MASTER 4
/** When IPRT is isn't concurrent safe: Worker is ready after waiting for
* master with a timeout. */
#define GIP_TSC_DELTA_SYNC_PRESTART_WORKER 5
/** The TSC-refinement interval in seconds. */
#define GIP_TSC_REFINE_INTERVAL 5
/** The TSC-delta threshold for the SUPGIPUSETSCDELTA_PRACTICALLY_ZERO rating */
#define GIP_TSC_DELTA_THRESHOLD_PRACTICALLY_ZERO 24
/** The TSC-delta threshold for the SUPGIPUSETSCDELTA_ROUGHLY_ZERO rating */
#define GIP_TSC_DELTA_THRESHOLD_ROUGHLY_ZERO 384
/** @def VBOX_SVN_REV
* The makefile should define this if it can. */
#ifndef VBOX_SVN_REV
# define VBOX_SVN_REV 0
#endif
#if 0 /* Don't start the GIP timers. Useful when debugging the IPRT timer code. */
# define DO_NOT_START_GIP
#endif
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static DECLCALLBACK(int) supdrvSessionObjHandleRetain(RTHANDLETABLE hHandleTable, void *pvObj, void *pvCtx, void *pvUser);
static DECLCALLBACK(void) supdrvSessionObjHandleDelete(RTHANDLETABLE hHandleTable, uint32_t h, void *pvObj, void *pvCtx, void *pvUser);
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);
static int supdrvIOCtl_LoggerSettings(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLOGGERSETTINGS pReq);
static int supdrvIOCtl_TscDeltaMeasure(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPTSCDELTAMEASURE pReq);
static DECLCALLBACK(void) supdrvGipSyncAndInvariantTimer(PRTTIMER pTimer, void *pvUser, uint64_t iTick);
static void supdrvGipInit(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE pGip, RTHCPHYS HCPhys, uint64_t u64NanoTS,
static void supdrvGipUpdate(PSUPDRVDEVEXT pDevExt, uint64_t u64NanoTS, uint64_t u64TSC, RTCPUID idCpu, uint64_t iTick);
static void supdrvGipInitCpu(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE pGip, PSUPGIPCPU pCpu, uint64_t u64NanoTS);
static int supdrvIOCtl_ResumeSuspendedKbds(void);
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/**
* Array of the R0 SUP API.
*
* While making changes to these exports, make sure to update the IOC
* minor version (SUPDRV_IOC_VERSION).
*/
static SUPFUNC g_aFunctions[] =
{
/* SED: START */
/* 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: */
{ "SUPGetGIP", (void *)SUPGetGIP },
{ "SUPR0ComponentDeregisterFactory", (void *)SUPR0ComponentDeregisterFactory },
{ "SUPR0ComponentQueryFactory", (void *)SUPR0ComponentQueryFactory },
{ "SUPR0ComponentRegisterFactory", (void *)SUPR0ComponentRegisterFactory },
{ "SUPR0ContAlloc", (void *)SUPR0ContAlloc },
{ "SUPR0ContFree", (void *)SUPR0ContFree },
{ "SUPR0EnableVTx", (void *)SUPR0EnableVTx },
{ "SUPR0SuspendVTxOnCpu", (void *)SUPR0SuspendVTxOnCpu },
{ "SUPR0ResumeVTxOnCpu", (void *)SUPR0ResumeVTxOnCpu },
{ "SUPR0GetKernelFeatures", (void *)SUPR0GetKernelFeatures },
{ "SUPR0GetPagingMode", (void *)SUPR0GetPagingMode },
{ "SUPR0GetSvmUsability", (void *)SUPR0GetSvmUsability },
{ "SUPR0GetVmxUsability", (void *)SUPR0GetVmxUsability },
{ "SUPR0LockMem", (void *)SUPR0LockMem },
{ "SUPR0LowAlloc", (void *)SUPR0LowAlloc },
{ "SUPR0LowFree", (void *)SUPR0LowFree },
{ "SUPR0MemAlloc", (void *)SUPR0MemAlloc },
{ "SUPR0MemFree", (void *)SUPR0MemFree },
{ "SUPR0MemGetPhys", (void *)SUPR0MemGetPhys },
{ "SUPR0ObjAddRef", (void *)SUPR0ObjAddRef },
{ "SUPR0ObjAddRefEx", (void *)SUPR0ObjAddRefEx },
{ "SUPR0ObjRegister", (void *)SUPR0ObjRegister },
{ "SUPR0ObjRelease", (void *)SUPR0ObjRelease },
{ "SUPR0ObjVerifyAccess", (void *)SUPR0ObjVerifyAccess },
{ "SUPR0PageAllocEx", (void *)SUPR0PageAllocEx },
{ "SUPR0PageFree", (void *)SUPR0PageFree },
{ "SUPR0Printf", (void *)SUPR0Printf },
{ "SUPR0TracerDeregisterDrv", (void *)SUPR0TracerDeregisterDrv },
{ "SUPR0TracerDeregisterImpl", (void *)SUPR0TracerDeregisterImpl },
{ "SUPR0TracerFireProbe", (void *)SUPR0TracerFireProbe },
{ "SUPR0TracerRegisterDrv", (void *)SUPR0TracerRegisterDrv },
{ "SUPR0TracerRegisterImpl", (void *)SUPR0TracerRegisterImpl },
{ "SUPR0TracerRegisterModule", (void *)SUPR0TracerRegisterModule },
{ "SUPR0TracerUmodProbeFire", (void *)SUPR0TracerUmodProbeFire },
{ "SUPR0UnlockMem", (void *)SUPR0UnlockMem },
{ "SUPSemEventClose", (void *)SUPSemEventClose },
{ "SUPSemEventCreate", (void *)SUPSemEventCreate },
{ "SUPSemEventGetResolution", (void *)SUPSemEventGetResolution },
{ "SUPSemEventMultiClose", (void *)SUPSemEventMultiClose },
{ "SUPSemEventMultiCreate", (void *)SUPSemEventMultiCreate },
{ "SUPSemEventMultiGetResolution", (void *)SUPSemEventMultiGetResolution },
{ "SUPSemEventMultiReset", (void *)SUPSemEventMultiReset },
{ "SUPSemEventMultiSignal", (void *)SUPSemEventMultiSignal },
{ "SUPSemEventMultiWait", (void *)SUPSemEventMultiWait },
{ "SUPSemEventMultiWaitNoResume", (void *)SUPSemEventMultiWaitNoResume },
{ "SUPSemEventMultiWaitNsAbsIntr", (void *)SUPSemEventMultiWaitNsAbsIntr },
{ "SUPSemEventMultiWaitNsRelIntr", (void *)SUPSemEventMultiWaitNsRelIntr },
{ "SUPSemEventSignal", (void *)SUPSemEventSignal },
{ "SUPSemEventWait", (void *)SUPSemEventWait },
{ "SUPSemEventWaitNoResume", (void *)SUPSemEventWaitNoResume },
{ "SUPSemEventWaitNsAbsIntr", (void *)SUPSemEventWaitNsAbsIntr },
{ "SUPSemEventWaitNsRelIntr", (void *)SUPSemEventWaitNsRelIntr },
{ "RTAssertAreQuiet", (void *)RTAssertAreQuiet },
{ "RTAssertMayPanic", (void *)RTAssertMayPanic },
{ "RTAssertMsg1", (void *)RTAssertMsg1 },
{ "RTAssertMsg2AddV", (void *)RTAssertMsg2AddV },
{ "RTAssertMsg2V", (void *)RTAssertMsg2V },
{ "RTAssertSetMayPanic", (void *)RTAssertSetMayPanic },
{ "RTAssertSetQuiet", (void *)RTAssertSetQuiet },
{ "RTCrc32", (void *)RTCrc32 },
{ "RTCrc32Finish", (void *)RTCrc32Finish },
{ "RTCrc32Process", (void *)RTCrc32Process },
{ "RTCrc32Start", (void *)RTCrc32Start },
{ "RTErrConvertFromErrno", (void *)RTErrConvertFromErrno },
{ "RTErrConvertToErrno", (void *)RTErrConvertToErrno },
{ "RTHandleTableAllocWithCtx", (void *)RTHandleTableAllocWithCtx },
{ "RTHandleTableCreate", (void *)RTHandleTableCreate },
{ "RTHandleTableCreateEx", (void *)RTHandleTableCreateEx },
{ "RTHandleTableDestroy", (void *)RTHandleTableDestroy },
{ "RTHandleTableFreeWithCtx", (void *)RTHandleTableFreeWithCtx },
{ "RTHandleTableLookupWithCtx", (void *)RTHandleTableLookupWithCtx },
{ "RTLogDefaultInstance", (void *)RTLogDefaultInstance },
{ "RTLogGetDefaultInstance", (void *)RTLogGetDefaultInstance },
{ "RTLogLoggerExV", (void *)RTLogLoggerExV },
{ "RTLogPrintfV", (void *)RTLogPrintfV },
{ "RTLogRelDefaultInstance", (void *)RTLogRelDefaultInstance },
{ "RTLogSetDefaultInstanceThread", (void *)RTLogSetDefaultInstanceThread },
{ "RTMemAllocExTag", (void *)RTMemAllocExTag },
{ "RTMemAllocTag", (void *)RTMemAllocTag },
{ "RTMemAllocVarTag", (void *)RTMemAllocVarTag },
{ "RTMemAllocZTag", (void *)RTMemAllocZTag },
{ "RTMemAllocZVarTag", (void *)RTMemAllocZVarTag },
{ "RTMemDupExTag", (void *)RTMemDupExTag },
{ "RTMemDupTag", (void *)RTMemDupTag },
{ "RTMemFree", (void *)RTMemFree },
{ "RTMemFreeEx", (void *)RTMemFreeEx },
{ "RTMemReallocTag", (void *)RTMemReallocTag },
{ "RTMpCpuId", (void *)RTMpCpuId },
{ "RTMpCpuIdFromSetIndex", (void *)RTMpCpuIdFromSetIndex },
{ "RTMpCpuIdToSetIndex", (void *)RTMpCpuIdToSetIndex },
{ "RTMpGetArraySize", (void *)RTMpGetArraySize },
{ "RTMpGetCount", (void *)RTMpGetCount },
{ "RTMpGetMaxCpuId", (void *)RTMpGetMaxCpuId },
{ "RTMpGetOnlineCount", (void *)RTMpGetOnlineCount },
{ "RTMpGetOnlineSet", (void *)RTMpGetOnlineSet },
{ "RTMpGetSet", (void *)RTMpGetSet },
{ "RTMpIsCpuOnline", (void *)RTMpIsCpuOnline },
{ "RTMpIsCpuPossible", (void *)RTMpIsCpuPossible },
{ "RTMpIsCpuWorkPending", (void *)RTMpIsCpuWorkPending },
{ "RTMpNotificationDeregister", (void *)RTMpNotificationDeregister },
{ "RTMpNotificationRegister", (void *)RTMpNotificationRegister },
{ "RTMpOnAll", (void *)RTMpOnAll },
{ "RTMpOnOthers", (void *)RTMpOnOthers },
{ "RTMpOnSpecific", (void *)RTMpOnSpecific },
{ "RTMpPokeCpu", (void *)RTMpPokeCpu },
{ "RTNetIPv4AddDataChecksum", (void *)RTNetIPv4AddDataChecksum },
{ "RTNetIPv4AddTCPChecksum", (void *)RTNetIPv4AddTCPChecksum },
{ "RTNetIPv4AddUDPChecksum", (void *)RTNetIPv4AddUDPChecksum },
{ "RTNetIPv4FinalizeChecksum", (void *)RTNetIPv4FinalizeChecksum },
{ "RTNetIPv4HdrChecksum", (void *)RTNetIPv4HdrChecksum },
{ "RTNetIPv4IsDHCPValid", (void *)RTNetIPv4IsDHCPValid },
{ "RTNetIPv4IsHdrValid", (void *)RTNetIPv4IsHdrValid },
{ "RTNetIPv4IsTCPSizeValid", (void *)RTNetIPv4IsTCPSizeValid },
{ "RTNetIPv4IsTCPValid", (void *)RTNetIPv4IsTCPValid },
{ "RTNetIPv4IsUDPSizeValid", (void *)RTNetIPv4IsUDPSizeValid },
{ "RTNetIPv4IsUDPValid", (void *)RTNetIPv4IsUDPValid },
{ "RTNetIPv4PseudoChecksum", (void *)RTNetIPv4PseudoChecksum },
{ "RTNetIPv4PseudoChecksumBits", (void *)RTNetIPv4PseudoChecksumBits },
{ "RTNetIPv4TCPChecksum", (void *)RTNetIPv4TCPChecksum },
{ "RTNetIPv4UDPChecksum", (void *)RTNetIPv4UDPChecksum },
{ "RTNetIPv6PseudoChecksum", (void *)RTNetIPv6PseudoChecksum },
{ "RTNetIPv6PseudoChecksumBits", (void *)RTNetIPv6PseudoChecksumBits },
{ "RTNetIPv6PseudoChecksumEx", (void *)RTNetIPv6PseudoChecksumEx },
{ "RTNetTCPChecksum", (void *)RTNetTCPChecksum },
{ "RTNetUDPChecksum", (void *)RTNetUDPChecksum },
{ "RTPowerNotificationDeregister", (void *)RTPowerNotificationDeregister },
{ "RTPowerNotificationRegister", (void *)RTPowerNotificationRegister },
{ "RTProcSelf", (void *)RTProcSelf },
{ "RTR0AssertPanicSystem", (void *)RTR0AssertPanicSystem },
{ "RTR0MemAreKrnlAndUsrDifferent", (void *)RTR0MemAreKrnlAndUsrDifferent },
{ "RTR0MemKernelIsValidAddr", (void *)RTR0MemKernelIsValidAddr },
{ "RTR0MemKernelCopyFrom", (void *)RTR0MemKernelCopyFrom },
{ "RTR0MemKernelCopyTo", (void *)RTR0MemKernelCopyTo },
{ "RTR0MemObjAddress", (void *)RTR0MemObjAddress },
{ "RTR0MemObjAddressR3", (void *)RTR0MemObjAddressR3 },
{ "RTR0MemObjAllocContTag", (void *)RTR0MemObjAllocContTag },
{ "RTR0MemObjAllocLowTag", (void *)RTR0MemObjAllocLowTag },
{ "RTR0MemObjAllocPageTag", (void *)RTR0MemObjAllocPageTag },
{ "RTR0MemObjAllocPhysExTag", (void *)RTR0MemObjAllocPhysExTag },
{ "RTR0MemObjAllocPhysNCTag", (void *)RTR0MemObjAllocPhysNCTag },
{ "RTR0MemObjAllocPhysTag", (void *)RTR0MemObjAllocPhysTag },
{ "RTR0MemObjEnterPhysTag", (void *)RTR0MemObjEnterPhysTag },
{ "RTR0MemObjFree", (void *)RTR0MemObjFree },
{ "RTR0MemObjGetPagePhysAddr", (void *)RTR0MemObjGetPagePhysAddr },
{ "RTR0MemObjIsMapping", (void *)RTR0MemObjIsMapping },
{ "RTR0MemObjLockUserTag", (void *)RTR0MemObjLockUserTag },
{ "RTR0MemObjMapKernelExTag", (void *)RTR0MemObjMapKernelExTag },
{ "RTR0MemObjMapKernelTag", (void *)RTR0MemObjMapKernelTag },
{ "RTR0MemObjMapUserTag", (void *)RTR0MemObjMapUserTag },
{ "RTR0MemObjProtect", (void *)RTR0MemObjProtect },
{ "RTR0MemObjSize", (void *)RTR0MemObjSize },
{ "RTR0MemUserCopyFrom", (void *)RTR0MemUserCopyFrom },
{ "RTR0MemUserCopyTo", (void *)RTR0MemUserCopyTo },
{ "RTR0MemUserIsValidAddr", (void *)RTR0MemUserIsValidAddr },
{ "RTR0ProcHandleSelf", (void *)RTR0ProcHandleSelf },
{ "RTSemEventCreate", (void *)RTSemEventCreate },
{ "RTSemEventDestroy", (void *)RTSemEventDestroy },
{ "RTSemEventGetResolution", (void *)RTSemEventGetResolution },
{ "RTSemEventMultiCreate", (void *)RTSemEventMultiCreate },
{ "RTSemEventMultiDestroy", (void *)RTSemEventMultiDestroy },
{ "RTSemEventMultiGetResolution", (void *)RTSemEventMultiGetResolution },
{ "RTSemEventMultiReset", (void *)RTSemEventMultiReset },
{ "RTSemEventMultiSignal", (void *)RTSemEventMultiSignal },
{ "RTSemEventMultiWait", (void *)RTSemEventMultiWait },
{ "RTSemEventMultiWaitEx", (void *)RTSemEventMultiWaitEx },
{ "RTSemEventMultiWaitExDebug", (void *)RTSemEventMultiWaitExDebug },
{ "RTSemEventMultiWaitNoResume", (void *)RTSemEventMultiWaitNoResume },
{ "RTSemEventSignal", (void *)RTSemEventSignal },
{ "RTSemEventWait", (void *)RTSemEventWait },
{ "RTSemEventWaitEx", (void *)RTSemEventWaitEx },
{ "RTSemEventWaitExDebug", (void *)RTSemEventWaitExDebug },
{ "RTSemEventWaitNoResume", (void *)RTSemEventWaitNoResume },
{ "RTSemFastMutexCreate", (void *)RTSemFastMutexCreate },
{ "RTSemFastMutexDestroy", (void *)RTSemFastMutexDestroy },
{ "RTSemFastMutexRelease", (void *)RTSemFastMutexRelease },
{ "RTSemFastMutexRequest", (void *)RTSemFastMutexRequest },
{ "RTSemMutexCreate", (void *)RTSemMutexCreate },
{ "RTSemMutexDestroy", (void *)RTSemMutexDestroy },
{ "RTSemMutexRelease", (void *)RTSemMutexRelease },
{ "RTSemMutexRequest", (void *)RTSemMutexRequest },
{ "RTSemMutexRequestDebug", (void *)RTSemMutexRequestDebug },
{ "RTSemMutexRequestNoResume", (void *)RTSemMutexRequestNoResume },
{ "RTSemMutexRequestNoResumeDebug", (void *)RTSemMutexRequestNoResumeDebug },
{ "RTSpinlockAcquire", (void *)RTSpinlockAcquire },
{ "RTSpinlockCreate", (void *)RTSpinlockCreate },
{ "RTSpinlockDestroy", (void *)RTSpinlockDestroy },
{ "RTSpinlockRelease", (void *)RTSpinlockRelease },
{ "RTStrCopy", (void *)RTStrCopy },
{ "RTStrDupTag", (void *)RTStrDupTag },
{ "RTStrFormat", (void *)RTStrFormat },
{ "RTStrFormatNumber", (void *)RTStrFormatNumber },
{ "RTStrFormatTypeDeregister", (void *)RTStrFormatTypeDeregister },
{ "RTStrFormatTypeRegister", (void *)RTStrFormatTypeRegister },
{ "RTStrFormatTypeSetUser", (void *)RTStrFormatTypeSetUser },
{ "RTStrFormatV", (void *)RTStrFormatV },
{ "RTStrFree", (void *)RTStrFree },
{ "RTStrNCmp", (void *)RTStrNCmp },
{ "RTStrPrintf", (void *)RTStrPrintf },
{ "RTStrPrintfEx", (void *)RTStrPrintfEx },
{ "RTStrPrintfExV", (void *)RTStrPrintfExV },
{ "RTStrPrintfV", (void *)RTStrPrintfV },
{ "RTThreadCreate", (void *)RTThreadCreate },
{ "RTThreadCtxHooksAreRegistered", (void *)RTThreadCtxHooksAreRegistered },
{ "RTThreadCtxHooksCreate", (void *)RTThreadCtxHooksCreate },
{ "RTThreadCtxHooksDeregister", (void *)RTThreadCtxHooksDeregister },
{ "RTThreadCtxHooksRegister", (void *)RTThreadCtxHooksRegister },
{ "RTThreadCtxHooksRelease", (void *)RTThreadCtxHooksRelease },
{ "RTThreadCtxHooksRetain", (void *)RTThreadCtxHooksRetain },
{ "RTThreadGetName", (void *)RTThreadGetName },
{ "RTThreadGetNative", (void *)RTThreadGetNative },
{ "RTThreadGetType", (void *)RTThreadGetType },
{ "RTThreadIsInInterrupt", (void *)RTThreadIsInInterrupt },
{ "RTThreadNativeSelf", (void *)RTThreadNativeSelf },
{ "RTThreadPreemptDisable", (void *)RTThreadPreemptDisable },
{ "RTThreadPreemptIsEnabled", (void *)RTThreadPreemptIsEnabled },
{ "RTThreadPreemptIsPending", (void *)RTThreadPreemptIsPending },
{ "RTThreadPreemptIsPendingTrusty", (void *)RTThreadPreemptIsPendingTrusty },
{ "RTThreadPreemptIsPossible", (void *)RTThreadPreemptIsPossible },
{ "RTThreadPreemptRestore", (void *)RTThreadPreemptRestore },
{ "RTThreadSelf", (void *)RTThreadSelf },
{ "RTThreadSelfName", (void *)RTThreadSelfName },
{ "RTThreadSleep", (void *)RTThreadSleep },
{ "RTThreadUserReset", (void *)RTThreadUserReset },
{ "RTThreadUserSignal", (void *)RTThreadUserSignal },
{ "RTThreadUserWait", (void *)RTThreadUserWait },
{ "RTThreadUserWaitNoResume", (void *)RTThreadUserWaitNoResume },
{ "RTThreadWait", (void *)RTThreadWait },
{ "RTThreadWaitNoResume", (void *)RTThreadWaitNoResume },
{ "RTThreadYield", (void *)RTThreadYield },
{ "RTTimeMilliTS", (void *)RTTimeMilliTS },
{ "RTTimeNanoTS", (void *)RTTimeNanoTS },
{ "RTTimeNow", (void *)RTTimeNow },
{ "RTTimerCanDoHighResolution", (void *)RTTimerCanDoHighResolution },
{ "RTTimerChangeInterval", (void *)RTTimerChangeInterval },
{ "RTTimerCreate", (void *)RTTimerCreate },
{ "RTTimerCreateEx", (void *)RTTimerCreateEx },
{ "RTTimerDestroy", (void *)RTTimerDestroy },
{ "RTTimerGetSystemGranularity", (void *)RTTimerGetSystemGranularity },
{ "RTTimerReleaseSystemGranularity", (void *)RTTimerReleaseSystemGranularity },
{ "RTTimerRequestSystemGranularity", (void *)RTTimerRequestSystemGranularity },
{ "RTTimerStart", (void *)RTTimerStart },
{ "RTTimerStop", (void *)RTTimerStop },
{ "RTTimeSystemMilliTS", (void *)RTTimeSystemMilliTS },
{ "RTTimeSystemNanoTS", (void *)RTTimeSystemNanoTS },
{ "RTUuidCompare", (void *)RTUuidCompare },
{ "RTUuidCompareStr", (void *)RTUuidCompareStr },
{ "RTUuidFromStr", (void *)RTUuidFromStr },
/* SED: END */
};
/**
* Drag in the rest of IRPT since we share it with the
* rest of the kernel modules on darwin.
*/
{
/* VBoxNetAdp */
/* VBoxUSB */
};
#endif /* RT_OS_DARWIN || RT_OS_SOLARIS || RT_OS_SOLARIS */
/**
* Initializes the device extentsion structure.
*
* @returns IPRT status code.
* @param pDevExt The device extension to initialize.
* @param cbSession The size of the session structure. The size of
* SUPDRVSESSION may be smaller when SUPDRV_AGNOSTIC is
* defined because we're skipping the OS specific members
* then.
*/
{
int rc;
#ifdef SUPDRV_WITH_RELEASE_LOGGER
/*
* Create the release log.
*/
static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
"VBOX_RELEASE_LOG", RT_ELEMENTS(s_apszGroups), s_apszGroups, RTLOGDEST_STDOUT | RTLOGDEST_DEBUGGER, NULL);
if (RT_SUCCESS(rc))
/** @todo Add native hook for getting logger config parameters and setting
* them. On linux we should use the module parameter stuff... */
#endif
/*
* Initialize it.
*/
memset(pDevExt, 0, sizeof(*pDevExt)); /* Does not wipe OS specific tail section of the structure. */
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
rc = RTSpinlockCreate(&pDevExt->hSessionHashTabSpinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "SUPDrvSession");
if (RT_SUCCESS(rc))
#ifdef SUPDRV_USE_MUTEX_FOR_LDR
#else
#endif
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
#ifdef SUPDRV_USE_MUTEX_FOR_LDR
#else
#endif
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
/*
* Fixup the absolute symbols.
*
* Because of the table indexing assumptions we'll have a little #ifdef orgy
* here rather than distributing this to OS specific files. At least for now.
*/
#ifdef RT_OS_DARWIN
# if ARCH_BITS == 32
if (SUPR0GetPagingMode() >= SUPPAGINGMODE_AMD64)
{
}
else
# else /* 64-bit darwin: */
# endif
#else /* !RT_OS_DARWIN */
# if ARCH_BITS == 64
# else
# endif
#endif /* !RT_OS_DARWIN */
return VINF_SUCCESS;
}
}
#ifdef SUPDRV_USE_MUTEX_FOR_GIP
#else
#endif
}
}
#ifdef SUPDRV_USE_MUTEX_FOR_LDR
#else
#endif
}
#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.
*/
#ifdef SUPDRV_USE_MUTEX_FOR_GIP
#else
#endif
#ifdef SUPDRV_USE_MUTEX_FOR_LDR
#else
#endif
/*
* Free lists.
*/
/* objects. */
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 fUnrestricted Unrestricted access (system) or restricted access
* (user)?
* @param ppSession Where to store the pointer to the session data.
*/
int VBOXCALL supdrvCreateSession(PSUPDRVDEVEXT pDevExt, bool fUser, bool fUnrestricted, PSUPDRVSESSION *ppSession)
{
int rc;
if (!SUP_IS_DEVEXT_VALID(pDevExt))
return VERR_INVALID_PARAMETER;
/*
* Allocate memory for the session data.
*/
if (pSession)
{
/* Initialize session data. */
if (!rc)
{
if (RT_SUCCESS(rc))
{
/*pSession->fInHashTable = false; */
/*pSession->pCommonNextHash = NULL;
pSession->ppOsSessionPtr = NULL; */
if (fUser)
{
}
else
{
}
/*pSession->pLdrUsage = NULL;
pSession->pVM = NULL;
pSession->pUsage = NULL;
pSession->pGip = NULL;
pSession->fGipReferenced = false;
pSession->Bundle.cUsed = 0; */
/*pSession->uTracerData = 0;*/
/*pSession->cTpProviders = 0;*/
/*pSession->cTpProbesFiring = 0;*/
/*RT_ZERO(pSession->apTpLookupTable);*/
return VINF_SUCCESS;
}
}
}
else
rc = VERR_NO_MEMORY;
return rc;
}
/**
* Cleans up the session in the context of the process to which it belongs, the
* caller will free the session and the session spinlock.
*
* This should normally occur when the session is closed or as the process
* exits. Careful reference counting in the OS specfic code makes sure that
* threads doing I/O control calls.
*
* @param pDevExt The device extension.
* @param pSession Session data.
*/
{
int rc;
AssertReleaseMsg(pSession->R0Process == RTR0ProcHandleSelf() || pSession->R0Process == NIL_RTR0PROCESS,
/*
* Remove logger instances related to this session.
*/
/*
* Destroy the handle table.
*/
/*
* 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)
}
/* free it and continue. */
}
}
Log2(("release objects - done\n"));
/*
* Do tracer cleanups related to this session.
*/
Log2(("release tracer stuff - start\n"));
Log2(("release tracer stuff - end\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.
*/
{
{
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"));
}
/**
* Common code for freeing a session when the reference count reaches zero.
*
* @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(("supdrvDestroySession: returns\n"));
}
/**
* Inserts the session into the global hash table.
*
* @retval VINF_SUCCESS on success.
* @retval VERR_WRONG_ORDER if the session was already inserted (asserted).
* @retval VERR_INVALID_PARAMETER if the session handle is invalid or a ring-0
* session (asserted).
* @retval VERR_DUPLICATE if there is already a session for that pid.
*
* @param pDevExt The device extension.
* @param pSession The session.
* @param ppOsSessionPtr Pointer to the OS session pointer, if any is
* available and used. This will set to point to the
* session while under the protection of the session
* hash table spinlock. It will also be kept in
* PSUPDRVSESSION::ppOsSessionPtr for lookup and
* cleanup use.
* @param pvUser Argument for supdrvOSSessionHashTabInserted.
*/
int VBOXCALL supdrvSessionHashTabInsert(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVSESSION *ppOsSessionPtr,
void *pvUser)
{
unsigned iHash;
/*
* Validate input.
*/
/*
* Calculate the hash table index and acquire the spinlock.
*/
/*
* If there are a collisions, we need to carefully check if we got a
* duplicate. There can only be one open session per process.
*/
if (pCur)
{
if (pCur)
{
{
AssertFailed();
return VERR_WRONG_ORDER;
}
return VERR_RESOURCE_IN_USE;
return VERR_DUPLICATE;
}
}
/*
* Insert it, doing a callout to the OS specific code in case it has
* anything it wishes to do while we're holding the spinlock.
*/
pSession->fInHashTable = true;
if (ppOsSessionPtr)
/*
* Retain a reference for the pointer in the session table.
*/
return VINF_SUCCESS;
}
/**
* Removes the session from the global hash table.
*
* @retval VINF_SUCCESS on success.
* @retval VERR_NOT_FOUND if the session was already removed (asserted).
* @retval VERR_INVALID_PARAMETER if the session handle is invalid or a ring-0
* session (asserted).
*
* @param pDevExt The device extension.
* @param pSession The session. The caller is expected to have a reference
* to this so it won't croak on us when we release the hash
* table reference.
* @param pvUser OS specific context value for the
* supdrvOSSessionHashTabInserted callback.
*/
int VBOXCALL supdrvSessionHashTabRemove(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, void *pvUser)
{
unsigned iHash;
/*
* Validate input.
*/
/*
* Calculate the hash table index and acquire the spinlock.
*/
/*
* Unlink it.
*/
else
{
{
}
if (pCur)
else
{
return VERR_NOT_FOUND;
}
}
pSession->fInHashTable = false;
/*
* Clear OS specific session pointer if available and do the OS callback.
*/
if (pSession->ppOsSessionPtr)
{
}
/*
* Drop the reference the hash table had to the session. This shouldn't
* be the last reference!
*/
if (cRefs == 0)
return VINF_SUCCESS;
}
/**
* Looks up the session for the current process in the global hash table or in
* OS specific pointer.
*
* @returns Pointer to the session with a reference that the caller must
* release. If no valid session was found, NULL is returned.
*
* @param pDevExt The device extension.
* @param Process The process ID.
* @param R0Process The ring-0 process handle.
* @param ppOsSessionPtr The OS session pointer if available. If not NULL,
* this is used instead of the hash table. For
* additional safety it must then be equal to the
* SUPDRVSESSION::ppOsSessionPtr member.
* This can be NULL even if the OS has a session
* pointer.
*/
PSUPDRVSESSION VBOXCALL supdrvSessionHashTabLookup(PSUPDRVDEVEXT pDevExt, RTPROCESS Process, RTR0PROCESS R0Process,
{
unsigned iHash;
/*
* Validate input.
*/
/*
* Calculate the hash table index and acquire the spinlock.
*/
/*
* If an OS session pointer is provided, always use it.
*/
if (ppOsSessionPtr)
{
pCur = *ppOsSessionPtr;
if ( pCur
}
else
{
/*
* Otherwise, do the hash table lookup.
*/
while ( pCur
}
/*
* Retain the session.
*/
if (pCur)
{
}
return pCur;
}
/**
* Retain a session to make sure it doesn't go away while it is in use.
*
* @returns New reference count on success, UINT32_MAX on failure.
* @param pSession Session data.
*/
{
return cRefs;
}
/**
* Releases a given session.
*
* @returns New reference count on success (0 if closed), UINT32_MAX on failure.
* @param pSession Session data.
*/
{
if (cRefs == 0)
return cRefs;
}
/**
* RTHandleTableDestroy callback used by supdrvCleanupSession.
*
* @returns IPRT status code, see SUPR0ObjAddRef.
* @param hHandleTable The handle table handle. Ignored.
* @param pvObj The object pointer.
* @param pvCtx Context, the handle type. Ignored.
* @param pvUser Session pointer.
*/
static DECLCALLBACK(int) supdrvSessionObjHandleRetain(RTHANDLETABLE hHandleTable, void *pvObj, void *pvCtx, void *pvUser)
{
}
/**
* RTHandleTableDestroy callback used by supdrvCleanupSession.
*
* @param hHandleTable The handle table handle. Ignored.
* @param h The handle value. Ignored.
* @param pvObj The object pointer.
* @param pvCtx Context, the handle type. Ignored.
* @param pvUser Session pointer.
*/
static DECLCALLBACK(void) supdrvSessionObjHandleDelete(RTHANDLETABLE hHandleTable, uint32_t h, void *pvObj, void *pvCtx, void *pvUser)
{
NOREF(h);
}
/**
* 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, VMCPUID idCpu, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession)
{
/*
* We check the two prereqs after doing this only to allow the compiler to optimize things better.
*/
&& pDevExt->pfnVMMR0EntryFast))
{
switch (uIOCtl)
{
break;
case SUP_IOCTL_FAST_DO_HM_RUN:
break;
case SUP_IOCTL_FAST_DO_NOP:
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.
*
* @returns 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 inner worker (tracing reasons).
*
* @returns IPRT status code.
* @retval 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.
*/
static int supdrvIOCtlInnerUnrestricted(uintptr_t uIOCtl, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPREQHDR pReqHdr)
{
/*
* 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 */
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.cbImageWithTabs), SUP_IOCTL_LDR_LOAD_SIZE_OUT);
("SUP_IOCTL_LDR_LOAD: offSymbols=%#lx cSymbols=%#lx cbImageWithTabs=%#lx\n", (long)pReq->u.In.offSymbols,
("SUP_IOCTL_LDR_LOAD: offStrTab=%#lx cbStrTab=%#lx cbImageWithTabs=%#lx\n", (long)pReq->u.In.offStrTab,
{
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.cbImageWithTabs));
("SUP_IOCTL_LDR_LOAD: sym #%ld: name off %#lx (max=%#lx)\n", (long)i, (long)paSyms[i].offName, (long)pReq->u.In.cbImageWithTabs));
REQ_CHECK_EXPR_FMT(RTStrEnd((char const *)&pReq->u.In.abImage[pReq->u.In.offStrTab + paSyms[i].offName],
("SUP_IOCTL_LDR_LOAD: sym #%ld: unterminated name! (%#lx / %#lx)\n", (long)i, (long)paSyms[i].offName, (long)pReq->u.In.cbImageWithTabs));
}
}
/* execute */
return 0;
}
{
/* validate */
/* execute */
return 0;
}
{
/* validate */
/* execute */
return 0;
}
{
/* validate */
REQ_CHECK_EXPR(SUP_IOCTL_LDR_GET_SYMBOL, RTStrEnd(pReq->u.In.szSymbol, 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 */
pReq->Hdr.rc = pDevExt->pfnVMMR0EntryEx(pReq->u.In.pVMR0, pReq->u.In.idCpu, pReq->u.In.uOperation, NULL, pReq->u.In.u64Arg, pSession);
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 */
pReq->Hdr.rc = pDevExt->pfnVMMR0EntryEx(pReq->u.In.pVMR0, pReq->u.In.idCpu, pReq->u.In.uOperation, pVMMReq, pReq->u.In.u64Arg, pSession);
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 */
Log4(("SUP_IOCTL_CALL_VMMR0_BIG: op=%u in=%u arg=%RX64 p/t=%RTproc/%RTthrd\n",
("SUP_IOCTL_CALL_VMMR0_BIG: cbIn=%#x < %#lx\n", pReq->Hdr.cbIn, SUP_IOCTL_CALL_VMMR0_BIG_SIZE(sizeof(SUPVMMR0REQHDR))));
REQ_CHECK_SIZES_EX(SUP_IOCTL_CALL_VMMR0_BIG, SUP_IOCTL_CALL_VMMR0_BIG_SIZE_IN(pVMMReq->cbReq), SUP_IOCTL_CALL_VMMR0_BIG_SIZE_OUT(pVMMReq->cbReq));
/* execute */
pReq->Hdr.rc = pDevExt->pfnVMMR0EntryEx(pReq->u.In.pVMR0, pReq->u.In.idCpu, pReq->u.In.uOperation, pVMMReq, pReq->u.In.u64Arg, pSession);
else
Log(("SUP_IOCTL_CALL_VMMR0_BIG: 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_BIG: 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_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 */
REQ_CHECK_EXPR_FMT(!(pReq->u.In.fProt & ~(RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC | RTMEM_PROT_NONE)),
REQ_CHECK_EXPR_FMT(!(pReq->u.In.offSub & PAGE_OFFSET_MASK), ("SUP_IOCTL_PAGE_PROTECT: offSub=%#x\n", pReq->u.In.offSub));
/* execute */
pReq->Hdr.rc = SUPR0PageProtect(pSession, pReq->u.In.pvR3, pReq->u.In.pvR0, pReq->u.In.offSub, pReq->u.In.cbSub, pReq->u.In.fProt);
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;
}
{
/* validate */
("SUP_IOCTL_LOGGER_SETTINGS: cbIn=%#x cbStrTab=%#zx LastChar=%d\n",
/* execute */
return 0;
}
{
/* validate */
/* execute */
{
case SUP_SEM_TYPE_EVENT:
{
{
case SUPSEMOP2_WAIT_MS_REL:
break;
case SUPSEMOP2_WAIT_NS_ABS:
break;
case SUPSEMOP2_WAIT_NS_REL:
break;
case SUPSEMOP2_SIGNAL:
break;
case SUPSEMOP2_CLOSE:
break;
case SUPSEMOP2_RESET:
default:
break;
}
break;
}
case SUP_SEM_TYPE_EVENT_MULTI:
{
{
case SUPSEMOP2_WAIT_MS_REL:
break;
case SUPSEMOP2_WAIT_NS_ABS:
break;
case SUPSEMOP2_WAIT_NS_REL:
break;
case SUPSEMOP2_SIGNAL:
break;
case SUPSEMOP2_CLOSE:
break;
case SUPSEMOP2_RESET:
break;
default:
break;
}
break;
}
default:
break;
}
return 0;
}
{
/* validate */
/* execute */
{
case SUP_SEM_TYPE_EVENT:
{
{
case SUPSEMOP3_CREATE:
break;
case SUPSEMOP3_GET_RESOLUTION:
break;
default:
break;
}
break;
}
case SUP_SEM_TYPE_EVENT_MULTI:
{
{
case SUPSEMOP3_CREATE:
break;
case SUPSEMOP3_GET_RESOLUTION:
break;
default:
break;
}
break;
}
default:
break;
}
return 0;
}
{
/* validate */
/* execute */
return 0;
}
{
/* validate */
/* execute */
return 0;
}
{
/* validate */
/* execute */
return 0;
}
{
/* validate */
/* execute */
pReqHdr->rc = supdrvIOCtl_TracerIOCtl(pDevExt, pSession, pReq->u.In.uCmd, pReq->u.In.uArg, &pReq->u.Out.iRetVal);
return 0;
}
{
/* validate */
return VERR_INVALID_PARAMETER;
/* execute */
return 0;
}
{
/* validate */
/* execute */
return 0;
}
{
/* validate */
return 0;
}
{
/* validate */
return 0;
}
{
/* validate */
return 0;
}
{
/* validate */
return 0;
}
{
/* validate */
return 0;
}
default:
break;
}
return VERR_GENERAL_FAILURE;
}
/**
* I/O Control inner worker for the restricted operations.
*
* @returns IPRT status code.
* @retval 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.
*/
static int supdrvIOCtlInnerRestricted(uintptr_t uIOCtl, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPREQHDR pReqHdr)
{
/*
* The switch.
*/
switch (SUP_CTL_CODE_NO_SIZE(uIOCtl))
{
{
{
return 0;
}
/*
* 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;
}
{
/* validate */
/* execute */
return 0;
}
default:
break;
}
return VERR_GENERAL_FAILURE;
}
/**
* I/O Control worker.
*
* @returns IPRT status code.
* @retval 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, size_t cbReq)
{
int rc;
/*
* Validate the request.
*/
{
return VERR_INVALID_PARAMETER;
}
{
OSDBGPRINT(("vboxdrv: Bad ioctl request header; cbIn=%#lx cbOut=%#lx fFlags=%#lx\n",
return VERR_INVALID_PARAMETER;
}
{
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;
}
/*
* Hand it to an inner function to avoid lots of unnecessary return tracepoints.
*/
if (pSession->fUnrestricted)
else
return rc;
}
/**
* Inter-Driver Communication (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;
}
{
return VINF_SUCCESS;
}
return VINF_SUCCESS;
}
{
}
{
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)
}
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 locked 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;
}
/*
* Let IPRT do the job.
*/
rc = RTR0MemObjLockUser(&Mem.MemObj, pvR3, cb, RTMEM_PROT_READ | RTMEM_PROT_WRITE, RTR0ProcHandleSelf());
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.
*/
{
}
/**
* 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().
*/
{
}
/**
*
* 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 %uMB (VBOX_MAX_ALLOC_PAGE_COUNT pages).\n", cPages, VBOX_MAX_ALLOC_PAGE_COUNT * (_1M / _4K)));
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;
}
/**
* Maps a chunk of memory previously allocated by SUPR0PageAllocEx into kernel
* space.
*
* @returns IPRT status code.
* @param pSession The session to associated the allocation with.
* @param pvR3 The ring-3 address returned by SUPR0PageAllocEx.
* @param offSub Where to start mapping. Must be page aligned.
* @param cbSub How much to map. Must be page aligned.
* @param fFlags Flags, MBZ.
* @param ppvR0 Where to return the address of the ring-0 mapping on
* success.
*/
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 further input validations before calling IPRT.
* (Cleanup is done indirectly by telling RTR0MemObjFree to include mappings.)
*/
{
if (RT_SUCCESS(rc))
}
else
}
return rc;
}
/**
* Changes the page level protection of one or more pages previously allocated
* by SUPR0PageAllocEx.
*
* @returns IPRT status code.
* @param pSession The session to associated the allocation with.
* @param pvR3 The ring-3 address returned by SUPR0PageAllocEx.
* NIL_RTR3PTR if the ring-3 mapping should be unaffected.
* @param pvR0 The ring-0 address returned by SUPR0PageAllocEx.
* NIL_RTR0PTR if the ring-0 mapping should be unaffected.
* @param offSub Where to start changing. Must be page aligned.
* @param cbSub How much to change. Must be page aligned.
* @param fProt The new page level protection, see RTMEM_PROT_*.
*/
SUPR0DECL(int) SUPR0PageProtect(PSUPDRVSESSION pSession, RTR3PTR pvR3, RTR0PTR pvR0, uint32_t offSub, uint32_t cbSub, uint32_t fProt)
{
int rc;
LogFlow(("SUPR0PageProtect: pSession=%p pvR3=%p pvR0=%p offSub=%#x cbSub=%#x fProt-%#x\n", pSession, pvR3, pvR0, offSub, cbSub, fProt));
/*
* Validate input. The allowed allocation size must be at least equal to the maximum guest VRAM size.
*/
AssertReturn(!(fProt & ~(RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC | RTMEM_PROT_NONE)), VERR_INVALID_PARAMETER);
/*
* Find the memory object.
*/
{
{
unsigned i;
{
|| pvR3 == NIL_RTR3PTR)
&& ( pvR0 == NIL_RTR0PTR
&& ( pvR3 == NIL_RTR3PTR
{
if (pvR0 != NIL_RTR0PTR)
if (pvR3 != NIL_RTR3PTR)
break;
}
}
}
}
if ( hMemObjR0 != NIL_RTR0MEMOBJ
|| hMemObjR3 != NIL_RTR0MEMOBJ)
{
/*
* Do some further input validations before calling IPRT.
*/
{
rc = VINF_SUCCESS;
if (hMemObjR3 != NIL_RTR0PTR)
}
else
}
return rc;
}
/**
* 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().
*/
{
}
/**
* 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
}
/**
* Suspends hardware virtualization extensions using the native OS API.
*
* This is called prior to entering raw-mode context.
*
* @returns @c true if suspended, @c false if not.
*/
SUPR0DECL(bool) SUPR0SuspendVTxOnCpu(void)
{
#ifdef RT_OS_DARWIN
return supdrvOSSuspendVTxOnCpu();
#else
return false;
#endif
}
/**
* Resumes hardware virtualization extensions using the native OS API.
*
* This is called after to entering raw-mode context.
*
* @param fSuspended The return value of SUPR0SuspendVTxOnCpu.
*/
{
#ifdef RT_OS_DARWIN
#else
Assert(!fSuspended);
#endif
}
/**
* Checks if Intel VT-x feature is usable on this CPU.
*
* @returns VBox status code.
* @param fIsSmxModeAmbiguous Where to write whether the SMX mode causes
* ambiguity that makes us unsure whether we
* really can use VT-x or not.
*
* @remarks Must be called with preemption disabled.
*/
{
bool fMaybeSmxMode;
bool fMsrLocked;
bool fSmxVmxAllowed;
bool fVmxAllowed;
bool fIsSmxModeAmbiguous;
int rc;
fIsSmxModeAmbiguous = false;
/* Check if the LOCK bit is set but excludes the required VMXON bit. */
if (fMsrLocked)
{
if (fVmxAllowed && fSmxVmxAllowed)
rc = VINF_SUCCESS;
else if (!fVmxAllowed && !fSmxVmxAllowed)
else if (!fMaybeSmxMode)
{
if (fVmxAllowed)
rc = VINF_SUCCESS;
else
}
else
{
/*
* CR4.SMXE is set but this doesn't mean the CPU is necessarily in SMX mode. We shall assume
* See @bugref{6873}.
*/
Assert(fMaybeSmxMode == true);
fIsSmxModeAmbiguous = true;
rc = VINF_SUCCESS;
}
}
else
{
/*
* MSR is not yet locked; we can change it ourselves here.
* Once the lock bit is set, this MSR can no longer be modified.
*
* Set both the VMXON and SMX_VMXON bits as we can't determine SMX mode
* accurately. See @bugref{6873}.
*/
/* Verify. */
if (fSmxVmxAllowed && fVmxAllowed)
rc = VINF_SUCCESS;
else
}
if (pfIsSmxModeAmbiguous)
return rc;
}
/**
* Checks if AMD-V SVM feature is usable on this CPU.
*
* @returns VBox status code.
* @param fInitSvm If usable, try to initialize SVM on this CPU.
*
* @remarks Must be called with preemption disabled.
*/
{
int rc;
if (!(fVmCr & MSR_K8_VM_CR_SVM_DISABLE))
{
rc = VINF_SUCCESS;
if (fInitSvm)
{
/* Turn on SVM in the EFER MSR. */
if (fEfer & MSR_K6_EFER_SVME)
else
{
/* Paranoia. */
if (fEfer & MSR_K6_EFER_SVME)
{
/* Restore previous value. */
}
else
}
}
}
else
return rc;
}
/**
* Queries the AMD-V and VT-x capabilities of the calling CPU.
*
* @returns VBox status code.
* @retval VERR_VMX_NO_VMX
* @retval VERR_VMX_MSR_ALL_VMXON_DISABLED
* @retval VERR_VMX_MSR_VMXON_DISABLED
* @retval VERR_VMX_MSR_LOCKING_FAILED
* @retval VERR_SVM_NO_SVM
* @retval VERR_SVM_DISABLED
* @retval VERR_UNSUPPORTED_CPU if not identifiable as an AMD, Intel or VIA
* (centaur) CPU.
*
* @param pSession The session handle.
* @param pfCaps Where to store the capabilities.
*/
{
int rc = VERR_UNSUPPORTED_CPU;
bool fIsSmxModeAmbiguous = false;
/*
* Input validation.
*/
*pfCaps = 0;
/* We may modify MSRs and re-read them, disable preemption so we make sure we don't migrate CPUs. */
if (ASMHasCpuId())
{
if ( ASMIsValidStdRange(uMaxId)
)
{
if ( (fFeaturesECX & X86_CPUID_FEATURE_ECX_VMX)
)
{
if (rc == VINF_SUCCESS)
{
*pfCaps |= SUPVTCAPS_VT_X;
{
}
}
}
else
}
&& ASMIsValidStdRange(uMaxId))
{
/* Check if SVM is available. */
if ( ASMIsValidExtRange(uExtMaxId)
&& uExtMaxId >= 0x8000000a
)
{
if (RT_SUCCESS(rc))
{
*pfCaps |= SUPVTCAPS_AMD_V;
/* Query AMD-V features. */
}
}
else
}
}
if (fIsSmxModeAmbiguous)
SUPR0Printf(("WARNING! CR4 hints SMX mode but your CPU is too secretive. Proceeding anyway... We wish you good luck!\n"));
return rc;
}
/**
* (Re-)initializes the per-cpu structure prior to starting or resuming the GIP
* updating.
*
* @param pGip Pointer to the GIP.
* @param pGipCpu The per CPU structure for this CPU.
* @param u64NanoTS The current time.
*/
{
/*
* Here we don't really care about applying the TSC delta. The re-initialization of this
* value is not relevant especially while (re)starting the GIP as the first few ones will
* be ignored anyway, see supdrvGipDoUpdateCpu().
*/
}
/**
* Set the current TSC and NanoTS value for the CPU.
*
* @param idCpu The CPU ID. Unused - we have to use the APIC ID.
* @param pvUser1 Pointer to the ring-0 GIP mapping.
* @param pvUser2 Pointer to the variable holding the current time.
*/
{
}
/**
* State structure for supdrvGipDetectGetGipCpuCallback.
*/
typedef struct SUPDRVGIPDETECTGETCPU
{
/** Bitmap of APIC IDs that has been seen (initialized to zero).
* Used to detect duplicate APIC IDs (paranoia). */
/** Mask of supported GIP CPU getter methods (SUPGIPGETCPU_XXX) (all bits set
* initially). The callback clears the methods not detected. */
uint32_t volatile fSupported;
/** The first callback detecting any kind of range issues (initialized to
* NIL_RTCPUID). */
RTCPUID volatile idCpuProblem;
/** Pointer to state structure for supdrvGipDetectGetGipCpuCallback. */
typedef SUPDRVGIPDETECTGETCPU *PSUPDRVGIPDETECTGETCPU;
/**
* Checks for alternative ways of getting the CPU ID.
*
* This also checks the APIC ID, CPU ID and CPU set index values against the
* GIP tables.
*
* @param idCpu The CPU ID. Unused - we have to use the APIC ID.
* @param pvUser1 Pointer to the state structure.
* @param pvUser2 Pointer to the GIP.
*/
static DECLCALLBACK(void) supdrvGipDetectGetGipCpuCallback(RTCPUID idCpu, void *pvUser1, void *pvUser2)
{
uint32_t fSupported = 0;
int iCpuSet;
AssertMsg(idCpu == RTMpCpuId(), ("idCpu=%#x RTMpCpuId()=%#x\n", idCpu, RTMpCpuId())); /* paranoia^3 */
/*
* Check that the CPU ID and CPU set index are interchangable.
*/
{
if ( iCpuSet >= 0
&& iCpuSet < RTCPUSET_MAX_CPUS
{
/*
* Check whether the IDTR.LIMIT contains a CPU number.
*/
#ifdef RT_ARCH_X86
#else
#endif
ASMGetIDTR(&Idtr);
{
{
ASMGetIDTR(&Idtr2);
}
}
/*
* Check whether RDTSCP is an option.
*/
if (ASMHasCpuId())
{
{
{
ASMNopPause();
}
}
}
}
}
/*
* Check that the APIC ID is unique.
*/
idApic = ASMGetApicId();
else
{
LogRel(("supdrvGipDetectGetGipCpuCallback: idCpu=%#x iCpuSet=%d idApic=%#x - duplicate APIC ID.\n",
}
/*
* Check that the iCpuSet is within the expected range.
*/
if (RT_UNLIKELY( iCpuSet < 0
|| iCpuSet >= RTCPUSET_MAX_CPUS
{
LogRel(("supdrvGipDetectGetGipCpuCallback: idCpu=%#x iCpuSet=%d idApic=%#x - CPU set index is out of range.\n",
}
else
{
{
LogRel(("supdrvGipDetectGetGipCpuCallback: idCpu=%#x iCpuSet=%d idApic=%#x - CPU id/index roundtrip problem: %#x\n",
}
}
/*
* Update the supported feature mask before we return.
*/
}
/**
* Increase the timer freqency on hosts where this is possible (NT).
*
* The idea is that more interrupts is better for us... Also, it's better than
* we increase the timer frequence, because we might end up getting inaccurate
* callbacks if someone else does it.
*
* @param pDevExt Sets u32SystemTimerGranularityGrant if increased.
*/
{
if (pDevExt->u32SystemTimerGranularityGrant == 0)
{
)
{
}
}
}
/**
* Undoes supdrvGipRequestHigherTimerFrequencyFromSystem.
*
* @param pDevExt Clears u32SystemTimerGranularityGrant.
*/
{
{
}
}
/**
*
* @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;
/*
* Validate
*/
#ifdef SUPDRV_USE_MUTEX_FOR_GIP
#else
#endif
{
/*
* Map it?
*/
rc = VINF_SUCCESS;
if (ppGipR3)
{
if (RT_SUCCESS(rc))
}
/*
* Get physical address.
*/
/*
* Reference globally.
*/
{
{
/*
* host timer frequency to make sure we don't get stuck in guest
* mode and to get better timer (and possibly clock) accuracy.
*/
LogFlow(("SUPR0GipMap: Resumes GIP updating\n"));
/*
* document me
*/
{
unsigned i;
}
/*
* document me
*/
|| RTMpGetOnlineCount() == 1)
else
/*
* Detect alternative ways to figure the CPU ID in ring-3 and
* raw-mode context. Check the sanity of the APIC IDs, CPU IDs,
* and CPU set indexes while we're at it.
*/
if (RT_SUCCESS(rc))
{
{
&& DetectState.fSupported != 0)
{
{
}
}
else
{
LogRel(("SUPR0GipMap: No supported ways of getting the APIC ID or CPU number in ring-3! (%#x)\n",
}
}
else
{
LogRel(("SUPR0GipMap: APIC ID, CPU ID or CPU set index problem detected on CPU #%u (%#x)!\n",
}
}
/*
* Start the GIP timer if all is well..
*/
if (RT_SUCCESS(rc))
{
#ifndef DO_NOT_START_GIP
#endif
rc = VINF_SUCCESS;
}
/*
* Bail out on error.
*/
if (RT_FAILURE(rc))
{
pSession->fGipReferenced = 0;
{
if (RT_SUCCESS(rc2))
}
}
}
}
}
else
{
Log(("SUPR0GipMap: GIP is not available!\n"));
}
#ifdef SUPDRV_USE_MUTEX_FOR_GIP
#else
#endif
/*
* Write returns.
*/
if (pHCPhysGip)
*pHCPhysGip = HCPhys;
if (ppGipR3)
#ifdef DEBUG_DARWIN_GIP
OSDBGPRINT(("SUPR0GipMap: returns %d *pHCPhysGip=%lx pGipR3=%p\n", rc, (unsigned long)HCPhys, (void *)pGipR3));
#else
LogFlow(( "SUPR0GipMap: returns %d *pHCPhysGip=%lx pGipR3=%p\n", rc, (unsigned long)HCPhys, (void *)pGipR3));
#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
#ifdef SUPDRV_USE_MUTEX_FOR_GIP
#else
#endif
/*
* Unmap anything?
*/
{
if (RT_SUCCESS(rc))
}
/*
* Dereference global GIP.
*/
{
pSession->fGipReferenced = 0;
{
LogFlow(("SUPR0GipUnmap: Suspends GIP updating\n"));
#ifndef DO_NOT_START_GIP
#endif
}
}
#ifdef SUPDRV_USE_MUTEX_FOR_GIP
#else
#endif
return rc;
}
/**
* Gets the GIP pointer.
*
* @returns Pointer to the GIP or NULL.
*/
{
return g_pSUPGlobalInfoPage;
}
/**
* 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)
{
{
void *pvFactory = pCur->pFactory->pfnQueryFactoryInterface(pCur->pFactory, pSession, pszInterfaceUuid);
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.
*/
{
int rc;
void *pv;
LogFlow(("supdrvIOCtl_LdrOpen: szName=%s cbImageWithTabs=%d\n", pReq->u.In.szName, pReq->u.In.cbImageWithTabs));
/*
* Check if we got an instance of the image already.
*/
{
{
{
/** @todo check cbImageBits and cbImageWithTabs here, if they differs that indicates that the images are different. */
return VINF_SUCCESS;
}
return VERR_INTERNAL_ERROR_3; /** @todo add VERR_TOO_MANY_REFERENCES */
}
}
/* (not found - add it!) */
/* If the loader interface is locked down, make userland fail early */
if (pDevExt->fLdrLockedDown)
{
Log(("supdrvIOCtl_LdrOpen: Not adding '%s' to image list, loader interface is locked down!\n", pReq->u.In.szName));
return VERR_PERMISSION_DENIED;
}
/*
* Allocate memory.
*/
if (!pv)
{
Log(("supdrvIOCtl_LdrOpen: RTMemAlloc() failed\n"));
return /*VERR_NO_MEMORY*/ VERR_INTERNAL_ERROR_2;
}
/*
* Setup and link in the LDR stuff.
*/
/*
* Try load it using the native loader, if that isn't supported, fall back
* on the older method.
*/
if (rc == VERR_NOT_SUPPORTED)
{
}
if (RT_FAILURE(rc))
{
return rc;
}
/*
* Link it.
*/
return VINF_SUCCESS;
}
/**
* Worker that validates a pointer to an image entrypoint.
*
* @returns IPRT status code.
* @param pDevExt The device globals.
* @param pImage The loader image.
* @param pv The pointer into the image.
* @param fMayBeNull Whether it may be NULL.
* @param pszWhat What is this entrypoint? (for logging)
* @param pbImageBits The image bits prepared by ring-3.
*
* @remarks Will leave the lock on failure.
*/
{
if (!fMayBeNull || pv)
{
{
return VERR_INVALID_PARAMETER;
}
{
if (RT_FAILURE(rc))
{
return rc;
}
}
}
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 cbImageWithBits=%d\n", pReq->u.In.pvImageBase, pReq->u.In.cbImageWithTabs));
/*
* Find the ldr image.
*/
if (!pUsage)
{
Log(("SUP_IOCTL_LDR_LOAD: couldn't find image!\n"));
return VERR_INVALID_HANDLE;
}
/*
* Validate input.
*/
{
Log(("SUP_IOCTL_LDR_LOAD: image size mismatch!! %d(prep) != %d(load) or %d != %d\n",
return VERR_INVALID_HANDLE;
}
{
if (uState != SUP_IOCTL_LDR_LOAD)
return VERR_ALREADY_LOADED;
}
/* If the loader interface is locked down, don't load new images */
if (pDevExt->fLdrLockedDown)
{
Log(("SUP_IOCTL_LDR_LOAD: Not loading '%s' image bits, loader interface is locked down!\n", pImage->szName));
return VERR_PERMISSION_DENIED;
}
{
case SUPLDRLOADEP_NOTHING:
break;
case SUPLDRLOADEP_VMMR0:
rc = supdrvLdrValidatePointer( pDevExt, pImage, pReq->u.In.EP.VMMR0.pvVMMR0, false, pReq->u.In.abImage, "pvVMMR0");
if (RT_SUCCESS(rc))
rc = supdrvLdrValidatePointer(pDevExt, pImage, pReq->u.In.EP.VMMR0.pvVMMR0EntryInt, false, pReq->u.In.abImage, "pvVMMR0EntryInt");
if (RT_SUCCESS(rc))
rc = supdrvLdrValidatePointer(pDevExt, pImage, pReq->u.In.EP.VMMR0.pvVMMR0EntryFast, false, pReq->u.In.abImage, "pvVMMR0EntryFast");
if (RT_SUCCESS(rc))
rc = supdrvLdrValidatePointer(pDevExt, pImage, pReq->u.In.EP.VMMR0.pvVMMR0EntryEx, false, pReq->u.In.abImage, "pvVMMR0EntryEx");
if (RT_FAILURE(rc))
return rc;
break;
case SUPLDRLOADEP_SERVICE:
rc = supdrvLdrValidatePointer(pDevExt, pImage, pReq->u.In.EP.Service.pfnServiceReq, false, pReq->u.In.abImage, "pfnServiceReq");
if (RT_FAILURE(rc))
return rc;
{
Log(("Out of range (%p LB %#x): apvReserved={%p,%p,%p} MBZ!\n",
return VERR_INVALID_PARAMETER;
}
break;
default:
return VERR_INVALID_PARAMETER;
}
rc = supdrvLdrValidatePointer(pDevExt, pImage, pReq->u.In.pfnModuleInit, true, pReq->u.In.abImage, "pfnModuleInit");
if (RT_FAILURE(rc))
return rc;
rc = supdrvLdrValidatePointer(pDevExt, pImage, pReq->u.In.pfnModuleTerm, true, pReq->u.In.abImage, "pfnModuleTerm");
if (RT_FAILURE(rc))
return rc;
/*
* Allocate and copy the tables.
*/
{
if (pImage->pachStrTab)
else
}
{
else
}
/*
* Copy the bits / complete native loading.
*/
if (RT_SUCCESS(rc))
{
else
{
}
}
/*
* Update any entry points.
*/
if (RT_SUCCESS(rc))
{
{
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.
*/
{
}
if (RT_FAILURE(rc))
{
/* Inform the tracing component in case ModuleInit registered TPs. */
}
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->cbImageBits))
{
break;
}
}
if (rc == VINF_SUCCESS)
{
/* unlink it */
if (pUsagePrev)
else
/* free it */
/*
* Dereference the image.
*/
else
}
else
{
}
}
else
{
/*
* Dereference both image and usage.
*/
}
return rc;
}
/**
* Lock down the image loader interface.
*
* @returns IPRT status code.
* @param pDevExt Device globals.
*/
{
LogFlow(("supdrvIOCtl_LdrLockDown:\n"));
if (!pDevExt->fLdrLockedDown)
{
pDevExt->fLdrLockedDown = true;
Log(("supdrvIOCtl_LdrLockDown: Image loader interface locked down\n"));
}
return VINF_SUCCESS;
}
/**
* Gets the address of a symbol in an open image.
*
* @returns IPRT status code.
* @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.
*
* Note! The int32_t is for native loading on solaris where the data
* and text segments are in very different places.
*/
{
{
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!
*/
{
/*
* Warn if we're releasing images while the image loader interface is
* locked down -- we won't be able to reload them!
*/
if (pDevExt->fLdrLockedDown)
Log(("supdrvLdrFree: Warning: unloading '%s' image, while loader interface is locked down!\n", pImage->szName));
/* 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
{
}
/* Inform the tracing component. */
/* do native unload if appropriate. */
/* free the image */
}
/**
* Acquires the loader lock.
*
* @returns IPRT status code.
* @param pDevExt The device extension.
*/
{
#ifdef SUPDRV_USE_MUTEX_FOR_LDR
#else
#endif
return rc;
}
/**
* Releases the loader lock.
*
* @returns IPRT status code.
* @param pDevExt The device extension.
*/
{
#ifdef SUPDRV_USE_MUTEX_FOR_LDR
#else
#endif
}
/**
* 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)
{
int rc;
/*
* Find the module first in the module referenced by the calling session.
*/
if (RT_SUCCESS(rc))
{
{
break;
}
if (pfnServiceReqHandler)
{
/*
* Call it.
*/
else
rc = pfnServiceReqHandler(pSession, pReq->u.In.uOperation, pReq->u.In.u64Arg, (PSUPR0SERVICEREQHDR)&pReq->abReqPkt[0]);
}
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 && !RT_ARCH_AMD64 && !DEBUG */
return VERR_NOT_IMPLEMENTED;
#endif /* RT_OS_WINDOWS && !RT_ARCH_AMD64 && !DEBUG */
}
/**
* Implements the logger settings request.
*
* @returns VBox status code.
* @param pDevExt The device extension.
* @param pSession The caller's session.
* @param pReq The request.
*/
static int supdrvIOCtl_LoggerSettings(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLOGGERSETTINGS pReq)
{
int rc;
/*
* Some further validation.
*/
{
break;
return VERR_INVALID_PARAMETER;
return VERR_ACCESS_DENIED;
break;
default:
return VERR_INTERNAL_ERROR;
}
/*
* Get the logger.
*/
{
break;
break;
default:
return VERR_INTERNAL_ERROR;
}
/*
* Do the job.
*/
{
if (pLogger)
{
if (RT_SUCCESS(rc))
}
else
rc = VERR_NOT_FOUND;
break;
{
if (pLogger)
else
{
static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
0 /* fFlags */,
? "VBOX_LOG"
: "VBOX_RELEASE_LOG",
NULL);
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
{
break;
break;
}
}
}
}
break;
}
{
break;
break;
}
break;
default:
{
break;
}
}
return rc;
}
/**
* Implements the MSR prober operations.
*
* @returns VBox status code.
* @param pDevExt The device extension.
* @param pReq The request.
*/
{
#ifdef SUPDRV_WITH_MSR_PROBER
int rc;
{
case SUPMSRPROBEROP_READ:
{
if (RT_SUCCESS(rc))
{
}
else if (rc == VERR_ACCESS_DENIED)
{
rc = VINF_SUCCESS;
}
break;
}
case SUPMSRPROBEROP_WRITE:
if (RT_SUCCESS(rc))
else if (rc == VERR_ACCESS_DENIED)
{
rc = VINF_SUCCESS;
}
break;
case SUPMSRPROBEROP_MODIFY:
break;
default:
return VERR_INVALID_FUNCTION;
}
return rc;
#else
return VERR_NOT_IMPLEMENTED;
#endif
}
/**
* Returns whether the host CPU sports an invariant TSC or not.
*
* @returns true if invariant TSC is supported, false otherwise.
*/
static bool supdrvIsInvariantTsc(void)
{
static bool s_fQueried = false;
static bool s_fIsInvariantTsc = false;
if (!s_fQueried)
{
if (ASMHasCpuId())
{
if (uEax >= 0x80000007)
{
s_fIsInvariantTsc = true;
}
}
s_fQueried = true;
}
return s_fIsInvariantTsc;
}
#ifdef SUPDRV_USE_TSC_DELTA_THREAD
/**
* Switches the TSC-delta measurement thread into the butchered state.
*
* @returns VBox status code.
* @param pDevExt Pointer to the device instance data.
* @param fSpinlockHeld Whether the TSC-delta spinlock is held or not.
* @param pszFailed An error message to log.
* @param rcFailed The error code to exit the thread with.
*/
static int supdrvTscDeltaThreadButchered(PSUPDRVDEVEXT pDevExt, bool fSpinlockHeld, const char *pszFailed, int rcFailed)
{
if (!fSpinlockHeld)
return rcFailed;
}
/**
* The TSC-delta measurement thread.
*
* @returns VBox status code.
* @param hThread The thread handle.
* @param pvUser Opaque pointer to the device instance data.
*/
{
static uint32_t cTimesMeasured = 0;
int rc = VERR_INTERNAL_ERROR_2;
for (;;)
{
/*
* Switch on the current state.
*/
switch (enmState)
{
{
if (RT_FAILURE(rc))
/* fall thru */
}
{
/* Simple adaptive timeout. */
if (cConsecutiveTimeouts++ == 10)
{
cConsecutiveTimeouts = 0;
}
if ( RT_FAILURE(rc)
&& rc != VERR_TIMEOUT)
break;
}
{
if (RT_FAILURE(rc))
RTThreadSleep(10);
/* fall thru */
}
{
cConsecutiveTimeouts = 0;
if (!cTimesMeasured++)
{
}
else
{
unsigned iCpu;
if (cTimesMeasured == UINT32_MAX)
cTimesMeasured = 1;
/* Measure TSC-deltas only for the CPUs that are in the set. */
rc = VINF_SUCCESS;
{
{
}
}
}
break;
}
return VINF_SUCCESS;
default:
return supdrvTscDeltaThreadButchered(pDevExt, true /* fSpinlockHeld */, "Invalid state", VERR_INVALID_STATE);
}
}
return rc;
}
/**
* Waits for the TSC-delta measurement thread to respond to a state change.
*
* @returns VINF_SUCCESS on success, VERR_TIMEOUT if it doesn't respond in time,
* other error code on internal error.
*
* @param pThis Pointer to the grant service instance data.
* @param enmCurState The current state.
* @param enmNewState The new state we're waiting for it to enter.
*/
static int supdrvTscDeltaThreadWait(PSUPDRVDEVEXT pDevExt, SUPDRVTSCDELTASTATE enmCurState, SUPDRVTSCDELTASTATE enmNewState)
{
/*
* Wait a short while for the expected state transition.
*/
int rc;
{
rc = VINF_SUCCESS;
}
{
/*
* Wait longer if the state has not yet transitioned to the one we want.
*/
if ( RT_SUCCESS(rc)
|| rc == VERR_TIMEOUT)
{
/*
* Check the state whether we've succeeded.
*/
if (enmState == enmNewState)
rc = VINF_SUCCESS;
else if (enmState == enmCurState)
{
rc = VERR_TIMEOUT;
OSDBGPRINT(("supdrvTscDeltaThreadWait: timed out state transition. enmState=%d enmNewState=%d\n", enmState,
enmNewState));
}
else
{
OSDBGPRINT(("supdrvTscDeltaThreadWait: invalid state transition from %d to %d, expected %d\n", enmCurState,
enmState, enmNewState));
}
}
else
}
else
{
OSDBGPRINT(("supdrvTscDeltaThreadWait: invalid state transition from %d to %d\n", enmCurState, enmNewState));
}
return rc;
}
/**
* Terminates the TSC-delta measurement thread.
*
* @param pDevExt Pointer to the device instance data.
*/
{
int rc;
if (RT_FAILURE(rc))
{
/* Signal a few more times before giving up. */
int cTriesLeft = 5;
while (--cTriesLeft > 0)
{
if (rc != VERR_TIMEOUT)
break;
}
}
}
/**
* Initializes and spawns the TSC-delta measurement thread.
*
* A thread is required for servicing re-measurement requests from events like
* under all contexts on all OSs.
*
* @returns VBox status code.
* @param pDevExt Pointer to the device instance data.
*
* @remarks Must only be called -after- initializing GIP and setting up MP
* notifications!
*/
{
int rc;
rc = RTSpinlockCreate(&pDevExt->hTscDeltaSpinlock, RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, "VBoxTscSpnLck");
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
rc = supdrvTscDeltaThreadWait(pDevExt, kSupDrvTscDeltaState_Creating, kSupDrvTscDeltaState_Listening);
if (RT_SUCCESS(rc))
{
return rc;
}
}
else
}
else
}
else
return rc;
}
/**
* Terminates the TSC-delta measurement thread and cleanup.
*
* @param pDevExt Pointer to the device instance data.
*/
{
{
}
{
}
{
}
}
/**
* Waits for TSC-delta measurements to be completed for all online CPUs.
*
* @returns VBox status code.
* @param pDevExt Pointer to the device instance data.
*/
{
int cTriesLeft = 5;
int cMsTotalWait;
int cMsWaited = 0;
int cMsWaitGranularity = 1;
return VERR_THREAD_NOT_WAITABLE;
while (cTriesLeft-- > 0)
{
return VINF_SUCCESS;
if (cMsWaited >= cMsTotalWait)
break;
}
return VERR_TIMEOUT;
}
#endif /* SUPDRV_USE_TSC_DELTA_THREAD */
/**
* Applies the TSC delta to the supplied raw TSC value.
*
* @returns VBox status code. (Ignored by all users, just FYI.)
* @param pGip Pointer to the GIP.
* @param puTsc Pointer to a valid TSC value before the TSC delta has been applied.
* @param idApic The APIC ID of the CPU @c puTsc corresponds to.
* @param fDeltaApplied Where to store whether the TSC delta was succesfully
* applied or not (optional, can be NULL).
*
* @remarks Maybe called with interrupts disabled in ring-0!
*
* @note Don't you dare change the delta calculation. If you really do, make
* sure you update all places where it's used (IPRT, SUPLibAll.cpp,
* SUPDrv.c, supdrvGipMpEvent, and more).
*/
DECLINLINE(int) supdrvTscDeltaApply(PSUPGLOBALINFOPAGE pGip, uint64_t *puTsc, uint16_t idApic, bool *pfDeltaApplied)
{
int rc;
/*
* Validate input.
*/
/*
* Carefully convert the idApic into a GIPCPU entry.
*/
{
{
/*
* Apply the delta if valid.
*/
{
if (pfDeltaApplied)
*pfDeltaApplied = true;
return VINF_SUCCESS;
}
rc = VINF_SUCCESS;
}
else
{
}
}
else
{
}
if (pfDeltaApplied)
*pfDeltaApplied = false;
return rc;
}
/**
* Measures the TSC frequency of the system.
*
* Uses a busy-wait method for the async. case as it is intended to help push
* the CPU frequency up, while for the invariant cases using a sleeping method.
*
* The TSC frequency can vary on systems which are not reported as invariant.
* On such systems the object of this function is to find out what the nominal,
* maximum TSC frequency under 'normal' CPU operation.
*
* @returns VBox status code.
* @param pDevExt Pointer to the device instance.
*
* @remarks Must be called only -after- measuring the TSC deltas.
*/
{
int cTriesLeft = 4;
/* Assert order. */
while (cTriesLeft-- > 0)
{
/*
* Synchronize with the host OS clock tick before reading the TSC.
* Especially important on older Windows version where the granularity is terrible.
*/
while (RTTimeSystemNanoTS() == u64NanoTsBefore)
ASMNopPause();
uFlags = ASMIntDisableFlags();
idApicBefore = ASMGetApicId();
u64TscBefore = ASMReadTSC();
{
/*
* Sleep-wait since the TSC frequency is constant, it eases host load.
* Shorter interval produces more variance in the frequency (esp. Windows).
*/
RTThreadSleep(200);
while (RTTimeSystemNanoTS() == u64NanoTsAfter)
ASMNopPause();
}
else
{
/* Busy-wait keeping the frequency up and measure. */
for (;;)
{
ASMNopPause();
else
break;
}
}
uFlags = ASMIntDisableFlags();
idApicAfter = ASMGetApicId();
u64TscAfter = ASMReadTSC();
{
int rc;
bool fAppliedBefore;
bool fAppliedAfter;
rc = supdrvTscDeltaApply(pGip, &u64TscBefore, idApicBefore, &fAppliedBefore); AssertRCReturn(rc, rc);
if ( !fAppliedBefore
|| !fAppliedAfter)
{
#ifdef SUPDRV_USE_TSC_DELTA_THREAD
/*
* The TSC-delta measurements are kicked-off asynchronously as each host CPU is initialized.
* Therefore, if we failed to have a delta for the CPU(s) we were scheduled on (idApicBefore
* and idApicAfter) then wait until we have TSC-delta measurements for all online CPUs and
* proceed. This should be triggered just once if we're rather unlucky.
*/
if (rc == VERR_TIMEOUT)
{
SUPR0Printf("vboxdrv: supdrvGipMeasureTscFreq: timedout waiting for TSC-delta measurements.\n");
}
#else
SUPR0Printf("vboxdrv: supdrvGipMeasureTscFreq: idApicBefore=%u idApicAfter=%u cTriesLeft=%u\n",
#endif
continue;
}
}
/*
* Update GIP.
*/
pGip->u64CpuHz = ((u64TscAfter - u64TscBefore) * RT_NS_1SEC_64) / (u64NanoTsAfter - u64NanoTsBefore);
return VINF_SUCCESS;
}
}
/**
* Timer callback function for TSC frequency refinement in invariant GIP mode.
*
* @param pTimer The timer.
* @param pvUser Opaque pointer to the device instance data.
* @param iTick The timer tick.
*/
{
bool fDeltaApplied = false;
/* Paranoia. */
#if !defined(RT_OS_OS2) /* PORTME: Disable if timers are called from clock interrupt handler or with interrupts disabled. */
while (RTTimeSystemNanoTS() == u64NanoTS)
ASMNopPause();
#endif
uFlags = ASMIntDisableFlags();
idApic = ASMGetApicId();
u64Tsc = ASMReadTSC();
&& !fDeltaApplied))
{
Log(("vboxdrv: failed to refine TSC frequency as TSC-deltas unavailable after %d seconds!\n",
return;
}
/* Calculate the TSC frequency. */
&& u64DeltaNanoTS < UINT32_MAX)
else
{
}
/* Update rest of GIP. */
}
/**
* Starts the TSC-frequency refinement phase asynchronously.
*
* @param pDevExt Pointer to the device instance data.
*/
{
int rc;
/* Validate. */
#ifdef SUPDRV_USE_TSC_DELTA_THREAD
/*
* If the TSC-delta thread is created, wait until it's done calculating
* the TSC-deltas on the relevant online CPUs before we start the TSC refinement.
*/
{
if (rc == VERR_TIMEOUT)
{
SUPR0Printf("vboxdrv: Skipping refinement of TSC frequency as TSC-delta measurement timed out!\n");
return;
}
}
#endif
/*
* Record the TSC and NanoTS as the starting anchor point for refinement of the
* TSC. We deliberately avoid using SUPReadTSC() here as we want to keep the
* reading of the TSC and the NanoTS as close as possible.
*/
while (RTTimeSystemNanoTS() == u64NanoTS)
ASMNopPause();
uFlags = ASMIntDisableFlags();
idApic = ASMGetApicId();
rc = RTTimerCreateEx(&pDevExt->pTscRefineTimer, 0 /* one-shot */, RTTIMER_FLAGS_CPU_ANY, supdrvRefineTscTimer, pDevExt);
if (RT_SUCCESS(rc))
{
/*
* Refine the TSC frequency measurement over a long interval. Ideally, we want to keep the
* interval as small as possible while gaining the most consistent and accurate frequency
* (compared to what the host OS might have measured).
*
* In theory, we gain more accuracy with longer intervals, but we want VMs to startup with the
* same TSC frequency whenever possible so we need to keep the interval short.
*/
}
else
}
/**
* Creates the GIP.
*
* @returns VBox status code.
* @param pDevExt Instance data. GIP stuff may be updated.
*/
{
unsigned cCpus;
int rc;
LogFlow(("supdrvGipCreate:\n"));
/* Assert order. */
/*
* Check the CPU count.
*/
cCpus = RTMpGetArraySize();
if ( cCpus > RTCPUSET_MAX_CPUS
{
SUPR0Printf("VBoxDrv: Too many CPUs (%u) for the GIP (max %u)\n", cCpus, RT_MIN(RTCPUSET_MAX_CPUS, 256));
return VERR_TOO_MANY_CPUS;
}
/*
* Allocate a contiguous set of pages with a default kernel mapping.
*/
rc = RTR0MemObjAllocCont(&pDevExt->GipMemObj, RT_UOFFSETOF(SUPGLOBALINFOPAGE, aCPUs[cCpus]), false /*fExecutable*/);
if (RT_FAILURE(rc))
{
return rc;
}
/*
* Allocate the TSC-delta sync struct on a separate cache line.
*/
/*
* Find a reasonable update interval and initialize the structure.
*/
/** @todo figure out why using a 100Ms interval upsets timekeeping in VMs.
* See @bugref{6710}. */
if (uMod)
supdrvGipInit(pDevExt, pGip, HCPhysGip, RTTimeSystemNanoTS(), RT_NS_1SEC / u32Interval /*=Hz*/, u32Interval, cCpus);
{
/* Basically, invariant Windows boxes, should never be detected as async (i.e. TSC-deltas should be 0). */
OSDBGPRINT(("supdrvGipCreate: The TSC-deltas should be normalized by the host OS, but verifying shows it's not!\n"));
return VERR_INTERNAL_ERROR_2;
}
#ifdef SUPDRV_USE_TSC_DELTA_THREAD
{
/* Initialize TSC-delta measurement thread before executing any Mp event callbacks. */
}
#endif
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
#ifndef SUPDRV_USE_TSC_DELTA_THREAD
{
/*
* Measure the TSC deltas now that we have MP notifications.
*/
int cTries = 5;
do
{
if (rc != VERR_TRY_AGAIN)
break;
} while (--cTries > 0);
}
else
{
AssertMsg(!pGip->aCPUs[iCpu].i64TSCDelta, ("iCpu=%u %lld mode=%d\n", iCpu, pGip->aCPUs[iCpu].i64TSCDelta, pGip->u32Mode));
}
#endif
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
/*
* Create the timer.
* If CPU_ALL isn't supported we'll have to fall back to synchronous mode.
*/
{
pDevExt);
if (rc == VERR_NOT_SUPPORTED)
{
OSDBGPRINT(("supdrvGipCreate: omni timer not supported, falling back to synchronous mode\n"));
}
}
if (RT_SUCCESS(rc))
{
/*
* We're good.
*/
return VINF_SUCCESS;
}
OSDBGPRINT(("supdrvGipCreate: failed create GIP timer at %u ns interval. rc=%Rrc\n", u32Interval, rc));
}
else
}
else
}
else
}
else
}
else
return rc;
}
/**
* Terminates the GIP.
*
* @param pDevExt Instance data. GIP stuff may be updated.
*/
{
int rc;
#ifdef DEBUG_DARWIN_GIP
#endif
/*
* Stop receiving MP notifications before tearing anything else down.
*/
#ifdef SUPDRV_USE_TSC_DELTA_THREAD
/*
* Terminate the TSC-delta measurement thread and resources.
*/
#endif
/*
* Destroy the TSC-refinement one-shot timer.
*/
if (pDevExt->pTscRefineTimer)
{
}
if (pDevExt->pvTscDeltaSync)
{
}
/*
* Invalid the GIP data.
*/
{
}
/*
* Destroy the timer and free the GIP memory object.
*/
{
}
{
}
/*
* Finally, make sure we've release the system timer resolution request
* if one actually succeeded and is still pending.
*/
}
/**
* Timer callback function for the sync and invariant GIP modes.
*
* @param pTimer The timer.
* @param pvUser Opaque pointer to the device extension.
* @param iTick The timer tick.
*/
static DECLCALLBACK(void) supdrvGipSyncAndInvariantTimer(PRTTIMER pTimer, void *pvUser, uint64_t iTick)
{
u64TSC = ASMReadTSC();
{
/*
* The calculations in supdrvGipUpdate() is very timing sensitive and doesn't handle
* missed timer ticks. So for now it is better to use a delta of 0 and have the TSC rate
* affected a bit until we get proper TSC deltas than implementing options like
* rescheduling the tick to be delivered on the right CPU or missing the tick entirely.
*
* The likely hood of this happening is really low. On Windows, Linux, and Solaris
* timers fire on the CPU they were registered/started on. Darwin timers doesn't
* necessarily (they are high priority threads waiting).
*/
Assert(!ASMIntAreEnabled());
}
}
/**
* Timer callback function for async GIP mode.
* @param pTimer The timer.
* @param pvUser Opaque pointer to the device extension.
* @param iTick The timer tick.
*/
{
/** @todo reset the transaction number and whatnot when iTick == 1. */
else
}
/**
* Finds our (@a idCpu) entry, or allocates a new one if not found.
*
* @returns Index of the CPU in the cache set.
* @param pGip The GIP.
* @param idCpu The CPU ID.
*/
{
/*
* ASSUMES that CPU IDs are constant.
*/
return i;
cTries = 0;
do
{
{
bool fRc;
if (fRc)
return i;
}
} while (cTries++ < 32);
return i - 1;
}
/**
* The calling CPU should be accounted as online, update GIP accordingly.
*
* This is used by supdrvGipMpEvent as well as the supdrvGipCreate.
*
* @param pDevExt The device extension.
* @param idCpu The CPU ID.
*/
{
int iCpuSet = 0;
uint32_t i = 0;
/*
* Do this behind a spinlock with interrupts disabled as this can fire
* on all CPUs simultaneously, see @bugref{6110}.
*/
/*
* Update the globals.
*/
if (iCpuSet >= 0)
{
}
/*
* Update the entry.
*/
idApic = ASMGetApicId();
/*
* Update the APIC ID and CPU set index mappings.
*/
#ifdef SUPDRV_USE_TSC_DELTA_THREAD
/*
* Add this CPU to the set of CPUs that require their TSC delta to be measured.
*
* We cannot poke the TSC-delta measurement thread from this context (on all OSs), so we only
* update the state and it'll get serviced when the thread's listening interval times out.
*/
{
{
}
}
#endif
/* commit it */
}
/**
* The CPU should be accounted as offline, update the GIP accordingly.
*
* This is used by supdrvGipMpEvent.
*
* @param pDevExt The device extension.
* @param idCpu The CPU ID.
*/
{
int iCpuSet;
unsigned i;
AssertReturnVoid(iCpuSet >= 0);
/* If we are the initiator going offline while measuring the TSC delta, unspin other waiting CPUs! */
{
}
/* Reset the TSC delta, we will recalculate it lazily. */
#ifdef SUPDRV_USE_TSC_DELTA_THREAD
/* Remove this CPU from the set of CPUs that we have obtained the TSC deltas. */
if (supdrvIsInvariantTsc())
#endif
/* commit it */
}
/**
* Multiprocessor event notification callback.
*
* This is used to make sure that the GIP master gets passed on to
* another CPU. It also updates the associated CPU data.
*
* @param enmEvent The event.
* @param idCpu The cpu it applies to.
* @param pvUser Pointer to the device extension.
*
* @remarks This function -must- fire on the newly online'd CPU for the
* RTMPEVENT_ONLINE case and can fire on any CPU for the
* RTMPEVENT_OFFLINE case.
*/
{
/*
* Update the GIP CPU data.
*/
if (pGip)
{
switch (enmEvent)
{
case RTMPEVENT_ONLINE:
break;
case RTMPEVENT_OFFLINE:
break;
}
}
/*
* Make sure there is a master GIP.
*/
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;
}
}
/*
* Adjust all the TSC deltas against the new GIP master.
*/
if (pGip)
{
{
if (iWorkerDelta != INT64_MAX)
}
}
}
}
}
/**
* Callback used by supdrvMeasureTscDeltas() to read the TSC on two CPUs and
* compute the delta between them.
*
* @param idCpu The CPU we are current scheduled on.
* @param pvUser1 Opaque pointer to the device instance data.
* @param pvUser2 Opaque pointer to the worker Cpu Id.
*
* @remarks Measuring TSC deltas between the CPUs is tricky because we need to
* read the TSC at exactly the same time on both the master and the worker
* CPUs. Due to DMA, bus arbitration, cache locality, contention, SMI,
* pipelining etc. there is no guaranteed way of doing this on x86 CPUs. We
* try to minimize the measurement error by computing the minimum read time
* of the compare statement in the worker by taking TSC measurements across
* it.
*
* We ignore the first few runs of the loop in order to prime the cache.
* Also, be careful about using 'pause' instruction in critical busy-wait
* loops in this code - it can cause undesired behaviour with
* hyperthreading.
*
* It must be noted that the computed minimum read time is mostly to
* eliminate huge deltas when the worker is too early and doesn't by itself
* help produce more accurate deltas. We allow two times the computed
* minimum as an arbibtrary acceptable threshold. Therefore, it is still
* possible to get negative deltas where there are none when the worker is
* earlier. As long as these occasional negative deltas are lower than the
* time it takes to exit guest-context and the OS to reschedule EMT on a
* different CPU we won't expose a TSC that jumped backwards. It is because
* of the existence of the negative deltas we don't recompute the delta with
* the master and worker interchanged to eliminate the remaining measurement
* error.
*/
static DECLCALLBACK(void) supdrvMeasureTscDeltaCallback(RTCPUID idCpu, void *pvUser1, void *pvUser2)
{
int cTriesLeft = 12;
return;
/* If the IPRT API isn't concurrent safe, the master and worker wait for each other
with a timeout to avoid deadlocking the entire system. */
if (!RTMpOnAllIsConcurrentSafe())
{
/** @todo This was introduced for Windows, but since Windows doesn't use this
* code path any longer (as DPC timeouts BSOD regardless of interrupts,
* see @bugref{6710} comment 81), eventually phase it out. */
uTscStart = ASMReadTSC();
{
{
uTscNow = ASMReadTSC();
{
/* Set the worker delta to indicate failure, not the master. */
return;
}
ASMNopPause();
}
}
else
{
{
uTscNow = ASMReadTSC();
{
return;
}
ASMNopPause();
}
}
}
while (cTriesLeft-- > 0)
{
unsigned i;
for (i = 0; i < GIP_TSC_DELTA_LOOPS; i++)
{
{
/*
* The master.
*/
/* Disable interrupts only in the master for as short a period
as possible, thanks again to Windows. See @bugref{6710} comment #73. */
uFlags = ASMIntDisableFlags();
{ /* nothing */ }
do
{
{ /* nothing */ }
{
{
}
}
}
else
{
/*
* The worker.
*/
{ /* nothing */ }
/*
* Keep reading the TSC until we notice that the master has read his. Reading
* the TSC -after- the master has updated the memory is way too late. We thus
* compensate by trying to measure how long it took for the worker to notice
* the memory flushed from the master.
*/
do
{
uTscWorker = ASMReadTSC();
{
/* This is totally arbitrary a.k.a I don't like it but I have no better ideas for now. */
{
if (uCmpReadTime < uMinCmpReadTime)
}
else
}
else if (i > GIP_TSC_DELTA_PRIMER_LOOPS)
{
if (uCmpReadTime < uMinCmpReadTime)
}
ASMNopPause();
}
}
break;
}
}
/**
* Clears TSC delta related variables.
*
* Clears all TSC samples as well as the delta synchronization variable on the
* all the per-CPU structs. Optionally also clears the per-cpu deltas too.
*
* @param pDevExt Pointer to the device instance data.
* @param fClearDeltas Whether the deltas are also to be cleared.
*/
{
unsigned iCpu;
{
if (fClearDeltas)
}
}
/**
* Measures the TSC delta between the master GIP CPU and one specified worker
* CPU.
*
* @returns VBox status code.
* @param pDevExt Pointer to the device instance data.
* @param idxWorker The index of the worker CPU from the GIP's array of
* CPUs.
*
* @remarks This can be called with preemption disabled!
*/
{
int rc;
{
return VINF_SUCCESS;
}
/* Set the master TSC as the initiator. */
{
/*
* Sleep here rather than spin as there is a parallel measurement
* being executed and that can take a good while to be done.
*/
RTThreadSleep(1);
}
{
/* Fire TSC-read workers on all CPUs but only synchronize between master and one worker to ease memory contention. */
if (RT_SUCCESS(rc))
{
{
/*
* Work the TSC delta applicability rating. It starts
* optimistic in supdrvGipInit, we downgrade it here.
*/
else
{
}
}
else
}
}
else
return rc;
}
/**
* Measures the TSC deltas between CPUs.
*
* @param pDevExt Pointer to the device instance data.
* @param pidxMaster Where to store the index of the chosen master TSC if we
* managed to determine the TSC deltas successfully.
* Optional, can be NULL.
*
* @returns VBox status code.
* @remarks Must be called only after supdrvGipInitOnCpu() as this function uses
* idCpu, GIP's online CPU set which are populated in
* supdrvGipInitOnCpu().
*/
{
unsigned iCpu;
int rc = VINF_SUCCESS;
/*
* Pick the first CPU online as the master TSC and make it the new GIP master based
* on the APIC ID.
*
* Technically we can simply use "idGipMaster" but doing this gives us master as CPU 0
*/
{
if (idxCpu != UINT16_MAX)
{
{
pGipCpu->i64TSCDelta = 0;
break;
}
}
}
{
if (pidxMaster)
*pidxMaster = idxMaster;
return VINF_SUCCESS;
}
{
{
if (RT_FAILURE(rc))
{
SUPR0Printf("supdrvMeasureTscDeltaOne failed. rc=%d CPU[%u].idCpu=%u Master[%u].idCpu=%u\n", rc, iCpu,
break;
}
{
SUPR0Printf("One or more CPUs transitioned between online & offline states. I'm confused, retrying...\n");
rc = VERR_TRY_AGAIN;
break;
}
}
}
if ( RT_SUCCESS(rc)
&& !pGipCpuMaster->i64TSCDelta
&& pidxMaster)
{
*pidxMaster = idxMaster;
}
return rc;
}
/**
* 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)
{
}
/**
* 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 (optional, can be NULL).
* @return false if the time stamp counters appear to be synchronized, 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 iEndCpu = RTMpGetArraySize();
int iCpu;
int cLoops = 8;
bool fAsync = false;
int rc = VINF_SUCCESS;
while (cLoops-- > 0)
{
{
if (RT_SUCCESS(rc))
{
{
fAsync = true;
Log(("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;
}
if (poffMin)
Log(("supdrvDetermineAsyncTsc: returns %d; iEndCpu=%d rc=%d offMin=%llx offMax=%llx\n",
#endif
return fAsync;
}
/**
* Determine the GIP TSC mode.
*
* @returns The most suitable TSC mode.
* @param pDevExt Pointer to the device instance data.
*/
{
/* Trust CPUs that declare their TSC to be invariant. */
#if 0 /** @todo this cannot be enabled until Michal's AMD laptop with insane deltas are working. */
if (supdrvIsInvariantTsc())
return SUPGIPMODE_INVARIANT_TSC;
#endif
/*
* Without invariant CPU ID bit - 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 slightly 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 symptoms 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.
*/
/** @todo this is now redundant. remove later. */
/* Check for "AuthenticAMD" */
if ( uEAX >= 1
{
/* Check for APM support and that TscInvariant is cleared. */
if (uEAX >= 0x80000007)
{
return SUPGIPMODE_ASYNC_TSC;
}
}
}
/** @todo later remove this when the above todo with AMD laptop is done (i.e.
* TSC deltas handled everywhere). */
if (supdrvIsInvariantTsc())
return SUPGIPMODE_INVARIANT_TSC;
return SUPGIPMODE_SYNC_TSC;
}
/**
* Initializes per-CPU GIP information.
*
* @param pDevExt Pointer to the device instance data.
* @param pGip Pointer to the GIP.
* @param pCpu Pointer to which GIP CPU to initalize.
* @param u64NanoTS The current nanosecond timestamp.
*/
static void supdrvGipInitCpu(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE pGip, PSUPGIPCPU pCpu, uint64_t u64NanoTS)
{
/* !!! Warning !!! The GIP may not be linked to the device instance data at this point!
which is why we have 2 separate parameters. Don't dereference pDevExt->pGip here. */
/*
* We don't know the following values until we've executed updates.
* So, we'll just pretend it's a 4 GHz CPU and adjust the history it on
* the 2nd timer callout.
*/
= pCpu->au32TSCHistory[0]
}
/**
* Initializes the GIP data.
*
* @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 frequency.
* @param uUpdateIntervalNS The update interval in nanoseconds.
* @param cCpus The CPU count.
*/
{
unsigned i;
#ifdef DEBUG_DARWIN_GIP
OSDBGPRINT(("supdrvGipInit: pGip=%p HCPhys=%lx u64NanoTS=%llu uUpdateHz=%d cCpus=%u\n", pGip, (long)HCPhys, u64NanoTS, uUpdateHz, cCpus));
#else
LogFlow(("supdrvGipInit: pGip=%p HCPhys=%lx u64NanoTS=%llu uUpdateHz=%d cCpus=%u\n", pGip, (long)HCPhys, u64NanoTS, uUpdateHz, cCpus));
#endif
/*
* Initialize the structure.
*/
/*|| pGip->u32Mode == SUPGIPMODE_SYNC_TSC */)
pGip->enmUseTscDelta = supdrvIsInvariantTsc() && supdrvOSAreTscDeltasInSync() /* Allow OS override (windows). */
else
for (i = 0; i < cCpus; i++)
/*
* Link it to the device extension.
*/
}
/**
* On CPU initialization callback for RTMpOnAll.
*
* @param idCpu The CPU ID.
* @param pvUser1 The device extension.
* @param pvUser2 The GIP.
*/
{
/* This is good enough, even though it will update some of the globals a
bit to much. */
}
/**
* 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 pDevExt The device extension.
* @param pGipCpu Pointer to the per cpu data.
* @param u64NanoTS The current time stamp.
* @param u64TSC The current TSC.
* @param iTick The current timer tick.
*
* @remarks Can be called with interrupts disabled!
*/
static void supdrvGipDoUpdateCpu(PSUPDRVDEVEXT pDevExt, PSUPGIPCPU pGipCpu, uint64_t u64NanoTS, uint64_t u64TSC, uint64_t iTick)
{
unsigned iTSCHistoryHead;
/* Delta between this and the previous update. */
/*
* Update the NanoTS.
*/
/*
* Calc TSC delta.
*/
/* We don't need to keep realculating the frequency when it's invariant. */
return;
if (u64TSCDelta >> 32)
{
}
/*
* On the 2nd and 3rd callout, reset the history with the current TSC
* interval since the values entered by supdrvGipInit are totally off.
* The interval on the 1st callout completely unreliable, the 2nd is a bit
* better, while the 3rd should be most reliable.
*/
|| u32TransactionId == 7)
&& ( iTick == 2
|| iTick == 3) ))
{
unsigned i;
}
/*
* Validate the NanoTS deltas between timer fires with an arbitrary threshold of 0.5%.
* Wait until we have at least one full history since the above history reset. The
* assumption is that the majority of the previous history values will be tolerable.
* See @bugref{6710} comment #67.
*/
{
{
u32 >>= 2;
u64TSCDelta >>= 2;
u64TSCDelta += u32;
u64TSCDelta >>= 1;
}
}
/*
* TSC History.
*/
/*
* UpdateIntervalTSC = average of last 8,2,1 intervals depending on update HZ.
*
* On Windows, we have an occasional (but recurring) sour value that messed up
* the history but taking only 1 interval reduces the precision overall.
* However, this problem existed before the invariant mode was introduced.
*/
{
u32 >>= 2;
u32UpdateIntervalTSC >>= 2;
u32UpdateIntervalTSC >>= 1;
/* Value chosen for a 2GHz Athlon64 running linux 2.6.10/11. */
}
{
u32UpdateIntervalTSC >>= 1;
/* value chosen on a 2GHz thinkpad running windows */
}
else
{
/* This value hasn't be checked yet.. waiting for OS/2 and 33Hz timers.. :-) */
}
ASMAtomicWriteU32(&pGipCpu->u32UpdateIntervalTSC, u32UpdateIntervalTSC + u32UpdateIntervalTSCSlack);
/*
* CpuHz.
*/
}
/**
* Updates the GIP.
*
* @param pDevExt The device extension.
* @param u64NanoTS The current nanosecond timesamp.
* @param u64TSC The current TSC timesamp.
* @param idCpu The CPU ID.
* @param iTick The current timer tick.
*
* @remarks Can be called with interrupts disabled!
*/
static void supdrvGipUpdate(PSUPDRVDEVEXT pDevExt, uint64_t u64NanoTS, uint64_t u64TSC, RTCPUID idCpu, uint64_t iTick)
{
/*
* Determine the relevant CPU data.
*/
else
{
return;
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->u32Mode != SUPGIPMODE_INVARIANT_TSC /* cuz we're not recalculating the frequency on invariants hosts. */
{
if (pGip->u64NanoTSLastUpdateHz)
{
#ifdef RT_ARCH_AMD64 /** @todo fix 64-bit div here to work on x86 linux. */
{
/** @todo r=ramshankar: Changing u32UpdateHz might screw up TSC frequency
* calculation on non-invariant hosts if it changes the history decision
* taken in supdrvGipDoUpdateCpu(). */
}
#endif
}
}
/*
* Update the data.
*/
/*
* Complete transaction.
*/
}
/**
* Updates the per cpu GIP data for the calling cpu.
*
* @param pDevExt The device extension.
* @param u64NanoTS The current nanosecond timesamp.
* @param u64TSC The current TSC timesamp.
* @param idCpu The CPU ID.
* @param idApic The APIC id for the CPU index.
* @param iTick The current timer tick.
*
* @remarks Can be called with interrupts disabled!
*/
{
/*
* Avoid a potential race when a CPU online notification doesn't fire on
* the onlined CPU but the tick creeps in before the event notification is
* run.
*/
{
}
{
{
/*
* Start update transaction.
*/
{
return;
}
/*
* Update the data.
*/
/*
* Complete transaction.
*/
}
}
}
/**
* Resume built-in keyboard on MacBook Air and Pro hosts.
* If there is no built-in keyboard device, return success anyway.
*
* @returns 0 on Mac OS X platform, VERR_NOT_IMPLEMENTED on the other ones.
*/
static int supdrvIOCtl_ResumeSuspendedKbds(void)
{
#if defined(RT_OS_DARWIN)
return supdrvDarwinResumeSuspendedKbds();
#else
return VERR_NOT_IMPLEMENTED;
#endif
}
/**
* Service a TSC-delta measurement request.
*
* @returns VBox status code.
* @param pDevExt Pointer to the device instance data.
* @param pSession The support driver session.
* @param pReq Pointer to the TSC-delta measurement request.
*/
static int supdrvIOCtl_TscDeltaMeasure(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPTSCDELTAMEASURE pReq)
{
int rc;
/*
* Validate.
*/
return VERR_WRONG_ORDER;
if (idCpuWorker == NIL_RTCPUID)
return VERR_INVALID_CPU_ID;
/*
* The request is a noop if the TSC delta isn't being used.
*/
return VINF_SUCCESS;
{
{
return VINF_SUCCESS;
#ifdef SUPDRV_USE_TSC_DELTA_THREAD
{
/** @todo Async. doesn't implement options like retries, waiting. We'll need
* to pass those options to the thread somehow and implement it in the
{
}
return VINF_SUCCESS;
}
#endif
while (cTries-- > 0)
{
if (RT_SUCCESS(rc))
{
break;
}
if (cMsWaitRetry)
}
break;
}
}
return rc;
}
/**
* Reads TSC with delta applied.
*
* Will try to resolve delta value INT64_MAX before applying it. This is the
* main purpose of this function, to handle the case where the delta needs to be
* determined.
*
* @returns VBox status code.
* @param pDevExt Pointer to the device instance data.
* @param pSession The support driver session.
* @param pReq Pointer to the TSC-read request.
*/
{
int rc;
/*
* Validate. We require the client to have mapped GIP (no asserting on
* ring-3 preconditions).
*/
return VERR_WRONG_ORDER;
/*
* We're usually here because we need to apply delta, but we shouldn't be
* upset if the GIP is some different mode.
*/
{
for (;;)
{
/*
* Start by gathering the data, using CLI for disabling preemption
* while we do that.
*/
int iGipCpu;
{
/*
* If we're lucky we've got a delta, but no predicitions here
* as this I/O control is normally only used when the TSC delta
* is set to INT64_MAX.
*/
{
rc = VINF_SUCCESS;
break;
}
/* Give up after a few times. */
if (cTries >= 4)
{
break;
}
/* Need to measure the delta an try again. */
}
else
{
/* This really shouldn't happen. */
break;
}
}
}
else
{
/*
* No delta to apply. Easy. Deal with preemption the lazy way.
*/
int iGipCpu;
else
rc = VINF_SUCCESS;
}
return rc;
}