VirtualBox

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

Last change on this file since 36847 was 36041, checked in by vboxsync, 14 years ago

Main/VMM: Use UVM w/ refcounting - part 1.

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