VirtualBox

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

Last change on this file since 92723 was 92723, checked in by vboxsync, 3 years ago

VMM: More driverless adjustments. bugref:10138

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 49.6 KB
Line 
1/* $Id: PDMDevice.cpp 92723 2021-12-02 23:08:59Z vboxsync $ */
2/** @file
3 * PDM - Pluggable Device and Driver Manager, Device parts.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_PDM_DEVICE
23#define PDMPCIDEV_INCLUDE_PRIVATE /* Hack to get pdmpcidevint.h included at the right point. */
24#include "PDMInternal.h"
25#include <VBox/vmm/pdm.h>
26#include <VBox/vmm/apic.h>
27#include <VBox/vmm/cfgm.h>
28#include <VBox/vmm/dbgf.h>
29#include <VBox/vmm/hm.h>
30#include <VBox/vmm/mm.h>
31#include <VBox/vmm/iom.h>
32#include <VBox/vmm/pgm.h>
33#include <VBox/vmm/vm.h>
34#include <VBox/vmm/uvm.h>
35#include <VBox/vmm/vmm.h>
36
37#include <VBox/version.h>
38#include <VBox/log.h>
39#include <VBox/msi.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 /** Pointer to the configuration node the registrations should be
68 * associated with. Can be NULL. */
69 PCFGMNODE pCfgNode;
70} PDMDEVREGCBINT;
71/** Pointer to a PDMDEVREGCBINT structure. */
72typedef PDMDEVREGCBINT *PPDMDEVREGCBINT;
73/** Pointer to a const PDMDEVREGCBINT structure. */
74typedef const PDMDEVREGCBINT *PCPDMDEVREGCBINT;
75
76
77/*********************************************************************************************************************************
78* Internal Functions *
79*********************************************************************************************************************************/
80static DECLCALLBACK(int) pdmR3DevReg_Register(PPDMDEVREGCB pCallbacks, PCPDMDEVREG pReg);
81static int pdmR3DevLoadModules(PVM pVM);
82static int pdmR3DevLoad(PVM pVM, PPDMDEVREGCBINT pRegCB, const char *pszFilename, const char *pszName);
83
84
85
86
87/**
88 * This function will initialize the devices for this VM instance.
89 *
90 *
91 * First of all this mean loading the builtin device and letting them
92 * register themselves. Beyond that any additional device modules are
93 * loaded and called for registration.
94 *
95 * Then the device configuration is enumerated, the instantiation order
96 * is determined, and finally they are instantiated.
97 *
98 * After all devices have been successfully instantiated the primary
99 * PCI Bus device is called to emulate the PCI BIOS, i.e. making the
100 * resource assignments. If there is no PCI device, this step is of course
101 * skipped.
102 *
103 * Finally the init completion routines of the instantiated devices
104 * are called.
105 *
106 * @returns VBox status code.
107 * @param pVM The cross context VM structure.
108 */
109int pdmR3DevInit(PVM pVM)
110{
111 LogFlow(("pdmR3DevInit:\n"));
112
113 AssertRelease(!(RT_UOFFSETOF(PDMDEVINS, achInstanceData) & 15));
114 AssertRelease(sizeof(pVM->pdm.s.pDevInstances->Internal.s) <= sizeof(pVM->pdm.s.pDevInstances->Internal.padding));
115
116 /*
117 * Load device modules.
118 */
119 int rc = pdmR3DevLoadModules(pVM);
120 if (RT_FAILURE(rc))
121 return rc;
122
123#ifdef VBOX_WITH_USB
124 /* ditto for USB Devices. */
125 rc = pdmR3UsbLoadModules(pVM);
126 if (RT_FAILURE(rc))
127 return rc;
128#endif
129
130 /*
131 * Get the RC & R0 devhlps and create the devhlp R3 task queue.
132 */
133 rc = PDMR3QueueCreateInternal(pVM, sizeof(PDMDEVHLPTASK), 8, 0, pdmR3DevHlpQueueConsumer, true, "DevHlp",
134 &pVM->pdm.s.pDevHlpQueueR3);
135 AssertRCReturn(rc, rc);
136 pVM->pdm.s.pDevHlpQueueR0 = PDMQueueR0Ptr(pVM->pdm.s.pDevHlpQueueR3);
137
138 /*
139 *
140 * Enumerate the device instance configurations
141 * and come up with a instantiation order.
142 *
143 */
144 /* Switch to /Devices, which contains the device instantiations. */
145 PCFGMNODE pDevicesNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "Devices");
146
147 /*
148 * Count the device instances.
149 */
150 PCFGMNODE pCur;
151 PCFGMNODE pInstanceNode;
152 unsigned cDevs = 0;
153 for (pCur = CFGMR3GetFirstChild(pDevicesNode); pCur; pCur = CFGMR3GetNextChild(pCur))
154 for (pInstanceNode = CFGMR3GetFirstChild(pCur); pInstanceNode; pInstanceNode = CFGMR3GetNextChild(pInstanceNode))
155 cDevs++;
156 if (!cDevs)
157 {
158 Log(("PDM: No devices were configured!\n"));
159 return VINF_SUCCESS;
160 }
161 Log2(("PDM: cDevs=%u\n", cDevs));
162
163 /*
164 * Collect info on each device instance.
165 */
166 struct DEVORDER
167 {
168 /** Configuration node. */
169 PCFGMNODE pNode;
170 /** Pointer to device. */
171 PPDMDEV pDev;
172 /** Init order. */
173 uint32_t u32Order;
174 /** VBox instance number. */
175 uint32_t iInstance;
176 } *paDevs = (struct DEVORDER *)alloca(sizeof(paDevs[0]) * (cDevs + 1)); /* (One extra for swapping) */
177 Assert(paDevs);
178 unsigned i = 0;
179 for (pCur = CFGMR3GetFirstChild(pDevicesNode); pCur; pCur = CFGMR3GetNextChild(pCur))
180 {
181 /* Get the device name. */
182 char szName[sizeof(paDevs[0].pDev->pReg->szName)];
183 rc = CFGMR3GetName(pCur, szName, sizeof(szName));
184 AssertMsgRCReturn(rc, ("Configuration error: device name is too long (or something)! rc=%Rrc\n", rc), rc);
185
186 /* Find the device. */
187 PPDMDEV pDev = pdmR3DevLookup(pVM, szName);
188 AssertLogRelMsgReturn(pDev, ("Configuration error: device '%s' not found!\n", szName), VERR_PDM_DEVICE_NOT_FOUND);
189
190 /* Configured priority or use default based on device class? */
191 uint32_t u32Order;
192 rc = CFGMR3QueryU32(pCur, "Priority", &u32Order);
193 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
194 {
195 uint32_t u32 = pDev->pReg->fClass;
196 for (u32Order = 1; !(u32 & u32Order); u32Order <<= 1)
197 /* nop */;
198 }
199 else
200 AssertMsgRCReturn(rc, ("Configuration error: reading \"Priority\" for the '%s' device failed rc=%Rrc!\n", szName, rc), rc);
201
202 /* Enumerate the device instances. */
203 uint32_t const iStart = i;
204 for (pInstanceNode = CFGMR3GetFirstChild(pCur); pInstanceNode; pInstanceNode = CFGMR3GetNextChild(pInstanceNode))
205 {
206 paDevs[i].pNode = pInstanceNode;
207 paDevs[i].pDev = pDev;
208 paDevs[i].u32Order = u32Order;
209
210 /* Get the instance number. */
211 char szInstance[32];
212 rc = CFGMR3GetName(pInstanceNode, szInstance, sizeof(szInstance));
213 AssertMsgRCReturn(rc, ("Configuration error: instance name is too long (or something)! rc=%Rrc\n", rc), rc);
214 char *pszNext = NULL;
215 rc = RTStrToUInt32Ex(szInstance, &pszNext, 0, &paDevs[i].iInstance);
216 AssertMsgRCReturn(rc, ("Configuration error: RTStrToInt32Ex failed on the instance name '%s'! rc=%Rrc\n", szInstance, rc), rc);
217 AssertMsgReturn(!*pszNext, ("Configuration error: the instance name '%s' isn't all digits. (%s)\n", szInstance, pszNext), VERR_INVALID_PARAMETER);
218
219 /* next instance */
220 i++;
221 }
222
223 /* check the number of instances */
224 if (i - iStart > pDev->pReg->cMaxInstances)
225 AssertLogRelMsgFailedReturn(("Configuration error: Too many instances of %s was configured: %u, max %u\n",
226 szName, i - iStart, pDev->pReg->cMaxInstances),
227 VERR_PDM_TOO_MANY_DEVICE_INSTANCES);
228 } /* devices */
229 Assert(i == cDevs);
230
231 /*
232 * Sort (bubble) the device array ascending on u32Order and instance number
233 * for a device.
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 || ( paDevs[i].u32Order == paDevs[i + 1].u32Order
242 && paDevs[i].iInstance > paDevs[i + 1].iInstance
243 && paDevs[i].pDev == paDevs[i + 1].pDev) )
244 {
245 paDevs[cDevs] = paDevs[i + 1];
246 paDevs[i + 1] = paDevs[i];
247 paDevs[i] = paDevs[cDevs];
248 j = i;
249 }
250 c = j;
251 }
252
253
254 /*
255 *
256 * Instantiate the devices.
257 *
258 */
259 for (i = 0; i < cDevs; i++)
260 {
261 PDMDEVREGR3 const * const pReg = paDevs[i].pDev->pReg;
262
263 /*
264 * Gather a bit of config.
265 */
266 /* trusted */
267 bool fTrusted;
268 rc = CFGMR3QueryBool(paDevs[i].pNode, "Trusted", &fTrusted);
269 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
270 fTrusted = false;
271 else if (RT_FAILURE(rc))
272 {
273 AssertMsgFailed(("configuration error: failed to query boolean \"Trusted\", rc=%Rrc\n", rc));
274 return rc;
275 }
276
277 /* RZEnabled, R0Enabled, RCEnabled*/
278 bool fR0Enabled = false;
279 bool fRCEnabled = false;
280 if ( (pReg->fFlags & (PDM_DEVREG_FLAGS_R0 | PDM_DEVREG_FLAGS_RC))
281#ifdef VBOX_WITH_PGM_NEM_MODE
282 && !PGMR3IsNemModeEnabled(pVM) /* No ring-0 in simplified memory mode. */
283#endif
284 && !SUPR3IsDriverless())
285 {
286 if (pReg->fFlags & PDM_DEVREG_FLAGS_R0)
287 {
288 if (pReg->fFlags & PDM_DEVREG_FLAGS_REQUIRE_R0)
289 fR0Enabled = true;
290 else
291 {
292 rc = CFGMR3QueryBoolDef(paDevs[i].pNode, "R0Enabled", &fR0Enabled,
293 !(pReg->fFlags & PDM_DEVREG_FLAGS_OPT_IN_R0));
294 AssertLogRelRCReturn(rc, rc);
295 }
296 }
297
298 if (pReg->fFlags & PDM_DEVREG_FLAGS_RC)
299 {
300 if (pReg->fFlags & PDM_DEVREG_FLAGS_REQUIRE_RC)
301 fRCEnabled = true;
302 else
303 {
304 rc = CFGMR3QueryBoolDef(paDevs[i].pNode, "RCEnabled", &fRCEnabled,
305 !(pReg->fFlags & PDM_DEVREG_FLAGS_OPT_IN_RC));
306 AssertLogRelRCReturn(rc, rc);
307 }
308 fRCEnabled = false;
309 }
310 }
311
312#ifdef VBOX_WITH_DBGF_TRACING
313 DBGFTRACEREVTSRC hDbgfTraceEvtSrc = NIL_DBGFTRACEREVTSRC;
314 bool fTracingEnabled = false;
315 bool fGCPhysRwAll = false;
316 rc = CFGMR3QueryBoolDef(paDevs[i].pNode, "TracingEnabled", &fTracingEnabled,
317 false);
318 AssertLogRelRCReturn(rc, rc);
319 if (fTracingEnabled)
320 {
321 rc = CFGMR3QueryBoolDef(paDevs[i].pNode, "TraceAllGstMemRw", &fGCPhysRwAll,
322 false);
323 AssertLogRelRCReturn(rc, rc);
324
325 /* Traced devices need to be trusted for now. */
326 if (fTrusted)
327 {
328 rc = DBGFR3TracerRegisterEvtSrc(pVM, pReg->szName, &hDbgfTraceEvtSrc);
329 AssertLogRelRCReturn(rc, rc);
330 }
331 else
332 AssertMsgFailedReturn(("configuration error: Device tracing needs a trusted device\n"), VERR_INCOMPATIBLE_CONFIG);
333 }
334#endif
335
336 /* config node */
337 PCFGMNODE pConfigNode = CFGMR3GetChild(paDevs[i].pNode, "Config");
338 if (!pConfigNode)
339 {
340 rc = CFGMR3InsertNode(paDevs[i].pNode, "Config", &pConfigNode);
341 if (RT_FAILURE(rc))
342 {
343 AssertMsgFailed(("Failed to create Config node! rc=%Rrc\n", rc));
344 return rc;
345 }
346 }
347 CFGMR3SetRestrictedRoot(pConfigNode);
348
349 /*
350 * Allocate the device instance and critical section.
351 */
352 AssertLogRelReturn(paDevs[i].pDev->cInstances < pReg->cMaxInstances,
353 VERR_PDM_TOO_MANY_DEVICE_INSTANCES);
354 PPDMDEVINS pDevIns;
355 PPDMCRITSECT pCritSect;
356 if (fR0Enabled || fRCEnabled)
357 {
358 AssertLogRel(fR0Enabled /* not possible to just enabled raw-mode atm. */);
359
360 rc = PDMR3LdrLoadR0(pVM->pUVM, pReg->pszR0Mod, paDevs[i].pDev->pszR0SearchPath);
361 if (RT_FAILURE(rc))
362 return VMR3SetError(pVM->pUVM, rc, RT_SRC_POS, "Failed to load ring-0 module '%s' for device '%s'",
363 pReg->pszR0Mod, pReg->szName);
364
365 PDMDEVICECREATEREQ Req;
366 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
367 Req.Hdr.cbReq = sizeof(Req);
368 Req.pDevInsR3 = NULL;
369 /** @todo Add tracer id in request so R0 can set up DEVINSR0 properly. */
370 Req.fFlags = pReg->fFlags;
371 Req.fClass = pReg->fClass;
372 Req.cMaxInstances = pReg->cMaxInstances;
373 Req.uSharedVersion = pReg->uSharedVersion;
374 Req.cbInstanceShared = pReg->cbInstanceShared;
375 Req.cbInstanceR3 = pReg->cbInstanceCC;
376 Req.cbInstanceRC = pReg->cbInstanceRC;
377 Req.cMaxPciDevices = pReg->cMaxPciDevices;
378 Req.cMaxMsixVectors = pReg->cMaxMsixVectors;
379 Req.iInstance = paDevs[i].iInstance;
380 Req.fRCEnabled = fRCEnabled;
381 Req.afReserved[0] = false;
382 Req.afReserved[1] = false;
383 Req.afReserved[2] = false;
384#ifdef VBOX_WITH_DBGF_TRACING
385 Req.hDbgfTracerEvtSrc = hDbgfTraceEvtSrc;
386#else
387 Req.hDbgfTracerEvtSrc = NIL_DBGFTRACEREVTSRC;
388#endif
389 rc = RTStrCopy(Req.szDevName, sizeof(Req.szDevName), pReg->szName);
390 AssertLogRelRCReturn(rc, rc);
391 rc = RTStrCopy(Req.szModName, sizeof(Req.szModName), pReg->pszR0Mod);
392 AssertLogRelRCReturn(rc, rc);
393 rc = VMMR3CallR0Emt(pVM, pVM->apCpusR3[0], VMMR0_DO_PDM_DEVICE_CREATE, 0, &Req.Hdr);
394 AssertLogRelMsgRCReturn(rc, ("VMMR0_DO_PDM_DEVICE_CREATE for %s failed: %Rrc\n", pReg->szName, rc), rc);
395 pDevIns = Req.pDevInsR3;
396 pCritSect = pDevIns->pCritSectRoR3;
397 Assert(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_R0_ENABLED);
398 }
399 else
400 {
401 /* The code in this else branch works by the same rules as the PDMR0Device.cpp
402 code, except there is only the ring-3 components of the device instance.
403 Changes here may need to be reflected in PDMR0DEvice.cpp and vice versa! */
404 uint32_t cb = RT_UOFFSETOF_DYN(PDMDEVINS, achInstanceData[pReg->cbInstanceCC]);
405 cb = RT_ALIGN_32(cb, 64);
406 uint32_t const offShared = cb;
407 cb += RT_ALIGN_32(pReg->cbInstanceShared, 64);
408 uint32_t const cbCritSect = RT_ALIGN_32(sizeof(*pCritSect), 64);
409 cb += cbCritSect;
410 uint32_t const cbMsixState = RT_ALIGN_32(pReg->cMaxMsixVectors * 16 + (pReg->cMaxMsixVectors + 7) / 8, _4K);
411 uint32_t const cbPciDev = RT_ALIGN_32(RT_UOFFSETOF_DYN(PDMPCIDEV, abMsixState[cbMsixState]), 64);
412 uint32_t const cPciDevs = RT_MIN(pReg->cMaxPciDevices, 1024);
413 uint32_t const cbPciDevs = cbPciDev * cPciDevs;
414 cb += cbPciDevs;
415 AssertLogRelMsgReturn(cb <= PDM_MAX_DEVICE_INSTANCE_SIZE_R3,
416 ("Device %s total instance size is to big: %u, max %u\n",
417 pReg->szName, cb, PDM_MAX_DEVICE_INSTANCE_SIZE_R3),
418 VERR_ALLOCATION_TOO_BIG);
419
420#if 0 /* Several devices demands cacheline aligned data, if not page aligned. Real problem in NEM mode. */
421 rc = MMR3HeapAllocZEx(pVM, MM_TAG_PDM_DEVICE, cb, (void **)&pDevIns);
422 AssertLogRelMsgRCReturn(rc, ("Failed to allocate %zu bytes of instance data for device '%s'. rc=%Rrc\n",
423 cb, pReg->szName, rc), rc);
424#else
425 pDevIns = (PPDMDEVINS)RTMemPageAllocZ(cb);
426 AssertLogRelMsgReturn(pDevIns, ("Failed to allocate %zu bytes of instance data for device '%s'\n", cb, pReg->szName),
427 VERR_NO_PAGE_MEMORY);
428#endif
429
430 /* Initialize it: */
431 pDevIns->u32Version = PDM_DEVINSR3_VERSION;
432 pDevIns->iInstance = paDevs[i].iInstance;
433 pDevIns->cbRing3 = cb;
434 //pDevIns->fR0Enabled = false;
435 //pDevIns->fRCEnabled = false;
436 pDevIns->pvInstanceDataR3 = (uint8_t *)pDevIns + offShared;
437 pDevIns->pvInstanceDataForR3 = &pDevIns->achInstanceData[0];
438 pCritSect = (PPDMCRITSECT)((uint8_t *)pDevIns + offShared + RT_ALIGN_32(pReg->cbInstanceShared, 64));
439 pDevIns->pCritSectRoR3 = pCritSect;
440 pDevIns->cbPciDev = cbPciDev;
441 pDevIns->cPciDevs = cPciDevs;
442 for (uint32_t iPciDev = 0; iPciDev < cPciDevs; iPciDev++)
443 {
444 PPDMPCIDEV pPciDev = (PPDMPCIDEV)((uint8_t *)pDevIns->pCritSectRoR3 + cbCritSect + cbPciDev * iPciDev);
445 if (iPciDev < RT_ELEMENTS(pDevIns->apPciDevs))
446 pDevIns->apPciDevs[iPciDev] = pPciDev;
447 pPciDev->cbConfig = _4K;
448 pPciDev->cbMsixState = cbMsixState;
449 pPciDev->idxSubDev = (uint16_t)iPciDev;
450 pPciDev->Int.s.idxSubDev = (uint16_t)iPciDev;
451 pPciDev->u32Magic = PDMPCIDEV_MAGIC;
452 }
453 }
454
455 pDevIns->pHlpR3 = fTrusted ? &g_pdmR3DevHlpTrusted : &g_pdmR3DevHlpUnTrusted;
456 pDevIns->pReg = pReg;
457 pDevIns->pCfg = pConfigNode;
458 //pDevIns->IBase.pfnQueryInterface = NULL;
459 //pDevIns->fTracing = 0;
460 pDevIns->idTracing = ++pVM->pdm.s.idTracingDev;
461
462 //pDevIns->Internal.s.pNextR3 = NULL;
463 //pDevIns->Internal.s.pPerDeviceNextR3 = NULL;
464 pDevIns->Internal.s.pDevR3 = paDevs[i].pDev;
465 //pDevIns->Internal.s.pLunsR3 = NULL;
466 //pDevIns->Internal.s.pfnAsyncNotify = NULL;
467 pDevIns->Internal.s.pCfgHandle = paDevs[i].pNode;
468 pDevIns->Internal.s.pVMR3 = pVM;
469#ifdef VBOX_WITH_DBGF_TRACING
470 pDevIns->Internal.s.hDbgfTraceEvtSrc = hDbgfTraceEvtSrc;
471#else
472 pDevIns->Internal.s.hDbgfTraceEvtSrc = NIL_DBGFTRACEREVTSRC;
473#endif
474 //pDevIns->Internal.s.pHeadPciDevR3 = NULL;
475 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_SUSPENDED;
476 //pDevIns->Internal.s.uLastIrqTag = 0;
477
478 rc = pdmR3CritSectInitDeviceAuto(pVM, pDevIns, pCritSect, RT_SRC_POS,
479 "%s#%uAuto", pDevIns->pReg->szName, pDevIns->iInstance);
480 AssertLogRelRCReturn(rc, rc);
481
482 /*
483 * Link it into all the lists.
484 */
485 /* The global instance FIFO. */
486 PPDMDEVINS pPrev1 = pVM->pdm.s.pDevInstances;
487 if (!pPrev1)
488 pVM->pdm.s.pDevInstances = pDevIns;
489 else
490 {
491 while (pPrev1->Internal.s.pNextR3)
492 pPrev1 = pPrev1->Internal.s.pNextR3;
493 pPrev1->Internal.s.pNextR3 = pDevIns;
494 }
495
496 /* The per device instance FIFO. */
497 PPDMDEVINS pPrev2 = paDevs[i].pDev->pInstances;
498 if (!pPrev2)
499 paDevs[i].pDev->pInstances = pDevIns;
500 else
501 {
502 while (pPrev2->Internal.s.pPerDeviceNextR3)
503 pPrev2 = pPrev2->Internal.s.pPerDeviceNextR3;
504 pPrev2->Internal.s.pPerDeviceNextR3 = pDevIns;
505 }
506
507#ifdef VBOX_WITH_DBGF_TRACING
508 /*
509 * Allocate memory for the MMIO/IO port registration tracking if DBGF tracing is enabled.
510 */
511 if (hDbgfTraceEvtSrc != NIL_DBGFTRACEREVTSRC)
512 {
513 pDevIns->Internal.s.paDbgfTraceTrack = (PPDMDEVINSDBGFTRACK)RTMemAllocZ(PDM_MAX_DEVICE_DBGF_TRACING_TRACK);
514 if (!pDevIns->Internal.s.paDbgfTraceTrack)
515 {
516 LogRel(("PDM: Failed to construct '%s'/%d! %Rra\n", pDevIns->pReg->szName, pDevIns->iInstance, VERR_NO_MEMORY));
517 if (VMR3GetErrorCount(pVM->pUVM) == 0)
518 VMSetError(pVM, rc, RT_SRC_POS, "Failed to construct device '%s' instance #%u",
519 pDevIns->pReg->szName, pDevIns->iInstance);
520 paDevs[i].pDev->cInstances--;
521 return VERR_NO_MEMORY;
522 }
523
524 pDevIns->Internal.s.idxDbgfTraceTrackNext = 0;
525 pDevIns->Internal.s.cDbgfTraceTrackMax = PDM_MAX_DEVICE_DBGF_TRACING_TRACK / sizeof(PDMDEVINSDBGFTRACK);
526 pDevIns->pHlpR3 = &g_pdmR3DevHlpTracing;
527 }
528#endif
529
530 /*
531 * Call the constructor.
532 */
533 paDevs[i].pDev->cInstances++;
534 Log(("PDM: Constructing device '%s' instance %d...\n", pDevIns->pReg->szName, pDevIns->iInstance));
535 rc = pDevIns->pReg->pfnConstruct(pDevIns, pDevIns->iInstance, pDevIns->pCfg);
536 if (RT_FAILURE(rc))
537 {
538 LogRel(("PDM: Failed to construct '%s'/%d! %Rra\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
539 if (VMR3GetErrorCount(pVM->pUVM) == 0)
540 VMSetError(pVM, rc, RT_SRC_POS, "Failed to construct device '%s' instance #%u",
541 pDevIns->pReg->szName, pDevIns->iInstance);
542 /* Because we're damn lazy, the destructor will be called even if
543 the constructor fails. So, no unlinking. */
544 paDevs[i].pDev->cInstances--;
545 return rc == VERR_VERSION_MISMATCH ? VERR_PDM_DEVICE_VERSION_MISMATCH : rc;
546 }
547
548 /*
549 * Call the ring-0 constructor if applicable.
550 */
551 if (fR0Enabled)
552 {
553 PDMDEVICEGENCALLREQ Req;
554 RT_ZERO(Req.Params);
555 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
556 Req.Hdr.cbReq = sizeof(Req);
557 Req.enmCall = PDMDEVICEGENCALL_CONSTRUCT;
558 Req.idxR0Device = pDevIns->Internal.s.idxR0Device;
559 Req.pDevInsR3 = pDevIns;
560 rc = VMMR3CallR0Emt(pVM, pVM->apCpusR3[0], VMMR0_DO_PDM_DEVICE_GEN_CALL, 0, &Req.Hdr);
561 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_R0_CONTRUCT;
562 if (RT_FAILURE(rc))
563 {
564 LogRel(("PDM: Failed to construct (ring-0) '%s'/%d! %Rra\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
565 if (VMR3GetErrorCount(pVM->pUVM) == 0)
566 VMSetError(pVM, rc, RT_SRC_POS, "The ring-0 constructor of device '%s' instance #%u failed",
567 pDevIns->pReg->szName, pDevIns->iInstance);
568 paDevs[i].pDev->cInstances--;
569 return rc == VERR_VERSION_MISMATCH ? VERR_PDM_DEVICE_VERSION_MISMATCH : rc;
570 }
571 }
572
573 } /* for device instances */
574
575#ifdef VBOX_WITH_USB
576 /* ditto for USB Devices. */
577 rc = pdmR3UsbInstantiateDevices(pVM);
578 if (RT_FAILURE(rc))
579 return rc;
580#endif
581
582 LogFlow(("pdmR3DevInit: returns %Rrc\n", VINF_SUCCESS));
583 return VINF_SUCCESS;
584}
585
586
587/**
588 * Performs the init complete callback after ring-0 and raw-mode has been
589 * initialized.
590 *
591 * @returns VBox status code.
592 * @param pVM The cross context VM structure.
593 */
594int pdmR3DevInitComplete(PVM pVM)
595{
596 int rc;
597
598 /*
599 * Iterate thru the device instances and work the callback.
600 */
601 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
602 {
603 if (pDevIns->pReg->pfnInitComplete)
604 {
605 PDMCritSectEnter(pVM, pDevIns->pCritSectRoR3, VERR_IGNORED);
606 rc = pDevIns->pReg->pfnInitComplete(pDevIns);
607 PDMCritSectLeave(pVM, pDevIns->pCritSectRoR3);
608 if (RT_FAILURE(rc))
609 {
610 AssertMsgFailed(("InitComplete on device '%s'/%d failed with rc=%Rrc\n",
611 pDevIns->pReg->szName, pDevIns->iInstance, rc));
612 return rc;
613 }
614 }
615 }
616
617#ifdef VBOX_WITH_USB
618 rc = pdmR3UsbVMInitComplete(pVM);
619 if (RT_FAILURE(rc))
620 {
621 Log(("pdmR3DevInit: returns %Rrc\n", rc));
622 return rc;
623 }
624#endif
625
626 LogFlow(("pdmR3DevInit: returns %Rrc\n", VINF_SUCCESS));
627 return VINF_SUCCESS;
628}
629
630
631/**
632 * Lookups a device structure by name.
633 * @internal
634 */
635PPDMDEV pdmR3DevLookup(PVM pVM, const char *pszName)
636{
637 size_t cchName = strlen(pszName);
638 for (PPDMDEV pDev = pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
639 if ( pDev->cchName == cchName
640 && !strcmp(pDev->pReg->szName, pszName))
641 return pDev;
642 return NULL;
643}
644
645
646/**
647 * Loads the device modules.
648 *
649 * @returns VBox status code.
650 * @param pVM The cross context VM structure.
651 */
652static int pdmR3DevLoadModules(PVM pVM)
653{
654 /*
655 * Initialize the callback structure.
656 */
657 PDMDEVREGCBINT RegCB;
658 RegCB.Core.u32Version = PDM_DEVREG_CB_VERSION;
659 RegCB.Core.pfnRegister = pdmR3DevReg_Register;
660 RegCB.pVM = pVM;
661 RegCB.pCfgNode = NULL;
662
663 /*
664 * Register the internal VMM APIC device.
665 */
666 int rc = pdmR3DevReg_Register(&RegCB.Core, &g_DeviceAPIC);
667 AssertRCReturn(rc, rc);
668
669 /*
670 * Load the builtin module.
671 */
672 PCFGMNODE pDevicesNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "PDM/Devices");
673 bool fLoadBuiltin;
674 rc = CFGMR3QueryBool(pDevicesNode, "LoadBuiltin", &fLoadBuiltin);
675 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
676 fLoadBuiltin = true;
677 else if (RT_FAILURE(rc))
678 {
679 AssertMsgFailed(("Configuration error: Querying boolean \"LoadBuiltin\" failed with %Rrc\n", rc));
680 return rc;
681 }
682 if (fLoadBuiltin)
683 {
684 /* make filename */
685 char *pszFilename = pdmR3FileR3("VBoxDD", true /*fShared*/);
686 if (!pszFilename)
687 return VERR_NO_TMP_MEMORY;
688 rc = pdmR3DevLoad(pVM, &RegCB, pszFilename, "VBoxDD");
689 RTMemTmpFree(pszFilename);
690 if (RT_FAILURE(rc))
691 return rc;
692
693 /* make filename */
694 pszFilename = pdmR3FileR3("VBoxDD2", true /*fShared*/);
695 if (!pszFilename)
696 return VERR_NO_TMP_MEMORY;
697 rc = pdmR3DevLoad(pVM, &RegCB, pszFilename, "VBoxDD2");
698 RTMemTmpFree(pszFilename);
699 if (RT_FAILURE(rc))
700 return rc;
701 }
702
703 /*
704 * Load additional device modules.
705 */
706 PCFGMNODE pCur;
707 for (pCur = CFGMR3GetFirstChild(pDevicesNode); pCur; pCur = CFGMR3GetNextChild(pCur))
708 {
709 /*
710 * Get the name and path.
711 */
712 char szName[PDMMOD_NAME_LEN];
713 rc = CFGMR3GetName(pCur, &szName[0], sizeof(szName));
714 if (rc == VERR_CFGM_NOT_ENOUGH_SPACE)
715 {
716 AssertMsgFailed(("configuration error: The module name is too long, cchName=%zu.\n", CFGMR3GetNameLen(pCur)));
717 return VERR_PDM_MODULE_NAME_TOO_LONG;
718 }
719 else if (RT_FAILURE(rc))
720 {
721 AssertMsgFailed(("CFGMR3GetName -> %Rrc.\n", rc));
722 return rc;
723 }
724
725 /* the path is optional, if no path the module name + path is used. */
726 char szFilename[RTPATH_MAX];
727 rc = CFGMR3QueryString(pCur, "Path", &szFilename[0], sizeof(szFilename));
728 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
729 strcpy(szFilename, szName);
730 else if (RT_FAILURE(rc))
731 {
732 AssertMsgFailed(("configuration error: Failure to query the module path, rc=%Rrc.\n", rc));
733 return rc;
734 }
735
736 /* prepend path? */
737 if (!RTPathHavePath(szFilename))
738 {
739 char *psz = pdmR3FileR3(szFilename, false /*fShared*/);
740 if (!psz)
741 return VERR_NO_TMP_MEMORY;
742 size_t cch = strlen(psz) + 1;
743 if (cch > sizeof(szFilename))
744 {
745 RTMemTmpFree(psz);
746 AssertMsgFailed(("Filename too long! cch=%d '%s'\n", cch, psz));
747 return VERR_FILENAME_TOO_LONG;
748 }
749 memcpy(szFilename, psz, cch);
750 RTMemTmpFree(psz);
751 }
752
753 /*
754 * Load the module and register it's devices.
755 */
756 RegCB.pCfgNode = pCur;
757 rc = pdmR3DevLoad(pVM, &RegCB, szFilename, szName);
758 if (RT_FAILURE(rc))
759 return rc;
760 }
761
762 return VINF_SUCCESS;
763}
764
765
766/**
767 * Loads one device module and call the registration entry point.
768 *
769 * @returns VBox status code.
770 * @param pVM The cross context VM structure.
771 * @param pRegCB The registration callback stuff.
772 * @param pszFilename Module filename.
773 * @param pszName Module name.
774 */
775static int pdmR3DevLoad(PVM pVM, PPDMDEVREGCBINT pRegCB, const char *pszFilename, const char *pszName)
776{
777 /*
778 * Load it.
779 */
780 int rc = pdmR3LoadR3U(pVM->pUVM, pszFilename, pszName);
781 if (RT_SUCCESS(rc))
782 {
783 /*
784 * Get the registration export and call it.
785 */
786 FNPDMVBOXDEVICESREGISTER *pfnVBoxDevicesRegister;
787 rc = PDMR3LdrGetSymbolR3(pVM, pszName, "VBoxDevicesRegister", (void **)&pfnVBoxDevicesRegister);
788 if (RT_SUCCESS(rc))
789 {
790 Log(("PDM: Calling VBoxDevicesRegister (%p) of %s (%s)\n", pfnVBoxDevicesRegister, pszName, pszFilename));
791 rc = pfnVBoxDevicesRegister(&pRegCB->Core, VBOX_VERSION);
792 if (RT_SUCCESS(rc))
793 Log(("PDM: Successfully loaded device module %s (%s).\n", pszName, pszFilename));
794 else
795 {
796 VMR3SetError(pVM->pUVM, rc, RT_SRC_POS, "VBoxDevicesRegister failed with rc=%Rrc for module %s (%s)",
797 rc, pszName, pszFilename);
798 AssertMsgFailed(("VBoxDevicesRegister failed with rc=%Rrc for module %s (%s)\n", rc, pszName, pszFilename));
799 }
800 }
801 else
802 {
803 AssertMsgFailed(("Failed to locate 'VBoxDevicesRegister' in %s (%s) rc=%Rrc\n", pszName, pszFilename, rc));
804 if (rc == VERR_SYMBOL_NOT_FOUND)
805 rc = VERR_PDM_NO_REGISTRATION_EXPORT;
806 VMR3SetError(pVM->pUVM, rc, RT_SRC_POS, "Failed to locate 'VBoxDevicesRegister' in %s (%s) rc=%Rrc",
807 pszName, pszFilename, rc);
808 }
809 }
810 else
811 AssertMsgFailed(("Failed to load %s %s!\n", pszFilename, pszName));
812 return rc;
813}
814
815
816/**
817 * @interface_method_impl{PDMDEVREGCB,pfnRegister}
818 */
819static DECLCALLBACK(int) pdmR3DevReg_Register(PPDMDEVREGCB pCallbacks, PCPDMDEVREG pReg)
820{
821 /*
822 * Validate the registration structure.
823 */
824 Assert(pReg);
825 AssertMsgReturn(pReg->u32Version == PDM_DEVREG_VERSION,
826 ("Unknown struct version %#x!\n", pReg->u32Version),
827 VERR_PDM_UNKNOWN_DEVREG_VERSION);
828
829 AssertMsgReturn( pReg->szName[0]
830 && strlen(pReg->szName) < sizeof(pReg->szName)
831 && pdmR3IsValidName(pReg->szName),
832 ("Invalid name '%.*s'\n", sizeof(pReg->szName), pReg->szName),
833 VERR_PDM_INVALID_DEVICE_REGISTRATION);
834 AssertMsgReturn( !(pReg->fFlags & PDM_DEVREG_FLAGS_RC)
835 || ( pReg->pszRCMod[0]
836 && strlen(pReg->pszRCMod) < RT_SIZEOFMEMB(PDMDEVICECREATEREQ, szModName)),
837 ("Invalid GC module name '%s' - (Device %s)\n", pReg->pszRCMod, pReg->szName),
838 VERR_PDM_INVALID_DEVICE_REGISTRATION);
839 AssertMsgReturn( !(pReg->fFlags & PDM_DEVREG_FLAGS_R0)
840 || ( pReg->pszR0Mod[0]
841 && strlen(pReg->pszR0Mod) < RT_SIZEOFMEMB(PDMDEVICECREATEREQ, szModName)),
842 ("Invalid R0 module name '%s' - (Device %s)\n", pReg->pszR0Mod, pReg->szName),
843 VERR_PDM_INVALID_DEVICE_REGISTRATION);
844 AssertMsgReturn((pReg->fFlags & PDM_DEVREG_FLAGS_HOST_BITS_MASK) == PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT,
845 ("Invalid host bits flags! fFlags=%#x (Device %s)\n", pReg->fFlags, pReg->szName),
846 VERR_PDM_INVALID_DEVICE_HOST_BITS);
847 AssertMsgReturn((pReg->fFlags & PDM_DEVREG_FLAGS_GUEST_BITS_MASK),
848 ("Invalid guest bits flags! fFlags=%#x (Device %s)\n", pReg->fFlags, pReg->szName),
849 VERR_PDM_INVALID_DEVICE_REGISTRATION);
850 AssertMsgReturn(pReg->fClass,
851 ("No class! (Device %s)\n", pReg->szName),
852 VERR_PDM_INVALID_DEVICE_REGISTRATION);
853 AssertMsgReturn(pReg->cMaxInstances > 0,
854 ("Max instances %u! (Device %s)\n", pReg->cMaxInstances, pReg->szName),
855 VERR_PDM_INVALID_DEVICE_REGISTRATION);
856 uint32_t const cbMaxInstance = pReg->fFlags & (PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0)
857 ? PDM_MAX_DEVICE_INSTANCE_SIZE : PDM_MAX_DEVICE_INSTANCE_SIZE_R3;
858 AssertMsgReturn(pReg->cbInstanceShared <= cbMaxInstance,
859 ("Instance size %u bytes! (Max %u; Device %s)\n", pReg->cbInstanceShared, cbMaxInstance, pReg->szName),
860 VERR_PDM_INVALID_DEVICE_REGISTRATION);
861 AssertMsgReturn(pReg->cbInstanceCC <= cbMaxInstance,
862 ("Instance size %d bytes! (Max %u; Device %s)\n", pReg->cbInstanceCC, cbMaxInstance, pReg->szName),
863 VERR_PDM_INVALID_DEVICE_REGISTRATION);
864 AssertMsgReturn(pReg->pfnConstruct,
865 ("No constructor! (Device %s)\n", pReg->szName),
866 VERR_PDM_INVALID_DEVICE_REGISTRATION);
867 AssertLogRelMsgReturn((pReg->fFlags & PDM_DEVREG_FLAGS_GUEST_BITS_MASK) == PDM_DEVREG_FLAGS_GUEST_BITS_DEFAULT,
868 ("PDM: Rejected device '%s' because it didn't match the guest bits.\n", pReg->szName),
869 VERR_PDM_INVALID_DEVICE_GUEST_BITS);
870 AssertLogRelMsg(pReg->u32VersionEnd == PDM_DEVREG_VERSION,
871 ("u32VersionEnd=%#x, expected %#x. (szName=%s)\n",
872 pReg->u32VersionEnd, PDM_DEVREG_VERSION, pReg->szName));
873 AssertLogRelMsgReturn(pReg->cMaxPciDevices <= 8, ("%#x (szName=%s)\n", pReg->cMaxPciDevices, pReg->szName),
874 VERR_PDM_INVALID_DEVICE_REGISTRATION);
875 AssertLogRelMsgReturn(pReg->cMaxMsixVectors <= VBOX_MSIX_MAX_ENTRIES,
876 ("%#x (szName=%s)\n", pReg->cMaxMsixVectors, pReg->szName),
877 VERR_PDM_INVALID_DEVICE_REGISTRATION);
878 AssertLogRelMsgReturn(pReg->fFlags & PDM_DEVREG_FLAGS_NEW_STYLE /* the flag is required now */,
879 ("PDM_DEVREG_FLAGS_NEW_STYLE not set for szName=%s!\n", pReg->szName),
880 VERR_PDM_INVALID_DEVICE_REGISTRATION);
881
882 /*
883 * Check for duplicate and find FIFO entry at the same time.
884 */
885 PCPDMDEVREGCBINT pRegCB = (PCPDMDEVREGCBINT)pCallbacks;
886 PPDMDEV pDevPrev = NULL;
887 PPDMDEV pDev = pRegCB->pVM->pdm.s.pDevs;
888 for (; pDev; pDevPrev = pDev, pDev = pDev->pNext)
889 AssertMsgReturn(strcmp(pDev->pReg->szName, pReg->szName),
890 ("Device '%s' already exists\n", pReg->szName),
891 VERR_PDM_DEVICE_NAME_CLASH);
892
893 /*
894 * Allocate new device structure, initialize and insert it into the list.
895 */
896 int rc;
897 pDev = (PPDMDEV)MMR3HeapAlloc(pRegCB->pVM, MM_TAG_PDM_DEVICE, sizeof(*pDev));
898 if (pDev)
899 {
900 pDev->pNext = NULL;
901 pDev->cInstances = 0;
902 pDev->pInstances = NULL;
903 pDev->pReg = pReg;
904 pDev->cchName = (uint32_t)strlen(pReg->szName);
905 rc = CFGMR3QueryStringAllocDef( pRegCB->pCfgNode, "RCSearchPath", &pDev->pszRCSearchPath, NULL);
906 if (RT_SUCCESS(rc))
907 rc = CFGMR3QueryStringAllocDef(pRegCB->pCfgNode, "R0SearchPath", &pDev->pszR0SearchPath, NULL);
908 if (RT_SUCCESS(rc))
909 {
910 if (pDevPrev)
911 pDevPrev->pNext = pDev;
912 else
913 pRegCB->pVM->pdm.s.pDevs = pDev;
914 Log(("PDM: Registered device '%s'\n", pReg->szName));
915 return VINF_SUCCESS;
916 }
917
918 MMR3HeapFree(pDev);
919 }
920 else
921 rc = VERR_NO_MEMORY;
922 return rc;
923}
924
925
926/**
927 * Locates a LUN.
928 *
929 * @returns VBox status code.
930 * @param pVM The cross context VM structure.
931 * @param pszDevice Device name.
932 * @param iInstance Device instance.
933 * @param iLun The Logical Unit to obtain the interface of.
934 * @param ppLun Where to store the pointer to the LUN if found.
935 * @thread Try only do this in EMT...
936 */
937int pdmR3DevFindLun(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPPDMLUN ppLun)
938{
939 /*
940 * Iterate registered devices looking for the device.
941 */
942 size_t cchDevice = strlen(pszDevice);
943 for (PPDMDEV pDev = pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
944 {
945 if ( pDev->cchName == cchDevice
946 && !memcmp(pDev->pReg->szName, pszDevice, cchDevice))
947 {
948 /*
949 * Iterate device instances.
950 */
951 for (PPDMDEVINS pDevIns = pDev->pInstances; pDevIns; pDevIns = pDevIns->Internal.s.pPerDeviceNextR3)
952 {
953 if (pDevIns->iInstance == iInstance)
954 {
955 /*
956 * Iterate luns.
957 */
958 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
959 {
960 if (pLun->iLun == iLun)
961 {
962 *ppLun = pLun;
963 return VINF_SUCCESS;
964 }
965 }
966 return VERR_PDM_LUN_NOT_FOUND;
967 }
968 }
969 return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND;
970 }
971 }
972 return VERR_PDM_DEVICE_NOT_FOUND;
973}
974
975
976/**
977 * Attaches a preconfigured driver to an existing device instance.
978 *
979 * This is used to change drivers and suchlike at runtime.
980 *
981 * @returns VBox status code.
982 * @param pUVM The user mode VM handle.
983 * @param pszDevice Device name.
984 * @param iInstance Device instance.
985 * @param iLun The Logical Unit to obtain the interface of.
986 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
987 * @param ppBase Where to store the base interface pointer. Optional.
988 * @thread EMT
989 */
990VMMR3DECL(int) PDMR3DeviceAttach(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, uint32_t fFlags, PPPDMIBASE ppBase)
991{
992 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
993 PVM pVM = pUVM->pVM;
994 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
995 VM_ASSERT_EMT(pVM);
996 LogFlow(("PDMR3DeviceAttach: pszDevice=%p:{%s} iInstance=%d iLun=%d fFlags=%#x ppBase=%p\n",
997 pszDevice, pszDevice, iInstance, iLun, fFlags, ppBase));
998
999 /*
1000 * Find the LUN in question.
1001 */
1002 PPDMLUN pLun;
1003 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
1004 if (RT_SUCCESS(rc))
1005 {
1006 /*
1007 * Can we attach anything at runtime?
1008 */
1009 PPDMDEVINS pDevIns = pLun->pDevIns;
1010 if (pDevIns->pReg->pfnAttach)
1011 {
1012 if (!pLun->pTop)
1013 {
1014 PDMCritSectEnter(pVM, pDevIns->pCritSectRoR3, VERR_IGNORED);
1015 rc = pDevIns->pReg->pfnAttach(pDevIns, iLun, fFlags);
1016 PDMCritSectLeave(pVM, pDevIns->pCritSectRoR3);
1017 }
1018 else
1019 rc = VERR_PDM_DRIVER_ALREADY_ATTACHED;
1020 }
1021 else
1022 rc = VERR_PDM_DEVICE_NO_RT_ATTACH;
1023
1024 if (ppBase)
1025 *ppBase = pLun->pTop ? &pLun->pTop->IBase : NULL;
1026 }
1027 else if (ppBase)
1028 *ppBase = NULL;
1029
1030 if (ppBase)
1031 LogFlow(("PDMR3DeviceAttach: returns %Rrc *ppBase=%p\n", rc, *ppBase));
1032 else
1033 LogFlow(("PDMR3DeviceAttach: returns %Rrc\n", rc));
1034 return rc;
1035}
1036
1037
1038/**
1039 * Detaches a driver chain from an existing device instance.
1040 *
1041 * This is used to change drivers and suchlike at runtime.
1042 *
1043 * @returns VBox status code.
1044 * @param pUVM The user mode VM handle.
1045 * @param pszDevice Device name.
1046 * @param iInstance Device instance.
1047 * @param iLun The Logical Unit to obtain the interface of.
1048 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
1049 * @thread EMT
1050 */
1051VMMR3DECL(int) PDMR3DeviceDetach(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, uint32_t fFlags)
1052{
1053 return PDMR3DriverDetach(pUVM, pszDevice, iInstance, iLun, NULL, 0, fFlags);
1054}
1055
1056
1057/**
1058 * References the critical section associated with a device for the use by a
1059 * timer or similar created by the device.
1060 *
1061 * @returns Pointer to the critical section.
1062 * @param pVM The cross context VM structure.
1063 * @param pDevIns The device instance in question.
1064 *
1065 * @internal
1066 */
1067VMMR3_INT_DECL(PPDMCRITSECT) PDMR3DevGetCritSect(PVM pVM, PPDMDEVINS pDevIns)
1068{
1069 VM_ASSERT_EMT(pVM); RT_NOREF_PV(pVM);
1070 VM_ASSERT_STATE(pVM, VMSTATE_CREATING);
1071 AssertPtr(pDevIns);
1072
1073 PPDMCRITSECT pCritSect = pDevIns->pCritSectRoR3;
1074 AssertPtr(pCritSect);
1075 pCritSect->s.fUsedByTimerOrSimilar = true;
1076
1077 return pCritSect;
1078}
1079
1080
1081/**
1082 * Attaches a preconfigured driver to an existing device or driver instance.
1083 *
1084 * This is used to change drivers and suchlike at runtime. The driver or device
1085 * at the end of the chain will be told to attach to whatever is configured
1086 * below it.
1087 *
1088 * @returns VBox status code.
1089 * @param pUVM The user mode VM handle.
1090 * @param pszDevice Device name.
1091 * @param iInstance Device instance.
1092 * @param iLun The Logical Unit to obtain the interface of.
1093 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
1094 * @param ppBase Where to store the base interface pointer. Optional.
1095 *
1096 * @thread EMT
1097 */
1098VMMR3DECL(int) PDMR3DriverAttach(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, uint32_t fFlags, PPPDMIBASE ppBase)
1099{
1100 LogFlow(("PDMR3DriverAttach: pszDevice=%p:{%s} iInstance=%d iLun=%d fFlags=%#x ppBase=%p\n",
1101 pszDevice, pszDevice, iInstance, iLun, fFlags, ppBase));
1102 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1103 PVM pVM = pUVM->pVM;
1104 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1105 VM_ASSERT_EMT(pVM);
1106
1107 if (ppBase)
1108 *ppBase = NULL;
1109
1110 /*
1111 * Find the LUN in question.
1112 */
1113 PPDMLUN pLun;
1114 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
1115 if (RT_SUCCESS(rc))
1116 {
1117 /*
1118 * Anything attached to the LUN?
1119 */
1120 PPDMDRVINS pDrvIns = pLun->pTop;
1121 if (!pDrvIns)
1122 {
1123 /* No, ask the device to attach to the new stuff. */
1124 PPDMDEVINS pDevIns = pLun->pDevIns;
1125 if (pDevIns->pReg->pfnAttach)
1126 {
1127 PDMCritSectEnter(pVM, pDevIns->pCritSectRoR3, VERR_IGNORED);
1128 rc = pDevIns->pReg->pfnAttach(pDevIns, iLun, fFlags);
1129 if (RT_SUCCESS(rc) && ppBase)
1130 *ppBase = pLun->pTop ? &pLun->pTop->IBase : NULL;
1131 PDMCritSectLeave(pVM, pDevIns->pCritSectRoR3);
1132 }
1133 else
1134 rc = VERR_PDM_DEVICE_NO_RT_ATTACH;
1135 }
1136 else
1137 {
1138 /* Yes, find the bottom most driver and ask it to attach to the new stuff. */
1139 while (pDrvIns->Internal.s.pDown)
1140 pDrvIns = pDrvIns->Internal.s.pDown;
1141 if (pDrvIns->pReg->pfnAttach)
1142 {
1143 rc = pDrvIns->pReg->pfnAttach(pDrvIns, fFlags);
1144 if (RT_SUCCESS(rc) && ppBase)
1145 *ppBase = pDrvIns->Internal.s.pDown
1146 ? &pDrvIns->Internal.s.pDown->IBase
1147 : NULL;
1148 }
1149 else
1150 rc = VERR_PDM_DRIVER_NO_RT_ATTACH;
1151 }
1152 }
1153
1154 if (ppBase)
1155 LogFlow(("PDMR3DriverAttach: returns %Rrc *ppBase=%p\n", rc, *ppBase));
1156 else
1157 LogFlow(("PDMR3DriverAttach: returns %Rrc\n", rc));
1158 return rc;
1159}
1160
1161
1162/**
1163 * Detaches the specified driver instance.
1164 *
1165 * This is used to replumb drivers at runtime for simulating hot plugging and
1166 * media changes.
1167 *
1168 * This is a superset of PDMR3DeviceDetach. It allows detaching drivers from
1169 * any driver or device by specifying the driver to start detaching at. The
1170 * only prerequisite is that the driver or device above implements the
1171 * pfnDetach callback (PDMDRVREG / PDMDEVREG).
1172 *
1173 * @returns VBox status code.
1174 * @param pUVM The user mode VM handle.
1175 * @param pszDevice Device name.
1176 * @param iDevIns Device instance.
1177 * @param iLun The Logical Unit in which to look for the driver.
1178 * @param pszDriver The name of the driver which to detach. If NULL
1179 * then the entire driver chain is detatched.
1180 * @param iOccurrence The occurrence of that driver in the chain. This is
1181 * usually 0.
1182 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
1183 * @thread EMT
1184 */
1185VMMR3DECL(int) PDMR3DriverDetach(PUVM pUVM, const char *pszDevice, unsigned iDevIns, unsigned iLun,
1186 const char *pszDriver, unsigned iOccurrence, uint32_t fFlags)
1187{
1188 LogFlow(("PDMR3DriverDetach: pszDevice=%p:{%s} iDevIns=%u iLun=%u pszDriver=%p:{%s} iOccurrence=%u fFlags=%#x\n",
1189 pszDevice, pszDevice, iDevIns, iLun, pszDriver, pszDriver, iOccurrence, fFlags));
1190 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1191 PVM pVM = pUVM->pVM;
1192 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1193 VM_ASSERT_EMT(pVM);
1194 AssertPtr(pszDevice);
1195 AssertPtrNull(pszDriver);
1196 Assert(iOccurrence == 0 || pszDriver);
1197 Assert(!(fFlags & ~(PDM_TACH_FLAGS_NOT_HOT_PLUG)));
1198
1199 /*
1200 * Find the LUN in question.
1201 */
1202 PPDMLUN pLun;
1203 int rc = pdmR3DevFindLun(pVM, pszDevice, iDevIns, iLun, &pLun);
1204 if (RT_SUCCESS(rc))
1205 {
1206 /*
1207 * Locate the driver.
1208 */
1209 PPDMDRVINS pDrvIns = pLun->pTop;
1210 if (pDrvIns)
1211 {
1212 if (pszDriver)
1213 {
1214 while (pDrvIns)
1215 {
1216 if (!strcmp(pDrvIns->pReg->szName, pszDriver))
1217 {
1218 if (iOccurrence == 0)
1219 break;
1220 iOccurrence--;
1221 }
1222 pDrvIns = pDrvIns->Internal.s.pDown;
1223 }
1224 }
1225 if (pDrvIns)
1226 rc = pdmR3DrvDetach(pDrvIns, fFlags);
1227 else
1228 rc = VERR_PDM_DRIVER_INSTANCE_NOT_FOUND;
1229 }
1230 else
1231 rc = VINF_PDM_NO_DRIVER_ATTACHED_TO_LUN;
1232 }
1233
1234 LogFlow(("PDMR3DriverDetach: returns %Rrc\n", rc));
1235 return rc;
1236}
1237
1238
1239/**
1240 * Runtime detach and reattach of a new driver chain or sub chain.
1241 *
1242 * This is intended to be called on a non-EMT thread, this will instantiate the
1243 * new driver (sub-)chain, and then the EMTs will do the actual replumbing. The
1244 * destruction of the old driver chain will be taken care of on the calling
1245 * thread.
1246 *
1247 * @returns VBox status code.
1248 * @param pUVM The user mode VM handle.
1249 * @param pszDevice Device name.
1250 * @param iDevIns Device instance.
1251 * @param iLun The Logical Unit in which to look for the driver.
1252 * @param pszDriver The name of the driver which to detach and replace.
1253 * If NULL then the entire driver chain is to be
1254 * reattached.
1255 * @param iOccurrence The occurrence of that driver in the chain. This is
1256 * usually 0.
1257 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
1258 * @param pCfg The configuration of the new driver chain that is
1259 * going to be attached. The subtree starts with the
1260 * node containing a Driver key, a Config subtree and
1261 * optionally an AttachedDriver subtree.
1262 * If this parameter is NULL, then this call will work
1263 * like at a non-pause version of PDMR3DriverDetach.
1264 * @param ppBase Where to store the base interface pointer to the new
1265 * driver. Optional.
1266 *
1267 * @thread Any thread. The EMTs will be involved at some point though.
1268 */
1269VMMR3DECL(int) PDMR3DriverReattach(PUVM pUVM, const char *pszDevice, unsigned iDevIns, unsigned iLun,
1270 const char *pszDriver, unsigned iOccurrence, uint32_t fFlags,
1271 PCFGMNODE pCfg, PPPDMIBASE ppBase)
1272{
1273 NOREF(pUVM); NOREF(pszDevice); NOREF(iDevIns); NOREF(iLun); NOREF(pszDriver); NOREF(iOccurrence);
1274 NOREF(fFlags); NOREF(pCfg); NOREF(ppBase);
1275 return VERR_NOT_IMPLEMENTED;
1276}
1277
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