VirtualBox

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

Last change on this file since 62564 was 62490, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 80.4 KB
Line 
1/* $Id: SUPLib.cpp 62490 2016-07-22 18:41:49Z 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/** VMMR0 Load Address. */
134static RTR0PTR g_pvVMMR0 = NIL_RTR0PTR;
135/** PAGE_ALLOC_EX sans kernel mapping support indicator. */
136static bool g_fSupportsPageAllocNoKernel = true;
137/** Fake mode indicator. (~0 at first, 0 or 1 after first test) */
138uint32_t g_uSupFakeMode = ~0;
139
140
141/*********************************************************************************************************************************
142* Internal Functions *
143*********************************************************************************************************************************/
144static int supInitFake(PSUPDRVSESSION *ppSession);
145static int supLoadModule(const char *pszFilename, const char *pszModule, const char *pszSrvReqHandler, void **ppvImageBase);
146static DECLCALLBACK(int) supLoadModuleResolveImport(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser);
147
148
149/** Touch a range of pages. */
150DECLINLINE(void) supR3TouchPages(void *pv, size_t cPages)
151{
152 uint32_t volatile *pu32 = (uint32_t volatile *)pv;
153 while (cPages-- > 0)
154 {
155 ASMAtomicCmpXchgU32(pu32, 0, 0);
156 pu32 += PAGE_SIZE / sizeof(uint32_t);
157 }
158}
159
160
161SUPR3DECL(int) SUPR3Install(void)
162{
163 return suplibOsInstall();
164}
165
166
167SUPR3DECL(int) SUPR3Uninstall(void)
168{
169 return suplibOsUninstall();
170}
171
172
173DECLEXPORT(int) supR3PreInit(PSUPPREINITDATA pPreInitData, uint32_t fFlags)
174{
175 /*
176 * The caller is kind of trustworthy, just perform some basic checks.
177 *
178 * Note! Do not do any fancy stuff here because IPRT has NOT been
179 * initialized at this point.
180 */
181 if (!VALID_PTR(pPreInitData))
182 return VERR_INVALID_POINTER;
183 if (g_fPreInited || g_cInits > 0)
184 return VERR_WRONG_ORDER;
185
186 if ( pPreInitData->u32Magic != SUPPREINITDATA_MAGIC
187 || pPreInitData->u32EndMagic != SUPPREINITDATA_MAGIC)
188 return VERR_INVALID_MAGIC;
189 if ( !(fFlags & SUPSECMAIN_FLAGS_DONT_OPEN_DEV)
190 && pPreInitData->Data.hDevice == SUP_HDEVICE_NIL)
191 return VERR_INVALID_HANDLE;
192 if ( (fFlags & SUPSECMAIN_FLAGS_DONT_OPEN_DEV)
193 && pPreInitData->Data.hDevice != SUP_HDEVICE_NIL)
194 return VERR_INVALID_PARAMETER;
195
196 /*
197 * Hand out the data.
198 */
199 int rc = supR3HardenedRecvPreInitData(pPreInitData);
200 if (RT_FAILURE(rc))
201 return rc;
202
203 /** @todo This may need some small restructuring later, it doesn't quite work with a root service flag... */
204 if (!(fFlags & SUPSECMAIN_FLAGS_DONT_OPEN_DEV))
205 {
206 g_supLibData = pPreInitData->Data;
207 g_fPreInited = true;
208 }
209
210 return VINF_SUCCESS;
211}
212
213
214SUPR3DECL(int) SUPR3InitEx(bool fUnrestricted, PSUPDRVSESSION *ppSession)
215{
216 /*
217 * Perform some sanity checks.
218 * (Got some trouble with compile time member alignment assertions.)
219 */
220 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, u64NanoTSLastUpdateHz) & 0x7));
221 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs) & 0x1f));
222 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs[1]) & 0x1f));
223 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs[0].u64NanoTS) & 0x7));
224 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs[0].u64TSC) & 0x7));
225 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs[0].u64CpuHz) & 0x7));
226
227 /*
228 * Check if already initialized.
229 */
230 if (ppSession)
231 *ppSession = g_pSession;
232 if (g_cInits++ > 0)
233 {
234 if (fUnrestricted && !g_supLibData.fUnrestricted)
235 {
236 g_cInits--;
237 if (ppSession)
238 *ppSession = NIL_RTR0PTR;
239 return VERR_VM_DRIVER_NOT_ACCESSIBLE; /** @todo different status code? */
240 }
241 return VINF_SUCCESS;
242 }
243
244 /*
245 * Check for fake mode.
246 *
247 * Fake mode is used when we're doing smoke testing and debugging.
248 * It's also useful on platforms where we haven't root access or which
249 * we haven't ported the support driver to.
250 */
251 if (g_uSupFakeMode == ~0U)
252 {
253 const char *psz = RTEnvGet("VBOX_SUPLIB_FAKE");
254 if (psz && !strcmp(psz, "fake"))
255 ASMAtomicCmpXchgU32(&g_uSupFakeMode, 1, ~0U);
256 else
257 ASMAtomicCmpXchgU32(&g_uSupFakeMode, 0, ~0U);
258 }
259 if (RT_UNLIKELY(g_uSupFakeMode))
260 return supInitFake(ppSession);
261
262 /*
263 * Open the support driver.
264 */
265 SUPINITOP enmWhat = kSupInitOp_Driver;
266 int rc = suplibOsInit(&g_supLibData, g_fPreInited, fUnrestricted, &enmWhat, NULL);
267 if (RT_SUCCESS(rc))
268 {
269 /*
270 * Negotiate the cookie.
271 */
272 SUPCOOKIE CookieReq;
273 memset(&CookieReq, 0xff, sizeof(CookieReq));
274 CookieReq.Hdr.u32Cookie = SUPCOOKIE_INITIAL_COOKIE;
275 CookieReq.Hdr.u32SessionCookie = RTRandU32();
276 CookieReq.Hdr.cbIn = SUP_IOCTL_COOKIE_SIZE_IN;
277 CookieReq.Hdr.cbOut = SUP_IOCTL_COOKIE_SIZE_OUT;
278 CookieReq.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
279 CookieReq.Hdr.rc = VERR_INTERNAL_ERROR;
280 strcpy(CookieReq.u.In.szMagic, SUPCOOKIE_MAGIC);
281 CookieReq.u.In.u32ReqVersion = SUPDRV_IOC_VERSION;
282 const uint32_t uMinVersion = (SUPDRV_IOC_VERSION & 0xffff0000) == 0x00230000
283 ? 0x00230003
284 : SUPDRV_IOC_VERSION & 0xffff0000;
285 CookieReq.u.In.u32MinVersion = uMinVersion;
286 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_COOKIE, &CookieReq, SUP_IOCTL_COOKIE_SIZE);
287 if ( RT_SUCCESS(rc)
288 && RT_SUCCESS(CookieReq.Hdr.rc))
289 {
290 if ( (CookieReq.u.Out.u32SessionVersion & 0xffff0000) == (SUPDRV_IOC_VERSION & 0xffff0000)
291 && CookieReq.u.Out.u32SessionVersion >= uMinVersion)
292 {
293 /*
294 * Query the functions.
295 */
296 PSUPQUERYFUNCS pFuncsReq = NULL;
297 if (g_supLibData.fUnrestricted)
298 {
299 pFuncsReq = (PSUPQUERYFUNCS)RTMemAllocZ(SUP_IOCTL_QUERY_FUNCS_SIZE(CookieReq.u.Out.cFunctions));
300 if (pFuncsReq)
301 {
302 pFuncsReq->Hdr.u32Cookie = CookieReq.u.Out.u32Cookie;
303 pFuncsReq->Hdr.u32SessionCookie = CookieReq.u.Out.u32SessionCookie;
304 pFuncsReq->Hdr.cbIn = SUP_IOCTL_QUERY_FUNCS_SIZE_IN;
305 pFuncsReq->Hdr.cbOut = SUP_IOCTL_QUERY_FUNCS_SIZE_OUT(CookieReq.u.Out.cFunctions);
306 pFuncsReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
307 pFuncsReq->Hdr.rc = VERR_INTERNAL_ERROR;
308 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_QUERY_FUNCS(CookieReq.u.Out.cFunctions), pFuncsReq,
309 SUP_IOCTL_QUERY_FUNCS_SIZE(CookieReq.u.Out.cFunctions));
310 if (RT_SUCCESS(rc))
311 rc = pFuncsReq->Hdr.rc;
312 if (RT_SUCCESS(rc))
313 {
314 /*
315 * Map the GIP into userspace.
316 */
317 Assert(!g_pSUPGlobalInfoPage);
318 SUPGIPMAP GipMapReq;
319 GipMapReq.Hdr.u32Cookie = CookieReq.u.Out.u32Cookie;
320 GipMapReq.Hdr.u32SessionCookie = CookieReq.u.Out.u32SessionCookie;
321 GipMapReq.Hdr.cbIn = SUP_IOCTL_GIP_MAP_SIZE_IN;
322 GipMapReq.Hdr.cbOut = SUP_IOCTL_GIP_MAP_SIZE_OUT;
323 GipMapReq.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
324 GipMapReq.Hdr.rc = VERR_INTERNAL_ERROR;
325 GipMapReq.u.Out.HCPhysGip = NIL_RTHCPHYS;
326 GipMapReq.u.Out.pGipR0 = NIL_RTR0PTR;
327 GipMapReq.u.Out.pGipR3 = NULL;
328 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_GIP_MAP, &GipMapReq, SUP_IOCTL_GIP_MAP_SIZE);
329 if (RT_SUCCESS(rc))
330 rc = GipMapReq.Hdr.rc;
331 if (RT_SUCCESS(rc))
332 {
333 /*
334 * Set the GIP globals.
335 */
336 AssertRelease(GipMapReq.u.Out.pGipR3->u32Magic == SUPGLOBALINFOPAGE_MAGIC);
337 AssertRelease(GipMapReq.u.Out.pGipR3->u32Version >= SUPGLOBALINFOPAGE_VERSION);
338
339 ASMAtomicXchgSize(&g_HCPhysSUPGlobalInfoPage, GipMapReq.u.Out.HCPhysGip);
340 ASMAtomicCmpXchgPtr((void * volatile *)&g_pSUPGlobalInfoPage, GipMapReq.u.Out.pGipR3, NULL);
341 ASMAtomicCmpXchgPtr((void * volatile *)&g_pSUPGlobalInfoPageR0, (void *)GipMapReq.u.Out.pGipR0, NULL);
342 }
343 }
344 }
345 else
346 rc = VERR_NO_MEMORY;
347 }
348
349 if (RT_SUCCESS(rc))
350 {
351 /*
352 * Set the globals and return success.
353 */
354 g_u32Cookie = CookieReq.u.Out.u32Cookie;
355 g_u32SessionCookie = CookieReq.u.Out.u32SessionCookie;
356 g_pSession = CookieReq.u.Out.pSession;
357 g_pSupFunctions = pFuncsReq;
358 if (ppSession)
359 *ppSession = CookieReq.u.Out.pSession;
360 return VINF_SUCCESS;
361 }
362
363 /* bailout */
364 RTMemFree(pFuncsReq);
365 }
366 else
367 {
368 LogRel(("Support driver version mismatch: SessionVersion=%#x DriverVersion=%#x ClientVersion=%#x MinVersion=%#x\n",
369 CookieReq.u.Out.u32SessionVersion, CookieReq.u.Out.u32DriverVersion, SUPDRV_IOC_VERSION, uMinVersion));
370 rc = VERR_VM_DRIVER_VERSION_MISMATCH;
371 }
372 }
373 else
374 {
375 if (RT_SUCCESS(rc))
376 {
377 rc = CookieReq.Hdr.rc;
378 LogRel(("Support driver version mismatch: DriverVersion=%#x ClientVersion=%#x rc=%Rrc\n",
379 CookieReq.u.Out.u32DriverVersion, SUPDRV_IOC_VERSION, rc));
380 if (rc != VERR_VM_DRIVER_VERSION_MISMATCH)
381 rc = VERR_VM_DRIVER_VERSION_MISMATCH;
382 }
383 else
384 {
385 /* for pre 0x00060000 drivers */
386 LogRel(("Support driver version mismatch: DriverVersion=too-old ClientVersion=%#x\n", SUPDRV_IOC_VERSION));
387 rc = VERR_VM_DRIVER_VERSION_MISMATCH;
388 }
389 }
390
391 suplibOsTerm(&g_supLibData);
392 }
393 g_cInits--;
394
395 return rc;
396}
397
398
399SUPR3DECL(int) SUPR3Init(PSUPDRVSESSION *ppSession)
400{
401 return SUPR3InitEx(true, ppSession);
402}
403
404/**
405 * Fake mode init.
406 */
407static int supInitFake(PSUPDRVSESSION *ppSession)
408{
409 Log(("SUP: Fake mode!\n"));
410 static const SUPFUNC s_aFakeFunctions[] =
411 {
412 /* name function */
413 { "SUPR0AbsIs64bit", 0 },
414 { "SUPR0Abs64bitKernelCS", 0 },
415 { "SUPR0Abs64bitKernelSS", 0 },
416 { "SUPR0Abs64bitKernelDS", 0 },
417 { "SUPR0AbsKernelCS", 8 },
418 { "SUPR0AbsKernelSS", 16 },
419 { "SUPR0AbsKernelDS", 16 },
420 { "SUPR0AbsKernelES", 16 },
421 { "SUPR0AbsKernelFS", 24 },
422 { "SUPR0AbsKernelGS", 32 },
423 { "SUPR0ComponentRegisterFactory", 0xefeefffd },
424 { "SUPR0ComponentDeregisterFactory", 0xefeefffe },
425 { "SUPR0ComponentQueryFactory", 0xefeeffff },
426 { "SUPR0ObjRegister", 0xefef0000 },
427 { "SUPR0ObjAddRef", 0xefef0001 },
428 { "SUPR0ObjAddRefEx", 0xefef0001 },
429 { "SUPR0ObjRelease", 0xefef0002 },
430 { "SUPR0ObjVerifyAccess", 0xefef0003 },
431 { "SUPR0LockMem", 0xefef0004 },
432 { "SUPR0UnlockMem", 0xefef0005 },
433 { "SUPR0ContAlloc", 0xefef0006 },
434 { "SUPR0ContFree", 0xefef0007 },
435 { "SUPR0MemAlloc", 0xefef0008 },
436 { "SUPR0MemGetPhys", 0xefef0009 },
437 { "SUPR0MemFree", 0xefef000a },
438 { "SUPR0Printf", 0xefef000b },
439 { "SUPR0GetPagingMode", 0xefef000c },
440 { "SUPR0EnableVTx", 0xefef000e },
441 { "RTMemAlloc", 0xefef000f },
442 { "RTMemAllocZ", 0xefef0010 },
443 { "RTMemFree", 0xefef0011 },
444 { "RTR0MemObjAddress", 0xefef0012 },
445 { "RTR0MemObjAddressR3", 0xefef0013 },
446 { "RTR0MemObjAllocPage", 0xefef0014 },
447 { "RTR0MemObjAllocPhysNC", 0xefef0015 },
448 { "RTR0MemObjAllocLow", 0xefef0016 },
449 { "RTR0MemObjEnterPhys", 0xefef0017 },
450 { "RTR0MemObjFree", 0xefef0018 },
451 { "RTR0MemObjGetPagePhysAddr", 0xefef0019 },
452 { "RTR0MemObjMapUser", 0xefef001a },
453 { "RTR0MemObjMapKernel", 0xefef001b },
454 { "RTR0MemObjMapKernelEx", 0xefef001c },
455 { "RTMpGetArraySize", 0xefef001c },
456 { "RTProcSelf", 0xefef001d },
457 { "RTR0ProcHandleSelf", 0xefef001e },
458 { "RTSemEventCreate", 0xefef001f },
459 { "RTSemEventSignal", 0xefef0020 },
460 { "RTSemEventWait", 0xefef0021 },
461 { "RTSemEventWaitNoResume", 0xefef0022 },
462 { "RTSemEventDestroy", 0xefef0023 },
463 { "RTSemEventMultiCreate", 0xefef0024 },
464 { "RTSemEventMultiSignal", 0xefef0025 },
465 { "RTSemEventMultiReset", 0xefef0026 },
466 { "RTSemEventMultiWait", 0xefef0027 },
467 { "RTSemEventMultiWaitNoResume", 0xefef0028 },
468 { "RTSemEventMultiDestroy", 0xefef0029 },
469 { "RTSemFastMutexCreate", 0xefef002a },
470 { "RTSemFastMutexDestroy", 0xefef002b },
471 { "RTSemFastMutexRequest", 0xefef002c },
472 { "RTSemFastMutexRelease", 0xefef002d },
473 { "RTSpinlockCreate", 0xefef002e },
474 { "RTSpinlockDestroy", 0xefef002f },
475 { "RTSpinlockAcquire", 0xefef0030 },
476 { "RTSpinlockRelease", 0xefef0031 },
477 { "RTSpinlockAcquireNoInts", 0xefef0032 },
478 { "RTTimeNanoTS", 0xefef0034 },
479 { "RTTimeMillieTS", 0xefef0035 },
480 { "RTTimeSystemNanoTS", 0xefef0036 },
481 { "RTTimeSystemMillieTS", 0xefef0037 },
482 { "RTThreadNativeSelf", 0xefef0038 },
483 { "RTThreadSleep", 0xefef0039 },
484 { "RTThreadYield", 0xefef003a },
485 { "RTTimerCreate", 0xefef003a },
486 { "RTTimerCreateEx", 0xefef003a },
487 { "RTTimerDestroy", 0xefef003a },
488 { "RTTimerStart", 0xefef003a },
489 { "RTTimerStop", 0xefef003a },
490 { "RTTimerChangeInterval", 0xefef003a },
491 { "RTTimerGetSystemGranularity", 0xefef003a },
492 { "RTTimerRequestSystemGranularity", 0xefef003a },
493 { "RTTimerReleaseSystemGranularity", 0xefef003a },
494 { "RTTimerCanDoHighResolution", 0xefef003a },
495 { "RTLogDefaultInstance", 0xefef003b },
496 { "RTLogRelGetDefaultInstance", 0xefef003c },
497 { "RTLogSetDefaultInstanceThread", 0xefef003d },
498 { "RTLogLogger", 0xefef003e },
499 { "RTLogLoggerEx", 0xefef003f },
500 { "RTLogLoggerExV", 0xefef0040 },
501 { "RTAssertMsg1", 0xefef0041 },
502 { "RTAssertMsg2", 0xefef0042 },
503 { "RTAssertMsg2V", 0xefef0043 },
504 { "SUPR0QueryVTCaps", 0xefef0044 },
505 };
506
507 /* fake r0 functions. */
508 g_pSupFunctions = (PSUPQUERYFUNCS)RTMemAllocZ(SUP_IOCTL_QUERY_FUNCS_SIZE(RT_ELEMENTS(s_aFakeFunctions)));
509 if (g_pSupFunctions)
510 {
511 g_pSupFunctions->u.Out.cFunctions = RT_ELEMENTS(s_aFakeFunctions);
512 memcpy(&g_pSupFunctions->u.Out.aFunctions[0], &s_aFakeFunctions[0], sizeof(s_aFakeFunctions));
513 g_pSession = (PSUPDRVSESSION)(void *)g_pSupFunctions;
514 if (ppSession)
515 *ppSession = g_pSession;
516
517 /* fake the GIP. */
518 g_pSUPGlobalInfoPage = (PSUPGLOBALINFOPAGE)RTMemPageAllocZ(PAGE_SIZE);
519 if (g_pSUPGlobalInfoPage)
520 {
521 g_pSUPGlobalInfoPageR0 = g_pSUPGlobalInfoPage;
522 g_HCPhysSUPGlobalInfoPage = NIL_RTHCPHYS & ~(RTHCPHYS)PAGE_OFFSET_MASK;
523 /* the page is supposed to be invalid, so don't set the magic. */
524 return VINF_SUCCESS;
525 }
526
527 RTMemFree(g_pSupFunctions);
528 g_pSupFunctions = NULL;
529 }
530 return VERR_NO_MEMORY;
531}
532
533
534SUPR3DECL(int) SUPR3Term(bool fForced)
535{
536 /*
537 * Verify state.
538 */
539 AssertMsg(g_cInits > 0, ("SUPR3Term() is called before SUPR3Init()!\n"));
540 if (g_cInits == 0)
541 return VERR_WRONG_ORDER;
542 if (g_cInits == 1 || fForced)
543 {
544 /*
545 * NULL the GIP pointer.
546 */
547 if (g_pSUPGlobalInfoPage)
548 {
549 ASMAtomicWriteNullPtr((void * volatile *)&g_pSUPGlobalInfoPage);
550 ASMAtomicWriteNullPtr((void * volatile *)&g_pSUPGlobalInfoPageR0);
551 ASMAtomicWriteSize(&g_HCPhysSUPGlobalInfoPage, NIL_RTHCPHYS);
552 /* just a little safe guard against threads using the page. */
553 RTThreadSleep(50);
554 }
555
556 /*
557 * Close the support driver.
558 */
559 int rc = suplibOsTerm(&g_supLibData);
560 if (rc)
561 return rc;
562
563 g_u32Cookie = 0;
564 g_u32SessionCookie = 0;
565 g_cInits = 0;
566 }
567 else
568 g_cInits--;
569
570 return 0;
571}
572
573
574SUPR3DECL(SUPPAGINGMODE) SUPR3GetPagingMode(void)
575{
576 /* fake */
577 if (RT_UNLIKELY(g_uSupFakeMode))
578#ifdef RT_ARCH_AMD64
579 return SUPPAGINGMODE_AMD64_GLOBAL_NX;
580#else
581 return SUPPAGINGMODE_32_BIT_GLOBAL;
582#endif
583
584 /*
585 * Issue IOCtl to the SUPDRV kernel module.
586 */
587 SUPGETPAGINGMODE Req;
588 Req.Hdr.u32Cookie = g_u32Cookie;
589 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
590 Req.Hdr.cbIn = SUP_IOCTL_GET_PAGING_MODE_SIZE_IN;
591 Req.Hdr.cbOut = SUP_IOCTL_GET_PAGING_MODE_SIZE_OUT;
592 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
593 Req.Hdr.rc = VERR_INTERNAL_ERROR;
594 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_GET_PAGING_MODE, &Req, SUP_IOCTL_GET_PAGING_MODE_SIZE);
595 if ( RT_FAILURE(rc)
596 || RT_FAILURE(Req.Hdr.rc))
597 {
598 LogRel(("SUPR3GetPagingMode: %Rrc %Rrc\n", rc, Req.Hdr.rc));
599 Req.u.Out.enmMode = SUPPAGINGMODE_INVALID;
600 }
601
602 return Req.u.Out.enmMode;
603}
604
605
606/**
607 * For later.
608 */
609static int supCallVMMR0ExFake(PVMR0 pVMR0, unsigned uOperation, uint64_t u64Arg, PSUPVMMR0REQHDR pReqHdr)
610{
611 AssertMsgFailed(("%d\n", uOperation)); NOREF(pVMR0); NOREF(uOperation); NOREF(u64Arg); NOREF(pReqHdr);
612 return VERR_NOT_SUPPORTED;
613}
614
615
616SUPR3DECL(int) SUPR3CallVMMR0Fast(PVMR0 pVMR0, unsigned uOperation, VMCPUID idCpu)
617{
618 NOREF(pVMR0);
619 if (RT_LIKELY(uOperation == SUP_VMMR0_DO_RAW_RUN))
620 return suplibOsIOCtlFast(&g_supLibData, SUP_IOCTL_FAST_DO_RAW_RUN, idCpu);
621 if (RT_LIKELY(uOperation == SUP_VMMR0_DO_HM_RUN))
622 return suplibOsIOCtlFast(&g_supLibData, SUP_IOCTL_FAST_DO_HM_RUN, idCpu);
623 if (RT_LIKELY(uOperation == SUP_VMMR0_DO_NOP))
624 return suplibOsIOCtlFast(&g_supLibData, SUP_IOCTL_FAST_DO_NOP, idCpu);
625
626 AssertMsgFailed(("%#x\n", uOperation));
627 return VERR_INTERNAL_ERROR;
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 /*
1119 * Use fallback for non-R0 mapping?
1120 */
1121 if ( !pR0Ptr
1122 && !g_fSupportsPageAllocNoKernel)
1123 return supPagePageAllocNoKernelFallback(cPages, ppvPages, paPages);
1124
1125 /*
1126 * Issue IOCtl to the SUPDRV kernel module.
1127 */
1128 int rc;
1129 PSUPPAGEALLOCEX pReq = (PSUPPAGEALLOCEX)RTMemTmpAllocZ(SUP_IOCTL_PAGE_ALLOC_EX_SIZE(cPages));
1130 if (pReq)
1131 {
1132 pReq->Hdr.u32Cookie = g_u32Cookie;
1133 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
1134 pReq->Hdr.cbIn = SUP_IOCTL_PAGE_ALLOC_EX_SIZE_IN;
1135 pReq->Hdr.cbOut = SUP_IOCTL_PAGE_ALLOC_EX_SIZE_OUT(cPages);
1136 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
1137 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
1138 pReq->u.In.cPages = (uint32_t)cPages; AssertRelease(pReq->u.In.cPages == cPages);
1139 pReq->u.In.fKernelMapping = pR0Ptr != NULL;
1140 pReq->u.In.fUserMapping = true;
1141 pReq->u.In.fReserved0 = false;
1142 pReq->u.In.fReserved1 = false;
1143 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_ALLOC_EX, pReq, SUP_IOCTL_PAGE_ALLOC_EX_SIZE(cPages));
1144 if (RT_SUCCESS(rc))
1145 {
1146 rc = pReq->Hdr.rc;
1147 if (RT_SUCCESS(rc))
1148 {
1149 *ppvPages = pReq->u.Out.pvR3;
1150 if (pR0Ptr)
1151 *pR0Ptr = pReq->u.Out.pvR0;
1152 if (paPages)
1153 for (size_t iPage = 0; iPage < cPages; iPage++)
1154 {
1155 paPages[iPage].uReserved = 0;
1156 paPages[iPage].Phys = pReq->u.Out.aPages[iPage];
1157 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
1158 }
1159#ifdef RT_OS_DARWIN /* HACK ALERT! */
1160 supR3TouchPages(pReq->u.Out.pvR3, cPages);
1161#endif
1162 }
1163 else if ( rc == VERR_NOT_SUPPORTED
1164 && !pR0Ptr)
1165 {
1166 g_fSupportsPageAllocNoKernel = false;
1167 rc = supPagePageAllocNoKernelFallback(cPages, ppvPages, paPages);
1168 }
1169 }
1170
1171 RTMemTmpFree(pReq);
1172 }
1173 else
1174 rc = VERR_NO_TMP_MEMORY;
1175 return rc;
1176
1177}
1178
1179
1180SUPR3DECL(int) SUPR3PageMapKernel(void *pvR3, uint32_t off, uint32_t cb, uint32_t fFlags, PRTR0PTR pR0Ptr)
1181{
1182 /*
1183 * Validate.
1184 */
1185 AssertPtrReturn(pvR3, VERR_INVALID_POINTER);
1186 AssertPtrReturn(pR0Ptr, VERR_INVALID_POINTER);
1187 Assert(!(off & PAGE_OFFSET_MASK));
1188 Assert(!(cb & PAGE_OFFSET_MASK) && cb);
1189 Assert(!fFlags);
1190 *pR0Ptr = NIL_RTR0PTR;
1191
1192 /* fake */
1193 if (RT_UNLIKELY(g_uSupFakeMode))
1194 return VERR_NOT_SUPPORTED;
1195
1196 /*
1197 * Issue IOCtl to the SUPDRV kernel module.
1198 */
1199 SUPPAGEMAPKERNEL Req;
1200 Req.Hdr.u32Cookie = g_u32Cookie;
1201 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1202 Req.Hdr.cbIn = SUP_IOCTL_PAGE_MAP_KERNEL_SIZE_IN;
1203 Req.Hdr.cbOut = SUP_IOCTL_PAGE_MAP_KERNEL_SIZE_OUT;
1204 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1205 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1206 Req.u.In.pvR3 = pvR3;
1207 Req.u.In.offSub = off;
1208 Req.u.In.cbSub = cb;
1209 Req.u.In.fFlags = fFlags;
1210 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_MAP_KERNEL, &Req, SUP_IOCTL_PAGE_MAP_KERNEL_SIZE);
1211 if (RT_SUCCESS(rc))
1212 rc = Req.Hdr.rc;
1213 if (RT_SUCCESS(rc))
1214 *pR0Ptr = Req.u.Out.pvR0;
1215 return rc;
1216}
1217
1218
1219SUPR3DECL(int) SUPR3PageProtect(void *pvR3, RTR0PTR R0Ptr, uint32_t off, uint32_t cb, uint32_t fProt)
1220{
1221 /*
1222 * Validate.
1223 */
1224 AssertPtrReturn(pvR3, VERR_INVALID_POINTER);
1225 Assert(!(off & PAGE_OFFSET_MASK));
1226 Assert(!(cb & PAGE_OFFSET_MASK) && cb);
1227 AssertReturn(!(fProt & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC)), VERR_INVALID_PARAMETER);
1228
1229 /* fake */
1230 if (RT_UNLIKELY(g_uSupFakeMode))
1231 return RTMemProtect((uint8_t *)pvR3 + off, cb, fProt);
1232
1233 /*
1234 * Some OSes can do this from ring-3, so try that before we
1235 * issue the IOCtl to the SUPDRV kernel module.
1236 * (Yea, this isn't very nice, but just try get the job done for now.)
1237 */
1238#if !defined(RT_OS_SOLARIS)
1239 RTMemProtect((uint8_t *)pvR3 + off, cb, fProt);
1240#endif
1241
1242 SUPPAGEPROTECT Req;
1243 Req.Hdr.u32Cookie = g_u32Cookie;
1244 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1245 Req.Hdr.cbIn = SUP_IOCTL_PAGE_PROTECT_SIZE_IN;
1246 Req.Hdr.cbOut = SUP_IOCTL_PAGE_PROTECT_SIZE_OUT;
1247 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1248 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1249 Req.u.In.pvR3 = pvR3;
1250 Req.u.In.pvR0 = R0Ptr;
1251 Req.u.In.offSub = off;
1252 Req.u.In.cbSub = cb;
1253 Req.u.In.fProt = fProt;
1254 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_PROTECT, &Req, SUP_IOCTL_PAGE_PROTECT_SIZE);
1255 if (RT_SUCCESS(rc))
1256 rc = Req.Hdr.rc;
1257 return rc;
1258}
1259
1260
1261SUPR3DECL(int) SUPR3PageFreeEx(void *pvPages, size_t cPages)
1262{
1263 /*
1264 * Validate.
1265 */
1266 AssertPtrReturn(pvPages, VERR_INVALID_POINTER);
1267 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1268
1269 /* fake */
1270 if (RT_UNLIKELY(g_uSupFakeMode))
1271 {
1272 RTMemPageFree(pvPages, cPages * PAGE_SIZE);
1273 return VINF_SUCCESS;
1274 }
1275
1276 /*
1277 * Try normal free first, then if it fails check if we're using the fallback
1278 * for the allocations without kernel mappings and attempt unlocking it.
1279 */
1280 NOREF(cPages);
1281 SUPPAGEFREE Req;
1282 Req.Hdr.u32Cookie = g_u32Cookie;
1283 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1284 Req.Hdr.cbIn = SUP_IOCTL_PAGE_FREE_SIZE_IN;
1285 Req.Hdr.cbOut = SUP_IOCTL_PAGE_FREE_SIZE_OUT;
1286 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1287 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1288 Req.u.In.pvR3 = pvPages;
1289 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_FREE, &Req, SUP_IOCTL_PAGE_FREE_SIZE);
1290 if (RT_SUCCESS(rc))
1291 {
1292 rc = Req.Hdr.rc;
1293 if ( rc == VERR_INVALID_PARAMETER
1294 && !g_fSupportsPageAllocNoKernel)
1295 {
1296 int rc2 = supR3PageUnlock(pvPages);
1297 if (RT_SUCCESS(rc2))
1298 rc = suplibOsPageFree(&g_supLibData, pvPages, cPages);
1299 }
1300 }
1301 return rc;
1302}
1303
1304
1305SUPR3DECL(void *) SUPR3ContAlloc(size_t cPages, PRTR0PTR pR0Ptr, PRTHCPHYS pHCPhys)
1306{
1307 /*
1308 * Validate.
1309 */
1310 AssertPtrReturn(pHCPhys, NULL);
1311 *pHCPhys = NIL_RTHCPHYS;
1312 AssertPtrNullReturn(pR0Ptr, NULL);
1313 if (pR0Ptr)
1314 *pR0Ptr = NIL_RTR0PTR;
1315 AssertPtrNullReturn(pHCPhys, NULL);
1316 AssertMsgReturn(cPages > 0 && cPages < 256, ("cPages=%d must be > 0 and < 256\n", cPages), NULL);
1317
1318 /* fake */
1319 if (RT_UNLIKELY(g_uSupFakeMode))
1320 {
1321 void *pv = RTMemPageAllocZ(cPages * PAGE_SIZE);
1322 if (pR0Ptr)
1323 *pR0Ptr = (RTR0PTR)pv;
1324 if (pHCPhys)
1325 *pHCPhys = (uintptr_t)pv + (PAGE_SHIFT * 1024);
1326 return pv;
1327 }
1328
1329 /*
1330 * Issue IOCtl to the SUPDRV kernel module.
1331 */
1332 SUPCONTALLOC Req;
1333 Req.Hdr.u32Cookie = g_u32Cookie;
1334 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1335 Req.Hdr.cbIn = SUP_IOCTL_CONT_ALLOC_SIZE_IN;
1336 Req.Hdr.cbOut = SUP_IOCTL_CONT_ALLOC_SIZE_OUT;
1337 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1338 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1339 Req.u.In.cPages = (uint32_t)cPages;
1340 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CONT_ALLOC, &Req, SUP_IOCTL_CONT_ALLOC_SIZE);
1341 if ( RT_SUCCESS(rc)
1342 && RT_SUCCESS(Req.Hdr.rc))
1343 {
1344 *pHCPhys = Req.u.Out.HCPhys;
1345 if (pR0Ptr)
1346 *pR0Ptr = Req.u.Out.pvR0;
1347#ifdef RT_OS_DARWIN /* HACK ALERT! */
1348 supR3TouchPages(Req.u.Out.pvR3, cPages);
1349#endif
1350 return Req.u.Out.pvR3;
1351 }
1352
1353 return NULL;
1354}
1355
1356
1357SUPR3DECL(int) SUPR3ContFree(void *pv, size_t cPages)
1358{
1359 /*
1360 * Validate.
1361 */
1362 if (!pv)
1363 return VINF_SUCCESS;
1364 AssertPtrReturn(pv, VERR_INVALID_POINTER);
1365 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1366
1367 /* fake */
1368 if (RT_UNLIKELY(g_uSupFakeMode))
1369 {
1370 RTMemPageFree(pv, cPages * PAGE_SIZE);
1371 return VINF_SUCCESS;
1372 }
1373
1374 /*
1375 * Issue IOCtl to the SUPDRV kernel module.
1376 */
1377 SUPCONTFREE Req;
1378 Req.Hdr.u32Cookie = g_u32Cookie;
1379 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1380 Req.Hdr.cbIn = SUP_IOCTL_CONT_FREE_SIZE_IN;
1381 Req.Hdr.cbOut = SUP_IOCTL_CONT_FREE_SIZE_OUT;
1382 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1383 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1384 Req.u.In.pvR3 = pv;
1385 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CONT_FREE, &Req, SUP_IOCTL_CONT_FREE_SIZE);
1386 if (RT_SUCCESS(rc))
1387 rc = Req.Hdr.rc;
1388 return rc;
1389}
1390
1391
1392SUPR3DECL(int) SUPR3LowAlloc(size_t cPages, void **ppvPages, PRTR0PTR ppvPagesR0, PSUPPAGE paPages)
1393{
1394 /*
1395 * Validate.
1396 */
1397 AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
1398 *ppvPages = NULL;
1399 AssertPtrReturn(paPages, VERR_INVALID_POINTER);
1400 AssertMsgReturn(cPages > 0 && cPages < 256, ("cPages=%d must be > 0 and < 256\n", cPages), VERR_PAGE_COUNT_OUT_OF_RANGE);
1401
1402 /* fake */
1403 if (RT_UNLIKELY(g_uSupFakeMode))
1404 {
1405 *ppvPages = RTMemPageAllocZ((size_t)cPages * PAGE_SIZE);
1406 if (!*ppvPages)
1407 return VERR_NO_LOW_MEMORY;
1408
1409 /* fake physical addresses. */
1410 RTHCPHYS Phys = (uintptr_t)*ppvPages + PAGE_SIZE * 1024;
1411 size_t iPage = cPages;
1412 while (iPage-- > 0)
1413 paPages[iPage].Phys = Phys + (iPage << PAGE_SHIFT);
1414 return VINF_SUCCESS;
1415 }
1416
1417 /*
1418 * Issue IOCtl to the SUPDRV kernel module.
1419 */
1420 int rc;
1421 PSUPLOWALLOC pReq = (PSUPLOWALLOC)RTMemTmpAllocZ(SUP_IOCTL_LOW_ALLOC_SIZE(cPages));
1422 if (pReq)
1423 {
1424 pReq->Hdr.u32Cookie = g_u32Cookie;
1425 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
1426 pReq->Hdr.cbIn = SUP_IOCTL_LOW_ALLOC_SIZE_IN;
1427 pReq->Hdr.cbOut = SUP_IOCTL_LOW_ALLOC_SIZE_OUT(cPages);
1428 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
1429 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
1430 pReq->u.In.cPages = (uint32_t)cPages; AssertRelease(pReq->u.In.cPages == cPages);
1431 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LOW_ALLOC, pReq, SUP_IOCTL_LOW_ALLOC_SIZE(cPages));
1432 if (RT_SUCCESS(rc))
1433 rc = pReq->Hdr.rc;
1434 if (RT_SUCCESS(rc))
1435 {
1436 *ppvPages = pReq->u.Out.pvR3;
1437 if (ppvPagesR0)
1438 *ppvPagesR0 = pReq->u.Out.pvR0;
1439 if (paPages)
1440 for (size_t iPage = 0; iPage < cPages; iPage++)
1441 {
1442 paPages[iPage].uReserved = 0;
1443 paPages[iPage].Phys = pReq->u.Out.aPages[iPage];
1444 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
1445 Assert(paPages[iPage].Phys <= UINT32_C(0xfffff000));
1446 }
1447#ifdef RT_OS_DARWIN /* HACK ALERT! */
1448 supR3TouchPages(pReq->u.Out.pvR3, cPages);
1449#endif
1450 }
1451 RTMemTmpFree(pReq);
1452 }
1453 else
1454 rc = VERR_NO_TMP_MEMORY;
1455
1456 return rc;
1457}
1458
1459
1460SUPR3DECL(int) SUPR3LowFree(void *pv, size_t cPages)
1461{
1462 /*
1463 * Validate.
1464 */
1465 if (!pv)
1466 return VINF_SUCCESS;
1467 AssertPtrReturn(pv, VERR_INVALID_POINTER);
1468 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1469
1470 /* fake */
1471 if (RT_UNLIKELY(g_uSupFakeMode))
1472 {
1473 RTMemPageFree(pv, cPages * PAGE_SIZE);
1474 return VINF_SUCCESS;
1475 }
1476
1477 /*
1478 * Issue IOCtl to the SUPDRV kernel module.
1479 */
1480 SUPCONTFREE Req;
1481 Req.Hdr.u32Cookie = g_u32Cookie;
1482 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1483 Req.Hdr.cbIn = SUP_IOCTL_LOW_FREE_SIZE_IN;
1484 Req.Hdr.cbOut = SUP_IOCTL_LOW_FREE_SIZE_OUT;
1485 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1486 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1487 Req.u.In.pvR3 = pv;
1488 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LOW_FREE, &Req, SUP_IOCTL_LOW_FREE_SIZE);
1489 if (RT_SUCCESS(rc))
1490 rc = Req.Hdr.rc;
1491 return rc;
1492}
1493
1494
1495SUPR3DECL(int) SUPR3HardenedVerifyInit(void)
1496{
1497#ifdef RT_OS_WINDOWS
1498 if (g_cInits == 0)
1499 return suplibOsHardenedVerifyInit();
1500#endif
1501 return VINF_SUCCESS;
1502}
1503
1504
1505SUPR3DECL(int) SUPR3HardenedVerifyTerm(void)
1506{
1507#ifdef RT_OS_WINDOWS
1508 if (g_cInits == 0)
1509 return suplibOsHardenedVerifyTerm();
1510#endif
1511 return VINF_SUCCESS;
1512}
1513
1514
1515SUPR3DECL(int) SUPR3HardenedVerifyFile(const char *pszFilename, const char *pszMsg, PRTFILE phFile)
1516{
1517 /*
1518 * Quick input validation.
1519 */
1520 AssertPtr(pszFilename);
1521 AssertPtr(pszMsg);
1522 AssertReturn(!phFile, VERR_NOT_IMPLEMENTED); /** @todo Implement this. The deal is that we make sure the
1523 file is the same we verified after opening it. */
1524
1525 /*
1526 * Only do the actual check in hardened builds.
1527 */
1528#ifdef VBOX_WITH_HARDENING
1529 int rc = supR3HardenedVerifyFixedFile(pszFilename, false /* fFatal */);
1530 if (RT_FAILURE(rc))
1531 LogRel(("SUPR3HardenedVerifyFile: %s: Verification of \"%s\" failed, rc=%Rrc\n", pszMsg, pszFilename, rc));
1532 return rc;
1533#else
1534 return VINF_SUCCESS;
1535#endif
1536}
1537
1538
1539SUPR3DECL(int) SUPR3HardenedVerifySelf(const char *pszArgv0, bool fInternal, PRTERRINFO pErrInfo)
1540{
1541 /*
1542 * Quick input validation.
1543 */
1544 AssertPtr(pszArgv0);
1545 RTErrInfoClear(pErrInfo);
1546
1547 /*
1548 * Get the executable image path as we need it for all the tests here.
1549 */
1550 char szExecPath[RTPATH_MAX];
1551 if (!RTProcGetExecutablePath(szExecPath, sizeof(szExecPath)))
1552 return RTErrInfoSet(pErrInfo, VERR_INTERNAL_ERROR_2, "RTProcGetExecutablePath failed");
1553
1554 int rc;
1555 if (fInternal)
1556 {
1557 /*
1558 * Internal applications must be launched directly without any PATH
1559 * searching involved.
1560 */
1561 if (RTPathCompare(pszArgv0, szExecPath) != 0)
1562 return RTErrInfoSetF(pErrInfo, VERR_SUPLIB_INVALID_ARGV0_INTERNAL,
1563 "argv[0] does not match the executable image path: '%s' != '%s'", pszArgv0, szExecPath);
1564
1565 /*
1566 * Internal applications must reside in or under the
1567 * RTPathAppPrivateArch directory.
1568 */
1569 char szAppPrivateArch[RTPATH_MAX];
1570 rc = RTPathAppPrivateArch(szAppPrivateArch, sizeof(szAppPrivateArch));
1571 if (RT_FAILURE(rc))
1572 return RTErrInfoSetF(pErrInfo, VERR_SUPLIB_INVALID_ARGV0_INTERNAL,
1573 "RTPathAppPrivateArch failed with rc=%Rrc", rc);
1574 size_t cchAppPrivateArch = strlen(szAppPrivateArch);
1575 if ( cchAppPrivateArch >= strlen(szExecPath)
1576 || !RTPATH_IS_SLASH(szExecPath[cchAppPrivateArch]))
1577 return RTErrInfoSet(pErrInfo, VERR_SUPLIB_INVALID_INTERNAL_APP_DIR,
1578 "Internal executable does reside under RTPathAppPrivateArch");
1579 szExecPath[cchAppPrivateArch] = '\0';
1580 if (RTPathCompare(szExecPath, szAppPrivateArch) != 0)
1581 return RTErrInfoSet(pErrInfo, VERR_SUPLIB_INVALID_INTERNAL_APP_DIR,
1582 "Internal executable does reside under RTPathAppPrivateArch");
1583 szExecPath[cchAppPrivateArch] = RTPATH_SLASH;
1584 }
1585
1586#ifdef VBOX_WITH_HARDENING
1587 /*
1588 * Verify that the image file and parent directories are sane.
1589 */
1590 rc = supR3HardenedVerifyFile(szExecPath, RTHCUINTPTR_MAX, false /*fMaybe3rdParty*/, pErrInfo);
1591 if (RT_FAILURE(rc))
1592 return rc;
1593#endif
1594
1595 return VINF_SUCCESS;
1596}
1597
1598
1599SUPR3DECL(int) SUPR3HardenedVerifyDir(const char *pszDirPath, bool fRecursive, bool fCheckFiles, PRTERRINFO pErrInfo)
1600{
1601 /*
1602 * Quick input validation
1603 */
1604 AssertPtr(pszDirPath);
1605 RTErrInfoClear(pErrInfo);
1606
1607 /*
1608 * Only do the actual check in hardened builds.
1609 */
1610#ifdef VBOX_WITH_HARDENING
1611 int rc = supR3HardenedVerifyDir(pszDirPath, fRecursive, fCheckFiles, pErrInfo);
1612 if (RT_FAILURE(rc) && !RTErrInfoIsSet(pErrInfo))
1613 LogRel(("supR3HardenedVerifyDir: Verification of \"%s\" failed, rc=%Rrc\n", pszDirPath, rc));
1614 return rc;
1615#else
1616 NOREF(pszDirPath); NOREF(fRecursive); NOREF(fCheckFiles);
1617 return VINF_SUCCESS;
1618#endif
1619}
1620
1621
1622SUPR3DECL(int) SUPR3HardenedVerifyPlugIn(const char *pszFilename, PRTERRINFO pErrInfo)
1623{
1624 /*
1625 * Quick input validation
1626 */
1627 AssertPtr(pszFilename);
1628 RTErrInfoClear(pErrInfo);
1629
1630 /*
1631 * Only do the actual check in hardened builds.
1632 */
1633#ifdef VBOX_WITH_HARDENING
1634 int rc = supR3HardenedVerifyFile(pszFilename, RTHCUINTPTR_MAX, true /*fMaybe3rdParty*/, pErrInfo);
1635 if (RT_FAILURE(rc) && !RTErrInfoIsSet(pErrInfo))
1636 LogRel(("supR3HardenedVerifyFile: Verification of \"%s\" failed, rc=%Rrc\n", pszFilename, rc));
1637 return rc;
1638#else
1639 return VINF_SUCCESS;
1640#endif
1641}
1642
1643
1644SUPR3DECL(int) SUPR3GipGetPhys(PRTHCPHYS pHCPhys)
1645{
1646 if (g_pSUPGlobalInfoPage)
1647 {
1648 *pHCPhys = g_HCPhysSUPGlobalInfoPage;
1649 return VINF_SUCCESS;
1650 }
1651 *pHCPhys = NIL_RTHCPHYS;
1652 return VERR_WRONG_ORDER;
1653}
1654
1655
1656SUPR3DECL(int) SUPR3QueryVTxSupported(void)
1657{
1658#ifdef RT_OS_LINUX
1659 return suplibOsQueryVTxSupported();
1660#else
1661 return VINF_SUCCESS;
1662#endif
1663}
1664
1665
1666SUPR3DECL(int) SUPR3QueryVTCaps(uint32_t *pfCaps)
1667{
1668 AssertPtrReturn(pfCaps, VERR_INVALID_POINTER);
1669
1670 *pfCaps = 0;
1671
1672 /* fake */
1673 if (RT_UNLIKELY(g_uSupFakeMode))
1674 return VINF_SUCCESS;
1675
1676 /*
1677 * Issue IOCtl to the SUPDRV kernel module.
1678 */
1679 SUPVTCAPS Req;
1680 Req.Hdr.u32Cookie = g_u32Cookie;
1681 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1682 Req.Hdr.cbIn = SUP_IOCTL_VT_CAPS_SIZE_IN;
1683 Req.Hdr.cbOut = SUP_IOCTL_VT_CAPS_SIZE_OUT;
1684 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1685 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1686 Req.u.Out.Caps = 0;
1687 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_VT_CAPS, &Req, SUP_IOCTL_VT_CAPS_SIZE);
1688 if (RT_SUCCESS(rc))
1689 {
1690 rc = Req.Hdr.rc;
1691 if (RT_SUCCESS(rc))
1692 *pfCaps = Req.u.Out.Caps;
1693 }
1694 return rc;
1695}
1696
1697
1698SUPR3DECL(int) SUPR3TracerOpen(uint32_t uCookie, uintptr_t uArg)
1699{
1700 /* fake */
1701 if (RT_UNLIKELY(g_uSupFakeMode))
1702 return VINF_SUCCESS;
1703
1704 /*
1705 * Issue IOCtl to the SUPDRV kernel module.
1706 */
1707 SUPTRACEROPEN Req;
1708 Req.Hdr.u32Cookie = g_u32Cookie;
1709 Req.Hdr.u32SessionCookie= g_u32SessionCookie;
1710 Req.Hdr.cbIn = SUP_IOCTL_TRACER_OPEN_SIZE_IN;
1711 Req.Hdr.cbOut = SUP_IOCTL_TRACER_OPEN_SIZE_OUT;
1712 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1713 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1714 Req.u.In.uCookie = uCookie;
1715 Req.u.In.uArg = uArg;
1716 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_OPEN, &Req, SUP_IOCTL_TRACER_OPEN_SIZE);
1717 if (RT_SUCCESS(rc))
1718 rc = Req.Hdr.rc;
1719 return rc;
1720}
1721
1722
1723SUPR3DECL(int) SUPR3TracerClose(void)
1724{
1725 /* fake */
1726 if (RT_UNLIKELY(g_uSupFakeMode))
1727 return VINF_SUCCESS;
1728
1729 /*
1730 * Issue IOCtl to the SUPDRV kernel module.
1731 */
1732 SUPREQHDR Req;
1733 Req.u32Cookie = g_u32Cookie;
1734 Req.u32SessionCookie= g_u32SessionCookie;
1735 Req.cbIn = SUP_IOCTL_TRACER_OPEN_SIZE_IN;
1736 Req.cbOut = SUP_IOCTL_TRACER_OPEN_SIZE_OUT;
1737 Req.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1738 Req.rc = VERR_INTERNAL_ERROR;
1739 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_CLOSE, &Req, SUP_IOCTL_TRACER_CLOSE_SIZE);
1740 if (RT_SUCCESS(rc))
1741 rc = Req.rc;
1742 return rc;
1743}
1744
1745
1746SUPR3DECL(int) SUPR3TracerIoCtl(uintptr_t uCmd, uintptr_t uArg, int32_t *piRetVal)
1747{
1748 /* fake */
1749 if (RT_UNLIKELY(g_uSupFakeMode))
1750 {
1751 *piRetVal = -1;
1752 return VERR_NOT_SUPPORTED;
1753 }
1754
1755 /*
1756 * Issue IOCtl to the SUPDRV kernel module.
1757 */
1758 SUPTRACERIOCTL Req;
1759 Req.Hdr.u32Cookie = g_u32Cookie;
1760 Req.Hdr.u32SessionCookie= g_u32SessionCookie;
1761 Req.Hdr.cbIn = SUP_IOCTL_TRACER_IOCTL_SIZE_IN;
1762 Req.Hdr.cbOut = SUP_IOCTL_TRACER_IOCTL_SIZE_OUT;
1763 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1764 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1765 Req.u.In.uCmd = uCmd;
1766 Req.u.In.uArg = uArg;
1767 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_IOCTL, &Req, SUP_IOCTL_TRACER_IOCTL_SIZE);
1768 if (RT_SUCCESS(rc))
1769 {
1770 rc = Req.Hdr.rc;
1771 *piRetVal = Req.u.Out.iRetVal;
1772 }
1773 return rc;
1774}
1775
1776
1777
1778typedef struct SUPDRVTRACERSTRTAB
1779{
1780 /** Pointer to the string table. */
1781 char *pchStrTab;
1782 /** The actual string table size. */
1783 uint32_t cbStrTab;
1784 /** The original string pointers. */
1785 RTUINTPTR apszOrgFunctions[1];
1786} SUPDRVTRACERSTRTAB, *PSUPDRVTRACERSTRTAB;
1787
1788
1789/**
1790 * Destroys a string table, restoring the original pszFunction member valus.
1791 *
1792 * @param pThis The string table structure.
1793 * @param paProbeLocs32 The probe location array, 32-bit type variant.
1794 * @param paProbeLocs64 The probe location array, 64-bit type variant.
1795 * @param cProbeLocs The number of elements in the array.
1796 * @param f32Bit Set if @a paProbeLocs32 should be used, when
1797 * clear use @a paProbeLocs64.
1798 */
1799static void supr3TracerDestroyStrTab(PSUPDRVTRACERSTRTAB pThis, PVTGPROBELOC32 paProbeLocs32, PVTGPROBELOC64 paProbeLocs64,
1800 uint32_t cProbeLocs, bool f32Bit)
1801{
1802 /* Restore. */
1803 size_t i = cProbeLocs;
1804 if (f32Bit)
1805 while (i--)
1806 paProbeLocs32[i].pszFunction = (uint32_t)pThis->apszOrgFunctions[i];
1807 else
1808 while (i--)
1809 paProbeLocs64[i].pszFunction = pThis->apszOrgFunctions[i];
1810
1811 /* Free. */
1812 RTMemFree(pThis->pchStrTab);
1813 RTMemFree(pThis);
1814}
1815
1816
1817/**
1818 * Creates a string table for the pszFunction members in the probe location
1819 * array.
1820 *
1821 * This will save and replace the pszFunction members with offsets.
1822 *
1823 * @returns Pointer to a string table structure. NULL on failure.
1824 * @param paProbeLocs32 The probe location array, 32-bit type variant.
1825 * @param paProbeLocs64 The probe location array, 64-bit type variant.
1826 * @param cProbeLocs The number of elements in the array.
1827 * @param offDelta Relocation offset for the string pointers.
1828 * @param f32Bit Set if @a paProbeLocs32 should be used, when
1829 * clear use @a paProbeLocs64.
1830 */
1831static PSUPDRVTRACERSTRTAB supr3TracerCreateStrTab(PVTGPROBELOC32 paProbeLocs32,
1832 PVTGPROBELOC64 paProbeLocs64,
1833 uint32_t cProbeLocs,
1834 RTUINTPTR offDelta,
1835 bool f32Bit)
1836{
1837 if (cProbeLocs > _128K)
1838 return NULL;
1839
1840 /*
1841 * Allocate the string table structures.
1842 */
1843 size_t cbThis = RT_OFFSETOF(SUPDRVTRACERSTRTAB, apszOrgFunctions[cProbeLocs]);
1844 PSUPDRVTRACERSTRTAB pThis = (PSUPDRVTRACERSTRTAB)RTMemAlloc(cbThis);
1845 if (!pThis)
1846 return NULL;
1847
1848 uint32_t const cHashBits = cProbeLocs * 2 - 1;
1849 uint32_t *pbmHash = (uint32_t *)RTMemAllocZ(RT_ALIGN_32(cHashBits, 64) / 8 );
1850 if (!pbmHash)
1851 {
1852 RTMemFree(pThis);
1853 return NULL;
1854 }
1855
1856 /*
1857 * Calc the max string table size and save the orignal pointers so we can
1858 * replace them later.
1859 */
1860 size_t cbMax = 1;
1861 for (uint32_t i = 0; i < cProbeLocs; i++)
1862 {
1863 pThis->apszOrgFunctions[i] = f32Bit ? paProbeLocs32[i].pszFunction : paProbeLocs64[i].pszFunction;
1864 const char *pszFunction = (const char *)(uintptr_t)(pThis->apszOrgFunctions[i] + offDelta);
1865 size_t cch = strlen(pszFunction);
1866 if (cch > _1K)
1867 {
1868 cbMax = 0;
1869 break;
1870 }
1871 cbMax += cch + 1;
1872 }
1873
1874 /* Alloc space for it. */
1875 if (cbMax > 0)
1876 pThis->pchStrTab = (char *)RTMemAlloc(cbMax);
1877 else
1878 pThis->pchStrTab = NULL;
1879 if (!pThis->pchStrTab)
1880 {
1881 RTMemFree(pbmHash);
1882 RTMemFree(pThis);
1883 return NULL;
1884 }
1885
1886 /*
1887 * Create the string table.
1888 */
1889 uint32_t off = 0;
1890 uint32_t offPrev = 0;
1891
1892 for (uint32_t i = 0; i < cProbeLocs; i++)
1893 {
1894 const char * const psz = (const char *)(uintptr_t)(pThis->apszOrgFunctions[i] + offDelta);
1895 size_t const cch = strlen(psz);
1896 uint32_t const iHashBit = RTStrHash1(psz) % cHashBits;
1897 if (ASMBitTestAndSet(pbmHash, iHashBit))
1898 {
1899 /* Often it's the most recent string. */
1900 if ( off - offPrev < cch + 1
1901 || memcmp(&pThis->pchStrTab[offPrev], psz, cch + 1))
1902 {
1903 /* It wasn't, search the entire string table. (lazy bird) */
1904 offPrev = 0;
1905 while (offPrev < off)
1906 {
1907 size_t cchCur = strlen(&pThis->pchStrTab[offPrev]);
1908 if ( cchCur == cch
1909 && !memcmp(&pThis->pchStrTab[offPrev], psz, cch + 1))
1910 break;
1911 offPrev += (uint32_t)cchCur + 1;
1912 }
1913 }
1914 }
1915 else
1916 offPrev = off;
1917
1918 /* Add the string to the table. */
1919 if (offPrev >= off)
1920 {
1921 memcpy(&pThis->pchStrTab[off], psz, cch + 1);
1922 offPrev = off;
1923 off += (uint32_t)cch + 1;
1924 }
1925
1926 /* Update the entry */
1927 if (f32Bit)
1928 paProbeLocs32[i].pszFunction = offPrev;
1929 else
1930 paProbeLocs64[i].pszFunction = offPrev;
1931 }
1932
1933 pThis->cbStrTab = off;
1934 RTMemFree(pbmHash);
1935 return pThis;
1936}
1937
1938
1939
1940SUPR3DECL(int) SUPR3TracerRegisterModule(uintptr_t hModNative, const char *pszModule, struct VTGOBJHDR *pVtgHdr,
1941 RTUINTPTR uVtgHdrAddr, uint32_t fFlags)
1942{
1943 /* Validate input. */
1944 NOREF(hModNative);
1945 AssertPtrReturn(pVtgHdr, VERR_INVALID_POINTER);
1946 AssertReturn(!memcmp(pVtgHdr->szMagic, VTGOBJHDR_MAGIC, sizeof(pVtgHdr->szMagic)), VERR_SUPDRV_VTG_MAGIC);
1947 AssertPtrReturn(pszModule, VERR_INVALID_POINTER);
1948 size_t cchModule = strlen(pszModule);
1949 AssertReturn(cchModule < RT_SIZEOFMEMB(SUPTRACERUMODREG, u.In.szName), VERR_FILENAME_TOO_LONG);
1950 AssertReturn(!RTPathHavePath(pszModule), VERR_INVALID_PARAMETER);
1951 AssertReturn(fFlags == SUP_TRACER_UMOD_FLAGS_EXE || fFlags == SUP_TRACER_UMOD_FLAGS_SHARED, VERR_INVALID_PARAMETER);
1952
1953 /*
1954 * Set the probe location array offset and size members. If the size is
1955 * zero, don't bother ring-0 with it.
1956 */
1957 if (!pVtgHdr->offProbeLocs)
1958 {
1959 uint64_t u64Tmp = pVtgHdr->uProbeLocsEnd.u64 - pVtgHdr->uProbeLocs.u64;
1960 if (u64Tmp >= UINT32_MAX)
1961 return VERR_SUPDRV_VTG_BAD_HDR_TOO_MUCH;
1962 pVtgHdr->cbProbeLocs = (uint32_t)u64Tmp;
1963
1964 u64Tmp = pVtgHdr->uProbeLocs.u64 - uVtgHdrAddr;
1965 if ((int64_t)u64Tmp != (int32_t)u64Tmp)
1966 {
1967 LogRel(("SUPR3TracerRegisterModule: VERR_SUPDRV_VTG_BAD_HDR_PTR - u64Tmp=%#llx uProbeLocs=%#llx uVtgHdrAddr=%RTptr\n",
1968 u64Tmp, pVtgHdr->uProbeLocs.u64, uVtgHdrAddr));
1969 return VERR_SUPDRV_VTG_BAD_HDR_PTR;
1970 }
1971 pVtgHdr->offProbeLocs = (int32_t)u64Tmp;
1972 }
1973
1974 if ( !pVtgHdr->cbProbeLocs
1975 || !pVtgHdr->cbProbes)
1976 return VINF_SUCCESS;
1977
1978 /*
1979 * Fake out.
1980 */
1981 if (RT_UNLIKELY(g_uSupFakeMode))
1982 return VINF_SUCCESS;
1983
1984 /*
1985 * Create a string table for the function names in the location array.
1986 * It's somewhat easier to do that here than from ring-0.
1987 */
1988 size_t const cProbeLocs = pVtgHdr->cbProbeLocs
1989 / (pVtgHdr->cBits == 32 ? sizeof(VTGPROBELOC32) : sizeof(VTGPROBELOC64));
1990 PVTGPROBELOC paProbeLocs = (PVTGPROBELOC)((uintptr_t)pVtgHdr + pVtgHdr->offProbeLocs);
1991 PSUPDRVTRACERSTRTAB pStrTab = supr3TracerCreateStrTab((PVTGPROBELOC32)paProbeLocs,
1992 (PVTGPROBELOC64)paProbeLocs,
1993 cProbeLocs, (uintptr_t)pVtgHdr - uVtgHdrAddr,
1994 pVtgHdr->cBits == 32);
1995 if (!pStrTab)
1996 return VERR_NO_MEMORY;
1997
1998
1999 /*
2000 * Issue IOCtl to the SUPDRV kernel module.
2001 */
2002 SUPTRACERUMODREG Req;
2003 Req.Hdr.u32Cookie = g_u32Cookie;
2004 Req.Hdr.u32SessionCookie= g_u32SessionCookie;
2005 Req.Hdr.cbIn = SUP_IOCTL_TRACER_UMOD_REG_SIZE_IN;
2006 Req.Hdr.cbOut = SUP_IOCTL_TRACER_UMOD_REG_SIZE_OUT;
2007 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2008 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2009 Req.u.In.uVtgHdrAddr = uVtgHdrAddr;
2010 Req.u.In.R3PtrVtgHdr = pVtgHdr;
2011 Req.u.In.R3PtrStrTab = pStrTab->pchStrTab;
2012 Req.u.In.cbStrTab = pStrTab->cbStrTab;
2013 Req.u.In.fFlags = fFlags;
2014
2015 memcpy(Req.u.In.szName, pszModule, cchModule + 1);
2016 if (!RTPathHasSuffix(Req.u.In.szName))
2017 {
2018 /* Add the default suffix if none is given. */
2019 switch (fFlags & SUP_TRACER_UMOD_FLAGS_TYPE_MASK)
2020 {
2021#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
2022 case SUP_TRACER_UMOD_FLAGS_EXE:
2023 if (cchModule + sizeof(".exe") <= sizeof(Req.u.In.szName))
2024 strcpy(&Req.u.In.szName[cchModule], ".exe");
2025 break;
2026#endif
2027
2028 case SUP_TRACER_UMOD_FLAGS_SHARED:
2029 {
2030 const char *pszSuff = RTLdrGetSuff();
2031 size_t cchSuff = strlen(pszSuff);
2032 if (cchModule + cchSuff < sizeof(Req.u.In.szName))
2033 memcpy(&Req.u.In.szName[cchModule], pszSuff, cchSuff + 1);
2034 break;
2035 }
2036 }
2037 }
2038
2039 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_UMOD_REG, &Req, SUP_IOCTL_TRACER_UMOD_REG_SIZE);
2040 if (RT_SUCCESS(rc))
2041 rc = Req.Hdr.rc;
2042
2043 supr3TracerDestroyStrTab(pStrTab, (PVTGPROBELOC32)paProbeLocs, (PVTGPROBELOC64)paProbeLocs,
2044 cProbeLocs, pVtgHdr->cBits == 32);
2045 return rc;
2046}
2047
2048
2049SUPR3DECL(int) SUPR3TracerDeregisterModule(struct VTGOBJHDR *pVtgHdr)
2050{
2051 /* Validate input. */
2052 AssertPtrReturn(pVtgHdr, VERR_INVALID_POINTER);
2053 AssertReturn(!memcmp(pVtgHdr->szMagic, VTGOBJHDR_MAGIC, sizeof(pVtgHdr->szMagic)), VERR_SUPDRV_VTG_MAGIC);
2054
2055 /*
2056 * Don't bother if the object is empty.
2057 */
2058 if ( !pVtgHdr->cbProbeLocs
2059 || !pVtgHdr->cbProbes)
2060 return VINF_SUCCESS;
2061
2062 /*
2063 * Fake out.
2064 */
2065 if (RT_UNLIKELY(g_uSupFakeMode))
2066 return VINF_SUCCESS;
2067
2068 /*
2069 * Issue IOCtl to the SUPDRV kernel module.
2070 */
2071 SUPTRACERUMODDEREG Req;
2072 Req.Hdr.u32Cookie = g_u32Cookie;
2073 Req.Hdr.u32SessionCookie= g_u32SessionCookie;
2074 Req.Hdr.cbIn = SUP_IOCTL_TRACER_UMOD_REG_SIZE_IN;
2075 Req.Hdr.cbOut = SUP_IOCTL_TRACER_UMOD_REG_SIZE_OUT;
2076 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2077 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2078 Req.u.In.pVtgHdr = pVtgHdr;
2079
2080 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_UMOD_DEREG, &Req, SUP_IOCTL_TRACER_UMOD_DEREG_SIZE);
2081 if (RT_SUCCESS(rc))
2082 rc = Req.Hdr.rc;
2083 return rc;
2084}
2085
2086
2087DECLASM(void) suplibTracerFireProbe(PVTGPROBELOC pProbeLoc, PSUPTRACERUMODFIREPROBE pReq)
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