1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
|
/* $Id: PDMAllCritSectRw.cpp $ */
/** @file
* IPRT - Read/Write Critical Section, Generic.
*/
/*
* Copyright (C) 2009-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_PDM//_CRITSECT
#include "PDMInternal.h"
#include <VBox/vmm/pdmcritsectrw.h>
#include <VBox/vmm/mm.h>
#include <VBox/vmm/vmm.h>
#include <VBox/vmm/vm.h>
#include <VBox/err.h>
#include <VBox/vmm/hm.h>
#include <VBox/log.h>
#include <iprt/asm.h>
#include <iprt/asm-amd64-x86.h>
#include <iprt/assert.h>
#ifdef IN_RING3
# include <iprt/lockvalidator.h>
# include <iprt/semaphore.h>
#endif
#if defined(IN_RING3) || defined(IN_RING0)
# include <iprt/thread.h>
#endif
/*********************************************************************************************************************************
* Defined Constants And Macros *
*********************************************************************************************************************************/
/** The number loops to spin for shared access in ring-3. */
#define PDMCRITSECTRW_SHRD_SPIN_COUNT_R3 20
/** The number loops to spin for shared access in ring-0. */
#define PDMCRITSECTRW_SHRD_SPIN_COUNT_R0 128
/** The number loops to spin for shared access in the raw-mode context. */
#define PDMCRITSECTRW_SHRD_SPIN_COUNT_RC 128
/** The number loops to spin for exclusive access in ring-3. */
#define PDMCRITSECTRW_EXCL_SPIN_COUNT_R3 20
/** The number loops to spin for exclusive access in ring-0. */
#define PDMCRITSECTRW_EXCL_SPIN_COUNT_R0 256
/** The number loops to spin for exclusive access in the raw-mode context. */
#define PDMCRITSECTRW_EXCL_SPIN_COUNT_RC 256
/* Undefine the automatic VBOX_STRICT API mappings. */
#undef PDMCritSectRwEnterExcl
#undef PDMCritSectRwTryEnterExcl
#undef PDMCritSectRwEnterShared
#undef PDMCritSectRwTryEnterShared
/**
* Gets the ring-3 native thread handle of the calling thread.
*
* @returns native thread handle (ring-3).
* @param pThis The read/write critical section. This is only used in
* R0 and RC.
*/
DECL_FORCE_INLINE(RTNATIVETHREAD) pdmCritSectRwGetNativeSelf(PCPDMCRITSECTRW pThis)
{
#ifdef IN_RING3
NOREF(pThis);
RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
#else
AssertMsgReturn(pThis->s.Core.u32Magic == RTCRITSECTRW_MAGIC, ("%RX32\n", pThis->s.Core.u32Magic),
NIL_RTNATIVETHREAD);
PVM pVM = pThis->s.CTX_SUFF(pVM); AssertPtr(pVM);
PVMCPU pVCpu = VMMGetCpu(pVM); AssertPtr(pVCpu);
RTNATIVETHREAD hNativeSelf = pVCpu->hNativeThread; Assert(hNativeSelf != NIL_RTNATIVETHREAD);
#endif
return hNativeSelf;
}
#ifdef IN_RING3
/**
* Changes the lock validator sub-class of the read/write critical section.
*
* It is recommended to try make sure that nobody is using this critical section
* while changing the value.
*
* @returns The old sub-class. RTLOCKVAL_SUB_CLASS_INVALID is returns if the
* lock validator isn't compiled in or either of the parameters are
* invalid.
* @param pThis Pointer to the read/write critical section.
* @param uSubClass The new sub-class value.
*/
VMMDECL(uint32_t) PDMR3CritSectRwSetSubClass(PPDMCRITSECTRW pThis, uint32_t uSubClass)
{
AssertPtrReturn(pThis, RTLOCKVAL_SUB_CLASS_INVALID);
AssertReturn(pThis->s.Core.u32Magic == RTCRITSECTRW_MAGIC, RTLOCKVAL_SUB_CLASS_INVALID);
# if defined(PDMCRITSECTRW_STRICT) && defined(IN_RING3)
AssertReturn(!(pThis->s.Core.fFlags & RTCRITSECT_FLAGS_NOP), RTLOCKVAL_SUB_CLASS_INVALID);
RTLockValidatorRecSharedSetSubClass(pThis->s.Core.pValidatorRead, uSubClass);
return RTLockValidatorRecExclSetSubClass(pThis->s.Core.pValidatorWrite, uSubClass);
# else
NOREF(uSubClass);
return RTLOCKVAL_SUB_CLASS_INVALID;
# endif
}
#endif /* IN_RING3 */
#ifdef IN_RING0
/**
* Go back to ring-3 so the kernel can do signals, APCs and other fun things.
*
* @param pThis Pointer to the read/write critical section.
*/
static void pdmR0CritSectRwYieldToRing3(PPDMCRITSECTRW pThis)
{
PVM pVM = pThis->s.CTX_SUFF(pVM); AssertPtr(pVM);
PVMCPU pVCpu = VMMGetCpu(pVM); AssertPtr(pVCpu);
int rc = VMMRZCallRing3(pVM, pVCpu, VMMCALLRING3_VM_R0_PREEMPT, NULL);
AssertRC(rc);
}
#endif /* IN_RING0 */
/**
* Worker that enters a read/write critical section with shard access.
*
* @returns VBox status code.
* @param pThis Pointer to the read/write critical section.
* @param rcBusy The busy return code for ring-0 and ring-3.
* @param fTryOnly Only try enter it, don't wait.
* @param pSrcPos The source position. (Can be NULL.)
* @param fNoVal No validation records.
*/
static int pdmCritSectRwEnterShared(PPDMCRITSECTRW pThis, int rcBusy, bool fTryOnly, PCRTLOCKVALSRCPOS pSrcPos, bool fNoVal)
{
/*
* Validate input.
*/
AssertPtr(pThis);
AssertReturn(pThis->s.Core.u32Magic == RTCRITSECTRW_MAGIC, VERR_SEM_DESTROYED);
#if !defined(PDMCRITSECTRW_STRICT) || !defined(IN_RING3)
NOREF(pSrcPos);
NOREF(fNoVal);
#endif
#ifdef IN_RING3
NOREF(rcBusy);
#endif
#if defined(PDMCRITSECTRW_STRICT) && defined(IN_RING3)
RTTHREAD hThreadSelf = RTThreadSelfAutoAdopt();
if (!fTryOnly)
{
int rc9;
RTNATIVETHREAD hNativeWriter;
ASMAtomicUoReadHandle(&pThis->s.Core.hNativeWriter, &hNativeWriter);
if (hNativeWriter != NIL_RTTHREAD && hNativeWriter == pdmCritSectRwGetNativeSelf(pThis))
rc9 = RTLockValidatorRecExclCheckOrder(pThis->s.Core.pValidatorWrite, hThreadSelf, pSrcPos, RT_INDEFINITE_WAIT);
else
rc9 = RTLockValidatorRecSharedCheckOrder(pThis->s.Core.pValidatorRead, hThreadSelf, pSrcPos, RT_INDEFINITE_WAIT);
if (RT_FAILURE(rc9))
return rc9;
}
#endif
/*
* Get cracking...
*/
uint64_t u64State = ASMAtomicReadU64(&pThis->s.Core.u64State);
uint64_t u64OldState = u64State;
for (;;)
{
if ((u64State & RTCSRW_DIR_MASK) == (RTCSRW_DIR_READ << RTCSRW_DIR_SHIFT))
{
/* It flows in the right direction, try follow it before it changes. */
uint64_t c = (u64State & RTCSRW_CNT_RD_MASK) >> RTCSRW_CNT_RD_SHIFT;
c++;
Assert(c < RTCSRW_CNT_MASK / 2);
u64State &= ~RTCSRW_CNT_RD_MASK;
u64State |= c << RTCSRW_CNT_RD_SHIFT;
if (ASMAtomicCmpXchgU64(&pThis->s.Core.u64State, u64State, u64OldState))
{
#if defined(PDMCRITSECTRW_STRICT) && defined(IN_RING3)
if (!fNoVal)
RTLockValidatorRecSharedAddOwner(pThis->s.Core.pValidatorRead, hThreadSelf, pSrcPos);
#endif
break;
}
}
else if ((u64State & (RTCSRW_CNT_RD_MASK | RTCSRW_CNT_WR_MASK)) == 0)
{
/* Wrong direction, but we're alone here and can simply try switch the direction. */
u64State &= ~(RTCSRW_CNT_RD_MASK | RTCSRW_CNT_WR_MASK | RTCSRW_DIR_MASK);
u64State |= (UINT64_C(1) << RTCSRW_CNT_RD_SHIFT) | (RTCSRW_DIR_READ << RTCSRW_DIR_SHIFT);
if (ASMAtomicCmpXchgU64(&pThis->s.Core.u64State, u64State, u64OldState))
{
Assert(!pThis->s.Core.fNeedReset);
#if defined(PDMCRITSECTRW_STRICT) && defined(IN_RING3)
if (!fNoVal)
RTLockValidatorRecSharedAddOwner(pThis->s.Core.pValidatorRead, hThreadSelf, pSrcPos);
#endif
break;
}
}
else
{
/* Is the writer perhaps doing a read recursion? */
RTNATIVETHREAD hNativeSelf = pdmCritSectRwGetNativeSelf(pThis);
RTNATIVETHREAD hNativeWriter;
ASMAtomicUoReadHandle(&pThis->s.Core.hNativeWriter, &hNativeWriter);
if (hNativeSelf == hNativeWriter)
{
#if defined(PDMCRITSECTRW_STRICT) && defined(IN_RING3)
if (!fNoVal)
{
int rc9 = RTLockValidatorRecExclRecursionMixed(pThis->s.Core.pValidatorWrite, &pThis->s.Core.pValidatorRead->Core, pSrcPos);
if (RT_FAILURE(rc9))
return rc9;
}
#endif
Assert(pThis->s.Core.cWriterReads < UINT32_MAX / 2);
ASMAtomicIncU32(&pThis->s.Core.cWriterReads);
STAM_REL_COUNTER_INC(&pThis->s.CTX_MID_Z(Stat,EnterShared));
return VINF_SUCCESS; /* don't break! */
}
/*
* If we're only trying, return already.
*/
if (fTryOnly)
{
STAM_REL_COUNTER_INC(&pThis->s.CTX_MID_Z(StatContention,EnterShared));
return VERR_SEM_BUSY;
}
#if defined(IN_RING3) || defined(IN_RING0)
# ifdef IN_RING0
if ( RTThreadPreemptIsEnabled(NIL_RTTHREAD)
&& ASMIntAreEnabled())
# endif
{
/*
* Add ourselves to the queue and wait for the direction to change.
*/
uint64_t c = (u64State & RTCSRW_CNT_RD_MASK) >> RTCSRW_CNT_RD_SHIFT;
c++;
Assert(c < RTCSRW_CNT_MASK / 2);
uint64_t cWait = (u64State & RTCSRW_WAIT_CNT_RD_MASK) >> RTCSRW_WAIT_CNT_RD_SHIFT;
cWait++;
Assert(cWait <= c);
Assert(cWait < RTCSRW_CNT_MASK / 2);
u64State &= ~(RTCSRW_CNT_RD_MASK | RTCSRW_WAIT_CNT_RD_MASK);
u64State |= (c << RTCSRW_CNT_RD_SHIFT) | (cWait << RTCSRW_WAIT_CNT_RD_SHIFT);
if (ASMAtomicCmpXchgU64(&pThis->s.Core.u64State, u64State, u64OldState))
{
for (uint32_t iLoop = 0; ; iLoop++)
{
int rc;
# ifdef IN_RING3
# if defined(PDMCRITSECTRW_STRICT) && defined(IN_RING3)
rc = RTLockValidatorRecSharedCheckBlocking(pThis->s.Core.pValidatorRead, hThreadSelf, pSrcPos, true,
RT_INDEFINITE_WAIT, RTTHREADSTATE_RW_READ, false);
if (RT_SUCCESS(rc))
# else
RTTHREAD hThreadSelf = RTThreadSelf();
RTThreadBlocking(hThreadSelf, RTTHREADSTATE_RW_READ, false);
# endif
# endif
{
for (;;)
{
rc = SUPSemEventMultiWaitNoResume(pThis->s.CTX_SUFF(pVM)->pSession,
(SUPSEMEVENTMULTI)pThis->s.Core.hEvtRead,
RT_INDEFINITE_WAIT);
if ( rc != VERR_INTERRUPTED
|| pThis->s.Core.u32Magic != RTCRITSECTRW_MAGIC)
break;
# ifdef IN_RING0
pdmR0CritSectRwYieldToRing3(pThis);
# endif
}
# ifdef IN_RING3
RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_RW_READ);
# endif
if (pThis->s.Core.u32Magic != RTCRITSECTRW_MAGIC)
return VERR_SEM_DESTROYED;
}
if (RT_FAILURE(rc))
{
/* Decrement the counts and return the error. */
for (;;)
{
u64OldState = u64State = ASMAtomicReadU64(&pThis->s.Core.u64State);
c = (u64State & RTCSRW_CNT_RD_MASK) >> RTCSRW_CNT_RD_SHIFT; Assert(c > 0);
c--;
cWait = (u64State & RTCSRW_WAIT_CNT_RD_MASK) >> RTCSRW_WAIT_CNT_RD_SHIFT; Assert(cWait > 0);
cWait--;
u64State &= ~(RTCSRW_CNT_RD_MASK | RTCSRW_WAIT_CNT_RD_MASK);
u64State |= (c << RTCSRW_CNT_RD_SHIFT) | (cWait << RTCSRW_WAIT_CNT_RD_SHIFT);
if (ASMAtomicCmpXchgU64(&pThis->s.Core.u64State, u64State, u64OldState))
break;
}
return rc;
}
Assert(pThis->s.Core.fNeedReset);
u64State = ASMAtomicReadU64(&pThis->s.Core.u64State);
if ((u64State & RTCSRW_DIR_MASK) == (RTCSRW_DIR_READ << RTCSRW_DIR_SHIFT))
break;
AssertMsg(iLoop < 1, ("%u\n", iLoop));
}
/* Decrement the wait count and maybe reset the semaphore (if we're last). */
for (;;)
{
u64OldState = u64State;
cWait = (u64State & RTCSRW_WAIT_CNT_RD_MASK) >> RTCSRW_WAIT_CNT_RD_SHIFT;
Assert(cWait > 0);
cWait--;
u64State &= ~RTCSRW_WAIT_CNT_RD_MASK;
u64State |= cWait << RTCSRW_WAIT_CNT_RD_SHIFT;
if (ASMAtomicCmpXchgU64(&pThis->s.Core.u64State, u64State, u64OldState))
{
if (cWait == 0)
{
if (ASMAtomicXchgBool(&pThis->s.Core.fNeedReset, false))
{
int rc = SUPSemEventMultiReset(pThis->s.CTX_SUFF(pVM)->pSession,
(SUPSEMEVENTMULTI)pThis->s.Core.hEvtRead);
AssertRCReturn(rc, rc);
}
}
break;
}
u64State = ASMAtomicReadU64(&pThis->s.Core.u64State);
}
# if defined(PDMCRITSECTRW_STRICT) && defined(IN_RING3)
if (!fNoVal)
RTLockValidatorRecSharedAddOwner(pThis->s.Core.pValidatorRead, hThreadSelf, pSrcPos);
# endif
break;
}
}
#endif /* IN_RING3 || IN_RING3 */
#ifndef IN_RING3
# ifdef IN_RING0
else
# endif
{
/*
* We cannot call SUPSemEventMultiWaitNoResume in this context. Go
* back to ring-3 and do it there or return rcBusy.
*/
STAM_REL_COUNTER_INC(&pThis->s.CTX_MID_Z(StatContention,EnterShared));
if (rcBusy == VINF_SUCCESS)
{
PVM pVM = pThis->s.CTX_SUFF(pVM); AssertPtr(pVM);
PVMCPU pVCpu = VMMGetCpu(pVM); AssertPtr(pVCpu);
/** @todo Should actually do this in via VMMR0.cpp instead of going all the way
* back to ring-3. Goes for both kind of crit sects. */
return VMMRZCallRing3(pVM, pVCpu, VMMCALLRING3_PDM_CRIT_SECT_RW_ENTER_SHARED, MMHyperCCToR3(pVM, pThis));
}
return rcBusy;
}
#endif /* !IN_RING3 */
}
if (pThis->s.Core.u32Magic != RTCRITSECTRW_MAGIC)
return VERR_SEM_DESTROYED;
ASMNopPause();
u64State = ASMAtomicReadU64(&pThis->s.Core.u64State);
u64OldState = u64State;
}
/* got it! */
STAM_REL_COUNTER_INC(&pThis->s.CTX_MID_Z(Stat,EnterShared));
Assert((ASMAtomicReadU64(&pThis->s.Core.u64State) & RTCSRW_DIR_MASK) == (RTCSRW_DIR_READ << RTCSRW_DIR_SHIFT));
return VINF_SUCCESS;
}
/**
* Enter a critical section with shared (read) access.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @retval rcBusy if in ring-0 or raw-mode context and it is busy.
* @retval VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
* @retval VERR_SEM_DESTROYED if the critical section is delete before or
* during the operation.
*
* @param pThis Pointer to the read/write critical section.
* @param rcBusy The status code to return when we're in RC or R0 and the
* section is busy. Pass VINF_SUCCESS to acquired the
* critical section thru a ring-3 call if necessary.
* @sa PDMCritSectRwEnterSharedDebug, PDMCritSectRwTryEnterShared,
* PDMCritSectRwTryEnterSharedDebug, PDMCritSectRwLeaveShared,
* RTCritSectRwEnterShared.
*/
VMMDECL(int) PDMCritSectRwEnterShared(PPDMCRITSECTRW pThis, int rcBusy)
{
#if !defined(PDMCRITSECTRW_STRICT) || !defined(IN_RING3)
return pdmCritSectRwEnterShared(pThis, rcBusy, false /*fTryOnly*/, NULL, false /*fNoVal*/);
#else
RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
return pdmCritSectRwEnterShared(pThis, rcBusy, false /*fTryOnly*/, &SrcPos, false /*fNoVal*/);
#endif
}
/**
* Enter a critical section with shared (read) access.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @retval rcBusy if in ring-0 or raw-mode context and it is busy.
* @retval VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
* @retval VERR_SEM_DESTROYED if the critical section is delete before or
* during the operation.
*
* @param pThis Pointer to the read/write critical section.
* @param rcBusy The status code to return when we're in RC or R0 and the
* section is busy. Pass VINF_SUCCESS to acquired the
* critical section thru a ring-3 call if necessary.
* @param uId Where we're entering the section.
* @param SRC_POS The source position.
* @sa PDMCritSectRwEnterShared, PDMCritSectRwTryEnterShared,
* PDMCritSectRwTryEnterSharedDebug, PDMCritSectRwLeaveShared,
* RTCritSectRwEnterSharedDebug.
*/
VMMDECL(int) PDMCritSectRwEnterSharedDebug(PPDMCRITSECTRW pThis, int rcBusy, RTHCUINTPTR uId, RT_SRC_POS_DECL)
{
NOREF(uId); NOREF(pszFile); NOREF(iLine); NOREF(pszFunction);
#if !defined(PDMCRITSECTRW_STRICT) || !defined(IN_RING3)
return pdmCritSectRwEnterShared(pThis, rcBusy, false /*fTryOnly*/, NULL, false /*fNoVal*/);
#else
RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
return pdmCritSectRwEnterShared(pThis, rcBusy, false /*fTryOnly*/, &SrcPos, false /*fNoVal*/);
#endif
}
/**
* Try enter a critical section with shared (read) access.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @retval VERR_SEM_BUSY if the critsect was owned.
* @retval VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
* @retval VERR_SEM_DESTROYED if the critical section is delete before or
* during the operation.
*
* @param pThis Pointer to the read/write critical section.
* @sa PDMCritSectRwTryEnterSharedDebug, PDMCritSectRwEnterShared,
* PDMCritSectRwEnterSharedDebug, PDMCritSectRwLeaveShared,
* RTCritSectRwTryEnterShared.
*/
VMMDECL(int) PDMCritSectRwTryEnterShared(PPDMCRITSECTRW pThis)
{
#if !defined(PDMCRITSECTRW_STRICT) || !defined(IN_RING3)
return pdmCritSectRwEnterShared(pThis, VERR_SEM_BUSY, true /*fTryOnly*/, NULL, false /*fNoVal*/);
#else
RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
return pdmCritSectRwEnterShared(pThis, VERR_SEM_BUSY, true /*fTryOnly*/, &SrcPos, false /*fNoVal*/);
#endif
}
/**
* Try enter a critical section with shared (read) access.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @retval VERR_SEM_BUSY if the critsect was owned.
* @retval VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
* @retval VERR_SEM_DESTROYED if the critical section is delete before or
* during the operation.
*
* @param pThis Pointer to the read/write critical section.
* @param uId Where we're entering the section.
* @param SRC_POS The source position.
* @sa PDMCritSectRwTryEnterShared, PDMCritSectRwEnterShared,
* PDMCritSectRwEnterSharedDebug, PDMCritSectRwLeaveShared,
* RTCritSectRwTryEnterSharedDebug.
*/
VMMDECL(int) PDMCritSectRwTryEnterSharedDebug(PPDMCRITSECTRW pThis, RTHCUINTPTR uId, RT_SRC_POS_DECL)
{
NOREF(uId); NOREF(pszFile); NOREF(iLine); NOREF(pszFunction);
#if !defined(PDMCRITSECTRW_STRICT) || !defined(IN_RING3)
return pdmCritSectRwEnterShared(pThis, VERR_SEM_BUSY, true /*fTryOnly*/, NULL, false /*fNoVal*/);
#else
RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
return pdmCritSectRwEnterShared(pThis, VERR_SEM_BUSY, true /*fTryOnly*/, &SrcPos, false /*fNoVal*/);
#endif
}
#ifdef IN_RING3
/**
* Enters a PDM read/write critical section with shared (read) access.
*
* @returns VINF_SUCCESS if entered successfully.
* @retval VERR_SEM_DESTROYED if the critical section is delete before or
* during the operation.
*
* @param pThis Pointer to the read/write critical section.
* @param fCallRing3 Whether this is a VMMRZCallRing3()request.
*/
VMMR3DECL(int) PDMR3CritSectRwEnterSharedEx(PPDMCRITSECTRW pThis, bool fCallRing3)
{
return pdmCritSectRwEnterShared(pThis, VERR_SEM_BUSY, false /*fTryAgain*/, NULL, fCallRing3);
}
#endif
/**
* Leave a critical section held with shared access.
*
* @returns VBox status code.
* @retval VERR_SEM_DESTROYED if the critical section is delete before or
* during the operation.
* @param pThis Pointer to the read/write critical section.
* @param fNoVal No validation records (i.e. queued release).
* @sa PDMCritSectRwEnterShared, PDMCritSectRwTryEnterShared,
* PDMCritSectRwEnterSharedDebug, PDMCritSectRwTryEnterSharedDebug,
* PDMCritSectRwLeaveExcl, RTCritSectRwLeaveShared.
*/
static int pdmCritSectRwLeaveSharedWorker(PPDMCRITSECTRW pThis, bool fNoVal)
{
/*
* Validate handle.
*/
AssertPtr(pThis);
AssertReturn(pThis->s.Core.u32Magic == RTCRITSECTRW_MAGIC, VERR_SEM_DESTROYED);
#if !defined(PDMCRITSECTRW_STRICT) || !defined(IN_RING3)
NOREF(fNoVal);
#endif
/*
* Check the direction and take action accordingly.
*/
uint64_t u64State = ASMAtomicReadU64(&pThis->s.Core.u64State);
uint64_t u64OldState = u64State;
if ((u64State & RTCSRW_DIR_MASK) == (RTCSRW_DIR_READ << RTCSRW_DIR_SHIFT))
{
#if defined(PDMCRITSECTRW_STRICT) && defined(IN_RING3)
if (fNoVal)
Assert(!RTLockValidatorRecSharedIsOwner(pThis->s.Core.pValidatorRead, NIL_RTTHREAD));
else
{
int rc9 = RTLockValidatorRecSharedCheckAndRelease(pThis->s.Core.pValidatorRead, NIL_RTTHREAD);
if (RT_FAILURE(rc9))
return rc9;
}
#endif
for (;;)
{
uint64_t c = (u64State & RTCSRW_CNT_RD_MASK) >> RTCSRW_CNT_RD_SHIFT;
AssertReturn(c > 0, VERR_NOT_OWNER);
c--;
if ( c > 0
|| (u64State & RTCSRW_CNT_WR_MASK) == 0)
{
/* Don't change the direction. */
u64State &= ~RTCSRW_CNT_RD_MASK;
u64State |= c << RTCSRW_CNT_RD_SHIFT;
if (ASMAtomicCmpXchgU64(&pThis->s.Core.u64State, u64State, u64OldState))
break;
}
else
{
#if defined(IN_RING3) || defined(IN_RING0)
# ifdef IN_RING0
if ( RTThreadPreemptIsEnabled(NIL_RTTHREAD)
&& ASMIntAreEnabled())
# endif
{
/* Reverse the direction and signal the writer threads. */
u64State &= ~(RTCSRW_CNT_RD_MASK | RTCSRW_DIR_MASK);
u64State |= RTCSRW_DIR_WRITE << RTCSRW_DIR_SHIFT;
if (ASMAtomicCmpXchgU64(&pThis->s.Core.u64State, u64State, u64OldState))
{
int rc = SUPSemEventSignal(pThis->s.CTX_SUFF(pVM)->pSession, (SUPSEMEVENT)pThis->s.Core.hEvtWrite);
AssertRC(rc);
break;
}
}
#endif /* IN_RING3 || IN_RING0 */
#ifndef IN_RING3
# ifdef IN_RING0
else
# endif
{
/* Queue the exit request (ring-3). */
PVM pVM = pThis->s.CTX_SUFF(pVM); AssertPtr(pVM);
PVMCPU pVCpu = VMMGetCpu(pVM); AssertPtr(pVCpu);
uint32_t i = pVCpu->pdm.s.cQueuedCritSectRwShrdLeaves++;
LogFlow(("PDMCritSectRwLeaveShared: [%d]=%p => R3 c=%d (%#llx)\n", i, pThis, c, u64State));
AssertFatal(i < RT_ELEMENTS(pVCpu->pdm.s.apQueuedCritSectRwShrdLeaves));
pVCpu->pdm.s.apQueuedCritSectRwShrdLeaves[i] = MMHyperCCToR3(pVM, pThis);
VMCPU_FF_SET(pVCpu, VMCPU_FF_PDM_CRITSECT);
VMCPU_FF_SET(pVCpu, VMCPU_FF_TO_R3);
STAM_REL_COUNTER_INC(&pVM->pdm.s.StatQueuedCritSectLeaves);
STAM_REL_COUNTER_INC(&pThis->s.StatContentionRZLeaveShared);
break;
}
#endif
}
ASMNopPause();
u64State = ASMAtomicReadU64(&pThis->s.Core.u64State);
u64OldState = u64State;
}
}
else
{
RTNATIVETHREAD hNativeSelf = pdmCritSectRwGetNativeSelf(pThis);
RTNATIVETHREAD hNativeWriter;
ASMAtomicUoReadHandle(&pThis->s.Core.hNativeWriter, &hNativeWriter);
AssertReturn(hNativeSelf == hNativeWriter, VERR_NOT_OWNER);
AssertReturn(pThis->s.Core.cWriterReads > 0, VERR_NOT_OWNER);
#if defined(PDMCRITSECTRW_STRICT) && defined(IN_RING3)
if (!fNoVal)
{
int rc = RTLockValidatorRecExclUnwindMixed(pThis->s.Core.pValidatorWrite, &pThis->s.Core.pValidatorRead->Core);
if (RT_FAILURE(rc))
return rc;
}
#endif
ASMAtomicDecU32(&pThis->s.Core.cWriterReads);
}
return VINF_SUCCESS;
}
/**
* Leave a critical section held with shared access.
*
* @returns VBox status code.
* @retval VERR_SEM_DESTROYED if the critical section is delete before or
* during the operation.
* @param pThis Pointer to the read/write critical section.
* @sa PDMCritSectRwEnterShared, PDMCritSectRwTryEnterShared,
* PDMCritSectRwEnterSharedDebug, PDMCritSectRwTryEnterSharedDebug,
* PDMCritSectRwLeaveExcl, RTCritSectRwLeaveShared.
*/
VMMDECL(int) PDMCritSectRwLeaveShared(PPDMCRITSECTRW pThis)
{
return pdmCritSectRwLeaveSharedWorker(pThis, false /*fNoVal*/);
}
#if defined(IN_RING3) || defined(IN_RING0)
/**
* PDMCritSectBothFF interface.
*
* @param pThis Pointer to the read/write critical section.
*/
void pdmCritSectRwLeaveSharedQueued(PPDMCRITSECTRW pThis)
{
pdmCritSectRwLeaveSharedWorker(pThis, true /*fNoVal*/);
}
#endif
/**
* Worker that enters a read/write critical section with exclusive access.
*
* @returns VBox status code.
* @param pThis Pointer to the read/write critical section.
* @param rcBusy The busy return code for ring-0 and ring-3.
* @param fTryOnly Only try enter it, don't wait.
* @param pSrcPos The source position. (Can be NULL.)
* @param fNoVal No validation records.
*/
static int pdmCritSectRwEnterExcl(PPDMCRITSECTRW pThis, int rcBusy, bool fTryOnly, PCRTLOCKVALSRCPOS pSrcPos, bool fNoVal)
{
/*
* Validate input.
*/
AssertPtr(pThis);
AssertReturn(pThis->s.Core.u32Magic == RTCRITSECTRW_MAGIC, VERR_SEM_DESTROYED);
#if !defined(PDMCRITSECTRW_STRICT) || !defined(IN_RING3)
NOREF(pSrcPos);
NOREF(fNoVal);
#endif
#ifdef IN_RING3
NOREF(rcBusy);
#endif
#if defined(PDMCRITSECTRW_STRICT) && defined(IN_RING3)
RTTHREAD hThreadSelf = NIL_RTTHREAD;
if (!fTryOnly)
{
hThreadSelf = RTThreadSelfAutoAdopt();
int rc9 = RTLockValidatorRecExclCheckOrder(pThis->s.Core.pValidatorWrite, hThreadSelf, pSrcPos, RT_INDEFINITE_WAIT);
if (RT_FAILURE(rc9))
return rc9;
}
#endif
/*
* Check if we're already the owner and just recursing.
*/
RTNATIVETHREAD hNativeSelf = pdmCritSectRwGetNativeSelf(pThis);
RTNATIVETHREAD hNativeWriter;
ASMAtomicUoReadHandle(&pThis->s.Core.hNativeWriter, &hNativeWriter);
if (hNativeSelf == hNativeWriter)
{
Assert((ASMAtomicReadU64(&pThis->s.Core.u64State) & RTCSRW_DIR_MASK) == (RTCSRW_DIR_WRITE << RTCSRW_DIR_SHIFT));
#if defined(PDMCRITSECTRW_STRICT) && defined(IN_RING3)
if (!fNoVal)
{
int rc9 = RTLockValidatorRecExclRecursion(pThis->s.Core.pValidatorWrite, pSrcPos);
if (RT_FAILURE(rc9))
return rc9;
}
#endif
Assert(pThis->s.Core.cWriteRecursions < UINT32_MAX / 2);
STAM_REL_COUNTER_INC(&pThis->s.CTX_MID_Z(Stat,EnterExcl));
ASMAtomicIncU32(&pThis->s.Core.cWriteRecursions);
return VINF_SUCCESS;
}
/*
* Get cracking.
*/
uint64_t u64State = ASMAtomicReadU64(&pThis->s.Core.u64State);
uint64_t u64OldState = u64State;
for (;;)
{
if ( (u64State & RTCSRW_DIR_MASK) == (RTCSRW_DIR_WRITE << RTCSRW_DIR_SHIFT)
|| (u64State & (RTCSRW_CNT_RD_MASK | RTCSRW_CNT_WR_MASK)) != 0)
{
/* It flows in the right direction, try follow it before it changes. */
uint64_t c = (u64State & RTCSRW_CNT_WR_MASK) >> RTCSRW_CNT_WR_SHIFT;
c++;
Assert(c < RTCSRW_CNT_MASK / 2);
u64State &= ~RTCSRW_CNT_WR_MASK;
u64State |= c << RTCSRW_CNT_WR_SHIFT;
if (ASMAtomicCmpXchgU64(&pThis->s.Core.u64State, u64State, u64OldState))
break;
}
else if ((u64State & (RTCSRW_CNT_RD_MASK | RTCSRW_CNT_WR_MASK)) == 0)
{
/* Wrong direction, but we're alone here and can simply try switch the direction. */
u64State &= ~(RTCSRW_CNT_RD_MASK | RTCSRW_CNT_WR_MASK | RTCSRW_DIR_MASK);
u64State |= (UINT64_C(1) << RTCSRW_CNT_WR_SHIFT) | (RTCSRW_DIR_WRITE << RTCSRW_DIR_SHIFT);
if (ASMAtomicCmpXchgU64(&pThis->s.Core.u64State, u64State, u64OldState))
break;
}
else if (fTryOnly)
{
/* Wrong direction and we're not supposed to wait, just return. */
STAM_REL_COUNTER_INC(&pThis->s.CTX_MID_Z(StatContention,EnterExcl));
return VERR_SEM_BUSY;
}
else
{
/* Add ourselves to the write count and break out to do the wait. */
uint64_t c = (u64State & RTCSRW_CNT_WR_MASK) >> RTCSRW_CNT_WR_SHIFT;
c++;
Assert(c < RTCSRW_CNT_MASK / 2);
u64State &= ~RTCSRW_CNT_WR_MASK;
u64State |= c << RTCSRW_CNT_WR_SHIFT;
if (ASMAtomicCmpXchgU64(&pThis->s.Core.u64State, u64State, u64OldState))
break;
}
if (pThis->s.Core.u32Magic != RTCRITSECTRW_MAGIC)
return VERR_SEM_DESTROYED;
ASMNopPause();
u64State = ASMAtomicReadU64(&pThis->s.Core.u64State);
u64OldState = u64State;
}
/*
* If we're in write mode now try grab the ownership. Play fair if there
* are threads already waiting.
*/
bool fDone = (u64State & RTCSRW_DIR_MASK) == (RTCSRW_DIR_WRITE << RTCSRW_DIR_SHIFT)
#if defined(IN_RING3)
&& ( ((u64State & RTCSRW_CNT_WR_MASK) >> RTCSRW_CNT_WR_SHIFT) == 1
|| fTryOnly)
#endif
;
if (fDone)
ASMAtomicCmpXchgHandle(&pThis->s.Core.hNativeWriter, hNativeSelf, NIL_RTNATIVETHREAD, fDone);
if (!fDone)
{
STAM_REL_COUNTER_INC(&pThis->s.CTX_MID_Z(StatContention,EnterExcl));
#if defined(IN_RING3) || defined(IN_RING0)
if ( !fTryOnly
# ifdef IN_RING0
&& RTThreadPreemptIsEnabled(NIL_RTTHREAD)
&& ASMIntAreEnabled()
# endif
)
{
/*
* Wait for our turn.
*/
for (uint32_t iLoop = 0; ; iLoop++)
{
int rc;
# ifdef IN_RING3
# ifdef PDMCRITSECTRW_STRICT
if (hThreadSelf == NIL_RTTHREAD)
hThreadSelf = RTThreadSelfAutoAdopt();
rc = RTLockValidatorRecExclCheckBlocking(pThis->s.Core.pValidatorWrite, hThreadSelf, pSrcPos, true,
RT_INDEFINITE_WAIT, RTTHREADSTATE_RW_WRITE, false);
if (RT_SUCCESS(rc))
# else
RTTHREAD hThreadSelf = RTThreadSelf();
RTThreadBlocking(hThreadSelf, RTTHREADSTATE_RW_WRITE, false);
# endif
# endif
{
for (;;)
{
rc = SUPSemEventWaitNoResume(pThis->s.CTX_SUFF(pVM)->pSession,
(SUPSEMEVENT)pThis->s.Core.hEvtWrite,
RT_INDEFINITE_WAIT);
if ( rc != VERR_INTERRUPTED
|| pThis->s.Core.u32Magic != RTCRITSECTRW_MAGIC)
break;
# ifdef IN_RING0
pdmR0CritSectRwYieldToRing3(pThis);
# endif
}
# ifdef IN_RING3
RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_RW_WRITE);
# endif
if (pThis->s.Core.u32Magic != RTCRITSECTRW_MAGIC)
return VERR_SEM_DESTROYED;
}
if (RT_FAILURE(rc))
{
/* Decrement the counts and return the error. */
for (;;)
{
u64OldState = u64State = ASMAtomicReadU64(&pThis->s.Core.u64State);
uint64_t c = (u64State & RTCSRW_CNT_WR_MASK) >> RTCSRW_CNT_WR_SHIFT; Assert(c > 0);
c--;
u64State &= ~RTCSRW_CNT_WR_MASK;
u64State |= c << RTCSRW_CNT_WR_SHIFT;
if (ASMAtomicCmpXchgU64(&pThis->s.Core.u64State, u64State, u64OldState))
break;
}
return rc;
}
u64State = ASMAtomicReadU64(&pThis->s.Core.u64State);
if ((u64State & RTCSRW_DIR_MASK) == (RTCSRW_DIR_WRITE << RTCSRW_DIR_SHIFT))
{
ASMAtomicCmpXchgHandle(&pThis->s.Core.hNativeWriter, hNativeSelf, NIL_RTNATIVETHREAD, fDone);
if (fDone)
break;
}
AssertMsg(iLoop < 1000, ("%u\n", iLoop)); /* may loop a few times here... */
}
}
else
#endif /* IN_RING3 || IN_RING0 */
{
#ifdef IN_RING3
/* TryEnter call - decrement the number of (waiting) writers. */
#else
/* We cannot call SUPSemEventWaitNoResume in this context. Go back to
ring-3 and do it there or return rcBusy. */
#endif
for (;;)
{
u64OldState = u64State = ASMAtomicReadU64(&pThis->s.Core.u64State);
uint64_t c = (u64State & RTCSRW_CNT_WR_MASK) >> RTCSRW_CNT_WR_SHIFT; Assert(c > 0);
c--;
u64State &= ~RTCSRW_CNT_WR_MASK;
u64State |= c << RTCSRW_CNT_WR_SHIFT;
if (ASMAtomicCmpXchgU64(&pThis->s.Core.u64State, u64State, u64OldState))
break;
}
#ifdef IN_RING3
return VERR_SEM_BUSY;
#else
if (rcBusy == VINF_SUCCESS)
{
Assert(!fTryOnly);
PVM pVM = pThis->s.CTX_SUFF(pVM); AssertPtr(pVM);
PVMCPU pVCpu = VMMGetCpu(pVM); AssertPtr(pVCpu);
/** @todo Should actually do this in via VMMR0.cpp instead of going all the way
* back to ring-3. Goes for both kind of crit sects. */
return VMMRZCallRing3(pVM, pVCpu, VMMCALLRING3_PDM_CRIT_SECT_RW_ENTER_EXCL, MMHyperCCToR3(pVM, pThis));
}
return rcBusy;
#endif
}
}
/*
* Got it!
*/
Assert((ASMAtomicReadU64(&pThis->s.Core.u64State) & RTCSRW_DIR_MASK) == (RTCSRW_DIR_WRITE << RTCSRW_DIR_SHIFT));
ASMAtomicWriteU32(&pThis->s.Core.cWriteRecursions, 1);
Assert(pThis->s.Core.cWriterReads == 0);
#if defined(PDMCRITSECTRW_STRICT) && defined(IN_RING3)
if (!fNoVal)
RTLockValidatorRecExclSetOwner(pThis->s.Core.pValidatorWrite, hThreadSelf, pSrcPos, true);
#endif
STAM_REL_COUNTER_INC(&pThis->s.CTX_MID_Z(Stat,EnterExcl));
STAM_PROFILE_ADV_START(&pThis->s.StatWriteLocked, swl);
return VINF_SUCCESS;
}
/**
* Try enter a critical section with exclusive (write) access.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @retval rcBusy if in ring-0 or raw-mode context and it is busy.
* @retval VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
* @retval VERR_SEM_DESTROYED if the critical section is delete before or
* during the operation.
*
* @param pThis Pointer to the read/write critical section.
* @param rcBusy The status code to return when we're in RC or R0 and the
* section is busy. Pass VINF_SUCCESS to acquired the
* critical section thru a ring-3 call if necessary.
* @sa PDMCritSectRwEnterExclDebug, PDMCritSectRwTryEnterExcl,
* PDMCritSectRwTryEnterExclDebug,
* PDMCritSectEnterDebug, PDMCritSectEnter,
* RTCritSectRwEnterExcl.
*/
VMMDECL(int) PDMCritSectRwEnterExcl(PPDMCRITSECTRW pThis, int rcBusy)
{
#if !defined(PDMCRITSECTRW_STRICT) || !defined(IN_RING3)
return pdmCritSectRwEnterExcl(pThis, rcBusy, false /*fTryAgain*/, NULL, false /*fNoVal*/);
#else
RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
return pdmCritSectRwEnterExcl(pThis, rcBusy, false /*fTryAgain*/, &SrcPos, false /*fNoVal*/);
#endif
}
/**
* Try enter a critical section with exclusive (write) access.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @retval rcBusy if in ring-0 or raw-mode context and it is busy.
* @retval VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
* @retval VERR_SEM_DESTROYED if the critical section is delete before or
* during the operation.
*
* @param pThis Pointer to the read/write critical section.
* @param rcBusy The status code to return when we're in RC or R0 and the
* section is busy. Pass VINF_SUCCESS to acquired the
* critical section thru a ring-3 call if necessary.
* @param uId Where we're entering the section.
* @param SRC_POS The source position.
* @sa PDMCritSectRwEnterExcl, PDMCritSectRwTryEnterExcl,
* PDMCritSectRwTryEnterExclDebug,
* PDMCritSectEnterDebug, PDMCritSectEnter,
* RTCritSectRwEnterExclDebug.
*/
VMMDECL(int) PDMCritSectRwEnterExclDebug(PPDMCRITSECTRW pThis, int rcBusy, RTHCUINTPTR uId, RT_SRC_POS_DECL)
{
NOREF(uId); NOREF(pszFile); NOREF(iLine); NOREF(pszFunction);
#if !defined(PDMCRITSECTRW_STRICT) || !defined(IN_RING3)
return pdmCritSectRwEnterExcl(pThis, rcBusy, false /*fTryAgain*/, NULL, false /*fNoVal*/);
#else
RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
return pdmCritSectRwEnterExcl(pThis, rcBusy, false /*fTryAgain*/, &SrcPos, false /*fNoVal*/);
#endif
}
/**
* Try enter a critical section with exclusive (write) access.
*
* @retval VINF_SUCCESS on success.
* @retval VERR_SEM_BUSY if the critsect was owned.
* @retval VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
* @retval VERR_SEM_DESTROYED if the critical section is delete before or
* during the operation.
*
* @param pThis Pointer to the read/write critical section.
* @sa PDMCritSectRwEnterExcl, PDMCritSectRwTryEnterExclDebug,
* PDMCritSectRwEnterExclDebug,
* PDMCritSectTryEnter, PDMCritSectTryEnterDebug,
* RTCritSectRwTryEnterExcl.
*/
VMMDECL(int) PDMCritSectRwTryEnterExcl(PPDMCRITSECTRW pThis)
{
#if !defined(PDMCRITSECTRW_STRICT) || !defined(IN_RING3)
return pdmCritSectRwEnterExcl(pThis, VERR_SEM_BUSY, true /*fTryAgain*/, NULL, false /*fNoVal*/);
#else
RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
return pdmCritSectRwEnterExcl(pThis, VERR_SEM_BUSY, true /*fTryAgain*/, &SrcPos, false /*fNoVal*/);
#endif
}
/**
* Try enter a critical section with exclusive (write) access.
*
* @retval VINF_SUCCESS on success.
* @retval VERR_SEM_BUSY if the critsect was owned.
* @retval VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
* @retval VERR_SEM_DESTROYED if the critical section is delete before or
* during the operation.
*
* @param pThis Pointer to the read/write critical section.
* @param uId Where we're entering the section.
* @param SRC_POS The source position.
* @sa PDMCritSectRwTryEnterExcl, PDMCritSectRwEnterExcl,
* PDMCritSectRwEnterExclDebug,
* PDMCritSectTryEnterDebug, PDMCritSectTryEnter,
* RTCritSectRwTryEnterExclDebug.
*/
VMMDECL(int) PDMCritSectRwTryEnterExclDebug(PPDMCRITSECTRW pThis, RTHCUINTPTR uId, RT_SRC_POS_DECL)
{
NOREF(uId); NOREF(pszFile); NOREF(iLine); NOREF(pszFunction);
#if !defined(PDMCRITSECTRW_STRICT) || !defined(IN_RING3)
return pdmCritSectRwEnterExcl(pThis, VERR_SEM_BUSY, true /*fTryAgain*/, NULL, false /*fNoVal*/);
#else
RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
return pdmCritSectRwEnterExcl(pThis, VERR_SEM_BUSY, true /*fTryAgain*/, &SrcPos, false /*fNoVal*/);
#endif
}
#ifdef IN_RING3
/**
* Enters a PDM read/write critical section with exclusive (write) access.
*
* @returns VINF_SUCCESS if entered successfully.
* @retval VERR_SEM_DESTROYED if the critical section is delete before or
* during the operation.
*
* @param pThis Pointer to the read/write critical section.
* @param fCallRing3 Whether this is a VMMRZCallRing3()request.
*/
VMMR3DECL(int) PDMR3CritSectRwEnterExclEx(PPDMCRITSECTRW pThis, bool fCallRing3)
{
return pdmCritSectRwEnterExcl(pThis, VERR_SEM_BUSY, false /*fTryAgain*/, NULL, fCallRing3 /*fNoVal*/);
}
#endif /* IN_RING3 */
/**
* Leave a critical section held exclusively.
*
* @returns VBox status code.
* @retval VERR_SEM_DESTROYED if the critical section is delete before or
* during the operation.
* @param pThis Pointer to the read/write critical section.
* @param fNoVal No validation records (i.e. queued release).
* @sa PDMCritSectRwLeaveShared, RTCritSectRwLeaveExcl.
*/
static int pdmCritSectRwLeaveExclWorker(PPDMCRITSECTRW pThis, bool fNoVal)
{
/*
* Validate handle.
*/
AssertPtr(pThis);
AssertReturn(pThis->s.Core.u32Magic == RTCRITSECTRW_MAGIC, VERR_SEM_DESTROYED);
#if !defined(PDMCRITSECTRW_STRICT) || !defined(IN_RING3)
NOREF(fNoVal);
#endif
RTNATIVETHREAD hNativeSelf = pdmCritSectRwGetNativeSelf(pThis);
RTNATIVETHREAD hNativeWriter;
ASMAtomicUoReadHandle(&pThis->s.Core.hNativeWriter, &hNativeWriter);
AssertReturn(hNativeSelf == hNativeWriter, VERR_NOT_OWNER);
/*
* Unwind one recursion. Is it the final one?
*/
if (pThis->s.Core.cWriteRecursions == 1)
{
AssertReturn(pThis->s.Core.cWriterReads == 0, VERR_WRONG_ORDER); /* (must release all read recursions before the final write.) */
#if defined(PDMCRITSECTRW_STRICT) && defined(IN_RING3)
if (fNoVal)
Assert(pThis->s.Core.pValidatorWrite->hThread == NIL_RTTHREAD);
else
{
int rc9 = RTLockValidatorRecExclReleaseOwner(pThis->s.Core.pValidatorWrite, true);
if (RT_FAILURE(rc9))
return rc9;
}
#endif
/*
* Update the state.
*/
#if defined(IN_RING3) || defined(IN_RING0)
# ifdef IN_RING0
if ( RTThreadPreemptIsEnabled(NIL_RTTHREAD)
&& ASMIntAreEnabled())
# endif
{
ASMAtomicWriteU32(&pThis->s.Core.cWriteRecursions, 0);
STAM_PROFILE_ADV_STOP(&pThis->s.StatWriteLocked, swl);
ASMAtomicWriteHandle(&pThis->s.Core.hNativeWriter, NIL_RTNATIVETHREAD);
for (;;)
{
uint64_t u64State = ASMAtomicReadU64(&pThis->s.Core.u64State);
uint64_t u64OldState = u64State;
uint64_t c = (u64State & RTCSRW_CNT_WR_MASK) >> RTCSRW_CNT_WR_SHIFT;
Assert(c > 0);
c--;
if ( c > 0
|| (u64State & RTCSRW_CNT_RD_MASK) == 0)
{
/* Don't change the direction, wake up the next writer if any. */
u64State &= ~RTCSRW_CNT_WR_MASK;
u64State |= c << RTCSRW_CNT_WR_SHIFT;
if (ASMAtomicCmpXchgU64(&pThis->s.Core.u64State, u64State, u64OldState))
{
if (c > 0)
{
int rc = SUPSemEventSignal(pThis->s.CTX_SUFF(pVM)->pSession, (SUPSEMEVENT)pThis->s.Core.hEvtWrite);
AssertRC(rc);
}
break;
}
}
else
{
/* Reverse the direction and signal the reader threads. */
u64State &= ~(RTCSRW_CNT_WR_MASK | RTCSRW_DIR_MASK);
u64State |= RTCSRW_DIR_READ << RTCSRW_DIR_SHIFT;
if (ASMAtomicCmpXchgU64(&pThis->s.Core.u64State, u64State, u64OldState))
{
Assert(!pThis->s.Core.fNeedReset);
ASMAtomicWriteBool(&pThis->s.Core.fNeedReset, true);
int rc = SUPSemEventMultiSignal(pThis->s.CTX_SUFF(pVM)->pSession, (SUPSEMEVENTMULTI)pThis->s.Core.hEvtRead);
AssertRC(rc);
break;
}
}
ASMNopPause();
if (pThis->s.Core.u32Magic != RTCRITSECTRW_MAGIC)
return VERR_SEM_DESTROYED;
}
}
#endif /* IN_RING3 || IN_RING0 */
#ifndef IN_RING3
# ifdef IN_RING0
else
# endif
{
/*
* We cannot call neither SUPSemEventSignal nor SUPSemEventMultiSignal,
* so queue the exit request (ring-3).
*/
PVM pVM = pThis->s.CTX_SUFF(pVM); AssertPtr(pVM);
PVMCPU pVCpu = VMMGetCpu(pVM); AssertPtr(pVCpu);
uint32_t i = pVCpu->pdm.s.cQueuedCritSectRwExclLeaves++;
LogFlow(("PDMCritSectRwLeaveShared: [%d]=%p => R3\n", i, pThis));
AssertFatal(i < RT_ELEMENTS(pVCpu->pdm.s.apQueuedCritSectLeaves));
pVCpu->pdm.s.apQueuedCritSectRwExclLeaves[i] = MMHyperCCToR3(pVM, pThis);
VMCPU_FF_SET(pVCpu, VMCPU_FF_PDM_CRITSECT);
VMCPU_FF_SET(pVCpu, VMCPU_FF_TO_R3);
STAM_REL_COUNTER_INC(&pVM->pdm.s.StatQueuedCritSectLeaves);
STAM_REL_COUNTER_INC(&pThis->s.StatContentionRZLeaveExcl);
}
#endif
}
else
{
/*
* Not the final recursion.
*/
Assert(pThis->s.Core.cWriteRecursions != 0);
#if defined(PDMCRITSECTRW_STRICT) && defined(IN_RING3)
if (fNoVal)
Assert(pThis->s.Core.pValidatorWrite->hThread == NIL_RTTHREAD);
else
{
int rc9 = RTLockValidatorRecExclUnwind(pThis->s.Core.pValidatorWrite);
if (RT_FAILURE(rc9))
return rc9;
}
#endif
ASMAtomicDecU32(&pThis->s.Core.cWriteRecursions);
}
return VINF_SUCCESS;
}
/**
* Leave a critical section held exclusively.
*
* @returns VBox status code.
* @retval VERR_SEM_DESTROYED if the critical section is delete before or
* during the operation.
* @param pThis Pointer to the read/write critical section.
* @sa PDMCritSectRwLeaveShared, RTCritSectRwLeaveExcl.
*/
VMMDECL(int) PDMCritSectRwLeaveExcl(PPDMCRITSECTRW pThis)
{
return pdmCritSectRwLeaveExclWorker(pThis, false /*fNoVal*/);
}
#if defined(IN_RING3) || defined(IN_RING0)
/**
* PDMCritSectBothFF interface.
*
* @param pThis Pointer to the read/write critical section.
*/
void pdmCritSectRwLeaveExclQueued(PPDMCRITSECTRW pThis)
{
pdmCritSectRwLeaveExclWorker(pThis, true /*fNoVal*/);
}
#endif
/**
* Checks the caller is the exclusive (write) owner of the critical section.
*
* @retval true if owner.
* @retval false if not owner.
* @param pThis Pointer to the read/write critical section.
* @sa PDMCritSectRwIsReadOwner, PDMCritSectIsOwner,
* RTCritSectRwIsWriteOwner.
*/
VMMDECL(bool) PDMCritSectRwIsWriteOwner(PPDMCRITSECTRW pThis)
{
/*
* Validate handle.
*/
AssertPtr(pThis);
AssertReturn(pThis->s.Core.u32Magic == RTCRITSECTRW_MAGIC, false);
/*
* Check ownership.
*/
RTNATIVETHREAD hNativeWriter;
ASMAtomicUoReadHandle(&pThis->s.Core.hNativeWriter, &hNativeWriter);
if (hNativeWriter == NIL_RTNATIVETHREAD)
return false;
return hNativeWriter == pdmCritSectRwGetNativeSelf(pThis);
}
/**
* Checks if the caller is one of the read owners of the critical section.
*
* @note !CAUTION! This API doesn't work reliably if lock validation isn't
* enabled. Meaning, the answer is not trustworhty unless
* RT_LOCK_STRICT or PDMCRITSECTRW_STRICT was defined at build time.
* Also, make sure you do not use RTCRITSECTRW_FLAGS_NO_LOCK_VAL when
* creating the semaphore. And finally, if you used a locking class,
* don't disable deadlock detection by setting cMsMinDeadlock to
* RT_INDEFINITE_WAIT.
*
* In short, only use this for assertions.
*
* @returns @c true if reader, @c false if not.
* @param pThis Pointer to the read/write critical section.
* @param fWannaHear What you'd like to hear when lock validation is not
* available. (For avoiding asserting all over the place.)
* @sa PDMCritSectRwIsWriteOwner, RTCritSectRwIsReadOwner.
*/
VMMDECL(bool) PDMCritSectRwIsReadOwner(PPDMCRITSECTRW pThis, bool fWannaHear)
{
/*
* Validate handle.
*/
AssertPtr(pThis);
AssertReturn(pThis->s.Core.u32Magic == RTCRITSECTRW_MAGIC, false);
/*
* Inspect the state.
*/
uint64_t u64State = ASMAtomicReadU64(&pThis->s.Core.u64State);
if ((u64State & RTCSRW_DIR_MASK) == (RTCSRW_DIR_WRITE << RTCSRW_DIR_SHIFT))
{
/*
* It's in write mode, so we can only be a reader if we're also the
* current writer.
*/
RTNATIVETHREAD hWriter;
ASMAtomicUoReadHandle(&pThis->s.Core.hNativeWriter, &hWriter);
if (hWriter == NIL_RTNATIVETHREAD)
return false;
return hWriter == pdmCritSectRwGetNativeSelf(pThis);
}
/*
* Read mode. If there are no current readers, then we cannot be a reader.
*/
if (!(u64State & RTCSRW_CNT_RD_MASK))
return false;
#if defined(PDMCRITSECTRW_STRICT) && defined(IN_RING3)
/*
* Ask the lock validator.
* Note! It doesn't know everything, let's deal with that if it becomes an issue...
*/
NOREF(fWannaHear);
return RTLockValidatorRecSharedIsOwner(pThis->s.Core.pValidatorRead, NIL_RTTHREAD);
#else
/*
* Ok, we don't know, just tell the caller what he want to hear.
*/
return fWannaHear;
#endif
}
/**
* Gets the write recursion count.
*
* @returns The write recursion count (0 if bad critsect).
* @param pThis Pointer to the read/write critical section.
* @sa PDMCritSectRwGetWriterReadRecursion, PDMCritSectRwGetReadCount,
* RTCritSectRwGetWriteRecursion.
*/
VMMDECL(uint32_t) PDMCritSectRwGetWriteRecursion(PPDMCRITSECTRW pThis)
{
/*
* Validate handle.
*/
AssertPtr(pThis);
AssertReturn(pThis->s.Core.u32Magic == RTCRITSECTRW_MAGIC, 0);
/*
* Return the requested data.
*/
return pThis->s.Core.cWriteRecursions;
}
/**
* Gets the read recursion count of the current writer.
*
* @returns The read recursion count (0 if bad critsect).
* @param pThis Pointer to the read/write critical section.
* @sa PDMCritSectRwGetWriteRecursion, PDMCritSectRwGetReadCount,
* RTCritSectRwGetWriterReadRecursion.
*/
VMMDECL(uint32_t) PDMCritSectRwGetWriterReadRecursion(PPDMCRITSECTRW pThis)
{
/*
* Validate handle.
*/
AssertPtr(pThis);
AssertReturn(pThis->s.Core.u32Magic == RTCRITSECTRW_MAGIC, 0);
/*
* Return the requested data.
*/
return pThis->s.Core.cWriterReads;
}
/**
* Gets the current number of reads.
*
* This includes all read recursions, so it might be higher than the number of
* read owners. It does not include reads done by the current writer.
*
* @returns The read count (0 if bad critsect).
* @param pThis Pointer to the read/write critical section.
* @sa PDMCritSectRwGetWriteRecursion, PDMCritSectRwGetWriterReadRecursion,
* RTCritSectRwGetReadCount.
*/
VMMDECL(uint32_t) PDMCritSectRwGetReadCount(PPDMCRITSECTRW pThis)
{
/*
* Validate input.
*/
AssertPtr(pThis);
AssertReturn(pThis->s.Core.u32Magic == RTCRITSECTRW_MAGIC, 0);
/*
* Return the requested data.
*/
uint64_t u64State = ASMAtomicReadU64(&pThis->s.Core.u64State);
if ((u64State & RTCSRW_DIR_MASK) != (RTCSRW_DIR_READ << RTCSRW_DIR_SHIFT))
return 0;
return (u64State & RTCSRW_CNT_RD_MASK) >> RTCSRW_CNT_RD_SHIFT;
}
/**
* Checks if the read/write critical section is initialized or not.
*
* @retval true if initialized.
* @retval false if not initialized.
* @param pThis Pointer to the read/write critical section.
* @sa PDMCritSectIsInitialized, RTCritSectRwIsInitialized.
*/
VMMDECL(bool) PDMCritSectRwIsInitialized(PCPDMCRITSECTRW pThis)
{
AssertPtr(pThis);
return pThis->s.Core.u32Magic == RTCRITSECTRW_MAGIC;
}
|