[37802] | 1 | /* $Id: PCIRawDevImpl.cpp 98274 2023-01-24 11:19:16Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * VirtualBox Driver Interface to raw PCI device.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2010-2023 Oracle and/or its affiliates.
|
---|
[37802] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
| 25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[37802] | 26 | */
|
---|
[45029] | 27 |
|
---|
[67914] | 28 | #define LOG_GROUP LOG_GROUP_DEV_PCI_RAW
|
---|
| 29 | #include "LoggingNew.h"
|
---|
| 30 |
|
---|
[42551] | 31 | #include "PCIRawDevImpl.h"
|
---|
| 32 | #include "PCIDeviceAttachmentImpl.h"
|
---|
[37802] | 33 | #include "ConsoleImpl.h"
|
---|
| 34 |
|
---|
| 35 | // generated header for events
|
---|
| 36 | #include "VBoxEvents.h"
|
---|
| 37 |
|
---|
[76760] | 38 | #include <VBox/err.h>
|
---|
| 39 |
|
---|
| 40 |
|
---|
[37802] | 41 | /**
|
---|
| 42 | * PCI raw driver instance data.
|
---|
| 43 | */
|
---|
| 44 | typedef struct DRVMAINPCIRAWDEV
|
---|
| 45 | {
|
---|
| 46 | /** Pointer to the real PCI raw object. */
|
---|
[42551] | 47 | PCIRawDev *pPCIRawDev;
|
---|
[37802] | 48 | /** Pointer to the driver instance structure. */
|
---|
| 49 | PPDMDRVINS pDrvIns;
|
---|
| 50 | /** Our PCI device connector interface. */
|
---|
| 51 | PDMIPCIRAWCONNECTOR IConnector;
|
---|
| 52 |
|
---|
| 53 | } DRVMAINPCIRAWDEV, *PDRVMAINPCIRAWDEV;
|
---|
| 54 |
|
---|
| 55 | //
|
---|
| 56 | // constructor / destructor
|
---|
| 57 | //
|
---|
[42551] | 58 | PCIRawDev::PCIRawDev(Console *console)
|
---|
[81355] | 59 | : mParent(console),
|
---|
| 60 | mpDrv(NULL)
|
---|
[37802] | 61 | {
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[42551] | 64 | PCIRawDev::~PCIRawDev()
|
---|
[37802] | 65 | {
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | /**
|
---|
| 69 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
| 70 | */
|
---|
[42551] | 71 | DECLCALLBACK(void *) PCIRawDev::drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
[37802] | 72 | {
|
---|
| 73 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
| 74 | PDRVMAINPCIRAWDEV pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINPCIRAWDEV);
|
---|
| 75 |
|
---|
| 76 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
|
---|
| 77 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIPCIRAWCONNECTOR, &pThis->IConnector);
|
---|
| 78 |
|
---|
| 79 | return NULL;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 | /**
|
---|
[65103] | 84 | * @interface_method_impl{PDMIPCIRAWCONNECTOR,pfnDeviceConstructComplete}
|
---|
[37802] | 85 | */
|
---|
[42551] | 86 | DECLCALLBACK(int) PCIRawDev::drvDeviceConstructComplete(PPDMIPCIRAWCONNECTOR pInterface, const char *pcszName,
|
---|
| 87 | uint32_t uHostPCIAddress, uint32_t uGuestPCIAddress,
|
---|
[98274] | 88 | int vrc)
|
---|
[37802] | 89 | {
|
---|
| 90 | PDRVMAINPCIRAWDEV pThis = RT_FROM_CPP_MEMBER(pInterface, DRVMAINPCIRAWDEV, IConnector);
|
---|
[42551] | 91 | Console *pConsole = pThis->pPCIRawDev->getParent();
|
---|
[51612] | 92 | const ComPtr<IMachine>& machine = pConsole->i_machine();
|
---|
[37802] | 93 | ComPtr<IVirtualBox> vbox;
|
---|
| 94 |
|
---|
[62372] | 95 | HRESULT hrc = machine->COMGETTER(Parent)(vbox.asOutParam());
|
---|
[62379] | 96 | Assert(SUCCEEDED(hrc)); NOREF(hrc);
|
---|
[37802] | 97 |
|
---|
| 98 | ComPtr<IEventSource> es;
|
---|
| 99 | hrc = vbox->COMGETTER(EventSource)(es.asOutParam());
|
---|
| 100 | Assert(SUCCEEDED(hrc));
|
---|
| 101 |
|
---|
| 102 | Bstr bstrId;
|
---|
| 103 | hrc = machine->COMGETTER(Id)(bstrId.asOutParam());
|
---|
| 104 | Assert(SUCCEEDED(hrc));
|
---|
| 105 |
|
---|
[42551] | 106 | ComObjPtr<PCIDeviceAttachment> pda;
|
---|
[37802] | 107 | BstrFmt bstrName(pcszName);
|
---|
| 108 | pda.createObject();
|
---|
[42551] | 109 | pda->init(machine, bstrName, uHostPCIAddress, uGuestPCIAddress, TRUE);
|
---|
[37802] | 110 |
|
---|
| 111 | Bstr msg("");
|
---|
[98274] | 112 | if (RT_FAILURE(vrc))
|
---|
| 113 | msg.printf("runtime error %Rrc", vrc);
|
---|
[37802] | 114 |
|
---|
[98274] | 115 | ::FireHostPCIDevicePlugEvent(es, bstrId.raw(), true /* plugged */, RT_SUCCESS_NP(vrc) /* success */, pda, msg.raw());
|
---|
[37802] | 116 |
|
---|
| 117 | return VINF_SUCCESS;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 |
|
---|
| 121 | /**
|
---|
[45029] | 122 | * @interface_method_impl{PDMDRVREG,pfnReset}
|
---|
[37802] | 123 | */
|
---|
[45029] | 124 | DECLCALLBACK(void) PCIRawDev::drvDestruct(PPDMDRVINS pDrvIns)
|
---|
[37802] | 125 | {
|
---|
[45029] | 126 | PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
|
---|
[45030] | 127 | PDRVMAINPCIRAWDEV pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINPCIRAWDEV);
|
---|
[45029] | 128 |
|
---|
[45030] | 129 | if (pThis->pPCIRawDev)
|
---|
| 130 | pThis->pPCIRawDev->mpDrv = NULL;
|
---|
[37802] | 131 | }
|
---|
| 132 |
|
---|
| 133 |
|
---|
| 134 | /**
|
---|
[45029] | 135 | * @interface_method_impl{PDMDRVREG,pfnConstruct}
|
---|
[37802] | 136 | */
|
---|
[42551] | 137 | DECLCALLBACK(int) PCIRawDev::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle, uint32_t fFlags)
|
---|
[37802] | 138 | {
|
---|
[63239] | 139 | RT_NOREF(fFlags);
|
---|
[45029] | 140 | PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
|
---|
[45030] | 141 | PDRVMAINPCIRAWDEV pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINPCIRAWDEV);
|
---|
[37802] | 142 |
|
---|
| 143 | /*
|
---|
| 144 | * Validate configuration.
|
---|
| 145 | */
|
---|
| 146 | if (!CFGMR3AreValuesValid(pCfgHandle, "Object\0"))
|
---|
| 147 | return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
|
---|
| 148 |
|
---|
| 149 | AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
|
---|
| 150 | ("Configuration error: Not possible to attach anything to this driver!\n"),
|
---|
| 151 | VERR_PDM_DRVINS_NO_ATTACH);
|
---|
| 152 |
|
---|
| 153 | /*
|
---|
| 154 | * IBase.
|
---|
| 155 | */
|
---|
[42551] | 156 | pDrvIns->IBase.pfnQueryInterface = PCIRawDev::drvQueryInterface;
|
---|
[37802] | 157 |
|
---|
| 158 | /*
|
---|
| 159 | * IConnector.
|
---|
| 160 | */
|
---|
[45030] | 161 | pThis->IConnector.pfnDeviceConstructComplete = PCIRawDev::drvDeviceConstructComplete;
|
---|
[37802] | 162 |
|
---|
| 163 | /*
|
---|
| 164 | * Get the object pointer and update the mpDrv member.
|
---|
| 165 | */
|
---|
| 166 | void *pv;
|
---|
[98274] | 167 | int vrc = CFGMR3QueryPtr(pCfgHandle, "Object", &pv);
|
---|
| 168 | if (RT_FAILURE(vrc))
|
---|
[37802] | 169 | {
|
---|
[98274] | 170 | AssertMsgFailed(("Configuration error: No \"Object\" value! vrc=%Rrc\n", vrc));
|
---|
| 171 | return vrc;
|
---|
[37802] | 172 | }
|
---|
| 173 |
|
---|
[45030] | 174 | pThis->pPCIRawDev = (PCIRawDev *)pv;
|
---|
| 175 | pThis->pPCIRawDev->mpDrv = pThis;
|
---|
[37802] | 176 |
|
---|
| 177 | return VINF_SUCCESS;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | /**
|
---|
| 181 | * Main raw PCI driver registration record.
|
---|
| 182 | */
|
---|
[42551] | 183 | const PDMDRVREG PCIRawDev::DrvReg =
|
---|
[37802] | 184 | {
|
---|
| 185 | /* u32Version */
|
---|
| 186 | PDM_DRVREG_VERSION,
|
---|
| 187 | /* szName */
|
---|
| 188 | "MainPciRaw",
|
---|
| 189 | /* szRCMod */
|
---|
| 190 | "",
|
---|
| 191 | /* szR0Mod */
|
---|
| 192 | "",
|
---|
| 193 | /* pszDescription */
|
---|
| 194 | "Main PCI raw driver (Main as in the API).",
|
---|
| 195 | /* fFlags */
|
---|
| 196 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
| 197 | /* fClass. */
|
---|
| 198 | PDM_DRVREG_CLASS_PCIRAW,
|
---|
| 199 | /* cMaxInstances */
|
---|
[40282] | 200 | ~0U,
|
---|
[37802] | 201 | /* cbInstance */
|
---|
| 202 | sizeof(DRVMAINPCIRAWDEV),
|
---|
| 203 | /* pfnConstruct */
|
---|
[42551] | 204 | PCIRawDev::drvConstruct,
|
---|
[37802] | 205 | /* pfnDestruct */
|
---|
[42551] | 206 | PCIRawDev::drvDestruct,
|
---|
[37802] | 207 | /* pfnRelocate */
|
---|
| 208 | NULL,
|
---|
| 209 | /* pfnIOCtl */
|
---|
| 210 | NULL,
|
---|
| 211 | /* pfnPowerOn */
|
---|
| 212 | NULL,
|
---|
| 213 | /* pfnReset */
|
---|
[63239] | 214 | NULL,
|
---|
[37802] | 215 | /* pfnSuspend */
|
---|
| 216 | NULL,
|
---|
| 217 | /* pfnResume */
|
---|
| 218 | NULL,
|
---|
| 219 | /* pfnAttach */
|
---|
| 220 | NULL,
|
---|
| 221 | /* pfnDetach */
|
---|
| 222 | NULL,
|
---|
| 223 | /* pfnPowerOff */
|
---|
| 224 | NULL,
|
---|
| 225 | /* pfnSoftReset */
|
---|
| 226 | NULL,
|
---|
| 227 | /* u32EndVersion */
|
---|
| 228 | PDM_DRVREG_VERSION
|
---|
| 229 | };
|
---|
[45029] | 230 |
|
---|