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 | *******************************************************************************/
|
---|
81 | typedef DECLCALLBACK(int) FNCALLVMMR0(PVM pVM, unsigned uOperation, void *pvArg);
|
---|
82 | typedef 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 | */
|
---|
96 | DECLEXPORT(PCSUPGLOBALINFOPAGE) g_pSUPGlobalInfoPage;
|
---|
97 | /** Address of the ring-0 mapping of the GIP. */
|
---|
98 | static PCSUPGLOBALINFOPAGE g_pSUPGlobalInfoPageR0;
|
---|
99 | /** The physical address of the GIP. */
|
---|
100 | static RTHCPHYS g_HCPhysSUPGlobalInfoPage = NIL_RTHCPHYS;
|
---|
101 |
|
---|
102 | /** The negotiated cookie. */
|
---|
103 | uint32_t g_u32Cookie = 0;
|
---|
104 | /** The negotiated session cookie. */
|
---|
105 | uint32_t g_u32SessionCookie;
|
---|
106 | /** Session handle. */
|
---|
107 | PSUPDRVSESSION g_pSession;
|
---|
108 | /** R0 SUP Functions used for resolving referenced to the SUPR0 module. */
|
---|
109 | static PSUPQUERYFUNCS_OUT g_pFunctions;
|
---|
110 |
|
---|
111 | #ifndef VBOX_WITHOUT_IDT_PATCHING
|
---|
112 | /** The negotiated interrupt number. */
|
---|
113 | static uint8_t g_u8Interrupt = 3;
|
---|
114 | /** Pointer to the generated code fore calling VMMR0. */
|
---|
115 | static PFNCALLVMMR0 g_pfnCallVMMR0;
|
---|
116 | #endif
|
---|
117 | /** VMMR0 Load Address. */
|
---|
118 | static void *g_pvVMMR0 = NULL;
|
---|
119 | /** Init counter. */
|
---|
120 | static unsigned g_cInits = 0;
|
---|
121 | /** Fake mode indicator. (~0 at first, 0 or 1 after first test) */
|
---|
122 | static uint32_t g_u32FakeMode = ~0;
|
---|
123 |
|
---|
124 |
|
---|
125 | /*******************************************************************************
|
---|
126 | * Internal Functions *
|
---|
127 | *******************************************************************************/
|
---|
128 | static int supLoadModule(const char *pszFilename, const char *pszModule, void **ppvImageBase);
|
---|
129 | #ifndef VBOX_WITHOUT_IDT_PATCHING
|
---|
130 | static int supInstallIDTE(void);
|
---|
131 | #endif
|
---|
132 | static DECLCALLBACK(int) supLoadModuleResolveImport(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser);
|
---|
133 |
|
---|
134 |
|
---|
135 | SUPR3DECL(int) SUPInstall(void)
|
---|
136 | {
|
---|
137 | return suplibOsInstall();
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | SUPR3DECL(int) SUPUninstall(void)
|
---|
142 | {
|
---|
143 | return suplibOsUninstall();
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | SUPR3DECL(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 |
|
---|
293 | SUPR3DECL(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 |
|
---|
336 | SUPR3DECL(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 |
|
---|
358 | SUPR3DECL(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 |
|
---|
379 | SUPR3DECL(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 |
|
---|
399 | SUPR3DECL(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 |
|
---|
410 | SUPR3DECL(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 |
|
---|
447 | SUPR3DECL(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 |
|
---|
472 | SUPR3DECL(void *) SUPContAlloc(unsigned cb, PRTHCPHYS pHCPhys)
|
---|
473 | {
|
---|
474 | return SUPContAlloc2(cb, NULL, pHCPhys);
|
---|
475 | }
|
---|
476 |
|
---|
477 |
|
---|
478 | SUPR3DECL(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 |
|
---|
516 | SUPR3DECL(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 |
|
---|
542 | SUPR3DECL(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 |
|
---|
595 | SUPR3DECL(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 |
|
---|
621 | SUPR3DECL(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 |
|
---|
643 | SUPR3DECL(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 |
|
---|
659 | SUPR3DECL(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 | */
|
---|
688 | static 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 | #ifndef __WIN__
|
---|
713 | *pb++ = 0xcc; /* fix me!! */
|
---|
714 | *pb++ = 0xc3;
|
---|
715 | #endif
|
---|
716 | *pb++ = 0xb8; /* mov eax, <g_u32Cookie> */
|
---|
717 | *(uint32_t *)pb = g_u32Cookie;
|
---|
718 | pb += sizeof(uint32_t);
|
---|
719 |
|
---|
720 | *pb++ = 0xcd; /* int <u8Interrupt> */
|
---|
721 | *pb++ = u8Interrupt;
|
---|
722 |
|
---|
723 | *pb++ = 0xc3; /* ret */
|
---|
724 |
|
---|
725 | #else
|
---|
726 | /*
|
---|
727 | * x86 stack:
|
---|
728 | * 0 saved esi
|
---|
729 | * 0 4 ret
|
---|
730 | * 4 8 pVM
|
---|
731 | * 8 c uOperation
|
---|
732 | * c 10 pvArg
|
---|
733 | */
|
---|
734 | *pb++ = 0x56; /* push esi */
|
---|
735 |
|
---|
736 | *pb++ = 0x8b; /* mov eax, [pVM] */
|
---|
737 | *pb++ = 0x44;
|
---|
738 | *pb++ = 0x24;
|
---|
739 | *pb++ = 0x08; /* esp+08h */
|
---|
740 |
|
---|
741 | *pb++ = 0x8b; /* mov edx, [uOperation] */
|
---|
742 | *pb++ = 0x54;
|
---|
743 | *pb++ = 0x24;
|
---|
744 | *pb++ = 0x0c; /* esp+0ch */
|
---|
745 |
|
---|
746 | *pb++ = 0x8b; /* mov ecx, [pvArg] */
|
---|
747 | *pb++ = 0x4c;
|
---|
748 | *pb++ = 0x24;
|
---|
749 | *pb++ = 0x10; /* esp+10h */
|
---|
750 |
|
---|
751 | *pb++ = 0xbe; /* mov esi, <g_u32Cookie> */
|
---|
752 | *(uint32_t *)pb = g_u32Cookie;
|
---|
753 | pb += sizeof(uint32_t);
|
---|
754 |
|
---|
755 | *pb++ = 0xcd; /* int <u8Interrupt> */
|
---|
756 | *pb++ = u8Interrupt;
|
---|
757 |
|
---|
758 | *pb++ = 0x5e; /* pop esi */
|
---|
759 |
|
---|
760 | *pb++ = 0xc3; /* ret */
|
---|
761 | #endif
|
---|
762 |
|
---|
763 | return VINF_SUCCESS;
|
---|
764 | }
|
---|
765 |
|
---|
766 |
|
---|
767 | /**
|
---|
768 | * Installs the IDTE patch.
|
---|
769 | *
|
---|
770 | * @return VBox status code.
|
---|
771 | */
|
---|
772 | static int supInstallIDTE(void)
|
---|
773 | {
|
---|
774 | /* already installed? */
|
---|
775 | if (g_u8Interrupt != 3 || g_u32FakeMode)
|
---|
776 | return VINF_SUCCESS;
|
---|
777 |
|
---|
778 | int rc = VINF_SUCCESS;
|
---|
779 | const unsigned cCpus = RTSystemProcessorGetCount();
|
---|
780 | if (cCpus <= 1)
|
---|
781 | {
|
---|
782 | /* UNI */
|
---|
783 | SUPIDTINSTALL_IN In;
|
---|
784 | In.u32Cookie = g_u32Cookie;
|
---|
785 | In.u32SessionCookie = g_u32SessionCookie;
|
---|
786 | SUPIDTINSTALL_OUT Out = {3};
|
---|
787 |
|
---|
788 | rc = suplibOsIOCtl(SUP_IOCTL_IDT_INSTALL, &In, sizeof(In), &Out, sizeof(Out));
|
---|
789 | if (VBOX_SUCCESS(rc))
|
---|
790 | {
|
---|
791 | g_u8Interrupt = Out.u8Idt;
|
---|
792 | rc = suplibGenerateCallVMMR0(Out.u8Idt);
|
---|
793 | }
|
---|
794 | }
|
---|
795 | else
|
---|
796 | {
|
---|
797 | /* SMP */
|
---|
798 | uint64_t u64AffMaskSaved = RTThreadGetAffinity();
|
---|
799 | uint64_t u64AffMaskPatched = RTSystemProcessorGetActiveMask() & u64AffMaskSaved;
|
---|
800 | unsigned cCpusPatched = 0;
|
---|
801 |
|
---|
802 | for (int i = 0; i < 64; i++)
|
---|
803 | {
|
---|
804 | /* Skip absent and inactive processors. */
|
---|
805 | uint64_t u64Mask = 1ULL << i;
|
---|
806 | if (!(u64Mask & u64AffMaskPatched))
|
---|
807 | continue;
|
---|
808 |
|
---|
809 | /* Change CPU */
|
---|
810 | int rc2 = RTThreadSetAffinity(u64Mask);
|
---|
811 | if (VBOX_FAILURE(rc2))
|
---|
812 | {
|
---|
813 | u64AffMaskPatched &= ~u64Mask;
|
---|
814 | Log(("SUPLoadVMM: Failed to set affinity to cpu no. %d, rc=%Vrc.\n", i, rc2));
|
---|
815 | continue;
|
---|
816 | }
|
---|
817 |
|
---|
818 | /* Patch the CPU. */
|
---|
819 | SUPIDTINSTALL_IN In;
|
---|
820 | In.u32Cookie = g_u32Cookie;
|
---|
821 | In.u32SessionCookie = g_u32SessionCookie;
|
---|
822 | SUPIDTINSTALL_OUT Out = {3};
|
---|
823 |
|
---|
824 | rc2 = suplibOsIOCtl(SUP_IOCTL_IDT_INSTALL, &In, sizeof(In), &Out, sizeof(Out));
|
---|
825 | if (VBOX_SUCCESS(rc2))
|
---|
826 | {
|
---|
827 | if (!cCpusPatched)
|
---|
828 | {
|
---|
829 | g_u8Interrupt = Out.u8Idt;
|
---|
830 | rc2 = suplibGenerateCallVMMR0(Out.u8Idt);
|
---|
831 | if (VBOX_FAILURE(rc))
|
---|
832 | rc2 = rc;
|
---|
833 | }
|
---|
834 | else
|
---|
835 | Assert(g_u8Interrupt == Out.u8Idt);
|
---|
836 | cCpusPatched++;
|
---|
837 | }
|
---|
838 | else
|
---|
839 | {
|
---|
840 |
|
---|
841 | Log(("SUPLoadVMM: Failed to patch cpu no. %d, rc=%Vrc.\n", i, rc2));
|
---|
842 | if (VBOX_SUCCESS(rc))
|
---|
843 | rc = rc2;
|
---|
844 | }
|
---|
845 | }
|
---|
846 |
|
---|
847 | /* Fail if no CPUs was patched! */
|
---|
848 | if (VBOX_SUCCESS(rc) && cCpusPatched <= 0)
|
---|
849 | rc = VERR_GENERAL_FAILURE;
|
---|
850 | /* Ignore failures if a CPU was patched. */
|
---|
851 | else if (VBOX_FAILURE(rc) && cCpusPatched > 0)
|
---|
852 | {
|
---|
853 | /** @todo add an eventlog/syslog line out this. */
|
---|
854 | rc = VINF_SUCCESS;
|
---|
855 | }
|
---|
856 |
|
---|
857 | /* Set/restore the thread affinity. */
|
---|
858 | if (VBOX_SUCCESS(rc))
|
---|
859 | {
|
---|
860 | rc = RTThreadSetAffinity(u64AffMaskPatched);
|
---|
861 | AssertRC(rc);
|
---|
862 | }
|
---|
863 | else
|
---|
864 | {
|
---|
865 | int rc2 = RTThreadSetAffinity(u64AffMaskSaved);
|
---|
866 | AssertRC(rc2);
|
---|
867 | }
|
---|
868 | }
|
---|
869 | return rc;
|
---|
870 | }
|
---|
871 | #endif /* !VBOX_WITHOUT_IDT_PATCHING */
|
---|
872 |
|
---|
873 |
|
---|
874 | /**
|
---|
875 | * Resolve an external symbol during RTLdrGetBits().
|
---|
876 | *
|
---|
877 | * @returns VBox status code.
|
---|
878 | * @param hLdrMod The loader module handle.
|
---|
879 | * @param pszModule Module name.
|
---|
880 | * @param pszSymbol Symbol name, NULL if uSymbol should be used.
|
---|
881 | * @param uSymbol Symbol ordinal, ~0 if pszSymbol should be used.
|
---|
882 | * @param pValue Where to store the symbol value (address).
|
---|
883 | * @param pvUser User argument.
|
---|
884 | */
|
---|
885 | static DECLCALLBACK(int) supLoadModuleResolveImport(RTLDRMOD hLdrMod, const char *pszModule,
|
---|
886 | const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser)
|
---|
887 | {
|
---|
888 | AssertPtr(pValue);
|
---|
889 | AssertPtr(pvUser);
|
---|
890 |
|
---|
891 | /*
|
---|
892 | * Only SUPR0 and VMMR0.r0
|
---|
893 | */
|
---|
894 | if ( pszModule
|
---|
895 | && *pszModule
|
---|
896 | && strcmp(pszModule, "SUPR0.dll")
|
---|
897 | && strcmp(pszModule, "VMMR0.r0"))
|
---|
898 | {
|
---|
899 | AssertMsgFailed(("%s is importing from %s! (expected 'SUPR0.dll' or 'VMMR0.r0', case-sensitiv)\n", pvUser, pszModule));
|
---|
900 | return VERR_SYMBOL_NOT_FOUND;
|
---|
901 | }
|
---|
902 |
|
---|
903 | /*
|
---|
904 | * No ordinals.
|
---|
905 | */
|
---|
906 | if (pszSymbol < (const char*)0x10000)
|
---|
907 | {
|
---|
908 | AssertMsgFailed(("%s is importing by ordinal (ord=%d)\n", pvUser, (int)(uintptr_t)pszSymbol));
|
---|
909 | return VERR_SYMBOL_NOT_FOUND;
|
---|
910 | }
|
---|
911 |
|
---|
912 | /*
|
---|
913 | * Lookup symbol.
|
---|
914 | */
|
---|
915 | /* skip the 64-bit ELF import prefix first. */
|
---|
916 | if (!strncmp(pszSymbol, "SUPR0$", sizeof("SUPR0$") - 1))
|
---|
917 | pszSymbol += sizeof("SUPR0$") - 1;
|
---|
918 |
|
---|
919 | /* iterate the function table. */
|
---|
920 | int c = g_pFunctions->cFunctions;
|
---|
921 | PSUPFUNC pFunc = &g_pFunctions->aFunctions[0];
|
---|
922 | while (c-- > 0)
|
---|
923 | {
|
---|
924 | if (!strcmp(pFunc->szName, pszSymbol))
|
---|
925 | {
|
---|
926 | *pValue = (uintptr_t)pFunc->pfn;
|
---|
927 | return VINF_SUCCESS;
|
---|
928 | }
|
---|
929 | pFunc++;
|
---|
930 | }
|
---|
931 |
|
---|
932 | /*
|
---|
933 | * Check the VMMR0.r0 module if loaded.
|
---|
934 | */
|
---|
935 | /** @todo call the SUPLoadModule caller.... */
|
---|
936 | /** @todo proper reference counting and such. */
|
---|
937 | if (g_pvVMMR0)
|
---|
938 | {
|
---|
939 | void *pvValue;
|
---|
940 | if (!SUPGetSymbolR0(g_pvVMMR0, pszSymbol, &pvValue))
|
---|
941 | {
|
---|
942 | *pValue = (uintptr_t)pvValue;
|
---|
943 | return VINF_SUCCESS;
|
---|
944 | }
|
---|
945 | }
|
---|
946 |
|
---|
947 | /*
|
---|
948 | * The GIP.
|
---|
949 | */
|
---|
950 | /** @todo R0 mapping? */
|
---|
951 | if ( pszSymbol
|
---|
952 | && g_pSUPGlobalInfoPage
|
---|
953 | && g_pSUPGlobalInfoPageR0
|
---|
954 | && !strcmp(pszSymbol, "g_SUPGlobalInfoPage"))
|
---|
955 | {
|
---|
956 | *pValue = (uintptr_t)g_pSUPGlobalInfoPageR0;
|
---|
957 | return VINF_SUCCESS;
|
---|
958 | }
|
---|
959 |
|
---|
960 | /*
|
---|
961 | * Despair.
|
---|
962 | */
|
---|
963 | c = g_pFunctions->cFunctions;
|
---|
964 | pFunc = &g_pFunctions->aFunctions[0];
|
---|
965 | while (c-- > 0)
|
---|
966 | {
|
---|
967 | AssertMsg2("%d: %s\n", g_pFunctions->cFunctions - c, pFunc->szName);
|
---|
968 | pFunc++;
|
---|
969 | }
|
---|
970 |
|
---|
971 | AssertMsgFailed(("%s is importing %s which we couldn't find\n", pvUser, pszSymbol));
|
---|
972 | return VERR_SYMBOL_NOT_FOUND;
|
---|
973 | }
|
---|
974 |
|
---|
975 |
|
---|
976 | /** Argument package for supLoadModuleCalcSizeCB. */
|
---|
977 | typedef struct SUPLDRCALCSIZEARGS
|
---|
978 | {
|
---|
979 | size_t cbStrings;
|
---|
980 | uint32_t cSymbols;
|
---|
981 | size_t cbImage;
|
---|
982 | } SUPLDRCALCSIZEARGS, *PSUPLDRCALCSIZEARGS;
|
---|
983 |
|
---|
984 | /**
|
---|
985 | * Callback used to calculate the image size.
|
---|
986 | * @return VINF_SUCCESS
|
---|
987 | */
|
---|
988 | static DECLCALLBACK(int) supLoadModuleCalcSizeCB(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
|
---|
989 | {
|
---|
990 | PSUPLDRCALCSIZEARGS pArgs = (PSUPLDRCALCSIZEARGS)pvUser;
|
---|
991 | if ( pszSymbol != NULL
|
---|
992 | && *pszSymbol
|
---|
993 | && Value <= pArgs->cbImage)
|
---|
994 | {
|
---|
995 | pArgs->cSymbols++;
|
---|
996 | pArgs->cbStrings += strlen(pszSymbol) + 1;
|
---|
997 | }
|
---|
998 | return VINF_SUCCESS;
|
---|
999 | }
|
---|
1000 |
|
---|
1001 |
|
---|
1002 | /** Argument package for supLoadModuleCreateTabsCB. */
|
---|
1003 | typedef struct SUPLDRCREATETABSARGS
|
---|
1004 | {
|
---|
1005 | size_t cbImage;
|
---|
1006 | PSUPLDRSYM pSym;
|
---|
1007 | char *pszBase;
|
---|
1008 | char *psz;
|
---|
1009 | } SUPLDRCREATETABSARGS, *PSUPLDRCREATETABSARGS;
|
---|
1010 |
|
---|
1011 | /**
|
---|
1012 | * Callback used to calculate the image size.
|
---|
1013 | * @return VINF_SUCCESS
|
---|
1014 | */
|
---|
1015 | static DECLCALLBACK(int) supLoadModuleCreateTabsCB(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
|
---|
1016 | {
|
---|
1017 | PSUPLDRCREATETABSARGS pArgs = (PSUPLDRCREATETABSARGS)pvUser;
|
---|
1018 | if ( pszSymbol != NULL
|
---|
1019 | && *pszSymbol
|
---|
1020 | && Value <= pArgs->cbImage)
|
---|
1021 | {
|
---|
1022 | pArgs->pSym->offSymbol = (uint32_t)Value;
|
---|
1023 | pArgs->pSym->offName = pArgs->psz - pArgs->pszBase;
|
---|
1024 | pArgs->pSym++;
|
---|
1025 |
|
---|
1026 | size_t cbCopy = strlen(pszSymbol) + 1;
|
---|
1027 | memcpy(pArgs->psz, pszSymbol, cbCopy);
|
---|
1028 | pArgs->psz += cbCopy;
|
---|
1029 | }
|
---|
1030 | return VINF_SUCCESS;
|
---|
1031 | }
|
---|
1032 |
|
---|
1033 |
|
---|
1034 | /**
|
---|
1035 | * Worker for SUPLoadModule().
|
---|
1036 | *
|
---|
1037 | * @returns VBox status code.
|
---|
1038 | * @param pszFilename Name of the VMMR0 image file
|
---|
1039 | */
|
---|
1040 | static int supLoadModule(const char *pszFilename, const char *pszModule, void **ppvImageBase)
|
---|
1041 | {
|
---|
1042 | /*
|
---|
1043 | * Validate input.
|
---|
1044 | */
|
---|
1045 | AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
|
---|
1046 | AssertPtrReturn(pszModule, VERR_INVALID_PARAMETER);
|
---|
1047 | AssertPtrReturn(ppvImageBase, VERR_INVALID_PARAMETER);
|
---|
1048 | AssertReturn(strlen(pszModule) < SIZEOFMEMB(SUPLDROPEN_IN, szName), VERR_FILENAME_TOO_LONG);
|
---|
1049 |
|
---|
1050 | const bool fIsVMMR0 = !strcmp(pszModule, "VMMR0.r0");
|
---|
1051 | *ppvImageBase = NULL;
|
---|
1052 |
|
---|
1053 | /*
|
---|
1054 | * Open image file and figure its size.
|
---|
1055 | */
|
---|
1056 | RTLDRMOD hLdrMod;
|
---|
1057 | int rc = RTLdrOpen(pszFilename, &hLdrMod);
|
---|
1058 | if (!VBOX_SUCCESS(rc))
|
---|
1059 | return rc;
|
---|
1060 |
|
---|
1061 | SUPLDRCALCSIZEARGS CalcArgs;
|
---|
1062 | CalcArgs.cbStrings = 0;
|
---|
1063 | CalcArgs.cSymbols = 0;
|
---|
1064 | CalcArgs.cbImage = RTLdrSize(hLdrMod);
|
---|
1065 | rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCalcSizeCB, &CalcArgs);
|
---|
1066 | if (VBOX_SUCCESS(rc))
|
---|
1067 | {
|
---|
1068 | const uint32_t offSymTab = RT_ALIGN_32(CalcArgs.cbImage, 8);
|
---|
1069 | const uint32_t offStrTab = offSymTab + CalcArgs.cSymbols * sizeof(SUPLDRSYM);
|
---|
1070 | const uint32_t cbImage = RT_ALIGN_32(offStrTab + CalcArgs.cbStrings, 8);
|
---|
1071 |
|
---|
1072 | /*
|
---|
1073 | * Open the R0 image.
|
---|
1074 | */
|
---|
1075 | SUPLDROPEN_IN OpenIn;
|
---|
1076 | OpenIn.u32Cookie = g_u32Cookie;
|
---|
1077 | OpenIn.u32SessionCookie = g_u32SessionCookie;
|
---|
1078 | OpenIn.cbImage = cbImage;
|
---|
1079 | strcpy(OpenIn.szName, pszModule);
|
---|
1080 | SUPLDROPEN_OUT OpenOut;
|
---|
1081 | if (!g_u32FakeMode)
|
---|
1082 | rc = suplibOsIOCtl(SUP_IOCTL_LDR_OPEN, &OpenIn, sizeof(OpenIn), &OpenOut, sizeof(OpenOut));
|
---|
1083 | else
|
---|
1084 | {
|
---|
1085 | OpenOut.fNeedsLoading = true;
|
---|
1086 | OpenOut.pvImageBase = (void *)0xef423420;
|
---|
1087 | }
|
---|
1088 | *ppvImageBase = OpenOut.pvImageBase;
|
---|
1089 | if ( VBOX_SUCCESS(rc)
|
---|
1090 | && OpenOut.fNeedsLoading)
|
---|
1091 | {
|
---|
1092 | /*
|
---|
1093 | * We need to load it.
|
---|
1094 | * Allocate memory for the image bits.
|
---|
1095 | */
|
---|
1096 | unsigned cbIn = RT_OFFSETOF(SUPLDRLOAD_IN, achImage[cbImage]);
|
---|
1097 | PSUPLDRLOAD_IN pIn = (PSUPLDRLOAD_IN)RTMemTmpAlloc(cbIn);
|
---|
1098 | if (pIn)
|
---|
1099 | {
|
---|
1100 | /*
|
---|
1101 | * Get the image bits.
|
---|
1102 | */
|
---|
1103 | rc = RTLdrGetBits(hLdrMod, &pIn->achImage[0], (uintptr_t)OpenOut.pvImageBase,
|
---|
1104 | supLoadModuleResolveImport, (void *)pszModule);
|
---|
1105 |
|
---|
1106 | /*
|
---|
1107 | * Get the entry points.
|
---|
1108 | */
|
---|
1109 | RTUINTPTR VMMR0Entry = 0;
|
---|
1110 | RTUINTPTR ModuleInit = 0;
|
---|
1111 | RTUINTPTR ModuleTerm = 0;
|
---|
1112 | if (fIsVMMR0 && VBOX_SUCCESS(rc))
|
---|
1113 | rc = RTLdrGetSymbolEx(hLdrMod, &pIn->achImage[0], (uintptr_t)OpenOut.pvImageBase, "VMMR0Entry", &VMMR0Entry);
|
---|
1114 | if (VBOX_SUCCESS(rc))
|
---|
1115 | {
|
---|
1116 | rc = RTLdrGetSymbolEx(hLdrMod, &pIn->achImage[0], (uintptr_t)OpenOut.pvImageBase, "ModuleInit", &ModuleInit);
|
---|
1117 | if (VBOX_FAILURE(rc))
|
---|
1118 | ModuleInit = 0;
|
---|
1119 |
|
---|
1120 | rc = RTLdrGetSymbolEx(hLdrMod, &pIn->achImage[0], (uintptr_t)OpenOut.pvImageBase, "ModuleTerm", &ModuleTerm);
|
---|
1121 | if (VBOX_FAILURE(rc))
|
---|
1122 | ModuleTerm = 0;
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 | /*
|
---|
1126 | * Create the symbol and string tables.
|
---|
1127 | */
|
---|
1128 | SUPLDRCREATETABSARGS CreateArgs;
|
---|
1129 | CreateArgs.cbImage = CalcArgs.cbImage;
|
---|
1130 | CreateArgs.pSym = (PSUPLDRSYM)&pIn->achImage[offSymTab];
|
---|
1131 | CreateArgs.pszBase = (char *)&pIn->achImage[offStrTab];
|
---|
1132 | CreateArgs.psz = CreateArgs.pszBase;
|
---|
1133 | rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCreateTabsCB, &CreateArgs);
|
---|
1134 | if (VBOX_SUCCESS(rc))
|
---|
1135 | {
|
---|
1136 | AssertRelease((size_t)(CreateArgs.psz - CreateArgs.pszBase) <= CalcArgs.cbStrings);
|
---|
1137 | AssertRelease((size_t)(CreateArgs.pSym - (PSUPLDRSYM)&pIn->achImage[offSymTab]) <= CalcArgs.cSymbols);
|
---|
1138 |
|
---|
1139 | /*
|
---|
1140 | * Upload the image.
|
---|
1141 | */
|
---|
1142 | pIn->u32Cookie = g_u32Cookie;
|
---|
1143 | pIn->u32SessionCookie = g_u32SessionCookie;
|
---|
1144 | pIn->pfnModuleInit = (PFNR0MODULEINIT)(uintptr_t)ModuleInit;
|
---|
1145 | pIn->pfnModuleTerm = (PFNR0MODULETERM)(uintptr_t)ModuleTerm;
|
---|
1146 | if (fIsVMMR0)
|
---|
1147 | {
|
---|
1148 | pIn->eEPType = pIn->EP_VMMR0;
|
---|
1149 | pIn->EP.VMMR0.pvVMMR0 = OpenOut.pvImageBase;
|
---|
1150 | pIn->EP.VMMR0.pvVMMR0Entry = (void *)(uintptr_t)VMMR0Entry;
|
---|
1151 | }
|
---|
1152 | else
|
---|
1153 | pIn->eEPType = pIn->EP_NOTHING;
|
---|
1154 | pIn->offStrTab = offStrTab;
|
---|
1155 | pIn->cbStrTab = CalcArgs.cbStrings;
|
---|
1156 | pIn->offSymbols = offSymTab;
|
---|
1157 | pIn->cSymbols = CalcArgs.cSymbols;
|
---|
1158 | pIn->cbImage = cbImage;
|
---|
1159 | pIn->pvImageBase = OpenOut.pvImageBase;
|
---|
1160 | if (!g_u32FakeMode)
|
---|
1161 | rc = suplibOsIOCtl(SUP_IOCTL_LDR_LOAD, pIn, cbIn, NULL, 0);
|
---|
1162 | else
|
---|
1163 | rc = VINF_SUCCESS;
|
---|
1164 | if ( VBOX_SUCCESS(rc)
|
---|
1165 | || rc == VERR_ALREADY_LOADED /* this is because of a competing process. */
|
---|
1166 | )
|
---|
1167 | {
|
---|
1168 | if (fIsVMMR0)
|
---|
1169 | g_pvVMMR0 = OpenOut.pvImageBase;
|
---|
1170 | RTMemTmpFree(pIn);
|
---|
1171 | RTLdrClose(hLdrMod);
|
---|
1172 | return VINF_SUCCESS;
|
---|
1173 | }
|
---|
1174 | }
|
---|
1175 | RTMemTmpFree(pIn);
|
---|
1176 | }
|
---|
1177 | else
|
---|
1178 | {
|
---|
1179 | AssertMsgFailed(("failed to allocated %d bytes for SUPLDRLOAD_IN structure!\n", cbIn));
|
---|
1180 | rc = VERR_NO_TMP_MEMORY;
|
---|
1181 | }
|
---|
1182 | }
|
---|
1183 | }
|
---|
1184 | RTLdrClose(hLdrMod);
|
---|
1185 | return rc;
|
---|
1186 | }
|
---|
1187 |
|
---|
1188 |
|
---|
1189 | SUPR3DECL(int) SUPFreeModule(void *pvImageBase)
|
---|
1190 | {
|
---|
1191 | /*
|
---|
1192 | * There is one special module. When this is freed we'll
|
---|
1193 | * free the IDT entry that goes with it.
|
---|
1194 | *
|
---|
1195 | * Note that we don't keep count of VMMR0.r0 loads here, so the
|
---|
1196 | * first unload will free it.
|
---|
1197 | */
|
---|
1198 | if (pvImageBase == g_pvVMMR0)
|
---|
1199 | {
|
---|
1200 | /*
|
---|
1201 | * This is the point where we remove the IDT hook. We do
|
---|
1202 | * that before unloading the R0 VMM part.
|
---|
1203 | */
|
---|
1204 | if (g_u32FakeMode)
|
---|
1205 | {
|
---|
1206 | #ifndef VBOX_WITHOUT_IDT_PATCHING
|
---|
1207 | g_u8Interrupt = 3;
|
---|
1208 | RTMemExecFree(*(void **)&g_pfnCallVMMR0);
|
---|
1209 | g_pfnCallVMMR0 = NULL;
|
---|
1210 | #endif
|
---|
1211 | g_pvVMMR0 = NULL;
|
---|
1212 | return VINF_SUCCESS;
|
---|
1213 | }
|
---|
1214 |
|
---|
1215 | #ifndef VBOX_WITHOUT_IDT_PATCHING
|
---|
1216 | /*
|
---|
1217 | * Uninstall IDT entry.
|
---|
1218 | */
|
---|
1219 | int rc = 0;
|
---|
1220 | if (g_u8Interrupt != 3)
|
---|
1221 | {
|
---|
1222 | SUPIDTREMOVE_IN In;
|
---|
1223 | In.u32Cookie = g_u32Cookie;
|
---|
1224 | In.u32SessionCookie = g_u32SessionCookie;
|
---|
1225 | rc = suplibOsIOCtl(SUP_IOCTL_IDT_REMOVE, &In, sizeof(In), NULL, 0);
|
---|
1226 | g_u8Interrupt = 3;
|
---|
1227 | RTMemExecFree(*(void **)&g_pfnCallVMMR0);
|
---|
1228 | g_pfnCallVMMR0 = NULL;
|
---|
1229 | }
|
---|
1230 | #endif
|
---|
1231 | }
|
---|
1232 |
|
---|
1233 | /*
|
---|
1234 | * Free the requested module.
|
---|
1235 | */
|
---|
1236 | SUPLDRFREE_IN In;
|
---|
1237 | In.u32Cookie = g_u32Cookie;
|
---|
1238 | In.u32SessionCookie = g_u32SessionCookie;
|
---|
1239 | In.pvImageBase = pvImageBase;
|
---|
1240 | int rc = VINF_SUCCESS;
|
---|
1241 | if (!g_u32FakeMode)
|
---|
1242 | rc = suplibOsIOCtl(SUP_IOCTL_LDR_FREE, &In, sizeof(In), NULL, 0);
|
---|
1243 | if ( VBOX_SUCCESS(rc)
|
---|
1244 | && pvImageBase == g_pvVMMR0)
|
---|
1245 | g_pvVMMR0 = NULL;
|
---|
1246 | return rc;
|
---|
1247 | }
|
---|
1248 |
|
---|
1249 |
|
---|
1250 | SUPR3DECL(int) SUPGetSymbolR0(void *pvImageBase, const char *pszSymbol, void **ppvValue)
|
---|
1251 | {
|
---|
1252 | *ppvValue = NULL;
|
---|
1253 |
|
---|
1254 | /*
|
---|
1255 | * Do ioctl.
|
---|
1256 | */
|
---|
1257 | size_t cchSymbol = strlen(pszSymbol);
|
---|
1258 | const size_t cbIn = RT_OFFSETOF(SUPLDRGETSYMBOL_IN, szSymbol[cchSymbol + 1]);
|
---|
1259 | SUPLDRGETSYMBOL_OUT Out = { NULL };
|
---|
1260 | PSUPLDRGETSYMBOL_IN pIn = (PSUPLDRGETSYMBOL_IN)alloca(cbIn);
|
---|
1261 | pIn->u32Cookie = g_u32Cookie;
|
---|
1262 | pIn->u32SessionCookie = g_u32SessionCookie;
|
---|
1263 | pIn->pvImageBase = pvImageBase;
|
---|
1264 | memcpy(pIn->szSymbol, pszSymbol, cchSymbol + 1);
|
---|
1265 | int rc = suplibOsIOCtl(SUP_IOCTL_LDR_GET_SYMBOL, pIn, cbIn, &Out, sizeof(Out));
|
---|
1266 | if (VBOX_SUCCESS(rc))
|
---|
1267 | *ppvValue = Out.pvSymbol;
|
---|
1268 | return rc;
|
---|
1269 | }
|
---|
1270 |
|
---|
1271 |
|
---|
1272 | SUPR3DECL(int) SUPLoadVMM(const char *pszFilename)
|
---|
1273 | {
|
---|
1274 | void *pvImageBase;
|
---|
1275 | return SUPLoadModule(pszFilename, "VMMR0.r0", &pvImageBase);
|
---|
1276 | }
|
---|
1277 |
|
---|
1278 |
|
---|
1279 | SUPR3DECL(int) SUPUnloadVMM(void)
|
---|
1280 | {
|
---|
1281 | return SUPFreeModule(g_pvVMMR0);
|
---|
1282 | }
|
---|
1283 |
|
---|
1284 |
|
---|
1285 | SUPR3DECL(int) SUPGipGetPhys(PRTHCPHYS pHCPhys)
|
---|
1286 | {
|
---|
1287 | if (g_pSUPGlobalInfoPage)
|
---|
1288 | {
|
---|
1289 | *pHCPhys = g_HCPhysSUPGlobalInfoPage;
|
---|
1290 | return VINF_SUCCESS;
|
---|
1291 | }
|
---|
1292 | *pHCPhys = NIL_RTHCPHYS;
|
---|
1293 | return VERR_WRONG_ORDER;
|
---|
1294 | }
|
---|
1295 |
|
---|