VirtualBox

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

Last change on this file since 84459 was 84459, checked in by vboxsync, 4 years ago

VMM/PDM: DBGF even tracing integration, bugref:9210

Integrates the new DBGF event tracing framework into PDM
devices. The new CFGM key "TracingEnabled" for a device
instance enables tracing using DBGF. A special tracing variant
of the PDM device helper is provided.

Disabled by default for now, enable with VBOX_WITH_DBGF_TRACING

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

© 2023 Oracle
ContactPrivacy policyTerms of Use