VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/PDM.cpp@ 41800

Last change on this file since 41800 was 41800, checked in by vboxsync, 12 years ago

Doxygen.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 99.7 KB
Line 
1/* $Id: PDM.cpp 41800 2012-06-17 16:18:26Z vboxsync $ */
2/** @file
3 * PDM - Pluggable Device Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2012 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/** @page pg_pdm PDM - The Pluggable Device & Driver Manager
20 *
21 * VirtualBox is designed to be very configurable, i.e. the ability to select
22 * virtual devices and configure them uniquely for a VM. For this reason
23 * virtual devices are not statically linked with the VMM but loaded, linked and
24 * instantiated at runtime by PDM using the information found in the
25 * Configuration Manager (CFGM).
26 *
27 * While the chief purpose of PDM is to manager of devices their drivers, it
28 * also serves as somewhere to put usful things like cross context queues, cross
29 * context synchronization (like critsect), VM centric thread management,
30 * asynchronous I/O framework, and so on.
31 *
32 * @see grp_pdm
33 *
34 *
35 * @section sec_pdm_dev The Pluggable Devices
36 *
37 * Devices register themselves when the module containing them is loaded. PDM
38 * will call the entry point 'VBoxDevicesRegister' when loading a device module.
39 * The device module will then use the supplied callback table to check the VMM
40 * version and to register its devices. Each device have an unique (for the
41 * configured VM) name. The name is not only used in PDM but also in CFGM (to
42 * organize device and device instance settings) and by anyone who wants to talk
43 * to a specific device instance.
44 *
45 * When all device modules have been successfully loaded PDM will instantiate
46 * those devices which are configured for the VM. Note that a device may have
47 * more than one instance, see network adaptors for instance. When
48 * instantiating a device PDM provides device instance memory and a callback
49 * table (aka Device Helpers / DevHlp) with the VM APIs which the device
50 * instance is trusted with.
51 *
52 * Some devices are trusted devices, most are not. The trusted devices are an
53 * integrated part of the VM and can obtain the VM handle from their device
54 * instance handles, thus enabling them to call any VM api. Untrusted devices
55 * can only use the callbacks provided during device instantiation.
56 *
57 * The main purpose in having DevHlps rather than just giving all the devices
58 * the VM handle and let them call the internal VM APIs directly, is both to
59 * create a binary interface that can be supported across releases and to
60 * create a barrier between devices and the VM. (The trusted / untrusted bit
61 * hasn't turned out to be of much use btw., but it's easy to maintain so there
62 * isn't any point in removing it.)
63 *
64 * A device can provide a ring-0 and/or a raw-mode context extension to improve
65 * the VM performance by handling exits and traps (respectively) without
66 * requiring context switches (to ring-3). Callbacks for MMIO and I/O ports can
67 * needs to be registered specifically for the additional contexts for this to
68 * make sense. Also, the device has to be trusted to be loaded into R0/RC
69 * because of the extra privilege it entails. Note that raw-mode code and data
70 * will be subject to relocation.
71 *
72 *
73 * @section sec_pdm_special_devs Special Devices
74 *
75 * Several kinds of devices interacts with the VMM and/or other device and PDM
76 * will work like a mediator for these. The typical pattern is that the device
77 * calls a special registration device helper with a set of callbacks, PDM
78 * responds by copying this and providing a pointer to a set helper callbacks
79 * for that particular kind of device. Unlike interfaces where the callback
80 * table pointer is used a 'this' pointer, these arrangements will use the
81 * device instance pointer (PPDMDEVINS) as a kind of 'this' pointer.
82 *
83 * For an example of this kind of setup, see the PIC. The PIC registers itself
84 * by calling PDMDEVHLPR3::pfnPICRegister. PDM saves the device instance,
85 * copies the callback tables (PDMPICREG), resolving the ring-0 and raw-mode
86 * addresses in the process, and hands back the pointer to a set of helper
87 * methods (PDMPICHLPR3). The PCI device then queries the ring-0 and raw-mode
88 * helpers using PDMPICHLPR3::pfnGetR0Helpers and PDMPICHLPR3::pfnGetRCHelpers.
89 * The PCI device repeats ths pfnGetRCHelpers call in it's relocation method
90 * since the address changes when RC is relocated.
91 *
92 * @see grp_pdm_device
93 *
94 *
95 * @section sec_pdm_usbdev The Pluggable USB Devices
96 *
97 * USB devices are handled a little bit differently than other devices. The
98 * general concepts wrt. pluggability are mostly the same, but the details
99 * varies. The registration entry point is 'VBoxUsbRegister', the device
100 * instance is PDMUSBINS and the callbacks helpers are different. Also, USB
101 * device are restricted to ring-3 and cannot have any ring-0 or raw-mode
102 * extensions (at least not yet).
103 *
104 * The way USB devices work differs greatly from other devices though since they
105 * aren't attaches directly to the PCI/ISA/whatever system buses but via a
106 * USB host control (OHCI, UHCI or EHCI). USB devices handles USB requests
107 * (URBs) and does not register I/O ports, MMIO ranges or PCI bus
108 * devices/functions.
109 *
110 * @see grp_pdm_usbdev
111 *
112 *
113 * @section sec_pdm_drv The Pluggable Drivers
114 *
115 * The VM devices are often accessing host hardware or OS facilities. For most
116 * devices these facilities can be abstracted in one or more levels. These
117 * abstractions are called drivers.
118 *
119 * For instance take a DVD/CD drive. This can be connected to a SCSI
120 * controller, an ATA controller or a SATA controller. The basics of the DVD/CD
121 * drive implementation remains the same - eject, insert, read, seek, and such.
122 * (For the scsi case, you might wanna speak SCSI directly to, but that can of
123 * course be fixed - see SCSI passthru.) So, it
124 * makes much sense to have a generic CD/DVD driver which implements this.
125 *
126 * Then the media 'inserted' into the DVD/CD drive can be a ISO image, or it can
127 * be read from a real CD or DVD drive (there are probably other custom formats
128 * someone could desire to read or construct too). So, it would make sense to
129 * have abstracted interfaces for dealing with this in a generic way so the
130 * cdrom unit doesn't have to implement it all. Thus we have created the
131 * CDROM/DVD media driver family.
132 *
133 * So, for this example the IDE controller #1 (i.e. secondary) will have
134 * the DVD/CD Driver attached to it's LUN #0 (master). When a media is mounted
135 * the DVD/CD Driver will have a ISO, HostDVD or RAW (media) Driver attached.
136 *
137 * It is possible to configure many levels of drivers inserting filters, loggers,
138 * or whatever you desire into the chain. We're using this for network sniffing
139 * for instance.
140 *
141 * The drivers are loaded in a similar manner to that of the device, namely by
142 * iterating a keyspace in CFGM, load the modules listed there and call
143 * 'VBoxDriversRegister' with a callback table.
144 *
145 * @see grp_pdm_driver
146 *
147 *
148 * @section sec_pdm_ifs Interfaces
149 *
150 * The pluggable drivers and devices exposes one standard interface (callback
151 * table) which is used to construct, destruct, attach, detach,( ++,) and query
152 * other interfaces. A device will query the interfaces required for it's
153 * operation during init and hot-plug. PDM may query some interfaces during
154 * runtime mounting too.
155 *
156 * An interface here means a function table contained within the device or
157 * driver instance data. Its method are invoked with the function table pointer
158 * as the first argument and they will calculate the address of the device or
159 * driver instance data from it. (This is one of the aspects which *might* have
160 * been better done in C++.)
161 *
162 * @see grp_pdm_interfaces
163 *
164 *
165 * @section sec_pdm_utils Utilities
166 *
167 * As mentioned earlier, PDM is the location of any usful constructs that doesn't
168 * quite fit into IPRT. The next subsections will discuss these.
169 *
170 * One thing these APIs all have in common is that resources will be associated
171 * with a device / driver and automatically freed after it has been destroyed if
172 * the destructor didn't do this.
173 *
174 *
175 * @subsection sec_pdm_async_completion Async I/O
176 *
177 * The PDM Async I/O API provides a somewhat platform agnostic interface for
178 * asynchronous I/O. For reasons of performance and complexity this does not
179 * build upon any IPRT API.
180 *
181 * @todo more details.
182 *
183 * @see grp_pdm_async_completion
184 *
185 *
186 * @subsection sec_pdm_async_task Async Task - not implemented
187 *
188 * @todo implement and describe
189 *
190 * @see grp_pdm_async_task
191 *
192 *
193 * @subsection sec_pdm_critsect Critical Section
194 *
195 * The PDM Critical Section API is currently building on the IPRT API with the
196 * same name. It adds the possibility to use critical sections in ring-0 and
197 * raw-mode as well as in ring-3. There are certain restrictions on the RC and
198 * R0 usage though since we're not able to wait on it, nor wake up anyone that
199 * is waiting on it. These restrictions origins with the use of a ring-3 event
200 * semaphore. In a later incarnation we plan to replace the ring-3 event
201 * semaphore with a ring-0 one, thus enabling us to wake up waiters while
202 * exectuing in ring-0 and making the hardware assisted execution mode more
203 * efficient. (Raw-mode won't benefit much from this, naturally.)
204 *
205 * @see grp_pdm_critsect
206 *
207 *
208 * @subsection sec_pdm_queue Queue
209 *
210 * The PDM Queue API is for queuing one or more tasks for later consumption in
211 * ring-3 by EMT, and optionally forcing a delayed or ASAP return to ring-3. The
212 * queues can also be run on a timer basis as an alternative to the ASAP thing.
213 * The queue will be flushed at forced action time.
214 *
215 * A queue can also be used by another thread (a I/O worker for instance) to
216 * send work / events over to the EMT.
217 *
218 * @see grp_pdm_queue
219 *
220 *
221 * @subsection sec_pdm_task Task - not implemented yet
222 *
223 * The PDM Task API is for flagging a task for execution at a later point when
224 * we're back in ring-3, optionally forcing the ring-3 return to happen ASAP.
225 * As you can see the concept is similar to queues only simpler.
226 *
227 * A task can also be scheduled by another thread (a I/O worker for instance) as
228 * a mean of getting something done in EMT.
229 *
230 * @see grp_pdm_task
231 *
232 *
233 * @subsection sec_pdm_thread Thread
234 *
235 * The PDM Thread API is there to help devices and drivers manage their threads
236 * correctly wrt. power on, suspend, resume, power off and destruction.
237 *
238 * The general usage pattern for threads in the employ of devices and drivers is
239 * that they shuffle data or requests while the VM is running and stop doing
240 * this when the VM is paused or powered down. Rogue threads running while the
241 * VM is paused can cause the state to change during saving or have other
242 * unwanted side effects. The PDM Threads API ensures that this won't happen.
243 *
244 * @see grp_pdm_thread
245 *
246 */
247
248
249/*******************************************************************************
250* Header Files *
251*******************************************************************************/
252#define LOG_GROUP LOG_GROUP_PDM
253#include "PDMInternal.h"
254#include <VBox/vmm/pdm.h>
255#include <VBox/vmm/mm.h>
256#include <VBox/vmm/pgm.h>
257#include <VBox/vmm/ssm.h>
258#include <VBox/vmm/vm.h>
259#include <VBox/vmm/uvm.h>
260#include <VBox/vmm/vmm.h>
261#include <VBox/param.h>
262#include <VBox/err.h>
263#include <VBox/sup.h>
264
265#include <VBox/log.h>
266#include <iprt/asm.h>
267#include <iprt/assert.h>
268#include <iprt/alloc.h>
269#include <iprt/ctype.h>
270#include <iprt/ldr.h>
271#include <iprt/path.h>
272#include <iprt/string.h>
273
274
275/*******************************************************************************
276* Defined Constants And Macros *
277*******************************************************************************/
278/** The PDM saved state version. */
279#define PDM_SAVED_STATE_VERSION 4
280#define PDM_SAVED_STATE_VERSION_PRE_NMI_FF 3
281
282/** The number of nanoseconds a suspend callback needs to take before
283 * PDMR3Suspend warns about it taking too long. */
284#define PDMSUSPEND_WARN_AT_NS UINT64_C(1200000000)
285
286/** The number of nanoseconds a suspend callback needs to take before
287 * PDMR3PowerOff warns about it taking too long. */
288#define PDMPOWEROFF_WARN_AT_NS UINT64_C( 900000000)
289
290
291/*******************************************************************************
292* Structures and Typedefs *
293*******************************************************************************/
294/**
295 * Statistics of asynchronous notification tasks - used by reset, suspend and
296 * power off.
297 */
298typedef struct PDMNOTIFYASYNCSTATS
299{
300 /** The start timestamp. */
301 uint64_t uStartNsTs;
302 /** When to log the next time. */
303 uint64_t cNsElapsedNextLog;
304 /** The loop counter. */
305 uint32_t cLoops;
306 /** The number of pending asynchronous notification tasks. */
307 uint32_t cAsync;
308 /** The name of the operation (log prefix). */
309 const char *pszOp;
310 /** The current list buffer position. */
311 size_t offList;
312 /** String containing a list of the pending tasks. */
313 char szList[1024];
314} PDMNOTIFYASYNCSTATS;
315/** Pointer to the stats of pending asynchronous notification tasks. */
316typedef PDMNOTIFYASYNCSTATS *PPDMNOTIFYASYNCSTATS;
317
318
319/*******************************************************************************
320* Internal Functions *
321*******************************************************************************/
322static DECLCALLBACK(int) pdmR3LiveExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uPass);
323static DECLCALLBACK(int) pdmR3SaveExec(PVM pVM, PSSMHANDLE pSSM);
324static DECLCALLBACK(int) pdmR3LoadExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
325static DECLCALLBACK(int) pdmR3LoadPrep(PVM pVM, PSSMHANDLE pSSM);
326
327static FNDBGFHANDLERINT pdmR3InfoTracingIds;
328
329
330/**
331 * Initializes the PDM part of the UVM.
332 *
333 * This doesn't really do much right now but has to be here for the sake
334 * of completeness.
335 *
336 * @returns VBox status code.
337 * @param pUVM Pointer to the user mode VM structure.
338 */
339VMMR3DECL(int) PDMR3InitUVM(PUVM pUVM)
340{
341 AssertCompile(sizeof(pUVM->pdm.s) <= sizeof(pUVM->pdm.padding));
342 AssertRelease(sizeof(pUVM->pdm.s) <= sizeof(pUVM->pdm.padding));
343 pUVM->pdm.s.pModules = NULL;
344 pUVM->pdm.s.pCritSects = NULL;
345 return RTCritSectInit(&pUVM->pdm.s.ListCritSect);
346}
347
348
349/**
350 * Initializes the PDM.
351 *
352 * @returns VBox status code.
353 * @param pVM The VM to operate on.
354 */
355VMMR3DECL(int) PDMR3Init(PVM pVM)
356{
357 LogFlow(("PDMR3Init\n"));
358
359 /*
360 * Assert alignment and sizes.
361 */
362 AssertRelease(!(RT_OFFSETOF(VM, pdm.s) & 31));
363 AssertRelease(sizeof(pVM->pdm.s) <= sizeof(pVM->pdm.padding));
364 AssertCompileMemberAlignment(PDM, CritSect, sizeof(uintptr_t));
365
366 /*
367 * Init the structure.
368 */
369 pVM->pdm.s.GCPhysVMMDevHeap = NIL_RTGCPHYS;
370 //pVM->pdm.s.idTracingDev = 0;
371 pVM->pdm.s.idTracingOther = 1024;
372
373 /*
374 * Initialize critical sections first.
375 */
376 int rc = pdmR3CritSectInitStats(pVM);
377 if (RT_SUCCESS(rc))
378 rc = PDMR3CritSectInit(pVM, &pVM->pdm.s.CritSect, RT_SRC_POS, "PDM");
379 if (RT_SUCCESS(rc))
380 {
381 rc = PDMR3CritSectInit(pVM, &pVM->pdm.s.NopCritSect, RT_SRC_POS, "NOP");
382 if (RT_SUCCESS(rc))
383 pVM->pdm.s.NopCritSect.s.Core.fFlags |= RTCRITSECT_FLAGS_NOP;
384 }
385
386 /*
387 * Initialize sub components.
388 */
389 if (RT_SUCCESS(rc))
390 rc = pdmR3LdrInitU(pVM->pUVM);
391#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
392 if (RT_SUCCESS(rc))
393 rc = pdmR3AsyncCompletionInit(pVM);
394#endif
395#ifdef VBOX_WITH_NETSHAPER
396 if (RT_SUCCESS(rc))
397 rc = pdmR3NetShaperInit(pVM);
398#endif
399 if (RT_SUCCESS(rc))
400 rc = pdmR3BlkCacheInit(pVM);
401 if (RT_SUCCESS(rc))
402 rc = pdmR3DrvInit(pVM);
403 if (RT_SUCCESS(rc))
404 rc = pdmR3DevInit(pVM);
405 if (RT_SUCCESS(rc))
406 {
407 /*
408 * Register the saved state data unit.
409 */
410 rc = SSMR3RegisterInternal(pVM, "pdm", 1, PDM_SAVED_STATE_VERSION, 128,
411 NULL, pdmR3LiveExec, NULL,
412 NULL, pdmR3SaveExec, NULL,
413 pdmR3LoadPrep, pdmR3LoadExec, NULL);
414 if (RT_SUCCESS(rc))
415 {
416 /*
417 * Register the info handlers.
418 */
419 DBGFR3InfoRegisterInternal(pVM, "pdmtracingids",
420 "Displays the tracing IDs assigned by PDM to devices, USB device, drivers and more.",
421 pdmR3InfoTracingIds);
422
423 LogFlow(("PDM: Successfully initialized\n"));
424 return rc;
425 }
426 }
427
428 /*
429 * Cleanup and return failure.
430 */
431 PDMR3Term(pVM);
432 LogFlow(("PDMR3Init: returns %Rrc\n", rc));
433 return rc;
434}
435
436
437/**
438 * Applies relocations to data and code managed by this
439 * component. This function will be called at init and
440 * whenever the VMM need to relocate it self inside the GC.
441 *
442 * @param pVM Pointer to the VM.
443 * @param offDelta Relocation delta relative to old location.
444 * @remark The loader subcomponent is relocated by PDMR3LdrRelocate() very
445 * early in the relocation phase.
446 */
447VMMR3DECL(void) PDMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
448{
449 LogFlow(("PDMR3Relocate\n"));
450
451 /*
452 * Queues.
453 */
454 pdmR3QueueRelocate(pVM, offDelta);
455 pVM->pdm.s.pDevHlpQueueRC = PDMQueueRCPtr(pVM->pdm.s.pDevHlpQueueR3);
456
457 /*
458 * Critical sections.
459 */
460 pdmR3CritSectRelocate(pVM);
461
462 /*
463 * The registered PIC.
464 */
465 if (pVM->pdm.s.Pic.pDevInsRC)
466 {
467 pVM->pdm.s.Pic.pDevInsRC += offDelta;
468 pVM->pdm.s.Pic.pfnSetIrqRC += offDelta;
469 pVM->pdm.s.Pic.pfnGetInterruptRC += offDelta;
470 }
471
472 /*
473 * The registered APIC.
474 */
475 if (pVM->pdm.s.Apic.pDevInsRC)
476 {
477 pVM->pdm.s.Apic.pDevInsRC += offDelta;
478 pVM->pdm.s.Apic.pfnGetInterruptRC += offDelta;
479 pVM->pdm.s.Apic.pfnSetBaseRC += offDelta;
480 pVM->pdm.s.Apic.pfnGetBaseRC += offDelta;
481 pVM->pdm.s.Apic.pfnSetTPRRC += offDelta;
482 pVM->pdm.s.Apic.pfnGetTPRRC += offDelta;
483 pVM->pdm.s.Apic.pfnBusDeliverRC += offDelta;
484 if (pVM->pdm.s.Apic.pfnLocalInterruptRC)
485 pVM->pdm.s.Apic.pfnLocalInterruptRC += offDelta;
486 pVM->pdm.s.Apic.pfnWriteMSRRC += offDelta;
487 pVM->pdm.s.Apic.pfnReadMSRRC += offDelta;
488 }
489
490 /*
491 * The registered I/O APIC.
492 */
493 if (pVM->pdm.s.IoApic.pDevInsRC)
494 {
495 pVM->pdm.s.IoApic.pDevInsRC += offDelta;
496 pVM->pdm.s.IoApic.pfnSetIrqRC += offDelta;
497 if (pVM->pdm.s.IoApic.pfnSendMsiRC)
498 pVM->pdm.s.IoApic.pfnSendMsiRC += offDelta;
499 }
500
501 /*
502 * The register PCI Buses.
503 */
504 for (unsigned i = 0; i < RT_ELEMENTS(pVM->pdm.s.aPciBuses); i++)
505 {
506 if (pVM->pdm.s.aPciBuses[i].pDevInsRC)
507 {
508 pVM->pdm.s.aPciBuses[i].pDevInsRC += offDelta;
509 pVM->pdm.s.aPciBuses[i].pfnSetIrqRC += offDelta;
510 }
511 }
512
513 /*
514 * Devices & Drivers.
515 */
516 PCPDMDEVHLPRC pDevHlpRC;
517 int rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_pdmRCDevHlp", &pDevHlpRC);
518 AssertReleaseMsgRC(rc, ("rc=%Rrc when resolving g_pdmRCDevHlp\n", rc));
519
520 PCPDMDRVHLPRC pDrvHlpRC;
521 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_pdmRCDevHlp", &pDrvHlpRC);
522 AssertReleaseMsgRC(rc, ("rc=%Rrc when resolving g_pdmRCDevHlp\n", rc));
523
524 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
525 {
526 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_RC)
527 {
528 pDevIns->pHlpRC = pDevHlpRC;
529 pDevIns->pvInstanceDataRC = MMHyperR3ToRC(pVM, pDevIns->pvInstanceDataR3);
530 if (pDevIns->pCritSectRoR3)
531 pDevIns->pCritSectRoRC = MMHyperR3ToRC(pVM, pDevIns->pCritSectRoR3);
532 pDevIns->Internal.s.pVMRC = pVM->pVMRC;
533 if (pDevIns->Internal.s.pPciBusR3)
534 pDevIns->Internal.s.pPciBusRC = MMHyperR3ToRC(pVM, pDevIns->Internal.s.pPciBusR3);
535 if (pDevIns->Internal.s.pPciDeviceR3)
536 pDevIns->Internal.s.pPciDeviceRC = MMHyperR3ToRC(pVM, pDevIns->Internal.s.pPciDeviceR3);
537 if (pDevIns->pReg->pfnRelocate)
538 {
539 LogFlow(("PDMR3Relocate: Relocating device '%s'/%d\n",
540 pDevIns->pReg->szName, pDevIns->iInstance));
541 pDevIns->pReg->pfnRelocate(pDevIns, offDelta);
542 }
543 }
544
545 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
546 {
547 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
548 {
549 if (pDrvIns->pReg->fFlags & PDM_DRVREG_FLAGS_RC)
550 {
551 pDrvIns->pHlpRC = pDrvHlpRC;
552 pDrvIns->pvInstanceDataRC = MMHyperR3ToRC(pVM, pDrvIns->pvInstanceDataR3);
553 pDrvIns->Internal.s.pVMRC = pVM->pVMRC;
554 if (pDrvIns->pReg->pfnRelocate)
555 {
556 LogFlow(("PDMR3Relocate: Relocating driver '%s'/%u attached to '%s'/%d/%u\n",
557 pDrvIns->pReg->szName, pDrvIns->iInstance,
558 pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun));
559 pDrvIns->pReg->pfnRelocate(pDrvIns, offDelta);
560 }
561 }
562 }
563 }
564
565 }
566}
567
568
569/**
570 * Worker for pdmR3Term that terminates a LUN chain.
571 *
572 * @param pVM Pointer to the VM.
573 * @param pLun The head of the chain.
574 * @param pszDevice The name of the device (for logging).
575 * @param iInstance The device instance number (for logging).
576 */
577static void pdmR3TermLuns(PVM pVM, PPDMLUN pLun, const char *pszDevice, unsigned iInstance)
578{
579 for (; pLun; pLun = pLun->pNext)
580 {
581 /*
582 * Destroy them one at a time from the bottom up.
583 * (The serial device/drivers depends on this - bad.)
584 */
585 PPDMDRVINS pDrvIns = pLun->pBottom;
586 pLun->pBottom = pLun->pTop = NULL;
587 while (pDrvIns)
588 {
589 PPDMDRVINS pDrvNext = pDrvIns->Internal.s.pUp;
590
591 if (pDrvIns->pReg->pfnDestruct)
592 {
593 LogFlow(("pdmR3DevTerm: Destroying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
594 pDrvIns->pReg->szName, pDrvIns->iInstance, pLun->iLun, pszDevice, iInstance));
595 pDrvIns->pReg->pfnDestruct(pDrvIns);
596 }
597 pDrvIns->Internal.s.pDrv->cInstances--;
598
599 TMR3TimerDestroyDriver(pVM, pDrvIns);
600 //PDMR3QueueDestroyDriver(pVM, pDrvIns);
601 //pdmR3ThreadDestroyDriver(pVM, pDrvIns);
602 SSMR3DeregisterDriver(pVM, pDrvIns, NULL, 0);
603
604 pDrvIns = pDrvNext;
605 }
606 }
607}
608
609
610/**
611 * Terminates the PDM.
612 *
613 * Termination means cleaning up and freeing all resources,
614 * the VM it self is at this point powered off or suspended.
615 *
616 * @returns VBox status code.
617 * @param pVM The VM to operate on.
618 */
619VMMR3DECL(int) PDMR3Term(PVM pVM)
620{
621 LogFlow(("PDMR3Term:\n"));
622 AssertMsg(PDMCritSectIsInitialized(&pVM->pdm.s.CritSect), ("bad init order!\n"));
623
624 /*
625 * Iterate the device instances and attach drivers, doing
626 * relevant destruction processing.
627 *
628 * N.B. There is no need to mess around freeing memory allocated
629 * from any MM heap since MM will do that in its Term function.
630 */
631 /* usb ones first. */
632 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
633 {
634 pdmR3TermLuns(pVM, pUsbIns->Internal.s.pLuns, pUsbIns->pReg->szName, pUsbIns->iInstance);
635
636 if (pUsbIns->pReg->pfnDestruct)
637 {
638 LogFlow(("pdmR3DevTerm: Destroying - device '%s'/%d\n",
639 pUsbIns->pReg->szName, pUsbIns->iInstance));
640 pUsbIns->pReg->pfnDestruct(pUsbIns);
641 }
642
643 //TMR3TimerDestroyUsb(pVM, pUsbIns);
644 //SSMR3DeregisterUsb(pVM, pUsbIns, NULL, 0);
645 pdmR3ThreadDestroyUsb(pVM, pUsbIns);
646 }
647
648 /* then the 'normal' ones. */
649 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
650 {
651 pdmR3TermLuns(pVM, pDevIns->Internal.s.pLunsR3, pDevIns->pReg->szName, pDevIns->iInstance);
652
653 if (pDevIns->pReg->pfnDestruct)
654 {
655 LogFlow(("pdmR3DevTerm: Destroying - device '%s'/%d\n",
656 pDevIns->pReg->szName, pDevIns->iInstance));
657 pDevIns->pReg->pfnDestruct(pDevIns);
658 }
659
660 TMR3TimerDestroyDevice(pVM, pDevIns);
661 //SSMR3DeregisterDriver(pVM, pDevIns, NULL, 0);
662 pdmR3CritSectDeleteDevice(pVM, pDevIns);
663 //pdmR3ThreadDestroyDevice(pVM, pDevIns);
664 //PDMR3QueueDestroyDevice(pVM, pDevIns);
665 PGMR3PhysMMIO2Deregister(pVM, pDevIns, UINT32_MAX);
666 }
667
668 /*
669 * Destroy all threads.
670 */
671 pdmR3ThreadDestroyAll(pVM);
672
673 /*
674 * Destroy the block cache.
675 */
676 pdmR3BlkCacheTerm(pVM);
677
678#ifdef VBOX_WITH_NETSHAPER
679 /*
680 * Destroy network bandwidth groups.
681 */
682 pdmR3NetShaperTerm(pVM);
683#endif
684#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
685 /*
686 * Free async completion managers.
687 */
688 pdmR3AsyncCompletionTerm(pVM);
689#endif
690
691 /*
692 * Free modules.
693 */
694 pdmR3LdrTermU(pVM->pUVM);
695
696 /*
697 * Destroy the PDM lock.
698 */
699 PDMR3CritSectDelete(&pVM->pdm.s.CritSect);
700 /* The MiscCritSect is deleted by PDMR3CritSectTerm. */
701
702 LogFlow(("PDMR3Term: returns %Rrc\n", VINF_SUCCESS));
703 return VINF_SUCCESS;
704}
705
706
707/**
708 * Terminates the PDM part of the UVM.
709 *
710 * This will unload any modules left behind.
711 *
712 * @param pUVM Pointer to the user mode VM structure.
713 */
714VMMR3DECL(void) PDMR3TermUVM(PUVM pUVM)
715{
716 /*
717 * In the normal cause of events we will now call pdmR3LdrTermU for
718 * the second time. In the case of init failure however, this might
719 * the first time, which is why we do it.
720 */
721 pdmR3LdrTermU(pUVM);
722
723 Assert(pUVM->pdm.s.pCritSects == NULL);
724 RTCritSectDelete(&pUVM->pdm.s.ListCritSect);
725}
726
727
728/**
729 * Bits that are saved in pass 0 and in the final pass.
730 *
731 * @param pVM Pointer to the VM.
732 * @param pSSM The saved state handle.
733 */
734static void pdmR3SaveBoth(PVM pVM, PSSMHANDLE pSSM)
735{
736 /*
737 * Save the list of device instances so we can check that they're all still
738 * there when we load the state and that nothing new has been added.
739 */
740 uint32_t i = 0;
741 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3, i++)
742 {
743 SSMR3PutU32(pSSM, i);
744 SSMR3PutStrZ(pSSM, pDevIns->pReg->szName);
745 SSMR3PutU32(pSSM, pDevIns->iInstance);
746 }
747 SSMR3PutU32(pSSM, UINT32_MAX); /* terminator */
748}
749
750
751/**
752 * Live save.
753 *
754 * @returns VBox status code.
755 * @param pVM Pointer to the VM.
756 * @param pSSM The saved state handle.
757 * @param uPass The pass.
758 */
759static DECLCALLBACK(int) pdmR3LiveExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uPass)
760{
761 LogFlow(("pdmR3LiveExec:\n"));
762 AssertReturn(uPass == 0, VERR_SSM_UNEXPECTED_PASS);
763 pdmR3SaveBoth(pVM, pSSM);
764 return VINF_SSM_DONT_CALL_AGAIN;
765}
766
767
768/**
769 * Execute state save operation.
770 *
771 * @returns VBox status code.
772 * @param pVM Pointer to the VM.
773 * @param pSSM The saved state handle.
774 */
775static DECLCALLBACK(int) pdmR3SaveExec(PVM pVM, PSSMHANDLE pSSM)
776{
777 LogFlow(("pdmR3SaveExec:\n"));
778
779 /*
780 * Save interrupt and DMA states.
781 */
782 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
783 {
784 PVMCPU pVCpu = &pVM->aCpus[idCpu];
785 SSMR3PutU32(pSSM, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_APIC));
786 SSMR3PutU32(pSSM, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_PIC));
787 SSMR3PutU32(pSSM, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_NMI));
788 SSMR3PutU32(pSSM, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_SMI));
789 }
790 SSMR3PutU32(pSSM, VM_FF_ISSET(pVM, VM_FF_PDM_DMA));
791
792 pdmR3SaveBoth(pVM, pSSM);
793 return VINF_SUCCESS;
794}
795
796
797/**
798 * Prepare state load operation.
799 *
800 * This will dispatch pending operations and clear the FFs governed by PDM and its devices.
801 *
802 * @returns VBox status code.
803 * @param pVM Pointer to the VM.
804 * @param pSSM The SSM handle.
805 */
806static DECLCALLBACK(int) pdmR3LoadPrep(PVM pVM, PSSMHANDLE pSSM)
807{
808 LogFlow(("pdmR3LoadPrep: %s%s\n",
809 VM_FF_ISSET(pVM, VM_FF_PDM_QUEUES) ? " VM_FF_PDM_QUEUES" : "",
810 VM_FF_ISSET(pVM, VM_FF_PDM_DMA) ? " VM_FF_PDM_DMA" : ""));
811#ifdef LOG_ENABLED
812 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
813 {
814 PVMCPU pVCpu = &pVM->aCpus[idCpu];
815 LogFlow(("pdmR3LoadPrep: VCPU %u %s%s\n", idCpu,
816 VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_APIC) ? " VMCPU_FF_INTERRUPT_APIC" : "",
817 VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_PIC) ? " VMCPU_FF_INTERRUPT_PIC" : ""));
818 }
819#endif
820 NOREF(pSSM);
821
822 /*
823 * In case there is work pending that will raise an interrupt,
824 * start a DMA transfer, or release a lock. (unlikely)
825 */
826 if (VM_FF_ISSET(pVM, VM_FF_PDM_QUEUES))
827 PDMR3QueueFlushAll(pVM);
828
829 /* Clear the FFs. */
830 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
831 {
832 PVMCPU pVCpu = &pVM->aCpus[idCpu];
833 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_APIC);
834 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_PIC);
835 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_NMI);
836 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_SMI);
837 }
838 VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
839
840 return VINF_SUCCESS;
841}
842
843
844/**
845 * Execute state load operation.
846 *
847 * @returns VBox status code.
848 * @param pVM Pointer to the VM.
849 * @param pSSM SSM operation handle.
850 * @param uVersion Data layout version.
851 * @param uPass The data pass.
852 */
853static DECLCALLBACK(int) pdmR3LoadExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
854{
855 int rc;
856
857 LogFlow(("pdmR3LoadExec: uPass=%#x\n", uPass));
858
859 /*
860 * Validate version.
861 */
862 if ( uVersion != PDM_SAVED_STATE_VERSION
863 && uVersion != PDM_SAVED_STATE_VERSION_PRE_NMI_FF)
864 {
865 AssertMsgFailed(("Invalid version uVersion=%d!\n", uVersion));
866 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
867 }
868
869 if (uPass == SSM_PASS_FINAL)
870 {
871 /*
872 * Load the interrupt and DMA states.
873 */
874 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
875 {
876 PVMCPU pVCpu = &pVM->aCpus[idCpu];
877
878 /* APIC interrupt */
879 uint32_t fInterruptPending = 0;
880 rc = SSMR3GetU32(pSSM, &fInterruptPending);
881 if (RT_FAILURE(rc))
882 return rc;
883 if (fInterruptPending & ~1)
884 {
885 AssertMsgFailed(("fInterruptPending=%#x (APIC)\n", fInterruptPending));
886 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
887 }
888 AssertRelease(!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_APIC));
889 if (fInterruptPending)
890 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC);
891
892 /* PIC interrupt */
893 fInterruptPending = 0;
894 rc = SSMR3GetU32(pSSM, &fInterruptPending);
895 if (RT_FAILURE(rc))
896 return rc;
897 if (fInterruptPending & ~1)
898 {
899 AssertMsgFailed(("fInterruptPending=%#x (PIC)\n", fInterruptPending));
900 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
901 }
902 AssertRelease(!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_PIC));
903 if (fInterruptPending)
904 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_PIC);
905
906 if (uVersion > PDM_SAVED_STATE_VERSION_PRE_NMI_FF)
907 {
908 /* NMI interrupt */
909 fInterruptPending = 0;
910 rc = SSMR3GetU32(pSSM, &fInterruptPending);
911 if (RT_FAILURE(rc))
912 return rc;
913 if (fInterruptPending & ~1)
914 {
915 AssertMsgFailed(("fInterruptPending=%#x (NMI)\n", fInterruptPending));
916 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
917 }
918 AssertRelease(!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_NMI));
919 if (fInterruptPending)
920 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_NMI);
921
922 /* SMI interrupt */
923 fInterruptPending = 0;
924 rc = SSMR3GetU32(pSSM, &fInterruptPending);
925 if (RT_FAILURE(rc))
926 return rc;
927 if (fInterruptPending & ~1)
928 {
929 AssertMsgFailed(("fInterruptPending=%#x (SMI)\n", fInterruptPending));
930 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
931 }
932 AssertRelease(!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_SMI));
933 if (fInterruptPending)
934 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_SMI);
935 }
936 }
937
938 /* DMA pending */
939 uint32_t fDMAPending = 0;
940 rc = SSMR3GetU32(pSSM, &fDMAPending);
941 if (RT_FAILURE(rc))
942 return rc;
943 if (fDMAPending & ~1)
944 {
945 AssertMsgFailed(("fDMAPending=%#x\n", fDMAPending));
946 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
947 }
948 if (fDMAPending)
949 VM_FF_SET(pVM, VM_FF_PDM_DMA);
950 Log(("pdmR3LoadExec: VM_FF_PDM_DMA=%RTbool\n", VM_FF_ISSET(pVM, VM_FF_PDM_DMA)));
951 }
952
953 /*
954 * Load the list of devices and verify that they are all there.
955 */
956 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
957 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_FOUND;
958
959 for (uint32_t i = 0; ; i++)
960 {
961 /* Get the sequence number / terminator. */
962 uint32_t u32Sep;
963 rc = SSMR3GetU32(pSSM, &u32Sep);
964 if (RT_FAILURE(rc))
965 return rc;
966 if (u32Sep == UINT32_MAX)
967 break;
968 if (u32Sep != i)
969 AssertMsgFailedReturn(("Out of sequence. u32Sep=%#x i=%#x\n", u32Sep, i), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
970
971 /* Get the name and instance number. */
972 char szName[RT_SIZEOFMEMB(PDMDEVREG, szName)];
973 rc = SSMR3GetStrZ(pSSM, szName, sizeof(szName));
974 if (RT_FAILURE(rc))
975 return rc;
976 uint32_t iInstance;
977 rc = SSMR3GetU32(pSSM, &iInstance);
978 if (RT_FAILURE(rc))
979 return rc;
980
981 /* Try locate it. */
982 PPDMDEVINS pDevIns;
983 for (pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
984 if ( !strcmp(szName, pDevIns->pReg->szName)
985 && pDevIns->iInstance == iInstance)
986 {
987 AssertLogRelMsgReturn(!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_FOUND),
988 ("%s/#%u\n", pDevIns->pReg->szName, pDevIns->iInstance),
989 VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
990 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_FOUND;
991 break;
992 }
993 if (!pDevIns)
994 {
995 LogRel(("Device '%s'/%d not found in current config\n", szName, iInstance));
996 if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
997 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Device '%s'/%d not found in current config"), szName, iInstance);
998 }
999 }
1000
1001 /*
1002 * Check that no additional devices were configured.
1003 */
1004 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1005 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_FOUND))
1006 {
1007 LogRel(("Device '%s'/%d not found in the saved state\n", pDevIns->pReg->szName, pDevIns->iInstance));
1008 if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
1009 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Device '%s'/%d not found in the saved state"),
1010 pDevIns->pReg->szName, pDevIns->iInstance);
1011 }
1012
1013 return VINF_SUCCESS;
1014}
1015
1016
1017/**
1018 * Worker for PDMR3PowerOn that deals with one driver.
1019 *
1020 * @param pDrvIns The driver instance.
1021 * @param pszDevName The parent device name.
1022 * @param iDevInstance The parent device instance number.
1023 * @param iLun The parent LUN number.
1024 */
1025DECLINLINE(int) pdmR3PowerOnDrv(PPDMDRVINS pDrvIns, const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1026{
1027 Assert(pDrvIns->Internal.s.fVMSuspended);
1028 if (pDrvIns->pReg->pfnPowerOn)
1029 {
1030 LogFlow(("PDMR3PowerOn: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1031 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1032 int rc = VINF_SUCCESS; pDrvIns->pReg->pfnPowerOn(pDrvIns);
1033 if (RT_FAILURE(rc))
1034 {
1035 LogRel(("PDMR3PowerOn: driver '%s'/%d on LUN#%d of device '%s'/%d -> %Rrc\n",
1036 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, rc));
1037 return rc;
1038 }
1039 }
1040 pDrvIns->Internal.s.fVMSuspended = false;
1041 return VINF_SUCCESS;
1042}
1043
1044
1045/**
1046 * Worker for PDMR3PowerOn that deals with one USB device instance.
1047 *
1048 * @returns VBox status code.
1049 * @param pUsbIns The USB device instance.
1050 */
1051DECLINLINE(int) pdmR3PowerOnUsb(PPDMUSBINS pUsbIns)
1052{
1053 Assert(pUsbIns->Internal.s.fVMSuspended);
1054 if (pUsbIns->pReg->pfnVMPowerOn)
1055 {
1056 LogFlow(("PDMR3PowerOn: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1057 int rc = VINF_SUCCESS; pUsbIns->pReg->pfnVMPowerOn(pUsbIns);
1058 if (RT_FAILURE(rc))
1059 {
1060 LogRel(("PDMR3PowerOn: device '%s'/%d -> %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1061 return rc;
1062 }
1063 }
1064 pUsbIns->Internal.s.fVMSuspended = false;
1065 return VINF_SUCCESS;
1066}
1067
1068
1069/**
1070 * Worker for PDMR3PowerOn that deals with one device instance.
1071 *
1072 * @returns VBox status code.
1073 * @param pDevIns The device instance.
1074 */
1075DECLINLINE(int) pdmR3PowerOnDev(PPDMDEVINS pDevIns)
1076{
1077 Assert(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED);
1078 if (pDevIns->pReg->pfnPowerOn)
1079 {
1080 LogFlow(("PDMR3PowerOn: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1081 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1082 int rc = VINF_SUCCESS; pDevIns->pReg->pfnPowerOn(pDevIns);
1083 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1084 if (RT_FAILURE(rc))
1085 {
1086 LogRel(("PDMR3PowerOn: device '%s'/%d -> %Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
1087 return rc;
1088 }
1089 }
1090 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
1091 return VINF_SUCCESS;
1092}
1093
1094
1095/**
1096 * This function will notify all the devices and their
1097 * attached drivers about the VM now being powered on.
1098 *
1099 * @param pVM Pointer to the VM.
1100 */
1101VMMR3DECL(void) PDMR3PowerOn(PVM pVM)
1102{
1103 LogFlow(("PDMR3PowerOn:\n"));
1104
1105 /*
1106 * Iterate thru the device instances and USB device instances,
1107 * processing the drivers associated with those.
1108 */
1109 int rc = VINF_SUCCESS;
1110 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns && RT_SUCCESS(rc); pDevIns = pDevIns->Internal.s.pNextR3)
1111 {
1112 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1113 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1114 rc = pdmR3PowerOnDrv(pDrvIns, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun);
1115 if (RT_SUCCESS(rc))
1116 rc = pdmR3PowerOnDev(pDevIns);
1117 }
1118
1119#ifdef VBOX_WITH_USB
1120 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns && RT_SUCCESS(rc); pUsbIns = pUsbIns->Internal.s.pNext)
1121 {
1122 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1123 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1124 rc = pdmR3PowerOnDrv(pDrvIns, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun);
1125 if (RT_SUCCESS(rc))
1126 rc = pdmR3PowerOnUsb(pUsbIns);
1127 }
1128#endif
1129
1130#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
1131 pdmR3AsyncCompletionResume(pVM);
1132#endif
1133
1134 /*
1135 * Resume all threads.
1136 */
1137 if (RT_SUCCESS(rc))
1138 pdmR3ThreadResumeAll(pVM);
1139
1140 /*
1141 * On failure, clean up via PDMR3Suspend.
1142 */
1143 if (RT_FAILURE(rc))
1144 PDMR3Suspend(pVM);
1145
1146 LogFlow(("PDMR3PowerOn: returns %Rrc\n", rc));
1147 return /*rc*/;
1148}
1149
1150
1151/**
1152 * Initializes the asynchronous notifi stats structure.
1153 *
1154 * @param pThis The asynchronous notifification stats.
1155 * @param pszOp The name of the operation.
1156 */
1157static void pdmR3NotifyAsyncInit(PPDMNOTIFYASYNCSTATS pThis, const char *pszOp)
1158{
1159 pThis->uStartNsTs = RTTimeNanoTS();
1160 pThis->cNsElapsedNextLog = 0;
1161 pThis->cLoops = 0;
1162 pThis->cAsync = 0;
1163 pThis->pszOp = pszOp;
1164 pThis->offList = 0;
1165 pThis->szList[0] = '\0';
1166}
1167
1168
1169/**
1170 * Begin a new loop, prepares to gather new stats.
1171 *
1172 * @param pThis The asynchronous notifification stats.
1173 */
1174static void pdmR3NotifyAsyncBeginLoop(PPDMNOTIFYASYNCSTATS pThis)
1175{
1176 pThis->cLoops++;
1177 pThis->cAsync = 0;
1178 pThis->offList = 0;
1179 pThis->szList[0] = '\0';
1180}
1181
1182
1183/**
1184 * Records a device or USB device with a pending asynchronous notification.
1185 *
1186 * @param pThis The asynchronous notifification stats.
1187 * @param pszName The name of the thing.
1188 * @param iInstance The instance number.
1189 */
1190static void pdmR3NotifyAsyncAdd(PPDMNOTIFYASYNCSTATS pThis, const char *pszName, uint32_t iInstance)
1191{
1192 pThis->cAsync++;
1193 if (pThis->offList < sizeof(pThis->szList) - 4)
1194 pThis->offList += RTStrPrintf(&pThis->szList[pThis->offList], sizeof(pThis->szList) - pThis->offList,
1195 pThis->offList == 0 ? "%s/%u" : ", %s/%u",
1196 pszName, iInstance);
1197}
1198
1199
1200/**
1201 * Records the asynchronous completition of a reset, suspend or power off.
1202 *
1203 * @param pThis The asynchronous notifification stats.
1204 * @param pszDrvName The driver name.
1205 * @param iDrvInstance The driver instance number.
1206 * @param pszDevName The device or USB device name.
1207 * @param iDevInstance The device or USB device instance number.
1208 * @param iLun The LUN.
1209 */
1210static void pdmR3NotifyAsyncAddDrv(PPDMNOTIFYASYNCSTATS pThis, const char *pszDrvName, uint32_t iDrvInstance,
1211 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1212{
1213 pThis->cAsync++;
1214 if (pThis->offList < sizeof(pThis->szList) - 8)
1215 pThis->offList += RTStrPrintf(&pThis->szList[pThis->offList], sizeof(pThis->szList) - pThis->offList,
1216 pThis->offList == 0 ? "%s/%u/%u/%s/%u" : ", %s/%u/%u/%s/%u",
1217 pszDevName, iDevInstance, iLun, pszDrvName, iDrvInstance);
1218}
1219
1220
1221/**
1222 * Log the stats.
1223 *
1224 * @param pThis The asynchronous notifification stats.
1225 */
1226static void pdmR3NotifyAsyncLog(PPDMNOTIFYASYNCSTATS pThis)
1227{
1228 /*
1229 * Return if we shouldn't log at this point.
1230 * We log with an internval increasing from 0 sec to 60 sec.
1231 */
1232 if (!pThis->cAsync)
1233 return;
1234
1235 uint64_t cNsElapsed = RTTimeNanoTS() - pThis->uStartNsTs;
1236 if (cNsElapsed < pThis->cNsElapsedNextLog)
1237 return;
1238
1239 if (pThis->cNsElapsedNextLog == 0)
1240 pThis->cNsElapsedNextLog = RT_NS_1SEC;
1241 else if (pThis->cNsElapsedNextLog >= RT_NS_1MIN / 2)
1242 pThis->cNsElapsedNextLog = RT_NS_1MIN;
1243 else
1244 pThis->cNsElapsedNextLog *= 2;
1245
1246 /*
1247 * Do the logging.
1248 */
1249 LogRel(("%s: after %5llu ms, %u loops: %u async tasks - %s\n",
1250 pThis->pszOp, cNsElapsed / RT_NS_1MS, pThis->cLoops, pThis->cAsync, pThis->szList));
1251}
1252
1253
1254/**
1255 * Wait for events and process pending requests.
1256 *
1257 * @param pThis The asynchronous notifification stats.
1258 * @param pVM Pointer to the VM.
1259 */
1260static void pdmR3NotifyAsyncWaitAndProcessRequests(PPDMNOTIFYASYNCSTATS pThis, PVM pVM)
1261{
1262 VM_ASSERT_EMT0(pVM);
1263 int rc = VMR3AsyncPdmNotificationWaitU(&pVM->pUVM->aCpus[0]);
1264 AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc - %s - %s\n", rc, pThis->pszOp, pThis->szList));
1265
1266 rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY, true /*fPriorityOnly*/);
1267 AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc - %s - %s\n", rc, pThis->pszOp, pThis->szList));
1268 rc = VMR3ReqProcessU(pVM->pUVM, 0/*idDstCpu*/, true /*fPriorityOnly*/);
1269 AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc - %s - %s\n", rc, pThis->pszOp, pThis->szList));
1270}
1271
1272
1273/**
1274 * Worker for PDMR3Reset that deals with one driver.
1275 *
1276 * @param pDrvIns The driver instance.
1277 * @param pAsync The structure for recording asynchronous
1278 * notification tasks.
1279 * @param pszDevName The parent device name.
1280 * @param iDevInstance The parent device instance number.
1281 * @param iLun The parent LUN number.
1282 */
1283DECLINLINE(bool) pdmR3ResetDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
1284 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1285{
1286 if (!pDrvIns->Internal.s.fVMReset)
1287 {
1288 pDrvIns->Internal.s.fVMReset = true;
1289 if (pDrvIns->pReg->pfnReset)
1290 {
1291 if (!pDrvIns->Internal.s.pfnAsyncNotify)
1292 {
1293 LogFlow(("PDMR3Reset: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1294 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1295 pDrvIns->pReg->pfnReset(pDrvIns);
1296 if (pDrvIns->Internal.s.pfnAsyncNotify)
1297 LogFlow(("PDMR3Reset: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1298 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1299 }
1300 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
1301 {
1302 LogFlow(("PDMR3Reset: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1303 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1304 pDrvIns->Internal.s.pfnAsyncNotify = NULL;
1305 }
1306 if (pDrvIns->Internal.s.pfnAsyncNotify)
1307 {
1308 pDrvIns->Internal.s.fVMReset = false;
1309 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance,
1310 pszDevName, iDevInstance, iLun);
1311 return false;
1312 }
1313 }
1314 }
1315 return true;
1316}
1317
1318
1319/**
1320 * Worker for PDMR3Reset that deals with one USB device instance.
1321 *
1322 * @param pUsbIns The USB device instance.
1323 * @param pAsync The structure for recording asynchronous
1324 * notification tasks.
1325 */
1326DECLINLINE(void) pdmR3ResetUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
1327{
1328 if (!pUsbIns->Internal.s.fVMReset)
1329 {
1330 pUsbIns->Internal.s.fVMReset = true;
1331 if (pUsbIns->pReg->pfnVMReset)
1332 {
1333 if (!pUsbIns->Internal.s.pfnAsyncNotify)
1334 {
1335 LogFlow(("PDMR3Reset: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1336 pUsbIns->pReg->pfnVMReset(pUsbIns);
1337 if (pUsbIns->Internal.s.pfnAsyncNotify)
1338 LogFlow(("PDMR3Reset: Async notification started - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1339 }
1340 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
1341 {
1342 LogFlow(("PDMR3Reset: Async notification completed - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1343 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
1344 }
1345 if (pUsbIns->Internal.s.pfnAsyncNotify)
1346 {
1347 pUsbIns->Internal.s.fVMReset = false;
1348 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
1349 }
1350 }
1351 }
1352}
1353
1354
1355/**
1356 * Worker for PDMR3Reset that deals with one device instance.
1357 *
1358 * @param pDevIns The device instance.
1359 * @param pAsync The structure for recording asynchronous
1360 * notification tasks.
1361 */
1362DECLINLINE(void) pdmR3ResetDev(PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
1363{
1364 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_RESET))
1365 {
1366 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_RESET;
1367 if (pDevIns->pReg->pfnReset)
1368 {
1369 uint64_t cNsElapsed = RTTimeNanoTS();
1370 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1371
1372 if (!pDevIns->Internal.s.pfnAsyncNotify)
1373 {
1374 LogFlow(("PDMR3Reset: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1375 pDevIns->pReg->pfnReset(pDevIns);
1376 if (pDevIns->Internal.s.pfnAsyncNotify)
1377 LogFlow(("PDMR3Reset: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1378 }
1379 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
1380 {
1381 LogFlow(("PDMR3Reset: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1382 pDevIns->Internal.s.pfnAsyncNotify = NULL;
1383 }
1384 if (pDevIns->Internal.s.pfnAsyncNotify)
1385 {
1386 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_RESET;
1387 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
1388 }
1389
1390 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1391 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1392 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1393 LogRel(("PDMR3Reset: device '%s'/%d took %'llu ns to reset\n",
1394 pDevIns->pReg->szName, pDevIns->iInstance, cNsElapsed));
1395 }
1396 }
1397}
1398
1399
1400/**
1401 * Resets a virtual CPU.
1402 *
1403 * Used by PDMR3Reset and CPU hot plugging.
1404 *
1405 * @param pVCpu The virtual CPU handle.
1406 */
1407VMMR3DECL(void) PDMR3ResetCpu(PVMCPU pVCpu)
1408{
1409 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_APIC);
1410 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_PIC);
1411 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_NMI);
1412 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_SMI);
1413}
1414
1415
1416/**
1417 * This function will notify all the devices and their attached drivers about
1418 * the VM now being reset.
1419 *
1420 * @param pVM Pointer to the VM.
1421 */
1422VMMR3DECL(void) PDMR3Reset(PVM pVM)
1423{
1424 LogFlow(("PDMR3Reset:\n"));
1425
1426 /*
1427 * Clear all the reset flags.
1428 */
1429 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1430 {
1431 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_RESET;
1432 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1433 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1434 pDrvIns->Internal.s.fVMReset = false;
1435 }
1436#ifdef VBOX_WITH_USB
1437 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1438 {
1439 pUsbIns->Internal.s.fVMReset = false;
1440 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1441 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1442 pDrvIns->Internal.s.fVMReset = false;
1443 }
1444#endif
1445
1446 /*
1447 * The outer loop repeats until there are no more async requests.
1448 */
1449 PDMNOTIFYASYNCSTATS Async;
1450 pdmR3NotifyAsyncInit(&Async, "PDMR3Reset");
1451 for (;;)
1452 {
1453 pdmR3NotifyAsyncBeginLoop(&Async);
1454
1455 /*
1456 * Iterate thru the device instances and USB device instances,
1457 * processing the drivers associated with those.
1458 */
1459 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1460 {
1461 unsigned const cAsyncStart = Async.cAsync;
1462
1463 if (Async.cAsync == cAsyncStart)
1464 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1465 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1466 if (!pdmR3ResetDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
1467 break;
1468
1469 if (Async.cAsync == cAsyncStart)
1470 pdmR3ResetDev(pDevIns, &Async);
1471 }
1472
1473#ifdef VBOX_WITH_USB
1474 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1475 {
1476 unsigned const cAsyncStart = Async.cAsync;
1477
1478 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1479 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1480 if (!pdmR3ResetDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
1481 break;
1482
1483 if (Async.cAsync == cAsyncStart)
1484 pdmR3ResetUsb(pUsbIns, &Async);
1485 }
1486#endif
1487 if (!Async.cAsync)
1488 break;
1489 pdmR3NotifyAsyncLog(&Async);
1490 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
1491 }
1492
1493 /*
1494 * Clear all pending interrupts and DMA operations.
1495 */
1496 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
1497 PDMR3ResetCpu(&pVM->aCpus[idCpu]);
1498 VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
1499
1500 LogFlow(("PDMR3Reset: returns void\n"));
1501}
1502
1503
1504/**
1505 * Worker for PDMR3Suspend that deals with one driver.
1506 *
1507 * @param pDrvIns The driver instance.
1508 * @param pAsync The structure for recording asynchronous
1509 * notification tasks.
1510 * @param pszDevName The parent device name.
1511 * @param iDevInstance The parent device instance number.
1512 * @param iLun The parent LUN number.
1513 */
1514DECLINLINE(bool) pdmR3SuspendDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
1515 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1516{
1517 if (!pDrvIns->Internal.s.fVMSuspended)
1518 {
1519 pDrvIns->Internal.s.fVMSuspended = true;
1520 if (pDrvIns->pReg->pfnSuspend)
1521 {
1522 uint64_t cNsElapsed = RTTimeNanoTS();
1523
1524 if (!pDrvIns->Internal.s.pfnAsyncNotify)
1525 {
1526 LogFlow(("PDMR3Suspend: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1527 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1528 pDrvIns->pReg->pfnSuspend(pDrvIns);
1529 if (pDrvIns->Internal.s.pfnAsyncNotify)
1530 LogFlow(("PDMR3Suspend: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1531 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1532 }
1533 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
1534 {
1535 LogFlow(("PDMR3Suspend: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1536 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1537 pDrvIns->Internal.s.pfnAsyncNotify = NULL;
1538 }
1539
1540 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1541 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1542 LogRel(("PDMR3Suspend: Driver '%s'/%d on LUN#%d of device '%s'/%d took %'llu ns to suspend\n",
1543 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, cNsElapsed));
1544
1545 if (pDrvIns->Internal.s.pfnAsyncNotify)
1546 {
1547 pDrvIns->Internal.s.fVMSuspended = false;
1548 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance, pszDevName, iDevInstance, iLun);
1549 return false;
1550 }
1551 }
1552 }
1553 return true;
1554}
1555
1556
1557/**
1558 * Worker for PDMR3Suspend that deals with one USB device instance.
1559 *
1560 * @param pUsbIns The USB device instance.
1561 * @param pAsync The structure for recording asynchronous
1562 * notification tasks.
1563 */
1564DECLINLINE(void) pdmR3SuspendUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
1565{
1566 if (!pUsbIns->Internal.s.fVMSuspended)
1567 {
1568 pUsbIns->Internal.s.fVMSuspended = true;
1569 if (pUsbIns->pReg->pfnVMSuspend)
1570 {
1571 uint64_t cNsElapsed = RTTimeNanoTS();
1572
1573 if (!pUsbIns->Internal.s.pfnAsyncNotify)
1574 {
1575 LogFlow(("PDMR3Suspend: Notifying - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1576 pUsbIns->pReg->pfnVMSuspend(pUsbIns);
1577 if (pUsbIns->Internal.s.pfnAsyncNotify)
1578 LogFlow(("PDMR3Suspend: Async notification started - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1579 }
1580 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
1581 {
1582 LogFlow(("PDMR3Suspend: Async notification completed - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1583 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
1584 }
1585 if (pUsbIns->Internal.s.pfnAsyncNotify)
1586 {
1587 pUsbIns->Internal.s.fVMSuspended = false;
1588 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
1589 }
1590
1591 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1592 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1593 LogRel(("PDMR3Suspend: USB device '%s'/%d took %'llu ns to suspend\n",
1594 pUsbIns->pReg->szName, pUsbIns->iInstance, cNsElapsed));
1595 }
1596 }
1597}
1598
1599
1600/**
1601 * Worker for PDMR3Suspend that deals with one device instance.
1602 *
1603 * @param pDevIns The device instance.
1604 * @param pAsync The structure for recording asynchronous
1605 * notification tasks.
1606 */
1607DECLINLINE(void) pdmR3SuspendDev(PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
1608{
1609 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED))
1610 {
1611 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_SUSPENDED;
1612 if (pDevIns->pReg->pfnSuspend)
1613 {
1614 uint64_t cNsElapsed = RTTimeNanoTS();
1615 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1616
1617 if (!pDevIns->Internal.s.pfnAsyncNotify)
1618 {
1619 LogFlow(("PDMR3Suspend: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1620 pDevIns->pReg->pfnSuspend(pDevIns);
1621 if (pDevIns->Internal.s.pfnAsyncNotify)
1622 LogFlow(("PDMR3Suspend: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1623 }
1624 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
1625 {
1626 LogFlow(("PDMR3Suspend: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1627 pDevIns->Internal.s.pfnAsyncNotify = NULL;
1628 }
1629 if (pDevIns->Internal.s.pfnAsyncNotify)
1630 {
1631 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
1632 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
1633 }
1634
1635 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1636 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1637 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1638 LogRel(("PDMR3Suspend: device '%s'/%d took %'llu ns to suspend\n",
1639 pDevIns->pReg->szName, pDevIns->iInstance, cNsElapsed));
1640 }
1641 }
1642}
1643
1644
1645/**
1646 * This function will notify all the devices and their attached drivers about
1647 * the VM now being suspended.
1648 *
1649 * @param pVM Pointer to the VM.
1650 * @thread EMT(0)
1651 */
1652VMMR3DECL(void) PDMR3Suspend(PVM pVM)
1653{
1654 LogFlow(("PDMR3Suspend:\n"));
1655 VM_ASSERT_EMT0(pVM);
1656 uint64_t cNsElapsed = RTTimeNanoTS();
1657
1658 /*
1659 * The outer loop repeats until there are no more async requests.
1660 *
1661 * Note! We depend on the suspended indicators to be in the desired state
1662 * and we do not reset them before starting because this allows
1663 * PDMR3PowerOn and PDMR3Resume to use PDMR3Suspend for cleaning up
1664 * on failure.
1665 */
1666 PDMNOTIFYASYNCSTATS Async;
1667 pdmR3NotifyAsyncInit(&Async, "PDMR3Suspend");
1668 for (;;)
1669 {
1670 pdmR3NotifyAsyncBeginLoop(&Async);
1671
1672 /*
1673 * Iterate thru the device instances and USB device instances,
1674 * processing the drivers associated with those.
1675 *
1676 * The attached drivers are normally processed first. Some devices
1677 * (like DevAHCI) though needs to be notified before the drivers so
1678 * that it doesn't kick off any new requests after the drivers stopped
1679 * taking any. (DrvVD changes to read-only in this particular case.)
1680 */
1681 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1682 {
1683 unsigned const cAsyncStart = Async.cAsync;
1684
1685 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION)
1686 pdmR3SuspendDev(pDevIns, &Async);
1687
1688 if (Async.cAsync == cAsyncStart)
1689 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1690 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1691 if (!pdmR3SuspendDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
1692 break;
1693
1694 if ( Async.cAsync == cAsyncStart
1695 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION))
1696 pdmR3SuspendDev(pDevIns, &Async);
1697 }
1698
1699#ifdef VBOX_WITH_USB
1700 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1701 {
1702 unsigned const cAsyncStart = Async.cAsync;
1703
1704 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1705 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1706 if (!pdmR3SuspendDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
1707 break;
1708
1709 if (Async.cAsync == cAsyncStart)
1710 pdmR3SuspendUsb(pUsbIns, &Async);
1711 }
1712#endif
1713 if (!Async.cAsync)
1714 break;
1715 pdmR3NotifyAsyncLog(&Async);
1716 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
1717 }
1718
1719 /*
1720 * Suspend all threads.
1721 */
1722 pdmR3ThreadSuspendAll(pVM);
1723
1724 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1725 LogRel(("PDMR3Suspend: %'llu ns run time\n", cNsElapsed));
1726}
1727
1728
1729/**
1730 * Worker for PDMR3Resume that deals with one driver.
1731 *
1732 * @param pDrvIns The driver instance.
1733 * @param pszDevName The parent device name.
1734 * @param iDevInstance The parent device instance number.
1735 * @param iLun The parent LUN number.
1736 */
1737DECLINLINE(int) pdmR3ResumeDrv(PPDMDRVINS pDrvIns, const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1738{
1739 Assert(pDrvIns->Internal.s.fVMSuspended);
1740 if (pDrvIns->pReg->pfnResume)
1741 {
1742 LogFlow(("PDMR3Resume: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1743 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1744 int rc = VINF_SUCCESS; pDrvIns->pReg->pfnResume(pDrvIns);
1745 if (RT_FAILURE(rc))
1746 {
1747 LogRel(("PDMR3Resume: driver '%s'/%d on LUN#%d of device '%s'/%d -> %Rrc\n",
1748 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, rc));
1749 return rc;
1750 }
1751 }
1752 pDrvIns->Internal.s.fVMSuspended = false;
1753 return VINF_SUCCESS;
1754}
1755
1756
1757/**
1758 * Worker for PDMR3Resume that deals with one USB device instance.
1759 *
1760 * @returns VBox status code.
1761 * @param pUsbIns The USB device instance.
1762 */
1763DECLINLINE(int) pdmR3ResumeUsb(PPDMUSBINS pUsbIns)
1764{
1765 Assert(pUsbIns->Internal.s.fVMSuspended);
1766 if (pUsbIns->pReg->pfnVMResume)
1767 {
1768 LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1769 int rc = VINF_SUCCESS; pUsbIns->pReg->pfnVMResume(pUsbIns);
1770 if (RT_FAILURE(rc))
1771 {
1772 LogRel(("PDMR3Resume: device '%s'/%d -> %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1773 return rc;
1774 }
1775 }
1776 pUsbIns->Internal.s.fVMSuspended = false;
1777 return VINF_SUCCESS;
1778}
1779
1780
1781/**
1782 * Worker for PDMR3Resume that deals with one device instance.
1783 *
1784 * @returns VBox status code.
1785 * @param pDevIns The device instance.
1786 */
1787DECLINLINE(int) pdmR3ResumeDev(PPDMDEVINS pDevIns)
1788{
1789 Assert(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED);
1790 if (pDevIns->pReg->pfnResume)
1791 {
1792 LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1793 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1794 int rc = VINF_SUCCESS; pDevIns->pReg->pfnResume(pDevIns);
1795 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1796 if (RT_FAILURE(rc))
1797 {
1798 LogRel(("PDMR3Resume: device '%s'/%d -> %Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
1799 return rc;
1800 }
1801 }
1802 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
1803 return VINF_SUCCESS;
1804}
1805
1806
1807/**
1808 * This function will notify all the devices and their
1809 * attached drivers about the VM now being resumed.
1810 *
1811 * @param pVM Pointer to the VM.
1812 */
1813VMMR3DECL(void) PDMR3Resume(PVM pVM)
1814{
1815 LogFlow(("PDMR3Resume:\n"));
1816
1817 /*
1818 * Iterate thru the device instances and USB device instances,
1819 * processing the drivers associated with those.
1820 */
1821 int rc = VINF_SUCCESS;
1822 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns && RT_SUCCESS(rc); pDevIns = pDevIns->Internal.s.pNextR3)
1823 {
1824 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1825 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1826 rc = pdmR3ResumeDrv(pDrvIns, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun);
1827 if (RT_SUCCESS(rc))
1828 rc = pdmR3ResumeDev(pDevIns);
1829 }
1830
1831#ifdef VBOX_WITH_USB
1832 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns && RT_SUCCESS(rc); pUsbIns = pUsbIns->Internal.s.pNext)
1833 {
1834 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1835 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1836 rc = pdmR3ResumeDrv(pDrvIns, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun);
1837 if (RT_SUCCESS(rc))
1838 rc = pdmR3ResumeUsb(pUsbIns);
1839 }
1840#endif
1841
1842 /*
1843 * Resume all threads.
1844 */
1845 if (RT_SUCCESS(rc))
1846 pdmR3ThreadResumeAll(pVM);
1847
1848 /*
1849 * Resume the block cache.
1850 */
1851 if (RT_SUCCESS(rc))
1852 pdmR3BlkCacheResume(pVM);
1853
1854 /*
1855 * On failure, clean up via PDMR3Suspend.
1856 */
1857 if (RT_FAILURE(rc))
1858 PDMR3Suspend(pVM);
1859
1860 LogFlow(("PDMR3Resume: returns %Rrc\n", rc));
1861 return /*rc*/;
1862}
1863
1864
1865/**
1866 * Worker for PDMR3PowerOff that deals with one driver.
1867 *
1868 * @param pDrvIns The driver instance.
1869 * @param pAsync The structure for recording asynchronous
1870 * notification tasks.
1871 * @param pszDevName The parent device name.
1872 * @param iDevInstance The parent device instance number.
1873 * @param iLun The parent LUN number.
1874 */
1875DECLINLINE(bool) pdmR3PowerOffDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
1876 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1877{
1878 if (!pDrvIns->Internal.s.fVMSuspended)
1879 {
1880 pDrvIns->Internal.s.fVMSuspended = true;
1881 if (pDrvIns->pReg->pfnPowerOff)
1882 {
1883 uint64_t cNsElapsed = RTTimeNanoTS();
1884
1885 if (!pDrvIns->Internal.s.pfnAsyncNotify)
1886 {
1887 LogFlow(("PDMR3PowerOff: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1888 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1889 pDrvIns->pReg->pfnPowerOff(pDrvIns);
1890 if (pDrvIns->Internal.s.pfnAsyncNotify)
1891 LogFlow(("PDMR3PowerOff: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1892 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1893 }
1894 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
1895 {
1896 LogFlow(("PDMR3PowerOff: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1897 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1898 pDrvIns->Internal.s.pfnAsyncNotify = NULL;
1899 }
1900
1901 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1902 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
1903 LogRel(("PDMR3PowerOff: Driver '%s'/%d on LUN#%d of device '%s'/%d took %'llu ns to power off\n",
1904 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, cNsElapsed));
1905
1906 if (pDrvIns->Internal.s.pfnAsyncNotify)
1907 {
1908 pDrvIns->Internal.s.fVMSuspended = false;
1909 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance,
1910 pszDevName, iDevInstance, iLun);
1911 return false;
1912 }
1913 }
1914 }
1915 return true;
1916}
1917
1918
1919/**
1920 * Worker for PDMR3PowerOff that deals with one USB device instance.
1921 *
1922 * @param pUsbIns The USB device instance.
1923 * @param pAsync The structure for recording asynchronous
1924 * notification tasks.
1925 */
1926DECLINLINE(void) pdmR3PowerOffUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
1927{
1928 if (!pUsbIns->Internal.s.fVMSuspended)
1929 {
1930 pUsbIns->Internal.s.fVMSuspended = true;
1931 if (pUsbIns->pReg->pfnVMPowerOff)
1932 {
1933 uint64_t cNsElapsed = RTTimeNanoTS();
1934
1935 if (!pUsbIns->Internal.s.pfnAsyncNotify)
1936 {
1937 LogFlow(("PDMR3PowerOff: Notifying - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1938 pUsbIns->pReg->pfnVMPowerOff(pUsbIns);
1939 if (pUsbIns->Internal.s.pfnAsyncNotify)
1940 LogFlow(("PDMR3PowerOff: Async notification started - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1941 }
1942 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
1943 {
1944 LogFlow(("PDMR3PowerOff: Async notification completed - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1945 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
1946 }
1947 if (pUsbIns->Internal.s.pfnAsyncNotify)
1948 {
1949 pUsbIns->Internal.s.fVMSuspended = false;
1950 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
1951 }
1952
1953 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1954 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
1955 LogRel(("PDMR3PowerOff: USB device '%s'/%d took %'llu ns to power off\n",
1956 pUsbIns->pReg->szName, pUsbIns->iInstance, cNsElapsed));
1957
1958 }
1959 }
1960}
1961
1962
1963/**
1964 * Worker for PDMR3PowerOff that deals with one device instance.
1965 *
1966 * @param pDevIns The device instance.
1967 * @param pAsync The structure for recording asynchronous
1968 * notification tasks.
1969 */
1970DECLINLINE(void) pdmR3PowerOffDev(PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
1971{
1972 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED))
1973 {
1974 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_SUSPENDED;
1975 if (pDevIns->pReg->pfnPowerOff)
1976 {
1977 uint64_t cNsElapsed = RTTimeNanoTS();
1978 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1979
1980 if (!pDevIns->Internal.s.pfnAsyncNotify)
1981 {
1982 LogFlow(("PDMR3PowerOff: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1983 pDevIns->pReg->pfnPowerOff(pDevIns);
1984 if (pDevIns->Internal.s.pfnAsyncNotify)
1985 LogFlow(("PDMR3PowerOff: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1986 }
1987 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
1988 {
1989 LogFlow(("PDMR3PowerOff: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1990 pDevIns->Internal.s.pfnAsyncNotify = NULL;
1991 }
1992 if (pDevIns->Internal.s.pfnAsyncNotify)
1993 {
1994 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
1995 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
1996 }
1997
1998 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1999 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2000 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
2001 LogFlow(("PDMR3PowerOff: Device '%s'/%d took %'llu ns to power off\n",
2002 pDevIns->pReg->szName, pDevIns->iInstance, cNsElapsed));
2003 }
2004 }
2005}
2006
2007
2008/**
2009 * This function will notify all the devices and their
2010 * attached drivers about the VM being powered off.
2011 *
2012 * @param pVM Pointer to the VM.
2013 */
2014VMMR3DECL(void) PDMR3PowerOff(PVM pVM)
2015{
2016 LogFlow(("PDMR3PowerOff:\n"));
2017 uint64_t cNsElapsed = RTTimeNanoTS();
2018
2019 /*
2020 * The outer loop repeats until there are no more async requests.
2021 */
2022 PDMNOTIFYASYNCSTATS Async;
2023 pdmR3NotifyAsyncInit(&Async, "PDMR3PowerOff");
2024 for (;;)
2025 {
2026 pdmR3NotifyAsyncBeginLoop(&Async);
2027
2028 /*
2029 * Iterate thru the device instances and USB device instances,
2030 * processing the drivers associated with those.
2031 *
2032 * The attached drivers are normally processed first. Some devices
2033 * (like DevAHCI) though needs to be notified before the drivers so
2034 * that it doesn't kick off any new requests after the drivers stopped
2035 * taking any. (DrvVD changes to read-only in this particular case.)
2036 */
2037 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2038 {
2039 unsigned const cAsyncStart = Async.cAsync;
2040
2041 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION)
2042 pdmR3PowerOffDev(pDevIns, &Async);
2043
2044 if (Async.cAsync == cAsyncStart)
2045 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2046 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2047 if (!pdmR3PowerOffDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
2048 break;
2049
2050 if ( Async.cAsync == cAsyncStart
2051 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION))
2052 pdmR3PowerOffDev(pDevIns, &Async);
2053 }
2054
2055#ifdef VBOX_WITH_USB
2056 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2057 {
2058 unsigned const cAsyncStart = Async.cAsync;
2059
2060 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2061 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2062 if (!pdmR3PowerOffDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
2063 break;
2064
2065 if (Async.cAsync == cAsyncStart)
2066 pdmR3PowerOffUsb(pUsbIns, &Async);
2067 }
2068#endif
2069 if (!Async.cAsync)
2070 break;
2071 pdmR3NotifyAsyncLog(&Async);
2072 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
2073 }
2074
2075 /*
2076 * Suspend all threads.
2077 */
2078 pdmR3ThreadSuspendAll(pVM);
2079
2080 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2081 LogRel(("PDMR3PowerOff: %'llu ns run time\n", cNsElapsed));
2082}
2083
2084
2085/**
2086 * Queries the base interface of a device instance.
2087 *
2088 * The caller can use this to query other interfaces the device implements
2089 * and use them to talk to the device.
2090 *
2091 * @returns VBox status code.
2092 * @param pVM Pointer to the VM.
2093 * @param pszDevice Device name.
2094 * @param iInstance Device instance.
2095 * @param ppBase Where to store the pointer to the base device interface on success.
2096 * @remark We're not doing any locking ATM, so don't try call this at times when the
2097 * device chain is known to be updated.
2098 */
2099VMMR3DECL(int) PDMR3QueryDevice(PVM pVM, const char *pszDevice, unsigned iInstance, PPDMIBASE *ppBase)
2100{
2101 LogFlow(("PDMR3DeviceQuery: pszDevice=%p:{%s} iInstance=%u ppBase=%p\n", pszDevice, pszDevice, iInstance, ppBase));
2102
2103 /*
2104 * Iterate registered devices looking for the device.
2105 */
2106 size_t cchDevice = strlen(pszDevice);
2107 for (PPDMDEV pDev = pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
2108 {
2109 if ( pDev->cchName == cchDevice
2110 && !memcmp(pDev->pReg->szName, pszDevice, cchDevice))
2111 {
2112 /*
2113 * Iterate device instances.
2114 */
2115 for (PPDMDEVINS pDevIns = pDev->pInstances; pDevIns; pDevIns = pDevIns->Internal.s.pPerDeviceNextR3)
2116 {
2117 if (pDevIns->iInstance == iInstance)
2118 {
2119 if (pDevIns->IBase.pfnQueryInterface)
2120 {
2121 *ppBase = &pDevIns->IBase;
2122 LogFlow(("PDMR3DeviceQuery: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
2123 return VINF_SUCCESS;
2124 }
2125
2126 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NO_IBASE\n"));
2127 return VERR_PDM_DEVICE_INSTANCE_NO_IBASE;
2128 }
2129 }
2130
2131 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NOT_FOUND\n"));
2132 return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND;
2133 }
2134 }
2135
2136 LogFlow(("PDMR3QueryDevice: returns VERR_PDM_DEVICE_NOT_FOUND\n"));
2137 return VERR_PDM_DEVICE_NOT_FOUND;
2138}
2139
2140
2141/**
2142 * Queries the base interface of a device LUN.
2143 *
2144 * This differs from PDMR3QueryLun by that it returns the interface on the
2145 * device and not the top level driver.
2146 *
2147 * @returns VBox status code.
2148 * @param pVM Pointer to the VM.
2149 * @param pszDevice Device name.
2150 * @param iInstance Device instance.
2151 * @param iLun The Logical Unit to obtain the interface of.
2152 * @param ppBase Where to store the base interface pointer.
2153 * @remark We're not doing any locking ATM, so don't try call this at times when the
2154 * device chain is known to be updated.
2155 */
2156VMMR3DECL(int) PDMR3QueryDeviceLun(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
2157{
2158 LogFlow(("PDMR3QueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
2159 pszDevice, pszDevice, iInstance, iLun, ppBase));
2160
2161 /*
2162 * Find the LUN.
2163 */
2164 PPDMLUN pLun;
2165 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
2166 if (RT_SUCCESS(rc))
2167 {
2168 *ppBase = pLun->pBase;
2169 LogFlow(("PDMR3QueryDeviceLun: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
2170 return VINF_SUCCESS;
2171 }
2172 LogFlow(("PDMR3QueryDeviceLun: returns %Rrc\n", rc));
2173 return rc;
2174}
2175
2176
2177/**
2178 * Query the interface of the top level driver on a LUN.
2179 *
2180 * @returns VBox status code.
2181 * @param pVM Pointer to the VM.
2182 * @param pszDevice Device name.
2183 * @param iInstance Device instance.
2184 * @param iLun The Logical Unit to obtain the interface of.
2185 * @param ppBase Where to store the base interface pointer.
2186 * @remark We're not doing any locking ATM, so don't try call this at times when the
2187 * device chain is known to be updated.
2188 */
2189VMMR3DECL(int) PDMR3QueryLun(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
2190{
2191 LogFlow(("PDMR3QueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
2192 pszDevice, pszDevice, iInstance, iLun, ppBase));
2193 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
2194
2195 /*
2196 * Find the LUN.
2197 */
2198 PPDMLUN pLun;
2199 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
2200 if (RT_SUCCESS(rc))
2201 {
2202 if (pLun->pTop)
2203 {
2204 *ppBase = &pLun->pTop->IBase;
2205 LogFlow(("PDMR3QueryLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
2206 return VINF_SUCCESS;
2207 }
2208 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
2209 }
2210 LogFlow(("PDMR3QueryLun: returns %Rrc\n", rc));
2211 return rc;
2212}
2213
2214
2215/**
2216 * Query the interface of a named driver on a LUN.
2217 *
2218 * If the driver appears more than once in the driver chain, the first instance
2219 * is returned.
2220 *
2221 * @returns VBox status code.
2222 * @param pVM Pointer to the VM.
2223 * @param pszDevice Device name.
2224 * @param iInstance Device instance.
2225 * @param iLun The Logical Unit to obtain the interface of.
2226 * @param pszDriver The driver name.
2227 * @param ppBase Where to store the base interface pointer.
2228 *
2229 * @remark We're not doing any locking ATM, so don't try call this at times when the
2230 * device chain is known to be updated.
2231 */
2232VMMR3DECL(int) PDMR3QueryDriverOnLun(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, const char *pszDriver, PPPDMIBASE ppBase)
2233{
2234 LogFlow(("PDMR3QueryDriverOnLun: pszDevice=%p:{%s} iInstance=%u iLun=%u pszDriver=%p:{%s} ppBase=%p\n",
2235 pszDevice, pszDevice, iInstance, iLun, pszDriver, pszDriver, ppBase));
2236
2237 /*
2238 * Find the LUN.
2239 */
2240 PPDMLUN pLun;
2241 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
2242 if (RT_SUCCESS(rc))
2243 {
2244 if (pLun->pTop)
2245 {
2246 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2247 if (!strcmp(pDrvIns->pReg->szName, pszDriver))
2248 {
2249 *ppBase = &pDrvIns->IBase;
2250 LogFlow(("PDMR3QueryDriverOnLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
2251 return VINF_SUCCESS;
2252
2253 }
2254 rc = VERR_PDM_DRIVER_NOT_FOUND;
2255 }
2256 else
2257 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
2258 }
2259 LogFlow(("PDMR3QueryDriverOnLun: returns %Rrc\n", rc));
2260 return rc;
2261}
2262
2263/**
2264 * Executes pending DMA transfers.
2265 * Forced Action handler.
2266 *
2267 * @param pVM Pointer to the VM.
2268 */
2269VMMR3DECL(void) PDMR3DmaRun(PVM pVM)
2270{
2271 /* Note! Not really SMP safe; restrict it to VCPU 0. */
2272 if (VMMGetCpuId(pVM) != 0)
2273 return;
2274
2275 if (VM_FF_TESTANDCLEAR(pVM, VM_FF_PDM_DMA))
2276 {
2277 if (pVM->pdm.s.pDmac)
2278 {
2279 bool fMore = pVM->pdm.s.pDmac->Reg.pfnRun(pVM->pdm.s.pDmac->pDevIns);
2280 if (fMore)
2281 VM_FF_SET(pVM, VM_FF_PDM_DMA);
2282 }
2283 }
2284}
2285
2286
2287/**
2288 * Service a VMMCALLRING3_PDM_LOCK call.
2289 *
2290 * @returns VBox status code.
2291 * @param pVM Pointer to the VM.
2292 */
2293VMMR3DECL(int) PDMR3LockCall(PVM pVM)
2294{
2295 return PDMR3CritSectEnterEx(&pVM->pdm.s.CritSect, true /* fHostCall */);
2296}
2297
2298
2299/**
2300 * Registers the VMM device heap
2301 *
2302 * @returns VBox status code.
2303 * @param pVM Pointer to the VM.
2304 * @param GCPhys The physical address.
2305 * @param pvHeap Ring-3 pointer.
2306 * @param cbSize Size of the heap.
2307 */
2308VMMR3DECL(int) PDMR3RegisterVMMDevHeap(PVM pVM, RTGCPHYS GCPhys, RTR3PTR pvHeap, unsigned cbSize)
2309{
2310 Assert(pVM->pdm.s.pvVMMDevHeap == NULL);
2311
2312 Log(("PDMR3RegisterVMMDevHeap %RGp %RHv %x\n", GCPhys, pvHeap, cbSize));
2313 pVM->pdm.s.pvVMMDevHeap = pvHeap;
2314 pVM->pdm.s.GCPhysVMMDevHeap = GCPhys;
2315 pVM->pdm.s.cbVMMDevHeap = cbSize;
2316 pVM->pdm.s.cbVMMDevHeapLeft = cbSize;
2317 return VINF_SUCCESS;
2318}
2319
2320
2321/**
2322 * Unregisters the VMM device heap
2323 *
2324 * @returns VBox status code.
2325 * @param pVM Pointer to the VM.
2326 * @param GCPhys The physical address.
2327 */
2328VMMR3DECL(int) PDMR3UnregisterVMMDevHeap(PVM pVM, RTGCPHYS GCPhys)
2329{
2330 Assert(pVM->pdm.s.GCPhysVMMDevHeap == GCPhys);
2331
2332 Log(("PDMR3UnregisterVMMDevHeap %RGp\n", GCPhys));
2333 pVM->pdm.s.pvVMMDevHeap = NULL;
2334 pVM->pdm.s.GCPhysVMMDevHeap = NIL_RTGCPHYS;
2335 pVM->pdm.s.cbVMMDevHeap = 0;
2336 pVM->pdm.s.cbVMMDevHeapLeft = 0;
2337 return VINF_SUCCESS;
2338}
2339
2340
2341/**
2342 * Allocates memory from the VMM device heap
2343 *
2344 * @returns VBox status code.
2345 * @param pVM Pointer to the VM.
2346 * @param cbSize Allocation size.
2347 * @param pv Ring-3 pointer. (out)
2348 */
2349VMMR3DECL(int) PDMR3VMMDevHeapAlloc(PVM pVM, unsigned cbSize, RTR3PTR *ppv)
2350{
2351#ifdef DEBUG_bird
2352 if (!cbSize || cbSize > pVM->pdm.s.cbVMMDevHeapLeft)
2353 return VERR_NO_MEMORY;
2354#else
2355 AssertReturn(cbSize && cbSize <= pVM->pdm.s.cbVMMDevHeapLeft, VERR_NO_MEMORY);
2356#endif
2357
2358 Log(("PDMR3VMMDevHeapAlloc %x\n", cbSize));
2359
2360 /** @todo not a real heap as there's currently only one user. */
2361 *ppv = pVM->pdm.s.pvVMMDevHeap;
2362 pVM->pdm.s.cbVMMDevHeapLeft = 0;
2363 return VINF_SUCCESS;
2364}
2365
2366
2367/**
2368 * Frees memory from the VMM device heap
2369 *
2370 * @returns VBox status code.
2371 * @param pVM Pointer to the VM.
2372 * @param pv Ring-3 pointer.
2373 */
2374VMMR3DECL(int) PDMR3VMMDevHeapFree(PVM pVM, RTR3PTR pv)
2375{
2376 Log(("PDMR3VMMDevHeapFree %RHv\n", pv));
2377
2378 /** @todo not a real heap as there's currently only one user. */
2379 pVM->pdm.s.cbVMMDevHeapLeft = pVM->pdm.s.cbVMMDevHeap;
2380 return VINF_SUCCESS;
2381}
2382
2383
2384/**
2385 * Worker for DBGFR3TraceConfig that checks if the given tracing group name
2386 * matches a device or driver name and applies the tracing config change.
2387 *
2388 * @returns VINF_SUCCESS or VERR_NOT_FOUND.
2389 * @param pVM Pointer to the VM.
2390 * @param pszName The tracing config group name. This is NULL if
2391 * the operation applies to every device and
2392 * driver.
2393 * @param cchName The length to match.
2394 * @param fEnable Whether to enable or disable the corresponding
2395 * trace points.
2396 * @param fApply Whether to actually apply the changes or just do
2397 * existence checks.
2398 */
2399VMMR3_INT_DECL(int) PDMR3TracingConfig(PVM pVM, const char *pszName, size_t cchName, bool fEnable, bool fApply)
2400{
2401 /** @todo This code is potentially racing driver attaching and detaching. */
2402
2403 /*
2404 * Applies to all.
2405 */
2406 if (pszName == NULL)
2407 {
2408 AssertReturn(fApply, VINF_SUCCESS);
2409
2410 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2411 {
2412 pDevIns->fTracing = fEnable;
2413 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2414 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2415 pDrvIns->fTracing = fEnable;
2416 }
2417
2418#ifdef VBOX_WITH_USB
2419 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2420 {
2421 pUsbIns->fTracing = fEnable;
2422 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2423 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2424 pDrvIns->fTracing = fEnable;
2425
2426 }
2427#endif
2428 return VINF_SUCCESS;
2429 }
2430
2431 /*
2432 * Specific devices, USB devices or drivers.
2433 * Decode prefix to figure which of these it applies to.
2434 */
2435 if (cchName <= 3)
2436 return VERR_NOT_FOUND;
2437
2438 uint32_t cMatches = 0;
2439 if (!strncmp("dev", pszName, 3))
2440 {
2441 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2442 {
2443 const char *pszDevName = pDevIns->Internal.s.pDevR3->pReg->szName;
2444 size_t cchDevName = strlen(pszDevName);
2445 if ( ( cchDevName == cchName
2446 && RTStrNICmp(pszName, pszDevName, cchDevName))
2447 || ( cchDevName == cchName - 3
2448 && RTStrNICmp(pszName + 3, pszDevName, cchDevName)) )
2449 {
2450 cMatches++;
2451 if (fApply)
2452 pDevIns->fTracing = fEnable;
2453 }
2454 }
2455 }
2456 else if (!strncmp("usb", pszName, 3))
2457 {
2458 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2459 {
2460 const char *pszUsbName = pUsbIns->Internal.s.pUsbDev->pReg->szName;
2461 size_t cchUsbName = strlen(pszUsbName);
2462 if ( ( cchUsbName == cchName
2463 && RTStrNICmp(pszName, pszUsbName, cchUsbName))
2464 || ( cchUsbName == cchName - 3
2465 && RTStrNICmp(pszName + 3, pszUsbName, cchUsbName)) )
2466 {
2467 cMatches++;
2468 if (fApply)
2469 pUsbIns->fTracing = fEnable;
2470 }
2471 }
2472 }
2473 else if (!strncmp("drv", pszName, 3))
2474 {
2475 AssertReturn(fApply, VINF_SUCCESS);
2476
2477 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2478 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2479 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2480 {
2481 const char *pszDrvName = pDrvIns->Internal.s.pDrv->pReg->szName;
2482 size_t cchDrvName = strlen(pszDrvName);
2483 if ( ( cchDrvName == cchName
2484 && RTStrNICmp(pszName, pszDrvName, cchDrvName))
2485 || ( cchDrvName == cchName - 3
2486 && RTStrNICmp(pszName + 3, pszDrvName, cchDrvName)) )
2487 {
2488 cMatches++;
2489 if (fApply)
2490 pDrvIns->fTracing = fEnable;
2491 }
2492 }
2493
2494#ifdef VBOX_WITH_USB
2495 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2496 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2497 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2498 {
2499 const char *pszDrvName = pDrvIns->Internal.s.pDrv->pReg->szName;
2500 size_t cchDrvName = strlen(pszDrvName);
2501 if ( ( cchDrvName == cchName
2502 && RTStrNICmp(pszName, pszDrvName, cchDrvName))
2503 || ( cchDrvName == cchName - 3
2504 && RTStrNICmp(pszName + 3, pszDrvName, cchDrvName)) )
2505 {
2506 cMatches++;
2507 if (fApply)
2508 pDrvIns->fTracing = fEnable;
2509 }
2510 }
2511#endif
2512 }
2513 else
2514 return VERR_NOT_FOUND;
2515
2516 return cMatches > 0 ? VINF_SUCCESS : VERR_NOT_FOUND;
2517}
2518
2519
2520/**
2521 * Worker for DBGFR3TraceQueryConfig that checks whether all drivers, devices,
2522 * and USB device have the same tracing settings.
2523 *
2524 * @returns true / false.
2525 * @param pVM Pointer to the VM.
2526 * @param fEnabled The tracing setting to check for.
2527 */
2528VMMR3_INT_DECL(bool) PDMR3TracingAreAll(PVM pVM, bool fEnabled)
2529{
2530 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2531 {
2532 if (pDevIns->fTracing != (uint32_t)fEnabled)
2533 return false;
2534
2535 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2536 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2537 if (pDrvIns->fTracing != (uint32_t)fEnabled)
2538 return false;
2539 }
2540
2541#ifdef VBOX_WITH_USB
2542 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2543 {
2544 if (pUsbIns->fTracing != (uint32_t)fEnabled)
2545 return false;
2546
2547 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2548 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2549 if (pDrvIns->fTracing != (uint32_t)fEnabled)
2550 return false;
2551 }
2552#endif
2553
2554 return true;
2555}
2556
2557
2558/**
2559 * Worker for PDMR3TracingQueryConfig that adds a prefixed name to the output
2560 * string.
2561 *
2562 * @returns VINF_SUCCESS or VERR_BUFFER_OVERFLOW
2563 * @param ppszDst The pointer to the output buffer pointer.
2564 * @param pcbDst The pointer to the output buffer size.
2565 * @param fSpace Whether to add a space before the name.
2566 * @param pszPrefix The name prefix.
2567 * @param pszName The name.
2568 */
2569static int pdmR3TracingAdd(char **ppszDst, size_t *pcbDst, bool fSpace, const char *pszPrefix, const char *pszName)
2570{
2571 size_t const cchPrefix = strlen(pszPrefix);
2572 if (!RTStrNICmp(pszPrefix, pszName, cchPrefix))
2573 pszName += cchPrefix;
2574 size_t const cchName = strlen(pszName);
2575
2576 size_t const cchThis = cchName + cchPrefix + fSpace;
2577 if (cchThis >= *pcbDst)
2578 return VERR_BUFFER_OVERFLOW;
2579 if (fSpace)
2580 {
2581 **ppszDst = ' ';
2582 memcpy(*ppszDst + 1, pszPrefix, cchPrefix);
2583 memcpy(*ppszDst + 1 + cchPrefix, pszName, cchName + 1);
2584 }
2585 else
2586 {
2587 memcpy(*ppszDst, pszPrefix, cchPrefix);
2588 memcpy(*ppszDst + cchPrefix, pszName, cchName + 1);
2589 }
2590 *ppszDst += cchThis;
2591 *pcbDst -= cchThis;
2592 return VINF_SUCCESS;
2593}
2594
2595
2596/**
2597 * Worker for DBGFR3TraceQueryConfig use when not everything is either enabled
2598 * or disabled.
2599 *
2600 * @returns VINF_SUCCESS or VERR_BUFFER_OVERFLOW
2601 * @param pVM Pointer to the VM.
2602 * @param pszConfig Where to store the config spec.
2603 * @param cbConfig The size of the output buffer.
2604 */
2605VMMR3_INT_DECL(int) PDMR3TracingQueryConfig(PVM pVM, char *pszConfig, size_t cbConfig)
2606{
2607 int rc;
2608 char *pszDst = pszConfig;
2609 size_t cbDst = cbConfig;
2610
2611 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2612 {
2613 if (pDevIns->fTracing)
2614 {
2615 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "dev", pDevIns->Internal.s.pDevR3->pReg->szName);
2616 if (RT_FAILURE(rc))
2617 return rc;
2618 }
2619
2620 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2621 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2622 if (pDrvIns->fTracing)
2623 {
2624 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "drv", pDrvIns->Internal.s.pDrv->pReg->szName);
2625 if (RT_FAILURE(rc))
2626 return rc;
2627 }
2628 }
2629
2630#ifdef VBOX_WITH_USB
2631 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2632 {
2633 if (pUsbIns->fTracing)
2634 {
2635 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "usb", pUsbIns->Internal.s.pUsbDev->pReg->szName);
2636 if (RT_FAILURE(rc))
2637 return rc;
2638 }
2639
2640 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2641 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2642 if (pDrvIns->fTracing)
2643 {
2644 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "drv", pDrvIns->Internal.s.pDrv->pReg->szName);
2645 if (RT_FAILURE(rc))
2646 return rc;
2647 }
2648 }
2649#endif
2650
2651 return VINF_SUCCESS;
2652}
2653
2654
2655/**
2656 * Checks that a PDMDRVREG::szName, PDMDEVREG::szName or PDMUSBREG::szName
2657 * field contains only a limited set of ASCII characters.
2658 *
2659 * @returns true / false.
2660 * @param pszName The name to validate.
2661 */
2662bool pdmR3IsValidName(const char *pszName)
2663{
2664 char ch;
2665 while ( (ch = *pszName) != '\0'
2666 && ( RT_C_IS_ALNUM(ch)
2667 || ch == '-'
2668 || ch == ' ' /** @todo disallow this! */
2669 || ch == '_') )
2670 pszName++;
2671 return ch == '\0';
2672}
2673
2674
2675/**
2676 * Info handler for 'pdmtracingids'.
2677 *
2678 * @param pVM Pointer to the VM.
2679 * @param pHlp The output helpers.
2680 * @param pszArgs The optional user arguments.
2681 *
2682 * @remarks Can be called on most threads.
2683 */
2684static DECLCALLBACK(void) pdmR3InfoTracingIds(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2685{
2686 /*
2687 * Parse the argument (optional).
2688 */
2689 if ( pszArgs
2690 && *pszArgs
2691 && strcmp(pszArgs, "all")
2692 && strcmp(pszArgs, "devices")
2693 && strcmp(pszArgs, "drivers")
2694 && strcmp(pszArgs, "usb"))
2695 {
2696 pHlp->pfnPrintf(pHlp, "Unable to grok '%s'\n", pszArgs);
2697 return;
2698 }
2699 bool fAll = !pszArgs || !*pszArgs || !strcmp(pszArgs, "all");
2700 bool fDevices = fAll || !strcmp(pszArgs, "devices");
2701 bool fUsbDevs = fAll || !strcmp(pszArgs, "usb");
2702 bool fDrivers = fAll || !strcmp(pszArgs, "drivers");
2703
2704 /*
2705 * Produce the requested output.
2706 */
2707/** @todo lock PDM lists! */
2708 /* devices */
2709 if (fDevices)
2710 {
2711 pHlp->pfnPrintf(pHlp, "Device tracing IDs:\n");
2712 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2713 pHlp->pfnPrintf(pHlp, "%05u %s\n", pDevIns->idTracing, pDevIns->Internal.s.pDevR3->pReg->szName);
2714 }
2715
2716 /* USB devices */
2717 if (fUsbDevs)
2718 {
2719 pHlp->pfnPrintf(pHlp, "USB device tracing IDs:\n");
2720 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2721 pHlp->pfnPrintf(pHlp, "%05u %s\n", pUsbIns->idTracing, pUsbIns->Internal.s.pUsbDev->pReg->szName);
2722 }
2723
2724 /* Drivers */
2725 if (fDrivers)
2726 {
2727 pHlp->pfnPrintf(pHlp, "Driver tracing IDs:\n");
2728 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2729 {
2730 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2731 {
2732 uint32_t iLevel = 0;
2733 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown, iLevel++)
2734 pHlp->pfnPrintf(pHlp, "%05u %s (level %u, lun %u, dev %s)\n",
2735 pDrvIns->idTracing, pDrvIns->Internal.s.pDrv->pReg->szName,
2736 iLevel, pLun->iLun, pDevIns->Internal.s.pDevR3->pReg->szName);
2737 }
2738 }
2739
2740 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2741 {
2742 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2743 {
2744 uint32_t iLevel = 0;
2745 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown, iLevel++)
2746 pHlp->pfnPrintf(pHlp, "%05u %s (level %u, lun %u, dev %s)\n",
2747 pDrvIns->idTracing, pDrvIns->Internal.s.pDrv->pReg->szName,
2748 iLevel, pLun->iLun, pUsbIns->Internal.s.pUsbDev->pReg->szName);
2749 }
2750 }
2751 }
2752}
2753
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