VirtualBox

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

Last change on this file since 63463 was 62877, checked in by vboxsync, 8 years ago

HostDrivers: gcc warnings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 80.1 KB
Line 
1/* $Id: SUPLib.cpp 62877 2016-08-02 15:05:45Z vboxsync $ */
2/** @file
3 * VirtualBox Support Library - Common code.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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_OFFSETOF(SUPGLOBALINFOPAGE, u64NanoTSLastUpdateHz) & 0x7));
217 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs) & 0x1f));
218 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs[1]) & 0x1f));
219 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs[0].u64NanoTS) & 0x7));
220 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs[0].u64TSC) & 0x7));
221 Assert(!(RT_OFFSETOF(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) == 0x00230000
279 ? 0x00230003
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 if (RT_LIKELY(uOperation == SUP_VMMR0_DO_RAW_RUN))
616 return suplibOsIOCtlFast(&g_supLibData, SUP_IOCTL_FAST_DO_RAW_RUN, idCpu);
617 if (RT_LIKELY(uOperation == SUP_VMMR0_DO_HM_RUN))
618 return suplibOsIOCtlFast(&g_supLibData, SUP_IOCTL_FAST_DO_HM_RUN, idCpu);
619 if (RT_LIKELY(uOperation == SUP_VMMR0_DO_NOP))
620 return suplibOsIOCtlFast(&g_supLibData, SUP_IOCTL_FAST_DO_NOP, idCpu);
621
622 AssertMsgFailed(("%#x\n", uOperation));
623 return VERR_INTERNAL_ERROR;
624}
625
626
627SUPR3DECL(int) SUPR3CallVMMR0Ex(PVMR0 pVMR0, VMCPUID idCpu, unsigned uOperation, uint64_t u64Arg, PSUPVMMR0REQHDR pReqHdr)
628{
629 /*
630 * The following operations don't belong here.
631 */
632 AssertMsgReturn( uOperation != SUP_VMMR0_DO_RAW_RUN
633 && uOperation != SUP_VMMR0_DO_HM_RUN
634 && uOperation != SUP_VMMR0_DO_NOP,
635 ("%#x\n", uOperation),
636 VERR_INTERNAL_ERROR);
637
638 /* fake */
639 if (RT_UNLIKELY(g_uSupFakeMode))
640 return supCallVMMR0ExFake(pVMR0, uOperation, u64Arg, pReqHdr);
641
642 int rc;
643 if (!pReqHdr)
644 {
645 /* no data. */
646 SUPCALLVMMR0 Req;
647 Req.Hdr.u32Cookie = g_u32Cookie;
648 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
649 Req.Hdr.cbIn = SUP_IOCTL_CALL_VMMR0_SIZE_IN(0);
650 Req.Hdr.cbOut = SUP_IOCTL_CALL_VMMR0_SIZE_OUT(0);
651 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
652 Req.Hdr.rc = VERR_INTERNAL_ERROR;
653 Req.u.In.pVMR0 = pVMR0;
654 Req.u.In.idCpu = idCpu;
655 Req.u.In.uOperation = uOperation;
656 Req.u.In.u64Arg = u64Arg;
657 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_VMMR0(0), &Req, SUP_IOCTL_CALL_VMMR0_SIZE(0));
658 if (RT_SUCCESS(rc))
659 rc = Req.Hdr.rc;
660 }
661 else if (SUP_IOCTL_CALL_VMMR0_SIZE(pReqHdr->cbReq) < _4K) /* FreeBSD won't copy more than 4K. */
662 {
663 AssertPtrReturn(pReqHdr, VERR_INVALID_POINTER);
664 AssertReturn(pReqHdr->u32Magic == SUPVMMR0REQHDR_MAGIC, VERR_INVALID_MAGIC);
665 const size_t cbReq = pReqHdr->cbReq;
666
667 PSUPCALLVMMR0 pReq = (PSUPCALLVMMR0)alloca(SUP_IOCTL_CALL_VMMR0_SIZE(cbReq));
668 pReq->Hdr.u32Cookie = g_u32Cookie;
669 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
670 pReq->Hdr.cbIn = SUP_IOCTL_CALL_VMMR0_SIZE_IN(cbReq);
671 pReq->Hdr.cbOut = SUP_IOCTL_CALL_VMMR0_SIZE_OUT(cbReq);
672 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
673 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
674 pReq->u.In.pVMR0 = pVMR0;
675 pReq->u.In.idCpu = idCpu;
676 pReq->u.In.uOperation = uOperation;
677 pReq->u.In.u64Arg = u64Arg;
678 memcpy(&pReq->abReqPkt[0], pReqHdr, cbReq);
679 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_VMMR0(cbReq), pReq, SUP_IOCTL_CALL_VMMR0_SIZE(cbReq));
680 if (RT_SUCCESS(rc))
681 rc = pReq->Hdr.rc;
682 memcpy(pReqHdr, &pReq->abReqPkt[0], cbReq);
683 }
684 else if (pReqHdr->cbReq <= _512K)
685 {
686 AssertPtrReturn(pReqHdr, VERR_INVALID_POINTER);
687 AssertReturn(pReqHdr->u32Magic == SUPVMMR0REQHDR_MAGIC, VERR_INVALID_MAGIC);
688 const size_t cbReq = pReqHdr->cbReq;
689
690 PSUPCALLVMMR0 pReq = (PSUPCALLVMMR0)RTMemTmpAlloc(SUP_IOCTL_CALL_VMMR0_BIG_SIZE(cbReq));
691 pReq->Hdr.u32Cookie = g_u32Cookie;
692 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
693 pReq->Hdr.cbIn = SUP_IOCTL_CALL_VMMR0_BIG_SIZE_IN(cbReq);
694 pReq->Hdr.cbOut = SUP_IOCTL_CALL_VMMR0_BIG_SIZE_OUT(cbReq);
695 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
696 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
697 pReq->u.In.pVMR0 = pVMR0;
698 pReq->u.In.idCpu = idCpu;
699 pReq->u.In.uOperation = uOperation;
700 pReq->u.In.u64Arg = u64Arg;
701 memcpy(&pReq->abReqPkt[0], pReqHdr, cbReq);
702 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_VMMR0_BIG, pReq, SUP_IOCTL_CALL_VMMR0_BIG_SIZE(cbReq));
703 if (RT_SUCCESS(rc))
704 rc = pReq->Hdr.rc;
705 memcpy(pReqHdr, &pReq->abReqPkt[0], cbReq);
706 RTMemTmpFree(pReq);
707 }
708 else
709 AssertMsgFailedReturn(("cbReq=%#x\n", pReqHdr->cbReq), VERR_OUT_OF_RANGE);
710 return rc;
711}
712
713
714SUPR3DECL(int) SUPR3CallVMMR0(PVMR0 pVMR0, VMCPUID idCpu, unsigned uOperation, void *pvArg)
715{
716 /*
717 * The following operations don't belong here.
718 */
719 AssertMsgReturn( uOperation != SUP_VMMR0_DO_RAW_RUN
720 && uOperation != SUP_VMMR0_DO_HM_RUN
721 && uOperation != SUP_VMMR0_DO_NOP,
722 ("%#x\n", uOperation),
723 VERR_INTERNAL_ERROR);
724 return SUPR3CallVMMR0Ex(pVMR0, idCpu, uOperation, (uintptr_t)pvArg, NULL);
725}
726
727
728SUPR3DECL(int) SUPR3SetVMForFastIOCtl(PVMR0 pVMR0)
729{
730 if (RT_UNLIKELY(g_uSupFakeMode))
731 return VINF_SUCCESS;
732
733 SUPSETVMFORFAST Req;
734 Req.Hdr.u32Cookie = g_u32Cookie;
735 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
736 Req.Hdr.cbIn = SUP_IOCTL_SET_VM_FOR_FAST_SIZE_IN;
737 Req.Hdr.cbOut = SUP_IOCTL_SET_VM_FOR_FAST_SIZE_OUT;
738 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
739 Req.Hdr.rc = VERR_INTERNAL_ERROR;
740 Req.u.In.pVMR0 = pVMR0;
741 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_SET_VM_FOR_FAST, &Req, SUP_IOCTL_SET_VM_FOR_FAST_SIZE);
742 if (RT_SUCCESS(rc))
743 rc = Req.Hdr.rc;
744 return rc;
745}
746
747
748SUPR3DECL(int) SUPR3CallR0Service(const char *pszService, size_t cchService, uint32_t uOperation, uint64_t u64Arg, PSUPR0SERVICEREQHDR pReqHdr)
749{
750 AssertReturn(cchService < RT_SIZEOFMEMB(SUPCALLSERVICE, u.In.szName), VERR_INVALID_PARAMETER);
751 Assert(strlen(pszService) == cchService);
752
753 /* fake */
754 if (RT_UNLIKELY(g_uSupFakeMode))
755 return VERR_NOT_SUPPORTED;
756
757 int rc;
758 if (!pReqHdr)
759 {
760 /* no data. */
761 SUPCALLSERVICE Req;
762 Req.Hdr.u32Cookie = g_u32Cookie;
763 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
764 Req.Hdr.cbIn = SUP_IOCTL_CALL_SERVICE_SIZE_IN(0);
765 Req.Hdr.cbOut = SUP_IOCTL_CALL_SERVICE_SIZE_OUT(0);
766 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
767 Req.Hdr.rc = VERR_INTERNAL_ERROR;
768 memcpy(Req.u.In.szName, pszService, cchService);
769 Req.u.In.szName[cchService] = '\0';
770 Req.u.In.uOperation = uOperation;
771 Req.u.In.u64Arg = u64Arg;
772 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_SERVICE(0), &Req, SUP_IOCTL_CALL_SERVICE_SIZE(0));
773 if (RT_SUCCESS(rc))
774 rc = Req.Hdr.rc;
775 }
776 else if (SUP_IOCTL_CALL_SERVICE_SIZE(pReqHdr->cbReq) < _4K) /* FreeBSD won't copy more than 4K. */
777 {
778 AssertPtrReturn(pReqHdr, VERR_INVALID_POINTER);
779 AssertReturn(pReqHdr->u32Magic == SUPR0SERVICEREQHDR_MAGIC, VERR_INVALID_MAGIC);
780 const size_t cbReq = pReqHdr->cbReq;
781
782 PSUPCALLSERVICE pReq = (PSUPCALLSERVICE)alloca(SUP_IOCTL_CALL_SERVICE_SIZE(cbReq));
783 pReq->Hdr.u32Cookie = g_u32Cookie;
784 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
785 pReq->Hdr.cbIn = SUP_IOCTL_CALL_SERVICE_SIZE_IN(cbReq);
786 pReq->Hdr.cbOut = SUP_IOCTL_CALL_SERVICE_SIZE_OUT(cbReq);
787 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
788 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
789 memcpy(pReq->u.In.szName, pszService, cchService);
790 pReq->u.In.szName[cchService] = '\0';
791 pReq->u.In.uOperation = uOperation;
792 pReq->u.In.u64Arg = u64Arg;
793 memcpy(&pReq->abReqPkt[0], pReqHdr, cbReq);
794 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_SERVICE(cbReq), pReq, SUP_IOCTL_CALL_SERVICE_SIZE(cbReq));
795 if (RT_SUCCESS(rc))
796 rc = pReq->Hdr.rc;
797 memcpy(pReqHdr, &pReq->abReqPkt[0], cbReq);
798 }
799 else /** @todo may have to remove the size limits one this request... */
800 AssertMsgFailedReturn(("cbReq=%#x\n", pReqHdr->cbReq), VERR_INTERNAL_ERROR);
801 return rc;
802}
803
804
805/**
806 * Worker for the SUPR3Logger* APIs.
807 *
808 * @returns VBox status code.
809 * @param enmWhich Which logger.
810 * @param fWhat What to do with the logger.
811 * @param pszFlags The flags settings.
812 * @param pszGroups The groups settings.
813 * @param pszDest The destination specificier.
814 */
815static int supR3LoggerSettings(SUPLOGGER enmWhich, uint32_t fWhat, const char *pszFlags, const char *pszGroups, const char *pszDest)
816{
817 uint32_t const cchFlags = pszFlags ? (uint32_t)strlen(pszFlags) : 0;
818 uint32_t const cchGroups = pszGroups ? (uint32_t)strlen(pszGroups) : 0;
819 uint32_t const cchDest = pszDest ? (uint32_t)strlen(pszDest) : 0;
820 uint32_t const cbStrTab = cchFlags + !!cchFlags
821 + cchGroups + !!cchGroups
822 + cchDest + !!cchDest
823 + (!cchFlags && !cchGroups && !cchDest);
824
825 PSUPLOGGERSETTINGS pReq = (PSUPLOGGERSETTINGS)alloca(SUP_IOCTL_LOGGER_SETTINGS_SIZE(cbStrTab));
826 pReq->Hdr.u32Cookie = g_u32Cookie;
827 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
828 pReq->Hdr.cbIn = SUP_IOCTL_LOGGER_SETTINGS_SIZE_IN(cbStrTab);
829 pReq->Hdr.cbOut = SUP_IOCTL_LOGGER_SETTINGS_SIZE_OUT;
830 pReq->Hdr.fFlags= SUPREQHDR_FLAGS_DEFAULT;
831 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
832 switch (enmWhich)
833 {
834 case SUPLOGGER_DEBUG: pReq->u.In.fWhich = SUPLOGGERSETTINGS_WHICH_DEBUG; break;
835 case SUPLOGGER_RELEASE: pReq->u.In.fWhich = SUPLOGGERSETTINGS_WHICH_RELEASE; break;
836 default:
837 return VERR_INVALID_PARAMETER;
838 }
839 pReq->u.In.fWhat = fWhat;
840
841 uint32_t off = 0;
842 if (cchFlags)
843 {
844 pReq->u.In.offFlags = off;
845 memcpy(&pReq->u.In.szStrings[off], pszFlags, cchFlags + 1);
846 off += cchFlags + 1;
847 }
848 else
849 pReq->u.In.offFlags = cbStrTab - 1;
850
851 if (cchGroups)
852 {
853 pReq->u.In.offGroups = off;
854 memcpy(&pReq->u.In.szStrings[off], pszGroups, cchGroups + 1);
855 off += cchGroups + 1;
856 }
857 else
858 pReq->u.In.offGroups = cbStrTab - 1;
859
860 if (cchDest)
861 {
862 pReq->u.In.offDestination = off;
863 memcpy(&pReq->u.In.szStrings[off], pszDest, cchDest + 1);
864 off += cchDest + 1;
865 }
866 else
867 pReq->u.In.offDestination = cbStrTab - 1;
868
869 if (!off)
870 {
871 pReq->u.In.szStrings[0] = '\0';
872 off++;
873 }
874 Assert(off == cbStrTab);
875 Assert(pReq->u.In.szStrings[cbStrTab - 1] == '\0');
876
877
878 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LOGGER_SETTINGS(cbStrTab), pReq, SUP_IOCTL_LOGGER_SETTINGS_SIZE(cbStrTab));
879 if (RT_SUCCESS(rc))
880 rc = pReq->Hdr.rc;
881 return rc;
882}
883
884
885SUPR3DECL(int) SUPR3LoggerSettings(SUPLOGGER enmWhich, const char *pszFlags, const char *pszGroups, const char *pszDest)
886{
887 return supR3LoggerSettings(enmWhich, SUPLOGGERSETTINGS_WHAT_SETTINGS, pszFlags, pszGroups, pszDest);
888}
889
890
891SUPR3DECL(int) SUPR3LoggerCreate(SUPLOGGER enmWhich, const char *pszFlags, const char *pszGroups, const char *pszDest)
892{
893 return supR3LoggerSettings(enmWhich, SUPLOGGERSETTINGS_WHAT_CREATE, pszFlags, pszGroups, pszDest);
894}
895
896
897SUPR3DECL(int) SUPR3LoggerDestroy(SUPLOGGER enmWhich)
898{
899 return supR3LoggerSettings(enmWhich, SUPLOGGERSETTINGS_WHAT_DESTROY, NULL, NULL, NULL);
900}
901
902
903SUPR3DECL(int) SUPR3PageAlloc(size_t cPages, void **ppvPages)
904{
905 /*
906 * Validate.
907 */
908 AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
909 *ppvPages = NULL;
910 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
911
912 /*
913 * Call OS specific worker.
914 */
915 return suplibOsPageAlloc(&g_supLibData, cPages, ppvPages);
916}
917
918
919SUPR3DECL(int) SUPR3PageFree(void *pvPages, size_t cPages)
920{
921 /*
922 * Validate.
923 */
924 AssertPtrReturn(pvPages, VERR_INVALID_POINTER);
925 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
926
927 /*
928 * Call OS specific worker.
929 */
930 return suplibOsPageFree(&g_supLibData, pvPages, cPages);
931}
932
933
934/**
935 * Locks down the physical memory backing a virtual memory
936 * range in the current process.
937 *
938 * @returns VBox status code.
939 * @param pvStart Start of virtual memory range.
940 * Must be page aligned.
941 * @param cPages Number of pages.
942 * @param paPages Where to store the physical page addresses returned.
943 * On entry this will point to an array of with cbMemory >> PAGE_SHIFT entries.
944 */
945SUPR3DECL(int) supR3PageLock(void *pvStart, size_t cPages, PSUPPAGE paPages)
946{
947 /*
948 * Validate.
949 */
950 AssertPtr(pvStart);
951 AssertMsg(RT_ALIGN_P(pvStart, PAGE_SIZE) == pvStart, ("pvStart (%p) must be page aligned\n", pvStart));
952 AssertPtr(paPages);
953
954 /* fake */
955 if (RT_UNLIKELY(g_uSupFakeMode))
956 {
957 RTHCPHYS Phys = (uintptr_t)pvStart + PAGE_SIZE * 1024;
958 size_t iPage = cPages;
959 while (iPage-- > 0)
960 paPages[iPage].Phys = Phys + (iPage << PAGE_SHIFT);
961 return VINF_SUCCESS;
962 }
963
964 /*
965 * Issue IOCtl to the SUPDRV kernel module.
966 */
967 int rc;
968 PSUPPAGELOCK pReq = (PSUPPAGELOCK)RTMemTmpAllocZ(SUP_IOCTL_PAGE_LOCK_SIZE(cPages));
969 if (RT_LIKELY(pReq))
970 {
971 pReq->Hdr.u32Cookie = g_u32Cookie;
972 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
973 pReq->Hdr.cbIn = SUP_IOCTL_PAGE_LOCK_SIZE_IN;
974 pReq->Hdr.cbOut = SUP_IOCTL_PAGE_LOCK_SIZE_OUT(cPages);
975 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
976 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
977 pReq->u.In.pvR3 = pvStart;
978 pReq->u.In.cPages = (uint32_t)cPages; AssertRelease(pReq->u.In.cPages == cPages);
979 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_LOCK, pReq, SUP_IOCTL_PAGE_LOCK_SIZE(cPages));
980 if (RT_SUCCESS(rc))
981 rc = pReq->Hdr.rc;
982 if (RT_SUCCESS(rc))
983 {
984 for (uint32_t iPage = 0; iPage < cPages; iPage++)
985 {
986 paPages[iPage].uReserved = 0;
987 paPages[iPage].Phys = pReq->u.Out.aPages[iPage];
988 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
989 }
990 }
991 RTMemTmpFree(pReq);
992 }
993 else
994 rc = VERR_NO_TMP_MEMORY;
995
996 return rc;
997}
998
999
1000/**
1001 * Releases locked down pages.
1002 *
1003 * @returns VBox status code.
1004 * @param pvStart Start of virtual memory range previously locked
1005 * down by SUPPageLock().
1006 */
1007SUPR3DECL(int) supR3PageUnlock(void *pvStart)
1008{
1009 /*
1010 * Validate.
1011 */
1012 AssertPtr(pvStart);
1013 AssertMsg(RT_ALIGN_P(pvStart, PAGE_SIZE) == pvStart, ("pvStart (%p) must be page aligned\n", pvStart));
1014
1015 /* fake */
1016 if (RT_UNLIKELY(g_uSupFakeMode))
1017 return VINF_SUCCESS;
1018
1019 /*
1020 * Issue IOCtl to the SUPDRV kernel module.
1021 */
1022 SUPPAGEUNLOCK Req;
1023 Req.Hdr.u32Cookie = g_u32Cookie;
1024 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1025 Req.Hdr.cbIn = SUP_IOCTL_PAGE_UNLOCK_SIZE_IN;
1026 Req.Hdr.cbOut = SUP_IOCTL_PAGE_UNLOCK_SIZE_OUT;
1027 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1028 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1029 Req.u.In.pvR3 = pvStart;
1030 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_UNLOCK, &Req, SUP_IOCTL_PAGE_UNLOCK_SIZE);
1031 if (RT_SUCCESS(rc))
1032 rc = Req.Hdr.rc;
1033 return rc;
1034}
1035
1036
1037SUPR3DECL(int) SUPR3LockDownLoader(PRTERRINFO pErrInfo)
1038{
1039 /* fake */
1040 if (RT_UNLIKELY(g_uSupFakeMode))
1041 return VINF_SUCCESS;
1042
1043 /*
1044 * Lock down the module loader interface.
1045 */
1046 SUPREQHDR ReqHdr;
1047 ReqHdr.u32Cookie = g_u32Cookie;
1048 ReqHdr.u32SessionCookie = g_u32SessionCookie;
1049 ReqHdr.cbIn = SUP_IOCTL_LDR_LOCK_DOWN_SIZE_IN;
1050 ReqHdr.cbOut = SUP_IOCTL_LDR_LOCK_DOWN_SIZE_OUT;
1051 ReqHdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1052 ReqHdr.rc = VERR_INTERNAL_ERROR;
1053 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_LOCK_DOWN, &ReqHdr, SUP_IOCTL_LDR_LOCK_DOWN_SIZE);
1054 if (RT_FAILURE(rc))
1055 return RTErrInfoSetF(pErrInfo, rc,
1056 "SUPR3LockDownLoader: SUP_IOCTL_LDR_LOCK_DOWN ioctl returned %Rrc", rc);
1057
1058 return ReqHdr.rc;
1059}
1060
1061
1062/**
1063 * Fallback for SUPR3PageAllocEx on systems where RTR0MemObjPhysAllocNC isn't
1064 * supported.
1065 */
1066static int supPagePageAllocNoKernelFallback(size_t cPages, void **ppvPages, PSUPPAGE paPages)
1067{
1068 int rc = suplibOsPageAlloc(&g_supLibData, cPages, ppvPages);
1069 if (RT_SUCCESS(rc))
1070 {
1071 if (!paPages)
1072 paPages = (PSUPPAGE)alloca(sizeof(paPages[0]) * cPages);
1073 rc = supR3PageLock(*ppvPages, cPages, paPages);
1074 if (RT_FAILURE(rc))
1075 suplibOsPageFree(&g_supLibData, *ppvPages, cPages);
1076 }
1077 return rc;
1078}
1079
1080
1081SUPR3DECL(int) SUPR3PageAllocEx(size_t cPages, uint32_t fFlags, void **ppvPages, PRTR0PTR pR0Ptr, PSUPPAGE paPages)
1082{
1083 /*
1084 * Validate.
1085 */
1086 AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
1087 *ppvPages = NULL;
1088 AssertPtrNullReturn(pR0Ptr, VERR_INVALID_POINTER);
1089 if (pR0Ptr)
1090 *pR0Ptr = NIL_RTR0PTR;
1091 AssertPtrNullReturn(paPages, VERR_INVALID_POINTER);
1092 AssertMsgReturn(cPages > 0 && cPages <= VBOX_MAX_ALLOC_PAGE_COUNT, ("cPages=%zu\n", cPages), VERR_PAGE_COUNT_OUT_OF_RANGE);
1093 AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
1094
1095 /* fake */
1096 if (RT_UNLIKELY(g_uSupFakeMode))
1097 {
1098 void *pv = RTMemPageAllocZ(cPages * PAGE_SIZE);
1099 if (!pv)
1100 return VERR_NO_MEMORY;
1101 *ppvPages = pv;
1102 if (pR0Ptr)
1103 *pR0Ptr = (RTR0PTR)pv;
1104 if (paPages)
1105 for (size_t iPage = 0; iPage < cPages; iPage++)
1106 {
1107 paPages[iPage].uReserved = 0;
1108 paPages[iPage].Phys = (iPage + 4321) << PAGE_SHIFT;
1109 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
1110 }
1111 return VINF_SUCCESS;
1112 }
1113
1114 /*
1115 * Use fallback for non-R0 mapping?
1116 */
1117 if ( !pR0Ptr
1118 && !g_fSupportsPageAllocNoKernel)
1119 return supPagePageAllocNoKernelFallback(cPages, ppvPages, paPages);
1120
1121 /*
1122 * Issue IOCtl to the SUPDRV kernel module.
1123 */
1124 int rc;
1125 PSUPPAGEALLOCEX pReq = (PSUPPAGEALLOCEX)RTMemTmpAllocZ(SUP_IOCTL_PAGE_ALLOC_EX_SIZE(cPages));
1126 if (pReq)
1127 {
1128 pReq->Hdr.u32Cookie = g_u32Cookie;
1129 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
1130 pReq->Hdr.cbIn = SUP_IOCTL_PAGE_ALLOC_EX_SIZE_IN;
1131 pReq->Hdr.cbOut = SUP_IOCTL_PAGE_ALLOC_EX_SIZE_OUT(cPages);
1132 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
1133 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
1134 pReq->u.In.cPages = (uint32_t)cPages; AssertRelease(pReq->u.In.cPages == cPages);
1135 pReq->u.In.fKernelMapping = pR0Ptr != NULL;
1136 pReq->u.In.fUserMapping = true;
1137 pReq->u.In.fReserved0 = false;
1138 pReq->u.In.fReserved1 = false;
1139 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_ALLOC_EX, pReq, SUP_IOCTL_PAGE_ALLOC_EX_SIZE(cPages));
1140 if (RT_SUCCESS(rc))
1141 {
1142 rc = pReq->Hdr.rc;
1143 if (RT_SUCCESS(rc))
1144 {
1145 *ppvPages = pReq->u.Out.pvR3;
1146 if (pR0Ptr)
1147 *pR0Ptr = pReq->u.Out.pvR0;
1148 if (paPages)
1149 for (size_t iPage = 0; iPage < cPages; iPage++)
1150 {
1151 paPages[iPage].uReserved = 0;
1152 paPages[iPage].Phys = pReq->u.Out.aPages[iPage];
1153 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
1154 }
1155#ifdef RT_OS_DARWIN /* HACK ALERT! */
1156 supR3TouchPages(pReq->u.Out.pvR3, cPages);
1157#endif
1158 }
1159 else if ( rc == VERR_NOT_SUPPORTED
1160 && !pR0Ptr)
1161 {
1162 g_fSupportsPageAllocNoKernel = false;
1163 rc = supPagePageAllocNoKernelFallback(cPages, ppvPages, paPages);
1164 }
1165 }
1166
1167 RTMemTmpFree(pReq);
1168 }
1169 else
1170 rc = VERR_NO_TMP_MEMORY;
1171 return rc;
1172
1173}
1174
1175
1176SUPR3DECL(int) SUPR3PageMapKernel(void *pvR3, uint32_t off, uint32_t cb, uint32_t fFlags, PRTR0PTR pR0Ptr)
1177{
1178 /*
1179 * Validate.
1180 */
1181 AssertPtrReturn(pvR3, VERR_INVALID_POINTER);
1182 AssertPtrReturn(pR0Ptr, VERR_INVALID_POINTER);
1183 Assert(!(off & PAGE_OFFSET_MASK));
1184 Assert(!(cb & PAGE_OFFSET_MASK) && cb);
1185 Assert(!fFlags);
1186 *pR0Ptr = NIL_RTR0PTR;
1187
1188 /* fake */
1189 if (RT_UNLIKELY(g_uSupFakeMode))
1190 return VERR_NOT_SUPPORTED;
1191
1192 /*
1193 * Issue IOCtl to the SUPDRV kernel module.
1194 */
1195 SUPPAGEMAPKERNEL Req;
1196 Req.Hdr.u32Cookie = g_u32Cookie;
1197 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1198 Req.Hdr.cbIn = SUP_IOCTL_PAGE_MAP_KERNEL_SIZE_IN;
1199 Req.Hdr.cbOut = SUP_IOCTL_PAGE_MAP_KERNEL_SIZE_OUT;
1200 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1201 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1202 Req.u.In.pvR3 = pvR3;
1203 Req.u.In.offSub = off;
1204 Req.u.In.cbSub = cb;
1205 Req.u.In.fFlags = fFlags;
1206 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_MAP_KERNEL, &Req, SUP_IOCTL_PAGE_MAP_KERNEL_SIZE);
1207 if (RT_SUCCESS(rc))
1208 rc = Req.Hdr.rc;
1209 if (RT_SUCCESS(rc))
1210 *pR0Ptr = Req.u.Out.pvR0;
1211 return rc;
1212}
1213
1214
1215SUPR3DECL(int) SUPR3PageProtect(void *pvR3, RTR0PTR R0Ptr, uint32_t off, uint32_t cb, uint32_t fProt)
1216{
1217 /*
1218 * Validate.
1219 */
1220 AssertPtrReturn(pvR3, VERR_INVALID_POINTER);
1221 Assert(!(off & PAGE_OFFSET_MASK));
1222 Assert(!(cb & PAGE_OFFSET_MASK) && cb);
1223 AssertReturn(!(fProt & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC)), VERR_INVALID_PARAMETER);
1224
1225 /* fake */
1226 if (RT_UNLIKELY(g_uSupFakeMode))
1227 return RTMemProtect((uint8_t *)pvR3 + off, cb, fProt);
1228
1229 /*
1230 * Some OSes can do this from ring-3, so try that before we
1231 * issue the IOCtl to the SUPDRV kernel module.
1232 * (Yea, this isn't very nice, but just try get the job done for now.)
1233 */
1234#if !defined(RT_OS_SOLARIS)
1235 RTMemProtect((uint8_t *)pvR3 + off, cb, fProt);
1236#endif
1237
1238 SUPPAGEPROTECT Req;
1239 Req.Hdr.u32Cookie = g_u32Cookie;
1240 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1241 Req.Hdr.cbIn = SUP_IOCTL_PAGE_PROTECT_SIZE_IN;
1242 Req.Hdr.cbOut = SUP_IOCTL_PAGE_PROTECT_SIZE_OUT;
1243 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1244 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1245 Req.u.In.pvR3 = pvR3;
1246 Req.u.In.pvR0 = R0Ptr;
1247 Req.u.In.offSub = off;
1248 Req.u.In.cbSub = cb;
1249 Req.u.In.fProt = fProt;
1250 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_PROTECT, &Req, SUP_IOCTL_PAGE_PROTECT_SIZE);
1251 if (RT_SUCCESS(rc))
1252 rc = Req.Hdr.rc;
1253 return rc;
1254}
1255
1256
1257SUPR3DECL(int) SUPR3PageFreeEx(void *pvPages, size_t cPages)
1258{
1259 /*
1260 * Validate.
1261 */
1262 AssertPtrReturn(pvPages, VERR_INVALID_POINTER);
1263 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1264
1265 /* fake */
1266 if (RT_UNLIKELY(g_uSupFakeMode))
1267 {
1268 RTMemPageFree(pvPages, cPages * PAGE_SIZE);
1269 return VINF_SUCCESS;
1270 }
1271
1272 /*
1273 * Try normal free first, then if it fails check if we're using the fallback
1274 * for the allocations without kernel mappings and attempt unlocking it.
1275 */
1276 NOREF(cPages);
1277 SUPPAGEFREE Req;
1278 Req.Hdr.u32Cookie = g_u32Cookie;
1279 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1280 Req.Hdr.cbIn = SUP_IOCTL_PAGE_FREE_SIZE_IN;
1281 Req.Hdr.cbOut = SUP_IOCTL_PAGE_FREE_SIZE_OUT;
1282 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1283 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1284 Req.u.In.pvR3 = pvPages;
1285 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_FREE, &Req, SUP_IOCTL_PAGE_FREE_SIZE);
1286 if (RT_SUCCESS(rc))
1287 {
1288 rc = Req.Hdr.rc;
1289 if ( rc == VERR_INVALID_PARAMETER
1290 && !g_fSupportsPageAllocNoKernel)
1291 {
1292 int rc2 = supR3PageUnlock(pvPages);
1293 if (RT_SUCCESS(rc2))
1294 rc = suplibOsPageFree(&g_supLibData, pvPages, cPages);
1295 }
1296 }
1297 return rc;
1298}
1299
1300
1301SUPR3DECL(void *) SUPR3ContAlloc(size_t cPages, PRTR0PTR pR0Ptr, PRTHCPHYS pHCPhys)
1302{
1303 /*
1304 * Validate.
1305 */
1306 AssertPtrReturn(pHCPhys, NULL);
1307 *pHCPhys = NIL_RTHCPHYS;
1308 AssertPtrNullReturn(pR0Ptr, NULL);
1309 if (pR0Ptr)
1310 *pR0Ptr = NIL_RTR0PTR;
1311 AssertPtrNullReturn(pHCPhys, NULL);
1312 AssertMsgReturn(cPages > 0 && cPages < 256, ("cPages=%d must be > 0 and < 256\n", cPages), NULL);
1313
1314 /* fake */
1315 if (RT_UNLIKELY(g_uSupFakeMode))
1316 {
1317 void *pv = RTMemPageAllocZ(cPages * PAGE_SIZE);
1318 if (pR0Ptr)
1319 *pR0Ptr = (RTR0PTR)pv;
1320 if (pHCPhys)
1321 *pHCPhys = (uintptr_t)pv + (PAGE_SHIFT * 1024);
1322 return pv;
1323 }
1324
1325 /*
1326 * Issue IOCtl to the SUPDRV kernel module.
1327 */
1328 SUPCONTALLOC Req;
1329 Req.Hdr.u32Cookie = g_u32Cookie;
1330 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1331 Req.Hdr.cbIn = SUP_IOCTL_CONT_ALLOC_SIZE_IN;
1332 Req.Hdr.cbOut = SUP_IOCTL_CONT_ALLOC_SIZE_OUT;
1333 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1334 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1335 Req.u.In.cPages = (uint32_t)cPages;
1336 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CONT_ALLOC, &Req, SUP_IOCTL_CONT_ALLOC_SIZE);
1337 if ( RT_SUCCESS(rc)
1338 && RT_SUCCESS(Req.Hdr.rc))
1339 {
1340 *pHCPhys = Req.u.Out.HCPhys;
1341 if (pR0Ptr)
1342 *pR0Ptr = Req.u.Out.pvR0;
1343#ifdef RT_OS_DARWIN /* HACK ALERT! */
1344 supR3TouchPages(Req.u.Out.pvR3, cPages);
1345#endif
1346 return Req.u.Out.pvR3;
1347 }
1348
1349 return NULL;
1350}
1351
1352
1353SUPR3DECL(int) SUPR3ContFree(void *pv, size_t cPages)
1354{
1355 /*
1356 * Validate.
1357 */
1358 if (!pv)
1359 return VINF_SUCCESS;
1360 AssertPtrReturn(pv, VERR_INVALID_POINTER);
1361 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1362
1363 /* fake */
1364 if (RT_UNLIKELY(g_uSupFakeMode))
1365 {
1366 RTMemPageFree(pv, cPages * PAGE_SIZE);
1367 return VINF_SUCCESS;
1368 }
1369
1370 /*
1371 * Issue IOCtl to the SUPDRV kernel module.
1372 */
1373 SUPCONTFREE Req;
1374 Req.Hdr.u32Cookie = g_u32Cookie;
1375 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1376 Req.Hdr.cbIn = SUP_IOCTL_CONT_FREE_SIZE_IN;
1377 Req.Hdr.cbOut = SUP_IOCTL_CONT_FREE_SIZE_OUT;
1378 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1379 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1380 Req.u.In.pvR3 = pv;
1381 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CONT_FREE, &Req, SUP_IOCTL_CONT_FREE_SIZE);
1382 if (RT_SUCCESS(rc))
1383 rc = Req.Hdr.rc;
1384 return rc;
1385}
1386
1387
1388SUPR3DECL(int) SUPR3LowAlloc(size_t cPages, void **ppvPages, PRTR0PTR ppvPagesR0, PSUPPAGE paPages)
1389{
1390 /*
1391 * Validate.
1392 */
1393 AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
1394 *ppvPages = NULL;
1395 AssertPtrReturn(paPages, VERR_INVALID_POINTER);
1396 AssertMsgReturn(cPages > 0 && cPages < 256, ("cPages=%d must be > 0 and < 256\n", cPages), VERR_PAGE_COUNT_OUT_OF_RANGE);
1397
1398 /* fake */
1399 if (RT_UNLIKELY(g_uSupFakeMode))
1400 {
1401 *ppvPages = RTMemPageAllocZ((size_t)cPages * PAGE_SIZE);
1402 if (!*ppvPages)
1403 return VERR_NO_LOW_MEMORY;
1404
1405 /* fake physical addresses. */
1406 RTHCPHYS Phys = (uintptr_t)*ppvPages + PAGE_SIZE * 1024;
1407 size_t iPage = cPages;
1408 while (iPage-- > 0)
1409 paPages[iPage].Phys = Phys + (iPage << PAGE_SHIFT);
1410 return VINF_SUCCESS;
1411 }
1412
1413 /*
1414 * Issue IOCtl to the SUPDRV kernel module.
1415 */
1416 int rc;
1417 PSUPLOWALLOC pReq = (PSUPLOWALLOC)RTMemTmpAllocZ(SUP_IOCTL_LOW_ALLOC_SIZE(cPages));
1418 if (pReq)
1419 {
1420 pReq->Hdr.u32Cookie = g_u32Cookie;
1421 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
1422 pReq->Hdr.cbIn = SUP_IOCTL_LOW_ALLOC_SIZE_IN;
1423 pReq->Hdr.cbOut = SUP_IOCTL_LOW_ALLOC_SIZE_OUT(cPages);
1424 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
1425 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
1426 pReq->u.In.cPages = (uint32_t)cPages; AssertRelease(pReq->u.In.cPages == cPages);
1427 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LOW_ALLOC, pReq, SUP_IOCTL_LOW_ALLOC_SIZE(cPages));
1428 if (RT_SUCCESS(rc))
1429 rc = pReq->Hdr.rc;
1430 if (RT_SUCCESS(rc))
1431 {
1432 *ppvPages = pReq->u.Out.pvR3;
1433 if (ppvPagesR0)
1434 *ppvPagesR0 = pReq->u.Out.pvR0;
1435 if (paPages)
1436 for (size_t iPage = 0; iPage < cPages; iPage++)
1437 {
1438 paPages[iPage].uReserved = 0;
1439 paPages[iPage].Phys = pReq->u.Out.aPages[iPage];
1440 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
1441 Assert(paPages[iPage].Phys <= UINT32_C(0xfffff000));
1442 }
1443#ifdef RT_OS_DARWIN /* HACK ALERT! */
1444 supR3TouchPages(pReq->u.Out.pvR3, cPages);
1445#endif
1446 }
1447 RTMemTmpFree(pReq);
1448 }
1449 else
1450 rc = VERR_NO_TMP_MEMORY;
1451
1452 return rc;
1453}
1454
1455
1456SUPR3DECL(int) SUPR3LowFree(void *pv, size_t cPages)
1457{
1458 /*
1459 * Validate.
1460 */
1461 if (!pv)
1462 return VINF_SUCCESS;
1463 AssertPtrReturn(pv, VERR_INVALID_POINTER);
1464 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1465
1466 /* fake */
1467 if (RT_UNLIKELY(g_uSupFakeMode))
1468 {
1469 RTMemPageFree(pv, cPages * PAGE_SIZE);
1470 return VINF_SUCCESS;
1471 }
1472
1473 /*
1474 * Issue IOCtl to the SUPDRV kernel module.
1475 */
1476 SUPCONTFREE Req;
1477 Req.Hdr.u32Cookie = g_u32Cookie;
1478 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1479 Req.Hdr.cbIn = SUP_IOCTL_LOW_FREE_SIZE_IN;
1480 Req.Hdr.cbOut = SUP_IOCTL_LOW_FREE_SIZE_OUT;
1481 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1482 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1483 Req.u.In.pvR3 = pv;
1484 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LOW_FREE, &Req, SUP_IOCTL_LOW_FREE_SIZE);
1485 if (RT_SUCCESS(rc))
1486 rc = Req.Hdr.rc;
1487 return rc;
1488}
1489
1490
1491SUPR3DECL(int) SUPR3HardenedVerifyInit(void)
1492{
1493#ifdef RT_OS_WINDOWS
1494 if (g_cInits == 0)
1495 return suplibOsHardenedVerifyInit();
1496#endif
1497 return VINF_SUCCESS;
1498}
1499
1500
1501SUPR3DECL(int) SUPR3HardenedVerifyTerm(void)
1502{
1503#ifdef RT_OS_WINDOWS
1504 if (g_cInits == 0)
1505 return suplibOsHardenedVerifyTerm();
1506#endif
1507 return VINF_SUCCESS;
1508}
1509
1510
1511SUPR3DECL(int) SUPR3HardenedVerifyFile(const char *pszFilename, const char *pszMsg, PRTFILE phFile)
1512{
1513 /*
1514 * Quick input validation.
1515 */
1516 AssertPtr(pszFilename);
1517 AssertPtr(pszMsg);
1518 AssertReturn(!phFile, VERR_NOT_IMPLEMENTED); /** @todo Implement this. The deal is that we make sure the
1519 file is the same we verified after opening it. */
1520 RT_NOREF2(pszFilename, pszMsg);
1521
1522 /*
1523 * Only do the actual check in hardened builds.
1524 */
1525#ifdef VBOX_WITH_HARDENING
1526 int rc = supR3HardenedVerifyFixedFile(pszFilename, false /* fFatal */);
1527 if (RT_FAILURE(rc))
1528 LogRel(("SUPR3HardenedVerifyFile: %s: Verification of \"%s\" failed, rc=%Rrc\n", pszMsg, pszFilename, rc));
1529 return rc;
1530#else
1531 return VINF_SUCCESS;
1532#endif
1533}
1534
1535
1536SUPR3DECL(int) SUPR3HardenedVerifySelf(const char *pszArgv0, bool fInternal, PRTERRINFO pErrInfo)
1537{
1538 /*
1539 * Quick input validation.
1540 */
1541 AssertPtr(pszArgv0);
1542 RTErrInfoClear(pErrInfo);
1543
1544 /*
1545 * Get the executable image path as we need it for all the tests here.
1546 */
1547 char szExecPath[RTPATH_MAX];
1548 if (!RTProcGetExecutablePath(szExecPath, sizeof(szExecPath)))
1549 return RTErrInfoSet(pErrInfo, VERR_INTERNAL_ERROR_2, "RTProcGetExecutablePath failed");
1550
1551 int rc;
1552 if (fInternal)
1553 {
1554 /*
1555 * Internal applications must be launched directly without any PATH
1556 * searching involved.
1557 */
1558 if (RTPathCompare(pszArgv0, szExecPath) != 0)
1559 return RTErrInfoSetF(pErrInfo, VERR_SUPLIB_INVALID_ARGV0_INTERNAL,
1560 "argv[0] does not match the executable image path: '%s' != '%s'", pszArgv0, szExecPath);
1561
1562 /*
1563 * Internal applications must reside in or under the
1564 * RTPathAppPrivateArch directory.
1565 */
1566 char szAppPrivateArch[RTPATH_MAX];
1567 rc = RTPathAppPrivateArch(szAppPrivateArch, sizeof(szAppPrivateArch));
1568 if (RT_FAILURE(rc))
1569 return RTErrInfoSetF(pErrInfo, VERR_SUPLIB_INVALID_ARGV0_INTERNAL,
1570 "RTPathAppPrivateArch failed with rc=%Rrc", rc);
1571 size_t cchAppPrivateArch = strlen(szAppPrivateArch);
1572 if ( cchAppPrivateArch >= strlen(szExecPath)
1573 || !RTPATH_IS_SLASH(szExecPath[cchAppPrivateArch]))
1574 return RTErrInfoSet(pErrInfo, VERR_SUPLIB_INVALID_INTERNAL_APP_DIR,
1575 "Internal executable does reside under RTPathAppPrivateArch");
1576 szExecPath[cchAppPrivateArch] = '\0';
1577 if (RTPathCompare(szExecPath, szAppPrivateArch) != 0)
1578 return RTErrInfoSet(pErrInfo, VERR_SUPLIB_INVALID_INTERNAL_APP_DIR,
1579 "Internal executable does reside under RTPathAppPrivateArch");
1580 szExecPath[cchAppPrivateArch] = RTPATH_SLASH;
1581 }
1582
1583#ifdef VBOX_WITH_HARDENING
1584 /*
1585 * Verify that the image file and parent directories are sane.
1586 */
1587 rc = supR3HardenedVerifyFile(szExecPath, RTHCUINTPTR_MAX, false /*fMaybe3rdParty*/, pErrInfo);
1588 if (RT_FAILURE(rc))
1589 return rc;
1590#endif
1591
1592 return VINF_SUCCESS;
1593}
1594
1595
1596SUPR3DECL(int) SUPR3HardenedVerifyDir(const char *pszDirPath, bool fRecursive, bool fCheckFiles, PRTERRINFO pErrInfo)
1597{
1598 /*
1599 * Quick input validation
1600 */
1601 AssertPtr(pszDirPath);
1602 RTErrInfoClear(pErrInfo);
1603
1604 /*
1605 * Only do the actual check in hardened builds.
1606 */
1607#ifdef VBOX_WITH_HARDENING
1608 int rc = supR3HardenedVerifyDir(pszDirPath, fRecursive, fCheckFiles, pErrInfo);
1609 if (RT_FAILURE(rc) && !RTErrInfoIsSet(pErrInfo))
1610 LogRel(("supR3HardenedVerifyDir: Verification of \"%s\" failed, rc=%Rrc\n", pszDirPath, rc));
1611 return rc;
1612#else
1613 NOREF(pszDirPath); NOREF(fRecursive); NOREF(fCheckFiles);
1614 return VINF_SUCCESS;
1615#endif
1616}
1617
1618
1619SUPR3DECL(int) SUPR3HardenedVerifyPlugIn(const char *pszFilename, PRTERRINFO pErrInfo)
1620{
1621 /*
1622 * Quick input validation
1623 */
1624 AssertPtr(pszFilename);
1625 RTErrInfoClear(pErrInfo);
1626
1627 /*
1628 * Only do the actual check in hardened builds.
1629 */
1630#ifdef VBOX_WITH_HARDENING
1631 int rc = supR3HardenedVerifyFile(pszFilename, RTHCUINTPTR_MAX, true /*fMaybe3rdParty*/, pErrInfo);
1632 if (RT_FAILURE(rc) && !RTErrInfoIsSet(pErrInfo))
1633 LogRel(("supR3HardenedVerifyFile: Verification of \"%s\" failed, rc=%Rrc\n", pszFilename, rc));
1634 return rc;
1635#else
1636 RT_NOREF1(pszFilename);
1637 return VINF_SUCCESS;
1638#endif
1639}
1640
1641
1642SUPR3DECL(int) SUPR3GipGetPhys(PRTHCPHYS pHCPhys)
1643{
1644 if (g_pSUPGlobalInfoPage)
1645 {
1646 *pHCPhys = g_HCPhysSUPGlobalInfoPage;
1647 return VINF_SUCCESS;
1648 }
1649 *pHCPhys = NIL_RTHCPHYS;
1650 return VERR_WRONG_ORDER;
1651}
1652
1653
1654SUPR3DECL(int) SUPR3QueryVTxSupported(void)
1655{
1656#ifdef RT_OS_LINUX
1657 return suplibOsQueryVTxSupported();
1658#else
1659 return VINF_SUCCESS;
1660#endif
1661}
1662
1663
1664SUPR3DECL(int) SUPR3QueryVTCaps(uint32_t *pfCaps)
1665{
1666 AssertPtrReturn(pfCaps, VERR_INVALID_POINTER);
1667
1668 *pfCaps = 0;
1669
1670 /* fake */
1671 if (RT_UNLIKELY(g_uSupFakeMode))
1672 return VINF_SUCCESS;
1673
1674 /*
1675 * Issue IOCtl to the SUPDRV kernel module.
1676 */
1677 SUPVTCAPS Req;
1678 Req.Hdr.u32Cookie = g_u32Cookie;
1679 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1680 Req.Hdr.cbIn = SUP_IOCTL_VT_CAPS_SIZE_IN;
1681 Req.Hdr.cbOut = SUP_IOCTL_VT_CAPS_SIZE_OUT;
1682 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1683 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1684 Req.u.Out.Caps = 0;
1685 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_VT_CAPS, &Req, SUP_IOCTL_VT_CAPS_SIZE);
1686 if (RT_SUCCESS(rc))
1687 {
1688 rc = Req.Hdr.rc;
1689 if (RT_SUCCESS(rc))
1690 *pfCaps = Req.u.Out.Caps;
1691 }
1692 return rc;
1693}
1694
1695
1696SUPR3DECL(int) SUPR3TracerOpen(uint32_t uCookie, uintptr_t uArg)
1697{
1698 /* fake */
1699 if (RT_UNLIKELY(g_uSupFakeMode))
1700 return VINF_SUCCESS;
1701
1702 /*
1703 * Issue IOCtl to the SUPDRV kernel module.
1704 */
1705 SUPTRACEROPEN Req;
1706 Req.Hdr.u32Cookie = g_u32Cookie;
1707 Req.Hdr.u32SessionCookie= g_u32SessionCookie;
1708 Req.Hdr.cbIn = SUP_IOCTL_TRACER_OPEN_SIZE_IN;
1709 Req.Hdr.cbOut = SUP_IOCTL_TRACER_OPEN_SIZE_OUT;
1710 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1711 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1712 Req.u.In.uCookie = uCookie;
1713 Req.u.In.uArg = uArg;
1714 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_OPEN, &Req, SUP_IOCTL_TRACER_OPEN_SIZE);
1715 if (RT_SUCCESS(rc))
1716 rc = Req.Hdr.rc;
1717 return rc;
1718}
1719
1720
1721SUPR3DECL(int) SUPR3TracerClose(void)
1722{
1723 /* fake */
1724 if (RT_UNLIKELY(g_uSupFakeMode))
1725 return VINF_SUCCESS;
1726
1727 /*
1728 * Issue IOCtl to the SUPDRV kernel module.
1729 */
1730 SUPREQHDR Req;
1731 Req.u32Cookie = g_u32Cookie;
1732 Req.u32SessionCookie= g_u32SessionCookie;
1733 Req.cbIn = SUP_IOCTL_TRACER_OPEN_SIZE_IN;
1734 Req.cbOut = SUP_IOCTL_TRACER_OPEN_SIZE_OUT;
1735 Req.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1736 Req.rc = VERR_INTERNAL_ERROR;
1737 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_CLOSE, &Req, SUP_IOCTL_TRACER_CLOSE_SIZE);
1738 if (RT_SUCCESS(rc))
1739 rc = Req.rc;
1740 return rc;
1741}
1742
1743
1744SUPR3DECL(int) SUPR3TracerIoCtl(uintptr_t uCmd, uintptr_t uArg, int32_t *piRetVal)
1745{
1746 /* fake */
1747 if (RT_UNLIKELY(g_uSupFakeMode))
1748 {
1749 *piRetVal = -1;
1750 return VERR_NOT_SUPPORTED;
1751 }
1752
1753 /*
1754 * Issue IOCtl to the SUPDRV kernel module.
1755 */
1756 SUPTRACERIOCTL Req;
1757 Req.Hdr.u32Cookie = g_u32Cookie;
1758 Req.Hdr.u32SessionCookie= g_u32SessionCookie;
1759 Req.Hdr.cbIn = SUP_IOCTL_TRACER_IOCTL_SIZE_IN;
1760 Req.Hdr.cbOut = SUP_IOCTL_TRACER_IOCTL_SIZE_OUT;
1761 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1762 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1763 Req.u.In.uCmd = uCmd;
1764 Req.u.In.uArg = uArg;
1765 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_IOCTL, &Req, SUP_IOCTL_TRACER_IOCTL_SIZE);
1766 if (RT_SUCCESS(rc))
1767 {
1768 rc = Req.Hdr.rc;
1769 *piRetVal = Req.u.Out.iRetVal;
1770 }
1771 return rc;
1772}
1773
1774
1775
1776typedef struct SUPDRVTRACERSTRTAB
1777{
1778 /** Pointer to the string table. */
1779 char *pchStrTab;
1780 /** The actual string table size. */
1781 uint32_t cbStrTab;
1782 /** The original string pointers. */
1783 RTUINTPTR apszOrgFunctions[1];
1784} SUPDRVTRACERSTRTAB, *PSUPDRVTRACERSTRTAB;
1785
1786
1787/**
1788 * Destroys a string table, restoring the original pszFunction member valus.
1789 *
1790 * @param pThis The string table structure.
1791 * @param paProbeLocs32 The probe location array, 32-bit type variant.
1792 * @param paProbeLocs64 The probe location array, 64-bit type variant.
1793 * @param cProbeLocs The number of elements in the array.
1794 * @param f32Bit Set if @a paProbeLocs32 should be used, when
1795 * clear use @a paProbeLocs64.
1796 */
1797static void supr3TracerDestroyStrTab(PSUPDRVTRACERSTRTAB pThis, PVTGPROBELOC32 paProbeLocs32, PVTGPROBELOC64 paProbeLocs64,
1798 uint32_t cProbeLocs, bool f32Bit)
1799{
1800 /* Restore. */
1801 size_t i = cProbeLocs;
1802 if (f32Bit)
1803 while (i--)
1804 paProbeLocs32[i].pszFunction = (uint32_t)pThis->apszOrgFunctions[i];
1805 else
1806 while (i--)
1807 paProbeLocs64[i].pszFunction = pThis->apszOrgFunctions[i];
1808
1809 /* Free. */
1810 RTMemFree(pThis->pchStrTab);
1811 RTMemFree(pThis);
1812}
1813
1814
1815/**
1816 * Creates a string table for the pszFunction members in the probe location
1817 * array.
1818 *
1819 * This will save and replace the pszFunction members with offsets.
1820 *
1821 * @returns Pointer to a string table structure. NULL on failure.
1822 * @param paProbeLocs32 The probe location array, 32-bit type variant.
1823 * @param paProbeLocs64 The probe location array, 64-bit type variant.
1824 * @param cProbeLocs The number of elements in the array.
1825 * @param offDelta Relocation offset for the string pointers.
1826 * @param f32Bit Set if @a paProbeLocs32 should be used, when
1827 * clear use @a paProbeLocs64.
1828 */
1829static PSUPDRVTRACERSTRTAB supr3TracerCreateStrTab(PVTGPROBELOC32 paProbeLocs32,
1830 PVTGPROBELOC64 paProbeLocs64,
1831 uint32_t cProbeLocs,
1832 RTUINTPTR offDelta,
1833 bool f32Bit)
1834{
1835 if (cProbeLocs > _128K)
1836 return NULL;
1837
1838 /*
1839 * Allocate the string table structures.
1840 */
1841 size_t cbThis = RT_OFFSETOF(SUPDRVTRACERSTRTAB, apszOrgFunctions[cProbeLocs]);
1842 PSUPDRVTRACERSTRTAB pThis = (PSUPDRVTRACERSTRTAB)RTMemAlloc(cbThis);
1843 if (!pThis)
1844 return NULL;
1845
1846 uint32_t const cHashBits = cProbeLocs * 2 - 1;
1847 uint32_t *pbmHash = (uint32_t *)RTMemAllocZ(RT_ALIGN_32(cHashBits, 64) / 8 );
1848 if (!pbmHash)
1849 {
1850 RTMemFree(pThis);
1851 return NULL;
1852 }
1853
1854 /*
1855 * Calc the max string table size and save the orignal pointers so we can
1856 * replace them later.
1857 */
1858 size_t cbMax = 1;
1859 for (uint32_t i = 0; i < cProbeLocs; i++)
1860 {
1861 pThis->apszOrgFunctions[i] = f32Bit ? paProbeLocs32[i].pszFunction : paProbeLocs64[i].pszFunction;
1862 const char *pszFunction = (const char *)(uintptr_t)(pThis->apszOrgFunctions[i] + offDelta);
1863 size_t cch = strlen(pszFunction);
1864 if (cch > _1K)
1865 {
1866 cbMax = 0;
1867 break;
1868 }
1869 cbMax += cch + 1;
1870 }
1871
1872 /* Alloc space for it. */
1873 if (cbMax > 0)
1874 pThis->pchStrTab = (char *)RTMemAlloc(cbMax);
1875 else
1876 pThis->pchStrTab = NULL;
1877 if (!pThis->pchStrTab)
1878 {
1879 RTMemFree(pbmHash);
1880 RTMemFree(pThis);
1881 return NULL;
1882 }
1883
1884 /*
1885 * Create the string table.
1886 */
1887 uint32_t off = 0;
1888 uint32_t offPrev = 0;
1889
1890 for (uint32_t i = 0; i < cProbeLocs; i++)
1891 {
1892 const char * const psz = (const char *)(uintptr_t)(pThis->apszOrgFunctions[i] + offDelta);
1893 size_t const cch = strlen(psz);
1894 uint32_t const iHashBit = RTStrHash1(psz) % cHashBits;
1895 if (ASMBitTestAndSet(pbmHash, iHashBit))
1896 {
1897 /* Often it's the most recent string. */
1898 if ( off - offPrev < cch + 1
1899 || memcmp(&pThis->pchStrTab[offPrev], psz, cch + 1))
1900 {
1901 /* It wasn't, search the entire string table. (lazy bird) */
1902 offPrev = 0;
1903 while (offPrev < off)
1904 {
1905 size_t cchCur = strlen(&pThis->pchStrTab[offPrev]);
1906 if ( cchCur == cch
1907 && !memcmp(&pThis->pchStrTab[offPrev], psz, cch + 1))
1908 break;
1909 offPrev += (uint32_t)cchCur + 1;
1910 }
1911 }
1912 }
1913 else
1914 offPrev = off;
1915
1916 /* Add the string to the table. */
1917 if (offPrev >= off)
1918 {
1919 memcpy(&pThis->pchStrTab[off], psz, cch + 1);
1920 offPrev = off;
1921 off += (uint32_t)cch + 1;
1922 }
1923
1924 /* Update the entry */
1925 if (f32Bit)
1926 paProbeLocs32[i].pszFunction = offPrev;
1927 else
1928 paProbeLocs64[i].pszFunction = offPrev;
1929 }
1930
1931 pThis->cbStrTab = off;
1932 RTMemFree(pbmHash);
1933 return pThis;
1934}
1935
1936
1937
1938SUPR3DECL(int) SUPR3TracerRegisterModule(uintptr_t hModNative, const char *pszModule, struct VTGOBJHDR *pVtgHdr,
1939 RTUINTPTR uVtgHdrAddr, uint32_t fFlags)
1940{
1941 /* Validate input. */
1942 NOREF(hModNative);
1943 AssertPtrReturn(pVtgHdr, VERR_INVALID_POINTER);
1944 AssertReturn(!memcmp(pVtgHdr->szMagic, VTGOBJHDR_MAGIC, sizeof(pVtgHdr->szMagic)), VERR_SUPDRV_VTG_MAGIC);
1945 AssertPtrReturn(pszModule, VERR_INVALID_POINTER);
1946 size_t cchModule = strlen(pszModule);
1947 AssertReturn(cchModule < RT_SIZEOFMEMB(SUPTRACERUMODREG, u.In.szName), VERR_FILENAME_TOO_LONG);
1948 AssertReturn(!RTPathHavePath(pszModule), VERR_INVALID_PARAMETER);
1949 AssertReturn(fFlags == SUP_TRACER_UMOD_FLAGS_EXE || fFlags == SUP_TRACER_UMOD_FLAGS_SHARED, VERR_INVALID_PARAMETER);
1950
1951 /*
1952 * Set the probe location array offset and size members. If the size is
1953 * zero, don't bother ring-0 with it.
1954 */
1955 if (!pVtgHdr->offProbeLocs)
1956 {
1957 uint64_t u64Tmp = pVtgHdr->uProbeLocsEnd.u64 - pVtgHdr->uProbeLocs.u64;
1958 if (u64Tmp >= UINT32_MAX)
1959 return VERR_SUPDRV_VTG_BAD_HDR_TOO_MUCH;
1960 pVtgHdr->cbProbeLocs = (uint32_t)u64Tmp;
1961
1962 u64Tmp = pVtgHdr->uProbeLocs.u64 - uVtgHdrAddr;
1963 if ((int64_t)u64Tmp != (int32_t)u64Tmp)
1964 {
1965 LogRel(("SUPR3TracerRegisterModule: VERR_SUPDRV_VTG_BAD_HDR_PTR - u64Tmp=%#llx uProbeLocs=%#llx uVtgHdrAddr=%RTptr\n",
1966 u64Tmp, pVtgHdr->uProbeLocs.u64, uVtgHdrAddr));
1967 return VERR_SUPDRV_VTG_BAD_HDR_PTR;
1968 }
1969 pVtgHdr->offProbeLocs = (int32_t)u64Tmp;
1970 }
1971
1972 if ( !pVtgHdr->cbProbeLocs
1973 || !pVtgHdr->cbProbes)
1974 return VINF_SUCCESS;
1975
1976 /*
1977 * Fake out.
1978 */
1979 if (RT_UNLIKELY(g_uSupFakeMode))
1980 return VINF_SUCCESS;
1981
1982 /*
1983 * Create a string table for the function names in the location array.
1984 * It's somewhat easier to do that here than from ring-0.
1985 */
1986 uint32_t const cProbeLocs = pVtgHdr->cbProbeLocs
1987 / (pVtgHdr->cBits == 32 ? sizeof(VTGPROBELOC32) : sizeof(VTGPROBELOC64));
1988 PVTGPROBELOC paProbeLocs = (PVTGPROBELOC)((uintptr_t)pVtgHdr + pVtgHdr->offProbeLocs);
1989 PSUPDRVTRACERSTRTAB pStrTab = supr3TracerCreateStrTab((PVTGPROBELOC32)paProbeLocs,
1990 (PVTGPROBELOC64)paProbeLocs,
1991 cProbeLocs, (uintptr_t)pVtgHdr - uVtgHdrAddr,
1992 pVtgHdr->cBits == 32);
1993 if (!pStrTab)
1994 return VERR_NO_MEMORY;
1995
1996
1997 /*
1998 * Issue IOCtl to the SUPDRV kernel module.
1999 */
2000 SUPTRACERUMODREG Req;
2001 Req.Hdr.u32Cookie = g_u32Cookie;
2002 Req.Hdr.u32SessionCookie= g_u32SessionCookie;
2003 Req.Hdr.cbIn = SUP_IOCTL_TRACER_UMOD_REG_SIZE_IN;
2004 Req.Hdr.cbOut = SUP_IOCTL_TRACER_UMOD_REG_SIZE_OUT;
2005 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2006 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2007 Req.u.In.uVtgHdrAddr = uVtgHdrAddr;
2008 Req.u.In.R3PtrVtgHdr = pVtgHdr;
2009 Req.u.In.R3PtrStrTab = pStrTab->pchStrTab;
2010 Req.u.In.cbStrTab = pStrTab->cbStrTab;
2011 Req.u.In.fFlags = fFlags;
2012
2013 memcpy(Req.u.In.szName, pszModule, cchModule + 1);
2014 if (!RTPathHasSuffix(Req.u.In.szName))
2015 {
2016 /* Add the default suffix if none is given. */
2017 switch (fFlags & SUP_TRACER_UMOD_FLAGS_TYPE_MASK)
2018 {
2019#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
2020 case SUP_TRACER_UMOD_FLAGS_EXE:
2021 if (cchModule + sizeof(".exe") <= sizeof(Req.u.In.szName))
2022 strcpy(&Req.u.In.szName[cchModule], ".exe");
2023 break;
2024#endif
2025
2026 case SUP_TRACER_UMOD_FLAGS_SHARED:
2027 {
2028 const char *pszSuff = RTLdrGetSuff();
2029 size_t cchSuff = strlen(pszSuff);
2030 if (cchModule + cchSuff < sizeof(Req.u.In.szName))
2031 memcpy(&Req.u.In.szName[cchModule], pszSuff, cchSuff + 1);
2032 break;
2033 }
2034 }
2035 }
2036
2037 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_UMOD_REG, &Req, SUP_IOCTL_TRACER_UMOD_REG_SIZE);
2038 if (RT_SUCCESS(rc))
2039 rc = Req.Hdr.rc;
2040
2041 supr3TracerDestroyStrTab(pStrTab, (PVTGPROBELOC32)paProbeLocs, (PVTGPROBELOC64)paProbeLocs,
2042 cProbeLocs, pVtgHdr->cBits == 32);
2043 return rc;
2044}
2045
2046
2047SUPR3DECL(int) SUPR3TracerDeregisterModule(struct VTGOBJHDR *pVtgHdr)
2048{
2049 /* Validate input. */
2050 AssertPtrReturn(pVtgHdr, VERR_INVALID_POINTER);
2051 AssertReturn(!memcmp(pVtgHdr->szMagic, VTGOBJHDR_MAGIC, sizeof(pVtgHdr->szMagic)), VERR_SUPDRV_VTG_MAGIC);
2052
2053 /*
2054 * Don't bother if the object is empty.
2055 */
2056 if ( !pVtgHdr->cbProbeLocs
2057 || !pVtgHdr->cbProbes)
2058 return VINF_SUCCESS;
2059
2060 /*
2061 * Fake out.
2062 */
2063 if (RT_UNLIKELY(g_uSupFakeMode))
2064 return VINF_SUCCESS;
2065
2066 /*
2067 * Issue IOCtl to the SUPDRV kernel module.
2068 */
2069 SUPTRACERUMODDEREG Req;
2070 Req.Hdr.u32Cookie = g_u32Cookie;
2071 Req.Hdr.u32SessionCookie= g_u32SessionCookie;
2072 Req.Hdr.cbIn = SUP_IOCTL_TRACER_UMOD_REG_SIZE_IN;
2073 Req.Hdr.cbOut = SUP_IOCTL_TRACER_UMOD_REG_SIZE_OUT;
2074 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2075 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2076 Req.u.In.pVtgHdr = pVtgHdr;
2077
2078 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_UMOD_DEREG, &Req, SUP_IOCTL_TRACER_UMOD_DEREG_SIZE);
2079 if (RT_SUCCESS(rc))
2080 rc = Req.Hdr.rc;
2081 return rc;
2082}
2083
2084
2085DECLASM(void) suplibTracerFireProbe(PVTGPROBELOC pProbeLoc, PSUPTRACERUMODFIREPROBE pReq)
2086{
2087 RT_NOREF1(pProbeLoc);
2088
2089 pReq->Hdr.u32Cookie = g_u32Cookie;
2090 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
2091 Assert(pReq->Hdr.cbIn == SUP_IOCTL_TRACER_UMOD_FIRE_PROBE_SIZE_IN);
2092 Assert(pReq->Hdr.cbOut == SUP_IOCTL_TRACER_UMOD_FIRE_PROBE_SIZE_OUT);
2093 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2094 pReq->Hdr.rc = VINF_SUCCESS;
2095
2096 suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_UMOD_FIRE_PROBE, pReq, SUP_IOCTL_TRACER_UMOD_FIRE_PROBE_SIZE);
2097}
2098
2099
2100SUPR3DECL(int) SUPR3MsrProberRead(uint32_t uMsr, RTCPUID idCpu, uint64_t *puValue, bool *pfGp)
2101{
2102 SUPMSRPROBER Req;
2103 Req.Hdr.u32Cookie = g_u32Cookie;
2104 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
2105 Req.Hdr.cbIn = SUP_IOCTL_MSR_PROBER_SIZE_IN;
2106 Req.Hdr.cbOut = SUP_IOCTL_MSR_PROBER_SIZE_OUT;
2107 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2108 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2109
2110 Req.u.In.enmOp = SUPMSRPROBEROP_READ;
2111 Req.u.In.uMsr = uMsr;
2112 Req.u.In.idCpu = idCpu == NIL_RTCPUID ? UINT32_MAX : idCpu;
2113
2114 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_MSR_PROBER, &Req, SUP_IOCTL_MSR_PROBER_SIZE);
2115 if (RT_SUCCESS(rc))
2116 rc = Req.Hdr.rc;
2117 if (RT_SUCCESS(rc))
2118 {
2119 if (puValue)
2120 *puValue = Req.u.Out.uResults.Read.uValue;
2121 if (pfGp)
2122 *pfGp = Req.u.Out.uResults.Read.fGp;
2123 }
2124
2125 return rc;
2126}
2127
2128
2129SUPR3DECL(int) SUPR3MsrProberWrite(uint32_t uMsr, RTCPUID idCpu, uint64_t uValue, bool *pfGp)
2130{
2131 SUPMSRPROBER Req;
2132 Req.Hdr.u32Cookie = g_u32Cookie;
2133 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
2134 Req.Hdr.cbIn = SUP_IOCTL_MSR_PROBER_SIZE_IN;
2135 Req.Hdr.cbOut = SUP_IOCTL_MSR_PROBER_SIZE_OUT;
2136 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2137 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2138
2139 Req.u.In.enmOp = SUPMSRPROBEROP_WRITE;
2140 Req.u.In.uMsr = uMsr;
2141 Req.u.In.idCpu = idCpu == NIL_RTCPUID ? UINT32_MAX : idCpu;
2142 Req.u.In.uArgs.Write.uToWrite = uValue;
2143
2144 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_MSR_PROBER, &Req, SUP_IOCTL_MSR_PROBER_SIZE);
2145 if (RT_SUCCESS(rc))
2146 rc = Req.Hdr.rc;
2147 if (RT_SUCCESS(rc) && pfGp)
2148 *pfGp = Req.u.Out.uResults.Write.fGp;
2149
2150 return rc;
2151}
2152
2153
2154SUPR3DECL(int) SUPR3MsrProberModify(uint32_t uMsr, RTCPUID idCpu, uint64_t fAndMask, uint64_t fOrMask,
2155 PSUPMSRPROBERMODIFYRESULT pResult)
2156{
2157 return SUPR3MsrProberModifyEx(uMsr, idCpu, fAndMask, fOrMask, false /*fFaster*/, pResult);
2158}
2159
2160
2161SUPR3DECL(int) SUPR3MsrProberModifyEx(uint32_t uMsr, RTCPUID idCpu, uint64_t fAndMask, uint64_t fOrMask, bool fFaster,
2162 PSUPMSRPROBERMODIFYRESULT pResult)
2163{
2164 SUPMSRPROBER Req;
2165 Req.Hdr.u32Cookie = g_u32Cookie;
2166 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
2167 Req.Hdr.cbIn = SUP_IOCTL_MSR_PROBER_SIZE_IN;
2168 Req.Hdr.cbOut = SUP_IOCTL_MSR_PROBER_SIZE_OUT;
2169 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2170 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2171
2172 Req.u.In.enmOp = fFaster ? SUPMSRPROBEROP_MODIFY_FASTER : SUPMSRPROBEROP_MODIFY;
2173 Req.u.In.uMsr = uMsr;
2174 Req.u.In.idCpu = idCpu == NIL_RTCPUID ? UINT32_MAX : idCpu;
2175 Req.u.In.uArgs.Modify.fAndMask = fAndMask;
2176 Req.u.In.uArgs.Modify.fOrMask = fOrMask;
2177
2178 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_MSR_PROBER, &Req, SUP_IOCTL_MSR_PROBER_SIZE);
2179 if (RT_SUCCESS(rc))
2180 rc = Req.Hdr.rc;
2181 if (RT_SUCCESS(rc))
2182 *pResult = Req.u.Out.uResults.Modify;
2183
2184 return rc;
2185}
2186
2187
2188SUPR3DECL(int) SUPR3ResumeSuspendedKeyboards(void)
2189{
2190#ifdef RT_OS_DARWIN
2191 /*
2192 * Issue IOCtl to the SUPDRV kernel module.
2193 */
2194 SUPREQHDR Req;
2195 Req.u32Cookie = g_u32Cookie;
2196 Req.u32SessionCookie= g_u32SessionCookie;
2197 Req.cbIn = SUP_IOCTL_RESUME_SUSPENDED_KBDS_SIZE_IN;
2198 Req.cbOut = SUP_IOCTL_RESUME_SUSPENDED_KBDS_SIZE_OUT;
2199 Req.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2200 Req.rc = VERR_INTERNAL_ERROR;
2201 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_RESUME_SUSPENDED_KBDS, &Req, SUP_IOCTL_RESUME_SUSPENDED_KBDS_SIZE);
2202 if (RT_SUCCESS(rc))
2203 rc = Req.rc;
2204 return rc;
2205#else /* !RT_OS_DARWIN */
2206 return VERR_NOT_SUPPORTED;
2207#endif
2208}
2209
2210
2211SUPR3DECL(int) SUPR3TscDeltaMeasure(RTCPUID idCpu, bool fAsync, bool fForce, uint8_t cRetries, uint8_t cMsWaitRetry)
2212{
2213 SUPTSCDELTAMEASURE Req;
2214 Req.Hdr.u32Cookie = g_u32Cookie;
2215 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
2216 Req.Hdr.cbIn = SUP_IOCTL_TSC_DELTA_MEASURE_SIZE_IN;
2217 Req.Hdr.cbOut = SUP_IOCTL_TSC_DELTA_MEASURE_SIZE_OUT;
2218 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2219 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2220
2221 Req.u.In.cRetries = cRetries;
2222 Req.u.In.fAsync = fAsync;
2223 Req.u.In.fForce = fForce;
2224 Req.u.In.idCpu = idCpu;
2225 Req.u.In.cMsWaitRetry = cMsWaitRetry;
2226
2227 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TSC_DELTA_MEASURE, &Req, SUP_IOCTL_TSC_DELTA_MEASURE_SIZE);
2228 if (RT_SUCCESS(rc))
2229 rc = Req.Hdr.rc;
2230 return rc;
2231}
2232
2233
2234SUPR3DECL(int) SUPR3ReadTsc(uint64_t *puTsc, uint16_t *pidApic)
2235{
2236 AssertReturn(puTsc, VERR_INVALID_PARAMETER);
2237
2238 SUPTSCREAD Req;
2239 Req.Hdr.u32Cookie = g_u32Cookie;
2240 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
2241 Req.Hdr.cbIn = SUP_IOCTL_TSC_READ_SIZE_IN;
2242 Req.Hdr.cbOut = SUP_IOCTL_TSC_READ_SIZE_OUT;
2243 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2244 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2245
2246 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TSC_READ, &Req, SUP_IOCTL_TSC_READ_SIZE);
2247 if (RT_SUCCESS(rc))
2248 {
2249 rc = Req.Hdr.rc;
2250 *puTsc = Req.u.Out.u64AdjustedTsc;
2251 if (pidApic)
2252 *pidApic = Req.u.Out.idApic;
2253 }
2254 return rc;
2255}
2256
2257
2258SUPR3DECL(int) SUPR3GipSetFlags(uint32_t fOrMask, uint32_t fAndMask)
2259{
2260 AssertMsgReturn(!(fOrMask & ~SUPGIP_FLAGS_VALID_MASK),
2261 ("fOrMask=%#x ValidMask=%#x\n", fOrMask, SUPGIP_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER);
2262 AssertMsgReturn((fAndMask & ~SUPGIP_FLAGS_VALID_MASK) == ~SUPGIP_FLAGS_VALID_MASK,
2263 ("fAndMask=%#x ValidMask=%#x\n", fAndMask, SUPGIP_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER);
2264
2265 SUPGIPSETFLAGS Req;
2266 Req.Hdr.u32Cookie = g_u32Cookie;
2267 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
2268 Req.Hdr.cbIn = SUP_IOCTL_GIP_SET_FLAGS_SIZE_IN;
2269 Req.Hdr.cbOut = SUP_IOCTL_GIP_SET_FLAGS_SIZE_OUT;
2270 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2271 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2272
2273 Req.u.In.fAndMask = fAndMask;
2274 Req.u.In.fOrMask = fOrMask;
2275
2276 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_GIP_SET_FLAGS, &Req, SUP_IOCTL_GIP_SET_FLAGS_SIZE);
2277 if (RT_SUCCESS(rc))
2278 rc = Req.Hdr.rc;
2279 return rc;
2280}
2281
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