VirtualBox

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

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

VMM: Made @param pVCpu more uniform and to the point.

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