VirtualBox

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

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

SUP: SUPInit(ppSession=NULL, cbReserved=0) -> SUPR3Init(ppSession)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 63.7 KB
Line 
1/* $Id: SUPLib.cpp 11794 2008-08-29 09:13:37Z 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/*******************************************************************************
51* Header Files *
52*******************************************************************************/
53#define LOG_GROUP LOG_GROUP_SUP
54#include <VBox/sup.h>
55#include <VBox/err.h>
56#include <VBox/param.h>
57#include <VBox/vmm.h>
58#include <VBox/log.h>
59#include <VBox/x86.h>
60
61#include <iprt/assert.h>
62#include <iprt/alloc.h>
63#include <iprt/alloca.h>
64#include <iprt/ldr.h>
65#include <iprt/asm.h>
66#include <iprt/mp.h>
67#include <iprt/cpuset.h>
68#include <iprt/thread.h>
69#include <iprt/process.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 unsigned 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 = 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 = 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 = 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 unsigned 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 = 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) SUPLoadModule(const char *pszFilename, const char *pszModule, void **ppvImageBase)
1144{
1145 /*
1146 * Load the module.
1147 * If it's VMMR0.r0 we need to install the IDTE.
1148 */
1149 int rc = supLoadModule(pszFilename, pszModule, ppvImageBase);
1150#ifdef VBOX_WITH_IDT_PATCHING
1151 if ( RT_SUCCESS(rc)
1152 && !strcmp(pszModule, "VMMR0.r0"))
1153 {
1154 rc = supInstallIDTE();
1155 if (RT_FAILURE(rc))
1156 SUPFreeModule(*ppvImageBase);
1157 }
1158#endif /* VBOX_WITH_IDT_PATCHING */
1159
1160 return rc;
1161}
1162
1163
1164#ifdef VBOX_WITH_IDT_PATCHING
1165/**
1166 * Generates the code for calling the interrupt gate.
1167 *
1168 * @returns VBox status code.
1169 * g_pfnCallVMMR0 is changed on success.
1170 * @param u8Interrupt The interrupt number.
1171 */
1172static int suplibGenerateCallVMMR0(uint8_t u8Interrupt)
1173{
1174 /*
1175 * Allocate memory.
1176 */
1177 uint8_t *pb = (uint8_t *)RTMemExecAlloc(256);
1178 AssertReturn(pb, VERR_NO_MEMORY);
1179 memset(pb, 0xcc, 256);
1180 Assert(!g_pfnCallVMMR0);
1181 g_pfnCallVMMR0 = *(PFNCALLVMMR0*)&pb;
1182
1183 /*
1184 * Generate the code.
1185 */
1186#ifdef RT_ARCH_AMD64
1187 /*
1188 * reg params:
1189 * <GCC> <MSC> <argument>
1190 * rdi rcx pVMR0
1191 * esi edx uOperation
1192 * rdx r8 pvArg
1193 *
1194 * eax eax [g_u32Gookie]
1195 */
1196 *pb++ = 0xb8; /* mov eax, <g_u32Cookie> */
1197 *(uint32_t *)pb = g_u32Cookie;
1198 pb += sizeof(uint32_t);
1199
1200 *pb++ = 0xcd; /* int <u8Interrupt> */
1201 *pb++ = u8Interrupt;
1202
1203 *pb++ = 0xc3; /* ret */
1204
1205#else
1206 /*
1207 * x86 stack:
1208 * 0 saved esi
1209 * 0 4 ret
1210 * 4 8 pVM
1211 * 8 c uOperation
1212 * c 10 pvArg
1213 */
1214 *pb++ = 0x56; /* push esi */
1215
1216 *pb++ = 0x8b; /* mov eax, [pVM] */
1217 *pb++ = 0x44;
1218 *pb++ = 0x24;
1219 *pb++ = 0x08; /* esp+08h */
1220
1221 *pb++ = 0x8b; /* mov edx, [uOperation] */
1222 *pb++ = 0x54;
1223 *pb++ = 0x24;
1224 *pb++ = 0x0c; /* esp+0ch */
1225
1226 *pb++ = 0x8b; /* mov ecx, [pvArg] */
1227 *pb++ = 0x4c;
1228 *pb++ = 0x24;
1229 *pb++ = 0x10; /* esp+10h */
1230
1231 *pb++ = 0xbe; /* mov esi, <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++ = 0x5e; /* pop esi */
1239
1240 *pb++ = 0xc3; /* ret */
1241#endif
1242
1243 return VINF_SUCCESS;
1244}
1245
1246
1247/**
1248 * Installs the IDTE patch.
1249 *
1250 * @return VBox status code.
1251 */
1252static int supInstallIDTE(void)
1253{
1254 /* already installed? */
1255 if (g_u8Interrupt != 3 || g_u32FakeMode)
1256 return VINF_SUCCESS;
1257
1258 int rc = VINF_SUCCESS;
1259 const RTCPUID cCpus = RTMpGetCount();
1260 if (cCpus <= 1)
1261 {
1262 /* UNI */
1263 SUPIDTINSTALL Req;
1264 Req.Hdr.u32Cookie = g_u32Cookie;
1265 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1266 Req.Hdr.cbIn = SUP_IOCTL_IDT_INSTALL_SIZE_IN;
1267 Req.Hdr.cbOut = SUP_IOCTL_IDT_INSTALL_SIZE_OUT;
1268 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1269 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1270 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_IDT_INSTALL, &Req, SUP_IOCTL_IDT_INSTALL_SIZE);
1271 if (RT_SUCCESS(rc))
1272 rc = Req.Hdr.rc;
1273 if (RT_SUCCESS(rc))
1274 {
1275 g_u8Interrupt = Req.u.Out.u8Idt;
1276 rc = suplibGenerateCallVMMR0(Req.u.Out.u8Idt);
1277 }
1278 }
1279 else
1280 {
1281 /* SMP */
1282 uint64_t u64AffMaskSaved = RTThreadGetAffinity();
1283 RTCPUSET OnlineSet;
1284 uint64_t u64AffMaskPatched = RTCpuSetToU64(RTMpGetOnlineSet(&OnlineSet)) & u64AffMaskSaved;
1285 unsigned cCpusPatched = 0;
1286 AssertLogRelReturn(cCpus < 64, VERR_INTERNAL_ERROR);
1287
1288 for (int i = 0; i < 64; i++)
1289 {
1290 /* Skip absent and inactive processors. */
1291 uint64_t u64Mask = 1ULL << i;
1292 if (!(u64Mask & u64AffMaskPatched))
1293 continue;
1294
1295 /* Change CPU */
1296 int rc2 = RTThreadSetAffinity(u64Mask);
1297 if (RT_FAILURE(rc2))
1298 {
1299 u64AffMaskPatched &= ~u64Mask;
1300 LogRel(("SUPLoadVMM: Failed to set affinity to cpu no. %d, rc=%Vrc.\n", i, rc2));
1301 continue;
1302 }
1303
1304 /* Patch the CPU. */
1305 SUPIDTINSTALL Req;
1306 Req.Hdr.u32Cookie = g_u32Cookie;
1307 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1308 Req.Hdr.cbIn = SUP_IOCTL_IDT_INSTALL_SIZE_IN;
1309 Req.Hdr.cbOut = SUP_IOCTL_IDT_INSTALL_SIZE_OUT;
1310 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1311 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1312 rc2 = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_IDT_INSTALL, &Req, SUP_IOCTL_IDT_INSTALL_SIZE);
1313 if (RT_SUCCESS(rc2))
1314 rc2 = Req.Hdr.rc;
1315 if (RT_SUCCESS(rc2))
1316 {
1317 if (!cCpusPatched)
1318 {
1319 g_u8Interrupt = Req.u.Out.u8Idt;
1320 rc2 = suplibGenerateCallVMMR0(Req.u.Out.u8Idt);
1321 if (RT_FAILURE(rc2))
1322 {
1323 LogRel(("suplibGenerateCallVMMR0 failed with rc=%Vrc.\n", i, rc2));
1324 rc = rc2;
1325 }
1326 }
1327 else
1328 Assert(g_u8Interrupt == Req.u.Out.u8Idt);
1329 cCpusPatched++;
1330 }
1331 else
1332 {
1333
1334 LogRel(("SUPLoadVMM: Failed to patch cpu no. %d, rc=%Vrc.\n", i, rc2));
1335 if (RT_SUCCESS(rc))
1336 rc = rc2;
1337 }
1338 }
1339
1340 /* Fail if no CPUs was patched! */
1341 if (RT_SUCCESS(rc) && cCpusPatched <= 0)
1342 rc = VERR_GENERAL_FAILURE;
1343 /* Ignore failures if a CPU was patched. */
1344 else if (RT_FAILURE(rc) && cCpusPatched > 0)
1345 rc = VINF_SUCCESS;
1346
1347 /* Set/restore the thread affinity. */
1348 if (RT_SUCCESS(rc))
1349 {
1350 rc = RTThreadSetAffinity(u64AffMaskPatched);
1351 AssertRC(rc);
1352 }
1353 else
1354 {
1355 int rc2 = RTThreadSetAffinity(u64AffMaskSaved);
1356 AssertRC(rc2);
1357 }
1358 }
1359 return rc;
1360}
1361#endif /* VBOX_WITH_IDT_PATCHING */
1362
1363
1364/**
1365 * Resolve an external symbol during RTLdrGetBits().
1366 *
1367 * @returns VBox status code.
1368 * @param hLdrMod The loader module handle.
1369 * @param pszModule Module name.
1370 * @param pszSymbol Symbol name, NULL if uSymbol should be used.
1371 * @param uSymbol Symbol ordinal, ~0 if pszSymbol should be used.
1372 * @param pValue Where to store the symbol value (address).
1373 * @param pvUser User argument.
1374 */
1375static DECLCALLBACK(int) supLoadModuleResolveImport(RTLDRMOD hLdrMod, const char *pszModule,
1376 const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser)
1377{
1378 AssertPtr(pValue);
1379 AssertPtr(pvUser);
1380
1381 /*
1382 * Only SUPR0 and VMMR0.r0
1383 */
1384 if ( pszModule
1385 && *pszModule
1386 && strcmp(pszModule, "SUPR0.dll")
1387 && strcmp(pszModule, "VMMR0.r0"))
1388 {
1389 AssertMsgFailed(("%s is importing from %s! (expected 'SUPR0.dll' or 'VMMR0.r0', case-sensitiv)\n", pvUser, pszModule));
1390 return VERR_SYMBOL_NOT_FOUND;
1391 }
1392
1393 /*
1394 * No ordinals.
1395 */
1396 if (pszSymbol < (const char*)0x10000)
1397 {
1398 AssertMsgFailed(("%s is importing by ordinal (ord=%d)\n", pvUser, (int)(uintptr_t)pszSymbol));
1399 return VERR_SYMBOL_NOT_FOUND;
1400 }
1401
1402 /*
1403 * Lookup symbol.
1404 */
1405 /* skip the 64-bit ELF import prefix first. */
1406 if (!strncmp(pszSymbol, "SUPR0$", sizeof("SUPR0$") - 1))
1407 pszSymbol += sizeof("SUPR0$") - 1;
1408
1409 /*
1410 * Check the VMMR0.r0 module if loaded.
1411 */
1412 /** @todo call the SUPLoadModule caller.... */
1413 /** @todo proper reference counting and such. */
1414 if (g_pvVMMR0 != NIL_RTR0PTR)
1415 {
1416 void *pvValue;
1417 if (!SUPGetSymbolR0((void *)g_pvVMMR0, pszSymbol, &pvValue))
1418 {
1419 *pValue = (uintptr_t)pvValue;
1420 return VINF_SUCCESS;
1421 }
1422 }
1423
1424 /* iterate the function table. */
1425 int c = g_pFunctions->u.Out.cFunctions;
1426 PSUPFUNC pFunc = &g_pFunctions->u.Out.aFunctions[0];
1427 while (c-- > 0)
1428 {
1429 if (!strcmp(pFunc->szName, pszSymbol))
1430 {
1431 *pValue = (uintptr_t)pFunc->pfn;
1432 return VINF_SUCCESS;
1433 }
1434 pFunc++;
1435 }
1436
1437 /*
1438 * The GIP.
1439 */
1440 /** @todo R0 mapping? */
1441 if ( pszSymbol
1442 && g_pSUPGlobalInfoPage
1443 && g_pSUPGlobalInfoPageR0
1444 && !strcmp(pszSymbol, "g_SUPGlobalInfoPage"))
1445 {
1446 *pValue = (uintptr_t)g_pSUPGlobalInfoPageR0;
1447 return VINF_SUCCESS;
1448 }
1449
1450 /*
1451 * Despair.
1452 */
1453 c = g_pFunctions->u.Out.cFunctions;
1454 pFunc = &g_pFunctions->u.Out.aFunctions[0];
1455 while (c-- > 0)
1456 {
1457 AssertMsg2("%d: %s\n", g_pFunctions->u.Out.cFunctions - c, pFunc->szName);
1458 pFunc++;
1459 }
1460
1461 AssertMsg2("%s is importing %s which we couldn't find\n", pvUser, pszSymbol);
1462 AssertMsgFailed(("%s is importing %s which we couldn't find\n", pvUser, pszSymbol));
1463 if (g_u32FakeMode)
1464 {
1465 *pValue = 0xdeadbeef;
1466 return VINF_SUCCESS;
1467 }
1468 return VERR_SYMBOL_NOT_FOUND;
1469}
1470
1471
1472/** Argument package for supLoadModuleCalcSizeCB. */
1473typedef struct SUPLDRCALCSIZEARGS
1474{
1475 size_t cbStrings;
1476 uint32_t cSymbols;
1477 size_t cbImage;
1478} SUPLDRCALCSIZEARGS, *PSUPLDRCALCSIZEARGS;
1479
1480/**
1481 * Callback used to calculate the image size.
1482 * @return VINF_SUCCESS
1483 */
1484static DECLCALLBACK(int) supLoadModuleCalcSizeCB(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
1485{
1486 PSUPLDRCALCSIZEARGS pArgs = (PSUPLDRCALCSIZEARGS)pvUser;
1487 if ( pszSymbol != NULL
1488 && *pszSymbol
1489 && Value <= pArgs->cbImage)
1490 {
1491 pArgs->cSymbols++;
1492 pArgs->cbStrings += strlen(pszSymbol) + 1;
1493 }
1494 return VINF_SUCCESS;
1495}
1496
1497
1498/** Argument package for supLoadModuleCreateTabsCB. */
1499typedef struct SUPLDRCREATETABSARGS
1500{
1501 size_t cbImage;
1502 PSUPLDRSYM pSym;
1503 char *pszBase;
1504 char *psz;
1505} SUPLDRCREATETABSARGS, *PSUPLDRCREATETABSARGS;
1506
1507/**
1508 * Callback used to calculate the image size.
1509 * @return VINF_SUCCESS
1510 */
1511static DECLCALLBACK(int) supLoadModuleCreateTabsCB(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
1512{
1513 PSUPLDRCREATETABSARGS pArgs = (PSUPLDRCREATETABSARGS)pvUser;
1514 if ( pszSymbol != NULL
1515 && *pszSymbol
1516 && Value <= pArgs->cbImage)
1517 {
1518 pArgs->pSym->offSymbol = (uint32_t)Value;
1519 pArgs->pSym->offName = pArgs->psz - pArgs->pszBase;
1520 pArgs->pSym++;
1521
1522 size_t cbCopy = strlen(pszSymbol) + 1;
1523 memcpy(pArgs->psz, pszSymbol, cbCopy);
1524 pArgs->psz += cbCopy;
1525 }
1526 return VINF_SUCCESS;
1527}
1528
1529
1530/**
1531 * Worker for SUPLoadModule().
1532 *
1533 * @returns VBox status code.
1534 * @param pszFilename Name of the VMMR0 image file
1535 */
1536static int supLoadModule(const char *pszFilename, const char *pszModule, void **ppvImageBase)
1537{
1538 /*
1539 * Validate input.
1540 */
1541 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
1542 AssertPtrReturn(pszModule, VERR_INVALID_PARAMETER);
1543 AssertPtrReturn(ppvImageBase, VERR_INVALID_PARAMETER);
1544 AssertReturn(strlen(pszModule) < RT_SIZEOFMEMB(SUPLDROPEN, u.In.szName), VERR_FILENAME_TOO_LONG);
1545
1546 const bool fIsVMMR0 = !strcmp(pszModule, "VMMR0.r0");
1547 *ppvImageBase = NULL;
1548
1549 /*
1550 * Open image file and figure its size.
1551 */
1552 RTLDRMOD hLdrMod;
1553 int rc = RTLdrOpen(pszFilename, &hLdrMod);
1554 if (!RT_SUCCESS(rc))
1555 return rc;
1556
1557 SUPLDRCALCSIZEARGS CalcArgs;
1558 CalcArgs.cbStrings = 0;
1559 CalcArgs.cSymbols = 0;
1560 CalcArgs.cbImage = RTLdrSize(hLdrMod);
1561 rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCalcSizeCB, &CalcArgs);
1562 if (RT_SUCCESS(rc))
1563 {
1564 const uint32_t offSymTab = RT_ALIGN_32(CalcArgs.cbImage, 8);
1565 const uint32_t offStrTab = offSymTab + CalcArgs.cSymbols * sizeof(SUPLDRSYM);
1566 const uint32_t cbImage = RT_ALIGN_32(offStrTab + CalcArgs.cbStrings, 8);
1567
1568 /*
1569 * Open the R0 image.
1570 */
1571 SUPLDROPEN OpenReq;
1572 OpenReq.Hdr.u32Cookie = g_u32Cookie;
1573 OpenReq.Hdr.u32SessionCookie = g_u32SessionCookie;
1574 OpenReq.Hdr.cbIn = SUP_IOCTL_LDR_OPEN_SIZE_IN;
1575 OpenReq.Hdr.cbOut = SUP_IOCTL_LDR_OPEN_SIZE_OUT;
1576 OpenReq.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1577 OpenReq.Hdr.rc = VERR_INTERNAL_ERROR;
1578 OpenReq.u.In.cbImage = cbImage;
1579 strcpy(OpenReq.u.In.szName, pszModule);
1580 if (!g_u32FakeMode)
1581 {
1582 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_OPEN, &OpenReq, SUP_IOCTL_LDR_OPEN_SIZE);
1583 if (RT_SUCCESS(rc))
1584 rc = OpenReq.Hdr.rc;
1585 }
1586 else
1587 {
1588 OpenReq.u.Out.fNeedsLoading = true;
1589 OpenReq.u.Out.pvImageBase = 0xef423420;
1590 }
1591 *ppvImageBase = (void *)OpenReq.u.Out.pvImageBase;
1592 if ( RT_SUCCESS(rc)
1593 && OpenReq.u.Out.fNeedsLoading)
1594 {
1595 /*
1596 * We need to load it.
1597 * Allocate memory for the image bits.
1598 */
1599 PSUPLDRLOAD pLoadReq = (PSUPLDRLOAD)RTMemTmpAlloc(SUP_IOCTL_LDR_LOAD_SIZE(cbImage));
1600 if (pLoadReq)
1601 {
1602 /*
1603 * Get the image bits.
1604 */
1605 rc = RTLdrGetBits(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase,
1606 supLoadModuleResolveImport, (void *)pszModule);
1607
1608 if (RT_SUCCESS(rc))
1609 {
1610 /*
1611 * Get the entry points.
1612 */
1613 RTUINTPTR VMMR0EntryInt = 0;
1614 RTUINTPTR VMMR0EntryFast = 0;
1615 RTUINTPTR VMMR0EntryEx = 0;
1616 RTUINTPTR ModuleInit = 0;
1617 RTUINTPTR ModuleTerm = 0;
1618 if (fIsVMMR0)
1619 {
1620 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "VMMR0EntryInt", &VMMR0EntryInt);
1621 if (RT_SUCCESS(rc))
1622 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "VMMR0EntryFast", &VMMR0EntryFast);
1623 if (RT_SUCCESS(rc))
1624 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "VMMR0EntryEx", &VMMR0EntryEx);
1625 }
1626 if (RT_SUCCESS(rc))
1627 {
1628 int rc2 = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "ModuleInit", &ModuleInit);
1629 if (RT_FAILURE(rc2))
1630 ModuleInit = 0;
1631
1632 rc2 = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.achImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "ModuleTerm", &ModuleTerm);
1633 if (RT_FAILURE(rc2))
1634 ModuleTerm = 0;
1635 }
1636 if (RT_SUCCESS(rc))
1637 {
1638 /*
1639 * Create the symbol and string tables.
1640 */
1641 SUPLDRCREATETABSARGS CreateArgs;
1642 CreateArgs.cbImage = CalcArgs.cbImage;
1643 CreateArgs.pSym = (PSUPLDRSYM)&pLoadReq->u.In.achImage[offSymTab];
1644 CreateArgs.pszBase = (char *)&pLoadReq->u.In.achImage[offStrTab];
1645 CreateArgs.psz = CreateArgs.pszBase;
1646 rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCreateTabsCB, &CreateArgs);
1647 if (RT_SUCCESS(rc))
1648 {
1649 AssertRelease((size_t)(CreateArgs.psz - CreateArgs.pszBase) <= CalcArgs.cbStrings);
1650 AssertRelease((size_t)(CreateArgs.pSym - (PSUPLDRSYM)&pLoadReq->u.In.achImage[offSymTab]) <= CalcArgs.cSymbols);
1651
1652 /*
1653 * Upload the image.
1654 */
1655 pLoadReq->Hdr.u32Cookie = g_u32Cookie;
1656 pLoadReq->Hdr.u32SessionCookie = g_u32SessionCookie;
1657 pLoadReq->Hdr.cbIn = SUP_IOCTL_LDR_LOAD_SIZE_IN(cbImage);
1658 pLoadReq->Hdr.cbOut = SUP_IOCTL_LDR_LOAD_SIZE_OUT;
1659 pLoadReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_IN;
1660 pLoadReq->Hdr.rc = VERR_INTERNAL_ERROR;
1661
1662 pLoadReq->u.In.pfnModuleInit = (RTR0PTR)ModuleInit;
1663 pLoadReq->u.In.pfnModuleTerm = (RTR0PTR)ModuleTerm;
1664 if (fIsVMMR0)
1665 {
1666 pLoadReq->u.In.eEPType = SUPLDRLOADEP_VMMR0;
1667 pLoadReq->u.In.EP.VMMR0.pvVMMR0 = OpenReq.u.Out.pvImageBase;
1668 pLoadReq->u.In.EP.VMMR0.pvVMMR0EntryInt = (RTR0PTR)VMMR0EntryInt;
1669 pLoadReq->u.In.EP.VMMR0.pvVMMR0EntryFast= (RTR0PTR)VMMR0EntryFast;
1670 pLoadReq->u.In.EP.VMMR0.pvVMMR0EntryEx = (RTR0PTR)VMMR0EntryEx;
1671 }
1672 else
1673 pLoadReq->u.In.eEPType = SUPLDRLOADEP_NOTHING;
1674 pLoadReq->u.In.offStrTab = offStrTab;
1675 pLoadReq->u.In.cbStrTab = (uint32_t)CalcArgs.cbStrings;
1676 AssertRelease(pLoadReq->u.In.cbStrTab == CalcArgs.cbStrings);
1677 pLoadReq->u.In.offSymbols = offSymTab;
1678 pLoadReq->u.In.cSymbols = CalcArgs.cSymbols;
1679 pLoadReq->u.In.cbImage = cbImage;
1680 pLoadReq->u.In.pvImageBase = OpenReq.u.Out.pvImageBase;
1681 if (!g_u32FakeMode)
1682 {
1683 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_LOAD, pLoadReq, SUP_IOCTL_LDR_LOAD_SIZE(cbImage));
1684 if (RT_SUCCESS(rc))
1685 rc = pLoadReq->Hdr.rc;
1686 }
1687 else
1688 rc = VINF_SUCCESS;
1689 if ( RT_SUCCESS(rc)
1690 || rc == VERR_ALREADY_LOADED /* A competing process. */
1691 )
1692 {
1693 LogRel(("SUP: Loaded %s (%s) at %#p - ModuleInit at %RTptr and ModuleTerm at %RTptr\n", pszModule, pszFilename,
1694 OpenReq.u.Out.pvImageBase, ModuleInit, ModuleTerm));
1695 if (fIsVMMR0)
1696 {
1697 g_pvVMMR0 = OpenReq.u.Out.pvImageBase;
1698 LogRel(("SUP: VMMR0EntryEx located at %RTptr, VMMR0EntryFast at %RTptr and VMMR0EntryInt at %RTptr\n",
1699 VMMR0EntryEx, VMMR0EntryFast, VMMR0EntryInt));
1700 }
1701#ifdef RT_OS_WINDOWS
1702 LogRel(("SUP: windbg> .reload /f %s=%#p\n", pszFilename, OpenReq.u.Out.pvImageBase));
1703#endif
1704
1705 RTMemTmpFree(pLoadReq);
1706 RTLdrClose(hLdrMod);
1707 return VINF_SUCCESS;
1708 }
1709 }
1710 }
1711 }
1712 RTMemTmpFree(pLoadReq);
1713 }
1714 else
1715 {
1716 AssertMsgFailed(("failed to allocated %d bytes for SUPLDRLOAD_IN structure!\n", SUP_IOCTL_LDR_LOAD_SIZE(cbImage)));
1717 rc = VERR_NO_TMP_MEMORY;
1718 }
1719 }
1720 else if (RT_SUCCESS(rc))
1721 {
1722 if (fIsVMMR0)
1723 g_pvVMMR0 = OpenReq.u.Out.pvImageBase;
1724 LogRel(("SUP: Opened %s (%s) at %#p.\n", pszModule, pszFilename, OpenReq.u.Out.pvImageBase));
1725#ifdef RT_OS_WINDOWS
1726 LogRel(("SUP: windbg> .reload /f %s=%#p\n", pszFilename, OpenReq.u.Out.pvImageBase));
1727#endif
1728 }
1729 }
1730 RTLdrClose(hLdrMod);
1731 return rc;
1732}
1733
1734
1735SUPR3DECL(int) SUPFreeModule(void *pvImageBase)
1736{
1737 /* fake */
1738 if (RT_UNLIKELY(g_u32FakeMode))
1739 {
1740#ifdef VBOX_WITH_IDT_PATCHING
1741 g_u8Interrupt = 3;
1742 RTMemExecFree(*(void **)&g_pfnCallVMMR0);
1743 g_pfnCallVMMR0 = NULL;
1744#endif
1745 g_pvVMMR0 = NIL_RTR0PTR;
1746 return VINF_SUCCESS;
1747 }
1748
1749#ifdef VBOX_WITH_IDT_PATCHING
1750 /*
1751 * There is one special module. When this is freed we'll
1752 * free the IDT entry that goes with it.
1753 *
1754 * Note that we don't keep count of VMMR0.r0 loads here, so the
1755 * first unload will free it.
1756 */
1757 if ( (RTR0PTR)pvImageBase == g_pvVMMR0
1758 && g_u8Interrupt != 3)
1759 {
1760 SUPIDTREMOVE Req;
1761 Req.Hdr.u32Cookie = g_u32Cookie;
1762 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1763 Req.Hdr.cbIn = SUP_IOCTL_IDT_REMOVE_SIZE_IN;
1764 Req.Hdr.cbOut = SUP_IOCTL_IDT_REMOVE_SIZE_OUT;
1765 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1766 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1767 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_IDT_REMOVE, &Req, SUP_IOCTL_IDT_REMOVE_SIZE);
1768 if (RT_SUCCESS(rc))
1769 rc = Req.Hdr.rc;
1770 AssertRC(rc);
1771 g_u8Interrupt = 3;
1772 RTMemExecFree(*(void **)&g_pfnCallVMMR0);
1773 g_pfnCallVMMR0 = NULL;
1774 }
1775#endif /* VBOX_WITH_IDT_PATCHING */
1776
1777 /*
1778 * Free the requested module.
1779 */
1780 SUPLDRFREE Req;
1781 Req.Hdr.u32Cookie = g_u32Cookie;
1782 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1783 Req.Hdr.cbIn = SUP_IOCTL_LDR_FREE_SIZE_IN;
1784 Req.Hdr.cbOut = SUP_IOCTL_LDR_FREE_SIZE_OUT;
1785 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1786 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1787 Req.u.In.pvImageBase = (RTR0PTR)pvImageBase;
1788 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_FREE, &Req, SUP_IOCTL_LDR_FREE_SIZE);
1789 if (RT_SUCCESS(rc))
1790 rc = Req.Hdr.rc;
1791 if ( RT_SUCCESS(rc)
1792 && (RTR0PTR)pvImageBase == g_pvVMMR0)
1793 g_pvVMMR0 = NIL_RTR0PTR;
1794 return rc;
1795}
1796
1797
1798SUPR3DECL(int) SUPGetSymbolR0(void *pvImageBase, const char *pszSymbol, void **ppvValue)
1799{
1800 *ppvValue = NULL;
1801
1802 /* fake */
1803 if (RT_UNLIKELY(g_u32FakeMode))
1804 {
1805 *ppvValue = (void *)(uintptr_t)0xdeadf00d;
1806 return VINF_SUCCESS;
1807 }
1808
1809 /*
1810 * Do ioctl.
1811 */
1812 SUPLDRGETSYMBOL Req;
1813 Req.Hdr.u32Cookie = g_u32Cookie;
1814 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
1815 Req.Hdr.cbIn = SUP_IOCTL_LDR_GET_SYMBOL_SIZE_IN;
1816 Req.Hdr.cbOut = SUP_IOCTL_LDR_GET_SYMBOL_SIZE_OUT;
1817 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
1818 Req.Hdr.rc = VERR_INTERNAL_ERROR;
1819 Req.u.In.pvImageBase = (RTR0PTR)pvImageBase;
1820 size_t cchSymbol = strlen(pszSymbol);
1821 if (cchSymbol >= sizeof(Req.u.In.szSymbol))
1822 return VERR_SYMBOL_NOT_FOUND;
1823 memcpy(Req.u.In.szSymbol, pszSymbol, cchSymbol + 1);
1824 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_GET_SYMBOL, &Req, SUP_IOCTL_LDR_GET_SYMBOL_SIZE);
1825 if (RT_SUCCESS(rc))
1826 rc = Req.Hdr.rc;
1827 if (RT_SUCCESS(rc))
1828 *ppvValue = (void *)Req.u.Out.pvSymbol;
1829 return rc;
1830}
1831
1832
1833SUPR3DECL(int) SUPLoadVMM(const char *pszFilename)
1834{
1835 void *pvImageBase;
1836 return SUPLoadModule(pszFilename, "VMMR0.r0", &pvImageBase);
1837}
1838
1839
1840SUPR3DECL(int) SUPUnloadVMM(void)
1841{
1842 return SUPFreeModule((void*)g_pvVMMR0);
1843}
1844
1845
1846SUPR3DECL(int) SUPGipGetPhys(PRTHCPHYS pHCPhys)
1847{
1848 if (g_pSUPGlobalInfoPage)
1849 {
1850 *pHCPhys = g_HCPhysSUPGlobalInfoPage;
1851 return VINF_SUCCESS;
1852 }
1853 *pHCPhys = NIL_RTHCPHYS;
1854 return VERR_WRONG_ORDER;
1855}
1856
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