VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/SUPLib.cpp@ 49634

Last change on this file since 49634 was 49634, checked in by vboxsync, 11 years ago

SUPDrv: Added an interface for probing MSRs.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 74.7 KB
Line 
1/* $Id: SUPLib.cpp 49634 2013-11-22 18:11:29Z vboxsync $ */
2/** @file
3 * VirtualBox Support Library - Common code.
4 */
5
6/*
7 * Copyright (C) 2006-2013 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/** @page pg_sup SUP - The Support Library
28 *
29 * The support library is responsible for providing facilities to load
30 * VMM Host Ring-0 code, to call Host VMM Ring-0 code from Ring-3 Host
31 * code, to pin down physical memory, and more.
32 *
33 * The VMM Host Ring-0 code can be combined in the support driver if
34 * permitted by kernel module license policies. If it is not combined
35 * it will be externalized in a .r0 module that will be loaded using
36 * the IPRT loader.
37 *
38 * The Ring-0 calling is done thru a generic SUP interface which will
39 * transfer an argument set and call a predefined entry point in the Host
40 * VMM Ring-0 code.
41 *
42 * See @ref grp_sup "SUP - Support APIs" for API details.
43 */
44
45/*******************************************************************************
46* Header Files *
47*******************************************************************************/
48#define LOG_GROUP LOG_GROUP_SUP
49#include <VBox/sup.h>
50#include <VBox/err.h>
51#include <VBox/param.h>
52#include <VBox/log.h>
53#include <VBox/VBoxTpG.h>
54
55#include <iprt/assert.h>
56#include <iprt/alloc.h>
57#include <iprt/alloca.h>
58#include <iprt/ldr.h>
59#include <iprt/asm.h>
60#include <iprt/mp.h>
61#include <iprt/cpuset.h>
62#include <iprt/thread.h>
63#include <iprt/process.h>
64#include <iprt/path.h>
65#include <iprt/string.h>
66#include <iprt/env.h>
67#include <iprt/rand.h>
68#include <iprt/x86.h>
69
70#include "SUPDrvIOC.h"
71#include "SUPLibInternal.h"
72
73
74/*******************************************************************************
75* Defined Constants And Macros *
76*******************************************************************************/
77/** R0 VMM module name. */
78#define VMMR0_NAME "VMMR0"
79
80
81/*******************************************************************************
82* Structures and Typedefs *
83*******************************************************************************/
84typedef DECLCALLBACK(int) FNCALLVMMR0(PVMR0 pVMR0, unsigned uOperation, void *pvArg);
85typedef FNCALLVMMR0 *PFNCALLVMMR0;
86
87
88/*******************************************************************************
89* Global Variables *
90*******************************************************************************/
91/** Init counter. */
92static uint32_t g_cInits = 0;
93/** Whether we've been preinitied. */
94static bool g_fPreInited = false;
95/** The SUPLib instance data.
96 * Well, at least parts of it, specifically the parts that are being handed over
97 * via the pre-init mechanism from the hardened executable stub. */
98SUPLIBDATA g_supLibData =
99{
100 /*.hDevice = */ SUP_HDEVICE_NIL,
101 /*.fUnrestricted = */ true
102#if defined(RT_OS_DARWIN)
103 ,/* .uConnection = */ NULL
104#elif defined(RT_OS_LINUX)
105 ,/* .fSysMadviseWorks = */ false
106#endif
107};
108
109/** Pointer to the Global Information Page.
110 *
111 * This pointer is valid as long as SUPLib has a open session. Anyone using
112 * the page must treat this pointer as highly volatile and not trust it beyond
113 * one transaction.
114 *
115 * @todo This will probably deserve it's own session or some other good solution...
116 */
117DECLEXPORT(PSUPGLOBALINFOPAGE) g_pSUPGlobalInfoPage;
118/** Address of the ring-0 mapping of the GIP. */
119PSUPGLOBALINFOPAGE g_pSUPGlobalInfoPageR0;
120/** The physical address of the GIP. */
121static RTHCPHYS g_HCPhysSUPGlobalInfoPage = NIL_RTHCPHYS;
122
123/** The negotiated cookie. */
124uint32_t g_u32Cookie = 0;
125/** The negotiated session cookie. */
126uint32_t g_u32SessionCookie;
127/** Session handle. */
128PSUPDRVSESSION g_pSession;
129/** R0 SUP Functions used for resolving referenced to the SUPR0 module. */
130PSUPQUERYFUNCS g_pSupFunctions;
131
132/** VMMR0 Load Address. */
133static RTR0PTR g_pvVMMR0 = NIL_RTR0PTR;
134/** PAGE_ALLOC_EX sans kernel mapping support indicator. */
135static bool g_fSupportsPageAllocNoKernel = true;
136/** Fake mode indicator. (~0 at first, 0 or 1 after first test) */
137uint32_t g_uSupFakeMode = ~0;
138
139
140/*******************************************************************************
141* Internal Functions *
142*******************************************************************************/
143static int supInitFake(PSUPDRVSESSION *ppSession);
144static int supLoadModule(const char *pszFilename, const char *pszModule, const char *pszSrvReqHandler, void **ppvImageBase);
145static DECLCALLBACK(int) supLoadModuleResolveImport(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser);
146
147
148/** Touch a range of pages. */
149DECLINLINE(void) supR3TouchPages(void *pv, size_t cPages)
150{
151 uint32_t volatile *pu32 = (uint32_t volatile *)pv;
152 while (cPages-- > 0)
153 {
154 ASMAtomicCmpXchgU32(pu32, 0, 0);
155 pu32 += PAGE_SIZE / sizeof(uint32_t);
156 }
157}
158
159
160SUPR3DECL(int) SUPR3Install(void)
161{
162 return suplibOsInstall();
163}
164
165
166SUPR3DECL(int) SUPR3Uninstall(void)
167{
168 return suplibOsUninstall();
169}
170
171
172DECLEXPORT(int) supR3PreInit(PSUPPREINITDATA pPreInitData, uint32_t fFlags)
173{
174 /*
175 * The caller is kind of trustworthy, just perform some basic checks.
176 *
177 * Note! Do not do any fancy stuff here because IPRT has NOT been
178 * initialized at this point.
179 */
180 if (!VALID_PTR(pPreInitData))
181 return VERR_INVALID_POINTER;
182 if (g_fPreInited || g_cInits > 0)
183 return VERR_WRONG_ORDER;
184
185 if ( pPreInitData->u32Magic != SUPPREINITDATA_MAGIC
186 || pPreInitData->u32EndMagic != SUPPREINITDATA_MAGIC)
187 return VERR_INVALID_MAGIC;
188 if ( !(fFlags & SUPSECMAIN_FLAGS_DONT_OPEN_DEV)
189 && pPreInitData->Data.hDevice == SUP_HDEVICE_NIL)
190 return VERR_INVALID_HANDLE;
191 if ( (fFlags & SUPSECMAIN_FLAGS_DONT_OPEN_DEV)
192 && pPreInitData->Data.hDevice != SUP_HDEVICE_NIL)
193 return VERR_INVALID_PARAMETER;
194
195 /*
196 * Hand out the data.
197 */
198 int rc = supR3HardenedRecvPreInitData(pPreInitData);
199 if (RT_FAILURE(rc))
200 return rc;
201
202 /** @todo This may need some small restructuring later, it doesn't quite work with a root service flag... */
203 if (!(fFlags & SUPSECMAIN_FLAGS_DONT_OPEN_DEV))
204 {
205 g_supLibData = pPreInitData->Data;
206 g_fPreInited = true;
207 }
208
209 return VINF_SUCCESS;
210}
211
212
213SUPR3DECL(int) SUPR3InitEx(bool fUnrestricted, PSUPDRVSESSION *ppSession)
214{
215 /*
216 * Perform some sanity checks.
217 * (Got some trouble with compile time member alignment assertions.)
218 */
219 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, u64NanoTSLastUpdateHz) & 0x7));
220 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs) & 0x1f));
221 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs[1]) & 0x1f));
222 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs[0].u64NanoTS) & 0x7));
223 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs[0].u64TSC) & 0x7));
224 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs[0].u64CpuHz) & 0x7));
225
226 /*
227 * Check if already initialized.
228 */
229 if (ppSession)
230 *ppSession = g_pSession;
231 if (g_cInits++ > 0)
232 {
233 if (fUnrestricted && !g_supLibData.fUnrestricted)
234 {
235 g_cInits--;
236 if (ppSession)
237 *ppSession = NIL_RTR0PTR;
238 return VERR_VM_DRIVER_NOT_ACCESSIBLE; /** @todo different status code? */
239 }
240 return VINF_SUCCESS;
241 }
242
243 /*
244 * Check for fake mode.
245 *
246 * Fake mode is used when we're doing smoke testing and debugging.
247 * It's also useful on platforms where we haven't root access or which
248 * we haven't ported the support driver to.
249 */
250 if (g_uSupFakeMode == ~0U)
251 {
252 const char *psz = RTEnvGet("VBOX_SUPLIB_FAKE");
253 if (psz && !strcmp(psz, "fake"))
254 ASMAtomicCmpXchgU32(&g_uSupFakeMode, 1, ~0U);
255 else
256 ASMAtomicCmpXchgU32(&g_uSupFakeMode, 0, ~0U);
257 }
258 if (RT_UNLIKELY(g_uSupFakeMode))
259 return supInitFake(ppSession);
260
261 /*
262 * Open the support driver.
263 */
264 int rc = suplibOsInit(&g_supLibData, g_fPreInited, fUnrestricted);
265 if (RT_SUCCESS(rc))
266 {
267 /*
268 * Negotiate the cookie.
269 */
270 SUPCOOKIE CookieReq;
271 memset(&CookieReq, 0xff, sizeof(CookieReq));
272 CookieReq.Hdr.u32Cookie = SUPCOOKIE_INITIAL_COOKIE;
273 CookieReq.Hdr.u32SessionCookie = RTRandU32();
274 CookieReq.Hdr.cbIn = SUP_IOCTL_COOKIE_SIZE_IN;
275 CookieReq.Hdr.cbOut = SUP_IOCTL_COOKIE_SIZE_OUT;
276 CookieReq.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
277 CookieReq.Hdr.rc = VERR_INTERNAL_ERROR;
278 strcpy(CookieReq.u.In.szMagic, SUPCOOKIE_MAGIC);
279 CookieReq.u.In.u32ReqVersion = SUPDRV_IOC_VERSION;
280 const uint32_t uMinVersion = (SUPDRV_IOC_VERSION & 0xffff0000) == 0x001a0000
281 ? 0x001a0005
282 : SUPDRV_IOC_VERSION & 0xffff0000;
283 CookieReq.u.In.u32MinVersion = uMinVersion;
284 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_COOKIE, &CookieReq, SUP_IOCTL_COOKIE_SIZE);
285 if ( RT_SUCCESS(rc)
286 && RT_SUCCESS(CookieReq.Hdr.rc))
287 {
288 if ( (CookieReq.u.Out.u32SessionVersion & 0xffff0000) == (SUPDRV_IOC_VERSION & 0xffff0000)
289 && CookieReq.u.Out.u32SessionVersion >= uMinVersion)
290 {
291 /*
292 * Query the functions.
293 */
294 PSUPQUERYFUNCS pFuncsReq = NULL;
295 if (g_supLibData.fUnrestricted)
296 {
297 pFuncsReq = (PSUPQUERYFUNCS)RTMemAllocZ(SUP_IOCTL_QUERY_FUNCS_SIZE(CookieReq.u.Out.cFunctions));
298 if (pFuncsReq)
299 {
300 pFuncsReq->Hdr.u32Cookie = CookieReq.u.Out.u32Cookie;
301 pFuncsReq->Hdr.u32SessionCookie = CookieReq.u.Out.u32SessionCookie;
302 pFuncsReq->Hdr.cbIn = SUP_IOCTL_QUERY_FUNCS_SIZE_IN;
303 pFuncsReq->Hdr.cbOut = SUP_IOCTL_QUERY_FUNCS_SIZE_OUT(CookieReq.u.Out.cFunctions);
304 pFuncsReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
305 pFuncsReq->Hdr.rc = VERR_INTERNAL_ERROR;
306 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_QUERY_FUNCS(CookieReq.u.Out.cFunctions), pFuncsReq,
307 SUP_IOCTL_QUERY_FUNCS_SIZE(CookieReq.u.Out.cFunctions));
308 if (RT_SUCCESS(rc))
309 rc = pFuncsReq->Hdr.rc;
310 if (RT_SUCCESS(rc))
311 {
312 /*
313 * Map the GIP into userspace.
314 */
315 Assert(!g_pSUPGlobalInfoPage);
316 SUPGIPMAP GipMapReq;
317 GipMapReq.Hdr.u32Cookie = CookieReq.u.Out.u32Cookie;
318 GipMapReq.Hdr.u32SessionCookie = CookieReq.u.Out.u32SessionCookie;
319 GipMapReq.Hdr.cbIn = SUP_IOCTL_GIP_MAP_SIZE_IN;
320 GipMapReq.Hdr.cbOut = SUP_IOCTL_GIP_MAP_SIZE_OUT;
321 GipMapReq.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
322 GipMapReq.Hdr.rc = VERR_INTERNAL_ERROR;
323 GipMapReq.u.Out.HCPhysGip = NIL_RTHCPHYS;
324 GipMapReq.u.Out.pGipR0 = NIL_RTR0PTR;
325 GipMapReq.u.Out.pGipR3 = NULL;
326 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_GIP_MAP, &GipMapReq, SUP_IOCTL_GIP_MAP_SIZE);
327 if (RT_SUCCESS(rc))
328 rc = GipMapReq.Hdr.rc;
329 if (RT_SUCCESS(rc))
330 {
331 /*
332 * Set the GIP globals.
333 */
334 AssertRelease(GipMapReq.u.Out.pGipR3->u32Magic == SUPGLOBALINFOPAGE_MAGIC);
335 AssertRelease(GipMapReq.u.Out.pGipR3->u32Version >= SUPGLOBALINFOPAGE_VERSION);
336
337 ASMAtomicXchgSize(&g_HCPhysSUPGlobalInfoPage, GipMapReq.u.Out.HCPhysGip);
338 ASMAtomicCmpXchgPtr((void * volatile *)&g_pSUPGlobalInfoPage, GipMapReq.u.Out.pGipR3, NULL);
339 ASMAtomicCmpXchgPtr((void * volatile *)&g_pSUPGlobalInfoPageR0, (void *)GipMapReq.u.Out.pGipR0, NULL);
340 }
341 }
342 }
343 else
344 rc = VERR_NO_MEMORY;
345 }
346
347 if (RT_SUCCESS(rc))
348 {
349 /*
350 * Set the globals and return success.
351 */
352 g_u32Cookie = CookieReq.u.Out.u32Cookie;
353 g_u32SessionCookie = CookieReq.u.Out.u32SessionCookie;
354 g_pSession = CookieReq.u.Out.pSession;
355 g_pSupFunctions = pFuncsReq;
356 if (ppSession)
357 *ppSession = CookieReq.u.Out.pSession;
358 return VINF_SUCCESS;
359 }
360
361 /* bailout */
362 RTMemFree(pFuncsReq);
363 }
364 else
365 {
366 LogRel(("Support driver version mismatch: SessionVersion=%#x DriverVersion=%#x ClientVersion=%#x MinVersion=%#x\n",
367 CookieReq.u.Out.u32SessionVersion, CookieReq.u.Out.u32DriverVersion, SUPDRV_IOC_VERSION, uMinVersion));
368 rc = VERR_VM_DRIVER_VERSION_MISMATCH;
369 }
370 }
371 else
372 {
373 if (RT_SUCCESS(rc))
374 {
375 rc = CookieReq.Hdr.rc;
376 LogRel(("Support driver version mismatch: DriverVersion=%#x ClientVersion=%#x rc=%Rrc\n",
377 CookieReq.u.Out.u32DriverVersion, SUPDRV_IOC_VERSION, rc));
378 if (rc != VERR_VM_DRIVER_VERSION_MISMATCH)
379 rc = VERR_VM_DRIVER_VERSION_MISMATCH;
380 }
381 else
382 {
383 /* for pre 0x00060000 drivers */
384 LogRel(("Support driver version mismatch: DriverVersion=too-old ClientVersion=%#x\n", SUPDRV_IOC_VERSION));
385 rc = VERR_VM_DRIVER_VERSION_MISMATCH;
386 }
387 }
388
389 suplibOsTerm(&g_supLibData);
390 }
391 g_cInits--;
392
393 return rc;
394}
395
396
397SUPR3DECL(int) SUPR3Init(PSUPDRVSESSION *ppSession)
398{
399 return SUPR3InitEx(true, ppSession);
400}
401
402/**
403 * Fake mode init.
404 */
405static int supInitFake(PSUPDRVSESSION *ppSession)
406{
407 Log(("SUP: Fake mode!\n"));
408 static const SUPFUNC s_aFakeFunctions[] =
409 {
410 /* name function */
411 { "SUPR0AbsIs64bit", 0 },
412 { "SUPR0Abs64bitKernelCS", 0 },
413 { "SUPR0Abs64bitKernelSS", 0 },
414 { "SUPR0Abs64bitKernelDS", 0 },
415 { "SUPR0AbsKernelCS", 8 },
416 { "SUPR0AbsKernelSS", 16 },
417 { "SUPR0AbsKernelDS", 16 },
418 { "SUPR0AbsKernelES", 16 },
419 { "SUPR0AbsKernelFS", 24 },
420 { "SUPR0AbsKernelGS", 32 },
421 { "SUPR0ComponentRegisterFactory", 0xefeefffd },
422 { "SUPR0ComponentDeregisterFactory", 0xefeefffe },
423 { "SUPR0ComponentQueryFactory", 0xefeeffff },
424 { "SUPR0ObjRegister", 0xefef0000 },
425 { "SUPR0ObjAddRef", 0xefef0001 },
426 { "SUPR0ObjAddRefEx", 0xefef0001 },
427 { "SUPR0ObjRelease", 0xefef0002 },
428 { "SUPR0ObjVerifyAccess", 0xefef0003 },
429 { "SUPR0LockMem", 0xefef0004 },
430 { "SUPR0UnlockMem", 0xefef0005 },
431 { "SUPR0ContAlloc", 0xefef0006 },
432 { "SUPR0ContFree", 0xefef0007 },
433 { "SUPR0MemAlloc", 0xefef0008 },
434 { "SUPR0MemGetPhys", 0xefef0009 },
435 { "SUPR0MemFree", 0xefef000a },
436 { "SUPR0Printf", 0xefef000b },
437 { "SUPR0GetPagingMode", 0xefef000c },
438 { "SUPR0EnableVTx", 0xefef000e },
439 { "RTMemAlloc", 0xefef000f },
440 { "RTMemAllocZ", 0xefef0010 },
441 { "RTMemFree", 0xefef0011 },
442 { "RTR0MemObjAddress", 0xefef0012 },
443 { "RTR0MemObjAddressR3", 0xefef0013 },
444 { "RTR0MemObjAllocPage", 0xefef0014 },
445 { "RTR0MemObjAllocPhysNC", 0xefef0015 },
446 { "RTR0MemObjAllocLow", 0xefef0016 },
447 { "RTR0MemObjEnterPhys", 0xefef0017 },
448 { "RTR0MemObjFree", 0xefef0018 },
449 { "RTR0MemObjGetPagePhysAddr", 0xefef0019 },
450 { "RTR0MemObjMapUser", 0xefef001a },
451 { "RTR0MemObjMapKernel", 0xefef001b },
452 { "RTR0MemObjMapKernelEx", 0xefef001c },
453 { "RTMpGetArraySize", 0xefef001c },
454 { "RTProcSelf", 0xefef001d },
455 { "RTR0ProcHandleSelf", 0xefef001e },
456 { "RTSemEventCreate", 0xefef001f },
457 { "RTSemEventSignal", 0xefef0020 },
458 { "RTSemEventWait", 0xefef0021 },
459 { "RTSemEventWaitNoResume", 0xefef0022 },
460 { "RTSemEventDestroy", 0xefef0023 },
461 { "RTSemEventMultiCreate", 0xefef0024 },
462 { "RTSemEventMultiSignal", 0xefef0025 },
463 { "RTSemEventMultiReset", 0xefef0026 },
464 { "RTSemEventMultiWait", 0xefef0027 },
465 { "RTSemEventMultiWaitNoResume", 0xefef0028 },
466 { "RTSemEventMultiDestroy", 0xefef0029 },
467 { "RTSemFastMutexCreate", 0xefef002a },
468 { "RTSemFastMutexDestroy", 0xefef002b },
469 { "RTSemFastMutexRequest", 0xefef002c },
470 { "RTSemFastMutexRelease", 0xefef002d },
471 { "RTSpinlockCreate", 0xefef002e },
472 { "RTSpinlockDestroy", 0xefef002f },
473 { "RTSpinlockAcquire", 0xefef0030 },
474 { "RTSpinlockRelease", 0xefef0031 },
475 { "RTSpinlockAcquireNoInts", 0xefef0032 },
476 { "RTSpinlockReleaseNoInts", 0xefef0033 },
477 { "RTTimeNanoTS", 0xefef0034 },
478 { "RTTimeMillieTS", 0xefef0035 },
479 { "RTTimeSystemNanoTS", 0xefef0036 },
480 { "RTTimeSystemMillieTS", 0xefef0037 },
481 { "RTThreadNativeSelf", 0xefef0038 },
482 { "RTThreadSleep", 0xefef0039 },
483 { "RTThreadYield", 0xefef003a },
484 { "RTTimerCreate", 0xefef003a },
485 { "RTTimerCreateEx", 0xefef003a },
486 { "RTTimerDestroy", 0xefef003a },
487 { "RTTimerStart", 0xefef003a },
488 { "RTTimerStop", 0xefef003a },
489 { "RTTimerChangeInterval", 0xefef003a },
490 { "RTTimerGetSystemGranularity", 0xefef003a },
491 { "RTTimerRequestSystemGranularity", 0xefef003a },
492 { "RTTimerReleaseSystemGranularity", 0xefef003a },
493 { "RTTimerCanDoHighResolution", 0xefef003a },
494 { "RTLogDefaultInstance", 0xefef003b },
495 { "RTLogRelDefaultInstance", 0xefef003c },
496 { "RTLogSetDefaultInstanceThread", 0xefef003d },
497 { "RTLogLogger", 0xefef003e },
498 { "RTLogLoggerEx", 0xefef003f },
499 { "RTLogLoggerExV", 0xefef0040 },
500 { "RTAssertMsg1", 0xefef0041 },
501 { "RTAssertMsg2", 0xefef0042 },
502 { "RTAssertMsg2V", 0xefef0043 },
503 { "SUPR0QueryVTCaps", 0xefef0044 },
504 };
505
506 /* fake r0 functions. */
507 g_pSupFunctions = (PSUPQUERYFUNCS)RTMemAllocZ(SUP_IOCTL_QUERY_FUNCS_SIZE(RT_ELEMENTS(s_aFakeFunctions)));
508 if (g_pSupFunctions)
509 {
510 g_pSupFunctions->u.Out.cFunctions = RT_ELEMENTS(s_aFakeFunctions);
511 memcpy(&g_pSupFunctions->u.Out.aFunctions[0], &s_aFakeFunctions[0], sizeof(s_aFakeFunctions));
512 g_pSession = (PSUPDRVSESSION)(void *)g_pSupFunctions;
513 if (ppSession)
514 *ppSession = g_pSession;
515
516 /* fake the GIP. */
517 g_pSUPGlobalInfoPage = (PSUPGLOBALINFOPAGE)RTMemPageAllocZ(PAGE_SIZE);
518 if (g_pSUPGlobalInfoPage)
519 {
520 g_pSUPGlobalInfoPageR0 = g_pSUPGlobalInfoPage;
521 g_HCPhysSUPGlobalInfoPage = NIL_RTHCPHYS & ~(RTHCPHYS)PAGE_OFFSET_MASK;
522 /* the page is supposed to be invalid, so don't set the magic. */
523 return VINF_SUCCESS;
524 }
525
526 RTMemFree(g_pSupFunctions);
527 g_pSupFunctions = NULL;
528 }
529 return VERR_NO_MEMORY;
530}
531
532
533SUPR3DECL(int) SUPR3Term(bool fForced)
534{
535 /*
536 * Verify state.
537 */
538 AssertMsg(g_cInits > 0, ("SUPR3Term() is called before SUPR3Init()!\n"));
539 if (g_cInits == 0)
540 return VERR_WRONG_ORDER;
541 if (g_cInits == 1 || fForced)
542 {
543 /*
544 * NULL the GIP pointer.
545 */
546 if (g_pSUPGlobalInfoPage)
547 {
548 ASMAtomicWriteNullPtr((void * volatile *)&g_pSUPGlobalInfoPage);
549 ASMAtomicWriteNullPtr((void * volatile *)&g_pSUPGlobalInfoPageR0);
550 ASMAtomicWriteSize(&g_HCPhysSUPGlobalInfoPage, NIL_RTHCPHYS);
551 /* just a little safe guard against threads using the page. */
552 RTThreadSleep(50);
553 }
554
555 /*
556 * Close the support driver.
557 */
558 int rc = suplibOsTerm(&g_supLibData);
559 if (rc)
560 return rc;
561
562 g_u32Cookie = 0;
563 g_u32SessionCookie = 0;
564 g_cInits = 0;
565 }
566 else
567 g_cInits--;
568
569 return 0;
570}
571
572
573SUPR3DECL(SUPPAGINGMODE) SUPR3GetPagingMode(void)
574{
575 /* fake */
576 if (RT_UNLIKELY(g_uSupFakeMode))
577#ifdef RT_ARCH_AMD64
578 return SUPPAGINGMODE_AMD64_GLOBAL_NX;
579#else
580 return SUPPAGINGMODE_32_BIT_GLOBAL;
581#endif
582
583 /*
584 * Issue IOCtl to the SUPDRV kernel module.
585 */
586 SUPGETPAGINGMODE Req;
587 Req.Hdr.u32Cookie = g_u32Cookie;
588 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
589 Req.Hdr.cbIn = SUP_IOCTL_GET_PAGING_MODE_SIZE_IN;
590 Req.Hdr.cbOut = SUP_IOCTL_GET_PAGING_MODE_SIZE_OUT;
591 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
592 Req.Hdr.rc = VERR_INTERNAL_ERROR;
593 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_GET_PAGING_MODE, &Req, SUP_IOCTL_GET_PAGING_MODE_SIZE);
594 if ( RT_FAILURE(rc)
595 || RT_FAILURE(Req.Hdr.rc))
596 {
597 LogRel(("SUPR3GetPagingMode: %Rrc %Rrc\n", rc, Req.Hdr.rc));
598 Req.u.Out.enmMode = SUPPAGINGMODE_INVALID;
599 }
600
601 return Req.u.Out.enmMode;
602}
603
604
605/**
606 * For later.
607 */
608static int supCallVMMR0ExFake(PVMR0 pVMR0, unsigned uOperation, uint64_t u64Arg, PSUPVMMR0REQHDR pReqHdr)
609{
610 AssertMsgFailed(("%d\n", uOperation)); NOREF(pVMR0); NOREF(uOperation); NOREF(u64Arg); NOREF(pReqHdr);
611 return VERR_NOT_SUPPORTED;
612}
613
614
615SUPR3DECL(int) SUPR3CallVMMR0Fast(PVMR0 pVMR0, unsigned uOperation, VMCPUID idCpu)
616{
617 NOREF(pVMR0);
618 if (RT_LIKELY(uOperation == SUP_VMMR0_DO_RAW_RUN))
619 return suplibOsIOCtlFast(&g_supLibData, SUP_IOCTL_FAST_DO_RAW_RUN, idCpu);
620 if (RT_LIKELY(uOperation == SUP_VMMR0_DO_HM_RUN))
621 return suplibOsIOCtlFast(&g_supLibData, SUP_IOCTL_FAST_DO_HM_RUN, idCpu);
622 if (RT_LIKELY(uOperation == SUP_VMMR0_DO_NOP))
623 return suplibOsIOCtlFast(&g_supLibData, SUP_IOCTL_FAST_DO_NOP, idCpu);
624
625 AssertMsgFailed(("%#x\n", uOperation));
626 return VERR_INTERNAL_ERROR;
627}
628
629
630SUPR3DECL(int) SUPR3CallVMMR0Ex(PVMR0 pVMR0, VMCPUID idCpu, unsigned uOperation, uint64_t u64Arg, PSUPVMMR0REQHDR pReqHdr)
631{
632 /*
633 * The following operations don't belong here.
634 */
635 AssertMsgReturn( uOperation != SUP_VMMR0_DO_RAW_RUN
636 && uOperation != SUP_VMMR0_DO_HM_RUN
637 && uOperation != SUP_VMMR0_DO_NOP,
638 ("%#x\n", uOperation),
639 VERR_INTERNAL_ERROR);
640
641 /* fake */
642 if (RT_UNLIKELY(g_uSupFakeMode))
643 return supCallVMMR0ExFake(pVMR0, uOperation, u64Arg, pReqHdr);
644
645 int rc;
646 if (!pReqHdr)
647 {
648 /* no data. */
649 SUPCALLVMMR0 Req;
650 Req.Hdr.u32Cookie = g_u32Cookie;
651 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
652 Req.Hdr.cbIn = SUP_IOCTL_CALL_VMMR0_SIZE_IN(0);
653 Req.Hdr.cbOut = SUP_IOCTL_CALL_VMMR0_SIZE_OUT(0);
654 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
655 Req.Hdr.rc = VERR_INTERNAL_ERROR;
656 Req.u.In.pVMR0 = pVMR0;
657 Req.u.In.idCpu = idCpu;
658 Req.u.In.uOperation = uOperation;
659 Req.u.In.u64Arg = u64Arg;
660 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_VMMR0(0), &Req, SUP_IOCTL_CALL_VMMR0_SIZE(0));
661 if (RT_SUCCESS(rc))
662 rc = Req.Hdr.rc;
663 }
664 else if (SUP_IOCTL_CALL_VMMR0_SIZE(pReqHdr->cbReq) < _4K) /* FreeBSD won't copy more than 4K. */
665 {
666 AssertPtrReturn(pReqHdr, VERR_INVALID_POINTER);
667 AssertReturn(pReqHdr->u32Magic == SUPVMMR0REQHDR_MAGIC, VERR_INVALID_MAGIC);
668 const size_t cbReq = pReqHdr->cbReq;
669
670 PSUPCALLVMMR0 pReq = (PSUPCALLVMMR0)alloca(SUP_IOCTL_CALL_VMMR0_SIZE(cbReq));
671 pReq->Hdr.u32Cookie = g_u32Cookie;
672 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
673 pReq->Hdr.cbIn = SUP_IOCTL_CALL_VMMR0_SIZE_IN(cbReq);
674 pReq->Hdr.cbOut = SUP_IOCTL_CALL_VMMR0_SIZE_OUT(cbReq);
675 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
676 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
677 pReq->u.In.pVMR0 = pVMR0;
678 pReq->u.In.idCpu = idCpu;
679 pReq->u.In.uOperation = uOperation;
680 pReq->u.In.u64Arg = u64Arg;
681 memcpy(&pReq->abReqPkt[0], pReqHdr, cbReq);
682 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_VMMR0(cbReq), pReq, SUP_IOCTL_CALL_VMMR0_SIZE(cbReq));
683 if (RT_SUCCESS(rc))
684 rc = pReq->Hdr.rc;
685 memcpy(pReqHdr, &pReq->abReqPkt[0], cbReq);
686 }
687 else if (pReqHdr->cbReq <= _512K)
688 {
689 AssertPtrReturn(pReqHdr, VERR_INVALID_POINTER);
690 AssertReturn(pReqHdr->u32Magic == SUPVMMR0REQHDR_MAGIC, VERR_INVALID_MAGIC);
691 const size_t cbReq = pReqHdr->cbReq;
692
693 PSUPCALLVMMR0 pReq = (PSUPCALLVMMR0)RTMemTmpAlloc(SUP_IOCTL_CALL_VMMR0_BIG_SIZE(cbReq));
694 pReq->Hdr.u32Cookie = g_u32Cookie;
695 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
696 pReq->Hdr.cbIn = SUP_IOCTL_CALL_VMMR0_BIG_SIZE_IN(cbReq);
697 pReq->Hdr.cbOut = SUP_IOCTL_CALL_VMMR0_BIG_SIZE_OUT(cbReq);
698 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
699 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
700 pReq->u.In.pVMR0 = pVMR0;
701 pReq->u.In.idCpu = idCpu;
702 pReq->u.In.uOperation = uOperation;
703 pReq->u.In.u64Arg = u64Arg;
704 memcpy(&pReq->abReqPkt[0], pReqHdr, cbReq);
705 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_VMMR0_BIG, pReq, SUP_IOCTL_CALL_VMMR0_BIG_SIZE(cbReq));
706 if (RT_SUCCESS(rc))
707 rc = pReq->Hdr.rc;
708 memcpy(pReqHdr, &pReq->abReqPkt[0], cbReq);
709 RTMemTmpFree(pReq);
710 }
711 else
712 AssertMsgFailedReturn(("cbReq=%#x\n", pReqHdr->cbReq), VERR_OUT_OF_RANGE);
713 return rc;
714}
715
716
717SUPR3DECL(int) SUPR3CallVMMR0(PVMR0 pVMR0, VMCPUID idCpu, unsigned uOperation, void *pvArg)
718{
719 /*
720 * The following operations don't belong here.
721 */
722 AssertMsgReturn( uOperation != SUP_VMMR0_DO_RAW_RUN
723 && uOperation != SUP_VMMR0_DO_HM_RUN
724 && uOperation != SUP_VMMR0_DO_NOP,
725 ("%#x\n", uOperation),
726 VERR_INTERNAL_ERROR);
727 return SUPR3CallVMMR0Ex(pVMR0, idCpu, uOperation, (uintptr_t)pvArg, NULL);
728}
729
730
731SUPR3DECL(int) SUPR3SetVMForFastIOCtl(PVMR0 pVMR0)
732{
733 if (RT_UNLIKELY(g_uSupFakeMode))
734 return VINF_SUCCESS;
735
736 SUPSETVMFORFAST Req;
737 Req.Hdr.u32Cookie = g_u32Cookie;
738 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
739 Req.Hdr.cbIn = SUP_IOCTL_SET_VM_FOR_FAST_SIZE_IN;
740 Req.Hdr.cbOut = SUP_IOCTL_SET_VM_FOR_FAST_SIZE_OUT;
741 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
742 Req.Hdr.rc = VERR_INTERNAL_ERROR;
743 Req.u.In.pVMR0 = pVMR0;
744 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_SET_VM_FOR_FAST, &Req, SUP_IOCTL_SET_VM_FOR_FAST_SIZE);
745 if (RT_SUCCESS(rc))
746 rc = Req.Hdr.rc;
747 return rc;
748}
749
750
751SUPR3DECL(int) SUPR3CallR0Service(const char *pszService, size_t cchService, uint32_t uOperation, uint64_t u64Arg, PSUPR0SERVICEREQHDR pReqHdr)
752{
753 AssertReturn(cchService < RT_SIZEOFMEMB(SUPCALLSERVICE, u.In.szName), VERR_INVALID_PARAMETER);
754 Assert(strlen(pszService) == cchService);
755
756 /* fake */
757 if (RT_UNLIKELY(g_uSupFakeMode))
758 return VERR_NOT_SUPPORTED;
759
760 int rc;
761 if (!pReqHdr)
762 {
763 /* no data. */
764 SUPCALLSERVICE Req;
765 Req.Hdr.u32Cookie = g_u32Cookie;
766 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
767 Req.Hdr.cbIn = SUP_IOCTL_CALL_SERVICE_SIZE_IN(0);
768 Req.Hdr.cbOut = SUP_IOCTL_CALL_SERVICE_SIZE_OUT(0);
769 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
770 Req.Hdr.rc = VERR_INTERNAL_ERROR;
771 memcpy(Req.u.In.szName, pszService, cchService);
772 Req.u.In.szName[cchService] = '\0';
773 Req.u.In.uOperation = uOperation;
774 Req.u.In.u64Arg = u64Arg;
775 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_SERVICE(0), &Req, SUP_IOCTL_CALL_SERVICE_SIZE(0));
776 if (RT_SUCCESS(rc))
777 rc = Req.Hdr.rc;
778 }
779 else if (SUP_IOCTL_CALL_SERVICE_SIZE(pReqHdr->cbReq) < _4K) /* FreeBSD won't copy more than 4K. */
780 {
781 AssertPtrReturn(pReqHdr, VERR_INVALID_POINTER);
782 AssertReturn(pReqHdr->u32Magic == SUPR0SERVICEREQHDR_MAGIC, VERR_INVALID_MAGIC);
783 const size_t cbReq = pReqHdr->cbReq;
784
785 PSUPCALLSERVICE pReq = (PSUPCALLSERVICE)alloca(SUP_IOCTL_CALL_SERVICE_SIZE(cbReq));
786 pReq->Hdr.u32Cookie = g_u32Cookie;
787 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
788 pReq->Hdr.cbIn = SUP_IOCTL_CALL_SERVICE_SIZE_IN(cbReq);
789 pReq->Hdr.cbOut = SUP_IOCTL_CALL_SERVICE_SIZE_OUT(cbReq);
790 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
791 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
792 memcpy(pReq->u.In.szName, pszService, cchService);
793 pReq->u.In.szName[cchService] = '\0';
794 pReq->u.In.uOperation = uOperation;
795 pReq->u.In.u64Arg = u64Arg;
796 memcpy(&pReq->abReqPkt[0], pReqHdr, cbReq);
797 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_SERVICE(cbReq), pReq, SUP_IOCTL_CALL_SERVICE_SIZE(cbReq));
798 if (RT_SUCCESS(rc))
799 rc = pReq->Hdr.rc;
800 memcpy(pReqHdr, &pReq->abReqPkt[0], cbReq);
801 }
802 else /** @todo may have to remove the size limits one this request... */
803 AssertMsgFailedReturn(("cbReq=%#x\n", pReqHdr->cbReq), VERR_INTERNAL_ERROR);
804 return rc;
805}
806
807
808/**
809 * Worker for the SUPR3Logger* APIs.
810 *
811 * @returns VBox status code.
812 * @param enmWhich Which logger.
813 * @param fWhat What to do with the logger.
814 * @param pszFlags The flags settings.
815 * @param pszGroups The groups settings.
816 * @param pszDest The destination specificier.
817 */
818static int supR3LoggerSettings(SUPLOGGER enmWhich, uint32_t fWhat, const char *pszFlags, const char *pszGroups, const char *pszDest)
819{
820 uint32_t const cchFlags = pszFlags ? (uint32_t)strlen(pszFlags) : 0;
821 uint32_t const cchGroups = pszGroups ? (uint32_t)strlen(pszGroups) : 0;
822 uint32_t const cchDest = pszDest ? (uint32_t)strlen(pszDest) : 0;
823 uint32_t const cbStrTab = cchFlags + !!cchFlags
824 + cchGroups + !!cchGroups
825 + cchDest + !!cchDest
826 + (!cchFlags && !cchGroups && !cchDest);
827
828 PSUPLOGGERSETTINGS pReq = (PSUPLOGGERSETTINGS)alloca(SUP_IOCTL_LOGGER_SETTINGS_SIZE(cbStrTab));
829 pReq->Hdr.u32Cookie = g_u32Cookie;
830 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
831 pReq->Hdr.cbIn = SUP_IOCTL_LOGGER_SETTINGS_SIZE_IN(cbStrTab);
832 pReq->Hdr.cbOut = SUP_IOCTL_LOGGER_SETTINGS_SIZE_OUT;
833 pReq->Hdr.fFlags= SUPREQHDR_FLAGS_DEFAULT;
834 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
835 switch (enmWhich)
836 {
837 case SUPLOGGER_DEBUG: pReq->u.In.fWhich = SUPLOGGERSETTINGS_WHICH_DEBUG; break;
838 case SUPLOGGER_RELEASE: pReq->u.In.fWhich = SUPLOGGERSETTINGS_WHICH_RELEASE; break;
839 default:
840 return VERR_INVALID_PARAMETER;
841 }
842 pReq->u.In.fWhat = fWhat;
843
844 uint32_t off = 0;
845 if (cchFlags)
846 {
847 pReq->u.In.offFlags = off;
848 memcpy(&pReq->u.In.szStrings[off], pszFlags, cchFlags + 1);
849 off += cchFlags + 1;
850 }
851 else
852 pReq->u.In.offFlags = cbStrTab - 1;
853
854 if (cchGroups)
855 {
856 pReq->u.In.offGroups = off;
857 memcpy(&pReq->u.In.szStrings[off], pszGroups, cchGroups + 1);
858 off += cchGroups + 1;
859 }
860 else
861 pReq->u.In.offGroups = cbStrTab - 1;
862
863 if (cchDest)
864 {
865 pReq->u.In.offDestination = off;
866 memcpy(&pReq->u.In.szStrings[off], pszDest, cchDest + 1);
867 off += cchDest + 1;
868 }
869 else
870 pReq->u.In.offDestination = cbStrTab - 1;
871
872 if (!off)
873 {
874 pReq->u.In.szStrings[0] = '\0';
875 off++;
876 }
877 Assert(off == cbStrTab);
878 Assert(pReq->u.In.szStrings[cbStrTab - 1] == '\0');
879
880
881 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LOGGER_SETTINGS(cbStrTab), pReq, SUP_IOCTL_LOGGER_SETTINGS_SIZE(cbStrTab));
882 if (RT_SUCCESS(rc))
883 rc = pReq->Hdr.rc;
884 return rc;
885}
886
887
888SUPR3DECL(int) SUPR3LoggerSettings(SUPLOGGER enmWhich, const char *pszFlags, const char *pszGroups, const char *pszDest)
889{
890 return supR3LoggerSettings(enmWhich, SUPLOGGERSETTINGS_WHAT_SETTINGS, pszFlags, pszGroups, pszDest);
891}
892
893
894SUPR3DECL(int) SUPR3LoggerCreate(SUPLOGGER enmWhich, const char *pszFlags, const char *pszGroups, const char *pszDest)
895{
896 return supR3LoggerSettings(enmWhich, SUPLOGGERSETTINGS_WHAT_CREATE, pszFlags, pszGroups, pszDest);
897}
898
899
900SUPR3DECL(int) SUPR3LoggerDestroy(SUPLOGGER enmWhich)
901{
902 return supR3LoggerSettings(enmWhich, SUPLOGGERSETTINGS_WHAT_DESTROY, NULL, NULL, NULL);
903}
904
905
906SUPR3DECL(int) SUPR3PageAlloc(size_t cPages, void **ppvPages)
907{
908 /*
909 * Validate.
910 */
911 AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
912 *ppvPages = NULL;
913 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
914
915 /*
916 * Call OS specific worker.
917 */
918 return suplibOsPageAlloc(&g_supLibData, cPages, ppvPages);
919}
920
921
922SUPR3DECL(int) SUPR3PageFree(void *pvPages, size_t cPages)
923{
924 /*
925 * Validate.
926 */
927 AssertPtrReturn(pvPages, VERR_INVALID_POINTER);
928 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
929
930 /*
931 * Call OS specific worker.
932 */
933 return suplibOsPageFree(&g_supLibData, pvPages, cPages);
934}
935
936
937/**
938 * Locks down the physical memory backing a virtual memory
939 * range in the current process.
940 *
941 * @returns VBox status code.
942 * @param pvStart Start of virtual memory range.
943 * Must be page aligned.
944 * @param cPages Number of pages.
945 * @param paPages Where to store the physical page addresses returned.
946 * On entry this will point to an array of with cbMemory >> PAGE_SHIFT entries.
947 */
948SUPR3DECL(int) supR3PageLock(void *pvStart, size_t cPages, PSUPPAGE paPages)
949{
950 /*
951 * Validate.
952 */
953 AssertPtr(pvStart);
954 AssertMsg(RT_ALIGN_P(pvStart, PAGE_SIZE) == pvStart, ("pvStart (%p) must be page aligned\n", pvStart));
955 AssertPtr(paPages);
956
957 /* fake */
958 if (RT_UNLIKELY(g_uSupFakeMode))
959 {
960 RTHCPHYS Phys = (uintptr_t)pvStart + PAGE_SIZE * 1024;
961 size_t iPage = cPages;
962 while (iPage-- > 0)
963 paPages[iPage].Phys = Phys + (iPage << PAGE_SHIFT);
964 return VINF_SUCCESS;
965 }
966
967 /*
968 * Issue IOCtl to the SUPDRV kernel module.
969 */
970 int rc;
971 PSUPPAGELOCK pReq = (PSUPPAGELOCK)RTMemTmpAllocZ(SUP_IOCTL_PAGE_LOCK_SIZE(cPages));
972 if (RT_LIKELY(pReq))
973 {
974 pReq->Hdr.u32Cookie = g_u32Cookie;
975 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
976 pReq->Hdr.cbIn = SUP_IOCTL_PAGE_LOCK_SIZE_IN;
977 pReq->Hdr.cbOut = SUP_IOCTL_PAGE_LOCK_SIZE_OUT(cPages);
978 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
979 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
980 pReq->u.In.pvR3 = pvStart;
981 pReq->u.In.cPages = (uint32_t)cPages; AssertRelease(pReq->u.In.cPages == cPages);
982 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_LOCK, pReq, SUP_IOCTL_PAGE_LOCK_SIZE(cPages));
983 if (RT_SUCCESS(rc))
984 rc = pReq->Hdr.rc;
985 if (RT_SUCCESS(rc))
986 {
987 for (uint32_t iPage = 0; iPage < cPages; iPage++)
988 {
989 paPages[iPage].uReserved = 0;
990 paPages[iPage].Phys = pReq->u.Out.aPages[iPage];
991 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
992 }
993 }
994 RTMemTmpFree(pReq);
995 }
996 else
997 rc = VERR_NO_TMP_MEMORY;
998
999 return rc;
1000}
1001
1002
1003/**
1004 * Releases locked down pages.
1005 *
1006 * @returns VBox status code.
1007 * @param pvStart Start of virtual memory range previously locked
1008 * down by SUPPageLock().
1009 */
1010SUPR3DECL(int) supR3PageUnlock(void *pvStart)
1011{
1012 /*
1013 * Validate.
1014 */
1015 AssertPtr(pvStart);
1016 AssertMsg(RT_ALIGN_P(pvStart, PAGE_SIZE) == pvStart, ("pvStart (%p) must be page aligned\n", pvStart));
1017
1018 /* fake */
1019 if (RT_UNLIKELY(g_uSupFakeMode))
1020 return VINF_SUCCESS;
1021
1022 /*
1023 * Issue IOCtl to the SUPDRV kernel module.
1024 */
1025 SUPPAGEUNLOCK Req;
1026 Req.Hdr.u32Cookie = g_u32Cookie;
1027 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1028 Req.Hdr.cbIn = SUP_IOCTL_PAGE_UNLOCK_SIZE_IN;
1029 Req.Hdr.cbOut = SUP_IOCTL_PAGE_UNLOCK_SIZE_OUT;
1030 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1031 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1032 Req.u.In.pvR3 = pvStart;
1033 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_UNLOCK, &Req, SUP_IOCTL_PAGE_UNLOCK_SIZE);
1034 if (RT_SUCCESS(rc))
1035 rc = Req.Hdr.rc;
1036 return rc;
1037}
1038
1039
1040/**
1041 * Fallback for SUPR3PageAllocEx on systems where RTR0MemObjPhysAllocNC isn't
1042 * supported.
1043 */
1044static int supPagePageAllocNoKernelFallback(size_t cPages, void **ppvPages, PSUPPAGE paPages)
1045{
1046 int rc = suplibOsPageAlloc(&g_supLibData, cPages, ppvPages);
1047 if (RT_SUCCESS(rc))
1048 {
1049 if (!paPages)
1050 paPages = (PSUPPAGE)alloca(sizeof(paPages[0]) * cPages);
1051 rc = supR3PageLock(*ppvPages, cPages, paPages);
1052 if (RT_FAILURE(rc))
1053 suplibOsPageFree(&g_supLibData, *ppvPages, cPages);
1054 }
1055 return rc;
1056}
1057
1058
1059SUPR3DECL(int) SUPR3PageAllocEx(size_t cPages, uint32_t fFlags, void **ppvPages, PRTR0PTR pR0Ptr, PSUPPAGE paPages)
1060{
1061 /*
1062 * Validate.
1063 */
1064 AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
1065 *ppvPages = NULL;
1066 AssertPtrNullReturn(pR0Ptr, VERR_INVALID_POINTER);
1067 if (pR0Ptr)
1068 *pR0Ptr = NIL_RTR0PTR;
1069 AssertPtrNullReturn(paPages, VERR_INVALID_POINTER);
1070 AssertMsgReturn(cPages > 0 && cPages <= VBOX_MAX_ALLOC_PAGE_COUNT, ("cPages=%zu\n", cPages), VERR_PAGE_COUNT_OUT_OF_RANGE);
1071 AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
1072
1073 /* fake */
1074 if (RT_UNLIKELY(g_uSupFakeMode))
1075 {
1076 void *pv = RTMemPageAllocZ(cPages * PAGE_SIZE);
1077 if (!pv)
1078 return VERR_NO_MEMORY;
1079 *ppvPages = pv;
1080 if (pR0Ptr)
1081 *pR0Ptr = (RTR0PTR)pv;
1082 if (paPages)
1083 for (size_t iPage = 0; iPage < cPages; iPage++)
1084 {
1085 paPages[iPage].uReserved = 0;
1086 paPages[iPage].Phys = (iPage + 4321) << PAGE_SHIFT;
1087 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
1088 }
1089 return VINF_SUCCESS;
1090 }
1091
1092 /*
1093 * Use fallback for non-R0 mapping?
1094 */
1095 if ( !pR0Ptr
1096 && !g_fSupportsPageAllocNoKernel)
1097 return supPagePageAllocNoKernelFallback(cPages, ppvPages, paPages);
1098
1099 /*
1100 * Issue IOCtl to the SUPDRV kernel module.
1101 */
1102 int rc;
1103 PSUPPAGEALLOCEX pReq = (PSUPPAGEALLOCEX)RTMemTmpAllocZ(SUP_IOCTL_PAGE_ALLOC_EX_SIZE(cPages));
1104 if (pReq)
1105 {
1106 pReq->Hdr.u32Cookie = g_u32Cookie;
1107 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
1108 pReq->Hdr.cbIn = SUP_IOCTL_PAGE_ALLOC_EX_SIZE_IN;
1109 pReq->Hdr.cbOut = SUP_IOCTL_PAGE_ALLOC_EX_SIZE_OUT(cPages);
1110 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
1111 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
1112 pReq->u.In.cPages = (uint32_t)cPages; AssertRelease(pReq->u.In.cPages == cPages);
1113 pReq->u.In.fKernelMapping = pR0Ptr != NULL;
1114 pReq->u.In.fUserMapping = true;
1115 pReq->u.In.fReserved0 = false;
1116 pReq->u.In.fReserved1 = false;
1117 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_ALLOC_EX, pReq, SUP_IOCTL_PAGE_ALLOC_EX_SIZE(cPages));
1118 if (RT_SUCCESS(rc))
1119 {
1120 rc = pReq->Hdr.rc;
1121 if (RT_SUCCESS(rc))
1122 {
1123 *ppvPages = pReq->u.Out.pvR3;
1124 if (pR0Ptr)
1125 *pR0Ptr = pReq->u.Out.pvR0;
1126 if (paPages)
1127 for (size_t iPage = 0; iPage < cPages; iPage++)
1128 {
1129 paPages[iPage].uReserved = 0;
1130 paPages[iPage].Phys = pReq->u.Out.aPages[iPage];
1131 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
1132 }
1133#ifdef RT_OS_DARWIN /* HACK ALERT! */
1134 supR3TouchPages(pReq->u.Out.pvR3, cPages);
1135#endif
1136 }
1137 else if ( rc == VERR_NOT_SUPPORTED
1138 && !pR0Ptr)
1139 {
1140 g_fSupportsPageAllocNoKernel = false;
1141 rc = supPagePageAllocNoKernelFallback(cPages, ppvPages, paPages);
1142 }
1143 }
1144
1145 RTMemTmpFree(pReq);
1146 }
1147 else
1148 rc = VERR_NO_TMP_MEMORY;
1149 return rc;
1150
1151}
1152
1153
1154SUPR3DECL(int) SUPR3PageMapKernel(void *pvR3, uint32_t off, uint32_t cb, uint32_t fFlags, PRTR0PTR pR0Ptr)
1155{
1156 /*
1157 * Validate.
1158 */
1159 AssertPtrReturn(pvR3, VERR_INVALID_POINTER);
1160 AssertPtrReturn(pR0Ptr, VERR_INVALID_POINTER);
1161 Assert(!(off & PAGE_OFFSET_MASK));
1162 Assert(!(cb & PAGE_OFFSET_MASK) && cb);
1163 Assert(!fFlags);
1164 *pR0Ptr = NIL_RTR0PTR;
1165
1166 /* fake */
1167 if (RT_UNLIKELY(g_uSupFakeMode))
1168 return VERR_NOT_SUPPORTED;
1169
1170 /*
1171 * Issue IOCtl to the SUPDRV kernel module.
1172 */
1173 SUPPAGEMAPKERNEL Req;
1174 Req.Hdr.u32Cookie = g_u32Cookie;
1175 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1176 Req.Hdr.cbIn = SUP_IOCTL_PAGE_MAP_KERNEL_SIZE_IN;
1177 Req.Hdr.cbOut = SUP_IOCTL_PAGE_MAP_KERNEL_SIZE_OUT;
1178 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1179 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1180 Req.u.In.pvR3 = pvR3;
1181 Req.u.In.offSub = off;
1182 Req.u.In.cbSub = cb;
1183 Req.u.In.fFlags = fFlags;
1184 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_MAP_KERNEL, &Req, SUP_IOCTL_PAGE_MAP_KERNEL_SIZE);
1185 if (RT_SUCCESS(rc))
1186 rc = Req.Hdr.rc;
1187 if (RT_SUCCESS(rc))
1188 *pR0Ptr = Req.u.Out.pvR0;
1189 return rc;
1190}
1191
1192
1193SUPR3DECL(int) SUPR3PageProtect(void *pvR3, RTR0PTR R0Ptr, uint32_t off, uint32_t cb, uint32_t fProt)
1194{
1195 /*
1196 * Validate.
1197 */
1198 AssertPtrReturn(pvR3, VERR_INVALID_POINTER);
1199 Assert(!(off & PAGE_OFFSET_MASK));
1200 Assert(!(cb & PAGE_OFFSET_MASK) && cb);
1201 AssertReturn(!(fProt & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC)), VERR_INVALID_PARAMETER);
1202
1203 /* fake */
1204 if (RT_UNLIKELY(g_uSupFakeMode))
1205 return RTMemProtect((uint8_t *)pvR3 + off, cb, fProt);
1206
1207 /*
1208 * Some OSes can do this from ring-3, so try that before we
1209 * issue the IOCtl to the SUPDRV kernel module.
1210 * (Yea, this isn't very nice, but just try get the job done for now.)
1211 */
1212#if !defined(RT_OS_SOLARIS)
1213 RTMemProtect((uint8_t *)pvR3 + off, cb, fProt);
1214#endif
1215
1216 SUPPAGEPROTECT Req;
1217 Req.Hdr.u32Cookie = g_u32Cookie;
1218 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1219 Req.Hdr.cbIn = SUP_IOCTL_PAGE_PROTECT_SIZE_IN;
1220 Req.Hdr.cbOut = SUP_IOCTL_PAGE_PROTECT_SIZE_OUT;
1221 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1222 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1223 Req.u.In.pvR3 = pvR3;
1224 Req.u.In.pvR0 = R0Ptr;
1225 Req.u.In.offSub = off;
1226 Req.u.In.cbSub = cb;
1227 Req.u.In.fProt = fProt;
1228 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_PROTECT, &Req, SUP_IOCTL_PAGE_PROTECT_SIZE);
1229 if (RT_SUCCESS(rc))
1230 rc = Req.Hdr.rc;
1231 return rc;
1232}
1233
1234
1235SUPR3DECL(int) SUPR3PageFreeEx(void *pvPages, size_t cPages)
1236{
1237 /*
1238 * Validate.
1239 */
1240 AssertPtrReturn(pvPages, VERR_INVALID_POINTER);
1241 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1242
1243 /* fake */
1244 if (RT_UNLIKELY(g_uSupFakeMode))
1245 {
1246 RTMemPageFree(pvPages, cPages * PAGE_SIZE);
1247 return VINF_SUCCESS;
1248 }
1249
1250 /*
1251 * Try normal free first, then if it fails check if we're using the fallback
1252 * for the allocations without kernel mappings and attempt unlocking it.
1253 */
1254 NOREF(cPages);
1255 SUPPAGEFREE Req;
1256 Req.Hdr.u32Cookie = g_u32Cookie;
1257 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1258 Req.Hdr.cbIn = SUP_IOCTL_PAGE_FREE_SIZE_IN;
1259 Req.Hdr.cbOut = SUP_IOCTL_PAGE_FREE_SIZE_OUT;
1260 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1261 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1262 Req.u.In.pvR3 = pvPages;
1263 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_FREE, &Req, SUP_IOCTL_PAGE_FREE_SIZE);
1264 if (RT_SUCCESS(rc))
1265 {
1266 rc = Req.Hdr.rc;
1267 if ( rc == VERR_INVALID_PARAMETER
1268 && !g_fSupportsPageAllocNoKernel)
1269 {
1270 int rc2 = supR3PageUnlock(pvPages);
1271 if (RT_SUCCESS(rc2))
1272 rc = suplibOsPageFree(&g_supLibData, pvPages, cPages);
1273 }
1274 }
1275 return rc;
1276}
1277
1278
1279SUPR3DECL(void *) SUPR3ContAlloc(size_t cPages, PRTR0PTR pR0Ptr, PRTHCPHYS pHCPhys)
1280{
1281 /*
1282 * Validate.
1283 */
1284 AssertPtrReturn(pHCPhys, NULL);
1285 *pHCPhys = NIL_RTHCPHYS;
1286 AssertPtrNullReturn(pR0Ptr, NULL);
1287 if (pR0Ptr)
1288 *pR0Ptr = NIL_RTR0PTR;
1289 AssertPtrNullReturn(pHCPhys, NULL);
1290 AssertMsgReturn(cPages > 0 && cPages < 256, ("cPages=%d must be > 0 and < 256\n", cPages), NULL);
1291
1292 /* fake */
1293 if (RT_UNLIKELY(g_uSupFakeMode))
1294 {
1295 void *pv = RTMemPageAllocZ(cPages * PAGE_SIZE);
1296 if (pR0Ptr)
1297 *pR0Ptr = (RTR0PTR)pv;
1298 if (pHCPhys)
1299 *pHCPhys = (uintptr_t)pv + (PAGE_SHIFT * 1024);
1300 return pv;
1301 }
1302
1303 /*
1304 * Issue IOCtl to the SUPDRV kernel module.
1305 */
1306 SUPCONTALLOC Req;
1307 Req.Hdr.u32Cookie = g_u32Cookie;
1308 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1309 Req.Hdr.cbIn = SUP_IOCTL_CONT_ALLOC_SIZE_IN;
1310 Req.Hdr.cbOut = SUP_IOCTL_CONT_ALLOC_SIZE_OUT;
1311 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1312 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1313 Req.u.In.cPages = (uint32_t)cPages;
1314 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CONT_ALLOC, &Req, SUP_IOCTL_CONT_ALLOC_SIZE);
1315 if ( RT_SUCCESS(rc)
1316 && RT_SUCCESS(Req.Hdr.rc))
1317 {
1318 *pHCPhys = Req.u.Out.HCPhys;
1319 if (pR0Ptr)
1320 *pR0Ptr = Req.u.Out.pvR0;
1321#ifdef RT_OS_DARWIN /* HACK ALERT! */
1322 supR3TouchPages(Req.u.Out.pvR3, cPages);
1323#endif
1324 return Req.u.Out.pvR3;
1325 }
1326
1327 return NULL;
1328}
1329
1330
1331SUPR3DECL(int) SUPR3ContFree(void *pv, size_t cPages)
1332{
1333 /*
1334 * Validate.
1335 */
1336 if (!pv)
1337 return VINF_SUCCESS;
1338 AssertPtrReturn(pv, VERR_INVALID_POINTER);
1339 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1340
1341 /* fake */
1342 if (RT_UNLIKELY(g_uSupFakeMode))
1343 {
1344 RTMemPageFree(pv, cPages * PAGE_SIZE);
1345 return VINF_SUCCESS;
1346 }
1347
1348 /*
1349 * Issue IOCtl to the SUPDRV kernel module.
1350 */
1351 SUPCONTFREE Req;
1352 Req.Hdr.u32Cookie = g_u32Cookie;
1353 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1354 Req.Hdr.cbIn = SUP_IOCTL_CONT_FREE_SIZE_IN;
1355 Req.Hdr.cbOut = SUP_IOCTL_CONT_FREE_SIZE_OUT;
1356 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1357 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1358 Req.u.In.pvR3 = pv;
1359 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CONT_FREE, &Req, SUP_IOCTL_CONT_FREE_SIZE);
1360 if (RT_SUCCESS(rc))
1361 rc = Req.Hdr.rc;
1362 return rc;
1363}
1364
1365
1366SUPR3DECL(int) SUPR3LowAlloc(size_t cPages, void **ppvPages, PRTR0PTR ppvPagesR0, PSUPPAGE paPages)
1367{
1368 /*
1369 * Validate.
1370 */
1371 AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
1372 *ppvPages = NULL;
1373 AssertPtrReturn(paPages, VERR_INVALID_POINTER);
1374 AssertMsgReturn(cPages > 0 && cPages < 256, ("cPages=%d must be > 0 and < 256\n", cPages), VERR_PAGE_COUNT_OUT_OF_RANGE);
1375
1376 /* fake */
1377 if (RT_UNLIKELY(g_uSupFakeMode))
1378 {
1379 *ppvPages = RTMemPageAllocZ((size_t)cPages * PAGE_SIZE);
1380 if (!*ppvPages)
1381 return VERR_NO_LOW_MEMORY;
1382
1383 /* fake physical addresses. */
1384 RTHCPHYS Phys = (uintptr_t)*ppvPages + PAGE_SIZE * 1024;
1385 size_t iPage = cPages;
1386 while (iPage-- > 0)
1387 paPages[iPage].Phys = Phys + (iPage << PAGE_SHIFT);
1388 return VINF_SUCCESS;
1389 }
1390
1391 /*
1392 * Issue IOCtl to the SUPDRV kernel module.
1393 */
1394 int rc;
1395 PSUPLOWALLOC pReq = (PSUPLOWALLOC)RTMemTmpAllocZ(SUP_IOCTL_LOW_ALLOC_SIZE(cPages));
1396 if (pReq)
1397 {
1398 pReq->Hdr.u32Cookie = g_u32Cookie;
1399 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
1400 pReq->Hdr.cbIn = SUP_IOCTL_LOW_ALLOC_SIZE_IN;
1401 pReq->Hdr.cbOut = SUP_IOCTL_LOW_ALLOC_SIZE_OUT(cPages);
1402 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
1403 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
1404 pReq->u.In.cPages = (uint32_t)cPages; AssertRelease(pReq->u.In.cPages == cPages);
1405 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LOW_ALLOC, pReq, SUP_IOCTL_LOW_ALLOC_SIZE(cPages));
1406 if (RT_SUCCESS(rc))
1407 rc = pReq->Hdr.rc;
1408 if (RT_SUCCESS(rc))
1409 {
1410 *ppvPages = pReq->u.Out.pvR3;
1411 if (ppvPagesR0)
1412 *ppvPagesR0 = pReq->u.Out.pvR0;
1413 if (paPages)
1414 for (size_t iPage = 0; iPage < cPages; iPage++)
1415 {
1416 paPages[iPage].uReserved = 0;
1417 paPages[iPage].Phys = pReq->u.Out.aPages[iPage];
1418 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
1419 Assert(paPages[iPage].Phys <= UINT32_C(0xfffff000));
1420 }
1421#ifdef RT_OS_DARWIN /* HACK ALERT! */
1422 supR3TouchPages(pReq->u.Out.pvR3, cPages);
1423#endif
1424 }
1425 RTMemTmpFree(pReq);
1426 }
1427 else
1428 rc = VERR_NO_TMP_MEMORY;
1429
1430 return rc;
1431}
1432
1433
1434SUPR3DECL(int) SUPR3LowFree(void *pv, size_t cPages)
1435{
1436 /*
1437 * Validate.
1438 */
1439 if (!pv)
1440 return VINF_SUCCESS;
1441 AssertPtrReturn(pv, VERR_INVALID_POINTER);
1442 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1443
1444 /* fake */
1445 if (RT_UNLIKELY(g_uSupFakeMode))
1446 {
1447 RTMemPageFree(pv, cPages * PAGE_SIZE);
1448 return VINF_SUCCESS;
1449 }
1450
1451 /*
1452 * Issue IOCtl to the SUPDRV kernel module.
1453 */
1454 SUPCONTFREE Req;
1455 Req.Hdr.u32Cookie = g_u32Cookie;
1456 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1457 Req.Hdr.cbIn = SUP_IOCTL_LOW_FREE_SIZE_IN;
1458 Req.Hdr.cbOut = SUP_IOCTL_LOW_FREE_SIZE_OUT;
1459 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1460 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1461 Req.u.In.pvR3 = pv;
1462 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LOW_FREE, &Req, SUP_IOCTL_LOW_FREE_SIZE);
1463 if (RT_SUCCESS(rc))
1464 rc = Req.Hdr.rc;
1465 return rc;
1466}
1467
1468
1469SUPR3DECL(int) SUPR3HardenedVerifyFile(const char *pszFilename, const char *pszMsg, PRTFILE phFile)
1470{
1471 /*
1472 * Quick input validation.
1473 */
1474 AssertPtr(pszFilename);
1475 AssertPtr(pszMsg);
1476 AssertReturn(!phFile, VERR_NOT_IMPLEMENTED); /** @todo Implement this. The deal is that we make sure the
1477 file is the same we verified after opening it. */
1478
1479 /*
1480 * Only do the actual check in hardened builds.
1481 */
1482#ifdef VBOX_WITH_HARDENING
1483 int rc = supR3HardenedVerifyFixedFile(pszFilename, false /* fFatal */);
1484 if (RT_FAILURE(rc))
1485 LogRel(("SUPR3HardenedVerifyFile: %s: Verification of \"%s\" failed, rc=%Rrc\n", pszMsg, pszFilename, rc));
1486 return rc;
1487#else
1488 return VINF_SUCCESS;
1489#endif
1490}
1491
1492
1493SUPR3DECL(int) SUPR3HardenedVerifySelf(const char *pszArgv0, bool fInternal, PRTERRINFO pErrInfo)
1494{
1495 /*
1496 * Quick input validation.
1497 */
1498 AssertPtr(pszArgv0);
1499 RTErrInfoClear(pErrInfo);
1500
1501 /*
1502 * Get the executable image path as we need it for all the tests here.
1503 */
1504 char szExecPath[RTPATH_MAX];
1505 if (!RTProcGetExecutablePath(szExecPath, sizeof(szExecPath)))
1506 return RTErrInfoSet(pErrInfo, VERR_INTERNAL_ERROR_2, "RTProcGetExecutablePath failed");
1507
1508 int rc;
1509 if (fInternal)
1510 {
1511 /*
1512 * Internal applications must be launched directly without any PATH
1513 * searching involved.
1514 */
1515 if (RTPathCompare(pszArgv0, szExecPath) != 0)
1516 return RTErrInfoSetF(pErrInfo, VERR_SUPLIB_INVALID_ARGV0_INTERNAL,
1517 "argv[0] does not match the executable image path: '%s' != '%s'", pszArgv0, szExecPath);
1518
1519 /*
1520 * Internal applications must reside in or under the
1521 * RTPathAppPrivateArch directory.
1522 */
1523 char szAppPrivateArch[RTPATH_MAX];
1524 rc = RTPathAppPrivateArch(szAppPrivateArch, sizeof(szAppPrivateArch));
1525 if (RT_FAILURE(rc))
1526 return RTErrInfoSetF(pErrInfo, VERR_SUPLIB_INVALID_ARGV0_INTERNAL,
1527 "RTPathAppPrivateArch failed with rc=%Rrc", rc);
1528 size_t cchAppPrivateArch = strlen(szAppPrivateArch);
1529 if ( cchAppPrivateArch >= strlen(szExecPath)
1530 || !RTPATH_IS_SLASH(szExecPath[cchAppPrivateArch]))
1531 return RTErrInfoSet(pErrInfo, VERR_SUPLIB_INVALID_INTERNAL_APP_DIR,
1532 "Internal executable does reside under RTPathAppPrivateArch");
1533 szExecPath[cchAppPrivateArch] = '\0';
1534 if (RTPathCompare(szExecPath, szAppPrivateArch) != 0)
1535 return RTErrInfoSet(pErrInfo, VERR_SUPLIB_INVALID_INTERNAL_APP_DIR,
1536 "Internal executable does reside under RTPathAppPrivateArch");
1537 szExecPath[cchAppPrivateArch] = RTPATH_SLASH;
1538 }
1539
1540#ifdef VBOX_WITH_HARDENING
1541 /*
1542 * Verify that the image file and parent directories are sane.
1543 */
1544 rc = supR3HardenedVerifyFile(szExecPath, RTHCUINTPTR_MAX, pErrInfo);
1545 if (RT_FAILURE(rc))
1546 return rc;
1547#endif
1548
1549 return VINF_SUCCESS;
1550}
1551
1552
1553SUPR3DECL(int) SUPR3HardenedVerifyDir(const char *pszDirPath, bool fRecursive, bool fCheckFiles, PRTERRINFO pErrInfo)
1554{
1555 /*
1556 * Quick input validation
1557 */
1558 AssertPtr(pszDirPath);
1559 RTErrInfoClear(pErrInfo);
1560
1561 /*
1562 * Only do the actual check in hardened builds.
1563 */
1564#ifdef VBOX_WITH_HARDENING
1565 int rc = supR3HardenedVerifyDir(pszDirPath, fRecursive, fCheckFiles, pErrInfo);
1566 if (RT_FAILURE(rc) && !RTErrInfoIsSet(pErrInfo))
1567 LogRel(("supR3HardenedVerifyDir: Verification of \"%s\" failed, rc=%Rrc\n", pszDirPath, rc));
1568 return rc;
1569#else
1570 NOREF(pszDirPath); NOREF(fRecursive); NOREF(fCheckFiles);
1571 return VINF_SUCCESS;
1572#endif
1573}
1574
1575
1576SUPR3DECL(int) SUPR3HardenedVerifyPlugIn(const char *pszFilename, PRTERRINFO pErrInfo)
1577{
1578 /*
1579 * Quick input validation
1580 */
1581 AssertPtr(pszFilename);
1582 RTErrInfoClear(pErrInfo);
1583
1584 /*
1585 * Only do the actual check in hardened builds.
1586 */
1587#ifdef VBOX_WITH_HARDENING
1588 int rc = supR3HardenedVerifyFile(pszFilename, RTHCUINTPTR_MAX, pErrInfo);
1589 if (RT_FAILURE(rc) && !RTErrInfoIsSet(pErrInfo))
1590 LogRel(("supR3HardenedVerifyFile: Verification of \"%s\" failed, rc=%Rrc\n", pszFilename, rc));
1591 return rc;
1592#else
1593 return VINF_SUCCESS;
1594#endif
1595}
1596
1597
1598SUPR3DECL(int) SUPR3GipGetPhys(PRTHCPHYS pHCPhys)
1599{
1600 if (g_pSUPGlobalInfoPage)
1601 {
1602 *pHCPhys = g_HCPhysSUPGlobalInfoPage;
1603 return VINF_SUCCESS;
1604 }
1605 *pHCPhys = NIL_RTHCPHYS;
1606 return VERR_WRONG_ORDER;
1607}
1608
1609
1610SUPR3DECL(int) SUPR3QueryVTxSupported(void)
1611{
1612#ifdef RT_OS_LINUX
1613 return suplibOsQueryVTxSupported();
1614#else
1615 return VINF_SUCCESS;
1616#endif
1617}
1618
1619
1620SUPR3DECL(int) SUPR3QueryVTCaps(uint32_t *pfCaps)
1621{
1622 AssertPtrReturn(pfCaps, VERR_INVALID_POINTER);
1623
1624 *pfCaps = 0;
1625
1626 /* fake */
1627 if (RT_UNLIKELY(g_uSupFakeMode))
1628 return VINF_SUCCESS;
1629
1630 /*
1631 * Issue IOCtl to the SUPDRV kernel module.
1632 */
1633 SUPVTCAPS Req;
1634 Req.Hdr.u32Cookie = g_u32Cookie;
1635 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1636 Req.Hdr.cbIn = SUP_IOCTL_VT_CAPS_SIZE_IN;
1637 Req.Hdr.cbOut = SUP_IOCTL_VT_CAPS_SIZE_OUT;
1638 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1639 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1640 Req.u.Out.Caps = 0;
1641 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_VT_CAPS, &Req, SUP_IOCTL_VT_CAPS_SIZE);
1642 if (RT_SUCCESS(rc))
1643 {
1644 rc = Req.Hdr.rc;
1645 if (RT_SUCCESS(rc))
1646 *pfCaps = Req.u.Out.Caps;
1647 }
1648 return rc;
1649}
1650
1651
1652SUPR3DECL(int) SUPR3TracerOpen(uint32_t uCookie, uintptr_t uArg)
1653{
1654 /* fake */
1655 if (RT_UNLIKELY(g_uSupFakeMode))
1656 return VINF_SUCCESS;
1657
1658 /*
1659 * Issue IOCtl to the SUPDRV kernel module.
1660 */
1661 SUPTRACEROPEN Req;
1662 Req.Hdr.u32Cookie = g_u32Cookie;
1663 Req.Hdr.u32SessionCookie= g_u32SessionCookie;
1664 Req.Hdr.cbIn = SUP_IOCTL_TRACER_OPEN_SIZE_IN;
1665 Req.Hdr.cbOut = SUP_IOCTL_TRACER_OPEN_SIZE_OUT;
1666 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1667 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1668 Req.u.In.uCookie = uCookie;
1669 Req.u.In.uArg = uArg;
1670 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_OPEN, &Req, SUP_IOCTL_TRACER_OPEN_SIZE);
1671 if (RT_SUCCESS(rc))
1672 rc = Req.Hdr.rc;
1673 return rc;
1674}
1675
1676
1677SUPR3DECL(int) SUPR3TracerClose(void)
1678{
1679 /* fake */
1680 if (RT_UNLIKELY(g_uSupFakeMode))
1681 return VINF_SUCCESS;
1682
1683 /*
1684 * Issue IOCtl to the SUPDRV kernel module.
1685 */
1686 SUPREQHDR Req;
1687 Req.u32Cookie = g_u32Cookie;
1688 Req.u32SessionCookie= g_u32SessionCookie;
1689 Req.cbIn = SUP_IOCTL_TRACER_OPEN_SIZE_IN;
1690 Req.cbOut = SUP_IOCTL_TRACER_OPEN_SIZE_OUT;
1691 Req.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1692 Req.rc = VERR_INTERNAL_ERROR;
1693 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_CLOSE, &Req, SUP_IOCTL_TRACER_CLOSE_SIZE);
1694 if (RT_SUCCESS(rc))
1695 rc = Req.rc;
1696 return rc;
1697}
1698
1699
1700SUPR3DECL(int) SUPR3TracerIoCtl(uintptr_t uCmd, uintptr_t uArg, int32_t *piRetVal)
1701{
1702 /* fake */
1703 if (RT_UNLIKELY(g_uSupFakeMode))
1704 {
1705 *piRetVal = -1;
1706 return VERR_NOT_SUPPORTED;
1707 }
1708
1709 /*
1710 * Issue IOCtl to the SUPDRV kernel module.
1711 */
1712 SUPTRACERIOCTL Req;
1713 Req.Hdr.u32Cookie = g_u32Cookie;
1714 Req.Hdr.u32SessionCookie= g_u32SessionCookie;
1715 Req.Hdr.cbIn = SUP_IOCTL_TRACER_IOCTL_SIZE_IN;
1716 Req.Hdr.cbOut = SUP_IOCTL_TRACER_IOCTL_SIZE_OUT;
1717 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1718 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1719 Req.u.In.uCmd = uCmd;
1720 Req.u.In.uArg = uArg;
1721 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_IOCTL, &Req, SUP_IOCTL_TRACER_IOCTL_SIZE);
1722 if (RT_SUCCESS(rc))
1723 {
1724 rc = Req.Hdr.rc;
1725 *piRetVal = Req.u.Out.iRetVal;
1726 }
1727 return rc;
1728}
1729
1730
1731
1732typedef struct SUPDRVTRACERSTRTAB
1733{
1734 /** Pointer to the string table. */
1735 char *pchStrTab;
1736 /** The actual string table size. */
1737 uint32_t cbStrTab;
1738 /** The original string pointers. */
1739 RTUINTPTR apszOrgFunctions[1];
1740} SUPDRVTRACERSTRTAB, *PSUPDRVTRACERSTRTAB;
1741
1742
1743/**
1744 * Destroys a string table, restoring the original pszFunction member valus.
1745 *
1746 * @param pThis The string table structure.
1747 * @param paProbLocs The probe location array.
1748 * @param cProbLocs The number of probe locations.
1749 */
1750static void supr3TracerDestroyStrTab(PSUPDRVTRACERSTRTAB pThis, PVTGPROBELOC32 paProbeLocs32, PVTGPROBELOC64 paProbeLocs64,
1751 uint32_t cProbeLocs, bool f32Bit)
1752{
1753 /* Restore. */
1754 size_t i = cProbeLocs;
1755 if (f32Bit)
1756 while (i--)
1757 paProbeLocs32[i].pszFunction = (uint32_t)pThis->apszOrgFunctions[i];
1758 else
1759 while (i--)
1760 paProbeLocs64[i].pszFunction = pThis->apszOrgFunctions[i];
1761
1762 /* Free. */
1763 RTMemFree(pThis->pchStrTab);
1764 RTMemFree(pThis);
1765}
1766
1767
1768/**
1769 * Creates a string table for the pszFunction members in the probe location
1770 * array.
1771 *
1772 * This will save and replace the pszFunction members with offsets.
1773 *
1774 * @returns Pointer to a string table structure. NULL on failure.
1775 * @param paProbLocs The probe location array.
1776 * @param cProbLocs The number of elements in the array.
1777 * @param cBits
1778 */
1779static PSUPDRVTRACERSTRTAB supr3TracerCreateStrTab(PVTGPROBELOC32 paProbeLocs32,
1780 PVTGPROBELOC64 paProbeLocs64,
1781 uint32_t cProbeLocs,
1782 RTUINTPTR offDelta,
1783 bool f32Bit)
1784{
1785 if (cProbeLocs > _128K)
1786 return NULL;
1787
1788 /*
1789 * Allocate the string table structures.
1790 */
1791 size_t cbThis = RT_OFFSETOF(SUPDRVTRACERSTRTAB, apszOrgFunctions[cProbeLocs]);
1792 PSUPDRVTRACERSTRTAB pThis = (PSUPDRVTRACERSTRTAB)RTMemAlloc(cbThis);
1793 if (!pThis)
1794 return NULL;
1795
1796 uint32_t const cHashBits = cProbeLocs * 2 - 1;
1797 uint32_t *pbmHash = (uint32_t *)RTMemAllocZ(RT_ALIGN_32(cHashBits, 64) / 8 );
1798 if (!pbmHash)
1799 {
1800 RTMemFree(pThis);
1801 return NULL;
1802 }
1803
1804 /*
1805 * Calc the max string table size and save the orignal pointers so we can
1806 * replace them later.
1807 */
1808 size_t cbMax = 1;
1809 for (uint32_t i = 0; i < cProbeLocs; i++)
1810 {
1811 pThis->apszOrgFunctions[i] = f32Bit ? paProbeLocs32[i].pszFunction : paProbeLocs64[i].pszFunction;
1812 const char *pszFunction = (const char *)(uintptr_t)(pThis->apszOrgFunctions[i] + offDelta);
1813 size_t cch = strlen(pszFunction);
1814 if (cch > _1K)
1815 {
1816 cbMax = 0;
1817 break;
1818 }
1819 cbMax += cch + 1;
1820 }
1821
1822 /* Alloc space for it. */
1823 if (cbMax > 0)
1824 pThis->pchStrTab = (char *)RTMemAlloc(cbMax);
1825 else
1826 pThis->pchStrTab = NULL;
1827 if (!pThis->pchStrTab)
1828 {
1829 RTMemFree(pbmHash);
1830 RTMemFree(pThis);
1831 return NULL;
1832 }
1833
1834 /*
1835 * Create the string table.
1836 */
1837 uint32_t off = 0;
1838 uint32_t offPrev = 0;
1839
1840 for (uint32_t i = 0; i < cProbeLocs; i++)
1841 {
1842 const char * const psz = (const char *)(uintptr_t)(pThis->apszOrgFunctions[i] + offDelta);
1843 size_t const cch = strlen(psz);
1844 uint32_t const iHashBit = RTStrHash1(psz) % cHashBits;
1845 if (ASMBitTestAndSet(pbmHash, iHashBit))
1846 {
1847 /* Often it's the most recent string. */
1848 if ( off - offPrev < cch + 1
1849 || memcmp(&pThis->pchStrTab[offPrev], psz, cch + 1))
1850 {
1851 /* It wasn't, search the entire string table. (lazy bird) */
1852 offPrev = 0;
1853 while (offPrev < off)
1854 {
1855 size_t cchCur = strlen(&pThis->pchStrTab[offPrev]);
1856 if ( cchCur == cch
1857 && !memcmp(&pThis->pchStrTab[offPrev], psz, cch + 1))
1858 break;
1859 offPrev += (uint32_t)cchCur + 1;
1860 }
1861 }
1862 }
1863 else
1864 offPrev = off;
1865
1866 /* Add the string to the table. */
1867 if (offPrev >= off)
1868 {
1869 memcpy(&pThis->pchStrTab[off], psz, cch + 1);
1870 offPrev = off;
1871 off += (uint32_t)cch + 1;
1872 }
1873
1874 /* Update the entry */
1875 if (f32Bit)
1876 paProbeLocs32[i].pszFunction = offPrev;
1877 else
1878 paProbeLocs64[i].pszFunction = offPrev;
1879 }
1880
1881 pThis->cbStrTab = off;
1882 RTMemFree(pbmHash);
1883 return pThis;
1884}
1885
1886
1887
1888SUPR3DECL(int) SUPR3TracerRegisterModule(uintptr_t hModNative, const char *pszModule, struct VTGOBJHDR *pVtgHdr,
1889 RTUINTPTR uVtgHdrAddr, uint32_t fFlags)
1890{
1891 /* Validate input. */
1892 NOREF(hModNative);
1893 AssertPtrReturn(pVtgHdr, VERR_INVALID_POINTER);
1894 AssertReturn(!memcmp(pVtgHdr->szMagic, VTGOBJHDR_MAGIC, sizeof(pVtgHdr->szMagic)), VERR_SUPDRV_VTG_MAGIC);
1895 AssertPtrReturn(pszModule, VERR_INVALID_POINTER);
1896 size_t cchModule = strlen(pszModule);
1897 AssertReturn(cchModule < RT_SIZEOFMEMB(SUPTRACERUMODREG, u.In.szName), VERR_FILENAME_TOO_LONG);
1898 AssertReturn(!RTPathHavePath(pszModule), VERR_INVALID_PARAMETER);
1899 AssertReturn(fFlags == SUP_TRACER_UMOD_FLAGS_EXE || fFlags == SUP_TRACER_UMOD_FLAGS_SHARED, VERR_INVALID_PARAMETER);
1900
1901 /*
1902 * Set the probe location array offset and size members. If the size is
1903 * zero, don't bother ring-0 with it.
1904 */
1905 if (!pVtgHdr->offProbeLocs)
1906 {
1907 uint64_t u64Tmp = pVtgHdr->uProbeLocsEnd.u64 - pVtgHdr->uProbeLocs.u64;
1908 if (u64Tmp >= UINT32_MAX)
1909 return VERR_SUPDRV_VTG_BAD_HDR_TOO_MUCH;
1910 pVtgHdr->cbProbeLocs = (uint32_t)u64Tmp;
1911
1912 u64Tmp = pVtgHdr->uProbeLocs.u64 - uVtgHdrAddr;
1913 if ((int64_t)u64Tmp != (int32_t)u64Tmp)
1914 {
1915 LogRel(("SUPR3TracerRegisterModule: VERR_SUPDRV_VTG_BAD_HDR_PTR - u64Tmp=%#llx uProbeLocs=%#llx uVtgHdrAddr=%RTptr\n",
1916 u64Tmp, pVtgHdr->uProbeLocs.u64, uVtgHdrAddr));
1917 return VERR_SUPDRV_VTG_BAD_HDR_PTR;
1918 }
1919 pVtgHdr->offProbeLocs = (int32_t)u64Tmp;
1920 }
1921
1922 if ( !pVtgHdr->cbProbeLocs
1923 || !pVtgHdr->cbProbes)
1924 return VINF_SUCCESS;
1925
1926 /*
1927 * Fake out.
1928 */
1929 if (RT_UNLIKELY(g_uSupFakeMode))
1930 return VINF_SUCCESS;
1931
1932 /*
1933 * Create a string table for the function names in the location array.
1934 * It's somewhat easier to do that here than from ring-0.
1935 */
1936 size_t const cProbeLocs = pVtgHdr->cbProbeLocs
1937 / (pVtgHdr->cBits == 32 ? sizeof(VTGPROBELOC32) : sizeof(VTGPROBELOC64));
1938 PVTGPROBELOC paProbeLocs = (PVTGPROBELOC)((uintptr_t)pVtgHdr + pVtgHdr->offProbeLocs);
1939 PSUPDRVTRACERSTRTAB pStrTab = supr3TracerCreateStrTab((PVTGPROBELOC32)paProbeLocs,
1940 (PVTGPROBELOC64)paProbeLocs,
1941 cProbeLocs, (uintptr_t)pVtgHdr - uVtgHdrAddr,
1942 pVtgHdr->cBits == 32);
1943 if (!pStrTab)
1944 return VERR_NO_MEMORY;
1945
1946
1947 /*
1948 * Issue IOCtl to the SUPDRV kernel module.
1949 */
1950 SUPTRACERUMODREG Req;
1951 Req.Hdr.u32Cookie = g_u32Cookie;
1952 Req.Hdr.u32SessionCookie= g_u32SessionCookie;
1953 Req.Hdr.cbIn = SUP_IOCTL_TRACER_UMOD_REG_SIZE_IN;
1954 Req.Hdr.cbOut = SUP_IOCTL_TRACER_UMOD_REG_SIZE_OUT;
1955 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1956 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1957 Req.u.In.uVtgHdrAddr = uVtgHdrAddr;
1958 Req.u.In.R3PtrVtgHdr = pVtgHdr;
1959 Req.u.In.R3PtrStrTab = pStrTab->pchStrTab;
1960 Req.u.In.cbStrTab = pStrTab->cbStrTab;
1961 Req.u.In.fFlags = fFlags;
1962
1963 memcpy(Req.u.In.szName, pszModule, cchModule + 1);
1964 if (!RTPathHasSuffix(Req.u.In.szName))
1965 {
1966 /* Add the default suffix if none is given. */
1967 switch (fFlags & SUP_TRACER_UMOD_FLAGS_TYPE_MASK)
1968 {
1969#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
1970 case SUP_TRACER_UMOD_FLAGS_EXE:
1971 if (cchModule + sizeof(".exe") <= sizeof(Req.u.In.szName))
1972 strcpy(&Req.u.In.szName[cchModule], ".exe");
1973 break;
1974#endif
1975
1976 case SUP_TRACER_UMOD_FLAGS_SHARED:
1977 {
1978 const char *pszSuff = RTLdrGetSuff();
1979 size_t cchSuff = strlen(pszSuff);
1980 if (cchModule + cchSuff < sizeof(Req.u.In.szName))
1981 memcpy(&Req.u.In.szName[cchModule], pszSuff, cchSuff + 1);
1982 break;
1983 }
1984 }
1985 }
1986
1987 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_UMOD_REG, &Req, SUP_IOCTL_TRACER_UMOD_REG_SIZE);
1988 if (RT_SUCCESS(rc))
1989 rc = Req.Hdr.rc;
1990
1991 supr3TracerDestroyStrTab(pStrTab, (PVTGPROBELOC32)paProbeLocs, (PVTGPROBELOC64)paProbeLocs,
1992 cProbeLocs, pVtgHdr->cBits == 32);
1993 return rc;
1994}
1995
1996
1997SUPR3DECL(int) SUPR3TracerDeregisterModule(struct VTGOBJHDR *pVtgHdr)
1998{
1999 /* Validate input. */
2000 AssertPtrReturn(pVtgHdr, VERR_INVALID_POINTER);
2001 AssertReturn(!memcmp(pVtgHdr->szMagic, VTGOBJHDR_MAGIC, sizeof(pVtgHdr->szMagic)), VERR_SUPDRV_VTG_MAGIC);
2002
2003 /*
2004 * Don't bother if the object is empty.
2005 */
2006 if ( !pVtgHdr->cbProbeLocs
2007 || !pVtgHdr->cbProbes)
2008 return VINF_SUCCESS;
2009
2010 /*
2011 * Fake out.
2012 */
2013 if (RT_UNLIKELY(g_uSupFakeMode))
2014 return VINF_SUCCESS;
2015
2016 /*
2017 * Issue IOCtl to the SUPDRV kernel module.
2018 */
2019 SUPTRACERUMODDEREG Req;
2020 Req.Hdr.u32Cookie = g_u32Cookie;
2021 Req.Hdr.u32SessionCookie= g_u32SessionCookie;
2022 Req.Hdr.cbIn = SUP_IOCTL_TRACER_UMOD_REG_SIZE_IN;
2023 Req.Hdr.cbOut = SUP_IOCTL_TRACER_UMOD_REG_SIZE_OUT;
2024 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2025 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2026 Req.u.In.pVtgHdr = pVtgHdr;
2027
2028 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_UMOD_DEREG, &Req, SUP_IOCTL_TRACER_UMOD_DEREG_SIZE);
2029 if (RT_SUCCESS(rc))
2030 rc = Req.Hdr.rc;
2031 return rc;
2032}
2033
2034
2035DECLASM(void) suplibTracerFireProbe(PVTGPROBELOC pProbeLoc, PSUPTRACERUMODFIREPROBE pReq)
2036{
2037 pReq->Hdr.u32Cookie = g_u32Cookie;
2038 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
2039 Assert(pReq->Hdr.cbIn == SUP_IOCTL_TRACER_UMOD_FIRE_PROBE_SIZE_IN);
2040 Assert(pReq->Hdr.cbOut == SUP_IOCTL_TRACER_UMOD_FIRE_PROBE_SIZE_OUT);
2041 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2042 pReq->Hdr.rc = VINF_SUCCESS;
2043
2044 suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_UMOD_FIRE_PROBE, pReq, SUP_IOCTL_TRACER_UMOD_FIRE_PROBE_SIZE);
2045}
2046
2047
2048SUPR3DECL(int) SUPR3MsrProberRead(uint32_t uMsr, RTCPUID idCpu, uint64_t *puValue, bool *pfGp)
2049{
2050 SUPMSRPROBER Req;
2051 Req.Hdr.u32Cookie = g_u32Cookie;
2052 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
2053 Req.Hdr.cbIn = SUP_IOCTL_MSR_PROBER_SIZE_IN;
2054 Req.Hdr.cbOut = SUP_IOCTL_MSR_PROBER_SIZE_OUT;
2055 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2056 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2057
2058 Req.u.In.enmOp = SUPMSRPROBEROP_READ;
2059 Req.u.In.uMsr = uMsr;
2060 Req.u.In.idCpu = idCpu == NIL_RTCPUID ? UINT32_MAX : idCpu;
2061
2062 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_MSR_PROBER, &Req, SUP_IOCTL_MSR_PROBER_SIZE);
2063 if (RT_SUCCESS(rc))
2064 rc = Req.Hdr.rc;
2065 if (RT_SUCCESS(rc))
2066 {
2067 if (puValue)
2068 *puValue = Req.u.Out.uResults.Read.uValue;
2069 if (pfGp)
2070 *pfGp = Req.u.Out.uResults.Read.fGp;
2071 }
2072
2073 return rc;
2074}
2075
2076
2077SUPR3DECL(int) SUPR3MsrProberWrite(uint32_t uMsr, RTCPUID idCpu, uint64_t uValue, bool *pfGp)
2078{
2079 SUPMSRPROBER Req;
2080 Req.Hdr.u32Cookie = g_u32Cookie;
2081 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
2082 Req.Hdr.cbIn = SUP_IOCTL_MSR_PROBER_SIZE_IN;
2083 Req.Hdr.cbOut = SUP_IOCTL_MSR_PROBER_SIZE_OUT;
2084 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2085 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2086
2087 Req.u.In.enmOp = SUPMSRPROBEROP_WRITE;
2088 Req.u.In.uMsr = uMsr;
2089 Req.u.In.idCpu = idCpu == NIL_RTCPUID ? UINT32_MAX : idCpu;
2090 Req.u.In.uArgs.Write.uToWrite = uValue;
2091
2092 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_MSR_PROBER, &Req, SUP_IOCTL_MSR_PROBER_SIZE);
2093 if (RT_SUCCESS(rc))
2094 rc = Req.Hdr.rc;
2095 if (RT_SUCCESS(rc) && pfGp)
2096 *pfGp = Req.u.Out.uResults.Write.fGp;
2097
2098 return rc;
2099}
2100
2101
2102SUPR3DECL(int) SUPR3MsrProberModify(uint32_t uMsr, RTCPUID idCpu, uint64_t fAndMask, uint64_t fOrMask,
2103 PSUPMSRPROBERMODIFYRESULT pResult)
2104{
2105 return SUPR3MsrProberModifyEx(uMsr, idCpu, fAndMask, fOrMask, false /*fFaster*/, pResult);
2106}
2107
2108
2109SUPR3DECL(int) SUPR3MsrProberModifyEx(uint32_t uMsr, RTCPUID idCpu, uint64_t fAndMask, uint64_t fOrMask, bool fFaster,
2110 PSUPMSRPROBERMODIFYRESULT pResult)
2111{
2112 SUPMSRPROBER Req;
2113 Req.Hdr.u32Cookie = g_u32Cookie;
2114 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
2115 Req.Hdr.cbIn = SUP_IOCTL_MSR_PROBER_SIZE_IN;
2116 Req.Hdr.cbOut = SUP_IOCTL_MSR_PROBER_SIZE_OUT;
2117 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2118 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2119
2120 Req.u.In.enmOp = fFaster ? SUPMSRPROBEROP_MODIFY_FASTER : SUPMSRPROBEROP_MODIFY;
2121 Req.u.In.uMsr = uMsr;
2122 Req.u.In.idCpu = idCpu == NIL_RTCPUID ? UINT32_MAX : idCpu;
2123 Req.u.In.uArgs.Modify.fAndMask = fAndMask;
2124 Req.u.In.uArgs.Modify.fOrMask = fOrMask;
2125
2126 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_MSR_PROBER, &Req, SUP_IOCTL_MSR_PROBER_SIZE);
2127 if (RT_SUCCESS(rc))
2128 rc = Req.Hdr.rc;
2129 if (RT_SUCCESS(rc))
2130 *pResult = Req.u.Out.uResults.Modify;
2131
2132 return rc;
2133}
2134
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