VirtualBox

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

Last change on this file since 78126 was 77348, checked in by vboxsync, 6 years ago

PDM: Added too on seemingly bogus release assertions. ticketref:18331

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