VirtualBox

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

Last change on this file since 96407 was 96407, checked in by vboxsync, 22 months ago

scm copyright and license note update

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

© 2023 Oracle
ContactPrivacy policyTerms of Use