VirtualBox

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

Last change on this file since 84459 was 84459, checked in by vboxsync, 5 years ago

VMM/PDM: DBGF even tracing integration, bugref:9210

Integrates the new DBGF event tracing framework into PDM
devices. The new CFGM key "TracingEnabled" for a device
instance enables tracing using DBGF. A special tracing variant
of the PDM device helper is provided.

Disabled by default for now, enable with VBOX_WITH_DBGF_TRACING

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