VirtualBox

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

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

Solaris/NetFltBow: support for VNIC templates and VLANs.

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