VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/PDMAllCritSect.cpp@ 90380

最後變更 在這個檔案從90380是 90379,由 vboxsync 提交於 4 年 前

VMM: Implementing blocking on critical sections in ring-0 HM context (actual code is disabled). bugref:6695

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id Revision
檔案大小: 30.0 KB
 
1/* $Id: PDMAllCritSect.cpp 90379 2021-07-28 20:00:43Z vboxsync $ */
2/** @file
3 * PDM - Write-Only Critical Section, All Contexts.
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.alldomusa.eu.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/mm.h>
26#include <VBox/vmm/vmm.h>
27#include <VBox/vmm/vmcc.h>
28#include <VBox/err.h>
29#include <VBox/vmm/hm.h>
30
31#include <VBox/log.h>
32#include <iprt/asm.h>
33#include <iprt/asm-amd64-x86.h>
34#include <iprt/assert.h>
35#ifdef IN_RING3
36# include <iprt/lockvalidator.h>
37# include <iprt/semaphore.h>
38#endif
39#if defined(IN_RING3) || defined(IN_RING0)
40# include <iprt/thread.h>
41#endif
42
43
44/*********************************************************************************************************************************
45* Defined Constants And Macros *
46*********************************************************************************************************************************/
47/** The number loops to spin for in ring-3. */
48#define PDMCRITSECT_SPIN_COUNT_R3 20
49/** The number loops to spin for in ring-0. */
50#define PDMCRITSECT_SPIN_COUNT_R0 256
51/** The number loops to spin for in the raw-mode context. */
52#define PDMCRITSECT_SPIN_COUNT_RC 256
53
54
55/** Skips some of the overly paranoid atomic updates.
56 * Makes some assumptions about cache coherence, though not brave enough not to
57 * always end with an atomic update. */
58#define PDMCRITSECT_WITH_LESS_ATOMIC_STUFF
59
60/* Undefine the automatic VBOX_STRICT API mappings. */
61#undef PDMCritSectEnter
62#undef PDMCritSectTryEnter
63
64
65/**
66 * Gets the ring-3 native thread handle of the calling thread.
67 *
68 * @returns native thread handle (ring-3).
69 * @param pVM The cross context VM structure.
70 * @param pCritSect The critical section. This is used in R0 and RC.
71 */
72DECL_FORCE_INLINE(RTNATIVETHREAD) pdmCritSectGetNativeSelf(PVMCC pVM, PCPDMCRITSECT pCritSect)
73{
74#ifdef IN_RING3
75 RT_NOREF(pVM, pCritSect);
76 RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
77#else
78 AssertMsgReturn(pCritSect->s.Core.u32Magic == RTCRITSECT_MAGIC, ("%RX32\n", pCritSect->s.Core.u32Magic),
79 NIL_RTNATIVETHREAD);
80 PVMCPUCC pVCpu = VMMGetCpu(pVM); AssertPtr(pVCpu);
81 RTNATIVETHREAD hNativeSelf = pVCpu ? pVCpu->hNativeThread : NIL_RTNATIVETHREAD; Assert(hNativeSelf != NIL_RTNATIVETHREAD);
82#endif
83 return hNativeSelf;
84}
85
86
87/**
88 * Tail code called when we've won the battle for the lock.
89 *
90 * @returns VINF_SUCCESS.
91 *
92 * @param pCritSect The critical section.
93 * @param hNativeSelf The native handle of this thread.
94 * @param pSrcPos The source position of the lock operation.
95 */
96DECL_FORCE_INLINE(int) pdmCritSectEnterFirst(PPDMCRITSECT pCritSect, RTNATIVETHREAD hNativeSelf, PCRTLOCKVALSRCPOS pSrcPos)
97{
98 AssertMsg(pCritSect->s.Core.NativeThreadOwner == NIL_RTNATIVETHREAD, ("NativeThreadOwner=%p\n", pCritSect->s.Core.NativeThreadOwner));
99 Assert(!(pCritSect->s.Core.fFlags & PDMCRITSECT_FLAGS_PENDING_UNLOCK));
100
101# ifdef PDMCRITSECT_WITH_LESS_ATOMIC_STUFF
102 pCritSect->s.Core.cNestings = 1;
103# else
104 ASMAtomicWriteS32(&pCritSect->s.Core.cNestings, 1);
105# endif
106 Assert(pCritSect->s.Core.cNestings == 1);
107 ASMAtomicWriteHandle(&pCritSect->s.Core.NativeThreadOwner, hNativeSelf);
108
109# ifdef PDMCRITSECT_STRICT
110 RTLockValidatorRecExclSetOwner(pCritSect->s.Core.pValidatorRec, NIL_RTTHREAD, pSrcPos, true);
111# else
112 NOREF(pSrcPos);
113# endif
114
115 STAM_PROFILE_ADV_START(&pCritSect->s.StatLocked, l);
116 return VINF_SUCCESS;
117}
118
119
120#if defined(IN_RING3) || defined(IN_RING0)
121/**
122 * Deals with the contended case in ring-3 and ring-0.
123 *
124 * @retval VINF_SUCCESS on success.
125 * @retval VERR_SEM_DESTROYED if destroyed.
126 *
127 * @param pVM The cross context VM structure.
128 * @param pCritSect The critsect.
129 * @param hNativeSelf The native thread handle.
130 * @param pSrcPos The source position of the lock operation.
131 */
132static int pdmR3R0CritSectEnterContended(PVMCC pVM, PPDMCRITSECT pCritSect, RTNATIVETHREAD hNativeSelf, PCRTLOCKVALSRCPOS pSrcPos)
133{
134 /*
135 * Start waiting.
136 */
137 if (ASMAtomicIncS32(&pCritSect->s.Core.cLockers) == 0)
138 return pdmCritSectEnterFirst(pCritSect, hNativeSelf, pSrcPos);
139# ifdef IN_RING3
140 STAM_REL_COUNTER_INC(&pCritSect->s.StatContentionR3);
141# else
142 STAM_REL_COUNTER_INC(&pCritSect->s.StatContentionRZLock);
143# endif
144
145 /*
146 * The wait loop.
147 */
148 PSUPDRVSESSION pSession = pVM->pSession;
149 SUPSEMEVENT hEvent = (SUPSEMEVENT)pCritSect->s.Core.EventSem;
150# ifdef IN_RING3
151# ifdef PDMCRITSECT_STRICT
152 RTTHREAD hThreadSelf = RTThreadSelfAutoAdopt();
153 int rc2 = RTLockValidatorRecExclCheckOrder(pCritSect->s.Core.pValidatorRec, hThreadSelf, pSrcPos, RT_INDEFINITE_WAIT);
154 if (RT_FAILURE(rc2))
155 return rc2;
156# else
157 RTTHREAD hThreadSelf = RTThreadSelf();
158# endif
159# endif
160 for (;;)
161 {
162 /*
163 * Do the wait.
164 *
165 * In ring-3 this gets cluttered by lock validation and thread state
166 * maintainence.
167 *
168 * In ring-0 we have to deal with the possibility that the thread has
169 * been signalled and the interruptible wait function returning
170 * immediately. In that case we do normal R0/RC rcBusy handling.
171 */
172# ifdef IN_RING3
173# ifdef PDMCRITSECT_STRICT
174 int rc9 = RTLockValidatorRecExclCheckBlocking(pCritSect->s.Core.pValidatorRec, hThreadSelf, pSrcPos,
175 !(pCritSect->s.Core.fFlags & RTCRITSECT_FLAGS_NO_NESTING),
176 RT_INDEFINITE_WAIT, RTTHREADSTATE_CRITSECT, true);
177 if (RT_FAILURE(rc9))
178 return rc9;
179# else
180 RTThreadBlocking(hThreadSelf, RTTHREADSTATE_CRITSECT, true);
181# endif
182 int rc = SUPSemEventWaitNoResume(pSession, hEvent, RT_INDEFINITE_WAIT);
183 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_CRITSECT);
184# else /* IN_RING0 */
185 int rc = SUPSemEventWaitNoResume(pSession, hEvent, RT_INDEFINITE_WAIT);
186# endif /* IN_RING0 */
187
188 /*
189 * Deal with the return code and critsect destruction.
190 */
191 if (RT_UNLIKELY(pCritSect->s.Core.u32Magic != RTCRITSECT_MAGIC))
192 return VERR_SEM_DESTROYED;
193 if (rc == VINF_SUCCESS)
194 return pdmCritSectEnterFirst(pCritSect, hNativeSelf, pSrcPos);
195 AssertMsg(rc == VERR_INTERRUPTED, ("rc=%Rrc\n", rc));
196
197# ifdef IN_RING0
198 /* Something is pending (signal, APC, debugger, whatever), just go back
199 to ring-3 so the kernel can deal with it when leaving kernel context.
200
201 Note! We've incremented cLockers already and cannot safely decrement
202 it without creating a race with PDMCritSectLeave, resulting in
203 spurious wakeups. */
204 PVMCPUCC pVCpu = VMMGetCpu(pVM); AssertPtr(pVCpu);
205 rc = VMMRZCallRing3(pVM, pVCpu, VMMCALLRING3_VM_R0_PREEMPT, NULL);
206 AssertRC(rc);
207# else
208 RT_NOREF(pVM);
209# endif
210 }
211 /* won't get here */
212}
213#endif /* IN_RING3 || IN_RING0 */
214
215
216/**
217 * Common worker for the debug and normal APIs.
218 *
219 * @returns VINF_SUCCESS if entered successfully.
220 * @returns rcBusy when encountering a busy critical section in GC/R0.
221 * @retval VERR_SEM_DESTROYED if the critical section is delete before or
222 * during the operation.
223 *
224 * @param pVM The cross context VM structure.
225 * @param pCritSect The PDM critical section to enter.
226 * @param rcBusy The status code to return when we're in GC or R0
227 * @param pSrcPos The source position of the lock operation.
228 */
229DECL_FORCE_INLINE(int) pdmCritSectEnter(PVMCC pVM, PPDMCRITSECT pCritSect, int rcBusy, PCRTLOCKVALSRCPOS pSrcPos)
230{
231 Assert(pCritSect->s.Core.cNestings < 8); /* useful to catch incorrect locking */
232 Assert(pCritSect->s.Core.cNestings >= 0);
233
234 /*
235 * If the critical section has already been destroyed, then inform the caller.
236 */
237 AssertMsgReturn(pCritSect->s.Core.u32Magic == RTCRITSECT_MAGIC,
238 ("%p %RX32\n", pCritSect, pCritSect->s.Core.u32Magic),
239 VERR_SEM_DESTROYED);
240
241 /*
242 * See if we're lucky.
243 */
244 /* NOP ... */
245 if (!(pCritSect->s.Core.fFlags & RTCRITSECT_FLAGS_NOP))
246 { /* We're more likely to end up here with real critsects than a NOP one. */ }
247 else
248 return VINF_SUCCESS;
249
250 RTNATIVETHREAD hNativeSelf = pdmCritSectGetNativeSelf(pVM, pCritSect);
251 AssertReturn(hNativeSelf != NIL_RTNATIVETHREAD, VERR_VM_THREAD_NOT_EMT);
252 /* ... not owned ... */
253 if (ASMAtomicCmpXchgS32(&pCritSect->s.Core.cLockers, 0, -1))
254 return pdmCritSectEnterFirst(pCritSect, hNativeSelf, pSrcPos);
255
256 /* ... or nested. */
257 if (pCritSect->s.Core.NativeThreadOwner == hNativeSelf)
258 {
259 Assert(pCritSect->s.Core.cNestings >= 1);
260# ifdef PDMCRITSECT_WITH_LESS_ATOMIC_STUFF
261 pCritSect->s.Core.cNestings += 1;
262# else
263 ASMAtomicIncS32(&pCritSect->s.Core.cNestings);
264# endif
265 ASMAtomicIncS32(&pCritSect->s.Core.cLockers);
266 return VINF_SUCCESS;
267 }
268
269 /*
270 * Spin for a bit without incrementing the counter.
271 */
272 /** @todo Move this to cfgm variables since it doesn't make sense to spin on UNI
273 * cpu systems. */
274 int32_t cSpinsLeft = CTX_SUFF(PDMCRITSECT_SPIN_COUNT_);
275 while (cSpinsLeft-- > 0)
276 {
277 if (ASMAtomicCmpXchgS32(&pCritSect->s.Core.cLockers, 0, -1))
278 return pdmCritSectEnterFirst(pCritSect, hNativeSelf, pSrcPos);
279 ASMNopPause();
280 /** @todo Should use monitor/mwait on e.g. &cLockers here, possibly with a
281 cli'ed pendingpreemption check up front using sti w/ instruction fusing
282 for avoiding races. Hmm ... This is assuming the other party is actually
283 executing code on another CPU ... which we could keep track of if we
284 wanted. */
285 }
286
287#ifdef IN_RING3
288 /*
289 * Take the slow path.
290 */
291 NOREF(rcBusy);
292 return pdmR3R0CritSectEnterContended(pVM, pCritSect, hNativeSelf, pSrcPos);
293
294#elif defined(IN_RING0)
295# if 0 /* new code */
296 /*
297 * In ring-0 context we have to take the special VT-x/AMD-V HM context into
298 * account when waiting on contended locks.
299 *
300 * While we usually (it can be VINF_SUCCESS) have to option via the rcBusy
301 * parameter of going to back to ring-3 and to re-start the work there, it's
302 * almost always more efficient to try wait for the lock here. The rcBusy
303 * will be used if we encounter an VERR_INTERRUPTED situation though.
304 *
305 * We must never block if VMMRZCallRing3Disable is active.
306 */
307 PVMCPUCC pVCpu = VMMGetCpu(pVM);
308 if (pVCpu)
309 {
310 VMMR0EMTBLOCKCTX Ctx;
311 int rc = VMMR0EmtPrepareToBlock(pVCpu, rcBusy, __FUNCTION__, pCritSect, &Ctx);
312 if (rc == VINF_SUCCESS)
313 {
314 Assert(RTThreadPreemptIsEnabled(NIL_RTTHREAD));
315
316 rc = pdmR3R0CritSectEnterContended(pVM, pCritSect, hNativeSelf, pSrcPos);
317
318 VMMR0EmtResumeAfterBlocking(pVCpu, &Ctx);
319 }
320 else
321 STAM_REL_COUNTER_INC(&pCritSect->s.StatContentionRZLockBusy);
322 return rc;
323 }
324
325 /* Non-EMT. */
326 Assert(RTThreadPreemptIsEnabled(NIL_RTTHREAD));
327 return pdmR3R0CritSectEnterContended(pVM, pCritSect, hNativeSelf, pSrcPos);
328
329# else /* old code: */
330 /*
331 * We preemption hasn't been disabled, we can block here in ring-0.
332 */
333 if ( RTThreadPreemptIsEnabled(NIL_RTTHREAD)
334 && ASMIntAreEnabled())
335 return pdmR3R0CritSectEnterContended(pVM, pCritSect, hNativeSelf, pSrcPos);
336
337 STAM_REL_COUNTER_INC(&pCritSect->s.StatContentionRZLock);
338
339 /*
340 * Call ring-3 to acquire the critical section?
341 */
342 if (rcBusy == VINF_SUCCESS)
343 {
344 PVMCPUCC pVCpu = VMMGetCpu(pVM);
345 AssertReturn(pVCpu, VERR_PDM_CRITSECT_IPE);
346 return VMMRZCallRing3(pVM, pVCpu, VMMCALLRING3_PDM_CRIT_SECT_ENTER, MMHyperCCToR3(pVM, pCritSect));
347 }
348
349 /*
350 * Return busy.
351 */
352 LogFlow(("PDMCritSectEnter: locked => R3 (%Rrc)\n", rcBusy));
353 return rcBusy;
354# endif /* old code */
355#else
356# error "Unsupported context"
357#endif
358}
359
360
361/**
362 * Enters a PDM critical section.
363 *
364 * @returns VINF_SUCCESS if entered successfully.
365 * @returns rcBusy when encountering a busy critical section in RC/R0.
366 * @retval VERR_SEM_DESTROYED if the critical section is delete before or
367 * during the operation.
368 *
369 * @param pVM The cross context VM structure.
370 * @param pCritSect The PDM critical section to enter.
371 * @param rcBusy The status code to return when we're in RC or R0
372 * and the section is busy. Pass VINF_SUCCESS to
373 * acquired the critical section thru a ring-3
374 * call if necessary.
375 */
376VMMDECL(int) PDMCritSectEnter(PVMCC pVM, PPDMCRITSECT pCritSect, int rcBusy)
377{
378#ifndef PDMCRITSECT_STRICT
379 return pdmCritSectEnter(pVM, pCritSect, rcBusy, NULL);
380#else
381 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
382 return pdmCritSectEnter(pVM, pCritSect, rcBusy, &SrcPos);
383#endif
384}
385
386
387/**
388 * Enters a PDM critical section, with location information for debugging.
389 *
390 * @returns VINF_SUCCESS if entered successfully.
391 * @returns rcBusy when encountering a busy critical section in RC/R0.
392 * @retval VERR_SEM_DESTROYED if the critical section is delete before or
393 * during the operation.
394 *
395 * @param pVM The cross context VM structure.
396 * @param pCritSect The PDM critical section to enter.
397 * @param rcBusy The status code to return when we're in RC or R0
398 * and the section is busy. Pass VINF_SUCCESS to
399 * acquired the critical section thru a ring-3
400 * call if necessary.
401 * @param uId Some kind of locking location ID. Typically a
402 * return address up the stack. Optional (0).
403 * @param SRC_POS The source position where to lock is being
404 * acquired from. Optional.
405 */
406VMMDECL(int) PDMCritSectEnterDebug(PVMCC pVM, PPDMCRITSECT pCritSect, int rcBusy, RTHCUINTPTR uId, RT_SRC_POS_DECL)
407{
408#ifdef PDMCRITSECT_STRICT
409 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
410 return pdmCritSectEnter(pVM, pCritSect, rcBusy, &SrcPos);
411#else
412 NOREF(uId); RT_SRC_POS_NOREF();
413 return pdmCritSectEnter(pVM, pCritSect, rcBusy, NULL);
414#endif
415}
416
417
418/**
419 * Common worker for the debug and normal APIs.
420 *
421 * @retval VINF_SUCCESS on success.
422 * @retval VERR_SEM_BUSY if the critsect was owned.
423 * @retval VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
424 * @retval VERR_SEM_DESTROYED if the critical section is delete before or
425 * during the operation.
426 *
427 * @param pVM The cross context VM structure.
428 * @param pCritSect The critical section.
429 * @param pSrcPos The source position of the lock operation.
430 */
431static int pdmCritSectTryEnter(PVMCC pVM, PPDMCRITSECT pCritSect, PCRTLOCKVALSRCPOS pSrcPos)
432{
433 /*
434 * If the critical section has already been destroyed, then inform the caller.
435 */
436 AssertMsgReturn(pCritSect->s.Core.u32Magic == RTCRITSECT_MAGIC,
437 ("%p %RX32\n", pCritSect, pCritSect->s.Core.u32Magic),
438 VERR_SEM_DESTROYED);
439
440 /*
441 * See if we're lucky.
442 */
443 /* NOP ... */
444 if (!(pCritSect->s.Core.fFlags & RTCRITSECT_FLAGS_NOP))
445 { /* We're more likely to end up here with real critsects than a NOP one. */ }
446 else
447 return VINF_SUCCESS;
448
449 RTNATIVETHREAD hNativeSelf = pdmCritSectGetNativeSelf(pVM, pCritSect);
450 AssertReturn(hNativeSelf != NIL_RTNATIVETHREAD, VERR_VM_THREAD_NOT_EMT);
451 /* ... not owned ... */
452 if (ASMAtomicCmpXchgS32(&pCritSect->s.Core.cLockers, 0, -1))
453 return pdmCritSectEnterFirst(pCritSect, hNativeSelf, pSrcPos);
454
455 /* ... or nested. */
456 if (pCritSect->s.Core.NativeThreadOwner == hNativeSelf)
457 {
458 Assert(pCritSect->s.Core.cNestings >= 1);
459# ifdef PDMCRITSECT_WITH_LESS_ATOMIC_STUFF
460 pCritSect->s.Core.cNestings += 1;
461# else
462 ASMAtomicIncS32(&pCritSect->s.Core.cNestings);
463# endif
464 ASMAtomicIncS32(&pCritSect->s.Core.cLockers);
465 return VINF_SUCCESS;
466 }
467
468 /* no spinning */
469
470 /*
471 * Return busy.
472 */
473#ifdef IN_RING3
474 STAM_REL_COUNTER_INC(&pCritSect->s.StatContentionR3);
475#else
476 STAM_REL_COUNTER_INC(&pCritSect->s.StatContentionRZLockBusy);
477#endif
478 LogFlow(("PDMCritSectTryEnter: locked\n"));
479 return VERR_SEM_BUSY;
480}
481
482
483/**
484 * Try enter a critical section.
485 *
486 * @retval VINF_SUCCESS on success.
487 * @retval VERR_SEM_BUSY if the critsect was owned.
488 * @retval VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
489 * @retval VERR_SEM_DESTROYED if the critical section is delete before or
490 * during the operation.
491 *
492 * @param pVM The cross context VM structure.
493 * @param pCritSect The critical section.
494 */
495VMMDECL(int) PDMCritSectTryEnter(PVMCC pVM, PPDMCRITSECT pCritSect)
496{
497#ifndef PDMCRITSECT_STRICT
498 return pdmCritSectTryEnter(pVM, pCritSect, NULL);
499#else
500 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
501 return pdmCritSectTryEnter(pVM, pCritSect, &SrcPos);
502#endif
503}
504
505
506/**
507 * Try enter a critical section, with location information for debugging.
508 *
509 * @retval VINF_SUCCESS on success.
510 * @retval VERR_SEM_BUSY if the critsect was owned.
511 * @retval VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
512 * @retval VERR_SEM_DESTROYED if the critical section is delete before or
513 * during the operation.
514 *
515 * @param pVM The cross context VM structure.
516 * @param pCritSect The critical section.
517 * @param uId Some kind of locking location ID. Typically a
518 * return address up the stack. Optional (0).
519 * @param SRC_POS The source position where to lock is being
520 * acquired from. Optional.
521 */
522VMMDECL(int) PDMCritSectTryEnterDebug(PVMCC pVM, PPDMCRITSECT pCritSect, RTHCUINTPTR uId, RT_SRC_POS_DECL)
523{
524#ifdef PDMCRITSECT_STRICT
525 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
526 return pdmCritSectTryEnter(pVM, pCritSect, &SrcPos);
527#else
528 NOREF(uId); RT_SRC_POS_NOREF();
529 return pdmCritSectTryEnter(pVM, pCritSect, NULL);
530#endif
531}
532
533
534#ifdef IN_RING3
535/**
536 * Enters a PDM critical section.
537 *
538 * @returns VINF_SUCCESS if entered successfully.
539 * @returns rcBusy when encountering a busy critical section in GC/R0.
540 * @retval VERR_SEM_DESTROYED if the critical section is delete before or
541 * during the operation.
542 *
543 * @param pVM The cross context VM structure.
544 * @param pCritSect The PDM critical section to enter.
545 * @param fCallRing3 Whether this is a VMMRZCallRing3()request.
546 */
547VMMR3DECL(int) PDMR3CritSectEnterEx(PVM pVM, PPDMCRITSECT pCritSect, bool fCallRing3)
548{
549 int rc = PDMCritSectEnter(pVM, pCritSect, VERR_IGNORED);
550 if ( rc == VINF_SUCCESS
551 && fCallRing3
552 && pCritSect->s.Core.pValidatorRec
553 && pCritSect->s.Core.pValidatorRec->hThread != NIL_RTTHREAD)
554 RTLockValidatorRecExclReleaseOwnerUnchecked(pCritSect->s.Core.pValidatorRec);
555 return rc;
556}
557#endif /* IN_RING3 */
558
559
560/**
561 * Leaves a critical section entered with PDMCritSectEnter().
562 *
563 * @returns Indication whether we really exited the critical section.
564 * @retval VINF_SUCCESS if we really exited.
565 * @retval VINF_SEM_NESTED if we only reduced the nesting count.
566 * @retval VERR_NOT_OWNER if you somehow ignore release assertions.
567 *
568 * @param pVM The cross context VM structure.
569 * @param pCritSect The PDM critical section to leave.
570 */
571VMMDECL(int) PDMCritSectLeave(PVMCC pVM, PPDMCRITSECT pCritSect)
572{
573 AssertMsg(pCritSect->s.Core.u32Magic == RTCRITSECT_MAGIC, ("%p %RX32\n", pCritSect, pCritSect->s.Core.u32Magic));
574 Assert(pCritSect->s.Core.u32Magic == RTCRITSECT_MAGIC);
575
576 /* Check for NOP sections before asserting ownership. */
577 if (!(pCritSect->s.Core.fFlags & RTCRITSECT_FLAGS_NOP))
578 { /* We're more likely to end up here with real critsects than a NOP one. */ }
579 else
580 return VINF_SUCCESS;
581
582 /*
583 * Always check that the caller is the owner (screw performance).
584 */
585 RTNATIVETHREAD const hNativeSelf = pdmCritSectGetNativeSelf(pVM, pCritSect);
586 AssertReleaseMsgReturn(pCritSect->s.Core.NativeThreadOwner == hNativeSelf || hNativeSelf == NIL_RTNATIVETHREAD,
587 ("%p %s: %p != %p; cLockers=%d cNestings=%d\n", pCritSect, R3STRING(pCritSect->s.pszName),
588 pCritSect->s.Core.NativeThreadOwner, hNativeSelf,
589 pCritSect->s.Core.cLockers, pCritSect->s.Core.cNestings),
590 VERR_NOT_OWNER);
591
592 /*
593 * Nested leave.
594 */
595 int32_t const cNestings = pCritSect->s.Core.cNestings;
596 Assert(cNestings >= 1);
597 if (cNestings > 1)
598 {
599# ifdef PDMCRITSECT_WITH_LESS_ATOMIC_STUFF
600 pCritSect->s.Core.cNestings = cNestings - 1;
601# else
602 ASMAtomicWriteS32(&pCritSect->s.Core.cNestings, cNestings - 1);
603# endif
604 ASMAtomicDecS32(&pCritSect->s.Core.cLockers);
605 Assert(pCritSect->s.Core.cLockers >= 0);
606 return VINF_SEM_NESTED;
607 }
608
609#ifdef IN_RING0
610# if 0 /** @todo Make SUPSemEventSignal interrupt safe (handle table++) and enable this for: defined(RT_OS_LINUX) || defined(RT_OS_OS2) */
611 if (1) /* SUPSemEventSignal is safe */
612# else
613 if (ASMIntAreEnabled())
614# endif
615#endif
616#if defined(IN_RING3) || defined(IN_RING0)
617 {
618 /*
619 * Leave for real.
620 */
621 /* update members. */
622 SUPSEMEVENT hEventToSignal = pCritSect->s.hEventToSignal;
623 pCritSect->s.hEventToSignal = NIL_SUPSEMEVENT;
624# ifdef IN_RING3
625# if defined(PDMCRITSECT_STRICT)
626 if (pCritSect->s.Core.pValidatorRec->hThread != NIL_RTTHREAD)
627 RTLockValidatorRecExclReleaseOwnerUnchecked(pCritSect->s.Core.pValidatorRec);
628# endif
629 Assert(!pCritSect->s.Core.pValidatorRec || pCritSect->s.Core.pValidatorRec->hThread == NIL_RTTHREAD);
630# endif
631# ifdef PDMCRITSECT_WITH_LESS_ATOMIC_STUFF
632 //pCritSect->s.Core.cNestings = 0; /* not really needed */
633 pCritSect->s.Core.NativeThreadOwner = NIL_RTNATIVETHREAD;
634# else
635 ASMAtomicWriteS32(&pCritSect->s.Core.cNestings, 0);
636 ASMAtomicWriteHandle(&pCritSect->s.Core.NativeThreadOwner, NIL_RTNATIVETHREAD);
637# endif
638 ASMAtomicAndU32(&pCritSect->s.Core.fFlags, ~PDMCRITSECT_FLAGS_PENDING_UNLOCK);
639
640 /* stop and decrement lockers. */
641 STAM_PROFILE_ADV_STOP(&pCritSect->s.StatLocked, l);
642 ASMCompilerBarrier();
643 if (ASMAtomicDecS32(&pCritSect->s.Core.cLockers) < 0)
644 { /* hopefully likely */ }
645 else
646 {
647 /* Someone is waiting, wake up one of them. */
648 SUPSEMEVENT hEvent = (SUPSEMEVENT)pCritSect->s.Core.EventSem;
649 PSUPDRVSESSION pSession = pVM->pSession;
650 int rc = SUPSemEventSignal(pSession, hEvent);
651 AssertRC(rc);
652 }
653
654 /* Signal exit event. */
655 if (RT_LIKELY(hEventToSignal == NIL_SUPSEMEVENT))
656 { /* likely */ }
657 else
658 {
659 Log8(("Signalling %#p\n", hEventToSignal));
660 int rc = SUPSemEventSignal(pVM->pSession, hEventToSignal);
661 AssertRC(rc);
662 }
663
664# if defined(DEBUG_bird) && defined(IN_RING0)
665 VMMTrashVolatileXMMRegs();
666# endif
667 }
668#endif /* IN_RING3 || IN_RING0 */
669#ifdef IN_RING0
670 else
671#endif
672#if defined(IN_RING0) || defined(IN_RC)
673 {
674 /*
675 * Try leave it.
676 */
677 if (pCritSect->s.Core.cLockers == 0)
678 {
679# ifdef PDMCRITSECT_WITH_LESS_ATOMIC_STUFF
680 //pCritSect->s.Core.cNestings = 0; /* not really needed */
681# else
682 ASMAtomicWriteS32(&pCritSect->s.Core.cNestings, 0);
683# endif
684 RTNATIVETHREAD hNativeThread = pCritSect->s.Core.NativeThreadOwner;
685 ASMAtomicAndU32(&pCritSect->s.Core.fFlags, ~PDMCRITSECT_FLAGS_PENDING_UNLOCK);
686 STAM_PROFILE_ADV_STOP(&pCritSect->s.StatLocked, l);
687
688 ASMAtomicWriteHandle(&pCritSect->s.Core.NativeThreadOwner, NIL_RTNATIVETHREAD);
689 if (ASMAtomicCmpXchgS32(&pCritSect->s.Core.cLockers, -1, 0))
690 return VINF_SUCCESS;
691
692 /* darn, someone raced in on us. */
693 ASMAtomicWriteHandle(&pCritSect->s.Core.NativeThreadOwner, hNativeThread);
694 STAM_PROFILE_ADV_START(&pCritSect->s.StatLocked, l);
695# ifdef PDMCRITSECT_WITH_LESS_ATOMIC_STUFF
696 //pCritSect->s.Core.cNestings = 1;
697 Assert(pCritSect->s.Core.cNestings == 1);
698# else
699 //Assert(pCritSect->s.Core.cNestings == 0);
700 ASMAtomicWriteS32(&pCritSect->s.Core.cNestings, 1);
701# endif
702 }
703 ASMAtomicOrU32(&pCritSect->s.Core.fFlags, PDMCRITSECT_FLAGS_PENDING_UNLOCK);
704
705 /*
706 * Queue the request.
707 */
708 PVMCPUCC pVCpu = VMMGetCpu(pVM); AssertPtr(pVCpu);
709 uint32_t i = pVCpu->pdm.s.cQueuedCritSectLeaves++;
710 LogFlow(("PDMCritSectLeave: [%d]=%p => R3\n", i, pCritSect));
711 AssertFatal(i < RT_ELEMENTS(pVCpu->pdm.s.apQueuedCritSectLeaves));
712 pVCpu->pdm.s.apQueuedCritSectLeaves[i] = MMHyperCCToR3(pVM, pCritSect);
713 VMCPU_FF_SET(pVCpu, VMCPU_FF_PDM_CRITSECT);
714 VMCPU_FF_SET(pVCpu, VMCPU_FF_TO_R3);
715 STAM_REL_COUNTER_INC(&pVM->pdm.s.StatQueuedCritSectLeaves);
716 STAM_REL_COUNTER_INC(&pCritSect->s.StatContentionRZUnlock);
717 }
718#endif /* IN_RING0 || IN_RC */
719
720 return VINF_SUCCESS;
721}
722
723
724#if defined(IN_RING0) || defined(IN_RING3)
725/**
726 * Schedule a event semaphore for signalling upon critsect exit.
727 *
728 * @returns VINF_SUCCESS on success.
729 * @returns VERR_TOO_MANY_SEMAPHORES if an event was already scheduled.
730 * @returns VERR_NOT_OWNER if we're not the critsect owner (ring-3 only).
731 * @returns VERR_SEM_DESTROYED if RTCritSectDelete was called while waiting.
732 *
733 * @param pCritSect The critical section.
734 * @param hEventToSignal The support driver event semaphore that should be
735 * signalled.
736 */
737VMMDECL(int) PDMHCCritSectScheduleExitEvent(PPDMCRITSECT pCritSect, SUPSEMEVENT hEventToSignal)
738{
739 AssertPtr(pCritSect);
740 Assert(!(pCritSect->s.Core.fFlags & RTCRITSECT_FLAGS_NOP));
741 Assert(hEventToSignal != NIL_SUPSEMEVENT);
742# ifdef IN_RING3
743 if (RT_UNLIKELY(!RTCritSectIsOwner(&pCritSect->s.Core)))
744 return VERR_NOT_OWNER;
745# endif
746 if (RT_LIKELY( pCritSect->s.hEventToSignal == NIL_RTSEMEVENT
747 || pCritSect->s.hEventToSignal == hEventToSignal))
748 {
749 pCritSect->s.hEventToSignal = hEventToSignal;
750 return VINF_SUCCESS;
751 }
752 return VERR_TOO_MANY_SEMAPHORES;
753}
754#endif /* IN_RING0 || IN_RING3 */
755
756
757/**
758 * Checks the caller is the owner of the critical section.
759 *
760 * @returns true if owner.
761 * @returns false if not owner.
762 * @param pVM The cross context VM structure.
763 * @param pCritSect The critical section.
764 */
765VMMDECL(bool) PDMCritSectIsOwner(PVMCC pVM, PCPDMCRITSECT pCritSect)
766{
767#ifdef IN_RING3
768 RT_NOREF(pVM);
769 return RTCritSectIsOwner(&pCritSect->s.Core);
770#else
771 PVMCPUCC pVCpu = VMMGetCpu(pVM);
772 if ( !pVCpu
773 || pCritSect->s.Core.NativeThreadOwner != pVCpu->hNativeThread)
774 return false;
775 return (pCritSect->s.Core.fFlags & PDMCRITSECT_FLAGS_PENDING_UNLOCK) == 0
776 || pCritSect->s.Core.cNestings > 1;
777#endif
778}
779
780
781/**
782 * Checks the specified VCPU is the owner of the critical section.
783 *
784 * @returns true if owner.
785 * @returns false if not owner.
786 * @param pVCpu The cross context virtual CPU structure.
787 * @param pCritSect The critical section.
788 */
789VMMDECL(bool) PDMCritSectIsOwnerEx(PVMCPUCC pVCpu, PCPDMCRITSECT pCritSect)
790{
791#ifdef IN_RING3
792 NOREF(pVCpu);
793 return RTCritSectIsOwner(&pCritSect->s.Core);
794#else
795 Assert(VMCC_GET_CPU(pVCpu->CTX_SUFF(pVM), pVCpu->idCpu) == pVCpu);
796 if (pCritSect->s.Core.NativeThreadOwner != pVCpu->hNativeThread)
797 return false;
798 return (pCritSect->s.Core.fFlags & PDMCRITSECT_FLAGS_PENDING_UNLOCK) == 0
799 || pCritSect->s.Core.cNestings > 1;
800#endif
801}
802
803
804/**
805 * Checks if anyone is waiting on the critical section we own.
806 *
807 * @returns true if someone is waiting.
808 * @returns false if no one is waiting.
809 * @param pVM The cross context VM structure.
810 * @param pCritSect The critical section.
811 */
812VMMDECL(bool) PDMCritSectHasWaiters(PVMCC pVM, PCPDMCRITSECT pCritSect)
813{
814 AssertReturn(pCritSect->s.Core.u32Magic == RTCRITSECT_MAGIC, false);
815 Assert(pCritSect->s.Core.NativeThreadOwner == pdmCritSectGetNativeSelf(pVM, pCritSect)); RT_NOREF(pVM);
816 return pCritSect->s.Core.cLockers >= pCritSect->s.Core.cNestings;
817}
818
819
820/**
821 * Checks if a critical section is initialized or not.
822 *
823 * @returns true if initialized.
824 * @returns false if not initialized.
825 * @param pCritSect The critical section.
826 */
827VMMDECL(bool) PDMCritSectIsInitialized(PCPDMCRITSECT pCritSect)
828{
829 return RTCritSectIsInitialized(&pCritSect->s.Core);
830}
831
832
833/**
834 * Gets the recursion depth.
835 *
836 * @returns The recursion depth.
837 * @param pCritSect The critical section.
838 */
839VMMDECL(uint32_t) PDMCritSectGetRecursion(PCPDMCRITSECT pCritSect)
840{
841 return RTCritSectGetRecursion(&pCritSect->s.Core);
842}
843
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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