SUPDrv.c revision dbb5c497af4fd871af632a41a391450464cd1135
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/* $Revision$ */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/** @file
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * VBoxDrv - The VirtualBox Support Driver - Common code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Copyright (C) 2006-2010 Oracle Corporation
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * This file is part of VirtualBox Open Source Edition (OSE), as
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * available from http://www.virtualbox.org. This file is free software;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * you can redistribute it and/or modify it under the terms of the GNU
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * General Public License (GPL) as published by the Free Software
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Foundation, in version 2 as it comes in the "COPYING" file of the
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * The contents of this file may alternatively be used under the terms
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * of the Common Development and Distribution License Version 1.0
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * VirtualBox OSE distribution, in which case the provisions of the
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * CDDL are applicable instead of those of the GPL.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * You may elect to license modified versions of this file under the
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * terms and conditions of either the GPL or the CDDL or both.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/*******************************************************************************
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico* Header Files *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico*******************************************************************************/
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#define LOG_GROUP LOG_GROUP_SUP_DRV
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#define SUPDRV_AGNOSTIC
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include "SUPDrvInternal.h"
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifndef PAGE_SHIFT
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico# include <iprt/param.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include <iprt/asm.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include <iprt/asm-amd64-x86.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include <iprt/asm-math.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include <iprt/cpuset.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include <iprt/handletable.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include <iprt/mem.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include <iprt/mp.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include <iprt/power.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include <iprt/process.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include <iprt/semaphore.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include <iprt/spinlock.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include <iprt/thread.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include <iprt/uuid.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include <iprt/net.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include <iprt/crc.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include <iprt/string.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include <iprt/timer.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#if defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico# include <iprt/rand.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico# include <iprt/path.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include <VBox/param.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include <VBox/log.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include <VBox/err.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include <VBox/hwacc_svm.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include <VBox/hwacc_vmx.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#include <VBox/x86.h>
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef VBOX_WITH_DTRACE
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico# include "SUPDrv-dtrace.h"
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico# define VBOXDRV_SUPDRV_SESSION_CREATE(pvSession, fUser) do { } while (0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico# define VBOXDRV_SUPDRV_SESSION_CLOSE(pvSession) do { } while (0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico# define VBOXDRV_SUPDRV_IOCCLOSE(pvSession) do { } while (0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico# define VBOXDRV_SUPDRV_IOCTL_ENTRY(pvSession, uIOCtl, pvReqHdr) do { } while (0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico# define VBOXDRV_SUPDRV_IOCTL_RETURN(pvSession, uIOCtl, pvReqHdr, rcRet, rcReq) do { } while (0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Logging assignments:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Log - useful stuff, like failures.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * LogFlow - program flow, except the really noisy bits.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Log2 - Cleanup.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Log3 - Loader flow noise.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Log4 - Call VMMR0 flow noise.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Log5 - Native yet-to-be-defined noise.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Log6 - Native ioctl flow noise.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Logging requires BUILD_TYPE=debug and possibly changes to the logger
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * instanciation in log-vbox.c(pp).
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/*******************************************************************************
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico* Defined Constants And Macros *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico*******************************************************************************/
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/** The frequency by which we recalculate the u32UpdateHz and
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * u32UpdateIntervalNS GIP members. The value must be a power of 2. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#define GIP_UPDATEHZ_RECALC_FREQ 0x800
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/** @def VBOX_SVN_REV
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * The makefile should define this if it can. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifndef VBOX_SVN_REV
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico# define VBOX_SVN_REV 0
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#if 0 /* Don't start the GIP timers. Useful when debugging the IPRT timer code. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico# define DO_NOT_START_GIP
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/*******************************************************************************
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico* Internal Functions *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico*******************************************************************************/
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic DECLCALLBACK(int) supdrvSessionObjHandleRetain(RTHANDLETABLE hHandleTable, void *pvObj, void *pvCtx, void *pvUser);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic DECLCALLBACK(void) supdrvSessionObjHandleDelete(RTHANDLETABLE hHandleTable, uint32_t h, void *pvObj, void *pvCtx, void *pvUser);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvMemAdd(PSUPDRVMEMREF pMem, PSUPDRVSESSION pSession);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvMemRelease(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr, SUPDRVMEMREFTYPE eType);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvIOCtl_LdrOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDROPEN pReq);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvIOCtl_LdrLoad(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDRLOAD pReq);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvIOCtl_LdrFree(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDRFREE pReq);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvIOCtl_LdrGetSymbol(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDRGETSYMBOL pReq);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvIDC_LdrGetSymbol(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVIDCREQGETSYM pReq);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvLdrSetVMMR0EPs(PSUPDRVDEVEXT pDevExt, void *pvVMMR0, void *pvVMMR0EntryInt, void *pvVMMR0EntryFast, void *pvVMMR0EntryEx);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic void supdrvLdrUnsetVMMR0EPs(PSUPDRVDEVEXT pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvLdrAddUsage(PSUPDRVSESSION pSession, PSUPDRVLDRIMAGE pImage);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic void supdrvLdrFree(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoDECLINLINE(int) supdrvLdrLock(PSUPDRVDEVEXT pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoDECLINLINE(int) supdrvLdrUnlock(PSUPDRVDEVEXT pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvIOCtl_CallServiceModule(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPCALLSERVICE pReq);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvIOCtl_LoggerSettings(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLOGGERSETTINGS pReq);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvGipCreate(PSUPDRVDEVEXT pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic void supdrvGipDestroy(PSUPDRVDEVEXT pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic DECLCALLBACK(void) supdrvGipSyncTimer(PRTTIMER pTimer, void *pvUser, uint64_t iTick);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic DECLCALLBACK(void) supdrvGipAsyncTimer(PRTTIMER pTimer, void *pvUser, uint64_t iTick);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic DECLCALLBACK(void) supdrvGipMpEvent(RTMPEVENT enmEvent, RTCPUID idCpu, void *pvUser);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic void supdrvGipInit(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE pGip, RTHCPHYS HCPhys, uint64_t u64NanoTS, unsigned uUpdateHz);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic void supdrvGipTerm(PSUPGLOBALINFOPAGE pGip);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic void supdrvGipUpdate(PSUPGLOBALINFOPAGE pGip, uint64_t u64NanoTS, uint64_t u64TSC, uint64_t iTick);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic void supdrvGipUpdatePerCpu(PSUPGLOBALINFOPAGE pGip, uint64_t u64NanoTS, uint64_t u64TSC, unsigned iCpu, uint64_t iTick);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/*******************************************************************************
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico* Global Variables *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico*******************************************************************************/
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoDECLEXPORT(PSUPGLOBALINFOPAGE) g_pSUPGlobalInfoPage = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Array of the R0 SUP API.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic SUPFUNC g_aFunctions[] =
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* name function */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Entries with absolute addresses determined at runtime, fixup
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico code makes ugly ASSUMPTIONS about the order here: */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0AbsIs64bit", (void *)0 },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0Abs64bitKernelCS", (void *)0 },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0Abs64bitKernelSS", (void *)0 },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0Abs64bitKernelDS", (void *)0 },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0AbsKernelCS", (void *)0 },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0AbsKernelSS", (void *)0 },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0AbsKernelDS", (void *)0 },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0AbsKernelES", (void *)0 },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0AbsKernelFS", (void *)0 },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0AbsKernelGS", (void *)0 },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Normal function pointers: */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0ComponentRegisterFactory", (void *)SUPR0ComponentRegisterFactory },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0ComponentDeregisterFactory", (void *)SUPR0ComponentDeregisterFactory },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0ComponentQueryFactory", (void *)SUPR0ComponentQueryFactory },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0ObjRegister", (void *)SUPR0ObjRegister },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0ObjAddRef", (void *)SUPR0ObjAddRef },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0ObjAddRefEx", (void *)SUPR0ObjAddRefEx },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0ObjRelease", (void *)SUPR0ObjRelease },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0ObjVerifyAccess", (void *)SUPR0ObjVerifyAccess },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0LockMem", (void *)SUPR0LockMem },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0UnlockMem", (void *)SUPR0UnlockMem },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0ContAlloc", (void *)SUPR0ContAlloc },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0ContFree", (void *)SUPR0ContFree },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0LowAlloc", (void *)SUPR0LowAlloc },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0LowFree", (void *)SUPR0LowFree },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0MemAlloc", (void *)SUPR0MemAlloc },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0MemGetPhys", (void *)SUPR0MemGetPhys },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0MemFree", (void *)SUPR0MemFree },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0PageAllocEx", (void *)SUPR0PageAllocEx },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0PageFree", (void *)SUPR0PageFree },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0Printf", (void *)SUPR0Printf }, /** @todo needs wrapping? */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPSemEventCreate", (void *)SUPSemEventCreate },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPSemEventClose", (void *)SUPSemEventClose },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPSemEventSignal", (void *)SUPSemEventSignal },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPSemEventWait", (void *)SUPSemEventWait },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPSemEventWaitNoResume", (void *)SUPSemEventWaitNoResume },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPSemEventWaitNsAbsIntr", (void *)SUPSemEventWaitNsAbsIntr },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPSemEventWaitNsRelIntr", (void *)SUPSemEventWaitNsRelIntr },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPSemEventGetResolution", (void *)SUPSemEventGetResolution },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPSemEventMultiCreate", (void *)SUPSemEventMultiCreate },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPSemEventMultiClose", (void *)SUPSemEventMultiClose },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPSemEventMultiSignal", (void *)SUPSemEventMultiSignal },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPSemEventMultiReset", (void *)SUPSemEventMultiReset },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPSemEventMultiWait", (void *)SUPSemEventMultiWait },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPSemEventMultiWaitNoResume", (void *)SUPSemEventMultiWaitNoResume },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPSemEventMultiWaitNsAbsIntr", (void *)SUPSemEventMultiWaitNsAbsIntr },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPSemEventMultiWaitNsRelIntr", (void *)SUPSemEventMultiWaitNsRelIntr },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPSemEventMultiGetResolution", (void *)SUPSemEventMultiGetResolution },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0GetPagingMode", (void *)SUPR0GetPagingMode },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPR0EnableVTx", (void *)SUPR0EnableVTx },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "SUPGetGIP", (void *)SUPGetGIP },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "g_pSUPGlobalInfoPage", (void *)&g_pSUPGlobalInfoPage },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMemAllocTag", (void *)RTMemAllocTag },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMemAllocZTag", (void *)RTMemAllocZTag },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMemAllocVarTag", (void *)RTMemAllocVarTag },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMemAllocZVarTag", (void *)RTMemAllocZVarTag },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMemFree", (void *)RTMemFree },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMemDupTag", (void *)RTMemDupTag },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMemDupExTag", (void *)RTMemDupExTag },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMemReallocTag", (void *)RTMemReallocTag },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0MemObjAllocLowTag", (void *)RTR0MemObjAllocLowTag },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0MemObjAllocPageTag", (void *)RTR0MemObjAllocPageTag },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0MemObjAllocPhysTag", (void *)RTR0MemObjAllocPhysTag },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0MemObjAllocPhysExTag", (void *)RTR0MemObjAllocPhysExTag },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0MemObjAllocPhysNCTag", (void *)RTR0MemObjAllocPhysNCTag },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0MemObjAllocContTag", (void *)RTR0MemObjAllocContTag },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0MemObjEnterPhysTag", (void *)RTR0MemObjEnterPhysTag },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0MemObjLockUserTag", (void *)RTR0MemObjLockUserTag },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0MemObjMapKernelTag", (void *)RTR0MemObjMapKernelTag },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0MemObjMapKernelExTag", (void *)RTR0MemObjMapKernelExTag },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0MemObjMapUserTag", (void *)RTR0MemObjMapUserTag },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0MemObjProtect", (void *)RTR0MemObjProtect },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0MemObjAddress", (void *)RTR0MemObjAddress },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0MemObjAddressR3", (void *)RTR0MemObjAddressR3 },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0MemObjSize", (void *)RTR0MemObjSize },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0MemObjIsMapping", (void *)RTR0MemObjIsMapping },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0MemObjGetPagePhysAddr", (void *)RTR0MemObjGetPagePhysAddr },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0MemObjFree", (void *)RTR0MemObjFree },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0MemUserCopyFrom", (void *)RTR0MemUserCopyFrom },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0MemUserCopyTo", (void *)RTR0MemUserCopyTo },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0MemUserIsValidAddr", (void *)RTR0MemUserIsValidAddr },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0MemKernelIsValidAddr", (void *)RTR0MemKernelIsValidAddr },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0MemAreKrnlAndUsrDifferent", (void *)RTR0MemAreKrnlAndUsrDifferent },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemMutexCreate", (void *)RTSemMutexCreate },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemMutexRequest", (void *)RTSemMutexRequest },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemMutexRequestDebug", (void *)RTSemMutexRequestDebug },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemMutexRequestNoResume", (void *)RTSemMutexRequestNoResume },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemMutexRequestNoResumeDebug", (void *)RTSemMutexRequestNoResumeDebug },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemMutexRelease", (void *)RTSemMutexRelease },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemMutexDestroy", (void *)RTSemMutexDestroy },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTProcSelf", (void *)RTProcSelf },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0ProcHandleSelf", (void *)RTR0ProcHandleSelf },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemFastMutexCreate", (void *)RTSemFastMutexCreate },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemFastMutexDestroy", (void *)RTSemFastMutexDestroy },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemFastMutexRequest", (void *)RTSemFastMutexRequest },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemFastMutexRelease", (void *)RTSemFastMutexRelease },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemEventCreate", (void *)RTSemEventCreate },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemEventSignal", (void *)RTSemEventSignal },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemEventWait", (void *)RTSemEventWait },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemEventWaitNoResume", (void *)RTSemEventWaitNoResume },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemEventWaitEx", (void *)RTSemEventWaitEx },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemEventWaitExDebug", (void *)RTSemEventWaitExDebug },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemEventGetResolution", (void *)RTSemEventGetResolution },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemEventDestroy", (void *)RTSemEventDestroy },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemEventMultiCreate", (void *)RTSemEventMultiCreate },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemEventMultiSignal", (void *)RTSemEventMultiSignal },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemEventMultiReset", (void *)RTSemEventMultiReset },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemEventMultiWait", (void *)RTSemEventMultiWait },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemEventMultiWaitNoResume", (void *)RTSemEventMultiWaitNoResume },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemEventMultiWaitEx", (void *)RTSemEventMultiWaitEx },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemEventMultiWaitExDebug", (void *)RTSemEventMultiWaitExDebug },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemEventMultiGetResolution", (void *)RTSemEventMultiGetResolution },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSemEventMultiDestroy", (void *)RTSemEventMultiDestroy },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSpinlockCreate", (void *)RTSpinlockCreate },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSpinlockDestroy", (void *)RTSpinlockDestroy },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSpinlockAcquire", (void *)RTSpinlockAcquire },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSpinlockRelease", (void *)RTSpinlockRelease },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSpinlockAcquireNoInts", (void *)RTSpinlockAcquireNoInts },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTSpinlockReleaseNoInts", (void *)RTSpinlockReleaseNoInts },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTTimeNanoTS", (void *)RTTimeNanoTS },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTTimeMilliTS", (void *)RTTimeMilliTS },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTTimeSystemNanoTS", (void *)RTTimeSystemNanoTS },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTTimeSystemMilliTS", (void *)RTTimeSystemMilliTS },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTThreadNativeSelf", (void *)RTThreadNativeSelf },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTThreadSleep", (void *)RTThreadSleep },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTThreadYield", (void *)RTThreadYield },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#if 0 /* Thread APIs, Part 2. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTThreadSelf", (void *)RTThreadSelf },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTThreadCreate", (void *)RTThreadCreate },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTThreadGetNative", (void *)RTThreadGetNative },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTThreadWait", (void *)RTThreadWait },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTThreadWaitNoResume", (void *)RTThreadWaitNoResume },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTThreadGetName", (void *)RTThreadGetName },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTThreadSelfName", (void *)RTThreadSelfName },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTThreadGetType", (void *)RTThreadGetType },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTThreadUserSignal", (void *)RTThreadUserSignal },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTThreadUserReset", (void *)RTThreadUserReset },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTThreadUserWait", (void *)RTThreadUserWait },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTThreadUserWaitNoResume", (void *)RTThreadUserWaitNoResume },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTThreadPreemptIsEnabled", (void *)RTThreadPreemptIsEnabled },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTThreadPreemptIsPending", (void *)RTThreadPreemptIsPending },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTThreadPreemptIsPendingTrusty", (void *)RTThreadPreemptIsPendingTrusty },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTThreadPreemptIsPossible", (void *)RTThreadPreemptIsPossible },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTThreadPreemptDisable", (void *)RTThreadPreemptDisable },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTThreadPreemptRestore", (void *)RTThreadPreemptRestore },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTThreadIsInInterrupt", (void *)RTThreadIsInInterrupt },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTTimerCreate", (void *)RTTimerCreate },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTTimerCreateEx", (void *)RTTimerCreateEx },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTTimerDestroy", (void *)RTTimerDestroy },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTTimerStart", (void *)RTTimerStart },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTTimerStop", (void *)RTTimerStop },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTTimerChangeInterval", (void *)RTTimerChangeInterval },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTTimerGetSystemGranularity", (void *)RTTimerGetSystemGranularity },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTTimerRequestSystemGranularity", (void *)RTTimerRequestSystemGranularity },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTTimerReleaseSystemGranularity", (void *)RTTimerReleaseSystemGranularity },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTTimerCanDoHighResolution", (void *)RTTimerCanDoHighResolution },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTLogDefaultInstance", (void *)RTLogDefaultInstance },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMpCpuId", (void *)RTMpCpuId },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMpCpuIdFromSetIndex", (void *)RTMpCpuIdFromSetIndex },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMpCpuIdToSetIndex", (void *)RTMpCpuIdToSetIndex },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMpGetArraySize", (void *)RTMpGetArraySize },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMpIsCpuPossible", (void *)RTMpIsCpuPossible },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMpGetCount", (void *)RTMpGetCount },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMpGetMaxCpuId", (void *)RTMpGetMaxCpuId },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMpGetOnlineCount", (void *)RTMpGetOnlineCount },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMpGetOnlineSet", (void *)RTMpGetOnlineSet },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMpGetSet", (void *)RTMpGetSet },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMpIsCpuOnline", (void *)RTMpIsCpuOnline },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMpIsCpuWorkPending", (void *)RTMpIsCpuWorkPending },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMpOnAll", (void *)RTMpOnAll },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMpOnOthers", (void *)RTMpOnOthers },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMpOnSpecific", (void *)RTMpOnSpecific },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTMpPokeCpu", (void *)RTMpPokeCpu },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTPowerNotificationRegister", (void *)RTPowerNotificationRegister },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTPowerNotificationDeregister", (void *)RTPowerNotificationDeregister },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTLogRelDefaultInstance", (void *)RTLogRelDefaultInstance },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTLogSetDefaultInstanceThread", (void *)RTLogSetDefaultInstanceThread },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTLogLoggerExV", (void *)RTLogLoggerExV },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTLogPrintfV", (void *)RTLogPrintfV },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTR0AssertPanicSystem", (void *)RTR0AssertPanicSystem },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTAssertMsg1", (void *)RTAssertMsg1 },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTAssertMsg2V", (void *)RTAssertMsg2V },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTAssertSetQuiet", (void *)RTAssertSetQuiet },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTAssertMayPanic", (void *)RTAssertMayPanic },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTAssertSetMayPanic", (void *)RTAssertSetMayPanic },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTAssertAreQuiet", (void *)RTAssertAreQuiet },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTStrFormat", (void *)RTStrFormat },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTStrFormatNumber", (void *)RTStrFormatNumber },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTStrFormatTypeDeregister", (void *)RTStrFormatTypeDeregister },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTStrFormatTypeRegister", (void *)RTStrFormatTypeRegister },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTStrFormatTypeSetUser", (void *)RTStrFormatTypeSetUser },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTStrFormatV", (void *)RTStrFormatV },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTStrPrintf", (void *)RTStrPrintf },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTStrPrintfEx", (void *)RTStrPrintfEx },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTStrPrintfExV", (void *)RTStrPrintfExV },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTStrPrintfV", (void *)RTStrPrintfV },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTHandleTableAllocWithCtx", (void *)RTHandleTableAllocWithCtx },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTHandleTableCreate", (void *)RTHandleTableCreate },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTHandleTableCreateEx", (void *)RTHandleTableCreateEx },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTHandleTableDestroy", (void *)RTHandleTableDestroy },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTHandleTableFreeWithCtx", (void *)RTHandleTableFreeWithCtx },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTHandleTableLookupWithCtx", (void *)RTHandleTableLookupWithCtx },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTNetIPv4AddDataChecksum", (void *)RTNetIPv4AddDataChecksum },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTNetIPv4AddTCPChecksum", (void *)RTNetIPv4AddTCPChecksum },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTNetIPv4AddUDPChecksum", (void *)RTNetIPv4AddUDPChecksum },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTNetIPv4FinalizeChecksum", (void *)RTNetIPv4FinalizeChecksum },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTNetIPv4HdrChecksum", (void *)RTNetIPv4HdrChecksum },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTNetIPv4IsDHCPValid", (void *)RTNetIPv4IsDHCPValid },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTNetIPv4IsHdrValid", (void *)RTNetIPv4IsHdrValid },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTNetIPv4IsTCPSizeValid", (void *)RTNetIPv4IsTCPSizeValid },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTNetIPv4IsTCPValid", (void *)RTNetIPv4IsTCPValid },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTNetIPv4IsUDPSizeValid", (void *)RTNetIPv4IsUDPSizeValid },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTNetIPv4IsUDPValid", (void *)RTNetIPv4IsUDPValid },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTNetIPv4PseudoChecksum", (void *)RTNetIPv4PseudoChecksum },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTNetIPv4PseudoChecksumBits", (void *)RTNetIPv4PseudoChecksumBits },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTNetIPv4TCPChecksum", (void *)RTNetIPv4TCPChecksum },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTNetIPv4UDPChecksum", (void *)RTNetIPv4UDPChecksum },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTNetIPv6PseudoChecksum", (void *)RTNetIPv6PseudoChecksum },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTNetIPv6PseudoChecksumBits", (void *)RTNetIPv6PseudoChecksumBits },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTNetIPv6PseudoChecksumEx", (void *)RTNetIPv6PseudoChecksumEx },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTNetTCPChecksum", (void *)RTNetTCPChecksum },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTNetUDPChecksum", (void *)RTNetUDPChecksum },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTCrc32", (void *)RTCrc32 },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTCrc32Finish", (void *)RTCrc32Finish },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTCrc32Process", (void *)RTCrc32Process },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { "RTCrc32Start", (void *)RTCrc32Start },
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico};
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#if defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Drag in the rest of IRPT since we share it with the
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * rest of the kernel modules on darwin.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoPFNRT g_apfnVBoxDrvIPRTDeps[] =
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* VBoxNetFlt */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico (PFNRT)RTErrConvertFromErrno,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico (PFNRT)RTUuidCompare,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico (PFNRT)RTUuidCompareStr,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico (PFNRT)RTUuidFromStr,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico (PFNRT)RTStrDupTag,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico (PFNRT)RTStrFree,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* VBoxNetAdp */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico (PFNRT)RTRandBytes,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* VBoxUSB */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico (PFNRT)RTPathStripFilename,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico NULL
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico};
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif /* RT_OS_DARWIN || RT_OS_SOLARIS || RT_OS_SOLARIS */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Initializes the device extentsion structure.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt The device extension to initialize.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param cbSession The size of the session structure. The size of
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * SUPDRVSESSION may be smaller when SUPDRV_AGNOSTIC is
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * defined because we're skipping the OS specific members
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * then.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoint VBOXCALL supdrvInitDevExt(PSUPDRVDEVEXT pDevExt, size_t cbSession)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef SUPDRV_WITH_RELEASE_LOGGER
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Create the release log.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PRTLOGGER pRelLogger;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTLogCreate(&pRelLogger, 0 /* fFlags */, "all",
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico "VBOX_RELEASE_LOG", RT_ELEMENTS(s_apszGroups), s_apszGroups,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTLOGDEST_STDOUT | RTLOGDEST_DEBUGGER, NULL);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTLogRelSetDefaultInstance(pRelLogger);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /** @todo Add native hook for getting logger config parameters and setting
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * them. On linux we should use the module parameter stuff... */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Initialize it.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico memset(pDevExt, 0, sizeof(*pDevExt));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTSpinlockCreate(&pDevExt->Spinlock);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef SUPDRV_USE_MUTEX_FOR_LDR
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTSemMutexCreate(&pDevExt->mtxLdr);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTSemFastMutexCreate(&pDevExt->mtxLdr);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTSemFastMutexCreate(&pDevExt->mtxComponentFactory);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef SUPDRV_USE_MUTEX_FOR_LDR
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTSemMutexCreate(&pDevExt->mtxGip);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTSemFastMutexCreate(&pDevExt->mtxGip);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = supdrvGipCreate(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->u32Cookie = BIRD; /** @todo make this random? */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->cbSession = cbSession;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Fixup the absolute symbols.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Because of the table indexing assumptions we'll have a little #ifdef orgy
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * here rather than distributing this to OS specific files. At least for now.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef RT_OS_DARWIN
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico# if ARCH_BITS == 32
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (SUPR0GetPagingMode() >= SUPPAGINGMODE_AMD64)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[0].pfn = (void *)1; /* SUPR0AbsIs64bit */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[1].pfn = (void *)0x80; /* SUPR0Abs64bitKernelCS - KERNEL64_CS, seg.h */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[2].pfn = (void *)0x88; /* SUPR0Abs64bitKernelSS - KERNEL64_SS, seg.h */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[3].pfn = (void *)0x88; /* SUPR0Abs64bitKernelDS - KERNEL64_SS, seg.h */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[0].pfn = g_aFunctions[1].pfn = g_aFunctions[2].pfn = g_aFunctions[4].pfn = (void *)0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[4].pfn = (void *)0x08; /* SUPR0AbsKernelCS - KERNEL_CS, seg.h */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[5].pfn = (void *)0x10; /* SUPR0AbsKernelSS - KERNEL_DS, seg.h */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[6].pfn = (void *)0x10; /* SUPR0AbsKernelDS - KERNEL_DS, seg.h */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[7].pfn = (void *)0x10; /* SUPR0AbsKernelES - KERNEL_DS, seg.h */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[8].pfn = (void *)0x10; /* SUPR0AbsKernelFS - KERNEL_DS, seg.h */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[9].pfn = (void *)0x48; /* SUPR0AbsKernelGS - CPU_DATA_GS, seg.h */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico# else /* 64-bit darwin: */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[0].pfn = (void *)1; /* SUPR0AbsIs64bit */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[1].pfn = (void *)(uintptr_t)ASMGetCS(); /* SUPR0Abs64bitKernelCS */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[2].pfn = (void *)(uintptr_t)ASMGetSS(); /* SUPR0Abs64bitKernelSS */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[3].pfn = (void *)0; /* SUPR0Abs64bitKernelDS */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[4].pfn = (void *)(uintptr_t)ASMGetCS(); /* SUPR0AbsKernelCS */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[5].pfn = (void *)(uintptr_t)ASMGetSS(); /* SUPR0AbsKernelSS */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[6].pfn = (void *)0; /* SUPR0AbsKernelDS */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[7].pfn = (void *)0; /* SUPR0AbsKernelES */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[8].pfn = (void *)0; /* SUPR0AbsKernelFS */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[9].pfn = (void *)0; /* SUPR0AbsKernelGS */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico# endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#else /* !RT_OS_DARWIN */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico# if ARCH_BITS == 64
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[0].pfn = (void *)1; /* SUPR0AbsIs64bit */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[1].pfn = (void *)(uintptr_t)ASMGetCS(); /* SUPR0Abs64bitKernelCS */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[2].pfn = (void *)(uintptr_t)ASMGetSS(); /* SUPR0Abs64bitKernelSS */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[3].pfn = (void *)(uintptr_t)ASMGetDS(); /* SUPR0Abs64bitKernelDS */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico# else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[0].pfn = g_aFunctions[1].pfn = g_aFunctions[2].pfn = g_aFunctions[4].pfn = (void *)0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico# endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[4].pfn = (void *)(uintptr_t)ASMGetCS(); /* SUPR0AbsKernelCS */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[5].pfn = (void *)(uintptr_t)ASMGetSS(); /* SUPR0AbsKernelSS */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[6].pfn = (void *)(uintptr_t)ASMGetDS(); /* SUPR0AbsKernelDS */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[7].pfn = (void *)(uintptr_t)ASMGetES(); /* SUPR0AbsKernelES */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[8].pfn = (void *)(uintptr_t)ASMGetFS(); /* SUPR0AbsKernelFS */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_aFunctions[9].pfn = (void *)(uintptr_t)ASMGetGS(); /* SUPR0AbsKernelGS */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif /* !RT_OS_DARWIN */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef SUPDRV_USE_MUTEX_FOR_GIP
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSemMutexDestroy(pDevExt->mtxGip);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->mtxGip = NIL_RTSEMMUTEX;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSemFastMutexDestroy(pDevExt->mtxGip);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->mtxGip = NIL_RTSEMFASTMUTEX;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSemFastMutexDestroy(pDevExt->mtxComponentFactory);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->mtxComponentFactory = NIL_RTSEMFASTMUTEX;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef SUPDRV_USE_MUTEX_FOR_LDR
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSemMutexDestroy(pDevExt->mtxLdr);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->mtxLdr = NIL_RTSEMMUTEX;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSemFastMutexDestroy(pDevExt->mtxLdr);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->mtxLdr = NIL_RTSEMFASTMUTEX;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockDestroy(pDevExt->Spinlock);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->Spinlock = NIL_RTSPINLOCK;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef SUPDRV_WITH_RELEASE_LOGGER
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTLogDestroy(RTLogRelSetDefaultInstance(NULL));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTLogDestroy(RTLogSetDefaultInstance(NULL));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Delete the device extension (e.g. cleanup members).
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt The device extension to delete.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicovoid VBOXCALL supdrvDeleteDevExt(PSUPDRVDEVEXT pDevExt)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVOBJ pObj;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVUSAGE pUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Kill mutexes and spinlocks.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef SUPDRV_USE_MUTEX_FOR_GIP
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSemMutexDestroy(pDevExt->mtxGip);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->mtxGip = NIL_RTSEMMUTEX;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSemFastMutexDestroy(pDevExt->mtxGip);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->mtxGip = NIL_RTSEMFASTMUTEX;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef SUPDRV_USE_MUTEX_FOR_LDR
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSemMutexDestroy(pDevExt->mtxLdr);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->mtxLdr = NIL_RTSEMMUTEX;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSemFastMutexDestroy(pDevExt->mtxLdr);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->mtxLdr = NIL_RTSEMFASTMUTEX;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockDestroy(pDevExt->Spinlock);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->Spinlock = NIL_RTSPINLOCK;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSemFastMutexDestroy(pDevExt->mtxComponentFactory);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->mtxComponentFactory = NIL_RTSEMFASTMUTEX;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Free lists.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* objects. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj = pDevExt->pObjs;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Assert(!pObj); /* (can trigger on forced unloads) */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pObjs = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico while (pObj)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico void *pvFree = pObj;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj = pObj->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMemFree(pvFree);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* usage records. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage = pDevExt->pUsageFree;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pUsageFree = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico while (pUsage)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico void *pvFree = pUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage = pUsage->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMemFree(pvFree);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* kill the GIP. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvGipDestroy(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef SUPDRV_WITH_RELEASE_LOGGER
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* destroy the loggers. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTLogDestroy(RTLogRelSetDefaultInstance(NULL));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTLogDestroy(RTLogSetDefaultInstance(NULL));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Create session.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt Device extension.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param fUser Flag indicating whether this is a user or kernel session.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param ppSession Where to store the pointer to the session data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoint VBOXCALL supdrvCreateSession(PSUPDRVDEVEXT pDevExt, bool fUser, PSUPDRVSESSION *ppSession)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Allocate memory for the session data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVSESSION pSession = *ppSession = (PSUPDRVSESSION)RTMemAllocZ(pDevExt->cbSession);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pSession)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Initialize session data. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTSpinlockCreate(&pSession->Spinlock);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!rc)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTHandleTableCreateEx(&pSession->hHandleTable,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTHANDLETABLE_FLAGS_LOCKED | RTHANDLETABLE_FLAGS_CONTEXT,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico 1 /*uBase*/, 32768 /*cMax*/, supdrvSessionObjHandleRetain, pSession);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Assert(pSession->Spinlock != NIL_RTSPINLOCK);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->pDevExt = pDevExt;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->u32Cookie = BIRD_INV;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*pSession->pLdrUsage = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->pVM = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->pUsage = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->pGip = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->fGipReferenced = false;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->Bundle.cUsed = 0; */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->Uid = NIL_RTUID;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->Gid = NIL_RTGID;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (fUser)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->Process = RTProcSelf();
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->R0Process = RTR0ProcHandleSelf();
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->Process = NIL_RTPROCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->R0Process = NIL_RTR0PROCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_SESSION_CREATE(pSession, fUser);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("Created session %p initial cookie=%#x\n", pSession, pSession->u32Cookie));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockDestroy(pSession->Spinlock);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMemFree(pSession);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *ppSession = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("Failed to create spinlock, rc=%d!\n", rc));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VERR_NO_MEMORY;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Shared code for cleaning up a session.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt Device extension.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession Session data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * This data will be freed by this routine.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicovoid VBOXCALL supdrvCloseSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_SESSION_CLOSE(pSession);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Cleanup the session first.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvCleanupSession(pDevExt, pSession);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Free the rest of the session stuff.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockDestroy(pSession->Spinlock);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->Spinlock = NIL_RTSPINLOCK;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->pDevExt = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMemFree(pSession);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("supdrvCloseSession: returns\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Shared code for cleaning up a session (but not quite freeing it).
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * This is primarily intended for MAC OS X where we have to clean up the memory
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * stuff before the file handle is closed.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt Device extension.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession Session data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * This data will be freed by this routine.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicovoid VBOXCALL supdrvCleanupSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVBUNDLE pBundle;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("supdrvCleanupSession: pSession=%p\n", pSession));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Remove logger instances related to this session.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTLogSetDefaultInstanceThread(NULL, (uintptr_t)pSession);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Destroy the handle table.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTHandleTableDestroy(pSession->hHandleTable, supdrvSessionObjHandleDelete, pSession);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertRC(rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->hHandleTable = NIL_RTHANDLETABLE;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Release object references made in this session.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * In theory there should be noone racing us in this session.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log2(("release objects - start\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pSession->pUsage)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVUSAGE pUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockAcquire(pDevExt->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico while ((pUsage = pSession->pUsage) != NULL)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVOBJ pObj = pUsage->pObj;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->pUsage = pUsage->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsg(pUsage->cUsage >= 1 && pObj->cUsage >= pUsage->cUsage, ("glob %d; sess %d\n", pObj->cUsage, pUsage->cUsage));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pUsage->cUsage < pObj->cUsage)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj->cUsage -= pUsage->cUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Destroy the object and free the record. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pDevExt->pObjs == pObj)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pObjs = pObj->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVOBJ pObjPrev;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (pObjPrev = pDevExt->pObjs; pObjPrev; pObjPrev = pObjPrev->pNext)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pObjPrev->pNext == pObj)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObjPrev->pNext = pObj->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Assert(pObjPrev);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("supdrvCleanupSession: destroying %p/%d (%p/%p) cpid=%RTproc pid=%RTproc dtor=%p\n",
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj, pObj->enmType, pObj->pvUser1, pObj->pvUser2, pObj->CreatorProcess, RTProcSelf(), pObj->pfnDestructor));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pObj->pfnDestructor)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj->pfnDestructor(pObj, pObj->pvUser1, pObj->pvUser2);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMemFree(pObj);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* free it and continue. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMemFree(pUsage);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockAcquire(pDevExt->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsg(!pSession->pUsage, ("Some buster reregistered an object during desturction!\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log2(("release objects - done\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Release memory allocated in the session.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * We do not serialize this as we assume that the application will
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * not allocated memory while closing the file handle object.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log2(("freeing memory:\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pBundle = &pSession->Bundle;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico while (pBundle)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVBUNDLE pToFree;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico unsigned i;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Check and unlock all entries in the bundle.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (i = 0; i < RT_ELEMENTS(pBundle->aMem); i++)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pBundle->aMem[i].MemObj != NIL_RTR0MEMOBJ)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log2(("eType=%d pvR0=%p pvR3=%p cb=%ld\n", pBundle->aMem[i].eType, RTR0MemObjAddress(pBundle->aMem[i].MemObj),
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico (void *)RTR0MemObjAddressR3(pBundle->aMem[i].MapObjR3), (long)RTR0MemObjSize(pBundle->aMem[i].MemObj)));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pBundle->aMem[i].MapObjR3 != NIL_RTR0MEMOBJ)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTR0MemObjFree(pBundle->aMem[i].MapObjR3, false);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertRC(rc); /** @todo figure out how to handle this. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pBundle->aMem[i].MapObjR3 = NIL_RTR0MEMOBJ;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTR0MemObjFree(pBundle->aMem[i].MemObj, true /* fFreeMappings */);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertRC(rc); /** @todo figure out how to handle this. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pBundle->aMem[i].MemObj = NIL_RTR0MEMOBJ;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pBundle->aMem[i].eType = MEMREF_TYPE_UNUSED;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Advance and free previous bundle.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pToFree = pBundle;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pBundle = pBundle->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pToFree->pNext = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pToFree->cUsed = 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pToFree != &pSession->Bundle)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMemFree(pToFree);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log2(("freeing memory - done\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Deregister component factories.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSemFastMutexRequest(pDevExt->mtxComponentFactory);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log2(("deregistering component factories:\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pDevExt->pComponentFactoryHead)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVFACTORYREG pPrev = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVFACTORYREG pCur = pDevExt->pComponentFactoryHead;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico while (pCur)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pCur->pSession == pSession)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* unlink it */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVFACTORYREG pNext = pCur->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pPrev)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pPrev->pNext = pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pComponentFactoryHead = pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* free it */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pCur->pNext = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pCur->pSession = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pCur->pFactory = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMemFree(pCur);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* next */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pCur = pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* next */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pPrev = pCur;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pCur = pCur->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSemFastMutexRelease(pDevExt->mtxComponentFactory);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log2(("deregistering component factories - done\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Loaded images needs to be dereferenced and possibly freed up.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrLock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log2(("freeing images:\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pSession->pLdrUsage)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVLDRUSAGE pUsage = pSession->pLdrUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->pLdrUsage = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico while (pUsage)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico void *pvFree = pUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVLDRIMAGE pImage = pUsage->pImage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pImage->cUsage > pUsage->cUsage)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->cUsage -= pUsage->cUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrFree(pDevExt, pImage);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage->pImage = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage = pUsage->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMemFree(pvFree);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrUnlock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log2(("freeing images - done\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Unmap the GIP.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log2(("umapping GIP:\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pSession->GipMapObjR3 != NIL_RTR0MEMOBJ)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico SUPR0GipUnmap(pSession);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->fGipReferenced = 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log2(("umapping GIP - done\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * RTHandleTableDestroy callback used by supdrvCleanupSession.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code, see SUPR0ObjAddRef.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param hHandleTable The handle table handle. Ignored.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvObj The object pointer.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvCtx Context, the handle type. Ignored.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvUser Session pointer.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic DECLCALLBACK(int) supdrvSessionObjHandleRetain(RTHANDLETABLE hHandleTable, void *pvObj, void *pvCtx, void *pvUser)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico NOREF(pvCtx);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico NOREF(hHandleTable);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return SUPR0ObjAddRefEx(pvObj, (PSUPDRVSESSION)pvUser, true /*fNoBlocking*/);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * RTHandleTableDestroy callback used by supdrvCleanupSession.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param hHandleTable The handle table handle. Ignored.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param h The handle value. Ignored.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvObj The object pointer.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvCtx Context, the handle type. Ignored.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvUser Session pointer.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic DECLCALLBACK(void) supdrvSessionObjHandleDelete(RTHANDLETABLE hHandleTable, uint32_t h, void *pvObj, void *pvCtx, void *pvUser)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico NOREF(pvCtx);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico NOREF(h);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico NOREF(hHandleTable);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico SUPR0ObjRelease(pvObj, (PSUPDRVSESSION)pvUser);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Fast path I/O Control worker.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns VBox status code that should be passed down to ring-3 unchanged.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param uIOCtl Function number.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param idCpu VMCPU id.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt Device extention.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession Session data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoint VBOXCALL supdrvIOCtlFast(uintptr_t uIOCtl, VMCPUID idCpu, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * We check the two prereqs after doing this only to allow the compiler to optimize things better.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_LIKELY(pSession->pVM && pDevExt->pfnVMMR0EntryFast))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico switch (uIOCtl)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_IOCTL_FAST_DO_RAW_RUN:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pfnVMMR0EntryFast(pSession->pVM, idCpu, SUP_VMMR0_DO_RAW_RUN);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_IOCTL_FAST_DO_HWACC_RUN:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pfnVMMR0EntryFast(pSession->pVM, idCpu, SUP_VMMR0_DO_HWACC_RUN);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_IOCTL_FAST_DO_NOP:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pfnVMMR0EntryFast(pSession->pVM, idCpu, SUP_VMMR0_DO_NOP);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico default:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_INTERNAL_ERROR;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_INTERNAL_ERROR;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Helper for supdrvIOCtl. Check if pszStr contains any character of pszChars.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * We would use strpbrk here if this function would be contained in the RedHat kABI white
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * list, see http://www.kerneldrivers.org/RHEL5.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns 1 if pszStr does contain any character of pszChars, 0 otherwise.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pszStr String to check
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pszChars Character set
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvCheckInvalidChar(const char *pszStr, const char *pszChars)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int chCur;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico while ((chCur = *pszStr++) != '\0')
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int ch;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico const char *psz = pszChars;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico while ((ch = *psz++) != '\0')
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (ch == chCur)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 1;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * I/O Control worker.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VERR_INVALID_PARAMETER if the request is invalid.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param uIOCtl Function number.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt Device extention.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession Session data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pReqHdr The request header.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoint VBOXCALL supdrvIOCtl(uintptr_t uIOCtl, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPREQHDR pReqHdr)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_ENTRY(pSession, uIOCtl, pReqHdr);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Validate the request.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* this first check could probably be omitted as its also done by the OS specific code... */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_UNLIKELY( (pReqHdr->fFlags & SUPREQHDR_FLAGS_MAGIC_MASK) != SUPREQHDR_FLAGS_MAGIC
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || pReqHdr->cbIn < sizeof(*pReqHdr)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || pReqHdr->cbOut < sizeof(*pReqHdr)))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(("vboxdrv: Bad ioctl request header; cbIn=%#lx cbOut=%#lx fFlags=%#lx\n",
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico (long)pReqHdr->cbIn, (long)pReqHdr->cbOut, (long)pReqHdr->fFlags));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VERR_INVALID_PARAMETER, VINF_SUCCESS);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_UNLIKELY(uIOCtl == SUP_IOCTL_COOKIE))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pReqHdr->u32Cookie != SUPCOOKIE_INITIAL_COOKIE)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(("SUP_IOCTL_COOKIE: bad cookie %#lx\n", (long)pReqHdr->u32Cookie));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VERR_INVALID_PARAMETER, VINF_SUCCESS);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else if (RT_UNLIKELY( pReqHdr->u32Cookie != pDevExt->u32Cookie
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || pReqHdr->u32SessionCookie != pSession->u32Cookie))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(("vboxdrv: bad cookie %#lx / %#lx.\n", (long)pReqHdr->u32Cookie, (long)pReqHdr->u32SessionCookie));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VERR_INVALID_PARAMETER, VINF_SUCCESS);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Validation macros
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#define REQ_CHECK_SIZES_EX(Name, cbInExpect, cbOutExpect) \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico do { \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_UNLIKELY(pReqHdr->cbIn != (cbInExpect) || pReqHdr->cbOut != (cbOutExpect))) \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(( #Name ": Invalid input/output sizes. cbIn=%ld expected %ld. cbOut=%ld expected %ld.\n", \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico (long)pReq->Hdr.cbIn, (long)(cbInExpect), (long)pReq->Hdr.cbOut, (long)(cbOutExpect))); \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VERR_INVALID_PARAMETER, VERR_INVALID_PARAMETER); \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return pReq->Hdr.rc = VERR_INVALID_PARAMETER; \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico } \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico } while (0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#define REQ_CHECK_SIZES(Name) REQ_CHECK_SIZES_EX(Name, Name ## _SIZE_IN, Name ## _SIZE_OUT)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#define REQ_CHECK_SIZE_IN(Name, cbInExpect) \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico do { \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_UNLIKELY(pReqHdr->cbIn != (cbInExpect))) \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(( #Name ": Invalid input/output sizes. cbIn=%ld expected %ld.\n", \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico (long)pReq->Hdr.cbIn, (long)(cbInExpect))); \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VERR_INVALID_PARAMETER, VERR_INVALID_PARAMETER); \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return pReq->Hdr.rc = VERR_INVALID_PARAMETER; \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico } \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico } while (0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#define REQ_CHECK_SIZE_OUT(Name, cbOutExpect) \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico do { \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_UNLIKELY(pReqHdr->cbOut != (cbOutExpect))) \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(( #Name ": Invalid input/output sizes. cbOut=%ld expected %ld.\n", \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico (long)pReq->Hdr.cbOut, (long)(cbOutExpect))); \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VERR_INVALID_PARAMETER, VERR_INVALID_PARAMETER); \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return pReq->Hdr.rc = VERR_INVALID_PARAMETER; \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico } \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico } while (0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#define REQ_CHECK_EXPR(Name, expr) \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico do { \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_UNLIKELY(!(expr))) \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(( #Name ": %s\n", #expr)); \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VERR_INVALID_PARAMETER, VERR_INVALID_PARAMETER); \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return pReq->Hdr.rc = VERR_INVALID_PARAMETER; \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico } \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico } while (0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#define REQ_CHECK_EXPR_FMT(expr, fmt) \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico do { \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_UNLIKELY(!(expr))) \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT( fmt ); \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VERR_INVALID_PARAMETER, VERR_INVALID_PARAMETER); \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return pReq->Hdr.rc = VERR_INVALID_PARAMETER; \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico } \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico } while (0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * The switch.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico switch (SUP_CTL_CODE_NO_SIZE(uIOCtl))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_COOKIE):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPCOOKIE pReq = (PSUPCOOKIE)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES(SUP_IOCTL_COOKIE);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (strncmp(pReq->u.In.szMagic, SUPCOOKIE_MAGIC, sizeof(pReq->u.In.szMagic)))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(("SUP_IOCTL_COOKIE: invalid magic %.16s\n", pReq->u.In.szMagic));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = VERR_INVALID_MAGIC;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, VERR_INVALID_MAGIC);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#if 0
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Call out to the OS specific code and let it do permission checks on the
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * client process.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!supdrvOSValidateClientProcess(pDevExt, pSession))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.u32Cookie = 0xffffffff;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.u32SessionCookie = 0xffffffff;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.u32SessionVersion = 0xffffffff;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.u32DriverVersion = SUPDRV_IOC_VERSION;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.pSession = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.cFunctions = 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = VERR_PERMISSION_DENIED;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, VERR_PERMISSION_DENIED);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Match the version.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * The current logic is very simple, match the major interface version.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( pReq->u.In.u32MinVersion > SUPDRV_IOC_VERSION
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || (pReq->u.In.u32MinVersion & 0xffff0000) != (SUPDRV_IOC_VERSION & 0xffff0000))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(("SUP_IOCTL_COOKIE: Version mismatch. Requested: %#x Min: %#x Current: %#x\n",
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.In.u32ReqVersion, pReq->u.In.u32MinVersion, SUPDRV_IOC_VERSION));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.u32Cookie = 0xffffffff;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.u32SessionCookie = 0xffffffff;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.u32SessionVersion = 0xffffffff;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.u32DriverVersion = SUPDRV_IOC_VERSION;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.pSession = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.cFunctions = 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = VERR_VERSION_MISMATCH;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Fill in return data and be gone.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * N.B. The first one to change SUPDRV_IOC_VERSION shall makes sure that
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * u32SessionVersion <= u32ReqVersion!
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /** @todo Somehow validate the client and negotiate a secure cookie... */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.u32Cookie = pDevExt->u32Cookie;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.u32SessionCookie = pSession->u32Cookie;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.u32SessionVersion = SUPDRV_IOC_VERSION;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.u32DriverVersion = SUPDRV_IOC_VERSION;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.pSession = pSession;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.cFunctions = sizeof(g_aFunctions) / sizeof(g_aFunctions[0]);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_QUERY_FUNCS(0)):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPQUERYFUNCS pReq = (PSUPQUERYFUNCS)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES_EX(SUP_IOCTL_QUERY_FUNCS, SUP_IOCTL_QUERY_FUNCS_SIZE_IN, SUP_IOCTL_QUERY_FUNCS_SIZE_OUT(RT_ELEMENTS(g_aFunctions)));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.cFunctions = RT_ELEMENTS(g_aFunctions);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico memcpy(&pReq->u.Out.aFunctions[0], g_aFunctions, sizeof(g_aFunctions));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_PAGE_LOCK):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPPAGELOCK pReq = (PSUPPAGELOCK)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZE_IN(SUP_IOCTL_PAGE_LOCK, SUP_IOCTL_PAGE_LOCK_SIZE_IN);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZE_OUT(SUP_IOCTL_PAGE_LOCK, SUP_IOCTL_PAGE_LOCK_SIZE_OUT(pReq->u.In.cPages));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_PAGE_LOCK, pReq->u.In.cPages > 0);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_PAGE_LOCK, pReq->u.In.pvR3 >= PAGE_SIZE);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPR0LockMem(pSession, pReq->u.In.pvR3, pReq->u.In.cPages, &pReq->u.Out.aPages[0]);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_FAILURE(pReq->Hdr.rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.cbOut = sizeof(pReq->Hdr);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_PAGE_UNLOCK):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPPAGEUNLOCK pReq = (PSUPPAGEUNLOCK)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES(SUP_IOCTL_PAGE_UNLOCK);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPR0UnlockMem(pSession, pReq->u.In.pvR3);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_CONT_ALLOC):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPCONTALLOC pReq = (PSUPCONTALLOC)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES(SUP_IOCTL_CONT_ALLOC);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPR0ContAlloc(pSession, pReq->u.In.cPages, &pReq->u.Out.pvR0, &pReq->u.Out.pvR3, &pReq->u.Out.HCPhys);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_FAILURE(pReq->Hdr.rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.cbOut = sizeof(pReq->Hdr);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_CONT_FREE):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPCONTFREE pReq = (PSUPCONTFREE)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES(SUP_IOCTL_CONT_FREE);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPR0ContFree(pSession, (RTHCUINTPTR)pReq->u.In.pvR3);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_LDR_OPEN):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPLDROPEN pReq = (PSUPLDROPEN)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES(SUP_IOCTL_LDR_OPEN);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_LDR_OPEN, pReq->u.In.cbImageWithTabs > 0);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_LDR_OPEN, pReq->u.In.cbImageWithTabs < 16*_1M);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_LDR_OPEN, pReq->u.In.cbImageBits > 0);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_LDR_OPEN, pReq->u.In.cbImageBits > 0);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_LDR_OPEN, pReq->u.In.cbImageBits < pReq->u.In.cbImageWithTabs);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_LDR_OPEN, pReq->u.In.szName[0]);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_LDR_OPEN, RTStrEnd(pReq->u.In.szName, sizeof(pReq->u.In.szName)));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_LDR_OPEN, !supdrvCheckInvalidChar(pReq->u.In.szName, ";:()[]{}/\\|&*%#@!~`\"'"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_LDR_OPEN, RTStrEnd(pReq->u.In.szFilename, sizeof(pReq->u.In.szFilename)));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = supdrvIOCtl_LdrOpen(pDevExt, pSession, pReq);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_LDR_LOAD):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPLDRLOAD pReq = (PSUPLDRLOAD)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(Name, pReq->Hdr.cbIn >= sizeof(*pReq));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES_EX(SUP_IOCTL_LDR_LOAD, SUP_IOCTL_LDR_LOAD_SIZE_IN(pReq->u.In.cbImageWithTabs), SUP_IOCTL_LDR_LOAD_SIZE_OUT);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_LDR_LOAD, pReq->u.In.cSymbols <= 16384);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR_FMT( !pReq->u.In.cSymbols
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || ( pReq->u.In.offSymbols < pReq->u.In.cbImageWithTabs
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && pReq->u.In.offSymbols + pReq->u.In.cSymbols * sizeof(SUPLDRSYM) <= pReq->u.In.cbImageWithTabs),
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ("SUP_IOCTL_LDR_LOAD: offSymbols=%#lx cSymbols=%#lx cbImageWithTabs=%#lx\n", (long)pReq->u.In.offSymbols,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico (long)pReq->u.In.cSymbols, (long)pReq->u.In.cbImageWithTabs));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR_FMT( !pReq->u.In.cbStrTab
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || ( pReq->u.In.offStrTab < pReq->u.In.cbImageWithTabs
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && pReq->u.In.offStrTab + pReq->u.In.cbStrTab <= pReq->u.In.cbImageWithTabs
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && pReq->u.In.cbStrTab <= pReq->u.In.cbImageWithTabs),
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ("SUP_IOCTL_LDR_LOAD: offStrTab=%#lx cbStrTab=%#lx cbImageWithTabs=%#lx\n", (long)pReq->u.In.offStrTab,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico (long)pReq->u.In.cbStrTab, (long)pReq->u.In.cbImageWithTabs));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pReq->u.In.cSymbols)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint32_t i;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPLDRSYM paSyms = (PSUPLDRSYM)&pReq->u.In.abImage[pReq->u.In.offSymbols];
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (i = 0; i < pReq->u.In.cSymbols; i++)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR_FMT(paSyms[i].offSymbol < pReq->u.In.cbImageWithTabs,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ("SUP_IOCTL_LDR_LOAD: sym #%ld: symb off %#lx (max=%#lx)\n", (long)i, (long)paSyms[i].offSymbol, (long)pReq->u.In.cbImageWithTabs));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR_FMT(paSyms[i].offName < pReq->u.In.cbStrTab,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ("SUP_IOCTL_LDR_LOAD: sym #%ld: name off %#lx (max=%#lx)\n", (long)i, (long)paSyms[i].offName, (long)pReq->u.In.cbImageWithTabs));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR_FMT(RTStrEnd((char const *)&pReq->u.In.abImage[pReq->u.In.offStrTab + paSyms[i].offName],
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.In.cbStrTab - paSyms[i].offName),
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ("SUP_IOCTL_LDR_LOAD: sym #%ld: unterminated name! (%#lx / %#lx)\n", (long)i, (long)paSyms[i].offName, (long)pReq->u.In.cbImageWithTabs));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = supdrvIOCtl_LdrLoad(pDevExt, pSession, pReq);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_LDR_FREE):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPLDRFREE pReq = (PSUPLDRFREE)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES(SUP_IOCTL_LDR_FREE);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = supdrvIOCtl_LdrFree(pDevExt, pSession, pReq);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_LDR_GET_SYMBOL):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPLDRGETSYMBOL pReq = (PSUPLDRGETSYMBOL)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES(SUP_IOCTL_LDR_GET_SYMBOL);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_LDR_GET_SYMBOL, RTStrEnd(pReq->u.In.szSymbol, sizeof(pReq->u.In.szSymbol)));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = supdrvIOCtl_LdrGetSymbol(pDevExt, pSession, pReq);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_CALL_VMMR0(0)):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPCALLVMMR0 pReq = (PSUPCALLVMMR0)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log4(("SUP_IOCTL_CALL_VMMR0: op=%u in=%u arg=%RX64 p/t=%RTproc/%RTthrd\n",
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.In.uOperation, pReq->Hdr.cbIn, pReq->u.In.u64Arg, RTProcSelf(), RTThreadNativeSelf()));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pReq->Hdr.cbIn == SUP_IOCTL_CALL_VMMR0_SIZE(0))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES_EX(SUP_IOCTL_CALL_VMMR0, SUP_IOCTL_CALL_VMMR0_SIZE_IN(0), SUP_IOCTL_CALL_VMMR0_SIZE_OUT(0));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_LIKELY(pDevExt->pfnVMMR0EntryEx))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = pDevExt->pfnVMMR0EntryEx(pReq->u.In.pVMR0, pReq->u.In.idCpu, pReq->u.In.uOperation, NULL, pReq->u.In.u64Arg, pSession);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = VERR_WRONG_ORDER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPVMMR0REQHDR pVMMReq = (PSUPVMMR0REQHDR)&pReq->abReqPkt[0];
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR_FMT(pReq->Hdr.cbIn >= SUP_IOCTL_CALL_VMMR0_SIZE(sizeof(SUPVMMR0REQHDR)),
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ("SUP_IOCTL_CALL_VMMR0: cbIn=%#x < %#lx\n", pReq->Hdr.cbIn, SUP_IOCTL_CALL_VMMR0_SIZE(sizeof(SUPVMMR0REQHDR))));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_CALL_VMMR0, pVMMReq->u32Magic == SUPVMMR0REQHDR_MAGIC);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES_EX(SUP_IOCTL_CALL_VMMR0, SUP_IOCTL_CALL_VMMR0_SIZE_IN(pVMMReq->cbReq), SUP_IOCTL_CALL_VMMR0_SIZE_OUT(pVMMReq->cbReq));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_LIKELY(pDevExt->pfnVMMR0EntryEx))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = pDevExt->pfnVMMR0EntryEx(pReq->u.In.pVMR0, pReq->u.In.idCpu, pReq->u.In.uOperation, pVMMReq, pReq->u.In.u64Arg, pSession);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = VERR_WRONG_ORDER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( RT_FAILURE(pReq->Hdr.rc)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && pReq->Hdr.rc != VERR_INTERRUPTED
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && pReq->Hdr.rc != VERR_TIMEOUT)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("SUP_IOCTL_CALL_VMMR0: rc=%Rrc op=%u out=%u arg=%RX64 p/t=%RTproc/%RTthrd\n",
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc, pReq->u.In.uOperation, pReq->Hdr.cbOut, pReq->u.In.u64Arg, RTProcSelf(), RTThreadNativeSelf()));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log4(("SUP_IOCTL_CALL_VMMR0: rc=%Rrc op=%u out=%u arg=%RX64 p/t=%RTproc/%RTthrd\n",
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc, pReq->u.In.uOperation, pReq->Hdr.cbOut, pReq->u.In.u64Arg, RTProcSelf(), RTThreadNativeSelf()));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_GET_PAGING_MODE):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPGETPAGINGMODE pReq = (PSUPGETPAGINGMODE)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES(SUP_IOCTL_GET_PAGING_MODE);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.enmMode = SUPR0GetPagingMode();
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_LOW_ALLOC):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPLOWALLOC pReq = (PSUPLOWALLOC)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_LOW_ALLOC, pReq->Hdr.cbIn <= SUP_IOCTL_LOW_ALLOC_SIZE_IN);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES_EX(SUP_IOCTL_LOW_ALLOC, SUP_IOCTL_LOW_ALLOC_SIZE_IN, SUP_IOCTL_LOW_ALLOC_SIZE_OUT(pReq->u.In.cPages));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPR0LowAlloc(pSession, pReq->u.In.cPages, &pReq->u.Out.pvR0, &pReq->u.Out.pvR3, &pReq->u.Out.aPages[0]);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_FAILURE(pReq->Hdr.rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.cbOut = sizeof(pReq->Hdr);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_LOW_FREE):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPLOWFREE pReq = (PSUPLOWFREE)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES(SUP_IOCTL_LOW_FREE);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPR0LowFree(pSession, (RTHCUINTPTR)pReq->u.In.pvR3);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_GIP_MAP):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPGIPMAP pReq = (PSUPGIPMAP)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES(SUP_IOCTL_GIP_MAP);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPR0GipMap(pSession, &pReq->u.Out.pGipR3, &pReq->u.Out.HCPhysGip);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(pReq->Hdr.rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.pGipR0 = pDevExt->pGip;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_GIP_UNMAP):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPGIPUNMAP pReq = (PSUPGIPUNMAP)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES(SUP_IOCTL_GIP_UNMAP);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPR0GipUnmap(pSession);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_SET_VM_FOR_FAST):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPSETVMFORFAST pReq = (PSUPSETVMFORFAST)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES(SUP_IOCTL_SET_VM_FOR_FAST);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR_FMT( !pReq->u.In.pVMR0
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || ( VALID_PTR(pReq->u.In.pVMR0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && !((uintptr_t)pReq->u.In.pVMR0 & (PAGE_SIZE - 1))),
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ("SUP_IOCTL_SET_VM_FOR_FAST: pVMR0=%p!\n", pReq->u.In.pVMR0));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->pVM = pReq->u.In.pVMR0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_PAGE_ALLOC_EX):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPPAGEALLOCEX pReq = (PSUPPAGEALLOCEX)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_PAGE_ALLOC_EX, pReq->Hdr.cbIn <= SUP_IOCTL_PAGE_ALLOC_EX_SIZE_IN);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico 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));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR_FMT(pReq->u.In.fKernelMapping || pReq->u.In.fUserMapping,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ("SUP_IOCTL_PAGE_ALLOC_EX: No mapping requested!\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR_FMT(pReq->u.In.fUserMapping,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ("SUP_IOCTL_PAGE_ALLOC_EX: Must have user mapping!\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR_FMT(!pReq->u.In.fReserved0 && !pReq->u.In.fReserved1,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ("SUP_IOCTL_PAGE_ALLOC_EX: fReserved0=%d fReserved1=%d\n", pReq->u.In.fReserved0, pReq->u.In.fReserved1));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPR0PageAllocEx(pSession, pReq->u.In.cPages, 0 /* fFlags */,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.In.fUserMapping ? &pReq->u.Out.pvR3 : NULL,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.In.fKernelMapping ? &pReq->u.Out.pvR0 : NULL,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico &pReq->u.Out.aPages[0]);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_FAILURE(pReq->Hdr.rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.cbOut = sizeof(pReq->Hdr);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_PAGE_MAP_KERNEL):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPPAGEMAPKERNEL pReq = (PSUPPAGEMAPKERNEL)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES(SUP_IOCTL_PAGE_MAP_KERNEL);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR_FMT(!pReq->u.In.fFlags, ("SUP_IOCTL_PAGE_MAP_KERNEL: fFlags=%#x! MBZ\n", pReq->u.In.fFlags));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR_FMT(!(pReq->u.In.offSub & PAGE_OFFSET_MASK), ("SUP_IOCTL_PAGE_MAP_KERNEL: offSub=%#x\n", pReq->u.In.offSub));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR_FMT(pReq->u.In.cbSub && !(pReq->u.In.cbSub & PAGE_OFFSET_MASK),
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ("SUP_IOCTL_PAGE_MAP_KERNEL: cbSub=%#x\n", pReq->u.In.cbSub));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPR0PageMapKernel(pSession, pReq->u.In.pvR3, pReq->u.In.offSub, pReq->u.In.cbSub,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.In.fFlags, &pReq->u.Out.pvR0);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_FAILURE(pReq->Hdr.rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.cbOut = sizeof(pReq->Hdr);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_PAGE_PROTECT):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPPAGEPROTECT pReq = (PSUPPAGEPROTECT)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES(SUP_IOCTL_PAGE_PROTECT);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR_FMT(!(pReq->u.In.fProt & ~(RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC | RTMEM_PROT_NONE)),
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ("SUP_IOCTL_PAGE_PROTECT: fProt=%#x!\n", pReq->u.In.fProt));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR_FMT(!(pReq->u.In.offSub & PAGE_OFFSET_MASK), ("SUP_IOCTL_PAGE_PROTECT: offSub=%#x\n", pReq->u.In.offSub));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR_FMT(pReq->u.In.cbSub && !(pReq->u.In.cbSub & PAGE_OFFSET_MASK),
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ("SUP_IOCTL_PAGE_PROTECT: cbSub=%#x\n", pReq->u.In.cbSub));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico 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);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_PAGE_FREE):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPPAGEFREE pReq = (PSUPPAGEFREE)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES(SUP_IOCTL_PAGE_FREE);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPR0PageFree(pSession, pReq->u.In.pvR3);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_CALL_SERVICE(0)):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPCALLSERVICE pReq = (PSUPCALLSERVICE)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log4(("SUP_IOCTL_CALL_SERVICE: op=%u in=%u arg=%RX64 p/t=%RTproc/%RTthrd\n",
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.In.uOperation, pReq->Hdr.cbIn, pReq->u.In.u64Arg, RTProcSelf(), RTThreadNativeSelf()));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pReq->Hdr.cbIn == SUP_IOCTL_CALL_SERVICE_SIZE(0))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES_EX(SUP_IOCTL_CALL_SERVICE, SUP_IOCTL_CALL_SERVICE_SIZE_IN(0), SUP_IOCTL_CALL_SERVICE_SIZE_OUT(0));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPR0SERVICEREQHDR pSrvReq = (PSUPR0SERVICEREQHDR)&pReq->abReqPkt[0];
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR_FMT(pReq->Hdr.cbIn >= SUP_IOCTL_CALL_SERVICE_SIZE(sizeof(SUPR0SERVICEREQHDR)),
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ("SUP_IOCTL_CALL_SERVICE: cbIn=%#x < %#lx\n", pReq->Hdr.cbIn, SUP_IOCTL_CALL_SERVICE_SIZE(sizeof(SUPR0SERVICEREQHDR))));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_CALL_SERVICE, pSrvReq->u32Magic == SUPR0SERVICEREQHDR_MAGIC);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES_EX(SUP_IOCTL_CALL_SERVICE, SUP_IOCTL_CALL_SERVICE_SIZE_IN(pSrvReq->cbReq), SUP_IOCTL_CALL_SERVICE_SIZE_OUT(pSrvReq->cbReq));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_CALL_SERVICE, RTStrEnd(pReq->u.In.szName, sizeof(pReq->u.In.szName)));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = supdrvIOCtl_CallServiceModule(pDevExt, pSession, pReq);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_LOGGER_SETTINGS(0)):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPLOGGERSETTINGS pReq = (PSUPLOGGERSETTINGS)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico size_t cbStrTab;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZE_OUT(SUP_IOCTL_LOGGER_SETTINGS, SUP_IOCTL_LOGGER_SETTINGS_SIZE_OUT);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_LOGGER_SETTINGS, pReq->Hdr.cbIn >= SUP_IOCTL_LOGGER_SETTINGS_SIZE_IN(1));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico cbStrTab = pReq->Hdr.cbIn - SUP_IOCTL_LOGGER_SETTINGS_SIZE_IN(0);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_LOGGER_SETTINGS, pReq->u.In.offGroups < cbStrTab);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_LOGGER_SETTINGS, pReq->u.In.offFlags < cbStrTab);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_LOGGER_SETTINGS, pReq->u.In.offDestination < cbStrTab);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR_FMT(pReq->u.In.szStrings[cbStrTab - 1] == '\0',
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ("SUP_IOCTL_LOGGER_SETTINGS: cbIn=%#x cbStrTab=%#zx LastChar=%d\n",
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.cbIn, cbStrTab, pReq->u.In.szStrings[cbStrTab - 1]));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_LOGGER_SETTINGS, pReq->u.In.fWhich <= SUPLOGGERSETTINGS_WHICH_RELEASE);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_LOGGER_SETTINGS, pReq->u.In.fWhat <= SUPLOGGERSETTINGS_WHAT_DESTROY);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = supdrvIOCtl_LoggerSettings(pDevExt, pSession, pReq);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_SEM_CREATE):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPSEMCREATE pReq = (PSUPSEMCREATE)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES_EX(SUP_IOCTL_SEM_CREATE, SUP_IOCTL_SEM_CREATE_SIZE_IN, SUP_IOCTL_SEM_CREATE_SIZE_OUT);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico switch (pReq->u.In.uType)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_SEM_TYPE_EVENT:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico SUPSEMEVENT hEvent;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPSemEventCreate(pSession, &hEvent);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.hSem = (uint32_t)(uintptr_t)hEvent;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_SEM_TYPE_EVENT_MULTI:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico SUPSEMEVENTMULTI hEventMulti;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPSemEventMultiCreate(pSession, &hEventMulti);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.hSem = (uint32_t)(uintptr_t)hEventMulti;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico default:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_SEM_OP):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPSEMOP pReq = (PSUPSEMOP)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES_EX(SUP_IOCTL_SEM_OP, SUP_IOCTL_SEM_OP_SIZE_IN, SUP_IOCTL_SEM_OP_SIZE_OUT);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico switch (pReq->u.In.uType)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_SEM_TYPE_EVENT:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico SUPSEMEVENT hEvent = (SUPSEMEVENT)(uintptr_t)pReq->u.In.hSem;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico switch (pReq->u.In.uOp)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP_WAIT:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPSemEventWaitNoResume(pSession, hEvent, pReq->u.In.cMillies);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP_SIGNAL:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPSemEventSignal(pSession, hEvent);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP_CLOSE:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPSemEventClose(pSession, hEvent);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP_RESET:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico default:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = VERR_INVALID_FUNCTION;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_SEM_TYPE_EVENT_MULTI:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico SUPSEMEVENTMULTI hEventMulti = (SUPSEMEVENTMULTI)(uintptr_t)pReq->u.In.hSem;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico switch (pReq->u.In.uOp)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP_WAIT:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPSemEventMultiWaitNoResume(pSession, hEventMulti, pReq->u.In.cMillies);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP_SIGNAL:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPSemEventMultiSignal(pSession, hEventMulti);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP_CLOSE:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPSemEventMultiClose(pSession, hEventMulti);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP_RESET:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPSemEventMultiReset(pSession, hEventMulti);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico default:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = VERR_INVALID_FUNCTION;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico default:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_SEM_OP2):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPSEMOP2 pReq = (PSUPSEMOP2)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES_EX(SUP_IOCTL_SEM_OP2, SUP_IOCTL_SEM_OP2_SIZE_IN, SUP_IOCTL_SEM_OP2_SIZE_OUT);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_SEM_OP2, pReq->u.In.uReserved == 0);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico switch (pReq->u.In.uType)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_SEM_TYPE_EVENT:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico SUPSEMEVENT hEvent = (SUPSEMEVENT)(uintptr_t)pReq->u.In.hSem;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico switch (pReq->u.In.uOp)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP2_WAIT_MS_REL:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPSemEventWaitNoResume(pSession, hEvent, pReq->u.In.uArg.cRelMsTimeout);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP2_WAIT_NS_ABS:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPSemEventWaitNsAbsIntr(pSession, hEvent, pReq->u.In.uArg.uAbsNsTimeout);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP2_WAIT_NS_REL:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPSemEventWaitNsRelIntr(pSession, hEvent, pReq->u.In.uArg.cRelNsTimeout);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP2_SIGNAL:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPSemEventSignal(pSession, hEvent);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP2_CLOSE:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPSemEventClose(pSession, hEvent);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP2_RESET:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico default:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = VERR_INVALID_FUNCTION;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_SEM_TYPE_EVENT_MULTI:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico SUPSEMEVENTMULTI hEventMulti = (SUPSEMEVENTMULTI)(uintptr_t)pReq->u.In.hSem;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico switch (pReq->u.In.uOp)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP2_WAIT_MS_REL:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPSemEventMultiWaitNoResume(pSession, hEventMulti, pReq->u.In.uArg.cRelMsTimeout);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP2_WAIT_NS_ABS:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPSemEventMultiWaitNsAbsIntr(pSession, hEventMulti, pReq->u.In.uArg.uAbsNsTimeout);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP2_WAIT_NS_REL:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPSemEventMultiWaitNsRelIntr(pSession, hEventMulti, pReq->u.In.uArg.cRelNsTimeout);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP2_SIGNAL:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPSemEventMultiSignal(pSession, hEventMulti);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP2_CLOSE:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPSemEventMultiClose(pSession, hEventMulti);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP2_RESET:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPSemEventMultiReset(pSession, hEventMulti);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico default:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = VERR_INVALID_FUNCTION;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico default:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_SEM_OP3):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPSEMOP3 pReq = (PSUPSEMOP3)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES_EX(SUP_IOCTL_SEM_OP3, SUP_IOCTL_SEM_OP3_SIZE_IN, SUP_IOCTL_SEM_OP3_SIZE_OUT);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_SEM_OP3, pReq->u.In.u32Reserved == 0 && pReq->u.In.u64Reserved == 0);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico switch (pReq->u.In.uType)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_SEM_TYPE_EVENT:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico SUPSEMEVENT hEvent = (SUPSEMEVENT)(uintptr_t)pReq->u.In.hSem;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico switch (pReq->u.In.uOp)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP3_CREATE:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_SEM_OP3, hEvent == NIL_SUPSEMEVENT);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPSemEventCreate(pSession, &hEvent);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.hSem = (uint32_t)(uintptr_t)hEvent;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP3_GET_RESOLUTION:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_SEM_OP3, hEvent == NIL_SUPSEMEVENT);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.cbOut = sizeof(*pReq);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.cNsResolution = SUPSemEventGetResolution(pSession);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico default:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = VERR_INVALID_FUNCTION;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_SEM_TYPE_EVENT_MULTI:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico SUPSEMEVENTMULTI hEventMulti = (SUPSEMEVENTMULTI)(uintptr_t)pReq->u.In.hSem;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico switch (pReq->u.In.uOp)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP3_CREATE:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_SEM_OP3, hEventMulti == NIL_SUPSEMEVENTMULTI);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPSemEventMultiCreate(pSession, &hEventMulti);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.hSem = (uint32_t)(uintptr_t)hEventMulti;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPSEMOP3_GET_RESOLUTION:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_SEM_OP3, hEventMulti == NIL_SUPSEMEVENTMULTI);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.cNsResolution = SUPSemEventMultiGetResolution(pSession);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico default:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = VERR_INVALID_FUNCTION;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico default:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_VT_CAPS):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* validate */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPVTCAPS pReq = (PSUPVTCAPS)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_SIZES(SUP_IOCTL_VT_CAPS);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_EXPR(SUP_IOCTL_VT_CAPS, pReq->Hdr.cbIn <= SUP_IOCTL_VT_CAPS_SIZE_IN);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* execute */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPR0QueryVTCaps(pSession, &pReq->u.Out.Caps);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_FAILURE(pReq->Hdr.rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.cbOut = sizeof(pReq->Hdr);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VINF_SUCCESS, pReq->Hdr.rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico default:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("Unknown IOCTL %#lx\n", (long)uIOCtl));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VBOXDRV_SUPDRV_IOCTL_RETURN(pSession, uIOCtl, pReqHdr, VERR_GENERAL_FAILURE, VERR_GENERAL_FAILURE);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_GENERAL_FAILURE;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Inter-Driver Communcation (IDC) worker.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns VBox status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VINF_SUCCESS on success.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VERR_INVALID_PARAMETER if the request is invalid.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VERR_NOT_SUPPORTED if the request isn't supported.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param uReq The request (function) code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt Device extention.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession Session data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pReqHdr The request header.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoint VBOXCALL supdrvIDC(uintptr_t uReq, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVIDCREQHDR pReqHdr)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * The OS specific code has already validated the pSession
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * pointer, and the request size being greater or equal to
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * size of the header.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * So, just check that pSession is a kernel context session.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_UNLIKELY( pSession
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && pSession->R0Process != NIL_RTR0PROCESS))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Validation macro.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#define REQ_CHECK_IDC_SIZE(Name, cbExpect) \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico do { \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_UNLIKELY(pReqHdr->cb != (cbExpect))) \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico { \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(( #Name ": Invalid input/output sizes. cb=%ld expected %ld.\n", \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico (long)pReqHdr->cb, (long)(cbExpect))); \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return pReqHdr->rc = VERR_INVALID_PARAMETER; \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico } \
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico } while (0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico switch (uReq)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPDRV_IDC_REQ_CONNECT:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVIDCREQCONNECT pReq = (PSUPDRVIDCREQCONNECT)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_IDC_SIZE(SUPDRV_IDC_REQ_CONNECT, sizeof(*pReq));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Validate the cookie and other input.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pReq->Hdr.pSession != NULL)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(("SUPDRV_IDC_REQ_CONNECT: pSession=%p expected NULL!\n", pReq->Hdr.pSession));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return pReqHdr->rc = VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pReq->u.In.u32MagicCookie != SUPDRVIDCREQ_CONNECT_MAGIC_COOKIE)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(("SUPDRV_IDC_REQ_CONNECT: u32MagicCookie=%#x expected %#x!\n",
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico (unsigned)pReq->u.In.u32MagicCookie, (unsigned)SUPDRVIDCREQ_CONNECT_MAGIC_COOKIE));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return pReqHdr->rc = VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( pReq->u.In.uMinVersion > pReq->u.In.uReqVersion
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || (pReq->u.In.uMinVersion & UINT32_C(0xffff0000)) != (pReq->u.In.uReqVersion & UINT32_C(0xffff0000)))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(("SUPDRV_IDC_REQ_CONNECT: uMinVersion=%#x uMaxVersion=%#x doesn't match!\n",
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.In.uMinVersion, pReq->u.In.uReqVersion));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return pReqHdr->rc = VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Match the version.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * The current logic is very simple, match the major interface version.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( pReq->u.In.uMinVersion > SUPDRV_IDC_VERSION
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || (pReq->u.In.uMinVersion & 0xffff0000) != (SUPDRV_IDC_VERSION & 0xffff0000))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(("SUPDRV_IDC_REQ_CONNECT: Version mismatch. Requested: %#x Min: %#x Current: %#x\n",
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.In.uReqVersion, pReq->u.In.uMinVersion, (unsigned)SUPDRV_IDC_VERSION));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.pSession = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.uSessionVersion = 0xffffffff;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.uDriverVersion = SUPDRV_IDC_VERSION;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.uDriverRevision = VBOX_SVN_REV;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = VERR_VERSION_MISMATCH;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.pSession = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.uSessionVersion = SUPDRV_IDC_VERSION;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.uDriverVersion = SUPDRV_IDC_VERSION;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.uDriverRevision = VBOX_SVN_REV;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * On NT we will already have a session associated with the
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * client, just like with the SUP_IOCTL_COOKIE request, while
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * the other doesn't.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef RT_OS_WINDOWS
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(!pSession, VERR_INTERNAL_ERROR);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = supdrvCreateSession(pDevExt, false /* fUser */, &pSession);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_FAILURE(pReq->Hdr.rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(("SUPDRV_IDC_REQ_CONNECT: failed to create session, rc=%d\n", pReq->Hdr.rc));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.pSession = pSession;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.pSession = pSession;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPDRV_IDC_REQ_DISCONNECT:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_IDC_SIZE(SUPDRV_IDC_REQ_DISCONNECT, sizeof(*pReqHdr));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef RT_OS_WINDOWS
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Windows will destroy the session when the file object is destroyed. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvCloseSession(pDevExt, pSession);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return pReqHdr->rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPDRV_IDC_REQ_GET_SYMBOL:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVIDCREQGETSYM pReq = (PSUPDRVIDCREQGETSYM)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_IDC_SIZE(SUPDRV_IDC_REQ_GET_SYMBOL, sizeof(*pReq));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = supdrvIDC_LdrGetSymbol(pDevExt, pSession, pReq);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPDRV_IDC_REQ_COMPONENT_REGISTER_FACTORY:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVIDCREQCOMPREGFACTORY pReq = (PSUPDRVIDCREQCOMPREGFACTORY)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_IDC_SIZE(SUPDRV_IDC_REQ_COMPONENT_REGISTER_FACTORY, sizeof(*pReq));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPR0ComponentRegisterFactory(pSession, pReq->u.In.pFactory);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPDRV_IDC_REQ_COMPONENT_DEREGISTER_FACTORY:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVIDCREQCOMPDEREGFACTORY pReq = (PSUPDRVIDCREQCOMPDEREGFACTORY)pReqHdr;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico REQ_CHECK_IDC_SIZE(SUPDRV_IDC_REQ_COMPONENT_DEREGISTER_FACTORY, sizeof(*pReq));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->Hdr.rc = SUPR0ComponentDeregisterFactory(pSession, pReq->u.In.pFactory);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico default:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("Unknown IDC %#lx\n", (long)uReq));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#undef REQ_CHECK_IDC_SIZE
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_NOT_SUPPORTED;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Register a object for reference counting.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * The object is registered with one reference in the specified session.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns Unique identifier on success (pointer).
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * All future reference must use this identifier.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns NULL on failure.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pfnDestructor The destructore function which will be called when the reference count reaches 0.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvUser1 The first user argument.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvUser2 The second user argument.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(void *) SUPR0ObjRegister(PSUPDRVSESSION pSession, SUPDRVOBJTYPE enmType, PFNSUPDRVDESTRUCTOR pfnDestructor, void *pvUser1, void *pvUser2)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVDEVEXT pDevExt = pSession->pDevExt;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVOBJ pObj;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVUSAGE pUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Validate the input.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(SUP_IS_SESSION_VALID(pSession), NULL);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(enmType > SUPDRVOBJTYPE_INVALID && enmType < SUPDRVOBJTYPE_END, NULL);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertPtrReturn(pfnDestructor, NULL);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Allocate and initialize the object.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj = (PSUPDRVOBJ)RTMemAlloc(sizeof(*pObj));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!pObj)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj->u32Magic = SUPDRVOBJ_MAGIC;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj->enmType = enmType;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj->pNext = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj->cUsage = 1;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj->pfnDestructor = pfnDestructor;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj->pvUser1 = pvUser1;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj->pvUser2 = pvUser2;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj->CreatorUid = pSession->Uid;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj->CreatorGid = pSession->Gid;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj->CreatorProcess= pSession->Process;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvOSObjInitCreator(pObj, pSession);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Allocate the usage record.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * (We keep freed usage records around to simplify SUPR0ObjAddRefEx().)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockAcquire(pDevExt->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage = pDevExt->pUsageFree;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pUsage)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pUsageFree = pUsage->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage = (PSUPDRVUSAGE)RTMemAlloc(sizeof(*pUsage));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!pUsage)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMemFree(pObj);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockAcquire(pDevExt->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Insert the object and create the session usage record.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* The object. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj->pNext = pDevExt->pObjs;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pObjs = pObj;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* The session record. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage->cUsage = 1;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage->pObj = pObj;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage->pNext = pSession->pUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Log2(("SUPR0ObjRegister: pUsage=%p:{.pObj=%p, .pNext=%p}\n", pUsage, pUsage->pObj, pUsage->pNext)); */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->pUsage = pUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("SUPR0ObjRegister: returns %p (pvUser1=%p, pvUser=%p)\n", pObj, pvUser1, pvUser2));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return pObj;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Increment the reference counter for the object associating the reference
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * with the specified session.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvObj The identifier returned by SUPR0ObjRegister().
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession The session which is referencing the object.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @remarks The caller should not own any spinlocks and must carefully protect
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * itself against potential race with the destructor so freed memory
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * isn't accessed here.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0ObjAddRef(void *pvObj, PSUPDRVSESSION pSession)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return SUPR0ObjAddRefEx(pvObj, pSession, false /* fNoBlocking */);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Increment the reference counter for the object associating the reference
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * with the specified session.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VERR_TRY_AGAIN if fNoBlocking was set and a new usage record
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * couldn't be allocated. (If you see this you're not doing the right
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * thing and it won't ever work reliably.)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvObj The identifier returned by SUPR0ObjRegister().
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession The session which is referencing the object.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param fNoBlocking Set if it's not OK to block. Never try to make the
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * first reference to an object in a session with this
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * argument set.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @remarks The caller should not own any spinlocks and must carefully protect
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * itself against potential race with the destructor so freed memory
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * isn't accessed here.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0ObjAddRefEx(void *pvObj, PSUPDRVSESSION pSession, bool fNoBlocking)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVDEVEXT pDevExt = pSession->pDevExt;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVOBJ pObj = (PSUPDRVOBJ)pvObj;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVUSAGE pUsagePre;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVUSAGE pUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Validate the input.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Be ready for the destruction race (someone might be stuck in the
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * destructor waiting a lock we own).
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertPtrReturn(pObj, VERR_INVALID_POINTER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsgReturn(pObj->u32Magic == SUPDRVOBJ_MAGIC || pObj->u32Magic == SUPDRVOBJ_MAGIC_DEAD,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ("Invalid pvObj=%p magic=%#x (expected %#x or %#x)\n", pvObj, pObj->u32Magic, SUPDRVOBJ_MAGIC, SUPDRVOBJ_MAGIC_DEAD),
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockAcquire(pDevExt->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_UNLIKELY(pObj->u32Magic != SUPDRVOBJ_MAGIC))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsgFailed(("pvObj=%p magic=%#x\n", pvObj, pObj->u32Magic));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_WRONG_ORDER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Preallocate the usage record if we can.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsagePre = pDevExt->pUsageFree;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pUsagePre)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pUsageFree = pUsagePre->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else if (!fNoBlocking)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsagePre = (PSUPDRVUSAGE)RTMemAlloc(sizeof(*pUsagePre));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!pUsagePre)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_NO_MEMORY;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockAcquire(pDevExt->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_UNLIKELY(pObj->u32Magic != SUPDRVOBJ_MAGIC))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsgFailed(("pvObj=%p magic=%#x\n", pvObj, pObj->u32Magic));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_WRONG_ORDER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Reference the object.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj->cUsage++;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Look for the session record.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (pUsage = pSession->pUsage; pUsage; pUsage = pUsage->pNext)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*Log(("SUPR0AddRef: pUsage=%p:{.pObj=%p, .pNext=%p}\n", pUsage, pUsage->pObj, pUsage->pNext));*/
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pUsage->pObj == pObj)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pUsage)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage->cUsage++;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else if (pUsagePre)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* create a new session record. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsagePre->cUsage = 1;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsagePre->pObj = pObj;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsagePre->pNext = pSession->pUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->pUsage = pUsagePre;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*Log(("SUPR0AddRef: pUsagePre=%p:{.pObj=%p, .pNext=%p}\n", pUsagePre, pUsagePre->pObj, pUsagePre->pNext));*/
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsagePre = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj->cUsage--;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VERR_TRY_AGAIN;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Put any unused usage record into the free list..
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pUsagePre)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsagePre->pNext = pDevExt->pUsageFree;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pUsageFree = pUsagePre;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Decrement / destroy a reference counter record for an object.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * The object is uniquely identified by pfnDestructor+pvUser1+pvUser2.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VINF_SUCCESS if not destroyed.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VINF_OBJECT_DESTROYED if it's destroyed by this release call.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VERR_INVALID_PARAMETER if the object isn't valid. Will assert in
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * string builds.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvObj The identifier returned by SUPR0ObjRegister().
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession The session which is referencing the object.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0ObjRelease(void *pvObj, PSUPDRVSESSION pSession)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVDEVEXT pDevExt = pSession->pDevExt;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVOBJ pObj = (PSUPDRVOBJ)pvObj;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc = VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVUSAGE pUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVUSAGE pUsagePrev;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Validate the input.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsgReturn(VALID_PTR(pObj) && pObj->u32Magic == SUPDRVOBJ_MAGIC,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ("Invalid pvObj=%p magic=%#x (exepcted %#x)\n", pvObj, pObj ? pObj->u32Magic : 0, SUPDRVOBJ_MAGIC),
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Acquire the spinlock and look for the usage record.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockAcquire(pDevExt->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (pUsagePrev = NULL, pUsage = pSession->pUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsagePrev = pUsage, pUsage = pUsage->pNext)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*Log2(("SUPR0ObjRelease: pUsage=%p:{.pObj=%p, .pNext=%p}\n", pUsage, pUsage->pObj, pUsage->pNext));*/
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pUsage->pObj == pObj)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsg(pUsage->cUsage >= 1 && pObj->cUsage >= pUsage->cUsage, ("glob %d; sess %d\n", pObj->cUsage, pUsage->cUsage));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pUsage->cUsage > 1)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj->cUsage--;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage->cUsage--;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Free the session record.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pUsagePrev)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsagePrev->pNext = pUsage->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->pUsage = pUsage->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage->pNext = pDevExt->pUsageFree;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pUsageFree = pUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* What about the object? */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pObj->cUsage > 1)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj->cUsage--;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Object is to be destroyed, unlink it.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj->u32Magic = SUPDRVOBJ_MAGIC_DEAD;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VINF_OBJECT_DESTROYED;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pDevExt->pObjs == pObj)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pObjs = pObj->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVOBJ pObjPrev;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (pObjPrev = pDevExt->pObjs; pObjPrev; pObjPrev = pObjPrev->pNext)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pObjPrev->pNext == pObj)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObjPrev->pNext = pObj->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Assert(pObjPrev);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Call the destructor and free the object if required.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (rc == VINF_OBJECT_DESTROYED)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("SUPR0ObjRelease: destroying %p/%d (%p/%p) cpid=%RTproc pid=%RTproc dtor=%p\n",
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj, pObj->enmType, pObj->pvUser1, pObj->pvUser2, pObj->CreatorProcess, RTProcSelf(), pObj->pfnDestructor));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pObj->pfnDestructor)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj->pfnDestructor(pObj, pObj->pvUser1, pObj->pvUser2);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMemFree(pObj);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsg(pUsage, ("pvObj=%p\n", pvObj));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Verifies that the current process can access the specified object.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns The following IPRT status code:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VINF_SUCCESS if access was granted.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VERR_PERMISSION_DENIED if denied access.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VERR_INVALID_PARAMETER if invalid parameter.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvObj The identifier returned by SUPR0ObjRegister().
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession The session which wishes to access the object.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pszObjName Object string name. This is optional and depends on the object type.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @remark The caller is responsible for making sure the object isn't removed while
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * we're inside this function. If uncertain about this, just call AddRef before calling us.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0ObjVerifyAccess(void *pvObj, PSUPDRVSESSION pSession, const char *pszObjName)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVOBJ pObj = (PSUPDRVOBJ)pvObj;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Validate the input.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsgReturn(VALID_PTR(pObj) && pObj->u32Magic == SUPDRVOBJ_MAGIC,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ("Invalid pvObj=%p magic=%#x (exepcted %#x)\n", pvObj, pObj ? pObj->u32Magic : 0, SUPDRVOBJ_MAGIC),
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Check access. (returns true if a decision has been made.)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VERR_INTERNAL_ERROR;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (supdrvOSObjCanAccess(pObj, pSession, pszObjName, &rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Default policy is to allow the user to access his own
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * stuff but nothing else.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pObj->CreatorUid == pSession->Uid)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_PERMISSION_DENIED;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Lock pages.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession Session to which the locked memory should be associated.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvR3 Start of the memory range to lock.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * This must be page aligned.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param cPages Number of pages to lock.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param paPages Where to put the physical addresses of locked memory.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0LockMem(PSUPDRVSESSION pSession, RTR3PTR pvR3, uint32_t cPages, PRTHCPHYS paPages)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico SUPDRVMEMREF Mem = { NIL_RTR0MEMOBJ, NIL_RTR0MEMOBJ, MEMREF_TYPE_UNUSED };
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico const size_t cb = (size_t)cPages << PAGE_SHIFT;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("SUPR0LockMem: pSession=%p pvR3=%p cPages=%d paPages=%p\n", pSession, (void *)pvR3, cPages, paPages));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Verify input.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertPtrReturn(paPages, VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( RT_ALIGN_R3PT(pvR3, PAGE_SIZE, RTR3PTR) != pvR3
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || !pvR3)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("pvR3 (%p) must be page aligned and not NULL!\n", (void *)pvR3));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Let IPRT do the job.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Mem.eType = MEMREF_TYPE_LOCKED;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTR0MemObjLockUser(&Mem.MemObj, pvR3, cb, RTMEM_PROT_READ | RTMEM_PROT_WRITE, RTR0ProcHandleSelf());
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint32_t iPage = cPages;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsg(RTR0MemObjAddressR3(Mem.MemObj) == pvR3, ("%p == %p\n", RTR0MemObjAddressR3(Mem.MemObj), pvR3));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsg(RTR0MemObjSize(Mem.MemObj) == cb, ("%x == %x\n", RTR0MemObjSize(Mem.MemObj), cb));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico while (iPage-- > 0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico paPages[iPage] = RTR0MemObjGetPagePhysAddr(Mem.MemObj, iPage);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_UNLIKELY(paPages[iPage] == NIL_RTCCPHYS))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsgFailed(("iPage=%d\n", iPage));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VERR_INTERNAL_ERROR;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = supdrvMemAdd(&Mem, pSession);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_FAILURE(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc2 = RTR0MemObjFree(Mem.MemObj, false);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertRC(rc2);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Unlocks the memory pointed to by pv.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession Session to which the memory was locked.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvR3 Memory to unlock.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0UnlockMem(PSUPDRVSESSION pSession, RTR3PTR pvR3)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("SUPR0UnlockMem: pSession=%p pvR3=%p\n", pSession, (void *)pvR3));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return supdrvMemRelease(pSession, (RTHCUINTPTR)pvR3, MEMREF_TYPE_LOCKED);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Allocates a chunk of page aligned memory with contiguous and fixed physical
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * backing.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession Session data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param cPages Number of pages to allocate.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param ppvR0 Where to put the address of Ring-0 mapping the allocated memory.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param ppvR3 Where to put the address of Ring-3 mapping the allocated memory.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pHCPhys Where to put the physical address of allocated memory.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0ContAlloc(PSUPDRVSESSION pSession, uint32_t cPages, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PRTHCPHYS pHCPhys)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico SUPDRVMEMREF Mem = { NIL_RTR0MEMOBJ, NIL_RTR0MEMOBJ, MEMREF_TYPE_UNUSED };
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("SUPR0ContAlloc: pSession=%p cPages=%d ppvR0=%p ppvR3=%p pHCPhys=%p\n", pSession, cPages, ppvR0, ppvR3, pHCPhys));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Validate input.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!ppvR3 || !ppvR0 || !pHCPhys)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("Null pointer. All of these should be set: pSession=%p ppvR0=%p ppvR3=%p pHCPhys=%p\n",
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession, ppvR0, ppvR3, pHCPhys));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (cPages < 1 || cPages >= 256)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("Illegal request cPages=%d, must be greater than 0 and smaller than 256.\n", cPages));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_PAGE_COUNT_OUT_OF_RANGE;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Let IPRT do the job.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTR0MemObjAllocCont(&Mem.MemObj, cPages << PAGE_SHIFT, true /* executable R0 mapping */);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc2;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTR0MemObjMapUser(&Mem.MapObjR3, Mem.MemObj, (RTR3PTR)-1, 0,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMEM_PROT_EXEC | RTMEM_PROT_WRITE | RTMEM_PROT_READ, RTR0ProcHandleSelf());
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Mem.eType = MEMREF_TYPE_CONT;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = supdrvMemAdd(&Mem, pSession);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!rc)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *ppvR0 = RTR0MemObjAddress(Mem.MemObj);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *ppvR3 = RTR0MemObjAddressR3(Mem.MapObjR3);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *pHCPhys = RTR0MemObjGetPagePhysAddr(Mem.MemObj, 0);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc2 = RTR0MemObjFree(Mem.MapObjR3, false);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertRC(rc2);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc2 = RTR0MemObjFree(Mem.MemObj, false);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertRC(rc2);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Frees memory allocated using SUPR0ContAlloc().
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession The session to which the memory was allocated.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param uPtr Pointer to the memory (ring-3 or ring-0).
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0ContFree(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("SUPR0ContFree: pSession=%p uPtr=%p\n", pSession, (void *)uPtr));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return supdrvMemRelease(pSession, uPtr, MEMREF_TYPE_CONT);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Allocates a chunk of page aligned memory with fixed physical backing below 4GB.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * The memory isn't zeroed.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession Session data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param cPages Number of pages to allocate.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param ppvR0 Where to put the address of Ring-0 mapping of the allocated memory.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param ppvR3 Where to put the address of Ring-3 mapping of the allocated memory.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param paPages Where to put the physical addresses of allocated memory.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0LowAlloc(PSUPDRVSESSION pSession, uint32_t cPages, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PRTHCPHYS paPages)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico unsigned iPage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico SUPDRVMEMREF Mem = { NIL_RTR0MEMOBJ, NIL_RTR0MEMOBJ, MEMREF_TYPE_UNUSED };
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("SUPR0LowAlloc: pSession=%p cPages=%d ppvR3=%p ppvR0=%p paPages=%p\n", pSession, cPages, ppvR3, ppvR0, paPages));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Validate input.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!ppvR3 || !ppvR0 || !paPages)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("Null pointer. All of these should be set: pSession=%p ppvR3=%p ppvR0=%p paPages=%p\n",
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession, ppvR3, ppvR0, paPages));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (cPages < 1 || cPages >= 256)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("Illegal request cPages=%d, must be greater than 0 and smaller than 256.\n", cPages));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_PAGE_COUNT_OUT_OF_RANGE;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Let IPRT do the work.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTR0MemObjAllocLow(&Mem.MemObj, cPages << PAGE_SHIFT, true /* executable ring-0 mapping */);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc2;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTR0MemObjMapUser(&Mem.MapObjR3, Mem.MemObj, (RTR3PTR)-1, 0,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMEM_PROT_EXEC | RTMEM_PROT_WRITE | RTMEM_PROT_READ, RTR0ProcHandleSelf());
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Mem.eType = MEMREF_TYPE_LOW;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = supdrvMemAdd(&Mem, pSession);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!rc)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (iPage = 0; iPage < cPages; iPage++)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico paPages[iPage] = RTR0MemObjGetPagePhysAddr(Mem.MemObj, iPage);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsg(!(paPages[iPage] & (PAGE_SIZE - 1)), ("iPage=%d Phys=%RHp\n", paPages[iPage]));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *ppvR0 = RTR0MemObjAddress(Mem.MemObj);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *ppvR3 = RTR0MemObjAddressR3(Mem.MapObjR3);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc2 = RTR0MemObjFree(Mem.MapObjR3, false);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertRC(rc2);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc2 = RTR0MemObjFree(Mem.MemObj, false);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertRC(rc2);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Frees memory allocated using SUPR0LowAlloc().
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession The session to which the memory was allocated.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param uPtr Pointer to the memory (ring-3 or ring-0).
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0LowFree(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("SUPR0LowFree: pSession=%p uPtr=%p\n", pSession, (void *)uPtr));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return supdrvMemRelease(pSession, uPtr, MEMREF_TYPE_LOW);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Allocates a chunk of memory with both R0 and R3 mappings.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * The memory is fixed and it's possible to query the physical addresses using SUPR0MemGetPhys().
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession The session to associated the allocation with.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param cb Number of bytes to allocate.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param ppvR0 Where to store the address of the Ring-0 mapping.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param ppvR3 Where to store the address of the Ring-3 mapping.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0MemAlloc(PSUPDRVSESSION pSession, uint32_t cb, PRTR0PTR ppvR0, PRTR3PTR ppvR3)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico SUPDRVMEMREF Mem = { NIL_RTR0MEMOBJ, NIL_RTR0MEMOBJ, MEMREF_TYPE_UNUSED };
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("SUPR0MemAlloc: pSession=%p cb=%d ppvR0=%p ppvR3=%p\n", pSession, cb, ppvR0, ppvR3));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Validate input.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertPtrReturn(ppvR0, VERR_INVALID_POINTER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertPtrReturn(ppvR3, VERR_INVALID_POINTER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (cb < 1 || cb >= _4M)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("Illegal request cb=%u; must be greater than 0 and smaller than 4MB.\n", cb));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Let IPRT do the work.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTR0MemObjAllocPage(&Mem.MemObj, cb, true /* executable ring-0 mapping */);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc2;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTR0MemObjMapUser(&Mem.MapObjR3, Mem.MemObj, (RTR3PTR)-1, 0,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMEM_PROT_EXEC | RTMEM_PROT_WRITE | RTMEM_PROT_READ, RTR0ProcHandleSelf());
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Mem.eType = MEMREF_TYPE_MEM;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = supdrvMemAdd(&Mem, pSession);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!rc)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *ppvR0 = RTR0MemObjAddress(Mem.MemObj);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *ppvR3 = RTR0MemObjAddressR3(Mem.MapObjR3);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc2 = RTR0MemObjFree(Mem.MapObjR3, false);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertRC(rc2);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc2 = RTR0MemObjFree(Mem.MemObj, false);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertRC(rc2);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Get the physical addresses of memory allocated using SUPR0MemAlloc().
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession The session to which the memory was allocated.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param uPtr The Ring-0 or Ring-3 address returned by SUPR0MemAlloc().
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param paPages Where to store the physical addresses.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0MemGetPhys(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr, PSUPPAGE paPages) /** @todo switch this bugger to RTHCPHYS */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVBUNDLE pBundle;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("SUPR0MemGetPhys: pSession=%p uPtr=%p paPages=%p\n", pSession, (void *)uPtr, paPages));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Validate input.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertPtrReturn(paPages, VERR_INVALID_POINTER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(uPtr, VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Search for the address.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockAcquire(pSession->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (pBundle = &pSession->Bundle; pBundle; pBundle = pBundle->pNext)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pBundle->cUsed > 0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico unsigned i;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (i = 0; i < RT_ELEMENTS(pBundle->aMem); i++)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( pBundle->aMem[i].eType == MEMREF_TYPE_MEM
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && pBundle->aMem[i].MemObj != NIL_RTR0MEMOBJ
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && ( (RTHCUINTPTR)RTR0MemObjAddress(pBundle->aMem[i].MemObj) == uPtr
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || ( pBundle->aMem[i].MapObjR3 != NIL_RTR0MEMOBJ
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && RTR0MemObjAddressR3(pBundle->aMem[i].MapObjR3) == uPtr)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico )
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico )
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico const size_t cPages = RTR0MemObjSize(pBundle->aMem[i].MemObj) >> PAGE_SHIFT;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico size_t iPage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (iPage = 0; iPage < cPages; iPage++)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico paPages[iPage].Phys = RTR0MemObjGetPagePhysAddr(pBundle->aMem[i].MemObj, iPage);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico paPages[iPage].uReserved = 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockRelease(pSession->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockRelease(pSession->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("Failed to find %p!!!\n", (void *)uPtr));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Free memory allocated by SUPR0MemAlloc().
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession The session owning the allocation.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param uPtr The Ring-0 or Ring-3 address returned by SUPR0MemAlloc().
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0MemFree(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("SUPR0MemFree: pSession=%p uPtr=%p\n", pSession, (void *)uPtr));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return supdrvMemRelease(pSession, uPtr, MEMREF_TYPE_MEM);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Allocates a chunk of memory with a kernel or/and a user mode mapping.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * The memory is fixed and it's possible to query the physical addresses using
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * SUPR0MemGetPhys().
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession The session to associated the allocation with.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param cPages The number of pages to allocate.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param fFlags Flags, reserved for the future. Must be zero.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param ppvR3 Where to store the address of the Ring-3 mapping.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * NULL if no ring-3 mapping.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param ppvR3 Where to store the address of the Ring-0 mapping.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * NULL if no ring-0 mapping.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param paPages Where to store the addresses of the pages. Optional.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0PageAllocEx(PSUPDRVSESSION pSession, uint32_t cPages, uint32_t fFlags, PRTR3PTR ppvR3, PRTR0PTR ppvR0, PRTHCPHYS paPages)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico SUPDRVMEMREF Mem = { NIL_RTR0MEMOBJ, NIL_RTR0MEMOBJ, MEMREF_TYPE_UNUSED };
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("SUPR0PageAlloc: pSession=%p cb=%d ppvR3=%p\n", pSession, cPages, ppvR3));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Validate input. The allowed allocation size must be at least equal to the maximum guest VRAM size.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertPtrNullReturn(ppvR3, VERR_INVALID_POINTER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertPtrNullReturn(ppvR0, VERR_INVALID_POINTER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(ppvR3 || ppvR0, VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (cPages < 1 || cPages > VBOX_MAX_ALLOC_PAGE_COUNT)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico 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)));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_PAGE_COUNT_OUT_OF_RANGE;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Let IPRT do the work.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (ppvR0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTR0MemObjAllocPage(&Mem.MemObj, (size_t)cPages * PAGE_SIZE, true /* fExecutable */);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTR0MemObjAllocPhysNC(&Mem.MemObj, (size_t)cPages * PAGE_SIZE, NIL_RTHCPHYS);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc2;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (ppvR3)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTR0MemObjMapUser(&Mem.MapObjR3, Mem.MemObj, (RTR3PTR)-1, 0,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMEM_PROT_EXEC | RTMEM_PROT_WRITE | RTMEM_PROT_READ, RTR0ProcHandleSelf());
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Mem.MapObjR3 = NIL_RTR0MEMOBJ;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Mem.eType = MEMREF_TYPE_PAGE;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = supdrvMemAdd(&Mem, pSession);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!rc)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (ppvR3)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *ppvR3 = RTR0MemObjAddressR3(Mem.MapObjR3);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (ppvR0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *ppvR0 = RTR0MemObjAddress(Mem.MemObj);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (paPages)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint32_t iPage = cPages;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico while (iPage-- > 0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico paPages[iPage] = RTR0MemObjGetPagePhysAddr(Mem.MapObjR3, iPage);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Assert(paPages[iPage] != NIL_RTHCPHYS);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc2 = RTR0MemObjFree(Mem.MapObjR3, false);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertRC(rc2);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc2 = RTR0MemObjFree(Mem.MemObj, false);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertRC(rc2);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Maps a chunk of memory previously allocated by SUPR0PageAllocEx into kernel
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * space.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession The session to associated the allocation with.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvR3 The ring-3 address returned by SUPR0PageAllocEx.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param offSub Where to start mapping. Must be page aligned.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param cbSub How much to map. Must be page aligned.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param fFlags Flags, MBZ.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param ppvR0 Where to reutrn the address of the ring-0 mapping on
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * success.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0PageMapKernel(PSUPDRVSESSION pSession, RTR3PTR pvR3, uint32_t offSub, uint32_t cbSub,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint32_t fFlags, PRTR0PTR ppvR0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVBUNDLE pBundle;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTR0MEMOBJ hMemObj = NIL_RTR0MEMOBJ;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("SUPR0PageMapKernel: pSession=%p pvR3=%p offSub=%#x cbSub=%#x\n", pSession, pvR3, offSub, cbSub));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Validate input. The allowed allocation size must be at least equal to the maximum guest VRAM size.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertPtrNullReturn(ppvR0, VERR_INVALID_POINTER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(!(offSub & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(!(cbSub & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(cbSub, VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Find the memory object.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockAcquire(pSession->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (pBundle = &pSession->Bundle; pBundle; pBundle = pBundle->pNext)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pBundle->cUsed > 0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico unsigned i;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (i = 0; i < RT_ELEMENTS(pBundle->aMem); i++)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( ( pBundle->aMem[i].eType == MEMREF_TYPE_PAGE
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && pBundle->aMem[i].MemObj != NIL_RTR0MEMOBJ
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && pBundle->aMem[i].MapObjR3 != NIL_RTR0MEMOBJ
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && RTR0MemObjAddressR3(pBundle->aMem[i].MapObjR3) == pvR3)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || ( pBundle->aMem[i].eType == MEMREF_TYPE_LOCKED
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && pBundle->aMem[i].MemObj != NIL_RTR0MEMOBJ
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && pBundle->aMem[i].MapObjR3 == NIL_RTR0MEMOBJ
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && RTR0MemObjAddressR3(pBundle->aMem[i].MemObj) == pvR3))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico hMemObj = pBundle->aMem[i].MemObj;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockRelease(pSession->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (hMemObj != NIL_RTR0MEMOBJ)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Do some furter input validations before calling IPRT.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * (Cleanup is done indirectly by telling RTR0MemObjFree to include mappings.)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico size_t cbMemObj = RTR0MemObjSize(hMemObj);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( offSub < cbMemObj
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && cbSub <= cbMemObj
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && offSub + cbSub <= cbMemObj)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTR0MEMOBJ hMapObj;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTR0MemObjMapKernelEx(&hMapObj, hMemObj, (void *)-1, 0,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMEM_PROT_READ | RTMEM_PROT_WRITE, offSub, cbSub);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *ppvR0 = RTR0MemObjAddress(hMapObj);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico SUPR0Printf("SUPR0PageMapKernel: cbMemObj=%#x offSub=%#x cbSub=%#x\n", cbMemObj, offSub, cbSub);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Changes the page level protection of one or more pages previously allocated
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * by SUPR0PageAllocEx.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession The session to associated the allocation with.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvR3 The ring-3 address returned by SUPR0PageAllocEx.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * NIL_RTR3PTR if the ring-3 mapping should be unaffected.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvR0 The ring-0 address returned by SUPR0PageAllocEx.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * NIL_RTR0PTR if the ring-0 mapping should be unaffected.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param offSub Where to start changing. Must be page aligned.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param cbSub How much to change. Must be page aligned.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param fProt The new page level protection, see RTMEM_PROT_*.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0PageProtect(PSUPDRVSESSION pSession, RTR3PTR pvR3, RTR0PTR pvR0, uint32_t offSub, uint32_t cbSub, uint32_t fProt)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVBUNDLE pBundle;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTR0MEMOBJ hMemObjR0 = NIL_RTR0MEMOBJ;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTR0MEMOBJ hMemObjR3 = NIL_RTR0MEMOBJ;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("SUPR0PageProtect: pSession=%p pvR3=%p pvR0=%p offSub=%#x cbSub=%#x fProt-%#x\n", pSession, pvR3, pvR0, offSub, cbSub, fProt));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Validate input. The allowed allocation size must be at least equal to the maximum guest VRAM size.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(!(fProt & ~(RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC | RTMEM_PROT_NONE)), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(!(offSub & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(!(cbSub & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(cbSub, VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Find the memory object.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockAcquire(pSession->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (pBundle = &pSession->Bundle; pBundle; pBundle = pBundle->pNext)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pBundle->cUsed > 0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico unsigned i;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (i = 0; i < RT_ELEMENTS(pBundle->aMem); i++)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( pBundle->aMem[i].eType == MEMREF_TYPE_PAGE
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && pBundle->aMem[i].MemObj != NIL_RTR0MEMOBJ
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && ( pBundle->aMem[i].MapObjR3 != NIL_RTR0MEMOBJ
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || pvR3 == NIL_RTR3PTR)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && ( pvR0 == NIL_RTR0PTR
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || RTR0MemObjAddress(pBundle->aMem[i].MemObj) == pvR0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && ( pvR3 == NIL_RTR3PTR
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || RTR0MemObjAddressR3(pBundle->aMem[i].MapObjR3) == pvR3))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pvR0 != NIL_RTR0PTR)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico hMemObjR0 = pBundle->aMem[i].MemObj;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pvR3 != NIL_RTR3PTR)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico hMemObjR3 = pBundle->aMem[i].MapObjR3;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockRelease(pSession->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( hMemObjR0 != NIL_RTR0MEMOBJ
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || hMemObjR3 != NIL_RTR0MEMOBJ)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Do some furter input validations before calling IPRT.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico size_t cbMemObj = hMemObjR0 != NIL_RTR0PTR ? RTR0MemObjSize(hMemObjR0) : RTR0MemObjSize(hMemObjR3);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( offSub < cbMemObj
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && cbSub <= cbMemObj
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && offSub + cbSub <= cbMemObj)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (hMemObjR3 != NIL_RTR0PTR)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTR0MemObjProtect(hMemObjR3, offSub, cbSub, fProt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (hMemObjR0 != NIL_RTR0PTR && RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTR0MemObjProtect(hMemObjR0, offSub, cbSub, fProt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico SUPR0Printf("SUPR0PageMapKernel: cbMemObj=%#x offSub=%#x cbSub=%#x\n", cbMemObj, offSub, cbSub);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Free memory allocated by SUPR0PageAlloc() and SUPR0PageAllocEx().
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession The session owning the allocation.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvR3 The Ring-3 address returned by SUPR0PageAlloc() or
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * SUPR0PageAllocEx().
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0PageFree(PSUPDRVSESSION pSession, RTR3PTR pvR3)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("SUPR0PageFree: pSession=%p pvR3=%p\n", pSession, (void *)pvR3));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return supdrvMemRelease(pSession, (RTHCUINTPTR)pvR3, MEMREF_TYPE_PAGE);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Gets the paging mode of the current CPU.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns Paging mode, SUPPAGEINGMODE_INVALID on error.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(SUPPAGINGMODE) SUPR0GetPagingMode(void)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico SUPPAGINGMODE enmMode;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTR0UINTREG cr0 = ASMGetCR0();
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ((cr0 & (X86_CR0_PG | X86_CR0_PE)) != (X86_CR0_PG | X86_CR0_PE))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico enmMode = SUPPAGINGMODE_INVALID;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTR0UINTREG cr4 = ASMGetCR4();
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint32_t fNXEPlusLMA = 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (cr4 & X86_CR4_PAE)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint32_t fAmdFeatures = ASMCpuId_EDX(0x80000001);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (fAmdFeatures & (X86_CPUID_AMD_FEATURE_EDX_NX | X86_CPUID_AMD_FEATURE_EDX_LONG_MODE))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint64_t efer = ASMRdMsr(MSR_K6_EFER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ((fAmdFeatures & X86_CPUID_AMD_FEATURE_EDX_NX) && (efer & MSR_K6_EFER_NXE))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico fNXEPlusLMA |= RT_BIT(0);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ((fAmdFeatures & X86_CPUID_AMD_FEATURE_EDX_LONG_MODE) && (efer & MSR_K6_EFER_LMA))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico fNXEPlusLMA |= RT_BIT(1);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico switch ((cr4 & (X86_CR4_PAE | X86_CR4_PGE)) | fNXEPlusLMA)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case 0:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico enmMode = SUPPAGINGMODE_32_BIT;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case X86_CR4_PGE:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico enmMode = SUPPAGINGMODE_32_BIT_GLOBAL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case X86_CR4_PAE:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico enmMode = SUPPAGINGMODE_PAE;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case X86_CR4_PAE | RT_BIT(0):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico enmMode = SUPPAGINGMODE_PAE_NX;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case X86_CR4_PAE | X86_CR4_PGE:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico enmMode = SUPPAGINGMODE_PAE_GLOBAL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case X86_CR4_PAE | X86_CR4_PGE | RT_BIT(0):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico enmMode = SUPPAGINGMODE_PAE_GLOBAL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case RT_BIT(1) | X86_CR4_PAE:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico enmMode = SUPPAGINGMODE_AMD64;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case RT_BIT(1) | X86_CR4_PAE | RT_BIT(0):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico enmMode = SUPPAGINGMODE_AMD64_NX;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case RT_BIT(1) | X86_CR4_PAE | X86_CR4_PGE:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico enmMode = SUPPAGINGMODE_AMD64_GLOBAL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case RT_BIT(1) | X86_CR4_PAE | X86_CR4_PGE | RT_BIT(0):
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico enmMode = SUPPAGINGMODE_AMD64_GLOBAL_NX;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico default:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsgFailed(("Cannot happen! cr4=%#x fNXEPlusLMA=%d\n", cr4, fNXEPlusLMA));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico enmMode = SUPPAGINGMODE_INVALID;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return enmMode;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Enables or disabled hardware virtualization extensions using native OS APIs.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns VBox status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VINF_SUCCESS on success.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VERR_NOT_SUPPORTED if not supported by the native OS.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param fEnable Whether to enable or disable.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0EnableVTx(bool fEnable)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef RT_OS_DARWIN
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return supdrvOSEnableVTx(fEnable);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_NOT_SUPPORTED;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/** @todo document me */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0QueryVTCaps(PSUPDRVSESSION pSession, uint32_t *pfCaps)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Input validation.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertPtrReturn(pfCaps, VERR_INVALID_POINTER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *pfCaps = 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (ASMHasCpuId())
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint32_t u32FeaturesECX;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint32_t u32Dummy;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint32_t u32FeaturesEDX;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint32_t u32VendorEBX, u32VendorECX, u32VendorEDX, u32AMDFeatureEDX, u32AMDFeatureECX;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint64_t val;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMCpuId(0, &u32Dummy, &u32VendorEBX, &u32VendorECX, &u32VendorEDX);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMCpuId(1, &u32Dummy, &u32Dummy, &u32FeaturesECX, &u32FeaturesEDX);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Query AMD features. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMCpuId(0x80000001, &u32Dummy, &u32Dummy, &u32AMDFeatureECX, &u32AMDFeatureEDX);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( u32VendorEBX == X86_CPUID_VENDOR_INTEL_EBX
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && u32VendorECX == X86_CPUID_VENDOR_INTEL_ECX
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && u32VendorEDX == X86_CPUID_VENDOR_INTEL_EDX
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico )
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( (u32FeaturesECX & X86_CPUID_FEATURE_ECX_VMX)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && (u32FeaturesEDX & X86_CPUID_FEATURE_EDX_MSR)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && (u32FeaturesEDX & X86_CPUID_FEATURE_EDX_FXSR)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico )
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico val = ASMRdMsr(MSR_IA32_FEATURE_CONTROL);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Both the LOCK and VMXON bit must be set; otherwise VMXON will generate a #GP.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Once the lock bit is set, this MSR can no longer be modified.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( (val & (MSR_IA32_FEATURE_CONTROL_VMXON|MSR_IA32_FEATURE_CONTROL_LOCK))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico == (MSR_IA32_FEATURE_CONTROL_VMXON|MSR_IA32_FEATURE_CONTROL_LOCK) /* enabled and locked */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || !(val & MSR_IA32_FEATURE_CONTROL_LOCK) /* not enabled, but not locked either */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico )
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico VMX_CAPABILITY vtCaps;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *pfCaps |= SUPVTCAPS_VT_X;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico vtCaps.u = ASMRdMsr(MSR_IA32_VMX_PROCBASED_CTLS);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (vtCaps.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC_USE_SECONDARY_EXEC_CTRL)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico vtCaps.u = ASMRdMsr(MSR_IA32_VMX_PROCBASED_CTLS2);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (vtCaps.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC2_EPT)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *pfCaps |= SUPVTCAPS_NESTED_PAGING;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_VMX_MSR_LOCKED_OR_DISABLED;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_VMX_NO_VMX;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( u32VendorEBX == X86_CPUID_VENDOR_AMD_EBX
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && u32VendorECX == X86_CPUID_VENDOR_AMD_ECX
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && u32VendorEDX == X86_CPUID_VENDOR_AMD_EDX
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico )
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( (u32AMDFeatureECX & X86_CPUID_AMD_FEATURE_ECX_SVM)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && (u32FeaturesEDX & X86_CPUID_FEATURE_EDX_MSR)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && (u32FeaturesEDX & X86_CPUID_FEATURE_EDX_FXSR)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico )
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Check if SVM is disabled */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico val = ASMRdMsr(MSR_K8_VM_CR);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!(val & MSR_K8_VM_CR_SVM_DISABLE))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *pfCaps |= SUPVTCAPS_AMD_V;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Query AMD features. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMCpuId(0x8000000A, &u32Dummy, &u32Dummy, &u32Dummy, &u32FeaturesEDX);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (u32FeaturesEDX & AMD_CPUID_SVM_FEATURE_EDX_NESTED_PAGING)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *pfCaps |= SUPVTCAPS_NESTED_PAGING;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_SVM_DISABLED;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_SVM_NO_SVM;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_UNSUPPORTED_CPU;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * (Re-)initializes the per-cpu structure prior to starting or resuming the GIP
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * updating.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pGipCpu The per CPU structure for this CPU.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param u64NanoTS The current time.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic void supdrvGipReInitCpu(PSUPGIPCPU pGipCpu, uint64_t u64NanoTS)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGipCpu->u64TSC = ASMReadTSC() - pGipCpu->u32UpdateIntervalTSC;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGipCpu->u64NanoTS = u64NanoTS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Set the current TSC and NanoTS value for the CPU.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param idCpu The CPU ID. Unused - we have to use the APIC ID.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvUser1 Pointer to the ring-0 GIP mapping.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvUser2 Pointer to the variable holding the current time.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic DECLCALLBACK(void) supdrvGipReInitCpuCallback(RTCPUID idCpu, void *pvUser1, void *pvUser2)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPGLOBALINFOPAGE pGip = (PSUPGLOBALINFOPAGE)pvUser1;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico unsigned iCpu = ASMGetApicId();
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_LIKELY(iCpu < RT_ELEMENTS(pGip->aCPUs)))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvGipReInitCpu(&pGip->aCPUs[iCpu], *(uint64_t *)pvUser2);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico NOREF(pvUser2);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico NOREF(idCpu);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Maps the GIP into userspace and/or get the physical address of the GIP.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession Session to which the GIP mapping should belong.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param ppGipR3 Where to store the address of the ring-3 mapping. (optional)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pHCPhysGip Where to store the physical address. (optional)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @remark There is no reference counting on the mapping, so one call to this function
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * count globally as one reference. One call to SUPR0GipUnmap() is will unmap GIP
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * and remove the session as a GIP user.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0GipMap(PSUPDRVSESSION pSession, PRTR3PTR ppGipR3, PRTHCPHYS pHCPhysGip)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVDEVEXT pDevExt = pSession->pDevExt;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTR3PTR pGipR3 = NIL_RTR3PTR;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTHCPHYS HCPhys = NIL_RTHCPHYS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("SUPR0GipMap: pSession=%p ppGipR3=%p pHCPhysGip=%p\n", pSession, ppGipR3, pHCPhysGip));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Validate
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertPtrNullReturn(ppGipR3, VERR_INVALID_POINTER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertPtrNullReturn(pHCPhysGip, VERR_INVALID_POINTER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef SUPDRV_USE_MUTEX_FOR_GIP
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSemMutexRequest(pDevExt->mtxGip, RT_INDEFINITE_WAIT);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSemFastMutexRequest(pDevExt->mtxGip);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pDevExt->pGip)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Map it?
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (ppGipR3)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pSession->GipMapObjR3 == NIL_RTR0MEMOBJ)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTR0MemObjMapUser(&pSession->GipMapObjR3, pDevExt->GipMemObj, (RTR3PTR)-1, 0,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMEM_PROT_READ, RTR0ProcHandleSelf());
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGipR3 = RTR0MemObjAddressR3(pSession->GipMapObjR3);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Get physical address.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pHCPhysGip && RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico HCPhys = pDevExt->HCPhysGip;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Reference globally.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!pSession->fGipReferenced && RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->fGipReferenced = 1;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->cGipUsers++;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pDevExt->cGipUsers == 1)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPGLOBALINFOPAGE pGipR0 = pDevExt->pGip;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint64_t u64NanoTS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico unsigned i;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("SUPR0GipMap: Resumes GIP updating\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pGipR0->aCPUs[0].u32TransactionId != 2 /* not the first time */)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (i = 0; i < RT_ELEMENTS(pGipR0->aCPUs); i++)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMAtomicUoWriteU32(&pGipR0->aCPUs[i].u32TransactionId,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico (pGipR0->aCPUs[i].u32TransactionId + GIP_UPDATEHZ_RECALC_FREQ * 2)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico & ~(GIP_UPDATEHZ_RECALC_FREQ * 2 - 1));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMAtomicWriteU64(&pGipR0->u64NanoTSLastUpdateHz, 0);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u64NanoTS = RTTimeSystemNanoTS() - pGipR0->u32UpdateIntervalNS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( pGipR0->u32Mode == SUPGIPMODE_SYNC_TSC
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || RTMpGetOnlineCount() == 1)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvGipReInitCpu(&pGipR0->aCPUs[0], u64NanoTS);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMpOnAll(supdrvGipReInitCpuCallback, pGipR0, &u64NanoTS);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifndef DO_NOT_START_GIP
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTTimerStart(pDevExt->pGipTimer, 0); AssertRC(rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VERR_GENERAL_FAILURE;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("SUPR0GipMap: GIP is not available!\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef SUPDRV_USE_MUTEX_FOR_GIP
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSemMutexRelease(pDevExt->mtxGip);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSemFastMutexRelease(pDevExt->mtxGip);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Write returns.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pHCPhysGip)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *pHCPhysGip = HCPhys;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (ppGipR3)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *ppGipR3 = pGipR3;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef DEBUG_DARWIN_GIP
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(("SUPR0GipMap: returns %d *pHCPhysGip=%lx pGipR3=%p\n", rc, (unsigned long)HCPhys, (void *)pGipR3));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(( "SUPR0GipMap: returns %d *pHCPhysGip=%lx pGipR3=%p\n", rc, (unsigned long)HCPhys, (void *)pGipR3));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Unmaps any user mapping of the GIP and terminates all GIP access
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * from this session.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession Session to which the GIP mapping should belong.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0GipUnmap(PSUPDRVSESSION pSession)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVDEVEXT pDevExt = pSession->pDevExt;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef DEBUG_DARWIN_GIP
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(("SUPR0GipUnmap: pSession=%p pGip=%p GipMapObjR3=%p\n",
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->GipMapObjR3 != NIL_RTR0MEMOBJ ? RTR0MemObjAddress(pSession->GipMapObjR3) : NULL,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->GipMapObjR3));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("SUPR0GipUnmap: pSession=%p\n", pSession));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef SUPDRV_USE_MUTEX_FOR_GIP
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSemMutexRequest(pDevExt->mtxGip, RT_INDEFINITE_WAIT);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSemFastMutexRequest(pDevExt->mtxGip);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Unmap anything?
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pSession->GipMapObjR3 != NIL_RTR0MEMOBJ)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTR0MemObjFree(pSession->GipMapObjR3, false);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertRC(rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->GipMapObjR3 = NIL_RTR0MEMOBJ;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Dereference global GIP.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pSession->fGipReferenced && !rc)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->fGipReferenced = 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( pDevExt->cGipUsers > 0
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && !--pDevExt->cGipUsers)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("SUPR0GipUnmap: Suspends GIP updating\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifndef DO_NOT_START_GIP
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTTimerStop(pDevExt->pGipTimer); AssertRC(rc); rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef SUPDRV_USE_MUTEX_FOR_GIP
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSemMutexRelease(pDevExt->mtxGip);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSemFastMutexRelease(pDevExt->mtxGip);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Gets the GIP pointer.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns Pointer to the GIP or NULL.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPDECL(PSUPGLOBALINFOPAGE) SUPGetGIP(void)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return g_pSUPGlobalInfoPage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Register a component factory with the support driver.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * This is currently restricted to kernel sessions only.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns VBox status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VINF_SUCCESS on success.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VERR_NO_MEMORY if we're out of memory.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VERR_ALREADY_EXISTS if the factory has already been registered.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VERR_ACCESS_DENIED if it isn't a kernel session.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VERR_INVALID_PARAMETER on invalid parameter.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VERR_INVALID_POINTER on invalid pointer parameter.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession The SUPDRV session (must be a ring-0 session).
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pFactory Pointer to the component factory registration structure.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @remarks This interface is also available via SUPR0IdcComponentRegisterFactory.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0ComponentRegisterFactory(PSUPDRVSESSION pSession, PCSUPDRVFACTORY pFactory)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVFACTORYREG pNewReg;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico const char *psz;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Validate parameters.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(pSession->R0Process == NIL_RTR0PROCESS, VERR_ACCESS_DENIED);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertPtrReturn(pFactory, VERR_INVALID_POINTER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertPtrReturn(pFactory->pfnQueryFactoryInterface, VERR_INVALID_POINTER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico psz = RTStrEnd(pFactory->szName, sizeof(pFactory->szName));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(psz, VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Allocate and initialize a new registration structure.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pNewReg = (PSUPDRVFACTORYREG)RTMemAlloc(sizeof(SUPDRVFACTORYREG));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pNewReg)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pNewReg->pNext = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pNewReg->pFactory = pFactory;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pNewReg->pSession = pSession;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pNewReg->cchName = psz - &pFactory->szName[0];
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Add it to the tail of the list after checking for prior registration.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTSemFastMutexRequest(pSession->pDevExt->mtxComponentFactory);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVFACTORYREG pPrev = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVFACTORYREG pCur = pSession->pDevExt->pComponentFactoryHead;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico while (pCur && pCur->pFactory != pFactory)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pPrev = pCur;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pCur = pCur->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!pCur)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pPrev)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pPrev->pNext = pNewReg;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->pDevExt->pComponentFactoryHead = pNewReg;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VERR_ALREADY_EXISTS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSemFastMutexRelease(pSession->pDevExt->mtxComponentFactory);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_FAILURE(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMemFree(pNewReg);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VERR_NO_MEMORY;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Deregister a component factory.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns VBox status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VINF_SUCCESS on success.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VERR_NOT_FOUND if the factory wasn't registered.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VERR_ACCESS_DENIED if it isn't a kernel session.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VERR_INVALID_PARAMETER on invalid parameter.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VERR_INVALID_POINTER on invalid pointer parameter.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession The SUPDRV session (must be a ring-0 session).
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pFactory Pointer to the component factory registration structure
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * previously passed SUPR0ComponentRegisterFactory().
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @remarks This interface is also available via SUPR0IdcComponentDeregisterFactory.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0ComponentDeregisterFactory(PSUPDRVSESSION pSession, PCSUPDRVFACTORY pFactory)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Validate parameters.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(pSession->R0Process == NIL_RTR0PROCESS, VERR_ACCESS_DENIED);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertPtrReturn(pFactory, VERR_INVALID_POINTER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Take the lock and look for the registration record.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTSemFastMutexRequest(pSession->pDevExt->mtxComponentFactory);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVFACTORYREG pPrev = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVFACTORYREG pCur = pSession->pDevExt->pComponentFactoryHead;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico while (pCur && pCur->pFactory != pFactory)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pPrev = pCur;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pCur = pCur->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pCur)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!pPrev)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->pDevExt->pComponentFactoryHead = pCur->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pPrev->pNext = pCur->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pCur->pNext = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pCur->pFactory = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pCur->pSession = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VERR_NOT_FOUND;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSemFastMutexRelease(pSession->pDevExt->mtxComponentFactory);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMemFree(pCur);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Queries a component factory.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns VBox status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VERR_INVALID_PARAMETER on invalid parameter.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VERR_INVALID_POINTER on invalid pointer parameter.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VERR_SUPDRV_COMPONENT_NOT_FOUND if the component factory wasn't found.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @retval VERR_SUPDRV_INTERFACE_NOT_SUPPORTED if the interface wasn't supported.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession The SUPDRV session.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pszName The name of the component factory.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pszInterfaceUuid The UUID of the factory interface (stringified).
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param ppvFactoryIf Where to store the factory interface.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoSUPR0DECL(int) SUPR0ComponentQueryFactory(PSUPDRVSESSION pSession, const char *pszName, const char *pszInterfaceUuid, void **ppvFactoryIf)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico const char *pszEnd;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico size_t cchName;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Validate parameters.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertPtrReturn(pszName, VERR_INVALID_POINTER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pszEnd = RTStrEnd(pszName, RT_SIZEOFMEMB(SUPDRVFACTORY, szName));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(pszEnd, VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico cchName = pszEnd - pszName;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertPtrReturn(pszInterfaceUuid, VERR_INVALID_POINTER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pszEnd = RTStrEnd(pszInterfaceUuid, RTUUID_STR_LENGTH);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(pszEnd, VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertPtrReturn(ppvFactoryIf, VERR_INVALID_POINTER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *ppvFactoryIf = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Take the lock and try all factories by this name.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTSemFastMutexRequest(pSession->pDevExt->mtxComponentFactory);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVFACTORYREG pCur = pSession->pDevExt->pComponentFactoryHead;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VERR_SUPDRV_COMPONENT_NOT_FOUND;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico while (pCur)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( pCur->cchName == cchName
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && !memcmp(pCur->pFactory->szName, pszName, cchName))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico void *pvFactory = pCur->pFactory->pfnQueryFactoryInterface(pCur->pFactory, pSession, pszInterfaceUuid);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pvFactory)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *ppvFactoryIf = pvFactory;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VERR_SUPDRV_INTERFACE_NOT_SUPPORTED;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* next */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pCur = pCur->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSemFastMutexRelease(pSession->pDevExt->mtxComponentFactory);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Adds a memory object to the session.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pMem Memory tracking structure containing the
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * information to track.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession The session.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvMemAdd(PSUPDRVMEMREF pMem, PSUPDRVSESSION pSession)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVBUNDLE pBundle;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Find free entry and record the allocation.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockAcquire(pSession->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (pBundle = &pSession->Bundle; pBundle; pBundle = pBundle->pNext)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pBundle->cUsed < RT_ELEMENTS(pBundle->aMem))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico unsigned i;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (i = 0; i < RT_ELEMENTS(pBundle->aMem); i++)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pBundle->aMem[i].MemObj == NIL_RTR0MEMOBJ)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pBundle->cUsed++;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pBundle->aMem[i] = *pMem;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockRelease(pSession->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertFailed(); /* !!this can't be happening!!! */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockRelease(pSession->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Need to allocate a new bundle.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Insert into the last entry in the bundle.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pBundle = (PSUPDRVBUNDLE)RTMemAllocZ(sizeof(*pBundle));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!pBundle)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_NO_MEMORY;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* take last entry. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pBundle->cUsed++;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pBundle->aMem[RT_ELEMENTS(pBundle->aMem) - 1] = *pMem;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* insert into list. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockAcquire(pSession->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pBundle->pNext = pSession->Bundle.pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->Bundle.pNext = pBundle;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockRelease(pSession->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Releases a memory object referenced by pointer and type.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession Session data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param uPtr Pointer to memory. This is matched against both the R0 and R3 addresses.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param eType Memory type.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvMemRelease(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr, SUPDRVMEMREFTYPE eType)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVBUNDLE pBundle;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Validate input.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!uPtr)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("Illegal address %p\n", (void *)uPtr));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Search for the address.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockAcquire(pSession->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (pBundle = &pSession->Bundle; pBundle; pBundle = pBundle->pNext)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pBundle->cUsed > 0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico unsigned i;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (i = 0; i < RT_ELEMENTS(pBundle->aMem); i++)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( pBundle->aMem[i].eType == eType
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && pBundle->aMem[i].MemObj != NIL_RTR0MEMOBJ
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && ( (RTHCUINTPTR)RTR0MemObjAddress(pBundle->aMem[i].MemObj) == uPtr
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || ( pBundle->aMem[i].MapObjR3 != NIL_RTR0MEMOBJ
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && RTR0MemObjAddressR3(pBundle->aMem[i].MapObjR3) == uPtr))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico )
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Make a copy of it and release it outside the spinlock. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico SUPDRVMEMREF Mem = pBundle->aMem[i];
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pBundle->aMem[i].eType = MEMREF_TYPE_UNUSED;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pBundle->aMem[i].MemObj = NIL_RTR0MEMOBJ;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pBundle->aMem[i].MapObjR3 = NIL_RTR0MEMOBJ;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockRelease(pSession->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (Mem.MapObjR3 != NIL_RTR0MEMOBJ)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc = RTR0MemObjFree(Mem.MapObjR3, false);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertRC(rc); /** @todo figure out how to handle this. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (Mem.MemObj != NIL_RTR0MEMOBJ)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc = RTR0MemObjFree(Mem.MemObj, true /* fFreeMappings */);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertRC(rc); /** @todo figure out how to handle this. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockRelease(pSession->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("Failed to find %p!!! (eType=%d)\n", (void *)uPtr, eType));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Opens an image. If it's the first time it's opened the call must upload
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * the bits using the supdrvIOCtl_LdrLoad() / SUPDRV_IOCTL_LDR_LOAD function.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * This is the 1st step of the loading.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt Device globals.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession Session data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pReq The open request.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvIOCtl_LdrOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDROPEN pReq)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVLDRIMAGE pImage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico void *pv;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico size_t cchName = strlen(pReq->u.In.szName); /* (caller checked < 32). */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("supdrvIOCtl_LdrOpen: szName=%s cbImageWithTabs=%d\n", pReq->u.In.szName, pReq->u.In.cbImageWithTabs));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Check if we got an instance of the image already.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrLock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (pImage = pDevExt->pLdrImages; pImage; pImage = pImage->pNext)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( pImage->szName[cchName] == '\0'
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && !memcmp(pImage->szName, pReq->u.In.szName, cchName))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /** @todo check cbImageBits and cbImageWithTabs here, if they differs that indicates that the images are different. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->cUsage++;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.pvImageBase = pImage->pvImage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.fNeedsLoading = pImage->uState == SUP_IOCTL_LDR_OPEN;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.fNativeLoader = pImage->fNative;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrAddUsage(pSession, pImage);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrUnlock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* (not found - add it!) */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Allocate memory.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pv = RTMemAlloc(RT_OFFSETOF(SUPDRVLDRIMAGE, szName[cchName + 1]));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!pv)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrUnlock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("supdrvIOCtl_LdrOpen: RTMemAlloc() failed\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return /*VERR_NO_MEMORY*/ VERR_INTERNAL_ERROR_2;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Setup and link in the LDR stuff.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage = (PSUPDRVLDRIMAGE)pv;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->pvImage = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->pvImageAlloc = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->cbImageWithTabs = pReq->u.In.cbImageWithTabs;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->cbImageBits = pReq->u.In.cbImageBits;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->cSymbols = 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->paSymbols = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->pachStrTab = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->cbStrTab = 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->pfnModuleInit = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->pfnModuleTerm = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->pfnServiceReqHandler = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->uState = SUP_IOCTL_LDR_OPEN;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->cUsage = 1;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico memcpy(pImage->szName, pReq->u.In.szName, cchName + 1);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Try load it using the native loader, if that isn't supported, fall back
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * on the older method.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->fNative = true;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = supdrvOSLdrOpen(pDevExt, pImage, pReq->u.In.szFilename);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (rc == VERR_NOT_SUPPORTED)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->pvImageAlloc = RTMemExecAlloc(pImage->cbImageBits + 31);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->pvImage = RT_ALIGN_P(pImage->pvImageAlloc, 32);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->fNative = false;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = pImage->pvImageAlloc ? VINF_SUCCESS : VERR_NO_EXEC_MEMORY;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_FAILURE(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrUnlock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMemFree(pImage);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("supdrvIOCtl_LdrOpen(%s): failed - %Rrc\n", pReq->u.In.szName, rc));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Assert(VALID_PTR(pImage->pvImage) || RT_FAILURE(rc));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Link it.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->pNext = pDevExt->pLdrImages;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pLdrImages = pImage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrAddUsage(pSession, pImage);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.pvImageBase = pImage->pvImage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.fNeedsLoading = true;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.fNativeLoader = pImage->fNative;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrUnlock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#if defined(RT_OS_WINDOWS) && defined(DEBUG)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico SUPR0Printf("VBoxDrv: windbg> .reload /f %s=%#p\n", pImage->szName, pImage->pvImage);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Worker that validates a pointer to an image entrypoint.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt The device globals.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pImage The loader image.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pv The pointer into the image.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param fMayBeNull Whether it may be NULL.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pszWhat What is this entrypoint? (for logging)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pbImageBits The image bits prepared by ring-3.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @remarks Will leave the lock on failure.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvLdrValidatePointer(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, void *pv,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico bool fMayBeNull, const uint8_t *pbImageBits, const char *pszWhat)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!fMayBeNull || pv)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ((uintptr_t)pv - (uintptr_t)pImage->pvImage >= pImage->cbImageBits)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrUnlock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("Out of range (%p LB %#x): %s=%p\n", pImage->pvImage, pImage->cbImageBits, pszWhat, pv));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pImage->fNative)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc = supdrvOSLdrValidatePointer(pDevExt, pImage, pv, pbImageBits);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_FAILURE(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrUnlock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("Bad entry point address: %s=%p (rc=%Rrc)\n", pszWhat, pv, rc));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Loads the image bits.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * This is the 2nd step of the loading.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt Device globals.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession Session data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pReq The request.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvIOCtl_LdrLoad(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDRLOAD pReq)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVLDRUSAGE pUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVLDRIMAGE pImage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("supdrvIOCtl_LdrLoad: pvImageBase=%p cbImageWithBits=%d\n", pReq->u.In.pvImageBase, pReq->u.In.cbImageWithTabs));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Find the ldr image.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrLock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage = pSession->pLdrUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico while (pUsage && pUsage->pImage->pvImage != pReq->u.In.pvImageBase)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage = pUsage->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!pUsage)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrUnlock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("SUP_IOCTL_LDR_LOAD: couldn't find image!\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_INVALID_HANDLE;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage = pUsage->pImage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Validate input.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( pImage->cbImageWithTabs != pReq->u.In.cbImageWithTabs
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || pImage->cbImageBits != pReq->u.In.cbImageBits)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrUnlock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("SUP_IOCTL_LDR_LOAD: image size mismatch!! %d(prep) != %d(load) or %d != %d\n",
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->cbImageWithTabs, pReq->u.In.cbImageWithTabs, pImage->cbImageBits, pReq->u.In.cbImageBits));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_INVALID_HANDLE;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pImage->uState != SUP_IOCTL_LDR_OPEN)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico unsigned uState = pImage->uState;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrUnlock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (uState != SUP_IOCTL_LDR_LOAD)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsgFailed(("SUP_IOCTL_LDR_LOAD: invalid image state %d (%#x)!\n", uState, uState));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_ALREADY_LOADED;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico switch (pReq->u.In.eEPType)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPLDRLOADEP_NOTHING:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPLDRLOADEP_VMMR0:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = supdrvLdrValidatePointer( pDevExt, pImage, pReq->u.In.EP.VMMR0.pvVMMR0, false, pReq->u.In.abImage, "pvVMMR0");
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = supdrvLdrValidatePointer(pDevExt, pImage, pReq->u.In.EP.VMMR0.pvVMMR0EntryInt, false, pReq->u.In.abImage, "pvVMMR0EntryInt");
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = supdrvLdrValidatePointer(pDevExt, pImage, pReq->u.In.EP.VMMR0.pvVMMR0EntryFast, false, pReq->u.In.abImage, "pvVMMR0EntryFast");
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = supdrvLdrValidatePointer(pDevExt, pImage, pReq->u.In.EP.VMMR0.pvVMMR0EntryEx, false, pReq->u.In.abImage, "pvVMMR0EntryEx");
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_FAILURE(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPLDRLOADEP_SERVICE:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = supdrvLdrValidatePointer(pDevExt, pImage, pReq->u.In.EP.Service.pfnServiceReq, false, pReq->u.In.abImage, "pfnServiceReq");
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_FAILURE(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( pReq->u.In.EP.Service.apvReserved[0] != NIL_RTR0PTR
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || pReq->u.In.EP.Service.apvReserved[1] != NIL_RTR0PTR
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || pReq->u.In.EP.Service.apvReserved[2] != NIL_RTR0PTR)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrUnlock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("Out of range (%p LB %#x): apvReserved={%p,%p,%p} MBZ!\n",
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->pvImage, pReq->u.In.cbImageWithTabs,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.In.EP.Service.apvReserved[0],
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.In.EP.Service.apvReserved[1],
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.In.EP.Service.apvReserved[2]));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico default:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrUnlock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("Invalid eEPType=%d\n", pReq->u.In.eEPType));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = supdrvLdrValidatePointer(pDevExt, pImage, pReq->u.In.pfnModuleInit, true, pReq->u.In.abImage, "pfnModuleInit");
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_FAILURE(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = supdrvLdrValidatePointer(pDevExt, pImage, pReq->u.In.pfnModuleTerm, true, pReq->u.In.abImage, "pfnModuleTerm");
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_FAILURE(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Allocate and copy the tables.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * (No need to do try/except as this is a buffered request.)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->cbStrTab = pReq->u.In.cbStrTab;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pImage->cbStrTab)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->pachStrTab = (char *)RTMemAlloc(pImage->cbStrTab);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pImage->pachStrTab)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico memcpy(pImage->pachStrTab, &pReq->u.In.abImage[pReq->u.In.offStrTab], pImage->cbStrTab);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = /*VERR_NO_MEMORY*/ VERR_INTERNAL_ERROR_3;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->cSymbols = pReq->u.In.cSymbols;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc) && pImage->cSymbols)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico size_t cbSymbols = pImage->cSymbols * sizeof(SUPLDRSYM);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->paSymbols = (PSUPLDRSYM)RTMemAlloc(cbSymbols);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pImage->paSymbols)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico memcpy(pImage->paSymbols, &pReq->u.In.abImage[pReq->u.In.offSymbols], cbSymbols);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = /*VERR_NO_MEMORY*/ VERR_INTERNAL_ERROR_4;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Copy the bits / complete native loading.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->uState = SUP_IOCTL_LDR_LOAD;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->pfnModuleInit = pReq->u.In.pfnModuleInit;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->pfnModuleTerm = pReq->u.In.pfnModuleTerm;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pImage->fNative)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = supdrvOSLdrLoad(pDevExt, pImage, pReq->u.In.abImage);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico memcpy(pImage->pvImage, &pReq->u.In.abImage[0], pImage->cbImageBits);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Update any entry points.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico switch (pReq->u.In.eEPType)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico default:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPLDRLOADEP_NOTHING:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPLDRLOADEP_VMMR0:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = supdrvLdrSetVMMR0EPs(pDevExt, pReq->u.In.EP.VMMR0.pvVMMR0, pReq->u.In.EP.VMMR0.pvVMMR0EntryInt,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.In.EP.VMMR0.pvVMMR0EntryFast, pReq->u.In.EP.VMMR0.pvVMMR0EntryEx);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPLDRLOADEP_SERVICE:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->pfnServiceReqHandler = pReq->u.In.EP.Service.pfnServiceReq;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * On success call the module initialization.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("supdrvIOCtl_LdrLoad: pfnModuleInit=%p\n", pImage->pfnModuleInit));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc) && pImage->pfnModuleInit)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("supdrvIOCtl_LdrLoad: calling pfnModuleInit=%p\n", pImage->pfnModuleInit));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = pImage->pfnModuleInit();
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (rc && pDevExt->pvVMMR0 == pImage->pvImage)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrUnsetVMMR0EPs(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_FAILURE(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->uState = SUP_IOCTL_LDR_OPEN;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->pfnModuleInit = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->pfnModuleTerm = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->pfnServiceReqHandler= NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->cbStrTab = 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMemFree(pImage->pachStrTab);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->pachStrTab = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMemFree(pImage->paSymbols);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->paSymbols = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->cSymbols = 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrUnlock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Frees a previously loaded (prep'ed) image.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt Device globals.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession Session data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pReq The request.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvIOCtl_LdrFree(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDRFREE pReq)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVLDRUSAGE pUsagePrev;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVLDRUSAGE pUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVLDRIMAGE pImage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("supdrvIOCtl_LdrFree: pvImageBase=%p\n", pReq->u.In.pvImageBase));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Find the ldr image.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrLock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsagePrev = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage = pSession->pLdrUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico while (pUsage && pUsage->pImage->pvImage != pReq->u.In.pvImageBase)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsagePrev = pUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage = pUsage->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!pUsage)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrUnlock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("SUP_IOCTL_LDR_FREE: couldn't find image!\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_INVALID_HANDLE;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Check if we can remove anything.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage = pUsage->pImage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pImage->cUsage <= 1 || pUsage->cUsage <= 1)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Check if there are any objects with destructors in the image, if
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * so leave it for the session cleanup routine so we get a chance to
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * clean things up in the right order and not leave them all dangling.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockAcquire(pDevExt->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pImage->cUsage <= 1)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVOBJ pObj;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (pObj = pDevExt->pObjs; pObj; pObj = pObj->pNext)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_UNLIKELY((uintptr_t)pObj->pfnDestructor - (uintptr_t)pImage->pvImage < pImage->cbImageBits))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VERR_DANGLING_OBJECTS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVUSAGE pGenUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (pGenUsage = pSession->pUsage; pGenUsage; pGenUsage = pGenUsage->pNext)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_UNLIKELY((uintptr_t)pGenUsage->pObj->pfnDestructor - (uintptr_t)pImage->pvImage < pImage->cbImageBits))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VERR_DANGLING_OBJECTS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (rc == VINF_SUCCESS)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* unlink it */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pUsagePrev)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsagePrev->pNext = pUsage->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->pLdrUsage = pUsage->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* free it */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage->pImage = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage->pNext = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMemFree(pUsage);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Dereference the image.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pImage->cUsage <= 1)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrFree(pDevExt, pImage);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->cUsage--;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("supdrvIOCtl_LdrFree: Dangling objects in %p/%s!\n", pImage->pvImage, pImage->szName));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VINF_SUCCESS; /** @todo BRANCH-2.1: remove this after branching. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Dereference both image and usage.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->cUsage--;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage->cUsage--;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrUnlock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Gets the address of a symbol in an open image.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt Device globals.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession Session data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pReq The request buffer.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvIOCtl_LdrGetSymbol(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDRGETSYMBOL pReq)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVLDRIMAGE pImage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVLDRUSAGE pUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint32_t i;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPLDRSYM paSyms;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico const char *pchStrings;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico const size_t cbSymbol = strlen(pReq->u.In.szSymbol) + 1;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico void *pvSymbol = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc = VERR_GENERAL_FAILURE;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log3(("supdrvIOCtl_LdrGetSymbol: pvImageBase=%p szSymbol=\"%s\"\n", pReq->u.In.pvImageBase, pReq->u.In.szSymbol));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Find the ldr image.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrLock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage = pSession->pLdrUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico while (pUsage && pUsage->pImage->pvImage != pReq->u.In.pvImageBase)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage = pUsage->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!pUsage)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrUnlock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("SUP_IOCTL_LDR_GET_SYMBOL: couldn't find image!\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_INVALID_HANDLE;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage = pUsage->pImage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pImage->uState != SUP_IOCTL_LDR_LOAD)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico unsigned uState = pImage->uState;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrUnlock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("SUP_IOCTL_LDR_GET_SYMBOL: invalid image state %d (%#x)!\n", uState, uState)); NOREF(uState);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_ALREADY_LOADED;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Search the symbol strings.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pchStrings = pImage->pachStrTab;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico paSyms = pImage->paSymbols;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (i = 0; i < pImage->cSymbols; i++)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( paSyms[i].offSymbol < pImage->cbImageBits /* paranoia */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && paSyms[i].offName + cbSymbol <= pImage->cbStrTab
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && !memcmp(pchStrings + paSyms[i].offName, pReq->u.In.szSymbol, cbSymbol))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pvSymbol = (uint8_t *)pImage->pvImage + paSyms[i].offSymbol;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrUnlock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.pvSymbol = pvSymbol;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Gets the address of a symbol in an open image or the support driver.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns VINF_SUCCESS on success.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt Device globals.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession Session data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pReq The request buffer.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvIDC_LdrGetSymbol(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVIDCREQGETSYM pReq)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico const char *pszSymbol = pReq->u.In.pszSymbol;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico const char *pszModule = pReq->u.In.pszModule;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico size_t cbSymbol;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico char const *pszEnd;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint32_t i;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Input validation.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertPtrReturn(pszSymbol, VERR_INVALID_POINTER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pszEnd = RTStrEnd(pszSymbol, 512);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(pszEnd, VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico cbSymbol = pszEnd - pszSymbol + 1;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pszModule)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertPtrReturn(pszModule, VERR_INVALID_POINTER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pszEnd = RTStrEnd(pszModule, 64);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(pszEnd, VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log3(("supdrvIDC_LdrGetSymbol: pszModule=%p:{%s} pszSymbol=%p:{%s}\n", pszModule, pszModule, pszSymbol, pszSymbol));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( !pszModule
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || !strcmp(pszModule, "SupDrv"))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Search the support driver export table.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (i = 0; i < RT_ELEMENTS(g_aFunctions); i++)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!strcmp(g_aFunctions[i].szName, pszSymbol))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.pfnSymbol = g_aFunctions[i].pfn;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Find the loader image.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVLDRIMAGE pImage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrLock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (pImage = pDevExt->pLdrImages; pImage; pImage = pImage->pNext)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!strcmp(pImage->szName, pszModule))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pImage && pImage->uState == SUP_IOCTL_LDR_LOAD)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Search the symbol strings.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico const char *pchStrings = pImage->pachStrTab;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PCSUPLDRSYM paSyms = pImage->paSymbols;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (i = 0; i < pImage->cSymbols; i++)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( paSyms[i].offSymbol < pImage->cbImageBits /* paranoia */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && paSyms[i].offName + cbSymbol <= pImage->cbStrTab
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && !memcmp(pchStrings + paSyms[i].offName, pszSymbol, cbSymbol))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Found it! Calc the symbol address and add a reference to the module.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.Out.pfnSymbol = (PFNRT)((uint8_t *)pImage->pvImage + paSyms[i].offSymbol);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = supdrvLdrAddUsage(pSession, pImage);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = pImage ? VERR_WRONG_ORDER : VERR_MODULE_NOT_FOUND;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrUnlock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Updates the VMMR0 entry point pointers.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt Device globals.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession Session data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pVMMR0 VMMR0 image handle.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvVMMR0EntryInt VMMR0EntryInt address.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvVMMR0EntryFast VMMR0EntryFast address.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvVMMR0EntryEx VMMR0EntryEx address.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @remark Caller must own the loader mutex.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvLdrSetVMMR0EPs(PSUPDRVDEVEXT pDevExt, void *pvVMMR0, void *pvVMMR0EntryInt, void *pvVMMR0EntryFast, void *pvVMMR0EntryEx)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("supdrvLdrSetR0EP pvVMMR0=%p pvVMMR0EntryInt=%p\n", pvVMMR0, pvVMMR0EntryInt));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Check if not yet set.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!pDevExt->pvVMMR0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pvVMMR0 = pvVMMR0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pfnVMMR0EntryInt = pvVMMR0EntryInt;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pfnVMMR0EntryFast = pvVMMR0EntryFast;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pfnVMMR0EntryEx = pvVMMR0EntryEx;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Return failure or success depending on whether the values match or not.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( pDevExt->pvVMMR0 != pvVMMR0
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || (void *)pDevExt->pfnVMMR0EntryInt != pvVMMR0EntryInt
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || (void *)pDevExt->pfnVMMR0EntryFast != pvVMMR0EntryFast
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || (void *)pDevExt->pfnVMMR0EntryEx != pvVMMR0EntryEx)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsgFailed(("SUP_IOCTL_LDR_SETR0EP: Already set pointing to a different module!\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Unsets the VMMR0 entry point installed by supdrvLdrSetR0EP.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt Device globals.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic void supdrvLdrUnsetVMMR0EPs(PSUPDRVDEVEXT pDevExt)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pvVMMR0 = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pfnVMMR0EntryInt = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pfnVMMR0EntryFast = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pfnVMMR0EntryEx = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Adds a usage reference in the specified session of an image.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Called while owning the loader semaphore.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns VINF_SUCCESS on success and VERR_NO_MEMORY on failure.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession Session in question.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pImage Image which the session is using.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvLdrAddUsage(PSUPDRVSESSION pSession, PSUPDRVLDRIMAGE pImage)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVLDRUSAGE pUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("supdrvLdrAddUsage: pImage=%p\n", pImage));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Referenced it already?
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage = pSession->pLdrUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico while (pUsage)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pUsage->pImage == pImage)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage->cUsage++;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage = pUsage->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Allocate new usage record.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage = (PSUPDRVLDRUSAGE)RTMemAlloc(sizeof(*pUsage));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(pUsage, /*VERR_NO_MEMORY*/ VERR_INTERNAL_ERROR_5);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage->cUsage = 1;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage->pImage = pImage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pUsage->pNext = pSession->pLdrUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pSession->pLdrUsage = pUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Frees a load image.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt Pointer to device extension.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pImage Pointer to the image we're gonna free.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * This image must exit!
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @remark The caller MUST own SUPDRVDEVEXT::mtxLdr!
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic void supdrvLdrFree(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVLDRIMAGE pImagePrev;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("supdrvLdrFree: pImage=%p\n", pImage));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* find it - arg. should've used doubly linked list. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Assert(pDevExt->pLdrImages);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImagePrev = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pDevExt->pLdrImages != pImage)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImagePrev = pDevExt->pLdrImages;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico while (pImagePrev->pNext != pImage)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImagePrev = pImagePrev->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Assert(pImagePrev->pNext == pImage);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* unlink */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pImagePrev)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImagePrev->pNext = pImage->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pLdrImages = pImage->pNext;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* check if this is VMMR0.r0 unset its entry point pointers. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pDevExt->pvVMMR0 == pImage->pvImage)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrUnsetVMMR0EPs(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* check for objects with destructors in this image. (Shouldn't happen.) */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pDevExt->pObjs)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico unsigned cObjs = 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVOBJ pObj;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockAcquire(pDevExt->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (pObj = pDevExt->pObjs; pObj; pObj = pObj->pNext)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_UNLIKELY((uintptr_t)pObj->pfnDestructor - (uintptr_t)pImage->pvImage < pImage->cbImageBits))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pObj->pfnDestructor = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico cObjs++;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (cObjs)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(("supdrvLdrFree: Image '%s' has %d dangling objects!\n", pImage->szName, cObjs));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* call termination function if fully loaded. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( pImage->pfnModuleTerm
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && pImage->uState == SUP_IOCTL_LDR_LOAD)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("supdrvIOCtl_LdrLoad: calling pfnModuleTerm=%p\n", pImage->pfnModuleTerm));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->pfnModuleTerm();
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* do native unload if appropriate. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pImage->fNative)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvOSLdrUnload(pDevExt, pImage);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* free the image */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->cUsage = 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->pNext = 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->uState = SUP_IOCTL_LDR_FREE;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMemExecFree(pImage->pvImageAlloc, pImage->cbImageBits + 31);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->pvImageAlloc = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMemFree(pImage->pachStrTab);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->pachStrTab = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMemFree(pImage->paSymbols);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pImage->paSymbols = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMemFree(pImage);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Acquires the loader lock.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt The device extension.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoDECLINLINE(int) supdrvLdrLock(PSUPDRVDEVEXT pDevExt)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef SUPDRV_USE_MUTEX_FOR_LDR
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc = RTSemMutexRequest(pDevExt->mtxLdr, RT_INDEFINITE_WAIT);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc = RTSemFastMutexRequest(pDevExt->mtxLdr);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertRC(rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Releases the loader lock.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns IPRT status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt The device extension.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoDECLINLINE(int) supdrvLdrUnlock(PSUPDRVDEVEXT pDevExt)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef SUPDRV_USE_MUTEX_FOR_LDR
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return RTSemMutexRelease(pDevExt->mtxLdr);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return RTSemFastMutexRelease(pDevExt->mtxLdr);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Implements the service call request.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns VBox status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt The device extension.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession The calling session.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pReq The request packet, valid.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvIOCtl_CallServiceModule(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPCALLSERVICE pReq)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#if !defined(RT_OS_WINDOWS) || defined(DEBUG)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Find the module first in the module referenced by the calling session.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = supdrvLdrLock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PFNSUPR0SERVICEREQHANDLER pfnServiceReqHandler = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVLDRUSAGE pUsage;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (pUsage = pSession->pLdrUsage; pUsage; pUsage = pUsage->pNext)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( pUsage->pImage->pfnServiceReqHandler
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && !strcmp(pUsage->pImage->szName, pReq->u.In.szName))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pfnServiceReqHandler = pUsage->pImage->pfnServiceReqHandler;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvLdrUnlock(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pfnServiceReqHandler)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Call it.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pReq->Hdr.cbIn == SUP_IOCTL_CALL_SERVICE_SIZE(0))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = pfnServiceReqHandler(pSession, pReq->u.In.uOperation, pReq->u.In.u64Arg, NULL);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = pfnServiceReqHandler(pSession, pReq->u.In.uOperation, pReq->u.In.u64Arg, (PSUPR0SERVICEREQHDR)&pReq->abReqPkt[0]);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VERR_SUPDRV_SERVICE_NOT_FOUND;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* log it */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( RT_FAILURE(rc)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && rc != VERR_INTERRUPTED
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && rc != VERR_TIMEOUT)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("SUP_IOCTL_CALL_SERVICE: rc=%Rrc op=%u out=%u arg=%RX64 p/t=%RTproc/%RTthrd\n",
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc, pReq->u.In.uOperation, pReq->Hdr.cbOut, pReq->u.In.u64Arg, RTProcSelf(), RTThreadNativeSelf()));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log4(("SUP_IOCTL_CALL_SERVICE: rc=%Rrc op=%u out=%u arg=%RX64 p/t=%RTproc/%RTthrd\n",
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc, pReq->u.In.uOperation, pReq->Hdr.cbOut, pReq->u.In.u64Arg, RTProcSelf(), RTThreadNativeSelf()));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#else /* RT_OS_WINDOWS && !DEBUG */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_NOT_IMPLEMENTED;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif /* RT_OS_WINDOWS && !DEBUG */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Implements the logger settings request.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns VBox status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt The device extension.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pSession The caller's session.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pReq The request.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvIOCtl_LoggerSettings(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLOGGERSETTINGS pReq)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico const char *pszGroup = &pReq->u.In.szStrings[pReq->u.In.offGroups];
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico const char *pszFlags = &pReq->u.In.szStrings[pReq->u.In.offFlags];
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico const char *pszDest = &pReq->u.In.szStrings[pReq->u.In.offDestination];
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PRTLOGGER pLogger = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Some further validation.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico switch (pReq->u.In.fWhat)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPLOGGERSETTINGS_WHAT_SETTINGS:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPLOGGERSETTINGS_WHAT_CREATE:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPLOGGERSETTINGS_WHAT_DESTROY:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (*pszGroup || *pszFlags || *pszDest)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_INVALID_PARAMETER;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pReq->u.In.fWhich == SUPLOGGERSETTINGS_WHICH_RELEASE)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_ACCESS_DENIED;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico default:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_INTERNAL_ERROR;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Get the logger.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico switch (pReq->u.In.fWhich)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPLOGGERSETTINGS_WHICH_DEBUG:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pLogger = RTLogGetDefaultInstance();
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPLOGGERSETTINGS_WHICH_RELEASE:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pLogger = RTLogRelDefaultInstance();
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico default:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_INTERNAL_ERROR;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Do the job.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico switch (pReq->u.In.fWhat)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPLOGGERSETTINGS_WHAT_SETTINGS:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pLogger)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTLogFlags(pLogger, pszFlags);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTLogGroupSettings(pLogger, pszGroup);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico NOREF(pszDest);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VERR_NOT_FOUND;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPLOGGERSETTINGS_WHAT_CREATE:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pLogger)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VERR_ALREADY_EXISTS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTLogCreate(&pLogger,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico 0 /* fFlags */,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pszGroup,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pReq->u.In.fWhich == SUPLOGGERSETTINGS_WHICH_DEBUG
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ? "VBOX_LOG"
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico : "VBOX_RELEASE_LOG",
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RT_ELEMENTS(s_apszGroups),
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico s_apszGroups,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTLOGDEST_STDOUT | RTLOGDEST_DEBUGGER,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico NULL);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTLogFlags(pLogger, pszFlags);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico NOREF(pszDest);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico switch (pReq->u.In.fWhich)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPLOGGERSETTINGS_WHICH_DEBUG:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pLogger = RTLogSetDefaultInstance(pLogger);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPLOGGERSETTINGS_WHICH_RELEASE:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pLogger = RTLogRelSetDefaultInstance(pLogger);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTLogDestroy(pLogger);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPLOGGERSETTINGS_WHAT_DESTROY:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico switch (pReq->u.In.fWhich)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPLOGGERSETTINGS_WHICH_DEBUG:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pLogger = RTLogSetDefaultInstance(NULL);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico case SUPLOGGERSETTINGS_WHICH_RELEASE:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pLogger = RTLogRelSetDefaultInstance(NULL);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTLogDestroy(pLogger);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico default:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = VERR_INTERNAL_ERROR;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Creates the GIP.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns VBox status code.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt Instance data. GIP stuff may be updated.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic int supdrvGipCreate(PSUPDRVDEVEXT pDevExt)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPGLOBALINFOPAGE pGip;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTHCPHYS HCPhysGip;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint32_t u32SystemResolution;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint32_t u32Interval;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("supdrvGipCreate:\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* assert order */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Assert(pDevExt->u32SystemTimerGranularityGrant == 0);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Assert(pDevExt->GipMemObj == NIL_RTR0MEMOBJ);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Assert(!pDevExt->pGipTimer);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Allocate a suitable page with a default kernel mapping.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTR0MemObjAllocLow(&pDevExt->GipMemObj, PAGE_SIZE, false);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_FAILURE(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(("supdrvGipCreate: failed to allocate the GIP page. rc=%d\n", rc));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGip = (PSUPGLOBALINFOPAGE)RTR0MemObjAddress(pDevExt->GipMemObj); AssertPtr(pGip);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico HCPhysGip = RTR0MemObjGetPagePhysAddr(pDevExt->GipMemObj, 0); Assert(HCPhysGip != NIL_RTHCPHYS);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#if 0 /** @todo Disabled this as we didn't used to do it before and causes unnecessary stress on laptops.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * It only applies to Windows and should probably revisited later, if possible made part of the
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * timer code (return min granularity in RTTimerGetSystemGranularity and set it in RTTimerStart). */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Try bump up the system timer resolution.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * The more interrupts the better...
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( RT_SUCCESS(RTTimerRequestSystemGranularity( 488281 /* 2048 HZ */, &u32SystemResolution))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || RT_SUCCESS(RTTimerRequestSystemGranularity( 500000 /* 2000 HZ */, &u32SystemResolution))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || RT_SUCCESS(RTTimerRequestSystemGranularity( 976563 /* 1024 HZ */, &u32SystemResolution))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || RT_SUCCESS(RTTimerRequestSystemGranularity( 1000000 /* 1000 HZ */, &u32SystemResolution))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || RT_SUCCESS(RTTimerRequestSystemGranularity( 1953125 /* 512 HZ */, &u32SystemResolution))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || RT_SUCCESS(RTTimerRequestSystemGranularity( 2000000 /* 500 HZ */, &u32SystemResolution))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || RT_SUCCESS(RTTimerRequestSystemGranularity( 3906250 /* 256 HZ */, &u32SystemResolution))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || RT_SUCCESS(RTTimerRequestSystemGranularity( 4000000 /* 250 HZ */, &u32SystemResolution))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || RT_SUCCESS(RTTimerRequestSystemGranularity( 7812500 /* 128 HZ */, &u32SystemResolution))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || RT_SUCCESS(RTTimerRequestSystemGranularity(10000000 /* 100 HZ */, &u32SystemResolution))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || RT_SUCCESS(RTTimerRequestSystemGranularity(15625000 /* 64 HZ */, &u32SystemResolution))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || RT_SUCCESS(RTTimerRequestSystemGranularity(31250000 /* 32 HZ */, &u32SystemResolution))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico )
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Assert(RTTimerGetSystemGranularity() <= u32SystemResolution);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->u32SystemTimerGranularityGrant = u32SystemResolution;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Find a reasonable update interval and initialize the structure.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u32Interval = u32SystemResolution = RTTimerGetSystemGranularity();
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico while (u32Interval < 10000000 /* 10 ms */)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u32Interval += u32SystemResolution;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvGipInit(pDevExt, pGip, HCPhysGip, RTTimeSystemNanoTS(), 1000000000 / u32Interval /*=Hz*/);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Create the timer.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * If CPU_ALL isn't supported we'll have to fall back to synchronous mode.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pGip->u32Mode == SUPGIPMODE_ASYNC_TSC)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTTimerCreateEx(&pDevExt->pGipTimer, u32Interval, RTTIMER_FLAGS_CPU_ALL, supdrvGipAsyncTimer, pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (rc == VERR_NOT_SUPPORTED)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(("supdrvGipCreate: omni timer not supported, falling back to synchronous mode\n"));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGip->u32Mode = SUPGIPMODE_SYNC_TSC;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pGip->u32Mode != SUPGIPMODE_ASYNC_TSC)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTTimerCreateEx(&pDevExt->pGipTimer, u32Interval, 0, supdrvGipSyncTimer, pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pGip->u32Mode == SUPGIPMODE_ASYNC_TSC)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTMpNotificationRegister(supdrvGipMpEvent, pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * We're good.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("supdrvGipCreate: %ld ns interval.\n", (long)u32Interval));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_pSUPGlobalInfoPage = pGip;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(("supdrvGipCreate: failed register MP event notfication. rc=%d\n", rc));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(("supdrvGipCreate: failed create GIP timer at %ld ns interval. rc=%d\n", (long)u32Interval, rc));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Assert(!pDevExt->pGipTimer);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvGipDestroy(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Terminates the GIP.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt Instance data. GIP stuff may be updated.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic void supdrvGipDestroy(PSUPDRVDEVEXT pDevExt)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef DEBUG_DARWIN_GIP
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(("supdrvGipDestroy: pDevExt=%p pGip=%p pGipTimer=%p GipMemObj=%p\n", pDevExt,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->GipMemObj != NIL_RTR0MEMOBJ ? RTR0MemObjAddress(pDevExt->GipMemObj) : NULL,
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pGipTimer, pDevExt->GipMemObj));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Invalid the GIP data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pDevExt->pGip)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvGipTerm(pDevExt->pGip);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pGip = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico g_pSUPGlobalInfoPage = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Destroy the timer and free the GIP memory object.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pDevExt->pGipTimer)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTTimerDestroy(pDevExt->pGipTimer); AssertRC(rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pGipTimer = NULL;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pDevExt->GipMemObj != NIL_RTR0MEMOBJ)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTR0MemObjFree(pDevExt->GipMemObj, true /* free mappings */); AssertRC(rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->GipMemObj = NIL_RTR0MEMOBJ;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Finally, release the system timer resolution request if one succeeded.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pDevExt->u32SystemTimerGranularityGrant)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTTimerReleaseSystemGranularity(pDevExt->u32SystemTimerGranularityGrant); AssertRC(rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->u32SystemTimerGranularityGrant = 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Timer callback function sync GIP mode.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pTimer The timer.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvUser The device extension.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic DECLCALLBACK(void) supdrvGipSyncTimer(PRTTIMER pTimer, void *pvUser, uint64_t iTick)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTCCUINTREG fOldFlags = ASMIntDisableFlags(); /* No interruptions please (real problem on S10). */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pvUser;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint64_t u64TSC = ASMReadTSC();
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint64_t NanoTS = RTTimeSystemNanoTS();
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvGipUpdate(pDevExt->pGip, NanoTS, u64TSC, iTick);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMSetFlags(fOldFlags);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Timer callback function for async GIP mode.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pTimer The timer.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvUser The device extension.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic DECLCALLBACK(void) supdrvGipAsyncTimer(PRTTIMER pTimer, void *pvUser, uint64_t iTick)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTCCUINTREG fOldFlags = ASMIntDisableFlags(); /* No interruptions please (real problem on S10). */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pvUser;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTCPUID idCpu = RTMpCpuId();
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint64_t u64TSC = ASMReadTSC();
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint64_t NanoTS = RTTimeSystemNanoTS();
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /** @todo reset the transaction number and whatnot when iTick == 1. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pDevExt->idGipMaster == idCpu)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvGipUpdate(pDevExt->pGip, NanoTS, u64TSC, iTick);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvGipUpdatePerCpu(pDevExt->pGip, NanoTS, u64TSC, ASMGetApicId(), iTick);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMSetFlags(fOldFlags);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Multiprocessor event notification callback.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * This is used to make sue that the GIP master gets passed on to
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * another CPU.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param enmEvent The event.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param idCpu The cpu it applies to.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvUser Pointer to the device extension.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic DECLCALLBACK(void) supdrvGipMpEvent(RTMPEVENT enmEvent, RTCPUID idCpu, void *pvUser)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pvUser;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (enmEvent == RTMPEVENT_OFFLINE)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTCPUID idGipMaster;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMAtomicReadSize(&pDevExt->idGipMaster, &idGipMaster);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (idGipMaster == idCpu)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Find a new GIP master.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico bool fIgnored;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico unsigned i;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTCPUID idNewGipMaster = NIL_RTCPUID;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTCPUSET OnlineCpus;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTMpGetOnlineSet(&OnlineCpus);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (i = 0; i < RTCPUSET_MAX_CPUS; i++)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico RTCPUID idCurCpu = RTMpCpuIdFromSetIndex(i);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( RTCpuSetIsMember(&OnlineCpus, idCurCpu)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && idCurCpu != idGipMaster)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico idNewGipMaster = idCurCpu;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("supdrvGipMpEvent: Gip master %#lx -> %#lx\n", (long)idGipMaster, (long)idNewGipMaster));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMAtomicCmpXchgSize(&pDevExt->idGipMaster, idNewGipMaster, idGipMaster, fIgnored);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico NOREF(fIgnored);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Callback used by supdrvDetermineAsyncTSC to read the TSC on a CPU.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param idCpu Ignored.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvUser1 Where to put the TSC.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pvUser2 Ignored.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic DECLCALLBACK(void) supdrvDetermineAsyncTscWorker(RTCPUID idCpu, void *pvUser1, void *pvUser2)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#if 1
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMAtomicWriteU64((uint64_t volatile *)pvUser1, ASMReadTSC());
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *(uint64_t *)pvUser1 = ASMReadTSC();
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Determine if Async GIP mode is required because of TSC drift.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * When using the default/normal timer code it is essential that the time stamp counter
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * (TSC) runs never backwards, that is, a read operation to the counter should return
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * a bigger value than any previous read operation. This is guaranteed by the latest
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * AMD CPUs and by newer Intel CPUs which never enter the C2 state (P4). In any other
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * case we have to choose the asynchronous timer mode.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param poffMin Pointer to the determined difference between different cores.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @return false if the time stamp counters appear to be synchron, true otherwise.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic bool supdrvDetermineAsyncTsc(uint64_t *poffMin)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Just iterate all the cpus 8 times and make sure that the TSC is
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * ever increasing. We don't bother taking TSC rollover into account.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int iEndCpu = RTMpGetArraySize();
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int iCpu;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int cLoops = 8;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico bool fAsync = false;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc = VINF_SUCCESS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint64_t offMax = 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint64_t offMin = ~(uint64_t)0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint64_t PrevTsc = ASMReadTSC();
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico while (cLoops-- > 0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (iCpu = 0; iCpu < iEndCpu; iCpu++)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint64_t CurTsc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = RTMpOnSpecific(RTMpCpuIdFromSetIndex(iCpu), supdrvDetermineAsyncTscWorker, &CurTsc, NULL);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_SUCCESS(rc))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (CurTsc <= PrevTsc)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico fAsync = true;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico offMin = offMax = PrevTsc - CurTsc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("supdrvDetermineAsyncTsc: iCpu=%d cLoops=%d CurTsc=%llx PrevTsc=%llx\n",
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico iCpu, cLoops, CurTsc, PrevTsc));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Gather statistics (except the first time). */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (iCpu != 0 || cLoops != 7)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint64_t off = CurTsc - PrevTsc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (off < offMin)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico offMin = off;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (off > offMax)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico offMax = off;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log2(("%d/%d: off=%llx\n", cLoops, iCpu, off));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Next */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PrevTsc = CurTsc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else if (rc == VERR_NOT_SUPPORTED)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsg(rc == VERR_CPU_NOT_FOUND || rc == VERR_CPU_OFFLINE, ("%d\n", rc));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* broke out of the loop. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (iCpu < iEndCpu)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico break;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *poffMin = offMin; /* Almost RTMpOnSpecific profiling. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Log(("supdrvDetermineAsyncTsc: returns %d; iEndCpu=%d rc=%d offMin=%llx offMax=%llx\n",
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico fAsync, iEndCpu, rc, offMin, offMax));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#if !defined(RT_OS_SOLARIS) && !defined(RT_OS_OS2) && !defined(RT_OS_WINDOWS)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(("vboxdrv: fAsync=%d offMin=%#lx offMax=%#lx\n", fAsync, (long)offMin, (long)offMax));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return fAsync;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Determin the GIP TSC mode.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @returns The most suitable TSC mode.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt Pointer to the device instance data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic SUPGIPMODE supdrvGipDeterminTscMode(PSUPDRVDEVEXT pDevExt)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * On SMP we're faced with two problems:
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * (1) There might be a skew between the CPU, so that cpu0
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * returns a TSC that is sligtly different from cpu1.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * (2) Power management (and other things) may cause the TSC
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * to run at a non-constant speed, and cause the speed
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * to be different on the cpus. This will result in (1).
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * So, on SMP systems we'll have to select the ASYNC update method
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * if there are symphoms of these problems.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RTMpGetCount() > 1)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint32_t uEAX, uEBX, uECX, uEDX;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint64_t u64DiffCoresIgnored;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Permit the user and/or the OS specfic bits to force async mode. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (supdrvOSGetForcedAsyncTscMode(pDevExt))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return SUPGIPMODE_ASYNC_TSC;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Try check for current differences between the cpus. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (supdrvDetermineAsyncTsc(&u64DiffCoresIgnored))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return SUPGIPMODE_ASYNC_TSC;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * If the CPU supports power management and is an AMD one we
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * won't trust it unless it has the TscInvariant bit is set.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Check for "AuthenticAMD" */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMCpuId(0, &uEAX, &uEBX, &uECX, &uEDX);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( uEAX >= 1
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && uEBX == X86_CPUID_VENDOR_AMD_EBX
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && uECX == X86_CPUID_VENDOR_AMD_ECX
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && uEDX == X86_CPUID_VENDOR_AMD_EDX)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Check for APM support and that TscInvariant is cleared. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMCpuId(0x80000000, &uEAX, &uEBX, &uECX, &uEDX);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (uEAX >= 0x80000007)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMCpuId(0x80000007, &uEAX, &uEBX, &uECX, &uEDX);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if ( !(uEDX & RT_BIT(8))/* TscInvariant */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && (uEDX & 0x3e)) /* STC|TM|THERMTRIP|VID|FID. Ignore TS. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return SUPGIPMODE_ASYNC_TSC;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return SUPGIPMODE_SYNC_TSC;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Initializes the GIP data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pDevExt Pointer to the device instance data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pGip Pointer to the read-write kernel mapping of the GIP.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param HCPhys The physical address of the GIP.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param u64NanoTS The current nanosecond timestamp.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param uUpdateHz The update freqence.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic void supdrvGipInit(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE pGip, RTHCPHYS HCPhys, uint64_t u64NanoTS, unsigned uUpdateHz)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico unsigned i;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef DEBUG_DARWIN_GIP
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico OSDBGPRINT(("supdrvGipInit: pGip=%p HCPhys=%lx u64NanoTS=%llu uUpdateHz=%d\n", pGip, (long)HCPhys, u64NanoTS, uUpdateHz));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico LogFlow(("supdrvGipInit: pGip=%p HCPhys=%lx u64NanoTS=%llu uUpdateHz=%d\n", pGip, (long)HCPhys, u64NanoTS, uUpdateHz));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Initialize the structure.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico memset(pGip, 0, PAGE_SIZE);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGip->u32Magic = SUPGLOBALINFOPAGE_MAGIC;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGip->u32Version = SUPGLOBALINFOPAGE_VERSION;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGip->u32Mode = supdrvGipDeterminTscMode(pDevExt);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGip->u32UpdateHz = uUpdateHz;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGip->u32UpdateIntervalNS = 1000000000 / uUpdateHz;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGip->u64NanoTSLastUpdateHz = u64NanoTS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (i = 0; i < RT_ELEMENTS(pGip->aCPUs); i++)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGip->aCPUs[i].u32TransactionId = 2;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGip->aCPUs[i].u64NanoTS = u64NanoTS;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGip->aCPUs[i].u64TSC = ASMReadTSC();
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * We don't know the following values until we've executed updates.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * So, we'll just pretend it's a 4 GHz CPU and adjust the history it on
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * the 2nd timer callout.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGip->aCPUs[i].u64CpuHz = _4G + 1; /* tstGIP-2 depends on this. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGip->aCPUs[i].u32UpdateIntervalTSC
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico = pGip->aCPUs[i].au32TSCHistory[0]
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico = pGip->aCPUs[i].au32TSCHistory[1]
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico = pGip->aCPUs[i].au32TSCHistory[2]
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico = pGip->aCPUs[i].au32TSCHistory[3]
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico = pGip->aCPUs[i].au32TSCHistory[4]
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico = pGip->aCPUs[i].au32TSCHistory[5]
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico = pGip->aCPUs[i].au32TSCHistory[6]
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico = pGip->aCPUs[i].au32TSCHistory[7]
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico = /*pGip->aCPUs[i].u64CpuHz*/ _4G / uUpdateHz;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Link it to the device extension.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->pGip = pGip;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->HCPhysGip = HCPhys;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pDevExt->cGipUsers = 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Invalidates the GIP data upon termination.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pGip Pointer to the read-write kernel mapping of the GIP.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic void supdrvGipTerm(PSUPGLOBALINFOPAGE pGip)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico unsigned i;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGip->u32Magic = 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (i = 0; i < RT_ELEMENTS(pGip->aCPUs); i++)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGip->aCPUs[i].u64NanoTS = 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGip->aCPUs[i].u64TSC = 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGip->aCPUs[i].iTSCHistoryHead = 0;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Worker routine for supdrvGipUpdate and supdrvGipUpdatePerCpu that
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * updates all the per cpu data except the transaction id.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pGip The GIP.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pGipCpu Pointer to the per cpu data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param u64NanoTS The current time stamp.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param u64TSC The current TSC.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param iTick The current timer tick.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic void supdrvGipDoUpdateCpu(PSUPGLOBALINFOPAGE pGip, PSUPGIPCPU pGipCpu, uint64_t u64NanoTS, uint64_t u64TSC, uint64_t iTick)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint64_t u64TSCDelta;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint32_t u32UpdateIntervalTSC;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint32_t u32UpdateIntervalTSCSlack;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico unsigned iTSCHistoryHead;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint64_t u64CpuHz;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint32_t u32TransactionId;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Delta between this and the previous update. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMAtomicUoWriteU32(&pGipCpu->u32PrevUpdateIntervalNS, (uint32_t)(u64NanoTS - pGipCpu->u64NanoTS));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Update the NanoTS.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMAtomicXchgU64(&pGipCpu->u64NanoTS, u64NanoTS);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Calc TSC delta.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /** @todo validate the NanoTS delta, don't trust the OS to call us when it should... */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u64TSCDelta = u64TSC - pGipCpu->u64TSC;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMAtomicXchgU64(&pGipCpu->u64TSC, u64TSC);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (u64TSCDelta >> 32)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u64TSCDelta = pGipCpu->u32UpdateIntervalTSC;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGipCpu->cErrors++;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * On the 2nd and 3rd callout, reset the history with the current TSC
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * interval since the values entered by supdrvGipInit are totally off.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * The interval on the 1st callout completely unreliable, the 2nd is a bit
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * better, while the 3rd should be most reliable.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u32TransactionId = pGipCpu->u32TransactionId;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_UNLIKELY( ( u32TransactionId == 5
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || u32TransactionId == 7)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico && ( iTick == 2
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico || iTick == 3) ))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico unsigned i;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico for (i = 0; i < RT_ELEMENTS(pGipCpu->au32TSCHistory); i++)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMAtomicUoWriteU32(&pGipCpu->au32TSCHistory[i], (uint32_t)u64TSCDelta);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * TSC History.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico Assert(RT_ELEMENTS(pGipCpu->au32TSCHistory) == 8);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico iTSCHistoryHead = (pGipCpu->iTSCHistoryHead + 1) & 7;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMAtomicXchgU32(&pGipCpu->iTSCHistoryHead, iTSCHistoryHead);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMAtomicXchgU32(&pGipCpu->au32TSCHistory[iTSCHistoryHead], (uint32_t)u64TSCDelta);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * UpdateIntervalTSC = average of last 8,2,1 intervals depending on update HZ.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pGip->u32UpdateHz >= 1000)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint32_t u32;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u32 = pGipCpu->au32TSCHistory[0];
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u32 += pGipCpu->au32TSCHistory[1];
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u32 += pGipCpu->au32TSCHistory[2];
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u32 += pGipCpu->au32TSCHistory[3];
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u32 >>= 2;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u32UpdateIntervalTSC = pGipCpu->au32TSCHistory[4];
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u32UpdateIntervalTSC += pGipCpu->au32TSCHistory[5];
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u32UpdateIntervalTSC += pGipCpu->au32TSCHistory[6];
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u32UpdateIntervalTSC += pGipCpu->au32TSCHistory[7];
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u32UpdateIntervalTSC >>= 2;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u32UpdateIntervalTSC += u32;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u32UpdateIntervalTSC >>= 1;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Value choosen for a 2GHz Athlon64 running linux 2.6.10/11, . */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u32UpdateIntervalTSCSlack = u32UpdateIntervalTSC >> 14;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else if (pGip->u32UpdateHz >= 90)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u32UpdateIntervalTSC = (uint32_t)u64TSCDelta;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u32UpdateIntervalTSC += pGipCpu->au32TSCHistory[(iTSCHistoryHead - 1) & 7];
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u32UpdateIntervalTSC >>= 1;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* value choosen on a 2GHz thinkpad running windows */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u32UpdateIntervalTSCSlack = u32UpdateIntervalTSC >> 7;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u32UpdateIntervalTSC = (uint32_t)u64TSCDelta;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* This value hasn't be checked yet.. waiting for OS/2 and 33Hz timers.. :-) */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u32UpdateIntervalTSCSlack = u32UpdateIntervalTSC >> 6;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMAtomicXchgU32(&pGipCpu->u32UpdateIntervalTSC, u32UpdateIntervalTSC + u32UpdateIntervalTSCSlack);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * CpuHz.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico u64CpuHz = ASMMult2xU32RetU64(u32UpdateIntervalTSC, pGip->u32UpdateHz);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMAtomicXchgU64(&pGipCpu->u64CpuHz, u64CpuHz);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Updates the GIP.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pGip Pointer to the GIP.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param u64NanoTS The current nanosecond timesamp.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param u64TSC The current TSC timesamp.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param iTick The current timer tick.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic void supdrvGipUpdate(PSUPGLOBALINFOPAGE pGip, uint64_t u64NanoTS, uint64_t u64TSC, uint64_t iTick)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Determin the relevant CPU data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPGIPCPU pGipCpu;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pGip->u32Mode != SUPGIPMODE_ASYNC_TSC)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGipCpu = &pGip->aCPUs[0];
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico unsigned iCpu = ASMGetApicId();
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_UNLIKELY(iCpu >= RT_ELEMENTS(pGip->aCPUs)))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGipCpu = &pGip->aCPUs[iCpu];
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Start update transaction.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!(ASMAtomicIncU32(&pGipCpu->u32TransactionId) & 1))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* this can happen on win32 if we're taking to long and there are more CPUs around. shouldn't happen though. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsgFailed(("Invalid transaction id, %#x, not odd!\n", pGipCpu->u32TransactionId));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMAtomicIncU32(&pGipCpu->u32TransactionId);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGipCpu->cErrors++;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Recalc the update frequency every 0x800th time.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!(pGipCpu->u32TransactionId & (GIP_UPDATEHZ_RECALC_FREQ * 2 - 2)))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pGip->u64NanoTSLastUpdateHz)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#ifdef RT_ARCH_AMD64 /** @todo fix 64-bit div here to work on x86 linux. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint64_t u64Delta = u64NanoTS - pGip->u64NanoTSLastUpdateHz;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint32_t u32UpdateHz = (uint32_t)((UINT64_C(1000000000) * GIP_UPDATEHZ_RECALC_FREQ) / u64Delta);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (u32UpdateHz <= 2000 && u32UpdateHz >= 30)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMAtomicXchgU32(&pGip->u32UpdateHz, u32UpdateHz);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMAtomicXchgU32(&pGip->u32UpdateIntervalNS, 1000000000 / u32UpdateHz);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico#endif
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMAtomicXchgU64(&pGip->u64NanoTSLastUpdateHz, u64NanoTS);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Update the data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvGipDoUpdateCpu(pGip, pGipCpu, u64NanoTS, u64TSC, iTick);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Complete transaction.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMAtomicIncU32(&pGipCpu->u32TransactionId);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/**
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Updates the per cpu GIP data for the calling cpu.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param pGip Pointer to the GIP.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param u64NanoTS The current nanosecond timesamp.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param u64TSC The current TSC timesamp.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param iCpu The CPU index.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * @param iTick The current timer tick.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic void supdrvGipUpdatePerCpu(PSUPGLOBALINFOPAGE pGip, uint64_t u64NanoTS, uint64_t u64TSC, unsigned iCpu, uint64_t iTick)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico{
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PSUPGIPCPU pGipCpu;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (RT_LIKELY(iCpu < RT_ELEMENTS(pGip->aCPUs)))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGipCpu = &pGip->aCPUs[iCpu];
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Start update transaction.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (!(ASMAtomicIncU32(&pGipCpu->u32TransactionId) & 1))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico {
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsgFailed(("Invalid transaction id, %#x, not odd!\n", pGipCpu->u32TransactionId));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMAtomicIncU32(&pGipCpu->u32TransactionId);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pGipCpu->cErrors++;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Update the data.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico supdrvGipDoUpdateCpu(pGip, pGipCpu, u64NanoTS, u64TSC, iTick);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /*
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Complete transaction.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico ASMAtomicIncU32(&pGipCpu->u32TransactionId);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico }
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico}
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico