VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/PDMCritSect.cpp@ 61574

Last change on this file since 61574 was 58170, checked in by vboxsync, 9 years ago

doxygen: fixes.

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

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