PDM.cpp revision 7521bd76d764c3c9534c162e2a37fefb446f2071
a0f8619bc2bbe3614578c21b5b50a88d2841e7aavboxsync * PDM - Pluggable Device Manager.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * available from http://www.virtualbox.org. This file is free software;
a0f8619bc2bbe3614578c21b5b50a88d2841e7aavboxsync * you can redistribute it and/or modify it under the terms of the GNU
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * General Public License (GPL) as published by the Free Software
a0f8619bc2bbe3614578c21b5b50a88d2841e7aavboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a0f8619bc2bbe3614578c21b5b50a88d2841e7aavboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a0f8619bc2bbe3614578c21b5b50a88d2841e7aavboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
a0f8619bc2bbe3614578c21b5b50a88d2841e7aavboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
a0f8619bc2bbe3614578c21b5b50a88d2841e7aavboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
a0f8619bc2bbe3614578c21b5b50a88d2841e7aavboxsync * additional information or have any questions.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync/** @page pg_pdm PDM - The Pluggable Device Manager
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * VBox is designed to be very configurable, i.e. the ability to select
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * virtual devices and configure them uniquely for a VM. For this reason
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * virtual devices are not statically linked with the VMM but loaded and
0c437bb10c61b229407a7517efde04dfe3b1e4a1vboxsync * linked at runtime thru the Configuration Manager (CFGM). PDM will use
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync * CFGM to enumerate devices which needs loading and instantiation.
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync * @section sec_pdm_dev The Pluggable Device
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * Devices register themselves when the module containing them is loaded.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * PDM will call an entry point 'VBoxDevicesRegister' when loading a device
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync * module. The device module will then use the supplied callback table to
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * check the VMM version and to register its devices. Each device have an
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * unique (for the configured VM) name (string). The name is not only used
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * in PDM but in CFGM - to organize device and device instance settings - and
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * by anyone who wants to do ioctls to the device.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * When all device modules have been successfully loaded PDM will instantiate
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * those devices which are configured for the VM. Mark that this might mean
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * creating several instances of some devices. When instantiating a device
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * PDM provides device instance memory and a callback table with the VM APIs
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * which the device instance is trusted with.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * Some devices are trusted devices, most are not. The trusted devices are
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * an integrated part of the VM and can obtain the VM handle from their
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * device instance handles, thus enabling them to call any VM api. Untrusted
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * devices are can only use the callbacks provided during device
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * instantiation.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * The guest context extention (optional) of a device is initialized as part
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * of the GC init. A device marks in it's registration structure that it have
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * a GC part, in which module and which name the entry point have. PDM will
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * use its loader facilities to load this module into GC and to find the
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * specified entry point.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * When writing a GC extention the programmer must keep in mind that this
22ec733a5e041fcdfe02fce2eafc9faf8b0077ddvboxsync * code will be relocated, so that using global/static pointer variables
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * won't work.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * @section sec_pdm_drv The Pluggable Drivers
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * The VM devices are often accessing host hardware or OS facilities. For
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * most devices these facilities can be abstracted in one or more levels.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * These abstractions are called drivers.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * For instance take a DVD/CD drive. This can be connected to a SCSI
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * controller, EIDE controller or SATA controller. The basics of the
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * DVD/CD drive implementation remains the same - eject, insert,
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * read, seek, and such. (For the scsi case, you might wanna speak SCSI
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * directly to, but that can of course be fixed.) So, it makes much sense to
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * have a generic CD/DVD driver which implements this.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * Then the media 'inserted' into the DVD/CD drive can be a ISO image, or
230bd8589bba39933ac5ec21482d6186d675e604vboxsync * it can be read from a real CD or DVD drive (there are probably other
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * custom formats someone could desire to read or construct too). So, it
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * would make sense to have abstracted interfaces for dealing with this
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * in a generic way so the cdrom unit doesn't have to implement it all.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * Thus we have created the CDROM/DVD media driver family.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * So, for this example the IDE controller #1 (i.e. secondary) will have
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * the DVD/CD Driver attached to it's LUN #0 (master). When a media is mounted
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * the DVD/CD Driver will have a ISO, NativeCD, NativeDVD or RAW (media) Driver
230bd8589bba39933ac5ec21482d6186d675e604vboxsync * attached.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * It is possible to configure many levels of drivers inserting filters, loggers,
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * or whatever you desire into the chain.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * @subsection sec_pdm_drv_interfaces Interfaces
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * The pluggable drivers exposes one standard interface (callback table) which
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * is used to construct, destruct, attach, detach, and query other interfaces.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * A device will query the interfaces required for it's operation during init
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * and hotplug. PDM will query some interfaces during runtime mounting too.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * ... list interfaces ...
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync/*******************************************************************************
a0f8619bc2bbe3614578c21b5b50a88d2841e7aavboxsync* Header Files *
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync*******************************************************************************/
8bed792bc65abd39393889351f22263ce6c289bfvboxsync/*******************************************************************************
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync* Defined Constants And Macros *
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync*******************************************************************************/
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync/** The PDM saved state version. */
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync/*******************************************************************************
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync* Internal Functions *
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync*******************************************************************************/
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsyncstatic DECLCALLBACK(int) pdmR3Save(PVM pVM, PSSMHANDLE pSSM);
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsyncstatic DECLCALLBACK(int) pdmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version);
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsyncstatic DECLCALLBACK(int) pdmR3LoadPrep(PVM pVM, PSSMHANDLE pSSM);
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsyncstatic DECLCALLBACK(void) pdmR3PollerTimer(PVM pVM, PTMTIMER pTimer, void *pvUser);
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * Initializes the PDM part of the UVM.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * This doesn't really do much right now but has to be here for the sake
9ee436b6765f11cddb90819b9c4fc67899ba479bvboxsync * of completeness.
a0f8619bc2bbe3614578c21b5b50a88d2841e7aavboxsync * @returns VBox status code.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * @param pUVM Pointer to the user mode VM structure.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync AssertCompile(sizeof(pUVM->pdm.s) <= sizeof(pUVM->pdm.padding));
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync AssertRelease(sizeof(pUVM->pdm.s) <= sizeof(pUVM->pdm.padding));
a0f8619bc2bbe3614578c21b5b50a88d2841e7aavboxsync * Initializes the PDM.
a0f8619bc2bbe3614578c21b5b50a88d2841e7aavboxsync * @returns VBox status code.
a0f8619bc2bbe3614578c21b5b50a88d2841e7aavboxsync * @param pVM The VM to operate on.
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync * Assert alignment and sizes.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync AssertRelease(sizeof(pVM->pdm.s) <= sizeof(pVM->pdm.padding));
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * Init the structure.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync int rc = TMR3TimerCreateInternal(pVM, TMCLOCK_VIRTUAL, pdmR3PollerTimer, NULL, "PDM Poller", &pVM->pdm.s.pTimerPollers);
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * Initialize sub compontents.
a0f8619bc2bbe3614578c21b5b50a88d2841e7aavboxsync rc = PDMR3CritSectInit(pVM, &pVM->pdm.s.CritSect, "PDM");
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * Register the saved state data unit.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync rc = SSMR3RegisterInternal(pVM, "pdm", 1, PDM_SAVED_STATE_VERSION, 128,
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * Cleanup and return failure.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * Applies relocations to data and code managed by this
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * component. This function will be called at init and
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * whenever the VMM need to relocate it self inside the GC.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * @param pVM VM handle.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * @param offDelta Relocation delta relative to old location.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * @remark The loader subcomponent is relocated by PDMR3LdrRelocate() very
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsync * early in the relocation phase.
632b4638bd18092c6b8edb4e1028c9be112f5076vboxsyncPDMR3DECL(void) PDMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
while (pDrvIns)
pdmR3TermLuns(pVM, pDevIns->Internal.s.pLunsHC, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance);
return VINF_SUCCESS;
uint32_t i = 0;
for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextHC, i++)
return VINF_SUCCESS;
return rc;
if (fInterruptPending)
fInterruptPending = 0;
return rc;
if (fInterruptPending)
return rc;
if (fDMAPending)
uint32_t i = 0;
return rc;
if (u32Sep != i)
AssertMsgFailedReturn(("Out of seqence. u32Sep=%#x i=%#x\n", u32Sep, i), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
return rc;
return rc;
if (!pDevIns)
if (pDevIns)
LogRel(("Device '%s'/%d not found in saved state\n", pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
return VINF_SUCCESS;
pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
#ifdef VBOX_WITH_USB
pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
#ifdef VBOX_WITH_USB
pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
#ifdef VBOX_WITH_USB
pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
#ifdef VBOX_WITH_USB
pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
#ifdef VBOX_WITH_USB
pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
PDMR3DECL(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));
for (PPDMDEVINS pDevIns = pDev->pInstances; pDevIns; pDevIns = pDevIns->Internal.s.pPerDeviceNextHC)
return VINF_SUCCESS;
return VERR_PDM_DEVICE_NOT_FOUND;
PDMR3DECL(int) PDMR3QueryDeviceLun(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
return VINF_SUCCESS;
return rc;
PDMR3DECL(int) PDMR3QueryLun(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
return VINF_SUCCESS;
return rc;
if (fMore)
if (iPoller)
while (iPoller-- > 0)