VirtualBox

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

Last change on this file since 78254 was 78208, checked in by vboxsync, 6 years ago

PDM,APIC,PIC: Don't use AssertRelease on VMCPU_FF_INTERRUPT_PIC, VMCPU_FF_INTERRUPT_APIC and friends during restore. Instead added debug assertions in the relevant functions causing these to be set by misbehaving device state loaders. ticketref:18331 ticketref:18265

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