VirtualBox

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

Last change on this file since 103914 was 100097, checked in by vboxsync, 18 months ago

HostDrivers/Support/darwin: Implement suplibOsIsNemSupportedWhenNoVtxOrAmdV() and query kern.hv.supported in order to determine whether NEM is available. Allows to mark hardware virtualization as supported so the GUI allows setting the CPU count for a VM , bugref:10454

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 85.2 KB
Line 
1/* $Id: SUPLib.cpp 100097 2023-06-07 17:45:33Z vboxsync $ */
2/** @file
3 * VirtualBox Support Library - Common code.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
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 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
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
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.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
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
41 * code, to pin down physical memory, and more.
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
45 * it will be externalized in a .r0 module that will be loaded using
46 * the IPRT loader.
47 *
48 * The Ring-0 calling is done thru a generic SUP interface which will
49 * transfer an argument set and call a predefined entry point in the Host
50 * VMM Ring-0 code.
51 *
52 * See @ref grp_sup "SUP - Support APIs" for API details.
53 */
54
55
56/*********************************************************************************************************************************
57* Header Files *
58*********************************************************************************************************************************/
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>
64#include <VBox/VBoxTpG.h>
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>
71#include <iprt/mp.h>
72#include <iprt/cpuset.h>
73#include <iprt/thread.h>
74#include <iprt/process.h>
75#include <iprt/path.h>
76#include <iprt/string.h>
77#include <iprt/env.h>
78#include <iprt/rand.h>
79#include <iprt/x86.h>
80
81#include "SUPDrvIOC.h"
82#include "SUPLibInternal.h"
83
84
85/*********************************************************************************************************************************
86* Defined Constants And Macros *
87*********************************************************************************************************************************/
88/** R0 VMM module name. */
89#define VMMR0_NAME "VMMR0"
90
91
92/*********************************************************************************************************************************
93* Structures and Typedefs *
94*********************************************************************************************************************************/
95typedef DECLCALLBACKTYPE(int, FNCALLVMMR0,(PVMR0 pVMR0, unsigned uOperation, void *pvArg));
96typedef FNCALLVMMR0 *PFNCALLVMMR0;
97
98
99/*********************************************************************************************************************************
100* Global Variables *
101*********************************************************************************************************************************/
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.
107 * Well, at least parts of it, specifically the parts that are being handed over
108 * via the pre-init mechanism from the hardened executable stub. */
109DECL_HIDDEN_DATA(SUPLIBDATA) g_supLibData =
110{
111 /*.hDevice = */ SUP_HDEVICE_NIL,
112 /*.fUnrestricted = */ true,
113 /*.fDriverless = */ false
114#if defined(RT_OS_DARWIN)
115 ,/* .uConnection = */ 0
116#elif defined(RT_OS_LINUX)
117 ,/* .fSysMadviseWorks = */ false
118#endif
119};
120
121/** Pointer to the Global Information Page.
122 *
123 * This pointer is valid as long as SUPLib has a open session. Anyone using
124 * the page must treat this pointer as highly volatile and not trust it beyond
125 * one transaction.
126 *
127 * @todo This will probably deserve it's own session or some other good solution...
128 */
129DECLEXPORT(PSUPGLOBALINFOPAGE) g_pSUPGlobalInfoPage;
130/** Address of the ring-0 mapping of the GIP. */
131PSUPGLOBALINFOPAGE g_pSUPGlobalInfoPageR0;
132/** The physical address of the GIP. */
133static RTHCPHYS g_HCPhysSUPGlobalInfoPage = NIL_RTHCPHYS;
134
135/** The negotiated cookie. */
136DECL_HIDDEN_DATA(uint32_t) g_u32Cookie = 0;
137/** The negotiated session cookie. */
138DECL_HIDDEN_DATA(uint32_t) g_u32SessionCookie;
139/** The session version. */
140DECL_HIDDEN_DATA(uint32_t) g_uSupSessionVersion = 0;
141/** Session handle. */
142DECL_HIDDEN_DATA(PSUPDRVSESSION) g_pSession;
143/** R0 SUP Functions used for resolving referenced to the SUPR0 module. */
144DECL_HIDDEN_DATA(PSUPQUERYFUNCS) g_pSupFunctions;
145
146/** PAGE_ALLOC_EX sans kernel mapping support indicator. */
147static bool g_fSupportsPageAllocNoKernel = true;
148/** Fake mode indicator. (~0 at first, 0 or 1 after first test) */
149DECL_HIDDEN_DATA(uint32_t) g_uSupFakeMode = UINT32_MAX;
150
151
152/*********************************************************************************************************************************
153* Internal Functions *
154*********************************************************************************************************************************/
155static int supInitFake(PSUPDRVSESSION *ppSession);
156
157
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
170SUPR3DECL(int) SUPR3Install(void)
171{
172 return suplibOsInstall();
173}
174
175
176SUPR3DECL(int) SUPR3Uninstall(void)
177{
178 return suplibOsUninstall();
179}
180
181
182DECL_NOTHROW(DECLEXPORT(int)) supR3PreInit(PSUPPREINITDATA pPreInitData, uint32_t fFlags)
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 */
190 if (!RT_VALID_PTR(pPreInitData))
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)
199 && pPreInitData->Data.hDevice == SUP_HDEVICE_NIL
200 && !pPreInitData->Data.fDriverless)
201 return VERR_INVALID_HANDLE;
202 if ( ( (fFlags & SUPSECMAIN_FLAGS_DONT_OPEN_DEV)
203 || pPreInitData->Data.fDriverless)
204 && pPreInitData->Data.hDevice != SUP_HDEVICE_NIL)
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
225SUPR3DECL(int) SUPR3InitEx(uint32_t fFlags, PSUPDRVSESSION *ppSession)
226{
227 /*
228 * Perform some sanity checks.
229 * (Got some trouble with compile time member alignment assertions.)
230 */
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));
237
238#ifdef VBOX_WITH_DRIVERLESS_FORCED
239 fFlags |= SUPR3INIT_F_DRIVERLESS;
240 fFlags &= ~SUPR3INIT_F_UNRESTRICTED;
241#endif
242
243 /*
244 * Check if already initialized.
245 */
246 if (ppSession)
247 *ppSession = g_pSession;
248 if (g_cInits++ > 0)
249 {
250 if ( (fFlags & SUPR3INIT_F_UNRESTRICTED)
251 && !g_supLibData.fUnrestricted
252 && !g_supLibData.fDriverless)
253 {
254 g_cInits--;
255 if (ppSession)
256 *ppSession = NIL_RTR0PTR;
257 return VERR_VM_DRIVER_NOT_ACCESSIBLE; /** @todo different status code? */
258 }
259 return VINF_SUCCESS;
260 }
261
262 /*
263 * Check for fake mode.
264 *
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 */
269 if (g_uSupFakeMode == ~0U)
270 {
271 const char *psz = RTEnvGet("VBOX_SUPLIB_FAKE");
272 if (psz && !strcmp(psz, "fake"))
273 ASMAtomicCmpXchgU32(&g_uSupFakeMode, 1, ~0U);
274 else
275 ASMAtomicCmpXchgU32(&g_uSupFakeMode, 0, ~0U);
276 }
277 if (RT_UNLIKELY(g_uSupFakeMode))
278 return supInitFake(ppSession);
279
280 /*
281 * Open the support driver.
282 */
283 SUPINITOP enmWhat = kSupInitOp_Driver;
284 int rc = suplibOsInit(&g_supLibData, g_fPreInited, fFlags, &enmWhat, NULL);
285 if (RT_SUCCESS(rc) && !g_supLibData.fDriverless)
286 {
287 /*
288 * Negotiate the cookie.
289 */
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);
299 CookieReq.u.In.u32ReqVersion = SUPDRV_IOC_VERSION;
300 const uint32_t uMinVersion = (SUPDRV_IOC_VERSION & 0xffff0000) == 0x00330000
301 ? 0x00330004
302 : SUPDRV_IOC_VERSION & 0xffff0000;
303 CookieReq.u.In.u32MinVersion = uMinVersion;
304 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_COOKIE, &CookieReq, SUP_IOCTL_COOKIE_SIZE);
305 if ( RT_SUCCESS(rc)
306 && RT_SUCCESS(CookieReq.Hdr.rc))
307 {
308 g_uSupSessionVersion = CookieReq.u.Out.u32SessionVersion;
309 if ( (CookieReq.u.Out.u32SessionVersion & 0xffff0000) == (SUPDRV_IOC_VERSION & 0xffff0000)
310 && CookieReq.u.Out.u32SessionVersion >= uMinVersion)
311 {
312 /*
313 * Query the functions.
314 */
315 PSUPQUERYFUNCS pFuncsReq = NULL;
316 if (g_supLibData.fUnrestricted)
317 {
318 pFuncsReq = (PSUPQUERYFUNCS)RTMemAllocZ(SUP_IOCTL_QUERY_FUNCS_SIZE(CookieReq.u.Out.cFunctions));
319 if (pFuncsReq)
320 {
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));
329 if (RT_SUCCESS(rc))
330 rc = pFuncsReq->Hdr.rc;
331 if (RT_SUCCESS(rc))
332 {
333 /*
334 * Map the GIP into userspace.
335 */
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);
357
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 }
362 }
363 }
364 else
365 rc = VERR_NO_MEMORY;
366 }
367
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;
376 g_pSupFunctions = pFuncsReq;
377 if (ppSession)
378 *ppSession = CookieReq.u.Out.pSession;
379 return VINF_SUCCESS;
380 }
381
382 /* bailout */
383 RTMemFree(pFuncsReq);
384 }
385 else
386 {
387 LogRel(("Support driver version mismatch: SessionVersion=%#x DriverVersion=%#x ClientVersion=%#x MinVersion=%#x\n",
388 CookieReq.u.Out.u32SessionVersion, CookieReq.u.Out.u32DriverVersion, SUPDRV_IOC_VERSION, uMinVersion));
389 rc = VERR_VM_DRIVER_VERSION_MISMATCH;
390 }
391 }
392 else
393 {
394 if (RT_SUCCESS(rc))
395 {
396 rc = CookieReq.Hdr.rc;
397 LogRel(("Support driver version mismatch: DriverVersion=%#x ClientVersion=%#x rc=%Rrc\n",
398 CookieReq.u.Out.u32DriverVersion, SUPDRV_IOC_VERSION, rc));
399 if (rc != VERR_VM_DRIVER_VERSION_MISMATCH)
400 rc = VERR_VM_DRIVER_VERSION_MISMATCH;
401 }
402 else
403 {
404 /* for pre 0x00060000 drivers */
405 LogRel(("Support driver version mismatch: DriverVersion=too-old ClientVersion=%#x\n", SUPDRV_IOC_VERSION));
406 rc = VERR_VM_DRIVER_VERSION_MISMATCH;
407 }
408 }
409
410 suplibOsTerm(&g_supLibData);
411 }
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
422 g_cInits--;
423
424 return rc;
425}
426
427
428SUPR3DECL(int) SUPR3Init(PSUPDRVSESSION *ppSession)
429{
430#ifndef VBOX_WITH_DRIVERLESS_FORCED
431 return SUPR3InitEx(SUPR3INIT_F_UNRESTRICTED, ppSession);
432#else
433 return SUPR3InitEx(SUPR3INIT_F_DRIVERLESS, ppSession);
434#endif
435}
436
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 {
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 },
538 };
539
540 /* fake r0 functions. */
541 g_pSupFunctions = (PSUPQUERYFUNCS)RTMemAllocZ(SUP_IOCTL_QUERY_FUNCS_SIZE(RT_ELEMENTS(s_aFakeFunctions)));
542 if (g_pSupFunctions)
543 {
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;
547 if (ppSession)
548 *ppSession = g_pSession;
549
550 /* fake the GIP. */
551 g_pSUPGlobalInfoPage = (PSUPGLOBALINFOPAGE)RTMemPageAllocZ(PAGE_SIZE);
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
560 RTMemFree(g_pSupFunctions);
561 g_pSupFunctions = NULL;
562 }
563 return VERR_NO_MEMORY;
564}
565
566
567SUPR3DECL(int) SUPR3Term(bool fForced)
568{
569 /*
570 * Verify state.
571 */
572 AssertMsg(g_cInits > 0, ("SUPR3Term() is called before SUPR3Init()!\n"));
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 {
582 ASMAtomicWriteNullPtr((void * volatile *)&g_pSUPGlobalInfoPage);
583 ASMAtomicWriteNullPtr((void * volatile *)&g_pSUPGlobalInfoPageR0);
584 ASMAtomicWriteU64(&g_HCPhysSUPGlobalInfoPage, NIL_RTHCPHYS);
585 /* just a little safe guard against threads using the page. */
586 RTThreadSleep(50);
587 }
588
589 /*
590 * Close the support driver.
591 */
592 int rc = suplibOsTerm(&g_supLibData);
593 if (rc)
594 return rc;
595
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;
602 }
603 else
604 g_cInits--;
605
606 return 0;
607}
608
609
610SUPR3DECL(bool) SUPR3IsDriverless(void)
611{
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;
615}
616
617
618SUPR3DECL(SUPPAGINGMODE) SUPR3GetPagingMode(void)
619{
620 /*
621 * Deal with driverless first.
622 */
623 if (g_supLibData.fDriverless)
624#if defined(RT_ARCH_AMD64)
625 return SUPPAGINGMODE_AMD64_GLOBAL_NX;
626#elif defined(RT_ARCH_X86)
627 return SUPPAGINGMODE_32_BIT_GLOBAL;
628#else
629 return SUPPAGINGMODE_INVALID;
630#endif
631
632 /*
633 * Issue IOCtl to the SUPDRV kernel module.
634 */
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;
642 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_GET_PAGING_MODE, &Req, SUP_IOCTL_GET_PAGING_MODE_SIZE);
643 if ( RT_FAILURE(rc)
644 || RT_FAILURE(Req.Hdr.rc))
645 {
646 LogRel(("SUPR3GetPagingMode: %Rrc %Rrc\n", rc, Req.Hdr.rc));
647 Req.u.Out.enmMode = SUPPAGINGMODE_INVALID;
648 }
649
650 return Req.u.Out.enmMode;
651}
652
653
654/**
655 * For later.
656 */
657static int supCallVMMR0ExFake(PVMR0 pVMR0, unsigned uOperation, uint64_t u64Arg, PSUPVMMR0REQHDR pReqHdr)
658{
659 AssertMsgFailed(("%d\n", uOperation)); NOREF(pVMR0); NOREF(uOperation); NOREF(u64Arg); NOREF(pReqHdr);
660 return VERR_NOT_SUPPORTED;
661}
662
663
664SUPR3DECL(int) SUPR3CallVMMR0Fast(PVMR0 pVMR0, unsigned uOperation, VMCPUID idCpu)
665{
666 NOREF(pVMR0);
667 static const uintptr_t s_auFunctions[3] =
668 {
669 SUP_IOCTL_FAST_DO_HM_RUN,
670 SUP_IOCTL_FAST_DO_NEM_RUN,
671 SUP_IOCTL_FAST_DO_NOP,
672 };
673 AssertCompile(SUP_VMMR0_DO_HM_RUN == 0);
674 AssertCompile(SUP_VMMR0_DO_NEM_RUN == 1);
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);
678}
679
680
681SUPR3DECL(int) SUPR3CallVMMR0Ex(PVMR0 pVMR0, VMCPUID idCpu, unsigned uOperation, uint64_t u64Arg, PSUPVMMR0REQHDR pReqHdr)
682{
683 /*
684 * The following operations don't belong here.
685 */
686 AssertMsgReturn( uOperation != SUP_VMMR0_DO_HM_RUN
687 && uOperation != SUP_VMMR0_DO_NEM_RUN
688 && uOperation != SUP_VMMR0_DO_NOP,
689 ("%#x\n", uOperation),
690 VERR_INTERNAL_ERROR);
691
692 /* fake */
693 if (RT_UNLIKELY(g_uSupFakeMode))
694 return supCallVMMR0ExFake(pVMR0, uOperation, u64Arg, pReqHdr);
695
696 int rc;
697 if (!pReqHdr)
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;
708 Req.u.In.idCpu = idCpu;
709 Req.u.In.uOperation = uOperation;
710 Req.u.In.u64Arg = u64Arg;
711 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_VMMR0(0), &Req, SUP_IOCTL_CALL_VMMR0_SIZE(0));
712 if (RT_SUCCESS(rc))
713 rc = Req.Hdr.rc;
714 }
715 else if (SUP_IOCTL_CALL_VMMR0_SIZE(pReqHdr->cbReq) < _4K) /* FreeBSD won't copy more than 4K. */
716 {
717 AssertPtrReturn(pReqHdr, VERR_INVALID_POINTER);
718 AssertReturn(pReqHdr->u32Magic == SUPVMMR0REQHDR_MAGIC, VERR_INVALID_MAGIC);
719 const size_t cbReq = pReqHdr->cbReq;
720
721 PSUPCALLVMMR0 pReq = (PSUPCALLVMMR0)alloca(SUP_IOCTL_CALL_VMMR0_SIZE(cbReq));
722 pReq->Hdr.u32Cookie = g_u32Cookie;
723 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
724 pReq->Hdr.cbIn = SUP_IOCTL_CALL_VMMR0_SIZE_IN(cbReq);
725 pReq->Hdr.cbOut = SUP_IOCTL_CALL_VMMR0_SIZE_OUT(cbReq);
726 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
727 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
728 pReq->u.In.pVMR0 = pVMR0;
729 pReq->u.In.idCpu = idCpu;
730 pReq->u.In.uOperation = uOperation;
731 pReq->u.In.u64Arg = u64Arg;
732 memcpy(&pReq->abReqPkt[0], pReqHdr, cbReq);
733 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_VMMR0(cbReq), pReq, SUP_IOCTL_CALL_VMMR0_SIZE(cbReq));
734 if (RT_SUCCESS(rc))
735 rc = pReq->Hdr.rc;
736 memcpy(pReqHdr, &pReq->abReqPkt[0], cbReq);
737 }
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);
764 return rc;
765}
766
767
768SUPR3DECL(int) SUPR3CallVMMR0(PVMR0 pVMR0, VMCPUID idCpu, unsigned uOperation, void *pvArg)
769{
770 /*
771 * The following operations don't belong here.
772 */
773 AssertMsgReturn( uOperation != SUP_VMMR0_DO_HM_RUN
774 && uOperation != SUP_VMMR0_DO_NEM_RUN
775 && uOperation != SUP_VMMR0_DO_NOP,
776 ("%#x\n", uOperation),
777 VERR_INTERNAL_ERROR);
778 return SUPR3CallVMMR0Ex(pVMR0, idCpu, uOperation, (uintptr_t)pvArg, NULL);
779}
780
781
782SUPR3DECL(int) SUPR3SetVMForFastIOCtl(PVMR0 pVMR0)
783{
784 if (RT_UNLIKELY(g_uSupFakeMode))
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;
795 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_SET_VM_FOR_FAST, &Req, SUP_IOCTL_SET_VM_FOR_FAST_SIZE);
796 if (RT_SUCCESS(rc))
797 rc = Req.Hdr.rc;
798 return rc;
799}
800
801
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 */
808 if (RT_UNLIKELY(g_uSupFakeMode))
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
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.
867 * @param pszDest The destination specificier.
868 */
869static int supR3LoggerSettings(SUPLOGGER enmWhich, uint32_t fWhat, const char *pszFlags, const char *pszGroups, const char *pszDest)
870{
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);
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
957SUPR3DECL(int) SUPR3PageAlloc(size_t cPages, uint32_t fFlags, void **ppvPages)
958{
959 /*
960 * Validate.
961 */
962 AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
963 *ppvPages = NULL;
964 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
965 AssertReturn(!(fFlags & ~SUP_PAGE_ALLOC_F_VALID_MASK), VERR_INVALID_FLAGS);
966
967 /*
968 * Call OS specific worker.
969 */
970 return suplibOsPageAlloc(&g_supLibData, cPages, fFlags, ppvPages);
971}
972
973
974SUPR3DECL(int) SUPR3PageFree(void *pvPages, size_t cPages)
975{
976 /*
977 * Validate.
978 */
979 AssertPtrReturn(pvPages, VERR_INVALID_POINTER);
980 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
981
982 /*
983 * Call OS specific worker.
984 */
985 return suplibOsPageFree(&g_supLibData, pvPages, cPages);
986}
987
988
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 */
1000SUPR3DECL(int) supR3PageLock(void *pvStart, size_t cPages, PSUPPAGE paPages)
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 */
1010 if (RT_UNLIKELY(g_uSupFakeMode))
1011 {
1012 RTHCPHYS Phys = (uintptr_t)pvStart + PAGE_SIZE * 1024;
1013 size_t iPage = cPages;
1014 while (iPage-- > 0)
1015 paPages[iPage].Phys = Phys + (iPage << PAGE_SHIFT);
1016 return VINF_SUCCESS;
1017 }
1018
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;
1033 pReq->u.In.cPages = (uint32_t)cPages; AssertRelease(pReq->u.In.cPages == cPages);
1034 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_LOCK, pReq, SUP_IOCTL_PAGE_LOCK_SIZE(cPages));
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];
1043 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
1044 }
1045 }
1046 RTMemTmpFree(pReq);
1047 }
1048 else
1049 rc = VERR_NO_TMP_MEMORY;
1050
1051 return rc;
1052}
1053
1054
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 */
1062SUPR3DECL(int) supR3PageUnlock(void *pvStart)
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
1070 /* fake */
1071 if (RT_UNLIKELY(g_uSupFakeMode))
1072 return VINF_SUCCESS;
1073
1074 /*
1075 * Issue IOCtl to the SUPDRV kernel module.
1076 */
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;
1085 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_UNLOCK, &Req, SUP_IOCTL_PAGE_UNLOCK_SIZE);
1086 if (RT_SUCCESS(rc))
1087 rc = Req.Hdr.rc;
1088 return rc;
1089}
1090
1091
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
1117/**
1118 * Fallback for SUPR3PageAllocEx on systems where RTR0MemObjPhysAllocNC isn't
1119 * supported.
1120 */
1121static int supPagePageAllocNoKernelFallback(size_t cPages, void **ppvPages, PSUPPAGE paPages)
1122{
1123 int rc = suplibOsPageAlloc(&g_supLibData, cPages, 0, ppvPages);
1124 if (RT_SUCCESS(rc))
1125 {
1126 Assert(ASMMemIsZero(*ppvPages, cPages << PAGE_SHIFT));
1127 if (!paPages)
1128 paPages = (PSUPPAGE)alloca(sizeof(paPages[0]) * cPages);
1129 rc = supR3PageLock(*ppvPages, cPages, paPages);
1130 if (RT_FAILURE(rc))
1131 suplibOsPageFree(&g_supLibData, *ppvPages, cPages);
1132 }
1133 return rc;
1134}
1135
1136
1137SUPR3DECL(int) SUPR3PageAllocEx(size_t cPages, uint32_t fFlags, void **ppvPages, PRTR0PTR pR0Ptr, PSUPPAGE paPages)
1138{
1139 /*
1140 * Validate.
1141 */
1142 AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
1143 *ppvPages = NULL;
1144 AssertPtrNullReturn(pR0Ptr, VERR_INVALID_POINTER);
1145 if (pR0Ptr)
1146 *pR0Ptr = NIL_RTR0PTR;
1147 AssertPtrNullReturn(paPages, VERR_INVALID_POINTER);
1148 AssertMsgReturn(cPages > 0 && cPages <= VBOX_MAX_ALLOC_PAGE_COUNT, ("cPages=%zu\n", cPages), VERR_PAGE_COUNT_OUT_OF_RANGE);
1149 AssertReturn(!fFlags, VERR_INVALID_FLAGS);
1150
1151 /*
1152 * Deal with driverless mode first.
1153 */
1154 if (g_supLibData.fDriverless)
1155 {
1156 int rc = SUPR3PageAlloc(cPages, 0 /*fFlags*/, ppvPages);
1157 Assert(RT_FAILURE(rc) || ASMMemIsZero(*ppvPages, cPages << PAGE_SHIFT));
1158 if (pR0Ptr)
1159 *pR0Ptr = NIL_RTR0PTR;
1160 if (paPages)
1161 for (size_t iPage = 0; iPage < cPages; iPage++)
1162 {
1163 paPages[iPage].uReserved = 0;
1164 paPages[iPage].Phys = NIL_RTHCPHYS;
1165 }
1166 return rc;
1167 }
1168
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
1176 /*
1177 * Use fallback for non-R0 mapping?
1178 */
1179 if ( !pR0Ptr
1180 && !g_fSupportsPageAllocNoKernel)
1181 return supPagePageAllocNoKernelFallback(cPages, ppvPages, paPages);
1182
1183 /*
1184 * Issue IOCtl to the SUPDRV kernel module.
1185 */
1186 int rc;
1187 PSUPPAGEALLOCEX pReq = (PSUPPAGEALLOCEX)RTMemTmpAllocZ(SUP_IOCTL_PAGE_ALLOC_EX_SIZE(cPages));
1188 if (pReq)
1189 {
1190 pReq->Hdr.u32Cookie = g_u32Cookie;
1191 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
1192 pReq->Hdr.cbIn = SUP_IOCTL_PAGE_ALLOC_EX_SIZE_IN;
1193 pReq->Hdr.cbOut = SUP_IOCTL_PAGE_ALLOC_EX_SIZE_OUT(cPages);
1194 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
1195 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
1196 pReq->u.In.cPages = (uint32_t)cPages; AssertRelease(pReq->u.In.cPages == cPages);
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));
1202 if (RT_SUCCESS(rc))
1203 {
1204 rc = pReq->Hdr.rc;
1205 if (RT_SUCCESS(rc))
1206 {
1207 *ppvPages = pReq->u.Out.pvR3;
1208 if (pR0Ptr)
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
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];
1224 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
1225 }
1226 }
1227 else if ( rc == VERR_NOT_SUPPORTED
1228 && !pR0Ptr)
1229 {
1230 g_fSupportsPageAllocNoKernel = false;
1231 rc = supPagePageAllocNoKernelFallback(cPages, ppvPages, paPages);
1232 }
1233 }
1234
1235 RTMemTmpFree(pReq);
1236 }
1237 else
1238 rc = VERR_NO_TMP_MEMORY;
1239 return rc;
1240
1241}
1242
1243
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
1256 /*
1257 * Not a valid operation in driverless mode.
1258 */
1259 AssertReturn(g_supLibData.fDriverless, VERR_SUP_DRIVERLESS);
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
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
1294 /*
1295 * Deal with driverless mode first.
1296 */
1297 if (g_supLibData.fDriverless)
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 */
1305#if !defined(RT_OS_SOLARIS)
1306 RTMemProtect((uint8_t *)pvR3 + off, cb, fProt);
1307#endif
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
1328SUPR3DECL(int) SUPR3PageFreeEx(void *pvPages, size_t cPages)
1329{
1330 /*
1331 * Validate.
1332 */
1333 AssertPtrReturn(pvPages, VERR_INVALID_POINTER);
1334 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1335
1336 /*
1337 * Deal with driverless mode first.
1338 */
1339 if (g_supLibData.fDriverless)
1340 {
1341 SUPR3PageFree(pvPages, cPages);
1342 return VINF_SUCCESS;
1343 }
1344
1345 /*
1346 * Try normal free first, then if it fails check if we're using the fallback
1347 * for the allocations without kernel mappings and attempt unlocking it.
1348 */
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))
1360 {
1361 rc = Req.Hdr.rc;
1362 if ( rc == VERR_INVALID_PARAMETER
1363 && !g_fSupportsPageAllocNoKernel)
1364 {
1365 int rc2 = supR3PageUnlock(pvPages);
1366 if (RT_SUCCESS(rc2))
1367 rc = suplibOsPageFree(&g_supLibData, pvPages, cPages);
1368 }
1369 }
1370 return rc;
1371}
1372
1373
1374SUPR3DECL(void *) SUPR3ContAlloc(size_t cPages, PRTR0PTR pR0Ptr, PRTHCPHYS pHCPhys)
1375{
1376 /*
1377 * Validate.
1378 */
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
1387 /*
1388 * Deal with driverless mode first.
1389 */
1390 if (g_supLibData.fDriverless)
1391 {
1392 void *pvPages = NULL;
1393 int rc = SUPR3PageAlloc(cPages, 0 /*fFlags*/, &pvPages);
1394 if (pR0Ptr)
1395 *pR0Ptr = NIL_RTR0PTR;
1396 if (pHCPhys)
1397 *pHCPhys = NIL_RTHCPHYS;
1398 return RT_SUCCESS(rc) ? pvPages : NULL;
1399 }
1400
1401 /*
1402 * Issue IOCtl to the SUPDRV kernel module.
1403 */
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;
1411 Req.u.In.cPages = (uint32_t)cPages;
1412 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CONT_ALLOC, &Req, SUP_IOCTL_CONT_ALLOC_SIZE);
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;
1419#ifdef RT_OS_DARWIN /* HACK ALERT! */
1420 supR3TouchPages(Req.u.Out.pvR3, cPages);
1421#endif
1422 return Req.u.Out.pvR3;
1423 }
1424
1425 return NULL;
1426}
1427
1428
1429SUPR3DECL(int) SUPR3ContFree(void *pv, size_t cPages)
1430{
1431 /*
1432 * Validate.
1433 */
1434 if (!pv)
1435 return VINF_SUCCESS;
1436 AssertPtrReturn(pv, VERR_INVALID_POINTER);
1437 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1438
1439 /*
1440 * Deal with driverless mode first.
1441 */
1442 if (g_supLibData.fDriverless)
1443 return SUPR3PageFree(pv, cPages);
1444
1445 /*
1446 * Issue IOCtl to the SUPDRV kernel module.
1447 */
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;
1456 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CONT_FREE, &Req, SUP_IOCTL_CONT_FREE_SIZE);
1457 if (RT_SUCCESS(rc))
1458 rc = Req.Hdr.rc;
1459 return rc;
1460}
1461
1462
1463SUPR3DECL(int) SUPR3LowAlloc(size_t cPages, void **ppvPages, PRTR0PTR ppvPagesR0, PSUPPAGE paPages)
1464{
1465 /*
1466 * Validate.
1467 */
1468 AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
1469 *ppvPages = NULL;
1470 AssertPtrReturn(paPages, VERR_INVALID_POINTER);
1471 AssertMsgReturn(cPages > 0 && cPages < 256, ("cPages=%d must be > 0 and < 256\n", cPages), VERR_PAGE_COUNT_OUT_OF_RANGE);
1472
1473 /* fake */
1474 if (RT_UNLIKELY(g_uSupFakeMode))
1475 {
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;
1482 size_t iPage = cPages;
1483 while (iPage-- > 0)
1484 paPages[iPage].Phys = Phys + (iPage << PAGE_SHIFT);
1485 return VINF_SUCCESS;
1486 }
1487
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;
1501 pReq->u.In.cPages = (uint32_t)cPages; AssertRelease(pReq->u.In.cPages == cPages);
1502 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LOW_ALLOC, pReq, SUP_IOCTL_LOW_ALLOC_SIZE(cPages));
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];
1515 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
1516 Assert(paPages[iPage].Phys <= UINT32_C(0xfffff000));
1517 }
1518#ifdef RT_OS_DARWIN /* HACK ALERT! */
1519 supR3TouchPages(pReq->u.Out.pvR3, cPages);
1520#endif
1521 }
1522 RTMemTmpFree(pReq);
1523 }
1524 else
1525 rc = VERR_NO_TMP_MEMORY;
1526
1527 return rc;
1528}
1529
1530
1531SUPR3DECL(int) SUPR3LowFree(void *pv, size_t cPages)
1532{
1533 /*
1534 * Validate.
1535 */
1536 if (!pv)
1537 return VINF_SUCCESS;
1538 AssertPtrReturn(pv, VERR_INVALID_POINTER);
1539 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1540
1541 /* fake */
1542 if (RT_UNLIKELY(g_uSupFakeMode))
1543 {
1544 RTMemPageFree(pv, cPages * PAGE_SIZE);
1545 return VINF_SUCCESS;
1546 }
1547
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;
1559 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LOW_FREE, &Req, SUP_IOCTL_LOW_FREE_SIZE);
1560 if (RT_SUCCESS(rc))
1561 rc = Req.Hdr.rc;
1562 return rc;
1563}
1564
1565
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
1586SUPR3DECL(int) SUPR3HardenedVerifyFile(const char *pszFilename, const char *pszMsg, PRTFILE phFile)
1587{
1588 /*
1589 * Quick input validation.
1590 */
1591 AssertPtr(pszFilename);
1592 AssertPtr(pszMsg);
1593 AssertReturn(!phFile, VERR_NOT_IMPLEMENTED); /** @todo Implement this. The deal is that we make sure the
1594 file is the same we verified after opening it. */
1595 RT_NOREF2(pszFilename, pszMsg);
1596
1597 /*
1598 * Only do the actual check in hardened builds.
1599 */
1600#ifdef VBOX_WITH_HARDENING
1601 int rc = supR3HardenedVerifyFixedFile(pszFilename, false /* fFatal */);
1602 if (RT_FAILURE(rc))
1603 LogRel(("SUPR3HardenedVerifyFile: %s: Verification of \"%s\" failed, rc=%Rrc\n", pszMsg, pszFilename, rc));
1604 return rc;
1605#else
1606 return VINF_SUCCESS;
1607#endif
1608}
1609
1610
1611SUPR3DECL(int) SUPR3HardenedVerifySelf(const char *pszArgv0, bool fInternal, PRTERRINFO pErrInfo)
1612{
1613 /*
1614 * Quick input validation.
1615 */
1616 AssertPtr(pszArgv0);
1617 RTErrInfoClear(pErrInfo);
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)))
1624 return RTErrInfoSet(pErrInfo, VERR_INTERNAL_ERROR_2, "RTProcGetExecutablePath failed");
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)
1634 return RTErrInfoSetF(pErrInfo, VERR_SUPLIB_INVALID_ARGV0_INTERNAL,
1635 "argv[0] does not match the executable image path: '%s' != '%s'", pszArgv0, szExecPath);
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))
1644 return RTErrInfoSetF(pErrInfo, VERR_SUPLIB_INVALID_ARGV0_INTERNAL,
1645 "RTPathAppPrivateArch failed with rc=%Rrc", rc);
1646 size_t cchAppPrivateArch = strlen(szAppPrivateArch);
1647 if ( cchAppPrivateArch >= strlen(szExecPath)
1648 || !RTPATH_IS_SLASH(szExecPath[cchAppPrivateArch]))
1649 return RTErrInfoSet(pErrInfo, VERR_SUPLIB_INVALID_INTERNAL_APP_DIR,
1650 "Internal executable does reside under RTPathAppPrivateArch");
1651 szExecPath[cchAppPrivateArch] = '\0';
1652 if (RTPathCompare(szExecPath, szAppPrivateArch) != 0)
1653 return RTErrInfoSet(pErrInfo, VERR_SUPLIB_INVALID_INTERNAL_APP_DIR,
1654 "Internal executable does reside under RTPathAppPrivateArch");
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 */
1662 rc = supR3HardenedVerifyFile(szExecPath, RTHCUINTPTR_MAX, false /*fMaybe3rdParty*/, pErrInfo);
1663 if (RT_FAILURE(rc))
1664 return rc;
1665#endif
1666
1667 return VINF_SUCCESS;
1668}
1669
1670
1671SUPR3DECL(int) SUPR3HardenedVerifyDir(const char *pszDirPath, bool fRecursive, bool fCheckFiles, PRTERRINFO pErrInfo)
1672{
1673 /*
1674 * Quick input validation
1675 */
1676 AssertPtr(pszDirPath);
1677 RTErrInfoClear(pErrInfo);
1678
1679 /*
1680 * Only do the actual check in hardened builds.
1681 */
1682#ifdef VBOX_WITH_HARDENING
1683 int rc = supR3HardenedVerifyDir(pszDirPath, fRecursive, fCheckFiles, pErrInfo);
1684 if (RT_FAILURE(rc) && !RTErrInfoIsSet(pErrInfo))
1685 LogRel(("supR3HardenedVerifyDir: Verification of \"%s\" failed, rc=%Rrc\n", pszDirPath, rc));
1686 return rc;
1687#else
1688 NOREF(pszDirPath); NOREF(fRecursive); NOREF(fCheckFiles);
1689 return VINF_SUCCESS;
1690#endif
1691}
1692
1693
1694SUPR3DECL(int) SUPR3HardenedVerifyPlugIn(const char *pszFilename, PRTERRINFO pErrInfo)
1695{
1696 /*
1697 * Quick input validation
1698 */
1699 AssertPtr(pszFilename);
1700 RTErrInfoClear(pErrInfo);
1701
1702 /*
1703 * Only do the actual check in hardened builds.
1704 */
1705#ifdef VBOX_WITH_HARDENING
1706 int rc = supR3HardenedVerifyFile(pszFilename, RTHCUINTPTR_MAX, true /*fMaybe3rdParty*/, pErrInfo);
1707 if (RT_FAILURE(rc) && !RTErrInfoIsSet(pErrInfo))
1708 LogRel(("supR3HardenedVerifyFile: Verification of \"%s\" failed, rc=%Rrc\n", pszFilename, rc));
1709 return rc;
1710#else
1711 RT_NOREF1(pszFilename);
1712 return VINF_SUCCESS;
1713#endif
1714}
1715
1716
1717SUPR3DECL(int) SUPR3GipGetPhys(PRTHCPHYS pHCPhys)
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
1728
1729SUPR3DECL(int) SUPR3QueryVTxSupported(const char **ppszWhy)
1730{
1731 *ppszWhy = NULL;
1732#ifdef RT_OS_LINUX
1733 return suplibOsQueryVTxSupported(ppszWhy);
1734#else
1735 return VINF_SUCCESS;
1736#endif
1737}
1738
1739
1740SUPR3DECL(int) SUPR3QueryVTCaps(uint32_t *pfCaps)
1741{
1742 AssertPtrReturn(pfCaps, VERR_INVALID_POINTER);
1743
1744 *pfCaps = 0;
1745
1746 int rc;
1747 if (!g_supLibData.fDriverless)
1748 {
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);
1761 if (RT_SUCCESS(rc))
1762 {
1763 rc = Req.Hdr.rc;
1764 if (RT_SUCCESS(rc))
1765 *pfCaps = Req.u.Out.fCaps;
1766 }
1767 }
1768 /*
1769 * Fail this call in driverless mode.
1770 */
1771 else
1772 rc = VERR_SUP_DRIVERLESS;
1773 return rc;
1774}
1775
1776
1777SUPR3DECL(bool) SUPR3IsNemSupportedWhenNoVtxOrAmdV(void)
1778{
1779#if defined(RT_OS_WINDOWS) || defined(RT_OS_DARWIN)
1780 return suplibOsIsNemSupportedWhenNoVtxOrAmdV();
1781#else
1782 return false;
1783#endif
1784}
1785
1786
1787SUPR3DECL(int) SUPR3QueryMicrocodeRev(uint32_t *uMicrocodeRev)
1788{
1789 AssertPtrReturn(uMicrocodeRev, VERR_INVALID_POINTER);
1790
1791 *uMicrocodeRev = 0;
1792
1793 int rc;
1794 if (!g_supLibData.fDriverless)
1795 {
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);
1808 if (RT_SUCCESS(rc))
1809 {
1810 rc = Req.Hdr.rc;
1811 if (RT_SUCCESS(rc))
1812 *uMicrocodeRev = Req.u.Out.MicrocodeRev;
1813 }
1814 }
1815 /*
1816 * Just fail the call in driverless mode.
1817 */
1818 else
1819 rc = VERR_SUP_DRIVERLESS;
1820 return rc;
1821}
1822
1823
1824SUPR3DECL(int) SUPR3TracerOpen(uint32_t uCookie, uintptr_t uArg)
1825{
1826 /* fake */
1827 if (RT_UNLIKELY(g_uSupFakeMode))
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 */
1852 if (RT_UNLIKELY(g_uSupFakeMode))
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 */
1875 if (RT_UNLIKELY(g_uSupFakeMode))
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
1902
1903
1904typedef struct SUPDRVTRACERSTRTAB
1905{
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.
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.
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);
1940}
1941
1942
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.
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.
1956 */
1957static PSUPDRVTRACERSTRTAB supr3TracerCreateStrTab(PVTGPROBELOC32 paProbeLocs32,
1958 PVTGPROBELOC64 paProbeLocs64,
1959 uint32_t cProbeLocs,
1960 RTUINTPTR offDelta,
1961 bool f32Bit)
1962{
1963 if (cProbeLocs > _128K)
1964 return NULL;
1965
1966 /*
1967 * Allocate the string table structures.
1968 */
1969 size_t cbThis = RT_UOFFSETOF_DYN(SUPDRVTRACERSTRTAB, apszOrgFunctions[cProbeLocs]);
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{
2069 /* Validate input. */
2070 NOREF(hModNative);
2071 AssertPtrReturn(pVtgHdr, VERR_INVALID_POINTER);
2072 AssertReturn(!memcmp(pVtgHdr->szMagic, VTGOBJHDR_MAGIC, sizeof(pVtgHdr->szMagic)), VERR_SUPDRV_VTG_MAGIC);
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
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
2090 u64Tmp = pVtgHdr->uProbeLocs.u64 - uVtgHdrAddr;
2091 if ((int64_t)u64Tmp != (int32_t)u64Tmp)
2092 {
2093 LogRel(("SUPR3TracerRegisterModule: VERR_SUPDRV_VTG_BAD_HDR_PTR - u64Tmp=%#llx uProbeLocs=%#llx uVtgHdrAddr=%RTptr\n",
2094 u64Tmp, pVtgHdr->uProbeLocs.u64, uVtgHdrAddr));
2095 return VERR_SUPDRV_VTG_BAD_HDR_PTR;
2096 }
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 */
2107 if (RT_UNLIKELY(g_uSupFakeMode))
2108 return VINF_SUCCESS;
2109
2110 /*
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 */
2114 uint32_t const cProbeLocs = pVtgHdr->cbProbeLocs
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 /*
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;
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;
2139 Req.u.In.fFlags = fFlags;
2140
2141 memcpy(Req.u.In.szName, pszModule, cchModule + 1);
2142 if (!RTPathHasSuffix(Req.u.In.szName))
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;
2168
2169 supr3TracerDestroyStrTab(pStrTab, (PVTGPROBELOC32)paProbeLocs, (PVTGPROBELOC64)paProbeLocs,
2170 cProbeLocs, pVtgHdr->cBits == 32);
2171 return rc;
2172}
2173
2174
2175SUPR3DECL(int) SUPR3TracerDeregisterModule(struct VTGOBJHDR *pVtgHdr)
2176{
2177 /* Validate input. */
2178 AssertPtrReturn(pVtgHdr, VERR_INVALID_POINTER);
2179 AssertReturn(!memcmp(pVtgHdr->szMagic, VTGOBJHDR_MAGIC, sizeof(pVtgHdr->szMagic)), VERR_SUPDRV_VTG_MAGIC);
2180
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 */
2191 if (RT_UNLIKELY(g_uSupFakeMode))
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
2212
2213DECLASM(void) suplibTracerFireProbe(PVTGPROBELOC pProbeLoc, PSUPTRACERUMODFIREPROBE pReq)
2214{
2215 RT_NOREF1(pProbeLoc);
2216
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
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
2315
2316SUPR3DECL(int) SUPR3ResumeSuspendedKeyboards(void)
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;
2325 Req.cbIn = SUP_IOCTL_RESUME_SUSPENDED_KBDS_SIZE_IN;
2326 Req.cbOut = SUP_IOCTL_RESUME_SUSPENDED_KBDS_SIZE_OUT;
2327 Req.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2328 Req.rc = VERR_INTERNAL_ERROR;
2329 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_RESUME_SUSPENDED_KBDS, &Req, SUP_IOCTL_RESUME_SUSPENDED_KBDS_SIZE);
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
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
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
2374 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TSC_READ, &Req, SUP_IOCTL_TSC_READ_SIZE);
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
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
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.

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