VirtualBox

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

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

HostDrivers/Support: Introduce reference counting for the session object to keep it alive while there is someone accessing it, fixes R0 assertion on OS X when killing the VM process

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