VirtualBox

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

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

Backed out r65776

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