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
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
|
/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/param.h>
#include <sys/thread.h>
#include <sys/cpuvar.h>
#include <sys/inttypes.h>
#include <sys/cmn_err.h>
#include <sys/time.h>
#include <sys/mutex.h>
#include <sys/systm.h>
#include <sys/kcpc.h>
#include <sys/cpc_impl.h>
#include <sys/cpc_pcbe.h>
#include <sys/atomic.h>
#include <sys/sunddi.h>
#include <sys/modctl.h>
#include <sys/sdt.h>
#if defined(__x86)
#include <asm/clock.h>
#endif
kmutex_t kcpc_ctx_llock[CPC_HASH_BUCKETS]; /* protects ctx_list */
kcpc_ctx_t *kcpc_ctx_list[CPC_HASH_BUCKETS]; /* head of list */
krwlock_t kcpc_cpuctx_lock; /* lock for 'kcpc_cpuctx' below */
int kcpc_cpuctx; /* number of cpu-specific contexts */
int kcpc_counts_include_idle = 1; /* Project Private /etc/system variable */
/*
* These are set when a PCBE module is loaded.
*/
uint_t cpc_ncounters = 0;
pcbe_ops_t *pcbe_ops = NULL;
/*
* Statistics on (mis)behavior
*/
static uint32_t kcpc_intrctx_count; /* # overflows in an interrupt handler */
static uint32_t kcpc_nullctx_count; /* # overflows in a thread with no ctx */
/*
* Is misbehaviour (overflow in a thread with no context) fatal?
*/
#ifdef DEBUG
static int kcpc_nullctx_panic = 1;
#else
static int kcpc_nullctx_panic = 0;
#endif
static void kcpc_lwp_create(kthread_t *t, kthread_t *ct);
static void kcpc_restore(kcpc_ctx_t *ctx);
static void kcpc_save(kcpc_ctx_t *ctx);
static void kcpc_free(kcpc_ctx_t *ctx, int isexec);
static int kcpc_configure_reqs(kcpc_ctx_t *ctx, kcpc_set_t *set, int *subcode);
static void kcpc_free_configs(kcpc_set_t *set);
static kcpc_ctx_t *kcpc_ctx_alloc(void);
static void kcpc_ctx_clone(kcpc_ctx_t *ctx, kcpc_ctx_t *cctx);
static void kcpc_ctx_free(kcpc_ctx_t *ctx);
static int kcpc_assign_reqs(kcpc_set_t *set, kcpc_ctx_t *ctx);
static int kcpc_tryassign(kcpc_set_t *set, int starting_req, int *scratch);
static kcpc_set_t *kcpc_dup_set(kcpc_set_t *set);
void
kcpc_register_pcbe(pcbe_ops_t *ops)
{
pcbe_ops = ops;
cpc_ncounters = pcbe_ops->pcbe_ncounters();
}
int
kcpc_bind_cpu(kcpc_set_t *set, processorid_t cpuid, int *subcode)
{
cpu_t *cp;
kcpc_ctx_t *ctx;
int error;
ctx = kcpc_ctx_alloc();
if (kcpc_assign_reqs(set, ctx) != 0) {
kcpc_ctx_free(ctx);
*subcode = CPC_RESOURCE_UNAVAIL;
return (EINVAL);
}
ctx->kc_cpuid = cpuid;
ctx->kc_thread = curthread;
set->ks_data = kmem_zalloc(set->ks_nreqs * sizeof (uint64_t), KM_SLEEP);
if ((error = kcpc_configure_reqs(ctx, set, subcode)) != 0) {
kmem_free(set->ks_data, set->ks_nreqs * sizeof (uint64_t));
kcpc_ctx_free(ctx);
return (error);
}
set->ks_ctx = ctx;
ctx->kc_set = set;
/*
* We must hold cpu_lock to prevent DR, offlining, or unbinding while
* we are manipulating the cpu_t and programming the hardware, else the
* the cpu_t could go away while we're looking at it.
*/
mutex_enter(&cpu_lock);
cp = cpu_get(cpuid);
if (cp == NULL)
/*
* The CPU could have been DRd out while we were getting set up.
*/
goto unbound;
mutex_enter(&cp->cpu_cpc_ctxlock);
if (cp->cpu_cpc_ctx != NULL) {
/*
* If this CPU already has a bound set, return an error.
*/
mutex_exit(&cp->cpu_cpc_ctxlock);
goto unbound;
}
if (curthread->t_bind_cpu != cpuid) {
mutex_exit(&cp->cpu_cpc_ctxlock);
goto unbound;
}
cp->cpu_cpc_ctx = ctx;
/*
* Kernel preemption must be disabled while fiddling with the hardware
* registers to prevent partial updates.
*/
kpreempt_disable();
ctx->kc_rawtick = KCPC_GET_TICK();
pcbe_ops->pcbe_program(ctx);
kpreempt_enable();
mutex_exit(&cp->cpu_cpc_ctxlock);
mutex_exit(&cpu_lock);
return (0);
unbound:
mutex_exit(&cpu_lock);
set->ks_ctx = NULL;
kmem_free(set->ks_data, set->ks_nreqs * sizeof (uint64_t));
kcpc_ctx_free(ctx);
return (EAGAIN);
}
int
kcpc_bind_thread(kcpc_set_t *set, kthread_t *t, int *subcode)
{
kcpc_ctx_t *ctx;
int error;
/*
* Only one set is allowed per context, so ensure there is no
* existing context.
*/
if (t->t_cpc_ctx != NULL)
return (EEXIST);
ctx = kcpc_ctx_alloc();
/*
* The context must begin life frozen until it has been properly
* programmed onto the hardware. This prevents the context ops from
* worrying about it until we're ready.
*/
ctx->kc_flags |= KCPC_CTX_FREEZE;
ctx->kc_hrtime = gethrtime();
if (kcpc_assign_reqs(set, ctx) != 0) {
kcpc_ctx_free(ctx);
*subcode = CPC_RESOURCE_UNAVAIL;
return (EINVAL);
}
ctx->kc_cpuid = -1;
if (set->ks_flags & CPC_BIND_LWP_INHERIT)
ctx->kc_flags |= KCPC_CTX_LWPINHERIT;
ctx->kc_thread = t;
t->t_cpc_ctx = ctx;
/*
* Permit threads to look at their own hardware counters from userland.
*/
ctx->kc_flags |= KCPC_CTX_NONPRIV;
/*
* Create the data store for this set.
*/
set->ks_data = kmem_alloc(set->ks_nreqs * sizeof (uint64_t), KM_SLEEP);
if ((error = kcpc_configure_reqs(ctx, set, subcode)) != 0) {
kmem_free(set->ks_data, set->ks_nreqs * sizeof (uint64_t));
kcpc_ctx_free(ctx);
t->t_cpc_ctx = NULL;
return (error);
}
set->ks_ctx = ctx;
ctx->kc_set = set;
/*
* Add a device context to the subject thread.
*/
installctx(t, ctx, kcpc_save, kcpc_restore, NULL,
kcpc_lwp_create, NULL, kcpc_free);
/*
* Ask the backend to program the hardware.
*/
if (t == curthread) {
kpreempt_disable();
ctx->kc_rawtick = KCPC_GET_TICK();
atomic_and_uint(&ctx->kc_flags, ~KCPC_CTX_FREEZE);
pcbe_ops->pcbe_program(ctx);
kpreempt_enable();
} else
/*
* Since we are the agent LWP, we know the victim LWP is stopped
* until we're done here; no need to worry about preemption or
* migration here. We still use an atomic op to clear the flag
* to ensure the flags are always self-consistent; they can
* still be accessed from, for instance, another CPU doing a
* kcpc_invalidate_all().
*/
atomic_and_uint(&ctx->kc_flags, ~KCPC_CTX_FREEZE);
return (0);
}
/*
* Walk through each request in the set and ask the PCBE to configure a
* corresponding counter.
*/
static int
kcpc_configure_reqs(kcpc_ctx_t *ctx, kcpc_set_t *set, int *subcode)
{
int i;
int ret;
kcpc_request_t *rp;
for (i = 0; i < set->ks_nreqs; i++) {
int n;
rp = &set->ks_req[i];
n = rp->kr_picnum;
ASSERT(n >= 0 && n < cpc_ncounters);
ASSERT(ctx->kc_pics[n].kp_req == NULL);
if (rp->kr_flags & CPC_OVF_NOTIFY_EMT) {
if ((pcbe_ops->pcbe_caps & CPC_CAP_OVERFLOW_INTERRUPT)
== 0) {
*subcode = -1;
return (ENOTSUP);
}
/*
* If any of the counters have requested overflow
* notification, we flag the context as being one that
* cares about overflow.
*/
ctx->kc_flags |= KCPC_CTX_SIGOVF;
}
rp->kr_config = NULL;
if ((ret = pcbe_ops->pcbe_configure(n, rp->kr_event,
rp->kr_preset, rp->kr_flags, rp->kr_nattrs, rp->kr_attr,
&(rp->kr_config), (void *)ctx)) != 0) {
kcpc_free_configs(set);
*subcode = ret;
switch (ret) {
case CPC_ATTR_REQUIRES_PRIVILEGE:
case CPC_HV_NO_ACCESS:
return (EACCES);
default:
return (EINVAL);
}
}
ctx->kc_pics[n].kp_req = rp;
rp->kr_picp = &ctx->kc_pics[n];
rp->kr_data = set->ks_data + rp->kr_index;
*rp->kr_data = rp->kr_preset;
}
return (0);
}
static void
kcpc_free_configs(kcpc_set_t *set)
{
int i;
for (i = 0; i < set->ks_nreqs; i++)
if (set->ks_req[i].kr_config != NULL)
pcbe_ops->pcbe_free(set->ks_req[i].kr_config);
}
/*
* buf points to a user address and the data should be copied out to that
* address in the current process.
*/
int
kcpc_sample(kcpc_set_t *set, uint64_t *buf, hrtime_t *hrtime, uint64_t *tick)
{
kcpc_ctx_t *ctx = set->ks_ctx;
uint64_t curtick = KCPC_GET_TICK();
if (ctx == NULL)
return (EINVAL);
else if (ctx->kc_flags & KCPC_CTX_INVALID)
return (EAGAIN);
if ((ctx->kc_flags & KCPC_CTX_FREEZE) == 0) {
/*
* Kernel preemption must be disabled while reading the
* hardware regs, and if this is a CPU-bound context, while
* checking the CPU binding of the current thread.
*/
kpreempt_disable();
if (ctx->kc_cpuid != -1) {
if (curthread->t_bind_cpu != ctx->kc_cpuid) {
kpreempt_enable();
return (EAGAIN);
}
}
if (ctx->kc_thread == curthread) {
ctx->kc_hrtime = gethrtime();
pcbe_ops->pcbe_sample(ctx);
ctx->kc_vtick += curtick - ctx->kc_rawtick;
ctx->kc_rawtick = curtick;
}
kpreempt_enable();
/*
* The config may have been invalidated by
* the pcbe_sample op.
*/
if (ctx->kc_flags & KCPC_CTX_INVALID)
return (EAGAIN);
}
if (copyout(set->ks_data, buf,
set->ks_nreqs * sizeof (uint64_t)) == -1)
return (EFAULT);
if (copyout(&ctx->kc_hrtime, hrtime, sizeof (uint64_t)) == -1)
return (EFAULT);
if (copyout(&ctx->kc_vtick, tick, sizeof (uint64_t)) == -1)
return (EFAULT);
return (0);
}
/*
* Stop the counters on the CPU this context is bound to.
*/
static void
kcpc_stop_hw(kcpc_ctx_t *ctx)
{
cpu_t *cp;
ASSERT((ctx->kc_flags & (KCPC_CTX_INVALID | KCPC_CTX_INVALID_STOPPED))
== KCPC_CTX_INVALID);
kpreempt_disable();
cp = cpu_get(ctx->kc_cpuid);
ASSERT(cp != NULL);
if (cp == CPU) {
pcbe_ops->pcbe_allstop();
atomic_or_uint(&ctx->kc_flags,
KCPC_CTX_INVALID_STOPPED);
} else
kcpc_remote_stop(cp);
kpreempt_enable();
}
int
kcpc_unbind(kcpc_set_t *set)
{
kcpc_ctx_t *ctx = set->ks_ctx;
kthread_t *t;
if (ctx == NULL)
return (EINVAL);
atomic_or_uint(&ctx->kc_flags, KCPC_CTX_INVALID);
if (ctx->kc_cpuid == -1) {
t = ctx->kc_thread;
/*
* The context is thread-bound and therefore has a device
* context. It will be freed via removectx() calling
* freectx() calling kcpc_free().
*/
if (t == curthread &&
(ctx->kc_flags & KCPC_CTX_INVALID_STOPPED) == 0) {
kpreempt_disable();
pcbe_ops->pcbe_allstop();
atomic_or_uint(&ctx->kc_flags,
KCPC_CTX_INVALID_STOPPED);
kpreempt_enable();
}
#ifdef DEBUG
if (removectx(t, ctx, kcpc_save, kcpc_restore, NULL,
kcpc_lwp_create, NULL, kcpc_free) == 0)
panic("kcpc_unbind: context %p not preset on thread %p",
ctx, t);
#else
(void) removectx(t, ctx, kcpc_save, kcpc_restore, NULL,
kcpc_lwp_create, NULL, kcpc_free);
#endif /* DEBUG */
t->t_cpc_set = NULL;
t->t_cpc_ctx = NULL;
} else {
/*
* If we are unbinding a CPU-bound set from a remote CPU, the
* native CPU's idle thread could be in the midst of programming
* this context onto the CPU. We grab the context's lock here to
* ensure that the idle thread is done with it. When we release
* the lock, the CPU no longer has a context and the idle thread
* will move on.
*
* cpu_lock must be held to prevent the CPU from being DR'd out
* while we disassociate the context from the cpu_t.
*/
cpu_t *cp;
mutex_enter(&cpu_lock);
cp = cpu_get(ctx->kc_cpuid);
if (cp != NULL) {
/*
* The CPU may have been DR'd out of the system.
*/
mutex_enter(&cp->cpu_cpc_ctxlock);
if ((ctx->kc_flags & KCPC_CTX_INVALID_STOPPED) == 0)
kcpc_stop_hw(ctx);
ASSERT(ctx->kc_flags & KCPC_CTX_INVALID_STOPPED);
cp->cpu_cpc_ctx = NULL;
mutex_exit(&cp->cpu_cpc_ctxlock);
}
mutex_exit(&cpu_lock);
if (ctx->kc_thread == curthread) {
kcpc_free(ctx, 0);
curthread->t_cpc_set = NULL;
}
}
return (0);
}
int
kcpc_preset(kcpc_set_t *set, int index, uint64_t preset)
{
int i;
ASSERT(set != NULL);
ASSERT(set->ks_ctx != NULL);
ASSERT(set->ks_ctx->kc_thread == curthread);
ASSERT(set->ks_ctx->kc_cpuid == -1);
if (index < 0 || index >= set->ks_nreqs)
return (EINVAL);
for (i = 0; i < set->ks_nreqs; i++)
if (set->ks_req[i].kr_index == index)
break;
ASSERT(i != set->ks_nreqs);
set->ks_req[i].kr_preset = preset;
return (0);
}
int
kcpc_restart(kcpc_set_t *set)
{
kcpc_ctx_t *ctx = set->ks_ctx;
int i;
ASSERT(ctx != NULL);
ASSERT(ctx->kc_thread == curthread);
ASSERT(ctx->kc_cpuid == -1);
kpreempt_disable();
/*
* If the user is doing this on a running set, make sure the counters
* are stopped first.
*/
if ((ctx->kc_flags & KCPC_CTX_FREEZE) == 0)
pcbe_ops->pcbe_allstop();
for (i = 0; i < set->ks_nreqs; i++) {
*(set->ks_req[i].kr_data) = set->ks_req[i].kr_preset;
pcbe_ops->pcbe_configure(0, NULL, set->ks_req[i].kr_preset,
0, 0, NULL, &set->ks_req[i].kr_config, NULL);
}
/*
* Ask the backend to program the hardware.
*/
ctx->kc_rawtick = KCPC_GET_TICK();
atomic_and_uint(&ctx->kc_flags, ~KCPC_CTX_FREEZE);
pcbe_ops->pcbe_program(ctx);
kpreempt_enable();
return (0);
}
/*
* Caller must hold kcpc_cpuctx_lock.
*/
int
kcpc_enable(kthread_t *t, int cmd, int enable)
{
kcpc_ctx_t *ctx = t->t_cpc_ctx;
kcpc_set_t *set = t->t_cpc_set;
kcpc_set_t *newset;
int i;
int flag;
int err;
ASSERT(RW_READ_HELD(&kcpc_cpuctx_lock));
if (ctx == NULL) {
/*
* This thread has a set but no context; it must be a
* CPU-bound set.
*/
ASSERT(t->t_cpc_set != NULL);
ASSERT(t->t_cpc_set->ks_ctx->kc_cpuid != -1);
return (EINVAL);
} else if (ctx->kc_flags & KCPC_CTX_INVALID)
return (EAGAIN);
if (cmd == CPC_ENABLE) {
if ((ctx->kc_flags & KCPC_CTX_FREEZE) == 0)
return (EINVAL);
kpreempt_disable();
atomic_and_uint(&ctx->kc_flags, ~KCPC_CTX_FREEZE);
kcpc_restore(ctx);
kpreempt_enable();
} else if (cmd == CPC_DISABLE) {
if (ctx->kc_flags & KCPC_CTX_FREEZE)
return (EINVAL);
kpreempt_disable();
kcpc_save(ctx);
atomic_or_uint(&ctx->kc_flags, KCPC_CTX_FREEZE);
kpreempt_enable();
} else if (cmd == CPC_USR_EVENTS || cmd == CPC_SYS_EVENTS) {
/*
* Strategy for usr/sys: stop counters and update set's presets
* with current counter values, unbind, update requests with
* new config, then re-bind.
*/
flag = (cmd == CPC_USR_EVENTS) ?
CPC_COUNT_USER: CPC_COUNT_SYSTEM;
kpreempt_disable();
atomic_or_uint(&ctx->kc_flags,
KCPC_CTX_INVALID | KCPC_CTX_INVALID_STOPPED);
pcbe_ops->pcbe_allstop();
kpreempt_enable();
for (i = 0; i < set->ks_nreqs; i++) {
set->ks_req[i].kr_preset = *(set->ks_req[i].kr_data);
if (enable)
set->ks_req[i].kr_flags |= flag;
else
set->ks_req[i].kr_flags &= ~flag;
}
newset = kcpc_dup_set(set);
if (kcpc_unbind(set) != 0)
return (EINVAL);
t->t_cpc_set = newset;
if (kcpc_bind_thread(newset, t, &err) != 0) {
t->t_cpc_set = NULL;
kcpc_free_set(newset);
return (EINVAL);
}
} else
return (EINVAL);
return (0);
}
/*
* Provide PCBEs with a way of obtaining the configs of every counter which will
* be programmed together.
*
* If current is NULL, provide the first config.
*
* If data != NULL, caller wants to know where the data store associated with
* the config we return is located.
*/
void *
kcpc_next_config(void *token, void *current, uint64_t **data)
{
int i;
kcpc_pic_t *pic;
kcpc_ctx_t *ctx = (kcpc_ctx_t *)token;
if (current == NULL) {
/*
* Client would like the first config, which may not be in
* counter 0; we need to search through the counters for the
* first config.
*/
for (i = 0; i < cpc_ncounters; i++)
if (ctx->kc_pics[i].kp_req != NULL)
break;
/*
* There are no counters configured for the given context.
*/
if (i == cpc_ncounters)
return (NULL);
} else {
/*
* There surely is a faster way to do this.
*/
for (i = 0; i < cpc_ncounters; i++) {
pic = &ctx->kc_pics[i];
if (pic->kp_req != NULL &&
current == pic->kp_req->kr_config)
break;
}
/*
* We found the current config at picnum i. Now search for the
* next configured PIC.
*/
for (i++; i < cpc_ncounters; i++) {
pic = &ctx->kc_pics[i];
if (pic->kp_req != NULL)
break;
}
if (i == cpc_ncounters)
return (NULL);
}
if (data != NULL) {
*data = ctx->kc_pics[i].kp_req->kr_data;
}
return (ctx->kc_pics[i].kp_req->kr_config);
}
static kcpc_ctx_t *
kcpc_ctx_alloc(void)
{
kcpc_ctx_t *ctx;
long hash;
ctx = (kcpc_ctx_t *)kmem_alloc(sizeof (kcpc_ctx_t), KM_SLEEP);
hash = CPC_HASH_CTX(ctx);
mutex_enter(&kcpc_ctx_llock[hash]);
ctx->kc_next = kcpc_ctx_list[hash];
kcpc_ctx_list[hash] = ctx;
mutex_exit(&kcpc_ctx_llock[hash]);
ctx->kc_pics = (kcpc_pic_t *)kmem_zalloc(sizeof (kcpc_pic_t) *
cpc_ncounters, KM_SLEEP);
ctx->kc_flags = 0;
ctx->kc_vtick = 0;
ctx->kc_rawtick = 0;
ctx->kc_cpuid = -1;
return (ctx);
}
/*
* Copy set from ctx to the child context, cctx, if it has CPC_BIND_LWP_INHERIT
* in the flags.
*/
static void
kcpc_ctx_clone(kcpc_ctx_t *ctx, kcpc_ctx_t *cctx)
{
kcpc_set_t *ks = ctx->kc_set, *cks;
int i, j;
int code;
ASSERT(ks != NULL);
if ((ks->ks_flags & CPC_BIND_LWP_INHERIT) == 0)
return;
cks = kmem_alloc(sizeof (*cks), KM_SLEEP);
cctx->kc_set = cks;
cks->ks_flags = ks->ks_flags;
cks->ks_nreqs = ks->ks_nreqs;
cks->ks_req = kmem_alloc(cks->ks_nreqs *
sizeof (kcpc_request_t), KM_SLEEP);
cks->ks_data = kmem_alloc(cks->ks_nreqs * sizeof (uint64_t),
KM_SLEEP);
cks->ks_ctx = cctx;
for (i = 0; i < cks->ks_nreqs; i++) {
cks->ks_req[i].kr_index = ks->ks_req[i].kr_index;
cks->ks_req[i].kr_picnum = ks->ks_req[i].kr_picnum;
(void) strncpy(cks->ks_req[i].kr_event,
ks->ks_req[i].kr_event, CPC_MAX_EVENT_LEN);
cks->ks_req[i].kr_preset = ks->ks_req[i].kr_preset;
cks->ks_req[i].kr_flags = ks->ks_req[i].kr_flags;
cks->ks_req[i].kr_nattrs = ks->ks_req[i].kr_nattrs;
if (ks->ks_req[i].kr_nattrs > 0) {
cks->ks_req[i].kr_attr =
kmem_alloc(ks->ks_req[i].kr_nattrs *
sizeof (kcpc_attr_t), KM_SLEEP);
}
for (j = 0; j < ks->ks_req[i].kr_nattrs; j++) {
(void) strncpy(cks->ks_req[i].kr_attr[j].ka_name,
ks->ks_req[i].kr_attr[j].ka_name,
CPC_MAX_ATTR_LEN);
cks->ks_req[i].kr_attr[j].ka_val =
ks->ks_req[i].kr_attr[j].ka_val;
}
}
if (kcpc_configure_reqs(cctx, cks, &code) != 0)
kcpc_invalidate_config(cctx);
}
static void
kcpc_ctx_free(kcpc_ctx_t *ctx)
{
kcpc_ctx_t **loc;
long hash = CPC_HASH_CTX(ctx);
mutex_enter(&kcpc_ctx_llock[hash]);
loc = &kcpc_ctx_list[hash];
ASSERT(*loc != NULL);
while (*loc != ctx)
loc = &(*loc)->kc_next;
*loc = ctx->kc_next;
mutex_exit(&kcpc_ctx_llock[hash]);
kmem_free(ctx->kc_pics, cpc_ncounters * sizeof (kcpc_pic_t));
kmem_free(ctx, sizeof (*ctx));
}
/*
* Generic interrupt handler used on hardware that generates
* overflow interrupts.
*
* Note: executed at high-level interrupt context!
*/
/*ARGSUSED*/
kcpc_ctx_t *
kcpc_overflow_intr(caddr_t arg, uint64_t bitmap)
{
kcpc_ctx_t *ctx;
kthread_t *t = curthread;
int i;
/*
* On both x86 and UltraSPARC, we may deliver the high-level
* interrupt in kernel mode, just after we've started to run an
* interrupt thread. (That's because the hardware helpfully
* delivers the overflow interrupt some random number of cycles
* after the instruction that caused the overflow by which time
* we're in some part of the kernel, not necessarily running on
* the right thread).
*
* Check for this case here -- find the pinned thread
* that was running when the interrupt went off.
*/
if (t->t_flag & T_INTR_THREAD) {
klwp_t *lwp;
atomic_add_32(&kcpc_intrctx_count, 1);
/*
* Note that t_lwp is always set to point at the underlying
* thread, thus this will work in the presence of nested
* interrupts.
*/
ctx = NULL;
if ((lwp = t->t_lwp) != NULL) {
t = lwptot(lwp);
ctx = t->t_cpc_ctx;
}
} else
ctx = t->t_cpc_ctx;
if (ctx == NULL) {
/*
* This can easily happen if we're using the counters in
* "shared" mode, for example, and an overflow interrupt
* occurs while we are running cpustat. In that case, the
* bound thread that has the context that belongs to this
* CPU is almost certainly sleeping (if it was running on
* the CPU we'd have found it above), and the actual
* interrupted thread has no knowledge of performance counters!
*/
ctx = curthread->t_cpu->cpu_cpc_ctx;
if (ctx != NULL) {
/*
* Return the bound context for this CPU to
* the interrupt handler so that it can synchronously
* sample the hardware counters and restart them.
*/
return (ctx);
}
/*
* As long as the overflow interrupt really is delivered early
* enough after trapping into the kernel to avoid switching
* threads, we must always be able to find the cpc context,
* or something went terribly wrong i.e. we ended up
* running a passivated interrupt thread, a kernel
* thread or we interrupted idle, all of which are Very Bad.
*/
if (kcpc_nullctx_panic)
panic("null cpc context, thread %p", (void *)t);
atomic_add_32(&kcpc_nullctx_count, 1);
} else if ((ctx->kc_flags & KCPC_CTX_INVALID) == 0) {
/*
* Schedule an ast to sample the counters, which will
* propagate any overflow into the virtualized performance
* counter(s), and may deliver a signal.
*/
ttolwp(t)->lwp_pcb.pcb_flags |= CPC_OVERFLOW;
/*
* If a counter has overflowed which was counting on behalf of
* a request which specified CPC_OVF_NOTIFY_EMT, send the
* process a signal.
*/
for (i = 0; i < cpc_ncounters; i++) {
if (ctx->kc_pics[i].kp_req != NULL &&
bitmap & (1 << i) &&
ctx->kc_pics[i].kp_req->kr_flags &
CPC_OVF_NOTIFY_EMT) {
/*
* A signal has been requested for this PIC, so
* so freeze the context. The interrupt handler
* has already stopped the counter hardware.
*/
atomic_or_uint(&ctx->kc_flags, KCPC_CTX_FREEZE);
atomic_or_uint(&ctx->kc_pics[i].kp_flags,
KCPC_PIC_OVERFLOWED);
}
}
aston(t);
}
return (NULL);
}
/*
* The current thread context had an overflow interrupt; we're
* executing here in high-level interrupt context.
*/
/*ARGSUSED*/
uint_t
kcpc_hw_overflow_intr(caddr_t arg1, caddr_t arg2)
{
kcpc_ctx_t *ctx;
uint64_t bitmap;
if (pcbe_ops == NULL ||
(bitmap = pcbe_ops->pcbe_overflow_bitmap()) == 0)
return (DDI_INTR_UNCLAIMED);
/*
* Prevent any further interrupts.
*/
pcbe_ops->pcbe_allstop();
/*
* Invoke the "generic" handler.
*
* If the interrupt has occurred in the context of an lwp owning
* the counters, then the handler posts an AST to the lwp to
* trigger the actual sampling, and optionally deliver a signal or
* restart the counters, on the way out of the kernel using
* kcpc_hw_overflow_ast() (see below).
*
* On the other hand, if the handler returns the context to us
* directly, then it means that there are no other threads in
* the middle of updating it, no AST has been posted, and so we
* should sample the counters here, and restart them with no
* further fuss.
*/
if ((ctx = kcpc_overflow_intr(arg1, bitmap)) != NULL) {
uint64_t curtick = KCPC_GET_TICK();
ctx->kc_hrtime = gethrtime_waitfree();
ctx->kc_vtick += curtick - ctx->kc_rawtick;
ctx->kc_rawtick = curtick;
pcbe_ops->pcbe_sample(ctx);
pcbe_ops->pcbe_program(ctx);
}
return (DDI_INTR_CLAIMED);
}
/*
* Called from trap() when processing the ast posted by the high-level
* interrupt handler.
*/
int
kcpc_overflow_ast()
{
kcpc_ctx_t *ctx = curthread->t_cpc_ctx;
int i;
int found = 0;
uint64_t curtick = KCPC_GET_TICK();
ASSERT(ctx != NULL); /* Beware of interrupt skid. */
/*
* An overflow happened: sample the context to ensure that
* the overflow is propagated into the upper bits of the
* virtualized 64-bit counter(s).
*/
kpreempt_disable();
ctx->kc_hrtime = gethrtime_waitfree();
pcbe_ops->pcbe_sample(ctx);
kpreempt_enable();
ctx->kc_vtick += curtick - ctx->kc_rawtick;
/*
* The interrupt handler has marked any pics with KCPC_PIC_OVERFLOWED
* if that pic generated an overflow and if the request it was counting
* on behalf of had CPC_OVERFLOW_REQUEST specified. We go through all
* pics in the context and clear the KCPC_PIC_OVERFLOWED flags. If we
* found any overflowed pics, keep the context frozen and return true
* (thus causing a signal to be sent).
*/
for (i = 0; i < cpc_ncounters; i++) {
if (ctx->kc_pics[i].kp_flags & KCPC_PIC_OVERFLOWED) {
atomic_and_uint(&ctx->kc_pics[i].kp_flags,
~KCPC_PIC_OVERFLOWED);
found = 1;
}
}
if (found)
return (1);
/*
* Otherwise, re-enable the counters and continue life as before.
*/
kpreempt_disable();
atomic_and_uint(&ctx->kc_flags, ~KCPC_CTX_FREEZE);
pcbe_ops->pcbe_program(ctx);
kpreempt_enable();
return (0);
}
/*
* Called when switching away from current thread.
*/
static void
kcpc_save(kcpc_ctx_t *ctx)
{
if (ctx->kc_flags & KCPC_CTX_INVALID) {
if (ctx->kc_flags & KCPC_CTX_INVALID_STOPPED)
return;
/*
* This context has been invalidated but the counters have not
* been stopped. Stop them here and mark the context stopped.
*/
pcbe_ops->pcbe_allstop();
atomic_or_uint(&ctx->kc_flags, KCPC_CTX_INVALID_STOPPED);
return;
}
pcbe_ops->pcbe_allstop();
if (ctx->kc_flags & KCPC_CTX_FREEZE)
return;
/*
* Need to sample for all reqs into each req's current mpic.
*/
ctx->kc_hrtime = gethrtime();
ctx->kc_vtick += KCPC_GET_TICK() - ctx->kc_rawtick;
pcbe_ops->pcbe_sample(ctx);
}
static void
kcpc_restore(kcpc_ctx_t *ctx)
{
if ((ctx->kc_flags & (KCPC_CTX_INVALID | KCPC_CTX_INVALID_STOPPED)) ==
KCPC_CTX_INVALID)
/*
* The context is invalidated but has not been marked stopped.
* We mark it as such here because we will not start the
* counters during this context switch.
*/
atomic_or_uint(&ctx->kc_flags, KCPC_CTX_INVALID_STOPPED);
if (ctx->kc_flags & (KCPC_CTX_INVALID | KCPC_CTX_FREEZE))
return;
/*
* While programming the hardware, the counters should be stopped. We
* don't do an explicit pcbe_allstop() here because they should have
* been stopped already by the last consumer.
*/
ctx->kc_rawtick = KCPC_GET_TICK();
pcbe_ops->pcbe_program(ctx);
}
/*
* If kcpc_counts_include_idle is set to 0 by the sys admin, we add the the
* following context operators to the idle thread on each CPU. They stop the
* counters when the idle thread is switched on, and they start them again when
* it is switched off.
*/
/*ARGSUSED*/
void
kcpc_idle_save(struct cpu *cp)
{
/*
* The idle thread shouldn't be run anywhere else.
*/
ASSERT(CPU == cp);
/*
* We must hold the CPU's context lock to ensure the context isn't freed
* while we're looking at it.
*/
mutex_enter(&cp->cpu_cpc_ctxlock);
if ((cp->cpu_cpc_ctx == NULL) ||
(cp->cpu_cpc_ctx->kc_flags & KCPC_CTX_INVALID)) {
mutex_exit(&cp->cpu_cpc_ctxlock);
return;
}
pcbe_ops->pcbe_program(cp->cpu_cpc_ctx);
mutex_exit(&cp->cpu_cpc_ctxlock);
}
void
kcpc_idle_restore(struct cpu *cp)
{
/*
* The idle thread shouldn't be run anywhere else.
*/
ASSERT(CPU == cp);
/*
* We must hold the CPU's context lock to ensure the context isn't freed
* while we're looking at it.
*/
mutex_enter(&cp->cpu_cpc_ctxlock);
if ((cp->cpu_cpc_ctx == NULL) ||
(cp->cpu_cpc_ctx->kc_flags & KCPC_CTX_INVALID)) {
mutex_exit(&cp->cpu_cpc_ctxlock);
return;
}
pcbe_ops->pcbe_allstop();
mutex_exit(&cp->cpu_cpc_ctxlock);
}
/*ARGSUSED*/
static void
kcpc_lwp_create(kthread_t *t, kthread_t *ct)
{
kcpc_ctx_t *ctx = t->t_cpc_ctx, *cctx;
int i;
if (ctx == NULL || (ctx->kc_flags & KCPC_CTX_LWPINHERIT) == 0)
return;
rw_enter(&kcpc_cpuctx_lock, RW_READER);
if (ctx->kc_flags & KCPC_CTX_INVALID) {
rw_exit(&kcpc_cpuctx_lock);
return;
}
cctx = kcpc_ctx_alloc();
kcpc_ctx_clone(ctx, cctx);
rw_exit(&kcpc_cpuctx_lock);
/*
* Copy the parent context's kc_flags field, but don't overwrite
* the child's in case it was modified during kcpc_ctx_clone.
*/
cctx->kc_flags |= ctx->kc_flags;
cctx->kc_thread = ct;
cctx->kc_cpuid = -1;
ct->t_cpc_set = cctx->kc_set;
ct->t_cpc_ctx = cctx;
if (cctx->kc_flags & KCPC_CTX_SIGOVF) {
kcpc_set_t *ks = cctx->kc_set;
/*
* Our contract with the user requires us to immediately send an
* overflow signal to all children if we have the LWPINHERIT
* and SIGOVF flags set. In addition, all counters should be
* set to UINT64_MAX, and their pic's overflow flag turned on
* so that our trap() processing knows to send a signal.
*/
atomic_or_uint(&cctx->kc_flags, KCPC_CTX_FREEZE);
for (i = 0; i < ks->ks_nreqs; i++) {
kcpc_request_t *kr = &ks->ks_req[i];
if (kr->kr_flags & CPC_OVF_NOTIFY_EMT) {
*(kr->kr_data) = UINT64_MAX;
kr->kr_picp->kp_flags |= KCPC_PIC_OVERFLOWED;
}
}
ttolwp(ct)->lwp_pcb.pcb_flags |= CPC_OVERFLOW;
aston(ct);
}
installctx(ct, cctx, kcpc_save, kcpc_restore,
NULL, kcpc_lwp_create, NULL, kcpc_free);
}
/*
* Counter Stoppage Theory
*
* The counters may need to be stopped properly at the following occasions:
*
* 1) An LWP exits.
* 2) A thread exits.
* 3) An LWP performs an exec().
* 4) A bound set is unbound.
*
* In addition to stopping the counters, the CPC context (a kcpc_ctx_t) may need
* to be freed as well.
*
* Case 1: kcpc_passivate(), called via lwp_exit(), stops the counters. Later on
* when the thread is freed, kcpc_free(), called by freectx(), frees the
* context.
*
* Case 2: same as case 1 except kcpc_passivate is called from thread_exit().
*
* Case 3: kcpc_free(), called via freectx() via exec(), recognizes that it has
* been called from exec. It stops the counters _and_ frees the context.
*
* Case 4: kcpc_unbind() stops the hardware _and_ frees the context.
*
* CPU-bound counters are always stopped via kcpc_unbind().
*/
/*
* We're being called to delete the context; we ensure that all associated data
* structures are freed, and that the hardware is passivated if this is an exec.
*/
/*ARGSUSED*/
static void
kcpc_free(kcpc_ctx_t *ctx, int isexec)
{
int i;
kcpc_set_t *set = ctx->kc_set;
ASSERT(set != NULL);
atomic_or_uint(&ctx->kc_flags, KCPC_CTX_INVALID);
if (isexec) {
/*
* This thread is execing, and after the exec it should not have
* any performance counter context. Stop the counters properly
* here so the system isn't surprised by an overflow interrupt
* later.
*/
if (ctx->kc_cpuid != -1) {
cpu_t *cp;
/*
* CPU-bound context; stop the appropriate CPU's ctrs.
* Hold cpu_lock while examining the CPU to ensure it
* doesn't go away.
*/
mutex_enter(&cpu_lock);
cp = cpu_get(ctx->kc_cpuid);
/*
* The CPU could have been DR'd out, so only stop the
* CPU and clear its context pointer if the CPU still
* exists.
*/
if (cp != NULL) {
mutex_enter(&cp->cpu_cpc_ctxlock);
kcpc_stop_hw(ctx);
cp->cpu_cpc_ctx = NULL;
mutex_exit(&cp->cpu_cpc_ctxlock);
}
mutex_exit(&cpu_lock);
ASSERT(curthread->t_cpc_ctx == NULL);
} else {
/*
* Thread-bound context; stop _this_ CPU's counters.
*/
kpreempt_disable();
pcbe_ops->pcbe_allstop();
atomic_or_uint(&ctx->kc_flags,
KCPC_CTX_INVALID_STOPPED);
kpreempt_enable();
curthread->t_cpc_ctx = NULL;
}
/*
* Since we are being called from an exec and we know that
* exec is not permitted via the agent thread, we should clean
* up this thread's CPC state completely, and not leave dangling
* CPC pointers behind.
*/
ASSERT(ctx->kc_thread == curthread);
curthread->t_cpc_set = NULL;
}
/*
* Walk through each request in this context's set and free the PCBE's
* configuration if it exists.
*/
for (i = 0; i < set->ks_nreqs; i++) {
if (set->ks_req[i].kr_config != NULL)
pcbe_ops->pcbe_free(set->ks_req[i].kr_config);
}
kmem_free(set->ks_data, set->ks_nreqs * sizeof (uint64_t));
kcpc_ctx_free(ctx);
kcpc_free_set(set);
}
/*
* Free the memory associated with a request set.
*/
void
kcpc_free_set(kcpc_set_t *set)
{
int i;
kcpc_request_t *req;
ASSERT(set->ks_req != NULL);
for (i = 0; i < set->ks_nreqs; i++) {
req = &set->ks_req[i];
if (req->kr_nattrs != 0) {
kmem_free(req->kr_attr,
req->kr_nattrs * sizeof (kcpc_attr_t));
}
}
kmem_free(set->ks_req, sizeof (kcpc_request_t) * set->ks_nreqs);
kmem_free(set, sizeof (kcpc_set_t));
}
/*
* Grab every existing context and mark it as invalid.
*/
void
kcpc_invalidate_all(void)
{
kcpc_ctx_t *ctx;
long hash;
for (hash = 0; hash < CPC_HASH_BUCKETS; hash++) {
mutex_enter(&kcpc_ctx_llock[hash]);
for (ctx = kcpc_ctx_list[hash]; ctx; ctx = ctx->kc_next)
atomic_or_uint(&ctx->kc_flags, KCPC_CTX_INVALID);
mutex_exit(&kcpc_ctx_llock[hash]);
}
}
/*
* Interface for PCBEs to signal that an existing configuration has suddenly
* become invalid.
*/
void
kcpc_invalidate_config(void *token)
{
kcpc_ctx_t *ctx = token;
ASSERT(ctx != NULL);
atomic_or_uint(&ctx->kc_flags, KCPC_CTX_INVALID);
}
/*
* Called from lwp_exit() and thread_exit()
*/
void
kcpc_passivate(void)
{
kcpc_ctx_t *ctx = curthread->t_cpc_ctx;
kcpc_set_t *set = curthread->t_cpc_set;
if (set == NULL)
return;
/*
* We're cleaning up after this thread; ensure there are no dangling
* CPC pointers left behind. The context and set will be freed by
* freectx() in the case of an LWP-bound set, and by kcpc_unbind() in
* the case of a CPU-bound set.
*/
curthread->t_cpc_ctx = NULL;
if (ctx == NULL) {
/*
* This thread has a set but no context; it must be a CPU-bound
* set. The hardware will be stopped via kcpc_unbind() when the
* process exits and closes its file descriptors with
* kcpc_close(). Our only job here is to clean up this thread's
* state; the set will be freed with the unbind().
*/
(void) kcpc_unbind(set);
/*
* Unbinding a set belonging to the current thread should clear
* its set pointer.
*/
ASSERT(curthread->t_cpc_set == NULL);
return;
}
curthread->t_cpc_set = NULL;
/*
* This thread/LWP is exiting but context switches will continue to
* happen for a bit as the exit proceeds. Kernel preemption must be
* disabled here to prevent a race between checking or setting the
* INVALID_STOPPED flag here and kcpc_restore() setting the flag during
* a context switch.
*/
kpreempt_disable();
if ((ctx->kc_flags & KCPC_CTX_INVALID_STOPPED) == 0) {
pcbe_ops->pcbe_allstop();
atomic_or_uint(&ctx->kc_flags,
KCPC_CTX_INVALID | KCPC_CTX_INVALID_STOPPED);
}
kpreempt_enable();
}
/*
* Assign the requests in the given set to the PICs in the context.
* Returns 0 if successful, -1 on failure.
*/
/*ARGSUSED*/
static int
kcpc_assign_reqs(kcpc_set_t *set, kcpc_ctx_t *ctx)
{
int i;
int *picnum_save;
ASSERT(set->ks_nreqs <= cpc_ncounters);
/*
* Provide kcpc_tryassign() with scratch space to avoid doing an
* alloc/free with every invocation.
*/
picnum_save = kmem_alloc(set->ks_nreqs * sizeof (int), KM_SLEEP);
/*
* kcpc_tryassign() blindly walks through each request in the set,
* seeing if a counter can count its event. If yes, it assigns that
* counter. However, that counter may have been the only capable counter
* for _another_ request's event. The solution is to try every possible
* request first. Note that this does not cover all solutions, as
* that would require all unique orderings of requests, an n^n operation
* which would be unacceptable for architectures with many counters.
*/
for (i = 0; i < set->ks_nreqs; i++)
if (kcpc_tryassign(set, i, picnum_save) == 0)
break;
kmem_free(picnum_save, set->ks_nreqs * sizeof (int));
if (i == set->ks_nreqs)
return (-1);
return (0);
}
static int
kcpc_tryassign(kcpc_set_t *set, int starting_req, int *scratch)
{
int i;
int j;
uint64_t bitmap = 0, resmap = 0;
uint64_t ctrmap;
/*
* We are attempting to assign the reqs to pics, but we may fail. If we
* fail, we need to restore the state of the requests to what it was
* when we found it, as some reqs may have been explicitly assigned to
* a specific PIC beforehand. We do this by snapshotting the assignments
* now and restoring from it later if we fail.
*
* Also we note here which counters have already been claimed by
* requests with explicit counter assignments.
*/
for (i = 0; i < set->ks_nreqs; i++) {
scratch[i] = set->ks_req[i].kr_picnum;
if (set->ks_req[i].kr_picnum != -1)
resmap |= (1 << set->ks_req[i].kr_picnum);
}
/*
* Walk through requests assigning them to the first PIC that is
* capable.
*/
i = starting_req;
do {
if (set->ks_req[i].kr_picnum != -1) {
ASSERT((bitmap & (1 << set->ks_req[i].kr_picnum)) == 0);
bitmap |= (1 << set->ks_req[i].kr_picnum);
if (++i == set->ks_nreqs)
i = 0;
continue;
}
ctrmap = pcbe_ops->pcbe_event_coverage(set->ks_req[i].kr_event);
for (j = 0; j < cpc_ncounters; j++) {
if (ctrmap & (1 << j) && (bitmap & (1 << j)) == 0 &&
(resmap & (1 << j)) == 0) {
/*
* We can assign this counter because:
*
* 1. It can count the event (ctrmap)
* 2. It hasn't been assigned yet (bitmap)
* 3. It wasn't reserved by a request (resmap)
*/
bitmap |= (1 << j);
break;
}
}
if (j == cpc_ncounters) {
for (i = 0; i < set->ks_nreqs; i++)
set->ks_req[i].kr_picnum = scratch[i];
return (-1);
}
set->ks_req[i].kr_picnum = j;
if (++i == set->ks_nreqs)
i = 0;
} while (i != starting_req);
return (0);
}
kcpc_set_t *
kcpc_dup_set(kcpc_set_t *set)
{
kcpc_set_t *new;
int i;
int j;
new = kmem_alloc(sizeof (*new), KM_SLEEP);
new->ks_flags = set->ks_flags;
new->ks_nreqs = set->ks_nreqs;
new->ks_req = kmem_alloc(set->ks_nreqs * sizeof (kcpc_request_t),
KM_SLEEP);
new->ks_data = NULL;
new->ks_ctx = NULL;
for (i = 0; i < new->ks_nreqs; i++) {
new->ks_req[i].kr_config = NULL;
new->ks_req[i].kr_index = set->ks_req[i].kr_index;
new->ks_req[i].kr_picnum = set->ks_req[i].kr_picnum;
new->ks_req[i].kr_picp = NULL;
new->ks_req[i].kr_data = NULL;
(void) strncpy(new->ks_req[i].kr_event, set->ks_req[i].kr_event,
CPC_MAX_EVENT_LEN);
new->ks_req[i].kr_preset = set->ks_req[i].kr_preset;
new->ks_req[i].kr_flags = set->ks_req[i].kr_flags;
new->ks_req[i].kr_nattrs = set->ks_req[i].kr_nattrs;
new->ks_req[i].kr_attr = kmem_alloc(new->ks_req[i].kr_nattrs *
sizeof (kcpc_attr_t), KM_SLEEP);
for (j = 0; j < new->ks_req[i].kr_nattrs; j++) {
new->ks_req[i].kr_attr[j].ka_val =
set->ks_req[i].kr_attr[j].ka_val;
(void) strncpy(new->ks_req[i].kr_attr[j].ka_name,
set->ks_req[i].kr_attr[j].ka_name,
CPC_MAX_ATTR_LEN);
}
}
return (new);
}
int
kcpc_allow_nonpriv(void *token)
{
return (((kcpc_ctx_t *)token)->kc_flags & KCPC_CTX_NONPRIV);
}
void
kcpc_invalidate(kthread_t *t)
{
kcpc_ctx_t *ctx = t->t_cpc_ctx;
if (ctx != NULL)
atomic_or_uint(&ctx->kc_flags, KCPC_CTX_INVALID);
}
/*
* Given a PCBE ID, attempt to load a matching PCBE module. The strings given
* are used to construct PCBE names, starting with the most specific,
* "pcbe.first.second.third.fourth" and ending with the least specific,
* "pcbe.first".
*
* Returns 0 if a PCBE was successfully loaded and -1 upon error.
*/
int
kcpc_pcbe_tryload(const char *prefix, uint_t first, uint_t second, uint_t third)
{
uint_t s[3];
s[0] = first;
s[1] = second;
s[2] = third;
return (modload_qualified("pcbe",
"pcbe", prefix, ".", s, 3) < 0 ? -1 : 0);
}
|