VirtualBox

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

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

DBGF,DBGC,++: PVM -> PUVM. Some refactoring and cleanup as well.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 100.6 KB
Line 
1/* $Id: PDM.cpp 44399 2013-01-27 21:12:53Z 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 * Worker for PDMR3Suspend that deals with one driver.
1514 *
1515 * @param pDrvIns The driver instance.
1516 * @param pAsync The structure for recording asynchronous
1517 * notification tasks.
1518 * @param pszDevName The parent device name.
1519 * @param iDevInstance The parent device instance number.
1520 * @param iLun The parent LUN number.
1521 */
1522DECLINLINE(bool) pdmR3SuspendDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
1523 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1524{
1525 if (!pDrvIns->Internal.s.fVMSuspended)
1526 {
1527 pDrvIns->Internal.s.fVMSuspended = true;
1528 if (pDrvIns->pReg->pfnSuspend)
1529 {
1530 uint64_t cNsElapsed = RTTimeNanoTS();
1531
1532 if (!pDrvIns->Internal.s.pfnAsyncNotify)
1533 {
1534 LogFlow(("PDMR3Suspend: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1535 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1536 pDrvIns->pReg->pfnSuspend(pDrvIns);
1537 if (pDrvIns->Internal.s.pfnAsyncNotify)
1538 LogFlow(("PDMR3Suspend: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1539 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1540 }
1541 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
1542 {
1543 LogFlow(("PDMR3Suspend: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1544 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1545 pDrvIns->Internal.s.pfnAsyncNotify = NULL;
1546 }
1547
1548 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1549 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1550 LogRel(("PDMR3Suspend: Driver '%s'/%d on LUN#%d of device '%s'/%d took %'llu ns to suspend\n",
1551 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, cNsElapsed));
1552
1553 if (pDrvIns->Internal.s.pfnAsyncNotify)
1554 {
1555 pDrvIns->Internal.s.fVMSuspended = false;
1556 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance, pszDevName, iDevInstance, iLun);
1557 return false;
1558 }
1559 }
1560 }
1561 return true;
1562}
1563
1564
1565/**
1566 * Worker for PDMR3Suspend that deals with one USB device instance.
1567 *
1568 * @param pUsbIns The USB device instance.
1569 * @param pAsync The structure for recording asynchronous
1570 * notification tasks.
1571 */
1572DECLINLINE(void) pdmR3SuspendUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
1573{
1574 if (!pUsbIns->Internal.s.fVMSuspended)
1575 {
1576 pUsbIns->Internal.s.fVMSuspended = true;
1577 if (pUsbIns->pReg->pfnVMSuspend)
1578 {
1579 uint64_t cNsElapsed = RTTimeNanoTS();
1580
1581 if (!pUsbIns->Internal.s.pfnAsyncNotify)
1582 {
1583 LogFlow(("PDMR3Suspend: Notifying - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1584 pUsbIns->pReg->pfnVMSuspend(pUsbIns);
1585 if (pUsbIns->Internal.s.pfnAsyncNotify)
1586 LogFlow(("PDMR3Suspend: Async notification started - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1587 }
1588 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
1589 {
1590 LogFlow(("PDMR3Suspend: Async notification completed - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1591 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
1592 }
1593 if (pUsbIns->Internal.s.pfnAsyncNotify)
1594 {
1595 pUsbIns->Internal.s.fVMSuspended = false;
1596 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
1597 }
1598
1599 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1600 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1601 LogRel(("PDMR3Suspend: USB device '%s'/%d took %'llu ns to suspend\n",
1602 pUsbIns->pReg->szName, pUsbIns->iInstance, cNsElapsed));
1603 }
1604 }
1605}
1606
1607
1608/**
1609 * Worker for PDMR3Suspend that deals with one device instance.
1610 *
1611 * @param pDevIns The device instance.
1612 * @param pAsync The structure for recording asynchronous
1613 * notification tasks.
1614 */
1615DECLINLINE(void) pdmR3SuspendDev(PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
1616{
1617 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED))
1618 {
1619 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_SUSPENDED;
1620 if (pDevIns->pReg->pfnSuspend)
1621 {
1622 uint64_t cNsElapsed = RTTimeNanoTS();
1623 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1624
1625 if (!pDevIns->Internal.s.pfnAsyncNotify)
1626 {
1627 LogFlow(("PDMR3Suspend: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1628 pDevIns->pReg->pfnSuspend(pDevIns);
1629 if (pDevIns->Internal.s.pfnAsyncNotify)
1630 LogFlow(("PDMR3Suspend: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1631 }
1632 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
1633 {
1634 LogFlow(("PDMR3Suspend: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1635 pDevIns->Internal.s.pfnAsyncNotify = NULL;
1636 }
1637 if (pDevIns->Internal.s.pfnAsyncNotify)
1638 {
1639 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
1640 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
1641 }
1642
1643 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1644 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1645 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1646 LogRel(("PDMR3Suspend: device '%s'/%d took %'llu ns to suspend\n",
1647 pDevIns->pReg->szName, pDevIns->iInstance, cNsElapsed));
1648 }
1649 }
1650}
1651
1652
1653/**
1654 * This function will notify all the devices and their attached drivers about
1655 * the VM now being suspended.
1656 *
1657 * @param pVM Pointer to the VM.
1658 * @thread EMT(0)
1659 */
1660VMMR3_INT_DECL(void) PDMR3Suspend(PVM pVM)
1661{
1662 LogFlow(("PDMR3Suspend:\n"));
1663 VM_ASSERT_EMT0(pVM);
1664 uint64_t cNsElapsed = RTTimeNanoTS();
1665
1666 /*
1667 * The outer loop repeats until there are no more async requests.
1668 *
1669 * Note! We depend on the suspended indicators to be in the desired state
1670 * and we do not reset them before starting because this allows
1671 * PDMR3PowerOn and PDMR3Resume to use PDMR3Suspend for cleaning up
1672 * on failure.
1673 */
1674 PDMNOTIFYASYNCSTATS Async;
1675 pdmR3NotifyAsyncInit(&Async, "PDMR3Suspend");
1676 for (;;)
1677 {
1678 pdmR3NotifyAsyncBeginLoop(&Async);
1679
1680 /*
1681 * Iterate thru the device instances and USB device instances,
1682 * processing the drivers associated with those.
1683 *
1684 * The attached drivers are normally processed first. Some devices
1685 * (like DevAHCI) though needs to be notified before the drivers so
1686 * that it doesn't kick off any new requests after the drivers stopped
1687 * taking any. (DrvVD changes to read-only in this particular case.)
1688 */
1689 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1690 {
1691 unsigned const cAsyncStart = Async.cAsync;
1692
1693 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION)
1694 pdmR3SuspendDev(pDevIns, &Async);
1695
1696 if (Async.cAsync == cAsyncStart)
1697 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1698 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1699 if (!pdmR3SuspendDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
1700 break;
1701
1702 if ( Async.cAsync == cAsyncStart
1703 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION))
1704 pdmR3SuspendDev(pDevIns, &Async);
1705 }
1706
1707#ifdef VBOX_WITH_USB
1708 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1709 {
1710 unsigned const cAsyncStart = Async.cAsync;
1711
1712 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1713 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1714 if (!pdmR3SuspendDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
1715 break;
1716
1717 if (Async.cAsync == cAsyncStart)
1718 pdmR3SuspendUsb(pUsbIns, &Async);
1719 }
1720#endif
1721 if (!Async.cAsync)
1722 break;
1723 pdmR3NotifyAsyncLog(&Async);
1724 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
1725 }
1726
1727 /*
1728 * Suspend all threads.
1729 */
1730 pdmR3ThreadSuspendAll(pVM);
1731
1732 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1733 LogRel(("PDMR3Suspend: %'llu ns run time\n", cNsElapsed));
1734}
1735
1736
1737/**
1738 * Worker for PDMR3Resume that deals with one driver.
1739 *
1740 * @param pDrvIns The driver instance.
1741 * @param pszDevName The parent device name.
1742 * @param iDevInstance The parent device instance number.
1743 * @param iLun The parent LUN number.
1744 */
1745DECLINLINE(int) pdmR3ResumeDrv(PPDMDRVINS pDrvIns, const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1746{
1747 Assert(pDrvIns->Internal.s.fVMSuspended);
1748 if (pDrvIns->pReg->pfnResume)
1749 {
1750 LogFlow(("PDMR3Resume: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1751 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1752 int rc = VINF_SUCCESS; pDrvIns->pReg->pfnResume(pDrvIns);
1753 if (RT_FAILURE(rc))
1754 {
1755 LogRel(("PDMR3Resume: driver '%s'/%d on LUN#%d of device '%s'/%d -> %Rrc\n",
1756 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, rc));
1757 return rc;
1758 }
1759 }
1760 pDrvIns->Internal.s.fVMSuspended = false;
1761 return VINF_SUCCESS;
1762}
1763
1764
1765/**
1766 * Worker for PDMR3Resume that deals with one USB device instance.
1767 *
1768 * @returns VBox status code.
1769 * @param pUsbIns The USB device instance.
1770 */
1771DECLINLINE(int) pdmR3ResumeUsb(PPDMUSBINS pUsbIns)
1772{
1773 Assert(pUsbIns->Internal.s.fVMSuspended);
1774 if (pUsbIns->pReg->pfnVMResume)
1775 {
1776 LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1777 int rc = VINF_SUCCESS; pUsbIns->pReg->pfnVMResume(pUsbIns);
1778 if (RT_FAILURE(rc))
1779 {
1780 LogRel(("PDMR3Resume: device '%s'/%d -> %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1781 return rc;
1782 }
1783 }
1784 pUsbIns->Internal.s.fVMSuspended = false;
1785 return VINF_SUCCESS;
1786}
1787
1788
1789/**
1790 * Worker for PDMR3Resume that deals with one device instance.
1791 *
1792 * @returns VBox status code.
1793 * @param pDevIns The device instance.
1794 */
1795DECLINLINE(int) pdmR3ResumeDev(PPDMDEVINS pDevIns)
1796{
1797 Assert(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED);
1798 if (pDevIns->pReg->pfnResume)
1799 {
1800 LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1801 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1802 int rc = VINF_SUCCESS; pDevIns->pReg->pfnResume(pDevIns);
1803 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1804 if (RT_FAILURE(rc))
1805 {
1806 LogRel(("PDMR3Resume: device '%s'/%d -> %Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
1807 return rc;
1808 }
1809 }
1810 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
1811 return VINF_SUCCESS;
1812}
1813
1814
1815/**
1816 * This function will notify all the devices and their
1817 * attached drivers about the VM now being resumed.
1818 *
1819 * @param pVM Pointer to the VM.
1820 */
1821VMMR3_INT_DECL(void) PDMR3Resume(PVM pVM)
1822{
1823 LogFlow(("PDMR3Resume:\n"));
1824
1825 /*
1826 * Iterate thru the device instances and USB device instances,
1827 * processing the drivers associated with those.
1828 */
1829 int rc = VINF_SUCCESS;
1830 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns && RT_SUCCESS(rc); pDevIns = pDevIns->Internal.s.pNextR3)
1831 {
1832 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1833 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1834 rc = pdmR3ResumeDrv(pDrvIns, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun);
1835 if (RT_SUCCESS(rc))
1836 rc = pdmR3ResumeDev(pDevIns);
1837 }
1838
1839#ifdef VBOX_WITH_USB
1840 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns && RT_SUCCESS(rc); pUsbIns = pUsbIns->Internal.s.pNext)
1841 {
1842 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1843 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1844 rc = pdmR3ResumeDrv(pDrvIns, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun);
1845 if (RT_SUCCESS(rc))
1846 rc = pdmR3ResumeUsb(pUsbIns);
1847 }
1848#endif
1849
1850 /*
1851 * Resume all threads.
1852 */
1853 if (RT_SUCCESS(rc))
1854 pdmR3ThreadResumeAll(pVM);
1855
1856 /*
1857 * Resume the block cache.
1858 */
1859 if (RT_SUCCESS(rc))
1860 pdmR3BlkCacheResume(pVM);
1861
1862 /*
1863 * On failure, clean up via PDMR3Suspend.
1864 */
1865 if (RT_FAILURE(rc))
1866 PDMR3Suspend(pVM);
1867
1868 LogFlow(("PDMR3Resume: returns %Rrc\n", rc));
1869 return /*rc*/;
1870}
1871
1872
1873/**
1874 * Worker for PDMR3PowerOff that deals with one driver.
1875 *
1876 * @param pDrvIns The driver instance.
1877 * @param pAsync The structure for recording asynchronous
1878 * notification tasks.
1879 * @param pszDevName The parent device name.
1880 * @param iDevInstance The parent device instance number.
1881 * @param iLun The parent LUN number.
1882 */
1883DECLINLINE(bool) pdmR3PowerOffDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
1884 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1885{
1886 if (!pDrvIns->Internal.s.fVMSuspended)
1887 {
1888 pDrvIns->Internal.s.fVMSuspended = true;
1889 if (pDrvIns->pReg->pfnPowerOff)
1890 {
1891 uint64_t cNsElapsed = RTTimeNanoTS();
1892
1893 if (!pDrvIns->Internal.s.pfnAsyncNotify)
1894 {
1895 LogFlow(("PDMR3PowerOff: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1896 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1897 pDrvIns->pReg->pfnPowerOff(pDrvIns);
1898 if (pDrvIns->Internal.s.pfnAsyncNotify)
1899 LogFlow(("PDMR3PowerOff: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1900 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1901 }
1902 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
1903 {
1904 LogFlow(("PDMR3PowerOff: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1905 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1906 pDrvIns->Internal.s.pfnAsyncNotify = NULL;
1907 }
1908
1909 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1910 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
1911 LogRel(("PDMR3PowerOff: Driver '%s'/%d on LUN#%d of device '%s'/%d took %'llu ns to power off\n",
1912 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, cNsElapsed));
1913
1914 if (pDrvIns->Internal.s.pfnAsyncNotify)
1915 {
1916 pDrvIns->Internal.s.fVMSuspended = false;
1917 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance,
1918 pszDevName, iDevInstance, iLun);
1919 return false;
1920 }
1921 }
1922 }
1923 return true;
1924}
1925
1926
1927/**
1928 * Worker for PDMR3PowerOff that deals with one USB device instance.
1929 *
1930 * @param pUsbIns The USB device instance.
1931 * @param pAsync The structure for recording asynchronous
1932 * notification tasks.
1933 */
1934DECLINLINE(void) pdmR3PowerOffUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
1935{
1936 if (!pUsbIns->Internal.s.fVMSuspended)
1937 {
1938 pUsbIns->Internal.s.fVMSuspended = true;
1939 if (pUsbIns->pReg->pfnVMPowerOff)
1940 {
1941 uint64_t cNsElapsed = RTTimeNanoTS();
1942
1943 if (!pUsbIns->Internal.s.pfnAsyncNotify)
1944 {
1945 LogFlow(("PDMR3PowerOff: Notifying - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1946 pUsbIns->pReg->pfnVMPowerOff(pUsbIns);
1947 if (pUsbIns->Internal.s.pfnAsyncNotify)
1948 LogFlow(("PDMR3PowerOff: Async notification started - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1949 }
1950 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
1951 {
1952 LogFlow(("PDMR3PowerOff: Async notification completed - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1953 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
1954 }
1955 if (pUsbIns->Internal.s.pfnAsyncNotify)
1956 {
1957 pUsbIns->Internal.s.fVMSuspended = false;
1958 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
1959 }
1960
1961 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1962 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
1963 LogRel(("PDMR3PowerOff: USB device '%s'/%d took %'llu ns to power off\n",
1964 pUsbIns->pReg->szName, pUsbIns->iInstance, cNsElapsed));
1965
1966 }
1967 }
1968}
1969
1970
1971/**
1972 * Worker for PDMR3PowerOff that deals with one device instance.
1973 *
1974 * @param pDevIns The device instance.
1975 * @param pAsync The structure for recording asynchronous
1976 * notification tasks.
1977 */
1978DECLINLINE(void) pdmR3PowerOffDev(PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
1979{
1980 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED))
1981 {
1982 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_SUSPENDED;
1983 if (pDevIns->pReg->pfnPowerOff)
1984 {
1985 uint64_t cNsElapsed = RTTimeNanoTS();
1986 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1987
1988 if (!pDevIns->Internal.s.pfnAsyncNotify)
1989 {
1990 LogFlow(("PDMR3PowerOff: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1991 pDevIns->pReg->pfnPowerOff(pDevIns);
1992 if (pDevIns->Internal.s.pfnAsyncNotify)
1993 LogFlow(("PDMR3PowerOff: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1994 }
1995 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
1996 {
1997 LogFlow(("PDMR3PowerOff: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1998 pDevIns->Internal.s.pfnAsyncNotify = NULL;
1999 }
2000 if (pDevIns->Internal.s.pfnAsyncNotify)
2001 {
2002 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
2003 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
2004 }
2005
2006 PDMCritSectLeave(pDevIns->pCritSectRoR3);
2007 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2008 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
2009 LogFlow(("PDMR3PowerOff: Device '%s'/%d took %'llu ns to power off\n",
2010 pDevIns->pReg->szName, pDevIns->iInstance, cNsElapsed));
2011 }
2012 }
2013}
2014
2015
2016/**
2017 * This function will notify all the devices and their
2018 * attached drivers about the VM being powered off.
2019 *
2020 * @param pVM Pointer to the VM.
2021 */
2022VMMR3DECL(void) PDMR3PowerOff(PVM pVM)
2023{
2024 LogFlow(("PDMR3PowerOff:\n"));
2025 uint64_t cNsElapsed = RTTimeNanoTS();
2026
2027 /*
2028 * The outer loop repeats until there are no more async requests.
2029 */
2030 PDMNOTIFYASYNCSTATS Async;
2031 pdmR3NotifyAsyncInit(&Async, "PDMR3PowerOff");
2032 for (;;)
2033 {
2034 pdmR3NotifyAsyncBeginLoop(&Async);
2035
2036 /*
2037 * Iterate thru the device instances and USB device instances,
2038 * processing the drivers associated with those.
2039 *
2040 * The attached drivers are normally processed first. Some devices
2041 * (like DevAHCI) though needs to be notified before the drivers so
2042 * that it doesn't kick off any new requests after the drivers stopped
2043 * taking any. (DrvVD changes to read-only in this particular case.)
2044 */
2045 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2046 {
2047 unsigned const cAsyncStart = Async.cAsync;
2048
2049 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION)
2050 pdmR3PowerOffDev(pDevIns, &Async);
2051
2052 if (Async.cAsync == cAsyncStart)
2053 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2054 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2055 if (!pdmR3PowerOffDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
2056 break;
2057
2058 if ( Async.cAsync == cAsyncStart
2059 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION))
2060 pdmR3PowerOffDev(pDevIns, &Async);
2061 }
2062
2063#ifdef VBOX_WITH_USB
2064 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2065 {
2066 unsigned const cAsyncStart = Async.cAsync;
2067
2068 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2069 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2070 if (!pdmR3PowerOffDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
2071 break;
2072
2073 if (Async.cAsync == cAsyncStart)
2074 pdmR3PowerOffUsb(pUsbIns, &Async);
2075 }
2076#endif
2077 if (!Async.cAsync)
2078 break;
2079 pdmR3NotifyAsyncLog(&Async);
2080 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
2081 }
2082
2083 /*
2084 * Suspend all threads.
2085 */
2086 pdmR3ThreadSuspendAll(pVM);
2087
2088 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2089 LogRel(("PDMR3PowerOff: %'llu ns run time\n", cNsElapsed));
2090}
2091
2092
2093/**
2094 * Queries the base interface of a device instance.
2095 *
2096 * The caller can use this to query other interfaces the device implements
2097 * and use them to talk to the device.
2098 *
2099 * @returns VBox status code.
2100 * @param pUVM The user mode VM handle.
2101 * @param pszDevice Device name.
2102 * @param iInstance Device instance.
2103 * @param ppBase Where to store the pointer to the base device interface on success.
2104 * @remark We're not doing any locking ATM, so don't try call this at times when the
2105 * device chain is known to be updated.
2106 */
2107VMMR3DECL(int) PDMR3QueryDevice(PUVM pUVM, const char *pszDevice, unsigned iInstance, PPDMIBASE *ppBase)
2108{
2109 LogFlow(("PDMR3DeviceQuery: pszDevice=%p:{%s} iInstance=%u ppBase=%p\n", pszDevice, pszDevice, iInstance, ppBase));
2110 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2111 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2112
2113 /*
2114 * Iterate registered devices looking for the device.
2115 */
2116 size_t cchDevice = strlen(pszDevice);
2117 for (PPDMDEV pDev = pUVM->pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
2118 {
2119 if ( pDev->cchName == cchDevice
2120 && !memcmp(pDev->pReg->szName, pszDevice, cchDevice))
2121 {
2122 /*
2123 * Iterate device instances.
2124 */
2125 for (PPDMDEVINS pDevIns = pDev->pInstances; pDevIns; pDevIns = pDevIns->Internal.s.pPerDeviceNextR3)
2126 {
2127 if (pDevIns->iInstance == iInstance)
2128 {
2129 if (pDevIns->IBase.pfnQueryInterface)
2130 {
2131 *ppBase = &pDevIns->IBase;
2132 LogFlow(("PDMR3DeviceQuery: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
2133 return VINF_SUCCESS;
2134 }
2135
2136 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NO_IBASE\n"));
2137 return VERR_PDM_DEVICE_INSTANCE_NO_IBASE;
2138 }
2139 }
2140
2141 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NOT_FOUND\n"));
2142 return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND;
2143 }
2144 }
2145
2146 LogFlow(("PDMR3QueryDevice: returns VERR_PDM_DEVICE_NOT_FOUND\n"));
2147 return VERR_PDM_DEVICE_NOT_FOUND;
2148}
2149
2150
2151/**
2152 * Queries the base interface of a device LUN.
2153 *
2154 * This differs from PDMR3QueryLun by that it returns the interface on the
2155 * device and not the top level driver.
2156 *
2157 * @returns VBox status code.
2158 * @param pUVM The user mode VM handle.
2159 * @param pszDevice Device name.
2160 * @param iInstance Device instance.
2161 * @param iLun The Logical Unit to obtain the interface of.
2162 * @param ppBase Where to store the base interface pointer.
2163 * @remark We're not doing any locking ATM, so don't try call this at times when the
2164 * device chain is known to be updated.
2165 */
2166VMMR3DECL(int) PDMR3QueryDeviceLun(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
2167{
2168 LogFlow(("PDMR3QueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
2169 pszDevice, pszDevice, iInstance, iLun, ppBase));
2170 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2171 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2172
2173 /*
2174 * Find the LUN.
2175 */
2176 PPDMLUN pLun;
2177 int rc = pdmR3DevFindLun(pUVM->pVM, pszDevice, iInstance, iLun, &pLun);
2178 if (RT_SUCCESS(rc))
2179 {
2180 *ppBase = pLun->pBase;
2181 LogFlow(("PDMR3QueryDeviceLun: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
2182 return VINF_SUCCESS;
2183 }
2184 LogFlow(("PDMR3QueryDeviceLun: returns %Rrc\n", rc));
2185 return rc;
2186}
2187
2188
2189/**
2190 * Query the interface of the top level driver on a LUN.
2191 *
2192 * @returns VBox status code.
2193 * @param pUVM The user mode VM handle.
2194 * @param pszDevice Device name.
2195 * @param iInstance Device instance.
2196 * @param iLun The Logical Unit to obtain the interface of.
2197 * @param ppBase Where to store the base interface pointer.
2198 * @remark We're not doing any locking ATM, so don't try call this at times when the
2199 * device chain is known to be updated.
2200 */
2201VMMR3DECL(int) PDMR3QueryLun(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
2202{
2203 LogFlow(("PDMR3QueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
2204 pszDevice, pszDevice, iInstance, iLun, ppBase));
2205 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2206 PVM pVM = pUVM->pVM;
2207 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
2208
2209 /*
2210 * Find the LUN.
2211 */
2212 PPDMLUN pLun;
2213 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
2214 if (RT_SUCCESS(rc))
2215 {
2216 if (pLun->pTop)
2217 {
2218 *ppBase = &pLun->pTop->IBase;
2219 LogFlow(("PDMR3QueryLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
2220 return VINF_SUCCESS;
2221 }
2222 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
2223 }
2224 LogFlow(("PDMR3QueryLun: returns %Rrc\n", rc));
2225 return rc;
2226}
2227
2228
2229/**
2230 * Query the interface of a named driver on a LUN.
2231 *
2232 * If the driver appears more than once in the driver chain, the first instance
2233 * is returned.
2234 *
2235 * @returns VBox status code.
2236 * @param pUVM The user mode VM handle.
2237 * @param pszDevice Device name.
2238 * @param iInstance Device instance.
2239 * @param iLun The Logical Unit to obtain the interface of.
2240 * @param pszDriver The driver name.
2241 * @param ppBase Where to store the base interface pointer.
2242 *
2243 * @remark We're not doing any locking ATM, so don't try call this at times when the
2244 * device chain is known to be updated.
2245 */
2246VMMR3DECL(int) PDMR3QueryDriverOnLun(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, const char *pszDriver, PPPDMIBASE ppBase)
2247{
2248 LogFlow(("PDMR3QueryDriverOnLun: pszDevice=%p:{%s} iInstance=%u iLun=%u pszDriver=%p:{%s} ppBase=%p\n",
2249 pszDevice, pszDevice, iInstance, iLun, pszDriver, pszDriver, ppBase));
2250 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2251 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2252
2253 /*
2254 * Find the LUN.
2255 */
2256 PPDMLUN pLun;
2257 int rc = pdmR3DevFindLun(pUVM->pVM, pszDevice, iInstance, iLun, &pLun);
2258 if (RT_SUCCESS(rc))
2259 {
2260 if (pLun->pTop)
2261 {
2262 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2263 if (!strcmp(pDrvIns->pReg->szName, pszDriver))
2264 {
2265 *ppBase = &pDrvIns->IBase;
2266 LogFlow(("PDMR3QueryDriverOnLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
2267 return VINF_SUCCESS;
2268
2269 }
2270 rc = VERR_PDM_DRIVER_NOT_FOUND;
2271 }
2272 else
2273 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
2274 }
2275 LogFlow(("PDMR3QueryDriverOnLun: returns %Rrc\n", rc));
2276 return rc;
2277}
2278
2279/**
2280 * Executes pending DMA transfers.
2281 * Forced Action handler.
2282 *
2283 * @param pVM Pointer to the VM.
2284 */
2285VMMR3DECL(void) PDMR3DmaRun(PVM pVM)
2286{
2287 /* Note! Not really SMP safe; restrict it to VCPU 0. */
2288 if (VMMGetCpuId(pVM) != 0)
2289 return;
2290
2291 if (VM_FF_TESTANDCLEAR(pVM, VM_FF_PDM_DMA))
2292 {
2293 if (pVM->pdm.s.pDmac)
2294 {
2295 bool fMore = pVM->pdm.s.pDmac->Reg.pfnRun(pVM->pdm.s.pDmac->pDevIns);
2296 if (fMore)
2297 VM_FF_SET(pVM, VM_FF_PDM_DMA);
2298 }
2299 }
2300}
2301
2302
2303/**
2304 * Service a VMMCALLRING3_PDM_LOCK call.
2305 *
2306 * @returns VBox status code.
2307 * @param pVM Pointer to the VM.
2308 */
2309VMMR3_INT_DECL(int) PDMR3LockCall(PVM pVM)
2310{
2311 return PDMR3CritSectEnterEx(&pVM->pdm.s.CritSect, true /* fHostCall */);
2312}
2313
2314
2315/**
2316 * Registers the VMM device heap
2317 *
2318 * @returns VBox status code.
2319 * @param pVM Pointer to the VM.
2320 * @param GCPhys The physical address.
2321 * @param pvHeap Ring-3 pointer.
2322 * @param cbSize Size of the heap.
2323 */
2324VMMR3_INT_DECL(int) PDMR3VmmDevHeapRegister(PVM pVM, RTGCPHYS GCPhys, RTR3PTR pvHeap, unsigned cbSize)
2325{
2326 Assert(pVM->pdm.s.pvVMMDevHeap == NULL);
2327
2328 Log(("PDMR3VmmDevHeapRegister %RGp %RHv %x\n", GCPhys, pvHeap, cbSize));
2329 pVM->pdm.s.pvVMMDevHeap = pvHeap;
2330 pVM->pdm.s.GCPhysVMMDevHeap = GCPhys;
2331 pVM->pdm.s.cbVMMDevHeap = cbSize;
2332 pVM->pdm.s.cbVMMDevHeapLeft = cbSize;
2333 return VINF_SUCCESS;
2334}
2335
2336
2337/**
2338 * Unregisters the VMM device heap
2339 *
2340 * @returns VBox status code.
2341 * @param pVM Pointer to the VM.
2342 * @param GCPhys The physical address.
2343 */
2344VMMR3_INT_DECL(int) PDMR3VmmDevHeapUnregister(PVM pVM, RTGCPHYS GCPhys)
2345{
2346 Assert(pVM->pdm.s.GCPhysVMMDevHeap == GCPhys);
2347
2348 Log(("PDMR3VmmDevHeapUnregister %RGp\n", GCPhys));
2349 pVM->pdm.s.pvVMMDevHeap = NULL;
2350 pVM->pdm.s.GCPhysVMMDevHeap = NIL_RTGCPHYS;
2351 pVM->pdm.s.cbVMMDevHeap = 0;
2352 pVM->pdm.s.cbVMMDevHeapLeft = 0;
2353 return VINF_SUCCESS;
2354}
2355
2356
2357/**
2358 * Allocates memory from the VMM device heap
2359 *
2360 * @returns VBox status code.
2361 * @param pVM Pointer to the VM.
2362 * @param cbSize Allocation size.
2363 * @param pv Ring-3 pointer. (out)
2364 */
2365VMMR3_INT_DECL(int) PDMR3VmmDevHeapAlloc(PVM pVM, size_t cbSize, RTR3PTR *ppv)
2366{
2367#ifdef DEBUG_bird
2368 if (!cbSize || cbSize > pVM->pdm.s.cbVMMDevHeapLeft)
2369 return VERR_NO_MEMORY;
2370#else
2371 AssertReturn(cbSize && cbSize <= pVM->pdm.s.cbVMMDevHeapLeft, VERR_NO_MEMORY);
2372#endif
2373
2374 Log(("PDMR3VMMDevHeapAlloc: %#zx\n", cbSize));
2375
2376 /** @todo Not a real heap as there's currently only one user. */
2377 *ppv = pVM->pdm.s.pvVMMDevHeap;
2378 pVM->pdm.s.cbVMMDevHeapLeft = 0;
2379 return VINF_SUCCESS;
2380}
2381
2382
2383/**
2384 * Frees memory from the VMM device heap
2385 *
2386 * @returns VBox status code.
2387 * @param pVM Pointer to the VM.
2388 * @param pv Ring-3 pointer.
2389 */
2390VMMR3_INT_DECL(int) PDMR3VmmDevHeapFree(PVM pVM, RTR3PTR pv)
2391{
2392 Log(("PDMR3VmmDevHeapFree: %RHv\n", pv));
2393
2394 /** @todo not a real heap as there's currently only one user. */
2395 pVM->pdm.s.cbVMMDevHeapLeft = pVM->pdm.s.cbVMMDevHeap;
2396 return VINF_SUCCESS;
2397}
2398
2399
2400/**
2401 * Worker for DBGFR3TraceConfig that checks if the given tracing group name
2402 * matches a device or driver name and applies the tracing config change.
2403 *
2404 * @returns VINF_SUCCESS or VERR_NOT_FOUND.
2405 * @param pVM Pointer to the VM.
2406 * @param pszName The tracing config group name. This is NULL if
2407 * the operation applies to every device and
2408 * driver.
2409 * @param cchName The length to match.
2410 * @param fEnable Whether to enable or disable the corresponding
2411 * trace points.
2412 * @param fApply Whether to actually apply the changes or just do
2413 * existence checks.
2414 */
2415VMMR3_INT_DECL(int) PDMR3TracingConfig(PVM pVM, const char *pszName, size_t cchName, bool fEnable, bool fApply)
2416{
2417 /** @todo This code is potentially racing driver attaching and detaching. */
2418
2419 /*
2420 * Applies to all.
2421 */
2422 if (pszName == NULL)
2423 {
2424 AssertReturn(fApply, VINF_SUCCESS);
2425
2426 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2427 {
2428 pDevIns->fTracing = fEnable;
2429 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2430 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2431 pDrvIns->fTracing = fEnable;
2432 }
2433
2434#ifdef VBOX_WITH_USB
2435 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2436 {
2437 pUsbIns->fTracing = fEnable;
2438 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2439 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2440 pDrvIns->fTracing = fEnable;
2441
2442 }
2443#endif
2444 return VINF_SUCCESS;
2445 }
2446
2447 /*
2448 * Specific devices, USB devices or drivers.
2449 * Decode prefix to figure which of these it applies to.
2450 */
2451 if (cchName <= 3)
2452 return VERR_NOT_FOUND;
2453
2454 uint32_t cMatches = 0;
2455 if (!strncmp("dev", pszName, 3))
2456 {
2457 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2458 {
2459 const char *pszDevName = pDevIns->Internal.s.pDevR3->pReg->szName;
2460 size_t cchDevName = strlen(pszDevName);
2461 if ( ( cchDevName == cchName
2462 && RTStrNICmp(pszName, pszDevName, cchDevName))
2463 || ( cchDevName == cchName - 3
2464 && RTStrNICmp(pszName + 3, pszDevName, cchDevName)) )
2465 {
2466 cMatches++;
2467 if (fApply)
2468 pDevIns->fTracing = fEnable;
2469 }
2470 }
2471 }
2472 else if (!strncmp("usb", pszName, 3))
2473 {
2474 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2475 {
2476 const char *pszUsbName = pUsbIns->Internal.s.pUsbDev->pReg->szName;
2477 size_t cchUsbName = strlen(pszUsbName);
2478 if ( ( cchUsbName == cchName
2479 && RTStrNICmp(pszName, pszUsbName, cchUsbName))
2480 || ( cchUsbName == cchName - 3
2481 && RTStrNICmp(pszName + 3, pszUsbName, cchUsbName)) )
2482 {
2483 cMatches++;
2484 if (fApply)
2485 pUsbIns->fTracing = fEnable;
2486 }
2487 }
2488 }
2489 else if (!strncmp("drv", pszName, 3))
2490 {
2491 AssertReturn(fApply, VINF_SUCCESS);
2492
2493 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2494 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2495 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2496 {
2497 const char *pszDrvName = pDrvIns->Internal.s.pDrv->pReg->szName;
2498 size_t cchDrvName = strlen(pszDrvName);
2499 if ( ( cchDrvName == cchName
2500 && RTStrNICmp(pszName, pszDrvName, cchDrvName))
2501 || ( cchDrvName == cchName - 3
2502 && RTStrNICmp(pszName + 3, pszDrvName, cchDrvName)) )
2503 {
2504 cMatches++;
2505 if (fApply)
2506 pDrvIns->fTracing = fEnable;
2507 }
2508 }
2509
2510#ifdef VBOX_WITH_USB
2511 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2512 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2513 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2514 {
2515 const char *pszDrvName = pDrvIns->Internal.s.pDrv->pReg->szName;
2516 size_t cchDrvName = strlen(pszDrvName);
2517 if ( ( cchDrvName == cchName
2518 && RTStrNICmp(pszName, pszDrvName, cchDrvName))
2519 || ( cchDrvName == cchName - 3
2520 && RTStrNICmp(pszName + 3, pszDrvName, cchDrvName)) )
2521 {
2522 cMatches++;
2523 if (fApply)
2524 pDrvIns->fTracing = fEnable;
2525 }
2526 }
2527#endif
2528 }
2529 else
2530 return VERR_NOT_FOUND;
2531
2532 return cMatches > 0 ? VINF_SUCCESS : VERR_NOT_FOUND;
2533}
2534
2535
2536/**
2537 * Worker for DBGFR3TraceQueryConfig that checks whether all drivers, devices,
2538 * and USB device have the same tracing settings.
2539 *
2540 * @returns true / false.
2541 * @param pVM Pointer to the VM.
2542 * @param fEnabled The tracing setting to check for.
2543 */
2544VMMR3_INT_DECL(bool) PDMR3TracingAreAll(PVM pVM, bool fEnabled)
2545{
2546 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2547 {
2548 if (pDevIns->fTracing != (uint32_t)fEnabled)
2549 return false;
2550
2551 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2552 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2553 if (pDrvIns->fTracing != (uint32_t)fEnabled)
2554 return false;
2555 }
2556
2557#ifdef VBOX_WITH_USB
2558 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2559 {
2560 if (pUsbIns->fTracing != (uint32_t)fEnabled)
2561 return false;
2562
2563 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2564 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2565 if (pDrvIns->fTracing != (uint32_t)fEnabled)
2566 return false;
2567 }
2568#endif
2569
2570 return true;
2571}
2572
2573
2574/**
2575 * Worker for PDMR3TracingQueryConfig that adds a prefixed name to the output
2576 * string.
2577 *
2578 * @returns VINF_SUCCESS or VERR_BUFFER_OVERFLOW
2579 * @param ppszDst The pointer to the output buffer pointer.
2580 * @param pcbDst The pointer to the output buffer size.
2581 * @param fSpace Whether to add a space before the name.
2582 * @param pszPrefix The name prefix.
2583 * @param pszName The name.
2584 */
2585static int pdmR3TracingAdd(char **ppszDst, size_t *pcbDst, bool fSpace, const char *pszPrefix, const char *pszName)
2586{
2587 size_t const cchPrefix = strlen(pszPrefix);
2588 if (!RTStrNICmp(pszPrefix, pszName, cchPrefix))
2589 pszName += cchPrefix;
2590 size_t const cchName = strlen(pszName);
2591
2592 size_t const cchThis = cchName + cchPrefix + fSpace;
2593 if (cchThis >= *pcbDst)
2594 return VERR_BUFFER_OVERFLOW;
2595 if (fSpace)
2596 {
2597 **ppszDst = ' ';
2598 memcpy(*ppszDst + 1, pszPrefix, cchPrefix);
2599 memcpy(*ppszDst + 1 + cchPrefix, pszName, cchName + 1);
2600 }
2601 else
2602 {
2603 memcpy(*ppszDst, pszPrefix, cchPrefix);
2604 memcpy(*ppszDst + cchPrefix, pszName, cchName + 1);
2605 }
2606 *ppszDst += cchThis;
2607 *pcbDst -= cchThis;
2608 return VINF_SUCCESS;
2609}
2610
2611
2612/**
2613 * Worker for DBGFR3TraceQueryConfig use when not everything is either enabled
2614 * or disabled.
2615 *
2616 * @returns VINF_SUCCESS or VERR_BUFFER_OVERFLOW
2617 * @param pVM Pointer to the VM.
2618 * @param pszConfig Where to store the config spec.
2619 * @param cbConfig The size of the output buffer.
2620 */
2621VMMR3_INT_DECL(int) PDMR3TracingQueryConfig(PVM pVM, char *pszConfig, size_t cbConfig)
2622{
2623 int rc;
2624 char *pszDst = pszConfig;
2625 size_t cbDst = cbConfig;
2626
2627 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2628 {
2629 if (pDevIns->fTracing)
2630 {
2631 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "dev", pDevIns->Internal.s.pDevR3->pReg->szName);
2632 if (RT_FAILURE(rc))
2633 return rc;
2634 }
2635
2636 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2637 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2638 if (pDrvIns->fTracing)
2639 {
2640 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "drv", pDrvIns->Internal.s.pDrv->pReg->szName);
2641 if (RT_FAILURE(rc))
2642 return rc;
2643 }
2644 }
2645
2646#ifdef VBOX_WITH_USB
2647 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2648 {
2649 if (pUsbIns->fTracing)
2650 {
2651 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "usb", pUsbIns->Internal.s.pUsbDev->pReg->szName);
2652 if (RT_FAILURE(rc))
2653 return rc;
2654 }
2655
2656 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2657 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2658 if (pDrvIns->fTracing)
2659 {
2660 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "drv", pDrvIns->Internal.s.pDrv->pReg->szName);
2661 if (RT_FAILURE(rc))
2662 return rc;
2663 }
2664 }
2665#endif
2666
2667 return VINF_SUCCESS;
2668}
2669
2670
2671/**
2672 * Checks that a PDMDRVREG::szName, PDMDEVREG::szName or PDMUSBREG::szName
2673 * field contains only a limited set of ASCII characters.
2674 *
2675 * @returns true / false.
2676 * @param pszName The name to validate.
2677 */
2678bool pdmR3IsValidName(const char *pszName)
2679{
2680 char ch;
2681 while ( (ch = *pszName) != '\0'
2682 && ( RT_C_IS_ALNUM(ch)
2683 || ch == '-'
2684 || ch == ' ' /** @todo disallow this! */
2685 || ch == '_') )
2686 pszName++;
2687 return ch == '\0';
2688}
2689
2690
2691/**
2692 * Info handler for 'pdmtracingids'.
2693 *
2694 * @param pVM Pointer to the VM.
2695 * @param pHlp The output helpers.
2696 * @param pszArgs The optional user arguments.
2697 *
2698 * @remarks Can be called on most threads.
2699 */
2700static DECLCALLBACK(void) pdmR3InfoTracingIds(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2701{
2702 /*
2703 * Parse the argument (optional).
2704 */
2705 if ( pszArgs
2706 && *pszArgs
2707 && strcmp(pszArgs, "all")
2708 && strcmp(pszArgs, "devices")
2709 && strcmp(pszArgs, "drivers")
2710 && strcmp(pszArgs, "usb"))
2711 {
2712 pHlp->pfnPrintf(pHlp, "Unable to grok '%s'\n", pszArgs);
2713 return;
2714 }
2715 bool fAll = !pszArgs || !*pszArgs || !strcmp(pszArgs, "all");
2716 bool fDevices = fAll || !strcmp(pszArgs, "devices");
2717 bool fUsbDevs = fAll || !strcmp(pszArgs, "usb");
2718 bool fDrivers = fAll || !strcmp(pszArgs, "drivers");
2719
2720 /*
2721 * Produce the requested output.
2722 */
2723/** @todo lock PDM lists! */
2724 /* devices */
2725 if (fDevices)
2726 {
2727 pHlp->pfnPrintf(pHlp, "Device tracing IDs:\n");
2728 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2729 pHlp->pfnPrintf(pHlp, "%05u %s\n", pDevIns->idTracing, pDevIns->Internal.s.pDevR3->pReg->szName);
2730 }
2731
2732 /* USB devices */
2733 if (fUsbDevs)
2734 {
2735 pHlp->pfnPrintf(pHlp, "USB device tracing IDs:\n");
2736 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2737 pHlp->pfnPrintf(pHlp, "%05u %s\n", pUsbIns->idTracing, pUsbIns->Internal.s.pUsbDev->pReg->szName);
2738 }
2739
2740 /* Drivers */
2741 if (fDrivers)
2742 {
2743 pHlp->pfnPrintf(pHlp, "Driver tracing IDs:\n");
2744 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2745 {
2746 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2747 {
2748 uint32_t iLevel = 0;
2749 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown, iLevel++)
2750 pHlp->pfnPrintf(pHlp, "%05u %s (level %u, lun %u, dev %s)\n",
2751 pDrvIns->idTracing, pDrvIns->Internal.s.pDrv->pReg->szName,
2752 iLevel, pLun->iLun, pDevIns->Internal.s.pDevR3->pReg->szName);
2753 }
2754 }
2755
2756 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2757 {
2758 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2759 {
2760 uint32_t iLevel = 0;
2761 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown, iLevel++)
2762 pHlp->pfnPrintf(pHlp, "%05u %s (level %u, lun %u, dev %s)\n",
2763 pDrvIns->idTracing, pDrvIns->Internal.s.pDrv->pReg->szName,
2764 iLevel, pLun->iLun, pUsbIns->Internal.s.pUsbDev->pReg->szName);
2765 }
2766 }
2767 }
2768}
2769
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