VirtualBox

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

Last change on this file since 26168 was 26168, checked in by vboxsync, 15 years ago

PDM: s/pDrvHlp\(|R0|R3|RC\)/pHlp\1/g - PDMDRVINS.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 51.0 KB
Line 
1/* $Id: PDMDriver.cpp 26168 2010-02-02 20:00:05Z vboxsync $ */
2/** @file
3 * PDM - Pluggable Device and Driver Manager, Driver parts.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP LOG_GROUP_PDM_DRIVER
27#include "PDMInternal.h"
28#include <VBox/pdm.h>
29#include <VBox/mm.h>
30#include <VBox/cfgm.h>
31#include <VBox/vmm.h>
32#include <VBox/sup.h>
33#include <VBox/vm.h>
34#include <VBox/version.h>
35#include <VBox/err.h>
36
37#include <VBox/log.h>
38#include <iprt/assert.h>
39#include <iprt/thread.h>
40#include <iprt/string.h>
41#include <iprt/asm.h>
42#include <iprt/alloc.h>
43#include <iprt/path.h>
44
45
46/*******************************************************************************
47* Structures and Typedefs *
48*******************************************************************************/
49/**
50 * Internal callback structure pointer.
51 *
52 * The main purpose is to define the extra data we associate
53 * with PDMDRVREGCB so we can find the VM instance and so on.
54 */
55typedef struct PDMDRVREGCBINT
56{
57 /** The callback structure. */
58 PDMDRVREGCB Core;
59 /** A bit of padding. */
60 uint32_t u32[4];
61 /** VM Handle. */
62 PVM pVM;
63} PDMDRVREGCBINT, *PPDMDRVREGCBINT;
64typedef const PDMDRVREGCBINT *PCPDMDRVREGCBINT;
65
66
67/*******************************************************************************
68* Internal Functions *
69*******************************************************************************/
70/** @def PDMDRV_ASSERT_DRVINS
71 * Asserts the validity of the driver instance.
72 */
73#ifdef VBOX_STRICT
74# define PDMDRV_ASSERT_DRVINS(pDrvIns) \
75 do { \
76 AssertPtr(pDrvIns); \
77 Assert(pDrvIns->u32Version == PDM_DRVINS_VERSION); \
78 Assert(pDrvIns->pvInstanceDataR3 == (void *)&pDrvIns->achInstanceData[0]); \
79 } while (0)
80#else
81# define PDMDRV_ASSERT_DRVINS(pDrvIns) do { } while (0)
82#endif
83/** @} */
84
85static DECLCALLBACK(int) pdmR3DrvRegister(PCPDMDRVREGCB pCallbacks, PCPDMDRVREG pReg);
86static int pdmR3DrvLoad(PVM pVM, PPDMDRVREGCBINT pRegCB, const char *pszFilename, const char *pszName);
87
88
89
90/**
91 * Register external drivers
92 *
93 * @returns VBox status code.
94 * @param pVM The VM to operate on.
95 * @param pfnCallback Driver registration callback
96 */
97VMMR3DECL(int) PDMR3RegisterDrivers(PVM pVM, FNPDMVBOXDRIVERSREGISTER pfnCallback)
98{
99 /*
100 * The registration callbacks.
101 */
102 PDMDRVREGCBINT RegCB;
103 RegCB.Core.u32Version = PDM_DRVREG_CB_VERSION;
104 RegCB.Core.pfnRegister = pdmR3DrvRegister;
105 RegCB.pVM = pVM;
106
107 int rc = pfnCallback(&RegCB.Core, VBOX_VERSION);
108 if (RT_FAILURE(rc))
109 AssertMsgFailed(("VBoxDriversRegister failed with rc=%Rrc\n"));
110
111 return rc;
112}
113
114/**
115 * This function will initialize the drivers for this VM instance.
116 *
117 * First of all this mean loading the builtin drivers and letting them
118 * register themselves. Beyond that any additional driver modules are
119 * loaded and called for registration.
120 *
121 * @returns VBox status code.
122 * @param pVM VM Handle.
123 */
124int pdmR3DrvInit(PVM pVM)
125{
126 LogFlow(("pdmR3DrvInit:\n"));
127
128 AssertRelease(!(RT_OFFSETOF(PDMDRVINS, achInstanceData) & 15));
129 PPDMDRVINS pDrvInsAssert;
130 AssertRelease(sizeof(pDrvInsAssert->Internal.s) <= sizeof(pDrvInsAssert->Internal.padding));
131
132 /*
133 * The registration callbacks.
134 */
135 PDMDRVREGCBINT RegCB;
136 RegCB.Core.u32Version = PDM_DRVREG_CB_VERSION;
137 RegCB.Core.pfnRegister = pdmR3DrvRegister;
138 RegCB.pVM = pVM;
139
140 /*
141 * Load the builtin module
142 */
143 PCFGMNODE pDriversNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "PDM/Drivers");
144 bool fLoadBuiltin;
145 int rc = CFGMR3QueryBool(pDriversNode, "LoadBuiltin", &fLoadBuiltin);
146 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
147 fLoadBuiltin = true;
148 else if (RT_FAILURE(rc))
149 {
150 AssertMsgFailed(("Configuration error: Querying boolean \"LoadBuiltin\" failed with %Rrc\n", rc));
151 return rc;
152 }
153 if (fLoadBuiltin)
154 {
155 /* make filename */
156 char *pszFilename = pdmR3FileR3("VBoxDD", /*fShared=*/true);
157 if (!pszFilename)
158 return VERR_NO_TMP_MEMORY;
159 rc = pdmR3DrvLoad(pVM, &RegCB, pszFilename, "VBoxDD");
160 RTMemTmpFree(pszFilename);
161 if (RT_FAILURE(rc))
162 return rc;
163 }
164
165 /*
166 * Load additional driver modules.
167 */
168 for (PCFGMNODE pCur = CFGMR3GetFirstChild(pDriversNode); pCur; pCur = CFGMR3GetNextChild(pCur))
169 {
170 /*
171 * Get the name and path.
172 */
173 char szName[PDMMOD_NAME_LEN];
174 rc = CFGMR3GetName(pCur, &szName[0], sizeof(szName));
175 if (rc == VERR_CFGM_NOT_ENOUGH_SPACE)
176 {
177 AssertMsgFailed(("configuration error: The module name is too long, cchName=%zu.\n", CFGMR3GetNameLen(pCur)));
178 return VERR_PDM_MODULE_NAME_TOO_LONG;
179 }
180 else if (RT_FAILURE(rc))
181 {
182 AssertMsgFailed(("CFGMR3GetName -> %Rrc.\n", rc));
183 return rc;
184 }
185
186 /* the path is optional, if no path the module name + path is used. */
187 char szFilename[RTPATH_MAX];
188 rc = CFGMR3QueryString(pCur, "Path", &szFilename[0], sizeof(szFilename));
189 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
190 strcpy(szFilename, szName);
191 else if (RT_FAILURE(rc))
192 {
193 AssertMsgFailed(("configuration error: Failure to query the module path, rc=%Rrc.\n", rc));
194 return rc;
195 }
196
197 /* prepend path? */
198 if (!RTPathHavePath(szFilename))
199 {
200 char *psz = pdmR3FileR3(szFilename);
201 if (!psz)
202 return VERR_NO_TMP_MEMORY;
203 size_t cch = strlen(psz) + 1;
204 if (cch > sizeof(szFilename))
205 {
206 RTMemTmpFree(psz);
207 AssertMsgFailed(("Filename too long! cch=%d '%s'\n", cch, psz));
208 return VERR_FILENAME_TOO_LONG;
209 }
210 memcpy(szFilename, psz, cch);
211 RTMemTmpFree(psz);
212 }
213
214 /*
215 * Load the module and register it's drivers.
216 */
217 rc = pdmR3DrvLoad(pVM, &RegCB, szFilename, szName);
218 if (RT_FAILURE(rc))
219 return rc;
220 }
221
222 LogFlow(("pdmR3DrvInit: returns VINF_SUCCESS\n"));
223 return VINF_SUCCESS;
224}
225
226
227/**
228 * Loads one driver module and call the registration entry point.
229 *
230 * @returns VBox status code.
231 * @param pVM VM handle.
232 * @param pRegCB The registration callback stuff.
233 * @param pszFilename Module filename.
234 * @param pszName Module name.
235 */
236static int pdmR3DrvLoad(PVM pVM, PPDMDRVREGCBINT pRegCB, const char *pszFilename, const char *pszName)
237{
238 /*
239 * Load it.
240 */
241 int rc = pdmR3LoadR3U(pVM->pUVM, pszFilename, pszName);
242 if (RT_SUCCESS(rc))
243 {
244 /*
245 * Get the registration export and call it.
246 */
247 FNPDMVBOXDRIVERSREGISTER *pfnVBoxDriversRegister;
248 rc = PDMR3LdrGetSymbolR3(pVM, pszName, "VBoxDriversRegister", (void **)&pfnVBoxDriversRegister);
249 if (RT_SUCCESS(rc))
250 {
251 Log(("PDM: Calling VBoxDriversRegister (%p) of %s (%s)\n", pfnVBoxDriversRegister, pszName, pszFilename));
252 rc = pfnVBoxDriversRegister(&pRegCB->Core, VBOX_VERSION);
253 if (RT_SUCCESS(rc))
254 Log(("PDM: Successfully loaded driver module %s (%s).\n", pszName, pszFilename));
255 else
256 AssertMsgFailed(("VBoxDriversRegister failed with rc=%Rrc\n"));
257 }
258 else
259 {
260 AssertMsgFailed(("Failed to locate 'VBoxDriversRegister' in %s (%s) rc=%Rrc\n", pszName, pszFilename, rc));
261 if (rc == VERR_SYMBOL_NOT_FOUND)
262 rc = VERR_PDM_NO_REGISTRATION_EXPORT;
263 }
264 }
265 else
266 AssertMsgFailed(("Failed to load %s (%s) rc=%Rrc!\n", pszName, pszFilename, rc));
267 return rc;
268}
269
270
271/** @interface_method_impl{PDMDRVREGCB,pfnRegister} */
272static DECLCALLBACK(int) pdmR3DrvRegister(PCPDMDRVREGCB pCallbacks, PCPDMDRVREG pReg)
273{
274 /*
275 * Validate the registration structure.
276 */
277 AssertPtrReturn(pReg, VERR_INVALID_POINTER);
278 AssertMsgReturn(pReg->u32Version == PDM_DRVREG_VERSION,
279 ("%#x\n", pReg->u32Version),
280 VERR_PDM_UNKNOWN_DRVREG_VERSION);
281 AssertReturn(pReg->szName[0], VERR_PDM_INVALID_DRIVER_REGISTRATION);
282 AssertMsgReturn(memchr(pReg->szName, '\0', sizeof(pReg->szName)),
283 (".*s\n", sizeof(pReg->szName), pReg->szName),
284 VERR_PDM_INVALID_DRIVER_REGISTRATION);
285 AssertMsgReturn( !(pReg->fFlags & PDM_DRVREG_FLAGS_R0)
286 || ( pReg->szR0Mod[0]
287 && memchr(pReg->szR0Mod, '\0', sizeof(pReg->szR0Mod))),
288 ("%s: %.*s\n", pReg->szName, sizeof(pReg->szR0Mod), pReg->szR0Mod),
289 VERR_PDM_INVALID_DRIVER_REGISTRATION);
290 AssertMsgReturn( !(pReg->fFlags & PDM_DRVREG_FLAGS_RC)
291 || ( pReg->szRCMod[0]
292 && memchr(pReg->szRCMod, '\0', sizeof(pReg->szRCMod))),
293 ("%s: %.*s\n", pReg->szName, sizeof(pReg->szRCMod), pReg->szRCMod),
294 VERR_PDM_INVALID_DRIVER_REGISTRATION);
295 AssertMsgReturn(VALID_PTR(pReg->pszDescription),
296 ("%s: %p\n", pReg->szName, pReg->pszDescription),
297 VERR_PDM_INVALID_DRIVER_REGISTRATION);
298 AssertMsgReturn(!(pReg->fFlags & ~(PDM_DRVREG_FLAGS_HOST_BITS_MASK | PDM_DRVREG_FLAGS_R0 | PDM_DRVREG_FLAGS_RC)),
299 ("%s: %#x\n", pReg->szName, pReg->fFlags),
300 VERR_PDM_INVALID_DRIVER_REGISTRATION);
301 AssertMsgReturn((pReg->fFlags & PDM_DRVREG_FLAGS_HOST_BITS_MASK) == PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
302 ("%s: %#x\n", pReg->szName, pReg->fFlags),
303 VERR_PDM_INVALID_DRIVER_HOST_BITS);
304 AssertMsgReturn(pReg->cMaxInstances > 0,
305 ("%s: %#x\n", pReg->szName, pReg->cMaxInstances),
306 VERR_PDM_INVALID_DRIVER_REGISTRATION);
307 AssertMsgReturn(pReg->cbInstance <= _1M,
308 ("%s: %#x\n", pReg->szName, pReg->cbInstance),
309 VERR_PDM_INVALID_DRIVER_REGISTRATION);
310 AssertMsgReturn(VALID_PTR(pReg->pfnConstruct),
311 ("%s: %p\n", pReg->szName, pReg->pfnConstruct),
312 VERR_PDM_INVALID_DRIVER_REGISTRATION);
313 AssertMsgReturn(VALID_PTR(pReg->pfnRelocate) || !(pReg->fFlags & PDM_DRVREG_FLAGS_RC),
314 ("%s: %#x\n", pReg->szName, pReg->cbInstance),
315 VERR_PDM_INVALID_DRIVER_REGISTRATION);
316 AssertMsgReturn(pReg->pfnSoftReset == NULL,
317 ("%s: %p\n", pReg->szName, pReg->pfnSoftReset),
318 VERR_PDM_INVALID_DRIVER_REGISTRATION);
319 AssertMsgReturn(pReg->u32VersionEnd == PDM_DRVREG_VERSION,
320 ("%s: #x\n", pReg->szName, pReg->u32VersionEnd),
321 VERR_PDM_INVALID_DRIVER_REGISTRATION);
322
323 /*
324 * Check for duplicate and find FIFO entry at the same time.
325 */
326 PCPDMDRVREGCBINT pRegCB = (PCPDMDRVREGCBINT)pCallbacks;
327 PPDMDRV pDrvPrev = NULL;
328 PPDMDRV pDrv = pRegCB->pVM->pdm.s.pDrvs;
329 for (; pDrv; pDrvPrev = pDrv, pDrv = pDrv->pNext)
330 {
331 if (!strcmp(pDrv->pReg->szName, pReg->szName))
332 {
333 AssertMsgFailed(("Driver '%s' already exists\n", pReg->szName));
334 return VERR_PDM_DRIVER_NAME_CLASH;
335 }
336 }
337
338 /*
339 * Allocate new driver structure and insert it into the list.
340 */
341 pDrv = (PPDMDRV)MMR3HeapAlloc(pRegCB->pVM, MM_TAG_PDM_DRIVER, sizeof(*pDrv));
342 if (pDrv)
343 {
344 pDrv->pNext = NULL;
345 pDrv->cInstances = 0;
346 pDrv->iNextInstance = 0;
347 pDrv->pReg = pReg;
348
349 if (pDrvPrev)
350 pDrvPrev->pNext = pDrv;
351 else
352 pRegCB->pVM->pdm.s.pDrvs = pDrv;
353 Log(("PDM: Registered driver '%s'\n", pReg->szName));
354 return VINF_SUCCESS;
355 }
356 return VERR_NO_MEMORY;
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 * Instantiate a driver.
375 *
376 * @returns VBox status code, including informational statuses.
377 *
378 * @param pVM The VM handle.
379 * @param pNode The CFGM node for the driver.
380 * @param pBaseInterface The base interface.
381 * @param pDrvAbove The driver above it. NULL if it's the top-most
382 * driver.
383 * @param pLun The LUN the driver is being attached to. NULL
384 * if we're instantiating a driver chain before
385 * attaching it - untested.
386 * @param ppBaseInterface Where to return the pointer to the base
387 * interface of the newly created driver.
388 *
389 * @remarks Recursive calls to this function is normal as the drivers will
390 * attach to anything below them during the pfnContruct call.
391 */
392int pdmR3DrvInstantiate(PVM pVM, PCFGMNODE pNode, PPDMIBASE pBaseInterface, PPDMDRVINS pDrvAbove,
393 PPDMLUN pLun, PPDMIBASE *ppBaseInterface)
394{
395 Assert(!pDrvAbove || !pDrvAbove->Internal.s.pDown);
396 Assert(!pDrvAbove || !pDrvAbove->pDownBase);
397
398 Assert(pBaseInterface->pfnQueryInterface(pBaseInterface, PDMIBASE_IID) == pBaseInterface);
399
400 /*
401 * Find the driver.
402 */
403 char *pszName;
404 int rc = CFGMR3QueryStringAlloc(pNode, "Driver", &pszName);
405 if (RT_SUCCESS(rc))
406 {
407 PPDMDRV pDrv = pdmR3DrvLookup(pVM, pszName);
408 MMR3HeapFree(pszName);
409 if ( pDrv
410 && pDrv->cInstances < pDrv->pReg->cMaxInstances)
411 {
412 /* config node */
413 PCFGMNODE pConfigNode = CFGMR3GetChild(pNode, "Config");
414 if (!pConfigNode)
415 rc = CFGMR3InsertNode(pNode, "Config", &pConfigNode);
416 if (RT_SUCCESS(rc))
417 {
418 CFGMR3SetRestrictedRoot(pConfigNode);
419
420 /*
421 * Allocate the driver instance.
422 */
423 size_t cb = RT_OFFSETOF(PDMDRVINS, achInstanceData[pDrv->pReg->cbInstance]);
424 cb = RT_ALIGN_Z(cb, 16);
425 bool const fHyperHeap = !!(pDrv->pReg->fFlags & (PDM_DRVREG_FLAGS_R0 | PDM_DRVREG_FLAGS_RC));
426 PPDMDRVINS pNew;
427 if (fHyperHeap)
428 rc = MMHyperAlloc(pVM, cb, 64, MM_TAG_PDM_DRIVER, (void **)&pNew);
429 else
430 rc = MMR3HeapAllocZEx(pVM, MM_TAG_PDM_DRIVER, cb, (void **)&pNew);
431 if (pNew)
432 {
433 /*
434 * Initialize the instance structure (declaration order).
435 */
436 pNew->u32Version = PDM_DRVINS_VERSION;
437 pNew->Internal.s.pUp = pDrvAbove ? pDrvAbove : NULL;
438 //pNew->Internal.s.pDown = NULL;
439 pNew->Internal.s.pLun = pLun;
440 pNew->Internal.s.pDrv = pDrv;
441 pNew->Internal.s.pVMR3 = pVM;
442 pNew->Internal.s.pVMR0 = pDrv->pReg->fFlags & PDM_DRVREG_FLAGS_R0 ? pVM->pVMR0 : NIL_RTR0PTR;
443 pNew->Internal.s.pVMRC = pDrv->pReg->fFlags & PDM_DRVREG_FLAGS_RC ? pVM->pVMRC : NIL_RTRCPTR;
444 //pNew->Internal.s.fDetaching = false;
445 pNew->Internal.s.fVMSuspended = true;
446 //pNew->Internal.s.fVMReset = false;
447 pNew->Internal.s.fHyperHeap = fHyperHeap;
448 //pNew->Internal.s.pfnAsyncNotify = NULL;
449 pNew->Internal.s.pCfgHandle = pNode;
450 pNew->pReg = pDrv->pReg;
451 pNew->pCfgHandle = pConfigNode;
452 pNew->iInstance = pDrv->iNextInstance;
453 pNew->pUpBase = pBaseInterface;
454 Assert(!pDrvAbove || pBaseInterface == &pDrvAbove->IBase);
455 //pNew->pDownBase = NULL;
456 //pNew->IBase.pfnQueryInterface = NULL;
457 pNew->pHlpR3 = &g_pdmR3DrvHlp;
458 pNew->pvInstanceDataR3 = &pNew->achInstanceData[0];
459 if (pDrv->pReg->fFlags & PDM_DRVREG_FLAGS_R0)
460 {
461 pNew->pvInstanceDataR0 = MMHyperR3ToR0(pVM, &pNew->achInstanceData[0]);
462 rc = PDMR3LdrGetSymbolR0(pVM, NULL, "g_pdmR0DrvHlp", &pNew->pHlpR0);
463 AssertReleaseRCReturn(rc, rc);
464
465 }
466 if (pDrv->pReg->fFlags & PDM_DRVREG_FLAGS_RC)
467 {
468 pNew->pvInstanceDataR0 = MMHyperR3ToRC(pVM, &pNew->achInstanceData[0]);
469 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_pdmRCDrvHlp", &pNew->pHlpRC);
470 AssertReleaseRCReturn(rc, rc);
471 }
472
473 pDrv->iNextInstance++;
474 pDrv->cInstances++;
475
476 /*
477 * Link with it with the driver above / LUN.
478 */
479 if (pDrvAbove)
480 {
481 pDrvAbove->pDownBase = &pNew->IBase;
482 pDrvAbove->Internal.s.pDown = pNew;
483 }
484 else if (pLun)
485 pLun->pTop = pNew;
486 if (pLun)
487 pLun->pBottom = pNew;
488
489 /*
490 * Invoke the constructor.
491 */
492 rc = pDrv->pReg->pfnConstruct(pNew, pNew->pCfgHandle, 0 /*fFlags*/);
493 if (RT_SUCCESS(rc))
494 {
495 AssertPtr(pNew->IBase.pfnQueryInterface);
496 Assert(pNew->IBase.pfnQueryInterface(&pNew->IBase, PDMIBASE_IID) == &pNew->IBase);
497
498 /* Success! */
499 *ppBaseInterface = &pNew->IBase;
500 if (pLun)
501 Log(("PDM: Attached driver %p:'%s'/%d to LUN#%d on device '%s'/%d, pDrvAbove=%p:'%s'/%d\n",
502 pNew, pDrv->pReg->szName, pNew->iInstance,
503 pLun->iLun,
504 pLun->pDevIns ? pLun->pDevIns->pReg->szName : pLun->pUsbIns->pReg->szName,
505 pLun->pDevIns ? pLun->pDevIns->iInstance : pLun->pUsbIns->iInstance,
506 pDrvAbove, pDrvAbove ? pDrvAbove->pReg->szName : "", pDrvAbove ? pDrvAbove->iInstance : -1));
507 else
508 Log(("PDM: Attached driver %p:'%s'/%d, pDrvAbove=%p:'%s'/%d\n",
509 pNew, pDrv->pReg->szName, pNew->iInstance,
510 pDrvAbove, pDrvAbove ? pDrvAbove->pReg->szName : "", pDrvAbove ? pDrvAbove->iInstance : -1));
511 }
512 else
513 pdmR3DrvDestroyChain(pNew, PDM_TACH_FLAGS_NO_CALLBACKS);
514 }
515 else
516 {
517 AssertMsgFailed(("Failed to allocate %d bytes for instantiating driver '%s'\n", cb, pszName));
518 rc = VERR_NO_MEMORY;
519 }
520 }
521 else
522 AssertMsgFailed(("Failed to create Config node! rc=%Rrc\n", rc));
523 }
524 else if (pDrv)
525 {
526 AssertMsgFailed(("Too many instances of driver '%s', max is %u\n", pszName, pDrv->pReg->cMaxInstances));
527 rc = VERR_PDM_TOO_MANY_DRIVER_INSTANCES;
528 }
529 else
530 {
531 AssertMsgFailed(("Driver '%s' wasn't found!\n", pszName));
532 rc = VERR_PDM_DRIVER_NOT_FOUND;
533 }
534 }
535 else
536 {
537 AssertMsgFailed(("Query for string value of \"Driver\" -> %Rrc\n", rc));
538 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
539 rc = VERR_PDM_CFG_MISSING_DRIVER_NAME;
540 }
541 return rc;
542}
543
544
545/**
546 * Detaches a driver from whatever it's attached to.
547 * This will of course lead to the destruction of the driver and all drivers below it in the chain.
548 *
549 * @returns VINF_SUCCESS
550 * @param pDrvIns The driver instance to detach.
551 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
552 */
553int pdmR3DrvDetach(PPDMDRVINS pDrvIns, uint32_t fFlags)
554{
555 PDMDRV_ASSERT_DRVINS(pDrvIns);
556 LogFlow(("pdmR3DrvDetach: pDrvIns=%p '%s'/%d\n", pDrvIns, pDrvIns->pReg->szName, pDrvIns->iInstance));
557 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
558
559 /*
560 * Check that we're not doing this recursively, that could have unwanted sideeffects!
561 */
562 if (pDrvIns->Internal.s.fDetaching)
563 {
564 AssertMsgFailed(("Recursive detach! '%s'/%d\n", pDrvIns->pReg->szName, pDrvIns->iInstance));
565 return VINF_SUCCESS; }
566
567 /*
568 * Check that we actually can detach this instance.
569 * The requirement is that the driver/device above has a detach method.
570 */
571 if (pDrvIns->Internal.s.pUp
572 ? !pDrvIns->Internal.s.pUp->pReg->pfnDetach
573 : !pDrvIns->Internal.s.pLun->pDevIns->pReg->pfnDetach)
574 {
575 AssertMsgFailed(("Cannot detach driver instance because the driver/device above doesn't support it!\n"));
576 return VERR_PDM_DRIVER_DETACH_NOT_POSSIBLE;
577 }
578
579 /*
580 * Join paths with pdmR3DrvDestroyChain.
581 */
582 pdmR3DrvDestroyChain(pDrvIns, fFlags);
583 return VINF_SUCCESS;
584}
585
586
587/**
588 * Destroys a driver chain starting with the specified driver.
589 *
590 * This is used when unplugging a device at run time.
591 *
592 * @param pDrvIns Pointer to the driver instance to start with.
593 * @param fFlags PDM_TACH_FLAGS_NOT_HOT_PLUG, PDM_TACH_FLAGS_NO_CALLBACKS
594 * or 0.
595 */
596void pdmR3DrvDestroyChain(PPDMDRVINS pDrvIns, uint32_t fFlags)
597{
598 PVM pVM = pDrvIns->Internal.s.pVMR3;
599 VM_ASSERT_EMT(pVM);
600
601 /*
602 * Detach the bottommost driver until we've detached pDrvIns.
603 */
604 pDrvIns->Internal.s.fDetaching = true;
605 PPDMDRVINS pCur;
606 do
607 {
608 /* find the driver to detach. */
609 pCur = pDrvIns;
610 while (pCur->Internal.s.pDown)
611 pCur = pCur->Internal.s.pDown;
612 LogFlow(("pdmR3DrvDestroyChain: pCur=%p '%s'/%d\n", pCur, pCur->pReg->szName, pCur->iInstance));
613
614 /*
615 * Unlink it and notify parent.
616 */
617 pCur->Internal.s.fDetaching = true;
618
619 PPDMLUN pLun = pCur->Internal.s.pLun;
620 Assert(pLun->pBottom == pCur);
621 pLun->pBottom = pCur->Internal.s.pUp;
622
623 if (pCur->Internal.s.pUp)
624 {
625 /* driver parent */
626 PPDMDRVINS pParent = pCur->Internal.s.pUp;
627 pCur->Internal.s.pUp = NULL;
628 pParent->Internal.s.pDown = NULL;
629
630 if (!(fFlags & PDM_TACH_FLAGS_NO_CALLBACKS) && pParent->pReg->pfnDetach)
631 pParent->pReg->pfnDetach(pParent, fFlags);
632
633 pParent->pDownBase = NULL;
634 }
635 else
636 {
637 /* device parent */
638 Assert(pLun->pTop == pCur);
639 pLun->pTop = NULL;
640 if (!(fFlags & PDM_TACH_FLAGS_NO_CALLBACKS) && pLun->pDevIns->pReg->pfnDetach)
641 pLun->pDevIns->pReg->pfnDetach(pLun->pDevIns, pLun->iLun, fFlags);
642 }
643
644 /*
645 * Call destructor.
646 */
647 pCur->pUpBase = NULL;
648 if (pCur->pReg->pfnDestruct)
649 pCur->pReg->pfnDestruct(pCur);
650 pCur->Internal.s.pDrv->cInstances--;
651
652 /*
653 * Free all resources allocated by the driver.
654 */
655 /* Queues. */
656 int rc = PDMR3QueueDestroyDriver(pVM, pCur);
657 AssertRC(rc);
658
659 /* Timers. */
660 rc = TMR3TimerDestroyDriver(pVM, pCur);
661 AssertRC(rc);
662
663 /* SSM data units. */
664 rc = SSMR3DeregisterDriver(pVM, pCur, NULL, 0);
665 AssertRC(rc);
666
667 /* PDM threads. */
668 rc = pdmR3ThreadDestroyDriver(pVM, pCur);
669 AssertRC(rc);
670
671 /* Finally, the driver it self. */
672 bool fHyperHeap = pCur->Internal.s.fHyperHeap;
673 ASMMemFill32(pCur, RT_OFFSETOF(PDMDRVINS, achInstanceData[pCur->pReg->cbInstance]), 0xdeadd0d0);
674 if (fHyperHeap)
675 MMHyperFree(pVM, pCur);
676 else
677 MMR3HeapFree(pCur);
678
679 } while (pCur != pDrvIns);
680}
681
682
683
684
685/** @name Driver Helpers
686 * @{
687 */
688
689/** @interface_method_impl{PDMDRVHLP,pfnAttach} */
690static DECLCALLBACK(int) pdmR3DrvHlp_Attach(PPDMDRVINS pDrvIns, uint32_t fFlags, PPDMIBASE *ppBaseInterface)
691{
692 PDMDRV_ASSERT_DRVINS(pDrvIns);
693 PVM pVM = pDrvIns->Internal.s.pVMR3;
694 VM_ASSERT_EMT(pVM);
695 LogFlow(("pdmR3DrvHlp_Attach: caller='%s'/%d: fFlags=%#x\n", pDrvIns->pReg->szName, pDrvIns->iInstance, fFlags));
696 Assert(!(fFlags & ~(PDM_TACH_FLAGS_NOT_HOT_PLUG)));
697
698 /*
699 * Check that there isn't anything attached already.
700 */
701 int rc;
702 if (!pDrvIns->Internal.s.pDown)
703 {
704 Assert(pDrvIns->Internal.s.pLun->pBottom == pDrvIns);
705
706 /*
707 * Get the attached driver configuration.
708 */
709 PCFGMNODE pNode = CFGMR3GetChild(pDrvIns->Internal.s.pCfgHandle, "AttachedDriver");
710 if (pNode)
711 rc = pdmR3DrvInstantiate(pVM, pNode, &pDrvIns->IBase, pDrvIns, pDrvIns->Internal.s.pLun, ppBaseInterface);
712 else
713 rc = VERR_PDM_NO_ATTACHED_DRIVER;
714 }
715 else
716 {
717 AssertMsgFailed(("Already got a driver attached. The driver should keep track of such things!\n"));
718 rc = VERR_PDM_DRIVER_ALREADY_ATTACHED;
719 }
720
721 LogFlow(("pdmR3DrvHlp_Attach: caller='%s'/%d: return %Rrc\n",
722 pDrvIns->pReg->szName, pDrvIns->iInstance, rc));
723 return rc;
724}
725
726
727/** @interface_method_impl{PDMDRVHLP,pfnDetach} */
728static DECLCALLBACK(int) pdmR3DrvHlp_Detach(PPDMDRVINS pDrvIns, uint32_t fFlags)
729{
730 PDMDRV_ASSERT_DRVINS(pDrvIns);
731 LogFlow(("pdmR3DrvHlp_Detach: caller='%s'/%d: fFlags=%#x\n",
732 pDrvIns->pReg->szName, pDrvIns->iInstance, fFlags));
733 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
734
735 /*
736 * Anything attached?
737 */
738 int rc;
739 if (pDrvIns->Internal.s.pDown)
740 rc = pdmR3DrvDetach(pDrvIns->Internal.s.pDown, fFlags);
741 else
742 {
743 AssertMsgFailed(("Nothing attached!\n"));
744 rc = VERR_PDM_NO_DRIVER_ATTACHED;
745 }
746
747 LogFlow(("pdmR3DrvHlp_Detach: caller='%s'/%d: returns %Rrc\n",
748 pDrvIns->pReg->szName, pDrvIns->iInstance, rc));
749 return rc;
750}
751
752
753/** @interface_method_impl{PDMDRVHLP,pfnDetachSelf} */
754static DECLCALLBACK(int) pdmR3DrvHlp_DetachSelf(PPDMDRVINS pDrvIns, uint32_t fFlags)
755{
756 PDMDRV_ASSERT_DRVINS(pDrvIns);
757 LogFlow(("pdmR3DrvHlp_DetachSelf: caller='%s'/%d: fFlags=%#x\n",
758 pDrvIns->pReg->szName, pDrvIns->iInstance, fFlags));
759 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
760
761 int rc = pdmR3DrvDetach(pDrvIns, fFlags);
762
763 LogFlow(("pdmR3DrvHlp_Detach: returns %Rrc\n", rc)); /* pDrvIns is freed by now. */
764 return rc;
765}
766
767
768/** @interface_method_impl{PDMDRVHLP,pfnMountPrepare} */
769static DECLCALLBACK(int) pdmR3DrvHlp_MountPrepare(PPDMDRVINS pDrvIns, const char *pszFilename, const char *pszCoreDriver)
770{
771 PDMDRV_ASSERT_DRVINS(pDrvIns);
772 LogFlow(("pdmR3DrvHlp_MountPrepare: caller='%s'/%d: pszFilename=%p:{%s} pszCoreDriver=%p:{%s}\n",
773 pDrvIns->pReg->szName, pDrvIns->iInstance, pszFilename, pszFilename, pszCoreDriver, pszCoreDriver));
774 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
775
776 /*
777 * Do the caller have anything attached below itself?
778 */
779 if (pDrvIns->Internal.s.pDown)
780 {
781 AssertMsgFailed(("Cannot prepare a mount when something's attached to you!\n"));
782 return VERR_PDM_DRIVER_ALREADY_ATTACHED;
783 }
784
785 /*
786 * We're asked to prepare, so we'll start off by nuking the
787 * attached configuration tree.
788 */
789 PCFGMNODE pNode = CFGMR3GetChild(pDrvIns->Internal.s.pCfgHandle, "AttachedDriver");
790 if (pNode)
791 CFGMR3RemoveNode(pNode);
792
793 /*
794 * If there is no core driver, we'll have to probe for it.
795 */
796 if (!pszCoreDriver)
797 {
798 /** @todo implement image probing. */
799 AssertReleaseMsgFailed(("Not implemented!\n"));
800 return VERR_NOT_IMPLEMENTED;
801 }
802
803 /*
804 * Construct the basic attached driver configuration.
805 */
806 int rc = CFGMR3InsertNode(pDrvIns->Internal.s.pCfgHandle, "AttachedDriver", &pNode);
807 if (RT_SUCCESS(rc))
808 {
809 rc = CFGMR3InsertString(pNode, "Driver", pszCoreDriver);
810 if (RT_SUCCESS(rc))
811 {
812 PCFGMNODE pCfg;
813 rc = CFGMR3InsertNode(pNode, "Config", &pCfg);
814 if (RT_SUCCESS(rc))
815 {
816 rc = CFGMR3InsertString(pCfg, "Path", pszFilename);
817 if (RT_SUCCESS(rc))
818 {
819 LogFlow(("pdmR3DrvHlp_MountPrepare: caller='%s'/%d: returns %Rrc (Driver=%s)\n",
820 pDrvIns->pReg->szName, pDrvIns->iInstance, rc, pszCoreDriver));
821 return rc;
822 }
823 else
824 AssertMsgFailed(("Path string insert failed, rc=%Rrc\n", rc));
825 }
826 else
827 AssertMsgFailed(("Config node failed, rc=%Rrc\n", rc));
828 }
829 else
830 AssertMsgFailed(("Driver string insert failed, rc=%Rrc\n", rc));
831 CFGMR3RemoveNode(pNode);
832 }
833 else
834 AssertMsgFailed(("AttachedDriver node insert failed, rc=%Rrc\n", rc));
835
836 LogFlow(("pdmR3DrvHlp_MountPrepare: caller='%s'/%d: returns %Rrc\n",
837 pDrvIns->pReg->szName, pDrvIns->iInstance, rc));
838 return rc;
839}
840
841
842/** @interface_method_impl{PDMDRVHLP,pfnAssertEMT} */
843static DECLCALLBACK(bool) pdmR3DrvHlp_AssertEMT(PPDMDRVINS pDrvIns, const char *pszFile, unsigned iLine, const char *pszFunction)
844{
845 PDMDRV_ASSERT_DRVINS(pDrvIns);
846 if (VM_IS_EMT(pDrvIns->Internal.s.pVMR3))
847 return true;
848
849 char szMsg[100];
850 RTStrPrintf(szMsg, sizeof(szMsg), "AssertEMT '%s'/%d\n", pDrvIns->pReg->szName, pDrvIns->iInstance);
851 RTAssertMsg1Weak(szMsg, iLine, pszFile, pszFunction);
852 AssertBreakpoint();
853 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
854 return false;
855}
856
857
858/** @interface_method_impl{PDMDRVHLP,pfnAssertOther} */
859static DECLCALLBACK(bool) pdmR3DrvHlp_AssertOther(PPDMDRVINS pDrvIns, const char *pszFile, unsigned iLine, const char *pszFunction)
860{
861 PDMDRV_ASSERT_DRVINS(pDrvIns);
862 if (!VM_IS_EMT(pDrvIns->Internal.s.pVMR3))
863 return true;
864
865 char szMsg[100];
866 RTStrPrintf(szMsg, sizeof(szMsg), "AssertOther '%s'/%d\n", pDrvIns->pReg->szName, pDrvIns->iInstance);
867 RTAssertMsg1Weak(szMsg, iLine, pszFile, pszFunction);
868 AssertBreakpoint();
869 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
870 return false;
871}
872
873
874/** @interface_method_impl{PDMDRVHLP,pfnVMSetError} */
875static DECLCALLBACK(int) pdmR3DrvHlp_VMSetError(PPDMDRVINS pDrvIns, int rc, RT_SRC_POS_DECL, const char *pszFormat, ...)
876{
877 PDMDRV_ASSERT_DRVINS(pDrvIns);
878 va_list args;
879 va_start(args, pszFormat);
880 int rc2 = VMSetErrorV(pDrvIns->Internal.s.pVMR3, rc, RT_SRC_POS_ARGS, pszFormat, args); Assert(rc2 == rc); NOREF(rc2);
881 va_end(args);
882 return rc;
883}
884
885
886/** @interface_method_impl{PDMDRVHLP,pfnVMSetErrorV} */
887static DECLCALLBACK(int) pdmR3DrvHlp_VMSetErrorV(PPDMDRVINS pDrvIns, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va)
888{
889 PDMDRV_ASSERT_DRVINS(pDrvIns);
890 int rc2 = VMSetErrorV(pDrvIns->Internal.s.pVMR3, rc, RT_SRC_POS_ARGS, pszFormat, va); Assert(rc2 == rc); NOREF(rc2);
891 return rc;
892}
893
894
895/** @interface_method_impl{PDMDRVHLP,pfnVMSetRuntimeError} */
896static DECLCALLBACK(int) pdmR3DrvHlp_VMSetRuntimeError(PPDMDRVINS pDrvIns, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, ...)
897{
898 PDMDRV_ASSERT_DRVINS(pDrvIns);
899 va_list args;
900 va_start(args, pszFormat);
901 int rc = VMSetRuntimeErrorV(pDrvIns->Internal.s.pVMR3, fFlags, pszErrorId, pszFormat, args);
902 va_end(args);
903 return rc;
904}
905
906
907/** @interface_method_impl{PDMDRVHLP,pfnVMSetRuntimeErrorV} */
908static DECLCALLBACK(int) pdmR3DrvHlp_VMSetRuntimeErrorV(PPDMDRVINS pDrvIns, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list va)
909{
910 PDMDRV_ASSERT_DRVINS(pDrvIns);
911 int rc = VMSetRuntimeErrorV(pDrvIns->Internal.s.pVMR3, fFlags, pszErrorId, pszFormat, va);
912 return rc;
913}
914
915
916/** @interface_method_impl{PDMDEVHLPR3,pfnVMState} */
917static DECLCALLBACK(VMSTATE) pdmR3DrvHlp_VMState(PPDMDRVINS pDrvIns)
918{
919 PDMDRV_ASSERT_DRVINS(pDrvIns);
920
921 VMSTATE enmVMState = VMR3GetState(pDrvIns->Internal.s.pVMR3);
922
923 LogFlow(("pdmR3DrvHlp_VMState: caller='%s'/%d: returns %d (%s)\n", pDrvIns->pReg->szName, pDrvIns->iInstance,
924 enmVMState, VMR3GetStateName(enmVMState)));
925 return enmVMState;
926}
927
928
929/** @interface_method_impl{PDMDEVHLPR3,pfnVMTeleportedAndNotFullyResumedYet} */
930static DECLCALLBACK(bool) pdmR3DrvHlp_VMTeleportedAndNotFullyResumedYet(PPDMDRVINS pDrvIns)
931{
932 PDMDRV_ASSERT_DRVINS(pDrvIns);
933
934 bool fRc = VMR3TeleportedAndNotFullyResumedYet(pDrvIns->Internal.s.pVMR3);
935
936 LogFlow(("pdmR3DrvHlp_VMState: caller='%s'/%d: returns %RTbool)\n", pDrvIns->pReg->szName, pDrvIns->iInstance,
937 fRc));
938 return fRc;
939}
940
941
942/** @interface_method_impl{PDMDRVHLP,pfnPDMQueueCreate} */
943static DECLCALLBACK(int) pdmR3DrvHlp_PDMQueueCreate(PPDMDRVINS pDrvIns, uint32_t cbItem, uint32_t cItems, uint32_t cMilliesInterval,
944 PFNPDMQUEUEDRV pfnCallback, const char *pszName, PPDMQUEUE *ppQueue)
945{
946 PDMDRV_ASSERT_DRVINS(pDrvIns);
947 LogFlow(("pdmR3DrvHlp_PDMQueueCreate: caller='%s'/%d: cbItem=%d cItems=%d cMilliesInterval=%d pfnCallback=%p pszName=%p:{%s} ppQueue=%p\n",
948 pDrvIns->pReg->szName, pDrvIns->iInstance, cbItem, cItems, cMilliesInterval, pfnCallback, pszName, pszName, ppQueue, ppQueue));
949 PVM pVM = pDrvIns->Internal.s.pVMR3;
950 VM_ASSERT_EMT(pVM);
951
952 if (pDrvIns->iInstance > 0)
953 {
954 pszName = MMR3HeapAPrintf(pVM, MM_TAG_PDM_DRIVER_DESC, "%s_%u", pszName, pDrvIns->iInstance);
955 AssertLogRelReturn(pszName, VERR_NO_MEMORY);
956 }
957
958 int rc = PDMR3QueueCreateDriver(pVM, pDrvIns, cbItem, cItems, cMilliesInterval, pfnCallback, pszName, ppQueue);
959
960 LogFlow(("pdmR3DrvHlp_PDMQueueCreate: caller='%s'/%d: returns %Rrc *ppQueue=%p\n", pDrvIns->pReg->szName, pDrvIns->iInstance, rc, *ppQueue));
961 return rc;
962}
963
964
965/** @interface_method_impl{PDMDRVHLP,pfnTMGetVirtualFreq} */
966static DECLCALLBACK(uint64_t) pdmR3DrvHlp_TMGetVirtualFreq(PPDMDRVINS pDrvIns)
967{
968 PDMDRV_ASSERT_DRVINS(pDrvIns);
969
970 return TMVirtualGetFreq(pDrvIns->Internal.s.pVMR3);
971}
972
973
974/** @interface_method_impl{PDMDRVHLP,pfnTMGetVirtualTime} */
975static DECLCALLBACK(uint64_t) pdmR3DrvHlp_TMGetVirtualTime(PPDMDRVINS pDrvIns)
976{
977 PDMDRV_ASSERT_DRVINS(pDrvIns);
978
979 return TMVirtualGet(pDrvIns->Internal.s.pVMR3);
980}
981
982
983/** @interface_method_impl{PDMDRVHLP,pfnTMTimerCreate} */
984static DECLCALLBACK(int) pdmR3DrvHlp_TMTimerCreate(PPDMDRVINS pDrvIns, TMCLOCK enmClock, PFNTMTIMERDRV pfnCallback, void *pvUser, uint32_t fFlags, const char *pszDesc, PPTMTIMERR3 ppTimer)
985{
986 PDMDRV_ASSERT_DRVINS(pDrvIns);
987 LogFlow(("pdmR3DrvHlp_TMTimerCreate: caller='%s'/%d: enmClock=%d pfnCallback=%p pvUser=%p fFlags=%#x pszDesc=%p:{%s} ppTimer=%p\n",
988 pDrvIns->pReg->szName, pDrvIns->iInstance, enmClock, pfnCallback, pvUser, fFlags, pszDesc, pszDesc, ppTimer));
989
990 int rc = TMR3TimerCreateDriver(pDrvIns->Internal.s.pVMR3, pDrvIns, enmClock, pfnCallback, pvUser, fFlags, pszDesc, ppTimer);
991
992 LogFlow(("pdmR3DrvHlp_TMTimerCreate: caller='%s'/%d: returns %Rrc *ppTimer=%p\n", pDrvIns->pReg->szName, pDrvIns->iInstance, rc, *ppTimer));
993 return rc;
994}
995
996
997
998/** @interface_method_impl{PDMDRVHLP,pfnSSMRegister} */
999static DECLCALLBACK(int) pdmR3DrvHlp_SSMRegister(PPDMDRVINS pDrvIns, uint32_t uVersion, size_t cbGuess,
1000 PFNSSMDRVLIVEPREP pfnLivePrep, PFNSSMDRVLIVEEXEC pfnLiveExec, PFNSSMDRVLIVEVOTE pfnLiveVote,
1001 PFNSSMDRVSAVEPREP pfnSavePrep, PFNSSMDRVSAVEEXEC pfnSaveExec, PFNSSMDRVSAVEDONE pfnSaveDone,
1002 PFNSSMDRVLOADPREP pfnLoadPrep, PFNSSMDRVLOADEXEC pfnLoadExec, PFNSSMDRVLOADDONE pfnLoadDone)
1003{
1004 PDMDRV_ASSERT_DRVINS(pDrvIns);
1005 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
1006 LogFlow(("pdmR3DrvHlp_SSMRegister: caller='%s'/%d: uVersion=#x cbGuess=%#x \n"
1007 " pfnLivePrep=%p pfnLiveExec=%p pfnLiveVote=%p pfnSavePrep=%p pfnSaveExec=%p pfnSaveDone=%p pszLoadPrep=%p pfnLoadExec=%p pfnLoaddone=%p\n",
1008 pDrvIns->pReg->szName, pDrvIns->iInstance, uVersion, cbGuess,
1009 pfnLivePrep, pfnLiveExec, pfnLiveVote,
1010 pfnSavePrep, pfnSaveExec, pfnSaveDone, pfnLoadPrep, pfnLoadExec, pfnLoadDone));
1011
1012 int rc = SSMR3RegisterDriver(pDrvIns->Internal.s.pVMR3, pDrvIns, pDrvIns->pReg->szName, pDrvIns->iInstance,
1013 uVersion, cbGuess,
1014 pfnLivePrep, pfnLiveExec, pfnLiveVote,
1015 pfnSavePrep, pfnSaveExec, pfnSaveDone,
1016 pfnLoadPrep, pfnLoadExec, pfnLoadDone);
1017
1018 LogFlow(("pdmR3DrvHlp_SSMRegister: caller='%s'/%d: returns %Rrc\n", pDrvIns->pReg->szName, pDrvIns->iInstance, rc));
1019 return rc;
1020}
1021
1022
1023/** @interface_method_impl{PDMDRVHLP,pfnSSMDeregister} */
1024static DECLCALLBACK(int) pdmR3DrvHlp_SSMDeregister(PPDMDRVINS pDrvIns, const char *pszName, uint32_t u32Instance)
1025{
1026 PDMDRV_ASSERT_DRVINS(pDrvIns);
1027 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
1028 LogFlow(("pdmR3DrvHlp_SSMDeregister: caller='%s'/%d: pszName=%p:{%s} u32Instance=%#x\n",
1029 pDrvIns->pReg->szName, pDrvIns->iInstance, pszName, pszName, u32Instance));
1030
1031 int rc = SSMR3DeregisterDriver(pDrvIns->Internal.s.pVMR3, pDrvIns, pszName, u32Instance);
1032
1033 LogFlow(("pdmR3DrvHlp_SSMDeregister: caller='%s'/%d: returns %Rrc\n", pDrvIns->pReg->szName, pDrvIns->iInstance, rc));
1034 return rc;
1035}
1036
1037
1038/** @interface_method_impl{PDMDRVHLP,pfnSTAMRegister} */
1039static DECLCALLBACK(void) pdmR3DrvHlp_STAMRegister(PPDMDRVINS pDrvIns, void *pvSample, STAMTYPE enmType, const char *pszName, STAMUNIT enmUnit, const char *pszDesc)
1040{
1041 PDMDRV_ASSERT_DRVINS(pDrvIns);
1042 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
1043
1044 STAM_REG(pDrvIns->Internal.s.pVMR3, pvSample, enmType, pszName, enmUnit, pszDesc);
1045 /** @todo track the samples so they can be dumped & deregistered when the driver instance is destroyed.
1046 * For now we just have to be careful not to use this call for drivers which can be unloaded. */
1047}
1048
1049
1050/** @interface_method_impl{PDMDRVHLP,pfnSTAMRegisterF} */
1051static DECLCALLBACK(void) pdmR3DrvHlp_STAMRegisterF(PPDMDRVINS pDrvIns, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility,
1052 STAMUNIT enmUnit, const char *pszDesc, const char *pszName, ...)
1053{
1054 PDMDRV_ASSERT_DRVINS(pDrvIns);
1055 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
1056
1057 va_list args;
1058 va_start(args, pszName);
1059 int rc = STAMR3RegisterV(pDrvIns->Internal.s.pVMR3, pvSample, enmType, enmVisibility, enmUnit, pszDesc, pszName, args);
1060 va_end(args);
1061 AssertRC(rc);
1062}
1063
1064
1065/** @interface_method_impl{PDMDRVHLP,pfnSTAMRegisterV} */
1066static DECLCALLBACK(void) pdmR3DrvHlp_STAMRegisterV(PPDMDRVINS pDrvIns, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility,
1067 STAMUNIT enmUnit, const char *pszDesc, const char *pszName, va_list args)
1068{
1069 PDMDRV_ASSERT_DRVINS(pDrvIns);
1070 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
1071
1072 int rc = STAMR3RegisterV(pDrvIns->Internal.s.pVMR3, pvSample, enmType, enmVisibility, enmUnit, pszDesc, pszName, args);
1073 AssertRC(rc);
1074}
1075
1076
1077/** @interface_method_impl{PDMDRVHLP,pfnSTAMDeregister} */
1078static DECLCALLBACK(int) pdmR3DrvHlp_STAMDeregister(PPDMDRVINS pDrvIns, void *pvSample)
1079{
1080 PDMDRV_ASSERT_DRVINS(pDrvIns);
1081 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
1082
1083 int rc = STAMR3DeregisterU(pDrvIns->Internal.s.pVMR3->pUVM, pvSample);
1084 AssertRC(rc);
1085 return rc;
1086}
1087
1088
1089/** @interface_method_impl{PDMDRVHLP,pfnSUPCallVMMR0Ex} */
1090static DECLCALLBACK(int) pdmR3DrvHlp_SUPCallVMMR0Ex(PPDMDRVINS pDrvIns, unsigned uOperation, void *pvArg, unsigned cbArg)
1091{
1092 PDMDRV_ASSERT_DRVINS(pDrvIns);
1093 LogFlow(("pdmR3DrvHlp_SSMCallVMMR0Ex: caller='%s'/%d: uOperation=%u pvArg=%p cbArg=%d\n",
1094 pDrvIns->pReg->szName, pDrvIns->iInstance, uOperation, pvArg, cbArg));
1095 int rc;
1096 if ( uOperation >= VMMR0_DO_SRV_START
1097 && uOperation < VMMR0_DO_SRV_END)
1098 rc = SUPR3CallVMMR0Ex(pDrvIns->Internal.s.pVMR3->pVMR0, NIL_VMCPUID, uOperation, 0, (PSUPVMMR0REQHDR)pvArg);
1099 else
1100 {
1101 AssertMsgFailed(("Invalid uOperation=%u\n", uOperation));
1102 rc = VERR_INVALID_PARAMETER;
1103 }
1104
1105 LogFlow(("pdmR3DrvHlp_SUPCallVMMR0Ex: caller='%s'/%d: returns %Rrc\n", pDrvIns->pReg->szName, pDrvIns->iInstance, rc));
1106 return rc;
1107}
1108
1109
1110/** @interface_method_impl{PDMDRVHLP,pfnUSBRegisterHub} */
1111static DECLCALLBACK(int) pdmR3DrvHlp_USBRegisterHub(PPDMDRVINS pDrvIns, uint32_t fVersions, uint32_t cPorts, PCPDMUSBHUBREG pUsbHubReg, PPCPDMUSBHUBHLP ppUsbHubHlp)
1112{
1113 PDMDRV_ASSERT_DRVINS(pDrvIns);
1114 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
1115 LogFlow(("pdmR3DrvHlp_USBRegisterHub: caller='%s'/%d: fVersions=%#x cPorts=%#x pUsbHubReg=%p ppUsbHubHlp=%p\n",
1116 pDrvIns->pReg->szName, pDrvIns->iInstance, fVersions, cPorts, pUsbHubReg, ppUsbHubHlp));
1117
1118#ifdef VBOX_WITH_USB
1119 int rc = pdmR3UsbRegisterHub(pDrvIns->Internal.s.pVMR3, pDrvIns, fVersions, cPorts, pUsbHubReg, ppUsbHubHlp);
1120#else
1121 int rc = VERR_NOT_SUPPORTED;
1122#endif
1123
1124 LogFlow(("pdmR3DrvHlp_USBRegisterHub: caller='%s'/%d: returns %Rrc\n", pDrvIns->pReg->szName, pDrvIns->iInstance, rc));
1125 return rc;
1126}
1127
1128
1129/** @interface_method_impl{PDMDRVHLP,pfnSetAsyncNotification} */
1130static DECLCALLBACK(int) pdmR3DrvHlp_SetAsyncNotification(PPDMDRVINS pDrvIns, PFNPDMDRVASYNCNOTIFY pfnAsyncNotify)
1131{
1132 PDMDRV_ASSERT_DRVINS(pDrvIns);
1133 VM_ASSERT_EMT0(pDrvIns->Internal.s.pVMR3);
1134 LogFlow(("pdmR3DrvHlp_SetAsyncNotification: caller='%s'/%d: pfnAsyncNotify=%p\n", pDrvIns->pReg->szName, pDrvIns->iInstance, pfnAsyncNotify));
1135
1136 int rc = VINF_SUCCESS;
1137 AssertStmt(pfnAsyncNotify, rc = VERR_INVALID_PARAMETER);
1138 AssertStmt(!pDrvIns->Internal.s.pfnAsyncNotify, rc = VERR_WRONG_ORDER);
1139 AssertStmt(pDrvIns->Internal.s.fVMSuspended || pDrvIns->Internal.s.fVMReset, rc = VERR_WRONG_ORDER);
1140 VMSTATE enmVMState = VMR3GetState(pDrvIns->Internal.s.pVMR3);
1141 AssertStmt( enmVMState == VMSTATE_SUSPENDING
1142 || enmVMState == VMSTATE_SUSPENDING_EXT_LS
1143 || enmVMState == VMSTATE_SUSPENDING_LS
1144 || enmVMState == VMSTATE_RESETTING
1145 || enmVMState == VMSTATE_RESETTING_LS
1146 || enmVMState == VMSTATE_POWERING_OFF
1147 || enmVMState == VMSTATE_POWERING_OFF_LS,
1148 rc = VERR_INVALID_STATE);
1149
1150 if (RT_SUCCESS(rc))
1151 pDrvIns->Internal.s.pfnAsyncNotify = pfnAsyncNotify;
1152
1153 LogFlow(("pdmR3DrvHlp_SetAsyncNotification: caller='%s'/%d: returns %Rrc\n", pDrvIns->pReg->szName, pDrvIns->iInstance, rc));
1154 return rc;
1155}
1156
1157
1158/** @interface_method_impl{PDMDRVHLP,pfnAsyncNotificationCompleted} */
1159static DECLCALLBACK(void) pdmR3DrvHlp_AsyncNotificationCompleted(PPDMDRVINS pDrvIns)
1160{
1161 PDMDRV_ASSERT_DRVINS(pDrvIns);
1162 PVM pVM = pDrvIns->Internal.s.pVMR3;
1163
1164 VMSTATE enmVMState = VMR3GetState(pVM);
1165 if ( enmVMState == VMSTATE_SUSPENDING
1166 || enmVMState == VMSTATE_SUSPENDING_EXT_LS
1167 || enmVMState == VMSTATE_SUSPENDING_LS
1168 || enmVMState == VMSTATE_RESETTING
1169 || enmVMState == VMSTATE_RESETTING_LS
1170 || enmVMState == VMSTATE_POWERING_OFF
1171 || enmVMState == VMSTATE_POWERING_OFF_LS)
1172 {
1173 LogFlow(("pdmR3DrvHlp_AsyncNotificationCompleted: caller='%s'/%d:\n", pDrvIns->pReg->szName, pDrvIns->iInstance));
1174 VMR3AsyncPdmNotificationWakeupU(pVM->pUVM);
1175 }
1176 else
1177 LogFlow(("pdmR3DrvHlp_AsyncNotificationCompleted: caller='%s'/%d: enmVMState=%d\n", pDrvIns->pReg->szName, pDrvIns->iInstance, enmVMState));
1178}
1179
1180
1181/** @interface_method_impl{PDMDRVHLP,pfnPDMThreadCreate} */
1182static DECLCALLBACK(int) pdmR3DrvHlp_PDMThreadCreate(PPDMDRVINS pDrvIns, PPPDMTHREAD ppThread, void *pvUser, PFNPDMTHREADDRV pfnThread,
1183 PFNPDMTHREADWAKEUPDRV pfnWakeup, size_t cbStack, RTTHREADTYPE enmType, const char *pszName)
1184{
1185 PDMDRV_ASSERT_DRVINS(pDrvIns);
1186 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
1187 LogFlow(("pdmR3DrvHlp_PDMThreadCreate: caller='%s'/%d: ppThread=%p pvUser=%p pfnThread=%p pfnWakeup=%p cbStack=%#zx enmType=%d pszName=%p:{%s}\n",
1188 pDrvIns->pReg->szName, pDrvIns->iInstance, ppThread, pvUser, pfnThread, pfnWakeup, cbStack, enmType, pszName, pszName));
1189
1190 int rc = pdmR3ThreadCreateDriver(pDrvIns->Internal.s.pVMR3, pDrvIns, ppThread, pvUser, pfnThread, pfnWakeup, cbStack, enmType, pszName);
1191
1192 LogFlow(("pdmR3DrvHlp_PDMThreadCreate: caller='%s'/%d: returns %Rrc *ppThread=%RTthrd\n", pDrvIns->pReg->szName, pDrvIns->iInstance,
1193 rc, *ppThread));
1194 return rc;
1195}
1196
1197
1198/** @interface_method_impl{PDMDRVHLP,pfnPDMAsyncCompletionTemplateCreate} */
1199static DECLCALLBACK(int) pdmR3DrvHlp_PDMAsyncCompletionTemplateCreate(PPDMDRVINS pDrvIns, PPPDMASYNCCOMPLETIONTEMPLATE ppTemplate,
1200 PFNPDMASYNCCOMPLETEDRV pfnCompleted, void *pvTemplateUser,
1201 const char *pszDesc)
1202{
1203 PDMDRV_ASSERT_DRVINS(pDrvIns);
1204 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
1205 LogFlow(("pdmR3DrvHlp_PDMAsyncCompletionTemplateCreate: caller='%s'/%d: ppTemplate=%p pfnCompleted=%p pszDesc=%p:{%s}\n",
1206 pDrvIns->pReg->szName, pDrvIns->iInstance, ppTemplate, pfnCompleted, pszDesc, pszDesc));
1207
1208 int rc = PDMR3AsyncCompletionTemplateCreateDriver(pDrvIns->Internal.s.pVMR3, pDrvIns, ppTemplate, pfnCompleted, pvTemplateUser, pszDesc);
1209
1210 LogFlow(("pdmR3DrvHlp_PDMAsyncCompletionTemplateCreate: caller='%s'/%d: returns %Rrc *ppThread=%p\n", pDrvIns->pReg->szName,
1211 pDrvIns->iInstance, rc, *ppTemplate));
1212 return rc;
1213}
1214
1215
1216/** @interface_method_impl{PDMDRVHLP,pfnPDMLdrGetRCInterfaceSymbols} */
1217static DECLCALLBACK(int) pdmR3DrvHlp_PDMLdrGetRCInterfaceSymbols(PPDMDRVINS pDrvIns, void *pvInterface, size_t cbInterface,
1218 const char *pszSymPrefix, const char *pszSymList)
1219{
1220 PDMDRV_ASSERT_DRVINS(pDrvIns);
1221 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
1222 LogFlow(("pdmR3DrvHlp_PDMLdrGetRCInterfaceSymbols: caller='%s'/%d: pvInterface=%p cbInterface=%zu pszSymPrefix=%p:{%s} pszSymList=%p:{%s}\n",
1223 pDrvIns->pReg->szName, pDrvIns->iInstance, pvInterface, cbInterface, pszSymPrefix, pszSymPrefix, pszSymList, pszSymList));
1224
1225 int rc;
1226 if ( strncmp(pszSymPrefix, "drv", 3) == 0
1227 && RTStrIStr(pszSymPrefix + 3, pDrvIns->pReg->szName) != NULL)
1228 {
1229 if (pDrvIns->pReg->fFlags & PDM_DRVREG_FLAGS_RC)
1230 rc = PDMR3LdrGetInterfaceSymbols(pDrvIns->Internal.s.pVMR3, pvInterface, cbInterface,
1231 pDrvIns->pReg->szName, pszSymPrefix, pszSymList,
1232 false /*fRing0OrRC*/);
1233 else
1234 {
1235 AssertMsgFailed(("Not a raw-mode enabled driver\n"));
1236 rc = VERR_PERMISSION_DENIED;
1237 }
1238 }
1239 else
1240 {
1241 AssertMsgFailed(("Invalid prefix '%s' for '%s'; must start with 'drv' and contain the driver name!\n",
1242 pszSymPrefix, pDrvIns->pReg->szName));
1243 rc = VERR_INVALID_NAME;
1244 }
1245
1246 LogFlow(("pdmR3DrvHlp_PDMLdrGetRCInterfaceSymbols: caller='%s'/%d: returns %Rrc\n", pDrvIns->pReg->szName,
1247 pDrvIns->iInstance, rc));
1248 return rc;
1249}
1250
1251
1252/** @interface_method_impl{PDMDRVHLP,pfnPDMLdrGetR0InterfaceSymbols} */
1253static DECLCALLBACK(int) pdmR3DrvHlp_PDMLdrGetR0InterfaceSymbols(PPDMDRVINS pDrvIns, void *pvInterface, size_t cbInterface,
1254 const char *pszSymPrefix, const char *pszSymList)
1255{
1256 PDMDRV_ASSERT_DRVINS(pDrvIns);
1257 VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
1258 LogFlow(("pdmR3DrvHlp_PDMLdrGetR0InterfaceSymbols: caller='%s'/%d: pvInterface=%p cbInterface=%zu pszSymPrefix=%p:{%s} pszSymList=%p:{%s}\n",
1259 pDrvIns->pReg->szName, pDrvIns->iInstance, pvInterface, cbInterface, pszSymPrefix, pszSymPrefix, pszSymList, pszSymList));
1260
1261 int rc;
1262 if ( strncmp(pszSymPrefix, "drv", 3) == 0
1263 && RTStrIStr(pszSymPrefix + 3, pDrvIns->pReg->szName) != NULL)
1264 {
1265 if (pDrvIns->pReg->fFlags & PDM_DRVREG_FLAGS_R0)
1266 rc = PDMR3LdrGetInterfaceSymbols(pDrvIns->Internal.s.pVMR3, pvInterface, cbInterface,
1267 pDrvIns->pReg->szName, pszSymPrefix, pszSymList,
1268 true /*fRing0OrRC*/);
1269 else
1270 {
1271 AssertMsgFailed(("Not a ring-0 enabled driver\n"));
1272 rc = VERR_PERMISSION_DENIED;
1273 }
1274 }
1275 else
1276 {
1277 AssertMsgFailed(("Invalid prefix '%s' for '%s'; must start with 'drv' and contain the driver name!\n",
1278 pszSymPrefix, pDrvIns->pReg->szName));
1279 rc = VERR_INVALID_NAME;
1280 }
1281
1282 LogFlow(("pdmR3DrvHlp_PDMLdrGetR0InterfaceSymbols: caller='%s'/%d: returns %Rrc\n", pDrvIns->pReg->szName,
1283 pDrvIns->iInstance, rc));
1284 return rc;
1285}
1286
1287
1288/**
1289 * The driver helper structure.
1290 */
1291const PDMDRVHLPR3 g_pdmR3DrvHlp =
1292{
1293 PDM_DRVHLPR3_VERSION,
1294 pdmR3DrvHlp_Attach,
1295 pdmR3DrvHlp_Detach,
1296 pdmR3DrvHlp_DetachSelf,
1297 pdmR3DrvHlp_MountPrepare,
1298 pdmR3DrvHlp_AssertEMT,
1299 pdmR3DrvHlp_AssertOther,
1300 pdmR3DrvHlp_VMSetError,
1301 pdmR3DrvHlp_VMSetErrorV,
1302 pdmR3DrvHlp_VMSetRuntimeError,
1303 pdmR3DrvHlp_VMSetRuntimeErrorV,
1304 pdmR3DrvHlp_VMState,
1305 pdmR3DrvHlp_VMTeleportedAndNotFullyResumedYet,
1306 pdmR3DrvHlp_PDMQueueCreate,
1307 pdmR3DrvHlp_TMGetVirtualFreq,
1308 pdmR3DrvHlp_TMGetVirtualTime,
1309 pdmR3DrvHlp_TMTimerCreate,
1310 pdmR3DrvHlp_SSMRegister,
1311 pdmR3DrvHlp_SSMDeregister,
1312 pdmR3DrvHlp_STAMRegister,
1313 pdmR3DrvHlp_STAMRegisterF,
1314 pdmR3DrvHlp_STAMRegisterV,
1315 pdmR3DrvHlp_STAMDeregister,
1316 pdmR3DrvHlp_SUPCallVMMR0Ex,
1317 pdmR3DrvHlp_USBRegisterHub,
1318 pdmR3DrvHlp_SetAsyncNotification,
1319 pdmR3DrvHlp_AsyncNotificationCompleted,
1320 pdmR3DrvHlp_PDMThreadCreate,
1321 pdmR3DrvHlp_PDMAsyncCompletionTemplateCreate,
1322 pdmR3DrvHlp_PDMLdrGetRCInterfaceSymbols,
1323 pdmR3DrvHlp_PDMLdrGetR0InterfaceSymbols,
1324 PDM_DRVHLPR3_VERSION /* u32TheEnd */
1325};
1326
1327/** @} */
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