VirtualBox

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

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

Use RTERRINFO in SUPLib for hardening APIs and such.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 57.9 KB
Line 
1/* $Id: PDMLdr.cpp 35188 2010-12-16 15:13:07Z 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/pdm.h>
26#include <VBox/mm.h>
27#include <VBox/vmm.h>
28#include <VBox/vm.h>
29#include <VBox/uvm.h>
30#include <VBox/sup.h>
31#include <VBox/param.h>
32#include <VBox/err.h>
33#include <VBox/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, "VMMGCBuiltin.gc"))
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 ? RTPathSharedLibs(szPath, sizeof(szPath))
1115 : RTPathAppPrivateArch(szPath, sizeof(szPath));
1116 if (!RT_SUCCESS(rc))
1117 {
1118 AssertMsgFailed(("RTPath[SharedLibs|AppPrivateArch](,%d) failed rc=%d!\n", sizeof(szPath), rc));
1119 return NULL;
1120 }
1121
1122 return pdmR3FileConstruct(szPath, pszFile, pszDefaultExt);
1123}
1124
1125
1126/** @internal */
1127typedef struct QMFEIPARG
1128{
1129 RTINTPTR uPC;
1130
1131 char *pszNearSym1;
1132 size_t cchNearSym1;
1133 RTINTPTR offNearSym1;
1134
1135 char *pszNearSym2;
1136 size_t cchNearSym2;
1137 RTINTPTR offNearSym2;
1138} QMFEIPARG, *PQMFEIPARG;
1139
1140
1141/**
1142 * Enumeration callback function used by RTLdrEnumSymbols().
1143 *
1144 * @returns VBox status code. Failure will stop the enumeration.
1145 * @param hLdrMod The loader module handle.
1146 * @param pszSymbol Symbol name. NULL if ordinal only.
1147 * @param uSymbol Symbol ordinal, ~0 if not used.
1148 * @param Value Symbol value.
1149 * @param pvUser The user argument specified to RTLdrEnumSymbols().
1150 */
1151static DECLCALLBACK(int) pdmR3QueryModFromEIPEnumSymbols(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
1152{
1153 PQMFEIPARG pArgs = (PQMFEIPARG)pvUser;
1154
1155 RTINTPTR off = Value - pArgs->uPC;
1156 if (off <= 0) /* near1 is before or at same location. */
1157 {
1158 if (off > pArgs->offNearSym1)
1159 {
1160 pArgs->offNearSym1 = off;
1161 if (pArgs->pszNearSym1 && pArgs->cchNearSym1)
1162 {
1163 *pArgs->pszNearSym1 = '\0';
1164 if (pszSymbol)
1165 strncat(pArgs->pszNearSym1, pszSymbol, pArgs->cchNearSym1);
1166 else
1167 {
1168 char szOrd[32];
1169 RTStrPrintf(szOrd, sizeof(szOrd), "#%#x", uSymbol);
1170 strncat(pArgs->pszNearSym1, szOrd, pArgs->cchNearSym1);
1171 }
1172 }
1173 }
1174 }
1175 else /* near2 is after */
1176 {
1177 if (off < pArgs->offNearSym2)
1178 {
1179 pArgs->offNearSym2 = off;
1180 if (pArgs->pszNearSym2 && pArgs->cchNearSym2)
1181 {
1182 *pArgs->pszNearSym2 = '\0';
1183 if (pszSymbol)
1184 strncat(pArgs->pszNearSym2, pszSymbol, pArgs->cchNearSym2);
1185 else
1186 {
1187 char szOrd[32];
1188 RTStrPrintf(szOrd, sizeof(szOrd), "#%#x", uSymbol);
1189 strncat(pArgs->pszNearSym2, szOrd, pArgs->cchNearSym2);
1190 }
1191 }
1192 }
1193 }
1194
1195 return VINF_SUCCESS;
1196}
1197
1198
1199/**
1200 * Internal worker for PDMR3LdrQueryRCModFromPC and PDMR3LdrQueryR0ModFromPC.
1201 *
1202 * @returns VBox status code.
1203 *
1204 * @param pVM VM handle
1205 * @param uPC The program counter (eip/rip) to locate the module for.
1206 * @param enmType The module type.
1207 * @param pszModName Where to store the module name.
1208 * @param cchModName Size of the module name buffer.
1209 * @param pMod Base address of the module.
1210 * @param pszNearSym1 Name of the closes symbol from below.
1211 * @param cchNearSym1 Size of the buffer pointed to by pszNearSym1.
1212 * @param pNearSym1 The address of pszNearSym1.
1213 * @param pszNearSym2 Name of the closes symbol from below.
1214 * @param cchNearSym2 Size of the buffer pointed to by pszNearSym2.
1215 * @param pNearSym2 The address of pszNearSym2.
1216 */
1217static int pdmR3LdrQueryModFromPC(PVM pVM, RTUINTPTR uPC, PDMMODTYPE enmType,
1218 char *pszModName, size_t cchModName, PRTUINTPTR pMod,
1219 char *pszNearSym1, size_t cchNearSym1, PRTUINTPTR pNearSym1,
1220 char *pszNearSym2, size_t cchNearSym2, PRTUINTPTR pNearSym2)
1221{
1222 PUVM pUVM = pVM->pUVM;
1223 int rc = VERR_MODULE_NOT_FOUND;
1224 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
1225 for (PPDMMOD pCur= pUVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
1226 {
1227 if (pCur->eType != enmType)
1228 continue;
1229
1230 /* The following RTLdrOpen call is a dirty hack to get ring-0 module information. */
1231 RTLDRMOD hLdrMod = pCur->hLdrMod;
1232 if (hLdrMod == NIL_RTLDRMOD && uPC >= pCur->ImageBase)
1233 {
1234 int rc2 = RTLdrOpen(pCur->szFilename, 0 /*fFlags*/, RTLDRARCH_HOST, &hLdrMod);
1235 if (RT_FAILURE(rc2))
1236 hLdrMod = NIL_RTLDRMOD;
1237 }
1238
1239 if ( hLdrMod != NIL_RTLDRMOD
1240 && uPC - pCur->ImageBase < RTLdrSize(hLdrMod))
1241 {
1242 if (pMod)
1243 *pMod = pCur->ImageBase;
1244 if (pszModName && cchModName)
1245 {
1246 *pszModName = '\0';
1247 strncat(pszModName, pCur->szName, cchModName);
1248 }
1249 if (pNearSym1) *pNearSym1 = 0;
1250 if (pNearSym2) *pNearSym2 = 0;
1251 if (pszNearSym1) *pszNearSym1 = '\0';
1252 if (pszNearSym2) *pszNearSym2 = '\0';
1253
1254 /*
1255 * Locate the nearest symbols.
1256 */
1257 QMFEIPARG Args;
1258 Args.uPC = uPC;
1259 Args.pszNearSym1 = pszNearSym1;
1260 Args.cchNearSym1 = cchNearSym1;
1261 Args.offNearSym1 = RTINTPTR_MIN;
1262 Args.pszNearSym2 = pszNearSym2;
1263 Args.cchNearSym2 = cchNearSym2;
1264 Args.offNearSym2 = RTINTPTR_MAX;
1265
1266 rc = RTLdrEnumSymbols(hLdrMod, RTLDR_ENUM_SYMBOL_FLAGS_ALL, pCur->pvBits, pCur->ImageBase,
1267 pdmR3QueryModFromEIPEnumSymbols, &Args);
1268 if (pNearSym1 && Args.offNearSym1 != RTINTPTR_MIN)
1269 *pNearSym1 = Args.offNearSym1 + uPC;
1270 if (pNearSym2 && Args.offNearSym2 != RTINTPTR_MAX)
1271 *pNearSym2 = Args.offNearSym2 + uPC;
1272
1273 rc = VINF_SUCCESS;
1274 }
1275
1276 if (hLdrMod != pCur->hLdrMod && hLdrMod != NIL_RTLDRMOD)
1277 RTLdrClose(hLdrMod);
1278
1279 if (RT_SUCCESS(rc))
1280 break;
1281 }
1282 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
1283 return rc;
1284}
1285
1286
1287/**
1288 * Queries raw-mode context module information from an PC (eip/rip).
1289 *
1290 * This is typically used to locate a crash address.
1291 *
1292 * @returns VBox status code.
1293 *
1294 * @param pVM VM handle
1295 * @param uPC The program counter (eip/rip) to locate the module for.
1296 * @param pszModName Where to store the module name.
1297 * @param cchModName Size of the module name buffer.
1298 * @param pMod Base address of the module.
1299 * @param pszNearSym1 Name of the closes symbol from below.
1300 * @param cchNearSym1 Size of the buffer pointed to by pszNearSym1.
1301 * @param pNearSym1 The address of pszNearSym1.
1302 * @param pszNearSym2 Name of the closes symbol from below.
1303 * @param cchNearSym2 Size of the buffer pointed to by pszNearSym2.
1304 * @param pNearSym2 The address of pszNearSym2.
1305 */
1306VMMR3DECL(int) PDMR3LdrQueryRCModFromPC(PVM pVM, RTRCPTR uPC,
1307 char *pszModName, size_t cchModName, PRTRCPTR pMod,
1308 char *pszNearSym1, size_t cchNearSym1, PRTRCPTR pNearSym1,
1309 char *pszNearSym2, size_t cchNearSym2, PRTRCPTR pNearSym2)
1310{
1311 RTUINTPTR AddrMod = 0;
1312 RTUINTPTR AddrNear1 = 0;
1313 RTUINTPTR AddrNear2 = 0;
1314 int rc = pdmR3LdrQueryModFromPC(pVM, uPC, PDMMOD_TYPE_RC,
1315 pszModName, cchModName, &AddrMod,
1316 pszNearSym1, cchNearSym1, &AddrNear1,
1317 pszNearSym2, cchNearSym2, &AddrNear2);
1318 if (RT_SUCCESS(rc))
1319 {
1320 if (pMod)
1321 *pMod = (RTRCPTR)AddrMod;
1322 if (pNearSym1)
1323 *pNearSym1 = (RTRCPTR)AddrNear1;
1324 if (pNearSym2)
1325 *pNearSym2 = (RTRCPTR)AddrNear2;
1326 }
1327 return rc;
1328}
1329
1330
1331/**
1332 * Queries ring-0 context module information from an PC (eip/rip).
1333 *
1334 * This is typically used to locate a crash address.
1335 *
1336 * @returns VBox status code.
1337 *
1338 * @param pVM VM handle
1339 * @param uPC The program counter (eip/rip) to locate the module for.
1340 * @param pszModName Where to store the module name.
1341 * @param cchModName Size of the module name buffer.
1342 * @param pMod Base address of the module.
1343 * @param pszNearSym1 Name of the closes symbol from below.
1344 * @param cchNearSym1 Size of the buffer pointed to by pszNearSym1.
1345 * @param pNearSym1 The address of pszNearSym1.
1346 * @param pszNearSym2 Name of the closes symbol from below.
1347 * @param cchNearSym2 Size of the buffer pointed to by pszNearSym2. Optional.
1348 * @param pNearSym2 The address of pszNearSym2. Optional.
1349 */
1350VMMR3DECL(int) PDMR3LdrQueryR0ModFromPC(PVM pVM, RTR0PTR uPC,
1351 char *pszModName, size_t cchModName, PRTR0PTR pMod,
1352 char *pszNearSym1, size_t cchNearSym1, PRTR0PTR pNearSym1,
1353 char *pszNearSym2, size_t cchNearSym2, PRTR0PTR pNearSym2)
1354{
1355 RTUINTPTR AddrMod = 0;
1356 RTUINTPTR AddrNear1 = 0;
1357 RTUINTPTR AddrNear2 = 0;
1358 int rc = pdmR3LdrQueryModFromPC(pVM, uPC, PDMMOD_TYPE_R0,
1359 pszModName, cchModName, &AddrMod,
1360 pszNearSym1, cchNearSym1, &AddrNear1,
1361 pszNearSym2, cchNearSym2, &AddrNear2);
1362 if (RT_SUCCESS(rc))
1363 {
1364 if (pMod)
1365 *pMod = (RTR0PTR)AddrMod;
1366 if (pNearSym1)
1367 *pNearSym1 = (RTR0PTR)AddrNear1;
1368 if (pNearSym2)
1369 *pNearSym2 = (RTR0PTR)AddrNear2;
1370 }
1371 return rc;
1372}
1373
1374
1375/**
1376 * Enumerate all PDM modules.
1377 *
1378 * @returns VBox status.
1379 * @param pVM VM Handle.
1380 * @param pfnCallback Function to call back for each of the modules.
1381 * @param pvArg User argument.
1382 */
1383VMMR3DECL(int) PDMR3LdrEnumModules(PVM pVM, PFNPDMR3ENUM pfnCallback, void *pvArg)
1384{
1385 PUVM pUVM = pVM->pUVM;
1386 int rc = VINF_SUCCESS;
1387 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
1388 for (PPDMMOD pCur = pUVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
1389 {
1390 rc = pfnCallback(pVM,
1391 pCur->szFilename,
1392 pCur->szName,
1393 pCur->ImageBase,
1394 pCur->eType == PDMMOD_TYPE_RC ? RTLdrSize(pCur->hLdrMod) : 0,
1395 pCur->eType == PDMMOD_TYPE_RC,
1396 pvArg);
1397 if (RT_FAILURE(rc))
1398 break;
1399 }
1400 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
1401 return rc;
1402}
1403
1404
1405/**
1406 * Locates a module.
1407 *
1408 * @returns Pointer to the module if found.
1409 * @param pUVM Pointer to the user mode VM structure.
1410 * @param pszModule The module name.
1411 * @param enmType The module type.
1412 * @param fLazy Lazy loading the module if set.
1413 * @param pszSearchPath Search path for use when lazy loading.
1414 */
1415static PPDMMOD pdmR3LdrFindModule(PUVM pUVM, const char *pszModule, PDMMODTYPE enmType,
1416 bool fLazy, const char *pszSearchPath)
1417{
1418 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
1419 for (PPDMMOD pModule = pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
1420 if ( pModule->eType == enmType
1421 && !strcmp(pModule->szName, pszModule))
1422 {
1423 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
1424 return pModule;
1425 }
1426 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
1427 if (fLazy)
1428 {
1429 switch (enmType)
1430 {
1431#ifdef VBOX_WITH_RAW_MODE
1432 case PDMMOD_TYPE_RC:
1433 {
1434 char *pszFilename = pdmR3FileRC(pszModule, pszSearchPath);
1435 if (pszFilename)
1436 {
1437 int rc = PDMR3LdrLoadRC(pUVM->pVM, pszFilename, pszModule);
1438 RTMemTmpFree(pszFilename);
1439 if (RT_SUCCESS(rc))
1440 return pdmR3LdrFindModule(pUVM, pszModule, enmType, false, NULL);
1441 }
1442 break;
1443 }
1444#endif
1445
1446 case PDMMOD_TYPE_R0:
1447 {
1448 int rc = pdmR3LoadR0U(pUVM, NULL, pszModule, pszSearchPath);
1449 if (RT_SUCCESS(rc))
1450 return pdmR3LdrFindModule(pUVM, pszModule, enmType, false, NULL);
1451 break;
1452 }
1453
1454 default:
1455 AssertFailed();
1456 }
1457 }
1458 return NULL;
1459}
1460
1461
1462/**
1463 * Resolves a ring-0 or raw-mode context interface.
1464 *
1465 * @returns VBox status code.
1466 * @param pVM The VM handle.
1467 * @param pvInterface Pointer to the interface structure. The symbol list
1468 * describes the layout.
1469 * @param cbInterface The size of the structure pvInterface is pointing
1470 * to. For bounds checking.
1471 * @param pszModule The module name. If NULL we assume it's the default
1472 * R0 or RC module (@a fRing0OrRC). We'll attempt to
1473 * load the module if it isn't found in the module
1474 * list.
1475 * @param pszSearchPath The module search path. If NULL, search the
1476 * architecture dependent install directory.
1477 * @param pszSymPrefix What to prefix the symbols in the list with. The
1478 * idea is that you define a list that goes with an
1479 * interface (INTERFACE_SYM_LIST) and reuse it with
1480 * each implementation.
1481 * @param pszSymList The symbol list for the interface. This is a
1482 * semi-colon separated list of symbol base names. As
1483 * mentioned above, each is prefixed with @a
1484 * pszSymPrefix before resolving. There are a couple
1485 * of special symbol names that will cause us to skip
1486 * ahead a little bit:
1487 * - U8:whatever,
1488 * - U16:whatever,
1489 * - U32:whatever,
1490 * - U64:whatever,
1491 * - RCPTR:whatever,
1492 * - R3PTR:whatever,
1493 * - R0PTR:whatever,
1494 * - GCPHYS:whatever,
1495 * - HCPHYS:whatever.
1496 * @param fRing0 Set if it's a ring-0 context interface, clear if
1497 * it's raw-mode context interface.
1498 */
1499VMMR3DECL(int) PDMR3LdrGetInterfaceSymbols(PVM pVM, void *pvInterface, size_t cbInterface,
1500 const char *pszModule, const char *pszSearchPath,
1501 const char *pszSymPrefix, const char *pszSymList,
1502 bool fRing0)
1503{
1504 /*
1505 * Find the module.
1506 */
1507 int rc = VINF_SUCCESS;
1508 PPDMMOD pModule = pdmR3LdrFindModule(pVM->pUVM,
1509 pszModule ? pszModule : fRing0 ? "VMMR0.r0" : "VMMGC.gc",
1510 fRing0 ? PDMMOD_TYPE_R0 : PDMMOD_TYPE_RC,
1511 true /*fLazy*/, pszSearchPath);
1512 if (pModule)
1513 {
1514 /* Prep the symbol name. */
1515 char szSymbol[256];
1516 size_t const cchSymPrefix = strlen(pszSymPrefix);
1517 AssertReturn(cchSymPrefix + 5 < sizeof(szSymbol), VERR_SYMBOL_NOT_FOUND);
1518 memcpy(szSymbol, pszSymPrefix, cchSymPrefix);
1519
1520 /*
1521 * Iterate the symbol list.
1522 */
1523 uint32_t offInterface = 0;
1524 const char *pszCur = pszSymList;
1525 while (pszCur)
1526 {
1527 /*
1528 * Find the end of the current symbol name.
1529 */
1530 size_t cchSym;
1531 const char *pszNext = strchr(pszCur, ';');
1532 if (pszNext)
1533 {
1534 cchSym = pszNext - pszCur;
1535 pszNext++;
1536 }
1537 else
1538 cchSym = strlen(pszCur);
1539 AssertBreakStmt(cchSym > 0, rc = VERR_INVALID_PARAMETER);
1540
1541 /* Is it a skip instruction? */
1542 const char *pszColon = (const char *)memchr(pszCur, ':', cchSym);
1543 if (pszColon)
1544 {
1545 /*
1546 * String switch on the instruction and execute it, checking
1547 * that we didn't overshoot the interface structure.
1548 */
1549#define IS_SKIP_INSTR(szInstr) \
1550 ( cchSkip == sizeof(szInstr) - 1 \
1551 && !memcmp(pszCur, szInstr, sizeof(szInstr) - 1) )
1552
1553 size_t const cchSkip = pszColon - pszCur;
1554 if (IS_SKIP_INSTR("U8"))
1555 offInterface += sizeof(uint8_t);
1556 else if (IS_SKIP_INSTR("U16"))
1557 offInterface += sizeof(uint16_t);
1558 else if (IS_SKIP_INSTR("U32"))
1559 offInterface += sizeof(uint32_t);
1560 else if (IS_SKIP_INSTR("U64"))
1561 offInterface += sizeof(uint64_t);
1562 else if (IS_SKIP_INSTR("RCPTR"))
1563 offInterface += sizeof(RTRCPTR);
1564 else if (IS_SKIP_INSTR("R3PTR"))
1565 offInterface += sizeof(RTR3PTR);
1566 else if (IS_SKIP_INSTR("R0PTR"))
1567 offInterface += sizeof(RTR0PTR);
1568 else if (IS_SKIP_INSTR("HCPHYS"))
1569 offInterface += sizeof(RTHCPHYS);
1570 else if (IS_SKIP_INSTR("GCPHYS"))
1571 offInterface += sizeof(RTGCPHYS);
1572 else
1573 AssertMsgFailedBreakStmt(("Invalid skip instruction %.*s (prefix=%s)\n", cchSym, pszCur, pszSymPrefix),
1574 rc = VERR_INVALID_PARAMETER);
1575 AssertMsgBreakStmt(offInterface <= cbInterface,
1576 ("off=%#x cb=%#x (sym=%.*s prefix=%s)\n", offInterface, cbInterface, cchSym, pszCur, pszSymPrefix),
1577 rc = VERR_BUFFER_OVERFLOW);
1578#undef IS_SKIP_INSTR
1579 }
1580 else
1581 {
1582 /*
1583 * Construct the symbol name, get its value, store it and
1584 * advance the interface cursor.
1585 */
1586 AssertReturn(cchSymPrefix + cchSym < sizeof(szSymbol), VERR_SYMBOL_NOT_FOUND);
1587 memcpy(&szSymbol[cchSymPrefix], pszCur, cchSym);
1588 szSymbol[cchSymPrefix + cchSym] = '\0';
1589
1590 if (fRing0)
1591 {
1592 void *pvValue;
1593 rc = SUPR3GetSymbolR0((void *)(RTR0PTR)pModule->ImageBase, szSymbol, &pvValue);
1594 AssertMsgRCBreak(rc, ("Couldn't find symbol '%s' in module '%s'\n", szSymbol, pModule->szName));
1595
1596 PRTR0PTR pValue = (PRTR0PTR)((uintptr_t)pvInterface + offInterface);
1597 AssertMsgBreakStmt(offInterface + sizeof(*pValue) <= cbInterface,
1598 ("off=%#x cb=%#x sym=%s\n", offInterface, cbInterface, szSymbol),
1599 rc = VERR_BUFFER_OVERFLOW);
1600 *pValue = (RTR0PTR)pvValue;
1601 Assert((void *)*pValue == pvValue);
1602 offInterface += sizeof(*pValue);
1603 }
1604 else
1605 {
1606 RTUINTPTR Value;
1607 rc = RTLdrGetSymbolEx(pModule->hLdrMod, pModule->pvBits, pModule->ImageBase, szSymbol, &Value);
1608 AssertMsgRCBreak(rc, ("Couldn't find symbol '%s' in module '%s'\n", szSymbol, pModule->szName));
1609
1610 PRTRCPTR pValue = (PRTRCPTR)((uintptr_t)pvInterface + offInterface);
1611 AssertMsgBreakStmt(offInterface + sizeof(*pValue) <= cbInterface,
1612 ("off=%#x cb=%#x sym=%s\n", offInterface, cbInterface, szSymbol),
1613 rc = VERR_BUFFER_OVERFLOW);
1614 *pValue = (RTRCPTR)Value;
1615 Assert(*pValue == Value);
1616 offInterface += sizeof(*pValue);
1617 }
1618 }
1619
1620 /* advance */
1621 pszCur = pszNext;
1622 }
1623
1624 }
1625 else
1626 rc = VERR_MODULE_NOT_FOUND;
1627 return rc;
1628}
1629
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