VirtualBox

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

Last change on this file since 31157 was 31157, checked in by vboxsync, 14 years ago

iprt,++: Tag allocation in all builds with a string, defaulting to FILE.

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