VirtualBox

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

Last change on this file since 26430 was 26430, checked in by vboxsync, 15 years ago

Introducing RTR0MemObjAllocPhysEx

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 74.4 KB
Line 
1/* $Id: SUPLib.cpp 26430 2010-02-11 14:23:01Z vboxsync $ */
2/** @file
3 * VirtualBox Support Library - Common code.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/** @page pg_sup SUP - The Support Library
32 *
33 * The support library is responsible for providing facilities to load
34 * VMM Host Ring-0 code, to call Host VMM Ring-0 code from Ring-3 Host
35 * code, to pin down physical memory, and more.
36 *
37 * The VMM Host Ring-0 code can be combined in the support driver if
38 * permitted by kernel module license policies. If it is not combined
39 * it will be externalized in a .r0 module that will be loaded using
40 * the IPRT loader.
41 *
42 * The Ring-0 calling is done thru a generic SUP interface which will
43 * tranfer an argument set and call a predefined entry point in the Host
44 * VMM Ring-0 code.
45 *
46 * See @ref grp_sup "SUP - Support APIs" for API details.
47 */
48
49/*******************************************************************************
50* Header Files *
51*******************************************************************************/
52#define LOG_GROUP LOG_GROUP_SUP
53#include <VBox/sup.h>
54#include <VBox/err.h>
55#include <VBox/param.h>
56#include <VBox/vmm.h>
57#include <VBox/log.h>
58#include <VBox/x86.h>
59
60#include <iprt/assert.h>
61#include <iprt/alloc.h>
62#include <iprt/alloca.h>
63#include <iprt/ldr.h>
64#include <iprt/asm.h>
65#include <iprt/mp.h>
66#include <iprt/cpuset.h>
67#include <iprt/thread.h>
68#include <iprt/process.h>
69#include <iprt/path.h>
70#include <iprt/string.h>
71#include <iprt/env.h>
72#include <iprt/rand.h>
73
74#include "SUPLibInternal.h"
75#include "SUPDrvIOC.h"
76
77
78/*******************************************************************************
79* Defined Constants And Macros *
80*******************************************************************************/
81/** R0 VMM module name. */
82#define VMMR0_NAME "VMMR0"
83
84
85/*******************************************************************************
86* Structures and Typedefs *
87*******************************************************************************/
88typedef DECLCALLBACK(int) FNCALLVMMR0(PVMR0 pVMR0, unsigned uOperation, void *pvArg);
89typedef FNCALLVMMR0 *PFNCALLVMMR0;
90
91
92/*******************************************************************************
93* Global Variables *
94*******************************************************************************/
95/** Init counter. */
96static uint32_t g_cInits = 0;
97/** Whether we've been preinitied. */
98static bool g_fPreInited = false;
99/** The SUPLib instance data.
100 * Well, at least parts of it, specificly the parts that are being handed over
101 * via the pre-init mechanism from the hardened executable stub. */
102SUPLIBDATA g_supLibData =
103{
104 NIL_RTFILE
105#if defined(RT_OS_DARWIN)
106 , NULL
107#elif defined(RT_OS_LINUX)
108 , false
109#endif
110};
111
112/** Pointer to the Global Information Page.
113 *
114 * This pointer is valid as long as SUPLib has a open session. Anyone using
115 * the page must treat this pointer as higly volatile and not trust it beyond
116 * one transaction.
117 *
118 * @todo This will probably deserve it's own session or some other good solution...
119 */
120DECLEXPORT(PSUPGLOBALINFOPAGE) g_pSUPGlobalInfoPage;
121/** Address of the ring-0 mapping of the GIP. */
122static PSUPGLOBALINFOPAGE g_pSUPGlobalInfoPageR0;
123/** The physical address of the GIP. */
124static RTHCPHYS g_HCPhysSUPGlobalInfoPage = NIL_RTHCPHYS;
125
126/** The negotiated cookie. */
127uint32_t g_u32Cookie = 0;
128/** The negotiated session cookie. */
129uint32_t g_u32SessionCookie;
130/** Session handle. */
131PSUPDRVSESSION g_pSession;
132/** R0 SUP Functions used for resolving referenced to the SUPR0 module. */
133static PSUPQUERYFUNCS g_pFunctions;
134
135/** VMMR0 Load Address. */
136static RTR0PTR g_pvVMMR0 = NIL_RTR0PTR;
137/** PAGE_ALLOC_EX sans kernel mapping support indicator. */
138static bool g_fSupportsPageAllocNoKernel = true;
139/** Fake mode indicator. (~0 at first, 0 or 1 after first test) */
140static uint32_t g_u32FakeMode = ~0;
141
142
143/*******************************************************************************
144* Internal Functions *
145*******************************************************************************/
146static int supInitFake(PSUPDRVSESSION *ppSession);
147static int supLoadModule(const char *pszFilename, const char *pszModule, const char *pszSrvReqHandler, void **ppvImageBase);
148static DECLCALLBACK(int) supLoadModuleResolveImport(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser);
149
150
151/** Touch a range of pages. */
152DECLINLINE(void) supR3TouchPages(void *pv, size_t cPages)
153{
154 uint32_t volatile *pu32 = (uint32_t volatile *)pv;
155 while (cPages-- > 0)
156 {
157 ASMAtomicCmpXchgU32(pu32, 0, 0);
158 pu32 += PAGE_SIZE / sizeof(uint32_t);
159 }
160}
161
162
163SUPR3DECL(int) SUPR3Install(void)
164{
165 return suplibOsInstall();
166}
167
168
169SUPR3DECL(int) SUPR3Uninstall(void)
170{
171 return suplibOsUninstall();
172}
173
174
175DECLEXPORT(int) supR3PreInit(PSUPPREINITDATA pPreInitData, uint32_t fFlags)
176{
177 /*
178 * The caller is kind of trustworthy, just perform some basic checks.
179 *
180 * Note! Do not do any fancy stuff here because IPRT has NOT been
181 * initialized at this point.
182 */
183 if (!VALID_PTR(pPreInitData))
184 return VERR_INVALID_POINTER;
185 if (g_fPreInited || g_cInits > 0)
186 return VERR_WRONG_ORDER;
187
188 if ( pPreInitData->u32Magic != SUPPREINITDATA_MAGIC
189 || pPreInitData->u32EndMagic != SUPPREINITDATA_MAGIC)
190 return VERR_INVALID_MAGIC;
191 if ( !(fFlags & SUPSECMAIN_FLAGS_DONT_OPEN_DEV)
192 && pPreInitData->Data.hDevice == NIL_RTFILE)
193 return VERR_INVALID_HANDLE;
194 if ( (fFlags & SUPSECMAIN_FLAGS_DONT_OPEN_DEV)
195 && pPreInitData->Data.hDevice != NIL_RTFILE)
196 return VERR_INVALID_PARAMETER;
197
198 /*
199 * Hand out the data.
200 */
201 int rc = supR3HardenedRecvPreInitData(pPreInitData);
202 if (RT_FAILURE(rc))
203 return rc;
204
205 /** @todo This may need some small restructuring later, it doesn't quite work with a root service flag... */
206 if (!(fFlags & SUPSECMAIN_FLAGS_DONT_OPEN_DEV))
207 {
208 g_supLibData = pPreInitData->Data;
209 g_fPreInited = true;
210 }
211
212 return VINF_SUCCESS;
213}
214
215
216SUPR3DECL(int) SUPR3Init(PSUPDRVSESSION *ppSession)
217{
218 /*
219 * Perform some sanity checks.
220 * (Got some trouble with compile time member alignment assertions.)
221 */
222 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, u64NanoTSLastUpdateHz) & 0x7));
223 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs) & 0x1f));
224 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs[1]) & 0x1f));
225 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs[0].u64NanoTS) & 0x7));
226 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs[0].u64TSC) & 0x7));
227 Assert(!(RT_OFFSETOF(SUPGLOBALINFOPAGE, aCPUs[0].u64CpuHz) & 0x7));
228
229 /*
230 * Check if already initialized.
231 */
232 if (ppSession)
233 *ppSession = g_pSession;
234 if (g_cInits++ > 0)
235 return VINF_SUCCESS;
236
237 /*
238 * Check for fake mode.
239 *
240 * Fake mode is used when we're doing smoke testing and debugging.
241 * It's also useful on platforms where we haven't root access or which
242 * we haven't ported the support driver to.
243 */
244 if (g_u32FakeMode == ~0U)
245 {
246 const char *psz = RTEnvGet("VBOX_SUPLIB_FAKE");
247 if (psz && !strcmp(psz, "fake"))
248 ASMAtomicCmpXchgU32(&g_u32FakeMode, 1, ~0U);
249 else
250 ASMAtomicCmpXchgU32(&g_u32FakeMode, 0, ~0U);
251 }
252 if (RT_UNLIKELY(g_u32FakeMode))
253 return supInitFake(ppSession);
254
255 /*
256 * Open the support driver.
257 */
258 int rc = suplibOsInit(&g_supLibData, g_fPreInited);
259 if (RT_SUCCESS(rc))
260 {
261 /*
262 * Negotiate the cookie.
263 */
264 SUPCOOKIE CookieReq;
265 memset(&CookieReq, 0xff, sizeof(CookieReq));
266 CookieReq.Hdr.u32Cookie = SUPCOOKIE_INITIAL_COOKIE;
267 CookieReq.Hdr.u32SessionCookie = RTRandU32();
268 CookieReq.Hdr.cbIn = SUP_IOCTL_COOKIE_SIZE_IN;
269 CookieReq.Hdr.cbOut = SUP_IOCTL_COOKIE_SIZE_OUT;
270 CookieReq.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
271 CookieReq.Hdr.rc = VERR_INTERNAL_ERROR;
272 strcpy(CookieReq.u.In.szMagic, SUPCOOKIE_MAGIC);
273 CookieReq.u.In.u32ReqVersion = SUPDRV_IOC_VERSION;
274 const uint32_t uMinVersion = (SUPDRV_IOC_VERSION & 0xffff0000) == 0x00140000
275 ? 0x00140001
276 : SUPDRV_IOC_VERSION & 0xffff0000;
277 CookieReq.u.In.u32MinVersion = uMinVersion;
278 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_COOKIE, &CookieReq, SUP_IOCTL_COOKIE_SIZE);
279 if ( RT_SUCCESS(rc)
280 && RT_SUCCESS(CookieReq.Hdr.rc))
281 {
282 if ( (CookieReq.u.Out.u32SessionVersion & 0xffff0000) == (SUPDRV_IOC_VERSION & 0xffff0000)
283 && CookieReq.u.Out.u32SessionVersion >= uMinVersion)
284 {
285 /*
286 * Query the functions.
287 */
288 PSUPQUERYFUNCS pFuncsReq = (PSUPQUERYFUNCS)RTMemAllocZ(SUP_IOCTL_QUERY_FUNCS_SIZE(CookieReq.u.Out.cFunctions));
289 if (pFuncsReq)
290 {
291 pFuncsReq->Hdr.u32Cookie = CookieReq.u.Out.u32Cookie;
292 pFuncsReq->Hdr.u32SessionCookie = CookieReq.u.Out.u32SessionCookie;
293 pFuncsReq->Hdr.cbIn = SUP_IOCTL_QUERY_FUNCS_SIZE_IN;
294 pFuncsReq->Hdr.cbOut = SUP_IOCTL_QUERY_FUNCS_SIZE_OUT(CookieReq.u.Out.cFunctions);
295 pFuncsReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
296 pFuncsReq->Hdr.rc = VERR_INTERNAL_ERROR;
297 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_QUERY_FUNCS(CookieReq.u.Out.cFunctions), pFuncsReq, SUP_IOCTL_QUERY_FUNCS_SIZE(CookieReq.u.Out.cFunctions));
298 if (RT_SUCCESS(rc))
299 rc = pFuncsReq->Hdr.rc;
300 if (RT_SUCCESS(rc))
301 {
302 /*
303 * Map the GIP into userspace.
304 */
305 Assert(!g_pSUPGlobalInfoPage);
306 SUPGIPMAP GipMapReq;
307 GipMapReq.Hdr.u32Cookie = CookieReq.u.Out.u32Cookie;
308 GipMapReq.Hdr.u32SessionCookie = CookieReq.u.Out.u32SessionCookie;
309 GipMapReq.Hdr.cbIn = SUP_IOCTL_GIP_MAP_SIZE_IN;
310 GipMapReq.Hdr.cbOut = SUP_IOCTL_GIP_MAP_SIZE_OUT;
311 GipMapReq.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
312 GipMapReq.Hdr.rc = VERR_INTERNAL_ERROR;
313 GipMapReq.u.Out.HCPhysGip = NIL_RTHCPHYS;
314 GipMapReq.u.Out.pGipR0 = NIL_RTR0PTR;
315 GipMapReq.u.Out.pGipR3 = NULL;
316 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_GIP_MAP, &GipMapReq, SUP_IOCTL_GIP_MAP_SIZE);
317 if (RT_SUCCESS(rc))
318 rc = GipMapReq.Hdr.rc;
319 if (RT_SUCCESS(rc))
320 {
321 AssertRelease(GipMapReq.u.Out.pGipR3->u32Magic == SUPGLOBALINFOPAGE_MAGIC);
322 AssertRelease(GipMapReq.u.Out.pGipR3->u32Version >= SUPGLOBALINFOPAGE_VERSION);
323
324 /*
325 * Set the globals and return success.
326 */
327 ASMAtomicXchgSize(&g_HCPhysSUPGlobalInfoPage, GipMapReq.u.Out.HCPhysGip);
328 ASMAtomicCmpXchgPtr((void * volatile *)&g_pSUPGlobalInfoPage, GipMapReq.u.Out.pGipR3, NULL);
329 ASMAtomicCmpXchgPtr((void * volatile *)&g_pSUPGlobalInfoPageR0, (void *)GipMapReq.u.Out.pGipR0, NULL);
330
331 g_u32Cookie = CookieReq.u.Out.u32Cookie;
332 g_u32SessionCookie = CookieReq.u.Out.u32SessionCookie;
333 g_pSession = CookieReq.u.Out.pSession;
334 g_pFunctions = pFuncsReq;
335 if (ppSession)
336 *ppSession = CookieReq.u.Out.pSession;
337 return VINF_SUCCESS;
338 }
339 }
340
341 /* bailout */
342 RTMemFree(pFuncsReq);
343 }
344 else
345 rc = VERR_NO_MEMORY;
346 }
347 else
348 {
349 LogRel(("Support driver version mismatch: SessionVersion=%#x DriverVersion=%#x ClientVersion=%#x MinVersion=%#x\n",
350 CookieReq.u.Out.u32SessionVersion, CookieReq.u.Out.u32DriverVersion, SUPDRV_IOC_VERSION, uMinVersion));
351 rc = VERR_VM_DRIVER_VERSION_MISMATCH;
352 }
353 }
354 else
355 {
356 if (RT_SUCCESS(rc))
357 {
358 rc = CookieReq.Hdr.rc;
359 LogRel(("Support driver version mismatch: DriverVersion=%#x ClientVersion=%#x rc=%Rrc\n",
360 CookieReq.u.Out.u32DriverVersion, SUPDRV_IOC_VERSION, rc));
361 if (rc != VERR_VM_DRIVER_VERSION_MISMATCH)
362 rc = VERR_VM_DRIVER_VERSION_MISMATCH;
363 }
364 else
365 {
366 /* for pre 0x00060000 drivers */
367 LogRel(("Support driver version mismatch: DriverVersion=too-old ClientVersion=%#x\n", SUPDRV_IOC_VERSION));
368 rc = VERR_VM_DRIVER_VERSION_MISMATCH;
369 }
370 }
371
372 suplibOsTerm(&g_supLibData);
373 }
374 g_cInits--;
375
376 return rc;
377}
378
379/**
380 * Fake mode init.
381 */
382static int supInitFake(PSUPDRVSESSION *ppSession)
383{
384 Log(("SUP: Fake mode!\n"));
385 static const SUPFUNC s_aFakeFunctions[] =
386 {
387 /* name function */
388 { "SUPR0AbsIs64bit", 0 },
389 { "SUPR0Abs64bitKernelCS", 0 },
390 { "SUPR0Abs64bitKernelSS", 0 },
391 { "SUPR0Abs64bitKernelDS", 0 },
392 { "SUPR0AbsKernelCS", 8 },
393 { "SUPR0AbsKernelSS", 16 },
394 { "SUPR0AbsKernelDS", 16 },
395 { "SUPR0AbsKernelES", 16 },
396 { "SUPR0AbsKernelFS", 24 },
397 { "SUPR0AbsKernelGS", 32 },
398 { "SUPR0ComponentRegisterFactory", 0xefeefffd },
399 { "SUPR0ComponentDeregisterFactory", 0xefeefffe },
400 { "SUPR0ComponentQueryFactory", 0xefeeffff },
401 { "SUPR0ObjRegister", 0xefef0000 },
402 { "SUPR0ObjAddRef", 0xefef0001 },
403 { "SUPR0ObjAddRefEx", 0xefef0001 },
404 { "SUPR0ObjRelease", 0xefef0002 },
405 { "SUPR0ObjVerifyAccess", 0xefef0003 },
406 { "SUPR0LockMem", 0xefef0004 },
407 { "SUPR0UnlockMem", 0xefef0005 },
408 { "SUPR0ContAlloc", 0xefef0006 },
409 { "SUPR0ContFree", 0xefef0007 },
410 { "SUPR0MemAlloc", 0xefef0008 },
411 { "SUPR0MemGetPhys", 0xefef0009 },
412 { "SUPR0MemFree", 0xefef000a },
413 { "SUPR0Printf", 0xefef000b },
414 { "SUPR0GetPagingMode", 0xefef000c },
415 { "SUPR0EnableVTx", 0xefef000e },
416 { "RTMemAlloc", 0xefef000f },
417 { "RTMemAllocZ", 0xefef0010 },
418 { "RTMemFree", 0xefef0011 },
419 { "RTR0MemObjAddress", 0xefef0012 },
420 { "RTR0MemObjAddressR3", 0xefef0013 },
421 { "RTR0MemObjAllocPage", 0xefef0014 },
422 { "RTR0MemObjAllocPhysNC", 0xefef0015 },
423 { "RTR0MemObjAllocLow", 0xefef0016 },
424 { "RTR0MemObjEnterPhys", 0xefef0017 },
425 { "RTR0MemObjFree", 0xefef0018 },
426 { "RTR0MemObjGetPagePhysAddr", 0xefef0019 },
427 { "RTR0MemObjMapUser", 0xefef001a },
428 { "RTR0MemObjMapKernel", 0xefef001b },
429 { "RTR0MemObjMapKernelEx", 0xefef001c },
430 { "RTProcSelf", 0xefef001d },
431 { "RTR0ProcHandleSelf", 0xefef001e },
432 { "RTSemEventCreate", 0xefef001f },
433 { "RTSemEventSignal", 0xefef0020 },
434 { "RTSemEventWait", 0xefef0021 },
435 { "RTSemEventWaitNoResume", 0xefef0022 },
436 { "RTSemEventDestroy", 0xefef0023 },
437 { "RTSemEventMultiCreate", 0xefef0024 },
438 { "RTSemEventMultiSignal", 0xefef0025 },
439 { "RTSemEventMultiReset", 0xefef0026 },
440 { "RTSemEventMultiWait", 0xefef0027 },
441 { "RTSemEventMultiWaitNoResume", 0xefef0028 },
442 { "RTSemEventMultiDestroy", 0xefef0029 },
443 { "RTSemFastMutexCreate", 0xefef002a },
444 { "RTSemFastMutexDestroy", 0xefef002b },
445 { "RTSemFastMutexRequest", 0xefef002c },
446 { "RTSemFastMutexRelease", 0xefef002d },
447 { "RTSpinlockCreate", 0xefef002e },
448 { "RTSpinlockDestroy", 0xefef002f },
449 { "RTSpinlockAcquire", 0xefef0030 },
450 { "RTSpinlockRelease", 0xefef0031 },
451 { "RTSpinlockAcquireNoInts", 0xefef0032 },
452 { "RTSpinlockReleaseNoInts", 0xefef0033 },
453 { "RTTimeNanoTS", 0xefef0034 },
454 { "RTTimeMillieTS", 0xefef0035 },
455 { "RTTimeSystemNanoTS", 0xefef0036 },
456 { "RTTimeSystemMillieTS", 0xefef0037 },
457 { "RTThreadNativeSelf", 0xefef0038 },
458 { "RTThreadSleep", 0xefef0039 },
459 { "RTThreadYield", 0xefef003a },
460 { "RTLogDefaultInstance", 0xefef003b },
461 { "RTLogRelDefaultInstance", 0xefef003c },
462 { "RTLogSetDefaultInstanceThread", 0xefef003d },
463 { "RTLogLogger", 0xefef003e },
464 { "RTLogLoggerEx", 0xefef003f },
465 { "RTLogLoggerExV", 0xefef0040 },
466 { "RTAssertMsg1", 0xefef0041 },
467 { "RTAssertMsg2", 0xefef0042 },
468 { "RTAssertMsg2V", 0xefef0043 },
469 { "SUPR0QueryVTCaps", 0xefef0044 },
470 };
471
472 /* fake r0 functions. */
473 g_pFunctions = (PSUPQUERYFUNCS)RTMemAllocZ(SUP_IOCTL_QUERY_FUNCS_SIZE(RT_ELEMENTS(s_aFakeFunctions)));
474 if (g_pFunctions)
475 {
476 g_pFunctions->u.Out.cFunctions = RT_ELEMENTS(s_aFakeFunctions);
477 memcpy(&g_pFunctions->u.Out.aFunctions[0], &s_aFakeFunctions[0], sizeof(s_aFakeFunctions));
478 g_pSession = (PSUPDRVSESSION)(void *)g_pFunctions;
479 if (ppSession)
480 *ppSession = g_pSession;
481
482 /* fake the GIP. */
483 g_pSUPGlobalInfoPage = (PSUPGLOBALINFOPAGE)RTMemPageAllocZ(PAGE_SIZE);
484 if (g_pSUPGlobalInfoPage)
485 {
486 g_pSUPGlobalInfoPageR0 = g_pSUPGlobalInfoPage;
487 g_HCPhysSUPGlobalInfoPage = NIL_RTHCPHYS & ~(RTHCPHYS)PAGE_OFFSET_MASK;
488 /* the page is supposed to be invalid, so don't set the magic. */
489 return VINF_SUCCESS;
490 }
491
492 RTMemFree(g_pFunctions);
493 g_pFunctions = NULL;
494 }
495 return VERR_NO_MEMORY;
496}
497
498
499SUPR3DECL(int) SUPR3Term(bool fForced)
500{
501 /*
502 * Verify state.
503 */
504 AssertMsg(g_cInits > 0, ("SUPR3Term() is called before SUPR3Init()!\n"));
505 if (g_cInits == 0)
506 return VERR_WRONG_ORDER;
507 if (g_cInits == 1 || fForced)
508 {
509 /*
510 * NULL the GIP pointer.
511 */
512 if (g_pSUPGlobalInfoPage)
513 {
514 ASMAtomicXchgPtr((void * volatile *)&g_pSUPGlobalInfoPage, NULL);
515 ASMAtomicXchgPtr((void * volatile *)&g_pSUPGlobalInfoPageR0, NULL);
516 ASMAtomicXchgSize(&g_HCPhysSUPGlobalInfoPage, NIL_RTHCPHYS);
517 /* just a little safe guard against threads using the page. */
518 RTThreadSleep(50);
519 }
520
521 /*
522 * Close the support driver.
523 */
524 int rc = suplibOsTerm(&g_supLibData);
525 if (rc)
526 return rc;
527
528 g_u32Cookie = 0;
529 g_u32SessionCookie = 0;
530 g_cInits = 0;
531 }
532 else
533 g_cInits--;
534
535 return 0;
536}
537
538
539SUPR3DECL(SUPPAGINGMODE) SUPR3GetPagingMode(void)
540{
541 /* fake */
542 if (RT_UNLIKELY(g_u32FakeMode))
543#ifdef RT_ARCH_AMD64
544 return SUPPAGINGMODE_AMD64_GLOBAL_NX;
545#else
546 return SUPPAGINGMODE_32_BIT_GLOBAL;
547#endif
548
549 /*
550 * Issue IOCtl to the SUPDRV kernel module.
551 */
552 SUPGETPAGINGMODE Req;
553 Req.Hdr.u32Cookie = g_u32Cookie;
554 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
555 Req.Hdr.cbIn = SUP_IOCTL_GET_PAGING_MODE_SIZE_IN;
556 Req.Hdr.cbOut = SUP_IOCTL_GET_PAGING_MODE_SIZE_OUT;
557 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
558 Req.Hdr.rc = VERR_INTERNAL_ERROR;
559 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_GET_PAGING_MODE, &Req, SUP_IOCTL_GET_PAGING_MODE_SIZE);
560 if ( RT_FAILURE(rc)
561 || RT_FAILURE(Req.Hdr.rc))
562 {
563 LogRel(("SUPR3GetPagingMode: %Rrc %Rrc\n", rc, Req.Hdr.rc));
564 Req.u.Out.enmMode = SUPPAGINGMODE_INVALID;
565 }
566
567 return Req.u.Out.enmMode;
568}
569
570
571/**
572 * For later.
573 */
574static int supCallVMMR0ExFake(PVMR0 pVMR0, unsigned uOperation, uint64_t u64Arg, PSUPVMMR0REQHDR pReqHdr)
575{
576 AssertMsgFailed(("%d\n", uOperation));
577 return VERR_NOT_SUPPORTED;
578}
579
580
581SUPR3DECL(int) SUPR3CallVMMR0Fast(PVMR0 pVMR0, unsigned uOperation, VMCPUID idCpu)
582{
583 if (RT_LIKELY(uOperation == SUP_VMMR0_DO_RAW_RUN))
584 return suplibOsIOCtlFast(&g_supLibData, SUP_IOCTL_FAST_DO_RAW_RUN, idCpu);
585 if (RT_LIKELY(uOperation == SUP_VMMR0_DO_HWACC_RUN))
586 return suplibOsIOCtlFast(&g_supLibData, SUP_IOCTL_FAST_DO_HWACC_RUN, idCpu);
587 if (RT_LIKELY(uOperation == SUP_VMMR0_DO_NOP))
588 return suplibOsIOCtlFast(&g_supLibData, SUP_IOCTL_FAST_DO_NOP, idCpu);
589
590 AssertMsgFailed(("%#x\n", uOperation));
591 return VERR_INTERNAL_ERROR;
592}
593
594
595SUPR3DECL(int) SUPR3CallVMMR0Ex(PVMR0 pVMR0, VMCPUID idCpu, unsigned uOperation, uint64_t u64Arg, PSUPVMMR0REQHDR pReqHdr)
596{
597 /*
598 * The following operations don't belong here.
599 */
600 AssertMsgReturn( uOperation != SUP_VMMR0_DO_RAW_RUN
601 && uOperation != SUP_VMMR0_DO_HWACC_RUN
602 && uOperation != SUP_VMMR0_DO_NOP,
603 ("%#x\n", uOperation),
604 VERR_INTERNAL_ERROR);
605
606 /* fake */
607 if (RT_UNLIKELY(g_u32FakeMode))
608 return supCallVMMR0ExFake(pVMR0, uOperation, u64Arg, pReqHdr);
609
610 int rc;
611 if (!pReqHdr)
612 {
613 /* no data. */
614 SUPCALLVMMR0 Req;
615 Req.Hdr.u32Cookie = g_u32Cookie;
616 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
617 Req.Hdr.cbIn = SUP_IOCTL_CALL_VMMR0_SIZE_IN(0);
618 Req.Hdr.cbOut = SUP_IOCTL_CALL_VMMR0_SIZE_OUT(0);
619 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
620 Req.Hdr.rc = VERR_INTERNAL_ERROR;
621 Req.u.In.pVMR0 = pVMR0;
622 Req.u.In.idCpu = idCpu;
623 Req.u.In.uOperation = uOperation;
624 Req.u.In.u64Arg = u64Arg;
625 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_VMMR0(0), &Req, SUP_IOCTL_CALL_VMMR0_SIZE(0));
626 if (RT_SUCCESS(rc))
627 rc = Req.Hdr.rc;
628 }
629 else if (SUP_IOCTL_CALL_VMMR0_SIZE(pReqHdr->cbReq) < _4K) /* FreeBSD won't copy more than 4K. */
630 {
631 AssertPtrReturn(pReqHdr, VERR_INVALID_POINTER);
632 AssertReturn(pReqHdr->u32Magic == SUPVMMR0REQHDR_MAGIC, VERR_INVALID_MAGIC);
633 const size_t cbReq = pReqHdr->cbReq;
634
635 PSUPCALLVMMR0 pReq = (PSUPCALLVMMR0)alloca(SUP_IOCTL_CALL_VMMR0_SIZE(cbReq));
636 pReq->Hdr.u32Cookie = g_u32Cookie;
637 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
638 pReq->Hdr.cbIn = SUP_IOCTL_CALL_VMMR0_SIZE_IN(cbReq);
639 pReq->Hdr.cbOut = SUP_IOCTL_CALL_VMMR0_SIZE_OUT(cbReq);
640 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
641 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
642 pReq->u.In.pVMR0 = pVMR0;
643 pReq->u.In.idCpu = idCpu;
644 pReq->u.In.uOperation = uOperation;
645 pReq->u.In.u64Arg = u64Arg;
646 memcpy(&pReq->abReqPkt[0], pReqHdr, cbReq);
647 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_VMMR0(cbReq), pReq, SUP_IOCTL_CALL_VMMR0_SIZE(cbReq));
648 if (RT_SUCCESS(rc))
649 rc = pReq->Hdr.rc;
650 memcpy(pReqHdr, &pReq->abReqPkt[0], cbReq);
651 }
652 else /** @todo may have to remove the size limits one this request... */
653 AssertMsgFailedReturn(("cbReq=%#x\n", pReqHdr->cbReq), VERR_INTERNAL_ERROR);
654 return rc;
655}
656
657
658SUPR3DECL(int) SUPR3CallVMMR0(PVMR0 pVMR0, VMCPUID idCpu, unsigned uOperation, void *pvArg)
659{
660 /*
661 * The following operations don't belong here.
662 */
663 AssertMsgReturn( uOperation != SUP_VMMR0_DO_RAW_RUN
664 && uOperation != SUP_VMMR0_DO_HWACC_RUN
665 && uOperation != SUP_VMMR0_DO_NOP,
666 ("%#x\n", uOperation),
667 VERR_INTERNAL_ERROR);
668 return SUPR3CallVMMR0Ex(pVMR0, idCpu, uOperation, (uintptr_t)pvArg, NULL);
669}
670
671
672SUPR3DECL(int) SUPR3SetVMForFastIOCtl(PVMR0 pVMR0)
673{
674 if (RT_UNLIKELY(g_u32FakeMode))
675 return VINF_SUCCESS;
676
677 SUPSETVMFORFAST Req;
678 Req.Hdr.u32Cookie = g_u32Cookie;
679 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
680 Req.Hdr.cbIn = SUP_IOCTL_SET_VM_FOR_FAST_SIZE_IN;
681 Req.Hdr.cbOut = SUP_IOCTL_SET_VM_FOR_FAST_SIZE_OUT;
682 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
683 Req.Hdr.rc = VERR_INTERNAL_ERROR;
684 Req.u.In.pVMR0 = pVMR0;
685 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_SET_VM_FOR_FAST, &Req, SUP_IOCTL_SET_VM_FOR_FAST_SIZE);
686 if (RT_SUCCESS(rc))
687 rc = Req.Hdr.rc;
688 return rc;
689}
690
691
692SUPR3DECL(int) SUPR3CallR0Service(const char *pszService, size_t cchService, uint32_t uOperation, uint64_t u64Arg, PSUPR0SERVICEREQHDR pReqHdr)
693{
694 AssertReturn(cchService < RT_SIZEOFMEMB(SUPCALLSERVICE, u.In.szName), VERR_INVALID_PARAMETER);
695 Assert(strlen(pszService) == cchService);
696
697 /* fake */
698 if (RT_UNLIKELY(g_u32FakeMode))
699 return VERR_NOT_SUPPORTED;
700
701 int rc;
702 if (!pReqHdr)
703 {
704 /* no data. */
705 SUPCALLSERVICE Req;
706 Req.Hdr.u32Cookie = g_u32Cookie;
707 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
708 Req.Hdr.cbIn = SUP_IOCTL_CALL_SERVICE_SIZE_IN(0);
709 Req.Hdr.cbOut = SUP_IOCTL_CALL_SERVICE_SIZE_OUT(0);
710 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
711 Req.Hdr.rc = VERR_INTERNAL_ERROR;
712 memcpy(Req.u.In.szName, pszService, cchService);
713 Req.u.In.szName[cchService] = '\0';
714 Req.u.In.uOperation = uOperation;
715 Req.u.In.u64Arg = u64Arg;
716 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_SERVICE(0), &Req, SUP_IOCTL_CALL_SERVICE_SIZE(0));
717 if (RT_SUCCESS(rc))
718 rc = Req.Hdr.rc;
719 }
720 else if (SUP_IOCTL_CALL_SERVICE_SIZE(pReqHdr->cbReq) < _4K) /* FreeBSD won't copy more than 4K. */
721 {
722 AssertPtrReturn(pReqHdr, VERR_INVALID_POINTER);
723 AssertReturn(pReqHdr->u32Magic == SUPR0SERVICEREQHDR_MAGIC, VERR_INVALID_MAGIC);
724 const size_t cbReq = pReqHdr->cbReq;
725
726 PSUPCALLSERVICE pReq = (PSUPCALLSERVICE)alloca(SUP_IOCTL_CALL_SERVICE_SIZE(cbReq));
727 pReq->Hdr.u32Cookie = g_u32Cookie;
728 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
729 pReq->Hdr.cbIn = SUP_IOCTL_CALL_SERVICE_SIZE_IN(cbReq);
730 pReq->Hdr.cbOut = SUP_IOCTL_CALL_SERVICE_SIZE_OUT(cbReq);
731 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
732 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
733 memcpy(pReq->u.In.szName, pszService, cchService);
734 pReq->u.In.szName[cchService] = '\0';
735 pReq->u.In.uOperation = uOperation;
736 pReq->u.In.u64Arg = u64Arg;
737 memcpy(&pReq->abReqPkt[0], pReqHdr, cbReq);
738 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_SERVICE(cbReq), pReq, SUP_IOCTL_CALL_SERVICE_SIZE(cbReq));
739 if (RT_SUCCESS(rc))
740 rc = pReq->Hdr.rc;
741 memcpy(pReqHdr, &pReq->abReqPkt[0], cbReq);
742 }
743 else /** @todo may have to remove the size limits one this request... */
744 AssertMsgFailedReturn(("cbReq=%#x\n", pReqHdr->cbReq), VERR_INTERNAL_ERROR);
745 return rc;
746}
747
748
749/**
750 * Worker for the SUPR3Logger* APIs.
751 *
752 * @returns VBox status code.
753 * @param enmWhich Which logger.
754 * @param fWhat What to do with the logger.
755 * @param pszFlags The flags settings.
756 * @param pszGroups The groups settings.
757 * @param pszDest The destionation specificier.
758 */
759static int supR3LoggerSettings(SUPLOGGER enmWhich, uint32_t fWhat, const char *pszFlags, const char *pszGroups, const char *pszDest)
760{
761 uint32_t const cchFlags = pszFlags ? (uint32_t)strlen(pszFlags) : 0;
762 uint32_t const cchGroups = pszGroups ? (uint32_t)strlen(pszGroups) : 0;
763 uint32_t const cchDest = pszDest ? (uint32_t)strlen(pszDest) : 0;
764 uint32_t const cbStrTab = cchFlags + !!cchFlags
765 + cchGroups + !!cchGroups
766 + cchDest + !!cchDest
767 + (!cchFlags && !cchGroups && !cchDest);
768
769 PSUPLOGGERSETTINGS pReq = (PSUPLOGGERSETTINGS)alloca(SUP_IOCTL_LOGGER_SETTINGS_SIZE(cbStrTab));
770 pReq->Hdr.u32Cookie = g_u32Cookie;
771 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
772 pReq->Hdr.cbIn = SUP_IOCTL_LOGGER_SETTINGS_SIZE_IN(cbStrTab);
773 pReq->Hdr.cbOut = SUP_IOCTL_LOGGER_SETTINGS_SIZE_OUT;
774 pReq->Hdr.fFlags= SUPREQHDR_FLAGS_DEFAULT;
775 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
776 switch (enmWhich)
777 {
778 case SUPLOGGER_DEBUG: pReq->u.In.fWhich = SUPLOGGERSETTINGS_WHICH_DEBUG; break;
779 case SUPLOGGER_RELEASE: pReq->u.In.fWhich = SUPLOGGERSETTINGS_WHICH_RELEASE; break;
780 default:
781 return VERR_INVALID_PARAMETER;
782 }
783 pReq->u.In.fWhat = fWhat;
784
785 uint32_t off = 0;
786 if (cchFlags)
787 {
788 pReq->u.In.offFlags = off;
789 memcpy(&pReq->u.In.szStrings[off], pszFlags, cchFlags + 1);
790 off += cchFlags + 1;
791 }
792 else
793 pReq->u.In.offFlags = cbStrTab - 1;
794
795 if (cchGroups)
796 {
797 pReq->u.In.offGroups = off;
798 memcpy(&pReq->u.In.szStrings[off], pszGroups, cchGroups + 1);
799 off += cchGroups + 1;
800 }
801 else
802 pReq->u.In.offGroups = cbStrTab - 1;
803
804 if (cchDest)
805 {
806 pReq->u.In.offDestination = off;
807 memcpy(&pReq->u.In.szStrings[off], pszDest, cchDest + 1);
808 off += cchDest + 1;
809 }
810 else
811 pReq->u.In.offDestination = cbStrTab - 1;
812
813 if (!off)
814 {
815 pReq->u.In.szStrings[0] = '\0';
816 off++;
817 }
818 Assert(off == cbStrTab);
819 Assert(pReq->u.In.szStrings[cbStrTab - 1] == '\0');
820
821
822 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LOGGER_SETTINGS(cbStrTab), pReq, SUP_IOCTL_LOGGER_SETTINGS_SIZE(cbStrTab));
823 if (RT_SUCCESS(rc))
824 rc = pReq->Hdr.rc;
825 return rc;
826}
827
828
829SUPR3DECL(int) SUPR3LoggerSettings(SUPLOGGER enmWhich, const char *pszFlags, const char *pszGroups, const char *pszDest)
830{
831 return supR3LoggerSettings(enmWhich, SUPLOGGERSETTINGS_WHAT_SETTINGS, pszFlags, pszGroups, pszDest);
832}
833
834
835SUPR3DECL(int) SUPR3LoggerCreate(SUPLOGGER enmWhich, const char *pszFlags, const char *pszGroups, const char *pszDest)
836{
837 return supR3LoggerSettings(enmWhich, SUPLOGGERSETTINGS_WHAT_CREATE, pszFlags, pszGroups, pszDest);
838}
839
840
841SUPR3DECL(int) SUPR3LoggerDestroy(SUPLOGGER enmWhich)
842{
843 return supR3LoggerSettings(enmWhich, SUPLOGGERSETTINGS_WHAT_DESTROY, NULL, NULL, NULL);
844}
845
846
847SUPR3DECL(int) SUPR3PageAlloc(size_t cPages, void **ppvPages)
848{
849 /*
850 * Validate.
851 */
852 AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
853 *ppvPages = NULL;
854 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
855
856 /*
857 * Call OS specific worker.
858 */
859 return suplibOsPageAlloc(&g_supLibData, cPages, ppvPages);
860}
861
862
863SUPR3DECL(int) SUPR3PageFree(void *pvPages, size_t cPages)
864{
865 /*
866 * Validate.
867 */
868 AssertPtrReturn(pvPages, VERR_INVALID_POINTER);
869 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
870
871 /*
872 * Call OS specific worker.
873 */
874 return suplibOsPageFree(&g_supLibData, pvPages, cPages);
875}
876
877
878/**
879 * Locks down the physical memory backing a virtual memory
880 * range in the current process.
881 *
882 * @returns VBox status code.
883 * @param pvStart Start of virtual memory range.
884 * Must be page aligned.
885 * @param cPages Number of pages.
886 * @param paPages Where to store the physical page addresses returned.
887 * On entry this will point to an array of with cbMemory >> PAGE_SHIFT entries.
888 */
889SUPR3DECL(int) supR3PageLock(void *pvStart, size_t cPages, PSUPPAGE paPages)
890{
891 /*
892 * Validate.
893 */
894 AssertPtr(pvStart);
895 AssertMsg(RT_ALIGN_P(pvStart, PAGE_SIZE) == pvStart, ("pvStart (%p) must be page aligned\n", pvStart));
896 AssertPtr(paPages);
897
898 /* fake */
899 if (RT_UNLIKELY(g_u32FakeMode))
900 {
901 RTHCPHYS Phys = (uintptr_t)pvStart + PAGE_SIZE * 1024;
902 size_t iPage = cPages;
903 while (iPage-- > 0)
904 paPages[iPage].Phys = Phys + (iPage << PAGE_SHIFT);
905 return VINF_SUCCESS;
906 }
907
908 /*
909 * Issue IOCtl to the SUPDRV kernel module.
910 */
911 int rc;
912 PSUPPAGELOCK pReq = (PSUPPAGELOCK)RTMemTmpAllocZ(SUP_IOCTL_PAGE_LOCK_SIZE(cPages));
913 if (RT_LIKELY(pReq))
914 {
915 pReq->Hdr.u32Cookie = g_u32Cookie;
916 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
917 pReq->Hdr.cbIn = SUP_IOCTL_PAGE_LOCK_SIZE_IN;
918 pReq->Hdr.cbOut = SUP_IOCTL_PAGE_LOCK_SIZE_OUT(cPages);
919 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
920 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
921 pReq->u.In.pvR3 = pvStart;
922 pReq->u.In.cPages = (uint32_t)cPages; AssertRelease(pReq->u.In.cPages == cPages);
923 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_LOCK, pReq, SUP_IOCTL_PAGE_LOCK_SIZE(cPages));
924 if (RT_SUCCESS(rc))
925 rc = pReq->Hdr.rc;
926 if (RT_SUCCESS(rc))
927 {
928 for (uint32_t iPage = 0; iPage < cPages; iPage++)
929 {
930 paPages[iPage].uReserved = 0;
931 paPages[iPage].Phys = pReq->u.Out.aPages[iPage];
932 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
933 }
934 }
935 RTMemTmpFree(pReq);
936 }
937 else
938 rc = VERR_NO_TMP_MEMORY;
939
940 return rc;
941}
942
943
944/**
945 * Releases locked down pages.
946 *
947 * @returns VBox status code.
948 * @param pvStart Start of virtual memory range previously locked
949 * down by SUPPageLock().
950 */
951SUPR3DECL(int) supR3PageUnlock(void *pvStart)
952{
953 /*
954 * Validate.
955 */
956 AssertPtr(pvStart);
957 AssertMsg(RT_ALIGN_P(pvStart, PAGE_SIZE) == pvStart, ("pvStart (%p) must be page aligned\n", pvStart));
958
959 /* fake */
960 if (RT_UNLIKELY(g_u32FakeMode))
961 return VINF_SUCCESS;
962
963 /*
964 * Issue IOCtl to the SUPDRV kernel module.
965 */
966 SUPPAGEUNLOCK Req;
967 Req.Hdr.u32Cookie = g_u32Cookie;
968 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
969 Req.Hdr.cbIn = SUP_IOCTL_PAGE_UNLOCK_SIZE_IN;
970 Req.Hdr.cbOut = SUP_IOCTL_PAGE_UNLOCK_SIZE_OUT;
971 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
972 Req.Hdr.rc = VERR_INTERNAL_ERROR;
973 Req.u.In.pvR3 = pvStart;
974 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_UNLOCK, &Req, SUP_IOCTL_PAGE_UNLOCK_SIZE);
975 if (RT_SUCCESS(rc))
976 rc = Req.Hdr.rc;
977 return rc;
978}
979
980
981/**
982 * Fallback for SUPR3PageAllocEx on systems where RTR0MemObjPhysAllocNC isn't
983 * supported.
984 */
985static int supPagePageAllocNoKernelFallback(size_t cPages, void **ppvPages, PSUPPAGE paPages)
986{
987 int rc = suplibOsPageAlloc(&g_supLibData, cPages, ppvPages);
988 if (RT_SUCCESS(rc))
989 {
990 if (!paPages)
991 paPages = (PSUPPAGE)alloca(sizeof(paPages[0]) * cPages);
992 rc = supR3PageLock(*ppvPages, cPages, paPages);
993 if (RT_FAILURE(rc))
994 suplibOsPageFree(&g_supLibData, *ppvPages, cPages);
995 }
996 return rc;
997}
998
999
1000SUPR3DECL(int) SUPR3PageAllocEx(size_t cPages, uint32_t fFlags, void **ppvPages, PRTR0PTR pR0Ptr, PSUPPAGE paPages)
1001{
1002 /*
1003 * Validate.
1004 */
1005 AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
1006 *ppvPages = NULL;
1007 AssertPtrNullReturn(pR0Ptr, VERR_INVALID_POINTER);
1008 if (pR0Ptr)
1009 *pR0Ptr = NIL_RTR0PTR;
1010 AssertPtrNullReturn(paPages, VERR_INVALID_POINTER);
1011 AssertMsgReturn(cPages > 0 && cPages <= VBOX_MAX_ALLOC_PAGE_COUNT, ("cPages=%zu\n", cPages), VERR_PAGE_COUNT_OUT_OF_RANGE);
1012
1013 /* fake */
1014 if (RT_UNLIKELY(g_u32FakeMode))
1015 {
1016 void *pv = RTMemPageAllocZ(cPages * PAGE_SIZE);
1017 if (!pv)
1018 return VERR_NO_MEMORY;
1019 *ppvPages = pv;
1020 if (pR0Ptr)
1021 *pR0Ptr = (RTR0PTR)pv;
1022 if (paPages)
1023 for (size_t iPage = 0; iPage < cPages; iPage++)
1024 {
1025 paPages[iPage].uReserved = 0;
1026 paPages[iPage].Phys = (iPage + 4321) << PAGE_SHIFT;
1027 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
1028 }
1029 return VINF_SUCCESS;
1030 }
1031
1032 /*
1033 * Use fallback for non-R0 mapping?
1034 */
1035 if ( !pR0Ptr
1036 && !g_fSupportsPageAllocNoKernel)
1037 return supPagePageAllocNoKernelFallback(cPages, ppvPages, paPages);
1038
1039 /*
1040 * Issue IOCtl to the SUPDRV kernel module.
1041 */
1042 int rc;
1043 PSUPPAGEALLOCEX pReq = (PSUPPAGEALLOCEX)RTMemTmpAllocZ(SUP_IOCTL_PAGE_ALLOC_EX_SIZE(cPages));
1044 if (pReq)
1045 {
1046 pReq->Hdr.u32Cookie = g_u32Cookie;
1047 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
1048 pReq->Hdr.cbIn = SUP_IOCTL_PAGE_ALLOC_EX_SIZE_IN;
1049 pReq->Hdr.cbOut = SUP_IOCTL_PAGE_ALLOC_EX_SIZE_OUT(cPages);
1050 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
1051 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
1052 pReq->u.In.cPages = (uint32_t)cPages; AssertRelease(pReq->u.In.cPages == cPages);
1053 pReq->u.In.fKernelMapping = pR0Ptr != NULL;
1054 pReq->u.In.fUserMapping = true;
1055 pReq->u.In.fReserved0 = false;
1056 pReq->u.In.fReserved1 = false;
1057 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_ALLOC_EX, pReq, SUP_IOCTL_PAGE_ALLOC_EX_SIZE(cPages));
1058 if (RT_SUCCESS(rc))
1059 {
1060 rc = pReq->Hdr.rc;
1061 if (RT_SUCCESS(rc))
1062 {
1063 *ppvPages = pReq->u.Out.pvR3;
1064 if (pR0Ptr)
1065 *pR0Ptr = pReq->u.Out.pvR0;
1066 if (paPages)
1067 for (size_t iPage = 0; iPage < cPages; iPage++)
1068 {
1069 paPages[iPage].uReserved = 0;
1070 paPages[iPage].Phys = pReq->u.Out.aPages[iPage];
1071 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
1072 }
1073#ifdef RT_OS_DARWIN /* HACK ALERT! */
1074 supR3TouchPages(pReq->u.Out.pvR3, cPages);
1075#endif
1076 }
1077 else if ( rc == VERR_NOT_SUPPORTED
1078 && !pR0Ptr)
1079 {
1080 g_fSupportsPageAllocNoKernel = false;
1081 rc = supPagePageAllocNoKernelFallback(cPages, ppvPages, paPages);
1082 }
1083 }
1084
1085 RTMemTmpFree(pReq);
1086 }
1087 else
1088 rc = VERR_NO_TMP_MEMORY;
1089 return rc;
1090
1091}
1092
1093
1094SUPR3DECL(int) SUPR3PageMapKernel(void *pvR3, uint32_t off, uint32_t cb, uint32_t fFlags, PRTR0PTR pR0Ptr)
1095{
1096 /*
1097 * Validate.
1098 */
1099 AssertPtrReturn(pvR3, VERR_INVALID_POINTER);
1100 AssertPtrReturn(pR0Ptr, VERR_INVALID_POINTER);
1101 Assert(!(off & PAGE_OFFSET_MASK));
1102 Assert(!(cb & PAGE_OFFSET_MASK) && cb);
1103 Assert(!fFlags);
1104 *pR0Ptr = NIL_RTR0PTR;
1105
1106 /* fake */
1107 if (RT_UNLIKELY(g_u32FakeMode))
1108 return VERR_NOT_SUPPORTED;
1109
1110 /*
1111 * Issue IOCtl to the SUPDRV kernel module.
1112 */
1113 SUPPAGEMAPKERNEL Req;
1114 Req.Hdr.u32Cookie = g_u32Cookie;
1115 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1116 Req.Hdr.cbIn = SUP_IOCTL_PAGE_MAP_KERNEL_SIZE_IN;
1117 Req.Hdr.cbOut = SUP_IOCTL_PAGE_MAP_KERNEL_SIZE_OUT;
1118 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1119 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1120 Req.u.In.pvR3 = pvR3;
1121 Req.u.In.offSub = off;
1122 Req.u.In.cbSub = cb;
1123 Req.u.In.fFlags = fFlags;
1124 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_MAP_KERNEL, &Req, SUP_IOCTL_PAGE_MAP_KERNEL_SIZE);
1125 if (RT_SUCCESS(rc))
1126 rc = Req.Hdr.rc;
1127 if (RT_SUCCESS(rc))
1128 *pR0Ptr = Req.u.Out.pvR0;
1129 return rc;
1130}
1131
1132
1133SUPR3DECL(int) SUPR3PageProtect(void *pvR3, RTR0PTR R0Ptr, uint32_t off, uint32_t cb, uint32_t fProt)
1134{
1135 /*
1136 * Validate.
1137 */
1138 AssertPtrReturn(pvR3, VERR_INVALID_POINTER);
1139 Assert(!(off & PAGE_OFFSET_MASK));
1140 Assert(!(cb & PAGE_OFFSET_MASK) && cb);
1141 AssertReturn(!(fProt & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC)), VERR_INVALID_PARAMETER);
1142
1143 /* fake */
1144 if (RT_UNLIKELY(g_u32FakeMode))
1145 return RTMemProtect((uint8_t *)pvR3 + off, cb, fProt);
1146
1147 /*
1148 * Some OSes can do this from ring-3, so try that before we
1149 * issue the IOCtl to the SUPDRV kernel module.
1150 * (Yea, this isn't very nice, but just try get the job done for now.)
1151 */
1152#if !defined(RT_OS_SOLARIS)
1153 RTMemProtect((uint8_t *)pvR3 + off, cb, fProt);
1154#endif
1155
1156 SUPPAGEPROTECT Req;
1157 Req.Hdr.u32Cookie = g_u32Cookie;
1158 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1159 Req.Hdr.cbIn = SUP_IOCTL_PAGE_PROTECT_SIZE_IN;
1160 Req.Hdr.cbOut = SUP_IOCTL_PAGE_PROTECT_SIZE_OUT;
1161 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1162 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1163 Req.u.In.pvR3 = pvR3;
1164 Req.u.In.pvR0 = R0Ptr;
1165 Req.u.In.offSub = off;
1166 Req.u.In.cbSub = cb;
1167 Req.u.In.fProt = fProt;
1168 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_PROTECT, &Req, SUP_IOCTL_PAGE_PROTECT_SIZE);
1169 if (RT_SUCCESS(rc))
1170 rc = Req.Hdr.rc;
1171 return rc;
1172}
1173
1174
1175SUPR3DECL(int) SUPR3PageFreeEx(void *pvPages, size_t cPages)
1176{
1177 /*
1178 * Validate.
1179 */
1180 AssertPtrReturn(pvPages, VERR_INVALID_POINTER);
1181 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1182
1183 /* fake */
1184 if (RT_UNLIKELY(g_u32FakeMode))
1185 {
1186 RTMemPageFree(pvPages);
1187 return VINF_SUCCESS;
1188 }
1189
1190 /*
1191 * Try normal free first, then if it fails check if we're using the fallback .
1192 * for the allocations without kernel mappings and attempt unlocking it.
1193 */
1194 NOREF(cPages);
1195 SUPPAGEFREE Req;
1196 Req.Hdr.u32Cookie = g_u32Cookie;
1197 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1198 Req.Hdr.cbIn = SUP_IOCTL_PAGE_FREE_SIZE_IN;
1199 Req.Hdr.cbOut = SUP_IOCTL_PAGE_FREE_SIZE_OUT;
1200 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1201 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1202 Req.u.In.pvR3 = pvPages;
1203 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_FREE, &Req, SUP_IOCTL_PAGE_FREE_SIZE);
1204 if (RT_SUCCESS(rc))
1205 {
1206 rc = Req.Hdr.rc;
1207 if ( rc == VERR_INVALID_PARAMETER
1208 && !g_fSupportsPageAllocNoKernel)
1209 {
1210 int rc2 = supR3PageUnlock(pvPages);
1211 if (RT_SUCCESS(rc2))
1212 rc = suplibOsPageFree(&g_supLibData, pvPages, cPages);
1213 }
1214 }
1215 return rc;
1216}
1217
1218
1219SUPR3DECL(void *) SUPR3ContAlloc(size_t cPages, PRTR0PTR pR0Ptr, PRTHCPHYS pHCPhys)
1220{
1221 /*
1222 * Validate.
1223 */
1224 AssertPtrReturn(pHCPhys, NULL);
1225 *pHCPhys = NIL_RTHCPHYS;
1226 AssertPtrNullReturn(pR0Ptr, NULL);
1227 if (pR0Ptr)
1228 *pR0Ptr = NIL_RTR0PTR;
1229 AssertPtrNullReturn(pHCPhys, NULL);
1230 AssertMsgReturn(cPages > 0 && cPages < 256, ("cPages=%d must be > 0 and < 256\n", cPages), NULL);
1231
1232 /* fake */
1233 if (RT_UNLIKELY(g_u32FakeMode))
1234 {
1235 void *pv = RTMemPageAllocZ(cPages * PAGE_SIZE);
1236 if (pR0Ptr)
1237 *pR0Ptr = (RTR0PTR)pv;
1238 if (pHCPhys)
1239 *pHCPhys = (uintptr_t)pv + (PAGE_SHIFT * 1024);
1240 return pv;
1241 }
1242
1243 /*
1244 * Issue IOCtl to the SUPDRV kernel module.
1245 */
1246 SUPCONTALLOC Req;
1247 Req.Hdr.u32Cookie = g_u32Cookie;
1248 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1249 Req.Hdr.cbIn = SUP_IOCTL_CONT_ALLOC_SIZE_IN;
1250 Req.Hdr.cbOut = SUP_IOCTL_CONT_ALLOC_SIZE_OUT;
1251 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1252 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1253 Req.u.In.cPages = (uint32_t)cPages;
1254 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CONT_ALLOC, &Req, SUP_IOCTL_CONT_ALLOC_SIZE);
1255 if ( RT_SUCCESS(rc)
1256 && RT_SUCCESS(Req.Hdr.rc))
1257 {
1258 *pHCPhys = Req.u.Out.HCPhys;
1259 if (pR0Ptr)
1260 *pR0Ptr = Req.u.Out.pvR0;
1261#ifdef RT_OS_DARWIN /* HACK ALERT! */
1262 supR3TouchPages(Req.u.Out.pvR3, cPages);
1263#endif
1264 return Req.u.Out.pvR3;
1265 }
1266
1267 return NULL;
1268}
1269
1270
1271SUPR3DECL(int) SUPR3ContFree(void *pv, size_t cPages)
1272{
1273 /*
1274 * Validate.
1275 */
1276 if (!pv)
1277 return VINF_SUCCESS;
1278 AssertPtrReturn(pv, VERR_INVALID_POINTER);
1279 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1280
1281 /* fake */
1282 if (RT_UNLIKELY(g_u32FakeMode))
1283 {
1284 RTMemPageFree(pv);
1285 return VINF_SUCCESS;
1286 }
1287
1288 /*
1289 * Issue IOCtl to the SUPDRV kernel module.
1290 */
1291 SUPCONTFREE Req;
1292 Req.Hdr.u32Cookie = g_u32Cookie;
1293 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1294 Req.Hdr.cbIn = SUP_IOCTL_CONT_FREE_SIZE_IN;
1295 Req.Hdr.cbOut = SUP_IOCTL_CONT_FREE_SIZE_OUT;
1296 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1297 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1298 Req.u.In.pvR3 = pv;
1299 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CONT_FREE, &Req, SUP_IOCTL_CONT_FREE_SIZE);
1300 if (RT_SUCCESS(rc))
1301 rc = Req.Hdr.rc;
1302 return rc;
1303}
1304
1305
1306SUPR3DECL(int) SUPR3LowAlloc(size_t cPages, void **ppvPages, PRTR0PTR ppvPagesR0, PSUPPAGE paPages)
1307{
1308 /*
1309 * Validate.
1310 */
1311 AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
1312 *ppvPages = NULL;
1313 AssertPtrReturn(paPages, VERR_INVALID_POINTER);
1314 AssertMsgReturn(cPages > 0 && cPages < 256, ("cPages=%d must be > 0 and < 256\n", cPages), VERR_PAGE_COUNT_OUT_OF_RANGE);
1315
1316 /* fake */
1317 if (RT_UNLIKELY(g_u32FakeMode))
1318 {
1319 *ppvPages = RTMemPageAllocZ((size_t)cPages * PAGE_SIZE);
1320 if (!*ppvPages)
1321 return VERR_NO_LOW_MEMORY;
1322
1323 /* fake physical addresses. */
1324 RTHCPHYS Phys = (uintptr_t)*ppvPages + PAGE_SIZE * 1024;
1325 size_t iPage = cPages;
1326 while (iPage-- > 0)
1327 paPages[iPage].Phys = Phys + (iPage << PAGE_SHIFT);
1328 return VINF_SUCCESS;
1329 }
1330
1331 /*
1332 * Issue IOCtl to the SUPDRV kernel module.
1333 */
1334 int rc;
1335 PSUPLOWALLOC pReq = (PSUPLOWALLOC)RTMemTmpAllocZ(SUP_IOCTL_LOW_ALLOC_SIZE(cPages));
1336 if (pReq)
1337 {
1338 pReq->Hdr.u32Cookie = g_u32Cookie;
1339 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
1340 pReq->Hdr.cbIn = SUP_IOCTL_LOW_ALLOC_SIZE_IN;
1341 pReq->Hdr.cbOut = SUP_IOCTL_LOW_ALLOC_SIZE_OUT(cPages);
1342 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
1343 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
1344 pReq->u.In.cPages = (uint32_t)cPages; AssertRelease(pReq->u.In.cPages == cPages);
1345 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LOW_ALLOC, pReq, SUP_IOCTL_LOW_ALLOC_SIZE(cPages));
1346 if (RT_SUCCESS(rc))
1347 rc = pReq->Hdr.rc;
1348 if (RT_SUCCESS(rc))
1349 {
1350 *ppvPages = pReq->u.Out.pvR3;
1351 if (ppvPagesR0)
1352 *ppvPagesR0 = pReq->u.Out.pvR0;
1353 if (paPages)
1354 for (size_t iPage = 0; iPage < cPages; iPage++)
1355 {
1356 paPages[iPage].uReserved = 0;
1357 paPages[iPage].Phys = pReq->u.Out.aPages[iPage];
1358 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
1359 Assert(paPages[iPage].Phys <= UINT32_C(0xfffff000));
1360 }
1361#ifdef RT_OS_DARWIN /* HACK ALERT! */
1362 supR3TouchPages(pReq->u.Out.pvR3, cPages);
1363#endif
1364 }
1365 RTMemTmpFree(pReq);
1366 }
1367 else
1368 rc = VERR_NO_TMP_MEMORY;
1369
1370 return rc;
1371}
1372
1373
1374SUPR3DECL(int) SUPR3LowFree(void *pv, size_t cPages)
1375{
1376 /*
1377 * Validate.
1378 */
1379 if (!pv)
1380 return VINF_SUCCESS;
1381 AssertPtrReturn(pv, VERR_INVALID_POINTER);
1382 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1383
1384 /* fake */
1385 if (RT_UNLIKELY(g_u32FakeMode))
1386 {
1387 RTMemPageFree(pv);
1388 return VINF_SUCCESS;
1389 }
1390
1391 /*
1392 * Issue IOCtl to the SUPDRV kernel module.
1393 */
1394 SUPCONTFREE Req;
1395 Req.Hdr.u32Cookie = g_u32Cookie;
1396 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1397 Req.Hdr.cbIn = SUP_IOCTL_LOW_FREE_SIZE_IN;
1398 Req.Hdr.cbOut = SUP_IOCTL_LOW_FREE_SIZE_OUT;
1399 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1400 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1401 Req.u.In.pvR3 = pv;
1402 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LOW_FREE, &Req, SUP_IOCTL_LOW_FREE_SIZE);
1403 if (RT_SUCCESS(rc))
1404 rc = Req.Hdr.rc;
1405 return rc;
1406}
1407
1408
1409SUPR3DECL(int) SUPR3HardenedVerifyFile(const char *pszFilename, const char *pszMsg, PRTFILE phFile)
1410{
1411 /*
1412 * Quick input validation.
1413 */
1414 AssertPtr(pszFilename);
1415 AssertPtr(pszMsg);
1416 AssertReturn(!phFile, VERR_NOT_IMPLEMENTED); /** @todo Implement this. The deal is that we make sure the
1417 file is the same we verified after opening it. */
1418
1419 /*
1420 * Only do the actual check in hardened builds.
1421 */
1422#ifdef VBOX_WITH_HARDENING
1423 int rc = supR3HardenedVerifyFile(pszFilename, false /* fFatal */);
1424 if (RT_FAILURE(rc))
1425 LogRel(("SUPR3HardenedVerifyFile: %s: Verification of \"%s\" failed, rc=%Rrc\n", pszMsg, pszFilename, rc));
1426 return rc;
1427#else
1428 return VINF_SUCCESS;
1429#endif
1430}
1431
1432
1433SUPR3DECL(int) SUPR3LoadModule(const char *pszFilename, const char *pszModule, void **ppvImageBase)
1434{
1435 int rc = VINF_SUCCESS;
1436#ifdef VBOX_WITH_HARDENING
1437 /*
1438 * Check that the module can be trusted.
1439 */
1440 rc = supR3HardenedVerifyFile(pszFilename, false /* fFatal */);
1441#endif
1442 if (RT_SUCCESS(rc))
1443 rc = supLoadModule(pszFilename, pszModule, NULL, ppvImageBase);
1444 else
1445 LogRel(("SUPR3LoadModule: Verification of \"%s\" failed, rc=%Rrc\n", rc));
1446 return rc;
1447}
1448
1449
1450SUPR3DECL(int) SUPR3LoadServiceModule(const char *pszFilename, const char *pszModule,
1451 const char *pszSrvReqHandler, void **ppvImageBase)
1452{
1453 int rc = VINF_SUCCESS;
1454 AssertPtrReturn(pszSrvReqHandler, VERR_INVALID_PARAMETER);
1455
1456#ifdef VBOX_WITH_HARDENING
1457 /*
1458 * Check that the module can be trusted.
1459 */
1460 rc = supR3HardenedVerifyFile(pszFilename, false /* fFatal */);
1461#endif
1462 if (RT_SUCCESS(rc))
1463 rc = supLoadModule(pszFilename, pszModule, pszSrvReqHandler, ppvImageBase);
1464 else
1465 LogRel(("SUPR3LoadServiceModule: Verification of \"%s\" failed, rc=%Rrc\n", rc));
1466 return rc;
1467}
1468
1469
1470/**
1471 * Resolve an external symbol during RTLdrGetBits().
1472 *
1473 * @returns VBox status code.
1474 * @param hLdrMod The loader module handle.
1475 * @param pszModule Module name.
1476 * @param pszSymbol Symbol name, NULL if uSymbol should be used.
1477 * @param uSymbol Symbol ordinal, ~0 if pszSymbol should be used.
1478 * @param pValue Where to store the symbol value (address).
1479 * @param pvUser User argument.
1480 */
1481static DECLCALLBACK(int) supLoadModuleResolveImport(RTLDRMOD hLdrMod, const char *pszModule,
1482 const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser)
1483{
1484 AssertPtr(pValue);
1485 AssertPtr(pvUser);
1486
1487 /*
1488 * Only SUPR0 and VMMR0.r0
1489 */
1490 if ( pszModule
1491 && *pszModule
1492 && strcmp(pszModule, "VBoxDrv.sys")
1493 && strcmp(pszModule, "VMMR0.r0"))
1494 {
1495 AssertMsgFailed(("%s is importing from %s! (expected 'SUPR0.dll' or 'VMMR0.r0', case-sensitiv)\n", pvUser, pszModule));
1496 return VERR_SYMBOL_NOT_FOUND;
1497 }
1498
1499 /*
1500 * No ordinals.
1501 */
1502 if (pszSymbol < (const char*)0x10000)
1503 {
1504 AssertMsgFailed(("%s is importing by ordinal (ord=%d)\n", pvUser, (int)(uintptr_t)pszSymbol));
1505 return VERR_SYMBOL_NOT_FOUND;
1506 }
1507
1508 /*
1509 * Lookup symbol.
1510 */
1511 /** @todo is this actually used??? */
1512 /* skip the 64-bit ELF import prefix first. */
1513 if (!strncmp(pszSymbol, "SUPR0$", sizeof("SUPR0$") - 1))
1514 pszSymbol += sizeof("SUPR0$") - 1;
1515
1516 /*
1517 * Check the VMMR0.r0 module if loaded.
1518 */
1519 /** @todo call the SUPR3LoadModule caller.... */
1520 /** @todo proper reference counting and such. */
1521 if (g_pvVMMR0 != NIL_RTR0PTR)
1522 {
1523 void *pvValue;
1524 if (!SUPR3GetSymbolR0((void *)g_pvVMMR0, pszSymbol, &pvValue))
1525 {
1526 *pValue = (uintptr_t)pvValue;
1527 return VINF_SUCCESS;
1528 }
1529 }
1530
1531 /* iterate the function table. */
1532 int c = g_pFunctions->u.Out.cFunctions;
1533 PSUPFUNC pFunc = &g_pFunctions->u.Out.aFunctions[0];
1534 while (c-- > 0)
1535 {
1536 if (!strcmp(pFunc->szName, pszSymbol))
1537 {
1538 *pValue = (uintptr_t)pFunc->pfn;
1539 return VINF_SUCCESS;
1540 }
1541 pFunc++;
1542 }
1543
1544 /*
1545 * The GIP.
1546 */
1547 if ( pszSymbol
1548 && g_pSUPGlobalInfoPage
1549 && g_pSUPGlobalInfoPageR0
1550 && !strcmp(pszSymbol, "g_SUPGlobalInfoPage")
1551 )
1552 {
1553 *pValue = (uintptr_t)g_pSUPGlobalInfoPageR0;
1554 return VINF_SUCCESS;
1555 }
1556
1557 /*
1558 * Despair.
1559 */
1560 c = g_pFunctions->u.Out.cFunctions;
1561 pFunc = &g_pFunctions->u.Out.aFunctions[0];
1562 while (c-- > 0)
1563 {
1564 RTAssertMsg2Weak("%d: %s\n", g_pFunctions->u.Out.cFunctions - c, pFunc->szName);
1565 pFunc++;
1566 }
1567
1568 RTAssertMsg2Weak("%s is importing %s which we couldn't find\n", pvUser, pszSymbol);
1569 AssertMsgFailed(("%s is importing %s which we couldn't find\n", pvUser, pszSymbol));
1570 if (g_u32FakeMode)
1571 {
1572 *pValue = 0xdeadbeef;
1573 return VINF_SUCCESS;
1574 }
1575 return VERR_SYMBOL_NOT_FOUND;
1576}
1577
1578
1579/** Argument package for supLoadModuleCalcSizeCB. */
1580typedef struct SUPLDRCALCSIZEARGS
1581{
1582 size_t cbStrings;
1583 uint32_t cSymbols;
1584 size_t cbImage;
1585} SUPLDRCALCSIZEARGS, *PSUPLDRCALCSIZEARGS;
1586
1587/**
1588 * Callback used to calculate the image size.
1589 * @return VINF_SUCCESS
1590 */
1591static DECLCALLBACK(int) supLoadModuleCalcSizeCB(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
1592{
1593 PSUPLDRCALCSIZEARGS pArgs = (PSUPLDRCALCSIZEARGS)pvUser;
1594 if ( pszSymbol != NULL
1595 && *pszSymbol
1596 && Value <= pArgs->cbImage)
1597 {
1598 pArgs->cSymbols++;
1599 pArgs->cbStrings += strlen(pszSymbol) + 1;
1600 }
1601 return VINF_SUCCESS;
1602}
1603
1604
1605/** Argument package for supLoadModuleCreateTabsCB. */
1606typedef struct SUPLDRCREATETABSARGS
1607{
1608 size_t cbImage;
1609 PSUPLDRSYM pSym;
1610 char *pszBase;
1611 char *psz;
1612} SUPLDRCREATETABSARGS, *PSUPLDRCREATETABSARGS;
1613
1614/**
1615 * Callback used to calculate the image size.
1616 * @return VINF_SUCCESS
1617 */
1618static DECLCALLBACK(int) supLoadModuleCreateTabsCB(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
1619{
1620 PSUPLDRCREATETABSARGS pArgs = (PSUPLDRCREATETABSARGS)pvUser;
1621 if ( pszSymbol != NULL
1622 && *pszSymbol
1623 && Value <= pArgs->cbImage)
1624 {
1625 pArgs->pSym->offSymbol = (uint32_t)Value;
1626 pArgs->pSym->offName = pArgs->psz - pArgs->pszBase;
1627 pArgs->pSym++;
1628
1629 size_t cbCopy = strlen(pszSymbol) + 1;
1630 memcpy(pArgs->psz, pszSymbol, cbCopy);
1631 pArgs->psz += cbCopy;
1632 }
1633 return VINF_SUCCESS;
1634}
1635
1636
1637/**
1638 * Worker for SUPR3LoadModule().
1639 *
1640 * @returns VBox status code.
1641 * @param pszFilename Name of the VMMR0 image file
1642 */
1643static int supLoadModule(const char *pszFilename, const char *pszModule, const char *pszSrvReqHandler, void **ppvImageBase)
1644{
1645 int rc;
1646
1647 /*
1648 * Validate input.
1649 */
1650 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
1651 AssertPtrReturn(pszModule, VERR_INVALID_PARAMETER);
1652 AssertPtrReturn(ppvImageBase, VERR_INVALID_PARAMETER);
1653 AssertReturn(strlen(pszModule) < RT_SIZEOFMEMB(SUPLDROPEN, u.In.szName), VERR_FILENAME_TOO_LONG);
1654 char szAbsFilename[RT_SIZEOFMEMB(SUPLDROPEN, u.In.szFilename)];
1655 rc = RTPathAbs(pszFilename, szAbsFilename, sizeof(szAbsFilename));
1656 if (RT_FAILURE(rc))
1657 return rc;
1658 pszFilename = szAbsFilename;
1659
1660 const bool fIsVMMR0 = !strcmp(pszModule, "VMMR0.r0");
1661 AssertReturn(!pszSrvReqHandler || !fIsVMMR0, VERR_INTERNAL_ERROR);
1662 *ppvImageBase = NULL;
1663
1664 /*
1665 * Open image file and figure its size.
1666 */
1667 RTLDRMOD hLdrMod;
1668 rc = RTLdrOpen(pszFilename, 0, RTLDRARCH_HOST, &hLdrMod);
1669 if (!RT_SUCCESS(rc))
1670 return rc;
1671
1672 SUPLDRCALCSIZEARGS CalcArgs;
1673 CalcArgs.cbStrings = 0;
1674 CalcArgs.cSymbols = 0;
1675 CalcArgs.cbImage = RTLdrSize(hLdrMod);
1676 rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCalcSizeCB, &CalcArgs);
1677 if (RT_SUCCESS(rc))
1678 {
1679 const uint32_t offSymTab = RT_ALIGN_32(CalcArgs.cbImage, 8);
1680 const uint32_t offStrTab = offSymTab + CalcArgs.cSymbols * sizeof(SUPLDRSYM);
1681 const uint32_t cbImageWithTabs = RT_ALIGN_32(offStrTab + CalcArgs.cbStrings, 8);
1682
1683 /*
1684 * Open the R0 image.
1685 */
1686 SUPLDROPEN OpenReq;
1687 OpenReq.Hdr.u32Cookie = g_u32Cookie;
1688 OpenReq.Hdr.u32SessionCookie = g_u32SessionCookie;
1689 OpenReq.Hdr.cbIn = SUP_IOCTL_LDR_OPEN_SIZE_IN;
1690 OpenReq.Hdr.cbOut = SUP_IOCTL_LDR_OPEN_SIZE_OUT;
1691 OpenReq.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1692 OpenReq.Hdr.rc = VERR_INTERNAL_ERROR;
1693 OpenReq.u.In.cbImageWithTabs = cbImageWithTabs;
1694 OpenReq.u.In.cbImageBits = (uint32_t)CalcArgs.cbImage;
1695 strcpy(OpenReq.u.In.szName, pszModule);
1696 strcpy(OpenReq.u.In.szFilename, pszFilename);
1697 if (!g_u32FakeMode)
1698 {
1699 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_OPEN, &OpenReq, SUP_IOCTL_LDR_OPEN_SIZE);
1700 if (RT_SUCCESS(rc))
1701 rc = OpenReq.Hdr.rc;
1702 }
1703 else
1704 {
1705 OpenReq.u.Out.fNeedsLoading = true;
1706 OpenReq.u.Out.pvImageBase = 0xef423420;
1707 }
1708 *ppvImageBase = (void *)OpenReq.u.Out.pvImageBase;
1709 if ( RT_SUCCESS(rc)
1710 && OpenReq.u.Out.fNeedsLoading)
1711 {
1712 /*
1713 * We need to load it.
1714 * Allocate memory for the image bits.
1715 */
1716 PSUPLDRLOAD pLoadReq = (PSUPLDRLOAD)RTMemTmpAlloc(SUP_IOCTL_LDR_LOAD_SIZE(cbImageWithTabs));
1717 if (pLoadReq)
1718 {
1719 /*
1720 * Get the image bits.
1721 */
1722 rc = RTLdrGetBits(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase,
1723 supLoadModuleResolveImport, (void *)pszModule);
1724
1725 if (RT_SUCCESS(rc))
1726 {
1727 /*
1728 * Get the entry points.
1729 */
1730 RTUINTPTR VMMR0EntryInt = 0;
1731 RTUINTPTR VMMR0EntryFast = 0;
1732 RTUINTPTR VMMR0EntryEx = 0;
1733 RTUINTPTR SrvReqHandler = 0;
1734 RTUINTPTR ModuleInit = 0;
1735 RTUINTPTR ModuleTerm = 0;
1736 if (fIsVMMR0)
1737 {
1738 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "VMMR0EntryInt", &VMMR0EntryInt);
1739 if (RT_SUCCESS(rc))
1740 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "VMMR0EntryFast", &VMMR0EntryFast);
1741 if (RT_SUCCESS(rc))
1742 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "VMMR0EntryEx", &VMMR0EntryEx);
1743 }
1744 else if (pszSrvReqHandler)
1745 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, pszSrvReqHandler, &SrvReqHandler);
1746 if (RT_SUCCESS(rc))
1747 {
1748 int rc2 = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "ModuleInit", &ModuleInit);
1749 if (RT_FAILURE(rc2))
1750 ModuleInit = 0;
1751
1752 rc2 = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "ModuleTerm", &ModuleTerm);
1753 if (RT_FAILURE(rc2))
1754 ModuleTerm = 0;
1755 }
1756 if (RT_SUCCESS(rc))
1757 {
1758 /*
1759 * Create the symbol and string tables.
1760 */
1761 SUPLDRCREATETABSARGS CreateArgs;
1762 CreateArgs.cbImage = CalcArgs.cbImage;
1763 CreateArgs.pSym = (PSUPLDRSYM)&pLoadReq->u.In.abImage[offSymTab];
1764 CreateArgs.pszBase = (char *)&pLoadReq->u.In.abImage[offStrTab];
1765 CreateArgs.psz = CreateArgs.pszBase;
1766 rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCreateTabsCB, &CreateArgs);
1767 if (RT_SUCCESS(rc))
1768 {
1769 AssertRelease((size_t)(CreateArgs.psz - CreateArgs.pszBase) <= CalcArgs.cbStrings);
1770 AssertRelease((size_t)(CreateArgs.pSym - (PSUPLDRSYM)&pLoadReq->u.In.abImage[offSymTab]) <= CalcArgs.cSymbols);
1771
1772 /*
1773 * Upload the image.
1774 */
1775 pLoadReq->Hdr.u32Cookie = g_u32Cookie;
1776 pLoadReq->Hdr.u32SessionCookie = g_u32SessionCookie;
1777 pLoadReq->Hdr.cbIn = SUP_IOCTL_LDR_LOAD_SIZE_IN(cbImageWithTabs);
1778 pLoadReq->Hdr.cbOut = SUP_IOCTL_LDR_LOAD_SIZE_OUT;
1779 pLoadReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_IN;
1780 pLoadReq->Hdr.rc = VERR_INTERNAL_ERROR;
1781
1782 pLoadReq->u.In.pfnModuleInit = (RTR0PTR)ModuleInit;
1783 pLoadReq->u.In.pfnModuleTerm = (RTR0PTR)ModuleTerm;
1784 if (fIsVMMR0)
1785 {
1786 pLoadReq->u.In.eEPType = SUPLDRLOADEP_VMMR0;
1787 pLoadReq->u.In.EP.VMMR0.pvVMMR0 = OpenReq.u.Out.pvImageBase;
1788 pLoadReq->u.In.EP.VMMR0.pvVMMR0EntryInt = (RTR0PTR)VMMR0EntryInt;
1789 pLoadReq->u.In.EP.VMMR0.pvVMMR0EntryFast= (RTR0PTR)VMMR0EntryFast;
1790 pLoadReq->u.In.EP.VMMR0.pvVMMR0EntryEx = (RTR0PTR)VMMR0EntryEx;
1791 }
1792 else if (pszSrvReqHandler)
1793 {
1794 pLoadReq->u.In.eEPType = SUPLDRLOADEP_SERVICE;
1795 pLoadReq->u.In.EP.Service.pfnServiceReq = (RTR0PTR)SrvReqHandler;
1796 pLoadReq->u.In.EP.Service.apvReserved[0] = NIL_RTR0PTR;
1797 pLoadReq->u.In.EP.Service.apvReserved[1] = NIL_RTR0PTR;
1798 pLoadReq->u.In.EP.Service.apvReserved[2] = NIL_RTR0PTR;
1799 }
1800 else
1801 pLoadReq->u.In.eEPType = SUPLDRLOADEP_NOTHING;
1802 pLoadReq->u.In.offStrTab = offStrTab;
1803 pLoadReq->u.In.cbStrTab = (uint32_t)CalcArgs.cbStrings;
1804 AssertRelease(pLoadReq->u.In.cbStrTab == CalcArgs.cbStrings);
1805 pLoadReq->u.In.cbImageBits = (uint32_t)CalcArgs.cbImage;
1806 pLoadReq->u.In.offSymbols = offSymTab;
1807 pLoadReq->u.In.cSymbols = CalcArgs.cSymbols;
1808 pLoadReq->u.In.cbImageWithTabs = cbImageWithTabs;
1809 pLoadReq->u.In.pvImageBase = OpenReq.u.Out.pvImageBase;
1810 if (!g_u32FakeMode)
1811 {
1812 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_LOAD, pLoadReq, SUP_IOCTL_LDR_LOAD_SIZE(cbImageWithTabs));
1813 if (RT_SUCCESS(rc))
1814 rc = pLoadReq->Hdr.rc;
1815 }
1816 else
1817 rc = VINF_SUCCESS;
1818 if ( RT_SUCCESS(rc)
1819 || rc == VERR_ALREADY_LOADED /* A competing process. */
1820 )
1821 {
1822 LogRel(("SUP: Loaded %s (%s) at %#p - ModuleInit at %RTptr and ModuleTerm at %RTptr%s\n",
1823 pszModule, pszFilename, OpenReq.u.Out.pvImageBase, ModuleInit, ModuleTerm,
1824 OpenReq.u.Out.fNativeLoader ? " using the native ring-0 loader" : ""));
1825 if (fIsVMMR0)
1826 {
1827 g_pvVMMR0 = OpenReq.u.Out.pvImageBase;
1828 LogRel(("SUP: VMMR0EntryEx located at %RTptr, VMMR0EntryFast at %RTptr and VMMR0EntryInt at %RTptr\n",
1829 VMMR0EntryEx, VMMR0EntryFast, VMMR0EntryInt));
1830 }
1831#ifdef RT_OS_WINDOWS
1832 LogRel(("SUP: windbg> .reload /f %s=%#p\n", pszFilename, OpenReq.u.Out.pvImageBase));
1833#endif
1834
1835 RTMemTmpFree(pLoadReq);
1836 RTLdrClose(hLdrMod);
1837 return VINF_SUCCESS;
1838 }
1839 }
1840 }
1841 }
1842 RTMemTmpFree(pLoadReq);
1843 }
1844 else
1845 {
1846 AssertMsgFailed(("failed to allocated %u bytes for SUPLDRLOAD_IN structure!\n", SUP_IOCTL_LDR_LOAD_SIZE(cbImageWithTabs)));
1847 rc = VERR_NO_TMP_MEMORY;
1848 }
1849 }
1850 else if (RT_SUCCESS(rc))
1851 {
1852 if (fIsVMMR0)
1853 g_pvVMMR0 = OpenReq.u.Out.pvImageBase;
1854 LogRel(("SUP: Opened %s (%s) at %#p.\n", pszModule, pszFilename, OpenReq.u.Out.pvImageBase,
1855 OpenReq.u.Out.fNativeLoader ? " loaded by the native ring-0 loader" : ""));
1856#ifdef RT_OS_WINDOWS
1857 LogRel(("SUP: windbg> .reload /f %s=%#p\n", pszFilename, OpenReq.u.Out.pvImageBase));
1858#endif
1859 }
1860 }
1861 RTLdrClose(hLdrMod);
1862 return rc;
1863}
1864
1865
1866SUPR3DECL(int) SUPR3FreeModule(void *pvImageBase)
1867{
1868 /* fake */
1869 if (RT_UNLIKELY(g_u32FakeMode))
1870 {
1871 g_pvVMMR0 = NIL_RTR0PTR;
1872 return VINF_SUCCESS;
1873 }
1874
1875 /*
1876 * Free the requested module.
1877 */
1878 SUPLDRFREE Req;
1879 Req.Hdr.u32Cookie = g_u32Cookie;
1880 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1881 Req.Hdr.cbIn = SUP_IOCTL_LDR_FREE_SIZE_IN;
1882 Req.Hdr.cbOut = SUP_IOCTL_LDR_FREE_SIZE_OUT;
1883 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1884 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1885 Req.u.In.pvImageBase = (RTR0PTR)pvImageBase;
1886 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_FREE, &Req, SUP_IOCTL_LDR_FREE_SIZE);
1887 if (RT_SUCCESS(rc))
1888 rc = Req.Hdr.rc;
1889 if ( RT_SUCCESS(rc)
1890 && (RTR0PTR)pvImageBase == g_pvVMMR0)
1891 g_pvVMMR0 = NIL_RTR0PTR;
1892 return rc;
1893}
1894
1895
1896SUPR3DECL(int) SUPR3GetSymbolR0(void *pvImageBase, const char *pszSymbol, void **ppvValue)
1897{
1898 *ppvValue = NULL;
1899
1900 /* fake */
1901 if (RT_UNLIKELY(g_u32FakeMode))
1902 {
1903 *ppvValue = (void *)(uintptr_t)0xdeadf00d;
1904 return VINF_SUCCESS;
1905 }
1906
1907 /*
1908 * Do ioctl.
1909 */
1910 SUPLDRGETSYMBOL Req;
1911 Req.Hdr.u32Cookie = g_u32Cookie;
1912 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1913 Req.Hdr.cbIn = SUP_IOCTL_LDR_GET_SYMBOL_SIZE_IN;
1914 Req.Hdr.cbOut = SUP_IOCTL_LDR_GET_SYMBOL_SIZE_OUT;
1915 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1916 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1917 Req.u.In.pvImageBase = (RTR0PTR)pvImageBase;
1918 size_t cchSymbol = strlen(pszSymbol);
1919 if (cchSymbol >= sizeof(Req.u.In.szSymbol))
1920 return VERR_SYMBOL_NOT_FOUND;
1921 memcpy(Req.u.In.szSymbol, pszSymbol, cchSymbol + 1);
1922 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_GET_SYMBOL, &Req, SUP_IOCTL_LDR_GET_SYMBOL_SIZE);
1923 if (RT_SUCCESS(rc))
1924 rc = Req.Hdr.rc;
1925 if (RT_SUCCESS(rc))
1926 *ppvValue = (void *)Req.u.Out.pvSymbol;
1927 return rc;
1928}
1929
1930
1931SUPR3DECL(int) SUPR3LoadVMM(const char *pszFilename)
1932{
1933 void *pvImageBase;
1934 return SUPR3LoadModule(pszFilename, "VMMR0.r0", &pvImageBase);
1935}
1936
1937
1938SUPR3DECL(int) SUPR3UnloadVMM(void)
1939{
1940 return SUPR3FreeModule((void*)g_pvVMMR0);
1941}
1942
1943
1944SUPR3DECL(int) SUPR3GipGetPhys(PRTHCPHYS pHCPhys)
1945{
1946 if (g_pSUPGlobalInfoPage)
1947 {
1948 *pHCPhys = g_HCPhysSUPGlobalInfoPage;
1949 return VINF_SUCCESS;
1950 }
1951 *pHCPhys = NIL_RTHCPHYS;
1952 return VERR_WRONG_ORDER;
1953}
1954
1955
1956/**
1957 * Worker for SUPR3HardenedLdrLoad and SUPR3HardenedLdrLoadAppPriv.
1958 *
1959 * @returns iprt status code.
1960 * @param pszFilename The full file name.
1961 * @param phLdrMod Where to store the handle to the loaded module.
1962 */
1963static int supR3HardenedLdrLoadIt(const char *pszFilename, PRTLDRMOD phLdrMod)
1964{
1965#ifdef VBOX_WITH_HARDENING
1966 /*
1967 * Verify the image file.
1968 */
1969 int rc = supR3HardenedVerifyFile(pszFilename, false /* fFatal */);
1970 if (RT_FAILURE(rc))
1971 {
1972 LogRel(("supR3HardenedLdrLoadIt: Verification of \"%s\" failed, rc=%Rrc\n", pszFilename, rc));
1973 return rc;
1974 }
1975#endif
1976
1977 /*
1978 * Try load it.
1979 */
1980 return RTLdrLoad(pszFilename, phLdrMod);
1981}
1982
1983
1984SUPR3DECL(int) SUPR3HardenedLdrLoad(const char *pszFilename, PRTLDRMOD phLdrMod)
1985{
1986 /*
1987 * Validate input.
1988 */
1989 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
1990 AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
1991 *phLdrMod = NIL_RTLDRMOD;
1992 AssertReturn(RTPathHavePath(pszFilename), VERR_INVALID_PARAMETER);
1993
1994 /*
1995 * Add the default extension if it's missing.
1996 */
1997 if (!RTPathHaveExt(pszFilename))
1998 {
1999 const char *pszSuff = RTLdrGetSuff();
2000 size_t cchSuff = strlen(pszSuff);
2001 size_t cchFilename = strlen(pszFilename);
2002 char *psz = (char *)alloca(cchFilename + cchSuff + 1);
2003 AssertReturn(psz, VERR_NO_TMP_MEMORY);
2004 memcpy(psz, pszFilename, cchFilename);
2005 memcpy(psz + cchFilename, pszSuff, cchSuff + 1);
2006 pszFilename = psz;
2007 }
2008
2009 /*
2010 * Pass it on to the common library loader.
2011 */
2012 return supR3HardenedLdrLoadIt(pszFilename, phLdrMod);
2013}
2014
2015
2016SUPR3DECL(int) SUPR3HardenedLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod)
2017{
2018 LogFlow(("SUPR3HardenedLdrLoadAppPriv: pszFilename=%p:{%s} phLdrMod=%p\n", pszFilename, pszFilename, phLdrMod));
2019
2020 /*
2021 * Validate input.
2022 */
2023 AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
2024 *phLdrMod = NIL_RTLDRMOD;
2025 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
2026 AssertMsgReturn(!RTPathHavePath(pszFilename), ("%s\n", pszFilename), VERR_INVALID_PARAMETER);
2027
2028 /*
2029 * Check the filename.
2030 */
2031 size_t cchFilename = strlen(pszFilename);
2032 AssertMsgReturn(cchFilename < (RTPATH_MAX / 4) * 3, ("%zu\n", cchFilename), VERR_INVALID_PARAMETER);
2033
2034 const char *pszExt = "";
2035 size_t cchExt = 0;
2036 if (!RTPathHaveExt(pszFilename))
2037 {
2038 pszExt = RTLdrGetSuff();
2039 cchExt = strlen(pszExt);
2040 }
2041
2042 /*
2043 * Construct the private arch path and check if the file exists.
2044 */
2045 char szPath[RTPATH_MAX];
2046 int rc = RTPathAppPrivateArch(szPath, sizeof(szPath) - 1 - cchExt - cchFilename);
2047 AssertRCReturn(rc, rc);
2048
2049 char *psz = strchr(szPath, '\0');
2050 *psz++ = RTPATH_SLASH;
2051 memcpy(psz, pszFilename, cchFilename);
2052 psz += cchFilename;
2053 memcpy(psz, pszExt, cchExt + 1);
2054
2055 if (!RTPathExists(szPath))
2056 {
2057 LogRel(("SUPR3HardenedLdrLoadAppPriv: \"%s\" not found\n", szPath));
2058 return VERR_FILE_NOT_FOUND;
2059 }
2060
2061 /*
2062 * Pass it on to SUPR3HardenedLdrLoad.
2063 */
2064 rc = SUPR3HardenedLdrLoad(szPath, phLdrMod);
2065
2066 LogFlow(("SUPR3HardenedLdrLoadAppPriv: returns %Rrc\n", rc));
2067 return rc;
2068}
2069
2070
2071SUPR3DECL(int) SUPR3QueryVTxSupported(void)
2072{
2073#ifdef RT_OS_LINUX
2074 return suplibOsQueryVTxSupported();
2075#else
2076 return VINF_SUCCESS;
2077#endif
2078}
2079
2080
2081SUPR3DECL(int) SUPR3QueryVTCaps(uint32_t *pfCaps)
2082{
2083 AssertPtrReturn(pfCaps, VERR_INVALID_POINTER);
2084
2085 *pfCaps = 0;
2086
2087 /* fake */
2088 if (RT_UNLIKELY(g_u32FakeMode))
2089 return VINF_SUCCESS;
2090
2091 /*
2092 * Issue IOCtl to the SUPDRV kernel module.
2093 */
2094 SUPVTCAPS Req;
2095 Req.Hdr.u32Cookie = g_u32Cookie;
2096 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
2097 Req.Hdr.cbIn = SUP_IOCTL_VT_CAPS_SIZE_IN;
2098 Req.Hdr.cbOut = SUP_IOCTL_VT_CAPS_SIZE_OUT;
2099 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
2100 Req.Hdr.rc = VERR_INTERNAL_ERROR;
2101 Req.u.Out.Caps = 0;
2102 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_VT_CAPS, &Req, SUP_IOCTL_VT_CAPS_SIZE);
2103 if (RT_SUCCESS(rc))
2104 {
2105 rc = Req.Hdr.rc;
2106 if (RT_SUCCESS(rc))
2107 *pfCaps = Req.u.Out.Caps;
2108 }
2109 return rc;
2110}
2111
Note: See TracBrowser for help on using the repository browser.

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