VirtualBox

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

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

Completed most of VBOX_WITHOUT_IDT_PATCHING. (hope I didn't break anything...) TODO: IST support on AMD64.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 39.4 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.virtualbox.org. 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
384#else
385 if (uOperation == VMMR0_DO_RAW_RUN)
386 {
387 Assert(!pvArg);
388 return suplibOSIOCtlFast(SUP_IOCTL_FAST_DO_RAW_RUN);
389 }
390 if (uOperation == VMMR0_DO_HWACC_RUN)
391 {
392 Assert(!pvArg);
393 return suplibOSIOCtlFast(SUP_IOCTL_FAST_DO_HWACC_RUN);
394 }
395 if (uOperation == VMMR0_DO_NOP)
396 {
397 Assert(!pvArg);
398 return suplibOSIOCtlFast(SUP_IOCTL_FAST_DO_NOP);
399 }
400 return SUPCallVMMR0Ex(pVM, uOperation, pvArg, pvArg ? sizeof(pvArg) : 0);
401#endif
402}
403
404
405SUPR3DECL(int) SUPSetVMForFastIOCtl(PVMR0 pVMR0)
406{
407 SUPSETVMFORFAST_IN In;
408 In.u32Cookie = g_u32Cookie;
409 In.u32SessionCookie = g_u32SessionCookie;
410 In.pVMR0 = pVMR0;
411 Assert(!g_u32FakeMode);
412 return suplibOsIOCtl(SUP_IOCTL_SET_VM_FOR_FAST, &In, sizeof(In), NULL, 0);
413}
414
415
416SUPR3DECL(int) SUPPageLock(void *pvStart, size_t cbMemory, PSUPPAGE paPages)
417{
418 /*
419 * Validate.
420 */
421 AssertPtr(pvStart);
422 AssertMsg(RT_ALIGN_P(pvStart, PAGE_SIZE) == pvStart, ("pvStart (%p) must be page aligned\n", pvStart));
423 AssertMsg(RT_ALIGN_Z(cbMemory, PAGE_SIZE) == cbMemory, ("cbMemory (%#zx) must be page aligned\n", cbMemory));
424 AssertPtr(paPages);
425
426 /*
427 * Issue IOCtl to the SUPDRV kernel module.
428 */
429 SUPPINPAGES_IN In;
430 In.u32Cookie = g_u32Cookie;
431 In.u32SessionCookie = g_u32SessionCookie;
432 In.pv = pvStart;
433 In.cb = (uint32_t)cbMemory; AssertRelease(In.cb == cbMemory);
434 PSUPPINPAGES_OUT pOut = (PSUPPINPAGES_OUT)(void*)paPages;
435 Assert(RT_OFFSETOF(SUPPINPAGES_OUT, aPages) == 0 && sizeof(paPages[0]) == sizeof(pOut->aPages[0]));
436 int rc;
437 if (!g_u32FakeMode)
438 rc = suplibOsIOCtl(SUP_IOCTL_PINPAGES, &In, sizeof(In), pOut, RT_OFFSETOF(SUPPINPAGES_OUT, aPages[cbMemory >> PAGE_SHIFT]));
439 else
440 {
441 /* fake a successfull result. */
442 RTHCPHYS Phys = (uintptr_t)pvStart + PAGE_SIZE * 1024;
443 unsigned iPage = (unsigned)cbMemory >> PAGE_SHIFT;
444 while (iPage-- > 0)
445 paPages[iPage].Phys = Phys + (iPage << PAGE_SHIFT);
446 rc = VINF_SUCCESS;
447 }
448
449 return rc;
450}
451
452
453SUPR3DECL(int) SUPPageUnlock(void *pvStart)
454{
455 /*
456 * Validate.
457 */
458 AssertPtr(pvStart);
459 AssertMsg(RT_ALIGN_P(pvStart, PAGE_SIZE) == pvStart, ("pvStart (%p) must be page aligned\n", pvStart));
460
461 /*
462 * Issue IOCtl to the SUPDRV kernel module.
463 */
464 SUPUNPINPAGES_IN In;
465 In.u32Cookie = g_u32Cookie;
466 In.u32SessionCookie = g_u32SessionCookie;
467 In.pv = pvStart;
468 int rc;
469 if (!g_u32FakeMode)
470 rc = suplibOsIOCtl(SUP_IOCTL_UNPINPAGES, &In, sizeof(In), NULL, 0);
471 else
472 rc = VINF_SUCCESS;
473
474 return rc;
475}
476
477
478SUPR3DECL(void *) SUPContAlloc(unsigned cb, PRTHCPHYS pHCPhys)
479{
480 return SUPContAlloc2(cb, NULL, pHCPhys);
481}
482
483
484SUPR3DECL(void *) SUPContAlloc2(unsigned cb, void **ppvR0, PRTHCPHYS pHCPhys)
485{
486 /*
487 * Validate.
488 */
489 AssertMsg(cb > 64 && cb < PAGE_SIZE * 256, ("cb=%d must be > 64 and < %d (256 pages)\n", cb, PAGE_SIZE * 256));
490 AssertPtr(pHCPhys);
491 *pHCPhys = NIL_RTHCPHYS;
492
493 /*
494 * Issue IOCtl to the SUPDRV kernel module.
495 */
496 SUPCONTALLOC_IN In;
497 In.u32Cookie = g_u32Cookie;
498 In.u32SessionCookie = g_u32SessionCookie;
499 In.cb = RT_ALIGN_32(cb, PAGE_SIZE);
500 SUPCONTALLOC_OUT Out;
501 int rc;
502 if (!g_u32FakeMode)
503 rc = suplibOsIOCtl(SUP_IOCTL_CONT_ALLOC, &In, sizeof(In), &Out, sizeof(Out));
504 else
505 {
506 rc = SUPPageAlloc(In.cb >> PAGE_SHIFT, &Out.pvR3);
507 Out.HCPhys = (uintptr_t)Out.pvR3 + (PAGE_SHIFT * 1024);
508 Out.pvR0 = Out.pvR3;
509 }
510 if (VBOX_SUCCESS(rc))
511 {
512 *pHCPhys = (RTHCPHYS)Out.HCPhys;
513 if (ppvR0)
514 *ppvR0 = Out.pvR0;
515 return Out.pvR3;
516 }
517
518 return NULL;
519}
520
521
522SUPR3DECL(int) SUPContFree(void *pv)
523{
524 /*
525 * Validate.
526 */
527 AssertPtr(pv);
528 if (!pv)
529 return VINF_SUCCESS;
530
531 /*
532 * Issue IOCtl to the SUPDRV kernel module.
533 */
534 SUPCONTFREE_IN In;
535 In.u32Cookie = g_u32Cookie;
536 In.u32SessionCookie = g_u32SessionCookie;
537 In.pv = pv;
538 int rc;
539 if (!g_u32FakeMode)
540 rc = suplibOsIOCtl(SUP_IOCTL_CONT_FREE, &In, sizeof(In), NULL, 0);
541 else
542 rc = SUPPageFree(pv);
543
544 return rc;
545}
546
547
548SUPR3DECL(int) SUPLowAlloc(unsigned cPages, void **ppvPages, PSUPPAGE paPages)
549{
550 /*
551 * Validate.
552 */
553 AssertMsg(cPages > 0 && cPages < 256, ("cPages=%d must be > 0 and < 256\n", cPages));
554 AssertPtr(ppvPages);
555 *ppvPages = NULL;
556 AssertPtr(paPages);
557
558 int rc;
559 if (!g_u32FakeMode)
560 {
561 /*
562 * Issue IOCtl to the SUPDRV kernel module.
563 */
564 SUPLOWALLOC_IN In;
565 In.u32Cookie = g_u32Cookie;
566 In.u32SessionCookie = g_u32SessionCookie;
567 In.cPages = cPages;
568 size_t cbOut = RT_OFFSETOF(SUPLOWALLOC_OUT, aPages[cPages]);
569 PSUPLOWALLOC_OUT pOut = (PSUPLOWALLOC_OUT)RTMemAllocZ(cbOut);
570 if (pOut)
571 {
572 rc = suplibOsIOCtl(SUP_IOCTL_LOW_ALLOC, &In, sizeof(In), pOut, cbOut);
573 if (VBOX_SUCCESS(rc))
574 {
575 *ppvPages = pOut->pvVirt;
576 AssertCompile(sizeof(paPages[0]) == sizeof(pOut->aPages[0]));
577 memcpy(paPages, &pOut->aPages[0], sizeof(paPages[0]) * cPages);
578 }
579 RTMemFree(pOut);
580 }
581 else
582 rc = VERR_NO_MEMORY;
583 }
584 else
585 {
586 rc = SUPPageAlloc(cPages, ppvPages);
587 if (VBOX_SUCCESS(rc))
588 {
589 /* fake physical addresses. */
590 RTHCPHYS Phys = (uintptr_t)*ppvPages + PAGE_SIZE * 1024;
591 unsigned iPage = cPages;
592 while (iPage-- > 0)
593 paPages[iPage].Phys = Phys + (iPage << PAGE_SHIFT);
594 }
595 }
596
597 return rc;
598}
599
600
601SUPR3DECL(int) SUPLowFree(void *pv)
602{
603 /*
604 * Validate.
605 */
606 AssertPtr(pv);
607 if (!pv)
608 return VINF_SUCCESS;
609
610 /*
611 * Issue IOCtl to the SUPDRV kernel module.
612 */
613 SUPLOWFREE_IN In;
614 In.u32Cookie = g_u32Cookie;
615 In.u32SessionCookie = g_u32SessionCookie;
616 In.pv = pv;
617 int rc;
618 if (!g_u32FakeMode)
619 rc = suplibOsIOCtl(SUP_IOCTL_LOW_FREE, &In, sizeof(In), NULL, 0);
620 else
621 rc = SUPPageFree(pv);
622
623 return rc;
624}
625
626
627SUPR3DECL(int) SUPPageAlloc(size_t cPages, void **ppvPages)
628{
629 /*
630 * Validate.
631 */
632 if (cPages == 0)
633 {
634 AssertMsgFailed(("Invalid param cPages=0, must be > 0\n"));
635 return VERR_INVALID_PARAMETER;
636 }
637 AssertPtr(ppvPages);
638 if (!ppvPages)
639 return VERR_INVALID_PARAMETER;
640 *ppvPages = NULL;
641
642 /*
643 * Call OS specific worker.
644 */
645 return suplibOsPageAlloc(cPages, ppvPages);
646}
647
648
649SUPR3DECL(int) SUPPageFree(void *pvPages)
650{
651 /*
652 * Validate.
653 */
654 AssertPtr(pvPages);
655 if (!pvPages)
656 return VINF_SUCCESS;
657
658 /*
659 * Call OS specific worker.
660 */
661 return suplibOsPageFree(pvPages);
662}
663
664
665SUPR3DECL(int) SUPLoadModule(const char *pszFilename, const char *pszModule, void **ppvImageBase)
666{
667 /*
668 * Load the module.
669 * If it's VMMR0.r0 we need to install the IDTE.
670 */
671 int rc = supLoadModule(pszFilename, pszModule, ppvImageBase);
672#ifndef VBOX_WITHOUT_IDT_PATCHING
673 if ( VBOX_SUCCESS(rc)
674 && !strcmp(pszModule, "VMMR0.r0"))
675 {
676 rc = supInstallIDTE();
677 if (VBOX_FAILURE(rc))
678 SUPFreeModule(*ppvImageBase);
679 }
680#endif /* VBOX_WITHOUT_IDT_PATCHING */
681
682 return rc;
683}
684
685
686#ifndef VBOX_WITHOUT_IDT_PATCHING
687/**
688 * Generates the code for calling the interrupt gate.
689 *
690 * @returns VBox status code.
691 * g_pfnCallVMMR0 is changed on success.
692 * @param u8Interrupt The interrupt number.
693 */
694static int suplibGenerateCallVMMR0(uint8_t u8Interrupt)
695{
696 /*
697 * Allocate memory.
698 */
699 uint8_t *pb = (uint8_t *)RTMemExecAlloc(256);
700 AssertReturn(pb, VERR_NO_MEMORY);
701 memset(pb, 0xcc, 256);
702 Assert(!g_pfnCallVMMR0);
703 g_pfnCallVMMR0 = *(PFNCALLVMMR0*)&pb;
704
705 /*
706 * Generate the code.
707 */
708#ifdef __AMD64__
709 /*
710 * reg params:
711 * <GCC> <MSC> <argument>
712 * rdi rcx pVM
713 * esi edx uOperation
714 * rdx r8 pvArg
715 *
716 * eax eax [g_u32Gookie]
717 */
718 *pb++ = 0xb8; /* mov eax, <g_u32Cookie> */
719 *(uint32_t *)pb = g_u32Cookie;
720 pb += sizeof(uint32_t);
721
722 *pb++ = 0xcd; /* int <u8Interrupt> */
723 *pb++ = u8Interrupt;
724
725 *pb++ = 0xc3; /* ret */
726
727#else
728 /*
729 * x86 stack:
730 * 0 saved esi
731 * 0 4 ret
732 * 4 8 pVM
733 * 8 c uOperation
734 * c 10 pvArg
735 */
736 *pb++ = 0x56; /* push esi */
737
738 *pb++ = 0x8b; /* mov eax, [pVM] */
739 *pb++ = 0x44;
740 *pb++ = 0x24;
741 *pb++ = 0x08; /* esp+08h */
742
743 *pb++ = 0x8b; /* mov edx, [uOperation] */
744 *pb++ = 0x54;
745 *pb++ = 0x24;
746 *pb++ = 0x0c; /* esp+0ch */
747
748 *pb++ = 0x8b; /* mov ecx, [pvArg] */
749 *pb++ = 0x4c;
750 *pb++ = 0x24;
751 *pb++ = 0x10; /* esp+10h */
752
753 *pb++ = 0xbe; /* mov esi, <g_u32Cookie> */
754 *(uint32_t *)pb = g_u32Cookie;
755 pb += sizeof(uint32_t);
756
757 *pb++ = 0xcd; /* int <u8Interrupt> */
758 *pb++ = u8Interrupt;
759
760 *pb++ = 0x5e; /* pop esi */
761
762 *pb++ = 0xc3; /* ret */
763#endif
764
765 return VINF_SUCCESS;
766}
767
768
769/**
770 * Installs the IDTE patch.
771 *
772 * @return VBox status code.
773 */
774static int supInstallIDTE(void)
775{
776 /* already installed? */
777 if (g_u8Interrupt != 3 || g_u32FakeMode)
778 return VINF_SUCCESS;
779
780 int rc = VINF_SUCCESS;
781 const unsigned cCpus = RTSystemProcessorGetCount();
782 if (cCpus <= 1)
783 {
784 /* UNI */
785 SUPIDTINSTALL_IN In;
786 In.u32Cookie = g_u32Cookie;
787 In.u32SessionCookie = g_u32SessionCookie;
788 SUPIDTINSTALL_OUT Out = {3};
789
790 rc = suplibOsIOCtl(SUP_IOCTL_IDT_INSTALL, &In, sizeof(In), &Out, sizeof(Out));
791 if (VBOX_SUCCESS(rc))
792 {
793 g_u8Interrupt = Out.u8Idt;
794 rc = suplibGenerateCallVMMR0(Out.u8Idt);
795 }
796 }
797 else
798 {
799 /* SMP */
800 uint64_t u64AffMaskSaved = RTThreadGetAffinity();
801 uint64_t u64AffMaskPatched = RTSystemProcessorGetActiveMask() & u64AffMaskSaved;
802 unsigned cCpusPatched = 0;
803
804 for (int i = 0; i < 64; i++)
805 {
806 /* Skip absent and inactive processors. */
807 uint64_t u64Mask = 1ULL << i;
808 if (!(u64Mask & u64AffMaskPatched))
809 continue;
810
811 /* Change CPU */
812 int rc2 = RTThreadSetAffinity(u64Mask);
813 if (VBOX_FAILURE(rc2))
814 {
815 u64AffMaskPatched &= ~u64Mask;
816 Log(("SUPLoadVMM: Failed to set affinity to cpu no. %d, rc=%Vrc.\n", i, rc2));
817 continue;
818 }
819
820 /* Patch the CPU. */
821 SUPIDTINSTALL_IN In;
822 In.u32Cookie = g_u32Cookie;
823 In.u32SessionCookie = g_u32SessionCookie;
824 SUPIDTINSTALL_OUT Out = {3};
825
826 rc2 = suplibOsIOCtl(SUP_IOCTL_IDT_INSTALL, &In, sizeof(In), &Out, sizeof(Out));
827 if (VBOX_SUCCESS(rc2))
828 {
829 if (!cCpusPatched)
830 {
831 g_u8Interrupt = Out.u8Idt;
832 rc2 = suplibGenerateCallVMMR0(Out.u8Idt);
833 if (VBOX_FAILURE(rc))
834 rc2 = rc;
835 }
836 else
837 Assert(g_u8Interrupt == Out.u8Idt);
838 cCpusPatched++;
839 }
840 else
841 {
842
843 Log(("SUPLoadVMM: Failed to patch cpu no. %d, rc=%Vrc.\n", i, rc2));
844 if (VBOX_SUCCESS(rc))
845 rc = rc2;
846 }
847 }
848
849 /* Fail if no CPUs was patched! */
850 if (VBOX_SUCCESS(rc) && cCpusPatched <= 0)
851 rc = VERR_GENERAL_FAILURE;
852 /* Ignore failures if a CPU was patched. */
853 else if (VBOX_FAILURE(rc) && cCpusPatched > 0)
854 {
855 /** @todo add an eventlog/syslog line out this. */
856 rc = VINF_SUCCESS;
857 }
858
859 /* Set/restore the thread affinity. */
860 if (VBOX_SUCCESS(rc))
861 {
862 rc = RTThreadSetAffinity(u64AffMaskPatched);
863 AssertRC(rc);
864 }
865 else
866 {
867 int rc2 = RTThreadSetAffinity(u64AffMaskSaved);
868 AssertRC(rc2);
869 }
870 }
871 return rc;
872}
873#endif /* !VBOX_WITHOUT_IDT_PATCHING */
874
875
876/**
877 * Resolve an external symbol during RTLdrGetBits().
878 *
879 * @returns VBox status code.
880 * @param hLdrMod The loader module handle.
881 * @param pszModule Module name.
882 * @param pszSymbol Symbol name, NULL if uSymbol should be used.
883 * @param uSymbol Symbol ordinal, ~0 if pszSymbol should be used.
884 * @param pValue Where to store the symbol value (address).
885 * @param pvUser User argument.
886 */
887static DECLCALLBACK(int) supLoadModuleResolveImport(RTLDRMOD hLdrMod, const char *pszModule,
888 const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser)
889{
890 AssertPtr(pValue);
891 AssertPtr(pvUser);
892
893 /*
894 * Only SUPR0 and VMMR0.r0
895 */
896 if ( pszModule
897 && *pszModule
898 && strcmp(pszModule, "SUPR0.dll")
899 && strcmp(pszModule, "VMMR0.r0"))
900 {
901 AssertMsgFailed(("%s is importing from %s! (expected 'SUPR0.dll' or 'VMMR0.r0', case-sensitiv)\n", pvUser, pszModule));
902 return VERR_SYMBOL_NOT_FOUND;
903 }
904
905 /*
906 * No ordinals.
907 */
908 if (pszSymbol < (const char*)0x10000)
909 {
910 AssertMsgFailed(("%s is importing by ordinal (ord=%d)\n", pvUser, (int)(uintptr_t)pszSymbol));
911 return VERR_SYMBOL_NOT_FOUND;
912 }
913
914 /*
915 * Lookup symbol.
916 */
917 /* skip the 64-bit ELF import prefix first. */
918 if (!strncmp(pszSymbol, "SUPR0$", sizeof("SUPR0$") - 1))
919 pszSymbol += sizeof("SUPR0$") - 1;
920
921 /* iterate the function table. */
922 int c = g_pFunctions->cFunctions;
923 PSUPFUNC pFunc = &g_pFunctions->aFunctions[0];
924 while (c-- > 0)
925 {
926 if (!strcmp(pFunc->szName, pszSymbol))
927 {
928 *pValue = (uintptr_t)pFunc->pfn;
929 return VINF_SUCCESS;
930 }
931 pFunc++;
932 }
933
934 /*
935 * Check the VMMR0.r0 module if loaded.
936 */
937 /** @todo call the SUPLoadModule caller.... */
938 /** @todo proper reference counting and such. */
939 if (g_pvVMMR0)
940 {
941 void *pvValue;
942 if (!SUPGetSymbolR0(g_pvVMMR0, pszSymbol, &pvValue))
943 {
944 *pValue = (uintptr_t)pvValue;
945 return VINF_SUCCESS;
946 }
947 }
948
949 /*
950 * The GIP.
951 */
952 /** @todo R0 mapping? */
953 if ( pszSymbol
954 && g_pSUPGlobalInfoPage
955 && g_pSUPGlobalInfoPageR0
956 && !strcmp(pszSymbol, "g_SUPGlobalInfoPage"))
957 {
958 *pValue = (uintptr_t)g_pSUPGlobalInfoPageR0;
959 return VINF_SUCCESS;
960 }
961
962 /*
963 * Despair.
964 */
965 c = g_pFunctions->cFunctions;
966 pFunc = &g_pFunctions->aFunctions[0];
967 while (c-- > 0)
968 {
969 AssertMsg2("%d: %s\n", g_pFunctions->cFunctions - c, pFunc->szName);
970 pFunc++;
971 }
972
973 AssertMsgFailed(("%s is importing %s which we couldn't find\n", pvUser, pszSymbol));
974 return VERR_SYMBOL_NOT_FOUND;
975}
976
977
978/** Argument package for supLoadModuleCalcSizeCB. */
979typedef struct SUPLDRCALCSIZEARGS
980{
981 size_t cbStrings;
982 uint32_t cSymbols;
983 size_t cbImage;
984} SUPLDRCALCSIZEARGS, *PSUPLDRCALCSIZEARGS;
985
986/**
987 * Callback used to calculate the image size.
988 * @return VINF_SUCCESS
989 */
990static DECLCALLBACK(int) supLoadModuleCalcSizeCB(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
991{
992 PSUPLDRCALCSIZEARGS pArgs = (PSUPLDRCALCSIZEARGS)pvUser;
993 if ( pszSymbol != NULL
994 && *pszSymbol
995 && Value <= pArgs->cbImage)
996 {
997 pArgs->cSymbols++;
998 pArgs->cbStrings += strlen(pszSymbol) + 1;
999 }
1000 return VINF_SUCCESS;
1001}
1002
1003
1004/** Argument package for supLoadModuleCreateTabsCB. */
1005typedef struct SUPLDRCREATETABSARGS
1006{
1007 size_t cbImage;
1008 PSUPLDRSYM pSym;
1009 char *pszBase;
1010 char *psz;
1011} SUPLDRCREATETABSARGS, *PSUPLDRCREATETABSARGS;
1012
1013/**
1014 * Callback used to calculate the image size.
1015 * @return VINF_SUCCESS
1016 */
1017static DECLCALLBACK(int) supLoadModuleCreateTabsCB(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
1018{
1019 PSUPLDRCREATETABSARGS pArgs = (PSUPLDRCREATETABSARGS)pvUser;
1020 if ( pszSymbol != NULL
1021 && *pszSymbol
1022 && Value <= pArgs->cbImage)
1023 {
1024 pArgs->pSym->offSymbol = (uint32_t)Value;
1025 pArgs->pSym->offName = pArgs->psz - pArgs->pszBase;
1026 pArgs->pSym++;
1027
1028 size_t cbCopy = strlen(pszSymbol) + 1;
1029 memcpy(pArgs->psz, pszSymbol, cbCopy);
1030 pArgs->psz += cbCopy;
1031 }
1032 return VINF_SUCCESS;
1033}
1034
1035
1036/**
1037 * Worker for SUPLoadModule().
1038 *
1039 * @returns VBox status code.
1040 * @param pszFilename Name of the VMMR0 image file
1041 */
1042static int supLoadModule(const char *pszFilename, const char *pszModule, void **ppvImageBase)
1043{
1044 /*
1045 * Validate input.
1046 */
1047 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
1048 AssertPtrReturn(pszModule, VERR_INVALID_PARAMETER);
1049 AssertPtrReturn(ppvImageBase, VERR_INVALID_PARAMETER);
1050 AssertReturn(strlen(pszModule) < SIZEOFMEMB(SUPLDROPEN_IN, szName), VERR_FILENAME_TOO_LONG);
1051
1052 const bool fIsVMMR0 = !strcmp(pszModule, "VMMR0.r0");
1053 *ppvImageBase = NULL;
1054
1055 /*
1056 * Open image file and figure its size.
1057 */
1058 RTLDRMOD hLdrMod;
1059 int rc = RTLdrOpen(pszFilename, &hLdrMod);
1060 if (!VBOX_SUCCESS(rc))
1061 return rc;
1062
1063 SUPLDRCALCSIZEARGS CalcArgs;
1064 CalcArgs.cbStrings = 0;
1065 CalcArgs.cSymbols = 0;
1066 CalcArgs.cbImage = RTLdrSize(hLdrMod);
1067 rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCalcSizeCB, &CalcArgs);
1068 if (VBOX_SUCCESS(rc))
1069 {
1070 const uint32_t offSymTab = RT_ALIGN_32(CalcArgs.cbImage, 8);
1071 const uint32_t offStrTab = offSymTab + CalcArgs.cSymbols * sizeof(SUPLDRSYM);
1072 const uint32_t cbImage = RT_ALIGN_32(offStrTab + CalcArgs.cbStrings, 8);
1073
1074 /*
1075 * Open the R0 image.
1076 */
1077 SUPLDROPEN_IN OpenIn;
1078 OpenIn.u32Cookie = g_u32Cookie;
1079 OpenIn.u32SessionCookie = g_u32SessionCookie;
1080 OpenIn.cbImage = cbImage;
1081 strcpy(OpenIn.szName, pszModule);
1082 SUPLDROPEN_OUT OpenOut;
1083 if (!g_u32FakeMode)
1084 rc = suplibOsIOCtl(SUP_IOCTL_LDR_OPEN, &OpenIn, sizeof(OpenIn), &OpenOut, sizeof(OpenOut));
1085 else
1086 {
1087 OpenOut.fNeedsLoading = true;
1088 OpenOut.pvImageBase = (void *)0xef423420;
1089 }
1090 *ppvImageBase = OpenOut.pvImageBase;
1091 if ( VBOX_SUCCESS(rc)
1092 && OpenOut.fNeedsLoading)
1093 {
1094 /*
1095 * We need to load it.
1096 * Allocate memory for the image bits.
1097 */
1098 unsigned cbIn = RT_OFFSETOF(SUPLDRLOAD_IN, achImage[cbImage]);
1099 PSUPLDRLOAD_IN pIn = (PSUPLDRLOAD_IN)RTMemTmpAlloc(cbIn);
1100 if (pIn)
1101 {
1102 /*
1103 * Get the image bits.
1104 */
1105 rc = RTLdrGetBits(hLdrMod, &pIn->achImage[0], (uintptr_t)OpenOut.pvImageBase,
1106 supLoadModuleResolveImport, (void *)pszModule);
1107
1108 /*
1109 * Get the entry points.
1110 */
1111 RTUINTPTR VMMR0Entry = 0;
1112 RTUINTPTR ModuleInit = 0;
1113 RTUINTPTR ModuleTerm = 0;
1114 if (fIsVMMR0 && VBOX_SUCCESS(rc))
1115 rc = RTLdrGetSymbolEx(hLdrMod, &pIn->achImage[0], (uintptr_t)OpenOut.pvImageBase, "VMMR0Entry", &VMMR0Entry);
1116 if (VBOX_SUCCESS(rc))
1117 {
1118 rc = RTLdrGetSymbolEx(hLdrMod, &pIn->achImage[0], (uintptr_t)OpenOut.pvImageBase, "ModuleInit", &ModuleInit);
1119 if (VBOX_FAILURE(rc))
1120 ModuleInit = 0;
1121
1122 rc = RTLdrGetSymbolEx(hLdrMod, &pIn->achImage[0], (uintptr_t)OpenOut.pvImageBase, "ModuleTerm", &ModuleTerm);
1123 if (VBOX_FAILURE(rc))
1124 ModuleTerm = 0;
1125 }
1126
1127 /*
1128 * Create the symbol and string tables.
1129 */
1130 SUPLDRCREATETABSARGS CreateArgs;
1131 CreateArgs.cbImage = CalcArgs.cbImage;
1132 CreateArgs.pSym = (PSUPLDRSYM)&pIn->achImage[offSymTab];
1133 CreateArgs.pszBase = (char *)&pIn->achImage[offStrTab];
1134 CreateArgs.psz = CreateArgs.pszBase;
1135 rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCreateTabsCB, &CreateArgs);
1136 if (VBOX_SUCCESS(rc))
1137 {
1138 AssertRelease((size_t)(CreateArgs.psz - CreateArgs.pszBase) <= CalcArgs.cbStrings);
1139 AssertRelease((size_t)(CreateArgs.pSym - (PSUPLDRSYM)&pIn->achImage[offSymTab]) <= CalcArgs.cSymbols);
1140
1141 /*
1142 * Upload the image.
1143 */
1144 pIn->u32Cookie = g_u32Cookie;
1145 pIn->u32SessionCookie = g_u32SessionCookie;
1146 pIn->pfnModuleInit = (PFNR0MODULEINIT)(uintptr_t)ModuleInit;
1147 pIn->pfnModuleTerm = (PFNR0MODULETERM)(uintptr_t)ModuleTerm;
1148 if (fIsVMMR0)
1149 {
1150 pIn->eEPType = pIn->EP_VMMR0;
1151 pIn->EP.VMMR0.pvVMMR0 = OpenOut.pvImageBase;
1152 pIn->EP.VMMR0.pvVMMR0Entry = (void *)(uintptr_t)VMMR0Entry;
1153 }
1154 else
1155 pIn->eEPType = pIn->EP_NOTHING;
1156 pIn->offStrTab = offStrTab;
1157 pIn->cbStrTab = CalcArgs.cbStrings;
1158 pIn->offSymbols = offSymTab;
1159 pIn->cSymbols = CalcArgs.cSymbols;
1160 pIn->cbImage = cbImage;
1161 pIn->pvImageBase = OpenOut.pvImageBase;
1162 if (!g_u32FakeMode)
1163 rc = suplibOsIOCtl(SUP_IOCTL_LDR_LOAD, pIn, cbIn, NULL, 0);
1164 else
1165 rc = VINF_SUCCESS;
1166 if ( VBOX_SUCCESS(rc)
1167 || rc == VERR_ALREADY_LOADED /* this is because of a competing process. */
1168 )
1169 {
1170 if (fIsVMMR0)
1171 g_pvVMMR0 = OpenOut.pvImageBase;
1172 RTMemTmpFree(pIn);
1173 RTLdrClose(hLdrMod);
1174 return VINF_SUCCESS;
1175 }
1176 }
1177 RTMemTmpFree(pIn);
1178 }
1179 else
1180 {
1181 AssertMsgFailed(("failed to allocated %d bytes for SUPLDRLOAD_IN structure!\n", cbIn));
1182 rc = VERR_NO_TMP_MEMORY;
1183 }
1184 }
1185 }
1186 RTLdrClose(hLdrMod);
1187 return rc;
1188}
1189
1190
1191SUPR3DECL(int) SUPFreeModule(void *pvImageBase)
1192{
1193 /*
1194 * There is one special module. When this is freed we'll
1195 * free the IDT entry that goes with it.
1196 *
1197 * Note that we don't keep count of VMMR0.r0 loads here, so the
1198 * first unload will free it.
1199 */
1200 if (pvImageBase == g_pvVMMR0)
1201 {
1202 /*
1203 * This is the point where we remove the IDT hook. We do
1204 * that before unloading the R0 VMM part.
1205 */
1206 if (g_u32FakeMode)
1207 {
1208#ifndef VBOX_WITHOUT_IDT_PATCHING
1209 g_u8Interrupt = 3;
1210 RTMemExecFree(*(void **)&g_pfnCallVMMR0);
1211 g_pfnCallVMMR0 = NULL;
1212#endif
1213 g_pvVMMR0 = NULL;
1214 return VINF_SUCCESS;
1215 }
1216
1217#ifndef VBOX_WITHOUT_IDT_PATCHING
1218 /*
1219 * Uninstall IDT entry.
1220 */
1221 int rc = 0;
1222 if (g_u8Interrupt != 3)
1223 {
1224 SUPIDTREMOVE_IN In;
1225 In.u32Cookie = g_u32Cookie;
1226 In.u32SessionCookie = g_u32SessionCookie;
1227 rc = suplibOsIOCtl(SUP_IOCTL_IDT_REMOVE, &In, sizeof(In), NULL, 0);
1228 g_u8Interrupt = 3;
1229 RTMemExecFree(*(void **)&g_pfnCallVMMR0);
1230 g_pfnCallVMMR0 = NULL;
1231 }
1232#endif
1233 }
1234
1235 /*
1236 * Free the requested module.
1237 */
1238 SUPLDRFREE_IN In;
1239 In.u32Cookie = g_u32Cookie;
1240 In.u32SessionCookie = g_u32SessionCookie;
1241 In.pvImageBase = pvImageBase;
1242 int rc = VINF_SUCCESS;
1243 if (!g_u32FakeMode)
1244 rc = suplibOsIOCtl(SUP_IOCTL_LDR_FREE, &In, sizeof(In), NULL, 0);
1245 if ( VBOX_SUCCESS(rc)
1246 && pvImageBase == g_pvVMMR0)
1247 g_pvVMMR0 = NULL;
1248 return rc;
1249}
1250
1251
1252SUPR3DECL(int) SUPGetSymbolR0(void *pvImageBase, const char *pszSymbol, void **ppvValue)
1253{
1254 *ppvValue = NULL;
1255
1256 /*
1257 * Do ioctl.
1258 */
1259 size_t cchSymbol = strlen(pszSymbol);
1260 const size_t cbIn = RT_OFFSETOF(SUPLDRGETSYMBOL_IN, szSymbol[cchSymbol + 1]);
1261 SUPLDRGETSYMBOL_OUT Out = { NULL };
1262 PSUPLDRGETSYMBOL_IN pIn = (PSUPLDRGETSYMBOL_IN)alloca(cbIn);
1263 pIn->u32Cookie = g_u32Cookie;
1264 pIn->u32SessionCookie = g_u32SessionCookie;
1265 pIn->pvImageBase = pvImageBase;
1266 memcpy(pIn->szSymbol, pszSymbol, cchSymbol + 1);
1267 int rc = suplibOsIOCtl(SUP_IOCTL_LDR_GET_SYMBOL, pIn, cbIn, &Out, sizeof(Out));
1268 if (VBOX_SUCCESS(rc))
1269 *ppvValue = Out.pvSymbol;
1270 return rc;
1271}
1272
1273
1274SUPR3DECL(int) SUPLoadVMM(const char *pszFilename)
1275{
1276 void *pvImageBase;
1277 return SUPLoadModule(pszFilename, "VMMR0.r0", &pvImageBase);
1278}
1279
1280
1281SUPR3DECL(int) SUPUnloadVMM(void)
1282{
1283 return SUPFreeModule(g_pvVMMR0);
1284}
1285
1286
1287SUPR3DECL(int) SUPGipGetPhys(PRTHCPHYS pHCPhys)
1288{
1289 if (g_pSUPGlobalInfoPage)
1290 {
1291 *pHCPhys = g_HCPhysSUPGlobalInfoPage;
1292 return VINF_SUCCESS;
1293 }
1294 *pHCPhys = NIL_RTHCPHYS;
1295 return VERR_WRONG_ORDER;
1296}
1297
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