VirtualBox

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

Last change on this file was 104386, checked in by vboxsync, 4 weeks ago

VMM/GIC: Add a dedicated GIC device implementation for linux.arm64 which interfaces with the in-kernel KVM GIC device emulation, bugref:10391

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

© 2023 Oracle
ContactPrivacy policyTerms of Use