VirtualBox

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

Last change on this file since 56050 was 56050, checked in by vboxsync, 10 years ago

PDMLdr.cpp: Made PDMR3LdrGetSymbolRCLazy handle VMMRC.rc lazy loading correctly when presented with pszModule as NULL.

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