VirtualBox

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

Last change on this file since 58591 was 58398, checked in by vboxsync, 9 years ago

PDM.cpp: doxygen fix.

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