VirtualBox

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

Last change on this file since 25536 was 25534, checked in by vboxsync, 15 years ago

SUPDrv/SUPR0.def: Missing exports.

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