VirtualBox

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

Last change on this file since 107227 was 107227, checked in by vboxsync, 6 weeks ago

VMM: Cleaning up ARMv8 / x86 split. jiraref:VBP-1470

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