VirtualBox

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

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

SUPDrv,VMM: Tracepoints in raw-mode.

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

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