VirtualBox

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

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

PDM,++: Change APIs used by Main from PVM to PUVM.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 100.4 KB
Line 
1/* $Id: PDM.cpp 44351 2013-01-24 12:04:39Z 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 */
339VMMR3_INT_DECL(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 Pointer to the VM.
354 */
355VMMR3_INT_DECL(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 */
447VMMR3_INT_DECL(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 Pointer to the VM.
618 */
619VMMR3_INT_DECL(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 */
714VMMR3_INT_DECL(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 Pointer to the VMCPU.
1406 */
1407VMMR3_INT_DECL(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 */
1422VMMR3_INT_DECL(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 (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_RESET_NOTIFICATION)
1464 pdmR3ResetDev(pDevIns, &Async);
1465
1466 if (Async.cAsync == cAsyncStart)
1467 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1468 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1469 if (!pdmR3ResetDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
1470 break;
1471
1472 if ( Async.cAsync == cAsyncStart
1473 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_RESET_NOTIFICATION))
1474 pdmR3ResetDev(pDevIns, &Async);
1475 }
1476
1477#ifdef VBOX_WITH_USB
1478 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1479 {
1480 unsigned const cAsyncStart = Async.cAsync;
1481
1482 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1483 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1484 if (!pdmR3ResetDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
1485 break;
1486
1487 if (Async.cAsync == cAsyncStart)
1488 pdmR3ResetUsb(pUsbIns, &Async);
1489 }
1490#endif
1491 if (!Async.cAsync)
1492 break;
1493 pdmR3NotifyAsyncLog(&Async);
1494 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
1495 }
1496
1497 /*
1498 * Clear all pending interrupts and DMA operations.
1499 */
1500 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
1501 PDMR3ResetCpu(&pVM->aCpus[idCpu]);
1502 VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
1503
1504 LogFlow(("PDMR3Reset: returns void\n"));
1505}
1506
1507
1508/**
1509 * Worker for PDMR3Suspend that deals with one driver.
1510 *
1511 * @param pDrvIns The driver instance.
1512 * @param pAsync The structure for recording asynchronous
1513 * notification tasks.
1514 * @param pszDevName The parent device name.
1515 * @param iDevInstance The parent device instance number.
1516 * @param iLun The parent LUN number.
1517 */
1518DECLINLINE(bool) pdmR3SuspendDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
1519 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1520{
1521 if (!pDrvIns->Internal.s.fVMSuspended)
1522 {
1523 pDrvIns->Internal.s.fVMSuspended = true;
1524 if (pDrvIns->pReg->pfnSuspend)
1525 {
1526 uint64_t cNsElapsed = RTTimeNanoTS();
1527
1528 if (!pDrvIns->Internal.s.pfnAsyncNotify)
1529 {
1530 LogFlow(("PDMR3Suspend: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1531 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1532 pDrvIns->pReg->pfnSuspend(pDrvIns);
1533 if (pDrvIns->Internal.s.pfnAsyncNotify)
1534 LogFlow(("PDMR3Suspend: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1535 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1536 }
1537 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
1538 {
1539 LogFlow(("PDMR3Suspend: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1540 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1541 pDrvIns->Internal.s.pfnAsyncNotify = NULL;
1542 }
1543
1544 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1545 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1546 LogRel(("PDMR3Suspend: Driver '%s'/%d on LUN#%d of device '%s'/%d took %'llu ns to suspend\n",
1547 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, cNsElapsed));
1548
1549 if (pDrvIns->Internal.s.pfnAsyncNotify)
1550 {
1551 pDrvIns->Internal.s.fVMSuspended = false;
1552 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance, pszDevName, iDevInstance, iLun);
1553 return false;
1554 }
1555 }
1556 }
1557 return true;
1558}
1559
1560
1561/**
1562 * Worker for PDMR3Suspend that deals with one USB device instance.
1563 *
1564 * @param pUsbIns The USB device instance.
1565 * @param pAsync The structure for recording asynchronous
1566 * notification tasks.
1567 */
1568DECLINLINE(void) pdmR3SuspendUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
1569{
1570 if (!pUsbIns->Internal.s.fVMSuspended)
1571 {
1572 pUsbIns->Internal.s.fVMSuspended = true;
1573 if (pUsbIns->pReg->pfnVMSuspend)
1574 {
1575 uint64_t cNsElapsed = RTTimeNanoTS();
1576
1577 if (!pUsbIns->Internal.s.pfnAsyncNotify)
1578 {
1579 LogFlow(("PDMR3Suspend: Notifying - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1580 pUsbIns->pReg->pfnVMSuspend(pUsbIns);
1581 if (pUsbIns->Internal.s.pfnAsyncNotify)
1582 LogFlow(("PDMR3Suspend: Async notification started - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1583 }
1584 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
1585 {
1586 LogFlow(("PDMR3Suspend: Async notification completed - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1587 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
1588 }
1589 if (pUsbIns->Internal.s.pfnAsyncNotify)
1590 {
1591 pUsbIns->Internal.s.fVMSuspended = false;
1592 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
1593 }
1594
1595 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1596 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1597 LogRel(("PDMR3Suspend: USB device '%s'/%d took %'llu ns to suspend\n",
1598 pUsbIns->pReg->szName, pUsbIns->iInstance, cNsElapsed));
1599 }
1600 }
1601}
1602
1603
1604/**
1605 * Worker for PDMR3Suspend that deals with one device instance.
1606 *
1607 * @param pDevIns The device instance.
1608 * @param pAsync The structure for recording asynchronous
1609 * notification tasks.
1610 */
1611DECLINLINE(void) pdmR3SuspendDev(PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
1612{
1613 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED))
1614 {
1615 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_SUSPENDED;
1616 if (pDevIns->pReg->pfnSuspend)
1617 {
1618 uint64_t cNsElapsed = RTTimeNanoTS();
1619 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1620
1621 if (!pDevIns->Internal.s.pfnAsyncNotify)
1622 {
1623 LogFlow(("PDMR3Suspend: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1624 pDevIns->pReg->pfnSuspend(pDevIns);
1625 if (pDevIns->Internal.s.pfnAsyncNotify)
1626 LogFlow(("PDMR3Suspend: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1627 }
1628 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
1629 {
1630 LogFlow(("PDMR3Suspend: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1631 pDevIns->Internal.s.pfnAsyncNotify = NULL;
1632 }
1633 if (pDevIns->Internal.s.pfnAsyncNotify)
1634 {
1635 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
1636 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
1637 }
1638
1639 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1640 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1641 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1642 LogRel(("PDMR3Suspend: device '%s'/%d took %'llu ns to suspend\n",
1643 pDevIns->pReg->szName, pDevIns->iInstance, cNsElapsed));
1644 }
1645 }
1646}
1647
1648
1649/**
1650 * This function will notify all the devices and their attached drivers about
1651 * the VM now being suspended.
1652 *
1653 * @param pVM Pointer to the VM.
1654 * @thread EMT(0)
1655 */
1656VMMR3_INT_DECL(void) PDMR3Suspend(PVM pVM)
1657{
1658 LogFlow(("PDMR3Suspend:\n"));
1659 VM_ASSERT_EMT0(pVM);
1660 uint64_t cNsElapsed = RTTimeNanoTS();
1661
1662 /*
1663 * The outer loop repeats until there are no more async requests.
1664 *
1665 * Note! We depend on the suspended indicators to be in the desired state
1666 * and we do not reset them before starting because this allows
1667 * PDMR3PowerOn and PDMR3Resume to use PDMR3Suspend for cleaning up
1668 * on failure.
1669 */
1670 PDMNOTIFYASYNCSTATS Async;
1671 pdmR3NotifyAsyncInit(&Async, "PDMR3Suspend");
1672 for (;;)
1673 {
1674 pdmR3NotifyAsyncBeginLoop(&Async);
1675
1676 /*
1677 * Iterate thru the device instances and USB device instances,
1678 * processing the drivers associated with those.
1679 *
1680 * The attached drivers are normally processed first. Some devices
1681 * (like DevAHCI) though needs to be notified before the drivers so
1682 * that it doesn't kick off any new requests after the drivers stopped
1683 * taking any. (DrvVD changes to read-only in this particular case.)
1684 */
1685 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1686 {
1687 unsigned const cAsyncStart = Async.cAsync;
1688
1689 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION)
1690 pdmR3SuspendDev(pDevIns, &Async);
1691
1692 if (Async.cAsync == cAsyncStart)
1693 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1694 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1695 if (!pdmR3SuspendDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
1696 break;
1697
1698 if ( Async.cAsync == cAsyncStart
1699 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION))
1700 pdmR3SuspendDev(pDevIns, &Async);
1701 }
1702
1703#ifdef VBOX_WITH_USB
1704 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1705 {
1706 unsigned const cAsyncStart = Async.cAsync;
1707
1708 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1709 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1710 if (!pdmR3SuspendDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
1711 break;
1712
1713 if (Async.cAsync == cAsyncStart)
1714 pdmR3SuspendUsb(pUsbIns, &Async);
1715 }
1716#endif
1717 if (!Async.cAsync)
1718 break;
1719 pdmR3NotifyAsyncLog(&Async);
1720 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
1721 }
1722
1723 /*
1724 * Suspend all threads.
1725 */
1726 pdmR3ThreadSuspendAll(pVM);
1727
1728 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1729 LogRel(("PDMR3Suspend: %'llu ns run time\n", cNsElapsed));
1730}
1731
1732
1733/**
1734 * Worker for PDMR3Resume that deals with one driver.
1735 *
1736 * @param pDrvIns The driver instance.
1737 * @param pszDevName The parent device name.
1738 * @param iDevInstance The parent device instance number.
1739 * @param iLun The parent LUN number.
1740 */
1741DECLINLINE(int) pdmR3ResumeDrv(PPDMDRVINS pDrvIns, const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1742{
1743 Assert(pDrvIns->Internal.s.fVMSuspended);
1744 if (pDrvIns->pReg->pfnResume)
1745 {
1746 LogFlow(("PDMR3Resume: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1747 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1748 int rc = VINF_SUCCESS; pDrvIns->pReg->pfnResume(pDrvIns);
1749 if (RT_FAILURE(rc))
1750 {
1751 LogRel(("PDMR3Resume: driver '%s'/%d on LUN#%d of device '%s'/%d -> %Rrc\n",
1752 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, rc));
1753 return rc;
1754 }
1755 }
1756 pDrvIns->Internal.s.fVMSuspended = false;
1757 return VINF_SUCCESS;
1758}
1759
1760
1761/**
1762 * Worker for PDMR3Resume that deals with one USB device instance.
1763 *
1764 * @returns VBox status code.
1765 * @param pUsbIns The USB device instance.
1766 */
1767DECLINLINE(int) pdmR3ResumeUsb(PPDMUSBINS pUsbIns)
1768{
1769 Assert(pUsbIns->Internal.s.fVMSuspended);
1770 if (pUsbIns->pReg->pfnVMResume)
1771 {
1772 LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1773 int rc = VINF_SUCCESS; pUsbIns->pReg->pfnVMResume(pUsbIns);
1774 if (RT_FAILURE(rc))
1775 {
1776 LogRel(("PDMR3Resume: device '%s'/%d -> %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1777 return rc;
1778 }
1779 }
1780 pUsbIns->Internal.s.fVMSuspended = false;
1781 return VINF_SUCCESS;
1782}
1783
1784
1785/**
1786 * Worker for PDMR3Resume that deals with one device instance.
1787 *
1788 * @returns VBox status code.
1789 * @param pDevIns The device instance.
1790 */
1791DECLINLINE(int) pdmR3ResumeDev(PPDMDEVINS pDevIns)
1792{
1793 Assert(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED);
1794 if (pDevIns->pReg->pfnResume)
1795 {
1796 LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1797 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1798 int rc = VINF_SUCCESS; pDevIns->pReg->pfnResume(pDevIns);
1799 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1800 if (RT_FAILURE(rc))
1801 {
1802 LogRel(("PDMR3Resume: device '%s'/%d -> %Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
1803 return rc;
1804 }
1805 }
1806 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
1807 return VINF_SUCCESS;
1808}
1809
1810
1811/**
1812 * This function will notify all the devices and their
1813 * attached drivers about the VM now being resumed.
1814 *
1815 * @param pVM Pointer to the VM.
1816 */
1817VMMR3_INT_DECL(void) PDMR3Resume(PVM pVM)
1818{
1819 LogFlow(("PDMR3Resume:\n"));
1820
1821 /*
1822 * Iterate thru the device instances and USB device instances,
1823 * processing the drivers associated with those.
1824 */
1825 int rc = VINF_SUCCESS;
1826 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns && RT_SUCCESS(rc); pDevIns = pDevIns->Internal.s.pNextR3)
1827 {
1828 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1829 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1830 rc = pdmR3ResumeDrv(pDrvIns, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun);
1831 if (RT_SUCCESS(rc))
1832 rc = pdmR3ResumeDev(pDevIns);
1833 }
1834
1835#ifdef VBOX_WITH_USB
1836 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns && RT_SUCCESS(rc); pUsbIns = pUsbIns->Internal.s.pNext)
1837 {
1838 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1839 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1840 rc = pdmR3ResumeDrv(pDrvIns, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun);
1841 if (RT_SUCCESS(rc))
1842 rc = pdmR3ResumeUsb(pUsbIns);
1843 }
1844#endif
1845
1846 /*
1847 * Resume all threads.
1848 */
1849 if (RT_SUCCESS(rc))
1850 pdmR3ThreadResumeAll(pVM);
1851
1852 /*
1853 * Resume the block cache.
1854 */
1855 if (RT_SUCCESS(rc))
1856 pdmR3BlkCacheResume(pVM);
1857
1858 /*
1859 * On failure, clean up via PDMR3Suspend.
1860 */
1861 if (RT_FAILURE(rc))
1862 PDMR3Suspend(pVM);
1863
1864 LogFlow(("PDMR3Resume: returns %Rrc\n", rc));
1865 return /*rc*/;
1866}
1867
1868
1869/**
1870 * Worker for PDMR3PowerOff that deals with one driver.
1871 *
1872 * @param pDrvIns The driver instance.
1873 * @param pAsync The structure for recording asynchronous
1874 * notification tasks.
1875 * @param pszDevName The parent device name.
1876 * @param iDevInstance The parent device instance number.
1877 * @param iLun The parent LUN number.
1878 */
1879DECLINLINE(bool) pdmR3PowerOffDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
1880 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1881{
1882 if (!pDrvIns->Internal.s.fVMSuspended)
1883 {
1884 pDrvIns->Internal.s.fVMSuspended = true;
1885 if (pDrvIns->pReg->pfnPowerOff)
1886 {
1887 uint64_t cNsElapsed = RTTimeNanoTS();
1888
1889 if (!pDrvIns->Internal.s.pfnAsyncNotify)
1890 {
1891 LogFlow(("PDMR3PowerOff: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1892 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1893 pDrvIns->pReg->pfnPowerOff(pDrvIns);
1894 if (pDrvIns->Internal.s.pfnAsyncNotify)
1895 LogFlow(("PDMR3PowerOff: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1896 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1897 }
1898 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
1899 {
1900 LogFlow(("PDMR3PowerOff: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1901 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1902 pDrvIns->Internal.s.pfnAsyncNotify = NULL;
1903 }
1904
1905 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1906 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
1907 LogRel(("PDMR3PowerOff: Driver '%s'/%d on LUN#%d of device '%s'/%d took %'llu ns to power off\n",
1908 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, cNsElapsed));
1909
1910 if (pDrvIns->Internal.s.pfnAsyncNotify)
1911 {
1912 pDrvIns->Internal.s.fVMSuspended = false;
1913 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance,
1914 pszDevName, iDevInstance, iLun);
1915 return false;
1916 }
1917 }
1918 }
1919 return true;
1920}
1921
1922
1923/**
1924 * Worker for PDMR3PowerOff that deals with one USB device instance.
1925 *
1926 * @param pUsbIns The USB device instance.
1927 * @param pAsync The structure for recording asynchronous
1928 * notification tasks.
1929 */
1930DECLINLINE(void) pdmR3PowerOffUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
1931{
1932 if (!pUsbIns->Internal.s.fVMSuspended)
1933 {
1934 pUsbIns->Internal.s.fVMSuspended = true;
1935 if (pUsbIns->pReg->pfnVMPowerOff)
1936 {
1937 uint64_t cNsElapsed = RTTimeNanoTS();
1938
1939 if (!pUsbIns->Internal.s.pfnAsyncNotify)
1940 {
1941 LogFlow(("PDMR3PowerOff: Notifying - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1942 pUsbIns->pReg->pfnVMPowerOff(pUsbIns);
1943 if (pUsbIns->Internal.s.pfnAsyncNotify)
1944 LogFlow(("PDMR3PowerOff: Async notification started - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1945 }
1946 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
1947 {
1948 LogFlow(("PDMR3PowerOff: Async notification completed - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1949 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
1950 }
1951 if (pUsbIns->Internal.s.pfnAsyncNotify)
1952 {
1953 pUsbIns->Internal.s.fVMSuspended = false;
1954 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
1955 }
1956
1957 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1958 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
1959 LogRel(("PDMR3PowerOff: USB device '%s'/%d took %'llu ns to power off\n",
1960 pUsbIns->pReg->szName, pUsbIns->iInstance, cNsElapsed));
1961
1962 }
1963 }
1964}
1965
1966
1967/**
1968 * Worker for PDMR3PowerOff that deals with one device instance.
1969 *
1970 * @param pDevIns The device instance.
1971 * @param pAsync The structure for recording asynchronous
1972 * notification tasks.
1973 */
1974DECLINLINE(void) pdmR3PowerOffDev(PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
1975{
1976 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED))
1977 {
1978 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_SUSPENDED;
1979 if (pDevIns->pReg->pfnPowerOff)
1980 {
1981 uint64_t cNsElapsed = RTTimeNanoTS();
1982 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1983
1984 if (!pDevIns->Internal.s.pfnAsyncNotify)
1985 {
1986 LogFlow(("PDMR3PowerOff: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1987 pDevIns->pReg->pfnPowerOff(pDevIns);
1988 if (pDevIns->Internal.s.pfnAsyncNotify)
1989 LogFlow(("PDMR3PowerOff: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1990 }
1991 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
1992 {
1993 LogFlow(("PDMR3PowerOff: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1994 pDevIns->Internal.s.pfnAsyncNotify = NULL;
1995 }
1996 if (pDevIns->Internal.s.pfnAsyncNotify)
1997 {
1998 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
1999 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
2000 }
2001
2002 PDMCritSectLeave(pDevIns->pCritSectRoR3);
2003 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2004 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
2005 LogFlow(("PDMR3PowerOff: Device '%s'/%d took %'llu ns to power off\n",
2006 pDevIns->pReg->szName, pDevIns->iInstance, cNsElapsed));
2007 }
2008 }
2009}
2010
2011
2012/**
2013 * This function will notify all the devices and their
2014 * attached drivers about the VM being powered off.
2015 *
2016 * @param pVM Pointer to the VM.
2017 */
2018VMMR3DECL(void) PDMR3PowerOff(PVM pVM)
2019{
2020 LogFlow(("PDMR3PowerOff:\n"));
2021 uint64_t cNsElapsed = RTTimeNanoTS();
2022
2023 /*
2024 * The outer loop repeats until there are no more async requests.
2025 */
2026 PDMNOTIFYASYNCSTATS Async;
2027 pdmR3NotifyAsyncInit(&Async, "PDMR3PowerOff");
2028 for (;;)
2029 {
2030 pdmR3NotifyAsyncBeginLoop(&Async);
2031
2032 /*
2033 * Iterate thru the device instances and USB device instances,
2034 * processing the drivers associated with those.
2035 *
2036 * The attached drivers are normally processed first. Some devices
2037 * (like DevAHCI) though needs to be notified before the drivers so
2038 * that it doesn't kick off any new requests after the drivers stopped
2039 * taking any. (DrvVD changes to read-only in this particular case.)
2040 */
2041 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2042 {
2043 unsigned const cAsyncStart = Async.cAsync;
2044
2045 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION)
2046 pdmR3PowerOffDev(pDevIns, &Async);
2047
2048 if (Async.cAsync == cAsyncStart)
2049 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2050 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2051 if (!pdmR3PowerOffDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
2052 break;
2053
2054 if ( Async.cAsync == cAsyncStart
2055 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION))
2056 pdmR3PowerOffDev(pDevIns, &Async);
2057 }
2058
2059#ifdef VBOX_WITH_USB
2060 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2061 {
2062 unsigned const cAsyncStart = Async.cAsync;
2063
2064 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2065 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2066 if (!pdmR3PowerOffDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
2067 break;
2068
2069 if (Async.cAsync == cAsyncStart)
2070 pdmR3PowerOffUsb(pUsbIns, &Async);
2071 }
2072#endif
2073 if (!Async.cAsync)
2074 break;
2075 pdmR3NotifyAsyncLog(&Async);
2076 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
2077 }
2078
2079 /*
2080 * Suspend all threads.
2081 */
2082 pdmR3ThreadSuspendAll(pVM);
2083
2084 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2085 LogRel(("PDMR3PowerOff: %'llu ns run time\n", cNsElapsed));
2086}
2087
2088
2089/**
2090 * Queries the base interface of a device instance.
2091 *
2092 * The caller can use this to query other interfaces the device implements
2093 * and use them to talk to the device.
2094 *
2095 * @returns VBox status code.
2096 * @param pUVM The user mode VM handle.
2097 * @param pszDevice Device name.
2098 * @param iInstance Device instance.
2099 * @param ppBase Where to store the pointer to the base device interface on success.
2100 * @remark We're not doing any locking ATM, so don't try call this at times when the
2101 * device chain is known to be updated.
2102 */
2103VMMR3DECL(int) PDMR3QueryDevice(PUVM pUVM, const char *pszDevice, unsigned iInstance, PPDMIBASE *ppBase)
2104{
2105 LogFlow(("PDMR3DeviceQuery: pszDevice=%p:{%s} iInstance=%u ppBase=%p\n", pszDevice, pszDevice, iInstance, ppBase));
2106 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2107 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2108
2109 /*
2110 * Iterate registered devices looking for the device.
2111 */
2112 size_t cchDevice = strlen(pszDevice);
2113 for (PPDMDEV pDev = pUVM->pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
2114 {
2115 if ( pDev->cchName == cchDevice
2116 && !memcmp(pDev->pReg->szName, pszDevice, cchDevice))
2117 {
2118 /*
2119 * Iterate device instances.
2120 */
2121 for (PPDMDEVINS pDevIns = pDev->pInstances; pDevIns; pDevIns = pDevIns->Internal.s.pPerDeviceNextR3)
2122 {
2123 if (pDevIns->iInstance == iInstance)
2124 {
2125 if (pDevIns->IBase.pfnQueryInterface)
2126 {
2127 *ppBase = &pDevIns->IBase;
2128 LogFlow(("PDMR3DeviceQuery: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
2129 return VINF_SUCCESS;
2130 }
2131
2132 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NO_IBASE\n"));
2133 return VERR_PDM_DEVICE_INSTANCE_NO_IBASE;
2134 }
2135 }
2136
2137 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NOT_FOUND\n"));
2138 return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND;
2139 }
2140 }
2141
2142 LogFlow(("PDMR3QueryDevice: returns VERR_PDM_DEVICE_NOT_FOUND\n"));
2143 return VERR_PDM_DEVICE_NOT_FOUND;
2144}
2145
2146
2147/**
2148 * Queries the base interface of a device LUN.
2149 *
2150 * This differs from PDMR3QueryLun by that it returns the interface on the
2151 * device and not the top level driver.
2152 *
2153 * @returns VBox status code.
2154 * @param pUVM The user mode VM handle.
2155 * @param pszDevice Device name.
2156 * @param iInstance Device instance.
2157 * @param iLun The Logical Unit to obtain the interface of.
2158 * @param ppBase Where to store the base interface pointer.
2159 * @remark We're not doing any locking ATM, so don't try call this at times when the
2160 * device chain is known to be updated.
2161 */
2162VMMR3DECL(int) PDMR3QueryDeviceLun(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
2163{
2164 LogFlow(("PDMR3QueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
2165 pszDevice, pszDevice, iInstance, iLun, ppBase));
2166 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2167 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2168
2169 /*
2170 * Find the LUN.
2171 */
2172 PPDMLUN pLun;
2173 int rc = pdmR3DevFindLun(pUVM->pVM, pszDevice, iInstance, iLun, &pLun);
2174 if (RT_SUCCESS(rc))
2175 {
2176 *ppBase = pLun->pBase;
2177 LogFlow(("PDMR3QueryDeviceLun: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
2178 return VINF_SUCCESS;
2179 }
2180 LogFlow(("PDMR3QueryDeviceLun: returns %Rrc\n", rc));
2181 return rc;
2182}
2183
2184
2185/**
2186 * Query the interface of the top level driver on a LUN.
2187 *
2188 * @returns VBox status code.
2189 * @param pUVM The user mode VM handle.
2190 * @param pszDevice Device name.
2191 * @param iInstance Device instance.
2192 * @param iLun The Logical Unit to obtain the interface of.
2193 * @param ppBase Where to store the base interface pointer.
2194 * @remark We're not doing any locking ATM, so don't try call this at times when the
2195 * device chain is known to be updated.
2196 */
2197VMMR3DECL(int) PDMR3QueryLun(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
2198{
2199 LogFlow(("PDMR3QueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
2200 pszDevice, pszDevice, iInstance, iLun, ppBase));
2201 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2202 PVM pVM = pUVM->pVM;
2203 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
2204
2205 /*
2206 * Find the LUN.
2207 */
2208 PPDMLUN pLun;
2209 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
2210 if (RT_SUCCESS(rc))
2211 {
2212 if (pLun->pTop)
2213 {
2214 *ppBase = &pLun->pTop->IBase;
2215 LogFlow(("PDMR3QueryLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
2216 return VINF_SUCCESS;
2217 }
2218 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
2219 }
2220 LogFlow(("PDMR3QueryLun: returns %Rrc\n", rc));
2221 return rc;
2222}
2223
2224
2225/**
2226 * Query the interface of a named driver on a LUN.
2227 *
2228 * If the driver appears more than once in the driver chain, the first instance
2229 * is returned.
2230 *
2231 * @returns VBox status code.
2232 * @param pUVM The user mode VM handle.
2233 * @param pszDevice Device name.
2234 * @param iInstance Device instance.
2235 * @param iLun The Logical Unit to obtain the interface of.
2236 * @param pszDriver The driver name.
2237 * @param ppBase Where to store the base interface pointer.
2238 *
2239 * @remark We're not doing any locking ATM, so don't try call this at times when the
2240 * device chain is known to be updated.
2241 */
2242VMMR3DECL(int) PDMR3QueryDriverOnLun(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, const char *pszDriver, PPPDMIBASE ppBase)
2243{
2244 LogFlow(("PDMR3QueryDriverOnLun: pszDevice=%p:{%s} iInstance=%u iLun=%u pszDriver=%p:{%s} ppBase=%p\n",
2245 pszDevice, pszDevice, iInstance, iLun, pszDriver, pszDriver, ppBase));
2246 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2247 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2248
2249 /*
2250 * Find the LUN.
2251 */
2252 PPDMLUN pLun;
2253 int rc = pdmR3DevFindLun(pUVM->pVM, pszDevice, iInstance, iLun, &pLun);
2254 if (RT_SUCCESS(rc))
2255 {
2256 if (pLun->pTop)
2257 {
2258 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2259 if (!strcmp(pDrvIns->pReg->szName, pszDriver))
2260 {
2261 *ppBase = &pDrvIns->IBase;
2262 LogFlow(("PDMR3QueryDriverOnLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
2263 return VINF_SUCCESS;
2264
2265 }
2266 rc = VERR_PDM_DRIVER_NOT_FOUND;
2267 }
2268 else
2269 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
2270 }
2271 LogFlow(("PDMR3QueryDriverOnLun: returns %Rrc\n", rc));
2272 return rc;
2273}
2274
2275/**
2276 * Executes pending DMA transfers.
2277 * Forced Action handler.
2278 *
2279 * @param pVM Pointer to the VM.
2280 */
2281VMMR3DECL(void) PDMR3DmaRun(PVM pVM)
2282{
2283 /* Note! Not really SMP safe; restrict it to VCPU 0. */
2284 if (VMMGetCpuId(pVM) != 0)
2285 return;
2286
2287 if (VM_FF_TESTANDCLEAR(pVM, VM_FF_PDM_DMA))
2288 {
2289 if (pVM->pdm.s.pDmac)
2290 {
2291 bool fMore = pVM->pdm.s.pDmac->Reg.pfnRun(pVM->pdm.s.pDmac->pDevIns);
2292 if (fMore)
2293 VM_FF_SET(pVM, VM_FF_PDM_DMA);
2294 }
2295 }
2296}
2297
2298
2299/**
2300 * Service a VMMCALLRING3_PDM_LOCK call.
2301 *
2302 * @returns VBox status code.
2303 * @param pVM Pointer to the VM.
2304 */
2305VMMR3_INT_DECL(int) PDMR3LockCall(PVM pVM)
2306{
2307 return PDMR3CritSectEnterEx(&pVM->pdm.s.CritSect, true /* fHostCall */);
2308}
2309
2310
2311/**
2312 * Registers the VMM device heap
2313 *
2314 * @returns VBox status code.
2315 * @param pVM Pointer to the VM.
2316 * @param GCPhys The physical address.
2317 * @param pvHeap Ring-3 pointer.
2318 * @param cbSize Size of the heap.
2319 */
2320VMMR3_INT_DECL(int) PDMR3VmmDevHeapRegister(PVM pVM, RTGCPHYS GCPhys, RTR3PTR pvHeap, unsigned cbSize)
2321{
2322 Assert(pVM->pdm.s.pvVMMDevHeap == NULL);
2323
2324 Log(("PDMR3VmmDevHeapRegister %RGp %RHv %x\n", GCPhys, pvHeap, cbSize));
2325 pVM->pdm.s.pvVMMDevHeap = pvHeap;
2326 pVM->pdm.s.GCPhysVMMDevHeap = GCPhys;
2327 pVM->pdm.s.cbVMMDevHeap = cbSize;
2328 pVM->pdm.s.cbVMMDevHeapLeft = cbSize;
2329 return VINF_SUCCESS;
2330}
2331
2332
2333/**
2334 * Unregisters the VMM device heap
2335 *
2336 * @returns VBox status code.
2337 * @param pVM Pointer to the VM.
2338 * @param GCPhys The physical address.
2339 */
2340VMMR3_INT_DECL(int) PDMR3VmmDevHeapUnregister(PVM pVM, RTGCPHYS GCPhys)
2341{
2342 Assert(pVM->pdm.s.GCPhysVMMDevHeap == GCPhys);
2343
2344 Log(("PDMR3VmmDevHeapUnregister %RGp\n", GCPhys));
2345 pVM->pdm.s.pvVMMDevHeap = NULL;
2346 pVM->pdm.s.GCPhysVMMDevHeap = NIL_RTGCPHYS;
2347 pVM->pdm.s.cbVMMDevHeap = 0;
2348 pVM->pdm.s.cbVMMDevHeapLeft = 0;
2349 return VINF_SUCCESS;
2350}
2351
2352
2353/**
2354 * Allocates memory from the VMM device heap
2355 *
2356 * @returns VBox status code.
2357 * @param pVM Pointer to the VM.
2358 * @param cbSize Allocation size.
2359 * @param pv Ring-3 pointer. (out)
2360 */
2361VMMR3_INT_DECL(int) PDMR3VmmDevHeapAlloc(PVM pVM, size_t cbSize, RTR3PTR *ppv)
2362{
2363#ifdef DEBUG_bird
2364 if (!cbSize || cbSize > pVM->pdm.s.cbVMMDevHeapLeft)
2365 return VERR_NO_MEMORY;
2366#else
2367 AssertReturn(cbSize && cbSize <= pVM->pdm.s.cbVMMDevHeapLeft, VERR_NO_MEMORY);
2368#endif
2369
2370 Log(("PDMR3VMMDevHeapAlloc: %#zx\n", cbSize));
2371
2372 /** @todo Not a real heap as there's currently only one user. */
2373 *ppv = pVM->pdm.s.pvVMMDevHeap;
2374 pVM->pdm.s.cbVMMDevHeapLeft = 0;
2375 return VINF_SUCCESS;
2376}
2377
2378
2379/**
2380 * Frees memory from the VMM device heap
2381 *
2382 * @returns VBox status code.
2383 * @param pVM Pointer to the VM.
2384 * @param pv Ring-3 pointer.
2385 */
2386VMMR3_INT_DECL(int) PDMR3VmmDevHeapFree(PVM pVM, RTR3PTR pv)
2387{
2388 Log(("PDMR3VmmDevHeapFree: %RHv\n", pv));
2389
2390 /** @todo not a real heap as there's currently only one user. */
2391 pVM->pdm.s.cbVMMDevHeapLeft = pVM->pdm.s.cbVMMDevHeap;
2392 return VINF_SUCCESS;
2393}
2394
2395
2396/**
2397 * Worker for DBGFR3TraceConfig that checks if the given tracing group name
2398 * matches a device or driver name and applies the tracing config change.
2399 *
2400 * @returns VINF_SUCCESS or VERR_NOT_FOUND.
2401 * @param pVM Pointer to the VM.
2402 * @param pszName The tracing config group name. This is NULL if
2403 * the operation applies to every device and
2404 * driver.
2405 * @param cchName The length to match.
2406 * @param fEnable Whether to enable or disable the corresponding
2407 * trace points.
2408 * @param fApply Whether to actually apply the changes or just do
2409 * existence checks.
2410 */
2411VMMR3_INT_DECL(int) PDMR3TracingConfig(PVM pVM, const char *pszName, size_t cchName, bool fEnable, bool fApply)
2412{
2413 /** @todo This code is potentially racing driver attaching and detaching. */
2414
2415 /*
2416 * Applies to all.
2417 */
2418 if (pszName == NULL)
2419 {
2420 AssertReturn(fApply, VINF_SUCCESS);
2421
2422 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2423 {
2424 pDevIns->fTracing = fEnable;
2425 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2426 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2427 pDrvIns->fTracing = fEnable;
2428 }
2429
2430#ifdef VBOX_WITH_USB
2431 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2432 {
2433 pUsbIns->fTracing = fEnable;
2434 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2435 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2436 pDrvIns->fTracing = fEnable;
2437
2438 }
2439#endif
2440 return VINF_SUCCESS;
2441 }
2442
2443 /*
2444 * Specific devices, USB devices or drivers.
2445 * Decode prefix to figure which of these it applies to.
2446 */
2447 if (cchName <= 3)
2448 return VERR_NOT_FOUND;
2449
2450 uint32_t cMatches = 0;
2451 if (!strncmp("dev", pszName, 3))
2452 {
2453 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2454 {
2455 const char *pszDevName = pDevIns->Internal.s.pDevR3->pReg->szName;
2456 size_t cchDevName = strlen(pszDevName);
2457 if ( ( cchDevName == cchName
2458 && RTStrNICmp(pszName, pszDevName, cchDevName))
2459 || ( cchDevName == cchName - 3
2460 && RTStrNICmp(pszName + 3, pszDevName, cchDevName)) )
2461 {
2462 cMatches++;
2463 if (fApply)
2464 pDevIns->fTracing = fEnable;
2465 }
2466 }
2467 }
2468 else if (!strncmp("usb", pszName, 3))
2469 {
2470 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2471 {
2472 const char *pszUsbName = pUsbIns->Internal.s.pUsbDev->pReg->szName;
2473 size_t cchUsbName = strlen(pszUsbName);
2474 if ( ( cchUsbName == cchName
2475 && RTStrNICmp(pszName, pszUsbName, cchUsbName))
2476 || ( cchUsbName == cchName - 3
2477 && RTStrNICmp(pszName + 3, pszUsbName, cchUsbName)) )
2478 {
2479 cMatches++;
2480 if (fApply)
2481 pUsbIns->fTracing = fEnable;
2482 }
2483 }
2484 }
2485 else if (!strncmp("drv", pszName, 3))
2486 {
2487 AssertReturn(fApply, VINF_SUCCESS);
2488
2489 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2490 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2491 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2492 {
2493 const char *pszDrvName = pDrvIns->Internal.s.pDrv->pReg->szName;
2494 size_t cchDrvName = strlen(pszDrvName);
2495 if ( ( cchDrvName == cchName
2496 && RTStrNICmp(pszName, pszDrvName, cchDrvName))
2497 || ( cchDrvName == cchName - 3
2498 && RTStrNICmp(pszName + 3, pszDrvName, cchDrvName)) )
2499 {
2500 cMatches++;
2501 if (fApply)
2502 pDrvIns->fTracing = fEnable;
2503 }
2504 }
2505
2506#ifdef VBOX_WITH_USB
2507 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2508 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2509 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2510 {
2511 const char *pszDrvName = pDrvIns->Internal.s.pDrv->pReg->szName;
2512 size_t cchDrvName = strlen(pszDrvName);
2513 if ( ( cchDrvName == cchName
2514 && RTStrNICmp(pszName, pszDrvName, cchDrvName))
2515 || ( cchDrvName == cchName - 3
2516 && RTStrNICmp(pszName + 3, pszDrvName, cchDrvName)) )
2517 {
2518 cMatches++;
2519 if (fApply)
2520 pDrvIns->fTracing = fEnable;
2521 }
2522 }
2523#endif
2524 }
2525 else
2526 return VERR_NOT_FOUND;
2527
2528 return cMatches > 0 ? VINF_SUCCESS : VERR_NOT_FOUND;
2529}
2530
2531
2532/**
2533 * Worker for DBGFR3TraceQueryConfig that checks whether all drivers, devices,
2534 * and USB device have the same tracing settings.
2535 *
2536 * @returns true / false.
2537 * @param pVM Pointer to the VM.
2538 * @param fEnabled The tracing setting to check for.
2539 */
2540VMMR3_INT_DECL(bool) PDMR3TracingAreAll(PVM pVM, bool fEnabled)
2541{
2542 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2543 {
2544 if (pDevIns->fTracing != (uint32_t)fEnabled)
2545 return false;
2546
2547 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; 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
2553#ifdef VBOX_WITH_USB
2554 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2555 {
2556 if (pUsbIns->fTracing != (uint32_t)fEnabled)
2557 return false;
2558
2559 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2560 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2561 if (pDrvIns->fTracing != (uint32_t)fEnabled)
2562 return false;
2563 }
2564#endif
2565
2566 return true;
2567}
2568
2569
2570/**
2571 * Worker for PDMR3TracingQueryConfig that adds a prefixed name to the output
2572 * string.
2573 *
2574 * @returns VINF_SUCCESS or VERR_BUFFER_OVERFLOW
2575 * @param ppszDst The pointer to the output buffer pointer.
2576 * @param pcbDst The pointer to the output buffer size.
2577 * @param fSpace Whether to add a space before the name.
2578 * @param pszPrefix The name prefix.
2579 * @param pszName The name.
2580 */
2581static int pdmR3TracingAdd(char **ppszDst, size_t *pcbDst, bool fSpace, const char *pszPrefix, const char *pszName)
2582{
2583 size_t const cchPrefix = strlen(pszPrefix);
2584 if (!RTStrNICmp(pszPrefix, pszName, cchPrefix))
2585 pszName += cchPrefix;
2586 size_t const cchName = strlen(pszName);
2587
2588 size_t const cchThis = cchName + cchPrefix + fSpace;
2589 if (cchThis >= *pcbDst)
2590 return VERR_BUFFER_OVERFLOW;
2591 if (fSpace)
2592 {
2593 **ppszDst = ' ';
2594 memcpy(*ppszDst + 1, pszPrefix, cchPrefix);
2595 memcpy(*ppszDst + 1 + cchPrefix, pszName, cchName + 1);
2596 }
2597 else
2598 {
2599 memcpy(*ppszDst, pszPrefix, cchPrefix);
2600 memcpy(*ppszDst + cchPrefix, pszName, cchName + 1);
2601 }
2602 *ppszDst += cchThis;
2603 *pcbDst -= cchThis;
2604 return VINF_SUCCESS;
2605}
2606
2607
2608/**
2609 * Worker for DBGFR3TraceQueryConfig use when not everything is either enabled
2610 * or disabled.
2611 *
2612 * @returns VINF_SUCCESS or VERR_BUFFER_OVERFLOW
2613 * @param pVM Pointer to the VM.
2614 * @param pszConfig Where to store the config spec.
2615 * @param cbConfig The size of the output buffer.
2616 */
2617VMMR3_INT_DECL(int) PDMR3TracingQueryConfig(PVM pVM, char *pszConfig, size_t cbConfig)
2618{
2619 int rc;
2620 char *pszDst = pszConfig;
2621 size_t cbDst = cbConfig;
2622
2623 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2624 {
2625 if (pDevIns->fTracing)
2626 {
2627 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "dev", pDevIns->Internal.s.pDevR3->pReg->szName);
2628 if (RT_FAILURE(rc))
2629 return rc;
2630 }
2631
2632 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2633 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2634 if (pDrvIns->fTracing)
2635 {
2636 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "drv", pDrvIns->Internal.s.pDrv->pReg->szName);
2637 if (RT_FAILURE(rc))
2638 return rc;
2639 }
2640 }
2641
2642#ifdef VBOX_WITH_USB
2643 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2644 {
2645 if (pUsbIns->fTracing)
2646 {
2647 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "usb", pUsbIns->Internal.s.pUsbDev->pReg->szName);
2648 if (RT_FAILURE(rc))
2649 return rc;
2650 }
2651
2652 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2653 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2654 if (pDrvIns->fTracing)
2655 {
2656 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "drv", pDrvIns->Internal.s.pDrv->pReg->szName);
2657 if (RT_FAILURE(rc))
2658 return rc;
2659 }
2660 }
2661#endif
2662
2663 return VINF_SUCCESS;
2664}
2665
2666
2667/**
2668 * Checks that a PDMDRVREG::szName, PDMDEVREG::szName or PDMUSBREG::szName
2669 * field contains only a limited set of ASCII characters.
2670 *
2671 * @returns true / false.
2672 * @param pszName The name to validate.
2673 */
2674bool pdmR3IsValidName(const char *pszName)
2675{
2676 char ch;
2677 while ( (ch = *pszName) != '\0'
2678 && ( RT_C_IS_ALNUM(ch)
2679 || ch == '-'
2680 || ch == ' ' /** @todo disallow this! */
2681 || ch == '_') )
2682 pszName++;
2683 return ch == '\0';
2684}
2685
2686
2687/**
2688 * Info handler for 'pdmtracingids'.
2689 *
2690 * @param pVM Pointer to the VM.
2691 * @param pHlp The output helpers.
2692 * @param pszArgs The optional user arguments.
2693 *
2694 * @remarks Can be called on most threads.
2695 */
2696static DECLCALLBACK(void) pdmR3InfoTracingIds(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2697{
2698 /*
2699 * Parse the argument (optional).
2700 */
2701 if ( pszArgs
2702 && *pszArgs
2703 && strcmp(pszArgs, "all")
2704 && strcmp(pszArgs, "devices")
2705 && strcmp(pszArgs, "drivers")
2706 && strcmp(pszArgs, "usb"))
2707 {
2708 pHlp->pfnPrintf(pHlp, "Unable to grok '%s'\n", pszArgs);
2709 return;
2710 }
2711 bool fAll = !pszArgs || !*pszArgs || !strcmp(pszArgs, "all");
2712 bool fDevices = fAll || !strcmp(pszArgs, "devices");
2713 bool fUsbDevs = fAll || !strcmp(pszArgs, "usb");
2714 bool fDrivers = fAll || !strcmp(pszArgs, "drivers");
2715
2716 /*
2717 * Produce the requested output.
2718 */
2719/** @todo lock PDM lists! */
2720 /* devices */
2721 if (fDevices)
2722 {
2723 pHlp->pfnPrintf(pHlp, "Device tracing IDs:\n");
2724 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2725 pHlp->pfnPrintf(pHlp, "%05u %s\n", pDevIns->idTracing, pDevIns->Internal.s.pDevR3->pReg->szName);
2726 }
2727
2728 /* USB devices */
2729 if (fUsbDevs)
2730 {
2731 pHlp->pfnPrintf(pHlp, "USB device tracing IDs:\n");
2732 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2733 pHlp->pfnPrintf(pHlp, "%05u %s\n", pUsbIns->idTracing, pUsbIns->Internal.s.pUsbDev->pReg->szName);
2734 }
2735
2736 /* Drivers */
2737 if (fDrivers)
2738 {
2739 pHlp->pfnPrintf(pHlp, "Driver tracing IDs:\n");
2740 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2741 {
2742 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; 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, pDevIns->Internal.s.pDevR3->pReg->szName);
2749 }
2750 }
2751
2752 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2753 {
2754 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2755 {
2756 uint32_t iLevel = 0;
2757 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown, iLevel++)
2758 pHlp->pfnPrintf(pHlp, "%05u %s (level %u, lun %u, dev %s)\n",
2759 pDrvIns->idTracing, pDrvIns->Internal.s.pDrv->pReg->szName,
2760 iLevel, pLun->iLun, pUsbIns->Internal.s.pUsbDev->pReg->szName);
2761 }
2762 }
2763 }
2764}
2765
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