VirtualBox

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

Last change on this file since 99385 was 98122, checked in by vboxsync, 2 years ago

VMM/PDM: Fixed assertion in PDMR3ResumeUsb after attaching a virtual MSD.

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