VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/PDMDriver.cpp@ 39839

Last change on this file since 39839 was 39839, checked in by vboxsync, 13 years ago

PDM: Initial driver chain transformation code (untested).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 65.1 KB
Line 
1/* $Id: PDMDriver.cpp 39839 2012-01-23 16:05:57Z vboxsync $ */
2/** @file
3 * PDM - Pluggable Device and Driver Manager, Driver parts.
4 */
5
6/*
7 * Copyright (C) 2006-2010 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
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_PDM_DRIVER
23#include "PDMInternal.h"
24#include <VBox/vmm/pdm.h>
25#include <VBox/vmm/mm.h>
26#include <VBox/vmm/cfgm.h>
27#include <VBox/vmm/vmm.h>
28#include <VBox/sup.h>
29#include <VBox/vmm/vm.h>
30#include <VBox/version.h>
31#include <VBox/err.h>
32
33#include <VBox/log.h>
34#include <iprt/assert.h>
35#include <iprt/asm.h>
36#include <iprt/ctype.h>
37#include <iprt/mem.h>
38#include <iprt/thread.h>
39#include <iprt/path.h>
40#include <iprt/string.h>
41
42
43/*******************************************************************************
44* Structures and Typedefs *
45*******************************************************************************/
46/**
47 * Internal callback structure pointer.
48 *
49 * The main purpose is to define the extra data we associate
50 * with PDMDRVREGCB so we can find the VM instance and so on.
51 */
52typedef struct PDMDRVREGCBINT
53{
54 /** The callback structure. */
55 PDMDRVREGCB Core;
56 /** A bit of padding. */
57 uint32_t u32[4];
58 /** VM Handle. */
59 PVM pVM;
60 /** Pointer to the configuration node the registrations should be
61 * associated with. Can be NULL. */
62 PCFGMNODE pCfgNode;
63} PDMDRVREGCBINT, *PPDMDRVREGCBINT;
64typedef const PDMDRVREGCBINT *PCPDMDRVREGCBINT;
65
66
67/*******************************************************************************
68* Internal Functions *
69*******************************************************************************/
70static DECLCALLBACK(int) pdmR3DrvRegister(PCPDMDRVREGCB pCallbacks, PCPDMDRVREG pReg);
71static int pdmR3DrvLoad(PVM pVM, PPDMDRVREGCBINT pRegCB, const char *pszFilename, const char *pszName);
72
73
74/**
75 * Register drivers in a statically linked environment.
76 *
77 * @returns VBox status code.
78 * @param pVM The VM to operate on.
79 * @param pfnCallback Driver registration callback
80 */
81VMMR3DECL(int) PDMR3DrvStaticRegistration(PVM pVM, FNPDMVBOXDRIVERSREGISTER pfnCallback)
82{
83 /*
84 * The registration callbacks.
85 */
86 PDMDRVREGCBINT RegCB;
87 RegCB.Core.u32Version = PDM_DRVREG_CB_VERSION;
88 RegCB.Core.pfnRegister = pdmR3DrvRegister;
89 RegCB.pVM = pVM;
90 RegCB.pCfgNode = NULL;
91
92 int rc = pfnCallback(&RegCB.Core, VBOX_VERSION);
93 if (RT_FAILURE(rc))
94 AssertMsgFailed(("VBoxDriversRegister failed with rc=%Rrc\n"));
95
96 return rc;
97}
98
99
100/**
101 * This function will initialize the drivers for this VM instance.
102 *
103 * First of all this mean loading the builtin drivers and letting them
104 * register themselves. Beyond that any additional driver modules are
105 * loaded and called for registration.
106 *
107 * @returns VBox status code.
108 * @param pVM VM Handle.
109 */
110int pdmR3DrvInit(PVM pVM)
111{
112 LogFlow(("pdmR3DrvInit:\n"));
113
114 AssertRelease(!(RT_OFFSETOF(PDMDRVINS, achInstanceData) & 15));
115 PPDMDRVINS pDrvInsAssert; NOREF(pDrvInsAssert);
116 AssertCompile(sizeof(pDrvInsAssert->Internal.s) <= sizeof(pDrvInsAssert->Internal.padding));
117 AssertRelease(sizeof(pDrvInsAssert->Internal.s) <= sizeof(pDrvInsAssert->Internal.padding));
118
119 /*
120 * The registration callbacks.
121 */
122 PDMDRVREGCBINT RegCB;
123 RegCB.Core.u32Version = PDM_DRVREG_CB_VERSION;
124 RegCB.Core.pfnRegister = pdmR3DrvRegister;
125 RegCB.pVM = pVM;
126 RegCB.pCfgNode = NULL;
127
128 /*
129 * Load the builtin module
130 */
131 PCFGMNODE pDriversNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "PDM/Drivers");
132 bool fLoadBuiltin;
133 int rc = CFGMR3QueryBool(pDriversNode, "LoadBuiltin", &fLoadBuiltin);
134 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
135 fLoadBuiltin = true;
136 else if (RT_FAILURE(rc))
137 {
138 AssertMsgFailed(("Configuration error: Querying boolean \"LoadBuiltin\" failed with %Rrc\n", rc));
139 return rc;
140 }
141 if (fLoadBuiltin)
142 {
143 /* make filename */
144 char *pszFilename = pdmR3FileR3("VBoxDD", true /*fShared*/);
145 if (!pszFilename)
146 return VERR_NO_TMP_MEMORY;
147 rc = pdmR3DrvLoad(pVM, &RegCB, pszFilename, "VBoxDD");
148 RTMemTmpFree(pszFilename);
149 if (RT_FAILURE(rc))
150 return rc;
151 }
152
153 /*
154 * Load additional driver modules.
155 */
156 for (PCFGMNODE pCur = CFGMR3GetFirstChild(pDriversNode); pCur; pCur = CFGMR3GetNextChild(pCur))
157 {
158 /*
159 * Get the name and path.
160 */
161 char szName[PDMMOD_NAME_LEN];
162 rc = CFGMR3GetName(pCur, &szName[0], sizeof(szName));
163 if (rc == VERR_CFGM_NOT_ENOUGH_SPACE)
164 {
165 AssertMsgFailed(("configuration error: The module name is too long, cchName=%zu.\n", CFGMR3GetNameLen(pCur)));
166 return VERR_PDM_MODULE_NAME_TOO_LONG;
167 }
168 else if (RT_FAILURE(rc))
169 {
170 AssertMsgFailed(("CFGMR3GetName -> %Rrc.\n", rc));
171 return rc;
172 }
173
174 /* the path is optional, if no path the module name + path is used. */
175 char szFilename[RTPATH_MAX];
176 rc = CFGMR3QueryString(pCur, "Path", &szFilename[0], sizeof(szFilename));
177 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
178 strcpy(szFilename, szName);
179 else if (RT_FAILURE(rc))
180 {
181 AssertMsgFailed(("configuration error: Failure to query the module path, rc=%Rrc.\n", rc));
182 return rc;
183 }
184
185 /* prepend path? */
186 if (!RTPathHavePath(szFilename))
187 {
188 char *psz = pdmR3FileR3(szFilename, false /*fShared*/);
189 if (!psz)
190 return VERR_NO_TMP_MEMORY;
191 size_t cch = strlen(psz) + 1;
192 if (cch > sizeof(szFilename))
193 {
194 RTMemTmpFree(psz);
195 AssertMsgFailed(("Filename too long! cch=%d '%s'\n", cch, psz));
196 return VERR_FILENAME_TOO_LONG;
197 }
198 memcpy(szFilename, psz, cch);
199 RTMemTmpFree(psz);
200 }
201
202 /*
203 * Load the module and register it's drivers.
204 */
205 RegCB.pCfgNode = pCur;
206 rc = pdmR3DrvLoad(pVM, &RegCB, szFilename, szName);
207 if (RT_FAILURE(rc))
208 return rc;
209 }
210
211 LogFlow(("pdmR3DrvInit: returns VINF_SUCCESS\n"));
212 return VINF_SUCCESS;
213}
214
215
216/**
217 * Loads one driver module and call the registration entry point.
218 *
219 * @returns VBox status code.
220 * @param pVM VM handle.
221 * @param pRegCB The registration callback stuff.
222 * @param pszFilename Module filename.
223 * @param pszName Module name.
224 */
225static int pdmR3DrvLoad(PVM pVM, PPDMDRVREGCBINT pRegCB, const char *pszFilename, const char *pszName)
226{
227 /*
228 * Load it.
229 */
230 int rc = pdmR3LoadR3U(pVM->pUVM, pszFilename, pszName);
231 if (RT_SUCCESS(rc))
232 {
233 /*
234 * Get the registration export and call it.
235 */
236 FNPDMVBOXDRIVERSREGISTER *pfnVBoxDriversRegister;
237 rc = PDMR3LdrGetSymbolR3(pVM, pszName, "VBoxDriversRegister", (void **)&pfnVBoxDriversRegister);
238 if (RT_SUCCESS(rc))
239 {
240 Log(("PDM: Calling VBoxDriversRegister (%p) of %s (%s)\n", pfnVBoxDriversRegister, pszName, pszFilename));
241 rc = pfnVBoxDriversRegister(&pRegCB->Core, VBOX_VERSION);
242 if (RT_SUCCESS(rc))
243 Log(("PDM: Successfully loaded driver module %s (%s).\n", pszName, pszFilename));
244 else
245 AssertMsgFailed(("VBoxDriversRegister failed with rc=%Rrc\n"));
246 }
247 else
248 {
249 AssertMsgFailed(("Failed to locate 'VBoxDriversRegister' in %s (%s) rc=%Rrc\n", pszName, pszFilename, rc));
250 if (rc == VERR_SYMBOL_NOT_FOUND)
251 rc = VERR_PDM_NO_REGISTRATION_EXPORT;
252 }
253 }
254 else
255 AssertMsgFailed(("Failed to load %s (%s) rc=%Rrc!\n", pszName, pszFilename, rc));
256 return rc;
257}
258
259
260/** @interface_method_impl{PDMDRVREGCB,pfnRegister} */
261static DECLCALLBACK(int) pdmR3DrvRegister(PCPDMDRVREGCB pCallbacks, PCPDMDRVREG pReg)
262{
263 /*
264 * Validate the registration structure.
265 */
266 AssertPtrReturn(pReg, VERR_INVALID_POINTER);
267 AssertMsgReturn(pReg->u32Version == PDM_DRVREG_VERSION,
268 ("%#x\n", pReg->u32Version),
269 VERR_PDM_UNKNOWN_DRVREG_VERSION);
270 AssertReturn(pReg->szName[0], VERR_PDM_INVALID_DRIVER_REGISTRATION);
271 AssertMsgReturn(RTStrEnd(pReg->szName, sizeof(pReg->szName)),
272 ("%.*s\n", sizeof(pReg->szName), pReg->szName),
273 VERR_PDM_INVALID_DRIVER_REGISTRATION);
274 AssertMsgReturn(pdmR3IsValidName(pReg->szName), ("%.*s\n", pReg->szName),
275 VERR_PDM_INVALID_DRIVER_REGISTRATION);
276 AssertMsgReturn( !(pReg->fFlags & PDM_DRVREG_FLAGS_R0)
277 || ( pReg->szR0Mod[0]
278 && RTStrEnd(pReg->szR0Mod, sizeof(pReg->szR0Mod))),
279 ("%s: %.*s\n", pReg->szName, sizeof(pReg->szR0Mod), pReg->szR0Mod),
280 VERR_PDM_INVALID_DRIVER_REGISTRATION);
281 AssertMsgReturn( !(pReg->fFlags & PDM_DRVREG_FLAGS_RC)
282 || ( pReg->szRCMod[0]
283 && RTStrEnd(pReg->szRCMod, sizeof(pReg->szRCMod))),
284 ("%s: %.*s\n", pReg->szName, sizeof(pReg->szRCMod), pReg->szRCMod),
285 VERR_PDM_INVALID_DRIVER_REGISTRATION);
286 AssertMsgReturn(VALID_PTR(pReg->pszDescription),
287 ("%s: %p\n", pReg->szName, pReg->pszDescription),
288 VERR_PDM_INVALID_DRIVER_REGISTRATION);
289 AssertMsgReturn(!(pReg->fFlags & ~(PDM_DRVREG_FLAGS_HOST_BITS_MASK | PDM_DRVREG_FLAGS_R0 | PDM_DRVREG_FLAGS_RC)),
290 ("%s: %#x\n", pReg->szName, pReg->fFlags),
291 VERR_PDM_INVALID_DRIVER_REGISTRATION);
292 AssertMsgReturn((pReg->fFlags & PDM_DRVREG_FLAGS_HOST_BITS_MASK) == PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
293 ("%s: %#x\n", pReg->szName, pReg->fFlags),
294 VERR_PDM_INVALID_DRIVER_HOST_BITS);
295 AssertMsgReturn(pReg->cMaxInstances > 0,
296 ("%s: %#x\n", pReg->szName, pReg->cMaxInstances),
297 VERR_PDM_INVALID_DRIVER_REGISTRATION);
298 AssertMsgReturn(pReg->cbInstance <= _1M,
299 ("%s: %#x\n", pReg->szName, pReg->cbInstance),
300 VERR_PDM_INVALID_DRIVER_REGISTRATION);
301 AssertMsgReturn(VALID_PTR(pReg->pfnConstruct),
302 ("%s: %p\n", pReg->szName, pReg->pfnConstruct),
303 VERR_PDM_INVALID_DRIVER_REGISTRATION);
304 AssertMsgReturn(VALID_PTR(pReg->pfnRelocate) || !(pReg->fFlags & PDM_DRVREG_FLAGS_RC),
305 ("%s: %#x\n", pReg->szName, pReg->cbInstance),
306 VERR_PDM_INVALID_DRIVER_REGISTRATION);
307 AssertMsgReturn(pReg->pfnSoftReset == NULL,
308 ("%s: %p\n", pReg->szName, pReg->pfnSoftReset),
309 VERR_PDM_INVALID_DRIVER_REGISTRATION);
310 AssertMsgReturn(pReg->u32VersionEnd == PDM_DRVREG_VERSION,
311 ("%s: #x\n", pReg->szName, pReg->u32VersionEnd),
312 VERR_PDM_INVALID_DRIVER_REGISTRATION);
313
314 /*
315 * Check for duplicate and find FIFO entry at the same time.
316 */
317 PCPDMDRVREGCBINT pRegCB = (PCPDMDRVREGCBINT)pCallbacks;
318 PPDMDRV pDrvPrev = NULL;
319 PPDMDRV pDrv = pRegCB->pVM->pdm.s.pDrvs;
320 for (; pDrv; pDrvPrev = pDrv, pDrv = pDrv->pNext)
321 {
322 if (!strcmp(pDrv->pReg->szName, pReg->szName))
323 {
324 AssertMsgFailed(("Driver '%s' already exists\n", pReg->szName));
325 return VERR_PDM_DRIVER_NAME_CLASH;
326 }
327 }
328
329 /*
330 * Allocate new driver structure and insert it into the list.
331 */
332 int rc;
333 pDrv = (PPDMDRV)MMR3HeapAlloc(pRegCB->pVM, MM_TAG_PDM_DRIVER, sizeof(*pDrv));
334 if (pDrv)
335 {
336 pDrv->pNext = NULL;
337 pDrv->cInstances = 0;
338 pDrv->iNextInstance = 0;
339 pDrv->pReg = pReg;
340 rc = CFGMR3QueryStringAllocDef( pRegCB->pCfgNode, "RCSearchPath", &pDrv->pszRCSearchPath, NULL);
341 if (RT_SUCCESS(rc))
342 rc = CFGMR3QueryStringAllocDef(pRegCB->pCfgNode, "R0SearchPath", &pDrv->pszR0SearchPath, NULL);
343 if (RT_SUCCESS(rc))
344 {
345 if (pDrvPrev)
346 pDrvPrev->pNext = pDrv;
347 else
348 pRegCB->pVM->pdm.s.pDrvs = pDrv;
349 Log(("PDM: Registered driver '%s'\n", pReg->szName));
350 return VINF_SUCCESS;
351 }
352 MMR3HeapFree(pDrv);
353 }
354 else
355 rc = VERR_NO_MEMORY;
356 return rc;
357}
358
359
360/**
361 * Lookups a driver structure by name.
362 * @internal
363 */
364PPDMDRV pdmR3DrvLookup(PVM pVM, const char *pszName)
365{
366 for (PPDMDRV pDrv = pVM->pdm.s.pDrvs; pDrv; pDrv = pDrv->pNext)
367 if (!strcmp(pDrv->pReg->szName, pszName))
368 return pDrv;
369 return NULL;
370}
371
372
373/**
374 * Transforms the driver chain as it's being instantiated.
375 *
376 * Worker for pdmR3DrvInstantiate.
377 *
378 * @returns VBox status code.
379 * @param pVM The VM handle.
380 * @param pDrvAbove The driver above, NULL if top.
381 * @param pLun The LUN.
382 * @param ppNode The AttachedDriver node, replaced if any
383 * morphing took place.
384 */
385static int pdmR3DrvMaybeTransformChain(PVM pVM, PPDMDRVINS pDrvAbove, PPDMLUN pLun, PCFGMNODE *ppNode)
386{
387 /*
388 * The typical state of affairs is that there are no injections.
389 */
390 PCFGMNODE pCurTrans = CFGMR3GetFirstChild(CFGMR3GetChild(CFGMR3GetRoot(pVM), "PDM/DriverTransformations"));
391 if (!pCurTrans)
392 return VINF_SUCCESS;
393
394 /*
395 * Gather the attributes used in the matching process.
396 */
397 const char *pszDevice = pLun->pDevIns->Internal.s.pDevR3->pReg->szName;
398 char szLun[32];
399 RTStrPrintf(szLun, sizeof(szLun), "%u", pLun->iLun);
400 const char *pszAbove = pDrvAbove ? pDrvAbove->Internal.s.pDrv->pReg->szName : "<top>";
401 char *pszThisDrv;
402 int rc = CFGMR3QueryStringAlloc(*ppNode, "Driver", &pszThisDrv);
403 AssertMsgRCReturn(rc, ("Query for string value of \"Driver\" -> %Rrc\n", rc),
404 rc == VERR_CFGM_VALUE_NOT_FOUND ? VERR_PDM_CFG_MISSING_DRIVER_NAME : rc);
405
406
407 /*
408 * Enumerate possible driver chain transformations.
409 */
410 for (; pCurTrans != NULL; pCurTrans = CFGMR3GetNextChild(pCurTrans))
411 {
412 /* Match against the driver multi pattern. */
413 char *pszMultiPat;
414 rc = CFGMR3QueryStringAllocDef(pCurTrans, "Driver", &pszMultiPat, "*");
415 AssertLogRelRCReturn(rc, rc);
416 bool fMatch = RTStrSimplePatternMultiMatch(pszMultiPat, RTSTR_MAX, pszDevice, RTSTR_MAX, NULL);
417 MMR3HeapFree(pszMultiPat);
418 if (!fMatch)
419 continue;
420
421 /* Match against the lun multi pattern. */
422 rc = CFGMR3QueryStringAllocDef(pCurTrans, "LUN", &pszMultiPat, "*");
423 AssertLogRelRCReturn(rc, rc);
424 fMatch = RTStrSimplePatternMultiMatch(pszMultiPat, RTSTR_MAX, szLun, RTSTR_MAX, NULL);
425 MMR3HeapFree(pszMultiPat);
426 if (!fMatch)
427 continue;
428
429 /* Match against the below-driver multi pattern. */
430 rc = CFGMR3QueryStringAllocDef(pCurTrans, "BelowDriver", &pszMultiPat, "*");
431 AssertLogRelRCReturn(rc, rc);
432 fMatch = RTStrSimplePatternMultiMatch(pszMultiPat, RTSTR_MAX, pszAbove, RTSTR_MAX, NULL);
433 MMR3HeapFree(pszMultiPat);
434 if (!fMatch)
435 continue;
436
437 /* Match against the above-driver multi pattern. */
438 /** @todo add some config to avoid infinie application of an
439 * AboveDriver transformation. */
440 rc = CFGMR3QueryStringAllocDef(pCurTrans, "AboveDriver", &pszMultiPat, "*");
441 AssertLogRelRCReturn(rc, rc);
442 fMatch = RTStrSimplePatternMultiMatch(pszMultiPat, RTSTR_MAX, pszThisDrv, RTSTR_MAX, NULL);
443 MMR3HeapFree(pszMultiPat);
444 if (!fMatch)
445 continue;
446
447 /*
448 * We've got a match! Now, what are we supposed to do?
449 */
450 char szAction[16];
451 rc = CFGMR3QueryStringDef(pCurTrans, "Action", szAction, sizeof(szAction), "inject");
452 AssertLogRelRCReturn(rc, rc);
453 AssertLogRelMsgFailedReturn(("Action='%s', valid values are 'inject', 'replace' and 'remove'.\n", szAction),
454 VERR_PDM_MISCONFIGURED_DRV_TRANSFORMATION);
455
456 AssertLogRelMsgReturn( !strcmp(szAction, "inject")
457 || !strcmp(szAction, "replace")
458 || !strcmp(szAction, "remove"),
459 ("Action='%s', valid values are 'inject', 'replace' and 'remove'.\n", szAction),
460 VERR_PDM_MISCONFIGURED_DRV_TRANSFORMATION);
461
462 /* Get the attached driver to inject. */
463 PCFGMNODE pTransAttDrv = NULL;
464 if (!strcmp(szAction, "inject") || !strcmp(szAction, "replace"))
465 {
466 pTransAttDrv = CFGMR3GetChild(pCurTrans, "AttachedDriver");
467 AssertLogRelMsgReturn(pTransAttDrv,
468 ("An %s transformation requires an AttachedDriver child node!\n", szAction),
469 VERR_PDM_MISCONFIGURED_DRV_TRANSFORMATION);
470 }
471
472
473 /*
474 * Remove the node.
475 */
476 if (!strcmp(szAction, "remove"))
477 {
478 PCFGMNODE pBelowThis = CFGMR3GetChild(*ppNode, "AttachedDriver");
479 if (pBelowThis)
480 {
481 PCFGMNODE pBelowThisCopy = CFGMR3DuplicateSubTree(pBelowThis);
482 AssertLogRelReturn(pBelowThisCopy, VERR_NO_MEMORY);
483
484 rc = CFGMR3ReplaceSubTree(*ppNode, pBelowThisCopy);
485 if (RT_FAILURE(rc))
486 {
487 CFGMR3RemoveNode(pBelowThis);
488 AssertLogRelReturn(("rc=%Rrc\n", rc), rc);
489 }
490 }
491 else
492 {
493 CFGMR3RemoveNode(*ppNode);
494 *ppNode = NULL;
495 }
496 }
497 /*
498 * Replace the driver about to be instantiated.
499 */
500 else if (!strcmp(szAction, "replace"))
501 {
502 PCFGMNODE pTransCopy = CFGMR3DuplicateSubTree(pTransAttDrv);
503 AssertLogRelBreakStmt(pTransCopy, rc = VERR_NO_MEMORY);
504 rc = VERR_PDM_MISCONFIGURED_DRV_TRANSFORMATION;
505
506 PCFGMNODE pBelowThis = CFGMR3GetChild(*ppNode, "AttachedDriver");
507 if (!pBelowThis)
508 rc = VINF_SUCCESS;
509 else
510 {
511 PCFGMNODE pBelowThisCopy = CFGMR3DuplicateSubTree(pBelowThis);
512 if (pBelowThisCopy)
513 {
514 rc = CFGMR3InsertSubTree(pTransCopy, "AttachedDriver", pBelowThisCopy, NULL);
515 AssertLogRelRC(rc);
516 if (RT_FAILURE(rc))
517 CFGMR3RemoveNode(pBelowThisCopy);
518 }
519 }
520 if (RT_SUCCESS(rc))
521 rc = CFGMR3ReplaceSubTree(*ppNode, pTransCopy);
522 if (RT_FAILURE(rc))
523 CFGMR3RemoveNode(pTransCopy);
524 }
525 /*
526 * Inject a driver before the driver about to be instantiated.
527 */
528 else if (!strcmp(szAction, "inject"))
529 {
530 PCFGMNODE pTransCopy = CFGMR3DuplicateSubTree(pTransAttDrv);
531 AssertLogRelBreakStmt(pTransCopy, rc = VERR_NO_MEMORY);
532 rc = VERR_PDM_MISCONFIGURED_DRV_TRANSFORMATION;
533
534 PCFGMNODE pThisCopy = CFGMR3DuplicateSubTree(*ppNode);
535 if (pThisCopy)
536 {
537 rc = CFGMR3InsertSubTree(pTransCopy, "AttachedDriver", pThisCopy, NULL);
538 AssertLogRelRC(rc);
539 if (RT_FAILURE(rc))
540 CFGMR3RemoveNode(pThisCopy);
541 }
542 if (RT_SUCCESS(rc))
543 rc = CFGMR3ReplaceSubTree(*ppNode, pTransCopy);
544 if (RT_FAILURE(rc))
545 CFGMR3RemoveNode(pTransCopy);
546 }
547 else
548 AssertFailed();
549 break;
550 }
551
552 return rc;
553}
554
555
556/**
557 * Instantiate a driver.
558 *
559 * @returns VBox status code, including informational statuses.
560 *
561 * @param pVM The VM handle.
562 * @param pNode The CFGM node for the driver.
563 * @param pBaseInterface The base interface.
564 * @param pDrvAbove The driver above it. NULL if it's the top-most
565 * driver.
566 * @param pLun The LUN the driver is being attached to. NULL
567 * if we're instantiating a driver chain before
568 * attaching it - untested.
569 * @param ppBaseInterface Where to return the pointer to the base
570 * interface of the newly created driver.
571 *
572 * @remarks Recursive calls to this function is normal as the drivers will
573 * attach to anything below them during the pfnContruct call.
574 */
575int pdmR3DrvInstantiate(PVM pVM, PCFGMNODE pNode, PPDMIBASE pBaseInterface, PPDMDRVINS pDrvAbove,
576 PPDMLUN pLun, PPDMIBASE *ppBaseInterface)
577{
578 Assert(!pDrvAbove || !pDrvAbove->Internal.s.pDown);
579 Assert(!pDrvAbove || !pDrvAbove->pDownBase);
580
581 Assert(pBaseInterface->pfnQueryInterface(pBaseInterface, PDMIBASE_IID) == pBaseInterface);
582
583 /*
584 * Do driver chain injections
585 */
586 int rc = pdmR3DrvMaybeTransformChain(pVM, pDrvAbove, pLun, &pNode);
587 if (RT_FAILURE(rc))
588 return rc;
589
590 /*
591 * Find the driver.
592 */
593 char *pszName;
594 rc = CFGMR3QueryStringAlloc(pNode, "Driver", &pszName);
595 if (RT_SUCCESS(rc))
596 {
597 PPDMDRV pDrv = pdmR3DrvLookup(pVM, pszName);
598 MMR3HeapFree(pszName);
599 if ( pDrv
600 && pDrv->cInstances < pDrv->pReg->cMaxInstances)
601 {
602 /* config node */
603 PCFGMNODE pConfigNode = CFGMR3GetChild(pNode, "Config");
604 if (!pConfigNode)
605 rc = CFGMR3InsertNode(pNode, "Config", &pConfigNode);
606 if (RT_SUCCESS(rc))
607 {
608 CFGMR3SetRestrictedRoot(pConfigNode);
609
610 /*
611 * Allocate the driver instance.
612 */
613 size_t cb = RT_OFFSETOF(PDMDRVINS, achInstanceData[pDrv->pReg->cbInstance]);
614 cb = RT_ALIGN_Z(cb, 16);
615 bool const fHyperHeap = !!(pDrv->pReg->fFlags & (PDM_DRVREG_FLAGS_R0 | PDM_DRVREG_FLAGS_RC));
616 PPDMDRVINS pNew;
617 if (fHyperHeap)
618 rc = MMHyperAlloc(pVM, cb, 64, MM_TAG_PDM_DRIVER, (void **)&pNew);
619 else
620 rc = MMR3HeapAllocZEx(pVM, MM_TAG_PDM_DRIVER, cb, (void **)&pNew);
621 if (pNew)
622 {
623 /*
624 * Initialize the instance structure (declaration order).
625 */
626 pNew->u32Version = PDM_DRVINS_VERSION;
627 pNew->Internal.s.pUp = pDrvAbove ? pDrvAbove : NULL;
628 //pNew->Internal.s.pDown = NULL;
629 pNew->Internal.s.pLun = pLun;
630 pNew->Internal.s.pDrv = pDrv;
631 pNew->Internal.s.pVMR3 = pVM;
632 pNew->Internal.s.pVMR0 = pDrv->pReg->fFlags & PDM_DRVREG_FLAGS_R0 ? pVM->pVMR0 : NIL_RTR0PTR;
633 pNew->Internal.s.pVMRC = pDrv->pReg->fFlags & PDM_DRVREG_FLAGS_RC ? pVM->pVMRC : NIL_RTRCPTR;
634 //pNew->Internal.s.fDetaching = false;
635 pNew->Internal.s.fVMSuspended = true;
636 //pNew->Internal.s.fVMReset = false;
637 pNew->Internal.s.fHyperHeap = fHyperHeap;
638 //pNew->Internal.s.pfnAsyncNotify = NULL;
639 pNew->Internal.s.pCfgHandle = pNode;
640 pNew->pReg = pDrv->pReg;
641 pNew->pCfg = pConfigNode;
642 pNew->iInstance = pDrv->iNextInstance;
643 pNew->pUpBase = pBaseInterface;
644 Assert(!pDrvAbove || pBaseInterface == &pDrvAbove->IBase);
645 //pNew->pDownBase = NULL;
646 //pNew->IBase.pfnQueryInterface = NULL;
647 pNew->pHlpR3 = &g_pdmR3DrvHlp;
648 pNew->pvInstanceDataR3 = &pNew->achInstanceData[0];
649 if (pDrv->pReg->fFlags & PDM_DRVREG_FLAGS_R0)
650 {
651 pNew->pvInstanceDataR0 = MMHyperR3ToR0(pVM, &pNew->achInstanceData[0]);
652 rc = PDMR3LdrGetSymbolR0(pVM, NULL, "g_pdmR0DrvHlp", &pNew->pHlpR0);
653 AssertReleaseRCReturn(rc, rc);
654
655 }
656 if (pDrv->pReg->fFlags & PDM_DRVREG_FLAGS_RC)
657 {
658 pNew->pvInstanceDataR0 = MMHyperR3ToRC(pVM, &pNew->achInstanceData[0]);
659 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_pdmRCDrvHlp", &pNew->pHlpRC);
660 AssertReleaseRCReturn(rc, rc);
661 }
662
663 pDrv->iNextInstance++;
664 pDrv->cInstances++;
665
666 /*
667 * Link with it with the driver above / LUN.
668 */
669 if (pDrvAbove)
670 {
671 pDrvAbove->pDownBase = &pNew->IBase;
672 pDrvAbove->Internal.s.pDown = pNew;
673 }
674 else if (pLun)
675 pLun->pTop = pNew;
676 if (pLun)
677 pLun->pBottom = pNew;
678
679 /*
680 * Invoke the constructor.
681 */
682 rc = pDrv->pReg->pfnConstruct(pNew, pNew->pCfg, 0 /*fFlags*/);
683 if (RT_SUCCESS(rc))
684 {
685 AssertPtr(pNew->IBase.pfnQueryInterface);
686 Assert(pNew->IBase.pfnQueryInterface(&pNew->IBase, PDMIBASE_IID) == &pNew->IBase);
687
688 /* Success! */
689 *ppBaseInterface = &pNew->IBase;
690 if (pLun)
691 Log(("PDM: Attached driver %p:'%s'/%d to LUN#%d on device '%s'/%d, pDrvAbove=%p:'%s'/%d\n",
692 pNew, pDrv->pReg->szName, pNew->iInstance,
693 pLun->iLun,
694 pLun->pDevIns ? pLun->pDevIns->pReg->szName : pLun->pUsbIns->pReg->szName,
695 pLun->pDevIns ? pLun->pDevIns->iInstance : pLun->pUsbIns->iInstance,
696 pDrvAbove, pDrvAbove ? pDrvAbove->pReg->szName : "", pDrvAbove ? pDrvAbove->iInstance : UINT32_MAX));
697 else
698 Log(("PDM: Attached driver %p:'%s'/%d, pDrvAbove=%p:'%s'/%d\n",
699 pNew, pDrv->pReg->szName, pNew->iInstance,
700 pDrvAbove, pDrvAbove ? pDrvAbove->pReg->szName : "", pDrvAbove ? pDrvAbove->iInstance : UINT32_MAX));
701 }
702 else
703 {
704 pdmR3DrvDestroyChain(pNew, PDM_TACH_FLAGS_NO_CALLBACKS);
705 if (rc == VERR_VERSION_MISMATCH)
706 rc = VERR_PDM_DRIVER_VERSION_MISMATCH;
707 }
708 }
709 else
710 {
711 AssertMsgFailed(("Failed to allocate %d bytes for instantiating driver '%s'\n", cb, pszName));
712 rc = VERR_NO_MEMORY;
713 }
714 }
715 else
716 AssertMsgFailed(("Failed to create Config node! rc=%Rrc\n", rc));
717 }
718 else if (pDrv)
719 {
720 AssertMsgFailed(("Too many instances of driver '%s', max is %u\n", pszName, pDrv->pReg->cMaxInstances));
721 rc = VERR_PDM_TOO_MANY_DRIVER_INSTANCES;
722 }
723 else
724 {
725 AssertMsgFailed(("Driver '%s' wasn't found!\n", pszName));
726 rc = VERR_PDM_DRIVER_NOT_FOUND;
727 }
728 }
729 else
730 {
731 AssertMsgFailed(("Query for string value of \"Driver\" -> %Rrc\n", rc));
732 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
733 rc = VERR_PDM_CFG_MISSING_DRIVER_NAME;
734 }
735 return rc;
736}
737
738
739/**
740 * Detaches a driver from whatever it's attached to.
741 * This will of course lead to the destruction of the driver and all drivers below it in the chain.
742 *
743 * @returns VINF_SUCCESS
744 * @param pDrvIns The driver instance to detach.
745 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
746 */
747int pdmR3DrvDetach(PPDMDRVINS pDrvIns, uint32_t fFlags)
748{
749 PDMDRV_ASSERT_DRVINS(pDrvIns);
750 LogFlow(("pdmR3DrvDetach: pDrvIns=%p '%s'/%d\n", pDrvIns, pDrvIns->pReg->szName, pDrvIns->iInstance));
751 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
752
753 /*
754 * Check that we're not doing this recursively, that could have unwanted sideeffects!
755 */
756 if (pDrvIns->Internal.s.fDetaching)
757 {
758 AssertMsgFailed(("Recursive detach! '%s'/%d\n", pDrvIns->pReg->szName, pDrvIns->iInstance));
759 return VINF_SUCCESS; }
760
761 /*
762 * Check that we actually can detach this instance.
763 * The requirement is that the driver/device above has a detach method.
764 */
765 if (pDrvIns->Internal.s.pUp
766 ? !pDrvIns->Internal.s.pUp->pReg->pfnDetach
767 : !pDrvIns->Internal.s.pLun->pDevIns->pReg->pfnDetach)
768 {
769 AssertMsgFailed(("Cannot detach driver instance because the driver/device above doesn't support it!\n"));
770 return VERR_PDM_DRIVER_DETACH_NOT_POSSIBLE;
771 }
772
773 /*
774 * Join paths with pdmR3DrvDestroyChain.
775 */
776 pdmR3DrvDestroyChain(pDrvIns, fFlags);
777 return VINF_SUCCESS;
778}
779
780
781/**
782 * Destroys a driver chain starting with the specified driver.
783 *
784 * This is used when unplugging a device at run time.
785 *
786 * @param pDrvIns Pointer to the driver instance to start with.
787 * @param fFlags PDM_TACH_FLAGS_NOT_HOT_PLUG, PDM_TACH_FLAGS_NO_CALLBACKS
788 * or 0.
789 */
790void pdmR3DrvDestroyChain(PPDMDRVINS pDrvIns, uint32_t fFlags)
791{
792 PVM pVM = pDrvIns->Internal.s.pVMR3;
793 VM_ASSERT_EMT(pVM);
794
795 /*
796 * Detach the bottommost driver until we've detached pDrvIns.
797 */
798 pDrvIns->Internal.s.fDetaching = true;
799 PPDMDRVINS pCur;
800 do
801 {
802 /* find the driver to detach. */
803 pCur = pDrvIns;
804 while (pCur->Internal.s.pDown)
805 pCur = pCur->Internal.s.pDown;
806 LogFlow(("pdmR3DrvDestroyChain: pCur=%p '%s'/%d\n", pCur, pCur->pReg->szName, pCur->iInstance));
807
808 /*
809 * Unlink it and notify parent.
810 */
811 pCur->Internal.s.fDetaching = true;
812
813 PPDMLUN pLun = pCur->Internal.s.pLun;
814 Assert(pLun->pBottom == pCur);
815 pLun->pBottom = pCur->Internal.s.pUp;
816
817 if (pCur->Internal.s.pUp)
818 {
819 /* driver parent */
820 PPDMDRVINS pParent = pCur->Internal.s.pUp;
821 pCur->Internal.s.pUp = NULL;
822 pParent->Internal.s.pDown = NULL;
823
824 if (!(fFlags & PDM_TACH_FLAGS_NO_CALLBACKS) && pParent->pReg->pfnDetach)
825 pParent->pReg->pfnDetach(pParent, fFlags);
826
827 pParent->pDownBase = NULL;
828 }
829 else
830 {
831 /* device parent */
832 Assert(pLun->pTop == pCur);
833 pLun->pTop = NULL;
834 if (!(fFlags & PDM_TACH_FLAGS_NO_CALLBACKS) && pLun->pDevIns->pReg->pfnDetach)
835 {
836 PDMCritSectEnter(pLun->pDevIns->pCritSectRoR3, VERR_IGNORED);
837 pLun->pDevIns->pReg->pfnDetach(pLun->pDevIns, pLun->iLun, fFlags);
838 PDMCritSectLeave(pLun->pDevIns->pCritSectRoR3);
839 }
840 }
841
842 /*
843 * Call destructor.
844 */
845 pCur->pUpBase = NULL;
846 if (pCur->pReg->pfnDestruct)
847 pCur->pReg->pfnDestruct(pCur);
848 pCur->Internal.s.pDrv->cInstances--;
849
850 /*
851 * Free all resources allocated by the driver.
852 */
853 /* Queues. */
854 int rc = PDMR3QueueDestroyDriver(pVM, pCur);
855 AssertRC(rc);
856
857 /* Timers. */
858 rc = TMR3TimerDestroyDriver(pVM, pCur);
859 AssertRC(rc);
860
861 /* SSM data units. */
862 rc = SSMR3DeregisterDriver(pVM, pCur, NULL, 0);
863 AssertRC(rc);
864
865 /* PDM threads. */
866 rc = pdmR3ThreadDestroyDriver(pVM, pCur);
867 AssertRC(rc);
868
869 /* Info handlers. */
870 rc = DBGFR3InfoDeregisterDriver(pVM, pCur, NULL);
871 AssertRC(rc);
872
873 /* PDM critsects. */
874 rc = pdmR3CritSectDeleteDriver(pVM, pCur);
875 AssertRC(rc);
876
877 /* Block caches. */
878 PDMR3BlkCacheReleaseDriver(pVM, pCur);
879
880 /* Finally, the driver it self. */
881 bool fHyperHeap = pCur->Internal.s.fHyperHeap;
882 ASMMemFill32(pCur, RT_OFFSETOF(PDMDRVINS, achInstanceData[pCur->pReg->cbInstance]), 0xdeadd0d0);
883 if (fHyperHeap)
884 MMHyperFree(pVM, pCur);
885 else
886 MMR3HeapFree(pCur);
887
888 } while (pCur != pDrvIns);
889}
890
891
892
893
894/** @name Driver Helpers
895 * @{
896 */
897
898/** @interface_method_impl{PDMDRVHLP,pfnAttach} */
899static DECLCALLBACK(int) pdmR3DrvHlp_Attach(PPDMDRVINS pDrvIns, uint32_t fFlags, PPDMIBASE *ppBaseInterface)
900{
901 PDMDRV_ASSERT_DRVINS(pDrvIns);
902 PVM pVM = pDrvIns->Internal.s.pVMR3;
903 VM_ASSERT_EMT(pVM);
904 LogFlow(("pdmR3DrvHlp_Attach: caller='%s'/%d: fFlags=%#x\n", pDrvIns->pReg->szName, pDrvIns->iInstance, fFlags));
905 Assert(!(fFlags & ~(PDM_TACH_FLAGS_NOT_HOT_PLUG)));
906
907 /*
908 * Check that there isn't anything attached already.
909 */
910 int rc;
911 if (!pDrvIns->Internal.s.pDown)
912 {
913 Assert(pDrvIns->Internal.s.pLun->pBottom == pDrvIns);
914
915 /*
916 * Get the attached driver configuration.
917 */
918 PCFGMNODE pNode = CFGMR3GetChild(pDrvIns->Internal.s.pCfgHandle, "AttachedDriver");
919 if (pNode)
920 rc = pdmR3DrvInstantiate(pVM, pNode, &pDrvIns->IBase, pDrvIns, pDrvIns->Internal.s.pLun, ppBaseInterface);
921 else
922 rc = VERR_PDM_NO_ATTACHED_DRIVER;
923 }
924 else
925 {
926 AssertMsgFailed(("Already got a driver attached. The driver should keep track of such things!\n"));
927 rc = VERR_PDM_DRIVER_ALREADY_ATTACHED;
928 }
929
930 LogFlow(("pdmR3DrvHlp_Attach: caller='%s'/%d: return %Rrc\n",
931 pDrvIns->pReg->szName, pDrvIns->iInstance, rc));
932 return rc;
933}
934
935
936/** @interface_method_impl{PDMDRVHLP,pfnDetach} */
937static DECLCALLBACK(int) pdmR3DrvHlp_Detach(PPDMDRVINS pDrvIns, uint32_t fFlags)
938{
939 PDMDRV_ASSERT_DRVINS(pDrvIns);
940 LogFlow(("pdmR3DrvHlp_Detach: caller='%s'/%d: fFlags=%#x\n",
941 pDrvIns->pReg->szName, pDrvIns->iInstance, fFlags));
942 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
943
944 /*
945 * Anything attached?
946 */
947 int rc;
948 if (pDrvIns->Internal.s.pDown)
949 rc = pdmR3DrvDetach(pDrvIns->Internal.s.pDown, fFlags);
950 else
951 {
952 AssertMsgFailed(("Nothing attached!\n"));
953 rc = VERR_PDM_NO_DRIVER_ATTACHED;
954 }
955
956 LogFlow(("pdmR3DrvHlp_Detach: caller='%s'/%d: returns %Rrc\n",
957 pDrvIns->pReg->szName, pDrvIns->iInstance, rc));
958 return rc;
959}
960
961
962/** @interface_method_impl{PDMDRVHLP,pfnDetachSelf} */
963static DECLCALLBACK(int) pdmR3DrvHlp_DetachSelf(PPDMDRVINS pDrvIns, uint32_t fFlags)
964{
965 PDMDRV_ASSERT_DRVINS(pDrvIns);
966 LogFlow(("pdmR3DrvHlp_DetachSelf: caller='%s'/%d: fFlags=%#x\n",
967 pDrvIns->pReg->szName, pDrvIns->iInstance, fFlags));
968 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
969
970 int rc = pdmR3DrvDetach(pDrvIns, fFlags);
971
972 LogFlow(("pdmR3DrvHlp_Detach: returns %Rrc\n", rc)); /* pDrvIns is freed by now. */
973 return rc;
974}
975
976
977/** @interface_method_impl{PDMDRVHLP,pfnMountPrepare} */
978static DECLCALLBACK(int) pdmR3DrvHlp_MountPrepare(PPDMDRVINS pDrvIns, const char *pszFilename, const char *pszCoreDriver)
979{
980 PDMDRV_ASSERT_DRVINS(pDrvIns);
981 LogFlow(("pdmR3DrvHlp_MountPrepare: caller='%s'/%d: pszFilename=%p:{%s} pszCoreDriver=%p:{%s}\n",
982 pDrvIns->pReg->szName, pDrvIns->iInstance, pszFilename, pszFilename, pszCoreDriver, pszCoreDriver));
983 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
984
985 /*
986 * Do the caller have anything attached below itself?
987 */
988 if (pDrvIns->Internal.s.pDown)
989 {
990 AssertMsgFailed(("Cannot prepare a mount when something's attached to you!\n"));
991 return VERR_PDM_DRIVER_ALREADY_ATTACHED;
992 }
993
994 /*
995 * We're asked to prepare, so we'll start off by nuking the
996 * attached configuration tree.
997 */
998 PCFGMNODE pNode = CFGMR3GetChild(pDrvIns->Internal.s.pCfgHandle, "AttachedDriver");
999 if (pNode)
1000 CFGMR3RemoveNode(pNode);
1001
1002 /*
1003 * If there is no core driver, we'll have to probe for it.
1004 */
1005 if (!pszCoreDriver)
1006 {
1007 /** @todo implement image probing. */
1008 AssertReleaseMsgFailed(("Not implemented!\n"));
1009 return VERR_NOT_IMPLEMENTED;
1010 }
1011
1012 /*
1013 * Construct the basic attached driver configuration.
1014 */
1015 int rc = CFGMR3InsertNode(pDrvIns->Internal.s.pCfgHandle, "AttachedDriver", &pNode);
1016 if (RT_SUCCESS(rc))
1017 {
1018 rc = CFGMR3InsertString(pNode, "Driver", pszCoreDriver);
1019 if (RT_SUCCESS(rc))
1020 {
1021 PCFGMNODE pCfg;
1022 rc = CFGMR3InsertNode(pNode, "Config", &pCfg);
1023 if (RT_SUCCESS(rc))
1024 {
1025 rc = CFGMR3InsertString(pCfg, "Path", pszFilename);
1026 if (RT_SUCCESS(rc))
1027 {
1028 LogFlow(("pdmR3DrvHlp_MountPrepare: caller='%s'/%d: returns %Rrc (Driver=%s)\n",
1029 pDrvIns->pReg->szName, pDrvIns->iInstance, rc, pszCoreDriver));
1030 return rc;
1031 }
1032 else
1033 AssertMsgFailed(("Path string insert failed, rc=%Rrc\n", rc));
1034 }
1035 else
1036 AssertMsgFailed(("Config node failed, rc=%Rrc\n", rc));
1037 }
1038 else
1039 AssertMsgFailed(("Driver string insert failed, rc=%Rrc\n", rc));
1040 CFGMR3RemoveNode(pNode);
1041 }
1042 else
1043 AssertMsgFailed(("AttachedDriver node insert failed, rc=%Rrc\n", rc));
1044
1045 LogFlow(("pdmR3DrvHlp_MountPrepare: caller='%s'/%d: returns %Rrc\n",
1046 pDrvIns->pReg->szName, pDrvIns->iInstance, rc));
1047 return rc;
1048}
1049
1050
1051/** @interface_method_impl{PDMDRVHLP,pfnAssertEMT} */
1052static DECLCALLBACK(bool) pdmR3DrvHlp_AssertEMT(PPDMDRVINS pDrvIns, const char *pszFile, unsigned iLine, const char *pszFunction)
1053{
1054 PDMDRV_ASSERT_DRVINS(pDrvIns);
1055 if (VM_IS_EMT(pDrvIns->Internal.s.pVMR3))
1056 return true;
1057
1058 char szMsg[100];
1059 RTStrPrintf(szMsg, sizeof(szMsg), "AssertEMT '%s'/%d\n", pDrvIns->pReg->szName, pDrvIns->iInstance);
1060 RTAssertMsg1Weak(szMsg, iLine, pszFile, pszFunction);
1061 AssertBreakpoint();
1062 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
1063 return false;
1064}
1065
1066
1067/** @interface_method_impl{PDMDRVHLP,pfnAssertOther} */
1068static DECLCALLBACK(bool) pdmR3DrvHlp_AssertOther(PPDMDRVINS pDrvIns, const char *pszFile, unsigned iLine, const char *pszFunction)
1069{
1070 PDMDRV_ASSERT_DRVINS(pDrvIns);
1071 if (!VM_IS_EMT(pDrvIns->Internal.s.pVMR3))
1072 return true;
1073
1074 char szMsg[100];
1075 RTStrPrintf(szMsg, sizeof(szMsg), "AssertOther '%s'/%d\n", pDrvIns->pReg->szName, pDrvIns->iInstance);
1076 RTAssertMsg1Weak(szMsg, iLine, pszFile, pszFunction);
1077 AssertBreakpoint();
1078 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
1079 return false;
1080}
1081
1082
1083/** @interface_method_impl{PDMDRVHLP,pfnVMSetError} */
1084static DECLCALLBACK(int) pdmR3DrvHlp_VMSetError(PPDMDRVINS pDrvIns, int rc, RT_SRC_POS_DECL, const char *pszFormat, ...)
1085{
1086 PDMDRV_ASSERT_DRVINS(pDrvIns);
1087 va_list args;
1088 va_start(args, pszFormat);
1089 int rc2 = VMSetErrorV(pDrvIns->Internal.s.pVMR3, rc, RT_SRC_POS_ARGS, pszFormat, args); Assert(rc2 == rc); NOREF(rc2);
1090 va_end(args);
1091 return rc;
1092}
1093
1094
1095/** @interface_method_impl{PDMDRVHLP,pfnVMSetErrorV} */
1096static DECLCALLBACK(int) pdmR3DrvHlp_VMSetErrorV(PPDMDRVINS pDrvIns, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va)
1097{
1098 PDMDRV_ASSERT_DRVINS(pDrvIns);
1099 int rc2 = VMSetErrorV(pDrvIns->Internal.s.pVMR3, rc, RT_SRC_POS_ARGS, pszFormat, va); Assert(rc2 == rc); NOREF(rc2);
1100 return rc;
1101}
1102
1103
1104/** @interface_method_impl{PDMDRVHLP,pfnVMSetRuntimeError} */
1105static DECLCALLBACK(int) pdmR3DrvHlp_VMSetRuntimeError(PPDMDRVINS pDrvIns, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, ...)
1106{
1107 PDMDRV_ASSERT_DRVINS(pDrvIns);
1108 va_list args;
1109 va_start(args, pszFormat);
1110 int rc = VMSetRuntimeErrorV(pDrvIns->Internal.s.pVMR3, fFlags, pszErrorId, pszFormat, args);
1111 va_end(args);
1112 return rc;
1113}
1114
1115
1116/** @interface_method_impl{PDMDRVHLP,pfnVMSetRuntimeErrorV} */
1117static DECLCALLBACK(int) pdmR3DrvHlp_VMSetRuntimeErrorV(PPDMDRVINS pDrvIns, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list va)
1118{
1119 PDMDRV_ASSERT_DRVINS(pDrvIns);
1120 int rc = VMSetRuntimeErrorV(pDrvIns->Internal.s.pVMR3, fFlags, pszErrorId, pszFormat, va);
1121 return rc;
1122}
1123
1124
1125/** @interface_method_impl{PDMDEVHLPR3,pfnVMState} */
1126static DECLCALLBACK(VMSTATE) pdmR3DrvHlp_VMState(PPDMDRVINS pDrvIns)
1127{
1128 PDMDRV_ASSERT_DRVINS(pDrvIns);
1129
1130 VMSTATE enmVMState = VMR3GetState(pDrvIns->Internal.s.pVMR3);
1131
1132 LogFlow(("pdmR3DrvHlp_VMState: caller='%s'/%d: returns %d (%s)\n", pDrvIns->pReg->szName, pDrvIns->iInstance,
1133 enmVMState, VMR3GetStateName(enmVMState)));
1134 return enmVMState;
1135}
1136
1137
1138/** @interface_method_impl{PDMDEVHLPR3,pfnVMTeleportedAndNotFullyResumedYet} */
1139static DECLCALLBACK(bool) pdmR3DrvHlp_VMTeleportedAndNotFullyResumedYet(PPDMDRVINS pDrvIns)
1140{
1141 PDMDRV_ASSERT_DRVINS(pDrvIns);
1142
1143 bool fRc = VMR3TeleportedAndNotFullyResumedYet(pDrvIns->Internal.s.pVMR3);
1144
1145 LogFlow(("pdmR3DrvHlp_VMState: caller='%s'/%d: returns %RTbool)\n", pDrvIns->pReg->szName, pDrvIns->iInstance,
1146 fRc));
1147 return fRc;
1148}
1149
1150
1151/** @interface_method_impl{PDMDEVHLPR3,pfnGetSupDrvSession} */
1152static DECLCALLBACK(PSUPDRVSESSION) pdmR3DrvHlp_GetSupDrvSession(PPDMDRVINS pDrvIns)
1153{
1154 PDMDRV_ASSERT_DRVINS(pDrvIns);
1155
1156 PSUPDRVSESSION pSession = pDrvIns->Internal.s.pVMR3->pSession;
1157 LogFlow(("pdmR3DrvHlp_GetSupDrvSession: caller='%s'/%d: returns %p)\n", pDrvIns->pReg->szName, pDrvIns->iInstance,
1158 pSession));
1159 return pSession;
1160}
1161
1162
1163/** @interface_method_impl{PDMDRVHLP,pfnQueueCreate} */
1164static DECLCALLBACK(int) pdmR3DrvHlp_QueueCreate(PPDMDRVINS pDrvIns, uint32_t cbItem, uint32_t cItems, uint32_t cMilliesInterval,
1165 PFNPDMQUEUEDRV pfnCallback, const char *pszName, PPDMQUEUE *ppQueue)
1166{
1167 PDMDRV_ASSERT_DRVINS(pDrvIns);
1168 LogFlow(("pdmR3DrvHlp_PDMQueueCreate: caller='%s'/%d: cbItem=%d cItems=%d cMilliesInterval=%d pfnCallback=%p pszName=%p:{%s} ppQueue=%p\n",
1169 pDrvIns->pReg->szName, pDrvIns->iInstance, cbItem, cItems, cMilliesInterval, pfnCallback, pszName, pszName, ppQueue, ppQueue));
1170 PVM pVM = pDrvIns->Internal.s.pVMR3;
1171 VM_ASSERT_EMT(pVM);
1172
1173 if (pDrvIns->iInstance > 0)
1174 {
1175 pszName = MMR3HeapAPrintf(pVM, MM_TAG_PDM_DRIVER_DESC, "%s_%u", pszName, pDrvIns->iInstance);
1176 AssertLogRelReturn(pszName, VERR_NO_MEMORY);
1177 }
1178
1179 int rc = PDMR3QueueCreateDriver(pVM, pDrvIns, cbItem, cItems, cMilliesInterval, pfnCallback, pszName, ppQueue);
1180
1181 LogFlow(("pdmR3DrvHlp_PDMQueueCreate: caller='%s'/%d: returns %Rrc *ppQueue=%p\n", pDrvIns->pReg->szName, pDrvIns->iInstance, rc, *ppQueue));
1182 return rc;
1183}
1184
1185
1186/** @interface_method_impl{PDMDRVHLP,pfnTMGetVirtualFreq} */
1187static DECLCALLBACK(uint64_t) pdmR3DrvHlp_TMGetVirtualFreq(PPDMDRVINS pDrvIns)
1188{
1189 PDMDRV_ASSERT_DRVINS(pDrvIns);
1190
1191 return TMVirtualGetFreq(pDrvIns->Internal.s.pVMR3);
1192}
1193
1194
1195/** @interface_method_impl{PDMDRVHLP,pfnTMGetVirtualTime} */
1196static DECLCALLBACK(uint64_t) pdmR3DrvHlp_TMGetVirtualTime(PPDMDRVINS pDrvIns)
1197{
1198 PDMDRV_ASSERT_DRVINS(pDrvIns);
1199
1200 return TMVirtualGet(pDrvIns->Internal.s.pVMR3);
1201}
1202
1203
1204/** @interface_method_impl{PDMDRVHLP,pfnTMTimerCreate} */
1205static DECLCALLBACK(int) pdmR3DrvHlp_TMTimerCreate(PPDMDRVINS pDrvIns, TMCLOCK enmClock, PFNTMTIMERDRV pfnCallback, void *pvUser, uint32_t fFlags, const char *pszDesc, PPTMTIMERR3 ppTimer)
1206{
1207 PDMDRV_ASSERT_DRVINS(pDrvIns);
1208 LogFlow(("pdmR3DrvHlp_TMTimerCreate: caller='%s'/%d: enmClock=%d pfnCallback=%p pvUser=%p fFlags=%#x pszDesc=%p:{%s} ppTimer=%p\n",
1209 pDrvIns->pReg->szName, pDrvIns->iInstance, enmClock, pfnCallback, pvUser, fFlags, pszDesc, pszDesc, ppTimer));
1210
1211 int rc = TMR3TimerCreateDriver(pDrvIns->Internal.s.pVMR3, pDrvIns, enmClock, pfnCallback, pvUser, fFlags, pszDesc, ppTimer);
1212
1213 LogFlow(("pdmR3DrvHlp_TMTimerCreate: caller='%s'/%d: returns %Rrc *ppTimer=%p\n", pDrvIns->pReg->szName, pDrvIns->iInstance, rc, *ppTimer));
1214 return rc;
1215}
1216
1217
1218
1219/** @interface_method_impl{PDMDRVHLP,pfnSSMRegister} */
1220static DECLCALLBACK(int) pdmR3DrvHlp_SSMRegister(PPDMDRVINS pDrvIns, uint32_t uVersion, size_t cbGuess,
1221 PFNSSMDRVLIVEPREP pfnLivePrep, PFNSSMDRVLIVEEXEC pfnLiveExec, PFNSSMDRVLIVEVOTE pfnLiveVote,
1222 PFNSSMDRVSAVEPREP pfnSavePrep, PFNSSMDRVSAVEEXEC pfnSaveExec, PFNSSMDRVSAVEDONE pfnSaveDone,
1223 PFNSSMDRVLOADPREP pfnLoadPrep, PFNSSMDRVLOADEXEC pfnLoadExec, PFNSSMDRVLOADDONE pfnLoadDone)
1224{
1225 PDMDRV_ASSERT_DRVINS(pDrvIns);
1226 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
1227 LogFlow(("pdmR3DrvHlp_SSMRegister: caller='%s'/%d: uVersion=#x cbGuess=%#x \n"
1228 " pfnLivePrep=%p pfnLiveExec=%p pfnLiveVote=%p pfnSavePrep=%p pfnSaveExec=%p pfnSaveDone=%p pszLoadPrep=%p pfnLoadExec=%p pfnLoaddone=%p\n",
1229 pDrvIns->pReg->szName, pDrvIns->iInstance, uVersion, cbGuess,
1230 pfnLivePrep, pfnLiveExec, pfnLiveVote,
1231 pfnSavePrep, pfnSaveExec, pfnSaveDone, pfnLoadPrep, pfnLoadExec, pfnLoadDone));
1232
1233 int rc = SSMR3RegisterDriver(pDrvIns->Internal.s.pVMR3, pDrvIns, pDrvIns->pReg->szName, pDrvIns->iInstance,
1234 uVersion, cbGuess,
1235 pfnLivePrep, pfnLiveExec, pfnLiveVote,
1236 pfnSavePrep, pfnSaveExec, pfnSaveDone,
1237 pfnLoadPrep, pfnLoadExec, pfnLoadDone);
1238
1239 LogFlow(("pdmR3DrvHlp_SSMRegister: caller='%s'/%d: returns %Rrc\n", pDrvIns->pReg->szName, pDrvIns->iInstance, rc));
1240 return rc;
1241}
1242
1243
1244/** @interface_method_impl{PDMDRVHLP,pfnSSMDeregister} */
1245static DECLCALLBACK(int) pdmR3DrvHlp_SSMDeregister(PPDMDRVINS pDrvIns, const char *pszName, uint32_t u32Instance)
1246{
1247 PDMDRV_ASSERT_DRVINS(pDrvIns);
1248 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
1249 LogFlow(("pdmR3DrvHlp_SSMDeregister: caller='%s'/%d: pszName=%p:{%s} u32Instance=%#x\n",
1250 pDrvIns->pReg->szName, pDrvIns->iInstance, pszName, pszName, u32Instance));
1251
1252 int rc = SSMR3DeregisterDriver(pDrvIns->Internal.s.pVMR3, pDrvIns, pszName, u32Instance);
1253
1254 LogFlow(("pdmR3DrvHlp_SSMDeregister: caller='%s'/%d: returns %Rrc\n", pDrvIns->pReg->szName, pDrvIns->iInstance, rc));
1255 return rc;
1256}
1257
1258
1259/** @interface_method_impl{PDMDEVHLP,pfnDBGFInfoRegister} */
1260static DECLCALLBACK(int) pdmR3DrvHlp_DBGFInfoRegister(PPDMDRVINS pDrvIns, const char *pszName, const char *pszDesc, PFNDBGFHANDLERDRV pfnHandler)
1261{
1262 PDMDRV_ASSERT_DRVINS(pDrvIns);
1263 LogFlow(("pdmR3DrvHlp_DBGFInfoRegister: caller='%s'/%d: pszName=%p:{%s} pszDesc=%p:{%s} pfnHandler=%p\n",
1264 pDrvIns->pReg->szName, pDrvIns->iInstance, pszName, pszName, pszDesc, pszDesc, pfnHandler));
1265
1266 int rc = DBGFR3InfoRegisterDriver(pDrvIns->Internal.s.pVMR3, pszName, pszDesc, pfnHandler, pDrvIns);
1267
1268 LogFlow(("pdmR3DrvHlp_DBGFInfoRegister: caller='%s'/%d: returns %Rrc\n", pDrvIns->pReg->szName, pDrvIns->iInstance, rc));
1269 return rc;
1270}
1271
1272
1273/** @interface_method_impl{PDMDEVHLP,pfnDBGFInfoDeregister} */
1274static DECLCALLBACK(int) pdmR3DrvHlp_DBGFInfoDeregister(PPDMDRVINS pDrvIns, const char *pszName)
1275{
1276 PDMDRV_ASSERT_DRVINS(pDrvIns);
1277 LogFlow(("pdmR3DrvHlp_DBGFInfoDeregister: caller='%s'/%d: pszName=%p:{%s} pszDesc=%p:{%s} pfnHandler=%p\n",
1278 pDrvIns->pReg->szName, pDrvIns->iInstance, pszName));
1279
1280 int rc = DBGFR3InfoDeregisterDriver(pDrvIns->Internal.s.pVMR3, pDrvIns, pszName);
1281
1282 LogFlow(("pdmR3DrvHlp_DBGFInfoDeregister: caller='%s'/%d: returns %Rrc\n", pDrvIns->pReg->szName, pDrvIns->iInstance, rc));
1283
1284 return rc;
1285}
1286
1287
1288/** @interface_method_impl{PDMDRVHLP,pfnSTAMRegister} */
1289static DECLCALLBACK(void) pdmR3DrvHlp_STAMRegister(PPDMDRVINS pDrvIns, void *pvSample, STAMTYPE enmType, const char *pszName, STAMUNIT enmUnit, const char *pszDesc)
1290{
1291 PDMDRV_ASSERT_DRVINS(pDrvIns);
1292 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
1293
1294 STAM_REG(pDrvIns->Internal.s.pVMR3, pvSample, enmType, pszName, enmUnit, pszDesc);
1295 /** @todo track the samples so they can be dumped & deregistered when the driver instance is destroyed.
1296 * For now we just have to be careful not to use this call for drivers which can be unloaded. */
1297}
1298
1299
1300/** @interface_method_impl{PDMDRVHLP,pfnSTAMRegisterF} */
1301static DECLCALLBACK(void) pdmR3DrvHlp_STAMRegisterF(PPDMDRVINS pDrvIns, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility,
1302 STAMUNIT enmUnit, const char *pszDesc, const char *pszName, ...)
1303{
1304 PDMDRV_ASSERT_DRVINS(pDrvIns);
1305 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
1306
1307 va_list args;
1308 va_start(args, pszName);
1309 int rc = STAMR3RegisterV(pDrvIns->Internal.s.pVMR3, pvSample, enmType, enmVisibility, enmUnit, pszDesc, pszName, args);
1310 va_end(args);
1311 AssertRC(rc);
1312}
1313
1314
1315/** @interface_method_impl{PDMDRVHLP,pfnSTAMRegisterV} */
1316static DECLCALLBACK(void) pdmR3DrvHlp_STAMRegisterV(PPDMDRVINS pDrvIns, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility,
1317 STAMUNIT enmUnit, const char *pszDesc, const char *pszName, va_list args)
1318{
1319 PDMDRV_ASSERT_DRVINS(pDrvIns);
1320 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
1321
1322 int rc = STAMR3RegisterV(pDrvIns->Internal.s.pVMR3, pvSample, enmType, enmVisibility, enmUnit, pszDesc, pszName, args);
1323 AssertRC(rc);
1324}
1325
1326
1327/** @interface_method_impl{PDMDRVHLP,pfnSTAMDeregister} */
1328static DECLCALLBACK(int) pdmR3DrvHlp_STAMDeregister(PPDMDRVINS pDrvIns, void *pvSample)
1329{
1330 PDMDRV_ASSERT_DRVINS(pDrvIns);
1331 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
1332
1333 int rc = STAMR3DeregisterU(pDrvIns->Internal.s.pVMR3->pUVM, pvSample);
1334 AssertRC(rc);
1335 return rc;
1336}
1337
1338
1339/** @interface_method_impl{PDMDRVHLP,pfnSUPCallVMMR0Ex} */
1340static DECLCALLBACK(int) pdmR3DrvHlp_SUPCallVMMR0Ex(PPDMDRVINS pDrvIns, unsigned uOperation, void *pvArg, unsigned cbArg)
1341{
1342 PDMDRV_ASSERT_DRVINS(pDrvIns);
1343 LogFlow(("pdmR3DrvHlp_SSMCallVMMR0Ex: caller='%s'/%d: uOperation=%u pvArg=%p cbArg=%d\n",
1344 pDrvIns->pReg->szName, pDrvIns->iInstance, uOperation, pvArg, cbArg));
1345 int rc;
1346 if ( uOperation >= VMMR0_DO_SRV_START
1347 && uOperation < VMMR0_DO_SRV_END)
1348 rc = SUPR3CallVMMR0Ex(pDrvIns->Internal.s.pVMR3->pVMR0, NIL_VMCPUID, uOperation, 0, (PSUPVMMR0REQHDR)pvArg);
1349 else
1350 {
1351 AssertMsgFailed(("Invalid uOperation=%u\n", uOperation));
1352 rc = VERR_INVALID_PARAMETER;
1353 }
1354
1355 LogFlow(("pdmR3DrvHlp_SUPCallVMMR0Ex: caller='%s'/%d: returns %Rrc\n", pDrvIns->pReg->szName, pDrvIns->iInstance, rc));
1356 return rc;
1357}
1358
1359
1360/** @interface_method_impl{PDMDRVHLP,pfnUSBRegisterHub} */
1361static DECLCALLBACK(int) pdmR3DrvHlp_USBRegisterHub(PPDMDRVINS pDrvIns, uint32_t fVersions, uint32_t cPorts, PCPDMUSBHUBREG pUsbHubReg, PPCPDMUSBHUBHLP ppUsbHubHlp)
1362{
1363 PDMDRV_ASSERT_DRVINS(pDrvIns);
1364 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
1365 LogFlow(("pdmR3DrvHlp_USBRegisterHub: caller='%s'/%d: fVersions=%#x cPorts=%#x pUsbHubReg=%p ppUsbHubHlp=%p\n",
1366 pDrvIns->pReg->szName, pDrvIns->iInstance, fVersions, cPorts, pUsbHubReg, ppUsbHubHlp));
1367
1368#ifdef VBOX_WITH_USB
1369 int rc = pdmR3UsbRegisterHub(pDrvIns->Internal.s.pVMR3, pDrvIns, fVersions, cPorts, pUsbHubReg, ppUsbHubHlp);
1370#else
1371 int rc = VERR_NOT_SUPPORTED;
1372#endif
1373
1374 LogFlow(("pdmR3DrvHlp_USBRegisterHub: caller='%s'/%d: returns %Rrc\n", pDrvIns->pReg->szName, pDrvIns->iInstance, rc));
1375 return rc;
1376}
1377
1378
1379/** @interface_method_impl{PDMDRVHLP,pfnSetAsyncNotification} */
1380static DECLCALLBACK(int) pdmR3DrvHlp_SetAsyncNotification(PPDMDRVINS pDrvIns, PFNPDMDRVASYNCNOTIFY pfnAsyncNotify)
1381{
1382 PDMDRV_ASSERT_DRVINS(pDrvIns);
1383 VM_ASSERT_EMT0(pDrvIns->Internal.s.pVMR3);
1384 LogFlow(("pdmR3DrvHlp_SetAsyncNotification: caller='%s'/%d: pfnAsyncNotify=%p\n", pDrvIns->pReg->szName, pDrvIns->iInstance, pfnAsyncNotify));
1385
1386 int rc = VINF_SUCCESS;
1387 AssertStmt(pfnAsyncNotify, rc = VERR_INVALID_PARAMETER);
1388 AssertStmt(!pDrvIns->Internal.s.pfnAsyncNotify, rc = VERR_WRONG_ORDER);
1389 AssertStmt(pDrvIns->Internal.s.fVMSuspended || pDrvIns->Internal.s.fVMReset, rc = VERR_WRONG_ORDER);
1390 VMSTATE enmVMState = VMR3GetState(pDrvIns->Internal.s.pVMR3);
1391 AssertStmt( enmVMState == VMSTATE_SUSPENDING
1392 || enmVMState == VMSTATE_SUSPENDING_EXT_LS
1393 || enmVMState == VMSTATE_SUSPENDING_LS
1394 || enmVMState == VMSTATE_RESETTING
1395 || enmVMState == VMSTATE_RESETTING_LS
1396 || enmVMState == VMSTATE_POWERING_OFF
1397 || enmVMState == VMSTATE_POWERING_OFF_LS,
1398 rc = VERR_INVALID_STATE);
1399
1400 if (RT_SUCCESS(rc))
1401 pDrvIns->Internal.s.pfnAsyncNotify = pfnAsyncNotify;
1402
1403 LogFlow(("pdmR3DrvHlp_SetAsyncNotification: caller='%s'/%d: returns %Rrc\n", pDrvIns->pReg->szName, pDrvIns->iInstance, rc));
1404 return rc;
1405}
1406
1407
1408/** @interface_method_impl{PDMDRVHLP,pfnAsyncNotificationCompleted} */
1409static DECLCALLBACK(void) pdmR3DrvHlp_AsyncNotificationCompleted(PPDMDRVINS pDrvIns)
1410{
1411 PDMDRV_ASSERT_DRVINS(pDrvIns);
1412 PVM pVM = pDrvIns->Internal.s.pVMR3;
1413
1414 VMSTATE enmVMState = VMR3GetState(pVM);
1415 if ( enmVMState == VMSTATE_SUSPENDING
1416 || enmVMState == VMSTATE_SUSPENDING_EXT_LS
1417 || enmVMState == VMSTATE_SUSPENDING_LS
1418 || enmVMState == VMSTATE_RESETTING
1419 || enmVMState == VMSTATE_RESETTING_LS
1420 || enmVMState == VMSTATE_POWERING_OFF
1421 || enmVMState == VMSTATE_POWERING_OFF_LS)
1422 {
1423 LogFlow(("pdmR3DrvHlp_AsyncNotificationCompleted: caller='%s'/%d:\n", pDrvIns->pReg->szName, pDrvIns->iInstance));
1424 VMR3AsyncPdmNotificationWakeupU(pVM->pUVM);
1425 }
1426 else
1427 LogFlow(("pdmR3DrvHlp_AsyncNotificationCompleted: caller='%s'/%d: enmVMState=%d\n", pDrvIns->pReg->szName, pDrvIns->iInstance, enmVMState));
1428}
1429
1430
1431/** @interface_method_impl{PDMDRVHLP,pfnThreadCreate} */
1432static DECLCALLBACK(int) pdmR3DrvHlp_ThreadCreate(PPDMDRVINS pDrvIns, PPPDMTHREAD ppThread, void *pvUser, PFNPDMTHREADDRV pfnThread,
1433 PFNPDMTHREADWAKEUPDRV pfnWakeup, size_t cbStack, RTTHREADTYPE enmType, const char *pszName)
1434{
1435 PDMDRV_ASSERT_DRVINS(pDrvIns);
1436 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
1437 LogFlow(("pdmR3DrvHlp_ThreadCreate: caller='%s'/%d: ppThread=%p pvUser=%p pfnThread=%p pfnWakeup=%p cbStack=%#zx enmType=%d pszName=%p:{%s}\n",
1438 pDrvIns->pReg->szName, pDrvIns->iInstance, ppThread, pvUser, pfnThread, pfnWakeup, cbStack, enmType, pszName, pszName));
1439
1440 int rc = pdmR3ThreadCreateDriver(pDrvIns->Internal.s.pVMR3, pDrvIns, ppThread, pvUser, pfnThread, pfnWakeup, cbStack, enmType, pszName);
1441
1442 LogFlow(("pdmR3DrvHlp_ThreadCreate: caller='%s'/%d: returns %Rrc *ppThread=%RTthrd\n", pDrvIns->pReg->szName, pDrvIns->iInstance,
1443 rc, *ppThread));
1444 return rc;
1445}
1446
1447
1448/** @interface_method_impl{PDMDRVHLP,pfnAsyncCompletionTemplateCreate} */
1449static DECLCALLBACK(int) pdmR3DrvHlp_AsyncCompletionTemplateCreate(PPDMDRVINS pDrvIns, PPPDMASYNCCOMPLETIONTEMPLATE ppTemplate,
1450 PFNPDMASYNCCOMPLETEDRV pfnCompleted, void *pvTemplateUser,
1451 const char *pszDesc)
1452{
1453 PDMDRV_ASSERT_DRVINS(pDrvIns);
1454 LogFlow(("pdmR3DrvHlp_AsyncCompletionTemplateCreate: caller='%s'/%d: ppTemplate=%p pfnCompleted=%p pszDesc=%p:{%s}\n",
1455 pDrvIns->pReg->szName, pDrvIns->iInstance, ppTemplate, pfnCompleted, pszDesc, pszDesc));
1456
1457 int rc = PDMR3AsyncCompletionTemplateCreateDriver(pDrvIns->Internal.s.pVMR3, pDrvIns, ppTemplate, pfnCompleted, pvTemplateUser, pszDesc);
1458
1459 LogFlow(("pdmR3DrvHlp_AsyncCompletionTemplateCreate: caller='%s'/%d: returns %Rrc *ppThread=%p\n", pDrvIns->pReg->szName,
1460 pDrvIns->iInstance, rc, *ppTemplate));
1461 return rc;
1462}
1463
1464
1465/** @interface_method_impl{PDMDRVHLP,pfnLdrGetRCInterfaceSymbols} */
1466static DECLCALLBACK(int) pdmR3DrvHlp_LdrGetRCInterfaceSymbols(PPDMDRVINS pDrvIns, void *pvInterface, size_t cbInterface,
1467 const char *pszSymPrefix, const char *pszSymList)
1468{
1469 PDMDRV_ASSERT_DRVINS(pDrvIns);
1470 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
1471 LogFlow(("pdmR3DrvHlp_LdrGetRCInterfaceSymbols: caller='%s'/%d: pvInterface=%p cbInterface=%zu pszSymPrefix=%p:{%s} pszSymList=%p:{%s}\n",
1472 pDrvIns->pReg->szName, pDrvIns->iInstance, pvInterface, cbInterface, pszSymPrefix, pszSymPrefix, pszSymList, pszSymList));
1473
1474 int rc;
1475 if ( strncmp(pszSymPrefix, "drv", 3) == 0
1476 && RTStrIStr(pszSymPrefix + 3, pDrvIns->pReg->szName) != NULL)
1477 {
1478 if (pDrvIns->pReg->fFlags & PDM_DRVREG_FLAGS_RC)
1479 rc = PDMR3LdrGetInterfaceSymbols(pDrvIns->Internal.s.pVMR3,
1480 pvInterface, cbInterface,
1481 pDrvIns->pReg->szRCMod, pDrvIns->Internal.s.pDrv->pszRCSearchPath,
1482 pszSymPrefix, pszSymList,
1483 false /*fRing0OrRC*/);
1484 else
1485 {
1486 AssertMsgFailed(("Not a raw-mode enabled driver\n"));
1487 rc = VERR_PERMISSION_DENIED;
1488 }
1489 }
1490 else
1491 {
1492 AssertMsgFailed(("Invalid prefix '%s' for '%s'; must start with 'drv' and contain the driver name!\n",
1493 pszSymPrefix, pDrvIns->pReg->szName));
1494 rc = VERR_INVALID_NAME;
1495 }
1496
1497 LogFlow(("pdmR3DrvHlp_LdrGetRCInterfaceSymbols: caller='%s'/%d: returns %Rrc\n", pDrvIns->pReg->szName,
1498 pDrvIns->iInstance, rc));
1499 return rc;
1500}
1501
1502
1503/** @interface_method_impl{PDMDRVHLP,pfnLdrGetR0InterfaceSymbols} */
1504static DECLCALLBACK(int) pdmR3DrvHlp_LdrGetR0InterfaceSymbols(PPDMDRVINS pDrvIns, void *pvInterface, size_t cbInterface,
1505 const char *pszSymPrefix, const char *pszSymList)
1506{
1507 PDMDRV_ASSERT_DRVINS(pDrvIns);
1508 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
1509 LogFlow(("pdmR3DrvHlp_LdrGetR0InterfaceSymbols: caller='%s'/%d: pvInterface=%p cbInterface=%zu pszSymPrefix=%p:{%s} pszSymList=%p:{%s}\n",
1510 pDrvIns->pReg->szName, pDrvIns->iInstance, pvInterface, cbInterface, pszSymPrefix, pszSymPrefix, pszSymList, pszSymList));
1511
1512 int rc;
1513 if ( strncmp(pszSymPrefix, "drv", 3) == 0
1514 && RTStrIStr(pszSymPrefix + 3, pDrvIns->pReg->szName) != NULL)
1515 {
1516 if (pDrvIns->pReg->fFlags & PDM_DRVREG_FLAGS_R0)
1517 rc = PDMR3LdrGetInterfaceSymbols(pDrvIns->Internal.s.pVMR3,
1518 pvInterface, cbInterface,
1519 pDrvIns->pReg->szR0Mod, pDrvIns->Internal.s.pDrv->pszR0SearchPath,
1520 pszSymPrefix, pszSymList,
1521 true /*fRing0OrRC*/);
1522 else
1523 {
1524 AssertMsgFailed(("Not a ring-0 enabled driver\n"));
1525 rc = VERR_PERMISSION_DENIED;
1526 }
1527 }
1528 else
1529 {
1530 AssertMsgFailed(("Invalid prefix '%s' for '%s'; must start with 'drv' and contain the driver name!\n",
1531 pszSymPrefix, pDrvIns->pReg->szName));
1532 rc = VERR_INVALID_NAME;
1533 }
1534
1535 LogFlow(("pdmR3DrvHlp_LdrGetR0InterfaceSymbols: caller='%s'/%d: returns %Rrc\n", pDrvIns->pReg->szName,
1536 pDrvIns->iInstance, rc));
1537 return rc;
1538}
1539
1540
1541/** @interface_method_impl{PDMDRVHLP,pfnCritSectInit} */
1542static DECLCALLBACK(int) pdmR3DrvHlp_CritSectInit(PPDMDRVINS pDrvIns, PPDMCRITSECT pCritSect,
1543 RT_SRC_POS_DECL, const char *pszName)
1544{
1545 PDMDRV_ASSERT_DRVINS(pDrvIns);
1546 PVM pVM = pDrvIns->Internal.s.pVMR3;
1547 VM_ASSERT_EMT(pVM);
1548 LogFlow(("pdmR3DrvHlp_CritSectInit: caller='%s'/%d: pCritSect=%p pszName=%s\n",
1549 pDrvIns->pReg->szName, pDrvIns->iInstance, pCritSect, pszName));
1550
1551 int rc = pdmR3CritSectInitDriver(pVM, pDrvIns, pCritSect, RT_SRC_POS_ARGS, "%s_%u", pszName, pDrvIns->iInstance);
1552
1553 LogFlow(("pdmR3DrvHlp_CritSectInit: caller='%s'/%d: returns %Rrc\n", pDrvIns->pReg->szName,
1554 pDrvIns->iInstance, rc));
1555 return rc;
1556}
1557
1558
1559/** @interface_method_impl{PDMDRVHLP,pfnCallR0} */
1560static DECLCALLBACK(int) pdmR3DrvHlp_CallR0(PPDMDRVINS pDrvIns, uint32_t uOperation, uint64_t u64Arg)
1561{
1562 PDMDRV_ASSERT_DRVINS(pDrvIns);
1563 PVM pVM = pDrvIns->Internal.s.pVMR3;
1564 LogFlow(("pdmR3DrvHlp_CallR0: caller='%s'/%d: uOperation=%#x u64Arg=%#RX64\n",
1565 pDrvIns->pReg->szName, pDrvIns->iInstance, uOperation, u64Arg));
1566
1567 /*
1568 * Lazy resolve the ring-0 entry point.
1569 */
1570 int rc = VINF_SUCCESS;
1571 PFNPDMDRVREQHANDLERR0 pfnReqHandlerR0 = pDrvIns->Internal.s.pfnReqHandlerR0;
1572 if (RT_UNLIKELY(pfnReqHandlerR0 == NIL_RTR0PTR))
1573 {
1574 if (pDrvIns->pReg->fFlags & PDM_DRVREG_FLAGS_R0)
1575 {
1576 char szSymbol[ sizeof("drvR0") + sizeof(pDrvIns->pReg->szName) + sizeof("ReqHandler")];
1577 strcat(strcat(strcpy(szSymbol, "drvR0"), pDrvIns->pReg->szName), "ReqHandler");
1578 szSymbol[sizeof("drvR0") - 1] = RT_C_TO_UPPER(szSymbol[sizeof("drvR0") - 1]);
1579
1580 rc = PDMR3LdrGetSymbolR0Lazy(pVM, pDrvIns->pReg->szR0Mod, pDrvIns->Internal.s.pDrv->pszR0SearchPath, szSymbol,
1581 &pfnReqHandlerR0);
1582 if (RT_SUCCESS(rc))
1583 pDrvIns->Internal.s.pfnReqHandlerR0 = pfnReqHandlerR0;
1584 else
1585 pfnReqHandlerR0 = NIL_RTR0PTR;
1586 }
1587 else
1588 rc = VERR_ACCESS_DENIED;
1589 }
1590 if (RT_LIKELY(pfnReqHandlerR0 != NIL_RTR0PTR))
1591 {
1592 /*
1593 * Make the ring-0 call.
1594 */
1595 PDMDRIVERCALLREQHANDLERREQ Req;
1596 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
1597 Req.Hdr.cbReq = sizeof(Req);
1598 Req.pDrvInsR0 = PDMDRVINS_2_R0PTR(pDrvIns);
1599 Req.uOperation = uOperation;
1600 Req.u32Alignment = 0;
1601 Req.u64Arg = u64Arg;
1602 rc = SUPR3CallVMMR0Ex(pVM->pVMR0, NIL_VMCPUID, VMMR0_DO_PDM_DRIVER_CALL_REQ_HANDLER, 0, &Req.Hdr);
1603 }
1604
1605 LogFlow(("pdmR3DrvHlp_CallR0: caller='%s'/%d: returns %Rrc\n", pDrvIns->pReg->szName,
1606 pDrvIns->iInstance, rc));
1607 return rc;
1608}
1609
1610
1611/** @interface_method_impl{PDMDRVHLP,pfnFTSetCheckpoint} */
1612static DECLCALLBACK(int) pdmR3DrvHlp_FTSetCheckpoint(PPDMDRVINS pDrvIns, FTMCHECKPOINTTYPE enmType)
1613{
1614 PDMDRV_ASSERT_DRVINS(pDrvIns);
1615 return FTMSetCheckpoint(pDrvIns->Internal.s.pVMR3, enmType);
1616}
1617
1618
1619/** @interface_method_impl{PDMDRVHLP,pfnBlkCacheRetain} */
1620static DECLCALLBACK(int) pdmR3DrvHlp_BlkCacheRetain(PPDMDRVINS pDrvIns, PPPDMBLKCACHE ppBlkCache,
1621 PFNPDMBLKCACHEXFERCOMPLETEDRV pfnXferComplete,
1622 PFNPDMBLKCACHEXFERENQUEUEDRV pfnXferEnqueue,
1623 PFNPDMBLKCACHEXFERENQUEUEDISCARDDRV pfnXferEnqueueDiscard,
1624 const char *pcszId)
1625{
1626 PDMDRV_ASSERT_DRVINS(pDrvIns);
1627 return PDMR3BlkCacheRetainDriver(pDrvIns->Internal.s.pVMR3, pDrvIns, ppBlkCache,
1628 pfnXferComplete, pfnXferEnqueue, pfnXferEnqueueDiscard, pcszId);
1629}
1630
1631
1632/**
1633 * The driver helper structure.
1634 */
1635const PDMDRVHLPR3 g_pdmR3DrvHlp =
1636{
1637 PDM_DRVHLPR3_VERSION,
1638 pdmR3DrvHlp_Attach,
1639 pdmR3DrvHlp_Detach,
1640 pdmR3DrvHlp_DetachSelf,
1641 pdmR3DrvHlp_MountPrepare,
1642 pdmR3DrvHlp_AssertEMT,
1643 pdmR3DrvHlp_AssertOther,
1644 pdmR3DrvHlp_VMSetError,
1645 pdmR3DrvHlp_VMSetErrorV,
1646 pdmR3DrvHlp_VMSetRuntimeError,
1647 pdmR3DrvHlp_VMSetRuntimeErrorV,
1648 pdmR3DrvHlp_VMState,
1649 pdmR3DrvHlp_VMTeleportedAndNotFullyResumedYet,
1650 pdmR3DrvHlp_GetSupDrvSession,
1651 pdmR3DrvHlp_QueueCreate,
1652 pdmR3DrvHlp_TMGetVirtualFreq,
1653 pdmR3DrvHlp_TMGetVirtualTime,
1654 pdmR3DrvHlp_TMTimerCreate,
1655 pdmR3DrvHlp_SSMRegister,
1656 pdmR3DrvHlp_SSMDeregister,
1657 pdmR3DrvHlp_DBGFInfoRegister,
1658 pdmR3DrvHlp_DBGFInfoDeregister,
1659 pdmR3DrvHlp_STAMRegister,
1660 pdmR3DrvHlp_STAMRegisterF,
1661 pdmR3DrvHlp_STAMRegisterV,
1662 pdmR3DrvHlp_STAMDeregister,
1663 pdmR3DrvHlp_SUPCallVMMR0Ex,
1664 pdmR3DrvHlp_USBRegisterHub,
1665 pdmR3DrvHlp_SetAsyncNotification,
1666 pdmR3DrvHlp_AsyncNotificationCompleted,
1667 pdmR3DrvHlp_ThreadCreate,
1668 pdmR3DrvHlp_AsyncCompletionTemplateCreate,
1669 pdmR3DrvHlp_LdrGetRCInterfaceSymbols,
1670 pdmR3DrvHlp_LdrGetR0InterfaceSymbols,
1671 pdmR3DrvHlp_CritSectInit,
1672 pdmR3DrvHlp_CallR0,
1673 pdmR3DrvHlp_FTSetCheckpoint,
1674 pdmR3DrvHlp_BlkCacheRetain,
1675 PDM_DRVHLPR3_VERSION /* u32TheEnd */
1676};
1677
1678/** @} */
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