VirtualBox

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

Last change on this file since 27955 was 27936, checked in by vboxsync, 15 years ago

pdmR3PowerOffDev: Fixed copy & past error that prevented PDMDEVREG::pfnPowerOff from being called on drivers without a pfnSuspend callback.

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