VirtualBox

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

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

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 74.0 KB
Line 
1/* $Id: PDM.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * PDM - Pluggable Device Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/** @page pg_pdm PDM - The Pluggable Device & Driver Manager
20 *
21 * VirtualBox is designed to be very configurable, i.e. the ability to select
22 * virtual devices and configure them uniquely for a VM. For this reason
23 * virtual devices are not statically linked with the VMM but loaded, linked and
24 * instantiated at runtime by PDM using the information found in the
25 * Configuration Manager (CFGM).
26 *
27 * While the chief purpose of PDM is to manager of devices their drivers, it
28 * also serves as somewhere to put usful things like cross context queues, cross
29 * context synchronization (like critsect), VM centric thread management,
30 * asynchronous I/O framework, and so on.
31 *
32 * @see grp_pdm
33 *
34 *
35 * @section sec_pdm_dev The Pluggable Devices
36 *
37 * Devices register themselves when the module containing them is loaded. PDM
38 * will call the entry point 'VBoxDevicesRegister' when loading a device module.
39 * The device module will then use the supplied callback table to check the VMM
40 * version and to register its devices. Each device have an unique (for the
41 * configured VM) name. The name is not only used in PDM but also in CFGM (to
42 * organize device and device instance settings) and by anyone who wants to talk
43 * to a specific device instance.
44 *
45 * When all device modules have been successfully loaded PDM will instantiate
46 * those devices which are configured for the VM. Note that a device may have
47 * more than one instance, see network adaptors for instance. When
48 * instantiating a device PDM provides device instance memory and a callback
49 * table (aka Device Helpers / DevHlp) with the VM APIs which the device
50 * instance is trusted with.
51 *
52 * Some devices are trusted devices, most are not. The trusted devices are an
53 * integrated part of the VM and can obtain the VM handle from their device
54 * instance handles, thus enabling them to call any VM api. Untrusted devices
55 * can only use the callbacks provided during device instantiation.
56 *
57 * The main purpose in having DevHlps rather than just giving all the devices
58 * the VM handle and let them call the internal VM APIs directly, is both to
59 * create a binary interface that can be supported accross releases and to
60 * create a barrier between devices and the VM. (The trusted / untrusted bit
61 * hasn't turned out to be of much use btw., but it's easy to maintain so there
62 * isn't any point in removing it.)
63 *
64 * A device can provide a ring-0 and/or a raw-mode context extension to improve
65 * the VM performance by handling exits and traps (respectively) without
66 * requiring context switches (to ring-3). Callbacks for MMIO and I/O ports can
67 * needs to be registered specifically for the additional contexts for this to
68 * make sense. Also, the device has to be trusted to be loaded into R0/RC
69 * because of the extra privilege it entails. Note that raw-mode code and data
70 * will be subject to relocation.
71 *
72 *
73 * @section sec_pdm_special_devs Special Devices
74 *
75 * Several kinds of devices interacts with the VMM and/or other device and PDM
76 * will work like a mediator for these. The typical pattern is that the device
77 * calls a special registration device helper with a set of callbacks, PDM
78 * responds by copying this and providing a pointer to a set helper callbacks
79 * for that particular kind of device. Unlike interfaces where the callback
80 * table pointer is used a 'this' pointer, these arrangements will use the
81 * device instance pointer (PPDMDEVINS) as a kind of 'this' pointer.
82 *
83 * For an example of this kind of setup, see the PIC. The PIC registers itself
84 * by calling PDMDEVHLPR3::pfnPICRegister. PDM saves the device instance,
85 * copies the callback tables (PDMPICREG), resolving the ring-0 and raw-mode
86 * addresses in the process, and hands back the pointer to a set of helper
87 * methods (PDMPICHLPR3). The PCI device then queries the ring-0 and raw-mode
88 * helpers using PDMPICHLPR3::pfnGetR0Helpers and PDMPICHLPR3::pfnGetRCHelpers.
89 * The PCI device repeates ths pfnGetRCHelpers call in it's relocation method
90 * since the address changes when RC is relocated.
91 *
92 * @see grp_pdm_device
93 *
94 *
95 * @section sec_pdm_usbdev The Pluggable USB Devices
96 *
97 * USB devices are handled a little bit differently than other devices. The
98 * general concepts wrt. pluggability are mostly the same, but the details
99 * varies. The registration entry point is 'VBoxUsbRegister', the device
100 * instance is PDMUSBINS and the callbacks helpers are different. Also, USB
101 * device are restricted to ring-3 and cannot have any ring-0 or raw-mode
102 * extensions (at least not yet).
103 *
104 * The way USB devices work differs greatly from other devices though since they
105 * aren't attaches directly to the PCI/ISA/whatever system buses but via a
106 * USB host control (OHCI, UHCI or EHCI). USB devices handles USB requests
107 * (URBs) and does not register I/O ports, MMIO ranges or PCI bus
108 * devices/functions.
109 *
110 * @see grp_pdm_usbdev
111 *
112 *
113 * @section sec_pdm_drv The Pluggable Drivers
114 *
115 * The VM devices are often accessing host hardware or OS facilities. For most
116 * devices these facilities can be abstracted in one or more levels. These
117 * abstractions are called drivers.
118 *
119 * For instance take a DVD/CD drive. This can be connected to a SCSI
120 * controller, an ATA controller or a SATA controller. The basics of the DVD/CD
121 * drive implementation remains the same - eject, insert, read, seek, and such.
122 * (For the scsi case, you might wanna speak SCSI directly to, but that can of
123 * course be fixed - see SCSI passthru.) So, it
124 * makes much sense to have a generic CD/DVD driver which implements this.
125 *
126 * Then the media 'inserted' into the DVD/CD drive can be a ISO image, or it can
127 * be read from a real CD or DVD drive (there are probably other custom formats
128 * someone could desire to read or construct too). So, it would make sense to
129 * have abstracted interfaces for dealing with this in a generic way so the
130 * cdrom unit doesn't have to implement it all. Thus we have created the
131 * CDROM/DVD media driver family.
132 *
133 * So, for this example the IDE controller #1 (i.e. secondary) will have
134 * the DVD/CD Driver attached to it's LUN #0 (master). When a media is mounted
135 * the DVD/CD Driver will have a ISO, HostDVD or RAW (media) Driver attached.
136 *
137 * It is possible to configure many levels of drivers inserting filters, loggers,
138 * or whatever you desire into the chain. We're using this for network sniffing
139 * for instance.
140 *
141 * The drivers are loaded in a similar manner to that of the device, namely by
142 * iterating a keyspace in CFGM, load the modules listed there and call
143 * 'VBoxDriversRegister' with a callback table.
144 *
145 * @see grp_pdm_driver
146 *
147 *
148 * @section sec_pdm_ifs Interfaces
149 *
150 * The pluggable drivers and devices exposes one standard interface (callback
151 * table) which is used to construct, destruct, attach, detach,( ++,) and query
152 * other interfaces. A device will query the interfaces required for it's
153 * operation during init and hot-plug. PDM may query some interfaces during
154 * runtime mounting too.
155 *
156 * An interface here means a function table contained within the device or
157 * driver instance data. Its method are invoked with the function table pointer
158 * as the first argument and they will calculate the address of the device or
159 * driver instance data from it. (This is one of the aspects which *might* have
160 * been better done in C++.)
161 *
162 * @see grp_pdm_interfaces
163 *
164 *
165 * @section sec_pdm_utils Utilities
166 *
167 * As mentioned earlier, PDM is the location of any usful constrcts that doesn't
168 * quite fit into IPRT. The next subsections will discuss these.
169 *
170 * One thing these APIs all have in common is that resources will be associated
171 * with a device / driver and automatically freed after it has been destroyed if
172 * the destructor didn't do this.
173 *
174 *
175 * @subsection sec_pdm_async_completion Async I/O
176 *
177 * The PDM Async I/O API provides a somewhat platform agnostic interface for
178 * asynchronous I/O. For reasons of performance and complexcity this does not
179 * build upon any IPRT API.
180 *
181 * @todo more details.
182 *
183 * @see grp_pdm_async_completion
184 *
185 *
186 * @subsection sec_pdm_async_task Async Task - not implemented
187 *
188 * @todo implement and describe
189 *
190 * @see grp_pdm_async_task
191 *
192 *
193 * @subsection sec_pdm_critsect Critical Section
194 *
195 * The PDM Critical Section API is currently building on the IPRT API with the
196 * same name. It adds the posibility to use critical sections in ring-0 and
197 * raw-mode as well as in ring-3. There are certain restrictions on the RC and
198 * R0 usage though since we're not able to wait on it, nor wake up anyone that
199 * is waiting on it. These restrictions origins with the use of a ring-3 event
200 * semaphore. In a later incarnation we plan to replace the ring-3 event
201 * semaphore with a ring-0 one, thus enabling us to wake up waiters while
202 * exectuing in ring-0 and making the hardware assisted execution mode more
203 * efficient. (Raw-mode won't benefit much from this, naturally.)
204 *
205 * @see grp_pdm_critsect
206 *
207 *
208 * @subsection sec_pdm_queue Queue
209 *
210 * The PDM Queue API is for queuing one or more tasks for later consumption in
211 * ring-3 by EMT, and optinally forcing a delayed or ASAP return to ring-3. The
212 * queues can also be run on a timer basis as an alternative to the ASAP thing.
213 * The queue will be flushed at forced action time.
214 *
215 * A queue can also be used by another thread (a I/O worker for instance) to
216 * send work / events over to the EMT.
217 *
218 * @see grp_pdm_queue
219 *
220 *
221 * @subsection sec_pdm_task Task - not implemented yet
222 *
223 * The PDM Task API is for flagging a task for execution at a later point when
224 * we're back in ring-3, optionally forcing the ring-3 return to happen ASAP.
225 * As you can see the concept is similar to queues only simpler.
226 *
227 * A task can also be scheduled by another thread (a I/O worker for instance) as
228 * a mean of getting something done in EMT.
229 *
230 * @see grp_pdm_task
231 *
232 *
233 * @subsection sec_pdm_thread Thread
234 *
235 * The PDM Thread API is there to help devices and drivers manage their threads
236 * correctly wrt. power on, suspend, resume, power off and destruction.
237 *
238 * The general usage pattern for threads in the employ of devices and drivers is
239 * that they shuffle data or requests while the VM is running and stop doing
240 * this when the VM is paused or powered down. Rogue threads running while the
241 * VM is paused can cause the state to change during saving or have other
242 * unwanted side effects. The PDM Threads API ensures that this won't happen.
243 *
244 * @see grp_pdm_thread
245 *
246 */
247
248
249/*******************************************************************************
250* Header Files *
251*******************************************************************************/
252#define LOG_GROUP LOG_GROUP_PDM
253#include "PDMInternal.h"
254#include <VBox/pdm.h>
255#include <VBox/mm.h>
256#include <VBox/pgm.h>
257#include <VBox/ssm.h>
258#include <VBox/vm.h>
259#include <VBox/uvm.h>
260#include <VBox/vmm.h>
261#include <VBox/param.h>
262#include <VBox/err.h>
263#include <VBox/sup.h>
264
265#include <VBox/log.h>
266#include <iprt/asm.h>
267#include <iprt/assert.h>
268#include <iprt/alloc.h>
269#include <iprt/ldr.h>
270#include <iprt/path.h>
271#include <iprt/string.h>
272
273
274/*******************************************************************************
275* Defined Constants And Macros *
276*******************************************************************************/
277/** The PDM saved state version. */
278#define PDM_SAVED_STATE_VERSION 4
279#define PDM_SAVED_STATE_VERSION_PRE_NMI_FF 3
280
281
282/*******************************************************************************
283* Internal Functions *
284*******************************************************************************/
285static DECLCALLBACK(int) pdmR3LiveExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uPass);
286static DECLCALLBACK(int) pdmR3SaveExec(PVM pVM, PSSMHANDLE pSSM);
287static DECLCALLBACK(int) pdmR3LoadExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
288static DECLCALLBACK(int) pdmR3LoadPrep(PVM pVM, PSSMHANDLE pSSM);
289
290
291
292/**
293 * Initializes the PDM part of the UVM.
294 *
295 * This doesn't really do much right now but has to be here for the sake
296 * of completeness.
297 *
298 * @returns VBox status code.
299 * @param pUVM Pointer to the user mode VM structure.
300 */
301VMMR3DECL(int) PDMR3InitUVM(PUVM pUVM)
302{
303 AssertCompile(sizeof(pUVM->pdm.s) <= sizeof(pUVM->pdm.padding));
304 AssertRelease(sizeof(pUVM->pdm.s) <= sizeof(pUVM->pdm.padding));
305 pUVM->pdm.s.pModules = NULL;
306 pUVM->pdm.s.pCritSects = NULL;
307 return RTCritSectInit(&pUVM->pdm.s.ListCritSect);
308}
309
310
311/**
312 * Initializes the PDM.
313 *
314 * @returns VBox status code.
315 * @param pVM The VM to operate on.
316 */
317VMMR3DECL(int) PDMR3Init(PVM pVM)
318{
319 LogFlow(("PDMR3Init\n"));
320
321 /*
322 * Assert alignment and sizes.
323 */
324 AssertRelease(!(RT_OFFSETOF(VM, pdm.s) & 31));
325 AssertRelease(sizeof(pVM->pdm.s) <= sizeof(pVM->pdm.padding));
326 AssertCompileMemberAlignment(PDM, CritSect, sizeof(uintptr_t));
327 /*
328 * Init the structure.
329 */
330 pVM->pdm.s.offVM = RT_OFFSETOF(VM, pdm.s);
331 pVM->pdm.s.GCPhysVMMDevHeap = NIL_RTGCPHYS;
332
333 /*
334 * Initialize sub compontents.
335 */
336 int rc = pdmR3CritSectInitStats(pVM);
337 if (RT_SUCCESS(rc))
338 rc = PDMR3CritSectInit(pVM, &pVM->pdm.s.CritSect, RT_SRC_POS, "PDM");
339 if (RT_SUCCESS(rc))
340 rc = pdmR3LdrInitU(pVM->pUVM);
341#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
342 if (RT_SUCCESS(rc))
343 rc = pdmR3AsyncCompletionInit(pVM);
344#endif
345 if (RT_SUCCESS(rc))
346 rc = pdmR3DrvInit(pVM);
347 if (RT_SUCCESS(rc))
348 rc = pdmR3DevInit(pVM);
349 if (RT_SUCCESS(rc))
350 {
351 /*
352 * Register the saved state data unit.
353 */
354 rc = SSMR3RegisterInternal(pVM, "pdm", 1, PDM_SAVED_STATE_VERSION, 128,
355 NULL, pdmR3LiveExec, NULL,
356 NULL, pdmR3SaveExec, NULL,
357 pdmR3LoadPrep, pdmR3LoadExec, NULL);
358 if (RT_SUCCESS(rc))
359 {
360 LogFlow(("PDM: Successfully initialized\n"));
361 return rc;
362 }
363 }
364
365 /*
366 * Cleanup and return failure.
367 */
368 PDMR3Term(pVM);
369 LogFlow(("PDMR3Init: returns %Rrc\n", rc));
370 return rc;
371}
372
373
374/**
375 * Applies relocations to data and code managed by this
376 * component. This function will be called at init and
377 * whenever the VMM need to relocate it self inside the GC.
378 *
379 * @param pVM VM handle.
380 * @param offDelta Relocation delta relative to old location.
381 * @remark The loader subcomponent is relocated by PDMR3LdrRelocate() very
382 * early in the relocation phase.
383 */
384VMMR3DECL(void) PDMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
385{
386 LogFlow(("PDMR3Relocate\n"));
387
388 /*
389 * Queues.
390 */
391 pdmR3QueueRelocate(pVM, offDelta);
392 pVM->pdm.s.pDevHlpQueueRC = PDMQueueRCPtr(pVM->pdm.s.pDevHlpQueueR3);
393
394 /*
395 * Critical sections.
396 */
397 pdmR3CritSectRelocate(pVM);
398
399 /*
400 * The registered PIC.
401 */
402 if (pVM->pdm.s.Pic.pDevInsRC)
403 {
404 pVM->pdm.s.Pic.pDevInsRC += offDelta;
405 pVM->pdm.s.Pic.pfnSetIrqRC += offDelta;
406 pVM->pdm.s.Pic.pfnGetInterruptRC += offDelta;
407 }
408
409 /*
410 * The registered APIC.
411 */
412 if (pVM->pdm.s.Apic.pDevInsRC)
413 {
414 pVM->pdm.s.Apic.pDevInsRC += offDelta;
415 pVM->pdm.s.Apic.pfnGetInterruptRC += offDelta;
416 pVM->pdm.s.Apic.pfnSetBaseRC += offDelta;
417 pVM->pdm.s.Apic.pfnGetBaseRC += offDelta;
418 pVM->pdm.s.Apic.pfnSetTPRRC += offDelta;
419 pVM->pdm.s.Apic.pfnGetTPRRC += offDelta;
420 pVM->pdm.s.Apic.pfnBusDeliverRC += offDelta;
421 if (pVM->pdm.s.Apic.pfnLocalInterruptRC)
422 pVM->pdm.s.Apic.pfnLocalInterruptRC += offDelta;
423 pVM->pdm.s.Apic.pfnWriteMSRRC += offDelta;
424 pVM->pdm.s.Apic.pfnReadMSRRC += offDelta;
425 }
426
427 /*
428 * The registered I/O APIC.
429 */
430 if (pVM->pdm.s.IoApic.pDevInsRC)
431 {
432 pVM->pdm.s.IoApic.pDevInsRC += offDelta;
433 pVM->pdm.s.IoApic.pfnSetIrqRC += offDelta;
434 }
435
436 /*
437 * The register PCI Buses.
438 */
439 for (unsigned i = 0; i < RT_ELEMENTS(pVM->pdm.s.aPciBuses); i++)
440 {
441 if (pVM->pdm.s.aPciBuses[i].pDevInsRC)
442 {
443 pVM->pdm.s.aPciBuses[i].pDevInsRC += offDelta;
444 pVM->pdm.s.aPciBuses[i].pfnSetIrqRC += offDelta;
445 }
446 }
447
448 /*
449 * Devices & Drivers.
450 */
451 PCPDMDEVHLPRC pDevHlpRC;
452 int rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_pdmRCDevHlp", &pDevHlpRC);
453 AssertReleaseMsgRC(rc, ("rc=%Rrc when resolving g_pdmRCDevHlp\n", rc));
454
455 PCPDMDRVHLPRC pDrvHlpRC;
456 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_pdmRCDevHlp", &pDrvHlpRC);
457 AssertReleaseMsgRC(rc, ("rc=%Rrc when resolving g_pdmRCDevHlp\n", rc));
458
459 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
460 {
461 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_RC)
462 {
463 pDevIns->pHlpRC = pDevHlpRC;
464 pDevIns->pvInstanceDataRC = MMHyperR3ToRC(pVM, pDevIns->pvInstanceDataR3);
465 if (pDevIns->pCritSectR3)
466 pDevIns->pCritSectRC = MMHyperR3ToRC(pVM, pDevIns->pCritSectR3);
467 pDevIns->Internal.s.pVMRC = pVM->pVMRC;
468 if (pDevIns->Internal.s.pPciBusR3)
469 pDevIns->Internal.s.pPciBusRC = MMHyperR3ToRC(pVM, pDevIns->Internal.s.pPciBusR3);
470 if (pDevIns->Internal.s.pPciDeviceR3)
471 pDevIns->Internal.s.pPciDeviceRC = MMHyperR3ToRC(pVM, pDevIns->Internal.s.pPciDeviceR3);
472 if (pDevIns->pReg->pfnRelocate)
473 {
474 LogFlow(("PDMR3Relocate: Relocating device '%s'/%d\n",
475 pDevIns->pReg->szName, pDevIns->iInstance));
476 pDevIns->pReg->pfnRelocate(pDevIns, offDelta);
477 }
478 }
479
480 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
481 {
482 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
483 {
484 if (pDrvIns->pReg->fFlags & PDM_DRVREG_FLAGS_RC)
485 {
486 pDrvIns->pHlpRC = pDrvHlpRC;
487 pDrvIns->pvInstanceDataRC = MMHyperR3ToRC(pVM, pDrvIns->pvInstanceDataR3);
488 pDrvIns->Internal.s.pVMRC = pVM->pVMRC;
489 if (pDrvIns->pReg->pfnRelocate)
490 {
491 LogFlow(("PDMR3Relocate: Relocating driver '%s'/%u attached to '%s'/%d/%u\n",
492 pDrvIns->pReg->szName, pDrvIns->iInstance,
493 pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun));
494 pDrvIns->pReg->pfnRelocate(pDrvIns, offDelta);
495 }
496 }
497 }
498 }
499
500 }
501}
502
503
504/**
505 * Worker for pdmR3Term that terminates a LUN chain.
506 *
507 * @param pVM Pointer to the shared VM structure.
508 * @param pLun The head of the chain.
509 * @param pszDevice The name of the device (for logging).
510 * @param iInstance The device instance number (for logging).
511 */
512static void pdmR3TermLuns(PVM pVM, PPDMLUN pLun, const char *pszDevice, unsigned iInstance)
513{
514 for (; pLun; pLun = pLun->pNext)
515 {
516 /*
517 * Destroy them one at a time from the bottom up.
518 * (The serial device/drivers depends on this - bad.)
519 */
520 PPDMDRVINS pDrvIns = pLun->pBottom;
521 pLun->pBottom = pLun->pTop = NULL;
522 while (pDrvIns)
523 {
524 PPDMDRVINS pDrvNext = pDrvIns->Internal.s.pUp;
525
526 if (pDrvIns->pReg->pfnDestruct)
527 {
528 LogFlow(("pdmR3DevTerm: Destroying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
529 pDrvIns->pReg->szName, pDrvIns->iInstance, pLun->iLun, pszDevice, iInstance));
530 pDrvIns->pReg->pfnDestruct(pDrvIns);
531 }
532 pDrvIns->Internal.s.pDrv->cInstances--;
533
534 TMR3TimerDestroyDriver(pVM, pDrvIns);
535 //PDMR3QueueDestroyDriver(pVM, pDrvIns);
536 //pdmR3ThreadDestroyDriver(pVM, pDrvIns);
537 SSMR3DeregisterDriver(pVM, pDrvIns, NULL, 0);
538
539 pDrvIns = pDrvNext;
540 }
541 }
542}
543
544
545/**
546 * Terminates the PDM.
547 *
548 * Termination means cleaning up and freeing all resources,
549 * the VM it self is at this point powered off or suspended.
550 *
551 * @returns VBox status code.
552 * @param pVM The VM to operate on.
553 */
554VMMR3DECL(int) PDMR3Term(PVM pVM)
555{
556 LogFlow(("PDMR3Term:\n"));
557 AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
558
559 /*
560 * Iterate the device instances and attach drivers, doing
561 * relevant destruction processing.
562 *
563 * N.B. There is no need to mess around freeing memory allocated
564 * from any MM heap since MM will do that in its Term function.
565 */
566 /* usb ones first. */
567 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
568 {
569 pdmR3TermLuns(pVM, pUsbIns->Internal.s.pLuns, pUsbIns->pReg->szName, pUsbIns->iInstance);
570
571 if (pUsbIns->pReg->pfnDestruct)
572 {
573 LogFlow(("pdmR3DevTerm: Destroying - device '%s'/%d\n",
574 pUsbIns->pReg->szName, pUsbIns->iInstance));
575 pUsbIns->pReg->pfnDestruct(pUsbIns);
576 }
577
578 //TMR3TimerDestroyUsb(pVM, pUsbIns);
579 //SSMR3DeregisterUsb(pVM, pUsbIns, NULL, 0);
580 pdmR3ThreadDestroyUsb(pVM, pUsbIns);
581 }
582
583 /* then the 'normal' ones. */
584 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
585 {
586 pdmR3TermLuns(pVM, pDevIns->Internal.s.pLunsR3, pDevIns->pReg->szName, pDevIns->iInstance);
587
588 if (pDevIns->pReg->pfnDestruct)
589 {
590 LogFlow(("pdmR3DevTerm: Destroying - device '%s'/%d\n",
591 pDevIns->pReg->szName, pDevIns->iInstance));
592 pDevIns->pReg->pfnDestruct(pDevIns);
593 }
594
595 TMR3TimerDestroyDevice(pVM, pDevIns);
596 //SSMR3DeregisterDriver(pVM, pDevIns, NULL, 0);
597 pdmR3CritSectDeleteDevice(pVM, pDevIns);
598 //pdmR3ThreadDestroyDevice(pVM, pDevIns);
599 //PDMR3QueueDestroyDevice(pVM, pDevIns);
600 PGMR3PhysMMIO2Deregister(pVM, pDevIns, UINT32_MAX);
601 }
602
603 /*
604 * Destroy all threads.
605 */
606 pdmR3ThreadDestroyAll(pVM);
607
608#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
609 /*
610 * Free async completion managers.
611 */
612 pdmR3AsyncCompletionTerm(pVM);
613#endif
614
615 /*
616 * Free modules.
617 */
618 pdmR3LdrTermU(pVM->pUVM);
619
620 /*
621 * Destroy the PDM lock.
622 */
623 PDMR3CritSectDelete(&pVM->pdm.s.CritSect);
624 /* The MiscCritSect is deleted by PDMR3CritSectTerm. */
625
626 LogFlow(("PDMR3Term: returns %Rrc\n", VINF_SUCCESS));
627 return VINF_SUCCESS;
628}
629
630
631/**
632 * Terminates the PDM part of the UVM.
633 *
634 * This will unload any modules left behind.
635 *
636 * @param pUVM Pointer to the user mode VM structure.
637 */
638VMMR3DECL(void) PDMR3TermUVM(PUVM pUVM)
639{
640 /*
641 * In the normal cause of events we will now call pdmR3LdrTermU for
642 * the second time. In the case of init failure however, this might
643 * the first time, which is why we do it.
644 */
645 pdmR3LdrTermU(pUVM);
646
647 Assert(pUVM->pdm.s.pCritSects == NULL);
648 RTCritSectDelete(&pUVM->pdm.s.ListCritSect);
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->pfnPowerOff)
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->pfnPowerOff)
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.

© 2023 Oracle
ContactPrivacy policyTerms of Use