VirtualBox

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

Last change on this file since 92651 was 92613, checked in by vboxsync, 3 years ago

SUP,IPRT,++: Adding SUPSECMAIN_FLAGS_DRIVERLESS_IEM_ALLOWED and SUPR3INIT_F_DRIVERLESS_NEM_FALLBACK to SUPLib and RTR3INIT_FLAGS_TRY_SUPLIB to RTR3Init*, the latter probably reflects the actual state there a lot better. Currently only the TRY_SUPLIB option works, the other two aren't really implemented in SUPLib yet. bugref:10138

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