VirtualBox

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

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

SUPDrv,SUPLib: Expose the high resolution event semaphore APIs.

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

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