VirtualBox

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

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

space

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