VirtualBox

source: vbox/trunk/src/VBox/VMM/PDMDevice.cpp@ 26169

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

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

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 35.8 KB
Line 
1/* $Id: PDMDevice.cpp 26169 2010-02-02 20:19:15Z vboxsync $ */
2/** @file
3 * PDM - Pluggable Device and Driver Manager, Device parts.
4 */
5
6/*
7 * Copyright (C) 2006-2010 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_DEVICE
27#include "PDMInternal.h"
28#include <VBox/pdm.h>
29#include <VBox/mm.h>
30#include <VBox/pgm.h>
31#include <VBox/iom.h>
32#include <VBox/cfgm.h>
33#include <VBox/rem.h>
34#include <VBox/dbgf.h>
35#include <VBox/vm.h>
36#include <VBox/vmm.h>
37
38#include <VBox/version.h>
39#include <VBox/log.h>
40#include <VBox/err.h>
41#include <iprt/alloc.h>
42#include <iprt/alloca.h>
43#include <iprt/asm.h>
44#include <iprt/assert.h>
45#include <iprt/path.h>
46#include <iprt/semaphore.h>
47#include <iprt/string.h>
48#include <iprt/thread.h>
49
50
51/*******************************************************************************
52* Structures and Typedefs *
53*******************************************************************************/
54/**
55 * Internal callback structure pointer.
56 * The main purpose is to define the extra data we associate
57 * with PDMDEVREGCB so we can find the VM instance and so on.
58 */
59typedef struct PDMDEVREGCBINT
60{
61 /** The callback structure. */
62 PDMDEVREGCB Core;
63 /** A bit of padding. */
64 uint32_t u32[4];
65 /** VM Handle. */
66 PVM pVM;
67} PDMDEVREGCBINT;
68/** Pointer to a PDMDEVREGCBINT structure. */
69typedef PDMDEVREGCBINT *PPDMDEVREGCBINT;
70/** Pointer to a const PDMDEVREGCBINT structure. */
71typedef const PDMDEVREGCBINT *PCPDMDEVREGCBINT;
72
73
74/*******************************************************************************
75* Internal Functions *
76*******************************************************************************/
77static DECLCALLBACK(int) pdmR3DevReg_Register(PPDMDEVREGCB pCallbacks, PCPDMDEVREG pReg);
78static int pdmR3DevLoadModules(PVM pVM);
79static int pdmR3DevLoad(PVM pVM, PPDMDEVREGCBINT pRegCB, const char *pszFilename, const char *pszName);
80
81
82
83
84/**
85 * This function will initialize the devices for this VM instance.
86 *
87 *
88 * First of all this mean loading the builtin device and letting them
89 * register themselves. Beyond that any additional device modules are
90 * loaded and called for registration.
91 *
92 * Then the device configuration is enumerated, the instantiation order
93 * is determined, and finally they are instantiated.
94 *
95 * After all devices have been successfully instantiated the primary
96 * PCI Bus device is called to emulate the PCI BIOS, i.e. making the
97 * resource assignments. If there is no PCI device, this step is of course
98 * skipped.
99 *
100 * Finally the init completion routines of the instantiated devices
101 * are called.
102 *
103 * @returns VBox status code.
104 * @param pVM VM Handle.
105 */
106int pdmR3DevInit(PVM pVM)
107{
108 LogFlow(("pdmR3DevInit:\n"));
109
110 AssertRelease(!(RT_OFFSETOF(PDMDEVINS, achInstanceData) & 15));
111 AssertRelease(sizeof(pVM->pdm.s.pDevInstances->Internal.s) <= sizeof(pVM->pdm.s.pDevInstances->Internal.padding));
112
113 /*
114 * Load device modules.
115 */
116 int rc = pdmR3DevLoadModules(pVM);
117 if (RT_FAILURE(rc))
118 return rc;
119
120#ifdef VBOX_WITH_USB
121 /* ditto for USB Devices. */
122 rc = pdmR3UsbLoadModules(pVM);
123 if (RT_FAILURE(rc))
124 return rc;
125#endif
126
127 /*
128 * Get the RC & R0 devhlps and create the devhlp R3 task queue.
129 */
130 PCPDMDEVHLPRC pHlpRC;
131 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_pdmRCDevHlp", &pHlpRC);
132 AssertReleaseRCReturn(rc, rc);
133
134 PCPDMDEVHLPR0 pHlpR0;
135 rc = PDMR3LdrGetSymbolR0(pVM, NULL, "g_pdmR0DevHlp", &pHlpR0);
136 AssertReleaseRCReturn(rc, rc);
137
138 rc = PDMR3QueueCreateInternal(pVM, sizeof(PDMDEVHLPTASK), 8, 0, pdmR3DevHlpQueueConsumer, true, "DevHlp",
139 &pVM->pdm.s.pDevHlpQueueR3);
140 AssertRCReturn(rc, rc);
141 pVM->pdm.s.pDevHlpQueueR0 = PDMQueueR0Ptr(pVM->pdm.s.pDevHlpQueueR3);
142 pVM->pdm.s.pDevHlpQueueRC = PDMQueueRCPtr(pVM->pdm.s.pDevHlpQueueR3);
143
144
145 /*
146 *
147 * Enumerate the device instance configurations
148 * and come up with a instantiation order.
149 *
150 */
151 /* Switch to /Devices, which contains the device instantiations. */
152 PCFGMNODE pDevicesNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "Devices");
153
154 /*
155 * Count the device instances.
156 */
157 PCFGMNODE pCur;
158 PCFGMNODE pInstanceNode;
159 unsigned cDevs = 0;
160 for (pCur = CFGMR3GetFirstChild(pDevicesNode); pCur; pCur = CFGMR3GetNextChild(pCur))
161 for (pInstanceNode = CFGMR3GetFirstChild(pCur); pInstanceNode; pInstanceNode = CFGMR3GetNextChild(pInstanceNode))
162 cDevs++;
163 if (!cDevs)
164 {
165 Log(("PDM: No devices were configured!\n"));
166 return VINF_SUCCESS;
167 }
168 Log2(("PDM: cDevs=%d!\n", cDevs));
169
170 /*
171 * Collect info on each device instance.
172 */
173 struct DEVORDER
174 {
175 /** Configuration node. */
176 PCFGMNODE pNode;
177 /** Pointer to device. */
178 PPDMDEV pDev;
179 /** Init order. */
180 uint32_t u32Order;
181 /** VBox instance number. */
182 uint32_t iInstance;
183 } *paDevs = (struct DEVORDER *)alloca(sizeof(paDevs[0]) * (cDevs + 1)); /* (One extra for swapping) */
184 Assert(paDevs);
185 unsigned i = 0;
186 for (pCur = CFGMR3GetFirstChild(pDevicesNode); pCur; pCur = CFGMR3GetNextChild(pCur))
187 {
188 /* Get the device name. */
189 char szName[sizeof(paDevs[0].pDev->pReg->szName)];
190 rc = CFGMR3GetName(pCur, szName, sizeof(szName));
191 AssertMsgRCReturn(rc, ("Configuration error: device name is too long (or something)! rc=%Rrc\n", rc), rc);
192
193 /* Find the device. */
194 PPDMDEV pDev = pdmR3DevLookup(pVM, szName);
195 AssertMsgReturn(pDev, ("Configuration error: device '%s' not found!\n", szName), VERR_PDM_DEVICE_NOT_FOUND);
196
197 /* Configured priority or use default based on device class? */
198 uint32_t u32Order;
199 rc = CFGMR3QueryU32(pCur, "Priority", &u32Order);
200 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
201 {
202 uint32_t u32 = pDev->pReg->fClass;
203 for (u32Order = 1; !(u32 & u32Order); u32Order <<= 1)
204 /* nop */;
205 }
206 else
207 AssertMsgRCReturn(rc, ("Configuration error: reading \"Priority\" for the '%s' device failed rc=%Rrc!\n", szName, rc), rc);
208
209 /* Enumerate the device instances. */
210 uint32_t const iStart = i;
211 for (pInstanceNode = CFGMR3GetFirstChild(pCur); pInstanceNode; pInstanceNode = CFGMR3GetNextChild(pInstanceNode))
212 {
213 paDevs[i].pNode = pInstanceNode;
214 paDevs[i].pDev = pDev;
215 paDevs[i].u32Order = u32Order;
216
217 /* Get the instance number. */
218 char szInstance[32];
219 rc = CFGMR3GetName(pInstanceNode, szInstance, sizeof(szInstance));
220 AssertMsgRCReturn(rc, ("Configuration error: instance name is too long (or something)! rc=%Rrc\n", rc), rc);
221 char *pszNext = NULL;
222 rc = RTStrToUInt32Ex(szInstance, &pszNext, 0, &paDevs[i].iInstance);
223 AssertMsgRCReturn(rc, ("Configuration error: RTStrToInt32Ex failed on the instance name '%s'! rc=%Rrc\n", szInstance, rc), rc);
224 AssertMsgReturn(!*pszNext, ("Configuration error: the instance name '%s' isn't all digits. (%s)\n", szInstance, pszNext), VERR_INVALID_PARAMETER);
225
226 /* next instance */
227 i++;
228 }
229
230 /* check the number of instances */
231 if (i - iStart > pDev->pReg->cMaxInstances)
232 AssertLogRelMsgFailedReturn(("Configuration error: Too many instances of %s was configured: %u, max %u\n",
233 szName, i - iStart, pDev->pReg->cMaxInstances),
234 VERR_PDM_TOO_MANY_DEVICE_INSTANCES);
235 } /* devices */
236 Assert(i == cDevs);
237
238 /*
239 * Sort the device array ascending on u32Order. (bubble)
240 */
241 unsigned c = cDevs - 1;
242 while (c)
243 {
244 unsigned j = 0;
245 for (i = 0; i < c; i++)
246 if (paDevs[i].u32Order > paDevs[i + 1].u32Order)
247 {
248 paDevs[cDevs] = paDevs[i + 1];
249 paDevs[i + 1] = paDevs[i];
250 paDevs[i] = paDevs[cDevs];
251 j = i;
252 }
253 c = j;
254 }
255
256
257 /*
258 *
259 * Instantiate the devices.
260 *
261 */
262 for (i = 0; i < cDevs; i++)
263 {
264 /*
265 * Gather a bit of config.
266 */
267 /* trusted */
268 bool fTrusted;
269 rc = CFGMR3QueryBool(paDevs[i].pNode, "Trusted", &fTrusted);
270 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
271 fTrusted = false;
272 else if (RT_FAILURE(rc))
273 {
274 AssertMsgFailed(("configuration error: failed to query boolean \"Trusted\", rc=%Rrc\n", rc));
275 return rc;
276 }
277 /* config node */
278 PCFGMNODE pConfigNode = CFGMR3GetChild(paDevs[i].pNode, "Config");
279 if (!pConfigNode)
280 {
281 rc = CFGMR3InsertNode(paDevs[i].pNode, "Config", &pConfigNode);
282 if (RT_FAILURE(rc))
283 {
284 AssertMsgFailed(("Failed to create Config node! rc=%Rrc\n", rc));
285 return rc;
286 }
287 }
288 CFGMR3SetRestrictedRoot(pConfigNode);
289
290 /*
291 * Allocate the device instance.
292 */
293 AssertReturn(paDevs[i].pDev->cInstances < paDevs[i].pDev->pReg->cMaxInstances, VERR_PDM_TOO_MANY_DEVICE_INSTANCES);
294 size_t cb = RT_OFFSETOF(PDMDEVINS, achInstanceData[paDevs[i].pDev->pReg->cbInstance]);
295 cb = RT_ALIGN_Z(cb, 16);
296 PPDMDEVINS pDevIns;
297 if (paDevs[i].pDev->pReg->fFlags & (PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0))
298 rc = MMR3HyperAllocOnceNoRel(pVM, cb, 0, MM_TAG_PDM_DEVICE, (void **)&pDevIns);
299 else
300 rc = MMR3HeapAllocZEx(pVM, MM_TAG_PDM_DEVICE, cb, (void **)&pDevIns);
301 if (RT_FAILURE(rc))
302 {
303 AssertMsgFailed(("Failed to allocate %d bytes of instance data for device '%s'. rc=%Rrc\n",
304 cb, paDevs[i].pDev->pReg->szName, rc));
305 return rc;
306 }
307
308 /*
309 * Initialize it.
310 */
311 pDevIns->u32Version = PDM_DEVINS_VERSION;
312 //pDevIns->Internal.s.pNextR3 = NULL;
313 //pDevIns->Internal.s.pPerDeviceNextR3 = NULL;
314 pDevIns->Internal.s.pDevR3 = paDevs[i].pDev;
315 pDevIns->Internal.s.pVMR3 = pVM;
316 pDevIns->Internal.s.pVMR0 = pVM->pVMR0;
317 pDevIns->Internal.s.pVMRC = pVM->pVMRC;
318 //pDevIns->Internal.s.pLunsR3 = NULL;
319 pDevIns->Internal.s.pCfgHandle = paDevs[i].pNode;
320 //pDevIns->Internal.s.pPciDeviceR3 = NULL;
321 //pDevIns->Internal.s.pPciBusR3 = NULL;
322 //pDevIns->Internal.s.pPciDeviceR0 = 0;
323 //pDevIns->Internal.s.pPciBusR0 = 0;
324 //pDevIns->Internal.s.pPciDeviceRC = 0;
325 //pDevIns->Internal.s.pPciBusRC = 0;
326 pDevIns->Internal.s.fIntFlags = PDMDEVINSINT_FLAGS_SUSPENDED;
327 pDevIns->pHlpR3 = fTrusted ? &g_pdmR3DevHlpTrusted : &g_pdmR3DevHlpUnTrusted;
328 pDevIns->pHlpRC = pHlpRC;
329 pDevIns->pHlpR0 = pHlpR0;
330 pDevIns->pReg = paDevs[i].pDev->pReg;
331 pDevIns->pCfgHandle = pConfigNode;
332 pDevIns->iInstance = paDevs[i].iInstance;
333 pDevIns->pvInstanceDataR3 = &pDevIns->achInstanceData[0];
334 pDevIns->pvInstanceDataRC = pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_RC
335 ? MMHyperR3ToRC(pVM, pDevIns->pvInstanceDataR3) : NIL_RTRCPTR;
336 pDevIns->pvInstanceDataR0 = pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_R0
337 ? MMHyperR3ToR0(pVM, pDevIns->pvInstanceDataR3) : NIL_RTR0PTR;
338
339 /*
340 * Link it into all the lists.
341 */
342 /* The global instance FIFO. */
343 PPDMDEVINS pPrev1 = pVM->pdm.s.pDevInstances;
344 if (!pPrev1)
345 pVM->pdm.s.pDevInstances = pDevIns;
346 else
347 {
348 while (pPrev1->Internal.s.pNextR3)
349 pPrev1 = pPrev1->Internal.s.pNextR3;
350 pPrev1->Internal.s.pNextR3 = pDevIns;
351 }
352
353 /* The per device instance FIFO. */
354 PPDMDEVINS pPrev2 = paDevs[i].pDev->pInstances;
355 if (!pPrev2)
356 paDevs[i].pDev->pInstances = pDevIns;
357 else
358 {
359 while (pPrev2->Internal.s.pPerDeviceNextR3)
360 pPrev2 = pPrev2->Internal.s.pPerDeviceNextR3;
361 pPrev2->Internal.s.pPerDeviceNextR3 = pDevIns;
362 }
363
364 /*
365 * Call the constructor.
366 */
367 paDevs[i].pDev->cInstances++;
368 Log(("PDM: Constructing device '%s' instance %d...\n", pDevIns->pReg->szName, pDevIns->iInstance));
369 rc = pDevIns->pReg->pfnConstruct(pDevIns, pDevIns->iInstance, pDevIns->pCfgHandle);
370 if (RT_FAILURE(rc))
371 {
372 LogRel(("PDM: Failed to construct '%s'/%d! %Rra\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
373 paDevs[i].pDev->cInstances--;
374 /* because we're damn lazy right now, we'll say that the destructor will be called even if the constructor fails. */
375 return rc;
376 }
377 } /* for device instances */
378
379#ifdef VBOX_WITH_USB
380 /* ditto for USB Devices. */
381 rc = pdmR3UsbInstantiateDevices(pVM);
382 if (RT_FAILURE(rc))
383 return rc;
384#endif
385
386
387 /*
388 *
389 * PCI BIOS Fake and Init Complete.
390 *
391 */
392 if (pVM->pdm.s.aPciBuses[0].pDevInsR3)
393 {
394 pdmLock(pVM);
395 rc = pVM->pdm.s.aPciBuses[0].pfnFakePCIBIOSR3(pVM->pdm.s.aPciBuses[0].pDevInsR3);
396 pdmUnlock(pVM);
397 if (RT_FAILURE(rc))
398 {
399 AssertMsgFailed(("PCI BIOS fake failed rc=%Rrc\n", rc));
400 return rc;
401 }
402 }
403
404 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
405 {
406 if (pDevIns->pReg->pfnInitComplete)
407 {
408 rc = pDevIns->pReg->pfnInitComplete(pDevIns);
409 if (RT_FAILURE(rc))
410 {
411 AssertMsgFailed(("InitComplete on device '%s'/%d failed with rc=%Rrc\n",
412 pDevIns->pReg->szName, pDevIns->iInstance, rc));
413 return rc;
414 }
415 }
416 }
417
418#ifdef VBOX_WITH_USB
419 /* ditto for USB Devices. */
420 rc = pdmR3UsbVMInitComplete(pVM);
421 if (RT_FAILURE(rc))
422 return rc;
423#endif
424
425 LogFlow(("pdmR3DevInit: returns %Rrc\n", VINF_SUCCESS));
426 return VINF_SUCCESS;
427}
428
429
430/**
431 * Lookups a device structure by name.
432 * @internal
433 */
434PPDMDEV pdmR3DevLookup(PVM pVM, const char *pszName)
435{
436 size_t cchName = strlen(pszName);
437 for (PPDMDEV pDev = pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
438 if ( pDev->cchName == cchName
439 && !strcmp(pDev->pReg->szName, pszName))
440 return pDev;
441 return NULL;
442}
443
444
445/**
446 * Loads the device modules.
447 *
448 * @returns VBox status code.
449 * @param pVM Pointer to the shared VM structure.
450 */
451static int pdmR3DevLoadModules(PVM pVM)
452{
453 /*
454 * Initialize the callback structure.
455 */
456 PDMDEVREGCBINT RegCB;
457 RegCB.Core.u32Version = PDM_DEVREG_CB_VERSION;
458 RegCB.Core.pfnRegister = pdmR3DevReg_Register;
459 RegCB.pVM = pVM;
460
461 /*
462 * Load the builtin module
463 */
464 PCFGMNODE pDevicesNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "PDM/Devices");
465 bool fLoadBuiltin;
466 int rc = CFGMR3QueryBool(pDevicesNode, "LoadBuiltin", &fLoadBuiltin);
467 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
468 fLoadBuiltin = true;
469 else if (RT_FAILURE(rc))
470 {
471 AssertMsgFailed(("Configuration error: Querying boolean \"LoadBuiltin\" failed with %Rrc\n", rc));
472 return rc;
473 }
474 if (fLoadBuiltin)
475 {
476 /* make filename */
477 char *pszFilename = pdmR3FileR3("VBoxDD", /* fShared = */ true);
478 if (!pszFilename)
479 return VERR_NO_TMP_MEMORY;
480 rc = pdmR3DevLoad(pVM, &RegCB, pszFilename, "VBoxDD");
481 RTMemTmpFree(pszFilename);
482 if (RT_FAILURE(rc))
483 return rc;
484
485 /* make filename */
486 pszFilename = pdmR3FileR3("VBoxDD2", /* fShared = */ true);
487 if (!pszFilename)
488 return VERR_NO_TMP_MEMORY;
489 rc = pdmR3DevLoad(pVM, &RegCB, pszFilename, "VBoxDD2");
490 RTMemTmpFree(pszFilename);
491 if (RT_FAILURE(rc))
492 return rc;
493 }
494
495 /*
496 * Load additional device modules.
497 */
498 PCFGMNODE pCur;
499 for (pCur = CFGMR3GetFirstChild(pDevicesNode); pCur; pCur = CFGMR3GetNextChild(pCur))
500 {
501 /*
502 * Get the name and path.
503 */
504 char szName[PDMMOD_NAME_LEN];
505 rc = CFGMR3GetName(pCur, &szName[0], sizeof(szName));
506 if (rc == VERR_CFGM_NOT_ENOUGH_SPACE)
507 {
508 AssertMsgFailed(("configuration error: The module name is too long, cchName=%zu.\n", CFGMR3GetNameLen(pCur)));
509 return VERR_PDM_MODULE_NAME_TOO_LONG;
510 }
511 else if (RT_FAILURE(rc))
512 {
513 AssertMsgFailed(("CFGMR3GetName -> %Rrc.\n", rc));
514 return rc;
515 }
516
517 /* the path is optional, if no path the module name + path is used. */
518 char szFilename[RTPATH_MAX];
519 rc = CFGMR3QueryString(pCur, "Path", &szFilename[0], sizeof(szFilename));
520 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
521 strcpy(szFilename, szName);
522 else if (RT_FAILURE(rc))
523 {
524 AssertMsgFailed(("configuration error: Failure to query the module path, rc=%Rrc.\n", rc));
525 return rc;
526 }
527
528 /* prepend path? */
529 if (!RTPathHavePath(szFilename))
530 {
531 char *psz = pdmR3FileR3(szFilename);
532 if (!psz)
533 return VERR_NO_TMP_MEMORY;
534 size_t cch = strlen(psz) + 1;
535 if (cch > sizeof(szFilename))
536 {
537 RTMemTmpFree(psz);
538 AssertMsgFailed(("Filename too long! cch=%d '%s'\n", cch, psz));
539 return VERR_FILENAME_TOO_LONG;
540 }
541 memcpy(szFilename, psz, cch);
542 RTMemTmpFree(psz);
543 }
544
545 /*
546 * Load the module and register it's devices.
547 */
548 rc = pdmR3DevLoad(pVM, &RegCB, szFilename, szName);
549 if (RT_FAILURE(rc))
550 return rc;
551 }
552
553 return VINF_SUCCESS;
554}
555
556
557/**
558 * Loads one device module and call the registration entry point.
559 *
560 * @returns VBox status code.
561 * @param pVM VM handle.
562 * @param pRegCB The registration callback stuff.
563 * @param pszFilename Module filename.
564 * @param pszName Module name.
565 */
566static int pdmR3DevLoad(PVM pVM, PPDMDEVREGCBINT pRegCB, const char *pszFilename, const char *pszName)
567{
568 /*
569 * Load it.
570 */
571 int rc = pdmR3LoadR3U(pVM->pUVM, pszFilename, pszName);
572 if (RT_SUCCESS(rc))
573 {
574 /*
575 * Get the registration export and call it.
576 */
577 FNPDMVBOXDEVICESREGISTER *pfnVBoxDevicesRegister;
578 rc = PDMR3LdrGetSymbolR3(pVM, pszName, "VBoxDevicesRegister", (void **)&pfnVBoxDevicesRegister);
579 if (RT_SUCCESS(rc))
580 {
581 Log(("PDM: Calling VBoxDevicesRegister (%p) of %s (%s)\n", pfnVBoxDevicesRegister, pszName, pszFilename));
582 rc = pfnVBoxDevicesRegister(&pRegCB->Core, VBOX_VERSION);
583 if (RT_SUCCESS(rc))
584 Log(("PDM: Successfully loaded device module %s (%s).\n", pszName, pszFilename));
585 else
586 AssertMsgFailed(("VBoxDevicesRegister failed with rc=%Rrc for module %s (%s)\n", rc, pszName, pszFilename));
587 }
588 else
589 {
590 AssertMsgFailed(("Failed to locate 'VBoxDevicesRegister' in %s (%s) rc=%Rrc\n", pszName, pszFilename, rc));
591 if (rc == VERR_SYMBOL_NOT_FOUND)
592 rc = VERR_PDM_NO_REGISTRATION_EXPORT;
593 }
594 }
595 else
596 AssertMsgFailed(("Failed to load %s %s!\n", pszFilename, pszName));
597 return rc;
598}
599
600
601/**
602 * @interface_method_impl{PDMDEVREGCB,pfnRegister}
603 */
604static DECLCALLBACK(int) pdmR3DevReg_Register(PPDMDEVREGCB pCallbacks, PCPDMDEVREG pReg)
605{
606 /*
607 * Validate the registration structure.
608 */
609 Assert(pReg);
610 AssertMsgReturn(pReg->u32Version == PDM_DEVREG_VERSION,
611 ("Unknown struct version %#x!\n", pReg->u32Version),
612 VERR_PDM_UNKNOWN_DEVREG_VERSION);
613
614 AssertMsgReturn( pReg->szName[0]
615 && strlen(pReg->szName) < sizeof(pReg->szName),
616 ("Invalid name '%s'\n", pReg->szName),
617 VERR_PDM_INVALID_DEVICE_REGISTRATION);
618 AssertMsgReturn( !(pReg->fFlags & PDM_DEVREG_FLAGS_RC)
619 || ( pReg->szRCMod[0]
620 && strlen(pReg->szRCMod) < sizeof(pReg->szRCMod)),
621 ("Invalid GC module name '%s' - (Device %s)\n", pReg->szRCMod, pReg->szName),
622 VERR_PDM_INVALID_DEVICE_REGISTRATION);
623 AssertMsgReturn( !(pReg->fFlags & PDM_DEVREG_FLAGS_R0)
624 || ( pReg->szR0Mod[0]
625 && strlen(pReg->szR0Mod) < sizeof(pReg->szR0Mod)),
626 ("Invalid R0 module name '%s' - (Device %s)\n", pReg->szR0Mod, pReg->szName),
627 VERR_PDM_INVALID_DEVICE_REGISTRATION);
628 AssertMsgReturn((pReg->fFlags & PDM_DEVREG_FLAGS_HOST_BITS_MASK) == PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT,
629 ("Invalid host bits flags! fFlags=%#x (Device %s)\n", pReg->fFlags, pReg->szName),
630 VERR_PDM_INVALID_DEVICE_HOST_BITS);
631 AssertMsgReturn((pReg->fFlags & PDM_DEVREG_FLAGS_GUEST_BITS_MASK),
632 ("Invalid guest bits flags! fFlags=%#x (Device %s)\n", pReg->fFlags, pReg->szName),
633 VERR_PDM_INVALID_DEVICE_REGISTRATION);
634 AssertMsgReturn(pReg->fClass,
635 ("No class! (Device %s)\n", pReg->szName),
636 VERR_PDM_INVALID_DEVICE_REGISTRATION);
637 AssertMsgReturn(pReg->cMaxInstances > 0,
638 ("Max instances %u! (Device %s)\n", pReg->cMaxInstances, pReg->szName),
639 VERR_PDM_INVALID_DEVICE_REGISTRATION);
640 AssertMsgReturn(pReg->cbInstance <= (uint32_t)(pReg->fFlags & (PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0) ? 96 * _1K : _1M),
641 ("Instance size %d bytes! (Device %s)\n", pReg->cbInstance, pReg->szName),
642 VERR_PDM_INVALID_DEVICE_REGISTRATION);
643 AssertMsgReturn(pReg->pfnConstruct,
644 ("No constructore! (Device %s)\n", pReg->szName),
645 VERR_PDM_INVALID_DEVICE_REGISTRATION);
646 AssertLogRelMsgReturn((pReg->fFlags & PDM_DEVREG_FLAGS_GUEST_BITS_MASK) == PDM_DEVREG_FLAGS_GUEST_BITS_DEFAULT,
647 ("PDM: Rejected device '%s' because it didn't match the guest bits.\n", pReg->szName),
648 VERR_PDM_INVALID_DEVICE_GUEST_BITS);
649 AssertLogRelMsg(pReg->u32VersionEnd == PDM_DEVREG_VERSION,
650 ("u32VersionEnd=%#x, expected %#x. (szName=%s)\n",
651 pReg->u32VersionEnd, PDM_DEVREG_VERSION, pReg->szName));
652
653 /*
654 * Check for duplicate and find FIFO entry at the same time.
655 */
656 PCPDMDEVREGCBINT pRegCB = (PCPDMDEVREGCBINT)pCallbacks;
657 PPDMDEV pDevPrev = NULL;
658 PPDMDEV pDev = pRegCB->pVM->pdm.s.pDevs;
659 for (; pDev; pDevPrev = pDev, pDev = pDev->pNext)
660 AssertMsgReturn(strcmp(pDev->pReg->szName, pReg->szName),
661 ("Device '%s' already exists\n", pReg->szName),
662 VERR_PDM_DEVICE_NAME_CLASH);
663
664 /*
665 * Allocate new device structure and insert it into the list.
666 */
667 pDev = (PPDMDEV)MMR3HeapAlloc(pRegCB->pVM, MM_TAG_PDM_DEVICE, sizeof(*pDev));
668 if (pDev)
669 {
670 pDev->pNext = NULL;
671 pDev->cInstances = 0;
672 pDev->pInstances = NULL;
673 pDev->pReg = pReg;
674 pDev->cchName = (uint32_t)strlen(pReg->szName);
675
676 if (pDevPrev)
677 pDevPrev->pNext = pDev;
678 else
679 pRegCB->pVM->pdm.s.pDevs = pDev;
680 Log(("PDM: Registered device '%s'\n", pReg->szName));
681 return VINF_SUCCESS;
682 }
683 return VERR_NO_MEMORY;
684}
685
686
687/**
688 * Locates a LUN.
689 *
690 * @returns VBox status code.
691 * @param pVM VM Handle.
692 * @param pszDevice Device name.
693 * @param iInstance Device instance.
694 * @param iLun The Logical Unit to obtain the interface of.
695 * @param ppLun Where to store the pointer to the LUN if found.
696 * @thread Try only do this in EMT...
697 */
698int pdmR3DevFindLun(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPPDMLUN ppLun)
699{
700 /*
701 * Iterate registered devices looking for the device.
702 */
703 size_t cchDevice = strlen(pszDevice);
704 for (PPDMDEV pDev = pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
705 {
706 if ( pDev->cchName == cchDevice
707 && !memcmp(pDev->pReg->szName, pszDevice, cchDevice))
708 {
709 /*
710 * Iterate device instances.
711 */
712 for (PPDMDEVINS pDevIns = pDev->pInstances; pDevIns; pDevIns = pDevIns->Internal.s.pPerDeviceNextR3)
713 {
714 if (pDevIns->iInstance == iInstance)
715 {
716 /*
717 * Iterate luns.
718 */
719 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
720 {
721 if (pLun->iLun == iLun)
722 {
723 *ppLun = pLun;
724 return VINF_SUCCESS;
725 }
726 }
727 return VERR_PDM_LUN_NOT_FOUND;
728 }
729 }
730 return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND;
731 }
732 }
733 return VERR_PDM_DEVICE_NOT_FOUND;
734}
735
736
737/**
738 * Attaches a preconfigured driver to an existing device instance.
739 *
740 * This is used to change drivers and suchlike at runtime.
741 *
742 * @returns VBox status code.
743 * @param pVM VM Handle.
744 * @param pszDevice Device name.
745 * @param iInstance Device instance.
746 * @param iLun The Logical Unit to obtain the interface of.
747 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
748 * @param ppBase Where to store the base interface pointer. Optional.
749 * @thread EMT
750 */
751VMMR3DECL(int) PDMR3DeviceAttach(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, uint32_t fFlags, PPPDMIBASE ppBase)
752{
753 VM_ASSERT_EMT(pVM);
754 LogFlow(("PDMR3DeviceAttach: pszDevice=%p:{%s} iInstance=%d iLun=%d fFlags=%#x ppBase=%p\n",
755 pszDevice, pszDevice, iInstance, iLun, fFlags, ppBase));
756
757 /*
758 * Find the LUN in question.
759 */
760 PPDMLUN pLun;
761 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
762 if (RT_SUCCESS(rc))
763 {
764 /*
765 * Can we attach anything at runtime?
766 */
767 PPDMDEVINS pDevIns = pLun->pDevIns;
768 if (pDevIns->pReg->pfnAttach)
769 {
770 if (!pLun->pTop)
771 {
772 rc = pDevIns->pReg->pfnAttach(pDevIns, iLun, fFlags);
773 }
774 else
775 rc = VERR_PDM_DRIVER_ALREADY_ATTACHED;
776 }
777 else
778 rc = VERR_PDM_DEVICE_NO_RT_ATTACH;
779
780 if (ppBase)
781 *ppBase = pLun->pTop ? &pLun->pTop->IBase : NULL;
782 }
783 else if (ppBase)
784 *ppBase = NULL;
785
786 if (ppBase)
787 LogFlow(("PDMR3DeviceAttach: returns %Rrc *ppBase=%p\n", rc, *ppBase));
788 else
789 LogFlow(("PDMR3DeviceAttach: returns %Rrc\n", rc));
790 return rc;
791}
792
793
794/**
795 * Detaches a driver chain from an existing device instance.
796 *
797 * This is used to change drivers and suchlike at runtime.
798 *
799 * @returns VBox status code.
800 * @param pVM VM Handle.
801 * @param pszDevice Device name.
802 * @param iInstance Device instance.
803 * @param iLun The Logical Unit to obtain the interface of.
804 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
805 * @thread EMT
806 */
807VMMR3DECL(int) PDMR3DeviceDetach(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, uint32_t fFlags)
808{
809 return PDMR3DriverDetach(pVM, pszDevice, iInstance, iLun, NULL, 0, fFlags);
810}
811
812
813/**
814 * Attaches a preconfigured driver to an existing device or driver instance.
815 *
816 * This is used to change drivers and suchlike at runtime. The driver or device
817 * at the end of the chain will be told to attach to whatever is configured
818 * below it.
819 *
820 * @returns VBox status code.
821 * @param pVM VM Handle.
822 * @param pszDevice Device name.
823 * @param iInstance Device instance.
824 * @param iLun The Logical Unit to obtain the interface of.
825 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
826 * @param ppBase Where to store the base interface pointer. Optional.
827 *
828 * @thread EMT
829 */
830VMMR3DECL(int) PDMR3DriverAttach(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, uint32_t fFlags, PPPDMIBASE ppBase)
831{
832 VM_ASSERT_EMT(pVM);
833 LogFlow(("PDMR3DriverAttach: pszDevice=%p:{%s} iInstance=%d iLun=%d fFlags=%#x ppBase=%p\n",
834 pszDevice, pszDevice, iInstance, iLun, fFlags, ppBase));
835
836 if (ppBase)
837 *ppBase = NULL;
838
839 /*
840 * Find the LUN in question.
841 */
842 PPDMLUN pLun;
843 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
844 if (RT_SUCCESS(rc))
845 {
846 /*
847 * Anything attached to the LUN?
848 */
849 PPDMDRVINS pDrvIns = pLun->pTop;
850 if (!pDrvIns)
851 {
852 /* No, ask the device to attach to the new stuff. */
853 PPDMDEVINS pDevIns = pLun->pDevIns;
854 if (pDevIns->pReg->pfnAttach)
855 {
856 rc = pDevIns->pReg->pfnAttach(pDevIns, iLun, fFlags);
857 if (RT_SUCCESS(rc) && ppBase)
858 *ppBase = pLun->pTop ? &pLun->pTop->IBase : NULL;
859 }
860 else
861 rc = VERR_PDM_DEVICE_NO_RT_ATTACH;
862 }
863 else
864 {
865 /* Yes, find the bottom most driver and ask it to attach to the new stuff. */
866 while (pDrvIns->Internal.s.pDown)
867 pDrvIns = pDrvIns->Internal.s.pDown;
868 if (pDrvIns->pReg->pfnAttach)
869 {
870 rc = pDrvIns->pReg->pfnAttach(pDrvIns, fFlags);
871 if (RT_SUCCESS(rc) && ppBase)
872 *ppBase = pDrvIns->Internal.s.pDown
873 ? &pDrvIns->Internal.s.pDown->IBase
874 : NULL;
875 }
876 else
877 rc = VERR_PDM_DRIVER_NO_RT_ATTACH;
878 }
879 }
880
881 if (ppBase)
882 LogFlow(("PDMR3DriverAttach: returns %Rrc *ppBase=%p\n", rc, *ppBase));
883 else
884 LogFlow(("PDMR3DriverAttach: returns %Rrc\n", rc));
885 return rc;
886}
887
888
889/**
890 * Detaches the specified driver instance.
891 *
892 * This is used to replumb drivers at runtime for simulating hot plugging and
893 * media changes.
894 *
895 * This is a superset of PDMR3DeviceDetach. It allows detaching drivers from
896 * any driver or device by specifying the driver to start detaching at. The
897 * only prerequisite is that the driver or device above implements the
898 * pfnDetach callback (PDMDRVREG / PDMDEVREG).
899 *
900 * @returns VBox status code.
901 * @param pVM VM Handle.
902 * @param pszDevice Device name.
903 * @param iDevIns Device instance.
904 * @param iLun The Logical Unit in which to look for the driver.
905 * @param pszDriver The name of the driver which to detach. If NULL
906 * then the entire driver chain is detatched.
907 * @param iOccurance The occurance of that driver in the chain. This is
908 * usually 0.
909 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
910 * @thread EMT
911 */
912VMMR3DECL(int) PDMR3DriverDetach(PVM pVM, const char *pszDevice, unsigned iDevIns, unsigned iLun,
913 const char *pszDriver, unsigned iOccurance, uint32_t fFlags)
914{
915 LogFlow(("PDMR3DriverDetach: pszDevice=%p:{%s} iDevIns=%u iLun=%u pszDriver=%p:{%s} iOccurance=%u fFlags=%#x\n",
916 pszDevice, pszDevice, iDevIns, iLun, pszDriver, iOccurance, fFlags));
917 VM_ASSERT_EMT(pVM);
918 AssertPtr(pszDevice);
919 AssertPtrNull(pszDriver);
920 Assert(iOccurance == 0 || pszDriver);
921 Assert(!(fFlags & ~(PDM_TACH_FLAGS_NOT_HOT_PLUG)));
922
923 /*
924 * Find the LUN in question.
925 */
926 PPDMLUN pLun;
927 int rc = pdmR3DevFindLun(pVM, pszDevice, iDevIns, iLun, &pLun);
928 if (RT_SUCCESS(rc))
929 {
930 /*
931 * Locate the driver.
932 */
933 PPDMDRVINS pDrvIns = pLun->pTop;
934 if (pDrvIns)
935 {
936 if (pszDriver)
937 {
938 while (pDrvIns)
939 {
940 if (!strcmp(pDrvIns->pReg->szName, pszDriver))
941 {
942 if (iOccurance == 0)
943 break;
944 iOccurance--;
945 }
946 pDrvIns = pDrvIns->Internal.s.pDown;
947 }
948 }
949 if (pDrvIns)
950 rc = pdmR3DrvDetach(pDrvIns, fFlags);
951 else
952 rc = VERR_PDM_DRIVER_INSTANCE_NOT_FOUND;
953 }
954 else
955 rc = VINF_PDM_NO_DRIVER_ATTACHED_TO_LUN;
956 }
957
958 LogFlow(("PDMR3DriverDetach: returns %Rrc\n", rc));
959 return rc;
960}
961
962
963/**
964 * Runtime detach and reattach of a new driver chain or sub chain.
965 *
966 * This is intended to be called on a non-EMT thread, this will instantiate the
967 * new driver (sub-)chain, and then the EMTs will do the actual replumbing. The
968 * destruction of the old driver chain will be taken care of on the calling
969 * thread.
970 *
971 * @returns VBox status code.
972 * @param pVM VM Handle.
973 * @param pszDevice Device name.
974 * @param iDevIns Device instance.
975 * @param iLun The Logical Unit in which to look for the driver.
976 * @param pszDriver The name of the driver which to detach and replace.
977 * If NULL then the entire driver chain is to be
978 * reattached.
979 * @param iOccurance The occurance of that driver in the chain. This is
980 * usually 0.
981 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
982 * @param pCfg The configuration of the new driver chain that is
983 * going to be attached. The subtree starts with the
984 * node containing a Driver key, a Config subtree and
985 * optionally an AttachedDriver subtree.
986 * If this parameter is NULL, then this call will work
987 * like at a non-pause version of PDMR3DriverDetach.
988 * @param ppBase Where to store the base interface pointer to the new
989 * driver. Optional.
990 *
991 * @thread Any thread. The EMTs will be involved at some point though.
992 */
993VMMR3DECL(int) PDMR3DriverReattach(PVM pVM, const char *pszDevice, unsigned iDevIns, unsigned iLun,
994 const char *pszDriver, unsigned iOccurance, uint32_t fFlags,
995 PCFGMNODE pCfg, PPPDMIBASE ppBase)
996{
997 return VERR_NOT_IMPLEMENTED;
998}
999
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