VirtualBox

source: vbox/trunk/src/VBox/VMM/PDMLdr.cpp@ 20007

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

IPRT,SUP: Renamed RTPathProgram to RTPathExecDir to make it clear what it returns. Renamed hardened version of it as well.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 38.3 KB
Line 
1/* $Id: PDMLdr.cpp 19924 2009-05-22 21:52:47Z 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 */
56typedef struct PDMGETIMPORTARGS
57{
58 PVM pVM;
59 PPDMMOD pModule;
60} PDMGETIMPORTARGS, *PPDMGETIMPORTARGS;
61
62
63/*******************************************************************************
64* Internal Functions *
65*******************************************************************************/
66static DECLCALLBACK(int) pdmR3GetImportRC(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser);
67static int pdmR3LoadR0U(PUVM pUVM, const char *pszFilename, const char *pszName);
68static char * pdmR3FileRC(const char *pszFile);
69static char * pdmR3FileR0(const char *pszFile);
70static char * pdmR3File(const char *pszFile, const char *pszDefaultExt, bool fShared);
71static 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 */
81VMMR3DECL(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 */
97int 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 */
121void 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 */
177VMMR3DECL(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 */
239int 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 */
315static 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 (!strcmp(pszSymbol, "g_TRPMCPU"))
339 *pValue = VM_RC_ADDR(pVM, &pVM->aCpus[0].trpm);
340 else if ( !strncmp(pszSymbol, "VMM", 3)
341 || !strcmp(pszSymbol, "g_Logger")
342 || !strcmp(pszSymbol, "g_RelLogger"))
343 {
344 RTRCPTR RCPtr = 0;
345 rc = VMMR3GetImportRC(pVM, pszSymbol, &RCPtr);
346 if (RT_SUCCESS(rc))
347 *pValue = RCPtr;
348 }
349 else if ( !strncmp(pszSymbol, "TM", 2)
350 || !strcmp(pszSymbol, "g_pSUPGlobalInfoPage"))
351 {
352 RTRCPTR RCPtr = 0;
353 rc = TMR3GetImportRC(pVM, pszSymbol, &RCPtr);
354 if (RT_SUCCESS(rc))
355 *pValue = RCPtr;
356 }
357 else
358 {
359 AssertMsg(!pszModule, ("Unknown builtin symbol '%s' for module '%s'!\n", pszSymbol, pModule->szName)); NOREF(pModule);
360 rc = VERR_SYMBOL_NOT_FOUND;
361 }
362 if (RT_SUCCESS(rc) || pszModule)
363 return rc;
364 }
365
366 /*
367 * Search for module.
368 */
369 PPDMMOD pCur = pVM->pUVM->pdm.s.pModules;
370 while (pCur)
371 {
372 if ( pCur->eType == PDMMOD_TYPE_RC
373 && ( !pszModule
374 || !strcmp(pCur->szName, pszModule))
375 )
376 {
377 /* Search for the symbol. */
378 int rc = RTLdrGetSymbolEx(pCur->hLdrMod, pCur->pvBits, pCur->ImageBase, pszSymbol, pValue);
379 if (RT_SUCCESS(rc))
380 {
381 AssertMsg(*pValue - pCur->ImageBase < RTLdrSize(pCur->hLdrMod),
382 ("%RRv-%RRv %s %RRv\n", (RTRCPTR)pCur->ImageBase,
383 (RTRCPTR)(pCur->ImageBase + RTLdrSize(pCur->hLdrMod) - 1),
384 pszSymbol, (RTRCPTR)*pValue));
385 return rc;
386 }
387 if (pszModule)
388 {
389 AssertMsgFailed(("Couldn't find symbol '%s' in module '%s'!\n", pszSymbol, pszModule));
390 LogRel(("PDMLdr: Couldn't find symbol '%s' in module '%s'!\n", pszSymbol, pszModule));
391 return VERR_SYMBOL_NOT_FOUND;
392 }
393 }
394
395 /* next */
396 pCur = pCur->pNext;
397 }
398
399 AssertMsgFailed(("Couldn't find module '%s' for resolving symbol '%s'!\n", pszModule, pszSymbol));
400 return VERR_SYMBOL_NOT_FOUND;
401}
402
403
404/**
405 * Loads a module into the guest context (i.e. into the Hypervisor memory region).
406 *
407 * @returns VBox status code.
408 * @param pVM The VM to load it into.
409 * @param pszFilename Filename of the module binary.
410 * @param pszName Module name. Case sensitive and the length is limited!
411 */
412VMMR3DECL(int) PDMR3LdrLoadRC(PVM pVM, const char *pszFilename, const char *pszName)
413{
414 /*
415 * Validate input.
416 */
417 AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
418 PPDMMOD pCur = pVM->pUVM->pdm.s.pModules;
419 while (pCur)
420 {
421 if (!strcmp(pCur->szName, pszName))
422 {
423 AssertMsgFailed(("We've already got a module '%s' loaded!\n", pszName));
424 return VERR_PDM_MODULE_NAME_CLASH;
425 }
426 /* next */
427 pCur = pCur->pNext;
428 }
429
430 /*
431 * Find the file if not specified.
432 */
433 char *pszFile = NULL;
434 if (!pszFilename)
435 pszFilename = pszFile = pdmR3FileRC(pszName);
436
437 /*
438 * Allocate the module list node.
439 */
440 PPDMMOD pModule = (PPDMMOD)RTMemAllocZ(sizeof(*pModule) + strlen(pszFilename));
441 if (!pModule)
442 {
443 RTMemTmpFree(pszFile);
444 return VERR_NO_MEMORY;
445 }
446 AssertMsg(strlen(pszName) + 1 < sizeof(pModule->szName),
447 ("pazName is too long (%d chars) max is %d chars.\n", strlen(pszName), sizeof(pModule->szName) - 1));
448 strcpy(pModule->szName, pszName);
449 pModule->eType = PDMMOD_TYPE_RC;
450 strcpy(pModule->szFilename, pszFilename);
451
452
453 /*
454 * Open the loader item.
455 */
456 int rc = SUPR3HardenedVerifyFile(pszFilename, "PDMR3LdrLoadRC", NULL);
457 if (RT_SUCCESS(rc))
458 rc = RTLdrOpen(pszFilename, 0, RTLDRARCH_X86_32, &pModule->hLdrMod);
459 if (RT_SUCCESS(rc))
460 {
461 /*
462 * Allocate space in the hypervisor.
463 */
464 size_t cb = RTLdrSize(pModule->hLdrMod);
465 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
466 uint32_t cPages = (uint32_t)(cb >> PAGE_SHIFT);
467 if (((size_t)cPages << PAGE_SHIFT) == cb)
468 {
469 PSUPPAGE paPages = (PSUPPAGE)RTMemTmpAlloc(cPages * sizeof(paPages[0]));
470 if (paPages)
471 {
472 rc = SUPR3PageAllocEx(cPages, 0 /*fFlags*/, &pModule->pvBits, NULL /*pR0Ptr*/, paPages);
473 if (RT_SUCCESS(rc))
474 {
475 RTGCPTR GCPtr;
476 rc = MMR3HyperMapPages(pVM, pModule->pvBits, NIL_RTR0PTR,
477 cPages, paPages, pModule->szName, &GCPtr);
478 if (RT_SUCCESS(rc))
479 {
480 MMR3HyperReserve(pVM, PAGE_SIZE, "fence", NULL);
481
482 /*
483 * Get relocated image bits.
484 */
485 Assert(MMHyperR3ToRC(pVM, pModule->pvBits) == GCPtr);
486 pModule->ImageBase = GCPtr;
487 PDMGETIMPORTARGS Args;
488 Args.pVM = pVM;
489 Args.pModule = pModule;
490 rc = RTLdrGetBits(pModule->hLdrMod, pModule->pvBits, pModule->ImageBase, pdmR3GetImportRC, &Args);
491 if (RT_SUCCESS(rc))
492 {
493 /*
494 * Insert the module.
495 */
496 PUVM pUVM = pVM->pUVM;
497 if (pUVM->pdm.s.pModules)
498 {
499 /* we don't expect this list to be very long, so rather save the tail pointer. */
500 PPDMMOD pCur = pUVM->pdm.s.pModules;
501 while (pCur->pNext)
502 pCur = pCur->pNext;
503 pCur->pNext = pModule;
504 }
505 else
506 pUVM->pdm.s.pModules = pModule; /* (pNext is zeroed by alloc) */
507 Log(("PDM: RC Module at %RRv %s (%s)\n", (RTRCPTR)pModule->ImageBase, pszName, pszFilename));
508 RTMemTmpFree(pszFile);
509 RTMemTmpFree(paPages);
510 return VINF_SUCCESS;
511 }
512 }
513 else
514 {
515 AssertRC(rc);
516 SUPR3PageFreeEx(pModule->pvBits, cPages);
517 }
518 }
519 else
520 AssertMsgFailed(("SUPPageAlloc(%d,) -> %Rrc\n", cPages, rc));
521 RTMemTmpFree(paPages);
522 }
523 else
524 rc = VERR_NO_TMP_MEMORY;
525 }
526 else
527 rc = VERR_OUT_OF_RANGE;
528 int rc2 = RTLdrClose(pModule->hLdrMod);
529 AssertRC(rc2);
530 }
531 RTMemFree(pModule);
532 RTMemTmpFree(pszFile);
533
534 /* Don't consider VERR_PDM_MODULE_NAME_CLASH and VERR_NO_MEMORY above as these are very unlikely. */
535 if (RT_FAILURE(rc))
536 return VMSetError(pVM, rc, RT_SRC_POS, N_("Cannot load GC module %s"), pszFilename);
537 return rc;
538}
539
540
541/**
542 * Loads a module into the ring-0 context.
543 *
544 * @returns VBox status code.
545 * @param pUVM Pointer to the user mode VM structure.
546 * @param pszFilename Filename of the module binary.
547 * @param pszName Module name. Case sensitive and the length is limited!
548 */
549static int pdmR3LoadR0U(PUVM pUVM, const char *pszFilename, const char *pszName)
550{
551 /*
552 * Validate input.
553 */
554 PPDMMOD pCur = pUVM->pdm.s.pModules;
555 while (pCur)
556 {
557 if (!strcmp(pCur->szName, pszName))
558 {
559 AssertMsgFailed(("We've already got a module '%s' loaded!\n", pszName));
560 return VERR_PDM_MODULE_NAME_CLASH;
561 }
562 /* next */
563 pCur = pCur->pNext;
564 }
565
566 /*
567 * Find the file if not specified.
568 */
569 char *pszFile = NULL;
570 if (!pszFilename)
571 pszFilename = pszFile = pdmR3FileR0(pszName);
572
573 /*
574 * Allocate the module list node.
575 */
576 PPDMMOD pModule = (PPDMMOD)RTMemAllocZ(sizeof(*pModule) + strlen(pszFilename));
577 if (!pModule)
578 {
579 RTMemTmpFree(pszFile);
580 return VERR_NO_MEMORY;
581 }
582 AssertMsg(strlen(pszName) + 1 < sizeof(pModule->szName),
583 ("pazName is too long (%d chars) max is %d chars.\n", strlen(pszName), sizeof(pModule->szName) - 1));
584 strcpy(pModule->szName, pszName);
585 pModule->eType = PDMMOD_TYPE_R0;
586 strcpy(pModule->szFilename, pszFilename);
587
588 /*
589 * Ask the support library to load it.
590 */
591 void *pvImageBase;
592 int rc = SUPLoadModule(pszFilename, pszName, &pvImageBase);
593 if (RT_SUCCESS(rc))
594 {
595 pModule->hLdrMod = NIL_RTLDRMOD;
596 pModule->ImageBase = (uintptr_t)pvImageBase;
597
598 /*
599 * Insert the module.
600 */
601 if (pUVM->pdm.s.pModules)
602 {
603 /* we don't expect this list to be very long, so rather save the tail pointer. */
604 PPDMMOD pCur = pUVM->pdm.s.pModules;
605 while (pCur->pNext)
606 pCur = pCur->pNext;
607 pCur->pNext = pModule;
608 }
609 else
610 pUVM->pdm.s.pModules = pModule; /* (pNext is zeroed by alloc) */
611 Log(("PDM: R0 Module at %RHv %s (%s)\n", (RTR0PTR)pModule->ImageBase, pszName, pszFilename));
612 RTMemTmpFree(pszFile);
613 return VINF_SUCCESS;
614 }
615
616 RTMemFree(pModule);
617 RTMemTmpFree(pszFile);
618 LogRel(("pdmR3LoadR0U: pszName=\"%s\" rc=%Rrc\n", pszName, rc));
619
620 /* Don't consider VERR_PDM_MODULE_NAME_CLASH and VERR_NO_MEMORY above as these are very unlikely. */
621 if (RT_FAILURE(rc) && pUVM->pVM) /** @todo VMR3SetErrorU. */
622 return VMSetError(pUVM->pVM, rc, RT_SRC_POS, N_("Cannot load R0 module %s"), pszFilename);
623 return rc;
624}
625
626
627
628/**
629 * Get the address of a symbol in a given HC ring 3 module.
630 *
631 * @returns VBox status code.
632 * @param pVM VM handle.
633 * @param pszModule Module name.
634 * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
635 * ordinal value rather than a string pointer.
636 * @param ppvValue Where to store the symbol value.
637 */
638VMMR3DECL(int) PDMR3LdrGetSymbolR3(PVM pVM, const char *pszModule, const char *pszSymbol, void **ppvValue)
639{
640 /*
641 * Validate input.
642 */
643 AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
644
645 /*
646 * Find the module.
647 */
648 for (PPDMMOD pModule = pVM->pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
649 {
650 if ( pModule->eType == PDMMOD_TYPE_R3
651 && !strcmp(pModule->szName, pszModule))
652 {
653 RTUINTPTR Value = 0;
654 int rc = RTLdrGetSymbolEx(pModule->hLdrMod, pModule->pvBits, pModule->ImageBase, pszSymbol, &Value);
655 if (RT_SUCCESS(rc))
656 {
657 *ppvValue = (void *)(uintptr_t)Value;
658 Assert((uintptr_t)*ppvValue == Value);
659 }
660 else
661 {
662 if ((uintptr_t)pszSymbol < 0x10000)
663 AssertMsg(rc, ("Couldn't symbol '%u' in module '%s'\n", (unsigned)(uintptr_t)pszSymbol, pszModule));
664 else
665 AssertMsg(rc, ("Couldn't symbol '%s' in module '%s'\n", pszSymbol, pszModule));
666 }
667 return rc;
668 }
669 }
670
671 AssertMsgFailed(("Couldn't locate module '%s'\n", pszModule));
672 return VERR_SYMBOL_NOT_FOUND;
673}
674
675
676/**
677 * Get the address of a symbol in a given HC ring 0 module.
678 *
679 * @returns VBox status code.
680 * @param pVM VM handle.
681 * @param pszModule Module name. If NULL the main R0 module (VMMR0.r0) is assumes.
682 * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
683 * ordinal value rather than a string pointer.
684 * @param ppvValue Where to store the symbol value.
685 */
686VMMR3DECL(int) PDMR3LdrGetSymbolR0(PVM pVM, const char *pszModule, const char *pszSymbol, PRTR0PTR ppvValue)
687{
688#ifdef PDMLDR_FAKE_MODE
689 *ppvValue = 0xdeadbeef;
690 return VINF_SUCCESS;
691
692#else
693 /*
694 * Validate input.
695 */
696 AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
697 if (!pszModule)
698 pszModule = "VMMR0.r0";
699
700 /*
701 * Find the module.
702 */
703 for (PPDMMOD pModule = pVM->pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
704 {
705 if ( pModule->eType == PDMMOD_TYPE_R0
706 && !strcmp(pModule->szName, pszModule))
707 {
708 int rc = SUPGetSymbolR0((void *)(uintptr_t)pModule->ImageBase, pszSymbol, (void **)ppvValue);
709 if (RT_FAILURE(rc))
710 {
711 AssertMsgRC(rc, ("Couldn't find symbol '%s' in module '%s'\n", pszSymbol, pszModule));
712 LogRel(("PDMGetSymbol: Couldn't find symbol '%s' in module '%s'\n", pszSymbol, pszModule));
713 }
714 return rc;
715 }
716 }
717
718 AssertMsgFailed(("Couldn't locate module '%s'\n", pszModule));
719 return VERR_SYMBOL_NOT_FOUND;
720#endif
721}
722
723
724/**
725 * Same as PDMR3LdrGetSymbolR0 except that the module will be attempted loaded if not found.
726 *
727 * @returns VBox status code.
728 * @param pVM VM handle.
729 * @param pszModule Module name. If NULL the main R0 module (VMMR0.r0) is assumed.
730 * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
731 * ordinal value rather than a string pointer.
732 * @param ppvValue Where to store the symbol value.
733 */
734VMMR3DECL(int) PDMR3LdrGetSymbolR0Lazy(PVM pVM, const char *pszModule, const char *pszSymbol, PRTR0PTR ppvValue)
735{
736#ifdef PDMLDR_FAKE_MODE
737 *ppvValue = 0xdeadbeef;
738 return VINF_SUCCESS;
739
740#else
741 /*
742 * Since we're lazy, we'll only check if the module is present
743 * and hand it over to PDMR3LdrGetSymbolR0 when that's done.
744 */
745 AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
746 if (pszModule)
747 {
748 AssertMsgReturn(!strpbrk(pszModule, "/\\:\n\r\t"), ("pszModule=%s\n", pszModule), VERR_INVALID_PARAMETER);
749 PPDMMOD pModule;
750 for (pModule = pVM->pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
751 if ( pModule->eType == PDMMOD_TYPE_R0
752 && !strcmp(pModule->szName, pszModule))
753 break;
754 if (!pModule)
755 {
756 int rc = pdmR3LoadR0U(pVM->pUVM, NULL, pszModule);
757 AssertMsgRCReturn(rc, ("pszModule=%s rc=%Rrc\n", pszModule, rc), VERR_MODULE_NOT_FOUND);
758 }
759 }
760 return PDMR3LdrGetSymbolR0(pVM, pszModule, pszSymbol, ppvValue);
761#endif
762}
763
764
765/**
766 * Get the address of a symbol in a given RC module.
767 *
768 * @returns VBox status code.
769 * @param pVM VM handle.
770 * @param pszModule Module name. If NULL the main R0 module (VMMGC.gc) is assumes.
771 * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
772 * ordinal value rather than a string pointer.
773 * @param pRCPtrValue Where to store the symbol value.
774 */
775VMMR3DECL(int) PDMR3LdrGetSymbolRC(PVM pVM, const char *pszModule, const char *pszSymbol, PRTRCPTR pRCPtrValue)
776{
777#ifdef PDMLDR_FAKE_MODE
778 *pRCPtrValue = 0xfeedf00d;
779 return VINF_SUCCESS;
780
781#else
782 /*
783 * Validate input.
784 */
785 AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
786 if (!pszModule)
787 pszModule = "VMMGC.gc";
788
789 /*
790 * Find the module.
791 */
792 for (PPDMMOD pModule = pVM->pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
793 {
794 if ( pModule->eType == PDMMOD_TYPE_RC
795 && !strcmp(pModule->szName, pszModule))
796 {
797 RTUINTPTR Value;
798 int rc = RTLdrGetSymbolEx(pModule->hLdrMod, pModule->pvBits, pModule->ImageBase, pszSymbol, &Value);
799 if (RT_SUCCESS(rc))
800 {
801 *pRCPtrValue = (RTGCPTR)Value;
802 Assert(*pRCPtrValue == Value);
803 }
804 else
805 {
806 if ((uintptr_t)pszSymbol < 0x10000)
807 AssertMsg(rc, ("Couldn't symbol '%u' in module '%s'\n", (unsigned)(uintptr_t)pszSymbol, pszModule));
808 else
809 AssertMsg(rc, ("Couldn't symbol '%s' in module '%s'\n", pszSymbol, pszModule));
810 }
811 return rc;
812 }
813 }
814
815 AssertMsgFailed(("Couldn't locate module '%s'\n", pszModule));
816 return VERR_SYMBOL_NOT_FOUND;
817#endif
818}
819
820
821/**
822 * Same as PDMR3LdrGetSymbolRC except that the module will be attempted loaded if not found.
823 *
824 * @returns VBox status code.
825 * @param pVM VM handle.
826 * @param pszModule Module name. If NULL the main R0 module (VMMGC.gc) is assumes.
827 * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
828 * ordinal value rather than a string pointer.
829 * @param pRCPtrValue Where to store the symbol value.
830 */
831VMMR3DECL(int) PDMR3LdrGetSymbolRCLazy(PVM pVM, const char *pszModule, const char *pszSymbol, PRTRCPTR pRCPtrValue)
832{
833#ifdef PDMLDR_FAKE_MODE
834 *pRCPtrValue = 0xfeedf00d;
835 return VINF_SUCCESS;
836
837#else
838 /*
839 * Since we're lazy, we'll only check if the module is present
840 * and hand it over to PDMR3LdrGetSymbolRC when that's done.
841 */
842 AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
843 if (pszModule)
844 {
845 AssertMsgReturn(!strpbrk(pszModule, "/\\:\n\r\t"), ("pszModule=%s\n", pszModule), VERR_INVALID_PARAMETER);
846 PPDMMOD pModule;
847 for (pModule = pVM->pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
848 if ( pModule->eType == PDMMOD_TYPE_RC
849 && !strcmp(pModule->szName, pszModule))
850 break;
851 if (!pModule)
852 {
853 char *pszFilename = pdmR3FileRC(pszModule);
854 AssertMsgReturn(pszFilename, ("pszModule=%s\n", pszModule), VERR_MODULE_NOT_FOUND);
855 int rc = PDMR3LdrLoadRC(pVM, pszFilename, pszModule);
856 RTMemTmpFree(pszFilename);
857 AssertMsgRCReturn(rc, ("pszModule=%s rc=%Rrc\n", pszModule, rc), VERR_MODULE_NOT_FOUND);
858 }
859 }
860 return PDMR3LdrGetSymbolRC(pVM, pszModule, pszSymbol, pRCPtrValue);
861#endif
862}
863
864
865/**
866 * Constructs the full filename for a R3 image file.
867 *
868 * @returns Pointer to temporary memory containing the filename.
869 * Caller must free this using RTMemTmpFree().
870 * @returns NULL on failure.
871 *
872 * @param pszFile File name (no path).
873 */
874char *pdmR3FileR3(const char *pszFile, bool fShared)
875{
876 return pdmR3File(pszFile, NULL, fShared);
877}
878
879
880/**
881 * Constructs the full filename for a R0 image file.
882 *
883 * @returns Pointer to temporary memory containing the filename.
884 * Caller must free this using RTMemTmpFree().
885 * @returns NULL on failure.
886 *
887 * @param pszFile File name (no path).
888 */
889char *pdmR3FileR0(const char *pszFile)
890{
891 return pdmR3File(pszFile, NULL, /*fShared=*/false);
892}
893
894
895/**
896 * Constructs the full filename for a RC image file.
897 *
898 * @returns Pointer to temporary memory containing the filename.
899 * Caller must free this using RTMemTmpFree().
900 * @returns NULL on failure.
901 *
902 * @param pszFile File name (no path).
903 */
904char *pdmR3FileRC(const char *pszFile)
905{
906 return pdmR3File(pszFile, NULL, /*fShared=*/false);
907}
908
909
910/**
911 * Worker for pdmR3File().
912 *
913 * @returns Pointer to temporary memory containing the filename.
914 * Caller must free this using RTMemTmpFree().
915 * @returns NULL on failure.
916 *
917 * @param pszDir Directory part
918 * @param pszFile File name part
919 * @param pszDefaultExt Extension part
920 */
921static char *pdmR3FileConstruct(const char *pszDir, const char *pszFile, const char *pszDefaultExt)
922{
923 /*
924 * Allocate temp memory for return buffer.
925 */
926 size_t cchDir = strlen(pszDir);
927 size_t cchFile = strlen(pszFile);
928 size_t cchDefaultExt;
929
930 /*
931 * Default extention?
932 */
933 if (!pszDefaultExt || strchr(pszFile, '.'))
934 cchDefaultExt = 0;
935 else
936 cchDefaultExt = strlen(pszDefaultExt);
937
938 size_t cchPath = cchDir + 1 + cchFile + cchDefaultExt + 1;
939 AssertMsgReturn(cchPath <= RTPATH_MAX, ("Path too long!\n"), NULL);
940
941 char *pszRet = (char *)RTMemTmpAlloc(cchDir + 1 + cchFile + cchDefaultExt + 1);
942 AssertMsgReturn(pszRet, ("Out of temporary memory!\n"), NULL);
943
944 /*
945 * Construct the filename.
946 */
947 memcpy(pszRet, pszDir, cchDir);
948 pszRet[cchDir++] = '/'; /* this works everywhere */
949 memcpy(pszRet + cchDir, pszFile, cchFile + 1);
950 if (cchDefaultExt)
951 memcpy(pszRet + cchDir + cchFile, pszDefaultExt, cchDefaultExt + 1);
952
953 return pszRet;
954}
955
956
957/**
958 * Worker for pdmR3FileRC(), pdmR3FileR0() and pdmR3FileR3().
959 *
960 * @returns Pointer to temporary memory containing the filename.
961 * Caller must free this using RTMemTmpFree().
962 * @returns NULL on failure.
963 * @param pszFile File name (no path).
964 * @param pszDefaultExt The default extention, NULL if none.
965 * @param fShared If true, search in the shared directory (/usr/lib on Unix), else
966 * search in the private directory (/usr/lib/virtualbox on Unix).
967 * Ignored if VBOX_PATH_SHARED_LIBS is not defined.
968 * @todo We'll have this elsewhere than in the root later!
969 * @todo Remove the fShared hack again once we don't need to link against VBoxDD anymore!
970 */
971static char *pdmR3File(const char *pszFile, const char *pszDefaultExt, bool fShared)
972{
973 char szPath[RTPATH_MAX];
974 int rc;
975
976 rc = fShared ? RTPathSharedLibs(szPath, sizeof(szPath))
977 : RTPathAppPrivateArch(szPath, sizeof(szPath));
978 if (!RT_SUCCESS(rc))
979 {
980 AssertMsgFailed(("RTPath[SharedLibs|AppPrivateArch](,%d) failed rc=%d!\n", sizeof(szPath), rc));
981 return NULL;
982 }
983
984 return pdmR3FileConstruct(szPath, pszFile, pszDefaultExt);
985}
986
987
988/** @internal */
989typedef struct QMFEIPARG
990{
991 RTRCUINTPTR uPC;
992
993 char *pszNearSym1;
994 size_t cchNearSym1;
995 RTRCINTPTR offNearSym1;
996
997 char *pszNearSym2;
998 size_t cchNearSym2;
999 RTRCINTPTR offNearSym2;
1000} QMFEIPARG, *PQMFEIPARG;
1001
1002/**
1003 * Queries module information from an PC (eip/rip).
1004 *
1005 * This is typically used to locate a crash address.
1006 *
1007 * @returns VBox status code.
1008 *
1009 * @param pVM VM handle
1010 * @param uPC The program counter (eip/rip) to locate the module for.
1011 * @param pszModName Where to store the module name.
1012 * @param cchModName Size of the module name buffer.
1013 * @param pMod Base address of the module.
1014 * @param pszNearSym1 Name of the closes symbol from below.
1015 * @param cchNearSym1 Size of the buffer pointed to by pszNearSym1.
1016 * @param pNearSym1 The address of pszNearSym1.
1017 * @param pszNearSym2 Name of the closes symbol from below.
1018 * @param cchNearSym2 Size of the buffer pointed to by pszNearSym2.
1019 * @param pNearSym2 The address of pszNearSym2.
1020 */
1021VMMR3DECL(int) PDMR3LdrQueryRCModFromPC(PVM pVM, RTRCPTR uPC,
1022 char *pszModName, size_t cchModName, PRTRCPTR pMod,
1023 char *pszNearSym1, size_t cchNearSym1, PRTRCPTR pNearSym1,
1024 char *pszNearSym2, size_t cchNearSym2, PRTRCPTR pNearSym2)
1025{
1026 int rc = VERR_MODULE_NOT_FOUND;
1027 PPDMMOD pCur;
1028 for (pCur = pVM->pUVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
1029 {
1030 /* Skip anything which isn't in GC. */
1031 if (pCur->eType != PDMMOD_TYPE_RC)
1032 continue;
1033 if (uPC - pCur->ImageBase < RTLdrSize(pCur->hLdrMod))
1034 {
1035 if (pMod)
1036 *pMod = pCur->ImageBase;
1037 if (pszModName && cchModName)
1038 {
1039 *pszModName = '\0';
1040 strncat(pszModName, pCur->szName, cchModName);
1041 }
1042 if (pNearSym1) *pNearSym1 = 0;
1043 if (pNearSym2) *pNearSym2 = 0;
1044 if (pszNearSym1) *pszNearSym1 = '\0';
1045 if (pszNearSym2) *pszNearSym2 = '\0';
1046
1047 /*
1048 * Locate the nearest symbols.
1049 */
1050 QMFEIPARG Args;
1051 Args.uPC = uPC;
1052 Args.pszNearSym1 = pszNearSym1;
1053 Args.cchNearSym1 = cchNearSym1;
1054 Args.offNearSym1 = RTRCINTPTR_MIN;
1055 Args.pszNearSym2 = pszNearSym2;
1056 Args.cchNearSym2 = cchNearSym2;
1057 Args.offNearSym2 = RTRCINTPTR_MAX;
1058
1059 rc = RTLdrEnumSymbols(pCur->hLdrMod, RTLDR_ENUM_SYMBOL_FLAGS_ALL, pCur->pvBits, pCur->ImageBase,
1060 pdmR3QueryModFromEIPEnumSymbols, &Args);
1061 if (pNearSym1 && Args.offNearSym1 != INT_MIN)
1062 *pNearSym1 = Args.offNearSym1 + uPC;
1063 if (pNearSym2 && Args.offNearSym2 != INT_MAX)
1064 *pNearSym2 = Args.offNearSym2 + uPC;
1065
1066 rc = VINF_SUCCESS;
1067 if (pCur->eType == PDMMOD_TYPE_RC)
1068 break;
1069 }
1070
1071 }
1072 return rc;
1073}
1074
1075
1076/**
1077 * Enumeration callback function used by RTLdrEnumSymbols().
1078 *
1079 * @returns VBox status code. Failure will stop the enumeration.
1080 * @param hLdrMod The loader module handle.
1081 * @param pszSymbol Symbol name. NULL if ordinal only.
1082 * @param uSymbol Symbol ordinal, ~0 if not used.
1083 * @param Value Symbol value.
1084 * @param pvUser The user argument specified to RTLdrEnumSymbols().
1085 */
1086static DECLCALLBACK(int) pdmR3QueryModFromEIPEnumSymbols(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
1087{
1088 PQMFEIPARG pArgs = (PQMFEIPARG)pvUser;
1089
1090 RTINTPTR off = Value - pArgs->uPC;
1091 if (off <= 0) /* near1 is before or at same location. */
1092 {
1093 if (off > pArgs->offNearSym1)
1094 {
1095 pArgs->offNearSym1 = off;
1096 if (pArgs->pszNearSym1 && pArgs->cchNearSym1)
1097 {
1098 *pArgs->pszNearSym1 = '\0';
1099 if (pszSymbol)
1100 strncat(pArgs->pszNearSym1, pszSymbol, pArgs->cchNearSym1);
1101 else
1102 {
1103 char szOrd[32];
1104 RTStrPrintf(szOrd, sizeof(szOrd), "#%#x", uSymbol);
1105 strncat(pArgs->pszNearSym1, szOrd, pArgs->cchNearSym1);
1106 }
1107 }
1108 }
1109 }
1110 else /* near2 is after */
1111 {
1112 if (off < pArgs->offNearSym2)
1113 {
1114 pArgs->offNearSym2 = off;
1115 if (pArgs->pszNearSym2 && pArgs->cchNearSym2)
1116 {
1117 *pArgs->pszNearSym2 = '\0';
1118 if (pszSymbol)
1119 strncat(pArgs->pszNearSym2, pszSymbol, pArgs->cchNearSym2);
1120 else
1121 {
1122 char szOrd[32];
1123 RTStrPrintf(szOrd, sizeof(szOrd), "#%#x", uSymbol);
1124 strncat(pArgs->pszNearSym2, szOrd, pArgs->cchNearSym2);
1125 }
1126 }
1127 }
1128 }
1129
1130 return VINF_SUCCESS;
1131}
1132
1133
1134/**
1135 * Enumerate all PDM modules.
1136 *
1137 * @returns VBox status.
1138 * @param pVM VM Handle.
1139 * @param pfnCallback Function to call back for each of the modules.
1140 * @param pvArg User argument.
1141 */
1142VMMR3DECL(int) PDMR3LdrEnumModules(PVM pVM, PFNPDMR3ENUM pfnCallback, void *pvArg)
1143{
1144 PPDMMOD pCur;
1145 for (pCur = pVM->pUVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
1146 {
1147 int rc = pfnCallback(pVM,
1148 pCur->szFilename,
1149 pCur->szName,
1150 pCur->ImageBase,
1151 pCur->eType == PDMMOD_TYPE_RC ? RTLdrSize(pCur->hLdrMod) : 0,
1152 pCur->eType == PDMMOD_TYPE_RC,
1153 pvArg);
1154 if (RT_FAILURE(rc))
1155 return rc;
1156 }
1157 return VINF_SUCCESS;
1158}
1159
Note: See TracBrowser for help on using the repository browser.

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