VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/SUPDrv.cpp@ 70917

Last change on this file since 70917 was 70917, checked in by vboxsync, 7 years ago

SUPDrv,VMM: Added SUPR0GetRawModeUsability() for checking whether we're allowed to modify CR4 under Hyper-V. It's called from VMMR0/ModuleInit and the result is cached in a global variable and checked before we call into RC.

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

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