VirtualBox

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

Last change on this file was 104328, checked in by vboxsync, 5 weeks ago

SUPDrv: Belated bumping of the major I/O control interface version following the new parameter added to RTR0MemObjAllocCont. See r158085. [build fix] bugref:10457

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

© 2023 Oracle
ContactPrivacy policyTerms of Use