VirtualBox

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

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

SUPDrv: Doxygen for r90051.

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