VirtualBox

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

Last change on this file since 13073 was 12426, checked in by vboxsync, 16 years ago

SUPLib: build fix

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