VirtualBox

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

Last change on this file since 18499 was 18480, checked in by vboxsync, 15 years ago

SUPDrv.c: size_t warning.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use