VirtualBox

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

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

SUPDrv: Change SUPR0Printf to debug Log statement.

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