VirtualBox

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

Last change on this file since 107044 was 106061, checked in by vboxsync, 2 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 122.3 KB
Line 
1/* $Id: PDM.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * PDM - Pluggable Device Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2024 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 RTCritSectInitNamed(&pUVM->pdm.s.ListCritSect, "PDM-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 if (RT_SUCCESS(rc))
505 rc = RTCritSectRwInitNamed(&pVM->pdm.s.CoreListCritSectRw, "PDM-CoreListCritSectRw");
506
507 /*
508 * Initialize sub components.
509 */
510 if (RT_SUCCESS(rc))
511 rc = pdmR3TaskInit(pVM);
512 if (RT_SUCCESS(rc))
513 rc = pdmR3LdrInitU(pVM->pUVM);
514#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
515 if (RT_SUCCESS(rc))
516 rc = pdmR3AsyncCompletionInit(pVM);
517#endif
518#ifdef VBOX_WITH_NETSHAPER
519 if (RT_SUCCESS(rc))
520 rc = pdmR3NetShaperInit(pVM);
521#endif
522 if (RT_SUCCESS(rc))
523 rc = pdmR3BlkCacheInit(pVM);
524 if (RT_SUCCESS(rc))
525 rc = pdmR3DrvInit(pVM);
526 if (RT_SUCCESS(rc))
527 rc = pdmR3DevInit(pVM);
528 if (RT_SUCCESS(rc))
529 {
530 /*
531 * Register the saved state data unit.
532 */
533 rc = SSMR3RegisterInternal(pVM, "pdm", 1, PDM_SAVED_STATE_VERSION, 128,
534 NULL, pdmR3LiveExec, NULL,
535 NULL, pdmR3SaveExec, NULL,
536 pdmR3LoadPrep, pdmR3LoadExec, NULL);
537 if (RT_SUCCESS(rc))
538 {
539 /*
540 * Register the info handlers.
541 */
542 DBGFR3InfoRegisterInternal(pVM, "pdmtracingids",
543 "Displays the tracing IDs assigned by PDM to devices, USB device, drivers and more.",
544 pdmR3InfoTracingIds);
545
546 LogFlow(("PDM: Successfully initialized\n"));
547 return rc;
548 }
549 }
550
551 /*
552 * Cleanup and return failure.
553 */
554 PDMR3Term(pVM);
555 LogFlow(("PDMR3Init: returns %Rrc\n", rc));
556 return rc;
557}
558
559
560/**
561 * Init phase completed callback.
562 *
563 * We use this for calling PDMDEVREG::pfnInitComplete callback after everything
564 * else has been initialized.
565 *
566 * @returns VBox status code.
567 * @param pVM The cross context VM structure.
568 * @param enmWhat The phase that was completed.
569 */
570VMMR3_INT_DECL(int) PDMR3InitCompleted(PVM pVM, VMINITCOMPLETED enmWhat)
571{
572 if (enmWhat == VMINITCOMPLETED_RING0)
573 return pdmR3DevInitComplete(pVM);
574 return VINF_SUCCESS;
575}
576
577
578/**
579 * Applies relocations to data and code managed by this
580 * component. This function will be called at init and
581 * whenever the VMM need to relocate it self inside the GC.
582 *
583 * @param pVM The cross context VM structure.
584 * @param offDelta Relocation delta relative to old location.
585 * @remark The loader subcomponent is relocated by PDMR3LdrRelocate() very
586 * early in the relocation phase.
587 */
588VMMR3_INT_DECL(void) PDMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
589{
590 LogFlow(("PDMR3Relocate\n"));
591 RT_NOREF(pVM, offDelta);
592
593#ifdef VBOX_WITH_RAW_MODE_KEEP /* needs fixing */
594 /*
595 * The registered PIC.
596 */
597 if (pVM->pdm.s.Pic.pDevInsRC)
598 {
599 pVM->pdm.s.Pic.pDevInsRC += offDelta;
600 pVM->pdm.s.Pic.pfnSetIrqRC += offDelta;
601 pVM->pdm.s.Pic.pfnGetInterruptRC += offDelta;
602 }
603
604 /*
605 * The registered APIC.
606 */
607 if (pVM->pdm.s.Apic.pDevInsRC)
608 pVM->pdm.s.Apic.pDevInsRC += offDelta;
609
610 /*
611 * The registered I/O APIC.
612 */
613 if (pVM->pdm.s.IoApic.pDevInsRC)
614 {
615 pVM->pdm.s.IoApic.pDevInsRC += offDelta;
616 pVM->pdm.s.IoApic.pfnSetIrqRC += offDelta;
617 if (pVM->pdm.s.IoApic.pfnSendMsiRC)
618 pVM->pdm.s.IoApic.pfnSendMsiRC += offDelta;
619 if (pVM->pdm.s.IoApic.pfnSetEoiRC)
620 pVM->pdm.s.IoApic.pfnSetEoiRC += offDelta;
621 }
622
623 /*
624 * Devices & Drivers.
625 */
626 int rc;
627 PCPDMDEVHLPRC pDevHlpRC = NIL_RTRCPTR;
628 if (VM_IS_RAW_MODE_ENABLED(pVM))
629 {
630 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_pdmRCDevHlp", &pDevHlpRC);
631 AssertReleaseMsgRC(rc, ("rc=%Rrc when resolving g_pdmRCDevHlp\n", rc));
632 }
633
634 PCPDMDRVHLPRC pDrvHlpRC = NIL_RTRCPTR;
635 if (VM_IS_RAW_MODE_ENABLED(pVM))
636 {
637 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_pdmRCDevHlp", &pDrvHlpRC);
638 AssertReleaseMsgRC(rc, ("rc=%Rrc when resolving g_pdmRCDevHlp\n", rc));
639 }
640
641 RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
642 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
643 {
644 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_RC)
645 {
646 pDevIns->pHlpRC = pDevHlpRC;
647 pDevIns->pvInstanceDataRC = MMHyperR3ToRC(pVM, pDevIns->pvInstanceDataR3);
648 if (pDevIns->pCritSectRoR3)
649 pDevIns->pCritSectRoRC = MMHyperR3ToRC(pVM, pDevIns->pCritSectRoR3);
650 pDevIns->Internal.s.pVMRC = pVM->pVMRC;
651
652 PPDMPCIDEV pPciDev = pDevIns->Internal.s.pHeadPciDevR3;
653 if (pPciDev)
654 {
655 pDevIns->Internal.s.pHeadPciDevRC = MMHyperR3ToRC(pVM, pPciDev);
656 do
657 {
658 pPciDev->Int.s.pDevInsRC = MMHyperR3ToRC(pVM, pPciDev->Int.s.pDevInsR3);
659 pPciDev->Int.s.pPdmBusRC = MMHyperR3ToRC(pVM, pPciDev->Int.s.pPdmBusR3);
660 if (pPciDev->Int.s.pNextR3)
661 pPciDev->Int.s.pNextRC = MMHyperR3ToRC(pVM, pPciDev->Int.s.pNextR3);
662 pPciDev = pPciDev->Int.s.pNextR3;
663 } while (pPciDev);
664 }
665
666 if (pDevIns->pReg->pfnRelocate)
667 {
668 LogFlow(("PDMR3Relocate: Relocating device '%s'/%d\n",
669 pDevIns->pReg->szName, pDevIns->iInstance));
670 pDevIns->pReg->pfnRelocate(pDevIns, offDelta);
671 }
672 }
673
674 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
675 {
676 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
677 {
678 if (pDrvIns->pReg->fFlags & PDM_DRVREG_FLAGS_RC)
679 {
680 pDrvIns->pHlpRC = pDrvHlpRC;
681 pDrvIns->pvInstanceDataRC = MMHyperR3ToRC(pVM, pDrvIns->pvInstanceDataR3);
682 pDrvIns->Internal.s.pVMRC = pVM->pVMRC;
683 if (pDrvIns->pReg->pfnRelocate)
684 {
685 LogFlow(("PDMR3Relocate: Relocating driver '%s'/%u attached to '%s'/%d/%u\n",
686 pDrvIns->pReg->szName, pDrvIns->iInstance,
687 pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun));
688 pDrvIns->pReg->pfnRelocate(pDrvIns, offDelta);
689 }
690 }
691 }
692 }
693
694 }
695 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
696
697#endif /* VBOX_WITH_RAW_MODE_KEEP */
698}
699
700
701/**
702 * Worker for pdmR3Term that terminates a LUN chain.
703 *
704 * @note Caller owns CoreListCritSectRw in exclusive mode.
705 * Will temporarily leave and retake the lock when calling destructors.
706 *
707 * @param pVM The cross context VM structure.
708 * @param pLun The head of the chain.
709 * @param pszDevice The name of the device (for logging).
710 * @param iInstance The device instance number (for logging).
711 */
712static void pdmR3TermLuns(PVM pVM, PPDMLUN pLun, const char *pszDevice, unsigned iInstance)
713{
714 RT_NOREF2(pszDevice, iInstance);
715
716 for (; pLun; pLun = pLun->pNext)
717 {
718 /*
719 * Destroy them one at a time from the bottom up.
720 * (The serial device/drivers depends on this - bad.)
721 */
722 PPDMDRVINS pDrvIns = pLun->pBottom;
723 pLun->pBottom = pLun->pTop = NULL;
724 while (pDrvIns)
725 {
726 PPDMDRVINS pDrvNext = pDrvIns->Internal.s.pUp;
727
728 if (pDrvIns->pReg->pfnDestruct)
729 {
730 LogFlow(("pdmR3DevTerm: Destroying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
731 pDrvIns->pReg->szName, pDrvIns->iInstance, pLun->iLun, pszDevice, iInstance));
732 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
733 pDrvIns->pReg->pfnDestruct(pDrvIns);
734 RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
735 }
736 pDrvIns->Internal.s.pDrv->cInstances--;
737
738 /* Order of resource freeing like in pdmR3DrvDestroyChain, but
739 * not all need to be done as they are done globally later. */
740 //PDMR3QueueDestroyDriver(pVM, pDrvIns);
741 TMR3TimerDestroyDriver(pVM, pDrvIns);
742 SSMR3DeregisterDriver(pVM, pDrvIns, NULL, 0);
743 //pdmR3ThreadDestroyDriver(pVM, pDrvIns);
744 //DBGFR3InfoDeregisterDriver(pVM, pDrvIns, NULL);
745 //pdmR3CritSectBothDeleteDriver(pVM, pDrvIns);
746 //PDMR3BlkCacheReleaseDriver(pVM, pDrvIns);
747#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
748 //pdmR3AsyncCompletionTemplateDestroyDriver(pVM, pDrvIns);
749#endif
750
751 /* Clear the driver struture to catch sloppy code. */
752 ASMMemFill32(pDrvIns, RT_UOFFSETOF_DYN(PDMDRVINS, achInstanceData[pDrvIns->pReg->cbInstance]), 0xdeadd0d0);
753
754 pDrvIns = pDrvNext;
755 }
756 }
757}
758
759
760/**
761 * Terminates the PDM.
762 *
763 * Termination means cleaning up and freeing all resources,
764 * the VM it self is at this point powered off or suspended.
765 *
766 * @returns VBox status code.
767 * @param pVM The cross context VM structure.
768 */
769VMMR3_INT_DECL(int) PDMR3Term(PVM pVM)
770{
771 LogFlow(("PDMR3Term:\n"));
772 AssertMsg(PDMCritSectIsInitialized(&pVM->pdm.s.CritSect), ("bad init order!\n"));
773
774 /*
775 * Iterate the device instances and attach drivers, doing
776 * relevant destruction processing.
777 *
778 * N.B. There is no need to mess around freeing memory allocated
779 * from any MM heap since MM will do that in its Term function.
780 */
781 RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
782
783 /* usb ones first. */
784 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
785 {
786 pdmR3TermLuns(pVM, pUsbIns->Internal.s.pLuns, pUsbIns->pReg->szName, pUsbIns->iInstance);
787
788 /*
789 * Detach it from the HUB (if it's actually attached to one) so the HUB has
790 * a chance to stop accessing any data.
791 */
792 PPDMUSBHUB pHub = pUsbIns->Internal.s.pHub;
793 if (pHub)
794 {
795 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
796 int rc = pHub->Reg.pfnDetachDevice(pHub->pDrvIns, pUsbIns, pUsbIns->Internal.s.iPort);
797 RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
798 if (RT_SUCCESS(rc))
799 {
800 pHub->cAvailablePorts++;
801 Assert(pHub->cAvailablePorts > 0 && pHub->cAvailablePorts <= pHub->cPorts);
802 pUsbIns->Internal.s.pHub = NULL;
803 }
804 else
805 LogRel(("PDM: Failed to detach USB device '%s' instance %d from %p: %Rrc\n",
806 pUsbIns->pReg->szName, pUsbIns->iInstance, pHub, rc));
807 }
808
809 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
810
811 if (pUsbIns->pReg->pfnDestruct)
812 {
813 LogFlow(("pdmR3DevTerm: Destroying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
814 pUsbIns->pReg->pfnDestruct(pUsbIns);
815 }
816
817 //TMR3TimerDestroyUsb(pVM, pUsbIns);
818 //SSMR3DeregisterUsb(pVM, pUsbIns, NULL, 0);
819 pdmR3ThreadDestroyUsb(pVM, pUsbIns);
820
821 if (pUsbIns->pszName)
822 {
823 RTStrFree(pUsbIns->pszName); /* See the RTStrDup() call in PDMUsb.cpp:pdmR3UsbCreateDevice. */
824 pUsbIns->pszName = NULL;
825 }
826
827 RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
828 }
829
830 /* then the 'normal' ones. */
831 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
832 {
833 pdmR3TermLuns(pVM, pDevIns->Internal.s.pLunsR3, pDevIns->pReg->szName, pDevIns->iInstance);
834
835 if (pDevIns->pReg->pfnDestruct)
836 {
837 LogFlow(("pdmR3DevTerm: Destroying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
838 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
839 pDevIns->pReg->pfnDestruct(pDevIns);
840 RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
841 }
842
843 if (pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_R0_CONTRUCT)
844 {
845 LogFlow(("pdmR3DevTerm: Destroying (ring-0) - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
846 PDMDEVICEGENCALLREQ Req;
847 RT_ZERO(Req.Params);
848 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
849 Req.Hdr.cbReq = sizeof(Req);
850 Req.enmCall = PDMDEVICEGENCALL_DESTRUCT;
851 Req.idxR0Device = pDevIns->Internal.s.idxR0Device;
852 Req.pDevInsR3 = pDevIns;
853 int rc2 = VMMR3CallR0(pVM, VMMR0_DO_PDM_DEVICE_GEN_CALL, 0, &Req.Hdr);
854 AssertRC(rc2);
855 }
856
857 if (pDevIns->Internal.s.paDbgfTraceTrack)
858 {
859 RTMemFree(pDevIns->Internal.s.paDbgfTraceTrack);
860 pDevIns->Internal.s.paDbgfTraceTrack = NULL;
861 }
862
863#ifdef VBOX_WITH_DBGF_TRACING
864 if (pDevIns->Internal.s.hDbgfTraceEvtSrc != NIL_DBGFTRACEREVTSRC)
865 {
866 DBGFR3TracerDeregisterEvtSrc(pVM, pDevIns->Internal.s.hDbgfTraceEvtSrc);
867 pDevIns->Internal.s.hDbgfTraceEvtSrc = NIL_DBGFTRACEREVTSRC;
868 }
869#endif
870 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
871
872 TMR3TimerDestroyDevice(pVM, pDevIns);
873 SSMR3DeregisterDevice(pVM, pDevIns, NULL, 0);
874 pdmR3CritSectBothDeleteDevice(pVM, pDevIns);
875 pdmR3ThreadDestroyDevice(pVM, pDevIns);
876 PDMR3QueueDestroyDevice(pVM, pDevIns);
877#if 0
878 PGMR3PhysMmio2Deregister(pVM, pDevIns, NIL_PGMMMIO2HANDLE);
879#endif
880#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
881 pdmR3AsyncCompletionTemplateDestroyDevice(pVM, pDevIns);
882#endif
883 DBGFR3InfoDeregisterDevice(pVM, pDevIns, NULL);
884
885 RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
886 }
887
888 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
889
890 /*
891 * Destroy all threads.
892 */
893 pdmR3ThreadDestroyAll(pVM);
894
895 /*
896 * Destroy the block cache.
897 */
898 pdmR3BlkCacheTerm(pVM);
899
900#ifdef VBOX_WITH_NETSHAPER
901 /*
902 * Destroy network bandwidth groups.
903 */
904 pdmR3NetShaperTerm(pVM);
905#endif
906#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
907 /*
908 * Free async completion managers.
909 */
910 pdmR3AsyncCompletionTerm(pVM);
911#endif
912
913 /*
914 * Free modules.
915 */
916 pdmR3LdrTermU(pVM->pUVM, false /*fFinal*/);
917
918 /*
919 * Stop task threads.
920 */
921 pdmR3TaskTerm(pVM);
922
923 /*
924 * Cleanup any leftover queues.
925 */
926 pdmR3QueueTerm(pVM);
927
928 /*
929 * Destroy the PDM lock.
930 */
931 PDMR3CritSectDelete(pVM, &pVM->pdm.s.CritSect);
932 /* The MiscCritSect is deleted by PDMR3CritSectBothTerm later. */
933
934 if (RTCritSectRwIsInitialized(&pVM->pdm.s.CoreListCritSectRw))
935 RTCritSectRwDelete(&pVM->pdm.s.CoreListCritSectRw);
936
937 LogFlow(("PDMR3Term: returns %Rrc\n", VINF_SUCCESS));
938 return VINF_SUCCESS;
939}
940
941
942/**
943 * Terminates the PDM part of the UVM.
944 *
945 * This will unload any modules left behind.
946 *
947 * @param pUVM Pointer to the user mode VM structure.
948 */
949VMMR3_INT_DECL(void) PDMR3TermUVM(PUVM pUVM)
950{
951 /*
952 * In the normal cause of events we will now call pdmR3LdrTermU for
953 * the second time. In the case of init failure however, this might
954 * the first time, which is why we do it.
955 */
956 pdmR3LdrTermU(pUVM, true /*fFinal*/);
957
958 Assert(pUVM->pdm.s.pCritSects == NULL);
959 Assert(pUVM->pdm.s.pRwCritSects == NULL);
960 RTCritSectDelete(&pUVM->pdm.s.ListCritSect);
961}
962
963
964/**
965 * For APIC assertions.
966 *
967 * @returns true if we've loaded state.
968 * @param pVM The cross context VM structure.
969 */
970VMMR3_INT_DECL(bool) PDMR3HasLoadedState(PVM pVM)
971{
972 return pVM->pdm.s.fStateLoaded;
973}
974
975
976/**
977 * Bits that are saved in pass 0 and in the final pass.
978 *
979 * @param pVM The cross context VM structure.
980 * @param pSSM The saved state handle.
981 */
982static void pdmR3SaveBoth(PVM pVM, PSSMHANDLE pSSM)
983{
984 /*
985 * Save the list of device instances so we can check that they're all still
986 * there when we load the state and that nothing new has been added.
987 */
988 uint32_t i = 0;
989 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3, i++)
990 {
991 SSMR3PutU32(pSSM, i);
992 SSMR3PutStrZ(pSSM, pDevIns->pReg->szName);
993 SSMR3PutU32(pSSM, pDevIns->iInstance);
994 }
995 SSMR3PutU32(pSSM, UINT32_MAX); /* terminator */
996}
997
998
999/**
1000 * Live save.
1001 *
1002 * @returns VBox status code.
1003 * @param pVM The cross context VM structure.
1004 * @param pSSM The saved state handle.
1005 * @param uPass The pass.
1006 */
1007static DECLCALLBACK(int) pdmR3LiveExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uPass)
1008{
1009 LogFlow(("pdmR3LiveExec:\n"));
1010 AssertReturn(uPass == 0, VERR_SSM_UNEXPECTED_PASS);
1011 pdmR3SaveBoth(pVM, pSSM);
1012 return VINF_SSM_DONT_CALL_AGAIN;
1013}
1014
1015
1016/**
1017 * Execute state save operation.
1018 *
1019 * @returns VBox status code.
1020 * @param pVM The cross context VM structure.
1021 * @param pSSM The saved state handle.
1022 */
1023static DECLCALLBACK(int) pdmR3SaveExec(PVM pVM, PSSMHANDLE pSSM)
1024{
1025 LogFlow(("pdmR3SaveExec:\n"));
1026
1027 /*
1028 * Save interrupt and DMA states.
1029 */
1030 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
1031 {
1032 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
1033#if defined(VBOX_VMM_TARGET_ARMV8)
1034 SSMR3PutU32(pSSM, VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_IRQ));
1035 SSMR3PutU32(pSSM, VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_FIQ));
1036#else
1037 SSMR3PutU32(pSSM, VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC));
1038 SSMR3PutU32(pSSM, VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_PIC));
1039#endif
1040 SSMR3PutU32(pSSM, VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_NMI));
1041 SSMR3PutU32(pSSM, VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_SMI));
1042 }
1043 SSMR3PutU32(pSSM, VM_FF_IS_SET(pVM, VM_FF_PDM_DMA));
1044
1045 pdmR3SaveBoth(pVM, pSSM);
1046 return VINF_SUCCESS;
1047}
1048
1049
1050/**
1051 * Prepare state load operation.
1052 *
1053 * This will dispatch pending operations and clear the FFs governed by PDM and its devices.
1054 *
1055 * @returns VBox status code.
1056 * @param pVM The cross context VM structure.
1057 * @param pSSM The SSM handle.
1058 */
1059static DECLCALLBACK(int) pdmR3LoadPrep(PVM pVM, PSSMHANDLE pSSM)
1060{
1061 LogFlow(("pdmR3LoadPrep: %s%s\n",
1062 VM_FF_IS_SET(pVM, VM_FF_PDM_QUEUES) ? " VM_FF_PDM_QUEUES" : "",
1063 VM_FF_IS_SET(pVM, VM_FF_PDM_DMA) ? " VM_FF_PDM_DMA" : ""));
1064#ifdef LOG_ENABLED
1065 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
1066 {
1067 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
1068#if defined(VBOX_VMM_TARGET_ARMV8)
1069 LogFlow(("pdmR3LoadPrep: VCPU %u %s%s\n", idCpu,
1070 VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_IRQ) ? " VMCPU_FF_INTERRUPT_IRQ" : "",
1071 VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_FIQ) ? " VMCPU_FF_INTERRUPT_FIQ" : ""));
1072#else
1073 LogFlow(("pdmR3LoadPrep: VCPU %u %s%s\n", idCpu,
1074 VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC) ? " VMCPU_FF_INTERRUPT_APIC" : "",
1075 VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_PIC) ? " VMCPU_FF_INTERRUPT_PIC" : ""));
1076#endif
1077 }
1078#endif
1079 NOREF(pSSM);
1080
1081 /*
1082 * In case there is work pending that will raise an interrupt,
1083 * start a DMA transfer, or release a lock. (unlikely)
1084 */
1085 if (VM_FF_IS_SET(pVM, VM_FF_PDM_QUEUES))
1086 PDMR3QueueFlushAll(pVM);
1087
1088 /* Clear the FFs. */
1089 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
1090 {
1091 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
1092#if defined(VBOX_VMM_TARGET_ARMV8)
1093 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_IRQ);
1094 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_FIQ);
1095#else
1096 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_APIC);
1097 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_PIC);
1098#endif
1099 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_NMI);
1100 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_SMI);
1101 }
1102 VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
1103
1104 return VINF_SUCCESS;
1105}
1106
1107
1108/**
1109 * Execute state load operation.
1110 *
1111 * @returns VBox status code.
1112 * @param pVM The cross context VM structure.
1113 * @param pSSM SSM operation handle.
1114 * @param uVersion Data layout version.
1115 * @param uPass The data pass.
1116 */
1117static DECLCALLBACK(int) pdmR3LoadExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
1118{
1119 int rc;
1120
1121 LogFlow(("pdmR3LoadExec: uPass=%#x\n", uPass));
1122
1123 /*
1124 * Validate version.
1125 */
1126 if ( uVersion != PDM_SAVED_STATE_VERSION
1127 && uVersion != PDM_SAVED_STATE_VERSION_PRE_NMI_FF
1128 && uVersion != PDM_SAVED_STATE_VERSION_PRE_PDM_AUDIO)
1129 {
1130 AssertMsgFailed(("Invalid version uVersion=%d!\n", uVersion));
1131 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1132 }
1133
1134 if (uPass == SSM_PASS_FINAL)
1135 {
1136 /*
1137 * Load the interrupt and DMA states.
1138 *
1139 * The APIC, PIC and DMA devices does not restore these, we do. In the
1140 * APIC and PIC cases, it is possible that some devices is incorrectly
1141 * setting IRQs during restore. We'll warn when this happens. (There
1142 * are debug assertions in PDMDevMiscHlp.cpp and APICAll.cpp for
1143 * catching the buggy device.)
1144 */
1145 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
1146 {
1147 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
1148
1149 /* APIC/IRQ interrupt */
1150 uint32_t fInterruptPending = 0;
1151 rc = SSMR3GetU32(pSSM, &fInterruptPending);
1152 if (RT_FAILURE(rc))
1153 return rc;
1154 if (fInterruptPending & ~1)
1155 {
1156 AssertMsgFailed(("fInterruptPending=%#x (APIC)\n", fInterruptPending));
1157 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1158 }
1159#if defined(VBOX_VMM_TARGET_ARMV8)
1160 AssertLogRelMsg(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_IRQ),
1161 ("VCPU%03u: VMCPU_FF_INTERRUPT_IRQ set! Devices shouldn't set interrupts during state restore...\n", idCpu));
1162 if (fInterruptPending)
1163 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_IRQ);
1164#else
1165 AssertLogRelMsg(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC),
1166 ("VCPU%03u: VMCPU_FF_INTERRUPT_APIC set! Devices shouldn't set interrupts during state restore...\n", idCpu));
1167 if (fInterruptPending)
1168 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC);
1169#endif
1170
1171 /* PIC/FIQ interrupt */
1172 fInterruptPending = 0;
1173 rc = SSMR3GetU32(pSSM, &fInterruptPending);
1174 if (RT_FAILURE(rc))
1175 return rc;
1176 if (fInterruptPending & ~1)
1177 {
1178 AssertMsgFailed(("fInterruptPending=%#x (PIC)\n", fInterruptPending));
1179 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1180 }
1181#if defined(VBOX_VMM_TARGET_ARMV8)
1182 AssertLogRelMsg(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_FIQ),
1183 ("VCPU%03u: VMCPU_FF_INTERRUPT_FIQ set! Devices shouldn't set interrupts during state restore...\n", idCpu));
1184 if (fInterruptPending)
1185 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_FIQ);
1186#else
1187 AssertLogRelMsg(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_PIC),
1188 ("VCPU%03u: VMCPU_FF_INTERRUPT_PIC set! Devices shouldn't set interrupts during state restore...\n", idCpu));
1189 if (fInterruptPending)
1190 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_PIC);
1191#endif
1192
1193 if (uVersion > PDM_SAVED_STATE_VERSION_PRE_NMI_FF)
1194 {
1195 /* NMI interrupt */
1196 fInterruptPending = 0;
1197 rc = SSMR3GetU32(pSSM, &fInterruptPending);
1198 if (RT_FAILURE(rc))
1199 return rc;
1200 if (fInterruptPending & ~1)
1201 {
1202 AssertMsgFailed(("fInterruptPending=%#x (NMI)\n", fInterruptPending));
1203 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1204 }
1205 AssertLogRelMsg(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_NMI), ("VCPU%3u: VMCPU_FF_INTERRUPT_NMI set!\n", idCpu));
1206 if (fInterruptPending)
1207 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_NMI);
1208
1209 /* SMI interrupt */
1210 fInterruptPending = 0;
1211 rc = SSMR3GetU32(pSSM, &fInterruptPending);
1212 if (RT_FAILURE(rc))
1213 return rc;
1214 if (fInterruptPending & ~1)
1215 {
1216 AssertMsgFailed(("fInterruptPending=%#x (SMI)\n", fInterruptPending));
1217 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1218 }
1219 AssertLogRelMsg(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_SMI), ("VCPU%3u: VMCPU_FF_INTERRUPT_SMI set!\n", idCpu));
1220 if (fInterruptPending)
1221 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_SMI);
1222 }
1223 }
1224
1225 /* DMA pending */
1226 uint32_t fDMAPending = 0;
1227 rc = SSMR3GetU32(pSSM, &fDMAPending);
1228 if (RT_FAILURE(rc))
1229 return rc;
1230 if (fDMAPending & ~1)
1231 {
1232 AssertMsgFailed(("fDMAPending=%#x\n", fDMAPending));
1233 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1234 }
1235 if (fDMAPending)
1236 VM_FF_SET(pVM, VM_FF_PDM_DMA);
1237 Log(("pdmR3LoadExec: VM_FF_PDM_DMA=%RTbool\n", VM_FF_IS_SET(pVM, VM_FF_PDM_DMA)));
1238 }
1239
1240 /*
1241 * Load the list of devices and verify that they are all there.
1242 */
1243 RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
1244 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1245 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_FOUND;
1246 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
1247
1248 for (uint32_t i = 0; ; i++)
1249 {
1250 /* Get the sequence number / terminator. */
1251 uint32_t u32Sep;
1252 rc = SSMR3GetU32(pSSM, &u32Sep);
1253 if (RT_FAILURE(rc))
1254 return rc;
1255 if (u32Sep == UINT32_MAX)
1256 break;
1257 if (u32Sep != i)
1258 AssertMsgFailedReturn(("Out of sequence. u32Sep=%#x i=%#x\n", u32Sep, i), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
1259
1260 /* Get the name and instance number. */
1261 char szName[RT_SIZEOFMEMB(PDMDEVREG, szName)];
1262 rc = SSMR3GetStrZ(pSSM, szName, sizeof(szName));
1263 if (RT_FAILURE(rc))
1264 return rc;
1265 uint32_t iInstance;
1266 rc = SSMR3GetU32(pSSM, &iInstance);
1267 if (RT_FAILURE(rc))
1268 return rc;
1269
1270 /* Try locate it. */
1271 RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
1272 PPDMDEVINS pDevIns;
1273 for (pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1274 if ( !RTStrCmp(szName, pDevIns->pReg->szName)
1275 && pDevIns->iInstance == iInstance)
1276 {
1277 AssertLogRelMsgReturnStmt(!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_FOUND),
1278 ("%s/#%u\n", pDevIns->pReg->szName, pDevIns->iInstance),
1279 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw),
1280 VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
1281 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_FOUND;
1282 break;
1283 }
1284 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
1285
1286 if (!pDevIns)
1287 {
1288 bool fSkip = false;
1289
1290 /* Skip the non-existing (deprecated) "AudioSniffer" device stored in the saved state. */
1291 if ( uVersion <= PDM_SAVED_STATE_VERSION_PRE_PDM_AUDIO
1292 && !RTStrCmp(szName, "AudioSniffer"))
1293 fSkip = true;
1294
1295 if (!fSkip)
1296 {
1297 LogRel(("Device '%s'/%d not found in current config\n", szName, iInstance));
1298 if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
1299 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Device '%s'/%d not found in current config"), szName, iInstance);
1300 }
1301 }
1302 }
1303
1304 /*
1305 * Check that no additional devices were configured.
1306 */
1307 RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
1308 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1309 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_FOUND))
1310 {
1311 LogRel(("Device '%s'/%d not found in the saved state\n", pDevIns->pReg->szName, pDevIns->iInstance));
1312 /** @todo The TPM PPI device was added due to @bugref{10701} and it is not an issue if it isn't there
1313 * in the saved state because there is nothing to load. It might make sense to add a new
1314 * flag to PDMDEVREG::fFlags to indicate that having a new device added for a saved state is okay.
1315 * (For now I just want to get saved states unwedged).
1316 */
1317 if ( SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT
1318 && RTStrCmp(pDevIns->pReg->szName, "tpm-ppi") /* !!HACK ALERT!! - ugly, see todo above */)
1319 {
1320 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
1321 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Device '%s'/%d not found in the saved state"),
1322 pDevIns->pReg->szName, pDevIns->iInstance);
1323 }
1324 }
1325 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
1326
1327 /*
1328 * Indicate that we've been called (for assertions).
1329 */
1330 pVM->pdm.s.fStateLoaded = true;
1331
1332 return VINF_SUCCESS;
1333}
1334
1335
1336/**
1337 * Worker for PDMR3PowerOn that deals with one driver.
1338 *
1339 * @param pDrvIns The driver instance.
1340 * @param pszDevName The parent device name.
1341 * @param iDevInstance The parent device instance number.
1342 * @param iLun The parent LUN number.
1343 */
1344DECLINLINE(int) pdmR3PowerOnDrv(PPDMDRVINS pDrvIns, const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1345{
1346 Assert(pDrvIns->Internal.s.fVMSuspended);
1347 if (pDrvIns->pReg->pfnPowerOn)
1348 {
1349 LogFlow(("PDMR3PowerOn: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1350 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1351 int rc = VINF_SUCCESS; pDrvIns->pReg->pfnPowerOn(pDrvIns);
1352 if (RT_FAILURE(rc))
1353 {
1354 LogRel(("PDMR3PowerOn: Driver '%s'/%d on LUN#%d of device '%s'/%d -> %Rrc\n",
1355 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, rc));
1356 return rc;
1357 }
1358 }
1359 pDrvIns->Internal.s.fVMSuspended = false;
1360 return VINF_SUCCESS;
1361}
1362
1363
1364/**
1365 * Worker for PDMR3PowerOn that deals with one USB device instance.
1366 *
1367 * @returns VBox status code.
1368 * @param pUsbIns The USB device instance.
1369 */
1370DECLINLINE(int) pdmR3PowerOnUsb(PPDMUSBINS pUsbIns)
1371{
1372 Assert(pUsbIns->Internal.s.fVMSuspended);
1373 if (pUsbIns->pReg->pfnVMPowerOn)
1374 {
1375 LogFlow(("PDMR3PowerOn: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1376 int rc = VINF_SUCCESS; pUsbIns->pReg->pfnVMPowerOn(pUsbIns);
1377 if (RT_FAILURE(rc))
1378 {
1379 LogRel(("PDMR3PowerOn: Device '%s'/%d -> %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1380 return rc;
1381 }
1382 }
1383 pUsbIns->Internal.s.fVMSuspended = false;
1384 return VINF_SUCCESS;
1385}
1386
1387
1388/**
1389 * Worker for PDMR3PowerOn that deals with one device instance.
1390 *
1391 * @returns VBox status code.
1392 * @param pVM The cross context VM structure.
1393 * @param pDevIns The device instance.
1394 */
1395DECLINLINE(int) pdmR3PowerOnDev(PVM pVM, PPDMDEVINS pDevIns)
1396{
1397 Assert(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED);
1398 if (pDevIns->pReg->pfnPowerOn)
1399 {
1400 LogFlow(("PDMR3PowerOn: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1401 PDMCritSectEnter(pVM, pDevIns->pCritSectRoR3, VERR_IGNORED);
1402 int rc = VINF_SUCCESS; pDevIns->pReg->pfnPowerOn(pDevIns);
1403 PDMCritSectLeave(pVM, pDevIns->pCritSectRoR3);
1404 if (RT_FAILURE(rc))
1405 {
1406 LogRel(("PDMR3PowerOn: Device '%s'/%d -> %Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
1407 return rc;
1408 }
1409 }
1410 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
1411 return VINF_SUCCESS;
1412}
1413
1414
1415/**
1416 * This function will notify all the devices and their
1417 * attached drivers about the VM now being powered on.
1418 *
1419 * @param pVM The cross context VM structure.
1420 */
1421VMMR3DECL(void) PDMR3PowerOn(PVM pVM)
1422{
1423 LogFlow(("PDMR3PowerOn:\n"));
1424
1425 /*
1426 * Iterate thru the device instances and USB device instances,
1427 * processing the drivers associated with those.
1428 */
1429 RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
1430 /** @todo locking */
1431 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
1432 int rc = VINF_SUCCESS;
1433 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns && RT_SUCCESS(rc); pDevIns = pDevIns->Internal.s.pNextR3)
1434 {
1435 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1436 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1437 rc = pdmR3PowerOnDrv(pDrvIns, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun);
1438 if (RT_SUCCESS(rc))
1439 rc = pdmR3PowerOnDev(pVM, pDevIns);
1440 }
1441
1442#ifdef VBOX_WITH_USB
1443 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns && RT_SUCCESS(rc); pUsbIns = pUsbIns->Internal.s.pNext)
1444 {
1445 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1446 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1447 rc = pdmR3PowerOnDrv(pDrvIns, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun);
1448 if (RT_SUCCESS(rc))
1449 rc = pdmR3PowerOnUsb(pUsbIns);
1450 }
1451#endif
1452
1453#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
1454 pdmR3AsyncCompletionResume(pVM);
1455#endif
1456
1457 /*
1458 * Resume all threads.
1459 */
1460 if (RT_SUCCESS(rc))
1461 pdmR3ThreadResumeAll(pVM);
1462
1463 /*
1464 * On failure, clean up via PDMR3Suspend.
1465 */
1466 if (RT_FAILURE(rc))
1467 PDMR3Suspend(pVM);
1468
1469 LogFlow(("PDMR3PowerOn: returns %Rrc\n", rc));
1470 return /*rc*/;
1471}
1472
1473
1474/**
1475 * Initializes the asynchronous notifi stats structure.
1476 *
1477 * @param pThis The asynchronous notifification stats.
1478 * @param pszOp The name of the operation.
1479 */
1480static void pdmR3NotifyAsyncInit(PPDMNOTIFYASYNCSTATS pThis, const char *pszOp)
1481{
1482 pThis->uStartNsTs = RTTimeNanoTS();
1483 pThis->cNsElapsedNextLog = 0;
1484 pThis->cLoops = 0;
1485 pThis->cAsync = 0;
1486 pThis->pszOp = pszOp;
1487 pThis->offList = 0;
1488 pThis->szList[0] = '\0';
1489}
1490
1491
1492/**
1493 * Begin a new loop, prepares to gather new stats.
1494 *
1495 * @param pThis The asynchronous notifification stats.
1496 */
1497static void pdmR3NotifyAsyncBeginLoop(PPDMNOTIFYASYNCSTATS pThis)
1498{
1499 pThis->cLoops++;
1500 pThis->cAsync = 0;
1501 pThis->offList = 0;
1502 pThis->szList[0] = '\0';
1503}
1504
1505
1506/**
1507 * Records a device or USB device with a pending asynchronous notification.
1508 *
1509 * @param pThis The asynchronous notifification stats.
1510 * @param pszName The name of the thing.
1511 * @param iInstance The instance number.
1512 */
1513static void pdmR3NotifyAsyncAdd(PPDMNOTIFYASYNCSTATS pThis, const char *pszName, uint32_t iInstance)
1514{
1515 pThis->cAsync++;
1516 if (pThis->offList < sizeof(pThis->szList) - 4)
1517 pThis->offList += RTStrPrintf(&pThis->szList[pThis->offList], sizeof(pThis->szList) - pThis->offList,
1518 pThis->offList == 0 ? "%s/%u" : ", %s/%u",
1519 pszName, iInstance);
1520}
1521
1522
1523/**
1524 * Records the asynchronous completition of a reset, suspend or power off.
1525 *
1526 * @param pThis The asynchronous notifification stats.
1527 * @param pszDrvName The driver name.
1528 * @param iDrvInstance The driver instance number.
1529 * @param pszDevName The device or USB device name.
1530 * @param iDevInstance The device or USB device instance number.
1531 * @param iLun The LUN.
1532 */
1533static void pdmR3NotifyAsyncAddDrv(PPDMNOTIFYASYNCSTATS pThis, const char *pszDrvName, uint32_t iDrvInstance,
1534 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1535{
1536 pThis->cAsync++;
1537 if (pThis->offList < sizeof(pThis->szList) - 8)
1538 pThis->offList += RTStrPrintf(&pThis->szList[pThis->offList], sizeof(pThis->szList) - pThis->offList,
1539 pThis->offList == 0 ? "%s/%u/%u/%s/%u" : ", %s/%u/%u/%s/%u",
1540 pszDevName, iDevInstance, iLun, pszDrvName, iDrvInstance);
1541}
1542
1543
1544/**
1545 * Log the stats.
1546 *
1547 * @param pThis The asynchronous notifification stats.
1548 */
1549static void pdmR3NotifyAsyncLog(PPDMNOTIFYASYNCSTATS pThis)
1550{
1551 /*
1552 * Return if we shouldn't log at this point.
1553 * We log with an internval increasing from 0 sec to 60 sec.
1554 */
1555 if (!pThis->cAsync)
1556 return;
1557
1558 uint64_t cNsElapsed = RTTimeNanoTS() - pThis->uStartNsTs;
1559 if (cNsElapsed < pThis->cNsElapsedNextLog)
1560 return;
1561
1562 if (pThis->cNsElapsedNextLog == 0)
1563 pThis->cNsElapsedNextLog = RT_NS_1SEC;
1564 else if (pThis->cNsElapsedNextLog >= RT_NS_1MIN / 2)
1565 pThis->cNsElapsedNextLog = RT_NS_1MIN;
1566 else
1567 pThis->cNsElapsedNextLog *= 2;
1568
1569 /*
1570 * Do the logging.
1571 */
1572 LogRel(("%s: after %5llu ms, %u loops: %u async tasks - %s\n",
1573 pThis->pszOp, cNsElapsed / RT_NS_1MS, pThis->cLoops, pThis->cAsync, pThis->szList));
1574}
1575
1576
1577/**
1578 * Wait for events and process pending requests.
1579 *
1580 * @param pThis The asynchronous notifification stats.
1581 * @param pVM The cross context VM structure.
1582 */
1583static void pdmR3NotifyAsyncWaitAndProcessRequests(PPDMNOTIFYASYNCSTATS pThis, PVM pVM)
1584{
1585 VM_ASSERT_EMT0(pVM);
1586 int rc = VMR3AsyncPdmNotificationWaitU(&pVM->pUVM->aCpus[0]);
1587 AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc - %s - %s\n", rc, pThis->pszOp, pThis->szList));
1588
1589 rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY, true /*fPriorityOnly*/);
1590 AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc - %s - %s\n", rc, pThis->pszOp, pThis->szList));
1591 rc = VMR3ReqProcessU(pVM->pUVM, 0/*idDstCpu*/, true /*fPriorityOnly*/);
1592 AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc - %s - %s\n", rc, pThis->pszOp, pThis->szList));
1593}
1594
1595
1596/**
1597 * Worker for PDMR3Reset that deals with one driver.
1598 *
1599 * @param pDrvIns The driver instance.
1600 * @param pAsync The structure for recording asynchronous
1601 * notification tasks.
1602 * @param pszDevName The parent device name.
1603 * @param iDevInstance The parent device instance number.
1604 * @param iLun The parent LUN number.
1605 */
1606DECLINLINE(bool) pdmR3ResetDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
1607 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1608{
1609 if (!pDrvIns->Internal.s.fVMReset)
1610 {
1611 pDrvIns->Internal.s.fVMReset = true;
1612 if (pDrvIns->pReg->pfnReset)
1613 {
1614 if (!pDrvIns->Internal.s.pfnAsyncNotify)
1615 {
1616 LogFlow(("PDMR3Reset: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1617 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1618 pDrvIns->pReg->pfnReset(pDrvIns);
1619 if (pDrvIns->Internal.s.pfnAsyncNotify)
1620 LogFlow(("PDMR3Reset: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1621 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1622 }
1623 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
1624 {
1625 LogFlow(("PDMR3Reset: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1626 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1627 pDrvIns->Internal.s.pfnAsyncNotify = NULL;
1628 }
1629 if (pDrvIns->Internal.s.pfnAsyncNotify)
1630 {
1631 pDrvIns->Internal.s.fVMReset = false;
1632 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance,
1633 pszDevName, iDevInstance, iLun);
1634 return false;
1635 }
1636 }
1637 }
1638 return true;
1639}
1640
1641
1642/**
1643 * Worker for PDMR3Reset that deals with one USB device instance.
1644 *
1645 * @param pUsbIns The USB device instance.
1646 * @param pAsync The structure for recording asynchronous
1647 * notification tasks.
1648 */
1649DECLINLINE(void) pdmR3ResetUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
1650{
1651 if (!pUsbIns->Internal.s.fVMReset)
1652 {
1653 pUsbIns->Internal.s.fVMReset = true;
1654 if (pUsbIns->pReg->pfnVMReset)
1655 {
1656 if (!pUsbIns->Internal.s.pfnAsyncNotify)
1657 {
1658 LogFlow(("PDMR3Reset: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1659 pUsbIns->pReg->pfnVMReset(pUsbIns);
1660 if (pUsbIns->Internal.s.pfnAsyncNotify)
1661 LogFlow(("PDMR3Reset: Async notification started - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1662 }
1663 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
1664 {
1665 LogFlow(("PDMR3Reset: Async notification completed - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1666 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
1667 }
1668 if (pUsbIns->Internal.s.pfnAsyncNotify)
1669 {
1670 pUsbIns->Internal.s.fVMReset = false;
1671 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
1672 }
1673 }
1674 }
1675}
1676
1677
1678/**
1679 * Worker for PDMR3Reset that deals with one device instance.
1680 *
1681 * @param pVM The cross context VM structure.
1682 * @param pDevIns The device instance.
1683 * @param pAsync The structure for recording asynchronous notification tasks.
1684 */
1685DECLINLINE(void) pdmR3ResetDev(PVM pVM, PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
1686{
1687 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_RESET))
1688 {
1689 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_RESET;
1690 if (pDevIns->pReg->pfnReset)
1691 {
1692 uint64_t cNsElapsed = RTTimeNanoTS();
1693 PDMCritSectEnter(pVM, pDevIns->pCritSectRoR3, VERR_IGNORED);
1694
1695 if (!pDevIns->Internal.s.pfnAsyncNotify)
1696 {
1697 LogFlow(("PDMR3Reset: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1698 pDevIns->pReg->pfnReset(pDevIns);
1699 if (pDevIns->Internal.s.pfnAsyncNotify)
1700 LogFlow(("PDMR3Reset: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1701 }
1702 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
1703 {
1704 LogFlow(("PDMR3Reset: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1705 pDevIns->Internal.s.pfnAsyncNotify = NULL;
1706 }
1707 if (pDevIns->Internal.s.pfnAsyncNotify)
1708 {
1709 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_RESET;
1710 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
1711 }
1712
1713 PDMCritSectLeave(pVM, pDevIns->pCritSectRoR3);
1714 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1715 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1716 LogRel(("PDMR3Reset: Device '%s'/%d took %'llu ns to reset\n",
1717 pDevIns->pReg->szName, pDevIns->iInstance, cNsElapsed));
1718 }
1719 }
1720}
1721
1722
1723/**
1724 * Resets a virtual CPU.
1725 *
1726 * Used by PDMR3Reset and CPU hot plugging.
1727 *
1728 * @param pVCpu The cross context virtual CPU structure.
1729 */
1730VMMR3_INT_DECL(void) PDMR3ResetCpu(PVMCPU pVCpu)
1731{
1732#if defined(VBOX_VMM_TARGET_ARMV8)
1733 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_IRQ);
1734 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_FIQ);
1735#else
1736 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_APIC);
1737 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_PIC);
1738#endif
1739 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_NMI);
1740 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_SMI);
1741}
1742
1743
1744/**
1745 * This function will notify all the devices and their attached drivers about
1746 * the VM now being reset.
1747 *
1748 * @param pVM The cross context VM structure.
1749 */
1750VMMR3_INT_DECL(void) PDMR3Reset(PVM pVM)
1751{
1752 LogFlow(("PDMR3Reset:\n"));
1753
1754 /*
1755 * Clear all the reset flags.
1756 */
1757 RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
1758 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1759 {
1760 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_RESET;
1761 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1762 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1763 pDrvIns->Internal.s.fVMReset = false;
1764 }
1765#ifdef VBOX_WITH_USB
1766 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1767 {
1768 pUsbIns->Internal.s.fVMReset = false;
1769 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1770 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1771 pDrvIns->Internal.s.fVMReset = false;
1772 }
1773#endif
1774 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
1775
1776 /*
1777 * The outer loop repeats until there are no more async requests.
1778 */
1779 PDMNOTIFYASYNCSTATS Async;
1780 pdmR3NotifyAsyncInit(&Async, "PDMR3Reset");
1781 for (;;)
1782 {
1783 pdmR3NotifyAsyncBeginLoop(&Async);
1784
1785 /*
1786 * Iterate thru the device instances and USB device instances,
1787 * processing the drivers associated with those.
1788 */
1789 /** @todo locking */
1790 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1791 {
1792 unsigned const cAsyncStart = Async.cAsync;
1793
1794 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_RESET_NOTIFICATION)
1795 pdmR3ResetDev(pVM, pDevIns, &Async);
1796
1797 if (Async.cAsync == cAsyncStart)
1798 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1799 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1800 if (!pdmR3ResetDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
1801 break;
1802
1803 if ( Async.cAsync == cAsyncStart
1804 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_RESET_NOTIFICATION))
1805 pdmR3ResetDev(pVM, pDevIns, &Async);
1806 }
1807
1808#ifdef VBOX_WITH_USB
1809 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1810 {
1811 unsigned const cAsyncStart = Async.cAsync;
1812
1813 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1814 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1815 if (!pdmR3ResetDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
1816 break;
1817
1818 if (Async.cAsync == cAsyncStart)
1819 pdmR3ResetUsb(pUsbIns, &Async);
1820 }
1821#endif
1822 if (!Async.cAsync)
1823 break;
1824 pdmR3NotifyAsyncLog(&Async);
1825 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
1826 }
1827
1828 /*
1829 * Clear all pending interrupts and DMA operations.
1830 */
1831 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
1832 PDMR3ResetCpu(pVM->apCpusR3[idCpu]);
1833 VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
1834
1835 LogFlow(("PDMR3Reset: returns void\n"));
1836}
1837
1838
1839/**
1840 * This function will tell all the devices to setup up their memory structures
1841 * after VM construction and after VM reset.
1842 *
1843 * @param pVM The cross context VM structure.
1844 * @param fAtReset Indicates the context, after reset if @c true or after
1845 * construction if @c false.
1846 */
1847VMMR3_INT_DECL(void) PDMR3MemSetup(PVM pVM, bool fAtReset)
1848{
1849 LogFlow(("PDMR3MemSetup: fAtReset=%RTbool\n", fAtReset));
1850 PDMDEVMEMSETUPCTX const enmCtx = fAtReset ? PDMDEVMEMSETUPCTX_AFTER_RESET : PDMDEVMEMSETUPCTX_AFTER_CONSTRUCTION;
1851
1852 /*
1853 * Iterate thru the device instances and work the callback.
1854 */
1855 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1856 if (pDevIns->pReg->pfnMemSetup)
1857 {
1858 PDMCritSectEnter(pVM, pDevIns->pCritSectRoR3, VERR_IGNORED);
1859 pDevIns->pReg->pfnMemSetup(pDevIns, enmCtx);
1860 PDMCritSectLeave(pVM, pDevIns->pCritSectRoR3);
1861 }
1862
1863 LogFlow(("PDMR3MemSetup: returns void\n"));
1864}
1865
1866
1867/**
1868 * Retrieves and resets the info left behind by PDMDevHlpVMReset.
1869 *
1870 * @returns True if hard reset, false if soft reset.
1871 * @param pVM The cross context VM structure.
1872 * @param fOverride If non-zero, the override flags will be used instead
1873 * of the reset flags kept by PDM. (For triple faults.)
1874 * @param pfResetFlags Where to return the reset flags (PDMVMRESET_F_XXX).
1875 * @thread EMT
1876 */
1877VMMR3_INT_DECL(bool) PDMR3GetResetInfo(PVM pVM, uint32_t fOverride, uint32_t *pfResetFlags)
1878{
1879 VM_ASSERT_EMT(pVM);
1880
1881 /*
1882 * Get the reset flags.
1883 */
1884 uint32_t fResetFlags;
1885 fResetFlags = ASMAtomicXchgU32(&pVM->pdm.s.fResetFlags, 0);
1886 if (fOverride)
1887 fResetFlags = fOverride;
1888 *pfResetFlags = fResetFlags;
1889
1890 /*
1891 * To try avoid trouble, we never ever do soft/warm resets on SMP systems
1892 * with more than CPU #0 active. However, if only one CPU is active we
1893 * will ask the firmware what it wants us to do (because the firmware may
1894 * depend on the VMM doing a lot of what is normally its responsibility,
1895 * like clearing memory).
1896 */
1897 bool fOtherCpusActive = false;
1898 VMCPUID idCpu = pVM->cCpus;
1899 while (idCpu-- > 1)
1900 {
1901 EMSTATE enmState = EMGetState(pVM->apCpusR3[idCpu]);
1902 if ( enmState != EMSTATE_WAIT_SIPI
1903 && enmState != EMSTATE_NONE)
1904 {
1905 fOtherCpusActive = true;
1906 break;
1907 }
1908 }
1909
1910 bool fHardReset = fOtherCpusActive
1911 || (fResetFlags & PDMVMRESET_F_SRC_MASK) < PDMVMRESET_F_LAST_ALWAYS_HARD
1912 || !pVM->pdm.s.pFirmware
1913 || pVM->pdm.s.pFirmware->Reg.pfnIsHardReset(pVM->pdm.s.pFirmware->pDevIns, fResetFlags);
1914
1915 Log(("PDMR3GetResetInfo: returns fHardReset=%RTbool fResetFlags=%#x\n", fHardReset, fResetFlags));
1916 return fHardReset;
1917}
1918
1919
1920/**
1921 * Performs a soft reset of devices.
1922 *
1923 * @param pVM The cross context VM structure.
1924 * @param fResetFlags PDMVMRESET_F_XXX.
1925 */
1926VMMR3_INT_DECL(void) PDMR3SoftReset(PVM pVM, uint32_t fResetFlags)
1927{
1928 LogFlow(("PDMR3SoftReset: fResetFlags=%#x\n", fResetFlags));
1929
1930 /*
1931 * Iterate thru the device instances and work the callback.
1932 */
1933 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1934 if (pDevIns->pReg->pfnSoftReset)
1935 {
1936 PDMCritSectEnter(pVM, pDevIns->pCritSectRoR3, VERR_IGNORED);
1937 pDevIns->pReg->pfnSoftReset(pDevIns, fResetFlags);
1938 PDMCritSectLeave(pVM, pDevIns->pCritSectRoR3);
1939 }
1940
1941 LogFlow(("PDMR3SoftReset: returns void\n"));
1942}
1943
1944
1945/**
1946 * Worker for PDMR3Suspend that deals with one driver.
1947 *
1948 * @param pDrvIns The driver instance.
1949 * @param pAsync The structure for recording asynchronous
1950 * notification tasks.
1951 * @param pszDevName The parent device name.
1952 * @param iDevInstance The parent device instance number.
1953 * @param iLun The parent LUN number.
1954 */
1955DECLINLINE(bool) pdmR3SuspendDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
1956 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1957{
1958 if (!pDrvIns->Internal.s.fVMSuspended)
1959 {
1960 pDrvIns->Internal.s.fVMSuspended = true;
1961 if (pDrvIns->pReg->pfnSuspend)
1962 {
1963 uint64_t cNsElapsed = RTTimeNanoTS();
1964
1965 if (!pDrvIns->Internal.s.pfnAsyncNotify)
1966 {
1967 LogFlow(("PDMR3Suspend: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1968 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1969 pDrvIns->pReg->pfnSuspend(pDrvIns);
1970 if (pDrvIns->Internal.s.pfnAsyncNotify)
1971 LogFlow(("PDMR3Suspend: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1972 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1973 }
1974 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
1975 {
1976 LogFlow(("PDMR3Suspend: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1977 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1978 pDrvIns->Internal.s.pfnAsyncNotify = NULL;
1979 }
1980
1981 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1982 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1983 LogRel(("PDMR3Suspend: Driver '%s'/%d on LUN#%d of device '%s'/%d took %'llu ns to suspend\n",
1984 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, cNsElapsed));
1985
1986 if (pDrvIns->Internal.s.pfnAsyncNotify)
1987 {
1988 pDrvIns->Internal.s.fVMSuspended = false;
1989 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance, pszDevName, iDevInstance, iLun);
1990 return false;
1991 }
1992 }
1993 }
1994 return true;
1995}
1996
1997
1998/**
1999 * Worker for PDMR3Suspend that deals with one USB device instance.
2000 *
2001 * @param pUsbIns The USB device instance.
2002 * @param pAsync The structure for recording asynchronous
2003 * notification tasks.
2004 */
2005DECLINLINE(void) pdmR3SuspendUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
2006{
2007 if (!pUsbIns->Internal.s.fVMSuspended)
2008 {
2009 pUsbIns->Internal.s.fVMSuspended = true;
2010 if (pUsbIns->pReg->pfnVMSuspend)
2011 {
2012 uint64_t cNsElapsed = RTTimeNanoTS();
2013
2014 if (!pUsbIns->Internal.s.pfnAsyncNotify)
2015 {
2016 LogFlow(("PDMR3Suspend: Notifying - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
2017 pUsbIns->pReg->pfnVMSuspend(pUsbIns);
2018 if (pUsbIns->Internal.s.pfnAsyncNotify)
2019 LogFlow(("PDMR3Suspend: Async notification started - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
2020 }
2021 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
2022 {
2023 LogFlow(("PDMR3Suspend: Async notification completed - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
2024 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
2025 }
2026 if (pUsbIns->Internal.s.pfnAsyncNotify)
2027 {
2028 pUsbIns->Internal.s.fVMSuspended = false;
2029 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
2030 }
2031
2032 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2033 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
2034 LogRel(("PDMR3Suspend: USB device '%s'/%d took %'llu ns to suspend\n",
2035 pUsbIns->pReg->szName, pUsbIns->iInstance, cNsElapsed));
2036 }
2037 }
2038}
2039
2040
2041/**
2042 * Worker for PDMR3Suspend that deals with one device instance.
2043 *
2044 * @param pVM The cross context VM structure.
2045 * @param pDevIns The device instance.
2046 * @param pAsync The structure for recording asynchronous notification tasks.
2047 */
2048DECLINLINE(void) pdmR3SuspendDev(PVM pVM, PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
2049{
2050 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED))
2051 {
2052 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_SUSPENDED;
2053 if (pDevIns->pReg->pfnSuspend)
2054 {
2055 uint64_t cNsElapsed = RTTimeNanoTS();
2056 PDMCritSectEnter(pVM, pDevIns->pCritSectRoR3, VERR_IGNORED);
2057
2058 if (!pDevIns->Internal.s.pfnAsyncNotify)
2059 {
2060 LogFlow(("PDMR3Suspend: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
2061 pDevIns->pReg->pfnSuspend(pDevIns);
2062 if (pDevIns->Internal.s.pfnAsyncNotify)
2063 LogFlow(("PDMR3Suspend: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
2064 }
2065 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
2066 {
2067 LogFlow(("PDMR3Suspend: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
2068 pDevIns->Internal.s.pfnAsyncNotify = NULL;
2069 }
2070 if (pDevIns->Internal.s.pfnAsyncNotify)
2071 {
2072 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
2073 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
2074 }
2075
2076 PDMCritSectLeave(pVM, pDevIns->pCritSectRoR3);
2077 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2078 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
2079 LogRel(("PDMR3Suspend: Device '%s'/%d took %'llu ns to suspend\n",
2080 pDevIns->pReg->szName, pDevIns->iInstance, cNsElapsed));
2081 }
2082 }
2083}
2084
2085
2086/**
2087 * This function will notify all the devices and their attached drivers about
2088 * the VM now being suspended.
2089 *
2090 * @param pVM The cross context VM structure.
2091 * @thread EMT(0)
2092 */
2093VMMR3_INT_DECL(void) PDMR3Suspend(PVM pVM)
2094{
2095 LogFlow(("PDMR3Suspend:\n"));
2096 VM_ASSERT_EMT0(pVM);
2097 uint64_t cNsElapsed = RTTimeNanoTS();
2098
2099 /*
2100 * The outer loop repeats until there are no more async requests.
2101 *
2102 * Note! We depend on the suspended indicators to be in the desired state
2103 * and we do not reset them before starting because this allows
2104 * PDMR3PowerOn and PDMR3Resume to use PDMR3Suspend for cleaning up
2105 * on failure.
2106 */
2107 PDMNOTIFYASYNCSTATS Async;
2108 pdmR3NotifyAsyncInit(&Async, "PDMR3Suspend");
2109 for (;;)
2110 {
2111 pdmR3NotifyAsyncBeginLoop(&Async);
2112
2113 /*
2114 * Iterate thru the device instances and USB device instances,
2115 * processing the drivers associated with those.
2116 *
2117 * The attached drivers are normally processed first. Some devices
2118 * (like DevAHCI) though needs to be notified before the drivers so
2119 * that it doesn't kick off any new requests after the drivers stopped
2120 * taking any. (DrvVD changes to read-only in this particular case.)
2121 */
2122 RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
2123 /** @todo locking */
2124 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
2125 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2126 {
2127 unsigned const cAsyncStart = Async.cAsync;
2128
2129 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION)
2130 pdmR3SuspendDev(pVM, pDevIns, &Async);
2131
2132 if (Async.cAsync == cAsyncStart)
2133 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2134 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2135 if (!pdmR3SuspendDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
2136 break;
2137
2138 if ( Async.cAsync == cAsyncStart
2139 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION))
2140 pdmR3SuspendDev(pVM, pDevIns, &Async);
2141 }
2142
2143#ifdef VBOX_WITH_USB
2144 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2145 {
2146 unsigned const cAsyncStart = Async.cAsync;
2147
2148 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2149 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2150 if (!pdmR3SuspendDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
2151 break;
2152
2153 if (Async.cAsync == cAsyncStart)
2154 pdmR3SuspendUsb(pUsbIns, &Async);
2155 }
2156#endif
2157 if (!Async.cAsync)
2158 break;
2159 pdmR3NotifyAsyncLog(&Async);
2160 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
2161 }
2162
2163 /*
2164 * Suspend all threads.
2165 */
2166 pdmR3ThreadSuspendAll(pVM);
2167
2168 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2169 LogRel(("PDMR3Suspend: %'llu ns run time\n", cNsElapsed));
2170}
2171
2172
2173/**
2174 * Worker for PDMR3Resume that deals with one driver.
2175 *
2176 * @param pDrvIns The driver instance.
2177 * @param pszDevName The parent device name.
2178 * @param iDevInstance The parent device instance number.
2179 * @param iLun The parent LUN number.
2180 */
2181DECLINLINE(int) pdmR3ResumeDrv(PPDMDRVINS pDrvIns, const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
2182{
2183 Assert(pDrvIns->Internal.s.fVMSuspended);
2184 if (pDrvIns->pReg->pfnResume)
2185 {
2186 LogFlow(("PDMR3Resume: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
2187 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
2188 int rc = VINF_SUCCESS; pDrvIns->pReg->pfnResume(pDrvIns);
2189 if (RT_FAILURE(rc))
2190 {
2191 LogRel(("PDMR3Resume: Driver '%s'/%d on LUN#%d of device '%s'/%d -> %Rrc\n",
2192 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, rc));
2193 return rc;
2194 }
2195 }
2196 pDrvIns->Internal.s.fVMSuspended = false;
2197 return VINF_SUCCESS;
2198}
2199
2200
2201/**
2202 * Worker for PDMR3Resume that deals with one USB device instance.
2203 *
2204 * @returns VBox status code.
2205 * @param pUsbIns The USB device instance.
2206 */
2207DECLINLINE(int) pdmR3ResumeUsb(PPDMUSBINS pUsbIns)
2208{
2209 if (pUsbIns->Internal.s.fVMSuspended)
2210 {
2211 if (pUsbIns->pReg->pfnVMResume)
2212 {
2213 LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
2214 int rc = VINF_SUCCESS; pUsbIns->pReg->pfnVMResume(pUsbIns);
2215 if (RT_FAILURE(rc))
2216 {
2217 LogRel(("PDMR3Resume: Device '%s'/%d -> %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
2218 return rc;
2219 }
2220 }
2221 pUsbIns->Internal.s.fVMSuspended = false;
2222 }
2223 return VINF_SUCCESS;
2224}
2225
2226
2227/**
2228 * Worker for PDMR3Resume that deals with one device instance.
2229 *
2230 * @returns VBox status code.
2231 * @param pVM The cross context VM structure.
2232 * @param pDevIns The device instance.
2233 */
2234DECLINLINE(int) pdmR3ResumeDev(PVM pVM, PPDMDEVINS pDevIns)
2235{
2236 Assert(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED);
2237 if (pDevIns->pReg->pfnResume)
2238 {
2239 LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
2240 PDMCritSectEnter(pVM, pDevIns->pCritSectRoR3, VERR_IGNORED);
2241 int rc = VINF_SUCCESS; pDevIns->pReg->pfnResume(pDevIns);
2242 PDMCritSectLeave(pVM, pDevIns->pCritSectRoR3);
2243 if (RT_FAILURE(rc))
2244 {
2245 LogRel(("PDMR3Resume: Device '%s'/%d -> %Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
2246 return rc;
2247 }
2248 }
2249 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
2250 return VINF_SUCCESS;
2251}
2252
2253
2254/**
2255 * This function will notify all the devices and their
2256 * attached drivers about the VM now being resumed.
2257 *
2258 * @param pVM The cross context VM structure.
2259 */
2260VMMR3_INT_DECL(void) PDMR3Resume(PVM pVM)
2261{
2262 LogFlow(("PDMR3Resume:\n"));
2263
2264 /*
2265 * Iterate thru the device instances and USB device instances,
2266 * processing the drivers associated with those.
2267 */
2268 RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
2269 /** @todo locking */
2270 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
2271 int rc = VINF_SUCCESS;
2272 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns && RT_SUCCESS(rc); pDevIns = pDevIns->Internal.s.pNextR3)
2273 {
2274 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
2275 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
2276 rc = pdmR3ResumeDrv(pDrvIns, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun);
2277 if (RT_SUCCESS(rc))
2278 rc = pdmR3ResumeDev(pVM, pDevIns);
2279 }
2280
2281#ifdef VBOX_WITH_USB
2282 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns && RT_SUCCESS(rc); pUsbIns = pUsbIns->Internal.s.pNext)
2283 {
2284 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
2285 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
2286 rc = pdmR3ResumeDrv(pDrvIns, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun);
2287 if (RT_SUCCESS(rc))
2288 rc = pdmR3ResumeUsb(pUsbIns);
2289 }
2290#endif
2291
2292 /*
2293 * Resume all threads.
2294 */
2295 if (RT_SUCCESS(rc))
2296 pdmR3ThreadResumeAll(pVM);
2297
2298 /*
2299 * Resume the block cache.
2300 */
2301 if (RT_SUCCESS(rc))
2302 pdmR3BlkCacheResume(pVM);
2303
2304 /*
2305 * On failure, clean up via PDMR3Suspend.
2306 */
2307 if (RT_FAILURE(rc))
2308 PDMR3Suspend(pVM);
2309
2310 LogFlow(("PDMR3Resume: returns %Rrc\n", rc));
2311 return /*rc*/;
2312}
2313
2314
2315/**
2316 * Worker for PDMR3PowerOff that deals with one driver.
2317 *
2318 * @param pDrvIns The driver instance.
2319 * @param pAsync The structure for recording asynchronous
2320 * notification tasks.
2321 * @param pszDevName The parent device name.
2322 * @param iDevInstance The parent device instance number.
2323 * @param iLun The parent LUN number.
2324 */
2325DECLINLINE(bool) pdmR3PowerOffDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
2326 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
2327{
2328 if (!pDrvIns->Internal.s.fVMSuspended)
2329 {
2330 pDrvIns->Internal.s.fVMSuspended = true;
2331 if (pDrvIns->pReg->pfnPowerOff)
2332 {
2333 uint64_t cNsElapsed = RTTimeNanoTS();
2334
2335 if (!pDrvIns->Internal.s.pfnAsyncNotify)
2336 {
2337 LogFlow(("PDMR3PowerOff: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
2338 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
2339 pDrvIns->pReg->pfnPowerOff(pDrvIns);
2340 if (pDrvIns->Internal.s.pfnAsyncNotify)
2341 LogFlow(("PDMR3PowerOff: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
2342 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
2343 }
2344 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
2345 {
2346 LogFlow(("PDMR3PowerOff: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
2347 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
2348 pDrvIns->Internal.s.pfnAsyncNotify = NULL;
2349 }
2350
2351 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2352 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
2353 LogRel(("PDMR3PowerOff: Driver '%s'/%d on LUN#%d of device '%s'/%d took %'llu ns to power off\n",
2354 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, cNsElapsed));
2355
2356 if (pDrvIns->Internal.s.pfnAsyncNotify)
2357 {
2358 pDrvIns->Internal.s.fVMSuspended = false;
2359 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance,
2360 pszDevName, iDevInstance, iLun);
2361 return false;
2362 }
2363 }
2364 }
2365 return true;
2366}
2367
2368
2369/**
2370 * Worker for PDMR3PowerOff that deals with one USB device instance.
2371 *
2372 * @param pUsbIns The USB device instance.
2373 * @param pAsync The structure for recording asynchronous
2374 * notification tasks.
2375 */
2376DECLINLINE(void) pdmR3PowerOffUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
2377{
2378 if (!pUsbIns->Internal.s.fVMSuspended)
2379 {
2380 pUsbIns->Internal.s.fVMSuspended = true;
2381 if (pUsbIns->pReg->pfnVMPowerOff)
2382 {
2383 uint64_t cNsElapsed = RTTimeNanoTS();
2384
2385 if (!pUsbIns->Internal.s.pfnAsyncNotify)
2386 {
2387 LogFlow(("PDMR3PowerOff: Notifying - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
2388 pUsbIns->pReg->pfnVMPowerOff(pUsbIns);
2389 if (pUsbIns->Internal.s.pfnAsyncNotify)
2390 LogFlow(("PDMR3PowerOff: Async notification started - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
2391 }
2392 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
2393 {
2394 LogFlow(("PDMR3PowerOff: Async notification completed - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
2395 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
2396 }
2397 if (pUsbIns->Internal.s.pfnAsyncNotify)
2398 {
2399 pUsbIns->Internal.s.fVMSuspended = false;
2400 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
2401 }
2402
2403 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2404 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
2405 LogRel(("PDMR3PowerOff: USB device '%s'/%d took %'llu ns to power off\n",
2406 pUsbIns->pReg->szName, pUsbIns->iInstance, cNsElapsed));
2407
2408 }
2409 }
2410}
2411
2412
2413/**
2414 * Worker for PDMR3PowerOff that deals with one device instance.
2415 *
2416 * @param pVM The cross context VM structure.
2417 * @param pDevIns The device instance.
2418 * @param pAsync The structure for recording asynchronous notification tasks.
2419 */
2420DECLINLINE(void) pdmR3PowerOffDev(PVM pVM, PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
2421{
2422 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED))
2423 {
2424 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_SUSPENDED;
2425 if (pDevIns->pReg->pfnPowerOff)
2426 {
2427 uint64_t cNsElapsed = RTTimeNanoTS();
2428 PDMCritSectEnter(pVM, pDevIns->pCritSectRoR3, VERR_IGNORED);
2429
2430 if (!pDevIns->Internal.s.pfnAsyncNotify)
2431 {
2432 LogFlow(("PDMR3PowerOff: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
2433 pDevIns->pReg->pfnPowerOff(pDevIns);
2434 if (pDevIns->Internal.s.pfnAsyncNotify)
2435 LogFlow(("PDMR3PowerOff: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
2436 }
2437 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
2438 {
2439 LogFlow(("PDMR3PowerOff: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
2440 pDevIns->Internal.s.pfnAsyncNotify = NULL;
2441 }
2442 if (pDevIns->Internal.s.pfnAsyncNotify)
2443 {
2444 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
2445 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
2446 }
2447
2448 PDMCritSectLeave(pVM, pDevIns->pCritSectRoR3);
2449 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2450 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
2451 LogFlow(("PDMR3PowerOff: Device '%s'/%d took %'llu ns to power off\n",
2452 pDevIns->pReg->szName, pDevIns->iInstance, cNsElapsed));
2453 }
2454 }
2455}
2456
2457
2458/**
2459 * This function will notify all the devices and their
2460 * attached drivers about the VM being powered off.
2461 *
2462 * @param pVM The cross context VM structure.
2463 */
2464VMMR3DECL(void) PDMR3PowerOff(PVM pVM)
2465{
2466 LogFlow(("PDMR3PowerOff:\n"));
2467 uint64_t cNsElapsed = RTTimeNanoTS();
2468
2469 /*
2470 * Clear the suspended flags on all devices and drivers first because they
2471 * might have been set during a suspend but the power off callbacks should
2472 * be called in any case.
2473 */
2474 RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
2475 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2476 {
2477 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
2478
2479 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2480 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2481 pDrvIns->Internal.s.fVMSuspended = false;
2482 }
2483
2484#ifdef VBOX_WITH_USB
2485 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2486 {
2487 pUsbIns->Internal.s.fVMSuspended = false;
2488
2489 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2490 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2491 pDrvIns->Internal.s.fVMSuspended = false;
2492 }
2493#endif
2494 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
2495
2496 /*
2497 * The outer loop repeats until there are no more async requests.
2498 */
2499 PDMNOTIFYASYNCSTATS Async;
2500 pdmR3NotifyAsyncInit(&Async, "PDMR3PowerOff");
2501 for (;;)
2502 {
2503 pdmR3NotifyAsyncBeginLoop(&Async);
2504
2505 /*
2506 * Iterate thru the device instances and USB device instances,
2507 * processing the drivers associated with those.
2508 *
2509 * The attached drivers are normally processed first. Some devices
2510 * (like DevAHCI) though needs to be notified before the drivers so
2511 * that it doesn't kick off any new requests after the drivers stopped
2512 * taking any. (DrvVD changes to read-only in this particular case.)
2513 */
2514 /** @todo locking */
2515 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2516 {
2517 unsigned const cAsyncStart = Async.cAsync;
2518
2519 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION)
2520 pdmR3PowerOffDev(pVM, pDevIns, &Async);
2521
2522 if (Async.cAsync == cAsyncStart)
2523 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2524 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2525 if (!pdmR3PowerOffDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
2526 break;
2527
2528 if ( Async.cAsync == cAsyncStart
2529 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION))
2530 pdmR3PowerOffDev(pVM, pDevIns, &Async);
2531 }
2532
2533#ifdef VBOX_WITH_USB
2534 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2535 {
2536 unsigned const cAsyncStart = Async.cAsync;
2537
2538 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2539 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2540 if (!pdmR3PowerOffDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
2541 break;
2542
2543 if (Async.cAsync == cAsyncStart)
2544 pdmR3PowerOffUsb(pUsbIns, &Async);
2545 }
2546#endif
2547 if (!Async.cAsync)
2548 break;
2549 pdmR3NotifyAsyncLog(&Async);
2550 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
2551 }
2552
2553 /*
2554 * Suspend all threads.
2555 */
2556 pdmR3ThreadSuspendAll(pVM);
2557
2558 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2559 LogRel(("PDMR3PowerOff: %'llu ns run time\n", cNsElapsed));
2560}
2561
2562
2563/**
2564 * Queries the base interface of a device instance.
2565 *
2566 * The caller can use this to query other interfaces the device implements
2567 * and use them to talk to the device.
2568 *
2569 * @returns VBox status code.
2570 * @param pUVM The user mode VM handle.
2571 * @param pszDevice Device name.
2572 * @param iInstance Device instance.
2573 * @param ppBase Where to store the pointer to the base device interface on success.
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) PDMR3QueryDevice(PUVM pUVM, const char *pszDevice, unsigned iInstance, PPDMIBASE *ppBase)
2578{
2579 LogFlow(("PDMR3DeviceQuery: pszDevice=%p:{%s} iInstance=%u ppBase=%p\n", pszDevice, pszDevice, iInstance, ppBase));
2580 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2581 PVM const pVM = pUVM->pVM;
2582 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
2583
2584 /*
2585 * Iterate registered devices looking for the device.
2586 */
2587 size_t const cchDevice = strlen(pszDevice);
2588 RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
2589 for (PPDMDEV pDev = pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
2590 {
2591 if ( pDev->cchName == cchDevice
2592 && !memcmp(pDev->pReg->szName, pszDevice, cchDevice))
2593 {
2594 /*
2595 * Iterate device instances.
2596 */
2597 for (PPDMDEVINS pDevIns = pDev->pInstances; pDevIns; pDevIns = pDevIns->Internal.s.pPerDeviceNextR3)
2598 {
2599 if (pDevIns->iInstance == iInstance)
2600 {
2601 if (pDevIns->IBase.pfnQueryInterface)
2602 {
2603 *ppBase = &pDevIns->IBase;
2604 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
2605 LogFlow(("PDMR3DeviceQuery: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
2606 return VINF_SUCCESS;
2607 }
2608
2609 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
2610 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NO_IBASE\n"));
2611 return VERR_PDM_DEVICE_INSTANCE_NO_IBASE;
2612 }
2613 }
2614
2615 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
2616 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NOT_FOUND\n"));
2617 return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND;
2618 }
2619 }
2620
2621 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
2622 LogFlow(("PDMR3QueryDevice: returns VERR_PDM_DEVICE_NOT_FOUND\n"));
2623 return VERR_PDM_DEVICE_NOT_FOUND;
2624}
2625
2626
2627/**
2628 * Queries the base interface of a device LUN.
2629 *
2630 * This differs from PDMR3QueryLun by that it returns the interface on the
2631 * device and not the top level driver.
2632 *
2633 * @returns VBox status code.
2634 * @param pUVM The user mode VM handle.
2635 * @param pszDevice Device name.
2636 * @param iInstance Device instance.
2637 * @param iLun The Logical Unit to obtain the interface of.
2638 * @param ppBase Where to store the base interface pointer.
2639 * @remark We're not doing any locking ATM, so don't try call this at times when the
2640 * device chain is known to be updated.
2641 */
2642VMMR3DECL(int) PDMR3QueryDeviceLun(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
2643{
2644 LogFlow(("PDMR3QueryDeviceLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
2645 pszDevice, pszDevice, iInstance, iLun, ppBase));
2646 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2647 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2648
2649 /*
2650 * Find the LUN.
2651 */
2652 PPDMLUN pLun;
2653 int rc = pdmR3DevFindLun(pUVM->pVM, pszDevice, iInstance, iLun, &pLun);
2654 if (RT_SUCCESS(rc))
2655 {
2656 *ppBase = pLun->pBase;
2657 LogFlow(("PDMR3QueryDeviceLun: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
2658 return VINF_SUCCESS;
2659 }
2660 LogFlow(("PDMR3QueryDeviceLun: returns %Rrc\n", rc));
2661 return rc;
2662}
2663
2664
2665/**
2666 * Query the interface of the top level driver on a LUN.
2667 *
2668 * @returns VBox status code.
2669 * @param pUVM The user mode VM handle.
2670 * @param pszDevice Device name.
2671 * @param iInstance Device instance.
2672 * @param iLun The Logical Unit to obtain the interface of.
2673 * @param ppBase Where to store the base interface pointer.
2674 * @remark We're not doing any locking ATM, so don't try call this at times when the
2675 * device chain is known to be updated.
2676 */
2677VMMR3DECL(int) PDMR3QueryLun(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
2678{
2679 LogFlow(("PDMR3QueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
2680 pszDevice, pszDevice, iInstance, iLun, ppBase));
2681 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2682 PVM pVM = pUVM->pVM;
2683 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
2684
2685 /*
2686 * Find the LUN.
2687 */
2688 RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
2689 PPDMLUN pLun;
2690 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
2691 if (RT_SUCCESS(rc))
2692 {
2693 if (pLun->pTop)
2694 {
2695 *ppBase = &pLun->pTop->IBase;
2696 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
2697 LogFlow(("PDMR3QueryLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
2698 return VINF_SUCCESS;
2699 }
2700 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
2701 }
2702 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
2703 LogFlow(("PDMR3QueryLun: returns %Rrc\n", rc));
2704 return rc;
2705}
2706
2707
2708/**
2709 * Query the interface of a named driver on a LUN.
2710 *
2711 * If the driver appears more than once in the driver chain, the first instance
2712 * is returned.
2713 *
2714 * @returns VBox status code.
2715 * @param pUVM The user mode VM handle.
2716 * @param pszDevice Device name.
2717 * @param iInstance Device instance.
2718 * @param iLun The Logical Unit to obtain the interface of.
2719 * @param pszDriver The driver name.
2720 * @param ppBase Where to store the base interface pointer.
2721 *
2722 * @remark We're not doing any locking ATM, so don't try call this at times when the
2723 * device chain is known to be updated.
2724 */
2725VMMR3DECL(int) PDMR3QueryDriverOnLun(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, const char *pszDriver, PPPDMIBASE ppBase)
2726{
2727 LogFlow(("PDMR3QueryDriverOnLun: pszDevice=%p:{%s} iInstance=%u iLun=%u pszDriver=%p:{%s} ppBase=%p\n",
2728 pszDevice, pszDevice, iInstance, iLun, pszDriver, pszDriver, ppBase));
2729 *ppBase = NULL;
2730 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2731 PVM const pVM = pUVM->pVM;
2732 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
2733
2734 /*
2735 * Find the LUN.
2736 */
2737 RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
2738 PPDMLUN pLun;
2739 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
2740 if (RT_SUCCESS(rc))
2741 {
2742 if (pLun->pTop)
2743 {
2744 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2745 if (!strcmp(pDrvIns->pReg->szName, pszDriver))
2746 {
2747 *ppBase = &pDrvIns->IBase;
2748 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
2749 LogFlow(("PDMR3QueryDriverOnLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
2750 return VINF_SUCCESS;
2751 }
2752 rc = VERR_PDM_DRIVER_NOT_FOUND;
2753 }
2754 else
2755 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
2756 }
2757 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
2758 LogFlow(("PDMR3QueryDriverOnLun: returns %Rrc\n", rc));
2759 return rc;
2760}
2761
2762
2763/**
2764 * Executes pending DMA transfers.
2765 * Forced Action handler.
2766 *
2767 * @param pVM The cross context VM structure.
2768 */
2769VMMR3DECL(void) PDMR3DmaRun(PVM pVM)
2770{
2771 /** @todo this is bad in SMP... */
2772 /* Note! Not really SMP safe; restrict it to VCPU 0. */
2773 if (VMMGetCpuId(pVM) != 0)
2774 return;
2775
2776 if (VM_FF_TEST_AND_CLEAR(pVM, VM_FF_PDM_DMA))
2777 {
2778 PPDMDMAC const pDmac = pVM->pdm.s.pDmac;
2779 if (pDmac)
2780 {
2781 bool const fMore = pDmac->Reg.pfnRun(pDmac->pDevIns);
2782 if (fMore)
2783 VM_FF_SET(pVM, VM_FF_PDM_DMA);
2784 }
2785 }
2786}
2787
2788
2789/**
2790 * Allocates memory from the VMM device heap.
2791 *
2792 * @returns VBox status code.
2793 * @param pVM The cross context VM structure.
2794 * @param cbSize Allocation size.
2795 * @param pfnNotify Mapping/unmapping notification callback.
2796 * @param ppv Ring-3 pointer. (out)
2797 */
2798VMMR3_INT_DECL(int) PDMR3VmmDevHeapAlloc(PVM pVM, size_t cbSize, PFNPDMVMMDEVHEAPNOTIFY pfnNotify, RTR3PTR *ppv)
2799{
2800#ifdef DEBUG_bird
2801 if (!cbSize || cbSize > pVM->pdm.s.cbVMMDevHeapLeft)
2802 return VERR_NO_MEMORY;
2803#else
2804 AssertReturn(cbSize && cbSize <= pVM->pdm.s.cbVMMDevHeapLeft, VERR_NO_MEMORY);
2805#endif
2806
2807 Log(("PDMR3VMMDevHeapAlloc: %#zx\n", cbSize));
2808
2809 /** @todo Not a real heap as there's currently only one user. */
2810 *ppv = pVM->pdm.s.pvVMMDevHeap;
2811 pVM->pdm.s.cbVMMDevHeapLeft = 0;
2812 pVM->pdm.s.pfnVMMDevHeapNotify = pfnNotify;
2813 return VINF_SUCCESS;
2814}
2815
2816
2817/**
2818 * Frees memory from the VMM device heap
2819 *
2820 * @returns VBox status code.
2821 * @param pVM The cross context VM structure.
2822 * @param pv Ring-3 pointer.
2823 */
2824VMMR3_INT_DECL(int) PDMR3VmmDevHeapFree(PVM pVM, RTR3PTR pv)
2825{
2826 Log(("PDMR3VmmDevHeapFree: %RHv\n", pv)); RT_NOREF_PV(pv);
2827
2828 /** @todo not a real heap as there's currently only one user. */
2829 pVM->pdm.s.cbVMMDevHeapLeft = pVM->pdm.s.cbVMMDevHeap;
2830 pVM->pdm.s.pfnVMMDevHeapNotify = NULL;
2831 return VINF_SUCCESS;
2832}
2833
2834
2835/**
2836 * Worker for DBGFR3TraceConfig that checks if the given tracing group name
2837 * matches a device or driver name and applies the tracing config change.
2838 *
2839 * @returns VINF_SUCCESS or VERR_NOT_FOUND.
2840 * @param pVM The cross context VM structure.
2841 * @param pszName The tracing config group name. This is NULL if
2842 * the operation applies to every device and
2843 * driver.
2844 * @param cchName The length to match.
2845 * @param fEnable Whether to enable or disable the corresponding
2846 * trace points.
2847 * @param fApply Whether to actually apply the changes or just do
2848 * existence checks.
2849 */
2850VMMR3_INT_DECL(int) PDMR3TracingConfig(PVM pVM, const char *pszName, size_t cchName, bool fEnable, bool fApply)
2851{
2852 /** @todo This code is potentially racing driver attaching and detaching. */
2853
2854 /*
2855 * Applies to all.
2856 */
2857 if (pszName == NULL)
2858 {
2859 AssertReturn(fApply, VINF_SUCCESS);
2860 RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
2861
2862 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2863 {
2864 pDevIns->fTracing = fEnable;
2865 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2866 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2867 pDrvIns->fTracing = fEnable;
2868 }
2869
2870#ifdef VBOX_WITH_USB
2871 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2872 {
2873 pUsbIns->fTracing = fEnable;
2874 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2875 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2876 pDrvIns->fTracing = fEnable;
2877
2878 }
2879#endif
2880
2881 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
2882 return VINF_SUCCESS;
2883 }
2884
2885 /*
2886 * Specific devices, USB devices or drivers.
2887 * Decode prefix to figure which of these it applies to.
2888 */
2889 if (cchName <= 3)
2890 return VERR_NOT_FOUND;
2891
2892 uint32_t cMatches = 0;
2893 if (!strncmp("dev", pszName, 3))
2894 {
2895 RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw); /* Shared-mode is sufficient for this. */
2896 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2897 {
2898 const char *pszDevName = pDevIns->Internal.s.pDevR3->pReg->szName;
2899 size_t cchDevName = strlen(pszDevName);
2900 if ( ( cchDevName == cchName
2901 && RTStrNICmp(pszName, pszDevName, cchDevName))
2902 || ( cchDevName == cchName - 3
2903 && RTStrNICmp(pszName + 3, pszDevName, cchDevName)) )
2904 {
2905 cMatches++;
2906 if (fApply)
2907 pDevIns->fTracing = fEnable;
2908 }
2909 }
2910 }
2911 else if (!strncmp("usb", pszName, 3))
2912 {
2913 RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw); /* Shared-mode is sufficient for this. */
2914 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2915 {
2916 const char *pszUsbName = pUsbIns->Internal.s.pUsbDev->pReg->szName;
2917 size_t cchUsbName = strlen(pszUsbName);
2918 if ( ( cchUsbName == cchName
2919 && RTStrNICmp(pszName, pszUsbName, cchUsbName))
2920 || ( cchUsbName == cchName - 3
2921 && RTStrNICmp(pszName + 3, pszUsbName, cchUsbName)) )
2922 {
2923 cMatches++;
2924 if (fApply)
2925 pUsbIns->fTracing = fEnable;
2926 }
2927 }
2928 }
2929 else if (!strncmp("drv", pszName, 3))
2930 {
2931 AssertReturn(fApply, VINF_SUCCESS);
2932 RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw); /* Shared-mode is sufficient for this. */
2933
2934 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2935 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2936 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2937 {
2938 const char *pszDrvName = pDrvIns->Internal.s.pDrv->pReg->szName;
2939 size_t cchDrvName = strlen(pszDrvName);
2940 if ( ( cchDrvName == cchName
2941 && RTStrNICmp(pszName, pszDrvName, cchDrvName))
2942 || ( cchDrvName == cchName - 3
2943 && RTStrNICmp(pszName + 3, pszDrvName, cchDrvName)) )
2944 {
2945 cMatches++;
2946 if (fApply)
2947 pDrvIns->fTracing = fEnable;
2948 }
2949 }
2950
2951#ifdef VBOX_WITH_USB
2952 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2953 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2954 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2955 {
2956 const char *pszDrvName = pDrvIns->Internal.s.pDrv->pReg->szName;
2957 size_t cchDrvName = strlen(pszDrvName);
2958 if ( ( cchDrvName == cchName
2959 && RTStrNICmp(pszName, pszDrvName, cchDrvName))
2960 || ( cchDrvName == cchName - 3
2961 && RTStrNICmp(pszName + 3, pszDrvName, cchDrvName)) )
2962 {
2963 cMatches++;
2964 if (fApply)
2965 pDrvIns->fTracing = fEnable;
2966 }
2967 }
2968#endif
2969
2970 }
2971 else
2972 return VERR_NOT_FOUND;
2973
2974 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
2975 return cMatches > 0 ? VINF_SUCCESS : VERR_NOT_FOUND;
2976}
2977
2978
2979/**
2980 * Worker for DBGFR3TraceQueryConfig that checks whether all drivers, devices,
2981 * and USB device have the same tracing settings.
2982 *
2983 * @returns true / false.
2984 * @param pVM The cross context VM structure.
2985 * @param fEnabled The tracing setting to check for.
2986 */
2987VMMR3_INT_DECL(bool) PDMR3TracingAreAll(PVM pVM, bool fEnabled)
2988{
2989 RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
2990
2991 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2992 {
2993 if (pDevIns->fTracing != (uint32_t)fEnabled)
2994 {
2995 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
2996 return false;
2997 }
2998
2999 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
3000 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
3001 if (pDrvIns->fTracing != (uint32_t)fEnabled)
3002 {
3003 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
3004 return false;
3005 }
3006 }
3007
3008#ifdef VBOX_WITH_USB
3009 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
3010 {
3011 if (pUsbIns->fTracing != (uint32_t)fEnabled)
3012 {
3013 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
3014 return false;
3015 }
3016
3017 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
3018 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
3019 if (pDrvIns->fTracing != (uint32_t)fEnabled)
3020 {
3021 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
3022 return false;
3023 }
3024 }
3025#endif
3026
3027 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
3028 return true;
3029}
3030
3031
3032/**
3033 * Worker for PDMR3TracingQueryConfig that adds a prefixed name to the output
3034 * string.
3035 *
3036 * @returns VINF_SUCCESS or VERR_BUFFER_OVERFLOW
3037 * @param ppszDst The pointer to the output buffer pointer.
3038 * @param pcbDst The pointer to the output buffer size.
3039 * @param fSpace Whether to add a space before the name.
3040 * @param pszPrefix The name prefix.
3041 * @param pszName The name.
3042 */
3043static int pdmR3TracingAdd(char **ppszDst, size_t *pcbDst, bool fSpace, const char *pszPrefix, const char *pszName)
3044{
3045 size_t const cchPrefix = strlen(pszPrefix);
3046 if (!RTStrNICmp(pszPrefix, pszName, cchPrefix))
3047 pszName += cchPrefix;
3048 size_t const cchName = strlen(pszName);
3049
3050 size_t const cchThis = cchName + cchPrefix + fSpace;
3051 if (cchThis >= *pcbDst)
3052 return VERR_BUFFER_OVERFLOW;
3053 if (fSpace)
3054 {
3055 **ppszDst = ' ';
3056 memcpy(*ppszDst + 1, pszPrefix, cchPrefix);
3057 memcpy(*ppszDst + 1 + cchPrefix, pszName, cchName + 1);
3058 }
3059 else
3060 {
3061 memcpy(*ppszDst, pszPrefix, cchPrefix);
3062 memcpy(*ppszDst + cchPrefix, pszName, cchName + 1);
3063 }
3064 *ppszDst += cchThis;
3065 *pcbDst -= cchThis;
3066 return VINF_SUCCESS;
3067}
3068
3069
3070/**
3071 * Worker for DBGFR3TraceQueryConfig use when not everything is either enabled
3072 * or disabled.
3073 *
3074 * @returns VINF_SUCCESS or VERR_BUFFER_OVERFLOW
3075 * @param pVM The cross context VM structure.
3076 * @param pszConfig Where to store the config spec.
3077 * @param cbConfig The size of the output buffer.
3078 */
3079VMMR3_INT_DECL(int) PDMR3TracingQueryConfig(PVM pVM, char *pszConfig, size_t cbConfig)
3080{
3081 char *pszDst = pszConfig;
3082 size_t cbDst = cbConfig;
3083
3084 RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
3085
3086 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
3087 {
3088 if (pDevIns->fTracing)
3089 {
3090 int rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "dev", pDevIns->Internal.s.pDevR3->pReg->szName);
3091 if (RT_FAILURE(rc))
3092 {
3093 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
3094 return rc;
3095 }
3096 }
3097
3098 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
3099 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
3100 if (pDrvIns->fTracing)
3101 {
3102 int rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "drv", pDrvIns->Internal.s.pDrv->pReg->szName);
3103 if (RT_FAILURE(rc))
3104 {
3105 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
3106 return rc;
3107 }
3108 }
3109 }
3110
3111#ifdef VBOX_WITH_USB
3112 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
3113 {
3114 if (pUsbIns->fTracing)
3115 {
3116 int rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "usb", pUsbIns->Internal.s.pUsbDev->pReg->szName);
3117 if (RT_FAILURE(rc))
3118 {
3119 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
3120 return rc;
3121 }
3122 }
3123
3124 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
3125 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
3126 if (pDrvIns->fTracing)
3127 {
3128 int rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "drv", pDrvIns->Internal.s.pDrv->pReg->szName);
3129 if (RT_FAILURE(rc))
3130 {
3131 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
3132 return rc;
3133 }
3134 }
3135 }
3136#endif
3137
3138 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
3139 return VINF_SUCCESS;
3140}
3141
3142
3143/**
3144 * Checks that a PDMDRVREG::szName, PDMDEVREG::szName or PDMUSBREG::szName
3145 * field contains only a limited set of ASCII characters.
3146 *
3147 * @returns true / false.
3148 * @param pszName The name to validate.
3149 */
3150bool pdmR3IsValidName(const char *pszName)
3151{
3152 char ch;
3153 while ( (ch = *pszName) != '\0'
3154 && ( RT_C_IS_ALNUM(ch)
3155 || ch == '-'
3156 || ch == ' ' /** @todo disallow this! */
3157 || ch == '_') )
3158 pszName++;
3159 return ch == '\0';
3160}
3161
3162
3163/**
3164 * Info handler for 'pdmtracingids'.
3165 *
3166 * @param pVM The cross context VM structure.
3167 * @param pHlp The output helpers.
3168 * @param pszArgs The optional user arguments.
3169 *
3170 * @remarks Can be called on most threads.
3171 */
3172static DECLCALLBACK(void) pdmR3InfoTracingIds(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
3173{
3174 /*
3175 * Parse the argument (optional).
3176 */
3177 if ( pszArgs
3178 && *pszArgs
3179 && strcmp(pszArgs, "all")
3180 && strcmp(pszArgs, "devices")
3181 && strcmp(pszArgs, "drivers")
3182 && strcmp(pszArgs, "usb"))
3183 {
3184 pHlp->pfnPrintf(pHlp, "Unable to grok '%s'\n", pszArgs);
3185 return;
3186 }
3187 bool fAll = !pszArgs || !*pszArgs || !strcmp(pszArgs, "all");
3188 bool fDevices = fAll || !strcmp(pszArgs, "devices");
3189 bool fUsbDevs = fAll || !strcmp(pszArgs, "usb");
3190 bool fDrivers = fAll || !strcmp(pszArgs, "drivers");
3191
3192 /*
3193 * Produce the requested output.
3194 */
3195/** @todo lock PDM lists! */
3196 /* devices */
3197 if (fDevices)
3198 {
3199 pHlp->pfnPrintf(pHlp, "Device tracing IDs:\n");
3200 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
3201 pHlp->pfnPrintf(pHlp, "%05u %s\n", pDevIns->idTracing, pDevIns->Internal.s.pDevR3->pReg->szName);
3202 }
3203
3204 /* USB devices */
3205 if (fUsbDevs)
3206 {
3207 pHlp->pfnPrintf(pHlp, "USB device tracing IDs:\n");
3208 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
3209 pHlp->pfnPrintf(pHlp, "%05u %s\n", pUsbIns->idTracing, pUsbIns->Internal.s.pUsbDev->pReg->szName);
3210 }
3211
3212 /* Drivers */
3213 if (fDrivers)
3214 {
3215 pHlp->pfnPrintf(pHlp, "Driver tracing IDs:\n");
3216 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
3217 {
3218 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
3219 {
3220 uint32_t iLevel = 0;
3221 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown, iLevel++)
3222 pHlp->pfnPrintf(pHlp, "%05u %s (level %u, lun %u, dev %s)\n",
3223 pDrvIns->idTracing, pDrvIns->Internal.s.pDrv->pReg->szName,
3224 iLevel, pLun->iLun, pDevIns->Internal.s.pDevR3->pReg->szName);
3225 }
3226 }
3227
3228 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
3229 {
3230 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
3231 {
3232 uint32_t iLevel = 0;
3233 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown, iLevel++)
3234 pHlp->pfnPrintf(pHlp, "%05u %s (level %u, lun %u, dev %s)\n",
3235 pDrvIns->idTracing, pDrvIns->Internal.s.pDrv->pReg->szName,
3236 iLevel, pLun->iLun, pUsbIns->Internal.s.pUsbDev->pReg->szName);
3237 }
3238 }
3239 }
3240}
3241
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