VirtualBox

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

Last change on this file since 20554 was 20528, checked in by vboxsync, 16 years ago

SUP: SUPR0PageProtect & SUPR0PageProtect - for creating guard (hyper) heap/stack pages.

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