VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/SUPDrv.c@ 25308

Last change on this file since 25308 was 25307, checked in by vboxsync, 15 years ago

SUPDrv: Use RTSemMutex instead of RTSemFastMutex for the loader on windows.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 195.6 KB
Line 
1/* $Revision: 25307 $ */
2/** @file
3 * VBoxDrv - The VirtualBox Support Driver - Common code.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#define LOG_GROUP LOG_GROUP_SUP_DRV
35#include "SUPDrvInternal.h"
36#ifndef PAGE_SHIFT
37# include <iprt/param.h>
38#endif
39#include <iprt/alloc.h>
40#include <iprt/cpuset.h>
41#include <iprt/handletable.h>
42#include <iprt/mp.h>
43#include <iprt/power.h>
44#include <iprt/process.h>
45#include <iprt/semaphore.h>
46#include <iprt/spinlock.h>
47#include <iprt/thread.h>
48#include <iprt/uuid.h>
49#include <VBox/param.h>
50#include <VBox/log.h>
51#include <VBox/err.h>
52#if defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
53# include <iprt/crc32.h>
54# include <iprt/net.h>
55# include <iprt/string.h>
56# include <iprt/rand.h>
57# include <iprt/path.h>
58#endif
59
60/*
61 * Logging assignments:
62 * Log - useful stuff, like failures.
63 * LogFlow - program flow, except the really noisy bits.
64 * Log2 - Cleanup.
65 * Log3 - Loader flow noise.
66 * Log4 - Call VMMR0 flow noise.
67 * Log5 - Native yet-to-be-defined noise.
68 * Log6 - Native ioctl flow noise.
69 *
70 * Logging requires BUILD_TYPE=debug and possibly changes to the logger
71 * instanciation in log-vbox.c(pp).
72 */
73
74
75/*******************************************************************************
76* Defined Constants And Macros *
77*******************************************************************************/
78/** The frequency by which we recalculate the u32UpdateHz and
79 * u32UpdateIntervalNS GIP members. The value must be a power of 2. */
80#define GIP_UPDATEHZ_RECALC_FREQ 0x800
81
82/** @def VBOX_SVN_REV
83 * The makefile should define this if it can. */
84#ifndef VBOX_SVN_REV
85# define VBOX_SVN_REV 0
86#endif
87
88
89/*******************************************************************************
90* Internal Functions *
91*******************************************************************************/
92static DECLCALLBACK(int) supdrvSessionObjHandleRetain(RTHANDLETABLE hHandleTable, void *pvObj, void *pvCtx, void *pvUser);
93static DECLCALLBACK(void) supdrvSessionObjHandleDelete(RTHANDLETABLE hHandleTable, uint32_t h, void *pvObj, void *pvCtx, void *pvUser);
94static int supdrvMemAdd(PSUPDRVMEMREF pMem, PSUPDRVSESSION pSession);
95static int supdrvMemRelease(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr, SUPDRVMEMREFTYPE eType);
96static int supdrvIOCtl_LdrOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDROPEN pReq);
97static int supdrvIOCtl_LdrLoad(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDRLOAD pReq);
98static int supdrvIOCtl_LdrFree(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDRFREE pReq);
99static int supdrvIOCtl_LdrGetSymbol(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDRGETSYMBOL pReq);
100static int supdrvIDC_LdrGetSymbol(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVIDCREQGETSYM pReq);
101static int supdrvLdrSetVMMR0EPs(PSUPDRVDEVEXT pDevExt, void *pvVMMR0, void *pvVMMR0EntryInt, void *pvVMMR0EntryFast, void *pvVMMR0EntryEx);
102static void supdrvLdrUnsetVMMR0EPs(PSUPDRVDEVEXT pDevExt);
103static int supdrvLdrAddUsage(PSUPDRVSESSION pSession, PSUPDRVLDRIMAGE pImage);
104static void supdrvLdrFree(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage);
105DECLINLINE(int) supdrvLdrLock(PSUPDRVDEVEXT pDevExt);
106DECLINLINE(int) supdrvLdrUnlock(PSUPDRVDEVEXT pDevExt);
107static int supdrvIOCtl_CallServiceModule(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPCALLSERVICE pReq);
108static int supdrvIOCtl_LoggerSettings(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLOGGERSETTINGS pReq);
109static int supdrvGipCreate(PSUPDRVDEVEXT pDevExt);
110static void supdrvGipDestroy(PSUPDRVDEVEXT pDevExt);
111static DECLCALLBACK(void) supdrvGipSyncTimer(PRTTIMER pTimer, void *pvUser, uint64_t iTick);
112static DECLCALLBACK(void) supdrvGipAsyncTimer(PRTTIMER pTimer, void *pvUser, uint64_t iTick);
113static DECLCALLBACK(void) supdrvGipMpEvent(RTMPEVENT enmEvent, RTCPUID idCpu, void *pvUser);
114
115#ifdef RT_WITH_W64_UNWIND_HACK
116DECLASM(int) supdrvNtWrapVMMR0EntryEx(PFNRT pfnVMMR0EntryEx, PVM pVM, VMCPUID idCpu, unsigned uOperation, PSUPVMMR0REQHDR pReq, uint64_t u64Arg, PSUPDRVSESSION pSession);
117DECLASM(int) supdrvNtWrapVMMR0EntryFast(PFNRT pfnVMMR0EntryFast, PVM pVM, VMCPUID idCpu, unsigned uOperation);
118DECLASM(void) supdrvNtWrapObjDestructor(PFNRT pfnDestruction, void *pvObj, void *pvUser1, void *pvUser2);
119DECLASM(void *) supdrvNtWrapQueryFactoryInterface(PFNRT pfnQueryFactoryInterface, struct SUPDRVFACTORY const *pSupDrvFactory, PSUPDRVSESSION pSession, const char *pszInterfaceUuid);
120DECLASM(int) supdrvNtWrapModuleInit(PFNRT pfnModuleInit);
121DECLASM(void) supdrvNtWrapModuleTerm(PFNRT pfnModuleTerm);
122DECLASM(int) supdrvNtWrapServiceReqHandler(PFNRT pfnServiceReqHandler, PSUPDRVSESSION pSession, uint32_t uOperation, uint64_t u64Arg, PSUPR0SERVICEREQHDR pReqHdr);
123
124DECLASM(int) UNWIND_WRAP(SUPR0ComponentRegisterFactory)(PSUPDRVSESSION pSession, PCSUPDRVFACTORY pFactory);
125DECLASM(int) UNWIND_WRAP(SUPR0ComponentDeregisterFactory)(PSUPDRVSESSION pSession, PCSUPDRVFACTORY pFactory);
126DECLASM(int) UNWIND_WRAP(SUPR0ComponentQueryFactory)(PSUPDRVSESSION pSession, const char *pszName, const char *pszInterfaceUuid, void **ppvFactoryIf);
127DECLASM(void *) UNWIND_WRAP(SUPR0ObjRegister)(PSUPDRVSESSION pSession, SUPDRVOBJTYPE enmType, PFNSUPDRVDESTRUCTOR pfnDestructor, void *pvUser1, void *pvUser2);
128DECLASM(int) UNWIND_WRAP(SUPR0ObjAddRef)(void *pvObj, PSUPDRVSESSION pSession);
129DECLASM(int) UNWIND_WRAP(SUPR0ObjAddRefEx)(void *pvObj, PSUPDRVSESSION pSession, bool fNoPreempt);
130DECLASM(int) UNWIND_WRAP(SUPR0ObjRelease)(void *pvObj, PSUPDRVSESSION pSession);
131DECLASM(int) UNWIND_WRAP(SUPR0ObjVerifyAccess)(void *pvObj, PSUPDRVSESSION pSession, const char *pszObjName);
132DECLASM(int) UNWIND_WRAP(SUPR0LockMem)(PSUPDRVSESSION pSession, RTR3PTR pvR3, uint32_t cPages, PRTHCPHYS paPages);
133DECLASM(int) UNWIND_WRAP(SUPR0UnlockMem)(PSUPDRVSESSION pSession, RTR3PTR pvR3);
134DECLASM(int) UNWIND_WRAP(SUPR0ContAlloc)(PSUPDRVSESSION pSession, uint32_t cPages, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PRTHCPHYS pHCPhys);
135DECLASM(int) UNWIND_WRAP(SUPR0ContFree)(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr);
136DECLASM(int) UNWIND_WRAP(SUPR0LowAlloc)(PSUPDRVSESSION pSession, uint32_t cPages, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PRTHCPHYS paPages);
137DECLASM(int) UNWIND_WRAP(SUPR0LowFree)(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr);
138DECLASM(int) UNWIND_WRAP(SUPR0MemAlloc)(PSUPDRVSESSION pSession, uint32_t cb, PRTR0PTR ppvR0, PRTR3PTR ppvR3);
139DECLASM(int) UNWIND_WRAP(SUPR0MemGetPhys)(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr, PSUPPAGE paPages);
140DECLASM(int) UNWIND_WRAP(SUPR0MemFree)(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr);
141DECLASM(int) UNWIND_WRAP(SUPR0PageAllocEx)(PSUPDRVSESSION pSession, uint32_t cPages, uint32_t fFlags, PRTR3PTR ppvR3, PRTR0PTR ppvR0, PRTHCPHYS paPages);
142DECLASM(int) UNWIND_WRAP(SUPR0PageFree)(PSUPDRVSESSION pSession, RTR3PTR pvR3);
143//DECLASM(int) UNWIND_WRAP(SUPR0Printf)(const char *pszFormat, ...);
144DECLASM(int) UNWIND_WRAP(SUPSemEventCreate)(PSUPDRVSESSION pSession, PSUPSEMEVENT phEvent);
145DECLASM(int) UNWIND_WRAP(SUPSemEventClose)(PSUPDRVSESSION pSession, SUPSEMEVENT hEvent);
146DECLASM(int) UNWIND_WRAP(SUPSemEventSignal)(PSUPDRVSESSION pSession, SUPSEMEVENT hEvent);
147DECLASM(int) UNWIND_WRAP(SUPSemEventWait)(PSUPDRVSESSION pSession, SUPSEMEVENT hEvent, uint32_t cMillies);
148DECLASM(int) UNWIND_WRAP(SUPSemEventWaitNoResume)(PSUPDRVSESSION pSession, SUPSEMEVENT hEvent, uint32_t cMillies);
149DECLASM(int) UNWIND_WRAP(SUPSemEventMultiCreate)(PSUPDRVSESSION pSession, PSUPSEMEVENTMULTI phEventMulti);
150DECLASM(int) UNWIND_WRAP(SUPSemEventMultiClose)(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti);
151DECLASM(int) UNWIND_WRAP(SUPSemEventMultiSignal)(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti);
152DECLASM(int) UNWIND_WRAP(SUPSemEventMultiReset)(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti);
153DECLASM(int) UNWIND_WRAP(SUPSemEventMultiWait)(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti, uint32_t cMillies);
154DECLASM(int) UNWIND_WRAP(SUPSemEventMultiWaitNoResume)(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti, uint32_t cMillies);
155DECLASM(SUPPAGINGMODE) UNWIND_WRAP(SUPR0GetPagingMode)(void);
156DECLASM(void *) UNWIND_WRAP(RTMemAlloc)(size_t cb) RT_NO_THROW;
157DECLASM(void *) UNWIND_WRAP(RTMemAllocZ)(size_t cb) RT_NO_THROW;
158DECLASM(void) UNWIND_WRAP(RTMemFree)(void *pv) RT_NO_THROW;
159DECLASM(void *) UNWIND_WRAP(RTMemDup)(const void *pvSrc, size_t cb) RT_NO_THROW;
160DECLASM(void *) UNWIND_WRAP(RTMemDupEx)(const void *pvSrc, size_t cbSrc, size_t cbExtra) RT_NO_THROW;
161DECLASM(void *) UNWIND_WRAP(RTMemRealloc)(void *pvOld, size_t cbNew) RT_NO_THROW;
162DECLASM(int) UNWIND_WRAP(RTR0MemObjAllocLow)(PRTR0MEMOBJ pMemObj, size_t cb, bool fExecutable);
163DECLASM(int) UNWIND_WRAP(RTR0MemObjAllocPage)(PRTR0MEMOBJ pMemObj, size_t cb, bool fExecutable);
164DECLASM(int) UNWIND_WRAP(RTR0MemObjAllocPhys)(PRTR0MEMOBJ pMemObj, size_t cb, RTHCPHYS PhysHighest);
165DECLASM(int) UNWIND_WRAP(RTR0MemObjAllocPhysNC)(PRTR0MEMOBJ pMemObj, size_t cb, RTHCPHYS PhysHighest);
166DECLASM(int) UNWIND_WRAP(RTR0MemObjAllocCont)(PRTR0MEMOBJ pMemObj, size_t cb, bool fExecutable);
167DECLASM(int) UNWIND_WRAP(RTR0MemObjEnterPhys)(PRTR0MEMOBJ pMemObj, RTHCPHYS Phys, size_t cb);
168DECLASM(int) UNWIND_WRAP(RTR0MemObjLockUser)(PRTR0MEMOBJ pMemObj, RTR3PTR R3Ptr, size_t cb, uint32_t fFlags, RTR0PROCESS R0Process);
169DECLASM(int) UNWIND_WRAP(RTR0MemObjMapKernel)(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, void *pvFixed, size_t uAlignment, unsigned fProt);
170DECLASM(int) UNWIND_WRAP(RTR0MemObjMapKernelEx)(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, void *pvFixed, size_t uAlignment, unsigned fProt, size_t offSub, size_t cbSub);
171DECLASM(int) UNWIND_WRAP(RTR0MemObjMapUser)(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process);
172DECLASM(int) UNWIND_WRAP(RTR0MemObjProtect)(RTR0MEMOBJ hMemObj, size_t offsub, size_t cbSub, uint32_t fProt);
173/*DECLASM(void *) UNWIND_WRAP(RTR0MemObjAddress)(RTR0MEMOBJ MemObj); - not necessary */
174/*DECLASM(RTR3PTR) UNWIND_WRAP(RTR0MemObjAddressR3)(RTR0MEMOBJ MemObj); - not necessary */
175/*DECLASM(size_t) UNWIND_WRAP(RTR0MemObjSize)(RTR0MEMOBJ MemObj); - not necessary */
176/*DECLASM(bool) UNWIND_WRAP(RTR0MemObjIsMapping)(RTR0MEMOBJ MemObj); - not necessary */
177/*DECLASM(RTHCPHYS) UNWIND_WRAP(RTR0MemObjGetPagePhysAddr)(RTR0MEMOBJ MemObj, size_t iPage); - not necessary */
178DECLASM(int) UNWIND_WRAP(RTR0MemObjFree)(RTR0MEMOBJ MemObj, bool fFreeMappings);
179DECLASM(int) UNWIND_WRAP(RTR0MemUserCopyFrom)(void *pvDst, RTR3PTR R3PtrSrc, size_t cb);
180DECLASM(int) UNWIND_WRAP(RTR0MemUserCopyTo)(RTR3PTR R3PtrDst, void const *pvSrc, size_t cb);
181/* RTR0MemUserIsValidAddr - not necessary */
182/* RTR0MemKernelIsValidAddr - not necessary */
183/* RTR0MemAreKrnlAndUsrDifferent - not necessary */
184/* RTProcSelf - not necessary */
185/* RTR0ProcHandleSelf - not necessary */
186DECLASM(int) UNWIND_WRAP(RTSemFastMutexCreate)(PRTSEMFASTMUTEX pMutexSem);
187DECLASM(int) UNWIND_WRAP(RTSemFastMutexDestroy)(RTSEMFASTMUTEX MutexSem);
188DECLASM(int) UNWIND_WRAP(RTSemFastMutexRequest)(RTSEMFASTMUTEX MutexSem);
189DECLASM(int) UNWIND_WRAP(RTSemFastMutexRelease)(RTSEMFASTMUTEX MutexSem);
190DECLASM(int) UNWIND_WRAP(RTSemEventCreate)(PRTSEMEVENT pEventSem);
191DECLASM(int) UNWIND_WRAP(RTSemEventSignal)(RTSEMEVENT EventSem);
192DECLASM(int) UNWIND_WRAP(RTSemEventWait)(RTSEMEVENT EventSem, unsigned cMillies);
193DECLASM(int) UNWIND_WRAP(RTSemEventWaitNoResume)(RTSEMEVENT EventSem, unsigned cMillies);
194DECLASM(int) UNWIND_WRAP(RTSemEventDestroy)(RTSEMEVENT EventSem);
195DECLASM(int) UNWIND_WRAP(RTSemEventMultiCreate)(PRTSEMEVENTMULTI pEventMultiSem);
196DECLASM(int) UNWIND_WRAP(RTSemEventMultiSignal)(RTSEMEVENTMULTI EventMultiSem);
197DECLASM(int) UNWIND_WRAP(RTSemEventMultiReset)(RTSEMEVENTMULTI EventMultiSem);
198DECLASM(int) UNWIND_WRAP(RTSemEventMultiWait)(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies);
199DECLASM(int) UNWIND_WRAP(RTSemEventMultiWaitNoResume)(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies);
200DECLASM(int) UNWIND_WRAP(RTSemEventMultiDestroy)(RTSEMEVENTMULTI EventMultiSem);
201DECLASM(int) UNWIND_WRAP(RTSpinlockCreate)(PRTSPINLOCK pSpinlock);
202DECLASM(int) UNWIND_WRAP(RTSpinlockDestroy)(RTSPINLOCK Spinlock);
203DECLASM(void) UNWIND_WRAP(RTSpinlockAcquire)(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp);
204DECLASM(void) UNWIND_WRAP(RTSpinlockRelease)(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp);
205DECLASM(void) UNWIND_WRAP(RTSpinlockAcquireNoInts)(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp);
206DECLASM(void) UNWIND_WRAP(RTSpinlockReleaseNoInts)(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp);
207/* RTTimeNanoTS - not necessary */
208/* RTTimeMilliTS - not necessary */
209/* RTTimeSystemNanoTS - not necessary */
210/* RTTimeSystemMilliTS - not necessary */
211/* RTThreadNativeSelf - not necessary */
212DECLASM(int) UNWIND_WRAP(RTThreadSleep)(unsigned cMillies);
213DECLASM(bool) UNWIND_WRAP(RTThreadYield)(void);
214#if 0
215/* RTThreadSelf - not necessary */
216DECLASM(int) UNWIND_WRAP(RTThreadCreate)(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
217 RTTHREADTYPE enmType, unsigned fFlags, const char *pszName);
218DECLASM(RTNATIVETHREAD) UNWIND_WRAP(RTThreadGetNative)(RTTHREAD Thread);
219DECLASM(int) UNWIND_WRAP(RTThreadWait)(RTTHREAD Thread, unsigned cMillies, int *prc);
220DECLASM(int) UNWIND_WRAP(RTThreadWaitNoResume)(RTTHREAD Thread, unsigned cMillies, int *prc);
221DECLASM(const char *) UNWIND_WRAP(RTThreadGetName)(RTTHREAD Thread);
222DECLASM(const char *) UNWIND_WRAP(RTThreadSelfName)(void);
223DECLASM(RTTHREADTYPE) UNWIND_WRAP(RTThreadGetType)(RTTHREAD Thread);
224DECLASM(int) UNWIND_WRAP(RTThreadUserSignal)(RTTHREAD Thread);
225DECLASM(int) UNWIND_WRAP(RTThreadUserReset)(RTTHREAD Thread);
226DECLASM(int) UNWIND_WRAP(RTThreadUserWait)(RTTHREAD Thread, unsigned cMillies);
227DECLASM(int) UNWIND_WRAP(RTThreadUserWaitNoResume)(RTTHREAD Thread, unsigned cMillies);
228#endif
229/* RTThreadPreemptIsEnabled - not necessary */
230/* RTThreadPreemptIsPending - not necessary */
231/* RTThreadPreemptIsPendingTrusty - not necessary */
232DECLASM(void) UNWIND_WRAP(RTThreadPreemptDisable)(PRTTHREADPREEMPTSTATE pState);
233DECLASM(void) UNWIND_WRAP(RTThreadPreemptRestore)(RTTHREADPREEMPTSTATE pState);
234/* RTLogDefaultInstance - a bit of a gamble, but we do not want the overhead! */
235/* RTMpCpuId - not necessary */
236/* RTMpCpuIdFromSetIndex - not necessary */
237/* RTMpCpuIdToSetIndex - not necessary */
238/* RTMpIsCpuPossible - not necessary */
239/* RTMpGetCount - not necessary */
240/* RTMpGetMaxCpuId - not necessary */
241/* RTMpGetOnlineCount - not necessary */
242/* RTMpGetOnlineSet - not necessary */
243/* RTMpGetSet - not necessary */
244/* RTMpIsCpuOnline - not necessary */
245DECLASM(int) UNWIND_WRAP(RTMpIsCpuWorkPending)(void);
246DECLASM(int) UNWIND_WRAP(RTMpOnAll)(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2);
247DECLASM(int) UNWIND_WRAP(RTMpOnOthers)(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2);
248DECLASM(int) UNWIND_WRAP(RTMpOnSpecific)(RTCPUID idCpu, PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2);
249DECLASM(int) UNWIND_WRAP(RTMpPokeCpu)(RTCPUID idCpu);
250/* RTLogRelDefaultInstance - not necessary. */
251DECLASM(int) UNWIND_WRAP(RTLogSetDefaultInstanceThread)(PRTLOGGER pLogger, uintptr_t uKey);
252/* RTLogLogger - can't wrap this buster. */
253/* RTLogLoggerEx - can't wrap this buster. */
254DECLASM(void) UNWIND_WRAP(RTLogLoggerExV)(PRTLOGGER pLogger, unsigned fFlags, unsigned iGroup, const char *pszFormat, va_list args);
255/* RTLogPrintf - can't wrap this buster. */ /** @todo provide va_list log wrappers in RuntimeR0. */
256DECLASM(void) UNWIND_WRAP(RTLogPrintfV)(const char *pszFormat, va_list args);
257DECLASM(void) UNWIND_WRAP(AssertMsg1)(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction);
258/* AssertMsg2 - can't wrap this buster. */
259#endif /* RT_WITH_W64_UNWIND_HACK */
260
261
262/*******************************************************************************
263* Global Variables *
264*******************************************************************************/
265DECLEXPORT(PSUPGLOBALINFOPAGE) g_pSUPGlobalInfoPage = NULL;
266
267/**
268 * Array of the R0 SUP API.
269 */
270static SUPFUNC g_aFunctions[] =
271{
272 /* name function */
273 /* Entries with absolute addresses determined at runtime, fixup
274 code makes ugly ASSUMPTIONS about the order here: */
275 { "SUPR0AbsIs64bit", (void *)0 },
276 { "SUPR0Abs64bitKernelCS", (void *)0 },
277 { "SUPR0Abs64bitKernelSS", (void *)0 },
278 { "SUPR0Abs64bitKernelDS", (void *)0 },
279 { "SUPR0AbsKernelCS", (void *)0 },
280 { "SUPR0AbsKernelSS", (void *)0 },
281 { "SUPR0AbsKernelDS", (void *)0 },
282 { "SUPR0AbsKernelES", (void *)0 },
283 { "SUPR0AbsKernelFS", (void *)0 },
284 { "SUPR0AbsKernelGS", (void *)0 },
285 /* Normal function pointers: */
286 { "SUPR0ComponentRegisterFactory", (void *)UNWIND_WRAP(SUPR0ComponentRegisterFactory) },
287 { "SUPR0ComponentDeregisterFactory", (void *)UNWIND_WRAP(SUPR0ComponentDeregisterFactory) },
288 { "SUPR0ComponentQueryFactory", (void *)UNWIND_WRAP(SUPR0ComponentQueryFactory) },
289 { "SUPR0ObjRegister", (void *)UNWIND_WRAP(SUPR0ObjRegister) },
290 { "SUPR0ObjAddRef", (void *)UNWIND_WRAP(SUPR0ObjAddRef) },
291 { "SUPR0ObjAddRefEx", (void *)UNWIND_WRAP(SUPR0ObjAddRefEx) },
292 { "SUPR0ObjRelease", (void *)UNWIND_WRAP(SUPR0ObjRelease) },
293 { "SUPR0ObjVerifyAccess", (void *)UNWIND_WRAP(SUPR0ObjVerifyAccess) },
294 { "SUPR0LockMem", (void *)UNWIND_WRAP(SUPR0LockMem) },
295 { "SUPR0UnlockMem", (void *)UNWIND_WRAP(SUPR0UnlockMem) },
296 { "SUPR0ContAlloc", (void *)UNWIND_WRAP(SUPR0ContAlloc) },
297 { "SUPR0ContFree", (void *)UNWIND_WRAP(SUPR0ContFree) },
298 { "SUPR0LowAlloc", (void *)UNWIND_WRAP(SUPR0LowAlloc) },
299 { "SUPR0LowFree", (void *)UNWIND_WRAP(SUPR0LowFree) },
300 { "SUPR0MemAlloc", (void *)UNWIND_WRAP(SUPR0MemAlloc) },
301 { "SUPR0MemGetPhys", (void *)UNWIND_WRAP(SUPR0MemGetPhys) },
302 { "SUPR0MemFree", (void *)UNWIND_WRAP(SUPR0MemFree) },
303 { "SUPR0PageAllocEx", (void *)UNWIND_WRAP(SUPR0PageAllocEx) },
304 { "SUPR0PageFree", (void *)UNWIND_WRAP(SUPR0PageFree) },
305 { "SUPR0Printf", (void *)SUPR0Printf }, /** @todo needs wrapping? */
306 { "SUPSemEventCreate", (void *)UNWIND_WRAP(SUPSemEventCreate) },
307 { "SUPSemEventClose", (void *)UNWIND_WRAP(SUPSemEventClose) },
308 { "SUPSemEventSignal", (void *)UNWIND_WRAP(SUPSemEventSignal) },
309 { "SUPSemEventWait", (void *)UNWIND_WRAP(SUPSemEventWait) },
310 { "SUPSemEventWaitNoResume", (void *)UNWIND_WRAP(SUPSemEventWaitNoResume) },
311 { "SUPSemEventMultiCreate", (void *)UNWIND_WRAP(SUPSemEventMultiCreate) },
312 { "SUPSemEventMultiClose", (void *)UNWIND_WRAP(SUPSemEventMultiClose) },
313 { "SUPSemEventMultiSignal", (void *)UNWIND_WRAP(SUPSemEventMultiSignal) },
314 { "SUPSemEventMultiReset", (void *)UNWIND_WRAP(SUPSemEventMultiReset) },
315 { "SUPSemEventMultiWait", (void *)UNWIND_WRAP(SUPSemEventMultiWait) },
316 { "SUPSemEventMultiWaitNoResume", (void *)UNWIND_WRAP(SUPSemEventMultiWaitNoResume) },
317 { "SUPR0GetPagingMode", (void *)UNWIND_WRAP(SUPR0GetPagingMode) },
318 { "SUPR0EnableVTx", (void *)SUPR0EnableVTx },
319 { "SUPGetGIP", (void *)SUPGetGIP },
320 { "g_pSUPGlobalInfoPage", (void *)&g_pSUPGlobalInfoPage },
321 { "RTMemAlloc", (void *)UNWIND_WRAP(RTMemAlloc) },
322 { "RTMemAllocZ", (void *)UNWIND_WRAP(RTMemAllocZ) },
323 { "RTMemFree", (void *)UNWIND_WRAP(RTMemFree) },
324 /*{ "RTMemDup", (void *)UNWIND_WRAP(RTMemDup) },
325 { "RTMemDupEx", (void *)UNWIND_WRAP(RTMemDupEx) },*/
326 { "RTMemRealloc", (void *)UNWIND_WRAP(RTMemRealloc) },
327 { "RTR0MemObjAllocLow", (void *)UNWIND_WRAP(RTR0MemObjAllocLow) },
328 { "RTR0MemObjAllocPage", (void *)UNWIND_WRAP(RTR0MemObjAllocPage) },
329 { "RTR0MemObjAllocPhys", (void *)UNWIND_WRAP(RTR0MemObjAllocPhys) },
330 { "RTR0MemObjAllocPhysNC", (void *)UNWIND_WRAP(RTR0MemObjAllocPhysNC) },
331 { "RTR0MemObjAllocCont", (void *)UNWIND_WRAP(RTR0MemObjAllocCont) },
332 { "RTR0MemObjEnterPhys", (void *)UNWIND_WRAP(RTR0MemObjEnterPhys) },
333 { "RTR0MemObjLockUser", (void *)UNWIND_WRAP(RTR0MemObjLockUser) },
334 { "RTR0MemObjMapKernel", (void *)UNWIND_WRAP(RTR0MemObjMapKernel) },
335 { "RTR0MemObjMapKernelEx", (void *)UNWIND_WRAP(RTR0MemObjMapKernelEx) },
336 { "RTR0MemObjMapUser", (void *)UNWIND_WRAP(RTR0MemObjMapUser) },
337 { "RTR0MemObjProtect", (void *)UNWIND_WRAP(RTR0MemObjProtect) },
338 { "RTR0MemObjAddress", (void *)RTR0MemObjAddress },
339 { "RTR0MemObjAddressR3", (void *)RTR0MemObjAddressR3 },
340 { "RTR0MemObjSize", (void *)RTR0MemObjSize },
341 { "RTR0MemObjIsMapping", (void *)RTR0MemObjIsMapping },
342 { "RTR0MemObjGetPagePhysAddr", (void *)RTR0MemObjGetPagePhysAddr },
343 { "RTR0MemObjFree", (void *)UNWIND_WRAP(RTR0MemObjFree) },
344 { "RTR0MemUserCopyFrom", (void *)UNWIND_WRAP(RTR0MemUserCopyFrom) },
345 { "RTR0MemUserCopyTo", (void *)UNWIND_WRAP(RTR0MemUserCopyTo) },
346 { "RTR0MemUserIsValidAddr", (void *)RTR0MemUserIsValidAddr },
347 { "RTR0MemKernelIsValidAddr", (void *)RTR0MemKernelIsValidAddr },
348 { "RTR0MemAreKrnlAndUsrDifferent", (void *)RTR0MemAreKrnlAndUsrDifferent },
349/* These don't work yet on linux - use fast mutexes!
350 { "RTSemMutexCreate", (void *)RTSemMutexCreate },
351 { "RTSemMutexRequest", (void *)RTSemMutexRequest },
352 { "RTSemMutexRelease", (void *)RTSemMutexRelease },
353 { "RTSemMutexDestroy", (void *)RTSemMutexDestroy },
354*/
355 { "RTProcSelf", (void *)RTProcSelf },
356 { "RTR0ProcHandleSelf", (void *)RTR0ProcHandleSelf },
357 { "RTSemFastMutexCreate", (void *)UNWIND_WRAP(RTSemFastMutexCreate) },
358 { "RTSemFastMutexDestroy", (void *)UNWIND_WRAP(RTSemFastMutexDestroy) },
359 { "RTSemFastMutexRequest", (void *)UNWIND_WRAP(RTSemFastMutexRequest) },
360 { "RTSemFastMutexRelease", (void *)UNWIND_WRAP(RTSemFastMutexRelease) },
361 { "RTSemEventCreate", (void *)UNWIND_WRAP(RTSemEventCreate) },
362 { "RTSemEventSignal", (void *)UNWIND_WRAP(RTSemEventSignal) },
363 { "RTSemEventWait", (void *)UNWIND_WRAP(RTSemEventWait) },
364 { "RTSemEventWaitNoResume", (void *)UNWIND_WRAP(RTSemEventWaitNoResume) },
365 { "RTSemEventDestroy", (void *)UNWIND_WRAP(RTSemEventDestroy) },
366 { "RTSemEventMultiCreate", (void *)UNWIND_WRAP(RTSemEventMultiCreate) },
367 { "RTSemEventMultiSignal", (void *)UNWIND_WRAP(RTSemEventMultiSignal) },
368 { "RTSemEventMultiReset", (void *)UNWIND_WRAP(RTSemEventMultiReset) },
369 { "RTSemEventMultiWait", (void *)UNWIND_WRAP(RTSemEventMultiWait) },
370 { "RTSemEventMultiWaitNoResume", (void *)UNWIND_WRAP(RTSemEventMultiWaitNoResume) },
371 { "RTSemEventMultiDestroy", (void *)UNWIND_WRAP(RTSemEventMultiDestroy) },
372 { "RTSpinlockCreate", (void *)UNWIND_WRAP(RTSpinlockCreate) },
373 { "RTSpinlockDestroy", (void *)UNWIND_WRAP(RTSpinlockDestroy) },
374 { "RTSpinlockAcquire", (void *)UNWIND_WRAP(RTSpinlockAcquire) },
375 { "RTSpinlockRelease", (void *)UNWIND_WRAP(RTSpinlockRelease) },
376 { "RTSpinlockAcquireNoInts", (void *)UNWIND_WRAP(RTSpinlockAcquireNoInts) },
377 { "RTSpinlockReleaseNoInts", (void *)UNWIND_WRAP(RTSpinlockReleaseNoInts) },
378 { "RTTimeNanoTS", (void *)RTTimeNanoTS },
379 { "RTTimeMilliTS", (void *)RTTimeMilliTS },
380 { "RTTimeSystemNanoTS", (void *)RTTimeSystemNanoTS },
381 { "RTTimeSystemMilliTS", (void *)RTTimeSystemMilliTS },
382 { "RTThreadNativeSelf", (void *)RTThreadNativeSelf },
383 { "RTThreadSleep", (void *)UNWIND_WRAP(RTThreadSleep) },
384 { "RTThreadYield", (void *)UNWIND_WRAP(RTThreadYield) },
385#if 0 /* Thread APIs, Part 2. */
386 { "RTThreadSelf", (void *)UNWIND_WRAP(RTThreadSelf) },
387 { "RTThreadCreate", (void *)UNWIND_WRAP(RTThreadCreate) }, /** @todo need to wrap the callback */
388 { "RTThreadGetNative", (void *)UNWIND_WRAP(RTThreadGetNative) },
389 { "RTThreadWait", (void *)UNWIND_WRAP(RTThreadWait) },
390 { "RTThreadWaitNoResume", (void *)UNWIND_WRAP(RTThreadWaitNoResume) },
391 { "RTThreadGetName", (void *)UNWIND_WRAP(RTThreadGetName) },
392 { "RTThreadSelfName", (void *)UNWIND_WRAP(RTThreadSelfName) },
393 { "RTThreadGetType", (void *)UNWIND_WRAP(RTThreadGetType) },
394 { "RTThreadUserSignal", (void *)UNWIND_WRAP(RTThreadUserSignal) },
395 { "RTThreadUserReset", (void *)UNWIND_WRAP(RTThreadUserReset) },
396 { "RTThreadUserWait", (void *)UNWIND_WRAP(RTThreadUserWait) },
397 { "RTThreadUserWaitNoResume", (void *)UNWIND_WRAP(RTThreadUserWaitNoResume) },
398#endif
399 { "RTThreadPreemptIsEnabled", (void *)RTThreadPreemptIsEnabled },
400 { "RTThreadPreemptIsPending", (void *)RTThreadPreemptIsPending },
401 { "RTThreadPreemptIsPendingTrusty", (void *)RTThreadPreemptIsPendingTrusty },
402 { "RTThreadPreemptIsPossible", (void *)RTThreadPreemptIsPossible },
403 { "RTThreadPreemptDisable", (void *)UNWIND_WRAP(RTThreadPreemptDisable) },
404 { "RTThreadPreemptRestore", (void *)UNWIND_WRAP(RTThreadPreemptRestore) },
405 { "RTThreadIsInInterrupt", (void *)RTThreadIsInInterrupt },
406
407 { "RTLogDefaultInstance", (void *)RTLogDefaultInstance },
408 { "RTMpCpuId", (void *)RTMpCpuId },
409 { "RTMpCpuIdFromSetIndex", (void *)RTMpCpuIdFromSetIndex },
410 { "RTMpCpuIdToSetIndex", (void *)RTMpCpuIdToSetIndex },
411 { "RTMpIsCpuPossible", (void *)RTMpIsCpuPossible },
412 { "RTMpGetCount", (void *)RTMpGetCount },
413 { "RTMpGetMaxCpuId", (void *)RTMpGetMaxCpuId },
414 { "RTMpGetOnlineCount", (void *)RTMpGetOnlineCount },
415 { "RTMpGetOnlineSet", (void *)RTMpGetOnlineSet },
416 { "RTMpGetSet", (void *)RTMpGetSet },
417 { "RTMpIsCpuOnline", (void *)RTMpIsCpuOnline },
418 { "RTMpIsCpuWorkPending", (void *)UNWIND_WRAP(RTMpIsCpuWorkPending) },
419 { "RTMpOnAll", (void *)UNWIND_WRAP(RTMpOnAll) },
420 { "RTMpOnOthers", (void *)UNWIND_WRAP(RTMpOnOthers) },
421 { "RTMpOnSpecific", (void *)UNWIND_WRAP(RTMpOnSpecific) },
422 { "RTMpPokeCpu", (void *)UNWIND_WRAP(RTMpPokeCpu) },
423 { "RTPowerNotificationRegister", (void *)RTPowerNotificationRegister },
424 { "RTPowerNotificationDeregister", (void *)RTPowerNotificationDeregister },
425 { "RTLogRelDefaultInstance", (void *)RTLogRelDefaultInstance },
426 { "RTLogSetDefaultInstanceThread", (void *)UNWIND_WRAP(RTLogSetDefaultInstanceThread) },
427 { "RTLogLogger", (void *)RTLogLogger }, /** @todo remove this */
428 { "RTLogLoggerEx", (void *)RTLogLoggerEx }, /** @todo remove this */
429 { "RTLogLoggerExV", (void *)UNWIND_WRAP(RTLogLoggerExV) },
430 { "RTLogPrintf", (void *)RTLogPrintf }, /** @todo remove this */
431 { "RTLogPrintfV", (void *)UNWIND_WRAP(RTLogPrintfV) },
432 { "AssertMsg1", (void *)UNWIND_WRAP(AssertMsg1) },
433 { "AssertMsg2", (void *)AssertMsg2 }, /** @todo replace this by RTAssertMsg2V */
434#if defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS)
435 { "RTR0AssertPanicSystem", (void *)RTR0AssertPanicSystem },
436#endif
437#if defined(RT_OS_DARWIN)
438 { "RTAssertMsg1", (void *)RTAssertMsg1 },
439 { "RTAssertMsg2", (void *)RTAssertMsg2 },
440 { "RTAssertMsg2V", (void *)RTAssertMsg2V },
441#endif
442};
443
444#if defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
445/**
446 * Drag in the rest of IRPT since we share it with the
447 * rest of the kernel modules on darwin.
448 */
449PFNRT g_apfnVBoxDrvIPRTDeps[] =
450{
451 /* VBoxNetFlt */
452 (PFNRT)RTCrc32,
453 (PFNRT)RTErrConvertFromErrno,
454 (PFNRT)RTNetIPv4IsHdrValid,
455 (PFNRT)RTNetIPv4TCPChecksum,
456 (PFNRT)RTNetIPv4UDPChecksum,
457 (PFNRT)RTUuidCompare,
458 (PFNRT)RTUuidCompareStr,
459 (PFNRT)RTUuidFromStr,
460 (PFNRT)RTStrDup,
461 (PFNRT)RTStrFree,
462 /* VBoxNetAdp */
463 (PFNRT)RTRandBytes,
464 /* VBoxUSB */
465 (PFNRT)RTPathStripFilename,
466 NULL
467};
468#endif /* RT_OS_DARWIN || RT_OS_SOLARIS || RT_OS_SOLARIS */
469
470
471/**
472 * Initializes the device extentsion structure.
473 *
474 * @returns IPRT status code.
475 * @param pDevExt The device extension to initialize.
476 */
477int VBOXCALL supdrvInitDevExt(PSUPDRVDEVEXT pDevExt)
478{
479 int rc;
480
481#ifdef SUPDRV_WITH_RELEASE_LOGGER
482 /*
483 * Create the release log.
484 */
485 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
486 PRTLOGGER pRelLogger;
487 rc = RTLogCreate(&pRelLogger, 0 /* fFlags */, "all",
488 "VBOX_RELEASE_LOG", RT_ELEMENTS(s_apszGroups), s_apszGroups,
489 RTLOGDEST_STDOUT | RTLOGDEST_DEBUGGER, NULL);
490 if (RT_SUCCESS(rc))
491 RTLogRelSetDefaultInstance(pRelLogger);
492 /** @todo Add native hook for getting logger config parameters and setting
493 * them. On linux we should use the module parameter stuff... */
494#endif
495
496 /*
497 * Initialize it.
498 */
499 memset(pDevExt, 0, sizeof(*pDevExt));
500 rc = RTSpinlockCreate(&pDevExt->Spinlock);
501 if (!rc)
502 {
503#ifdef SUPDRV_USE_MUTEX_FOR_LDR
504 rc = RTSemMutexCreate(&pDevExt->mtxLdr);
505#else
506 rc = RTSemFastMutexCreate(&pDevExt->mtxLdr);
507#endif
508 if (!rc)
509 {
510 rc = RTSemFastMutexCreate(&pDevExt->mtxComponentFactory);
511 if (!rc)
512 {
513 rc = RTSemFastMutexCreate(&pDevExt->mtxGip);
514 if (!rc)
515 {
516 rc = supdrvGipCreate(pDevExt);
517 if (RT_SUCCESS(rc))
518 {
519 pDevExt->u32Cookie = BIRD; /** @todo make this random? */
520
521 /*
522 * Fixup the absolute symbols.
523 *
524 * Because of the table indexing assumptions we'll have a little #ifdef orgy
525 * here rather than distributing this to OS specific files. At least for now.
526 */
527#ifdef RT_OS_DARWIN
528# if ARCH_BITS == 32
529 if (SUPR0GetPagingMode() >= SUPPAGINGMODE_AMD64)
530 {
531 g_aFunctions[0].pfn = (void *)1; /* SUPR0AbsIs64bit */
532 g_aFunctions[1].pfn = (void *)0x80; /* SUPR0Abs64bitKernelCS - KERNEL64_CS, seg.h */
533 g_aFunctions[2].pfn = (void *)0x88; /* SUPR0Abs64bitKernelSS - KERNEL64_SS, seg.h */
534 g_aFunctions[3].pfn = (void *)0x88; /* SUPR0Abs64bitKernelDS - KERNEL64_SS, seg.h */
535 }
536 else
537 g_aFunctions[0].pfn = g_aFunctions[1].pfn = g_aFunctions[2].pfn = g_aFunctions[4].pfn = (void *)0;
538 g_aFunctions[4].pfn = (void *)0x08; /* SUPR0AbsKernelCS - KERNEL_CS, seg.h */
539 g_aFunctions[5].pfn = (void *)0x10; /* SUPR0AbsKernelSS - KERNEL_DS, seg.h */
540 g_aFunctions[6].pfn = (void *)0x10; /* SUPR0AbsKernelDS - KERNEL_DS, seg.h */
541 g_aFunctions[7].pfn = (void *)0x10; /* SUPR0AbsKernelES - KERNEL_DS, seg.h */
542 g_aFunctions[8].pfn = (void *)0x10; /* SUPR0AbsKernelFS - KERNEL_DS, seg.h */
543 g_aFunctions[9].pfn = (void *)0x48; /* SUPR0AbsKernelGS - CPU_DATA_GS, seg.h */
544# else /* 64-bit darwin: */
545 g_aFunctions[0].pfn = (void *)1; /* SUPR0AbsIs64bit */
546 g_aFunctions[1].pfn = (void *)(uintptr_t)ASMGetCS(); /* SUPR0Abs64bitKernelCS */
547 g_aFunctions[2].pfn = (void *)(uintptr_t)ASMGetSS(); /* SUPR0Abs64bitKernelSS */
548 g_aFunctions[3].pfn = (void *)0; /* SUPR0Abs64bitKernelDS */
549 g_aFunctions[4].pfn = (void *)(uintptr_t)ASMGetCS(); /* SUPR0AbsKernelCS */
550 g_aFunctions[5].pfn = (void *)(uintptr_t)ASMGetSS(); /* SUPR0AbsKernelSS */
551 g_aFunctions[6].pfn = (void *)0; /* SUPR0AbsKernelDS */
552 g_aFunctions[7].pfn = (void *)0; /* SUPR0AbsKernelES */
553 g_aFunctions[8].pfn = (void *)0; /* SUPR0AbsKernelFS */
554 g_aFunctions[9].pfn = (void *)0; /* SUPR0AbsKernelGS */
555
556# endif
557#else /* !RT_OS_DARWIN */
558# if ARCH_BITS == 64
559 g_aFunctions[0].pfn = (void *)1; /* SUPR0AbsIs64bit */
560 g_aFunctions[1].pfn = (void *)(uintptr_t)ASMGetCS(); /* SUPR0Abs64bitKernelCS */
561 g_aFunctions[2].pfn = (void *)(uintptr_t)ASMGetSS(); /* SUPR0Abs64bitKernelSS */
562 g_aFunctions[3].pfn = (void *)(uintptr_t)ASMGetDS(); /* SUPR0Abs64bitKernelDS */
563# else
564 g_aFunctions[0].pfn = g_aFunctions[1].pfn = g_aFunctions[2].pfn = g_aFunctions[4].pfn = (void *)0;
565# endif
566 g_aFunctions[4].pfn = (void *)(uintptr_t)ASMGetCS(); /* SUPR0AbsKernelCS */
567 g_aFunctions[5].pfn = (void *)(uintptr_t)ASMGetSS(); /* SUPR0AbsKernelSS */
568 g_aFunctions[6].pfn = (void *)(uintptr_t)ASMGetDS(); /* SUPR0AbsKernelDS */
569 g_aFunctions[7].pfn = (void *)(uintptr_t)ASMGetES(); /* SUPR0AbsKernelES */
570 g_aFunctions[8].pfn = (void *)(uintptr_t)ASMGetFS(); /* SUPR0AbsKernelFS */
571 g_aFunctions[9].pfn = (void *)(uintptr_t)ASMGetGS(); /* SUPR0AbsKernelGS */
572#endif /* !RT_OS_DARWIN */
573 return VINF_SUCCESS;
574 }
575
576 RTSemFastMutexDestroy(pDevExt->mtxGip);
577 pDevExt->mtxGip = NIL_RTSEMFASTMUTEX;
578 }
579 RTSemFastMutexDestroy(pDevExt->mtxComponentFactory);
580 pDevExt->mtxComponentFactory = NIL_RTSEMFASTMUTEX;
581 }
582#ifdef SUPDRV_USE_MUTEX_FOR_LDR
583 RTSemMutexDestroy(pDevExt->mtxLdr);
584 pDevExt->mtxLdr = NIL_RTSEMMUTEX;
585#else
586 RTSemFastMutexDestroy(pDevExt->mtxLdr);
587 pDevExt->mtxLdr = NIL_RTSEMFASTMUTEX;
588#endif
589 }
590 RTSpinlockDestroy(pDevExt->Spinlock);
591 pDevExt->Spinlock = NIL_RTSPINLOCK;
592 }
593#ifdef SUPDRV_WITH_RELEASE_LOGGER
594 RTLogDestroy(RTLogRelSetDefaultInstance(NULL));
595 RTLogDestroy(RTLogSetDefaultInstance(NULL));
596#endif
597
598 return rc;
599}
600
601
602/**
603 * Delete the device extension (e.g. cleanup members).
604 *
605 * @param pDevExt The device extension to delete.
606 */
607void VBOXCALL supdrvDeleteDevExt(PSUPDRVDEVEXT pDevExt)
608{
609 PSUPDRVOBJ pObj;
610 PSUPDRVUSAGE pUsage;
611
612 /*
613 * Kill mutexes and spinlocks.
614 */
615 RTSemFastMutexDestroy(pDevExt->mtxGip);
616 pDevExt->mtxGip = NIL_RTSEMFASTMUTEX;
617#ifdef SUPDRV_USE_MUTEX_FOR_LDR
618 RTSemMutexDestroy(pDevExt->mtxLdr);
619 pDevExt->mtxLdr = NIL_RTSEMMUTEX;
620#else
621 RTSemFastMutexDestroy(pDevExt->mtxLdr);
622 pDevExt->mtxLdr = NIL_RTSEMFASTMUTEX;
623#endif
624 RTSpinlockDestroy(pDevExt->Spinlock);
625 pDevExt->Spinlock = NIL_RTSPINLOCK;
626 RTSemFastMutexDestroy(pDevExt->mtxComponentFactory);
627 pDevExt->mtxComponentFactory = NIL_RTSEMFASTMUTEX;
628
629 /*
630 * Free lists.
631 */
632 /* objects. */
633 pObj = pDevExt->pObjs;
634#if !defined(DEBUG_bird) || !defined(RT_OS_LINUX) /* breaks unloading, temporary, remove me! */
635 Assert(!pObj); /* (can trigger on forced unloads) */
636#endif
637 pDevExt->pObjs = NULL;
638 while (pObj)
639 {
640 void *pvFree = pObj;
641 pObj = pObj->pNext;
642 RTMemFree(pvFree);
643 }
644
645 /* usage records. */
646 pUsage = pDevExt->pUsageFree;
647 pDevExt->pUsageFree = NULL;
648 while (pUsage)
649 {
650 void *pvFree = pUsage;
651 pUsage = pUsage->pNext;
652 RTMemFree(pvFree);
653 }
654
655 /* kill the GIP. */
656 supdrvGipDestroy(pDevExt);
657
658#ifdef SUPDRV_WITH_RELEASE_LOGGER
659 /* destroy the loggers. */
660 RTLogDestroy(RTLogRelSetDefaultInstance(NULL));
661 RTLogDestroy(RTLogSetDefaultInstance(NULL));
662#endif
663}
664
665
666/**
667 * Create session.
668 *
669 * @returns IPRT status code.
670 * @param pDevExt Device extension.
671 * @param fUser Flag indicating whether this is a user or kernel session.
672 * @param ppSession Where to store the pointer to the session data.
673 */
674int VBOXCALL supdrvCreateSession(PSUPDRVDEVEXT pDevExt, bool fUser, PSUPDRVSESSION *ppSession)
675{
676 /*
677 * Allocate memory for the session data.
678 */
679 int rc = VERR_NO_MEMORY;
680 PSUPDRVSESSION pSession = *ppSession = (PSUPDRVSESSION)RTMemAllocZ(sizeof(*pSession));
681 if (pSession)
682 {
683 /* Initialize session data. */
684 rc = RTSpinlockCreate(&pSession->Spinlock);
685 if (!rc)
686 {
687 rc = RTHandleTableCreateEx(&pSession->hHandleTable,
688 RTHANDLETABLE_FLAGS_LOCKED | RTHANDLETABLE_FLAGS_CONTEXT,
689 1 /*uBase*/, 32768 /*cMax*/, supdrvSessionObjHandleRetain, pSession);
690 if (RT_SUCCESS(rc))
691 {
692 Assert(pSession->Spinlock != NIL_RTSPINLOCK);
693 pSession->pDevExt = pDevExt;
694 pSession->u32Cookie = BIRD_INV;
695 /*pSession->pLdrUsage = NULL;
696 pSession->pVM = NULL;
697 pSession->pUsage = NULL;
698 pSession->pGip = NULL;
699 pSession->fGipReferenced = false;
700 pSession->Bundle.cUsed = 0; */
701 pSession->Uid = NIL_RTUID;
702 pSession->Gid = NIL_RTGID;
703 if (fUser)
704 {
705 pSession->Process = RTProcSelf();
706 pSession->R0Process = RTR0ProcHandleSelf();
707 }
708 else
709 {
710 pSession->Process = NIL_RTPROCESS;
711 pSession->R0Process = NIL_RTR0PROCESS;
712 }
713
714 LogFlow(("Created session %p initial cookie=%#x\n", pSession, pSession->u32Cookie));
715 return VINF_SUCCESS;
716 }
717
718 RTSpinlockDestroy(pSession->Spinlock);
719 }
720 RTMemFree(pSession);
721 *ppSession = NULL;
722 Log(("Failed to create spinlock, rc=%d!\n", rc));
723 }
724
725 return rc;
726}
727
728
729/**
730 * Shared code for cleaning up a session.
731 *
732 * @param pDevExt Device extension.
733 * @param pSession Session data.
734 * This data will be freed by this routine.
735 */
736void VBOXCALL supdrvCloseSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession)
737{
738 /*
739 * Cleanup the session first.
740 */
741 supdrvCleanupSession(pDevExt, pSession);
742
743 /*
744 * Free the rest of the session stuff.
745 */
746 RTSpinlockDestroy(pSession->Spinlock);
747 pSession->Spinlock = NIL_RTSPINLOCK;
748 pSession->pDevExt = NULL;
749 RTMemFree(pSession);
750 LogFlow(("supdrvCloseSession: returns\n"));
751}
752
753
754/**
755 * Shared code for cleaning up a session (but not quite freeing it).
756 *
757 * This is primarily intended for MAC OS X where we have to clean up the memory
758 * stuff before the file handle is closed.
759 *
760 * @param pDevExt Device extension.
761 * @param pSession Session data.
762 * This data will be freed by this routine.
763 */
764void VBOXCALL supdrvCleanupSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession)
765{
766 int rc;
767 PSUPDRVBUNDLE pBundle;
768 LogFlow(("supdrvCleanupSession: pSession=%p\n", pSession));
769
770 /*
771 * Remove logger instances related to this session.
772 */
773 RTLogSetDefaultInstanceThread(NULL, (uintptr_t)pSession);
774
775 /*
776 * Destroy the handle table.
777 */
778 rc = RTHandleTableDestroy(pSession->hHandleTable, supdrvSessionObjHandleDelete, pSession);
779 AssertRC(rc);
780 pSession->hHandleTable = NIL_RTHANDLETABLE;
781
782 /*
783 * Release object references made in this session.
784 * In theory there should be noone racing us in this session.
785 */
786 Log2(("release objects - start\n"));
787 if (pSession->pUsage)
788 {
789 RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
790 PSUPDRVUSAGE pUsage;
791 RTSpinlockAcquire(pDevExt->Spinlock, &SpinlockTmp);
792
793 while ((pUsage = pSession->pUsage) != NULL)
794 {
795 PSUPDRVOBJ pObj = pUsage->pObj;
796 pSession->pUsage = pUsage->pNext;
797
798 AssertMsg(pUsage->cUsage >= 1 && pObj->cUsage >= pUsage->cUsage, ("glob %d; sess %d\n", pObj->cUsage, pUsage->cUsage));
799 if (pUsage->cUsage < pObj->cUsage)
800 {
801 pObj->cUsage -= pUsage->cUsage;
802 RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
803 }
804 else
805 {
806 /* Destroy the object and free the record. */
807 if (pDevExt->pObjs == pObj)
808 pDevExt->pObjs = pObj->pNext;
809 else
810 {
811 PSUPDRVOBJ pObjPrev;
812 for (pObjPrev = pDevExt->pObjs; pObjPrev; pObjPrev = pObjPrev->pNext)
813 if (pObjPrev->pNext == pObj)
814 {
815 pObjPrev->pNext = pObj->pNext;
816 break;
817 }
818 Assert(pObjPrev);
819 }
820 RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
821
822 Log(("supdrvCleanupSession: destroying %p/%d (%p/%p) cpid=%RTproc pid=%RTproc dtor=%p\n",
823 pObj, pObj->enmType, pObj->pvUser1, pObj->pvUser2, pObj->CreatorProcess, RTProcSelf(), pObj->pfnDestructor));
824 if (pObj->pfnDestructor)
825#ifdef RT_WITH_W64_UNWIND_HACK
826 supdrvNtWrapObjDestructor((PFNRT)pObj->pfnDestructor, pObj, pObj->pvUser1, pObj->pvUser2);
827#else
828 pObj->pfnDestructor(pObj, pObj->pvUser1, pObj->pvUser2);
829#endif
830 RTMemFree(pObj);
831 }
832
833 /* free it and continue. */
834 RTMemFree(pUsage);
835
836 RTSpinlockAcquire(pDevExt->Spinlock, &SpinlockTmp);
837 }
838
839 RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
840 AssertMsg(!pSession->pUsage, ("Some buster reregistered an object during desturction!\n"));
841 }
842 Log2(("release objects - done\n"));
843
844 /*
845 * Release memory allocated in the session.
846 *
847 * We do not serialize this as we assume that the application will
848 * not allocated memory while closing the file handle object.
849 */
850 Log2(("freeing memory:\n"));
851 pBundle = &pSession->Bundle;
852 while (pBundle)
853 {
854 PSUPDRVBUNDLE pToFree;
855 unsigned i;
856
857 /*
858 * Check and unlock all entries in the bundle.
859 */
860 for (i = 0; i < RT_ELEMENTS(pBundle->aMem); i++)
861 {
862 if (pBundle->aMem[i].MemObj != NIL_RTR0MEMOBJ)
863 {
864 Log2(("eType=%d pvR0=%p pvR3=%p cb=%ld\n", pBundle->aMem[i].eType, RTR0MemObjAddress(pBundle->aMem[i].MemObj),
865 (void *)RTR0MemObjAddressR3(pBundle->aMem[i].MapObjR3), (long)RTR0MemObjSize(pBundle->aMem[i].MemObj)));
866 if (pBundle->aMem[i].MapObjR3 != NIL_RTR0MEMOBJ)
867 {
868 rc = RTR0MemObjFree(pBundle->aMem[i].MapObjR3, false);
869 AssertRC(rc); /** @todo figure out how to handle this. */
870 pBundle->aMem[i].MapObjR3 = NIL_RTR0MEMOBJ;
871 }
872 rc = RTR0MemObjFree(pBundle->aMem[i].MemObj, true /* fFreeMappings */);
873 AssertRC(rc); /** @todo figure out how to handle this. */
874 pBundle->aMem[i].MemObj = NIL_RTR0MEMOBJ;
875 pBundle->aMem[i].eType = MEMREF_TYPE_UNUSED;
876 }
877 }
878
879 /*
880 * Advance and free previous bundle.
881 */
882 pToFree = pBundle;
883 pBundle = pBundle->pNext;
884
885 pToFree->pNext = NULL;
886 pToFree->cUsed = 0;
887 if (pToFree != &pSession->Bundle)
888 RTMemFree(pToFree);
889 }
890 Log2(("freeing memory - done\n"));
891
892 /*
893 * Deregister component factories.
894 */
895 RTSemFastMutexRequest(pDevExt->mtxComponentFactory);
896 Log2(("deregistering component factories:\n"));
897 if (pDevExt->pComponentFactoryHead)
898 {
899 PSUPDRVFACTORYREG pPrev = NULL;
900 PSUPDRVFACTORYREG pCur = pDevExt->pComponentFactoryHead;
901 while (pCur)
902 {
903 if (pCur->pSession == pSession)
904 {
905 /* unlink it */
906 PSUPDRVFACTORYREG pNext = pCur->pNext;
907 if (pPrev)
908 pPrev->pNext = pNext;
909 else
910 pDevExt->pComponentFactoryHead = pNext;
911
912 /* free it */
913 pCur->pNext = NULL;
914 pCur->pSession = NULL;
915 pCur->pFactory = NULL;
916 RTMemFree(pCur);
917
918 /* next */
919 pCur = pNext;
920 }
921 else
922 {
923 /* next */
924 pPrev = pCur;
925 pCur = pCur->pNext;
926 }
927 }
928 }
929 RTSemFastMutexRelease(pDevExt->mtxComponentFactory);
930 Log2(("deregistering component factories - done\n"));
931
932 /*
933 * Loaded images needs to be dereferenced and possibly freed up.
934 */
935 supdrvLdrLock(pDevExt);
936 Log2(("freeing images:\n"));
937 if (pSession->pLdrUsage)
938 {
939 PSUPDRVLDRUSAGE pUsage = pSession->pLdrUsage;
940 pSession->pLdrUsage = NULL;
941 while (pUsage)
942 {
943 void *pvFree = pUsage;
944 PSUPDRVLDRIMAGE pImage = pUsage->pImage;
945 if (pImage->cUsage > pUsage->cUsage)
946 pImage->cUsage -= pUsage->cUsage;
947 else
948 supdrvLdrFree(pDevExt, pImage);
949 pUsage->pImage = NULL;
950 pUsage = pUsage->pNext;
951 RTMemFree(pvFree);
952 }
953 }
954 supdrvLdrUnlock(pDevExt);
955 Log2(("freeing images - done\n"));
956
957 /*
958 * Unmap the GIP.
959 */
960 Log2(("umapping GIP:\n"));
961 if (pSession->GipMapObjR3 != NIL_RTR0MEMOBJ)
962 {
963 SUPR0GipUnmap(pSession);
964 pSession->fGipReferenced = 0;
965 }
966 Log2(("umapping GIP - done\n"));
967}
968
969
970/**
971 * RTHandleTableDestroy callback used by supdrvCleanupSession.
972 *
973 * @returns IPRT status code, see SUPR0ObjAddRef.
974 * @param hHandleTable The handle table handle. Ignored.
975 * @param pvObj The object pointer.
976 * @param pvCtx Context, the handle type. Ignored.
977 * @param pvUser Session pointer.
978 */
979static DECLCALLBACK(int) supdrvSessionObjHandleRetain(RTHANDLETABLE hHandleTable, void *pvObj, void *pvCtx, void *pvUser)
980{
981 NOREF(pvCtx);
982 NOREF(hHandleTable);
983 return SUPR0ObjAddRefEx(pvObj, (PSUPDRVSESSION)pvUser, true /*fNoBlocking*/);
984}
985
986
987/**
988 * RTHandleTableDestroy callback used by supdrvCleanupSession.
989 *
990 * @param hHandleTable The handle table handle. Ignored.
991 * @param h The handle value. Ignored.
992 * @param pvObj The object pointer.
993 * @param pvCtx Context, the handle type. Ignored.
994 * @param pvUser Session pointer.
995 */
996static DECLCALLBACK(void) supdrvSessionObjHandleDelete(RTHANDLETABLE hHandleTable, uint32_t h, void *pvObj, void *pvCtx, void *pvUser)
997{
998 NOREF(pvCtx);
999 NOREF(h);
1000 NOREF(hHandleTable);
1001 SUPR0ObjRelease(pvObj, (PSUPDRVSESSION)pvUser);
1002}
1003
1004
1005/**
1006 * Fast path I/O Control worker.
1007 *
1008 * @returns VBox status code that should be passed down to ring-3 unchanged.
1009 * @param uIOCtl Function number.
1010 * @param idCpu VMCPU id.
1011 * @param pDevExt Device extention.
1012 * @param pSession Session data.
1013 */
1014int VBOXCALL supdrvIOCtlFast(uintptr_t uIOCtl, VMCPUID idCpu, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession)
1015{
1016 /*
1017 * We check the two prereqs after doing this only to allow the compiler to optimize things better.
1018 */
1019 if (RT_LIKELY(pSession->pVM && pDevExt->pfnVMMR0EntryFast))
1020 {
1021 switch (uIOCtl)
1022 {
1023 case SUP_IOCTL_FAST_DO_RAW_RUN:
1024#ifdef RT_WITH_W64_UNWIND_HACK
1025 supdrvNtWrapVMMR0EntryFast((PFNRT)pDevExt->pfnVMMR0EntryFast, pSession->pVM, idCpu, SUP_VMMR0_DO_RAW_RUN);
1026#else
1027 pDevExt->pfnVMMR0EntryFast(pSession->pVM, idCpu, SUP_VMMR0_DO_RAW_RUN);
1028#endif
1029 break;
1030 case SUP_IOCTL_FAST_DO_HWACC_RUN:
1031#ifdef RT_WITH_W64_UNWIND_HACK
1032 supdrvNtWrapVMMR0EntryFast((PFNRT)pDevExt->pfnVMMR0EntryFast, pSession->pVM, idCpu, SUP_VMMR0_DO_HWACC_RUN);
1033#else
1034 pDevExt->pfnVMMR0EntryFast(pSession->pVM, idCpu, SUP_VMMR0_DO_HWACC_RUN);
1035#endif
1036 break;
1037 case SUP_IOCTL_FAST_DO_NOP:
1038#ifdef RT_WITH_W64_UNWIND_HACK
1039 supdrvNtWrapVMMR0EntryFast((PFNRT)pDevExt->pfnVMMR0EntryFast, pSession->pVM, idCpu, SUP_VMMR0_DO_NOP);
1040#else
1041 pDevExt->pfnVMMR0EntryFast(pSession->pVM, idCpu, SUP_VMMR0_DO_NOP);
1042#endif
1043 break;
1044 default:
1045 return VERR_INTERNAL_ERROR;
1046 }
1047 return VINF_SUCCESS;
1048 }
1049 return VERR_INTERNAL_ERROR;
1050}
1051
1052
1053/**
1054 * Helper for supdrvIOCtl. Check if pszStr contains any character of pszChars.
1055 * We would use strpbrk here if this function would be contained in the RedHat kABI white
1056 * list, see http://www.kerneldrivers.org/RHEL5.
1057 *
1058 * @return 1 if pszStr does contain any character of pszChars, 0 otherwise.
1059 * @param pszStr String to check
1060 * @param pszChars Character set
1061 */
1062static int supdrvCheckInvalidChar(const char *pszStr, const char *pszChars)
1063{
1064 int chCur;
1065 while ((chCur = *pszStr++) != '\0')
1066 {
1067 int ch;
1068 const char *psz = pszChars;
1069 while ((ch = *psz++) != '\0')
1070 if (ch == chCur)
1071 return 1;
1072
1073 }
1074 return 0;
1075}
1076
1077
1078/**
1079 * I/O Control worker.
1080 *
1081 * @returns 0 on success.
1082 * @returns VERR_INVALID_PARAMETER if the request is invalid.
1083 *
1084 * @param uIOCtl Function number.
1085 * @param pDevExt Device extention.
1086 * @param pSession Session data.
1087 * @param pReqHdr The request header.
1088 */
1089int VBOXCALL supdrvIOCtl(uintptr_t uIOCtl, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPREQHDR pReqHdr)
1090{
1091 /*
1092 * Validate the request.
1093 */
1094 /* this first check could probably be omitted as its also done by the OS specific code... */
1095 if (RT_UNLIKELY( (pReqHdr->fFlags & SUPREQHDR_FLAGS_MAGIC_MASK) != SUPREQHDR_FLAGS_MAGIC
1096 || pReqHdr->cbIn < sizeof(*pReqHdr)
1097 || pReqHdr->cbOut < sizeof(*pReqHdr)))
1098 {
1099 OSDBGPRINT(("vboxdrv: Bad ioctl request header; cbIn=%#lx cbOut=%#lx fFlags=%#lx\n",
1100 (long)pReqHdr->cbIn, (long)pReqHdr->cbOut, (long)pReqHdr->fFlags));
1101 return VERR_INVALID_PARAMETER;
1102 }
1103 if (RT_UNLIKELY(uIOCtl == SUP_IOCTL_COOKIE))
1104 {
1105 if (pReqHdr->u32Cookie != SUPCOOKIE_INITIAL_COOKIE)
1106 {
1107 OSDBGPRINT(("SUP_IOCTL_COOKIE: bad cookie %#lx\n", (long)pReqHdr->u32Cookie));
1108 return VERR_INVALID_PARAMETER;
1109 }
1110 }
1111 else if (RT_UNLIKELY( pReqHdr->u32Cookie != pDevExt->u32Cookie
1112 || pReqHdr->u32SessionCookie != pSession->u32Cookie))
1113 {
1114 OSDBGPRINT(("vboxdrv: bad cookie %#lx / %#lx.\n", (long)pReqHdr->u32Cookie, (long)pReqHdr->u32SessionCookie));
1115 return VERR_INVALID_PARAMETER;
1116 }
1117
1118/*
1119 * Validation macros
1120 */
1121#define REQ_CHECK_SIZES_EX(Name, cbInExpect, cbOutExpect) \
1122 do { \
1123 if (RT_UNLIKELY(pReqHdr->cbIn != (cbInExpect) || pReqHdr->cbOut != (cbOutExpect))) \
1124 { \
1125 OSDBGPRINT(( #Name ": Invalid input/output sizes. cbIn=%ld expected %ld. cbOut=%ld expected %ld.\n", \
1126 (long)pReq->Hdr.cbIn, (long)(cbInExpect), (long)pReq->Hdr.cbOut, (long)(cbOutExpect))); \
1127 return pReq->Hdr.rc = VERR_INVALID_PARAMETER; \
1128 } \
1129 } while (0)
1130
1131#define REQ_CHECK_SIZES(Name) REQ_CHECK_SIZES_EX(Name, Name ## _SIZE_IN, Name ## _SIZE_OUT)
1132
1133#define REQ_CHECK_SIZE_IN(Name, cbInExpect) \
1134 do { \
1135 if (RT_UNLIKELY(pReqHdr->cbIn != (cbInExpect))) \
1136 { \
1137 OSDBGPRINT(( #Name ": Invalid input/output sizes. cbIn=%ld expected %ld.\n", \
1138 (long)pReq->Hdr.cbIn, (long)(cbInExpect))); \
1139 return pReq->Hdr.rc = VERR_INVALID_PARAMETER; \
1140 } \
1141 } while (0)
1142
1143#define REQ_CHECK_SIZE_OUT(Name, cbOutExpect) \
1144 do { \
1145 if (RT_UNLIKELY(pReqHdr->cbOut != (cbOutExpect))) \
1146 { \
1147 OSDBGPRINT(( #Name ": Invalid input/output sizes. cbOut=%ld expected %ld.\n", \
1148 (long)pReq->Hdr.cbOut, (long)(cbOutExpect))); \
1149 return pReq->Hdr.rc = VERR_INVALID_PARAMETER; \
1150 } \
1151 } while (0)
1152
1153#define REQ_CHECK_EXPR(Name, expr) \
1154 do { \
1155 if (RT_UNLIKELY(!(expr))) \
1156 { \
1157 OSDBGPRINT(( #Name ": %s\n", #expr)); \
1158 return pReq->Hdr.rc = VERR_INVALID_PARAMETER; \
1159 } \
1160 } while (0)
1161
1162#define REQ_CHECK_EXPR_FMT(expr, fmt) \
1163 do { \
1164 if (RT_UNLIKELY(!(expr))) \
1165 { \
1166 OSDBGPRINT( fmt ); \
1167 return pReq->Hdr.rc = VERR_INVALID_PARAMETER; \
1168 } \
1169 } while (0)
1170
1171
1172 /*
1173 * The switch.
1174 */
1175 switch (SUP_CTL_CODE_NO_SIZE(uIOCtl))
1176 {
1177 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_COOKIE):
1178 {
1179 PSUPCOOKIE pReq = (PSUPCOOKIE)pReqHdr;
1180 REQ_CHECK_SIZES(SUP_IOCTL_COOKIE);
1181 if (strncmp(pReq->u.In.szMagic, SUPCOOKIE_MAGIC, sizeof(pReq->u.In.szMagic)))
1182 {
1183 OSDBGPRINT(("SUP_IOCTL_COOKIE: invalid magic %.16s\n", pReq->u.In.szMagic));
1184 pReq->Hdr.rc = VERR_INVALID_MAGIC;
1185 return 0;
1186 }
1187
1188#if 0
1189 /*
1190 * Call out to the OS specific code and let it do permission checks on the
1191 * client process.
1192 */
1193 if (!supdrvOSValidateClientProcess(pDevExt, pSession))
1194 {
1195 pReq->u.Out.u32Cookie = 0xffffffff;
1196 pReq->u.Out.u32SessionCookie = 0xffffffff;
1197 pReq->u.Out.u32SessionVersion = 0xffffffff;
1198 pReq->u.Out.u32DriverVersion = SUPDRV_IOC_VERSION;
1199 pReq->u.Out.pSession = NULL;
1200 pReq->u.Out.cFunctions = 0;
1201 pReq->Hdr.rc = VERR_PERMISSION_DENIED;
1202 return 0;
1203 }
1204#endif
1205
1206 /*
1207 * Match the version.
1208 * The current logic is very simple, match the major interface version.
1209 */
1210 if ( pReq->u.In.u32MinVersion > SUPDRV_IOC_VERSION
1211 || (pReq->u.In.u32MinVersion & 0xffff0000) != (SUPDRV_IOC_VERSION & 0xffff0000))
1212 {
1213 OSDBGPRINT(("SUP_IOCTL_COOKIE: Version mismatch. Requested: %#x Min: %#x Current: %#x\n",
1214 pReq->u.In.u32ReqVersion, pReq->u.In.u32MinVersion, SUPDRV_IOC_VERSION));
1215 pReq->u.Out.u32Cookie = 0xffffffff;
1216 pReq->u.Out.u32SessionCookie = 0xffffffff;
1217 pReq->u.Out.u32SessionVersion = 0xffffffff;
1218 pReq->u.Out.u32DriverVersion = SUPDRV_IOC_VERSION;
1219 pReq->u.Out.pSession = NULL;
1220 pReq->u.Out.cFunctions = 0;
1221 pReq->Hdr.rc = VERR_VERSION_MISMATCH;
1222 return 0;
1223 }
1224
1225 /*
1226 * Fill in return data and be gone.
1227 * N.B. The first one to change SUPDRV_IOC_VERSION shall makes sure that
1228 * u32SessionVersion <= u32ReqVersion!
1229 */
1230 /** @todo Somehow validate the client and negotiate a secure cookie... */
1231 pReq->u.Out.u32Cookie = pDevExt->u32Cookie;
1232 pReq->u.Out.u32SessionCookie = pSession->u32Cookie;
1233 pReq->u.Out.u32SessionVersion = SUPDRV_IOC_VERSION;
1234 pReq->u.Out.u32DriverVersion = SUPDRV_IOC_VERSION;
1235 pReq->u.Out.pSession = pSession;
1236 pReq->u.Out.cFunctions = sizeof(g_aFunctions) / sizeof(g_aFunctions[0]);
1237 pReq->Hdr.rc = VINF_SUCCESS;
1238 return 0;
1239 }
1240
1241 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_QUERY_FUNCS(0)):
1242 {
1243 /* validate */
1244 PSUPQUERYFUNCS pReq = (PSUPQUERYFUNCS)pReqHdr;
1245 REQ_CHECK_SIZES_EX(SUP_IOCTL_QUERY_FUNCS, SUP_IOCTL_QUERY_FUNCS_SIZE_IN, SUP_IOCTL_QUERY_FUNCS_SIZE_OUT(RT_ELEMENTS(g_aFunctions)));
1246
1247 /* execute */
1248 pReq->u.Out.cFunctions = RT_ELEMENTS(g_aFunctions);
1249 memcpy(&pReq->u.Out.aFunctions[0], g_aFunctions, sizeof(g_aFunctions));
1250 pReq->Hdr.rc = VINF_SUCCESS;
1251 return 0;
1252 }
1253
1254 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_PAGE_LOCK):
1255 {
1256 /* validate */
1257 PSUPPAGELOCK pReq = (PSUPPAGELOCK)pReqHdr;
1258 REQ_CHECK_SIZE_IN(SUP_IOCTL_PAGE_LOCK, SUP_IOCTL_PAGE_LOCK_SIZE_IN);
1259 REQ_CHECK_SIZE_OUT(SUP_IOCTL_PAGE_LOCK, SUP_IOCTL_PAGE_LOCK_SIZE_OUT(pReq->u.In.cPages));
1260 REQ_CHECK_EXPR(SUP_IOCTL_PAGE_LOCK, pReq->u.In.cPages > 0);
1261 REQ_CHECK_EXPR(SUP_IOCTL_PAGE_LOCK, pReq->u.In.pvR3 >= PAGE_SIZE);
1262
1263 /* execute */
1264 pReq->Hdr.rc = SUPR0LockMem(pSession, pReq->u.In.pvR3, pReq->u.In.cPages, &pReq->u.Out.aPages[0]);
1265 if (RT_FAILURE(pReq->Hdr.rc))
1266 pReq->Hdr.cbOut = sizeof(pReq->Hdr);
1267 return 0;
1268 }
1269
1270 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_PAGE_UNLOCK):
1271 {
1272 /* validate */
1273 PSUPPAGEUNLOCK pReq = (PSUPPAGEUNLOCK)pReqHdr;
1274 REQ_CHECK_SIZES(SUP_IOCTL_PAGE_UNLOCK);
1275
1276 /* execute */
1277 pReq->Hdr.rc = SUPR0UnlockMem(pSession, pReq->u.In.pvR3);
1278 return 0;
1279 }
1280
1281 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_CONT_ALLOC):
1282 {
1283 /* validate */
1284 PSUPCONTALLOC pReq = (PSUPCONTALLOC)pReqHdr;
1285 REQ_CHECK_SIZES(SUP_IOCTL_CONT_ALLOC);
1286
1287 /* execute */
1288 pReq->Hdr.rc = SUPR0ContAlloc(pSession, pReq->u.In.cPages, &pReq->u.Out.pvR0, &pReq->u.Out.pvR3, &pReq->u.Out.HCPhys);
1289 if (RT_FAILURE(pReq->Hdr.rc))
1290 pReq->Hdr.cbOut = sizeof(pReq->Hdr);
1291 return 0;
1292 }
1293
1294 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_CONT_FREE):
1295 {
1296 /* validate */
1297 PSUPCONTFREE pReq = (PSUPCONTFREE)pReqHdr;
1298 REQ_CHECK_SIZES(SUP_IOCTL_CONT_FREE);
1299
1300 /* execute */
1301 pReq->Hdr.rc = SUPR0ContFree(pSession, (RTHCUINTPTR)pReq->u.In.pvR3);
1302 return 0;
1303 }
1304
1305 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_LDR_OPEN):
1306 {
1307 /* validate */
1308 PSUPLDROPEN pReq = (PSUPLDROPEN)pReqHdr;
1309 REQ_CHECK_SIZES(SUP_IOCTL_LDR_OPEN);
1310 REQ_CHECK_EXPR(SUP_IOCTL_LDR_OPEN, pReq->u.In.cbImageWithTabs > 0);
1311 REQ_CHECK_EXPR(SUP_IOCTL_LDR_OPEN, pReq->u.In.cbImageWithTabs < 16*_1M);
1312 REQ_CHECK_EXPR(SUP_IOCTL_LDR_OPEN, pReq->u.In.cbImageBits > 0);
1313 REQ_CHECK_EXPR(SUP_IOCTL_LDR_OPEN, pReq->u.In.cbImageBits > 0);
1314 REQ_CHECK_EXPR(SUP_IOCTL_LDR_OPEN, pReq->u.In.cbImageBits < pReq->u.In.cbImageWithTabs);
1315 REQ_CHECK_EXPR(SUP_IOCTL_LDR_OPEN, pReq->u.In.szName[0]);
1316 REQ_CHECK_EXPR(SUP_IOCTL_LDR_OPEN, memchr(pReq->u.In.szName, '\0', sizeof(pReq->u.In.szName)));
1317 REQ_CHECK_EXPR(SUP_IOCTL_LDR_OPEN, !supdrvCheckInvalidChar(pReq->u.In.szName, ";:()[]{}/\\|&*%#@!~`\"'"));
1318 REQ_CHECK_EXPR(SUP_IOCTL_LDR_OPEN, memchr(pReq->u.In.szFilename, '\0', sizeof(pReq->u.In.szFilename)));
1319
1320 /* execute */
1321 pReq->Hdr.rc = supdrvIOCtl_LdrOpen(pDevExt, pSession, pReq);
1322 return 0;
1323 }
1324
1325 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_LDR_LOAD):
1326 {
1327 /* validate */
1328 PSUPLDRLOAD pReq = (PSUPLDRLOAD)pReqHdr;
1329 REQ_CHECK_EXPR(Name, pReq->Hdr.cbIn >= sizeof(*pReq));
1330 REQ_CHECK_SIZES_EX(SUP_IOCTL_LDR_LOAD, SUP_IOCTL_LDR_LOAD_SIZE_IN(pReq->u.In.cbImageWithTabs), SUP_IOCTL_LDR_LOAD_SIZE_OUT);
1331 REQ_CHECK_EXPR(SUP_IOCTL_LDR_LOAD, pReq->u.In.cSymbols <= 16384);
1332 REQ_CHECK_EXPR_FMT( !pReq->u.In.cSymbols
1333 || ( pReq->u.In.offSymbols < pReq->u.In.cbImageWithTabs
1334 && pReq->u.In.offSymbols + pReq->u.In.cSymbols * sizeof(SUPLDRSYM) <= pReq->u.In.cbImageWithTabs),
1335 ("SUP_IOCTL_LDR_LOAD: offSymbols=%#lx cSymbols=%#lx cbImageWithTabs=%#lx\n", (long)pReq->u.In.offSymbols,
1336 (long)pReq->u.In.cSymbols, (long)pReq->u.In.cbImageWithTabs));
1337 REQ_CHECK_EXPR_FMT( !pReq->u.In.cbStrTab
1338 || ( pReq->u.In.offStrTab < pReq->u.In.cbImageWithTabs
1339 && pReq->u.In.offStrTab + pReq->u.In.cbStrTab <= pReq->u.In.cbImageWithTabs
1340 && pReq->u.In.cbStrTab <= pReq->u.In.cbImageWithTabs),
1341 ("SUP_IOCTL_LDR_LOAD: offStrTab=%#lx cbStrTab=%#lx cbImageWithTabs=%#lx\n", (long)pReq->u.In.offStrTab,
1342 (long)pReq->u.In.cbStrTab, (long)pReq->u.In.cbImageWithTabs));
1343
1344 if (pReq->u.In.cSymbols)
1345 {
1346 uint32_t i;
1347 PSUPLDRSYM paSyms = (PSUPLDRSYM)&pReq->u.In.achImage[pReq->u.In.offSymbols];
1348 for (i = 0; i < pReq->u.In.cSymbols; i++)
1349 {
1350 REQ_CHECK_EXPR_FMT(paSyms[i].offSymbol < pReq->u.In.cbImageWithTabs,
1351 ("SUP_IOCTL_LDR_LOAD: sym #%ld: symb off %#lx (max=%#lx)\n", (long)i, (long)paSyms[i].offSymbol, (long)pReq->u.In.cbImageWithTabs));
1352 REQ_CHECK_EXPR_FMT(paSyms[i].offName < pReq->u.In.cbStrTab,
1353 ("SUP_IOCTL_LDR_LOAD: sym #%ld: name off %#lx (max=%#lx)\n", (long)i, (long)paSyms[i].offName, (long)pReq->u.In.cbImageWithTabs));
1354 REQ_CHECK_EXPR_FMT(memchr(&pReq->u.In.achImage[pReq->u.In.offStrTab + paSyms[i].offName], '\0', pReq->u.In.cbStrTab - paSyms[i].offName),
1355 ("SUP_IOCTL_LDR_LOAD: sym #%ld: unterminated name! (%#lx / %#lx)\n", (long)i, (long)paSyms[i].offName, (long)pReq->u.In.cbImageWithTabs));
1356 }
1357 }
1358
1359 /* execute */
1360 pReq->Hdr.rc = supdrvIOCtl_LdrLoad(pDevExt, pSession, pReq);
1361 return 0;
1362 }
1363
1364 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_LDR_FREE):
1365 {
1366 /* validate */
1367 PSUPLDRFREE pReq = (PSUPLDRFREE)pReqHdr;
1368 REQ_CHECK_SIZES(SUP_IOCTL_LDR_FREE);
1369
1370 /* execute */
1371 pReq->Hdr.rc = supdrvIOCtl_LdrFree(pDevExt, pSession, pReq);
1372 return 0;
1373 }
1374
1375 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_LDR_GET_SYMBOL):
1376 {
1377 /* validate */
1378 PSUPLDRGETSYMBOL pReq = (PSUPLDRGETSYMBOL)pReqHdr;
1379 REQ_CHECK_SIZES(SUP_IOCTL_LDR_GET_SYMBOL);
1380 REQ_CHECK_EXPR(SUP_IOCTL_LDR_GET_SYMBOL, memchr(pReq->u.In.szSymbol, '\0', sizeof(pReq->u.In.szSymbol)));
1381
1382 /* execute */
1383 pReq->Hdr.rc = supdrvIOCtl_LdrGetSymbol(pDevExt, pSession, pReq);
1384 return 0;
1385 }
1386
1387 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_CALL_VMMR0(0)):
1388 {
1389 /* validate */
1390 PSUPCALLVMMR0 pReq = (PSUPCALLVMMR0)pReqHdr;
1391 Log4(("SUP_IOCTL_CALL_VMMR0: op=%u in=%u arg=%RX64 p/t=%RTproc/%RTthrd\n",
1392 pReq->u.In.uOperation, pReq->Hdr.cbIn, pReq->u.In.u64Arg, RTProcSelf(), RTThreadNativeSelf()));
1393
1394 if (pReq->Hdr.cbIn == SUP_IOCTL_CALL_VMMR0_SIZE(0))
1395 {
1396 REQ_CHECK_SIZES_EX(SUP_IOCTL_CALL_VMMR0, SUP_IOCTL_CALL_VMMR0_SIZE_IN(0), SUP_IOCTL_CALL_VMMR0_SIZE_OUT(0));
1397
1398 /* execute */
1399 if (RT_LIKELY(pDevExt->pfnVMMR0EntryEx))
1400#ifdef RT_WITH_W64_UNWIND_HACK
1401 pReq->Hdr.rc = supdrvNtWrapVMMR0EntryEx((PFNRT)pDevExt->pfnVMMR0EntryEx, pReq->u.In.pVMR0, pReq->u.In.idCpu, pReq->u.In.uOperation, NULL, pReq->u.In.u64Arg, pSession);
1402#else
1403 pReq->Hdr.rc = pDevExt->pfnVMMR0EntryEx(pReq->u.In.pVMR0, pReq->u.In.idCpu, pReq->u.In.uOperation, NULL, pReq->u.In.u64Arg, pSession);
1404#endif
1405 else
1406 pReq->Hdr.rc = VERR_WRONG_ORDER;
1407 }
1408 else
1409 {
1410 PSUPVMMR0REQHDR pVMMReq = (PSUPVMMR0REQHDR)&pReq->abReqPkt[0];
1411 REQ_CHECK_EXPR_FMT(pReq->Hdr.cbIn >= SUP_IOCTL_CALL_VMMR0_SIZE(sizeof(SUPVMMR0REQHDR)),
1412 ("SUP_IOCTL_CALL_VMMR0: cbIn=%#x < %#lx\n", pReq->Hdr.cbIn, SUP_IOCTL_CALL_VMMR0_SIZE(sizeof(SUPVMMR0REQHDR))));
1413 REQ_CHECK_EXPR(SUP_IOCTL_CALL_VMMR0, pVMMReq->u32Magic == SUPVMMR0REQHDR_MAGIC);
1414 REQ_CHECK_SIZES_EX(SUP_IOCTL_CALL_VMMR0, SUP_IOCTL_CALL_VMMR0_SIZE_IN(pVMMReq->cbReq), SUP_IOCTL_CALL_VMMR0_SIZE_OUT(pVMMReq->cbReq));
1415
1416 /* execute */
1417 if (RT_LIKELY(pDevExt->pfnVMMR0EntryEx))
1418#ifdef RT_WITH_W64_UNWIND_HACK
1419 pReq->Hdr.rc = supdrvNtWrapVMMR0EntryEx((PFNRT)pDevExt->pfnVMMR0EntryEx, pReq->u.In.pVMR0, pReq->u.In.idCpu, pReq->u.In.uOperation, pVMMReq, pReq->u.In.u64Arg, pSession);
1420#else
1421 pReq->Hdr.rc = pDevExt->pfnVMMR0EntryEx(pReq->u.In.pVMR0, pReq->u.In.idCpu, pReq->u.In.uOperation, pVMMReq, pReq->u.In.u64Arg, pSession);
1422#endif
1423 else
1424 pReq->Hdr.rc = VERR_WRONG_ORDER;
1425 }
1426
1427 if ( RT_FAILURE(pReq->Hdr.rc)
1428 && pReq->Hdr.rc != VERR_INTERRUPTED
1429 && pReq->Hdr.rc != VERR_TIMEOUT)
1430 Log(("SUP_IOCTL_CALL_VMMR0: rc=%Rrc op=%u out=%u arg=%RX64 p/t=%RTproc/%RTthrd\n",
1431 pReq->Hdr.rc, pReq->u.In.uOperation, pReq->Hdr.cbOut, pReq->u.In.u64Arg, RTProcSelf(), RTThreadNativeSelf()));
1432 else
1433 Log4(("SUP_IOCTL_CALL_VMMR0: rc=%Rrc op=%u out=%u arg=%RX64 p/t=%RTproc/%RTthrd\n",
1434 pReq->Hdr.rc, pReq->u.In.uOperation, pReq->Hdr.cbOut, pReq->u.In.u64Arg, RTProcSelf(), RTThreadNativeSelf()));
1435 return 0;
1436 }
1437
1438 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_GET_PAGING_MODE):
1439 {
1440 /* validate */
1441 PSUPGETPAGINGMODE pReq = (PSUPGETPAGINGMODE)pReqHdr;
1442 REQ_CHECK_SIZES(SUP_IOCTL_GET_PAGING_MODE);
1443
1444 /* execute */
1445 pReq->Hdr.rc = VINF_SUCCESS;
1446 pReq->u.Out.enmMode = SUPR0GetPagingMode();
1447 return 0;
1448 }
1449
1450 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_LOW_ALLOC):
1451 {
1452 /* validate */
1453 PSUPLOWALLOC pReq = (PSUPLOWALLOC)pReqHdr;
1454 REQ_CHECK_EXPR(SUP_IOCTL_LOW_ALLOC, pReq->Hdr.cbIn <= SUP_IOCTL_LOW_ALLOC_SIZE_IN);
1455 REQ_CHECK_SIZES_EX(SUP_IOCTL_LOW_ALLOC, SUP_IOCTL_LOW_ALLOC_SIZE_IN, SUP_IOCTL_LOW_ALLOC_SIZE_OUT(pReq->u.In.cPages));
1456
1457 /* execute */
1458 pReq->Hdr.rc = SUPR0LowAlloc(pSession, pReq->u.In.cPages, &pReq->u.Out.pvR0, &pReq->u.Out.pvR3, &pReq->u.Out.aPages[0]);
1459 if (RT_FAILURE(pReq->Hdr.rc))
1460 pReq->Hdr.cbOut = sizeof(pReq->Hdr);
1461 return 0;
1462 }
1463
1464 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_LOW_FREE):
1465 {
1466 /* validate */
1467 PSUPLOWFREE pReq = (PSUPLOWFREE)pReqHdr;
1468 REQ_CHECK_SIZES(SUP_IOCTL_LOW_FREE);
1469
1470 /* execute */
1471 pReq->Hdr.rc = SUPR0LowFree(pSession, (RTHCUINTPTR)pReq->u.In.pvR3);
1472 return 0;
1473 }
1474
1475 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_GIP_MAP):
1476 {
1477 /* validate */
1478 PSUPGIPMAP pReq = (PSUPGIPMAP)pReqHdr;
1479 REQ_CHECK_SIZES(SUP_IOCTL_GIP_MAP);
1480
1481 /* execute */
1482 pReq->Hdr.rc = SUPR0GipMap(pSession, &pReq->u.Out.pGipR3, &pReq->u.Out.HCPhysGip);
1483 if (RT_SUCCESS(pReq->Hdr.rc))
1484 pReq->u.Out.pGipR0 = pDevExt->pGip;
1485 return 0;
1486 }
1487
1488 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_GIP_UNMAP):
1489 {
1490 /* validate */
1491 PSUPGIPUNMAP pReq = (PSUPGIPUNMAP)pReqHdr;
1492 REQ_CHECK_SIZES(SUP_IOCTL_GIP_UNMAP);
1493
1494 /* execute */
1495 pReq->Hdr.rc = SUPR0GipUnmap(pSession);
1496 return 0;
1497 }
1498
1499 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_SET_VM_FOR_FAST):
1500 {
1501 /* validate */
1502 PSUPSETVMFORFAST pReq = (PSUPSETVMFORFAST)pReqHdr;
1503 REQ_CHECK_SIZES(SUP_IOCTL_SET_VM_FOR_FAST);
1504 REQ_CHECK_EXPR_FMT( !pReq->u.In.pVMR0
1505 || ( VALID_PTR(pReq->u.In.pVMR0)
1506 && !((uintptr_t)pReq->u.In.pVMR0 & (PAGE_SIZE - 1))),
1507 ("SUP_IOCTL_SET_VM_FOR_FAST: pVMR0=%p!\n", pReq->u.In.pVMR0));
1508 /* execute */
1509 pSession->pVM = pReq->u.In.pVMR0;
1510 pReq->Hdr.rc = VINF_SUCCESS;
1511 return 0;
1512 }
1513
1514 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_PAGE_ALLOC_EX):
1515 {
1516 /* validate */
1517 PSUPPAGEALLOCEX pReq = (PSUPPAGEALLOCEX)pReqHdr;
1518 REQ_CHECK_EXPR(SUP_IOCTL_PAGE_ALLOC_EX, pReq->Hdr.cbIn <= SUP_IOCTL_PAGE_ALLOC_EX_SIZE_IN);
1519 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));
1520 REQ_CHECK_EXPR_FMT(pReq->u.In.fKernelMapping || pReq->u.In.fUserMapping,
1521 ("SUP_IOCTL_PAGE_ALLOC_EX: No mapping requested!\n"));
1522 REQ_CHECK_EXPR_FMT(pReq->u.In.fUserMapping,
1523 ("SUP_IOCTL_PAGE_ALLOC_EX: Must have user mapping!\n"));
1524 REQ_CHECK_EXPR_FMT(!pReq->u.In.fReserved0 && !pReq->u.In.fReserved1,
1525 ("SUP_IOCTL_PAGE_ALLOC_EX: fReserved0=%d fReserved1=%d\n", pReq->u.In.fReserved0, pReq->u.In.fReserved1));
1526
1527 /* execute */
1528 pReq->Hdr.rc = SUPR0PageAllocEx(pSession, pReq->u.In.cPages, 0 /* fFlags */,
1529 pReq->u.In.fUserMapping ? &pReq->u.Out.pvR3 : NULL,
1530 pReq->u.In.fKernelMapping ? &pReq->u.Out.pvR0 : NULL,
1531 &pReq->u.Out.aPages[0]);
1532 if (RT_FAILURE(pReq->Hdr.rc))
1533 pReq->Hdr.cbOut = sizeof(pReq->Hdr);
1534 return 0;
1535 }
1536
1537 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_PAGE_MAP_KERNEL):
1538 {
1539 /* validate */
1540 PSUPPAGEMAPKERNEL pReq = (PSUPPAGEMAPKERNEL)pReqHdr;
1541 REQ_CHECK_SIZES(SUP_IOCTL_PAGE_MAP_KERNEL);
1542 REQ_CHECK_EXPR_FMT(!pReq->u.In.fFlags, ("SUP_IOCTL_PAGE_MAP_KERNEL: fFlags=%#x! MBZ\n", pReq->u.In.fFlags));
1543 REQ_CHECK_EXPR_FMT(!(pReq->u.In.offSub & PAGE_OFFSET_MASK), ("SUP_IOCTL_PAGE_MAP_KERNEL: offSub=%#x\n", pReq->u.In.offSub));
1544 REQ_CHECK_EXPR_FMT(pReq->u.In.cbSub && !(pReq->u.In.cbSub & PAGE_OFFSET_MASK),
1545 ("SUP_IOCTL_PAGE_MAP_KERNEL: cbSub=%#x\n", pReq->u.In.cbSub));
1546
1547 /* execute */
1548 pReq->Hdr.rc = SUPR0PageMapKernel(pSession, pReq->u.In.pvR3, pReq->u.In.offSub, pReq->u.In.cbSub,
1549 pReq->u.In.fFlags, &pReq->u.Out.pvR0);
1550 if (RT_FAILURE(pReq->Hdr.rc))
1551 pReq->Hdr.cbOut = sizeof(pReq->Hdr);
1552 return 0;
1553 }
1554
1555 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_PAGE_PROTECT):
1556 {
1557 /* validate */
1558 PSUPPAGEPROTECT pReq = (PSUPPAGEPROTECT)pReqHdr;
1559 REQ_CHECK_SIZES(SUP_IOCTL_PAGE_PROTECT);
1560 REQ_CHECK_EXPR_FMT(!(pReq->u.In.fProt & ~(RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC | RTMEM_PROT_NONE)),
1561 ("SUP_IOCTL_PAGE_PROTECT: fProt=%#x!\n", pReq->u.In.fProt));
1562 REQ_CHECK_EXPR_FMT(!(pReq->u.In.offSub & PAGE_OFFSET_MASK), ("SUP_IOCTL_PAGE_PROTECT: offSub=%#x\n", pReq->u.In.offSub));
1563 REQ_CHECK_EXPR_FMT(pReq->u.In.cbSub && !(pReq->u.In.cbSub & PAGE_OFFSET_MASK),
1564 ("SUP_IOCTL_PAGE_PROTECT: cbSub=%#x\n", pReq->u.In.cbSub));
1565
1566 /* execute */
1567 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);
1568 return 0;
1569 }
1570
1571 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_PAGE_FREE):
1572 {
1573 /* validate */
1574 PSUPPAGEFREE pReq = (PSUPPAGEFREE)pReqHdr;
1575 REQ_CHECK_SIZES(SUP_IOCTL_PAGE_FREE);
1576
1577 /* execute */
1578 pReq->Hdr.rc = SUPR0PageFree(pSession, pReq->u.In.pvR3);
1579 return 0;
1580 }
1581
1582 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_CALL_SERVICE(0)):
1583 {
1584 /* validate */
1585 PSUPCALLSERVICE pReq = (PSUPCALLSERVICE)pReqHdr;
1586 Log4(("SUP_IOCTL_CALL_SERVICE: op=%u in=%u arg=%RX64 p/t=%RTproc/%RTthrd\n",
1587 pReq->u.In.uOperation, pReq->Hdr.cbIn, pReq->u.In.u64Arg, RTProcSelf(), RTThreadNativeSelf()));
1588
1589 if (pReq->Hdr.cbIn == SUP_IOCTL_CALL_SERVICE_SIZE(0))
1590 REQ_CHECK_SIZES_EX(SUP_IOCTL_CALL_SERVICE, SUP_IOCTL_CALL_SERVICE_SIZE_IN(0), SUP_IOCTL_CALL_SERVICE_SIZE_OUT(0));
1591 else
1592 {
1593 PSUPR0SERVICEREQHDR pSrvReq = (PSUPR0SERVICEREQHDR)&pReq->abReqPkt[0];
1594 REQ_CHECK_EXPR_FMT(pReq->Hdr.cbIn >= SUP_IOCTL_CALL_SERVICE_SIZE(sizeof(SUPR0SERVICEREQHDR)),
1595 ("SUP_IOCTL_CALL_SERVICE: cbIn=%#x < %#lx\n", pReq->Hdr.cbIn, SUP_IOCTL_CALL_SERVICE_SIZE(sizeof(SUPR0SERVICEREQHDR))));
1596 REQ_CHECK_EXPR(SUP_IOCTL_CALL_SERVICE, pSrvReq->u32Magic == SUPR0SERVICEREQHDR_MAGIC);
1597 REQ_CHECK_SIZES_EX(SUP_IOCTL_CALL_SERVICE, SUP_IOCTL_CALL_SERVICE_SIZE_IN(pSrvReq->cbReq), SUP_IOCTL_CALL_SERVICE_SIZE_OUT(pSrvReq->cbReq));
1598 }
1599 REQ_CHECK_EXPR(SUP_IOCTL_CALL_SERVICE, memchr(pReq->u.In.szName, '\0', sizeof(pReq->u.In.szName)));
1600
1601 /* execute */
1602 pReq->Hdr.rc = supdrvIOCtl_CallServiceModule(pDevExt, pSession, pReq);
1603 return 0;
1604 }
1605
1606 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_LOGGER_SETTINGS(0)):
1607 {
1608 /* validate */
1609 PSUPLOGGERSETTINGS pReq = (PSUPLOGGERSETTINGS)pReqHdr;
1610 size_t cbStrTab;
1611 REQ_CHECK_SIZE_OUT(SUP_IOCTL_LOGGER_SETTINGS, SUP_IOCTL_LOGGER_SETTINGS_SIZE_OUT);
1612 REQ_CHECK_EXPR(SUP_IOCTL_LOGGER_SETTINGS, pReq->Hdr.cbIn >= SUP_IOCTL_LOGGER_SETTINGS_SIZE_IN(1));
1613 cbStrTab = pReq->Hdr.cbIn - SUP_IOCTL_LOGGER_SETTINGS_SIZE_IN(0);
1614 REQ_CHECK_EXPR(SUP_IOCTL_LOGGER_SETTINGS, pReq->u.In.offGroups < cbStrTab);
1615 REQ_CHECK_EXPR(SUP_IOCTL_LOGGER_SETTINGS, pReq->u.In.offFlags < cbStrTab);
1616 REQ_CHECK_EXPR(SUP_IOCTL_LOGGER_SETTINGS, pReq->u.In.offDestination < cbStrTab);
1617 REQ_CHECK_EXPR_FMT(pReq->u.In.szStrings[cbStrTab - 1] == '\0',
1618 ("SUP_IOCTL_LOGGER_SETTINGS: cbIn=%#x cbStrTab=%#zx LastChar=%d\n",
1619 pReq->Hdr.cbIn, cbStrTab, pReq->u.In.szStrings[cbStrTab - 1]));
1620 REQ_CHECK_EXPR(SUP_IOCTL_LOGGER_SETTINGS, pReq->u.In.fWhich <= SUPLOGGERSETTINGS_WHICH_RELEASE);
1621 REQ_CHECK_EXPR(SUP_IOCTL_LOGGER_SETTINGS, pReq->u.In.fWhat <= SUPLOGGERSETTINGS_WHAT_DESTROY);
1622
1623 /* execute */
1624 pReq->Hdr.rc = supdrvIOCtl_LoggerSettings(pDevExt, pSession, pReq);
1625 return 0;
1626 }
1627
1628 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_SEM_CREATE):
1629 {
1630 /* validate */
1631 PSUPSEMCREATE pReq = (PSUPSEMCREATE)pReqHdr;
1632 REQ_CHECK_SIZES_EX(SUP_IOCTL_SEM_CREATE, SUP_IOCTL_SEM_CREATE_SIZE_IN, SUP_IOCTL_SEM_CREATE_SIZE_OUT);
1633
1634 /* execute */
1635 switch (pReq->u.In.uType)
1636 {
1637 case SUP_SEM_TYPE_EVENT:
1638 {
1639 SUPSEMEVENT hEvent;
1640 pReq->Hdr.rc = SUPSemEventCreate(pSession, &hEvent);
1641 pReq->u.Out.hSem = (uint32_t)(uintptr_t)hEvent;
1642 break;
1643 }
1644
1645 case SUP_SEM_TYPE_EVENT_MULTI:
1646 {
1647 SUPSEMEVENTMULTI hEventMulti;
1648 pReq->Hdr.rc = SUPSemEventMultiCreate(pSession, &hEventMulti);
1649 pReq->u.Out.hSem = (uint32_t)(uintptr_t)hEventMulti;
1650 break;
1651 }
1652
1653 default:
1654 pReq->Hdr.rc = VERR_INVALID_PARAMETER;
1655 break;
1656 }
1657 return 0;
1658 }
1659
1660 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_SEM_OP):
1661 {
1662 /* validate */
1663 PSUPSEMOP pReq = (PSUPSEMOP)pReqHdr;
1664 REQ_CHECK_SIZES_EX(SUP_IOCTL_SEM_OP, SUP_IOCTL_SEM_OP_SIZE_IN, SUP_IOCTL_SEM_OP_SIZE_OUT);
1665
1666 /* execute */
1667 switch (pReq->u.In.uType)
1668 {
1669 case SUP_SEM_TYPE_EVENT:
1670 {
1671 SUPSEMEVENT hEvent = (SUPSEMEVENT)(uintptr_t)pReq->u.In.hSem;
1672 switch (pReq->u.In.uOp)
1673 {
1674 case SUPSEMOP_WAIT:
1675 pReq->Hdr.rc = SUPSemEventWaitNoResume(pSession, hEvent, pReq->u.In.cMillies);
1676 break;
1677 case SUPSEMOP_SIGNAL:
1678 pReq->Hdr.rc = SUPSemEventSignal(pSession, hEvent);
1679 break;
1680 case SUPSEMOP_CLOSE:
1681 pReq->Hdr.rc = SUPSemEventClose(pSession, hEvent);
1682 break;
1683 case SUPSEMOP_RESET:
1684 default:
1685 pReq->Hdr.rc = VERR_INVALID_FUNCTION;
1686 break;
1687 }
1688 break;
1689 }
1690
1691 case SUP_SEM_TYPE_EVENT_MULTI:
1692 {
1693 SUPSEMEVENTMULTI hEventMulti = (SUPSEMEVENTMULTI)(uintptr_t)pReq->u.In.hSem;
1694 switch (pReq->u.In.uOp)
1695 {
1696 case SUPSEMOP_WAIT:
1697 pReq->Hdr.rc = SUPSemEventMultiWaitNoResume(pSession, hEventMulti, pReq->u.In.cMillies);
1698 break;
1699 case SUPSEMOP_SIGNAL:
1700 pReq->Hdr.rc = SUPSemEventMultiSignal(pSession, hEventMulti);
1701 break;
1702 case SUPSEMOP_CLOSE:
1703 pReq->Hdr.rc = SUPSemEventMultiClose(pSession, hEventMulti);
1704 break;
1705 case SUPSEMOP_RESET:
1706 pReq->Hdr.rc = SUPSemEventMultiReset(pSession, hEventMulti);
1707 break;
1708 default:
1709 pReq->Hdr.rc = VERR_INVALID_FUNCTION;
1710 break;
1711 }
1712 break;
1713 }
1714
1715 default:
1716 pReq->Hdr.rc = VERR_INVALID_PARAMETER;
1717 break;
1718 }
1719 return 0;
1720 }
1721
1722 case SUP_CTL_CODE_NO_SIZE(SUP_IOCTL_VT_CAPS):
1723 {
1724 /* validate */
1725 PSUPVTCAPS pReq = (PSUPVTCAPS)pReqHdr;
1726 REQ_CHECK_SIZES(SUP_IOCTL_VT_CAPS);
1727 REQ_CHECK_EXPR(SUP_IOCTL_VT_CAPS, pReq->Hdr.cbIn <= SUP_IOCTL_VT_CAPS_SIZE_IN);
1728
1729 /* execute */
1730 pReq->Hdr.rc = SUPR0QueryVTCaps(pSession, &pReq->u.Out.Caps);
1731 if (RT_FAILURE(pReq->Hdr.rc))
1732 pReq->Hdr.cbOut = sizeof(pReq->Hdr);
1733 return 0;
1734 }
1735
1736 default:
1737 Log(("Unknown IOCTL %#lx\n", (long)uIOCtl));
1738 break;
1739 }
1740 return SUPDRV_ERR_GENERAL_FAILURE;
1741}
1742
1743
1744/**
1745 * Inter-Driver Communcation (IDC) worker.
1746 *
1747 * @returns VBox status code.
1748 * @retval VINF_SUCCESS on success.
1749 * @retval VERR_INVALID_PARAMETER if the request is invalid.
1750 * @retval VERR_NOT_SUPPORTED if the request isn't supported.
1751 *
1752 * @param uReq The request (function) code.
1753 * @param pDevExt Device extention.
1754 * @param pSession Session data.
1755 * @param pReqHdr The request header.
1756 */
1757int VBOXCALL supdrvIDC(uintptr_t uReq, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVIDCREQHDR pReqHdr)
1758{
1759 /*
1760 * The OS specific code has already validated the pSession
1761 * pointer, and the request size being greater or equal to
1762 * size of the header.
1763 *
1764 * So, just check that pSession is a kernel context session.
1765 */
1766 if (RT_UNLIKELY( pSession
1767 && pSession->R0Process != NIL_RTR0PROCESS))
1768 return VERR_INVALID_PARAMETER;
1769
1770/*
1771 * Validation macro.
1772 */
1773#define REQ_CHECK_IDC_SIZE(Name, cbExpect) \
1774 do { \
1775 if (RT_UNLIKELY(pReqHdr->cb != (cbExpect))) \
1776 { \
1777 OSDBGPRINT(( #Name ": Invalid input/output sizes. cb=%ld expected %ld.\n", \
1778 (long)pReqHdr->cb, (long)(cbExpect))); \
1779 return pReqHdr->rc = VERR_INVALID_PARAMETER; \
1780 } \
1781 } while (0)
1782
1783 switch (uReq)
1784 {
1785 case SUPDRV_IDC_REQ_CONNECT:
1786 {
1787 PSUPDRVIDCREQCONNECT pReq = (PSUPDRVIDCREQCONNECT)pReqHdr;
1788 REQ_CHECK_IDC_SIZE(SUPDRV_IDC_REQ_CONNECT, sizeof(*pReq));
1789
1790 /*
1791 * Validate the cookie and other input.
1792 */
1793 if (pReq->Hdr.pSession != NULL)
1794 {
1795 OSDBGPRINT(("SUPDRV_IDC_REQ_CONNECT: pSession=%p expected NULL!\n", pReq->Hdr.pSession));
1796 return pReqHdr->rc = VERR_INVALID_PARAMETER;
1797 }
1798 if (pReq->u.In.u32MagicCookie != SUPDRVIDCREQ_CONNECT_MAGIC_COOKIE)
1799 {
1800 OSDBGPRINT(("SUPDRV_IDC_REQ_CONNECT: u32MagicCookie=%#x expected %#x!\n",
1801 (unsigned)pReq->u.In.u32MagicCookie, (unsigned)SUPDRVIDCREQ_CONNECT_MAGIC_COOKIE));
1802 return pReqHdr->rc = VERR_INVALID_PARAMETER;
1803 }
1804 if ( pReq->u.In.uMinVersion > pReq->u.In.uReqVersion
1805 || (pReq->u.In.uMinVersion & UINT32_C(0xffff0000)) != (pReq->u.In.uReqVersion & UINT32_C(0xffff0000)))
1806 {
1807 OSDBGPRINT(("SUPDRV_IDC_REQ_CONNECT: uMinVersion=%#x uMaxVersion=%#x doesn't match!\n",
1808 pReq->u.In.uMinVersion, pReq->u.In.uReqVersion));
1809 return pReqHdr->rc = VERR_INVALID_PARAMETER;
1810 }
1811
1812 /*
1813 * Match the version.
1814 * The current logic is very simple, match the major interface version.
1815 */
1816 if ( pReq->u.In.uMinVersion > SUPDRV_IDC_VERSION
1817 || (pReq->u.In.uMinVersion & 0xffff0000) != (SUPDRV_IDC_VERSION & 0xffff0000))
1818 {
1819 OSDBGPRINT(("SUPDRV_IDC_REQ_CONNECT: Version mismatch. Requested: %#x Min: %#x Current: %#x\n",
1820 pReq->u.In.uReqVersion, pReq->u.In.uMinVersion, (unsigned)SUPDRV_IDC_VERSION));
1821 pReq->u.Out.pSession = NULL;
1822 pReq->u.Out.uSessionVersion = 0xffffffff;
1823 pReq->u.Out.uDriverVersion = SUPDRV_IDC_VERSION;
1824 pReq->u.Out.uDriverRevision = VBOX_SVN_REV;
1825 pReq->Hdr.rc = VERR_VERSION_MISMATCH;
1826 return VINF_SUCCESS;
1827 }
1828
1829 pReq->u.Out.pSession = NULL;
1830 pReq->u.Out.uSessionVersion = SUPDRV_IDC_VERSION;
1831 pReq->u.Out.uDriverVersion = SUPDRV_IDC_VERSION;
1832 pReq->u.Out.uDriverRevision = VBOX_SVN_REV;
1833
1834 /*
1835 * On NT we will already have a session associated with the
1836 * client, just like with the SUP_IOCTL_COOKIE request, while
1837 * the other doesn't.
1838 */
1839#ifdef RT_OS_WINDOWS
1840 pReq->Hdr.rc = VINF_SUCCESS;
1841#else
1842 AssertReturn(!pSession, VERR_INTERNAL_ERROR);
1843 pReq->Hdr.rc = supdrvCreateSession(pDevExt, false /* fUser */, &pSession);
1844 if (RT_FAILURE(pReq->Hdr.rc))
1845 {
1846 OSDBGPRINT(("SUPDRV_IDC_REQ_CONNECT: failed to create session, rc=%d\n", pReq->Hdr.rc));
1847 return VINF_SUCCESS;
1848 }
1849#endif
1850
1851 pReq->u.Out.pSession = pSession;
1852 pReq->Hdr.pSession = pSession;
1853
1854 return VINF_SUCCESS;
1855 }
1856
1857 case SUPDRV_IDC_REQ_DISCONNECT:
1858 {
1859 REQ_CHECK_IDC_SIZE(SUPDRV_IDC_REQ_DISCONNECT, sizeof(*pReqHdr));
1860
1861#ifdef RT_OS_WINDOWS
1862 /* Windows will destroy the session when the file object is destroyed. */
1863#else
1864 supdrvCloseSession(pDevExt, pSession);
1865#endif
1866 return pReqHdr->rc = VINF_SUCCESS;
1867 }
1868
1869 case SUPDRV_IDC_REQ_GET_SYMBOL:
1870 {
1871 PSUPDRVIDCREQGETSYM pReq = (PSUPDRVIDCREQGETSYM)pReqHdr;
1872 REQ_CHECK_IDC_SIZE(SUPDRV_IDC_REQ_GET_SYMBOL, sizeof(*pReq));
1873
1874 pReq->Hdr.rc = supdrvIDC_LdrGetSymbol(pDevExt, pSession, pReq);
1875 return VINF_SUCCESS;
1876 }
1877
1878 case SUPDRV_IDC_REQ_COMPONENT_REGISTER_FACTORY:
1879 {
1880 PSUPDRVIDCREQCOMPREGFACTORY pReq = (PSUPDRVIDCREQCOMPREGFACTORY)pReqHdr;
1881 REQ_CHECK_IDC_SIZE(SUPDRV_IDC_REQ_COMPONENT_REGISTER_FACTORY, sizeof(*pReq));
1882
1883 pReq->Hdr.rc = SUPR0ComponentRegisterFactory(pSession, pReq->u.In.pFactory);
1884 return VINF_SUCCESS;
1885 }
1886
1887 case SUPDRV_IDC_REQ_COMPONENT_DEREGISTER_FACTORY:
1888 {
1889 PSUPDRVIDCREQCOMPDEREGFACTORY pReq = (PSUPDRVIDCREQCOMPDEREGFACTORY)pReqHdr;
1890 REQ_CHECK_IDC_SIZE(SUPDRV_IDC_REQ_COMPONENT_DEREGISTER_FACTORY, sizeof(*pReq));
1891
1892 pReq->Hdr.rc = SUPR0ComponentDeregisterFactory(pSession, pReq->u.In.pFactory);
1893 return VINF_SUCCESS;
1894 }
1895
1896 default:
1897 Log(("Unknown IDC %#lx\n", (long)uReq));
1898 break;
1899 }
1900
1901#undef REQ_CHECK_IDC_SIZE
1902 return VERR_NOT_SUPPORTED;
1903}
1904
1905
1906/**
1907 * Register a object for reference counting.
1908 * The object is registered with one reference in the specified session.
1909 *
1910 * @returns Unique identifier on success (pointer).
1911 * All future reference must use this identifier.
1912 * @returns NULL on failure.
1913 * @param pfnDestructor The destructore function which will be called when the reference count reaches 0.
1914 * @param pvUser1 The first user argument.
1915 * @param pvUser2 The second user argument.
1916 */
1917SUPR0DECL(void *) SUPR0ObjRegister(PSUPDRVSESSION pSession, SUPDRVOBJTYPE enmType, PFNSUPDRVDESTRUCTOR pfnDestructor, void *pvUser1, void *pvUser2)
1918{
1919 RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
1920 PSUPDRVDEVEXT pDevExt = pSession->pDevExt;
1921 PSUPDRVOBJ pObj;
1922 PSUPDRVUSAGE pUsage;
1923
1924 /*
1925 * Validate the input.
1926 */
1927 AssertReturn(SUP_IS_SESSION_VALID(pSession), NULL);
1928 AssertReturn(enmType > SUPDRVOBJTYPE_INVALID && enmType < SUPDRVOBJTYPE_END, NULL);
1929 AssertPtrReturn(pfnDestructor, NULL);
1930
1931 /*
1932 * Allocate and initialize the object.
1933 */
1934 pObj = (PSUPDRVOBJ)RTMemAlloc(sizeof(*pObj));
1935 if (!pObj)
1936 return NULL;
1937 pObj->u32Magic = SUPDRVOBJ_MAGIC;
1938 pObj->enmType = enmType;
1939 pObj->pNext = NULL;
1940 pObj->cUsage = 1;
1941 pObj->pfnDestructor = pfnDestructor;
1942 pObj->pvUser1 = pvUser1;
1943 pObj->pvUser2 = pvUser2;
1944 pObj->CreatorUid = pSession->Uid;
1945 pObj->CreatorGid = pSession->Gid;
1946 pObj->CreatorProcess= pSession->Process;
1947 supdrvOSObjInitCreator(pObj, pSession);
1948
1949 /*
1950 * Allocate the usage record.
1951 * (We keep freed usage records around to simplify SUPR0ObjAddRefEx().)
1952 */
1953 RTSpinlockAcquire(pDevExt->Spinlock, &SpinlockTmp);
1954
1955 pUsage = pDevExt->pUsageFree;
1956 if (pUsage)
1957 pDevExt->pUsageFree = pUsage->pNext;
1958 else
1959 {
1960 RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
1961 pUsage = (PSUPDRVUSAGE)RTMemAlloc(sizeof(*pUsage));
1962 if (!pUsage)
1963 {
1964 RTMemFree(pObj);
1965 return NULL;
1966 }
1967 RTSpinlockAcquire(pDevExt->Spinlock, &SpinlockTmp);
1968 }
1969
1970 /*
1971 * Insert the object and create the session usage record.
1972 */
1973 /* The object. */
1974 pObj->pNext = pDevExt->pObjs;
1975 pDevExt->pObjs = pObj;
1976
1977 /* The session record. */
1978 pUsage->cUsage = 1;
1979 pUsage->pObj = pObj;
1980 pUsage->pNext = pSession->pUsage;
1981 /* Log2(("SUPR0ObjRegister: pUsage=%p:{.pObj=%p, .pNext=%p}\n", pUsage, pUsage->pObj, pUsage->pNext)); */
1982 pSession->pUsage = pUsage;
1983
1984 RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
1985
1986 Log(("SUPR0ObjRegister: returns %p (pvUser1=%p, pvUser=%p)\n", pObj, pvUser1, pvUser2));
1987 return pObj;
1988}
1989
1990
1991/**
1992 * Increment the reference counter for the object associating the reference
1993 * with the specified session.
1994 *
1995 * @returns IPRT status code.
1996 * @param pvObj The identifier returned by SUPR0ObjRegister().
1997 * @param pSession The session which is referencing the object.
1998 *
1999 * @remarks The caller should not own any spinlocks and must carefully protect
2000 * itself against potential race with the destructor so freed memory
2001 * isn't accessed here.
2002 */
2003SUPR0DECL(int) SUPR0ObjAddRef(void *pvObj, PSUPDRVSESSION pSession)
2004{
2005 return SUPR0ObjAddRefEx(pvObj, pSession, false /* fNoBlocking */);
2006}
2007
2008
2009/**
2010 * Increment the reference counter for the object associating the reference
2011 * with the specified session.
2012 *
2013 * @returns IPRT status code.
2014 * @retval VERR_TRY_AGAIN if fNoBlocking was set and a new usage record
2015 * couldn't be allocated. (If you see this you're not doing the right
2016 * thing and it won't ever work reliably.)
2017 *
2018 * @param pvObj The identifier returned by SUPR0ObjRegister().
2019 * @param pSession The session which is referencing the object.
2020 * @param fNoBlocking Set if it's not OK to block. Never try to make the
2021 * first reference to an object in a session with this
2022 * argument set.
2023 *
2024 * @remarks The caller should not own any spinlocks and must carefully protect
2025 * itself against potential race with the destructor so freed memory
2026 * isn't accessed here.
2027 */
2028SUPR0DECL(int) SUPR0ObjAddRefEx(void *pvObj, PSUPDRVSESSION pSession, bool fNoBlocking)
2029{
2030 RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
2031 PSUPDRVDEVEXT pDevExt = pSession->pDevExt;
2032 PSUPDRVOBJ pObj = (PSUPDRVOBJ)pvObj;
2033 int rc = VINF_SUCCESS;
2034 PSUPDRVUSAGE pUsagePre;
2035 PSUPDRVUSAGE pUsage;
2036
2037 /*
2038 * Validate the input.
2039 * Be ready for the destruction race (someone might be stuck in the
2040 * destructor waiting a lock we own).
2041 */
2042 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
2043 AssertPtrReturn(pObj, VERR_INVALID_POINTER);
2044 AssertMsgReturn(pObj->u32Magic == SUPDRVOBJ_MAGIC || pObj->u32Magic == SUPDRVOBJ_MAGIC_DEAD,
2045 ("Invalid pvObj=%p magic=%#x (expected %#x or %#x)\n", pvObj, pObj->u32Magic, SUPDRVOBJ_MAGIC, SUPDRVOBJ_MAGIC_DEAD),
2046 VERR_INVALID_PARAMETER);
2047
2048 RTSpinlockAcquire(pDevExt->Spinlock, &SpinlockTmp);
2049
2050 if (RT_UNLIKELY(pObj->u32Magic != SUPDRVOBJ_MAGIC))
2051 {
2052 RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
2053
2054 AssertMsgFailed(("pvObj=%p magic=%#x\n", pvObj, pObj->u32Magic));
2055 return VERR_WRONG_ORDER;
2056 }
2057
2058 /*
2059 * Preallocate the usage record if we can.
2060 */
2061 pUsagePre = pDevExt->pUsageFree;
2062 if (pUsagePre)
2063 pDevExt->pUsageFree = pUsagePre->pNext;
2064 else if (!fNoBlocking)
2065 {
2066 RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
2067 pUsagePre = (PSUPDRVUSAGE)RTMemAlloc(sizeof(*pUsagePre));
2068 if (!pUsagePre)
2069 return VERR_NO_MEMORY;
2070
2071 RTSpinlockAcquire(pDevExt->Spinlock, &SpinlockTmp);
2072 if (RT_UNLIKELY(pObj->u32Magic != SUPDRVOBJ_MAGIC))
2073 {
2074 RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
2075
2076 AssertMsgFailed(("pvObj=%p magic=%#x\n", pvObj, pObj->u32Magic));
2077 return VERR_WRONG_ORDER;
2078 }
2079 }
2080
2081 /*
2082 * Reference the object.
2083 */
2084 pObj->cUsage++;
2085
2086 /*
2087 * Look for the session record.
2088 */
2089 for (pUsage = pSession->pUsage; pUsage; pUsage = pUsage->pNext)
2090 {
2091 /*Log(("SUPR0AddRef: pUsage=%p:{.pObj=%p, .pNext=%p}\n", pUsage, pUsage->pObj, pUsage->pNext));*/
2092 if (pUsage->pObj == pObj)
2093 break;
2094 }
2095 if (pUsage)
2096 pUsage->cUsage++;
2097 else if (pUsagePre)
2098 {
2099 /* create a new session record. */
2100 pUsagePre->cUsage = 1;
2101 pUsagePre->pObj = pObj;
2102 pUsagePre->pNext = pSession->pUsage;
2103 pSession->pUsage = pUsagePre;
2104 /*Log(("SUPR0AddRef: pUsagePre=%p:{.pObj=%p, .pNext=%p}\n", pUsagePre, pUsagePre->pObj, pUsagePre->pNext));*/
2105
2106 pUsagePre = NULL;
2107 }
2108 else
2109 {
2110 pObj->cUsage--;
2111 rc = VERR_TRY_AGAIN;
2112 }
2113
2114 /*
2115 * Put any unused usage record into the free list..
2116 */
2117 if (pUsagePre)
2118 {
2119 pUsagePre->pNext = pDevExt->pUsageFree;
2120 pDevExt->pUsageFree = pUsagePre;
2121 }
2122
2123 RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
2124
2125 return rc;
2126}
2127
2128
2129/**
2130 * Decrement / destroy a reference counter record for an object.
2131 *
2132 * The object is uniquely identified by pfnDestructor+pvUser1+pvUser2.
2133 *
2134 * @returns IPRT status code.
2135 * @retval VINF_SUCCESS if not destroyed.
2136 * @retval VINF_OBJECT_DESTROYED if it's destroyed by this release call.
2137 * @retval VERR_INVALID_PARAMETER if the object isn't valid. Will assert in
2138 * string builds.
2139 *
2140 * @param pvObj The identifier returned by SUPR0ObjRegister().
2141 * @param pSession The session which is referencing the object.
2142 */
2143SUPR0DECL(int) SUPR0ObjRelease(void *pvObj, PSUPDRVSESSION pSession)
2144{
2145 RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
2146 PSUPDRVDEVEXT pDevExt = pSession->pDevExt;
2147 PSUPDRVOBJ pObj = (PSUPDRVOBJ)pvObj;
2148 int rc = VERR_INVALID_PARAMETER;
2149 PSUPDRVUSAGE pUsage;
2150 PSUPDRVUSAGE pUsagePrev;
2151
2152 /*
2153 * Validate the input.
2154 */
2155 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
2156 AssertMsgReturn(VALID_PTR(pObj) && pObj->u32Magic == SUPDRVOBJ_MAGIC,
2157 ("Invalid pvObj=%p magic=%#x (exepcted %#x)\n", pvObj, pObj ? pObj->u32Magic : 0, SUPDRVOBJ_MAGIC),
2158 VERR_INVALID_PARAMETER);
2159
2160 /*
2161 * Acquire the spinlock and look for the usage record.
2162 */
2163 RTSpinlockAcquire(pDevExt->Spinlock, &SpinlockTmp);
2164
2165 for (pUsagePrev = NULL, pUsage = pSession->pUsage;
2166 pUsage;
2167 pUsagePrev = pUsage, pUsage = pUsage->pNext)
2168 {
2169 /*Log2(("SUPR0ObjRelease: pUsage=%p:{.pObj=%p, .pNext=%p}\n", pUsage, pUsage->pObj, pUsage->pNext));*/
2170 if (pUsage->pObj == pObj)
2171 {
2172 rc = VINF_SUCCESS;
2173 AssertMsg(pUsage->cUsage >= 1 && pObj->cUsage >= pUsage->cUsage, ("glob %d; sess %d\n", pObj->cUsage, pUsage->cUsage));
2174 if (pUsage->cUsage > 1)
2175 {
2176 pObj->cUsage--;
2177 pUsage->cUsage--;
2178 }
2179 else
2180 {
2181 /*
2182 * Free the session record.
2183 */
2184 if (pUsagePrev)
2185 pUsagePrev->pNext = pUsage->pNext;
2186 else
2187 pSession->pUsage = pUsage->pNext;
2188 pUsage->pNext = pDevExt->pUsageFree;
2189 pDevExt->pUsageFree = pUsage;
2190
2191 /* What about the object? */
2192 if (pObj->cUsage > 1)
2193 pObj->cUsage--;
2194 else
2195 {
2196 /*
2197 * Object is to be destroyed, unlink it.
2198 */
2199 pObj->u32Magic = SUPDRVOBJ_MAGIC_DEAD;
2200 rc = VINF_OBJECT_DESTROYED;
2201 if (pDevExt->pObjs == pObj)
2202 pDevExt->pObjs = pObj->pNext;
2203 else
2204 {
2205 PSUPDRVOBJ pObjPrev;
2206 for (pObjPrev = pDevExt->pObjs; pObjPrev; pObjPrev = pObjPrev->pNext)
2207 if (pObjPrev->pNext == pObj)
2208 {
2209 pObjPrev->pNext = pObj->pNext;
2210 break;
2211 }
2212 Assert(pObjPrev);
2213 }
2214 }
2215 }
2216 break;
2217 }
2218 }
2219
2220 RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
2221
2222 /*
2223 * Call the destructor and free the object if required.
2224 */
2225 if (rc == VINF_OBJECT_DESTROYED)
2226 {
2227 Log(("SUPR0ObjRelease: destroying %p/%d (%p/%p) cpid=%RTproc pid=%RTproc dtor=%p\n",
2228 pObj, pObj->enmType, pObj->pvUser1, pObj->pvUser2, pObj->CreatorProcess, RTProcSelf(), pObj->pfnDestructor));
2229 if (pObj->pfnDestructor)
2230#ifdef RT_WITH_W64_UNWIND_HACK
2231 supdrvNtWrapObjDestructor((PFNRT)pObj->pfnDestructor, pObj, pObj->pvUser1, pObj->pvUser2);
2232#else
2233 pObj->pfnDestructor(pObj, pObj->pvUser1, pObj->pvUser2);
2234#endif
2235 RTMemFree(pObj);
2236 }
2237
2238 AssertMsg(pUsage, ("pvObj=%p\n", pvObj));
2239 return rc;
2240}
2241
2242
2243/**
2244 * Verifies that the current process can access the specified object.
2245 *
2246 * @returns The following IPRT status code:
2247 * @retval VINF_SUCCESS if access was granted.
2248 * @retval VERR_PERMISSION_DENIED if denied access.
2249 * @retval VERR_INVALID_PARAMETER if invalid parameter.
2250 *
2251 * @param pvObj The identifier returned by SUPR0ObjRegister().
2252 * @param pSession The session which wishes to access the object.
2253 * @param pszObjName Object string name. This is optional and depends on the object type.
2254 *
2255 * @remark The caller is responsible for making sure the object isn't removed while
2256 * we're inside this function. If uncertain about this, just call AddRef before calling us.
2257 */
2258SUPR0DECL(int) SUPR0ObjVerifyAccess(void *pvObj, PSUPDRVSESSION pSession, const char *pszObjName)
2259{
2260 PSUPDRVOBJ pObj = (PSUPDRVOBJ)pvObj;
2261 int rc;
2262
2263 /*
2264 * Validate the input.
2265 */
2266 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
2267 AssertMsgReturn(VALID_PTR(pObj) && pObj->u32Magic == SUPDRVOBJ_MAGIC,
2268 ("Invalid pvObj=%p magic=%#x (exepcted %#x)\n", pvObj, pObj ? pObj->u32Magic : 0, SUPDRVOBJ_MAGIC),
2269 VERR_INVALID_PARAMETER);
2270
2271 /*
2272 * Check access. (returns true if a decision has been made.)
2273 */
2274 rc = VERR_INTERNAL_ERROR;
2275 if (supdrvOSObjCanAccess(pObj, pSession, pszObjName, &rc))
2276 return rc;
2277
2278 /*
2279 * Default policy is to allow the user to access his own
2280 * stuff but nothing else.
2281 */
2282 if (pObj->CreatorUid == pSession->Uid)
2283 return VINF_SUCCESS;
2284 return VERR_PERMISSION_DENIED;
2285}
2286
2287
2288/**
2289 * Lock pages.
2290 *
2291 * @returns IPRT status code.
2292 * @param pSession Session to which the locked memory should be associated.
2293 * @param pvR3 Start of the memory range to lock.
2294 * This must be page aligned.
2295 * @param cPages Number of pages to lock.
2296 * @param paPages Where to put the physical addresses of locked memory.
2297 */
2298SUPR0DECL(int) SUPR0LockMem(PSUPDRVSESSION pSession, RTR3PTR pvR3, uint32_t cPages, PRTHCPHYS paPages)
2299{
2300 int rc;
2301 SUPDRVMEMREF Mem = { NIL_RTR0MEMOBJ, NIL_RTR0MEMOBJ, MEMREF_TYPE_UNUSED };
2302 const size_t cb = (size_t)cPages << PAGE_SHIFT;
2303 LogFlow(("SUPR0LockMem: pSession=%p pvR3=%p cPages=%d paPages=%p\n", pSession, (void *)pvR3, cPages, paPages));
2304
2305 /*
2306 * Verify input.
2307 */
2308 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
2309 AssertPtrReturn(paPages, VERR_INVALID_PARAMETER);
2310 if ( RT_ALIGN_R3PT(pvR3, PAGE_SIZE, RTR3PTR) != pvR3
2311 || !pvR3)
2312 {
2313 Log(("pvR3 (%p) must be page aligned and not NULL!\n", (void *)pvR3));
2314 return VERR_INVALID_PARAMETER;
2315 }
2316
2317 /*
2318 * Let IPRT do the job.
2319 */
2320 Mem.eType = MEMREF_TYPE_LOCKED;
2321 rc = RTR0MemObjLockUser(&Mem.MemObj, pvR3, cb, RTMEM_PROT_READ | RTMEM_PROT_WRITE, RTR0ProcHandleSelf());
2322 if (RT_SUCCESS(rc))
2323 {
2324 uint32_t iPage = cPages;
2325 AssertMsg(RTR0MemObjAddressR3(Mem.MemObj) == pvR3, ("%p == %p\n", RTR0MemObjAddressR3(Mem.MemObj), pvR3));
2326 AssertMsg(RTR0MemObjSize(Mem.MemObj) == cb, ("%x == %x\n", RTR0MemObjSize(Mem.MemObj), cb));
2327
2328 while (iPage-- > 0)
2329 {
2330 paPages[iPage] = RTR0MemObjGetPagePhysAddr(Mem.MemObj, iPage);
2331 if (RT_UNLIKELY(paPages[iPage] == NIL_RTCCPHYS))
2332 {
2333 AssertMsgFailed(("iPage=%d\n", iPage));
2334 rc = VERR_INTERNAL_ERROR;
2335 break;
2336 }
2337 }
2338 if (RT_SUCCESS(rc))
2339 rc = supdrvMemAdd(&Mem, pSession);
2340 if (RT_FAILURE(rc))
2341 {
2342 int rc2 = RTR0MemObjFree(Mem.MemObj, false);
2343 AssertRC(rc2);
2344 }
2345 }
2346
2347 return rc;
2348}
2349
2350
2351/**
2352 * Unlocks the memory pointed to by pv.
2353 *
2354 * @returns IPRT status code.
2355 * @param pSession Session to which the memory was locked.
2356 * @param pvR3 Memory to unlock.
2357 */
2358SUPR0DECL(int) SUPR0UnlockMem(PSUPDRVSESSION pSession, RTR3PTR pvR3)
2359{
2360 LogFlow(("SUPR0UnlockMem: pSession=%p pvR3=%p\n", pSession, (void *)pvR3));
2361 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
2362 return supdrvMemRelease(pSession, (RTHCUINTPTR)pvR3, MEMREF_TYPE_LOCKED);
2363}
2364
2365
2366/**
2367 * Allocates a chunk of page aligned memory with contiguous and fixed physical
2368 * backing.
2369 *
2370 * @returns IPRT status code.
2371 * @param pSession Session data.
2372 * @param cPages Number of pages to allocate.
2373 * @param ppvR0 Where to put the address of Ring-0 mapping the allocated memory.
2374 * @param ppvR3 Where to put the address of Ring-3 mapping the allocated memory.
2375 * @param pHCPhys Where to put the physical address of allocated memory.
2376 */
2377SUPR0DECL(int) SUPR0ContAlloc(PSUPDRVSESSION pSession, uint32_t cPages, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PRTHCPHYS pHCPhys)
2378{
2379 int rc;
2380 SUPDRVMEMREF Mem = { NIL_RTR0MEMOBJ, NIL_RTR0MEMOBJ, MEMREF_TYPE_UNUSED };
2381 LogFlow(("SUPR0ContAlloc: pSession=%p cPages=%d ppvR0=%p ppvR3=%p pHCPhys=%p\n", pSession, cPages, ppvR0, ppvR3, pHCPhys));
2382
2383 /*
2384 * Validate input.
2385 */
2386 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
2387 if (!ppvR3 || !ppvR0 || !pHCPhys)
2388 {
2389 Log(("Null pointer. All of these should be set: pSession=%p ppvR0=%p ppvR3=%p pHCPhys=%p\n",
2390 pSession, ppvR0, ppvR3, pHCPhys));
2391 return VERR_INVALID_PARAMETER;
2392
2393 }
2394 if (cPages < 1 || cPages >= 256)
2395 {
2396 Log(("Illegal request cPages=%d, must be greater than 0 and smaller than 256.\n", cPages));
2397 return VERR_PAGE_COUNT_OUT_OF_RANGE;
2398 }
2399
2400 /*
2401 * Let IPRT do the job.
2402 */
2403 rc = RTR0MemObjAllocCont(&Mem.MemObj, cPages << PAGE_SHIFT, true /* executable R0 mapping */);
2404 if (RT_SUCCESS(rc))
2405 {
2406 int rc2;
2407 rc = RTR0MemObjMapUser(&Mem.MapObjR3, Mem.MemObj, (RTR3PTR)-1, 0,
2408 RTMEM_PROT_EXEC | RTMEM_PROT_WRITE | RTMEM_PROT_READ, RTR0ProcHandleSelf());
2409 if (RT_SUCCESS(rc))
2410 {
2411 Mem.eType = MEMREF_TYPE_CONT;
2412 rc = supdrvMemAdd(&Mem, pSession);
2413 if (!rc)
2414 {
2415 *ppvR0 = RTR0MemObjAddress(Mem.MemObj);
2416 *ppvR3 = RTR0MemObjAddressR3(Mem.MapObjR3);
2417 *pHCPhys = RTR0MemObjGetPagePhysAddr(Mem.MemObj, 0);
2418 return 0;
2419 }
2420
2421 rc2 = RTR0MemObjFree(Mem.MapObjR3, false);
2422 AssertRC(rc2);
2423 }
2424 rc2 = RTR0MemObjFree(Mem.MemObj, false);
2425 AssertRC(rc2);
2426 }
2427
2428 return rc;
2429}
2430
2431
2432/**
2433 * Frees memory allocated using SUPR0ContAlloc().
2434 *
2435 * @returns IPRT status code.
2436 * @param pSession The session to which the memory was allocated.
2437 * @param uPtr Pointer to the memory (ring-3 or ring-0).
2438 */
2439SUPR0DECL(int) SUPR0ContFree(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr)
2440{
2441 LogFlow(("SUPR0ContFree: pSession=%p uPtr=%p\n", pSession, (void *)uPtr));
2442 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
2443 return supdrvMemRelease(pSession, uPtr, MEMREF_TYPE_CONT);
2444}
2445
2446
2447/**
2448 * Allocates a chunk of page aligned memory with fixed physical backing below 4GB.
2449 *
2450 * The memory isn't zeroed.
2451 *
2452 * @returns IPRT status code.
2453 * @param pSession Session data.
2454 * @param cPages Number of pages to allocate.
2455 * @param ppvR0 Where to put the address of Ring-0 mapping of the allocated memory.
2456 * @param ppvR3 Where to put the address of Ring-3 mapping of the allocated memory.
2457 * @param paPages Where to put the physical addresses of allocated memory.
2458 */
2459SUPR0DECL(int) SUPR0LowAlloc(PSUPDRVSESSION pSession, uint32_t cPages, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PRTHCPHYS paPages)
2460{
2461 unsigned iPage;
2462 int rc;
2463 SUPDRVMEMREF Mem = { NIL_RTR0MEMOBJ, NIL_RTR0MEMOBJ, MEMREF_TYPE_UNUSED };
2464 LogFlow(("SUPR0LowAlloc: pSession=%p cPages=%d ppvR3=%p ppvR0=%p paPages=%p\n", pSession, cPages, ppvR3, ppvR0, paPages));
2465
2466 /*
2467 * Validate input.
2468 */
2469 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
2470 if (!ppvR3 || !ppvR0 || !paPages)
2471 {
2472 Log(("Null pointer. All of these should be set: pSession=%p ppvR3=%p ppvR0=%p paPages=%p\n",
2473 pSession, ppvR3, ppvR0, paPages));
2474 return VERR_INVALID_PARAMETER;
2475
2476 }
2477 if (cPages < 1 || cPages >= 256)
2478 {
2479 Log(("Illegal request cPages=%d, must be greater than 0 and smaller than 256.\n", cPages));
2480 return VERR_PAGE_COUNT_OUT_OF_RANGE;
2481 }
2482
2483 /*
2484 * Let IPRT do the work.
2485 */
2486 rc = RTR0MemObjAllocLow(&Mem.MemObj, cPages << PAGE_SHIFT, true /* executable ring-0 mapping */);
2487 if (RT_SUCCESS(rc))
2488 {
2489 int rc2;
2490 rc = RTR0MemObjMapUser(&Mem.MapObjR3, Mem.MemObj, (RTR3PTR)-1, 0,
2491 RTMEM_PROT_EXEC | RTMEM_PROT_WRITE | RTMEM_PROT_READ, RTR0ProcHandleSelf());
2492 if (RT_SUCCESS(rc))
2493 {
2494 Mem.eType = MEMREF_TYPE_LOW;
2495 rc = supdrvMemAdd(&Mem, pSession);
2496 if (!rc)
2497 {
2498 for (iPage = 0; iPage < cPages; iPage++)
2499 {
2500 paPages[iPage] = RTR0MemObjGetPagePhysAddr(Mem.MemObj, iPage);
2501 AssertMsg(!(paPages[iPage] & (PAGE_SIZE - 1)), ("iPage=%d Phys=%RHp\n", paPages[iPage]));
2502 }
2503 *ppvR0 = RTR0MemObjAddress(Mem.MemObj);
2504 *ppvR3 = RTR0MemObjAddressR3(Mem.MapObjR3);
2505 return 0;
2506 }
2507
2508 rc2 = RTR0MemObjFree(Mem.MapObjR3, false);
2509 AssertRC(rc2);
2510 }
2511
2512 rc2 = RTR0MemObjFree(Mem.MemObj, false);
2513 AssertRC(rc2);
2514 }
2515
2516 return rc;
2517}
2518
2519
2520/**
2521 * Frees memory allocated using SUPR0LowAlloc().
2522 *
2523 * @returns IPRT status code.
2524 * @param pSession The session to which the memory was allocated.
2525 * @param uPtr Pointer to the memory (ring-3 or ring-0).
2526 */
2527SUPR0DECL(int) SUPR0LowFree(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr)
2528{
2529 LogFlow(("SUPR0LowFree: pSession=%p uPtr=%p\n", pSession, (void *)uPtr));
2530 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
2531 return supdrvMemRelease(pSession, uPtr, MEMREF_TYPE_LOW);
2532}
2533
2534
2535
2536/**
2537 * Allocates a chunk of memory with both R0 and R3 mappings.
2538 * The memory is fixed and it's possible to query the physical addresses using SUPR0MemGetPhys().
2539 *
2540 * @returns IPRT status code.
2541 * @param pSession The session to associated the allocation with.
2542 * @param cb Number of bytes to allocate.
2543 * @param ppvR0 Where to store the address of the Ring-0 mapping.
2544 * @param ppvR3 Where to store the address of the Ring-3 mapping.
2545 */
2546SUPR0DECL(int) SUPR0MemAlloc(PSUPDRVSESSION pSession, uint32_t cb, PRTR0PTR ppvR0, PRTR3PTR ppvR3)
2547{
2548 int rc;
2549 SUPDRVMEMREF Mem = { NIL_RTR0MEMOBJ, NIL_RTR0MEMOBJ, MEMREF_TYPE_UNUSED };
2550 LogFlow(("SUPR0MemAlloc: pSession=%p cb=%d ppvR0=%p ppvR3=%p\n", pSession, cb, ppvR0, ppvR3));
2551
2552 /*
2553 * Validate input.
2554 */
2555 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
2556 AssertPtrReturn(ppvR0, VERR_INVALID_POINTER);
2557 AssertPtrReturn(ppvR3, VERR_INVALID_POINTER);
2558 if (cb < 1 || cb >= _4M)
2559 {
2560 Log(("Illegal request cb=%u; must be greater than 0 and smaller than 4MB.\n", cb));
2561 return VERR_INVALID_PARAMETER;
2562 }
2563
2564 /*
2565 * Let IPRT do the work.
2566 */
2567 rc = RTR0MemObjAllocPage(&Mem.MemObj, cb, true /* executable ring-0 mapping */);
2568 if (RT_SUCCESS(rc))
2569 {
2570 int rc2;
2571 rc = RTR0MemObjMapUser(&Mem.MapObjR3, Mem.MemObj, (RTR3PTR)-1, 0,
2572 RTMEM_PROT_EXEC | RTMEM_PROT_WRITE | RTMEM_PROT_READ, RTR0ProcHandleSelf());
2573 if (RT_SUCCESS(rc))
2574 {
2575 Mem.eType = MEMREF_TYPE_MEM;
2576 rc = supdrvMemAdd(&Mem, pSession);
2577 if (!rc)
2578 {
2579 *ppvR0 = RTR0MemObjAddress(Mem.MemObj);
2580 *ppvR3 = RTR0MemObjAddressR3(Mem.MapObjR3);
2581 return VINF_SUCCESS;
2582 }
2583
2584 rc2 = RTR0MemObjFree(Mem.MapObjR3, false);
2585 AssertRC(rc2);
2586 }
2587
2588 rc2 = RTR0MemObjFree(Mem.MemObj, false);
2589 AssertRC(rc2);
2590 }
2591
2592 return rc;
2593}
2594
2595
2596/**
2597 * Get the physical addresses of memory allocated using SUPR0MemAlloc().
2598 *
2599 * @returns IPRT status code.
2600 * @param pSession The session to which the memory was allocated.
2601 * @param uPtr The Ring-0 or Ring-3 address returned by SUPR0MemAlloc().
2602 * @param paPages Where to store the physical addresses.
2603 */
2604SUPR0DECL(int) SUPR0MemGetPhys(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr, PSUPPAGE paPages) /** @todo switch this bugger to RTHCPHYS */
2605{
2606 PSUPDRVBUNDLE pBundle;
2607 RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
2608 LogFlow(("SUPR0MemGetPhys: pSession=%p uPtr=%p paPages=%p\n", pSession, (void *)uPtr, paPages));
2609
2610 /*
2611 * Validate input.
2612 */
2613 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
2614 AssertPtrReturn(paPages, VERR_INVALID_POINTER);
2615 AssertReturn(uPtr, VERR_INVALID_PARAMETER);
2616
2617 /*
2618 * Search for the address.
2619 */
2620 RTSpinlockAcquire(pSession->Spinlock, &SpinlockTmp);
2621 for (pBundle = &pSession->Bundle; pBundle; pBundle = pBundle->pNext)
2622 {
2623 if (pBundle->cUsed > 0)
2624 {
2625 unsigned i;
2626 for (i = 0; i < RT_ELEMENTS(pBundle->aMem); i++)
2627 {
2628 if ( pBundle->aMem[i].eType == MEMREF_TYPE_MEM
2629 && pBundle->aMem[i].MemObj != NIL_RTR0MEMOBJ
2630 && ( (RTHCUINTPTR)RTR0MemObjAddress(pBundle->aMem[i].MemObj) == uPtr
2631 || ( pBundle->aMem[i].MapObjR3 != NIL_RTR0MEMOBJ
2632 && RTR0MemObjAddressR3(pBundle->aMem[i].MapObjR3) == uPtr)
2633 )
2634 )
2635 {
2636 const size_t cPages = RTR0MemObjSize(pBundle->aMem[i].MemObj) >> PAGE_SHIFT;
2637 size_t iPage;
2638 for (iPage = 0; iPage < cPages; iPage++)
2639 {
2640 paPages[iPage].Phys = RTR0MemObjGetPagePhysAddr(pBundle->aMem[i].MemObj, iPage);
2641 paPages[iPage].uReserved = 0;
2642 }
2643 RTSpinlockRelease(pSession->Spinlock, &SpinlockTmp);
2644 return VINF_SUCCESS;
2645 }
2646 }
2647 }
2648 }
2649 RTSpinlockRelease(pSession->Spinlock, &SpinlockTmp);
2650 Log(("Failed to find %p!!!\n", (void *)uPtr));
2651 return VERR_INVALID_PARAMETER;
2652}
2653
2654
2655/**
2656 * Free memory allocated by SUPR0MemAlloc().
2657 *
2658 * @returns IPRT status code.
2659 * @param pSession The session owning the allocation.
2660 * @param uPtr The Ring-0 or Ring-3 address returned by SUPR0MemAlloc().
2661 */
2662SUPR0DECL(int) SUPR0MemFree(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr)
2663{
2664 LogFlow(("SUPR0MemFree: pSession=%p uPtr=%p\n", pSession, (void *)uPtr));
2665 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
2666 return supdrvMemRelease(pSession, uPtr, MEMREF_TYPE_MEM);
2667}
2668
2669
2670/**
2671 * Allocates a chunk of memory with a kernel or/and a user mode mapping.
2672 *
2673 * The memory is fixed and it's possible to query the physical addresses using
2674 * SUPR0MemGetPhys().
2675 *
2676 * @returns IPRT status code.
2677 * @param pSession The session to associated the allocation with.
2678 * @param cPages The number of pages to allocate.
2679 * @param fFlags Flags, reserved for the future. Must be zero.
2680 * @param ppvR3 Where to store the address of the Ring-3 mapping.
2681 * NULL if no ring-3 mapping.
2682 * @param ppvR3 Where to store the address of the Ring-0 mapping.
2683 * NULL if no ring-0 mapping.
2684 * @param paPages Where to store the addresses of the pages. Optional.
2685 */
2686SUPR0DECL(int) SUPR0PageAllocEx(PSUPDRVSESSION pSession, uint32_t cPages, uint32_t fFlags, PRTR3PTR ppvR3, PRTR0PTR ppvR0, PRTHCPHYS paPages)
2687{
2688 int rc;
2689 SUPDRVMEMREF Mem = { NIL_RTR0MEMOBJ, NIL_RTR0MEMOBJ, MEMREF_TYPE_UNUSED };
2690 LogFlow(("SUPR0PageAlloc: pSession=%p cb=%d ppvR3=%p\n", pSession, cPages, ppvR3));
2691
2692 /*
2693 * Validate input. The allowed allocation size must be at least equal to the maximum guest VRAM size.
2694 */
2695 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
2696 AssertPtrNullReturn(ppvR3, VERR_INVALID_POINTER);
2697 AssertPtrNullReturn(ppvR0, VERR_INVALID_POINTER);
2698 AssertReturn(ppvR3 || ppvR0, VERR_INVALID_PARAMETER);
2699 AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
2700 if (cPages < 1 || cPages > VBOX_MAX_ALLOC_PAGE_COUNT)
2701 {
2702 Log(("SUPR0PageAlloc: Illegal request cb=%u; must be greater than 0 and smaller than 128MB.\n", cPages));
2703 return VERR_PAGE_COUNT_OUT_OF_RANGE;
2704 }
2705
2706 /*
2707 * Let IPRT do the work.
2708 */
2709 if (ppvR0)
2710 rc = RTR0MemObjAllocPage(&Mem.MemObj, (size_t)cPages * PAGE_SIZE, true /* fExecutable */);
2711 else
2712 rc = RTR0MemObjAllocPhysNC(&Mem.MemObj, (size_t)cPages * PAGE_SIZE, NIL_RTHCPHYS);
2713 if (RT_SUCCESS(rc))
2714 {
2715 int rc2;
2716 if (ppvR3)
2717 rc = RTR0MemObjMapUser(&Mem.MapObjR3, Mem.MemObj, (RTR3PTR)-1, 0,
2718 RTMEM_PROT_EXEC | RTMEM_PROT_WRITE | RTMEM_PROT_READ, RTR0ProcHandleSelf());
2719 else
2720 Mem.MapObjR3 = NIL_RTR0MEMOBJ;
2721 if (RT_SUCCESS(rc))
2722 {
2723 Mem.eType = MEMREF_TYPE_PAGE;
2724 rc = supdrvMemAdd(&Mem, pSession);
2725 if (!rc)
2726 {
2727 if (ppvR3)
2728 *ppvR3 = RTR0MemObjAddressR3(Mem.MapObjR3);
2729 if (ppvR0)
2730 *ppvR0 = RTR0MemObjAddress(Mem.MemObj);
2731 if (paPages)
2732 {
2733 uint32_t iPage = cPages;
2734 while (iPage-- > 0)
2735 {
2736 paPages[iPage] = RTR0MemObjGetPagePhysAddr(Mem.MapObjR3, iPage);
2737 Assert(paPages[iPage] != NIL_RTHCPHYS);
2738 }
2739 }
2740 return VINF_SUCCESS;
2741 }
2742
2743 rc2 = RTR0MemObjFree(Mem.MapObjR3, false);
2744 AssertRC(rc2);
2745 }
2746
2747 rc2 = RTR0MemObjFree(Mem.MemObj, false);
2748 AssertRC(rc2);
2749 }
2750 return rc;
2751}
2752
2753
2754/**
2755 * Maps a chunk of memory previously allocated by SUPR0PageAllocEx into kernel
2756 * space.
2757 *
2758 * @returns IPRT status code.
2759 * @param pSession The session to associated the allocation with.
2760 * @param pvR3 The ring-3 address returned by SUPR0PageAllocEx.
2761 * @param offSub Where to start mapping. Must be page aligned.
2762 * @param cbSub How much to map. Must be page aligned.
2763 * @param fFlags Flags, MBZ.
2764 * @param ppvR0 Where to reutrn the address of the ring-0 mapping on
2765 * success.
2766 */
2767SUPR0DECL(int) SUPR0PageMapKernel(PSUPDRVSESSION pSession, RTR3PTR pvR3, uint32_t offSub, uint32_t cbSub,
2768 uint32_t fFlags, PRTR0PTR ppvR0)
2769{
2770 int rc;
2771 PSUPDRVBUNDLE pBundle;
2772 RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
2773 RTR0MEMOBJ hMemObj = NIL_RTR0MEMOBJ;
2774 LogFlow(("SUPR0PageMapKernel: pSession=%p pvR3=%p offSub=%#x cbSub=%#x\n", pSession, pvR3, offSub, cbSub));
2775
2776 /*
2777 * Validate input. The allowed allocation size must be at least equal to the maximum guest VRAM size.
2778 */
2779 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
2780 AssertPtrNullReturn(ppvR0, VERR_INVALID_POINTER);
2781 AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
2782 AssertReturn(!(offSub & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
2783 AssertReturn(!(cbSub & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
2784 AssertReturn(cbSub, VERR_INVALID_PARAMETER);
2785
2786 /*
2787 * Find the memory object.
2788 */
2789 RTSpinlockAcquire(pSession->Spinlock, &SpinlockTmp);
2790 for (pBundle = &pSession->Bundle; pBundle; pBundle = pBundle->pNext)
2791 {
2792 if (pBundle->cUsed > 0)
2793 {
2794 unsigned i;
2795 for (i = 0; i < RT_ELEMENTS(pBundle->aMem); i++)
2796 {
2797 if ( ( pBundle->aMem[i].eType == MEMREF_TYPE_PAGE
2798 && pBundle->aMem[i].MemObj != NIL_RTR0MEMOBJ
2799 && pBundle->aMem[i].MapObjR3 != NIL_RTR0MEMOBJ
2800 && RTR0MemObjAddressR3(pBundle->aMem[i].MapObjR3) == pvR3)
2801 || ( pBundle->aMem[i].eType == MEMREF_TYPE_LOCKED
2802 && pBundle->aMem[i].MemObj != NIL_RTR0MEMOBJ
2803 && pBundle->aMem[i].MapObjR3 == NIL_RTR0MEMOBJ
2804 && RTR0MemObjAddressR3(pBundle->aMem[i].MemObj) == pvR3))
2805 {
2806 hMemObj = pBundle->aMem[i].MemObj;
2807 break;
2808 }
2809 }
2810 }
2811 }
2812 RTSpinlockRelease(pSession->Spinlock, &SpinlockTmp);
2813
2814 rc = VERR_INVALID_PARAMETER;
2815 if (hMemObj != NIL_RTR0MEMOBJ)
2816 {
2817 /*
2818 * Do some furter input validations before calling IPRT.
2819 * (Cleanup is done indirectly by telling RTR0MemObjFree to include mappings.)
2820 */
2821 size_t cbMemObj = RTR0MemObjSize(hMemObj);
2822 if ( offSub < cbMemObj
2823 && cbSub <= cbMemObj
2824 && offSub + cbSub <= cbMemObj)
2825 {
2826 RTR0MEMOBJ hMapObj;
2827 rc = RTR0MemObjMapKernelEx(&hMapObj, hMemObj, (void *)-1, 0,
2828 RTMEM_PROT_READ | RTMEM_PROT_WRITE, offSub, cbSub);
2829 if (RT_SUCCESS(rc))
2830 *ppvR0 = RTR0MemObjAddress(hMapObj);
2831 }
2832 else
2833 SUPR0Printf("SUPR0PageMapKernel: cbMemObj=%#x offSub=%#x cbSub=%#x\n", cbMemObj, offSub, cbSub);
2834
2835 }
2836 return rc;
2837}
2838
2839
2840/**
2841 * Changes the page level protection of one or more pages previously allocated
2842 * by SUPR0PageAllocEx.
2843 *
2844 * @returns IPRT status code.
2845 * @param pSession The session to associated the allocation with.
2846 * @param pvR3 The ring-3 address returned by SUPR0PageAllocEx.
2847 * NIL_RTR3PTR if the ring-3 mapping should be unaffected.
2848 * @param pvR0 The ring-0 address returned by SUPR0PageAllocEx.
2849 * NIL_RTR0PTR if the ring-0 mapping should be unaffected.
2850 * @param offSub Where to start changing. Must be page aligned.
2851 * @param cbSub How much to change. Must be page aligned.
2852 * @param fProt The new page level protection, see RTMEM_PROT_*.
2853 */
2854SUPR0DECL(int) SUPR0PageProtect(PSUPDRVSESSION pSession, RTR3PTR pvR3, RTR0PTR pvR0, uint32_t offSub, uint32_t cbSub, uint32_t fProt)
2855{
2856 int rc;
2857 PSUPDRVBUNDLE pBundle;
2858 RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
2859 RTR0MEMOBJ hMemObjR0 = NIL_RTR0MEMOBJ;
2860 RTR0MEMOBJ hMemObjR3 = NIL_RTR0MEMOBJ;
2861 LogFlow(("SUPR0PageProtect: pSession=%p pvR3=%p pvR0=%p offSub=%#x cbSub=%#x fProt-%#x\n", pSession, pvR3, pvR0, offSub, cbSub, fProt));
2862
2863 /*
2864 * Validate input. The allowed allocation size must be at least equal to the maximum guest VRAM size.
2865 */
2866 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
2867 AssertReturn(!(fProt & ~(RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC | RTMEM_PROT_NONE)), VERR_INVALID_PARAMETER);
2868 AssertReturn(!(offSub & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
2869 AssertReturn(!(cbSub & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
2870 AssertReturn(cbSub, VERR_INVALID_PARAMETER);
2871
2872 /*
2873 * Find the memory object.
2874 */
2875 RTSpinlockAcquire(pSession->Spinlock, &SpinlockTmp);
2876 for (pBundle = &pSession->Bundle; pBundle; pBundle = pBundle->pNext)
2877 {
2878 if (pBundle->cUsed > 0)
2879 {
2880 unsigned i;
2881 for (i = 0; i < RT_ELEMENTS(pBundle->aMem); i++)
2882 {
2883 if ( pBundle->aMem[i].eType == MEMREF_TYPE_PAGE
2884 && pBundle->aMem[i].MemObj != NIL_RTR0MEMOBJ
2885 && ( pBundle->aMem[i].MapObjR3 != NIL_RTR0MEMOBJ
2886 || pvR3 == NIL_RTR3PTR)
2887 && ( pvR0 == NIL_RTR0PTR
2888 || RTR0MemObjAddress(pBundle->aMem[i].MemObj) == pvR0)
2889 && ( pvR3 == NIL_RTR3PTR
2890 || RTR0MemObjAddressR3(pBundle->aMem[i].MapObjR3) == pvR3))
2891 {
2892 if (pvR0 != NIL_RTR0PTR)
2893 hMemObjR0 = pBundle->aMem[i].MemObj;
2894 if (pvR3 != NIL_RTR3PTR)
2895 hMemObjR3 = pBundle->aMem[i].MapObjR3;
2896 break;
2897 }
2898 }
2899 }
2900 }
2901 RTSpinlockRelease(pSession->Spinlock, &SpinlockTmp);
2902
2903 rc = VERR_INVALID_PARAMETER;
2904 if ( hMemObjR0 != NIL_RTR0MEMOBJ
2905 || hMemObjR3 != NIL_RTR0MEMOBJ)
2906 {
2907 /*
2908 * Do some furter input validations before calling IPRT.
2909 */
2910 size_t cbMemObj = hMemObjR0 != NIL_RTR0PTR ? RTR0MemObjSize(hMemObjR0) : RTR0MemObjSize(hMemObjR3);
2911 if ( offSub < cbMemObj
2912 && cbSub <= cbMemObj
2913 && offSub + cbSub <= cbMemObj)
2914 {
2915 rc = VINF_SUCCESS;
2916 if (hMemObjR3 != NIL_RTR0PTR)
2917 rc = RTR0MemObjProtect(hMemObjR3, offSub, cbSub, fProt);
2918 if (hMemObjR0 != NIL_RTR0PTR && RT_SUCCESS(rc))
2919 rc = RTR0MemObjProtect(hMemObjR0, offSub, cbSub, fProt);
2920 }
2921 else
2922 SUPR0Printf("SUPR0PageMapKernel: cbMemObj=%#x offSub=%#x cbSub=%#x\n", cbMemObj, offSub, cbSub);
2923
2924 }
2925 return rc;
2926
2927}
2928
2929
2930/**
2931 * Free memory allocated by SUPR0PageAlloc() and SUPR0PageAllocEx().
2932 *
2933 * @returns IPRT status code.
2934 * @param pSession The session owning the allocation.
2935 * @param pvR3 The Ring-3 address returned by SUPR0PageAlloc() or
2936 * SUPR0PageAllocEx().
2937 */
2938SUPR0DECL(int) SUPR0PageFree(PSUPDRVSESSION pSession, RTR3PTR pvR3)
2939{
2940 LogFlow(("SUPR0PageFree: pSession=%p pvR3=%p\n", pSession, (void *)pvR3));
2941 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
2942 return supdrvMemRelease(pSession, (RTHCUINTPTR)pvR3, MEMREF_TYPE_PAGE);
2943}
2944
2945
2946/**
2947 * Maps the GIP into userspace and/or get the physical address of the GIP.
2948 *
2949 * @returns IPRT status code.
2950 * @param pSession Session to which the GIP mapping should belong.
2951 * @param ppGipR3 Where to store the address of the ring-3 mapping. (optional)
2952 * @param pHCPhysGip Where to store the physical address. (optional)
2953 *
2954 * @remark There is no reference counting on the mapping, so one call to this function
2955 * count globally as one reference. One call to SUPR0GipUnmap() is will unmap GIP
2956 * and remove the session as a GIP user.
2957 */
2958SUPR0DECL(int) SUPR0GipMap(PSUPDRVSESSION pSession, PRTR3PTR ppGipR3, PRTHCPHYS pHCPhysGip)
2959{
2960 int rc;
2961 PSUPDRVDEVEXT pDevExt = pSession->pDevExt;
2962 RTR3PTR pGipR3 = NIL_RTR3PTR;
2963 RTHCPHYS HCPhys = NIL_RTHCPHYS;
2964 LogFlow(("SUPR0GipMap: pSession=%p ppGipR3=%p pHCPhysGip=%p\n", pSession, ppGipR3, pHCPhysGip));
2965
2966 /*
2967 * Validate
2968 */
2969 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
2970 AssertPtrNullReturn(ppGipR3, VERR_INVALID_POINTER);
2971 AssertPtrNullReturn(pHCPhysGip, VERR_INVALID_POINTER);
2972
2973 RTSemFastMutexRequest(pDevExt->mtxGip);
2974 if (pDevExt->pGip)
2975 {
2976 /*
2977 * Map it?
2978 */
2979 rc = VINF_SUCCESS;
2980 if (ppGipR3)
2981 {
2982 if (pSession->GipMapObjR3 == NIL_RTR0MEMOBJ)
2983 rc = RTR0MemObjMapUser(&pSession->GipMapObjR3, pDevExt->GipMemObj, (RTR3PTR)-1, 0,
2984 RTMEM_PROT_READ, RTR0ProcHandleSelf());
2985 if (RT_SUCCESS(rc))
2986 pGipR3 = RTR0MemObjAddressR3(pSession->GipMapObjR3);
2987 }
2988
2989 /*
2990 * Get physical address.
2991 */
2992 if (pHCPhysGip && RT_SUCCESS(rc))
2993 HCPhys = pDevExt->HCPhysGip;
2994
2995 /*
2996 * Reference globally.
2997 */
2998 if (!pSession->fGipReferenced && RT_SUCCESS(rc))
2999 {
3000 pSession->fGipReferenced = 1;
3001 pDevExt->cGipUsers++;
3002 if (pDevExt->cGipUsers == 1)
3003 {
3004 PSUPGLOBALINFOPAGE pGipR0 = pDevExt->pGip;
3005 unsigned i;
3006
3007 LogFlow(("SUPR0GipMap: Resumes GIP updating\n"));
3008
3009 for (i = 0; i < RT_ELEMENTS(pGipR0->aCPUs); i++)
3010 ASMAtomicXchgU32(&pGipR0->aCPUs[i].u32TransactionId, pGipR0->aCPUs[i].u32TransactionId & ~(GIP_UPDATEHZ_RECALC_FREQ * 2 - 1));
3011 ASMAtomicXchgU64(&pGipR0->u64NanoTSLastUpdateHz, 0);
3012
3013 rc = RTTimerStart(pDevExt->pGipTimer, 0);
3014 AssertRC(rc); rc = VINF_SUCCESS;
3015 }
3016 }
3017 }
3018 else
3019 {
3020 rc = SUPDRV_ERR_GENERAL_FAILURE;
3021 Log(("SUPR0GipMap: GIP is not available!\n"));
3022 }
3023 RTSemFastMutexRelease(pDevExt->mtxGip);
3024
3025 /*
3026 * Write returns.
3027 */
3028 if (pHCPhysGip)
3029 *pHCPhysGip = HCPhys;
3030 if (ppGipR3)
3031 *ppGipR3 = pGipR3;
3032
3033#ifdef DEBUG_DARWIN_GIP
3034 OSDBGPRINT(("SUPR0GipMap: returns %d *pHCPhysGip=%lx pGipR3=%p\n", rc, (unsigned long)HCPhys, (void *)pGipR3));
3035#else
3036 LogFlow(( "SUPR0GipMap: returns %d *pHCPhysGip=%lx pGipR3=%p\n", rc, (unsigned long)HCPhys, (void *)pGipR3));
3037#endif
3038 return rc;
3039}
3040
3041
3042/**
3043 * Unmaps any user mapping of the GIP and terminates all GIP access
3044 * from this session.
3045 *
3046 * @returns IPRT status code.
3047 * @param pSession Session to which the GIP mapping should belong.
3048 */
3049SUPR0DECL(int) SUPR0GipUnmap(PSUPDRVSESSION pSession)
3050{
3051 int rc = VINF_SUCCESS;
3052 PSUPDRVDEVEXT pDevExt = pSession->pDevExt;
3053#ifdef DEBUG_DARWIN_GIP
3054 OSDBGPRINT(("SUPR0GipUnmap: pSession=%p pGip=%p GipMapObjR3=%p\n",
3055 pSession,
3056 pSession->GipMapObjR3 != NIL_RTR0MEMOBJ ? RTR0MemObjAddress(pSession->GipMapObjR3) : NULL,
3057 pSession->GipMapObjR3));
3058#else
3059 LogFlow(("SUPR0GipUnmap: pSession=%p\n", pSession));
3060#endif
3061 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
3062
3063 RTSemFastMutexRequest(pDevExt->mtxGip);
3064
3065 /*
3066 * Unmap anything?
3067 */
3068 if (pSession->GipMapObjR3 != NIL_RTR0MEMOBJ)
3069 {
3070 rc = RTR0MemObjFree(pSession->GipMapObjR3, false);
3071 AssertRC(rc);
3072 if (RT_SUCCESS(rc))
3073 pSession->GipMapObjR3 = NIL_RTR0MEMOBJ;
3074 }
3075
3076 /*
3077 * Dereference global GIP.
3078 */
3079 if (pSession->fGipReferenced && !rc)
3080 {
3081 pSession->fGipReferenced = 0;
3082 if ( pDevExt->cGipUsers > 0
3083 && !--pDevExt->cGipUsers)
3084 {
3085 LogFlow(("SUPR0GipUnmap: Suspends GIP updating\n"));
3086 rc = RTTimerStop(pDevExt->pGipTimer); AssertRC(rc); rc = VINF_SUCCESS;
3087 }
3088 }
3089
3090 RTSemFastMutexRelease(pDevExt->mtxGip);
3091
3092 return rc;
3093}
3094
3095
3096/**
3097 * Gets the GIP pointer.
3098 *
3099 * @returns Pointer to the GIP or NULL.
3100 */
3101SUPDECL(PSUPGLOBALINFOPAGE) SUPGetGIP(void)
3102{
3103 return g_pSUPGlobalInfoPage;
3104}
3105
3106
3107/**
3108 * Register a component factory with the support driver.
3109 *
3110 * This is currently restricted to kernel sessions only.
3111 *
3112 * @returns VBox status code.
3113 * @retval VINF_SUCCESS on success.
3114 * @retval VERR_NO_MEMORY if we're out of memory.
3115 * @retval VERR_ALREADY_EXISTS if the factory has already been registered.
3116 * @retval VERR_ACCESS_DENIED if it isn't a kernel session.
3117 * @retval VERR_INVALID_PARAMETER on invalid parameter.
3118 * @retval VERR_INVALID_POINTER on invalid pointer parameter.
3119 *
3120 * @param pSession The SUPDRV session (must be a ring-0 session).
3121 * @param pFactory Pointer to the component factory registration structure.
3122 *
3123 * @remarks This interface is also available via SUPR0IdcComponentRegisterFactory.
3124 */
3125SUPR0DECL(int) SUPR0ComponentRegisterFactory(PSUPDRVSESSION pSession, PCSUPDRVFACTORY pFactory)
3126{
3127 PSUPDRVFACTORYREG pNewReg;
3128 const char *psz;
3129 int rc;
3130
3131 /*
3132 * Validate parameters.
3133 */
3134 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
3135 AssertReturn(pSession->R0Process == NIL_RTR0PROCESS, VERR_ACCESS_DENIED);
3136 AssertPtrReturn(pFactory, VERR_INVALID_POINTER);
3137 AssertPtrReturn(pFactory->pfnQueryFactoryInterface, VERR_INVALID_POINTER);
3138 psz = (const char *)memchr(pFactory->szName, '\0', sizeof(pFactory->szName));
3139 AssertReturn(psz, VERR_INVALID_PARAMETER);
3140
3141 /*
3142 * Allocate and initialize a new registration structure.
3143 */
3144 pNewReg = (PSUPDRVFACTORYREG)RTMemAlloc(sizeof(SUPDRVFACTORYREG));
3145 if (pNewReg)
3146 {
3147 pNewReg->pNext = NULL;
3148 pNewReg->pFactory = pFactory;
3149 pNewReg->pSession = pSession;
3150 pNewReg->cchName = psz - &pFactory->szName[0];
3151
3152 /*
3153 * Add it to the tail of the list after checking for prior registration.
3154 */
3155 rc = RTSemFastMutexRequest(pSession->pDevExt->mtxComponentFactory);
3156 if (RT_SUCCESS(rc))
3157 {
3158 PSUPDRVFACTORYREG pPrev = NULL;
3159 PSUPDRVFACTORYREG pCur = pSession->pDevExt->pComponentFactoryHead;
3160 while (pCur && pCur->pFactory != pFactory)
3161 {
3162 pPrev = pCur;
3163 pCur = pCur->pNext;
3164 }
3165 if (!pCur)
3166 {
3167 if (pPrev)
3168 pPrev->pNext = pNewReg;
3169 else
3170 pSession->pDevExt->pComponentFactoryHead = pNewReg;
3171 rc = VINF_SUCCESS;
3172 }
3173 else
3174 rc = VERR_ALREADY_EXISTS;
3175
3176 RTSemFastMutexRelease(pSession->pDevExt->mtxComponentFactory);
3177 }
3178
3179 if (RT_FAILURE(rc))
3180 RTMemFree(pNewReg);
3181 }
3182 else
3183 rc = VERR_NO_MEMORY;
3184 return rc;
3185}
3186
3187
3188/**
3189 * Deregister a component factory.
3190 *
3191 * @returns VBox status code.
3192 * @retval VINF_SUCCESS on success.
3193 * @retval VERR_NOT_FOUND if the factory wasn't registered.
3194 * @retval VERR_ACCESS_DENIED if it isn't a kernel session.
3195 * @retval VERR_INVALID_PARAMETER on invalid parameter.
3196 * @retval VERR_INVALID_POINTER on invalid pointer parameter.
3197 *
3198 * @param pSession The SUPDRV session (must be a ring-0 session).
3199 * @param pFactory Pointer to the component factory registration structure
3200 * previously passed SUPR0ComponentRegisterFactory().
3201 *
3202 * @remarks This interface is also available via SUPR0IdcComponentDeregisterFactory.
3203 */
3204SUPR0DECL(int) SUPR0ComponentDeregisterFactory(PSUPDRVSESSION pSession, PCSUPDRVFACTORY pFactory)
3205{
3206 int rc;
3207
3208 /*
3209 * Validate parameters.
3210 */
3211 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
3212 AssertReturn(pSession->R0Process == NIL_RTR0PROCESS, VERR_ACCESS_DENIED);
3213 AssertPtrReturn(pFactory, VERR_INVALID_POINTER);
3214
3215 /*
3216 * Take the lock and look for the registration record.
3217 */
3218 rc = RTSemFastMutexRequest(pSession->pDevExt->mtxComponentFactory);
3219 if (RT_SUCCESS(rc))
3220 {
3221 PSUPDRVFACTORYREG pPrev = NULL;
3222 PSUPDRVFACTORYREG pCur = pSession->pDevExt->pComponentFactoryHead;
3223 while (pCur && pCur->pFactory != pFactory)
3224 {
3225 pPrev = pCur;
3226 pCur = pCur->pNext;
3227 }
3228 if (pCur)
3229 {
3230 if (!pPrev)
3231 pSession->pDevExt->pComponentFactoryHead = pCur->pNext;
3232 else
3233 pPrev->pNext = pCur->pNext;
3234
3235 pCur->pNext = NULL;
3236 pCur->pFactory = NULL;
3237 pCur->pSession = NULL;
3238 rc = VINF_SUCCESS;
3239 }
3240 else
3241 rc = VERR_NOT_FOUND;
3242
3243 RTSemFastMutexRelease(pSession->pDevExt->mtxComponentFactory);
3244
3245 RTMemFree(pCur);
3246 }
3247 return rc;
3248}
3249
3250
3251/**
3252 * Queries a component factory.
3253 *
3254 * @returns VBox status code.
3255 * @retval VERR_INVALID_PARAMETER on invalid parameter.
3256 * @retval VERR_INVALID_POINTER on invalid pointer parameter.
3257 * @retval VERR_SUPDRV_COMPONENT_NOT_FOUND if the component factory wasn't found.
3258 * @retval VERR_SUPDRV_INTERFACE_NOT_SUPPORTED if the interface wasn't supported.
3259 *
3260 * @param pSession The SUPDRV session.
3261 * @param pszName The name of the component factory.
3262 * @param pszInterfaceUuid The UUID of the factory interface (stringified).
3263 * @param ppvFactoryIf Where to store the factory interface.
3264 */
3265SUPR0DECL(int) SUPR0ComponentQueryFactory(PSUPDRVSESSION pSession, const char *pszName, const char *pszInterfaceUuid, void **ppvFactoryIf)
3266{
3267 const char *pszEnd;
3268 size_t cchName;
3269 int rc;
3270
3271 /*
3272 * Validate parameters.
3273 */
3274 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
3275
3276 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
3277 pszEnd = memchr(pszName, '\0', RT_SIZEOFMEMB(SUPDRVFACTORY, szName));
3278 AssertReturn(pszEnd, VERR_INVALID_PARAMETER);
3279 cchName = pszEnd - pszName;
3280
3281 AssertPtrReturn(pszInterfaceUuid, VERR_INVALID_POINTER);
3282 pszEnd = memchr(pszInterfaceUuid, '\0', RTUUID_STR_LENGTH);
3283 AssertReturn(pszEnd, VERR_INVALID_PARAMETER);
3284
3285 AssertPtrReturn(ppvFactoryIf, VERR_INVALID_POINTER);
3286 *ppvFactoryIf = NULL;
3287
3288 /*
3289 * Take the lock and try all factories by this name.
3290 */
3291 rc = RTSemFastMutexRequest(pSession->pDevExt->mtxComponentFactory);
3292 if (RT_SUCCESS(rc))
3293 {
3294 PSUPDRVFACTORYREG pCur = pSession->pDevExt->pComponentFactoryHead;
3295 rc = VERR_SUPDRV_COMPONENT_NOT_FOUND;
3296 while (pCur)
3297 {
3298 if ( pCur->cchName == cchName
3299 && !memcmp(pCur->pFactory->szName, pszName, cchName))
3300 {
3301#ifdef RT_WITH_W64_UNWIND_HACK
3302 void *pvFactory = supdrvNtWrapQueryFactoryInterface((PFNRT)pCur->pFactory->pfnQueryFactoryInterface, pCur->pFactory, pSession, pszInterfaceUuid);
3303#else
3304 void *pvFactory = pCur->pFactory->pfnQueryFactoryInterface(pCur->pFactory, pSession, pszInterfaceUuid);
3305#endif
3306 if (pvFactory)
3307 {
3308 *ppvFactoryIf = pvFactory;
3309 rc = VINF_SUCCESS;
3310 break;
3311 }
3312 rc = VERR_SUPDRV_INTERFACE_NOT_SUPPORTED;
3313 }
3314
3315 /* next */
3316 pCur = pCur->pNext;
3317 }
3318
3319 RTSemFastMutexRelease(pSession->pDevExt->mtxComponentFactory);
3320 }
3321 return rc;
3322}
3323
3324
3325/**
3326 * Adds a memory object to the session.
3327 *
3328 * @returns IPRT status code.
3329 * @param pMem Memory tracking structure containing the
3330 * information to track.
3331 * @param pSession The session.
3332 */
3333static int supdrvMemAdd(PSUPDRVMEMREF pMem, PSUPDRVSESSION pSession)
3334{
3335 PSUPDRVBUNDLE pBundle;
3336 RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
3337
3338 /*
3339 * Find free entry and record the allocation.
3340 */
3341 RTSpinlockAcquire(pSession->Spinlock, &SpinlockTmp);
3342 for (pBundle = &pSession->Bundle; pBundle; pBundle = pBundle->pNext)
3343 {
3344 if (pBundle->cUsed < RT_ELEMENTS(pBundle->aMem))
3345 {
3346 unsigned i;
3347 for (i = 0; i < RT_ELEMENTS(pBundle->aMem); i++)
3348 {
3349 if (pBundle->aMem[i].MemObj == NIL_RTR0MEMOBJ)
3350 {
3351 pBundle->cUsed++;
3352 pBundle->aMem[i] = *pMem;
3353 RTSpinlockRelease(pSession->Spinlock, &SpinlockTmp);
3354 return VINF_SUCCESS;
3355 }
3356 }
3357 AssertFailed(); /* !!this can't be happening!!! */
3358 }
3359 }
3360 RTSpinlockRelease(pSession->Spinlock, &SpinlockTmp);
3361
3362 /*
3363 * Need to allocate a new bundle.
3364 * Insert into the last entry in the bundle.
3365 */
3366 pBundle = (PSUPDRVBUNDLE)RTMemAllocZ(sizeof(*pBundle));
3367 if (!pBundle)
3368 return VERR_NO_MEMORY;
3369
3370 /* take last entry. */
3371 pBundle->cUsed++;
3372 pBundle->aMem[RT_ELEMENTS(pBundle->aMem) - 1] = *pMem;
3373
3374 /* insert into list. */
3375 RTSpinlockAcquire(pSession->Spinlock, &SpinlockTmp);
3376 pBundle->pNext = pSession->Bundle.pNext;
3377 pSession->Bundle.pNext = pBundle;
3378 RTSpinlockRelease(pSession->Spinlock, &SpinlockTmp);
3379
3380 return VINF_SUCCESS;
3381}
3382
3383
3384/**
3385 * Releases a memory object referenced by pointer and type.
3386 *
3387 * @returns IPRT status code.
3388 * @param pSession Session data.
3389 * @param uPtr Pointer to memory. This is matched against both the R0 and R3 addresses.
3390 * @param eType Memory type.
3391 */
3392static int supdrvMemRelease(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr, SUPDRVMEMREFTYPE eType)
3393{
3394 PSUPDRVBUNDLE pBundle;
3395 RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
3396
3397 /*
3398 * Validate input.
3399 */
3400 if (!uPtr)
3401 {
3402 Log(("Illegal address %p\n", (void *)uPtr));
3403 return VERR_INVALID_PARAMETER;
3404 }
3405
3406 /*
3407 * Search for the address.
3408 */
3409 RTSpinlockAcquire(pSession->Spinlock, &SpinlockTmp);
3410 for (pBundle = &pSession->Bundle; pBundle; pBundle = pBundle->pNext)
3411 {
3412 if (pBundle->cUsed > 0)
3413 {
3414 unsigned i;
3415 for (i = 0; i < RT_ELEMENTS(pBundle->aMem); i++)
3416 {
3417 if ( pBundle->aMem[i].eType == eType
3418 && pBundle->aMem[i].MemObj != NIL_RTR0MEMOBJ
3419 && ( (RTHCUINTPTR)RTR0MemObjAddress(pBundle->aMem[i].MemObj) == uPtr
3420 || ( pBundle->aMem[i].MapObjR3 != NIL_RTR0MEMOBJ
3421 && RTR0MemObjAddressR3(pBundle->aMem[i].MapObjR3) == uPtr))
3422 )
3423 {
3424 /* Make a copy of it and release it outside the spinlock. */
3425 SUPDRVMEMREF Mem = pBundle->aMem[i];
3426 pBundle->aMem[i].eType = MEMREF_TYPE_UNUSED;
3427 pBundle->aMem[i].MemObj = NIL_RTR0MEMOBJ;
3428 pBundle->aMem[i].MapObjR3 = NIL_RTR0MEMOBJ;
3429 RTSpinlockRelease(pSession->Spinlock, &SpinlockTmp);
3430
3431 if (Mem.MapObjR3 != NIL_RTR0MEMOBJ)
3432 {
3433 int rc = RTR0MemObjFree(Mem.MapObjR3, false);
3434 AssertRC(rc); /** @todo figure out how to handle this. */
3435 }
3436 if (Mem.MemObj != NIL_RTR0MEMOBJ)
3437 {
3438 int rc = RTR0MemObjFree(Mem.MemObj, true /* fFreeMappings */);
3439 AssertRC(rc); /** @todo figure out how to handle this. */
3440 }
3441 return VINF_SUCCESS;
3442 }
3443 }
3444 }
3445 }
3446 RTSpinlockRelease(pSession->Spinlock, &SpinlockTmp);
3447 Log(("Failed to find %p!!! (eType=%d)\n", (void *)uPtr, eType));
3448 return VERR_INVALID_PARAMETER;
3449}
3450
3451
3452/**
3453 * Opens an image. If it's the first time it's opened the call must upload
3454 * the bits using the supdrvIOCtl_LdrLoad() / SUPDRV_IOCTL_LDR_LOAD function.
3455 *
3456 * This is the 1st step of the loading.
3457 *
3458 * @returns IPRT status code.
3459 * @param pDevExt Device globals.
3460 * @param pSession Session data.
3461 * @param pReq The open request.
3462 */
3463static int supdrvIOCtl_LdrOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDROPEN pReq)
3464{
3465 int rc;
3466 PSUPDRVLDRIMAGE pImage;
3467 void *pv;
3468 size_t cchName = strlen(pReq->u.In.szName); /* (caller checked < 32). */
3469 LogFlow(("supdrvIOCtl_LdrOpen: szName=%s cbImageWithTabs=%d\n", pReq->u.In.szName, pReq->u.In.cbImageWithTabs));
3470
3471 /*
3472 * Check if we got an instance of the image already.
3473 */
3474 supdrvLdrLock(pDevExt);
3475 for (pImage = pDevExt->pLdrImages; pImage; pImage = pImage->pNext)
3476 {
3477 if ( pImage->szName[cchName] == '\0'
3478 && !memcmp(pImage->szName, pReq->u.In.szName, cchName))
3479 {
3480 /** @todo check cbImageBits and cbImageWithTabs here, if they differs that indicates that the images are different. */
3481 pImage->cUsage++;
3482 pReq->u.Out.pvImageBase = pImage->pvImage;
3483 pReq->u.Out.fNeedsLoading = pImage->uState == SUP_IOCTL_LDR_OPEN;
3484 pReq->u.Out.fNativeLoader = pImage->fNative;
3485 supdrvLdrAddUsage(pSession, pImage);
3486 supdrvLdrUnlock(pDevExt);
3487 return VINF_SUCCESS;
3488 }
3489 }
3490 /* (not found - add it!) */
3491
3492 /*
3493 * Allocate memory.
3494 */
3495 pv = RTMemAlloc(RT_OFFSETOF(SUPDRVLDRIMAGE, szName[cchName + 1]));
3496 if (!pv)
3497 {
3498 supdrvLdrUnlock(pDevExt);
3499 Log(("supdrvIOCtl_LdrOpen: RTMemAlloc() failed\n"));
3500 return VERR_NO_MEMORY;
3501 }
3502
3503 /*
3504 * Setup and link in the LDR stuff.
3505 */
3506 pImage = (PSUPDRVLDRIMAGE)pv;
3507 pImage->pvImage = NULL;
3508 pImage->pvImageAlloc = NULL;
3509 pImage->cbImageWithTabs = pReq->u.In.cbImageWithTabs;
3510 pImage->cbImageBits = pReq->u.In.cbImageBits;
3511 pImage->cSymbols = 0;
3512 pImage->paSymbols = NULL;
3513 pImage->pachStrTab = NULL;
3514 pImage->cbStrTab = 0;
3515 pImage->pfnModuleInit = NULL;
3516 pImage->pfnModuleTerm = NULL;
3517 pImage->pfnServiceReqHandler = NULL;
3518 pImage->uState = SUP_IOCTL_LDR_OPEN;
3519 pImage->cUsage = 1;
3520 memcpy(pImage->szName, pReq->u.In.szName, cchName + 1);
3521
3522 /*
3523 * Try load it using the native loader, if that isn't supported, fall back
3524 * on the older method.
3525 */
3526 pImage->fNative = true;
3527 rc = supdrvOSLdrOpen(pDevExt, pImage, pReq->u.In.szFilename);
3528 if (rc == VERR_NOT_SUPPORTED)
3529 {
3530 pImage->pvImageAlloc = RTMemExecAlloc(pImage->cbImageBits + 31);
3531 pImage->pvImage = RT_ALIGN_P(pImage->pvImageAlloc, 32);
3532 pImage->fNative = false;
3533 rc = pImage->pvImageAlloc ? VINF_SUCCESS : VERR_NO_MEMORY;
3534 }
3535 if (RT_FAILURE(rc))
3536 {
3537 supdrvLdrUnlock(pDevExt);
3538 RTMemFree(pImage);
3539 Log(("supdrvIOCtl_LdrOpen(%s): failed - %Rrc\n", pReq->u.In.szName, rc));
3540 return rc;
3541 }
3542 Assert(VALID_PTR(pImage->pvImage) || RT_FAILURE(rc));
3543
3544 /*
3545 * Link it.
3546 */
3547 pImage->pNext = pDevExt->pLdrImages;
3548 pDevExt->pLdrImages = pImage;
3549
3550 supdrvLdrAddUsage(pSession, pImage);
3551
3552 pReq->u.Out.pvImageBase = pImage->pvImage;
3553 pReq->u.Out.fNeedsLoading = true;
3554 pReq->u.Out.fNativeLoader = pImage->fNative;
3555 supdrvLdrUnlock(pDevExt);
3556
3557#if defined(RT_OS_WINDOWS) && defined(DEBUG)
3558 SUPR0Printf("VBoxDrv: windbg> .reload /f %s=%#p\n", pImage->szName, pImage->pvImage);
3559#endif
3560 return VINF_SUCCESS;
3561}
3562
3563
3564/**
3565 * Worker that validates a pointer to an image entrypoint.
3566 *
3567 * @returns IPRT status code.
3568 * @param pDevExt The device globals.
3569 * @param pImage The loader image.
3570 * @param pv The pointer into the image.
3571 * @param fMayBeNull Whether it may be NULL.
3572 * @param pszWhat What is this entrypoint? (for logging)
3573 * @param pbImageBits The image bits prepared by ring-3.
3574 *
3575 * @remarks Will leave the lock on failure.
3576 */
3577static int supdrvLdrValidatePointer(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, void *pv,
3578 bool fMayBeNull, const uint8_t *pbImageBits, const char *pszWhat)
3579{
3580 if (!fMayBeNull || pv)
3581 {
3582 if ((uintptr_t)pv - (uintptr_t)pImage->pvImage >= pImage->cbImageBits)
3583 {
3584 supdrvLdrUnlock(pDevExt);
3585 Log(("Out of range (%p LB %#x): %s=%p\n", pImage->pvImage, pImage->cbImageBits, pszWhat, pv));
3586 return VERR_INVALID_PARAMETER;
3587 }
3588
3589 if (pImage->fNative)
3590 {
3591 int rc = supdrvOSLdrValidatePointer(pDevExt, pImage, pv, pbImageBits);
3592 if (RT_FAILURE(rc))
3593 {
3594 supdrvLdrUnlock(pDevExt);
3595 Log(("Bad entry point address: %s=%p (rc=%Rrc)\n", pszWhat, pv, rc));
3596 return rc;
3597 }
3598 }
3599 }
3600 return VINF_SUCCESS;
3601}
3602
3603
3604/**
3605 * Loads the image bits.
3606 *
3607 * This is the 2nd step of the loading.
3608 *
3609 * @returns IPRT status code.
3610 * @param pDevExt Device globals.
3611 * @param pSession Session data.
3612 * @param pReq The request.
3613 */
3614static int supdrvIOCtl_LdrLoad(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDRLOAD pReq)
3615{
3616 PSUPDRVLDRUSAGE pUsage;
3617 PSUPDRVLDRIMAGE pImage;
3618 int rc;
3619 LogFlow(("supdrvIOCtl_LdrLoad: pvImageBase=%p cbImageWithBits=%d\n", pReq->u.In.pvImageBase, pReq->u.In.cbImageWithTabs));
3620
3621 /*
3622 * Find the ldr image.
3623 */
3624 supdrvLdrLock(pDevExt);
3625 pUsage = pSession->pLdrUsage;
3626 while (pUsage && pUsage->pImage->pvImage != pReq->u.In.pvImageBase)
3627 pUsage = pUsage->pNext;
3628 if (!pUsage)
3629 {
3630 supdrvLdrUnlock(pDevExt);
3631 Log(("SUP_IOCTL_LDR_LOAD: couldn't find image!\n"));
3632 return VERR_INVALID_HANDLE;
3633 }
3634 pImage = pUsage->pImage;
3635
3636 /*
3637 * Validate input.
3638 */
3639 if ( pImage->cbImageWithTabs != pReq->u.In.cbImageWithTabs
3640 || pImage->cbImageBits != pReq->u.In.cbImageBits)
3641 {
3642 supdrvLdrUnlock(pDevExt);
3643 Log(("SUP_IOCTL_LDR_LOAD: image size mismatch!! %d(prep) != %d(load) or %d != %d\n",
3644 pImage->cbImageWithTabs, pReq->u.In.cbImageWithTabs, pImage->cbImageBits, pReq->u.In.cbImageBits));
3645 return VERR_INVALID_HANDLE;
3646 }
3647
3648 if (pImage->uState != SUP_IOCTL_LDR_OPEN)
3649 {
3650 unsigned uState = pImage->uState;
3651 supdrvLdrUnlock(pDevExt);
3652 if (uState != SUP_IOCTL_LDR_LOAD)
3653 AssertMsgFailed(("SUP_IOCTL_LDR_LOAD: invalid image state %d (%#x)!\n", uState, uState));
3654 return SUPDRV_ERR_ALREADY_LOADED;
3655 }
3656
3657 switch (pReq->u.In.eEPType)
3658 {
3659 case SUPLDRLOADEP_NOTHING:
3660 break;
3661
3662 case SUPLDRLOADEP_VMMR0:
3663 rc = supdrvLdrValidatePointer( pDevExt, pImage, pReq->u.In.EP.VMMR0.pvVMMR0, false, pReq->u.In.achImage, "pvVMMR0");
3664 if (RT_SUCCESS(rc))
3665 rc = supdrvLdrValidatePointer(pDevExt, pImage, pReq->u.In.EP.VMMR0.pvVMMR0EntryInt, false, pReq->u.In.achImage, "pvVMMR0EntryInt");
3666 if (RT_SUCCESS(rc))
3667 rc = supdrvLdrValidatePointer(pDevExt, pImage, pReq->u.In.EP.VMMR0.pvVMMR0EntryFast, false, pReq->u.In.achImage, "pvVMMR0EntryFast");
3668 if (RT_SUCCESS(rc))
3669 rc = supdrvLdrValidatePointer(pDevExt, pImage, pReq->u.In.EP.VMMR0.pvVMMR0EntryEx, false, pReq->u.In.achImage, "pvVMMR0EntryEx");
3670 if (RT_FAILURE(rc))
3671 return rc;
3672 break;
3673
3674 case SUPLDRLOADEP_SERVICE:
3675 rc = supdrvLdrValidatePointer(pDevExt, pImage, pReq->u.In.EP.Service.pfnServiceReq, false, pReq->u.In.achImage, "pfnServiceReq");
3676 if (RT_FAILURE(rc))
3677 return rc;
3678 if ( pReq->u.In.EP.Service.apvReserved[0] != NIL_RTR0PTR
3679 || pReq->u.In.EP.Service.apvReserved[1] != NIL_RTR0PTR
3680 || pReq->u.In.EP.Service.apvReserved[2] != NIL_RTR0PTR)
3681 {
3682 supdrvLdrUnlock(pDevExt);
3683 Log(("Out of range (%p LB %#x): apvReserved={%p,%p,%p} MBZ!\n",
3684 pImage->pvImage, pReq->u.In.cbImageWithTabs,
3685 pReq->u.In.EP.Service.apvReserved[0],
3686 pReq->u.In.EP.Service.apvReserved[1],
3687 pReq->u.In.EP.Service.apvReserved[2]));
3688 return VERR_INVALID_PARAMETER;
3689 }
3690 break;
3691
3692 default:
3693 supdrvLdrUnlock(pDevExt);
3694 Log(("Invalid eEPType=%d\n", pReq->u.In.eEPType));
3695 return VERR_INVALID_PARAMETER;
3696 }
3697
3698 rc = supdrvLdrValidatePointer(pDevExt, pImage, pReq->u.In.pfnModuleInit, true, pReq->u.In.achImage, "pfnModuleInit");
3699 if (RT_FAILURE(rc))
3700 return rc;
3701 rc = supdrvLdrValidatePointer(pDevExt, pImage, pReq->u.In.pfnModuleTerm, true, pReq->u.In.achImage, "pfnModuleTerm");
3702 if (RT_FAILURE(rc))
3703 return rc;
3704
3705 /*
3706 * Allocate and copy the tables.
3707 * (No need to do try/except as this is a buffered request.)
3708 */
3709 pImage->cbStrTab = pReq->u.In.cbStrTab;
3710 if (pImage->cbStrTab)
3711 {
3712 pImage->pachStrTab = (char *)RTMemAlloc(pImage->cbStrTab);
3713 if (pImage->pachStrTab)
3714 memcpy(pImage->pachStrTab, &pReq->u.In.achImage[pReq->u.In.offStrTab], pImage->cbStrTab);
3715 else
3716 rc = VERR_NO_MEMORY;
3717 }
3718
3719 pImage->cSymbols = pReq->u.In.cSymbols;
3720 if (RT_SUCCESS(rc) && pImage->cSymbols)
3721 {
3722 size_t cbSymbols = pImage->cSymbols * sizeof(SUPLDRSYM);
3723 pImage->paSymbols = (PSUPLDRSYM)RTMemAlloc(cbSymbols);
3724 if (pImage->paSymbols)
3725 memcpy(pImage->paSymbols, &pReq->u.In.achImage[pReq->u.In.offSymbols], cbSymbols);
3726 else
3727 rc = VERR_NO_MEMORY;
3728 }
3729
3730 /*
3731 * Copy the bits / complete native loading.
3732 */
3733 if (RT_SUCCESS(rc))
3734 {
3735 pImage->uState = SUP_IOCTL_LDR_LOAD;
3736 pImage->pfnModuleInit = pReq->u.In.pfnModuleInit;
3737 pImage->pfnModuleTerm = pReq->u.In.pfnModuleTerm;
3738
3739 if (pImage->fNative)
3740 rc = supdrvOSLdrLoad(pDevExt, pImage, pReq->u.In.achImage);
3741 else
3742 memcpy(pImage->pvImage, &pReq->u.In.achImage[0], pImage->cbImageBits);
3743 }
3744
3745 /*
3746 * Update any entry points.
3747 */
3748 if (RT_SUCCESS(rc))
3749 {
3750 switch (pReq->u.In.eEPType)
3751 {
3752 default:
3753 case SUPLDRLOADEP_NOTHING:
3754 rc = VINF_SUCCESS;
3755 break;
3756 case SUPLDRLOADEP_VMMR0:
3757 rc = supdrvLdrSetVMMR0EPs(pDevExt, pReq->u.In.EP.VMMR0.pvVMMR0, pReq->u.In.EP.VMMR0.pvVMMR0EntryInt,
3758 pReq->u.In.EP.VMMR0.pvVMMR0EntryFast, pReq->u.In.EP.VMMR0.pvVMMR0EntryEx);
3759 break;
3760 case SUPLDRLOADEP_SERVICE:
3761 pImage->pfnServiceReqHandler = pReq->u.In.EP.Service.pfnServiceReq;
3762 rc = VINF_SUCCESS;
3763 break;
3764 }
3765 }
3766
3767 /*
3768 * On success call the module initialization.
3769 */
3770 LogFlow(("supdrvIOCtl_LdrLoad: pfnModuleInit=%p\n", pImage->pfnModuleInit));
3771 if (RT_SUCCESS(rc) && pImage->pfnModuleInit)
3772 {
3773 Log(("supdrvIOCtl_LdrLoad: calling pfnModuleInit=%p\n", pImage->pfnModuleInit));
3774#ifdef RT_WITH_W64_UNWIND_HACK
3775 rc = supdrvNtWrapModuleInit((PFNRT)pImage->pfnModuleInit);
3776#else
3777 rc = pImage->pfnModuleInit();
3778#endif
3779 if (rc && pDevExt->pvVMMR0 == pImage->pvImage)
3780 supdrvLdrUnsetVMMR0EPs(pDevExt);
3781 }
3782
3783 if (RT_FAILURE(rc))
3784 {
3785 pImage->uState = SUP_IOCTL_LDR_OPEN;
3786 pImage->pfnModuleInit = NULL;
3787 pImage->pfnModuleTerm = NULL;
3788 pImage->pfnServiceReqHandler= NULL;
3789 pImage->cbStrTab = 0;
3790 RTMemFree(pImage->pachStrTab);
3791 pImage->pachStrTab = NULL;
3792 RTMemFree(pImage->paSymbols);
3793 pImage->paSymbols = NULL;
3794 pImage->cSymbols = 0;
3795 }
3796
3797 supdrvLdrUnlock(pDevExt);
3798 return rc;
3799}
3800
3801
3802/**
3803 * Frees a previously loaded (prep'ed) image.
3804 *
3805 * @returns IPRT status code.
3806 * @param pDevExt Device globals.
3807 * @param pSession Session data.
3808 * @param pReq The request.
3809 */
3810static int supdrvIOCtl_LdrFree(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDRFREE pReq)
3811{
3812 int rc;
3813 PSUPDRVLDRUSAGE pUsagePrev;
3814 PSUPDRVLDRUSAGE pUsage;
3815 PSUPDRVLDRIMAGE pImage;
3816 LogFlow(("supdrvIOCtl_LdrFree: pvImageBase=%p\n", pReq->u.In.pvImageBase));
3817
3818 /*
3819 * Find the ldr image.
3820 */
3821 supdrvLdrLock(pDevExt);
3822 pUsagePrev = NULL;
3823 pUsage = pSession->pLdrUsage;
3824 while (pUsage && pUsage->pImage->pvImage != pReq->u.In.pvImageBase)
3825 {
3826 pUsagePrev = pUsage;
3827 pUsage = pUsage->pNext;
3828 }
3829 if (!pUsage)
3830 {
3831 supdrvLdrUnlock(pDevExt);
3832 Log(("SUP_IOCTL_LDR_FREE: couldn't find image!\n"));
3833 return VERR_INVALID_HANDLE;
3834 }
3835
3836 /*
3837 * Check if we can remove anything.
3838 */
3839 rc = VINF_SUCCESS;
3840 pImage = pUsage->pImage;
3841 if (pImage->cUsage <= 1 || pUsage->cUsage <= 1)
3842 {
3843 /*
3844 * Check if there are any objects with destructors in the image, if
3845 * so leave it for the session cleanup routine so we get a chance to
3846 * clean things up in the right order and not leave them all dangling.
3847 */
3848 RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
3849 RTSpinlockAcquire(pDevExt->Spinlock, &SpinlockTmp);
3850 if (pImage->cUsage <= 1)
3851 {
3852 PSUPDRVOBJ pObj;
3853 for (pObj = pDevExt->pObjs; pObj; pObj = pObj->pNext)
3854 if (RT_UNLIKELY((uintptr_t)pObj->pfnDestructor - (uintptr_t)pImage->pvImage < pImage->cbImageBits))
3855 {
3856 rc = VERR_DANGLING_OBJECTS;
3857 break;
3858 }
3859 }
3860 else
3861 {
3862 PSUPDRVUSAGE pGenUsage;
3863 for (pGenUsage = pSession->pUsage; pGenUsage; pGenUsage = pGenUsage->pNext)
3864 if (RT_UNLIKELY((uintptr_t)pGenUsage->pObj->pfnDestructor - (uintptr_t)pImage->pvImage < pImage->cbImageBits))
3865 {
3866 rc = VERR_DANGLING_OBJECTS;
3867 break;
3868 }
3869 }
3870 RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
3871 if (rc == VINF_SUCCESS)
3872 {
3873 /* unlink it */
3874 if (pUsagePrev)
3875 pUsagePrev->pNext = pUsage->pNext;
3876 else
3877 pSession->pLdrUsage = pUsage->pNext;
3878
3879 /* free it */
3880 pUsage->pImage = NULL;
3881 pUsage->pNext = NULL;
3882 RTMemFree(pUsage);
3883
3884 /*
3885 * Dereference the image.
3886 */
3887 if (pImage->cUsage <= 1)
3888 supdrvLdrFree(pDevExt, pImage);
3889 else
3890 pImage->cUsage--;
3891 }
3892 else
3893 {
3894 Log(("supdrvIOCtl_LdrFree: Dangling objects in %p/%s!\n", pImage->pvImage, pImage->szName));
3895 rc = VINF_SUCCESS; /** @todo BRANCH-2.1: remove this after branching. */
3896 }
3897 }
3898 else
3899 {
3900 /*
3901 * Dereference both image and usage.
3902 */
3903 pImage->cUsage--;
3904 pUsage->cUsage--;
3905 }
3906
3907 supdrvLdrUnlock(pDevExt);
3908 return rc;
3909}
3910
3911
3912/**
3913 * Gets the address of a symbol in an open image.
3914 *
3915 * @returns 0 on success.
3916 * @returns SUPDRV_ERR_* on failure.
3917 * @param pDevExt Device globals.
3918 * @param pSession Session data.
3919 * @param pReq The request buffer.
3920 */
3921static int supdrvIOCtl_LdrGetSymbol(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDRGETSYMBOL pReq)
3922{
3923 PSUPDRVLDRIMAGE pImage;
3924 PSUPDRVLDRUSAGE pUsage;
3925 uint32_t i;
3926 PSUPLDRSYM paSyms;
3927 const char *pchStrings;
3928 const size_t cbSymbol = strlen(pReq->u.In.szSymbol) + 1;
3929 void *pvSymbol = NULL;
3930 int rc = VERR_GENERAL_FAILURE;
3931 Log3(("supdrvIOCtl_LdrGetSymbol: pvImageBase=%p szSymbol=\"%s\"\n", pReq->u.In.pvImageBase, pReq->u.In.szSymbol));
3932
3933 /*
3934 * Find the ldr image.
3935 */
3936 supdrvLdrLock(pDevExt);
3937 pUsage = pSession->pLdrUsage;
3938 while (pUsage && pUsage->pImage->pvImage != pReq->u.In.pvImageBase)
3939 pUsage = pUsage->pNext;
3940 if (!pUsage)
3941 {
3942 supdrvLdrUnlock(pDevExt);
3943 Log(("SUP_IOCTL_LDR_GET_SYMBOL: couldn't find image!\n"));
3944 return VERR_INVALID_HANDLE;
3945 }
3946 pImage = pUsage->pImage;
3947 if (pImage->uState != SUP_IOCTL_LDR_LOAD)
3948 {
3949 unsigned uState = pImage->uState;
3950 supdrvLdrUnlock(pDevExt);
3951 Log(("SUP_IOCTL_LDR_GET_SYMBOL: invalid image state %d (%#x)!\n", uState, uState)); NOREF(uState);
3952 return VERR_ALREADY_LOADED;
3953 }
3954
3955 /*
3956 * Search the symbol strings.
3957 */
3958 pchStrings = pImage->pachStrTab;
3959 paSyms = pImage->paSymbols;
3960 for (i = 0; i < pImage->cSymbols; i++)
3961 {
3962 if ( paSyms[i].offSymbol < pImage->cbImageBits /* paranoia */
3963 && paSyms[i].offName + cbSymbol <= pImage->cbStrTab
3964 && !memcmp(pchStrings + paSyms[i].offName, pReq->u.In.szSymbol, cbSymbol))
3965 {
3966 pvSymbol = (uint8_t *)pImage->pvImage + paSyms[i].offSymbol;
3967 rc = VINF_SUCCESS;
3968 break;
3969 }
3970 }
3971 supdrvLdrUnlock(pDevExt);
3972 pReq->u.Out.pvSymbol = pvSymbol;
3973 return rc;
3974}
3975
3976
3977/**
3978 * Gets the address of a symbol in an open image or the support driver.
3979 *
3980 * @returns VINF_SUCCESS on success.
3981 * @returns
3982 * @param pDevExt Device globals.
3983 * @param pSession Session data.
3984 * @param pReq The request buffer.
3985 */
3986static int supdrvIDC_LdrGetSymbol(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVIDCREQGETSYM pReq)
3987{
3988 int rc = VINF_SUCCESS;
3989 const char *pszSymbol = pReq->u.In.pszSymbol;
3990 const char *pszModule = pReq->u.In.pszModule;
3991 size_t cbSymbol;
3992 char const *pszEnd;
3993 uint32_t i;
3994
3995 /*
3996 * Input validation.
3997 */
3998 AssertPtrReturn(pszSymbol, VERR_INVALID_POINTER);
3999 pszEnd = (char *)memchr(pszSymbol, '\0', 512);
4000 AssertReturn(pszEnd, VERR_INVALID_PARAMETER);
4001 cbSymbol = pszEnd - pszSymbol + 1;
4002
4003 if (pszModule)
4004 {
4005 AssertPtrReturn(pszModule, VERR_INVALID_POINTER);
4006 pszEnd = (char *)memchr(pszModule, '\0', 64);
4007 AssertReturn(pszEnd, VERR_INVALID_PARAMETER);
4008 }
4009 Log3(("supdrvIDC_LdrGetSymbol: pszModule=%p:{%s} pszSymbol=%p:{%s}\n", pszModule, pszModule, pszSymbol, pszSymbol));
4010
4011
4012 if ( !pszModule
4013 || !strcmp(pszModule, "SupDrv"))
4014 {
4015 /*
4016 * Search the support driver export table.
4017 */
4018 for (i = 0; i < RT_ELEMENTS(g_aFunctions); i++)
4019 if (!strcmp(g_aFunctions[i].szName, pszSymbol))
4020 {
4021 pReq->u.Out.pfnSymbol = g_aFunctions[i].pfn;
4022 break;
4023 }
4024 }
4025 else
4026 {
4027 /*
4028 * Find the loader image.
4029 */
4030 PSUPDRVLDRIMAGE pImage;
4031
4032 supdrvLdrLock(pDevExt);
4033
4034 for (pImage = pDevExt->pLdrImages; pImage; pImage = pImage->pNext)
4035 if (!strcmp(pImage->szName, pszModule))
4036 break;
4037 if (pImage && pImage->uState == SUP_IOCTL_LDR_LOAD)
4038 {
4039 /*
4040 * Search the symbol strings.
4041 */
4042 const char *pchStrings = pImage->pachStrTab;
4043 PCSUPLDRSYM paSyms = pImage->paSymbols;
4044 for (i = 0; i < pImage->cSymbols; i++)
4045 {
4046 if ( paSyms[i].offSymbol < pImage->cbImageBits /* paranoia */
4047 && paSyms[i].offName + cbSymbol <= pImage->cbStrTab
4048 && !memcmp(pchStrings + paSyms[i].offName, pszSymbol, cbSymbol))
4049 {
4050 /*
4051 * Found it! Calc the symbol address and add a reference to the module.
4052 */
4053 pReq->u.Out.pfnSymbol = (PFNRT)((uint8_t *)pImage->pvImage + paSyms[i].offSymbol);
4054 rc = supdrvLdrAddUsage(pSession, pImage);
4055 break;
4056 }
4057 }
4058 }
4059 else
4060 rc = pImage ? VERR_WRONG_ORDER : VERR_MODULE_NOT_FOUND;
4061
4062 supdrvLdrUnlock(pDevExt);
4063 }
4064 return rc;
4065}
4066
4067
4068/**
4069 * Updates the VMMR0 entry point pointers.
4070 *
4071 * @returns IPRT status code.
4072 * @param pDevExt Device globals.
4073 * @param pSession Session data.
4074 * @param pVMMR0 VMMR0 image handle.
4075 * @param pvVMMR0EntryInt VMMR0EntryInt address.
4076 * @param pvVMMR0EntryFast VMMR0EntryFast address.
4077 * @param pvVMMR0EntryEx VMMR0EntryEx address.
4078 * @remark Caller must own the loader mutex.
4079 */
4080static int supdrvLdrSetVMMR0EPs(PSUPDRVDEVEXT pDevExt, void *pvVMMR0, void *pvVMMR0EntryInt, void *pvVMMR0EntryFast, void *pvVMMR0EntryEx)
4081{
4082 int rc = VINF_SUCCESS;
4083 LogFlow(("supdrvLdrSetR0EP pvVMMR0=%p pvVMMR0EntryInt=%p\n", pvVMMR0, pvVMMR0EntryInt));
4084
4085
4086 /*
4087 * Check if not yet set.
4088 */
4089 if (!pDevExt->pvVMMR0)
4090 {
4091 pDevExt->pvVMMR0 = pvVMMR0;
4092 pDevExt->pfnVMMR0EntryInt = pvVMMR0EntryInt;
4093 pDevExt->pfnVMMR0EntryFast = pvVMMR0EntryFast;
4094 pDevExt->pfnVMMR0EntryEx = pvVMMR0EntryEx;
4095 }
4096 else
4097 {
4098 /*
4099 * Return failure or success depending on whether the values match or not.
4100 */
4101 if ( pDevExt->pvVMMR0 != pvVMMR0
4102 || (void *)pDevExt->pfnVMMR0EntryInt != pvVMMR0EntryInt
4103 || (void *)pDevExt->pfnVMMR0EntryFast != pvVMMR0EntryFast
4104 || (void *)pDevExt->pfnVMMR0EntryEx != pvVMMR0EntryEx)
4105 {
4106 AssertMsgFailed(("SUP_IOCTL_LDR_SETR0EP: Already set pointing to a different module!\n"));
4107 rc = VERR_INVALID_PARAMETER;
4108 }
4109 }
4110 return rc;
4111}
4112
4113
4114/**
4115 * Unsets the VMMR0 entry point installed by supdrvLdrSetR0EP.
4116 *
4117 * @param pDevExt Device globals.
4118 */
4119static void supdrvLdrUnsetVMMR0EPs(PSUPDRVDEVEXT pDevExt)
4120{
4121 pDevExt->pvVMMR0 = NULL;
4122 pDevExt->pfnVMMR0EntryInt = NULL;
4123 pDevExt->pfnVMMR0EntryFast = NULL;
4124 pDevExt->pfnVMMR0EntryEx = NULL;
4125}
4126
4127
4128/**
4129 * Adds a usage reference in the specified session of an image.
4130 *
4131 * Called while owning the loader semaphore.
4132 *
4133 * @returns VINF_SUCCESS on success and VERR_NO_MEMORY on failure.
4134 * @param pSession Session in question.
4135 * @param pImage Image which the session is using.
4136 */
4137static int supdrvLdrAddUsage(PSUPDRVSESSION pSession, PSUPDRVLDRIMAGE pImage)
4138{
4139 PSUPDRVLDRUSAGE pUsage;
4140 LogFlow(("supdrvLdrAddUsage: pImage=%p\n", pImage));
4141
4142 /*
4143 * Referenced it already?
4144 */
4145 pUsage = pSession->pLdrUsage;
4146 while (pUsage)
4147 {
4148 if (pUsage->pImage == pImage)
4149 {
4150 pUsage->cUsage++;
4151 return VINF_SUCCESS;
4152 }
4153 pUsage = pUsage->pNext;
4154 }
4155
4156 /*
4157 * Allocate new usage record.
4158 */
4159 pUsage = (PSUPDRVLDRUSAGE)RTMemAlloc(sizeof(*pUsage));
4160 AssertReturn(pUsage, VERR_NO_MEMORY);
4161 pUsage->cUsage = 1;
4162 pUsage->pImage = pImage;
4163 pUsage->pNext = pSession->pLdrUsage;
4164 pSession->pLdrUsage = pUsage;
4165 return VINF_SUCCESS;
4166}
4167
4168
4169/**
4170 * Frees a load image.
4171 *
4172 * @param pDevExt Pointer to device extension.
4173 * @param pImage Pointer to the image we're gonna free.
4174 * This image must exit!
4175 * @remark The caller MUST own SUPDRVDEVEXT::mtxLdr!
4176 */
4177static void supdrvLdrFree(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
4178{
4179 PSUPDRVLDRIMAGE pImagePrev;
4180 LogFlow(("supdrvLdrFree: pImage=%p\n", pImage));
4181
4182 /* find it - arg. should've used doubly linked list. */
4183 Assert(pDevExt->pLdrImages);
4184 pImagePrev = NULL;
4185 if (pDevExt->pLdrImages != pImage)
4186 {
4187 pImagePrev = pDevExt->pLdrImages;
4188 while (pImagePrev->pNext != pImage)
4189 pImagePrev = pImagePrev->pNext;
4190 Assert(pImagePrev->pNext == pImage);
4191 }
4192
4193 /* unlink */
4194 if (pImagePrev)
4195 pImagePrev->pNext = pImage->pNext;
4196 else
4197 pDevExt->pLdrImages = pImage->pNext;
4198
4199 /* check if this is VMMR0.r0 unset its entry point pointers. */
4200 if (pDevExt->pvVMMR0 == pImage->pvImage)
4201 supdrvLdrUnsetVMMR0EPs(pDevExt);
4202
4203 /* check for objects with destructors in this image. (Shouldn't happen.) */
4204 if (pDevExt->pObjs)
4205 {
4206 unsigned cObjs = 0;
4207 PSUPDRVOBJ pObj;
4208 RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
4209 RTSpinlockAcquire(pDevExt->Spinlock, &SpinlockTmp);
4210 for (pObj = pDevExt->pObjs; pObj; pObj = pObj->pNext)
4211 if (RT_UNLIKELY((uintptr_t)pObj->pfnDestructor - (uintptr_t)pImage->pvImage < pImage->cbImageBits))
4212 {
4213 pObj->pfnDestructor = NULL;
4214 cObjs++;
4215 }
4216 RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
4217 if (cObjs)
4218 OSDBGPRINT(("supdrvLdrFree: Image '%s' has %d dangling objects!\n", pImage->szName, cObjs));
4219 }
4220
4221 /* call termination function if fully loaded. */
4222 if ( pImage->pfnModuleTerm
4223 && pImage->uState == SUP_IOCTL_LDR_LOAD)
4224 {
4225 LogFlow(("supdrvIOCtl_LdrLoad: calling pfnModuleTerm=%p\n", pImage->pfnModuleTerm));
4226#ifdef RT_WITH_W64_UNWIND_HACK
4227 supdrvNtWrapModuleTerm(pImage->pfnModuleTerm);
4228#else
4229 pImage->pfnModuleTerm();
4230#endif
4231 }
4232
4233 /* do native unload if appropriate. */
4234 if (pImage->fNative)
4235 supdrvOSLdrUnload(pDevExt, pImage);
4236
4237 /* free the image */
4238 pImage->cUsage = 0;
4239 pImage->pNext = 0;
4240 pImage->uState = SUP_IOCTL_LDR_FREE;
4241 RTMemExecFree(pImage->pvImageAlloc);
4242 pImage->pvImageAlloc = NULL;
4243 RTMemFree(pImage->pachStrTab);
4244 pImage->pachStrTab = NULL;
4245 RTMemFree(pImage->paSymbols);
4246 pImage->paSymbols = NULL;
4247 RTMemFree(pImage);
4248}
4249
4250
4251/**
4252 * Acquires the loader lock.
4253 *
4254 * @returns IPRT status code.
4255 * @param pDevExt The device extension.
4256 */
4257DECLINLINE(int) supdrvLdrLock(PSUPDRVDEVEXT pDevExt)
4258{
4259#ifdef SUPDRV_USE_MUTEX_FOR_LDR
4260 return RTSemMutexRequest(pDevExt->mtxLdr, RT_INDEFINITE_WAIT);
4261#else
4262 return RTSemFastMutexRequest(pDevExt->mtxLdr);
4263#endif
4264}
4265
4266
4267/**
4268 * Releases the loader lock.
4269 *
4270 * @returns IPRT status code.
4271 * @param pDevExt The device extension.
4272 */
4273DECLINLINE(int) supdrvLdrUnlock(PSUPDRVDEVEXT pDevExt)
4274{
4275#ifdef SUPDRV_USE_MUTEX_FOR_LDR
4276 return RTSemMutexRelease(pDevExt->mtxLdr);
4277#else
4278 return RTSemFastMutexRelease(pDevExt->mtxLdr);
4279#endif
4280}
4281
4282
4283/**
4284 * Implements the service call request.
4285 *
4286 * @returns VBox status code.
4287 * @param pDevExt The device extension.
4288 * @param pSession The calling session.
4289 * @param pReq The request packet, valid.
4290 */
4291static int supdrvIOCtl_CallServiceModule(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPCALLSERVICE pReq)
4292{
4293#if !defined(RT_OS_WINDOWS) || defined(DEBUG)
4294 int rc;
4295
4296 /*
4297 * Find the module first in the module referenced by the calling session.
4298 */
4299 rc = supdrvLdrLock(pDevExt);
4300 if (RT_SUCCESS(rc))
4301 {
4302 PFNSUPR0SERVICEREQHANDLER pfnServiceReqHandler = NULL;
4303 PSUPDRVLDRUSAGE pUsage;
4304
4305 for (pUsage = pSession->pLdrUsage; pUsage; pUsage = pUsage->pNext)
4306 if ( pUsage->pImage->pfnServiceReqHandler
4307 && !strcmp(pUsage->pImage->szName, pReq->u.In.szName))
4308 {
4309 pfnServiceReqHandler = pUsage->pImage->pfnServiceReqHandler;
4310 break;
4311 }
4312 supdrvLdrUnlock(pDevExt);
4313
4314 if (pfnServiceReqHandler)
4315 {
4316 /*
4317 * Call it.
4318 */
4319 if (pReq->Hdr.cbIn == SUP_IOCTL_CALL_SERVICE_SIZE(0))
4320#ifdef RT_WITH_W64_UNWIND_HACK
4321 rc = supdrvNtWrapServiceReqHandler((PFNRT)pfnServiceReqHandler, pSession, pReq->u.In.uOperation, pReq->u.In.u64Arg, NULL);
4322#else
4323 rc = pfnServiceReqHandler(pSession, pReq->u.In.uOperation, pReq->u.In.u64Arg, NULL);
4324#endif
4325 else
4326#ifdef RT_WITH_W64_UNWIND_HACK
4327 rc = supdrvNtWrapServiceReqHandler((PFNRT)pfnServiceReqHandler, pSession, pReq->u.In.uOperation,
4328 pReq->u.In.u64Arg, (PSUPR0SERVICEREQHDR)&pReq->abReqPkt[0]);
4329#else
4330 rc = pfnServiceReqHandler(pSession, pReq->u.In.uOperation, pReq->u.In.u64Arg, (PSUPR0SERVICEREQHDR)&pReq->abReqPkt[0]);
4331#endif
4332 }
4333 else
4334 rc = VERR_SUPDRV_SERVICE_NOT_FOUND;
4335 }
4336
4337 /* log it */
4338 if ( RT_FAILURE(rc)
4339 && rc != VERR_INTERRUPTED
4340 && rc != VERR_TIMEOUT)
4341 Log(("SUP_IOCTL_CALL_SERVICE: rc=%Rrc op=%u out=%u arg=%RX64 p/t=%RTproc/%RTthrd\n",
4342 rc, pReq->u.In.uOperation, pReq->Hdr.cbOut, pReq->u.In.u64Arg, RTProcSelf(), RTThreadNativeSelf()));
4343 else
4344 Log4(("SUP_IOCTL_CALL_SERVICE: rc=%Rrc op=%u out=%u arg=%RX64 p/t=%RTproc/%RTthrd\n",
4345 rc, pReq->u.In.uOperation, pReq->Hdr.cbOut, pReq->u.In.u64Arg, RTProcSelf(), RTThreadNativeSelf()));
4346 return rc;
4347#else /* RT_OS_WINDOWS && !DEBUG */
4348 return VERR_NOT_IMPLEMENTED;
4349#endif /* RT_OS_WINDOWS && !DEBUG */
4350}
4351
4352
4353/**
4354 * Implements the logger settings request.
4355 *
4356 * @returns VBox status code.
4357 * @param pDevExt The device extension.
4358 * @param pSession The caller's session.
4359 * @param pReq The request.
4360 */
4361static int supdrvIOCtl_LoggerSettings(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLOGGERSETTINGS pReq)
4362{
4363 const char *pszGroup = &pReq->u.In.szStrings[pReq->u.In.offGroups];
4364 const char *pszFlags = &pReq->u.In.szStrings[pReq->u.In.offFlags];
4365 const char *pszDest = &pReq->u.In.szStrings[pReq->u.In.offDestination];
4366 PRTLOGGER pLogger = NULL;
4367 int rc;
4368
4369 /*
4370 * Some further validation.
4371 */
4372 switch (pReq->u.In.fWhat)
4373 {
4374 case SUPLOGGERSETTINGS_WHAT_SETTINGS:
4375 case SUPLOGGERSETTINGS_WHAT_CREATE:
4376 break;
4377
4378 case SUPLOGGERSETTINGS_WHAT_DESTROY:
4379 if (*pszGroup || *pszFlags || *pszDest)
4380 return VERR_INVALID_PARAMETER;
4381 if (pReq->u.In.fWhich == SUPLOGGERSETTINGS_WHICH_RELEASE)
4382 return VERR_ACCESS_DENIED;
4383 break;
4384
4385 default:
4386 return VERR_INTERNAL_ERROR;
4387 }
4388
4389 /*
4390 * Get the logger.
4391 */
4392 switch (pReq->u.In.fWhich)
4393 {
4394 case SUPLOGGERSETTINGS_WHICH_DEBUG:
4395 pLogger = RTLogGetDefaultInstance();
4396 break;
4397
4398 case SUPLOGGERSETTINGS_WHICH_RELEASE:
4399 pLogger = RTLogRelDefaultInstance();
4400 break;
4401
4402 default:
4403 return VERR_INTERNAL_ERROR;
4404 }
4405
4406 /*
4407 * Do the job.
4408 */
4409 switch (pReq->u.In.fWhat)
4410 {
4411 case SUPLOGGERSETTINGS_WHAT_SETTINGS:
4412 if (pLogger)
4413 {
4414 rc = RTLogFlags(pLogger, pszFlags);
4415 if (RT_SUCCESS(rc))
4416 rc = RTLogGroupSettings(pLogger, pszGroup);
4417 NOREF(pszDest);
4418 }
4419 else
4420 rc = VERR_NOT_FOUND;
4421 break;
4422
4423 case SUPLOGGERSETTINGS_WHAT_CREATE:
4424 {
4425 if (pLogger)
4426 rc = VERR_ALREADY_EXISTS;
4427 else
4428 {
4429 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
4430
4431 rc = RTLogCreate(&pLogger,
4432 0 /* fFlags */,
4433 pszGroup,
4434 pReq->u.In.fWhich == SUPLOGGERSETTINGS_WHICH_DEBUG
4435 ? "VBOX_LOG"
4436 : "VBOX_RELEASE_LOG",
4437 RT_ELEMENTS(s_apszGroups),
4438 s_apszGroups,
4439 RTLOGDEST_STDOUT | RTLOGDEST_DEBUGGER,
4440 NULL);
4441 if (RT_SUCCESS(rc))
4442 {
4443 rc = RTLogFlags(pLogger, pszFlags);
4444 NOREF(pszDest);
4445 if (RT_SUCCESS(rc))
4446 {
4447 switch (pReq->u.In.fWhich)
4448 {
4449 case SUPLOGGERSETTINGS_WHICH_DEBUG:
4450 pLogger = RTLogSetDefaultInstance(pLogger);
4451 break;
4452 case SUPLOGGERSETTINGS_WHICH_RELEASE:
4453 pLogger = RTLogRelSetDefaultInstance(pLogger);
4454 break;
4455 }
4456 }
4457 RTLogDestroy(pLogger);
4458 }
4459 }
4460 break;
4461 }
4462
4463 case SUPLOGGERSETTINGS_WHAT_DESTROY:
4464 switch (pReq->u.In.fWhich)
4465 {
4466 case SUPLOGGERSETTINGS_WHICH_DEBUG:
4467 pLogger = RTLogSetDefaultInstance(NULL);
4468 break;
4469 case SUPLOGGERSETTINGS_WHICH_RELEASE:
4470 pLogger = RTLogRelSetDefaultInstance(NULL);
4471 break;
4472 }
4473 rc = RTLogDestroy(pLogger);
4474 break;
4475
4476 default:
4477 {
4478 rc = VERR_INTERNAL_ERROR;
4479 break;
4480 }
4481 }
4482
4483 return rc;
4484}
4485
4486
4487/**
4488 * Creates the GIP.
4489 *
4490 * @returns VBox status code.
4491 * @param pDevExt Instance data. GIP stuff may be updated.
4492 */
4493static int supdrvGipCreate(PSUPDRVDEVEXT pDevExt)
4494{
4495 PSUPGLOBALINFOPAGE pGip;
4496 RTHCPHYS HCPhysGip;
4497 uint32_t u32SystemResolution;
4498 uint32_t u32Interval;
4499 int rc;
4500
4501 LogFlow(("supdrvGipCreate:\n"));
4502
4503 /* assert order */
4504 Assert(pDevExt->u32SystemTimerGranularityGrant == 0);
4505 Assert(pDevExt->GipMemObj == NIL_RTR0MEMOBJ);
4506 Assert(!pDevExt->pGipTimer);
4507
4508 /*
4509 * Allocate a suitable page with a default kernel mapping.
4510 */
4511 rc = RTR0MemObjAllocLow(&pDevExt->GipMemObj, PAGE_SIZE, false);
4512 if (RT_FAILURE(rc))
4513 {
4514 OSDBGPRINT(("supdrvGipCreate: failed to allocate the GIP page. rc=%d\n", rc));
4515 return rc;
4516 }
4517 pGip = (PSUPGLOBALINFOPAGE)RTR0MemObjAddress(pDevExt->GipMemObj); AssertPtr(pGip);
4518 HCPhysGip = RTR0MemObjGetPagePhysAddr(pDevExt->GipMemObj, 0); Assert(HCPhysGip != NIL_RTHCPHYS);
4519
4520#if 0 /** @todo Disabled this as we didn't used to do it before and causes unnecessary stress on laptops.
4521 * It only applies to Windows and should probably revisited later, if possible made part of the
4522 * timer code (return min granularity in RTTimerGetSystemGranularity and set it in RTTimerStart). */
4523 /*
4524 * Try bump up the system timer resolution.
4525 * The more interrupts the better...
4526 */
4527 if ( RT_SUCCESS(RTTimerRequestSystemGranularity( 488281 /* 2048 HZ */, &u32SystemResolution))
4528 || RT_SUCCESS(RTTimerRequestSystemGranularity( 500000 /* 2000 HZ */, &u32SystemResolution))
4529 || RT_SUCCESS(RTTimerRequestSystemGranularity( 976563 /* 1024 HZ */, &u32SystemResolution))
4530 || RT_SUCCESS(RTTimerRequestSystemGranularity( 1000000 /* 1000 HZ */, &u32SystemResolution))
4531 || RT_SUCCESS(RTTimerRequestSystemGranularity( 1953125 /* 512 HZ */, &u32SystemResolution))
4532 || RT_SUCCESS(RTTimerRequestSystemGranularity( 2000000 /* 500 HZ */, &u32SystemResolution))
4533 || RT_SUCCESS(RTTimerRequestSystemGranularity( 3906250 /* 256 HZ */, &u32SystemResolution))
4534 || RT_SUCCESS(RTTimerRequestSystemGranularity( 4000000 /* 250 HZ */, &u32SystemResolution))
4535 || RT_SUCCESS(RTTimerRequestSystemGranularity( 7812500 /* 128 HZ */, &u32SystemResolution))
4536 || RT_SUCCESS(RTTimerRequestSystemGranularity(10000000 /* 100 HZ */, &u32SystemResolution))
4537 || RT_SUCCESS(RTTimerRequestSystemGranularity(15625000 /* 64 HZ */, &u32SystemResolution))
4538 || RT_SUCCESS(RTTimerRequestSystemGranularity(31250000 /* 32 HZ */, &u32SystemResolution))
4539 )
4540 {
4541 Assert(RTTimerGetSystemGranularity() <= u32SystemResolution);
4542 pDevExt->u32SystemTimerGranularityGrant = u32SystemResolution;
4543 }
4544#endif
4545
4546 /*
4547 * Find a reasonable update interval and initialize the structure.
4548 */
4549 u32Interval = u32SystemResolution = RTTimerGetSystemGranularity();
4550 while (u32Interval < 10000000 /* 10 ms */)
4551 u32Interval += u32SystemResolution;
4552
4553 supdrvGipInit(pDevExt, pGip, HCPhysGip, RTTimeSystemNanoTS(), 1000000000 / u32Interval /*=Hz*/);
4554
4555 /*
4556 * Create the timer.
4557 * If CPU_ALL isn't supported we'll have to fall back to synchronous mode.
4558 */
4559 if (pGip->u32Mode == SUPGIPMODE_ASYNC_TSC)
4560 {
4561 rc = RTTimerCreateEx(&pDevExt->pGipTimer, u32Interval, RTTIMER_FLAGS_CPU_ALL, supdrvGipAsyncTimer, pDevExt);
4562 if (rc == VERR_NOT_SUPPORTED)
4563 {
4564 OSDBGPRINT(("supdrvGipCreate: omni timer not supported, falling back to synchronous mode\n"));
4565 pGip->u32Mode = SUPGIPMODE_SYNC_TSC;
4566 }
4567 }
4568 if (pGip->u32Mode != SUPGIPMODE_ASYNC_TSC)
4569 rc = RTTimerCreateEx(&pDevExt->pGipTimer, u32Interval, 0, supdrvGipSyncTimer, pDevExt);
4570 if (RT_SUCCESS(rc))
4571 {
4572 if (pGip->u32Mode == SUPGIPMODE_ASYNC_TSC)
4573 rc = RTMpNotificationRegister(supdrvGipMpEvent, pDevExt);
4574 if (RT_SUCCESS(rc))
4575 {
4576 /*
4577 * We're good.
4578 */
4579 dprintf(("supdrvGipCreate: %ld ns interval.\n", (long)u32Interval));
4580 g_pSUPGlobalInfoPage = pGip;
4581 return VINF_SUCCESS;
4582 }
4583
4584 OSDBGPRINT(("supdrvGipCreate: failed register MP event notfication. rc=%d\n", rc));
4585 }
4586 else
4587 {
4588 OSDBGPRINT(("supdrvGipCreate: failed create GIP timer at %ld ns interval. rc=%d\n", (long)u32Interval, rc));
4589 Assert(!pDevExt->pGipTimer);
4590 }
4591 supdrvGipDestroy(pDevExt);
4592 return rc;
4593}
4594
4595
4596/**
4597 * Terminates the GIP.
4598 *
4599 * @param pDevExt Instance data. GIP stuff may be updated.
4600 */
4601static void supdrvGipDestroy(PSUPDRVDEVEXT pDevExt)
4602{
4603 int rc;
4604#ifdef DEBUG_DARWIN_GIP
4605 OSDBGPRINT(("supdrvGipDestroy: pDevExt=%p pGip=%p pGipTimer=%p GipMemObj=%p\n", pDevExt,
4606 pDevExt->GipMemObj != NIL_RTR0MEMOBJ ? RTR0MemObjAddress(pDevExt->GipMemObj) : NULL,
4607 pDevExt->pGipTimer, pDevExt->GipMemObj));
4608#endif
4609
4610 /*
4611 * Invalid the GIP data.
4612 */
4613 if (pDevExt->pGip)
4614 {
4615 supdrvGipTerm(pDevExt->pGip);
4616 pDevExt->pGip = NULL;
4617 }
4618 g_pSUPGlobalInfoPage = NULL;
4619
4620 /*
4621 * Destroy the timer and free the GIP memory object.
4622 */
4623 if (pDevExt->pGipTimer)
4624 {
4625 rc = RTTimerDestroy(pDevExt->pGipTimer); AssertRC(rc);
4626 pDevExt->pGipTimer = NULL;
4627 }
4628
4629 if (pDevExt->GipMemObj != NIL_RTR0MEMOBJ)
4630 {
4631 rc = RTR0MemObjFree(pDevExt->GipMemObj, true /* free mappings */); AssertRC(rc);
4632 pDevExt->GipMemObj = NIL_RTR0MEMOBJ;
4633 }
4634
4635 /*
4636 * Finally, release the system timer resolution request if one succeeded.
4637 */
4638 if (pDevExt->u32SystemTimerGranularityGrant)
4639 {
4640 rc = RTTimerReleaseSystemGranularity(pDevExt->u32SystemTimerGranularityGrant); AssertRC(rc);
4641 pDevExt->u32SystemTimerGranularityGrant = 0;
4642 }
4643}
4644
4645
4646/**
4647 * Timer callback function sync GIP mode.
4648 * @param pTimer The timer.
4649 * @param pvUser The device extension.
4650 */
4651static DECLCALLBACK(void) supdrvGipSyncTimer(PRTTIMER pTimer, void *pvUser, uint64_t iTick)
4652{
4653 RTCCUINTREG fOldFlags = ASMIntDisableFlags(); /* No interruptions please (real problem on S10). */
4654 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pvUser;
4655 uint64_t u64TSC = ASMReadTSC();
4656 uint64_t NanoTS = RTTimeSystemNanoTS();
4657
4658 supdrvGipUpdate(pDevExt->pGip, NanoTS, u64TSC);
4659
4660 ASMSetFlags(fOldFlags);
4661}
4662
4663
4664/**
4665 * Timer callback function for async GIP mode.
4666 * @param pTimer The timer.
4667 * @param pvUser The device extension.
4668 */
4669static DECLCALLBACK(void) supdrvGipAsyncTimer(PRTTIMER pTimer, void *pvUser, uint64_t iTick)
4670{
4671 RTCCUINTREG fOldFlags = ASMIntDisableFlags(); /* No interruptions please (real problem on S10). */
4672 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pvUser;
4673 RTCPUID idCpu = RTMpCpuId();
4674 uint64_t u64TSC = ASMReadTSC();
4675 uint64_t NanoTS = RTTimeSystemNanoTS();
4676
4677 /** @todo reset the transaction number and whatnot when iTick == 1. */
4678 if (pDevExt->idGipMaster == idCpu)
4679 supdrvGipUpdate(pDevExt->pGip, NanoTS, u64TSC);
4680 else
4681 supdrvGipUpdatePerCpu(pDevExt->pGip, NanoTS, u64TSC, ASMGetApicId());
4682
4683 ASMSetFlags(fOldFlags);
4684}
4685
4686
4687/**
4688 * Multiprocessor event notification callback.
4689 *
4690 * This is used to make sue that the GIP master gets passed on to
4691 * another CPU.
4692 *
4693 * @param enmEvent The event.
4694 * @param idCpu The cpu it applies to.
4695 * @param pvUser Pointer to the device extension.
4696 */
4697static DECLCALLBACK(void) supdrvGipMpEvent(RTMPEVENT enmEvent, RTCPUID idCpu, void *pvUser)
4698{
4699 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pvUser;
4700 if (enmEvent == RTMPEVENT_OFFLINE)
4701 {
4702 RTCPUID idGipMaster;
4703 ASMAtomicReadSize(&pDevExt->idGipMaster, &idGipMaster);
4704 if (idGipMaster == idCpu)
4705 {
4706 /*
4707 * Find a new GIP master.
4708 */
4709 bool fIgnored;
4710 unsigned i;
4711 RTCPUID idNewGipMaster = NIL_RTCPUID;
4712 RTCPUSET OnlineCpus;
4713 RTMpGetOnlineSet(&OnlineCpus);
4714
4715 for (i = 0; i < RTCPUSET_MAX_CPUS; i++)
4716 {
4717 RTCPUID idCurCpu = RTMpCpuIdFromSetIndex(i);
4718 if ( RTCpuSetIsMember(&OnlineCpus, idCurCpu)
4719 && idCurCpu != idGipMaster)
4720 {
4721 idNewGipMaster = idCurCpu;
4722 break;
4723 }
4724 }
4725
4726 dprintf(("supdrvGipMpEvent: Gip master %#lx -> %#lx\n", (long)idGipMaster, (long)idNewGipMaster));
4727 ASMAtomicCmpXchgSize(&pDevExt->idGipMaster, idNewGipMaster, idGipMaster, fIgnored);
4728 NOREF(fIgnored);
4729 }
4730 }
4731}
4732
4733
4734/**
4735 * Initializes the GIP data.
4736 *
4737 * @returns IPRT status code.
4738 * @param pDevExt Pointer to the device instance data.
4739 * @param pGip Pointer to the read-write kernel mapping of the GIP.
4740 * @param HCPhys The physical address of the GIP.
4741 * @param u64NanoTS The current nanosecond timestamp.
4742 * @param uUpdateHz The update freqence.
4743 */
4744int VBOXCALL supdrvGipInit(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE pGip, RTHCPHYS HCPhys, uint64_t u64NanoTS, unsigned uUpdateHz)
4745{
4746 unsigned i;
4747#ifdef DEBUG_DARWIN_GIP
4748 OSDBGPRINT(("supdrvGipInit: pGip=%p HCPhys=%lx u64NanoTS=%llu uUpdateHz=%d\n", pGip, (long)HCPhys, u64NanoTS, uUpdateHz));
4749#else
4750 LogFlow(("supdrvGipInit: pGip=%p HCPhys=%lx u64NanoTS=%llu uUpdateHz=%d\n", pGip, (long)HCPhys, u64NanoTS, uUpdateHz));
4751#endif
4752
4753 /*
4754 * Initialize the structure.
4755 */
4756 memset(pGip, 0, PAGE_SIZE);
4757 pGip->u32Magic = SUPGLOBALINFOPAGE_MAGIC;
4758 pGip->u32Version = SUPGLOBALINFOPAGE_VERSION;
4759 pGip->u32Mode = supdrvGipDeterminTscMode(pDevExt);
4760 pGip->u32UpdateHz = uUpdateHz;
4761 pGip->u32UpdateIntervalNS = 1000000000 / uUpdateHz;
4762 pGip->u64NanoTSLastUpdateHz = u64NanoTS;
4763
4764 for (i = 0; i < RT_ELEMENTS(pGip->aCPUs); i++)
4765 {
4766 pGip->aCPUs[i].u32TransactionId = 2;
4767 pGip->aCPUs[i].u64NanoTS = u64NanoTS;
4768 pGip->aCPUs[i].u64TSC = ASMReadTSC();
4769
4770 /*
4771 * We don't know the following values until we've executed updates.
4772 * So, we'll just insert very high values.
4773 */
4774 pGip->aCPUs[i].u64CpuHz = _4G + 1;
4775 pGip->aCPUs[i].u32UpdateIntervalTSC = _2G / 4;
4776 pGip->aCPUs[i].au32TSCHistory[0] = _2G / 4;
4777 pGip->aCPUs[i].au32TSCHistory[1] = _2G / 4;
4778 pGip->aCPUs[i].au32TSCHistory[2] = _2G / 4;
4779 pGip->aCPUs[i].au32TSCHistory[3] = _2G / 4;
4780 pGip->aCPUs[i].au32TSCHistory[4] = _2G / 4;
4781 pGip->aCPUs[i].au32TSCHistory[5] = _2G / 4;
4782 pGip->aCPUs[i].au32TSCHistory[6] = _2G / 4;
4783 pGip->aCPUs[i].au32TSCHistory[7] = _2G / 4;
4784 }
4785
4786 /*
4787 * Link it to the device extension.
4788 */
4789 pDevExt->pGip = pGip;
4790 pDevExt->HCPhysGip = HCPhys;
4791 pDevExt->cGipUsers = 0;
4792
4793 return VINF_SUCCESS;
4794}
4795
4796
4797/**
4798 * Callback used by supdrvDetermineAsyncTSC to read the TSC on a CPU.
4799 *
4800 * @param idCpu Ignored.
4801 * @param pvUser1 Where to put the TSC.
4802 * @param pvUser2 Ignored.
4803 */
4804static DECLCALLBACK(void) supdrvDetermineAsyncTscWorker(RTCPUID idCpu, void *pvUser1, void *pvUser2)
4805{
4806#if 1
4807 ASMAtomicWriteU64((uint64_t volatile *)pvUser1, ASMReadTSC());
4808#else
4809 *(uint64_t *)pvUser1 = ASMReadTSC();
4810#endif
4811}
4812
4813
4814/**
4815 * Determine if Async GIP mode is required because of TSC drift.
4816 *
4817 * When using the default/normal timer code it is essential that the time stamp counter
4818 * (TSC) runs never backwards, that is, a read operation to the counter should return
4819 * a bigger value than any previous read operation. This is guaranteed by the latest
4820 * AMD CPUs and by newer Intel CPUs which never enter the C2 state (P4). In any other
4821 * case we have to choose the asynchronous timer mode.
4822 *
4823 * @param poffMin Pointer to the determined difference between different cores.
4824 * @return false if the time stamp counters appear to be synchron, true otherwise.
4825 */
4826bool VBOXCALL supdrvDetermineAsyncTsc(uint64_t *poffMin)
4827{
4828 /*
4829 * Just iterate all the cpus 8 times and make sure that the TSC is
4830 * ever increasing. We don't bother taking TSC rollover into account.
4831 */
4832 RTCPUSET CpuSet;
4833 int iLastCpu = RTCpuLastIndex(RTMpGetSet(&CpuSet));
4834 int iCpu;
4835 int cLoops = 8;
4836 bool fAsync = false;
4837 int rc = VINF_SUCCESS;
4838 uint64_t offMax = 0;
4839 uint64_t offMin = ~(uint64_t)0;
4840 uint64_t PrevTsc = ASMReadTSC();
4841
4842 while (cLoops-- > 0)
4843 {
4844 for (iCpu = 0; iCpu <= iLastCpu; iCpu++)
4845 {
4846 uint64_t CurTsc;
4847 rc = RTMpOnSpecific(RTMpCpuIdFromSetIndex(iCpu), supdrvDetermineAsyncTscWorker, &CurTsc, NULL);
4848 if (RT_SUCCESS(rc))
4849 {
4850 if (CurTsc <= PrevTsc)
4851 {
4852 fAsync = true;
4853 offMin = offMax = PrevTsc - CurTsc;
4854 dprintf(("supdrvDetermineAsyncTsc: iCpu=%d cLoops=%d CurTsc=%llx PrevTsc=%llx\n",
4855 iCpu, cLoops, CurTsc, PrevTsc));
4856 break;
4857 }
4858
4859 /* Gather statistics (except the first time). */
4860 if (iCpu != 0 || cLoops != 7)
4861 {
4862 uint64_t off = CurTsc - PrevTsc;
4863 if (off < offMin)
4864 offMin = off;
4865 if (off > offMax)
4866 offMax = off;
4867 dprintf2(("%d/%d: off=%llx\n", cLoops, iCpu, off));
4868 }
4869
4870 /* Next */
4871 PrevTsc = CurTsc;
4872 }
4873 else if (rc == VERR_NOT_SUPPORTED)
4874 break;
4875 else
4876 AssertMsg(rc == VERR_CPU_NOT_FOUND || rc == VERR_CPU_OFFLINE, ("%d\n", rc));
4877 }
4878
4879 /* broke out of the loop. */
4880 if (iCpu <= iLastCpu)
4881 break;
4882 }
4883
4884 *poffMin = offMin; /* Almost RTMpOnSpecific profiling. */
4885 dprintf(("supdrvDetermineAsyncTsc: returns %d; iLastCpu=%d rc=%d offMin=%llx offMax=%llx\n",
4886 fAsync, iLastCpu, rc, offMin, offMax));
4887#if !defined(RT_OS_SOLARIS) && !defined(RT_OS_OS2) && !defined(RT_OS_WINDOWS)
4888 OSDBGPRINT(("vboxdrv: fAsync=%d offMin=%#lx offMax=%#lx\n", fAsync, (long)offMin, (long)offMax));
4889#endif
4890 return fAsync;
4891}
4892
4893
4894/**
4895 * Invalidates the GIP data upon termination.
4896 *
4897 * @param pGip Pointer to the read-write kernel mapping of the GIP.
4898 */
4899void VBOXCALL supdrvGipTerm(PSUPGLOBALINFOPAGE pGip)
4900{
4901 unsigned i;
4902 pGip->u32Magic = 0;
4903 for (i = 0; i < RT_ELEMENTS(pGip->aCPUs); i++)
4904 {
4905 pGip->aCPUs[i].u64NanoTS = 0;
4906 pGip->aCPUs[i].u64TSC = 0;
4907 pGip->aCPUs[i].iTSCHistoryHead = 0;
4908 }
4909}
4910
4911
4912/**
4913 * Worker routine for supdrvGipUpdate and supdrvGipUpdatePerCpu that
4914 * updates all the per cpu data except the transaction id.
4915 *
4916 * @param pGip The GIP.
4917 * @param pGipCpu Pointer to the per cpu data.
4918 * @param u64NanoTS The current time stamp.
4919 * @param u64TSC The current TSC.
4920 */
4921static void supdrvGipDoUpdateCpu(PSUPGLOBALINFOPAGE pGip, PSUPGIPCPU pGipCpu, uint64_t u64NanoTS, uint64_t u64TSC)
4922{
4923 uint64_t u64TSCDelta;
4924 uint32_t u32UpdateIntervalTSC;
4925 uint32_t u32UpdateIntervalTSCSlack;
4926 unsigned iTSCHistoryHead;
4927 uint64_t u64CpuHz;
4928
4929 /* Delta between this and the previous update. */
4930 pGipCpu->u32UpdateIntervalNS = (uint32_t)(u64NanoTS - pGipCpu->u64NanoTS);
4931
4932 /*
4933 * Update the NanoTS.
4934 */
4935 ASMAtomicXchgU64(&pGipCpu->u64NanoTS, u64NanoTS);
4936
4937 /*
4938 * Calc TSC delta.
4939 */
4940 /** @todo validate the NanoTS delta, don't trust the OS to call us when it should... */
4941 u64TSCDelta = u64TSC - pGipCpu->u64TSC;
4942 ASMAtomicXchgU64(&pGipCpu->u64TSC, u64TSC);
4943
4944 if (u64TSCDelta >> 32)
4945 {
4946 u64TSCDelta = pGipCpu->u32UpdateIntervalTSC;
4947 pGipCpu->cErrors++;
4948 }
4949
4950 /*
4951 * TSC History.
4952 */
4953 Assert(RT_ELEMENTS(pGipCpu->au32TSCHistory) == 8);
4954
4955 iTSCHistoryHead = (pGipCpu->iTSCHistoryHead + 1) & 7;
4956 ASMAtomicXchgU32(&pGipCpu->iTSCHistoryHead, iTSCHistoryHead);
4957 ASMAtomicXchgU32(&pGipCpu->au32TSCHistory[iTSCHistoryHead], (uint32_t)u64TSCDelta);
4958
4959 /*
4960 * UpdateIntervalTSC = average of last 8,2,1 intervals depending on update HZ.
4961 */
4962 if (pGip->u32UpdateHz >= 1000)
4963 {
4964 uint32_t u32;
4965 u32 = pGipCpu->au32TSCHistory[0];
4966 u32 += pGipCpu->au32TSCHistory[1];
4967 u32 += pGipCpu->au32TSCHistory[2];
4968 u32 += pGipCpu->au32TSCHistory[3];
4969 u32 >>= 2;
4970 u32UpdateIntervalTSC = pGipCpu->au32TSCHistory[4];
4971 u32UpdateIntervalTSC += pGipCpu->au32TSCHistory[5];
4972 u32UpdateIntervalTSC += pGipCpu->au32TSCHistory[6];
4973 u32UpdateIntervalTSC += pGipCpu->au32TSCHistory[7];
4974 u32UpdateIntervalTSC >>= 2;
4975 u32UpdateIntervalTSC += u32;
4976 u32UpdateIntervalTSC >>= 1;
4977
4978 /* Value choosen for a 2GHz Athlon64 running linux 2.6.10/11, . */
4979 u32UpdateIntervalTSCSlack = u32UpdateIntervalTSC >> 14;
4980 }
4981 else if (pGip->u32UpdateHz >= 90)
4982 {
4983 u32UpdateIntervalTSC = (uint32_t)u64TSCDelta;
4984 u32UpdateIntervalTSC += pGipCpu->au32TSCHistory[(iTSCHistoryHead - 1) & 7];
4985 u32UpdateIntervalTSC >>= 1;
4986
4987 /* value choosen on a 2GHz thinkpad running windows */
4988 u32UpdateIntervalTSCSlack = u32UpdateIntervalTSC >> 7;
4989 }
4990 else
4991 {
4992 u32UpdateIntervalTSC = (uint32_t)u64TSCDelta;
4993
4994 /* This value hasn't be checked yet.. waiting for OS/2 and 33Hz timers.. :-) */
4995 u32UpdateIntervalTSCSlack = u32UpdateIntervalTSC >> 6;
4996 }
4997 ASMAtomicXchgU32(&pGipCpu->u32UpdateIntervalTSC, u32UpdateIntervalTSC + u32UpdateIntervalTSCSlack);
4998
4999 /*
5000 * CpuHz.
5001 */
5002 u64CpuHz = ASMMult2xU32RetU64(u32UpdateIntervalTSC, pGip->u32UpdateHz);
5003 ASMAtomicXchgU64(&pGipCpu->u64CpuHz, u64CpuHz);
5004}
5005
5006
5007/**
5008 * Updates the GIP.
5009 *
5010 * @param pGip Pointer to the GIP.
5011 * @param u64NanoTS The current nanosecond timesamp.
5012 * @param u64TSC The current TSC timesamp.
5013 */
5014void VBOXCALL supdrvGipUpdate(PSUPGLOBALINFOPAGE pGip, uint64_t u64NanoTS, uint64_t u64TSC)
5015{
5016 /*
5017 * Determin the relevant CPU data.
5018 */
5019 PSUPGIPCPU pGipCpu;
5020 if (pGip->u32Mode != SUPGIPMODE_ASYNC_TSC)
5021 pGipCpu = &pGip->aCPUs[0];
5022 else
5023 {
5024 unsigned iCpu = ASMGetApicId();
5025 if (RT_LIKELY(iCpu >= RT_ELEMENTS(pGip->aCPUs)))
5026 return;
5027 pGipCpu = &pGip->aCPUs[iCpu];
5028 }
5029
5030 /*
5031 * Start update transaction.
5032 */
5033 if (!(ASMAtomicIncU32(&pGipCpu->u32TransactionId) & 1))
5034 {
5035 /* this can happen on win32 if we're taking to long and there are more CPUs around. shouldn't happen though. */
5036 AssertMsgFailed(("Invalid transaction id, %#x, not odd!\n", pGipCpu->u32TransactionId));
5037 ASMAtomicIncU32(&pGipCpu->u32TransactionId);
5038 pGipCpu->cErrors++;
5039 return;
5040 }
5041
5042 /*
5043 * Recalc the update frequency every 0x800th time.
5044 */
5045 if (!(pGipCpu->u32TransactionId & (GIP_UPDATEHZ_RECALC_FREQ * 2 - 2)))
5046 {
5047 if (pGip->u64NanoTSLastUpdateHz)
5048 {
5049#ifdef RT_ARCH_AMD64 /** @todo fix 64-bit div here to work on x86 linux. */
5050 uint64_t u64Delta = u64NanoTS - pGip->u64NanoTSLastUpdateHz;
5051 uint32_t u32UpdateHz = (uint32_t)((UINT64_C(1000000000) * GIP_UPDATEHZ_RECALC_FREQ) / u64Delta);
5052 if (u32UpdateHz <= 2000 && u32UpdateHz >= 30)
5053 {
5054 ASMAtomicXchgU32(&pGip->u32UpdateHz, u32UpdateHz);
5055 ASMAtomicXchgU32(&pGip->u32UpdateIntervalNS, 1000000000 / u32UpdateHz);
5056 }
5057#endif
5058 }
5059 ASMAtomicXchgU64(&pGip->u64NanoTSLastUpdateHz, u64NanoTS);
5060 }
5061
5062 /*
5063 * Update the data.
5064 */
5065 supdrvGipDoUpdateCpu(pGip, pGipCpu, u64NanoTS, u64TSC);
5066
5067 /*
5068 * Complete transaction.
5069 */
5070 ASMAtomicIncU32(&pGipCpu->u32TransactionId);
5071}
5072
5073
5074/**
5075 * Updates the per cpu GIP data for the calling cpu.
5076 *
5077 * @param pGip Pointer to the GIP.
5078 * @param u64NanoTS The current nanosecond timesamp.
5079 * @param u64TSC The current TSC timesamp.
5080 * @param iCpu The CPU index.
5081 */
5082void VBOXCALL supdrvGipUpdatePerCpu(PSUPGLOBALINFOPAGE pGip, uint64_t u64NanoTS, uint64_t u64TSC, unsigned iCpu)
5083{
5084 PSUPGIPCPU pGipCpu;
5085
5086 if (RT_LIKELY(iCpu < RT_ELEMENTS(pGip->aCPUs)))
5087 {
5088 pGipCpu = &pGip->aCPUs[iCpu];
5089
5090 /*
5091 * Start update transaction.
5092 */
5093 if (!(ASMAtomicIncU32(&pGipCpu->u32TransactionId) & 1))
5094 {
5095 AssertMsgFailed(("Invalid transaction id, %#x, not odd!\n", pGipCpu->u32TransactionId));
5096 ASMAtomicIncU32(&pGipCpu->u32TransactionId);
5097 pGipCpu->cErrors++;
5098 return;
5099 }
5100
5101 /*
5102 * Update the data.
5103 */
5104 supdrvGipDoUpdateCpu(pGip, pGipCpu, u64NanoTS, u64TSC);
5105
5106 /*
5107 * Complete transaction.
5108 */
5109 ASMAtomicIncU32(&pGipCpu->u32TransactionId);
5110 }
5111}
5112
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette