VirtualBox

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

最後變更 在這個檔案從92697是 92697,由 vboxsync 提交於 3 年 前

SUP: Added SUPR3INIT_F_DRIVERLESS as a forced driverless option and an associated SUPR3IsDriverless(). The SUPR3INIT_F_DRIVERLESS flag requires the host specific init code to allow driverless, only done so on linux thus far. Started adjusting some of the SUPR3 APIs for driverless operation. bugref:10138

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette