VirtualBox

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

Last change on this file since 58251 was 58126, checked in by vboxsync, 9 years ago

VMM: Fixed almost all the Doxygen warnings.

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