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
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#if defined(lint) || defined(__lint)
#include <sys/types.h>
#include <sys/thread.h>
#include <sys/cpuvar.h>
#include <vm/page.h>
#else /* __lint */
#include "assym.h"
#endif /* __lint */
#include <sys/mutex_impl.h>
#include <sys/asm_linkage.h>
#include <sys/asm_misc.h>
#include <sys/regset.h>
#include <sys/rwlock_impl.h>
#include <sys/lockstat.h>
/*
* lock_try(lp), ulock_try(lp)
* - returns non-zero on success.
* - doesn't block interrupts so don't use this to spin on a lock.
*
* ulock_try() is for a lock in the user address space.
*/
#if defined(lint) || defined(__lint)
/* ARGSUSED */
int
lock_try(lock_t *lp)
{ return (0); }
/* ARGSUSED */
int
lock_spin_try(lock_t *lp)
{ return (0); }
/* ARGSUSED */
int
ulock_try(lock_t *lp)
{ return (0); }
#else /* __lint */
.globl kernelbase
#if defined(__amd64)
ENTRY(lock_try)
movb $-1, %dl
movzbq %dl, %rax
xchgb %dl, (%rdi)
xorb %dl, %al
.lock_try_lockstat_patch_point:
ret
testb %al, %al
jnz 0f
ret
0:
movq %gs:CPU_THREAD, %rdx /* rdx = thread addr */
movq %rdi, %rsi /* rsi = lock addr */
movl $LS_LOCK_TRY_ACQUIRE, %edi /* edi = event */
jmp lockstat_wrapper
SET_SIZE(lock_try)
ENTRY(lock_spin_try)
movb $-1, %dl
movzbq %dl, %rax
xchgb %dl, (%rdi)
xorb %dl, %al
ret
SET_SIZE(lock_spin_try)
ENTRY(ulock_try)
#ifdef DEBUG
movq kernelbase(%rip), %rax
cmpq %rax, %rdi /* test uaddr < kernelbase */
jb ulock_pass /* uaddr < kernelbase, proceed */
movq %rdi, %r12 /* preserve lock ptr for debugging */
leaq .ulock_panic_msg(%rip), %rdi
pushq %rbp /* align stack properly */
movq %rsp, %rbp
xorl %eax, %eax /* clear for varargs */
call panic
#endif /* DEBUG */
ulock_pass:
movl $1, %eax
xchgb %al, (%rdi)
xorb $1, %al
ret
SET_SIZE(ulock_try)
#else
ENTRY(lock_try)
movl $1,%edx
movl 4(%esp),%ecx /* ecx = lock addr */
xorl %eax,%eax
xchgb %dl, (%ecx) /* using dl will avoid partial */
testb %dl,%dl /* stalls on P6 ? */
setz %al
.lock_try_lockstat_patch_point:
ret
movl %gs:CPU_THREAD, %edx /* edx = thread addr */
testl %eax, %eax
jz 0f
movl $LS_LOCK_TRY_ACQUIRE, %eax
jmp lockstat_wrapper
0:
ret
SET_SIZE(lock_try)
ENTRY(lock_spin_try)
movl $-1,%edx
movl 4(%esp),%ecx /* ecx = lock addr */
xorl %eax,%eax
xchgb %dl, (%ecx) /* using dl will avoid partial */
testb %dl,%dl /* stalls on P6 ? */
setz %al
ret
SET_SIZE(lock_spin_try)
ENTRY(ulock_try)
#ifdef DEBUG
movl kernelbase, %eax
cmpl %eax, 4(%esp) /* test uaddr < kernelbase */
jb ulock_pass /* uaddr < kernelbase, proceed */
pushl $.ulock_panic_msg
call panic
#endif /* DEBUG */
ulock_pass:
movl $1,%eax
movl 4(%esp),%ecx
xchgb %al, (%ecx)
xorb $1, %al
ret
SET_SIZE(ulock_try)
#endif /* !__amd64 */
#ifdef DEBUG
.data
.ulock_panic_msg:
.string "ulock_try: Argument is above kernelbase"
.text
#endif /* DEBUG */
#endif /* __lint */
/*
* lock_clear(lp)
* - unlock lock without changing interrupt priority level.
*/
#if defined(lint) || defined(__lint)
/* ARGSUSED */
void
lock_clear(lock_t *lp)
{}
/* ARGSUSED */
void
ulock_clear(lock_t *lp)
{}
#else /* __lint */
#if defined(__amd64)
ENTRY(lock_clear)
movb $0, (%rdi)
.lock_clear_lockstat_patch_point:
ret
movq %rdi, %rsi /* rsi = lock addr */
movq %gs:CPU_THREAD, %rdx /* rdx = thread addr */
movl $LS_LOCK_CLEAR_RELEASE, %edi /* edi = event */
jmp lockstat_wrapper
SET_SIZE(lock_clear)
ENTRY(ulock_clear)
#ifdef DEBUG
movq kernelbase(%rip), %rcx
cmpq %rcx, %rdi /* test uaddr < kernelbase */
jb ulock_clr /* uaddr < kernelbase, proceed */
leaq .ulock_clear_msg(%rip), %rdi
pushq %rbp /* align stack properly */
movq %rsp, %rbp
xorl %eax, %eax /* clear for varargs */
call panic
#endif
ulock_clr:
movb $0, (%rdi)
ret
SET_SIZE(ulock_clear)
#else
ENTRY(lock_clear)
movl 4(%esp), %eax
movb $0, (%eax)
.lock_clear_lockstat_patch_point:
ret
movl %gs:CPU_THREAD, %edx /* edx = thread addr */
movl %eax, %ecx /* ecx = lock pointer */
movl $LS_LOCK_CLEAR_RELEASE, %eax
jmp lockstat_wrapper
SET_SIZE(lock_clear)
ENTRY(ulock_clear)
#ifdef DEBUG
movl kernelbase, %ecx
cmpl %ecx, 4(%esp) /* test uaddr < kernelbase */
jb ulock_clr /* uaddr < kernelbase, proceed */
pushl $.ulock_clear_msg
call panic
#endif
ulock_clr:
movl 4(%esp),%eax
xorl %ecx,%ecx
movb %cl, (%eax)
ret
SET_SIZE(ulock_clear)
#endif /* !__amd64 */
#ifdef DEBUG
.data
.ulock_clear_msg:
.string "ulock_clear: Argument is above kernelbase"
.text
#endif /* DEBUG */
#endif /* __lint */
/*
* lock_set_spl(lock_t *lp, int new_pil, u_short *old_pil)
* Drops lp, sets pil to new_pil, stores old pil in *old_pil.
*/
#if defined(lint) || defined(__lint)
/* ARGSUSED */
void
lock_set_spl(lock_t *lp, int new_pil, u_short *old_pil)
{}
#else /* __lint */
#if defined(__amd64)
ENTRY(lock_set_spl)
pushq %rbp
movq %rsp, %rbp
subq $32, %rsp
movl %esi, 8(%rsp) /* save priority level */
movq %rdx, 16(%rsp) /* save old pil ptr */
movq %rdi, 24(%rsp) /* save lock pointer */
movl %esi, %edi /* pass priority level */
call splr /* raise priority level */
movq 24(%rsp), %rdi /* rdi = lock addr */
movb $-1, %dl
xchgb %dl, (%rdi) /* try to set lock */
testb %dl, %dl /* did we get the lock? ... */
jnz .lss_miss /* ... no, go to C for the hard case */
movq 16(%rsp), %rdx /* rdx = old pil addr */
movw %ax, (%rdx) /* store old pil */
leave
.lock_set_spl_lockstat_patch_point:
ret
movq %rdi, %rsi /* rsi = lock addr */
movq %gs:CPU_THREAD, %rdx /* rdx = thread addr */
movl $LS_LOCK_SET_SPL_ACQUIRE, %edi
jmp lockstat_wrapper
.lss_miss:
movl 8(%rsp), %esi /* new_pil */
movq 16(%rsp), %rdx /* old_pil_addr */
movl %eax, %ecx /* original pil */
leave /* unwind stack */
jmp lock_set_spl_spin
SET_SIZE(lock_set_spl)
#else
ENTRY(lock_set_spl)
movl 8(%esp), %eax /* get priority level */
pushl %eax
call splr /* raise priority level */
movl 8(%esp), %ecx /* ecx = lock addr */
movl $-1, %edx
addl $4, %esp
xchgb %dl, (%ecx) /* try to set lock */
testb %dl, %dl /* did we get the lock? ... */
movl 12(%esp), %edx /* edx = olp pil addr (ZF unaffected) */
jnz .lss_miss /* ... no, go to C for the hard case */
movw %ax, (%edx) /* store old pil */
.lock_set_spl_lockstat_patch_point:
ret
movl %gs:CPU_THREAD, %edx /* edx = thread addr*/
movl $LS_LOCK_SET_SPL_ACQUIRE, %eax
jmp lockstat_wrapper
.lss_miss:
pushl %eax /* original pil */
pushl %edx /* old_pil addr */
pushl 16(%esp) /* new_pil */
pushl %ecx /* lock addr */
call lock_set_spl_spin
addl $16, %esp
ret
SET_SIZE(lock_set_spl)
#endif /* !__amd64 */
#endif /* __lint */
/*
* void
* lock_init(lp)
*/
#if defined(__lint)
/* ARGSUSED */
void
lock_init(lock_t *lp)
{}
#else /* __lint */
#if defined(__amd64)
ENTRY(lock_init)
movb $0, (%rdi)
ret
SET_SIZE(lock_init)
#else
ENTRY(lock_init)
movl 4(%esp), %eax
movb $0, (%eax)
ret
SET_SIZE(lock_init)
#endif /* !__amd64 */
#endif /* __lint */
/*
* void
* lock_set(lp)
*/
#if defined(lint) || defined(__lint)
/* ARGSUSED */
void
lock_set(lock_t *lp)
{}
#else /* __lint */
#if defined(__amd64)
ENTRY(lock_set)
movb $-1, %dl
xchgb %dl, (%rdi) /* try to set lock */
testb %dl, %dl /* did we get it? */
jnz lock_set_spin /* no, go to C for the hard case */
.lock_set_lockstat_patch_point:
ret
movq %rdi, %rsi /* rsi = lock addr */
movq %gs:CPU_THREAD, %rdx /* rdx = thread addr */
movl $LS_LOCK_SET_ACQUIRE, %edi
jmp lockstat_wrapper
SET_SIZE(lock_set)
#else
ENTRY(lock_set)
movl 4(%esp), %ecx /* ecx = lock addr */
movl $-1, %edx
xchgb %dl, (%ecx) /* try to set lock */
testb %dl, %dl /* did we get it? */
jnz lock_set_spin /* no, go to C for the hard case */
.lock_set_lockstat_patch_point:
ret
movl %gs:CPU_THREAD, %edx /* edx = thread addr */
movl $LS_LOCK_SET_ACQUIRE, %eax
jmp lockstat_wrapper
SET_SIZE(lock_set)
#endif /* !__amd64 */
#endif /* __lint */
/*
* lock_clear_splx(lp, s)
*/
#if defined(lint) || defined(__lint)
/* ARGSUSED */
void
lock_clear_splx(lock_t *lp, int s)
{}
#else /* __lint */
#if defined(__amd64)
ENTRY(lock_clear_splx)
movb $0, (%rdi) /* clear lock */
.lock_clear_splx_lockstat_patch_point:
jmp 0f
0:
movl %esi, %edi /* arg for splx */
jmp splx /* let splx do its thing */
.lock_clear_splx_lockstat:
pushq %rbp /* align stack properly */
movq %rsp, %rbp
subq $16, %rsp /* space to save args across splx */
movq %rdi, 8(%rsp) /* save lock ptr across splx call */
movl %esi, %edi /* arg for splx */
call splx /* lower the priority */
movq 8(%rsp), %rsi /* rsi = lock ptr */
leave /* unwind stack */
movq %gs:CPU_THREAD, %rdx /* rdx = thread addr */
movl $LS_LOCK_CLEAR_SPLX_RELEASE, %edi
jmp lockstat_wrapper
SET_SIZE(lock_clear_splx)
#else
ENTRY(lock_clear_splx)
movl 4(%esp), %eax /* eax = lock addr */
movb $0, (%eax) /* clear lock */
.lock_clear_splx_lockstat_patch_point:
jmp 0f
0:
movl 8(%esp), %edx /* edx = desired pil */
movl %edx, 4(%esp) /* set spl arg up for splx */
jmp splx /* let splx do it's thing */
.lock_clear_splx_lockstat:
movl 8(%esp), %edx /* edx = desired pil */
pushl %ebp /* set up stack frame */
movl %esp, %ebp
pushl %edx
call splx
leave /* unwind stack */
movl 4(%esp), %ecx /* ecx = lock pointer */
movl %gs:CPU_THREAD, %edx /* edx = thread addr */
movl $LS_LOCK_CLEAR_SPLX_RELEASE, %eax
jmp lockstat_wrapper
SET_SIZE(lock_clear_splx)
#endif /* !__amd64 */
#if defined(__GNUC_AS__)
#define LOCK_CLEAR_SPLX_LOCKSTAT_PATCH_VAL \
(.lock_clear_splx_lockstat - .lock_clear_splx_lockstat_patch_point - 2)
#define LOCK_CLEAR_SPLX_LOCKSTAT_PATCH_POINT \
(.lock_clear_splx_lockstat_patch_point + 1)
#else
#define LOCK_CLEAR_SPLX_LOCKSTAT_PATCH_VAL \
[.lock_clear_splx_lockstat - .lock_clear_splx_lockstat_patch_point - 2]
#define LOCK_CLEAR_SPLX_LOCKSTAT_PATCH_POINT \
[.lock_clear_splx_lockstat_patch_point + 1]
#endif
#endif /* __lint */
/*
* mutex_enter() and mutex_exit().
*
* These routines handle the simple cases of mutex_enter() (adaptive
* lock, not held) and mutex_exit() (adaptive lock, held, no waiters).
* If anything complicated is going on we punt to mutex_vector_enter().
*
* mutex_tryenter() is similar to mutex_enter() but returns zero if
* the lock cannot be acquired, nonzero on success.
*
* If mutex_exit() gets preempted in the window between checking waiters
* and clearing the lock, we can miss wakeups. Disabling preemption
* in the mutex code is prohibitively expensive, so instead we detect
* mutex preemption by examining the trapped PC in the interrupt path.
* If we interrupt a thread in mutex_exit() that has not yet cleared
* the lock, cmnint() resets its PC back to the beginning of
* mutex_exit() so it will check again for waiters when it resumes.
*
* The lockstat code below is activated when the lockstat driver
* calls lockstat_hot_patch() to hot-patch the kernel mutex code.
* Note that we don't need to test lockstat_event_mask here -- we won't
* patch this code in unless we're gathering ADAPTIVE_HOLD lockstats.
*/
#if defined(lint) || defined(__lint)
/* ARGSUSED */
void
mutex_enter(kmutex_t *lp)
{}
/* ARGSUSED */
int
mutex_tryenter(kmutex_t *lp)
{ return (0); }
/* ARGSUSED */
int
mutex_adaptive_tryenter(mutex_impl_t *lp)
{ return (0); }
/* ARGSUSED */
void
mutex_exit(kmutex_t *lp)
{}
#else
#if defined(__amd64)
ENTRY_NP(mutex_enter)
movq %gs:CPU_THREAD, %rdx /* rdx = thread ptr */
xorl %eax, %eax /* rax = 0 (unheld adaptive) */
lock
cmpxchgq %rdx, (%rdi)
jnz mutex_vector_enter
.mutex_enter_lockstat_patch_point:
#if defined(OPTERON_WORKAROUND_6323525)
.mutex_enter_6323525_patch_point:
ret /* nop space for lfence */
nop
nop
.mutex_enter_lockstat_6323525_patch_point: /* new patch point if lfence */
nop
#else /* OPTERON_WORKAROUND_6323525 */
ret
#endif /* OPTERON_WORKAROUND_6323525 */
movq %rdi, %rsi
movl $LS_MUTEX_ENTER_ACQUIRE, %edi
/*
* expects %rdx=thread, %rsi=lock, %edi=lockstat event
*/
ALTENTRY(lockstat_wrapper)
incb T_LOCKSTAT(%rdx) /* curthread->t_lockstat++ */
leaq lockstat_probemap(%rip), %rax
movl (%rax, %rdi, DTRACE_IDSIZE), %eax
testl %eax, %eax /* check for non-zero probe */
jz 1f
pushq %rbp /* align stack properly */
movq %rsp, %rbp
movl %eax, %edi
call *lockstat_probe
leave /* unwind stack */
1:
movq %gs:CPU_THREAD, %rdx /* reload thread ptr */
decb T_LOCKSTAT(%rdx) /* curthread->t_lockstat-- */
movl $1, %eax /* return success if tryenter */
ret
SET_SIZE(lockstat_wrapper)
SET_SIZE(mutex_enter)
/*
* expects %rcx=thread, %rdx=arg, %rsi=lock, %edi=lockstat event
*/
ENTRY(lockstat_wrapper_arg)
incb T_LOCKSTAT(%rcx) /* curthread->t_lockstat++ */
leaq lockstat_probemap(%rip), %rax
movl (%rax, %rdi, DTRACE_IDSIZE), %eax
testl %eax, %eax /* check for non-zero probe */
jz 1f
pushq %rbp /* align stack properly */
movq %rsp, %rbp
movl %eax, %edi
call *lockstat_probe
leave /* unwind stack */
1:
movq %gs:CPU_THREAD, %rdx /* reload thread ptr */
decb T_LOCKSTAT(%rdx) /* curthread->t_lockstat-- */
movl $1, %eax /* return success if tryenter */
ret
SET_SIZE(lockstat_wrapper_arg)
ENTRY(mutex_tryenter)
movq %gs:CPU_THREAD, %rdx /* rdx = thread ptr */
xorl %eax, %eax /* rax = 0 (unheld adaptive) */
lock
cmpxchgq %rdx, (%rdi)
jnz mutex_vector_tryenter
not %eax /* return success (nonzero) */
#if defined(OPTERON_WORKAROUND_6323525)
.mutex_tryenter_lockstat_patch_point:
.mutex_tryenter_6323525_patch_point:
ret /* nop space for lfence */
nop
nop
.mutex_tryenter_lockstat_6323525_patch_point: /* new patch point if lfence */
nop
#else /* OPTERON_WORKAROUND_6323525 */
.mutex_tryenter_lockstat_patch_point:
ret
#endif /* OPTERON_WORKAROUND_6323525 */
movq %rdi, %rsi
movl $LS_MUTEX_ENTER_ACQUIRE, %edi
jmp lockstat_wrapper
SET_SIZE(mutex_tryenter)
ENTRY(mutex_adaptive_tryenter)
movq %gs:CPU_THREAD, %rdx /* rdx = thread ptr */
xorl %eax, %eax /* rax = 0 (unheld adaptive) */
lock
cmpxchgq %rdx, (%rdi)
jnz 0f
not %eax /* return success (nonzero) */
#if defined(OPTERON_WORKAROUND_6323525)
.mutex_atryenter_6323525_patch_point:
ret /* nop space for lfence */
nop
nop
nop
#else /* OPTERON_WORKAROUND_6323525 */
ret
#endif /* OPTERON_WORKAROUND_6323525 */
0:
xorl %eax, %eax /* return failure */
ret
SET_SIZE(mutex_adaptive_tryenter)
.globl mutex_owner_running_critical_start
ENTRY(mutex_owner_running)
mutex_owner_running_critical_start:
movq (%rdi), %r11 /* get owner field */
andq $MUTEX_THREAD, %r11 /* remove waiters bit */
cmpq $0, %r11 /* if free, skip */
je 1f /* go return 0 */
movq T_CPU(%r11), %r8 /* get owner->t_cpu */
movq CPU_THREAD(%r8), %r9 /* get t_cpu->cpu_thread */
.mutex_owner_running_critical_end:
cmpq %r11, %r9 /* owner == running thread? */
je 2f /* yes, go return cpu */
1:
xorq %rax, %rax /* return 0 */
ret
2:
movq %r8, %rax /* return cpu */
ret
SET_SIZE(mutex_owner_running)
.globl mutex_owner_running_critical_size
.type mutex_owner_running_critical_size, @object
.align CPTRSIZE
mutex_owner_running_critical_size:
.quad .mutex_owner_running_critical_end - mutex_owner_running_critical_start
SET_SIZE(mutex_owner_running_critical_size)
.globl mutex_exit_critical_start
ENTRY(mutex_exit)
mutex_exit_critical_start: /* If interrupted, restart here */
movq %gs:CPU_THREAD, %rdx
cmpq %rdx, (%rdi)
jne mutex_vector_exit /* wrong type or wrong owner */
movq $0, (%rdi) /* clear owner AND lock */
.mutex_exit_critical_end:
.mutex_exit_lockstat_patch_point:
ret
movq %rdi, %rsi
movl $LS_MUTEX_EXIT_RELEASE, %edi
jmp lockstat_wrapper
SET_SIZE(mutex_exit)
.globl mutex_exit_critical_size
.type mutex_exit_critical_size, @object
.align CPTRSIZE
mutex_exit_critical_size:
.quad .mutex_exit_critical_end - mutex_exit_critical_start
SET_SIZE(mutex_exit_critical_size)
#else
ENTRY_NP(mutex_enter)
movl %gs:CPU_THREAD, %edx /* edx = thread ptr */
movl 4(%esp), %ecx /* ecx = lock ptr */
xorl %eax, %eax /* eax = 0 (unheld adaptive) */
lock
cmpxchgl %edx, (%ecx)
jnz mutex_vector_enter
#if defined(OPTERON_WORKAROUND_6323525)
.mutex_enter_lockstat_patch_point:
.mutex_enter_6323525_patch_point:
ret /* nop space for lfence */
nop
nop
.mutex_enter_lockstat_6323525_patch_point: /* new patch point if lfence */
nop
#else /* OPTERON_WORKAROUND_6323525 */
.mutex_enter_lockstat_patch_point:
ret
#endif /* OPTERON_WORKAROUND_6323525 */
movl $LS_MUTEX_ENTER_ACQUIRE, %eax
ALTENTRY(lockstat_wrapper) /* expects edx=thread, ecx=lock, */
/* eax=lockstat event */
pushl %ebp /* buy a frame */
movl %esp, %ebp
incb T_LOCKSTAT(%edx) /* curthread->t_lockstat++ */
pushl %edx /* save thread pointer */
movl $lockstat_probemap, %edx
movl (%edx, %eax, DTRACE_IDSIZE), %eax
testl %eax, %eax /* check for non-zero probe */
jz 1f
pushl %ecx /* push lock */
pushl %eax /* push probe ID */
call *lockstat_probe
addl $8, %esp
1:
popl %edx /* restore thread pointer */
decb T_LOCKSTAT(%edx) /* curthread->t_lockstat-- */
movl $1, %eax /* return success if tryenter */
popl %ebp /* pop off frame */
ret
SET_SIZE(lockstat_wrapper)
SET_SIZE(mutex_enter)
ENTRY(lockstat_wrapper_arg) /* expects edx=thread, ecx=lock, */
/* eax=lockstat event, pushed arg */
incb T_LOCKSTAT(%edx) /* curthread->t_lockstat++ */
pushl %edx /* save thread pointer */
movl $lockstat_probemap, %edx
movl (%edx, %eax, DTRACE_IDSIZE), %eax
testl %eax, %eax /* check for non-zero probe */
jz 1f
pushl %ebp /* save %ebp */
pushl 8(%esp) /* push arg1 */
movl %ebp, 12(%esp) /* fake up the stack frame */
movl %esp, %ebp /* fake up base pointer */
addl $12, %ebp /* adjust faked base pointer */
pushl %ecx /* push lock */
pushl %eax /* push probe ID */
call *lockstat_probe
addl $12, %esp /* adjust for arguments */
popl %ebp /* pop frame */
1:
popl %edx /* restore thread pointer */
decb T_LOCKSTAT(%edx) /* curthread->t_lockstat-- */
movl $1, %eax /* return success if tryenter */
addl $4, %esp /* pop argument */
ret
SET_SIZE(lockstat_wrapper_arg)
ENTRY(mutex_tryenter)
movl %gs:CPU_THREAD, %edx /* edx = thread ptr */
movl 4(%esp), %ecx /* ecx = lock ptr */
xorl %eax, %eax /* eax = 0 (unheld adaptive) */
lock
cmpxchgl %edx, (%ecx)
jnz mutex_vector_tryenter
movl %ecx, %eax
#if defined(OPTERON_WORKAROUND_6323525)
.mutex_tryenter_lockstat_patch_point:
.mutex_tryenter_6323525_patch_point:
ret /* nop space for lfence */
nop
nop
.mutex_tryenter_lockstat_6323525_patch_point: /* new patch point if lfence */
nop
#else /* OPTERON_WORKAROUND_6323525 */
.mutex_tryenter_lockstat_patch_point:
ret
#endif /* OPTERON_WORKAROUND_6323525 */
movl $LS_MUTEX_ENTER_ACQUIRE, %eax
jmp lockstat_wrapper
SET_SIZE(mutex_tryenter)
ENTRY(mutex_adaptive_tryenter)
movl %gs:CPU_THREAD, %edx /* edx = thread ptr */
movl 4(%esp), %ecx /* ecx = lock ptr */
xorl %eax, %eax /* eax = 0 (unheld adaptive) */
lock
cmpxchgl %edx, (%ecx)
jnz 0f
movl %ecx, %eax
#if defined(OPTERON_WORKAROUND_6323525)
.mutex_atryenter_6323525_patch_point:
ret /* nop space for lfence */
nop
nop
nop
#else /* OPTERON_WORKAROUND_6323525 */
ret
#endif /* OPTERON_WORKAROUND_6323525 */
0:
xorl %eax, %eax
ret
SET_SIZE(mutex_adaptive_tryenter)
.globl mutex_owner_running_critical_start
ENTRY(mutex_owner_running)
mutex_owner_running_critical_start:
movl 4(%esp), %eax /* get owner field */
movl (%eax), %eax
andl $MUTEX_THREAD, %eax /* remove waiters bit */
cmpl $0, %eax /* if free, skip */
je 1f /* go return 0 */
movl T_CPU(%eax), %ecx /* get owner->t_cpu */
movl CPU_THREAD(%ecx), %edx /* get t_cpu->cpu_thread */
.mutex_owner_running_critical_end:
cmpl %eax, %edx /* owner == running thread? */
je 2f /* yes, go return cpu */
1:
xorl %eax, %eax /* return 0 */
ret
2:
movl %ecx, %eax /* return cpu */
ret
SET_SIZE(mutex_owner_running)
.globl mutex_owner_running_critical_size
.type mutex_owner_running_critical_size, @object
.align CPTRSIZE
mutex_owner_running_critical_size:
.long .mutex_owner_running_critical_end - mutex_owner_running_critical_start
SET_SIZE(mutex_owner_running_critical_size)
.globl mutex_exit_critical_start
ENTRY(mutex_exit)
mutex_exit_critical_start: /* If interrupted, restart here */
movl %gs:CPU_THREAD, %edx
movl 4(%esp), %ecx
cmpl %edx, (%ecx)
jne mutex_vector_exit /* wrong type or wrong owner */
movl $0, (%ecx) /* clear owner AND lock */
.mutex_exit_critical_end:
.mutex_exit_lockstat_patch_point:
ret
movl $LS_MUTEX_EXIT_RELEASE, %eax
jmp lockstat_wrapper
SET_SIZE(mutex_exit)
.globl mutex_exit_critical_size
.type mutex_exit_critical_size, @object
.align CPTRSIZE
mutex_exit_critical_size:
.long .mutex_exit_critical_end - mutex_exit_critical_start
SET_SIZE(mutex_exit_critical_size)
#endif /* !__amd64 */
#endif /* __lint */
/*
* rw_enter() and rw_exit().
*
* These routines handle the simple cases of rw_enter (write-locking an unheld
* lock or read-locking a lock that's neither write-locked nor write-wanted)
* and rw_exit (no waiters or not the last reader). If anything complicated
* is going on we punt to rw_enter_sleep() and rw_exit_wakeup(), respectively.
*/
#if defined(lint) || defined(__lint)
/* ARGSUSED */
void
rw_enter(krwlock_t *lp, krw_t rw)
{}
/* ARGSUSED */
void
rw_exit(krwlock_t *lp)
{}
#else /* __lint */
#if defined(__amd64)
ENTRY(rw_enter)
movq %gs:CPU_THREAD, %rdx /* rdx = thread ptr */
cmpl $RW_WRITER, %esi
je .rw_write_enter
incl T_KPRI_REQ(%rdx) /* THREAD_KPRI_REQUEST() */
movq (%rdi), %rax /* rax = old rw_wwwh value */
testl $RW_WRITE_LOCKED|RW_WRITE_WANTED, %eax
jnz rw_enter_sleep
leaq RW_READ_LOCK(%rax), %rdx /* rdx = new rw_wwwh value */
lock
cmpxchgq %rdx, (%rdi) /* try to grab read lock */
jnz rw_enter_sleep
.rw_read_enter_lockstat_patch_point:
ret
movq %gs:CPU_THREAD, %rcx /* rcx = thread ptr */
movq %rdi, %rsi /* rsi = lock ptr */
movl $LS_RW_ENTER_ACQUIRE, %edi
movl $RW_READER, %edx
jmp lockstat_wrapper_arg
.rw_write_enter:
orq $RW_WRITE_LOCKED, %rdx /* rdx = write-locked value */
xorl %eax, %eax /* rax = unheld value */
lock
cmpxchgq %rdx, (%rdi) /* try to grab write lock */
jnz rw_enter_sleep
#if defined(OPTERON_WORKAROUND_6323525)
.rw_write_enter_lockstat_patch_point:
.rw_write_enter_6323525_patch_point:
ret
nop
nop
.rw_write_enter_lockstat_6323525_patch_point:
nop
#else /* OPTERON_WORKAROUND_6323525 */
.rw_write_enter_lockstat_patch_point:
ret
#endif /* OPTERON_WORKAROUND_6323525 */
movq %gs:CPU_THREAD, %rcx /* rcx = thread ptr */
movq %rdi, %rsi /* rsi = lock ptr */
movl $LS_RW_ENTER_ACQUIRE, %edi
movl $RW_WRITER, %edx
jmp lockstat_wrapper_arg
SET_SIZE(rw_enter)
ENTRY(rw_exit)
movq (%rdi), %rax /* rax = old rw_wwwh value */
cmpl $RW_READ_LOCK, %eax /* single-reader, no waiters? */
jne .rw_not_single_reader
xorl %edx, %edx /* rdx = new value (unheld) */
.rw_read_exit:
lock
cmpxchgq %rdx, (%rdi) /* try to drop read lock */
jnz rw_exit_wakeup
movq %gs:CPU_THREAD, %rcx /* rcx = thread ptr */
decl T_KPRI_REQ(%rcx) /* THREAD_KPRI_RELEASE() */
.rw_read_exit_lockstat_patch_point:
ret
movq %rdi, %rsi /* rsi = lock ptr */
movl $LS_RW_EXIT_RELEASE, %edi
movl $RW_READER, %edx
jmp lockstat_wrapper_arg
.rw_not_single_reader:
testl $RW_WRITE_LOCKED, %eax /* write-locked or write-wanted? */
jnz .rw_write_exit
leaq -RW_READ_LOCK(%rax), %rdx /* rdx = new value */
cmpl $RW_READ_LOCK, %edx
jge .rw_read_exit /* not last reader, safe to drop */
jmp rw_exit_wakeup /* last reader with waiters */
.rw_write_exit:
movq %gs:CPU_THREAD, %rax /* rax = thread ptr */
xorl %edx, %edx /* rdx = new value (unheld) */
orq $RW_WRITE_LOCKED, %rax /* eax = write-locked value */
lock
cmpxchgq %rdx, (%rdi) /* try to drop read lock */
jnz rw_exit_wakeup
.rw_write_exit_lockstat_patch_point:
ret
movq %gs:CPU_THREAD, %rcx /* rcx = thread ptr */
movq %rdi, %rsi /* rsi - lock ptr */
movl $LS_RW_EXIT_RELEASE, %edi
movl $RW_WRITER, %edx
jmp lockstat_wrapper_arg
SET_SIZE(rw_exit)
#else
ENTRY(rw_enter)
movl %gs:CPU_THREAD, %edx /* edx = thread ptr */
movl 4(%esp), %ecx /* ecx = lock ptr */
cmpl $RW_WRITER, 8(%esp)
je .rw_write_enter
incl T_KPRI_REQ(%edx) /* THREAD_KPRI_REQUEST() */
movl (%ecx), %eax /* eax = old rw_wwwh value */
testl $RW_WRITE_LOCKED|RW_WRITE_WANTED, %eax
jnz rw_enter_sleep
leal RW_READ_LOCK(%eax), %edx /* edx = new rw_wwwh value */
lock
cmpxchgl %edx, (%ecx) /* try to grab read lock */
jnz rw_enter_sleep
.rw_read_enter_lockstat_patch_point:
ret
movl %gs:CPU_THREAD, %edx /* edx = thread ptr */
movl $LS_RW_ENTER_ACQUIRE, %eax
pushl $RW_READER
jmp lockstat_wrapper_arg
.rw_write_enter:
orl $RW_WRITE_LOCKED, %edx /* edx = write-locked value */
xorl %eax, %eax /* eax = unheld value */
lock
cmpxchgl %edx, (%ecx) /* try to grab write lock */
jnz rw_enter_sleep
#if defined(OPTERON_WORKAROUND_6323525)
.rw_write_enter_lockstat_patch_point:
.rw_write_enter_6323525_patch_point:
ret
nop
nop
.rw_write_enter_lockstat_6323525_patch_point:
nop
#else /* OPTERON_WORKAROUND_6323525 */
.rw_write_enter_lockstat_patch_point:
ret
#endif /* OPTERON_WORKAROUND_6323525 */
movl %gs:CPU_THREAD, %edx /* edx = thread ptr */
movl $LS_RW_ENTER_ACQUIRE, %eax
pushl $RW_WRITER
jmp lockstat_wrapper_arg
SET_SIZE(rw_enter)
ENTRY(rw_exit)
movl 4(%esp), %ecx /* ecx = lock ptr */
movl (%ecx), %eax /* eax = old rw_wwwh value */
cmpl $RW_READ_LOCK, %eax /* single-reader, no waiters? */
jne .rw_not_single_reader
xorl %edx, %edx /* edx = new value (unheld) */
.rw_read_exit:
lock
cmpxchgl %edx, (%ecx) /* try to drop read lock */
jnz rw_exit_wakeup
movl %gs:CPU_THREAD, %edx /* edx = thread ptr */
decl T_KPRI_REQ(%edx) /* THREAD_KPRI_RELEASE() */
.rw_read_exit_lockstat_patch_point:
ret
movl $LS_RW_EXIT_RELEASE, %eax
pushl $RW_READER
jmp lockstat_wrapper_arg
.rw_not_single_reader:
testl $RW_WRITE_LOCKED, %eax /* write-locked or write-wanted? */
jnz .rw_write_exit
leal -RW_READ_LOCK(%eax), %edx /* edx = new value */
cmpl $RW_READ_LOCK, %edx
jge .rw_read_exit /* not last reader, safe to drop */
jmp rw_exit_wakeup /* last reader with waiters */
.rw_write_exit:
movl %gs:CPU_THREAD, %eax /* eax = thread ptr */
xorl %edx, %edx /* edx = new value (unheld) */
orl $RW_WRITE_LOCKED, %eax /* eax = write-locked value */
lock
cmpxchgl %edx, (%ecx) /* try to drop read lock */
jnz rw_exit_wakeup
.rw_write_exit_lockstat_patch_point:
ret
movl %gs:CPU_THREAD, %edx /* edx = thread ptr */
movl $LS_RW_EXIT_RELEASE, %eax
pushl $RW_WRITER
jmp lockstat_wrapper_arg
SET_SIZE(rw_exit)
#endif /* !__amd64 */
#endif /* __lint */
#if defined(OPTERON_WORKAROUND_6323525)
#if defined(lint) || defined(__lint)
int workaround_6323525_patched;
void
patch_workaround_6323525(void)
{}
#else /* lint */
/*
* If it is necessary to patch the lock enter routines with the lfence
* workaround, workaround_6323525_patched is set to a non-zero value so that
* the lockstat_hat_patch routine can patch to the new location of the 'ret'
* instruction.
*/
DGDEF3(workaround_6323525_patched, 4, 4)
.long 0
#if defined(__amd64)
#define HOT_MUTEX_PATCH(srcaddr, dstaddr, size) \
movq $size, %rbx; \
movq $dstaddr, %r13; \
addq %rbx, %r13; \
movq $srcaddr, %r12; \
addq %rbx, %r12; \
0: \
decq %r13; \
decq %r12; \
movzbl (%r12), %esi; \
movq $1, %rdx; \
movq %r13, %rdi; \
call hot_patch_kernel_text; \
decq %rbx; \
testq %rbx, %rbx; \
jg 0b;
/*
* patch_workaround_6323525: provide workaround for 6323525
*
* The workaround is to place a fencing instruction (lfence) between the
* mutex operation and the subsequent read-modify-write instruction.
*
* This routine hot patches the lfence instruction on top of the space
* reserved by nops in the lock enter routines.
*/
ENTRY_NP(patch_workaround_6323525)
pushq %rbp
movq %rsp, %rbp
pushq %r12
pushq %r13
pushq %rbx
/*
* lockstat_hot_patch() to use the alternate lockstat workaround
* 6323525 patch points (points past the lfence instruction to the
* new ret) when workaround_6323525_patched is set.
*/
movl $1, workaround_6323525_patched
/*
* patch ret/nop/nop/nop to lfence/ret at the end of the lock enter
* routines. The 4 bytes are patched in reverse order so that the
* the existing ret is overwritten last. This provides lock enter
* sanity during the intermediate patching stages.
*/
HOT_MUTEX_PATCH(_lfence_insn, .mutex_enter_6323525_patch_point, 4)
HOT_MUTEX_PATCH(_lfence_insn, .mutex_tryenter_6323525_patch_point, 4)
HOT_MUTEX_PATCH(_lfence_insn, .mutex_atryenter_6323525_patch_point, 4)
HOT_MUTEX_PATCH(_lfence_insn, .rw_write_enter_6323525_patch_point, 4)
popq %rbx
popq %r13
popq %r12
movq %rbp, %rsp
popq %rbp
ret
_lfence_insn:
lfence
ret
SET_SIZE(patch_workaround_6323525)
#else /* __amd64 */
#define HOT_MUTEX_PATCH(srcaddr, dstaddr, size) \
movl $size, %ebx; \
movl $srcaddr, %esi; \
addl %ebx, %esi; \
movl $dstaddr, %edi; \
addl %ebx, %edi; \
0: \
decl %esi; \
decl %edi; \
pushl $1; \
movzbl (%esi), %eax; \
pushl %eax; \
pushl %edi; \
call hot_patch_kernel_text; \
addl $12, %esp; \
decl %ebx; \
testl %ebx, %ebx; \
jg 0b;
/* see comments above */
ENTRY_NP(patch_workaround_6323525)
pushl %ebp
movl %esp, %ebp
pushl %ebx
pushl %esi
pushl %edi
movl $1, workaround_6323525_patched
HOT_MUTEX_PATCH(_lfence_insn, .mutex_enter_6323525_patch_point, 4)
HOT_MUTEX_PATCH(_lfence_insn, .mutex_tryenter_6323525_patch_point, 4)
HOT_MUTEX_PATCH(_lfence_insn, .mutex_atryenter_6323525_patch_point, 4)
HOT_MUTEX_PATCH(_lfence_insn, .rw_write_enter_6323525_patch_point, 4)
popl %edi
popl %esi
popl %ebx
movl %ebp, %esp
popl %ebp
ret
_lfence_insn:
.byte 0xf, 0xae, 0xe8 / [lfence instruction]
ret
SET_SIZE(patch_workaround_6323525)
#endif /* !__amd64 */
#endif /* !lint */
#endif /* OPTERON_WORKAROUND_6323525 */
#if defined(lint) || defined(__lint)
void
lockstat_hot_patch(void)
{}
#else
#if defined(__amd64)
#define HOT_PATCH(addr, event, active_instr, normal_instr, len) \
movq $normal_instr, %rsi; \
movq $active_instr, %rdi; \
leaq lockstat_probemap(%rip), %rax; \
movl _MUL(event, DTRACE_IDSIZE)(%rax), %eax; \
testl %eax, %eax; \
jz 9f; \
movq %rdi, %rsi; \
9: \
movq $len, %rdx; \
movq $addr, %rdi; \
call hot_patch_kernel_text
#else
#define HOT_PATCH(addr, event, active_instr, normal_instr, len) \
movl $normal_instr, %ecx; \
movl $active_instr, %edx; \
movl $lockstat_probemap, %eax; \
movl _MUL(event, DTRACE_IDSIZE)(%eax), %eax; \
testl %eax, %eax; \
jz . + 4; \
movl %edx, %ecx; \
pushl $len; \
pushl %ecx; \
pushl $addr; \
call hot_patch_kernel_text; \
addl $12, %esp;
#endif /* !__amd64 */
ENTRY(lockstat_hot_patch)
#if defined(__amd64)
pushq %rbp /* align stack properly */
movq %rsp, %rbp
#endif /* __amd64 */
#if defined(OPTERON_WORKAROUND_6323525)
cmpl $0, workaround_6323525_patched
je 1f
HOT_PATCH(.mutex_enter_lockstat_6323525_patch_point,
LS_MUTEX_ENTER_ACQUIRE, NOP_INSTR, RET_INSTR, 1)
HOT_PATCH(.mutex_tryenter_lockstat_6323525_patch_point,
LS_MUTEX_ENTER_ACQUIRE, NOP_INSTR, RET_INSTR, 1)
HOT_PATCH(.rw_write_enter_lockstat_6323525_patch_point,
LS_RW_ENTER_ACQUIRE, NOP_INSTR, RET_INSTR, 1)
jmp 2f
1:
HOT_PATCH(.mutex_enter_lockstat_patch_point,
LS_MUTEX_ENTER_ACQUIRE, NOP_INSTR, RET_INSTR, 1)
HOT_PATCH(.mutex_tryenter_lockstat_patch_point,
LS_MUTEX_ENTER_ACQUIRE, NOP_INSTR, RET_INSTR, 1)
HOT_PATCH(.rw_write_enter_lockstat_patch_point,
LS_RW_ENTER_ACQUIRE, NOP_INSTR, RET_INSTR, 1)
2:
#else /* OPTERON_WORKAROUND_6323525 */
HOT_PATCH(.mutex_enter_lockstat_patch_point,
LS_MUTEX_ENTER_ACQUIRE, NOP_INSTR, RET_INSTR, 1)
HOT_PATCH(.mutex_tryenter_lockstat_patch_point,
LS_MUTEX_ENTER_ACQUIRE, NOP_INSTR, RET_INSTR, 1)
HOT_PATCH(.rw_write_enter_lockstat_patch_point,
LS_RW_ENTER_ACQUIRE, NOP_INSTR, RET_INSTR, 1)
#endif /* !OPTERON_WORKAROUND_6323525 */
HOT_PATCH(.mutex_exit_lockstat_patch_point,
LS_MUTEX_EXIT_RELEASE, NOP_INSTR, RET_INSTR, 1)
HOT_PATCH(.rw_read_enter_lockstat_patch_point,
LS_RW_ENTER_ACQUIRE, NOP_INSTR, RET_INSTR, 1)
HOT_PATCH(.rw_write_exit_lockstat_patch_point,
LS_RW_EXIT_RELEASE, NOP_INSTR, RET_INSTR, 1)
HOT_PATCH(.rw_read_exit_lockstat_patch_point,
LS_RW_EXIT_RELEASE, NOP_INSTR, RET_INSTR, 1)
HOT_PATCH(.lock_set_lockstat_patch_point,
LS_LOCK_SET_ACQUIRE, NOP_INSTR, RET_INSTR, 1)
HOT_PATCH(.lock_try_lockstat_patch_point,
LS_LOCK_TRY_ACQUIRE, NOP_INSTR, RET_INSTR, 1)
HOT_PATCH(.lock_clear_lockstat_patch_point,
LS_LOCK_CLEAR_RELEASE, NOP_INSTR, RET_INSTR, 1)
HOT_PATCH(.lock_set_spl_lockstat_patch_point,
LS_LOCK_SET_SPL_ACQUIRE, NOP_INSTR, RET_INSTR, 1)
HOT_PATCH(LOCK_CLEAR_SPLX_LOCKSTAT_PATCH_POINT,
LS_LOCK_CLEAR_SPLX_RELEASE,
LOCK_CLEAR_SPLX_LOCKSTAT_PATCH_VAL, 0, 1);
#if defined(__amd64)
leave /* unwind stack */
#endif /* __amd64 */
ret
SET_SIZE(lockstat_hot_patch)
#endif /* __lint */
#if defined(lint) || defined(__lint)
/* XX64 membar_*() should be inlines */
void
membar_sync(void)
{}
void
membar_enter(void)
{}
void
membar_exit(void)
{}
void
membar_producer(void)
{}
void
membar_consumer(void)
{}
#else /* __lint */
#if defined(__amd64)
ENTRY(membar_enter)
ALTENTRY(membar_exit)
ALTENTRY(membar_sync)
mfence /* lighter weight than lock; xorq $0,(%rsp) */
ret
SET_SIZE(membar_sync)
SET_SIZE(membar_exit)
SET_SIZE(membar_enter)
ENTRY(membar_producer)
sfence
ret
SET_SIZE(membar_producer)
ENTRY(membar_consumer)
lfence
ret
SET_SIZE(membar_consumer)
#else
ENTRY(membar_enter)
ALTENTRY(membar_exit)
ALTENTRY(membar_sync)
lock
xorl $0, (%esp)
ret
SET_SIZE(membar_sync)
SET_SIZE(membar_exit)
SET_SIZE(membar_enter)
/*
* On machines that support sfence and lfence, these
* memory barriers can be more precisely implemented
* without causing the whole world to stop
*/
ENTRY(membar_producer)
.globl _patch_sfence_ret
_patch_sfence_ret: /* c.f. membar #StoreStore */
lock
xorl $0, (%esp)
ret
SET_SIZE(membar_producer)
ENTRY(membar_consumer)
.globl _patch_lfence_ret
_patch_lfence_ret: /* c.f. membar #LoadLoad */
lock
xorl $0, (%esp)
ret
SET_SIZE(membar_consumer)
#endif /* !__amd64 */
#endif /* __lint */
/*
* thread_onproc()
* Set thread in onproc state for the specified CPU.
* Also set the thread lock pointer to the CPU's onproc lock.
* Since the new lock isn't held, the store ordering is important.
* If not done in assembler, the compiler could reorder the stores.
*/
#if defined(lint) || defined(__lint)
void
thread_onproc(kthread_id_t t, cpu_t *cp)
{
t->t_state = TS_ONPROC;
t->t_lockp = &cp->cpu_thread_lock;
}
#else /* __lint */
#if defined(__amd64)
ENTRY(thread_onproc)
addq $CPU_THREAD_LOCK, %rsi /* pointer to disp_lock while running */
movl $ONPROC_THREAD, T_STATE(%rdi) /* set state to TS_ONPROC */
movq %rsi, T_LOCKP(%rdi) /* store new lock pointer */
ret
SET_SIZE(thread_onproc)
#else
ENTRY(thread_onproc)
movl 4(%esp), %eax
movl 8(%esp), %ecx
addl $CPU_THREAD_LOCK, %ecx /* pointer to disp_lock while running */
movl $ONPROC_THREAD, T_STATE(%eax) /* set state to TS_ONPROC */
movl %ecx, T_LOCKP(%eax) /* store new lock pointer */
ret
SET_SIZE(thread_onproc)
#endif /* !__amd64 */
#endif /* __lint */
/*
* mutex_delay_default(void)
* Spins for approx a few hundred processor cycles and returns to caller.
*/
#if defined(lint) || defined(__lint)
void
mutex_delay_default(void)
{}
#else /* __lint */
#if defined(__amd64)
ENTRY(mutex_delay_default)
movq $92,%r11
0: decq %r11
jg 0b
ret
SET_SIZE(mutex_delay_default)
#else
ENTRY(mutex_delay_default)
push %ebp
movl %esp,%ebp
andl $-16,%esp
push %ebx
movl $93,%ebx
0: decl %ebx
jg 0b
pop %ebx
leave
ret
SET_SIZE(mutex_delay_default)
#endif /* !__amd64 */
#endif /* __lint */
|