VirtualBox

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

Last change on this file since 54650 was 54650, checked in by vboxsync, 10 years ago

SUPDrv/VMMR0: properly handle the CR4 shadow register on Linux >= 4.0

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 78.1 KB
Line 
1/* $Id: SUPLib.cpp 54650 2015-03-05 13:42:10Z vboxsync $ */
2/** @file
3 * VirtualBox Support Library - Common code.
4 */
5
6/*
7 * Copyright (C) 2006-2014 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 SUPINITOP enmWhat = kSupInitOp_Driver;
265 int rc = suplibOsInit(&g_supLibData, g_fPreInited, fUnrestricted, &enmWhat, NULL);
266 if (RT_SUCCESS(rc))
267 {
268 /*
269 * Negotiate the cookie.
270 */
271 SUPCOOKIE CookieReq;
272 memset(&CookieReq, 0xff, sizeof(CookieReq));
273 CookieReq.Hdr.u32Cookie = SUPCOOKIE_INITIAL_COOKIE;
274 CookieReq.Hdr.u32SessionCookie = RTRandU32();
275 CookieReq.Hdr.cbIn = SUP_IOCTL_COOKIE_SIZE_IN;
276 CookieReq.Hdr.cbOut = SUP_IOCTL_COOKIE_SIZE_OUT;
277 CookieReq.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
278 CookieReq.Hdr.rc = VERR_INTERNAL_ERROR;
279 strcpy(CookieReq.u.In.szMagic, SUPCOOKIE_MAGIC);
280 CookieReq.u.In.u32ReqVersion = SUPDRV_IOC_VERSION;
281 const uint32_t uMinVersion = (SUPDRV_IOC_VERSION & 0xffff0000) == 0x001f0000
282 ? 0x001f0004
283 : SUPDRV_IOC_VERSION & 0xffff0000;
284 CookieReq.u.In.u32MinVersion = uMinVersion;
285 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_COOKIE, &CookieReq, SUP_IOCTL_COOKIE_SIZE);
286 if ( RT_SUCCESS(rc)
287 && RT_SUCCESS(CookieReq.Hdr.rc))
288 {
289 if ( (CookieReq.u.Out.u32SessionVersion & 0xffff0000) == (SUPDRV_IOC_VERSION & 0xffff0000)
290 && CookieReq.u.Out.u32SessionVersion >= uMinVersion)
291 {
292 /*
293 * Query the functions.
294 */
295 PSUPQUERYFUNCS pFuncsReq = NULL;
296 if (g_supLibData.fUnrestricted)
297 {
298 pFuncsReq = (PSUPQUERYFUNCS)RTMemAllocZ(SUP_IOCTL_QUERY_FUNCS_SIZE(CookieReq.u.Out.cFunctions));
299 if (pFuncsReq)
300 {
301 pFuncsReq->Hdr.u32Cookie = CookieReq.u.Out.u32Cookie;
302 pFuncsReq->Hdr.u32SessionCookie = CookieReq.u.Out.u32SessionCookie;
303 pFuncsReq->Hdr.cbIn = SUP_IOCTL_QUERY_FUNCS_SIZE_IN;
304 pFuncsReq->Hdr.cbOut = SUP_IOCTL_QUERY_FUNCS_SIZE_OUT(CookieReq.u.Out.cFunctions);
305 pFuncsReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
306 pFuncsReq->Hdr.rc = VERR_INTERNAL_ERROR;
307 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_QUERY_FUNCS(CookieReq.u.Out.cFunctions), pFuncsReq,
308 SUP_IOCTL_QUERY_FUNCS_SIZE(CookieReq.u.Out.cFunctions));
309 if (RT_SUCCESS(rc))
310 rc = pFuncsReq->Hdr.rc;
311 if (RT_SUCCESS(rc))
312 {
313 /*
314 * Map the GIP into userspace.
315 */
316 Assert(!g_pSUPGlobalInfoPage);
317 SUPGIPMAP GipMapReq;
318 GipMapReq.Hdr.u32Cookie = CookieReq.u.Out.u32Cookie;
319 GipMapReq.Hdr.u32SessionCookie = CookieReq.u.Out.u32SessionCookie;
320 GipMapReq.Hdr.cbIn = SUP_IOCTL_GIP_MAP_SIZE_IN;
321 GipMapReq.Hdr.cbOut = SUP_IOCTL_GIP_MAP_SIZE_OUT;
322 GipMapReq.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
323 GipMapReq.Hdr.rc = VERR_INTERNAL_ERROR;
324 GipMapReq.u.Out.HCPhysGip = NIL_RTHCPHYS;
325 GipMapReq.u.Out.pGipR0 = NIL_RTR0PTR;
326 GipMapReq.u.Out.pGipR3 = NULL;
327 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_GIP_MAP, &GipMapReq, SUP_IOCTL_GIP_MAP_SIZE);
328 if (RT_SUCCESS(rc))
329 rc = GipMapReq.Hdr.rc;
330 if (RT_SUCCESS(rc))
331 {
332 /*
333 * Set the GIP globals.
334 */
335 AssertRelease(GipMapReq.u.Out.pGipR3->u32Magic == SUPGLOBALINFOPAGE_MAGIC);
336 AssertRelease(GipMapReq.u.Out.pGipR3->u32Version >= SUPGLOBALINFOPAGE_VERSION);
337
338 ASMAtomicXchgSize(&g_HCPhysSUPGlobalInfoPage, GipMapReq.u.Out.HCPhysGip);
339 ASMAtomicCmpXchgPtr((void * volatile *)&g_pSUPGlobalInfoPage, GipMapReq.u.Out.pGipR3, NULL);
340 ASMAtomicCmpXchgPtr((void * volatile *)&g_pSUPGlobalInfoPageR0, (void *)GipMapReq.u.Out.pGipR0, NULL);
341 }
342 }
343 }
344 else
345 rc = VERR_NO_MEMORY;
346 }
347
348 if (RT_SUCCESS(rc))
349 {
350 /*
351 * Set the globals and return success.
352 */
353 g_u32Cookie = CookieReq.u.Out.u32Cookie;
354 g_u32SessionCookie = CookieReq.u.Out.u32SessionCookie;
355 g_pSession = CookieReq.u.Out.pSession;
356 g_pSupFunctions = pFuncsReq;
357 if (ppSession)
358 *ppSession = CookieReq.u.Out.pSession;
359 return VINF_SUCCESS;
360 }
361
362 /* bailout */
363 RTMemFree(pFuncsReq);
364 }
365 else
366 {
367 LogRel(("Support driver version mismatch: SessionVersion=%#x DriverVersion=%#x ClientVersion=%#x MinVersion=%#x\n",
368 CookieReq.u.Out.u32SessionVersion, CookieReq.u.Out.u32DriverVersion, SUPDRV_IOC_VERSION, uMinVersion));
369 rc = VERR_VM_DRIVER_VERSION_MISMATCH;
370 }
371 }
372 else
373 {
374 if (RT_SUCCESS(rc))
375 {
376 rc = CookieReq.Hdr.rc;
377 LogRel(("Support driver version mismatch: DriverVersion=%#x ClientVersion=%#x rc=%Rrc\n",
378 CookieReq.u.Out.u32DriverVersion, SUPDRV_IOC_VERSION, rc));
379 if (rc != VERR_VM_DRIVER_VERSION_MISMATCH)
380 rc = VERR_VM_DRIVER_VERSION_MISMATCH;
381 }
382 else
383 {
384 /* for pre 0x00060000 drivers */
385 LogRel(("Support driver version mismatch: DriverVersion=too-old ClientVersion=%#x\n", SUPDRV_IOC_VERSION));
386 rc = VERR_VM_DRIVER_VERSION_MISMATCH;
387 }
388 }
389
390 suplibOsTerm(&g_supLibData);
391 }
392 g_cInits--;
393
394 return rc;
395}
396
397
398SUPR3DECL(int) SUPR3Init(PSUPDRVSESSION *ppSession)
399{
400 return SUPR3InitEx(true, ppSession);
401}
402
403/**
404 * Fake mode init.
405 */
406static int supInitFake(PSUPDRVSESSION *ppSession)
407{
408 Log(("SUP: Fake mode!\n"));
409 static const SUPFUNC s_aFakeFunctions[] =
410 {
411 /* name function */
412 { "SUPR0AbsIs64bit", 0 },
413 { "SUPR0Abs64bitKernelCS", 0 },
414 { "SUPR0Abs64bitKernelSS", 0 },
415 { "SUPR0Abs64bitKernelDS", 0 },
416 { "SUPR0AbsKernelCS", 8 },
417 { "SUPR0AbsKernelSS", 16 },
418 { "SUPR0AbsKernelDS", 16 },
419 { "SUPR0AbsKernelES", 16 },
420 { "SUPR0AbsKernelFS", 24 },
421 { "SUPR0AbsKernelGS", 32 },
422 { "SUPR0ComponentRegisterFactory", 0xefeefffd },
423 { "SUPR0ComponentDeregisterFactory", 0xefeefffe },
424 { "SUPR0ComponentQueryFactory", 0xefeeffff },
425 { "SUPR0ObjRegister", 0xefef0000 },
426 { "SUPR0ObjAddRef", 0xefef0001 },
427 { "SUPR0ObjAddRefEx", 0xefef0001 },
428 { "SUPR0ObjRelease", 0xefef0002 },
429 { "SUPR0ObjVerifyAccess", 0xefef0003 },
430 { "SUPR0LockMem", 0xefef0004 },
431 { "SUPR0UnlockMem", 0xefef0005 },
432 { "SUPR0ContAlloc", 0xefef0006 },
433 { "SUPR0ContFree", 0xefef0007 },
434 { "SUPR0MemAlloc", 0xefef0008 },
435 { "SUPR0MemGetPhys", 0xefef0009 },
436 { "SUPR0MemFree", 0xefef000a },
437 { "SUPR0Printf", 0xefef000b },
438 { "SUPR0GetPagingMode", 0xefef000c },
439 { "SUPR0EnableVTx", 0xefef000e },
440 { "RTMemAlloc", 0xefef000f },
441 { "RTMemAllocZ", 0xefef0010 },
442 { "RTMemFree", 0xefef0011 },
443 { "RTR0MemObjAddress", 0xefef0012 },
444 { "RTR0MemObjAddressR3", 0xefef0013 },
445 { "RTR0MemObjAllocPage", 0xefef0014 },
446 { "RTR0MemObjAllocPhysNC", 0xefef0015 },
447 { "RTR0MemObjAllocLow", 0xefef0016 },
448 { "RTR0MemObjEnterPhys", 0xefef0017 },
449 { "RTR0MemObjFree", 0xefef0018 },
450 { "RTR0MemObjGetPagePhysAddr", 0xefef0019 },
451 { "RTR0MemObjMapUser", 0xefef001a },
452 { "RTR0MemObjMapKernel", 0xefef001b },
453 { "RTR0MemObjMapKernelEx", 0xefef001c },
454 { "RTMpGetArraySize", 0xefef001c },
455 { "RTProcSelf", 0xefef001d },
456 { "RTR0ProcHandleSelf", 0xefef001e },
457 { "RTSemEventCreate", 0xefef001f },
458 { "RTSemEventSignal", 0xefef0020 },
459 { "RTSemEventWait", 0xefef0021 },
460 { "RTSemEventWaitNoResume", 0xefef0022 },
461 { "RTSemEventDestroy", 0xefef0023 },
462 { "RTSemEventMultiCreate", 0xefef0024 },
463 { "RTSemEventMultiSignal", 0xefef0025 },
464 { "RTSemEventMultiReset", 0xefef0026 },
465 { "RTSemEventMultiWait", 0xefef0027 },
466 { "RTSemEventMultiWaitNoResume", 0xefef0028 },
467 { "RTSemEventMultiDestroy", 0xefef0029 },
468 { "RTSemFastMutexCreate", 0xefef002a },
469 { "RTSemFastMutexDestroy", 0xefef002b },
470 { "RTSemFastMutexRequest", 0xefef002c },
471 { "RTSemFastMutexRelease", 0xefef002d },
472 { "RTSpinlockCreate", 0xefef002e },
473 { "RTSpinlockDestroy", 0xefef002f },
474 { "RTSpinlockAcquire", 0xefef0030 },
475 { "RTSpinlockRelease", 0xefef0031 },
476 { "RTSpinlockAcquireNoInts", 0xefef0032 },
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
1040SUPR3DECL(int) SUPR3LockDownLoader(PRTERRINFO pErrInfo)
1041{
1042 /* fake */
1043 if (RT_UNLIKELY(g_uSupFakeMode))
1044 return VINF_SUCCESS;
1045
1046 /*
1047 * Lock down the module loader interface.
1048 */
1049 SUPREQHDR ReqHdr;
1050 ReqHdr.u32Cookie = g_u32Cookie;
1051 ReqHdr.u32SessionCookie = g_u32SessionCookie;
1052 ReqHdr.cbIn = SUP_IOCTL_LDR_LOCK_DOWN_SIZE_IN;
1053 ReqHdr.cbOut = SUP_IOCTL_LDR_LOCK_DOWN_SIZE_OUT;
1054 ReqHdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1055 ReqHdr.rc = VERR_INTERNAL_ERROR;
1056 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_LOCK_DOWN, &ReqHdr, SUP_IOCTL_LDR_LOCK_DOWN_SIZE);
1057 if (RT_FAILURE(rc))
1058 return RTErrInfoSetF(pErrInfo, rc,
1059 "SUPR3LockDownLoader: SUP_IOCTL_LDR_LOCK_DOWN ioctl returned %Rrc", rc);
1060
1061 return ReqHdr.rc;
1062}
1063
1064
1065/**
1066 * Fallback for SUPR3PageAllocEx on systems where RTR0MemObjPhysAllocNC isn't
1067 * supported.
1068 */
1069static int supPagePageAllocNoKernelFallback(size_t cPages, void **ppvPages, PSUPPAGE paPages)
1070{
1071 int rc = suplibOsPageAlloc(&g_supLibData, cPages, ppvPages);
1072 if (RT_SUCCESS(rc))
1073 {
1074 if (!paPages)
1075 paPages = (PSUPPAGE)alloca(sizeof(paPages[0]) * cPages);
1076 rc = supR3PageLock(*ppvPages, cPages, paPages);
1077 if (RT_FAILURE(rc))
1078 suplibOsPageFree(&g_supLibData, *ppvPages, cPages);
1079 }
1080 return rc;
1081}
1082
1083
1084SUPR3DECL(int) SUPR3PageAllocEx(size_t cPages, uint32_t fFlags, void **ppvPages, PRTR0PTR pR0Ptr, PSUPPAGE paPages)
1085{
1086 /*
1087 * Validate.
1088 */
1089 AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
1090 *ppvPages = NULL;
1091 AssertPtrNullReturn(pR0Ptr, VERR_INVALID_POINTER);
1092 if (pR0Ptr)
1093 *pR0Ptr = NIL_RTR0PTR;
1094 AssertPtrNullReturn(paPages, VERR_INVALID_POINTER);
1095 AssertMsgReturn(cPages > 0 && cPages <= VBOX_MAX_ALLOC_PAGE_COUNT, ("cPages=%zu\n", cPages), VERR_PAGE_COUNT_OUT_OF_RANGE);
1096 AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
1097
1098 /* fake */
1099 if (RT_UNLIKELY(g_uSupFakeMode))
1100 {
1101 void *pv = RTMemPageAllocZ(cPages * PAGE_SIZE);
1102 if (!pv)
1103 return VERR_NO_MEMORY;
1104 *ppvPages = pv;
1105 if (pR0Ptr)
1106 *pR0Ptr = (RTR0PTR)pv;
1107 if (paPages)
1108 for (size_t iPage = 0; iPage < cPages; iPage++)
1109 {
1110 paPages[iPage].uReserved = 0;
1111 paPages[iPage].Phys = (iPage + 4321) << PAGE_SHIFT;
1112 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
1113 }
1114 return VINF_SUCCESS;
1115 }
1116
1117 /*
1118 * Use fallback for non-R0 mapping?
1119 */
1120 if ( !pR0Ptr
1121 && !g_fSupportsPageAllocNoKernel)
1122 return supPagePageAllocNoKernelFallback(cPages, ppvPages, paPages);
1123
1124 /*
1125 * Issue IOCtl to the SUPDRV kernel module.
1126 */
1127 int rc;
1128 PSUPPAGEALLOCEX pReq = (PSUPPAGEALLOCEX)RTMemTmpAllocZ(SUP_IOCTL_PAGE_ALLOC_EX_SIZE(cPages));
1129 if (pReq)
1130 {
1131 pReq->Hdr.u32Cookie = g_u32Cookie;
1132 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
1133 pReq->Hdr.cbIn = SUP_IOCTL_PAGE_ALLOC_EX_SIZE_IN;
1134 pReq->Hdr.cbOut = SUP_IOCTL_PAGE_ALLOC_EX_SIZE_OUT(cPages);
1135 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
1136 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
1137 pReq->u.In.cPages = (uint32_t)cPages; AssertRelease(pReq->u.In.cPages == cPages);
1138 pReq->u.In.fKernelMapping = pR0Ptr != NULL;
1139 pReq->u.In.fUserMapping = true;
1140 pReq->u.In.fReserved0 = false;
1141 pReq->u.In.fReserved1 = false;
1142 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_ALLOC_EX, pReq, SUP_IOCTL_PAGE_ALLOC_EX_SIZE(cPages));
1143 if (RT_SUCCESS(rc))
1144 {
1145 rc = pReq->Hdr.rc;
1146 if (RT_SUCCESS(rc))
1147 {
1148 *ppvPages = pReq->u.Out.pvR3;
1149 if (pR0Ptr)
1150 *pR0Ptr = pReq->u.Out.pvR0;
1151 if (paPages)
1152 for (size_t iPage = 0; iPage < cPages; iPage++)
1153 {
1154 paPages[iPage].uReserved = 0;
1155 paPages[iPage].Phys = pReq->u.Out.aPages[iPage];
1156 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
1157 }
1158#ifdef RT_OS_DARWIN /* HACK ALERT! */
1159 supR3TouchPages(pReq->u.Out.pvR3, cPages);
1160#endif
1161 }
1162 else if ( rc == VERR_NOT_SUPPORTED
1163 && !pR0Ptr)
1164 {
1165 g_fSupportsPageAllocNoKernel = false;
1166 rc = supPagePageAllocNoKernelFallback(cPages, ppvPages, paPages);
1167 }
1168 }
1169
1170 RTMemTmpFree(pReq);
1171 }
1172 else
1173 rc = VERR_NO_TMP_MEMORY;
1174 return rc;
1175
1176}
1177
1178
1179SUPR3DECL(int) SUPR3PageMapKernel(void *pvR3, uint32_t off, uint32_t cb, uint32_t fFlags, PRTR0PTR pR0Ptr)
1180{
1181 /*
1182 * Validate.
1183 */
1184 AssertPtrReturn(pvR3, VERR_INVALID_POINTER);
1185 AssertPtrReturn(pR0Ptr, VERR_INVALID_POINTER);
1186 Assert(!(off & PAGE_OFFSET_MASK));
1187 Assert(!(cb & PAGE_OFFSET_MASK) && cb);
1188 Assert(!fFlags);
1189 *pR0Ptr = NIL_RTR0PTR;
1190
1191 /* fake */
1192 if (RT_UNLIKELY(g_uSupFakeMode))
1193 return VERR_NOT_SUPPORTED;
1194
1195 /*
1196 * Issue IOCtl to the SUPDRV kernel module.
1197 */
1198 SUPPAGEMAPKERNEL Req;
1199 Req.Hdr.u32Cookie = g_u32Cookie;
1200 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1201 Req.Hdr.cbIn = SUP_IOCTL_PAGE_MAP_KERNEL_SIZE_IN;
1202 Req.Hdr.cbOut = SUP_IOCTL_PAGE_MAP_KERNEL_SIZE_OUT;
1203 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1204 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1205 Req.u.In.pvR3 = pvR3;
1206 Req.u.In.offSub = off;
1207 Req.u.In.cbSub = cb;
1208 Req.u.In.fFlags = fFlags;
1209 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_MAP_KERNEL, &Req, SUP_IOCTL_PAGE_MAP_KERNEL_SIZE);
1210 if (RT_SUCCESS(rc))
1211 rc = Req.Hdr.rc;
1212 if (RT_SUCCESS(rc))
1213 *pR0Ptr = Req.u.Out.pvR0;
1214 return rc;
1215}
1216
1217
1218SUPR3DECL(int) SUPR3PageProtect(void *pvR3, RTR0PTR R0Ptr, uint32_t off, uint32_t cb, uint32_t fProt)
1219{
1220 /*
1221 * Validate.
1222 */
1223 AssertPtrReturn(pvR3, VERR_INVALID_POINTER);
1224 Assert(!(off & PAGE_OFFSET_MASK));
1225 Assert(!(cb & PAGE_OFFSET_MASK) && cb);
1226 AssertReturn(!(fProt & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC)), VERR_INVALID_PARAMETER);
1227
1228 /* fake */
1229 if (RT_UNLIKELY(g_uSupFakeMode))
1230 return RTMemProtect((uint8_t *)pvR3 + off, cb, fProt);
1231
1232 /*
1233 * Some OSes can do this from ring-3, so try that before we
1234 * issue the IOCtl to the SUPDRV kernel module.
1235 * (Yea, this isn't very nice, but just try get the job done for now.)
1236 */
1237#if !defined(RT_OS_SOLARIS)
1238 RTMemProtect((uint8_t *)pvR3 + off, cb, fProt);
1239#endif
1240
1241 SUPPAGEPROTECT Req;
1242 Req.Hdr.u32Cookie = g_u32Cookie;
1243 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1244 Req.Hdr.cbIn = SUP_IOCTL_PAGE_PROTECT_SIZE_IN;
1245 Req.Hdr.cbOut = SUP_IOCTL_PAGE_PROTECT_SIZE_OUT;
1246 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1247 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1248 Req.u.In.pvR3 = pvR3;
1249 Req.u.In.pvR0 = R0Ptr;
1250 Req.u.In.offSub = off;
1251 Req.u.In.cbSub = cb;
1252 Req.u.In.fProt = fProt;
1253 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_PROTECT, &Req, SUP_IOCTL_PAGE_PROTECT_SIZE);
1254 if (RT_SUCCESS(rc))
1255 rc = Req.Hdr.rc;
1256 return rc;
1257}
1258
1259
1260SUPR3DECL(int) SUPR3PageFreeEx(void *pvPages, size_t cPages)
1261{
1262 /*
1263 * Validate.
1264 */
1265 AssertPtrReturn(pvPages, VERR_INVALID_POINTER);
1266 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1267
1268 /* fake */
1269 if (RT_UNLIKELY(g_uSupFakeMode))
1270 {
1271 RTMemPageFree(pvPages, cPages * PAGE_SIZE);
1272 return VINF_SUCCESS;
1273 }
1274
1275 /*
1276 * Try normal free first, then if it fails check if we're using the fallback
1277 * for the allocations without kernel mappings and attempt unlocking it.
1278 */
1279 NOREF(cPages);
1280 SUPPAGEFREE Req;
1281 Req.Hdr.u32Cookie = g_u32Cookie;
1282 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1283 Req.Hdr.cbIn = SUP_IOCTL_PAGE_FREE_SIZE_IN;
1284 Req.Hdr.cbOut = SUP_IOCTL_PAGE_FREE_SIZE_OUT;
1285 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1286 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1287 Req.u.In.pvR3 = pvPages;
1288 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_FREE, &Req, SUP_IOCTL_PAGE_FREE_SIZE);
1289 if (RT_SUCCESS(rc))
1290 {
1291 rc = Req.Hdr.rc;
1292 if ( rc == VERR_INVALID_PARAMETER
1293 && !g_fSupportsPageAllocNoKernel)
1294 {
1295 int rc2 = supR3PageUnlock(pvPages);
1296 if (RT_SUCCESS(rc2))
1297 rc = suplibOsPageFree(&g_supLibData, pvPages, cPages);
1298 }
1299 }
1300 return rc;
1301}
1302
1303
1304SUPR3DECL(void *) SUPR3ContAlloc(size_t cPages, PRTR0PTR pR0Ptr, PRTHCPHYS pHCPhys)
1305{
1306 /*
1307 * Validate.
1308 */
1309 AssertPtrReturn(pHCPhys, NULL);
1310 *pHCPhys = NIL_RTHCPHYS;
1311 AssertPtrNullReturn(pR0Ptr, NULL);
1312 if (pR0Ptr)
1313 *pR0Ptr = NIL_RTR0PTR;
1314 AssertPtrNullReturn(pHCPhys, NULL);
1315 AssertMsgReturn(cPages > 0 && cPages < 256, ("cPages=%d must be > 0 and < 256\n", cPages), NULL);
1316
1317 /* fake */
1318 if (RT_UNLIKELY(g_uSupFakeMode))
1319 {
1320 void *pv = RTMemPageAllocZ(cPages * PAGE_SIZE);
1321 if (pR0Ptr)
1322 *pR0Ptr = (RTR0PTR)pv;
1323 if (pHCPhys)
1324 *pHCPhys = (uintptr_t)pv + (PAGE_SHIFT * 1024);
1325 return pv;
1326 }
1327
1328 /*
1329 * Issue IOCtl to the SUPDRV kernel module.
1330 */
1331 SUPCONTALLOC Req;
1332 Req.Hdr.u32Cookie = g_u32Cookie;
1333 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1334 Req.Hdr.cbIn = SUP_IOCTL_CONT_ALLOC_SIZE_IN;
1335 Req.Hdr.cbOut = SUP_IOCTL_CONT_ALLOC_SIZE_OUT;
1336 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1337 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1338 Req.u.In.cPages = (uint32_t)cPages;
1339 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CONT_ALLOC, &Req, SUP_IOCTL_CONT_ALLOC_SIZE);
1340 if ( RT_SUCCESS(rc)
1341 && RT_SUCCESS(Req.Hdr.rc))
1342 {
1343 *pHCPhys = Req.u.Out.HCPhys;
1344 if (pR0Ptr)
1345 *pR0Ptr = Req.u.Out.pvR0;
1346#ifdef RT_OS_DARWIN /* HACK ALERT! */
1347 supR3TouchPages(Req.u.Out.pvR3, cPages);
1348#endif
1349 return Req.u.Out.pvR3;
1350 }
1351
1352 return NULL;
1353}
1354
1355
1356SUPR3DECL(int) SUPR3ContFree(void *pv, size_t cPages)
1357{
1358 /*
1359 * Validate.
1360 */
1361 if (!pv)
1362 return VINF_SUCCESS;
1363 AssertPtrReturn(pv, VERR_INVALID_POINTER);
1364 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1365
1366 /* fake */
1367 if (RT_UNLIKELY(g_uSupFakeMode))
1368 {
1369 RTMemPageFree(pv, cPages * PAGE_SIZE);
1370 return VINF_SUCCESS;
1371 }
1372
1373 /*
1374 * Issue IOCtl to the SUPDRV kernel module.
1375 */
1376 SUPCONTFREE Req;
1377 Req.Hdr.u32Cookie = g_u32Cookie;
1378 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1379 Req.Hdr.cbIn = SUP_IOCTL_CONT_FREE_SIZE_IN;
1380 Req.Hdr.cbOut = SUP_IOCTL_CONT_FREE_SIZE_OUT;
1381 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1382 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1383 Req.u.In.pvR3 = pv;
1384 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CONT_FREE, &Req, SUP_IOCTL_CONT_FREE_SIZE);
1385 if (RT_SUCCESS(rc))
1386 rc = Req.Hdr.rc;
1387 return rc;
1388}
1389
1390
1391SUPR3DECL(int) SUPR3LowAlloc(size_t cPages, void **ppvPages, PRTR0PTR ppvPagesR0, PSUPPAGE paPages)
1392{
1393 /*
1394 * Validate.
1395 */
1396 AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
1397 *ppvPages = NULL;
1398 AssertPtrReturn(paPages, VERR_INVALID_POINTER);
1399 AssertMsgReturn(cPages > 0 && cPages < 256, ("cPages=%d must be > 0 and < 256\n", cPages), VERR_PAGE_COUNT_OUT_OF_RANGE);
1400
1401 /* fake */
1402 if (RT_UNLIKELY(g_uSupFakeMode))
1403 {
1404 *ppvPages = RTMemPageAllocZ((size_t)cPages * PAGE_SIZE);
1405 if (!*ppvPages)
1406 return VERR_NO_LOW_MEMORY;
1407
1408 /* fake physical addresses. */
1409 RTHCPHYS Phys = (uintptr_t)*ppvPages + PAGE_SIZE * 1024;
1410 size_t iPage = cPages;
1411 while (iPage-- > 0)
1412 paPages[iPage].Phys = Phys + (iPage << PAGE_SHIFT);
1413 return VINF_SUCCESS;
1414 }
1415
1416 /*
1417 * Issue IOCtl to the SUPDRV kernel module.
1418 */
1419 int rc;
1420 PSUPLOWALLOC pReq = (PSUPLOWALLOC)RTMemTmpAllocZ(SUP_IOCTL_LOW_ALLOC_SIZE(cPages));
1421 if (pReq)
1422 {
1423 pReq->Hdr.u32Cookie = g_u32Cookie;
1424 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
1425 pReq->Hdr.cbIn = SUP_IOCTL_LOW_ALLOC_SIZE_IN;
1426 pReq->Hdr.cbOut = SUP_IOCTL_LOW_ALLOC_SIZE_OUT(cPages);
1427 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
1428 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
1429 pReq->u.In.cPages = (uint32_t)cPages; AssertRelease(pReq->u.In.cPages == cPages);
1430 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LOW_ALLOC, pReq, SUP_IOCTL_LOW_ALLOC_SIZE(cPages));
1431 if (RT_SUCCESS(rc))
1432 rc = pReq->Hdr.rc;
1433 if (RT_SUCCESS(rc))
1434 {
1435 *ppvPages = pReq->u.Out.pvR3;
1436 if (ppvPagesR0)
1437 *ppvPagesR0 = pReq->u.Out.pvR0;
1438 if (paPages)
1439 for (size_t iPage = 0; iPage < cPages; iPage++)
1440 {
1441 paPages[iPage].uReserved = 0;
1442 paPages[iPage].Phys = pReq->u.Out.aPages[iPage];
1443 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
1444 Assert(paPages[iPage].Phys <= UINT32_C(0xfffff000));
1445 }
1446#ifdef RT_OS_DARWIN /* HACK ALERT! */
1447 supR3TouchPages(pReq->u.Out.pvR3, cPages);
1448#endif
1449 }
1450 RTMemTmpFree(pReq);
1451 }
1452 else
1453 rc = VERR_NO_TMP_MEMORY;
1454
1455 return rc;
1456}
1457
1458
1459SUPR3DECL(int) SUPR3LowFree(void *pv, size_t cPages)
1460{
1461 /*
1462 * Validate.
1463 */
1464 if (!pv)
1465 return VINF_SUCCESS;
1466 AssertPtrReturn(pv, VERR_INVALID_POINTER);
1467 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1468
1469 /* fake */
1470 if (RT_UNLIKELY(g_uSupFakeMode))
1471 {
1472 RTMemPageFree(pv, cPages * PAGE_SIZE);
1473 return VINF_SUCCESS;
1474 }
1475
1476 /*
1477 * Issue IOCtl to the SUPDRV kernel module.
1478 */
1479 SUPCONTFREE Req;
1480 Req.Hdr.u32Cookie = g_u32Cookie;
1481 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1482 Req.Hdr.cbIn = SUP_IOCTL_LOW_FREE_SIZE_IN;
1483 Req.Hdr.cbOut = SUP_IOCTL_LOW_FREE_SIZE_OUT;
1484 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1485 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1486 Req.u.In.pvR3 = pv;
1487 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LOW_FREE, &Req, SUP_IOCTL_LOW_FREE_SIZE);
1488 if (RT_SUCCESS(rc))
1489 rc = Req.Hdr.rc;
1490 return rc;
1491}
1492
1493
1494SUPR3DECL(int) SUPR3HardenedVerifyInit(void)
1495{
1496#ifdef RT_OS_WINDOWS
1497 if (g_cInits == 0)
1498 return suplibOsHardenedVerifyInit();
1499#endif
1500 return VINF_SUCCESS;
1501}
1502
1503
1504SUPR3DECL(int) SUPR3HardenedVerifyTerm(void)
1505{
1506#ifdef RT_OS_WINDOWS
1507 if (g_cInits == 0)
1508 return suplibOsHardenedVerifyTerm();
1509#endif
1510 return VINF_SUCCESS;
1511}
1512
1513
1514SUPR3DECL(int) SUPR3HardenedVerifyFile(const char *pszFilename, const char *pszMsg, PRTFILE phFile)
1515{
1516 /*
1517 * Quick input validation.
1518 */
1519 AssertPtr(pszFilename);
1520 AssertPtr(pszMsg);
1521 AssertReturn(!phFile, VERR_NOT_IMPLEMENTED); /** @todo Implement this. The deal is that we make sure the
1522 file is the same we verified after opening it. */
1523
1524 /*
1525 * Only do the actual check in hardened builds.
1526 */
1527#ifdef VBOX_WITH_HARDENING
1528 int rc = supR3HardenedVerifyFixedFile(pszFilename, false /* fFatal */);
1529 if (RT_FAILURE(rc))
1530 LogRel(("SUPR3HardenedVerifyFile: %s: Verification of \"%s\" failed, rc=%Rrc\n", pszMsg, pszFilename, rc));
1531 return rc;
1532#else
1533 return VINF_SUCCESS;
1534#endif
1535}
1536
1537
1538SUPR3DECL(int) SUPR3HardenedVerifySelf(const char *pszArgv0, bool fInternal, PRTERRINFO pErrInfo)
1539{
1540 /*
1541 * Quick input validation.
1542 */
1543 AssertPtr(pszArgv0);
1544 RTErrInfoClear(pErrInfo);
1545
1546 /*
1547 * Get the executable image path as we need it for all the tests here.
1548 */
1549 char szExecPath[RTPATH_MAX];
1550 if (!RTProcGetExecutablePath(szExecPath, sizeof(szExecPath)))
1551 return RTErrInfoSet(pErrInfo, VERR_INTERNAL_ERROR_2, "RTProcGetExecutablePath failed");
1552
1553 int rc;
1554 if (fInternal)
1555 {
1556 /*
1557 * Internal applications must be launched directly without any PATH
1558 * searching involved.
1559 */
1560 if (RTPathCompare(pszArgv0, szExecPath) != 0)
1561 return RTErrInfoSetF(pErrInfo, VERR_SUPLIB_INVALID_ARGV0_INTERNAL,
1562 "argv[0] does not match the executable image path: '%s' != '%s'", pszArgv0, szExecPath);
1563
1564 /*
1565 * Internal applications must reside in or under the
1566 * RTPathAppPrivateArch directory.
1567 */
1568 char szAppPrivateArch[RTPATH_MAX];
1569 rc = RTPathAppPrivateArch(szAppPrivateArch, sizeof(szAppPrivateArch));
1570 if (RT_FAILURE(rc))
1571 return RTErrInfoSetF(pErrInfo, VERR_SUPLIB_INVALID_ARGV0_INTERNAL,
1572 "RTPathAppPrivateArch failed with rc=%Rrc", rc);
1573 size_t cchAppPrivateArch = strlen(szAppPrivateArch);
1574 if ( cchAppPrivateArch >= strlen(szExecPath)
1575 || !RTPATH_IS_SLASH(szExecPath[cchAppPrivateArch]))
1576 return RTErrInfoSet(pErrInfo, VERR_SUPLIB_INVALID_INTERNAL_APP_DIR,
1577 "Internal executable does reside under RTPathAppPrivateArch");
1578 szExecPath[cchAppPrivateArch] = '\0';
1579 if (RTPathCompare(szExecPath, szAppPrivateArch) != 0)
1580 return RTErrInfoSet(pErrInfo, VERR_SUPLIB_INVALID_INTERNAL_APP_DIR,
1581 "Internal executable does reside under RTPathAppPrivateArch");
1582 szExecPath[cchAppPrivateArch] = RTPATH_SLASH;
1583 }
1584
1585#ifdef VBOX_WITH_HARDENING
1586 /*
1587 * Verify that the image file and parent directories are sane.
1588 */
1589 rc = supR3HardenedVerifyFile(szExecPath, RTHCUINTPTR_MAX, false /*fMaybe3rdParty*/, pErrInfo);
1590 if (RT_FAILURE(rc))
1591 return rc;
1592#endif
1593
1594 return VINF_SUCCESS;
1595}
1596
1597
1598SUPR3DECL(int) SUPR3HardenedVerifyDir(const char *pszDirPath, bool fRecursive, bool fCheckFiles, PRTERRINFO pErrInfo)
1599{
1600 /*
1601 * Quick input validation
1602 */
1603 AssertPtr(pszDirPath);
1604 RTErrInfoClear(pErrInfo);
1605
1606 /*
1607 * Only do the actual check in hardened builds.
1608 */
1609#ifdef VBOX_WITH_HARDENING
1610 int rc = supR3HardenedVerifyDir(pszDirPath, fRecursive, fCheckFiles, pErrInfo);
1611 if (RT_FAILURE(rc) && !RTErrInfoIsSet(pErrInfo))
1612 LogRel(("supR3HardenedVerifyDir: Verification of \"%s\" failed, rc=%Rrc\n", pszDirPath, rc));
1613 return rc;
1614#else
1615 NOREF(pszDirPath); NOREF(fRecursive); NOREF(fCheckFiles);
1616 return VINF_SUCCESS;
1617#endif
1618}
1619
1620
1621SUPR3DECL(int) SUPR3HardenedVerifyPlugIn(const char *pszFilename, PRTERRINFO pErrInfo)
1622{
1623 /*
1624 * Quick input validation
1625 */
1626 AssertPtr(pszFilename);
1627 RTErrInfoClear(pErrInfo);
1628
1629 /*
1630 * Only do the actual check in hardened builds.
1631 */
1632#ifdef VBOX_WITH_HARDENING
1633 int rc = supR3HardenedVerifyFile(pszFilename, RTHCUINTPTR_MAX, true /*fMaybe3rdParty*/, pErrInfo);
1634 if (RT_FAILURE(rc) && !RTErrInfoIsSet(pErrInfo))
1635 LogRel(("supR3HardenedVerifyFile: Verification of \"%s\" failed, rc=%Rrc\n", pszFilename, rc));
1636 return rc;
1637#else
1638 return VINF_SUCCESS;
1639#endif
1640}
1641
1642
1643SUPR3DECL(int) SUPR3GipGetPhys(PRTHCPHYS pHCPhys)
1644{
1645 if (g_pSUPGlobalInfoPage)
1646 {
1647 *pHCPhys = g_HCPhysSUPGlobalInfoPage;
1648 return VINF_SUCCESS;
1649 }
1650 *pHCPhys = NIL_RTHCPHYS;
1651 return VERR_WRONG_ORDER;
1652}
1653
1654
1655SUPR3DECL(int) SUPR3QueryVTxSupported(void)
1656{
1657#ifdef RT_OS_LINUX
1658 return suplibOsQueryVTxSupported();
1659#else
1660 return VINF_SUCCESS;
1661#endif
1662}
1663
1664
1665SUPR3DECL(int) SUPR3QueryVTCaps(uint32_t *pfCaps)
1666{
1667 AssertPtrReturn(pfCaps, VERR_INVALID_POINTER);
1668
1669 *pfCaps = 0;
1670
1671 /* fake */
1672 if (RT_UNLIKELY(g_uSupFakeMode))
1673 return VINF_SUCCESS;
1674
1675 /*
1676 * Issue IOCtl to the SUPDRV kernel module.
1677 */
1678 SUPVTCAPS Req;
1679 Req.Hdr.u32Cookie = g_u32Cookie;
1680 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1681 Req.Hdr.cbIn = SUP_IOCTL_VT_CAPS_SIZE_IN;
1682 Req.Hdr.cbOut = SUP_IOCTL_VT_CAPS_SIZE_OUT;
1683 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1684 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1685 Req.u.Out.Caps = 0;
1686 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_VT_CAPS, &Req, SUP_IOCTL_VT_CAPS_SIZE);
1687 if (RT_SUCCESS(rc))
1688 {
1689 rc = Req.Hdr.rc;
1690 if (RT_SUCCESS(rc))
1691 *pfCaps = Req.u.Out.Caps;
1692 }
1693 return rc;
1694}
1695
1696
1697SUPR3DECL(int) SUPR3TracerOpen(uint32_t uCookie, uintptr_t uArg)
1698{
1699 /* fake */
1700 if (RT_UNLIKELY(g_uSupFakeMode))
1701 return VINF_SUCCESS;
1702
1703 /*
1704 * Issue IOCtl to the SUPDRV kernel module.
1705 */
1706 SUPTRACEROPEN Req;
1707 Req.Hdr.u32Cookie = g_u32Cookie;
1708 Req.Hdr.u32SessionCookie= g_u32SessionCookie;
1709 Req.Hdr.cbIn = SUP_IOCTL_TRACER_OPEN_SIZE_IN;
1710 Req.Hdr.cbOut = SUP_IOCTL_TRACER_OPEN_SIZE_OUT;
1711 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1712 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1713 Req.u.In.uCookie = uCookie;
1714 Req.u.In.uArg = uArg;
1715 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_OPEN, &Req, SUP_IOCTL_TRACER_OPEN_SIZE);
1716 if (RT_SUCCESS(rc))
1717 rc = Req.Hdr.rc;
1718 return rc;
1719}
1720
1721
1722SUPR3DECL(int) SUPR3TracerClose(void)
1723{
1724 /* fake */
1725 if (RT_UNLIKELY(g_uSupFakeMode))
1726 return VINF_SUCCESS;
1727
1728 /*
1729 * Issue IOCtl to the SUPDRV kernel module.
1730 */
1731 SUPREQHDR Req;
1732 Req.u32Cookie = g_u32Cookie;
1733 Req.u32SessionCookie= g_u32SessionCookie;
1734 Req.cbIn = SUP_IOCTL_TRACER_OPEN_SIZE_IN;
1735 Req.cbOut = SUP_IOCTL_TRACER_OPEN_SIZE_OUT;
1736 Req.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1737 Req.rc = VERR_INTERNAL_ERROR;
1738 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_CLOSE, &Req, SUP_IOCTL_TRACER_CLOSE_SIZE);
1739 if (RT_SUCCESS(rc))
1740 rc = Req.rc;
1741 return rc;
1742}
1743
1744
1745SUPR3DECL(int) SUPR3TracerIoCtl(uintptr_t uCmd, uintptr_t uArg, int32_t *piRetVal)
1746{
1747 /* fake */
1748 if (RT_UNLIKELY(g_uSupFakeMode))
1749 {
1750 *piRetVal = -1;
1751 return VERR_NOT_SUPPORTED;
1752 }
1753
1754 /*
1755 * Issue IOCtl to the SUPDRV kernel module.
1756 */
1757 SUPTRACERIOCTL Req;
1758 Req.Hdr.u32Cookie = g_u32Cookie;
1759 Req.Hdr.u32SessionCookie= g_u32SessionCookie;
1760 Req.Hdr.cbIn = SUP_IOCTL_TRACER_IOCTL_SIZE_IN;
1761 Req.Hdr.cbOut = SUP_IOCTL_TRACER_IOCTL_SIZE_OUT;
1762 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1763 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1764 Req.u.In.uCmd = uCmd;
1765 Req.u.In.uArg = uArg;
1766 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_IOCTL, &Req, SUP_IOCTL_TRACER_IOCTL_SIZE);
1767 if (RT_SUCCESS(rc))
1768 {
1769 rc = Req.Hdr.rc;
1770 *piRetVal = Req.u.Out.iRetVal;
1771 }
1772 return rc;
1773}
1774
1775
1776
1777typedef struct SUPDRVTRACERSTRTAB
1778{
1779 /** Pointer to the string table. */
1780 char *pchStrTab;
1781 /** The actual string table size. */
1782 uint32_t cbStrTab;
1783 /** The original string pointers. */
1784 RTUINTPTR apszOrgFunctions[1];
1785} SUPDRVTRACERSTRTAB, *PSUPDRVTRACERSTRTAB;
1786
1787
1788/**
1789 * Destroys a string table, restoring the original pszFunction member valus.
1790 *
1791 * @param pThis The string table structure.
1792 * @param paProbLocs The probe location array.
1793 * @param cProbLocs The number of probe locations.
1794 */
1795static void supr3TracerDestroyStrTab(PSUPDRVTRACERSTRTAB pThis, PVTGPROBELOC32 paProbeLocs32, PVTGPROBELOC64 paProbeLocs64,
1796 uint32_t cProbeLocs, bool f32Bit)
1797{
1798 /* Restore. */
1799 size_t i = cProbeLocs;
1800 if (f32Bit)
1801 while (i--)
1802 paProbeLocs32[i].pszFunction = (uint32_t)pThis->apszOrgFunctions[i];
1803 else
1804 while (i--)
1805 paProbeLocs64[i].pszFunction = pThis->apszOrgFunctions[i];
1806
1807 /* Free. */
1808 RTMemFree(pThis->pchStrTab);
1809 RTMemFree(pThis);
1810}
1811
1812
1813/**
1814 * Creates a string table for the pszFunction members in the probe location
1815 * array.
1816 *
1817 * This will save and replace the pszFunction members with offsets.
1818 *
1819 * @returns Pointer to a string table structure. NULL on failure.
1820 * @param paProbLocs The probe location array.
1821 * @param cProbLocs The number of elements in the array.
1822 * @param cBits
1823 */
1824static PSUPDRVTRACERSTRTAB supr3TracerCreateStrTab(PVTGPROBELOC32 paProbeLocs32,
1825 PVTGPROBELOC64 paProbeLocs64,
1826 uint32_t cProbeLocs,
1827 RTUINTPTR offDelta,
1828 bool f32Bit)
1829{
1830 if (cProbeLocs > _128K)
1831 return NULL;
1832
1833 /*
1834 * Allocate the string table structures.
1835 */
1836 size_t cbThis = RT_OFFSETOF(SUPDRVTRACERSTRTAB, apszOrgFunctions[cProbeLocs]);
1837 PSUPDRVTRACERSTRTAB pThis = (PSUPDRVTRACERSTRTAB)RTMemAlloc(cbThis);
1838 if (!pThis)
1839 return NULL;
1840
1841 uint32_t const cHashBits = cProbeLocs * 2 - 1;
1842 uint32_t *pbmHash = (uint32_t *)RTMemAllocZ(RT_ALIGN_32(cHashBits, 64) / 8 );
1843 if (!pbmHash)
1844 {
1845 RTMemFree(pThis);
1846 return NULL;
1847 }
1848
1849 /*
1850 * Calc the max string table size and save the orignal pointers so we can
1851 * replace them later.
1852 */
1853 size_t cbMax = 1;
1854 for (uint32_t i = 0; i < cProbeLocs; i++)
1855 {
1856 pThis->apszOrgFunctions[i] = f32Bit ? paProbeLocs32[i].pszFunction : paProbeLocs64[i].pszFunction;
1857 const char *pszFunction = (const char *)(uintptr_t)(pThis->apszOrgFunctions[i] + offDelta);
1858 size_t cch = strlen(pszFunction);
1859 if (cch > _1K)
1860 {
1861 cbMax = 0;
1862 break;
1863 }
1864 cbMax += cch + 1;
1865 }
1866
1867 /* Alloc space for it. */
1868 if (cbMax > 0)
1869 pThis->pchStrTab = (char *)RTMemAlloc(cbMax);
1870 else
1871 pThis->pchStrTab = NULL;
1872 if (!pThis->pchStrTab)
1873 {
1874 RTMemFree(pbmHash);
1875 RTMemFree(pThis);
1876 return NULL;
1877 }
1878
1879 /*
1880 * Create the string table.
1881 */
1882 uint32_t off = 0;
1883 uint32_t offPrev = 0;
1884
1885 for (uint32_t i = 0; i < cProbeLocs; i++)
1886 {
1887 const char * const psz = (const char *)(uintptr_t)(pThis->apszOrgFunctions[i] + offDelta);
1888 size_t const cch = strlen(psz);
1889 uint32_t const iHashBit = RTStrHash1(psz) % cHashBits;
1890 if (ASMBitTestAndSet(pbmHash, iHashBit))
1891 {
1892 /* Often it's the most recent string. */
1893 if ( off - offPrev < cch + 1
1894 || memcmp(&pThis->pchStrTab[offPrev], psz, cch + 1))
1895 {
1896 /* It wasn't, search the entire string table. (lazy bird) */
1897 offPrev = 0;
1898 while (offPrev < off)
1899 {
1900 size_t cchCur = strlen(&pThis->pchStrTab[offPrev]);
1901 if ( cchCur == cch
1902 && !memcmp(&pThis->pchStrTab[offPrev], psz, cch + 1))
1903 break;
1904 offPrev += (uint32_t)cchCur + 1;
1905 }
1906 }
1907 }
1908 else
1909 offPrev = off;
1910
1911 /* Add the string to the table. */
1912 if (offPrev >= off)
1913 {
1914 memcpy(&pThis->pchStrTab[off], psz, cch + 1);
1915 offPrev = off;
1916 off += (uint32_t)cch + 1;
1917 }
1918
1919 /* Update the entry */
1920 if (f32Bit)
1921 paProbeLocs32[i].pszFunction = offPrev;
1922 else
1923 paProbeLocs64[i].pszFunction = offPrev;
1924 }
1925
1926 pThis->cbStrTab = off;
1927 RTMemFree(pbmHash);
1928 return pThis;
1929}
1930
1931
1932
1933SUPR3DECL(int) SUPR3TracerRegisterModule(uintptr_t hModNative, const char *pszModule, struct VTGOBJHDR *pVtgHdr,
1934 RTUINTPTR uVtgHdrAddr, uint32_t fFlags)
1935{
1936 /* Validate input. */
1937 NOREF(hModNative);
1938 AssertPtrReturn(pVtgHdr, VERR_INVALID_POINTER);
1939 AssertReturn(!memcmp(pVtgHdr->szMagic, VTGOBJHDR_MAGIC, sizeof(pVtgHdr->szMagic)), VERR_SUPDRV_VTG_MAGIC);
1940 AssertPtrReturn(pszModule, VERR_INVALID_POINTER);
1941 size_t cchModule = strlen(pszModule);
1942 AssertReturn(cchModule < RT_SIZEOFMEMB(SUPTRACERUMODREG, u.In.szName), VERR_FILENAME_TOO_LONG);
1943 AssertReturn(!RTPathHavePath(pszModule), VERR_INVALID_PARAMETER);
1944 AssertReturn(fFlags == SUP_TRACER_UMOD_FLAGS_EXE || fFlags == SUP_TRACER_UMOD_FLAGS_SHARED, VERR_INVALID_PARAMETER);
1945
1946 /*
1947 * Set the probe location array offset and size members. If the size is
1948 * zero, don't bother ring-0 with it.
1949 */
1950 if (!pVtgHdr->offProbeLocs)
1951 {
1952 uint64_t u64Tmp = pVtgHdr->uProbeLocsEnd.u64 - pVtgHdr->uProbeLocs.u64;
1953 if (u64Tmp >= UINT32_MAX)
1954 return VERR_SUPDRV_VTG_BAD_HDR_TOO_MUCH;
1955 pVtgHdr->cbProbeLocs = (uint32_t)u64Tmp;
1956
1957 u64Tmp = pVtgHdr->uProbeLocs.u64 - uVtgHdrAddr;
1958 if ((int64_t)u64Tmp != (int32_t)u64Tmp)
1959 {
1960 LogRel(("SUPR3TracerRegisterModule: VERR_SUPDRV_VTG_BAD_HDR_PTR - u64Tmp=%#llx uProbeLocs=%#llx uVtgHdrAddr=%RTptr\n",
1961 u64Tmp, pVtgHdr->uProbeLocs.u64, uVtgHdrAddr));
1962 return VERR_SUPDRV_VTG_BAD_HDR_PTR;
1963 }
1964 pVtgHdr->offProbeLocs = (int32_t)u64Tmp;
1965 }
1966
1967 if ( !pVtgHdr->cbProbeLocs
1968 || !pVtgHdr->cbProbes)
1969 return VINF_SUCCESS;
1970
1971 /*
1972 * Fake out.
1973 */
1974 if (RT_UNLIKELY(g_uSupFakeMode))
1975 return VINF_SUCCESS;
1976
1977 /*
1978 * Create a string table for the function names in the location array.
1979 * It's somewhat easier to do that here than from ring-0.
1980 */
1981 size_t const cProbeLocs = pVtgHdr->cbProbeLocs
1982 / (pVtgHdr->cBits == 32 ? sizeof(VTGPROBELOC32) : sizeof(VTGPROBELOC64));
1983 PVTGPROBELOC paProbeLocs = (PVTGPROBELOC)((uintptr_t)pVtgHdr + pVtgHdr->offProbeLocs);
1984 PSUPDRVTRACERSTRTAB pStrTab = supr3TracerCreateStrTab((PVTGPROBELOC32)paProbeLocs,
1985 (PVTGPROBELOC64)paProbeLocs,
1986 cProbeLocs, (uintptr_t)pVtgHdr - uVtgHdrAddr,
1987 pVtgHdr->cBits == 32);
1988 if (!pStrTab)
1989 return VERR_NO_MEMORY;
1990
1991
1992 /*
1993 * Issue IOCtl to the SUPDRV kernel module.
1994 */
1995 SUPTRACERUMODREG Req;
1996 Req.Hdr.u32Cookie = g_u32Cookie;
1997 Req.Hdr.u32SessionCookie= g_u32SessionCookie;
1998 Req.Hdr.cbIn = SUP_IOCTL_TRACER_UMOD_REG_SIZE_IN;
1999 Req.Hdr.cbOut = SUP_IOCTL_TRACER_UMOD_REG_SIZE_OUT;
2000 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2001 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2002 Req.u.In.uVtgHdrAddr = uVtgHdrAddr;
2003 Req.u.In.R3PtrVtgHdr = pVtgHdr;
2004 Req.u.In.R3PtrStrTab = pStrTab->pchStrTab;
2005 Req.u.In.cbStrTab = pStrTab->cbStrTab;
2006 Req.u.In.fFlags = fFlags;
2007
2008 memcpy(Req.u.In.szName, pszModule, cchModule + 1);
2009 if (!RTPathHasSuffix(Req.u.In.szName))
2010 {
2011 /* Add the default suffix if none is given. */
2012 switch (fFlags & SUP_TRACER_UMOD_FLAGS_TYPE_MASK)
2013 {
2014#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
2015 case SUP_TRACER_UMOD_FLAGS_EXE:
2016 if (cchModule + sizeof(".exe") <= sizeof(Req.u.In.szName))
2017 strcpy(&Req.u.In.szName[cchModule], ".exe");
2018 break;
2019#endif
2020
2021 case SUP_TRACER_UMOD_FLAGS_SHARED:
2022 {
2023 const char *pszSuff = RTLdrGetSuff();
2024 size_t cchSuff = strlen(pszSuff);
2025 if (cchModule + cchSuff < sizeof(Req.u.In.szName))
2026 memcpy(&Req.u.In.szName[cchModule], pszSuff, cchSuff + 1);
2027 break;
2028 }
2029 }
2030 }
2031
2032 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_UMOD_REG, &Req, SUP_IOCTL_TRACER_UMOD_REG_SIZE);
2033 if (RT_SUCCESS(rc))
2034 rc = Req.Hdr.rc;
2035
2036 supr3TracerDestroyStrTab(pStrTab, (PVTGPROBELOC32)paProbeLocs, (PVTGPROBELOC64)paProbeLocs,
2037 cProbeLocs, pVtgHdr->cBits == 32);
2038 return rc;
2039}
2040
2041
2042SUPR3DECL(int) SUPR3TracerDeregisterModule(struct VTGOBJHDR *pVtgHdr)
2043{
2044 /* Validate input. */
2045 AssertPtrReturn(pVtgHdr, VERR_INVALID_POINTER);
2046 AssertReturn(!memcmp(pVtgHdr->szMagic, VTGOBJHDR_MAGIC, sizeof(pVtgHdr->szMagic)), VERR_SUPDRV_VTG_MAGIC);
2047
2048 /*
2049 * Don't bother if the object is empty.
2050 */
2051 if ( !pVtgHdr->cbProbeLocs
2052 || !pVtgHdr->cbProbes)
2053 return VINF_SUCCESS;
2054
2055 /*
2056 * Fake out.
2057 */
2058 if (RT_UNLIKELY(g_uSupFakeMode))
2059 return VINF_SUCCESS;
2060
2061 /*
2062 * Issue IOCtl to the SUPDRV kernel module.
2063 */
2064 SUPTRACERUMODDEREG Req;
2065 Req.Hdr.u32Cookie = g_u32Cookie;
2066 Req.Hdr.u32SessionCookie= g_u32SessionCookie;
2067 Req.Hdr.cbIn = SUP_IOCTL_TRACER_UMOD_REG_SIZE_IN;
2068 Req.Hdr.cbOut = SUP_IOCTL_TRACER_UMOD_REG_SIZE_OUT;
2069 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2070 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2071 Req.u.In.pVtgHdr = pVtgHdr;
2072
2073 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_UMOD_DEREG, &Req, SUP_IOCTL_TRACER_UMOD_DEREG_SIZE);
2074 if (RT_SUCCESS(rc))
2075 rc = Req.Hdr.rc;
2076 return rc;
2077}
2078
2079
2080DECLASM(void) suplibTracerFireProbe(PVTGPROBELOC pProbeLoc, PSUPTRACERUMODFIREPROBE pReq)
2081{
2082 pReq->Hdr.u32Cookie = g_u32Cookie;
2083 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
2084 Assert(pReq->Hdr.cbIn == SUP_IOCTL_TRACER_UMOD_FIRE_PROBE_SIZE_IN);
2085 Assert(pReq->Hdr.cbOut == SUP_IOCTL_TRACER_UMOD_FIRE_PROBE_SIZE_OUT);
2086 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2087 pReq->Hdr.rc = VINF_SUCCESS;
2088
2089 suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_UMOD_FIRE_PROBE, pReq, SUP_IOCTL_TRACER_UMOD_FIRE_PROBE_SIZE);
2090}
2091
2092
2093SUPR3DECL(int) SUPR3MsrProberRead(uint32_t uMsr, RTCPUID idCpu, uint64_t *puValue, bool *pfGp)
2094{
2095 SUPMSRPROBER Req;
2096 Req.Hdr.u32Cookie = g_u32Cookie;
2097 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
2098 Req.Hdr.cbIn = SUP_IOCTL_MSR_PROBER_SIZE_IN;
2099 Req.Hdr.cbOut = SUP_IOCTL_MSR_PROBER_SIZE_OUT;
2100 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2101 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2102
2103 Req.u.In.enmOp = SUPMSRPROBEROP_READ;
2104 Req.u.In.uMsr = uMsr;
2105 Req.u.In.idCpu = idCpu == NIL_RTCPUID ? UINT32_MAX : idCpu;
2106
2107 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_MSR_PROBER, &Req, SUP_IOCTL_MSR_PROBER_SIZE);
2108 if (RT_SUCCESS(rc))
2109 rc = Req.Hdr.rc;
2110 if (RT_SUCCESS(rc))
2111 {
2112 if (puValue)
2113 *puValue = Req.u.Out.uResults.Read.uValue;
2114 if (pfGp)
2115 *pfGp = Req.u.Out.uResults.Read.fGp;
2116 }
2117
2118 return rc;
2119}
2120
2121
2122SUPR3DECL(int) SUPR3MsrProberWrite(uint32_t uMsr, RTCPUID idCpu, uint64_t uValue, bool *pfGp)
2123{
2124 SUPMSRPROBER Req;
2125 Req.Hdr.u32Cookie = g_u32Cookie;
2126 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
2127 Req.Hdr.cbIn = SUP_IOCTL_MSR_PROBER_SIZE_IN;
2128 Req.Hdr.cbOut = SUP_IOCTL_MSR_PROBER_SIZE_OUT;
2129 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2130 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2131
2132 Req.u.In.enmOp = SUPMSRPROBEROP_WRITE;
2133 Req.u.In.uMsr = uMsr;
2134 Req.u.In.idCpu = idCpu == NIL_RTCPUID ? UINT32_MAX : idCpu;
2135 Req.u.In.uArgs.Write.uToWrite = uValue;
2136
2137 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_MSR_PROBER, &Req, SUP_IOCTL_MSR_PROBER_SIZE);
2138 if (RT_SUCCESS(rc))
2139 rc = Req.Hdr.rc;
2140 if (RT_SUCCESS(rc) && pfGp)
2141 *pfGp = Req.u.Out.uResults.Write.fGp;
2142
2143 return rc;
2144}
2145
2146
2147SUPR3DECL(int) SUPR3MsrProberModify(uint32_t uMsr, RTCPUID idCpu, uint64_t fAndMask, uint64_t fOrMask,
2148 PSUPMSRPROBERMODIFYRESULT pResult)
2149{
2150 return SUPR3MsrProberModifyEx(uMsr, idCpu, fAndMask, fOrMask, false /*fFaster*/, pResult);
2151}
2152
2153
2154SUPR3DECL(int) SUPR3MsrProberModifyEx(uint32_t uMsr, RTCPUID idCpu, uint64_t fAndMask, uint64_t fOrMask, bool fFaster,
2155 PSUPMSRPROBERMODIFYRESULT pResult)
2156{
2157 SUPMSRPROBER Req;
2158 Req.Hdr.u32Cookie = g_u32Cookie;
2159 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
2160 Req.Hdr.cbIn = SUP_IOCTL_MSR_PROBER_SIZE_IN;
2161 Req.Hdr.cbOut = SUP_IOCTL_MSR_PROBER_SIZE_OUT;
2162 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2163 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2164
2165 Req.u.In.enmOp = fFaster ? SUPMSRPROBEROP_MODIFY_FASTER : SUPMSRPROBEROP_MODIFY;
2166 Req.u.In.uMsr = uMsr;
2167 Req.u.In.idCpu = idCpu == NIL_RTCPUID ? UINT32_MAX : idCpu;
2168 Req.u.In.uArgs.Modify.fAndMask = fAndMask;
2169 Req.u.In.uArgs.Modify.fOrMask = fOrMask;
2170
2171 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_MSR_PROBER, &Req, SUP_IOCTL_MSR_PROBER_SIZE);
2172 if (RT_SUCCESS(rc))
2173 rc = Req.Hdr.rc;
2174 if (RT_SUCCESS(rc))
2175 *pResult = Req.u.Out.uResults.Modify;
2176
2177 return rc;
2178}
2179
2180
2181SUPR3DECL(int) SUPR3ResumeSuspendedKeyboards(void)
2182{
2183#ifdef RT_OS_DARWIN
2184 /*
2185 * Issue IOCtl to the SUPDRV kernel module.
2186 */
2187 SUPREQHDR Req;
2188 Req.u32Cookie = g_u32Cookie;
2189 Req.u32SessionCookie= g_u32SessionCookie;
2190 Req.cbIn = SUP_IOCTL_RESUME_SUSPENDED_KBDS_SIZE_IN;
2191 Req.cbOut = SUP_IOCTL_RESUME_SUSPENDED_KBDS_SIZE_OUT;
2192 Req.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2193 Req.rc = VERR_INTERNAL_ERROR;
2194 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_RESUME_SUSPENDED_KBDS, &Req, SUP_IOCTL_RESUME_SUSPENDED_KBDS_SIZE);
2195 if (RT_SUCCESS(rc))
2196 rc = Req.rc;
2197 return rc;
2198#else /* !RT_OS_DARWIN */
2199 return VERR_NOT_SUPPORTED;
2200#endif
2201}
2202
2203
2204SUPR3DECL(int) SUPR3TscDeltaMeasure(RTCPUID idCpu, bool fAsync, bool fForce, uint8_t cRetries, uint8_t cMsWaitRetry)
2205{
2206 SUPTSCDELTAMEASURE Req;
2207 Req.Hdr.u32Cookie = g_u32Cookie;
2208 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
2209 Req.Hdr.cbIn = SUP_IOCTL_TSC_DELTA_MEASURE_SIZE_IN;
2210 Req.Hdr.cbOut = SUP_IOCTL_TSC_DELTA_MEASURE_SIZE_OUT;
2211 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2212 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2213
2214 Req.u.In.cRetries = cRetries;
2215 Req.u.In.fAsync = fAsync;
2216 Req.u.In.fForce = fForce;
2217 Req.u.In.idCpu = idCpu;
2218 Req.u.In.cMsWaitRetry = cMsWaitRetry;
2219
2220 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TSC_DELTA_MEASURE, &Req, SUP_IOCTL_TSC_DELTA_MEASURE_SIZE);
2221 if (RT_SUCCESS(rc))
2222 rc = Req.Hdr.rc;
2223 return rc;
2224}
2225
2226
2227SUPR3DECL(int) SUPR3ReadTsc(uint64_t *puTsc, uint16_t *pidApic)
2228{
2229 AssertReturn(puTsc, VERR_INVALID_PARAMETER);
2230
2231 SUPTSCREAD Req;
2232 Req.Hdr.u32Cookie = g_u32Cookie;
2233 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
2234 Req.Hdr.cbIn = SUP_IOCTL_TSC_READ_SIZE_IN;
2235 Req.Hdr.cbOut = SUP_IOCTL_TSC_READ_SIZE_OUT;
2236 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2237 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2238
2239 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TSC_READ, &Req, SUP_IOCTL_TSC_READ_SIZE);
2240 if (RT_SUCCESS(rc))
2241 {
2242 rc = Req.Hdr.rc;
2243 *puTsc = Req.u.Out.u64AdjustedTsc;
2244 if (pidApic)
2245 *pidApic = Req.u.Out.idApic;
2246 }
2247 return rc;
2248}
2249
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