VirtualBox

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

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

IPRT: Kernel thread-context hooks, linux implementation. Extended tstR0ThreadPreemption testcase to test the thread-context hooks.

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