VirtualBox

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

Last change on this file since 93718 was 93718, checked in by vboxsync, 3 years ago

VMM/MM: Removed the hyper heap. bugref:10093 bugref:9517

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