VirtualBox

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

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

PDMAsyncCompletion: PVM -> PUVM (one instance), internalize internal APIs where possible (not all because of test cases). API docs lives where the implmentation lives, NOT in headers (IPRT is the exception).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 100.6 KB
Line 
1/* $Id: PDM.cpp 44358 2013-01-24 16:05:55Z 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 //SSMR3DeregisterDriver(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 }
670
671 /*
672 * Destroy all threads.
673 */
674 pdmR3ThreadDestroyAll(pVM);
675
676 /*
677 * Destroy the block cache.
678 */
679 pdmR3BlkCacheTerm(pVM);
680
681#ifdef VBOX_WITH_NETSHAPER
682 /*
683 * Destroy network bandwidth groups.
684 */
685 pdmR3NetShaperTerm(pVM);
686#endif
687#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
688 /*
689 * Free async completion managers.
690 */
691 pdmR3AsyncCompletionTerm(pVM);
692#endif
693
694 /*
695 * Free modules.
696 */
697 pdmR3LdrTermU(pVM->pUVM);
698
699 /*
700 * Destroy the PDM lock.
701 */
702 PDMR3CritSectDelete(&pVM->pdm.s.CritSect);
703 /* The MiscCritSect is deleted by PDMR3CritSectTerm. */
704
705 LogFlow(("PDMR3Term: returns %Rrc\n", VINF_SUCCESS));
706 return VINF_SUCCESS;
707}
708
709
710/**
711 * Terminates the PDM part of the UVM.
712 *
713 * This will unload any modules left behind.
714 *
715 * @param pUVM Pointer to the user mode VM structure.
716 */
717VMMR3_INT_DECL(void) PDMR3TermUVM(PUVM pUVM)
718{
719 /*
720 * In the normal cause of events we will now call pdmR3LdrTermU for
721 * the second time. In the case of init failure however, this might
722 * the first time, which is why we do it.
723 */
724 pdmR3LdrTermU(pUVM);
725
726 Assert(pUVM->pdm.s.pCritSects == NULL);
727 RTCritSectDelete(&pUVM->pdm.s.ListCritSect);
728}
729
730
731/**
732 * Bits that are saved in pass 0 and in the final pass.
733 *
734 * @param pVM Pointer to the VM.
735 * @param pSSM The saved state handle.
736 */
737static void pdmR3SaveBoth(PVM pVM, PSSMHANDLE pSSM)
738{
739 /*
740 * Save the list of device instances so we can check that they're all still
741 * there when we load the state and that nothing new has been added.
742 */
743 uint32_t i = 0;
744 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3, i++)
745 {
746 SSMR3PutU32(pSSM, i);
747 SSMR3PutStrZ(pSSM, pDevIns->pReg->szName);
748 SSMR3PutU32(pSSM, pDevIns->iInstance);
749 }
750 SSMR3PutU32(pSSM, UINT32_MAX); /* terminator */
751}
752
753
754/**
755 * Live save.
756 *
757 * @returns VBox status code.
758 * @param pVM Pointer to the VM.
759 * @param pSSM The saved state handle.
760 * @param uPass The pass.
761 */
762static DECLCALLBACK(int) pdmR3LiveExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uPass)
763{
764 LogFlow(("pdmR3LiveExec:\n"));
765 AssertReturn(uPass == 0, VERR_SSM_UNEXPECTED_PASS);
766 pdmR3SaveBoth(pVM, pSSM);
767 return VINF_SSM_DONT_CALL_AGAIN;
768}
769
770
771/**
772 * Execute state save operation.
773 *
774 * @returns VBox status code.
775 * @param pVM Pointer to the VM.
776 * @param pSSM The saved state handle.
777 */
778static DECLCALLBACK(int) pdmR3SaveExec(PVM pVM, PSSMHANDLE pSSM)
779{
780 LogFlow(("pdmR3SaveExec:\n"));
781
782 /*
783 * Save interrupt and DMA states.
784 */
785 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
786 {
787 PVMCPU pVCpu = &pVM->aCpus[idCpu];
788 SSMR3PutU32(pSSM, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_APIC));
789 SSMR3PutU32(pSSM, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_PIC));
790 SSMR3PutU32(pSSM, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_NMI));
791 SSMR3PutU32(pSSM, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_SMI));
792 }
793 SSMR3PutU32(pSSM, VM_FF_ISSET(pVM, VM_FF_PDM_DMA));
794
795 pdmR3SaveBoth(pVM, pSSM);
796 return VINF_SUCCESS;
797}
798
799
800/**
801 * Prepare state load operation.
802 *
803 * This will dispatch pending operations and clear the FFs governed by PDM and its devices.
804 *
805 * @returns VBox status code.
806 * @param pVM Pointer to the VM.
807 * @param pSSM The SSM handle.
808 */
809static DECLCALLBACK(int) pdmR3LoadPrep(PVM pVM, PSSMHANDLE pSSM)
810{
811 LogFlow(("pdmR3LoadPrep: %s%s\n",
812 VM_FF_ISSET(pVM, VM_FF_PDM_QUEUES) ? " VM_FF_PDM_QUEUES" : "",
813 VM_FF_ISSET(pVM, VM_FF_PDM_DMA) ? " VM_FF_PDM_DMA" : ""));
814#ifdef LOG_ENABLED
815 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
816 {
817 PVMCPU pVCpu = &pVM->aCpus[idCpu];
818 LogFlow(("pdmR3LoadPrep: VCPU %u %s%s\n", idCpu,
819 VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_APIC) ? " VMCPU_FF_INTERRUPT_APIC" : "",
820 VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_PIC) ? " VMCPU_FF_INTERRUPT_PIC" : ""));
821 }
822#endif
823 NOREF(pSSM);
824
825 /*
826 * In case there is work pending that will raise an interrupt,
827 * start a DMA transfer, or release a lock. (unlikely)
828 */
829 if (VM_FF_ISSET(pVM, VM_FF_PDM_QUEUES))
830 PDMR3QueueFlushAll(pVM);
831
832 /* Clear the FFs. */
833 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
834 {
835 PVMCPU pVCpu = &pVM->aCpus[idCpu];
836 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_APIC);
837 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_PIC);
838 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_NMI);
839 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_SMI);
840 }
841 VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
842
843 return VINF_SUCCESS;
844}
845
846
847/**
848 * Execute state load operation.
849 *
850 * @returns VBox status code.
851 * @param pVM Pointer to the VM.
852 * @param pSSM SSM operation handle.
853 * @param uVersion Data layout version.
854 * @param uPass The data pass.
855 */
856static DECLCALLBACK(int) pdmR3LoadExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
857{
858 int rc;
859
860 LogFlow(("pdmR3LoadExec: uPass=%#x\n", uPass));
861
862 /*
863 * Validate version.
864 */
865 if ( uVersion != PDM_SAVED_STATE_VERSION
866 && uVersion != PDM_SAVED_STATE_VERSION_PRE_NMI_FF)
867 {
868 AssertMsgFailed(("Invalid version uVersion=%d!\n", uVersion));
869 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
870 }
871
872 if (uPass == SSM_PASS_FINAL)
873 {
874 /*
875 * Load the interrupt and DMA states.
876 */
877 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
878 {
879 PVMCPU pVCpu = &pVM->aCpus[idCpu];
880
881 /* APIC interrupt */
882 uint32_t fInterruptPending = 0;
883 rc = SSMR3GetU32(pSSM, &fInterruptPending);
884 if (RT_FAILURE(rc))
885 return rc;
886 if (fInterruptPending & ~1)
887 {
888 AssertMsgFailed(("fInterruptPending=%#x (APIC)\n", fInterruptPending));
889 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
890 }
891 AssertRelease(!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_APIC));
892 if (fInterruptPending)
893 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC);
894
895 /* PIC interrupt */
896 fInterruptPending = 0;
897 rc = SSMR3GetU32(pSSM, &fInterruptPending);
898 if (RT_FAILURE(rc))
899 return rc;
900 if (fInterruptPending & ~1)
901 {
902 AssertMsgFailed(("fInterruptPending=%#x (PIC)\n", fInterruptPending));
903 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
904 }
905 AssertRelease(!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_PIC));
906 if (fInterruptPending)
907 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_PIC);
908
909 if (uVersion > PDM_SAVED_STATE_VERSION_PRE_NMI_FF)
910 {
911 /* NMI interrupt */
912 fInterruptPending = 0;
913 rc = SSMR3GetU32(pSSM, &fInterruptPending);
914 if (RT_FAILURE(rc))
915 return rc;
916 if (fInterruptPending & ~1)
917 {
918 AssertMsgFailed(("fInterruptPending=%#x (NMI)\n", fInterruptPending));
919 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
920 }
921 AssertRelease(!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_NMI));
922 if (fInterruptPending)
923 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_NMI);
924
925 /* SMI interrupt */
926 fInterruptPending = 0;
927 rc = SSMR3GetU32(pSSM, &fInterruptPending);
928 if (RT_FAILURE(rc))
929 return rc;
930 if (fInterruptPending & ~1)
931 {
932 AssertMsgFailed(("fInterruptPending=%#x (SMI)\n", fInterruptPending));
933 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
934 }
935 AssertRelease(!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_SMI));
936 if (fInterruptPending)
937 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_SMI);
938 }
939 }
940
941 /* DMA pending */
942 uint32_t fDMAPending = 0;
943 rc = SSMR3GetU32(pSSM, &fDMAPending);
944 if (RT_FAILURE(rc))
945 return rc;
946 if (fDMAPending & ~1)
947 {
948 AssertMsgFailed(("fDMAPending=%#x\n", fDMAPending));
949 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
950 }
951 if (fDMAPending)
952 VM_FF_SET(pVM, VM_FF_PDM_DMA);
953 Log(("pdmR3LoadExec: VM_FF_PDM_DMA=%RTbool\n", VM_FF_ISSET(pVM, VM_FF_PDM_DMA)));
954 }
955
956 /*
957 * Load the list of devices and verify that they are all there.
958 */
959 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
960 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_FOUND;
961
962 for (uint32_t i = 0; ; i++)
963 {
964 /* Get the sequence number / terminator. */
965 uint32_t u32Sep;
966 rc = SSMR3GetU32(pSSM, &u32Sep);
967 if (RT_FAILURE(rc))
968 return rc;
969 if (u32Sep == UINT32_MAX)
970 break;
971 if (u32Sep != i)
972 AssertMsgFailedReturn(("Out of sequence. u32Sep=%#x i=%#x\n", u32Sep, i), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
973
974 /* Get the name and instance number. */
975 char szName[RT_SIZEOFMEMB(PDMDEVREG, szName)];
976 rc = SSMR3GetStrZ(pSSM, szName, sizeof(szName));
977 if (RT_FAILURE(rc))
978 return rc;
979 uint32_t iInstance;
980 rc = SSMR3GetU32(pSSM, &iInstance);
981 if (RT_FAILURE(rc))
982 return rc;
983
984 /* Try locate it. */
985 PPDMDEVINS pDevIns;
986 for (pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
987 if ( !strcmp(szName, pDevIns->pReg->szName)
988 && pDevIns->iInstance == iInstance)
989 {
990 AssertLogRelMsgReturn(!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_FOUND),
991 ("%s/#%u\n", pDevIns->pReg->szName, pDevIns->iInstance),
992 VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
993 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_FOUND;
994 break;
995 }
996 if (!pDevIns)
997 {
998 LogRel(("Device '%s'/%d not found in current config\n", szName, iInstance));
999 if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
1000 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Device '%s'/%d not found in current config"), szName, iInstance);
1001 }
1002 }
1003
1004 /*
1005 * Check that no additional devices were configured.
1006 */
1007 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1008 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_FOUND))
1009 {
1010 LogRel(("Device '%s'/%d not found in the saved state\n", pDevIns->pReg->szName, pDevIns->iInstance));
1011 if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
1012 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Device '%s'/%d not found in the saved state"),
1013 pDevIns->pReg->szName, pDevIns->iInstance);
1014 }
1015
1016 return VINF_SUCCESS;
1017}
1018
1019
1020/**
1021 * Worker for PDMR3PowerOn that deals with one driver.
1022 *
1023 * @param pDrvIns The driver instance.
1024 * @param pszDevName The parent device name.
1025 * @param iDevInstance The parent device instance number.
1026 * @param iLun The parent LUN number.
1027 */
1028DECLINLINE(int) pdmR3PowerOnDrv(PPDMDRVINS pDrvIns, const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1029{
1030 Assert(pDrvIns->Internal.s.fVMSuspended);
1031 if (pDrvIns->pReg->pfnPowerOn)
1032 {
1033 LogFlow(("PDMR3PowerOn: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1034 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1035 int rc = VINF_SUCCESS; pDrvIns->pReg->pfnPowerOn(pDrvIns);
1036 if (RT_FAILURE(rc))
1037 {
1038 LogRel(("PDMR3PowerOn: driver '%s'/%d on LUN#%d of device '%s'/%d -> %Rrc\n",
1039 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, rc));
1040 return rc;
1041 }
1042 }
1043 pDrvIns->Internal.s.fVMSuspended = false;
1044 return VINF_SUCCESS;
1045}
1046
1047
1048/**
1049 * Worker for PDMR3PowerOn that deals with one USB device instance.
1050 *
1051 * @returns VBox status code.
1052 * @param pUsbIns The USB device instance.
1053 */
1054DECLINLINE(int) pdmR3PowerOnUsb(PPDMUSBINS pUsbIns)
1055{
1056 Assert(pUsbIns->Internal.s.fVMSuspended);
1057 if (pUsbIns->pReg->pfnVMPowerOn)
1058 {
1059 LogFlow(("PDMR3PowerOn: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1060 int rc = VINF_SUCCESS; pUsbIns->pReg->pfnVMPowerOn(pUsbIns);
1061 if (RT_FAILURE(rc))
1062 {
1063 LogRel(("PDMR3PowerOn: device '%s'/%d -> %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1064 return rc;
1065 }
1066 }
1067 pUsbIns->Internal.s.fVMSuspended = false;
1068 return VINF_SUCCESS;
1069}
1070
1071
1072/**
1073 * Worker for PDMR3PowerOn that deals with one device instance.
1074 *
1075 * @returns VBox status code.
1076 * @param pDevIns The device instance.
1077 */
1078DECLINLINE(int) pdmR3PowerOnDev(PPDMDEVINS pDevIns)
1079{
1080 Assert(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED);
1081 if (pDevIns->pReg->pfnPowerOn)
1082 {
1083 LogFlow(("PDMR3PowerOn: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1084 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1085 int rc = VINF_SUCCESS; pDevIns->pReg->pfnPowerOn(pDevIns);
1086 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1087 if (RT_FAILURE(rc))
1088 {
1089 LogRel(("PDMR3PowerOn: device '%s'/%d -> %Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
1090 return rc;
1091 }
1092 }
1093 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
1094 return VINF_SUCCESS;
1095}
1096
1097
1098/**
1099 * This function will notify all the devices and their
1100 * attached drivers about the VM now being powered on.
1101 *
1102 * @param pVM Pointer to the VM.
1103 */
1104VMMR3DECL(void) PDMR3PowerOn(PVM pVM)
1105{
1106 LogFlow(("PDMR3PowerOn:\n"));
1107
1108 /*
1109 * Iterate thru the device instances and USB device instances,
1110 * processing the drivers associated with those.
1111 */
1112 int rc = VINF_SUCCESS;
1113 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns && RT_SUCCESS(rc); pDevIns = pDevIns->Internal.s.pNextR3)
1114 {
1115 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1116 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1117 rc = pdmR3PowerOnDrv(pDrvIns, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun);
1118 if (RT_SUCCESS(rc))
1119 rc = pdmR3PowerOnDev(pDevIns);
1120 }
1121
1122#ifdef VBOX_WITH_USB
1123 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns && RT_SUCCESS(rc); pUsbIns = pUsbIns->Internal.s.pNext)
1124 {
1125 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1126 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1127 rc = pdmR3PowerOnDrv(pDrvIns, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun);
1128 if (RT_SUCCESS(rc))
1129 rc = pdmR3PowerOnUsb(pUsbIns);
1130 }
1131#endif
1132
1133#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
1134 pdmR3AsyncCompletionResume(pVM);
1135#endif
1136
1137 /*
1138 * Resume all threads.
1139 */
1140 if (RT_SUCCESS(rc))
1141 pdmR3ThreadResumeAll(pVM);
1142
1143 /*
1144 * On failure, clean up via PDMR3Suspend.
1145 */
1146 if (RT_FAILURE(rc))
1147 PDMR3Suspend(pVM);
1148
1149 LogFlow(("PDMR3PowerOn: returns %Rrc\n", rc));
1150 return /*rc*/;
1151}
1152
1153
1154/**
1155 * Initializes the asynchronous notifi stats structure.
1156 *
1157 * @param pThis The asynchronous notifification stats.
1158 * @param pszOp The name of the operation.
1159 */
1160static void pdmR3NotifyAsyncInit(PPDMNOTIFYASYNCSTATS pThis, const char *pszOp)
1161{
1162 pThis->uStartNsTs = RTTimeNanoTS();
1163 pThis->cNsElapsedNextLog = 0;
1164 pThis->cLoops = 0;
1165 pThis->cAsync = 0;
1166 pThis->pszOp = pszOp;
1167 pThis->offList = 0;
1168 pThis->szList[0] = '\0';
1169}
1170
1171
1172/**
1173 * Begin a new loop, prepares to gather new stats.
1174 *
1175 * @param pThis The asynchronous notifification stats.
1176 */
1177static void pdmR3NotifyAsyncBeginLoop(PPDMNOTIFYASYNCSTATS pThis)
1178{
1179 pThis->cLoops++;
1180 pThis->cAsync = 0;
1181 pThis->offList = 0;
1182 pThis->szList[0] = '\0';
1183}
1184
1185
1186/**
1187 * Records a device or USB device with a pending asynchronous notification.
1188 *
1189 * @param pThis The asynchronous notifification stats.
1190 * @param pszName The name of the thing.
1191 * @param iInstance The instance number.
1192 */
1193static void pdmR3NotifyAsyncAdd(PPDMNOTIFYASYNCSTATS pThis, const char *pszName, uint32_t iInstance)
1194{
1195 pThis->cAsync++;
1196 if (pThis->offList < sizeof(pThis->szList) - 4)
1197 pThis->offList += RTStrPrintf(&pThis->szList[pThis->offList], sizeof(pThis->szList) - pThis->offList,
1198 pThis->offList == 0 ? "%s/%u" : ", %s/%u",
1199 pszName, iInstance);
1200}
1201
1202
1203/**
1204 * Records the asynchronous completition of a reset, suspend or power off.
1205 *
1206 * @param pThis The asynchronous notifification stats.
1207 * @param pszDrvName The driver name.
1208 * @param iDrvInstance The driver instance number.
1209 * @param pszDevName The device or USB device name.
1210 * @param iDevInstance The device or USB device instance number.
1211 * @param iLun The LUN.
1212 */
1213static void pdmR3NotifyAsyncAddDrv(PPDMNOTIFYASYNCSTATS pThis, const char *pszDrvName, uint32_t iDrvInstance,
1214 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1215{
1216 pThis->cAsync++;
1217 if (pThis->offList < sizeof(pThis->szList) - 8)
1218 pThis->offList += RTStrPrintf(&pThis->szList[pThis->offList], sizeof(pThis->szList) - pThis->offList,
1219 pThis->offList == 0 ? "%s/%u/%u/%s/%u" : ", %s/%u/%u/%s/%u",
1220 pszDevName, iDevInstance, iLun, pszDrvName, iDrvInstance);
1221}
1222
1223
1224/**
1225 * Log the stats.
1226 *
1227 * @param pThis The asynchronous notifification stats.
1228 */
1229static void pdmR3NotifyAsyncLog(PPDMNOTIFYASYNCSTATS pThis)
1230{
1231 /*
1232 * Return if we shouldn't log at this point.
1233 * We log with an internval increasing from 0 sec to 60 sec.
1234 */
1235 if (!pThis->cAsync)
1236 return;
1237
1238 uint64_t cNsElapsed = RTTimeNanoTS() - pThis->uStartNsTs;
1239 if (cNsElapsed < pThis->cNsElapsedNextLog)
1240 return;
1241
1242 if (pThis->cNsElapsedNextLog == 0)
1243 pThis->cNsElapsedNextLog = RT_NS_1SEC;
1244 else if (pThis->cNsElapsedNextLog >= RT_NS_1MIN / 2)
1245 pThis->cNsElapsedNextLog = RT_NS_1MIN;
1246 else
1247 pThis->cNsElapsedNextLog *= 2;
1248
1249 /*
1250 * Do the logging.
1251 */
1252 LogRel(("%s: after %5llu ms, %u loops: %u async tasks - %s\n",
1253 pThis->pszOp, cNsElapsed / RT_NS_1MS, pThis->cLoops, pThis->cAsync, pThis->szList));
1254}
1255
1256
1257/**
1258 * Wait for events and process pending requests.
1259 *
1260 * @param pThis The asynchronous notifification stats.
1261 * @param pVM Pointer to the VM.
1262 */
1263static void pdmR3NotifyAsyncWaitAndProcessRequests(PPDMNOTIFYASYNCSTATS pThis, PVM pVM)
1264{
1265 VM_ASSERT_EMT0(pVM);
1266 int rc = VMR3AsyncPdmNotificationWaitU(&pVM->pUVM->aCpus[0]);
1267 AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc - %s - %s\n", rc, pThis->pszOp, pThis->szList));
1268
1269 rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY, true /*fPriorityOnly*/);
1270 AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc - %s - %s\n", rc, pThis->pszOp, pThis->szList));
1271 rc = VMR3ReqProcessU(pVM->pUVM, 0/*idDstCpu*/, true /*fPriorityOnly*/);
1272 AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc - %s - %s\n", rc, pThis->pszOp, pThis->szList));
1273}
1274
1275
1276/**
1277 * Worker for PDMR3Reset that deals with one driver.
1278 *
1279 * @param pDrvIns The driver instance.
1280 * @param pAsync The structure for recording asynchronous
1281 * notification tasks.
1282 * @param pszDevName The parent device name.
1283 * @param iDevInstance The parent device instance number.
1284 * @param iLun The parent LUN number.
1285 */
1286DECLINLINE(bool) pdmR3ResetDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
1287 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1288{
1289 if (!pDrvIns->Internal.s.fVMReset)
1290 {
1291 pDrvIns->Internal.s.fVMReset = true;
1292 if (pDrvIns->pReg->pfnReset)
1293 {
1294 if (!pDrvIns->Internal.s.pfnAsyncNotify)
1295 {
1296 LogFlow(("PDMR3Reset: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1297 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1298 pDrvIns->pReg->pfnReset(pDrvIns);
1299 if (pDrvIns->Internal.s.pfnAsyncNotify)
1300 LogFlow(("PDMR3Reset: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1301 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1302 }
1303 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
1304 {
1305 LogFlow(("PDMR3Reset: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1306 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1307 pDrvIns->Internal.s.pfnAsyncNotify = NULL;
1308 }
1309 if (pDrvIns->Internal.s.pfnAsyncNotify)
1310 {
1311 pDrvIns->Internal.s.fVMReset = false;
1312 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance,
1313 pszDevName, iDevInstance, iLun);
1314 return false;
1315 }
1316 }
1317 }
1318 return true;
1319}
1320
1321
1322/**
1323 * Worker for PDMR3Reset that deals with one USB device instance.
1324 *
1325 * @param pUsbIns The USB device instance.
1326 * @param pAsync The structure for recording asynchronous
1327 * notification tasks.
1328 */
1329DECLINLINE(void) pdmR3ResetUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
1330{
1331 if (!pUsbIns->Internal.s.fVMReset)
1332 {
1333 pUsbIns->Internal.s.fVMReset = true;
1334 if (pUsbIns->pReg->pfnVMReset)
1335 {
1336 if (!pUsbIns->Internal.s.pfnAsyncNotify)
1337 {
1338 LogFlow(("PDMR3Reset: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1339 pUsbIns->pReg->pfnVMReset(pUsbIns);
1340 if (pUsbIns->Internal.s.pfnAsyncNotify)
1341 LogFlow(("PDMR3Reset: Async notification started - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1342 }
1343 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
1344 {
1345 LogFlow(("PDMR3Reset: Async notification completed - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1346 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
1347 }
1348 if (pUsbIns->Internal.s.pfnAsyncNotify)
1349 {
1350 pUsbIns->Internal.s.fVMReset = false;
1351 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
1352 }
1353 }
1354 }
1355}
1356
1357
1358/**
1359 * Worker for PDMR3Reset that deals with one device instance.
1360 *
1361 * @param pDevIns The device instance.
1362 * @param pAsync The structure for recording asynchronous
1363 * notification tasks.
1364 */
1365DECLINLINE(void) pdmR3ResetDev(PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
1366{
1367 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_RESET))
1368 {
1369 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_RESET;
1370 if (pDevIns->pReg->pfnReset)
1371 {
1372 uint64_t cNsElapsed = RTTimeNanoTS();
1373 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1374
1375 if (!pDevIns->Internal.s.pfnAsyncNotify)
1376 {
1377 LogFlow(("PDMR3Reset: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1378 pDevIns->pReg->pfnReset(pDevIns);
1379 if (pDevIns->Internal.s.pfnAsyncNotify)
1380 LogFlow(("PDMR3Reset: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1381 }
1382 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
1383 {
1384 LogFlow(("PDMR3Reset: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1385 pDevIns->Internal.s.pfnAsyncNotify = NULL;
1386 }
1387 if (pDevIns->Internal.s.pfnAsyncNotify)
1388 {
1389 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_RESET;
1390 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
1391 }
1392
1393 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1394 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1395 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1396 LogRel(("PDMR3Reset: device '%s'/%d took %'llu ns to reset\n",
1397 pDevIns->pReg->szName, pDevIns->iInstance, cNsElapsed));
1398 }
1399 }
1400}
1401
1402
1403/**
1404 * Resets a virtual CPU.
1405 *
1406 * Used by PDMR3Reset and CPU hot plugging.
1407 *
1408 * @param pVCpu Pointer to the VMCPU.
1409 */
1410VMMR3_INT_DECL(void) PDMR3ResetCpu(PVMCPU pVCpu)
1411{
1412 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_APIC);
1413 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_PIC);
1414 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_NMI);
1415 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_SMI);
1416}
1417
1418
1419/**
1420 * This function will notify all the devices and their attached drivers about
1421 * the VM now being reset.
1422 *
1423 * @param pVM Pointer to the VM.
1424 */
1425VMMR3_INT_DECL(void) PDMR3Reset(PVM pVM)
1426{
1427 LogFlow(("PDMR3Reset:\n"));
1428
1429 /*
1430 * Clear all the reset flags.
1431 */
1432 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1433 {
1434 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_RESET;
1435 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1436 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1437 pDrvIns->Internal.s.fVMReset = false;
1438 }
1439#ifdef VBOX_WITH_USB
1440 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1441 {
1442 pUsbIns->Internal.s.fVMReset = false;
1443 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1444 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1445 pDrvIns->Internal.s.fVMReset = false;
1446 }
1447#endif
1448
1449 /*
1450 * The outer loop repeats until there are no more async requests.
1451 */
1452 PDMNOTIFYASYNCSTATS Async;
1453 pdmR3NotifyAsyncInit(&Async, "PDMR3Reset");
1454 for (;;)
1455 {
1456 pdmR3NotifyAsyncBeginLoop(&Async);
1457
1458 /*
1459 * Iterate thru the device instances and USB device instances,
1460 * processing the drivers associated with those.
1461 */
1462 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1463 {
1464 unsigned const cAsyncStart = Async.cAsync;
1465
1466 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_RESET_NOTIFICATION)
1467 pdmR3ResetDev(pDevIns, &Async);
1468
1469 if (Async.cAsync == cAsyncStart)
1470 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1471 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1472 if (!pdmR3ResetDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
1473 break;
1474
1475 if ( Async.cAsync == cAsyncStart
1476 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_RESET_NOTIFICATION))
1477 pdmR3ResetDev(pDevIns, &Async);
1478 }
1479
1480#ifdef VBOX_WITH_USB
1481 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1482 {
1483 unsigned const cAsyncStart = Async.cAsync;
1484
1485 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1486 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1487 if (!pdmR3ResetDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
1488 break;
1489
1490 if (Async.cAsync == cAsyncStart)
1491 pdmR3ResetUsb(pUsbIns, &Async);
1492 }
1493#endif
1494 if (!Async.cAsync)
1495 break;
1496 pdmR3NotifyAsyncLog(&Async);
1497 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
1498 }
1499
1500 /*
1501 * Clear all pending interrupts and DMA operations.
1502 */
1503 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
1504 PDMR3ResetCpu(&pVM->aCpus[idCpu]);
1505 VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
1506
1507 LogFlow(("PDMR3Reset: returns void\n"));
1508}
1509
1510
1511/**
1512 * Worker for PDMR3Suspend that deals with one driver.
1513 *
1514 * @param pDrvIns The driver instance.
1515 * @param pAsync The structure for recording asynchronous
1516 * notification tasks.
1517 * @param pszDevName The parent device name.
1518 * @param iDevInstance The parent device instance number.
1519 * @param iLun The parent LUN number.
1520 */
1521DECLINLINE(bool) pdmR3SuspendDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
1522 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1523{
1524 if (!pDrvIns->Internal.s.fVMSuspended)
1525 {
1526 pDrvIns->Internal.s.fVMSuspended = true;
1527 if (pDrvIns->pReg->pfnSuspend)
1528 {
1529 uint64_t cNsElapsed = RTTimeNanoTS();
1530
1531 if (!pDrvIns->Internal.s.pfnAsyncNotify)
1532 {
1533 LogFlow(("PDMR3Suspend: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1534 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1535 pDrvIns->pReg->pfnSuspend(pDrvIns);
1536 if (pDrvIns->Internal.s.pfnAsyncNotify)
1537 LogFlow(("PDMR3Suspend: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1538 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1539 }
1540 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
1541 {
1542 LogFlow(("PDMR3Suspend: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1543 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1544 pDrvIns->Internal.s.pfnAsyncNotify = NULL;
1545 }
1546
1547 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1548 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1549 LogRel(("PDMR3Suspend: Driver '%s'/%d on LUN#%d of device '%s'/%d took %'llu ns to suspend\n",
1550 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, cNsElapsed));
1551
1552 if (pDrvIns->Internal.s.pfnAsyncNotify)
1553 {
1554 pDrvIns->Internal.s.fVMSuspended = false;
1555 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance, pszDevName, iDevInstance, iLun);
1556 return false;
1557 }
1558 }
1559 }
1560 return true;
1561}
1562
1563
1564/**
1565 * Worker for PDMR3Suspend that deals with one USB device instance.
1566 *
1567 * @param pUsbIns The USB device instance.
1568 * @param pAsync The structure for recording asynchronous
1569 * notification tasks.
1570 */
1571DECLINLINE(void) pdmR3SuspendUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
1572{
1573 if (!pUsbIns->Internal.s.fVMSuspended)
1574 {
1575 pUsbIns->Internal.s.fVMSuspended = true;
1576 if (pUsbIns->pReg->pfnVMSuspend)
1577 {
1578 uint64_t cNsElapsed = RTTimeNanoTS();
1579
1580 if (!pUsbIns->Internal.s.pfnAsyncNotify)
1581 {
1582 LogFlow(("PDMR3Suspend: Notifying - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1583 pUsbIns->pReg->pfnVMSuspend(pUsbIns);
1584 if (pUsbIns->Internal.s.pfnAsyncNotify)
1585 LogFlow(("PDMR3Suspend: Async notification started - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1586 }
1587 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
1588 {
1589 LogFlow(("PDMR3Suspend: Async notification completed - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1590 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
1591 }
1592 if (pUsbIns->Internal.s.pfnAsyncNotify)
1593 {
1594 pUsbIns->Internal.s.fVMSuspended = false;
1595 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
1596 }
1597
1598 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1599 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1600 LogRel(("PDMR3Suspend: USB device '%s'/%d took %'llu ns to suspend\n",
1601 pUsbIns->pReg->szName, pUsbIns->iInstance, cNsElapsed));
1602 }
1603 }
1604}
1605
1606
1607/**
1608 * Worker for PDMR3Suspend that deals with one device instance.
1609 *
1610 * @param pDevIns The device instance.
1611 * @param pAsync The structure for recording asynchronous
1612 * notification tasks.
1613 */
1614DECLINLINE(void) pdmR3SuspendDev(PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
1615{
1616 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED))
1617 {
1618 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_SUSPENDED;
1619 if (pDevIns->pReg->pfnSuspend)
1620 {
1621 uint64_t cNsElapsed = RTTimeNanoTS();
1622 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1623
1624 if (!pDevIns->Internal.s.pfnAsyncNotify)
1625 {
1626 LogFlow(("PDMR3Suspend: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1627 pDevIns->pReg->pfnSuspend(pDevIns);
1628 if (pDevIns->Internal.s.pfnAsyncNotify)
1629 LogFlow(("PDMR3Suspend: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1630 }
1631 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
1632 {
1633 LogFlow(("PDMR3Suspend: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1634 pDevIns->Internal.s.pfnAsyncNotify = NULL;
1635 }
1636 if (pDevIns->Internal.s.pfnAsyncNotify)
1637 {
1638 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
1639 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
1640 }
1641
1642 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1643 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1644 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1645 LogRel(("PDMR3Suspend: device '%s'/%d took %'llu ns to suspend\n",
1646 pDevIns->pReg->szName, pDevIns->iInstance, cNsElapsed));
1647 }
1648 }
1649}
1650
1651
1652/**
1653 * This function will notify all the devices and their attached drivers about
1654 * the VM now being suspended.
1655 *
1656 * @param pVM Pointer to the VM.
1657 * @thread EMT(0)
1658 */
1659VMMR3_INT_DECL(void) PDMR3Suspend(PVM pVM)
1660{
1661 LogFlow(("PDMR3Suspend:\n"));
1662 VM_ASSERT_EMT0(pVM);
1663 uint64_t cNsElapsed = RTTimeNanoTS();
1664
1665 /*
1666 * The outer loop repeats until there are no more async requests.
1667 *
1668 * Note! We depend on the suspended indicators to be in the desired state
1669 * and we do not reset them before starting because this allows
1670 * PDMR3PowerOn and PDMR3Resume to use PDMR3Suspend for cleaning up
1671 * on failure.
1672 */
1673 PDMNOTIFYASYNCSTATS Async;
1674 pdmR3NotifyAsyncInit(&Async, "PDMR3Suspend");
1675 for (;;)
1676 {
1677 pdmR3NotifyAsyncBeginLoop(&Async);
1678
1679 /*
1680 * Iterate thru the device instances and USB device instances,
1681 * processing the drivers associated with those.
1682 *
1683 * The attached drivers are normally processed first. Some devices
1684 * (like DevAHCI) though needs to be notified before the drivers so
1685 * that it doesn't kick off any new requests after the drivers stopped
1686 * taking any. (DrvVD changes to read-only in this particular case.)
1687 */
1688 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1689 {
1690 unsigned const cAsyncStart = Async.cAsync;
1691
1692 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION)
1693 pdmR3SuspendDev(pDevIns, &Async);
1694
1695 if (Async.cAsync == cAsyncStart)
1696 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1697 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1698 if (!pdmR3SuspendDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
1699 break;
1700
1701 if ( Async.cAsync == cAsyncStart
1702 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION))
1703 pdmR3SuspendDev(pDevIns, &Async);
1704 }
1705
1706#ifdef VBOX_WITH_USB
1707 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1708 {
1709 unsigned const cAsyncStart = Async.cAsync;
1710
1711 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1712 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1713 if (!pdmR3SuspendDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
1714 break;
1715
1716 if (Async.cAsync == cAsyncStart)
1717 pdmR3SuspendUsb(pUsbIns, &Async);
1718 }
1719#endif
1720 if (!Async.cAsync)
1721 break;
1722 pdmR3NotifyAsyncLog(&Async);
1723 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
1724 }
1725
1726 /*
1727 * Suspend all threads.
1728 */
1729 pdmR3ThreadSuspendAll(pVM);
1730
1731 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1732 LogRel(("PDMR3Suspend: %'llu ns run time\n", cNsElapsed));
1733}
1734
1735
1736/**
1737 * Worker for PDMR3Resume that deals with one driver.
1738 *
1739 * @param pDrvIns The driver instance.
1740 * @param pszDevName The parent device name.
1741 * @param iDevInstance The parent device instance number.
1742 * @param iLun The parent LUN number.
1743 */
1744DECLINLINE(int) pdmR3ResumeDrv(PPDMDRVINS pDrvIns, const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1745{
1746 Assert(pDrvIns->Internal.s.fVMSuspended);
1747 if (pDrvIns->pReg->pfnResume)
1748 {
1749 LogFlow(("PDMR3Resume: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1750 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1751 int rc = VINF_SUCCESS; pDrvIns->pReg->pfnResume(pDrvIns);
1752 if (RT_FAILURE(rc))
1753 {
1754 LogRel(("PDMR3Resume: driver '%s'/%d on LUN#%d of device '%s'/%d -> %Rrc\n",
1755 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, rc));
1756 return rc;
1757 }
1758 }
1759 pDrvIns->Internal.s.fVMSuspended = false;
1760 return VINF_SUCCESS;
1761}
1762
1763
1764/**
1765 * Worker for PDMR3Resume that deals with one USB device instance.
1766 *
1767 * @returns VBox status code.
1768 * @param pUsbIns The USB device instance.
1769 */
1770DECLINLINE(int) pdmR3ResumeUsb(PPDMUSBINS pUsbIns)
1771{
1772 Assert(pUsbIns->Internal.s.fVMSuspended);
1773 if (pUsbIns->pReg->pfnVMResume)
1774 {
1775 LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1776 int rc = VINF_SUCCESS; pUsbIns->pReg->pfnVMResume(pUsbIns);
1777 if (RT_FAILURE(rc))
1778 {
1779 LogRel(("PDMR3Resume: device '%s'/%d -> %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1780 return rc;
1781 }
1782 }
1783 pUsbIns->Internal.s.fVMSuspended = false;
1784 return VINF_SUCCESS;
1785}
1786
1787
1788/**
1789 * Worker for PDMR3Resume that deals with one device instance.
1790 *
1791 * @returns VBox status code.
1792 * @param pDevIns The device instance.
1793 */
1794DECLINLINE(int) pdmR3ResumeDev(PPDMDEVINS pDevIns)
1795{
1796 Assert(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED);
1797 if (pDevIns->pReg->pfnResume)
1798 {
1799 LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1800 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1801 int rc = VINF_SUCCESS; pDevIns->pReg->pfnResume(pDevIns);
1802 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1803 if (RT_FAILURE(rc))
1804 {
1805 LogRel(("PDMR3Resume: device '%s'/%d -> %Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
1806 return rc;
1807 }
1808 }
1809 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
1810 return VINF_SUCCESS;
1811}
1812
1813
1814/**
1815 * This function will notify all the devices and their
1816 * attached drivers about the VM now being resumed.
1817 *
1818 * @param pVM Pointer to the VM.
1819 */
1820VMMR3_INT_DECL(void) PDMR3Resume(PVM pVM)
1821{
1822 LogFlow(("PDMR3Resume:\n"));
1823
1824 /*
1825 * Iterate thru the device instances and USB device instances,
1826 * processing the drivers associated with those.
1827 */
1828 int rc = VINF_SUCCESS;
1829 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns && RT_SUCCESS(rc); pDevIns = pDevIns->Internal.s.pNextR3)
1830 {
1831 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1832 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1833 rc = pdmR3ResumeDrv(pDrvIns, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun);
1834 if (RT_SUCCESS(rc))
1835 rc = pdmR3ResumeDev(pDevIns);
1836 }
1837
1838#ifdef VBOX_WITH_USB
1839 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns && RT_SUCCESS(rc); pUsbIns = pUsbIns->Internal.s.pNext)
1840 {
1841 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1842 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1843 rc = pdmR3ResumeDrv(pDrvIns, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun);
1844 if (RT_SUCCESS(rc))
1845 rc = pdmR3ResumeUsb(pUsbIns);
1846 }
1847#endif
1848
1849 /*
1850 * Resume all threads.
1851 */
1852 if (RT_SUCCESS(rc))
1853 pdmR3ThreadResumeAll(pVM);
1854
1855 /*
1856 * Resume the block cache.
1857 */
1858 if (RT_SUCCESS(rc))
1859 pdmR3BlkCacheResume(pVM);
1860
1861 /*
1862 * On failure, clean up via PDMR3Suspend.
1863 */
1864 if (RT_FAILURE(rc))
1865 PDMR3Suspend(pVM);
1866
1867 LogFlow(("PDMR3Resume: returns %Rrc\n", rc));
1868 return /*rc*/;
1869}
1870
1871
1872/**
1873 * Worker for PDMR3PowerOff that deals with one driver.
1874 *
1875 * @param pDrvIns The driver instance.
1876 * @param pAsync The structure for recording asynchronous
1877 * notification tasks.
1878 * @param pszDevName The parent device name.
1879 * @param iDevInstance The parent device instance number.
1880 * @param iLun The parent LUN number.
1881 */
1882DECLINLINE(bool) pdmR3PowerOffDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
1883 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1884{
1885 if (!pDrvIns->Internal.s.fVMSuspended)
1886 {
1887 pDrvIns->Internal.s.fVMSuspended = true;
1888 if (pDrvIns->pReg->pfnPowerOff)
1889 {
1890 uint64_t cNsElapsed = RTTimeNanoTS();
1891
1892 if (!pDrvIns->Internal.s.pfnAsyncNotify)
1893 {
1894 LogFlow(("PDMR3PowerOff: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1895 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1896 pDrvIns->pReg->pfnPowerOff(pDrvIns);
1897 if (pDrvIns->Internal.s.pfnAsyncNotify)
1898 LogFlow(("PDMR3PowerOff: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1899 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1900 }
1901 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
1902 {
1903 LogFlow(("PDMR3PowerOff: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1904 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1905 pDrvIns->Internal.s.pfnAsyncNotify = NULL;
1906 }
1907
1908 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1909 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
1910 LogRel(("PDMR3PowerOff: Driver '%s'/%d on LUN#%d of device '%s'/%d took %'llu ns to power off\n",
1911 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, cNsElapsed));
1912
1913 if (pDrvIns->Internal.s.pfnAsyncNotify)
1914 {
1915 pDrvIns->Internal.s.fVMSuspended = false;
1916 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance,
1917 pszDevName, iDevInstance, iLun);
1918 return false;
1919 }
1920 }
1921 }
1922 return true;
1923}
1924
1925
1926/**
1927 * Worker for PDMR3PowerOff that deals with one USB device instance.
1928 *
1929 * @param pUsbIns The USB device instance.
1930 * @param pAsync The structure for recording asynchronous
1931 * notification tasks.
1932 */
1933DECLINLINE(void) pdmR3PowerOffUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
1934{
1935 if (!pUsbIns->Internal.s.fVMSuspended)
1936 {
1937 pUsbIns->Internal.s.fVMSuspended = true;
1938 if (pUsbIns->pReg->pfnVMPowerOff)
1939 {
1940 uint64_t cNsElapsed = RTTimeNanoTS();
1941
1942 if (!pUsbIns->Internal.s.pfnAsyncNotify)
1943 {
1944 LogFlow(("PDMR3PowerOff: Notifying - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1945 pUsbIns->pReg->pfnVMPowerOff(pUsbIns);
1946 if (pUsbIns->Internal.s.pfnAsyncNotify)
1947 LogFlow(("PDMR3PowerOff: Async notification started - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1948 }
1949 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
1950 {
1951 LogFlow(("PDMR3PowerOff: Async notification completed - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1952 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
1953 }
1954 if (pUsbIns->Internal.s.pfnAsyncNotify)
1955 {
1956 pUsbIns->Internal.s.fVMSuspended = false;
1957 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
1958 }
1959
1960 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1961 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
1962 LogRel(("PDMR3PowerOff: USB device '%s'/%d took %'llu ns to power off\n",
1963 pUsbIns->pReg->szName, pUsbIns->iInstance, cNsElapsed));
1964
1965 }
1966 }
1967}
1968
1969
1970/**
1971 * Worker for PDMR3PowerOff that deals with one device instance.
1972 *
1973 * @param pDevIns The device instance.
1974 * @param pAsync The structure for recording asynchronous
1975 * notification tasks.
1976 */
1977DECLINLINE(void) pdmR3PowerOffDev(PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
1978{
1979 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED))
1980 {
1981 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_SUSPENDED;
1982 if (pDevIns->pReg->pfnPowerOff)
1983 {
1984 uint64_t cNsElapsed = RTTimeNanoTS();
1985 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1986
1987 if (!pDevIns->Internal.s.pfnAsyncNotify)
1988 {
1989 LogFlow(("PDMR3PowerOff: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1990 pDevIns->pReg->pfnPowerOff(pDevIns);
1991 if (pDevIns->Internal.s.pfnAsyncNotify)
1992 LogFlow(("PDMR3PowerOff: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1993 }
1994 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
1995 {
1996 LogFlow(("PDMR3PowerOff: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1997 pDevIns->Internal.s.pfnAsyncNotify = NULL;
1998 }
1999 if (pDevIns->Internal.s.pfnAsyncNotify)
2000 {
2001 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
2002 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
2003 }
2004
2005 PDMCritSectLeave(pDevIns->pCritSectRoR3);
2006 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2007 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
2008 LogFlow(("PDMR3PowerOff: Device '%s'/%d took %'llu ns to power off\n",
2009 pDevIns->pReg->szName, pDevIns->iInstance, cNsElapsed));
2010 }
2011 }
2012}
2013
2014
2015/**
2016 * This function will notify all the devices and their
2017 * attached drivers about the VM being powered off.
2018 *
2019 * @param pVM Pointer to the VM.
2020 */
2021VMMR3DECL(void) PDMR3PowerOff(PVM pVM)
2022{
2023 LogFlow(("PDMR3PowerOff:\n"));
2024 uint64_t cNsElapsed = RTTimeNanoTS();
2025
2026 /*
2027 * The outer loop repeats until there are no more async requests.
2028 */
2029 PDMNOTIFYASYNCSTATS Async;
2030 pdmR3NotifyAsyncInit(&Async, "PDMR3PowerOff");
2031 for (;;)
2032 {
2033 pdmR3NotifyAsyncBeginLoop(&Async);
2034
2035 /*
2036 * Iterate thru the device instances and USB device instances,
2037 * processing the drivers associated with those.
2038 *
2039 * The attached drivers are normally processed first. Some devices
2040 * (like DevAHCI) though needs to be notified before the drivers so
2041 * that it doesn't kick off any new requests after the drivers stopped
2042 * taking any. (DrvVD changes to read-only in this particular case.)
2043 */
2044 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2045 {
2046 unsigned const cAsyncStart = Async.cAsync;
2047
2048 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION)
2049 pdmR3PowerOffDev(pDevIns, &Async);
2050
2051 if (Async.cAsync == cAsyncStart)
2052 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2053 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2054 if (!pdmR3PowerOffDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
2055 break;
2056
2057 if ( Async.cAsync == cAsyncStart
2058 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION))
2059 pdmR3PowerOffDev(pDevIns, &Async);
2060 }
2061
2062#ifdef VBOX_WITH_USB
2063 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2064 {
2065 unsigned const cAsyncStart = Async.cAsync;
2066
2067 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2068 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2069 if (!pdmR3PowerOffDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
2070 break;
2071
2072 if (Async.cAsync == cAsyncStart)
2073 pdmR3PowerOffUsb(pUsbIns, &Async);
2074 }
2075#endif
2076 if (!Async.cAsync)
2077 break;
2078 pdmR3NotifyAsyncLog(&Async);
2079 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
2080 }
2081
2082 /*
2083 * Suspend all threads.
2084 */
2085 pdmR3ThreadSuspendAll(pVM);
2086
2087 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2088 LogRel(("PDMR3PowerOff: %'llu ns run time\n", cNsElapsed));
2089}
2090
2091
2092/**
2093 * Queries the base interface of a device instance.
2094 *
2095 * The caller can use this to query other interfaces the device implements
2096 * and use them to talk to the device.
2097 *
2098 * @returns VBox status code.
2099 * @param pUVM The user mode VM handle.
2100 * @param pszDevice Device name.
2101 * @param iInstance Device instance.
2102 * @param ppBase Where to store the pointer to the base device interface on success.
2103 * @remark We're not doing any locking ATM, so don't try call this at times when the
2104 * device chain is known to be updated.
2105 */
2106VMMR3DECL(int) PDMR3QueryDevice(PUVM pUVM, const char *pszDevice, unsigned iInstance, PPDMIBASE *ppBase)
2107{
2108 LogFlow(("PDMR3DeviceQuery: pszDevice=%p:{%s} iInstance=%u ppBase=%p\n", pszDevice, pszDevice, iInstance, ppBase));
2109 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2110 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2111
2112 /*
2113 * Iterate registered devices looking for the device.
2114 */
2115 size_t cchDevice = strlen(pszDevice);
2116 for (PPDMDEV pDev = pUVM->pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
2117 {
2118 if ( pDev->cchName == cchDevice
2119 && !memcmp(pDev->pReg->szName, pszDevice, cchDevice))
2120 {
2121 /*
2122 * Iterate device instances.
2123 */
2124 for (PPDMDEVINS pDevIns = pDev->pInstances; pDevIns; pDevIns = pDevIns->Internal.s.pPerDeviceNextR3)
2125 {
2126 if (pDevIns->iInstance == iInstance)
2127 {
2128 if (pDevIns->IBase.pfnQueryInterface)
2129 {
2130 *ppBase = &pDevIns->IBase;
2131 LogFlow(("PDMR3DeviceQuery: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
2132 return VINF_SUCCESS;
2133 }
2134
2135 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NO_IBASE\n"));
2136 return VERR_PDM_DEVICE_INSTANCE_NO_IBASE;
2137 }
2138 }
2139
2140 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NOT_FOUND\n"));
2141 return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND;
2142 }
2143 }
2144
2145 LogFlow(("PDMR3QueryDevice: returns VERR_PDM_DEVICE_NOT_FOUND\n"));
2146 return VERR_PDM_DEVICE_NOT_FOUND;
2147}
2148
2149
2150/**
2151 * Queries the base interface of a device LUN.
2152 *
2153 * This differs from PDMR3QueryLun by that it returns the interface on the
2154 * device and not the top level driver.
2155 *
2156 * @returns VBox status code.
2157 * @param pUVM The user mode VM handle.
2158 * @param pszDevice Device name.
2159 * @param iInstance Device instance.
2160 * @param iLun The Logical Unit to obtain the interface of.
2161 * @param ppBase Where to store the base interface pointer.
2162 * @remark We're not doing any locking ATM, so don't try call this at times when the
2163 * device chain is known to be updated.
2164 */
2165VMMR3DECL(int) PDMR3QueryDeviceLun(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
2166{
2167 LogFlow(("PDMR3QueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
2168 pszDevice, pszDevice, iInstance, iLun, ppBase));
2169 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2170 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2171
2172 /*
2173 * Find the LUN.
2174 */
2175 PPDMLUN pLun;
2176 int rc = pdmR3DevFindLun(pUVM->pVM, pszDevice, iInstance, iLun, &pLun);
2177 if (RT_SUCCESS(rc))
2178 {
2179 *ppBase = pLun->pBase;
2180 LogFlow(("PDMR3QueryDeviceLun: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
2181 return VINF_SUCCESS;
2182 }
2183 LogFlow(("PDMR3QueryDeviceLun: returns %Rrc\n", rc));
2184 return rc;
2185}
2186
2187
2188/**
2189 * Query the interface of the top level driver on a LUN.
2190 *
2191 * @returns VBox status code.
2192 * @param pUVM The user mode VM handle.
2193 * @param pszDevice Device name.
2194 * @param iInstance Device instance.
2195 * @param iLun The Logical Unit to obtain the interface of.
2196 * @param ppBase Where to store the base interface pointer.
2197 * @remark We're not doing any locking ATM, so don't try call this at times when the
2198 * device chain is known to be updated.
2199 */
2200VMMR3DECL(int) PDMR3QueryLun(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
2201{
2202 LogFlow(("PDMR3QueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
2203 pszDevice, pszDevice, iInstance, iLun, ppBase));
2204 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2205 PVM pVM = pUVM->pVM;
2206 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
2207
2208 /*
2209 * Find the LUN.
2210 */
2211 PPDMLUN pLun;
2212 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
2213 if (RT_SUCCESS(rc))
2214 {
2215 if (pLun->pTop)
2216 {
2217 *ppBase = &pLun->pTop->IBase;
2218 LogFlow(("PDMR3QueryLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
2219 return VINF_SUCCESS;
2220 }
2221 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
2222 }
2223 LogFlow(("PDMR3QueryLun: returns %Rrc\n", rc));
2224 return rc;
2225}
2226
2227
2228/**
2229 * Query the interface of a named driver on a LUN.
2230 *
2231 * If the driver appears more than once in the driver chain, the first instance
2232 * is returned.
2233 *
2234 * @returns VBox status code.
2235 * @param pUVM The user mode VM handle.
2236 * @param pszDevice Device name.
2237 * @param iInstance Device instance.
2238 * @param iLun The Logical Unit to obtain the interface of.
2239 * @param pszDriver The driver name.
2240 * @param ppBase Where to store the base interface pointer.
2241 *
2242 * @remark We're not doing any locking ATM, so don't try call this at times when the
2243 * device chain is known to be updated.
2244 */
2245VMMR3DECL(int) PDMR3QueryDriverOnLun(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, const char *pszDriver, PPPDMIBASE ppBase)
2246{
2247 LogFlow(("PDMR3QueryDriverOnLun: pszDevice=%p:{%s} iInstance=%u iLun=%u pszDriver=%p:{%s} ppBase=%p\n",
2248 pszDevice, pszDevice, iInstance, iLun, pszDriver, pszDriver, ppBase));
2249 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2250 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2251
2252 /*
2253 * Find the LUN.
2254 */
2255 PPDMLUN pLun;
2256 int rc = pdmR3DevFindLun(pUVM->pVM, pszDevice, iInstance, iLun, &pLun);
2257 if (RT_SUCCESS(rc))
2258 {
2259 if (pLun->pTop)
2260 {
2261 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2262 if (!strcmp(pDrvIns->pReg->szName, pszDriver))
2263 {
2264 *ppBase = &pDrvIns->IBase;
2265 LogFlow(("PDMR3QueryDriverOnLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
2266 return VINF_SUCCESS;
2267
2268 }
2269 rc = VERR_PDM_DRIVER_NOT_FOUND;
2270 }
2271 else
2272 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
2273 }
2274 LogFlow(("PDMR3QueryDriverOnLun: returns %Rrc\n", rc));
2275 return rc;
2276}
2277
2278/**
2279 * Executes pending DMA transfers.
2280 * Forced Action handler.
2281 *
2282 * @param pVM Pointer to the VM.
2283 */
2284VMMR3DECL(void) PDMR3DmaRun(PVM pVM)
2285{
2286 /* Note! Not really SMP safe; restrict it to VCPU 0. */
2287 if (VMMGetCpuId(pVM) != 0)
2288 return;
2289
2290 if (VM_FF_TESTANDCLEAR(pVM, VM_FF_PDM_DMA))
2291 {
2292 if (pVM->pdm.s.pDmac)
2293 {
2294 bool fMore = pVM->pdm.s.pDmac->Reg.pfnRun(pVM->pdm.s.pDmac->pDevIns);
2295 if (fMore)
2296 VM_FF_SET(pVM, VM_FF_PDM_DMA);
2297 }
2298 }
2299}
2300
2301
2302/**
2303 * Service a VMMCALLRING3_PDM_LOCK call.
2304 *
2305 * @returns VBox status code.
2306 * @param pVM Pointer to the VM.
2307 */
2308VMMR3_INT_DECL(int) PDMR3LockCall(PVM pVM)
2309{
2310 return PDMR3CritSectEnterEx(&pVM->pdm.s.CritSect, true /* fHostCall */);
2311}
2312
2313
2314/**
2315 * Registers the VMM device heap
2316 *
2317 * @returns VBox status code.
2318 * @param pVM Pointer to the VM.
2319 * @param GCPhys The physical address.
2320 * @param pvHeap Ring-3 pointer.
2321 * @param cbSize Size of the heap.
2322 */
2323VMMR3_INT_DECL(int) PDMR3VmmDevHeapRegister(PVM pVM, RTGCPHYS GCPhys, RTR3PTR pvHeap, unsigned cbSize)
2324{
2325 Assert(pVM->pdm.s.pvVMMDevHeap == NULL);
2326
2327 Log(("PDMR3VmmDevHeapRegister %RGp %RHv %x\n", GCPhys, pvHeap, cbSize));
2328 pVM->pdm.s.pvVMMDevHeap = pvHeap;
2329 pVM->pdm.s.GCPhysVMMDevHeap = GCPhys;
2330 pVM->pdm.s.cbVMMDevHeap = cbSize;
2331 pVM->pdm.s.cbVMMDevHeapLeft = cbSize;
2332 return VINF_SUCCESS;
2333}
2334
2335
2336/**
2337 * Unregisters the VMM device heap
2338 *
2339 * @returns VBox status code.
2340 * @param pVM Pointer to the VM.
2341 * @param GCPhys The physical address.
2342 */
2343VMMR3_INT_DECL(int) PDMR3VmmDevHeapUnregister(PVM pVM, RTGCPHYS GCPhys)
2344{
2345 Assert(pVM->pdm.s.GCPhysVMMDevHeap == GCPhys);
2346
2347 Log(("PDMR3VmmDevHeapUnregister %RGp\n", GCPhys));
2348 pVM->pdm.s.pvVMMDevHeap = NULL;
2349 pVM->pdm.s.GCPhysVMMDevHeap = NIL_RTGCPHYS;
2350 pVM->pdm.s.cbVMMDevHeap = 0;
2351 pVM->pdm.s.cbVMMDevHeapLeft = 0;
2352 return VINF_SUCCESS;
2353}
2354
2355
2356/**
2357 * Allocates memory from the VMM device heap
2358 *
2359 * @returns VBox status code.
2360 * @param pVM Pointer to the VM.
2361 * @param cbSize Allocation size.
2362 * @param pv Ring-3 pointer. (out)
2363 */
2364VMMR3_INT_DECL(int) PDMR3VmmDevHeapAlloc(PVM pVM, size_t cbSize, RTR3PTR *ppv)
2365{
2366#ifdef DEBUG_bird
2367 if (!cbSize || cbSize > pVM->pdm.s.cbVMMDevHeapLeft)
2368 return VERR_NO_MEMORY;
2369#else
2370 AssertReturn(cbSize && cbSize <= pVM->pdm.s.cbVMMDevHeapLeft, VERR_NO_MEMORY);
2371#endif
2372
2373 Log(("PDMR3VMMDevHeapAlloc: %#zx\n", cbSize));
2374
2375 /** @todo Not a real heap as there's currently only one user. */
2376 *ppv = pVM->pdm.s.pvVMMDevHeap;
2377 pVM->pdm.s.cbVMMDevHeapLeft = 0;
2378 return VINF_SUCCESS;
2379}
2380
2381
2382/**
2383 * Frees memory from the VMM device heap
2384 *
2385 * @returns VBox status code.
2386 * @param pVM Pointer to the VM.
2387 * @param pv Ring-3 pointer.
2388 */
2389VMMR3_INT_DECL(int) PDMR3VmmDevHeapFree(PVM pVM, RTR3PTR pv)
2390{
2391 Log(("PDMR3VmmDevHeapFree: %RHv\n", pv));
2392
2393 /** @todo not a real heap as there's currently only one user. */
2394 pVM->pdm.s.cbVMMDevHeapLeft = pVM->pdm.s.cbVMMDevHeap;
2395 return VINF_SUCCESS;
2396}
2397
2398
2399/**
2400 * Worker for DBGFR3TraceConfig that checks if the given tracing group name
2401 * matches a device or driver name and applies the tracing config change.
2402 *
2403 * @returns VINF_SUCCESS or VERR_NOT_FOUND.
2404 * @param pVM Pointer to the VM.
2405 * @param pszName The tracing config group name. This is NULL if
2406 * the operation applies to every device and
2407 * driver.
2408 * @param cchName The length to match.
2409 * @param fEnable Whether to enable or disable the corresponding
2410 * trace points.
2411 * @param fApply Whether to actually apply the changes or just do
2412 * existence checks.
2413 */
2414VMMR3_INT_DECL(int) PDMR3TracingConfig(PVM pVM, const char *pszName, size_t cchName, bool fEnable, bool fApply)
2415{
2416 /** @todo This code is potentially racing driver attaching and detaching. */
2417
2418 /*
2419 * Applies to all.
2420 */
2421 if (pszName == NULL)
2422 {
2423 AssertReturn(fApply, VINF_SUCCESS);
2424
2425 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2426 {
2427 pDevIns->fTracing = fEnable;
2428 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2429 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2430 pDrvIns->fTracing = fEnable;
2431 }
2432
2433#ifdef VBOX_WITH_USB
2434 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2435 {
2436 pUsbIns->fTracing = fEnable;
2437 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2438 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2439 pDrvIns->fTracing = fEnable;
2440
2441 }
2442#endif
2443 return VINF_SUCCESS;
2444 }
2445
2446 /*
2447 * Specific devices, USB devices or drivers.
2448 * Decode prefix to figure which of these it applies to.
2449 */
2450 if (cchName <= 3)
2451 return VERR_NOT_FOUND;
2452
2453 uint32_t cMatches = 0;
2454 if (!strncmp("dev", pszName, 3))
2455 {
2456 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2457 {
2458 const char *pszDevName = pDevIns->Internal.s.pDevR3->pReg->szName;
2459 size_t cchDevName = strlen(pszDevName);
2460 if ( ( cchDevName == cchName
2461 && RTStrNICmp(pszName, pszDevName, cchDevName))
2462 || ( cchDevName == cchName - 3
2463 && RTStrNICmp(pszName + 3, pszDevName, cchDevName)) )
2464 {
2465 cMatches++;
2466 if (fApply)
2467 pDevIns->fTracing = fEnable;
2468 }
2469 }
2470 }
2471 else if (!strncmp("usb", pszName, 3))
2472 {
2473 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2474 {
2475 const char *pszUsbName = pUsbIns->Internal.s.pUsbDev->pReg->szName;
2476 size_t cchUsbName = strlen(pszUsbName);
2477 if ( ( cchUsbName == cchName
2478 && RTStrNICmp(pszName, pszUsbName, cchUsbName))
2479 || ( cchUsbName == cchName - 3
2480 && RTStrNICmp(pszName + 3, pszUsbName, cchUsbName)) )
2481 {
2482 cMatches++;
2483 if (fApply)
2484 pUsbIns->fTracing = fEnable;
2485 }
2486 }
2487 }
2488 else if (!strncmp("drv", pszName, 3))
2489 {
2490 AssertReturn(fApply, VINF_SUCCESS);
2491
2492 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2493 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2494 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2495 {
2496 const char *pszDrvName = pDrvIns->Internal.s.pDrv->pReg->szName;
2497 size_t cchDrvName = strlen(pszDrvName);
2498 if ( ( cchDrvName == cchName
2499 && RTStrNICmp(pszName, pszDrvName, cchDrvName))
2500 || ( cchDrvName == cchName - 3
2501 && RTStrNICmp(pszName + 3, pszDrvName, cchDrvName)) )
2502 {
2503 cMatches++;
2504 if (fApply)
2505 pDrvIns->fTracing = fEnable;
2506 }
2507 }
2508
2509#ifdef VBOX_WITH_USB
2510 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2511 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2512 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2513 {
2514 const char *pszDrvName = pDrvIns->Internal.s.pDrv->pReg->szName;
2515 size_t cchDrvName = strlen(pszDrvName);
2516 if ( ( cchDrvName == cchName
2517 && RTStrNICmp(pszName, pszDrvName, cchDrvName))
2518 || ( cchDrvName == cchName - 3
2519 && RTStrNICmp(pszName + 3, pszDrvName, cchDrvName)) )
2520 {
2521 cMatches++;
2522 if (fApply)
2523 pDrvIns->fTracing = fEnable;
2524 }
2525 }
2526#endif
2527 }
2528 else
2529 return VERR_NOT_FOUND;
2530
2531 return cMatches > 0 ? VINF_SUCCESS : VERR_NOT_FOUND;
2532}
2533
2534
2535/**
2536 * Worker for DBGFR3TraceQueryConfig that checks whether all drivers, devices,
2537 * and USB device have the same tracing settings.
2538 *
2539 * @returns true / false.
2540 * @param pVM Pointer to the VM.
2541 * @param fEnabled The tracing setting to check for.
2542 */
2543VMMR3_INT_DECL(bool) PDMR3TracingAreAll(PVM pVM, bool fEnabled)
2544{
2545 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2546 {
2547 if (pDevIns->fTracing != (uint32_t)fEnabled)
2548 return false;
2549
2550 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2551 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2552 if (pDrvIns->fTracing != (uint32_t)fEnabled)
2553 return false;
2554 }
2555
2556#ifdef VBOX_WITH_USB
2557 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2558 {
2559 if (pUsbIns->fTracing != (uint32_t)fEnabled)
2560 return false;
2561
2562 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2563 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2564 if (pDrvIns->fTracing != (uint32_t)fEnabled)
2565 return false;
2566 }
2567#endif
2568
2569 return true;
2570}
2571
2572
2573/**
2574 * Worker for PDMR3TracingQueryConfig that adds a prefixed name to the output
2575 * string.
2576 *
2577 * @returns VINF_SUCCESS or VERR_BUFFER_OVERFLOW
2578 * @param ppszDst The pointer to the output buffer pointer.
2579 * @param pcbDst The pointer to the output buffer size.
2580 * @param fSpace Whether to add a space before the name.
2581 * @param pszPrefix The name prefix.
2582 * @param pszName The name.
2583 */
2584static int pdmR3TracingAdd(char **ppszDst, size_t *pcbDst, bool fSpace, const char *pszPrefix, const char *pszName)
2585{
2586 size_t const cchPrefix = strlen(pszPrefix);
2587 if (!RTStrNICmp(pszPrefix, pszName, cchPrefix))
2588 pszName += cchPrefix;
2589 size_t const cchName = strlen(pszName);
2590
2591 size_t const cchThis = cchName + cchPrefix + fSpace;
2592 if (cchThis >= *pcbDst)
2593 return VERR_BUFFER_OVERFLOW;
2594 if (fSpace)
2595 {
2596 **ppszDst = ' ';
2597 memcpy(*ppszDst + 1, pszPrefix, cchPrefix);
2598 memcpy(*ppszDst + 1 + cchPrefix, pszName, cchName + 1);
2599 }
2600 else
2601 {
2602 memcpy(*ppszDst, pszPrefix, cchPrefix);
2603 memcpy(*ppszDst + cchPrefix, pszName, cchName + 1);
2604 }
2605 *ppszDst += cchThis;
2606 *pcbDst -= cchThis;
2607 return VINF_SUCCESS;
2608}
2609
2610
2611/**
2612 * Worker for DBGFR3TraceQueryConfig use when not everything is either enabled
2613 * or disabled.
2614 *
2615 * @returns VINF_SUCCESS or VERR_BUFFER_OVERFLOW
2616 * @param pVM Pointer to the VM.
2617 * @param pszConfig Where to store the config spec.
2618 * @param cbConfig The size of the output buffer.
2619 */
2620VMMR3_INT_DECL(int) PDMR3TracingQueryConfig(PVM pVM, char *pszConfig, size_t cbConfig)
2621{
2622 int rc;
2623 char *pszDst = pszConfig;
2624 size_t cbDst = cbConfig;
2625
2626 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2627 {
2628 if (pDevIns->fTracing)
2629 {
2630 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "dev", pDevIns->Internal.s.pDevR3->pReg->szName);
2631 if (RT_FAILURE(rc))
2632 return rc;
2633 }
2634
2635 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2636 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2637 if (pDrvIns->fTracing)
2638 {
2639 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "drv", pDrvIns->Internal.s.pDrv->pReg->szName);
2640 if (RT_FAILURE(rc))
2641 return rc;
2642 }
2643 }
2644
2645#ifdef VBOX_WITH_USB
2646 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2647 {
2648 if (pUsbIns->fTracing)
2649 {
2650 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "usb", pUsbIns->Internal.s.pUsbDev->pReg->szName);
2651 if (RT_FAILURE(rc))
2652 return rc;
2653 }
2654
2655 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2656 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2657 if (pDrvIns->fTracing)
2658 {
2659 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "drv", pDrvIns->Internal.s.pDrv->pReg->szName);
2660 if (RT_FAILURE(rc))
2661 return rc;
2662 }
2663 }
2664#endif
2665
2666 return VINF_SUCCESS;
2667}
2668
2669
2670/**
2671 * Checks that a PDMDRVREG::szName, PDMDEVREG::szName or PDMUSBREG::szName
2672 * field contains only a limited set of ASCII characters.
2673 *
2674 * @returns true / false.
2675 * @param pszName The name to validate.
2676 */
2677bool pdmR3IsValidName(const char *pszName)
2678{
2679 char ch;
2680 while ( (ch = *pszName) != '\0'
2681 && ( RT_C_IS_ALNUM(ch)
2682 || ch == '-'
2683 || ch == ' ' /** @todo disallow this! */
2684 || ch == '_') )
2685 pszName++;
2686 return ch == '\0';
2687}
2688
2689
2690/**
2691 * Info handler for 'pdmtracingids'.
2692 *
2693 * @param pVM Pointer to the VM.
2694 * @param pHlp The output helpers.
2695 * @param pszArgs The optional user arguments.
2696 *
2697 * @remarks Can be called on most threads.
2698 */
2699static DECLCALLBACK(void) pdmR3InfoTracingIds(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2700{
2701 /*
2702 * Parse the argument (optional).
2703 */
2704 if ( pszArgs
2705 && *pszArgs
2706 && strcmp(pszArgs, "all")
2707 && strcmp(pszArgs, "devices")
2708 && strcmp(pszArgs, "drivers")
2709 && strcmp(pszArgs, "usb"))
2710 {
2711 pHlp->pfnPrintf(pHlp, "Unable to grok '%s'\n", pszArgs);
2712 return;
2713 }
2714 bool fAll = !pszArgs || !*pszArgs || !strcmp(pszArgs, "all");
2715 bool fDevices = fAll || !strcmp(pszArgs, "devices");
2716 bool fUsbDevs = fAll || !strcmp(pszArgs, "usb");
2717 bool fDrivers = fAll || !strcmp(pszArgs, "drivers");
2718
2719 /*
2720 * Produce the requested output.
2721 */
2722/** @todo lock PDM lists! */
2723 /* devices */
2724 if (fDevices)
2725 {
2726 pHlp->pfnPrintf(pHlp, "Device tracing IDs:\n");
2727 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2728 pHlp->pfnPrintf(pHlp, "%05u %s\n", pDevIns->idTracing, pDevIns->Internal.s.pDevR3->pReg->szName);
2729 }
2730
2731 /* USB devices */
2732 if (fUsbDevs)
2733 {
2734 pHlp->pfnPrintf(pHlp, "USB device tracing IDs:\n");
2735 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2736 pHlp->pfnPrintf(pHlp, "%05u %s\n", pUsbIns->idTracing, pUsbIns->Internal.s.pUsbDev->pReg->szName);
2737 }
2738
2739 /* Drivers */
2740 if (fDrivers)
2741 {
2742 pHlp->pfnPrintf(pHlp, "Driver tracing IDs:\n");
2743 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2744 {
2745 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2746 {
2747 uint32_t iLevel = 0;
2748 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown, iLevel++)
2749 pHlp->pfnPrintf(pHlp, "%05u %s (level %u, lun %u, dev %s)\n",
2750 pDrvIns->idTracing, pDrvIns->Internal.s.pDrv->pReg->szName,
2751 iLevel, pLun->iLun, pDevIns->Internal.s.pDevR3->pReg->szName);
2752 }
2753 }
2754
2755 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2756 {
2757 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2758 {
2759 uint32_t iLevel = 0;
2760 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown, iLevel++)
2761 pHlp->pfnPrintf(pHlp, "%05u %s (level %u, lun %u, dev %s)\n",
2762 pDrvIns->idTracing, pDrvIns->Internal.s.pDrv->pReg->szName,
2763 iLevel, pLun->iLun, pUsbIns->Internal.s.pUsbDev->pReg->szName);
2764 }
2765 }
2766 }
2767}
2768
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