VirtualBox

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

Last change on this file since 107031 was 106476, checked in by vboxsync, 3 months ago

VMMArm: Skeleton of the PMU device emulation enough to make Windows/ARM boot as a guest and have it out of the NEM darwin backend, bugref:10778

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