VirtualBox

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

Last change on this file since 43379 was 43379, checked in by vboxsync, 12 years ago

SUPDrv,VMM: Prepared for SUPR0EnableVTx on darwin.

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

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