1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright IBM Corp. 2019
4 * Author(s): Thomas Richter <tmricht@linux.ibm.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
9 *
10 * Architecture specific trace_event function. Save event's bc000 raw data
11 * to file. File name is aux.ctr.## where ## stands for the CPU number the
12 * sample was taken from.
13 */
14
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <inttypes.h>
19
20 #include <sys/stat.h>
21 #include <linux/compiler.h>
22 #include <asm/byteorder.h>
23
24 #include "debug.h"
25 #include "session.h"
26 #include "evlist.h"
27 #include "color.h"
28 #include "sample-raw.h"
29 #include "s390-cpumcf-kernel.h"
30 #include "pmu-events/pmu-events.h"
31
ctrset_size(struct cf_ctrset_entry * set)32 static size_t ctrset_size(struct cf_ctrset_entry *set)
33 {
34 return sizeof(*set) + set->ctr * sizeof(u64);
35 }
36
ctrset_valid(struct cf_ctrset_entry * set)37 static bool ctrset_valid(struct cf_ctrset_entry *set)
38 {
39 return set->def == S390_CPUMCF_DIAG_DEF;
40 }
41
42 /* CPU Measurement Counter Facility raw data is a byte stream. It is 8 byte
43 * aligned and might have trailing padding bytes.
44 * Display the raw data on screen.
45 */
s390_cpumcfdg_testctr(struct perf_sample * sample)46 static bool s390_cpumcfdg_testctr(struct perf_sample *sample)
47 {
48 size_t len = sample->raw_size, offset = 0;
49 unsigned char *buf = sample->raw_data;
50 struct cf_trailer_entry *te;
51 struct cf_ctrset_entry *cep, ce;
52
53 if (!len)
54 return false;
55 while (offset < len) {
56 cep = (struct cf_ctrset_entry *)(buf + offset);
57 ce.def = be16_to_cpu(cep->def);
58 ce.set = be16_to_cpu(cep->set);
59 ce.ctr = be16_to_cpu(cep->ctr);
60 ce.res1 = be16_to_cpu(cep->res1);
61
62 if (!ctrset_valid(&ce) || offset + ctrset_size(&ce) > len) {
63 /* Raw data for counter sets are always multiple of 8
64 * bytes. Prepending a 4 bytes size field to the
65 * raw data block in the sample causes the perf tool
66 * to append 4 padding bytes to make the raw data part
67 * of the sample a multiple of eight bytes again.
68 *
69 * If the last entry (trailer) is 4 bytes off the raw
70 * area data end, all is good.
71 */
72 if (len - offset - sizeof(*te) == 4)
73 break;
74 pr_err("Invalid counter set entry at %zd\n", offset);
75 return false;
76 }
77 offset += ctrset_size(&ce);
78 }
79 return true;
80 }
81
82 /* Dump event bc000 on screen, already tested on correctness. */
s390_cpumcfdg_dumptrail(const char * color,size_t offset,struct cf_trailer_entry * tep)83 static void s390_cpumcfdg_dumptrail(const char *color, size_t offset,
84 struct cf_trailer_entry *tep)
85 {
86 struct cf_trailer_entry te;
87
88 te.flags = be64_to_cpu(tep->flags);
89 te.cfvn = be16_to_cpu(tep->cfvn);
90 te.csvn = be16_to_cpu(tep->csvn);
91 te.cpu_speed = be32_to_cpu(tep->cpu_speed);
92 te.timestamp = be64_to_cpu(tep->timestamp);
93 te.progusage1 = be64_to_cpu(tep->progusage1);
94 te.progusage2 = be64_to_cpu(tep->progusage2);
95 te.progusage3 = be64_to_cpu(tep->progusage3);
96 te.tod_base = be64_to_cpu(tep->tod_base);
97 te.mach_type = be16_to_cpu(tep->mach_type);
98 te.res1 = be16_to_cpu(tep->res1);
99 te.res2 = be32_to_cpu(tep->res2);
100
101 color_fprintf(stdout, color, " [%#08zx] Trailer:%c%c%c%c%c"
102 " Cfvn:%d Csvn:%d Speed:%d TOD:%#llx\n",
103 offset, te.clock_base ? 'T' : ' ',
104 te.speed ? 'S' : ' ', te.mtda ? 'M' : ' ',
105 te.caca ? 'C' : ' ', te.lcda ? 'L' : ' ',
106 te.cfvn, te.csvn, te.cpu_speed, te.timestamp);
107 color_fprintf(stdout, color, "\t\t1:%lx 2:%lx 3:%lx TOD-Base:%#llx"
108 " Type:%x\n\n",
109 te.progusage1, te.progusage2, te.progusage3,
110 te.tod_base, te.mach_type);
111 }
112
113 /* Return starting number of a counter set */
get_counterset_start(int setnr)114 static int get_counterset_start(int setnr)
115 {
116 switch (setnr) {
117 case CPUMF_CTR_SET_BASIC: /* Basic counter set */
118 return 0;
119 case CPUMF_CTR_SET_USER: /* Problem state counter set */
120 return 32;
121 case CPUMF_CTR_SET_CRYPTO: /* Crypto counter set */
122 return 64;
123 case CPUMF_CTR_SET_EXT: /* Extended counter set */
124 return 128;
125 case CPUMF_CTR_SET_MT_DIAG: /* Diagnostic counter set */
126 return 448;
127 default:
128 return -1;
129 }
130 }
131
132 struct get_counter_name_data {
133 int wanted;
134 const char *result;
135 };
136
get_counter_name_callback(const struct pmu_event * evp,const struct pmu_events_table * table __maybe_unused,void * vdata)137 static int get_counter_name_callback(const struct pmu_event *evp,
138 const struct pmu_events_table *table __maybe_unused,
139 void *vdata)
140 {
141 struct get_counter_name_data *data = vdata;
142 int rc, event_nr;
143
144 if (evp->name == NULL || evp->event == NULL)
145 return 0;
146 rc = sscanf(evp->event, "event=%x", &event_nr);
147 if (rc == 1 && event_nr == data->wanted) {
148 data->result = evp->name;
149 return 1; /* Terminate the search. */
150 }
151 return 0;
152 }
153
154 /* Scan the PMU table and extract the logical name of a counter from the
155 * PMU events table. Input is the counter set and counter number with in the
156 * set. Construct the event number and use this as key. If they match return
157 * the name of this counter.
158 * If no match is found a NULL pointer is returned.
159 */
get_counter_name(int set,int nr,const struct pmu_events_table * table)160 static const char *get_counter_name(int set, int nr, const struct pmu_events_table *table)
161 {
162 struct get_counter_name_data data = {
163 .wanted = get_counterset_start(set) + nr,
164 .result = NULL,
165 };
166
167 if (!table)
168 return NULL;
169
170 pmu_events_table_for_each_event(table, get_counter_name_callback, &data);
171 return data.result;
172 }
173
s390_cpumcfdg_dump(struct perf_sample * sample)174 static void s390_cpumcfdg_dump(struct perf_sample *sample)
175 {
176 size_t i, len = sample->raw_size, offset = 0;
177 unsigned char *buf = sample->raw_data;
178 const char *color = PERF_COLOR_BLUE;
179 struct cf_ctrset_entry *cep, ce;
180 const struct pmu_events_table *table;
181 u64 *p;
182
183 table = pmu_events_table__find();
184 while (offset < len) {
185 cep = (struct cf_ctrset_entry *)(buf + offset);
186
187 ce.def = be16_to_cpu(cep->def);
188 ce.set = be16_to_cpu(cep->set);
189 ce.ctr = be16_to_cpu(cep->ctr);
190 ce.res1 = be16_to_cpu(cep->res1);
191
192 if (!ctrset_valid(&ce)) { /* Print trailer */
193 s390_cpumcfdg_dumptrail(color, offset,
194 (struct cf_trailer_entry *)cep);
195 return;
196 }
197
198 color_fprintf(stdout, color, " [%#08zx] Counterset:%d"
199 " Counters:%d\n", offset, ce.set, ce.ctr);
200 for (i = 0, p = (u64 *)(cep + 1); i < ce.ctr; ++i, ++p) {
201 const char *ev_name = get_counter_name(ce.set, i, table);
202
203 color_fprintf(stdout, color,
204 "\tCounter:%03d %s Value:%#018lx\n", i,
205 ev_name ?: "<unknown>", be64_to_cpu(*p));
206 }
207 offset += ctrset_size(&ce);
208 }
209 }
210
211 /* S390 specific trace event function. Check for PERF_RECORD_SAMPLE events
212 * and if the event was triggered by a counter set diagnostic event display
213 * its raw data.
214 * The function is only invoked when the dump flag -D is set.
215 */
evlist__s390_sample_raw(struct evlist * evlist,union perf_event * event,struct perf_sample * sample)216 void evlist__s390_sample_raw(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
217 {
218 struct evsel *ev_bc000;
219
220 if (event->header.type != PERF_RECORD_SAMPLE)
221 return;
222
223 ev_bc000 = evlist__event2evsel(evlist, event);
224 if (ev_bc000 == NULL ||
225 ev_bc000->core.attr.config != PERF_EVENT_CPUM_CF_DIAG)
226 return;
227
228 /* Display raw data on screen */
229 if (!s390_cpumcfdg_testctr(sample)) {
230 pr_err("Invalid counter set data encountered\n");
231 return;
232 }
233 s390_cpumcfdg_dump(sample);
234 }
235