VirtualBox

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

Last change on this file since 55705 was 54385, checked in by vboxsync, 10 years ago

PDM/Audio: Ignore old AudioSniffer device when loading older saved states.

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