VirtualBox

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

Last change on this file since 39669 was 39653, checked in by vboxsync, 13 years ago

PDM.cpp: another one

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