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