VirtualBox

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

Last change on this file since 40026 was 39839, checked in by vboxsync, 13 years ago

PDM: Initial driver chain transformation code (untested).

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