VirtualBox

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

Last change on this file since 95512 was 93899, checked in by vboxsync, 3 years ago

SUPLib: Turn on DRIVERLESS flag when VBOX_WITH_DRIVERLESS_FORCED is defined. bugref:9898

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