VirtualBox

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

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

PDMLdr.cpp: Relax the initialization order requirements.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 61.4 KB
Line 
1/* $Id: PDMLdr.cpp 55490 2015-04-28 15:42:11Z 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, VMMGC_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.r0";
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 /*
869 * Since we're lazy, we'll only check if the module is present
870 * and hand it over to PDMR3LdrGetSymbolR0 when that's done.
871 */
872 if (pszModule)
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 return PDMR3LdrGetSymbolR0(pVM, pszModule, pszSymbol, ppvValue);
889#endif
890}
891
892
893/**
894 * Get the address of a symbol in a given RC module.
895 *
896 * @returns VBox status code.
897 * @param pVM Pointer to the VM.
898 * @param pszModule Module name. If NULL the main R0 module (VMMGC.gc) is assumes.
899 * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
900 * ordinal value rather than a string pointer.
901 * @param pRCPtrValue Where to store the symbol value.
902 */
903VMMR3DECL(int) PDMR3LdrGetSymbolRC(PVM pVM, const char *pszModule, const char *pszSymbol, PRTRCPTR pRCPtrValue)
904{
905#if defined(PDMLDR_FAKE_MODE) || !defined(VBOX_WITH_RAW_MODE)
906 Assert(!HMIsEnabled(pVM));
907 *pRCPtrValue = NIL_RTRCPTR;
908 return VINF_SUCCESS;
909
910#else
911 /*
912 * Validate input.
913 */
914 AssertPtr(pVM);
915 AssertPtrNull(pszModule);
916 AssertPtr(pRCPtrValue);
917 AssertMsg(MMR3IsInitialized(pVM), ("bad init order!\n"));
918
919 if (!pszModule)
920 pszModule = "VMMGC.gc";
921
922 /*
923 * Find the module.
924 */
925 PUVM pUVM = pVM->pUVM;
926 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
927 for (PPDMMOD pModule = pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
928 {
929 if ( pModule->eType == PDMMOD_TYPE_RC
930 && !strcmp(pModule->szName, pszModule))
931 {
932 RTUINTPTR Value;
933 int rc = RTLdrGetSymbolEx(pModule->hLdrMod, pModule->pvBits, pModule->ImageBase, UINT32_MAX, pszSymbol, &Value);
934 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
935 if (RT_SUCCESS(rc))
936 {
937 *pRCPtrValue = (RTGCPTR)Value;
938 Assert(*pRCPtrValue == Value);
939 }
940 else
941 {
942 if ((uintptr_t)pszSymbol < 0x10000)
943 AssertMsg(rc, ("Couldn't symbol '%u' in module '%s'\n", (unsigned)(uintptr_t)pszSymbol, pszModule));
944 else
945 AssertMsg(rc, ("Couldn't symbol '%s' in module '%s'\n", pszSymbol, pszModule));
946 }
947 return rc;
948 }
949 }
950 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
951 AssertMsgFailed(("Couldn't locate module '%s'\n", pszModule));
952 return VERR_SYMBOL_NOT_FOUND;
953#endif
954}
955
956
957/**
958 * Same as PDMR3LdrGetSymbolRC except that the module will be attempted loaded if not found.
959 *
960 * @returns VBox status code.
961 * @param pVM Pointer to the VM.
962 * @param pszModule Module name. If NULL the main R0 module (VMMGC.gc) is assumes.
963 * @param pszSearchPath List of directories to search if @a pszFile is
964 * not qualified with a path. Can be NULL, in which
965 * case the arch dependent install dir is searched.
966 * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
967 * ordinal value rather than a string pointer.
968 * @param pRCPtrValue Where to store the symbol value.
969 */
970VMMR3DECL(int) PDMR3LdrGetSymbolRCLazy(PVM pVM, const char *pszModule, const char *pszSearchPath, const char *pszSymbol,
971 PRTRCPTR pRCPtrValue)
972{
973#if defined(PDMLDR_FAKE_MODE) || !defined(VBOX_WITH_RAW_MODE)
974 Assert(!HMIsEnabled(pVM));
975 *pRCPtrValue = NIL_RTRCPTR;
976 return VINF_SUCCESS;
977
978#else
979 AssertPtr(pVM);
980 AssertPtrNull(pszModule);
981 AssertPtr(pRCPtrValue);
982 AssertMsg(MMR3IsInitialized(pVM), ("bad init order!\n"));
983
984 /*
985 * Since we're lazy, we'll only check if the module is present
986 * and hand it over to PDMR3LdrGetSymbolRC when that's done.
987 */
988 if (pszModule)
989 {
990 AssertMsgReturn(!strpbrk(pszModule, "/\\:\n\r\t"), ("pszModule=%s\n", pszModule), VERR_INVALID_PARAMETER);
991 PUVM pUVM = pVM->pUVM;
992 PPDMMOD pModule;
993 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
994 for (pModule = pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
995 if ( pModule->eType == PDMMOD_TYPE_RC
996 && !strcmp(pModule->szName, pszModule))
997 break;
998 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
999 if (!pModule)
1000 {
1001 char *pszFilename = pdmR3FileRC(pszModule, pszSearchPath);
1002 AssertMsgReturn(pszFilename, ("pszModule=%s\n", pszModule), VERR_MODULE_NOT_FOUND);
1003 int rc = PDMR3LdrLoadRC(pVM, pszFilename, pszModule);
1004 RTMemTmpFree(pszFilename);
1005 AssertMsgRCReturn(rc, ("pszModule=%s rc=%Rrc\n", pszModule, rc), VERR_MODULE_NOT_FOUND);
1006 }
1007 }
1008 return PDMR3LdrGetSymbolRC(pVM, pszModule, pszSymbol, pRCPtrValue);
1009#endif
1010}
1011
1012
1013/**
1014 * Constructs the full filename for a R3 image file.
1015 *
1016 * @returns Pointer to temporary memory containing the filename.
1017 * Caller must free this using RTMemTmpFree().
1018 * @returns NULL on failure.
1019 *
1020 * @param pszFile File name (no path).
1021 */
1022char *pdmR3FileR3(const char *pszFile, bool fShared)
1023{
1024 return pdmR3File(pszFile, NULL, NULL, fShared);
1025}
1026
1027
1028/**
1029 * Constructs the full filename for a R0 image file.
1030 *
1031 * @returns Pointer to temporary memory containing the filename.
1032 * Caller must free this using RTMemTmpFree().
1033 * @returns NULL on failure.
1034 *
1035 * @param pszFile File name (no path).
1036 * @param pszSearchPath List of directories to search if @a pszFile is
1037 * not qualified with a path. Can be NULL, in which
1038 * case the arch dependent install dir is searched.
1039 */
1040char *pdmR3FileR0(const char *pszFile, const char *pszSearchPath)
1041{
1042 return pdmR3File(pszFile, NULL, pszSearchPath, /*fShared=*/false);
1043}
1044
1045
1046/**
1047 * Constructs the full filename for a RC image file.
1048 *
1049 * @returns Pointer to temporary memory containing the filename.
1050 * Caller must free this using RTMemTmpFree().
1051 * @returns NULL on failure.
1052 *
1053 * @param pszFile File name (no path).
1054 * @param pszSearchPath List of directories to search if @a pszFile is
1055 * not qualified with a path. Can be NULL, in which
1056 * case the arch dependent install dir is searched.
1057 */
1058char *pdmR3FileRC(const char *pszFile, const char *pszSearchPath)
1059{
1060 return pdmR3File(pszFile, NULL, pszSearchPath, /*fShared=*/false);
1061}
1062
1063
1064/**
1065 * Worker for pdmR3File().
1066 *
1067 * @returns Pointer to temporary memory containing the filename.
1068 * Caller must free this using RTMemTmpFree().
1069 * @returns NULL on failure.
1070 *
1071 * @param pszDir Directory part
1072 * @param pszFile File name part
1073 * @param pszDefaultExt Extension part
1074 */
1075static char *pdmR3FileConstruct(const char *pszDir, const char *pszFile, const char *pszDefaultExt)
1076{
1077 /*
1078 * Allocate temp memory for return buffer.
1079 */
1080 size_t cchDir = strlen(pszDir);
1081 size_t cchFile = strlen(pszFile);
1082 size_t cchDefaultExt;
1083
1084 /*
1085 * Default extention?
1086 */
1087 if (!pszDefaultExt || strchr(pszFile, '.'))
1088 cchDefaultExt = 0;
1089 else
1090 cchDefaultExt = strlen(pszDefaultExt);
1091
1092 size_t cchPath = cchDir + 1 + cchFile + cchDefaultExt + 1;
1093 AssertMsgReturn(cchPath <= RTPATH_MAX, ("Path too long!\n"), NULL);
1094
1095 char *pszRet = (char *)RTMemTmpAlloc(cchDir + 1 + cchFile + cchDefaultExt + 1);
1096 AssertMsgReturn(pszRet, ("Out of temporary memory!\n"), NULL);
1097
1098 /*
1099 * Construct the filename.
1100 */
1101 memcpy(pszRet, pszDir, cchDir);
1102 pszRet[cchDir++] = '/'; /* this works everywhere */
1103 memcpy(pszRet + cchDir, pszFile, cchFile + 1);
1104 if (cchDefaultExt)
1105 memcpy(pszRet + cchDir + cchFile, pszDefaultExt, cchDefaultExt + 1);
1106
1107 return pszRet;
1108}
1109
1110
1111/**
1112 * Worker for pdmR3FileRC(), pdmR3FileR0() and pdmR3FileR3().
1113 *
1114 * @returns Pointer to temporary memory containing the filename.
1115 * Caller must free this using RTMemTmpFree().
1116 * @returns NULL on failure.
1117 * @param pszFile File name (no path).
1118 * @param pszDefaultExt The default extention, NULL if none.
1119 * @param pszSearchPath List of directories to search if @a pszFile is
1120 * not qualified with a path. Can be NULL, in which
1121 * case the arch dependent install dir is searched.
1122 * @param fShared If true, search in the shared directory (/usr/lib on Unix), else
1123 * search in the private directory (/usr/lib/virtualbox on Unix).
1124 * Ignored if VBOX_PATH_SHARED_LIBS is not defined.
1125 * @todo We'll have this elsewhere than in the root later!
1126 * @todo Remove the fShared hack again once we don't need to link against VBoxDD anymore!
1127 */
1128static char *pdmR3File(const char *pszFile, const char *pszDefaultExt, const char *pszSearchPath, bool fShared)
1129{
1130 char szPath[RTPATH_MAX];
1131 int rc;
1132
1133 AssertLogRelReturn(!fShared || !pszSearchPath, NULL);
1134 Assert(!RTPathHavePath(pszFile));
1135
1136 /*
1137 * If there is a path, search it.
1138 */
1139 if ( pszSearchPath
1140 && *pszSearchPath)
1141 {
1142 /* Check the filename length. */
1143 size_t const cchFile = strlen(pszFile);
1144 if (cchFile >= sizeof(szPath))
1145 return NULL;
1146
1147 /*
1148 * Walk the search path.
1149 */
1150 const char *psz = pszSearchPath;
1151 while (*psz)
1152 {
1153 /* Skip leading blanks - no directories with leading spaces, thank you. */
1154 while (RT_C_IS_BLANK(*psz))
1155 psz++;
1156
1157 /* Find the end of this element. */
1158 const char *pszNext;
1159 const char *pszEnd = strchr(psz, ';');
1160 if (!pszEnd)
1161 pszEnd = pszNext = strchr(psz, '\0');
1162 else
1163 pszNext = pszEnd + 1;
1164 if (pszEnd != psz)
1165 {
1166 rc = RTPathJoinEx(szPath, sizeof(szPath), psz, pszEnd - psz, pszFile, cchFile);
1167 if (RT_SUCCESS(rc))
1168 {
1169 if (RTFileExists(szPath))
1170 {
1171 size_t cchPath = strlen(szPath) + 1;
1172 char *pszRet = (char *)RTMemTmpAlloc(cchPath);
1173 if (pszRet)
1174 memcpy(pszRet, szPath, cchPath);
1175 return pszRet;
1176 }
1177 }
1178 }
1179
1180 /* advance */
1181 psz = pszNext;
1182 }
1183 }
1184
1185 /*
1186 * Use the default location.
1187 */
1188 rc = fShared
1189 ? RTPathSharedLibs( szPath, sizeof(szPath))
1190 : RTPathAppPrivateArch(szPath, sizeof(szPath));
1191 if (!RT_SUCCESS(rc))
1192 {
1193 AssertMsgFailed(("RTPath[SharedLibs|AppPrivateArch](,%d) failed rc=%d!\n", sizeof(szPath), rc));
1194 return NULL;
1195 }
1196
1197 return pdmR3FileConstruct(szPath, pszFile, pszDefaultExt);
1198}
1199
1200
1201/** @internal */
1202typedef struct QMFEIPARG
1203{
1204 RTINTPTR uPC;
1205
1206 char *pszNearSym1;
1207 size_t cchNearSym1;
1208 RTINTPTR offNearSym1;
1209
1210 char *pszNearSym2;
1211 size_t cchNearSym2;
1212 RTINTPTR offNearSym2;
1213} QMFEIPARG, *PQMFEIPARG;
1214
1215
1216/**
1217 * Enumeration callback function used by RTLdrEnumSymbols().
1218 *
1219 * @returns VBox status code. Failure will stop the enumeration.
1220 * @param hLdrMod The loader module handle.
1221 * @param pszSymbol Symbol name. NULL if ordinal only.
1222 * @param uSymbol Symbol ordinal, ~0 if not used.
1223 * @param Value Symbol value.
1224 * @param pvUser The user argument specified to RTLdrEnumSymbols().
1225 */
1226static DECLCALLBACK(int) pdmR3QueryModFromEIPEnumSymbols(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol,
1227 RTUINTPTR Value, void *pvUser)
1228{
1229 PQMFEIPARG pArgs = (PQMFEIPARG)pvUser;
1230 NOREF(hLdrMod);
1231
1232 RTINTPTR off = Value - pArgs->uPC;
1233 if (off <= 0) /* near1 is before or at same location. */
1234 {
1235 if (off > pArgs->offNearSym1)
1236 {
1237 pArgs->offNearSym1 = off;
1238 if (pArgs->pszNearSym1 && pArgs->cchNearSym1)
1239 {
1240 *pArgs->pszNearSym1 = '\0';
1241 if (pszSymbol)
1242 strncat(pArgs->pszNearSym1, pszSymbol, pArgs->cchNearSym1);
1243 else
1244 {
1245 char szOrd[32];
1246 RTStrPrintf(szOrd, sizeof(szOrd), "#%#x", uSymbol);
1247 strncat(pArgs->pszNearSym1, szOrd, pArgs->cchNearSym1);
1248 }
1249 }
1250 }
1251 }
1252 else /* near2 is after */
1253 {
1254 if (off < pArgs->offNearSym2)
1255 {
1256 pArgs->offNearSym2 = off;
1257 if (pArgs->pszNearSym2 && pArgs->cchNearSym2)
1258 {
1259 *pArgs->pszNearSym2 = '\0';
1260 if (pszSymbol)
1261 strncat(pArgs->pszNearSym2, pszSymbol, pArgs->cchNearSym2);
1262 else
1263 {
1264 char szOrd[32];
1265 RTStrPrintf(szOrd, sizeof(szOrd), "#%#x", uSymbol);
1266 strncat(pArgs->pszNearSym2, szOrd, pArgs->cchNearSym2);
1267 }
1268 }
1269 }
1270 }
1271
1272 return VINF_SUCCESS;
1273}
1274
1275
1276/**
1277 * Internal worker for PDMR3LdrQueryRCModFromPC and PDMR3LdrQueryR0ModFromPC.
1278 *
1279 * @returns VBox status code.
1280 *
1281 * @param pVM Pointer to the VM
1282 * @param uPC The program counter (eip/rip) to locate the module for.
1283 * @param enmType The module type.
1284 * @param pszModName Where to store the module name.
1285 * @param cchModName Size of the module name buffer.
1286 * @param pMod Base address of the module.
1287 * @param pszNearSym1 Name of the closes symbol from below.
1288 * @param cchNearSym1 Size of the buffer pointed to by pszNearSym1.
1289 * @param pNearSym1 The address of pszNearSym1.
1290 * @param pszNearSym2 Name of the closes symbol from below.
1291 * @param cchNearSym2 Size of the buffer pointed to by pszNearSym2.
1292 * @param pNearSym2 The address of pszNearSym2.
1293 */
1294static int pdmR3LdrQueryModFromPC(PVM pVM, RTUINTPTR uPC, PDMMODTYPE enmType,
1295 char *pszModName, size_t cchModName, PRTUINTPTR pMod,
1296 char *pszNearSym1, size_t cchNearSym1, PRTUINTPTR pNearSym1,
1297 char *pszNearSym2, size_t cchNearSym2, PRTUINTPTR pNearSym2)
1298{
1299 PUVM pUVM = pVM->pUVM;
1300 int rc = VERR_MODULE_NOT_FOUND;
1301 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
1302 for (PPDMMOD pCur= pUVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
1303 {
1304 if (pCur->eType != enmType)
1305 continue;
1306
1307 /* The following RTLdrOpen call is a dirty hack to get ring-0 module information. */
1308 RTLDRMOD hLdrMod = pCur->hLdrMod;
1309 if (hLdrMod == NIL_RTLDRMOD && uPC >= pCur->ImageBase)
1310 {
1311 int rc2 = RTLdrOpen(pCur->szFilename, 0 /*fFlags*/, RTLDRARCH_HOST, &hLdrMod);
1312 if (RT_FAILURE(rc2))
1313 hLdrMod = NIL_RTLDRMOD;
1314 }
1315
1316 if ( hLdrMod != NIL_RTLDRMOD
1317 && uPC - pCur->ImageBase < RTLdrSize(hLdrMod))
1318 {
1319 if (pMod)
1320 *pMod = pCur->ImageBase;
1321 if (pszModName && cchModName)
1322 {
1323 *pszModName = '\0';
1324 strncat(pszModName, pCur->szName, cchModName);
1325 }
1326 if (pNearSym1) *pNearSym1 = 0;
1327 if (pNearSym2) *pNearSym2 = 0;
1328 if (pszNearSym1) *pszNearSym1 = '\0';
1329 if (pszNearSym2) *pszNearSym2 = '\0';
1330
1331 /*
1332 * Locate the nearest symbols.
1333 */
1334 QMFEIPARG Args;
1335 Args.uPC = uPC;
1336 Args.pszNearSym1 = pszNearSym1;
1337 Args.cchNearSym1 = cchNearSym1;
1338 Args.offNearSym1 = RTINTPTR_MIN;
1339 Args.pszNearSym2 = pszNearSym2;
1340 Args.cchNearSym2 = cchNearSym2;
1341 Args.offNearSym2 = RTINTPTR_MAX;
1342
1343 rc = RTLdrEnumSymbols(hLdrMod, RTLDR_ENUM_SYMBOL_FLAGS_ALL, pCur->pvBits, pCur->ImageBase,
1344 pdmR3QueryModFromEIPEnumSymbols, &Args);
1345 if (pNearSym1 && Args.offNearSym1 != RTINTPTR_MIN)
1346 *pNearSym1 = Args.offNearSym1 + uPC;
1347 if (pNearSym2 && Args.offNearSym2 != RTINTPTR_MAX)
1348 *pNearSym2 = Args.offNearSym2 + uPC;
1349
1350 rc = VINF_SUCCESS;
1351 }
1352
1353 if (hLdrMod != pCur->hLdrMod && hLdrMod != NIL_RTLDRMOD)
1354 RTLdrClose(hLdrMod);
1355
1356 if (RT_SUCCESS(rc))
1357 break;
1358 }
1359 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
1360 return rc;
1361}
1362
1363
1364/**
1365 * Queries raw-mode context module information from an PC (eip/rip).
1366 *
1367 * This is typically used to locate a crash address.
1368 *
1369 * @returns VBox status code.
1370 *
1371 * @param pVM Pointer to the VM
1372 * @param uPC The program counter (eip/rip) to locate the module for.
1373 * @param pszModName Where to store the module name.
1374 * @param cchModName Size of the module name buffer.
1375 * @param pMod Base address of the module.
1376 * @param pszNearSym1 Name of the closes symbol from below.
1377 * @param cchNearSym1 Size of the buffer pointed to by pszNearSym1.
1378 * @param pNearSym1 The address of pszNearSym1.
1379 * @param pszNearSym2 Name of the closes symbol from below.
1380 * @param cchNearSym2 Size of the buffer pointed to by pszNearSym2.
1381 * @param pNearSym2 The address of pszNearSym2.
1382 */
1383VMMR3_INT_DECL(int) PDMR3LdrQueryRCModFromPC(PVM pVM, RTRCPTR uPC,
1384 char *pszModName, size_t cchModName, PRTRCPTR pMod,
1385 char *pszNearSym1, size_t cchNearSym1, PRTRCPTR pNearSym1,
1386 char *pszNearSym2, size_t cchNearSym2, PRTRCPTR pNearSym2)
1387{
1388 RTUINTPTR AddrMod = 0;
1389 RTUINTPTR AddrNear1 = 0;
1390 RTUINTPTR AddrNear2 = 0;
1391 int rc = pdmR3LdrQueryModFromPC(pVM, uPC, PDMMOD_TYPE_RC,
1392 pszModName, cchModName, &AddrMod,
1393 pszNearSym1, cchNearSym1, &AddrNear1,
1394 pszNearSym2, cchNearSym2, &AddrNear2);
1395 if (RT_SUCCESS(rc))
1396 {
1397 if (pMod)
1398 *pMod = (RTRCPTR)AddrMod;
1399 if (pNearSym1)
1400 *pNearSym1 = (RTRCPTR)AddrNear1;
1401 if (pNearSym2)
1402 *pNearSym2 = (RTRCPTR)AddrNear2;
1403 }
1404 return rc;
1405}
1406
1407
1408/**
1409 * Queries ring-0 context module information from an PC (eip/rip).
1410 *
1411 * This is typically used to locate a crash address.
1412 *
1413 * @returns VBox status code.
1414 *
1415 * @param pVM Pointer to the VM
1416 * @param uPC The program counter (eip/rip) to locate the module for.
1417 * @param pszModName Where to store the module name.
1418 * @param cchModName Size of the module name buffer.
1419 * @param pMod Base address of the module.
1420 * @param pszNearSym1 Name of the closes symbol from below.
1421 * @param cchNearSym1 Size of the buffer pointed to by pszNearSym1.
1422 * @param pNearSym1 The address of pszNearSym1.
1423 * @param pszNearSym2 Name of the closes symbol from below.
1424 * @param cchNearSym2 Size of the buffer pointed to by pszNearSym2. Optional.
1425 * @param pNearSym2 The address of pszNearSym2. Optional.
1426 */
1427VMMR3_INT_DECL(int) PDMR3LdrQueryR0ModFromPC(PVM pVM, RTR0PTR uPC,
1428 char *pszModName, size_t cchModName, PRTR0PTR pMod,
1429 char *pszNearSym1, size_t cchNearSym1, PRTR0PTR pNearSym1,
1430 char *pszNearSym2, size_t cchNearSym2, PRTR0PTR pNearSym2)
1431{
1432 RTUINTPTR AddrMod = 0;
1433 RTUINTPTR AddrNear1 = 0;
1434 RTUINTPTR AddrNear2 = 0;
1435 int rc = pdmR3LdrQueryModFromPC(pVM, uPC, PDMMOD_TYPE_R0,
1436 pszModName, cchModName, &AddrMod,
1437 pszNearSym1, cchNearSym1, &AddrNear1,
1438 pszNearSym2, cchNearSym2, &AddrNear2);
1439 if (RT_SUCCESS(rc))
1440 {
1441 if (pMod)
1442 *pMod = (RTR0PTR)AddrMod;
1443 if (pNearSym1)
1444 *pNearSym1 = (RTR0PTR)AddrNear1;
1445 if (pNearSym2)
1446 *pNearSym2 = (RTR0PTR)AddrNear2;
1447 }
1448 return rc;
1449}
1450
1451
1452/**
1453 * Enumerate all PDM modules.
1454 *
1455 * @returns VBox status.
1456 * @param pVM Pointer to the VM.
1457 * @param pfnCallback Function to call back for each of the modules.
1458 * @param pvArg User argument.
1459 */
1460VMMR3DECL(int) PDMR3LdrEnumModules(PVM pVM, PFNPDMR3ENUM pfnCallback, void *pvArg)
1461{
1462 PUVM pUVM = pVM->pUVM;
1463 int rc = VINF_SUCCESS;
1464 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
1465 for (PPDMMOD pCur = pUVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
1466 {
1467 rc = pfnCallback(pVM,
1468 pCur->szFilename,
1469 pCur->szName,
1470 pCur->ImageBase,
1471 pCur->eType == PDMMOD_TYPE_RC ? RTLdrSize(pCur->hLdrMod) : 0,
1472 pCur->eType == PDMMOD_TYPE_RC ? PDMLDRCTX_RAW_MODE
1473 : pCur->eType == PDMMOD_TYPE_R0 ? PDMLDRCTX_RING_0
1474 : pCur->eType == PDMMOD_TYPE_R3 ? PDMLDRCTX_RING_3
1475 : PDMLDRCTX_INVALID,
1476 pvArg);
1477 if (RT_FAILURE(rc))
1478 break;
1479 }
1480 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
1481 return rc;
1482}
1483
1484
1485/**
1486 * Locates a module.
1487 *
1488 * @returns Pointer to the module if found.
1489 * @param pUVM Pointer to the user mode VM structure.
1490 * @param pszModule The module name.
1491 * @param enmType The module type.
1492 * @param fLazy Lazy loading the module if set.
1493 * @param pszSearchPath Search path for use when lazy loading.
1494 */
1495static PPDMMOD pdmR3LdrFindModule(PUVM pUVM, const char *pszModule, PDMMODTYPE enmType,
1496 bool fLazy, const char *pszSearchPath)
1497{
1498 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
1499 for (PPDMMOD pModule = pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
1500 if ( pModule->eType == enmType
1501 && !strcmp(pModule->szName, pszModule))
1502 {
1503 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
1504 return pModule;
1505 }
1506 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
1507 if (fLazy)
1508 {
1509 switch (enmType)
1510 {
1511#ifdef VBOX_WITH_RAW_MODE
1512 case PDMMOD_TYPE_RC:
1513 {
1514 char *pszFilename = pdmR3FileRC(pszModule, pszSearchPath);
1515 if (pszFilename)
1516 {
1517 int rc = PDMR3LdrLoadRC(pUVM->pVM, pszFilename, pszModule);
1518 RTMemTmpFree(pszFilename);
1519 if (RT_SUCCESS(rc))
1520 return pdmR3LdrFindModule(pUVM, pszModule, enmType, false, NULL);
1521 }
1522 break;
1523 }
1524#endif
1525
1526 case PDMMOD_TYPE_R0:
1527 {
1528 int rc = pdmR3LoadR0U(pUVM, NULL, pszModule, pszSearchPath);
1529 if (RT_SUCCESS(rc))
1530 return pdmR3LdrFindModule(pUVM, pszModule, enmType, false, NULL);
1531 break;
1532 }
1533
1534 default:
1535 AssertFailed();
1536 }
1537 }
1538 return NULL;
1539}
1540
1541
1542/**
1543 * Resolves a ring-0 or raw-mode context interface.
1544 *
1545 * @returns VBox status code.
1546 * @param pVM Pointer to the VM.
1547 * @param pvInterface Pointer to the interface structure. The symbol list
1548 * describes the layout.
1549 * @param cbInterface The size of the structure pvInterface is pointing
1550 * to. For bounds checking.
1551 * @param pszModule The module name. If NULL we assume it's the default
1552 * R0 or RC module (@a fRing0OrRC). We'll attempt to
1553 * load the module if it isn't found in the module
1554 * list.
1555 * @param pszSearchPath The module search path. If NULL, search the
1556 * architecture dependent install directory.
1557 * @param pszSymPrefix What to prefix the symbols in the list with. The
1558 * idea is that you define a list that goes with an
1559 * interface (INTERFACE_SYM_LIST) and reuse it with
1560 * each implementation.
1561 * @param pszSymList The symbol list for the interface. This is a
1562 * semi-colon separated list of symbol base names. As
1563 * mentioned above, each is prefixed with @a
1564 * pszSymPrefix before resolving. There are a couple
1565 * of special symbol names that will cause us to skip
1566 * ahead a little bit:
1567 * - U8:whatever,
1568 * - U16:whatever,
1569 * - U32:whatever,
1570 * - U64:whatever,
1571 * - RCPTR:whatever,
1572 * - R3PTR:whatever,
1573 * - R0PTR:whatever,
1574 * - GCPHYS:whatever,
1575 * - HCPHYS:whatever.
1576 * @param fRing0 Set if it's a ring-0 context interface, clear if
1577 * it's raw-mode context interface.
1578 */
1579VMMR3_INT_DECL(int) PDMR3LdrGetInterfaceSymbols(PVM pVM, void *pvInterface, size_t cbInterface,
1580 const char *pszModule, const char *pszSearchPath,
1581 const char *pszSymPrefix, const char *pszSymList,
1582 bool fRing0)
1583{
1584 bool const fNullRun = !fRing0 && HMIsEnabled(pVM);
1585
1586 /*
1587 * Find the module.
1588 */
1589 int rc = VINF_SUCCESS;
1590 PPDMMOD pModule = NULL;
1591 if (!fNullRun)
1592 pModule = pdmR3LdrFindModule(pVM->pUVM,
1593 pszModule ? pszModule : fRing0 ? "VMMR0.r0" : "VMMGC.gc",
1594 fRing0 ? PDMMOD_TYPE_R0 : PDMMOD_TYPE_RC,
1595 true /*fLazy*/, pszSearchPath);
1596 if (pModule || fNullRun)
1597 {
1598 /* Prep the symbol name. */
1599 char szSymbol[256];
1600 size_t const cchSymPrefix = strlen(pszSymPrefix);
1601 AssertReturn(cchSymPrefix + 5 < sizeof(szSymbol), VERR_SYMBOL_NOT_FOUND);
1602 memcpy(szSymbol, pszSymPrefix, cchSymPrefix);
1603
1604 /*
1605 * Iterate the symbol list.
1606 */
1607 uint32_t offInterface = 0;
1608 const char *pszCur = pszSymList;
1609 while (pszCur)
1610 {
1611 /*
1612 * Find the end of the current symbol name.
1613 */
1614 size_t cchSym;
1615 const char *pszNext = strchr(pszCur, ';');
1616 if (pszNext)
1617 {
1618 cchSym = pszNext - pszCur;
1619 pszNext++;
1620 }
1621 else
1622 cchSym = strlen(pszCur);
1623 AssertBreakStmt(cchSym > 0, rc = VERR_INVALID_PARAMETER);
1624
1625 /* Is it a skip instruction? */
1626 const char *pszColon = (const char *)memchr(pszCur, ':', cchSym);
1627 if (pszColon)
1628 {
1629 /*
1630 * String switch on the instruction and execute it, checking
1631 * that we didn't overshoot the interface structure.
1632 */
1633#define IS_SKIP_INSTR(szInstr) \
1634 ( cchSkip == sizeof(szInstr) - 1 \
1635 && !memcmp(pszCur, szInstr, sizeof(szInstr) - 1) )
1636
1637 size_t const cchSkip = pszColon - pszCur;
1638 if (IS_SKIP_INSTR("U8"))
1639 offInterface += sizeof(uint8_t);
1640 else if (IS_SKIP_INSTR("U16"))
1641 offInterface += sizeof(uint16_t);
1642 else if (IS_SKIP_INSTR("U32"))
1643 offInterface += sizeof(uint32_t);
1644 else if (IS_SKIP_INSTR("U64"))
1645 offInterface += sizeof(uint64_t);
1646 else if (IS_SKIP_INSTR("RCPTR"))
1647 offInterface += sizeof(RTRCPTR);
1648 else if (IS_SKIP_INSTR("R3PTR"))
1649 offInterface += sizeof(RTR3PTR);
1650 else if (IS_SKIP_INSTR("R0PTR"))
1651 offInterface += sizeof(RTR0PTR);
1652 else if (IS_SKIP_INSTR("HCPHYS"))
1653 offInterface += sizeof(RTHCPHYS);
1654 else if (IS_SKIP_INSTR("GCPHYS"))
1655 offInterface += sizeof(RTGCPHYS);
1656 else
1657 AssertMsgFailedBreakStmt(("Invalid skip instruction %.*s (prefix=%s)\n", cchSym, pszCur, pszSymPrefix),
1658 rc = VERR_INVALID_PARAMETER);
1659 AssertMsgBreakStmt(offInterface <= cbInterface,
1660 ("off=%#x cb=%#x (sym=%.*s prefix=%s)\n", offInterface, cbInterface, cchSym, pszCur, pszSymPrefix),
1661 rc = VERR_BUFFER_OVERFLOW);
1662#undef IS_SKIP_INSTR
1663 }
1664 else
1665 {
1666 /*
1667 * Construct the symbol name, get its value, store it and
1668 * advance the interface cursor.
1669 */
1670 AssertReturn(cchSymPrefix + cchSym < sizeof(szSymbol), VERR_SYMBOL_NOT_FOUND);
1671 memcpy(&szSymbol[cchSymPrefix], pszCur, cchSym);
1672 szSymbol[cchSymPrefix + cchSym] = '\0';
1673
1674 if (fRing0)
1675 {
1676 void *pvValue = NULL;
1677 if (!fNullRun)
1678 {
1679 rc = SUPR3GetSymbolR0((void *)(RTR0PTR)pModule->ImageBase, szSymbol, &pvValue);
1680 AssertMsgRCBreak(rc, ("Couldn't find symbol '%s' in module '%s'\n", szSymbol, pModule->szName));
1681 }
1682
1683 PRTR0PTR pValue = (PRTR0PTR)((uintptr_t)pvInterface + offInterface);
1684 AssertMsgBreakStmt(offInterface + sizeof(*pValue) <= cbInterface,
1685 ("off=%#x cb=%#x sym=%s\n", offInterface, cbInterface, szSymbol),
1686 rc = VERR_BUFFER_OVERFLOW);
1687 *pValue = (RTR0PTR)pvValue;
1688 Assert((void *)*pValue == pvValue);
1689 offInterface += sizeof(*pValue);
1690 }
1691 else
1692 {
1693 RTUINTPTR Value = 0;
1694 if (!fNullRun)
1695 {
1696 rc = RTLdrGetSymbolEx(pModule->hLdrMod, pModule->pvBits, pModule->ImageBase, UINT32_MAX, szSymbol, &Value);
1697 AssertMsgRCBreak(rc, ("Couldn't find symbol '%s' in module '%s'\n", szSymbol, pModule->szName));
1698 }
1699
1700 PRTRCPTR pValue = (PRTRCPTR)((uintptr_t)pvInterface + offInterface);
1701 AssertMsgBreakStmt(offInterface + sizeof(*pValue) <= cbInterface,
1702 ("off=%#x cb=%#x sym=%s\n", offInterface, cbInterface, szSymbol),
1703 rc = VERR_BUFFER_OVERFLOW);
1704 *pValue = (RTRCPTR)Value;
1705 Assert(*pValue == Value);
1706 offInterface += sizeof(*pValue);
1707 }
1708 }
1709
1710 /* advance */
1711 pszCur = pszNext;
1712 }
1713
1714 }
1715 else
1716 rc = VERR_MODULE_NOT_FOUND;
1717 return rc;
1718}
1719
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