VirtualBox

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

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

PDMDevice.cpp: Check the number of devices instances against the max number.

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