VirtualBox

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

最後變更 在這個檔案從25816是 25816,由 vboxsync 提交於 15 年 前

CPU hotplug: Merge the first patch. Resets a CPU state if a CPU was removed from the VM

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette