Changeset 81616 in vbox for trunk/src/VBox/Devices/Samples
- Timestamp:
- Oct 31, 2019 7:43:07 PM (6 years ago)
- svn:sync-xref-src-repo-rev:
- 134373
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Devices/Samples/DevPlayground.cpp
r81591 r81616 52 52 { 53 53 /** The function number. */ 54 uint8_t iFun;54 uint8_t iFun; 55 55 /** Device function name. */ 56 char szName[31]; 56 char szName[31]; 57 /** MMIO region \#0 name. */ 58 char szMmio0[32]; 59 /** MMIO region \#2 name. */ 60 char szMmio2[32]; 61 /** The MMIO region \#0 handle. */ 62 IOMMMIOHANDLE hMmio0; 63 /** The MMIO region \#2 handle. */ 64 IOMMMIOHANDLE hMmio2; 57 65 } VBOXPLAYGROUNDDEVICEFUNCTION; 58 66 /** Pointer to a PCI function of the playground device. */ … … 78 86 *********************************************************************************************************************************/ 79 87 80 PDMBOTHCBDECL(int) devPlaygroundMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)88 static DECLCALLBACK(VBOXSTRICTRC) devPlaygroundMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb) 81 89 { 82 90 NOREF(pDevIns); 83 91 NOREF(pvUser); 84 NOREF( GCPhysAddr);92 NOREF(off); 85 93 NOREF(pv); 86 94 NOREF(cb); … … 89 97 90 98 91 PDMBOTHCBDECL(int) devPlaygroundMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void const *pv, unsigned cb)99 static DECLCALLBACK(VBOXSTRICTRC) devPlaygroundMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb) 92 100 { 93 101 NOREF(pDevIns); 94 102 NOREF(pvUser); 95 NOREF( GCPhysAddr);103 NOREF(off); 96 104 NOREF(pv); 97 105 NOREF(cb); … … 101 109 102 110 /** 103 * @callback_method_impl{FNPCIIOREGIONMAP}104 */105 static DECLCALLBACK(int) devPlaygroundMap(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev, uint32_t iRegion,106 RTGCPHYS GCPhysAddress, RTGCPHYS cb, PCIADDRESSSPACE enmType)107 {108 RT_NOREF(pPciDev, enmType, cb);109 110 switch (iRegion)111 {112 case 0:113 case 2:114 Assert( enmType == (PCIADDRESSSPACE)(PCI_ADDRESS_SPACE_MEM | PCI_ADDRESS_SPACE_BAR64)115 || enmType == (PCIADDRESSSPACE)(PCI_ADDRESS_SPACE_MEM_PREFETCH | PCI_ADDRESS_SPACE_BAR64));116 if (GCPhysAddress == NIL_RTGCPHYS)117 return VINF_SUCCESS; /* We ignore the unmap notification. */118 return PDMDevHlpMMIOExMap(pDevIns, pPciDev, iRegion, GCPhysAddress);119 120 default:121 /* We should never get here */122 AssertMsgFailedReturn(("Invalid PCI region param in map callback"), VERR_INTERNAL_ERROR);123 }124 }125 126 127 /**128 111 * @callback_method_impl{FNSSMDEVSAVEEXEC} 129 112 */ … … 131 114 { 132 115 PVBOXPLAYGROUNDDEVICE pThis = PDMDEVINS_2_DATA(pDevIns, PVBOXPLAYGROUNDDEVICE); 116 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3; 133 117 134 118 /* dummy (real devices would need to save their state here) */ … … 137 121 /* Demo of some API stuff - very unusual, think twice if there's no better 138 122 * solution which doesn't need API interaction. */ 139 HRESULT hrc = S_OK; 140 com::Bstr bstrSnapName; 141 com::Guid uuid(COM_IIDOF(ISnapshot)); 142 ISnapshot *pSnap = (ISnapshot *)PDMDevHlpQueryGenericUserObject(pDevIns, uuid.raw()); 143 if (pSnap) 123 #if 0 124 try 144 125 { 145 hrc = pSnap->COMGETTER(Name)(bstrSnapName.asOutParam()); 146 AssertComRCReturn(hrc, VERR_INVALID_STATE); 126 HRESULT hrc = S_OK; 127 com::Bstr bstrSnapName; 128 com::Guid uuid(COM_IIDOF(ISnapshot)); 129 ISnapshot *pSnap = (ISnapshot *)PDMDevHlpQueryGenericUserObject(pDevIns, uuid.raw()); 130 if (pSnap) 131 { 132 hrc = pSnap->COMGETTER(Name)(bstrSnapName.asOutParam()); 133 AssertComRCReturn(hrc, VERR_INVALID_STATE); 134 } 135 com::Utf8Str strSnapName(bstrSnapName); 136 pHlp->pfnSSMPutStrZ(pSSM, strSnapName.c_str()); 137 LogRel(("Playground: saving state of snapshot '%s', hrc=%Rhrc\n", strSnapName.c_str(), hrc)); 147 138 } 148 com::Utf8Str strSnapName(bstrSnapName); 149 SSMR3PutStrZ(pSSM, strSnapName.c_str()); 150 LogRel(("Playground: saving state of snapshot '%s', hrc=%Rhrc\n", strSnapName.c_str(), hrc)); 151 152 return VINF_SUCCESS; 153 } 139 catch (...) 140 { 141 AssertLogRelFailed(); 142 return VERR_UNEXPECTED_EXCEPTION; 143 } 144 #else 145 RT_NOREF(pSSM, pHlp); 146 #endif 147 148 return VINF_SUCCESS; 149 } 150 154 151 155 152 /** … … 159 156 { 160 157 PVBOXPLAYGROUNDDEVICE pThis = PDMDEVINS_2_DATA(pDevIns, PVBOXPLAYGROUNDDEVICE); 158 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3; 161 159 162 160 if (uVersion > PLAYGROUND_SSM_VERSION) … … 169 167 /* Reading the stuff written to saved state, just a demo. */ 170 168 char szSnapName[256]; 171 int rc = SSMR3GetStrZ(pSSM, szSnapName, sizeof(szSnapName));169 int rc = pHlp->pfnSSMGetStrZ(pSSM, szSnapName, sizeof(szSnapName)); 172 170 AssertRCReturn(rc, rc); 173 171 LogRel(("Playground: loading state of snapshot '%s'\n", szSnapName)); … … 178 176 179 177 /** 178 * @interface_method_impl{PDMDEVREG,pfnDestruct} 179 */ 180 static DECLCALLBACK(int) devPlaygroundDestruct(PPDMDEVINS pDevIns) 181 { 182 /* 183 * Check the versions here as well since the destructor is *always* called. 184 * THIS IS ALWAYS THE FIRST STATEMENT IN A DESTRUCTOR! 185 */ 186 PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns); 187 188 return VINF_SUCCESS; 189 } 190 191 192 /** 180 193 * @interface_method_impl{PDMDEVREG,pfnConstruct} 181 194 */ 182 195 static DECLCALLBACK(int) devPlaygroundConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg) 183 196 { 184 RT_NOREF(iInstance, pCfg);185 int rc = VINF_SUCCESS;186 187 197 /* 188 198 * Check that the device instance and device helper structures are compatible. 189 */ 190 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns); 199 * THIS IS ALWAYS THE FIRST STATEMENT IN A CONSTRUCTOR! 200 */ 201 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns); /* This must come first. */ 202 Assert(iInstance == 0); RT_NOREF(iInstance); 191 203 192 204 /* … … 199 211 */ 200 212 PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "Whatever1|NumFunctions|BigBAR0MB|BigBAR0GB|BigBAR2MB|BigBAR2GB", ""); 213 214 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3; 201 215 202 216 uint8_t uNumFunctions; 203 217 AssertCompile(RT_ELEMENTS(pThis->aPciFuns) <= RT_ELEMENTS(pDevIns->apPciDevs)); 204 rc = CFGMR3QueryU8Def(pCfg, "NumFunctions", &uNumFunctions, RT_ELEMENTS(pThis->aPciFuns));218 int rc = pHlp->pfnCFGMQueryU8Def(pCfg, "NumFunctions", &uNumFunctions, RT_ELEMENTS(pThis->aPciFuns)); 205 219 if (RT_FAILURE(rc)) 206 220 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to query integer value \"NumFunctions\"")); … … 210 224 RTGCPHYS cbFirstBAR; 211 225 uint16_t uBigBAR0GB; 212 rc = CFGMR3QueryU16Def(pCfg, "BigBAR0GB", &uBigBAR0GB, 0); /* Default to nothing. */226 rc = pHlp->pfnCFGMQueryU16Def(pCfg, "BigBAR0GB", &uBigBAR0GB, 0); /* Default to nothing. */ 213 227 if (RT_FAILURE(rc)) 214 228 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to query integer value \"BigBAR0GB\"")); … … 221 235 { 222 236 uint16_t uBigBAR0MB; 223 rc = CFGMR3QueryU16Def(pCfg, "BigBAR0MB", &uBigBAR0MB, 8); /* 8 MB default. */237 rc = pHlp->pfnCFGMQueryU16Def(pCfg, "BigBAR0MB", &uBigBAR0MB, 8); /* 8 MB default. */ 224 238 if (RT_FAILURE(rc)) 225 239 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to query integer value \"BigBAR0MB\"")); … … 231 245 RTGCPHYS cbSecondBAR; 232 246 uint16_t uBigBAR2GB; 233 rc = CFGMR3QueryU16Def(pCfg, "BigBAR2GB", &uBigBAR2GB, 0); /* Default to nothing. */247 rc = pHlp->pfnCFGMQueryU16Def(pCfg, "BigBAR2GB", &uBigBAR2GB, 0); /* Default to nothing. */ 234 248 if (RT_FAILURE(rc)) 235 249 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to query integer value \"BigBAR2GB\"")); … … 242 256 { 243 257 uint16_t uBigBAR2MB; 244 rc = CFGMR3QueryU16Def(pCfg, "BigBAR2MB", &uBigBAR2MB, 16); /* 16 MB default. */258 rc = pHlp->pfnCFGMQueryU16Def(pCfg, "BigBAR2MB", &uBigBAR2MB, 16); /* 16 MB default. */ 245 259 if (RT_FAILURE(rc)) 246 260 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to query integer value \"BigBAR2MB\"")); … … 276 290 /* First region. */ 277 291 RTGCPHYS const cbFirst = iPciFun == 0 ? cbFirstBAR : iPciFun * _4K; 278 rc = PDMDevHlpPCIIORegionRegisterEx(pDevIns, pPciDev, 0, cbFirst,279 (PCIADDRESSSPACE)( PCI_ADDRESS_SPACE_MEM | PCI_ADDRESS_SPACE_BAR64280 | (iPciFun == 0 ? PCI_ADDRESS_SPACE_MEM_PREFETCH : 0)),281 devPlaygroundMap);292 RTStrPrintf(pFun->szMmio0, sizeof(pFun->szMmio0), "PG-F%d-BAR0", iPciFun); 293 rc = PDMDevHlpMmioCreate(pDevIns, cbFirst, pPciDev, 0 /*iPciRegion*/, 294 devPlaygroundMMIOWrite, devPlaygroundMMIORead, NULL /*pvUser*/, 295 IOMMMIO_FLAGS_READ_PASSTHRU | IOMMMIO_FLAGS_WRITE_PASSTHRU, pFun->szMmio0, &pFun->hMmio0); 282 296 AssertLogRelRCReturn(rc, rc); 283 char *pszRegionName = NULL; 284 RTStrAPrintf(&pszRegionName, "PG-F%d-BAR0", iPciFun); 285 Assert(pszRegionName); 286 rc = PDMDevHlpMMIOExPreRegister(pDevIns, pPciDev, 0, cbFirst, 287 IOMMMIO_FLAGS_READ_PASSTHRU | IOMMMIO_FLAGS_WRITE_PASSTHRU, pszRegionName, 288 NULL /*pvUser*/, devPlaygroundMMIOWrite, devPlaygroundMMIORead, NULL /*pfnFill*/, 289 NIL_RTR0PTR /*pvUserR0*/, NULL /*pszWriteR0*/, NULL /*pszReadR0*/, NULL /*pszFillR0*/, 290 NIL_RTRCPTR /*pvUserRC*/, NULL /*pszWriteRC*/, NULL /*pszReadRC*/, NULL /*pszFillRC*/); 297 298 rc = PDMDevHlpPCIIORegionRegisterMmioEx(pDevIns, pPciDev, 0, cbFirst, 299 (PCIADDRESSSPACE)( PCI_ADDRESS_SPACE_MEM | PCI_ADDRESS_SPACE_BAR64 300 | (iPciFun == 0 ? PCI_ADDRESS_SPACE_MEM_PREFETCH : 0)), 301 pFun->hMmio0, NULL); 291 302 AssertLogRelRCReturn(rc, rc); 292 303 293 304 /* Second region. */ 294 305 RTGCPHYS const cbSecond = iPciFun == 0 ? cbSecondBAR : iPciFun * _32K; 295 rc = PDMDevHlpPCIIORegionRegisterEx(pDevIns, pPciDev, 2, cbSecond,296 (PCIADDRESSSPACE)( PCI_ADDRESS_SPACE_MEM | PCI_ADDRESS_SPACE_BAR64297 | (iPciFun == 0 ? PCI_ADDRESS_SPACE_MEM_PREFETCH : 0)),298 devPlaygroundMap);306 RTStrPrintf(pFun->szMmio2, sizeof(pFun->szMmio2), "PG-F%d-BAR2", iPciFun); 307 rc = PDMDevHlpMmioCreate(pDevIns, cbSecond, pPciDev, 2 << 16 /*iPciRegion*/, 308 devPlaygroundMMIOWrite, devPlaygroundMMIORead, NULL /*pvUser*/, 309 IOMMMIO_FLAGS_READ_PASSTHRU | IOMMMIO_FLAGS_WRITE_PASSTHRU, pFun->szMmio2, &pFun->hMmio2); 299 310 AssertLogRelRCReturn(rc, rc); 300 pszRegionName = NULL; 301 RTStrAPrintf(&pszRegionName, "PG-F%d-BAR2", iPciFun); 302 Assert(pszRegionName); 303 rc = PDMDevHlpMMIOExPreRegister(pDevIns, pPciDev, 2, cbSecond, 304 IOMMMIO_FLAGS_READ_PASSTHRU | IOMMMIO_FLAGS_WRITE_PASSTHRU, pszRegionName, 305 NULL /*pvUser*/, devPlaygroundMMIOWrite, devPlaygroundMMIORead, NULL /*pfnFill*/, 306 NIL_RTR0PTR /*pvUserR0*/, NULL /*pszWriteR0*/, NULL /*pszReadR0*/, NULL /*pszFillR0*/, 307 NIL_RTRCPTR /*pvUserRC*/, NULL /*pszWriteRC*/, NULL /*pszReadRC*/, NULL /*pszFillRC*/); 311 312 rc = PDMDevHlpPCIIORegionRegisterMmioEx(pDevIns, pPciDev, 2, cbSecond, 313 (PCIADDRESSSPACE)( PCI_ADDRESS_SPACE_MEM | PCI_ADDRESS_SPACE_BAR64 314 | (iPciFun == 0 ? PCI_ADDRESS_SPACE_MEM_PREFETCH : 0)), 315 pFun->hMmio2, NULL); 308 316 AssertLogRelRCReturn(rc, rc); 309 317 … … 324 332 325 333 /** 326 * @interface_method_impl{PDMDEVREG,pfnDestruct}327 */328 static DECLCALLBACK(int) devPlaygroundDestruct(PPDMDEVINS pDevIns)329 {330 /*331 * Check the versions here as well since the destructor is *always* called.332 */333 PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);334 335 return VINF_SUCCESS;336 }337 338 339 /**340 334 * The device registration structure. 341 335 */ … … 345 339 /* .uReserved0 = */ 0, 346 340 /* .szName = */ "playground", 347 /* .fFlags = */ PDM_DEVREG_FLAGS_DEFAULT_BITS ,341 /* .fFlags = */ PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_NEW_STYLE, 348 342 /* .fClass = */ PDM_DEVREG_CLASS_MISC, 349 343 /* .cMaxInstances = */ 1,
Note:
See TracChangeset
for help on using the changeset viewer.