VirtualBox

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

Last change on this file since 23011 was 22890, checked in by vboxsync, 15 years ago

VM::cCPUs -> VM::cCpus so it matches all the other cCpus and aCpus members.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 52.1 KB
Line 
1/* $Id: PDM.cpp 22890 2009-09-09 23:11:31Z vboxsync $ */
2/** @file
3 * PDM - Pluggable Device Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/** @page pg_pdm PDM - The Pluggable Device & Driver Manager
24 *
25 * VirtualBox is designed to be very configurable, i.e. the ability to select
26 * virtual devices and configure them uniquely for a VM. For this reason
27 * virtual devices are not statically linked with the VMM but loaded, linked and
28 * instantiated at runtime by PDM using the information found in the
29 * Configuration Manager (CFGM).
30 *
31 * While the chief purpose of PDM is to manager of devices their drivers, it
32 * also serves as somewhere to put usful things like cross context queues, cross
33 * context synchronization (like critsect), VM centric thread management,
34 * asynchronous I/O framework, and so on.
35 *
36 * @see grp_pdm
37 *
38 *
39 * @section sec_pdm_dev The Pluggable Devices
40 *
41 * Devices register themselves when the module containing them is loaded. PDM
42 * will call the entry point 'VBoxDevicesRegister' when loading a device module.
43 * The device module will then use the supplied callback table to check the VMM
44 * version and to register its devices. Each device have an unique (for the
45 * configured VM) name. The name is not only used in PDM but also in CFGM (to
46 * organize device and device instance settings) and by anyone who wants to talk
47 * to a specific device instance.
48 *
49 * When all device modules have been successfully loaded PDM will instantiate
50 * those devices which are configured for the VM. Note that a device may have
51 * more than one instance, see network adaptors for instance. When
52 * instantiating a device PDM provides device instance memory and a callback
53 * table (aka Device Helpers / DevHlp) with the VM APIs which the device
54 * instance is trusted with.
55 *
56 * Some devices are trusted devices, most are not. The trusted devices are an
57 * integrated part of the VM and can obtain the VM handle from their device
58 * instance handles, thus enabling them to call any VM api. Untrusted devices
59 * can only use the callbacks provided during device instantiation.
60 *
61 * The main purpose in having DevHlps rather than just giving all the devices
62 * the VM handle and let them call the internal VM APIs directly, is both to
63 * create a binary interface that can be supported accross releases and to
64 * create a barrier between devices and the VM. (The trusted / untrusted bit
65 * hasn't turned out to be of much use btw., but it's easy to maintain so there
66 * isn't any point in removing it.)
67 *
68 * A device can provide a ring-0 and/or a raw-mode context extension to improve
69 * the VM performance by handling exits and traps (respectively) without
70 * requiring context switches (to ring-3). Callbacks for MMIO and I/O ports can
71 * needs to be registered specifically for the additional contexts for this to
72 * make sense. Also, the device has to be trusted to be loaded into R0/RC
73 * because of the extra privilege it entails. Note that raw-mode code and data
74 * will be subject to relocation.
75 *
76 *
77 * @section sec_pdm_special_devs Special Devices
78 *
79 * Several kinds of devices interacts with the VMM and/or other device and PDM
80 * will work like a mediator for these. The typical pattern is that the device
81 * calls a special registration device helper with a set of callbacks, PDM
82 * responds by copying this and providing a pointer to a set helper callbacks
83 * for that particular kind of device. Unlike interfaces where the callback
84 * table pointer is used a 'this' pointer, these arrangements will use the
85 * device instance pointer (PPDMDEVINS) as a kind of 'this' pointer.
86 *
87 * For an example of this kind of setup, see the PIC. The PIC registers itself
88 * by calling PDMDEVHLPR3::pfnPICRegister. PDM saves the device instance,
89 * copies the callback tables (PDMPICREG), resolving the ring-0 and raw-mode
90 * addresses in the process, and hands back the pointer to a set of helper
91 * methods (PDMPICHLPR3). The PCI device then queries the ring-0 and raw-mode
92 * helpers using PDMPICHLPR3::pfnGetR0Helpers and PDMPICHLPR3::pfnGetRCHelpers.
93 * The PCI device repeates ths pfnGetRCHelpers call in it's relocation method
94 * since the address changes when RC is relocated.
95 *
96 * @see grp_pdm_device
97 *
98 *
99 * @section sec_pdm_usbdev The Pluggable USB Devices
100 *
101 * USB devices are handled a little bit differently than other devices. The
102 * general concepts wrt. pluggability are mostly the same, but the details
103 * varies. The registration entry point is 'VBoxUsbRegister', the device
104 * instance is PDMUSBINS and the callbacks helpers are different. Also, USB
105 * device are restricted to ring-3 and cannot have any ring-0 or raw-mode
106 * extensions (at least not yet).
107 *
108 * The way USB devices work differs greatly from other devices though since they
109 * aren't attaches directly to the PCI/ISA/whatever system buses but via a
110 * USB host control (OHCI, UHCI or EHCI). USB devices handles USB requests
111 * (URBs) and does not register I/O ports, MMIO ranges or PCI bus
112 * devices/functions.
113 *
114 * @see grp_pdm_usbdev
115 *
116 *
117 * @section sec_pdm_drv The Pluggable Drivers
118 *
119 * The VM devices are often accessing host hardware or OS facilities. For most
120 * devices these facilities can be abstracted in one or more levels. These
121 * abstractions are called drivers.
122 *
123 * For instance take a DVD/CD drive. This can be connected to a SCSI
124 * controller, an ATA controller or a SATA controller. The basics of the DVD/CD
125 * drive implementation remains the same - eject, insert, read, seek, and such.
126 * (For the scsi case, you might wanna speak SCSI directly to, but that can of
127 * course be fixed - see SCSI passthru.) So, it
128 * makes much sense to have a generic CD/DVD driver which implements this.
129 *
130 * Then the media 'inserted' into the DVD/CD drive can be a ISO image, or it can
131 * be read from a real CD or DVD drive (there are probably other custom formats
132 * someone could desire to read or construct too). So, it would make sense to
133 * have abstracted interfaces for dealing with this in a generic way so the
134 * cdrom unit doesn't have to implement it all. Thus we have created the
135 * CDROM/DVD media driver family.
136 *
137 * So, for this example the IDE controller #1 (i.e. secondary) will have
138 * the DVD/CD Driver attached to it's LUN #0 (master). When a media is mounted
139 * the DVD/CD Driver will have a ISO, HostDVD or RAW (media) Driver attached.
140 *
141 * It is possible to configure many levels of drivers inserting filters, loggers,
142 * or whatever you desire into the chain. We're using this for network sniffing
143 * for instance.
144 *
145 * The drivers are loaded in a similar manner to that of the device, namely by
146 * iterating a keyspace in CFGM, load the modules listed there and call
147 * 'VBoxDriversRegister' with a callback table.
148 *
149 * @see grp_pdm_driver
150 *
151 *
152 * @section sec_pdm_ifs Interfaces
153 *
154 * The pluggable drivers and devices exposes one standard interface (callback
155 * table) which is used to construct, destruct, attach, detach,( ++,) and query
156 * other interfaces. A device will query the interfaces required for it's
157 * operation during init and hotplug. PDM may query some interfaces during
158 * runtime mounting too.
159 *
160 * An interface here means a function table contained within the device or
161 * driver instance data. Its method are invoked with the function table pointer
162 * as the first argument and they will calculate the address of the device or
163 * driver instance data from it. (This is one of the aspects which *might* have
164 * been better done in C++.)
165 *
166 * @see grp_pdm_interfaces
167 *
168 *
169 * @section sec_pdm_utils Utilities
170 *
171 * As mentioned earlier, PDM is the location of any usful constrcts that doesn't
172 * quite fit into IPRT. The next subsections will discuss these.
173 *
174 * One thing these APIs all have in common is that resources will be associated
175 * with a device / driver and automatically freed after it has been destroyed if
176 * the destructor didn't do this.
177 *
178 *
179 * @subsection sec_pdm_async_completion Async I/O
180 *
181 * The PDM Async I/O API provides a somewhat platform agnostic interface for
182 * asynchronous I/O. For reasons of performance and complexcity this does not
183 * build upon any IPRT API.
184 *
185 * @todo more details.
186 *
187 * @see grp_pdm_async_completion
188 *
189 *
190 * @subsection sec_pdm_async_task Async Task - not implemented
191 *
192 * @todo implement and describe
193 *
194 * @see grp_pdm_async_task
195 *
196 *
197 * @subsection sec_pdm_critsect Critical Section
198 *
199 * The PDM Critical Section API is currently building on the IPRT API with the
200 * same name. It adds the posibility to use critical sections in ring-0 and
201 * raw-mode as well as in ring-3. There are certain restrictions on the RC and
202 * R0 usage though since we're not able to wait on it, nor wake up anyone that
203 * is waiting on it. These restrictions origins with the use of a ring-3 event
204 * semaphore. In a later incarnation we plan to replace the ring-3 event
205 * semaphore with a ring-0 one, thus enabling us to wake up waiters while
206 * exectuing in ring-0 and making the hardware assisted execution mode more
207 * efficient. (Raw-mode won't benefit much from this, naturally.)
208 *
209 * @see grp_pdm_critsect
210 *
211 *
212 * @subsection sec_pdm_queue Queue
213 *
214 * The PDM Queue API is for queuing one or more tasks for later consumption in
215 * ring-3 by EMT, and optinally forcing a delayed or ASAP return to ring-3. The
216 * queues can also be run on a timer basis as an alternative to the ASAP thing.
217 * The queue will be flushed at forced action time.
218 *
219 * A queue can also be used by another thread (a I/O worker for instance) to
220 * send work / events over to the EMT.
221 *
222 * @see grp_pdm_queue
223 *
224 *
225 * @subsection sec_pdm_task Task - not implemented yet
226 *
227 * The PDM Task API is for flagging a task for execution at a later point when
228 * we're back in ring-3, optionally forcing the ring-3 return to happen ASAP.
229 * As you can see the concept is similar to queues only simpler.
230 *
231 * A task can also be scheduled by another thread (a I/O worker for instance) as
232 * a mean of getting something done in EMT.
233 *
234 * @see grp_pdm_task
235 *
236 *
237 * @subsection sec_pdm_thread Thread
238 *
239 * The PDM Thread API is there to help devices and drivers manage their threads
240 * correctly wrt. power on, suspend, resume, power off and destruction.
241 *
242 * The general usage pattern for threads in the employ of devices and drivers is
243 * that they shuffle data or requests while the VM is running and stop doing
244 * this when the VM is paused or powered down. Rogue threads running while the
245 * VM is paused can cause the state to change during saving or have other
246 * unwanted side effects. The PDM Threads API ensures that this won't happen.
247 *
248 * @see grp_pdm_thread
249 *
250 */
251
252
253/*******************************************************************************
254* Header Files *
255*******************************************************************************/
256#define LOG_GROUP LOG_GROUP_PDM
257#include "PDMInternal.h"
258#include <VBox/pdm.h>
259#include <VBox/mm.h>
260#include <VBox/pgm.h>
261#include <VBox/ssm.h>
262#include <VBox/vm.h>
263#include <VBox/uvm.h>
264#include <VBox/vmm.h>
265#include <VBox/param.h>
266#include <VBox/err.h>
267#include <VBox/sup.h>
268
269#include <VBox/log.h>
270#include <iprt/asm.h>
271#include <iprt/assert.h>
272#include <iprt/alloc.h>
273#include <iprt/ldr.h>
274#include <iprt/path.h>
275#include <iprt/string.h>
276
277
278/*******************************************************************************
279* Defined Constants And Macros *
280*******************************************************************************/
281/** The PDM saved state version. */
282#define PDM_SAVED_STATE_VERSION 4
283#define PDM_SAVED_STATE_VERSION_PRE_NMI_FF 3
284
285
286/*******************************************************************************
287* Internal Functions *
288*******************************************************************************/
289static DECLCALLBACK(int) pdmR3Save(PVM pVM, PSSMHANDLE pSSM);
290static DECLCALLBACK(int) pdmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
291static DECLCALLBACK(int) pdmR3LoadPrep(PVM pVM, PSSMHANDLE pSSM);
292
293
294
295/**
296 * Initializes the PDM part of the UVM.
297 *
298 * This doesn't really do much right now but has to be here for the sake
299 * of completeness.
300 *
301 * @returns VBox status code.
302 * @param pUVM Pointer to the user mode VM structure.
303 */
304VMMR3DECL(int) PDMR3InitUVM(PUVM pUVM)
305{
306 AssertCompile(sizeof(pUVM->pdm.s) <= sizeof(pUVM->pdm.padding));
307 AssertRelease(sizeof(pUVM->pdm.s) <= sizeof(pUVM->pdm.padding));
308 pUVM->pdm.s.pModules = NULL;
309 return VINF_SUCCESS;
310}
311
312
313/**
314 * Initializes the PDM.
315 *
316 * @returns VBox status code.
317 * @param pVM The VM to operate on.
318 */
319VMMR3DECL(int) PDMR3Init(PVM pVM)
320{
321 LogFlow(("PDMR3Init\n"));
322
323 /*
324 * Assert alignment and sizes.
325 */
326 AssertRelease(!(RT_OFFSETOF(VM, pdm.s) & 31));
327 AssertRelease(sizeof(pVM->pdm.s) <= sizeof(pVM->pdm.padding));
328 AssertCompileMemberAlignment(PDM, CritSect, sizeof(uintptr_t));
329 /*
330 * Init the structure.
331 */
332 pVM->pdm.s.offVM = RT_OFFSETOF(VM, pdm.s);
333 pVM->pdm.s.GCPhysVMMDevHeap = NIL_RTGCPHYS;
334
335 /*
336 * Initialize sub compontents.
337 */
338 int rc = RTCritSectInit(&pVM->pdm.s.MiscCritSect);
339 if (RT_SUCCESS(rc))
340 rc = pdmR3CritSectInit(pVM);
341 if (RT_SUCCESS(rc))
342 rc = PDMR3CritSectInit(pVM, &pVM->pdm.s.CritSect, "PDM");
343 if (RT_SUCCESS(rc))
344 rc = pdmR3LdrInitU(pVM->pUVM);
345#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
346 if (RT_SUCCESS(rc))
347 rc = pdmR3AsyncCompletionInit(pVM);
348#endif
349 if (RT_SUCCESS(rc))
350 rc = pdmR3DrvInit(pVM);
351 if (RT_SUCCESS(rc))
352 rc = pdmR3DevInit(pVM);
353 if (RT_SUCCESS(rc))
354 {
355 /*
356 * Register the saved state data unit.
357 */
358 rc = SSMR3RegisterInternal(pVM, "pdm", 1, PDM_SAVED_STATE_VERSION, 128,
359 NULL, NULL, NULL,
360 NULL, pdmR3Save, NULL,
361 pdmR3LoadPrep, pdmR3Load, NULL);
362 if (RT_SUCCESS(rc))
363 {
364 LogFlow(("PDM: Successfully initialized\n"));
365 return rc;
366 }
367 }
368
369 /*
370 * Cleanup and return failure.
371 */
372 PDMR3Term(pVM);
373 LogFlow(("PDMR3Init: returns %Rrc\n", rc));
374 return rc;
375}
376
377
378/**
379 * Applies relocations to data and code managed by this
380 * component. This function will be called at init and
381 * whenever the VMM need to relocate it self inside the GC.
382 *
383 * @param pVM VM handle.
384 * @param offDelta Relocation delta relative to old location.
385 * @remark The loader subcomponent is relocated by PDMR3LdrRelocate() very
386 * early in the relocation phase.
387 */
388VMMR3DECL(void) PDMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
389{
390 LogFlow(("PDMR3Relocate\n"));
391
392 /*
393 * Queues.
394 */
395 pdmR3QueueRelocate(pVM, offDelta);
396 pVM->pdm.s.pDevHlpQueueRC = PDMQueueRCPtr(pVM->pdm.s.pDevHlpQueueR3);
397
398 /*
399 * Critical sections.
400 */
401 pdmR3CritSectRelocate(pVM);
402
403 /*
404 * The registered PIC.
405 */
406 if (pVM->pdm.s.Pic.pDevInsRC)
407 {
408 pVM->pdm.s.Pic.pDevInsRC += offDelta;
409 pVM->pdm.s.Pic.pfnSetIrqRC += offDelta;
410 pVM->pdm.s.Pic.pfnGetInterruptRC += offDelta;
411 }
412
413 /*
414 * The registered APIC.
415 */
416 if (pVM->pdm.s.Apic.pDevInsRC)
417 {
418 pVM->pdm.s.Apic.pDevInsRC += offDelta;
419 pVM->pdm.s.Apic.pfnGetInterruptRC += offDelta;
420 pVM->pdm.s.Apic.pfnSetBaseRC += offDelta;
421 pVM->pdm.s.Apic.pfnGetBaseRC += offDelta;
422 pVM->pdm.s.Apic.pfnSetTPRRC += offDelta;
423 pVM->pdm.s.Apic.pfnGetTPRRC += offDelta;
424 pVM->pdm.s.Apic.pfnBusDeliverRC += offDelta;
425 pVM->pdm.s.Apic.pfnWriteMSRRC += offDelta;
426 pVM->pdm.s.Apic.pfnReadMSRRC += offDelta;
427 }
428
429 /*
430 * The registered I/O APIC.
431 */
432 if (pVM->pdm.s.IoApic.pDevInsRC)
433 {
434 pVM->pdm.s.IoApic.pDevInsRC += offDelta;
435 pVM->pdm.s.IoApic.pfnSetIrqRC += offDelta;
436 }
437
438 /*
439 * The register PCI Buses.
440 */
441 for (unsigned i = 0; i < RT_ELEMENTS(pVM->pdm.s.aPciBuses); i++)
442 {
443 if (pVM->pdm.s.aPciBuses[i].pDevInsRC)
444 {
445 pVM->pdm.s.aPciBuses[i].pDevInsRC += offDelta;
446 pVM->pdm.s.aPciBuses[i].pfnSetIrqRC += offDelta;
447 }
448 }
449
450 /*
451 * Devices.
452 */
453 PCPDMDEVHLPRC pDevHlpRC;
454 int rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_pdmRCDevHlp", &pDevHlpRC);
455 AssertReleaseMsgRC(rc, ("rc=%Rrc when resolving g_pdmRCDevHlp\n", rc));
456 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
457 {
458 if (pDevIns->pDevReg->fFlags & PDM_DEVREG_FLAGS_RC)
459 {
460 pDevIns->pDevHlpRC = pDevHlpRC;
461 pDevIns->pvInstanceDataRC = MMHyperR3ToRC(pVM, pDevIns->pvInstanceDataR3);
462 pDevIns->Internal.s.pVMRC = pVM->pVMRC;
463 if (pDevIns->Internal.s.pPciBusR3)
464 pDevIns->Internal.s.pPciBusRC = MMHyperR3ToRC(pVM, pDevIns->Internal.s.pPciBusR3);
465 if (pDevIns->Internal.s.pPciDeviceR3)
466 pDevIns->Internal.s.pPciDeviceRC = MMHyperR3ToRC(pVM, pDevIns->Internal.s.pPciDeviceR3);
467 if (pDevIns->pDevReg->pfnRelocate)
468 {
469 LogFlow(("PDMR3Relocate: Relocating device '%s'/%d\n",
470 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
471 pDevIns->pDevReg->pfnRelocate(pDevIns, offDelta);
472 }
473 }
474 }
475}
476
477
478/**
479 * Worker for pdmR3Term that terminates a LUN chain.
480 *
481 * @param pVM Pointer to the shared VM structure.
482 * @param pLun The head of the chain.
483 * @param pszDevice The name of the device (for logging).
484 * @param iInstance The device instance number (for logging).
485 */
486static void pdmR3TermLuns(PVM pVM, PPDMLUN pLun, const char *pszDevice, unsigned iInstance)
487{
488 for (; pLun; pLun = pLun->pNext)
489 {
490 /*
491 * Destroy them one at a time from the bottom up.
492 * (The serial device/drivers depends on this - bad.)
493 */
494 PPDMDRVINS pDrvIns = pLun->pBottom;
495 pLun->pBottom = pLun->pTop = NULL;
496 while (pDrvIns)
497 {
498 PPDMDRVINS pDrvNext = pDrvIns->Internal.s.pUp;
499
500 if (pDrvIns->pDrvReg->pfnDestruct)
501 {
502 LogFlow(("pdmR3DevTerm: Destroying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
503 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pszDevice, iInstance));
504 pDrvIns->pDrvReg->pfnDestruct(pDrvIns);
505 }
506
507 TMR3TimerDestroyDriver(pVM, pDrvIns);
508 //PDMR3QueueDestroyDriver(pVM, pDrvIns);
509 //pdmR3ThreadDestroyDriver(pVM, pDrvIns);
510 SSMR3DeregisterDriver(pVM, pDrvIns, NULL, 0);
511
512 pDrvIns = pDrvNext;
513 }
514 }
515}
516
517
518/**
519 * Terminates the PDM.
520 *
521 * Termination means cleaning up and freeing all resources,
522 * the VM it self is at this point powered off or suspended.
523 *
524 * @returns VBox status code.
525 * @param pVM The VM to operate on.
526 */
527VMMR3DECL(int) PDMR3Term(PVM pVM)
528{
529 LogFlow(("PDMR3Term:\n"));
530 AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
531
532 /*
533 * Iterate the device instances and attach drivers, doing
534 * relevant destruction processing.
535 *
536 * N.B. There is no need to mess around freeing memory allocated
537 * from any MM heap since MM will do that in its Term function.
538 */
539 /* usb ones first. */
540 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
541 {
542 pdmR3TermLuns(pVM, pUsbIns->Internal.s.pLuns, pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance);
543
544 if (pUsbIns->pUsbReg->pfnDestruct)
545 {
546 LogFlow(("pdmR3DevTerm: Destroying - device '%s'/%d\n",
547 pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
548 pUsbIns->pUsbReg->pfnDestruct(pUsbIns);
549 }
550
551 //TMR3TimerDestroyUsb(pVM, pUsbIns);
552 //SSMR3DeregisterUsb(pVM, pUsbIns, NULL, 0);
553 pdmR3ThreadDestroyUsb(pVM, pUsbIns);
554 }
555
556 /* then the 'normal' ones. */
557 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
558 {
559 pdmR3TermLuns(pVM, pDevIns->Internal.s.pLunsR3, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance);
560
561 if (pDevIns->pDevReg->pfnDestruct)
562 {
563 LogFlow(("pdmR3DevTerm: Destroying - device '%s'/%d\n",
564 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
565 pDevIns->pDevReg->pfnDestruct(pDevIns);
566 }
567
568 TMR3TimerDestroyDevice(pVM, pDevIns);
569 //SSMR3DeregisterDriver(pVM, pDevIns, NULL, 0);
570 pdmR3CritSectDeleteDevice(pVM, pDevIns);
571 //pdmR3ThreadDestroyDevice(pVM, pDevIns);
572 //PDMR3QueueDestroyDevice(pVM, pDevIns);
573 PGMR3PhysMMIO2Deregister(pVM, pDevIns, UINT32_MAX);
574 }
575
576 /*
577 * Destroy all threads.
578 */
579 pdmR3ThreadDestroyAll(pVM);
580
581#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
582 /*
583 * Free async completion managers.
584 */
585 pdmR3AsyncCompletionTerm(pVM);
586#endif
587
588 /*
589 * Free modules.
590 */
591 pdmR3LdrTermU(pVM->pUVM);
592
593 /*
594 * Destroy the PDM lock.
595 */
596 PDMR3CritSectDelete(&pVM->pdm.s.CritSect);
597 /* The MiscCritSect is deleted by PDMR3CritSectTerm. */
598
599 LogFlow(("PDMR3Term: returns %Rrc\n", VINF_SUCCESS));
600 return VINF_SUCCESS;
601}
602
603
604/**
605 * Terminates the PDM part of the UVM.
606 *
607 * This will unload any modules left behind.
608 *
609 * @param pUVM Pointer to the user mode VM structure.
610 */
611VMMR3DECL(void) PDMR3TermUVM(PUVM pUVM)
612{
613 /*
614 * In the normal cause of events we will now call pdmR3LdrTermU for
615 * the second time. In the case of init failure however, this might
616 * the first time, which is why we do it.
617 */
618 pdmR3LdrTermU(pUVM);
619}
620
621
622
623
624
625/**
626 * Execute state save operation.
627 *
628 * @returns VBox status code.
629 * @param pVM VM Handle.
630 * @param pSSM SSM operation handle.
631 */
632static DECLCALLBACK(int) pdmR3Save(PVM pVM, PSSMHANDLE pSSM)
633{
634 LogFlow(("pdmR3Save:\n"));
635
636 /*
637 * Save interrupt and DMA states.
638 */
639 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
640 {
641 PVMCPU pVCpu = &pVM->aCpus[idCpu];
642 SSMR3PutUInt(pSSM, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_APIC));
643 SSMR3PutUInt(pSSM, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_PIC));
644 SSMR3PutUInt(pSSM, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_NMI));
645 SSMR3PutUInt(pSSM, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_SMI));
646 }
647 SSMR3PutUInt(pSSM, VM_FF_ISSET(pVM, VM_FF_PDM_DMA));
648
649 /*
650 * Save the list of device instances so we can check that
651 * they're all still there when we load the state and that
652 * nothing new have been added.
653 */
654 /** @todo We might have to filter out some device classes, like USB attached devices. */
655 uint32_t i = 0;
656 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3, i++)
657 {
658 SSMR3PutU32(pSSM, i);
659 SSMR3PutStrZ(pSSM, pDevIns->pDevReg->szDeviceName);
660 SSMR3PutU32(pSSM, pDevIns->iInstance);
661 }
662 return SSMR3PutU32(pSSM, ~0); /* terminator */
663}
664
665
666/**
667 * Prepare state load operation.
668 *
669 * This will dispatch pending operations and clear the FFs governed by PDM and its devices.
670 *
671 * @returns VBox status code.
672 * @param pVM The VM handle.
673 * @param pSSM The SSM handle.
674 */
675static DECLCALLBACK(int) pdmR3LoadPrep(PVM pVM, PSSMHANDLE pSSM)
676{
677 LogFlow(("pdmR3LoadPrep: %s%s%s%s\n",
678 VM_FF_ISSET(pVM, VM_FF_PDM_QUEUES) ? " VM_FF_PDM_QUEUES" : "",
679 VM_FF_ISSET(pVM, VM_FF_PDM_DMA) ? " VM_FF_PDM_DMA" : ""
680 ));
681#ifdef LOG_ENABLED
682 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
683 {
684 PVMCPU pVCpu = &pVM->aCpus[idCpu];
685 LogFlow(("pdmR3LoadPrep: VCPU %d %s%s%s%s\n", idCpu,
686 VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_APIC) ? " VMCPU_FF_INTERRUPT_APIC" : "",
687 VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_PIC) ? " VMCPU_FF_INTERRUPT_PIC" : ""
688 ));
689 }
690#endif
691
692 /*
693 * In case there is work pending that will raise an interrupt,
694 * start a DMA transfer, or release a lock. (unlikely)
695 */
696 if (VM_FF_ISSET(pVM, VM_FF_PDM_QUEUES))
697 PDMR3QueueFlushAll(pVM);
698
699 /* Clear the FFs. */
700 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
701 {
702 PVMCPU pVCpu = &pVM->aCpus[idCpu];
703 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_APIC);
704 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_PIC);
705 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_NMI);
706 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_SMI);
707 }
708 VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
709
710 return VINF_SUCCESS;
711}
712
713
714/**
715 * Execute state load operation.
716 *
717 * @returns VBox status code.
718 * @param pVM VM Handle.
719 * @param pSSM SSM operation handle.
720 * @param uVersion Data layout version.
721 * @param uPass The data pass.
722 */
723static DECLCALLBACK(int) pdmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
724{
725 int rc;
726
727 LogFlow(("pdmR3Load:\n"));
728 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
729
730 /*
731 * Validate version.
732 */
733 if ( uVersion != PDM_SAVED_STATE_VERSION
734 && uVersion != PDM_SAVED_STATE_VERSION_PRE_NMI_FF)
735 {
736 AssertMsgFailed(("pdmR3Load: Invalid version uVersion=%d!\n", uVersion));
737 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
738 }
739
740 /*
741 * Load the interrupt and DMA states.
742 */
743 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
744 {
745 PVMCPU pVCpu = &pVM->aCpus[idCpu];
746
747 /* APIC interrupt */
748 RTUINT fInterruptPending = 0;
749 rc = SSMR3GetUInt(pSSM, &fInterruptPending);
750 if (RT_FAILURE(rc))
751 return rc;
752 if (fInterruptPending & ~1)
753 {
754 AssertMsgFailed(("fInterruptPending=%#x (APIC)\n", fInterruptPending));
755 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
756 }
757 AssertRelease(!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_APIC));
758 if (fInterruptPending)
759 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC);
760
761 /* PIC interrupt */
762 fInterruptPending = 0;
763 rc = SSMR3GetUInt(pSSM, &fInterruptPending);
764 if (RT_FAILURE(rc))
765 return rc;
766 if (fInterruptPending & ~1)
767 {
768 AssertMsgFailed(("fInterruptPending=%#x (PIC)\n", fInterruptPending));
769 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
770 }
771 AssertRelease(!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_PIC));
772 if (fInterruptPending)
773 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_PIC);
774
775 if (uVersion > PDM_SAVED_STATE_VERSION_PRE_NMI_FF)
776 {
777 /* NMI interrupt */
778 RTUINT fInterruptPending = 0;
779 rc = SSMR3GetUInt(pSSM, &fInterruptPending);
780 if (RT_FAILURE(rc))
781 return rc;
782 if (fInterruptPending & ~1)
783 {
784 AssertMsgFailed(("fInterruptPending=%#x (NMI)\n", fInterruptPending));
785 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
786 }
787 AssertRelease(!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_NMI));
788 if (fInterruptPending)
789 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_NMI);
790
791 /* SMI interrupt */
792 fInterruptPending = 0;
793 rc = SSMR3GetUInt(pSSM, &fInterruptPending);
794 if (RT_FAILURE(rc))
795 return rc;
796 if (fInterruptPending & ~1)
797 {
798 AssertMsgFailed(("fInterruptPending=%#x (SMI)\n", fInterruptPending));
799 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
800 }
801 AssertRelease(!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_SMI));
802 if (fInterruptPending)
803 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_SMI);
804 }
805 }
806
807 /* DMA pending */
808 RTUINT fDMAPending = 0;
809 rc = SSMR3GetUInt(pSSM, &fDMAPending);
810 if (RT_FAILURE(rc))
811 return rc;
812 if (fDMAPending & ~1)
813 {
814 AssertMsgFailed(("fDMAPending=%#x\n", fDMAPending));
815 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
816 }
817 AssertRelease(!VM_FF_ISSET(pVM, VM_FF_PDM_DMA));
818 if (fDMAPending)
819 VM_FF_SET(pVM, VM_FF_PDM_DMA);
820
821 /*
822 * Load the list of devices and verify that they are all there.
823 *
824 * We boldly ASSUME that the order is fixed and that it's a good, this
825 * makes it way easier to validate...
826 */
827 uint32_t i = 0;
828 PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances;
829 for (;; pDevIns = pDevIns->Internal.s.pNextR3, i++)
830 {
831 /* Get the separator / terminator. */
832 uint32_t u32Sep;
833 int rc = SSMR3GetU32(pSSM, &u32Sep);
834 if (RT_FAILURE(rc))
835 return rc;
836 if (u32Sep == (uint32_t)~0)
837 break;
838 if (u32Sep != i)
839 AssertMsgFailedReturn(("Out of seqence. u32Sep=%#x i=%#x\n", u32Sep, i), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
840
841 /* get the name and instance number. */
842 char szDeviceName[sizeof(pDevIns->pDevReg->szDeviceName)];
843 rc = SSMR3GetStrZ(pSSM, szDeviceName, sizeof(szDeviceName));
844 if (RT_FAILURE(rc))
845 return rc;
846 RTUINT iInstance;
847 rc = SSMR3GetUInt(pSSM, &iInstance);
848 if (RT_FAILURE(rc))
849 return rc;
850
851 /* compare */
852 if (!pDevIns)
853 {
854 LogRel(("Device '%s'/%d not found in current config\n", szDeviceName, iInstance));
855 if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
856 AssertFailedReturn(VERR_SSM_LOAD_CONFIG_MISMATCH);
857 break;
858 }
859 if ( strcmp(szDeviceName, pDevIns->pDevReg->szDeviceName)
860 || pDevIns->iInstance != iInstance)
861 {
862 LogRel(("u32Sep=%d loaded '%s'/%d configured '%s'/%d\n",
863 u32Sep, szDeviceName, iInstance, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
864 if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
865 AssertFailedReturn(VERR_SSM_LOAD_CONFIG_MISMATCH);
866 }
867 }
868
869 /*
870 * Too many devices?
871 */
872 if (pDevIns)
873 {
874 LogRel(("Device '%s'/%d not found in saved state\n", pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
875 if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
876 AssertFailedReturn(VERR_SSM_LOAD_CONFIG_MISMATCH);
877 }
878
879 return VINF_SUCCESS;
880}
881
882
883/**
884 * This function will notify all the devices and their
885 * attached drivers about the VM now being powered on.
886 *
887 * @param pVM VM Handle.
888 */
889VMMR3DECL(void) PDMR3PowerOn(PVM pVM)
890{
891 LogFlow(("PDMR3PowerOn:\n"));
892
893 /*
894 * Iterate the device instances.
895 * The attached drivers are processed first.
896 */
897 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
898 {
899 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
900 /** @todo Inverse the order here? */
901 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
902 if (pDrvIns->pDrvReg->pfnPowerOn)
903 {
904 LogFlow(("PDMR3PowerOn: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
905 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
906 pDrvIns->pDrvReg->pfnPowerOn(pDrvIns);
907 }
908
909 if (pDevIns->pDevReg->pfnPowerOn)
910 {
911 LogFlow(("PDMR3PowerOn: Notifying - device '%s'/%d\n",
912 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
913 pDevIns->pDevReg->pfnPowerOn(pDevIns);
914 }
915 }
916
917#ifdef VBOX_WITH_USB
918 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
919 {
920 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
921 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
922 if (pDrvIns->pDrvReg->pfnPowerOn)
923 {
924 LogFlow(("PDMR3PowerOn: Notifying - driver '%s'/%d on LUN#%d of usb device '%s'/%d\n",
925 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
926 pDrvIns->pDrvReg->pfnPowerOn(pDrvIns);
927 }
928
929 if (pUsbIns->pUsbReg->pfnVMPowerOn)
930 {
931 LogFlow(("PDMR3PowerOn: Notifying - device '%s'/%d\n",
932 pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
933 pUsbIns->pUsbReg->pfnVMPowerOn(pUsbIns);
934 }
935 }
936#endif
937
938 /*
939 * Resume all threads.
940 */
941 pdmR3ThreadResumeAll(pVM);
942
943 LogFlow(("PDMR3PowerOn: returns void\n"));
944}
945
946
947
948
949/**
950 * This function will notify all the devices and their
951 * attached drivers about the VM now being reset.
952 *
953 * @param pVM VM Handle.
954 */
955VMMR3DECL(void) PDMR3Reset(PVM pVM)
956{
957 LogFlow(("PDMR3Reset:\n"));
958
959 /*
960 * Clear all pending interrupts and DMA operations.
961 */
962 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
963 {
964 PVMCPU pVCpu = &pVM->aCpus[idCpu];
965 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_APIC);
966 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_PIC);
967 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_NMI);
968 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_SMI);
969 }
970 VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
971
972 /*
973 * Iterate the device instances.
974 * The attached drivers are processed first.
975 */
976 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
977 {
978 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
979 /** @todo Inverse the order here? */
980 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
981 if (pDrvIns->pDrvReg->pfnReset)
982 {
983 LogFlow(("PDMR3Reset: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
984 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
985 pDrvIns->pDrvReg->pfnReset(pDrvIns);
986 }
987
988 if (pDevIns->pDevReg->pfnReset)
989 {
990 LogFlow(("PDMR3Reset: Notifying - device '%s'/%d\n",
991 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
992 pDevIns->pDevReg->pfnReset(pDevIns);
993 }
994 }
995
996#ifdef VBOX_WITH_USB
997 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
998 {
999 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1000 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1001 if (pDrvIns->pDrvReg->pfnReset)
1002 {
1003 LogFlow(("PDMR3Reset: Notifying - driver '%s'/%d on LUN#%d of usb device '%s'/%d\n",
1004 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
1005 pDrvIns->pDrvReg->pfnReset(pDrvIns);
1006 }
1007
1008 if (pUsbIns->pUsbReg->pfnVMReset)
1009 {
1010 LogFlow(("PDMR3Reset: Notifying - device '%s'/%d\n",
1011 pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
1012 pUsbIns->pUsbReg->pfnVMReset(pUsbIns);
1013 }
1014 }
1015#endif
1016
1017 LogFlow(("PDMR3Reset: returns void\n"));
1018}
1019
1020
1021/**
1022 * This function will notify all the devices and their
1023 * attached drivers about the VM now being reset.
1024 *
1025 * @param pVM VM Handle.
1026 */
1027VMMR3DECL(void) PDMR3Suspend(PVM pVM)
1028{
1029 LogFlow(("PDMR3Suspend:\n"));
1030
1031 /*
1032 * Iterate the device instances.
1033 * The attached drivers are processed first.
1034 */
1035 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1036 {
1037 /*
1038 * Some devices need to be notified first that the VM is suspended to ensure that that there are no pending
1039 * requests from the guest which are still processed. Calling the drivers before these requests are finished
1040 * might lead to errors otherwise. One example is the SATA controller which might still have I/O requests
1041 * pending. But DrvVD sets the files into readonly mode and every request will fail then.
1042 */
1043 if (pDevIns->pDevReg->pfnSuspend && (pDevIns->pDevReg->fFlags & PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION))
1044 {
1045 LogFlow(("PDMR3Suspend: Notifying - device '%s'/%d\n",
1046 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
1047 pDevIns->pDevReg->pfnSuspend(pDevIns);
1048 }
1049
1050 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1051 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1052 if (pDrvIns->pDrvReg->pfnSuspend)
1053 {
1054 LogFlow(("PDMR3Suspend: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1055 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
1056 pDrvIns->pDrvReg->pfnSuspend(pDrvIns);
1057 }
1058
1059 /* Don't call the suspend notification again if it was already called. */
1060 if (pDevIns->pDevReg->pfnSuspend && !(pDevIns->pDevReg->fFlags & PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION))
1061 {
1062 LogFlow(("PDMR3Suspend: Notifying - device '%s'/%d\n",
1063 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
1064 pDevIns->pDevReg->pfnSuspend(pDevIns);
1065 }
1066 }
1067
1068#ifdef VBOX_WITH_USB
1069 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1070 {
1071 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1072 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1073 if (pDrvIns->pDrvReg->pfnSuspend)
1074 {
1075 LogFlow(("PDMR3Suspend: Notifying - driver '%s'/%d on LUN#%d of usb device '%s'/%d\n",
1076 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
1077 pDrvIns->pDrvReg->pfnSuspend(pDrvIns);
1078 }
1079
1080 if (pUsbIns->pUsbReg->pfnVMSuspend)
1081 {
1082 LogFlow(("PDMR3Suspend: Notifying - device '%s'/%d\n",
1083 pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
1084 pUsbIns->pUsbReg->pfnVMSuspend(pUsbIns);
1085 }
1086 }
1087#endif
1088
1089 /*
1090 * Suspend all threads.
1091 */
1092 pdmR3ThreadSuspendAll(pVM);
1093
1094 LogFlow(("PDMR3Suspend: returns void\n"));
1095}
1096
1097
1098/**
1099 * This function will notify all the devices and their
1100 * attached drivers about the VM now being resumed.
1101 *
1102 * @param pVM VM Handle.
1103 */
1104VMMR3DECL(void) PDMR3Resume(PVM pVM)
1105{
1106 LogFlow(("PDMR3Resume:\n"));
1107
1108 /*
1109 * Iterate the device instances.
1110 * The attached drivers are processed first.
1111 */
1112 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1113 {
1114 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1115 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1116 if (pDrvIns->pDrvReg->pfnResume)
1117 {
1118 LogFlow(("PDMR3Resume: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1119 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
1120 pDrvIns->pDrvReg->pfnResume(pDrvIns);
1121 }
1122
1123 if (pDevIns->pDevReg->pfnResume)
1124 {
1125 LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n",
1126 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
1127 pDevIns->pDevReg->pfnResume(pDevIns);
1128 }
1129 }
1130
1131#ifdef VBOX_WITH_USB
1132 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1133 {
1134 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1135 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1136 if (pDrvIns->pDrvReg->pfnResume)
1137 {
1138 LogFlow(("PDMR3Resume: Notifying - driver '%s'/%d on LUN#%d of usb device '%s'/%d\n",
1139 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
1140 pDrvIns->pDrvReg->pfnResume(pDrvIns);
1141 }
1142
1143 if (pUsbIns->pUsbReg->pfnVMResume)
1144 {
1145 LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n",
1146 pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
1147 pUsbIns->pUsbReg->pfnVMResume(pUsbIns);
1148 }
1149 }
1150#endif
1151
1152 /*
1153 * Resume all threads.
1154 */
1155 pdmR3ThreadResumeAll(pVM);
1156
1157 LogFlow(("PDMR3Resume: returns void\n"));
1158}
1159
1160
1161/**
1162 * This function will notify all the devices and their
1163 * attached drivers about the VM being powered off.
1164 *
1165 * @param pVM VM Handle.
1166 */
1167VMMR3DECL(void) PDMR3PowerOff(PVM pVM)
1168{
1169 LogFlow(("PDMR3PowerOff:\n"));
1170
1171 /*
1172 * Iterate the device instances.
1173 * The attached drivers are processed first.
1174 */
1175 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1176 {
1177
1178 if (pDevIns->pDevReg->pfnPowerOff && (pDevIns->pDevReg->fFlags & PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION))
1179 {
1180 LogFlow(("PDMR3PowerOff: Notifying - device '%s'/%d\n",
1181 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
1182 pDevIns->pDevReg->pfnPowerOff(pDevIns);
1183 }
1184
1185 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1186 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1187 if (pDrvIns->pDrvReg->pfnPowerOff)
1188 {
1189 LogFlow(("PDMR3PowerOff: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1190 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
1191 pDrvIns->pDrvReg->pfnPowerOff(pDrvIns);
1192 }
1193
1194 if (pDevIns->pDevReg->pfnPowerOff && !(pDevIns->pDevReg->fFlags & PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION))
1195 {
1196 LogFlow(("PDMR3PowerOff: Notifying - device '%s'/%d\n",
1197 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
1198 pDevIns->pDevReg->pfnPowerOff(pDevIns);
1199 }
1200 }
1201
1202#ifdef VBOX_WITH_USB
1203 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1204 {
1205 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1206 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1207 if (pDrvIns->pDrvReg->pfnPowerOff)
1208 {
1209 LogFlow(("PDMR3PowerOff: Notifying - driver '%s'/%d on LUN#%d of usb device '%s'/%d\n",
1210 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
1211 pDrvIns->pDrvReg->pfnPowerOff(pDrvIns);
1212 }
1213
1214 if (pUsbIns->pUsbReg->pfnVMPowerOff)
1215 {
1216 LogFlow(("PDMR3PowerOff: Notifying - device '%s'/%d\n",
1217 pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
1218 pUsbIns->pUsbReg->pfnVMPowerOff(pUsbIns);
1219 }
1220 }
1221#endif
1222
1223 /*
1224 * Suspend all threads.
1225 */
1226 pdmR3ThreadSuspendAll(pVM);
1227
1228 LogFlow(("PDMR3PowerOff: returns void\n"));
1229}
1230
1231
1232/**
1233 * Queries the base interace of a device instance.
1234 *
1235 * The caller can use this to query other interfaces the device implements
1236 * and use them to talk to the device.
1237 *
1238 * @returns VBox status code.
1239 * @param pVM VM handle.
1240 * @param pszDevice Device name.
1241 * @param iInstance Device instance.
1242 * @param ppBase Where to store the pointer to the base device interface on success.
1243 * @remark We're not doing any locking ATM, so don't try call this at times when the
1244 * device chain is known to be updated.
1245 */
1246VMMR3DECL(int) PDMR3QueryDevice(PVM pVM, const char *pszDevice, unsigned iInstance, PPDMIBASE *ppBase)
1247{
1248 LogFlow(("PDMR3DeviceQuery: pszDevice=%p:{%s} iInstance=%u ppBase=%p\n", pszDevice, pszDevice, iInstance, ppBase));
1249
1250 /*
1251 * Iterate registered devices looking for the device.
1252 */
1253 size_t cchDevice = strlen(pszDevice);
1254 for (PPDMDEV pDev = pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
1255 {
1256 if ( pDev->cchName == cchDevice
1257 && !memcmp(pDev->pDevReg->szDeviceName, pszDevice, cchDevice))
1258 {
1259 /*
1260 * Iterate device instances.
1261 */
1262 for (PPDMDEVINS pDevIns = pDev->pInstances; pDevIns; pDevIns = pDevIns->Internal.s.pPerDeviceNextR3)
1263 {
1264 if (pDevIns->iInstance == iInstance)
1265 {
1266 if (pDevIns->IBase.pfnQueryInterface)
1267 {
1268 *ppBase = &pDevIns->IBase;
1269 LogFlow(("PDMR3DeviceQuery: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
1270 return VINF_SUCCESS;
1271 }
1272
1273 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NO_IBASE\n"));
1274 return VERR_PDM_DEVICE_INSTANCE_NO_IBASE;
1275 }
1276 }
1277
1278 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NOT_FOUND\n"));
1279 return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND;
1280 }
1281 }
1282
1283 LogFlow(("PDMR3QueryDevice: returns VERR_PDM_DEVICE_NOT_FOUND\n"));
1284 return VERR_PDM_DEVICE_NOT_FOUND;
1285}
1286
1287
1288/**
1289 * Queries the base interface of a device LUN.
1290 *
1291 * This differs from PDMR3QueryLun by that it returns the interface on the
1292 * device and not the top level driver.
1293 *
1294 * @returns VBox status code.
1295 * @param pVM VM Handle.
1296 * @param pszDevice Device name.
1297 * @param iInstance Device instance.
1298 * @param iLun The Logical Unit to obtain the interface of.
1299 * @param ppBase Where to store the base interface pointer.
1300 * @remark We're not doing any locking ATM, so don't try call this at times when the
1301 * device chain is known to be updated.
1302 */
1303VMMR3DECL(int) PDMR3QueryDeviceLun(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
1304{
1305 LogFlow(("PDMR3QueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
1306 pszDevice, pszDevice, iInstance, iLun, ppBase));
1307
1308 /*
1309 * Find the LUN.
1310 */
1311 PPDMLUN pLun;
1312 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
1313 if (RT_SUCCESS(rc))
1314 {
1315 *ppBase = pLun->pBase;
1316 LogFlow(("PDMR3QueryDeviceLun: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
1317 return VINF_SUCCESS;
1318 }
1319 LogFlow(("PDMR3QueryDeviceLun: returns %Rrc\n", rc));
1320 return rc;
1321}
1322
1323
1324/**
1325 * Query the interface of the top level driver on a LUN.
1326 *
1327 * @returns VBox status code.
1328 * @param pVM VM Handle.
1329 * @param pszDevice Device name.
1330 * @param iInstance Device instance.
1331 * @param iLun The Logical Unit to obtain the interface of.
1332 * @param ppBase Where to store the base interface pointer.
1333 * @remark We're not doing any locking ATM, so don't try call this at times when the
1334 * device chain is known to be updated.
1335 */
1336VMMR3DECL(int) PDMR3QueryLun(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
1337{
1338 LogFlow(("PDMR3QueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
1339 pszDevice, pszDevice, iInstance, iLun, ppBase));
1340
1341 /*
1342 * Find the LUN.
1343 */
1344 PPDMLUN pLun;
1345 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
1346 if (RT_SUCCESS(rc))
1347 {
1348 if (pLun->pTop)
1349 {
1350 *ppBase = &pLun->pTop->IBase;
1351 LogFlow(("PDMR3QueryLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
1352 return VINF_SUCCESS;
1353 }
1354 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
1355 }
1356 LogFlow(("PDMR3QueryLun: returns %Rrc\n", rc));
1357 return rc;
1358}
1359
1360/**
1361 * Executes pending DMA transfers.
1362 * Forced Action handler.
1363 *
1364 * @param pVM VM handle.
1365 */
1366VMMR3DECL(void) PDMR3DmaRun(PVM pVM)
1367{
1368 /* Note! Not really SMP safe; restrict it to VCPU 0. */
1369 if (VMMGetCpuId(pVM) != 0)
1370 return;
1371
1372 if (VM_FF_TESTANDCLEAR(pVM, VM_FF_PDM_DMA))
1373 {
1374 if (pVM->pdm.s.pDmac)
1375 {
1376 bool fMore = pVM->pdm.s.pDmac->Reg.pfnRun(pVM->pdm.s.pDmac->pDevIns);
1377 if (fMore)
1378 VM_FF_SET(pVM, VM_FF_PDM_DMA);
1379 }
1380 }
1381}
1382
1383
1384/**
1385 * Service a VMMCALLRING3_PDM_LOCK call.
1386 *
1387 * @returns VBox status code.
1388 * @param pVM The VM handle.
1389 */
1390VMMR3DECL(int) PDMR3LockCall(PVM pVM)
1391{
1392 return PDMR3CritSectEnterEx(&pVM->pdm.s.CritSect, true /* fHostCall */);
1393}
1394
1395
1396/**
1397 * Registers the VMM device heap
1398 *
1399 * @returns VBox status code.
1400 * @param pVM VM handle.
1401 * @param GCPhys The physical address.
1402 * @param pvHeap Ring-3 pointer.
1403 * @param cbSize Size of the heap.
1404 */
1405VMMR3DECL(int) PDMR3RegisterVMMDevHeap(PVM pVM, RTGCPHYS GCPhys, RTR3PTR pvHeap, unsigned cbSize)
1406{
1407 Assert(pVM->pdm.s.pvVMMDevHeap == NULL);
1408
1409 Log(("PDMR3RegisterVMMDevHeap %RGp %RHv %x\n", GCPhys, pvHeap, cbSize));
1410 pVM->pdm.s.pvVMMDevHeap = pvHeap;
1411 pVM->pdm.s.GCPhysVMMDevHeap = GCPhys;
1412 pVM->pdm.s.cbVMMDevHeap = cbSize;
1413 pVM->pdm.s.cbVMMDevHeapLeft = cbSize;
1414 return VINF_SUCCESS;
1415}
1416
1417
1418/**
1419 * Unregisters the VMM device heap
1420 *
1421 * @returns VBox status code.
1422 * @param pVM VM handle.
1423 * @param GCPhys The physical address.
1424 */
1425VMMR3DECL(int) PDMR3UnregisterVMMDevHeap(PVM pVM, RTGCPHYS GCPhys)
1426{
1427 Assert(pVM->pdm.s.GCPhysVMMDevHeap == GCPhys);
1428
1429 Log(("PDMR3UnregisterVMMDevHeap %RGp\n", GCPhys));
1430 pVM->pdm.s.pvVMMDevHeap = NULL;
1431 pVM->pdm.s.GCPhysVMMDevHeap = NIL_RTGCPHYS;
1432 pVM->pdm.s.cbVMMDevHeap = 0;
1433 pVM->pdm.s.cbVMMDevHeapLeft = 0;
1434 return VINF_SUCCESS;
1435}
1436
1437
1438/**
1439 * Allocates memory from the VMM device heap
1440 *
1441 * @returns VBox status code.
1442 * @param pVM VM handle.
1443 * @param cbSize Allocation size.
1444 * @param pv Ring-3 pointer. (out)
1445 */
1446VMMR3DECL(int) PDMR3VMMDevHeapAlloc(PVM pVM, unsigned cbSize, RTR3PTR *ppv)
1447{
1448#ifdef DEBUG_bird
1449 if (!cbSize || cbSize > pVM->pdm.s.cbVMMDevHeapLeft)
1450 return VERR_NO_MEMORY;
1451#else
1452 AssertReturn(cbSize && cbSize <= pVM->pdm.s.cbVMMDevHeapLeft, VERR_NO_MEMORY);
1453#endif
1454
1455 Log(("PDMR3VMMDevHeapAlloc %x\n", cbSize));
1456
1457 /** @todo not a real heap as there's currently only one user. */
1458 *ppv = pVM->pdm.s.pvVMMDevHeap;
1459 pVM->pdm.s.cbVMMDevHeapLeft = 0;
1460 return VINF_SUCCESS;
1461}
1462
1463
1464/**
1465 * Frees memory from the VMM device heap
1466 *
1467 * @returns VBox status code.
1468 * @param pVM VM handle.
1469 * @param pv Ring-3 pointer.
1470 */
1471VMMR3DECL(int) PDMR3VMMDevHeapFree(PVM pVM, RTR3PTR pv)
1472{
1473 Log(("PDMR3VMMDevHeapFree %RHv\n", pv));
1474
1475 /** @todo not a real heap as there's currently only one user. */
1476 pVM->pdm.s.cbVMMDevHeapLeft = pVM->pdm.s.cbVMMDevHeap;
1477 return VINF_SUCCESS;
1478}
1479
1480/**
1481 * Release the PDM lock if owned by the current VCPU
1482 *
1483 * @param pVM The VM to operate on.
1484 */
1485VMMR3DECL(void) PDMR3ReleaseOwnedLocks(PVM pVM)
1486{
1487 while (PDMCritSectIsOwner(&pVM->pdm.s.CritSect))
1488 PDMCritSectLeave(&pVM->pdm.s.CritSect);
1489}
Note: See TracBrowser for help on using the repository browser.

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