VirtualBox

Changeset 106022 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Sep 12, 2024 9:44:36 AM (3 months ago)
Author:
vboxsync
Message:

VMM/PDM: Added PDMR3DriverEnumInstances for use by Console::i_onNATDnsChanged, introducing a read/write lock for the core PDM lists (devices, drivers, usb devices, and more). Medium risk since it involves new locking.

Location:
trunk/src/VBox/VMM
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/VMM/VMMR3/PDM.cpp

    r105068 r106022  
    462462    pUVM->pdm.s.pCritSects = NULL;
    463463    pUVM->pdm.s.pRwCritSects = NULL;
    464     return RTCritSectInit(&pUVM->pdm.s.ListCritSect);
     464    return RTCritSectInitNamed(&pUVM->pdm.s.ListCritSect, "PDM-ListCritSect");
    465465}
    466466
     
    502502            pVM->pdm.s.NopCritSect.s.Core.fFlags |= RTCRITSECT_FLAGS_NOP;
    503503    }
     504    if (RT_SUCCESS(rc))
     505        rc = RTCritSectRwInitNamed(&pVM->pdm.s.CoreListCritSectRw, "PDM-CoreListCritSectRw");
    504506
    505507    /*
     
    614616        pVM->pdm.s.IoApic.pfnSetIrqRC       += offDelta;
    615617        if (pVM->pdm.s.IoApic.pfnSendMsiRC)
    616             pVM->pdm.s.IoApic.pfnSendMsiRC      += offDelta;
     618            pVM->pdm.s.IoApic.pfnSendMsiRC  += offDelta;
    617619        if (pVM->pdm.s.IoApic.pfnSetEoiRC)
    618             pVM->pdm.s.IoApic.pfnSetEoiRC       += offDelta;
     620            pVM->pdm.s.IoApic.pfnSetEoiRC   += offDelta;
    619621    }
    620622
     
    637639    }
    638640
     641    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
    639642    for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
    640643    {
     
    690693
    691694    }
     695    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
     696
    692697#endif /* VBOX_WITH_RAW_MODE_KEEP */
    693698}
     
    696701/**
    697702 * Worker for pdmR3Term that terminates a LUN chain.
     703 *
     704 * @note    Caller owns CoreListCritSectRw in exclusive mode.
     705 *          Will temporarily leave and retake the lock when calling destructors.
    698706 *
    699707 * @param   pVM         The cross context VM structure.
     
    722730                LogFlow(("pdmR3DevTerm: Destroying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
    723731                         pDrvIns->pReg->szName, pDrvIns->iInstance, pLun->iLun, pszDevice, iInstance));
     732                RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    724733                pDrvIns->pReg->pfnDestruct(pDrvIns);
     734                RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
    725735            }
    726736            pDrvIns->Internal.s.pDrv->cInstances--;
     
    769779     *      from any MM heap since MM will do that in its Term function.
    770780     */
     781    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     782
    771783    /* usb ones first. */
    772784    for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
     
    781793        if (pHub)
    782794        {
     795            RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    783796            int rc = pHub->Reg.pfnDetachDevice(pHub->pDrvIns, pUsbIns, pUsbIns->Internal.s.iPort);
    784             if (RT_FAILURE(rc))
    785             {
    786                 LogRel(("PDM: Failed to detach USB device '%s' instance %d from %p: %Rrc\n",
    787                         pUsbIns->pReg->szName, pUsbIns->iInstance, pHub, rc));
    788             }
    789             else
     797            RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     798            if (RT_SUCCESS(rc))
    790799            {
    791800                pHub->cAvailablePorts++;
     
    793802                pUsbIns->Internal.s.pHub = NULL;
    794803            }
    795         }
     804            else
     805                LogRel(("PDM: Failed to detach USB device '%s' instance %d from %p: %Rrc\n",
     806                        pUsbIns->pReg->szName, pUsbIns->iInstance, pHub, rc));
     807        }
     808
     809        RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    796810
    797811        if (pUsbIns->pReg->pfnDestruct)
    798812        {
    799             LogFlow(("pdmR3DevTerm: Destroying - device '%s'/%d\n",
    800                      pUsbIns->pReg->szName, pUsbIns->iInstance));
     813            LogFlow(("pdmR3DevTerm: Destroying - device '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
    801814            pUsbIns->pReg->pfnDestruct(pUsbIns);
    802815        }
     
    811824            pUsbIns->pszName = NULL;
    812825        }
     826
     827        RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
    813828    }
    814829
     
    821836        {
    822837            LogFlow(("pdmR3DevTerm: Destroying - device '%s'/%d\n", pDevIns->pReg->szName, pDevIns->iInstance));
     838            RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    823839            pDevIns->pReg->pfnDestruct(pDevIns);
     840            RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
    824841        }
    825842
     
    851868        }
    852869#endif
     870        RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    853871
    854872        TMR3TimerDestroyDevice(pVM, pDevIns);
     
    864882#endif
    865883        DBGFR3InfoDeregisterDevice(pVM, pDevIns, NULL);
    866     }
     884
     885        RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     886    }
     887
     888    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    867889
    868890    /*
     
    909931    PDMR3CritSectDelete(pVM, &pVM->pdm.s.CritSect);
    910932    /* The MiscCritSect is deleted by PDMR3CritSectBothTerm later. */
     933
     934    if (RTCritSectRwIsInitialized(&pVM->pdm.s.CoreListCritSectRw))
     935        RTCritSectRwDelete(&pVM->pdm.s.CoreListCritSectRw);
    911936
    912937    LogFlow(("PDMR3Term: returns %Rrc\n", VINF_SUCCESS));
     
    12161241     * Load the list of devices and verify that they are all there.
    12171242     */
     1243    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
    12181244    for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
    12191245        pDevIns->Internal.s.fIntFlags &= ~PDMDEVINSINT_FLAGS_FOUND;
     1246    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    12201247
    12211248    for (uint32_t i = 0; ; i++)
     
    12421269
    12431270        /* Try locate it. */
     1271        RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
    12441272        PPDMDEVINS pDevIns;
    12451273        for (pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
     
    12471275                && pDevIns->iInstance == iInstance)
    12481276            {
    1249                 AssertLogRelMsgReturn(!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_FOUND),
    1250                                       ("%s/#%u\n", pDevIns->pReg->szName, pDevIns->iInstance),
    1251                                       VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
     1277                AssertLogRelMsgReturnStmt(!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_FOUND),
     1278                                          ("%s/#%u\n", pDevIns->pReg->szName, pDevIns->iInstance),
     1279                                          RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw),
     1280                                          VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
    12521281                pDevIns->Internal.s.fIntFlags |= PDMDEVINSINT_FLAGS_FOUND;
    12531282                break;
    12541283            }
     1284        RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    12551285
    12561286        if (!pDevIns)
     
    12751305     * Check that no additional devices were configured.
    12761306     */
     1307    RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
    12771308    for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
    12781309        if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_FOUND))
     
    12851316             */
    12861317            if (   SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT
    1287                 && RTStrCmp(pDevIns->pReg->szName, "tpm-ppi"))
     1318                && RTStrCmp(pDevIns->pReg->szName, "tpm-ppi") /* !!HACK ALERT!! - ugly, see todo above */)
     1319            {
     1320                RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    12881321                return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Device '%s'/%d not found in the saved state"),
    12891322                                        pDevIns->pReg->szName, pDevIns->iInstance);
    1290         }
    1291 
     1323            }
     1324        }
     1325    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    12921326
    12931327    /*
     
    13931427     * processing the drivers associated with those.
    13941428     */
     1429    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     1430    /** @todo locking   */
     1431    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    13951432    int rc = VINF_SUCCESS;
    13961433    for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances;  pDevIns && RT_SUCCESS(rc);  pDevIns = pDevIns->Internal.s.pNextR3)
     
    17181755     * Clear all the reset flags.
    17191756     */
     1757    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
    17201758    for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
    17211759    {
     
    17341772    }
    17351773#endif
     1774    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    17361775
    17371776    /*
     
    17481787         * processing the drivers associated with those.
    17491788         */
     1789        /** @todo locking   */
    17501790        for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
    17511791        {
     
    20802120         * taking any. (DrvVD changes to read-only in this particular case.)
    20812121         */
     2122        RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     2123        /** @todo locking   */
     2124        RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    20822125        for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
    20832126        {
     
    22232266     * processing the drivers associated with those.
    22242267     */
     2268    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     2269    /** @todo locking   */
     2270    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    22252271    int rc = VINF_SUCCESS;
    2226     for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances;  pDevIns && RT_SUCCESS(rc); pDevIns = pDevIns->Internal.s.pNextR3)
    2227     {
    2228         for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3;  pLun && RT_SUCCESS(rc);  pLun    = pLun->pNext)
    2229             for (PPDMDRVINS pDrvIns = pLun->pTop;  pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
     2272    for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns && RT_SUCCESS(rc); pDevIns = pDevIns->Internal.s.pNextR3)
     2273    {
     2274        for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
     2275            for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
    22302276                rc = pdmR3ResumeDrv(pDrvIns, pDevIns->pReg->szName, pDevIns->iInstance, pLun->iLun);
    22312277        if (RT_SUCCESS(rc))
     
    22342280
    22352281#ifdef VBOX_WITH_USB
    2236     for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances;  pUsbIns && RT_SUCCESS(rc); pUsbIns = pUsbIns->Internal.s.pNext)
    2237     {
    2238         for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns;  pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
    2239             for (PPDMDRVINS pDrvIns = pLun->pTop;  pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
     2282    for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns && RT_SUCCESS(rc); pUsbIns = pUsbIns->Internal.s.pNext)
     2283    {
     2284        for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun && RT_SUCCESS(rc); pLun = pLun->pNext)
     2285            for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns && RT_SUCCESS(rc); pDrvIns = pDrvIns->Internal.s.pDown)
    22402286                rc = pdmR3ResumeDrv(pDrvIns, pUsbIns->pReg->szName, pUsbIns->iInstance, pLun->iLun);
    22412287        if (RT_SUCCESS(rc))
     
    24262472     * be called in any case.
    24272473     */
     2474    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
    24282475    for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
    24292476    {
     
    24452492    }
    24462493#endif
     2494    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    24472495
    24482496    /*
     
    24642512         * taking any. (DrvVD changes to read-only in this particular case.)
    24652513         */
     2514        /** @todo locking   */
    24662515        for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
    24672516        {
     
    25302579    LogFlow(("PDMR3DeviceQuery: pszDevice=%p:{%s} iInstance=%u ppBase=%p\n", pszDevice, pszDevice, iInstance, ppBase));
    25312580    UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
    2532     VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
     2581    PVM const pVM = pUVM->pVM;
     2582    VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
    25332583
    25342584    /*
    25352585     * Iterate registered devices looking for the device.
    25362586     */
    2537     size_t cchDevice = strlen(pszDevice);
    2538     for (PPDMDEV pDev = pUVM->pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
     2587    size_t const cchDevice = strlen(pszDevice);
     2588    RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
     2589    for (PPDMDEV pDev = pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
    25392590    {
    25402591        if (    pDev->cchName == cchDevice
     
    25512602                    {
    25522603                        *ppBase = &pDevIns->IBase;
     2604                        RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    25532605                        LogFlow(("PDMR3DeviceQuery: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
    25542606                        return VINF_SUCCESS;
    25552607                    }
    25562608
     2609                    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    25572610                    LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NO_IBASE\n"));
    25582611                    return VERR_PDM_DEVICE_INSTANCE_NO_IBASE;
     
    25602613            }
    25612614
     2615            RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    25622616            LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NOT_FOUND\n"));
    25632617            return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND;
     
    25652619    }
    25662620
     2621    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    25672622    LogFlow(("PDMR3QueryDevice: returns VERR_PDM_DEVICE_NOT_FOUND\n"));
    25682623    return VERR_PDM_DEVICE_NOT_FOUND;
     
    26312686     * Find the LUN.
    26322687     */
     2688    RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
    26332689    PPDMLUN pLun;
    26342690    int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
     
    26382694        {
    26392695            *ppBase = &pLun->pTop->IBase;
     2696            RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    26402697            LogFlow(("PDMR3QueryLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
    26412698            return VINF_SUCCESS;
     
    26432700        rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
    26442701    }
     2702    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    26452703    LogFlow(("PDMR3QueryLun: returns %Rrc\n", rc));
    26462704    return rc;
     
    26692727    LogFlow(("PDMR3QueryDriverOnLun: pszDevice=%p:{%s} iInstance=%u iLun=%u pszDriver=%p:{%s} ppBase=%p\n",
    26702728             pszDevice, pszDevice, iInstance, iLun, pszDriver, pszDriver, ppBase));
     2729    *ppBase = NULL;
    26712730    UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
    2672     VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
     2731    PVM const pVM = pUVM->pVM;
     2732    VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
    26732733
    26742734    /*
    26752735     * Find the LUN.
    26762736     */
     2737    RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
    26772738    PPDMLUN pLun;
    2678     int rc = pdmR3DevFindLun(pUVM->pVM, pszDevice, iInstance, iLun, &pLun);
     2739    int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
    26792740    if (RT_SUCCESS(rc))
    26802741    {
     
    26852746                {
    26862747                    *ppBase = &pDrvIns->IBase;
     2748                    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    26872749                    LogFlow(("PDMR3QueryDriverOnLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
    26882750                    return VINF_SUCCESS;
    2689 
    26902751                }
    26912752            rc = VERR_PDM_DRIVER_NOT_FOUND;
     
    26942755            rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
    26952756    }
     2757    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    26962758    LogFlow(("PDMR3QueryDriverOnLun: returns %Rrc\n", rc));
    26972759    return rc;
    26982760}
    26992761
     2762
    27002763/**
    27012764 * Executes pending DMA transfers.
     
    27062769VMMR3DECL(void) PDMR3DmaRun(PVM pVM)
    27072770{
     2771    /** @todo this is bad in SMP... */
    27082772    /* Note! Not really SMP safe; restrict it to VCPU 0. */
    27092773    if (VMMGetCpuId(pVM) != 0)
     
    27122776    if (VM_FF_TEST_AND_CLEAR(pVM, VM_FF_PDM_DMA))
    27132777    {
    2714         if (pVM->pdm.s.pDmac)
    2715         {
    2716             bool fMore = pVM->pdm.s.pDmac->Reg.pfnRun(pVM->pdm.s.pDmac->pDevIns);
     2778        PPDMDMAC const pDmac = pVM->pdm.s.pDmac;
     2779        if (pDmac)
     2780        {
     2781            bool const fMore = pDmac->Reg.pfnRun(pDmac->pDevIns);
    27172782            if (fMore)
    27182783                VM_FF_SET(pVM, VM_FF_PDM_DMA);
     
    27932858    {
    27942859        AssertReturn(fApply, VINF_SUCCESS);
     2860        RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
    27952861
    27962862        for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
     
    28122878        }
    28132879#endif
     2880
     2881        RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    28142882        return VINF_SUCCESS;
    28152883    }
     
    28252893    if (!strncmp("dev", pszName, 3))
    28262894    {
     2895        RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw); /* Shared-mode is sufficient for this. */
    28272896        for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
    28282897        {
     
    28422911    else if (!strncmp("usb", pszName, 3))
    28432912    {
     2913        RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw); /* Shared-mode is sufficient for this. */
    28442914        for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
    28452915        {
     
    28602930    {
    28612931        AssertReturn(fApply, VINF_SUCCESS);
     2932        RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw); /* Shared-mode is sufficient for this. */
    28622933
    28632934        for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
     
    28962967                }
    28972968#endif
     2969
    28982970    }
    28992971    else
    29002972        return VERR_NOT_FOUND;
    29012973
     2974    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    29022975    return cMatches > 0 ? VINF_SUCCESS : VERR_NOT_FOUND;
    29032976}
     
    29142987VMMR3_INT_DECL(bool) PDMR3TracingAreAll(PVM pVM, bool fEnabled)
    29152988{
     2989    RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
     2990
    29162991    for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
    29172992    {
    29182993        if (pDevIns->fTracing != (uint32_t)fEnabled)
     2994        {
     2995            RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    29192996            return false;
     2997        }
    29202998
    29212999        for (PPDMLUN pLun = pDevIns->Internal.s.pLunsR3; pLun; pLun = pLun->pNext)
    29223000            for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
    29233001                if (pDrvIns->fTracing != (uint32_t)fEnabled)
     3002                {
     3003                    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    29243004                    return false;
     3005                }
    29253006    }
    29263007
     
    29293010    {
    29303011        if (pUsbIns->fTracing != (uint32_t)fEnabled)
     3012        {
     3013            RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    29313014            return false;
     3015        }
    29323016
    29333017        for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
    29343018            for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
    29353019                if (pDrvIns->fTracing != (uint32_t)fEnabled)
     3020                {
     3021                    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    29363022                    return false;
     3023                }
    29373024    }
    29383025#endif
    29393026
     3027    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    29403028    return true;
    29413029}
     
    29913079VMMR3_INT_DECL(int) PDMR3TracingQueryConfig(PVM pVM, char *pszConfig, size_t cbConfig)
    29923080{
    2993     int     rc;
    29943081    char   *pszDst = pszConfig;
    29953082    size_t  cbDst  = cbConfig;
    29963083
     3084    RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
     3085
    29973086    for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
    29983087    {
    29993088        if (pDevIns->fTracing)
    30003089        {
    3001             rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "dev", pDevIns->Internal.s.pDevR3->pReg->szName);
     3090            int rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "dev", pDevIns->Internal.s.pDevR3->pReg->szName);
    30023091            if (RT_FAILURE(rc))
     3092            {
     3093                RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    30033094                return rc;
     3095            }
    30043096        }
    30053097
     
    30083100                if (pDrvIns->fTracing)
    30093101                {
    3010                     rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "drv", pDrvIns->Internal.s.pDrv->pReg->szName);
     3102                    int rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "drv", pDrvIns->Internal.s.pDrv->pReg->szName);
    30113103                    if (RT_FAILURE(rc))
     3104                    {
     3105                        RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    30123106                        return rc;
     3107                    }
    30133108                }
    30143109    }
     
    30193114        if (pUsbIns->fTracing)
    30203115        {
    3021             rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "usb", pUsbIns->Internal.s.pUsbDev->pReg->szName);
     3116            int rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "usb", pUsbIns->Internal.s.pUsbDev->pReg->szName);
    30223117            if (RT_FAILURE(rc))
     3118            {
     3119                RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    30233120                return rc;
     3121            }
    30243122        }
    30253123
     
    30283126                if (pDrvIns->fTracing)
    30293127                {
    3030                     rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "drv", pDrvIns->Internal.s.pDrv->pReg->szName);
     3128                    int rc = pdmR3TracingAdd(&pszDst, &cbDst, pszDst != pszConfig, "drv", pDrvIns->Internal.s.pDrv->pReg->szName);
    30313129                    if (RT_FAILURE(rc))
     3130                    {
     3131                        RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    30323132                        return rc;
     3133                    }
    30333134                }
    30343135    }
    30353136#endif
    30363137
     3138    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    30373139    return VINF_SUCCESS;
    30383140}
  • trunk/src/VBox/VMM/VMMR3/PDMDevHlp.cpp

    r105352 r106022  
    181181
    182182/** @interface_method_impl{PDMDEVHLPR3,pfnMmioCreateEx} */
    183 static DECLCALLBACK(int) pdmR3DevHlp_MmioCreateEx(PPDMDEVINS pDevIns, RTGCPHYS cbRegion,
    184                                                   uint32_t fFlags, PPDMPCIDEV pPciDev, uint32_t iPciRegion,
    185                                                   PFNIOMMMIONEWWRITE pfnWrite, PFNIOMMMIONEWREAD pfnRead, PFNIOMMMIONEWFILL pfnFill,
     183static DECLCALLBACK(int) pdmR3DevHlp_MmioCreateEx(PPDMDEVINS pDevIns, RTGCPHYS cbRegion, uint32_t fFlags,
     184                                                  PPDMPCIDEV pPciDev, uint32_t iPciRegion, PFNIOMMMIONEWWRITE pfnWrite,
     185                                                  PFNIOMMMIONEWREAD pfnRead, PFNIOMMMIONEWFILL pfnFill,
    186186                                                  void *pvUser, const char *pszDesc, PIOMMMIOHANDLE phRegion)
    187187{
     
    25992599{
    26002600    PDMDEV_ASSERT_DEVINS(pDevIns);
    2601     PVM pVM = pDevIns->Internal.s.pVMR3;
     2601    PVM const pVM = pDevIns->Internal.s.pVMR3;
    26022602    VM_ASSERT_EMT(pVM);
    26032603    LogFlow(("pdmR3DevHlp_DriverAttach: caller='%s'/%d: iLun=%d pBaseInterface=%p ppBaseInterface=%p pszDesc=%p:{%s}\n",
     
    26072607     * Lookup the LUN, it might already be registered.
    26082608     */
     2609    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     2610
    26092611    PPDMLUN pLunPrev = NULL;
    26102612    PPDMLUN pLun = pDevIns->Internal.s.pLunsR3;
     
    26182620    if (!pLun)
    26192621    {
    2620         if (    !pBaseInterface
    2621             ||  !pszDesc
    2622             ||  !*pszDesc)
     2622        if (   !pBaseInterface
     2623            || !pszDesc
     2624            || !*pszDesc)
    26232625        {
     2626            RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    26242627            Assert(pBaseInterface);
    26252628            Assert(pszDesc || *pszDesc);
     
    26292632        pLun = (PPDMLUN)MMR3HeapAlloc(pVM, MM_TAG_PDM_LUN, sizeof(*pLun));
    26302633        if (!pLun)
     2634        {
     2635            RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    26312636            return VERR_NO_MEMORY;
     2637        }
    26322638
    26332639        pLun->iLun      = iLun;
     
    26482654    else if (pLun->pTop)
    26492655    {
     2656        RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    26502657        AssertMsgFailed(("Already attached! The device should keep track of such things!\n"));
    26512658        LogFlow(("pdmR3DevHlp_DriverAttach: caller='%s'/%d: returns %Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, VERR_PDM_DRIVER_ALREADY_ATTACHED));
     
    26592666     */
    26602667    int rc;
    2661     PCFGMNODE pNode = CFGMR3GetChildF(pDevIns->Internal.s.pCfgHandle, "LUN#%u", iLun);
     2668    PCFGMNODE const pNode = CFGMR3GetChildF(pDevIns->Internal.s.pCfgHandle, "LUN#%u", iLun);
    26622669    if (pNode)
    26632670        rc = pdmR3DrvInstantiate(pVM, pNode, pBaseInterface, NULL /*pDrvAbove*/, pLun, ppBaseInterface);
     
    26652672        rc = VERR_PDM_NO_ATTACHED_DRIVER;
    26662673
     2674    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
     2675
    26672676    LogFlow(("pdmR3DevHlp_DriverAttach: caller='%s'/%d: returns %Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
    26682677    return rc;
     
    26772686             pDevIns->pReg->szName, pDevIns->iInstance, pDrvIns));
    26782687
    2679 #ifdef VBOX_STRICT
    2680     PVM pVM = pDevIns->Internal.s.pVMR3;
     2688    PVM const pVM = pDevIns->Internal.s.pVMR3;
    26812689    VM_ASSERT_EMT(pVM);
    2682 #endif
    2683 
    2684     int rc = pdmR3DrvDetach(pDrvIns, fFlags);
     2690
     2691    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     2692    int rc = pdmR3DrvDetach(pVM, pDrvIns, fFlags);
     2693    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    26852694
    26862695    LogFlow(("pdmR3DevHlp_DriverDetach: caller='%s'/%d: returns %Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
     
    26942703{
    26952704    PDMDEV_ASSERT_DEVINS(pDevIns);
    2696     PVM pVM = pDevIns->Internal.s.pVMR3;
     2705    PVM const pVM = pDevIns->Internal.s.pVMR3;
    26972706    VM_ASSERT_EMT(pVM);
    26982707    LogFlow(("pdmR3DevHlp_DriverReconfigure: caller='%s'/%d: iLun=%u cDepth=%u fFlags=%#x\n",
     
    27152724    }
    27162725    AssertReturn(fFlags == 0, VERR_INVALID_FLAGS);
     2726
     2727    /*
     2728     * Exclusively lock the core stuff (mind, pdmR3DrvDetach will leave + reenter).
     2729     */
     2730    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
    27172731
    27182732    /*
     
    27242738            if (pLun->pTop)
    27252739            {
    2726                 int rc = pdmR3DrvDetach(pLun->pTop, 0);
    2727                 AssertRCReturn(rc, rc);
     2740                int rc = pdmR3DrvDetach(pVM, pLun->pTop, 0);
     2741                AssertRCReturnStmt(rc, RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw), rc);
    27282742            }
    27292743            break;
     
    27672781    }
    27682782
     2783    /* Keep the lock till the end to serialize the CFGM changes. */
     2784    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    27692785    LogFlow(("pdmR3DevHlp_DriverReconfigure: caller='%s'/%d: returns %Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
    27702786    return rc;
     
    35133529     * Only one DMA device.
    35143530     */
    3515     PVM pVM = pDevIns->Internal.s.pVMR3;
    3516     if (pVM->pdm.s.pRtc)
     3531    PVM const pVM = pDevIns->Internal.s.pVMR3;
     3532    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     3533
     3534    int rc = VINF_SUCCESS;
     3535    if (!pVM->pdm.s.pRtc)
     3536    {
     3537        /*
     3538         * Allocate and initialize pci bus structure.
     3539         */
     3540        PPDMRTC pRtc = (PPDMRTC)MMR3HeapAlloc(pDevIns->Internal.s.pVMR3, MM_TAG_PDM_DEVICE, sizeof(*pRtc));
     3541        if (pRtc)
     3542        {
     3543            pRtc->pDevIns   = pDevIns;
     3544            pRtc->Reg       = *pRtcReg;
     3545            pVM->pdm.s.pRtc = pRtc;
     3546
     3547            /* set the helper pointer. */
     3548            *ppRtcHlp = &g_pdmR3DevRtcHlp;
     3549            Log(("PDM: Registered RTC device '%s'/%d pDevIns=%p\n",
     3550                 pDevIns->pReg->szName, pDevIns->iInstance, pDevIns));
     3551        }
     3552        else
     3553            rc = VERR_NO_MEMORY;
     3554    }
     3555    else
    35173556    {
    35183557        AssertMsgFailed(("Only one RTC device is supported!\n"));
    3519         LogFlow(("pdmR3DevHlp_RTCRegister: caller='%s'/%d: returns %Rrc\n",
    3520                  pDevIns->pReg->szName, pDevIns->iInstance, VERR_INVALID_PARAMETER));
    3521         return VERR_INVALID_PARAMETER;
    3522     }
    3523 
    3524     /*
    3525      * Allocate and initialize pci bus structure.
    3526      */
    3527     int rc = VINF_SUCCESS;
    3528     PPDMRTC pRtc = (PPDMRTC)MMR3HeapAlloc(pDevIns->Internal.s.pVMR3, MM_TAG_PDM_DEVICE, sizeof(*pRtc));
    3529     if (pRtc)
    3530     {
    3531         pRtc->pDevIns   = pDevIns;
    3532         pRtc->Reg       = *pRtcReg;
    3533         pVM->pdm.s.pRtc = pRtc;
    3534 
    3535         /* set the helper pointer. */
    3536         *ppRtcHlp = &g_pdmR3DevRtcHlp;
    3537         Log(("PDM: Registered RTC device '%s'/%d pDevIns=%p\n",
    3538              pDevIns->pReg->szName, pDevIns->iInstance, pDevIns));
    3539     }
    3540     else
    3541         rc = VERR_NO_MEMORY;
    3542 
     3558        rc = VERR_ALREADY_EXISTS;
     3559    }
     3560
     3561    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    35433562    LogFlow(("pdmR3DevHlp_RTCRegister: caller='%s'/%d: returns %Rrc\n",
    35443563             pDevIns->pReg->szName, pDevIns->iInstance, rc));
     
    35553574    LogFlow(("pdmR3DevHlp_DMARegister: caller='%s'/%d: uChannel=%d pfnTransferHandler=%p pvUser=%p\n",
    35563575             pDevIns->pReg->szName, pDevIns->iInstance, uChannel, pfnTransferHandler, pvUser));
    3557     int rc = VINF_SUCCESS;
    3558     if (pVM->pdm.s.pDmac)
    3559         pVM->pdm.s.pDmac->Reg.pfnRegister(pVM->pdm.s.pDmac->pDevIns, uChannel, pDevIns, pfnTransferHandler, pvUser);
     3576    int            rc    = VINF_SUCCESS;
     3577    PPDMDMAC const pDmac = pVM->pdm.s.pDmac;
     3578    if (pDmac)
     3579        pDmac->Reg.pfnRegister(pDmac->pDevIns, uChannel, pDevIns, pfnTransferHandler, pvUser);
    35603580    else
    35613581    {
     
    35773597    LogFlow(("pdmR3DevHlp_DMAReadMemory: caller='%s'/%d: uChannel=%d pvBuffer=%p off=%#x cbBlock=%#x pcbRead=%p\n",
    35783598             pDevIns->pReg->szName, pDevIns->iInstance, uChannel, pvBuffer, off, cbBlock, pcbRead));
    3579     int rc = VINF_SUCCESS;
    3580     if (pVM->pdm.s.pDmac)
    3581     {
    3582         uint32_t cb = pVM->pdm.s.pDmac->Reg.pfnReadMemory(pVM->pdm.s.pDmac->pDevIns, uChannel, pvBuffer, off, cbBlock);
     3599    int            rc    = VINF_SUCCESS;
     3600    PPDMDMAC const pDmac = pVM->pdm.s.pDmac;
     3601    if (pDmac)
     3602    {
     3603        uint32_t cb = pDmac->Reg.pfnReadMemory(pDmac->pDevIns, uChannel, pvBuffer, off, cbBlock);
    35833604        if (pcbRead)
    35843605            *pcbRead = cb;
     
    36033624    LogFlow(("pdmR3DevHlp_DMAWriteMemory: caller='%s'/%d: uChannel=%d pvBuffer=%p off=%#x cbBlock=%#x pcbWritten=%p\n",
    36043625             pDevIns->pReg->szName, pDevIns->iInstance, uChannel, pvBuffer, off, cbBlock, pcbWritten));
    3605     int rc = VINF_SUCCESS;
    3606     if (pVM->pdm.s.pDmac)
    3607     {
    3608         uint32_t cb = pVM->pdm.s.pDmac->Reg.pfnWriteMemory(pVM->pdm.s.pDmac->pDevIns, uChannel, pvBuffer, off, cbBlock);
     3626    int            rc    = VINF_SUCCESS;
     3627    PPDMDMAC const pDmac = pVM->pdm.s.pDmac;
     3628    if (pDmac)
     3629    {
     3630        uint32_t cb = pDmac->Reg.pfnWriteMemory(pDmac->pDevIns, uChannel, pvBuffer, off, cbBlock);
    36093631        if (pcbWritten)
    36103632            *pcbWritten = cb;
     
    36293651    LogFlow(("pdmR3DevHlp_DMASetDREQ: caller='%s'/%d: uChannel=%d uLevel=%d\n",
    36303652             pDevIns->pReg->szName, pDevIns->iInstance, uChannel, uLevel));
    3631     int rc = VINF_SUCCESS;
    3632     if (pVM->pdm.s.pDmac)
    3633         pVM->pdm.s.pDmac->Reg.pfnSetDREQ(pVM->pdm.s.pDmac->pDevIns, uChannel, uLevel);
     3653    int            rc    = VINF_SUCCESS;
     3654    PPDMDMAC const pDmac = pVM->pdm.s.pDmac;
     3655    if (pDmac)
     3656        pDmac->Reg.pfnSetDREQ(pDmac->pDevIns, uChannel, uLevel);
    36343657    else
    36353658    {
     
    36503673    LogFlow(("pdmR3DevHlp_DMAGetChannelMode: caller='%s'/%d: uChannel=%d\n",
    36513674             pDevIns->pReg->szName, pDevIns->iInstance, uChannel));
    3652     uint8_t u8Mode;
    3653     if (pVM->pdm.s.pDmac)
    3654         u8Mode = pVM->pdm.s.pDmac->Reg.pfnGetChannelMode(pVM->pdm.s.pDmac->pDevIns, uChannel);
     3675    uint8_t        u8Mode;
     3676    PPDMDMAC const pDmac = pVM->pdm.s.pDmac;
     3677    if (pDmac)
     3678        u8Mode = pDmac->Reg.pfnGetChannelMode(pDmac->pDevIns, uChannel);
    36553679    else
    36563680    {
     
    36883712             pDevIns->pReg->szName, pDevIns->iInstance, iReg, u8Value));
    36893713    int rc;
    3690     if (pVM->pdm.s.pRtc)
    3691     {
    3692         PPDMDEVINS pDevInsRtc = pVM->pdm.s.pRtc->pDevIns;
     3714    PPDMRTC const pRtc = pVM->pdm.s.pRtc;
     3715    if (pRtc)
     3716    {
     3717        PPDMDEVINS const pDevInsRtc = pRtc->pDevIns;
    36933718        rc = PDMCritSectEnter(pVM, pDevInsRtc->pCritSectRoR3, VERR_IGNORED);
    36943719        if (RT_SUCCESS(rc))
    36953720        {
    3696             rc = pVM->pdm.s.pRtc->Reg.pfnWrite(pDevInsRtc, iReg, u8Value);
     3721            rc = pRtc->Reg.pfnWrite(pDevInsRtc, iReg, u8Value);
    36973722            PDMCritSectLeave(pVM, pDevInsRtc->pCritSectRoR3);
    36983723        }
     
    37173742             pDevIns->pReg->szName, pDevIns->iInstance, iReg, pu8Value));
    37183743    int rc;
    3719     if (pVM->pdm.s.pRtc)
    3720     {
    3721         PPDMDEVINS pDevInsRtc = pVM->pdm.s.pRtc->pDevIns;
     3744    PPDMRTC const pRtc = pVM->pdm.s.pRtc;
     3745    if (pRtc)
     3746    {
     3747        PPDMDEVINS const pDevInsRtc = pRtc->pDevIns;
    37223748        rc = PDMCritSectEnter(pVM, pDevInsRtc->pCritSectRoR3, VERR_IGNORED);
    37233749        if (RT_SUCCESS(rc))
    37243750        {
    3725             rc = pVM->pdm.s.pRtc->Reg.pfnRead(pDevInsRtc, iReg, pu8Value);
     3751            rc = pRtc->Reg.pfnRead(pDevInsRtc, iReg, pu8Value);
    37263752            PDMCritSectLeave(pVM, pDevInsRtc->pCritSectRoR3);
    37273753        }
     
    39794005    VM_ASSERT_STATE_RETURN(pVM, VMSTATE_CREATING, VERR_WRONG_ORDER);
    39804006
     4007    /* Take the core lock exclusively just to be safe. */
     4008    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     4009
    39814010    /*
    39824011     * Find free PCI bus entry.
     
    39864015        if (!pVM->pdm.s.aPciBuses[iBus].pDevInsR3)
    39874016            break;
    3988     AssertLogRelMsgReturn(iBus < RT_ELEMENTS(pVM->pdm.s.aPciBuses),
    3989                           ("Too many PCI buses. Max=%u\n", RT_ELEMENTS(pVM->pdm.s.aPciBuses)),
    3990                           VERR_OUT_OF_RESOURCES);
     4017    AssertLogRelMsgReturnStmt(iBus < RT_ELEMENTS(pVM->pdm.s.aPciBuses),
     4018                              ("Too many PCI buses. Max=%u\n", RT_ELEMENTS(pVM->pdm.s.aPciBuses)),
     4019                              RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw),
     4020                              VERR_OUT_OF_RESOURCES);
    39914021    PPDMPCIBUS pPciBus = &pVM->pdm.s.aPciBuses[iBus];
    39924022
     
    40054035
    40064036    Log(("PDM: Registered PCI bus device '%s'/%d pDevIns=%p\n", pDevIns->pReg->szName, pDevIns->iInstance, pDevIns));
     4037
     4038    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    40074039
    40084040    /* set the helper pointer and return. */
     
    40424074    VM_ASSERT_EMT0_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
    40434075
     4076    /* Take the core lock exclusively just to be safe. */
     4077    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     4078
    40444079    /*
    40454080     * Find free IOMMU slot.
     
    40514086        if (!pVM->pdm.s.aIommus[idxIommu].pDevInsR3)
    40524087            break;
    4053     AssertLogRelMsgReturn(idxIommu < RT_ELEMENTS(pVM->pdm.s.aIommus),
    4054                           ("Too many IOMMUs. Max=%u\n", RT_ELEMENTS(pVM->pdm.s.aIommus)),
    4055                           VERR_OUT_OF_RESOURCES);
     4088    AssertLogRelMsgReturnStmt(idxIommu < RT_ELEMENTS(pVM->pdm.s.aIommus),
     4089                              ("Too many IOMMUs. Max=%u\n", RT_ELEMENTS(pVM->pdm.s.aIommus)),
     4090                              RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw),
     4091                              VERR_OUT_OF_RESOURCES);
    40564092#else
    40574093    /* Currently we support only a single IOMMU. */
    4058     AssertMsgReturn(!pVM->pdm.s.aIommus[0].pDevInsR3,
    4059                     ("%s/%u: Only one IOMMU device is supported!\n", pDevIns->pReg->szName, pDevIns->iInstance),
    4060                     VERR_ALREADY_EXISTS);
     4094    AssertMsgReturnStmt(!pVM->pdm.s.aIommus[idxIommu].pDevInsR3,
     4095                        ("%s/%u: Only one IOMMU device is supported!\n", pDevIns->pReg->szName, pDevIns->iInstance),
     4096                        RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw),
     4097                        VERR_ALREADY_EXISTS);
    40614098#endif
    40624099    PPDMIOMMUR3 pIommu = &pVM->pdm.s.aIommus[idxIommu];
     
    40714108    pIommu->pfnMsiRemap      = pIommuReg->pfnMsiRemap;
    40724109    Log(("PDM: Registered IOMMU device '%s'/%d pDevIns=%p\n", pDevIns->pReg->szName, pDevIns->iInstance, pDevIns));
     4110
     4111    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    40734112
    40744113    /* Set the helper pointer and return. */
     
    41074146    VM_ASSERT_EMT0_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
    41084147
     4148    /* Take the core lock exclusively just to be safe. */
     4149    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     4150
    41094151    /*
    41104152     * Only one PIC device.
    41114153     */
    4112     AssertMsgReturn(pVM->pdm.s.Pic.pDevInsR3 == NULL, ("%s/%d: Only one PIC!\n", pDevIns->pReg->szName, pDevIns->iInstance),
    4113                     VERR_ALREADY_EXISTS);
     4154    AssertMsgReturnStmt(pVM->pdm.s.Pic.pDevInsR3 == NULL,
     4155                        ("%s/%d: Only one PIC!\n", pDevIns->pReg->szName, pDevIns->iInstance),
     4156                        RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw),
     4157                        VERR_ALREADY_EXISTS);
    41144158
    41154159    /*
     
    41214165    Log(("PDM: Registered PIC device '%s'/%d pDevIns=%p\n", pDevIns->pReg->szName, pDevIns->iInstance, pDevIns));
    41224166
     4167    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
     4168
    41234169    /* set the helper pointer and return. */
    41244170    *ppPicHlp = &g_pdmR3DevPicHlp;
     
    41404186    VM_ASSERT_EMT0_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
    41414187
     4188    /* Take the core lock exclusively just to be safe. */
     4189    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     4190
    41424191    /*
    41434192     * Only one APIC device. On SMP we have single logical device covering all LAPICs,
    41444193     * as they need to communicate and share state easily.
    41454194     */
    4146     AssertMsgReturn(pVM->pdm.s.Apic.pDevInsR3 == NULL,
    4147                     ("%s/%u: Only one APIC device is supported!\n", pDevIns->pReg->szName, pDevIns->iInstance),
    4148                     VERR_ALREADY_EXISTS);
     4195    AssertMsgReturnStmt(pVM->pdm.s.Apic.pDevInsR3 == NULL,
     4196                        ("%s/%u: Only one APIC device is supported!\n", pDevIns->pReg->szName, pDevIns->iInstance),
     4197                        RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw),
     4198                        VERR_ALREADY_EXISTS);
    41494199
    41504200    /*
     
    41564206    Assert(pVM->pdm.s.Apic.pDevInsRC || !VM_IS_RAW_MODE_ENABLED(pVM));
    41574207#endif
     4208
     4209    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    41584210
    41594211    LogFlow(("pdmR3DevHlp_ApicRegister: caller='%s'/%d: returns %Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, VINF_SUCCESS));
     
    41864238    VM_ASSERT_EMT0_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
    41874239
     4240    /* Take the core lock exclusively just to be safe. */
     4241    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     4242
    41884243    /*
    41894244     * The I/O APIC requires the APIC to be present (hacks++).
    41904245     * If the I/O APIC does GC stuff so must the APIC.
    41914246     */
    4192     AssertMsgReturn(pVM->pdm.s.Apic.pDevInsR3 != NULL, ("Configuration error / Init order error! No APIC!\n"), VERR_WRONG_ORDER);
     4247    AssertMsgReturnStmt(pVM->pdm.s.Apic.pDevInsR3 != NULL,
     4248                        ("Configuration error / Init order error! No APIC!\n"),
     4249                        RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw),
     4250                        VERR_WRONG_ORDER);
    41934251
    41944252    /*
    41954253     * Only one I/O APIC device.
    41964254     */
    4197     AssertMsgReturn(pVM->pdm.s.IoApic.pDevInsR3 == NULL,
    4198                     ("Only one IOAPIC device is supported! (caller %s/%d)\n", pDevIns->pReg->szName, pDevIns->iInstance),
    4199                     VERR_ALREADY_EXISTS);
     4255    AssertMsgReturnStmt(pVM->pdm.s.IoApic.pDevInsR3 == NULL,
     4256                        ("Only one IOAPIC device is supported! (caller %s/%d)\n", pDevIns->pReg->szName, pDevIns->iInstance),
     4257                        RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw),
     4258                        VERR_ALREADY_EXISTS);
    42004259
    42014260    /*
     
    42074266    pVM->pdm.s.IoApic.pfnSetEoiR3  = pIoApicReg->pfnSetEoi;
    42084267    Log(("PDM: Registered I/O APIC device '%s'/%d pDevIns=%p\n", pDevIns->pReg->szName, pDevIns->iInstance, pDevIns));
     4268
     4269    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    42094270
    42104271    /* set the helper pointer and return. */
     
    42354296     * Only one HPET device.
    42364297     */
    4237     AssertMsgReturn(pVM->pdm.s.pHpet == NULL,
    4238                     ("Only one HPET device is supported! (caller %s/%d)\n", pDevIns->pReg->szName, pDevIns->iInstance),
    4239                     VERR_ALREADY_EXISTS);
     4298    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     4299
     4300    AssertMsgReturnStmt(pVM->pdm.s.pHpet == NULL,
     4301                        ("Only one HPET device is supported! (caller %s/%d)\n", pDevIns->pReg->szName, pDevIns->iInstance),
     4302                        RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw),
     4303                        VERR_ALREADY_EXISTS);
    42404304
    42414305    /*
     
    42434307     */
    42444308    pVM->pdm.s.pHpet = pDevIns;
     4309
     4310    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
     4311
    42454312    *ppHpetHlpR3 = &g_pdmR3DevHpetHlp;
    42464313
     
    43304397     * Only one DMA device.
    43314398     */
    4332     PVM pVM = pDevIns->Internal.s.pVMR3;
    4333     if (pVM->pdm.s.pDmac)
     4399    PVM const pVM = pDevIns->Internal.s.pVMR3;
     4400    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     4401
     4402    int rc = VINF_SUCCESS;
     4403    if (!pVM->pdm.s.pDmac)
     4404    {
     4405
     4406        /*
     4407         * Allocate and initialize pci bus structure.
     4408         */
     4409        PPDMDMAC pDmac = (PPDMDMAC)MMR3HeapAlloc(pDevIns->Internal.s.pVMR3, MM_TAG_PDM_DEVICE, sizeof(*pDmac));
     4410        if (pDmac)
     4411        {
     4412            pDmac->pDevIns   = pDevIns;
     4413            pDmac->Reg       = *pDmacReg;
     4414            pVM->pdm.s.pDmac = pDmac;
     4415
     4416            /* set the helper pointer. */
     4417            *ppDmacHlp = &g_pdmR3DevDmacHlp;
     4418            Log(("PDM: Registered DMAC device '%s'/%d pDevIns=%p\n",
     4419                 pDevIns->pReg->szName, pDevIns->iInstance, pDevIns));
     4420        }
     4421        else
     4422            rc = VERR_NO_MEMORY;
     4423    }
     4424    else
    43344425    {
    43354426        AssertMsgFailed(("Only one DMA device is supported!\n"));
    4336         LogFlow(("pdmR3DevHlp_DMACRegister: caller='%s'/%d: returns %Rrc\n",
    4337                  pDevIns->pReg->szName, pDevIns->iInstance, VERR_INVALID_PARAMETER));
    4338         return VERR_INVALID_PARAMETER;
    4339     }
    4340 
    4341     /*
    4342      * Allocate and initialize pci bus structure.
    4343      */
    4344     int rc = VINF_SUCCESS;
    4345     PPDMDMAC  pDmac = (PPDMDMAC)MMR3HeapAlloc(pDevIns->Internal.s.pVMR3, MM_TAG_PDM_DEVICE, sizeof(*pDmac));
    4346     if (pDmac)
    4347     {
    4348         pDmac->pDevIns   = pDevIns;
    4349         pDmac->Reg       = *pDmacReg;
    4350         pVM->pdm.s.pDmac = pDmac;
    4351 
    4352         /* set the helper pointer. */
    4353         *ppDmacHlp = &g_pdmR3DevDmacHlp;
    4354         Log(("PDM: Registered DMAC device '%s'/%d pDevIns=%p\n",
    4355              pDevIns->pReg->szName, pDevIns->iInstance, pDevIns));
    4356     }
    4357     else
    4358         rc = VERR_NO_MEMORY;
    4359 
     4427        rc = VERR_ALREADY_EXISTS;
     4428    }
     4429
     4430    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    43604431    LogFlow(("pdmR3DevHlp_DMACRegister: caller='%s'/%d: returns %Rrc\n",
    43614432             pDevIns->pReg->szName, pDevIns->iInstance, rc));
     
    43754446             pDevIns->pReg->szName, pDevIns->iInstance, GCPhys, pvHeap, cbHeap));
    43764447
     4448    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
    43774449    if (pVM->pdm.s.pvVMMDevHeap == NULL)
    43784450    {
     
    43904462        {
    43914463            pVM->pdm.s.GCPhysVMMDevHeap = GCPhys;
    4392             if (pVM->pdm.s.pfnVMMDevHeapNotify)
    4393                 pVM->pdm.s.pfnVMMDevHeapNotify(pVM, pvHeap, GCPhys);
     4464            PFNPDMVMMDEVHEAPNOTIFY const pfnVMMDevHeapNotify = pVM->pdm.s.pfnVMMDevHeapNotify;
     4465            if (pfnVMMDevHeapNotify)
     4466                pfnVMMDevHeapNotify(pVM, pvHeap, GCPhys);
    43944467        }
    43954468    }
     4469    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    43964470
    43974471    LogFlow(("pdmR3DevHlp_RegisterVMMDevHeap: caller='%s'/%d: returns %Rrc\n",
     
    44384512
    44394513    /*
    4440      * Only one DMA device.
     4514     * Only one firmware registration.
    44414515     */
    4442     PVM pVM = pDevIns->Internal.s.pVMR3;
    4443     if (pVM->pdm.s.pFirmware)
     4516    PVM const pVM = pDevIns->Internal.s.pVMR3;
     4517    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     4518
     4519    int rc = VINF_SUCCESS;
     4520    if (!pVM->pdm.s.pFirmware)
     4521    {
     4522        /*
     4523         * Allocate and initialize pci bus structure.
     4524         */
     4525        PPDMFW pFirmware = (PPDMFW)MMR3HeapAlloc(pDevIns->Internal.s.pVMR3, MM_TAG_PDM_DEVICE, sizeof(*pFirmware));
     4526        if (pFirmware)
     4527        {
     4528            pFirmware->pDevIns   = pDevIns;
     4529            pFirmware->Reg       = *pFwReg;
     4530            pVM->pdm.s.pFirmware = pFirmware;
     4531
     4532            /* set the helper pointer. */
     4533            *ppFwHlp = &g_pdmR3DevFirmwareHlp;
     4534            Log(("PDM: Registered firmware device '%s'/%d pDevIns=%p\n",
     4535                 pDevIns->pReg->szName, pDevIns->iInstance, pDevIns));
     4536        }
     4537        else
     4538            rc = VERR_NO_MEMORY;
     4539    }
     4540    else
    44444541    {
    44454542        AssertMsgFailed(("Only one firmware device is supported!\n"));
    4446         LogFlow(("pdmR3DevHlp_FirmwareRegister: caller='%s'/%d: returns %Rrc\n",
    4447                  pDevIns->pReg->szName, pDevIns->iInstance, VERR_INVALID_PARAMETER));
    4448         return VERR_INVALID_PARAMETER;
    4449     }
    4450 
    4451     /*
    4452      * Allocate and initialize pci bus structure.
    4453      */
    4454     int rc = VINF_SUCCESS;
    4455     PPDMFW pFirmware = (PPDMFW)MMR3HeapAlloc(pDevIns->Internal.s.pVMR3, MM_TAG_PDM_DEVICE, sizeof(*pFirmware));
    4456     if (pFirmware)
    4457     {
    4458         pFirmware->pDevIns   = pDevIns;
    4459         pFirmware->Reg       = *pFwReg;
    4460         pVM->pdm.s.pFirmware = pFirmware;
    4461 
    4462         /* set the helper pointer. */
    4463         *ppFwHlp = &g_pdmR3DevFirmwareHlp;
    4464         Log(("PDM: Registered firmware device '%s'/%d pDevIns=%p\n",
    4465              pDevIns->pReg->szName, pDevIns->iInstance, pDevIns));
    4466     }
    4467     else
    4468         rc = VERR_NO_MEMORY;
    4469 
     4543        rc = VERR_ALREADY_EXISTS;
     4544    }
     4545
     4546    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    44704547    LogFlow(("pdmR3DevHlp_FirmwareRegister: caller='%s'/%d: returns %Rrc\n",
    44714548             pDevIns->pReg->szName, pDevIns->iInstance, rc));
     
    48054882/** @interface_method_impl{PDMDEVHLPR3,pfnQueryLun} */
    48064883static DECLCALLBACK(int) pdmR3DevHlp_QueryLun(PPDMDEVINS pDevIns, const char *pszDevice,
    4807                                                  unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
     4884                                              unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
    48084885{
    48094886    PDMDEV_ASSERT_DEVINS(pDevIns);
  • trunk/src/VBox/VMM/VMMR3/PDMDevice.cpp

    r105687 r106022  
    501501         * Link it into all the lists.
    502502         */
     503        RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     504
    503505        /* The global instance FIFO. */
    504506        PPDMDEVINS pPrev1 = pVM->pdm.s.pDevInstances;
     
    536538                    VMSetError(pVM, rc, RT_SRC_POS, "Failed to construct device '%s' instance #%u",
    537539                               pDevIns->pReg->szName, pDevIns->iInstance);
    538                 paDevs[i].pDev->cInstances--;
     540                RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    539541                return VERR_NO_MEMORY;
    540542            }
     
    550552         */
    551553        paDevs[i].pDev->cInstances++;
     554        RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
     555
    552556        Log(("PDM: Constructing device '%s' instance %d...\n", pDevIns->pReg->szName, pDevIns->iInstance));
    553557        rc = pDevIns->pReg->pfnConstruct(pDevIns, pDevIns->iInstance, pDevIns->pCfg);
     
    560564            /* Because we're damn lazy, the destructor will be called even if
    561565               the constructor fails.  So, no unlinking. */
    562             paDevs[i].pDev->cInstances--;
     566            //paDevs[i].pDev->cInstances--;
    563567            return rc == VERR_VERSION_MISMATCH ? VERR_PDM_DEVICE_VERSION_MISMATCH : rc;
    564568        }
     
    584588                    VMSetError(pVM, rc, RT_SRC_POS, "The ring-0 constructor of device '%s' instance #%u failed",
    585589                               pDevIns->pReg->szName, pDevIns->iInstance);
    586                 paDevs[i].pDev->cInstances--;
     590                /* No unlinking, see above. */
     591                //paDevs[i].pDev->cInstances--;
    587592                return rc == VERR_VERSION_MISMATCH ? VERR_PDM_DEVICE_VERSION_MISMATCH : rc;
    588593            }
     
    617622     * Iterate thru the device instances and work the callback.
    618623     */
     624    RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
    619625    for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextR3)
    620626    {
    621627        if (pDevIns->pReg->pfnInitComplete)
    622628        {
     629            RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    623630            PDMCritSectEnter(pVM, pDevIns->pCritSectRoR3, VERR_IGNORED);
    624631            rc = pDevIns->pReg->pfnInitComplete(pDevIns);
     
    630637                return rc;
    631638            }
     639            RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
    632640        }
    633641    }
     642    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    634643
    635644#ifdef VBOX_WITH_USB
     
    653662PPDMDEV pdmR3DevLookup(PVM pVM, const char *pszName)
    654663{
    655     size_t cchName = strlen(pszName);
     664    size_t const cchName = strlen(pszName);
     665    RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
    656666    for (PPDMDEV pDev = pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
    657         if (    pDev->cchName == cchName
    658             &&  !strcmp(pDev->pReg->szName, pszName))
     667        if (   pDev->cchName == cchName
     668            && !strcmp(pDev->pReg->szName, pszName))
     669        {
     670            RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    659671            return pDev;
     672        }
     673    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    660674    return NULL;
    661675}
     
    916930     */
    917931    PCPDMDEVREGCBINT pRegCB = (PCPDMDEVREGCBINT)pCallbacks;
     932    PVM const        pVM    = pRegCB->pVM;
     933    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     934
    918935    PPDMDEV pDevPrev = NULL;
    919     PPDMDEV pDev = pRegCB->pVM->pdm.s.pDevs;
     936    PPDMDEV pDev     = pVM->pdm.s.pDevs;
    920937    for (; pDev; pDevPrev = pDev, pDev = pDev->pNext)
    921         AssertMsgReturn(strcmp(pDev->pReg->szName, pReg->szName),
    922                         ("Device '%s' already exists\n", pReg->szName),
    923                         VERR_PDM_DEVICE_NAME_CLASH);
     938        AssertMsgReturnStmt(strcmp(pDev->pReg->szName, pReg->szName),
     939                            ("Device '%s' already exists\n", pReg->szName),
     940                            RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw),
     941                            VERR_PDM_DEVICE_NAME_CLASH);
    924942
    925943    /*
     
    944962            else
    945963                pRegCB->pVM->pdm.s.pDevs = pDev;
     964
     965            RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    946966            Log(("PDM: Registered device '%s'\n", pReg->szName));
    947967            return VINF_SUCCESS;
     
    952972    else
    953973        rc = VERR_NO_MEMORY;
     974
     975    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    954976    return rc;
    955977}
     
    972994     * Iterate registered devices looking for the device.
    973995     */
    974     size_t cchDevice = strlen(pszDevice);
     996    size_t const cchDevice = strlen(pszDevice);
     997    RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
    975998    for (PPDMDEV pDev = pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
    976999    {
     
    9921015                        if (pLun->iLun == iLun)
    9931016                        {
     1017                            RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    9941018                            *ppLun = pLun;
    9951019                            return VINF_SUCCESS;
    9961020                        }
    9971021                    }
     1022                    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    9981023                    return VERR_PDM_LUN_NOT_FOUND;
    9991024                }
    10001025            }
     1026            RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    10011027            return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND;
    10021028        }
    10031029    }
     1030    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    10041031    return VERR_PDM_DEVICE_NOT_FOUND;
    10051032}
     
    10201047 * @thread  EMT
    10211048 */
    1022 VMMR3DECL(int) PDMR3DeviceAttach(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, uint32_t fFlags, PPPDMIBASE ppBase)
     1049VMMR3DECL(int) PDMR3DeviceAttach(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun,
     1050                                 uint32_t fFlags, PPPDMIBASE ppBase)
    10231051{
     1052    if (ppBase)
     1053        *ppBase = NULL;
    10241054    UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
    10251055    PVM pVM = pUVM->pVM;
     
    10321062     * Find the LUN in question.
    10331063     */
     1064    RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
    10341065    PPDMLUN pLun;
    10351066    int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
     
    10441075            if (!pLun->pTop)
    10451076            {
     1077                RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    10461078                PDMCritSectEnter(pVM, pDevIns->pCritSectRoR3, VERR_IGNORED);
    10471079                rc = pDevIns->pReg->pfnAttach(pDevIns, iLun, fFlags);
    10481080                PDMCritSectLeave(pVM, pDevIns->pCritSectRoR3);
     1081                RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
    10491082            }
    10501083            else
     
    10571090            *ppBase = pLun->pTop ? &pLun->pTop->IBase : NULL;
    10581091    }
    1059     else if (ppBase)
    1060         *ppBase = NULL;
     1092    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    10611093
    10621094    if (ppBase)
     
    11281160 * @thread  EMT
    11291161 */
    1130 VMMR3DECL(int) PDMR3DriverAttach(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, uint32_t fFlags, PPPDMIBASE ppBase)
     1162VMMR3DECL(int) PDMR3DriverAttach(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun,
     1163                                 uint32_t fFlags, PPPDMIBASE ppBase)
    11311164{
    11321165    LogFlow(("PDMR3DriverAttach: pszDevice=%p:{%s} iInstance=%d iLun=%d fFlags=%#x ppBase=%p\n",
    11331166             pszDevice, pszDevice, iInstance, iLun, fFlags, ppBase));
     1167    if (ppBase)
     1168        *ppBase = NULL;
    11341169    UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
    11351170    PVM pVM = pUVM->pVM;
     
    11371172    VM_ASSERT_EMT(pVM);
    11381173
    1139     if (ppBase)
    1140         *ppBase = NULL;
    1141 
    11421174    /*
    11431175     * Find the LUN in question.
    11441176     */
     1177    RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
    11451178    PPDMLUN pLun;
    11461179    int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
     
    11571190            if (pDevIns->pReg->pfnAttach)
    11581191            {
     1192                RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    11591193                PDMCritSectEnter(pVM, pDevIns->pCritSectRoR3, VERR_IGNORED);
    11601194                rc = pDevIns->pReg->pfnAttach(pDevIns, iLun, fFlags);
     
    11621196                    *ppBase = pLun->pTop ? &pLun->pTop->IBase : NULL;
    11631197                PDMCritSectLeave(pVM, pDevIns->pCritSectRoR3);
     1198                RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
    11641199            }
    11651200            else
     
    11731208            if (pDrvIns->pReg->pfnAttach)
    11741209            {
     1210                RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    11751211                rc = pDrvIns->pReg->pfnAttach(pDrvIns, fFlags);
     1212                RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
    11761213                if (RT_SUCCESS(rc) && ppBase)
    11771214                    *ppBase = pDrvIns->Internal.s.pDown
     
    11831220        }
    11841221    }
     1222    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    11851223
    11861224    if (ppBase)
     
    12211259             pszDevice, pszDevice, iDevIns, iLun, pszDriver, pszDriver, iOccurrence, fFlags));
    12221260    UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
    1223     PVM pVM = pUVM->pVM;
     1261    PVM const pVM = pUVM->pVM;
    12241262    VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
    12251263    VM_ASSERT_EMT(pVM);
     
    12321270     * Find the LUN in question.
    12331271     */
     1272    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
    12341273    PPDMLUN pLun;
    12351274    int rc = pdmR3DevFindLun(pVM, pszDevice, iDevIns, iLun, &pLun);
     
    12561295            }
    12571296            if (pDrvIns)
    1258                 rc = pdmR3DrvDetach(pDrvIns, fFlags);
     1297                rc = pdmR3DrvDetach(pVM, pDrvIns, fFlags);
    12591298            else
    12601299                rc = VERR_PDM_DRIVER_INSTANCE_NOT_FOUND;
     
    12631302            rc = VINF_PDM_NO_DRIVER_ATTACHED_TO_LUN;
    12641303    }
     1304    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    12651305
    12661306    LogFlow(("PDMR3DriverDetach: returns %Rrc\n", rc));
  • trunk/src/VBox/VMM/VMMR3/PDMDriver.cpp

    r98103 r106022  
    327327     * Check for duplicate and find FIFO entry at the same time.
    328328     */
    329     PCPDMDRVREGCBINT pRegCB = (PCPDMDRVREGCBINT)pCallbacks;
     329    PCPDMDRVREGCBINT const  pRegCB = (PCPDMDRVREGCBINT)pCallbacks;
     330    PVM const               pVM    = pRegCB->pVM;
     331    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     332
    330333    PPDMDRV pDrvPrev = NULL;
    331334    PPDMDRV pDrv = pRegCB->pVM->pdm.s.pDrvs;
     
    334337        if (!strcmp(pDrv->pReg->szName, pReg->szName))
    335338        {
     339            RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    336340            AssertMsgFailed(("Driver '%s' already exists\n", pReg->szName));
    337341            return VERR_PDM_DRIVER_NAME_CLASH;
     
    350354        pDrv->iNextInstance = 0;
    351355        pDrv->pReg          = pReg;
     356        pDrv->pInstances    = NULL;
    352357        rc = CFGMR3QueryStringAllocDef(    pRegCB->pCfgNode, "RCSearchPath", &pDrv->pszRCSearchPath, NULL);
    353358        if (RT_SUCCESS(rc))
     
    359364            else
    360365                pRegCB->pVM->pdm.s.pDrvs = pDrv;
     366            RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
     367
    361368            Log(("PDM: Registered driver '%s'\n", pReg->szName));
    362369            return VINF_SUCCESS;
     
    366373    else
    367374        rc = VERR_NO_MEMORY;
     375    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    368376    return rc;
    369377}
     
    376384PPDMDRV pdmR3DrvLookup(PVM pVM, const char *pszName)
    377385{
     386    RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
    378387    for (PPDMDRV pDrv = pVM->pdm.s.pDrvs; pDrv; pDrv = pDrv->pNext)
    379388        if (!strcmp(pDrv->pReg->szName, pszName))
     389        {
     390            RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    380391            return pDrv;
     392        }
     393    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    381394    return NULL;
    382395}
     
    394407 * @param   ppNode              The AttachedDriver node, replaced if any
    395408 *                              morphing took place.
     409 * @note    Caller owns CoreListCritSectRw exclusivly.
    396410 */
    397411static int pdmR3DrvMaybeTransformChain(PVM pVM, PPDMDRVINS pDrvAbove, PPDMLUN pLun, PCFGMNODE *ppNode)
     
    653667 * Instantiate a driver.
    654668 *
     669 * @note    The caller must enter CoreListCritSectRw with exclusive access
     670 *          rights before the call.  This function _will_ release the lock for
     671 *          doing callbacks (and failure cleanups), but it will still own it
     672 *          upon return.
     673 *
    655674 * @returns VBox status code, including informational statuses.
    656675 *
     
    680699
    681700    Assert(pBaseInterface->pfnQueryInterface(pBaseInterface, PDMIBASE_IID) == pBaseInterface);
     701
     702    /* Exclusive lock ownership w/o any extra recursions. */
     703    Assert(RTCritSectRwIsWriteOwner(&pVM->pdm.s.CoreListCritSectRw));
     704    Assert(RTCritSectRwGetWriteRecursion(&pVM->pdm.s.CoreListCritSectRw) == 1);
    682705
    683706    /*
     
    747770                    //pNew->Internal.s.pfnAsyncNotify = NULL;
    748771                    pNew->Internal.s.pCfgHandle     = pNode;
     772                    //pNew->Internal.s.pNext          = NULL;
    749773                    pNew->pReg                      = pDrv->pReg;
    750774                    pNew->pCfg                      = pConfigNode;
     
    775799#endif
    776800
     801                    /*
     802                     * Update PDMDRV members and link it into the instance list.
     803                     */
    777804                    pDrv->iNextInstance++;
    778805                    pDrv->cInstances++;
     806
     807                    PPDMDRVINS pPrevDrvIns = pDrv->pInstances;
     808                    if (!pPrevDrvIns)
     809                        pDrv->pInstances = pNew;
     810                    else
     811                    {
     812                        while (pPrevDrvIns->Internal.s.pNext)
     813                            pPrevDrvIns = pPrevDrvIns->Internal.s.pNext;
     814                        pPrevDrvIns->Internal.s.pNext = pNew;
     815                    }
    779816
    780817                    /*
     
    793830                    /*
    794831                     * Invoke the constructor.
     832                     *
     833                     * We have to leave the exclusive CoreListCritSectRw ownership here, as
     834                     * the constructor may call us back recursively to attach drivers down
     835                     * the stack, which would cause trouble in pdmR3DrvDestroyChain since
     836                     * it assumes there is no lock recursion.  There is also the possibility
     837                     * of the constructor engaging with PDM asynchronously via another thread.
    795838                     */
     839                    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    796840                    rc = pDrv->pReg->pfnConstruct(pNew, pNew->pCfg, 0 /*fFlags*/);
     841                    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
    797842                    if (RT_SUCCESS(rc))
    798843                    {
     
    816861                    else
    817862                    {
    818                         pdmR3DrvDestroyChain(pNew, PDM_TACH_FLAGS_NO_CALLBACKS);
     863                        pdmR3DrvDestroyChain(pVM, pNew, PDM_TACH_FLAGS_NO_CALLBACKS);
    819864                        if (rc == VERR_VERSION_MISMATCH)
    820865                            rc = VERR_PDM_DRIVER_VERSION_MISMATCH;
     
    854899 * This will of course lead to the destruction of the driver and all drivers below it in the chain.
    855900 *
     901 * @note    The caller must enter CoreListCritSectRw with exclusive access
     902 *          rights before the call.  This function _will_ release the lock for
     903 *          doing callbacks and cleaning up resources associated with the
     904 *          drivers being destroyed, but it will still own it upon return.
     905 *
    856906 * @returns VINF_SUCCESS
     907 * @param   pVM         The cross context VM structure.
    857908 * @param   pDrvIns     The driver instance to detach.
    858909 * @param   fFlags      Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
    859910 */
    860 int pdmR3DrvDetach(PPDMDRVINS pDrvIns, uint32_t fFlags)
     911int pdmR3DrvDetach(PVM pVM, PPDMDRVINS pDrvIns, uint32_t fFlags)
    861912{
    862913    PDMDRV_ASSERT_DRVINS(pDrvIns);
    863914    LogFlow(("pdmR3DrvDetach: pDrvIns=%p '%s'/%d\n", pDrvIns, pDrvIns->pReg->szName, pDrvIns->iInstance));
    864     VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
     915    VM_ASSERT_EMT(pVM);
    865916
    866917    /*
     
    891942     * Join paths with pdmR3DrvDestroyChain.
    892943     */
    893     pdmR3DrvDestroyChain(pDrvIns, fFlags);
     944    pdmR3DrvDestroyChain(pVM, pDrvIns, fFlags);
     945
    894946    return VINF_SUCCESS;
    895947}
     
    901953 * This is used when unplugging a device at run time.
    902954 *
     955 * @note    The caller must enter CoreListCritSectRw with exclusive access
     956 *          rights before the call.  This function _will_ release the lock for
     957 *          doing callbacks and cleaning up resources associated with the
     958 *          drivers being destroyed, but it will still own it upon return.
     959 *
     960 * @param   pVM         The cross context VM structure.
    903961 * @param   pDrvIns     Pointer to the driver instance to start with.
    904962 * @param   fFlags      PDM_TACH_FLAGS_NOT_HOT_PLUG, PDM_TACH_FLAGS_NO_CALLBACKS
    905963 *                      or 0.
    906964 */
    907 void pdmR3DrvDestroyChain(PPDMDRVINS pDrvIns, uint32_t fFlags)
    908 {
    909     PVM pVM = pDrvIns->Internal.s.pVMR3;
     965void pdmR3DrvDestroyChain(PVM pVM, PPDMDRVINS pDrvIns, uint32_t fFlags)
     966{
     967    Assert(pDrvIns->Internal.s.pVMR3 == pVM);
    910968    VM_ASSERT_EMT(pVM);
     969
     970    /* Exclusive lock ownership w/o any extra recursions. */
     971    Assert(RTCritSectRwIsWriteOwner(&pVM->pdm.s.CoreListCritSectRw));
     972    Assert(RTCritSectRwGetWriteRecursion(&pVM->pdm.s.CoreListCritSectRw) == 1);
    911973
    912974    /*
    913975     * Detach the bottommost driver until we've detached pDrvIns.
    914976     */
     977    Assert(!pDrvIns->Internal.s.fDetaching);
    915978    pDrvIns->Internal.s.fDetaching = true;
     979
    916980    PPDMDRVINS pCur;
    917981    do
     
    9401004
    9411005            if (!(fFlags & PDM_TACH_FLAGS_NO_CALLBACKS) && pParent->pReg->pfnDetach)
     1006            {
     1007                RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    9421008                pParent->pReg->pfnDetach(pParent, fFlags);
     1009                RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     1010            }
    9431011
    9441012            pParent->pDownBase = NULL;
     
    9551023                    if (pLun->pDevIns->pReg->pfnDetach)
    9561024                    {
     1025                        RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    9571026                        PDMCritSectEnter(pVM, pLun->pDevIns->pCritSectRoR3, VERR_IGNORED);
    9581027                        pLun->pDevIns->pReg->pfnDetach(pLun->pDevIns, pLun->iLun, fFlags);
    9591028                        PDMCritSectLeave(pVM, pLun->pDevIns->pCritSectRoR3);
     1029                        RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
    9601030                    }
    9611031                }
     
    9641034                    if (pLun->pUsbIns->pReg->pfnDriverDetach)
    9651035                    {
     1036                        RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    9661037                        /** @todo USB device locking? */
    9671038                        pLun->pUsbIns->pReg->pfnDriverDetach(pLun->pUsbIns, pLun->iLun, fFlags);
     1039                        RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
    9681040                    }
    9691041                }
     
    9721044
    9731045        /*
    974          * Call destructor.
     1046         * Unlink the instance and call the destructor.
    9751047         */
     1048        PPDMDRVINS pPrevEntry = pCur->Internal.s.pDrv->pInstances;
     1049        if (pCur == pPrevEntry)
     1050            pCur->Internal.s.pDrv->pInstances = pCur->Internal.s.pNext;
     1051        else if (pPrevEntry)
     1052        {
     1053            PPDMDRVINS pCurEntry = pPrevEntry->Internal.s.pNext;
     1054            while (pCurEntry != pCur && pCurEntry)
     1055            {
     1056                pPrevEntry = pCurEntry;
     1057                pCurEntry = pCurEntry->Internal.s.pNext;
     1058            }
     1059            AssertLogRelMsg(pCurEntry == pCur, ("%s", pCur->pReg->szName));
     1060            if (pCurEntry == pCur)
     1061                pPrevEntry->Internal.s.pNext = pCur->Internal.s.pNext;
     1062        }
     1063        else
     1064            AssertLogRelMsg(pPrevEntry != NULL, ("%s", pCur->pReg->szName));
     1065
    9761066        pCur->pUpBase = NULL;
     1067        pCur->Internal.s.pDrv->cInstances--;
     1068
     1069        RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
     1070
    9771071        if (pCur->pReg->pfnDestruct)
    9781072            pCur->pReg->pfnDestruct(pCur);
    979         pCur->Internal.s.pDrv->cInstances--;
    9801073
    9811074        /*
     
    10261119            MMR3HeapFree(pCur);
    10271120
     1121        RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
    10281122    } while (pCur != pDrvIns);
    10291123}
    10301124
     1125
     1126/**
     1127 * Enumerates driver instances via a callback.
     1128 *
     1129 * @returns VBox status code.
     1130 * @retval  VERR_PDM_DRIVER_NOT_FOUND if @a pszDriver isn't registered.
     1131 * @param   pUVM            The user mode VM handle.
     1132 * @param   pszDriver       The name of the driver which instances should be
     1133 *                          enumerated.
     1134 * @param   pfnCallback     The callback function.
     1135 * @param   pvUser          User argument.
     1136 */
     1137VMMR3DECL(int) PDMR3DriverEnumInstances(PUVM pUVM, const char *pszDriver, PFNPDMENUMDRVINS pfnCallback, void *pvUser)
     1138{
     1139    LogFlow(("PDMR3DriverEnumInstances: pszDriver=%p:{%s} pfnCallback=%p pvUser=%p\n", pszDriver, pszDriver, pfnCallback, pvUser));
     1140    UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
     1141    PVM const pVM = pUVM->pVM;
     1142    VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
     1143
     1144    /*
     1145     * First find the PDMDRV entry for the driver.
     1146     */
     1147    int           rc   = VINF_SUCCESS;
     1148    RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
     1149    PPDMDRV const pDrv = pdmR3DrvLookup(pVM, pszDriver);
     1150    if (pDrv)
     1151    {
     1152        /*
     1153         * Walk the list of instances.
     1154         */
     1155        for (PPDMDRVINSR3 pDrvIns = pDrv->pInstances; pDrvIns; pDrvIns = pDrvIns->Internal.s.pNext)
     1156        {
     1157            if (!pDrvIns->Internal.s.fDetaching) /* paranoia */
     1158            {
     1159                PPDMLUN const       pLun         = pDrvIns->Internal.s.pLun;
     1160                PPDMDEVINS const    pDevIns      = pLun->pDevIns;
     1161                const char * const  pszDevName   = pDevIns ? pDevIns->pReg->szName : pLun->pUsbIns->pReg->szName;
     1162                uint32_t const      uDevInstance = pDevIns ? pDevIns->iInstance    : pLun->pUsbIns->iInstance;
     1163                int rc2 = pfnCallback(&pDrvIns->IBase, pDrvIns->iInstance, pDevIns == NULL,
     1164                                      pszDevName, uDevInstance, pLun->iLun, pvUser);
     1165                if (RT_FAILURE(rc2))
     1166                {
     1167                    rc = rc2;
     1168                    break;
     1169                }
     1170            }
     1171        }
     1172    }
     1173    else
     1174        AssertFailedStmt(rc = VERR_PDM_DRIVER_NOT_FOUND);
     1175    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
     1176    return rc;
     1177}
    10311178
    10321179
     
    10491196     * Check that there isn't anything attached already.
    10501197     */
     1198    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
    10511199    int rc;
    10521200    if (!pDrvIns->Internal.s.pDown)
     
    10681216        rc = VERR_PDM_DRIVER_ALREADY_ATTACHED;
    10691217    }
     1218    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    10701219
    10711220    LogFlow(("pdmR3DrvHlp_Attach: caller='%s'/%d: return %Rrc\n",
     
    10811230    LogFlow(("pdmR3DrvHlp_Detach: caller='%s'/%d: fFlags=%#x\n",
    10821231             pDrvIns->pReg->szName, pDrvIns->iInstance, fFlags));
    1083     VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
     1232    PVM const pVM =  pDrvIns->Internal.s.pVMR3;
     1233    VM_ASSERT_EMT(pVM);
    10841234
    10851235    /*
     
    10871237     */
    10881238    int rc;
     1239    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
    10891240    if (pDrvIns->Internal.s.pDown)
    1090         rc = pdmR3DrvDetach(pDrvIns->Internal.s.pDown, fFlags);
     1241        rc = pdmR3DrvDetach(pVM, pDrvIns->Internal.s.pDown, fFlags);
    10911242    else
    10921243    {
     
    10941245        rc = VERR_PDM_NO_DRIVER_ATTACHED;
    10951246    }
     1247    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    10961248
    10971249    LogFlow(("pdmR3DrvHlp_Detach: caller='%s'/%d: returns %Rrc\n",
     
    11071259    LogFlow(("pdmR3DrvHlp_DetachSelf: caller='%s'/%d: fFlags=%#x\n",
    11081260             pDrvIns->pReg->szName, pDrvIns->iInstance, fFlags));
    1109     VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
    1110 
    1111     int rc = pdmR3DrvDetach(pDrvIns, fFlags);
     1261    PVM const pVM = pDrvIns->Internal.s.pVMR3;
     1262    VM_ASSERT_EMT(pVM);
     1263
     1264    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     1265    int rc = pdmR3DrvDetach(pVM, pDrvIns, fFlags);
     1266    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    11121267
    11131268    LogFlow(("pdmR3DrvHlp_Detach: returns %Rrc\n", rc)); /* pDrvIns is freed by now. */
     
    11221277    LogFlow(("pdmR3DrvHlp_MountPrepare: caller='%s'/%d: pszFilename=%p:{%s} pszCoreDriver=%p:{%s}\n",
    11231278             pDrvIns->pReg->szName, pDrvIns->iInstance, pszFilename, pszFilename, pszCoreDriver, pszCoreDriver));
    1124     VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
     1279    PVM const pVM = pDrvIns->Internal.s.pVMR3;
     1280    VM_ASSERT_EMT(pVM);
     1281
     1282    /* We're using CoreListCritSectRw for some setup & CFGM serialization here. */
     1283    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
    11251284
    11261285    /*
     
    11291288    if (pDrvIns->Internal.s.pDown)
    11301289    {
     1290        RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    11311291        AssertMsgFailed(("Cannot prepare a mount when something's attached to you!\n"));
    11321292        return VERR_PDM_DRIVER_ALREADY_ATTACHED;
     
    11371297     * attached configuration tree.
    11381298     */
    1139     PCFGMNODE   pNode = CFGMR3GetChild(pDrvIns->Internal.s.pCfgHandle, "AttachedDriver");
     1299    PCFGMNODE pNode = CFGMR3GetChild(pDrvIns->Internal.s.pCfgHandle, "AttachedDriver");
    11401300    if (pNode)
    11411301        CFGMR3RemoveNode(pNode);
     
    11471307    {
    11481308        /** @todo implement image probing. */
     1309        RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    11491310        AssertReleaseMsgFailed(("Not implemented!\n"));
    11501311        return VERR_NOT_IMPLEMENTED;
     
    11671328                if (RT_SUCCESS(rc))
    11681329                {
     1330                    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    11691331                    LogFlow(("pdmR3DrvHlp_MountPrepare: caller='%s'/%d: returns %Rrc (Driver=%s)\n",
    11701332                             pDrvIns->pReg->szName, pDrvIns->iInstance, rc, pszCoreDriver));
    11711333                    return rc;
    11721334                }
    1173                 else
    1174                     AssertMsgFailed(("Path string insert failed, rc=%Rrc\n", rc));
     1335                AssertMsgFailed(("Path string insert failed, rc=%Rrc\n", rc));
    11751336            }
    11761337            else
     
    11841345        AssertMsgFailed(("AttachedDriver node insert failed, rc=%Rrc\n", rc));
    11851346
     1347    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    11861348    LogFlow(("pdmR3DrvHlp_MountPrepare: caller='%s'/%d: returns %Rrc\n",
    11871349             pDrvIns->pReg->szName, pDrvIns->iInstance, rc));
     
    12261388{
    12271389    PDMDRV_ASSERT_DRVINS(pDrvIns);
    1228     int rc2 = VMSetErrorV(pDrvIns->Internal.s.pVMR3, rc, RT_SRC_POS_ARGS, pszFormat, va); Assert(rc2 == rc); NOREF(rc2);
     1390    int const rc2 = VMSetErrorV(pDrvIns->Internal.s.pVMR3, rc, RT_SRC_POS_ARGS, pszFormat, va);
     1391    Assert(rc2 == rc); RT_NOREF_PV(rc2);
    12291392    return rc;
    12301393}
     
    14071570    PDMDRV_ASSERT_DRVINS(pDrvIns);
    14081571    VM_ASSERT_EMT(pDrvIns->Internal.s.pVMR3);
    1409     LogFlow(("pdmR3DrvHlp_SSMRegister: caller='%s'/%d: uVersion=%#x cbGuess=%#x \n"
     1572    LogFlow(("pdmR3DrvHlp_SSMRegister: caller='%s'/%d: uVersion=%#x cbGuess=%#x\n"
    14101573             "    pfnLivePrep=%p pfnLiveExec=%p pfnLiveVote=%p  pfnSavePrep=%p pfnSaveExec=%p pfnSaveDone=%p pszLoadPrep=%p pfnLoadExec=%p pfnLoaddone=%p\n",
    14111574             pDrvIns->pReg->szName, pDrvIns->iInstance, uVersion, cbGuess,
     
    14531616
    14541617/** @interface_method_impl{PDMDRVHLPR3,pfnDBGFInfoRegister} */
    1455 static DECLCALLBACK(int) pdmR3DrvHlp_DBGFInfoRegister(PPDMDRVINS pDrvIns, const char *pszName, const char *pszDesc, PFNDBGFHANDLERDRV pfnHandler)
     1618static DECLCALLBACK(int) pdmR3DrvHlp_DBGFInfoRegister(PPDMDRVINS pDrvIns, const char *pszName, const char *pszDesc,
     1619                                                      PFNDBGFHANDLERDRV pfnHandler)
    14561620{
    14571621    PDMDRV_ASSERT_DRVINS(pDrvIns);
     
    14671631
    14681632/** @interface_method_impl{PDMDRVHLPR3,pfnDBGFInfoRegisterArgv} */
    1469 static DECLCALLBACK(int) pdmR3DrvHlp_DBGFInfoRegisterArgv(PPDMDRVINS pDrvIns, const char *pszName, const char *pszDesc, PFNDBGFINFOARGVDRV pfnHandler)
     1633static DECLCALLBACK(int) pdmR3DrvHlp_DBGFInfoRegisterArgv(PPDMDRVINS pDrvIns, const char *pszName, const char *pszDesc,
     1634                                                          PFNDBGFINFOARGVDRV pfnHandler)
    14701635{
    14711636    PDMDRV_ASSERT_DRVINS(pDrvIns);
     
    15161681
    15171682/** @interface_method_impl{PDMDRVHLPR3,pfnSTAMRegisterV} */
    1518 static DECLCALLBACK(void) pdmR3DrvHlp_STAMRegisterV(PPDMDRVINS pDrvIns, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility,
    1519                                                     STAMUNIT enmUnit, const char *pszDesc, const char *pszName, va_list args)
     1683static DECLCALLBACK(void) pdmR3DrvHlp_STAMRegisterV(PPDMDRVINS pDrvIns, void *pvSample, STAMTYPE enmType,
     1684                                                    STAMVISIBILITY enmVisibility, STAMUNIT enmUnit, const char *pszDesc,
     1685                                                    const char *pszName, va_list args)
    15201686{
    15211687    PDMDRV_ASSERT_DRVINS(pDrvIns);
     
    15461712
    15471713/** @interface_method_impl{PDMDRVHLPR3,pfnSTAMRegisterF} */
    1548 static DECLCALLBACK(void) pdmR3DrvHlp_STAMRegisterF(PPDMDRVINS pDrvIns, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility,
    1549                                                     STAMUNIT enmUnit, const char *pszDesc, const char *pszName, ...)
     1714static DECLCALLBACK(void) pdmR3DrvHlp_STAMRegisterF(PPDMDRVINS pDrvIns, void *pvSample, STAMTYPE enmType,
     1715                                                    STAMVISIBILITY enmVisibility, STAMUNIT enmUnit, const char *pszDesc,
     1716                                                    const char *pszName, ...)
    15501717{
    15511718    va_list va;
     
    15921759    if (    uOperation >= VMMR0_DO_SRV_START
    15931760        &&  uOperation <  VMMR0_DO_SRV_END)
    1594         rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pDrvIns->Internal.s.pVMR3), NIL_VMCPUID, uOperation, 0, (PSUPVMMR0REQHDR)pvArg);
     1761        rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pDrvIns->Internal.s.pVMR3), NIL_VMCPUID,
     1762                              uOperation, 0, (PSUPVMMR0REQHDR)pvArg);
    15951763    else
    15961764    {
     
    16051773
    16061774/** @interface_method_impl{PDMDRVHLPR3,pfnUSBRegisterHub} */
    1607 static DECLCALLBACK(int) pdmR3DrvHlp_USBRegisterHub(PPDMDRVINS pDrvIns, uint32_t fVersions, uint32_t cPorts, PCPDMUSBHUBREG pUsbHubReg, PPCPDMUSBHUBHLP ppUsbHubHlp)
     1775static DECLCALLBACK(int) pdmR3DrvHlp_USBRegisterHub(PPDMDRVINS pDrvIns, uint32_t fVersions, uint32_t cPorts,
     1776                                                    PCPDMUSBHUBREG pUsbHubReg, PPCPDMUSBHUBHLP ppUsbHubHlp)
    16081777{
    16091778    PDMDRV_ASSERT_DRVINS(pDrvIns);
     
    16761845
    16771846/** @interface_method_impl{PDMDRVHLPR3,pfnThreadCreate} */
    1678 static DECLCALLBACK(int) pdmR3DrvHlp_ThreadCreate(PPDMDRVINS pDrvIns, PPPDMTHREAD ppThread, void *pvUser, PFNPDMTHREADDRV pfnThread,
    1679                                                   PFNPDMTHREADWAKEUPDRV pfnWakeup, size_t cbStack, RTTHREADTYPE enmType, const char *pszName)
     1847static DECLCALLBACK(int) pdmR3DrvHlp_ThreadCreate(PPDMDRVINS pDrvIns, PPPDMTHREAD ppThread, void *pvUser,
     1848                                                  PFNPDMTHREADDRV pfnThread, PFNPDMTHREADWAKEUPDRV pfnWakeup, size_t cbStack,
     1849                                                  RTTHREADTYPE enmType, const char *pszName)
    16801850{
    16811851    PDMDRV_ASSERT_DRVINS(pDrvIns);
  • trunk/src/VBox/VMM/VMMR3/PDMUsb.cpp

    r98122 r106022  
    142142     * Check for duplicate registration and find the last hub for FIFO registration.
    143143     */
     144    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     145
    144146    PPDMUSBHUB pPrev = NULL;
    145147    for (PPDMUSBHUB pCur = pVM->pdm.s.pUsbHubs; pCur; pCur = pCur->pNext)
    146148    {
    147149        if (pCur->pDrvIns == pDrvIns)
     150        {
     151            RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    148152            return VERR_PDM_USB_HUB_EXISTS;
     153        }
    149154        pPrev = pCur;
    150155    }
     
    155160    PPDMUSBHUB pHub = (PPDMUSBHUB)MMR3HeapAlloc(pVM, MM_TAG_PDM_DRIVER, sizeof(*pHub));
    156161    if (!pHub)
     162    {
     163        RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    157164        return VERR_NO_MEMORY;
     165    }
    158166
    159167    pHub->fVersions = fVersions;
     
    169177    else
    170178        pVM->pdm.s.pUsbHubs = pHub;
     179
     180    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    171181
    172182    Log(("PDM: Registered USB hub %p/%s\n", pDrvIns, pDrvIns->pReg->szName));
     
    251261     * Check for duplicate and find FIFO entry at the same time.
    252262     */
    253     PCPDMUSBREGCBINT pRegCB = (PCPDMUSBREGCBINT)pCallbacks;
     263    PCPDMUSBREGCBINT const pRegCB   = (PCPDMUSBREGCBINT)pCallbacks;
     264    PVM const              pVM      = pRegCB->pVM;
     265    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     266
    254267    PPDMUSB pUsbPrev = NULL;
    255     PPDMUSB pUsb = pRegCB->pVM->pdm.s.pUsbDevs;
     268    PPDMUSB pUsb     = pVM->pdm.s.pUsbDevs;
    256269    for (; pUsb; pUsbPrev = pUsb, pUsb = pUsb->pNext)
    257         AssertMsgReturn(strcmp(pUsb->pReg->szName, pReg->szName),
    258                         ("USB Device '%s' already exists\n", pReg->szName),
    259                         VERR_PDM_USB_NAME_CLASH);
     270        AssertMsgReturnStmt(strcmp(pUsb->pReg->szName, pReg->szName),
     271                            ("USB Device '%s' already exists\n", pReg->szName),
     272                            RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw),
     273                            VERR_PDM_USB_NAME_CLASH);
    260274
    261275    /*
    262276     * Allocate new device structure and insert it into the list.
    263277     */
    264     pUsb = (PPDMUSB)MMR3HeapAlloc(pRegCB->pVM, MM_TAG_PDM_DEVICE, sizeof(*pUsb));
     278    pUsb = (PPDMUSB)MMR3HeapAlloc(pVM, MM_TAG_PDM_DEVICE, sizeof(*pUsb));
    265279    if (pUsb)
    266280    {
     
    274288            pUsbPrev->pNext = pUsb;
    275289        else
    276             pRegCB->pVM->pdm.s.pUsbDevs = pUsb;
     290            pVM->pdm.s.pUsbDevs = pUsb;
     291
     292        RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    277293        Log(("PDM: Registered USB device '%s'\n", pReg->szName));
    278294        return VINF_SUCCESS;
    279295    }
     296
     297    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    280298    return VERR_NO_MEMORY;
    281299}
     
    402420int pdmR3UsbVMInitComplete(PVM pVM)
    403421{
     422    RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
    404423    for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
    405424    {
    406425        if (pUsbIns->pReg->pfnVMInitComplete)
    407426        {
     427            RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    408428            int rc = pUsbIns->pReg->pfnVMInitComplete(pUsbIns);
    409429            if (RT_FAILURE(rc))
     
    413433                return rc;
    414434            }
    415         }
    416     }
     435            RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
     436        }
     437    }
     438    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    417439    return VINF_SUCCESS;
    418440}
     
    425447PPDMUSB pdmR3UsbLookup(PVM pVM, const char *pszName)
    426448{
    427     size_t cchName = strlen(pszName);
     449    size_t const cchName = strlen(pszName);
     450
     451    RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
    428452    for (PPDMUSB pUsb = pVM->pdm.s.pUsbDevs; pUsb; pUsb = pUsb->pNext)
    429         if (    pUsb->cchName == cchName
    430             &&  !strcmp(pUsb->pReg->szName, pszName))
     453        if (   pUsb->cchName == cchName
     454            && !strcmp(pUsb->pReg->szName, pszName))
     455        {
     456            RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    431457            return pUsb;
     458        }
     459    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    432460    return NULL;
    433461}
     
    449477        return VERR_PDM_NO_USB_HUBS;
    450478
     479    RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
    451480    for (PPDMUSBHUB pCur = pVM->pdm.s.pUsbHubs; pCur; pCur = pCur->pNext)
    452481        if (pCur->cAvailablePorts > 0)
     
    459488            }
    460489            /* For high-speed USB 2.0 devices only, allow USB 1.1 fallback. */
    461             if ((iUsbVersion & VUSB_STDVER_20) && (pCur->fVersions == VUSB_STDVER_11))
     490            if ((iUsbVersion & VUSB_STDVER_20) && pCur->fVersions == VUSB_STDVER_11)
    462491                *ppHub = pCur;
    463492        }
     493    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    464494    if (*ppHub)
    465495        return VINF_SUCCESS;
     
    662692     * Link it into all the lists.
    663693     */
     694    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     695
    664696    /* The global instance FIFO. */
    665697    PPDMUSBINS pPrev1 = pVM->pdm.s.pUsbInstances;
     
    689721        pPrev2->Internal.s.pPerDeviceNext = pUsbIns;
    690722    }
     723
     724    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    691725
    692726    /*
     
    11381172     * Destroy the luns with their driver chains and call the device destructor.
    11391173     */
     1174    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
    11401175    while (pUsbIns->Internal.s.pLuns)
    11411176    {
     
    11431178        pUsbIns->Internal.s.pLuns = pLun->pNext;
    11441179        if (pLun->pTop)
    1145             pdmR3DrvDestroyChain(pLun->pTop, PDM_TACH_FLAGS_NOT_HOT_PLUG); /* Hotplugging is handled differently here atm. */
     1180            pdmR3DrvDestroyChain(pVM, pLun->pTop, PDM_TACH_FLAGS_NOT_HOT_PLUG); /* Hotplugging is handled differently here atm. */
    11461181        MMR3HeapFree(pLun);
    11471182    }
     1183    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    11481184
    11491185    /* finally, the device. */
     
    11631199     * Unlink it.
    11641200     */
     1201    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     1202
    11651203    /* The global instance FIFO. */
    11661204    if (pVM->pdm.s.pUsbInstances == pUsbIns)
     
    11961234    }
    11971235
     1236    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
     1237
    11981238    /*
    11991239     * Trash it.
     
    12251265     */
    12261266    UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
    1227     PVM pVM = pUVM->pVM;
     1267    PVM const pVM = pUVM->pVM;
    12281268    VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
    12291269    VM_ASSERT_EMT(pVM);
     
    12331273     * Search the global list for it.
    12341274     */
     1275    RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
    12351276    PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances;
    12361277    for ( ; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
     
    12381279            break;
    12391280    if (!pUsbIns)
     1281    {
     1282        RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    12401283        return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND; /** @todo VERR_PDM_USB_INSTANCE_NOT_FOUND */
     1284    }
    12411285
    12421286    /*
     
    12441288     */
    12451289    PPDMUSBHUB pHub = pUsbIns->Internal.s.pHub;
     1290    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw); /** @todo would be nice to call the pfnDetachDevice owning this.. */
    12461291    if (pHub)
    12471292    {
     
    12541299        }
    12551300
     1301        RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
    12561302        pHub->cAvailablePorts++;
    12571303        Assert(pHub->cAvailablePorts > 0 && pHub->cAvailablePorts <= pHub->cPorts);
    12581304        pUsbIns->Internal.s.pHub = NULL;
     1305        RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    12591306    }
    12601307
     
    12921339 * @param   iLun            The Logical Unit to obtain the interface of.
    12931340 * @param   ppLun           Where to store the pointer to the LUN if found.
    1294  * @thread  Try only do this in EMT...
    12951341 */
    12961342static int pdmR3UsbFindLun(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPPDMLUN ppLun)
     
    12991345     * Iterate registered devices looking for the device.
    13001346     */
    1301     size_t cchDevice = strlen(pszDevice);
     1347    size_t const cchDevice = strlen(pszDevice);
     1348    RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
    13021349    for (PPDMUSB pUsbDev = pVM->pdm.s.pUsbDevs; pUsbDev; pUsbDev = pUsbDev->pNext)
    13031350    {
    1304         if (    pUsbDev->cchName == cchDevice
    1305             &&  !memcmp(pUsbDev->pReg->szName, pszDevice, cchDevice))
     1351        if (   pUsbDev->cchName == cchDevice
     1352            && !memcmp(pUsbDev->pReg->szName, pszDevice, cchDevice))
    13061353        {
    13071354            /*
     
    13201367                        {
    13211368                            *ppLun = pLun;
     1369                            RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    13221370                            return VINF_SUCCESS;
    13231371                        }
    13241372                    }
     1373                    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    13251374                    return VERR_PDM_LUN_NOT_FOUND;
    13261375                }
    13271376            }
     1377            RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    13281378            return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND;
    13291379        }
    13301380    }
     1381    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    13311382    return VERR_PDM_DEVICE_NOT_FOUND;
    13321383}
     
    13551406    LogFlow(("PDMR3UsbDriverAttach: pszDevice=%p:{%s} iDevIns=%d iLun=%d fFlags=%#x ppBase=%p\n",
    13561407             pszDevice, pszDevice, iDevIns, iLun, fFlags, ppBase));
     1408    if (ppBase)
     1409        *ppBase = NULL;
    13571410    UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
    1358     PVM pVM = pUVM->pVM;
     1411    PVM const pVM = pUVM->pVM;
    13591412    VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
    13601413    VM_ASSERT_EMT(pVM);
    1361 
    1362     if (ppBase)
    1363         *ppBase = NULL;
    13641414
    13651415    /*
     
    14531503     * Find the LUN in question.
    14541504     */
     1505    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
     1506
    14551507    PPDMLUN pLun;
    14561508    int rc = pdmR3UsbFindLun(pVM, pszDevice, iDevIns, iLun, &pLun);
     
    14771529            }
    14781530            if (pDrvIns)
    1479                 rc = pdmR3DrvDetach(pDrvIns, fFlags);
    1480             else
    1481                 rc = VERR_PDM_DRIVER_INSTANCE_NOT_FOUND;
     1531            {
     1532                rc = pdmR3DrvDetach(pVM, pDrvIns, fFlags);
     1533
     1534                RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
     1535                LogFlow(("PDMR3UsbDriverDetach: returns %Rrc (pdmR3DrvDetach)\n", rc));
     1536                return rc;
     1537            }
     1538            rc = VERR_PDM_DRIVER_INSTANCE_NOT_FOUND;
    14821539        }
    14831540        else
     
    14851542    }
    14861543
     1544    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    14871545    LogFlow(("PDMR3UsbDriverDetach: returns %Rrc\n", rc));
    14881546    return rc;
     
    15061564    LogFlow(("PDMR3UsbQueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
    15071565             pszDevice, pszDevice, iInstance, iLun, ppBase));
     1566    *ppBase = NULL;
    15081567    UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
    15091568    PVM pVM = pUVM->pVM;
     
    15131572     * Find the LUN.
    15141573     */
     1574    RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
     1575
    15151576    PPDMLUN pLun;
    15161577    int rc = pdmR3UsbFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
     
    15201581        {
    15211582            *ppBase = &pLun->pTop->IBase;
     1583
     1584            RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    15221585            LogFlow(("PDMR3UsbQueryLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
    15231586            return VINF_SUCCESS;
     
    15251588        rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
    15261589    }
     1590
     1591    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    15271592    LogFlow(("PDMR3UsbQueryLun: returns %Rrc\n", rc));
    15281593    return rc;
     
    15521617    LogFlow(("PDMR3QueryDriverOnLun: pszDevice=%p:{%s} iInstance=%u iLun=%u pszDriver=%p:{%s} ppBase=%p\n",
    15531618             pszDevice, pszDevice, iInstance, iLun, pszDriver, pszDriver, ppBase));
     1619    *ppBase = NULL;
    15541620    UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
    15551621    PVM pVM = pUVM->pVM;
     
    15591625     * Find the LUN.
    15601626     */
     1627    RTCritSectRwEnterShared(&pVM->pdm.s.CoreListCritSectRw);
     1628
    15611629    PPDMLUN pLun;
    15621630    int rc = pdmR3UsbFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
     
    15691637                {
    15701638                    *ppBase = &pDrvIns->IBase;
     1639
     1640                    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    15711641                    LogFlow(("PDMR3UsbQueryDriverOnLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
    15721642                    return VINF_SUCCESS;
    1573 
    15741643                }
    15751644            rc = VERR_PDM_DRIVER_NOT_FOUND;
     
    15781647            rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
    15791648    }
     1649
     1650    RTCritSectRwLeaveShared(&pVM->pdm.s.CoreListCritSectRw);
    15801651    LogFlow(("PDMR3UsbQueryDriverOnLun: returns %Rrc\n", rc));
    15811652    return rc;
     
    16001671     * Lookup the LUN, it might already be registered.
    16011672     */
     1673    RTCritSectRwEnterExcl(&pVM->pdm.s.CoreListCritSectRw);
    16021674    PPDMLUN pLunPrev = NULL;
    16031675    PPDMLUN pLun = pUsbIns->Internal.s.pLuns;
     
    16111683    if (!pLun)
    16121684    {
    1613         if (    !pBaseInterface
    1614             ||  !pszDesc
    1615             ||  !*pszDesc)
    1616         {
     1685        if (   !pBaseInterface
     1686            || !pszDesc
     1687            || !*pszDesc)
     1688        {
     1689            RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    16171690            Assert(pBaseInterface);
    16181691            Assert(pszDesc || *pszDesc);
     
    16221695        pLun = (PPDMLUN)MMR3HeapAlloc(pVM, MM_TAG_PDM_LUN, sizeof(*pLun));
    16231696        if (!pLun)
     1697        {
     1698            RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    16241699            return VERR_NO_MEMORY;
     1700        }
    16251701
    16261702        pLun->iLun      = iLun;
     
    16411717    else if (pLun->pTop)
    16421718    {
     1719        RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    16431720        AssertMsgFailed(("Already attached! The device should keep track of such things!\n"));
    16441721        LogFlow(("pdmR3UsbHlp_DriverAttach: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, VERR_PDM_DRIVER_ALREADY_ATTACHED));
     
    16571734    else
    16581735        rc = VERR_PDM_NO_ATTACHED_DRIVER;
     1736    RTCritSectRwLeaveExcl(&pVM->pdm.s.CoreListCritSectRw);
    16591737
    16601738
  • trunk/src/VBox/VMM/VMMR3/VMMR3.def

    r102092 r106022  
    281281    PDMR3DriverAttach
    282282    PDMR3DriverDetach
     283    PDMR3DriverEnumInstances
    283284    PDMR3NsBwGroupSetLimit
    284285    PDMR3QueryDeviceLun
  • trunk/src/VBox/VMM/include/PDMInternal.h

    r98570 r106022  
    417417    /** Pointer to the ring-0 request handler function. */
    418418    PFNPDMDRVREQHANDLERR0           pfnReqHandlerR0;
     419    /** Pointer to the next instance (starts at PDMDRV::pInstances). */
     420    R3PTRTYPE(PPDMDRVINS)           pNext;
    419421} PDMDRVINSINT;
    420422
     
    610612    PPDMDRVINS                      pBottom;
    611613    /** Pointer to the device instance which the LUN belongs to.
    612      * Either this is set or pUsbIns is set. Both is never set at the same time. */
     614     * Either this is set or pUsbIns is set. Both are never set at the same time. */
    613615    PPDMDEVINS                      pDevIns;
    614616    /** Pointer to the USB device instance which the LUN belongs to. */
     
    698700    /** The search path for ring-0 context modules (';' as separator). */
    699701    char                           *pszR0SearchPath;
     702    /** Pointer to chain of instances. */
     703    PPDMDRVINSR3                    pInstances;
    700704} PDMDRV;
    701705
     
    14641468     * concurrently. */
    14651469    PDMCRITSECT                     NopCritSect;
     1470    /** Critical read/write section protecting the core list: devices,
     1471     *  usb devices, drivers.
     1472     * @note The PDMUSERPERVM::ListCritSect lock covers queues and the stuff in
     1473     *       PDMUSERPERVM. */
     1474    RTCRITSECTRW                    CoreListCritSectRw;
    14661475
    14671476    /** The ring-0 capable task sets (max 128). */
     
    15041513    /** The registered RTC device. */
    15051514    R3PTRTYPE(PPDMRTC)              pRtc;
    1506     /** The registered USB HUBs. (FIFO) */
     1515    /** The registered USB HUBs. (FIFO)
     1516     * @note Protected by CoreListCritSectRw. */
    15071517    R3PTRTYPE(PPDMUSBHUB)           pUsbHubs;
    15081518
    15091519    /** @name Queues
     1520     * @note Protected by PDMUSERPERVM::ListCritSect.
    15101521     * @{ */
    15111522    /** Number of ring-0 capable queues in apQueues. */
     
    16521663    /** @todo move more stuff over here. */
    16531664
    1654     /** Lock protecting the lists below it and the queue list. */
     1665    /** Lock protecting the lists below it and the queue list (in PDM). */
    16551666    RTCRITSECT                      ListCritSect;
    16561667    /** Pointer to list of loaded modules. */
     
    17721783int         pdmR3DrvInstantiate(PVM pVM, PCFGMNODE pNode, PPDMIBASE pBaseInterface, PPDMDRVINS pDrvAbove,
    17731784                                PPDMLUN pLun, PPDMIBASE *ppBaseInterface);
    1774 int         pdmR3DrvDetach(PPDMDRVINS pDrvIns, uint32_t fFlags);
    1775 void        pdmR3DrvDestroyChain(PPDMDRVINS pDrvIns, uint32_t fFlags);
     1785int         pdmR3DrvDetach(PVM pVM, PPDMDRVINS pDrvIns, uint32_t fFlags);
     1786void        pdmR3DrvDestroyChain(PVM pVM, PPDMDRVINS pDrvIns, uint32_t fFlags);
    17761787PPDMDRV     pdmR3DrvLookup(PVM pVM, const char *pszName);
    17771788
Note: See TracChangeset for help on using the changeset viewer.

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