VirtualBox

Changeset 81616 in vbox for trunk/src/VBox/Devices/Samples


Ignore:
Timestamp:
Oct 31, 2019 7:43:07 PM (6 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
134373
Message:

DevPlayground,pdmdev.h: Converted the playground sample device. (untested) bugref:9218

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Samples/DevPlayground.cpp

    r81591 r81616  
    5252{
    5353    /** The function number. */
    54     uint8_t     iFun;
     54    uint8_t         iFun;
    5555    /** 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;
    5765} VBOXPLAYGROUNDDEVICEFUNCTION;
    5866/** Pointer to a PCI function of the playground device. */
     
    7886*********************************************************************************************************************************/
    7987
    80 PDMBOTHCBDECL(int) devPlaygroundMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
     88static DECLCALLBACK(VBOXSTRICTRC) devPlaygroundMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb)
    8189{
    8290    NOREF(pDevIns);
    8391    NOREF(pvUser);
    84     NOREF(GCPhysAddr);
     92    NOREF(off);
    8593    NOREF(pv);
    8694    NOREF(cb);
     
    8997
    9098
    91 PDMBOTHCBDECL(int) devPlaygroundMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void const *pv, unsigned cb)
     99static DECLCALLBACK(VBOXSTRICTRC) devPlaygroundMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb)
    92100{
    93101    NOREF(pDevIns);
    94102    NOREF(pvUser);
    95     NOREF(GCPhysAddr);
     103    NOREF(off);
    96104    NOREF(pv);
    97105    NOREF(cb);
     
    101109
    102110/**
    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 /**
    128111 * @callback_method_impl{FNSSMDEVSAVEEXEC}
    129112 */
     
    131114{
    132115    PVBOXPLAYGROUNDDEVICE pThis = PDMDEVINS_2_DATA(pDevIns, PVBOXPLAYGROUNDDEVICE);
     116    PCPDMDEVHLPR3         pHlp  = pDevIns->pHlpR3;
    133117
    134118    /* dummy (real devices would need to save their state here) */
     
    137121    /* Demo of some API stuff - very unusual, think twice if there's no better
    138122     * 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
    144125    {
    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));
    147138    }
    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
     145RT_NOREF(pSSM, pHlp);
     146#endif
     147
     148    return VINF_SUCCESS;
     149}
     150
    154151
    155152/**
     
    159156{
    160157    PVBOXPLAYGROUNDDEVICE pThis = PDMDEVINS_2_DATA(pDevIns, PVBOXPLAYGROUNDDEVICE);
     158    PCPDMDEVHLPR3         pHlp  = pDevIns->pHlpR3;
    161159
    162160    if (uVersion > PLAYGROUND_SSM_VERSION)
     
    169167    /* Reading the stuff written to saved state, just a demo. */
    170168    char szSnapName[256];
    171     int rc = SSMR3GetStrZ(pSSM, szSnapName, sizeof(szSnapName));
     169    int rc = pHlp->pfnSSMGetStrZ(pSSM, szSnapName, sizeof(szSnapName));
    172170    AssertRCReturn(rc, rc);
    173171    LogRel(("Playground: loading state of snapshot '%s'\n", szSnapName));
     
    178176
    179177/**
     178 * @interface_method_impl{PDMDEVREG,pfnDestruct}
     179 */
     180static 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/**
    180193 * @interface_method_impl{PDMDEVREG,pfnConstruct}
    181194 */
    182195static DECLCALLBACK(int) devPlaygroundConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
    183196{
    184     RT_NOREF(iInstance, pCfg);
    185     int rc = VINF_SUCCESS;
    186 
    187197    /*
    188198     * 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);
    191203
    192204    /*
     
    199211     */
    200212    PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "Whatever1|NumFunctions|BigBAR0MB|BigBAR0GB|BigBAR2MB|BigBAR2GB", "");
     213
     214    PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
    201215
    202216    uint8_t uNumFunctions;
    203217    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));
    205219    if (RT_FAILURE(rc))
    206220        return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to query integer value \"NumFunctions\""));
     
    210224    RTGCPHYS cbFirstBAR;
    211225    uint16_t uBigBAR0GB;
    212     rc = CFGMR3QueryU16Def(pCfg, "BigBAR0GB", &uBigBAR0GB, 0);  /* Default to nothing. */
     226    rc = pHlp->pfnCFGMQueryU16Def(pCfg, "BigBAR0GB", &uBigBAR0GB, 0);  /* Default to nothing. */
    213227    if (RT_FAILURE(rc))
    214228        return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to query integer value \"BigBAR0GB\""));
     
    221235    {
    222236        uint16_t uBigBAR0MB;
    223         rc = CFGMR3QueryU16Def(pCfg, "BigBAR0MB", &uBigBAR0MB, 8);  /* 8 MB default. */
     237        rc = pHlp->pfnCFGMQueryU16Def(pCfg, "BigBAR0MB", &uBigBAR0MB, 8);  /* 8 MB default. */
    224238        if (RT_FAILURE(rc))
    225239            return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to query integer value \"BigBAR0MB\""));
     
    231245    RTGCPHYS cbSecondBAR;
    232246    uint16_t uBigBAR2GB;
    233     rc = CFGMR3QueryU16Def(pCfg, "BigBAR2GB", &uBigBAR2GB, 0);  /* Default to nothing. */
     247    rc = pHlp->pfnCFGMQueryU16Def(pCfg, "BigBAR2GB", &uBigBAR2GB, 0);  /* Default to nothing. */
    234248    if (RT_FAILURE(rc))
    235249        return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to query integer value \"BigBAR2GB\""));
     
    242256    {
    243257        uint16_t uBigBAR2MB;
    244         rc = CFGMR3QueryU16Def(pCfg, "BigBAR2MB", &uBigBAR2MB, 16); /* 16 MB default. */
     258        rc = pHlp->pfnCFGMQueryU16Def(pCfg, "BigBAR2MB", &uBigBAR2MB, 16); /* 16 MB default. */
    245259        if (RT_FAILURE(rc))
    246260            return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to query integer value \"BigBAR2MB\""));
     
    276290        /* First region. */
    277291        RTGCPHYS const cbFirst = iPciFun == 0 ? cbFirstBAR : iPciFun * _4K;
    278         rc = PDMDevHlpPCIIORegionRegisterEx(pDevIns, pPciDev, 0, cbFirst,
    279                                             (PCIADDRESSSPACE)(  PCI_ADDRESS_SPACE_MEM | PCI_ADDRESS_SPACE_BAR64
    280                                                               | (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);
    282296        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);
    291302        AssertLogRelRCReturn(rc, rc);
    292303
    293304        /* Second region. */
    294305        RTGCPHYS const cbSecond = iPciFun == 0  ? cbSecondBAR : iPciFun * _32K;
    295         rc = PDMDevHlpPCIIORegionRegisterEx(pDevIns, pPciDev, 2, cbSecond,
    296                                             (PCIADDRESSSPACE)(  PCI_ADDRESS_SPACE_MEM | PCI_ADDRESS_SPACE_BAR64
    297                                                               | (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);
    299310        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);
    308316        AssertLogRelRCReturn(rc, rc);
    309317
     
    324332
    325333/**
    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 /**
    340334 * The device registration structure.
    341335 */
     
    345339    /* .uReserved0 = */             0,
    346340    /* .szName = */                 "playground",
    347     /* .fFlags = */                 PDM_DEVREG_FLAGS_DEFAULT_BITS,
     341    /* .fFlags = */                 PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_NEW_STYLE,
    348342    /* .fClass = */                 PDM_DEVREG_CLASS_MISC,
    349343    /* .cMaxInstances = */          1,
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette