VirtualBox

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

Last change on this file since 16804 was 15841, checked in by vboxsync, 16 years ago

SUPDrv,SUPLib: SUPR0ObjRelease returns VINF_OBJECT_DESTROYED if it destroyed the object. Made SUPLib require this version of the driver (joined the RTMp API change earlier today).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 68.1 KB
Line 
1/* $Id: SUPLib.cpp 15841 2009-01-07 16:40:02Z 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. */
102static SUPLIBDATA 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) == 0x000a0000
263 ? 0x000a0009
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 g_u32Cookie = CookieReq.u.Out.u32Cookie;
291 g_u32SessionCookie = CookieReq.u.Out.u32SessionCookie;
292 g_pSession = CookieReq.u.Out.pSession;
293 g_pFunctions = pFuncsReq;
294 if (ppSession)
295 *ppSession = CookieReq.u.Out.pSession;
296
297 /*
298 * Map the GIP into userspace.
299 * This is an optional feature, so we will ignore any failures here.
300 */
301 if (!g_pSUPGlobalInfoPage)
302 {
303 SUPGIPMAP GipMapReq;
304 GipMapReq.Hdr.u32Cookie = g_u32Cookie;
305 GipMapReq.Hdr.u32SessionCookie = g_u32SessionCookie;
306 GipMapReq.Hdr.cbIn = SUP_IOCTL_GIP_MAP_SIZE_IN;
307 GipMapReq.Hdr.cbOut = SUP_IOCTL_GIP_MAP_SIZE_OUT;
308 GipMapReq.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
309 GipMapReq.Hdr.rc = VERR_INTERNAL_ERROR;
310 GipMapReq.u.Out.HCPhysGip = NIL_RTHCPHYS;
311 GipMapReq.u.Out.pGipR0 = NIL_RTR0PTR;
312 GipMapReq.u.Out.pGipR3 = NULL;
313 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_GIP_MAP, &GipMapReq, SUP_IOCTL_GIP_MAP_SIZE);
314 if (RT_SUCCESS(rc))
315 rc = GipMapReq.Hdr.rc;
316 if (RT_SUCCESS(rc))
317 {
318 AssertRelease(GipMapReq.u.Out.pGipR3->u32Magic == SUPGLOBALINFOPAGE_MAGIC);
319 AssertRelease(GipMapReq.u.Out.pGipR3->u32Version >= SUPGLOBALINFOPAGE_VERSION);
320 ASMAtomicXchgSize(&g_HCPhysSUPGlobalInfoPage, GipMapReq.u.Out.HCPhysGip);
321 ASMAtomicCmpXchgPtr((void * volatile *)&g_pSUPGlobalInfoPage, GipMapReq.u.Out.pGipR3, NULL);
322 ASMAtomicCmpXchgPtr((void * volatile *)&g_pSUPGlobalInfoPageR0, (void *)GipMapReq.u.Out.pGipR0, NULL);
323 }
324 }
325 return VINF_SUCCESS;
326 }
327
328 /* bailout */
329 RTMemFree(pFuncsReq);
330 }
331 else
332 rc = VERR_NO_MEMORY;
333 }
334 else
335 {
336 LogRel(("Support driver version mismatch: SessionVersion=%#x DriverVersion=%#x ClientVersion=%#x MinVersion=%#x\n",
337 CookieReq.u.Out.u32SessionVersion, CookieReq.u.Out.u32DriverVersion, SUPDRV_IOC_VERSION, MinVersion));
338 rc = VERR_VM_DRIVER_VERSION_MISMATCH;
339 }
340 }
341 else
342 {
343 if (RT_SUCCESS(rc))
344 {
345 rc = CookieReq.Hdr.rc;
346 LogRel(("Support driver version mismatch: DriverVersion=%#x ClientVersion=%#x rc=%Rrc\n",
347 CookieReq.u.Out.u32DriverVersion, SUPDRV_IOC_VERSION, rc));
348 if (rc != VERR_VM_DRIVER_VERSION_MISMATCH)
349 rc = VERR_VM_DRIVER_VERSION_MISMATCH;
350 }
351 else
352 {
353 /* for pre 0x00060000 drivers */
354 LogRel(("Support driver version mismatch: DriverVersion=too-old ClientVersion=%#x\n", SUPDRV_IOC_VERSION));
355 rc = VERR_VM_DRIVER_VERSION_MISMATCH;
356 }
357 }
358
359 suplibOsTerm(&g_supLibData);
360 }
361 AssertMsgFailed(("SUPR3Init() failed rc=%Rrc\n", rc));
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, unsigned 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, 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.uOperation = uOperation;
612 Req.u.In.u64Arg = u64Arg;
613 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_VMMR0(0), &Req, SUP_IOCTL_CALL_VMMR0_SIZE(0));
614 if (RT_SUCCESS(rc))
615 rc = Req.Hdr.rc;
616 }
617 else if (SUP_IOCTL_CALL_VMMR0_SIZE(pReqHdr->cbReq) < _4K) /* FreeBSD won't copy more than 4K. */
618 {
619 AssertPtrReturn(pReqHdr, VERR_INVALID_POINTER);
620 AssertReturn(pReqHdr->u32Magic == SUPVMMR0REQHDR_MAGIC, VERR_INVALID_MAGIC);
621 const size_t cbReq = pReqHdr->cbReq;
622
623 PSUPCALLVMMR0 pReq = (PSUPCALLVMMR0)alloca(SUP_IOCTL_CALL_VMMR0_SIZE(cbReq));
624 pReq->Hdr.u32Cookie = g_u32Cookie;
625 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
626 pReq->Hdr.cbIn = SUP_IOCTL_CALL_VMMR0_SIZE_IN(cbReq);
627 pReq->Hdr.cbOut = SUP_IOCTL_CALL_VMMR0_SIZE_OUT(cbReq);
628 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
629 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
630 pReq->u.In.pVMR0 = pVMR0;
631 pReq->u.In.uOperation = uOperation;
632 pReq->u.In.u64Arg = u64Arg;
633 memcpy(&pReq->abReqPkt[0], pReqHdr, cbReq);
634 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_VMMR0(cbReq), pReq, SUP_IOCTL_CALL_VMMR0_SIZE(cbReq));
635 if (RT_SUCCESS(rc))
636 rc = pReq->Hdr.rc;
637 memcpy(pReqHdr, &pReq->abReqPkt[0], cbReq);
638 }
639 else /** @todo may have to remove the size limits one this request... */
640 AssertMsgFailedReturn(("cbReq=%#x\n", pReqHdr->cbReq), VERR_INTERNAL_ERROR);
641 return rc;
642}
643
644
645SUPR3DECL(int) SUPCallVMMR0(PVMR0 pVMR0, unsigned uOperation, void *pvArg)
646{
647 /*
648 * The following operations don't belong here.
649 */
650 AssertMsgReturn( uOperation != SUP_VMMR0_DO_RAW_RUN
651 && uOperation != SUP_VMMR0_DO_HWACC_RUN
652 && uOperation != SUP_VMMR0_DO_NOP,
653 ("%#x\n", uOperation),
654 VERR_INTERNAL_ERROR);
655 return SUPCallVMMR0Ex(pVMR0, uOperation, (uintptr_t)pvArg, NULL);
656}
657
658
659SUPR3DECL(int) SUPSetVMForFastIOCtl(PVMR0 pVMR0)
660{
661 if (RT_UNLIKELY(g_u32FakeMode))
662 return VINF_SUCCESS;
663
664 SUPSETVMFORFAST Req;
665 Req.Hdr.u32Cookie = g_u32Cookie;
666 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
667 Req.Hdr.cbIn = SUP_IOCTL_SET_VM_FOR_FAST_SIZE_IN;
668 Req.Hdr.cbOut = SUP_IOCTL_SET_VM_FOR_FAST_SIZE_OUT;
669 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
670 Req.Hdr.rc = VERR_INTERNAL_ERROR;
671 Req.u.In.pVMR0 = pVMR0;
672 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_SET_VM_FOR_FAST, &Req, SUP_IOCTL_SET_VM_FOR_FAST_SIZE);
673 if (RT_SUCCESS(rc))
674 rc = Req.Hdr.rc;
675 return rc;
676}
677
678
679SUPR3DECL(int) SUPR3CallR0Service(const char *pszService, size_t cchService, uint32_t uOperation, uint64_t u64Arg, PSUPR0SERVICEREQHDR pReqHdr)
680{
681 AssertReturn(cchService < RT_SIZEOFMEMB(SUPCALLSERVICE, u.In.szName), VERR_INVALID_PARAMETER);
682 Assert(strlen(pszService) == cchService);
683
684 /* fake */
685 if (RT_UNLIKELY(g_u32FakeMode))
686 return VERR_NOT_SUPPORTED;
687
688 int rc;
689 if (!pReqHdr)
690 {
691 /* no data. */
692 SUPCALLSERVICE Req;
693 Req.Hdr.u32Cookie = g_u32Cookie;
694 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
695 Req.Hdr.cbIn = SUP_IOCTL_CALL_SERVICE_SIZE_IN(0);
696 Req.Hdr.cbOut = SUP_IOCTL_CALL_SERVICE_SIZE_OUT(0);
697 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
698 Req.Hdr.rc = VERR_INTERNAL_ERROR;
699 memcpy(Req.u.In.szName, pszService, cchService);
700 Req.u.In.szName[cchService] = '\0';
701 Req.u.In.uOperation = uOperation;
702 Req.u.In.u64Arg = u64Arg;
703 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_SERVICE(0), &Req, SUP_IOCTL_CALL_SERVICE_SIZE(0));
704 if (RT_SUCCESS(rc))
705 rc = Req.Hdr.rc;
706 }
707 else if (SUP_IOCTL_CALL_SERVICE_SIZE(pReqHdr->cbReq) < _4K) /* FreeBSD won't copy more than 4K. */
708 {
709 AssertPtrReturn(pReqHdr, VERR_INVALID_POINTER);
710 AssertReturn(pReqHdr->u32Magic == SUPR0SERVICEREQHDR_MAGIC, VERR_INVALID_MAGIC);
711 const size_t cbReq = pReqHdr->cbReq;
712
713 PSUPCALLSERVICE pReq = (PSUPCALLSERVICE)alloca(SUP_IOCTL_CALL_SERVICE_SIZE(cbReq));
714 pReq->Hdr.u32Cookie = g_u32Cookie;
715 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
716 pReq->Hdr.cbIn = SUP_IOCTL_CALL_SERVICE_SIZE_IN(cbReq);
717 pReq->Hdr.cbOut = SUP_IOCTL_CALL_SERVICE_SIZE_OUT(cbReq);
718 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
719 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
720 memcpy(pReq->u.In.szName, pszService, cchService);
721 pReq->u.In.szName[cchService] = '\0';
722 pReq->u.In.uOperation = uOperation;
723 pReq->u.In.u64Arg = u64Arg;
724 memcpy(&pReq->abReqPkt[0], pReqHdr, cbReq);
725 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CALL_SERVICE(cbReq), pReq, SUP_IOCTL_CALL_SERVICE_SIZE(cbReq));
726 if (RT_SUCCESS(rc))
727 rc = pReq->Hdr.rc;
728 memcpy(pReqHdr, &pReq->abReqPkt[0], cbReq);
729 }
730 else /** @todo may have to remove the size limits one this request... */
731 AssertMsgFailedReturn(("cbReq=%#x\n", pReqHdr->cbReq), VERR_INTERNAL_ERROR);
732 return rc;
733}
734
735
736SUPR3DECL(int) SUPPageAlloc(size_t cPages, void **ppvPages)
737{
738 /*
739 * Validate.
740 */
741 AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
742 *ppvPages = NULL;
743 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
744
745#ifdef RT_OS_WINDOWS
746 /*
747 * Temporary hack for windows until we've sorted out the
748 * locked memory that doesn't need to be accessible from kernel space.
749 */
750 return SUPPageAllocLockedEx(cPages, ppvPages, NULL);
751#else
752 /*
753 * Call OS specific worker.
754 */
755 return suplibOsPageAlloc(&g_supLibData, cPages, ppvPages);
756#endif
757}
758
759
760SUPR3DECL(int) SUPPageFree(void *pvPages, size_t cPages)
761{
762 /*
763 * Validate.
764 */
765 AssertPtrReturn(pvPages, VERR_INVALID_POINTER);
766 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
767
768#ifdef RT_OS_WINDOWS
769 /*
770 * Temporary hack for windows, see above.
771 */
772 return SUPPageFreeLocked(pvPages, cPages);
773#else
774 /*
775 * Call OS specific worker.
776 */
777 return suplibOsPageFree(&g_supLibData, pvPages, cPages);
778#endif
779}
780
781
782SUPR3DECL(int) SUPPageLock(void *pvStart, size_t cPages, PSUPPAGE paPages)
783{
784 /*
785 * Validate.
786 */
787 AssertPtr(pvStart);
788 AssertMsg(RT_ALIGN_P(pvStart, PAGE_SIZE) == pvStart, ("pvStart (%p) must be page aligned\n", pvStart));
789 AssertPtr(paPages);
790
791 /* fake */
792 if (RT_UNLIKELY(g_u32FakeMode))
793 {
794 RTHCPHYS Phys = (uintptr_t)pvStart + PAGE_SIZE * 1024;
795 size_t iPage = cPages;
796 while (iPage-- > 0)
797 paPages[iPage].Phys = Phys + (iPage << PAGE_SHIFT);
798 return VINF_SUCCESS;
799 }
800
801 /*
802 * Issue IOCtl to the SUPDRV kernel module.
803 */
804 int rc;
805 PSUPPAGELOCK pReq = (PSUPPAGELOCK)RTMemTmpAllocZ(SUP_IOCTL_PAGE_LOCK_SIZE(cPages));
806 if (RT_LIKELY(pReq))
807 {
808 pReq->Hdr.u32Cookie = g_u32Cookie;
809 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
810 pReq->Hdr.cbIn = SUP_IOCTL_PAGE_LOCK_SIZE_IN;
811 pReq->Hdr.cbOut = SUP_IOCTL_PAGE_LOCK_SIZE_OUT(cPages);
812 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
813 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
814 pReq->u.In.pvR3 = pvStart;
815 pReq->u.In.cPages = (uint32_t)cPages; AssertRelease(pReq->u.In.cPages == cPages);
816 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_LOCK, pReq, SUP_IOCTL_PAGE_LOCK_SIZE(cPages));
817 if (RT_SUCCESS(rc))
818 rc = pReq->Hdr.rc;
819 if (RT_SUCCESS(rc))
820 {
821 for (uint32_t iPage = 0; iPage < cPages; iPage++)
822 {
823 paPages[iPage].uReserved = 0;
824 paPages[iPage].Phys = pReq->u.Out.aPages[iPage];
825 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
826 }
827 }
828 RTMemTmpFree(pReq);
829 }
830 else
831 rc = VERR_NO_TMP_MEMORY;
832
833 return rc;
834}
835
836
837SUPR3DECL(int) SUPPageUnlock(void *pvStart)
838{
839 /*
840 * Validate.
841 */
842 AssertPtr(pvStart);
843 AssertMsg(RT_ALIGN_P(pvStart, PAGE_SIZE) == pvStart, ("pvStart (%p) must be page aligned\n", pvStart));
844
845 /* fake */
846 if (RT_UNLIKELY(g_u32FakeMode))
847 return VINF_SUCCESS;
848
849 /*
850 * Issue IOCtl to the SUPDRV kernel module.
851 */
852 SUPPAGEUNLOCK Req;
853 Req.Hdr.u32Cookie = g_u32Cookie;
854 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
855 Req.Hdr.cbIn = SUP_IOCTL_PAGE_UNLOCK_SIZE_IN;
856 Req.Hdr.cbOut = SUP_IOCTL_PAGE_UNLOCK_SIZE_OUT;
857 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
858 Req.Hdr.rc = VERR_INTERNAL_ERROR;
859 Req.u.In.pvR3 = pvStart;
860 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_UNLOCK, &Req, SUP_IOCTL_PAGE_UNLOCK_SIZE);
861 if (RT_SUCCESS(rc))
862 rc = Req.Hdr.rc;
863 return rc;
864}
865
866
867SUPR3DECL(int) SUPPageAllocLockedEx(size_t cPages, void **ppvPages, PSUPPAGE paPages)
868{
869 return SUPR3PageAllocEx(cPages, 0 /*fFlags*/, ppvPages, NULL /*pR0Ptr*/, paPages);
870}
871
872
873SUPR3DECL(int) SUPPageFreeLocked(void *pvPages, size_t cPages)
874{
875 /*
876 * Validate.
877 */
878 AssertPtrReturn(pvPages, VERR_INVALID_POINTER);
879 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
880
881 /*
882 * Check if we're employing the fallback or not to avoid the
883 * fuzzy handling of this in SUPR3PageFreeEx.
884 */
885 int rc;
886 if (g_fSupportsPageAllocNoKernel)
887 rc = SUPR3PageFreeEx(pvPages, cPages);
888 else
889 {
890 /* fallback */
891 rc = SUPPageUnlock(pvPages);
892 if (RT_SUCCESS(rc))
893 rc = suplibOsPageFree(&g_supLibData, pvPages, cPages);
894 }
895 return rc;
896}
897
898
899/**
900 * Fallback for SUPPageAllocLockedEx on systems where RTR0MemObjPhysAllocNC isn't supported.
901 */
902static int supPagePageAllocNoKernelFallback(size_t cPages, void **ppvPages, PSUPPAGE paPages)
903{
904 int rc = suplibOsPageAlloc(&g_supLibData, cPages, ppvPages);
905 if (RT_SUCCESS(rc))
906 {
907 if (!paPages)
908 paPages = (PSUPPAGE)alloca(sizeof(paPages[0]) * cPages);
909 rc = SUPPageLock(*ppvPages, cPages, paPages);
910 if (RT_FAILURE(rc))
911 suplibOsPageFree(&g_supLibData, *ppvPages, cPages);
912 }
913 return rc;
914}
915
916
917SUPR3DECL(int) SUPR3PageAllocEx(size_t cPages, uint32_t fFlags, void **ppvPages, PRTR0PTR pR0Ptr, PSUPPAGE paPages)
918{
919 /*
920 * Validate.
921 */
922 AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
923 *ppvPages = NULL;
924 AssertPtrNullReturn(pR0Ptr, VERR_INVALID_POINTER);
925 if (pR0Ptr)
926 *pR0Ptr = NIL_RTR0PTR;
927 AssertPtrNullReturn(paPages, VERR_INVALID_POINTER);
928 AssertMsgReturn(cPages > 0 && cPages <= VBOX_MAX_ALLOC_PAGE_COUNT, ("cPages=%zu\n", cPages), VERR_PAGE_COUNT_OUT_OF_RANGE);
929
930 /* fake */
931 if (RT_UNLIKELY(g_u32FakeMode))
932 {
933 void *pv = RTMemPageAllocZ(cPages * PAGE_SIZE);
934 if (!pv)
935 return VERR_NO_MEMORY;
936 *ppvPages = pv;
937 if (pR0Ptr)
938 *pR0Ptr = (RTR0PTR)pv;
939 if (paPages)
940 for (size_t iPage = 0; iPage < cPages; iPage++)
941 {
942 paPages[iPage].uReserved = 0;
943 paPages[iPage].Phys = (iPage + 4321) << PAGE_SHIFT;
944 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
945 }
946 return VINF_SUCCESS;
947 }
948
949 /*
950 * Use fallback for non-R0 mapping?
951 */
952 if ( !pR0Ptr
953 && !g_fSupportsPageAllocNoKernel)
954 return supPagePageAllocNoKernelFallback(cPages, ppvPages, paPages);
955
956 /*
957 * Issue IOCtl to the SUPDRV kernel module.
958 */
959 int rc;
960 PSUPPAGEALLOCEX pReq = (PSUPPAGEALLOCEX)RTMemTmpAllocZ(SUP_IOCTL_PAGE_ALLOC_EX_SIZE(cPages));
961 if (pReq)
962 {
963 pReq->Hdr.u32Cookie = g_u32Cookie;
964 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
965 pReq->Hdr.cbIn = SUP_IOCTL_PAGE_ALLOC_EX_SIZE_IN;
966 pReq->Hdr.cbOut = SUP_IOCTL_PAGE_ALLOC_EX_SIZE_OUT(cPages);
967 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
968 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
969 pReq->u.In.cPages = (uint32_t)cPages; AssertRelease(pReq->u.In.cPages == cPages);
970 pReq->u.In.fKernelMapping = pR0Ptr != NULL;
971 pReq->u.In.fUserMapping = true;
972 pReq->u.In.fReserved0 = false;
973 pReq->u.In.fReserved1 = false;
974 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_ALLOC_EX, pReq, SUP_IOCTL_PAGE_ALLOC_EX_SIZE(cPages));
975 if (RT_SUCCESS(rc))
976 {
977 rc = pReq->Hdr.rc;
978 if (RT_SUCCESS(rc))
979 {
980 *ppvPages = pReq->u.Out.pvR3;
981 if (pR0Ptr)
982 *pR0Ptr = pReq->u.Out.pvR0;
983 if (paPages)
984 for (size_t iPage = 0; iPage < cPages; iPage++)
985 {
986 paPages[iPage].uReserved = 0;
987 paPages[iPage].Phys = pReq->u.Out.aPages[iPage];
988 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
989 }
990 }
991 else if ( rc == VERR_NOT_SUPPORTED
992 && !pR0Ptr)
993 {
994 g_fSupportsPageAllocNoKernel = false;
995 rc = supPagePageAllocNoKernelFallback(cPages, ppvPages, paPages);
996 }
997 }
998
999 RTMemTmpFree(pReq);
1000 }
1001 else
1002 rc = VERR_NO_TMP_MEMORY;
1003 return rc;
1004
1005}
1006
1007
1008SUPR3DECL(int) SUPR3PageMapKernel(void *pvR3, uint32_t off, uint32_t cb, uint32_t fFlags, PRTR0PTR pR0Ptr)
1009{
1010 /*
1011 * Validate.
1012 */
1013 AssertPtrReturn(pvR3, VERR_INVALID_POINTER);
1014 AssertPtrReturn(pR0Ptr, VERR_INVALID_POINTER);
1015 Assert(!(off & PAGE_OFFSET_MASK));
1016 Assert(!(cb & PAGE_OFFSET_MASK) && cb);
1017 Assert(!fFlags);
1018 *pR0Ptr = NIL_RTR0PTR;
1019
1020 /* fake */
1021 if (RT_UNLIKELY(g_u32FakeMode))
1022 return VERR_NOT_SUPPORTED;
1023
1024 /*
1025 * Issue IOCtl to the SUPDRV kernel module.
1026 */
1027 SUPPAGEMAPKERNEL Req;
1028 Req.Hdr.u32Cookie = g_u32Cookie;
1029 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1030 Req.Hdr.cbIn = SUP_IOCTL_PAGE_MAP_KERNEL_SIZE_IN;
1031 Req.Hdr.cbOut = SUP_IOCTL_PAGE_MAP_KERNEL_SIZE_OUT;
1032 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1033 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1034 Req.u.In.pvR3 = pvR3;
1035 Req.u.In.offSub = off;
1036 Req.u.In.cbSub = cb;
1037 Req.u.In.fFlags = fFlags;
1038 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_MAP_KERNEL, &Req, SUP_IOCTL_PAGE_MAP_KERNEL_SIZE);
1039 if (RT_SUCCESS(rc))
1040 rc = Req.Hdr.rc;
1041 if (RT_SUCCESS(rc))
1042 *pR0Ptr = Req.u.Out.pvR0;
1043 return rc;
1044}
1045
1046
1047SUPR3DECL(int) SUPR3PageFreeEx(void *pvPages, size_t cPages)
1048{
1049 /*
1050 * Validate.
1051 */
1052 AssertPtrReturn(pvPages, VERR_INVALID_POINTER);
1053 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1054
1055 /* fake */
1056 if (RT_UNLIKELY(g_u32FakeMode))
1057 {
1058 RTMemPageFree(pvPages);
1059 return VINF_SUCCESS;
1060 }
1061
1062 /*
1063 * Try normal free first, then if it fails check if we're using the fallback .
1064 * for the allocations without kernel mappings and attempt unlocking it.
1065 */
1066 NOREF(cPages);
1067 SUPPAGEFREE Req;
1068 Req.Hdr.u32Cookie = g_u32Cookie;
1069 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1070 Req.Hdr.cbIn = SUP_IOCTL_PAGE_FREE_SIZE_IN;
1071 Req.Hdr.cbOut = SUP_IOCTL_PAGE_FREE_SIZE_OUT;
1072 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1073 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1074 Req.u.In.pvR3 = pvPages;
1075 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_PAGE_FREE, &Req, SUP_IOCTL_PAGE_FREE_SIZE);
1076 if (RT_SUCCESS(rc))
1077 {
1078 rc = Req.Hdr.rc;
1079 if ( rc == VERR_INVALID_PARAMETER
1080 && !g_fSupportsPageAllocNoKernel)
1081 {
1082 int rc2 = SUPPageUnlock(pvPages);
1083 if (RT_SUCCESS(rc2))
1084 rc = suplibOsPageFree(&g_supLibData, pvPages, cPages);
1085 }
1086 }
1087 return rc;
1088}
1089
1090
1091SUPR3DECL(void *) SUPContAlloc(size_t cPages, PRTHCPHYS pHCPhys)
1092{
1093 return SUPContAlloc2(cPages, NIL_RTR0PTR, pHCPhys);
1094}
1095
1096
1097SUPR3DECL(void *) SUPContAlloc2(size_t cPages, PRTR0PTR pR0Ptr, PRTHCPHYS pHCPhys)
1098{
1099 /*
1100 * Validate.
1101 */
1102 AssertPtrReturn(pHCPhys, NULL);
1103 *pHCPhys = NIL_RTHCPHYS;
1104 AssertPtrNullReturn(pR0Ptr, NULL);
1105 if (pR0Ptr)
1106 *pR0Ptr = NIL_RTR0PTR;
1107 AssertPtrNullReturn(pHCPhys, NULL);
1108 AssertMsgReturn(cPages > 0 && cPages < 256, ("cPages=%d must be > 0 and < 256\n", cPages), NULL);
1109
1110 /* fake */
1111 if (RT_UNLIKELY(g_u32FakeMode))
1112 {
1113 void *pv = RTMemPageAllocZ(cPages * PAGE_SIZE);
1114 if (pR0Ptr)
1115 *pR0Ptr = (RTR0PTR)pv;
1116 if (pHCPhys)
1117 *pHCPhys = (uintptr_t)pv + (PAGE_SHIFT * 1024);
1118 return pv;
1119 }
1120
1121 /*
1122 * Issue IOCtl to the SUPDRV kernel module.
1123 */
1124 SUPCONTALLOC Req;
1125 Req.Hdr.u32Cookie = g_u32Cookie;
1126 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1127 Req.Hdr.cbIn = SUP_IOCTL_CONT_ALLOC_SIZE_IN;
1128 Req.Hdr.cbOut = SUP_IOCTL_CONT_ALLOC_SIZE_OUT;
1129 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1130 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1131 Req.u.In.cPages = (uint32_t)cPages;
1132 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CONT_ALLOC, &Req, SUP_IOCTL_CONT_ALLOC_SIZE);
1133 if ( RT_SUCCESS(rc)
1134 && RT_SUCCESS(Req.Hdr.rc))
1135 {
1136 *pHCPhys = Req.u.Out.HCPhys;
1137 if (pR0Ptr)
1138 *pR0Ptr = Req.u.Out.pvR0;
1139 return Req.u.Out.pvR3;
1140 }
1141
1142 return NULL;
1143}
1144
1145
1146SUPR3DECL(int) SUPContFree(void *pv, size_t cPages)
1147{
1148 /*
1149 * Validate.
1150 */
1151 if (!pv)
1152 return VINF_SUCCESS;
1153 AssertPtrReturn(pv, VERR_INVALID_POINTER);
1154 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1155
1156 /* fake */
1157 if (RT_UNLIKELY(g_u32FakeMode))
1158 {
1159 RTMemPageFree(pv);
1160 return VINF_SUCCESS;
1161 }
1162
1163 /*
1164 * Issue IOCtl to the SUPDRV kernel module.
1165 */
1166 SUPCONTFREE Req;
1167 Req.Hdr.u32Cookie = g_u32Cookie;
1168 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1169 Req.Hdr.cbIn = SUP_IOCTL_CONT_FREE_SIZE_IN;
1170 Req.Hdr.cbOut = SUP_IOCTL_CONT_FREE_SIZE_OUT;
1171 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1172 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1173 Req.u.In.pvR3 = pv;
1174 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_CONT_FREE, &Req, SUP_IOCTL_CONT_FREE_SIZE);
1175 if (RT_SUCCESS(rc))
1176 rc = Req.Hdr.rc;
1177 return rc;
1178}
1179
1180
1181SUPR3DECL(int) SUPLowAlloc(size_t cPages, void **ppvPages, PRTR0PTR ppvPagesR0, PSUPPAGE paPages)
1182{
1183 /*
1184 * Validate.
1185 */
1186 AssertPtrReturn(ppvPages, VERR_INVALID_POINTER);
1187 *ppvPages = NULL;
1188 AssertPtrReturn(paPages, VERR_INVALID_POINTER);
1189 AssertMsgReturn(cPages > 0 && cPages < 256, ("cPages=%d must be > 0 and < 256\n", cPages), VERR_PAGE_COUNT_OUT_OF_RANGE);
1190
1191 /* fake */
1192 if (RT_UNLIKELY(g_u32FakeMode))
1193 {
1194 *ppvPages = RTMemPageAllocZ((size_t)cPages * PAGE_SIZE);
1195 if (!*ppvPages)
1196 return VERR_NO_LOW_MEMORY;
1197
1198 /* fake physical addresses. */
1199 RTHCPHYS Phys = (uintptr_t)*ppvPages + PAGE_SIZE * 1024;
1200 size_t iPage = cPages;
1201 while (iPage-- > 0)
1202 paPages[iPage].Phys = Phys + (iPage << PAGE_SHIFT);
1203 return VINF_SUCCESS;
1204 }
1205
1206 /*
1207 * Issue IOCtl to the SUPDRV kernel module.
1208 */
1209 int rc;
1210 PSUPLOWALLOC pReq = (PSUPLOWALLOC)RTMemTmpAllocZ(SUP_IOCTL_LOW_ALLOC_SIZE(cPages));
1211 if (pReq)
1212 {
1213 pReq->Hdr.u32Cookie = g_u32Cookie;
1214 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
1215 pReq->Hdr.cbIn = SUP_IOCTL_LOW_ALLOC_SIZE_IN;
1216 pReq->Hdr.cbOut = SUP_IOCTL_LOW_ALLOC_SIZE_OUT(cPages);
1217 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_OUT;
1218 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
1219 pReq->u.In.cPages = (uint32_t)cPages; AssertRelease(pReq->u.In.cPages == cPages);
1220 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LOW_ALLOC, pReq, SUP_IOCTL_LOW_ALLOC_SIZE(cPages));
1221 if (RT_SUCCESS(rc))
1222 rc = pReq->Hdr.rc;
1223 if (RT_SUCCESS(rc))
1224 {
1225 *ppvPages = pReq->u.Out.pvR3;
1226 if (ppvPagesR0)
1227 *ppvPagesR0 = pReq->u.Out.pvR0;
1228 if (paPages)
1229 for (size_t iPage = 0; iPage < cPages; iPage++)
1230 {
1231 paPages[iPage].uReserved = 0;
1232 paPages[iPage].Phys = pReq->u.Out.aPages[iPage];
1233 Assert(!(paPages[iPage].Phys & ~X86_PTE_PAE_PG_MASK));
1234 Assert(paPages[iPage].Phys <= UINT32_C(0xfffff000));
1235 }
1236 }
1237 RTMemTmpFree(pReq);
1238 }
1239 else
1240 rc = VERR_NO_TMP_MEMORY;
1241
1242 return rc;
1243}
1244
1245
1246SUPR3DECL(int) SUPLowFree(void *pv, size_t cPages)
1247{
1248 /*
1249 * Validate.
1250 */
1251 if (!pv)
1252 return VINF_SUCCESS;
1253 AssertPtrReturn(pv, VERR_INVALID_POINTER);
1254 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
1255
1256 /* fake */
1257 if (RT_UNLIKELY(g_u32FakeMode))
1258 {
1259 RTMemPageFree(pv);
1260 return VINF_SUCCESS;
1261 }
1262
1263 /*
1264 * Issue IOCtl to the SUPDRV kernel module.
1265 */
1266 SUPCONTFREE Req;
1267 Req.Hdr.u32Cookie = g_u32Cookie;
1268 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1269 Req.Hdr.cbIn = SUP_IOCTL_LOW_FREE_SIZE_IN;
1270 Req.Hdr.cbOut = SUP_IOCTL_LOW_FREE_SIZE_OUT;
1271 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1272 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1273 Req.u.In.pvR3 = pv;
1274 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LOW_FREE, &Req, SUP_IOCTL_LOW_FREE_SIZE);
1275 if (RT_SUCCESS(rc))
1276 rc = Req.Hdr.rc;
1277 return rc;
1278}
1279
1280
1281SUPR3DECL(int) SUPR3HardenedVerifyFile(const char *pszFilename, const char *pszMsg, PRTFILE phFile)
1282{
1283 /*
1284 * Quick input validation.
1285 */
1286 AssertPtr(pszFilename);
1287 AssertPtr(pszMsg);
1288 AssertReturn(!phFile, VERR_NOT_IMPLEMENTED); /** @todo Implement this. The deal is that we make sure the
1289 file is the same we verified after opening it. */
1290
1291 /*
1292 * Only do the actual check in hardened builds.
1293 */
1294#ifdef VBOX_WITH_HARDENING
1295 int rc = supR3HardenedVerifyFile(pszFilename, false /* fFatal */);
1296 if (RT_FAILURE(rc))
1297 LogRel(("SUPR3HardenedVerifyFile: %s: Verification of \"%s\" failed, rc=%Rrc\n", pszMsg, pszFilename, rc));
1298 return rc;
1299#else
1300 return VINF_SUCCESS;
1301#endif
1302}
1303
1304
1305SUPR3DECL(int) SUPLoadModule(const char *pszFilename, const char *pszModule, void **ppvImageBase)
1306{
1307 int rc = VINF_SUCCESS;
1308#ifdef VBOX_WITH_HARDENING
1309 /*
1310 * Check that the module can be trusted.
1311 */
1312 rc = supR3HardenedVerifyFile(pszFilename, false /* fFatal */);
1313#endif
1314 if (RT_SUCCESS(rc))
1315 rc = supLoadModule(pszFilename, pszModule, NULL, ppvImageBase);
1316 else
1317 LogRel(("SUPLoadModule: Verification of \"%s\" failed, rc=%Rrc\n", rc));
1318 return rc;
1319}
1320
1321
1322SUPR3DECL(int) SUPR3LoadServiceModule(const char *pszFilename, const char *pszModule,
1323 const char *pszSrvReqHandler, void **ppvImageBase)
1324{
1325 int rc = VINF_SUCCESS;
1326 AssertPtrReturn(pszSrvReqHandler, VERR_INVALID_PARAMETER);
1327
1328#ifdef VBOX_WITH_HARDENING
1329 /*
1330 * Check that the module can be trusted.
1331 */
1332 rc = supR3HardenedVerifyFile(pszFilename, false /* fFatal */);
1333#endif
1334 if (RT_SUCCESS(rc))
1335 rc = supLoadModule(pszFilename, pszModule, pszSrvReqHandler, ppvImageBase);
1336 else
1337 LogRel(("SUPR3LoadServiceModule: Verification of \"%s\" failed, rc=%Rrc\n", rc));
1338 return rc;
1339}
1340
1341
1342/**
1343 * Resolve an external symbol during RTLdrGetBits().
1344 *
1345 * @returns VBox status code.
1346 * @param hLdrMod The loader module handle.
1347 * @param pszModule Module name.
1348 * @param pszSymbol Symbol name, NULL if uSymbol should be used.
1349 * @param uSymbol Symbol ordinal, ~0 if pszSymbol should be used.
1350 * @param pValue Where to store the symbol value (address).
1351 * @param pvUser User argument.
1352 */
1353static DECLCALLBACK(int) supLoadModuleResolveImport(RTLDRMOD hLdrMod, const char *pszModule,
1354 const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser)
1355{
1356 AssertPtr(pValue);
1357 AssertPtr(pvUser);
1358
1359 /*
1360 * Only SUPR0 and VMMR0.r0
1361 */
1362 if ( pszModule
1363 && *pszModule
1364 && strcmp(pszModule, "SUPR0.dll")
1365 && strcmp(pszModule, "VMMR0.r0"))
1366 {
1367 AssertMsgFailed(("%s is importing from %s! (expected 'SUPR0.dll' or 'VMMR0.r0', case-sensitiv)\n", pvUser, pszModule));
1368 return VERR_SYMBOL_NOT_FOUND;
1369 }
1370
1371 /*
1372 * No ordinals.
1373 */
1374 if (pszSymbol < (const char*)0x10000)
1375 {
1376 AssertMsgFailed(("%s is importing by ordinal (ord=%d)\n", pvUser, (int)(uintptr_t)pszSymbol));
1377 return VERR_SYMBOL_NOT_FOUND;
1378 }
1379
1380 /*
1381 * Lookup symbol.
1382 */
1383 /* skip the 64-bit ELF import prefix first. */
1384 if (!strncmp(pszSymbol, "SUPR0$", sizeof("SUPR0$") - 1))
1385 pszSymbol += sizeof("SUPR0$") - 1;
1386
1387 /*
1388 * Check the VMMR0.r0 module if loaded.
1389 */
1390 /** @todo call the SUPLoadModule caller.... */
1391 /** @todo proper reference counting and such. */
1392 if (g_pvVMMR0 != NIL_RTR0PTR)
1393 {
1394 void *pvValue;
1395 if (!SUPGetSymbolR0((void *)g_pvVMMR0, pszSymbol, &pvValue))
1396 {
1397 *pValue = (uintptr_t)pvValue;
1398 return VINF_SUCCESS;
1399 }
1400 }
1401
1402 /* iterate the function table. */
1403 int c = g_pFunctions->u.Out.cFunctions;
1404 PSUPFUNC pFunc = &g_pFunctions->u.Out.aFunctions[0];
1405 while (c-- > 0)
1406 {
1407 if (!strcmp(pFunc->szName, pszSymbol))
1408 {
1409 *pValue = (uintptr_t)pFunc->pfn;
1410 return VINF_SUCCESS;
1411 }
1412 pFunc++;
1413 }
1414
1415 /*
1416 * The GIP.
1417 */
1418 /** @todo R0 mapping? */
1419 if ( pszSymbol
1420 && g_pSUPGlobalInfoPage
1421 && g_pSUPGlobalInfoPageR0
1422 && !strcmp(pszSymbol, "g_SUPGlobalInfoPage"))
1423 {
1424 *pValue = (uintptr_t)g_pSUPGlobalInfoPageR0;
1425 return VINF_SUCCESS;
1426 }
1427
1428 /*
1429 * Despair.
1430 */
1431 c = g_pFunctions->u.Out.cFunctions;
1432 pFunc = &g_pFunctions->u.Out.aFunctions[0];
1433 while (c-- > 0)
1434 {
1435 AssertMsg2("%d: %s\n", g_pFunctions->u.Out.cFunctions - c, pFunc->szName);
1436 pFunc++;
1437 }
1438
1439 AssertMsg2("%s is importing %s which we couldn't find\n", pvUser, pszSymbol);
1440 AssertMsgFailed(("%s is importing %s which we couldn't find\n", pvUser, pszSymbol));
1441 if (g_u32FakeMode)
1442 {
1443 *pValue = 0xdeadbeef;
1444 return VINF_SUCCESS;
1445 }
1446 return VERR_SYMBOL_NOT_FOUND;
1447}
1448
1449
1450/** Argument package for supLoadModuleCalcSizeCB. */
1451typedef struct SUPLDRCALCSIZEARGS
1452{
1453 size_t cbStrings;
1454 uint32_t cSymbols;
1455 size_t cbImage;
1456} SUPLDRCALCSIZEARGS, *PSUPLDRCALCSIZEARGS;
1457
1458/**
1459 * Callback used to calculate the image size.
1460 * @return VINF_SUCCESS
1461 */
1462static DECLCALLBACK(int) supLoadModuleCalcSizeCB(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
1463{
1464 PSUPLDRCALCSIZEARGS pArgs = (PSUPLDRCALCSIZEARGS)pvUser;
1465 if ( pszSymbol != NULL
1466 && *pszSymbol
1467 && Value <= pArgs->cbImage)
1468 {
1469 pArgs->cSymbols++;
1470 pArgs->cbStrings += strlen(pszSymbol) + 1;
1471 }
1472 return VINF_SUCCESS;
1473}
1474
1475
1476/** Argument package for supLoadModuleCreateTabsCB. */
1477typedef struct SUPLDRCREATETABSARGS
1478{
1479 size_t cbImage;
1480 PSUPLDRSYM pSym;
1481 char *pszBase;
1482 char *psz;
1483} SUPLDRCREATETABSARGS, *PSUPLDRCREATETABSARGS;
1484
1485/**
1486 * Callback used to calculate the image size.
1487 * @return VINF_SUCCESS
1488 */
1489static DECLCALLBACK(int) supLoadModuleCreateTabsCB(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
1490{
1491 PSUPLDRCREATETABSARGS pArgs = (PSUPLDRCREATETABSARGS)pvUser;
1492 if ( pszSymbol != NULL
1493 && *pszSymbol
1494 && Value <= pArgs->cbImage)
1495 {
1496 pArgs->pSym->offSymbol = (uint32_t)Value;
1497 pArgs->pSym->offName = pArgs->psz - pArgs->pszBase;
1498 pArgs->pSym++;
1499
1500 size_t cbCopy = strlen(pszSymbol) + 1;
1501 memcpy(pArgs->psz, pszSymbol, cbCopy);
1502 pArgs->psz += cbCopy;
1503 }
1504 return VINF_SUCCESS;
1505}
1506
1507
1508/**
1509 * Worker for SUPLoadModule().
1510 *
1511 * @returns VBox status code.
1512 * @param pszFilename Name of the VMMR0 image file
1513 */
1514static int supLoadModule(const char *pszFilename, const char *pszModule, const char *pszSrvReqHandler, void **ppvImageBase)
1515{
1516 /*
1517 * Validate input.
1518 */
1519 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
1520 AssertPtrReturn(pszModule, VERR_INVALID_PARAMETER);
1521 AssertPtrReturn(ppvImageBase, VERR_INVALID_PARAMETER);
1522 AssertReturn(strlen(pszModule) < RT_SIZEOFMEMB(SUPLDROPEN, u.In.szName), VERR_FILENAME_TOO_LONG);
1523
1524 const bool fIsVMMR0 = !strcmp(pszModule, "VMMR0.r0");
1525 AssertReturn(!pszSrvReqHandler || !fIsVMMR0, VERR_INTERNAL_ERROR);
1526 *ppvImageBase = NULL;
1527
1528 /*
1529 * Open image file and figure its size.
1530 */
1531 RTLDRMOD hLdrMod;
1532 int rc = RTLdrOpen(pszFilename, &hLdrMod);
1533 if (!RT_SUCCESS(rc))
1534 return rc;
1535
1536 SUPLDRCALCSIZEARGS CalcArgs;
1537 CalcArgs.cbStrings = 0;
1538 CalcArgs.cSymbols = 0;
1539 CalcArgs.cbImage = RTLdrSize(hLdrMod);
1540 rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCalcSizeCB, &CalcArgs);
1541 if (RT_SUCCESS(rc))
1542 {
1543 const uint32_t offSymTab = RT_ALIGN_32(CalcArgs.cbImage, 8);
1544 const uint32_t offStrTab = offSymTab + CalcArgs.cSymbols * sizeof(SUPLDRSYM);
1545 const uint32_t cbImage = RT_ALIGN_32(offStrTab + CalcArgs.cbStrings, 8);
1546
1547 /*
1548 * Open the R0 image.
1549 */
1550 SUPLDROPEN OpenReq;
1551 OpenReq.Hdr.u32Cookie = g_u32Cookie;
1552 OpenReq.Hdr.u32SessionCookie = g_u32SessionCookie;
1553 OpenReq.Hdr.cbIn = SUP_IOCTL_LDR_OPEN_SIZE_IN;
1554 OpenReq.Hdr.cbOut = SUP_IOCTL_LDR_OPEN_SIZE_OUT;
1555 OpenReq.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1556 OpenReq.Hdr.rc = VERR_INTERNAL_ERROR;
1557 OpenReq.u.In.cbImage = cbImage;
1558 strcpy(OpenReq.u.In.szName, pszModule);
1559 if (!g_u32FakeMode)
1560 {
1561 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_OPEN, &OpenReq, SUP_IOCTL_LDR_OPEN_SIZE);
1562 if (RT_SUCCESS(rc))
1563 rc = OpenReq.Hdr.rc;
1564 }
1565 else
1566 {
1567 OpenReq.u.Out.fNeedsLoading = true;
1568 OpenReq.u.Out.pvImageBase = 0xef423420;
1569 }
1570 *ppvImageBase = (void *)OpenReq.u.Out.pvImageBase;
1571 if ( RT_SUCCESS(rc)
1572 && OpenReq.u.Out.fNeedsLoading)
1573 {
1574 /*
1575 * We need to load it.
1576 * Allocate memory for the image bits.
1577 */
1578 PSUPLDRLOAD pLoadReq = (PSUPLDRLOAD)RTMemTmpAlloc(SUP_IOCTL_LDR_LOAD_SIZE(cbImage));
1579 if (pLoadReq)
1580 {
1581 /*
1582 * Get the image bits.
1583 */
1584 rc = RTLdrGetBits(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase,
1585 supLoadModuleResolveImport, (void *)pszModule);
1586
1587 if (RT_SUCCESS(rc))
1588 {
1589 /*
1590 * Get the entry points.
1591 */
1592 RTUINTPTR VMMR0EntryInt = 0;
1593 RTUINTPTR VMMR0EntryFast = 0;
1594 RTUINTPTR VMMR0EntryEx = 0;
1595 RTUINTPTR SrvReqHandler = 0;
1596 RTUINTPTR ModuleInit = 0;
1597 RTUINTPTR ModuleTerm = 0;
1598 if (fIsVMMR0)
1599 {
1600 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "VMMR0EntryInt", &VMMR0EntryInt);
1601 if (RT_SUCCESS(rc))
1602 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "VMMR0EntryFast", &VMMR0EntryFast);
1603 if (RT_SUCCESS(rc))
1604 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "VMMR0EntryEx", &VMMR0EntryEx);
1605 }
1606 else if (pszSrvReqHandler)
1607 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, pszSrvReqHandler, &SrvReqHandler);
1608 if (RT_SUCCESS(rc))
1609 {
1610 int rc2 = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "ModuleInit", &ModuleInit);
1611 if (RT_FAILURE(rc2))
1612 ModuleInit = 0;
1613
1614 rc2 = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "ModuleTerm", &ModuleTerm);
1615 if (RT_FAILURE(rc2))
1616 ModuleTerm = 0;
1617 }
1618 if (RT_SUCCESS(rc))
1619 {
1620 /*
1621 * Create the symbol and string tables.
1622 */
1623 SUPLDRCREATETABSARGS CreateArgs;
1624 CreateArgs.cbImage = CalcArgs.cbImage;
1625 CreateArgs.pSym = (PSUPLDRSYM)&pLoadReq->u.In.achImage[offSymTab];
1626 CreateArgs.pszBase = (char *)&pLoadReq->u.In.achImage[offStrTab];
1627 CreateArgs.psz = CreateArgs.pszBase;
1628 rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCreateTabsCB, &CreateArgs);
1629 if (RT_SUCCESS(rc))
1630 {
1631 AssertRelease((size_t)(CreateArgs.psz - CreateArgs.pszBase) <= CalcArgs.cbStrings);
1632 AssertRelease((size_t)(CreateArgs.pSym - (PSUPLDRSYM)&pLoadReq->u.In.achImage[offSymTab]) <= CalcArgs.cSymbols);
1633
1634 /*
1635 * Upload the image.
1636 */
1637 pLoadReq->Hdr.u32Cookie = g_u32Cookie;
1638 pLoadReq->Hdr.u32SessionCookie = g_u32SessionCookie;
1639 pLoadReq->Hdr.cbIn = SUP_IOCTL_LDR_LOAD_SIZE_IN(cbImage);
1640 pLoadReq->Hdr.cbOut = SUP_IOCTL_LDR_LOAD_SIZE_OUT;
1641 pLoadReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_IN;
1642 pLoadReq->Hdr.rc = VERR_INTERNAL_ERROR;
1643
1644 pLoadReq->u.In.pfnModuleInit = (RTR0PTR)ModuleInit;
1645 pLoadReq->u.In.pfnModuleTerm = (RTR0PTR)ModuleTerm;
1646 if (fIsVMMR0)
1647 {
1648 pLoadReq->u.In.eEPType = SUPLDRLOADEP_VMMR0;
1649 pLoadReq->u.In.EP.VMMR0.pvVMMR0 = OpenReq.u.Out.pvImageBase;
1650 pLoadReq->u.In.EP.VMMR0.pvVMMR0EntryInt = (RTR0PTR)VMMR0EntryInt;
1651 pLoadReq->u.In.EP.VMMR0.pvVMMR0EntryFast= (RTR0PTR)VMMR0EntryFast;
1652 pLoadReq->u.In.EP.VMMR0.pvVMMR0EntryEx = (RTR0PTR)VMMR0EntryEx;
1653 }
1654 else if (pszSrvReqHandler)
1655 {
1656 pLoadReq->u.In.eEPType = SUPLDRLOADEP_SERVICE;
1657 pLoadReq->u.In.EP.Service.pfnServiceReq = (RTR0PTR)SrvReqHandler;
1658 pLoadReq->u.In.EP.Service.apvReserved[0] = NIL_RTR0PTR;
1659 pLoadReq->u.In.EP.Service.apvReserved[1] = NIL_RTR0PTR;
1660 pLoadReq->u.In.EP.Service.apvReserved[2] = NIL_RTR0PTR;
1661 }
1662 else
1663 pLoadReq->u.In.eEPType = SUPLDRLOADEP_NOTHING;
1664 pLoadReq->u.In.offStrTab = offStrTab;
1665 pLoadReq->u.In.cbStrTab = (uint32_t)CalcArgs.cbStrings;
1666 AssertRelease(pLoadReq->u.In.cbStrTab == CalcArgs.cbStrings);
1667 pLoadReq->u.In.offSymbols = offSymTab;
1668 pLoadReq->u.In.cSymbols = CalcArgs.cSymbols;
1669 pLoadReq->u.In.cbImage = cbImage;
1670 pLoadReq->u.In.pvImageBase = OpenReq.u.Out.pvImageBase;
1671 if (!g_u32FakeMode)
1672 {
1673 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_LOAD, pLoadReq, SUP_IOCTL_LDR_LOAD_SIZE(cbImage));
1674 if (RT_SUCCESS(rc))
1675 rc = pLoadReq->Hdr.rc;
1676 }
1677 else
1678 rc = VINF_SUCCESS;
1679 if ( RT_SUCCESS(rc)
1680 || rc == VERR_ALREADY_LOADED /* A competing process. */
1681 )
1682 {
1683 LogRel(("SUP: Loaded %s (%s) at %#p - ModuleInit at %RTptr and ModuleTerm at %RTptr\n", pszModule, pszFilename,
1684 OpenReq.u.Out.pvImageBase, ModuleInit, ModuleTerm));
1685 if (fIsVMMR0)
1686 {
1687 g_pvVMMR0 = OpenReq.u.Out.pvImageBase;
1688 LogRel(("SUP: VMMR0EntryEx located at %RTptr, VMMR0EntryFast at %RTptr and VMMR0EntryInt at %RTptr\n",
1689 VMMR0EntryEx, VMMR0EntryFast, VMMR0EntryInt));
1690 }
1691#ifdef RT_OS_WINDOWS
1692 LogRel(("SUP: windbg> .reload /f %s=%#p\n", pszFilename, OpenReq.u.Out.pvImageBase));
1693#endif
1694
1695 RTMemTmpFree(pLoadReq);
1696 RTLdrClose(hLdrMod);
1697 return VINF_SUCCESS;
1698 }
1699 }
1700 }
1701 }
1702 RTMemTmpFree(pLoadReq);
1703 }
1704 else
1705 {
1706 AssertMsgFailed(("failed to allocated %d bytes for SUPLDRLOAD_IN structure!\n", SUP_IOCTL_LDR_LOAD_SIZE(cbImage)));
1707 rc = VERR_NO_TMP_MEMORY;
1708 }
1709 }
1710 else if (RT_SUCCESS(rc))
1711 {
1712 if (fIsVMMR0)
1713 g_pvVMMR0 = OpenReq.u.Out.pvImageBase;
1714 LogRel(("SUP: Opened %s (%s) at %#p.\n", pszModule, pszFilename, OpenReq.u.Out.pvImageBase));
1715#ifdef RT_OS_WINDOWS
1716 LogRel(("SUP: windbg> .reload /f %s=%#p\n", pszFilename, OpenReq.u.Out.pvImageBase));
1717#endif
1718 }
1719 }
1720 RTLdrClose(hLdrMod);
1721 return rc;
1722}
1723
1724
1725SUPR3DECL(int) SUPFreeModule(void *pvImageBase)
1726{
1727 /* fake */
1728 if (RT_UNLIKELY(g_u32FakeMode))
1729 {
1730 g_pvVMMR0 = NIL_RTR0PTR;
1731 return VINF_SUCCESS;
1732 }
1733
1734 /*
1735 * Free the requested module.
1736 */
1737 SUPLDRFREE Req;
1738 Req.Hdr.u32Cookie = g_u32Cookie;
1739 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1740 Req.Hdr.cbIn = SUP_IOCTL_LDR_FREE_SIZE_IN;
1741 Req.Hdr.cbOut = SUP_IOCTL_LDR_FREE_SIZE_OUT;
1742 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1743 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1744 Req.u.In.pvImageBase = (RTR0PTR)pvImageBase;
1745 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_FREE, &Req, SUP_IOCTL_LDR_FREE_SIZE);
1746 if (RT_SUCCESS(rc))
1747 rc = Req.Hdr.rc;
1748 if ( RT_SUCCESS(rc)
1749 && (RTR0PTR)pvImageBase == g_pvVMMR0)
1750 g_pvVMMR0 = NIL_RTR0PTR;
1751 return rc;
1752}
1753
1754
1755SUPR3DECL(int) SUPGetSymbolR0(void *pvImageBase, const char *pszSymbol, void **ppvValue)
1756{
1757 *ppvValue = NULL;
1758
1759 /* fake */
1760 if (RT_UNLIKELY(g_u32FakeMode))
1761 {
1762 *ppvValue = (void *)(uintptr_t)0xdeadf00d;
1763 return VINF_SUCCESS;
1764 }
1765
1766 /*
1767 * Do ioctl.
1768 */
1769 SUPLDRGETSYMBOL Req;
1770 Req.Hdr.u32Cookie = g_u32Cookie;
1771 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1772 Req.Hdr.cbIn = SUP_IOCTL_LDR_GET_SYMBOL_SIZE_IN;
1773 Req.Hdr.cbOut = SUP_IOCTL_LDR_GET_SYMBOL_SIZE_OUT;
1774 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1775 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1776 Req.u.In.pvImageBase = (RTR0PTR)pvImageBase;
1777 size_t cchSymbol = strlen(pszSymbol);
1778 if (cchSymbol >= sizeof(Req.u.In.szSymbol))
1779 return VERR_SYMBOL_NOT_FOUND;
1780 memcpy(Req.u.In.szSymbol, pszSymbol, cchSymbol + 1);
1781 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_GET_SYMBOL, &Req, SUP_IOCTL_LDR_GET_SYMBOL_SIZE);
1782 if (RT_SUCCESS(rc))
1783 rc = Req.Hdr.rc;
1784 if (RT_SUCCESS(rc))
1785 *ppvValue = (void *)Req.u.Out.pvSymbol;
1786 return rc;
1787}
1788
1789
1790SUPR3DECL(int) SUPLoadVMM(const char *pszFilename)
1791{
1792 void *pvImageBase;
1793 return SUPLoadModule(pszFilename, "VMMR0.r0", &pvImageBase);
1794}
1795
1796
1797SUPR3DECL(int) SUPUnloadVMM(void)
1798{
1799 return SUPFreeModule((void*)g_pvVMMR0);
1800}
1801
1802
1803SUPR3DECL(int) SUPGipGetPhys(PRTHCPHYS pHCPhys)
1804{
1805 if (g_pSUPGlobalInfoPage)
1806 {
1807 *pHCPhys = g_HCPhysSUPGlobalInfoPage;
1808 return VINF_SUCCESS;
1809 }
1810 *pHCPhys = NIL_RTHCPHYS;
1811 return VERR_WRONG_ORDER;
1812}
1813
1814
1815/**
1816 * Worker for SUPR3HardenedLdrLoad and SUPR3HardenedLdrLoadAppPriv.
1817 *
1818 * @returns iprt status code.
1819 * @param pszFilename The full file name.
1820 * @param phLdrMod Where to store the handle to the loaded module.
1821 */
1822static int supR3HardenedLdrLoadIt(const char *pszFilename, PRTLDRMOD phLdrMod)
1823{
1824#ifdef VBOX_WITH_HARDENING
1825 /*
1826 * Verify the image file.
1827 */
1828 int rc = supR3HardenedVerifyFile(pszFilename, false /* fFatal */);
1829 if (RT_FAILURE(rc))
1830 {
1831 LogRel(("supR3HardenedLdrLoadIt: Verification of \"%s\" failed, rc=%Rrc\n", pszFilename, rc));
1832 return rc;
1833 }
1834#endif
1835
1836 /*
1837 * Try load it.
1838 */
1839 return RTLdrLoad(pszFilename, phLdrMod);
1840}
1841
1842
1843SUPR3DECL(int) SUPR3HardenedLdrLoad(const char *pszFilename, PRTLDRMOD phLdrMod)
1844{
1845 /*
1846 * Validate input.
1847 */
1848 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
1849 AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
1850 *phLdrMod = NIL_RTLDRMOD;
1851 AssertReturn(RTPathHavePath(pszFilename), VERR_INVALID_PARAMETER);
1852
1853 /*
1854 * Add the default extension if it's missing.
1855 */
1856 if (!RTPathHaveExt(pszFilename))
1857 {
1858 const char *pszSuff = RTLdrGetSuff();
1859 size_t cchSuff = strlen(pszSuff);
1860 size_t cchFilename = strlen(pszFilename);
1861 char *psz = (char *)alloca(cchFilename + cchSuff + 1);
1862 AssertReturn(psz, VERR_NO_TMP_MEMORY);
1863 memcpy(psz, pszFilename, cchFilename);
1864 memcpy(psz + cchFilename, pszSuff, cchSuff + 1);
1865 pszFilename = psz;
1866 }
1867
1868 /*
1869 * Pass it on to the common library loader.
1870 */
1871 return supR3HardenedLdrLoadIt(pszFilename, phLdrMod);
1872}
1873
1874
1875SUPR3DECL(int) SUPR3HardenedLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod)
1876{
1877 LogFlow(("SUPR3HardenedLdrLoadAppPriv: pszFilename=%p:{%s} phLdrMod=%p\n", pszFilename, pszFilename, phLdrMod));
1878
1879 /*
1880 * Validate input.
1881 */
1882 AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
1883 *phLdrMod = NIL_RTLDRMOD;
1884 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
1885 AssertMsgReturn(!RTPathHavePath(pszFilename), ("%s\n", pszFilename), VERR_INVALID_PARAMETER);
1886
1887 /*
1888 * Check the filename.
1889 */
1890 size_t cchFilename = strlen(pszFilename);
1891 AssertMsgReturn(cchFilename < (RTPATH_MAX / 4) * 3, ("%zu\n", cchFilename), VERR_INVALID_PARAMETER);
1892
1893 const char *pszExt = "";
1894 size_t cchExt = 0;
1895 if (!RTPathHaveExt(pszFilename))
1896 {
1897 pszExt = RTLdrGetSuff();
1898 cchExt = strlen(pszExt);
1899 }
1900
1901 /*
1902 * Construct the private arch path and check if the file exists.
1903 */
1904 char szPath[RTPATH_MAX];
1905 int rc = RTPathAppPrivateArch(szPath, sizeof(szPath) - 1 - cchExt - cchFilename);
1906 AssertRCReturn(rc, rc);
1907
1908 char *psz = strchr(szPath, '\0');
1909 *psz++ = RTPATH_SLASH;
1910 memcpy(psz, pszFilename, cchFilename);
1911 psz += cchFilename;
1912 memcpy(psz, pszExt, cchExt + 1);
1913
1914 if (!RTPathExists(szPath))
1915 {
1916 LogRel(("SUPR3HardenedLdrLoadAppPriv: \"%s\" not found\n", szPath));
1917 return VERR_FILE_NOT_FOUND;
1918 }
1919
1920 /*
1921 * Pass it on to SUPR3HardenedLdrLoad.
1922 */
1923 rc = SUPR3HardenedLdrLoad(szPath, phLdrMod);
1924
1925 LogFlow(("SUPR3HardenedLdrLoadAppPriv: returns %Rrc\n", rc));
1926 return rc;
1927}
1928
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