VirtualBox

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

Last change on this file since 61366 was 61366, checked in by vboxsync, 8 years ago

VMM/PDM: Missed relocation for the newly added callback to the IOAPIC registration structure.

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