VirtualBox

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

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

PDM,PGM,DevEFI,DevACPI,DevPcBios: Added memory setup phase after construction and reset to solve PGM/PDM reset order issue (PDM first, then PGM, only that wasn't possible previously since PDM reset would plant stuff in guest RAM).

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