VirtualBox

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

Last change on this file since 339 was 339, checked in by vboxsync, 18 years ago

Removed int3+ret blocking interrupt gate calls on Linux/GNU (amd64).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 39.3 KB
Line 
1/** @file
2 *
3 * VBox host drivers - Ring-0 support drivers - Shared code:
4 * Support library that implements the basic lowlevel OS interfaces
5 */
6
7/*
8 * Copyright (C) 2006 InnoTek Systemberatung GmbH
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.215389.xyz. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License as published by the Free Software Foundation,
14 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
15 * distribution. VirtualBox OSE is distributed in the hope that it will
16 * be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * If you received this file as part of a commercial VirtualBox
19 * distribution, then only the terms of your commercial VirtualBox
20 * license agreement apply instead of the previous paragraph.
21 */
22
23/** @page pg_sup SUP - The Support Library
24 *
25 * The support library is responsible for providing facilities to load
26 * VMM Host Ring-0 code, to call Host VMM Ring-0 code from Ring-3 Host
27 * code, and to pin down physical memory.
28 *
29 * The VMM Host Ring-0 code can be combined in the support driver if
30 * permitted by kernel module license policies. If it is not combined
31 * it will be externalized in a Win32 PE binary and will use the PDM
32 * PE loader to load it into memory.
33 *
34 * The Ring-0 calling is done thru a generic SUP interface which will
35 * tranfer an argument set and call a predefined entry point in the Host
36 * VMM Ring-0 code.
37 *
38 * See @ref grp_sup "SUP - Support APIs" for API details.
39 */
40
41
42/*******************************************************************************
43* Header Files *
44*******************************************************************************/
45#define LOG_GROUP LOG_GROUP_SUP
46#include <VBox/sup.h>
47#include <VBox/err.h>
48#include <VBox/param.h>
49#ifdef VBOX_WITHOUT_IDT_PATCHING
50# include <VBox/vmm.h>
51#endif
52#include <VBox/log.h>
53
54#include <iprt/assert.h>
55#include <iprt/alloc.h>
56#include <iprt/alloca.h>
57#include <iprt/ldr.h>
58#include <iprt/asm.h>
59#include <iprt/system.h>
60#include <iprt/thread.h>
61#include <iprt/process.h>
62#include <iprt/string.h>
63
64#include "SUPLibInternal.h"
65#include "SUPDRVIOC.h"
66
67#include <stdlib.h>
68
69
70
71/*******************************************************************************
72* Defined Constants And Macros *
73*******************************************************************************/
74/** R0 VMM module name. */
75#define VMMR0_NAME "VMMR0"
76
77
78/*******************************************************************************
79* Structures and Typedefs *
80*******************************************************************************/
81typedef DECLCALLBACK(int) FNCALLVMMR0(PVM pVM, unsigned uOperation, void *pvArg);
82typedef FNCALLVMMR0 *PFNCALLVMMR0;
83
84
85/*******************************************************************************
86* Global Variables *
87*******************************************************************************/
88/** Pointer to the Global Information Page.
89 *
90 * This pointer is valid as long as SUPLib has a open session. Anyone using
91 * the page must treat this pointer as higly volatile and not trust it beyond
92 * one transaction.
93 *
94 * @todo This will probably deserve it's own session or some other good solution...
95 */
96DECLEXPORT(PCSUPGLOBALINFOPAGE) g_pSUPGlobalInfoPage;
97/** Address of the ring-0 mapping of the GIP. */
98static PCSUPGLOBALINFOPAGE g_pSUPGlobalInfoPageR0;
99/** The physical address of the GIP. */
100static RTHCPHYS g_HCPhysSUPGlobalInfoPage = NIL_RTHCPHYS;
101
102/** The negotiated cookie. */
103uint32_t g_u32Cookie = 0;
104/** The negotiated session cookie. */
105uint32_t g_u32SessionCookie;
106/** Session handle. */
107PSUPDRVSESSION g_pSession;
108/** R0 SUP Functions used for resolving referenced to the SUPR0 module. */
109static PSUPQUERYFUNCS_OUT g_pFunctions;
110
111#ifndef VBOX_WITHOUT_IDT_PATCHING
112/** The negotiated interrupt number. */
113static uint8_t g_u8Interrupt = 3;
114/** Pointer to the generated code fore calling VMMR0. */
115static PFNCALLVMMR0 g_pfnCallVMMR0;
116#endif
117/** VMMR0 Load Address. */
118static void *g_pvVMMR0 = NULL;
119/** Init counter. */
120static unsigned g_cInits = 0;
121/** Fake mode indicator. (~0 at first, 0 or 1 after first test) */
122static uint32_t g_u32FakeMode = ~0;
123
124
125/*******************************************************************************
126* Internal Functions *
127*******************************************************************************/
128static int supLoadModule(const char *pszFilename, const char *pszModule, void **ppvImageBase);
129#ifndef VBOX_WITHOUT_IDT_PATCHING
130static int supInstallIDTE(void);
131#endif
132static DECLCALLBACK(int) supLoadModuleResolveImport(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser);
133
134
135SUPR3DECL(int) SUPInstall(void)
136{
137 return suplibOsInstall();
138}
139
140
141SUPR3DECL(int) SUPUninstall(void)
142{
143 return suplibOsUninstall();
144}
145
146
147SUPR3DECL(int) SUPInit(PSUPDRVSESSION *ppSession /* NULL */, size_t cbReserve /* 0 */)
148{
149 /*
150 * Check if already initialized.
151 */
152 if (ppSession)
153 *ppSession = g_pSession;
154 if (g_cInits++ > 0)
155 return VINF_SUCCESS;
156
157 /*
158 * Check for fake mode.
159 * Fake mode is used when we're doing smoke testing and debugging.
160 * It's also useful on platforms where we haven't root access or which
161 * we haven't ported the support driver to.
162 */
163 if (g_u32FakeMode == ~0U)
164 {
165 const char *psz = getenv("VBOX_SUPLIB_FAKE");
166 if (psz && !strcmp(psz, "fake"))
167 ASMAtomicCmpXchgU32(&g_u32FakeMode, 1, ~0U);
168 else
169 ASMAtomicCmpXchgU32(&g_u32FakeMode, 0, ~0U);
170 }
171 if (g_u32FakeMode)
172 {
173 Log(("SUP: Fake mode!\n"));
174
175 /* fake r0 functions. */
176 g_pFunctions = (PSUPQUERYFUNCS_OUT)RTMemAllocZ(RT_OFFSETOF(SUPQUERYFUNCS_OUT, aFunctions[8]));
177 if (g_pFunctions)
178 {
179 g_pFunctions->aFunctions[0].pfn = (void *)0xefefefef;
180 strcpy(g_pFunctions->aFunctions[0].szName, "SUPR0ContAlloc");
181 g_pFunctions->aFunctions[1].pfn = (void *)0xefefefdf;
182 strcpy(g_pFunctions->aFunctions[1].szName, "SUPR0ContFree");
183 g_pFunctions->aFunctions[2].pfn = (void *)0xefefefcf;
184 strcpy(g_pFunctions->aFunctions[2].szName, "SUPR0LockMem");
185 g_pFunctions->aFunctions[3].pfn = (void *)0xefefefbf;
186 strcpy(g_pFunctions->aFunctions[3].szName, "SUPR0UnlockMem");
187 g_pFunctions->aFunctions[4].pfn = (void *)0xefefefaf;
188 strcpy(g_pFunctions->aFunctions[4].szName, "SUPR0LockedAlloc");
189 g_pFunctions->aFunctions[5].pfn = (void *)0xefefef9f;
190 strcpy(g_pFunctions->aFunctions[5].szName, "SUPR0LockedFree");
191 g_pFunctions->aFunctions[6].pfn = (void *)0xefefef8f;
192 strcpy(g_pFunctions->aFunctions[6].szName, "SUPR0Printf");
193 g_pFunctions->cFunctions = 7;
194 g_pSession = (PSUPDRVSESSION)(void *)g_pFunctions;
195 if (ppSession)
196 *ppSession = g_pSession;
197#ifndef VBOX_WITHOUT_IDT_PATCHING
198 Assert(g_u8Interrupt == 3);
199#endif
200 /* fake the GIP. */
201 g_pSUPGlobalInfoPage = (PCSUPGLOBALINFOPAGE)RTMemPageAlloc(PAGE_SIZE);
202 if (g_pSUPGlobalInfoPage)
203 {
204 g_pSUPGlobalInfoPageR0 = g_pSUPGlobalInfoPage;
205 g_HCPhysSUPGlobalInfoPage = NIL_RTHCPHYS & ~(RTHCPHYS)PAGE_OFFSET_MASK;
206 /* the page is supposed to be invalid, so don't set a correct magic. */
207 return VINF_SUCCESS;
208 }
209 RTMemFree(g_pFunctions);
210 g_pFunctions = NULL;
211 }
212 return VERR_NO_MEMORY;
213 }
214
215 /**
216 * Open the support driver.
217 */
218 int rc = suplibOsInit(cbReserve);
219 if (VBOX_SUCCESS(rc))
220 {
221 /*
222 * Negotiate the cookie.
223 */
224 SUPCOOKIE_IN In;
225 SUPCOOKIE_OUT Out = {0,0};
226 strcpy(In.szMagic, SUPCOOKIE_MAGIC);
227 In.u32Version = SUPDRVIOC_VERSION;
228 rc = suplibOsIOCtl(SUP_IOCTL_COOKIE, &In, sizeof(In), &Out, sizeof(Out));
229 if (VBOX_SUCCESS(rc))
230 {
231 if (Out.u32Version == SUPDRVIOC_VERSION)
232 {
233 /*
234 * Query the functions.
235 */
236 SUPQUERYFUNCS_IN FuncsIn;
237 FuncsIn.u32Cookie = Out.u32Cookie;
238 FuncsIn.u32SessionCookie = Out.u32SessionCookie;
239 unsigned cbFuncsOut = RT_OFFSETOF(SUPQUERYFUNCS_OUT, aFunctions[Out.cFunctions]);
240 PSUPQUERYFUNCS_OUT pFuncsOut = (PSUPQUERYFUNCS_OUT)RTMemAllocZ(cbFuncsOut);
241 if (pFuncsOut)
242 {
243 rc = suplibOsIOCtl(SUP_IOCTL_QUERY_FUNCS, &FuncsIn, sizeof(FuncsIn), pFuncsOut, cbFuncsOut);
244 if (VBOX_SUCCESS(rc))
245 {
246 g_u32Cookie = Out.u32Cookie;
247 g_u32SessionCookie = Out.u32SessionCookie;
248 g_pSession = Out.pSession;
249 g_pFunctions = pFuncsOut;
250 if (ppSession)
251 *ppSession = Out.pSession;
252
253 /*
254 * Map the GIP into userspace.
255 * This is an optional feature, so we will ignore any failures here.
256 */
257 if (!g_pSUPGlobalInfoPage)
258 {
259 SUPGIPMAP_IN GipIn = {0};
260 SUPGIPMAP_OUT GipOut = {NULL, 0};
261 GipIn.u32Cookie = Out.u32Cookie;
262 GipIn.u32SessionCookie = Out.u32SessionCookie;
263 rc = suplibOsIOCtl(SUP_IOCTL_GIP_MAP, &GipIn, sizeof(GipIn), &GipOut, sizeof(GipOut));
264 if (VBOX_SUCCESS(rc))
265 {
266 ASMAtomicXchgSize(&g_HCPhysSUPGlobalInfoPage, GipOut.HCPhysGip);
267 ASMAtomicCmpXchgPtr((void * volatile *)&g_pSUPGlobalInfoPage, (void *)GipOut.pGipR3, NULL);
268 ASMAtomicCmpXchgPtr((void * volatile *)&g_pSUPGlobalInfoPageR0, (void *)GipOut.pGipR0, NULL);
269 }
270 else
271 rc = VINF_SUCCESS;
272 }
273 return rc;
274 }
275 RTMemFree(pFuncsOut);
276 }
277 else
278 rc = VERR_NO_MEMORY;
279 }
280 else
281 rc = VERR_VM_DRIVER_VERSION_MISMATCH;
282 }
283
284 suplibOsTerm();
285 }
286 AssertMsgFailed(("SUPInit() failed rc=%Vrc\n", rc));
287 g_cInits--;
288
289 return rc;
290}
291
292
293SUPR3DECL(int) SUPTerm(bool fForced)
294{
295 /*
296 * Verify state.
297 */
298 AssertMsg(g_cInits > 0, ("SUPTerm() is called before SUPInit()!\n"));
299 if (g_cInits == 0)
300 return VERR_WRONG_ORDER;
301 if (g_cInits == 1 || fForced)
302 {
303 /*
304 * NULL the GIP pointer.
305 */
306 if (g_pSUPGlobalInfoPage)
307 {
308 ASMAtomicXchgPtr((void * volatile *)&g_pSUPGlobalInfoPage, NULL);
309 ASMAtomicXchgPtr((void * volatile *)&g_pSUPGlobalInfoPageR0, NULL);
310 ASMAtomicXchgSize(&g_HCPhysSUPGlobalInfoPage, NIL_RTHCPHYS);
311 /* just a little safe guard against threads using the page. */
312 RTThreadSleep(50);
313 }
314
315 /*
316 * Close the support driver.
317 */
318 int rc = suplibOsTerm();
319 if (rc)
320 return rc;
321
322 g_u32Cookie = 0;
323 g_u32SessionCookie = 0;
324#ifndef VBOX_WITHOUT_IDT_PATCHING
325 g_u8Interrupt = 3;
326#endif
327 g_cInits = 0;
328 }
329 else
330 g_cInits--;
331
332 return 0;
333}
334
335
336SUPR3DECL(SUPPAGINGMODE) SUPGetPagingMode(void)
337{
338 /*
339 * Issue IOCtl to the SUPDRV kernel module.
340 */
341 SUPGETPAGINGMODE_IN In;
342 In.u32Cookie = g_u32Cookie;
343 In.u32SessionCookie = g_u32SessionCookie;
344 SUPGETPAGINGMODE_OUT Out = {SUPPAGINGMODE_INVALID};
345 int rc;
346 if (!g_u32FakeMode)
347 {
348 rc = suplibOsIOCtl(SUP_IOCTL_GET_PAGING_MODE, &In, sizeof(In), &Out, sizeof(Out));
349 if (VBOX_FAILURE(rc))
350 Out.enmMode = SUPPAGINGMODE_INVALID;
351 }
352 else
353 Out.enmMode = SUPPAGINGMODE_32_BIT_GLOBAL;
354
355 return Out.enmMode;
356}
357
358SUPR3DECL(int) SUPCallVMMR0Ex(PVM pVM, unsigned uOperation, void *pvArg, unsigned cbArg)
359{
360 /*
361 * Issue IOCtl to the SUPDRV kernel module.
362 */
363 SUPCALLVMMR0_IN In;
364 In.u32Cookie = g_u32Cookie;
365 In.u32SessionCookie = g_u32SessionCookie;
366 In.pVM = pVM;
367 In.uOperation = uOperation;
368 In.cbArg = cbArg;
369 In.pvArg = pvArg;
370 Assert(!g_u32FakeMode);
371 SUPCALLVMMR0_OUT Out = {VINF_SUCCESS};
372 int rc = suplibOsIOCtl(SUP_IOCTL_CALL_VMMR0, &In, sizeof(In), &Out, sizeof(Out));
373 if (VBOX_SUCCESS(rc))
374 rc = Out.rc;
375 return rc;
376}
377
378
379SUPR3DECL(int) SUPCallVMMR0(PVM pVM, unsigned uOperation, void *pvArg)
380{
381#ifndef VBOX_WITHOUT_IDT_PATCHING
382 return g_pfnCallVMMR0(pVM, uOperation, pvArg);
383#else
384 if (uOperation == VMMR0_DO_RUN_GC)
385 {
386 Assert(!pvArg);
387 return suplibOSIOCtlFast(SUP_IOCTL_FAST_DO_RAW_RUN);
388 }
389 if (uOperation == VMMR0_HWACC_RUN_GUEST)
390 {
391 Assert(!pvArg);
392 return suplibOSIOCtlFast(SUP_IOCTL_FAST_DO_HWACC_RUN);
393 }
394 return SUPCallVMMR0Ex(pVM, uOperation, pvArg, pvArg ? sizeof(pvArg) : 0);
395#endif
396}
397
398
399SUPR3DECL(int) SUPSetVMForFastIOCtl(PVMR0 pVMR0)
400{
401 SUPSETVMFORFAST_IN In;
402 In.u32Cookie = g_u32Cookie;
403 In.u32SessionCookie = g_u32SessionCookie;
404 In.pVMR0 = pVMR0;
405 Assert(!g_u32FakeMode);
406 return suplibOsIOCtl(SUP_IOCTL_SET_VM_FOR_FAST, &In, sizeof(In), NULL, 0);
407}
408
409
410SUPR3DECL(int) SUPPageLock(void *pvStart, size_t cbMemory, PSUPPAGE paPages)
411{
412 /*
413 * Validate.
414 */
415 AssertPtr(pvStart);
416 AssertMsg(RT_ALIGN_P(pvStart, PAGE_SIZE) == pvStart, ("pvStart (%p) must be page aligned\n", pvStart));
417 AssertMsg(RT_ALIGN_Z(cbMemory, PAGE_SIZE) == cbMemory, ("cbMemory (%#zx) must be page aligned\n", cbMemory));
418 AssertPtr(paPages);
419
420 /*
421 * Issue IOCtl to the SUPDRV kernel module.
422 */
423 SUPPINPAGES_IN In;
424 In.u32Cookie = g_u32Cookie;
425 In.u32SessionCookie = g_u32SessionCookie;
426 In.pv = pvStart;
427 In.cb = (uint32_t)cbMemory; AssertRelease(In.cb == cbMemory);
428 PSUPPINPAGES_OUT pOut = (PSUPPINPAGES_OUT)(void*)paPages;
429 Assert(RT_OFFSETOF(SUPPINPAGES_OUT, aPages) == 0 && sizeof(paPages[0]) == sizeof(pOut->aPages[0]));
430 int rc;
431 if (!g_u32FakeMode)
432 rc = suplibOsIOCtl(SUP_IOCTL_PINPAGES, &In, sizeof(In), pOut, RT_OFFSETOF(SUPPINPAGES_OUT, aPages[cbMemory >> PAGE_SHIFT]));
433 else
434 {
435 /* fake a successfull result. */
436 RTHCPHYS Phys = (uintptr_t)pvStart + PAGE_SIZE * 1024;
437 unsigned iPage = (unsigned)cbMemory >> PAGE_SHIFT;
438 while (iPage-- > 0)
439 paPages[iPage].Phys = Phys + (iPage << PAGE_SHIFT);
440 rc = VINF_SUCCESS;
441 }
442
443 return rc;
444}
445
446
447SUPR3DECL(int) SUPPageUnlock(void *pvStart)
448{
449 /*
450 * Validate.
451 */
452 AssertPtr(pvStart);
453 AssertMsg(RT_ALIGN_P(pvStart, PAGE_SIZE) == pvStart, ("pvStart (%p) must be page aligned\n", pvStart));
454
455 /*
456 * Issue IOCtl to the SUPDRV kernel module.
457 */
458 SUPUNPINPAGES_IN In;
459 In.u32Cookie = g_u32Cookie;
460 In.u32SessionCookie = g_u32SessionCookie;
461 In.pv = pvStart;
462 int rc;
463 if (!g_u32FakeMode)
464 rc = suplibOsIOCtl(SUP_IOCTL_UNPINPAGES, &In, sizeof(In), NULL, 0);
465 else
466 rc = VINF_SUCCESS;
467
468 return rc;
469}
470
471
472SUPR3DECL(void *) SUPContAlloc(unsigned cb, PRTHCPHYS pHCPhys)
473{
474 return SUPContAlloc2(cb, NULL, pHCPhys);
475}
476
477
478SUPR3DECL(void *) SUPContAlloc2(unsigned cb, void **ppvR0, PRTHCPHYS pHCPhys)
479{
480 /*
481 * Validate.
482 */
483 AssertMsg(cb > 64 && cb < PAGE_SIZE * 256, ("cb=%d must be > 64 and < %d (256 pages)\n", cb, PAGE_SIZE * 256));
484 AssertPtr(pHCPhys);
485 *pHCPhys = NIL_RTHCPHYS;
486
487 /*
488 * Issue IOCtl to the SUPDRV kernel module.
489 */
490 SUPCONTALLOC_IN In;
491 In.u32Cookie = g_u32Cookie;
492 In.u32SessionCookie = g_u32SessionCookie;
493 In.cb = RT_ALIGN_32(cb, PAGE_SIZE);
494 SUPCONTALLOC_OUT Out;
495 int rc;
496 if (!g_u32FakeMode)
497 rc = suplibOsIOCtl(SUP_IOCTL_CONT_ALLOC, &In, sizeof(In), &Out, sizeof(Out));
498 else
499 {
500 rc = SUPPageAlloc(In.cb >> PAGE_SHIFT, &Out.pvR3);
501 Out.HCPhys = (uintptr_t)Out.pvR3 + (PAGE_SHIFT * 1024);
502 Out.pvR0 = Out.pvR3;
503 }
504 if (VBOX_SUCCESS(rc))
505 {
506 *pHCPhys = (RTHCPHYS)Out.HCPhys;
507 if (ppvR0)
508 *ppvR0 = Out.pvR0;
509 return Out.pvR3;
510 }
511
512 return NULL;
513}
514
515
516SUPR3DECL(int) SUPContFree(void *pv)
517{
518 /*
519 * Validate.
520 */
521 AssertPtr(pv);
522 if (!pv)
523 return VINF_SUCCESS;
524
525 /*
526 * Issue IOCtl to the SUPDRV kernel module.
527 */
528 SUPCONTFREE_IN In;
529 In.u32Cookie = g_u32Cookie;
530 In.u32SessionCookie = g_u32SessionCookie;
531 In.pv = pv;
532 int rc;
533 if (!g_u32FakeMode)
534 rc = suplibOsIOCtl(SUP_IOCTL_CONT_FREE, &In, sizeof(In), NULL, 0);
535 else
536 rc = SUPPageFree(pv);
537
538 return rc;
539}
540
541
542SUPR3DECL(int) SUPLowAlloc(unsigned cPages, void **ppvPages, PSUPPAGE paPages)
543{
544 /*
545 * Validate.
546 */
547 AssertMsg(cPages > 0 && cPages < 256, ("cPages=%d must be > 0 and < 256\n", cPages));
548 AssertPtr(ppvPages);
549 *ppvPages = NULL;
550 AssertPtr(paPages);
551
552 int rc;
553 if (!g_u32FakeMode)
554 {
555 /*
556 * Issue IOCtl to the SUPDRV kernel module.
557 */
558 SUPLOWALLOC_IN In;
559 In.u32Cookie = g_u32Cookie;
560 In.u32SessionCookie = g_u32SessionCookie;
561 In.cPages = cPages;
562 size_t cbOut = RT_OFFSETOF(SUPLOWALLOC_OUT, aPages[cPages]);
563 PSUPLOWALLOC_OUT pOut = (PSUPLOWALLOC_OUT)RTMemAllocZ(cbOut);
564 if (pOut)
565 {
566 rc = suplibOsIOCtl(SUP_IOCTL_LOW_ALLOC, &In, sizeof(In), pOut, cbOut);
567 if (VBOX_SUCCESS(rc))
568 {
569 *ppvPages = pOut->pvVirt;
570 AssertCompile(sizeof(paPages[0]) == sizeof(pOut->aPages[0]));
571 memcpy(paPages, &pOut->aPages[0], sizeof(paPages[0]) * cPages);
572 }
573 RTMemFree(pOut);
574 }
575 else
576 rc = VERR_NO_MEMORY;
577 }
578 else
579 {
580 rc = SUPPageAlloc(cPages, ppvPages);
581 if (VBOX_SUCCESS(rc))
582 {
583 /* fake physical addresses. */
584 RTHCPHYS Phys = (uintptr_t)*ppvPages + PAGE_SIZE * 1024;
585 unsigned iPage = cPages;
586 while (iPage-- > 0)
587 paPages[iPage].Phys = Phys + (iPage << PAGE_SHIFT);
588 }
589 }
590
591 return rc;
592}
593
594
595SUPR3DECL(int) SUPLowFree(void *pv)
596{
597 /*
598 * Validate.
599 */
600 AssertPtr(pv);
601 if (!pv)
602 return VINF_SUCCESS;
603
604 /*
605 * Issue IOCtl to the SUPDRV kernel module.
606 */
607 SUPLOWFREE_IN In;
608 In.u32Cookie = g_u32Cookie;
609 In.u32SessionCookie = g_u32SessionCookie;
610 In.pv = pv;
611 int rc;
612 if (!g_u32FakeMode)
613 rc = suplibOsIOCtl(SUP_IOCTL_LOW_FREE, &In, sizeof(In), NULL, 0);
614 else
615 rc = SUPPageFree(pv);
616
617 return rc;
618}
619
620
621SUPR3DECL(int) SUPPageAlloc(size_t cPages, void **ppvPages)
622{
623 /*
624 * Validate.
625 */
626 if (cPages == 0)
627 {
628 AssertMsgFailed(("Invalid param cPages=0, must be > 0\n"));
629 return VERR_INVALID_PARAMETER;
630 }
631 AssertPtr(ppvPages);
632 if (!ppvPages)
633 return VERR_INVALID_PARAMETER;
634 *ppvPages = NULL;
635
636 /*
637 * Call OS specific worker.
638 */
639 return suplibOsPageAlloc(cPages, ppvPages);
640}
641
642
643SUPR3DECL(int) SUPPageFree(void *pvPages)
644{
645 /*
646 * Validate.
647 */
648 AssertPtr(pvPages);
649 if (!pvPages)
650 return VINF_SUCCESS;
651
652 /*
653 * Call OS specific worker.
654 */
655 return suplibOsPageFree(pvPages);
656}
657
658
659SUPR3DECL(int) SUPLoadModule(const char *pszFilename, const char *pszModule, void **ppvImageBase)
660{
661 /*
662 * Load the module.
663 * If it's VMMR0.r0 we need to install the IDTE.
664 */
665 int rc = supLoadModule(pszFilename, pszModule, ppvImageBase);
666#ifndef VBOX_WITHOUT_IDT_PATCHING
667 if ( VBOX_SUCCESS(rc)
668 && !strcmp(pszModule, "VMMR0.r0"))
669 {
670 rc = supInstallIDTE();
671 if (VBOX_FAILURE(rc))
672 SUPFreeModule(*ppvImageBase);
673 }
674#endif /* VBOX_WITHOUT_IDT_PATCHING */
675
676 return rc;
677}
678
679
680#ifndef VBOX_WITHOUT_IDT_PATCHING
681/**
682 * Generates the code for calling the interrupt gate.
683 *
684 * @returns VBox status code.
685 * g_pfnCallVMMR0 is changed on success.
686 * @param u8Interrupt The interrupt number.
687 */
688static int suplibGenerateCallVMMR0(uint8_t u8Interrupt)
689{
690 /*
691 * Allocate memory.
692 */
693 uint8_t *pb = (uint8_t *)RTMemExecAlloc(256);
694 AssertReturn(pb, VERR_NO_MEMORY);
695 memset(pb, 0xcc, 256);
696 Assert(!g_pfnCallVMMR0);
697 g_pfnCallVMMR0 = *(PFNCALLVMMR0*)&pb;
698
699 /*
700 * Generate the code.
701 */
702#ifdef __AMD64__
703 /*
704 * reg params:
705 * <GCC> <MSC> <argument>
706 * rdi rcx pVM
707 * esi edx uOperation
708 * rdx r8 pvArg
709 *
710 * eax eax [g_u32Gookie]
711 */
712 *pb++ = 0xb8; /* mov eax, <g_u32Cookie> */
713 *(uint32_t *)pb = g_u32Cookie;
714 pb += sizeof(uint32_t);
715
716 *pb++ = 0xcd; /* int <u8Interrupt> */
717 *pb++ = u8Interrupt;
718
719 *pb++ = 0xc3; /* ret */
720
721#else
722 /*
723 * x86 stack:
724 * 0 saved esi
725 * 0 4 ret
726 * 4 8 pVM
727 * 8 c uOperation
728 * c 10 pvArg
729 */
730 *pb++ = 0x56; /* push esi */
731
732 *pb++ = 0x8b; /* mov eax, [pVM] */
733 *pb++ = 0x44;
734 *pb++ = 0x24;
735 *pb++ = 0x08; /* esp+08h */
736
737 *pb++ = 0x8b; /* mov edx, [uOperation] */
738 *pb++ = 0x54;
739 *pb++ = 0x24;
740 *pb++ = 0x0c; /* esp+0ch */
741
742 *pb++ = 0x8b; /* mov ecx, [pvArg] */
743 *pb++ = 0x4c;
744 *pb++ = 0x24;
745 *pb++ = 0x10; /* esp+10h */
746
747 *pb++ = 0xbe; /* mov esi, <g_u32Cookie> */
748 *(uint32_t *)pb = g_u32Cookie;
749 pb += sizeof(uint32_t);
750
751 *pb++ = 0xcd; /* int <u8Interrupt> */
752 *pb++ = u8Interrupt;
753
754 *pb++ = 0x5e; /* pop esi */
755
756 *pb++ = 0xc3; /* ret */
757#endif
758
759 return VINF_SUCCESS;
760}
761
762
763/**
764 * Installs the IDTE patch.
765 *
766 * @return VBox status code.
767 */
768static int supInstallIDTE(void)
769{
770 /* already installed? */
771 if (g_u8Interrupt != 3 || g_u32FakeMode)
772 return VINF_SUCCESS;
773
774 int rc = VINF_SUCCESS;
775 const unsigned cCpus = RTSystemProcessorGetCount();
776 if (cCpus <= 1)
777 {
778 /* UNI */
779 SUPIDTINSTALL_IN In;
780 In.u32Cookie = g_u32Cookie;
781 In.u32SessionCookie = g_u32SessionCookie;
782 SUPIDTINSTALL_OUT Out = {3};
783
784 rc = suplibOsIOCtl(SUP_IOCTL_IDT_INSTALL, &In, sizeof(In), &Out, sizeof(Out));
785 if (VBOX_SUCCESS(rc))
786 {
787 g_u8Interrupt = Out.u8Idt;
788 rc = suplibGenerateCallVMMR0(Out.u8Idt);
789 }
790 }
791 else
792 {
793 /* SMP */
794 uint64_t u64AffMaskSaved = RTThreadGetAffinity();
795 uint64_t u64AffMaskPatched = RTSystemProcessorGetActiveMask() & u64AffMaskSaved;
796 unsigned cCpusPatched = 0;
797
798 for (int i = 0; i < 64; i++)
799 {
800 /* Skip absent and inactive processors. */
801 uint64_t u64Mask = 1ULL << i;
802 if (!(u64Mask & u64AffMaskPatched))
803 continue;
804
805 /* Change CPU */
806 int rc2 = RTThreadSetAffinity(u64Mask);
807 if (VBOX_FAILURE(rc2))
808 {
809 u64AffMaskPatched &= ~u64Mask;
810 Log(("SUPLoadVMM: Failed to set affinity to cpu no. %d, rc=%Vrc.\n", i, rc2));
811 continue;
812 }
813
814 /* Patch the CPU. */
815 SUPIDTINSTALL_IN In;
816 In.u32Cookie = g_u32Cookie;
817 In.u32SessionCookie = g_u32SessionCookie;
818 SUPIDTINSTALL_OUT Out = {3};
819
820 rc2 = suplibOsIOCtl(SUP_IOCTL_IDT_INSTALL, &In, sizeof(In), &Out, sizeof(Out));
821 if (VBOX_SUCCESS(rc2))
822 {
823 if (!cCpusPatched)
824 {
825 g_u8Interrupt = Out.u8Idt;
826 rc2 = suplibGenerateCallVMMR0(Out.u8Idt);
827 if (VBOX_FAILURE(rc))
828 rc2 = rc;
829 }
830 else
831 Assert(g_u8Interrupt == Out.u8Idt);
832 cCpusPatched++;
833 }
834 else
835 {
836
837 Log(("SUPLoadVMM: Failed to patch cpu no. %d, rc=%Vrc.\n", i, rc2));
838 if (VBOX_SUCCESS(rc))
839 rc = rc2;
840 }
841 }
842
843 /* Fail if no CPUs was patched! */
844 if (VBOX_SUCCESS(rc) && cCpusPatched <= 0)
845 rc = VERR_GENERAL_FAILURE;
846 /* Ignore failures if a CPU was patched. */
847 else if (VBOX_FAILURE(rc) && cCpusPatched > 0)
848 {
849 /** @todo add an eventlog/syslog line out this. */
850 rc = VINF_SUCCESS;
851 }
852
853 /* Set/restore the thread affinity. */
854 if (VBOX_SUCCESS(rc))
855 {
856 rc = RTThreadSetAffinity(u64AffMaskPatched);
857 AssertRC(rc);
858 }
859 else
860 {
861 int rc2 = RTThreadSetAffinity(u64AffMaskSaved);
862 AssertRC(rc2);
863 }
864 }
865 return rc;
866}
867#endif /* !VBOX_WITHOUT_IDT_PATCHING */
868
869
870/**
871 * Resolve an external symbol during RTLdrGetBits().
872 *
873 * @returns VBox status code.
874 * @param hLdrMod The loader module handle.
875 * @param pszModule Module name.
876 * @param pszSymbol Symbol name, NULL if uSymbol should be used.
877 * @param uSymbol Symbol ordinal, ~0 if pszSymbol should be used.
878 * @param pValue Where to store the symbol value (address).
879 * @param pvUser User argument.
880 */
881static DECLCALLBACK(int) supLoadModuleResolveImport(RTLDRMOD hLdrMod, const char *pszModule,
882 const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser)
883{
884 AssertPtr(pValue);
885 AssertPtr(pvUser);
886
887 /*
888 * Only SUPR0 and VMMR0.r0
889 */
890 if ( pszModule
891 && *pszModule
892 && strcmp(pszModule, "SUPR0.dll")
893 && strcmp(pszModule, "VMMR0.r0"))
894 {
895 AssertMsgFailed(("%s is importing from %s! (expected 'SUPR0.dll' or 'VMMR0.r0', case-sensitiv)\n", pvUser, pszModule));
896 return VERR_SYMBOL_NOT_FOUND;
897 }
898
899 /*
900 * No ordinals.
901 */
902 if (pszSymbol < (const char*)0x10000)
903 {
904 AssertMsgFailed(("%s is importing by ordinal (ord=%d)\n", pvUser, (int)(uintptr_t)pszSymbol));
905 return VERR_SYMBOL_NOT_FOUND;
906 }
907
908 /*
909 * Lookup symbol.
910 */
911 /* skip the 64-bit ELF import prefix first. */
912 if (!strncmp(pszSymbol, "SUPR0$", sizeof("SUPR0$") - 1))
913 pszSymbol += sizeof("SUPR0$") - 1;
914
915 /* iterate the function table. */
916 int c = g_pFunctions->cFunctions;
917 PSUPFUNC pFunc = &g_pFunctions->aFunctions[0];
918 while (c-- > 0)
919 {
920 if (!strcmp(pFunc->szName, pszSymbol))
921 {
922 *pValue = (uintptr_t)pFunc->pfn;
923 return VINF_SUCCESS;
924 }
925 pFunc++;
926 }
927
928 /*
929 * Check the VMMR0.r0 module if loaded.
930 */
931 /** @todo call the SUPLoadModule caller.... */
932 /** @todo proper reference counting and such. */
933 if (g_pvVMMR0)
934 {
935 void *pvValue;
936 if (!SUPGetSymbolR0(g_pvVMMR0, pszSymbol, &pvValue))
937 {
938 *pValue = (uintptr_t)pvValue;
939 return VINF_SUCCESS;
940 }
941 }
942
943 /*
944 * The GIP.
945 */
946 /** @todo R0 mapping? */
947 if ( pszSymbol
948 && g_pSUPGlobalInfoPage
949 && g_pSUPGlobalInfoPageR0
950 && !strcmp(pszSymbol, "g_SUPGlobalInfoPage"))
951 {
952 *pValue = (uintptr_t)g_pSUPGlobalInfoPageR0;
953 return VINF_SUCCESS;
954 }
955
956 /*
957 * Despair.
958 */
959 c = g_pFunctions->cFunctions;
960 pFunc = &g_pFunctions->aFunctions[0];
961 while (c-- > 0)
962 {
963 AssertMsg2("%d: %s\n", g_pFunctions->cFunctions - c, pFunc->szName);
964 pFunc++;
965 }
966
967 AssertMsgFailed(("%s is importing %s which we couldn't find\n", pvUser, pszSymbol));
968 return VERR_SYMBOL_NOT_FOUND;
969}
970
971
972/** Argument package for supLoadModuleCalcSizeCB. */
973typedef struct SUPLDRCALCSIZEARGS
974{
975 size_t cbStrings;
976 uint32_t cSymbols;
977 size_t cbImage;
978} SUPLDRCALCSIZEARGS, *PSUPLDRCALCSIZEARGS;
979
980/**
981 * Callback used to calculate the image size.
982 * @return VINF_SUCCESS
983 */
984static DECLCALLBACK(int) supLoadModuleCalcSizeCB(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
985{
986 PSUPLDRCALCSIZEARGS pArgs = (PSUPLDRCALCSIZEARGS)pvUser;
987 if ( pszSymbol != NULL
988 && *pszSymbol
989 && Value <= pArgs->cbImage)
990 {
991 pArgs->cSymbols++;
992 pArgs->cbStrings += strlen(pszSymbol) + 1;
993 }
994 return VINF_SUCCESS;
995}
996
997
998/** Argument package for supLoadModuleCreateTabsCB. */
999typedef struct SUPLDRCREATETABSARGS
1000{
1001 size_t cbImage;
1002 PSUPLDRSYM pSym;
1003 char *pszBase;
1004 char *psz;
1005} SUPLDRCREATETABSARGS, *PSUPLDRCREATETABSARGS;
1006
1007/**
1008 * Callback used to calculate the image size.
1009 * @return VINF_SUCCESS
1010 */
1011static DECLCALLBACK(int) supLoadModuleCreateTabsCB(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
1012{
1013 PSUPLDRCREATETABSARGS pArgs = (PSUPLDRCREATETABSARGS)pvUser;
1014 if ( pszSymbol != NULL
1015 && *pszSymbol
1016 && Value <= pArgs->cbImage)
1017 {
1018 pArgs->pSym->offSymbol = (uint32_t)Value;
1019 pArgs->pSym->offName = pArgs->psz - pArgs->pszBase;
1020 pArgs->pSym++;
1021
1022 size_t cbCopy = strlen(pszSymbol) + 1;
1023 memcpy(pArgs->psz, pszSymbol, cbCopy);
1024 pArgs->psz += cbCopy;
1025 }
1026 return VINF_SUCCESS;
1027}
1028
1029
1030/**
1031 * Worker for SUPLoadModule().
1032 *
1033 * @returns VBox status code.
1034 * @param pszFilename Name of the VMMR0 image file
1035 */
1036static int supLoadModule(const char *pszFilename, const char *pszModule, void **ppvImageBase)
1037{
1038 /*
1039 * Validate input.
1040 */
1041 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
1042 AssertPtrReturn(pszModule, VERR_INVALID_PARAMETER);
1043 AssertPtrReturn(ppvImageBase, VERR_INVALID_PARAMETER);
1044 AssertReturn(strlen(pszModule) < SIZEOFMEMB(SUPLDROPEN_IN, szName), VERR_FILENAME_TOO_LONG);
1045
1046 const bool fIsVMMR0 = !strcmp(pszModule, "VMMR0.r0");
1047 *ppvImageBase = NULL;
1048
1049 /*
1050 * Open image file and figure its size.
1051 */
1052 RTLDRMOD hLdrMod;
1053 int rc = RTLdrOpen(pszFilename, &hLdrMod);
1054 if (!VBOX_SUCCESS(rc))
1055 return rc;
1056
1057 SUPLDRCALCSIZEARGS CalcArgs;
1058 CalcArgs.cbStrings = 0;
1059 CalcArgs.cSymbols = 0;
1060 CalcArgs.cbImage = RTLdrSize(hLdrMod);
1061 rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCalcSizeCB, &CalcArgs);
1062 if (VBOX_SUCCESS(rc))
1063 {
1064 const uint32_t offSymTab = RT_ALIGN_32(CalcArgs.cbImage, 8);
1065 const uint32_t offStrTab = offSymTab + CalcArgs.cSymbols * sizeof(SUPLDRSYM);
1066 const uint32_t cbImage = RT_ALIGN_32(offStrTab + CalcArgs.cbStrings, 8);
1067
1068 /*
1069 * Open the R0 image.
1070 */
1071 SUPLDROPEN_IN OpenIn;
1072 OpenIn.u32Cookie = g_u32Cookie;
1073 OpenIn.u32SessionCookie = g_u32SessionCookie;
1074 OpenIn.cbImage = cbImage;
1075 strcpy(OpenIn.szName, pszModule);
1076 SUPLDROPEN_OUT OpenOut;
1077 if (!g_u32FakeMode)
1078 rc = suplibOsIOCtl(SUP_IOCTL_LDR_OPEN, &OpenIn, sizeof(OpenIn), &OpenOut, sizeof(OpenOut));
1079 else
1080 {
1081 OpenOut.fNeedsLoading = true;
1082 OpenOut.pvImageBase = (void *)0xef423420;
1083 }
1084 *ppvImageBase = OpenOut.pvImageBase;
1085 if ( VBOX_SUCCESS(rc)
1086 && OpenOut.fNeedsLoading)
1087 {
1088 /*
1089 * We need to load it.
1090 * Allocate memory for the image bits.
1091 */
1092 unsigned cbIn = RT_OFFSETOF(SUPLDRLOAD_IN, achImage[cbImage]);
1093 PSUPLDRLOAD_IN pIn = (PSUPLDRLOAD_IN)RTMemTmpAlloc(cbIn);
1094 if (pIn)
1095 {
1096 /*
1097 * Get the image bits.
1098 */
1099 rc = RTLdrGetBits(hLdrMod, &pIn->achImage[0], (uintptr_t)OpenOut.pvImageBase,
1100 supLoadModuleResolveImport, (void *)pszModule);
1101
1102 /*
1103 * Get the entry points.
1104 */
1105 RTUINTPTR VMMR0Entry = 0;
1106 RTUINTPTR ModuleInit = 0;
1107 RTUINTPTR ModuleTerm = 0;
1108 if (fIsVMMR0 && VBOX_SUCCESS(rc))
1109 rc = RTLdrGetSymbolEx(hLdrMod, &pIn->achImage[0], (uintptr_t)OpenOut.pvImageBase, "VMMR0Entry", &VMMR0Entry);
1110 if (VBOX_SUCCESS(rc))
1111 {
1112 rc = RTLdrGetSymbolEx(hLdrMod, &pIn->achImage[0], (uintptr_t)OpenOut.pvImageBase, "ModuleInit", &ModuleInit);
1113 if (VBOX_FAILURE(rc))
1114 ModuleInit = 0;
1115
1116 rc = RTLdrGetSymbolEx(hLdrMod, &pIn->achImage[0], (uintptr_t)OpenOut.pvImageBase, "ModuleTerm", &ModuleTerm);
1117 if (VBOX_FAILURE(rc))
1118 ModuleTerm = 0;
1119 }
1120
1121 /*
1122 * Create the symbol and string tables.
1123 */
1124 SUPLDRCREATETABSARGS CreateArgs;
1125 CreateArgs.cbImage = CalcArgs.cbImage;
1126 CreateArgs.pSym = (PSUPLDRSYM)&pIn->achImage[offSymTab];
1127 CreateArgs.pszBase = (char *)&pIn->achImage[offStrTab];
1128 CreateArgs.psz = CreateArgs.pszBase;
1129 rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCreateTabsCB, &CreateArgs);
1130 if (VBOX_SUCCESS(rc))
1131 {
1132 AssertRelease((size_t)(CreateArgs.psz - CreateArgs.pszBase) <= CalcArgs.cbStrings);
1133 AssertRelease((size_t)(CreateArgs.pSym - (PSUPLDRSYM)&pIn->achImage[offSymTab]) <= CalcArgs.cSymbols);
1134
1135 /*
1136 * Upload the image.
1137 */
1138 pIn->u32Cookie = g_u32Cookie;
1139 pIn->u32SessionCookie = g_u32SessionCookie;
1140 pIn->pfnModuleInit = (PFNR0MODULEINIT)(uintptr_t)ModuleInit;
1141 pIn->pfnModuleTerm = (PFNR0MODULETERM)(uintptr_t)ModuleTerm;
1142 if (fIsVMMR0)
1143 {
1144 pIn->eEPType = pIn->EP_VMMR0;
1145 pIn->EP.VMMR0.pvVMMR0 = OpenOut.pvImageBase;
1146 pIn->EP.VMMR0.pvVMMR0Entry = (void *)(uintptr_t)VMMR0Entry;
1147 }
1148 else
1149 pIn->eEPType = pIn->EP_NOTHING;
1150 pIn->offStrTab = offStrTab;
1151 pIn->cbStrTab = CalcArgs.cbStrings;
1152 pIn->offSymbols = offSymTab;
1153 pIn->cSymbols = CalcArgs.cSymbols;
1154 pIn->cbImage = cbImage;
1155 pIn->pvImageBase = OpenOut.pvImageBase;
1156 if (!g_u32FakeMode)
1157 rc = suplibOsIOCtl(SUP_IOCTL_LDR_LOAD, pIn, cbIn, NULL, 0);
1158 else
1159 rc = VINF_SUCCESS;
1160 if ( VBOX_SUCCESS(rc)
1161 || rc == VERR_ALREADY_LOADED /* this is because of a competing process. */
1162 )
1163 {
1164 if (fIsVMMR0)
1165 g_pvVMMR0 = OpenOut.pvImageBase;
1166 RTMemTmpFree(pIn);
1167 RTLdrClose(hLdrMod);
1168 return VINF_SUCCESS;
1169 }
1170 }
1171 RTMemTmpFree(pIn);
1172 }
1173 else
1174 {
1175 AssertMsgFailed(("failed to allocated %d bytes for SUPLDRLOAD_IN structure!\n", cbIn));
1176 rc = VERR_NO_TMP_MEMORY;
1177 }
1178 }
1179 }
1180 RTLdrClose(hLdrMod);
1181 return rc;
1182}
1183
1184
1185SUPR3DECL(int) SUPFreeModule(void *pvImageBase)
1186{
1187 /*
1188 * There is one special module. When this is freed we'll
1189 * free the IDT entry that goes with it.
1190 *
1191 * Note that we don't keep count of VMMR0.r0 loads here, so the
1192 * first unload will free it.
1193 */
1194 if (pvImageBase == g_pvVMMR0)
1195 {
1196 /*
1197 * This is the point where we remove the IDT hook. We do
1198 * that before unloading the R0 VMM part.
1199 */
1200 if (g_u32FakeMode)
1201 {
1202#ifndef VBOX_WITHOUT_IDT_PATCHING
1203 g_u8Interrupt = 3;
1204 RTMemExecFree(*(void **)&g_pfnCallVMMR0);
1205 g_pfnCallVMMR0 = NULL;
1206#endif
1207 g_pvVMMR0 = NULL;
1208 return VINF_SUCCESS;
1209 }
1210
1211#ifndef VBOX_WITHOUT_IDT_PATCHING
1212 /*
1213 * Uninstall IDT entry.
1214 */
1215 int rc = 0;
1216 if (g_u8Interrupt != 3)
1217 {
1218 SUPIDTREMOVE_IN In;
1219 In.u32Cookie = g_u32Cookie;
1220 In.u32SessionCookie = g_u32SessionCookie;
1221 rc = suplibOsIOCtl(SUP_IOCTL_IDT_REMOVE, &In, sizeof(In), NULL, 0);
1222 g_u8Interrupt = 3;
1223 RTMemExecFree(*(void **)&g_pfnCallVMMR0);
1224 g_pfnCallVMMR0 = NULL;
1225 }
1226#endif
1227 }
1228
1229 /*
1230 * Free the requested module.
1231 */
1232 SUPLDRFREE_IN In;
1233 In.u32Cookie = g_u32Cookie;
1234 In.u32SessionCookie = g_u32SessionCookie;
1235 In.pvImageBase = pvImageBase;
1236 int rc = VINF_SUCCESS;
1237 if (!g_u32FakeMode)
1238 rc = suplibOsIOCtl(SUP_IOCTL_LDR_FREE, &In, sizeof(In), NULL, 0);
1239 if ( VBOX_SUCCESS(rc)
1240 && pvImageBase == g_pvVMMR0)
1241 g_pvVMMR0 = NULL;
1242 return rc;
1243}
1244
1245
1246SUPR3DECL(int) SUPGetSymbolR0(void *pvImageBase, const char *pszSymbol, void **ppvValue)
1247{
1248 *ppvValue = NULL;
1249
1250 /*
1251 * Do ioctl.
1252 */
1253 size_t cchSymbol = strlen(pszSymbol);
1254 const size_t cbIn = RT_OFFSETOF(SUPLDRGETSYMBOL_IN, szSymbol[cchSymbol + 1]);
1255 SUPLDRGETSYMBOL_OUT Out = { NULL };
1256 PSUPLDRGETSYMBOL_IN pIn = (PSUPLDRGETSYMBOL_IN)alloca(cbIn);
1257 pIn->u32Cookie = g_u32Cookie;
1258 pIn->u32SessionCookie = g_u32SessionCookie;
1259 pIn->pvImageBase = pvImageBase;
1260 memcpy(pIn->szSymbol, pszSymbol, cchSymbol + 1);
1261 int rc = suplibOsIOCtl(SUP_IOCTL_LDR_GET_SYMBOL, pIn, cbIn, &Out, sizeof(Out));
1262 if (VBOX_SUCCESS(rc))
1263 *ppvValue = Out.pvSymbol;
1264 return rc;
1265}
1266
1267
1268SUPR3DECL(int) SUPLoadVMM(const char *pszFilename)
1269{
1270 void *pvImageBase;
1271 return SUPLoadModule(pszFilename, "VMMR0.r0", &pvImageBase);
1272}
1273
1274
1275SUPR3DECL(int) SUPUnloadVMM(void)
1276{
1277 return SUPFreeModule(g_pvVMMR0);
1278}
1279
1280
1281SUPR3DECL(int) SUPGipGetPhys(PRTHCPHYS pHCPhys)
1282{
1283 if (g_pSUPGlobalInfoPage)
1284 {
1285 *pHCPhys = g_HCPhysSUPGlobalInfoPage;
1286 return VINF_SUCCESS;
1287 }
1288 *pHCPhys = NIL_RTHCPHYS;
1289 return VERR_WRONG_ORDER;
1290}
1291
Note: See TracBrowser for help on using the repository browser.

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