VirtualBox

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

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

PDM/Audio: Removed old audio architecture.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 104.4 KB
Line 
1/* $Id: PDM.cpp 56085 2015-05-26 16:39:58Z 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 /* Skip the non-existing (deprecated) "AudioSniffer" device stored in the saved state. */
1040 if ( uVersion <= PDM_SAVED_STATE_VERSION_PRE_PDM_AUDIO
1041 && !RTStrCmp(szName, "AudioSniffer"))
1042 fSkip = true;
1043
1044 if (!fSkip)
1045 {
1046 LogRel(("Device '%s'/%d not found in current config\n", szName, iInstance));
1047 if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
1048 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Device '%s'/%d not found in current config"), szName, iInstance);
1049 }
1050 }
1051 }
1052
1053 /*
1054 * Check that no additional devices were configured.
1055 */
1056 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1057 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_FOUND))
1058 {
1059 LogRel(("Device '%s'/%d not found in the saved state\n", pDevIns->pReg->szName, pDevIns->iInstance));
1060 if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
1061 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Device '%s'/%d not found in the saved state"),
1062 pDevIns->pReg->szName, pDevIns->iInstance);
1063 }
1064
1065 return VINF_SUCCESS;
1066}
1067
1068
1069/**
1070 * Worker for PDMR3PowerOn that deals with one driver.
1071 *
1072 * @param pDrvIns The driver instance.
1073 * @param pszDevName The parent device name.
1074 * @param iDevInstance The parent device instance number.
1075 * @param iLun The parent LUN number.
1076 */
1077DECLINLINE(int) pdmR3PowerOnDrv(PPDMDRVINS pDrvIns, const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1078{
1079 Assert(pDrvIns->Internal.s.fVMSuspended);
1080 if (pDrvIns->pReg->pfnPowerOn)
1081 {
1082 LogFlow(("PDMR3PowerOn: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1083 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1084 int rc = VINF_SUCCESS; pDrvIns->pReg->pfnPowerOn(pDrvIns);
1085 if (RT_FAILURE(rc))
1086 {
1087 LogRel(("PDMR3PowerOn: driver '%s'/%d on LUN#%d of device '%s'/%d -> %Rrc\n",
1088 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, rc));
1089 return rc;
1090 }
1091 }
1092 pDrvIns->Internal.s.fVMSuspended = false;
1093 return VINF_SUCCESS;
1094}
1095
1096
1097/**
1098 * Worker for PDMR3PowerOn that deals with one USB device instance.
1099 *
1100 * @returns VBox status code.
1101 * @param pUsbIns The USB device instance.
1102 */
1103DECLINLINE(int) pdmR3PowerOnUsb(PPDMUSBINS pUsbIns)
1104{
1105 Assert(pUsbIns->Internal.s.fVMSuspended);
1106 if (pUsbIns->pReg->pfnVMPowerOn)
1107 {
1108 LogFlow(("PDMR3PowerOn: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1109 int rc = VINF_SUCCESS; pUsbIns->pReg->pfnVMPowerOn(pUsbIns);
1110 if (RT_FAILURE(rc))
1111 {
1112 LogRel(("PDMR3PowerOn: device '%s'/%d -> %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1113 return rc;
1114 }
1115 }
1116 pUsbIns->Internal.s.fVMSuspended = false;
1117 return VINF_SUCCESS;
1118}
1119
1120
1121/**
1122 * Worker for PDMR3PowerOn that deals with one device instance.
1123 *
1124 * @returns VBox status code.
1125 * @param pDevIns The device instance.
1126 */
1127DECLINLINE(int) pdmR3PowerOnDev(PPDMDEVINS pDevIns)
1128{
1129 Assert(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED);
1130 if (pDevIns->pReg->pfnPowerOn)
1131 {
1132 LogFlow(("PDMR3PowerOn: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1133 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1134 int rc = VINF_SUCCESS; pDevIns->pReg->pfnPowerOn(pDevIns);
1135 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1136 if (RT_FAILURE(rc))
1137 {
1138 LogRel(("PDMR3PowerOn: device '%s'/%d -> %Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
1139 return rc;
1140 }
1141 }
1142 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
1143 return VINF_SUCCESS;
1144}
1145
1146
1147/**
1148 * This function will notify all the devices and their
1149 * attached drivers about the VM now being powered on.
1150 *
1151 * @param pVM Pointer to the VM.
1152 */
1153VMMR3DECL(void) PDMR3PowerOn(PVM pVM)
1154{
1155 LogFlow(("PDMR3PowerOn:\n"));
1156
1157 /*
1158 * Iterate thru the device instances and USB device instances,
1159 * processing the drivers associated with those.
1160 */
1161 int rc = VINF_SUCCESS;
1162 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns && RT_SUCCESS(rc); pDevIns = pDevIns->Internal.s.pNextR3)
1163 {
1164 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1165 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1166 rc = pdmR3PowerOnDrv(pDrvIns, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun);
1167 if (RT_SUCCESS(rc))
1168 rc = pdmR3PowerOnDev(pDevIns);
1169 }
1170
1171#ifdef VBOX_WITH_USB
1172 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns && RT_SUCCESS(rc); pUsbIns = pUsbIns->Internal.s.pNext)
1173 {
1174 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1175 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1176 rc = pdmR3PowerOnDrv(pDrvIns, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun);
1177 if (RT_SUCCESS(rc))
1178 rc = pdmR3PowerOnUsb(pUsbIns);
1179 }
1180#endif
1181
1182#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
1183 pdmR3AsyncCompletionResume(pVM);
1184#endif
1185
1186 /*
1187 * Resume all threads.
1188 */
1189 if (RT_SUCCESS(rc))
1190 pdmR3ThreadResumeAll(pVM);
1191
1192 /*
1193 * On failure, clean up via PDMR3Suspend.
1194 */
1195 if (RT_FAILURE(rc))
1196 PDMR3Suspend(pVM);
1197
1198 LogFlow(("PDMR3PowerOn: returns %Rrc\n", rc));
1199 return /*rc*/;
1200}
1201
1202
1203/**
1204 * Initializes the asynchronous notifi stats structure.
1205 *
1206 * @param pThis The asynchronous notifification stats.
1207 * @param pszOp The name of the operation.
1208 */
1209static void pdmR3NotifyAsyncInit(PPDMNOTIFYASYNCSTATS pThis, const char *pszOp)
1210{
1211 pThis->uStartNsTs = RTTimeNanoTS();
1212 pThis->cNsElapsedNextLog = 0;
1213 pThis->cLoops = 0;
1214 pThis->cAsync = 0;
1215 pThis->pszOp = pszOp;
1216 pThis->offList = 0;
1217 pThis->szList[0] = '\0';
1218}
1219
1220
1221/**
1222 * Begin a new loop, prepares to gather new stats.
1223 *
1224 * @param pThis The asynchronous notifification stats.
1225 */
1226static void pdmR3NotifyAsyncBeginLoop(PPDMNOTIFYASYNCSTATS pThis)
1227{
1228 pThis->cLoops++;
1229 pThis->cAsync = 0;
1230 pThis->offList = 0;
1231 pThis->szList[0] = '\0';
1232}
1233
1234
1235/**
1236 * Records a device or USB device with a pending asynchronous notification.
1237 *
1238 * @param pThis The asynchronous notifification stats.
1239 * @param pszName The name of the thing.
1240 * @param iInstance The instance number.
1241 */
1242static void pdmR3NotifyAsyncAdd(PPDMNOTIFYASYNCSTATS pThis, const char *pszName, uint32_t iInstance)
1243{
1244 pThis->cAsync++;
1245 if (pThis->offList < sizeof(pThis->szList) - 4)
1246 pThis->offList += RTStrPrintf(&pThis->szList[pThis->offList], sizeof(pThis->szList) - pThis->offList,
1247 pThis->offList == 0 ? "%s/%u" : ", %s/%u",
1248 pszName, iInstance);
1249}
1250
1251
1252/**
1253 * Records the asynchronous completition of a reset, suspend or power off.
1254 *
1255 * @param pThis The asynchronous notifification stats.
1256 * @param pszDrvName The driver name.
1257 * @param iDrvInstance The driver instance number.
1258 * @param pszDevName The device or USB device name.
1259 * @param iDevInstance The device or USB device instance number.
1260 * @param iLun The LUN.
1261 */
1262static void pdmR3NotifyAsyncAddDrv(PPDMNOTIFYASYNCSTATS pThis, const char *pszDrvName, uint32_t iDrvInstance,
1263 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1264{
1265 pThis->cAsync++;
1266 if (pThis->offList < sizeof(pThis->szList) - 8)
1267 pThis->offList += RTStrPrintf(&pThis->szList[pThis->offList], sizeof(pThis->szList) - pThis->offList,
1268 pThis->offList == 0 ? "%s/%u/%u/%s/%u" : ", %s/%u/%u/%s/%u",
1269 pszDevName, iDevInstance, iLun, pszDrvName, iDrvInstance);
1270}
1271
1272
1273/**
1274 * Log the stats.
1275 *
1276 * @param pThis The asynchronous notifification stats.
1277 */
1278static void pdmR3NotifyAsyncLog(PPDMNOTIFYASYNCSTATS pThis)
1279{
1280 /*
1281 * Return if we shouldn't log at this point.
1282 * We log with an internval increasing from 0 sec to 60 sec.
1283 */
1284 if (!pThis->cAsync)
1285 return;
1286
1287 uint64_t cNsElapsed = RTTimeNanoTS() - pThis->uStartNsTs;
1288 if (cNsElapsed < pThis->cNsElapsedNextLog)
1289 return;
1290
1291 if (pThis->cNsElapsedNextLog == 0)
1292 pThis->cNsElapsedNextLog = RT_NS_1SEC;
1293 else if (pThis->cNsElapsedNextLog >= RT_NS_1MIN / 2)
1294 pThis->cNsElapsedNextLog = RT_NS_1MIN;
1295 else
1296 pThis->cNsElapsedNextLog *= 2;
1297
1298 /*
1299 * Do the logging.
1300 */
1301 LogRel(("%s: after %5llu ms, %u loops: %u async tasks - %s\n",
1302 pThis->pszOp, cNsElapsed / RT_NS_1MS, pThis->cLoops, pThis->cAsync, pThis->szList));
1303}
1304
1305
1306/**
1307 * Wait for events and process pending requests.
1308 *
1309 * @param pThis The asynchronous notifification stats.
1310 * @param pVM Pointer to the VM.
1311 */
1312static void pdmR3NotifyAsyncWaitAndProcessRequests(PPDMNOTIFYASYNCSTATS pThis, PVM pVM)
1313{
1314 VM_ASSERT_EMT0(pVM);
1315 int rc = VMR3AsyncPdmNotificationWaitU(&pVM->pUVM->aCpus[0]);
1316 AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc - %s - %s\n", rc, pThis->pszOp, pThis->szList));
1317
1318 rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY, true /*fPriorityOnly*/);
1319 AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc - %s - %s\n", rc, pThis->pszOp, pThis->szList));
1320 rc = VMR3ReqProcessU(pVM->pUVM, 0/*idDstCpu*/, true /*fPriorityOnly*/);
1321 AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc - %s - %s\n", rc, pThis->pszOp, pThis->szList));
1322}
1323
1324
1325/**
1326 * Worker for PDMR3Reset that deals with one driver.
1327 *
1328 * @param pDrvIns The driver instance.
1329 * @param pAsync The structure for recording asynchronous
1330 * notification tasks.
1331 * @param pszDevName The parent device name.
1332 * @param iDevInstance The parent device instance number.
1333 * @param iLun The parent LUN number.
1334 */
1335DECLINLINE(bool) pdmR3ResetDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
1336 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1337{
1338 if (!pDrvIns->Internal.s.fVMReset)
1339 {
1340 pDrvIns->Internal.s.fVMReset = true;
1341 if (pDrvIns->pReg->pfnReset)
1342 {
1343 if (!pDrvIns->Internal.s.pfnAsyncNotify)
1344 {
1345 LogFlow(("PDMR3Reset: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1346 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1347 pDrvIns->pReg->pfnReset(pDrvIns);
1348 if (pDrvIns->Internal.s.pfnAsyncNotify)
1349 LogFlow(("PDMR3Reset: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1350 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1351 }
1352 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
1353 {
1354 LogFlow(("PDMR3Reset: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1355 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1356 pDrvIns->Internal.s.pfnAsyncNotify = NULL;
1357 }
1358 if (pDrvIns->Internal.s.pfnAsyncNotify)
1359 {
1360 pDrvIns->Internal.s.fVMReset = false;
1361 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance,
1362 pszDevName, iDevInstance, iLun);
1363 return false;
1364 }
1365 }
1366 }
1367 return true;
1368}
1369
1370
1371/**
1372 * Worker for PDMR3Reset that deals with one USB device instance.
1373 *
1374 * @param pUsbIns The USB device instance.
1375 * @param pAsync The structure for recording asynchronous
1376 * notification tasks.
1377 */
1378DECLINLINE(void) pdmR3ResetUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
1379{
1380 if (!pUsbIns->Internal.s.fVMReset)
1381 {
1382 pUsbIns->Internal.s.fVMReset = true;
1383 if (pUsbIns->pReg->pfnVMReset)
1384 {
1385 if (!pUsbIns->Internal.s.pfnAsyncNotify)
1386 {
1387 LogFlow(("PDMR3Reset: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1388 pUsbIns->pReg->pfnVMReset(pUsbIns);
1389 if (pUsbIns->Internal.s.pfnAsyncNotify)
1390 LogFlow(("PDMR3Reset: Async notification started - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1391 }
1392 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
1393 {
1394 LogFlow(("PDMR3Reset: Async notification completed - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1395 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
1396 }
1397 if (pUsbIns->Internal.s.pfnAsyncNotify)
1398 {
1399 pUsbIns->Internal.s.fVMReset = false;
1400 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
1401 }
1402 }
1403 }
1404}
1405
1406
1407/**
1408 * Worker for PDMR3Reset that deals with one device instance.
1409 *
1410 * @param pDevIns The device instance.
1411 * @param pAsync The structure for recording asynchronous
1412 * notification tasks.
1413 */
1414DECLINLINE(void) pdmR3ResetDev(PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
1415{
1416 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_RESET))
1417 {
1418 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_RESET;
1419 if (pDevIns->pReg->pfnReset)
1420 {
1421 uint64_t cNsElapsed = RTTimeNanoTS();
1422 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1423
1424 if (!pDevIns->Internal.s.pfnAsyncNotify)
1425 {
1426 LogFlow(("PDMR3Reset: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1427 pDevIns->pReg->pfnReset(pDevIns);
1428 if (pDevIns->Internal.s.pfnAsyncNotify)
1429 LogFlow(("PDMR3Reset: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1430 }
1431 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
1432 {
1433 LogFlow(("PDMR3Reset: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1434 pDevIns->Internal.s.pfnAsyncNotify = NULL;
1435 }
1436 if (pDevIns->Internal.s.pfnAsyncNotify)
1437 {
1438 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_RESET;
1439 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
1440 }
1441
1442 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1443 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1444 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1445 LogRel(("PDMR3Reset: device '%s'/%d took %'llu ns to reset\n",
1446 pDevIns->pReg->szName, pDevIns->iInstance, cNsElapsed));
1447 }
1448 }
1449}
1450
1451
1452/**
1453 * Resets a virtual CPU.
1454 *
1455 * Used by PDMR3Reset and CPU hot plugging.
1456 *
1457 * @param pVCpu Pointer to the VMCPU.
1458 */
1459VMMR3_INT_DECL(void) PDMR3ResetCpu(PVMCPU pVCpu)
1460{
1461 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_APIC);
1462 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_PIC);
1463 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_NMI);
1464 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_SMI);
1465}
1466
1467
1468/**
1469 * This function will notify all the devices and their attached drivers about
1470 * the VM now being reset.
1471 *
1472 * @param pVM Pointer to the VM.
1473 */
1474VMMR3_INT_DECL(void) PDMR3Reset(PVM pVM)
1475{
1476 LogFlow(("PDMR3Reset:\n"));
1477
1478 /*
1479 * Clear all the reset flags.
1480 */
1481 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1482 {
1483 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_RESET;
1484 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1485 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1486 pDrvIns->Internal.s.fVMReset = false;
1487 }
1488#ifdef VBOX_WITH_USB
1489 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1490 {
1491 pUsbIns->Internal.s.fVMReset = false;
1492 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1493 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1494 pDrvIns->Internal.s.fVMReset = false;
1495 }
1496#endif
1497
1498 /*
1499 * The outer loop repeats until there are no more async requests.
1500 */
1501 PDMNOTIFYASYNCSTATS Async;
1502 pdmR3NotifyAsyncInit(&Async, "PDMR3Reset");
1503 for (;;)
1504 {
1505 pdmR3NotifyAsyncBeginLoop(&Async);
1506
1507 /*
1508 * Iterate thru the device instances and USB device instances,
1509 * processing the drivers associated with those.
1510 */
1511 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1512 {
1513 unsigned const cAsyncStart = Async.cAsync;
1514
1515 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_RESET_NOTIFICATION)
1516 pdmR3ResetDev(pDevIns, &Async);
1517
1518 if (Async.cAsync == cAsyncStart)
1519 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1520 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1521 if (!pdmR3ResetDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
1522 break;
1523
1524 if ( Async.cAsync == cAsyncStart
1525 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_RESET_NOTIFICATION))
1526 pdmR3ResetDev(pDevIns, &Async);
1527 }
1528
1529#ifdef VBOX_WITH_USB
1530 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1531 {
1532 unsigned const cAsyncStart = Async.cAsync;
1533
1534 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1535 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1536 if (!pdmR3ResetDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
1537 break;
1538
1539 if (Async.cAsync == cAsyncStart)
1540 pdmR3ResetUsb(pUsbIns, &Async);
1541 }
1542#endif
1543 if (!Async.cAsync)
1544 break;
1545 pdmR3NotifyAsyncLog(&Async);
1546 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
1547 }
1548
1549 /*
1550 * Clear all pending interrupts and DMA operations.
1551 */
1552 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
1553 PDMR3ResetCpu(&pVM->aCpus[idCpu]);
1554 VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
1555
1556 LogFlow(("PDMR3Reset: returns void\n"));
1557}
1558
1559
1560/**
1561 * This function will tell all the devices to setup up their memory structures
1562 * after VM construction and after VM reset.
1563 *
1564 * @param pVM Pointer to the VM.
1565 * @param fAtReset Indicates the context, after reset if @c true or after
1566 * construction if @c false.
1567 */
1568VMMR3_INT_DECL(void) PDMR3MemSetup(PVM pVM, bool fAtReset)
1569{
1570 LogFlow(("PDMR3MemSetup: fAtReset=%RTbool\n", fAtReset));
1571 PDMDEVMEMSETUPCTX const enmCtx = fAtReset ? PDMDEVMEMSETUPCTX_AFTER_RESET : PDMDEVMEMSETUPCTX_AFTER_CONSTRUCTION;
1572
1573 /*
1574 * Iterate thru the device instances and work the callback.
1575 */
1576 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1577 if (pDevIns->pReg->pfnMemSetup)
1578 {
1579 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1580 pDevIns->pReg->pfnMemSetup(pDevIns, enmCtx);
1581 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1582 }
1583
1584 LogFlow(("PDMR3MemSetup: returns void\n"));
1585}
1586
1587
1588/**
1589 * Worker for PDMR3Suspend that deals with one driver.
1590 *
1591 * @param pDrvIns The driver instance.
1592 * @param pAsync The structure for recording asynchronous
1593 * notification tasks.
1594 * @param pszDevName The parent device name.
1595 * @param iDevInstance The parent device instance number.
1596 * @param iLun The parent LUN number.
1597 */
1598DECLINLINE(bool) pdmR3SuspendDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
1599 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1600{
1601 if (!pDrvIns->Internal.s.fVMSuspended)
1602 {
1603 pDrvIns->Internal.s.fVMSuspended = true;
1604 if (pDrvIns->pReg->pfnSuspend)
1605 {
1606 uint64_t cNsElapsed = RTTimeNanoTS();
1607
1608 if (!pDrvIns->Internal.s.pfnAsyncNotify)
1609 {
1610 LogFlow(("PDMR3Suspend: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1611 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1612 pDrvIns->pReg->pfnSuspend(pDrvIns);
1613 if (pDrvIns->Internal.s.pfnAsyncNotify)
1614 LogFlow(("PDMR3Suspend: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1615 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1616 }
1617 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
1618 {
1619 LogFlow(("PDMR3Suspend: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1620 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1621 pDrvIns->Internal.s.pfnAsyncNotify = NULL;
1622 }
1623
1624 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1625 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1626 LogRel(("PDMR3Suspend: Driver '%s'/%d on LUN#%d of device '%s'/%d took %'llu ns to suspend\n",
1627 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, cNsElapsed));
1628
1629 if (pDrvIns->Internal.s.pfnAsyncNotify)
1630 {
1631 pDrvIns->Internal.s.fVMSuspended = false;
1632 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance, pszDevName, iDevInstance, iLun);
1633 return false;
1634 }
1635 }
1636 }
1637 return true;
1638}
1639
1640
1641/**
1642 * Worker for PDMR3Suspend that deals with one USB device instance.
1643 *
1644 * @param pUsbIns The USB device instance.
1645 * @param pAsync The structure for recording asynchronous
1646 * notification tasks.
1647 */
1648DECLINLINE(void) pdmR3SuspendUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
1649{
1650 if (!pUsbIns->Internal.s.fVMSuspended)
1651 {
1652 pUsbIns->Internal.s.fVMSuspended = true;
1653 if (pUsbIns->pReg->pfnVMSuspend)
1654 {
1655 uint64_t cNsElapsed = RTTimeNanoTS();
1656
1657 if (!pUsbIns->Internal.s.pfnAsyncNotify)
1658 {
1659 LogFlow(("PDMR3Suspend: Notifying - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1660 pUsbIns->pReg->pfnVMSuspend(pUsbIns);
1661 if (pUsbIns->Internal.s.pfnAsyncNotify)
1662 LogFlow(("PDMR3Suspend: Async notification started - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1663 }
1664 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
1665 {
1666 LogFlow(("PDMR3Suspend: Async notification completed - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1667 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
1668 }
1669 if (pUsbIns->Internal.s.pfnAsyncNotify)
1670 {
1671 pUsbIns->Internal.s.fVMSuspended = false;
1672 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
1673 }
1674
1675 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1676 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1677 LogRel(("PDMR3Suspend: USB device '%s'/%d took %'llu ns to suspend\n",
1678 pUsbIns->pReg->szName, pUsbIns->iInstance, cNsElapsed));
1679 }
1680 }
1681}
1682
1683
1684/**
1685 * Worker for PDMR3Suspend that deals with one device instance.
1686 *
1687 * @param pDevIns The device instance.
1688 * @param pAsync The structure for recording asynchronous
1689 * notification tasks.
1690 */
1691DECLINLINE(void) pdmR3SuspendDev(PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
1692{
1693 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED))
1694 {
1695 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_SUSPENDED;
1696 if (pDevIns->pReg->pfnSuspend)
1697 {
1698 uint64_t cNsElapsed = RTTimeNanoTS();
1699 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1700
1701 if (!pDevIns->Internal.s.pfnAsyncNotify)
1702 {
1703 LogFlow(("PDMR3Suspend: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1704 pDevIns->pReg->pfnSuspend(pDevIns);
1705 if (pDevIns->Internal.s.pfnAsyncNotify)
1706 LogFlow(("PDMR3Suspend: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1707 }
1708 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
1709 {
1710 LogFlow(("PDMR3Suspend: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1711 pDevIns->Internal.s.pfnAsyncNotify = NULL;
1712 }
1713 if (pDevIns->Internal.s.pfnAsyncNotify)
1714 {
1715 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
1716 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
1717 }
1718
1719 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1720 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1721 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1722 LogRel(("PDMR3Suspend: device '%s'/%d took %'llu ns to suspend\n",
1723 pDevIns->pReg->szName, pDevIns->iInstance, cNsElapsed));
1724 }
1725 }
1726}
1727
1728
1729/**
1730 * This function will notify all the devices and their attached drivers about
1731 * the VM now being suspended.
1732 *
1733 * @param pVM Pointer to the VM.
1734 * @thread EMT(0)
1735 */
1736VMMR3_INT_DECL(void) PDMR3Suspend(PVM pVM)
1737{
1738 LogFlow(("PDMR3Suspend:\n"));
1739 VM_ASSERT_EMT0(pVM);
1740 uint64_t cNsElapsed = RTTimeNanoTS();
1741
1742 /*
1743 * The outer loop repeats until there are no more async requests.
1744 *
1745 * Note! We depend on the suspended indicators to be in the desired state
1746 * and we do not reset them before starting because this allows
1747 * PDMR3PowerOn and PDMR3Resume to use PDMR3Suspend for cleaning up
1748 * on failure.
1749 */
1750 PDMNOTIFYASYNCSTATS Async;
1751 pdmR3NotifyAsyncInit(&Async, "PDMR3Suspend");
1752 for (;;)
1753 {
1754 pdmR3NotifyAsyncBeginLoop(&Async);
1755
1756 /*
1757 * Iterate thru the device instances and USB device instances,
1758 * processing the drivers associated with those.
1759 *
1760 * The attached drivers are normally processed first. Some devices
1761 * (like DevAHCI) though needs to be notified before the drivers so
1762 * that it doesn't kick off any new requests after the drivers stopped
1763 * taking any. (DrvVD changes to read-only in this particular case.)
1764 */
1765 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1766 {
1767 unsigned const cAsyncStart = Async.cAsync;
1768
1769 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION)
1770 pdmR3SuspendDev(pDevIns, &Async);
1771
1772 if (Async.cAsync == cAsyncStart)
1773 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1774 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1775 if (!pdmR3SuspendDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
1776 break;
1777
1778 if ( Async.cAsync == cAsyncStart
1779 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION))
1780 pdmR3SuspendDev(pDevIns, &Async);
1781 }
1782
1783#ifdef VBOX_WITH_USB
1784 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1785 {
1786 unsigned const cAsyncStart = Async.cAsync;
1787
1788 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1789 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1790 if (!pdmR3SuspendDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
1791 break;
1792
1793 if (Async.cAsync == cAsyncStart)
1794 pdmR3SuspendUsb(pUsbIns, &Async);
1795 }
1796#endif
1797 if (!Async.cAsync)
1798 break;
1799 pdmR3NotifyAsyncLog(&Async);
1800 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
1801 }
1802
1803 /*
1804 * Suspend all threads.
1805 */
1806 pdmR3ThreadSuspendAll(pVM);
1807
1808 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1809 LogRel(("PDMR3Suspend: %'llu ns run time\n", cNsElapsed));
1810}
1811
1812
1813/**
1814 * Worker for PDMR3Resume that deals with one driver.
1815 *
1816 * @param pDrvIns The driver instance.
1817 * @param pszDevName The parent device name.
1818 * @param iDevInstance The parent device instance number.
1819 * @param iLun The parent LUN number.
1820 */
1821DECLINLINE(int) pdmR3ResumeDrv(PPDMDRVINS pDrvIns, const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1822{
1823 Assert(pDrvIns->Internal.s.fVMSuspended);
1824 if (pDrvIns->pReg->pfnResume)
1825 {
1826 LogFlow(("PDMR3Resume: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1827 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1828 int rc = VINF_SUCCESS; pDrvIns->pReg->pfnResume(pDrvIns);
1829 if (RT_FAILURE(rc))
1830 {
1831 LogRel(("PDMR3Resume: driver '%s'/%d on LUN#%d of device '%s'/%d -> %Rrc\n",
1832 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, rc));
1833 return rc;
1834 }
1835 }
1836 pDrvIns->Internal.s.fVMSuspended = false;
1837 return VINF_SUCCESS;
1838}
1839
1840
1841/**
1842 * Worker for PDMR3Resume that deals with one USB device instance.
1843 *
1844 * @returns VBox status code.
1845 * @param pUsbIns The USB device instance.
1846 */
1847DECLINLINE(int) pdmR3ResumeUsb(PPDMUSBINS pUsbIns)
1848{
1849 Assert(pUsbIns->Internal.s.fVMSuspended);
1850 if (pUsbIns->pReg->pfnVMResume)
1851 {
1852 LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1853 int rc = VINF_SUCCESS; pUsbIns->pReg->pfnVMResume(pUsbIns);
1854 if (RT_FAILURE(rc))
1855 {
1856 LogRel(("PDMR3Resume: device '%s'/%d -> %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1857 return rc;
1858 }
1859 }
1860 pUsbIns->Internal.s.fVMSuspended = false;
1861 return VINF_SUCCESS;
1862}
1863
1864
1865/**
1866 * Worker for PDMR3Resume that deals with one device instance.
1867 *
1868 * @returns VBox status code.
1869 * @param pDevIns The device instance.
1870 */
1871DECLINLINE(int) pdmR3ResumeDev(PPDMDEVINS pDevIns)
1872{
1873 Assert(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED);
1874 if (pDevIns->pReg->pfnResume)
1875 {
1876 LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1877 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1878 int rc = VINF_SUCCESS; pDevIns->pReg->pfnResume(pDevIns);
1879 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1880 if (RT_FAILURE(rc))
1881 {
1882 LogRel(("PDMR3Resume: device '%s'/%d -> %Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
1883 return rc;
1884 }
1885 }
1886 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
1887 return VINF_SUCCESS;
1888}
1889
1890
1891/**
1892 * This function will notify all the devices and their
1893 * attached drivers about the VM now being resumed.
1894 *
1895 * @param pVM Pointer to the VM.
1896 */
1897VMMR3_INT_DECL(void) PDMR3Resume(PVM pVM)
1898{
1899 LogFlow(("PDMR3Resume:\n"));
1900
1901 /*
1902 * Iterate thru the device instances and USB device instances,
1903 * processing the drivers associated with those.
1904 */
1905 int rc = VINF_SUCCESS;
1906 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns && RT_SUCCESS(rc); pDevIns = pDevIns->Internal.s.pNextR3)
1907 {
1908 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1909 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1910 rc = pdmR3ResumeDrv(pDrvIns, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun);
1911 if (RT_SUCCESS(rc))
1912 rc = pdmR3ResumeDev(pDevIns);
1913 }
1914
1915#ifdef VBOX_WITH_USB
1916 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns && RT_SUCCESS(rc); pUsbIns = pUsbIns->Internal.s.pNext)
1917 {
1918 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1919 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1920 rc = pdmR3ResumeDrv(pDrvIns, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun);
1921 if (RT_SUCCESS(rc))
1922 rc = pdmR3ResumeUsb(pUsbIns);
1923 }
1924#endif
1925
1926 /*
1927 * Resume all threads.
1928 */
1929 if (RT_SUCCESS(rc))
1930 pdmR3ThreadResumeAll(pVM);
1931
1932 /*
1933 * Resume the block cache.
1934 */
1935 if (RT_SUCCESS(rc))
1936 pdmR3BlkCacheResume(pVM);
1937
1938 /*
1939 * On failure, clean up via PDMR3Suspend.
1940 */
1941 if (RT_FAILURE(rc))
1942 PDMR3Suspend(pVM);
1943
1944 LogFlow(("PDMR3Resume: returns %Rrc\n", rc));
1945 return /*rc*/;
1946}
1947
1948
1949/**
1950 * Worker for PDMR3PowerOff that deals with one driver.
1951 *
1952 * @param pDrvIns The driver instance.
1953 * @param pAsync The structure for recording asynchronous
1954 * notification tasks.
1955 * @param pszDevName The parent device name.
1956 * @param iDevInstance The parent device instance number.
1957 * @param iLun The parent LUN number.
1958 */
1959DECLINLINE(bool) pdmR3PowerOffDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
1960 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1961{
1962 if (!pDrvIns->Internal.s.fVMSuspended)
1963 {
1964 pDrvIns->Internal.s.fVMSuspended = true;
1965 if (pDrvIns->pReg->pfnPowerOff)
1966 {
1967 uint64_t cNsElapsed = RTTimeNanoTS();
1968
1969 if (!pDrvIns->Internal.s.pfnAsyncNotify)
1970 {
1971 LogFlow(("PDMR3PowerOff: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1972 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1973 pDrvIns->pReg->pfnPowerOff(pDrvIns);
1974 if (pDrvIns->Internal.s.pfnAsyncNotify)
1975 LogFlow(("PDMR3PowerOff: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1976 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1977 }
1978 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
1979 {
1980 LogFlow(("PDMR3PowerOff: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1981 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1982 pDrvIns->Internal.s.pfnAsyncNotify = NULL;
1983 }
1984
1985 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1986 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
1987 LogRel(("PDMR3PowerOff: Driver '%s'/%d on LUN#%d of device '%s'/%d took %'llu ns to power off\n",
1988 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, cNsElapsed));
1989
1990 if (pDrvIns->Internal.s.pfnAsyncNotify)
1991 {
1992 pDrvIns->Internal.s.fVMSuspended = false;
1993 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance,
1994 pszDevName, iDevInstance, iLun);
1995 return false;
1996 }
1997 }
1998 }
1999 return true;
2000}
2001
2002
2003/**
2004 * Worker for PDMR3PowerOff that deals with one USB device instance.
2005 *
2006 * @param pUsbIns The USB device instance.
2007 * @param pAsync The structure for recording asynchronous
2008 * notification tasks.
2009 */
2010DECLINLINE(void) pdmR3PowerOffUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
2011{
2012 if (!pUsbIns->Internal.s.fVMSuspended)
2013 {
2014 pUsbIns->Internal.s.fVMSuspended = true;
2015 if (pUsbIns->pReg->pfnVMPowerOff)
2016 {
2017 uint64_t cNsElapsed = RTTimeNanoTS();
2018
2019 if (!pUsbIns->Internal.s.pfnAsyncNotify)
2020 {
2021 LogFlow(("PDMR3PowerOff: Notifying - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
2022 pUsbIns->pReg->pfnVMPowerOff(pUsbIns);
2023 if (pUsbIns->Internal.s.pfnAsyncNotify)
2024 LogFlow(("PDMR3PowerOff: Async notification started - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
2025 }
2026 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
2027 {
2028 LogFlow(("PDMR3PowerOff: Async notification completed - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
2029 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
2030 }
2031 if (pUsbIns->Internal.s.pfnAsyncNotify)
2032 {
2033 pUsbIns->Internal.s.fVMSuspended = false;
2034 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
2035 }
2036
2037 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2038 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
2039 LogRel(("PDMR3PowerOff: USB device '%s'/%d took %'llu ns to power off\n",
2040 pUsbIns->pReg->szName, pUsbIns->iInstance, cNsElapsed));
2041
2042 }
2043 }
2044}
2045
2046
2047/**
2048 * Worker for PDMR3PowerOff that deals with one device instance.
2049 *
2050 * @param pDevIns The device instance.
2051 * @param pAsync The structure for recording asynchronous
2052 * notification tasks.
2053 */
2054DECLINLINE(void) pdmR3PowerOffDev(PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
2055{
2056 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED))
2057 {
2058 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_SUSPENDED;
2059 if (pDevIns->pReg->pfnPowerOff)
2060 {
2061 uint64_t cNsElapsed = RTTimeNanoTS();
2062 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
2063
2064 if (!pDevIns->Internal.s.pfnAsyncNotify)
2065 {
2066 LogFlow(("PDMR3PowerOff: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
2067 pDevIns->pReg->pfnPowerOff(pDevIns);
2068 if (pDevIns->Internal.s.pfnAsyncNotify)
2069 LogFlow(("PDMR3PowerOff: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
2070 }
2071 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
2072 {
2073 LogFlow(("PDMR3PowerOff: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
2074 pDevIns->Internal.s.pfnAsyncNotify = NULL;
2075 }
2076 if (pDevIns->Internal.s.pfnAsyncNotify)
2077 {
2078 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
2079 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
2080 }
2081
2082 PDMCritSectLeave(pDevIns->pCritSectRoR3);
2083 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2084 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
2085 LogFlow(("PDMR3PowerOff: Device '%s'/%d took %'llu ns to power off\n",
2086 pDevIns->pReg->szName, pDevIns->iInstance, cNsElapsed));
2087 }
2088 }
2089}
2090
2091
2092/**
2093 * This function will notify all the devices and their
2094 * attached drivers about the VM being powered off.
2095 *
2096 * @param pVM Pointer to the VM.
2097 */
2098VMMR3DECL(void) PDMR3PowerOff(PVM pVM)
2099{
2100 LogFlow(("PDMR3PowerOff:\n"));
2101 uint64_t cNsElapsed = RTTimeNanoTS();
2102
2103 /*
2104 * Clear the suspended flags on all devices and drivers first because they
2105 * might have been set during a suspend but the power off callbacks should
2106 * be called in any case.
2107 */
2108 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2109 {
2110 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
2111
2112 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2113 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2114 pDrvIns->Internal.s.fVMSuspended = false;
2115 }
2116
2117#ifdef VBOX_WITH_USB
2118 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2119 {
2120 pUsbIns->Internal.s.fVMSuspended = false;
2121
2122 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2123 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2124 pDrvIns->Internal.s.fVMSuspended = false;
2125 }
2126#endif
2127
2128 /*
2129 * The outer loop repeats until there are no more async requests.
2130 */
2131 PDMNOTIFYASYNCSTATS Async;
2132 pdmR3NotifyAsyncInit(&Async, "PDMR3PowerOff");
2133 for (;;)
2134 {
2135 pdmR3NotifyAsyncBeginLoop(&Async);
2136
2137 /*
2138 * Iterate thru the device instances and USB device instances,
2139 * processing the drivers associated with those.
2140 *
2141 * The attached drivers are normally processed first. Some devices
2142 * (like DevAHCI) though needs to be notified before the drivers so
2143 * that it doesn't kick off any new requests after the drivers stopped
2144 * taking any. (DrvVD changes to read-only in this particular case.)
2145 */
2146 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2147 {
2148 unsigned const cAsyncStart = Async.cAsync;
2149
2150 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION)
2151 pdmR3PowerOffDev(pDevIns, &Async);
2152
2153 if (Async.cAsync == cAsyncStart)
2154 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2155 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2156 if (!pdmR3PowerOffDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
2157 break;
2158
2159 if ( Async.cAsync == cAsyncStart
2160 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION))
2161 pdmR3PowerOffDev(pDevIns, &Async);
2162 }
2163
2164#ifdef VBOX_WITH_USB
2165 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2166 {
2167 unsigned const cAsyncStart = Async.cAsync;
2168
2169 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2170 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2171 if (!pdmR3PowerOffDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
2172 break;
2173
2174 if (Async.cAsync == cAsyncStart)
2175 pdmR3PowerOffUsb(pUsbIns, &Async);
2176 }
2177#endif
2178 if (!Async.cAsync)
2179 break;
2180 pdmR3NotifyAsyncLog(&Async);
2181 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
2182 }
2183
2184 /*
2185 * Suspend all threads.
2186 */
2187 pdmR3ThreadSuspendAll(pVM);
2188
2189 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2190 LogRel(("PDMR3PowerOff: %'llu ns run time\n", cNsElapsed));
2191}
2192
2193
2194/**
2195 * Queries the base interface of a device instance.
2196 *
2197 * The caller can use this to query other interfaces the device implements
2198 * and use them to talk to the device.
2199 *
2200 * @returns VBox status code.
2201 * @param pUVM The user mode VM handle.
2202 * @param pszDevice Device name.
2203 * @param iInstance Device instance.
2204 * @param ppBase Where to store the pointer to the base device interface on success.
2205 * @remark We're not doing any locking ATM, so don't try call this at times when the
2206 * device chain is known to be updated.
2207 */
2208VMMR3DECL(int) PDMR3QueryDevice(PUVM pUVM, const char *pszDevice, unsigned iInstance, PPDMIBASE *ppBase)
2209{
2210 LogFlow(("PDMR3DeviceQuery: pszDevice=%p:{%s} iInstance=%u ppBase=%p\n", pszDevice, pszDevice, iInstance, ppBase));
2211 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2212 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2213
2214 /*
2215 * Iterate registered devices looking for the device.
2216 */
2217 size_t cchDevice = strlen(pszDevice);
2218 for (PPDMDEV pDev = pUVM->pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
2219 {
2220 if ( pDev->cchName == cchDevice
2221 && !memcmp(pDev->pReg->szName, pszDevice, cchDevice))
2222 {
2223 /*
2224 * Iterate device instances.
2225 */
2226 for (PPDMDEVINS pDevIns = pDev->pInstances; pDevIns; pDevIns = pDevIns->Internal.s.pPerDeviceNextR3)
2227 {
2228 if (pDevIns->iInstance == iInstance)
2229 {
2230 if (pDevIns->IBase.pfnQueryInterface)
2231 {
2232 *ppBase = &pDevIns->IBase;
2233 LogFlow(("PDMR3DeviceQuery: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
2234 return VINF_SUCCESS;
2235 }
2236
2237 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NO_IBASE\n"));
2238 return VERR_PDM_DEVICE_INSTANCE_NO_IBASE;
2239 }
2240 }
2241
2242 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NOT_FOUND\n"));
2243 return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND;
2244 }
2245 }
2246
2247 LogFlow(("PDMR3QueryDevice: returns VERR_PDM_DEVICE_NOT_FOUND\n"));
2248 return VERR_PDM_DEVICE_NOT_FOUND;
2249}
2250
2251
2252/**
2253 * Queries the base interface of a device LUN.
2254 *
2255 * This differs from PDMR3QueryLun by that it returns the interface on the
2256 * device and not the top level driver.
2257 *
2258 * @returns VBox status code.
2259 * @param pUVM The user mode VM handle.
2260 * @param pszDevice Device name.
2261 * @param iInstance Device instance.
2262 * @param iLun The Logical Unit to obtain the interface of.
2263 * @param ppBase Where to store the base interface pointer.
2264 * @remark We're not doing any locking ATM, so don't try call this at times when the
2265 * device chain is known to be updated.
2266 */
2267VMMR3DECL(int) PDMR3QueryDeviceLun(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
2268{
2269 LogFlow(("PDMR3QueryDeviceLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
2270 pszDevice, pszDevice, iInstance, iLun, ppBase));
2271 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2272 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2273
2274 /*
2275 * Find the LUN.
2276 */
2277 PPDMLUN pLun;
2278 int rc = pdmR3DevFindLun(pUVM->pVM, pszDevice, iInstance, iLun, &pLun);
2279 if (RT_SUCCESS(rc))
2280 {
2281 *ppBase = pLun->pBase;
2282 LogFlow(("PDMR3QueryDeviceLun: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
2283 return VINF_SUCCESS;
2284 }
2285 LogFlow(("PDMR3QueryDeviceLun: returns %Rrc\n", rc));
2286 return rc;
2287}
2288
2289
2290/**
2291 * Query the interface of the top level driver on a LUN.
2292 *
2293 * @returns VBox status code.
2294 * @param pUVM The user mode VM handle.
2295 * @param pszDevice Device name.
2296 * @param iInstance Device instance.
2297 * @param iLun The Logical Unit to obtain the interface of.
2298 * @param ppBase Where to store the base interface pointer.
2299 * @remark We're not doing any locking ATM, so don't try call this at times when the
2300 * device chain is known to be updated.
2301 */
2302VMMR3DECL(int) PDMR3QueryLun(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
2303{
2304 LogFlow(("PDMR3QueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
2305 pszDevice, pszDevice, iInstance, iLun, ppBase));
2306 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2307 PVM pVM = pUVM->pVM;
2308 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
2309
2310 /*
2311 * Find the LUN.
2312 */
2313 PPDMLUN pLun;
2314 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
2315 if (RT_SUCCESS(rc))
2316 {
2317 if (pLun->pTop)
2318 {
2319 *ppBase = &pLun->pTop->IBase;
2320 LogFlow(("PDMR3QueryLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
2321 return VINF_SUCCESS;
2322 }
2323 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
2324 }
2325 LogFlow(("PDMR3QueryLun: returns %Rrc\n", rc));
2326 return rc;
2327}
2328
2329
2330/**
2331 * Query the interface of a named driver on a LUN.
2332 *
2333 * If the driver appears more than once in the driver chain, the first instance
2334 * is returned.
2335 *
2336 * @returns VBox status code.
2337 * @param pUVM The user mode VM handle.
2338 * @param pszDevice Device name.
2339 * @param iInstance Device instance.
2340 * @param iLun The Logical Unit to obtain the interface of.
2341 * @param pszDriver The driver name.
2342 * @param ppBase Where to store the base interface pointer.
2343 *
2344 * @remark We're not doing any locking ATM, so don't try call this at times when the
2345 * device chain is known to be updated.
2346 */
2347VMMR3DECL(int) PDMR3QueryDriverOnLun(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, const char *pszDriver, PPPDMIBASE ppBase)
2348{
2349 LogFlow(("PDMR3QueryDriverOnLun: pszDevice=%p:{%s} iInstance=%u iLun=%u pszDriver=%p:{%s} ppBase=%p\n",
2350 pszDevice, pszDevice, iInstance, iLun, pszDriver, pszDriver, ppBase));
2351 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2352 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2353
2354 /*
2355 * Find the LUN.
2356 */
2357 PPDMLUN pLun;
2358 int rc = pdmR3DevFindLun(pUVM->pVM, pszDevice, iInstance, iLun, &pLun);
2359 if (RT_SUCCESS(rc))
2360 {
2361 if (pLun->pTop)
2362 {
2363 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2364 if (!strcmp(pDrvIns->pReg->szName, pszDriver))
2365 {
2366 *ppBase = &pDrvIns->IBase;
2367 LogFlow(("PDMR3QueryDriverOnLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
2368 return VINF_SUCCESS;
2369
2370 }
2371 rc = VERR_PDM_DRIVER_NOT_FOUND;
2372 }
2373 else
2374 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
2375 }
2376 LogFlow(("PDMR3QueryDriverOnLun: returns %Rrc\n", rc));
2377 return rc;
2378}
2379
2380/**
2381 * Executes pending DMA transfers.
2382 * Forced Action handler.
2383 *
2384 * @param pVM Pointer to the VM.
2385 */
2386VMMR3DECL(void) PDMR3DmaRun(PVM pVM)
2387{
2388 /* Note! Not really SMP safe; restrict it to VCPU 0. */
2389 if (VMMGetCpuId(pVM) != 0)
2390 return;
2391
2392 if (VM_FF_TEST_AND_CLEAR(pVM, VM_FF_PDM_DMA))
2393 {
2394 if (pVM->pdm.s.pDmac)
2395 {
2396 bool fMore = pVM->pdm.s.pDmac->Reg.pfnRun(pVM->pdm.s.pDmac->pDevIns);
2397 if (fMore)
2398 VM_FF_SET(pVM, VM_FF_PDM_DMA);
2399 }
2400 }
2401}
2402
2403
2404/**
2405 * Service a VMMCALLRING3_PDM_LOCK call.
2406 *
2407 * @returns VBox status code.
2408 * @param pVM Pointer to the VM.
2409 */
2410VMMR3_INT_DECL(int) PDMR3LockCall(PVM pVM)
2411{
2412 return PDMR3CritSectEnterEx(&pVM->pdm.s.CritSect, true /* fHostCall */);
2413}
2414
2415
2416/**
2417 * Registers the VMM device heap
2418 *
2419 * @returns VBox status code.
2420 * @param pVM Pointer to the VM.
2421 * @param GCPhys The physical address.
2422 * @param pvHeap Ring-3 pointer.
2423 * @param cbSize Size of the heap.
2424 */
2425VMMR3_INT_DECL(int) PDMR3VmmDevHeapRegister(PVM pVM, RTGCPHYS GCPhys, RTR3PTR pvHeap, unsigned cbSize)
2426{
2427 Assert(pVM->pdm.s.pvVMMDevHeap == NULL);
2428
2429 Log(("PDMR3VmmDevHeapRegister %RGp %RHv %x\n", GCPhys, pvHeap, cbSize));
2430 pVM->pdm.s.pvVMMDevHeap = pvHeap;
2431 pVM->pdm.s.GCPhysVMMDevHeap = GCPhys;
2432 pVM->pdm.s.cbVMMDevHeap = cbSize;
2433 pVM->pdm.s.cbVMMDevHeapLeft = cbSize;
2434 return VINF_SUCCESS;
2435}
2436
2437
2438/**
2439 * Unregisters the VMM device heap
2440 *
2441 * @returns VBox status code.
2442 * @param pVM Pointer to the VM.
2443 * @param GCPhys The physical address.
2444 */
2445VMMR3_INT_DECL(int) PDMR3VmmDevHeapUnregister(PVM pVM, RTGCPHYS GCPhys)
2446{
2447 Assert(pVM->pdm.s.GCPhysVMMDevHeap == GCPhys);
2448
2449 Log(("PDMR3VmmDevHeapUnregister %RGp\n", GCPhys));
2450 pVM->pdm.s.pvVMMDevHeap = NULL;
2451 pVM->pdm.s.GCPhysVMMDevHeap = NIL_RTGCPHYS;
2452 pVM->pdm.s.cbVMMDevHeap = 0;
2453 pVM->pdm.s.cbVMMDevHeapLeft = 0;
2454 return VINF_SUCCESS;
2455}
2456
2457
2458/**
2459 * Allocates memory from the VMM device heap
2460 *
2461 * @returns VBox status code.
2462 * @param pVM Pointer to the VM.
2463 * @param cbSize Allocation size.
2464 * @param pv Ring-3 pointer. (out)
2465 */
2466VMMR3_INT_DECL(int) PDMR3VmmDevHeapAlloc(PVM pVM, size_t cbSize, RTR3PTR *ppv)
2467{
2468#ifdef DEBUG_bird
2469 if (!cbSize || cbSize > pVM->pdm.s.cbVMMDevHeapLeft)
2470 return VERR_NO_MEMORY;
2471#else
2472 AssertReturn(cbSize && cbSize <= pVM->pdm.s.cbVMMDevHeapLeft, VERR_NO_MEMORY);
2473#endif
2474
2475 Log(("PDMR3VMMDevHeapAlloc: %#zx\n", cbSize));
2476
2477 /** @todo Not a real heap as there's currently only one user. */
2478 *ppv = pVM->pdm.s.pvVMMDevHeap;
2479 pVM->pdm.s.cbVMMDevHeapLeft = 0;
2480 return VINF_SUCCESS;
2481}
2482
2483
2484/**
2485 * Frees memory from the VMM device heap
2486 *
2487 * @returns VBox status code.
2488 * @param pVM Pointer to the VM.
2489 * @param pv Ring-3 pointer.
2490 */
2491VMMR3_INT_DECL(int) PDMR3VmmDevHeapFree(PVM pVM, RTR3PTR pv)
2492{
2493 Log(("PDMR3VmmDevHeapFree: %RHv\n", pv));
2494
2495 /** @todo not a real heap as there's currently only one user. */
2496 pVM->pdm.s.cbVMMDevHeapLeft = pVM->pdm.s.cbVMMDevHeap;
2497 return VINF_SUCCESS;
2498}
2499
2500
2501/**
2502 * Worker for DBGFR3TraceConfig that checks if the given tracing group name
2503 * matches a device or driver name and applies the tracing config change.
2504 *
2505 * @returns VINF_SUCCESS or VERR_NOT_FOUND.
2506 * @param pVM Pointer to the VM.
2507 * @param pszName The tracing config group name. This is NULL if
2508 * the operation applies to every device and
2509 * driver.
2510 * @param cchName The length to match.
2511 * @param fEnable Whether to enable or disable the corresponding
2512 * trace points.
2513 * @param fApply Whether to actually apply the changes or just do
2514 * existence checks.
2515 */
2516VMMR3_INT_DECL(int) PDMR3TracingConfig(PVM pVM, const char *pszName, size_t cchName, bool fEnable, bool fApply)
2517{
2518 /** @todo This code is potentially racing driver attaching and detaching. */
2519
2520 /*
2521 * Applies to all.
2522 */
2523 if (pszName == NULL)
2524 {
2525 AssertReturn(fApply, VINF_SUCCESS);
2526
2527 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2528 {
2529 pDevIns->fTracing = fEnable;
2530 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2531 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2532 pDrvIns->fTracing = fEnable;
2533 }
2534
2535#ifdef VBOX_WITH_USB
2536 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2537 {
2538 pUsbIns->fTracing = fEnable;
2539 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2540 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2541 pDrvIns->fTracing = fEnable;
2542
2543 }
2544#endif
2545 return VINF_SUCCESS;
2546 }
2547
2548 /*
2549 * Specific devices, USB devices or drivers.
2550 * Decode prefix to figure which of these it applies to.
2551 */
2552 if (cchName <= 3)
2553 return VERR_NOT_FOUND;
2554
2555 uint32_t cMatches = 0;
2556 if (!strncmp("dev", pszName, 3))
2557 {
2558 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2559 {
2560 const char *pszDevName = pDevIns->Internal.s.pDevR3->pReg->szName;
2561 size_t cchDevName = strlen(pszDevName);
2562 if ( ( cchDevName == cchName
2563 && RTStrNICmp(pszName, pszDevName, cchDevName))
2564 || ( cchDevName == cchName - 3
2565 && RTStrNICmp(pszName + 3, pszDevName, cchDevName)) )
2566 {
2567 cMatches++;
2568 if (fApply)
2569 pDevIns->fTracing = fEnable;
2570 }
2571 }
2572 }
2573 else if (!strncmp("usb", pszName, 3))
2574 {
2575 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2576 {
2577 const char *pszUsbName = pUsbIns->Internal.s.pUsbDev->pReg->szName;
2578 size_t cchUsbName = strlen(pszUsbName);
2579 if ( ( cchUsbName == cchName
2580 && RTStrNICmp(pszName, pszUsbName, cchUsbName))
2581 || ( cchUsbName == cchName - 3
2582 && RTStrNICmp(pszName + 3, pszUsbName, cchUsbName)) )
2583 {
2584 cMatches++;
2585 if (fApply)
2586 pUsbIns->fTracing = fEnable;
2587 }
2588 }
2589 }
2590 else if (!strncmp("drv", pszName, 3))
2591 {
2592 AssertReturn(fApply, VINF_SUCCESS);
2593
2594 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2595 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2596 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2597 {
2598 const char *pszDrvName = pDrvIns->Internal.s.pDrv->pReg->szName;
2599 size_t cchDrvName = strlen(pszDrvName);
2600 if ( ( cchDrvName == cchName
2601 && RTStrNICmp(pszName, pszDrvName, cchDrvName))
2602 || ( cchDrvName == cchName - 3
2603 && RTStrNICmp(pszName + 3, pszDrvName, cchDrvName)) )
2604 {
2605 cMatches++;
2606 if (fApply)
2607 pDrvIns->fTracing = fEnable;
2608 }
2609 }
2610
2611#ifdef VBOX_WITH_USB
2612 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2613 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2614 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2615 {
2616 const char *pszDrvName = pDrvIns->Internal.s.pDrv->pReg->szName;
2617 size_t cchDrvName = strlen(pszDrvName);
2618 if ( ( cchDrvName == cchName
2619 && RTStrNICmp(pszName, pszDrvName, cchDrvName))
2620 || ( cchDrvName == cchName - 3
2621 && RTStrNICmp(pszName + 3, pszDrvName, cchDrvName)) )
2622 {
2623 cMatches++;
2624 if (fApply)
2625 pDrvIns->fTracing = fEnable;
2626 }
2627 }
2628#endif
2629 }
2630 else
2631 return VERR_NOT_FOUND;
2632
2633 return cMatches > 0 ? VINF_SUCCESS : VERR_NOT_FOUND;
2634}
2635
2636
2637/**
2638 * Worker for DBGFR3TraceQueryConfig that checks whether all drivers, devices,
2639 * and USB device have the same tracing settings.
2640 *
2641 * @returns true / false.
2642 * @param pVM Pointer to the VM.
2643 * @param fEnabled The tracing setting to check for.
2644 */
2645VMMR3_INT_DECL(bool) PDMR3TracingAreAll(PVM pVM, bool fEnabled)
2646{
2647 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2648 {
2649 if (pDevIns->fTracing != (uint32_t)fEnabled)
2650 return false;
2651
2652 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2653 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2654 if (pDrvIns->fTracing != (uint32_t)fEnabled)
2655 return false;
2656 }
2657
2658#ifdef VBOX_WITH_USB
2659 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2660 {
2661 if (pUsbIns->fTracing != (uint32_t)fEnabled)
2662 return false;
2663
2664 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2665 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2666 if (pDrvIns->fTracing != (uint32_t)fEnabled)
2667 return false;
2668 }
2669#endif
2670
2671 return true;
2672}
2673
2674
2675/**
2676 * Worker for PDMR3TracingQueryConfig that adds a prefixed name to the output
2677 * string.
2678 *
2679 * @returns VINF_SUCCESS or VERR_BUFFER_OVERFLOW
2680 * @param ppszDst The pointer to the output buffer pointer.
2681 * @param pcbDst The pointer to the output buffer size.
2682 * @param fSpace Whether to add a space before the name.
2683 * @param pszPrefix The name prefix.
2684 * @param pszName The name.
2685 */
2686static int pdmR3TracingAdd(char **ppszDst, size_t *pcbDst, bool fSpace, const char *pszPrefix, const char *pszName)
2687{
2688 size_t const cchPrefix = strlen(pszPrefix);
2689 if (!RTStrNICmp(pszPrefix, pszName, cchPrefix))
2690 pszName += cchPrefix;
2691 size_t const cchName = strlen(pszName);
2692
2693 size_t const cchThis = cchName + cchPrefix + fSpace;
2694 if (cchThis >= *pcbDst)
2695 return VERR_BUFFER_OVERFLOW;
2696 if (fSpace)
2697 {
2698 **ppszDst = ' ';
2699 memcpy(*ppszDst + 1, pszPrefix, cchPrefix);
2700 memcpy(*ppszDst + 1 + cchPrefix, pszName, cchName + 1);
2701 }
2702 else
2703 {
2704 memcpy(*ppszDst, pszPrefix, cchPrefix);
2705 memcpy(*ppszDst + cchPrefix, pszName, cchName + 1);
2706 }
2707 *ppszDst += cchThis;
2708 *pcbDst -= cchThis;
2709 return VINF_SUCCESS;
2710}
2711
2712
2713/**
2714 * Worker for DBGFR3TraceQueryConfig use when not everything is either enabled
2715 * or disabled.
2716 *
2717 * @returns VINF_SUCCESS or VERR_BUFFER_OVERFLOW
2718 * @param pVM Pointer to the VM.
2719 * @param pszConfig Where to store the config spec.
2720 * @param cbConfig The size of the output buffer.
2721 */
2722VMMR3_INT_DECL(int) PDMR3TracingQueryConfig(PVM pVM, char *pszConfig, size_t cbConfig)
2723{
2724 int rc;
2725 char *pszDst = pszConfig;
2726 size_t cbDst = cbConfig;
2727
2728 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2729 {
2730 if (pDevIns->fTracing)
2731 {
2732 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "dev", pDevIns->Internal.s.pDevR3->pReg->szName);
2733 if (RT_FAILURE(rc))
2734 return rc;
2735 }
2736
2737 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2738 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2739 if (pDrvIns->fTracing)
2740 {
2741 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "drv", pDrvIns->Internal.s.pDrv->pReg->szName);
2742 if (RT_FAILURE(rc))
2743 return rc;
2744 }
2745 }
2746
2747#ifdef VBOX_WITH_USB
2748 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2749 {
2750 if (pUsbIns->fTracing)
2751 {
2752 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "usb", pUsbIns->Internal.s.pUsbDev->pReg->szName);
2753 if (RT_FAILURE(rc))
2754 return rc;
2755 }
2756
2757 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2758 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2759 if (pDrvIns->fTracing)
2760 {
2761 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "drv", pDrvIns->Internal.s.pDrv->pReg->szName);
2762 if (RT_FAILURE(rc))
2763 return rc;
2764 }
2765 }
2766#endif
2767
2768 return VINF_SUCCESS;
2769}
2770
2771
2772/**
2773 * Checks that a PDMDRVREG::szName, PDMDEVREG::szName or PDMUSBREG::szName
2774 * field contains only a limited set of ASCII characters.
2775 *
2776 * @returns true / false.
2777 * @param pszName The name to validate.
2778 */
2779bool pdmR3IsValidName(const char *pszName)
2780{
2781 char ch;
2782 while ( (ch = *pszName) != '\0'
2783 && ( RT_C_IS_ALNUM(ch)
2784 || ch == '-'
2785 || ch == ' ' /** @todo disallow this! */
2786 || ch == '_') )
2787 pszName++;
2788 return ch == '\0';
2789}
2790
2791
2792/**
2793 * Info handler for 'pdmtracingids'.
2794 *
2795 * @param pVM Pointer to the VM.
2796 * @param pHlp The output helpers.
2797 * @param pszArgs The optional user arguments.
2798 *
2799 * @remarks Can be called on most threads.
2800 */
2801static DECLCALLBACK(void) pdmR3InfoTracingIds(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2802{
2803 /*
2804 * Parse the argument (optional).
2805 */
2806 if ( pszArgs
2807 && *pszArgs
2808 && strcmp(pszArgs, "all")
2809 && strcmp(pszArgs, "devices")
2810 && strcmp(pszArgs, "drivers")
2811 && strcmp(pszArgs, "usb"))
2812 {
2813 pHlp->pfnPrintf(pHlp, "Unable to grok '%s'\n", pszArgs);
2814 return;
2815 }
2816 bool fAll = !pszArgs || !*pszArgs || !strcmp(pszArgs, "all");
2817 bool fDevices = fAll || !strcmp(pszArgs, "devices");
2818 bool fUsbDevs = fAll || !strcmp(pszArgs, "usb");
2819 bool fDrivers = fAll || !strcmp(pszArgs, "drivers");
2820
2821 /*
2822 * Produce the requested output.
2823 */
2824/** @todo lock PDM lists! */
2825 /* devices */
2826 if (fDevices)
2827 {
2828 pHlp->pfnPrintf(pHlp, "Device tracing IDs:\n");
2829 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2830 pHlp->pfnPrintf(pHlp, "%05u %s\n", pDevIns->idTracing, pDevIns->Internal.s.pDevR3->pReg->szName);
2831 }
2832
2833 /* USB devices */
2834 if (fUsbDevs)
2835 {
2836 pHlp->pfnPrintf(pHlp, "USB device tracing IDs:\n");
2837 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2838 pHlp->pfnPrintf(pHlp, "%05u %s\n", pUsbIns->idTracing, pUsbIns->Internal.s.pUsbDev->pReg->szName);
2839 }
2840
2841 /* Drivers */
2842 if (fDrivers)
2843 {
2844 pHlp->pfnPrintf(pHlp, "Driver tracing IDs:\n");
2845 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2846 {
2847 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2848 {
2849 uint32_t iLevel = 0;
2850 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown, iLevel++)
2851 pHlp->pfnPrintf(pHlp, "%05u %s (level %u, lun %u, dev %s)\n",
2852 pDrvIns->idTracing, pDrvIns->Internal.s.pDrv->pReg->szName,
2853 iLevel, pLun->iLun, pDevIns->Internal.s.pDevR3->pReg->szName);
2854 }
2855 }
2856
2857 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2858 {
2859 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2860 {
2861 uint32_t iLevel = 0;
2862 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown, iLevel++)
2863 pHlp->pfnPrintf(pHlp, "%05u %s (level %u, lun %u, dev %s)\n",
2864 pDrvIns->idTracing, pDrvIns->Internal.s.pDrv->pReg->szName,
2865 iLevel, pLun->iLun, pUsbIns->Internal.s.pUsbDev->pReg->szName);
2866 }
2867 }
2868 }
2869}
2870
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