VirtualBox

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

Last change on this file since 103799 was 99385, checked in by vboxsync, 20 months ago

VMM/ArmV8: Skeleton of the GICv3 interrupt controller emulation, bugref:10404

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