VirtualBox

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

Last change on this file since 107818 was 107227, checked in by vboxsync, 3 months ago

VMM: Cleaning up ARMv8 / x86 split. jiraref:VBP-1470

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 122.6 KB
Line 
1/* $Id: PDM.cpp 107227 2024-12-04 15:20:14Z 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.Ic.pDevInsRC)
608 pVM->pdm.s.Ic.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#ifdef 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#elif defined(VBOX_VMM_TARGET_X86)
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#else
1040# error "port me"
1041#endif
1042 SSMR3PutU32(pSSM, VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_NMI));
1043 SSMR3PutU32(pSSM, VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_SMI));
1044 }
1045 SSMR3PutU32(pSSM, VM_FF_IS_SET(pVM, VM_FF_PDM_DMA));
1046
1047 pdmR3SaveBoth(pVM, pSSM);
1048 return VINF_SUCCESS;
1049}
1050
1051
1052/**
1053 * Prepare state load operation.
1054 *
1055 * This will dispatch pending operations and clear the FFs governed by PDM and its devices.
1056 *
1057 * @returns VBox status code.
1058 * @param pVM The cross context VM structure.
1059 * @param pSSM The SSM handle.
1060 */
1061static DECLCALLBACK(int) pdmR3LoadPrep(PVM pVM, PSSMHANDLE pSSM)
1062{
1063 LogFlow(("pdmR3LoadPrep: %s%s\n",
1064 VM_FF_IS_SET(pVM, VM_FF_PDM_QUEUES) ? " VM_FF_PDM_QUEUES" : "",
1065 VM_FF_IS_SET(pVM, VM_FF_PDM_DMA) ? " VM_FF_PDM_DMA" : ""));
1066#ifdef LOG_ENABLED
1067 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
1068 {
1069 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
1070# ifdef VBOX_VMM_TARGET_ARMV8
1071 LogFlow(("pdmR3LoadPrep: VCPU %u %s%s\n", idCpu,
1072 VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_IRQ) ? " VMCPU_FF_INTERRUPT_IRQ" : "",
1073 VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_FIQ) ? " VMCPU_FF_INTERRUPT_FIQ" : ""));
1074# elif defined(VBOX_VMM_TARGET_X86)
1075 LogFlow(("pdmR3LoadPrep: VCPU %u %s%s\n", idCpu,
1076 VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC) ? " VMCPU_FF_INTERRUPT_APIC" : "",
1077 VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_PIC) ? " VMCPU_FF_INTERRUPT_PIC" : ""));
1078# else
1079# error "port me"
1080# endif
1081 }
1082#endif
1083 NOREF(pSSM);
1084
1085 /*
1086 * In case there is work pending that will raise an interrupt,
1087 * start a DMA transfer, or release a lock. (unlikely)
1088 */
1089 if (VM_FF_IS_SET(pVM, VM_FF_PDM_QUEUES))
1090 PDMR3QueueFlushAll(pVM);
1091
1092 /* Clear the FFs. */
1093 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
1094 {
1095 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
1096#ifdef VBOX_VMM_TARGET_ARMV8
1097 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_IRQ);
1098 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_FIQ);
1099#elif defined(VBOX_VMM_TARGET_X86)
1100 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_APIC);
1101 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_PIC);
1102#else
1103# error "port me"
1104#endif
1105 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_NMI);
1106 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_SMI);
1107 }
1108 VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
1109
1110 return VINF_SUCCESS;
1111}
1112
1113
1114/**
1115 * Execute state load operation.
1116 *
1117 * @returns VBox status code.
1118 * @param pVM The cross context VM structure.
1119 * @param pSSM SSM operation handle.
1120 * @param uVersion Data layout version.
1121 * @param uPass The data pass.
1122 */
1123static DECLCALLBACK(int) pdmR3LoadExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
1124{
1125 int rc;
1126
1127 LogFlow(("pdmR3LoadExec: uPass=%#x\n", uPass));
1128
1129 /*
1130 * Validate version.
1131 */
1132 if ( uVersion != PDM_SAVED_STATE_VERSION
1133 && uVersion != PDM_SAVED_STATE_VERSION_PRE_NMI_FF
1134 && uVersion != PDM_SAVED_STATE_VERSION_PRE_PDM_AUDIO)
1135 {
1136 AssertMsgFailed(("Invalid version uVersion=%d!\n", uVersion));
1137 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1138 }
1139
1140 if (uPass == SSM_PASS_FINAL)
1141 {
1142 /*
1143 * Load the interrupt and DMA states.
1144 *
1145 * The APIC, PIC and DMA devices does not restore these, we do. In the
1146 * APIC and PIC cases, it is possible that some devices is incorrectly
1147 * setting IRQs during restore. We'll warn when this happens. (There
1148 * are debug assertions in PDMDevMiscHlp.cpp and APICAll.cpp for
1149 * catching the buggy device.)
1150 */
1151 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
1152 {
1153 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
1154
1155 /* APIC/IRQ interrupt */
1156 uint32_t fInterruptPending = 0;
1157 rc = SSMR3GetU32(pSSM, &fInterruptPending);
1158 if (RT_FAILURE(rc))
1159 return rc;
1160 if (fInterruptPending & ~1)
1161 {
1162 AssertMsgFailed(("fInterruptPending=%#x (APIC)\n", fInterruptPending));
1163 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1164 }
1165#ifdef VBOX_VMM_TARGET_ARMV8
1166 AssertLogRelMsg(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_IRQ),
1167 ("VCPU%03u: VMCPU_FF_INTERRUPT_IRQ set! Devices shouldn't set interrupts during state restore...\n", idCpu));
1168 if (fInterruptPending)
1169 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_IRQ);
1170
1171#elif defined(VBOX_VMM_TARGET_X86)
1172 AssertLogRelMsg(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC),
1173 ("VCPU%03u: VMCPU_FF_INTERRUPT_APIC set! Devices shouldn't set interrupts during state restore...\n", idCpu));
1174 if (fInterruptPending)
1175 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC);
1176#else
1177# error "port me"
1178#endif
1179
1180 /* PIC/FIQ interrupt */
1181 fInterruptPending = 0;
1182 rc = SSMR3GetU32(pSSM, &fInterruptPending);
1183 if (RT_FAILURE(rc))
1184 return rc;
1185 if (fInterruptPending & ~1)
1186 {
1187 AssertMsgFailed(("fInterruptPending=%#x (PIC)\n", fInterruptPending));
1188 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1189 }
1190#ifdef VBOX_VMM_TARGET_ARMV8
1191 AssertLogRelMsg(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_FIQ),
1192 ("VCPU%03u: VMCPU_FF_INTERRUPT_FIQ set! Devices shouldn't set interrupts during state restore...\n", idCpu));
1193 if (fInterruptPending)
1194 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_FIQ);
1195
1196#elif defined(VBOX_VMM_TARGET_X86)
1197 AssertLogRelMsg(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_PIC),
1198 ("VCPU%03u: VMCPU_FF_INTERRUPT_PIC set! Devices shouldn't set interrupts during state restore...\n", idCpu));
1199 if (fInterruptPending)
1200 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_PIC);
1201#else
1202# error "port me"
1203#endif
1204
1205 if (uVersion > PDM_SAVED_STATE_VERSION_PRE_NMI_FF)
1206 {
1207 /* NMI interrupt */
1208 fInterruptPending = 0;
1209 rc = SSMR3GetU32(pSSM, &fInterruptPending);
1210 if (RT_FAILURE(rc))
1211 return rc;
1212 if (fInterruptPending & ~1)
1213 {
1214 AssertMsgFailed(("fInterruptPending=%#x (NMI)\n", fInterruptPending));
1215 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1216 }
1217 AssertLogRelMsg(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_NMI), ("VCPU%3u: VMCPU_FF_INTERRUPT_NMI set!\n", idCpu));
1218 if (fInterruptPending)
1219 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_NMI);
1220
1221 /* SMI interrupt */
1222 fInterruptPending = 0;
1223 rc = SSMR3GetU32(pSSM, &fInterruptPending);
1224 if (RT_FAILURE(rc))
1225 return rc;
1226 if (fInterruptPending & ~1)
1227 {
1228 AssertMsgFailed(("fInterruptPending=%#x (SMI)\n", fInterruptPending));
1229 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1230 }
1231 AssertLogRelMsg(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_SMI), ("VCPU%3u: VMCPU_FF_INTERRUPT_SMI set!\n", idCpu));
1232 if (fInterruptPending)
1233 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_SMI);
1234 }
1235 }
1236
1237 /* DMA pending */
1238 uint32_t fDMAPending = 0;
1239 rc = SSMR3GetU32(pSSM, &fDMAPending);
1240 if (RT_FAILURE(rc))
1241 return rc;
1242 if (fDMAPending & ~1)
1243 {
1244 AssertMsgFailed(("fDMAPending=%#x\n", fDMAPending));
1245 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1246 }
1247 if (fDMAPending)
1248 VM_FF_SET(pVM, VM_FF_PDM_DMA);
1249 Log(("pdmR3LoadExec: VM_FF_PDM_DMA=%RTbool\n", VM_FF_IS_SET(pVM, VM_FF_PDM_DMA)));
1250 }
1251
1252 /*
1253 * Load the list of devices and verify that they are all there.
1254 */
1255 RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
1256 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1257 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_FOUND;
1258 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
1259
1260 for (uint32_t i = 0; ; i++)
1261 {
1262 /* Get the sequence number / terminator. */
1263 uint32_t u32Sep;
1264 rc = SSMR3GetU32(pSSM, &u32Sep);
1265 if (RT_FAILURE(rc))
1266 return rc;
1267 if (u32Sep == UINT32_MAX)
1268 break;
1269 if (u32Sep != i)
1270 AssertMsgFailedReturn(("Out of sequence. u32Sep=%#x i=%#x\n", u32Sep, i), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
1271
1272 /* Get the name and instance number. */
1273 char szName[RT_SIZEOFMEMB(PDMDEVREG, szName)];
1274 rc = SSMR3GetStrZ(pSSM, szName, sizeof(szName));
1275 if (RT_FAILURE(rc))
1276 return rc;
1277 uint32_t iInstance;
1278 rc = SSMR3GetU32(pSSM, &iInstance);
1279 if (RT_FAILURE(rc))
1280 return rc;
1281
1282 /* Try locate it. */
1283 RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
1284 PPDMDEVINS pDevIns;
1285 for (pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1286 if ( !RTStrCmp(szName, pDevIns->pReg->szName)
1287 && pDevIns->iInstance == iInstance)
1288 {
1289 AssertLogRelMsgReturnStmt(!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_FOUND),
1290 ("%s/#%u\n", pDevIns->pReg->szName, pDevIns->iInstance),
1291 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw),
1292 VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
1293 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_FOUND;
1294 break;
1295 }
1296 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
1297
1298 if (!pDevIns)
1299 {
1300 bool fSkip = false;
1301
1302 /* Skip the non-existing (deprecated) "AudioSniffer" device stored in the saved state. */
1303 if ( uVersion <= PDM_SAVED_STATE_VERSION_PRE_PDM_AUDIO
1304 && !RTStrCmp(szName, "AudioSniffer"))
1305 fSkip = true;
1306
1307 if (!fSkip)
1308 {
1309 LogRel(("Device '%s'/%d not found in current config\n", szName, iInstance));
1310 if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
1311 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Device '%s'/%d not found in current config"), szName, iInstance);
1312 }
1313 }
1314 }
1315
1316 /*
1317 * Check that no additional devices were configured.
1318 */
1319 RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
1320 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1321 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_FOUND))
1322 {
1323 LogRel(("Device '%s'/%d not found in the saved state\n", pDevIns->pReg->szName, pDevIns->iInstance));
1324 /** @todo The TPM PPI device was added due to @bugref{10701} and it is not an issue if it isn't there
1325 * in the saved state because there is nothing to load. It might make sense to add a new
1326 * flag to PDMDEVREG::fFlags to indicate that having a new device added for a saved state is okay.
1327 * (For now I just want to get saved states unwedged).
1328 */
1329 if ( SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT
1330 && RTStrCmp(pDevIns->pReg->szName, "tpm-ppi") /* !!HACK ALERT!! - ugly, see todo above */)
1331 {
1332 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
1333 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Device '%s'/%d not found in the saved state"),
1334 pDevIns->pReg->szName, pDevIns->iInstance);
1335 }
1336 }
1337 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
1338
1339 /*
1340 * Indicate that we've been called (for assertions).
1341 */
1342 pVM->pdm.s.fStateLoaded = true;
1343
1344 return VINF_SUCCESS;
1345}
1346
1347
1348/**
1349 * Worker for PDMR3PowerOn that deals with one driver.
1350 *
1351 * @param pDrvIns The driver instance.
1352 * @param pszDevName The parent device name.
1353 * @param iDevInstance The parent device instance number.
1354 * @param iLun The parent LUN number.
1355 */
1356DECLINLINE(int) pdmR3PowerOnDrv(PPDMDRVINS pDrvIns, const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1357{
1358 Assert(pDrvIns->Internal.s.fVMSuspended);
1359 if (pDrvIns->pReg->pfnPowerOn)
1360 {
1361 LogFlow(("PDMR3PowerOn: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1362 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1363 int rc = VINF_SUCCESS; pDrvIns->pReg->pfnPowerOn(pDrvIns);
1364 if (RT_FAILURE(rc))
1365 {
1366 LogRel(("PDMR3PowerOn: Driver '%s'/%d on LUN#%d of device '%s'/%d -> %Rrc\n",
1367 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, rc));
1368 return rc;
1369 }
1370 }
1371 pDrvIns->Internal.s.fVMSuspended = false;
1372 return VINF_SUCCESS;
1373}
1374
1375
1376/**
1377 * Worker for PDMR3PowerOn that deals with one USB device instance.
1378 *
1379 * @returns VBox status code.
1380 * @param pUsbIns The USB device instance.
1381 */
1382DECLINLINE(int) pdmR3PowerOnUsb(PPDMUSBINS pUsbIns)
1383{
1384 Assert(pUsbIns->Internal.s.fVMSuspended);
1385 if (pUsbIns->pReg->pfnVMPowerOn)
1386 {
1387 LogFlow(("PDMR3PowerOn: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1388 int rc = VINF_SUCCESS; pUsbIns->pReg->pfnVMPowerOn(pUsbIns);
1389 if (RT_FAILURE(rc))
1390 {
1391 LogRel(("PDMR3PowerOn: Device '%s'/%d -> %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1392 return rc;
1393 }
1394 }
1395 pUsbIns->Internal.s.fVMSuspended = false;
1396 return VINF_SUCCESS;
1397}
1398
1399
1400/**
1401 * Worker for PDMR3PowerOn that deals with one device instance.
1402 *
1403 * @returns VBox status code.
1404 * @param pVM The cross context VM structure.
1405 * @param pDevIns The device instance.
1406 */
1407DECLINLINE(int) pdmR3PowerOnDev(PVM pVM, PPDMDEVINS pDevIns)
1408{
1409 Assert(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED);
1410 if (pDevIns->pReg->pfnPowerOn)
1411 {
1412 LogFlow(("PDMR3PowerOn: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1413 PDMCritSectEnter(pVM, pDevIns->pCritSectRoR3, VERR_IGNORED);
1414 int rc = VINF_SUCCESS; pDevIns->pReg->pfnPowerOn(pDevIns);
1415 PDMCritSectLeave(pVM, pDevIns->pCritSectRoR3);
1416 if (RT_FAILURE(rc))
1417 {
1418 LogRel(("PDMR3PowerOn: Device '%s'/%d -> %Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
1419 return rc;
1420 }
1421 }
1422 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
1423 return VINF_SUCCESS;
1424}
1425
1426
1427/**
1428 * This function will notify all the devices and their
1429 * attached drivers about the VM now being powered on.
1430 *
1431 * @param pVM The cross context VM structure.
1432 */
1433VMMR3DECL(void) PDMR3PowerOn(PVM pVM)
1434{
1435 LogFlow(("PDMR3PowerOn:\n"));
1436
1437 /*
1438 * Iterate thru the device instances and USB device instances,
1439 * processing the drivers associated with those.
1440 */
1441 RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
1442 /** @todo locking */
1443 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
1444 int rc = VINF_SUCCESS;
1445 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns && RT_SUCCESS(rc); pDevIns = pDevIns->Internal.s.pNextR3)
1446 {
1447 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1448 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1449 rc = pdmR3PowerOnDrv(pDrvIns, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun);
1450 if (RT_SUCCESS(rc))
1451 rc = pdmR3PowerOnDev(pVM, pDevIns);
1452 }
1453
1454#ifdef VBOX_WITH_USB
1455 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns && RT_SUCCESS(rc); pUsbIns = pUsbIns->Internal.s.pNext)
1456 {
1457 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
1458 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
1459 rc = pdmR3PowerOnDrv(pDrvIns, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun);
1460 if (RT_SUCCESS(rc))
1461 rc = pdmR3PowerOnUsb(pUsbIns);
1462 }
1463#endif
1464
1465#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
1466 pdmR3AsyncCompletionResume(pVM);
1467#endif
1468
1469 /*
1470 * Resume all threads.
1471 */
1472 if (RT_SUCCESS(rc))
1473 pdmR3ThreadResumeAll(pVM);
1474
1475 /*
1476 * On failure, clean up via PDMR3Suspend.
1477 */
1478 if (RT_FAILURE(rc))
1479 PDMR3Suspend(pVM);
1480
1481 LogFlow(("PDMR3PowerOn: returns %Rrc\n", rc));
1482 return /*rc*/;
1483}
1484
1485
1486/**
1487 * Initializes the asynchronous notifi stats structure.
1488 *
1489 * @param pThis The asynchronous notifification stats.
1490 * @param pszOp The name of the operation.
1491 */
1492static void pdmR3NotifyAsyncInit(PPDMNOTIFYASYNCSTATS pThis, const char *pszOp)
1493{
1494 pThis->uStartNsTs = RTTimeNanoTS();
1495 pThis->cNsElapsedNextLog = 0;
1496 pThis->cLoops = 0;
1497 pThis->cAsync = 0;
1498 pThis->pszOp = pszOp;
1499 pThis->offList = 0;
1500 pThis->szList[0] = '\0';
1501}
1502
1503
1504/**
1505 * Begin a new loop, prepares to gather new stats.
1506 *
1507 * @param pThis The asynchronous notifification stats.
1508 */
1509static void pdmR3NotifyAsyncBeginLoop(PPDMNOTIFYASYNCSTATS pThis)
1510{
1511 pThis->cLoops++;
1512 pThis->cAsync = 0;
1513 pThis->offList = 0;
1514 pThis->szList[0] = '\0';
1515}
1516
1517
1518/**
1519 * Records a device or USB device with a pending asynchronous notification.
1520 *
1521 * @param pThis The asynchronous notifification stats.
1522 * @param pszName The name of the thing.
1523 * @param iInstance The instance number.
1524 */
1525static void pdmR3NotifyAsyncAdd(PPDMNOTIFYASYNCSTATS pThis, const char *pszName, uint32_t iInstance)
1526{
1527 pThis->cAsync++;
1528 if (pThis->offList < sizeof(pThis->szList) - 4)
1529 pThis->offList += RTStrPrintf(&pThis->szList[pThis->offList], sizeof(pThis->szList) - pThis->offList,
1530 pThis->offList == 0 ? "%s/%u" : ", %s/%u",
1531 pszName, iInstance);
1532}
1533
1534
1535/**
1536 * Records the asynchronous completition of a reset, suspend or power off.
1537 *
1538 * @param pThis The asynchronous notifification stats.
1539 * @param pszDrvName The driver name.
1540 * @param iDrvInstance The driver instance number.
1541 * @param pszDevName The device or USB device name.
1542 * @param iDevInstance The device or USB device instance number.
1543 * @param iLun The LUN.
1544 */
1545static void pdmR3NotifyAsyncAddDrv(PPDMNOTIFYASYNCSTATS pThis, const char *pszDrvName, uint32_t iDrvInstance,
1546 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1547{
1548 pThis->cAsync++;
1549 if (pThis->offList < sizeof(pThis->szList) - 8)
1550 pThis->offList += RTStrPrintf(&pThis->szList[pThis->offList], sizeof(pThis->szList) - pThis->offList,
1551 pThis->offList == 0 ? "%s/%u/%u/%s/%u" : ", %s/%u/%u/%s/%u",
1552 pszDevName, iDevInstance, iLun, pszDrvName, iDrvInstance);
1553}
1554
1555
1556/**
1557 * Log the stats.
1558 *
1559 * @param pThis The asynchronous notifification stats.
1560 */
1561static void pdmR3NotifyAsyncLog(PPDMNOTIFYASYNCSTATS pThis)
1562{
1563 /*
1564 * Return if we shouldn't log at this point.
1565 * We log with an internval increasing from 0 sec to 60 sec.
1566 */
1567 if (!pThis->cAsync)
1568 return;
1569
1570 uint64_t cNsElapsed = RTTimeNanoTS() - pThis->uStartNsTs;
1571 if (cNsElapsed < pThis->cNsElapsedNextLog)
1572 return;
1573
1574 if (pThis->cNsElapsedNextLog == 0)
1575 pThis->cNsElapsedNextLog = RT_NS_1SEC;
1576 else if (pThis->cNsElapsedNextLog >= RT_NS_1MIN / 2)
1577 pThis->cNsElapsedNextLog = RT_NS_1MIN;
1578 else
1579 pThis->cNsElapsedNextLog *= 2;
1580
1581 /*
1582 * Do the logging.
1583 */
1584 LogRel(("%s: after %5llu ms, %u loops: %u async tasks - %s\n",
1585 pThis->pszOp, cNsElapsed / RT_NS_1MS, pThis->cLoops, pThis->cAsync, pThis->szList));
1586}
1587
1588
1589/**
1590 * Wait for events and process pending requests.
1591 *
1592 * @param pThis The asynchronous notifification stats.
1593 * @param pVM The cross context VM structure.
1594 */
1595static void pdmR3NotifyAsyncWaitAndProcessRequests(PPDMNOTIFYASYNCSTATS pThis, PVM pVM)
1596{
1597 VM_ASSERT_EMT0(pVM);
1598 int rc = VMR3AsyncPdmNotificationWaitU(&pVM->pUVM->aCpus[0]);
1599 AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc - %s - %s\n", rc, pThis->pszOp, pThis->szList));
1600
1601 rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY, true /*fPriorityOnly*/);
1602 AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc - %s - %s\n", rc, pThis->pszOp, pThis->szList));
1603 rc = VMR3ReqProcessU(pVM->pUVM, 0/*idDstCpu*/, true /*fPriorityOnly*/);
1604 AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc - %s - %s\n", rc, pThis->pszOp, pThis->szList));
1605}
1606
1607
1608/**
1609 * Worker for PDMR3Reset that deals with one driver.
1610 *
1611 * @param pDrvIns The driver instance.
1612 * @param pAsync The structure for recording asynchronous
1613 * notification tasks.
1614 * @param pszDevName The parent device name.
1615 * @param iDevInstance The parent device instance number.
1616 * @param iLun The parent LUN number.
1617 */
1618DECLINLINE(bool) pdmR3ResetDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
1619 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1620{
1621 if (!pDrvIns->Internal.s.fVMReset)
1622 {
1623 pDrvIns->Internal.s.fVMReset = true;
1624 if (pDrvIns->pReg->pfnReset)
1625 {
1626 if (!pDrvIns->Internal.s.pfnAsyncNotify)
1627 {
1628 LogFlow(("PDMR3Reset: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1629 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1630 pDrvIns->pReg->pfnReset(pDrvIns);
1631 if (pDrvIns->Internal.s.pfnAsyncNotify)
1632 LogFlow(("PDMR3Reset: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1633 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1634 }
1635 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
1636 {
1637 LogFlow(("PDMR3Reset: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1638 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1639 pDrvIns->Internal.s.pfnAsyncNotify = NULL;
1640 }
1641 if (pDrvIns->Internal.s.pfnAsyncNotify)
1642 {
1643 pDrvIns->Internal.s.fVMReset = false;
1644 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance,
1645 pszDevName, iDevInstance, iLun);
1646 return false;
1647 }
1648 }
1649 }
1650 return true;
1651}
1652
1653
1654/**
1655 * Worker for PDMR3Reset that deals with one USB device instance.
1656 *
1657 * @param pUsbIns The USB device instance.
1658 * @param pAsync The structure for recording asynchronous
1659 * notification tasks.
1660 */
1661DECLINLINE(void) pdmR3ResetUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
1662{
1663 if (!pUsbIns->Internal.s.fVMReset)
1664 {
1665 pUsbIns->Internal.s.fVMReset = true;
1666 if (pUsbIns->pReg->pfnVMReset)
1667 {
1668 if (!pUsbIns->Internal.s.pfnAsyncNotify)
1669 {
1670 LogFlow(("PDMR3Reset: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1671 pUsbIns->pReg->pfnVMReset(pUsbIns);
1672 if (pUsbIns->Internal.s.pfnAsyncNotify)
1673 LogFlow(("PDMR3Reset: Async notification started - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1674 }
1675 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
1676 {
1677 LogFlow(("PDMR3Reset: Async notification completed - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1678 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
1679 }
1680 if (pUsbIns->Internal.s.pfnAsyncNotify)
1681 {
1682 pUsbIns->Internal.s.fVMReset = false;
1683 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
1684 }
1685 }
1686 }
1687}
1688
1689
1690/**
1691 * Worker for PDMR3Reset that deals with one device instance.
1692 *
1693 * @param pVM The cross context VM structure.
1694 * @param pDevIns The device instance.
1695 * @param pAsync The structure for recording asynchronous notification tasks.
1696 */
1697DECLINLINE(void) pdmR3ResetDev(PVM pVM, PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
1698{
1699 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_RESET))
1700 {
1701 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_RESET;
1702 if (pDevIns->pReg->pfnReset)
1703 {
1704 uint64_t cNsElapsed = RTTimeNanoTS();
1705 PDMCritSectEnter(pVM, pDevIns->pCritSectRoR3, VERR_IGNORED);
1706
1707 if (!pDevIns->Internal.s.pfnAsyncNotify)
1708 {
1709 LogFlow(("PDMR3Reset: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1710 pDevIns->pReg->pfnReset(pDevIns);
1711 if (pDevIns->Internal.s.pfnAsyncNotify)
1712 LogFlow(("PDMR3Reset: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1713 }
1714 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
1715 {
1716 LogFlow(("PDMR3Reset: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
1717 pDevIns->Internal.s.pfnAsyncNotify = NULL;
1718 }
1719 if (pDevIns->Internal.s.pfnAsyncNotify)
1720 {
1721 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_RESET;
1722 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
1723 }
1724
1725 PDMCritSectLeave(pVM, pDevIns->pCritSectRoR3);
1726 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1727 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1728 LogRel(("PDMR3Reset: Device '%s'/%d took %'llu ns to reset\n",
1729 pDevIns->pReg->szName, pDevIns->iInstance, cNsElapsed));
1730 }
1731 }
1732}
1733
1734
1735/**
1736 * Resets a virtual CPU.
1737 *
1738 * Used by PDMR3Reset and CPU hot plugging.
1739 *
1740 * @param pVCpu The cross context virtual CPU structure.
1741 */
1742VMMR3_INT_DECL(void) PDMR3ResetCpu(PVMCPU pVCpu)
1743{
1744#ifdef VBOX_VMM_TARGET_ARMV8
1745 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_IRQ);
1746 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_FIQ);
1747#elif defined(VBOX_VMM_TARGET_X86)
1748 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_APIC);
1749 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_PIC);
1750#else
1751# error "port me"
1752#endif
1753 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_NMI);
1754 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_SMI);
1755}
1756
1757
1758/**
1759 * This function will notify all the devices and their attached drivers about
1760 * the VM now being reset.
1761 *
1762 * @param pVM The cross context VM structure.
1763 */
1764VMMR3_INT_DECL(void) PDMR3Reset(PVM pVM)
1765{
1766 LogFlow(("PDMR3Reset:\n"));
1767
1768 /*
1769 * Clear all the reset flags.
1770 */
1771 RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
1772 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1773 {
1774 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_RESET;
1775 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1776 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1777 pDrvIns->Internal.s.fVMReset = false;
1778 }
1779#ifdef VBOX_WITH_USB
1780 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1781 {
1782 pUsbIns->Internal.s.fVMReset = false;
1783 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1784 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1785 pDrvIns->Internal.s.fVMReset = false;
1786 }
1787#endif
1788 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
1789
1790 /*
1791 * The outer loop repeats until there are no more async requests.
1792 */
1793 PDMNOTIFYASYNCSTATS Async;
1794 pdmR3NotifyAsyncInit(&Async, "PDMR3Reset");
1795 for (;;)
1796 {
1797 pdmR3NotifyAsyncBeginLoop(&Async);
1798
1799 /*
1800 * Iterate thru the device instances and USB device instances,
1801 * processing the drivers associated with those.
1802 */
1803 /** @todo locking */
1804 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1805 {
1806 unsigned const cAsyncStart = Async.cAsync;
1807
1808 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_RESET_NOTIFICATION)
1809 pdmR3ResetDev(pVM, pDevIns, &Async);
1810
1811 if (Async.cAsync == cAsyncStart)
1812 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
1813 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1814 if (!pdmR3ResetDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
1815 break;
1816
1817 if ( Async.cAsync == cAsyncStart
1818 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_RESET_NOTIFICATION))
1819 pdmR3ResetDev(pVM, pDevIns, &Async);
1820 }
1821
1822#ifdef VBOX_WITH_USB
1823 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1824 {
1825 unsigned const cAsyncStart = Async.cAsync;
1826
1827 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1828 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1829 if (!pdmR3ResetDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
1830 break;
1831
1832 if (Async.cAsync == cAsyncStart)
1833 pdmR3ResetUsb(pUsbIns, &Async);
1834 }
1835#endif
1836 if (!Async.cAsync)
1837 break;
1838 pdmR3NotifyAsyncLog(&Async);
1839 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
1840 }
1841
1842 /*
1843 * Clear all pending interrupts and DMA operations.
1844 */
1845 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
1846 PDMR3ResetCpu(pVM->apCpusR3[idCpu]);
1847 VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
1848
1849 LogFlow(("PDMR3Reset: returns void\n"));
1850}
1851
1852
1853/**
1854 * This function will tell all the devices to setup up their memory structures
1855 * after VM construction and after VM reset.
1856 *
1857 * @param pVM The cross context VM structure.
1858 * @param fAtReset Indicates the context, after reset if @c true or after
1859 * construction if @c false.
1860 */
1861VMMR3_INT_DECL(void) PDMR3MemSetup(PVM pVM, bool fAtReset)
1862{
1863 LogFlow(("PDMR3MemSetup: fAtReset=%RTbool\n", fAtReset));
1864 PDMDEVMEMSETUPCTX const enmCtx = fAtReset ? PDMDEVMEMSETUPCTX_AFTER_RESET : PDMDEVMEMSETUPCTX_AFTER_CONSTRUCTION;
1865
1866 /*
1867 * Iterate thru the device instances and work the callback.
1868 */
1869 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1870 if (pDevIns->pReg->pfnMemSetup)
1871 {
1872 PDMCritSectEnter(pVM, pDevIns->pCritSectRoR3, VERR_IGNORED);
1873 pDevIns->pReg->pfnMemSetup(pDevIns, enmCtx);
1874 PDMCritSectLeave(pVM, pDevIns->pCritSectRoR3);
1875 }
1876
1877 LogFlow(("PDMR3MemSetup: returns void\n"));
1878}
1879
1880
1881/**
1882 * Retrieves and resets the info left behind by PDMDevHlpVMReset.
1883 *
1884 * @returns True if hard reset, false if soft reset.
1885 * @param pVM The cross context VM structure.
1886 * @param fOverride If non-zero, the override flags will be used instead
1887 * of the reset flags kept by PDM. (For triple faults.)
1888 * @param pfResetFlags Where to return the reset flags (PDMVMRESET_F_XXX).
1889 * @thread EMT
1890 */
1891VMMR3_INT_DECL(bool) PDMR3GetResetInfo(PVM pVM, uint32_t fOverride, uint32_t *pfResetFlags)
1892{
1893 VM_ASSERT_EMT(pVM);
1894
1895 /*
1896 * Get the reset flags.
1897 */
1898 uint32_t fResetFlags;
1899 fResetFlags = ASMAtomicXchgU32(&pVM->pdm.s.fResetFlags, 0);
1900 if (fOverride)
1901 fResetFlags = fOverride;
1902 *pfResetFlags = fResetFlags;
1903
1904 /*
1905 * To try avoid trouble, we never ever do soft/warm resets on SMP systems
1906 * with more than CPU #0 active. However, if only one CPU is active we
1907 * will ask the firmware what it wants us to do (because the firmware may
1908 * depend on the VMM doing a lot of what is normally its responsibility,
1909 * like clearing memory).
1910 */
1911 bool fOtherCpusActive = false;
1912 VMCPUID idCpu = pVM->cCpus;
1913 while (idCpu-- > 1)
1914 {
1915 EMSTATE enmState = EMGetState(pVM->apCpusR3[idCpu]);
1916 if ( enmState != EMSTATE_WAIT_SIPI
1917 && enmState != EMSTATE_NONE)
1918 {
1919 fOtherCpusActive = true;
1920 break;
1921 }
1922 }
1923
1924 bool fHardReset = fOtherCpusActive
1925 || (fResetFlags & PDMVMRESET_F_SRC_MASK) < PDMVMRESET_F_LAST_ALWAYS_HARD
1926 || !pVM->pdm.s.pFirmware
1927 || pVM->pdm.s.pFirmware->Reg.pfnIsHardReset(pVM->pdm.s.pFirmware->pDevIns, fResetFlags);
1928
1929 Log(("PDMR3GetResetInfo: returns fHardReset=%RTbool fResetFlags=%#x\n", fHardReset, fResetFlags));
1930 return fHardReset;
1931}
1932
1933
1934/**
1935 * Performs a soft reset of devices.
1936 *
1937 * @param pVM The cross context VM structure.
1938 * @param fResetFlags PDMVMRESET_F_XXX.
1939 */
1940VMMR3_INT_DECL(void) PDMR3SoftReset(PVM pVM, uint32_t fResetFlags)
1941{
1942 LogFlow(("PDMR3SoftReset: fResetFlags=%#x\n", fResetFlags));
1943
1944 /*
1945 * Iterate thru the device instances and work the callback.
1946 */
1947 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
1948 if (pDevIns->pReg->pfnSoftReset)
1949 {
1950 PDMCritSectEnter(pVM, pDevIns->pCritSectRoR3, VERR_IGNORED);
1951 pDevIns->pReg->pfnSoftReset(pDevIns, fResetFlags);
1952 PDMCritSectLeave(pVM, pDevIns->pCritSectRoR3);
1953 }
1954
1955 LogFlow(("PDMR3SoftReset: returns void\n"));
1956}
1957
1958
1959/**
1960 * Worker for PDMR3Suspend that deals with one driver.
1961 *
1962 * @param pDrvIns The driver instance.
1963 * @param pAsync The structure for recording asynchronous
1964 * notification tasks.
1965 * @param pszDevName The parent device name.
1966 * @param iDevInstance The parent device instance number.
1967 * @param iLun The parent LUN number.
1968 */
1969DECLINLINE(bool) pdmR3SuspendDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
1970 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
1971{
1972 if (!pDrvIns->Internal.s.fVMSuspended)
1973 {
1974 pDrvIns->Internal.s.fVMSuspended = true;
1975 if (pDrvIns->pReg->pfnSuspend)
1976 {
1977 uint64_t cNsElapsed = RTTimeNanoTS();
1978
1979 if (!pDrvIns->Internal.s.pfnAsyncNotify)
1980 {
1981 LogFlow(("PDMR3Suspend: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1982 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1983 pDrvIns->pReg->pfnSuspend(pDrvIns);
1984 if (pDrvIns->Internal.s.pfnAsyncNotify)
1985 LogFlow(("PDMR3Suspend: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1986 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1987 }
1988 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
1989 {
1990 LogFlow(("PDMR3Suspend: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
1991 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
1992 pDrvIns->Internal.s.pfnAsyncNotify = NULL;
1993 }
1994
1995 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
1996 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
1997 LogRel(("PDMR3Suspend: Driver '%s'/%d on LUN#%d of device '%s'/%d took %'llu ns to suspend\n",
1998 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, cNsElapsed));
1999
2000 if (pDrvIns->Internal.s.pfnAsyncNotify)
2001 {
2002 pDrvIns->Internal.s.fVMSuspended = false;
2003 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance, pszDevName, iDevInstance, iLun);
2004 return false;
2005 }
2006 }
2007 }
2008 return true;
2009}
2010
2011
2012/**
2013 * Worker for PDMR3Suspend that deals with one USB device instance.
2014 *
2015 * @param pUsbIns The USB device instance.
2016 * @param pAsync The structure for recording asynchronous
2017 * notification tasks.
2018 */
2019DECLINLINE(void) pdmR3SuspendUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
2020{
2021 if (!pUsbIns->Internal.s.fVMSuspended)
2022 {
2023 pUsbIns->Internal.s.fVMSuspended = true;
2024 if (pUsbIns->pReg->pfnVMSuspend)
2025 {
2026 uint64_t cNsElapsed = RTTimeNanoTS();
2027
2028 if (!pUsbIns->Internal.s.pfnAsyncNotify)
2029 {
2030 LogFlow(("PDMR3Suspend: Notifying - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
2031 pUsbIns->pReg->pfnVMSuspend(pUsbIns);
2032 if (pUsbIns->Internal.s.pfnAsyncNotify)
2033 LogFlow(("PDMR3Suspend: Async notification started - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
2034 }
2035 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
2036 {
2037 LogFlow(("PDMR3Suspend: Async notification completed - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
2038 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
2039 }
2040 if (pUsbIns->Internal.s.pfnAsyncNotify)
2041 {
2042 pUsbIns->Internal.s.fVMSuspended = false;
2043 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
2044 }
2045
2046 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2047 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
2048 LogRel(("PDMR3Suspend: USB device '%s'/%d took %'llu ns to suspend\n",
2049 pUsbIns->pReg->szName, pUsbIns->iInstance, cNsElapsed));
2050 }
2051 }
2052}
2053
2054
2055/**
2056 * Worker for PDMR3Suspend that deals with one device instance.
2057 *
2058 * @param pVM The cross context VM structure.
2059 * @param pDevIns The device instance.
2060 * @param pAsync The structure for recording asynchronous notification tasks.
2061 */
2062DECLINLINE(void) pdmR3SuspendDev(PVM pVM, PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
2063{
2064 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED))
2065 {
2066 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_SUSPENDED;
2067 if (pDevIns->pReg->pfnSuspend)
2068 {
2069 uint64_t cNsElapsed = RTTimeNanoTS();
2070 PDMCritSectEnter(pVM, pDevIns->pCritSectRoR3, VERR_IGNORED);
2071
2072 if (!pDevIns->Internal.s.pfnAsyncNotify)
2073 {
2074 LogFlow(("PDMR3Suspend: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
2075 pDevIns->pReg->pfnSuspend(pDevIns);
2076 if (pDevIns->Internal.s.pfnAsyncNotify)
2077 LogFlow(("PDMR3Suspend: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
2078 }
2079 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
2080 {
2081 LogFlow(("PDMR3Suspend: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
2082 pDevIns->Internal.s.pfnAsyncNotify = NULL;
2083 }
2084 if (pDevIns->Internal.s.pfnAsyncNotify)
2085 {
2086 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
2087 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
2088 }
2089
2090 PDMCritSectLeave(pVM, pDevIns->pCritSectRoR3);
2091 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2092 if (cNsElapsed >= PDMSUSPEND_WARN_AT_NS)
2093 LogRel(("PDMR3Suspend: Device '%s'/%d took %'llu ns to suspend\n",
2094 pDevIns->pReg->szName, pDevIns->iInstance, cNsElapsed));
2095 }
2096 }
2097}
2098
2099
2100/**
2101 * This function will notify all the devices and their attached drivers about
2102 * the VM now being suspended.
2103 *
2104 * @param pVM The cross context VM structure.
2105 * @thread EMT(0)
2106 */
2107VMMR3_INT_DECL(void) PDMR3Suspend(PVM pVM)
2108{
2109 LogFlow(("PDMR3Suspend:\n"));
2110 VM_ASSERT_EMT0(pVM);
2111 uint64_t cNsElapsed = RTTimeNanoTS();
2112
2113 /*
2114 * The outer loop repeats until there are no more async requests.
2115 *
2116 * Note! We depend on the suspended indicators to be in the desired state
2117 * and we do not reset them before starting because this allows
2118 * PDMR3PowerOn and PDMR3Resume to use PDMR3Suspend for cleaning up
2119 * on failure.
2120 */
2121 PDMNOTIFYASYNCSTATS Async;
2122 pdmR3NotifyAsyncInit(&Async, "PDMR3Suspend");
2123 for (;;)
2124 {
2125 pdmR3NotifyAsyncBeginLoop(&Async);
2126
2127 /*
2128 * Iterate thru the device instances and USB device instances,
2129 * processing the drivers associated with those.
2130 *
2131 * The attached drivers are normally processed first. Some devices
2132 * (like DevAHCI) though needs to be notified before the drivers so
2133 * that it doesn't kick off any new requests after the drivers stopped
2134 * taking any. (DrvVD changes to read-only in this particular case.)
2135 */
2136 RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
2137 /** @todo locking */
2138 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
2139 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2140 {
2141 unsigned const cAsyncStart = Async.cAsync;
2142
2143 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION)
2144 pdmR3SuspendDev(pVM, pDevIns, &Async);
2145
2146 if (Async.cAsync == cAsyncStart)
2147 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2148 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2149 if (!pdmR3SuspendDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
2150 break;
2151
2152 if ( Async.cAsync == cAsyncStart
2153 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION))
2154 pdmR3SuspendDev(pVM, pDevIns, &Async);
2155 }
2156
2157#ifdef VBOX_WITH_USB
2158 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2159 {
2160 unsigned const cAsyncStart = Async.cAsync;
2161
2162 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2163 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2164 if (!pdmR3SuspendDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
2165 break;
2166
2167 if (Async.cAsync == cAsyncStart)
2168 pdmR3SuspendUsb(pUsbIns, &Async);
2169 }
2170#endif
2171 if (!Async.cAsync)
2172 break;
2173 pdmR3NotifyAsyncLog(&Async);
2174 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
2175 }
2176
2177 /*
2178 * Suspend all threads.
2179 */
2180 pdmR3ThreadSuspendAll(pVM);
2181
2182 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2183 LogRel(("PDMR3Suspend: %'llu ns run time\n", cNsElapsed));
2184}
2185
2186
2187/**
2188 * Worker for PDMR3Resume that deals with one driver.
2189 *
2190 * @param pDrvIns The driver instance.
2191 * @param pszDevName The parent device name.
2192 * @param iDevInstance The parent device instance number.
2193 * @param iLun The parent LUN number.
2194 */
2195DECLINLINE(int) pdmR3ResumeDrv(PPDMDRVINS pDrvIns, const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
2196{
2197 Assert(pDrvIns->Internal.s.fVMSuspended);
2198 if (pDrvIns->pReg->pfnResume)
2199 {
2200 LogFlow(("PDMR3Resume: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
2201 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
2202 int rc = VINF_SUCCESS; pDrvIns->pReg->pfnResume(pDrvIns);
2203 if (RT_FAILURE(rc))
2204 {
2205 LogRel(("PDMR3Resume: Driver '%s'/%d on LUN#%d of device '%s'/%d -> %Rrc\n",
2206 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, rc));
2207 return rc;
2208 }
2209 }
2210 pDrvIns->Internal.s.fVMSuspended = false;
2211 return VINF_SUCCESS;
2212}
2213
2214
2215/**
2216 * Worker for PDMR3Resume that deals with one USB device instance.
2217 *
2218 * @returns VBox status code.
2219 * @param pUsbIns The USB device instance.
2220 */
2221DECLINLINE(int) pdmR3ResumeUsb(PPDMUSBINS pUsbIns)
2222{
2223 if (pUsbIns->Internal.s.fVMSuspended)
2224 {
2225 if (pUsbIns->pReg->pfnVMResume)
2226 {
2227 LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
2228 int rc = VINF_SUCCESS; pUsbIns->pReg->pfnVMResume(pUsbIns);
2229 if (RT_FAILURE(rc))
2230 {
2231 LogRel(("PDMR3Resume: Device '%s'/%d -> %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
2232 return rc;
2233 }
2234 }
2235 pUsbIns->Internal.s.fVMSuspended = false;
2236 }
2237 return VINF_SUCCESS;
2238}
2239
2240
2241/**
2242 * Worker for PDMR3Resume that deals with one device instance.
2243 *
2244 * @returns VBox status code.
2245 * @param pVM The cross context VM structure.
2246 * @param pDevIns The device instance.
2247 */
2248DECLINLINE(int) pdmR3ResumeDev(PVM pVM, PPDMDEVINS pDevIns)
2249{
2250 Assert(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED);
2251 if (pDevIns->pReg->pfnResume)
2252 {
2253 LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
2254 PDMCritSectEnter(pVM, pDevIns->pCritSectRoR3, VERR_IGNORED);
2255 int rc = VINF_SUCCESS; pDevIns->pReg->pfnResume(pDevIns);
2256 PDMCritSectLeave(pVM, pDevIns->pCritSectRoR3);
2257 if (RT_FAILURE(rc))
2258 {
2259 LogRel(("PDMR3Resume: Device '%s'/%d -> %Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
2260 return rc;
2261 }
2262 }
2263 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
2264 return VINF_SUCCESS;
2265}
2266
2267
2268/**
2269 * This function will notify all the devices and their
2270 * attached drivers about the VM now being resumed.
2271 *
2272 * @param pVM The cross context VM structure.
2273 */
2274VMMR3_INT_DECL(void) PDMR3Resume(PVM pVM)
2275{
2276 LogFlow(("PDMR3Resume:\n"));
2277
2278 /*
2279 * Iterate thru the device instances and USB device instances,
2280 * processing the drivers associated with those.
2281 */
2282 RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
2283 /** @todo locking */
2284 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
2285 int rc = VINF_SUCCESS;
2286 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns && RT_SUCCESS(rc); pDevIns = pDevIns->Internal.s.pNextR3)
2287 {
2288 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
2289 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
2290 rc = pdmR3ResumeDrv(pDrvIns, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun);
2291 if (RT_SUCCESS(rc))
2292 rc = pdmR3ResumeDev(pVM, pDevIns);
2293 }
2294
2295#ifdef VBOX_WITH_USB
2296 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns && RT_SUCCESS(rc); pUsbIns = pUsbIns->Internal.s.pNext)
2297 {
2298 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
2299 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
2300 rc = pdmR3ResumeDrv(pDrvIns, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun);
2301 if (RT_SUCCESS(rc))
2302 rc = pdmR3ResumeUsb(pUsbIns);
2303 }
2304#endif
2305
2306 /*
2307 * Resume all threads.
2308 */
2309 if (RT_SUCCESS(rc))
2310 pdmR3ThreadResumeAll(pVM);
2311
2312 /*
2313 * Resume the block cache.
2314 */
2315 if (RT_SUCCESS(rc))
2316 pdmR3BlkCacheResume(pVM);
2317
2318 /*
2319 * On failure, clean up via PDMR3Suspend.
2320 */
2321 if (RT_FAILURE(rc))
2322 PDMR3Suspend(pVM);
2323
2324 LogFlow(("PDMR3Resume: returns %Rrc\n", rc));
2325 return /*rc*/;
2326}
2327
2328
2329/**
2330 * Worker for PDMR3PowerOff that deals with one driver.
2331 *
2332 * @param pDrvIns The driver instance.
2333 * @param pAsync The structure for recording asynchronous
2334 * notification tasks.
2335 * @param pszDevName The parent device name.
2336 * @param iDevInstance The parent device instance number.
2337 * @param iLun The parent LUN number.
2338 */
2339DECLINLINE(bool) pdmR3PowerOffDrv(PPDMDRVINS pDrvIns, PPDMNOTIFYASYNCSTATS pAsync,
2340 const char *pszDevName, uint32_t iDevInstance, uint32_t iLun)
2341{
2342 if (!pDrvIns->Internal.s.fVMSuspended)
2343 {
2344 pDrvIns->Internal.s.fVMSuspended = true;
2345 if (pDrvIns->pReg->pfnPowerOff)
2346 {
2347 uint64_t cNsElapsed = RTTimeNanoTS();
2348
2349 if (!pDrvIns->Internal.s.pfnAsyncNotify)
2350 {
2351 LogFlow(("PDMR3PowerOff: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
2352 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
2353 pDrvIns->pReg->pfnPowerOff(pDrvIns);
2354 if (pDrvIns->Internal.s.pfnAsyncNotify)
2355 LogFlow(("PDMR3PowerOff: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
2356 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
2357 }
2358 else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
2359 {
2360 LogFlow(("PDMR3PowerOff: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
2361 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance));
2362 pDrvIns->Internal.s.pfnAsyncNotify = NULL;
2363 }
2364
2365 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2366 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
2367 LogRel(("PDMR3PowerOff: Driver '%s'/%d on LUN#%d of device '%s'/%d took %'llu ns to power off\n",
2368 pDrvIns->pReg->szName, pDrvIns->iInstance, iLun, pszDevName, iDevInstance, cNsElapsed));
2369
2370 if (pDrvIns->Internal.s.pfnAsyncNotify)
2371 {
2372 pDrvIns->Internal.s.fVMSuspended = false;
2373 pdmR3NotifyAsyncAddDrv(pAsync, pDrvIns->Internal.s.pDrv->pReg->szName, pDrvIns->iInstance,
2374 pszDevName, iDevInstance, iLun);
2375 return false;
2376 }
2377 }
2378 }
2379 return true;
2380}
2381
2382
2383/**
2384 * Worker for PDMR3PowerOff that deals with one USB device instance.
2385 *
2386 * @param pUsbIns The USB device instance.
2387 * @param pAsync The structure for recording asynchronous
2388 * notification tasks.
2389 */
2390DECLINLINE(void) pdmR3PowerOffUsb(PPDMUSBINS pUsbIns, PPDMNOTIFYASYNCSTATS pAsync)
2391{
2392 if (!pUsbIns->Internal.s.fVMSuspended)
2393 {
2394 pUsbIns->Internal.s.fVMSuspended = true;
2395 if (pUsbIns->pReg->pfnVMPowerOff)
2396 {
2397 uint64_t cNsElapsed = RTTimeNanoTS();
2398
2399 if (!pUsbIns->Internal.s.pfnAsyncNotify)
2400 {
2401 LogFlow(("PDMR3PowerOff: Notifying - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
2402 pUsbIns->pReg->pfnVMPowerOff(pUsbIns);
2403 if (pUsbIns->Internal.s.pfnAsyncNotify)
2404 LogFlow(("PDMR3PowerOff: Async notification started - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
2405 }
2406 else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
2407 {
2408 LogFlow(("PDMR3PowerOff: Async notification completed - USB device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
2409 pUsbIns->Internal.s.pfnAsyncNotify = NULL;
2410 }
2411 if (pUsbIns->Internal.s.pfnAsyncNotify)
2412 {
2413 pUsbIns->Internal.s.fVMSuspended = false;
2414 pdmR3NotifyAsyncAdd(pAsync, pUsbIns->Internal.s.pUsbDev->pReg->szName, pUsbIns->iInstance);
2415 }
2416
2417 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2418 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
2419 LogRel(("PDMR3PowerOff: USB device '%s'/%d took %'llu ns to power off\n",
2420 pUsbIns->pReg->szName, pUsbIns->iInstance, cNsElapsed));
2421
2422 }
2423 }
2424}
2425
2426
2427/**
2428 * Worker for PDMR3PowerOff that deals with one device instance.
2429 *
2430 * @param pVM The cross context VM structure.
2431 * @param pDevIns The device instance.
2432 * @param pAsync The structure for recording asynchronous notification tasks.
2433 */
2434DECLINLINE(void) pdmR3PowerOffDev(PVM pVM, PPDMDEVINS pDevIns, PPDMNOTIFYASYNCSTATS pAsync)
2435{
2436 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED))
2437 {
2438 pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_SUSPENDED;
2439 if (pDevIns->pReg->pfnPowerOff)
2440 {
2441 uint64_t cNsElapsed = RTTimeNanoTS();
2442 PDMCritSectEnter(pVM, pDevIns->pCritSectRoR3, VERR_IGNORED);
2443
2444 if (!pDevIns->Internal.s.pfnAsyncNotify)
2445 {
2446 LogFlow(("PDMR3PowerOff: Notifying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
2447 pDevIns->pReg->pfnPowerOff(pDevIns);
2448 if (pDevIns->Internal.s.pfnAsyncNotify)
2449 LogFlow(("PDMR3PowerOff: Async notification started - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
2450 }
2451 else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
2452 {
2453 LogFlow(("PDMR3PowerOff: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
2454 pDevIns->Internal.s.pfnAsyncNotify = NULL;
2455 }
2456 if (pDevIns->Internal.s.pfnAsyncNotify)
2457 {
2458 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
2459 pdmR3NotifyAsyncAdd(pAsync, pDevIns->Internal.s.pDevR3->pReg->szName, pDevIns->iInstance);
2460 }
2461
2462 PDMCritSectLeave(pVM, pDevIns->pCritSectRoR3);
2463 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2464 if (cNsElapsed >= PDMPOWEROFF_WARN_AT_NS)
2465 LogFlow(("PDMR3PowerOff: Device '%s'/%d took %'llu ns to power off\n",
2466 pDevIns->pReg->szName, pDevIns->iInstance, cNsElapsed));
2467 }
2468 }
2469}
2470
2471
2472/**
2473 * This function will notify all the devices and their
2474 * attached drivers about the VM being powered off.
2475 *
2476 * @param pVM The cross context VM structure.
2477 */
2478VMMR3DECL(void) PDMR3PowerOff(PVM pVM)
2479{
2480 LogFlow(("PDMR3PowerOff:\n"));
2481 uint64_t cNsElapsed = RTTimeNanoTS();
2482
2483 /*
2484 * Clear the suspended flags on all devices and drivers first because they
2485 * might have been set during a suspend but the power off callbacks should
2486 * be called in any case.
2487 */
2488 RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
2489 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2490 {
2491 pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
2492
2493 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2494 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2495 pDrvIns->Internal.s.fVMSuspended = false;
2496 }
2497
2498#ifdef VBOX_WITH_USB
2499 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2500 {
2501 pUsbIns->Internal.s.fVMSuspended = false;
2502
2503 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2504 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2505 pDrvIns->Internal.s.fVMSuspended = false;
2506 }
2507#endif
2508 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
2509
2510 /*
2511 * The outer loop repeats until there are no more async requests.
2512 */
2513 PDMNOTIFYASYNCSTATS Async;
2514 pdmR3NotifyAsyncInit(&Async, "PDMR3PowerOff");
2515 for (;;)
2516 {
2517 pdmR3NotifyAsyncBeginLoop(&Async);
2518
2519 /*
2520 * Iterate thru the device instances and USB device instances,
2521 * processing the drivers associated with those.
2522 *
2523 * The attached drivers are normally processed first. Some devices
2524 * (like DevAHCI) though needs to be notified before the drivers so
2525 * that it doesn't kick off any new requests after the drivers stopped
2526 * taking any. (DrvVD changes to read-only in this particular case.)
2527 */
2528 /** @todo locking */
2529 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2530 {
2531 unsigned const cAsyncStart = Async.cAsync;
2532
2533 if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION)
2534 pdmR3PowerOffDev(pVM, pDevIns, &Async);
2535
2536 if (Async.cAsync == cAsyncStart)
2537 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2538 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2539 if (!pdmR3PowerOffDrv(pDrvIns, &Async, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun))
2540 break;
2541
2542 if ( Async.cAsync == cAsyncStart
2543 && !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION))
2544 pdmR3PowerOffDev(pVM, pDevIns, &Async);
2545 }
2546
2547#ifdef VBOX_WITH_USB
2548 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2549 {
2550 unsigned const cAsyncStart = Async.cAsync;
2551
2552 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2553 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2554 if (!pdmR3PowerOffDrv(pDrvIns, &Async, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun))
2555 break;
2556
2557 if (Async.cAsync == cAsyncStart)
2558 pdmR3PowerOffUsb(pUsbIns, &Async);
2559 }
2560#endif
2561 if (!Async.cAsync)
2562 break;
2563 pdmR3NotifyAsyncLog(&Async);
2564 pdmR3NotifyAsyncWaitAndProcessRequests(&Async, pVM);
2565 }
2566
2567 /*
2568 * Suspend all threads.
2569 */
2570 pdmR3ThreadSuspendAll(pVM);
2571
2572 cNsElapsed = RTTimeNanoTS() - cNsElapsed;
2573 LogRel(("PDMR3PowerOff: %'llu ns run time\n", cNsElapsed));
2574}
2575
2576
2577/**
2578 * Queries the base interface of a device instance.
2579 *
2580 * The caller can use this to query other interfaces the device implements
2581 * and use them to talk to the device.
2582 *
2583 * @returns VBox status code.
2584 * @param pUVM The user mode VM handle.
2585 * @param pszDevice Device name.
2586 * @param iInstance Device instance.
2587 * @param ppBase Where to store the pointer to the base device interface on success.
2588 * @remark We're not doing any locking ATM, so don't try call this at times when the
2589 * device chain is known to be updated.
2590 */
2591VMMR3DECL(int) PDMR3QueryDevice(PUVM pUVM, const char *pszDevice, unsigned iInstance, PPDMIBASE *ppBase)
2592{
2593 LogFlow(("PDMR3DeviceQuery: pszDevice=%p:{%s} iInstance=%u ppBase=%p\n", pszDevice, pszDevice, iInstance, ppBase));
2594 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2595 PVM const pVM = pUVM->pVM;
2596 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
2597
2598 /*
2599 * Iterate registered devices looking for the device.
2600 */
2601 size_t const cchDevice = strlen(pszDevice);
2602 RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
2603 for (PPDMDEV pDev = pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
2604 {
2605 if ( pDev->cchName == cchDevice
2606 && !memcmp(pDev->pReg->szName, pszDevice, cchDevice))
2607 {
2608 /*
2609 * Iterate device instances.
2610 */
2611 for (PPDMDEVINS pDevIns = pDev->pInstances; pDevIns; pDevIns = pDevIns->Internal.s.pPerDeviceNextR3)
2612 {
2613 if (pDevIns->iInstance == iInstance)
2614 {
2615 if (pDevIns->IBase.pfnQueryInterface)
2616 {
2617 *ppBase = &pDevIns->IBase;
2618 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
2619 LogFlow(("PDMR3DeviceQuery: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
2620 return VINF_SUCCESS;
2621 }
2622
2623 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
2624 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NO_IBASE\n"));
2625 return VERR_PDM_DEVICE_INSTANCE_NO_IBASE;
2626 }
2627 }
2628
2629 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
2630 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NOT_FOUND\n"));
2631 return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND;
2632 }
2633 }
2634
2635 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
2636 LogFlow(("PDMR3QueryDevice: returns VERR_PDM_DEVICE_NOT_FOUND\n"));
2637 return VERR_PDM_DEVICE_NOT_FOUND;
2638}
2639
2640
2641/**
2642 * Queries the base interface of a device LUN.
2643 *
2644 * This differs from PDMR3QueryLun by that it returns the interface on the
2645 * device and not the top level driver.
2646 *
2647 * @returns VBox status code.
2648 * @param pUVM The user mode VM handle.
2649 * @param pszDevice Device name.
2650 * @param iInstance Device instance.
2651 * @param iLun The Logical Unit to obtain the interface of.
2652 * @param ppBase Where to store the base interface pointer.
2653 * @remark We're not doing any locking ATM, so don't try call this at times when the
2654 * device chain is known to be updated.
2655 */
2656VMMR3DECL(int) PDMR3QueryDeviceLun(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
2657{
2658 LogFlow(("PDMR3QueryDeviceLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
2659 pszDevice, pszDevice, iInstance, iLun, ppBase));
2660 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2661 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2662
2663 /*
2664 * Find the LUN.
2665 */
2666 PPDMLUN pLun;
2667 int rc = pdmR3DevFindLun(pUVM->pVM, pszDevice, iInstance, iLun, &pLun);
2668 if (RT_SUCCESS(rc))
2669 {
2670 *ppBase = pLun->pBase;
2671 LogFlow(("PDMR3QueryDeviceLun: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
2672 return VINF_SUCCESS;
2673 }
2674 LogFlow(("PDMR3QueryDeviceLun: returns %Rrc\n", rc));
2675 return rc;
2676}
2677
2678
2679/**
2680 * Query the interface of the top level driver on a LUN.
2681 *
2682 * @returns VBox status code.
2683 * @param pUVM The user mode VM handle.
2684 * @param pszDevice Device name.
2685 * @param iInstance Device instance.
2686 * @param iLun The Logical Unit to obtain the interface of.
2687 * @param ppBase Where to store the base interface pointer.
2688 * @remark We're not doing any locking ATM, so don't try call this at times when the
2689 * device chain is known to be updated.
2690 */
2691VMMR3DECL(int) PDMR3QueryLun(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
2692{
2693 LogFlow(("PDMR3QueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
2694 pszDevice, pszDevice, iInstance, iLun, ppBase));
2695 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2696 PVM pVM = pUVM->pVM;
2697 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
2698
2699 /*
2700 * Find the LUN.
2701 */
2702 RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
2703 PPDMLUN pLun;
2704 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
2705 if (RT_SUCCESS(rc))
2706 {
2707 if (pLun->pTop)
2708 {
2709 *ppBase = &pLun->pTop->IBase;
2710 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
2711 LogFlow(("PDMR3QueryLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
2712 return VINF_SUCCESS;
2713 }
2714 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
2715 }
2716 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
2717 LogFlow(("PDMR3QueryLun: returns %Rrc\n", rc));
2718 return rc;
2719}
2720
2721
2722/**
2723 * Query the interface of a named driver on a LUN.
2724 *
2725 * If the driver appears more than once in the driver chain, the first instance
2726 * is returned.
2727 *
2728 * @returns VBox status code.
2729 * @param pUVM The user mode VM handle.
2730 * @param pszDevice Device name.
2731 * @param iInstance Device instance.
2732 * @param iLun The Logical Unit to obtain the interface of.
2733 * @param pszDriver The driver name.
2734 * @param ppBase Where to store the base interface pointer.
2735 *
2736 * @remark We're not doing any locking ATM, so don't try call this at times when the
2737 * device chain is known to be updated.
2738 */
2739VMMR3DECL(int) PDMR3QueryDriverOnLun(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, const char *pszDriver, PPPDMIBASE ppBase)
2740{
2741 LogFlow(("PDMR3QueryDriverOnLun: pszDevice=%p:{%s} iInstance=%u iLun=%u pszDriver=%p:{%s} ppBase=%p\n",
2742 pszDevice, pszDevice, iInstance, iLun, pszDriver, pszDriver, ppBase));
2743 *ppBase = NULL;
2744 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2745 PVM const pVM = pUVM->pVM;
2746 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
2747
2748 /*
2749 * Find the LUN.
2750 */
2751 RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
2752 PPDMLUN pLun;
2753 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
2754 if (RT_SUCCESS(rc))
2755 {
2756 if (pLun->pTop)
2757 {
2758 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2759 if (!strcmp(pDrvIns->pReg->szName, pszDriver))
2760 {
2761 *ppBase = &pDrvIns->IBase;
2762 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
2763 LogFlow(("PDMR3QueryDriverOnLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
2764 return VINF_SUCCESS;
2765 }
2766 rc = VERR_PDM_DRIVER_NOT_FOUND;
2767 }
2768 else
2769 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
2770 }
2771 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
2772 LogFlow(("PDMR3QueryDriverOnLun: returns %Rrc\n", rc));
2773 return rc;
2774}
2775
2776
2777/**
2778 * Executes pending DMA transfers.
2779 * Forced Action handler.
2780 *
2781 * @param pVM The cross context VM structure.
2782 */
2783VMMR3DECL(void) PDMR3DmaRun(PVM pVM)
2784{
2785 /** @todo this is bad in SMP... */
2786 /* Note! Not really SMP safe; restrict it to VCPU 0. */
2787 if (VMMGetCpuId(pVM) != 0)
2788 return;
2789
2790 if (VM_FF_TEST_AND_CLEAR(pVM, VM_FF_PDM_DMA))
2791 {
2792 PPDMDMAC const pDmac = pVM->pdm.s.pDmac;
2793 if (pDmac)
2794 {
2795 bool const fMore = pDmac->Reg.pfnRun(pDmac->pDevIns);
2796 if (fMore)
2797 VM_FF_SET(pVM, VM_FF_PDM_DMA);
2798 }
2799 }
2800}
2801
2802
2803/**
2804 * Allocates memory from the VMM device heap.
2805 *
2806 * @returns VBox status code.
2807 * @param pVM The cross context VM structure.
2808 * @param cbSize Allocation size.
2809 * @param pfnNotify Mapping/unmapping notification callback.
2810 * @param ppv Ring-3 pointer. (out)
2811 */
2812VMMR3_INT_DECL(int) PDMR3VmmDevHeapAlloc(PVM pVM, size_t cbSize, PFNPDMVMMDEVHEAPNOTIFY pfnNotify, RTR3PTR *ppv)
2813{
2814#ifdef DEBUG_bird
2815 if (!cbSize || cbSize > pVM->pdm.s.cbVMMDevHeapLeft)
2816 return VERR_NO_MEMORY;
2817#else
2818 AssertReturn(cbSize && cbSize <= pVM->pdm.s.cbVMMDevHeapLeft, VERR_NO_MEMORY);
2819#endif
2820
2821 Log(("PDMR3VMMDevHeapAlloc: %#zx\n", cbSize));
2822
2823 /** @todo Not a real heap as there's currently only one user. */
2824 *ppv = pVM->pdm.s.pvVMMDevHeap;
2825 pVM->pdm.s.cbVMMDevHeapLeft = 0;
2826 pVM->pdm.s.pfnVMMDevHeapNotify = pfnNotify;
2827 return VINF_SUCCESS;
2828}
2829
2830
2831/**
2832 * Frees memory from the VMM device heap
2833 *
2834 * @returns VBox status code.
2835 * @param pVM The cross context VM structure.
2836 * @param pv Ring-3 pointer.
2837 */
2838VMMR3_INT_DECL(int) PDMR3VmmDevHeapFree(PVM pVM, RTR3PTR pv)
2839{
2840 Log(("PDMR3VmmDevHeapFree: %RHv\n", pv)); RT_NOREF_PV(pv);
2841
2842 /** @todo not a real heap as there's currently only one user. */
2843 pVM->pdm.s.cbVMMDevHeapLeft = pVM->pdm.s.cbVMMDevHeap;
2844 pVM->pdm.s.pfnVMMDevHeapNotify = NULL;
2845 return VINF_SUCCESS;
2846}
2847
2848
2849/**
2850 * Worker for DBGFR3TraceConfig that checks if the given tracing group name
2851 * matches a device or driver name and applies the tracing config change.
2852 *
2853 * @returns VINF_SUCCESS or VERR_NOT_FOUND.
2854 * @param pVM The cross context VM structure.
2855 * @param pszName The tracing config group name. This is NULL if
2856 * the operation applies to every device and
2857 * driver.
2858 * @param cchName The length to match.
2859 * @param fEnable Whether to enable or disable the corresponding
2860 * trace points.
2861 * @param fApply Whether to actually apply the changes or just do
2862 * existence checks.
2863 */
2864VMMR3_INT_DECL(int) PDMR3TracingConfig(PVM pVM, const char *pszName, size_t cchName, bool fEnable, bool fApply)
2865{
2866 /** @todo This code is potentially racing driver attaching and detaching. */
2867
2868 /*
2869 * Applies to all.
2870 */
2871 if (pszName == NULL)
2872 {
2873 AssertReturn(fApply, VINF_SUCCESS);
2874 RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
2875
2876 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2877 {
2878 pDevIns->fTracing = fEnable;
2879 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2880 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2881 pDrvIns->fTracing = fEnable;
2882 }
2883
2884#ifdef VBOX_WITH_USB
2885 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2886 {
2887 pUsbIns->fTracing = fEnable;
2888 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2889 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2890 pDrvIns->fTracing = fEnable;
2891
2892 }
2893#endif
2894
2895 RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
2896 return VINF_SUCCESS;
2897 }
2898
2899 /*
2900 * Specific devices, USB devices or drivers.
2901 * Decode prefix to figure which of these it applies to.
2902 */
2903 if (cchName <= 3)
2904 return VERR_NOT_FOUND;
2905
2906 uint32_t cMatches = 0;
2907 if (!strncmp("dev", pszName, 3))
2908 {
2909 RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw); /* Shared-mode is sufficient for this. */
2910 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2911 {
2912 const char *pszDevName = pDevIns->Internal.s.pDevR3->pReg->szName;
2913 size_t cchDevName = strlen(pszDevName);
2914 if ( ( cchDevName == cchName
2915 && RTStrNICmp(pszName, pszDevName, cchDevName))
2916 || ( cchDevName == cchName - 3
2917 && RTStrNICmp(pszName + 3, pszDevName, cchDevName)) )
2918 {
2919 cMatches++;
2920 if (fApply)
2921 pDevIns->fTracing = fEnable;
2922 }
2923 }
2924 }
2925 else if (!strncmp("usb", pszName, 3))
2926 {
2927 RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw); /* Shared-mode is sufficient for this. */
2928 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2929 {
2930 const char *pszUsbName = pUsbIns->Internal.s.pUsbDev->pReg->szName;
2931 size_t cchUsbName = strlen(pszUsbName);
2932 if ( ( cchUsbName == cchName
2933 && RTStrNICmp(pszName, pszUsbName, cchUsbName))
2934 || ( cchUsbName == cchName - 3
2935 && RTStrNICmp(pszName + 3, pszUsbName, cchUsbName)) )
2936 {
2937 cMatches++;
2938 if (fApply)
2939 pUsbIns->fTracing = fEnable;
2940 }
2941 }
2942 }
2943 else if (!strncmp("drv", pszName, 3))
2944 {
2945 AssertReturn(fApply, VINF_SUCCESS);
2946 RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw); /* Shared-mode is sufficient for this. */
2947
2948 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
2949 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
2950 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2951 {
2952 const char *pszDrvName = pDrvIns->Internal.s.pDrv->pReg->szName;
2953 size_t cchDrvName = strlen(pszDrvName);
2954 if ( ( cchDrvName == cchName
2955 && RTStrNICmp(pszName, pszDrvName, cchDrvName))
2956 || ( cchDrvName == cchName - 3
2957 && RTStrNICmp(pszName + 3, pszDrvName, cchDrvName)) )
2958 {
2959 cMatches++;
2960 if (fApply)
2961 pDrvIns->fTracing = fEnable;
2962 }
2963 }
2964
2965#ifdef VBOX_WITH_USB
2966 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
2967 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
2968 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
2969 {
2970 const char *pszDrvName = pDrvIns->Internal.s.pDrv->pReg->szName;
2971 size_t cchDrvName = strlen(pszDrvName);
2972 if ( ( cchDrvName == cchName
2973 && RTStrNICmp(pszName, pszDrvName, cchDrvName))
2974 || ( cchDrvName == cchName - 3
2975 && RTStrNICmp(pszName + 3, pszDrvName, cchDrvName)) )
2976 {
2977 cMatches++;
2978 if (fApply)
2979 pDrvIns->fTracing = fEnable;
2980 }
2981 }
2982#endif
2983
2984 }
2985 else
2986 return VERR_NOT_FOUND;
2987
2988 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
2989 return cMatches > 0 ? VINF_SUCCESS : VERR_NOT_FOUND;
2990}
2991
2992
2993/**
2994 * Worker for DBGFR3TraceQueryConfig that checks whether all drivers, devices,
2995 * and USB device have the same tracing settings.
2996 *
2997 * @returns true / false.
2998 * @param pVM The cross context VM structure.
2999 * @param fEnabled The tracing setting to check for.
3000 */
3001VMMR3_INT_DECL(bool) PDMR3TracingAreAll(PVM pVM, bool fEnabled)
3002{
3003 RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
3004
3005 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
3006 {
3007 if (pDevIns->fTracing != (uint32_t)fEnabled)
3008 {
3009 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
3010 return false;
3011 }
3012
3013 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
3014 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
3015 if (pDrvIns->fTracing != (uint32_t)fEnabled)
3016 {
3017 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
3018 return false;
3019 }
3020 }
3021
3022#ifdef VBOX_WITH_USB
3023 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
3024 {
3025 if (pUsbIns->fTracing != (uint32_t)fEnabled)
3026 {
3027 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
3028 return false;
3029 }
3030
3031 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
3032 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
3033 if (pDrvIns->fTracing != (uint32_t)fEnabled)
3034 {
3035 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
3036 return false;
3037 }
3038 }
3039#endif
3040
3041 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
3042 return true;
3043}
3044
3045
3046/**
3047 * Worker for PDMR3TracingQueryConfig that adds a prefixed name to the output
3048 * string.
3049 *
3050 * @returns VINF_SUCCESS or VERR_BUFFER_OVERFLOW
3051 * @param ppszDst The pointer to the output buffer pointer.
3052 * @param pcbDst The pointer to the output buffer size.
3053 * @param fSpace Whether to add a space before the name.
3054 * @param pszPrefix The name prefix.
3055 * @param pszName The name.
3056 */
3057static int pdmR3TracingAdd(char **ppszDst, size_t *pcbDst, bool fSpace, const char *pszPrefix, const char *pszName)
3058{
3059 size_t const cchPrefix = strlen(pszPrefix);
3060 if (!RTStrNICmp(pszPrefix, pszName, cchPrefix))
3061 pszName += cchPrefix;
3062 size_t const cchName = strlen(pszName);
3063
3064 size_t const cchThis = cchName + cchPrefix + fSpace;
3065 if (cchThis >= *pcbDst)
3066 return VERR_BUFFER_OVERFLOW;
3067 if (fSpace)
3068 {
3069 **ppszDst = ' ';
3070 memcpy(*ppszDst + 1, pszPrefix, cchPrefix);
3071 memcpy(*ppszDst + 1 + cchPrefix, pszName, cchName + 1);
3072 }
3073 else
3074 {
3075 memcpy(*ppszDst, pszPrefix, cchPrefix);
3076 memcpy(*ppszDst + cchPrefix, pszName, cchName + 1);
3077 }
3078 *ppszDst += cchThis;
3079 *pcbDst -= cchThis;
3080 return VINF_SUCCESS;
3081}
3082
3083
3084/**
3085 * Worker for DBGFR3TraceQueryConfig use when not everything is either enabled
3086 * or disabled.
3087 *
3088 * @returns VINF_SUCCESS or VERR_BUFFER_OVERFLOW
3089 * @param pVM The cross context VM structure.
3090 * @param pszConfig Where to store the config spec.
3091 * @param cbConfig The size of the output buffer.
3092 */
3093VMMR3_INT_DECL(int) PDMR3TracingQueryConfig(PVM pVM, char *pszConfig, size_t cbConfig)
3094{
3095 char *pszDst = pszConfig;
3096 size_t cbDst = cbConfig;
3097
3098 RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
3099
3100 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
3101 {
3102 if (pDevIns->fTracing)
3103 {
3104 int rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "dev", pDevIns->Internal.s.pDevR3->pReg->szName);
3105 if (RT_FAILURE(rc))
3106 {
3107 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
3108 return rc;
3109 }
3110 }
3111
3112 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
3113 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
3114 if (pDrvIns->fTracing)
3115 {
3116 int rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "drv", pDrvIns->Internal.s.pDrv->pReg->szName);
3117 if (RT_FAILURE(rc))
3118 {
3119 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
3120 return rc;
3121 }
3122 }
3123 }
3124
3125#ifdef VBOX_WITH_USB
3126 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
3127 {
3128 if (pUsbIns->fTracing)
3129 {
3130 int rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "usb", pUsbIns->Internal.s.pUsbDev->pReg->szName);
3131 if (RT_FAILURE(rc))
3132 {
3133 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
3134 return rc;
3135 }
3136 }
3137
3138 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
3139 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
3140 if (pDrvIns->fTracing)
3141 {
3142 int rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "drv", pDrvIns->Internal.s.pDrv->pReg->szName);
3143 if (RT_FAILURE(rc))
3144 {
3145 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
3146 return rc;
3147 }
3148 }
3149 }
3150#endif
3151
3152 RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
3153 return VINF_SUCCESS;
3154}
3155
3156
3157/**
3158 * Checks that a PDMDRVREG::szName, PDMDEVREG::szName or PDMUSBREG::szName
3159 * field contains only a limited set of ASCII characters.
3160 *
3161 * @returns true / false.
3162 * @param pszName The name to validate.
3163 */
3164bool pdmR3IsValidName(const char *pszName)
3165{
3166 char ch;
3167 while ( (ch = *pszName) != '\0'
3168 && ( RT_C_IS_ALNUM(ch)
3169 || ch == '-'
3170 || ch == ' ' /** @todo disallow this! */
3171 || ch == '_') )
3172 pszName++;
3173 return ch == '\0';
3174}
3175
3176
3177/**
3178 * Info handler for 'pdmtracingids'.
3179 *
3180 * @param pVM The cross context VM structure.
3181 * @param pHlp The output helpers.
3182 * @param pszArgs The optional user arguments.
3183 *
3184 * @remarks Can be called on most threads.
3185 */
3186static DECLCALLBACK(void) pdmR3InfoTracingIds(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
3187{
3188 /*
3189 * Parse the argument (optional).
3190 */
3191 if ( pszArgs
3192 && *pszArgs
3193 && strcmp(pszArgs, "all")
3194 && strcmp(pszArgs, "devices")
3195 && strcmp(pszArgs, "drivers")
3196 && strcmp(pszArgs, "usb"))
3197 {
3198 pHlp->pfnPrintf(pHlp, "Unable to grok '%s'\n", pszArgs);
3199 return;
3200 }
3201 bool fAll = !pszArgs || !*pszArgs || !strcmp(pszArgs, "all");
3202 bool fDevices = fAll || !strcmp(pszArgs, "devices");
3203 bool fUsbDevs = fAll || !strcmp(pszArgs, "usb");
3204 bool fDrivers = fAll || !strcmp(pszArgs, "drivers");
3205
3206 /*
3207 * Produce the requested output.
3208 */
3209/** @todo lock PDM lists! */
3210 /* devices */
3211 if (fDevices)
3212 {
3213 pHlp->pfnPrintf(pHlp, "Device tracing IDs:\n");
3214 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
3215 pHlp->pfnPrintf(pHlp, "%05u %s\n", pDevIns->idTracing, pDevIns->Internal.s.pDevR3->pReg->szName);
3216 }
3217
3218 /* USB devices */
3219 if (fUsbDevs)
3220 {
3221 pHlp->pfnPrintf(pHlp, "USB device tracing IDs:\n");
3222 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
3223 pHlp->pfnPrintf(pHlp, "%05u %s\n", pUsbIns->idTracing, pUsbIns->Internal.s.pUsbDev->pReg->szName);
3224 }
3225
3226 /* Drivers */
3227 if (fDrivers)
3228 {
3229 pHlp->pfnPrintf(pHlp, "Driver tracing IDs:\n");
3230 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
3231 {
3232 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
3233 {
3234 uint32_t iLevel = 0;
3235 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown, iLevel++)
3236 pHlp->pfnPrintf(pHlp, "%05u %s (level %u, lun %u, dev %s)\n",
3237 pDrvIns->idTracing, pDrvIns->Internal.s.pDrv->pReg->szName,
3238 iLevel, pLun->iLun, pDevIns->Internal.s.pDevR3->pReg->szName);
3239 }
3240 }
3241
3242 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
3243 {
3244 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
3245 {
3246 uint32_t iLevel = 0;
3247 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown, iLevel++)
3248 pHlp->pfnPrintf(pHlp, "%05u %s (level %u, lun %u, dev %s)\n",
3249 pDrvIns->idTracing, pDrvIns->Internal.s.pDrv->pReg->szName,
3250 iLevel, pLun->iLun, pUsbIns->Internal.s.pUsbDev->pReg->szName);
3251 }
3252 }
3253 }
3254}
3255
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