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