VirtualBox

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

Last change on this file since 94319 was 93991, checked in by vboxsync, 3 years ago

VMM: Fix a possible PDM queue leak when destroying a VM, bugref:10093

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