1 | /* $Id: PDMLdr.cpp 14595 2008-11-25 20:21:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PDM - Pluggable Device Manager, module loader.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | //#define PDMLDR_FAKE_MODE
|
---|
23 |
|
---|
24 | /*******************************************************************************
|
---|
25 | * Header Files *
|
---|
26 | *******************************************************************************/
|
---|
27 | #define LOG_GROUP LOG_GROUP_PDM_LDR
|
---|
28 | #include "PDMInternal.h"
|
---|
29 | #include <VBox/pdm.h>
|
---|
30 | #include <VBox/mm.h>
|
---|
31 | #include <VBox/vmm.h>
|
---|
32 | #include <VBox/vm.h>
|
---|
33 | #include <VBox/uvm.h>
|
---|
34 | #include <VBox/sup.h>
|
---|
35 | #include <VBox/param.h>
|
---|
36 | #include <VBox/err.h>
|
---|
37 | #include <VBox/hwaccm.h>
|
---|
38 |
|
---|
39 | #include <VBox/log.h>
|
---|
40 | #include <iprt/assert.h>
|
---|
41 | #include <iprt/alloc.h>
|
---|
42 | #include <iprt/ldr.h>
|
---|
43 | #include <iprt/path.h>
|
---|
44 | #include <iprt/string.h>
|
---|
45 |
|
---|
46 | #include <limits.h>
|
---|
47 |
|
---|
48 |
|
---|
49 | /*******************************************************************************
|
---|
50 | * Structures and Typedefs *
|
---|
51 | *******************************************************************************/
|
---|
52 | /**
|
---|
53 | * Structure which the user argument of the RTLdrGetBits() callback points to.
|
---|
54 | * @internal
|
---|
55 | */
|
---|
56 | typedef struct PDMGETIMPORTARGS
|
---|
57 | {
|
---|
58 | PVM pVM;
|
---|
59 | PPDMMOD pModule;
|
---|
60 | } PDMGETIMPORTARGS, *PPDMGETIMPORTARGS;
|
---|
61 |
|
---|
62 |
|
---|
63 | /*******************************************************************************
|
---|
64 | * Internal Functions *
|
---|
65 | *******************************************************************************/
|
---|
66 | static DECLCALLBACK(int) pdmR3GetImportRC(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser);
|
---|
67 | static int pdmR3LoadR0U(PUVM pUVM, const char *pszFilename, const char *pszName);
|
---|
68 | static char * pdmR3FileRC(const char *pszFile);
|
---|
69 | static char * pdmR3FileR0(const char *pszFile);
|
---|
70 | static char * pdmR3File(const char *pszFile, const char *pszDefaultExt, bool fShared);
|
---|
71 | static DECLCALLBACK(int) pdmR3QueryModFromEIPEnumSymbols(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser);
|
---|
72 |
|
---|
73 |
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Loads the VMMR0.r0 module early in the init process.
|
---|
77 | *
|
---|
78 | * @returns VBox status code.
|
---|
79 | * @param pUVM Pointer to the user mode VM structure.
|
---|
80 | */
|
---|
81 | VMMR3DECL(int) PDMR3LdrLoadVMMR0U(PUVM pUVM)
|
---|
82 | {
|
---|
83 | return pdmR3LoadR0U(pUVM, NULL, VMMR0_MAIN_MODULE_NAME);
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Init the module loader part of PDM.
|
---|
89 | *
|
---|
90 | * This routine will load the Host Context Ring-0 and Guest
|
---|
91 | * Context VMM modules.
|
---|
92 | *
|
---|
93 | * @returns VBox stutus code.
|
---|
94 | * @param pUVM Pointer to the user mode VM structure.
|
---|
95 | * @param pvVMMR0Mod The opqaue returned by PDMR3LdrLoadVMMR0.
|
---|
96 | */
|
---|
97 | int pdmR3LdrInitU(PUVM pUVM)
|
---|
98 | {
|
---|
99 | #ifdef PDMLDR_FAKE_MODE
|
---|
100 | return VINF_SUCCESS;
|
---|
101 |
|
---|
102 | #else
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * Load the mandatory GC module, the VMMR0.r0 is loaded before VM creation.
|
---|
106 | */
|
---|
107 | return PDMR3LdrLoadRC(pUVM->pVM, NULL, VMMGC_MAIN_MODULE_NAME);
|
---|
108 | #endif
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Terminate the module loader part of PDM.
|
---|
114 | *
|
---|
115 | * This will unload and free all modules.
|
---|
116 | *
|
---|
117 | * @param pVM The VM handle.
|
---|
118 | *
|
---|
119 | * @remarks This is normally called twice during termination.
|
---|
120 | */
|
---|
121 | void pdmR3LdrTermU(PUVM pUVM)
|
---|
122 | {
|
---|
123 | /*
|
---|
124 | * Free the modules.
|
---|
125 | */
|
---|
126 | PPDMMOD pModule = pUVM->pdm.s.pModules;
|
---|
127 | pUVM->pdm.s.pModules = NULL;
|
---|
128 | while (pModule)
|
---|
129 | {
|
---|
130 | /* free loader item. */
|
---|
131 | if (pModule->hLdrMod != NIL_RTLDRMOD)
|
---|
132 | {
|
---|
133 | int rc2 = RTLdrClose(pModule->hLdrMod);
|
---|
134 | AssertRC(rc2);
|
---|
135 | pModule->hLdrMod = NIL_RTLDRMOD;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /* free bits. */
|
---|
139 | switch (pModule->eType)
|
---|
140 | {
|
---|
141 | case PDMMOD_TYPE_R0:
|
---|
142 | {
|
---|
143 | Assert(pModule->ImageBase);
|
---|
144 | int rc2 = SUPFreeModule((void *)(uintptr_t)pModule->ImageBase);
|
---|
145 | AssertRC(rc2);
|
---|
146 | pModule->ImageBase = 0;
|
---|
147 | break;
|
---|
148 | }
|
---|
149 |
|
---|
150 | case PDMMOD_TYPE_RC:
|
---|
151 | case PDMMOD_TYPE_R3:
|
---|
152 | /* MM will free this memory for us - it's alloc only memory. :-) */
|
---|
153 | break;
|
---|
154 |
|
---|
155 | default:
|
---|
156 | AssertMsgFailed(("eType=%d\n", pModule->eType));
|
---|
157 | break;
|
---|
158 | }
|
---|
159 | pModule->pvBits = NULL;
|
---|
160 |
|
---|
161 | void *pvFree = pModule;
|
---|
162 | pModule = pModule->pNext;
|
---|
163 | RTMemFree(pvFree);
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Applies relocations to GC modules.
|
---|
170 | *
|
---|
171 | * This must be done very early in the relocation
|
---|
172 | * process so that components can resolve GC symbols during relocation.
|
---|
173 | *
|
---|
174 | * @param pUVM Pointer to the user mode VM structure.
|
---|
175 | * @param offDelta Relocation delta relative to old location.
|
---|
176 | */
|
---|
177 | VMMR3DECL(void) PDMR3LdrRelocateU(PUVM pUVM, RTGCINTPTR offDelta)
|
---|
178 | {
|
---|
179 | LogFlow(("PDMR3LdrRelocate: offDelta=%RGv\n", offDelta));
|
---|
180 |
|
---|
181 | /*
|
---|
182 | * GC Modules.
|
---|
183 | */
|
---|
184 | if (pUVM->pdm.s.pModules)
|
---|
185 | {
|
---|
186 | /*
|
---|
187 | * The relocation have to be done in two passes so imports
|
---|
188 | * can be correctely resolved. The first pass will update
|
---|
189 | * the ImageBase saving the current value in OldImageBase.
|
---|
190 | * The second pass will do the actual relocation.
|
---|
191 | */
|
---|
192 | /* pass 1 */
|
---|
193 | PPDMMOD pCur;
|
---|
194 | for (pCur = pUVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
|
---|
195 | {
|
---|
196 | if (pCur->eType == PDMMOD_TYPE_RC)
|
---|
197 | {
|
---|
198 | pCur->OldImageBase = pCur->ImageBase;
|
---|
199 | pCur->ImageBase = MMHyperR3ToRC(pUVM->pVM, pCur->pvBits);
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | /* pass 2 */
|
---|
204 | for (pCur = pUVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
|
---|
205 | {
|
---|
206 | if (pCur->eType == PDMMOD_TYPE_RC)
|
---|
207 | {
|
---|
208 | PDMGETIMPORTARGS Args;
|
---|
209 | Args.pVM = pUVM->pVM;
|
---|
210 | Args.pModule = pCur;
|
---|
211 | int rc = RTLdrRelocate(pCur->hLdrMod, pCur->pvBits, pCur->ImageBase, pCur->OldImageBase,
|
---|
212 | pdmR3GetImportRC, &Args);
|
---|
213 | AssertFatalMsgRC(rc, ("RTLdrRelocate failed, rc=%d\n", rc));
|
---|
214 | DBGFR3ModuleRelocate(pUVM->pVM, pCur->OldImageBase, pCur->ImageBase, RTLdrSize(pCur->hLdrMod),
|
---|
215 | pCur->szFilename, pCur->szName);
|
---|
216 | }
|
---|
217 | }
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * Loads a module into the host context ring-3.
|
---|
224 | *
|
---|
225 | * This is used by the driver and device init functions to load modules
|
---|
226 | * containing the drivers and devices. The function can be extended to
|
---|
227 | * load modules which are not native to the environment we're running in,
|
---|
228 | * but at the moment this is not required.
|
---|
229 | *
|
---|
230 | * No reference counting is kept, since we don't implement any facilities
|
---|
231 | * for unloading the module. But the module will naturally be released
|
---|
232 | * when the VM terminates.
|
---|
233 | *
|
---|
234 | * @returns VBox status code.
|
---|
235 | * @param pUVM Pointer to the user mode VM structure.
|
---|
236 | * @param pszFilename Filename of the module binary.
|
---|
237 | * @param pszName Module name. Case sensitive and the length is limited!
|
---|
238 | */
|
---|
239 | int pdmR3LoadR3U(PUVM pUVM, const char *pszFilename, const char *pszName)
|
---|
240 | {
|
---|
241 | /*
|
---|
242 | * Validate input.
|
---|
243 | */
|
---|
244 | AssertMsg(pUVM->pVM->pdm.s.offVM, ("bad init order!\n"));
|
---|
245 | Assert(pszFilename);
|
---|
246 | size_t cchFilename = strlen(pszFilename);
|
---|
247 | Assert(pszName);
|
---|
248 | size_t cchName = strlen(pszName);
|
---|
249 | PPDMMOD pCur;
|
---|
250 | if (cchName >= sizeof(pCur->szName))
|
---|
251 | {
|
---|
252 | AssertMsgFailed(("Name is too long, cchName=%d pszName='%s'\n", cchName, pszName));
|
---|
253 | return VERR_INVALID_PARAMETER;
|
---|
254 | }
|
---|
255 |
|
---|
256 | /*
|
---|
257 | * Try lookup the name and see if the module exists.
|
---|
258 | */
|
---|
259 | for (pCur = pUVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
|
---|
260 | {
|
---|
261 | if (!strcmp(pCur->szName, pszName))
|
---|
262 | {
|
---|
263 | if (pCur->eType == PDMMOD_TYPE_R3)
|
---|
264 | return VINF_PDM_ALREADY_LOADED;
|
---|
265 | AssertMsgFailed(("We've already got a module '%s' loaded!\n", pszName));
|
---|
266 | return VERR_PDM_MODULE_NAME_CLASH;
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | /*
|
---|
271 | * Allocate the module list node and initialize it.
|
---|
272 | */
|
---|
273 | const char *pszSuff = RTLdrGetSuff();
|
---|
274 | size_t cchSuff = RTPathHaveExt(pszFilename) ? 0 : strlen(pszSuff);
|
---|
275 | PPDMMOD pModule = (PPDMMOD)RTMemAllocZ(RT_OFFSETOF(PDMMOD, szFilename[cchFilename + cchSuff + 1]));
|
---|
276 | if (!pModule)
|
---|
277 | return VERR_NO_MEMORY;
|
---|
278 |
|
---|
279 | pModule->eType = PDMMOD_TYPE_R3;
|
---|
280 | memcpy(pModule->szName, pszName, cchName); /* memory is zero'ed, no need to copy terminator :-) */
|
---|
281 | memcpy(pModule->szFilename, pszFilename, cchFilename);
|
---|
282 | memcpy(&pModule->szFilename[cchFilename], pszSuff, cchSuff);
|
---|
283 |
|
---|
284 | /*
|
---|
285 | * Load the loader item.
|
---|
286 | */
|
---|
287 | int rc = SUPR3HardenedVerifyFile(pModule->szFilename, "pdmR3LoadR3U", NULL);
|
---|
288 | if (RT_SUCCESS(rc))
|
---|
289 | rc = RTLdrLoad(pModule->szFilename, &pModule->hLdrMod);
|
---|
290 | if (RT_SUCCESS(rc))
|
---|
291 | {
|
---|
292 | pModule->pNext = pUVM->pdm.s.pModules;
|
---|
293 | pUVM->pdm.s.pModules = pModule;
|
---|
294 | return rc;
|
---|
295 | }
|
---|
296 |
|
---|
297 | /* Something went wrong, most likely module not found. Don't consider other unlikely errors */
|
---|
298 | rc = VMSetError(pUVM->pVM, rc, RT_SRC_POS, N_("Unable to load R3 module %s (%s)"), pModule->szFilename, pszName);
|
---|
299 | RTMemFree(pModule);
|
---|
300 | return rc;
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * Resolve an external symbol during RTLdrGetBits() of a RC module.
|
---|
306 | *
|
---|
307 | * @returns VBox status code.
|
---|
308 | * @param hLdrMod The loader module handle.
|
---|
309 | * @param pszModule Module name.
|
---|
310 | * @param pszSymbol Symbol name, NULL if uSymbol should be used.
|
---|
311 | * @param uSymbol Symbol ordinal, ~0 if pszSymbol should be used.
|
---|
312 | * @param pValue Where to store the symbol value (address).
|
---|
313 | * @param pvUser User argument.
|
---|
314 | */
|
---|
315 | static DECLCALLBACK(int) pdmR3GetImportRC(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser)
|
---|
316 | {
|
---|
317 | PVM pVM = ((PPDMGETIMPORTARGS)pvUser)->pVM;
|
---|
318 | PPDMMOD pModule = ((PPDMGETIMPORTARGS)pvUser)->pModule;
|
---|
319 |
|
---|
320 | /*
|
---|
321 | * Adjust input.
|
---|
322 | */
|
---|
323 | if (pszModule && !*pszModule)
|
---|
324 | pszModule = NULL;
|
---|
325 |
|
---|
326 | /*
|
---|
327 | * Builtin module.
|
---|
328 | */
|
---|
329 | if (!pszModule || !strcmp(pszModule, "VMMGCBuiltin.gc"))
|
---|
330 | {
|
---|
331 | int rc = VINF_SUCCESS;
|
---|
332 | if (!strcmp(pszSymbol, "g_VM"))
|
---|
333 | *pValue = pVM->pVMRC;
|
---|
334 | else if (!strcmp(pszSymbol, "g_CPUM"))
|
---|
335 | *pValue = VM_RC_ADDR(pVM, &pVM->cpum);
|
---|
336 | else if (!strcmp(pszSymbol, "g_TRPM"))
|
---|
337 | *pValue = VM_RC_ADDR(pVM, &pVM->trpm);
|
---|
338 | else if ( !strncmp(pszSymbol, "VMM", 3)
|
---|
339 | || !strcmp(pszSymbol, "g_Logger")
|
---|
340 | || !strcmp(pszSymbol, "g_RelLogger"))
|
---|
341 | {
|
---|
342 | RTRCPTR RCPtr = 0;
|
---|
343 | rc = VMMR3GetImportRC(pVM, pszSymbol, &RCPtr);
|
---|
344 | if (RT_SUCCESS(rc))
|
---|
345 | *pValue = RCPtr;
|
---|
346 | }
|
---|
347 | else if ( !strncmp(pszSymbol, "TM", 2)
|
---|
348 | || !strcmp(pszSymbol, "g_pSUPGlobalInfoPage"))
|
---|
349 | {
|
---|
350 | RTRCPTR RCPtr = 0;
|
---|
351 | rc = TMR3GetImportRC(pVM, pszSymbol, &RCPtr);
|
---|
352 | if (RT_SUCCESS(rc))
|
---|
353 | *pValue = RCPtr;
|
---|
354 | }
|
---|
355 | else
|
---|
356 | {
|
---|
357 | AssertMsg(!pszModule, ("Unknown builtin symbol '%s' for module '%s'!\n", pszSymbol, pModule->szName)); NOREF(pModule);
|
---|
358 | rc = VERR_SYMBOL_NOT_FOUND;
|
---|
359 | }
|
---|
360 | if (RT_SUCCESS(rc) || pszModule)
|
---|
361 | return rc;
|
---|
362 | }
|
---|
363 |
|
---|
364 | /*
|
---|
365 | * Search for module.
|
---|
366 | */
|
---|
367 | PPDMMOD pCur = pVM->pUVM->pdm.s.pModules;
|
---|
368 | while (pCur)
|
---|
369 | {
|
---|
370 | if ( pCur->eType == PDMMOD_TYPE_RC
|
---|
371 | && ( !pszModule
|
---|
372 | || !strcmp(pCur->szName, pszModule))
|
---|
373 | )
|
---|
374 | {
|
---|
375 | /* Search for the symbol. */
|
---|
376 | int rc = RTLdrGetSymbolEx(pCur->hLdrMod, pCur->pvBits, pCur->ImageBase, pszSymbol, pValue);
|
---|
377 | if (RT_SUCCESS(rc))
|
---|
378 | {
|
---|
379 | AssertMsg(*pValue - pCur->ImageBase < RTLdrSize(pCur->hLdrMod),
|
---|
380 | ("%RRv-%RRv %s %RRv\n", (RTRCPTR)pCur->ImageBase,
|
---|
381 | (RTRCPTR)(pCur->ImageBase + RTLdrSize(pCur->hLdrMod) - 1),
|
---|
382 | pszSymbol, (RTRCPTR)*pValue));
|
---|
383 | return rc;
|
---|
384 | }
|
---|
385 | if (pszModule)
|
---|
386 | {
|
---|
387 | AssertMsgFailed(("Couldn't find symbol '%s' in module '%s'!\n", pszSymbol, pszModule));
|
---|
388 | LogRel(("PDMLdr: Couldn't find symbol '%s' in module '%s'!\n", pszSymbol, pszModule));
|
---|
389 | return VERR_SYMBOL_NOT_FOUND;
|
---|
390 | }
|
---|
391 | }
|
---|
392 |
|
---|
393 | /* next */
|
---|
394 | pCur = pCur->pNext;
|
---|
395 | }
|
---|
396 |
|
---|
397 | AssertMsgFailed(("Couldn't find module '%s' for resolving symbol '%s'!\n", pszModule, pszSymbol));
|
---|
398 | return VERR_SYMBOL_NOT_FOUND;
|
---|
399 | }
|
---|
400 |
|
---|
401 |
|
---|
402 | /**
|
---|
403 | * Loads a module into the guest context (i.e. into the Hypervisor memory region).
|
---|
404 | *
|
---|
405 | * @returns VBox status code.
|
---|
406 | * @param pVM The VM to load it into.
|
---|
407 | * @param pszFilename Filename of the module binary.
|
---|
408 | * @param pszName Module name. Case sensitive and the length is limited!
|
---|
409 | */
|
---|
410 | VMMR3DECL(int) PDMR3LdrLoadRC(PVM pVM, const char *pszFilename, const char *pszName)
|
---|
411 | {
|
---|
412 | /*
|
---|
413 | * Validate input.
|
---|
414 | */
|
---|
415 | AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
|
---|
416 | PPDMMOD pCur = pVM->pUVM->pdm.s.pModules;
|
---|
417 | while (pCur)
|
---|
418 | {
|
---|
419 | if (!strcmp(pCur->szName, pszName))
|
---|
420 | {
|
---|
421 | AssertMsgFailed(("We've already got a module '%s' loaded!\n", pszName));
|
---|
422 | return VERR_PDM_MODULE_NAME_CLASH;
|
---|
423 | }
|
---|
424 | /* next */
|
---|
425 | pCur = pCur->pNext;
|
---|
426 | }
|
---|
427 |
|
---|
428 | /*
|
---|
429 | * Find the file if not specified.
|
---|
430 | */
|
---|
431 | char *pszFile = NULL;
|
---|
432 | if (!pszFilename)
|
---|
433 | pszFilename = pszFile = pdmR3FileRC(pszName);
|
---|
434 |
|
---|
435 | /*
|
---|
436 | * Allocate the module list node.
|
---|
437 | */
|
---|
438 | PPDMMOD pModule = (PPDMMOD)RTMemAllocZ(sizeof(*pModule) + strlen(pszFilename));
|
---|
439 | if (!pModule)
|
---|
440 | {
|
---|
441 | RTMemTmpFree(pszFile);
|
---|
442 | return VERR_NO_MEMORY;
|
---|
443 | }
|
---|
444 | AssertMsg(strlen(pszName) + 1 < sizeof(pModule->szName),
|
---|
445 | ("pazName is too long (%d chars) max is %d chars.\n", strlen(pszName), sizeof(pModule->szName) - 1));
|
---|
446 | strcpy(pModule->szName, pszName);
|
---|
447 | pModule->eType = PDMMOD_TYPE_RC;
|
---|
448 | strcpy(pModule->szFilename, pszFilename);
|
---|
449 |
|
---|
450 |
|
---|
451 | /*
|
---|
452 | * Open the loader item.
|
---|
453 | */
|
---|
454 | int rc = SUPR3HardenedVerifyFile(pszFilename, "PDMR3LdrLoadRC", NULL);
|
---|
455 | if (RT_SUCCESS(rc))
|
---|
456 | rc = RTLdrOpen(pszFilename, &pModule->hLdrMod);
|
---|
457 | if (RT_SUCCESS(rc))
|
---|
458 | {
|
---|
459 | /*
|
---|
460 | * Allocate space in the hypervisor.
|
---|
461 | */
|
---|
462 | size_t cb = RTLdrSize(pModule->hLdrMod);
|
---|
463 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
464 | uint32_t cPages = cb >> PAGE_SHIFT;
|
---|
465 | PSUPPAGE paPages = (PSUPPAGE)RTMemTmpAlloc(cPages * sizeof(paPages[0]));
|
---|
466 | if (paPages)
|
---|
467 | {
|
---|
468 | rc = SUPR3PageAllocEx(cPages, 0 /*fFlags*/, &pModule->pvBits, NULL /*pR0Ptr*/, paPages);
|
---|
469 | if (RT_SUCCESS(rc))
|
---|
470 | {
|
---|
471 | RTGCPTR GCPtr;
|
---|
472 | rc = MMR3HyperMapPages(pVM, pModule->pvBits, NIL_RTR0PTR,
|
---|
473 | cPages, paPages, pModule->szName, &GCPtr);
|
---|
474 | if (RT_SUCCESS(rc))
|
---|
475 | {
|
---|
476 | MMR3HyperReserve(pVM, PAGE_SIZE, "fence", NULL);
|
---|
477 |
|
---|
478 | /*
|
---|
479 | * Get relocated image bits.
|
---|
480 | */
|
---|
481 | Assert(MMHyperR3ToRC(pVM, pModule->pvBits) == GCPtr);
|
---|
482 | pModule->ImageBase = GCPtr;
|
---|
483 | PDMGETIMPORTARGS Args;
|
---|
484 | Args.pVM = pVM;
|
---|
485 | Args.pModule = pModule;
|
---|
486 | rc = RTLdrGetBits(pModule->hLdrMod, pModule->pvBits, pModule->ImageBase, pdmR3GetImportRC, &Args);
|
---|
487 | if (RT_SUCCESS(rc))
|
---|
488 | {
|
---|
489 | /*
|
---|
490 | * Insert the module.
|
---|
491 | */
|
---|
492 | PUVM pUVM = pVM->pUVM;
|
---|
493 | if (pUVM->pdm.s.pModules)
|
---|
494 | {
|
---|
495 | /* we don't expect this list to be very long, so rather save the tail pointer. */
|
---|
496 | PPDMMOD pCur = pUVM->pdm.s.pModules;
|
---|
497 | while (pCur->pNext)
|
---|
498 | pCur = pCur->pNext;
|
---|
499 | pCur->pNext = pModule;
|
---|
500 | }
|
---|
501 | else
|
---|
502 | pUVM->pdm.s.pModules = pModule; /* (pNext is zeroed by alloc) */
|
---|
503 | Log(("PDM: RC Module at %RRv %s (%s)\n", (RTRCPTR)pModule->ImageBase, pszName, pszFilename));
|
---|
504 | RTMemTmpFree(pszFile);
|
---|
505 | RTMemTmpFree(paPages);
|
---|
506 | return VINF_SUCCESS;
|
---|
507 | }
|
---|
508 | }
|
---|
509 | else
|
---|
510 | {
|
---|
511 | AssertRC(rc);
|
---|
512 | SUPR3PageFreeEx(pModule->pvBits, cPages);
|
---|
513 | }
|
---|
514 | }
|
---|
515 | else
|
---|
516 | AssertMsgFailed(("SUPPageAlloc(%d,) -> %Rrc\n", cPages, rc));
|
---|
517 | RTMemTmpFree(paPages);
|
---|
518 | }
|
---|
519 | else
|
---|
520 | rc = VERR_NO_TMP_MEMORY;
|
---|
521 | int rc2 = RTLdrClose(pModule->hLdrMod);
|
---|
522 | AssertRC(rc2);
|
---|
523 | }
|
---|
524 | RTMemFree(pModule);
|
---|
525 | RTMemTmpFree(pszFile);
|
---|
526 |
|
---|
527 | /* Don't consider VERR_PDM_MODULE_NAME_CLASH and VERR_NO_MEMORY above as these are very unlikely. */
|
---|
528 | if (RT_FAILURE(rc))
|
---|
529 | return VMSetError(pVM, rc, RT_SRC_POS, N_("Cannot load GC module %s"), pszFilename);
|
---|
530 | return rc;
|
---|
531 | }
|
---|
532 |
|
---|
533 |
|
---|
534 | /**
|
---|
535 | * Loads a module into the ring-0 context.
|
---|
536 | *
|
---|
537 | * @returns VBox status code.
|
---|
538 | * @param pUVM Pointer to the user mode VM structure.
|
---|
539 | * @param pszFilename Filename of the module binary.
|
---|
540 | * @param pszName Module name. Case sensitive and the length is limited!
|
---|
541 | */
|
---|
542 | static int pdmR3LoadR0U(PUVM pUVM, const char *pszFilename, const char *pszName)
|
---|
543 | {
|
---|
544 | /*
|
---|
545 | * Validate input.
|
---|
546 | */
|
---|
547 | PPDMMOD pCur = pUVM->pdm.s.pModules;
|
---|
548 | while (pCur)
|
---|
549 | {
|
---|
550 | if (!strcmp(pCur->szName, pszName))
|
---|
551 | {
|
---|
552 | AssertMsgFailed(("We've already got a module '%s' loaded!\n", pszName));
|
---|
553 | return VERR_PDM_MODULE_NAME_CLASH;
|
---|
554 | }
|
---|
555 | /* next */
|
---|
556 | pCur = pCur->pNext;
|
---|
557 | }
|
---|
558 |
|
---|
559 | /*
|
---|
560 | * Find the file if not specified.
|
---|
561 | */
|
---|
562 | char *pszFile = NULL;
|
---|
563 | if (!pszFilename)
|
---|
564 | pszFilename = pszFile = pdmR3FileR0(pszName);
|
---|
565 |
|
---|
566 | /*
|
---|
567 | * Allocate the module list node.
|
---|
568 | */
|
---|
569 | PPDMMOD pModule = (PPDMMOD)RTMemAllocZ(sizeof(*pModule) + strlen(pszFilename));
|
---|
570 | if (!pModule)
|
---|
571 | {
|
---|
572 | RTMemTmpFree(pszFile);
|
---|
573 | return VERR_NO_MEMORY;
|
---|
574 | }
|
---|
575 | AssertMsg(strlen(pszName) + 1 < sizeof(pModule->szName),
|
---|
576 | ("pazName is too long (%d chars) max is %d chars.\n", strlen(pszName), sizeof(pModule->szName) - 1));
|
---|
577 | strcpy(pModule->szName, pszName);
|
---|
578 | pModule->eType = PDMMOD_TYPE_R0;
|
---|
579 | strcpy(pModule->szFilename, pszFilename);
|
---|
580 |
|
---|
581 | /*
|
---|
582 | * Ask the support library to load it.
|
---|
583 | */
|
---|
584 | void *pvImageBase;
|
---|
585 | int rc = SUPLoadModule(pszFilename, pszName, &pvImageBase);
|
---|
586 | if (RT_SUCCESS(rc))
|
---|
587 | {
|
---|
588 | pModule->hLdrMod = NIL_RTLDRMOD;
|
---|
589 | pModule->ImageBase = (uintptr_t)pvImageBase;
|
---|
590 |
|
---|
591 | /*
|
---|
592 | * Insert the module.
|
---|
593 | */
|
---|
594 | if (pUVM->pdm.s.pModules)
|
---|
595 | {
|
---|
596 | /* we don't expect this list to be very long, so rather save the tail pointer. */
|
---|
597 | PPDMMOD pCur = pUVM->pdm.s.pModules;
|
---|
598 | while (pCur->pNext)
|
---|
599 | pCur = pCur->pNext;
|
---|
600 | pCur->pNext = pModule;
|
---|
601 | }
|
---|
602 | else
|
---|
603 | pUVM->pdm.s.pModules = pModule; /* (pNext is zeroed by alloc) */
|
---|
604 | Log(("PDM: R0 Module at %RHv %s (%s)\n", (RTR0PTR)pModule->ImageBase, pszName, pszFilename));
|
---|
605 | RTMemTmpFree(pszFile);
|
---|
606 | return VINF_SUCCESS;
|
---|
607 | }
|
---|
608 |
|
---|
609 | RTMemFree(pModule);
|
---|
610 | RTMemTmpFree(pszFile);
|
---|
611 | LogRel(("pdmR3LoadR0U: pszName=\"%s\" rc=%Rrc\n", pszName, rc));
|
---|
612 |
|
---|
613 | /* Don't consider VERR_PDM_MODULE_NAME_CLASH and VERR_NO_MEMORY above as these are very unlikely. */
|
---|
614 | if (RT_FAILURE(rc) && pUVM->pVM) /** @todo VMR3SetErrorU. */
|
---|
615 | return VMSetError(pUVM->pVM, rc, RT_SRC_POS, N_("Cannot load R0 module %s"), pszFilename);
|
---|
616 | return rc;
|
---|
617 | }
|
---|
618 |
|
---|
619 |
|
---|
620 |
|
---|
621 | /**
|
---|
622 | * Get the address of a symbol in a given HC ring 3 module.
|
---|
623 | *
|
---|
624 | * @returns VBox status code.
|
---|
625 | * @param pVM VM handle.
|
---|
626 | * @param pszModule Module name.
|
---|
627 | * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
|
---|
628 | * ordinal value rather than a string pointer.
|
---|
629 | * @param ppvValue Where to store the symbol value.
|
---|
630 | */
|
---|
631 | VMMR3DECL(int) PDMR3LdrGetSymbolR3(PVM pVM, const char *pszModule, const char *pszSymbol, void **ppvValue)
|
---|
632 | {
|
---|
633 | /*
|
---|
634 | * Validate input.
|
---|
635 | */
|
---|
636 | AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
|
---|
637 |
|
---|
638 | /*
|
---|
639 | * Find the module.
|
---|
640 | */
|
---|
641 | for (PPDMMOD pModule = pVM->pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
|
---|
642 | {
|
---|
643 | if ( pModule->eType == PDMMOD_TYPE_R3
|
---|
644 | && !strcmp(pModule->szName, pszModule))
|
---|
645 | {
|
---|
646 | RTUINTPTR Value = 0;
|
---|
647 | int rc = RTLdrGetSymbolEx(pModule->hLdrMod, pModule->pvBits, pModule->ImageBase, pszSymbol, &Value);
|
---|
648 | if (RT_SUCCESS(rc))
|
---|
649 | {
|
---|
650 | *ppvValue = (void *)(uintptr_t)Value;
|
---|
651 | Assert((uintptr_t)*ppvValue == Value);
|
---|
652 | }
|
---|
653 | else
|
---|
654 | {
|
---|
655 | if ((uintptr_t)pszSymbol < 0x10000)
|
---|
656 | AssertMsg(rc, ("Couldn't symbol '%u' in module '%s'\n", (unsigned)(uintptr_t)pszSymbol, pszModule));
|
---|
657 | else
|
---|
658 | AssertMsg(rc, ("Couldn't symbol '%s' in module '%s'\n", pszSymbol, pszModule));
|
---|
659 | }
|
---|
660 | return rc;
|
---|
661 | }
|
---|
662 | }
|
---|
663 |
|
---|
664 | AssertMsgFailed(("Couldn't locate module '%s'\n", pszModule));
|
---|
665 | return VERR_SYMBOL_NOT_FOUND;
|
---|
666 | }
|
---|
667 |
|
---|
668 |
|
---|
669 | /**
|
---|
670 | * Get the address of a symbol in a given HC ring 0 module.
|
---|
671 | *
|
---|
672 | * @returns VBox status code.
|
---|
673 | * @param pVM VM handle.
|
---|
674 | * @param pszModule Module name. If NULL the main R0 module (VMMR0.r0) is assumes.
|
---|
675 | * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
|
---|
676 | * ordinal value rather than a string pointer.
|
---|
677 | * @param ppvValue Where to store the symbol value.
|
---|
678 | */
|
---|
679 | VMMR3DECL(int) PDMR3LdrGetSymbolR0(PVM pVM, const char *pszModule, const char *pszSymbol, PRTR0PTR ppvValue)
|
---|
680 | {
|
---|
681 | #ifdef PDMLDR_FAKE_MODE
|
---|
682 | *ppvValue = 0xdeadbeef;
|
---|
683 | return VINF_SUCCESS;
|
---|
684 |
|
---|
685 | #else
|
---|
686 | /*
|
---|
687 | * Validate input.
|
---|
688 | */
|
---|
689 | AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
|
---|
690 | if (!pszModule)
|
---|
691 | pszModule = "VMMR0.r0";
|
---|
692 |
|
---|
693 | /*
|
---|
694 | * Find the module.
|
---|
695 | */
|
---|
696 | for (PPDMMOD pModule = pVM->pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
|
---|
697 | {
|
---|
698 | if ( pModule->eType == PDMMOD_TYPE_R0
|
---|
699 | && !strcmp(pModule->szName, pszModule))
|
---|
700 | {
|
---|
701 | int rc = SUPGetSymbolR0((void *)(uintptr_t)pModule->ImageBase, pszSymbol, (void **)ppvValue);
|
---|
702 | if (RT_FAILURE(rc))
|
---|
703 | {
|
---|
704 | AssertMsgRC(rc, ("Couldn't find symbol '%s' in module '%s'\n", pszSymbol, pszModule));
|
---|
705 | LogRel(("PDMGetSymbol: Couldn't find symbol '%s' in module '%s'\n", pszSymbol, pszModule));
|
---|
706 | }
|
---|
707 | return rc;
|
---|
708 | }
|
---|
709 | }
|
---|
710 |
|
---|
711 | AssertMsgFailed(("Couldn't locate module '%s'\n", pszModule));
|
---|
712 | return VERR_SYMBOL_NOT_FOUND;
|
---|
713 | #endif
|
---|
714 | }
|
---|
715 |
|
---|
716 |
|
---|
717 | /**
|
---|
718 | * Same as PDMR3LdrGetSymbolR0 except that the module will be attempted loaded if not found.
|
---|
719 | *
|
---|
720 | * @returns VBox status code.
|
---|
721 | * @param pVM VM handle.
|
---|
722 | * @param pszModule Module name. If NULL the main R0 module (VMMR0.r0) is assumed.
|
---|
723 | * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
|
---|
724 | * ordinal value rather than a string pointer.
|
---|
725 | * @param ppvValue Where to store the symbol value.
|
---|
726 | */
|
---|
727 | VMMR3DECL(int) PDMR3LdrGetSymbolR0Lazy(PVM pVM, const char *pszModule, const char *pszSymbol, PRTR0PTR ppvValue)
|
---|
728 | {
|
---|
729 | #ifdef PDMLDR_FAKE_MODE
|
---|
730 | *ppvValue = 0xdeadbeef;
|
---|
731 | return VINF_SUCCESS;
|
---|
732 |
|
---|
733 | #else
|
---|
734 | /*
|
---|
735 | * Since we're lazy, we'll only check if the module is present
|
---|
736 | * and hand it over to PDMR3LdrGetSymbolR0 when that's done.
|
---|
737 | */
|
---|
738 | AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
|
---|
739 | if (pszModule)
|
---|
740 | {
|
---|
741 | AssertMsgReturn(!strpbrk(pszModule, "/\\:\n\r\t"), ("pszModule=%s\n", pszModule), VERR_INVALID_PARAMETER);
|
---|
742 | PPDMMOD pModule;
|
---|
743 | for (pModule = pVM->pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
|
---|
744 | if ( pModule->eType == PDMMOD_TYPE_R0
|
---|
745 | && !strcmp(pModule->szName, pszModule))
|
---|
746 | break;
|
---|
747 | if (!pModule)
|
---|
748 | {
|
---|
749 | int rc = pdmR3LoadR0U(pVM->pUVM, NULL, pszModule);
|
---|
750 | AssertMsgRCReturn(rc, ("pszModule=%s rc=%Rrc\n", pszModule, rc), VERR_MODULE_NOT_FOUND);
|
---|
751 | }
|
---|
752 | }
|
---|
753 | return PDMR3LdrGetSymbolR0(pVM, pszModule, pszSymbol, ppvValue);
|
---|
754 | #endif
|
---|
755 | }
|
---|
756 |
|
---|
757 |
|
---|
758 | /**
|
---|
759 | * Get the address of a symbol in a given RC module.
|
---|
760 | *
|
---|
761 | * @returns VBox status code.
|
---|
762 | * @param pVM VM handle.
|
---|
763 | * @param pszModule Module name. If NULL the main R0 module (VMMGC.gc) is assumes.
|
---|
764 | * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
|
---|
765 | * ordinal value rather than a string pointer.
|
---|
766 | * @param pRCPtrValue Where to store the symbol value.
|
---|
767 | */
|
---|
768 | VMMR3DECL(int) PDMR3LdrGetSymbolRC(PVM pVM, const char *pszModule, const char *pszSymbol, PRTRCPTR pRCPtrValue)
|
---|
769 | {
|
---|
770 | #ifdef PDMLDR_FAKE_MODE
|
---|
771 | *pRCPtrValue = 0xfeedf00d;
|
---|
772 | return VINF_SUCCESS;
|
---|
773 |
|
---|
774 | #else
|
---|
775 | /*
|
---|
776 | * Validate input.
|
---|
777 | */
|
---|
778 | AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
|
---|
779 | if (!pszModule)
|
---|
780 | pszModule = "VMMGC.gc";
|
---|
781 |
|
---|
782 | /*
|
---|
783 | * Find the module.
|
---|
784 | */
|
---|
785 | for (PPDMMOD pModule = pVM->pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
|
---|
786 | {
|
---|
787 | if ( pModule->eType == PDMMOD_TYPE_RC
|
---|
788 | && !strcmp(pModule->szName, pszModule))
|
---|
789 | {
|
---|
790 | RTUINTPTR Value;
|
---|
791 | int rc = RTLdrGetSymbolEx(pModule->hLdrMod, pModule->pvBits, pModule->ImageBase, pszSymbol, &Value);
|
---|
792 | if (RT_SUCCESS(rc))
|
---|
793 | {
|
---|
794 | *pRCPtrValue = (RTGCPTR)Value;
|
---|
795 | Assert(*pRCPtrValue == Value);
|
---|
796 | }
|
---|
797 | else
|
---|
798 | {
|
---|
799 | if ((uintptr_t)pszSymbol < 0x10000)
|
---|
800 | AssertMsg(rc, ("Couldn't symbol '%u' in module '%s'\n", (unsigned)(uintptr_t)pszSymbol, pszModule));
|
---|
801 | else
|
---|
802 | AssertMsg(rc, ("Couldn't symbol '%s' in module '%s'\n", pszSymbol, pszModule));
|
---|
803 | }
|
---|
804 | return rc;
|
---|
805 | }
|
---|
806 | }
|
---|
807 |
|
---|
808 | AssertMsgFailed(("Couldn't locate module '%s'\n", pszModule));
|
---|
809 | return VERR_SYMBOL_NOT_FOUND;
|
---|
810 | #endif
|
---|
811 | }
|
---|
812 |
|
---|
813 |
|
---|
814 | /**
|
---|
815 | * Same as PDMR3LdrGetSymbolRC except that the module will be attempted loaded if not found.
|
---|
816 | *
|
---|
817 | * @returns VBox status code.
|
---|
818 | * @param pVM VM handle.
|
---|
819 | * @param pszModule Module name. If NULL the main R0 module (VMMGC.gc) is assumes.
|
---|
820 | * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
|
---|
821 | * ordinal value rather than a string pointer.
|
---|
822 | * @param pRCPtrValue Where to store the symbol value.
|
---|
823 | */
|
---|
824 | VMMR3DECL(int) PDMR3LdrGetSymbolRCLazy(PVM pVM, const char *pszModule, const char *pszSymbol, PRTRCPTR pRCPtrValue)
|
---|
825 | {
|
---|
826 | #ifdef PDMLDR_FAKE_MODE
|
---|
827 | *pRCPtrValue = 0xfeedf00d;
|
---|
828 | return VINF_SUCCESS;
|
---|
829 |
|
---|
830 | #else
|
---|
831 | /*
|
---|
832 | * Since we're lazy, we'll only check if the module is present
|
---|
833 | * and hand it over to PDMR3LdrGetSymbolRC when that's done.
|
---|
834 | */
|
---|
835 | AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
|
---|
836 | if (pszModule)
|
---|
837 | {
|
---|
838 | AssertMsgReturn(!strpbrk(pszModule, "/\\:\n\r\t"), ("pszModule=%s\n", pszModule), VERR_INVALID_PARAMETER);
|
---|
839 | PPDMMOD pModule;
|
---|
840 | for (pModule = pVM->pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
|
---|
841 | if ( pModule->eType == PDMMOD_TYPE_RC
|
---|
842 | && !strcmp(pModule->szName, pszModule))
|
---|
843 | break;
|
---|
844 | if (!pModule)
|
---|
845 | {
|
---|
846 | char *pszFilename = pdmR3FileRC(pszModule);
|
---|
847 | AssertMsgReturn(pszFilename, ("pszModule=%s\n", pszModule), VERR_MODULE_NOT_FOUND);
|
---|
848 | int rc = PDMR3LdrLoadRC(pVM, pszFilename, pszModule);
|
---|
849 | RTMemTmpFree(pszFilename);
|
---|
850 | AssertMsgRCReturn(rc, ("pszModule=%s rc=%Rrc\n", pszModule, rc), VERR_MODULE_NOT_FOUND);
|
---|
851 | }
|
---|
852 | }
|
---|
853 | return PDMR3LdrGetSymbolRC(pVM, pszModule, pszSymbol, pRCPtrValue);
|
---|
854 | #endif
|
---|
855 | }
|
---|
856 |
|
---|
857 |
|
---|
858 | /**
|
---|
859 | * Constructs the full filename for a R3 image file.
|
---|
860 | *
|
---|
861 | * @returns Pointer to temporary memory containing the filename.
|
---|
862 | * Caller must free this using RTMemTmpFree().
|
---|
863 | * @returns NULL on failure.
|
---|
864 | *
|
---|
865 | * @param pszFile File name (no path).
|
---|
866 | */
|
---|
867 | char *pdmR3FileR3(const char *pszFile, bool fShared)
|
---|
868 | {
|
---|
869 | return pdmR3File(pszFile, NULL, fShared);
|
---|
870 | }
|
---|
871 |
|
---|
872 |
|
---|
873 | /**
|
---|
874 | * Constructs the full filename for a R0 image file.
|
---|
875 | *
|
---|
876 | * @returns Pointer to temporary memory containing the filename.
|
---|
877 | * Caller must free this using RTMemTmpFree().
|
---|
878 | * @returns NULL on failure.
|
---|
879 | *
|
---|
880 | * @param pszFile File name (no path).
|
---|
881 | */
|
---|
882 | char *pdmR3FileR0(const char *pszFile)
|
---|
883 | {
|
---|
884 | return pdmR3File(pszFile, NULL, /*fShared=*/false);
|
---|
885 | }
|
---|
886 |
|
---|
887 |
|
---|
888 | /**
|
---|
889 | * Constructs the full filename for a RC image file.
|
---|
890 | *
|
---|
891 | * @returns Pointer to temporary memory containing the filename.
|
---|
892 | * Caller must free this using RTMemTmpFree().
|
---|
893 | * @returns NULL on failure.
|
---|
894 | *
|
---|
895 | * @param pszFile File name (no path).
|
---|
896 | */
|
---|
897 | char *pdmR3FileRC(const char *pszFile)
|
---|
898 | {
|
---|
899 | return pdmR3File(pszFile, NULL, /*fShared=*/false);
|
---|
900 | }
|
---|
901 |
|
---|
902 |
|
---|
903 | /**
|
---|
904 | * Worker for pdmR3File().
|
---|
905 | *
|
---|
906 | * @returns Pointer to temporary memory containing the filename.
|
---|
907 | * Caller must free this using RTMemTmpFree().
|
---|
908 | * @returns NULL on failure.
|
---|
909 | *
|
---|
910 | * @param pszDir Directory part
|
---|
911 | * @param pszFile File name part
|
---|
912 | * @param pszDefaultExt Extension part
|
---|
913 | */
|
---|
914 | static char *pdmR3FileConstruct(const char *pszDir, const char *pszFile, const char *pszDefaultExt)
|
---|
915 | {
|
---|
916 | /*
|
---|
917 | * Allocate temp memory for return buffer.
|
---|
918 | */
|
---|
919 | size_t cchDir = strlen(pszDir);
|
---|
920 | size_t cchFile = strlen(pszFile);
|
---|
921 | size_t cchDefaultExt;
|
---|
922 |
|
---|
923 | /*
|
---|
924 | * Default extention?
|
---|
925 | */
|
---|
926 | if (!pszDefaultExt || strchr(pszFile, '.'))
|
---|
927 | cchDefaultExt = 0;
|
---|
928 | else
|
---|
929 | cchDefaultExt = strlen(pszDefaultExt);
|
---|
930 |
|
---|
931 | size_t cchPath = cchDir + 1 + cchFile + cchDefaultExt + 1;
|
---|
932 | AssertMsgReturn(cchPath <= RTPATH_MAX, ("Path too long!\n"), NULL);
|
---|
933 |
|
---|
934 | char *pszRet = (char *)RTMemTmpAlloc(cchDir + 1 + cchFile + cchDefaultExt + 1);
|
---|
935 | AssertMsgReturn(pszRet, ("Out of temporary memory!\n"), NULL);
|
---|
936 |
|
---|
937 | /*
|
---|
938 | * Construct the filename.
|
---|
939 | */
|
---|
940 | memcpy(pszRet, pszDir, cchDir);
|
---|
941 | pszRet[cchDir++] = '/'; /* this works everywhere */
|
---|
942 | memcpy(pszRet + cchDir, pszFile, cchFile + 1);
|
---|
943 | if (cchDefaultExt)
|
---|
944 | memcpy(pszRet + cchDir + cchFile, pszDefaultExt, cchDefaultExt + 1);
|
---|
945 |
|
---|
946 | return pszRet;
|
---|
947 | }
|
---|
948 |
|
---|
949 |
|
---|
950 | /**
|
---|
951 | * Worker for pdmR3FileRC(), pdmR3FileR0() and pdmR3FileR3().
|
---|
952 | *
|
---|
953 | * @returns Pointer to temporary memory containing the filename.
|
---|
954 | * Caller must free this using RTMemTmpFree().
|
---|
955 | * @returns NULL on failure.
|
---|
956 | * @param pszFile File name (no path).
|
---|
957 | * @param pszDefaultExt The default extention, NULL if none.
|
---|
958 | * @param fShared If true, search in the shared directory (/usr/lib on Unix), else
|
---|
959 | * search in the private directory (/usr/lib/virtualbox on Unix).
|
---|
960 | * Ignored if VBOX_PATH_SHARED_LIBS is not defined.
|
---|
961 | * @todo We'll have this elsewhere than in the root later!
|
---|
962 | * @todo Remove the fShared hack again once we don't need to link against VBoxDD anymore!
|
---|
963 | */
|
---|
964 | static char *pdmR3File(const char *pszFile, const char *pszDefaultExt, bool fShared)
|
---|
965 | {
|
---|
966 | char szPath[RTPATH_MAX];
|
---|
967 | int rc;
|
---|
968 |
|
---|
969 | rc = fShared ? RTPathSharedLibs(szPath, sizeof(szPath))
|
---|
970 | : RTPathAppPrivateArch(szPath, sizeof(szPath));
|
---|
971 | if (!RT_SUCCESS(rc))
|
---|
972 | {
|
---|
973 | AssertMsgFailed(("RTPathProgram(,%d) failed rc=%d!\n", sizeof(szPath), rc));
|
---|
974 | return NULL;
|
---|
975 | }
|
---|
976 |
|
---|
977 | return pdmR3FileConstruct(szPath, pszFile, pszDefaultExt);
|
---|
978 | }
|
---|
979 |
|
---|
980 |
|
---|
981 | /** @internal */
|
---|
982 | typedef struct QMFEIPARG
|
---|
983 | {
|
---|
984 | RTRCUINTPTR uPC;
|
---|
985 |
|
---|
986 | char *pszNearSym1;
|
---|
987 | size_t cchNearSym1;
|
---|
988 | RTRCINTPTR offNearSym1;
|
---|
989 |
|
---|
990 | char *pszNearSym2;
|
---|
991 | size_t cchNearSym2;
|
---|
992 | RTRCINTPTR offNearSym2;
|
---|
993 | } QMFEIPARG, *PQMFEIPARG;
|
---|
994 |
|
---|
995 | /**
|
---|
996 | * Queries module information from an PC (eip/rip).
|
---|
997 | *
|
---|
998 | * This is typically used to locate a crash address.
|
---|
999 | *
|
---|
1000 | * @returns VBox status code.
|
---|
1001 | *
|
---|
1002 | * @param pVM VM handle
|
---|
1003 | * @param uPC The program counter (eip/rip) to locate the module for.
|
---|
1004 | * @param pszModName Where to store the module name.
|
---|
1005 | * @param cchModName Size of the module name buffer.
|
---|
1006 | * @param pMod Base address of the module.
|
---|
1007 | * @param pszNearSym1 Name of the closes symbol from below.
|
---|
1008 | * @param cchNearSym1 Size of the buffer pointed to by pszNearSym1.
|
---|
1009 | * @param pNearSym1 The address of pszNearSym1.
|
---|
1010 | * @param pszNearSym2 Name of the closes symbol from below.
|
---|
1011 | * @param cchNearSym2 Size of the buffer pointed to by pszNearSym2.
|
---|
1012 | * @param pNearSym2 The address of pszNearSym2.
|
---|
1013 | */
|
---|
1014 | VMMR3DECL(int) PDMR3LdrQueryRCModFromPC(PVM pVM, RTRCPTR uPC,
|
---|
1015 | char *pszModName, size_t cchModName, PRTRCPTR pMod,
|
---|
1016 | char *pszNearSym1, size_t cchNearSym1, PRTRCPTR pNearSym1,
|
---|
1017 | char *pszNearSym2, size_t cchNearSym2, PRTRCPTR pNearSym2)
|
---|
1018 | {
|
---|
1019 | int rc = VERR_MODULE_NOT_FOUND;
|
---|
1020 | PPDMMOD pCur;
|
---|
1021 | for (pCur = pVM->pUVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
|
---|
1022 | {
|
---|
1023 | /* Skip anything which isn't in GC. */
|
---|
1024 | if (pCur->eType != PDMMOD_TYPE_RC)
|
---|
1025 | continue;
|
---|
1026 | if (uPC - pCur->ImageBase < RTLdrSize(pCur->hLdrMod))
|
---|
1027 | {
|
---|
1028 | if (pMod)
|
---|
1029 | *pMod = pCur->ImageBase;
|
---|
1030 | if (pszModName && cchModName)
|
---|
1031 | {
|
---|
1032 | *pszModName = '\0';
|
---|
1033 | strncat(pszModName, pCur->szName, cchModName);
|
---|
1034 | }
|
---|
1035 | if (pNearSym1) *pNearSym1 = 0;
|
---|
1036 | if (pNearSym2) *pNearSym2 = 0;
|
---|
1037 | if (pszNearSym1) *pszNearSym1 = '\0';
|
---|
1038 | if (pszNearSym2) *pszNearSym2 = '\0';
|
---|
1039 |
|
---|
1040 | /*
|
---|
1041 | * Locate the nearest symbols.
|
---|
1042 | */
|
---|
1043 | QMFEIPARG Args;
|
---|
1044 | Args.uPC = uPC;
|
---|
1045 | Args.pszNearSym1 = pszNearSym1;
|
---|
1046 | Args.cchNearSym1 = cchNearSym1;
|
---|
1047 | Args.offNearSym1 = RTRCINTPTR_MIN;
|
---|
1048 | Args.pszNearSym2 = pszNearSym2;
|
---|
1049 | Args.cchNearSym2 = cchNearSym2;
|
---|
1050 | Args.offNearSym2 = RTRCINTPTR_MAX;
|
---|
1051 |
|
---|
1052 | rc = RTLdrEnumSymbols(pCur->hLdrMod, RTLDR_ENUM_SYMBOL_FLAGS_ALL, pCur->pvBits, pCur->ImageBase,
|
---|
1053 | pdmR3QueryModFromEIPEnumSymbols, &Args);
|
---|
1054 | if (pNearSym1 && Args.offNearSym1 != INT_MIN)
|
---|
1055 | *pNearSym1 = Args.offNearSym1 + uPC;
|
---|
1056 | if (pNearSym2 && Args.offNearSym2 != INT_MAX)
|
---|
1057 | *pNearSym2 = Args.offNearSym2 + uPC;
|
---|
1058 |
|
---|
1059 | rc = VINF_SUCCESS;
|
---|
1060 | if (pCur->eType == PDMMOD_TYPE_RC)
|
---|
1061 | break;
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | }
|
---|
1065 | return rc;
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 |
|
---|
1069 | /**
|
---|
1070 | * Enumeration callback function used by RTLdrEnumSymbols().
|
---|
1071 | *
|
---|
1072 | * @returns VBox status code. Failure will stop the enumeration.
|
---|
1073 | * @param hLdrMod The loader module handle.
|
---|
1074 | * @param pszSymbol Symbol name. NULL if ordinal only.
|
---|
1075 | * @param uSymbol Symbol ordinal, ~0 if not used.
|
---|
1076 | * @param Value Symbol value.
|
---|
1077 | * @param pvUser The user argument specified to RTLdrEnumSymbols().
|
---|
1078 | */
|
---|
1079 | static DECLCALLBACK(int) pdmR3QueryModFromEIPEnumSymbols(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
|
---|
1080 | {
|
---|
1081 | PQMFEIPARG pArgs = (PQMFEIPARG)pvUser;
|
---|
1082 |
|
---|
1083 | RTINTPTR off = Value - pArgs->uPC;
|
---|
1084 | if (off <= 0) /* near1 is before or at same location. */
|
---|
1085 | {
|
---|
1086 | if (off > pArgs->offNearSym1)
|
---|
1087 | {
|
---|
1088 | pArgs->offNearSym1 = off;
|
---|
1089 | if (pArgs->pszNearSym1 && pArgs->cchNearSym1)
|
---|
1090 | {
|
---|
1091 | *pArgs->pszNearSym1 = '\0';
|
---|
1092 | if (pszSymbol)
|
---|
1093 | strncat(pArgs->pszNearSym1, pszSymbol, pArgs->cchNearSym1);
|
---|
1094 | else
|
---|
1095 | {
|
---|
1096 | char szOrd[32];
|
---|
1097 | RTStrPrintf(szOrd, sizeof(szOrd), "#%#x", uSymbol);
|
---|
1098 | strncat(pArgs->pszNearSym1, szOrd, pArgs->cchNearSym1);
|
---|
1099 | }
|
---|
1100 | }
|
---|
1101 | }
|
---|
1102 | }
|
---|
1103 | else /* near2 is after */
|
---|
1104 | {
|
---|
1105 | if (off < pArgs->offNearSym2)
|
---|
1106 | {
|
---|
1107 | pArgs->offNearSym2 = off;
|
---|
1108 | if (pArgs->pszNearSym2 && pArgs->cchNearSym2)
|
---|
1109 | {
|
---|
1110 | *pArgs->pszNearSym2 = '\0';
|
---|
1111 | if (pszSymbol)
|
---|
1112 | strncat(pArgs->pszNearSym2, pszSymbol, pArgs->cchNearSym2);
|
---|
1113 | else
|
---|
1114 | {
|
---|
1115 | char szOrd[32];
|
---|
1116 | RTStrPrintf(szOrd, sizeof(szOrd), "#%#x", uSymbol);
|
---|
1117 | strncat(pArgs->pszNearSym2, szOrd, pArgs->cchNearSym2);
|
---|
1118 | }
|
---|
1119 | }
|
---|
1120 | }
|
---|
1121 | }
|
---|
1122 |
|
---|
1123 | return VINF_SUCCESS;
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 |
|
---|
1127 | /**
|
---|
1128 | * Enumerate all PDM modules.
|
---|
1129 | *
|
---|
1130 | * @returns VBox status.
|
---|
1131 | * @param pVM VM Handle.
|
---|
1132 | * @param pfnCallback Function to call back for each of the modules.
|
---|
1133 | * @param pvArg User argument.
|
---|
1134 | */
|
---|
1135 | VMMR3DECL(int) PDMR3LdrEnumModules(PVM pVM, PFNPDMR3ENUM pfnCallback, void *pvArg)
|
---|
1136 | {
|
---|
1137 | PPDMMOD pCur;
|
---|
1138 | for (pCur = pVM->pUVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
|
---|
1139 | {
|
---|
1140 | int rc = pfnCallback(pVM,
|
---|
1141 | pCur->szFilename,
|
---|
1142 | pCur->szName,
|
---|
1143 | pCur->ImageBase,
|
---|
1144 | pCur->eType == PDMMOD_TYPE_RC ? RTLdrSize(pCur->hLdrMod) : 0,
|
---|
1145 | pCur->eType == PDMMOD_TYPE_RC,
|
---|
1146 | pvArg);
|
---|
1147 | if (RT_FAILURE(rc))
|
---|
1148 | return rc;
|
---|
1149 | }
|
---|
1150 | return VINF_SUCCESS;
|
---|
1151 | }
|
---|
1152 |
|
---|