VirtualBox

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

Last change on this file since 37423 was 35346, checked in by vboxsync, 14 years ago

VMM reorg: Moving the public include files from include/VBox to include/VBox/vmm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 57.9 KB
Line 
1/* $Id: PDMLdr.cpp 35346 2010-12-27 16:13:13Z vboxsync $ */
2/** @file
3 * PDM - Pluggable Device Manager, module loader.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
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
18//#define PDMLDR_FAKE_MODE
19
20/*******************************************************************************
21* Header Files *
22*******************************************************************************/
23#define LOG_GROUP LOG_GROUP_PDM_LDR
24#include "PDMInternal.h"
25#include <VBox/vmm/pdm.h>
26#include <VBox/vmm/mm.h>
27#include <VBox/vmm/vmm.h>
28#include <VBox/vmm/vm.h>
29#include <VBox/vmm/uvm.h>
30#include <VBox/sup.h>
31#include <VBox/param.h>
32#include <VBox/err.h>
33#include <VBox/vmm/hwaccm.h>
34
35#include <VBox/log.h>
36#include <iprt/assert.h>
37#include <iprt/ctype.h>
38#include <iprt/file.h>
39#include <iprt/ldr.h>
40#include <iprt/mem.h>
41#include <iprt/path.h>
42#include <iprt/string.h>
43
44#include <limits.h>
45
46
47/*******************************************************************************
48* Structures and Typedefs *
49*******************************************************************************/
50/**
51 * Structure which the user argument of the RTLdrGetBits() callback points to.
52 * @internal
53 */
54typedef struct PDMGETIMPORTARGS
55{
56 PVM pVM;
57 PPDMMOD pModule;
58} PDMGETIMPORTARGS, *PPDMGETIMPORTARGS;
59
60
61/*******************************************************************************
62* Internal Functions *
63*******************************************************************************/
64static DECLCALLBACK(int) pdmR3GetImportRC(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser);
65static int pdmR3LoadR0U(PUVM pUVM, const char *pszFilename, const char *pszName, const char *pszSearchPath);
66static char *pdmR3FileRC(const char *pszFile, const char *pszSearchPath);
67static char *pdmR3FileR0(const char *pszFile, const char *pszSearchPath);
68static char *pdmR3File(const char *pszFile, const char *pszDefaultExt, const char *pszSearchPath, bool fShared);
69
70
71
72/**
73 * Loads the VMMR0.r0 module early in the init process.
74 *
75 * @returns VBox status code.
76 * @param pUVM Pointer to the user mode VM structure.
77 */
78VMMR3DECL(int) PDMR3LdrLoadVMMR0U(PUVM pUVM)
79{
80 return pdmR3LoadR0U(pUVM, NULL, VMMR0_MAIN_MODULE_NAME, NULL);
81}
82
83
84/**
85 * Init the module loader part of PDM.
86 *
87 * This routine will load the Host Context Ring-0 and Guest
88 * Context VMM modules.
89 *
90 * @returns VBox status code.
91 * @param pUVM Pointer to the user mode VM structure.
92 * @param pvVMMR0Mod The opaque returned by PDMR3LdrLoadVMMR0.
93 */
94int pdmR3LdrInitU(PUVM pUVM)
95{
96#if defined(PDMLDR_FAKE_MODE) || !defined(VBOX_WITH_RAW_MODE)
97 return VINF_SUCCESS;
98
99#else
100
101 /*
102 * Load the mandatory GC module, the VMMR0.r0 is loaded before VM creation.
103 */
104 return PDMR3LdrLoadRC(pUVM->pVM, NULL, VMMGC_MAIN_MODULE_NAME);
105#endif
106}
107
108
109/**
110 * Terminate the module loader part of PDM.
111 *
112 * This will unload and free all modules.
113 *
114 * @param pVM The VM handle.
115 *
116 * @remarks This is normally called twice during termination.
117 */
118void pdmR3LdrTermU(PUVM pUVM)
119{
120 /*
121 * Free the modules.
122 */
123 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
124 PPDMMOD pModule = pUVM->pdm.s.pModules;
125 pUVM->pdm.s.pModules = NULL;
126 while (pModule)
127 {
128 /* free loader item. */
129 if (pModule->hLdrMod != NIL_RTLDRMOD)
130 {
131 int rc2 = RTLdrClose(pModule->hLdrMod);
132 AssertRC(rc2);
133 pModule->hLdrMod = NIL_RTLDRMOD;
134 }
135
136 /* free bits. */
137 switch (pModule->eType)
138 {
139 case PDMMOD_TYPE_R0:
140 {
141 Assert(pModule->ImageBase);
142 int rc2 = SUPR3FreeModule((void *)(uintptr_t)pModule->ImageBase);
143 AssertRC(rc2);
144 pModule->ImageBase = 0;
145 break;
146 }
147
148#ifdef VBOX_WITH_RAW_MODE
149 case PDMMOD_TYPE_RC:
150#endif
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 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
166}
167
168
169/**
170 * Applies relocations to GC modules.
171 *
172 * This must be done very early in the relocation
173 * process so that components can resolve GC symbols during relocation.
174 *
175 * @param pUVM Pointer to the user mode VM structure.
176 * @param offDelta Relocation delta relative to old location.
177 */
178VMMR3DECL(void) PDMR3LdrRelocateU(PUVM pUVM, RTGCINTPTR offDelta)
179{
180#ifdef VBOX_WITH_RAW_MODE
181 LogFlow(("PDMR3LdrRelocate: offDelta=%RGv\n", offDelta));
182
183 /*
184 * GC Modules.
185 */
186 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
187 if (pUVM->pdm.s.pModules)
188 {
189 /*
190 * The relocation have to be done in two passes so imports
191 * can be correctly resolved. The first pass will update
192 * the ImageBase saving the current value in OldImageBase.
193 * The second pass will do the actual relocation.
194 */
195 /* pass 1 */
196 PPDMMOD pCur;
197 for (pCur = pUVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
198 {
199 if (pCur->eType == PDMMOD_TYPE_RC)
200 {
201 pCur->OldImageBase = pCur->ImageBase;
202 pCur->ImageBase = MMHyperR3ToRC(pUVM->pVM, pCur->pvBits);
203 }
204 }
205
206 /* pass 2 */
207 for (pCur = pUVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
208 {
209 if (pCur->eType == PDMMOD_TYPE_RC)
210 {
211 PDMGETIMPORTARGS Args;
212 Args.pVM = pUVM->pVM;
213 Args.pModule = pCur;
214 int rc = RTLdrRelocate(pCur->hLdrMod, pCur->pvBits, pCur->ImageBase, pCur->OldImageBase,
215 pdmR3GetImportRC, &Args);
216 AssertFatalMsgRC(rc, ("RTLdrRelocate failed, rc=%d\n", rc));
217 DBGFR3ModuleRelocate(pUVM->pVM, pCur->OldImageBase, pCur->ImageBase, RTLdrSize(pCur->hLdrMod),
218 pCur->szFilename, pCur->szName);
219 }
220 }
221 }
222 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
223#endif
224}
225
226
227/**
228 * Loads a module into the host context ring-3.
229 *
230 * This is used by the driver and device init functions to load modules
231 * containing the drivers and devices. The function can be extended to
232 * load modules which are not native to the environment we're running in,
233 * but at the moment this is not required.
234 *
235 * No reference counting is kept, since we don't implement any facilities
236 * for unloading the module. But the module will naturally be released
237 * when the VM terminates.
238 *
239 * @returns VBox status code.
240 * @param pUVM Pointer to the user mode VM structure.
241 * @param pszFilename Filename of the module binary.
242 * @param pszName Module name. Case sensitive and the length is limited!
243 */
244int pdmR3LoadR3U(PUVM pUVM, const char *pszFilename, const char *pszName)
245{
246 /*
247 * Validate input.
248 */
249 AssertMsg(pUVM->pVM->pdm.s.offVM, ("bad init order!\n"));
250 Assert(pszFilename);
251 size_t cchFilename = strlen(pszFilename);
252 Assert(pszName);
253 size_t cchName = strlen(pszName);
254 PPDMMOD pCur;
255 if (cchName >= sizeof(pCur->szName))
256 {
257 AssertMsgFailed(("Name is too long, cchName=%d pszName='%s'\n", cchName, pszName));
258 return VERR_INVALID_PARAMETER;
259 }
260
261 /*
262 * Try lookup the name and see if the module exists.
263 */
264 int rc;
265 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
266 for (pCur = pUVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
267 {
268 if (!strcmp(pCur->szName, pszName))
269 {
270 if (pCur->eType == PDMMOD_TYPE_R3)
271 rc = VINF_PDM_ALREADY_LOADED;
272 else
273 rc = VERR_PDM_MODULE_NAME_CLASH;
274 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
275
276 AssertMsgRC(rc, ("We've already got a module '%s' loaded!\n", pszName));
277 return rc;
278 }
279 }
280
281 /*
282 * Allocate the module list node and initialize it.
283 */
284 const char *pszSuff = RTLdrGetSuff();
285 size_t cchSuff = RTPathHaveExt(pszFilename) ? 0 : strlen(pszSuff);
286 PPDMMOD pModule = (PPDMMOD)RTMemAllocZ(RT_OFFSETOF(PDMMOD, szFilename[cchFilename + cchSuff + 1]));
287 if (pModule)
288 {
289 pModule->eType = PDMMOD_TYPE_R3;
290 memcpy(pModule->szName, pszName, cchName); /* memory is zero'd, no need to copy terminator :-) */
291 memcpy(pModule->szFilename, pszFilename, cchFilename);
292 memcpy(&pModule->szFilename[cchFilename], pszSuff, cchSuff);
293
294 /*
295 * Load the loader item.
296 */
297 RTERRINFOSTATIC ErrInfo;
298 RTErrInfoInitStatic(&ErrInfo);
299 rc = SUPR3HardenedLdrLoadPlugIn(pModule->szFilename, &pModule->hLdrMod, &ErrInfo.Core);
300 if (RT_SUCCESS(rc))
301 {
302 pModule->pNext = pUVM->pdm.s.pModules;
303 pUVM->pdm.s.pModules = pModule;
304 }
305 else
306 {
307 /* Something went wrong, most likely module not found. Don't consider other unlikely errors */
308 rc = VMSetError(pUVM->pVM, rc, RT_SRC_POS,
309 N_("Unable to load R3 module %s (%s): %s"), pModule->szFilename, pszName, ErrInfo.Core.pszMsg);
310 RTMemFree(pModule);
311 }
312 }
313 else
314 rc = VERR_NO_MEMORY;
315
316 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
317 return rc;
318}
319
320
321#ifdef VBOX_WITH_RAW_MODE
322/**
323 * Resolve an external symbol during RTLdrGetBits() of a RC module.
324 *
325 * @returns VBox status code.
326 * @param hLdrMod The loader module handle.
327 * @param pszModule Module name.
328 * @param pszSymbol Symbol name, NULL if uSymbol should be used.
329 * @param uSymbol Symbol ordinal, ~0 if pszSymbol should be used.
330 * @param pValue Where to store the symbol value (address).
331 * @param pvUser User argument.
332 */
333static DECLCALLBACK(int) pdmR3GetImportRC(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser)
334{
335 PVM pVM = ((PPDMGETIMPORTARGS)pvUser)->pVM;
336 PPDMMOD pModule = ((PPDMGETIMPORTARGS)pvUser)->pModule;
337
338 /*
339 * Adjust input.
340 */
341 if (pszModule && !*pszModule)
342 pszModule = NULL;
343
344 /*
345 * Builtin module.
346 */
347 if (!pszModule || !strcmp(pszModule, "VMMRCBuiltin.rc"))
348 {
349 int rc = VINF_SUCCESS;
350 if (!strcmp(pszSymbol, "g_VM"))
351 *pValue = pVM->pVMRC;
352 else if (!strcmp(pszSymbol, "g_CPUM"))
353 *pValue = VM_RC_ADDR(pVM, &pVM->cpum);
354 else if (!strcmp(pszSymbol, "g_TRPM"))
355 *pValue = VM_RC_ADDR(pVM, &pVM->trpm);
356 else if (!strcmp(pszSymbol, "g_TRPMCPU"))
357 *pValue = VM_RC_ADDR(pVM, &pVM->aCpus[0].trpm);
358 else if ( !strncmp(pszSymbol, "VMM", 3)
359 || !strcmp(pszSymbol, "g_Logger")
360 || !strcmp(pszSymbol, "g_RelLogger"))
361 {
362 RTRCPTR RCPtr = 0;
363 rc = VMMR3GetImportRC(pVM, pszSymbol, &RCPtr);
364 if (RT_SUCCESS(rc))
365 *pValue = RCPtr;
366 }
367 else if ( !strncmp(pszSymbol, "TM", 2)
368 || !strcmp(pszSymbol, "g_pSUPGlobalInfoPage"))
369 {
370 RTRCPTR RCPtr = 0;
371 rc = TMR3GetImportRC(pVM, pszSymbol, &RCPtr);
372 if (RT_SUCCESS(rc))
373 *pValue = RCPtr;
374 }
375 else
376 {
377 AssertMsg(!pszModule, ("Unknown builtin symbol '%s' for module '%s'!\n", pszSymbol, pModule->szName)); NOREF(pModule);
378 rc = VERR_SYMBOL_NOT_FOUND;
379 }
380 if (RT_SUCCESS(rc) || pszModule)
381 {
382 if (RT_FAILURE(rc))
383 LogRel(("PDMLdr: Couldn't find symbol '%s' in module '%s'!\n", pszSymbol, pszModule));
384 return rc;
385 }
386 }
387
388 /*
389 * Search for module.
390 */
391 PUVM pUVM = pVM->pUVM;
392 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
393 PPDMMOD pCur = pUVM->pdm.s.pModules;
394 while (pCur)
395 {
396 if ( pCur->eType == PDMMOD_TYPE_RC
397 && ( !pszModule
398 || !strcmp(pCur->szName, pszModule))
399 )
400 {
401 /* Search for the symbol. */
402 int rc = RTLdrGetSymbolEx(pCur->hLdrMod, pCur->pvBits, pCur->ImageBase, pszSymbol, pValue);
403 if (RT_SUCCESS(rc))
404 {
405 AssertMsg(*pValue - pCur->ImageBase < RTLdrSize(pCur->hLdrMod),
406 ("%RRv-%RRv %s %RRv\n", (RTRCPTR)pCur->ImageBase,
407 (RTRCPTR)(pCur->ImageBase + RTLdrSize(pCur->hLdrMod) - 1),
408 pszSymbol, (RTRCPTR)*pValue));
409 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
410 return rc;
411 }
412 if (pszModule)
413 {
414 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
415 AssertLogRelMsgFailed(("PDMLdr: Couldn't find symbol '%s' in module '%s'!\n", pszSymbol, pszModule));
416 return VERR_SYMBOL_NOT_FOUND;
417 }
418 }
419
420 /* next */
421 pCur = pCur->pNext;
422 }
423
424 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
425 AssertLogRelMsgFailed(("Couldn't find module '%s' for resolving symbol '%s'!\n", pszModule, pszSymbol));
426 return VERR_SYMBOL_NOT_FOUND;
427}
428
429
430/**
431 * Loads a module into the guest context (i.e. into the Hypervisor memory region).
432 *
433 * @returns VBox status code.
434 * @param pVM The VM to load it into.
435 * @param pszFilename Filename of the module binary.
436 * @param pszName Module name. Case sensitive and the length is limited!
437 */
438VMMR3DECL(int) PDMR3LdrLoadRC(PVM pVM, const char *pszFilename, const char *pszName)
439{
440 /*
441 * Validate input.
442 */
443 AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
444 PUVM pUVM = pVM->pUVM;
445 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
446 PPDMMOD pCur = pUVM->pdm.s.pModules;
447 while (pCur)
448 {
449 if (!strcmp(pCur->szName, pszName))
450 {
451 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
452 AssertMsgFailed(("We've already got a module '%s' loaded!\n", pszName));
453 return VERR_PDM_MODULE_NAME_CLASH;
454 }
455 /* next */
456 pCur = pCur->pNext;
457 }
458
459 /*
460 * Find the file if not specified.
461 */
462 char *pszFile = NULL;
463 if (!pszFilename)
464 pszFilename = pszFile = pdmR3FileRC(pszName, NULL);
465
466 /*
467 * Allocate the module list node.
468 */
469 PPDMMOD pModule = (PPDMMOD)RTMemAllocZ(sizeof(*pModule) + strlen(pszFilename));
470 if (!pModule)
471 {
472 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
473 RTMemTmpFree(pszFile);
474 return VERR_NO_MEMORY;
475 }
476 AssertMsg(strlen(pszName) + 1 < sizeof(pModule->szName),
477 ("pazName is too long (%d chars) max is %d chars.\n", strlen(pszName), sizeof(pModule->szName) - 1));
478 strcpy(pModule->szName, pszName);
479 pModule->eType = PDMMOD_TYPE_RC;
480 strcpy(pModule->szFilename, pszFilename);
481
482
483 /*
484 * Open the loader item.
485 */
486 RTERRINFOSTATIC ErrInfo;
487 RTErrInfoInitStatic(&ErrInfo);
488 int rc = SUPR3HardenedVerifyPlugIn(pszFilename, &ErrInfo.Core);
489 if (RT_SUCCESS(rc))
490 {
491 RTErrInfoClear(&ErrInfo.Core);
492 rc = RTLdrOpen(pszFilename, 0, RTLDRARCH_X86_32, &pModule->hLdrMod);
493 }
494 if (RT_SUCCESS(rc))
495 {
496 /*
497 * Allocate space in the hypervisor.
498 */
499 size_t cb = RTLdrSize(pModule->hLdrMod);
500 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
501 uint32_t cPages = (uint32_t)(cb >> PAGE_SHIFT);
502 if (((size_t)cPages << PAGE_SHIFT) == cb)
503 {
504 PSUPPAGE paPages = (PSUPPAGE)RTMemTmpAlloc(cPages * sizeof(paPages[0]));
505 if (paPages)
506 {
507 rc = SUPR3PageAllocEx(cPages, 0 /*fFlags*/, &pModule->pvBits, NULL /*pR0Ptr*/, paPages);
508 if (RT_SUCCESS(rc))
509 {
510 RTGCPTR GCPtr;
511 rc = MMR3HyperMapPages(pVM, pModule->pvBits, NIL_RTR0PTR,
512 cPages, paPages, pModule->szName, &GCPtr);
513 if (RT_SUCCESS(rc))
514 {
515 MMR3HyperReserve(pVM, PAGE_SIZE, "fence", NULL);
516
517 /*
518 * Get relocated image bits.
519 */
520 Assert(MMHyperR3ToRC(pVM, pModule->pvBits) == GCPtr);
521 pModule->ImageBase = GCPtr;
522 PDMGETIMPORTARGS Args;
523 Args.pVM = pVM;
524 Args.pModule = pModule;
525 rc = RTLdrGetBits(pModule->hLdrMod, pModule->pvBits, pModule->ImageBase, pdmR3GetImportRC, &Args);
526 if (RT_SUCCESS(rc))
527 {
528 /*
529 * Insert the module.
530 */
531 if (pUVM->pdm.s.pModules)
532 {
533 /* we don't expect this list to be very long, so rather save the tail pointer. */
534 pCur = pUVM->pdm.s.pModules;
535 while (pCur->pNext)
536 pCur = pCur->pNext;
537 pCur->pNext = pModule;
538 }
539 else
540 pUVM->pdm.s.pModules = pModule; /* (pNext is zeroed by alloc) */
541 Log(("PDM: RC Module at %RRv %s (%s)\n", (RTRCPTR)pModule->ImageBase, pszName, pszFilename));
542 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
543 RTMemTmpFree(pszFile);
544 RTMemTmpFree(paPages);
545 return VINF_SUCCESS;
546 }
547 }
548 else
549 {
550 AssertRC(rc);
551 SUPR3PageFreeEx(pModule->pvBits, cPages);
552 }
553 }
554 else
555 AssertMsgFailed(("SUPR3PageAlloc(%d,) -> %Rrc\n", cPages, rc));
556 RTMemTmpFree(paPages);
557 }
558 else
559 rc = VERR_NO_TMP_MEMORY;
560 }
561 else
562 rc = VERR_OUT_OF_RANGE;
563 int rc2 = RTLdrClose(pModule->hLdrMod);
564 AssertRC(rc2);
565 }
566 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
567
568 /* Don't consider VERR_PDM_MODULE_NAME_CLASH and VERR_NO_MEMORY above as these are very unlikely. */
569 if (RT_FAILURE(rc) && RTErrInfoIsSet(&ErrInfo.Core))
570 rc = VMSetError(pVM, rc, RT_SRC_POS, N_("Cannot load RC module %s: %s"), pszFilename, ErrInfo.Core.pszMsg);
571 else if (RT_FAILURE(rc))
572 rc = VMSetError(pVM, rc, RT_SRC_POS, N_("Cannot load RC module %s"), pszFilename);
573
574 RTMemFree(pModule);
575 RTMemTmpFree(pszFile);
576 return rc;
577}
578#endif /* VBOX_WITH_RAW_MODE */
579
580
581/**
582 * Loads a module into the ring-0 context.
583 *
584 * @returns VBox status code.
585 * @param pUVM Pointer to the user mode VM structure.
586 * @param pszFilename Filename of the module binary.
587 * @param pszName Module name. Case sensitive and the length is limited!
588 * @param pszSearchPath List of directories to search if @a pszFilename is
589 * not specified. Can be NULL, in which case the arch
590 * dependent install dir is searched.
591 */
592static int pdmR3LoadR0U(PUVM pUVM, const char *pszFilename, const char *pszName, const char *pszSearchPath)
593{
594 /*
595 * Validate input.
596 */
597 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
598 PPDMMOD pCur = pUVM->pdm.s.pModules;
599 while (pCur)
600 {
601 if (!strcmp(pCur->szName, pszName))
602 {
603 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
604 AssertMsgFailed(("We've already got a module '%s' loaded!\n", pszName));
605 return VERR_PDM_MODULE_NAME_CLASH;
606 }
607 /* next */
608 pCur = pCur->pNext;
609 }
610
611 /*
612 * Find the file if not specified.
613 */
614 char *pszFile = NULL;
615 if (!pszFilename)
616 pszFilename = pszFile = pdmR3FileR0(pszName, pszSearchPath);
617
618 /*
619 * Allocate the module list node.
620 */
621 PPDMMOD pModule = (PPDMMOD)RTMemAllocZ(sizeof(*pModule) + strlen(pszFilename));
622 if (!pModule)
623 {
624 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
625 RTMemTmpFree(pszFile);
626 return VERR_NO_MEMORY;
627 }
628 AssertMsg(strlen(pszName) + 1 < sizeof(pModule->szName),
629 ("pazName is too long (%d chars) max is %d chars.\n", strlen(pszName), sizeof(pModule->szName) - 1));
630 strcpy(pModule->szName, pszName);
631 pModule->eType = PDMMOD_TYPE_R0;
632 strcpy(pModule->szFilename, pszFilename);
633
634 /*
635 * Ask the support library to load it.
636 */
637 void *pvImageBase;
638 RTERRINFOSTATIC ErrInfo;
639 RTErrInfoInitStatic(&ErrInfo);
640 int rc = SUPR3LoadModule(pszFilename, pszName, &pvImageBase, &ErrInfo.Core);
641 if (RT_SUCCESS(rc))
642 {
643 pModule->hLdrMod = NIL_RTLDRMOD;
644 pModule->ImageBase = (uintptr_t)pvImageBase;
645
646 /*
647 * Insert the module.
648 */
649 if (pUVM->pdm.s.pModules)
650 {
651 /* we don't expect this list to be very long, so rather save the tail pointer. */
652 pCur = pUVM->pdm.s.pModules;
653 while (pCur->pNext)
654 pCur = pCur->pNext;
655 pCur->pNext = pModule;
656 }
657 else
658 pUVM->pdm.s.pModules = pModule; /* (pNext is zeroed by alloc) */
659 Log(("PDM: R0 Module at %RHv %s (%s)\n", (RTR0PTR)pModule->ImageBase, pszName, pszFilename));
660 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
661 RTMemTmpFree(pszFile);
662 return VINF_SUCCESS;
663 }
664
665 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
666 RTMemFree(pModule);
667 LogRel(("pdmR3LoadR0U: pszName=\"%s\" rc=%Rrc szErr=\"%s\"\n", pszName, rc, ErrInfo.Core.pszMsg));
668
669 /* Don't consider VERR_PDM_MODULE_NAME_CLASH and VERR_NO_MEMORY above as these are very unlikely. */
670 if (RT_FAILURE(rc) && pUVM->pVM) /** @todo VMR3SetErrorU. */
671 rc = VMSetError(pUVM->pVM, rc, RT_SRC_POS, N_("Cannot load R0 module %s: %s"), pszFilename, ErrInfo.Core.pszMsg);
672
673 RTMemTmpFree(pszFile); /* might be reference thru pszFilename in the above VMSetError call. */
674 return rc;
675}
676
677
678
679/**
680 * Get the address of a symbol in a given HC ring 3 module.
681 *
682 * @returns VBox status code.
683 * @param pVM VM handle.
684 * @param pszModule Module name.
685 * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
686 * ordinal value rather than a string pointer.
687 * @param ppvValue Where to store the symbol value.
688 */
689VMMR3DECL(int) PDMR3LdrGetSymbolR3(PVM pVM, const char *pszModule, const char *pszSymbol, void **ppvValue)
690{
691 /*
692 * Validate input.
693 */
694 AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
695
696 /*
697 * Find the module.
698 */
699 PUVM pUVM = pVM->pUVM;
700 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
701 for (PPDMMOD pModule = pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
702 {
703 if ( pModule->eType == PDMMOD_TYPE_R3
704 && !strcmp(pModule->szName, pszModule))
705 {
706 RTUINTPTR Value = 0;
707 int rc = RTLdrGetSymbolEx(pModule->hLdrMod, pModule->pvBits, pModule->ImageBase, pszSymbol, &Value);
708 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
709 if (RT_SUCCESS(rc))
710 {
711 *ppvValue = (void *)(uintptr_t)Value;
712 Assert((uintptr_t)*ppvValue == Value);
713 }
714 else
715 {
716 if ((uintptr_t)pszSymbol < 0x10000)
717 AssertMsg(rc, ("Couldn't symbol '%u' in module '%s'\n", (unsigned)(uintptr_t)pszSymbol, pszModule));
718 else
719 AssertMsg(rc, ("Couldn't symbol '%s' in module '%s'\n", pszSymbol, pszModule));
720 }
721 return rc;
722 }
723 }
724 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
725 AssertMsgFailed(("Couldn't locate module '%s'\n", pszModule));
726 return VERR_SYMBOL_NOT_FOUND;
727}
728
729
730/**
731 * Get the address of a symbol in a given HC ring 0 module.
732 *
733 * @returns VBox status code.
734 * @param pVM VM handle.
735 * @param pszModule Module name. If NULL the main R0 module (VMMR0.r0) is assumes.
736 * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
737 * ordinal value rather than a string pointer.
738 * @param ppvValue Where to store the symbol value.
739 */
740VMMR3DECL(int) PDMR3LdrGetSymbolR0(PVM pVM, const char *pszModule, const char *pszSymbol, PRTR0PTR ppvValue)
741{
742#ifdef PDMLDR_FAKE_MODE
743 *ppvValue = 0xdeadbeef;
744 return VINF_SUCCESS;
745
746#else
747 /*
748 * Validate input.
749 */
750 AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
751 if (!pszModule)
752 pszModule = "VMMR0.r0";
753
754 /*
755 * Find the module.
756 */
757 PUVM pUVM = pVM->pUVM;
758 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
759 for (PPDMMOD pModule = pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
760 {
761 if ( pModule->eType == PDMMOD_TYPE_R0
762 && !strcmp(pModule->szName, pszModule))
763 {
764 int rc = SUPR3GetSymbolR0((void *)(uintptr_t)pModule->ImageBase, pszSymbol, (void **)ppvValue);
765 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
766 if (RT_FAILURE(rc))
767 {
768 AssertMsgRC(rc, ("Couldn't find symbol '%s' in module '%s'\n", pszSymbol, pszModule));
769 LogRel(("PDMGetSymbol: Couldn't find symbol '%s' in module '%s'\n", pszSymbol, pszModule));
770 }
771 return rc;
772 }
773 }
774 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
775 AssertMsgFailed(("Couldn't locate module '%s'\n", pszModule));
776 return VERR_SYMBOL_NOT_FOUND;
777#endif
778}
779
780
781/**
782 * Same as PDMR3LdrGetSymbolR0 except that the module will be attempted loaded if not found.
783 *
784 * @returns VBox status code.
785 * @param pVM VM handle.
786 * @param pszModule Module name. If NULL the main R0 module (VMMR0.r0) is assumed.
787 * @param pszSearchPath List of directories to search if @a pszFile is
788 * not qualified with a path. Can be NULL, in which
789 * case the arch dependent install dir is searched.
790 * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
791 * ordinal value rather than a string pointer.
792 * @param ppvValue Where to store the symbol value.
793 */
794VMMR3DECL(int) PDMR3LdrGetSymbolR0Lazy(PVM pVM, const char *pszModule, const char *pszSearchPath, const char *pszSymbol,
795 PRTR0PTR ppvValue)
796{
797#ifdef PDMLDR_FAKE_MODE
798 *ppvValue = 0xdeadbeef;
799 return VINF_SUCCESS;
800
801#else
802 /*
803 * Since we're lazy, we'll only check if the module is present
804 * and hand it over to PDMR3LdrGetSymbolR0 when that's done.
805 */
806 AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
807 if (pszModule)
808 {
809 AssertMsgReturn(!strpbrk(pszModule, "/\\:\n\r\t"), ("pszModule=%s\n", pszModule), VERR_INVALID_PARAMETER);
810 PUVM pUVM = pVM->pUVM;
811 PPDMMOD pModule;
812 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
813 for (pModule = pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
814 if ( pModule->eType == PDMMOD_TYPE_R0
815 && !strcmp(pModule->szName, pszModule))
816 break;
817 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
818 if (!pModule)
819 {
820 int rc = pdmR3LoadR0U(pUVM, NULL, pszModule, pszSearchPath);
821 AssertMsgRCReturn(rc, ("pszModule=%s rc=%Rrc\n", pszModule, rc), VERR_MODULE_NOT_FOUND);
822 }
823 }
824 return PDMR3LdrGetSymbolR0(pVM, pszModule, pszSymbol, ppvValue);
825#endif
826}
827
828
829/**
830 * Get the address of a symbol in a given RC module.
831 *
832 * @returns VBox status code.
833 * @param pVM VM handle.
834 * @param pszModule Module name. If NULL the main R0 module (VMMGC.gc) is assumes.
835 * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
836 * ordinal value rather than a string pointer.
837 * @param pRCPtrValue Where to store the symbol value.
838 */
839VMMR3DECL(int) PDMR3LdrGetSymbolRC(PVM pVM, const char *pszModule, const char *pszSymbol, PRTRCPTR pRCPtrValue)
840{
841#if defined(PDMLDR_FAKE_MODE) || !defined(VBOX_WITH_RAW_MODE)
842 *pRCPtrValue = 0xfeedf00d;
843 return VINF_SUCCESS;
844
845#else
846 /*
847 * Validate input.
848 */
849 AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
850 if (!pszModule)
851 pszModule = "VMMGC.gc";
852
853 /*
854 * Find the module.
855 */
856 PUVM pUVM = pVM->pUVM;
857 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
858 for (PPDMMOD pModule = pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
859 {
860 if ( pModule->eType == PDMMOD_TYPE_RC
861 && !strcmp(pModule->szName, pszModule))
862 {
863 RTUINTPTR Value;
864 int rc = RTLdrGetSymbolEx(pModule->hLdrMod, pModule->pvBits, pModule->ImageBase, pszSymbol, &Value);
865 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
866 if (RT_SUCCESS(rc))
867 {
868 *pRCPtrValue = (RTGCPTR)Value;
869 Assert(*pRCPtrValue == Value);
870 }
871 else
872 {
873 if ((uintptr_t)pszSymbol < 0x10000)
874 AssertMsg(rc, ("Couldn't symbol '%u' in module '%s'\n", (unsigned)(uintptr_t)pszSymbol, pszModule));
875 else
876 AssertMsg(rc, ("Couldn't symbol '%s' in module '%s'\n", pszSymbol, pszModule));
877 }
878 return rc;
879 }
880 }
881 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
882 AssertMsgFailed(("Couldn't locate module '%s'\n", pszModule));
883 return VERR_SYMBOL_NOT_FOUND;
884#endif
885}
886
887
888/**
889 * Same as PDMR3LdrGetSymbolRC except that the module will be attempted loaded if not found.
890 *
891 * @returns VBox status code.
892 * @param pVM VM handle.
893 * @param pszModule Module name. If NULL the main R0 module (VMMGC.gc) is assumes.
894 * @param pszSearchPath List of directories to search if @a pszFile is
895 * not qualified with a path. Can be NULL, in which
896 * case the arch dependent install dir is searched.
897 * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
898 * ordinal value rather than a string pointer.
899 * @param pRCPtrValue Where to store the symbol value.
900 */
901VMMR3DECL(int) PDMR3LdrGetSymbolRCLazy(PVM pVM, const char *pszModule, const char *pszSearchPath, const char *pszSymbol,
902 PRTRCPTR pRCPtrValue)
903{
904#if defined(PDMLDR_FAKE_MODE) || !defined(VBOX_WITH_RAW_MODE)
905 *pRCPtrValue = 0xfeedf00d;
906 return VINF_SUCCESS;
907
908#else
909 /*
910 * Since we're lazy, we'll only check if the module is present
911 * and hand it over to PDMR3LdrGetSymbolRC when that's done.
912 */
913 AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
914 if (pszModule)
915 {
916 AssertMsgReturn(!strpbrk(pszModule, "/\\:\n\r\t"), ("pszModule=%s\n", pszModule), VERR_INVALID_PARAMETER);
917 PUVM pUVM = pVM->pUVM;
918 PPDMMOD pModule;
919 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
920 for (pModule = pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
921 if ( pModule->eType == PDMMOD_TYPE_RC
922 && !strcmp(pModule->szName, pszModule))
923 break;
924 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
925 if (!pModule)
926 {
927 char *pszFilename = pdmR3FileRC(pszModule, pszSearchPath);
928 AssertMsgReturn(pszFilename, ("pszModule=%s\n", pszModule), VERR_MODULE_NOT_FOUND);
929 int rc = PDMR3LdrLoadRC(pVM, pszFilename, pszModule);
930 RTMemTmpFree(pszFilename);
931 AssertMsgRCReturn(rc, ("pszModule=%s rc=%Rrc\n", pszModule, rc), VERR_MODULE_NOT_FOUND);
932 }
933 }
934 return PDMR3LdrGetSymbolRC(pVM, pszModule, pszSymbol, pRCPtrValue);
935#endif
936}
937
938
939/**
940 * Constructs the full filename for a R3 image file.
941 *
942 * @returns Pointer to temporary memory containing the filename.
943 * Caller must free this using RTMemTmpFree().
944 * @returns NULL on failure.
945 *
946 * @param pszFile File name (no path).
947 */
948char *pdmR3FileR3(const char *pszFile, bool fShared)
949{
950 return pdmR3File(pszFile, NULL, NULL, fShared);
951}
952
953
954/**
955 * Constructs the full filename for a R0 image file.
956 *
957 * @returns Pointer to temporary memory containing the filename.
958 * Caller must free this using RTMemTmpFree().
959 * @returns NULL on failure.
960 *
961 * @param pszFile File name (no path).
962 * @param pszSearchPath List of directories to search if @a pszFile is
963 * not qualified with a path. Can be NULL, in which
964 * case the arch dependent install dir is searched.
965 */
966char *pdmR3FileR0(const char *pszFile, const char *pszSearchPath)
967{
968 return pdmR3File(pszFile, NULL, pszSearchPath, /*fShared=*/false);
969}
970
971
972/**
973 * Constructs the full filename for a RC image file.
974 *
975 * @returns Pointer to temporary memory containing the filename.
976 * Caller must free this using RTMemTmpFree().
977 * @returns NULL on failure.
978 *
979 * @param pszFile File name (no path).
980 * @param pszSearchPath List of directories to search if @a pszFile is
981 * not qualified with a path. Can be NULL, in which
982 * case the arch dependent install dir is searched.
983 */
984char *pdmR3FileRC(const char *pszFile, const char *pszSearchPath)
985{
986 return pdmR3File(pszFile, NULL, pszSearchPath, /*fShared=*/false);
987}
988
989
990/**
991 * Worker for pdmR3File().
992 *
993 * @returns Pointer to temporary memory containing the filename.
994 * Caller must free this using RTMemTmpFree().
995 * @returns NULL on failure.
996 *
997 * @param pszDir Directory part
998 * @param pszFile File name part
999 * @param pszDefaultExt Extension part
1000 */
1001static char *pdmR3FileConstruct(const char *pszDir, const char *pszFile, const char *pszDefaultExt)
1002{
1003 /*
1004 * Allocate temp memory for return buffer.
1005 */
1006 size_t cchDir = strlen(pszDir);
1007 size_t cchFile = strlen(pszFile);
1008 size_t cchDefaultExt;
1009
1010 /*
1011 * Default extention?
1012 */
1013 if (!pszDefaultExt || strchr(pszFile, '.'))
1014 cchDefaultExt = 0;
1015 else
1016 cchDefaultExt = strlen(pszDefaultExt);
1017
1018 size_t cchPath = cchDir + 1 + cchFile + cchDefaultExt + 1;
1019 AssertMsgReturn(cchPath <= RTPATH_MAX, ("Path too long!\n"), NULL);
1020
1021 char *pszRet = (char *)RTMemTmpAlloc(cchDir + 1 + cchFile + cchDefaultExt + 1);
1022 AssertMsgReturn(pszRet, ("Out of temporary memory!\n"), NULL);
1023
1024 /*
1025 * Construct the filename.
1026 */
1027 memcpy(pszRet, pszDir, cchDir);
1028 pszRet[cchDir++] = '/'; /* this works everywhere */
1029 memcpy(pszRet + cchDir, pszFile, cchFile + 1);
1030 if (cchDefaultExt)
1031 memcpy(pszRet + cchDir + cchFile, pszDefaultExt, cchDefaultExt + 1);
1032
1033 return pszRet;
1034}
1035
1036
1037/**
1038 * Worker for pdmR3FileRC(), pdmR3FileR0() and pdmR3FileR3().
1039 *
1040 * @returns Pointer to temporary memory containing the filename.
1041 * Caller must free this using RTMemTmpFree().
1042 * @returns NULL on failure.
1043 * @param pszFile File name (no path).
1044 * @param pszDefaultExt The default extention, NULL if none.
1045 * @param pszSearchPath List of directories to search if @a pszFile is
1046 * not qualified with a path. Can be NULL, in which
1047 * case the arch dependent install dir is searched.
1048 * @param fShared If true, search in the shared directory (/usr/lib on Unix), else
1049 * search in the private directory (/usr/lib/virtualbox on Unix).
1050 * Ignored if VBOX_PATH_SHARED_LIBS is not defined.
1051 * @todo We'll have this elsewhere than in the root later!
1052 * @todo Remove the fShared hack again once we don't need to link against VBoxDD anymore!
1053 */
1054static char *pdmR3File(const char *pszFile, const char *pszDefaultExt, const char *pszSearchPath, bool fShared)
1055{
1056 char szPath[RTPATH_MAX];
1057 int rc;
1058
1059 AssertLogRelReturn(!fShared || !pszSearchPath, NULL);
1060 Assert(!RTPathHavePath(pszFile));
1061
1062 /*
1063 * If there is a path, search it.
1064 */
1065 if ( pszSearchPath
1066 && *pszSearchPath)
1067 {
1068 /* Check the filename length. */
1069 size_t const cchFile = strlen(pszFile);
1070 if (cchFile >= sizeof(szPath))
1071 return NULL;
1072
1073 /*
1074 * Walk the search path.
1075 */
1076 const char *psz = pszSearchPath;
1077 while (*psz)
1078 {
1079 /* Skip leading blanks - no directories with leading spaces, thank you. */
1080 while (RT_C_IS_BLANK(*psz))
1081 psz++;
1082
1083 /* Find the end of this element. */
1084 const char *pszNext;
1085 const char *pszEnd = strchr(psz, ';');
1086 if (!pszEnd)
1087 pszEnd = pszNext = strchr(psz, '\0');
1088 else
1089 pszNext = pszEnd + 1;
1090 if (pszEnd != psz)
1091 {
1092 rc = RTPathJoinEx(szPath, sizeof(szPath), psz, pszEnd - psz, pszFile, cchFile);
1093 if (RT_SUCCESS(rc))
1094 {
1095 if (RTFileExists(szPath))
1096 {
1097 size_t cchPath = strlen(szPath) + 1;
1098 char *pszRet = (char *)RTMemTmpAlloc(cchPath);
1099 if (pszRet)
1100 memcpy(pszRet, szPath, cchPath);
1101 return pszRet;
1102 }
1103 }
1104 }
1105
1106 /* advance */
1107 psz = pszNext;
1108 }
1109 }
1110
1111 /*
1112 * Use the default location.
1113 */
1114 rc = fShared
1115 ? RTPathSharedLibs( szPath, sizeof(szPath))
1116 : RTPathAppPrivateArch(szPath, sizeof(szPath));
1117 if (!RT_SUCCESS(rc))
1118 {
1119 AssertMsgFailed(("RTPath[SharedLibs|AppPrivateArch](,%d) failed rc=%d!\n", sizeof(szPath), rc));
1120 return NULL;
1121 }
1122
1123 return pdmR3FileConstruct(szPath, pszFile, pszDefaultExt);
1124}
1125
1126
1127/** @internal */
1128typedef struct QMFEIPARG
1129{
1130 RTINTPTR uPC;
1131
1132 char *pszNearSym1;
1133 size_t cchNearSym1;
1134 RTINTPTR offNearSym1;
1135
1136 char *pszNearSym2;
1137 size_t cchNearSym2;
1138 RTINTPTR offNearSym2;
1139} QMFEIPARG, *PQMFEIPARG;
1140
1141
1142/**
1143 * Enumeration callback function used by RTLdrEnumSymbols().
1144 *
1145 * @returns VBox status code. Failure will stop the enumeration.
1146 * @param hLdrMod The loader module handle.
1147 * @param pszSymbol Symbol name. NULL if ordinal only.
1148 * @param uSymbol Symbol ordinal, ~0 if not used.
1149 * @param Value Symbol value.
1150 * @param pvUser The user argument specified to RTLdrEnumSymbols().
1151 */
1152static DECLCALLBACK(int) pdmR3QueryModFromEIPEnumSymbols(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
1153{
1154 PQMFEIPARG pArgs = (PQMFEIPARG)pvUser;
1155
1156 RTINTPTR off = Value - pArgs->uPC;
1157 if (off <= 0) /* near1 is before or at same location. */
1158 {
1159 if (off > pArgs->offNearSym1)
1160 {
1161 pArgs->offNearSym1 = off;
1162 if (pArgs->pszNearSym1 && pArgs->cchNearSym1)
1163 {
1164 *pArgs->pszNearSym1 = '\0';
1165 if (pszSymbol)
1166 strncat(pArgs->pszNearSym1, pszSymbol, pArgs->cchNearSym1);
1167 else
1168 {
1169 char szOrd[32];
1170 RTStrPrintf(szOrd, sizeof(szOrd), "#%#x", uSymbol);
1171 strncat(pArgs->pszNearSym1, szOrd, pArgs->cchNearSym1);
1172 }
1173 }
1174 }
1175 }
1176 else /* near2 is after */
1177 {
1178 if (off < pArgs->offNearSym2)
1179 {
1180 pArgs->offNearSym2 = off;
1181 if (pArgs->pszNearSym2 && pArgs->cchNearSym2)
1182 {
1183 *pArgs->pszNearSym2 = '\0';
1184 if (pszSymbol)
1185 strncat(pArgs->pszNearSym2, pszSymbol, pArgs->cchNearSym2);
1186 else
1187 {
1188 char szOrd[32];
1189 RTStrPrintf(szOrd, sizeof(szOrd), "#%#x", uSymbol);
1190 strncat(pArgs->pszNearSym2, szOrd, pArgs->cchNearSym2);
1191 }
1192 }
1193 }
1194 }
1195
1196 return VINF_SUCCESS;
1197}
1198
1199
1200/**
1201 * Internal worker for PDMR3LdrQueryRCModFromPC and PDMR3LdrQueryR0ModFromPC.
1202 *
1203 * @returns VBox status code.
1204 *
1205 * @param pVM VM handle
1206 * @param uPC The program counter (eip/rip) to locate the module for.
1207 * @param enmType The module type.
1208 * @param pszModName Where to store the module name.
1209 * @param cchModName Size of the module name buffer.
1210 * @param pMod Base address of the module.
1211 * @param pszNearSym1 Name of the closes symbol from below.
1212 * @param cchNearSym1 Size of the buffer pointed to by pszNearSym1.
1213 * @param pNearSym1 The address of pszNearSym1.
1214 * @param pszNearSym2 Name of the closes symbol from below.
1215 * @param cchNearSym2 Size of the buffer pointed to by pszNearSym2.
1216 * @param pNearSym2 The address of pszNearSym2.
1217 */
1218static int pdmR3LdrQueryModFromPC(PVM pVM, RTUINTPTR uPC, PDMMODTYPE enmType,
1219 char *pszModName, size_t cchModName, PRTUINTPTR pMod,
1220 char *pszNearSym1, size_t cchNearSym1, PRTUINTPTR pNearSym1,
1221 char *pszNearSym2, size_t cchNearSym2, PRTUINTPTR pNearSym2)
1222{
1223 PUVM pUVM = pVM->pUVM;
1224 int rc = VERR_MODULE_NOT_FOUND;
1225 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
1226 for (PPDMMOD pCur= pUVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
1227 {
1228 if (pCur->eType != enmType)
1229 continue;
1230
1231 /* The following RTLdrOpen call is a dirty hack to get ring-0 module information. */
1232 RTLDRMOD hLdrMod = pCur->hLdrMod;
1233 if (hLdrMod == NIL_RTLDRMOD && uPC >= pCur->ImageBase)
1234 {
1235 int rc2 = RTLdrOpen(pCur->szFilename, 0 /*fFlags*/, RTLDRARCH_HOST, &hLdrMod);
1236 if (RT_FAILURE(rc2))
1237 hLdrMod = NIL_RTLDRMOD;
1238 }
1239
1240 if ( hLdrMod != NIL_RTLDRMOD
1241 && uPC - pCur->ImageBase < RTLdrSize(hLdrMod))
1242 {
1243 if (pMod)
1244 *pMod = pCur->ImageBase;
1245 if (pszModName && cchModName)
1246 {
1247 *pszModName = '\0';
1248 strncat(pszModName, pCur->szName, cchModName);
1249 }
1250 if (pNearSym1) *pNearSym1 = 0;
1251 if (pNearSym2) *pNearSym2 = 0;
1252 if (pszNearSym1) *pszNearSym1 = '\0';
1253 if (pszNearSym2) *pszNearSym2 = '\0';
1254
1255 /*
1256 * Locate the nearest symbols.
1257 */
1258 QMFEIPARG Args;
1259 Args.uPC = uPC;
1260 Args.pszNearSym1 = pszNearSym1;
1261 Args.cchNearSym1 = cchNearSym1;
1262 Args.offNearSym1 = RTINTPTR_MIN;
1263 Args.pszNearSym2 = pszNearSym2;
1264 Args.cchNearSym2 = cchNearSym2;
1265 Args.offNearSym2 = RTINTPTR_MAX;
1266
1267 rc = RTLdrEnumSymbols(hLdrMod, RTLDR_ENUM_SYMBOL_FLAGS_ALL, pCur->pvBits, pCur->ImageBase,
1268 pdmR3QueryModFromEIPEnumSymbols, &Args);
1269 if (pNearSym1 && Args.offNearSym1 != RTINTPTR_MIN)
1270 *pNearSym1 = Args.offNearSym1 + uPC;
1271 if (pNearSym2 && Args.offNearSym2 != RTINTPTR_MAX)
1272 *pNearSym2 = Args.offNearSym2 + uPC;
1273
1274 rc = VINF_SUCCESS;
1275 }
1276
1277 if (hLdrMod != pCur->hLdrMod && hLdrMod != NIL_RTLDRMOD)
1278 RTLdrClose(hLdrMod);
1279
1280 if (RT_SUCCESS(rc))
1281 break;
1282 }
1283 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
1284 return rc;
1285}
1286
1287
1288/**
1289 * Queries raw-mode context module information from an PC (eip/rip).
1290 *
1291 * This is typically used to locate a crash address.
1292 *
1293 * @returns VBox status code.
1294 *
1295 * @param pVM VM handle
1296 * @param uPC The program counter (eip/rip) to locate the module for.
1297 * @param pszModName Where to store the module name.
1298 * @param cchModName Size of the module name buffer.
1299 * @param pMod Base address of the module.
1300 * @param pszNearSym1 Name of the closes symbol from below.
1301 * @param cchNearSym1 Size of the buffer pointed to by pszNearSym1.
1302 * @param pNearSym1 The address of pszNearSym1.
1303 * @param pszNearSym2 Name of the closes symbol from below.
1304 * @param cchNearSym2 Size of the buffer pointed to by pszNearSym2.
1305 * @param pNearSym2 The address of pszNearSym2.
1306 */
1307VMMR3DECL(int) PDMR3LdrQueryRCModFromPC(PVM pVM, RTRCPTR uPC,
1308 char *pszModName, size_t cchModName, PRTRCPTR pMod,
1309 char *pszNearSym1, size_t cchNearSym1, PRTRCPTR pNearSym1,
1310 char *pszNearSym2, size_t cchNearSym2, PRTRCPTR pNearSym2)
1311{
1312 RTUINTPTR AddrMod = 0;
1313 RTUINTPTR AddrNear1 = 0;
1314 RTUINTPTR AddrNear2 = 0;
1315 int rc = pdmR3LdrQueryModFromPC(pVM, uPC, PDMMOD_TYPE_RC,
1316 pszModName, cchModName, &AddrMod,
1317 pszNearSym1, cchNearSym1, &AddrNear1,
1318 pszNearSym2, cchNearSym2, &AddrNear2);
1319 if (RT_SUCCESS(rc))
1320 {
1321 if (pMod)
1322 *pMod = (RTRCPTR)AddrMod;
1323 if (pNearSym1)
1324 *pNearSym1 = (RTRCPTR)AddrNear1;
1325 if (pNearSym2)
1326 *pNearSym2 = (RTRCPTR)AddrNear2;
1327 }
1328 return rc;
1329}
1330
1331
1332/**
1333 * Queries ring-0 context module information from an PC (eip/rip).
1334 *
1335 * This is typically used to locate a crash address.
1336 *
1337 * @returns VBox status code.
1338 *
1339 * @param pVM VM handle
1340 * @param uPC The program counter (eip/rip) to locate the module for.
1341 * @param pszModName Where to store the module name.
1342 * @param cchModName Size of the module name buffer.
1343 * @param pMod Base address of the module.
1344 * @param pszNearSym1 Name of the closes symbol from below.
1345 * @param cchNearSym1 Size of the buffer pointed to by pszNearSym1.
1346 * @param pNearSym1 The address of pszNearSym1.
1347 * @param pszNearSym2 Name of the closes symbol from below.
1348 * @param cchNearSym2 Size of the buffer pointed to by pszNearSym2. Optional.
1349 * @param pNearSym2 The address of pszNearSym2. Optional.
1350 */
1351VMMR3DECL(int) PDMR3LdrQueryR0ModFromPC(PVM pVM, RTR0PTR uPC,
1352 char *pszModName, size_t cchModName, PRTR0PTR pMod,
1353 char *pszNearSym1, size_t cchNearSym1, PRTR0PTR pNearSym1,
1354 char *pszNearSym2, size_t cchNearSym2, PRTR0PTR pNearSym2)
1355{
1356 RTUINTPTR AddrMod = 0;
1357 RTUINTPTR AddrNear1 = 0;
1358 RTUINTPTR AddrNear2 = 0;
1359 int rc = pdmR3LdrQueryModFromPC(pVM, uPC, PDMMOD_TYPE_R0,
1360 pszModName, cchModName, &AddrMod,
1361 pszNearSym1, cchNearSym1, &AddrNear1,
1362 pszNearSym2, cchNearSym2, &AddrNear2);
1363 if (RT_SUCCESS(rc))
1364 {
1365 if (pMod)
1366 *pMod = (RTR0PTR)AddrMod;
1367 if (pNearSym1)
1368 *pNearSym1 = (RTR0PTR)AddrNear1;
1369 if (pNearSym2)
1370 *pNearSym2 = (RTR0PTR)AddrNear2;
1371 }
1372 return rc;
1373}
1374
1375
1376/**
1377 * Enumerate all PDM modules.
1378 *
1379 * @returns VBox status.
1380 * @param pVM VM Handle.
1381 * @param pfnCallback Function to call back for each of the modules.
1382 * @param pvArg User argument.
1383 */
1384VMMR3DECL(int) PDMR3LdrEnumModules(PVM pVM, PFNPDMR3ENUM pfnCallback, void *pvArg)
1385{
1386 PUVM pUVM = pVM->pUVM;
1387 int rc = VINF_SUCCESS;
1388 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
1389 for (PPDMMOD pCur = pUVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
1390 {
1391 rc = pfnCallback(pVM,
1392 pCur->szFilename,
1393 pCur->szName,
1394 pCur->ImageBase,
1395 pCur->eType == PDMMOD_TYPE_RC ? RTLdrSize(pCur->hLdrMod) : 0,
1396 pCur->eType == PDMMOD_TYPE_RC,
1397 pvArg);
1398 if (RT_FAILURE(rc))
1399 break;
1400 }
1401 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
1402 return rc;
1403}
1404
1405
1406/**
1407 * Locates a module.
1408 *
1409 * @returns Pointer to the module if found.
1410 * @param pUVM Pointer to the user mode VM structure.
1411 * @param pszModule The module name.
1412 * @param enmType The module type.
1413 * @param fLazy Lazy loading the module if set.
1414 * @param pszSearchPath Search path for use when lazy loading.
1415 */
1416static PPDMMOD pdmR3LdrFindModule(PUVM pUVM, const char *pszModule, PDMMODTYPE enmType,
1417 bool fLazy, const char *pszSearchPath)
1418{
1419 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
1420 for (PPDMMOD pModule = pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
1421 if ( pModule->eType == enmType
1422 && !strcmp(pModule->szName, pszModule))
1423 {
1424 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
1425 return pModule;
1426 }
1427 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
1428 if (fLazy)
1429 {
1430 switch (enmType)
1431 {
1432#ifdef VBOX_WITH_RAW_MODE
1433 case PDMMOD_TYPE_RC:
1434 {
1435 char *pszFilename = pdmR3FileRC(pszModule, pszSearchPath);
1436 if (pszFilename)
1437 {
1438 int rc = PDMR3LdrLoadRC(pUVM->pVM, pszFilename, pszModule);
1439 RTMemTmpFree(pszFilename);
1440 if (RT_SUCCESS(rc))
1441 return pdmR3LdrFindModule(pUVM, pszModule, enmType, false, NULL);
1442 }
1443 break;
1444 }
1445#endif
1446
1447 case PDMMOD_TYPE_R0:
1448 {
1449 int rc = pdmR3LoadR0U(pUVM, NULL, pszModule, pszSearchPath);
1450 if (RT_SUCCESS(rc))
1451 return pdmR3LdrFindModule(pUVM, pszModule, enmType, false, NULL);
1452 break;
1453 }
1454
1455 default:
1456 AssertFailed();
1457 }
1458 }
1459 return NULL;
1460}
1461
1462
1463/**
1464 * Resolves a ring-0 or raw-mode context interface.
1465 *
1466 * @returns VBox status code.
1467 * @param pVM The VM handle.
1468 * @param pvInterface Pointer to the interface structure. The symbol list
1469 * describes the layout.
1470 * @param cbInterface The size of the structure pvInterface is pointing
1471 * to. For bounds checking.
1472 * @param pszModule The module name. If NULL we assume it's the default
1473 * R0 or RC module (@a fRing0OrRC). We'll attempt to
1474 * load the module if it isn't found in the module
1475 * list.
1476 * @param pszSearchPath The module search path. If NULL, search the
1477 * architecture dependent install directory.
1478 * @param pszSymPrefix What to prefix the symbols in the list with. The
1479 * idea is that you define a list that goes with an
1480 * interface (INTERFACE_SYM_LIST) and reuse it with
1481 * each implementation.
1482 * @param pszSymList The symbol list for the interface. This is a
1483 * semi-colon separated list of symbol base names. As
1484 * mentioned above, each is prefixed with @a
1485 * pszSymPrefix before resolving. There are a couple
1486 * of special symbol names that will cause us to skip
1487 * ahead a little bit:
1488 * - U8:whatever,
1489 * - U16:whatever,
1490 * - U32:whatever,
1491 * - U64:whatever,
1492 * - RCPTR:whatever,
1493 * - R3PTR:whatever,
1494 * - R0PTR:whatever,
1495 * - GCPHYS:whatever,
1496 * - HCPHYS:whatever.
1497 * @param fRing0 Set if it's a ring-0 context interface, clear if
1498 * it's raw-mode context interface.
1499 */
1500VMMR3DECL(int) PDMR3LdrGetInterfaceSymbols(PVM pVM, void *pvInterface, size_t cbInterface,
1501 const char *pszModule, const char *pszSearchPath,
1502 const char *pszSymPrefix, const char *pszSymList,
1503 bool fRing0)
1504{
1505 /*
1506 * Find the module.
1507 */
1508 int rc = VINF_SUCCESS;
1509 PPDMMOD pModule = pdmR3LdrFindModule(pVM->pUVM,
1510 pszModule ? pszModule : fRing0 ? "VMMR0.r0" : "VMMGC.gc",
1511 fRing0 ? PDMMOD_TYPE_R0 : PDMMOD_TYPE_RC,
1512 true /*fLazy*/, pszSearchPath);
1513 if (pModule)
1514 {
1515 /* Prep the symbol name. */
1516 char szSymbol[256];
1517 size_t const cchSymPrefix = strlen(pszSymPrefix);
1518 AssertReturn(cchSymPrefix + 5 < sizeof(szSymbol), VERR_SYMBOL_NOT_FOUND);
1519 memcpy(szSymbol, pszSymPrefix, cchSymPrefix);
1520
1521 /*
1522 * Iterate the symbol list.
1523 */
1524 uint32_t offInterface = 0;
1525 const char *pszCur = pszSymList;
1526 while (pszCur)
1527 {
1528 /*
1529 * Find the end of the current symbol name.
1530 */
1531 size_t cchSym;
1532 const char *pszNext = strchr(pszCur, ';');
1533 if (pszNext)
1534 {
1535 cchSym = pszNext - pszCur;
1536 pszNext++;
1537 }
1538 else
1539 cchSym = strlen(pszCur);
1540 AssertBreakStmt(cchSym > 0, rc = VERR_INVALID_PARAMETER);
1541
1542 /* Is it a skip instruction? */
1543 const char *pszColon = (const char *)memchr(pszCur, ':', cchSym);
1544 if (pszColon)
1545 {
1546 /*
1547 * String switch on the instruction and execute it, checking
1548 * that we didn't overshoot the interface structure.
1549 */
1550#define IS_SKIP_INSTR(szInstr) \
1551 ( cchSkip == sizeof(szInstr) - 1 \
1552 && !memcmp(pszCur, szInstr, sizeof(szInstr) - 1) )
1553
1554 size_t const cchSkip = pszColon - pszCur;
1555 if (IS_SKIP_INSTR("U8"))
1556 offInterface += sizeof(uint8_t);
1557 else if (IS_SKIP_INSTR("U16"))
1558 offInterface += sizeof(uint16_t);
1559 else if (IS_SKIP_INSTR("U32"))
1560 offInterface += sizeof(uint32_t);
1561 else if (IS_SKIP_INSTR("U64"))
1562 offInterface += sizeof(uint64_t);
1563 else if (IS_SKIP_INSTR("RCPTR"))
1564 offInterface += sizeof(RTRCPTR);
1565 else if (IS_SKIP_INSTR("R3PTR"))
1566 offInterface += sizeof(RTR3PTR);
1567 else if (IS_SKIP_INSTR("R0PTR"))
1568 offInterface += sizeof(RTR0PTR);
1569 else if (IS_SKIP_INSTR("HCPHYS"))
1570 offInterface += sizeof(RTHCPHYS);
1571 else if (IS_SKIP_INSTR("GCPHYS"))
1572 offInterface += sizeof(RTGCPHYS);
1573 else
1574 AssertMsgFailedBreakStmt(("Invalid skip instruction %.*s (prefix=%s)\n", cchSym, pszCur, pszSymPrefix),
1575 rc = VERR_INVALID_PARAMETER);
1576 AssertMsgBreakStmt(offInterface <= cbInterface,
1577 ("off=%#x cb=%#x (sym=%.*s prefix=%s)\n", offInterface, cbInterface, cchSym, pszCur, pszSymPrefix),
1578 rc = VERR_BUFFER_OVERFLOW);
1579#undef IS_SKIP_INSTR
1580 }
1581 else
1582 {
1583 /*
1584 * Construct the symbol name, get its value, store it and
1585 * advance the interface cursor.
1586 */
1587 AssertReturn(cchSymPrefix + cchSym < sizeof(szSymbol), VERR_SYMBOL_NOT_FOUND);
1588 memcpy(&szSymbol[cchSymPrefix], pszCur, cchSym);
1589 szSymbol[cchSymPrefix + cchSym] = '\0';
1590
1591 if (fRing0)
1592 {
1593 void *pvValue;
1594 rc = SUPR3GetSymbolR0((void *)(RTR0PTR)pModule->ImageBase, szSymbol, &pvValue);
1595 AssertMsgRCBreak(rc, ("Couldn't find symbol '%s' in module '%s'\n", szSymbol, pModule->szName));
1596
1597 PRTR0PTR pValue = (PRTR0PTR)((uintptr_t)pvInterface + offInterface);
1598 AssertMsgBreakStmt(offInterface + sizeof(*pValue) <= cbInterface,
1599 ("off=%#x cb=%#x sym=%s\n", offInterface, cbInterface, szSymbol),
1600 rc = VERR_BUFFER_OVERFLOW);
1601 *pValue = (RTR0PTR)pvValue;
1602 Assert((void *)*pValue == pvValue);
1603 offInterface += sizeof(*pValue);
1604 }
1605 else
1606 {
1607 RTUINTPTR Value;
1608 rc = RTLdrGetSymbolEx(pModule->hLdrMod, pModule->pvBits, pModule->ImageBase, szSymbol, &Value);
1609 AssertMsgRCBreak(rc, ("Couldn't find symbol '%s' in module '%s'\n", szSymbol, pModule->szName));
1610
1611 PRTRCPTR pValue = (PRTRCPTR)((uintptr_t)pvInterface + offInterface);
1612 AssertMsgBreakStmt(offInterface + sizeof(*pValue) <= cbInterface,
1613 ("off=%#x cb=%#x sym=%s\n", offInterface, cbInterface, szSymbol),
1614 rc = VERR_BUFFER_OVERFLOW);
1615 *pValue = (RTRCPTR)Value;
1616 Assert(*pValue == Value);
1617 offInterface += sizeof(*pValue);
1618 }
1619 }
1620
1621 /* advance */
1622 pszCur = pszNext;
1623 }
1624
1625 }
1626 else
1627 rc = VERR_MODULE_NOT_FOUND;
1628 return rc;
1629}
1630
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