VirtualBox

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

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

Init the dtrace bits...

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