VirtualBox

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

Last change on this file since 107200 was 107194, checked in by vboxsync, 2 months ago

VMM: More adjustments for VBOX_WITH_ONLY_PGM_NEM_MODE, VBOX_WITH_MINIMAL_R0, VBOX_WITH_HWVIRT and such. jiraref:VBP-1466

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