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
|
/*
* 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 (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2019 Joyent, Inc.
*/
/*
* This file contains preset event names from the Performance Application
* Programming Interface v3.5 which included the following notice:
*
* Copyright (c) 2005,6
* Innovative Computing Labs
* Computer Science Department,
* University of Tennessee,
* Knoxville, TN.
* All Rights Reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the University of Tennessee nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*
* This open source software license conforms to the BSD License template.
*/
/*
* Performance Counter Back-End for Intel processors supporting Architectural
* Performance Monitoring.
*/
#include <sys/cpuvar.h>
#include <sys/param.h>
#include <sys/cpc_impl.h>
#include <sys/cpc_pcbe.h>
#include <sys/modctl.h>
#include <sys/inttypes.h>
#include <sys/systm.h>
#include <sys/cmn_err.h>
#include <sys/x86_archext.h>
#include <sys/sdt.h>
#include <sys/archsystm.h>
#include <sys/privregs.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/cred.h>
#include <sys/policy.h>
#include "core_pcbe_table.h"
#include <core_pcbe_cpcgen.h>
static int core_pcbe_init(void);
static uint_t core_pcbe_ncounters(void);
static const char *core_pcbe_impl_name(void);
static const char *core_pcbe_cpuref(void);
static char *core_pcbe_list_events(uint_t picnum);
static char *core_pcbe_list_attrs(void);
static uint64_t core_pcbe_event_coverage(char *event);
static uint64_t core_pcbe_overflow_bitmap(void);
static int core_pcbe_configure(uint_t picnum, char *event, uint64_t preset,
uint32_t flags, uint_t nattrs, kcpc_attr_t *attrs, void **data,
void *token);
static void core_pcbe_program(void *token);
static void core_pcbe_allstop(void);
static void core_pcbe_sample(void *token);
static void core_pcbe_free(void *config);
#define FALSE 0
#define TRUE 1
/* Counter Type */
#define CORE_GPC 0 /* General-Purpose Counter (GPC) */
#define CORE_FFC 1 /* Fixed-Function Counter (FFC) */
/* MSR Addresses */
#define GPC_BASE_PMC 0x00c1 /* First GPC */
#define GPC_BASE_PES 0x0186 /* First GPC Event Select register */
#define FFC_BASE_PMC 0x0309 /* First FFC */
#define PERF_FIXED_CTR_CTRL 0x038d /* Used to enable/disable FFCs */
#define PERF_GLOBAL_STATUS 0x038e /* Overflow status register */
#define PERF_GLOBAL_CTRL 0x038f /* Used to enable/disable counting */
#define PERF_GLOBAL_OVF_CTRL 0x0390 /* Used to clear overflow status */
/*
* Processor Event Select register fields
*/
#define CORE_USR (1ULL << 16) /* Count while not in ring 0 */
#define CORE_OS (1ULL << 17) /* Count while in ring 0 */
#define CORE_EDGE (1ULL << 18) /* Enable edge detection */
#define CORE_PC (1ULL << 19) /* Enable pin control */
#define CORE_INT (1ULL << 20) /* Enable interrupt on overflow */
#define CORE_EN (1ULL << 22) /* Enable counting */
#define CORE_INV (1ULL << 23) /* Invert the CMASK */
#define CORE_ANYTHR (1ULL << 21) /* Count event for any thread on core */
#define CORE_UMASK_SHIFT 8
#define CORE_UMASK_MASK 0xffu
#define CORE_CMASK_SHIFT 24
#define CORE_CMASK_MASK 0xffu
/*
* Fixed-function counter attributes
*/
#define CORE_FFC_OS_EN (1ULL << 0) /* Count while not in ring 0 */
#define CORE_FFC_USR_EN (1ULL << 1) /* Count while in ring 1 */
#define CORE_FFC_ANYTHR (1ULL << 2) /* Count event for any thread on core */
#define CORE_FFC_PMI (1ULL << 3) /* Enable interrupt on overflow */
/*
* Number of bits for specifying each FFC's attributes in the control register
*/
#define CORE_FFC_ATTR_SIZE 4
/*
* CondChgd and OvfBuffer fields of global status and overflow control registers
*/
#define CONDCHGD (1ULL << 63)
#define OVFBUFFER (1ULL << 62)
#define MASK_CONDCHGD_OVFBUFFER (CONDCHGD | OVFBUFFER)
#define ALL_STOPPED 0ULL
#define BITMASK_XBITS(x) ((1ull << (x)) - 1ull)
/*
* Only the lower 32-bits can be written to in the general-purpose
* counters. The higher bits are extended from bit 31; all ones if
* bit 31 is one and all zeros otherwise.
*
* The fixed-function counters do not have this restriction.
*/
#define BITS_EXTENDED_FROM_31 (BITMASK_XBITS(width_gpc) & ~BITMASK_XBITS(31))
#define WRMSR(msr, value) \
wrmsr((msr), (value)); \
DTRACE_PROBE2(wrmsr, uint64_t, (msr), uint64_t, (value));
#define RDMSR(msr, value) \
(value) = rdmsr((msr)); \
DTRACE_PROBE2(rdmsr, uint64_t, (msr), uint64_t, (value));
typedef struct core_pcbe_config {
uint64_t core_rawpic;
uint64_t core_ctl; /* Event Select bits */
uint64_t core_pmc; /* Counter register address */
uint64_t core_pes; /* Event Select register address */
uint_t core_picno;
uint8_t core_pictype; /* CORE_GPC or CORE_FFC */
} core_pcbe_config_t;
pcbe_ops_t core_pcbe_ops = {
PCBE_VER_1, /* pcbe_ver */
CPC_CAP_OVERFLOW_INTERRUPT | CPC_CAP_OVERFLOW_PRECISE, /* pcbe_caps */
core_pcbe_ncounters, /* pcbe_ncounters */
core_pcbe_impl_name, /* pcbe_impl_name */
core_pcbe_cpuref, /* pcbe_cpuref */
core_pcbe_list_events, /* pcbe_list_events */
core_pcbe_list_attrs, /* pcbe_list_attrs */
core_pcbe_event_coverage, /* pcbe_event_coverage */
core_pcbe_overflow_bitmap, /* pcbe_overflow_bitmap */
core_pcbe_configure, /* pcbe_configure */
core_pcbe_program, /* pcbe_program */
core_pcbe_allstop, /* pcbe_allstop */
core_pcbe_sample, /* pcbe_sample */
core_pcbe_free /* pcbe_free */
};
struct nametable_core_uarch {
const char *name;
uint64_t restricted_bits;
uint8_t event_num;
};
/*
* Counting an event for all cores or all bus agents requires cpc_cpu privileges
*/
#define ALL_CORES (1ULL << 15)
#define ALL_AGENTS (1ULL << 13)
struct generic_events {
const char *name;
uint8_t event_num;
uint8_t umask;
};
static const struct generic_events cmn_generic_events[] = {
{ "PAPI_tot_cyc", 0x3c, 0x00 }, /* cpu_clk_unhalted.thread_p/core */
{ "PAPI_tot_ins", 0xc0, 0x00 }, /* inst_retired.any_p */
{ "PAPI_br_ins", 0xc4, 0x0c }, /* br_inst_retired.taken */
{ "PAPI_br_msp", 0xc5, 0x00 }, /* br_inst_retired.mispred */
{ "PAPI_br_ntk", 0xc4, 0x03 },
/* br_inst_retired.pred_not_taken|pred_taken */
{ "PAPI_br_prc", 0xc4, 0x05 },
/* br_inst_retired.pred_not_taken|pred_taken */
{ "PAPI_hw_int", 0xc8, 0x00 }, /* hw_int_rvc */
{ "PAPI_tot_iis", 0xaa, 0x01 }, /* macro_insts.decoded */
{ "PAPI_l1_dca", 0x43, 0x01 }, /* l1d_all_ref */
{ "PAPI_l1_icm", 0x81, 0x00 }, /* l1i_misses */
{ "PAPI_l1_icr", 0x80, 0x00 }, /* l1i_reads */
{ "PAPI_l1_tcw", 0x41, 0x0f }, /* l1d_cache_st.mesi */
{ "PAPI_l2_stm", 0x2a, 0x41 }, /* l2_st.self.i_state */
{ "PAPI_l2_tca", 0x2e, 0x4f }, /* l2_rqsts.self.demand.mesi */
{ "PAPI_l2_tch", 0x2e, 0x4e }, /* l2_rqsts.mes */
{ "PAPI_l2_tcm", 0x2e, 0x41 }, /* l2_rqsts.self.demand.i_state */
{ "PAPI_l2_tcw", 0x2a, 0x4f }, /* l2_st.self.mesi */
{ "PAPI_ld_ins", 0xc0, 0x01 }, /* inst_retired.loads */
{ "PAPI_lst_ins", 0xc0, 0x03 }, /* inst_retired.loads|stores */
{ "PAPI_sr_ins", 0xc0, 0x02 }, /* inst_retired.stores */
{ "PAPI_tlb_dm", 0x08, 0x01 }, /* dtlb_misses.any */
{ "PAPI_tlb_im", 0x82, 0x12 }, /* itlb.small_miss|large_miss */
{ "PAPI_tlb_tl", 0x0c, 0x03 }, /* page_walks */
{ "", NT_END, 0 }
};
static const struct generic_events generic_events_pic0[] = {
{ "PAPI_l1_dcm", 0xcb, 0x01 }, /* mem_load_retired.l1d_miss */
{ "", NT_END, 0 }
};
/*
* The events listed in the following table can be counted on all
* general-purpose counters on processors that are of Penryn and Merom Family
*/
static const struct nametable_core_uarch cmn_gpc_events_core_uarch[] = {
/* Alphabetical order of event name */
{ "baclears", 0x0, 0xe6 },
{ "bogus_br", 0x0, 0xe4 },
{ "br_bac_missp_exec", 0x0, 0x8a },
{ "br_call_exec", 0x0, 0x92 },
{ "br_call_missp_exec", 0x0, 0x93 },
{ "br_cnd_exec", 0x0, 0x8b },
{ "br_cnd_missp_exec", 0x0, 0x8c },
{ "br_ind_call_exec", 0x0, 0x94 },
{ "br_ind_exec", 0x0, 0x8d },
{ "br_ind_missp_exec", 0x0, 0x8e },
{ "br_inst_decoded", 0x0, 0xe0 },
{ "br_inst_exec", 0x0, 0x88 },
{ "br_inst_retired", 0x0, 0xc4 },
{ "br_inst_retired_mispred", 0x0, 0xc5 },
{ "br_missp_exec", 0x0, 0x89 },
{ "br_ret_bac_missp_exec", 0x0, 0x91 },
{ "br_ret_exec", 0x0, 0x8f },
{ "br_ret_missp_exec", 0x0, 0x90 },
{ "br_tkn_bubble_1", 0x0, 0x97 },
{ "br_tkn_bubble_2", 0x0, 0x98 },
{ "bus_bnr_drv", ALL_AGENTS, 0x61 },
{ "bus_data_rcv", ALL_CORES, 0x64 },
{ "bus_drdy_clocks", ALL_AGENTS, 0x62 },
{ "bus_hit_drv", ALL_AGENTS, 0x7a },
{ "bus_hitm_drv", ALL_AGENTS, 0x7b },
{ "bus_io_wait", ALL_CORES, 0x7f },
{ "bus_lock_clocks", ALL_CORES | ALL_AGENTS, 0x63 },
{ "bus_request_outstanding", ALL_CORES | ALL_AGENTS, 0x60 },
{ "bus_trans_any", ALL_CORES | ALL_AGENTS, 0x70 },
{ "bus_trans_brd", ALL_CORES | ALL_AGENTS, 0x65 },
{ "bus_trans_burst", ALL_CORES | ALL_AGENTS, 0x6e },
{ "bus_trans_def", ALL_CORES | ALL_AGENTS, 0x6d },
{ "bus_trans_ifetch", ALL_CORES | ALL_AGENTS, 0x68 },
{ "bus_trans_inval", ALL_CORES | ALL_AGENTS, 0x69 },
{ "bus_trans_io", ALL_CORES | ALL_AGENTS, 0x6c },
{ "bus_trans_mem", ALL_CORES | ALL_AGENTS, 0x6f },
{ "bus_trans_p", ALL_CORES | ALL_AGENTS, 0x6b },
{ "bus_trans_pwr", ALL_CORES | ALL_AGENTS, 0x6a },
{ "bus_trans_rfo", ALL_CORES | ALL_AGENTS, 0x66 },
{ "bus_trans_wb", ALL_CORES | ALL_AGENTS, 0x67 },
{ "busq_empty", ALL_CORES, 0x7d },
{ "cmp_snoop", ALL_CORES, 0x78 },
{ "cpu_clk_unhalted", 0x0, 0x3c },
{ "cycles_int", 0x0, 0xc6 },
{ "cycles_l1i_mem_stalled", 0x0, 0x86 },
{ "dtlb_misses", 0x0, 0x08 },
{ "eist_trans", 0x0, 0x3a },
{ "esp", 0x0, 0xab },
{ "ext_snoop", ALL_AGENTS, 0x77 },
{ "fp_mmx_trans", 0x0, 0xcc },
{ "hw_int_rcv", 0x0, 0xc8 },
{ "ild_stall", 0x0, 0x87 },
{ "inst_queue", 0x0, 0x83 },
{ "inst_retired", 0x0, 0xc0 },
{ "itlb", 0x0, 0x82 },
{ "itlb_miss_retired", 0x0, 0xc9 },
{ "l1d_all_ref", 0x0, 0x43 },
{ "l1d_cache_ld", 0x0, 0x40 },
{ "l1d_cache_lock", 0x0, 0x42 },
{ "l1d_cache_st", 0x0, 0x41 },
{ "l1d_m_evict", 0x0, 0x47 },
{ "l1d_m_repl", 0x0, 0x46 },
{ "l1d_pend_miss", 0x0, 0x48 },
{ "l1d_prefetch", 0x0, 0x4e },
{ "l1d_repl", 0x0, 0x45 },
{ "l1d_split", 0x0, 0x49 },
{ "l1i_misses", 0x0, 0x81 },
{ "l1i_reads", 0x0, 0x80 },
{ "l2_ads", ALL_CORES, 0x21 },
{ "l2_dbus_busy_rd", ALL_CORES, 0x23 },
{ "l2_ifetch", ALL_CORES, 0x28 },
{ "l2_ld", ALL_CORES, 0x29 },
{ "l2_lines_in", ALL_CORES, 0x24 },
{ "l2_lines_out", ALL_CORES, 0x26 },
{ "l2_lock", ALL_CORES, 0x2b },
{ "l2_m_lines_in", ALL_CORES, 0x25 },
{ "l2_m_lines_out", ALL_CORES, 0x27 },
{ "l2_no_req", ALL_CORES, 0x32 },
{ "l2_reject_busq", ALL_CORES, 0x30 },
{ "l2_rqsts", ALL_CORES, 0x2e },
{ "l2_st", ALL_CORES, 0x2a },
{ "load_block", 0x0, 0x03 },
{ "load_hit_pre", 0x0, 0x4c },
{ "machine_nukes", 0x0, 0xc3 },
{ "macro_insts", 0x0, 0xaa },
{ "memory_disambiguation", 0x0, 0x09 },
{ "misalign_mem_ref", 0x0, 0x05 },
{ "page_walks", 0x0, 0x0c },
{ "pref_rqsts_dn", 0x0, 0xf8 },
{ "pref_rqsts_up", 0x0, 0xf0 },
{ "rat_stalls", 0x0, 0xd2 },
{ "resource_stalls", 0x0, 0xdc },
{ "rs_uops_dispatched", 0x0, 0xa0 },
{ "seg_reg_renames", 0x0, 0xd5 },
{ "seg_rename_stalls", 0x0, 0xd4 },
{ "segment_reg_loads", 0x0, 0x06 },
{ "simd_assist", 0x0, 0xcd },
{ "simd_comp_inst_retired", 0x0, 0xca },
{ "simd_inst_retired", 0x0, 0xc7 },
{ "simd_instr_retired", 0x0, 0xce },
{ "simd_sat_instr_retired", 0x0, 0xcf },
{ "simd_sat_uop_exec", 0x0, 0xb1 },
{ "simd_uop_type_exec", 0x0, 0xb3 },
{ "simd_uops_exec", 0x0, 0xb0 },
{ "snoop_stall_drv", ALL_CORES | ALL_AGENTS, 0x7e },
{ "sse_pre_exec", 0x0, 0x07 },
{ "sse_pre_miss", 0x0, 0x4b },
{ "store_block", 0x0, 0x04 },
{ "thermal_trip", 0x0, 0x3b },
{ "uops_retired", 0x0, 0xc2 },
{ "x87_ops_retired", 0x0, 0xc1 },
{ "", 0x0, NT_END }
};
/*
* If any of the pic specific events require privileges, make sure to add a
* check in configure_gpc() to find whether an event hard-coded as a number by
* the user has any privilege requirements
*/
static const struct nametable_core_uarch pic0_events[] = {
/* Alphabetical order of event name */
{ "cycles_div_busy", 0x0, 0x14 },
{ "fp_comp_ops_exe", 0x0, 0x10 },
{ "idle_during_div", 0x0, 0x18 },
{ "mem_load_retired", 0x0, 0xcb },
{ "rs_uops_dispatched_port", 0x0, 0xa1 },
{ "", 0x0, NT_END }
};
static const struct nametable_core_uarch pic1_events[] = {
/* Alphabetical order of event name */
{ "delayed_bypass", 0x0, 0x19 },
{ "div", 0x0, 0x13 },
{ "fp_assist", 0x0, 0x11 },
{ "mul", 0x0, 0x12 },
{ "", 0x0, NT_END }
};
/* FFC entries must be in order */
static char *ffc_names_non_htt[] = {
"instr_retired.any",
"cpu_clk_unhalted.core",
"cpu_clk_unhalted.ref",
NULL
};
static char *ffc_names_htt[] = {
"instr_retired.any",
"cpu_clk_unhalted.thread",
"cpu_clk_unhalted.ref",
NULL
};
static char *ffc_genericnames[] = {
"PAPI_tot_ins",
"PAPI_tot_cyc",
"",
NULL
};
static char **ffc_names = NULL;
static char **ffc_allnames = NULL;
static char **gpc_names = NULL;
static uint32_t versionid;
static uint64_t num_gpc;
static uint64_t width_gpc;
static uint64_t mask_gpc;
static uint64_t num_ffc;
static uint64_t width_ffc;
static uint64_t mask_ffc;
static uint_t total_pmc;
static uint64_t control_ffc;
static uint64_t control_gpc;
static uint64_t control_mask;
static uint32_t arch_events_vector;
#define IMPL_NAME_LEN 100
static char core_impl_name[IMPL_NAME_LEN];
static const char *core_cpuref =
"See https://download.01.org/perfmon/index/ or Chapers 18 and 19 " \
"of the \"Intel 64 and IA-32 Architectures Software Developer's " \
"Manual Volume 3: System Programming Guide\" Order Number: " \
"325384-062US, March 2017.";
/* Architectural events */
#define ARCH_EVENTS_COMMON \
{ 0xc0, 0x00, C_ALL, "inst_retired.any_p" }, \
{ 0x3c, 0x01, C_ALL, "cpu_clk_unhalted.ref_p" }, \
{ 0x2e, 0x4f, C_ALL, "longest_lat_cache.reference" }, \
{ 0x2e, 0x41, C_ALL, "longest_lat_cache.miss" }, \
{ 0xc4, 0x00, C_ALL, "br_inst_retired.all_branches" }, \
{ 0xc5, 0x00, C_ALL, "br_misp_retired.all_branches" }
static const struct events_table_t arch_events_table_non_htt[] = {
{ 0x3c, 0x00, C_ALL, "cpu_clk_unhalted.core" },
ARCH_EVENTS_COMMON
};
static const struct events_table_t arch_events_table_htt[] = {
{ 0x3c, 0x00, C_ALL, "cpu_clk_unhalted.thread_p" },
ARCH_EVENTS_COMMON
};
static char *arch_genevents_table[] = {
"PAPI_tot_cyc", /* cpu_clk_unhalted.thread_p/core */
"PAPI_tot_ins", /* inst_retired.any_p */
"", /* cpu_clk_unhalted.ref_p */
"", /* longest_lat_cache.reference */
"", /* longest_lat_cache.miss */
"", /* br_inst_retired.all_branches */
"", /* br_misp_retired.all_branches */
};
static const struct events_table_t *arch_events_table = NULL;
static uint64_t known_arch_events;
static uint64_t known_ffc_num;
static const struct events_table_t *events_table = NULL;
/*
* Initialize string containing list of supported general-purpose counter
* events for processors of Penryn and Merom Family
*/
static void
pcbe_init_core_uarch()
{
const struct nametable_core_uarch *n;
const struct generic_events *k;
const struct nametable_core_uarch *picspecific_events;
const struct generic_events *picspecific_genericevents;
size_t common_size;
size_t size;
uint64_t i;
gpc_names = kmem_alloc(num_gpc * sizeof (char *), KM_SLEEP);
/* Calculate space needed to save all the common event names */
common_size = 0;
for (n = cmn_gpc_events_core_uarch; n->event_num != NT_END; n++) {
common_size += strlen(n->name) + 1;
}
for (k = cmn_generic_events; k->event_num != NT_END; k++) {
common_size += strlen(k->name) + 1;
}
for (i = 0; i < num_gpc; i++) {
size = 0;
picspecific_genericevents = NULL;
switch (i) {
case 0:
picspecific_events = pic0_events;
picspecific_genericevents = generic_events_pic0;
break;
case 1:
picspecific_events = pic1_events;
break;
default:
picspecific_events = NULL;
break;
}
if (picspecific_events != NULL) {
for (n = picspecific_events;
n->event_num != NT_END;
n++) {
size += strlen(n->name) + 1;
}
}
if (picspecific_genericevents != NULL) {
for (k = picspecific_genericevents;
k->event_num != NT_END; k++) {
size += strlen(k->name) + 1;
}
}
gpc_names[i] =
kmem_alloc(size + common_size + 1, KM_SLEEP);
gpc_names[i][0] = '\0';
if (picspecific_events != NULL) {
for (n = picspecific_events;
n->event_num != NT_END; n++) {
(void) strcat(gpc_names[i], n->name);
(void) strcat(gpc_names[i], ",");
}
}
if (picspecific_genericevents != NULL) {
for (k = picspecific_genericevents;
k->event_num != NT_END; k++) {
(void) strcat(gpc_names[i], k->name);
(void) strcat(gpc_names[i], ",");
}
}
for (n = cmn_gpc_events_core_uarch; n->event_num != NT_END;
n++) {
(void) strcat(gpc_names[i], n->name);
(void) strcat(gpc_names[i], ",");
}
for (k = cmn_generic_events; k->event_num != NT_END; k++) {
(void) strcat(gpc_names[i], k->name);
(void) strcat(gpc_names[i], ",");
}
/*
* Remove trailing comma.
*/
gpc_names[i][common_size + size - 1] = '\0';
}
}
static int
core_pcbe_init(void)
{
struct cpuid_regs cp;
size_t size;
uint64_t i;
uint64_t j;
uint64_t arch_events_vector_length;
size_t arch_events_string_length;
uint_t model, stepping;
if (cpuid_getvendor(CPU) != X86_VENDOR_Intel)
return (-1);
/* Obtain Basic CPUID information */
cp.cp_eax = 0x0;
(void) __cpuid_insn(&cp);
/* No Architectural Performance Monitoring Leaf returned by CPUID */
if (cp.cp_eax < 0xa) {
return (-1);
}
/* Obtain the Architectural Performance Monitoring Leaf */
cp.cp_eax = 0xa;
(void) __cpuid_insn(&cp);
versionid = cp.cp_eax & 0xFF;
/*
* Fixed-Function Counters (FFC)
*
* All Family 6 Model 15 and Model 23 processors have fixed-function
* counters. These counters were made Architectural with
* Family 6 Model 15 Stepping 9.
*/
switch (versionid) {
case 0:
return (-1);
case 2:
num_ffc = cp.cp_edx & 0x1F;
width_ffc = (cp.cp_edx >> 5) & 0xFF;
/*
* Some processors have an errata (AW34) where
* versionid is reported as 2 when actually 1.
* In this case, fixed-function counters are
* model-specific as in Version 1.
*/
if (num_ffc != 0) {
break;
}
/* FALLTHROUGH */
case 1:
num_ffc = 3;
width_ffc = 40;
versionid = 1;
break;
default:
num_ffc = cp.cp_edx & 0x1F;
width_ffc = (cp.cp_edx >> 5) & 0xFF;
break;
}
if (num_ffc >= 64)
return (-1);
/* Set HTT-specific names of architectural & FFC events */
if (is_x86_feature(x86_featureset, X86FSET_HTT)) {
ffc_names = ffc_names_htt;
arch_events_table = arch_events_table_htt;
known_arch_events =
sizeof (arch_events_table_htt) /
sizeof (struct events_table_t);
known_ffc_num =
sizeof (ffc_names_htt) / sizeof (char *);
} else {
ffc_names = ffc_names_non_htt;
arch_events_table = arch_events_table_non_htt;
known_arch_events =
sizeof (arch_events_table_non_htt) /
sizeof (struct events_table_t);
known_ffc_num =
sizeof (ffc_names_non_htt) / sizeof (char *);
}
if (num_ffc >= known_ffc_num) {
/*
* The system seems to have more fixed-function counters than
* what this PCBE is able to handle correctly. Default to the
* maximum number of fixed-function counters that this driver
* is aware of.
*/
num_ffc = known_ffc_num - 1;
}
mask_ffc = BITMASK_XBITS(width_ffc);
control_ffc = BITMASK_XBITS(num_ffc);
/*
* General Purpose Counters (GPC)
*/
num_gpc = (cp.cp_eax >> 8) & 0xFF;
width_gpc = (cp.cp_eax >> 16) & 0xFF;
if (num_gpc >= 64)
return (-1);
mask_gpc = BITMASK_XBITS(width_gpc);
control_gpc = BITMASK_XBITS(num_gpc);
control_mask = (control_ffc << 32) | control_gpc;
total_pmc = num_gpc + num_ffc;
if (total_pmc > 64) {
/* Too wide for the overflow bitmap */
return (-1);
}
/* FFC names */
ffc_allnames = kmem_alloc(num_ffc * sizeof (char *), KM_SLEEP);
for (i = 0; i < num_ffc; i++) {
ffc_allnames[i] = kmem_alloc(
strlen(ffc_names[i]) + strlen(ffc_genericnames[i]) + 2,
KM_SLEEP);
ffc_allnames[i][0] = '\0';
(void) strcat(ffc_allnames[i], ffc_names[i]);
/* Check if this ffc has a generic name */
if (strcmp(ffc_genericnames[i], "") != 0) {
(void) strcat(ffc_allnames[i], ",");
(void) strcat(ffc_allnames[i], ffc_genericnames[i]);
}
}
/* GPC events for Family 6 Models 15, 23 and 29 only */
if ((cpuid_getfamily(CPU) == 6) &&
((cpuid_getmodel(CPU) == 15) || (cpuid_getmodel(CPU) == 23) ||
(cpuid_getmodel(CPU) == 29))) {
(void) snprintf(core_impl_name, IMPL_NAME_LEN,
"Core Microarchitecture");
pcbe_init_core_uarch();
return (0);
}
(void) snprintf(core_impl_name, IMPL_NAME_LEN,
"Intel Arch PerfMon v%d on Family %d Model %d",
versionid, cpuid_getfamily(CPU), cpuid_getmodel(CPU));
/*
* Architectural events
*/
arch_events_vector_length = (cp.cp_eax >> 24) & 0xFF;
ASSERT(known_arch_events == arch_events_vector_length);
/*
* To handle the case where a new performance monitoring setup is run
* on a non-debug kernel
*/
if (known_arch_events > arch_events_vector_length) {
known_arch_events = arch_events_vector_length;
} else {
arch_events_vector_length = known_arch_events;
}
arch_events_vector = cp.cp_ebx &
BITMASK_XBITS(arch_events_vector_length);
/*
* Process architectural and non-architectural events using GPC
*/
if (num_gpc > 0) {
gpc_names = kmem_alloc(num_gpc * sizeof (char *), KM_SLEEP);
/* Calculate space required for the architectural gpc events */
arch_events_string_length = 0;
for (i = 0; i < known_arch_events; i++) {
if (((1U << i) & arch_events_vector) == 0) {
arch_events_string_length +=
strlen(arch_events_table[i].name) + 1;
if (strcmp(arch_genevents_table[i], "") != 0) {
arch_events_string_length +=
strlen(arch_genevents_table[i]) + 1;
}
}
}
/* Non-architectural events list */
model = cpuid_getmodel(CPU);
stepping = cpuid_getstep(CPU);
events_table = core_cpcgen_table(model, stepping);
for (i = 0; i < num_gpc; i++) {
/*
* Determine length of all supported event names
* (architectural + non-architectural)
*/
size = arch_events_string_length;
for (j = 0; events_table != NULL &&
events_table[j].eventselect != NT_END;
j++) {
if (C(i) & events_table[j].supported_counters) {
size += strlen(events_table[j].name) +
1;
}
}
/* Allocate memory for this pics list */
gpc_names[i] = kmem_alloc(size + 1, KM_SLEEP);
gpc_names[i][0] = '\0';
if (size == 0) {
continue;
}
/*
* Create the list of all supported events
* (architectural + non-architectural)
*/
for (j = 0; j < known_arch_events; j++) {
if (((1U << j) & arch_events_vector) == 0) {
(void) strcat(gpc_names[i],
arch_events_table[j].name);
(void) strcat(gpc_names[i], ",");
if (strcmp(
arch_genevents_table[j], "")
!= 0) {
(void) strcat(gpc_names[i],
arch_genevents_table[j]);
(void) strcat(gpc_names[i],
",");
}
}
}
for (j = 0; events_table != NULL &&
events_table[j].eventselect != NT_END;
j++) {
if (C(i) & events_table[j].supported_counters) {
(void) strcat(gpc_names[i],
events_table[j].name);
(void) strcat(gpc_names[i], ",");
}
}
/* Remove trailing comma */
gpc_names[i][size - 1] = '\0';
}
}
return (0);
}
static uint_t core_pcbe_ncounters()
{
return (total_pmc);
}
static const char *core_pcbe_impl_name(void)
{
return (core_impl_name);
}
static const char *core_pcbe_cpuref(void)
{
return (core_cpuref);
}
static char *core_pcbe_list_events(uint_t picnum)
{
ASSERT(picnum < cpc_ncounters);
if (picnum < num_gpc) {
return (gpc_names[picnum]);
} else {
return (ffc_allnames[picnum - num_gpc]);
}
}
static char *core_pcbe_list_attrs(void)
{
if (versionid >= 3) {
return ("edge,inv,umask,cmask,anythr");
} else {
return ("edge,pc,inv,umask,cmask");
}
}
static const struct nametable_core_uarch *
find_gpcevent_core_uarch(char *name,
const struct nametable_core_uarch *nametable)
{
const struct nametable_core_uarch *n;
int compare_result = -1;
for (n = nametable; n->event_num != NT_END; n++) {
compare_result = strcmp(name, n->name);
if (compare_result <= 0) {
break;
}
}
if (compare_result == 0) {
return (n);
}
return (NULL);
}
static const struct generic_events *
find_generic_events(char *name, const struct generic_events *table)
{
const struct generic_events *n;
for (n = table; n->event_num != NT_END; n++) {
if (strcmp(name, n->name) == 0) {
return (n);
};
}
return (NULL);
}
static const struct events_table_t *
find_gpcevent(char *name)
{
int i;
/* Search architectural events */
for (i = 0; i < known_arch_events; i++) {
if (strcmp(name, arch_events_table[i].name) == 0 ||
strcmp(name, arch_genevents_table[i]) == 0) {
if (((1U << i) & arch_events_vector) == 0) {
return (&arch_events_table[i]);
}
}
}
/* Search non-architectural events */
if (events_table != NULL) {
for (i = 0; events_table[i].eventselect != NT_END; i++) {
if (strcmp(name, events_table[i].name) == 0) {
return (&events_table[i]);
}
}
}
return (NULL);
}
static uint64_t
core_pcbe_event_coverage(char *event)
{
uint64_t bitmap;
uint64_t bitmask;
const struct events_table_t *n;
int i;
bitmap = 0;
/* Is it an event that a GPC can track? */
if (versionid >= 3) {
n = find_gpcevent(event);
if (n != NULL) {
bitmap |= (n->supported_counters &
BITMASK_XBITS(num_gpc));
}
} else {
if (find_generic_events(event, cmn_generic_events) != NULL) {
bitmap |= BITMASK_XBITS(num_gpc);
} else if (find_generic_events(event,
generic_events_pic0) != NULL) {
bitmap |= 1ULL;
} else if (find_gpcevent_core_uarch(event,
cmn_gpc_events_core_uarch) != NULL) {
bitmap |= BITMASK_XBITS(num_gpc);
} else if (find_gpcevent_core_uarch(event, pic0_events) !=
NULL) {
bitmap |= 1ULL;
} else if (find_gpcevent_core_uarch(event, pic1_events) !=
NULL) {
bitmap |= 1ULL << 1;
}
}
/* Check if the event can be counted in the fixed-function counters */
if (num_ffc > 0) {
bitmask = 1ULL << num_gpc;
for (i = 0; i < num_ffc; i++) {
if (strcmp(event, ffc_names[i]) == 0) {
bitmap |= bitmask;
} else if (strcmp(event, ffc_genericnames[i]) == 0) {
bitmap |= bitmask;
}
bitmask = bitmask << 1;
}
}
return (bitmap);
}
static uint64_t
core_pcbe_overflow_bitmap(void)
{
uint64_t interrupt_status;
uint64_t intrbits_ffc;
uint64_t intrbits_gpc;
extern int kcpc_hw_overflow_intr_installed;
uint64_t overflow_bitmap;
RDMSR(PERF_GLOBAL_STATUS, interrupt_status);
WRMSR(PERF_GLOBAL_OVF_CTRL, interrupt_status);
interrupt_status = interrupt_status & control_mask;
intrbits_ffc = (interrupt_status >> 32) & control_ffc;
intrbits_gpc = interrupt_status & control_gpc;
overflow_bitmap = (intrbits_ffc << num_gpc) | intrbits_gpc;
ASSERT(kcpc_hw_overflow_intr_installed);
(*kcpc_hw_enable_cpc_intr)();
return (overflow_bitmap);
}
static int
check_cpc_securitypolicy(core_pcbe_config_t *conf,
const struct nametable_core_uarch *n)
{
if (conf->core_ctl & n->restricted_bits) {
if (secpolicy_cpc_cpu(crgetcred()) != 0) {
return (CPC_ATTR_REQUIRES_PRIVILEGE);
}
}
return (0);
}
static int
configure_gpc(uint_t picnum, char *event, uint64_t preset, uint32_t flags,
uint_t nattrs, kcpc_attr_t *attrs, void **data)
{
core_pcbe_config_t conf;
const struct nametable_core_uarch *n;
const struct generic_events *k = NULL;
const struct nametable_core_uarch *m;
const struct nametable_core_uarch *picspecific_events;
struct nametable_core_uarch nt_raw = { "", 0x0, 0x0 };
uint_t i;
long event_num;
const struct events_table_t *eventcode;
if (((preset & BITS_EXTENDED_FROM_31) != 0) &&
((preset & BITS_EXTENDED_FROM_31) !=
BITS_EXTENDED_FROM_31)) {
/*
* Bits beyond bit-31 in the general-purpose counters can only
* be written to by extension of bit 31. We cannot preset
* these bits to any value other than all 1s or all 0s.
*/
return (CPC_ATTRIBUTE_OUT_OF_RANGE);
}
if (versionid >= 3) {
eventcode = find_gpcevent(event);
if (eventcode != NULL) {
if ((C(picnum) & eventcode->supported_counters) == 0) {
return (CPC_PIC_NOT_CAPABLE);
}
if (nattrs > 0 &&
(strncmp("PAPI_", event, 5) == 0)) {
return (CPC_ATTRIBUTE_OUT_OF_RANGE);
}
conf.core_ctl = eventcode->eventselect;
conf.core_ctl |= eventcode->unitmask <<
CORE_UMASK_SHIFT;
} else {
/* Event specified as raw event code */
if (ddi_strtol(event, NULL, 0, &event_num) != 0) {
return (CPC_INVALID_EVENT);
}
conf.core_ctl = event_num & 0xFF;
}
} else {
if ((k = find_generic_events(event, cmn_generic_events)) !=
NULL ||
(picnum == 0 &&
(k = find_generic_events(event, generic_events_pic0)) !=
NULL)) {
if (nattrs > 0) {
return (CPC_ATTRIBUTE_OUT_OF_RANGE);
}
conf.core_ctl = k->event_num;
conf.core_ctl |= k->umask << CORE_UMASK_SHIFT;
} else {
/* Not a generic event */
n = find_gpcevent_core_uarch(event,
cmn_gpc_events_core_uarch);
if (n == NULL) {
switch (picnum) {
case 0:
picspecific_events =
pic0_events;
break;
case 1:
picspecific_events =
pic1_events;
break;
default:
picspecific_events = NULL;
break;
}
if (picspecific_events != NULL) {
n = find_gpcevent_core_uarch(event,
picspecific_events);
}
}
if (n == NULL) {
/*
* Check if this is a case where the event was
* specified directly by its event number
* instead of its name string.
*/
if (ddi_strtol(event, NULL, 0, &event_num) !=
0) {
return (CPC_INVALID_EVENT);
}
event_num = event_num & 0xFF;
/*
* Search the event table to find out if the
* event specified has an privilege
* requirements. Currently none of the
* pic-specific counters have any privilege
* requirements. Hence only the table
* cmn_gpc_events_core_uarch is searched.
*/
for (m = cmn_gpc_events_core_uarch;
m->event_num != NT_END;
m++) {
if (event_num == m->event_num) {
break;
}
}
if (m->event_num == NT_END) {
nt_raw.event_num = (uint8_t)event_num;
n = &nt_raw;
} else {
n = m;
}
}
conf.core_ctl = n->event_num; /* Event Select */
}
}
conf.core_picno = picnum;
conf.core_pictype = CORE_GPC;
conf.core_rawpic = preset & mask_gpc;
conf.core_pes = GPC_BASE_PES + picnum;
conf.core_pmc = GPC_BASE_PMC + picnum;
for (i = 0; i < nattrs; i++) {
if (strncmp(attrs[i].ka_name, "umask", 6) == 0) {
if ((attrs[i].ka_val | CORE_UMASK_MASK) !=
CORE_UMASK_MASK) {
return (CPC_ATTRIBUTE_OUT_OF_RANGE);
}
/* Clear out the default umask */
conf.core_ctl &= ~ (CORE_UMASK_MASK <<
CORE_UMASK_SHIFT);
/* Use the user provided umask */
conf.core_ctl |= attrs[i].ka_val <<
CORE_UMASK_SHIFT;
} else if (strncmp(attrs[i].ka_name, "edge", 6) == 0) {
if (attrs[i].ka_val != 0)
conf.core_ctl |= CORE_EDGE;
} else if (strncmp(attrs[i].ka_name, "inv", 4) == 0) {
if (attrs[i].ka_val != 0)
conf.core_ctl |= CORE_INV;
} else if (strncmp(attrs[i].ka_name, "cmask", 6) == 0) {
if ((attrs[i].ka_val | CORE_CMASK_MASK) !=
CORE_CMASK_MASK) {
return (CPC_ATTRIBUTE_OUT_OF_RANGE);
}
conf.core_ctl |= attrs[i].ka_val <<
CORE_CMASK_SHIFT;
} else if (strncmp(attrs[i].ka_name, "anythr", 7) ==
0) {
if (versionid < 3)
return (CPC_INVALID_ATTRIBUTE);
if (secpolicy_cpc_cpu(crgetcred()) != 0) {
return (CPC_ATTR_REQUIRES_PRIVILEGE);
}
if (attrs[i].ka_val != 0)
conf.core_ctl |= CORE_ANYTHR;
} else {
return (CPC_INVALID_ATTRIBUTE);
}
}
if (flags & CPC_COUNT_USER)
conf.core_ctl |= CORE_USR;
if (flags & CPC_COUNT_SYSTEM)
conf.core_ctl |= CORE_OS;
if (flags & CPC_OVF_NOTIFY_EMT)
conf.core_ctl |= CORE_INT;
conf.core_ctl |= CORE_EN;
if (versionid < 3 && k == NULL) {
if (check_cpc_securitypolicy(&conf, n) != 0) {
return (CPC_ATTR_REQUIRES_PRIVILEGE);
}
}
*data = kmem_alloc(sizeof (core_pcbe_config_t), KM_SLEEP);
*((core_pcbe_config_t *)*data) = conf;
return (0);
}
static int
configure_ffc(uint_t picnum, char *event, uint64_t preset, uint32_t flags,
uint_t nattrs, kcpc_attr_t *attrs, void **data)
{
core_pcbe_config_t *conf;
uint_t i;
if (picnum - num_gpc >= num_ffc) {
return (CPC_INVALID_PICNUM);
}
if ((strcmp(ffc_names[picnum-num_gpc], event) != 0) &&
(strcmp(ffc_genericnames[picnum-num_gpc], event) != 0)) {
return (CPC_INVALID_EVENT);
}
if ((versionid < 3) && (nattrs != 0)) {
return (CPC_INVALID_ATTRIBUTE);
}
conf = kmem_alloc(sizeof (core_pcbe_config_t), KM_SLEEP);
conf->core_ctl = 0;
for (i = 0; i < nattrs; i++) {
if (strncmp(attrs[i].ka_name, "anythr", 7) == 0) {
if (secpolicy_cpc_cpu(crgetcred()) != 0) {
kmem_free(conf, sizeof (core_pcbe_config_t));
return (CPC_ATTR_REQUIRES_PRIVILEGE);
}
if (attrs[i].ka_val != 0) {
conf->core_ctl |= CORE_FFC_ANYTHR;
}
} else {
kmem_free(conf, sizeof (core_pcbe_config_t));
return (CPC_INVALID_ATTRIBUTE);
}
}
conf->core_picno = picnum;
conf->core_pictype = CORE_FFC;
conf->core_rawpic = preset & mask_ffc;
conf->core_pmc = FFC_BASE_PMC + (picnum - num_gpc);
/* All fixed-function counters have the same control register */
conf->core_pes = PERF_FIXED_CTR_CTRL;
if (flags & CPC_COUNT_USER)
conf->core_ctl |= CORE_FFC_USR_EN;
if (flags & CPC_COUNT_SYSTEM)
conf->core_ctl |= CORE_FFC_OS_EN;
if (flags & CPC_OVF_NOTIFY_EMT)
conf->core_ctl |= CORE_FFC_PMI;
*data = conf;
return (0);
}
/*ARGSUSED*/
static int
core_pcbe_configure(uint_t picnum, char *event, uint64_t preset,
uint32_t flags, uint_t nattrs, kcpc_attr_t *attrs, void **data,
void *token)
{
int ret;
core_pcbe_config_t *conf;
/*
* If we've been handed an existing configuration, we need only preset
* the counter value.
*/
if (*data != NULL) {
conf = *data;
ASSERT(conf->core_pictype == CORE_GPC ||
conf->core_pictype == CORE_FFC);
if (conf->core_pictype == CORE_GPC)
conf->core_rawpic = preset & mask_gpc;
else /* CORE_FFC */
conf->core_rawpic = preset & mask_ffc;
return (0);
}
if (picnum >= total_pmc) {
return (CPC_INVALID_PICNUM);
}
if (picnum < num_gpc) {
ret = configure_gpc(picnum, event, preset, flags,
nattrs, attrs, data);
} else {
ret = configure_ffc(picnum, event, preset, flags,
nattrs, attrs, data);
}
return (ret);
}
static void
core_pcbe_program(void *token)
{
core_pcbe_config_t *cfg;
uint64_t perf_global_ctrl;
uint64_t perf_fixed_ctr_ctrl;
uint64_t curcr4;
core_pcbe_allstop();
curcr4 = getcr4();
if (kcpc_allow_nonpriv(token))
/* Allow RDPMC at any ring level */
setcr4(curcr4 | CR4_PCE);
else
/* Allow RDPMC only at ring 0 */
setcr4(curcr4 & ~CR4_PCE);
/* Clear any overflow indicators before programming the counters */
WRMSR(PERF_GLOBAL_OVF_CTRL, MASK_CONDCHGD_OVFBUFFER | control_mask);
cfg = NULL;
perf_global_ctrl = 0;
perf_fixed_ctr_ctrl = 0;
cfg = (core_pcbe_config_t *)kcpc_next_config(token, cfg, NULL);
while (cfg != NULL) {
ASSERT(cfg->core_pictype == CORE_GPC ||
cfg->core_pictype == CORE_FFC);
if (cfg->core_pictype == CORE_GPC) {
/*
* General-purpose counter registers have write
* restrictions where only the lower 32-bits can be
* written to. The rest of the relevant bits are
* written to by extension from bit 31 (all ZEROS if
* bit-31 is ZERO and all ONE if bit-31 is ONE). This
* makes it possible to write to the counter register
* only values that have all ONEs or all ZEROs in the
* higher bits.
*/
if (((cfg->core_rawpic & BITS_EXTENDED_FROM_31) == 0) ||
((cfg->core_rawpic & BITS_EXTENDED_FROM_31) ==
BITS_EXTENDED_FROM_31)) {
/*
* Straighforward case where the higher bits
* are all ZEROs or all ONEs.
*/
WRMSR(cfg->core_pmc,
(cfg->core_rawpic & mask_gpc));
} else {
/*
* The high order bits are not all the same.
* We save what is currently in the registers
* and do not write to it. When we want to do
* a read from this register later (in
* core_pcbe_sample()), we subtract the value
* we save here to get the actual event count.
*
* NOTE: As a result, we will not get overflow
* interrupts as expected.
*/
RDMSR(cfg->core_pmc, cfg->core_rawpic);
cfg->core_rawpic = cfg->core_rawpic & mask_gpc;
}
WRMSR(cfg->core_pes, cfg->core_ctl);
perf_global_ctrl |= 1ull << cfg->core_picno;
} else {
/*
* Unlike the general-purpose counters, all relevant
* bits of fixed-function counters can be written to.
*/
WRMSR(cfg->core_pmc, cfg->core_rawpic & mask_ffc);
/*
* Collect the control bits for all the
* fixed-function counters and write it at one shot
* later in this function
*/
perf_fixed_ctr_ctrl |= cfg->core_ctl <<
((cfg->core_picno - num_gpc) * CORE_FFC_ATTR_SIZE);
perf_global_ctrl |=
1ull << (cfg->core_picno - num_gpc + 32);
}
cfg = (core_pcbe_config_t *)
kcpc_next_config(token, cfg, NULL);
}
/* Enable all the counters */
WRMSR(PERF_FIXED_CTR_CTRL, perf_fixed_ctr_ctrl);
WRMSR(PERF_GLOBAL_CTRL, perf_global_ctrl);
}
static void
core_pcbe_allstop(void)
{
/* Disable all the counters together */
WRMSR(PERF_GLOBAL_CTRL, ALL_STOPPED);
setcr4(getcr4() & ~CR4_PCE);
}
static void
core_pcbe_sample(void *token)
{
uint64_t *daddr;
uint64_t curpic;
core_pcbe_config_t *cfg;
uint64_t counter_mask;
cfg = (core_pcbe_config_t *)kcpc_next_config(token, NULL, &daddr);
while (cfg != NULL) {
ASSERT(cfg->core_pictype == CORE_GPC ||
cfg->core_pictype == CORE_FFC);
curpic = rdmsr(cfg->core_pmc);
DTRACE_PROBE4(core__pcbe__sample,
uint64_t, cfg->core_pmc,
uint64_t, curpic,
uint64_t, cfg->core_rawpic,
uint64_t, *daddr);
if (cfg->core_pictype == CORE_GPC) {
counter_mask = mask_gpc;
} else {
counter_mask = mask_ffc;
}
curpic = curpic & counter_mask;
if (curpic >= cfg->core_rawpic) {
*daddr += curpic - cfg->core_rawpic;
} else {
/* Counter overflowed since our last sample */
*daddr += counter_mask - (cfg->core_rawpic - curpic) +
1;
}
cfg->core_rawpic = *daddr & counter_mask;
cfg =
(core_pcbe_config_t *)kcpc_next_config(token, cfg, &daddr);
}
}
static void
core_pcbe_free(void *config)
{
kmem_free(config, sizeof (core_pcbe_config_t));
}
static struct modlpcbe core_modlpcbe = {
&mod_pcbeops,
"Core Performance Counters",
&core_pcbe_ops
};
static struct modlinkage core_modl = {
MODREV_1,
&core_modlpcbe,
};
int
_init(void)
{
if (core_pcbe_init() != 0) {
return (ENOTSUP);
}
return (mod_install(&core_modl));
}
int
_fini(void)
{
return (mod_remove(&core_modl));
}
int
_info(struct modinfo *mi)
{
return (mod_info(&core_modl, mi));
}
|