VirtualBox

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

Last change on this file since 40652 was 40652, checked in by vboxsync, 13 years ago

NetShaper,E1000: Basic framework and partial implementation for network shaper

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