PDM.cpp revision e2b721de2492ed4f33cf02aa9e72f6e05b7c9a61
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore/* $Id$ */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore/** @file
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * PDM - Pluggable Device Manager.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore/*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Copyright (C) 2006-2007 Sun Microsystems, Inc.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * This file is part of VirtualBox Open Source Edition (OSE), as
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * available from http://www.virtualbox.org. This file is free software;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * you can redistribute it and/or modify it under the terms of the GNU
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * General Public License (GPL) as published by the Free Software
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Foundation, in version 2 as it comes in the "COPYING" file of the
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * Clara, CA 95054 USA or visit http://www.sun.com if you need
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * additional information or have any questions.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore/** @page pg_pdm PDM - The Pluggable Device & Driver Manager
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * VirtualBox is designed to be very configurable, i.e. the ability to select
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * virtual devices and configure them uniquely for a VM. For this reason
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * virtual devices are not statically linked with the VMM but loaded, linked and
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * instantiated at runtime by PDM using the information found in the
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Configuration Manager (CFGM).
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * While the chief purpose of PDM is to manager of devices their drivers, it
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * also serves as somewhere to put usful things like cross context queues, cross
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * context synchronization (like critsect), VM centric thread management,
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * asynchronous I/O framework, and so on.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @see grp_pdm
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @section sec_pdm_dev The Pluggable Devices
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Devices register themselves when the module containing them is loaded. PDM
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * will call the entry point 'VBoxDevicesRegister' when loading a device module.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * The device module will then use the supplied callback table to check the VMM
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * version and to register its devices. Each device have an unique (for the
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * configured VM) name. The name is not only used in PDM but also in CFGM (to
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * organize device and device instance settings) and by anyone who wants to talk
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * to a specific device instance.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * When all device modules have been successfully loaded PDM will instantiate
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * those devices which are configured for the VM. Note that a device may have
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * more than one instance, see network adaptors for instance. When
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * instantiating a device PDM provides device instance memory and a callback
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * table (aka Device Helpers / DevHlp) with the VM APIs which the device
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * instance is trusted with.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Some devices are trusted devices, most are not. The trusted devices are an
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * integrated part of the VM and can obtain the VM handle from their device
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * instance handles, thus enabling them to call any VM api. Untrusted devices
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * can only use the callbacks provided during device instantiation.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * The main purpose in having DevHlps rather than just giving all the devices
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * the VM handle and let them call the internal VM APIs directly, is both to
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * create a binary interface that can be supported accross releases and to
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * create a barrier between devices and the VM. (The trusted / untrusted bit
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * hasn't turned out to be of much use btw., but it's easy to maintain so there
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * isn't any point in removing it.)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * A device can provide a ring-0 and/or a raw-mode context extension to improve
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * the VM performance by handling exits and traps (respectively) without
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * requiring context switches (to ring-3). Callbacks for MMIO and I/O ports can
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * needs to be registered specifically for the additional contexts for this to
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * make sense. Also, the device has to be trusted to be loaded into R0/RC
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * because of the extra privilege it entails. Note that raw-mode code and data
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * will be subject to relocation.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @section sec_pdm_special_devs Special Devices
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Several kinds of devices interacts with the VMM and/or other device and PDM
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * will work like a mediator for these. The typical pattern is that the device
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * calls a special registration device helper with a set of callbacks, PDM
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * responds by copying this and providing a pointer to a set helper callbacks
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * for that particular kind of device. Unlike interfaces where the callback
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * table pointer is used a 'this' pointer, these arrangements will use the
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * device instance pointer (PPDMDEVINS) as a kind of 'this' pointer.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * For an example of this kind of setup, see the PIC. The PIC registers itself
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * by calling PDMDEVHLPR3::pfnPICRegister. PDM saves the device instance,
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * copies the callback tables (PDMPICREG), resolving the ring-0 and raw-mode
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * addresses in the process, and hands back the pointer to a set of helper
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * methods (PDMPICHLPR3). The PCI device then queries the ring-0 and raw-mode
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * helpers using PDMPICHLPR3::pfnGetR0Helpers and PDMPICHLPR3::pfnGetRCHelpers.
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * The PCI device repeates ths pfnGetRCHelpers call in it's relocation method
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * since the address changes when RC is relocated.
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi *
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * @see grp_pdm_device
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi *
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @section sec_pdm_usbdev The Pluggable USB Devices
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * USB devices are handled a little bit differently than other devices. The
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * general concepts wrt. pluggability are mostly the same, but the details
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * varies. The registration entry point is 'VBoxUsbRegister', the device
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * instance is PDMUSBINS and the callbacks helpers are different. Also, USB
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * device are restricted to ring-3 and cannot have any ring-0 or raw-mode
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * extensions (at least not yet).
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * The way USB devices work differs greatly from other devices though since they
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * aren't attaches directly to the PCI/ISA/whatever system buses but via a
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * USB host control (OHCI, UHCI or EHCI). USB devices handles USB requests
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * (URBs) and does not register I/O ports, MMIO ranges or PCI bus
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * devices/functions.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @see grp_pdm_usbdev
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @section sec_pdm_drv The Pluggable Drivers
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * The VM devices are often accessing host hardware or OS facilities. For most
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * devices these facilities can be abstracted in one or more levels. These
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * abstractions are called drivers.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * For instance take a DVD/CD drive. This can be connected to a SCSI
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * controller, an ATA controller or a SATA controller. The basics of the DVD/CD
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * drive implementation remains the same - eject, insert, read, seek, and such.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * (For the scsi case, you might wanna speak SCSI directly to, but that can of
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * course be fixed - see SCSI passthru.) So, it
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * makes much sense to have a generic CD/DVD driver which implements this.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Then the media 'inserted' into the DVD/CD drive can be a ISO image, or it can
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * be read from a real CD or DVD drive (there are probably other custom formats
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * someone could desire to read or construct too). So, it would make sense to
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * have abstracted interfaces for dealing with this in a generic way so the
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * cdrom unit doesn't have to implement it all. Thus we have created the
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * CDROM/DVD media driver family.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * So, for this example the IDE controller #1 (i.e. secondary) will have
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * the DVD/CD Driver attached to it's LUN #0 (master). When a media is mounted
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * the DVD/CD Driver will have a ISO, HostDVD or RAW (media) Driver attached.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * It is possible to configure many levels of drivers inserting filters, loggers,
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * or whatever you desire into the chain. We're using this for network sniffing
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * for instance.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * The drivers are loaded in a similar manner to that of the device, namely by
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * iterating a keyspace in CFGM, load the modules listed there and call
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * 'VBoxDriversRegister' with a callback table.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @see grp_pdm_driver
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @section sec_pdm_ifs Interfaces
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * The pluggable drivers and devices exposes one standard interface (callback
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * table) which is used to construct, destruct, attach, detach,( ++,) and query
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * other interfaces. A device will query the interfaces required for it's
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * operation during init and hot-plug. PDM may query some interfaces during
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * runtime mounting too.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * An interface here means a function table contained within the device or
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * driver instance data. Its method are invoked with the function table pointer
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * as the first argument and they will calculate the address of the device or
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * driver instance data from it. (This is one of the aspects which *might* have
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * been better done in C++.)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @see grp_pdm_interfaces
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @section sec_pdm_utils Utilities
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * As mentioned earlier, PDM is the location of any usful constrcts that doesn't
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * quite fit into IPRT. The next subsections will discuss these.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * One thing these APIs all have in common is that resources will be associated
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * with a device / driver and automatically freed after it has been destroyed if
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * the destructor didn't do this.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @subsection sec_pdm_async_completion Async I/O
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * The PDM Async I/O API provides a somewhat platform agnostic interface for
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * asynchronous I/O. For reasons of performance and complexcity this does not
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * build upon any IPRT API.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @todo more details.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @see grp_pdm_async_completion
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @subsection sec_pdm_async_task Async Task - not implemented
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @todo implement and describe
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @see grp_pdm_async_task
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @subsection sec_pdm_critsect Critical Section
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * The PDM Critical Section API is currently building on the IPRT API with the
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * same name. It adds the posibility to use critical sections in ring-0 and
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * raw-mode as well as in ring-3. There are certain restrictions on the RC and
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * R0 usage though since we're not able to wait on it, nor wake up anyone that
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * is waiting on it. These restrictions origins with the use of a ring-3 event
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * semaphore. In a later incarnation we plan to replace the ring-3 event
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * semaphore with a ring-0 one, thus enabling us to wake up waiters while
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * exectuing in ring-0 and making the hardware assisted execution mode more
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * efficient. (Raw-mode won't benefit much from this, naturally.)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @see grp_pdm_critsect
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @subsection sec_pdm_queue Queue
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * The PDM Queue API is for queuing one or more tasks for later consumption in
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * ring-3 by EMT, and optinally forcing a delayed or ASAP return to ring-3. The
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * queues can also be run on a timer basis as an alternative to the ASAP thing.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * The queue will be flushed at forced action time.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * A queue can also be used by another thread (a I/O worker for instance) to
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * send work / events over to the EMT.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @see grp_pdm_queue
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @subsection sec_pdm_task Task - not implemented yet
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * The PDM Task API is for flagging a task for execution at a later point when
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * we're back in ring-3, optionally forcing the ring-3 return to happen ASAP.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * As you can see the concept is similar to queues only simpler.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * A task can also be scheduled by another thread (a I/O worker for instance) as
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * a mean of getting something done in EMT.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @see grp_pdm_task
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @subsection sec_pdm_thread Thread
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * The PDM Thread API is there to help devices and drivers manage their threads
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * correctly wrt. power on, suspend, resume, power off and destruction.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * The general usage pattern for threads in the employ of devices and drivers is
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * that they shuffle data or requests while the VM is running and stop doing
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * this when the VM is paused or powered down. Rogue threads running while the
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * VM is paused can cause the state to change during saving or have other
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * unwanted side effects. The PDM Threads API ensures that this won't happen.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @see grp_pdm_thread
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore/*******************************************************************************
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore* Header Files *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore*******************************************************************************/
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#define LOG_GROUP LOG_GROUP_PDM
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#include "PDMInternal.h"
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#include <VBox/pdm.h>
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#include <VBox/mm.h>
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#include <VBox/pgm.h>
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#include <VBox/ssm.h>
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#include <VBox/vm.h>
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#include <VBox/uvm.h>
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#include <VBox/vmm.h>
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#include <VBox/param.h>
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#include <VBox/err.h>
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#include <VBox/sup.h>
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#include <VBox/log.h>
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#include <iprt/asm.h>
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#include <iprt/assert.h>
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#include <iprt/alloc.h>
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#include <iprt/ldr.h>
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#include <iprt/path.h>
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#include <iprt/string.h>
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore/*******************************************************************************
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore* Defined Constants And Macros *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore*******************************************************************************/
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore/** The PDM saved state version. */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#define PDM_SAVED_STATE_VERSION 4
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#define PDM_SAVED_STATE_VERSION_PRE_NMI_FF 3
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore/*******************************************************************************
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore* Internal Functions *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore*******************************************************************************/
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amorestatic DECLCALLBACK(int) pdmR3LiveExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uPass);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amorestatic DECLCALLBACK(int) pdmR3SaveExec(PVM pVM, PSSMHANDLE pSSM);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amorestatic DECLCALLBACK(int) pdmR3LoadExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amorestatic DECLCALLBACK(int) pdmR3LoadPrep(PVM pVM, PSSMHANDLE pSSM);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore/**
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Initializes the PDM part of the UVM.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * This doesn't really do much right now but has to be here for the sake
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * of completeness.
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi *
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * @returns VBox status code.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param pUVM Pointer to the user mode VM structure.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'AmoreVMMR3DECL(int) PDMR3InitUVM(PUVM pUVM)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore{
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore AssertCompile(sizeof(pUVM->pdm.s) <= sizeof(pUVM->pdm.padding));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore AssertRelease(sizeof(pUVM->pdm.s) <= sizeof(pUVM->pdm.padding));
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi pUVM->pdm.s.pModules = NULL;
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi return VINF_SUCCESS;
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi}
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore/**
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Initializes the PDM.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @returns VBox status code.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param pVM The VM to operate on.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
fc2512cfb727d49529d8ed99164db871f4829b73Robert MustacchiVMMR3DECL(int) PDMR3Init(PVM pVM)
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi{
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore LogFlow(("PDMR3Init\n"));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Assert alignment and sizes.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore AssertRelease(!(RT_OFFSETOF(VM, pdm.s) & 31));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore AssertRelease(sizeof(pVM->pdm.s) <= sizeof(pVM->pdm.padding));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore AssertCompileMemberAlignment(PDM, CritSect, sizeof(uintptr_t));
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Init the structure.
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pVM->pdm.s.offVM = RT_OFFSETOF(VM, pdm.s);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pVM->pdm.s.GCPhysVMMDevHeap = NIL_RTGCPHYS;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Initialize sub compontents.
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi */
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi int rc = RTCritSectInit(&pVM->pdm.s.MiscCritSect);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (RT_SUCCESS(rc))
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi rc = pdmR3CritSectInit(pVM);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi if (RT_SUCCESS(rc))
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi rc = PDMR3CritSectInit(pVM, &pVM->pdm.s.CritSect, RT_SRC_POS, "PDM");
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi if (RT_SUCCESS(rc))
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi rc = pdmR3LdrInitU(pVM->pUVM);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (RT_SUCCESS(rc))
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi rc = pdmR3AsyncCompletionInit(pVM);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#endif
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (RT_SUCCESS(rc))
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore rc = pdmR3DrvInit(pVM);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (RT_SUCCESS(rc))
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore rc = pdmR3DevInit(pVM);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (RT_SUCCESS(rc))
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Register the saved state data unit.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore rc = SSMR3RegisterInternal(pVM, "pdm", 1, PDM_SAVED_STATE_VERSION, 128,
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore NULL, pdmR3LiveExec, NULL,
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore NULL, pdmR3SaveExec, NULL,
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pdmR3LoadPrep, pdmR3LoadExec, NULL);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (RT_SUCCESS(rc))
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi {
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi LogFlow(("PDM: Successfully initialized\n"));
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi return rc;
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi }
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi }
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi /*
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * Cleanup and return failure.
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore PDMR3Term(pVM);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore LogFlow(("PDMR3Init: returns %Rrc\n", rc));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return rc;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore}
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore/**
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Applies relocations to data and code managed by this
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * component. This function will be called at init and
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * whenever the VMM need to relocate it self inside the GC.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param pVM VM handle.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param offDelta Relocation delta relative to old location.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @remark The loader subcomponent is relocated by PDMR3LdrRelocate() very
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * early in the relocation phase.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'AmoreVMMR3DECL(void) PDMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore{
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore LogFlow(("PDMR3Relocate\n"));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Queues.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pdmR3QueueRelocate(pVM, offDelta);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pVM->pdm.s.pDevHlpQueueRC = PDMQueueRCPtr(pVM->pdm.s.pDevHlpQueueR3);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Critical sections.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pdmR3CritSectRelocate(pVM);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * The registered PIC.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (pVM->pdm.s.Pic.pDevInsRC)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pVM->pdm.s.Pic.pDevInsRC += offDelta;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pVM->pdm.s.Pic.pfnSetIrqRC += offDelta;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pVM->pdm.s.Pic.pfnGetInterruptRC += offDelta;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * The registered APIC.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (pVM->pdm.s.Apic.pDevInsRC)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pVM->pdm.s.Apic.pDevInsRC += offDelta;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pVM->pdm.s.Apic.pfnGetInterruptRC += offDelta;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pVM->pdm.s.Apic.pfnSetBaseRC += offDelta;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pVM->pdm.s.Apic.pfnGetBaseRC += offDelta;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pVM->pdm.s.Apic.pfnSetTPRRC += offDelta;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pVM->pdm.s.Apic.pfnGetTPRRC += offDelta;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pVM->pdm.s.Apic.pfnBusDeliverRC += offDelta;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (pVM->pdm.s.Apic.pfnLocalInterruptRC)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pVM->pdm.s.Apic.pfnLocalInterruptRC += offDelta;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pVM->pdm.s.Apic.pfnWriteMSRRC += offDelta;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pVM->pdm.s.Apic.pfnReadMSRRC += offDelta;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * The registered I/O APIC.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (pVM->pdm.s.IoApic.pDevInsRC)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pVM->pdm.s.IoApic.pDevInsRC += offDelta;
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi pVM->pdm.s.IoApic.pfnSetIrqRC += offDelta;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * The register PCI Buses.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore for (unsigned i = 0; i < RT_ELEMENTS(pVM->pdm.s.aPciBuses); i++)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (pVM->pdm.s.aPciBuses[i].pDevInsRC)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pVM->pdm.s.aPciBuses[i].pDevInsRC += offDelta;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pVM->pdm.s.aPciBuses[i].pfnSetIrqRC += offDelta;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Devices.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore PCPDMDEVHLPRC pDevHlpRC;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore int rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_pdmRCDevHlp", &pDevHlpRC);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore AssertReleaseMsgRC(rc, ("rc=%Rrc when resolving g_pdmRCDevHlp\n", rc));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_RC)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pDevIns->pDevHlpRC = pDevHlpRC;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pDevIns->pvInstanceDataRC = MMHyperR3ToRC(pVM, pDevIns->pvInstanceDataR3);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pDevIns->Internal.s.pVMRC = pVM->pVMRC;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (pDevIns->Internal.s.pPciBusR3)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pDevIns->Internal.s.pPciBusRC = MMHyperR3ToRC(pVM, pDevIns->Internal.s.pPciBusR3);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (pDevIns->Internal.s.pPciDeviceR3)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pDevIns->Internal.s.pPciDeviceRC = MMHyperR3ToRC(pVM, pDevIns->Internal.s.pPciDeviceR3);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (pDevIns->pReg->pfnRelocate)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore LogFlow(("PDMR3Relocate: Relocating device '%s'/%d\n",
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pDevIns->pReg->szDeviceName, pDevIns->iInstance));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pDevIns->pReg->pfnRelocate(pDevIns, offDelta);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore}
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore/**
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Worker for pdmR3Term that terminates a LUN chain.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param pVM Pointer to the shared VM structure.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param pLun The head of the chain.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param pszDevice The name of the device (for logging).
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param iInstance The device instance number (for logging).
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amorestatic void pdmR3TermLuns(PVM pVM, PPDMLUN pLun, const char *pszDevice, unsigned iInstance)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore{
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore for (; pLun; pLun = pLun->pNext)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Destroy them one at a time from the bottom up.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * (The serial device/drivers depends on this - bad.)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore PPDMDRVINS pDrvIns = pLun->pBottom;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pLun->pBottom = pLun->pTop = NULL;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore while (pDrvIns)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore PPDMDRVINS pDrvNext = pDrvIns->Internal.s.pUp;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (pDrvIns->pReg->pfnDestruct)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore LogFlow(("pdmR3DevTerm: Destroying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pDrvIns->pReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pszDevice, iInstance));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pDrvIns->pReg->pfnDestruct(pDrvIns);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pDrvIns->Internal.s.pDrv->cInstances--;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore TMR3TimerDestroyDriver(pVM, pDrvIns);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore //PDMR3QueueDestroyDriver(pVM, pDrvIns);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore //pdmR3ThreadDestroyDriver(pVM, pDrvIns);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore SSMR3DeregisterDriver(pVM, pDrvIns, NULL, 0);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pDrvIns = pDrvNext;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore}
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore/**
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Terminates the PDM.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Termination means cleaning up and freeing all resources,
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * the VM it self is at this point powered off or suspended.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @returns VBox status code.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param pVM The VM to operate on.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'AmoreVMMR3DECL(int) PDMR3Term(PVM pVM)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore{
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore LogFlow(("PDMR3Term:\n"));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Iterate the device instances and attach drivers, doing
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * relevant destruction processing.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * N.B. There is no need to mess around freeing memory allocated
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * from any MM heap since MM will do that in its Term function.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /* usb ones first. */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pdmR3TermLuns(pVM, pUsbIns->Internal.s.pLuns, pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (pUsbIns->pUsbReg->pfnDestruct)
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi {
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi LogFlow(("pdmR3DevTerm: Destroying - device '%s'/%d\n",
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi pUsbIns->pUsbReg->pfnDestruct(pUsbIns);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi }
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi //TMR3TimerDestroyUsb(pVM, pUsbIns);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi //SSMR3DeregisterUsb(pVM, pUsbIns, NULL, 0);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi pdmR3ThreadDestroyUsb(pVM, pUsbIns);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi }
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi /* then the 'normal' ones. */
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi {
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi pdmR3TermLuns(pVM, pDevIns->Internal.s.pLunsR3, pDevIns->pReg->szDeviceName, pDevIns->iInstance);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi if (pDevIns->pReg->pfnDestruct)
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi {
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi LogFlow(("pdmR3DevTerm: Destroying - device '%s'/%d\n",
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi pDevIns->pReg->szDeviceName, pDevIns->iInstance));
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi pDevIns->pReg->pfnDestruct(pDevIns);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi }
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi TMR3TimerDestroyDevice(pVM, pDevIns);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi //SSMR3DeregisterDriver(pVM, pDevIns, NULL, 0);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi pdmR3CritSectDeleteDevice(pVM, pDevIns);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi //pdmR3ThreadDestroyDevice(pVM, pDevIns);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi //PDMR3QueueDestroyDevice(pVM, pDevIns);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi PGMR3PhysMMIO2Deregister(pVM, pDevIns, UINT32_MAX);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi }
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi /*
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * Destroy all threads.
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi */
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi pdmR3ThreadDestroyAll(pVM);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Free async completion managers.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pdmR3AsyncCompletionTerm(pVM);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#endif
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Free modules.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pdmR3LdrTermU(pVM->pUVM);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Destroy the PDM lock.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore PDMR3CritSectDelete(&pVM->pdm.s.CritSect);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /* The MiscCritSect is deleted by PDMR3CritSectTerm. */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore LogFlow(("PDMR3Term: returns %Rrc\n", VINF_SUCCESS));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return VINF_SUCCESS;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore}
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore/**
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Terminates the PDM part of the UVM.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * This will unload any modules left behind.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param pUVM Pointer to the user mode VM structure.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'AmoreVMMR3DECL(void) PDMR3TermUVM(PUVM pUVM)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore{
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * In the normal cause of events we will now call pdmR3LdrTermU for
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * the second time. In the case of init failure however, this might
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * the first time, which is why we do it.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pdmR3LdrTermU(pUVM);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore}
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore/**
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Bits that are saved in pass 0 and in the final pass.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param pVM The VM handle.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param pSSM The saved state handle.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amorestatic void pdmR3SaveBoth(PVM pVM, PSSMHANDLE pSSM)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore{
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Save the list of device instances so we can check that they're all still
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * there when we load the state and that nothing new has been added.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore uint32_t i = 0;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3, i++)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore SSMR3PutU32(pSSM, i);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore SSMR3PutStrZ(pSSM, pDevIns->pReg->szDeviceName);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore SSMR3PutU32(pSSM, pDevIns->iInstance);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore SSMR3PutU32(pSSM, UINT32_MAX); /* terminator */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore}
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore/**
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Live save.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @returns VBox status code.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param pVM The VM handle.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param pSSM The saved state handle.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param uPass The pass.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amorestatic DECLCALLBACK(int) pdmR3LiveExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uPass)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore{
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore LogFlow(("pdmR3LiveExec:\n"));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore AssertReturn(uPass == 0, VERR_INTERNAL_ERROR_4);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pdmR3SaveBoth(pVM, pSSM);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return VINF_SSM_DONT_CALL_AGAIN;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore}
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore/**
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Execute state save operation.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @returns VBox status code.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param pVM The VM handle.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param pSSM The saved state handle.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amorestatic DECLCALLBACK(int) pdmR3SaveExec(PVM pVM, PSSMHANDLE pSSM)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore{
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore LogFlow(("pdmR3SaveExec:\n"));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Save interrupt and DMA states.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore PVMCPU pVCpu = &pVM->aCpus[idCpu];
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore SSMR3PutU32(pSSM, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_APIC));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore SSMR3PutU32(pSSM, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_PIC));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore SSMR3PutU32(pSSM, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_NMI));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore SSMR3PutU32(pSSM, VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_SMI));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore SSMR3PutU32(pSSM, VM_FF_ISSET(pVM, VM_FF_PDM_DMA));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pdmR3SaveBoth(pVM, pSSM);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return VINF_SUCCESS;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore}
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore/**
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Prepare state load operation.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * This will dispatch pending operations and clear the FFs governed by PDM and its devices.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @returns VBox status code.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param pVM The VM handle.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param pSSM The SSM handle.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amorestatic DECLCALLBACK(int) pdmR3LoadPrep(PVM pVM, PSSMHANDLE pSSM)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore{
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore LogFlow(("pdmR3LoadPrep: %s%s\n",
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore VM_FF_ISSET(pVM, VM_FF_PDM_QUEUES) ? " VM_FF_PDM_QUEUES" : "",
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore VM_FF_ISSET(pVM, VM_FF_PDM_DMA) ? " VM_FF_PDM_DMA" : ""));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#ifdef LOG_ENABLED
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore PVMCPU pVCpu = &pVM->aCpus[idCpu];
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore LogFlow(("pdmR3LoadPrep: VCPU %u %s%s\n", idCpu,
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_APIC) ? " VMCPU_FF_INTERRUPT_APIC" : "",
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_PIC) ? " VMCPU_FF_INTERRUPT_PIC" : ""));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#endif
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * In case there is work pending that will raise an interrupt,
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * start a DMA transfer, or release a lock. (unlikely)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (VM_FF_ISSET(pVM, VM_FF_PDM_QUEUES))
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore PDMR3QueueFlushAll(pVM);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /* Clear the FFs. */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore PVMCPU pVCpu = &pVM->aCpus[idCpu];
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_APIC);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_PIC);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_NMI);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_SMI);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return VINF_SUCCESS;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore}
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore/**
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Execute state load operation.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @returns VBox status code.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param pVM VM Handle.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param pSSM SSM operation handle.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param uVersion Data layout version.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param uPass The data pass.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amorestatic DECLCALLBACK(int) pdmR3LoadExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore{
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore int rc;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore LogFlow(("pdmR3LoadExec: uPass=%#x\n", uPass));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Validate version.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if ( uVersion != PDM_SAVED_STATE_VERSION
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore && uVersion != PDM_SAVED_STATE_VERSION_PRE_NMI_FF)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore AssertMsgFailed(("Invalid version uVersion=%d!\n", uVersion));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (uPass == SSM_PASS_FINAL)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Load the interrupt and DMA states.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore PVMCPU pVCpu = &pVM->aCpus[idCpu];
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /* APIC interrupt */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore uint32_t fInterruptPending = 0;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore rc = SSMR3GetU32(pSSM, &fInterruptPending);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (RT_FAILURE(rc))
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return rc;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (fInterruptPending & ~1)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore AssertMsgFailed(("fInterruptPending=%#x (APIC)\n", fInterruptPending));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore AssertRelease(!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_APIC));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (fInterruptPending)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /* PIC interrupt */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore fInterruptPending = 0;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore rc = SSMR3GetU32(pSSM, &fInterruptPending);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (RT_FAILURE(rc))
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return rc;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (fInterruptPending & ~1)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore AssertMsgFailed(("fInterruptPending=%#x (PIC)\n", fInterruptPending));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore AssertRelease(!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_PIC));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (fInterruptPending)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_PIC);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (uVersion > PDM_SAVED_STATE_VERSION_PRE_NMI_FF)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /* NMI interrupt */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore fInterruptPending = 0;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore rc = SSMR3GetU32(pSSM, &fInterruptPending);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (RT_FAILURE(rc))
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return rc;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (fInterruptPending & ~1)
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore AssertMsgFailed(("fInterruptPending=%#x (NMI)\n", fInterruptPending));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore AssertRelease(!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_NMI));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (fInterruptPending)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_NMI);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /* SMI interrupt */
b599bd937c305a895426e8c412ca920ce7824850Robert Mustacchi fInterruptPending = 0;
b599bd937c305a895426e8c412ca920ce7824850Robert Mustacchi rc = SSMR3GetU32(pSSM, &fInterruptPending);
b599bd937c305a895426e8c412ca920ce7824850Robert Mustacchi if (RT_FAILURE(rc))
b599bd937c305a895426e8c412ca920ce7824850Robert Mustacchi return rc;
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi if (fInterruptPending & ~1)
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore AssertMsgFailed(("fInterruptPending=%#x (SMI)\n", fInterruptPending));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore AssertRelease(!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_INTERRUPT_SMI));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (fInterruptPending)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_SMI);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /* DMA pending */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore uint32_t fDMAPending = 0;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore rc = SSMR3GetU32(pSSM, &fDMAPending);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (RT_FAILURE(rc))
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return rc;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (fDMAPending & ~1)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore AssertMsgFailed(("fDMAPending=%#x\n", fDMAPending));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (fDMAPending)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore VM_FF_SET(pVM, VM_FF_PDM_DMA);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore Log(("pdmR3LoadExec: VM_FF_PDM_DMA=%RTbool\n", VM_FF_ISSET(pVM, VM_FF_PDM_DMA)));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi * Load the list of devices and verify that they are all there.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_FOUND;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore for (uint32_t i = 0; ; i++)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /* Get the sequence number / terminator. */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore uint32_t u32Sep;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore rc = SSMR3GetU32(pSSM, &u32Sep);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (RT_FAILURE(rc))
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return rc;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (u32Sep == UINT32_MAX)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore break;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (u32Sep != i)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore AssertMsgFailedReturn(("Out of seqence. u32Sep=%#x i=%#x\n", u32Sep, i), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /* Get the name and instance number. */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore char szDeviceName[RT_SIZEOFMEMB(PDMDEVREG, szDeviceName)];
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore rc = SSMR3GetStrZ(pSSM, szDeviceName, sizeof(szDeviceName));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (RT_FAILURE(rc))
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi return rc;
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi uint32_t iInstance;
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi rc = SSMR3GetU32(pSSM, &iInstance);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi if (RT_FAILURE(rc))
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi return rc;
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi /* Try locate it. */
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi PPDMDEVINS pDevIns;
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi for (pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi if ( !strcmp(szDeviceName, pDevIns->pReg->szDeviceName)
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi && pDevIns->iInstance == iInstance)
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi {
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi AssertLogRelMsgReturn(!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_FOUND),
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi ("%s/#%u\n", pDevIns->pReg->szDeviceName, pDevIns->iInstance),
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_FOUND;
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi break;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi if (!pDevIns)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore LogRel(("Device '%s'/%d not found in current config\n", szDeviceName, iInstance));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Device '%s'/%d not found in current config"), szDeviceName, iInstance);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Check that no additional devices were configured.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_FOUND))
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore LogRel(("Device '%s'/%d not found in the saved state\n", pDevIns->pReg->szDeviceName, pDevIns->iInstance));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Device '%s'/%d not found in the saved state"),
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pDevIns->pReg->szDeviceName, pDevIns->iInstance);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return VINF_SUCCESS;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore}
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore/**
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Worker for PDMR3PowerOn that deals with one driver.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param pDrvIns The driver instance.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param pszDeviceName The parent device name.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param iDevInstance The parent device instance number.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param iLun The parent LUN number.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'AmoreDECLINLINE(int) pdmR3PowerOnDrv(PPDMDRVINS pDrvIns, const char *pszDeviceName, uint32_t iDevInstance, uint32_t iLun)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore{
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore Assert(pDrvIns->Internal.s.fVMSuspended);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (pDrvIns->pReg->pfnPowerOn)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore LogFlow(("PDMR3PowerOn: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pDrvIns->pReg->szDriverName, pDrvIns->iInstance, iLun, pszDeviceName, iDevInstance));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore int rc = VINF_SUCCESS; pDrvIns->pReg->pfnPowerOn(pDrvIns);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (RT_FAILURE(rc))
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore LogRel(("PDMR3PowerOn: driver '%s'/%d on LUN#%d of device '%s'/%d -> %Rrc\n",
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pDrvIns->pReg->szDriverName, pDrvIns->iInstance, iLun, pszDeviceName, iDevInstance, rc));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return rc;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pDrvIns->Internal.s.fVMSuspended = false;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return VINF_SUCCESS;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore}
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore/**
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Worker for PDMR3PowerOn that deals with one USB device instance.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @returns VBox status code.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param pUsbIns The USB device instance.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'AmoreDECLINLINE(int) pdmR3PowerOnUsb(PPDMUSBINS pUsbIns)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore{
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore Assert(pUsbIns->Internal.s.fVMSuspended);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (pUsbIns->pUsbReg->pfnVMPowerOn)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore LogFlow(("PDMR3PowerOn: Notifying - device '%s'/%d\n", pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore int rc = VINF_SUCCESS; pUsbIns->pUsbReg->pfnVMPowerOn(pUsbIns);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (RT_FAILURE(rc))
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore LogRel(("PDMR3PowerOn: device '%s'/%d -> %Rrc\n", pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance, rc));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return rc;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pUsbIns->Internal.s.fVMSuspended = false;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return VINF_SUCCESS;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore}
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore/**
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Worker for PDMR3PowerOn that deals with one device instance.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @returns VBox status code.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param pDevIns The device instance.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'AmoreDECLINLINE(int) pdmR3PowerOnDev(PPDMDEVINS pDevIns)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore{
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore Assert(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (pDevIns->pReg->pfnPowerOn)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore LogFlow(("PDMR3PowerOn: Notifying - device '%s'/%d\n", pDevIns->pReg->szDeviceName, pDevIns->iInstance));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore int rc = VINF_SUCCESS; pDevIns->pReg->pfnPowerOn(pDevIns);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (RT_FAILURE(rc))
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore LogRel(("PDMR3PowerOn: device '%s'/%d -> %Rrc\n", pDevIns->pReg->szDeviceName, pDevIns->iInstance, rc));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return rc;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore return VINF_SUCCESS;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore}
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore/**
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * This function will notify all the devices and their
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * attached drivers about the VM now being powered on.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore *
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * @param pVM VM Handle.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'AmoreVMMR3DECL(void) PDMR3PowerOn(PVM pVM)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore{
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore LogFlow(("PDMR3PowerOn:\n"));
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Iterate thru the device instances and USB device instances,
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * processing the drivers associated with those.
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore int rc = VINF_SUCCESS;
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns && RT_SUCCESS(rc); pDevIns = pDevIns->Internal.s.pNextR3)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore {
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore rc = pdmR3PowerOnDrv(pDrvIns, pDevIns->pReg->szDeviceName, pDevIns->iInstance, pLun->iLun);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (RT_SUCCESS(rc))
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore rc = pdmR3PowerOnDev(pDevIns);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#ifdef VBOX_WITH_USB
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns && RT_SUCCESS(rc); pUsbIns = pUsbIns->Internal.s.pNext)
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi {
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
fc2512cfb727d49529d8ed99164db871f4829b73Robert Mustacchi rc = pdmR3PowerOnDrv(pDrvIns, pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance, pLun->iLun);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (RT_SUCCESS(rc))
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore rc = pdmR3PowerOnUsb(pUsbIns);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore }
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore#endif
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore /*
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore * Resume all threads.
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore */
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore if (RT_SUCCESS(rc))
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore pdmR3ThreadResumeAll(pVM);
de572d98af8238405c5d1292a788b1a85b0c68ebGarrett D'Amore
/*
* On failure, clean up via PDMR3Suspend.
*/
if (RT_FAILURE(rc))
PDMR3Suspend(pVM);
LogFlow(("PDMR3PowerOn: returns %Rrc\n", rc));
return /*rc*/;
}
/**
* Worker for PDMR3Reset that deals with one driver.
*
* @param pDrvIns The driver instance.
* @param pcAsync The asynchronous reset notification counter.
* @param pszDeviceName The parent device name.
* @param iDevInstance The parent device instance number.
* @param iLun The parent LUN number.
*/
DECLINLINE(bool) pdmR3ResetDrv(PPDMDRVINS pDrvIns, unsigned *pcAsync,
const char *pszDeviceName, uint32_t iDevInstance, uint32_t iLun)
{
if (!pDrvIns->Internal.s.fVMReset)
{
pDrvIns->Internal.s.fVMReset = true;
if (pDrvIns->pReg->pfnReset)
{
if (!pDrvIns->Internal.s.pfnAsyncNotify)
{
LogFlow(("PDMR3Reset: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
pDrvIns->pReg->szDriverName, pDrvIns->iInstance, iLun, pszDeviceName, iDevInstance));
pDrvIns->pReg->pfnReset(pDrvIns);
if (pDrvIns->Internal.s.pfnAsyncNotify)
LogFlow(("PDMR3Reset: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
pDrvIns->pReg->szDriverName, pDrvIns->iInstance, iLun, pszDeviceName, iDevInstance));
}
else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
{
pDrvIns->Internal.s.pfnAsyncNotify = false;
LogFlow(("PDMR3Reset: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
pDrvIns->pReg->szDriverName, pDrvIns->iInstance, iLun, pszDeviceName, iDevInstance));
}
if (pDrvIns->Internal.s.pfnAsyncNotify)
{
pDrvIns->Internal.s.fVMReset = false;
(*pcAsync)++;
return false;
}
}
}
return true;
}
/**
* Worker for PDMR3Reset that deals with one USB device instance.
*
* @param pUsbIns The USB device instance.
* @param pcAsync The asynchronous reset notification counter.
*/
DECLINLINE(void) pdmR3ResetUsb(PPDMUSBINS pUsbIns, unsigned *pcAsync)
{
if (!pUsbIns->Internal.s.fVMReset)
{
pUsbIns->Internal.s.fVMReset = true;
if (pUsbIns->pUsbReg->pfnVMReset)
{
if (!pUsbIns->Internal.s.pfnAsyncNotify)
{
LogFlow(("PDMR3Reset: Notifying - device '%s'/%d\n", pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
pUsbIns->pUsbReg->pfnVMReset(pUsbIns);
if (pUsbIns->Internal.s.pfnAsyncNotify)
LogFlow(("PDMR3Reset: Async notification started - device '%s'/%d\n", pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
}
else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
{
LogFlow(("PDMR3Reset: Async notification completed - device '%s'/%d\n", pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
pUsbIns->Internal.s.pfnAsyncNotify = NULL;
}
if (pUsbIns->Internal.s.pfnAsyncNotify)
{
pUsbIns->Internal.s.fVMReset = false;
(*pcAsync)++;
}
}
}
}
/**
* Worker for PDMR3Reset that deals with one device instance.
*
* @param pDevIns The device instance.
* @param pcAsync The asynchronous reset notification counter.
*/
DECLINLINE(void) pdmR3ResetDev(PPDMDEVINS pDevIns, unsigned *pcAsync)
{
if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_RESET))
{
pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_RESET;
if (pDevIns->pReg->pfnReset)
{
if (!pDevIns->Internal.s.pfnAsyncNotify)
{
LogFlow(("PDMR3Reset: Notifying - device '%s'/%d\n", pDevIns->pReg->szDeviceName, pDevIns->iInstance));
pDevIns->pReg->pfnReset(pDevIns);
if (pDevIns->Internal.s.pfnAsyncNotify)
LogFlow(("PDMR3Reset: Async notification started - device '%s'/%d\n", pDevIns->pReg->szDeviceName, pDevIns->iInstance));
}
else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
{
LogFlow(("PDMR3Reset: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szDeviceName, pDevIns->iInstance));
pDevIns->Internal.s.pfnAsyncNotify = NULL;
}
if (pDevIns->Internal.s.pfnAsyncNotify)
{
pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_RESET;
(*pcAsync)++;
}
}
}
}
/**
* Resets a virtual CPU.
*
* Used by PDMR3Reset and CPU hot plugging.
*
* @param pVCpu The virtual CPU handle.
*/
VMMR3DECL(void) PDMR3ResetCpu(PVMCPU pVCpu)
{
VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_APIC);
VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_PIC);
VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_NMI);
VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_SMI);
}
/**
* This function will notify all the devices and their attached drivers about
* the VM now being reset.
*
* @param pVM VM Handle.
*/
VMMR3DECL(void) PDMR3Reset(PVM pVM)
{
LogFlow(("PDMR3Reset:\n"));
/*
* Clear all the reset flags.
*/
for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
{
pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_RESET;
for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
pDrvIns->Internal.s.fVMReset = false;
}
#ifdef VBOX_WITH_USB
for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
{
pUsbIns->Internal.s.fVMReset = false;
for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
pDrvIns->Internal.s.fVMReset = false;
}
#endif
/*
* The outer loop repeats until there are no more async requests.
*/
unsigned cAsync;
for (unsigned iLoop = 0; ; iLoop++)
{
/*
* Iterate thru the device instances and USB device instances,
* processing the drivers associated with those.
*/
cAsync = 0;
for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
{
unsigned const cAsyncStart = cAsync;
if (cAsync == cAsyncStart)
for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
if (!pdmR3ResetDrv(pDrvIns, &cAsync, pDevIns->pReg->szDeviceName, pDevIns->iInstance, pLun->iLun))
break;
if (cAsync == cAsyncStart)
pdmR3ResetDev(pDevIns, &cAsync);
}
#ifdef VBOX_WITH_USB
for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
{
unsigned const cAsyncStart = cAsync;
for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
if (!pdmR3ResetDrv(pDrvIns, &cAsync, pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance, pLun->iLun))
break;
if (cAsync == cAsyncStart)
pdmR3ResetUsb(pUsbIns, &cAsync);
}
#endif
if (!cAsync)
break;
/*
* Process requests.
*/
/** @todo This is utterly nuts and completely unsafe... will get back to it in a
* bit I hope... */
int rc = VMR3AsyncPdmNotificationWaitU(&pVM->pUVM->aCpus[0]);
AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc\n", rc));
rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY);
AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc\n", rc));
rc = VMR3ReqProcessU(pVM->pUVM, 0/*idDstCpu*/);
AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc\n", rc));
}
/*
* Clear all pending interrupts and DMA operations.
*/
for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
PDMR3ResetCpu(&pVM->aCpus[idCpu]);
VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
LogFlow(("PDMR3Reset: returns void\n"));
}
/**
* Worker for PDMR3Suspend that deals with one driver.
*
* @param pDrvIns The driver instance.
* @param pcAsync The asynchronous suspend notification counter.
* @param pszDeviceName The parent device name.
* @param iDevInstance The parent device instance number.
* @param iLun The parent LUN number.
*/
DECLINLINE(bool) pdmR3SuspendDrv(PPDMDRVINS pDrvIns, unsigned *pcAsync,
const char *pszDeviceName, uint32_t iDevInstance, uint32_t iLun)
{
if (!pDrvIns->Internal.s.fVMSuspended)
{
pDrvIns->Internal.s.fVMSuspended = true;
if (pDrvIns->pReg->pfnSuspend)
{
if (!pDrvIns->Internal.s.pfnAsyncNotify)
{
LogFlow(("PDMR3Suspend: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
pDrvIns->pReg->szDriverName, pDrvIns->iInstance, iLun, pszDeviceName, iDevInstance));
pDrvIns->pReg->pfnSuspend(pDrvIns);
if (pDrvIns->Internal.s.pfnAsyncNotify)
LogFlow(("PDMR3Suspend: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
pDrvIns->pReg->szDriverName, pDrvIns->iInstance, iLun, pszDeviceName, iDevInstance));
}
else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
{
pDrvIns->Internal.s.pfnAsyncNotify = false;
LogFlow(("PDMR3Suspend: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
pDrvIns->pReg->szDriverName, pDrvIns->iInstance, iLun, pszDeviceName, iDevInstance));
}
if (pDrvIns->Internal.s.pfnAsyncNotify)
{
pDrvIns->Internal.s.fVMSuspended = false;
(*pcAsync)++;
return false;
}
}
}
return true;
}
/**
* Worker for PDMR3Suspend that deals with one USB device instance.
*
* @param pUsbIns The USB device instance.
* @param pcAsync The asynchronous suspend notification counter.
*/
DECLINLINE(void) pdmR3SuspendUsb(PPDMUSBINS pUsbIns, unsigned *pcAsync)
{
if (!pUsbIns->Internal.s.fVMSuspended)
{
pUsbIns->Internal.s.fVMSuspended = true;
if (pUsbIns->pUsbReg->pfnVMSuspend)
{
if (!pUsbIns->Internal.s.pfnAsyncNotify)
{
LogFlow(("PDMR3Suspend: Notifying - device '%s'/%d\n", pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
pUsbIns->pUsbReg->pfnVMSuspend(pUsbIns);
if (pUsbIns->Internal.s.pfnAsyncNotify)
LogFlow(("PDMR3Suspend: Async notification started - device '%s'/%d\n", pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
}
else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
{
LogFlow(("PDMR3Suspend: Async notification completed - device '%s'/%d\n", pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
pUsbIns->Internal.s.pfnAsyncNotify = NULL;
}
if (pUsbIns->Internal.s.pfnAsyncNotify)
{
pUsbIns->Internal.s.fVMSuspended = false;
(*pcAsync)++;
}
}
}
}
/**
* Worker for PDMR3Suspend that deals with one device instance.
*
* @param pDevIns The device instance.
* @param pcAsync The asynchronous suspend notification counter.
*/
DECLINLINE(void) pdmR3SuspendDev(PPDMDEVINS pDevIns, unsigned *pcAsync)
{
if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED))
{
pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_SUSPENDED;
if (pDevIns->pReg->pfnSuspend)
{
if (!pDevIns->Internal.s.pfnAsyncNotify)
{
LogFlow(("PDMR3Suspend: Notifying - device '%s'/%d\n", pDevIns->pReg->szDeviceName, pDevIns->iInstance));
pDevIns->pReg->pfnSuspend(pDevIns);
if (pDevIns->Internal.s.pfnAsyncNotify)
LogFlow(("PDMR3Suspend: Async notification started - device '%s'/%d\n", pDevIns->pReg->szDeviceName, pDevIns->iInstance));
}
else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
{
LogFlow(("PDMR3Suspend: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szDeviceName, pDevIns->iInstance));
pDevIns->Internal.s.pfnAsyncNotify = NULL;
}
if (pDevIns->Internal.s.pfnAsyncNotify)
{
pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
(*pcAsync)++;
}
}
}
}
/**
* This function will notify all the devices and their attached drivers about
* the VM now being suspended.
*
* @param pVM The VM Handle.
* @thread EMT(0)
*/
VMMR3DECL(void) PDMR3Suspend(PVM pVM)
{
LogFlow(("PDMR3Suspend:\n"));
VM_ASSERT_EMT0(pVM);
/*
* The outer loop repeats until there are no more async requests.
*
* Note! We depend on the suspended indicators to be in the desired state
* and we do not reset them before starting because this allows
* PDMR3PowerOn and PDMR3Resume to use PDMR3Suspend for cleaning up
* on failure.
*/
unsigned cAsync;
for (unsigned iLoop = 0; ; iLoop++)
{
/*
* Iterate thru the device instances and USB device instances,
* processing the drivers associated with those.
*
* The attached drivers are normally processed first. Some devices
* (like DevAHCI) though needs to be notified before the drivers so
* that it doesn't kick off any new requests after the drivers stopped
* taking any. (DrvVD changes to read-only in this particular case.)
*/
cAsync = 0;
for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
{
unsigned const cAsyncStart = cAsync;
if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION)
pdmR3SuspendDev(pDevIns, &cAsync);
if (cAsync == cAsyncStart)
for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
if (!pdmR3SuspendDrv(pDrvIns, &cAsync, pDevIns->pReg->szDeviceName, pDevIns->iInstance, pLun->iLun))
break;
if ( cAsync == cAsyncStart
&& !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION))
pdmR3SuspendDev(pDevIns, &cAsync);
}
#ifdef VBOX_WITH_USB
for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
{
unsigned const cAsyncStart = cAsync;
for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
if (!pdmR3SuspendDrv(pDrvIns, &cAsync, pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance, pLun->iLun))
break;
if (cAsync == cAsyncStart)
pdmR3SuspendUsb(pUsbIns, &cAsync);
}
#endif
if (!cAsync)
break;
/*
* Process requests.
*/
/** @todo This is utterly nuts and completely unsafe... will get back to it in a
* bit I hope... */
int rc = VMR3AsyncPdmNotificationWaitU(&pVM->pUVM->aCpus[0]);
AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc\n", rc));
rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY);
AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc\n", rc));
rc = VMR3ReqProcessU(pVM->pUVM, 0/*idDstCpu*/);
AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc\n", rc));
}
/*
* Suspend all threads.
*/
pdmR3ThreadSuspendAll(pVM);
LogFlow(("PDMR3Suspend: returns void\n"));
}
/**
* Worker for PDMR3Resume that deals with one driver.
*
* @param pDrvIns The driver instance.
* @param pszDeviceName The parent device name.
* @param iDevInstance The parent device instance number.
* @param iLun The parent LUN number.
*/
DECLINLINE(int) pdmR3ResumeDrv(PPDMDRVINS pDrvIns, const char *pszDeviceName, uint32_t iDevInstance, uint32_t iLun)
{
Assert(pDrvIns->Internal.s.fVMSuspended);
if (pDrvIns->pReg->pfnResume)
{
LogFlow(("PDMR3Resume: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
pDrvIns->pReg->szDriverName, pDrvIns->iInstance, iLun, pszDeviceName, iDevInstance));
int rc = VINF_SUCCESS; pDrvIns->pReg->pfnResume(pDrvIns);
if (RT_FAILURE(rc))
{
LogRel(("PDMR3Resume: driver '%s'/%d on LUN#%d of device '%s'/%d -> %Rrc\n",
pDrvIns->pReg->szDriverName, pDrvIns->iInstance, iLun, pszDeviceName, iDevInstance, rc));
return rc;
}
}
pDrvIns->Internal.s.fVMSuspended = false;
return VINF_SUCCESS;
}
/**
* Worker for PDMR3Resume that deals with one USB device instance.
*
* @returns VBox status code.
* @param pUsbIns The USB device instance.
*/
DECLINLINE(int) pdmR3ResumeUsb(PPDMUSBINS pUsbIns)
{
Assert(pUsbIns->Internal.s.fVMSuspended);
if (pUsbIns->pUsbReg->pfnVMResume)
{
LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n", pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
int rc = VINF_SUCCESS; pUsbIns->pUsbReg->pfnVMResume(pUsbIns);
if (RT_FAILURE(rc))
{
LogRel(("PDMR3Resume: device '%s'/%d -> %Rrc\n", pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance, rc));
return rc;
}
}
pUsbIns->Internal.s.fVMSuspended = false;
return VINF_SUCCESS;
}
/**
* Worker for PDMR3Resume that deals with one device instance.
*
* @returns VBox status code.
* @param pDevIns The device instance.
*/
DECLINLINE(int) pdmR3ResumeDev(PPDMDEVINS pDevIns)
{
Assert(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED);
if (pDevIns->pReg->pfnResume)
{
LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n", pDevIns->pReg->szDeviceName, pDevIns->iInstance));
int rc = VINF_SUCCESS; pDevIns->pReg->pfnResume(pDevIns);
if (RT_FAILURE(rc))
{
LogRel(("PDMR3Resume: device '%s'/%d -> %Rrc\n", pDevIns->pReg->szDeviceName, pDevIns->iInstance, rc));
return rc;
}
}
pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
return VINF_SUCCESS;
}
/**
* This function will notify all the devices and their
* attached drivers about the VM now being resumed.
*
* @param pVM VM Handle.
*/
VMMR3DECL(void) PDMR3Resume(PVM pVM)
{
LogFlow(("PDMR3Resume:\n"));
/*
* Iterate thru the device instances and USB device instances,
* processing the drivers associated with those.
*/
int rc = VINF_SUCCESS;
for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns && RT_SUCCESS(rc); pDevIns = pDevIns->Internal.s.pNextR3)
{
for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
rc = pdmR3ResumeDrv(pDrvIns, pDevIns->pReg->szDeviceName, pDevIns->iInstance, pLun->iLun);
if (RT_SUCCESS(rc))
rc = pdmR3ResumeDev(pDevIns);
}
#ifdef VBOX_WITH_USB
for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns && RT_SUCCESS(rc); pUsbIns = pUsbIns->Internal.s.pNext)
{
for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
rc = pdmR3ResumeDrv(pDrvIns, pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance, pLun->iLun);
if (RT_SUCCESS(rc))
rc = pdmR3ResumeUsb(pUsbIns);
}
#endif
/*
* Resume all threads.
*/
if (RT_SUCCESS(rc))
pdmR3ThreadResumeAll(pVM);
/*
* On failure, clean up via PDMR3Suspend.
*/
if (RT_FAILURE(rc))
PDMR3Suspend(pVM);
LogFlow(("PDMR3Resume: returns %Rrc\n", rc));
return /*rc*/;
}
/**
* Worker for PDMR3PowerOff that deals with one driver.
*
* @param pDrvIns The driver instance.
* @param pcAsync The asynchronous power off notification counter.
* @param pszDeviceName The parent device name.
* @param iDevInstance The parent device instance number.
* @param iLun The parent LUN number.
*/
DECLINLINE(bool) pdmR3PowerOffDrv(PPDMDRVINS pDrvIns, unsigned *pcAsync,
const char *pszDeviceName, uint32_t iDevInstance, uint32_t iLun)
{
if (!pDrvIns->Internal.s.fVMSuspended)
{
pDrvIns->Internal.s.fVMSuspended = true;
if (pDrvIns->pReg->pfnSuspend)
{
if (!pDrvIns->Internal.s.pfnAsyncNotify)
{
LogFlow(("PDMR3PowerOff: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
pDrvIns->pReg->szDriverName, pDrvIns->iInstance, iLun, pszDeviceName, iDevInstance));
pDrvIns->pReg->pfnPowerOff(pDrvIns);
if (pDrvIns->Internal.s.pfnAsyncNotify)
LogFlow(("PDMR3PowerOff: Async notification started - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
pDrvIns->pReg->szDriverName, pDrvIns->iInstance, iLun, pszDeviceName, iDevInstance));
}
else if (pDrvIns->Internal.s.pfnAsyncNotify(pDrvIns))
{
pDrvIns->Internal.s.pfnAsyncNotify = false;
LogFlow(("PDMR3PowerOff: Async notification completed - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
pDrvIns->pReg->szDriverName, pDrvIns->iInstance, iLun, pszDeviceName, iDevInstance));
}
if (pDrvIns->Internal.s.pfnAsyncNotify)
{
pDrvIns->Internal.s.fVMSuspended = false;
(*pcAsync)++;
return false;
}
}
}
return true;
}
/**
* Worker for PDMR3PowerOff that deals with one USB device instance.
*
* @param pUsbIns The USB device instance.
* @param pcAsync The asynchronous power off notification counter.
*/
DECLINLINE(void) pdmR3PowerOffUsb(PPDMUSBINS pUsbIns, unsigned *pcAsync)
{
if (!pUsbIns->Internal.s.fVMSuspended)
{
pUsbIns->Internal.s.fVMSuspended = true;
if (pUsbIns->pUsbReg->pfnVMPowerOff)
{
if (!pUsbIns->Internal.s.pfnAsyncNotify)
{
LogFlow(("PDMR3PowerOff: Notifying - device '%s'/%d\n", pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
pUsbIns->pUsbReg->pfnVMPowerOff(pUsbIns);
if (pUsbIns->Internal.s.pfnAsyncNotify)
LogFlow(("PDMR3PowerOff: Async notification started - device '%s'/%d\n", pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
}
else if (pUsbIns->Internal.s.pfnAsyncNotify(pUsbIns))
{
LogFlow(("PDMR3PowerOff: Async notification completed - device '%s'/%d\n", pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
pUsbIns->Internal.s.pfnAsyncNotify = NULL;
}
if (pUsbIns->Internal.s.pfnAsyncNotify)
{
pUsbIns->Internal.s.fVMSuspended = false;
(*pcAsync)++;
}
}
}
}
/**
* Worker for PDMR3PowerOff that deals with one device instance.
*
* @param pDevIns The device instance.
* @param pcAsync The asynchronous power off notification counter.
*/
DECLINLINE(void) pdmR3PowerOffDev(PPDMDEVINS pDevIns, unsigned *pcAsync)
{
if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_SUSPENDED))
{
pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_SUSPENDED;
if (pDevIns->pReg->pfnSuspend)
{
if (!pDevIns->Internal.s.pfnAsyncNotify)
{
LogFlow(("PDMR3PowerOff: Notifying - device '%s'/%d\n", pDevIns->pReg->szDeviceName, pDevIns->iInstance));
pDevIns->pReg->pfnPowerOff(pDevIns);
if (pDevIns->Internal.s.pfnAsyncNotify)
LogFlow(("PDMR3PowerOff: Async notification started - device '%s'/%d\n", pDevIns->pReg->szDeviceName, pDevIns->iInstance));
}
else if (pDevIns->Internal.s.pfnAsyncNotify(pDevIns))
{
LogFlow(("PDMR3PowerOff: Async notification completed - device '%s'/%d\n", pDevIns->pReg->szDeviceName, pDevIns->iInstance));
pDevIns->Internal.s.pfnAsyncNotify = NULL;
}
if (pDevIns->Internal.s.pfnAsyncNotify)
{
pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_SUSPENDED;
(*pcAsync)++;
}
}
}
}
/**
* This function will notify all the devices and their
* attached drivers about the VM being powered off.
*
* @param pVM VM Handle.
*/
VMMR3DECL(void) PDMR3PowerOff(PVM pVM)
{
LogFlow(("PDMR3PowerOff:\n"));
/*
* The outer loop repeats until there are no more async requests.
*/
unsigned cAsync;
for (unsigned iLoop = 0; ; iLoop++)
{
/*
* Iterate thru the device instances and USB device instances,
* processing the drivers associated with those.
*
* The attached drivers are normally processed first. Some devices
* (like DevAHCI) though needs to be notified before the drivers so
* that it doesn't kick off any new requests after the drivers stopped
* taking any. (DrvVD changes to read-only in this particular case.)
*/
cAsync = 0;
for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
{
unsigned const cAsyncStart = cAsync;
if (pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION)
pdmR3PowerOffDev(pDevIns, &cAsync);
if (cAsync == cAsyncStart)
for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
if (!pdmR3PowerOffDrv(pDrvIns, &cAsync, pDevIns->pReg->szDeviceName, pDevIns->iInstance, pLun->iLun))
break;
if ( cAsync == cAsyncStart
&& !(pDevIns->pReg->fFlags & PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION))
pdmR3PowerOffDev(pDevIns, &cAsync);
}
#ifdef VBOX_WITH_USB
for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
{
unsigned const cAsyncStart = cAsync;
for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
if (!pdmR3PowerOffDrv(pDrvIns, &cAsync, pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance, pLun->iLun))
break;
if (cAsync == cAsyncStart)
pdmR3PowerOffUsb(pUsbIns, &cAsync);
}
#endif
if (!cAsync)
break;
/*
* Process requests.
*/
/** @todo This is utterly nuts and completely unsafe... will get back to it in a
* bit I hope... */
int rc = VMR3AsyncPdmNotificationWaitU(&pVM->pUVM->aCpus[0]);
AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc\n", rc));
rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY);
AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc\n", rc));
rc = VMR3ReqProcessU(pVM->pUVM, 0/*idDstCpu*/);
AssertReleaseMsg(rc == VINF_SUCCESS, ("%Rrc\n", rc));
}
/*
* Suspend all threads.
*/
pdmR3ThreadSuspendAll(pVM);
LogFlow(("PDMR3PowerOff: returns void\n"));
}
/**
* Queries the base interace of a device instance.
*
* The caller can use this to query other interfaces the device implements
* and use them to talk to the device.
*
* @returns VBox status code.
* @param pVM VM handle.
* @param pszDevice Device name.
* @param iInstance Device instance.
* @param ppBase Where to store the pointer to the base device interface on success.
* @remark We're not doing any locking ATM, so don't try call this at times when the
* device chain is known to be updated.
*/
VMMR3DECL(int) PDMR3QueryDevice(PVM pVM, const char *pszDevice, unsigned iInstance, PPDMIBASE *ppBase)
{
LogFlow(("PDMR3DeviceQuery: pszDevice=%p:{%s} iInstance=%u ppBase=%p\n", pszDevice, pszDevice, iInstance, ppBase));
/*
* Iterate registered devices looking for the device.
*/
size_t cchDevice = strlen(pszDevice);
for (PPDMDEV pDev = pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
{
if ( pDev->cchName == cchDevice
&& !memcmp(pDev->pReg->szDeviceName, pszDevice, cchDevice))
{
/*
* Iterate device instances.
*/
for (PPDMDEVINS pDevIns = pDev->pInstances; pDevIns; pDevIns = pDevIns->Internal.s.pPerDeviceNextR3)
{
if (pDevIns->iInstance == iInstance)
{
if (pDevIns->IBase.pfnQueryInterface)
{
*ppBase = &pDevIns->IBase;
LogFlow(("PDMR3DeviceQuery: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
return VINF_SUCCESS;
}
LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NO_IBASE\n"));
return VERR_PDM_DEVICE_INSTANCE_NO_IBASE;
}
}
LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NOT_FOUND\n"));
return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND;
}
}
LogFlow(("PDMR3QueryDevice: returns VERR_PDM_DEVICE_NOT_FOUND\n"));
return VERR_PDM_DEVICE_NOT_FOUND;
}
/**
* Queries the base interface of a device LUN.
*
* This differs from PDMR3QueryLun by that it returns the interface on the
* device and not the top level driver.
*
* @returns VBox status code.
* @param pVM VM Handle.
* @param pszDevice Device name.
* @param iInstance Device instance.
* @param iLun The Logical Unit to obtain the interface of.
* @param ppBase Where to store the base interface pointer.
* @remark We're not doing any locking ATM, so don't try call this at times when the
* device chain is known to be updated.
*/
VMMR3DECL(int) PDMR3QueryDeviceLun(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
{
LogFlow(("PDMR3QueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
pszDevice, pszDevice, iInstance, iLun, ppBase));
/*
* Find the LUN.
*/
PPDMLUN pLun;
int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
if (RT_SUCCESS(rc))
{
*ppBase = pLun->pBase;
LogFlow(("PDMR3QueryDeviceLun: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
return VINF_SUCCESS;
}
LogFlow(("PDMR3QueryDeviceLun: returns %Rrc\n", rc));
return rc;
}
/**
* Query the interface of the top level driver on a LUN.
*
* @returns VBox status code.
* @param pVM VM Handle.
* @param pszDevice Device name.
* @param iInstance Device instance.
* @param iLun The Logical Unit to obtain the interface of.
* @param ppBase Where to store the base interface pointer.
* @remark We're not doing any locking ATM, so don't try call this at times when the
* device chain is known to be updated.
*/
VMMR3DECL(int) PDMR3QueryLun(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
{
LogFlow(("PDMR3QueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
pszDevice, pszDevice, iInstance, iLun, ppBase));
/*
* Find the LUN.
*/
PPDMLUN pLun;
int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
if (RT_SUCCESS(rc))
{
if (pLun->pTop)
{
*ppBase = &pLun->pTop->IBase;
LogFlow(("PDMR3QueryLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
return VINF_SUCCESS;
}
rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
}
LogFlow(("PDMR3QueryLun: returns %Rrc\n", rc));
return rc;
}
/**
* Executes pending DMA transfers.
* Forced Action handler.
*
* @param pVM VM handle.
*/
VMMR3DECL(void) PDMR3DmaRun(PVM pVM)
{
/* Note! Not really SMP safe; restrict it to VCPU 0. */
if (VMMGetCpuId(pVM) != 0)
return;
if (VM_FF_TESTANDCLEAR(pVM, VM_FF_PDM_DMA))
{
if (pVM->pdm.s.pDmac)
{
bool fMore = pVM->pdm.s.pDmac->Reg.pfnRun(pVM->pdm.s.pDmac->pDevIns);
if (fMore)
VM_FF_SET(pVM, VM_FF_PDM_DMA);
}
}
}
/**
* Service a VMMCALLRING3_PDM_LOCK call.
*
* @returns VBox status code.
* @param pVM The VM handle.
*/
VMMR3DECL(int) PDMR3LockCall(PVM pVM)
{
return PDMR3CritSectEnterEx(&pVM->pdm.s.CritSect, true /* fHostCall */);
}
/**
* Registers the VMM device heap
*
* @returns VBox status code.
* @param pVM VM handle.
* @param GCPhys The physical address.
* @param pvHeap Ring-3 pointer.
* @param cbSize Size of the heap.
*/
VMMR3DECL(int) PDMR3RegisterVMMDevHeap(PVM pVM, RTGCPHYS GCPhys, RTR3PTR pvHeap, unsigned cbSize)
{
Assert(pVM->pdm.s.pvVMMDevHeap == NULL);
Log(("PDMR3RegisterVMMDevHeap %RGp %RHv %x\n", GCPhys, pvHeap, cbSize));
pVM->pdm.s.pvVMMDevHeap = pvHeap;
pVM->pdm.s.GCPhysVMMDevHeap = GCPhys;
pVM->pdm.s.cbVMMDevHeap = cbSize;
pVM->pdm.s.cbVMMDevHeapLeft = cbSize;
return VINF_SUCCESS;
}
/**
* Unregisters the VMM device heap
*
* @returns VBox status code.
* @param pVM VM handle.
* @param GCPhys The physical address.
*/
VMMR3DECL(int) PDMR3UnregisterVMMDevHeap(PVM pVM, RTGCPHYS GCPhys)
{
Assert(pVM->pdm.s.GCPhysVMMDevHeap == GCPhys);
Log(("PDMR3UnregisterVMMDevHeap %RGp\n", GCPhys));
pVM->pdm.s.pvVMMDevHeap = NULL;
pVM->pdm.s.GCPhysVMMDevHeap = NIL_RTGCPHYS;
pVM->pdm.s.cbVMMDevHeap = 0;
pVM->pdm.s.cbVMMDevHeapLeft = 0;
return VINF_SUCCESS;
}
/**
* Allocates memory from the VMM device heap
*
* @returns VBox status code.
* @param pVM VM handle.
* @param cbSize Allocation size.
* @param pv Ring-3 pointer. (out)
*/
VMMR3DECL(int) PDMR3VMMDevHeapAlloc(PVM pVM, unsigned cbSize, RTR3PTR *ppv)
{
#ifdef DEBUG_bird
if (!cbSize || cbSize > pVM->pdm.s.cbVMMDevHeapLeft)
return VERR_NO_MEMORY;
#else
AssertReturn(cbSize && cbSize <= pVM->pdm.s.cbVMMDevHeapLeft, VERR_NO_MEMORY);
#endif
Log(("PDMR3VMMDevHeapAlloc %x\n", cbSize));
/** @todo not a real heap as there's currently only one user. */
*ppv = pVM->pdm.s.pvVMMDevHeap;
pVM->pdm.s.cbVMMDevHeapLeft = 0;
return VINF_SUCCESS;
}
/**
* Frees memory from the VMM device heap
*
* @returns VBox status code.
* @param pVM VM handle.
* @param pv Ring-3 pointer.
*/
VMMR3DECL(int) PDMR3VMMDevHeapFree(PVM pVM, RTR3PTR pv)
{
Log(("PDMR3VMMDevHeapFree %RHv\n", pv));
/** @todo not a real heap as there's currently only one user. */
pVM->pdm.s.cbVMMDevHeapLeft = pVM->pdm.s.cbVMMDevHeap;
return VINF_SUCCESS;
}
/**
* Release the PDM lock if owned by the current VCPU
*
* @param pVM The VM to operate on.
*/
VMMR3DECL(void) PDMR3ReleaseOwnedLocks(PVM pVM)
{
while (PDMCritSectIsOwner(&pVM->pdm.s.CritSect))
PDMCritSectLeave(&pVM->pdm.s.CritSect);
}