VirtualBox

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

Last change on this file since 80191 was 80191, checked in by vboxsync, 5 years ago

VMM/r3: Refactored VMCPU enumeration in preparation that aCpus will be replaced with a pointer array. Removed two raw-mode offset members from the CPUM and CPUMCPU sub-structures. bugref:9217 bugref:9517

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 110.5 KB
Line 
1/* $Id: PDM.cpp 80191 2019-08-08 00:36:57Z vboxsync $ */
2/** @file
3 * PDM - Pluggable Device Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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 * The PDM handles devices and their drivers in a flexible and dynamic manner.
22 *
23 * VirtualBox is designed to be very configurable, i.e. the ability to select
24 * virtual devices and configure them uniquely for a VM. For this reason
25 * virtual devices are not statically linked with the VMM but loaded, linked and
26 * instantiated at runtime by PDM using the information found in the
27 * Configuration Manager (CFGM).
28 *
29 * While the chief purpose of PDM is to manager of devices their drivers, it
30 * also serves as somewhere to put usful things like cross context queues, cross
31 * context synchronization (like critsect), VM centric thread management,
32 * asynchronous I/O framework, and so on.
33 *
34 * @sa @ref grp_pdm
35 * @subpage pg_pdm_block_cache
36 *
37 *
38 * @section sec_pdm_dev The Pluggable Devices
39 *
40 * Devices register themselves when the module containing them is loaded. PDM
41 * will call the entry point 'VBoxDevicesRegister' when loading a device module.
42 * The device module will then use the supplied callback table to check the VMM
43 * version and to register its devices. Each device has an unique name (within
44 * the VM configuration anyway). The name is not only used in PDM, but also in
45 * CFGM to organize device and device instance settings, and by anyone who wants
46 * to talk to a specific device instance.
47 *
48 * When all device modules have been successfully loaded PDM will instantiate
49 * those devices which are configured for the VM. Note that a device may have
50 * more than one instance, take network adaptors as an example. When
51 * instantiating a device PDM provides device instance memory and a callback
52 * table (aka Device Helpers / DevHlp) with the VM APIs which the device
53 * instance is trusted with.
54 *
55 * Some devices are trusted devices, most are not. The trusted devices are an
56 * integrated part of the VM and can obtain the VM handle, thus enabling them to
57 * call any VM API. Untrusted devices can only use the callbacks provided
58 * during device instantiation.
59 *
60 * The main purpose in having DevHlps rather than just giving all the devices
61 * the VM handle and let them call the internal VM APIs directly, is both to
62 * create a binary interface that can be supported across releases and to
63 * create a barrier between devices and the VM. (The trusted / untrusted bit
64 * hasn't turned out to be of much use btw., but it's easy to maintain so there
65 * isn't any point in removing it.)
66 *
67 * A device can provide a ring-0 and/or a raw-mode context extension to improve
68 * the VM performance by handling exits and traps (respectively) without
69 * requiring context switches (to ring-3). Callbacks for MMIO and I/O ports
70 * need to be registered specifically for the additional contexts for this to
71 * make sense. Also, the device has to be trusted to be loaded into R0/RC
72 * because of the extra privilege it entails. Note that raw-mode code and data
73 * will be subject to relocation.
74 *
75 *
76 * @subsection sec_pdm_dev_pci PCI Devices
77 *
78 * A PDM device usually registers one a PCI device during it's instantiation,
79 * legacy devices may register zero, while a few (currently none) more
80 * complicated devices may register multiple PCI functions or devices.
81 *
82 * The bus, device and function assignments can either be done explictly via the
83 * configuration or the registration call, or it can be left up to the PCI bus.
84 * The typical VBox configuration construct (ConsoleImpl2.cpp) will do explict
85 * assignments for all devices it's BusAssignmentManager class knows about.
86 *
87 * For explict CFGM style configuration, the "PCIBusNo", "PCIDeviceNo", and
88 * "PCIFunctionNo" values in the PDM device instance configuration (not the
89 * "config" subkey, but the top level one) will be picked up for the primary PCI
90 * device. The primary PCI configuration is by default the first one, but this
91 * can be controlled using the @a idxDevCfg parameter of the
92 * PDMDEVHLPR3::pfnPCIRegister method. For subsequent configuration (@a
93 * idxDevCfg > 0) the values are taken from the "PciDevNN" subkey, where "NN" is
94 * replaced by the @a idxDevCfg value.
95 *
96 * There's currently a limit of 256 PCI devices per PDM device.
97 *
98 *
99 * @section sec_pdm_special_devs Special Devices
100 *
101 * Several kinds of devices interacts with the VMM and/or other device and PDM
102 * will work like a mediator for these. The typical pattern is that the device
103 * calls a special registration device helper with a set of callbacks, PDM
104 * responds by copying this and providing a pointer to a set helper callbacks
105 * for that particular kind of device. Unlike interfaces where the callback
106 * table pointer is used a 'this' pointer, these arrangements will use the
107 * device instance pointer (PPDMDEVINS) as a kind of 'this' pointer.
108 *
109 * For an example of this kind of setup, see the PIC. The PIC registers itself
110 * by calling PDMDEVHLPR3::pfnPICRegister. PDM saves the device instance,
111 * copies the callback tables (PDMPICREG), resolving the ring-0 and raw-mode
112 * addresses in the process, and hands back the pointer to a set of helper
113 * methods (PDMPICHLPR3). The PCI device then queries the ring-0 and raw-mode
114 * helpers using PDMPICHLPR3::pfnGetR0Helpers and PDMPICHLPR3::pfnGetRCHelpers.
115 * The PCI device repeats ths pfnGetRCHelpers call in it's relocation method
116 * since the address changes when RC is relocated.
117 *
118 * @see grp_pdm_device
119 *
120 *
121 * @section sec_pdm_usbdev The Pluggable USB Devices
122 *
123 * USB devices are handled a little bit differently than other devices. The
124 * general concepts wrt. pluggability are mostly the same, but the details
125 * varies. The registration entry point is 'VBoxUsbRegister', the device
126 * instance is PDMUSBINS and the callbacks helpers are different. Also, USB
127 * device are restricted to ring-3 and cannot have any ring-0 or raw-mode
128 * extensions (at least not yet).
129 *
130 * The way USB devices work differs greatly from other devices though since they
131 * aren't attaches directly to the PCI/ISA/whatever system buses but via a
132 * USB host control (OHCI, UHCI or EHCI). USB devices handle USB requests
133 * (URBs) and does not register I/O ports, MMIO ranges or PCI bus
134 * devices/functions.
135 *
136 * @see grp_pdm_usbdev
137 *
138 *
139 * @section sec_pdm_drv The Pluggable Drivers
140 *
141 * The VM devices are often accessing host hardware or OS facilities. For most
142 * devices these facilities can be abstracted in one or more levels. These
143 * abstractions are called drivers.
144 *
145 * For instance take a DVD/CD drive. This can be connected to a SCSI
146 * controller, an ATA controller or a SATA controller. The basics of the DVD/CD
147 * drive implementation remains the same - eject, insert, read, seek, and such.
148 * (For the scsi SCSCI, you might want to speak SCSI directly to, but that can of
149 * course be fixed - see SCSI passthru.) So, it
150 * makes much sense to have a generic CD/DVD driver which implements this.
151 *
152 * Then the media 'inserted' into the DVD/CD drive can be a ISO image, or it can
153 * be read from a real CD or DVD drive (there are probably other custom formats
154 * someone could desire to read or construct too). So, it would make sense to
155 * have abstracted interfaces for dealing with this in a generic way so the
156 * cdrom unit doesn't have to implement it all. Thus we have created the
157 * CDROM/DVD media driver family.
158 *
159 * So, for this example the IDE controller #1 (i.e. secondary) will have
160 * the DVD/CD Driver attached to it's LUN #0 (master). When a media is mounted
161 * the DVD/CD Driver will have a ISO, HostDVD or RAW (media) Driver attached.
162 *
163 * It is possible to configure many levels of drivers inserting filters, loggers,
164 * or whatever you desire into the chain. We're using this for network sniffing,
165 * for instance.
166 *
167 * The drivers are loaded in a similar manner to that of a device, namely by
168 * iterating a keyspace in CFGM, load the modules listed there and call
169 * 'VBoxDriversRegister' with a callback table.
170 *
171 * @see grp_pdm_driver
172 *
173 *
174 * @section sec_pdm_ifs Interfaces
175 *
176 * The pluggable drivers and devices expose one standard interface (callback
177 * table) which is used to construct, destruct, attach, detach,( ++,) and query
178 * other interfaces. A device will query the interfaces required for it's
179 * operation during init and hot-plug. PDM may query some interfaces during
180 * runtime mounting too.
181 *
182 * An interface here means a function table contained within the device or
183 * driver instance data. Its methods are invoked with the function table pointer
184 * as the first argument and they will calculate the address of the device or
185 * driver instance data from it. (This is one of the aspects which *might* have
186 * been better done in C++.)
187 *
188 * @see grp_pdm_interfaces
189 *
190 *
191 * @section sec_pdm_utils Utilities
192 *
193 * As mentioned earlier, PDM is the location of any usful constructs that doesn't
194 * quite fit into IPRT. The next subsections will discuss these.
195 *
196 * One thing these APIs all have in common is that resources will be associated
197 * with a device / driver and automatically freed after it has been destroyed if
198 * the destructor didn't do this.
199 *
200 *
201 * @subsection sec_pdm_async_completion Async I/O
202 *
203 * The PDM Async I/O API provides a somewhat platform agnostic interface for
204 * asynchronous I/O. For reasons of performance and complexity this does not
205 * build upon any IPRT API.
206 *
207 * @todo more details.
208 *
209 * @see grp_pdm_async_completion
210 *
211 *
212 * @subsection sec_pdm_async_task Async Task - not implemented
213 *
214 * @todo implement and describe
215 *
216 * @see grp_pdm_async_task
217 *
218 *
219 * @subsection sec_pdm_critsect Critical Section
220 *
221 * The PDM Critical Section API is currently building on the IPRT API with the
222 * same name. It adds the possibility to use critical sections in ring-0 and
223 * raw-mode as well as in ring-3. There are certain restrictions on the RC and
224 * R0 usage though since we're not able to wait on it, nor wake up anyone that
225 * is waiting on it. These restrictions origins with the use of a ring-3 event
226 * semaphore. In a later incarnation we plan to replace the ring-3 event
227 * semaphore with a ring-0 one, thus enabling us to wake up waiters while
228 * exectuing in ring-0 and making the hardware assisted execution mode more
229 * efficient. (Raw-mode won't benefit much from this, naturally.)
230 *
231 * @see grp_pdm_critsect
232 *
233 *
234 * @subsection sec_pdm_queue Queue
235 *
236 * The PDM Queue API is for queuing one or more tasks for later consumption in
237 * ring-3 by EMT, and optionally forcing a delayed or ASAP return to ring-3. The
238 * queues can also be run on a timer basis as an alternative to the ASAP thing.
239 * The queue will be flushed at forced action time.
240 *
241 * A queue can also be used by another thread (a I/O worker for instance) to
242 * send work / events over to the EMT.
243 *
244 * @see grp_pdm_queue
245 *
246 *
247 * @subsection sec_pdm_task Task - not implemented yet
248 *
249 * The PDM Task API is for flagging a task for execution at a later point when
250 * we're back in ring-3, optionally forcing the ring-3 return to happen ASAP.
251 * As you can see the concept is similar to queues only simpler.
252 *
253 * A task can also be scheduled by another thread (a I/O worker for instance) as
254 * a mean of getting something done in EMT.
255 *
256 * @see grp_pdm_task
257 *
258 *
259 * @subsection sec_pdm_thread Thread
260 *
261 * The PDM Thread API is there to help devices and drivers manage their threads
262 * correctly wrt. power on, suspend, resume, power off and destruction.
263 *
264 * The general usage pattern for threads in the employ of devices and drivers is
265 * that they shuffle data or requests while the VM is running and stop doing
266 * this when the VM is paused or powered down. Rogue threads running while the
267 * VM is paused can cause the state to change during saving or have other
268 * unwanted side effects. The PDM Threads API ensures that this won't happen.
269 *
270 * @see grp_pdm_thread
271 *
272 */
273
274
275/*********************************************************************************************************************************
276* Header Files *
277*********************************************************************************************************************************/
278#define VBOX_BUGREF_9217_PART_I
279#define LOG_GROUP LOG_GROUP_PDM
280#define PDMPCIDEV_INCLUDE_PRIVATE /* Hack to get pdmpcidevint.h included at the right point. */
281#include "PDMInternal.h"
282#include <VBox/vmm/pdm.h>
283#include <VBox/vmm/em.h>
284#include <VBox/vmm/mm.h>
285#include <VBox/vmm/pgm.h>
286#include <VBox/vmm/ssm.h>
287#include <VBox/vmm/hm.h>
288#include <VBox/vmm/vm.h>
289#include <VBox/vmm/uvm.h>
290#include <VBox/vmm/vmm.h>
291#include <VBox/param.h>
292#include <VBox/err.h>
293#include <VBox/sup.h>
294
295#include <VBox/log.h>
296#include <iprt/asm.h>
297#include <iprt/assert.h>
298#include <iprt/alloc.h>
299#include <iprt/ctype.h>
300#include <iprt/ldr.h>
301#include <iprt/path.h>
302#include <iprt/string.h>
303
304
305/*********************************************************************************************************************************
306* Defined Constants And Macros *
307*********************************************************************************************************************************/
308/** The PDM saved state version. */
309#define PDM_SAVED_STATE_VERSION 5
310/** Before the PDM audio architecture was introduced there was an "AudioSniffer"
311 * device which took care of multiplexing input/output audio data from/to various places.
312 * Thus this device is not needed/used anymore. */
313#define PDM_SAVED_STATE_VERSION_PRE_PDM_AUDIO 4
314#define PDM_SAVED_STATE_VERSION_PRE_NMI_FF 3
315
316/** The number of nanoseconds a suspend callback needs to take before
317 * PDMR3Suspend warns about it taking too long. */
318#define PDMSUSPEND_WARN_AT_NS UINT64_C(1200000000)
319
320/** The number of nanoseconds a suspend callback needs to take before
321 * PDMR3PowerOff warns about it taking too long. */
322#define PDMPOWEROFF_WARN_AT_NS UINT64_C( 900000000)
323
324
325/*********************************************************************************************************************************
326* Structures and Typedefs *
327*********************************************************************************************************************************/
328/**
329 * Statistics of asynchronous notification tasks - used by reset, suspend and
330 * power off.
331 */
332typedef struct PDMNOTIFYASYNCSTATS
333{
334 /** The start timestamp. */
335 uint64_t uStartNsTs;
336 /** When to log the next time. */
337 uint64_t cNsElapsedNextLog;
338 /** The loop counter. */
339 uint32_t cLoops;
340 /** The number of pending asynchronous notification tasks. */
341 uint32_t cAsync;
342 /** The name of the operation (log prefix). */
343 const char *pszOp;
344 /** The current list buffer position. */
345 size_t offList;
346 /** String containing a list of the pending tasks. */
347 char szList[1024];
348} PDMNOTIFYASYNCSTATS;
349/** Pointer to the stats of pending asynchronous notification tasks. */
350typedef PDMNOTIFYASYNCSTATS *PPDMNOTIFYASYNCSTATS;
351
352
353/*********************************************************************************************************************************
354* Internal Functions *
355*********************************************************************************************************************************/
356static DECLCALLBACK(int) pdmR3LiveExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uPass);
357static DECLCALLBACK(int) pdmR3SaveExec(PVM pVM, PSSMHANDLE pSSM);
358static DECLCALLBACK(int) pdmR3LoadExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
359static DECLCALLBACK(int) pdmR3LoadPrep(PVM pVM, PSSMHANDLE pSSM);
360
361static FNDBGFHANDLERINT pdmR3InfoTracingIds;
362
363
364/**
365 * Initializes the PDM part of the UVM.
366 *
367 * This doesn't really do much right now but has to be here for the sake
368 * of completeness.
369 *
370 * @returns VBox status code.
371 * @param pUVM Pointer to the user mode VM structure.
372 */
373VMMR3_INT_DECL(int) PDMR3InitUVM(PUVM pUVM)
374{
375 AssertCompile(sizeof(pUVM->pdm.s) <= sizeof(pUVM->pdm.padding));
376 AssertRelease(sizeof(pUVM->pdm.s) <= sizeof(pUVM->pdm.padding));
377 pUVM->pdm.s.pModules = NULL;
378 pUVM->pdm.s.pCritSects = NULL;
379 pUVM->pdm.s.pRwCritSects = NULL;
380 return RTCritSectInit(&pUVM->pdm.s.ListCritSect);
381}
382
383
384/**
385 * Initializes the PDM.
386 *
387 * @returns VBox status code.
388 * @param pVM The cross context VM structure.
389 */
390VMMR3_INT_DECL(int) PDMR3Init(PVM pVM)
391{
392 LogFlow(("PDMR3Init\n"));
393
394 /*
395 * Assert alignment and sizes.
396 */
397 AssertRelease(!(RT_UOFFSETOF(VM, pdm.s) & 31));
398 AssertRelease(sizeof(pVM->pdm.s) <= sizeof(pVM->pdm.padding));
399 AssertCompileMemberAlignment(PDM, CritSect, sizeof(uintptr_t));
400
401 /*
402 * Init the structure.
403 */
404 pVM->pdm.s.GCPhysVMMDevHeap = NIL_RTGCPHYS;
405 //pVM->pdm.s.idTracingDev = 0;
406 pVM->pdm.s.idTracingOther = 1024;
407
408 /*
409 * Initialize critical sections first.
410 */
411 int rc = pdmR3CritSectBothInitStats(pVM);
412 if (RT_SUCCESS(rc))
413 rc = PDMR3CritSectInit(pVM, &pVM->pdm.s.CritSect, RT_SRC_POS, "PDM");
414 if (RT_SUCCESS(rc))
415 {
416 rc = PDMR3CritSectInit(pVM, &pVM->pdm.s.NopCritSect, RT_SRC_POS, "NOP");
417 if (RT_SUCCESS(rc))
418 pVM->pdm.s.NopCritSect.s.Core.fFlags |= RTCRITSECT_FLAGS_NOP;
419 }
420
421 /*
422 * Initialize sub components.
423 */
424 if (RT_SUCCESS(rc))
425 rc = pdmR3LdrInitU(pVM->pUVM);
426#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
427 if (RT_SUCCESS(rc))
428 rc = pdmR3AsyncCompletionInit(pVM);
429#endif
430#ifdef VBOX_WITH_NETSHAPER
431 if (RT_SUCCESS(rc))
432 rc = pdmR3NetShaperInit(pVM);
433#endif
434 if (RT_SUCCESS(rc))
435 rc = pdmR3BlkCacheInit(pVM);
436 if (RT_SUCCESS(rc))
437 rc = pdmR3DrvInit(pVM);
438 if (RT_SUCCESS(rc))
439 rc = pdmR3DevInit(pVM);
440 if (RT_SUCCESS(rc))
441 {
442 /*
443 * Register the saved state data unit.
444 */
445 rc = SSMR3RegisterInternal(pVM, "pdm", 1, PDM_SAVED_STATE_VERSION, 128,
446 NULL, pdmR3LiveExec, NULL,
447 NULL, pdmR3SaveExec, NULL,
448 pdmR3LoadPrep, pdmR3LoadExec, NULL);
449 if (RT_SUCCESS(rc))
450 {
451 /*
452 * Register the info handlers.
453 */
454 DBGFR3InfoRegisterInternal(pVM, "pdmtracingids",
455 "Displays the tracing IDs assigned by PDM to devices, USB device, drivers and more.",
456 pdmR3InfoTracingIds);
457
458 LogFlow(("PDM: Successfully initialized\n"));
459 return rc;
460 }
461 }
462
463 /*
464 * Cleanup and return failure.
465 */
466 PDMR3Term(pVM);
467 LogFlow(("PDMR3Init: returns %Rrc\n", rc));
468 return rc;
469}
470
471
472/**
473 * Init phase completed callback.
474 *
475 * We use this for calling PDMDEVREG::pfnInitComplete callback after everything
476 * else has been initialized.
477 *
478 * @returns VBox status code.
479 * @param pVM The cross context VM structure.
480 * @param enmWhat The phase that was completed.
481 */
482VMMR3_INT_DECL(int) PDMR3InitCompleted(PVM pVM, VMINITCOMPLETED enmWhat)
483{
484 if (enmWhat == VMINITCOMPLETED_RING0)
485 return pdmR3DevInitComplete(pVM);
486 return VINF_SUCCESS;
487}
488
489
490/**
491 * Applies relocations to data and code managed by this
492 * component. This function will be called at init and
493 * whenever the VMM need to relocate it self inside the GC.
494 *
495 * @param pVM The cross context VM structure.
496 * @param offDelta Relocation delta relative to old location.
497 * @remark The loader subcomponent is relocated by PDMR3LdrRelocate() very
498 * early in the relocation phase.
499 */
500VMMR3_INT_DECL(void) PDMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
501{
502 LogFlow(("PDMR3Relocate\n"));
503
504 /*
505 * Queues.
506 */
507 pdmR3QueueRelocate(pVM, offDelta);
508 pVM->pdm.s.pDevHlpQueueRC = PDMQueueRCPtr(pVM->pdm.s.pDevHlpQueueR3);
509
510 /*
511 * Critical sections.
512 */
513 pdmR3CritSectBothRelocate(pVM);
514
515 /*
516 * The registered PIC.
517 */
518 if (pVM->pdm.s.Pic.pDevInsRC)
519 {
520 pVM->pdm.s.Pic.pDevInsRC += offDelta;
521 pVM->pdm.s.Pic.pfnSetIrqRC += offDelta;
522 pVM->pdm.s.Pic.pfnGetInterruptRC += offDelta;
523 }
524
525 /*
526 * The registered APIC.
527 */
528 if (pVM->pdm.s.Apic.pDevInsRC)
529 pVM->pdm.s.Apic.pDevInsRC += offDelta;
530
531 /*
532 * The registered I/O APIC.
533 */
534 if (pVM->pdm.s.IoApic.pDevInsRC)
535 {
536 pVM->pdm.s.IoApic.pDevInsRC += offDelta;
537 pVM->pdm.s.IoApic.pfnSetIrqRC += offDelta;
538 if (pVM->pdm.s.IoApic.pfnSendMsiRC)
539 pVM->pdm.s.IoApic.pfnSendMsiRC += offDelta;
540 if (pVM->pdm.s.IoApic.pfnSetEoiRC)
541 pVM->pdm.s.IoApic.pfnSetEoiRC += offDelta;
542 }
543
544 /*
545 * The register PCI Buses.
546 */
547 for (unsigned i = 0; i < RT_ELEMENTS(pVM->pdm.s.aPciBuses); i++)
548 {
549 if (pVM->pdm.s.aPciBuses[i].pDevInsRC)
550 {
551 pVM->pdm.s.aPciBuses[i].pDevInsRC += offDelta;
552 pVM->pdm.s.aPciBuses[i].pfnSetIrqRC += offDelta;
553 }
554 }
555
556 /*
557 * Devices & Drivers.
558 */
559 int rc;
560 PCPDMDEVHLPRC pDevHlpRC = NIL_RTRCPTR;
561 if (VM_IS_RAW_MODE_ENABLED(pVM))
562 {
563 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_pdmRCDevHlp", &pDevHlpRC);
564 AssertReleaseMsgRC(rc, ("rc=%Rrc when resolving g_pdmRCDevHlp\n", rc));
565 }
566
567 PCPDMDRVHLPRC pDrvHlpRC = NIL_RTRCPTR;
568 if (VM_IS_RAW_MODE_ENABLED(pVM))
569 {
570 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_pdmRCDevHlp", &pDrvHlpRC);
571 AssertReleaseMsgRC(rc, ("rc=%Rrc when resolving g_pdmRCDevHlp\n", rc));
572 }
573
574 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
575 {
576 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_RC)
577 {
578 pDevIns->pHlpRC = pDevHlpRC;
579 pDevIns->pvInstanceDataRC = MMHyperR3ToRC(pVM, pDevIns->pvInstanceDataR3);
580 if (pDevIns->pCritSectRoR3)
581 pDevIns->pCritSectRoRC = MMHyperR3ToRC(pVM, pDevIns->pCritSectRoR3);
582 pDevIns->Internal.s.pVMRC = pVM->pVMRC;
583
584 PPDMPCIDEV pPciDev = pDevIns->Internal.s.pHeadPciDevR3;
585 if (pPciDev)
586 {
587 pDevIns->Internal.s.pHeadPciDevRC = MMHyperR3ToRC(pVM, pPciDev);
588 do
589 {
590 pPciDev->Int.s.pDevInsRC = MMHyperR3ToRC(pVM, pPciDev->Int.s.pDevInsR3);
591 pPciDev->Int.s.pPdmBusRC = MMHyperR3ToRC(pVM, pPciDev->Int.s.pPdmBusR3);
592 if (pPciDev->Int.s.pNextR3)
593 pPciDev->Int.s.pNextRC = MMHyperR3ToRC(pVM, pPciDev->Int.s.pNextR3);
594 pPciDev = pPciDev->Int.s.pNextR3;
595 } while (pPciDev);
596 }
597
598 if (pDevIns->pReg->pfnRelocate)
599 {
600 LogFlow(("PDMR3Relocate: Relocating device '%s'/%d\n",
601 pDevIns->pReg->szName, pDevIns->iInstance));
602 pDevIns->pReg->pfnRelocate(pDevIns, offDelta);
603 }
604 }
605
606 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
607 {
608 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
609 {
610 if (pDrvIns->pReg->fFlags & PDM_DRVREG_FLAGS_RC)
611 {
612 pDrvIns->pHlpRC = pDrvHlpRC;
613 pDrvIns->pvInstanceDataRC = MMHyperR3ToRC(pVM, pDrvIns->pvInstanceDataR3);
614 pDrvIns->Internal.s.pVMRC = pVM->pVMRC;
615 if (pDrvIns->pReg->pfnRelocate)
616 {
617 LogFlow(("PDMR3Relocate: Relocating driver '%s'/%u attached to '%s'/%d/%u\n",
618 pDrvIns->pReg->szName, pDrvIns->iInstance,
619 pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun));
620 pDrvIns->pReg->pfnRelocate(pDrvIns, offDelta);
621 }
622 }
623 }
624 }
625
626 }
627}
628
629
630/**
631 * Worker for pdmR3Term that terminates a LUN chain.
632 *
633 * @param pVM The cross context VM structure.
634 * @param pLun The head of the chain.
635 * @param pszDevice The name of the device (for logging).
636 * @param iInstance The device instance number (for logging).
637 */
638static void pdmR3TermLuns(PVM pVM, PPDMLUN pLun, const char *pszDevice, unsigned iInstance)
639{
640 RT_NOREF2(pszDevice, iInstance);
641
642 for (; pLun; pLun = pLun->pNext)
643 {
644 /*
645 * Destroy them one at a time from the bottom up.
646 * (The serial device/drivers depends on this - bad.)
647 */
648 PPDMDRVINS pDrvIns = pLun->pBottom;
649 pLun->pBottom = pLun->pTop = NULL;
650 while (pDrvIns)
651 {
652 PPDMDRVINS pDrvNext = pDrvIns->Internal.s.pUp;
653
654 if (pDrvIns->pReg->pfnDestruct)
655 {
656 LogFlow(("pdmR3DevTerm: Destroying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
657 pDrvIns->pReg->szName, pDrvIns->iInstance, pLun->iLun, pszDevice, iInstance));
658 pDrvIns->pReg->pfnDestruct(pDrvIns);
659 }
660 pDrvIns->Internal.s.pDrv->cInstances--;
661
662 /* Order of resource freeing like in pdmR3DrvDestroyChain, but
663 * not all need to be done as they are done globally later. */
664 //PDMR3QueueDestroyDriver(pVM, pDrvIns);
665 TMR3TimerDestroyDriver(pVM, pDrvIns);
666 SSMR3DeregisterDriver(pVM, pDrvIns, NULL, 0);
667 //pdmR3ThreadDestroyDriver(pVM, pDrvIns);
668 //DBGFR3InfoDeregisterDriver(pVM, pDrvIns, NULL);
669 //pdmR3CritSectBothDeleteDriver(pVM, pDrvIns);
670 //PDMR3BlkCacheReleaseDriver(pVM, pDrvIns);
671#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
672 //pdmR3AsyncCompletionTemplateDestroyDriver(pVM, pDrvIns);
673#endif
674
675 /* Clear the driver struture to catch sloppy code. */
676 ASMMemFill32(pDrvIns, RT_UOFFSETOF_DYN(PDMDRVINS, achInstanceData[pDrvIns->pReg->cbInstance]), 0xdeadd0d0);
677
678 pDrvIns = pDrvNext;
679 }
680 }
681}
682
683
684/**
685 * Terminates the PDM.
686 *
687 * Termination means cleaning up and freeing all resources,
688 * the VM it self is at this point powered off or suspended.
689 *
690 * @returns VBox status code.
691 * @param pVM The cross context VM structure.
692 */
693VMMR3_INT_DECL(int) PDMR3Term(PVM pVM)
694{
695 LogFlow(("PDMR3Term:\n"));
696 AssertMsg(PDMCritSectIsInitialized(&pVM->pdm.s.CritSect), ("bad init order!\n"));
697
698 /*
699 * Iterate the device instances and attach drivers, doing
700 * relevant destruction processing.
701 *
702 * N.B. There is no need to mess around freeing memory allocated
703 * from any MM heap since MM will do that in its Term function.
704 */
705 /* usb ones first. */
706 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
707 {
708 pdmR3TermLuns(pVM, pUsbIns->Internal.s.pLuns, pUsbIns->pReg->szName, pUsbIns->iInstance);
709
710 /*
711 * Detach it from the HUB (if it's actually attached to one) so the HUB has
712 * a chance to stop accessing any data.
713 */
714 PPDMUSBHUB pHub = pUsbIns->Internal.s.pHub;
715 if (pHub)
716 {
717 int rc = pHub->Reg.pfnDetachDevice(pHub->pDrvIns, pUsbIns, pUsbIns->Internal.s.iPort);
718 if (RT_FAILURE(rc))
719 {
720 LogRel(("PDM: Failed to detach USB device '%s' instance %d from %p: %Rrc\n",
721 pUsbIns->pReg->szName, pUsbIns->iInstance, pHub, rc));
722 }
723 else
724 {
725 pHub->cAvailablePorts++;
726 Assert(pHub->cAvailablePorts > 0 && pHub->cAvailablePorts <= pHub->cPorts);
727 pUsbIns->Internal.s.pHub = NULL;
728 }
729 }
730
731 if (pUsbIns->pReg->pfnDestruct)
732 {
733 LogFlow(("pdmR3DevTerm: Destroying - device '%s'/%d\n",
734 pUsbIns->pReg->szName, pUsbIns->iInstance));
735 pUsbIns->pReg->pfnDestruct(pUsbIns);
736 }
737
738 //TMR3TimerDestroyUsb(pVM, pUsbIns);
739 //SSMR3DeregisterUsb(pVM, pUsbIns, NULL, 0);
740 pdmR3ThreadDestroyUsb(pVM, pUsbIns);
741 }
742
743 /* then the 'normal' ones. */
744 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
745 {
746 pdmR3TermLuns(pVM, pDevIns->Internal.s.pLunsR3, pDevIns->pReg->szName, pDevIns->iInstance);
747
748 if (pDevIns->pReg->pfnDestruct)
749 {
750 LogFlow(("pdmR3DevTerm: Destroying - device '%s'/%d\n",
751 pDevIns->pReg->szName, pDevIns->iInstance));
752 pDevIns->pReg->pfnDestruct(pDevIns);
753 }
754
755 TMR3TimerDestroyDevice(pVM, pDevIns);
756 SSMR3DeregisterDevice(pVM, pDevIns, NULL, 0);
757 pdmR3CritSectBothDeleteDevice(pVM, pDevIns);
758 pdmR3ThreadDestroyDevice(pVM, pDevIns);
759 PDMR3QueueDestroyDevice(pVM, pDevIns);
760 PGMR3PhysMMIOExDeregister(pVM, pDevIns, UINT32_MAX, UINT32_MAX);
761#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
762 pdmR3AsyncCompletionTemplateDestroyDevice(pVM, pDevIns);
763#endif
764 DBGFR3InfoDeregisterDevice(pVM, pDevIns, NULL);
765 }
766
767 /*
768 * Destroy all threads.
769 */
770 pdmR3ThreadDestroyAll(pVM);
771
772 /*
773 * Destroy the block cache.
774 */
775 pdmR3BlkCacheTerm(pVM);
776
777#ifdef VBOX_WITH_NETSHAPER
778 /*
779 * Destroy network bandwidth groups.
780 */
781 pdmR3NetShaperTerm(pVM);
782#endif
783#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
784 /*
785 * Free async completion managers.
786 */
787 pdmR3AsyncCompletionTerm(pVM);
788#endif
789
790 /*
791 * Free modules.
792 */
793 pdmR3LdrTermU(pVM->pUVM);
794
795 /*
796 * Destroy the PDM lock.
797 */
798 PDMR3CritSectDelete(&pVM->pdm.s.CritSect);
799 /* The MiscCritSect is deleted by PDMR3CritSectBothTerm later. */
800
801 LogFlow(("PDMR3Term: returns %Rrc\n", VINF_SUCCESS));
802 return VINF_SUCCESS;
803}
804
805
806/**
807 * Terminates the PDM part of the UVM.
808 *
809 * This will unload any modules left behind.
810 *
811 * @param pUVM Pointer to the user mode VM structure.
812 */
813VMMR3_INT_DECL(void) PDMR3TermUVM(PUVM pUVM)
814{
815 /*
816 * In the normal cause of events we will now call pdmR3LdrTermU for
817 * the second time. In the case of init failure however, this might
818 * the first time, which is why we do it.
819 */
820 pdmR3LdrTermU(pUVM);
821
822 Assert(pUVM->pdm.s.pCritSects == NULL);
823 Assert(pUVM->pdm.s.pRwCritSects == NULL);
824 RTCritSectDelete(&pUVM->pdm.s.ListCritSect);
825}
826
827
828/**
829 * For APIC assertions.
830 *
831 * @returns true if we've loaded state.
832 * @param pVM The cross context VM structure.
833 */
834VMMR3_INT_DECL(bool) PDMR3HasLoadedState(PVM pVM)
835{
836 return pVM->pdm.s.fStateLoaded;
837}
838
839
840/**
841 * Bits that are saved in pass 0 and in the final pass.
842 *
843 * @param pVM The cross context VM structure.
844 * @param pSSM The saved state handle.
845 */
846static void pdmR3SaveBoth(PVM pVM, PSSMHANDLE pSSM)
847{
848 /*
849 * Save the list of device instances so we can check that they're all still
850 * there when we load the state and that nothing new has been added.
851 */
852 uint32_t i = 0;
853 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3, i++)
854 {
855 SSMR3PutU32(pSSM, i);
856 SSMR3PutStrZ(pSSM, pDevIns->pReg->szName);
857 SSMR3PutU32(pSSM, pDevIns->iInstance);
858 }
859 SSMR3PutU32(pSSM, UINT32_MAX); /* terminator */
860}
861
862
863/**
864 * Live save.
865 *
866 * @returns VBox status code.
867 * @param pVM The cross context VM structure.
868 * @param pSSM The saved state handle.
869 * @param uPass The pass.
870 */
871static DECLCALLBACK(int) pdmR3LiveExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uPass)
872{
873 LogFlow(("pdmR3LiveExec:\n"));
874 AssertReturn(uPass == 0, VERR_SSM_UNEXPECTED_PASS);
875 pdmR3SaveBoth(pVM, pSSM);
876 return VINF_SSM_DONT_CALL_AGAIN;
877}
878
879
880/**
881 * Execute state save operation.
882 *
883 * @returns VBox status code.
884 * @param pVM The cross context VM structure.
885 * @param pSSM The saved state handle.
886 */
887static DECLCALLBACK(int) pdmR3SaveExec(PVM pVM, PSSMHANDLE pSSM)
888{
889 LogFlow(("pdmR3SaveExec:\n"));
890
891 /*
892 * Save interrupt and DMA states.
893 */
894 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
895 {
896 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
897 SSMR3PutU32(pSSM, VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC));
898 SSMR3PutU32(pSSM, VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_PIC));
899 SSMR3PutU32(pSSM, VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_NMI));
900 SSMR3PutU32(pSSM, VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_SMI));
901 }
902 SSMR3PutU32(pSSM, VM_FF_IS_SET(pVM, VM_FF_PDM_DMA));
903
904 pdmR3SaveBoth(pVM, pSSM);
905 return VINF_SUCCESS;
906}
907
908
909/**
910 * Prepare state load operation.
911 *
912 * This will dispatch pending operations and clear the FFs governed by PDM and its devices.
913 *
914 * @returns VBox status code.
915 * @param pVM The cross context VM structure.
916 * @param pSSM The SSM handle.
917 */
918static DECLCALLBACK(int) pdmR3LoadPrep(PVM pVM, PSSMHANDLE pSSM)
919{
920 LogFlow(("pdmR3LoadPrep: %s%s\n",
921 VM_FF_IS_SET(pVM, VM_FF_PDM_QUEUES) ? " VM_FF_PDM_QUEUES" : "",
922 VM_FF_IS_SET(pVM, VM_FF_PDM_DMA) ? " VM_FF_PDM_DMA" : ""));
923#ifdef LOG_ENABLED
924 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
925 {
926 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
927 LogFlow(("pdmR3LoadPrep: VCPU %u %s%s\n", idCpu,
928 VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC) ? " VMCPU_FF_INTERRUPT_APIC" : "",
929 VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_PIC) ? " VMCPU_FF_INTERRUPT_PIC" : ""));
930 }
931#endif
932 NOREF(pSSM);
933
934 /*
935 * In case there is work pending that will raise an interrupt,
936 * start a DMA transfer, or release a lock. (unlikely)
937 */
938 if (VM_FF_IS_SET(pVM, VM_FF_PDM_QUEUES))
939 PDMR3QueueFlushAll(pVM);
940
941 /* Clear the FFs. */
942 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
943 {
944 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
945 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_APIC);
946 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_PIC);
947 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_NMI);
948 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_SMI);
949 }
950 VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
951
952 return VINF_SUCCESS;
953}
954
955
956/**
957 * Execute state load operation.
958 *
959 * @returns VBox status code.
960 * @param pVM The cross context VM structure.
961 * @param pSSM SSM operation handle.
962 * @param uVersion Data layout version.
963 * @param uPass The data pass.
964 */
965static DECLCALLBACK(int) pdmR3LoadExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
966{
967 int rc;
968
969 LogFlow(("pdmR3LoadExec: uPass=%#x\n", uPass));
970
971 /*
972 * Validate version.
973 */
974 if ( uVersion != PDM_SAVED_STATE_VERSION
975 && uVersion != PDM_SAVED_STATE_VERSION_PRE_NMI_FF
976 && uVersion != PDM_SAVED_STATE_VERSION_PRE_PDM_AUDIO)
977 {
978 AssertMsgFailed(("Invalid version uVersion=%d!\n", uVersion));
979 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
980 }
981
982 if (uPass == SSM_PASS_FINAL)
983 {
984 /*
985 * Load the interrupt and DMA states.
986 *
987 * The APIC, PIC and DMA devices does not restore these, we do. In the
988 * APIC and PIC cases, it is possible that some devices is incorrectly
989 * setting IRQs during restore. We'll warn when this happens. (There
990 * are debug assertions in PDMDevMiscHlp.cpp and APICAll.cpp for
991 * catching the buggy device.)
992 */
993 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
994 {
995 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
996
997 /* APIC interrupt */
998 uint32_t fInterruptPending = 0;
999 rc = SSMR3GetU32(pSSM, &fInterruptPending);
1000 if (RT_FAILURE(rc))
1001 return rc;
1002 if (fInterruptPending & ~1)
1003 {
1004 AssertMsgFailed(("fInterruptPending=%#x (APIC)\n", fInterruptPending));
1005 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1006 }
1007 AssertLogRelMsg(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC),
1008 ("VCPU%03u: VMCPU_FF_INTERRUPT_APIC set! Devices shouldn't set interrupts during state restore...\n", idCpu));
1009 if (fInterruptPending)
1010 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC);
1011
1012 /* PIC interrupt */
1013 fInterruptPending = 0;
1014 rc = SSMR3GetU32(pSSM, &fInterruptPending);
1015 if (RT_FAILURE(rc))
1016 return rc;
1017 if (fInterruptPending & ~1)
1018 {
1019 AssertMsgFailed(("fInterruptPending=%#x (PIC)\n", fInterruptPending));
1020 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1021 }
1022 AssertLogRelMsg(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_PIC),
1023 ("VCPU%03u: VMCPU_FF_INTERRUPT_PIC set! Devices shouldn't set interrupts during state restore...\n", idCpu));
1024 if (fInterruptPending)
1025 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_PIC);
1026
1027 if (uVersion > PDM_SAVED_STATE_VERSION_PRE_NMI_FF)
1028 {
1029 /* NMI interrupt */
1030 fInterruptPending = 0;
1031 rc = SSMR3GetU32(pSSM, &fInterruptPending);
1032 if (RT_FAILURE(rc))
1033 return rc;
1034 if (fInterruptPending & ~1)
1035 {
1036 AssertMsgFailed(("fInterruptPending=%#x (NMI)\n", fInterruptPending));
1037 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1038 }
1039 AssertLogRelMsg(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_NMI), ("VCPU%3u: VMCPU_FF_INTERRUPT_NMI set!\n", idCpu));
1040 if (fInterruptPending)
1041 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_NMI);
1042
1043 /* SMI interrupt */
1044 fInterruptPending = 0;
1045 rc = SSMR3GetU32(pSSM, &fInterruptPending);
1046 if (RT_FAILURE(rc))
1047 return rc;
1048 if (fInterruptPending & ~1)
1049 {
1050 AssertMsgFailed(("fInterruptPending=%#x (SMI)\n", fInterruptPending));
1051 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1052 }
1053 AssertLogRelMsg(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_SMI), ("VCPU%3u: VMCPU_FF_INTERRUPT_SMI set!\n", idCpu));
1054 if (fInterruptPending)
1055 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_SMI);
1056 }
1057 }
1058
1059 /* DMA pending */
1060 uint32_t fDMAPending = 0;
1061 rc = SSMR3GetU32(pSSM, &fDMAPending);
1062 if (RT_FAILURE(rc))
1063 return rc;
1064 if (fDMAPending & ~1)
1065 {
1066 AssertMsgFailed(("fDMAPending=%#x\n", fDMAPending));
1067 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1068 }
1069 if (fDMAPending)
1070 VM_FF_SET(pVM, VM_FF_PDM_DMA);
1071 Log(("pdmR3LoadExec: VM_FF_PDM_DMA=%RTbool\n", VM_FF_IS_SET(pVM, VM_FF_PDM_DMA)));
1072 }
1073
1074 /*
1075 * Load the list of devices and verify that they are all there.
1076 */
1077 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1078 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_FOUND;
1079
1080 for (uint32_t i = 0; ; i++)
1081 {
1082 /* Get the sequence number / terminator. */
1083 uint32_t u32Sep;
1084 rc = SSMR3GetU32(pSSM, &u32Sep);
1085 if (RT_FAILURE(rc))
1086 return rc;
1087 if (u32Sep == UINT32_MAX)
1088 break;
1089 if (u32Sep != i)
1090 AssertMsgFailedReturn(("Out of sequence. u32Sep=%#x i=%#x\n", u32Sep, i), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
1091
1092 /* Get the name and instance number. */
1093 char szName[RT_SIZEOFMEMB(PDMDEVREG, szName)];
1094 rc = SSMR3GetStrZ(pSSM, szName, sizeof(szName));
1095 if (RT_FAILURE(rc))
1096 return rc;
1097 uint32_t iInstance;
1098 rc = SSMR3GetU32(pSSM, &iInstance);
1099 if (RT_FAILURE(rc))
1100 return rc;
1101
1102 /* Try locate it. */
1103 PPDMDEVINS pDevIns;
1104 for (pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1105 if ( !RTStrCmp(szName, pDevIns->pReg->szName)
1106 && pDevIns->iInstance == iInstance)
1107 {
1108 AssertLogRelMsgReturn(!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_FOUND),
1109 ("%s/#%u\n", pDevIns->pReg->szName, pDevIns->iInstance),
1110 VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
1111 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_FOUND;
1112 break;
1113 }
1114
1115 if (!pDevIns)
1116 {
1117 bool fSkip = false;
1118
1119 /* Skip the non-existing (deprecated) "AudioSniffer" device stored in the saved state. */
1120 if ( uVersion <= PDM_SAVED_STATE_VERSION_PRE_PDM_AUDIO
1121 && !RTStrCmp(szName, "AudioSniffer"))
1122 fSkip = true;
1123
1124 if (!fSkip)
1125 {
1126 LogRel(("Device '%s'/%d not found in current config\n", szName, iInstance));
1127 if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
1128 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Device '%s'/%d not found in current config"), szName, iInstance);
1129 }
1130 }
1131 }
1132
1133 /*
1134 * Check that no additional devices were configured.
1135 */
1136 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1137 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_FOUND))
1138 {
1139 LogRel(("Device '%s'/%d not found in the saved state\n", pDevIns->pReg->szName, pDevIns->iInstance));
1140 if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
1141 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Device '%s'/%d not found in the saved state"),
1142 pDevIns->pReg->szName, pDevIns->iInstance);
1143 }
1144
1145
1146 /*
1147 * Indicate that we've been called (for assertions).
1148 */
1149 pVM->pdm.s.fStateLoaded = true;
1150
1151 return VINF_SUCCESS;
1152}
1153
1154
1155/**
1156 * Worker for PDMR3PowerOn that deals with one driver.
1157 *
1158 * @param pDrvIns The driver instance.
1159 * @param pszDevName The parent device name.
1160 * @param iDevInstance The parent device instance number.
1161 * @param iLun The parent LUN number.
1162 */
1163DECLINLINE(int) pdmR3PowerOnDrv(PPDMDRVINS pDrvIns, const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1164{
1165 Assert(pDrvIns->Internal.s.fVMSuspended);
1166 if (pDrvIns->pReg->pfnPowerOn)
1167 {
1168 LogFlow(("PDMR3PowerOn: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1169 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1170 int rc = VINF_SUCCESS; pDrvIns->pReg->pfnPowerOn(pDrvIns);
1171 if (RT_FAILURE(rc))
1172 {
1173 LogRel(("PDMR3PowerOn: Driver '%s'/%d on LUN#%d of device '%s'/%d -> %Rrc\n",
1174 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, rc));
1175 return rc;
1176 }
1177 }
1178 pDrvIns->Internal.s.fVMSuspended = false;
1179 return VINF_SUCCESS;
1180}
1181
1182
1183/**
1184 * Worker for PDMR3PowerOn that deals with one USB device instance.
1185 *
1186 * @returns VBox status code.
1187 * @param pUsbIns The USB device instance.
1188 */
1189DECLINLINE(int) pdmR3PowerOnUsb(PPDMUSBINS pUsbIns)
1190{
1191 Assert(pUsbIns->Internal.s.fVMSuspended);
1192 if (pUsbIns->pReg->pfnVMPowerOn)
1193 {
1194 LogFlow(("PDMR3PowerOn: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1195 int rc = VINF_SUCCESS; pUsbIns->pReg->pfnVMPowerOn(pUsbIns);
1196 if (RT_FAILURE(rc))
1197 {
1198 LogRel(("PDMR3PowerOn: Device '%s'/%d -> %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1199 return rc;
1200 }
1201 }
1202 pUsbIns->Internal.s.fVMSuspended = false;
1203 return VINF_SUCCESS;
1204}
1205
1206
1207/**
1208 * Worker for PDMR3PowerOn that deals with one device instance.
1209 *
1210 * @returns VBox status code.
1211 * @param pDevIns The device instance.
1212 */
1213DECLINLINE(int) pdmR3PowerOnDev(PPDMDEVINS pDevIns)
1214{
1215 Assert(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED);
1216 if (pDevIns->pReg->pfnPowerOn)
1217 {
1218 LogFlow(("PDMR3PowerOn: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1219 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1220 int rc = VINF_SUCCESS; pDevIns->pReg->pfnPowerOn(pDevIns);
1221 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1222 if (RT_FAILURE(rc))
1223 {
1224 LogRel(("PDMR3PowerOn: Device '%s'/%d -> %Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
1225 return rc;
1226 }
1227 }
1228 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
1229 return VINF_SUCCESS;
1230}
1231
1232
1233/**
1234 * This function will notify all the devices and their
1235 * attached drivers about the VM now being powered on.
1236 *
1237 * @param pVM The cross context VM structure.
1238 */
1239VMMR3DECL(void) PDMR3PowerOn(PVM pVM)
1240{
1241 LogFlow(("PDMR3PowerOn:\n"));
1242
1243 /*
1244 * Iterate thru the device instances and USB device instances,
1245 * processing the drivers associated with those.
1246 */
1247 int rc = VINF_SUCCESS;
1248 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns && RT_SUCCESS(rc); pDevIns = pDevIns->Internal.s.pNextR3)
1249 {
1250 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1251 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1252 rc = pdmR3PowerOnDrv(pDrvIns, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun);
1253 if (RT_SUCCESS(rc))
1254 rc = pdmR3PowerOnDev(pDevIns);
1255 }
1256
1257#ifdef VBOX_WITH_USB
1258 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns && RT_SUCCESS(rc); pUsbIns = pUsbIns->Internal.s.pNext)
1259 {
1260 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1261 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1262 rc = pdmR3PowerOnDrv(pDrvIns, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun);
1263 if (RT_SUCCESS(rc))
1264 rc = pdmR3PowerOnUsb(pUsbIns);
1265 }
1266#endif
1267
1268#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
1269 pdmR3AsyncCompletionResume(pVM);
1270#endif
1271
1272 /*
1273 * Resume all threads.
1274 */
1275 if (RT_SUCCESS(rc))
1276 pdmR3ThreadResumeAll(pVM);
1277
1278 /*
1279 * On failure, clean up via PDMR3Suspend.
1280 */
1281 if (RT_FAILURE(rc))
1282 PDMR3Suspend(pVM);
1283
1284 LogFlow(("PDMR3PowerOn: returns %Rrc\n", rc));
1285 return /*rc*/;
1286}
1287
1288
1289/**
1290 * Initializes the asynchronous notifi stats structure.
1291 *
1292 * @param pThis The asynchronous notifification stats.
1293 * @param pszOp The name of the operation.
1294 */
1295static void pdmR3NotifyAsyncInit(PPDMNOTIFYASYNCSTATS pThis, const char *pszOp)
1296{
1297 pThis->uStartNsTs = RTTimeNanoTS();
1298 pThis->cNsElapsedNextLog = 0;
1299 pThis->cLoops = 0;
1300 pThis->cAsync = 0;
1301 pThis->pszOp = pszOp;
1302 pThis->offList = 0;
1303 pThis->szList[0] = '\0';
1304}
1305
1306
1307/**
1308 * Begin a new loop, prepares to gather new stats.
1309 *
1310 * @param pThis The asynchronous notifification stats.
1311 */
1312static void pdmR3NotifyAsyncBeginLoop(PPDMNOTIFYASYNCSTATS pThis)
1313{
1314 pThis->cLoops++;
1315 pThis->cAsync = 0;
1316 pThis->offList = 0;
1317 pThis->szList[0] = '\0';
1318}
1319
1320
1321/**
1322 * Records a device or USB device with a pending asynchronous notification.
1323 *
1324 * @param pThis The asynchronous notifification stats.
1325 * @param pszName The name of the thing.
1326 * @param iInstance The instance number.
1327 */
1328static void pdmR3NotifyAsyncAdd(PPDMNOTIFYASYNCSTATS pThis, const char *pszName, uint32_t iInstance)
1329{
1330 pThis->cAsync++;
1331 if (pThis->offList < sizeof(pThis->szList) - 4)
1332 pThis->offList += RTStrPrintf(&pThis->szList[pThis->offList], sizeof(pThis->szList) - pThis->offList,
1333 pThis->offList == 0 ? "%s/%u" : ", %s/%u",
1334 pszName, iInstance);
1335}
1336
1337
1338/**
1339 * Records the asynchronous completition of a reset, suspend or power off.
1340 *
1341 * @param pThis The asynchronous notifification stats.
1342 * @param pszDrvName The driver name.
1343 * @param iDrvInstance The driver instance number.
1344 * @param pszDevName The device or USB device name.
1345 * @param iDevInstance The device or USB device instance number.
1346 * @param iLun The LUN.
1347 */
1348static void pdmR3NotifyAsyncAddDrv(PPDMNOTIFYASYNCSTATS pThis, const char *pszDrvName, uint32_t iDrvInstance,
1349 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1350{
1351 pThis->cAsync++;
1352 if (pThis->offList < sizeof(pThis->szList) - 8)
1353 pThis->offList += RTStrPrintf(&pThis->szList[pThis->offList], sizeof(pThis->szList) - pThis->offList,
1354 pThis->offList == 0 ? "%s/%u/%u/%s/%u" : ", %s/%u/%u/%s/%u",
1355 pszDevName, iDevInstance, iLun, pszDrvName, iDrvInstance);
1356}
1357
1358
1359/**
1360 * Log the stats.
1361 *
1362 * @param pThis The asynchronous notifification stats.
1363 */
1364static void pdmR3NotifyAsyncLog(PPDMNOTIFYASYNCSTATS pThis)
1365{
1366 /*
1367 * Return if we shouldn't log at this point.
1368 * We log with an internval increasing from 0 sec to 60 sec.
1369 */
1370 if (!pThis->cAsync)
1371 return;
1372
1373 uint64_t cNsElapsed = RTTimeNanoTS() - pThis->uStartNsTs;
1374 if (cNsElapsed < pThis->cNsElapsedNextLog)
1375 return;
1376
1377 if (pThis->cNsElapsedNextLog == 0)
1378 pThis->cNsElapsedNextLog = RT_NS_1SEC;
1379 else if (pThis->cNsElapsedNextLog >= RT_NS_1MIN / 2)
1380 pThis->cNsElapsedNextLog = RT_NS_1MIN;
1381 else
1382 pThis->cNsElapsedNextLog *= 2;
1383
1384 /*
1385 * Do the logging.
1386 */
1387 LogRel(("%s: after %5llu ms, %u loops: %u async tasks - %s\n",
1388 pThis->pszOp, cNsElapsed / RT_NS_1MS, pThis->cLoops, pThis->cAsync, pThis->szList));
1389}
1390
1391
1392/**
1393 * Wait for events and process pending requests.
1394 *
1395 * @param pThis The asynchronous notifification stats.
1396 * @param pVM The cross context VM structure.
1397 */
1398static void pdmR3NotifyAsyncWaitAndProcessRequests(PPDMNOTIFYASYNCSTATS pThis, PVM pVM)
1399{
1400 VM_ASSERT_EMT0(pVM);
1401 int rc = VMR3AsyncPdmNotificationWaitU(&pVM->pUVM->aCpus[0]);
1402 AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc - %s - %s\n", rc, pThis->pszOp, pThis->szList));
1403
1404 rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY, true /*fPriorityOnly*/);
1405 AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc - %s - %s\n", rc, pThis->pszOp, pThis->szList));
1406 rc = VMR3ReqProcessU(pVM->pUVM, 0/*idDstCpu*/, true /*fPriorityOnly*/);
1407 AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc - %s - %s\n", rc, pThis->pszOp, pThis->szList));
1408}
1409
1410
1411/**
1412 * Worker for PDMR3Reset that deals with one driver.
1413 *
1414 * @param pDrvIns The driver instance.
1415 * @param pAsync The structure for recording asynchronous
1416 * notification tasks.
1417 * @param pszDevName The parent device name.
1418 * @param iDevInstance The parent device instance number.
1419 * @param iLun The parent LUN number.
1420 */
1421DECLINLINE(bool) pdmR3ResetDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
1422 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1423{
1424 if (!pDrvIns->Internal.s.fVMReset)
1425 {
1426 pDrvIns->Internal.s.fVMReset = true;
1427 if (pDrvIns->pReg->pfnReset)
1428 {
1429 if (!pDrvIns->Internal.s.pfnAsyncNotify)
1430 {
1431 LogFlow(("PDMR3Reset: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1432 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1433 pDrvIns->pReg->pfnReset(pDrvIns);
1434 if (pDrvIns->Internal.s.pfnAsyncNotify)
1435 LogFlow(("PDMR3Reset: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1436 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1437 }
1438 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
1439 {
1440 LogFlow(("PDMR3Reset: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1441 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1442 pDrvIns->Internal.s.pfnAsyncNotify = NULL;
1443 }
1444 if (pDrvIns->Internal.s.pfnAsyncNotify)
1445 {
1446 pDrvIns->Internal.s.fVMReset = false;
1447 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance,
1448 pszDevName, iDevInstance, iLun);
1449 return false;
1450 }
1451 }
1452 }
1453 return true;
1454}
1455
1456
1457/**
1458 * Worker for PDMR3Reset that deals with one USB device instance.
1459 *
1460 * @param pUsbIns The USB device instance.
1461 * @param pAsync The structure for recording asynchronous
1462 * notification tasks.
1463 */
1464DECLINLINE(void) pdmR3ResetUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
1465{
1466 if (!pUsbIns->Internal.s.fVMReset)
1467 {
1468 pUsbIns->Internal.s.fVMReset = true;
1469 if (pUsbIns->pReg->pfnVMReset)
1470 {
1471 if (!pUsbIns->Internal.s.pfnAsyncNotify)
1472 {
1473 LogFlow(("PDMR3Reset: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1474 pUsbIns->pReg->pfnVMReset(pUsbIns);
1475 if (pUsbIns->Internal.s.pfnAsyncNotify)
1476 LogFlow(("PDMR3Reset: Async notification started - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1477 }
1478 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
1479 {
1480 LogFlow(("PDMR3Reset: Async notification completed - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1481 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
1482 }
1483 if (pUsbIns->Internal.s.pfnAsyncNotify)
1484 {
1485 pUsbIns->Internal.s.fVMReset = false;
1486 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
1487 }
1488 }
1489 }
1490}
1491
1492
1493/**
1494 * Worker for PDMR3Reset that deals with one device instance.
1495 *
1496 * @param pDevIns The device instance.
1497 * @param pAsync The structure for recording asynchronous
1498 * notification tasks.
1499 */
1500DECLINLINE(void) pdmR3ResetDev(PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
1501{
1502 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_RESET))
1503 {
1504 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_RESET;
1505 if (pDevIns->pReg->pfnReset)
1506 {
1507 uint64_t cNsElapsed = RTTimeNanoTS();
1508 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1509
1510 if (!pDevIns->Internal.s.pfnAsyncNotify)
1511 {
1512 LogFlow(("PDMR3Reset: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1513 pDevIns->pReg->pfnReset(pDevIns);
1514 if (pDevIns->Internal.s.pfnAsyncNotify)
1515 LogFlow(("PDMR3Reset: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1516 }
1517 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
1518 {
1519 LogFlow(("PDMR3Reset: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1520 pDevIns->Internal.s.pfnAsyncNotify = NULL;
1521 }
1522 if (pDevIns->Internal.s.pfnAsyncNotify)
1523 {
1524 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_RESET;
1525 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
1526 }
1527
1528 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1529 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1530 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1531 LogRel(("PDMR3Reset: Device '%s'/%d took %'llu ns to reset\n",
1532 pDevIns->pReg->szName, pDevIns->iInstance, cNsElapsed));
1533 }
1534 }
1535}
1536
1537
1538/**
1539 * Resets a virtual CPU.
1540 *
1541 * Used by PDMR3Reset and CPU hot plugging.
1542 *
1543 * @param pVCpu The cross context virtual CPU structure.
1544 */
1545VMMR3_INT_DECL(void) PDMR3ResetCpu(PVMCPU pVCpu)
1546{
1547 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_APIC);
1548 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_PIC);
1549 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_NMI);
1550 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_SMI);
1551}
1552
1553
1554/**
1555 * This function will notify all the devices and their attached drivers about
1556 * the VM now being reset.
1557 *
1558 * @param pVM The cross context VM structure.
1559 */
1560VMMR3_INT_DECL(void) PDMR3Reset(PVM pVM)
1561{
1562 LogFlow(("PDMR3Reset:\n"));
1563
1564 /*
1565 * Clear all the reset flags.
1566 */
1567 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1568 {
1569 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_RESET;
1570 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1571 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1572 pDrvIns->Internal.s.fVMReset = false;
1573 }
1574#ifdef VBOX_WITH_USB
1575 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1576 {
1577 pUsbIns->Internal.s.fVMReset = false;
1578 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1579 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1580 pDrvIns->Internal.s.fVMReset = false;
1581 }
1582#endif
1583
1584 /*
1585 * The outer loop repeats until there are no more async requests.
1586 */
1587 PDMNOTIFYASYNCSTATS Async;
1588 pdmR3NotifyAsyncInit(&Async, "PDMR3Reset");
1589 for (;;)
1590 {
1591 pdmR3NotifyAsyncBeginLoop(&Async);
1592
1593 /*
1594 * Iterate thru the device instances and USB device instances,
1595 * processing the drivers associated with those.
1596 */
1597 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1598 {
1599 unsigned const cAsyncStart = Async.cAsync;
1600
1601 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_RESET_NOTIFICATION)
1602 pdmR3ResetDev(pDevIns, &Async);
1603
1604 if (Async.cAsync == cAsyncStart)
1605 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1606 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1607 if (!pdmR3ResetDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
1608 break;
1609
1610 if ( Async.cAsync == cAsyncStart
1611 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_RESET_NOTIFICATION))
1612 pdmR3ResetDev(pDevIns, &Async);
1613 }
1614
1615#ifdef VBOX_WITH_USB
1616 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1617 {
1618 unsigned const cAsyncStart = Async.cAsync;
1619
1620 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1621 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1622 if (!pdmR3ResetDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
1623 break;
1624
1625 if (Async.cAsync == cAsyncStart)
1626 pdmR3ResetUsb(pUsbIns, &Async);
1627 }
1628#endif
1629 if (!Async.cAsync)
1630 break;
1631 pdmR3NotifyAsyncLog(&Async);
1632 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
1633 }
1634
1635 /*
1636 * Clear all pending interrupts and DMA operations.
1637 */
1638 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
1639 PDMR3ResetCpu(pVM->apCpusR3[idCpu]);
1640 VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
1641
1642 LogFlow(("PDMR3Reset: returns void\n"));
1643}
1644
1645
1646/**
1647 * This function will tell all the devices to setup up their memory structures
1648 * after VM construction and after VM reset.
1649 *
1650 * @param pVM The cross context VM structure.
1651 * @param fAtReset Indicates the context, after reset if @c true or after
1652 * construction if @c false.
1653 */
1654VMMR3_INT_DECL(void) PDMR3MemSetup(PVM pVM, bool fAtReset)
1655{
1656 LogFlow(("PDMR3MemSetup: fAtReset=%RTbool\n", fAtReset));
1657 PDMDEVMEMSETUPCTX const enmCtx = fAtReset ? PDMDEVMEMSETUPCTX_AFTER_RESET : PDMDEVMEMSETUPCTX_AFTER_CONSTRUCTION;
1658
1659 /*
1660 * Iterate thru the device instances and work the callback.
1661 */
1662 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1663 if (pDevIns->pReg->pfnMemSetup)
1664 {
1665 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1666 pDevIns->pReg->pfnMemSetup(pDevIns, enmCtx);
1667 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1668 }
1669
1670 LogFlow(("PDMR3MemSetup: returns void\n"));
1671}
1672
1673
1674/**
1675 * Retrieves and resets the info left behind by PDMDevHlpVMReset.
1676 *
1677 * @returns True if hard reset, false if soft reset.
1678 * @param pVM The cross context VM structure.
1679 * @param fOverride If non-zero, the override flags will be used instead
1680 * of the reset flags kept by PDM. (For triple faults.)
1681 * @param pfResetFlags Where to return the reset flags (PDMVMRESET_F_XXX).
1682 * @thread EMT
1683 */
1684VMMR3_INT_DECL(bool) PDMR3GetResetInfo(PVM pVM, uint32_t fOverride, uint32_t *pfResetFlags)
1685{
1686 VM_ASSERT_EMT(pVM);
1687
1688 /*
1689 * Get the reset flags.
1690 */
1691 uint32_t fResetFlags;
1692 fResetFlags = ASMAtomicXchgU32(&pVM->pdm.s.fResetFlags, 0);
1693 if (fOverride)
1694 fResetFlags = fOverride;
1695 *pfResetFlags = fResetFlags;
1696
1697 /*
1698 * To try avoid trouble, we never ever do soft/warm resets on SMP systems
1699 * with more than CPU #0 active. However, if only one CPU is active we
1700 * will ask the firmware what it wants us to do (because the firmware may
1701 * depend on the VMM doing a lot of what is normally its responsibility,
1702 * like clearing memory).
1703 */
1704 bool fOtherCpusActive = false;
1705 VMCPUID idCpu = pVM->cCpus;
1706 while (idCpu-- > 1)
1707 {
1708 EMSTATE enmState = EMGetState(pVM->apCpusR3[idCpu]);
1709 if ( enmState != EMSTATE_WAIT_SIPI
1710 && enmState != EMSTATE_NONE)
1711 {
1712 fOtherCpusActive = true;
1713 break;
1714 }
1715 }
1716
1717 bool fHardReset = fOtherCpusActive
1718 || (fResetFlags & PDMVMRESET_F_SRC_MASK) < PDMVMRESET_F_LAST_ALWAYS_HARD
1719 || !pVM->pdm.s.pFirmware
1720 || pVM->pdm.s.pFirmware->Reg.pfnIsHardReset(pVM->pdm.s.pFirmware->pDevIns, fResetFlags);
1721
1722 Log(("PDMR3GetResetInfo: returns fHardReset=%RTbool fResetFlags=%#x\n", fHardReset, fResetFlags));
1723 return fHardReset;
1724}
1725
1726
1727/**
1728 * Performs a soft reset of devices.
1729 *
1730 * @param pVM The cross context VM structure.
1731 * @param fResetFlags PDMVMRESET_F_XXX.
1732 */
1733VMMR3_INT_DECL(void) PDMR3SoftReset(PVM pVM, uint32_t fResetFlags)
1734{
1735 LogFlow(("PDMR3SoftReset: fResetFlags=%#x\n", fResetFlags));
1736
1737 /*
1738 * Iterate thru the device instances and work the callback.
1739 */
1740 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1741 if (pDevIns->pReg->pfnSoftReset)
1742 {
1743 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1744 pDevIns->pReg->pfnSoftReset(pDevIns, fResetFlags);
1745 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1746 }
1747
1748 LogFlow(("PDMR3SoftReset: returns void\n"));
1749}
1750
1751
1752/**
1753 * Worker for PDMR3Suspend that deals with one driver.
1754 *
1755 * @param pDrvIns The driver instance.
1756 * @param pAsync The structure for recording asynchronous
1757 * notification tasks.
1758 * @param pszDevName The parent device name.
1759 * @param iDevInstance The parent device instance number.
1760 * @param iLun The parent LUN number.
1761 */
1762DECLINLINE(bool) pdmR3SuspendDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
1763 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1764{
1765 if (!pDrvIns->Internal.s.fVMSuspended)
1766 {
1767 pDrvIns->Internal.s.fVMSuspended = true;
1768 if (pDrvIns->pReg->pfnSuspend)
1769 {
1770 uint64_t cNsElapsed = RTTimeNanoTS();
1771
1772 if (!pDrvIns->Internal.s.pfnAsyncNotify)
1773 {
1774 LogFlow(("PDMR3Suspend: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1775 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1776 pDrvIns->pReg->pfnSuspend(pDrvIns);
1777 if (pDrvIns->Internal.s.pfnAsyncNotify)
1778 LogFlow(("PDMR3Suspend: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1779 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1780 }
1781 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
1782 {
1783 LogFlow(("PDMR3Suspend: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1784 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1785 pDrvIns->Internal.s.pfnAsyncNotify = NULL;
1786 }
1787
1788 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1789 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1790 LogRel(("PDMR3Suspend: Driver '%s'/%d on LUN#%d of device '%s'/%d took %'llu ns to suspend\n",
1791 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, cNsElapsed));
1792
1793 if (pDrvIns->Internal.s.pfnAsyncNotify)
1794 {
1795 pDrvIns->Internal.s.fVMSuspended = false;
1796 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance, pszDevName, iDevInstance, iLun);
1797 return false;
1798 }
1799 }
1800 }
1801 return true;
1802}
1803
1804
1805/**
1806 * Worker for PDMR3Suspend that deals with one USB device instance.
1807 *
1808 * @param pUsbIns The USB device instance.
1809 * @param pAsync The structure for recording asynchronous
1810 * notification tasks.
1811 */
1812DECLINLINE(void) pdmR3SuspendUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
1813{
1814 if (!pUsbIns->Internal.s.fVMSuspended)
1815 {
1816 pUsbIns->Internal.s.fVMSuspended = true;
1817 if (pUsbIns->pReg->pfnVMSuspend)
1818 {
1819 uint64_t cNsElapsed = RTTimeNanoTS();
1820
1821 if (!pUsbIns->Internal.s.pfnAsyncNotify)
1822 {
1823 LogFlow(("PDMR3Suspend: Notifying - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1824 pUsbIns->pReg->pfnVMSuspend(pUsbIns);
1825 if (pUsbIns->Internal.s.pfnAsyncNotify)
1826 LogFlow(("PDMR3Suspend: Async notification started - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1827 }
1828 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
1829 {
1830 LogFlow(("PDMR3Suspend: Async notification completed - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1831 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
1832 }
1833 if (pUsbIns->Internal.s.pfnAsyncNotify)
1834 {
1835 pUsbIns->Internal.s.fVMSuspended = false;
1836 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
1837 }
1838
1839 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1840 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1841 LogRel(("PDMR3Suspend: USB device '%s'/%d took %'llu ns to suspend\n",
1842 pUsbIns->pReg->szName, pUsbIns->iInstance, cNsElapsed));
1843 }
1844 }
1845}
1846
1847
1848/**
1849 * Worker for PDMR3Suspend that deals with one device instance.
1850 *
1851 * @param pDevIns The device instance.
1852 * @param pAsync The structure for recording asynchronous
1853 * notification tasks.
1854 */
1855DECLINLINE(void) pdmR3SuspendDev(PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
1856{
1857 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED))
1858 {
1859 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_SUSPENDED;
1860 if (pDevIns->pReg->pfnSuspend)
1861 {
1862 uint64_t cNsElapsed = RTTimeNanoTS();
1863 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
1864
1865 if (!pDevIns->Internal.s.pfnAsyncNotify)
1866 {
1867 LogFlow(("PDMR3Suspend: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1868 pDevIns->pReg->pfnSuspend(pDevIns);
1869 if (pDevIns->Internal.s.pfnAsyncNotify)
1870 LogFlow(("PDMR3Suspend: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1871 }
1872 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
1873 {
1874 LogFlow(("PDMR3Suspend: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1875 pDevIns->Internal.s.pfnAsyncNotify = NULL;
1876 }
1877 if (pDevIns->Internal.s.pfnAsyncNotify)
1878 {
1879 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
1880 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
1881 }
1882
1883 PDMCritSectLeave(pDevIns->pCritSectRoR3);
1884 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1885 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1886 LogRel(("PDMR3Suspend: Device '%s'/%d took %'llu ns to suspend\n",
1887 pDevIns->pReg->szName, pDevIns->iInstance, cNsElapsed));
1888 }
1889 }
1890}
1891
1892
1893/**
1894 * This function will notify all the devices and their attached drivers about
1895 * the VM now being suspended.
1896 *
1897 * @param pVM The cross context VM structure.
1898 * @thread EMT(0)
1899 */
1900VMMR3_INT_DECL(void) PDMR3Suspend(PVM pVM)
1901{
1902 LogFlow(("PDMR3Suspend:\n"));
1903 VM_ASSERT_EMT0(pVM);
1904 uint64_t cNsElapsed = RTTimeNanoTS();
1905
1906 /*
1907 * The outer loop repeats until there are no more async requests.
1908 *
1909 * Note! We depend on the suspended indicators to be in the desired state
1910 * and we do not reset them before starting because this allows
1911 * PDMR3PowerOn and PDMR3Resume to use PDMR3Suspend for cleaning up
1912 * on failure.
1913 */
1914 PDMNOTIFYASYNCSTATS Async;
1915 pdmR3NotifyAsyncInit(&Async, "PDMR3Suspend");
1916 for (;;)
1917 {
1918 pdmR3NotifyAsyncBeginLoop(&Async);
1919
1920 /*
1921 * Iterate thru the device instances and USB device instances,
1922 * processing the drivers associated with those.
1923 *
1924 * The attached drivers are normally processed first. Some devices
1925 * (like DevAHCI) though needs to be notified before the drivers so
1926 * that it doesn't kick off any new requests after the drivers stopped
1927 * taking any. (DrvVD changes to read-only in this particular case.)
1928 */
1929 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1930 {
1931 unsigned const cAsyncStart = Async.cAsync;
1932
1933 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION)
1934 pdmR3SuspendDev(pDevIns, &Async);
1935
1936 if (Async.cAsync == cAsyncStart)
1937 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1938 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1939 if (!pdmR3SuspendDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
1940 break;
1941
1942 if ( Async.cAsync == cAsyncStart
1943 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION))
1944 pdmR3SuspendDev(pDevIns, &Async);
1945 }
1946
1947#ifdef VBOX_WITH_USB
1948 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1949 {
1950 unsigned const cAsyncStart = Async.cAsync;
1951
1952 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1953 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1954 if (!pdmR3SuspendDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
1955 break;
1956
1957 if (Async.cAsync == cAsyncStart)
1958 pdmR3SuspendUsb(pUsbIns, &Async);
1959 }
1960#endif
1961 if (!Async.cAsync)
1962 break;
1963 pdmR3NotifyAsyncLog(&Async);
1964 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
1965 }
1966
1967 /*
1968 * Suspend all threads.
1969 */
1970 pdmR3ThreadSuspendAll(pVM);
1971
1972 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1973 LogRel(("PDMR3Suspend: %'llu ns run time\n", cNsElapsed));
1974}
1975
1976
1977/**
1978 * Worker for PDMR3Resume that deals with one driver.
1979 *
1980 * @param pDrvIns The driver instance.
1981 * @param pszDevName The parent device name.
1982 * @param iDevInstance The parent device instance number.
1983 * @param iLun The parent LUN number.
1984 */
1985DECLINLINE(int) pdmR3ResumeDrv(PPDMDRVINS pDrvIns, const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1986{
1987 Assert(pDrvIns->Internal.s.fVMSuspended);
1988 if (pDrvIns->pReg->pfnResume)
1989 {
1990 LogFlow(("PDMR3Resume: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1991 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1992 int rc = VINF_SUCCESS; pDrvIns->pReg->pfnResume(pDrvIns);
1993 if (RT_FAILURE(rc))
1994 {
1995 LogRel(("PDMR3Resume: Driver '%s'/%d on LUN#%d of device '%s'/%d -> %Rrc\n",
1996 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, rc));
1997 return rc;
1998 }
1999 }
2000 pDrvIns->Internal.s.fVMSuspended = false;
2001 return VINF_SUCCESS;
2002}
2003
2004
2005/**
2006 * Worker for PDMR3Resume that deals with one USB device instance.
2007 *
2008 * @returns VBox status code.
2009 * @param pUsbIns The USB device instance.
2010 */
2011DECLINLINE(int) pdmR3ResumeUsb(PPDMUSBINS pUsbIns)
2012{
2013 Assert(pUsbIns->Internal.s.fVMSuspended);
2014 if (pUsbIns->pReg->pfnVMResume)
2015 {
2016 LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
2017 int rc = VINF_SUCCESS; pUsbIns->pReg->pfnVMResume(pUsbIns);
2018 if (RT_FAILURE(rc))
2019 {
2020 LogRel(("PDMR3Resume: Device '%s'/%d -> %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
2021 return rc;
2022 }
2023 }
2024 pUsbIns->Internal.s.fVMSuspended = false;
2025 return VINF_SUCCESS;
2026}
2027
2028
2029/**
2030 * Worker for PDMR3Resume that deals with one device instance.
2031 *
2032 * @returns VBox status code.
2033 * @param pDevIns The device instance.
2034 */
2035DECLINLINE(int) pdmR3ResumeDev(PPDMDEVINS pDevIns)
2036{
2037 Assert(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED);
2038 if (pDevIns->pReg->pfnResume)
2039 {
2040 LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
2041 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
2042 int rc = VINF_SUCCESS; pDevIns->pReg->pfnResume(pDevIns);
2043 PDMCritSectLeave(pDevIns->pCritSectRoR3);
2044 if (RT_FAILURE(rc))
2045 {
2046 LogRel(("PDMR3Resume: Device '%s'/%d -> %Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
2047 return rc;
2048 }
2049 }
2050 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
2051 return VINF_SUCCESS;
2052}
2053
2054
2055/**
2056 * This function will notify all the devices and their
2057 * attached drivers about the VM now being resumed.
2058 *
2059 * @param pVM The cross context VM structure.
2060 */
2061VMMR3_INT_DECL(void) PDMR3Resume(PVM pVM)
2062{
2063 LogFlow(("PDMR3Resume:\n"));
2064
2065 /*
2066 * Iterate thru the device instances and USB device instances,
2067 * processing the drivers associated with those.
2068 */
2069 int rc = VINF_SUCCESS;
2070 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns && RT_SUCCESS(rc); pDevIns = pDevIns->Internal.s.pNextR3)
2071 {
2072 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
2073 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
2074 rc = pdmR3ResumeDrv(pDrvIns, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun);
2075 if (RT_SUCCESS(rc))
2076 rc = pdmR3ResumeDev(pDevIns);
2077 }
2078
2079#ifdef VBOX_WITH_USB
2080 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns && RT_SUCCESS(rc); pUsbIns = pUsbIns->Internal.s.pNext)
2081 {
2082 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
2083 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
2084 rc = pdmR3ResumeDrv(pDrvIns, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun);
2085 if (RT_SUCCESS(rc))
2086 rc = pdmR3ResumeUsb(pUsbIns);
2087 }
2088#endif
2089
2090 /*
2091 * Resume all threads.
2092 */
2093 if (RT_SUCCESS(rc))
2094 pdmR3ThreadResumeAll(pVM);
2095
2096 /*
2097 * Resume the block cache.
2098 */
2099 if (RT_SUCCESS(rc))
2100 pdmR3BlkCacheResume(pVM);
2101
2102 /*
2103 * On failure, clean up via PDMR3Suspend.
2104 */
2105 if (RT_FAILURE(rc))
2106 PDMR3Suspend(pVM);
2107
2108 LogFlow(("PDMR3Resume: returns %Rrc\n", rc));
2109 return /*rc*/;
2110}
2111
2112
2113/**
2114 * Worker for PDMR3PowerOff that deals with one driver.
2115 *
2116 * @param pDrvIns The driver instance.
2117 * @param pAsync The structure for recording asynchronous
2118 * notification tasks.
2119 * @param pszDevName The parent device name.
2120 * @param iDevInstance The parent device instance number.
2121 * @param iLun The parent LUN number.
2122 */
2123DECLINLINE(bool) pdmR3PowerOffDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
2124 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
2125{
2126 if (!pDrvIns->Internal.s.fVMSuspended)
2127 {
2128 pDrvIns->Internal.s.fVMSuspended = true;
2129 if (pDrvIns->pReg->pfnPowerOff)
2130 {
2131 uint64_t cNsElapsed = RTTimeNanoTS();
2132
2133 if (!pDrvIns->Internal.s.pfnAsyncNotify)
2134 {
2135 LogFlow(("PDMR3PowerOff: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
2136 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
2137 pDrvIns->pReg->pfnPowerOff(pDrvIns);
2138 if (pDrvIns->Internal.s.pfnAsyncNotify)
2139 LogFlow(("PDMR3PowerOff: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
2140 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
2141 }
2142 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
2143 {
2144 LogFlow(("PDMR3PowerOff: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
2145 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
2146 pDrvIns->Internal.s.pfnAsyncNotify = NULL;
2147 }
2148
2149 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2150 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
2151 LogRel(("PDMR3PowerOff: Driver '%s'/%d on LUN#%d of device '%s'/%d took %'llu ns to power off\n",
2152 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, cNsElapsed));
2153
2154 if (pDrvIns->Internal.s.pfnAsyncNotify)
2155 {
2156 pDrvIns->Internal.s.fVMSuspended = false;
2157 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance,
2158 pszDevName, iDevInstance, iLun);
2159 return false;
2160 }
2161 }
2162 }
2163 return true;
2164}
2165
2166
2167/**
2168 * Worker for PDMR3PowerOff that deals with one USB device instance.
2169 *
2170 * @param pUsbIns The USB device instance.
2171 * @param pAsync The structure for recording asynchronous
2172 * notification tasks.
2173 */
2174DECLINLINE(void) pdmR3PowerOffUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
2175{
2176 if (!pUsbIns->Internal.s.fVMSuspended)
2177 {
2178 pUsbIns->Internal.s.fVMSuspended = true;
2179 if (pUsbIns->pReg->pfnVMPowerOff)
2180 {
2181 uint64_t cNsElapsed = RTTimeNanoTS();
2182
2183 if (!pUsbIns->Internal.s.pfnAsyncNotify)
2184 {
2185 LogFlow(("PDMR3PowerOff: Notifying - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
2186 pUsbIns->pReg->pfnVMPowerOff(pUsbIns);
2187 if (pUsbIns->Internal.s.pfnAsyncNotify)
2188 LogFlow(("PDMR3PowerOff: Async notification started - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
2189 }
2190 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
2191 {
2192 LogFlow(("PDMR3PowerOff: Async notification completed - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
2193 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
2194 }
2195 if (pUsbIns->Internal.s.pfnAsyncNotify)
2196 {
2197 pUsbIns->Internal.s.fVMSuspended = false;
2198 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
2199 }
2200
2201 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2202 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
2203 LogRel(("PDMR3PowerOff: USB device '%s'/%d took %'llu ns to power off\n",
2204 pUsbIns->pReg->szName, pUsbIns->iInstance, cNsElapsed));
2205
2206 }
2207 }
2208}
2209
2210
2211/**
2212 * Worker for PDMR3PowerOff that deals with one device instance.
2213 *
2214 * @param pDevIns The device instance.
2215 * @param pAsync The structure for recording asynchronous
2216 * notification tasks.
2217 */
2218DECLINLINE(void) pdmR3PowerOffDev(PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
2219{
2220 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED))
2221 {
2222 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_SUSPENDED;
2223 if (pDevIns->pReg->pfnPowerOff)
2224 {
2225 uint64_t cNsElapsed = RTTimeNanoTS();
2226 PDMCritSectEnter(pDevIns->pCritSectRoR3, VERR_IGNORED);
2227
2228 if (!pDevIns->Internal.s.pfnAsyncNotify)
2229 {
2230 LogFlow(("PDMR3PowerOff: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
2231 pDevIns->pReg->pfnPowerOff(pDevIns);
2232 if (pDevIns->Internal.s.pfnAsyncNotify)
2233 LogFlow(("PDMR3PowerOff: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
2234 }
2235 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
2236 {
2237 LogFlow(("PDMR3PowerOff: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
2238 pDevIns->Internal.s.pfnAsyncNotify = NULL;
2239 }
2240 if (pDevIns->Internal.s.pfnAsyncNotify)
2241 {
2242 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
2243 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
2244 }
2245
2246 PDMCritSectLeave(pDevIns->pCritSectRoR3);
2247 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2248 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
2249 LogFlow(("PDMR3PowerOff: Device '%s'/%d took %'llu ns to power off\n",
2250 pDevIns->pReg->szName, pDevIns->iInstance, cNsElapsed));
2251 }
2252 }
2253}
2254
2255
2256/**
2257 * This function will notify all the devices and their
2258 * attached drivers about the VM being powered off.
2259 *
2260 * @param pVM The cross context VM structure.
2261 */
2262VMMR3DECL(void) PDMR3PowerOff(PVM pVM)
2263{
2264 LogFlow(("PDMR3PowerOff:\n"));
2265 uint64_t cNsElapsed = RTTimeNanoTS();
2266
2267 /*
2268 * Clear the suspended flags on all devices and drivers first because they
2269 * might have been set during a suspend but the power off callbacks should
2270 * be called in any case.
2271 */
2272 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2273 {
2274 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
2275
2276 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2277 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2278 pDrvIns->Internal.s.fVMSuspended = false;
2279 }
2280
2281#ifdef VBOX_WITH_USB
2282 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2283 {
2284 pUsbIns->Internal.s.fVMSuspended = false;
2285
2286 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2287 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2288 pDrvIns->Internal.s.fVMSuspended = false;
2289 }
2290#endif
2291
2292 /*
2293 * The outer loop repeats until there are no more async requests.
2294 */
2295 PDMNOTIFYASYNCSTATS Async;
2296 pdmR3NotifyAsyncInit(&Async, "PDMR3PowerOff");
2297 for (;;)
2298 {
2299 pdmR3NotifyAsyncBeginLoop(&Async);
2300
2301 /*
2302 * Iterate thru the device instances and USB device instances,
2303 * processing the drivers associated with those.
2304 *
2305 * The attached drivers are normally processed first. Some devices
2306 * (like DevAHCI) though needs to be notified before the drivers so
2307 * that it doesn't kick off any new requests after the drivers stopped
2308 * taking any. (DrvVD changes to read-only in this particular case.)
2309 */
2310 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2311 {
2312 unsigned const cAsyncStart = Async.cAsync;
2313
2314 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION)
2315 pdmR3PowerOffDev(pDevIns, &Async);
2316
2317 if (Async.cAsync == cAsyncStart)
2318 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2319 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2320 if (!pdmR3PowerOffDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
2321 break;
2322
2323 if ( Async.cAsync == cAsyncStart
2324 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION))
2325 pdmR3PowerOffDev(pDevIns, &Async);
2326 }
2327
2328#ifdef VBOX_WITH_USB
2329 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2330 {
2331 unsigned const cAsyncStart = Async.cAsync;
2332
2333 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2334 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2335 if (!pdmR3PowerOffDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
2336 break;
2337
2338 if (Async.cAsync == cAsyncStart)
2339 pdmR3PowerOffUsb(pUsbIns, &Async);
2340 }
2341#endif
2342 if (!Async.cAsync)
2343 break;
2344 pdmR3NotifyAsyncLog(&Async);
2345 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
2346 }
2347
2348 /*
2349 * Suspend all threads.
2350 */
2351 pdmR3ThreadSuspendAll(pVM);
2352
2353 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2354 LogRel(("PDMR3PowerOff: %'llu ns run time\n", cNsElapsed));
2355}
2356
2357
2358/**
2359 * Queries the base interface of a device instance.
2360 *
2361 * The caller can use this to query other interfaces the device implements
2362 * and use them to talk to the device.
2363 *
2364 * @returns VBox status code.
2365 * @param pUVM The user mode VM handle.
2366 * @param pszDevice Device name.
2367 * @param iInstance Device instance.
2368 * @param ppBase Where to store the pointer to the base device interface on success.
2369 * @remark We're not doing any locking ATM, so don't try call this at times when the
2370 * device chain is known to be updated.
2371 */
2372VMMR3DECL(int) PDMR3QueryDevice(PUVM pUVM, const char *pszDevice, unsigned iInstance, PPDMIBASE *ppBase)
2373{
2374 LogFlow(("PDMR3DeviceQuery: pszDevice=%p:{%s} iInstance=%u ppBase=%p\n", pszDevice, pszDevice, iInstance, ppBase));
2375 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2376 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2377
2378 /*
2379 * Iterate registered devices looking for the device.
2380 */
2381 size_t cchDevice = strlen(pszDevice);
2382 for (PPDMDEV pDev = pUVM->pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
2383 {
2384 if ( pDev->cchName == cchDevice
2385 && !memcmp(pDev->pReg->szName, pszDevice, cchDevice))
2386 {
2387 /*
2388 * Iterate device instances.
2389 */
2390 for (PPDMDEVINS pDevIns = pDev->pInstances; pDevIns; pDevIns = pDevIns->Internal.s.pPerDeviceNextR3)
2391 {
2392 if (pDevIns->iInstance == iInstance)
2393 {
2394 if (pDevIns->IBase.pfnQueryInterface)
2395 {
2396 *ppBase = &pDevIns->IBase;
2397 LogFlow(("PDMR3DeviceQuery: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
2398 return VINF_SUCCESS;
2399 }
2400
2401 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NO_IBASE\n"));
2402 return VERR_PDM_DEVICE_INSTANCE_NO_IBASE;
2403 }
2404 }
2405
2406 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NOT_FOUND\n"));
2407 return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND;
2408 }
2409 }
2410
2411 LogFlow(("PDMR3QueryDevice: returns VERR_PDM_DEVICE_NOT_FOUND\n"));
2412 return VERR_PDM_DEVICE_NOT_FOUND;
2413}
2414
2415
2416/**
2417 * Queries the base interface of a device LUN.
2418 *
2419 * This differs from PDMR3QueryLun by that it returns the interface on the
2420 * device and not the top level driver.
2421 *
2422 * @returns VBox status code.
2423 * @param pUVM The user mode VM handle.
2424 * @param pszDevice Device name.
2425 * @param iInstance Device instance.
2426 * @param iLun The Logical Unit to obtain the interface of.
2427 * @param ppBase Where to store the base interface pointer.
2428 * @remark We're not doing any locking ATM, so don't try call this at times when the
2429 * device chain is known to be updated.
2430 */
2431VMMR3DECL(int) PDMR3QueryDeviceLun(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
2432{
2433 LogFlow(("PDMR3QueryDeviceLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
2434 pszDevice, pszDevice, iInstance, iLun, ppBase));
2435 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2436 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2437
2438 /*
2439 * Find the LUN.
2440 */
2441 PPDMLUN pLun;
2442 int rc = pdmR3DevFindLun(pUVM->pVM, pszDevice, iInstance, iLun, &pLun);
2443 if (RT_SUCCESS(rc))
2444 {
2445 *ppBase = pLun->pBase;
2446 LogFlow(("PDMR3QueryDeviceLun: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
2447 return VINF_SUCCESS;
2448 }
2449 LogFlow(("PDMR3QueryDeviceLun: returns %Rrc\n", rc));
2450 return rc;
2451}
2452
2453
2454/**
2455 * Query the interface of the top level driver on a LUN.
2456 *
2457 * @returns VBox status code.
2458 * @param pUVM The user mode VM handle.
2459 * @param pszDevice Device name.
2460 * @param iInstance Device instance.
2461 * @param iLun The Logical Unit to obtain the interface of.
2462 * @param ppBase Where to store the base interface pointer.
2463 * @remark We're not doing any locking ATM, so don't try call this at times when the
2464 * device chain is known to be updated.
2465 */
2466VMMR3DECL(int) PDMR3QueryLun(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
2467{
2468 LogFlow(("PDMR3QueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
2469 pszDevice, pszDevice, iInstance, iLun, ppBase));
2470 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2471 PVM pVM = pUVM->pVM;
2472 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
2473
2474 /*
2475 * Find the LUN.
2476 */
2477 PPDMLUN pLun;
2478 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
2479 if (RT_SUCCESS(rc))
2480 {
2481 if (pLun->pTop)
2482 {
2483 *ppBase = &pLun->pTop->IBase;
2484 LogFlow(("PDMR3QueryLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
2485 return VINF_SUCCESS;
2486 }
2487 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
2488 }
2489 LogFlow(("PDMR3QueryLun: returns %Rrc\n", rc));
2490 return rc;
2491}
2492
2493
2494/**
2495 * Query the interface of a named driver on a LUN.
2496 *
2497 * If the driver appears more than once in the driver chain, the first instance
2498 * is returned.
2499 *
2500 * @returns VBox status code.
2501 * @param pUVM The user mode VM handle.
2502 * @param pszDevice Device name.
2503 * @param iInstance Device instance.
2504 * @param iLun The Logical Unit to obtain the interface of.
2505 * @param pszDriver The driver name.
2506 * @param ppBase Where to store the base interface pointer.
2507 *
2508 * @remark We're not doing any locking ATM, so don't try call this at times when the
2509 * device chain is known to be updated.
2510 */
2511VMMR3DECL(int) PDMR3QueryDriverOnLun(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, const char *pszDriver, PPPDMIBASE ppBase)
2512{
2513 LogFlow(("PDMR3QueryDriverOnLun: pszDevice=%p:{%s} iInstance=%u iLun=%u pszDriver=%p:{%s} ppBase=%p\n",
2514 pszDevice, pszDevice, iInstance, iLun, pszDriver, pszDriver, ppBase));
2515 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2516 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2517
2518 /*
2519 * Find the LUN.
2520 */
2521 PPDMLUN pLun;
2522 int rc = pdmR3DevFindLun(pUVM->pVM, pszDevice, iInstance, iLun, &pLun);
2523 if (RT_SUCCESS(rc))
2524 {
2525 if (pLun->pTop)
2526 {
2527 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2528 if (!strcmp(pDrvIns->pReg->szName, pszDriver))
2529 {
2530 *ppBase = &pDrvIns->IBase;
2531 LogFlow(("PDMR3QueryDriverOnLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
2532 return VINF_SUCCESS;
2533
2534 }
2535 rc = VERR_PDM_DRIVER_NOT_FOUND;
2536 }
2537 else
2538 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
2539 }
2540 LogFlow(("PDMR3QueryDriverOnLun: returns %Rrc\n", rc));
2541 return rc;
2542}
2543
2544/**
2545 * Executes pending DMA transfers.
2546 * Forced Action handler.
2547 *
2548 * @param pVM The cross context VM structure.
2549 */
2550VMMR3DECL(void) PDMR3DmaRun(PVM pVM)
2551{
2552 /* Note! Not really SMP safe; restrict it to VCPU 0. */
2553 if (VMMGetCpuId(pVM) != 0)
2554 return;
2555
2556 if (VM_FF_TEST_AND_CLEAR(pVM, VM_FF_PDM_DMA))
2557 {
2558 if (pVM->pdm.s.pDmac)
2559 {
2560 bool fMore = pVM->pdm.s.pDmac->Reg.pfnRun(pVM->pdm.s.pDmac->pDevIns);
2561 if (fMore)
2562 VM_FF_SET(pVM, VM_FF_PDM_DMA);
2563 }
2564 }
2565}
2566
2567
2568/**
2569 * Service a VMMCALLRING3_PDM_LOCK call.
2570 *
2571 * @returns VBox status code.
2572 * @param pVM The cross context VM structure.
2573 */
2574VMMR3_INT_DECL(int) PDMR3LockCall(PVM pVM)
2575{
2576 return PDMR3CritSectEnterEx(&pVM->pdm.s.CritSect, true /* fHostCall */);
2577}
2578
2579
2580/**
2581 * Allocates memory from the VMM device heap.
2582 *
2583 * @returns VBox status code.
2584 * @param pVM The cross context VM structure.
2585 * @param cbSize Allocation size.
2586 * @param pfnNotify Mapping/unmapping notification callback.
2587 * @param ppv Ring-3 pointer. (out)
2588 */
2589VMMR3_INT_DECL(int) PDMR3VmmDevHeapAlloc(PVM pVM, size_t cbSize, PFNPDMVMMDEVHEAPNOTIFY pfnNotify, RTR3PTR *ppv)
2590{
2591#ifdef DEBUG_bird
2592 if (!cbSize || cbSize > pVM->pdm.s.cbVMMDevHeapLeft)
2593 return VERR_NO_MEMORY;
2594#else
2595 AssertReturn(cbSize && cbSize <= pVM->pdm.s.cbVMMDevHeapLeft, VERR_NO_MEMORY);
2596#endif
2597
2598 Log(("PDMR3VMMDevHeapAlloc: %#zx\n", cbSize));
2599
2600 /** @todo Not a real heap as there's currently only one user. */
2601 *ppv = pVM->pdm.s.pvVMMDevHeap;
2602 pVM->pdm.s.cbVMMDevHeapLeft = 0;
2603 pVM->pdm.s.pfnVMMDevHeapNotify = pfnNotify;
2604 return VINF_SUCCESS;
2605}
2606
2607
2608/**
2609 * Frees memory from the VMM device heap
2610 *
2611 * @returns VBox status code.
2612 * @param pVM The cross context VM structure.
2613 * @param pv Ring-3 pointer.
2614 */
2615VMMR3_INT_DECL(int) PDMR3VmmDevHeapFree(PVM pVM, RTR3PTR pv)
2616{
2617 Log(("PDMR3VmmDevHeapFree: %RHv\n", pv)); RT_NOREF_PV(pv);
2618
2619 /** @todo not a real heap as there's currently only one user. */
2620 pVM->pdm.s.cbVMMDevHeapLeft = pVM->pdm.s.cbVMMDevHeap;
2621 pVM->pdm.s.pfnVMMDevHeapNotify = NULL;
2622 return VINF_SUCCESS;
2623}
2624
2625
2626/**
2627 * Worker for DBGFR3TraceConfig that checks if the given tracing group name
2628 * matches a device or driver name and applies the tracing config change.
2629 *
2630 * @returns VINF_SUCCESS or VERR_NOT_FOUND.
2631 * @param pVM The cross context VM structure.
2632 * @param pszName The tracing config group name. This is NULL if
2633 * the operation applies to every device and
2634 * driver.
2635 * @param cchName The length to match.
2636 * @param fEnable Whether to enable or disable the corresponding
2637 * trace points.
2638 * @param fApply Whether to actually apply the changes or just do
2639 * existence checks.
2640 */
2641VMMR3_INT_DECL(int) PDMR3TracingConfig(PVM pVM, const char *pszName, size_t cchName, bool fEnable, bool fApply)
2642{
2643 /** @todo This code is potentially racing driver attaching and detaching. */
2644
2645 /*
2646 * Applies to all.
2647 */
2648 if (pszName == NULL)
2649 {
2650 AssertReturn(fApply, VINF_SUCCESS);
2651
2652 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2653 {
2654 pDevIns->fTracing = fEnable;
2655 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2656 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2657 pDrvIns->fTracing = fEnable;
2658 }
2659
2660#ifdef VBOX_WITH_USB
2661 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2662 {
2663 pUsbIns->fTracing = fEnable;
2664 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2665 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2666 pDrvIns->fTracing = fEnable;
2667
2668 }
2669#endif
2670 return VINF_SUCCESS;
2671 }
2672
2673 /*
2674 * Specific devices, USB devices or drivers.
2675 * Decode prefix to figure which of these it applies to.
2676 */
2677 if (cchName <= 3)
2678 return VERR_NOT_FOUND;
2679
2680 uint32_t cMatches = 0;
2681 if (!strncmp("dev", pszName, 3))
2682 {
2683 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2684 {
2685 const char *pszDevName = pDevIns->Internal.s.pDevR3->pReg->szName;
2686 size_t cchDevName = strlen(pszDevName);
2687 if ( ( cchDevName == cchName
2688 && RTStrNICmp(pszName, pszDevName, cchDevName))
2689 || ( cchDevName == cchName - 3
2690 && RTStrNICmp(pszName + 3, pszDevName, cchDevName)) )
2691 {
2692 cMatches++;
2693 if (fApply)
2694 pDevIns->fTracing = fEnable;
2695 }
2696 }
2697 }
2698 else if (!strncmp("usb", pszName, 3))
2699 {
2700 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2701 {
2702 const char *pszUsbName = pUsbIns->Internal.s.pUsbDev->pReg->szName;
2703 size_t cchUsbName = strlen(pszUsbName);
2704 if ( ( cchUsbName == cchName
2705 && RTStrNICmp(pszName, pszUsbName, cchUsbName))
2706 || ( cchUsbName == cchName - 3
2707 && RTStrNICmp(pszName + 3, pszUsbName, cchUsbName)) )
2708 {
2709 cMatches++;
2710 if (fApply)
2711 pUsbIns->fTracing = fEnable;
2712 }
2713 }
2714 }
2715 else if (!strncmp("drv", pszName, 3))
2716 {
2717 AssertReturn(fApply, VINF_SUCCESS);
2718
2719 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2720 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2721 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2722 {
2723 const char *pszDrvName = pDrvIns->Internal.s.pDrv->pReg->szName;
2724 size_t cchDrvName = strlen(pszDrvName);
2725 if ( ( cchDrvName == cchName
2726 && RTStrNICmp(pszName, pszDrvName, cchDrvName))
2727 || ( cchDrvName == cchName - 3
2728 && RTStrNICmp(pszName + 3, pszDrvName, cchDrvName)) )
2729 {
2730 cMatches++;
2731 if (fApply)
2732 pDrvIns->fTracing = fEnable;
2733 }
2734 }
2735
2736#ifdef VBOX_WITH_USB
2737 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2738 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2739 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2740 {
2741 const char *pszDrvName = pDrvIns->Internal.s.pDrv->pReg->szName;
2742 size_t cchDrvName = strlen(pszDrvName);
2743 if ( ( cchDrvName == cchName
2744 && RTStrNICmp(pszName, pszDrvName, cchDrvName))
2745 || ( cchDrvName == cchName - 3
2746 && RTStrNICmp(pszName + 3, pszDrvName, cchDrvName)) )
2747 {
2748 cMatches++;
2749 if (fApply)
2750 pDrvIns->fTracing = fEnable;
2751 }
2752 }
2753#endif
2754 }
2755 else
2756 return VERR_NOT_FOUND;
2757
2758 return cMatches > 0 ? VINF_SUCCESS : VERR_NOT_FOUND;
2759}
2760
2761
2762/**
2763 * Worker for DBGFR3TraceQueryConfig that checks whether all drivers, devices,
2764 * and USB device have the same tracing settings.
2765 *
2766 * @returns true / false.
2767 * @param pVM The cross context VM structure.
2768 * @param fEnabled The tracing setting to check for.
2769 */
2770VMMR3_INT_DECL(bool) PDMR3TracingAreAll(PVM pVM, bool fEnabled)
2771{
2772 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2773 {
2774 if (pDevIns->fTracing != (uint32_t)fEnabled)
2775 return false;
2776
2777 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2778 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2779 if (pDrvIns->fTracing != (uint32_t)fEnabled)
2780 return false;
2781 }
2782
2783#ifdef VBOX_WITH_USB
2784 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2785 {
2786 if (pUsbIns->fTracing != (uint32_t)fEnabled)
2787 return false;
2788
2789 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2790 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2791 if (pDrvIns->fTracing != (uint32_t)fEnabled)
2792 return false;
2793 }
2794#endif
2795
2796 return true;
2797}
2798
2799
2800/**
2801 * Worker for PDMR3TracingQueryConfig that adds a prefixed name to the output
2802 * string.
2803 *
2804 * @returns VINF_SUCCESS or VERR_BUFFER_OVERFLOW
2805 * @param ppszDst The pointer to the output buffer pointer.
2806 * @param pcbDst The pointer to the output buffer size.
2807 * @param fSpace Whether to add a space before the name.
2808 * @param pszPrefix The name prefix.
2809 * @param pszName The name.
2810 */
2811static int pdmR3TracingAdd(char **ppszDst, size_t *pcbDst, bool fSpace, const char *pszPrefix, const char *pszName)
2812{
2813 size_t const cchPrefix = strlen(pszPrefix);
2814 if (!RTStrNICmp(pszPrefix, pszName, cchPrefix))
2815 pszName += cchPrefix;
2816 size_t const cchName = strlen(pszName);
2817
2818 size_t const cchThis = cchName + cchPrefix + fSpace;
2819 if (cchThis >= *pcbDst)
2820 return VERR_BUFFER_OVERFLOW;
2821 if (fSpace)
2822 {
2823 **ppszDst = ' ';
2824 memcpy(*ppszDst + 1, pszPrefix, cchPrefix);
2825 memcpy(*ppszDst + 1 + cchPrefix, pszName, cchName + 1);
2826 }
2827 else
2828 {
2829 memcpy(*ppszDst, pszPrefix, cchPrefix);
2830 memcpy(*ppszDst + cchPrefix, pszName, cchName + 1);
2831 }
2832 *ppszDst += cchThis;
2833 *pcbDst -= cchThis;
2834 return VINF_SUCCESS;
2835}
2836
2837
2838/**
2839 * Worker for DBGFR3TraceQueryConfig use when not everything is either enabled
2840 * or disabled.
2841 *
2842 * @returns VINF_SUCCESS or VERR_BUFFER_OVERFLOW
2843 * @param pVM The cross context VM structure.
2844 * @param pszConfig Where to store the config spec.
2845 * @param cbConfig The size of the output buffer.
2846 */
2847VMMR3_INT_DECL(int) PDMR3TracingQueryConfig(PVM pVM, char *pszConfig, size_t cbConfig)
2848{
2849 int rc;
2850 char *pszDst = pszConfig;
2851 size_t cbDst = cbConfig;
2852
2853 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2854 {
2855 if (pDevIns->fTracing)
2856 {
2857 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "dev", pDevIns->Internal.s.pDevR3->pReg->szName);
2858 if (RT_FAILURE(rc))
2859 return rc;
2860 }
2861
2862 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2863 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2864 if (pDrvIns->fTracing)
2865 {
2866 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "drv", pDrvIns->Internal.s.pDrv->pReg->szName);
2867 if (RT_FAILURE(rc))
2868 return rc;
2869 }
2870 }
2871
2872#ifdef VBOX_WITH_USB
2873 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2874 {
2875 if (pUsbIns->fTracing)
2876 {
2877 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "usb", pUsbIns->Internal.s.pUsbDev->pReg->szName);
2878 if (RT_FAILURE(rc))
2879 return rc;
2880 }
2881
2882 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2883 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2884 if (pDrvIns->fTracing)
2885 {
2886 rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "drv", pDrvIns->Internal.s.pDrv->pReg->szName);
2887 if (RT_FAILURE(rc))
2888 return rc;
2889 }
2890 }
2891#endif
2892
2893 return VINF_SUCCESS;
2894}
2895
2896
2897/**
2898 * Checks that a PDMDRVREG::szName, PDMDEVREG::szName or PDMUSBREG::szName
2899 * field contains only a limited set of ASCII characters.
2900 *
2901 * @returns true / false.
2902 * @param pszName The name to validate.
2903 */
2904bool pdmR3IsValidName(const char *pszName)
2905{
2906 char ch;
2907 while ( (ch = *pszName) != '\0'
2908 && ( RT_C_IS_ALNUM(ch)
2909 || ch == '-'
2910 || ch == ' ' /** @todo disallow this! */
2911 || ch == '_') )
2912 pszName++;
2913 return ch == '\0';
2914}
2915
2916
2917/**
2918 * Info handler for 'pdmtracingids'.
2919 *
2920 * @param pVM The cross context VM structure.
2921 * @param pHlp The output helpers.
2922 * @param pszArgs The optional user arguments.
2923 *
2924 * @remarks Can be called on most threads.
2925 */
2926static DECLCALLBACK(void) pdmR3InfoTracingIds(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2927{
2928 /*
2929 * Parse the argument (optional).
2930 */
2931 if ( pszArgs
2932 && *pszArgs
2933 && strcmp(pszArgs, "all")
2934 && strcmp(pszArgs, "devices")
2935 && strcmp(pszArgs, "drivers")
2936 && strcmp(pszArgs, "usb"))
2937 {
2938 pHlp->pfnPrintf(pHlp, "Unable to grok '%s'\n", pszArgs);
2939 return;
2940 }
2941 bool fAll = !pszArgs || !*pszArgs || !strcmp(pszArgs, "all");
2942 bool fDevices = fAll || !strcmp(pszArgs, "devices");
2943 bool fUsbDevs = fAll || !strcmp(pszArgs, "usb");
2944 bool fDrivers = fAll || !strcmp(pszArgs, "drivers");
2945
2946 /*
2947 * Produce the requested output.
2948 */
2949/** @todo lock PDM lists! */
2950 /* devices */
2951 if (fDevices)
2952 {
2953 pHlp->pfnPrintf(pHlp, "Device tracing IDs:\n");
2954 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2955 pHlp->pfnPrintf(pHlp, "%05u %s\n", pDevIns->idTracing, pDevIns->Internal.s.pDevR3->pReg->szName);
2956 }
2957
2958 /* USB devices */
2959 if (fUsbDevs)
2960 {
2961 pHlp->pfnPrintf(pHlp, "USB device tracing IDs:\n");
2962 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2963 pHlp->pfnPrintf(pHlp, "%05u %s\n", pUsbIns->idTracing, pUsbIns->Internal.s.pUsbDev->pReg->szName);
2964 }
2965
2966 /* Drivers */
2967 if (fDrivers)
2968 {
2969 pHlp->pfnPrintf(pHlp, "Driver tracing IDs:\n");
2970 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2971 {
2972 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2973 {
2974 uint32_t iLevel = 0;
2975 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown, iLevel++)
2976 pHlp->pfnPrintf(pHlp, "%05u %s (level %u, lun %u, dev %s)\n",
2977 pDrvIns->idTracing, pDrvIns->Internal.s.pDrv->pReg->szName,
2978 iLevel, pLun->iLun, pDevIns->Internal.s.pDevR3->pReg->szName);
2979 }
2980 }
2981
2982 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2983 {
2984 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2985 {
2986 uint32_t iLevel = 0;
2987 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown, iLevel++)
2988 pHlp->pfnPrintf(pHlp, "%05u %s (level %u, lun %u, dev %s)\n",
2989 pDrvIns->idTracing, pDrvIns->Internal.s.pDrv->pReg->szName,
2990 iLevel, pLun->iLun, pUsbIns->Internal.s.pUsbDev->pReg->szName);
2991 }
2992 }
2993 }
2994}
2995
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