VirtualBox

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

Last change on this file since 18195 was 18191, checked in by vboxsync, 16 years ago

SUPLoggerCtl: program for controlling the ring-0 logging.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 186.9 KB
Line 
1/* $Revision: 18191 $ */
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 uint32_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 pReq->u.In.u32MagicCookie, 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, 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 LogFlow(("supdrvIOCtl_LdrOpen: szName=%s cbImage=%d\n", pReq->u.In.szName, pReq->u.In.cbImage));
3309
3310 /*
3311 * Check if we got an instance of the image already.
3312 */
3313 RTSemFastMutexRequest(pDevExt->mtxLdr);
3314 for (pImage = pDevExt->pLdrImages; pImage; pImage = pImage->pNext)
3315 {
3316 if (!strcmp(pImage->szName, pReq->u.In.szName))
3317 {
3318 pImage->cUsage++;
3319 pReq->u.Out.pvImageBase = pImage->pvImage;
3320 pReq->u.Out.fNeedsLoading = pImage->uState == SUP_IOCTL_LDR_OPEN;
3321 supdrvLdrAddUsage(pSession, pImage);
3322 RTSemFastMutexRelease(pDevExt->mtxLdr);
3323 return VINF_SUCCESS;
3324 }
3325 }
3326 /* (not found - add it!) */
3327
3328 /*
3329 * Allocate memory.
3330 */
3331 cb = pReq->u.In.cbImage + sizeof(SUPDRVLDRIMAGE) + 31;
3332 pv = RTMemExecAlloc(cb);
3333 if (!pv)
3334 {
3335 RTSemFastMutexRelease(pDevExt->mtxLdr);
3336 Log(("supdrvIOCtl_LdrOpen: RTMemExecAlloc(%u) failed\n", cb));
3337 return VERR_NO_MEMORY;
3338 }
3339
3340 /*
3341 * Setup and link in the LDR stuff.
3342 */
3343 pImage = (PSUPDRVLDRIMAGE)pv;
3344 pImage->pvImage = RT_ALIGN_P(pImage + 1, 32);
3345 pImage->cbImage = pReq->u.In.cbImage;
3346 pImage->pfnModuleInit = NULL;
3347 pImage->pfnModuleTerm = NULL;
3348 pImage->pfnServiceReqHandler = NULL;
3349 pImage->uState = SUP_IOCTL_LDR_OPEN;
3350 pImage->cUsage = 1;
3351 strcpy(pImage->szName, pReq->u.In.szName);
3352
3353 pImage->pNext = pDevExt->pLdrImages;
3354 pDevExt->pLdrImages = pImage;
3355
3356 supdrvLdrAddUsage(pSession, pImage);
3357
3358 pReq->u.Out.pvImageBase = pImage->pvImage;
3359 pReq->u.Out.fNeedsLoading = true;
3360 RTSemFastMutexRelease(pDevExt->mtxLdr);
3361
3362#if defined(RT_OS_WINDOWS) && defined(DEBUG)
3363 SUPR0Printf("VBoxDrv: windbg> .reload /f %s=%#p\n", pImage->szName, pImage->pvImage);
3364#endif
3365 return VINF_SUCCESS;
3366}
3367
3368
3369/**
3370 * Loads the image bits.
3371 *
3372 * This is the 2nd step of the loading.
3373 *
3374 * @returns IPRT status code.
3375 * @param pDevExt Device globals.
3376 * @param pSession Session data.
3377 * @param pReq The request.
3378 */
3379static int supdrvIOCtl_LdrLoad(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDRLOAD pReq)
3380{
3381 PSUPDRVLDRUSAGE pUsage;
3382 PSUPDRVLDRIMAGE pImage;
3383 int rc;
3384 LogFlow(("supdrvIOCtl_LdrLoad: pvImageBase=%p cbImage=%d\n", pReq->u.In.pvImageBase, pReq->u.In.cbImage));
3385
3386 /*
3387 * Find the ldr image.
3388 */
3389 RTSemFastMutexRequest(pDevExt->mtxLdr);
3390 pUsage = pSession->pLdrUsage;
3391 while (pUsage && pUsage->pImage->pvImage != pReq->u.In.pvImageBase)
3392 pUsage = pUsage->pNext;
3393 if (!pUsage)
3394 {
3395 RTSemFastMutexRelease(pDevExt->mtxLdr);
3396 Log(("SUP_IOCTL_LDR_LOAD: couldn't find image!\n"));
3397 return VERR_INVALID_HANDLE;
3398 }
3399 pImage = pUsage->pImage;
3400 if (pImage->cbImage != pReq->u.In.cbImage)
3401 {
3402 RTSemFastMutexRelease(pDevExt->mtxLdr);
3403 Log(("SUP_IOCTL_LDR_LOAD: image size mismatch!! %d(prep) != %d(load)\n", pImage->cbImage, pReq->u.In.cbImage));
3404 return VERR_INVALID_HANDLE;
3405 }
3406 if (pImage->uState != SUP_IOCTL_LDR_OPEN)
3407 {
3408 unsigned uState = pImage->uState;
3409 RTSemFastMutexRelease(pDevExt->mtxLdr);
3410 if (uState != SUP_IOCTL_LDR_LOAD)
3411 AssertMsgFailed(("SUP_IOCTL_LDR_LOAD: invalid image state %d (%#x)!\n", uState, uState));
3412 return SUPDRV_ERR_ALREADY_LOADED;
3413 }
3414 switch (pReq->u.In.eEPType)
3415 {
3416 case SUPLDRLOADEP_NOTHING:
3417 break;
3418
3419 case SUPLDRLOADEP_VMMR0:
3420 if ( !pReq->u.In.EP.VMMR0.pvVMMR0
3421 || !pReq->u.In.EP.VMMR0.pvVMMR0EntryInt
3422 || !pReq->u.In.EP.VMMR0.pvVMMR0EntryFast
3423 || !pReq->u.In.EP.VMMR0.pvVMMR0EntryEx)
3424 {
3425 RTSemFastMutexRelease(pDevExt->mtxLdr);
3426 Log(("NULL pointer: pvVMMR0=%p pvVMMR0EntryInt=%p pvVMMR0EntryFast=%p pvVMMR0EntryEx=%p!\n",
3427 pReq->u.In.EP.VMMR0.pvVMMR0, pReq->u.In.EP.VMMR0.pvVMMR0EntryInt,
3428 pReq->u.In.EP.VMMR0.pvVMMR0EntryFast, pReq->u.In.EP.VMMR0.pvVMMR0EntryEx));
3429 return VERR_INVALID_PARAMETER;
3430 }
3431 /** @todo validate pReq->u.In.EP.VMMR0.pvVMMR0 against pvImage! */
3432 if ( (uintptr_t)pReq->u.In.EP.VMMR0.pvVMMR0EntryInt - (uintptr_t)pImage->pvImage >= pReq->u.In.cbImage
3433 || (uintptr_t)pReq->u.In.EP.VMMR0.pvVMMR0EntryFast - (uintptr_t)pImage->pvImage >= pReq->u.In.cbImage
3434 || (uintptr_t)pReq->u.In.EP.VMMR0.pvVMMR0EntryEx - (uintptr_t)pImage->pvImage >= pReq->u.In.cbImage)
3435 {
3436 RTSemFastMutexRelease(pDevExt->mtxLdr);
3437 Log(("Out of range (%p LB %#x): pvVMMR0EntryInt=%p, pvVMMR0EntryFast=%p or pvVMMR0EntryEx=%p is NULL!\n",
3438 pImage->pvImage, pReq->u.In.cbImage, pReq->u.In.EP.VMMR0.pvVMMR0EntryInt,
3439 pReq->u.In.EP.VMMR0.pvVMMR0EntryFast, pReq->u.In.EP.VMMR0.pvVMMR0EntryEx));
3440 return VERR_INVALID_PARAMETER;
3441 }
3442 break;
3443
3444 case SUPLDRLOADEP_SERVICE:
3445 if (!pReq->u.In.EP.Service.pfnServiceReq)
3446 {
3447 RTSemFastMutexRelease(pDevExt->mtxLdr);
3448 Log(("NULL pointer: pfnServiceReq=%p!\n", pReq->u.In.EP.Service.pfnServiceReq));
3449 return VERR_INVALID_PARAMETER;
3450 }
3451 if ((uintptr_t)pReq->u.In.EP.Service.pfnServiceReq - (uintptr_t)pImage->pvImage >= pReq->u.In.cbImage)
3452 {
3453 RTSemFastMutexRelease(pDevExt->mtxLdr);
3454 Log(("Out of range (%p LB %#x): pfnServiceReq=%p, pvVMMR0EntryFast=%p or pvVMMR0EntryEx=%p is NULL!\n",
3455 pImage->pvImage, pReq->u.In.cbImage, pReq->u.In.EP.Service.pfnServiceReq));
3456 return VERR_INVALID_PARAMETER;
3457 }
3458 if ( pReq->u.In.EP.Service.apvReserved[0] != NIL_RTR0PTR
3459 || pReq->u.In.EP.Service.apvReserved[1] != NIL_RTR0PTR
3460 || pReq->u.In.EP.Service.apvReserved[2] != NIL_RTR0PTR)
3461 {
3462 RTSemFastMutexRelease(pDevExt->mtxLdr);
3463 Log(("Out of range (%p LB %#x): apvReserved={%p,%p,%p} MBZ!\n",
3464 pImage->pvImage, pReq->u.In.cbImage,
3465 pReq->u.In.EP.Service.apvReserved[0],
3466 pReq->u.In.EP.Service.apvReserved[1],
3467 pReq->u.In.EP.Service.apvReserved[2]));
3468 return VERR_INVALID_PARAMETER;
3469 }
3470 break;
3471
3472 default:
3473 RTSemFastMutexRelease(pDevExt->mtxLdr);
3474 Log(("Invalid eEPType=%d\n", pReq->u.In.eEPType));
3475 return VERR_INVALID_PARAMETER;
3476 }
3477 if ( pReq->u.In.pfnModuleInit
3478 && (uintptr_t)pReq->u.In.pfnModuleInit - (uintptr_t)pImage->pvImage >= pReq->u.In.cbImage)
3479 {
3480 RTSemFastMutexRelease(pDevExt->mtxLdr);
3481 Log(("SUP_IOCTL_LDR_LOAD: pfnModuleInit=%p is outside the image (%p %d bytes)\n",
3482 pReq->u.In.pfnModuleInit, pImage->pvImage, pReq->u.In.cbImage));
3483 return VERR_INVALID_PARAMETER;
3484 }
3485 if ( pReq->u.In.pfnModuleTerm
3486 && (uintptr_t)pReq->u.In.pfnModuleTerm - (uintptr_t)pImage->pvImage >= pReq->u.In.cbImage)
3487 {
3488 RTSemFastMutexRelease(pDevExt->mtxLdr);
3489 Log(("SUP_IOCTL_LDR_LOAD: pfnModuleTerm=%p is outside the image (%p %d bytes)\n",
3490 pReq->u.In.pfnModuleTerm, pImage->pvImage, pReq->u.In.cbImage));
3491 return VERR_INVALID_PARAMETER;
3492 }
3493
3494 /*
3495 * Copy the memory.
3496 */
3497 /* no need to do try/except as this is a buffered request. */
3498 memcpy(pImage->pvImage, &pReq->u.In.achImage[0], pImage->cbImage);
3499 pImage->uState = SUP_IOCTL_LDR_LOAD;
3500 pImage->pfnModuleInit = pReq->u.In.pfnModuleInit;
3501 pImage->pfnModuleTerm = pReq->u.In.pfnModuleTerm;
3502 pImage->offSymbols = pReq->u.In.offSymbols;
3503 pImage->cSymbols = pReq->u.In.cSymbols;
3504 pImage->offStrTab = pReq->u.In.offStrTab;
3505 pImage->cbStrTab = pReq->u.In.cbStrTab;
3506
3507 /*
3508 * Update any entry points.
3509 */
3510 switch (pReq->u.In.eEPType)
3511 {
3512 default:
3513 case SUPLDRLOADEP_NOTHING:
3514 rc = VINF_SUCCESS;
3515 break;
3516 case SUPLDRLOADEP_VMMR0:
3517 rc = supdrvLdrSetVMMR0EPs(pDevExt, pReq->u.In.EP.VMMR0.pvVMMR0, pReq->u.In.EP.VMMR0.pvVMMR0EntryInt,
3518 pReq->u.In.EP.VMMR0.pvVMMR0EntryFast, pReq->u.In.EP.VMMR0.pvVMMR0EntryEx);
3519 break;
3520 case SUPLDRLOADEP_SERVICE:
3521 pImage->pfnServiceReqHandler = pReq->u.In.EP.Service.pfnServiceReq;
3522 rc = VINF_SUCCESS;
3523 break;
3524 }
3525
3526 /*
3527 * On success call the module initialization.
3528 */
3529 LogFlow(("supdrvIOCtl_LdrLoad: pfnModuleInit=%p\n", pImage->pfnModuleInit));
3530 if (RT_SUCCESS(rc) && pImage->pfnModuleInit)
3531 {
3532 Log(("supdrvIOCtl_LdrLoad: calling pfnModuleInit=%p\n", pImage->pfnModuleInit));
3533#ifdef RT_WITH_W64_UNWIND_HACK
3534 rc = supdrvNtWrapModuleInit((PFNRT)pImage->pfnModuleInit);
3535#else
3536 rc = pImage->pfnModuleInit();
3537#endif
3538 if (rc && pDevExt->pvVMMR0 == pImage->pvImage)
3539 supdrvLdrUnsetVMMR0EPs(pDevExt);
3540 }
3541
3542 if (rc)
3543 pImage->uState = SUP_IOCTL_LDR_OPEN;
3544
3545 RTSemFastMutexRelease(pDevExt->mtxLdr);
3546 return rc;
3547}
3548
3549
3550/**
3551 * Frees a previously loaded (prep'ed) image.
3552 *
3553 * @returns IPRT status code.
3554 * @param pDevExt Device globals.
3555 * @param pSession Session data.
3556 * @param pReq The request.
3557 */
3558static int supdrvIOCtl_LdrFree(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDRFREE pReq)
3559{
3560 int rc;
3561 PSUPDRVLDRUSAGE pUsagePrev;
3562 PSUPDRVLDRUSAGE pUsage;
3563 PSUPDRVLDRIMAGE pImage;
3564 LogFlow(("supdrvIOCtl_LdrFree: pvImageBase=%p\n", pReq->u.In.pvImageBase));
3565
3566 /*
3567 * Find the ldr image.
3568 */
3569 RTSemFastMutexRequest(pDevExt->mtxLdr);
3570 pUsagePrev = NULL;
3571 pUsage = pSession->pLdrUsage;
3572 while (pUsage && pUsage->pImage->pvImage != pReq->u.In.pvImageBase)
3573 {
3574 pUsagePrev = pUsage;
3575 pUsage = pUsage->pNext;
3576 }
3577 if (!pUsage)
3578 {
3579 RTSemFastMutexRelease(pDevExt->mtxLdr);
3580 Log(("SUP_IOCTL_LDR_FREE: couldn't find image!\n"));
3581 return VERR_INVALID_HANDLE;
3582 }
3583
3584 /*
3585 * Check if we can remove anything.
3586 */
3587 rc = VINF_SUCCESS;
3588 pImage = pUsage->pImage;
3589 if (pImage->cUsage <= 1 || pUsage->cUsage <= 1)
3590 {
3591 /*
3592 * Check if there are any objects with destructors in the image, if
3593 * so leave it for the session cleanup routine so we get a chance to
3594 * clean things up in the right order and not leave them all dangling.
3595 */
3596 RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
3597 RTSpinlockAcquire(pDevExt->Spinlock, &SpinlockTmp);
3598 if (pImage->cUsage <= 1)
3599 {
3600 PSUPDRVOBJ pObj;
3601 for (pObj = pDevExt->pObjs; pObj; pObj = pObj->pNext)
3602 if (RT_UNLIKELY((uintptr_t)pObj->pfnDestructor - (uintptr_t)pImage->pvImage < pImage->cbImage))
3603 {
3604 rc = VERR_DANGLING_OBJECTS;
3605 break;
3606 }
3607 }
3608 else
3609 {
3610 PSUPDRVUSAGE pGenUsage;
3611 for (pGenUsage = pSession->pUsage; pGenUsage; pGenUsage = pGenUsage->pNext)
3612 if (RT_UNLIKELY((uintptr_t)pGenUsage->pObj->pfnDestructor - (uintptr_t)pImage->pvImage < pImage->cbImage))
3613 {
3614 rc = VERR_DANGLING_OBJECTS;
3615 break;
3616 }
3617 }
3618 RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
3619 if (rc == VINF_SUCCESS)
3620 {
3621 /* unlink it */
3622 if (pUsagePrev)
3623 pUsagePrev->pNext = pUsage->pNext;
3624 else
3625 pSession->pLdrUsage = pUsage->pNext;
3626
3627 /* free it */
3628 pUsage->pImage = NULL;
3629 pUsage->pNext = NULL;
3630 RTMemFree(pUsage);
3631
3632 /*
3633 * Derefrence the image.
3634 */
3635 if (pImage->cUsage <= 1)
3636 supdrvLdrFree(pDevExt, pImage);
3637 else
3638 pImage->cUsage--;
3639 }
3640 else
3641 {
3642 Log(("supdrvIOCtl_LdrFree: Dangling objects in %p/%s!\n", pImage->pvImage, pImage->szName));
3643 rc = VINF_SUCCESS; /** @todo BRANCH-2.1: remove this after branching. */
3644 }
3645 }
3646 else
3647 {
3648 /*
3649 * Dereference both image and usage.
3650 */
3651 pImage->cUsage--;
3652 pUsage->cUsage--;
3653 }
3654
3655 RTSemFastMutexRelease(pDevExt->mtxLdr);
3656 return rc;
3657}
3658
3659
3660/**
3661 * Gets the address of a symbol in an open image.
3662 *
3663 * @returns 0 on success.
3664 * @returns SUPDRV_ERR_* on failure.
3665 * @param pDevExt Device globals.
3666 * @param pSession Session data.
3667 * @param pReq The request buffer.
3668 */
3669static int supdrvIOCtl_LdrGetSymbol(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLDRGETSYMBOL pReq)
3670{
3671 PSUPDRVLDRIMAGE pImage;
3672 PSUPDRVLDRUSAGE pUsage;
3673 uint32_t i;
3674 PSUPLDRSYM paSyms;
3675 const char *pchStrings;
3676 const size_t cbSymbol = strlen(pReq->u.In.szSymbol) + 1;
3677 void *pvSymbol = NULL;
3678 int rc = VERR_GENERAL_FAILURE;
3679 Log3(("supdrvIOCtl_LdrGetSymbol: pvImageBase=%p szSymbol=\"%s\"\n", pReq->u.In.pvImageBase, pReq->u.In.szSymbol));
3680
3681 /*
3682 * Find the ldr image.
3683 */
3684 RTSemFastMutexRequest(pDevExt->mtxLdr);
3685 pUsage = pSession->pLdrUsage;
3686 while (pUsage && pUsage->pImage->pvImage != pReq->u.In.pvImageBase)
3687 pUsage = pUsage->pNext;
3688 if (!pUsage)
3689 {
3690 RTSemFastMutexRelease(pDevExt->mtxLdr);
3691 Log(("SUP_IOCTL_LDR_GET_SYMBOL: couldn't find image!\n"));
3692 return VERR_INVALID_HANDLE;
3693 }
3694 pImage = pUsage->pImage;
3695 if (pImage->uState != SUP_IOCTL_LDR_LOAD)
3696 {
3697 unsigned uState = pImage->uState;
3698 RTSemFastMutexRelease(pDevExt->mtxLdr);
3699 Log(("SUP_IOCTL_LDR_GET_SYMBOL: invalid image state %d (%#x)!\n", uState, uState)); NOREF(uState);
3700 return VERR_ALREADY_LOADED;
3701 }
3702
3703 /*
3704 * Search the symbol strings.
3705 */
3706 pchStrings = (const char *)((uint8_t *)pImage->pvImage + pImage->offStrTab);
3707 paSyms = (PSUPLDRSYM)((uint8_t *)pImage->pvImage + pImage->offSymbols);
3708 for (i = 0; i < pImage->cSymbols; i++)
3709 {
3710 if ( paSyms[i].offSymbol < pImage->cbImage /* paranoia */
3711 && paSyms[i].offName + cbSymbol <= pImage->cbStrTab
3712 && !memcmp(pchStrings + paSyms[i].offName, pReq->u.In.szSymbol, cbSymbol))
3713 {
3714 pvSymbol = (uint8_t *)pImage->pvImage + paSyms[i].offSymbol;
3715 rc = VINF_SUCCESS;
3716 break;
3717 }
3718 }
3719 RTSemFastMutexRelease(pDevExt->mtxLdr);
3720 pReq->u.Out.pvSymbol = pvSymbol;
3721 return rc;
3722}
3723
3724
3725/**
3726 * Gets the address of a symbol in an open image or the support driver.
3727 *
3728 * @returns VINF_SUCCESS on success.
3729 * @returns
3730 * @param pDevExt Device globals.
3731 * @param pSession Session data.
3732 * @param pReq The request buffer.
3733 */
3734static int supdrvIDC_LdrGetSymbol(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVIDCREQGETSYM pReq)
3735{
3736 int rc = VINF_SUCCESS;
3737 const char *pszSymbol = pReq->u.In.pszSymbol;
3738 const char *pszModule = pReq->u.In.pszModule;
3739 size_t cbSymbol;
3740 char const *pszEnd;
3741 uint32_t i;
3742
3743 /*
3744 * Input validation.
3745 */
3746 AssertPtrReturn(pszSymbol, VERR_INVALID_POINTER);
3747 pszEnd = (char *)memchr(pszSymbol, '\0', 512);
3748 AssertReturn(pszEnd, VERR_INVALID_PARAMETER);
3749 cbSymbol = pszEnd - pszSymbol + 1;
3750
3751 if (pszModule)
3752 {
3753 AssertPtrReturn(pszModule, VERR_INVALID_POINTER);
3754 pszEnd = (char *)memchr(pszModule, '\0', 64);
3755 AssertReturn(pszEnd, VERR_INVALID_PARAMETER);
3756 }
3757 Log3(("supdrvIDC_LdrGetSymbol: pszModule=%p:{%s} pszSymbol=%p:{%s}\n", pszModule, pszModule, pszSymbol, pszSymbol));
3758
3759
3760 if ( !pszModule
3761 || !strcmp(pszModule, "SupDrv"))
3762 {
3763 /*
3764 * Search the support driver export table.
3765 */
3766 for (i = 0; i < RT_ELEMENTS(g_aFunctions); i++)
3767 if (!strcmp(g_aFunctions[i].szName, pszSymbol))
3768 {
3769 pReq->u.Out.pfnSymbol = g_aFunctions[i].pfn;
3770 break;
3771 }
3772 }
3773 else
3774 {
3775 /*
3776 * Find the loader image.
3777 */
3778 PSUPDRVLDRIMAGE pImage;
3779
3780 RTSemFastMutexRequest(pDevExt->mtxLdr);
3781
3782 for (pImage = pDevExt->pLdrImages; pImage; pImage = pImage->pNext)
3783 if (!strcmp(pImage->szName, pszModule))
3784 break;
3785 if (pImage && pImage->uState == SUP_IOCTL_LDR_LOAD)
3786 {
3787 /*
3788 * Search the symbol strings.
3789 */
3790 const char *pchStrings = (const char *)((uint8_t *)pImage->pvImage + pImage->offStrTab);
3791 PCSUPLDRSYM paSyms = (PCSUPLDRSYM)((uint8_t *)pImage->pvImage + pImage->offSymbols);
3792 for (i = 0; i < pImage->cSymbols; i++)
3793 {
3794 if ( paSyms[i].offSymbol < pImage->cbImage /* paranoia */
3795 && paSyms[i].offName + cbSymbol <= pImage->cbStrTab
3796 && !memcmp(pchStrings + paSyms[i].offName, pszSymbol, cbSymbol))
3797 {
3798 /*
3799 * Found it! Calc the symbol address and add a reference to the module.
3800 */
3801 pReq->u.Out.pfnSymbol = (PFNRT)((uint8_t *)pImage->pvImage + paSyms[i].offSymbol);
3802 rc = supdrvLdrAddUsage(pSession, pImage);
3803 break;
3804 }
3805 }
3806 }
3807 else
3808 rc = pImage ? VERR_WRONG_ORDER : VERR_MODULE_NOT_FOUND;
3809
3810 RTSemFastMutexRelease(pDevExt->mtxLdr);
3811 }
3812 return rc;
3813}
3814
3815
3816/**
3817 * Updates the VMMR0 entry point pointers.
3818 *
3819 * @returns IPRT status code.
3820 * @param pDevExt Device globals.
3821 * @param pSession Session data.
3822 * @param pVMMR0 VMMR0 image handle.
3823 * @param pvVMMR0EntryInt VMMR0EntryInt address.
3824 * @param pvVMMR0EntryFast VMMR0EntryFast address.
3825 * @param pvVMMR0EntryEx VMMR0EntryEx address.
3826 * @remark Caller must own the loader mutex.
3827 */
3828static int supdrvLdrSetVMMR0EPs(PSUPDRVDEVEXT pDevExt, void *pvVMMR0, void *pvVMMR0EntryInt, void *pvVMMR0EntryFast, void *pvVMMR0EntryEx)
3829{
3830 int rc = VINF_SUCCESS;
3831 LogFlow(("supdrvLdrSetR0EP pvVMMR0=%p pvVMMR0EntryInt=%p\n", pvVMMR0, pvVMMR0EntryInt));
3832
3833
3834 /*
3835 * Check if not yet set.
3836 */
3837 if (!pDevExt->pvVMMR0)
3838 {
3839 pDevExt->pvVMMR0 = pvVMMR0;
3840 pDevExt->pfnVMMR0EntryInt = pvVMMR0EntryInt;
3841 pDevExt->pfnVMMR0EntryFast = pvVMMR0EntryFast;
3842 pDevExt->pfnVMMR0EntryEx = pvVMMR0EntryEx;
3843 }
3844 else
3845 {
3846 /*
3847 * Return failure or success depending on whether the values match or not.
3848 */
3849 if ( pDevExt->pvVMMR0 != pvVMMR0
3850 || (void *)pDevExt->pfnVMMR0EntryInt != pvVMMR0EntryInt
3851 || (void *)pDevExt->pfnVMMR0EntryFast != pvVMMR0EntryFast
3852 || (void *)pDevExt->pfnVMMR0EntryEx != pvVMMR0EntryEx)
3853 {
3854 AssertMsgFailed(("SUP_IOCTL_LDR_SETR0EP: Already set pointing to a different module!\n"));
3855 rc = VERR_INVALID_PARAMETER;
3856 }
3857 }
3858 return rc;
3859}
3860
3861
3862/**
3863 * Unsets the VMMR0 entry point installed by supdrvLdrSetR0EP.
3864 *
3865 * @param pDevExt Device globals.
3866 */
3867static void supdrvLdrUnsetVMMR0EPs(PSUPDRVDEVEXT pDevExt)
3868{
3869 pDevExt->pvVMMR0 = NULL;
3870 pDevExt->pfnVMMR0EntryInt = NULL;
3871 pDevExt->pfnVMMR0EntryFast = NULL;
3872 pDevExt->pfnVMMR0EntryEx = NULL;
3873}
3874
3875
3876/**
3877 * Adds a usage reference in the specified session of an image.
3878 *
3879 * Called while owning the loader semaphore.
3880 *
3881 * @returns VINF_SUCCESS on success and VERR_NO_MEMORY on failure.
3882 * @param pSession Session in question.
3883 * @param pImage Image which the session is using.
3884 */
3885static int supdrvLdrAddUsage(PSUPDRVSESSION pSession, PSUPDRVLDRIMAGE pImage)
3886{
3887 PSUPDRVLDRUSAGE pUsage;
3888 LogFlow(("supdrvLdrAddUsage: pImage=%p\n", pImage));
3889
3890 /*
3891 * Referenced it already?
3892 */
3893 pUsage = pSession->pLdrUsage;
3894 while (pUsage)
3895 {
3896 if (pUsage->pImage == pImage)
3897 {
3898 pUsage->cUsage++;
3899 return VINF_SUCCESS;
3900 }
3901 pUsage = pUsage->pNext;
3902 }
3903
3904 /*
3905 * Allocate new usage record.
3906 */
3907 pUsage = (PSUPDRVLDRUSAGE)RTMemAlloc(sizeof(*pUsage));
3908 AssertReturn(pUsage, VERR_NO_MEMORY);
3909 pUsage->cUsage = 1;
3910 pUsage->pImage = pImage;
3911 pUsage->pNext = pSession->pLdrUsage;
3912 pSession->pLdrUsage = pUsage;
3913 return VINF_SUCCESS;
3914}
3915
3916
3917/**
3918 * Frees a load image.
3919 *
3920 * @param pDevExt Pointer to device extension.
3921 * @param pImage Pointer to the image we're gonna free.
3922 * This image must exit!
3923 * @remark The caller MUST own SUPDRVDEVEXT::mtxLdr!
3924 */
3925static void supdrvLdrFree(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
3926{
3927 PSUPDRVLDRIMAGE pImagePrev;
3928 LogFlow(("supdrvLdrFree: pImage=%p\n", pImage));
3929
3930 /* find it - arg. should've used doubly linked list. */
3931 Assert(pDevExt->pLdrImages);
3932 pImagePrev = NULL;
3933 if (pDevExt->pLdrImages != pImage)
3934 {
3935 pImagePrev = pDevExt->pLdrImages;
3936 while (pImagePrev->pNext != pImage)
3937 pImagePrev = pImagePrev->pNext;
3938 Assert(pImagePrev->pNext == pImage);
3939 }
3940
3941 /* unlink */
3942 if (pImagePrev)
3943 pImagePrev->pNext = pImage->pNext;
3944 else
3945 pDevExt->pLdrImages = pImage->pNext;
3946
3947 /* check if this is VMMR0.r0 unset its entry point pointers. */
3948 if (pDevExt->pvVMMR0 == pImage->pvImage)
3949 supdrvLdrUnsetVMMR0EPs(pDevExt);
3950
3951 /* check for objects with destructors in this image. (Shouldn't happen.) */
3952 if (pDevExt->pObjs)
3953 {
3954 unsigned cObjs = 0;
3955 PSUPDRVOBJ pObj;
3956 RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
3957 RTSpinlockAcquire(pDevExt->Spinlock, &SpinlockTmp);
3958 for (pObj = pDevExt->pObjs; pObj; pObj = pObj->pNext)
3959 if (RT_UNLIKELY((uintptr_t)pObj->pfnDestructor - (uintptr_t)pImage->pvImage < pImage->cbImage))
3960 {
3961 pObj->pfnDestructor = NULL;
3962 cObjs++;
3963 }
3964 RTSpinlockRelease(pDevExt->Spinlock, &SpinlockTmp);
3965 if (cObjs)
3966 OSDBGPRINT(("supdrvLdrFree: Image '%s' has %d dangling objects!\n", pImage->szName, cObjs));
3967 }
3968
3969 /* call termination function if fully loaded. */
3970 if ( pImage->pfnModuleTerm
3971 && pImage->uState == SUP_IOCTL_LDR_LOAD)
3972 {
3973 LogFlow(("supdrvIOCtl_LdrLoad: calling pfnModuleTerm=%p\n", pImage->pfnModuleTerm));
3974#ifdef RT_WITH_W64_UNWIND_HACK
3975 supdrvNtWrapModuleTerm(pImage->pfnModuleTerm);
3976#else
3977 pImage->pfnModuleTerm();
3978#endif
3979 }
3980
3981 /* free the image */
3982 pImage->cUsage = 0;
3983 pImage->pNext = 0;
3984 pImage->uState = SUP_IOCTL_LDR_FREE;
3985 RTMemExecFree(pImage);
3986}
3987
3988
3989/**
3990 * Implements the service call request.
3991 *
3992 * @returns VBox status code.
3993 * @param pDevExt The device extension.
3994 * @param pSession The calling session.
3995 * @param pReq The request packet, valid.
3996 */
3997static int supdrvIOCtl_CallServiceModule(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPCALLSERVICE pReq)
3998{
3999#if !defined(RT_OS_WINDOWS) || defined(DEBUG)
4000 int rc;
4001
4002 /*
4003 * Find the module first in the module referenced by the calling session.
4004 */
4005 rc = RTSemFastMutexRequest(pDevExt->mtxLdr);
4006 if (RT_SUCCESS(rc))
4007 {
4008 PFNSUPR0SERVICEREQHANDLER pfnServiceReqHandler = NULL;
4009 PSUPDRVLDRUSAGE pUsage;
4010
4011 for (pUsage = pSession->pLdrUsage; pUsage; pUsage = pUsage->pNext)
4012 if ( pUsage->pImage->pfnServiceReqHandler
4013 && !strcmp(pUsage->pImage->szName, pReq->u.In.szName))
4014 {
4015 pfnServiceReqHandler = pUsage->pImage->pfnServiceReqHandler;
4016 break;
4017 }
4018 RTSemFastMutexRelease(pDevExt->mtxLdr);
4019
4020 if (pfnServiceReqHandler)
4021 {
4022 /*
4023 * Call it.
4024 */
4025 if (pReq->Hdr.cbIn == SUP_IOCTL_CALL_SERVICE_SIZE(0))
4026#ifdef RT_WITH_W64_UNWIND_HACK
4027 rc = supdrvNtWrapServiceReqHandler((PFNRT)pfnServiceReqHandler, pSession, pReq->u.In.uOperation, pReq->u.In.u64Arg, NULL);
4028#else
4029 rc = pfnServiceReqHandler(pSession, pReq->u.In.uOperation, pReq->u.In.u64Arg, NULL);
4030#endif
4031 else
4032#ifdef RT_WITH_W64_UNWIND_HACK
4033 rc = supdrvNtWrapServiceReqHandler((PFNRT)pfnServiceReqHandler, pSession, pReq->u.In.uOperation,
4034 pReq->u.In.u64Arg, (PSUPR0SERVICEREQHDR)&pReq->abReqPkt[0]);
4035#else
4036 rc = pfnServiceReqHandler(pSession, pReq->u.In.uOperation, pReq->u.In.u64Arg, (PSUPR0SERVICEREQHDR)&pReq->abReqPkt[0]);
4037#endif
4038 }
4039 else
4040 rc = VERR_SUPDRV_SERVICE_NOT_FOUND;
4041 }
4042
4043 /* log it */
4044 if ( RT_FAILURE(rc)
4045 && rc != VERR_INTERRUPTED
4046 && rc != VERR_TIMEOUT)
4047 Log(("SUP_IOCTL_CALL_SERVICE: rc=%Rrc op=%u out=%u arg=%RX64 p/t=%RTproc/%RTthrd\n",
4048 rc, pReq->u.In.uOperation, pReq->Hdr.cbOut, pReq->u.In.u64Arg, RTProcSelf(), RTThreadNativeSelf()));
4049 else
4050 Log4(("SUP_IOCTL_CALL_SERVICE: rc=%Rrc op=%u out=%u arg=%RX64 p/t=%RTproc/%RTthrd\n",
4051 rc, pReq->u.In.uOperation, pReq->Hdr.cbOut, pReq->u.In.u64Arg, RTProcSelf(), RTThreadNativeSelf()));
4052 return rc;
4053#else /* RT_OS_WINDOWS && !DEBUG */
4054 return VERR_NOT_IMPLEMENTED;
4055#endif /* RT_OS_WINDOWS && !DEBUG */
4056}
4057
4058
4059/**
4060 * Implements the logger settings request.
4061 *
4062 * @returns VBox status code.
4063 * @param pDevExt The device extension.
4064 * @param pSession The caller's session.
4065 * @param pReq The request.
4066 */
4067static int supdrvIOCtl_LoggerSettings(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPLOGGERSETTINGS pReq)
4068{
4069 const char *pszGroup = &pReq->u.In.szStrings[pReq->u.In.offGroups];
4070 const char *pszFlags = &pReq->u.In.szStrings[pReq->u.In.offFlags];
4071 const char *pszDest = &pReq->u.In.szStrings[pReq->u.In.offDestination];
4072 PRTLOGGER pLogger = NULL;
4073 int rc;
4074
4075 /*
4076 * Some further validation.
4077 */
4078 switch (pReq->u.In.fWhat)
4079 {
4080 case SUPLOGGERSETTINGS_WHAT_SETTINGS:
4081 case SUPLOGGERSETTINGS_WHAT_CREATE:
4082 break;
4083
4084 case SUPLOGGERSETTINGS_WHAT_DESTROY:
4085 if (*pszGroup || *pszFlags || *pszDest)
4086 return VERR_INVALID_PARAMETER;
4087 if (pReq->u.In.fWhich == SUPLOGGERSETTINGS_WHICH_RELEASE)
4088 return VERR_ACCESS_DENIED;
4089 break;
4090
4091 default:
4092 return VERR_INTERNAL_ERROR;
4093 }
4094
4095 /*
4096 * Get the logger.
4097 */
4098 switch (pReq->u.In.fWhich)
4099 {
4100 case SUPLOGGERSETTINGS_WHICH_DEBUG:
4101 pLogger = RTLogGetDefaultInstance();
4102 break;
4103
4104 case SUPLOGGERSETTINGS_WHICH_RELEASE:
4105 pLogger = RTLogRelDefaultInstance();
4106 break;
4107
4108 default:
4109 return VERR_INTERNAL_ERROR;
4110 }
4111
4112 /*
4113 * Do the job.
4114 */
4115 switch (pReq->u.In.fWhat)
4116 {
4117 case SUPLOGGERSETTINGS_WHAT_SETTINGS:
4118 if (pLogger)
4119 {
4120 rc = RTLogFlags(pLogger, pszFlags);
4121 if (RT_SUCCESS(rc))
4122 rc = RTLogGroupSettings(pLogger, pszGroup);
4123 NOREF(pszDest);
4124 }
4125 else
4126 rc = VERR_NOT_FOUND;
4127 break;
4128
4129 case SUPLOGGERSETTINGS_WHAT_CREATE:
4130 {
4131 if (pLogger)
4132 rc = VERR_ALREADY_EXISTS;
4133 else
4134 {
4135 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
4136
4137 rc = RTLogCreate(&pLogger,
4138 0 /* fFlags */,
4139 pszGroup,
4140 pReq->u.In.fWhich == SUPLOGGERSETTINGS_WHICH_DEBUG
4141 ? "VBOX_LOG"
4142 : "VBOX_RELEASE_LOG",
4143 RT_ELEMENTS(s_apszGroups),
4144 s_apszGroups,
4145 RTLOGDEST_STDOUT | RTLOGDEST_DEBUGGER,
4146 NULL);
4147 if (RT_SUCCESS(rc))
4148 {
4149 rc = RTLogFlags(pLogger, pszFlags);
4150 NOREF(pszDest);
4151 if (RT_SUCCESS(rc))
4152 {
4153 switch (pReq->u.In.fWhich)
4154 {
4155 case SUPLOGGERSETTINGS_WHICH_DEBUG:
4156 pLogger = RTLogSetDefaultInstance(pLogger);
4157 break;
4158 case SUPLOGGERSETTINGS_WHICH_RELEASE:
4159 pLogger = RTLogRelSetDefaultInstance(pLogger);
4160 break;
4161 }
4162 }
4163 RTLogDestroy(pLogger);
4164 }
4165 }
4166 break;
4167 }
4168
4169 case SUPLOGGERSETTINGS_WHAT_DESTROY:
4170 switch (pReq->u.In.fWhich)
4171 {
4172 case SUPLOGGERSETTINGS_WHICH_DEBUG:
4173 pLogger = RTLogSetDefaultInstance(NULL);
4174 break;
4175 case SUPLOGGERSETTINGS_WHICH_RELEASE:
4176 pLogger = RTLogRelSetDefaultInstance(NULL);
4177 break;
4178 }
4179 rc = RTLogDestroy(pLogger);
4180 break;
4181
4182 default:
4183 {
4184 rc = VERR_INTERNAL_ERROR;
4185 break;
4186 }
4187 }
4188
4189 return rc;
4190}
4191
4192
4193/**
4194 * Gets the paging mode of the current CPU.
4195 *
4196 * @returns Paging mode, SUPPAGEINGMODE_INVALID on error.
4197 */
4198SUPR0DECL(SUPPAGINGMODE) SUPR0GetPagingMode(void)
4199{
4200 SUPPAGINGMODE enmMode;
4201
4202 RTR0UINTREG cr0 = ASMGetCR0();
4203 if ((cr0 & (X86_CR0_PG | X86_CR0_PE)) != (X86_CR0_PG | X86_CR0_PE))
4204 enmMode = SUPPAGINGMODE_INVALID;
4205 else
4206 {
4207 RTR0UINTREG cr4 = ASMGetCR4();
4208 uint32_t fNXEPlusLMA = 0;
4209 if (cr4 & X86_CR4_PAE)
4210 {
4211 uint32_t fAmdFeatures = ASMCpuId_EDX(0x80000001);
4212 if (fAmdFeatures & (X86_CPUID_AMD_FEATURE_EDX_NX | X86_CPUID_AMD_FEATURE_EDX_LONG_MODE))
4213 {
4214 uint64_t efer = ASMRdMsr(MSR_K6_EFER);
4215 if ((fAmdFeatures & X86_CPUID_AMD_FEATURE_EDX_NX) && (efer & MSR_K6_EFER_NXE))
4216 fNXEPlusLMA |= RT_BIT(0);
4217 if ((fAmdFeatures & X86_CPUID_AMD_FEATURE_EDX_LONG_MODE) && (efer & MSR_K6_EFER_LMA))
4218 fNXEPlusLMA |= RT_BIT(1);
4219 }
4220 }
4221
4222 switch ((cr4 & (X86_CR4_PAE | X86_CR4_PGE)) | fNXEPlusLMA)
4223 {
4224 case 0:
4225 enmMode = SUPPAGINGMODE_32_BIT;
4226 break;
4227
4228 case X86_CR4_PGE:
4229 enmMode = SUPPAGINGMODE_32_BIT_GLOBAL;
4230 break;
4231
4232 case X86_CR4_PAE:
4233 enmMode = SUPPAGINGMODE_PAE;
4234 break;
4235
4236 case X86_CR4_PAE | RT_BIT(0):
4237 enmMode = SUPPAGINGMODE_PAE_NX;
4238 break;
4239
4240 case X86_CR4_PAE | X86_CR4_PGE:
4241 enmMode = SUPPAGINGMODE_PAE_GLOBAL;
4242 break;
4243
4244 case X86_CR4_PAE | X86_CR4_PGE | RT_BIT(0):
4245 enmMode = SUPPAGINGMODE_PAE_GLOBAL;
4246 break;
4247
4248 case RT_BIT(1) | X86_CR4_PAE:
4249 enmMode = SUPPAGINGMODE_AMD64;
4250 break;
4251
4252 case RT_BIT(1) | X86_CR4_PAE | RT_BIT(0):
4253 enmMode = SUPPAGINGMODE_AMD64_NX;
4254 break;
4255
4256 case RT_BIT(1) | X86_CR4_PAE | X86_CR4_PGE:
4257 enmMode = SUPPAGINGMODE_AMD64_GLOBAL;
4258 break;
4259
4260 case RT_BIT(1) | X86_CR4_PAE | X86_CR4_PGE | RT_BIT(0):
4261 enmMode = SUPPAGINGMODE_AMD64_GLOBAL_NX;
4262 break;
4263
4264 default:
4265 AssertMsgFailed(("Cannot happen! cr4=%#x fNXEPlusLMA=%d\n", cr4, fNXEPlusLMA));
4266 enmMode = SUPPAGINGMODE_INVALID;
4267 break;
4268 }
4269 }
4270 return enmMode;
4271}
4272
4273
4274/**
4275 * Enables or disabled hardware virtualization extensions using native OS APIs.
4276 *
4277 * @returns VBox status code.
4278 * @retval VINF_SUCCESS on success.
4279 * @retval VERR_NOT_SUPPORTED if not supported by the native OS.
4280 *
4281 * @param fEnable Whether to enable or disable.
4282 */
4283SUPR0DECL(int) SUPR0EnableVTx(bool fEnable)
4284{
4285#ifdef RT_OS_DARWIN
4286 return supdrvOSEnableVTx(fEnable);
4287#else
4288 return VERR_NOT_SUPPORTED;
4289#endif
4290}
4291
4292
4293/**
4294 * Creates the GIP.
4295 *
4296 * @returns VBox status code.
4297 * @param pDevExt Instance data. GIP stuff may be updated.
4298 */
4299static int supdrvGipCreate(PSUPDRVDEVEXT pDevExt)
4300{
4301 PSUPGLOBALINFOPAGE pGip;
4302 RTHCPHYS HCPhysGip;
4303 uint32_t u32SystemResolution;
4304 uint32_t u32Interval;
4305 int rc;
4306
4307 LogFlow(("supdrvGipCreate:\n"));
4308
4309 /* assert order */
4310 Assert(pDevExt->u32SystemTimerGranularityGrant == 0);
4311 Assert(pDevExt->GipMemObj == NIL_RTR0MEMOBJ);
4312 Assert(!pDevExt->pGipTimer);
4313
4314 /*
4315 * Allocate a suitable page with a default kernel mapping.
4316 */
4317 rc = RTR0MemObjAllocLow(&pDevExt->GipMemObj, PAGE_SIZE, false);
4318 if (RT_FAILURE(rc))
4319 {
4320 OSDBGPRINT(("supdrvGipCreate: failed to allocate the GIP page. rc=%d\n", rc));
4321 return rc;
4322 }
4323 pGip = (PSUPGLOBALINFOPAGE)RTR0MemObjAddress(pDevExt->GipMemObj); AssertPtr(pGip);
4324 HCPhysGip = RTR0MemObjGetPagePhysAddr(pDevExt->GipMemObj, 0); Assert(HCPhysGip != NIL_RTHCPHYS);
4325
4326#if 0 /** @todo Disabled this as we didn't used to do it before and causes unnecessary stress on laptops.
4327 * It only applies to Windows and should probably revisited later, if possible made part of the
4328 * timer code (return min granularity in RTTimerGetSystemGranularity and set it in RTTimerStart). */
4329 /*
4330 * Try bump up the system timer resolution.
4331 * The more interrupts the better...
4332 */
4333 if ( RT_SUCCESS(RTTimerRequestSystemGranularity( 488281 /* 2048 HZ */, &u32SystemResolution))
4334 || RT_SUCCESS(RTTimerRequestSystemGranularity( 500000 /* 2000 HZ */, &u32SystemResolution))
4335 || RT_SUCCESS(RTTimerRequestSystemGranularity( 976563 /* 1024 HZ */, &u32SystemResolution))
4336 || RT_SUCCESS(RTTimerRequestSystemGranularity( 1000000 /* 1000 HZ */, &u32SystemResolution))
4337 || RT_SUCCESS(RTTimerRequestSystemGranularity( 1953125 /* 512 HZ */, &u32SystemResolution))
4338 || RT_SUCCESS(RTTimerRequestSystemGranularity( 2000000 /* 500 HZ */, &u32SystemResolution))
4339 || RT_SUCCESS(RTTimerRequestSystemGranularity( 3906250 /* 256 HZ */, &u32SystemResolution))
4340 || RT_SUCCESS(RTTimerRequestSystemGranularity( 4000000 /* 250 HZ */, &u32SystemResolution))
4341 || RT_SUCCESS(RTTimerRequestSystemGranularity( 7812500 /* 128 HZ */, &u32SystemResolution))
4342 || RT_SUCCESS(RTTimerRequestSystemGranularity(10000000 /* 100 HZ */, &u32SystemResolution))
4343 || RT_SUCCESS(RTTimerRequestSystemGranularity(15625000 /* 64 HZ */, &u32SystemResolution))
4344 || RT_SUCCESS(RTTimerRequestSystemGranularity(31250000 /* 32 HZ */, &u32SystemResolution))
4345 )
4346 {
4347 Assert(RTTimerGetSystemGranularity() <= u32SystemResolution);
4348 pDevExt->u32SystemTimerGranularityGrant = u32SystemResolution;
4349 }
4350#endif
4351
4352 /*
4353 * Find a reasonable update interval and initialize the structure.
4354 */
4355 u32Interval = u32SystemResolution = RTTimerGetSystemGranularity();
4356 while (u32Interval < 10000000 /* 10 ms */)
4357 u32Interval += u32SystemResolution;
4358
4359 supdrvGipInit(pDevExt, pGip, HCPhysGip, RTTimeSystemNanoTS(), 1000000000 / u32Interval /*=Hz*/);
4360
4361 /*
4362 * Create the timer.
4363 * If CPU_ALL isn't supported we'll have to fall back to synchronous mode.
4364 */
4365 if (pGip->u32Mode == SUPGIPMODE_ASYNC_TSC)
4366 {
4367 rc = RTTimerCreateEx(&pDevExt->pGipTimer, u32Interval, RTTIMER_FLAGS_CPU_ALL, supdrvGipAsyncTimer, pDevExt);
4368 if (rc == VERR_NOT_SUPPORTED)
4369 {
4370 OSDBGPRINT(("supdrvGipCreate: omni timer not supported, falling back to synchronous mode\n"));
4371 pGip->u32Mode = SUPGIPMODE_SYNC_TSC;
4372 }
4373 }
4374 if (pGip->u32Mode != SUPGIPMODE_ASYNC_TSC)
4375 rc = RTTimerCreateEx(&pDevExt->pGipTimer, u32Interval, 0, supdrvGipSyncTimer, pDevExt);
4376 if (RT_SUCCESS(rc))
4377 {
4378 if (pGip->u32Mode == SUPGIPMODE_ASYNC_TSC)
4379 rc = RTMpNotificationRegister(supdrvGipMpEvent, pDevExt);
4380 if (RT_SUCCESS(rc))
4381 {
4382 /*
4383 * We're good.
4384 */
4385 dprintf(("supdrvGipCreate: %ld ns interval.\n", (long)u32Interval));
4386 return VINF_SUCCESS;
4387 }
4388
4389 OSDBGPRINT(("supdrvGipCreate: failed register MP event notfication. rc=%d\n", rc));
4390 }
4391 else
4392 {
4393 OSDBGPRINT(("supdrvGipCreate: failed create GIP timer at %ld ns interval. rc=%d\n", (long)u32Interval, rc));
4394 Assert(!pDevExt->pGipTimer);
4395 }
4396 supdrvGipDestroy(pDevExt);
4397 return rc;
4398}
4399
4400
4401/**
4402 * Terminates the GIP.
4403 *
4404 * @param pDevExt Instance data. GIP stuff may be updated.
4405 */
4406static void supdrvGipDestroy(PSUPDRVDEVEXT pDevExt)
4407{
4408 int rc;
4409#ifdef DEBUG_DARWIN_GIP
4410 OSDBGPRINT(("supdrvGipDestroy: pDevExt=%p pGip=%p pGipTimer=%p GipMemObj=%p\n", pDevExt,
4411 pDevExt->GipMemObj != NIL_RTR0MEMOBJ ? RTR0MemObjAddress(pDevExt->GipMemObj) : NULL,
4412 pDevExt->pGipTimer, pDevExt->GipMemObj));
4413#endif
4414
4415 /*
4416 * Invalid the GIP data.
4417 */
4418 if (pDevExt->pGip)
4419 {
4420 supdrvGipTerm(pDevExt->pGip);
4421 pDevExt->pGip = NULL;
4422 }
4423
4424 /*
4425 * Destroy the timer and free the GIP memory object.
4426 */
4427 if (pDevExt->pGipTimer)
4428 {
4429 rc = RTTimerDestroy(pDevExt->pGipTimer); AssertRC(rc);
4430 pDevExt->pGipTimer = NULL;
4431 }
4432
4433 if (pDevExt->GipMemObj != NIL_RTR0MEMOBJ)
4434 {
4435 rc = RTR0MemObjFree(pDevExt->GipMemObj, true /* free mappings */); AssertRC(rc);
4436 pDevExt->GipMemObj = NIL_RTR0MEMOBJ;
4437 }
4438
4439 /*
4440 * Finally, release the system timer resolution request if one succeeded.
4441 */
4442 if (pDevExt->u32SystemTimerGranularityGrant)
4443 {
4444 rc = RTTimerReleaseSystemGranularity(pDevExt->u32SystemTimerGranularityGrant); AssertRC(rc);
4445 pDevExt->u32SystemTimerGranularityGrant = 0;
4446 }
4447}
4448
4449
4450/**
4451 * Timer callback function sync GIP mode.
4452 * @param pTimer The timer.
4453 * @param pvUser The device extension.
4454 */
4455static DECLCALLBACK(void) supdrvGipSyncTimer(PRTTIMER pTimer, void *pvUser, uint64_t iTick)
4456{
4457 RTCCUINTREG fOldFlags = ASMIntDisableFlags(); /* No interruptions please (real problem on S10). */
4458 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pvUser;
4459
4460 supdrvGipUpdate(pDevExt->pGip, RTTimeSystemNanoTS());
4461
4462 ASMSetFlags(fOldFlags);
4463}
4464
4465
4466/**
4467 * Timer callback function for async GIP mode.
4468 * @param pTimer The timer.
4469 * @param pvUser The device extension.
4470 */
4471static DECLCALLBACK(void) supdrvGipAsyncTimer(PRTTIMER pTimer, void *pvUser, uint64_t iTick)
4472{
4473 RTCCUINTREG fOldFlags = ASMIntDisableFlags(); /* No interruptions please (real problem on S10). */
4474 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pvUser;
4475 RTCPUID idCpu = RTMpCpuId();
4476 uint64_t NanoTS = RTTimeSystemNanoTS();
4477
4478 /** @todo reset the transaction number and whatnot when iTick == 1. */
4479 if (pDevExt->idGipMaster == idCpu)
4480 supdrvGipUpdate(pDevExt->pGip, NanoTS);
4481 else
4482 supdrvGipUpdatePerCpu(pDevExt->pGip, NanoTS, ASMGetApicId());
4483
4484 ASMSetFlags(fOldFlags);
4485}
4486
4487
4488/**
4489 * Multiprocessor event notification callback.
4490 *
4491 * This is used to make sue that the GIP master gets passed on to
4492 * another CPU.
4493 *
4494 * @param enmEvent The event.
4495 * @param idCpu The cpu it applies to.
4496 * @param pvUser Pointer to the device extension.
4497 */
4498static DECLCALLBACK(void) supdrvGipMpEvent(RTMPEVENT enmEvent, RTCPUID idCpu, void *pvUser)
4499{
4500 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pvUser;
4501 if (enmEvent == RTMPEVENT_OFFLINE)
4502 {
4503 RTCPUID idGipMaster;
4504 ASMAtomicReadSize(&pDevExt->idGipMaster, &idGipMaster);
4505 if (idGipMaster == idCpu)
4506 {
4507 /*
4508 * Find a new GIP master.
4509 */
4510 bool fIgnored;
4511 unsigned i;
4512 RTCPUID idNewGipMaster = NIL_RTCPUID;
4513 RTCPUSET OnlineCpus;
4514 RTMpGetOnlineSet(&OnlineCpus);
4515
4516 for (i = 0; i < RTCPUSET_MAX_CPUS; i++)
4517 {
4518 RTCPUID idCurCpu = RTMpCpuIdFromSetIndex(i);
4519 if ( RTCpuSetIsMember(&OnlineCpus, idCurCpu)
4520 && idCurCpu != idGipMaster)
4521 {
4522 idNewGipMaster = idCurCpu;
4523 break;
4524 }
4525 }
4526
4527 dprintf(("supdrvGipMpEvent: Gip master %#lx -> %#lx\n", (long)idGipMaster, (long)idNewGipMaster));
4528 ASMAtomicCmpXchgSize(&pDevExt->idGipMaster, idNewGipMaster, idGipMaster, fIgnored);
4529 NOREF(fIgnored);
4530 }
4531 }
4532}
4533
4534
4535/**
4536 * Initializes the GIP data.
4537 *
4538 * @returns IPRT status code.
4539 * @param pDevExt Pointer to the device instance data.
4540 * @param pGip Pointer to the read-write kernel mapping of the GIP.
4541 * @param HCPhys The physical address of the GIP.
4542 * @param u64NanoTS The current nanosecond timestamp.
4543 * @param uUpdateHz The update freqence.
4544 */
4545int VBOXCALL supdrvGipInit(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE pGip, RTHCPHYS HCPhys, uint64_t u64NanoTS, unsigned uUpdateHz)
4546{
4547 unsigned i;
4548#ifdef DEBUG_DARWIN_GIP
4549 OSDBGPRINT(("supdrvGipInit: pGip=%p HCPhys=%lx u64NanoTS=%llu uUpdateHz=%d\n", pGip, (long)HCPhys, u64NanoTS, uUpdateHz));
4550#else
4551 LogFlow(("supdrvGipInit: pGip=%p HCPhys=%lx u64NanoTS=%llu uUpdateHz=%d\n", pGip, (long)HCPhys, u64NanoTS, uUpdateHz));
4552#endif
4553
4554 /*
4555 * Initialize the structure.
4556 */
4557 memset(pGip, 0, PAGE_SIZE);
4558 pGip->u32Magic = SUPGLOBALINFOPAGE_MAGIC;
4559 pGip->u32Version = SUPGLOBALINFOPAGE_VERSION;
4560 pGip->u32Mode = supdrvGipDeterminTscMode(pDevExt);
4561 pGip->u32UpdateHz = uUpdateHz;
4562 pGip->u32UpdateIntervalNS = 1000000000 / uUpdateHz;
4563 pGip->u64NanoTSLastUpdateHz = u64NanoTS;
4564
4565 for (i = 0; i < RT_ELEMENTS(pGip->aCPUs); i++)
4566 {
4567 pGip->aCPUs[i].u32TransactionId = 2;
4568 pGip->aCPUs[i].u64NanoTS = u64NanoTS;
4569 pGip->aCPUs[i].u64TSC = ASMReadTSC();
4570
4571 /*
4572 * We don't know the following values until we've executed updates.
4573 * So, we'll just insert very high values.
4574 */
4575 pGip->aCPUs[i].u64CpuHz = _4G + 1;
4576 pGip->aCPUs[i].u32UpdateIntervalTSC = _2G / 4;
4577 pGip->aCPUs[i].au32TSCHistory[0] = _2G / 4;
4578 pGip->aCPUs[i].au32TSCHistory[1] = _2G / 4;
4579 pGip->aCPUs[i].au32TSCHistory[2] = _2G / 4;
4580 pGip->aCPUs[i].au32TSCHistory[3] = _2G / 4;
4581 pGip->aCPUs[i].au32TSCHistory[4] = _2G / 4;
4582 pGip->aCPUs[i].au32TSCHistory[5] = _2G / 4;
4583 pGip->aCPUs[i].au32TSCHistory[6] = _2G / 4;
4584 pGip->aCPUs[i].au32TSCHistory[7] = _2G / 4;
4585 }
4586
4587 /*
4588 * Link it to the device extension.
4589 */
4590 pDevExt->pGip = pGip;
4591 pDevExt->HCPhysGip = HCPhys;
4592 pDevExt->cGipUsers = 0;
4593
4594 return VINF_SUCCESS;
4595}
4596
4597
4598/**
4599 * Callback used by supdrvDetermineAsyncTSC to read the TSC on a CPU.
4600 *
4601 * @param idCpu Ignored.
4602 * @param pvUser1 Where to put the TSC.
4603 * @param pvUser2 Ignored.
4604 */
4605static DECLCALLBACK(void) supdrvDetermineAsyncTscWorker(RTCPUID idCpu, void *pvUser1, void *pvUser2)
4606{
4607#if 1
4608 ASMAtomicWriteU64((uint64_t volatile *)pvUser1, ASMReadTSC());
4609#else
4610 *(uint64_t *)pvUser1 = ASMReadTSC();
4611#endif
4612}
4613
4614
4615/**
4616 * Determine if Async GIP mode is required because of TSC drift.
4617 *
4618 * When using the default/normal timer code it is essential that the time stamp counter
4619 * (TSC) runs never backwards, that is, a read operation to the counter should return
4620 * a bigger value than any previous read operation. This is guaranteed by the latest
4621 * AMD CPUs and by newer Intel CPUs which never enter the C2 state (P4). In any other
4622 * case we have to choose the asynchronous timer mode.
4623 *
4624 * @param poffMin Pointer to the determined difference between different cores.
4625 * @return false if the time stamp counters appear to be synchron, true otherwise.
4626 */
4627bool VBOXCALL supdrvDetermineAsyncTsc(uint64_t *poffMin)
4628{
4629 /*
4630 * Just iterate all the cpus 8 times and make sure that the TSC is
4631 * ever increasing. We don't bother taking TSC rollover into account.
4632 */
4633 RTCPUSET CpuSet;
4634 int iLastCpu = RTCpuLastIndex(RTMpGetSet(&CpuSet));
4635 int iCpu;
4636 int cLoops = 8;
4637 bool fAsync = false;
4638 int rc = VINF_SUCCESS;
4639 uint64_t offMax = 0;
4640 uint64_t offMin = ~(uint64_t)0;
4641 uint64_t PrevTsc = ASMReadTSC();
4642
4643 while (cLoops-- > 0)
4644 {
4645 for (iCpu = 0; iCpu <= iLastCpu; iCpu++)
4646 {
4647 uint64_t CurTsc;
4648 rc = RTMpOnSpecific(RTMpCpuIdFromSetIndex(iCpu), supdrvDetermineAsyncTscWorker, &CurTsc, NULL);
4649 if (RT_SUCCESS(rc))
4650 {
4651 if (CurTsc <= PrevTsc)
4652 {
4653 fAsync = true;
4654 offMin = offMax = PrevTsc - CurTsc;
4655 dprintf(("supdrvDetermineAsyncTsc: iCpu=%d cLoops=%d CurTsc=%llx PrevTsc=%llx\n",
4656 iCpu, cLoops, CurTsc, PrevTsc));
4657 break;
4658 }
4659
4660 /* Gather statistics (except the first time). */
4661 if (iCpu != 0 || cLoops != 7)
4662 {
4663 uint64_t off = CurTsc - PrevTsc;
4664 if (off < offMin)
4665 offMin = off;
4666 if (off > offMax)
4667 offMax = off;
4668 dprintf2(("%d/%d: off=%llx\n", cLoops, iCpu, off));
4669 }
4670
4671 /* Next */
4672 PrevTsc = CurTsc;
4673 }
4674 else if (rc == VERR_NOT_SUPPORTED)
4675 break;
4676 else
4677 AssertMsg(rc == VERR_CPU_NOT_FOUND || rc == VERR_CPU_OFFLINE, ("%d\n", rc));
4678 }
4679
4680 /* broke out of the loop. */
4681 if (iCpu <= iLastCpu)
4682 break;
4683 }
4684
4685 *poffMin = offMin; /* Almost RTMpOnSpecific profiling. */
4686 dprintf(("supdrvDetermineAsyncTsc: returns %d; iLastCpu=%d rc=%d offMin=%llx offMax=%llx\n",
4687 fAsync, iLastCpu, rc, offMin, offMax));
4688#if !defined(RT_OS_SOLARIS) && !defined(RT_OS_OS2) && !defined(RT_OS_WINDOWS)
4689 OSDBGPRINT(("vboxdrv: fAsync=%d offMin=%#lx offMax=%#lx\n", fAsync, (long)offMin, (long)offMax));
4690#endif
4691 return fAsync;
4692}
4693
4694
4695/**
4696 * Determin the GIP TSC mode.
4697 *
4698 * @returns The most suitable TSC mode.
4699 * @param pDevExt Pointer to the device instance data.
4700 */
4701static SUPGIPMODE supdrvGipDeterminTscMode(PSUPDRVDEVEXT pDevExt)
4702{
4703 /*
4704 * On SMP we're faced with two problems:
4705 * (1) There might be a skew between the CPU, so that cpu0
4706 * returns a TSC that is sligtly different from cpu1.
4707 * (2) Power management (and other things) may cause the TSC
4708 * to run at a non-constant speed, and cause the speed
4709 * to be different on the cpus. This will result in (1).
4710 *
4711 * So, on SMP systems we'll have to select the ASYNC update method
4712 * if there are symphoms of these problems.
4713 */
4714 if (RTMpGetCount() > 1)
4715 {
4716 uint32_t uEAX, uEBX, uECX, uEDX;
4717 uint64_t u64DiffCoresIgnored;
4718
4719 /* Permit the user and/or the OS specfic bits to force async mode. */
4720 if (supdrvOSGetForcedAsyncTscMode(pDevExt))
4721 return SUPGIPMODE_ASYNC_TSC;
4722
4723 /* Try check for current differences between the cpus. */
4724 if (supdrvDetermineAsyncTsc(&u64DiffCoresIgnored))
4725 return SUPGIPMODE_ASYNC_TSC;
4726
4727 /*
4728 * If the CPU supports power management and is an AMD one we
4729 * won't trust it unless it has the TscInvariant bit is set.
4730 */
4731 /* Check for "AuthenticAMD" */
4732 ASMCpuId(0, &uEAX, &uEBX, &uECX, &uEDX);
4733 if ( uEAX >= 1
4734 && uEBX == X86_CPUID_VENDOR_AMD_EBX
4735 && uECX == X86_CPUID_VENDOR_AMD_ECX
4736 && uEDX == X86_CPUID_VENDOR_AMD_EDX)
4737 {
4738 /* Check for APM support and that TscInvariant is cleared. */
4739 ASMCpuId(0x80000000, &uEAX, &uEBX, &uECX, &uEDX);
4740 if (uEAX >= 0x80000007)
4741 {
4742 ASMCpuId(0x80000007, &uEAX, &uEBX, &uECX, &uEDX);
4743 if ( !(uEDX & RT_BIT(8))/* TscInvariant */
4744 && (uEDX & 0x3e)) /* STC|TM|THERMTRIP|VID|FID. Ignore TS. */
4745 return SUPGIPMODE_ASYNC_TSC;
4746 }
4747 }
4748 }
4749 return SUPGIPMODE_SYNC_TSC;
4750}
4751
4752
4753/**
4754 * Invalidates the GIP data upon termination.
4755 *
4756 * @param pGip Pointer to the read-write kernel mapping of the GIP.
4757 */
4758void VBOXCALL supdrvGipTerm(PSUPGLOBALINFOPAGE pGip)
4759{
4760 unsigned i;
4761 pGip->u32Magic = 0;
4762 for (i = 0; i < RT_ELEMENTS(pGip->aCPUs); i++)
4763 {
4764 pGip->aCPUs[i].u64NanoTS = 0;
4765 pGip->aCPUs[i].u64TSC = 0;
4766 pGip->aCPUs[i].iTSCHistoryHead = 0;
4767 }
4768}
4769
4770
4771/**
4772 * Worker routine for supdrvGipUpdate and supdrvGipUpdatePerCpu that
4773 * updates all the per cpu data except the transaction id.
4774 *
4775 * @param pGip The GIP.
4776 * @param pGipCpu Pointer to the per cpu data.
4777 * @param u64NanoTS The current time stamp.
4778 */
4779static void supdrvGipDoUpdateCpu(PSUPGLOBALINFOPAGE pGip, PSUPGIPCPU pGipCpu, uint64_t u64NanoTS)
4780{
4781 uint64_t u64TSC;
4782 uint64_t u64TSCDelta;
4783 uint32_t u32UpdateIntervalTSC;
4784 uint32_t u32UpdateIntervalTSCSlack;
4785 unsigned iTSCHistoryHead;
4786 uint64_t u64CpuHz;
4787
4788 /*
4789 * Update the NanoTS.
4790 */
4791 ASMAtomicXchgU64(&pGipCpu->u64NanoTS, u64NanoTS);
4792
4793 /*
4794 * Calc TSC delta.
4795 */
4796 /** @todo validate the NanoTS delta, don't trust the OS to call us when it should... */
4797 u64TSC = ASMReadTSC();
4798 u64TSCDelta = u64TSC - pGipCpu->u64TSC;
4799 ASMAtomicXchgU64(&pGipCpu->u64TSC, u64TSC);
4800
4801 if (u64TSCDelta >> 32)
4802 {
4803 u64TSCDelta = pGipCpu->u32UpdateIntervalTSC;
4804 pGipCpu->cErrors++;
4805 }
4806
4807 /*
4808 * TSC History.
4809 */
4810 Assert(RT_ELEMENTS(pGipCpu->au32TSCHistory) == 8);
4811
4812 iTSCHistoryHead = (pGipCpu->iTSCHistoryHead + 1) & 7;
4813 ASMAtomicXchgU32(&pGipCpu->iTSCHistoryHead, iTSCHistoryHead);
4814 ASMAtomicXchgU32(&pGipCpu->au32TSCHistory[iTSCHistoryHead], (uint32_t)u64TSCDelta);
4815
4816 /*
4817 * UpdateIntervalTSC = average of last 8,2,1 intervals depending on update HZ.
4818 */
4819 if (pGip->u32UpdateHz >= 1000)
4820 {
4821 uint32_t u32;
4822 u32 = pGipCpu->au32TSCHistory[0];
4823 u32 += pGipCpu->au32TSCHistory[1];
4824 u32 += pGipCpu->au32TSCHistory[2];
4825 u32 += pGipCpu->au32TSCHistory[3];
4826 u32 >>= 2;
4827 u32UpdateIntervalTSC = pGipCpu->au32TSCHistory[4];
4828 u32UpdateIntervalTSC += pGipCpu->au32TSCHistory[5];
4829 u32UpdateIntervalTSC += pGipCpu->au32TSCHistory[6];
4830 u32UpdateIntervalTSC += pGipCpu->au32TSCHistory[7];
4831 u32UpdateIntervalTSC >>= 2;
4832 u32UpdateIntervalTSC += u32;
4833 u32UpdateIntervalTSC >>= 1;
4834
4835 /* Value choosen for a 2GHz Athlon64 running linux 2.6.10/11, . */
4836 u32UpdateIntervalTSCSlack = u32UpdateIntervalTSC >> 14;
4837 }
4838 else if (pGip->u32UpdateHz >= 90)
4839 {
4840 u32UpdateIntervalTSC = (uint32_t)u64TSCDelta;
4841 u32UpdateIntervalTSC += pGipCpu->au32TSCHistory[(iTSCHistoryHead - 1) & 7];
4842 u32UpdateIntervalTSC >>= 1;
4843
4844 /* value choosen on a 2GHz thinkpad running windows */
4845 u32UpdateIntervalTSCSlack = u32UpdateIntervalTSC >> 7;
4846 }
4847 else
4848 {
4849 u32UpdateIntervalTSC = (uint32_t)u64TSCDelta;
4850
4851 /* This value hasn't be checked yet.. waiting for OS/2 and 33Hz timers.. :-) */
4852 u32UpdateIntervalTSCSlack = u32UpdateIntervalTSC >> 6;
4853 }
4854 ASMAtomicXchgU32(&pGipCpu->u32UpdateIntervalTSC, u32UpdateIntervalTSC + u32UpdateIntervalTSCSlack);
4855
4856 /*
4857 * CpuHz.
4858 */
4859 u64CpuHz = ASMMult2xU32RetU64(u32UpdateIntervalTSC, pGip->u32UpdateHz);
4860 ASMAtomicXchgU64(&pGipCpu->u64CpuHz, u64CpuHz);
4861}
4862
4863
4864/**
4865 * Updates the GIP.
4866 *
4867 * @param pGip Pointer to the GIP.
4868 * @param u64NanoTS The current nanosecond timesamp.
4869 */
4870void VBOXCALL supdrvGipUpdate(PSUPGLOBALINFOPAGE pGip, uint64_t u64NanoTS)
4871{
4872 /*
4873 * Determin the relevant CPU data.
4874 */
4875 PSUPGIPCPU pGipCpu;
4876 if (pGip->u32Mode != SUPGIPMODE_ASYNC_TSC)
4877 pGipCpu = &pGip->aCPUs[0];
4878 else
4879 {
4880 unsigned iCpu = ASMGetApicId();
4881 if (RT_LIKELY(iCpu >= RT_ELEMENTS(pGip->aCPUs)))
4882 return;
4883 pGipCpu = &pGip->aCPUs[iCpu];
4884 }
4885
4886 /*
4887 * Start update transaction.
4888 */
4889 if (!(ASMAtomicIncU32(&pGipCpu->u32TransactionId) & 1))
4890 {
4891 /* this can happen on win32 if we're taking to long and there are more CPUs around. shouldn't happen though. */
4892 AssertMsgFailed(("Invalid transaction id, %#x, not odd!\n", pGipCpu->u32TransactionId));
4893 ASMAtomicIncU32(&pGipCpu->u32TransactionId);
4894 pGipCpu->cErrors++;
4895 return;
4896 }
4897
4898 /*
4899 * Recalc the update frequency every 0x800th time.
4900 */
4901 if (!(pGipCpu->u32TransactionId & (GIP_UPDATEHZ_RECALC_FREQ * 2 - 2)))
4902 {
4903 if (pGip->u64NanoTSLastUpdateHz)
4904 {
4905#ifdef RT_ARCH_AMD64 /** @todo fix 64-bit div here to work on x86 linux. */
4906 uint64_t u64Delta = u64NanoTS - pGip->u64NanoTSLastUpdateHz;
4907 uint32_t u32UpdateHz = (uint32_t)((UINT64_C(1000000000) * GIP_UPDATEHZ_RECALC_FREQ) / u64Delta);
4908 if (u32UpdateHz <= 2000 && u32UpdateHz >= 30)
4909 {
4910 ASMAtomicXchgU32(&pGip->u32UpdateHz, u32UpdateHz);
4911 ASMAtomicXchgU32(&pGip->u32UpdateIntervalNS, 1000000000 / u32UpdateHz);
4912 }
4913#endif
4914 }
4915 ASMAtomicXchgU64(&pGip->u64NanoTSLastUpdateHz, u64NanoTS);
4916 }
4917
4918 /*
4919 * Update the data.
4920 */
4921 supdrvGipDoUpdateCpu(pGip, pGipCpu, u64NanoTS);
4922
4923 /*
4924 * Complete transaction.
4925 */
4926 ASMAtomicIncU32(&pGipCpu->u32TransactionId);
4927}
4928
4929
4930/**
4931 * Updates the per cpu GIP data for the calling cpu.
4932 *
4933 * @param pGip Pointer to the GIP.
4934 * @param u64NanoTS The current nanosecond timesamp.
4935 * @param iCpu The CPU index.
4936 */
4937void VBOXCALL supdrvGipUpdatePerCpu(PSUPGLOBALINFOPAGE pGip, uint64_t u64NanoTS, unsigned iCpu)
4938{
4939 PSUPGIPCPU pGipCpu;
4940
4941 if (RT_LIKELY(iCpu < RT_ELEMENTS(pGip->aCPUs)))
4942 {
4943 pGipCpu = &pGip->aCPUs[iCpu];
4944
4945 /*
4946 * Start update transaction.
4947 */
4948 if (!(ASMAtomicIncU32(&pGipCpu->u32TransactionId) & 1))
4949 {
4950 AssertMsgFailed(("Invalid transaction id, %#x, not odd!\n", pGipCpu->u32TransactionId));
4951 ASMAtomicIncU32(&pGipCpu->u32TransactionId);
4952 pGipCpu->cErrors++;
4953 return;
4954 }
4955
4956 /*
4957 * Update the data.
4958 */
4959 supdrvGipDoUpdateCpu(pGip, pGipCpu, u64NanoTS);
4960
4961 /*
4962 * Complete transaction.
4963 */
4964 ASMAtomicIncU32(&pGipCpu->u32TransactionId);
4965 }
4966}
4967
Note: See TracBrowser for help on using the repository browser.

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