VirtualBox

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

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

SUP: A couple of adjustments for driverless mode. bugref:10138 [fix for r149191]

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