VirtualBox

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

Last change on this file since 25270 was 25262, checked in by vboxsync, 15 years ago

SUPDrv: Works on vista...

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