VirtualBox

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

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

VMM: Removed PGM_WITHOUT_MAPPINGS and associated mapping code. bugref:9517

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 64.3 KB
Line 
1/* $Id: PDMLdr.cpp 91854 2021-10-20 00:50:11Z vboxsync $ */
2/** @file
3 * PDM - Pluggable Device Manager, module loader.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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 if ( !strncmp(pszSymbol, "TM", 2)
404 || !strcmp(pszSymbol, "g_pSUPGlobalInfoPage"))
405 {
406 RTRCPTR RCPtr = 0;
407 rc = TMR3GetImportRC(pVM, pszSymbol, &RCPtr);
408 if (RT_SUCCESS(rc))
409 *pValue = RCPtr;
410 }
411 else
412 {
413 AssertMsg(!pszModule, ("Unknown builtin symbol '%s' for module '%s'!\n", pszSymbol, pModule->szName)); NOREF(pModule);
414 rc = VERR_SYMBOL_NOT_FOUND;
415 }
416 if (RT_SUCCESS(rc) || pszModule)
417 {
418 if (RT_FAILURE(rc))
419 LogRel(("PDMLdr: Couldn't find symbol '%s' in module '%s'!\n", pszSymbol, pszModule));
420 return rc;
421 }
422 }
423
424 /*
425 * Search for module.
426 */
427 PUVM pUVM = pVM->pUVM;
428 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
429 PPDMMOD pCur = pUVM->pdm.s.pModules;
430 while (pCur)
431 {
432 if ( pCur->eType == PDMMOD_TYPE_RC
433 && ( !pszModule
434 || !strcmp(pCur->szName, pszModule))
435 )
436 {
437 /* Search for the symbol. */
438 int rc = RTLdrGetSymbolEx(pCur->hLdrMod, pCur->pvBits, pCur->ImageBase, UINT32_MAX, pszSymbol, pValue);
439 if (RT_SUCCESS(rc))
440 {
441 AssertMsg(*pValue - pCur->ImageBase < RTLdrSize(pCur->hLdrMod),
442 ("%RRv-%RRv %s %RRv\n", (RTRCPTR)pCur->ImageBase,
443 (RTRCPTR)(pCur->ImageBase + RTLdrSize(pCur->hLdrMod) - 1),
444 pszSymbol, (RTRCPTR)*pValue));
445 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
446 return rc;
447 }
448 if (pszModule)
449 {
450 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
451 AssertLogRelMsgFailed(("PDMLdr: Couldn't find symbol '%s' in module '%s'!\n", pszSymbol, pszModule));
452 return VERR_SYMBOL_NOT_FOUND;
453 }
454 }
455
456 /* next */
457 pCur = pCur->pNext;
458 }
459
460 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
461 AssertLogRelMsgFailed(("Couldn't find module '%s' for resolving symbol '%s'!\n", pszModule, pszSymbol));
462 return VERR_SYMBOL_NOT_FOUND;
463}
464
465
466/**
467 * Loads a module into the raw-mode context (i.e. into the Hypervisor memory
468 * region).
469 *
470 * @returns VBox status code.
471 * @retval VINF_PDM_ALREADY_LOADED if the module is already loaded (name +
472 * filename match).
473 * @retval VERR_PDM_MODULE_NAME_CLASH if a different file has already been
474 * loaded with the name module name.
475 *
476 * @param pVM The cross context VM structure.
477 * @param pszFilename Filename of the module binary.
478 * @param pszName Module name. Case sensitive and the length is limited!
479 */
480VMMR3DECL(int) PDMR3LdrLoadRC(PVM pVM, const char *pszFilename, const char *pszName)
481{
482 /*
483 * Validate input.
484 */
485 AssertMsg(MMR3IsInitialized(pVM), ("bad init order!\n"));
486 AssertReturn(VM_IS_RAW_MODE_ENABLED(pVM), VERR_PDM_HM_IPE);
487
488 /*
489 * Find the file if not specified.
490 */
491 char *pszFile = NULL;
492 if (!pszFilename)
493 pszFilename = pszFile = pdmR3FileRC(pszName, NULL);
494
495 /*
496 * Check if a module by that name is already loaded.
497 */
498 int rc;
499 PUVM pUVM = pVM->pUVM;
500 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
501 PPDMMOD pCur = pUVM->pdm.s.pModules;
502 while (pCur)
503 {
504 if (!strcmp(pCur->szName, pszName))
505 {
506 /* Name clash. Hopefully due to it being the same file. */
507 if (!strcmp(pCur->szFilename, pszFilename))
508 rc = VINF_PDM_ALREADY_LOADED;
509 else
510 {
511 rc = VERR_PDM_MODULE_NAME_CLASH;
512 AssertMsgFailed(("We've already got a module '%s' loaded!\n", pszName));
513 }
514 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
515 RTMemTmpFree(pszFile);
516 return rc;
517 }
518 /* next */
519 pCur = pCur->pNext;
520 }
521
522 /*
523 * Allocate the module list node.
524 */
525 PPDMMOD pModule = (PPDMMOD)RTMemAllocZ(sizeof(*pModule) + strlen(pszFilename));
526 if (!pModule)
527 {
528 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
529 RTMemTmpFree(pszFile);
530 return VERR_NO_MEMORY;
531 }
532 AssertMsg(strlen(pszName) + 1 < sizeof(pModule->szName),
533 ("pazName is too long (%d chars) max is %d chars.\n", strlen(pszName), sizeof(pModule->szName) - 1));
534 strcpy(pModule->szName, pszName);
535 pModule->eType = PDMMOD_TYPE_RC;
536 strcpy(pModule->szFilename, pszFilename);
537
538
539 /*
540 * Open the loader item.
541 */
542 RTERRINFOSTATIC ErrInfo;
543 RTErrInfoInitStatic(&ErrInfo);
544 rc = SUPR3HardenedVerifyPlugIn(pszFilename, &ErrInfo.Core);
545 if (RT_SUCCESS(rc))
546 {
547 RTErrInfoClear(&ErrInfo.Core);
548 rc = RTLdrOpen(pszFilename, 0, RTLDRARCH_X86_32, &pModule->hLdrMod);
549 }
550 if (RT_SUCCESS(rc))
551 {
552 /*
553 * Allocate space in the hypervisor.
554 */
555 size_t cb = RTLdrSize(pModule->hLdrMod);
556 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
557 uint32_t cPages = (uint32_t)(cb >> PAGE_SHIFT);
558 if (((size_t)cPages << PAGE_SHIFT) == cb)
559 {
560 PSUPPAGE paPages = (PSUPPAGE)RTMemTmpAlloc(cPages * sizeof(paPages[0]));
561 if (paPages)
562 {
563 rc = SUPR3PageAllocEx(cPages, 0 /*fFlags*/, &pModule->pvBits, NULL /*pR0Ptr*/, paPages);
564 if (RT_SUCCESS(rc))
565 {
566 RTGCPTR GCPtr;
567 rc = VERR_NOT_IMPLEMENTED; //MMR3HyperMapPages(pVM, pModule->pvBits, NIL_RTR0PTR, cPages, paPages, pModule->szName, &GCPtr);
568 if (RT_SUCCESS(rc))
569 {
570 //MMR3HyperReserveFence(pVM);
571
572 /*
573 * Get relocated image bits.
574 */
575 Assert(MMHyperR3ToRC(pVM, pModule->pvBits) == GCPtr);
576 pModule->ImageBase = GCPtr;
577 PDMGETIMPORTARGS Args;
578 Args.pVM = pVM;
579 Args.pModule = pModule;
580 rc = RTLdrGetBits(pModule->hLdrMod, pModule->pvBits, pModule->ImageBase, pdmR3GetImportRC, &Args);
581 if (RT_SUCCESS(rc))
582 {
583#ifdef VBOX_WITH_DTRACE_RC
584 /*
585 * Register the tracer bits if present.
586 */
587 RTLDRADDR uValue;
588 rc = RTLdrGetSymbolEx(pModule->hLdrMod, pModule->pvBits, pModule->ImageBase, UINT32_MAX,
589 "g_VTGObjHeader", &uValue);
590 if (RT_SUCCESS(rc))
591 {
592 PVTGOBJHDR pVtgHdr = (PVTGOBJHDR)MMHyperRCToCC(pVM, (RTRCPTR)uValue);
593 if ( pVtgHdr
594 && !memcmp(pVtgHdr->szMagic, VTGOBJHDR_MAGIC, sizeof(pVtgHdr->szMagic)))
595 rc = SUPR3TracerRegisterModule(~(uintptr_t)0, pModule->szName, pVtgHdr, uValue,
596 SUP_TRACER_UMOD_FLAGS_SHARED);
597 else
598 rc = pVtgHdr ? VERR_INVALID_MAGIC : VERR_INVALID_POINTER;
599 if (RT_FAILURE(rc))
600 LogRel(("PDMLdr: Failed to register tracepoints for '%s': %Rrc\n", pModule->szName, rc));
601 }
602#endif
603
604 /*
605 * Insert the module.
606 */
607 if (pUVM->pdm.s.pModules)
608 {
609 /* we don't expect this list to be very long, so rather save the tail pointer. */
610 pCur = pUVM->pdm.s.pModules;
611 while (pCur->pNext)
612 pCur = pCur->pNext;
613 pCur->pNext = pModule;
614 }
615 else
616 pUVM->pdm.s.pModules = pModule; /* (pNext is zeroed by alloc) */
617 Log(("PDM: RC Module at %RRv %s (%s)\n", (RTRCPTR)pModule->ImageBase, pszName, pszFilename));
618
619 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
620 RTMemTmpFree(pszFile);
621 RTMemTmpFree(paPages);
622
623 return VINF_SUCCESS;
624 }
625 }
626 else
627 {
628 AssertRC(rc);
629 SUPR3PageFreeEx(pModule->pvBits, cPages);
630 }
631 }
632 else
633 AssertMsgFailed(("SUPR3PageAlloc(%d,) -> %Rrc\n", cPages, rc));
634 RTMemTmpFree(paPages);
635 }
636 else
637 rc = VERR_NO_TMP_MEMORY;
638 }
639 else
640 rc = VERR_OUT_OF_RANGE;
641 int rc2 = RTLdrClose(pModule->hLdrMod);
642 AssertRC(rc2);
643 }
644 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
645
646 /* Don't consider VERR_PDM_MODULE_NAME_CLASH and VERR_NO_MEMORY above as these are very unlikely. */
647 if (RT_FAILURE(rc) && RTErrInfoIsSet(&ErrInfo.Core))
648 rc = VMSetError(pVM, rc, RT_SRC_POS, N_("Cannot load RC module %s: %s"), pszFilename, ErrInfo.Core.pszMsg);
649 else if (RT_FAILURE(rc))
650 rc = VMSetError(pVM, rc, RT_SRC_POS, N_("Cannot load RC module %s"), pszFilename);
651
652 RTMemFree(pModule);
653 RTMemTmpFree(pszFile);
654 return rc;
655}
656
657#endif /* VBOX_WITH_RAW_MODE_KEEP */
658
659/**
660 * Loads a module into the ring-0 context.
661 *
662 * @returns VBox status code.
663 * @param pUVM Pointer to the user mode VM structure.
664 * @param pszFilename Filename of the module binary.
665 * @param pszName Module name. Case sensitive and the length is limited!
666 * @param pszSearchPath List of directories to search if @a pszFilename is
667 * not specified. Can be NULL, in which case the arch
668 * dependent install dir is searched.
669 */
670static int pdmR3LoadR0U(PUVM pUVM, const char *pszFilename, const char *pszName, const char *pszSearchPath)
671{
672 /*
673 * Validate input.
674 */
675 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
676 PPDMMOD pCur = pUVM->pdm.s.pModules;
677 while (pCur)
678 {
679 if (!strcmp(pCur->szName, pszName))
680 {
681 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
682 AssertMsgFailed(("We've already got a module '%s' loaded!\n", pszName));
683 return VERR_PDM_MODULE_NAME_CLASH;
684 }
685 /* next */
686 pCur = pCur->pNext;
687 }
688
689 /*
690 * Find the file if not specified.
691 */
692 char *pszFile = NULL;
693 if (!pszFilename)
694 pszFilename = pszFile = pdmR3FileR0(pszName, pszSearchPath);
695
696 /*
697 * Allocate the module list node.
698 */
699 PPDMMOD pModule = (PPDMMOD)RTMemAllocZ(sizeof(*pModule) + strlen(pszFilename));
700 if (!pModule)
701 {
702 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
703 RTMemTmpFree(pszFile);
704 return VERR_NO_MEMORY;
705 }
706 AssertMsg(strlen(pszName) + 1 < sizeof(pModule->szName),
707 ("pazName is too long (%d chars) max is %d chars.\n", strlen(pszName), sizeof(pModule->szName) - 1));
708 strcpy(pModule->szName, pszName);
709 pModule->eType = PDMMOD_TYPE_R0;
710 strcpy(pModule->szFilename, pszFilename);
711
712 /*
713 * Ask the support library to load it.
714 */
715 void *pvImageBase;
716 RTERRINFOSTATIC ErrInfo;
717 RTErrInfoInitStatic(&ErrInfo);
718 int rc = SUPR3LoadModule(pszFilename, pszName, &pvImageBase, &ErrInfo.Core);
719 if (RT_SUCCESS(rc))
720 {
721 pModule->hLdrMod = NIL_RTLDRMOD;
722 pModule->ImageBase = (uintptr_t)pvImageBase;
723
724 /*
725 * Insert the module.
726 */
727 if (pUVM->pdm.s.pModules)
728 {
729 /* we don't expect this list to be very long, so rather save the tail pointer. */
730 pCur = pUVM->pdm.s.pModules;
731 while (pCur->pNext)
732 pCur = pCur->pNext;
733 pCur->pNext = pModule;
734 }
735 else
736 pUVM->pdm.s.pModules = pModule; /* (pNext is zeroed by alloc) */
737 Log(("PDM: R0 Module at %RHv %s (%s)\n", (RTR0PTR)pModule->ImageBase, pszName, pszFilename));
738 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
739 RTMemTmpFree(pszFile);
740 return VINF_SUCCESS;
741 }
742
743 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
744 RTMemFree(pModule);
745 LogRel(("PDMLdr: pdmR3LoadR0U: pszName=\"%s\" rc=%Rrc szErr=\"%s\"\n", pszName, rc, ErrInfo.Core.pszMsg));
746
747 /* Don't consider VERR_PDM_MODULE_NAME_CLASH and VERR_NO_MEMORY above as these are very unlikely. */
748 if (RT_FAILURE(rc))
749 rc = VMR3SetError(pUVM, rc, RT_SRC_POS, N_("Failed to load R0 module %s: %s"), pszFilename, ErrInfo.Core.pszMsg);
750
751 RTMemTmpFree(pszFile); /* might be reference thru pszFilename in the above VMSetError call. */
752 return rc;
753}
754
755
756/**
757 * Makes sure a ring-0 module is loaded.
758 *
759 * @returns VBox status code.
760 * @param pUVM Pointer to the user mode VM structure.
761 * @param pszModule Module name (no path).
762 * @param pszSearchPath List of directories to search for the module
763 * (assumes @a pszModule is also a filename).
764 */
765VMMR3_INT_DECL(int) PDMR3LdrLoadR0(PUVM pUVM, const char *pszModule, const char *pszSearchPath)
766{
767 /*
768 * Find the module.
769 */
770 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
771 for (PPDMMOD pModule = pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
772 {
773 if ( pModule->eType == PDMMOD_TYPE_R0
774 && !strcmp(pModule->szName, pszModule))
775 {
776 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
777 return VINF_SUCCESS;
778 }
779 }
780 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
781
782 /*
783 * Okay, load it.
784 */
785 return pdmR3LoadR0U(pUVM, NULL, pszModule, pszSearchPath);
786}
787
788
789/**
790 * Get the address of a symbol in a given HC ring 3 module.
791 *
792 * @returns VBox status code.
793 * @param pVM The cross context VM structure.
794 * @param pszModule Module name.
795 * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
796 * ordinal value rather than a string pointer.
797 * @param ppvValue Where to store the symbol value.
798 */
799VMMR3_INT_DECL(int) PDMR3LdrGetSymbolR3(PVM pVM, const char *pszModule, const char *pszSymbol, void **ppvValue)
800{
801 /*
802 * Validate input.
803 */
804 AssertPtr(pVM);
805 AssertPtr(pszModule);
806 AssertPtr(ppvValue);
807 PUVM pUVM = pVM->pUVM;
808 AssertMsg(RTCritSectIsInitialized(&pUVM->pdm.s.ListCritSect), ("bad init order!\n"));
809
810 /*
811 * Find the module.
812 */
813 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
814 for (PPDMMOD pModule = pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
815 {
816 if ( pModule->eType == PDMMOD_TYPE_R3
817 && !strcmp(pModule->szName, pszModule))
818 {
819 RTUINTPTR Value = 0;
820 int rc = RTLdrGetSymbolEx(pModule->hLdrMod, pModule->pvBits, pModule->ImageBase, UINT32_MAX, pszSymbol, &Value);
821 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
822 if (RT_SUCCESS(rc))
823 {
824 *ppvValue = (void *)(uintptr_t)Value;
825 Assert((uintptr_t)*ppvValue == Value);
826 }
827 else
828 {
829 if ((uintptr_t)pszSymbol < 0x10000)
830 AssertMsg(rc, ("Couldn't symbol '%u' in module '%s'\n", (unsigned)(uintptr_t)pszSymbol, pszModule));
831 else
832 AssertMsg(rc, ("Couldn't symbol '%s' in module '%s'\n", pszSymbol, pszModule));
833 }
834 return rc;
835 }
836 }
837 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
838 AssertMsgFailed(("Couldn't locate module '%s'\n", pszModule));
839 return VERR_SYMBOL_NOT_FOUND;
840}
841
842
843/**
844 * Get the address of a symbol in a given HC ring 0 module.
845 *
846 * @returns VBox status code.
847 * @param pVM The cross context VM structure.
848 * @param pszModule Module name. If NULL the main R0 module (VMMR0.r0) is assumes.
849 * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
850 * ordinal value rather than a string pointer.
851 * @param ppvValue Where to store the symbol value.
852 */
853VMMR3DECL(int) PDMR3LdrGetSymbolR0(PVM pVM, const char *pszModule, const char *pszSymbol, PRTR0PTR ppvValue)
854{
855#ifdef PDMLDR_FAKE_MODE
856 *ppvValue = 0xdeadbeef;
857 return VINF_SUCCESS;
858
859#else
860 /*
861 * Validate input.
862 */
863 AssertPtr(pVM);
864 AssertPtrNull(pszModule);
865 AssertPtr(ppvValue);
866 PUVM pUVM = pVM->pUVM;
867 AssertMsg(RTCritSectIsInitialized(&pUVM->pdm.s.ListCritSect), ("bad init order!\n"));
868
869 if (!pszModule)
870 pszModule = VMMR0_MAIN_MODULE_NAME;
871
872 /*
873 * Find the module.
874 */
875 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
876 for (PPDMMOD pModule = pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
877 {
878 if ( pModule->eType == PDMMOD_TYPE_R0
879 && !strcmp(pModule->szName, pszModule))
880 {
881 int rc = SUPR3GetSymbolR0((void *)(uintptr_t)pModule->ImageBase, pszSymbol, (void **)ppvValue);
882 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
883 if (RT_FAILURE(rc))
884 {
885 AssertMsgRC(rc, ("Couldn't find symbol '%s' in module '%s'\n", pszSymbol, pszModule));
886 LogRel(("PDMLdr: PDMGetSymbol: Couldn't find symbol '%s' in module '%s'\n", pszSymbol, pszModule));
887 }
888 return rc;
889 }
890 }
891 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
892 AssertMsgFailed(("Couldn't locate module '%s'\n", pszModule));
893 return VERR_SYMBOL_NOT_FOUND;
894#endif
895}
896
897
898/**
899 * Same as PDMR3LdrGetSymbolR0 except that the module will be attempted loaded if not found.
900 *
901 * @returns VBox status code.
902 * @param pVM The cross context VM structure.
903 * @param pszModule Module name. If NULL the main R0 module (VMMR0.r0) is assumed.
904 * @param pszSearchPath List of directories to search if @a pszFile is
905 * not qualified with a path. Can be NULL, in which
906 * case the arch dependent install dir is searched.
907 * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
908 * ordinal value rather than a string pointer.
909 * @param ppvValue Where to store the symbol value.
910 */
911VMMR3DECL(int) PDMR3LdrGetSymbolR0Lazy(PVM pVM, const char *pszModule, const char *pszSearchPath, const char *pszSymbol,
912 PRTR0PTR ppvValue)
913{
914#ifdef PDMLDR_FAKE_MODE
915 *ppvValue = 0xdeadbeef;
916 return VINF_SUCCESS;
917
918#else
919 AssertPtr(pVM);
920 AssertPtrNull(pszModule);
921 AssertPtr(ppvValue);
922 PUVM pUVM = pVM->pUVM;
923 AssertMsg(RTCritSectIsInitialized(&pUVM->pdm.s.ListCritSect), ("bad init order!\n"));
924
925 if (pszModule) /* (We don't lazy load the main R0 module.) */
926 {
927 /*
928 * Since we're lazy, we'll only check if the module is present
929 * and hand it over to PDMR3LdrGetSymbolR0 when that's done.
930 */
931 AssertMsgReturn(!strpbrk(pszModule, "/\\:\n\r\t"), ("pszModule=%s\n", pszModule), VERR_INVALID_PARAMETER);
932 PPDMMOD pModule;
933 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
934 for (pModule = pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
935 if ( pModule->eType == PDMMOD_TYPE_R0
936 && !strcmp(pModule->szName, pszModule))
937 break;
938 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
939 if (!pModule)
940 {
941 int rc = pdmR3LoadR0U(pUVM, NULL, pszModule, pszSearchPath);
942 AssertMsgRCReturn(rc, ("pszModule=%s rc=%Rrc\n", pszModule, rc), VERR_MODULE_NOT_FOUND);
943 }
944 }
945
946 return PDMR3LdrGetSymbolR0(pVM, pszModule, pszSymbol, ppvValue);
947#endif
948}
949
950
951/**
952 * Get the address of a symbol in a given RC module.
953 *
954 * @returns VBox status code.
955 * @param pVM The cross context VM structure.
956 * @param pszModule Module name. If NULL the main R0 module (VMMRC.rc)
957 * is assumes.
958 * @param pszSymbol Symbol name. If it's value is less than 64k it's
959 * treated like a ordinal value rather than a string
960 * pointer.
961 * @param pRCPtrValue Where to store the symbol value.
962 */
963VMMR3DECL(int) PDMR3LdrGetSymbolRC(PVM pVM, const char *pszModule, const char *pszSymbol, PRTRCPTR pRCPtrValue)
964{
965#if defined(PDMLDR_FAKE_MODE) || !defined(VBOX_WITH_RAW_MODE_KEEP)
966 RT_NOREF(pVM, pszModule, pszSymbol);
967 Assert(VM_IS_RAW_MODE_ENABLED(pVM));
968 *pRCPtrValue = NIL_RTRCPTR;
969 return VINF_SUCCESS;
970
971#else
972 /*
973 * Validate input.
974 */
975 AssertPtr(pVM);
976 AssertPtrNull(pszModule);
977 AssertPtr(pRCPtrValue);
978 AssertMsg(MMR3IsInitialized(pVM), ("bad init order!\n"));
979
980 if (!pszModule)
981 pszModule = VMMRC_MAIN_MODULE_NAME;
982
983 /*
984 * Find the module.
985 */
986 PUVM pUVM = pVM->pUVM;
987 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
988 for (PPDMMOD pModule = pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
989 {
990 if ( pModule->eType == PDMMOD_TYPE_RC
991 && !strcmp(pModule->szName, pszModule))
992 {
993 RTUINTPTR Value;
994 int rc = RTLdrGetSymbolEx(pModule->hLdrMod, pModule->pvBits, pModule->ImageBase, UINT32_MAX, pszSymbol, &Value);
995 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
996 if (RT_SUCCESS(rc))
997 {
998 *pRCPtrValue = (RTGCPTR)Value;
999 Assert(*pRCPtrValue == Value);
1000 }
1001 else
1002 {
1003 if ((uintptr_t)pszSymbol < 0x10000)
1004 AssertMsg(rc, ("Couldn't symbol '%u' in module '%s'\n", (unsigned)(uintptr_t)pszSymbol, pszModule));
1005 else
1006 AssertMsg(rc, ("Couldn't symbol '%s' in module '%s'\n", pszSymbol, pszModule));
1007 }
1008 return rc;
1009 }
1010 }
1011 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
1012 AssertMsgFailed(("Couldn't locate module '%s'\n", pszModule));
1013 return VERR_SYMBOL_NOT_FOUND;
1014#endif
1015}
1016
1017
1018/**
1019 * Same as PDMR3LdrGetSymbolRC except that the module will be attempted loaded if not found.
1020 *
1021 * @returns VBox status code.
1022 * @param pVM The cross context VM structure.
1023 * @param pszModule Module name. If NULL the main RC module (VMMRC.rc)
1024 * is assumed.
1025 * @param pszSearchPath List of directories to search if @a pszFile is
1026 * not qualified with a path. Can be NULL, in which
1027 * case the arch dependent install dir is searched.
1028 * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
1029 * ordinal value rather than a string pointer.
1030 * @param pRCPtrValue Where to store the symbol value.
1031 */
1032VMMR3DECL(int) PDMR3LdrGetSymbolRCLazy(PVM pVM, const char *pszModule, const char *pszSearchPath, const char *pszSymbol,
1033 PRTRCPTR pRCPtrValue)
1034{
1035#if defined(PDMLDR_FAKE_MODE) || !defined(VBOX_WITH_RAW_MODE_KEEP)
1036 RT_NOREF(pVM, pszModule, pszSearchPath, pszSymbol);
1037 Assert(VM_IS_RAW_MODE_ENABLED(pVM));
1038 *pRCPtrValue = NIL_RTRCPTR;
1039 return VINF_SUCCESS;
1040
1041#else
1042 AssertPtr(pVM);
1043 if (!pszModule)
1044 pszModule = VMMRC_MAIN_MODULE_NAME;
1045 AssertPtr(pszModule);
1046 AssertPtr(pRCPtrValue);
1047 AssertMsg(MMR3IsInitialized(pVM), ("bad init order!\n"));
1048
1049 /*
1050 * Since we're lazy, we'll only check if the module is present
1051 * and hand it over to PDMR3LdrGetSymbolRC when that's done.
1052 */
1053 AssertMsgReturn(!strpbrk(pszModule, "/\\:\n\r\t"), ("pszModule=%s\n", pszModule), VERR_INVALID_PARAMETER);
1054 PUVM pUVM = pVM->pUVM;
1055 PPDMMOD pModule;
1056 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
1057 for (pModule = pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
1058 if ( pModule->eType == PDMMOD_TYPE_RC
1059 && !strcmp(pModule->szName, pszModule))
1060 break;
1061 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
1062 if (!pModule)
1063 {
1064 char *pszFilename = pdmR3FileRC(pszModule, pszSearchPath);
1065 AssertMsgReturn(pszFilename, ("pszModule=%s\n", pszModule), VERR_MODULE_NOT_FOUND);
1066 int rc = PDMR3LdrLoadRC(pVM, pszFilename, pszModule);
1067 RTMemTmpFree(pszFilename);
1068 AssertMsgRCReturn(rc, ("pszModule=%s rc=%Rrc\n", pszModule, rc), VERR_MODULE_NOT_FOUND);
1069 }
1070
1071 return PDMR3LdrGetSymbolRC(pVM, pszModule, pszSymbol, pRCPtrValue);
1072#endif
1073}
1074
1075
1076/**
1077 * Constructs the full filename for a R3 image file.
1078 *
1079 * @returns Pointer to temporary memory containing the filename.
1080 * Caller must free this using RTMemTmpFree().
1081 * @returns NULL on failure.
1082 *
1083 * @param pszFile File name (no path).
1084 * @param fShared If true, search in the shared directory (/usr/lib on Unix), else
1085 * search in the private directory (/usr/lib/virtualbox on Unix).
1086 * Ignored if VBOX_PATH_SHARED_LIBS is not defined.
1087 */
1088char *pdmR3FileR3(const char *pszFile, bool fShared)
1089{
1090 return pdmR3File(pszFile, NULL, NULL, fShared);
1091}
1092
1093
1094/**
1095 * Constructs the full filename for a R0 image file.
1096 *
1097 * @returns Pointer to temporary memory containing the filename.
1098 * Caller must free this using RTMemTmpFree().
1099 * @returns NULL on failure.
1100 *
1101 * @param pszFile File name (no path).
1102 * @param pszSearchPath List of directories to search if @a pszFile is
1103 * not qualified with a path. Can be NULL, in which
1104 * case the arch dependent install dir is searched.
1105 */
1106char *pdmR3FileR0(const char *pszFile, const char *pszSearchPath)
1107{
1108 return pdmR3File(pszFile, NULL, pszSearchPath, /*fShared=*/false);
1109}
1110
1111
1112/**
1113 * Constructs the full filename for a RC image file.
1114 *
1115 * @returns Pointer to temporary memory containing the filename.
1116 * Caller must free this using RTMemTmpFree().
1117 * @returns NULL on failure.
1118 *
1119 * @param pszFile File name (no path).
1120 * @param pszSearchPath List of directories to search if @a pszFile is
1121 * not qualified with a path. Can be NULL, in which
1122 * case the arch dependent install dir is searched.
1123 */
1124char *pdmR3FileRC(const char *pszFile, const char *pszSearchPath)
1125{
1126 return pdmR3File(pszFile, NULL, pszSearchPath, /*fShared=*/false);
1127}
1128
1129
1130/**
1131 * Worker for pdmR3File().
1132 *
1133 * @returns Pointer to temporary memory containing the filename.
1134 * Caller must free this using RTMemTmpFree().
1135 * @returns NULL on failure.
1136 *
1137 * @param pszDir Directory part
1138 * @param pszFile File name part
1139 * @param pszDefaultExt Extension part
1140 */
1141static char *pdmR3FileConstruct(const char *pszDir, const char *pszFile, const char *pszDefaultExt)
1142{
1143 /*
1144 * Allocate temp memory for return buffer.
1145 */
1146 size_t cchDir = strlen(pszDir);
1147 size_t cchFile = strlen(pszFile);
1148 size_t cchDefaultExt;
1149
1150 /*
1151 * Default extention?
1152 */
1153 if (!pszDefaultExt || strchr(pszFile, '.'))
1154 cchDefaultExt = 0;
1155 else
1156 cchDefaultExt = strlen(pszDefaultExt);
1157
1158 size_t cchPath = cchDir + 1 + cchFile + cchDefaultExt + 1;
1159 AssertMsgReturn(cchPath <= RTPATH_MAX, ("Path too long!\n"), NULL);
1160
1161 char *pszRet = (char *)RTMemTmpAlloc(cchDir + 1 + cchFile + cchDefaultExt + 1);
1162 AssertMsgReturn(pszRet, ("Out of temporary memory!\n"), NULL);
1163
1164 /*
1165 * Construct the filename.
1166 */
1167 memcpy(pszRet, pszDir, cchDir);
1168 pszRet[cchDir++] = '/'; /* this works everywhere */
1169 memcpy(pszRet + cchDir, pszFile, cchFile + 1);
1170 if (cchDefaultExt)
1171 memcpy(pszRet + cchDir + cchFile, pszDefaultExt, cchDefaultExt + 1);
1172
1173 return pszRet;
1174}
1175
1176
1177/**
1178 * Worker for pdmR3FileRC(), pdmR3FileR0() and pdmR3FileR3().
1179 *
1180 * @returns Pointer to temporary memory containing the filename.
1181 * Caller must free this using RTMemTmpFree().
1182 * @returns NULL on failure.
1183 * @param pszFile File name (no path).
1184 * @param pszDefaultExt The default extention, NULL if none.
1185 * @param pszSearchPath List of directories to search if @a pszFile is
1186 * not qualified with a path. Can be NULL, in which
1187 * case the arch dependent install dir is searched.
1188 * @param fShared If true, search in the shared directory (/usr/lib on Unix), else
1189 * search in the private directory (/usr/lib/virtualbox on Unix).
1190 * Ignored if VBOX_PATH_SHARED_LIBS is not defined.
1191 * @todo We'll have this elsewhere than in the root later!
1192 * @todo Remove the fShared hack again once we don't need to link against VBoxDD anymore!
1193 */
1194static char *pdmR3File(const char *pszFile, const char *pszDefaultExt, const char *pszSearchPath, bool fShared)
1195{
1196 char szPath[RTPATH_MAX];
1197 int rc;
1198
1199 AssertLogRelReturn(!fShared || !pszSearchPath, NULL);
1200 Assert(!RTPathHavePath(pszFile));
1201
1202 /*
1203 * If there is a path, search it.
1204 */
1205 if ( pszSearchPath
1206 && *pszSearchPath)
1207 {
1208 /* Check the filename length. */
1209 size_t const cchFile = strlen(pszFile);
1210 if (cchFile >= sizeof(szPath))
1211 return NULL;
1212
1213 /*
1214 * Walk the search path.
1215 */
1216 const char *psz = pszSearchPath;
1217 while (*psz)
1218 {
1219 /* Skip leading blanks - no directories with leading spaces, thank you. */
1220 while (RT_C_IS_BLANK(*psz))
1221 psz++;
1222
1223 /* Find the end of this element. */
1224 const char *pszNext;
1225 const char *pszEnd = strchr(psz, ';');
1226 if (!pszEnd)
1227 pszEnd = pszNext = strchr(psz, '\0');
1228 else
1229 pszNext = pszEnd + 1;
1230 if (pszEnd != psz)
1231 {
1232 rc = RTPathJoinEx(szPath, sizeof(szPath), psz, pszEnd - psz, pszFile, cchFile);
1233 if (RT_SUCCESS(rc))
1234 {
1235 if (RTFileExists(szPath))
1236 {
1237 size_t cchPath = strlen(szPath) + 1;
1238 char *pszRet = (char *)RTMemTmpAlloc(cchPath);
1239 if (pszRet)
1240 memcpy(pszRet, szPath, cchPath);
1241 return pszRet;
1242 }
1243 }
1244 }
1245
1246 /* advance */
1247 psz = pszNext;
1248 }
1249 }
1250
1251 /*
1252 * Use the default location.
1253 */
1254 rc = fShared
1255 ? RTPathSharedLibs( szPath, sizeof(szPath))
1256 : RTPathAppPrivateArch(szPath, sizeof(szPath));
1257 if (!RT_SUCCESS(rc))
1258 {
1259 AssertMsgFailed(("RTPath[SharedLibs|AppPrivateArch](,%d) failed rc=%d!\n", sizeof(szPath), rc));
1260 return NULL;
1261 }
1262
1263 return pdmR3FileConstruct(szPath, pszFile, pszDefaultExt);
1264}
1265
1266
1267/** @internal */
1268typedef struct QMFEIPARG
1269{
1270 RTINTPTR uPC;
1271
1272 char *pszNearSym1;
1273 size_t cchNearSym1;
1274 RTINTPTR offNearSym1;
1275
1276 char *pszNearSym2;
1277 size_t cchNearSym2;
1278 RTINTPTR offNearSym2;
1279} QMFEIPARG, *PQMFEIPARG;
1280
1281
1282/**
1283 * Enumeration callback function used by RTLdrEnumSymbols().
1284 *
1285 * @returns VBox status code. Failure will stop the enumeration.
1286 * @param hLdrMod The loader module handle.
1287 * @param pszSymbol Symbol name. NULL if ordinal only.
1288 * @param uSymbol Symbol ordinal, ~0 if not used.
1289 * @param Value Symbol value.
1290 * @param pvUser The user argument specified to RTLdrEnumSymbols().
1291 */
1292static DECLCALLBACK(int) pdmR3QueryModFromEIPEnumSymbols(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol,
1293 RTUINTPTR Value, void *pvUser)
1294{
1295 PQMFEIPARG pArgs = (PQMFEIPARG)pvUser;
1296 NOREF(hLdrMod);
1297
1298 RTINTPTR off = Value - pArgs->uPC;
1299 if (off <= 0) /* near1 is before or at same location. */
1300 {
1301 if (off > pArgs->offNearSym1)
1302 {
1303 pArgs->offNearSym1 = off;
1304 if (pArgs->pszNearSym1 && pArgs->cchNearSym1)
1305 {
1306 *pArgs->pszNearSym1 = '\0';
1307 if (pszSymbol)
1308 strncat(pArgs->pszNearSym1, pszSymbol, pArgs->cchNearSym1);
1309 else
1310 {
1311 char szOrd[32];
1312 RTStrPrintf(szOrd, sizeof(szOrd), "#%#x", uSymbol);
1313 strncat(pArgs->pszNearSym1, szOrd, pArgs->cchNearSym1);
1314 }
1315 }
1316 }
1317 }
1318 else /* near2 is after */
1319 {
1320 if (off < pArgs->offNearSym2)
1321 {
1322 pArgs->offNearSym2 = off;
1323 if (pArgs->pszNearSym2 && pArgs->cchNearSym2)
1324 {
1325 *pArgs->pszNearSym2 = '\0';
1326 if (pszSymbol)
1327 strncat(pArgs->pszNearSym2, pszSymbol, pArgs->cchNearSym2);
1328 else
1329 {
1330 char szOrd[32];
1331 RTStrPrintf(szOrd, sizeof(szOrd), "#%#x", uSymbol);
1332 strncat(pArgs->pszNearSym2, szOrd, pArgs->cchNearSym2);
1333 }
1334 }
1335 }
1336 }
1337
1338 return VINF_SUCCESS;
1339}
1340
1341
1342/**
1343 * Internal worker for PDMR3LdrQueryRCModFromPC and PDMR3LdrQueryR0ModFromPC.
1344 *
1345 * @returns VBox status code.
1346 *
1347 * @param pVM The cross context VM structure.
1348 * @param uPC The program counter (eip/rip) to locate the module for.
1349 * @param enmType The module type.
1350 * @param pszModName Where to store the module name.
1351 * @param cchModName Size of the module name buffer.
1352 * @param pMod Base address of the module.
1353 * @param pszNearSym1 Name of the closes symbol from below.
1354 * @param cchNearSym1 Size of the buffer pointed to by pszNearSym1.
1355 * @param pNearSym1 The address of pszNearSym1.
1356 * @param pszNearSym2 Name of the closes symbol from below.
1357 * @param cchNearSym2 Size of the buffer pointed to by pszNearSym2.
1358 * @param pNearSym2 The address of pszNearSym2.
1359 */
1360static int pdmR3LdrQueryModFromPC(PVM pVM, RTUINTPTR uPC, PDMMODTYPE enmType,
1361 char *pszModName, size_t cchModName, PRTUINTPTR pMod,
1362 char *pszNearSym1, size_t cchNearSym1, PRTUINTPTR pNearSym1,
1363 char *pszNearSym2, size_t cchNearSym2, PRTUINTPTR pNearSym2)
1364{
1365 PUVM pUVM = pVM->pUVM;
1366 int rc = VERR_MODULE_NOT_FOUND;
1367 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
1368 for (PPDMMOD pCur= pUVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
1369 {
1370 if (pCur->eType != enmType)
1371 continue;
1372
1373 /* The following RTLdrOpen call is a dirty hack to get ring-0 module information. */
1374 RTLDRMOD hLdrMod = pCur->hLdrMod;
1375 if (hLdrMod == NIL_RTLDRMOD && uPC >= pCur->ImageBase)
1376 {
1377 int rc2 = RTLdrOpen(pCur->szFilename, 0 /*fFlags*/, RTLDRARCH_HOST, &hLdrMod);
1378 if (RT_FAILURE(rc2))
1379 hLdrMod = NIL_RTLDRMOD;
1380 }
1381
1382 if ( hLdrMod != NIL_RTLDRMOD
1383 && uPC - pCur->ImageBase < RTLdrSize(hLdrMod))
1384 {
1385 if (pMod)
1386 *pMod = pCur->ImageBase;
1387 if (pszModName && cchModName)
1388 {
1389 *pszModName = '\0';
1390 strncat(pszModName, pCur->szName, cchModName);
1391 }
1392 if (pNearSym1) *pNearSym1 = 0;
1393 if (pNearSym2) *pNearSym2 = 0;
1394 if (pszNearSym1) *pszNearSym1 = '\0';
1395 if (pszNearSym2) *pszNearSym2 = '\0';
1396
1397 /*
1398 * Locate the nearest symbols.
1399 */
1400 QMFEIPARG Args;
1401 Args.uPC = uPC;
1402 Args.pszNearSym1 = pszNearSym1;
1403 Args.cchNearSym1 = cchNearSym1;
1404 Args.offNearSym1 = RTINTPTR_MIN;
1405 Args.pszNearSym2 = pszNearSym2;
1406 Args.cchNearSym2 = cchNearSym2;
1407 Args.offNearSym2 = RTINTPTR_MAX;
1408
1409 rc = RTLdrEnumSymbols(hLdrMod, RTLDR_ENUM_SYMBOL_FLAGS_ALL, pCur->pvBits, pCur->ImageBase,
1410 pdmR3QueryModFromEIPEnumSymbols, &Args);
1411 if (pNearSym1 && Args.offNearSym1 != RTINTPTR_MIN)
1412 *pNearSym1 = Args.offNearSym1 + uPC;
1413 if (pNearSym2 && Args.offNearSym2 != RTINTPTR_MAX)
1414 *pNearSym2 = Args.offNearSym2 + uPC;
1415
1416 rc = VINF_SUCCESS;
1417 }
1418
1419 if (hLdrMod != pCur->hLdrMod && hLdrMod != NIL_RTLDRMOD)
1420 RTLdrClose(hLdrMod);
1421
1422 if (RT_SUCCESS(rc))
1423 break;
1424 }
1425 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
1426 return rc;
1427}
1428
1429
1430/**
1431 * Queries raw-mode context module information from an PC (eip/rip).
1432 *
1433 * This is typically used to locate a crash address.
1434 *
1435 * @returns VBox status code.
1436 *
1437 * @param pVM The cross context VM structure.
1438 * @param uPC The program counter (eip/rip) to locate the module for.
1439 * @param pszModName Where to store the module name.
1440 * @param cchModName Size of the module name buffer.
1441 * @param pMod Base address of the module.
1442 * @param pszNearSym1 Name of the closes symbol from below.
1443 * @param cchNearSym1 Size of the buffer pointed to by pszNearSym1.
1444 * @param pNearSym1 The address of pszNearSym1.
1445 * @param pszNearSym2 Name of the closes symbol from below.
1446 * @param cchNearSym2 Size of the buffer pointed to by pszNearSym2.
1447 * @param pNearSym2 The address of pszNearSym2.
1448 */
1449VMMR3_INT_DECL(int) PDMR3LdrQueryRCModFromPC(PVM pVM, RTRCPTR uPC,
1450 char *pszModName, size_t cchModName, PRTRCPTR pMod,
1451 char *pszNearSym1, size_t cchNearSym1, PRTRCPTR pNearSym1,
1452 char *pszNearSym2, size_t cchNearSym2, PRTRCPTR pNearSym2)
1453{
1454 RTUINTPTR AddrMod = 0;
1455 RTUINTPTR AddrNear1 = 0;
1456 RTUINTPTR AddrNear2 = 0;
1457 int rc = pdmR3LdrQueryModFromPC(pVM, uPC, PDMMOD_TYPE_RC,
1458 pszModName, cchModName, &AddrMod,
1459 pszNearSym1, cchNearSym1, &AddrNear1,
1460 pszNearSym2, cchNearSym2, &AddrNear2);
1461 if (RT_SUCCESS(rc))
1462 {
1463 if (pMod)
1464 *pMod = (RTRCPTR)AddrMod;
1465 if (pNearSym1)
1466 *pNearSym1 = (RTRCPTR)AddrNear1;
1467 if (pNearSym2)
1468 *pNearSym2 = (RTRCPTR)AddrNear2;
1469 }
1470 return rc;
1471}
1472
1473
1474/**
1475 * Queries ring-0 context module information from an PC (eip/rip).
1476 *
1477 * This is typically used to locate a crash address.
1478 *
1479 * @returns VBox status code.
1480 *
1481 * @param pVM The cross context VM structure.
1482 * @param uPC The program counter (eip/rip) to locate the module for.
1483 * @param pszModName Where to store the module name.
1484 * @param cchModName Size of the module name buffer.
1485 * @param pMod Base address of the module.
1486 * @param pszNearSym1 Name of the closes symbol from below.
1487 * @param cchNearSym1 Size of the buffer pointed to by pszNearSym1.
1488 * @param pNearSym1 The address of pszNearSym1.
1489 * @param pszNearSym2 Name of the closes symbol from below.
1490 * @param cchNearSym2 Size of the buffer pointed to by pszNearSym2. Optional.
1491 * @param pNearSym2 The address of pszNearSym2. Optional.
1492 */
1493VMMR3_INT_DECL(int) PDMR3LdrQueryR0ModFromPC(PVM pVM, RTR0PTR uPC,
1494 char *pszModName, size_t cchModName, PRTR0PTR pMod,
1495 char *pszNearSym1, size_t cchNearSym1, PRTR0PTR pNearSym1,
1496 char *pszNearSym2, size_t cchNearSym2, PRTR0PTR pNearSym2)
1497{
1498 RTUINTPTR AddrMod = 0;
1499 RTUINTPTR AddrNear1 = 0;
1500 RTUINTPTR AddrNear2 = 0;
1501 int rc = pdmR3LdrQueryModFromPC(pVM, uPC, PDMMOD_TYPE_R0,
1502 pszModName, cchModName, &AddrMod,
1503 pszNearSym1, cchNearSym1, &AddrNear1,
1504 pszNearSym2, cchNearSym2, &AddrNear2);
1505 if (RT_SUCCESS(rc))
1506 {
1507 if (pMod)
1508 *pMod = (RTR0PTR)AddrMod;
1509 if (pNearSym1)
1510 *pNearSym1 = (RTR0PTR)AddrNear1;
1511 if (pNearSym2)
1512 *pNearSym2 = (RTR0PTR)AddrNear2;
1513 }
1514 return rc;
1515}
1516
1517
1518/**
1519 * Enumerate all PDM modules.
1520 *
1521 * @returns VBox status code.
1522 * @param pVM The cross context VM structure.
1523 * @param pfnCallback Function to call back for each of the modules.
1524 * @param pvArg User argument.
1525 */
1526VMMR3DECL(int) PDMR3LdrEnumModules(PVM pVM, PFNPDMR3ENUM pfnCallback, void *pvArg)
1527{
1528 PUVM pUVM = pVM->pUVM;
1529 int rc = VINF_SUCCESS;
1530 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
1531 for (PPDMMOD pCur = pUVM->pdm.s.pModules; pCur; pCur = pCur->pNext)
1532 {
1533 rc = pfnCallback(pVM,
1534 pCur->szFilename,
1535 pCur->szName,
1536 pCur->ImageBase,
1537 pCur->eType == PDMMOD_TYPE_RC ? RTLdrSize(pCur->hLdrMod) : 0,
1538 pCur->eType == PDMMOD_TYPE_RC ? PDMLDRCTX_RAW_MODE
1539 : pCur->eType == PDMMOD_TYPE_R0 ? PDMLDRCTX_RING_0
1540 : pCur->eType == PDMMOD_TYPE_R3 ? PDMLDRCTX_RING_3
1541 : PDMLDRCTX_INVALID,
1542 pvArg);
1543 if (RT_FAILURE(rc))
1544 break;
1545 }
1546 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
1547 return rc;
1548}
1549
1550
1551/**
1552 * Locates a module.
1553 *
1554 * @returns Pointer to the module if found.
1555 * @param pUVM Pointer to the user mode VM structure.
1556 * @param pszModule The module name.
1557 * @param enmType The module type.
1558 * @param fLazy Lazy loading the module if set.
1559 * @param pszSearchPath Search path for use when lazy loading.
1560 */
1561static PPDMMOD pdmR3LdrFindModule(PUVM pUVM, const char *pszModule, PDMMODTYPE enmType,
1562 bool fLazy, const char *pszSearchPath)
1563{
1564 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
1565 for (PPDMMOD pModule = pUVM->pdm.s.pModules; pModule; pModule = pModule->pNext)
1566 if ( pModule->eType == enmType
1567 && !strcmp(pModule->szName, pszModule))
1568 {
1569 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
1570 return pModule;
1571 }
1572 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
1573 if (fLazy)
1574 {
1575 switch (enmType)
1576 {
1577#ifdef VBOX_WITH_RAW_MODE_KEEP
1578 case PDMMOD_TYPE_RC:
1579 {
1580 char *pszFilename = pdmR3FileRC(pszModule, pszSearchPath);
1581 if (pszFilename)
1582 {
1583 int rc = PDMR3LdrLoadRC(pUVM->pVM, pszFilename, pszModule);
1584 RTMemTmpFree(pszFilename);
1585 if (RT_SUCCESS(rc))
1586 return pdmR3LdrFindModule(pUVM, pszModule, enmType, false, NULL);
1587 }
1588 break;
1589 }
1590#endif
1591
1592 case PDMMOD_TYPE_R0:
1593 {
1594 int rc = pdmR3LoadR0U(pUVM, NULL, pszModule, pszSearchPath);
1595 if (RT_SUCCESS(rc))
1596 return pdmR3LdrFindModule(pUVM, pszModule, enmType, false, NULL);
1597 break;
1598 }
1599
1600 default:
1601 AssertFailed();
1602 }
1603 }
1604 return NULL;
1605}
1606
1607
1608/**
1609 * Resolves a ring-0 or raw-mode context interface.
1610 *
1611 * @returns VBox status code.
1612 * @param pVM The cross context VM structure.
1613 * @param pvInterface Pointer to the interface structure. The symbol list
1614 * describes the layout.
1615 * @param cbInterface The size of the structure pvInterface is pointing
1616 * to. For bounds checking.
1617 * @param pszModule The module name. If NULL we assume it's the default
1618 * R0 or RC module (@a fRing0OrRC). We'll attempt to
1619 * load the module if it isn't found in the module
1620 * list.
1621 * @param pszSearchPath The module search path. If NULL, search the
1622 * architecture dependent install directory.
1623 * @param pszSymPrefix What to prefix the symbols in the list with. The
1624 * idea is that you define a list that goes with an
1625 * interface (INTERFACE_SYM_LIST) and reuse it with
1626 * each implementation.
1627 * @param pszSymList The symbol list for the interface. This is a
1628 * semi-colon separated list of symbol base names. As
1629 * mentioned above, each is prefixed with @a
1630 * pszSymPrefix before resolving. There are a couple
1631 * of special symbol names that will cause us to skip
1632 * ahead a little bit:
1633 * - U8:whatever,
1634 * - U16:whatever,
1635 * - U32:whatever,
1636 * - U64:whatever,
1637 * - RCPTR:whatever,
1638 * - R3PTR:whatever,
1639 * - R0PTR:whatever,
1640 * - GCPHYS:whatever,
1641 * - HCPHYS:whatever.
1642 * @param fRing0 Set if it's a ring-0 context interface, clear if
1643 * it's raw-mode context interface.
1644 */
1645VMMR3_INT_DECL(int) PDMR3LdrGetInterfaceSymbols(PVM pVM, void *pvInterface, size_t cbInterface,
1646 const char *pszModule, const char *pszSearchPath,
1647 const char *pszSymPrefix, const char *pszSymList,
1648 bool fRing0)
1649{
1650 bool const fNullRun = !fRing0 && !VM_IS_RAW_MODE_ENABLED(pVM);
1651
1652 /*
1653 * Find the module.
1654 */
1655 int rc = VINF_SUCCESS;
1656 PPDMMOD pModule = NULL;
1657 if (!fNullRun)
1658 pModule = pdmR3LdrFindModule(pVM->pUVM,
1659 pszModule ? pszModule : fRing0 ? "VMMR0.r0" : "VMMRC.rc",
1660 fRing0 ? PDMMOD_TYPE_R0 : PDMMOD_TYPE_RC,
1661 true /*fLazy*/, pszSearchPath);
1662 if (pModule || fNullRun)
1663 {
1664 /* Prep the symbol name. */
1665 char szSymbol[256];
1666 size_t const cchSymPrefix = strlen(pszSymPrefix);
1667 AssertReturn(cchSymPrefix + 5 < sizeof(szSymbol), VERR_SYMBOL_NOT_FOUND);
1668 memcpy(szSymbol, pszSymPrefix, cchSymPrefix);
1669
1670 /*
1671 * Iterate the symbol list.
1672 */
1673 uint32_t offInterface = 0;
1674 const char *pszCur = pszSymList;
1675 while (pszCur)
1676 {
1677 /*
1678 * Find the end of the current symbol name.
1679 */
1680 size_t cchSym;
1681 const char *pszNext = strchr(pszCur, ';');
1682 if (pszNext)
1683 {
1684 cchSym = pszNext - pszCur;
1685 pszNext++;
1686 }
1687 else
1688 cchSym = strlen(pszCur);
1689 AssertBreakStmt(cchSym > 0, rc = VERR_INVALID_PARAMETER);
1690
1691 /* Is it a skip instruction? */
1692 const char *pszColon = (const char *)memchr(pszCur, ':', cchSym);
1693 if (pszColon)
1694 {
1695 /*
1696 * String switch on the instruction and execute it, checking
1697 * that we didn't overshoot the interface structure.
1698 */
1699#define IS_SKIP_INSTR(szInstr) \
1700 ( cchSkip == sizeof(szInstr) - 1 \
1701 && !memcmp(pszCur, szInstr, sizeof(szInstr) - 1) )
1702
1703 size_t const cchSkip = pszColon - pszCur;
1704 if (IS_SKIP_INSTR("U8"))
1705 offInterface += sizeof(uint8_t);
1706 else if (IS_SKIP_INSTR("U16"))
1707 offInterface += sizeof(uint16_t);
1708 else if (IS_SKIP_INSTR("U32"))
1709 offInterface += sizeof(uint32_t);
1710 else if (IS_SKIP_INSTR("U64"))
1711 offInterface += sizeof(uint64_t);
1712 else if (IS_SKIP_INSTR("RCPTR"))
1713 offInterface += sizeof(RTRCPTR);
1714 else if (IS_SKIP_INSTR("R3PTR"))
1715 offInterface += sizeof(RTR3PTR);
1716 else if (IS_SKIP_INSTR("R0PTR"))
1717 offInterface += sizeof(RTR0PTR);
1718 else if (IS_SKIP_INSTR("HCPHYS"))
1719 offInterface += sizeof(RTHCPHYS);
1720 else if (IS_SKIP_INSTR("GCPHYS"))
1721 offInterface += sizeof(RTGCPHYS);
1722 else
1723 AssertMsgFailedBreakStmt(("Invalid skip instruction %.*s (prefix=%s)\n", cchSym, pszCur, pszSymPrefix),
1724 rc = VERR_INVALID_PARAMETER);
1725 AssertMsgBreakStmt(offInterface <= cbInterface,
1726 ("off=%#x cb=%#x (sym=%.*s prefix=%s)\n", offInterface, cbInterface, cchSym, pszCur, pszSymPrefix),
1727 rc = VERR_BUFFER_OVERFLOW);
1728#undef IS_SKIP_INSTR
1729 }
1730 else
1731 {
1732 /*
1733 * Construct the symbol name, get its value, store it and
1734 * advance the interface cursor.
1735 */
1736 AssertReturn(cchSymPrefix + cchSym < sizeof(szSymbol), VERR_SYMBOL_NOT_FOUND);
1737 memcpy(&szSymbol[cchSymPrefix], pszCur, cchSym);
1738 szSymbol[cchSymPrefix + cchSym] = '\0';
1739
1740 if (fRing0)
1741 {
1742 void *pvValue = NULL;
1743 if (!fNullRun)
1744 {
1745 rc = SUPR3GetSymbolR0((void *)(RTR0PTR)pModule->ImageBase, szSymbol, &pvValue);
1746 AssertMsgRCBreak(rc, ("Couldn't find symbol '%s' in module '%s'\n", szSymbol, pModule->szName));
1747 }
1748
1749 PRTR0PTR pValue = (PRTR0PTR)((uintptr_t)pvInterface + offInterface);
1750 AssertMsgBreakStmt(offInterface + sizeof(*pValue) <= cbInterface,
1751 ("off=%#x cb=%#x sym=%s\n", offInterface, cbInterface, szSymbol),
1752 rc = VERR_BUFFER_OVERFLOW);
1753 *pValue = (RTR0PTR)pvValue;
1754 Assert((void *)*pValue == pvValue);
1755 offInterface += sizeof(*pValue);
1756 }
1757 else
1758 {
1759 RTUINTPTR Value = 0;
1760 if (!fNullRun)
1761 {
1762 rc = RTLdrGetSymbolEx(pModule->hLdrMod, pModule->pvBits, pModule->ImageBase, UINT32_MAX, szSymbol, &Value);
1763 AssertMsgRCBreak(rc, ("Couldn't find symbol '%s' in module '%s'\n", szSymbol, pModule->szName));
1764 }
1765
1766 PRTRCPTR pValue = (PRTRCPTR)((uintptr_t)pvInterface + offInterface);
1767 AssertMsgBreakStmt(offInterface + sizeof(*pValue) <= cbInterface,
1768 ("off=%#x cb=%#x sym=%s\n", offInterface, cbInterface, szSymbol),
1769 rc = VERR_BUFFER_OVERFLOW);
1770 *pValue = (RTRCPTR)Value;
1771 Assert(*pValue == Value);
1772 offInterface += sizeof(*pValue);
1773 }
1774 }
1775
1776 /* advance */
1777 pszCur = pszNext;
1778 }
1779
1780 }
1781 else
1782 rc = VERR_MODULE_NOT_FOUND;
1783 return rc;
1784}
1785
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