1 | /* $Id: PDMCritSect.cpp 46493 2013-06-11 13:34:40Z 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 | if (!fFinal)
|
---|
528 | STAMR3DeregisterF(pVM->pUVM, "/PDM/CritSects/%s/*", pCritSect->pszName);
|
---|
529 | RTStrFree((char *)pCritSect->pszName);
|
---|
530 | pCritSect->pszName = NULL;
|
---|
531 | return rc;
|
---|
532 | }
|
---|
533 |
|
---|
534 |
|
---|
535 | /**
|
---|
536 | * Deletes one read/write critical section.
|
---|
537 | *
|
---|
538 | * @returns VBox status code.
|
---|
539 | *
|
---|
540 | * @param pVM Pointer to the VM.
|
---|
541 | * @param pCritSect The read/write critical section.
|
---|
542 | * @param pPrev The previous critical section in the list.
|
---|
543 | * @param fFinal Set if this is the final call and statistics shouldn't be deregistered.
|
---|
544 | *
|
---|
545 | * @remarks Caller must have entered the ListCritSect.
|
---|
546 | */
|
---|
547 | static int pdmR3CritSectRwDeleteOne(PVM pVM, PUVM pUVM, PPDMCRITSECTRWINT pCritSect, PPDMCRITSECTRWINT pPrev, bool fFinal)
|
---|
548 | {
|
---|
549 | /*
|
---|
550 | * Assert free waiters and so on (c&p from RTCritSectRwDelete).
|
---|
551 | */
|
---|
552 | Assert(pCritSect->Core.u32Magic == RTCRITSECTRW_MAGIC);
|
---|
553 | //Assert(pCritSect->Core.cNestings == 0);
|
---|
554 | //Assert(pCritSect->Core.cLockers == -1);
|
---|
555 | Assert(pCritSect->Core.hNativeWriter == NIL_RTNATIVETHREAD);
|
---|
556 |
|
---|
557 | /*
|
---|
558 | * Invalidate the structure and free the semaphores.
|
---|
559 | */
|
---|
560 | if (!ASMAtomicCmpXchgU32(&pCritSect->Core.u32Magic, RTCRITSECTRW_MAGIC_DEAD, RTCRITSECTRW_MAGIC))
|
---|
561 | AssertFailed();
|
---|
562 |
|
---|
563 | /*
|
---|
564 | * Unlink it.
|
---|
565 | */
|
---|
566 | if (pPrev)
|
---|
567 | pPrev->pNext = pCritSect->pNext;
|
---|
568 | else
|
---|
569 | pUVM->pdm.s.pRwCritSects = pCritSect->pNext;
|
---|
570 |
|
---|
571 | /*
|
---|
572 | * Delete it (parts taken from RTCritSectRwDelete).
|
---|
573 | * In case someone is waiting we'll signal the semaphore cLockers + 1 times.
|
---|
574 | */
|
---|
575 | pCritSect->Core.fFlags = 0;
|
---|
576 | pCritSect->Core.u64State = 0;
|
---|
577 |
|
---|
578 | SUPSEMEVENT hEvtWrite = (SUPSEMEVENT)pCritSect->Core.hEvtWrite;
|
---|
579 | pCritSect->Core.hEvtWrite = NIL_RTSEMEVENT;
|
---|
580 | AssertCompile(sizeof(hEvtWrite) == sizeof(pCritSect->Core.hEvtWrite));
|
---|
581 |
|
---|
582 | SUPSEMEVENTMULTI hEvtRead = (SUPSEMEVENTMULTI)pCritSect->Core.hEvtRead;
|
---|
583 | pCritSect->Core.hEvtRead = NIL_RTSEMEVENTMULTI;
|
---|
584 | AssertCompile(sizeof(hEvtRead) == sizeof(pCritSect->Core.hEvtRead));
|
---|
585 |
|
---|
586 | int rc1 = SUPSemEventClose(pVM->pSession, hEvtWrite); AssertRC(rc1);
|
---|
587 | int rc2 = SUPSemEventMultiClose(pVM->pSession, hEvtRead); AssertRC(rc2);
|
---|
588 |
|
---|
589 | RTLockValidatorRecSharedDestroy(&pCritSect->Core.pValidatorRead);
|
---|
590 | RTLockValidatorRecExclDestroy(&pCritSect->Core.pValidatorWrite);
|
---|
591 |
|
---|
592 | pCritSect->pNext = NULL;
|
---|
593 | pCritSect->pvKey = NULL;
|
---|
594 | pCritSect->pVMR3 = NULL;
|
---|
595 | pCritSect->pVMR0 = NIL_RTR0PTR;
|
---|
596 | pCritSect->pVMRC = NIL_RTRCPTR;
|
---|
597 | if (!fFinal)
|
---|
598 | STAMR3DeregisterF(pVM->pUVM, "/PDM/CritSectsRw/%s/*", pCritSect->pszName);
|
---|
599 | RTStrFree((char *)pCritSect->pszName);
|
---|
600 | pCritSect->pszName = NULL;
|
---|
601 |
|
---|
602 | return RT_SUCCESS(rc1) ? rc2 : rc1;
|
---|
603 | }
|
---|
604 |
|
---|
605 |
|
---|
606 | /**
|
---|
607 | * Deletes all critical sections with a give initializer key.
|
---|
608 | *
|
---|
609 | * @returns VBox status code.
|
---|
610 | * The entire list is processed on failure, so we'll only
|
---|
611 | * return the first error code. This shouldn't be a problem
|
---|
612 | * since errors really shouldn't happen here.
|
---|
613 | * @param pVM Pointer to the VM.
|
---|
614 | * @param pvKey The initializer key.
|
---|
615 | */
|
---|
616 | static int pdmR3CritSectDeleteByKey(PVM pVM, void *pvKey)
|
---|
617 | {
|
---|
618 | /*
|
---|
619 | * Iterate the list and match key.
|
---|
620 | */
|
---|
621 | PUVM pUVM = pVM->pUVM;
|
---|
622 | int rc = VINF_SUCCESS;
|
---|
623 | PPDMCRITSECTINT pPrev = NULL;
|
---|
624 | RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
|
---|
625 | PPDMCRITSECTINT pCur = pUVM->pdm.s.pCritSects;
|
---|
626 | while (pCur)
|
---|
627 | {
|
---|
628 | if (pCur->pvKey == pvKey)
|
---|
629 | {
|
---|
630 | int rc2 = pdmR3CritSectDeleteOne(pVM, pUVM, pCur, pPrev, false /* not final */);
|
---|
631 | AssertRC(rc2);
|
---|
632 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
633 | rc = rc2;
|
---|
634 | }
|
---|
635 |
|
---|
636 | /* next */
|
---|
637 | pPrev = pCur;
|
---|
638 | pCur = pCur->pNext;
|
---|
639 | }
|
---|
640 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
641 | return rc;
|
---|
642 | }
|
---|
643 |
|
---|
644 |
|
---|
645 | /**
|
---|
646 | * Deletes all read/write critical sections with a give initializer key.
|
---|
647 | *
|
---|
648 | * @returns VBox status code.
|
---|
649 | * The entire list is processed on failure, so we'll only
|
---|
650 | * return the first error code. This shouldn't be a problem
|
---|
651 | * since errors really shouldn't happen here.
|
---|
652 | * @param pVM Pointer to the VM.
|
---|
653 | * @param pvKey The initializer key.
|
---|
654 | */
|
---|
655 | static int pdmR3CritSectRwDeleteByKey(PVM pVM, void *pvKey)
|
---|
656 | {
|
---|
657 | /*
|
---|
658 | * Iterate the list and match key.
|
---|
659 | */
|
---|
660 | PUVM pUVM = pVM->pUVM;
|
---|
661 | int rc = VINF_SUCCESS;
|
---|
662 | PPDMCRITSECTRWINT pPrev = NULL;
|
---|
663 | RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
|
---|
664 | PPDMCRITSECTRWINT pCur = pUVM->pdm.s.pRwCritSects;
|
---|
665 | while (pCur)
|
---|
666 | {
|
---|
667 | if (pCur->pvKey == pvKey)
|
---|
668 | {
|
---|
669 | int rc2 = pdmR3CritSectRwDeleteOne(pVM, pUVM, pCur, pPrev, false /* not final */);
|
---|
670 | AssertRC(rc2);
|
---|
671 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
672 | rc = rc2;
|
---|
673 | }
|
---|
674 |
|
---|
675 | /* next */
|
---|
676 | pPrev = pCur;
|
---|
677 | pCur = pCur->pNext;
|
---|
678 | }
|
---|
679 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
680 | return rc;
|
---|
681 | }
|
---|
682 |
|
---|
683 |
|
---|
684 | /**
|
---|
685 | * Deletes all undeleted critical sections (both types) initialized by a given
|
---|
686 | * device.
|
---|
687 | *
|
---|
688 | * @returns VBox status code.
|
---|
689 | * @param pVM Pointer to the VM.
|
---|
690 | * @param pDevIns The device handle.
|
---|
691 | */
|
---|
692 | int pdmR3CritSectBothDeleteDevice(PVM pVM, PPDMDEVINS pDevIns)
|
---|
693 | {
|
---|
694 | int rc1 = pdmR3CritSectDeleteByKey(pVM, pDevIns);
|
---|
695 | int rc2 = pdmR3CritSectRwDeleteByKey(pVM, pDevIns);
|
---|
696 | return RT_SUCCESS(rc1) ? rc2 : rc1;
|
---|
697 | }
|
---|
698 |
|
---|
699 |
|
---|
700 | /**
|
---|
701 | * Deletes all undeleted critical sections (both types) initialized by a given
|
---|
702 | * driver.
|
---|
703 | *
|
---|
704 | * @returns VBox status code.
|
---|
705 | * @param pVM Pointer to the VM.
|
---|
706 | * @param pDrvIns The driver handle.
|
---|
707 | */
|
---|
708 | int pdmR3CritSectBothDeleteDriver(PVM pVM, PPDMDRVINS pDrvIns)
|
---|
709 | {
|
---|
710 | int rc1 = pdmR3CritSectDeleteByKey(pVM, pDrvIns);
|
---|
711 | int rc2 = pdmR3CritSectRwDeleteByKey(pVM, pDrvIns);
|
---|
712 | return RT_SUCCESS(rc1) ? rc2 : rc1;
|
---|
713 | }
|
---|
714 |
|
---|
715 |
|
---|
716 | /**
|
---|
717 | * Deletes the critical section.
|
---|
718 | *
|
---|
719 | * @returns VBox status code.
|
---|
720 | * @param pCritSect The PDM critical section to destroy.
|
---|
721 | */
|
---|
722 | VMMR3DECL(int) PDMR3CritSectDelete(PPDMCRITSECT pCritSect)
|
---|
723 | {
|
---|
724 | if (!RTCritSectIsInitialized(&pCritSect->s.Core))
|
---|
725 | return VINF_SUCCESS;
|
---|
726 |
|
---|
727 | /*
|
---|
728 | * Find and unlink it.
|
---|
729 | */
|
---|
730 | PVM pVM = pCritSect->s.pVMR3;
|
---|
731 | PUVM pUVM = pVM->pUVM;
|
---|
732 | AssertReleaseReturn(pVM, VERR_PDM_CRITSECT_IPE);
|
---|
733 | PPDMCRITSECTINT pPrev = NULL;
|
---|
734 | RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
|
---|
735 | PPDMCRITSECTINT pCur = pUVM->pdm.s.pCritSects;
|
---|
736 | while (pCur)
|
---|
737 | {
|
---|
738 | if (pCur == &pCritSect->s)
|
---|
739 | {
|
---|
740 | int rc = pdmR3CritSectDeleteOne(pVM, pUVM, pCur, pPrev, false /* not final */);
|
---|
741 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
742 | return rc;
|
---|
743 | }
|
---|
744 |
|
---|
745 | /* next */
|
---|
746 | pPrev = pCur;
|
---|
747 | pCur = pCur->pNext;
|
---|
748 | }
|
---|
749 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
750 | AssertReleaseMsgFailed(("pCritSect=%p wasn't found!\n", pCritSect));
|
---|
751 | return VERR_PDM_CRITSECT_NOT_FOUND;
|
---|
752 | }
|
---|
753 |
|
---|
754 |
|
---|
755 | /**
|
---|
756 | * Deletes the read/write critical section.
|
---|
757 | *
|
---|
758 | * @returns VBox status code.
|
---|
759 | * @param pCritSect The PDM read/write critical section to destroy.
|
---|
760 | */
|
---|
761 | VMMR3DECL(int) PDMR3CritSectRwDelete(PPDMCRITSECTRW pCritSect)
|
---|
762 | {
|
---|
763 | if (!PDMCritSectRwIsInitialized(pCritSect))
|
---|
764 | return VINF_SUCCESS;
|
---|
765 |
|
---|
766 | /*
|
---|
767 | * Find and unlink it.
|
---|
768 | */
|
---|
769 | PVM pVM = pCritSect->s.pVMR3;
|
---|
770 | PUVM pUVM = pVM->pUVM;
|
---|
771 | AssertReleaseReturn(pVM, VERR_PDM_CRITSECT_IPE);
|
---|
772 | PPDMCRITSECTRWINT pPrev = NULL;
|
---|
773 | RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
|
---|
774 | PPDMCRITSECTRWINT pCur = pUVM->pdm.s.pRwCritSects;
|
---|
775 | while (pCur)
|
---|
776 | {
|
---|
777 | if (pCur == &pCritSect->s)
|
---|
778 | {
|
---|
779 | int rc = pdmR3CritSectRwDeleteOne(pVM, pUVM, pCur, pPrev, false /* not final */);
|
---|
780 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
781 | return rc;
|
---|
782 | }
|
---|
783 |
|
---|
784 | /* next */
|
---|
785 | pPrev = pCur;
|
---|
786 | pCur = pCur->pNext;
|
---|
787 | }
|
---|
788 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
789 | AssertReleaseMsgFailed(("pCritSect=%p wasn't found!\n", pCritSect));
|
---|
790 | return VERR_PDM_CRITSECT_NOT_FOUND;
|
---|
791 | }
|
---|
792 |
|
---|
793 |
|
---|
794 | /**
|
---|
795 | * Gets the name of the critical section.
|
---|
796 | *
|
---|
797 | *
|
---|
798 | * @returns Pointer to the critical section name (read only) on success,
|
---|
799 | * NULL on failure (invalid critical section).
|
---|
800 | * @param pCritSect The critical section.
|
---|
801 | */
|
---|
802 | VMMR3DECL(const char *) PDMR3CritSectName(PCPDMCRITSECT pCritSect)
|
---|
803 | {
|
---|
804 | AssertPtrReturn(pCritSect, NULL);
|
---|
805 | AssertReturn(pCritSect->s.Core.u32Magic == RTCRITSECT_MAGIC, NULL);
|
---|
806 | return pCritSect->s.pszName;
|
---|
807 | }
|
---|
808 |
|
---|
809 |
|
---|
810 | /**
|
---|
811 | * Gets the name of the read/write critical section.
|
---|
812 | *
|
---|
813 | *
|
---|
814 | * @returns Pointer to the critical section name (read only) on success,
|
---|
815 | * NULL on failure (invalid critical section).
|
---|
816 | * @param pCritSect The read/write critical section.
|
---|
817 | */
|
---|
818 | VMMR3DECL(const char *) PDMR3CritSectRwName(PCPDMCRITSECTRW pCritSect)
|
---|
819 | {
|
---|
820 | AssertPtrReturn(pCritSect, NULL);
|
---|
821 | AssertReturn(pCritSect->s.Core.u32Magic == RTCRITSECTRW_MAGIC, NULL);
|
---|
822 | return pCritSect->s.pszName;
|
---|
823 | }
|
---|
824 |
|
---|
825 |
|
---|
826 | /**
|
---|
827 | * Yield the critical section if someone is waiting on it.
|
---|
828 | *
|
---|
829 | * When yielding, we'll leave the critical section and try to make sure the
|
---|
830 | * other waiting threads get a chance of entering before we reclaim it.
|
---|
831 | *
|
---|
832 | * @retval true if yielded.
|
---|
833 | * @retval false if not yielded.
|
---|
834 | * @param pCritSect The critical section.
|
---|
835 | */
|
---|
836 | VMMR3DECL(bool) PDMR3CritSectYield(PPDMCRITSECT pCritSect)
|
---|
837 | {
|
---|
838 | AssertPtrReturn(pCritSect, false);
|
---|
839 | AssertReturn(pCritSect->s.Core.u32Magic == RTCRITSECT_MAGIC, false);
|
---|
840 | Assert(pCritSect->s.Core.NativeThreadOwner == RTThreadNativeSelf());
|
---|
841 | Assert(!(pCritSect->s.Core.fFlags & RTCRITSECT_FLAGS_NOP));
|
---|
842 |
|
---|
843 | /* No recursion allowed here. */
|
---|
844 | int32_t const cNestings = pCritSect->s.Core.cNestings;
|
---|
845 | AssertReturn(cNestings == 1, false);
|
---|
846 |
|
---|
847 | int32_t const cLockers = ASMAtomicReadS32(&pCritSect->s.Core.cLockers);
|
---|
848 | if (cLockers < cNestings)
|
---|
849 | return false;
|
---|
850 |
|
---|
851 | #ifdef PDMCRITSECT_STRICT
|
---|
852 | RTLOCKVALSRCPOS const SrcPos = pCritSect->s.Core.pValidatorRec->SrcPos;
|
---|
853 | #endif
|
---|
854 | PDMCritSectLeave(pCritSect);
|
---|
855 |
|
---|
856 | /*
|
---|
857 | * If we're lucky, then one of the waiters has entered the lock already.
|
---|
858 | * We spin a little bit in hope for this to happen so we can avoid the
|
---|
859 | * yield detour.
|
---|
860 | */
|
---|
861 | if (ASMAtomicUoReadS32(&pCritSect->s.Core.cNestings) == 0)
|
---|
862 | {
|
---|
863 | int cLoops = 20;
|
---|
864 | while ( cLoops > 0
|
---|
865 | && ASMAtomicUoReadS32(&pCritSect->s.Core.cNestings) == 0
|
---|
866 | && ASMAtomicUoReadS32(&pCritSect->s.Core.cLockers) >= 0)
|
---|
867 | {
|
---|
868 | ASMNopPause();
|
---|
869 | cLoops--;
|
---|
870 | }
|
---|
871 | if (cLoops == 0)
|
---|
872 | RTThreadYield();
|
---|
873 | }
|
---|
874 |
|
---|
875 | #ifdef PDMCRITSECT_STRICT
|
---|
876 | int rc = PDMCritSectEnterDebug(pCritSect, VERR_IGNORED,
|
---|
877 | SrcPos.uId, SrcPos.pszFile, SrcPos.uLine, SrcPos.pszFunction);
|
---|
878 | #else
|
---|
879 | int rc = PDMCritSectEnter(pCritSect, VERR_IGNORED);
|
---|
880 | #endif
|
---|
881 | AssertLogRelRC(rc);
|
---|
882 | return true;
|
---|
883 | }
|
---|
884 |
|
---|
885 |
|
---|
886 | /**
|
---|
887 | * Schedule a event semaphore for signalling upon critsect exit.
|
---|
888 | *
|
---|
889 | * @returns VINF_SUCCESS on success.
|
---|
890 | * @returns VERR_TOO_MANY_SEMAPHORES if an event was already scheduled.
|
---|
891 | * @returns VERR_NOT_OWNER if we're not the critsect owner.
|
---|
892 | * @returns VERR_SEM_DESTROYED if RTCritSectDelete was called while waiting.
|
---|
893 | *
|
---|
894 | * @param pCritSect The critical section.
|
---|
895 | * @param EventToSignal The semaphore that should be signalled.
|
---|
896 | */
|
---|
897 | VMMR3DECL(int) PDMR3CritSectScheduleExitEvent(PPDMCRITSECT pCritSect, RTSEMEVENT EventToSignal)
|
---|
898 | {
|
---|
899 | AssertPtr(pCritSect);
|
---|
900 | Assert(!(pCritSect->s.Core.fFlags & RTCRITSECT_FLAGS_NOP));
|
---|
901 | Assert(EventToSignal != NIL_RTSEMEVENT);
|
---|
902 | if (RT_UNLIKELY(!RTCritSectIsOwner(&pCritSect->s.Core)))
|
---|
903 | return VERR_NOT_OWNER;
|
---|
904 | if (RT_LIKELY( pCritSect->s.EventToSignal == NIL_RTSEMEVENT
|
---|
905 | || pCritSect->s.EventToSignal == EventToSignal))
|
---|
906 | {
|
---|
907 | pCritSect->s.EventToSignal = EventToSignal;
|
---|
908 | return VINF_SUCCESS;
|
---|
909 | }
|
---|
910 | return VERR_TOO_MANY_SEMAPHORES;
|
---|
911 | }
|
---|
912 |
|
---|
913 |
|
---|
914 | /**
|
---|
915 | * PDMR3CritSectBothCountOwned worker.
|
---|
916 | *
|
---|
917 | * @param pszName The critical section name.
|
---|
918 | * @param ppszNames Pointer to the pszNames variable.
|
---|
919 | * @param pcchLeft Pointer to the cchLeft variable.
|
---|
920 | * @param fFirst Whether this is the first name or not.
|
---|
921 | */
|
---|
922 | static void pdmR3CritSectAppendNameToList(char const *pszName, char **ppszNames, size_t *pcchLeft, bool fFirst)
|
---|
923 | {
|
---|
924 | size_t cchLeft = *pcchLeft;
|
---|
925 | if (cchLeft)
|
---|
926 | {
|
---|
927 | char *pszNames = *ppszNames;
|
---|
928 |
|
---|
929 | /* try add comma. */
|
---|
930 | if (fFirst)
|
---|
931 | {
|
---|
932 | *pszNames++ = ',';
|
---|
933 | if (--cchLeft)
|
---|
934 | {
|
---|
935 | *pszNames++ = ' ';
|
---|
936 | cchLeft--;
|
---|
937 | }
|
---|
938 | }
|
---|
939 |
|
---|
940 | /* try copy the name. */
|
---|
941 | if (cchLeft)
|
---|
942 | {
|
---|
943 | size_t const cchName = strlen(pszName);
|
---|
944 | if (cchName < cchLeft)
|
---|
945 | {
|
---|
946 | memcpy(pszNames, pszName, cchName);
|
---|
947 | pszNames += cchName;
|
---|
948 | cchLeft -= cchName;
|
---|
949 | }
|
---|
950 | else
|
---|
951 | {
|
---|
952 | if (cchLeft > 2)
|
---|
953 | {
|
---|
954 | memcpy(pszNames, pszName, cchLeft - 2);
|
---|
955 | pszNames += cchLeft - 2;
|
---|
956 | cchLeft = 2;
|
---|
957 | }
|
---|
958 | while (cchLeft-- > 0)
|
---|
959 | *pszNames++ = '+';
|
---|
960 | }
|
---|
961 | }
|
---|
962 | *pszNames = '\0';
|
---|
963 |
|
---|
964 | *pcchLeft = cchLeft;
|
---|
965 | *ppszNames = pszNames;
|
---|
966 | }
|
---|
967 | }
|
---|
968 |
|
---|
969 |
|
---|
970 | /**
|
---|
971 | * Counts the critical sections (both type) owned by the calling thread,
|
---|
972 | * optionally returning a comma separated list naming them.
|
---|
973 | *
|
---|
974 | * Read ownerships are not included in non-strict builds.
|
---|
975 | *
|
---|
976 | * This is for diagnostic purposes only.
|
---|
977 | *
|
---|
978 | * @returns Lock count.
|
---|
979 | *
|
---|
980 | * @param pVM Pointer to the VM.
|
---|
981 | * @param pszNames Where to return the critical section names.
|
---|
982 | * @param cbNames The size of the buffer.
|
---|
983 | */
|
---|
984 | VMMR3DECL(uint32_t) PDMR3CritSectCountOwned(PVM pVM, char *pszNames, size_t cbNames)
|
---|
985 | {
|
---|
986 | /*
|
---|
987 | * Init the name buffer.
|
---|
988 | */
|
---|
989 | size_t cchLeft = cbNames;
|
---|
990 | if (cchLeft)
|
---|
991 | {
|
---|
992 | cchLeft--;
|
---|
993 | pszNames[0] = pszNames[cchLeft] = '\0';
|
---|
994 | }
|
---|
995 |
|
---|
996 | /*
|
---|
997 | * Iterate the critical sections.
|
---|
998 | */
|
---|
999 | uint32_t cCritSects = 0;
|
---|
1000 | RTNATIVETHREAD const hNativeThread = RTThreadNativeSelf();
|
---|
1001 | /* This is unsafe, but wtf. */
|
---|
1002 | for (PPDMCRITSECTINT pCur = pVM->pUVM->pdm.s.pCritSects;
|
---|
1003 | pCur;
|
---|
1004 | pCur = pCur->pNext)
|
---|
1005 | {
|
---|
1006 | /* Same as RTCritSectIsOwner(). */
|
---|
1007 | if (pCur->Core.NativeThreadOwner == hNativeThread)
|
---|
1008 | {
|
---|
1009 | cCritSects++;
|
---|
1010 | pdmR3CritSectAppendNameToList(pCur->pszName, &pszNames, &cchLeft, cCritSects == 1);
|
---|
1011 | }
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | /* This is unsafe, but wtf. */
|
---|
1015 | for (PPDMCRITSECTRWINT pCur = pVM->pUVM->pdm.s.pRwCritSects;
|
---|
1016 | pCur;
|
---|
1017 | pCur = pCur->pNext)
|
---|
1018 | {
|
---|
1019 | if ( pCur->Core.hNativeWriter == hNativeThread
|
---|
1020 | || PDMCritSectRwIsReadOwner((PPDMCRITSECTRW)pCur, false /*fWannaHear*/) )
|
---|
1021 | {
|
---|
1022 | cCritSects++;
|
---|
1023 | pdmR3CritSectAppendNameToList(pCur->pszName, &pszNames, &cchLeft, cCritSects == 1);
|
---|
1024 | }
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | return cCritSects;
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 |
|
---|
1031 | /**
|
---|
1032 | * Leave all critical sections the calling thread owns.
|
---|
1033 | *
|
---|
1034 | * This is only used when entering guru meditation in order to prevent other
|
---|
1035 | * EMTs and I/O threads from deadlocking.
|
---|
1036 | *
|
---|
1037 | * @param pVM Pointer to the VM.
|
---|
1038 | */
|
---|
1039 | VMMR3_INT_DECL(void) PDMR3CritSectLeaveAll(PVM pVM)
|
---|
1040 | {
|
---|
1041 | RTNATIVETHREAD const hNativeSelf = RTThreadNativeSelf();
|
---|
1042 | PUVM pUVM = pVM->pUVM;
|
---|
1043 |
|
---|
1044 | RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
|
---|
1045 | for (PPDMCRITSECTINT pCur = pUVM->pdm.s.pCritSects;
|
---|
1046 | pCur;
|
---|
1047 | pCur = pCur->pNext)
|
---|
1048 | {
|
---|
1049 | while ( pCur->Core.NativeThreadOwner == hNativeSelf
|
---|
1050 | && pCur->Core.cNestings > 0)
|
---|
1051 | PDMCritSectLeave((PPDMCRITSECT)pCur);
|
---|
1052 | }
|
---|
1053 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
1054 | }
|
---|
1055 |
|
---|
1056 |
|
---|
1057 | /**
|
---|
1058 | * Gets the address of the NOP critical section.
|
---|
1059 | *
|
---|
1060 | * The NOP critical section will not perform any thread serialization but let
|
---|
1061 | * all enter immediately and concurrently.
|
---|
1062 | *
|
---|
1063 | * @returns The address of the NOP critical section.
|
---|
1064 | * @param pVM Pointer to the VM.
|
---|
1065 | */
|
---|
1066 | VMMR3DECL(PPDMCRITSECT) PDMR3CritSectGetNop(PVM pVM)
|
---|
1067 | {
|
---|
1068 | VM_ASSERT_VALID_EXT_RETURN(pVM, NULL);
|
---|
1069 | return &pVM->pdm.s.NopCritSect;
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 |
|
---|
1073 | /**
|
---|
1074 | * Gets the ring-0 address of the NOP critical section.
|
---|
1075 | *
|
---|
1076 | * @returns The ring-0 address of the NOP critical section.
|
---|
1077 | * @param pVM Pointer to the VM.
|
---|
1078 | */
|
---|
1079 | VMMR3DECL(R0PTRTYPE(PPDMCRITSECT)) PDMR3CritSectGetNopR0(PVM pVM)
|
---|
1080 | {
|
---|
1081 | VM_ASSERT_VALID_EXT_RETURN(pVM, NIL_RTR0PTR);
|
---|
1082 | return MMHyperR3ToR0(pVM, &pVM->pdm.s.NopCritSect);
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 |
|
---|
1086 | /**
|
---|
1087 | * Gets the raw-mode context address of the NOP critical section.
|
---|
1088 | *
|
---|
1089 | * @returns The raw-mode context address of the NOP critical section.
|
---|
1090 | * @param pVM Pointer to the VM.
|
---|
1091 | */
|
---|
1092 | VMMR3DECL(RCPTRTYPE(PPDMCRITSECT)) PDMR3CritSectGetNopRC(PVM pVM)
|
---|
1093 | {
|
---|
1094 | VM_ASSERT_VALID_EXT_RETURN(pVM, NIL_RTRCPTR);
|
---|
1095 | return MMHyperR3ToRC(pVM, &pVM->pdm.s.NopCritSect);
|
---|
1096 | }
|
---|
1097 |
|
---|