VirtualBox

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

Last change on this file since 24911 was 24748, checked in by vboxsync, 15 years ago

PDM.cpp: debug loop counter.

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