VirtualBox

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

Last change on this file since 37447 was 37443, checked in by vboxsync, 14 years ago

PDM: Implemented the NOP critical section.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 81.9 KB
Line 
1/* $Id: PDM.cpp 37443 2011-06-14 14:34:11Z 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
282/*******************************************************************************
283* Structures and Typedefs *
284*******************************************************************************/
285/**
286 * Statistics of asynchronous notification tasks - used by reset, suspend and
287 * power off.
288 */
289typedef struct PDMNOTIFYASYNCSTATS
290{
291 /** The the start timestamp. */
292 uint64_t uStartNsTs;
293 /** When to log the next time. */
294 uint64_t cNsElapsedNextLog;
295 /** The loop counter. */
296 uint32_t cLoops;
297 /** The number of pending asynchronous notification tasks. */
298 uint32_t cAsync;
299 /** The name of the operation (log prefix). */
300 const char *pszOp;
301 /** The current list buffer position. */
302 size_t offList;
303 /** String containing a list of the pending tasks. */
304 char szList[1024];
305} PDMNOTIFYASYNCSTATS;
306/** Pointer to the stats of pending asynchronous notification tasks. */
307typedef PDMNOTIFYASYNCSTATS *PPDMNOTIFYASYNCSTATS;
308
309
310/*******************************************************************************
311* Internal Functions *
312*******************************************************************************/
313static DECLCALLBACK(int) pdmR3LiveExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uPass);
314static DECLCALLBACK(int) pdmR3SaveExec(PVM pVM, PSSMHANDLE pSSM);
315static DECLCALLBACK(int) pdmR3LoadExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
316static DECLCALLBACK(int) pdmR3LoadPrep(PVM pVM, PSSMHANDLE pSSM);
317
318
319
320/**
321 * Initializes the PDM part of the UVM.
322 *
323 * This doesn't really do much right now but has to be here for the sake
324 * of completeness.
325 *
326 * @returns VBox status code.
327 * @param pUVM Pointer to the user mode VM structure.
328 */
329VMMR3DECL(int) PDMR3InitUVM(PUVM pUVM)
330{
331 AssertCompile(sizeof(pUVM->pdm.s) <= sizeof(pUVM->pdm.padding));
332 AssertRelease(sizeof(pUVM->pdm.s) <= sizeof(pUVM->pdm.padding));
333 pUVM->pdm.s.pModules = NULL;
334 pUVM->pdm.s.pCritSects = NULL;
335 return RTCritSectInit(&pUVM->pdm.s.ListCritSect);
336}
337
338
339/**
340 * Initializes the PDM.
341 *
342 * @returns VBox status code.
343 * @param pVM The VM to operate on.
344 */
345VMMR3DECL(int) PDMR3Init(PVM pVM)
346{
347 LogFlow(("PDMR3Init\n"));
348
349 /*
350 * Assert alignment and sizes.
351 */
352 AssertRelease(!(RT_OFFSETOF(VM, pdm.s) & 31));
353 AssertRelease(sizeof(pVM->pdm.s) <= sizeof(pVM->pdm.padding));
354 AssertCompileMemberAlignment(PDM, CritSect, sizeof(uintptr_t));
355
356 /*
357 * Init the structure.
358 */
359 pVM->pdm.s.GCPhysVMMDevHeap = NIL_RTGCPHYS;
360
361 /*
362 * Initialize critical sections first.
363 */
364 int rc = pdmR3CritSectInitStats(pVM);
365 if (RT_SUCCESS(rc))
366 rc = PDMR3CritSectInit(pVM, &pVM->pdm.s.CritSect, RT_SRC_POS, "PDM");
367 if (RT_SUCCESS(rc))
368 rc = PDMR3CritSectInit(pVM, &pVM->pdm.s.GiantDevCritSect, RT_SRC_POS, "Giant PDM Dev");
369 if (RT_SUCCESS(rc))
370 {
371 rc = PDMR3CritSectInit(pVM, &pVM->pdm.s.NopCritSect, RT_SRC_POS, "NOP");
372 if (RT_SUCCESS(rc))
373 pVM->pdm.s.NopCritSect.s.Core.fFlags |= RTCRITSECT_FLAGS_NOP;
374 }
375
376 /*
377 * Initialize sub components.
378 */
379 if (RT_SUCCESS(rc))
380 rc = pdmR3LdrInitU(pVM->pUVM);
381#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
382 if (RT_SUCCESS(rc))
383 rc = pdmR3AsyncCompletionInit(pVM);
384#endif
385 if (RT_SUCCESS(rc))
386 rc = pdmR3BlkCacheInit(pVM);
387 if (RT_SUCCESS(rc))
388 rc = pdmR3DrvInit(pVM);
389 if (RT_SUCCESS(rc))
390 rc = pdmR3DevInit(pVM);
391 if (RT_SUCCESS(rc))
392 {
393 /*
394 * Register the saved state data unit.
395 */
396 rc = SSMR3RegisterInternal(pVM, "pdm", 1, PDM_SAVED_STATE_VERSION, 128,
397 NULL, pdmR3LiveExec, NULL,
398 NULL, pdmR3SaveExec, NULL,
399 pdmR3LoadPrep, pdmR3LoadExec, NULL);
400 if (RT_SUCCESS(rc))
401 {
402 LogFlow(("PDM: Successfully initialized\n"));
403 return rc;
404 }
405 }
406
407 /*
408 * Cleanup and return failure.
409 */
410 PDMR3Term(pVM);
411 LogFlow(("PDMR3Init: returns %Rrc\n", rc));
412 return rc;
413}
414
415
416/**
417 * Applies relocations to data and code managed by this
418 * component. This function will be called at init and
419 * whenever the VMM need to relocate it self inside the GC.
420 *
421 * @param pVM VM handle.
422 * @param offDelta Relocation delta relative to old location.
423 * @remark The loader subcomponent is relocated by PDMR3LdrRelocate() very
424 * early in the relocation phase.
425 */
426VMMR3DECL(void) PDMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
427{
428 LogFlow(("PDMR3Relocate\n"));
429
430 /*
431 * Queues.
432 */
433 pdmR3QueueRelocate(pVM, offDelta);
434 pVM->pdm.s.pDevHlpQueueRC = PDMQueueRCPtr(pVM->pdm.s.pDevHlpQueueR3);
435
436 /*
437 * Critical sections.
438 */
439 pdmR3CritSectRelocate(pVM);
440
441 /*
442 * The registered PIC.
443 */
444 if (pVM->pdm.s.Pic.pDevInsRC)
445 {
446 pVM->pdm.s.Pic.pDevInsRC += offDelta;
447 pVM->pdm.s.Pic.pfnSetIrqRC += offDelta;
448 pVM->pdm.s.Pic.pfnGetInterruptRC += offDelta;
449 }
450
451 /*
452 * The registered APIC.
453 */
454 if (pVM->pdm.s.Apic.pDevInsRC)
455 {
456 pVM->pdm.s.Apic.pDevInsRC += offDelta;
457 pVM->pdm.s.Apic.pfnGetInterruptRC += offDelta;
458 pVM->pdm.s.Apic.pfnSetBaseRC += offDelta;
459 pVM->pdm.s.Apic.pfnGetBaseRC += offDelta;
460 pVM->pdm.s.Apic.pfnSetTPRRC += offDelta;
461 pVM->pdm.s.Apic.pfnGetTPRRC += offDelta;
462 pVM->pdm.s.Apic.pfnBusDeliverRC += offDelta;
463 if (pVM->pdm.s.Apic.pfnLocalInterruptRC)
464 pVM->pdm.s.Apic.pfnLocalInterruptRC += offDelta;
465 pVM->pdm.s.Apic.pfnWriteMSRRC += offDelta;
466 pVM->pdm.s.Apic.pfnReadMSRRC += offDelta;
467 }
468
469 /*
470 * The registered I/O APIC.
471 */
472 if (pVM->pdm.s.IoApic.pDevInsRC)
473 {
474 pVM->pdm.s.IoApic.pDevInsRC += offDelta;
475 pVM->pdm.s.IoApic.pfnSetIrqRC += offDelta;
476 if (pVM->pdm.s.IoApic.pfnSendMsiRC)
477 pVM->pdm.s.IoApic.pfnSendMsiRC += offDelta;
478 }
479
480 /*
481 * The register PCI Buses.
482 */
483 for (unsigned i = 0; i < RT_ELEMENTS(pVM->pdm.s.aPciBuses); i++)
484 {
485 if (pVM->pdm.s.aPciBuses[i].pDevInsRC)
486 {
487 pVM->pdm.s.aPciBuses[i].pDevInsRC += offDelta;
488 pVM->pdm.s.aPciBuses[i].pfnSetIrqRC += offDelta;
489 }
490 }
491
492 /*
493 * Devices & Drivers.
494 */
495 PCPDMDEVHLPRC pDevHlpRC;
496 int rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_pdmRCDevHlp", &pDevHlpRC);
497 AssertReleaseMsgRC(rc, ("rc=%Rrc when resolving g_pdmRCDevHlp\n", rc));
498
499 PCPDMDRVHLPRC pDrvHlpRC;
500 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_pdmRCDevHlp", &pDrvHlpRC);
501 AssertReleaseMsgRC(rc, ("rc=%Rrc when resolving g_pdmRCDevHlp\n", rc));
502
503 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
504 {
505 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_RC)
506 {
507 pDevIns->pHlpRC = pDevHlpRC;
508 pDevIns->pvInstanceDataRC = MMHyperR3ToRC(pVM, pDevIns->pvInstanceDataR3);
509 if (pDevIns->pCritSectR3)
510 pDevIns->pCritSectRC = MMHyperR3ToRC(pVM, pDevIns->pCritSectR3);
511 pDevIns->Internal.s.pVMRC = pVM->pVMRC;
512 if (pDevIns->Internal.s.pPciBusR3)
513 pDevIns->Internal.s.pPciBusRC = MMHyperR3ToRC(pVM, pDevIns->Internal.s.pPciBusR3);
514 if (pDevIns->Internal.s.pPciDeviceR3)
515 pDevIns->Internal.s.pPciDeviceRC = MMHyperR3ToRC(pVM, pDevIns->Internal.s.pPciDeviceR3);
516 if (pDevIns->pReg->pfnRelocate)
517 {
518 LogFlow(("PDMR3Relocate: Relocating device '%s'/%d\n",
519 pDevIns->pReg->szName, pDevIns->iInstance));
520 pDevIns->pReg->pfnRelocate(pDevIns, offDelta);
521 }
522 }
523
524 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
525 {
526 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
527 {
528 if (pDrvIns->pReg->fFlags & PDM_DRVREG_FLAGS_RC)
529 {
530 pDrvIns->pHlpRC = pDrvHlpRC;
531 pDrvIns->pvInstanceDataRC = MMHyperR3ToRC(pVM, pDrvIns->pvInstanceDataR3);
532 pDrvIns->Internal.s.pVMRC = pVM->pVMRC;
533 if (pDrvIns->pReg->pfnRelocate)
534 {
535 LogFlow(("PDMR3Relocate: Relocating driver '%s'/%u attached to '%s'/%d/%u\n",
536 pDrvIns->pReg->szName, pDrvIns->iInstance,
537 pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun));
538 pDrvIns->pReg->pfnRelocate(pDrvIns, offDelta);
539 }
540 }
541 }
542 }
543
544 }
545}
546
547
548/**
549 * Worker for pdmR3Term that terminates a LUN chain.
550 *
551 * @param pVM Pointer to the shared VM structure.
552 * @param pLun The head of the chain.
553 * @param pszDevice The name of the device (for logging).
554 * @param iInstance The device instance number (for logging).
555 */
556static void pdmR3TermLuns(PVM pVM, PPDMLUN pLun, const char *pszDevice, unsigned iInstance)
557{
558 for (; pLun; pLun = pLun->pNext)
559 {
560 /*
561 * Destroy them one at a time from the bottom up.
562 * (The serial device/drivers depends on this - bad.)
563 */
564 PPDMDRVINS pDrvIns = pLun->pBottom;
565 pLun->pBottom = pLun->pTop = NULL;
566 while (pDrvIns)
567 {
568 PPDMDRVINS pDrvNext = pDrvIns->Internal.s.pUp;
569
570 if (pDrvIns->pReg->pfnDestruct)
571 {
572 LogFlow(("pdmR3DevTerm: Destroying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
573 pDrvIns->pReg->szName, pDrvIns->iInstance, pLun->iLun, pszDevice, iInstance));
574 pDrvIns->pReg->pfnDestruct(pDrvIns);
575 }
576 pDrvIns->Internal.s.pDrv->cInstances--;
577
578 TMR3TimerDestroyDriver(pVM, pDrvIns);
579 //PDMR3QueueDestroyDriver(pVM, pDrvIns);
580 //pdmR3ThreadDestroyDriver(pVM, pDrvIns);
581 SSMR3DeregisterDriver(pVM, pDrvIns, NULL, 0);
582
583 pDrvIns = pDrvNext;
584 }
585 }
586}
587
588
589/**
590 * Terminates the PDM.
591 *
592 * Termination means cleaning up and freeing all resources,
593 * the VM it self is at this point powered off or suspended.
594 *
595 * @returns VBox status code.
596 * @param pVM The VM to operate on.
597 */
598VMMR3DECL(int) PDMR3Term(PVM pVM)
599{
600 LogFlow(("PDMR3Term:\n"));
601 AssertMsg(PDMCritSectIsInitialized(&pVM->pdm.s.CritSect), ("bad init order!\n"));
602
603 /*
604 * Iterate the device instances and attach drivers, doing
605 * relevant destruction processing.
606 *
607 * N.B. There is no need to mess around freeing memory allocated
608 * from any MM heap since MM will do that in its Term function.
609 */
610 /* usb ones first. */
611 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
612 {
613 pdmR3TermLuns(pVM, pUsbIns->Internal.s.pLuns, pUsbIns->pReg->szName, pUsbIns->iInstance);
614
615 if (pUsbIns->pReg->pfnDestruct)
616 {
617 LogFlow(("pdmR3DevTerm: Destroying - device '%s'/%d\n",
618 pUsbIns->pReg->szName, pUsbIns->iInstance));
619 pUsbIns->pReg->pfnDestruct(pUsbIns);
620 }
621
622 //TMR3TimerDestroyUsb(pVM, pUsbIns);
623 //SSMR3DeregisterUsb(pVM, pUsbIns, NULL, 0);
624 pdmR3ThreadDestroyUsb(pVM, pUsbIns);
625 }
626
627 /* then the 'normal' ones. */
628 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
629 {
630 pdmR3TermLuns(pVM, pDevIns->Internal.s.pLunsR3, pDevIns->pReg->szName, pDevIns->iInstance);
631
632 if (pDevIns->pReg->pfnDestruct)
633 {
634 LogFlow(("pdmR3DevTerm: Destroying - device '%s'/%d\n",
635 pDevIns->pReg->szName, pDevIns->iInstance));
636 pDevIns->pReg->pfnDestruct(pDevIns);
637 }
638
639 TMR3TimerDestroyDevice(pVM, pDevIns);
640 //SSMR3DeregisterDriver(pVM, pDevIns, NULL, 0);
641 pdmR3CritSectDeleteDevice(pVM, pDevIns);
642 //pdmR3ThreadDestroyDevice(pVM, pDevIns);
643 //PDMR3QueueDestroyDevice(pVM, pDevIns);
644 PGMR3PhysMMIO2Deregister(pVM, pDevIns, UINT32_MAX);
645 }
646
647 /*
648 * Destroy all threads.
649 */
650 pdmR3ThreadDestroyAll(pVM);
651
652 /*
653 * Destroy the block cache.
654 */
655 pdmR3BlkCacheTerm(pVM);
656
657#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
658 /*
659 * Free async completion managers.
660 */
661 pdmR3AsyncCompletionTerm(pVM);
662#endif
663
664 /*
665 * Free modules.
666 */
667 pdmR3LdrTermU(pVM->pUVM);
668
669 /*
670 * Destroy the PDM lock.
671 */
672 PDMR3CritSectDelete(&pVM->pdm.s.CritSect);
673 /* The MiscCritSect is deleted by PDMR3CritSectTerm. */
674
675 LogFlow(("PDMR3Term: returns %Rrc\n", VINF_SUCCESS));
676 return VINF_SUCCESS;
677}
678
679
680/**
681 * Terminates the PDM part of the UVM.
682 *
683 * This will unload any modules left behind.
684 *
685 * @param pUVM Pointer to the user mode VM structure.
686 */
687VMMR3DECL(void) PDMR3TermUVM(PUVM pUVM)
688{
689 /*
690 * In the normal cause of events we will now call pdmR3LdrTermU for
691 * the second time. In the case of init failure however, this might
692 * the first time, which is why we do it.
693 */
694 pdmR3LdrTermU(pUVM);
695
696 Assert(pUVM->pdm.s.pCritSects == NULL);
697 RTCritSectDelete(&pUVM->pdm.s.ListCritSect);
698}
699
700
701/**
702 * Bits that are saved in pass 0 and in the final pass.
703 *
704 * @param pVM The VM handle.
705 * @param pSSM The saved state handle.
706 */
707static void pdmR3SaveBoth(PVM pVM, PSSMHANDLE pSSM)
708{
709 /*
710 * Save the list of device instances so we can check that they're all still
711 * there when we load the state and that nothing new has been added.
712 */
713 uint32_t i = 0;
714 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3, i++)
715 {
716 SSMR3PutU32(pSSM, i);
717 SSMR3PutStrZ(pSSM, pDevIns->pReg->szName);
718 SSMR3PutU32(pSSM, pDevIns->iInstance);
719 }
720 SSMR3PutU32(pSSM, UINT32_MAX); /* terminator */
721}
722
723
724/**
725 * Live save.
726 *
727 * @returns VBox status code.
728 * @param pVM The VM handle.
729 * @param pSSM The saved state handle.
730 * @param uPass The pass.
731 */
732static DECLCALLBACK(int) pdmR3LiveExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uPass)
733{
734 LogFlow(("pdmR3LiveExec:\n"));
735 AssertReturn(uPass == 0, VERR_INTERNAL_ERROR_4);
736 pdmR3SaveBoth(pVM, pSSM);
737 return VINF_SSM_DONT_CALL_AGAIN;
738}
739
740
741/**
742 * Execute state save operation.
743 *
744 * @returns VBox status code.
745 * @param pVM The VM handle.
746 * @param pSSM The saved state handle.
747 */
748static DECLCALLBACK(int) pdmR3SaveExec(PVM pVM, PSSMHANDLE pSSM)
749{
750 LogFlow(("pdmR3SaveExec:\n"));
751
752 /*
753 * Save interrupt and DMA states.
754 */
755 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
756 {
757 PVMCPU pVCpu = &pVM->aCpus[idCpu];
758 SSMR3PutU32(pSSM, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_APIC));
759 SSMR3PutU32(pSSM, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_PIC));
760 SSMR3PutU32(pSSM, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_NMI));
761 SSMR3PutU32(pSSM, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_SMI));
762 }
763 SSMR3PutU32(pSSM, VM_FF_ISSET(pVM, VM_FF_PDM_DMA));
764
765 pdmR3SaveBoth(pVM, pSSM);
766 return VINF_SUCCESS;
767}
768
769
770/**
771 * Prepare state load operation.
772 *
773 * This will dispatch pending operations and clear the FFs governed by PDM and its devices.
774 *
775 * @returns VBox status code.
776 * @param pVM The VM handle.
777 * @param pSSM The SSM handle.
778 */
779static DECLCALLBACK(int) pdmR3LoadPrep(PVM pVM, PSSMHANDLE pSSM)
780{
781 LogFlow(("pdmR3LoadPrep: %s%s\n",
782 VM_FF_ISSET(pVM, VM_FF_PDM_QUEUES) ? " VM_FF_PDM_QUEUES" : "",
783 VM_FF_ISSET(pVM, VM_FF_PDM_DMA) ? " VM_FF_PDM_DMA" : ""));
784#ifdef LOG_ENABLED
785 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
786 {
787 PVMCPU pVCpu = &pVM->aCpus[idCpu];
788 LogFlow(("pdmR3LoadPrep: VCPU %u %s%s\n", idCpu,
789 VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_APIC) ? " VMCPU_FF_INTERRUPT_APIC" : "",
790 VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_PIC) ? " VMCPU_FF_INTERRUPT_PIC" : ""));
791 }
792#endif
793
794 /*
795 * In case there is work pending that will raise an interrupt,
796 * start a DMA transfer, or release a lock. (unlikely)
797 */
798 if (VM_FF_ISSET(pVM, VM_FF_PDM_QUEUES))
799 PDMR3QueueFlushAll(pVM);
800
801 /* Clear the FFs. */
802 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
803 {
804 PVMCPU pVCpu = &pVM->aCpus[idCpu];
805 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_APIC);
806 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_PIC);
807 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_NMI);
808 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_SMI);
809 }
810 VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
811
812 return VINF_SUCCESS;
813}
814
815
816/**
817 * Execute state load operation.
818 *
819 * @returns VBox status code.
820 * @param pVM VM Handle.
821 * @param pSSM SSM operation handle.
822 * @param uVersion Data layout version.
823 * @param uPass The data pass.
824 */
825static DECLCALLBACK(int) pdmR3LoadExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
826{
827 int rc;
828
829 LogFlow(("pdmR3LoadExec: uPass=%#x\n", uPass));
830
831 /*
832 * Validate version.
833 */
834 if ( uVersion != PDM_SAVED_STATE_VERSION
835 && uVersion != PDM_SAVED_STATE_VERSION_PRE_NMI_FF)
836 {
837 AssertMsgFailed(("Invalid version uVersion=%d!\n", uVersion));
838 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
839 }
840
841 if (uPass == SSM_PASS_FINAL)
842 {
843 /*
844 * Load the interrupt and DMA states.
845 */
846 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
847 {
848 PVMCPU pVCpu = &pVM->aCpus[idCpu];
849
850 /* APIC interrupt */
851 uint32_t fInterruptPending = 0;
852 rc = SSMR3GetU32(pSSM, &fInterruptPending);
853 if (RT_FAILURE(rc))
854 return rc;
855 if (fInterruptPending & ~1)
856 {
857 AssertMsgFailed(("fInterruptPending=%#x (APIC)\n", fInterruptPending));
858 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
859 }
860 AssertRelease(!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_APIC));
861 if (fInterruptPending)
862 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC);
863
864 /* PIC interrupt */
865 fInterruptPending = 0;
866 rc = SSMR3GetU32(pSSM, &fInterruptPending);
867 if (RT_FAILURE(rc))
868 return rc;
869 if (fInterruptPending & ~1)
870 {
871 AssertMsgFailed(("fInterruptPending=%#x (PIC)\n", fInterruptPending));
872 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
873 }
874 AssertRelease(!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_PIC));
875 if (fInterruptPending)
876 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_PIC);
877
878 if (uVersion > PDM_SAVED_STATE_VERSION_PRE_NMI_FF)
879 {
880 /* NMI interrupt */
881 fInterruptPending = 0;
882 rc = SSMR3GetU32(pSSM, &fInterruptPending);
883 if (RT_FAILURE(rc))
884 return rc;
885 if (fInterruptPending & ~1)
886 {
887 AssertMsgFailed(("fInterruptPending=%#x (NMI)\n", fInterruptPending));
888 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
889 }
890 AssertRelease(!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_NMI));
891 if (fInterruptPending)
892 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_NMI);
893
894 /* SMI interrupt */
895 fInterruptPending = 0;
896 rc = SSMR3GetU32(pSSM, &fInterruptPending);
897 if (RT_FAILURE(rc))
898 return rc;
899 if (fInterruptPending & ~1)
900 {
901 AssertMsgFailed(("fInterruptPending=%#x (SMI)\n", fInterruptPending));
902 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
903 }
904 AssertRelease(!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_SMI));
905 if (fInterruptPending)
906 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_SMI);
907 }
908 }
909
910 /* DMA pending */
911 uint32_t fDMAPending = 0;
912 rc = SSMR3GetU32(pSSM, &fDMAPending);
913 if (RT_FAILURE(rc))
914 return rc;
915 if (fDMAPending & ~1)
916 {
917 AssertMsgFailed(("fDMAPending=%#x\n", fDMAPending));
918 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
919 }
920 if (fDMAPending)
921 VM_FF_SET(pVM, VM_FF_PDM_DMA);
922 Log(("pdmR3LoadExec: VM_FF_PDM_DMA=%RTbool\n", VM_FF_ISSET(pVM, VM_FF_PDM_DMA)));
923 }
924
925 /*
926 * Load the list of devices and verify that they are all there.
927 */
928 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
929 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_FOUND;
930
931 for (uint32_t i = 0; ; i++)
932 {
933 /* Get the sequence number / terminator. */
934 uint32_t u32Sep;
935 rc = SSMR3GetU32(pSSM, &u32Sep);
936 if (RT_FAILURE(rc))
937 return rc;
938 if (u32Sep == UINT32_MAX)
939 break;
940 if (u32Sep != i)
941 AssertMsgFailedReturn(("Out of sequence. u32Sep=%#x i=%#x\n", u32Sep, i), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
942
943 /* Get the name and instance number. */
944 char szName[RT_SIZEOFMEMB(PDMDEVREG, szName)];
945 rc = SSMR3GetStrZ(pSSM, szName, sizeof(szName));
946 if (RT_FAILURE(rc))
947 return rc;
948 uint32_t iInstance;
949 rc = SSMR3GetU32(pSSM, &iInstance);
950 if (RT_FAILURE(rc))
951 return rc;
952
953 /* Try locate it. */
954 PPDMDEVINS pDevIns;
955 for (pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
956 if ( !strcmp(szName, pDevIns->pReg->szName)
957 && pDevIns->iInstance == iInstance)
958 {
959 AssertLogRelMsgReturn(!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_FOUND),
960 ("%s/#%u\n", pDevIns->pReg->szName, pDevIns->iInstance),
961 VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
962 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_FOUND;
963 break;
964 }
965 if (!pDevIns)
966 {
967 LogRel(("Device '%s'/%d not found in current config\n", szName, iInstance));
968 if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
969 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Device '%s'/%d not found in current config"), szName, iInstance);
970 }
971 }
972
973 /*
974 * Check that no additional devices were configured.
975 */
976 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
977 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_FOUND))
978 {
979 LogRel(("Device '%s'/%d not found in the saved state\n", pDevIns->pReg->szName, pDevIns->iInstance));
980 if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
981 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Device '%s'/%d not found in the saved state"),
982 pDevIns->pReg->szName, pDevIns->iInstance);
983 }
984
985 return VINF_SUCCESS;
986}
987
988
989/**
990 * Worker for PDMR3PowerOn that deals with one driver.
991 *
992 * @param pDrvIns The driver instance.
993 * @param pszDevName The parent device name.
994 * @param iDevInstance The parent device instance number.
995 * @param iLun The parent LUN number.
996 */
997DECLINLINE(int) pdmR3PowerOnDrv(PPDMDRVINS pDrvIns, const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
998{
999 Assert(pDrvIns->Internal.s.fVMSuspended);
1000 if (pDrvIns->pReg->pfnPowerOn)
1001 {
1002 LogFlow(("PDMR3PowerOn: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1003 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1004 int rc = VINF_SUCCESS; pDrvIns->pReg->pfnPowerOn(pDrvIns);
1005 if (RT_FAILURE(rc))
1006 {
1007 LogRel(("PDMR3PowerOn: driver '%s'/%d on LUN#%d of device '%s'/%d -> %Rrc\n",
1008 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, rc));
1009 return rc;
1010 }
1011 }
1012 pDrvIns->Internal.s.fVMSuspended = false;
1013 return VINF_SUCCESS;
1014}
1015
1016
1017/**
1018 * Worker for PDMR3PowerOn that deals with one USB device instance.
1019 *
1020 * @returns VBox status code.
1021 * @param pUsbIns The USB device instance.
1022 */
1023DECLINLINE(int) pdmR3PowerOnUsb(PPDMUSBINS pUsbIns)
1024{
1025 Assert(pUsbIns->Internal.s.fVMSuspended);
1026 if (pUsbIns->pReg->pfnVMPowerOn)
1027 {
1028 LogFlow(("PDMR3PowerOn: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1029 int rc = VINF_SUCCESS; pUsbIns->pReg->pfnVMPowerOn(pUsbIns);
1030 if (RT_FAILURE(rc))
1031 {
1032 LogRel(("PDMR3PowerOn: device '%s'/%d -> %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1033 return rc;
1034 }
1035 }
1036 pUsbIns->Internal.s.fVMSuspended = false;
1037 return VINF_SUCCESS;
1038}
1039
1040
1041/**
1042 * Worker for PDMR3PowerOn that deals with one device instance.
1043 *
1044 * @returns VBox status code.
1045 * @param pDevIns The device instance.
1046 */
1047DECLINLINE(int) pdmR3PowerOnDev(PPDMDEVINS pDevIns)
1048{
1049 Assert(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED);
1050 if (pDevIns->pReg->pfnPowerOn)
1051 {
1052 LogFlow(("PDMR3PowerOn: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1053 int rc = VINF_SUCCESS; pDevIns->pReg->pfnPowerOn(pDevIns);
1054 if (RT_FAILURE(rc))
1055 {
1056 LogRel(("PDMR3PowerOn: device '%s'/%d -> %Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
1057 return rc;
1058 }
1059 }
1060 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
1061 return VINF_SUCCESS;
1062}
1063
1064
1065/**
1066 * This function will notify all the devices and their
1067 * attached drivers about the VM now being powered on.
1068 *
1069 * @param pVM VM Handle.
1070 */
1071VMMR3DECL(void) PDMR3PowerOn(PVM pVM)
1072{
1073 LogFlow(("PDMR3PowerOn:\n"));
1074
1075 /*
1076 * Iterate thru the device instances and USB device instances,
1077 * processing the drivers associated with those.
1078 */
1079 int rc = VINF_SUCCESS;
1080 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns && RT_SUCCESS(rc); pDevIns = pDevIns->Internal.s.pNextR3)
1081 {
1082 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1083 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1084 rc = pdmR3PowerOnDrv(pDrvIns, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun);
1085 if (RT_SUCCESS(rc))
1086 rc = pdmR3PowerOnDev(pDevIns);
1087 }
1088
1089#ifdef VBOX_WITH_USB
1090 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns && RT_SUCCESS(rc); pUsbIns = pUsbIns->Internal.s.pNext)
1091 {
1092 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; 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, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun);
1095 if (RT_SUCCESS(rc))
1096 rc = pdmR3PowerOnUsb(pUsbIns);
1097 }
1098#endif
1099
1100#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
1101 pdmR3AsyncCompletionResume(pVM);
1102#endif
1103
1104 /*
1105 * Resume all threads.
1106 */
1107 if (RT_SUCCESS(rc))
1108 pdmR3ThreadResumeAll(pVM);
1109
1110 /*
1111 * On failure, clean up via PDMR3Suspend.
1112 */
1113 if (RT_FAILURE(rc))
1114 PDMR3Suspend(pVM);
1115
1116 LogFlow(("PDMR3PowerOn: returns %Rrc\n", rc));
1117 return /*rc*/;
1118}
1119
1120
1121/**
1122 * Initializes the asynchronous notifi stats structure.
1123 *
1124 * @param pThis The asynchronous notifification stats.
1125 * @param pszOp The name of the operation.
1126 */
1127static void pdmR3NotifyAsyncInit(PPDMNOTIFYASYNCSTATS pThis, const char *pszOp)
1128{
1129 pThis->uStartNsTs = RTTimeNanoTS();
1130 pThis->cNsElapsedNextLog = 0;
1131 pThis->cLoops = 0;
1132 pThis->cAsync = 0;
1133 pThis->pszOp = pszOp;
1134 pThis->offList = 0;
1135 pThis->szList[0] = '\0';
1136}
1137
1138
1139/**
1140 * Begin a new loop, prepares to gather new stats.
1141 *
1142 * @param pThis The asynchronous notifification stats.
1143 */
1144static void pdmR3NotifyAsyncBeginLoop(PPDMNOTIFYASYNCSTATS pThis)
1145{
1146 pThis->cLoops++;
1147 pThis->cAsync = 0;
1148 pThis->offList = 0;
1149 pThis->szList[0] = '\0';
1150}
1151
1152
1153/**
1154 * Records a device or USB device with a pending asynchronous notification.
1155 *
1156 * @param pThis The asynchronous notifification stats.
1157 * @param pszName The name of the thing.
1158 * @param iInstance The instance number.
1159 */
1160static void pdmR3NotifyAsyncAdd(PPDMNOTIFYASYNCSTATS pThis, const char *pszName, uint32_t iInstance)
1161{
1162 pThis->cAsync++;
1163 if (pThis->offList < sizeof(pThis->szList) - 4)
1164 pThis->offList += RTStrPrintf(&pThis->szList[pThis->offList], sizeof(pThis->szList) - pThis->offList,
1165 pThis->offList == 0 ? "%s/%u" : ", %s/%u",
1166 pszName, iInstance);
1167}
1168
1169
1170/**
1171 * Records the asynchronous completition of a reset, suspend or power off.
1172 *
1173 * @param pThis The asynchronous notifification stats.
1174 * @param pszDrvName The driver name.
1175 * @param iDrvInstance The driver instance number.
1176 * @param pszDevName The device or USB device name.
1177 * @param iDevInstance The device or USB device instance number.
1178 * @param iLun The LUN.
1179 */
1180static void pdmR3NotifyAsyncAddDrv(PPDMNOTIFYASYNCSTATS pThis, const char *pszDrvName, uint32_t iDrvInstance,
1181 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1182{
1183 pThis->cAsync++;
1184 if (pThis->offList < sizeof(pThis->szList) - 8)
1185 pThis->offList += RTStrPrintf(&pThis->szList[pThis->offList], sizeof(pThis->szList) - pThis->offList,
1186 pThis->offList == 0 ? "%s/%u/%u/%s/%u" : ", %s/%u/%u/%s/%u",
1187 pszDevName, iDevInstance, iLun, pszDrvName, iDrvInstance);
1188}
1189
1190
1191/**
1192 * Log the stats.
1193 *
1194 * @param pThis The asynchronous notifification stats.
1195 */
1196static void pdmR3NotifyAsyncLog(PPDMNOTIFYASYNCSTATS pThis)
1197{
1198 /*
1199 * Return if we shouldn't log at this point.
1200 * We log with an internval increasing from 0 sec to 60 sec.
1201 */
1202 if (!pThis->cAsync)
1203 return;
1204
1205 uint64_t cNsElapsed = RTTimeNanoTS() - pThis->uStartNsTs;
1206 if (cNsElapsed < pThis->cNsElapsedNextLog)
1207 return;
1208
1209 if (pThis->cNsElapsedNextLog == 0)
1210 pThis->cNsElapsedNextLog = RT_NS_1SEC;
1211 else if (pThis->cNsElapsedNextLog >= RT_NS_1MIN / 2)
1212 pThis->cNsElapsedNextLog = RT_NS_1MIN;
1213 else
1214 pThis->cNsElapsedNextLog *= 2;
1215
1216 /*
1217 * Do the logging.
1218 */
1219 LogRel(("%s: after %5llu ms, %u loops: %u async tasks - %s\n",
1220 pThis->pszOp, cNsElapsed / RT_NS_1MS, pThis->cLoops, pThis->cAsync, pThis->szList));
1221}
1222
1223
1224/**
1225 * Wait for events and process pending requests.
1226 *
1227 * @param pThis The asynchronous notifification stats.
1228 * @param pVM The VM handle.
1229 */
1230static void pdmR3NotifyAsyncWaitAndProcessRequests(PPDMNOTIFYASYNCSTATS pThis, PVM pVM)
1231{
1232 /** @todo This is utterly nuts and completely unsafe... will get back to it in a
1233 * bit I hope... */
1234 VM_ASSERT_EMT0(pVM);
1235 int rc = VMR3AsyncPdmNotificationWaitU(&pVM->pUVM->aCpus[0]);
1236 AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc - %s - %s\n", rc, pThis->pszOp, pThis->szList));
1237
1238 rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY);
1239 AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc - %s - %s\n", rc, pThis->pszOp, pThis->szList));
1240 rc = VMR3ReqProcessU(pVM->pUVM, 0/*idDstCpu*/);
1241 AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc - %s - %s\n", rc, pThis->pszOp, pThis->szList));
1242}
1243
1244
1245/**
1246 * Worker for PDMR3Reset that deals with one driver.
1247 *
1248 * @param pDrvIns The driver instance.
1249 * @param pAsync The structure for recording asynchronous
1250 * notification tasks.
1251 * @param pszDevName The parent device name.
1252 * @param iDevInstance The parent device instance number.
1253 * @param iLun The parent LUN number.
1254 */
1255DECLINLINE(bool) pdmR3ResetDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
1256 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1257{
1258 if (!pDrvIns->Internal.s.fVMReset)
1259 {
1260 pDrvIns->Internal.s.fVMReset = true;
1261 if (pDrvIns->pReg->pfnReset)
1262 {
1263 if (!pDrvIns->Internal.s.pfnAsyncNotify)
1264 {
1265 LogFlow(("PDMR3Reset: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1266 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1267 pDrvIns->pReg->pfnReset(pDrvIns);
1268 if (pDrvIns->Internal.s.pfnAsyncNotify)
1269 LogFlow(("PDMR3Reset: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1270 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1271 }
1272 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
1273 {
1274 pDrvIns->Internal.s.pfnAsyncNotify = false;
1275 LogFlow(("PDMR3Reset: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1276 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1277 }
1278 if (pDrvIns->Internal.s.pfnAsyncNotify)
1279 {
1280 pDrvIns->Internal.s.fVMReset = false;
1281 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance,
1282 pszDevName, iDevInstance, iLun);
1283 return false;
1284 }
1285 }
1286 }
1287 return true;
1288}
1289
1290
1291/**
1292 * Worker for PDMR3Reset that deals with one USB device instance.
1293 *
1294 * @param pUsbIns The USB device instance.
1295 * @param pAsync The structure for recording asynchronous
1296 * notification tasks.
1297 */
1298DECLINLINE(void) pdmR3ResetUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
1299{
1300 if (!pUsbIns->Internal.s.fVMReset)
1301 {
1302 pUsbIns->Internal.s.fVMReset = true;
1303 if (pUsbIns->pReg->pfnVMReset)
1304 {
1305 if (!pUsbIns->Internal.s.pfnAsyncNotify)
1306 {
1307 LogFlow(("PDMR3Reset: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1308 pUsbIns->pReg->pfnVMReset(pUsbIns);
1309 if (pUsbIns->Internal.s.pfnAsyncNotify)
1310 LogFlow(("PDMR3Reset: Async notification started - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1311 }
1312 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
1313 {
1314 LogFlow(("PDMR3Reset: Async notification completed - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1315 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
1316 }
1317 if (pUsbIns->Internal.s.pfnAsyncNotify)
1318 {
1319 pUsbIns->Internal.s.fVMReset = false;
1320 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
1321 }
1322 }
1323 }
1324}
1325
1326
1327/**
1328 * Worker for PDMR3Reset that deals with one device instance.
1329 *
1330 * @param pDevIns The device instance.
1331 * @param pAsync The structure for recording asynchronous
1332 * notification tasks.
1333 */
1334DECLINLINE(void) pdmR3ResetDev(PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
1335{
1336 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_RESET))
1337 {
1338 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_RESET;
1339 if (pDevIns->pReg->pfnReset)
1340 {
1341 if (!pDevIns->Internal.s.pfnAsyncNotify)
1342 {
1343 LogFlow(("PDMR3Reset: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1344 pDevIns->pReg->pfnReset(pDevIns);
1345 if (pDevIns->Internal.s.pfnAsyncNotify)
1346 LogFlow(("PDMR3Reset: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1347 }
1348 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
1349 {
1350 LogFlow(("PDMR3Reset: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1351 pDevIns->Internal.s.pfnAsyncNotify = NULL;
1352 }
1353 if (pDevIns->Internal.s.pfnAsyncNotify)
1354 {
1355 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_RESET;
1356 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
1357 }
1358 }
1359 }
1360}
1361
1362
1363/**
1364 * Resets a virtual CPU.
1365 *
1366 * Used by PDMR3Reset and CPU hot plugging.
1367 *
1368 * @param pVCpu The virtual CPU handle.
1369 */
1370VMMR3DECL(void) PDMR3ResetCpu(PVMCPU pVCpu)
1371{
1372 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_APIC);
1373 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_PIC);
1374 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_NMI);
1375 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_SMI);
1376}
1377
1378
1379/**
1380 * This function will notify all the devices and their attached drivers about
1381 * the VM now being reset.
1382 *
1383 * @param pVM VM Handle.
1384 */
1385VMMR3DECL(void) PDMR3Reset(PVM pVM)
1386{
1387 LogFlow(("PDMR3Reset:\n"));
1388
1389 /*
1390 * Clear all the reset flags.
1391 */
1392 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1393 {
1394 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_RESET;
1395 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1396 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1397 pDrvIns->Internal.s.fVMReset = false;
1398 }
1399#ifdef VBOX_WITH_USB
1400 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1401 {
1402 pUsbIns->Internal.s.fVMReset = false;
1403 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1404 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1405 pDrvIns->Internal.s.fVMReset = false;
1406 }
1407#endif
1408
1409 /*
1410 * The outer loop repeats until there are no more async requests.
1411 */
1412 PDMNOTIFYASYNCSTATS Async;
1413 pdmR3NotifyAsyncInit(&Async, "PDMR3Reset");
1414 for (;;)
1415 {
1416 pdmR3NotifyAsyncBeginLoop(&Async);
1417
1418 /*
1419 * Iterate thru the device instances and USB device instances,
1420 * processing the drivers associated with those.
1421 */
1422 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1423 {
1424 unsigned const cAsyncStart = Async.cAsync;
1425
1426 if (Async.cAsync == cAsyncStart)
1427 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1428 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1429 if (!pdmR3ResetDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
1430 break;
1431
1432 if (Async.cAsync == cAsyncStart)
1433 pdmR3ResetDev(pDevIns, &Async);
1434 }
1435
1436#ifdef VBOX_WITH_USB
1437 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1438 {
1439 unsigned const cAsyncStart = Async.cAsync;
1440
1441 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1442 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1443 if (!pdmR3ResetDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
1444 break;
1445
1446 if (Async.cAsync == cAsyncStart)
1447 pdmR3ResetUsb(pUsbIns, &Async);
1448 }
1449#endif
1450 if (!Async.cAsync)
1451 break;
1452 pdmR3NotifyAsyncLog(&Async);
1453 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
1454 }
1455
1456 /*
1457 * Clear all pending interrupts and DMA operations.
1458 */
1459 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
1460 PDMR3ResetCpu(&pVM->aCpus[idCpu]);
1461 VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
1462
1463 LogFlow(("PDMR3Reset: returns void\n"));
1464}
1465
1466
1467/**
1468 * Worker for PDMR3Suspend that deals with one driver.
1469 *
1470 * @param pDrvIns The driver instance.
1471 * @param pAsync The structure for recording asynchronous
1472 * notification tasks.
1473 * @param pszDevName The parent device name.
1474 * @param iDevInstance The parent device instance number.
1475 * @param iLun The parent LUN number.
1476 */
1477DECLINLINE(bool) pdmR3SuspendDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
1478 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1479{
1480 if (!pDrvIns->Internal.s.fVMSuspended)
1481 {
1482 pDrvIns->Internal.s.fVMSuspended = true;
1483 if (pDrvIns->pReg->pfnSuspend)
1484 {
1485 if (!pDrvIns->Internal.s.pfnAsyncNotify)
1486 {
1487 LogFlow(("PDMR3Suspend: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1488 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1489 pDrvIns->pReg->pfnSuspend(pDrvIns);
1490 if (pDrvIns->Internal.s.pfnAsyncNotify)
1491 LogFlow(("PDMR3Suspend: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1492 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1493 }
1494 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
1495 {
1496 pDrvIns->Internal.s.pfnAsyncNotify = NULL;
1497 LogFlow(("PDMR3Suspend: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1498 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1499 }
1500 if (pDrvIns->Internal.s.pfnAsyncNotify)
1501 {
1502 pDrvIns->Internal.s.fVMSuspended = false;
1503 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance, pszDevName, iDevInstance, iLun);
1504 return false;
1505 }
1506 }
1507 }
1508 return true;
1509}
1510
1511
1512/**
1513 * Worker for PDMR3Suspend that deals with one USB device instance.
1514 *
1515 * @param pUsbIns The USB device instance.
1516 * @param pAsync The structure for recording asynchronous
1517 * notification tasks.
1518 */
1519DECLINLINE(void) pdmR3SuspendUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
1520{
1521 if (!pUsbIns->Internal.s.fVMSuspended)
1522 {
1523 pUsbIns->Internal.s.fVMSuspended = true;
1524 if (pUsbIns->pReg->pfnVMSuspend)
1525 {
1526 if (!pUsbIns->Internal.s.pfnAsyncNotify)
1527 {
1528 LogFlow(("PDMR3Suspend: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1529 pUsbIns->pReg->pfnVMSuspend(pUsbIns);
1530 if (pUsbIns->Internal.s.pfnAsyncNotify)
1531 LogFlow(("PDMR3Suspend: Async notification started - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1532 }
1533 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
1534 {
1535 LogFlow(("PDMR3Suspend: Async notification completed - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1536 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
1537 }
1538 if (pUsbIns->Internal.s.pfnAsyncNotify)
1539 {
1540 pUsbIns->Internal.s.fVMSuspended = false;
1541 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
1542 }
1543 }
1544 }
1545}
1546
1547
1548/**
1549 * Worker for PDMR3Suspend that deals with one device instance.
1550 *
1551 * @param pDevIns The device instance.
1552 * @param pAsync The structure for recording asynchronous
1553 * notification tasks.
1554 */
1555DECLINLINE(void) pdmR3SuspendDev(PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
1556{
1557 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED))
1558 {
1559 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_SUSPENDED;
1560 if (pDevIns->pReg->pfnSuspend)
1561 {
1562 if (!pDevIns->Internal.s.pfnAsyncNotify)
1563 {
1564 LogFlow(("PDMR3Suspend: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1565 pDevIns->pReg->pfnSuspend(pDevIns);
1566 if (pDevIns->Internal.s.pfnAsyncNotify)
1567 LogFlow(("PDMR3Suspend: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1568 }
1569 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
1570 {
1571 LogFlow(("PDMR3Suspend: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1572 pDevIns->Internal.s.pfnAsyncNotify = NULL;
1573 }
1574 if (pDevIns->Internal.s.pfnAsyncNotify)
1575 {
1576 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
1577 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
1578 }
1579 }
1580 }
1581}
1582
1583
1584/**
1585 * This function will notify all the devices and their attached drivers about
1586 * the VM now being suspended.
1587 *
1588 * @param pVM The VM Handle.
1589 * @thread EMT(0)
1590 */
1591VMMR3DECL(void) PDMR3Suspend(PVM pVM)
1592{
1593 LogFlow(("PDMR3Suspend:\n"));
1594 VM_ASSERT_EMT0(pVM);
1595
1596 /*
1597 * The outer loop repeats until there are no more async requests.
1598 *
1599 * Note! We depend on the suspended indicators to be in the desired state
1600 * and we do not reset them before starting because this allows
1601 * PDMR3PowerOn and PDMR3Resume to use PDMR3Suspend for cleaning up
1602 * on failure.
1603 */
1604 PDMNOTIFYASYNCSTATS Async;
1605 pdmR3NotifyAsyncInit(&Async, "PDMR3Suspend");
1606 for (;;)
1607 {
1608 pdmR3NotifyAsyncBeginLoop(&Async);
1609
1610 /*
1611 * Iterate thru the device instances and USB device instances,
1612 * processing the drivers associated with those.
1613 *
1614 * The attached drivers are normally processed first. Some devices
1615 * (like DevAHCI) though needs to be notified before the drivers so
1616 * that it doesn't kick off any new requests after the drivers stopped
1617 * taking any. (DrvVD changes to read-only in this particular case.)
1618 */
1619 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1620 {
1621 unsigned const cAsyncStart = Async.cAsync;
1622
1623 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION)
1624 pdmR3SuspendDev(pDevIns, &Async);
1625
1626 if (Async.cAsync == cAsyncStart)
1627 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1628 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1629 if (!pdmR3SuspendDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
1630 break;
1631
1632 if ( Async.cAsync == cAsyncStart
1633 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION))
1634 pdmR3SuspendDev(pDevIns, &Async);
1635 }
1636
1637#ifdef VBOX_WITH_USB
1638 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1639 {
1640 unsigned const cAsyncStart = Async.cAsync;
1641
1642 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1643 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1644 if (!pdmR3SuspendDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
1645 break;
1646
1647 if (Async.cAsync == cAsyncStart)
1648 pdmR3SuspendUsb(pUsbIns, &Async);
1649 }
1650#endif
1651 if (!Async.cAsync)
1652 break;
1653 pdmR3NotifyAsyncLog(&Async);
1654 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
1655 }
1656
1657 /*
1658 * Suspend all threads.
1659 */
1660 pdmR3ThreadSuspendAll(pVM);
1661
1662 LogFlow(("PDMR3Suspend: returns void\n"));
1663}
1664
1665
1666/**
1667 * Worker for PDMR3Resume that deals with one driver.
1668 *
1669 * @param pDrvIns The driver instance.
1670 * @param pszDevName The parent device name.
1671 * @param iDevInstance The parent device instance number.
1672 * @param iLun The parent LUN number.
1673 */
1674DECLINLINE(int) pdmR3ResumeDrv(PPDMDRVINS pDrvIns, const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1675{
1676 Assert(pDrvIns->Internal.s.fVMSuspended);
1677 if (pDrvIns->pReg->pfnResume)
1678 {
1679 LogFlow(("PDMR3Resume: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1680 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1681 int rc = VINF_SUCCESS; pDrvIns->pReg->pfnResume(pDrvIns);
1682 if (RT_FAILURE(rc))
1683 {
1684 LogRel(("PDMR3Resume: driver '%s'/%d on LUN#%d of device '%s'/%d -> %Rrc\n",
1685 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, rc));
1686 return rc;
1687 }
1688 }
1689 pDrvIns->Internal.s.fVMSuspended = false;
1690 return VINF_SUCCESS;
1691}
1692
1693
1694/**
1695 * Worker for PDMR3Resume that deals with one USB device instance.
1696 *
1697 * @returns VBox status code.
1698 * @param pUsbIns The USB device instance.
1699 */
1700DECLINLINE(int) pdmR3ResumeUsb(PPDMUSBINS pUsbIns)
1701{
1702 Assert(pUsbIns->Internal.s.fVMSuspended);
1703 if (pUsbIns->pReg->pfnVMResume)
1704 {
1705 LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1706 int rc = VINF_SUCCESS; pUsbIns->pReg->pfnVMResume(pUsbIns);
1707 if (RT_FAILURE(rc))
1708 {
1709 LogRel(("PDMR3Resume: device '%s'/%d -> %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1710 return rc;
1711 }
1712 }
1713 pUsbIns->Internal.s.fVMSuspended = false;
1714 return VINF_SUCCESS;
1715}
1716
1717
1718/**
1719 * Worker for PDMR3Resume that deals with one device instance.
1720 *
1721 * @returns VBox status code.
1722 * @param pDevIns The device instance.
1723 */
1724DECLINLINE(int) pdmR3ResumeDev(PPDMDEVINS pDevIns)
1725{
1726 Assert(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED);
1727 if (pDevIns->pReg->pfnResume)
1728 {
1729 LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1730 int rc = VINF_SUCCESS; pDevIns->pReg->pfnResume(pDevIns);
1731 if (RT_FAILURE(rc))
1732 {
1733 LogRel(("PDMR3Resume: device '%s'/%d -> %Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
1734 return rc;
1735 }
1736 }
1737 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
1738 return VINF_SUCCESS;
1739}
1740
1741
1742/**
1743 * This function will notify all the devices and their
1744 * attached drivers about the VM now being resumed.
1745 *
1746 * @param pVM VM Handle.
1747 */
1748VMMR3DECL(void) PDMR3Resume(PVM pVM)
1749{
1750 LogFlow(("PDMR3Resume:\n"));
1751
1752 /*
1753 * Iterate thru the device instances and USB device instances,
1754 * processing the drivers associated with those.
1755 */
1756 int rc = VINF_SUCCESS;
1757 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns && RT_SUCCESS(rc); pDevIns = pDevIns->Internal.s.pNextR3)
1758 {
1759 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1760 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1761 rc = pdmR3ResumeDrv(pDrvIns, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun);
1762 if (RT_SUCCESS(rc))
1763 rc = pdmR3ResumeDev(pDevIns);
1764 }
1765
1766#ifdef VBOX_WITH_USB
1767 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns && RT_SUCCESS(rc); pUsbIns = pUsbIns->Internal.s.pNext)
1768 {
1769 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1770 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1771 rc = pdmR3ResumeDrv(pDrvIns, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun);
1772 if (RT_SUCCESS(rc))
1773 rc = pdmR3ResumeUsb(pUsbIns);
1774 }
1775#endif
1776
1777 /*
1778 * Resume all threads.
1779 */
1780 if (RT_SUCCESS(rc))
1781 pdmR3ThreadResumeAll(pVM);
1782
1783 /*
1784 * Resume the block cache.
1785 */
1786 if (RT_SUCCESS(rc))
1787 pdmR3BlkCacheResume(pVM);
1788
1789 /*
1790 * On failure, clean up via PDMR3Suspend.
1791 */
1792 if (RT_FAILURE(rc))
1793 PDMR3Suspend(pVM);
1794
1795 LogFlow(("PDMR3Resume: returns %Rrc\n", rc));
1796 return /*rc*/;
1797}
1798
1799
1800/**
1801 * Worker for PDMR3PowerOff that deals with one driver.
1802 *
1803 * @param pDrvIns The driver instance.
1804 * @param pAsync The structure for recording asynchronous
1805 * notification tasks.
1806 * @param pszDevName The parent device name.
1807 * @param iDevInstance The parent device instance number.
1808 * @param iLun The parent LUN number.
1809 */
1810DECLINLINE(bool) pdmR3PowerOffDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
1811 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1812{
1813 if (!pDrvIns->Internal.s.fVMSuspended)
1814 {
1815 pDrvIns->Internal.s.fVMSuspended = true;
1816 if (pDrvIns->pReg->pfnPowerOff)
1817 {
1818 if (!pDrvIns->Internal.s.pfnAsyncNotify)
1819 {
1820 LogFlow(("PDMR3PowerOff: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1821 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1822 pDrvIns->pReg->pfnPowerOff(pDrvIns);
1823 if (pDrvIns->Internal.s.pfnAsyncNotify)
1824 LogFlow(("PDMR3PowerOff: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1825 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1826 }
1827 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
1828 {
1829 pDrvIns->Internal.s.pfnAsyncNotify = false;
1830 LogFlow(("PDMR3PowerOff: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1831 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1832 }
1833 if (pDrvIns->Internal.s.pfnAsyncNotify)
1834 {
1835 pDrvIns->Internal.s.fVMSuspended = false;
1836 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance,
1837 pszDevName, iDevInstance, iLun);
1838 return false;
1839 }
1840 }
1841 }
1842 return true;
1843}
1844
1845
1846/**
1847 * Worker for PDMR3PowerOff that deals with one USB device instance.
1848 *
1849 * @param pUsbIns The USB device instance.
1850 * @param pAsync The structure for recording asynchronous
1851 * notification tasks.
1852 */
1853DECLINLINE(void) pdmR3PowerOffUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
1854{
1855 if (!pUsbIns->Internal.s.fVMSuspended)
1856 {
1857 pUsbIns->Internal.s.fVMSuspended = true;
1858 if (pUsbIns->pReg->pfnVMPowerOff)
1859 {
1860 if (!pUsbIns->Internal.s.pfnAsyncNotify)
1861 {
1862 LogFlow(("PDMR3PowerOff: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1863 pUsbIns->pReg->pfnVMPowerOff(pUsbIns);
1864 if (pUsbIns->Internal.s.pfnAsyncNotify)
1865 LogFlow(("PDMR3PowerOff: Async notification started - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1866 }
1867 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
1868 {
1869 LogFlow(("PDMR3PowerOff: Async notification completed - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1870 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
1871 }
1872 if (pUsbIns->Internal.s.pfnAsyncNotify)
1873 {
1874 pUsbIns->Internal.s.fVMSuspended = false;
1875 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
1876 }
1877 }
1878 }
1879}
1880
1881
1882/**
1883 * Worker for PDMR3PowerOff that deals with one device instance.
1884 *
1885 * @param pDevIns The device instance.
1886 * @param pAsync The structure for recording asynchronous
1887 * notification tasks.
1888 */
1889DECLINLINE(void) pdmR3PowerOffDev(PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
1890{
1891 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED))
1892 {
1893 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_SUSPENDED;
1894 if (pDevIns->pReg->pfnPowerOff)
1895 {
1896 if (!pDevIns->Internal.s.pfnAsyncNotify)
1897 {
1898 LogFlow(("PDMR3PowerOff: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1899 pDevIns->pReg->pfnPowerOff(pDevIns);
1900 if (pDevIns->Internal.s.pfnAsyncNotify)
1901 LogFlow(("PDMR3PowerOff: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1902 }
1903 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
1904 {
1905 LogFlow(("PDMR3PowerOff: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1906 pDevIns->Internal.s.pfnAsyncNotify = NULL;
1907 }
1908 if (pDevIns->Internal.s.pfnAsyncNotify)
1909 {
1910 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
1911 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
1912 }
1913 }
1914 }
1915}
1916
1917
1918/**
1919 * This function will notify all the devices and their
1920 * attached drivers about the VM being powered off.
1921 *
1922 * @param pVM VM Handle.
1923 */
1924VMMR3DECL(void) PDMR3PowerOff(PVM pVM)
1925{
1926 LogFlow(("PDMR3PowerOff:\n"));
1927
1928 /*
1929 * The outer loop repeats until there are no more async requests.
1930 */
1931 PDMNOTIFYASYNCSTATS Async;
1932 pdmR3NotifyAsyncInit(&Async, "PDMR3PowerOff");
1933 for (;;)
1934 {
1935 pdmR3NotifyAsyncBeginLoop(&Async);
1936
1937 /*
1938 * Iterate thru the device instances and USB device instances,
1939 * processing the drivers associated with those.
1940 *
1941 * The attached drivers are normally processed first. Some devices
1942 * (like DevAHCI) though needs to be notified before the drivers so
1943 * that it doesn't kick off any new requests after the drivers stopped
1944 * taking any. (DrvVD changes to read-only in this particular case.)
1945 */
1946 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1947 {
1948 unsigned const cAsyncStart = Async.cAsync;
1949
1950 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION)
1951 pdmR3PowerOffDev(pDevIns, &Async);
1952
1953 if (Async.cAsync == cAsyncStart)
1954 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1955 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1956 if (!pdmR3PowerOffDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
1957 break;
1958
1959 if ( Async.cAsync == cAsyncStart
1960 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION))
1961 pdmR3PowerOffDev(pDevIns, &Async);
1962 }
1963
1964#ifdef VBOX_WITH_USB
1965 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1966 {
1967 unsigned const cAsyncStart = Async.cAsync;
1968
1969 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1970 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1971 if (!pdmR3PowerOffDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
1972 break;
1973
1974 if (Async.cAsync == cAsyncStart)
1975 pdmR3PowerOffUsb(pUsbIns, &Async);
1976 }
1977#endif
1978 if (!Async.cAsync)
1979 break;
1980 pdmR3NotifyAsyncLog(&Async);
1981 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
1982 }
1983
1984 /*
1985 * Suspend all threads.
1986 */
1987 pdmR3ThreadSuspendAll(pVM);
1988
1989 LogFlow(("PDMR3PowerOff: returns void\n"));
1990}
1991
1992
1993/**
1994 * Queries the base interface of a device instance.
1995 *
1996 * The caller can use this to query other interfaces the device implements
1997 * and use them to talk to the device.
1998 *
1999 * @returns VBox status code.
2000 * @param pVM VM handle.
2001 * @param pszDevice Device name.
2002 * @param iInstance Device instance.
2003 * @param ppBase Where to store the pointer to the base device interface on success.
2004 * @remark We're not doing any locking ATM, so don't try call this at times when the
2005 * device chain is known to be updated.
2006 */
2007VMMR3DECL(int) PDMR3QueryDevice(PVM pVM, const char *pszDevice, unsigned iInstance, PPDMIBASE *ppBase)
2008{
2009 LogFlow(("PDMR3DeviceQuery: pszDevice=%p:{%s} iInstance=%u ppBase=%p\n", pszDevice, pszDevice, iInstance, ppBase));
2010
2011 /*
2012 * Iterate registered devices looking for the device.
2013 */
2014 size_t cchDevice = strlen(pszDevice);
2015 for (PPDMDEV pDev = pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
2016 {
2017 if ( pDev->cchName == cchDevice
2018 && !memcmp(pDev->pReg->szName, pszDevice, cchDevice))
2019 {
2020 /*
2021 * Iterate device instances.
2022 */
2023 for (PPDMDEVINS pDevIns = pDev->pInstances; pDevIns; pDevIns = pDevIns->Internal.s.pPerDeviceNextR3)
2024 {
2025 if (pDevIns->iInstance == iInstance)
2026 {
2027 if (pDevIns->IBase.pfnQueryInterface)
2028 {
2029 *ppBase = &pDevIns->IBase;
2030 LogFlow(("PDMR3DeviceQuery: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
2031 return VINF_SUCCESS;
2032 }
2033
2034 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NO_IBASE\n"));
2035 return VERR_PDM_DEVICE_INSTANCE_NO_IBASE;
2036 }
2037 }
2038
2039 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NOT_FOUND\n"));
2040 return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND;
2041 }
2042 }
2043
2044 LogFlow(("PDMR3QueryDevice: returns VERR_PDM_DEVICE_NOT_FOUND\n"));
2045 return VERR_PDM_DEVICE_NOT_FOUND;
2046}
2047
2048
2049/**
2050 * Queries the base interface of a device LUN.
2051 *
2052 * This differs from PDMR3QueryLun by that it returns the interface on the
2053 * device and not the top level driver.
2054 *
2055 * @returns VBox status code.
2056 * @param pVM VM Handle.
2057 * @param pszDevice Device name.
2058 * @param iInstance Device instance.
2059 * @param iLun The Logical Unit to obtain the interface of.
2060 * @param ppBase Where to store the base interface pointer.
2061 * @remark We're not doing any locking ATM, so don't try call this at times when the
2062 * device chain is known to be updated.
2063 */
2064VMMR3DECL(int) PDMR3QueryDeviceLun(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
2065{
2066 LogFlow(("PDMR3QueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
2067 pszDevice, pszDevice, iInstance, iLun, ppBase));
2068
2069 /*
2070 * Find the LUN.
2071 */
2072 PPDMLUN pLun;
2073 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
2074 if (RT_SUCCESS(rc))
2075 {
2076 *ppBase = pLun->pBase;
2077 LogFlow(("PDMR3QueryDeviceLun: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
2078 return VINF_SUCCESS;
2079 }
2080 LogFlow(("PDMR3QueryDeviceLun: returns %Rrc\n", rc));
2081 return rc;
2082}
2083
2084
2085/**
2086 * Query the interface of the top level driver on a LUN.
2087 *
2088 * @returns VBox status code.
2089 * @param pVM VM Handle.
2090 * @param pszDevice Device name.
2091 * @param iInstance Device instance.
2092 * @param iLun The Logical Unit to obtain the interface of.
2093 * @param ppBase Where to store the base interface pointer.
2094 * @remark We're not doing any locking ATM, so don't try call this at times when the
2095 * device chain is known to be updated.
2096 */
2097VMMR3DECL(int) PDMR3QueryLun(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
2098{
2099 LogFlow(("PDMR3QueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
2100 pszDevice, pszDevice, iInstance, iLun, ppBase));
2101 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
2102
2103 /*
2104 * Find the LUN.
2105 */
2106 PPDMLUN pLun;
2107 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
2108 if (RT_SUCCESS(rc))
2109 {
2110 if (pLun->pTop)
2111 {
2112 *ppBase = &pLun->pTop->IBase;
2113 LogFlow(("PDMR3QueryLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
2114 return VINF_SUCCESS;
2115 }
2116 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
2117 }
2118 LogFlow(("PDMR3QueryLun: returns %Rrc\n", rc));
2119 return rc;
2120}
2121
2122
2123/**
2124 * Query the interface of a named driver on a LUN.
2125 *
2126 * If the driver appears more than once in the driver chain, the first instance
2127 * is returned.
2128 *
2129 * @returns VBox status code.
2130 * @param pVM VM Handle.
2131 * @param pszDevice Device name.
2132 * @param iInstance Device instance.
2133 * @param iLun The Logical Unit to obtain the interface of.
2134 * @param pszDriver The driver name.
2135 * @param ppBase Where to store the base interface pointer.
2136 *
2137 * @remark We're not doing any locking ATM, so don't try call this at times when the
2138 * device chain is known to be updated.
2139 */
2140VMMR3DECL(int) PDMR3QueryDriverOnLun(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, const char *pszDriver, PPPDMIBASE ppBase)
2141{
2142 LogFlow(("PDMR3QueryDriverOnLun: pszDevice=%p:{%s} iInstance=%u iLun=%u pszDriver=%p:{%s} ppBase=%p\n",
2143 pszDevice, pszDevice, iInstance, iLun, pszDriver, pszDriver, ppBase));
2144
2145 /*
2146 * Find the LUN.
2147 */
2148 PPDMLUN pLun;
2149 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
2150 if (RT_SUCCESS(rc))
2151 {
2152 if (pLun->pTop)
2153 {
2154 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2155 if (!strcmp(pDrvIns->pReg->szName, pszDriver))
2156 {
2157 *ppBase = &pDrvIns->IBase;
2158 LogFlow(("PDMR3QueryDriverOnLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
2159 return VINF_SUCCESS;
2160
2161 }
2162 rc = VERR_PDM_DRIVER_NOT_FOUND;
2163 }
2164 else
2165 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
2166 }
2167 LogFlow(("PDMR3QueryDriverOnLun: returns %Rrc\n", rc));
2168 return rc;
2169}
2170
2171/**
2172 * Executes pending DMA transfers.
2173 * Forced Action handler.
2174 *
2175 * @param pVM VM handle.
2176 */
2177VMMR3DECL(void) PDMR3DmaRun(PVM pVM)
2178{
2179 /* Note! Not really SMP safe; restrict it to VCPU 0. */
2180 if (VMMGetCpuId(pVM) != 0)
2181 return;
2182
2183 if (VM_FF_TESTANDCLEAR(pVM, VM_FF_PDM_DMA))
2184 {
2185 if (pVM->pdm.s.pDmac)
2186 {
2187 bool fMore = pVM->pdm.s.pDmac->Reg.pfnRun(pVM->pdm.s.pDmac->pDevIns);
2188 if (fMore)
2189 VM_FF_SET(pVM, VM_FF_PDM_DMA);
2190 }
2191 }
2192}
2193
2194
2195/**
2196 * Service a VMMCALLRING3_PDM_LOCK call.
2197 *
2198 * @returns VBox status code.
2199 * @param pVM The VM handle.
2200 */
2201VMMR3DECL(int) PDMR3LockCall(PVM pVM)
2202{
2203 return PDMR3CritSectEnterEx(&pVM->pdm.s.CritSect, true /* fHostCall */);
2204}
2205
2206
2207/**
2208 * Registers the VMM device heap
2209 *
2210 * @returns VBox status code.
2211 * @param pVM VM handle.
2212 * @param GCPhys The physical address.
2213 * @param pvHeap Ring-3 pointer.
2214 * @param cbSize Size of the heap.
2215 */
2216VMMR3DECL(int) PDMR3RegisterVMMDevHeap(PVM pVM, RTGCPHYS GCPhys, RTR3PTR pvHeap, unsigned cbSize)
2217{
2218 Assert(pVM->pdm.s.pvVMMDevHeap == NULL);
2219
2220 Log(("PDMR3RegisterVMMDevHeap %RGp %RHv %x\n", GCPhys, pvHeap, cbSize));
2221 pVM->pdm.s.pvVMMDevHeap = pvHeap;
2222 pVM->pdm.s.GCPhysVMMDevHeap = GCPhys;
2223 pVM->pdm.s.cbVMMDevHeap = cbSize;
2224 pVM->pdm.s.cbVMMDevHeapLeft = cbSize;
2225 return VINF_SUCCESS;
2226}
2227
2228
2229/**
2230 * Unregisters the VMM device heap
2231 *
2232 * @returns VBox status code.
2233 * @param pVM VM handle.
2234 * @param GCPhys The physical address.
2235 */
2236VMMR3DECL(int) PDMR3UnregisterVMMDevHeap(PVM pVM, RTGCPHYS GCPhys)
2237{
2238 Assert(pVM->pdm.s.GCPhysVMMDevHeap == GCPhys);
2239
2240 Log(("PDMR3UnregisterVMMDevHeap %RGp\n", GCPhys));
2241 pVM->pdm.s.pvVMMDevHeap = NULL;
2242 pVM->pdm.s.GCPhysVMMDevHeap = NIL_RTGCPHYS;
2243 pVM->pdm.s.cbVMMDevHeap = 0;
2244 pVM->pdm.s.cbVMMDevHeapLeft = 0;
2245 return VINF_SUCCESS;
2246}
2247
2248
2249/**
2250 * Allocates memory from the VMM device heap
2251 *
2252 * @returns VBox status code.
2253 * @param pVM VM handle.
2254 * @param cbSize Allocation size.
2255 * @param pv Ring-3 pointer. (out)
2256 */
2257VMMR3DECL(int) PDMR3VMMDevHeapAlloc(PVM pVM, unsigned cbSize, RTR3PTR *ppv)
2258{
2259#ifdef DEBUG_bird
2260 if (!cbSize || cbSize > pVM->pdm.s.cbVMMDevHeapLeft)
2261 return VERR_NO_MEMORY;
2262#else
2263 AssertReturn(cbSize && cbSize <= pVM->pdm.s.cbVMMDevHeapLeft, VERR_NO_MEMORY);
2264#endif
2265
2266 Log(("PDMR3VMMDevHeapAlloc %x\n", cbSize));
2267
2268 /** @todo not a real heap as there's currently only one user. */
2269 *ppv = pVM->pdm.s.pvVMMDevHeap;
2270 pVM->pdm.s.cbVMMDevHeapLeft = 0;
2271 return VINF_SUCCESS;
2272}
2273
2274
2275/**
2276 * Frees memory from the VMM device heap
2277 *
2278 * @returns VBox status code.
2279 * @param pVM VM handle.
2280 * @param pv Ring-3 pointer.
2281 */
2282VMMR3DECL(int) PDMR3VMMDevHeapFree(PVM pVM, RTR3PTR pv)
2283{
2284 Log(("PDMR3VMMDevHeapFree %RHv\n", pv));
2285
2286 /** @todo not a real heap as there's currently only one user. */
2287 pVM->pdm.s.cbVMMDevHeapLeft = pVM->pdm.s.cbVMMDevHeap;
2288 return VINF_SUCCESS;
2289}
2290
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