VirtualBox

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

Last change on this file since 105853 was 105687, checked in by vboxsync, 5 months ago

VMMArm/GIC: Rename GICR3Kvm to GICR3Nem and add a stub for macOS Hypervisor.framework which will be handled by the NEM backend directly due to the hv_gic_* API being entangled with the rest of the hv_* API, bugref:10747

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 50.5 KB
Line 
1/* $Id: PDMDevice.cpp 105687 2024-08-15 12:45:46Z 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
688 /*
689 * Register the internal VMM GIC device, NEM variant.
690 */
691 rc = pdmR3DevReg_Register(&RegCB.Core, &g_DeviceGICNem);
692 AssertRCReturn(rc, rc);
693#else
694 /*
695 * Register the internal VMM APIC device.
696 */
697 int rc = pdmR3DevReg_Register(&RegCB.Core, &g_DeviceAPIC);
698 AssertRCReturn(rc, rc);
699#endif
700
701 /*
702 * Load the builtin module.
703 */
704 PCFGMNODE pDevicesNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "PDM/Devices");
705 bool fLoadBuiltin;
706 rc = CFGMR3QueryBool(pDevicesNode, "LoadBuiltin", &fLoadBuiltin);
707 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
708 fLoadBuiltin = true;
709 else if (RT_FAILURE(rc))
710 {
711 AssertMsgFailed(("Configuration error: Querying boolean \"LoadBuiltin\" failed with %Rrc\n", rc));
712 return rc;
713 }
714 if (fLoadBuiltin)
715 {
716 /* make filename */
717 char *pszFilename = pdmR3FileR3("VBoxDD", true /*fShared*/);
718 if (!pszFilename)
719 return VERR_NO_TMP_MEMORY;
720 rc = pdmR3DevLoad(pVM, &RegCB, pszFilename, "VBoxDD");
721 RTMemTmpFree(pszFilename);
722 if (RT_FAILURE(rc))
723 return rc;
724
725 /* make filename */
726 pszFilename = pdmR3FileR3("VBoxDD2", true /*fShared*/);
727 if (!pszFilename)
728 return VERR_NO_TMP_MEMORY;
729 rc = pdmR3DevLoad(pVM, &RegCB, pszFilename, "VBoxDD2");
730 RTMemTmpFree(pszFilename);
731 if (RT_FAILURE(rc))
732 return rc;
733 }
734
735 /*
736 * Load additional device modules.
737 */
738 PCFGMNODE pCur;
739 for (pCur = CFGMR3GetFirstChild(pDevicesNode); pCur; pCur = CFGMR3GetNextChild(pCur))
740 {
741 /*
742 * Get the name and path.
743 */
744 char szName[PDMMOD_NAME_LEN];
745 rc = CFGMR3GetName(pCur, &szName[0], sizeof(szName));
746 if (rc == VERR_CFGM_NOT_ENOUGH_SPACE)
747 {
748 AssertMsgFailed(("configuration error: The module name is too long, cchName=%zu.\n", CFGMR3GetNameLen(pCur)));
749 return VERR_PDM_MODULE_NAME_TOO_LONG;
750 }
751 else if (RT_FAILURE(rc))
752 {
753 AssertMsgFailed(("CFGMR3GetName -> %Rrc.\n", rc));
754 return rc;
755 }
756
757 /* the path is optional, if no path the module name + path is used. */
758 char szFilename[RTPATH_MAX];
759 rc = CFGMR3QueryString(pCur, "Path", &szFilename[0], sizeof(szFilename));
760 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
761 strcpy(szFilename, szName);
762 else if (RT_FAILURE(rc))
763 {
764 AssertMsgFailed(("configuration error: Failure to query the module path, rc=%Rrc.\n", rc));
765 return rc;
766 }
767
768 /* prepend path? */
769 if (!RTPathHavePath(szFilename))
770 {
771 char *psz = pdmR3FileR3(szFilename, false /*fShared*/);
772 if (!psz)
773 return VERR_NO_TMP_MEMORY;
774 size_t cch = strlen(psz) + 1;
775 if (cch > sizeof(szFilename))
776 {
777 RTMemTmpFree(psz);
778 AssertMsgFailed(("Filename too long! cch=%d '%s'\n", cch, psz));
779 return VERR_FILENAME_TOO_LONG;
780 }
781 memcpy(szFilename, psz, cch);
782 RTMemTmpFree(psz);
783 }
784
785 /*
786 * Load the module and register it's devices.
787 */
788 RegCB.pCfgNode = pCur;
789 rc = pdmR3DevLoad(pVM, &RegCB, szFilename, szName);
790 if (RT_FAILURE(rc))
791 return rc;
792 }
793
794 return VINF_SUCCESS;
795}
796
797
798/**
799 * Loads one device module and call the registration entry point.
800 *
801 * @returns VBox status code.
802 * @param pVM The cross context VM structure.
803 * @param pRegCB The registration callback stuff.
804 * @param pszFilename Module filename.
805 * @param pszName Module name.
806 */
807static int pdmR3DevLoad(PVM pVM, PPDMDEVREGCBINT pRegCB, const char *pszFilename, const char *pszName)
808{
809 /*
810 * Load it.
811 */
812 int rc = pdmR3LoadR3U(pVM->pUVM, pszFilename, pszName);
813 if (RT_SUCCESS(rc))
814 {
815 /*
816 * Get the registration export and call it.
817 */
818 FNPDMVBOXDEVICESREGISTER *pfnVBoxDevicesRegister;
819 rc = PDMR3LdrGetSymbolR3(pVM, pszName, "VBoxDevicesRegister", (void **)&pfnVBoxDevicesRegister);
820 if (RT_SUCCESS(rc))
821 {
822 Log(("PDM: Calling VBoxDevicesRegister (%p) of %s (%s)\n", pfnVBoxDevicesRegister, pszName, pszFilename));
823 rc = pfnVBoxDevicesRegister(&pRegCB->Core, VBOX_VERSION);
824 if (RT_SUCCESS(rc))
825 Log(("PDM: Successfully loaded device module %s (%s).\n", pszName, pszFilename));
826 else
827 {
828 VMR3SetError(pVM->pUVM, rc, RT_SRC_POS, "VBoxDevicesRegister failed with rc=%Rrc for module %s (%s)",
829 rc, pszName, pszFilename);
830 AssertMsgFailed(("VBoxDevicesRegister failed with rc=%Rrc for module %s (%s)\n", rc, pszName, pszFilename));
831 }
832 }
833 else
834 {
835 AssertMsgFailed(("Failed to locate 'VBoxDevicesRegister' in %s (%s) rc=%Rrc\n", pszName, pszFilename, rc));
836 if (rc == VERR_SYMBOL_NOT_FOUND)
837 rc = VERR_PDM_NO_REGISTRATION_EXPORT;
838 VMR3SetError(pVM->pUVM, rc, RT_SRC_POS, "Failed to locate 'VBoxDevicesRegister' in %s (%s) rc=%Rrc",
839 pszName, pszFilename, rc);
840 }
841 }
842 else
843 AssertMsgFailed(("Failed to load %s %s!\n", pszFilename, pszName));
844 return rc;
845}
846
847
848/**
849 * @interface_method_impl{PDMDEVREGCB,pfnRegister}
850 */
851static DECLCALLBACK(int) pdmR3DevReg_Register(PPDMDEVREGCB pCallbacks, PCPDMDEVREG pReg)
852{
853 /*
854 * Validate the registration structure.
855 */
856 Assert(pReg);
857 AssertMsgReturn(pReg->u32Version == PDM_DEVREG_VERSION,
858 ("Unknown struct version %#x!\n", pReg->u32Version),
859 VERR_PDM_UNKNOWN_DEVREG_VERSION);
860
861 AssertMsgReturn( pReg->szName[0]
862 && strlen(pReg->szName) < sizeof(pReg->szName)
863 && pdmR3IsValidName(pReg->szName),
864 ("Invalid name '%.*s'\n", sizeof(pReg->szName), pReg->szName),
865 VERR_PDM_INVALID_DEVICE_REGISTRATION);
866 AssertMsgReturn( !(pReg->fFlags & PDM_DEVREG_FLAGS_RC)
867 || ( pReg->pszRCMod[0]
868 && strlen(pReg->pszRCMod) < RT_SIZEOFMEMB(PDMDEVICECREATEREQ, szModName)),
869 ("Invalid GC module name '%s' - (Device %s)\n", pReg->pszRCMod, pReg->szName),
870 VERR_PDM_INVALID_DEVICE_REGISTRATION);
871 AssertMsgReturn( !(pReg->fFlags & PDM_DEVREG_FLAGS_R0)
872 || ( pReg->pszR0Mod[0]
873 && strlen(pReg->pszR0Mod) < RT_SIZEOFMEMB(PDMDEVICECREATEREQ, szModName)),
874 ("Invalid R0 module name '%s' - (Device %s)\n", pReg->pszR0Mod, pReg->szName),
875 VERR_PDM_INVALID_DEVICE_REGISTRATION);
876 AssertMsgReturn((pReg->fFlags & PDM_DEVREG_FLAGS_HOST_BITS_MASK) == PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT,
877 ("Invalid host bits flags! fFlags=%#x (Device %s)\n", pReg->fFlags, pReg->szName),
878 VERR_PDM_INVALID_DEVICE_HOST_BITS);
879 AssertMsgReturn((pReg->fFlags & PDM_DEVREG_FLAGS_GUEST_BITS_MASK),
880 ("Invalid guest bits flags! fFlags=%#x (Device %s)\n", pReg->fFlags, pReg->szName),
881 VERR_PDM_INVALID_DEVICE_REGISTRATION);
882 AssertMsgReturn(pReg->fClass,
883 ("No class! (Device %s)\n", pReg->szName),
884 VERR_PDM_INVALID_DEVICE_REGISTRATION);
885 AssertMsgReturn(pReg->cMaxInstances > 0,
886 ("Max instances %u! (Device %s)\n", pReg->cMaxInstances, pReg->szName),
887 VERR_PDM_INVALID_DEVICE_REGISTRATION);
888 uint32_t const cbMaxInstance = pReg->fFlags & (PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0)
889 ? PDM_MAX_DEVICE_INSTANCE_SIZE : PDM_MAX_DEVICE_INSTANCE_SIZE_R3;
890 AssertMsgReturn(pReg->cbInstanceShared <= cbMaxInstance,
891 ("Instance size %u bytes! (Max %u; Device %s)\n", pReg->cbInstanceShared, cbMaxInstance, pReg->szName),
892 VERR_PDM_INVALID_DEVICE_REGISTRATION);
893 AssertMsgReturn(pReg->cbInstanceCC <= cbMaxInstance,
894 ("Instance size %d bytes! (Max %u; Device %s)\n", pReg->cbInstanceCC, cbMaxInstance, pReg->szName),
895 VERR_PDM_INVALID_DEVICE_REGISTRATION);
896 AssertMsgReturn(pReg->pfnConstruct,
897 ("No constructor! (Device %s)\n", pReg->szName),
898 VERR_PDM_INVALID_DEVICE_REGISTRATION);
899 AssertLogRelMsgReturn((pReg->fFlags & PDM_DEVREG_FLAGS_GUEST_BITS_MASK) == PDM_DEVREG_FLAGS_GUEST_BITS_DEFAULT,
900 ("PDM: Rejected device '%s' because it didn't match the guest bits.\n", pReg->szName),
901 VERR_PDM_INVALID_DEVICE_GUEST_BITS);
902 AssertLogRelMsg(pReg->u32VersionEnd == PDM_DEVREG_VERSION,
903 ("u32VersionEnd=%#x, expected %#x. (szName=%s)\n",
904 pReg->u32VersionEnd, PDM_DEVREG_VERSION, pReg->szName));
905 AssertLogRelMsgReturn(pReg->cMaxPciDevices <= 8, ("%#x (szName=%s)\n", pReg->cMaxPciDevices, pReg->szName),
906 VERR_PDM_INVALID_DEVICE_REGISTRATION);
907 AssertLogRelMsgReturn(pReg->cMaxMsixVectors <= VBOX_MSIX_MAX_ENTRIES,
908 ("%#x (szName=%s)\n", pReg->cMaxMsixVectors, pReg->szName),
909 VERR_PDM_INVALID_DEVICE_REGISTRATION);
910 AssertLogRelMsgReturn(pReg->fFlags & PDM_DEVREG_FLAGS_NEW_STYLE /* the flag is required now */,
911 ("PDM_DEVREG_FLAGS_NEW_STYLE not set for szName=%s!\n", pReg->szName),
912 VERR_PDM_INVALID_DEVICE_REGISTRATION);
913
914 /*
915 * Check for duplicate and find FIFO entry at the same time.
916 */
917 PCPDMDEVREGCBINT pRegCB = (PCPDMDEVREGCBINT)pCallbacks;
918 PPDMDEV pDevPrev = NULL;
919 PPDMDEV pDev = pRegCB->pVM->pdm.s.pDevs;
920 for (; pDev; pDevPrev = pDev, pDev = pDev->pNext)
921 AssertMsgReturn(strcmp(pDev->pReg->szName, pReg->szName),
922 ("Device '%s' already exists\n", pReg->szName),
923 VERR_PDM_DEVICE_NAME_CLASH);
924
925 /*
926 * Allocate new device structure, initialize and insert it into the list.
927 */
928 int rc;
929 pDev = (PPDMDEV)MMR3HeapAlloc(pRegCB->pVM, MM_TAG_PDM_DEVICE, sizeof(*pDev));
930 if (pDev)
931 {
932 pDev->pNext = NULL;
933 pDev->cInstances = 0;
934 pDev->pInstances = NULL;
935 pDev->pReg = pReg;
936 pDev->cchName = (uint32_t)strlen(pReg->szName);
937 rc = CFGMR3QueryStringAllocDef( pRegCB->pCfgNode, "RCSearchPath", &pDev->pszRCSearchPath, NULL);
938 if (RT_SUCCESS(rc))
939 rc = CFGMR3QueryStringAllocDef(pRegCB->pCfgNode, "R0SearchPath", &pDev->pszR0SearchPath, NULL);
940 if (RT_SUCCESS(rc))
941 {
942 if (pDevPrev)
943 pDevPrev->pNext = pDev;
944 else
945 pRegCB->pVM->pdm.s.pDevs = pDev;
946 Log(("PDM: Registered device '%s'\n", pReg->szName));
947 return VINF_SUCCESS;
948 }
949
950 MMR3HeapFree(pDev);
951 }
952 else
953 rc = VERR_NO_MEMORY;
954 return rc;
955}
956
957
958/**
959 * Locates a LUN.
960 *
961 * @returns VBox status code.
962 * @param pVM The cross context VM structure.
963 * @param pszDevice Device name.
964 * @param iInstance Device instance.
965 * @param iLun The Logical Unit to obtain the interface of.
966 * @param ppLun Where to store the pointer to the LUN if found.
967 * @thread Try only do this in EMT...
968 */
969int pdmR3DevFindLun(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPPDMLUN ppLun)
970{
971 /*
972 * Iterate registered devices looking for the device.
973 */
974 size_t cchDevice = strlen(pszDevice);
975 for (PPDMDEV pDev = pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
976 {
977 if ( pDev->cchName == cchDevice
978 && !memcmp(pDev->pReg->szName, pszDevice, cchDevice))
979 {
980 /*
981 * Iterate device instances.
982 */
983 for (PPDMDEVINS pDevIns = pDev->pInstances; pDevIns; pDevIns = pDevIns->Internal.s.pPerDeviceNextR3)
984 {
985 if (pDevIns->iInstance == iInstance)
986 {
987 /*
988 * Iterate luns.
989 */
990 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
991 {
992 if (pLun->iLun == iLun)
993 {
994 *ppLun = pLun;
995 return VINF_SUCCESS;
996 }
997 }
998 return VERR_PDM_LUN_NOT_FOUND;
999 }
1000 }
1001 return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND;
1002 }
1003 }
1004 return VERR_PDM_DEVICE_NOT_FOUND;
1005}
1006
1007
1008/**
1009 * Attaches a preconfigured driver to an existing device instance.
1010 *
1011 * This is used to change drivers and suchlike at runtime.
1012 *
1013 * @returns VBox status code.
1014 * @param pUVM The user mode VM handle.
1015 * @param pszDevice Device name.
1016 * @param iInstance Device instance.
1017 * @param iLun The Logical Unit to obtain the interface of.
1018 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
1019 * @param ppBase Where to store the base interface pointer. Optional.
1020 * @thread EMT
1021 */
1022VMMR3DECL(int) PDMR3DeviceAttach(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, uint32_t fFlags, PPPDMIBASE ppBase)
1023{
1024 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1025 PVM pVM = pUVM->pVM;
1026 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1027 VM_ASSERT_EMT(pVM);
1028 LogFlow(("PDMR3DeviceAttach: pszDevice=%p:{%s} iInstance=%d iLun=%d fFlags=%#x ppBase=%p\n",
1029 pszDevice, pszDevice, iInstance, iLun, fFlags, ppBase));
1030
1031 /*
1032 * Find the LUN in question.
1033 */
1034 PPDMLUN pLun;
1035 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
1036 if (RT_SUCCESS(rc))
1037 {
1038 /*
1039 * Can we attach anything at runtime?
1040 */
1041 PPDMDEVINS pDevIns = pLun->pDevIns;
1042 if (pDevIns->pReg->pfnAttach)
1043 {
1044 if (!pLun->pTop)
1045 {
1046 PDMCritSectEnter(pVM, pDevIns->pCritSectRoR3, VERR_IGNORED);
1047 rc = pDevIns->pReg->pfnAttach(pDevIns, iLun, fFlags);
1048 PDMCritSectLeave(pVM, pDevIns->pCritSectRoR3);
1049 }
1050 else
1051 rc = VERR_PDM_DRIVER_ALREADY_ATTACHED;
1052 }
1053 else
1054 rc = VERR_PDM_DEVICE_NO_RT_ATTACH;
1055
1056 if (ppBase)
1057 *ppBase = pLun->pTop ? &pLun->pTop->IBase : NULL;
1058 }
1059 else if (ppBase)
1060 *ppBase = NULL;
1061
1062 if (ppBase)
1063 LogFlow(("PDMR3DeviceAttach: returns %Rrc *ppBase=%p\n", rc, *ppBase));
1064 else
1065 LogFlow(("PDMR3DeviceAttach: returns %Rrc\n", rc));
1066 return rc;
1067}
1068
1069
1070/**
1071 * Detaches a driver chain from an existing device instance.
1072 *
1073 * This is used to change drivers and suchlike at runtime.
1074 *
1075 * @returns VBox status code.
1076 * @param pUVM The user mode VM handle.
1077 * @param pszDevice Device name.
1078 * @param iInstance Device instance.
1079 * @param iLun The Logical Unit to obtain the interface of.
1080 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
1081 * @thread EMT
1082 */
1083VMMR3DECL(int) PDMR3DeviceDetach(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, uint32_t fFlags)
1084{
1085 return PDMR3DriverDetach(pUVM, pszDevice, iInstance, iLun, NULL, 0, fFlags);
1086}
1087
1088
1089/**
1090 * References the critical section associated with a device for the use by a
1091 * timer or similar created by the device.
1092 *
1093 * @returns Pointer to the critical section.
1094 * @param pVM The cross context VM structure.
1095 * @param pDevIns The device instance in question.
1096 *
1097 * @internal
1098 */
1099VMMR3_INT_DECL(PPDMCRITSECT) PDMR3DevGetCritSect(PVM pVM, PPDMDEVINS pDevIns)
1100{
1101 VM_ASSERT_EMT(pVM); RT_NOREF_PV(pVM);
1102 VM_ASSERT_STATE(pVM, VMSTATE_CREATING);
1103 AssertPtr(pDevIns);
1104
1105 PPDMCRITSECT pCritSect = pDevIns->pCritSectRoR3;
1106 AssertPtr(pCritSect);
1107 pCritSect->s.fUsedByTimerOrSimilar = true;
1108
1109 return pCritSect;
1110}
1111
1112
1113/**
1114 * Attaches a preconfigured driver to an existing device or driver instance.
1115 *
1116 * This is used to change drivers and suchlike at runtime. The driver or device
1117 * at the end of the chain will be told to attach to whatever is configured
1118 * below it.
1119 *
1120 * @returns VBox status code.
1121 * @param pUVM The user mode VM handle.
1122 * @param pszDevice Device name.
1123 * @param iInstance Device instance.
1124 * @param iLun The Logical Unit to obtain the interface of.
1125 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
1126 * @param ppBase Where to store the base interface pointer. Optional.
1127 *
1128 * @thread EMT
1129 */
1130VMMR3DECL(int) PDMR3DriverAttach(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, uint32_t fFlags, PPPDMIBASE ppBase)
1131{
1132 LogFlow(("PDMR3DriverAttach: pszDevice=%p:{%s} iInstance=%d iLun=%d fFlags=%#x ppBase=%p\n",
1133 pszDevice, pszDevice, iInstance, iLun, fFlags, ppBase));
1134 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1135 PVM pVM = pUVM->pVM;
1136 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1137 VM_ASSERT_EMT(pVM);
1138
1139 if (ppBase)
1140 *ppBase = NULL;
1141
1142 /*
1143 * Find the LUN in question.
1144 */
1145 PPDMLUN pLun;
1146 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
1147 if (RT_SUCCESS(rc))
1148 {
1149 /*
1150 * Anything attached to the LUN?
1151 */
1152 PPDMDRVINS pDrvIns = pLun->pTop;
1153 if (!pDrvIns)
1154 {
1155 /* No, ask the device to attach to the new stuff. */
1156 PPDMDEVINS pDevIns = pLun->pDevIns;
1157 if (pDevIns->pReg->pfnAttach)
1158 {
1159 PDMCritSectEnter(pVM, pDevIns->pCritSectRoR3, VERR_IGNORED);
1160 rc = pDevIns->pReg->pfnAttach(pDevIns, iLun, fFlags);
1161 if (RT_SUCCESS(rc) && ppBase)
1162 *ppBase = pLun->pTop ? &pLun->pTop->IBase : NULL;
1163 PDMCritSectLeave(pVM, pDevIns->pCritSectRoR3);
1164 }
1165 else
1166 rc = VERR_PDM_DEVICE_NO_RT_ATTACH;
1167 }
1168 else
1169 {
1170 /* Yes, find the bottom most driver and ask it to attach to the new stuff. */
1171 while (pDrvIns->Internal.s.pDown)
1172 pDrvIns = pDrvIns->Internal.s.pDown;
1173 if (pDrvIns->pReg->pfnAttach)
1174 {
1175 rc = pDrvIns->pReg->pfnAttach(pDrvIns, fFlags);
1176 if (RT_SUCCESS(rc) && ppBase)
1177 *ppBase = pDrvIns->Internal.s.pDown
1178 ? &pDrvIns->Internal.s.pDown->IBase
1179 : NULL;
1180 }
1181 else
1182 rc = VERR_PDM_DRIVER_NO_RT_ATTACH;
1183 }
1184 }
1185
1186 if (ppBase)
1187 LogFlow(("PDMR3DriverAttach: returns %Rrc *ppBase=%p\n", rc, *ppBase));
1188 else
1189 LogFlow(("PDMR3DriverAttach: returns %Rrc\n", rc));
1190 return rc;
1191}
1192
1193
1194/**
1195 * Detaches the specified driver instance.
1196 *
1197 * This is used to replumb drivers at runtime for simulating hot plugging and
1198 * media changes.
1199 *
1200 * This is a superset of PDMR3DeviceDetach. It allows detaching drivers from
1201 * any driver or device by specifying the driver to start detaching at. The
1202 * only prerequisite is that the driver or device above implements the
1203 * pfnDetach callback (PDMDRVREG / PDMDEVREG).
1204 *
1205 * @returns VBox status code.
1206 * @param pUVM The user mode VM handle.
1207 * @param pszDevice Device name.
1208 * @param iDevIns Device instance.
1209 * @param iLun The Logical Unit in which to look for the driver.
1210 * @param pszDriver The name of the driver which to detach. If NULL
1211 * then the entire driver chain is detatched.
1212 * @param iOccurrence The occurrence of that driver in the chain. This is
1213 * usually 0.
1214 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
1215 * @thread EMT
1216 */
1217VMMR3DECL(int) PDMR3DriverDetach(PUVM pUVM, const char *pszDevice, unsigned iDevIns, unsigned iLun,
1218 const char *pszDriver, unsigned iOccurrence, uint32_t fFlags)
1219{
1220 LogFlow(("PDMR3DriverDetach: pszDevice=%p:{%s} iDevIns=%u iLun=%u pszDriver=%p:{%s} iOccurrence=%u fFlags=%#x\n",
1221 pszDevice, pszDevice, iDevIns, iLun, pszDriver, pszDriver, iOccurrence, fFlags));
1222 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1223 PVM pVM = pUVM->pVM;
1224 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1225 VM_ASSERT_EMT(pVM);
1226 AssertPtr(pszDevice);
1227 AssertPtrNull(pszDriver);
1228 Assert(iOccurrence == 0 || pszDriver);
1229 Assert(!(fFlags & ~(PDM_TACH_FLAGS_NOT_HOT_PLUG)));
1230
1231 /*
1232 * Find the LUN in question.
1233 */
1234 PPDMLUN pLun;
1235 int rc = pdmR3DevFindLun(pVM, pszDevice, iDevIns, iLun, &pLun);
1236 if (RT_SUCCESS(rc))
1237 {
1238 /*
1239 * Locate the driver.
1240 */
1241 PPDMDRVINS pDrvIns = pLun->pTop;
1242 if (pDrvIns)
1243 {
1244 if (pszDriver)
1245 {
1246 while (pDrvIns)
1247 {
1248 if (!strcmp(pDrvIns->pReg->szName, pszDriver))
1249 {
1250 if (iOccurrence == 0)
1251 break;
1252 iOccurrence--;
1253 }
1254 pDrvIns = pDrvIns->Internal.s.pDown;
1255 }
1256 }
1257 if (pDrvIns)
1258 rc = pdmR3DrvDetach(pDrvIns, fFlags);
1259 else
1260 rc = VERR_PDM_DRIVER_INSTANCE_NOT_FOUND;
1261 }
1262 else
1263 rc = VINF_PDM_NO_DRIVER_ATTACHED_TO_LUN;
1264 }
1265
1266 LogFlow(("PDMR3DriverDetach: returns %Rrc\n", rc));
1267 return rc;
1268}
1269
1270
1271/**
1272 * Runtime detach and reattach of a new driver chain or sub chain.
1273 *
1274 * This is intended to be called on a non-EMT thread, this will instantiate the
1275 * new driver (sub-)chain, and then the EMTs will do the actual replumbing. The
1276 * destruction of the old driver chain will be taken care of on the calling
1277 * thread.
1278 *
1279 * @returns VBox status code.
1280 * @param pUVM The user mode VM handle.
1281 * @param pszDevice Device name.
1282 * @param iDevIns Device instance.
1283 * @param iLun The Logical Unit in which to look for the driver.
1284 * @param pszDriver The name of the driver which to detach and replace.
1285 * If NULL then the entire driver chain is to be
1286 * reattached.
1287 * @param iOccurrence The occurrence of that driver in the chain. This is
1288 * usually 0.
1289 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
1290 * @param pCfg The configuration of the new driver chain that is
1291 * going to be attached. The subtree starts with the
1292 * node containing a Driver key, a Config subtree and
1293 * optionally an AttachedDriver subtree.
1294 * If this parameter is NULL, then this call will work
1295 * like at a non-pause version of PDMR3DriverDetach.
1296 * @param ppBase Where to store the base interface pointer to the new
1297 * driver. Optional.
1298 *
1299 * @thread Any thread. The EMTs will be involved at some point though.
1300 */
1301VMMR3DECL(int) PDMR3DriverReattach(PUVM pUVM, const char *pszDevice, unsigned iDevIns, unsigned iLun,
1302 const char *pszDriver, unsigned iOccurrence, uint32_t fFlags,
1303 PCFGMNODE pCfg, PPPDMIBASE ppBase)
1304{
1305 NOREF(pUVM); NOREF(pszDevice); NOREF(iDevIns); NOREF(iLun); NOREF(pszDriver); NOREF(iOccurrence);
1306 NOREF(fFlags); NOREF(pCfg); NOREF(ppBase);
1307 return VERR_NOT_IMPLEMENTED;
1308}
1309
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