VirtualBox

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

Last change on this file since 93320 was 93182, checked in by vboxsync, 3 years ago

SUP: One more adjustment for driverless mode.?bugref:10138

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

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