VirtualBox

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

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

PDM: typo, device => driver

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 36.5 KB
Line 
1/* $Id: PDMDevice.cpp 23077 2009-09-17 09:26:32Z 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 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 } /* devices */
230 Assert(i == cDevs);
231
232 /*
233 * Sort the device array ascending on u32Order. (bubble)
234 */
235 unsigned c = cDevs - 1;
236 while (c)
237 {
238 unsigned j = 0;
239 for (i = 0; i < c; i++)
240 if (paDevs[i].u32Order > paDevs[i + 1].u32Order)
241 {
242 paDevs[cDevs] = paDevs[i + 1];
243 paDevs[i + 1] = paDevs[i];
244 paDevs[i] = paDevs[cDevs];
245 j = i;
246 }
247 c = j;
248 }
249
250
251 /*
252 *
253 * Instantiate the devices.
254 *
255 */
256 for (i = 0; i < cDevs; i++)
257 {
258 /*
259 * Gather a bit of config.
260 */
261 /* trusted */
262 bool fTrusted;
263 rc = CFGMR3QueryBool(paDevs[i].pNode, "Trusted", &fTrusted);
264 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
265 fTrusted = false;
266 else if (RT_FAILURE(rc))
267 {
268 AssertMsgFailed(("configuration error: failed to query boolean \"Trusted\", rc=%Rrc\n", rc));
269 return rc;
270 }
271 /* config node */
272 PCFGMNODE pConfigNode = CFGMR3GetChild(paDevs[i].pNode, "Config");
273 if (!pConfigNode)
274 {
275 rc = CFGMR3InsertNode(paDevs[i].pNode, "Config", &pConfigNode);
276 if (RT_FAILURE(rc))
277 {
278 AssertMsgFailed(("Failed to create Config node! rc=%Rrc\n", rc));
279 return rc;
280 }
281 }
282 CFGMR3SetRestrictedRoot(pConfigNode);
283
284 /*
285 * Allocate the device instance.
286 */
287 size_t cb = RT_OFFSETOF(PDMDEVINS, achInstanceData[paDevs[i].pDev->pDevReg->cbInstance]);
288 cb = RT_ALIGN_Z(cb, 16);
289 PPDMDEVINS pDevIns;
290 if (paDevs[i].pDev->pDevReg->fFlags & (PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0))
291 rc = MMR3HyperAllocOnceNoRel(pVM, cb, 0, MM_TAG_PDM_DEVICE, (void **)&pDevIns);
292 else
293 rc = MMR3HeapAllocZEx(pVM, MM_TAG_PDM_DEVICE, cb, (void **)&pDevIns);
294 if (RT_FAILURE(rc))
295 {
296 AssertMsgFailed(("Failed to allocate %d bytes of instance data for device '%s'. rc=%Rrc\n",
297 cb, paDevs[i].pDev->pDevReg->szDeviceName, rc));
298 return rc;
299 }
300
301 /*
302 * Initialize it.
303 */
304 pDevIns->u32Version = PDM_DEVINS_VERSION;
305 //pDevIns->Internal.s.pNextR3 = NULL;
306 //pDevIns->Internal.s.pPerDeviceNextR3 = NULL;
307 pDevIns->Internal.s.pDevR3 = paDevs[i].pDev;
308 pDevIns->Internal.s.pVMR3 = pVM;
309 pDevIns->Internal.s.pVMR0 = pVM->pVMR0;
310 pDevIns->Internal.s.pVMRC = pVM->pVMRC;
311 //pDevIns->Internal.s.pLunsR3 = NULL;
312 pDevIns->Internal.s.pCfgHandle = paDevs[i].pNode;
313 //pDevIns->Internal.s.pPciDeviceR3 = NULL;
314 //pDevIns->Internal.s.pPciBusR3 = NULL;
315 //pDevIns->Internal.s.pPciDeviceR0 = 0;
316 //pDevIns->Internal.s.pPciBusR0 = 0;
317 //pDevIns->Internal.s.pPciDeviceRC = 0;
318 //pDevIns->Internal.s.pPciBusRC = 0;
319 pDevIns->pDevHlpR3 = fTrusted ? &g_pdmR3DevHlpTrusted : &g_pdmR3DevHlpUnTrusted;
320 pDevIns->pDevHlpRC = pDevHlpRC;
321 pDevIns->pDevHlpR0 = pDevHlpR0;
322 pDevIns->pDevReg = paDevs[i].pDev->pDevReg;
323 pDevIns->pCfgHandle = pConfigNode;
324 pDevIns->iInstance = paDevs[i].iInstance;
325 pDevIns->pvInstanceDataR3 = &pDevIns->achInstanceData[0];
326 pDevIns->pvInstanceDataRC = pDevIns->pDevReg->fFlags & PDM_DEVREG_FLAGS_RC
327 ? MMHyperR3ToRC(pVM, pDevIns->pvInstanceDataR3) : NIL_RTRCPTR;
328 pDevIns->pvInstanceDataR0 = pDevIns->pDevReg->fFlags & PDM_DEVREG_FLAGS_R0
329 ? MMHyperR3ToR0(pVM, pDevIns->pvInstanceDataR3) : NIL_RTR0PTR;
330
331 /*
332 * Link it into all the lists.
333 */
334 /* The global instance FIFO. */
335 PPDMDEVINS pPrev1 = pVM->pdm.s.pDevInstances;
336 if (!pPrev1)
337 pVM->pdm.s.pDevInstances = pDevIns;
338 else
339 {
340 while (pPrev1->Internal.s.pNextR3)
341 pPrev1 = pPrev1->Internal.s.pNextR3;
342 pPrev1->Internal.s.pNextR3 = pDevIns;
343 }
344
345 /* The per device instance FIFO. */
346 PPDMDEVINS pPrev2 = paDevs[i].pDev->pInstances;
347 if (!pPrev2)
348 paDevs[i].pDev->pInstances = pDevIns;
349 else
350 {
351 while (pPrev2->Internal.s.pPerDeviceNextR3)
352 pPrev2 = pPrev2->Internal.s.pPerDeviceNextR3;
353 pPrev2->Internal.s.pPerDeviceNextR3 = pDevIns;
354 }
355
356 /*
357 * Call the constructor.
358 */
359 Log(("PDM: Constructing device '%s' instance %d...\n", pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
360 rc = pDevIns->pDevReg->pfnConstruct(pDevIns, pDevIns->iInstance, pDevIns->pCfgHandle);
361 if (RT_FAILURE(rc))
362 {
363 LogRel(("PDM: Failed to construct '%s'/%d! %Rra\n", pDevIns->pDevReg->szDeviceName, pDevIns->iInstance, rc));
364 /* because we're damn lazy right now, we'll say that the destructor will be called even if the constructor fails. */
365 return rc;
366 }
367 } /* for device instances */
368
369#ifdef VBOX_WITH_USB
370 /* ditto for USB Devices. */
371 rc = pdmR3UsbInstantiateDevices(pVM);
372 if (RT_FAILURE(rc))
373 return rc;
374#endif
375
376
377 /*
378 *
379 * PCI BIOS Fake and Init Complete.
380 *
381 */
382 if (pVM->pdm.s.aPciBuses[0].pDevInsR3)
383 {
384 pdmLock(pVM);
385 rc = pVM->pdm.s.aPciBuses[0].pfnFakePCIBIOSR3(pVM->pdm.s.aPciBuses[0].pDevInsR3);
386 pdmUnlock(pVM);
387 if (RT_FAILURE(rc))
388 {
389 AssertMsgFailed(("PCI BIOS fake failed rc=%Rrc\n", rc));
390 return rc;
391 }
392 }
393
394 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
395 {
396 if (pDevIns->pDevReg->pfnInitComplete)
397 {
398 rc = pDevIns->pDevReg->pfnInitComplete(pDevIns);
399 if (RT_FAILURE(rc))
400 {
401 AssertMsgFailed(("InitComplete on device '%s'/%d failed with rc=%Rrc\n",
402 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance, rc));
403 return rc;
404 }
405 }
406 }
407
408#ifdef VBOX_WITH_USB
409 /* ditto for USB Devices. */
410 rc = pdmR3UsbVMInitComplete(pVM);
411 if (RT_FAILURE(rc))
412 return rc;
413#endif
414
415 LogFlow(("pdmR3DevInit: returns %Rrc\n", VINF_SUCCESS));
416 return VINF_SUCCESS;
417}
418
419
420/**
421 * Lookups a device structure by name.
422 * @internal
423 */
424PPDMDEV pdmR3DevLookup(PVM pVM, const char *pszName)
425{
426 size_t cchName = strlen(pszName);
427 for (PPDMDEV pDev = pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
428 if ( pDev->cchName == cchName
429 && !strcmp(pDev->pDevReg->szDeviceName, pszName))
430 return pDev;
431 return NULL;
432}
433
434
435/**
436 * Loads the device modules.
437 *
438 * @returns VBox status code.
439 * @param pVM Pointer to the shared VM structure.
440 */
441static int pdmR3DevLoadModules(PVM pVM)
442{
443 /*
444 * Initialize the callback structure.
445 */
446 PDMDEVREGCBINT RegCB;
447 RegCB.Core.u32Version = PDM_DEVREG_CB_VERSION;
448 RegCB.Core.pfnRegister = pdmR3DevReg_Register;
449 RegCB.Core.pfnMMHeapAlloc = pdmR3DevReg_MMHeapAlloc;
450 RegCB.pVM = pVM;
451
452 /*
453 * Load the builtin module
454 */
455 PCFGMNODE pDevicesNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "PDM/Devices");
456 bool fLoadBuiltin;
457 int rc = CFGMR3QueryBool(pDevicesNode, "LoadBuiltin", &fLoadBuiltin);
458 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
459 fLoadBuiltin = true;
460 else if (RT_FAILURE(rc))
461 {
462 AssertMsgFailed(("Configuration error: Querying boolean \"LoadBuiltin\" failed with %Rrc\n", rc));
463 return rc;
464 }
465 if (fLoadBuiltin)
466 {
467 /* make filename */
468 char *pszFilename = pdmR3FileR3("VBoxDD", /* fShared = */ true);
469 if (!pszFilename)
470 return VERR_NO_TMP_MEMORY;
471 rc = pdmR3DevLoad(pVM, &RegCB, pszFilename, "VBoxDD");
472 RTMemTmpFree(pszFilename);
473 if (RT_FAILURE(rc))
474 return rc;
475
476 /* make filename */
477 pszFilename = pdmR3FileR3("VBoxDD2", /* fShared = */ true);
478 if (!pszFilename)
479 return VERR_NO_TMP_MEMORY;
480 rc = pdmR3DevLoad(pVM, &RegCB, pszFilename, "VBoxDD2");
481 RTMemTmpFree(pszFilename);
482 if (RT_FAILURE(rc))
483 return rc;
484 }
485
486 /*
487 * Load additional device modules.
488 */
489 PCFGMNODE pCur;
490 for (pCur = CFGMR3GetFirstChild(pDevicesNode); pCur; pCur = CFGMR3GetNextChild(pCur))
491 {
492 /*
493 * Get the name and path.
494 */
495 char szName[PDMMOD_NAME_LEN];
496 rc = CFGMR3GetName(pCur, &szName[0], sizeof(szName));
497 if (rc == VERR_CFGM_NOT_ENOUGH_SPACE)
498 {
499 AssertMsgFailed(("configuration error: The module name is too long, cchName=%zu.\n", CFGMR3GetNameLen(pCur)));
500 return VERR_PDM_MODULE_NAME_TOO_LONG;
501 }
502 else if (RT_FAILURE(rc))
503 {
504 AssertMsgFailed(("CFGMR3GetName -> %Rrc.\n", rc));
505 return rc;
506 }
507
508 /* the path is optional, if no path the module name + path is used. */
509 char szFilename[RTPATH_MAX];
510 rc = CFGMR3QueryString(pCur, "Path", &szFilename[0], sizeof(szFilename));
511 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
512 strcpy(szFilename, szName);
513 else if (RT_FAILURE(rc))
514 {
515 AssertMsgFailed(("configuration error: Failure to query the module path, rc=%Rrc.\n", rc));
516 return rc;
517 }
518
519 /* prepend path? */
520 if (!RTPathHavePath(szFilename))
521 {
522 char *psz = pdmR3FileR3(szFilename);
523 if (!psz)
524 return VERR_NO_TMP_MEMORY;
525 size_t cch = strlen(psz) + 1;
526 if (cch > sizeof(szFilename))
527 {
528 RTMemTmpFree(psz);
529 AssertMsgFailed(("Filename too long! cch=%d '%s'\n", cch, psz));
530 return VERR_FILENAME_TOO_LONG;
531 }
532 memcpy(szFilename, psz, cch);
533 RTMemTmpFree(psz);
534 }
535
536 /*
537 * Load the module and register it's devices.
538 */
539 rc = pdmR3DevLoad(pVM, &RegCB, szFilename, szName);
540 if (RT_FAILURE(rc))
541 return rc;
542 }
543
544 return VINF_SUCCESS;
545}
546
547
548/**
549 * Loads one device module and call the registration entry point.
550 *
551 * @returns VBox status code.
552 * @param pVM VM handle.
553 * @param pRegCB The registration callback stuff.
554 * @param pszFilename Module filename.
555 * @param pszName Module name.
556 */
557static int pdmR3DevLoad(PVM pVM, PPDMDEVREGCBINT pRegCB, const char *pszFilename, const char *pszName)
558{
559 /*
560 * Load it.
561 */
562 int rc = pdmR3LoadR3U(pVM->pUVM, pszFilename, pszName);
563 if (RT_SUCCESS(rc))
564 {
565 /*
566 * Get the registration export and call it.
567 */
568 FNPDMVBOXDEVICESREGISTER *pfnVBoxDevicesRegister;
569 rc = PDMR3LdrGetSymbolR3(pVM, pszName, "VBoxDevicesRegister", (void **)&pfnVBoxDevicesRegister);
570 if (RT_SUCCESS(rc))
571 {
572 Log(("PDM: Calling VBoxDevicesRegister (%p) of %s (%s)\n", pfnVBoxDevicesRegister, pszName, pszFilename));
573 rc = pfnVBoxDevicesRegister(&pRegCB->Core, VBOX_VERSION);
574 if (RT_SUCCESS(rc))
575 Log(("PDM: Successfully loaded device module %s (%s).\n", pszName, pszFilename));
576 else
577 AssertMsgFailed(("VBoxDevicesRegister failed with rc=%Rrc for module %s (%s)\n", rc, pszName, pszFilename));
578 }
579 else
580 {
581 AssertMsgFailed(("Failed to locate 'VBoxDevicesRegister' in %s (%s) rc=%Rrc\n", pszName, pszFilename, rc));
582 if (rc == VERR_SYMBOL_NOT_FOUND)
583 rc = VERR_PDM_NO_REGISTRATION_EXPORT;
584 }
585 }
586 else
587 AssertMsgFailed(("Failed to load %s %s!\n", pszFilename, pszName));
588 return rc;
589}
590
591
592/**
593 * Registers a device with the current VM instance.
594 *
595 * @returns VBox status code.
596 * @param pCallbacks Pointer to the callback table.
597 * @param pDevReg Pointer to the device registration record.
598 * This data must be permanent and readonly.
599 */
600static DECLCALLBACK(int) pdmR3DevReg_Register(PPDMDEVREGCB pCallbacks, PCPDMDEVREG pDevReg)
601{
602 /*
603 * Validate the registration structure.
604 */
605 Assert(pDevReg);
606 if (pDevReg->u32Version != PDM_DEVREG_VERSION)
607 {
608 AssertMsgFailed(("Unknown struct version %#x!\n", pDevReg->u32Version));
609 return VERR_PDM_UNKNOWN_DEVREG_VERSION;
610 }
611 if ( !pDevReg->szDeviceName[0]
612 || strlen(pDevReg->szDeviceName) >= sizeof(pDevReg->szDeviceName))
613 {
614 AssertMsgFailed(("Invalid name '%s'\n", pDevReg->szDeviceName));
615 return VERR_PDM_INVALID_DEVICE_REGISTRATION;
616 }
617 if ( (pDevReg->fFlags & PDM_DEVREG_FLAGS_RC)
618 && ( !pDevReg->szRCMod[0]
619 || strlen(pDevReg->szRCMod) >= sizeof(pDevReg->szRCMod)))
620 {
621 AssertMsgFailed(("Invalid GC module name '%s' - (Device %s)\n", pDevReg->szRCMod, pDevReg->szDeviceName));
622 return VERR_PDM_INVALID_DEVICE_REGISTRATION;
623 }
624 if ( (pDevReg->fFlags & PDM_DEVREG_FLAGS_R0)
625 && ( !pDevReg->szR0Mod[0]
626 || strlen(pDevReg->szR0Mod) >= sizeof(pDevReg->szR0Mod)))
627 {
628 AssertMsgFailed(("Invalid R0 module name '%s' - (Device %s)\n", pDevReg->szR0Mod, pDevReg->szDeviceName));
629 return VERR_PDM_INVALID_DEVICE_REGISTRATION;
630 }
631 if ((pDevReg->fFlags & PDM_DEVREG_FLAGS_HOST_BITS_MASK) != PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT)
632 {
633 AssertMsgFailed(("Invalid host bits flags! fFlags=%#x (Device %s)\n", pDevReg->fFlags, pDevReg->szDeviceName));
634 return VERR_PDM_INVALID_DEVICE_HOST_BITS;
635 }
636 if (!(pDevReg->fFlags & PDM_DEVREG_FLAGS_GUEST_BITS_MASK))
637 {
638 AssertMsgFailed(("Invalid guest bits flags! fFlags=%#x (Device %s)\n", pDevReg->fFlags, pDevReg->szDeviceName));
639 return VERR_PDM_INVALID_DEVICE_REGISTRATION;
640 }
641 if (!pDevReg->fClass)
642 {
643 AssertMsgFailed(("No class! (Device %s)\n", pDevReg->szDeviceName));
644 return VERR_PDM_INVALID_DEVICE_REGISTRATION;
645 }
646 if (pDevReg->cMaxInstances <= 0)
647 {
648 AssertMsgFailed(("Max instances %u! (Device %s)\n", pDevReg->cMaxInstances, pDevReg->szDeviceName));
649 return VERR_PDM_INVALID_DEVICE_REGISTRATION;
650 }
651 if (pDevReg->cbInstance > (RTUINT)(pDevReg->fFlags & (PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0) ? 96 * _1K : _1M))
652 {
653 AssertMsgFailed(("Instance size %d bytes! (Device %s)\n", pDevReg->cbInstance, pDevReg->szDeviceName));
654 return VERR_PDM_INVALID_DEVICE_REGISTRATION;
655 }
656 if (!pDevReg->pfnConstruct)
657 {
658 AssertMsgFailed(("No constructore! (Device %s)\n", pDevReg->szDeviceName));
659 return VERR_PDM_INVALID_DEVICE_REGISTRATION;
660 }
661 /* Check matching guest bits last without any asserting. Enables trial and error registration. */
662 if (!(pDevReg->fFlags & PDM_DEVREG_FLAGS_GUEST_BITS_DEFAULT))
663 {
664 Log(("PDM: Rejected device '%s' because it didn't match the guest bits.\n", pDevReg->szDeviceName));
665 return VERR_PDM_INVALID_DEVICE_GUEST_BITS;
666 }
667 AssertLogRelMsg(pDevReg->u32VersionEnd == PDM_DEVREG_VERSION,
668 ("u32VersionEnd=%#x, expected %#x. (szDeviceName=%s)\n",
669 pDevReg->u32VersionEnd, PDM_DEVREG_VERSION, pDevReg->szDeviceName));
670
671 /*
672 * Check for duplicate and find FIFO entry at the same time.
673 */
674 PCPDMDEVREGCBINT pRegCB = (PCPDMDEVREGCBINT)pCallbacks;
675 PPDMDEV pDevPrev = NULL;
676 PPDMDEV pDev = pRegCB->pVM->pdm.s.pDevs;
677 for (; pDev; pDevPrev = pDev, pDev = pDev->pNext)
678 {
679 if (!strcmp(pDev->pDevReg->szDeviceName, pDevReg->szDeviceName))
680 {
681 AssertMsgFailed(("Device '%s' already exists\n", pDevReg->szDeviceName));
682 return VERR_PDM_DEVICE_NAME_CLASH;
683 }
684 }
685
686 /*
687 * Allocate new device structure and insert it into the list.
688 */
689 pDev = (PPDMDEV)MMR3HeapAlloc(pRegCB->pVM, MM_TAG_PDM_DEVICE, sizeof(*pDev));
690 if (pDev)
691 {
692 pDev->pNext = NULL;
693 pDev->cInstances = 0;
694 pDev->pInstances = NULL;
695 pDev->pDevReg = pDevReg;
696 pDev->cchName = (uint32_t)strlen(pDevReg->szDeviceName);
697
698 if (pDevPrev)
699 pDevPrev->pNext = pDev;
700 else
701 pRegCB->pVM->pdm.s.pDevs = pDev;
702 Log(("PDM: Registered device '%s'\n", pDevReg->szDeviceName));
703 return VINF_SUCCESS;
704 }
705 return VERR_NO_MEMORY;
706}
707
708
709/**
710 * Allocate memory which is associated with current VM instance
711 * and automatically freed on it's destruction.
712 *
713 * @returns Pointer to allocated memory. The memory is *NOT* zero-ed.
714 * @param pCallbacks Pointer to the callback table.
715 * @param cb Number of bytes to allocate.
716 */
717static DECLCALLBACK(void *) pdmR3DevReg_MMHeapAlloc(PPDMDEVREGCB pCallbacks, size_t cb)
718{
719 Assert(pCallbacks);
720 Assert(pCallbacks->u32Version == PDM_DEVREG_CB_VERSION);
721
722 void *pv = MMR3HeapAlloc(((PPDMDEVREGCBINT)pCallbacks)->pVM, MM_TAG_PDM_DEVICE_USER, cb);
723 LogFlow(("pdmR3DevReg_MMHeapAlloc(,%#zx): returns %p\n", cb, pv));
724 return pv;
725}
726
727
728/**
729 * Locates a LUN.
730 *
731 * @returns VBox status code.
732 * @param pVM VM Handle.
733 * @param pszDevice Device name.
734 * @param iInstance Device instance.
735 * @param iLun The Logical Unit to obtain the interface of.
736 * @param ppLun Where to store the pointer to the LUN if found.
737 * @thread Try only do this in EMT...
738 */
739int pdmR3DevFindLun(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPPDMLUN ppLun)
740{
741 /*
742 * Iterate registered devices looking for the device.
743 */
744 size_t cchDevice = strlen(pszDevice);
745 for (PPDMDEV pDev = pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
746 {
747 if ( pDev->cchName == cchDevice
748 && !memcmp(pDev->pDevReg->szDeviceName, pszDevice, cchDevice))
749 {
750 /*
751 * Iterate device instances.
752 */
753 for (PPDMDEVINS pDevIns = pDev->pInstances; pDevIns; pDevIns = pDevIns->Internal.s.pPerDeviceNextR3)
754 {
755 if (pDevIns->iInstance == iInstance)
756 {
757 /*
758 * Iterate luns.
759 */
760 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
761 {
762 if (pLun->iLun == iLun)
763 {
764 *ppLun = pLun;
765 return VINF_SUCCESS;
766 }
767 }
768 return VERR_PDM_LUN_NOT_FOUND;
769 }
770 }
771 return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND;
772 }
773 }
774 return VERR_PDM_DEVICE_NOT_FOUND;
775}
776
777
778/**
779 * Attaches a preconfigured driver to an existing device instance.
780 *
781 * This is used to change drivers and suchlike at runtime.
782 *
783 * @returns VBox status code.
784 * @param pVM VM Handle.
785 * @param pszDevice Device name.
786 * @param iInstance Device instance.
787 * @param iLun The Logical Unit to obtain the interface of.
788 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
789 * @param ppBase Where to store the base interface pointer. Optional.
790 * @thread EMT
791 */
792VMMR3DECL(int) PDMR3DeviceAttach(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, uint32_t fFlags, PPPDMIBASE ppBase)
793{
794 VM_ASSERT_EMT(pVM);
795 LogFlow(("PDMR3DeviceAttach: pszDevice=%p:{%s} iInstance=%d iLun=%d fFlags=%#x ppBase=%p\n",
796 pszDevice, pszDevice, iInstance, iLun, fFlags, ppBase));
797
798 /*
799 * Find the LUN in question.
800 */
801 PPDMLUN pLun;
802 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
803 if (RT_SUCCESS(rc))
804 {
805 /*
806 * Can we attach anything at runtime?
807 */
808 PPDMDEVINS pDevIns = pLun->pDevIns;
809 if (pDevIns->pDevReg->pfnAttach)
810 {
811 if (!pLun->pTop)
812 {
813 rc = pDevIns->pDevReg->pfnAttach(pDevIns, iLun, fFlags);
814 }
815 else
816 rc = VERR_PDM_DRIVER_ALREADY_ATTACHED;
817 }
818 else
819 rc = VERR_PDM_DEVICE_NO_RT_ATTACH;
820
821 if (ppBase)
822 *ppBase = pLun->pTop ? &pLun->pTop->IBase : NULL;
823 }
824 else if (ppBase)
825 *ppBase = NULL;
826
827 if (ppBase)
828 LogFlow(("PDMR3DeviceAttach: returns %Rrc *ppBase=%p\n", rc, *ppBase));
829 else
830 LogFlow(("PDMR3DeviceAttach: returns %Rrc\n", rc));
831 return rc;
832}
833
834
835/**
836 * Detaches a driver chain from an existing device instance.
837 *
838 * This is used to change drivers and suchlike at runtime.
839 *
840 * @returns VBox status code.
841 * @param pVM VM Handle.
842 * @param pszDevice Device name.
843 * @param iInstance Device instance.
844 * @param iLun The Logical Unit to obtain the interface of.
845 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
846 * @thread EMT
847 */
848VMMR3DECL(int) PDMR3DeviceDetach(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, uint32_t fFlags)
849{
850 return PDMR3DriverDetach(pVM, pszDevice, iInstance, iLun, NULL, 0, fFlags);
851}
852
853
854/**
855 * Attaches a preconfigured driver to an existing device or driver instance.
856 *
857 * This is used to change drivers and suchlike at runtime. The driver or device
858 * at the end of the chain will be told to attach to whatever is configured
859 * below it.
860 *
861 * @returns VBox status code.
862 * @param pVM VM Handle.
863 * @param pszDevice Device name.
864 * @param iInstance Device instance.
865 * @param iLun The Logical Unit to obtain the interface of.
866 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
867 * @param ppBase Where to store the base interface pointer. Optional.
868 *
869 * @thread EMT
870 */
871VMMR3DECL(int) PDMR3DriverAttach(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, uint32_t fFlags, PPPDMIBASE ppBase)
872{
873 VM_ASSERT_EMT(pVM);
874 LogFlow(("PDMR3DriverAttach: pszDevice=%p:{%s} iInstance=%d iLun=%d fFlags=%#x ppBase=%p\n",
875 pszDevice, pszDevice, iInstance, iLun, fFlags, ppBase));
876
877 if (ppBase)
878 *ppBase = NULL;
879
880 /*
881 * Find the LUN in question.
882 */
883 PPDMLUN pLun;
884 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
885 if (RT_SUCCESS(rc))
886 {
887 /*
888 * Anything attached to the LUN?
889 */
890 PPDMDRVINS pDrvIns = pLun->pTop;
891 if (!pDrvIns)
892 {
893 /* No, ask the device to attach to the new stuff. */
894 PPDMDEVINS pDevIns = pLun->pDevIns;
895 if (pDevIns->pDevReg->pfnAttach)
896 {
897 rc = pDevIns->pDevReg->pfnAttach(pDevIns, iLun, fFlags);
898 if (RT_SUCCESS(rc) && ppBase)
899 *ppBase = pLun->pTop ? &pLun->pTop->IBase : NULL;
900 }
901 else
902 rc = VERR_PDM_DEVICE_NO_RT_ATTACH;
903 }
904 else
905 {
906 /* Yes, find the bottom most driver and ask it to attach to the new stuff. */
907 while (pDrvIns->Internal.s.pDown)
908 pDrvIns = pDrvIns->Internal.s.pDown;
909 if (pDrvIns->pDrvReg->pfnAttach)
910 {
911 rc = pDrvIns->pDrvReg->pfnAttach(pDrvIns, fFlags);
912 if (RT_SUCCESS(rc) && ppBase)
913 *ppBase = pDrvIns->Internal.s.pDown
914 ? &pDrvIns->Internal.s.pDown->IBase
915 : NULL;
916 }
917 else
918 rc = VERR_PDM_DRIVER_NO_RT_ATTACH;
919 }
920 }
921
922 if (ppBase)
923 LogFlow(("PDMR3DriverAttach: returns %Rrc *ppBase=%p\n", rc, *ppBase));
924 else
925 LogFlow(("PDMR3DriverAttach: returns %Rrc\n", rc));
926 return rc;
927}
928
929
930/**
931 * Detaches the specified driver instance.
932 *
933 * This is used to replumb drivers at runtime for simulating hot plugging and
934 * media changes.
935 *
936 * This is a superset of PDMR3DeviceDetach. It allows detaching drivers from
937 * any driver or device by specifying the driver to start detaching at. The
938 * only prerequisite is that the driver or device above implements the
939 * pfnDetach callback (PDMDRVREG / PDMDEVREG).
940 *
941 * @returns VBox status code.
942 * @param pVM VM Handle.
943 * @param pszDevice Device name.
944 * @param iDevIns Device instance.
945 * @param iLun The Logical Unit in which to look for the driver.
946 * @param pszDriver The name of the driver which to detach. If NULL
947 * then the entire driver chain is detatched.
948 * @param iOccurance The occurance of that driver in the chain. This is
949 * usually 0.
950 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
951 * @thread EMT
952 */
953VMMR3DECL(int) PDMR3DriverDetach(PVM pVM, const char *pszDevice, unsigned iDevIns, unsigned iLun,
954 const char *pszDriver, unsigned iOccurance, uint32_t fFlags)
955{
956 LogFlow(("PDMR3DriverDetach: pszDevice=%p:{%s} iDevIns=%u iLun=%u pszDriver=%p:{%s} iOccurance=%u fFlags=%#x\n",
957 pszDevice, pszDevice, iDevIns, iLun, pszDriver, iOccurance, fFlags));
958 VM_ASSERT_EMT(pVM);
959 AssertPtr(pszDevice);
960 AssertPtrNull(pszDriver);
961 Assert(iOccurance == 0 || pszDriver);
962 Assert(!(fFlags & ~(PDM_TACH_FLAGS_NOT_HOT_PLUG)));
963
964 /*
965 * Find the LUN in question.
966 */
967 PPDMLUN pLun;
968 int rc = pdmR3DevFindLun(pVM, pszDevice, iDevIns, iLun, &pLun);
969 if (RT_SUCCESS(rc))
970 {
971 /*
972 * Locate the driver.
973 */
974 PPDMDRVINS pDrvIns = pLun->pTop;
975 if (pDrvIns)
976 {
977 if (pszDriver)
978 {
979 while (pDrvIns)
980 {
981 if (!strcmp(pDrvIns->pDrvReg->szDriverName, pszDriver))
982 {
983 if (iOccurance == 0)
984 break;
985 iOccurance--;
986 }
987 pDrvIns = pDrvIns->Internal.s.pDown;
988 }
989 }
990 if (pDrvIns)
991 rc = pdmR3DrvDetach(pDrvIns, fFlags);
992 else
993 rc = VERR_PDM_DRIVER_INSTANCE_NOT_FOUND;
994 }
995 else
996 rc = VINF_PDM_NO_DRIVER_ATTACHED_TO_LUN;
997 }
998
999 LogFlow(("PDMR3DriverDetach: returns %Rrc\n", rc));
1000 return rc;
1001}
1002
1003
1004/**
1005 * Runtime detach and reattach of a new driver chain or sub chain.
1006 *
1007 * This is intended to be called on a non-EMT thread, this will instantiate the
1008 * new driver (sub-)chain, and then the EMTs will do the actual replumbing. The
1009 * destruction of the old driver chain will be taken care of on the calling
1010 * thread.
1011 *
1012 * @returns VBox status code.
1013 * @param pVM VM Handle.
1014 * @param pszDevice Device name.
1015 * @param iDevIns Device instance.
1016 * @param iLun The Logical Unit in which to look for the driver.
1017 * @param pszDriver The name of the driver which to detach and replace.
1018 * If NULL then the entire driver chain is to be
1019 * reattached.
1020 * @param iOccurance The occurance of that driver in the chain. This is
1021 * usually 0.
1022 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
1023 * @param pCfg The configuration of the new driver chain that is
1024 * going to be attached. The subtree starts with the
1025 * node containing a Driver key, a Config subtree and
1026 * optionally an AttachedDriver subtree.
1027 * If this parameter is NULL, then this call will work
1028 * like at a non-pause version of PDMR3DriverDetach.
1029 * @param ppBase Where to store the base interface pointer to the new
1030 * driver. Optional.
1031 *
1032 * @thread Any thread. The EMTs will be involved at some point though.
1033 */
1034VMMR3DECL(int) PDMR3DriverReattach(PVM pVM, const char *pszDevice, unsigned iDevIns, unsigned iLun,
1035 const char *pszDriver, unsigned iOccurance, uint32_t fFlags,
1036 PCFGMNODE pCfg, PPPDMIBASE ppBase)
1037{
1038 return VERR_NOT_IMPLEMENTED;
1039}
1040
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