VirtualBox

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

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

log rotation review and adjustments: Don't delete any excess files if log roation is disabled - we don't know what these files might be. Moved RTLOGGERFILE into log.c. Keep RTLogCreate simple, anyone needing rotation can use RTLogCreateEx[V]. Made RTLogGetDestinations produce the log rotation bits.

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