VirtualBox

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

Last change on this file since 80651 was 80281, checked in by vboxsync, 5 years ago

VMM,++: Refactoring code to use VMMC & VMMCPUCC. bugref:9217

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