VirtualBox

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

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

build fix

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