VirtualBox

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

最後變更 在這個檔案從28800是 28800,由 vboxsync 提交於 15 年 前

Automated rebranding to Oracle copyright/license strings via filemuncher

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette