VirtualBox

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

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

VMM: Doxygen fixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 38.3 KB
Line 
1/* $Id: PDMCritSect.cpp 58116 2015-10-08 14:51:53Z 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 Pointer to the VM.
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 Pointer to the VM.
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.
96 * First error code, rest is lost.
97 * @param pVMU The user mode VM handle.
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 Pointer to the VM.
132 * @param pCritSect The critical section.
133 * @param pvKey The owner key.
134 * @param SRC_POS 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 */
140static 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->hEventToSignal = NIL_SUPSEMEVENT;
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 SRC_POS 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 */
227static 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 SRC_POS 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 */
333VMMR3DECL(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 SRC_POS 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 */
363VMMR3DECL(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 SRC_POS The source position. Optional.
385 * @param pszNameFmt Format string for naming the critical section. For
386 * statistics and lock validation.
387 * @param va Arguments for the format string.
388 */
389int pdmR3CritSectInitDevice(PVM pVM, PPDMDEVINS pDevIns, PPDMCRITSECT pCritSect, RT_SRC_POS_DECL,
390 const char *pszNameFmt, va_list va)
391{
392 return pdmR3CritSectInitOne(pVM, &pCritSect->s, pDevIns, RT_SRC_POS_ARGS, pszNameFmt, va);
393}
394
395
396/**
397 * Initializes a PDM read/write critical section for a device.
398 *
399 * @returns VBox status code.
400 * @param pVM Pointer to the VM.
401 * @param pDevIns Device instance.
402 * @param pCritSect Pointer to the read/write critical section.
403 * @param SRC_POS The source position. Optional.
404 * @param pszNameFmt Format string for naming the critical section. For
405 * statistics and lock validation.
406 * @param va Arguments for the format string.
407 */
408int pdmR3CritSectRwInitDevice(PVM pVM, PPDMDEVINS pDevIns, PPDMCRITSECTRW pCritSect, RT_SRC_POS_DECL,
409 const char *pszNameFmt, va_list va)
410{
411 return pdmR3CritSectRwInitOne(pVM, &pCritSect->s, pDevIns, RT_SRC_POS_ARGS, pszNameFmt, va);
412}
413
414
415/**
416 * Initializes the automatic default PDM critical section for a device.
417 *
418 * @returns VBox status code.
419 * @param pVM Pointer to the VM.
420 * @param pDevIns Device instance.
421 * @param SRC_POS The source position. Optional.
422 * @param pCritSect Pointer to the critical section.
423 */
424int pdmR3CritSectInitDeviceAuto(PVM pVM, PPDMDEVINS pDevIns, PPDMCRITSECT pCritSect, RT_SRC_POS_DECL,
425 const char *pszNameFmt, ...)
426{
427 va_list va;
428 va_start(va, pszNameFmt);
429 int rc = pdmR3CritSectInitOne(pVM, &pCritSect->s, pDevIns, RT_SRC_POS_ARGS, pszNameFmt, va);
430 if (RT_SUCCESS(rc))
431 pCritSect->s.fAutomaticDefaultCritsect = true;
432 va_end(va);
433 return rc;
434}
435
436
437/**
438 * Initializes a PDM critical section for a driver.
439 *
440 * @returns VBox status code.
441 * @param pVM Pointer to the VM.
442 * @param pDrvIns Driver instance.
443 * @param pCritSect Pointer to the critical section.
444 * @param SRC_POS The source position. Optional.
445 * @param pszNameFmt Format string for naming the critical section. For
446 * statistics and lock validation.
447 * @param ... Arguments for the format string.
448 */
449int pdmR3CritSectInitDriver(PVM pVM, PPDMDRVINS pDrvIns, PPDMCRITSECT pCritSect, RT_SRC_POS_DECL,
450 const char *pszNameFmt, ...)
451{
452 va_list va;
453 va_start(va, pszNameFmt);
454 int rc = pdmR3CritSectInitOne(pVM, &pCritSect->s, pDrvIns, RT_SRC_POS_ARGS, pszNameFmt, va);
455 va_end(va);
456 return rc;
457}
458
459
460/**
461 * Initializes a PDM read/write critical section for a driver.
462 *
463 * @returns VBox status code.
464 * @param pVM Pointer to the VM.
465 * @param pDrvIns Driver instance.
466 * @param pCritSect Pointer to the read/write critical section.
467 * @param SRC_POS The source position. Optional.
468 * @param pszNameFmt Format string for naming the critical section. For
469 * statistics and lock validation.
470 * @param ... Arguments for the format string.
471 */
472int pdmR3CritSectRwInitDriver(PVM pVM, PPDMDRVINS pDrvIns, PPDMCRITSECTRW pCritSect, RT_SRC_POS_DECL,
473 const char *pszNameFmt, ...)
474{
475 va_list va;
476 va_start(va, pszNameFmt);
477 int rc = pdmR3CritSectRwInitOne(pVM, &pCritSect->s, pDrvIns, RT_SRC_POS_ARGS, pszNameFmt, va);
478 va_end(va);
479 return rc;
480}
481
482
483/**
484 * Deletes one critical section.
485 *
486 * @returns Return code from RTCritSectDelete.
487 *
488 * @param pVM Pointer to the VM.
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 Pointer to the VM.
546 * @param pCritSect The read/write critical section.
547 * @param pPrev The previous critical section in the list.
548 * @param fFinal Set if this is the final call and statistics shouldn't be deregistered.
549 *
550 * @remarks Caller must have entered the ListCritSect.
551 */
552static int pdmR3CritSectRwDeleteOne(PVM pVM, PUVM pUVM, PPDMCRITSECTRWINT pCritSect, PPDMCRITSECTRWINT pPrev, bool fFinal)
553{
554 /*
555 * Assert free waiters and so on (c&p from RTCritSectRwDelete).
556 */
557 Assert(pCritSect->Core.u32Magic == RTCRITSECTRW_MAGIC);
558 //Assert(pCritSect->Core.cNestings == 0);
559 //Assert(pCritSect->Core.cLockers == -1);
560 Assert(pCritSect->Core.hNativeWriter == NIL_RTNATIVETHREAD);
561
562 /*
563 * Invalidate the structure and free the semaphores.
564 */
565 if (!ASMAtomicCmpXchgU32(&pCritSect->Core.u32Magic, RTCRITSECTRW_MAGIC_DEAD, RTCRITSECTRW_MAGIC))
566 AssertFailed();
567
568 /*
569 * Unlink it.
570 */
571 if (pPrev)
572 pPrev->pNext = pCritSect->pNext;
573 else
574 pUVM->pdm.s.pRwCritSects = pCritSect->pNext;
575
576 /*
577 * Delete it (parts taken from RTCritSectRwDelete).
578 * In case someone is waiting we'll signal the semaphore cLockers + 1 times.
579 */
580 pCritSect->Core.fFlags = 0;
581 pCritSect->Core.u64State = 0;
582
583 SUPSEMEVENT hEvtWrite = (SUPSEMEVENT)pCritSect->Core.hEvtWrite;
584 pCritSect->Core.hEvtWrite = NIL_RTSEMEVENT;
585 AssertCompile(sizeof(hEvtWrite) == sizeof(pCritSect->Core.hEvtWrite));
586
587 SUPSEMEVENTMULTI hEvtRead = (SUPSEMEVENTMULTI)pCritSect->Core.hEvtRead;
588 pCritSect->Core.hEvtRead = NIL_RTSEMEVENTMULTI;
589 AssertCompile(sizeof(hEvtRead) == sizeof(pCritSect->Core.hEvtRead));
590
591 int rc1 = SUPSemEventClose(pVM->pSession, hEvtWrite); AssertRC(rc1);
592 int rc2 = SUPSemEventMultiClose(pVM->pSession, hEvtRead); AssertRC(rc2);
593
594 RTLockValidatorRecSharedDestroy(&pCritSect->Core.pValidatorRead);
595 RTLockValidatorRecExclDestroy(&pCritSect->Core.pValidatorWrite);
596
597 pCritSect->pNext = NULL;
598 pCritSect->pvKey = NULL;
599 pCritSect->pVMR3 = NULL;
600 pCritSect->pVMR0 = NIL_RTR0PTR;
601 pCritSect->pVMRC = NIL_RTRCPTR;
602 if (!fFinal)
603 STAMR3DeregisterF(pVM->pUVM, "/PDM/CritSectsRw/%s/*", pCritSect->pszName);
604 RTStrFree((char *)pCritSect->pszName);
605 pCritSect->pszName = NULL;
606
607 return RT_SUCCESS(rc1) ? rc2 : rc1;
608}
609
610
611/**
612 * Deletes all critical sections with a give initializer key.
613 *
614 * @returns VBox status code.
615 * The entire list is processed on failure, so we'll only
616 * return the first error code. This shouldn't be a problem
617 * since errors really shouldn't happen here.
618 * @param pVM Pointer to the VM.
619 * @param pvKey The initializer key.
620 */
621static int pdmR3CritSectDeleteByKey(PVM pVM, void *pvKey)
622{
623 /*
624 * Iterate the list and match key.
625 */
626 PUVM pUVM = pVM->pUVM;
627 int rc = VINF_SUCCESS;
628 PPDMCRITSECTINT pPrev = NULL;
629 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
630 PPDMCRITSECTINT pCur = pUVM->pdm.s.pCritSects;
631 while (pCur)
632 {
633 if (pCur->pvKey == pvKey)
634 {
635 int rc2 = pdmR3CritSectDeleteOne(pVM, pUVM, pCur, pPrev, false /* not final */);
636 AssertRC(rc2);
637 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
638 rc = rc2;
639 }
640
641 /* next */
642 pPrev = pCur;
643 pCur = pCur->pNext;
644 }
645 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
646 return rc;
647}
648
649
650/**
651 * Deletes all read/write critical sections with a give initializer key.
652 *
653 * @returns VBox status code.
654 * The entire list is processed on failure, so we'll only
655 * return the first error code. This shouldn't be a problem
656 * since errors really shouldn't happen here.
657 * @param pVM Pointer to the VM.
658 * @param pvKey The initializer key.
659 */
660static int pdmR3CritSectRwDeleteByKey(PVM pVM, void *pvKey)
661{
662 /*
663 * Iterate the list and match key.
664 */
665 PUVM pUVM = pVM->pUVM;
666 int rc = VINF_SUCCESS;
667 PPDMCRITSECTRWINT pPrev = NULL;
668 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
669 PPDMCRITSECTRWINT pCur = pUVM->pdm.s.pRwCritSects;
670 while (pCur)
671 {
672 if (pCur->pvKey == pvKey)
673 {
674 int rc2 = pdmR3CritSectRwDeleteOne(pVM, pUVM, pCur, pPrev, false /* not final */);
675 AssertRC(rc2);
676 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
677 rc = rc2;
678 }
679
680 /* next */
681 pPrev = pCur;
682 pCur = pCur->pNext;
683 }
684 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
685 return rc;
686}
687
688
689/**
690 * Deletes all undeleted critical sections (both types) initialized by a given
691 * device.
692 *
693 * @returns VBox status code.
694 * @param pVM Pointer to the VM.
695 * @param pDevIns The device handle.
696 */
697int pdmR3CritSectBothDeleteDevice(PVM pVM, PPDMDEVINS pDevIns)
698{
699 int rc1 = pdmR3CritSectDeleteByKey(pVM, pDevIns);
700 int rc2 = pdmR3CritSectRwDeleteByKey(pVM, pDevIns);
701 return RT_SUCCESS(rc1) ? rc2 : rc1;
702}
703
704
705/**
706 * Deletes all undeleted critical sections (both types) initialized by a given
707 * driver.
708 *
709 * @returns VBox status code.
710 * @param pVM Pointer to the VM.
711 * @param pDrvIns The driver handle.
712 */
713int pdmR3CritSectBothDeleteDriver(PVM pVM, PPDMDRVINS pDrvIns)
714{
715 int rc1 = pdmR3CritSectDeleteByKey(pVM, pDrvIns);
716 int rc2 = pdmR3CritSectRwDeleteByKey(pVM, pDrvIns);
717 return RT_SUCCESS(rc1) ? rc2 : rc1;
718}
719
720
721/**
722 * Deletes the critical section.
723 *
724 * @returns VBox status code.
725 * @param pCritSect The PDM critical section to destroy.
726 */
727VMMR3DECL(int) PDMR3CritSectDelete(PPDMCRITSECT pCritSect)
728{
729 if (!RTCritSectIsInitialized(&pCritSect->s.Core))
730 return VINF_SUCCESS;
731
732 /*
733 * Find and unlink it.
734 */
735 PVM pVM = pCritSect->s.pVMR3;
736 PUVM pUVM = pVM->pUVM;
737 AssertReleaseReturn(pVM, VERR_PDM_CRITSECT_IPE);
738 PPDMCRITSECTINT pPrev = NULL;
739 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
740 PPDMCRITSECTINT pCur = pUVM->pdm.s.pCritSects;
741 while (pCur)
742 {
743 if (pCur == &pCritSect->s)
744 {
745 int rc = pdmR3CritSectDeleteOne(pVM, pUVM, pCur, pPrev, false /* not final */);
746 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
747 return rc;
748 }
749
750 /* next */
751 pPrev = pCur;
752 pCur = pCur->pNext;
753 }
754 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
755 AssertReleaseMsgFailed(("pCritSect=%p wasn't found!\n", pCritSect));
756 return VERR_PDM_CRITSECT_NOT_FOUND;
757}
758
759
760/**
761 * Deletes the read/write critical section.
762 *
763 * @returns VBox status code.
764 * @param pCritSect The PDM read/write critical section to destroy.
765 */
766VMMR3DECL(int) PDMR3CritSectRwDelete(PPDMCRITSECTRW pCritSect)
767{
768 if (!PDMCritSectRwIsInitialized(pCritSect))
769 return VINF_SUCCESS;
770
771 /*
772 * Find and unlink it.
773 */
774 PVM pVM = pCritSect->s.pVMR3;
775 PUVM pUVM = pVM->pUVM;
776 AssertReleaseReturn(pVM, VERR_PDM_CRITSECT_IPE);
777 PPDMCRITSECTRWINT pPrev = NULL;
778 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
779 PPDMCRITSECTRWINT pCur = pUVM->pdm.s.pRwCritSects;
780 while (pCur)
781 {
782 if (pCur == &pCritSect->s)
783 {
784 int rc = pdmR3CritSectRwDeleteOne(pVM, pUVM, pCur, pPrev, false /* not final */);
785 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
786 return rc;
787 }
788
789 /* next */
790 pPrev = pCur;
791 pCur = pCur->pNext;
792 }
793 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
794 AssertReleaseMsgFailed(("pCritSect=%p wasn't found!\n", pCritSect));
795 return VERR_PDM_CRITSECT_NOT_FOUND;
796}
797
798
799/**
800 * Gets the name of the critical section.
801 *
802 *
803 * @returns Pointer to the critical section name (read only) on success,
804 * NULL on failure (invalid critical section).
805 * @param pCritSect The critical section.
806 */
807VMMR3DECL(const char *) PDMR3CritSectName(PCPDMCRITSECT pCritSect)
808{
809 AssertPtrReturn(pCritSect, NULL);
810 AssertReturn(pCritSect->s.Core.u32Magic == RTCRITSECT_MAGIC, NULL);
811 return pCritSect->s.pszName;
812}
813
814
815/**
816 * Gets the name of the read/write 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 read/write critical section.
822 */
823VMMR3DECL(const char *) PDMR3CritSectRwName(PCPDMCRITSECTRW pCritSect)
824{
825 AssertPtrReturn(pCritSect, NULL);
826 AssertReturn(pCritSect->s.Core.u32Magic == RTCRITSECTRW_MAGIC, NULL);
827 return pCritSect->s.pszName;
828}
829
830
831/**
832 * Yield the critical section if someone is waiting on it.
833 *
834 * When yielding, we'll leave the critical section and try to make sure the
835 * other waiting threads get a chance of entering before we reclaim it.
836 *
837 * @retval true if yielded.
838 * @retval false if not yielded.
839 * @param pCritSect The critical section.
840 */
841VMMR3DECL(bool) PDMR3CritSectYield(PPDMCRITSECT pCritSect)
842{
843 AssertPtrReturn(pCritSect, false);
844 AssertReturn(pCritSect->s.Core.u32Magic == RTCRITSECT_MAGIC, false);
845 Assert(pCritSect->s.Core.NativeThreadOwner == RTThreadNativeSelf());
846 Assert(!(pCritSect->s.Core.fFlags & RTCRITSECT_FLAGS_NOP));
847
848 /* No recursion allowed here. */
849 int32_t const cNestings = pCritSect->s.Core.cNestings;
850 AssertReturn(cNestings == 1, false);
851
852 int32_t const cLockers = ASMAtomicReadS32(&pCritSect->s.Core.cLockers);
853 if (cLockers < cNestings)
854 return false;
855
856#ifdef PDMCRITSECT_STRICT
857 RTLOCKVALSRCPOS const SrcPos = pCritSect->s.Core.pValidatorRec->SrcPos;
858#endif
859 PDMCritSectLeave(pCritSect);
860
861 /*
862 * If we're lucky, then one of the waiters has entered the lock already.
863 * We spin a little bit in hope for this to happen so we can avoid the
864 * yield detour.
865 */
866 if (ASMAtomicUoReadS32(&pCritSect->s.Core.cNestings) == 0)
867 {
868 int cLoops = 20;
869 while ( cLoops > 0
870 && ASMAtomicUoReadS32(&pCritSect->s.Core.cNestings) == 0
871 && ASMAtomicUoReadS32(&pCritSect->s.Core.cLockers) >= 0)
872 {
873 ASMNopPause();
874 cLoops--;
875 }
876 if (cLoops == 0)
877 RTThreadYield();
878 }
879
880#ifdef PDMCRITSECT_STRICT
881 int rc = PDMCritSectEnterDebug(pCritSect, VERR_IGNORED,
882 SrcPos.uId, SrcPos.pszFile, SrcPos.uLine, SrcPos.pszFunction);
883#else
884 int rc = PDMCritSectEnter(pCritSect, VERR_IGNORED);
885#endif
886 AssertLogRelRC(rc);
887 return true;
888}
889
890
891/**
892 * PDMR3CritSectBothCountOwned worker.
893 *
894 * @param pszName The critical section name.
895 * @param ppszNames Pointer to the pszNames variable.
896 * @param pcchLeft Pointer to the cchLeft variable.
897 * @param fFirst Whether this is the first name or not.
898 */
899static void pdmR3CritSectAppendNameToList(char const *pszName, char **ppszNames, size_t *pcchLeft, bool fFirst)
900{
901 size_t cchLeft = *pcchLeft;
902 if (cchLeft)
903 {
904 char *pszNames = *ppszNames;
905
906 /* try add comma. */
907 if (fFirst)
908 {
909 *pszNames++ = ',';
910 if (--cchLeft)
911 {
912 *pszNames++ = ' ';
913 cchLeft--;
914 }
915 }
916
917 /* try copy the name. */
918 if (cchLeft)
919 {
920 size_t const cchName = strlen(pszName);
921 if (cchName < cchLeft)
922 {
923 memcpy(pszNames, pszName, cchName);
924 pszNames += cchName;
925 cchLeft -= cchName;
926 }
927 else
928 {
929 if (cchLeft > 2)
930 {
931 memcpy(pszNames, pszName, cchLeft - 2);
932 pszNames += cchLeft - 2;
933 cchLeft = 2;
934 }
935 while (cchLeft-- > 0)
936 *pszNames++ = '+';
937 }
938 }
939 *pszNames = '\0';
940
941 *pcchLeft = cchLeft;
942 *ppszNames = pszNames;
943 }
944}
945
946
947/**
948 * Counts the critical sections (both type) owned by the calling thread,
949 * optionally returning a comma separated list naming them.
950 *
951 * Read ownerships are not included in non-strict builds.
952 *
953 * This is for diagnostic purposes only.
954 *
955 * @returns Lock count.
956 *
957 * @param pVM Pointer to the VM.
958 * @param pszNames Where to return the critical section names.
959 * @param cbNames The size of the buffer.
960 */
961VMMR3DECL(uint32_t) PDMR3CritSectCountOwned(PVM pVM, char *pszNames, size_t cbNames)
962{
963 /*
964 * Init the name buffer.
965 */
966 size_t cchLeft = cbNames;
967 if (cchLeft)
968 {
969 cchLeft--;
970 pszNames[0] = pszNames[cchLeft] = '\0';
971 }
972
973 /*
974 * Iterate the critical sections.
975 */
976 uint32_t cCritSects = 0;
977 RTNATIVETHREAD const hNativeThread = RTThreadNativeSelf();
978 /* This is unsafe, but wtf. */
979 for (PPDMCRITSECTINT pCur = pVM->pUVM->pdm.s.pCritSects;
980 pCur;
981 pCur = pCur->pNext)
982 {
983 /* Same as RTCritSectIsOwner(). */
984 if (pCur->Core.NativeThreadOwner == hNativeThread)
985 {
986 cCritSects++;
987 pdmR3CritSectAppendNameToList(pCur->pszName, &pszNames, &cchLeft, cCritSects == 1);
988 }
989 }
990
991 /* This is unsafe, but wtf. */
992 for (PPDMCRITSECTRWINT pCur = pVM->pUVM->pdm.s.pRwCritSects;
993 pCur;
994 pCur = pCur->pNext)
995 {
996 if ( pCur->Core.hNativeWriter == hNativeThread
997 || PDMCritSectRwIsReadOwner((PPDMCRITSECTRW)pCur, false /*fWannaHear*/) )
998 {
999 cCritSects++;
1000 pdmR3CritSectAppendNameToList(pCur->pszName, &pszNames, &cchLeft, cCritSects == 1);
1001 }
1002 }
1003
1004 return cCritSects;
1005}
1006
1007
1008/**
1009 * Leave all critical sections the calling thread owns.
1010 *
1011 * This is only used when entering guru meditation in order to prevent other
1012 * EMTs and I/O threads from deadlocking.
1013 *
1014 * @param pVM Pointer to the VM.
1015 */
1016VMMR3_INT_DECL(void) PDMR3CritSectLeaveAll(PVM pVM)
1017{
1018 RTNATIVETHREAD const hNativeSelf = RTThreadNativeSelf();
1019 PUVM pUVM = pVM->pUVM;
1020
1021 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
1022 for (PPDMCRITSECTINT pCur = pUVM->pdm.s.pCritSects;
1023 pCur;
1024 pCur = pCur->pNext)
1025 {
1026 while ( pCur->Core.NativeThreadOwner == hNativeSelf
1027 && pCur->Core.cNestings > 0)
1028 PDMCritSectLeave((PPDMCRITSECT)pCur);
1029 }
1030 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
1031}
1032
1033
1034/**
1035 * Gets the address of the NOP critical section.
1036 *
1037 * The NOP critical section will not perform any thread serialization but let
1038 * all enter immediately and concurrently.
1039 *
1040 * @returns The address of the NOP critical section.
1041 * @param pVM Pointer to the VM.
1042 */
1043VMMR3DECL(PPDMCRITSECT) PDMR3CritSectGetNop(PVM pVM)
1044{
1045 VM_ASSERT_VALID_EXT_RETURN(pVM, NULL);
1046 return &pVM->pdm.s.NopCritSect;
1047}
1048
1049
1050/**
1051 * Gets the ring-0 address of the NOP critical section.
1052 *
1053 * @returns The ring-0 address of the NOP critical section.
1054 * @param pVM Pointer to the VM.
1055 */
1056VMMR3DECL(R0PTRTYPE(PPDMCRITSECT)) PDMR3CritSectGetNopR0(PVM pVM)
1057{
1058 VM_ASSERT_VALID_EXT_RETURN(pVM, NIL_RTR0PTR);
1059 return MMHyperR3ToR0(pVM, &pVM->pdm.s.NopCritSect);
1060}
1061
1062
1063/**
1064 * Gets the raw-mode context address of the NOP critical section.
1065 *
1066 * @returns The raw-mode context address of the NOP critical section.
1067 * @param pVM Pointer to the VM.
1068 */
1069VMMR3DECL(RCPTRTYPE(PPDMCRITSECT)) PDMR3CritSectGetNopRC(PVM pVM)
1070{
1071 VM_ASSERT_VALID_EXT_RETURN(pVM, NIL_RTRCPTR);
1072 return MMHyperR3ToRC(pVM, &pVM->pdm.s.NopCritSect);
1073}
1074
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