[23] | 1 | /* $Id: PDMCritSect.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
[12983] | 3 | * PDM - Critical Sections, Ring-3.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[82968] | 7 | * Copyright (C) 2006-2020 Oracle Corporation
|
---|
[1] | 8 | *
|
---|
| 9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
| 10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
| 11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
[5999] | 12 | * General Public License (GPL) as published by the Free Software
|
---|
| 13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
| 14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
| 15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
[1] | 16 | */
|
---|
| 17 |
|
---|
| 18 |
|
---|
[57358] | 19 | /*********************************************************************************************************************************
|
---|
| 20 | * Header Files *
|
---|
| 21 | *********************************************************************************************************************************/
|
---|
[1] | 22 | #define LOG_GROUP LOG_GROUP_PDM//_CRITSECT
|
---|
| 23 | #include "PDMInternal.h"
|
---|
[35346] | 24 | #include <VBox/vmm/pdmcritsect.h>
|
---|
[45152] | 25 | #include <VBox/vmm/pdmcritsectrw.h>
|
---|
[35346] | 26 | #include <VBox/vmm/mm.h>
|
---|
| 27 | #include <VBox/vmm/vm.h>
|
---|
| 28 | #include <VBox/vmm/uvm.h>
|
---|
[19991] | 29 |
|
---|
[1] | 30 | #include <VBox/err.h>
|
---|
| 31 | #include <VBox/log.h>
|
---|
[20008] | 32 | #include <VBox/sup.h>
|
---|
[1] | 33 | #include <iprt/asm.h>
|
---|
| 34 | #include <iprt/assert.h>
|
---|
[25406] | 35 | #include <iprt/lockvalidator.h>
|
---|
[19991] | 36 | #include <iprt/string.h>
|
---|
[1] | 37 | #include <iprt/thread.h>
|
---|
| 38 |
|
---|
| 39 |
|
---|
[57358] | 40 | /*********************************************************************************************************************************
|
---|
| 41 | * Internal Functions *
|
---|
| 42 | *********************************************************************************************************************************/
|
---|
[28258] | 43 | static int pdmR3CritSectDeleteOne(PVM pVM, PUVM pUVM, PPDMCRITSECTINT pCritSect, PPDMCRITSECTINT pPrev, bool fFinal);
|
---|
[45152] | 44 | static int pdmR3CritSectRwDeleteOne(PVM pVM, PUVM pUVM, PPDMCRITSECTRWINT pCritSect, PPDMCRITSECTRWINT pPrev, bool fFinal);
|
---|
[1] | 45 |
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
[28258] | 49 | * Register statistics related to the critical sections.
|
---|
[1] | 50 | *
|
---|
| 51 | * @returns VBox status code.
|
---|
[58122] | 52 | * @param pVM The cross context VM structure.
|
---|
[1] | 53 | */
|
---|
[45152] | 54 | int pdmR3CritSectBothInitStats(PVM pVM)
|
---|
[1] | 55 | {
|
---|
[62644] | 56 | RT_NOREF_PV(pVM);
|
---|
[1] | 57 | STAM_REG(pVM, &pVM->pdm.s.StatQueuedCritSectLeaves, STAMTYPE_COUNTER, "/PDM/QueuedCritSectLeaves", STAMUNIT_OCCURENCES,
|
---|
[33595] | 58 | "Number of times a critical section leave request needed to be queued for ring-3 execution.");
|
---|
[1] | 59 | return VINF_SUCCESS;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * Relocates all the critical sections.
|
---|
| 65 | *
|
---|
[58122] | 66 | * @param pVM The cross context VM structure.
|
---|
[1] | 67 | */
|
---|
[45152] | 68 | void pdmR3CritSectBothRelocate(PVM pVM)
|
---|
[1] | 69 | {
|
---|
[28258] | 70 | PUVM pUVM = pVM->pUVM;
|
---|
| 71 | RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
|
---|
| 72 |
|
---|
| 73 | for (PPDMCRITSECTINT pCur = pUVM->pdm.s.pCritSects;
|
---|
[1] | 74 | pCur;
|
---|
| 75 | pCur = pCur->pNext)
|
---|
[12983] | 76 | pCur->pVMRC = pVM->pVMRC;
|
---|
[28258] | 77 |
|
---|
[45152] | 78 | for (PPDMCRITSECTRWINT pCur = pUVM->pdm.s.pRwCritSects;
|
---|
| 79 | pCur;
|
---|
| 80 | pCur = pCur->pNext)
|
---|
| 81 | pCur->pVMRC = pVM->pVMRC;
|
---|
| 82 |
|
---|
[28258] | 83 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
[1] | 84 | }
|
---|
| 85 |
|
---|
| 86 |
|
---|
| 87 | /**
|
---|
| 88 | * Deletes all remaining critical sections.
|
---|
| 89 | *
|
---|
[28258] | 90 | * This is called at the very end of the termination process. It is also called
|
---|
| 91 | * at the end of vmR3CreateU failure cleanup, which may cause it to be called
|
---|
[33540] | 92 | * twice depending on where vmR3CreateU actually failed. We have to do the
|
---|
[28258] | 93 | * latter call because other components expect the critical sections to be
|
---|
| 94 | * automatically deleted.
|
---|
[1] | 95 | *
|
---|
[58170] | 96 | * @returns VBox status code.
|
---|
[1] | 97 | * First error code, rest is lost.
|
---|
[58126] | 98 | * @param pVM The cross context VM structure.
|
---|
[1] | 99 | * @remark Don't confuse this with PDMR3CritSectDelete.
|
---|
| 100 | */
|
---|
[45152] | 101 | VMMR3_INT_DECL(int) PDMR3CritSectBothTerm(PVM pVM)
|
---|
[1] | 102 | {
|
---|
[28258] | 103 | PUVM pUVM = pVM->pUVM;
|
---|
| 104 | int rc = VINF_SUCCESS;
|
---|
| 105 | RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
|
---|
| 106 |
|
---|
| 107 | while (pUVM->pdm.s.pCritSects)
|
---|
[1] | 108 | {
|
---|
[28258] | 109 | int rc2 = pdmR3CritSectDeleteOne(pVM, pUVM, pUVM->pdm.s.pCritSects, NULL, true /* final */);
|
---|
[1] | 110 | AssertRC(rc2);
|
---|
[13816] | 111 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
[1] | 112 | rc = rc2;
|
---|
| 113 | }
|
---|
[28258] | 114 |
|
---|
[45152] | 115 | while (pUVM->pdm.s.pRwCritSects)
|
---|
| 116 | {
|
---|
| 117 | int rc2 = pdmR3CritSectRwDeleteOne(pVM, pUVM, pUVM->pdm.s.pRwCritSects, NULL, true /* final */);
|
---|
| 118 | AssertRC(rc2);
|
---|
| 119 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
| 120 | rc = rc2;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[28258] | 123 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
[1] | 124 | return rc;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 |
|
---|
| 128 | /**
|
---|
[33540] | 129 | * Initializes a critical section and inserts it into the list.
|
---|
[1] | 130 | *
|
---|
| 131 | * @returns VBox status code.
|
---|
[58122] | 132 | * @param pVM The cross context VM structure.
|
---|
[1] | 133 | * @param pCritSect The critical section.
|
---|
| 134 | * @param pvKey The owner key.
|
---|
[58116] | 135 | * @param SRC_POS The source position.
|
---|
[33540] | 136 | * @param pszNameFmt Format string for naming the critical section. For
|
---|
[25732] | 137 | * statistics and lock validation.
|
---|
| 138 | * @param va Arguments for the format string.
|
---|
[1] | 139 | */
|
---|
[37419] | 140 | static int pdmR3CritSectInitOne(PVM pVM, PPDMCRITSECTINT pCritSect, void *pvKey, RT_SRC_POS_DECL,
|
---|
| 141 | const char *pszNameFmt, va_list va)
|
---|
[1] | 142 | {
|
---|
| 143 | VM_ASSERT_EMT(pVM);
|
---|
[45152] | 144 | Assert(pCritSect->Core.u32Magic != RTCRITSECT_MAGIC);
|
---|
[19991] | 145 |
|
---|
| 146 | /*
|
---|
| 147 | * Allocate the semaphore.
|
---|
| 148 | */
|
---|
| 149 | AssertCompile(sizeof(SUPSEMEVENT) == sizeof(pCritSect->Core.EventSem));
|
---|
| 150 | int rc = SUPSemEventCreate(pVM->pSession, (PSUPSEMEVENT)&pCritSect->Core.EventSem);
|
---|
[13816] | 151 | if (RT_SUCCESS(rc))
|
---|
[1] | 152 | {
|
---|
[25732] | 153 | /* Only format the name once. */
|
---|
| 154 | char *pszName = RTStrAPrintf2V(pszNameFmt, va); /** @todo plug the "leak"... */
|
---|
| 155 | if (pszName)
|
---|
| 156 | {
|
---|
[62644] | 157 | RT_SRC_POS_NOREF();
|
---|
[25718] | 158 | #ifndef PDMCRITSECT_STRICT
|
---|
[25732] | 159 | pCritSect->Core.pValidatorRec = NULL;
|
---|
[25718] | 160 | #else
|
---|
[25732] | 161 | rc = RTLockValidatorRecExclCreate(&pCritSect->Core.pValidatorRec,
|
---|
[25748] | 162 | # ifdef RT_LOCK_STRICT_ORDER
|
---|
[25732] | 163 | RTLockValidatorClassForSrcPos(RT_SRC_POS_ARGS, "%s", pszName),
|
---|
[25748] | 164 | # else
|
---|
| 165 | NIL_RTLOCKVALCLASS,
|
---|
| 166 | # endif
|
---|
[25732] | 167 | RTLOCKVAL_SUB_CLASS_NONE,
|
---|
| 168 | pCritSect, true, "%s", pszName);
|
---|
[25718] | 169 | #endif
|
---|
[25732] | 170 | if (RT_SUCCESS(rc))
|
---|
| 171 | {
|
---|
| 172 | /*
|
---|
| 173 | * Initialize the structure (first bit is c&p from RTCritSectInitEx).
|
---|
| 174 | */
|
---|
| 175 | pCritSect->Core.u32Magic = RTCRITSECT_MAGIC;
|
---|
| 176 | pCritSect->Core.fFlags = 0;
|
---|
| 177 | pCritSect->Core.cNestings = 0;
|
---|
| 178 | pCritSect->Core.cLockers = -1;
|
---|
| 179 | pCritSect->Core.NativeThreadOwner = NIL_RTNATIVETHREAD;
|
---|
| 180 | pCritSect->pVMR3 = pVM;
|
---|
[80281] | 181 | pCritSect->pVMR0 = pVM->pVMR0ForCall;
|
---|
[25732] | 182 | pCritSect->pVMRC = pVM->pVMRC;
|
---|
| 183 | pCritSect->pvKey = pvKey;
|
---|
[37466] | 184 | pCritSect->fAutomaticDefaultCritsect = false;
|
---|
| 185 | pCritSect->fUsedByTimerOrSimilar = false;
|
---|
[56402] | 186 | pCritSect->hEventToSignal = NIL_SUPSEMEVENT;
|
---|
[25732] | 187 | pCritSect->pszName = pszName;
|
---|
[45152] | 188 |
|
---|
[25732] | 189 | STAMR3RegisterF(pVM, &pCritSect->StatContentionRZLock, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSects/%s/ContentionRZLock", pCritSect->pszName);
|
---|
| 190 | STAMR3RegisterF(pVM, &pCritSect->StatContentionRZUnlock,STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSects/%s/ContentionRZUnlock", pCritSect->pszName);
|
---|
| 191 | STAMR3RegisterF(pVM, &pCritSect->StatContentionR3, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSects/%s/ContentionR3", pCritSect->pszName);
|
---|
[18532] | 192 | #ifdef VBOX_WITH_STATISTICS
|
---|
[25732] | 193 | STAMR3RegisterF(pVM, &pCritSect->StatLocked, STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_OCCURENCE, NULL, "/PDM/CritSects/%s/Locked", pCritSect->pszName);
|
---|
[1] | 194 | #endif
|
---|
[45152] | 195 |
|
---|
| 196 | PUVM pUVM = pVM->pUVM;
|
---|
| 197 | RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
|
---|
| 198 | pCritSect->pNext = pUVM->pdm.s.pCritSects;
|
---|
| 199 | pUVM->pdm.s.pCritSects = pCritSect;
|
---|
| 200 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
| 201 |
|
---|
[25732] | 202 | return VINF_SUCCESS;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | RTStrFree(pszName);
|
---|
[25368] | 206 | }
|
---|
[25732] | 207 | else
|
---|
| 208 | rc = VERR_NO_STR_MEMORY;
|
---|
[25368] | 209 | SUPSemEventClose(pVM->pSession, (SUPSEMEVENT)pCritSect->Core.EventSem);
|
---|
[1] | 210 | }
|
---|
| 211 | return rc;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 |
|
---|
| 215 | /**
|
---|
[45152] | 216 | * Initializes a read/write critical section and inserts it into the list.
|
---|
| 217 | *
|
---|
| 218 | * @returns VBox status code.
|
---|
[58122] | 219 | * @param pVM The cross context VM structure.
|
---|
[45152] | 220 | * @param pCritSect The read/write critical section.
|
---|
| 221 | * @param pvKey The owner key.
|
---|
[58116] | 222 | * @param SRC_POS The source position.
|
---|
[45152] | 223 | * @param pszNameFmt Format string for naming the critical section. For
|
---|
| 224 | * statistics and lock validation.
|
---|
| 225 | * @param va Arguments for the format string.
|
---|
| 226 | */
|
---|
| 227 | static int pdmR3CritSectRwInitOne(PVM pVM, PPDMCRITSECTRWINT pCritSect, void *pvKey, RT_SRC_POS_DECL,
|
---|
| 228 | const char *pszNameFmt, va_list va)
|
---|
| 229 | {
|
---|
| 230 | VM_ASSERT_EMT(pVM);
|
---|
| 231 | Assert(pCritSect->Core.u32Magic != RTCRITSECTRW_MAGIC);
|
---|
| 232 |
|
---|
| 233 | /*
|
---|
| 234 | * Allocate the semaphores.
|
---|
| 235 | */
|
---|
| 236 | AssertCompile(sizeof(SUPSEMEVENT) == sizeof(pCritSect->Core.hEvtWrite));
|
---|
| 237 | int rc = SUPSemEventCreate(pVM->pSession, (PSUPSEMEVENT)&pCritSect->Core.hEvtWrite);
|
---|
| 238 | if (RT_SUCCESS(rc))
|
---|
| 239 | {
|
---|
| 240 | AssertCompile(sizeof(SUPSEMEVENTMULTI) == sizeof(pCritSect->Core.hEvtRead));
|
---|
| 241 | rc = SUPSemEventMultiCreate(pVM->pSession, (PSUPSEMEVENT)&pCritSect->Core.hEvtRead);
|
---|
| 242 | if (RT_SUCCESS(rc))
|
---|
| 243 | {
|
---|
| 244 | /* Only format the name once. */
|
---|
| 245 | char *pszName = RTStrAPrintf2V(pszNameFmt, va); /** @todo plug the "leak"... */
|
---|
| 246 | if (pszName)
|
---|
| 247 | {
|
---|
| 248 | pCritSect->Core.pValidatorRead = NULL;
|
---|
| 249 | pCritSect->Core.pValidatorWrite = NULL;
|
---|
[62644] | 250 | RT_SRC_POS_NOREF();
|
---|
[45177] | 251 | #ifdef PDMCRITSECTRW_STRICT
|
---|
[45152] | 252 | # ifdef RT_LOCK_STRICT_ORDER
|
---|
| 253 | RTLOCKVALCLASS hClass = RTLockValidatorClassForSrcPos(RT_SRC_POS_ARGS, "%s", pszName);
|
---|
| 254 | # else
|
---|
| 255 | RTLOCKVALCLASS hClass = NIL_RTLOCKVALCLASS;
|
---|
| 256 | # endif
|
---|
| 257 | rc = RTLockValidatorRecExclCreate(&pCritSect->Core.pValidatorWrite, hClass, RTLOCKVAL_SUB_CLASS_NONE,
|
---|
| 258 | pCritSect, true, "%s", pszName);
|
---|
| 259 | if (RT_SUCCESS(rc))
|
---|
| 260 | rc = RTLockValidatorRecSharedCreate(&pCritSect->Core.pValidatorRead, hClass, RTLOCKVAL_SUB_CLASS_NONE,
|
---|
| 261 | pCritSect, false /*fSignaller*/, true, "%s", pszName);
|
---|
| 262 | #endif
|
---|
| 263 | if (RT_SUCCESS(rc))
|
---|
| 264 | {
|
---|
| 265 | /*
|
---|
| 266 | * Initialize the structure (first bit is c&p from RTCritSectRwInitEx).
|
---|
| 267 | */
|
---|
[45177] | 268 | pCritSect->Core.u32Magic = RTCRITSECTRW_MAGIC;
|
---|
[45152] | 269 | pCritSect->Core.fNeedReset = false;
|
---|
| 270 | pCritSect->Core.u64State = 0;
|
---|
| 271 | pCritSect->Core.hNativeWriter = NIL_RTNATIVETHREAD;
|
---|
| 272 | pCritSect->Core.cWriterReads = 0;
|
---|
| 273 | pCritSect->Core.cWriteRecursions = 0;
|
---|
| 274 | #if HC_ARCH_BITS == 32
|
---|
| 275 | pCritSect->Core.HCPtrPadding = NIL_RTHCPTR;
|
---|
| 276 | #endif
|
---|
| 277 | pCritSect->pVMR3 = pVM;
|
---|
[80281] | 278 | pCritSect->pVMR0 = pVM->pVMR0ForCall;
|
---|
[45152] | 279 | pCritSect->pVMRC = pVM->pVMRC;
|
---|
| 280 | pCritSect->pvKey = pvKey;
|
---|
| 281 | pCritSect->pszName = pszName;
|
---|
| 282 |
|
---|
| 283 | STAMR3RegisterF(pVM, &pCritSect->StatContentionRZEnterExcl, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSectsRw/%s/ContentionRZEnterExcl", pCritSect->pszName);
|
---|
| 284 | STAMR3RegisterF(pVM, &pCritSect->StatContentionRZLeaveExcl, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSectsRw/%s/ContentionRZLeaveExcl", pCritSect->pszName);
|
---|
| 285 | STAMR3RegisterF(pVM, &pCritSect->StatContentionRZEnterShared, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSectsRw/%s/ContentionRZEnterShared", pCritSect->pszName);
|
---|
| 286 | STAMR3RegisterF(pVM, &pCritSect->StatContentionRZLeaveShared, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSectsRw/%s/ContentionRZLeaveShared", pCritSect->pszName);
|
---|
| 287 | STAMR3RegisterF(pVM, &pCritSect->StatContentionR3EnterExcl, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSectsRw/%s/ContentionR3EnterExcl", pCritSect->pszName);
|
---|
| 288 | STAMR3RegisterF(pVM, &pCritSect->StatContentionR3EnterShared, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSectsRw/%s/ContentionR3EnterShared", pCritSect->pszName);
|
---|
| 289 | STAMR3RegisterF(pVM, &pCritSect->StatRZEnterExcl, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSectsRw/%s/RZEnterExcl", pCritSect->pszName);
|
---|
| 290 | STAMR3RegisterF(pVM, &pCritSect->StatRZEnterShared, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSectsRw/%s/RZEnterShared", pCritSect->pszName);
|
---|
| 291 | STAMR3RegisterF(pVM, &pCritSect->StatR3EnterExcl, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSectsRw/%s/R3EnterExcl", pCritSect->pszName);
|
---|
| 292 | STAMR3RegisterF(pVM, &pCritSect->StatR3EnterShared, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSectsRw/%s/R3EnterShared", pCritSect->pszName);
|
---|
| 293 | #ifdef VBOX_WITH_STATISTICS
|
---|
| 294 | STAMR3RegisterF(pVM, &pCritSect->StatWriteLocked, STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_OCCURENCE, NULL, "/PDM/CritSectsRw/%s/WriteLocked", pCritSect->pszName);
|
---|
| 295 | #endif
|
---|
| 296 |
|
---|
| 297 | PUVM pUVM = pVM->pUVM;
|
---|
| 298 | RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
|
---|
| 299 | pCritSect->pNext = pUVM->pdm.s.pRwCritSects;
|
---|
| 300 | pUVM->pdm.s.pRwCritSects = pCritSect;
|
---|
| 301 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
| 302 |
|
---|
| 303 | return VINF_SUCCESS;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | RTStrFree(pszName);
|
---|
| 307 | }
|
---|
| 308 | else
|
---|
| 309 | rc = VERR_NO_STR_MEMORY;
|
---|
| 310 | SUPSemEventMultiClose(pVM->pSession, (SUPSEMEVENT)pCritSect->Core.hEvtRead);
|
---|
| 311 | }
|
---|
| 312 | SUPSemEventClose(pVM->pSession, (SUPSEMEVENT)pCritSect->Core.hEvtWrite);
|
---|
| 313 | }
|
---|
| 314 | return rc;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 |
|
---|
| 318 | /**
|
---|
[1] | 319 | * Initializes a PDM critical section for internal use.
|
---|
| 320 | *
|
---|
| 321 | * The PDM critical sections are derived from the IPRT critical sections, but
|
---|
[45152] | 322 | * works in ring-0 and raw-mode context as well.
|
---|
[1] | 323 | *
|
---|
| 324 | * @returns VBox status code.
|
---|
[58122] | 325 | * @param pVM The cross context VM structure.
|
---|
[1] | 326 | * @param pCritSect Pointer to the critical section.
|
---|
[58116] | 327 | * @param SRC_POS Use RT_SRC_POS.
|
---|
[33540] | 328 | * @param pszNameFmt Format string for naming the critical section. For
|
---|
[25732] | 329 | * statistics and lock validation.
|
---|
| 330 | * @param ... Arguments for the format string.
|
---|
[45152] | 331 | * @thread EMT
|
---|
[1] | 332 | */
|
---|
[25732] | 333 | VMMR3DECL(int) PDMR3CritSectInit(PVM pVM, PPDMCRITSECT pCritSect, RT_SRC_POS_DECL, const char *pszNameFmt, ...)
|
---|
[1] | 334 | {
|
---|
[9154] | 335 | #if HC_ARCH_BITS == 64 && GC_ARCH_BITS == 32
|
---|
[1] | 336 | AssertCompile(sizeof(pCritSect->padding) >= sizeof(pCritSect->s));
|
---|
[9154] | 337 | #endif
|
---|
[19735] | 338 | Assert(RT_ALIGN_P(pCritSect, sizeof(uintptr_t)) == pCritSect);
|
---|
[25732] | 339 | va_list va;
|
---|
| 340 | va_start(va, pszNameFmt);
|
---|
| 341 | int rc = pdmR3CritSectInitOne(pVM, &pCritSect->s, pCritSect, RT_SRC_POS_ARGS, pszNameFmt, va);
|
---|
| 342 | va_end(va);
|
---|
| 343 | return rc;
|
---|
[1] | 344 | }
|
---|
| 345 |
|
---|
| 346 |
|
---|
| 347 | /**
|
---|
[45152] | 348 | * Initializes a PDM read/write critical section for internal use.
|
---|
[1] | 349 | *
|
---|
[45152] | 350 | * The PDM read/write critical sections are derived from the IPRT read/write
|
---|
| 351 | * critical sections, but works in ring-0 and raw-mode context as well.
|
---|
[1] | 352 | *
|
---|
| 353 | * @returns VBox status code.
|
---|
[58122] | 354 | * @param pVM The cross context VM structure.
|
---|
[45152] | 355 | * @param pCritSect Pointer to the read/write critical section.
|
---|
[58116] | 356 | * @param SRC_POS Use RT_SRC_POS.
|
---|
[45152] | 357 | * @param pszNameFmt Format string for naming the critical section. For
|
---|
| 358 | * statistics and lock validation.
|
---|
| 359 | * @param ... Arguments for the format string.
|
---|
| 360 | * @thread EMT
|
---|
| 361 | */
|
---|
| 362 | VMMR3DECL(int) PDMR3CritSectRwInit(PVM pVM, PPDMCRITSECTRW pCritSect, RT_SRC_POS_DECL, const char *pszNameFmt, ...)
|
---|
| 363 | {
|
---|
| 364 | #if HC_ARCH_BITS == 64 && GC_ARCH_BITS == 32
|
---|
| 365 | AssertCompile(sizeof(pCritSect->padding) >= sizeof(pCritSect->s));
|
---|
| 366 | #endif
|
---|
| 367 | Assert(RT_ALIGN_P(pCritSect, sizeof(uintptr_t)) == pCritSect);
|
---|
| 368 | va_list va;
|
---|
| 369 | va_start(va, pszNameFmt);
|
---|
| 370 | int rc = pdmR3CritSectRwInitOne(pVM, &pCritSect->s, pCritSect, RT_SRC_POS_ARGS, pszNameFmt, va);
|
---|
| 371 | va_end(va);
|
---|
| 372 | return rc;
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 |
|
---|
| 376 | /**
|
---|
| 377 | * Initializes a PDM critical section for a device.
|
---|
| 378 | *
|
---|
| 379 | * @returns VBox status code.
|
---|
[58122] | 380 | * @param pVM The cross context VM structure.
|
---|
[45152] | 381 | * @param pDevIns Device instance.
|
---|
[1] | 382 | * @param pCritSect Pointer to the critical section.
|
---|
[58116] | 383 | * @param SRC_POS The source position. Optional.
|
---|
[28258] | 384 | * @param pszNameFmt Format string for naming the critical section. For
|
---|
[25732] | 385 | * statistics and lock validation.
|
---|
| 386 | * @param va Arguments for the format string.
|
---|
[1] | 387 | */
|
---|
[25732] | 388 | int pdmR3CritSectInitDevice(PVM pVM, PPDMDEVINS pDevIns, PPDMCRITSECT pCritSect, RT_SRC_POS_DECL,
|
---|
| 389 | const char *pszNameFmt, va_list va)
|
---|
[1] | 390 | {
|
---|
[25732] | 391 | return pdmR3CritSectInitOne(pVM, &pCritSect->s, pDevIns, RT_SRC_POS_ARGS, pszNameFmt, va);
|
---|
[1] | 392 | }
|
---|
| 393 |
|
---|
| 394 |
|
---|
| 395 | /**
|
---|
[45152] | 396 | * Initializes a PDM read/write critical section for a device.
|
---|
| 397 | *
|
---|
| 398 | * @returns VBox status code.
|
---|
[58122] | 399 | * @param pVM The cross context VM structure.
|
---|
[45152] | 400 | * @param pDevIns Device instance.
|
---|
| 401 | * @param pCritSect Pointer to the read/write critical section.
|
---|
[58116] | 402 | * @param SRC_POS The source position. Optional.
|
---|
[45152] | 403 | * @param pszNameFmt Format string for naming the critical section. For
|
---|
| 404 | * statistics and lock validation.
|
---|
| 405 | * @param va Arguments for the format string.
|
---|
| 406 | */
|
---|
| 407 | int pdmR3CritSectRwInitDevice(PVM pVM, PPDMDEVINS pDevIns, PPDMCRITSECTRW pCritSect, RT_SRC_POS_DECL,
|
---|
| 408 | const char *pszNameFmt, va_list va)
|
---|
| 409 | {
|
---|
| 410 | return pdmR3CritSectRwInitOne(pVM, &pCritSect->s, pDevIns, RT_SRC_POS_ARGS, pszNameFmt, va);
|
---|
| 411 | }
|
---|
| 412 |
|
---|
| 413 |
|
---|
| 414 | /**
|
---|
[37466] | 415 | * Initializes the automatic default PDM critical section for a device.
|
---|
| 416 | *
|
---|
| 417 | * @returns VBox status code.
|
---|
[58122] | 418 | * @param pVM The cross context VM structure.
|
---|
[37466] | 419 | * @param pDevIns Device instance.
|
---|
[58116] | 420 | * @param SRC_POS The source position. Optional.
|
---|
[37466] | 421 | * @param pCritSect Pointer to the critical section.
|
---|
[58126] | 422 | * @param pszNameFmt Format string for naming the critical section. For
|
---|
| 423 | * statistics and lock validation.
|
---|
| 424 | * @param ... Arguments for the format string.
|
---|
[37466] | 425 | */
|
---|
| 426 | int pdmR3CritSectInitDeviceAuto(PVM pVM, PPDMDEVINS pDevIns, PPDMCRITSECT pCritSect, RT_SRC_POS_DECL,
|
---|
| 427 | const char *pszNameFmt, ...)
|
---|
| 428 | {
|
---|
| 429 | va_list va;
|
---|
| 430 | va_start(va, pszNameFmt);
|
---|
| 431 | int rc = pdmR3CritSectInitOne(pVM, &pCritSect->s, pDevIns, RT_SRC_POS_ARGS, pszNameFmt, va);
|
---|
| 432 | if (RT_SUCCESS(rc))
|
---|
| 433 | pCritSect->s.fAutomaticDefaultCritsect = true;
|
---|
| 434 | va_end(va);
|
---|
| 435 | return rc;
|
---|
| 436 | }
|
---|
| 437 |
|
---|
| 438 |
|
---|
| 439 | /**
|
---|
[28258] | 440 | * Initializes a PDM critical section for a driver.
|
---|
| 441 | *
|
---|
| 442 | * @returns VBox status code.
|
---|
[58122] | 443 | * @param pVM The cross context VM structure.
|
---|
[28258] | 444 | * @param pDrvIns Driver instance.
|
---|
| 445 | * @param pCritSect Pointer to the critical section.
|
---|
[58116] | 446 | * @param SRC_POS The source position. Optional.
|
---|
[28258] | 447 | * @param pszNameFmt Format string for naming the critical section. For
|
---|
| 448 | * statistics and lock validation.
|
---|
| 449 | * @param ... Arguments for the format string.
|
---|
| 450 | */
|
---|
| 451 | int pdmR3CritSectInitDriver(PVM pVM, PPDMDRVINS pDrvIns, PPDMCRITSECT pCritSect, RT_SRC_POS_DECL,
|
---|
| 452 | const char *pszNameFmt, ...)
|
---|
| 453 | {
|
---|
| 454 | va_list va;
|
---|
| 455 | va_start(va, pszNameFmt);
|
---|
| 456 | int rc = pdmR3CritSectInitOne(pVM, &pCritSect->s, pDrvIns, RT_SRC_POS_ARGS, pszNameFmt, va);
|
---|
| 457 | va_end(va);
|
---|
| 458 | return rc;
|
---|
| 459 | }
|
---|
| 460 |
|
---|
| 461 |
|
---|
| 462 | /**
|
---|
[45152] | 463 | * Initializes a PDM read/write critical section for a driver.
|
---|
| 464 | *
|
---|
| 465 | * @returns VBox status code.
|
---|
[58122] | 466 | * @param pVM The cross context VM structure.
|
---|
[45152] | 467 | * @param pDrvIns Driver instance.
|
---|
| 468 | * @param pCritSect Pointer to the read/write critical section.
|
---|
[58116] | 469 | * @param SRC_POS The source position. Optional.
|
---|
[45152] | 470 | * @param pszNameFmt Format string for naming the critical section. For
|
---|
| 471 | * statistics and lock validation.
|
---|
| 472 | * @param ... Arguments for the format string.
|
---|
| 473 | */
|
---|
| 474 | int pdmR3CritSectRwInitDriver(PVM pVM, PPDMDRVINS pDrvIns, PPDMCRITSECTRW pCritSect, RT_SRC_POS_DECL,
|
---|
| 475 | const char *pszNameFmt, ...)
|
---|
| 476 | {
|
---|
| 477 | va_list va;
|
---|
| 478 | va_start(va, pszNameFmt);
|
---|
| 479 | int rc = pdmR3CritSectRwInitOne(pVM, &pCritSect->s, pDrvIns, RT_SRC_POS_ARGS, pszNameFmt, va);
|
---|
| 480 | va_end(va);
|
---|
| 481 | return rc;
|
---|
| 482 | }
|
---|
| 483 |
|
---|
| 484 |
|
---|
| 485 | /**
|
---|
[1] | 486 | * Deletes one critical section.
|
---|
| 487 | *
|
---|
| 488 | * @returns Return code from RTCritSectDelete.
|
---|
[20008] | 489 | *
|
---|
[58122] | 490 | * @param pVM The cross context VM structure.
|
---|
[58126] | 491 | * @param pUVM The user mode VM handle.
|
---|
[1] | 492 | * @param pCritSect The critical section.
|
---|
| 493 | * @param pPrev The previous critical section in the list.
|
---|
| 494 | * @param fFinal Set if this is the final call and statistics shouldn't be deregistered.
|
---|
[20008] | 495 | *
|
---|
[33540] | 496 | * @remarks Caller must have entered the ListCritSect.
|
---|
[1] | 497 | */
|
---|
[28258] | 498 | static int pdmR3CritSectDeleteOne(PVM pVM, PUVM pUVM, PPDMCRITSECTINT pCritSect, PPDMCRITSECTINT pPrev, bool fFinal)
|
---|
[1] | 499 | {
|
---|
[19991] | 500 | /*
|
---|
| 501 | * Assert free waiters and so on (c&p from RTCritSectDelete).
|
---|
| 502 | */
|
---|
| 503 | Assert(pCritSect->Core.u32Magic == RTCRITSECT_MAGIC);
|
---|
[62148] | 504 | //Assert(pCritSect->Core.cNestings == 0); - we no longer reset this when leaving.
|
---|
[19991] | 505 | Assert(pCritSect->Core.cLockers == -1);
|
---|
| 506 | Assert(pCritSect->Core.NativeThreadOwner == NIL_RTNATIVETHREAD);
|
---|
[28258] | 507 | Assert(RTCritSectIsOwner(&pUVM->pdm.s.ListCritSect));
|
---|
[19991] | 508 |
|
---|
| 509 | /*
|
---|
| 510 | * Unlink it.
|
---|
| 511 | */
|
---|
[1] | 512 | if (pPrev)
|
---|
| 513 | pPrev->pNext = pCritSect->pNext;
|
---|
| 514 | else
|
---|
[28258] | 515 | pUVM->pdm.s.pCritSects = pCritSect->pNext;
|
---|
[1] | 516 |
|
---|
[19991] | 517 | /*
|
---|
| 518 | * Delete it (parts taken from RTCritSectDelete).
|
---|
| 519 | * In case someone is waiting we'll signal the semaphore cLockers + 1 times.
|
---|
| 520 | */
|
---|
| 521 | ASMAtomicWriteU32(&pCritSect->Core.u32Magic, 0);
|
---|
| 522 | SUPSEMEVENT hEvent = (SUPSEMEVENT)pCritSect->Core.EventSem;
|
---|
| 523 | pCritSect->Core.EventSem = NIL_RTSEMEVENT;
|
---|
| 524 | while (pCritSect->Core.cLockers-- >= 0)
|
---|
| 525 | SUPSemEventSignal(pVM->pSession, hEvent);
|
---|
| 526 | ASMAtomicWriteS32(&pCritSect->Core.cLockers, -1);
|
---|
| 527 | int rc = SUPSemEventClose(pVM->pSession, hEvent);
|
---|
| 528 | AssertRC(rc);
|
---|
[25607] | 529 | RTLockValidatorRecExclDestroy(&pCritSect->Core.pValidatorRec);
|
---|
[18532] | 530 | pCritSect->pNext = NULL;
|
---|
| 531 | pCritSect->pvKey = NULL;
|
---|
| 532 | pCritSect->pVMR3 = NULL;
|
---|
| 533 | pCritSect->pVMR0 = NIL_RTR0PTR;
|
---|
| 534 | pCritSect->pVMRC = NIL_RTRCPTR;
|
---|
[46493] | 535 | if (!fFinal)
|
---|
| 536 | STAMR3DeregisterF(pVM->pUVM, "/PDM/CritSects/%s/*", pCritSect->pszName);
|
---|
[18532] | 537 | RTStrFree((char *)pCritSect->pszName);
|
---|
| 538 | pCritSect->pszName = NULL;
|
---|
[19991] | 539 | return rc;
|
---|
[1] | 540 | }
|
---|
| 541 |
|
---|
| 542 |
|
---|
| 543 | /**
|
---|
[45152] | 544 | * Deletes one read/write critical section.
|
---|
| 545 | *
|
---|
| 546 | * @returns VBox status code.
|
---|
| 547 | *
|
---|
[58122] | 548 | * @param pVM The cross context VM structure.
|
---|
[58126] | 549 | * @param pUVM The user mode VM handle.
|
---|
[45152] | 550 | * @param pCritSect The read/write critical section.
|
---|
| 551 | * @param pPrev The previous critical section in the list.
|
---|
| 552 | * @param fFinal Set if this is the final call and statistics shouldn't be deregistered.
|
---|
| 553 | *
|
---|
| 554 | * @remarks Caller must have entered the ListCritSect.
|
---|
| 555 | */
|
---|
| 556 | static int pdmR3CritSectRwDeleteOne(PVM pVM, PUVM pUVM, PPDMCRITSECTRWINT pCritSect, PPDMCRITSECTRWINT pPrev, bool fFinal)
|
---|
| 557 | {
|
---|
| 558 | /*
|
---|
| 559 | * Assert free waiters and so on (c&p from RTCritSectRwDelete).
|
---|
| 560 | */
|
---|
| 561 | Assert(pCritSect->Core.u32Magic == RTCRITSECTRW_MAGIC);
|
---|
| 562 | //Assert(pCritSect->Core.cNestings == 0);
|
---|
| 563 | //Assert(pCritSect->Core.cLockers == -1);
|
---|
| 564 | Assert(pCritSect->Core.hNativeWriter == NIL_RTNATIVETHREAD);
|
---|
| 565 |
|
---|
| 566 | /*
|
---|
| 567 | * Invalidate the structure and free the semaphores.
|
---|
| 568 | */
|
---|
| 569 | if (!ASMAtomicCmpXchgU32(&pCritSect->Core.u32Magic, RTCRITSECTRW_MAGIC_DEAD, RTCRITSECTRW_MAGIC))
|
---|
| 570 | AssertFailed();
|
---|
| 571 |
|
---|
| 572 | /*
|
---|
| 573 | * Unlink it.
|
---|
| 574 | */
|
---|
| 575 | if (pPrev)
|
---|
| 576 | pPrev->pNext = pCritSect->pNext;
|
---|
| 577 | else
|
---|
| 578 | pUVM->pdm.s.pRwCritSects = pCritSect->pNext;
|
---|
| 579 |
|
---|
| 580 | /*
|
---|
| 581 | * Delete it (parts taken from RTCritSectRwDelete).
|
---|
| 582 | * In case someone is waiting we'll signal the semaphore cLockers + 1 times.
|
---|
| 583 | */
|
---|
| 584 | pCritSect->Core.fFlags = 0;
|
---|
| 585 | pCritSect->Core.u64State = 0;
|
---|
| 586 |
|
---|
| 587 | SUPSEMEVENT hEvtWrite = (SUPSEMEVENT)pCritSect->Core.hEvtWrite;
|
---|
| 588 | pCritSect->Core.hEvtWrite = NIL_RTSEMEVENT;
|
---|
| 589 | AssertCompile(sizeof(hEvtWrite) == sizeof(pCritSect->Core.hEvtWrite));
|
---|
| 590 |
|
---|
| 591 | SUPSEMEVENTMULTI hEvtRead = (SUPSEMEVENTMULTI)pCritSect->Core.hEvtRead;
|
---|
| 592 | pCritSect->Core.hEvtRead = NIL_RTSEMEVENTMULTI;
|
---|
| 593 | AssertCompile(sizeof(hEvtRead) == sizeof(pCritSect->Core.hEvtRead));
|
---|
| 594 |
|
---|
| 595 | int rc1 = SUPSemEventClose(pVM->pSession, hEvtWrite); AssertRC(rc1);
|
---|
| 596 | int rc2 = SUPSemEventMultiClose(pVM->pSession, hEvtRead); AssertRC(rc2);
|
---|
| 597 |
|
---|
| 598 | RTLockValidatorRecSharedDestroy(&pCritSect->Core.pValidatorRead);
|
---|
| 599 | RTLockValidatorRecExclDestroy(&pCritSect->Core.pValidatorWrite);
|
---|
| 600 |
|
---|
| 601 | pCritSect->pNext = NULL;
|
---|
| 602 | pCritSect->pvKey = NULL;
|
---|
| 603 | pCritSect->pVMR3 = NULL;
|
---|
| 604 | pCritSect->pVMR0 = NIL_RTR0PTR;
|
---|
| 605 | pCritSect->pVMRC = NIL_RTRCPTR;
|
---|
[46493] | 606 | if (!fFinal)
|
---|
| 607 | STAMR3DeregisterF(pVM->pUVM, "/PDM/CritSectsRw/%s/*", pCritSect->pszName);
|
---|
[45152] | 608 | RTStrFree((char *)pCritSect->pszName);
|
---|
| 609 | pCritSect->pszName = NULL;
|
---|
| 610 |
|
---|
| 611 | return RT_SUCCESS(rc1) ? rc2 : rc1;
|
---|
| 612 | }
|
---|
| 613 |
|
---|
| 614 |
|
---|
| 615 | /**
|
---|
[1] | 616 | * Deletes all critical sections with a give initializer key.
|
---|
| 617 | *
|
---|
| 618 | * @returns VBox status code.
|
---|
| 619 | * The entire list is processed on failure, so we'll only
|
---|
| 620 | * return the first error code. This shouldn't be a problem
|
---|
| 621 | * since errors really shouldn't happen here.
|
---|
[58122] | 622 | * @param pVM The cross context VM structure.
|
---|
[1] | 623 | * @param pvKey The initializer key.
|
---|
| 624 | */
|
---|
| 625 | static int pdmR3CritSectDeleteByKey(PVM pVM, void *pvKey)
|
---|
| 626 | {
|
---|
| 627 | /*
|
---|
| 628 | * Iterate the list and match key.
|
---|
| 629 | */
|
---|
[28258] | 630 | PUVM pUVM = pVM->pUVM;
|
---|
[20008] | 631 | int rc = VINF_SUCCESS;
|
---|
[1] | 632 | PPDMCRITSECTINT pPrev = NULL;
|
---|
[28258] | 633 | RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
|
---|
| 634 | PPDMCRITSECTINT pCur = pUVM->pdm.s.pCritSects;
|
---|
[1] | 635 | while (pCur)
|
---|
| 636 | {
|
---|
| 637 | if (pCur->pvKey == pvKey)
|
---|
| 638 | {
|
---|
[28258] | 639 | int rc2 = pdmR3CritSectDeleteOne(pVM, pUVM, pCur, pPrev, false /* not final */);
|
---|
[1] | 640 | AssertRC(rc2);
|
---|
[13816] | 641 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
[1] | 642 | rc = rc2;
|
---|
| 643 | }
|
---|
| 644 |
|
---|
| 645 | /* next */
|
---|
| 646 | pPrev = pCur;
|
---|
| 647 | pCur = pCur->pNext;
|
---|
| 648 | }
|
---|
[28258] | 649 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
[1] | 650 | return rc;
|
---|
| 651 | }
|
---|
| 652 |
|
---|
| 653 |
|
---|
| 654 | /**
|
---|
[45152] | 655 | * Deletes all read/write critical sections with a give initializer key.
|
---|
[1] | 656 | *
|
---|
| 657 | * @returns VBox status code.
|
---|
[45152] | 658 | * The entire list is processed on failure, so we'll only
|
---|
| 659 | * return the first error code. This shouldn't be a problem
|
---|
| 660 | * since errors really shouldn't happen here.
|
---|
[58122] | 661 | * @param pVM The cross context VM structure.
|
---|
[45152] | 662 | * @param pvKey The initializer key.
|
---|
| 663 | */
|
---|
| 664 | static int pdmR3CritSectRwDeleteByKey(PVM pVM, void *pvKey)
|
---|
| 665 | {
|
---|
| 666 | /*
|
---|
| 667 | * Iterate the list and match key.
|
---|
| 668 | */
|
---|
| 669 | PUVM pUVM = pVM->pUVM;
|
---|
| 670 | int rc = VINF_SUCCESS;
|
---|
| 671 | PPDMCRITSECTRWINT pPrev = NULL;
|
---|
| 672 | RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
|
---|
| 673 | PPDMCRITSECTRWINT pCur = pUVM->pdm.s.pRwCritSects;
|
---|
| 674 | while (pCur)
|
---|
| 675 | {
|
---|
| 676 | if (pCur->pvKey == pvKey)
|
---|
| 677 | {
|
---|
| 678 | int rc2 = pdmR3CritSectRwDeleteOne(pVM, pUVM, pCur, pPrev, false /* not final */);
|
---|
| 679 | AssertRC(rc2);
|
---|
| 680 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
| 681 | rc = rc2;
|
---|
| 682 | }
|
---|
| 683 |
|
---|
| 684 | /* next */
|
---|
| 685 | pPrev = pCur;
|
---|
| 686 | pCur = pCur->pNext;
|
---|
| 687 | }
|
---|
| 688 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
| 689 | return rc;
|
---|
| 690 | }
|
---|
| 691 |
|
---|
| 692 |
|
---|
| 693 | /**
|
---|
| 694 | * Deletes all undeleted critical sections (both types) initialized by a given
|
---|
| 695 | * device.
|
---|
| 696 | *
|
---|
| 697 | * @returns VBox status code.
|
---|
[58122] | 698 | * @param pVM The cross context VM structure.
|
---|
[1] | 699 | * @param pDevIns The device handle.
|
---|
| 700 | */
|
---|
[45152] | 701 | int pdmR3CritSectBothDeleteDevice(PVM pVM, PPDMDEVINS pDevIns)
|
---|
[1] | 702 | {
|
---|
[45152] | 703 | int rc1 = pdmR3CritSectDeleteByKey(pVM, pDevIns);
|
---|
| 704 | int rc2 = pdmR3CritSectRwDeleteByKey(pVM, pDevIns);
|
---|
| 705 | return RT_SUCCESS(rc1) ? rc2 : rc1;
|
---|
[1] | 706 | }
|
---|
| 707 |
|
---|
| 708 |
|
---|
| 709 | /**
|
---|
[45152] | 710 | * Deletes all undeleted critical sections (both types) initialized by a given
|
---|
| 711 | * driver.
|
---|
[28258] | 712 | *
|
---|
| 713 | * @returns VBox status code.
|
---|
[58122] | 714 | * @param pVM The cross context VM structure.
|
---|
[28258] | 715 | * @param pDrvIns The driver handle.
|
---|
| 716 | */
|
---|
[45152] | 717 | int pdmR3CritSectBothDeleteDriver(PVM pVM, PPDMDRVINS pDrvIns)
|
---|
[28258] | 718 | {
|
---|
[45152] | 719 | int rc1 = pdmR3CritSectDeleteByKey(pVM, pDrvIns);
|
---|
| 720 | int rc2 = pdmR3CritSectRwDeleteByKey(pVM, pDrvIns);
|
---|
| 721 | return RT_SUCCESS(rc1) ? rc2 : rc1;
|
---|
[28258] | 722 | }
|
---|
| 723 |
|
---|
| 724 |
|
---|
| 725 | /**
|
---|
[1] | 726 | * Deletes the critical section.
|
---|
| 727 | *
|
---|
| 728 | * @returns VBox status code.
|
---|
| 729 | * @param pCritSect The PDM critical section to destroy.
|
---|
| 730 | */
|
---|
[12989] | 731 | VMMR3DECL(int) PDMR3CritSectDelete(PPDMCRITSECT pCritSect)
|
---|
[1] | 732 | {
|
---|
| 733 | if (!RTCritSectIsInitialized(&pCritSect->s.Core))
|
---|
| 734 | return VINF_SUCCESS;
|
---|
| 735 |
|
---|
| 736 | /*
|
---|
| 737 | * Find and unlink it.
|
---|
| 738 | */
|
---|
[20008] | 739 | PVM pVM = pCritSect->s.pVMR3;
|
---|
[28258] | 740 | PUVM pUVM = pVM->pUVM;
|
---|
[39402] | 741 | AssertReleaseReturn(pVM, VERR_PDM_CRITSECT_IPE);
|
---|
[1] | 742 | PPDMCRITSECTINT pPrev = NULL;
|
---|
[28258] | 743 | RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
|
---|
| 744 | PPDMCRITSECTINT pCur = pUVM->pdm.s.pCritSects;
|
---|
[1] | 745 | while (pCur)
|
---|
| 746 | {
|
---|
| 747 | if (pCur == &pCritSect->s)
|
---|
[20008] | 748 | {
|
---|
[28258] | 749 | int rc = pdmR3CritSectDeleteOne(pVM, pUVM, pCur, pPrev, false /* not final */);
|
---|
| 750 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
[20008] | 751 | return rc;
|
---|
| 752 | }
|
---|
[1] | 753 |
|
---|
| 754 | /* next */
|
---|
| 755 | pPrev = pCur;
|
---|
| 756 | pCur = pCur->pNext;
|
---|
| 757 | }
|
---|
[28258] | 758 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
[1] | 759 | AssertReleaseMsgFailed(("pCritSect=%p wasn't found!\n", pCritSect));
|
---|
[39402] | 760 | return VERR_PDM_CRITSECT_NOT_FOUND;
|
---|
[1] | 761 | }
|
---|
| 762 |
|
---|
| 763 |
|
---|
| 764 | /**
|
---|
[45152] | 765 | * Deletes the read/write critical section.
|
---|
| 766 | *
|
---|
| 767 | * @returns VBox status code.
|
---|
| 768 | * @param pCritSect The PDM read/write critical section to destroy.
|
---|
| 769 | */
|
---|
| 770 | VMMR3DECL(int) PDMR3CritSectRwDelete(PPDMCRITSECTRW pCritSect)
|
---|
| 771 | {
|
---|
| 772 | if (!PDMCritSectRwIsInitialized(pCritSect))
|
---|
| 773 | return VINF_SUCCESS;
|
---|
| 774 |
|
---|
| 775 | /*
|
---|
| 776 | * Find and unlink it.
|
---|
| 777 | */
|
---|
| 778 | PVM pVM = pCritSect->s.pVMR3;
|
---|
| 779 | PUVM pUVM = pVM->pUVM;
|
---|
| 780 | AssertReleaseReturn(pVM, VERR_PDM_CRITSECT_IPE);
|
---|
| 781 | PPDMCRITSECTRWINT pPrev = NULL;
|
---|
| 782 | RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
|
---|
| 783 | PPDMCRITSECTRWINT pCur = pUVM->pdm.s.pRwCritSects;
|
---|
| 784 | while (pCur)
|
---|
| 785 | {
|
---|
| 786 | if (pCur == &pCritSect->s)
|
---|
| 787 | {
|
---|
| 788 | int rc = pdmR3CritSectRwDeleteOne(pVM, pUVM, pCur, pPrev, false /* not final */);
|
---|
| 789 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
| 790 | return rc;
|
---|
| 791 | }
|
---|
| 792 |
|
---|
| 793 | /* next */
|
---|
| 794 | pPrev = pCur;
|
---|
| 795 | pCur = pCur->pNext;
|
---|
| 796 | }
|
---|
| 797 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
| 798 | AssertReleaseMsgFailed(("pCritSect=%p wasn't found!\n", pCritSect));
|
---|
| 799 | return VERR_PDM_CRITSECT_NOT_FOUND;
|
---|
| 800 | }
|
---|
| 801 |
|
---|
| 802 |
|
---|
| 803 | /**
|
---|
[20087] | 804 | * Gets the name of the critical section.
|
---|
| 805 | *
|
---|
| 806 | *
|
---|
| 807 | * @returns Pointer to the critical section name (read only) on success,
|
---|
| 808 | * NULL on failure (invalid critical section).
|
---|
| 809 | * @param pCritSect The critical section.
|
---|
| 810 | */
|
---|
| 811 | VMMR3DECL(const char *) PDMR3CritSectName(PCPDMCRITSECT pCritSect)
|
---|
| 812 | {
|
---|
| 813 | AssertPtrReturn(pCritSect, NULL);
|
---|
| 814 | AssertReturn(pCritSect->s.Core.u32Magic == RTCRITSECT_MAGIC, NULL);
|
---|
| 815 | return pCritSect->s.pszName;
|
---|
| 816 | }
|
---|
| 817 |
|
---|
| 818 |
|
---|
| 819 | /**
|
---|
[45152] | 820 | * Gets the name of the read/write critical section.
|
---|
| 821 | *
|
---|
| 822 | *
|
---|
| 823 | * @returns Pointer to the critical section name (read only) on success,
|
---|
| 824 | * NULL on failure (invalid critical section).
|
---|
| 825 | * @param pCritSect The read/write critical section.
|
---|
| 826 | */
|
---|
| 827 | VMMR3DECL(const char *) PDMR3CritSectRwName(PCPDMCRITSECTRW pCritSect)
|
---|
| 828 | {
|
---|
| 829 | AssertPtrReturn(pCritSect, NULL);
|
---|
| 830 | AssertReturn(pCritSect->s.Core.u32Magic == RTCRITSECTRW_MAGIC, NULL);
|
---|
| 831 | return pCritSect->s.pszName;
|
---|
| 832 | }
|
---|
| 833 |
|
---|
| 834 |
|
---|
| 835 | /**
|
---|
[23350] | 836 | * Yield the critical section if someone is waiting on it.
|
---|
| 837 | *
|
---|
| 838 | * When yielding, we'll leave the critical section and try to make sure the
|
---|
| 839 | * other waiting threads get a chance of entering before we reclaim it.
|
---|
| 840 | *
|
---|
| 841 | * @retval true if yielded.
|
---|
| 842 | * @retval false if not yielded.
|
---|
[80673] | 843 | * @param pVM The cross context VM structure.
|
---|
[23350] | 844 | * @param pCritSect The critical section.
|
---|
| 845 | */
|
---|
[80673] | 846 | VMMR3DECL(bool) PDMR3CritSectYield(PVM pVM, PPDMCRITSECT pCritSect)
|
---|
[23350] | 847 | {
|
---|
| 848 | AssertPtrReturn(pCritSect, false);
|
---|
| 849 | AssertReturn(pCritSect->s.Core.u32Magic == RTCRITSECT_MAGIC, false);
|
---|
| 850 | Assert(pCritSect->s.Core.NativeThreadOwner == RTThreadNativeSelf());
|
---|
[37419] | 851 | Assert(!(pCritSect->s.Core.fFlags & RTCRITSECT_FLAGS_NOP));
|
---|
[80673] | 852 | RT_NOREF(pVM);
|
---|
[23350] | 853 |
|
---|
| 854 | /* No recursion allowed here. */
|
---|
| 855 | int32_t const cNestings = pCritSect->s.Core.cNestings;
|
---|
| 856 | AssertReturn(cNestings == 1, false);
|
---|
| 857 |
|
---|
| 858 | int32_t const cLockers = ASMAtomicReadS32(&pCritSect->s.Core.cLockers);
|
---|
| 859 | if (cLockers < cNestings)
|
---|
| 860 | return false;
|
---|
| 861 |
|
---|
[25368] | 862 | #ifdef PDMCRITSECT_STRICT
|
---|
[25607] | 863 | RTLOCKVALSRCPOS const SrcPos = pCritSect->s.Core.pValidatorRec->SrcPos;
|
---|
[25368] | 864 | #endif
|
---|
[23350] | 865 | PDMCritSectLeave(pCritSect);
|
---|
| 866 |
|
---|
| 867 | /*
|
---|
| 868 | * If we're lucky, then one of the waiters has entered the lock already.
|
---|
| 869 | * We spin a little bit in hope for this to happen so we can avoid the
|
---|
[33540] | 870 | * yield detour.
|
---|
[23350] | 871 | */
|
---|
| 872 | if (ASMAtomicUoReadS32(&pCritSect->s.Core.cNestings) == 0)
|
---|
| 873 | {
|
---|
| 874 | int cLoops = 20;
|
---|
| 875 | while ( cLoops > 0
|
---|
| 876 | && ASMAtomicUoReadS32(&pCritSect->s.Core.cNestings) == 0
|
---|
| 877 | && ASMAtomicUoReadS32(&pCritSect->s.Core.cLockers) >= 0)
|
---|
| 878 | {
|
---|
| 879 | ASMNopPause();
|
---|
| 880 | cLoops--;
|
---|
| 881 | }
|
---|
| 882 | if (cLoops == 0)
|
---|
| 883 | RTThreadYield();
|
---|
| 884 | }
|
---|
| 885 |
|
---|
[25368] | 886 | #ifdef PDMCRITSECT_STRICT
|
---|
[39402] | 887 | int rc = PDMCritSectEnterDebug(pCritSect, VERR_IGNORED,
|
---|
[25478] | 888 | SrcPos.uId, SrcPos.pszFile, SrcPos.uLine, SrcPos.pszFunction);
|
---|
[25368] | 889 | #else
|
---|
[39402] | 890 | int rc = PDMCritSectEnter(pCritSect, VERR_IGNORED);
|
---|
[25368] | 891 | #endif
|
---|
[23350] | 892 | AssertLogRelRC(rc);
|
---|
| 893 | return true;
|
---|
| 894 | }
|
---|
| 895 |
|
---|
| 896 |
|
---|
| 897 | /**
|
---|
[45152] | 898 | * PDMR3CritSectBothCountOwned worker.
|
---|
[18532] | 899 | *
|
---|
[45152] | 900 | * @param pszName The critical section name.
|
---|
| 901 | * @param ppszNames Pointer to the pszNames variable.
|
---|
| 902 | * @param pcchLeft Pointer to the cchLeft variable.
|
---|
| 903 | * @param fFirst Whether this is the first name or not.
|
---|
| 904 | */
|
---|
| 905 | static void pdmR3CritSectAppendNameToList(char const *pszName, char **ppszNames, size_t *pcchLeft, bool fFirst)
|
---|
| 906 | {
|
---|
| 907 | size_t cchLeft = *pcchLeft;
|
---|
| 908 | if (cchLeft)
|
---|
| 909 | {
|
---|
| 910 | char *pszNames = *ppszNames;
|
---|
| 911 |
|
---|
| 912 | /* try add comma. */
|
---|
| 913 | if (fFirst)
|
---|
| 914 | {
|
---|
| 915 | *pszNames++ = ',';
|
---|
| 916 | if (--cchLeft)
|
---|
| 917 | {
|
---|
| 918 | *pszNames++ = ' ';
|
---|
| 919 | cchLeft--;
|
---|
| 920 | }
|
---|
| 921 | }
|
---|
| 922 |
|
---|
| 923 | /* try copy the name. */
|
---|
| 924 | if (cchLeft)
|
---|
| 925 | {
|
---|
| 926 | size_t const cchName = strlen(pszName);
|
---|
| 927 | if (cchName < cchLeft)
|
---|
| 928 | {
|
---|
| 929 | memcpy(pszNames, pszName, cchName);
|
---|
| 930 | pszNames += cchName;
|
---|
| 931 | cchLeft -= cchName;
|
---|
| 932 | }
|
---|
| 933 | else
|
---|
| 934 | {
|
---|
| 935 | if (cchLeft > 2)
|
---|
| 936 | {
|
---|
| 937 | memcpy(pszNames, pszName, cchLeft - 2);
|
---|
| 938 | pszNames += cchLeft - 2;
|
---|
| 939 | cchLeft = 2;
|
---|
| 940 | }
|
---|
| 941 | while (cchLeft-- > 0)
|
---|
| 942 | *pszNames++ = '+';
|
---|
| 943 | }
|
---|
| 944 | }
|
---|
| 945 | *pszNames = '\0';
|
---|
| 946 |
|
---|
| 947 | *pcchLeft = cchLeft;
|
---|
| 948 | *ppszNames = pszNames;
|
---|
| 949 | }
|
---|
| 950 | }
|
---|
| 951 |
|
---|
| 952 |
|
---|
| 953 | /**
|
---|
| 954 | * Counts the critical sections (both type) owned by the calling thread,
|
---|
| 955 | * optionally returning a comma separated list naming them.
|
---|
| 956 | *
|
---|
| 957 | * Read ownerships are not included in non-strict builds.
|
---|
| 958 | *
|
---|
[18532] | 959 | * This is for diagnostic purposes only.
|
---|
| 960 | *
|
---|
| 961 | * @returns Lock count.
|
---|
| 962 | *
|
---|
[58122] | 963 | * @param pVM The cross context VM structure.
|
---|
[18532] | 964 | * @param pszNames Where to return the critical section names.
|
---|
| 965 | * @param cbNames The size of the buffer.
|
---|
| 966 | */
|
---|
| 967 | VMMR3DECL(uint32_t) PDMR3CritSectCountOwned(PVM pVM, char *pszNames, size_t cbNames)
|
---|
| 968 | {
|
---|
| 969 | /*
|
---|
| 970 | * Init the name buffer.
|
---|
| 971 | */
|
---|
| 972 | size_t cchLeft = cbNames;
|
---|
| 973 | if (cchLeft)
|
---|
| 974 | {
|
---|
| 975 | cchLeft--;
|
---|
| 976 | pszNames[0] = pszNames[cchLeft] = '\0';
|
---|
| 977 | }
|
---|
| 978 |
|
---|
| 979 | /*
|
---|
| 980 | * Iterate the critical sections.
|
---|
| 981 | */
|
---|
[45152] | 982 | uint32_t cCritSects = 0;
|
---|
| 983 | RTNATIVETHREAD const hNativeThread = RTThreadNativeSelf();
|
---|
[18532] | 984 | /* This is unsafe, but wtf. */
|
---|
[28258] | 985 | for (PPDMCRITSECTINT pCur = pVM->pUVM->pdm.s.pCritSects;
|
---|
[18532] | 986 | pCur;
|
---|
| 987 | pCur = pCur->pNext)
|
---|
| 988 | {
|
---|
| 989 | /* Same as RTCritSectIsOwner(). */
|
---|
| 990 | if (pCur->Core.NativeThreadOwner == hNativeThread)
|
---|
| 991 | {
|
---|
| 992 | cCritSects++;
|
---|
[45152] | 993 | pdmR3CritSectAppendNameToList(pCur->pszName, &pszNames, &cchLeft, cCritSects == 1);
|
---|
| 994 | }
|
---|
| 995 | }
|
---|
[18532] | 996 |
|
---|
[45152] | 997 | /* This is unsafe, but wtf. */
|
---|
| 998 | for (PPDMCRITSECTRWINT pCur = pVM->pUVM->pdm.s.pRwCritSects;
|
---|
| 999 | pCur;
|
---|
| 1000 | pCur = pCur->pNext)
|
---|
| 1001 | {
|
---|
| 1002 | if ( pCur->Core.hNativeWriter == hNativeThread
|
---|
| 1003 | || PDMCritSectRwIsReadOwner((PPDMCRITSECTRW)pCur, false /*fWannaHear*/) )
|
---|
| 1004 | {
|
---|
| 1005 | cCritSects++;
|
---|
| 1006 | pdmR3CritSectAppendNameToList(pCur->pszName, &pszNames, &cchLeft, cCritSects == 1);
|
---|
[18532] | 1007 | }
|
---|
| 1008 | }
|
---|
| 1009 |
|
---|
| 1010 | return cCritSects;
|
---|
| 1011 | }
|
---|
[20753] | 1012 |
|
---|
| 1013 |
|
---|
| 1014 | /**
|
---|
| 1015 | * Leave all critical sections the calling thread owns.
|
---|
| 1016 | *
|
---|
[37443] | 1017 | * This is only used when entering guru meditation in order to prevent other
|
---|
| 1018 | * EMTs and I/O threads from deadlocking.
|
---|
| 1019 | *
|
---|
[58122] | 1020 | * @param pVM The cross context VM structure.
|
---|
[20753] | 1021 | */
|
---|
[45152] | 1022 | VMMR3_INT_DECL(void) PDMR3CritSectLeaveAll(PVM pVM)
|
---|
[20753] | 1023 | {
|
---|
| 1024 | RTNATIVETHREAD const hNativeSelf = RTThreadNativeSelf();
|
---|
[28258] | 1025 | PUVM pUVM = pVM->pUVM;
|
---|
[20753] | 1026 |
|
---|
[28258] | 1027 | RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
|
---|
| 1028 | for (PPDMCRITSECTINT pCur = pUVM->pdm.s.pCritSects;
|
---|
[20753] | 1029 | pCur;
|
---|
| 1030 | pCur = pCur->pNext)
|
---|
| 1031 | {
|
---|
| 1032 | while ( pCur->Core.NativeThreadOwner == hNativeSelf
|
---|
| 1033 | && pCur->Core.cNestings > 0)
|
---|
| 1034 | PDMCritSectLeave((PPDMCRITSECT)pCur);
|
---|
| 1035 | }
|
---|
[28258] | 1036 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
[20753] | 1037 | }
|
---|
| 1038 |
|
---|
[37443] | 1039 |
|
---|
| 1040 | /**
|
---|
| 1041 | * Gets the address of the NOP critical section.
|
---|
| 1042 | *
|
---|
| 1043 | * The NOP critical section will not perform any thread serialization but let
|
---|
| 1044 | * all enter immediately and concurrently.
|
---|
| 1045 | *
|
---|
| 1046 | * @returns The address of the NOP critical section.
|
---|
[58122] | 1047 | * @param pVM The cross context VM structure.
|
---|
[37443] | 1048 | */
|
---|
| 1049 | VMMR3DECL(PPDMCRITSECT) PDMR3CritSectGetNop(PVM pVM)
|
---|
| 1050 | {
|
---|
| 1051 | VM_ASSERT_VALID_EXT_RETURN(pVM, NULL);
|
---|
| 1052 | return &pVM->pdm.s.NopCritSect;
|
---|
| 1053 | }
|
---|
| 1054 |
|
---|
| 1055 |
|
---|
| 1056 | /**
|
---|
| 1057 | * Gets the ring-0 address of the NOP critical section.
|
---|
| 1058 | *
|
---|
| 1059 | * @returns The ring-0 address of the NOP critical section.
|
---|
[58122] | 1060 | * @param pVM The cross context VM structure.
|
---|
[37443] | 1061 | */
|
---|
| 1062 | VMMR3DECL(R0PTRTYPE(PPDMCRITSECT)) PDMR3CritSectGetNopR0(PVM pVM)
|
---|
| 1063 | {
|
---|
| 1064 | VM_ASSERT_VALID_EXT_RETURN(pVM, NIL_RTR0PTR);
|
---|
| 1065 | return MMHyperR3ToR0(pVM, &pVM->pdm.s.NopCritSect);
|
---|
| 1066 | }
|
---|
| 1067 |
|
---|
| 1068 |
|
---|
| 1069 | /**
|
---|
| 1070 | * Gets the raw-mode context address of the NOP critical section.
|
---|
| 1071 | *
|
---|
| 1072 | * @returns The raw-mode context address of the NOP critical section.
|
---|
[58122] | 1073 | * @param pVM The cross context VM structure.
|
---|
[37443] | 1074 | */
|
---|
| 1075 | VMMR3DECL(RCPTRTYPE(PPDMCRITSECT)) PDMR3CritSectGetNopRC(PVM pVM)
|
---|
| 1076 | {
|
---|
| 1077 | VM_ASSERT_VALID_EXT_RETURN(pVM, NIL_RTRCPTR);
|
---|
| 1078 | return MMHyperR3ToRC(pVM, &pVM->pdm.s.NopCritSect);
|
---|
| 1079 | }
|
---|
| 1080 |
|
---|