VirtualBox

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

Last change on this file since 56296 was 56287, checked in by vboxsync, 9 years ago

VMM: Updated (C) year.

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