VirtualBox

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

Last change on this file since 26550 was 26175, checked in by vboxsync, 15 years ago

PDM: Ring-0 and raw-mode context driver helpers. Driver RC relcoations.

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