VirtualBox

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

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

HostDrivers: Fix SUPR0QueryVTCaps() to be preemption safe.

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