VirtualBox

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

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

VMM: Kicking out raw-mode (work in progress) - VM,VMM. bugref:9517

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