xref: /openbmc/linux/tools/perf/util/s390-cpumsf.c (revision 16bd4321)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright IBM Corp. 2018
4  * Auxtrace support for s390 CPU-Measurement Sampling Facility
5  *
6  * Author(s):  Thomas Richter <tmricht@linux.ibm.com>
7  *
8  * Auxiliary traces are collected during 'perf record' using rbd000 event.
9  * Several PERF_RECORD_XXX are generated during recording:
10  *
11  * PERF_RECORD_AUX:
12  *	Records that new data landed in the AUX buffer part.
13  * PERF_RECORD_AUXTRACE:
14  *	Defines auxtrace data. Followed by the actual data. The contents of
15  *	the auxtrace data is dependent on the event and the CPU.
16  *	This record is generated by perf record command. For details
17  *	see Documentation/perf.data-file-format.txt.
18  * PERF_RECORD_AUXTRACE_INFO:
19  *	Defines a table of contains for PERF_RECORD_AUXTRACE records. This
20  *	record is generated during 'perf record' command. Each record contains up
21  *	to 256 entries describing offset and size of the AUXTRACE data in the
22  *	perf.data file.
23  * PERF_RECORD_AUXTRACE_ERROR:
24  *	Indicates an error during AUXTRACE collection such as buffer overflow.
25  * PERF_RECORD_FINISHED_ROUND:
26  *	Perf events are not necessarily in time stamp order, as they can be
27  *	collected in parallel on different CPUs. If the events should be
28  *	processed in time order they need to be sorted first.
29  *	Perf report guarantees that there is no reordering over a
30  *	PERF_RECORD_FINISHED_ROUND boundary event. All perf records with a
31  *	time stamp lower than this record are processed (and displayed) before
32  *	the succeeding perf record are processed.
33  *
34  * These records are evaluated during perf report command.
35  *
36  * 1. PERF_RECORD_AUXTRACE_INFO is used to set up the infrastructure for
37  * auxiliary trace data processing. See s390_cpumsf_process_auxtrace_info()
38  * below.
39  * Auxiliary trace data is collected per CPU. To merge the data into the report
40  * an auxtrace_queue is created for each CPU. It is assumed that the auxtrace
41  * data is in ascending order.
42  *
43  * Each queue has a double linked list of auxtrace_buffers. This list contains
44  * the offset and size of a CPU's auxtrace data. During auxtrace processing
45  * the data portion is mmap()'ed.
46  *
47  * To sort the queues in chronological order, all queue access is controlled
48  * by the auxtrace_heap. This is basicly a stack, each stack element has two
49  * entries, the queue number and a time stamp. However the stack is sorted by
50  * the time stamps. The highest time stamp is at the bottom the lowest
51  * (nearest) time stamp is at the top. That sort order is maintained at all
52  * times!
53  *
54  * After the auxtrace infrastructure has been setup, the auxtrace queues are
55  * filled with data (offset/size pairs) and the auxtrace_heap is populated.
56  *
57  * 2. PERF_RECORD_XXX processing triggers access to the auxtrace_queues.
58  * Each record is handled by s390_cpumsf_process_event(). The time stamp of
59  * the perf record is compared with the time stamp located on the auxtrace_heap
60  * top element. If that time stamp is lower than the time stamp from the
61  * record sample, the auxtrace queues will be processed. As auxtrace queues
62  * control many auxtrace_buffers and each buffer can be quite large, the
63  * auxtrace buffer might be processed only partially. In this case the
64  * position in the auxtrace_buffer of that queue is remembered and the time
65  * stamp of the last processed entry of the auxtrace_buffer replaces the
66  * current auxtrace_heap top.
67  *
68  * 3. Auxtrace_queues might run of out data and are feeded by the
69  * PERF_RECORD_AUXTRACE handling, see s390_cpumsf_process_auxtrace_event().
70  *
71  * Event Generation
72  * Each sampling-data entry in the auxilary trace data generates a perf sample.
73  * This sample is filled
74  * with data from the auxtrace such as PID/TID, instruction address, CPU state,
75  * etc. This sample is processed with perf_session__deliver_synth_event() to
76  * be included into the GUI.
77  *
78  * 4. PERF_RECORD_FINISHED_ROUND event is used to process all the remaining
79  * auxiliary traces entries until the time stamp of this record is reached
80  * auxtrace_heap top. This is triggered by ordered_event->deliver().
81  *
82  *
83  * Perf event processing.
84  * Event processing of PERF_RECORD_XXX entries relies on time stamp entries.
85  * This is the function call sequence:
86  *
87  * __cmd_report()
88  * |
89  * perf_session__process_events()
90  * |
91  * __perf_session__process_events()
92  * |
93  * perf_session__process_event()
94  * |  This functions splits the PERF_RECORD_XXX records.
95  * |  - Those generated by perf record command (type number equal or higher
96  * |    than PERF_RECORD_USER_TYPE_START) are handled by
97  * |    perf_session__process_user_event(see below)
98  * |  - Those generated by the kernel are handled by
99  * |    perf_evlist__parse_sample_timestamp()
100  * |
101  * perf_evlist__parse_sample_timestamp()
102  * |  Extract time stamp from sample data.
103  * |
104  * perf_session__queue_event()
105  * |  If timestamp is positive the sample is entered into an ordered_event
106  * |  list, sort order is the timestamp. The event processing is deferred until
107  * |  later (see perf_session__process_user_event()).
108  * |  Other timestamps (0 or -1) are handled immediately by
109  * |  perf_session__deliver_event(). These are events generated at start up
110  * |  of command perf record. They create PERF_RECORD_COMM and PERF_RECORD_MMAP*
111  * |  records. They are needed to create a list of running processes and its
112  * |  memory mappings and layout. They are needed at the beginning to enable
113  * |  command perf report to create process trees and memory mappings.
114  * |
115  * perf_session__deliver_event()
116  * |  Delivers a PERF_RECORD_XXX entry for handling.
117  * |
118  * auxtrace__process_event()
119  * |  The timestamp of the PERF_RECORD_XXX entry is taken to correlate with
120  * |  time stamps from the auxiliary trace buffers. This enables
121  * |  synchronization between auxiliary trace data and the events on the
122  * |  perf.data file.
123  * |
124  * machine__deliver_event()
125  * |  Handles the PERF_RECORD_XXX event. This depends on the record type.
126  *    It might update the process tree, update a process memory map or enter
127  *    a sample with IP and call back chain data into GUI data pool.
128  *
129  *
130  * Deferred processing determined by perf_session__process_user_event() is
131  * finally processed when a PERF_RECORD_FINISHED_ROUND is encountered. These
132  * are generated during command perf record.
133  * The timestamp of PERF_RECORD_FINISHED_ROUND event is taken to process all
134  * PERF_RECORD_XXX entries stored in the ordered_event list. This list was
135  * built up while reading the perf.data file.
136  * Each event is now processed by calling perf_session__deliver_event().
137  * This enables time synchronization between the data in the perf.data file and
138  * the data in the auxiliary trace buffers.
139  */
140 
141 #include <endian.h>
142 #include <errno.h>
143 #include <byteswap.h>
144 #include <inttypes.h>
145 #include <linux/kernel.h>
146 #include <linux/types.h>
147 #include <linux/bitops.h>
148 #include <linux/log2.h>
149 
150 #include <sys/stat.h>
151 #include <sys/types.h>
152 
153 #include "cpumap.h"
154 #include "color.h"
155 #include "evsel.h"
156 #include "evlist.h"
157 #include "machine.h"
158 #include "session.h"
159 #include "util.h"
160 #include "thread.h"
161 #include "debug.h"
162 #include "auxtrace.h"
163 #include "s390-cpumsf.h"
164 #include "s390-cpumsf-kernel.h"
165 #include "s390-cpumcf-kernel.h"
166 #include "config.h"
167 
168 struct s390_cpumsf {
169 	struct auxtrace		auxtrace;
170 	struct auxtrace_queues	queues;
171 	struct auxtrace_heap	heap;
172 	struct perf_session	*session;
173 	struct machine		*machine;
174 	u32			auxtrace_type;
175 	u32			pmu_type;
176 	u16			machine_type;
177 	bool			data_queued;
178 	bool			use_logfile;
179 	char			*logdir;
180 };
181 
182 struct s390_cpumsf_queue {
183 	struct s390_cpumsf	*sf;
184 	unsigned int		queue_nr;
185 	struct auxtrace_buffer	*buffer;
186 	int			cpu;
187 	FILE			*logfile;
188 	FILE			*logfile_ctr;
189 };
190 
191 /* Check if the raw data should be dumped to file. If this is the case and
192  * the file to dump to has not been opened for writing, do so.
193  *
194  * Return 0 on success and greater zero on error so processing continues.
195  */
196 static int s390_cpumcf_dumpctr(struct s390_cpumsf *sf,
197 			       struct perf_sample *sample)
198 {
199 	struct s390_cpumsf_queue *sfq;
200 	struct auxtrace_queue *q;
201 	int rc = 0;
202 
203 	if (!sf->use_logfile || sf->queues.nr_queues <= sample->cpu)
204 		return rc;
205 
206 	q = &sf->queues.queue_array[sample->cpu];
207 	sfq = q->priv;
208 	if (!sfq)		/* Queue not yet allocated */
209 		return rc;
210 
211 	if (!sfq->logfile_ctr) {
212 		char *name;
213 
214 		rc = (sf->logdir)
215 			? asprintf(&name, "%s/aux.ctr.%02x",
216 				 sf->logdir, sample->cpu)
217 			: asprintf(&name, "aux.ctr.%02x", sample->cpu);
218 		if (rc > 0)
219 			sfq->logfile_ctr = fopen(name, "w");
220 		if (sfq->logfile_ctr == NULL) {
221 			pr_err("Failed to open counter set log file %s, "
222 			       "continue...\n", name);
223 			rc = 1;
224 		}
225 		free(name);
226 	}
227 
228 	if (sfq->logfile_ctr) {
229 		/* See comment above for -4 */
230 		size_t n = fwrite(sample->raw_data, sample->raw_size - 4, 1,
231 				  sfq->logfile_ctr);
232 		if (n != 1) {
233 			pr_err("Failed to write counter set data\n");
234 			rc = 1;
235 		}
236 	}
237 	return rc;
238 }
239 
240 /* Display s390 CPU measurement facility basic-sampling data entry */
241 static bool s390_cpumsf_basic_show(const char *color, size_t pos,
242 				   struct hws_basic_entry *basic)
243 {
244 	if (basic->def != 1) {
245 		pr_err("Invalid AUX trace basic entry [%#08zx]\n", pos);
246 		return false;
247 	}
248 	color_fprintf(stdout, color, "    [%#08zx] Basic   Def:%04x Inst:%#04x"
249 		      " %c%c%c%c AS:%d ASN:%#04x IA:%#018llx\n"
250 		      "\t\tCL:%d HPP:%#018llx GPP:%#018llx\n",
251 		      pos, basic->def, basic->U,
252 		      basic->T ? 'T' : ' ',
253 		      basic->W ? 'W' : ' ',
254 		      basic->P ? 'P' : ' ',
255 		      basic->I ? 'I' : ' ',
256 		      basic->AS, basic->prim_asn, basic->ia, basic->CL,
257 		      basic->hpp, basic->gpp);
258 	return true;
259 }
260 
261 /* Display s390 CPU measurement facility diagnostic-sampling data entry */
262 static bool s390_cpumsf_diag_show(const char *color, size_t pos,
263 				  struct hws_diag_entry *diag)
264 {
265 	if (diag->def < S390_CPUMSF_DIAG_DEF_FIRST) {
266 		pr_err("Invalid AUX trace diagnostic entry [%#08zx]\n", pos);
267 		return false;
268 	}
269 	color_fprintf(stdout, color, "    [%#08zx] Diag    Def:%04x %c\n",
270 		      pos, diag->def, diag->I ? 'I' : ' ');
271 	return true;
272 }
273 
274 /* Return TOD timestamp contained in an trailer entry */
275 static unsigned long long trailer_timestamp(struct hws_trailer_entry *te)
276 {
277 	/* te->t set: TOD in STCKE format, bytes 8-15
278 	 * to->t not set: TOD in STCK format, bytes 0-7
279 	 */
280 	unsigned long long ts;
281 
282 	memcpy(&ts, &te->timestamp[te->t], sizeof(ts));
283 	return ts;
284 }
285 
286 /* Display s390 CPU measurement facility trailer entry */
287 static bool s390_cpumsf_trailer_show(const char *color, size_t pos,
288 				     struct hws_trailer_entry *te)
289 {
290 	if (te->bsdes != sizeof(struct hws_basic_entry)) {
291 		pr_err("Invalid AUX trace trailer entry [%#08zx]\n", pos);
292 		return false;
293 	}
294 	color_fprintf(stdout, color, "    [%#08zx] Trailer %c%c%c bsdes:%d"
295 		      " dsdes:%d Overflow:%lld Time:%#llx\n"
296 		      "\t\tC:%d TOD:%#lx 1:%#llx 2:%#llx\n",
297 		      pos,
298 		      te->f ? 'F' : ' ',
299 		      te->a ? 'A' : ' ',
300 		      te->t ? 'T' : ' ',
301 		      te->bsdes, te->dsdes, te->overflow,
302 		      trailer_timestamp(te), te->clock_base, te->progusage2,
303 		      te->progusage[0], te->progusage[1]);
304 	return true;
305 }
306 
307 /* Test a sample data block. It must be 4KB or a multiple thereof in size and
308  * 4KB page aligned. Each sample data page has a trailer entry at the
309  * end which contains the sample entry data sizes.
310  *
311  * Return true if the sample data block passes the checks and set the
312  * basic set entry size and diagnostic set entry size.
313  *
314  * Return false on failure.
315  *
316  * Note: Old hardware does not set the basic or diagnostic entry sizes
317  * in the trailer entry. Use the type number instead.
318  */
319 static bool s390_cpumsf_validate(int machine_type,
320 				 unsigned char *buf, size_t len,
321 				 unsigned short *bsdes,
322 				 unsigned short *dsdes)
323 {
324 	struct hws_basic_entry *basic = (struct hws_basic_entry *)buf;
325 	struct hws_trailer_entry *te;
326 
327 	*dsdes = *bsdes = 0;
328 	if (len & (S390_CPUMSF_PAGESZ - 1))	/* Illegal size */
329 		return false;
330 	if (basic->def != 1)		/* No basic set entry, must be first */
331 		return false;
332 	/* Check for trailer entry at end of SDB */
333 	te = (struct hws_trailer_entry *)(buf + S390_CPUMSF_PAGESZ
334 					      - sizeof(*te));
335 	*bsdes = te->bsdes;
336 	*dsdes = te->dsdes;
337 	if (!te->bsdes && !te->dsdes) {
338 		/* Very old hardware, use CPUID */
339 		switch (machine_type) {
340 		case 2097:
341 		case 2098:
342 			*dsdes = 64;
343 			*bsdes = 32;
344 			break;
345 		case 2817:
346 		case 2818:
347 			*dsdes = 74;
348 			*bsdes = 32;
349 			break;
350 		case 2827:
351 		case 2828:
352 			*dsdes = 85;
353 			*bsdes = 32;
354 			break;
355 		default:
356 			/* Illegal trailer entry */
357 			return false;
358 		}
359 	}
360 	return true;
361 }
362 
363 /* Return true if there is room for another entry */
364 static bool s390_cpumsf_reached_trailer(size_t entry_sz, size_t pos)
365 {
366 	size_t payload = S390_CPUMSF_PAGESZ - sizeof(struct hws_trailer_entry);
367 
368 	if (payload - (pos & (S390_CPUMSF_PAGESZ - 1)) < entry_sz)
369 		return false;
370 	return true;
371 }
372 
373 /* Dump an auxiliary buffer. These buffers are multiple of
374  * 4KB SDB pages.
375  */
376 static void s390_cpumsf_dump(struct s390_cpumsf *sf,
377 			     unsigned char *buf, size_t len)
378 {
379 	const char *color = PERF_COLOR_BLUE;
380 	struct hws_basic_entry *basic;
381 	struct hws_diag_entry *diag;
382 	unsigned short bsdes, dsdes;
383 	size_t pos = 0;
384 
385 	color_fprintf(stdout, color,
386 		      ". ... s390 AUX data: size %zu bytes\n",
387 		      len);
388 
389 	if (!s390_cpumsf_validate(sf->machine_type, buf, len, &bsdes,
390 				  &dsdes)) {
391 		pr_err("Invalid AUX trace data block size:%zu"
392 		       " (type:%d bsdes:%hd dsdes:%hd)\n",
393 		       len, sf->machine_type, bsdes, dsdes);
394 		return;
395 	}
396 
397 	/* s390 kernel always returns 4KB blocks fully occupied,
398 	 * no partially filled SDBs.
399 	 */
400 	while (pos < len) {
401 		/* Handle Basic entry */
402 		basic = (struct hws_basic_entry *)(buf + pos);
403 		if (s390_cpumsf_basic_show(color, pos, basic))
404 			pos += bsdes;
405 		else
406 			return;
407 
408 		/* Handle Diagnostic entry */
409 		diag = (struct hws_diag_entry *)(buf + pos);
410 		if (s390_cpumsf_diag_show(color, pos, diag))
411 			pos += dsdes;
412 		else
413 			return;
414 
415 		/* Check for trailer entry */
416 		if (!s390_cpumsf_reached_trailer(bsdes + dsdes, pos)) {
417 			/* Show trailer entry */
418 			struct hws_trailer_entry te;
419 
420 			pos = (pos + S390_CPUMSF_PAGESZ)
421 			       & ~(S390_CPUMSF_PAGESZ - 1);
422 			pos -= sizeof(te);
423 			memcpy(&te, buf + pos, sizeof(te));
424 			/* Set descriptor sizes in case of old hardware
425 			 * where these values are not set.
426 			 */
427 			te.bsdes = bsdes;
428 			te.dsdes = dsdes;
429 			if (s390_cpumsf_trailer_show(color, pos, &te))
430 				pos += sizeof(te);
431 			else
432 				return;
433 		}
434 	}
435 }
436 
437 static void s390_cpumsf_dump_event(struct s390_cpumsf *sf, unsigned char *buf,
438 				   size_t len)
439 {
440 	printf(".\n");
441 	s390_cpumsf_dump(sf, buf, len);
442 }
443 
444 #define	S390_LPP_PID_MASK	0xffffffff
445 
446 static bool s390_cpumsf_make_event(size_t pos,
447 				   struct hws_basic_entry *basic,
448 				   struct s390_cpumsf_queue *sfq)
449 {
450 	struct perf_sample sample = {
451 				.ip = basic->ia,
452 				.pid = basic->hpp & S390_LPP_PID_MASK,
453 				.tid = basic->hpp & S390_LPP_PID_MASK,
454 				.cpumode = PERF_RECORD_MISC_CPUMODE_UNKNOWN,
455 				.cpu = sfq->cpu,
456 				.period = 1
457 			    };
458 	union perf_event event;
459 
460 	memset(&event, 0, sizeof(event));
461 	if (basic->CL == 1)	/* Native LPAR mode */
462 		sample.cpumode = basic->P ? PERF_RECORD_MISC_USER
463 					  : PERF_RECORD_MISC_KERNEL;
464 	else if (basic->CL == 2)	/* Guest kernel/user space */
465 		sample.cpumode = basic->P ? PERF_RECORD_MISC_GUEST_USER
466 					  : PERF_RECORD_MISC_GUEST_KERNEL;
467 	else if (basic->gpp || basic->prim_asn != 0xffff)
468 		/* Use heuristics on old hardware */
469 		sample.cpumode = basic->P ? PERF_RECORD_MISC_GUEST_USER
470 					  : PERF_RECORD_MISC_GUEST_KERNEL;
471 	else
472 		sample.cpumode = basic->P ? PERF_RECORD_MISC_USER
473 					  : PERF_RECORD_MISC_KERNEL;
474 
475 	event.sample.header.type = PERF_RECORD_SAMPLE;
476 	event.sample.header.misc = sample.cpumode;
477 	event.sample.header.size = sizeof(struct perf_event_header);
478 
479 	pr_debug4("%s pos:%#zx ip:%#" PRIx64 " P:%d CL:%d pid:%d.%d cpumode:%d cpu:%d\n",
480 		 __func__, pos, sample.ip, basic->P, basic->CL, sample.pid,
481 		 sample.tid, sample.cpumode, sample.cpu);
482 	if (perf_session__deliver_synth_event(sfq->sf->session, &event,
483 					      &sample)) {
484 		pr_err("s390 Auxiliary Trace: failed to deliver event\n");
485 		return false;
486 	}
487 	return true;
488 }
489 
490 static unsigned long long get_trailer_time(const unsigned char *buf)
491 {
492 	struct hws_trailer_entry *te;
493 	unsigned long long aux_time;
494 
495 	te = (struct hws_trailer_entry *)(buf + S390_CPUMSF_PAGESZ
496 					      - sizeof(*te));
497 
498 	if (!te->clock_base)	/* TOD_CLOCK_BASE value missing */
499 		return 0;
500 
501 	/* Correct calculation to convert time stamp in trailer entry to
502 	 * nano seconds (taken from arch/s390 function tod_to_ns()).
503 	 * TOD_CLOCK_BASE is stored in trailer entry member progusage2.
504 	 */
505 	aux_time = trailer_timestamp(te) - te->progusage2;
506 	aux_time = (aux_time >> 9) * 125 + (((aux_time & 0x1ff) * 125) >> 9);
507 	return aux_time;
508 }
509 
510 /* Process the data samples of a single queue. The first parameter is a
511  * pointer to the queue, the second parameter is the time stamp. This
512  * is the time stamp:
513  * - of the event that triggered this processing.
514  * - or the time stamp when the last proccesing of this queue stopped.
515  *   In this case it stopped at a 4KB page boundary and record the
516  *   position on where to continue processing on the next invocation
517  *   (see buffer->use_data and buffer->use_size).
518  *
519  * When this function returns the second parameter is updated to
520  * reflect the time stamp of the last processed auxiliary data entry
521  * (taken from the trailer entry of that page). The caller uses this
522  * returned time stamp to record the last processed entry in this
523  * queue.
524  *
525  * The function returns:
526  * 0:  Processing successful. The second parameter returns the
527  *     time stamp from the trailer entry until which position
528  *     processing took place. Subsequent calls resume from this
529  *     position.
530  * <0: An error occurred during processing. The second parameter
531  *     returns the maximum time stamp.
532  * >0: Done on this queue. The second parameter returns the
533  *     maximum time stamp.
534  */
535 static int s390_cpumsf_samples(struct s390_cpumsf_queue *sfq, u64 *ts)
536 {
537 	struct s390_cpumsf *sf = sfq->sf;
538 	unsigned char *buf = sfq->buffer->use_data;
539 	size_t len = sfq->buffer->use_size;
540 	struct hws_basic_entry *basic;
541 	unsigned short bsdes, dsdes;
542 	size_t pos = 0;
543 	int err = 1;
544 	u64 aux_ts;
545 
546 	if (!s390_cpumsf_validate(sf->machine_type, buf, len, &bsdes,
547 				  &dsdes)) {
548 		*ts = ~0ULL;
549 		return -1;
550 	}
551 
552 	/* Get trailer entry time stamp and check if entries in
553 	 * this auxiliary page are ready for processing. If the
554 	 * time stamp of the first entry is too high, whole buffer
555 	 * can be skipped. In this case return time stamp.
556 	 */
557 	aux_ts = get_trailer_time(buf);
558 	if (!aux_ts) {
559 		pr_err("[%#08" PRIx64 "] Invalid AUX trailer entry TOD clock base\n",
560 		       (s64)sfq->buffer->data_offset);
561 		aux_ts = ~0ULL;
562 		goto out;
563 	}
564 	if (aux_ts > *ts) {
565 		*ts = aux_ts;
566 		return 0;
567 	}
568 
569 	while (pos < len) {
570 		/* Handle Basic entry */
571 		basic = (struct hws_basic_entry *)(buf + pos);
572 		if (s390_cpumsf_make_event(pos, basic, sfq))
573 			pos += bsdes;
574 		else {
575 			err = -EBADF;
576 			goto out;
577 		}
578 
579 		pos += dsdes;	/* Skip diagnositic entry */
580 
581 		/* Check for trailer entry */
582 		if (!s390_cpumsf_reached_trailer(bsdes + dsdes, pos)) {
583 			pos = (pos + S390_CPUMSF_PAGESZ)
584 			       & ~(S390_CPUMSF_PAGESZ - 1);
585 			/* Check existence of next page */
586 			if (pos >= len)
587 				break;
588 			aux_ts = get_trailer_time(buf + pos);
589 			if (!aux_ts) {
590 				aux_ts = ~0ULL;
591 				goto out;
592 			}
593 			if (aux_ts > *ts) {
594 				*ts = aux_ts;
595 				sfq->buffer->use_data += pos;
596 				sfq->buffer->use_size -= pos;
597 				return 0;
598 			}
599 		}
600 	}
601 out:
602 	*ts = aux_ts;
603 	sfq->buffer->use_size = 0;
604 	sfq->buffer->use_data = NULL;
605 	return err;	/* Buffer completely scanned or error */
606 }
607 
608 /* Run the s390 auxiliary trace decoder.
609  * Select the queue buffer to operate on, the caller already selected
610  * the proper queue, depending on second parameter 'ts'.
611  * This is the time stamp until which the auxiliary entries should
612  * be processed. This value is updated by called functions and
613  * returned to the caller.
614  *
615  * Resume processing in the current buffer. If there is no buffer
616  * get a new buffer from the queue and setup start position for
617  * processing.
618  * When a buffer is completely processed remove it from the queue
619  * before returning.
620  *
621  * This function returns
622  * 1: When the queue is empty. Second parameter will be set to
623  *    maximum time stamp.
624  * 0: Normal processing done.
625  * <0: Error during queue buffer setup. This causes the caller
626  *     to stop processing completely.
627  */
628 static int s390_cpumsf_run_decoder(struct s390_cpumsf_queue *sfq,
629 				   u64 *ts)
630 {
631 
632 	struct auxtrace_buffer *buffer;
633 	struct auxtrace_queue *queue;
634 	int err;
635 
636 	queue = &sfq->sf->queues.queue_array[sfq->queue_nr];
637 
638 	/* Get buffer and last position in buffer to resume
639 	 * decoding the auxiliary entries. One buffer might be large
640 	 * and decoding might stop in between. This depends on the time
641 	 * stamp of the trailer entry in each page of the auxiliary
642 	 * data and the time stamp of the event triggering the decoding.
643 	 */
644 	if (sfq->buffer == NULL) {
645 		sfq->buffer = buffer = auxtrace_buffer__next(queue,
646 							     sfq->buffer);
647 		if (!buffer) {
648 			*ts = ~0ULL;
649 			return 1;	/* Processing done on this queue */
650 		}
651 		/* Start with a new buffer on this queue */
652 		if (buffer->data) {
653 			buffer->use_size = buffer->size;
654 			buffer->use_data = buffer->data;
655 		}
656 		if (sfq->logfile) {	/* Write into log file */
657 			size_t rc = fwrite(buffer->data, buffer->size, 1,
658 					   sfq->logfile);
659 			if (rc != 1)
660 				pr_err("Failed to write auxiliary data\n");
661 		}
662 	} else
663 		buffer = sfq->buffer;
664 
665 	if (!buffer->data) {
666 		int fd = perf_data__fd(sfq->sf->session->data);
667 
668 		buffer->data = auxtrace_buffer__get_data(buffer, fd);
669 		if (!buffer->data)
670 			return -ENOMEM;
671 		buffer->use_size = buffer->size;
672 		buffer->use_data = buffer->data;
673 
674 		if (sfq->logfile) {	/* Write into log file */
675 			size_t rc = fwrite(buffer->data, buffer->size, 1,
676 					   sfq->logfile);
677 			if (rc != 1)
678 				pr_err("Failed to write auxiliary data\n");
679 		}
680 	}
681 	pr_debug4("%s queue_nr:%d buffer:%" PRId64 " offset:%#" PRIx64 " size:%#zx rest:%#zx\n",
682 		  __func__, sfq->queue_nr, buffer->buffer_nr, buffer->offset,
683 		  buffer->size, buffer->use_size);
684 	err = s390_cpumsf_samples(sfq, ts);
685 
686 	/* If non-zero, there is either an error (err < 0) or the buffer is
687 	 * completely done (err > 0). The error is unrecoverable, usually
688 	 * some descriptors could not be read successfully, so continue with
689 	 * the next buffer.
690 	 * In both cases the parameter 'ts' has been updated.
691 	 */
692 	if (err) {
693 		sfq->buffer = NULL;
694 		list_del(&buffer->list);
695 		auxtrace_buffer__free(buffer);
696 		if (err > 0)		/* Buffer done, no error */
697 			err = 0;
698 	}
699 	return err;
700 }
701 
702 static struct s390_cpumsf_queue *
703 s390_cpumsf_alloc_queue(struct s390_cpumsf *sf, unsigned int queue_nr)
704 {
705 	struct s390_cpumsf_queue *sfq;
706 
707 	sfq = zalloc(sizeof(struct s390_cpumsf_queue));
708 	if (sfq == NULL)
709 		return NULL;
710 
711 	sfq->sf = sf;
712 	sfq->queue_nr = queue_nr;
713 	sfq->cpu = -1;
714 	if (sf->use_logfile) {
715 		char *name;
716 		int rc;
717 
718 		rc = (sf->logdir)
719 			? asprintf(&name, "%s/aux.smp.%02x",
720 				 sf->logdir, queue_nr)
721 			: asprintf(&name, "aux.smp.%02x", queue_nr);
722 		if (rc > 0)
723 			sfq->logfile = fopen(name, "w");
724 		if (sfq->logfile == NULL) {
725 			pr_err("Failed to open auxiliary log file %s,"
726 			       "continue...\n", name);
727 			sf->use_logfile = false;
728 		}
729 		free(name);
730 	}
731 	return sfq;
732 }
733 
734 static int s390_cpumsf_setup_queue(struct s390_cpumsf *sf,
735 				   struct auxtrace_queue *queue,
736 				   unsigned int queue_nr, u64 ts)
737 {
738 	struct s390_cpumsf_queue *sfq = queue->priv;
739 
740 	if (list_empty(&queue->head))
741 		return 0;
742 
743 	if (sfq == NULL) {
744 		sfq = s390_cpumsf_alloc_queue(sf, queue_nr);
745 		if (!sfq)
746 			return -ENOMEM;
747 		queue->priv = sfq;
748 
749 		if (queue->cpu != -1)
750 			sfq->cpu = queue->cpu;
751 	}
752 	return auxtrace_heap__add(&sf->heap, queue_nr, ts);
753 }
754 
755 static int s390_cpumsf_setup_queues(struct s390_cpumsf *sf, u64 ts)
756 {
757 	unsigned int i;
758 	int ret = 0;
759 
760 	for (i = 0; i < sf->queues.nr_queues; i++) {
761 		ret = s390_cpumsf_setup_queue(sf, &sf->queues.queue_array[i],
762 					      i, ts);
763 		if (ret)
764 			break;
765 	}
766 	return ret;
767 }
768 
769 static int s390_cpumsf_update_queues(struct s390_cpumsf *sf, u64 ts)
770 {
771 	if (!sf->queues.new_data)
772 		return 0;
773 
774 	sf->queues.new_data = false;
775 	return s390_cpumsf_setup_queues(sf, ts);
776 }
777 
778 static int s390_cpumsf_process_queues(struct s390_cpumsf *sf, u64 timestamp)
779 {
780 	unsigned int queue_nr;
781 	u64 ts;
782 	int ret;
783 
784 	while (1) {
785 		struct auxtrace_queue *queue;
786 		struct s390_cpumsf_queue *sfq;
787 
788 		if (!sf->heap.heap_cnt)
789 			return 0;
790 
791 		if (sf->heap.heap_array[0].ordinal >= timestamp)
792 			return 0;
793 
794 		queue_nr = sf->heap.heap_array[0].queue_nr;
795 		queue = &sf->queues.queue_array[queue_nr];
796 		sfq = queue->priv;
797 
798 		auxtrace_heap__pop(&sf->heap);
799 		if (sf->heap.heap_cnt) {
800 			ts = sf->heap.heap_array[0].ordinal + 1;
801 			if (ts > timestamp)
802 				ts = timestamp;
803 		} else {
804 			ts = timestamp;
805 		}
806 
807 		ret = s390_cpumsf_run_decoder(sfq, &ts);
808 		if (ret < 0) {
809 			auxtrace_heap__add(&sf->heap, queue_nr, ts);
810 			return ret;
811 		}
812 		if (!ret) {
813 			ret = auxtrace_heap__add(&sf->heap, queue_nr, ts);
814 			if (ret < 0)
815 				return ret;
816 		}
817 	}
818 	return 0;
819 }
820 
821 static int s390_cpumsf_synth_error(struct s390_cpumsf *sf, int code, int cpu,
822 				   pid_t pid, pid_t tid, u64 ip, u64 timestamp)
823 {
824 	char msg[MAX_AUXTRACE_ERROR_MSG];
825 	union perf_event event;
826 	int err;
827 
828 	strncpy(msg, "Lost Auxiliary Trace Buffer", sizeof(msg) - 1);
829 	auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
830 			     code, cpu, pid, tid, ip, msg, timestamp);
831 
832 	err = perf_session__deliver_synth_event(sf->session, &event, NULL);
833 	if (err)
834 		pr_err("s390 Auxiliary Trace: failed to deliver error event,"
835 			"error %d\n", err);
836 	return err;
837 }
838 
839 static int s390_cpumsf_lost(struct s390_cpumsf *sf, struct perf_sample *sample)
840 {
841 	return s390_cpumsf_synth_error(sf, 1, sample->cpu,
842 				       sample->pid, sample->tid, 0,
843 				       sample->time);
844 }
845 
846 static int
847 s390_cpumsf_process_event(struct perf_session *session,
848 			  union perf_event *event,
849 			  struct perf_sample *sample,
850 			  struct perf_tool *tool)
851 {
852 	struct s390_cpumsf *sf = container_of(session->auxtrace,
853 					      struct s390_cpumsf,
854 					      auxtrace);
855 	u64 timestamp = sample->time;
856 	struct perf_evsel *ev_bc000;
857 
858 	int err = 0;
859 
860 	if (dump_trace)
861 		return 0;
862 
863 	if (!tool->ordered_events) {
864 		pr_err("s390 Auxiliary Trace requires ordered events\n");
865 		return -EINVAL;
866 	}
867 
868 	if (event->header.type == PERF_RECORD_SAMPLE &&
869 	    sample->raw_size) {
870 		/* Handle event with raw data */
871 		ev_bc000 = perf_evlist__event2evsel(session->evlist, event);
872 		if (ev_bc000 &&
873 		    ev_bc000->attr.config == PERF_EVENT_CPUM_CF_DIAG)
874 			err = s390_cpumcf_dumpctr(sf, sample);
875 		return err;
876 	}
877 
878 	if (event->header.type == PERF_RECORD_AUX &&
879 	    event->aux.flags & PERF_AUX_FLAG_TRUNCATED)
880 		return s390_cpumsf_lost(sf, sample);
881 
882 	if (timestamp) {
883 		err = s390_cpumsf_update_queues(sf, timestamp);
884 		if (!err)
885 			err = s390_cpumsf_process_queues(sf, timestamp);
886 	}
887 	return err;
888 }
889 
890 struct s390_cpumsf_synth {
891 	struct perf_tool cpumsf_tool;
892 	struct perf_session *session;
893 };
894 
895 static int
896 s390_cpumsf_process_auxtrace_event(struct perf_session *session,
897 				   union perf_event *event __maybe_unused,
898 				   struct perf_tool *tool __maybe_unused)
899 {
900 	struct s390_cpumsf *sf = container_of(session->auxtrace,
901 					      struct s390_cpumsf,
902 					      auxtrace);
903 
904 	int fd = perf_data__fd(session->data);
905 	struct auxtrace_buffer *buffer;
906 	off_t data_offset;
907 	int err;
908 
909 	if (sf->data_queued)
910 		return 0;
911 
912 	if (perf_data__is_pipe(session->data)) {
913 		data_offset = 0;
914 	} else {
915 		data_offset = lseek(fd, 0, SEEK_CUR);
916 		if (data_offset == -1)
917 			return -errno;
918 	}
919 
920 	err = auxtrace_queues__add_event(&sf->queues, session, event,
921 					 data_offset, &buffer);
922 	if (err)
923 		return err;
924 
925 	/* Dump here after copying piped trace out of the pipe */
926 	if (dump_trace) {
927 		if (auxtrace_buffer__get_data(buffer, fd)) {
928 			s390_cpumsf_dump_event(sf, buffer->data,
929 					       buffer->size);
930 			auxtrace_buffer__put_data(buffer);
931 		}
932 	}
933 	return 0;
934 }
935 
936 static void s390_cpumsf_free_events(struct perf_session *session __maybe_unused)
937 {
938 }
939 
940 static int s390_cpumsf_flush(struct perf_session *session __maybe_unused,
941 			     struct perf_tool *tool __maybe_unused)
942 {
943 	return 0;
944 }
945 
946 static void s390_cpumsf_free_queues(struct perf_session *session)
947 {
948 	struct s390_cpumsf *sf = container_of(session->auxtrace,
949 					      struct s390_cpumsf,
950 					      auxtrace);
951 	struct auxtrace_queues *queues = &sf->queues;
952 	unsigned int i;
953 
954 	for (i = 0; i < queues->nr_queues; i++) {
955 		struct s390_cpumsf_queue *sfq = (struct s390_cpumsf_queue *)
956 						queues->queue_array[i].priv;
957 
958 		if (sfq != NULL) {
959 			if (sfq->logfile) {
960 				fclose(sfq->logfile);
961 				sfq->logfile = NULL;
962 			}
963 			if (sfq->logfile_ctr) {
964 				fclose(sfq->logfile_ctr);
965 				sfq->logfile_ctr = NULL;
966 			}
967 		}
968 		zfree(&queues->queue_array[i].priv);
969 	}
970 	auxtrace_queues__free(queues);
971 }
972 
973 static void s390_cpumsf_free(struct perf_session *session)
974 {
975 	struct s390_cpumsf *sf = container_of(session->auxtrace,
976 					      struct s390_cpumsf,
977 					      auxtrace);
978 
979 	auxtrace_heap__free(&sf->heap);
980 	s390_cpumsf_free_queues(session);
981 	session->auxtrace = NULL;
982 	free(sf->logdir);
983 	free(sf);
984 }
985 
986 static int s390_cpumsf_get_type(const char *cpuid)
987 {
988 	int ret, family = 0;
989 
990 	ret = sscanf(cpuid, "%*[^,],%u", &family);
991 	return (ret == 1) ? family : 0;
992 }
993 
994 /* Check itrace options set on perf report command.
995  * Return true, if none are set or all options specified can be
996  * handled on s390 (currently only option 'd' for logging.
997  * Return false otherwise.
998  */
999 static bool check_auxtrace_itrace(struct itrace_synth_opts *itops)
1000 {
1001 	bool ison = false;
1002 
1003 	if (!itops || !itops->set)
1004 		return true;
1005 	ison = itops->inject || itops->instructions || itops->branches ||
1006 		itops->transactions || itops->ptwrites ||
1007 		itops->pwr_events || itops->errors ||
1008 		itops->dont_decode || itops->calls || itops->returns ||
1009 		itops->callchain || itops->thread_stack ||
1010 		itops->last_branch;
1011 	if (!ison)
1012 		return true;
1013 	pr_err("Unsupported --itrace options specified\n");
1014 	return false;
1015 }
1016 
1017 /* Check for AUXTRACE dump directory if it is needed.
1018  * On failure print an error message but continue.
1019  * Return 0 on wrong keyword in config file and 1 otherwise.
1020  */
1021 static int s390_cpumsf__config(const char *var, const char *value, void *cb)
1022 {
1023 	struct s390_cpumsf *sf = cb;
1024 	struct stat stbuf;
1025 	int rc;
1026 
1027 	if (strcmp(var, "auxtrace.dumpdir"))
1028 		return 0;
1029 	sf->logdir = strdup(value);
1030 	if (sf->logdir == NULL) {
1031 		pr_err("Failed to find auxtrace log directory %s,"
1032 		       " continue with current directory...\n", value);
1033 		return 1;
1034 	}
1035 	rc = stat(sf->logdir, &stbuf);
1036 	if (rc == -1 || !S_ISDIR(stbuf.st_mode)) {
1037 		pr_err("Missing auxtrace log directory %s,"
1038 		       " continue with current directory...\n", value);
1039 		free(sf->logdir);
1040 		sf->logdir = NULL;
1041 	}
1042 	return 1;
1043 }
1044 
1045 int s390_cpumsf_process_auxtrace_info(union perf_event *event,
1046 				      struct perf_session *session)
1047 {
1048 	struct auxtrace_info_event *auxtrace_info = &event->auxtrace_info;
1049 	struct s390_cpumsf *sf;
1050 	int err;
1051 
1052 	if (auxtrace_info->header.size < sizeof(struct auxtrace_info_event))
1053 		return -EINVAL;
1054 
1055 	sf = zalloc(sizeof(struct s390_cpumsf));
1056 	if (sf == NULL)
1057 		return -ENOMEM;
1058 
1059 	if (!check_auxtrace_itrace(session->itrace_synth_opts)) {
1060 		err = -EINVAL;
1061 		goto err_free;
1062 	}
1063 	sf->use_logfile = session->itrace_synth_opts->log;
1064 	if (sf->use_logfile)
1065 		perf_config(s390_cpumsf__config, sf);
1066 
1067 	err = auxtrace_queues__init(&sf->queues);
1068 	if (err)
1069 		goto err_free;
1070 
1071 	sf->session = session;
1072 	sf->machine = &session->machines.host; /* No kvm support */
1073 	sf->auxtrace_type = auxtrace_info->type;
1074 	sf->pmu_type = PERF_TYPE_RAW;
1075 	sf->machine_type = s390_cpumsf_get_type(session->evlist->env->cpuid);
1076 
1077 	sf->auxtrace.process_event = s390_cpumsf_process_event;
1078 	sf->auxtrace.process_auxtrace_event = s390_cpumsf_process_auxtrace_event;
1079 	sf->auxtrace.flush_events = s390_cpumsf_flush;
1080 	sf->auxtrace.free_events = s390_cpumsf_free_events;
1081 	sf->auxtrace.free = s390_cpumsf_free;
1082 	session->auxtrace = &sf->auxtrace;
1083 
1084 	if (dump_trace)
1085 		return 0;
1086 
1087 	err = auxtrace_queues__process_index(&sf->queues, session);
1088 	if (err)
1089 		goto err_free_queues;
1090 
1091 	if (sf->queues.populated)
1092 		sf->data_queued = true;
1093 
1094 	return 0;
1095 
1096 err_free_queues:
1097 	auxtrace_queues__free(&sf->queues);
1098 	session->auxtrace = NULL;
1099 err_free:
1100 	free(sf->logdir);
1101 	free(sf);
1102 	return err;
1103 }
1104