VirtualBox

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

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

VMM/APIC,SUP: Driverless tweaks for the APIC code. bugref:10138

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 84.0 KB
 
1/* $Id: SUPLib.cpp 92718 2021-12-02 21:44:14Z 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, 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 /*
1225 * Not a valid operation in driverless mode.
1226 */
1227 AssertReturn(g_supLibData.fDriverless, VERR_SUP_DRIVERLESS);
1228
1229 /*
1230 * Issue IOCtl to the SUPDRV kernel module.
1231 */
1232 SUPPAGEMAPKERNEL Req;
1233 Req.Hdr.u32Cookie = g_u32Cookie;
1234 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1235 Req.Hdr.cbIn = SUP_IOCTL_PAGE_MAP_KERNEL_SIZE_IN;
1236 Req.Hdr.cbOut = SUP_IOCTL_PAGE_MAP_KERNEL_SIZE_OUT;
1237 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1238 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1239 Req.u.In.pvR3 = pvR3;
1240 Req.u.In.offSub = off;
1241 Req.u.In.cbSub = cb;
1242 Req.u.In.fFlags = fFlags;
1243 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_MAP_KERNEL, &Req, SUP_IOCTL_PAGE_MAP_KERNEL_SIZE);
1244 if (RT_SUCCESS(rc))
1245 rc = Req.Hdr.rc;
1246 if (RT_SUCCESS(rc))
1247 *pR0Ptr = Req.u.Out.pvR0;
1248 return rc;
1249}
1250
1251
1252SUPR3DECL(int) SUPR3PageProtect(void *pvR3, RTR0PTR R0Ptr, uint32_t off, uint32_t cb, uint32_t fProt)
1253{
1254 /*
1255 * Validate.
1256 */
1257 AssertPtrReturn(pvR3, VERR_INVALID_POINTER);
1258 Assert(!(off & PAGE_OFFSET_MASK));
1259 Assert(!(cb & PAGE_OFFSET_MASK) && cb);
1260 AssertReturn(!(fProt & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC)), VERR_INVALID_PARAMETER);
1261
1262 /*
1263 * Deal with driverless mode first.
1264 */
1265 if (g_supLibData.fDriverless)
1266 return RTMemProtect((uint8_t *)pvR3 + off, cb, fProt);
1267
1268 /*
1269 * Some OSes can do this from ring-3, so try that before we
1270 * issue the IOCtl to the SUPDRV kernel module.
1271 * (Yea, this isn't very nice, but just try get the job done for now.)
1272 */
1273#if !defined(RT_OS_SOLARIS)
1274 RTMemProtect((uint8_t *)pvR3 + off, cb, fProt);
1275#endif
1276
1277 SUPPAGEPROTECT Req;
1278 Req.Hdr.u32Cookie = g_u32Cookie;
1279 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1280 Req.Hdr.cbIn = SUP_IOCTL_PAGE_PROTECT_SIZE_IN;
1281 Req.Hdr.cbOut = SUP_IOCTL_PAGE_PROTECT_SIZE_OUT;
1282 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1283 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1284 Req.u.In.pvR3 = pvR3;
1285 Req.u.In.pvR0 = R0Ptr;
1286 Req.u.In.offSub = off;
1287 Req.u.In.cbSub = cb;
1288 Req.u.In.fProt = fProt;
1289 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_PROTECT, &Req, SUP_IOCTL_PAGE_PROTECT_SIZE);
1290 if (RT_SUCCESS(rc))
1291 rc = Req.Hdr.rc;
1292 return rc;
1293}
1294
1295
1296SUPR3DECL(int) SUPR3PageFreeEx(void *pvPages, size_t cPages)
1297{
1298 /*
1299 * Validate.
1300 */
1301 AssertPtrReturn(pvPages, VERR_INVALID_POINTER);
1302 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1303
1304 /*
1305 * Deal with driverless mode first.
1306 */
1307 if (g_supLibData.fDriverless)
1308 {
1309 SUPR3PageFree(pvPages, cPages);
1310 return VINF_SUCCESS;
1311 }
1312
1313 /*
1314 * Try normal free first, then if it fails check if we're using the fallback
1315 * for the allocations without kernel mappings and attempt unlocking it.
1316 */
1317 NOREF(cPages);
1318 SUPPAGEFREE Req;
1319 Req.Hdr.u32Cookie = g_u32Cookie;
1320 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1321 Req.Hdr.cbIn = SUP_IOCTL_PAGE_FREE_SIZE_IN;
1322 Req.Hdr.cbOut = SUP_IOCTL_PAGE_FREE_SIZE_OUT;
1323 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1324 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1325 Req.u.In.pvR3 = pvPages;
1326 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_FREE, &Req, SUP_IOCTL_PAGE_FREE_SIZE);
1327 if (RT_SUCCESS(rc))
1328 {
1329 rc = Req.Hdr.rc;
1330 if ( rc == VERR_INVALID_PARAMETER
1331 && !g_fSupportsPageAllocNoKernel)
1332 {
1333 int rc2 = supR3PageUnlock(pvPages);
1334 if (RT_SUCCESS(rc2))
1335 rc = suplibOsPageFree(&g_supLibData, pvPages, cPages);
1336 }
1337 }
1338 return rc;
1339}
1340
1341
1342SUPR3DECL(void *) SUPR3ContAlloc(size_t cPages, PRTR0PTR pR0Ptr, PRTHCPHYS pHCPhys)
1343{
1344 /*
1345 * Validate.
1346 */
1347 AssertPtrReturn(pHCPhys, NULL);
1348 *pHCPhys = NIL_RTHCPHYS;
1349 AssertPtrNullReturn(pR0Ptr, NULL);
1350 if (pR0Ptr)
1351 *pR0Ptr = NIL_RTR0PTR;
1352 AssertPtrNullReturn(pHCPhys, NULL);
1353 AssertMsgReturn(cPages > 0 && cPages < 256, ("cPages=%d must be > 0 and < 256\n", cPages), NULL);
1354
1355 /*
1356 * Deal with driverless mode first.
1357 */
1358 if (g_supLibData.fDriverless)
1359 {
1360 void *pvPages = NULL;
1361 int rc = SUPR3PageAlloc(cPages, 0 /*fFlags*/, &pvPages);
1362 if (pR0Ptr)
1363 *pR0Ptr = NIL_RTR0PTR;
1364 if (pHCPhys)
1365 *pHCPhys = NIL_RTHCPHYS;
1366 return RT_SUCCESS(rc) ? pvPages : NULL;
1367 }
1368
1369 /*
1370 * Issue IOCtl to the SUPDRV kernel module.
1371 */
1372 SUPCONTALLOC Req;
1373 Req.Hdr.u32Cookie = g_u32Cookie;
1374 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1375 Req.Hdr.cbIn = SUP_IOCTL_CONT_ALLOC_SIZE_IN;
1376 Req.Hdr.cbOut = SUP_IOCTL_CONT_ALLOC_SIZE_OUT;
1377 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1378 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1379 Req.u.In.cPages = (uint32_t)cPages;
1380 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CONT_ALLOC, &Req, SUP_IOCTL_CONT_ALLOC_SIZE);
1381 if ( RT_SUCCESS(rc)
1382 && RT_SUCCESS(Req.Hdr.rc))
1383 {
1384 *pHCPhys = Req.u.Out.HCPhys;
1385 if (pR0Ptr)
1386 *pR0Ptr = Req.u.Out.pvR0;
1387#ifdef RT_OS_DARWIN /* HACK ALERT! */
1388 supR3TouchPages(Req.u.Out.pvR3, cPages);
1389#endif
1390 return Req.u.Out.pvR3;
1391 }
1392
1393 return NULL;
1394}
1395
1396
1397SUPR3DECL(int) SUPR3ContFree(void *pv, size_t cPages)
1398{
1399 /*
1400 * Validate.
1401 */
1402 if (!pv)
1403 return VINF_SUCCESS;
1404 AssertPtrReturn(pv, VERR_INVALID_POINTER);
1405 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1406
1407 /*
1408 * Deal with driverless mode first.
1409 */
1410 if (g_supLibData.fDriverless)
1411 return SUPR3PageFree(pv, cPages);
1412
1413 /*
1414 * Issue IOCtl to the SUPDRV kernel module.
1415 */
1416 SUPCONTFREE Req;
1417 Req.Hdr.u32Cookie = g_u32Cookie;
1418 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1419 Req.Hdr.cbIn = SUP_IOCTL_CONT_FREE_SIZE_IN;
1420 Req.Hdr.cbOut = SUP_IOCTL_CONT_FREE_SIZE_OUT;
1421 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1422 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1423 Req.u.In.pvR3 = pv;
1424 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CONT_FREE, &Req, SUP_IOCTL_CONT_FREE_SIZE);
1425 if (RT_SUCCESS(rc))
1426 rc = Req.Hdr.rc;
1427 return rc;
1428}
1429
1430
1431SUPR3DECL(int) SUPR3LowAlloc(size_t cPages, void **ppvPages, PRTR0PTR ppvPagesR0, PSUPPAGE paPages)
1432{
1433 /*
1434 * Validate.
1435 */
1436 AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
1437 *ppvPages = NULL;
1438 AssertPtrReturn(paPages, VERR_INVALID_POINTER);
1439 AssertMsgReturn(cPages > 0 && cPages < 256, ("cPages=%d must be > 0 and < 256\n", cPages), VERR_PAGE_COUNT_OUT_OF_RANGE);
1440
1441 /* fake */
1442 if (RT_UNLIKELY(g_uSupFakeMode))
1443 {
1444 *ppvPages = RTMemPageAllocZ((size_t)cPages * PAGE_SIZE);
1445 if (!*ppvPages)
1446 return VERR_NO_LOW_MEMORY;
1447
1448 /* fake physical addresses. */
1449 RTHCPHYS Phys = (uintptr_t)*ppvPages + PAGE_SIZE * 1024;
1450 size_t iPage = cPages;
1451 while (iPage-- > 0)
1452 paPages[iPage].Phys = Phys + (iPage << PAGE_SHIFT);
1453 return VINF_SUCCESS;
1454 }
1455
1456 /*
1457 * Issue IOCtl to the SUPDRV kernel module.
1458 */
1459 int rc;
1460 PSUPLOWALLOC pReq = (PSUPLOWALLOC)RTMemTmpAllocZ(SUP_IOCTL_LOW_ALLOC_SIZE(cPages));
1461 if (pReq)
1462 {
1463 pReq->Hdr.u32Cookie = g_u32Cookie;
1464 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
1465 pReq->Hdr.cbIn = SUP_IOCTL_LOW_ALLOC_SIZE_IN;
1466 pReq->Hdr.cbOut = SUP_IOCTL_LOW_ALLOC_SIZE_OUT(cPages);
1467 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
1468 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
1469 pReq->u.In.cPages = (uint32_t)cPages; AssertRelease(pReq->u.In.cPages == cPages);
1470 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LOW_ALLOC, pReq, SUP_IOCTL_LOW_ALLOC_SIZE(cPages));
1471 if (RT_SUCCESS(rc))
1472 rc = pReq->Hdr.rc;
1473 if (RT_SUCCESS(rc))
1474 {
1475 *ppvPages = pReq->u.Out.pvR3;
1476 if (ppvPagesR0)
1477 *ppvPagesR0 = pReq->u.Out.pvR0;
1478 if (paPages)
1479 for (size_t iPage = 0; iPage < cPages; iPage++)
1480 {
1481 paPages[iPage].uReserved = 0;
1482 paPages[iPage].Phys = pReq->u.Out.aPages[iPage];
1483 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
1484 Assert(paPages[iPage].Phys <= UINT32_C(0xfffff000));
1485 }
1486#ifdef RT_OS_DARWIN /* HACK ALERT! */
1487 supR3TouchPages(pReq->u.Out.pvR3, cPages);
1488#endif
1489 }
1490 RTMemTmpFree(pReq);
1491 }
1492 else
1493 rc = VERR_NO_TMP_MEMORY;
1494
1495 return rc;
1496}
1497
1498
1499SUPR3DECL(int) SUPR3LowFree(void *pv, size_t cPages)
1500{
1501 /*
1502 * Validate.
1503 */
1504 if (!pv)
1505 return VINF_SUCCESS;
1506 AssertPtrReturn(pv, VERR_INVALID_POINTER);
1507 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1508
1509 /* fake */
1510 if (RT_UNLIKELY(g_uSupFakeMode))
1511 {
1512 RTMemPageFree(pv, cPages * PAGE_SIZE);
1513 return VINF_SUCCESS;
1514 }
1515
1516 /*
1517 * Issue IOCtl to the SUPDRV kernel module.
1518 */
1519 SUPCONTFREE Req;
1520 Req.Hdr.u32Cookie = g_u32Cookie;
1521 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1522 Req.Hdr.cbIn = SUP_IOCTL_LOW_FREE_SIZE_IN;
1523 Req.Hdr.cbOut = SUP_IOCTL_LOW_FREE_SIZE_OUT;
1524 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1525 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1526 Req.u.In.pvR3 = pv;
1527 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LOW_FREE, &Req, SUP_IOCTL_LOW_FREE_SIZE);
1528 if (RT_SUCCESS(rc))
1529 rc = Req.Hdr.rc;
1530 return rc;
1531}
1532
1533
1534SUPR3DECL(int) SUPR3HardenedVerifyInit(void)
1535{
1536#ifdef RT_OS_WINDOWS
1537 if (g_cInits == 0)
1538 return suplibOsHardenedVerifyInit();
1539#endif
1540 return VINF_SUCCESS;
1541}
1542
1543
1544SUPR3DECL(int) SUPR3HardenedVerifyTerm(void)
1545{
1546#ifdef RT_OS_WINDOWS
1547 if (g_cInits == 0)
1548 return suplibOsHardenedVerifyTerm();
1549#endif
1550 return VINF_SUCCESS;
1551}
1552
1553
1554SUPR3DECL(int) SUPR3HardenedVerifyFile(const char *pszFilename, const char *pszMsg, PRTFILE phFile)
1555{
1556 /*
1557 * Quick input validation.
1558 */
1559 AssertPtr(pszFilename);
1560 AssertPtr(pszMsg);
1561 AssertReturn(!phFile, VERR_NOT_IMPLEMENTED); /** @todo Implement this. The deal is that we make sure the
1562 file is the same we verified after opening it. */
1563 RT_NOREF2(pszFilename, pszMsg);
1564
1565 /*
1566 * Only do the actual check in hardened builds.
1567 */
1568#ifdef VBOX_WITH_HARDENING
1569 int rc = supR3HardenedVerifyFixedFile(pszFilename, false /* fFatal */);
1570 if (RT_FAILURE(rc))
1571 LogRel(("SUPR3HardenedVerifyFile: %s: Verification of \"%s\" failed, rc=%Rrc\n", pszMsg, pszFilename, rc));
1572 return rc;
1573#else
1574 return VINF_SUCCESS;
1575#endif
1576}
1577
1578
1579SUPR3DECL(int) SUPR3HardenedVerifySelf(const char *pszArgv0, bool fInternal, PRTERRINFO pErrInfo)
1580{
1581 /*
1582 * Quick input validation.
1583 */
1584 AssertPtr(pszArgv0);
1585 RTErrInfoClear(pErrInfo);
1586
1587 /*
1588 * Get the executable image path as we need it for all the tests here.
1589 */
1590 char szExecPath[RTPATH_MAX];
1591 if (!RTProcGetExecutablePath(szExecPath, sizeof(szExecPath)))
1592 return RTErrInfoSet(pErrInfo, VERR_INTERNAL_ERROR_2, "RTProcGetExecutablePath failed");
1593
1594 int rc;
1595 if (fInternal)
1596 {
1597 /*
1598 * Internal applications must be launched directly without any PATH
1599 * searching involved.
1600 */
1601 if (RTPathCompare(pszArgv0, szExecPath) != 0)
1602 return RTErrInfoSetF(pErrInfo, VERR_SUPLIB_INVALID_ARGV0_INTERNAL,
1603 "argv[0] does not match the executable image path: '%s' != '%s'", pszArgv0, szExecPath);
1604
1605 /*
1606 * Internal applications must reside in or under the
1607 * RTPathAppPrivateArch directory.
1608 */
1609 char szAppPrivateArch[RTPATH_MAX];
1610 rc = RTPathAppPrivateArch(szAppPrivateArch, sizeof(szAppPrivateArch));
1611 if (RT_FAILURE(rc))
1612 return RTErrInfoSetF(pErrInfo, VERR_SUPLIB_INVALID_ARGV0_INTERNAL,
1613 "RTPathAppPrivateArch failed with rc=%Rrc", rc);
1614 size_t cchAppPrivateArch = strlen(szAppPrivateArch);
1615 if ( cchAppPrivateArch >= strlen(szExecPath)
1616 || !RTPATH_IS_SLASH(szExecPath[cchAppPrivateArch]))
1617 return RTErrInfoSet(pErrInfo, VERR_SUPLIB_INVALID_INTERNAL_APP_DIR,
1618 "Internal executable does reside under RTPathAppPrivateArch");
1619 szExecPath[cchAppPrivateArch] = '\0';
1620 if (RTPathCompare(szExecPath, szAppPrivateArch) != 0)
1621 return RTErrInfoSet(pErrInfo, VERR_SUPLIB_INVALID_INTERNAL_APP_DIR,
1622 "Internal executable does reside under RTPathAppPrivateArch");
1623 szExecPath[cchAppPrivateArch] = RTPATH_SLASH;
1624 }
1625
1626#ifdef VBOX_WITH_HARDENING
1627 /*
1628 * Verify that the image file and parent directories are sane.
1629 */
1630 rc = supR3HardenedVerifyFile(szExecPath, RTHCUINTPTR_MAX, false /*fMaybe3rdParty*/, pErrInfo);
1631 if (RT_FAILURE(rc))
1632 return rc;
1633#endif
1634
1635 return VINF_SUCCESS;
1636}
1637
1638
1639SUPR3DECL(int) SUPR3HardenedVerifyDir(const char *pszDirPath, bool fRecursive, bool fCheckFiles, PRTERRINFO pErrInfo)
1640{
1641 /*
1642 * Quick input validation
1643 */
1644 AssertPtr(pszDirPath);
1645 RTErrInfoClear(pErrInfo);
1646
1647 /*
1648 * Only do the actual check in hardened builds.
1649 */
1650#ifdef VBOX_WITH_HARDENING
1651 int rc = supR3HardenedVerifyDir(pszDirPath, fRecursive, fCheckFiles, pErrInfo);
1652 if (RT_FAILURE(rc) && !RTErrInfoIsSet(pErrInfo))
1653 LogRel(("supR3HardenedVerifyDir: Verification of \"%s\" failed, rc=%Rrc\n", pszDirPath, rc));
1654 return rc;
1655#else
1656 NOREF(pszDirPath); NOREF(fRecursive); NOREF(fCheckFiles);
1657 return VINF_SUCCESS;
1658#endif
1659}
1660
1661
1662SUPR3DECL(int) SUPR3HardenedVerifyPlugIn(const char *pszFilename, PRTERRINFO pErrInfo)
1663{
1664 /*
1665 * Quick input validation
1666 */
1667 AssertPtr(pszFilename);
1668 RTErrInfoClear(pErrInfo);
1669
1670 /*
1671 * Only do the actual check in hardened builds.
1672 */
1673#ifdef VBOX_WITH_HARDENING
1674 int rc = supR3HardenedVerifyFile(pszFilename, RTHCUINTPTR_MAX, true /*fMaybe3rdParty*/, pErrInfo);
1675 if (RT_FAILURE(rc) && !RTErrInfoIsSet(pErrInfo))
1676 LogRel(("supR3HardenedVerifyFile: Verification of \"%s\" failed, rc=%Rrc\n", pszFilename, rc));
1677 return rc;
1678#else
1679 RT_NOREF1(pszFilename);
1680 return VINF_SUCCESS;
1681#endif
1682}
1683
1684
1685SUPR3DECL(int) SUPR3GipGetPhys(PRTHCPHYS pHCPhys)
1686{
1687 if (g_pSUPGlobalInfoPage)
1688 {
1689 *pHCPhys = g_HCPhysSUPGlobalInfoPage;
1690 return VINF_SUCCESS;
1691 }
1692 *pHCPhys = NIL_RTHCPHYS;
1693 return VERR_WRONG_ORDER;
1694}
1695
1696
1697SUPR3DECL(int) SUPR3QueryVTxSupported(const char **ppszWhy)
1698{
1699 *ppszWhy = NULL;
1700#ifdef RT_OS_LINUX
1701 return suplibOsQueryVTxSupported(ppszWhy);
1702#else
1703 return VINF_SUCCESS;
1704#endif
1705}
1706
1707
1708SUPR3DECL(int) SUPR3QueryVTCaps(uint32_t *pfCaps)
1709{
1710 AssertPtrReturn(pfCaps, VERR_INVALID_POINTER);
1711
1712 *pfCaps = 0;
1713
1714 int rc;
1715 if (!g_supLibData.fDriverless)
1716 {
1717 /*
1718 * Issue IOCtl to the SUPDRV kernel module.
1719 */
1720 SUPVTCAPS Req;
1721 Req.Hdr.u32Cookie = g_u32Cookie;
1722 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1723 Req.Hdr.cbIn = SUP_IOCTL_VT_CAPS_SIZE_IN;
1724 Req.Hdr.cbOut = SUP_IOCTL_VT_CAPS_SIZE_OUT;
1725 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1726 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1727 Req.u.Out.fCaps = 0;
1728 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_VT_CAPS, &Req, SUP_IOCTL_VT_CAPS_SIZE);
1729 if (RT_SUCCESS(rc))
1730 {
1731 rc = Req.Hdr.rc;
1732 if (RT_SUCCESS(rc))
1733 *pfCaps = Req.u.Out.fCaps;
1734 }
1735 }
1736 /*
1737 * Fail this call in driverless mode.
1738 */
1739 else
1740 rc = VERR_SUP_DRIVERLESS;
1741 return rc;
1742}
1743
1744
1745SUPR3DECL(bool) SUPR3IsNemSupportedWhenNoVtxOrAmdV(void)
1746{
1747#ifdef RT_OS_WINDOWS
1748 return suplibOsIsNemSupportedWhenNoVtxOrAmdV();
1749#else
1750 return false;
1751#endif
1752}
1753
1754
1755SUPR3DECL(int) SUPR3QueryMicrocodeRev(uint32_t *uMicrocodeRev)
1756{
1757 AssertPtrReturn(uMicrocodeRev, VERR_INVALID_POINTER);
1758
1759 *uMicrocodeRev = 0;
1760
1761 int rc;
1762 if (!g_supLibData.fDriverless)
1763 {
1764 /*
1765 * Issue IOCtl to the SUPDRV kernel module.
1766 */
1767 SUPUCODEREV Req;
1768 Req.Hdr.u32Cookie = g_u32Cookie;
1769 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1770 Req.Hdr.cbIn = SUP_IOCTL_UCODE_REV_SIZE_IN;
1771 Req.Hdr.cbOut = SUP_IOCTL_UCODE_REV_SIZE_OUT;
1772 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1773 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1774 Req.u.Out.MicrocodeRev = 0;
1775 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_UCODE_REV, &Req, SUP_IOCTL_UCODE_REV_SIZE);
1776 if (RT_SUCCESS(rc))
1777 {
1778 rc = Req.Hdr.rc;
1779 if (RT_SUCCESS(rc))
1780 *uMicrocodeRev = Req.u.Out.MicrocodeRev;
1781 }
1782 }
1783 /*
1784 * Just fail the call in driverless mode.
1785 */
1786 else
1787 rc = VERR_SUP_DRIVERLESS;
1788 return rc;
1789}
1790
1791
1792SUPR3DECL(int) SUPR3TracerOpen(uint32_t uCookie, uintptr_t uArg)
1793{
1794 /* fake */
1795 if (RT_UNLIKELY(g_uSupFakeMode))
1796 return VINF_SUCCESS;
1797
1798 /*
1799 * Issue IOCtl to the SUPDRV kernel module.
1800 */
1801 SUPTRACEROPEN Req;
1802 Req.Hdr.u32Cookie = g_u32Cookie;
1803 Req.Hdr.u32SessionCookie= g_u32SessionCookie;
1804 Req.Hdr.cbIn = SUP_IOCTL_TRACER_OPEN_SIZE_IN;
1805 Req.Hdr.cbOut = SUP_IOCTL_TRACER_OPEN_SIZE_OUT;
1806 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1807 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1808 Req.u.In.uCookie = uCookie;
1809 Req.u.In.uArg = uArg;
1810 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_OPEN, &Req, SUP_IOCTL_TRACER_OPEN_SIZE);
1811 if (RT_SUCCESS(rc))
1812 rc = Req.Hdr.rc;
1813 return rc;
1814}
1815
1816
1817SUPR3DECL(int) SUPR3TracerClose(void)
1818{
1819 /* fake */
1820 if (RT_UNLIKELY(g_uSupFakeMode))
1821 return VINF_SUCCESS;
1822
1823 /*
1824 * Issue IOCtl to the SUPDRV kernel module.
1825 */
1826 SUPREQHDR Req;
1827 Req.u32Cookie = g_u32Cookie;
1828 Req.u32SessionCookie= g_u32SessionCookie;
1829 Req.cbIn = SUP_IOCTL_TRACER_OPEN_SIZE_IN;
1830 Req.cbOut = SUP_IOCTL_TRACER_OPEN_SIZE_OUT;
1831 Req.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1832 Req.rc = VERR_INTERNAL_ERROR;
1833 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_CLOSE, &Req, SUP_IOCTL_TRACER_CLOSE_SIZE);
1834 if (RT_SUCCESS(rc))
1835 rc = Req.rc;
1836 return rc;
1837}
1838
1839
1840SUPR3DECL(int) SUPR3TracerIoCtl(uintptr_t uCmd, uintptr_t uArg, int32_t *piRetVal)
1841{
1842 /* fake */
1843 if (RT_UNLIKELY(g_uSupFakeMode))
1844 {
1845 *piRetVal = -1;
1846 return VERR_NOT_SUPPORTED;
1847 }
1848
1849 /*
1850 * Issue IOCtl to the SUPDRV kernel module.
1851 */
1852 SUPTRACERIOCTL Req;
1853 Req.Hdr.u32Cookie = g_u32Cookie;
1854 Req.Hdr.u32SessionCookie= g_u32SessionCookie;
1855 Req.Hdr.cbIn = SUP_IOCTL_TRACER_IOCTL_SIZE_IN;
1856 Req.Hdr.cbOut = SUP_IOCTL_TRACER_IOCTL_SIZE_OUT;
1857 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1858 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1859 Req.u.In.uCmd = uCmd;
1860 Req.u.In.uArg = uArg;
1861 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_IOCTL, &Req, SUP_IOCTL_TRACER_IOCTL_SIZE);
1862 if (RT_SUCCESS(rc))
1863 {
1864 rc = Req.Hdr.rc;
1865 *piRetVal = Req.u.Out.iRetVal;
1866 }
1867 return rc;
1868}
1869
1870
1871
1872typedef struct SUPDRVTRACERSTRTAB
1873{
1874 /** Pointer to the string table. */
1875 char *pchStrTab;
1876 /** The actual string table size. */
1877 uint32_t cbStrTab;
1878 /** The original string pointers. */
1879 RTUINTPTR apszOrgFunctions[1];
1880} SUPDRVTRACERSTRTAB, *PSUPDRVTRACERSTRTAB;
1881
1882
1883/**
1884 * Destroys a string table, restoring the original pszFunction member valus.
1885 *
1886 * @param pThis The string table structure.
1887 * @param paProbeLocs32 The probe location array, 32-bit type variant.
1888 * @param paProbeLocs64 The probe location array, 64-bit type variant.
1889 * @param cProbeLocs The number of elements in the array.
1890 * @param f32Bit Set if @a paProbeLocs32 should be used, when
1891 * clear use @a paProbeLocs64.
1892 */
1893static void supr3TracerDestroyStrTab(PSUPDRVTRACERSTRTAB pThis, PVTGPROBELOC32 paProbeLocs32, PVTGPROBELOC64 paProbeLocs64,
1894 uint32_t cProbeLocs, bool f32Bit)
1895{
1896 /* Restore. */
1897 size_t i = cProbeLocs;
1898 if (f32Bit)
1899 while (i--)
1900 paProbeLocs32[i].pszFunction = (uint32_t)pThis->apszOrgFunctions[i];
1901 else
1902 while (i--)
1903 paProbeLocs64[i].pszFunction = pThis->apszOrgFunctions[i];
1904
1905 /* Free. */
1906 RTMemFree(pThis->pchStrTab);
1907 RTMemFree(pThis);
1908}
1909
1910
1911/**
1912 * Creates a string table for the pszFunction members in the probe location
1913 * array.
1914 *
1915 * This will save and replace the pszFunction members with offsets.
1916 *
1917 * @returns Pointer to a string table structure. NULL on failure.
1918 * @param paProbeLocs32 The probe location array, 32-bit type variant.
1919 * @param paProbeLocs64 The probe location array, 64-bit type variant.
1920 * @param cProbeLocs The number of elements in the array.
1921 * @param offDelta Relocation offset for the string pointers.
1922 * @param f32Bit Set if @a paProbeLocs32 should be used, when
1923 * clear use @a paProbeLocs64.
1924 */
1925static PSUPDRVTRACERSTRTAB supr3TracerCreateStrTab(PVTGPROBELOC32 paProbeLocs32,
1926 PVTGPROBELOC64 paProbeLocs64,
1927 uint32_t cProbeLocs,
1928 RTUINTPTR offDelta,
1929 bool f32Bit)
1930{
1931 if (cProbeLocs > _128K)
1932 return NULL;
1933
1934 /*
1935 * Allocate the string table structures.
1936 */
1937 size_t cbThis = RT_UOFFSETOF_DYN(SUPDRVTRACERSTRTAB, apszOrgFunctions[cProbeLocs]);
1938 PSUPDRVTRACERSTRTAB pThis = (PSUPDRVTRACERSTRTAB)RTMemAlloc(cbThis);
1939 if (!pThis)
1940 return NULL;
1941
1942 uint32_t const cHashBits = cProbeLocs * 2 - 1;
1943 uint32_t *pbmHash = (uint32_t *)RTMemAllocZ(RT_ALIGN_32(cHashBits, 64) / 8 );
1944 if (!pbmHash)
1945 {
1946 RTMemFree(pThis);
1947 return NULL;
1948 }
1949
1950 /*
1951 * Calc the max string table size and save the orignal pointers so we can
1952 * replace them later.
1953 */
1954 size_t cbMax = 1;
1955 for (uint32_t i = 0; i < cProbeLocs; i++)
1956 {
1957 pThis->apszOrgFunctions[i] = f32Bit ? paProbeLocs32[i].pszFunction : paProbeLocs64[i].pszFunction;
1958 const char *pszFunction = (const char *)(uintptr_t)(pThis->apszOrgFunctions[i] + offDelta);
1959 size_t cch = strlen(pszFunction);
1960 if (cch > _1K)
1961 {
1962 cbMax = 0;
1963 break;
1964 }
1965 cbMax += cch + 1;
1966 }
1967
1968 /* Alloc space for it. */
1969 if (cbMax > 0)
1970 pThis->pchStrTab = (char *)RTMemAlloc(cbMax);
1971 else
1972 pThis->pchStrTab = NULL;
1973 if (!pThis->pchStrTab)
1974 {
1975 RTMemFree(pbmHash);
1976 RTMemFree(pThis);
1977 return NULL;
1978 }
1979
1980 /*
1981 * Create the string table.
1982 */
1983 uint32_t off = 0;
1984 uint32_t offPrev = 0;
1985
1986 for (uint32_t i = 0; i < cProbeLocs; i++)
1987 {
1988 const char * const psz = (const char *)(uintptr_t)(pThis->apszOrgFunctions[i] + offDelta);
1989 size_t const cch = strlen(psz);
1990 uint32_t const iHashBit = RTStrHash1(psz) % cHashBits;
1991 if (ASMBitTestAndSet(pbmHash, iHashBit))
1992 {
1993 /* Often it's the most recent string. */
1994 if ( off - offPrev < cch + 1
1995 || memcmp(&pThis->pchStrTab[offPrev], psz, cch + 1))
1996 {
1997 /* It wasn't, search the entire string table. (lazy bird) */
1998 offPrev = 0;
1999 while (offPrev < off)
2000 {
2001 size_t cchCur = strlen(&pThis->pchStrTab[offPrev]);
2002 if ( cchCur == cch
2003 && !memcmp(&pThis->pchStrTab[offPrev], psz, cch + 1))
2004 break;
2005 offPrev += (uint32_t)cchCur + 1;
2006 }
2007 }
2008 }
2009 else
2010 offPrev = off;
2011
2012 /* Add the string to the table. */
2013 if (offPrev >= off)
2014 {
2015 memcpy(&pThis->pchStrTab[off], psz, cch + 1);
2016 offPrev = off;
2017 off += (uint32_t)cch + 1;
2018 }
2019
2020 /* Update the entry */
2021 if (f32Bit)
2022 paProbeLocs32[i].pszFunction = offPrev;
2023 else
2024 paProbeLocs64[i].pszFunction = offPrev;
2025 }
2026
2027 pThis->cbStrTab = off;
2028 RTMemFree(pbmHash);
2029 return pThis;
2030}
2031
2032
2033
2034SUPR3DECL(int) SUPR3TracerRegisterModule(uintptr_t hModNative, const char *pszModule, struct VTGOBJHDR *pVtgHdr,
2035 RTUINTPTR uVtgHdrAddr, uint32_t fFlags)
2036{
2037 /* Validate input. */
2038 NOREF(hModNative);
2039 AssertPtrReturn(pVtgHdr, VERR_INVALID_POINTER);
2040 AssertReturn(!memcmp(pVtgHdr->szMagic, VTGOBJHDR_MAGIC, sizeof(pVtgHdr->szMagic)), VERR_SUPDRV_VTG_MAGIC);
2041 AssertPtrReturn(pszModule, VERR_INVALID_POINTER);
2042 size_t cchModule = strlen(pszModule);
2043 AssertReturn(cchModule < RT_SIZEOFMEMB(SUPTRACERUMODREG, u.In.szName), VERR_FILENAME_TOO_LONG);
2044 AssertReturn(!RTPathHavePath(pszModule), VERR_INVALID_PARAMETER);
2045 AssertReturn(fFlags == SUP_TRACER_UMOD_FLAGS_EXE || fFlags == SUP_TRACER_UMOD_FLAGS_SHARED, VERR_INVALID_PARAMETER);
2046
2047 /*
2048 * Set the probe location array offset and size members. If the size is
2049 * zero, don't bother ring-0 with it.
2050 */
2051 if (!pVtgHdr->offProbeLocs)
2052 {
2053 uint64_t u64Tmp = pVtgHdr->uProbeLocsEnd.u64 - pVtgHdr->uProbeLocs.u64;
2054 if (u64Tmp >= UINT32_MAX)
2055 return VERR_SUPDRV_VTG_BAD_HDR_TOO_MUCH;
2056 pVtgHdr->cbProbeLocs = (uint32_t)u64Tmp;
2057
2058 u64Tmp = pVtgHdr->uProbeLocs.u64 - uVtgHdrAddr;
2059 if ((int64_t)u64Tmp != (int32_t)u64Tmp)
2060 {
2061 LogRel(("SUPR3TracerRegisterModule: VERR_SUPDRV_VTG_BAD_HDR_PTR - u64Tmp=%#llx uProbeLocs=%#llx uVtgHdrAddr=%RTptr\n",
2062 u64Tmp, pVtgHdr->uProbeLocs.u64, uVtgHdrAddr));
2063 return VERR_SUPDRV_VTG_BAD_HDR_PTR;
2064 }
2065 pVtgHdr->offProbeLocs = (int32_t)u64Tmp;
2066 }
2067
2068 if ( !pVtgHdr->cbProbeLocs
2069 || !pVtgHdr->cbProbes)
2070 return VINF_SUCCESS;
2071
2072 /*
2073 * Fake out.
2074 */
2075 if (RT_UNLIKELY(g_uSupFakeMode))
2076 return VINF_SUCCESS;
2077
2078 /*
2079 * Create a string table for the function names in the location array.
2080 * It's somewhat easier to do that here than from ring-0.
2081 */
2082 uint32_t const cProbeLocs = pVtgHdr->cbProbeLocs
2083 / (pVtgHdr->cBits == 32 ? sizeof(VTGPROBELOC32) : sizeof(VTGPROBELOC64));
2084 PVTGPROBELOC paProbeLocs = (PVTGPROBELOC)((uintptr_t)pVtgHdr + pVtgHdr->offProbeLocs);
2085 PSUPDRVTRACERSTRTAB pStrTab = supr3TracerCreateStrTab((PVTGPROBELOC32)paProbeLocs,
2086 (PVTGPROBELOC64)paProbeLocs,
2087 cProbeLocs, (uintptr_t)pVtgHdr - uVtgHdrAddr,
2088 pVtgHdr->cBits == 32);
2089 if (!pStrTab)
2090 return VERR_NO_MEMORY;
2091
2092
2093 /*
2094 * Issue IOCtl to the SUPDRV kernel module.
2095 */
2096 SUPTRACERUMODREG Req;
2097 Req.Hdr.u32Cookie = g_u32Cookie;
2098 Req.Hdr.u32SessionCookie= g_u32SessionCookie;
2099 Req.Hdr.cbIn = SUP_IOCTL_TRACER_UMOD_REG_SIZE_IN;
2100 Req.Hdr.cbOut = SUP_IOCTL_TRACER_UMOD_REG_SIZE_OUT;
2101 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2102 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2103 Req.u.In.uVtgHdrAddr = uVtgHdrAddr;
2104 Req.u.In.R3PtrVtgHdr = pVtgHdr;
2105 Req.u.In.R3PtrStrTab = pStrTab->pchStrTab;
2106 Req.u.In.cbStrTab = pStrTab->cbStrTab;
2107 Req.u.In.fFlags = fFlags;
2108
2109 memcpy(Req.u.In.szName, pszModule, cchModule + 1);
2110 if (!RTPathHasSuffix(Req.u.In.szName))
2111 {
2112 /* Add the default suffix if none is given. */
2113 switch (fFlags & SUP_TRACER_UMOD_FLAGS_TYPE_MASK)
2114 {
2115#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
2116 case SUP_TRACER_UMOD_FLAGS_EXE:
2117 if (cchModule + sizeof(".exe") <= sizeof(Req.u.In.szName))
2118 strcpy(&Req.u.In.szName[cchModule], ".exe");
2119 break;
2120#endif
2121
2122 case SUP_TRACER_UMOD_FLAGS_SHARED:
2123 {
2124 const char *pszSuff = RTLdrGetSuff();
2125 size_t cchSuff = strlen(pszSuff);
2126 if (cchModule + cchSuff < sizeof(Req.u.In.szName))
2127 memcpy(&Req.u.In.szName[cchModule], pszSuff, cchSuff + 1);
2128 break;
2129 }
2130 }
2131 }
2132
2133 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_UMOD_REG, &Req, SUP_IOCTL_TRACER_UMOD_REG_SIZE);
2134 if (RT_SUCCESS(rc))
2135 rc = Req.Hdr.rc;
2136
2137 supr3TracerDestroyStrTab(pStrTab, (PVTGPROBELOC32)paProbeLocs, (PVTGPROBELOC64)paProbeLocs,
2138 cProbeLocs, pVtgHdr->cBits == 32);
2139 return rc;
2140}
2141
2142
2143SUPR3DECL(int) SUPR3TracerDeregisterModule(struct VTGOBJHDR *pVtgHdr)
2144{
2145 /* Validate input. */
2146 AssertPtrReturn(pVtgHdr, VERR_INVALID_POINTER);
2147 AssertReturn(!memcmp(pVtgHdr->szMagic, VTGOBJHDR_MAGIC, sizeof(pVtgHdr->szMagic)), VERR_SUPDRV_VTG_MAGIC);
2148
2149 /*
2150 * Don't bother if the object is empty.
2151 */
2152 if ( !pVtgHdr->cbProbeLocs
2153 || !pVtgHdr->cbProbes)
2154 return VINF_SUCCESS;
2155
2156 /*
2157 * Fake out.
2158 */
2159 if (RT_UNLIKELY(g_uSupFakeMode))
2160 return VINF_SUCCESS;
2161
2162 /*
2163 * Issue IOCtl to the SUPDRV kernel module.
2164 */
2165 SUPTRACERUMODDEREG Req;
2166 Req.Hdr.u32Cookie = g_u32Cookie;
2167 Req.Hdr.u32SessionCookie= g_u32SessionCookie;
2168 Req.Hdr.cbIn = SUP_IOCTL_TRACER_UMOD_REG_SIZE_IN;
2169 Req.Hdr.cbOut = SUP_IOCTL_TRACER_UMOD_REG_SIZE_OUT;
2170 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2171 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2172 Req.u.In.pVtgHdr = pVtgHdr;
2173
2174 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_UMOD_DEREG, &Req, SUP_IOCTL_TRACER_UMOD_DEREG_SIZE);
2175 if (RT_SUCCESS(rc))
2176 rc = Req.Hdr.rc;
2177 return rc;
2178}
2179
2180
2181DECLASM(void) suplibTracerFireProbe(PVTGPROBELOC pProbeLoc, PSUPTRACERUMODFIREPROBE pReq)
2182{
2183 RT_NOREF1(pProbeLoc);
2184
2185 pReq->Hdr.u32Cookie = g_u32Cookie;
2186 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
2187 Assert(pReq->Hdr.cbIn == SUP_IOCTL_TRACER_UMOD_FIRE_PROBE_SIZE_IN);
2188 Assert(pReq->Hdr.cbOut == SUP_IOCTL_TRACER_UMOD_FIRE_PROBE_SIZE_OUT);
2189 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2190 pReq->Hdr.rc = VINF_SUCCESS;
2191
2192 suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TRACER_UMOD_FIRE_PROBE, pReq, SUP_IOCTL_TRACER_UMOD_FIRE_PROBE_SIZE);
2193}
2194
2195
2196SUPR3DECL(int) SUPR3MsrProberRead(uint32_t uMsr, RTCPUID idCpu, uint64_t *puValue, bool *pfGp)
2197{
2198 SUPMSRPROBER Req;
2199 Req.Hdr.u32Cookie = g_u32Cookie;
2200 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
2201 Req.Hdr.cbIn = SUP_IOCTL_MSR_PROBER_SIZE_IN;
2202 Req.Hdr.cbOut = SUP_IOCTL_MSR_PROBER_SIZE_OUT;
2203 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2204 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2205
2206 Req.u.In.enmOp = SUPMSRPROBEROP_READ;
2207 Req.u.In.uMsr = uMsr;
2208 Req.u.In.idCpu = idCpu == NIL_RTCPUID ? UINT32_MAX : idCpu;
2209
2210 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_MSR_PROBER, &Req, SUP_IOCTL_MSR_PROBER_SIZE);
2211 if (RT_SUCCESS(rc))
2212 rc = Req.Hdr.rc;
2213 if (RT_SUCCESS(rc))
2214 {
2215 if (puValue)
2216 *puValue = Req.u.Out.uResults.Read.uValue;
2217 if (pfGp)
2218 *pfGp = Req.u.Out.uResults.Read.fGp;
2219 }
2220
2221 return rc;
2222}
2223
2224
2225SUPR3DECL(int) SUPR3MsrProberWrite(uint32_t uMsr, RTCPUID idCpu, uint64_t uValue, bool *pfGp)
2226{
2227 SUPMSRPROBER Req;
2228 Req.Hdr.u32Cookie = g_u32Cookie;
2229 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
2230 Req.Hdr.cbIn = SUP_IOCTL_MSR_PROBER_SIZE_IN;
2231 Req.Hdr.cbOut = SUP_IOCTL_MSR_PROBER_SIZE_OUT;
2232 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2233 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2234
2235 Req.u.In.enmOp = SUPMSRPROBEROP_WRITE;
2236 Req.u.In.uMsr = uMsr;
2237 Req.u.In.idCpu = idCpu == NIL_RTCPUID ? UINT32_MAX : idCpu;
2238 Req.u.In.uArgs.Write.uToWrite = uValue;
2239
2240 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_MSR_PROBER, &Req, SUP_IOCTL_MSR_PROBER_SIZE);
2241 if (RT_SUCCESS(rc))
2242 rc = Req.Hdr.rc;
2243 if (RT_SUCCESS(rc) && pfGp)
2244 *pfGp = Req.u.Out.uResults.Write.fGp;
2245
2246 return rc;
2247}
2248
2249
2250SUPR3DECL(int) SUPR3MsrProberModify(uint32_t uMsr, RTCPUID idCpu, uint64_t fAndMask, uint64_t fOrMask,
2251 PSUPMSRPROBERMODIFYRESULT pResult)
2252{
2253 return SUPR3MsrProberModifyEx(uMsr, idCpu, fAndMask, fOrMask, false /*fFaster*/, pResult);
2254}
2255
2256
2257SUPR3DECL(int) SUPR3MsrProberModifyEx(uint32_t uMsr, RTCPUID idCpu, uint64_t fAndMask, uint64_t fOrMask, bool fFaster,
2258 PSUPMSRPROBERMODIFYRESULT pResult)
2259{
2260 SUPMSRPROBER Req;
2261 Req.Hdr.u32Cookie = g_u32Cookie;
2262 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
2263 Req.Hdr.cbIn = SUP_IOCTL_MSR_PROBER_SIZE_IN;
2264 Req.Hdr.cbOut = SUP_IOCTL_MSR_PROBER_SIZE_OUT;
2265 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2266 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2267
2268 Req.u.In.enmOp = fFaster ? SUPMSRPROBEROP_MODIFY_FASTER : SUPMSRPROBEROP_MODIFY;
2269 Req.u.In.uMsr = uMsr;
2270 Req.u.In.idCpu = idCpu == NIL_RTCPUID ? UINT32_MAX : idCpu;
2271 Req.u.In.uArgs.Modify.fAndMask = fAndMask;
2272 Req.u.In.uArgs.Modify.fOrMask = fOrMask;
2273
2274 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_MSR_PROBER, &Req, SUP_IOCTL_MSR_PROBER_SIZE);
2275 if (RT_SUCCESS(rc))
2276 rc = Req.Hdr.rc;
2277 if (RT_SUCCESS(rc))
2278 *pResult = Req.u.Out.uResults.Modify;
2279
2280 return rc;
2281}
2282
2283
2284SUPR3DECL(int) SUPR3ResumeSuspendedKeyboards(void)
2285{
2286#ifdef RT_OS_DARWIN
2287 /*
2288 * Issue IOCtl to the SUPDRV kernel module.
2289 */
2290 SUPREQHDR Req;
2291 Req.u32Cookie = g_u32Cookie;
2292 Req.u32SessionCookie= g_u32SessionCookie;
2293 Req.cbIn = SUP_IOCTL_RESUME_SUSPENDED_KBDS_SIZE_IN;
2294 Req.cbOut = SUP_IOCTL_RESUME_SUSPENDED_KBDS_SIZE_OUT;
2295 Req.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2296 Req.rc = VERR_INTERNAL_ERROR;
2297 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_RESUME_SUSPENDED_KBDS, &Req, SUP_IOCTL_RESUME_SUSPENDED_KBDS_SIZE);
2298 if (RT_SUCCESS(rc))
2299 rc = Req.rc;
2300 return rc;
2301#else /* !RT_OS_DARWIN */
2302 return VERR_NOT_SUPPORTED;
2303#endif
2304}
2305
2306
2307SUPR3DECL(int) SUPR3TscDeltaMeasure(RTCPUID idCpu, bool fAsync, bool fForce, uint8_t cRetries, uint8_t cMsWaitRetry)
2308{
2309 SUPTSCDELTAMEASURE Req;
2310 Req.Hdr.u32Cookie = g_u32Cookie;
2311 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
2312 Req.Hdr.cbIn = SUP_IOCTL_TSC_DELTA_MEASURE_SIZE_IN;
2313 Req.Hdr.cbOut = SUP_IOCTL_TSC_DELTA_MEASURE_SIZE_OUT;
2314 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2315 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2316
2317 Req.u.In.cRetries = cRetries;
2318 Req.u.In.fAsync = fAsync;
2319 Req.u.In.fForce = fForce;
2320 Req.u.In.idCpu = idCpu;
2321 Req.u.In.cMsWaitRetry = cMsWaitRetry;
2322
2323 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TSC_DELTA_MEASURE, &Req, SUP_IOCTL_TSC_DELTA_MEASURE_SIZE);
2324 if (RT_SUCCESS(rc))
2325 rc = Req.Hdr.rc;
2326 return rc;
2327}
2328
2329
2330SUPR3DECL(int) SUPR3ReadTsc(uint64_t *puTsc, uint16_t *pidApic)
2331{
2332 AssertReturn(puTsc, VERR_INVALID_PARAMETER);
2333
2334 SUPTSCREAD Req;
2335 Req.Hdr.u32Cookie = g_u32Cookie;
2336 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
2337 Req.Hdr.cbIn = SUP_IOCTL_TSC_READ_SIZE_IN;
2338 Req.Hdr.cbOut = SUP_IOCTL_TSC_READ_SIZE_OUT;
2339 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2340 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2341
2342 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_TSC_READ, &Req, SUP_IOCTL_TSC_READ_SIZE);
2343 if (RT_SUCCESS(rc))
2344 {
2345 rc = Req.Hdr.rc;
2346 *puTsc = Req.u.Out.u64AdjustedTsc;
2347 if (pidApic)
2348 *pidApic = Req.u.Out.idApic;
2349 }
2350 return rc;
2351}
2352
2353
2354SUPR3DECL(int) SUPR3GipSetFlags(uint32_t fOrMask, uint32_t fAndMask)
2355{
2356 AssertMsgReturn(!(fOrMask & ~SUPGIP_FLAGS_VALID_MASK),
2357 ("fOrMask=%#x ValidMask=%#x\n", fOrMask, SUPGIP_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER);
2358 AssertMsgReturn((fAndMask & ~SUPGIP_FLAGS_VALID_MASK) == ~SUPGIP_FLAGS_VALID_MASK,
2359 ("fAndMask=%#x ValidMask=%#x\n", fAndMask, SUPGIP_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER);
2360
2361 SUPGIPSETFLAGS Req;
2362 Req.Hdr.u32Cookie = g_u32Cookie;
2363 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
2364 Req.Hdr.cbIn = SUP_IOCTL_GIP_SET_FLAGS_SIZE_IN;
2365 Req.Hdr.cbOut = SUP_IOCTL_GIP_SET_FLAGS_SIZE_OUT;
2366 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2367 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2368
2369 Req.u.In.fAndMask = fAndMask;
2370 Req.u.In.fOrMask = fOrMask;
2371
2372 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_GIP_SET_FLAGS, &Req, SUP_IOCTL_GIP_SET_FLAGS_SIZE);
2373 if (RT_SUCCESS(rc))
2374 rc = Req.Hdr.rc;
2375 return rc;
2376}
2377
2378
2379SUPR3DECL(int) SUPR3GetHwvirtMsrs(PSUPHWVIRTMSRS pHwvirtMsrs, bool fForceRequery)
2380{
2381 AssertReturn(pHwvirtMsrs, VERR_INVALID_PARAMETER);
2382
2383 SUPGETHWVIRTMSRS Req;
2384 Req.Hdr.u32Cookie = g_u32Cookie;
2385 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
2386 Req.Hdr.cbIn = SUP_IOCTL_GET_HWVIRT_MSRS_SIZE_IN;
2387 Req.Hdr.cbOut = SUP_IOCTL_GET_HWVIRT_MSRS_SIZE_OUT;
2388 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2389 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2390
2391 Req.u.In.fForce = fForceRequery;
2392 Req.u.In.fReserved0 = false;
2393 Req.u.In.fReserved1 = false;
2394 Req.u.In.fReserved2 = false;
2395
2396 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_GET_HWVIRT_MSRS, &Req, SUP_IOCTL_GET_HWVIRT_MSRS_SIZE);
2397 if (RT_SUCCESS(rc))
2398 {
2399 rc = Req.Hdr.rc;
2400 *pHwvirtMsrs = Req.u.Out.HwvirtMsrs;
2401 }
2402 else
2403 RT_ZERO(*pHwvirtMsrs);
2404 return rc;
2405}
2406
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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