VirtualBox

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

Last change on this file since 87122 was 87122, checked in by vboxsync, 3 years ago

VMM/PDM: Create unique lock validator classes for each device instance (untested) so it's possible to lock two devices at once. bugref:9888

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

© 2023 Oracle
ContactPrivacy policyTerms of Use