VirtualBox

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

Last change on this file since 47363 was 47352, checked in by vboxsync, 11 years ago

Runtime/r0drv: Reference counting for kernel thread-context hooks.

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