VirtualBox

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

Last change on this file since 38183 was 38075, checked in by vboxsync, 13 years ago

SUPDrv: Check pSession in the IOCtl routine and clean up a little earlier on windows.

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

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