VirtualBox

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

Last change on this file since 97178 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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