xref: /linux/tools/perf/util/cs-etm.c (revision bd628c1bed7902ec1f24ba0fe70758949146abbe)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright(C) 2015-2018 Linaro Limited.
4  *
5  * Author: Tor Jeremiassen <tor@ti.com>
6  * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
7  */
8 
9 #include <linux/bitops.h>
10 #include <linux/err.h>
11 #include <linux/kernel.h>
12 #include <linux/log2.h>
13 #include <linux/types.h>
14 
15 #include <stdlib.h>
16 
17 #include "auxtrace.h"
18 #include "color.h"
19 #include "cs-etm.h"
20 #include "cs-etm-decoder/cs-etm-decoder.h"
21 #include "debug.h"
22 #include "evlist.h"
23 #include "intlist.h"
24 #include "machine.h"
25 #include "map.h"
26 #include "perf.h"
27 #include "thread.h"
28 #include "thread_map.h"
29 #include "thread-stack.h"
30 #include "util.h"
31 
32 #define MAX_TIMESTAMP (~0ULL)
33 
34 struct cs_etm_auxtrace {
35 	struct auxtrace auxtrace;
36 	struct auxtrace_queues queues;
37 	struct auxtrace_heap heap;
38 	struct itrace_synth_opts synth_opts;
39 	struct perf_session *session;
40 	struct machine *machine;
41 	struct thread *unknown_thread;
42 
43 	u8 timeless_decoding;
44 	u8 snapshot_mode;
45 	u8 data_queued;
46 	u8 sample_branches;
47 	u8 sample_instructions;
48 
49 	int num_cpu;
50 	u32 auxtrace_type;
51 	u64 branches_sample_type;
52 	u64 branches_id;
53 	u64 instructions_sample_type;
54 	u64 instructions_sample_period;
55 	u64 instructions_id;
56 	u64 **metadata;
57 	u64 kernel_start;
58 	unsigned int pmu_type;
59 };
60 
61 struct cs_etm_queue {
62 	struct cs_etm_auxtrace *etm;
63 	struct thread *thread;
64 	struct cs_etm_decoder *decoder;
65 	struct auxtrace_buffer *buffer;
66 	const struct cs_etm_state *state;
67 	union perf_event *event_buf;
68 	unsigned int queue_nr;
69 	pid_t pid, tid;
70 	int cpu;
71 	u64 time;
72 	u64 timestamp;
73 	u64 offset;
74 	u64 period_instructions;
75 	struct branch_stack *last_branch;
76 	struct branch_stack *last_branch_rb;
77 	size_t last_branch_pos;
78 	struct cs_etm_packet *prev_packet;
79 	struct cs_etm_packet *packet;
80 };
81 
82 static int cs_etm__update_queues(struct cs_etm_auxtrace *etm);
83 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
84 					   pid_t tid, u64 time_);
85 
86 /* PTMs ETMIDR [11:8] set to b0011 */
87 #define ETMIDR_PTM_VERSION 0x00000300
88 
89 static u32 cs_etm__get_v7_protocol_version(u32 etmidr)
90 {
91 	etmidr &= ETMIDR_PTM_VERSION;
92 
93 	if (etmidr == ETMIDR_PTM_VERSION)
94 		return CS_ETM_PROTO_PTM;
95 
96 	return CS_ETM_PROTO_ETMV3;
97 }
98 
99 static void cs_etm__packet_dump(const char *pkt_string)
100 {
101 	const char *color = PERF_COLOR_BLUE;
102 	int len = strlen(pkt_string);
103 
104 	if (len && (pkt_string[len-1] == '\n'))
105 		color_fprintf(stdout, color, "	%s", pkt_string);
106 	else
107 		color_fprintf(stdout, color, "	%s\n", pkt_string);
108 
109 	fflush(stdout);
110 }
111 
112 static void cs_etm__dump_event(struct cs_etm_auxtrace *etm,
113 			       struct auxtrace_buffer *buffer)
114 {
115 	int i, ret;
116 	const char *color = PERF_COLOR_BLUE;
117 	struct cs_etm_decoder_params d_params;
118 	struct cs_etm_trace_params *t_params;
119 	struct cs_etm_decoder *decoder;
120 	size_t buffer_used = 0;
121 
122 	fprintf(stdout, "\n");
123 	color_fprintf(stdout, color,
124 		     ". ... CoreSight ETM Trace data: size %zu bytes\n",
125 		     buffer->size);
126 
127 	/* Use metadata to fill in trace parameters for trace decoder */
128 	t_params = zalloc(sizeof(*t_params) * etm->num_cpu);
129 	for (i = 0; i < etm->num_cpu; i++) {
130 		if (etm->metadata[i][CS_ETM_MAGIC] == __perf_cs_etmv3_magic) {
131 			u32 etmidr = etm->metadata[i][CS_ETM_ETMIDR];
132 
133 			t_params[i].protocol =
134 					cs_etm__get_v7_protocol_version(etmidr);
135 			t_params[i].etmv3.reg_ctrl =
136 					etm->metadata[i][CS_ETM_ETMCR];
137 			t_params[i].etmv3.reg_trc_id =
138 					etm->metadata[i][CS_ETM_ETMTRACEIDR];
139 		} else if (etm->metadata[i][CS_ETM_MAGIC] ==
140 						      __perf_cs_etmv4_magic) {
141 			t_params[i].protocol = CS_ETM_PROTO_ETMV4i;
142 			t_params[i].etmv4.reg_idr0 =
143 					etm->metadata[i][CS_ETMV4_TRCIDR0];
144 			t_params[i].etmv4.reg_idr1 =
145 					etm->metadata[i][CS_ETMV4_TRCIDR1];
146 			t_params[i].etmv4.reg_idr2 =
147 					etm->metadata[i][CS_ETMV4_TRCIDR2];
148 			t_params[i].etmv4.reg_idr8 =
149 					etm->metadata[i][CS_ETMV4_TRCIDR8];
150 			t_params[i].etmv4.reg_configr =
151 					etm->metadata[i][CS_ETMV4_TRCCONFIGR];
152 			t_params[i].etmv4.reg_traceidr =
153 					etm->metadata[i][CS_ETMV4_TRCTRACEIDR];
154 		}
155 	}
156 
157 	/* Set decoder parameters to simply print the trace packets */
158 	d_params.packet_printer = cs_etm__packet_dump;
159 	d_params.operation = CS_ETM_OPERATION_PRINT;
160 	d_params.formatted = true;
161 	d_params.fsyncs = false;
162 	d_params.hsyncs = false;
163 	d_params.frame_aligned = true;
164 
165 	decoder = cs_etm_decoder__new(etm->num_cpu, &d_params, t_params);
166 
167 	zfree(&t_params);
168 
169 	if (!decoder)
170 		return;
171 	do {
172 		size_t consumed;
173 
174 		ret = cs_etm_decoder__process_data_block(
175 				decoder, buffer->offset,
176 				&((u8 *)buffer->data)[buffer_used],
177 				buffer->size - buffer_used, &consumed);
178 		if (ret)
179 			break;
180 
181 		buffer_used += consumed;
182 	} while (buffer_used < buffer->size);
183 
184 	cs_etm_decoder__free(decoder);
185 }
186 
187 static int cs_etm__flush_events(struct perf_session *session,
188 				struct perf_tool *tool)
189 {
190 	int ret;
191 	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
192 						   struct cs_etm_auxtrace,
193 						   auxtrace);
194 	if (dump_trace)
195 		return 0;
196 
197 	if (!tool->ordered_events)
198 		return -EINVAL;
199 
200 	if (!etm->timeless_decoding)
201 		return -EINVAL;
202 
203 	ret = cs_etm__update_queues(etm);
204 
205 	if (ret < 0)
206 		return ret;
207 
208 	return cs_etm__process_timeless_queues(etm, -1, MAX_TIMESTAMP - 1);
209 }
210 
211 static void cs_etm__free_queue(void *priv)
212 {
213 	struct cs_etm_queue *etmq = priv;
214 
215 	if (!etmq)
216 		return;
217 
218 	thread__zput(etmq->thread);
219 	cs_etm_decoder__free(etmq->decoder);
220 	zfree(&etmq->event_buf);
221 	zfree(&etmq->last_branch);
222 	zfree(&etmq->last_branch_rb);
223 	zfree(&etmq->prev_packet);
224 	zfree(&etmq->packet);
225 	free(etmq);
226 }
227 
228 static void cs_etm__free_events(struct perf_session *session)
229 {
230 	unsigned int i;
231 	struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
232 						   struct cs_etm_auxtrace,
233 						   auxtrace);
234 	struct auxtrace_queues *queues = &aux->queues;
235 
236 	for (i = 0; i < queues->nr_queues; i++) {
237 		cs_etm__free_queue(queues->queue_array[i].priv);
238 		queues->queue_array[i].priv = NULL;
239 	}
240 
241 	auxtrace_queues__free(queues);
242 }
243 
244 static void cs_etm__free(struct perf_session *session)
245 {
246 	int i;
247 	struct int_node *inode, *tmp;
248 	struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
249 						   struct cs_etm_auxtrace,
250 						   auxtrace);
251 	cs_etm__free_events(session);
252 	session->auxtrace = NULL;
253 
254 	/* First remove all traceID/CPU# nodes for the RB tree */
255 	intlist__for_each_entry_safe(inode, tmp, traceid_list)
256 		intlist__remove(traceid_list, inode);
257 	/* Then the RB tree itself */
258 	intlist__delete(traceid_list);
259 
260 	for (i = 0; i < aux->num_cpu; i++)
261 		zfree(&aux->metadata[i]);
262 
263 	thread__zput(aux->unknown_thread);
264 	zfree(&aux->metadata);
265 	zfree(&aux);
266 }
267 
268 static u8 cs_etm__cpu_mode(struct cs_etm_queue *etmq, u64 address)
269 {
270 	struct machine *machine;
271 
272 	machine = etmq->etm->machine;
273 
274 	if (address >= etmq->etm->kernel_start) {
275 		if (machine__is_host(machine))
276 			return PERF_RECORD_MISC_KERNEL;
277 		else
278 			return PERF_RECORD_MISC_GUEST_KERNEL;
279 	} else {
280 		if (machine__is_host(machine))
281 			return PERF_RECORD_MISC_USER;
282 		else if (perf_guest)
283 			return PERF_RECORD_MISC_GUEST_USER;
284 		else
285 			return PERF_RECORD_MISC_HYPERVISOR;
286 	}
287 }
288 
289 static u32 cs_etm__mem_access(struct cs_etm_queue *etmq, u64 address,
290 			      size_t size, u8 *buffer)
291 {
292 	u8  cpumode;
293 	u64 offset;
294 	int len;
295 	struct	 thread *thread;
296 	struct	 machine *machine;
297 	struct	 addr_location al;
298 
299 	if (!etmq)
300 		return -1;
301 
302 	machine = etmq->etm->machine;
303 	cpumode = cs_etm__cpu_mode(etmq, address);
304 
305 	thread = etmq->thread;
306 	if (!thread) {
307 		if (cpumode != PERF_RECORD_MISC_KERNEL)
308 			return -EINVAL;
309 		thread = etmq->etm->unknown_thread;
310 	}
311 
312 	if (!thread__find_map(thread, cpumode, address, &al) || !al.map->dso)
313 		return 0;
314 
315 	if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
316 	    dso__data_status_seen(al.map->dso, DSO_DATA_STATUS_SEEN_ITRACE))
317 		return 0;
318 
319 	offset = al.map->map_ip(al.map, address);
320 
321 	map__load(al.map);
322 
323 	len = dso__data_read_offset(al.map->dso, machine, offset, buffer, size);
324 
325 	if (len <= 0)
326 		return 0;
327 
328 	return len;
329 }
330 
331 static struct cs_etm_queue *cs_etm__alloc_queue(struct cs_etm_auxtrace *etm,
332 						unsigned int queue_nr)
333 {
334 	int i;
335 	struct cs_etm_decoder_params d_params;
336 	struct cs_etm_trace_params  *t_params;
337 	struct cs_etm_queue *etmq;
338 	size_t szp = sizeof(struct cs_etm_packet);
339 
340 	etmq = zalloc(sizeof(*etmq));
341 	if (!etmq)
342 		return NULL;
343 
344 	etmq->packet = zalloc(szp);
345 	if (!etmq->packet)
346 		goto out_free;
347 
348 	if (etm->synth_opts.last_branch || etm->sample_branches) {
349 		etmq->prev_packet = zalloc(szp);
350 		if (!etmq->prev_packet)
351 			goto out_free;
352 	}
353 
354 	if (etm->synth_opts.last_branch) {
355 		size_t sz = sizeof(struct branch_stack);
356 
357 		sz += etm->synth_opts.last_branch_sz *
358 		      sizeof(struct branch_entry);
359 		etmq->last_branch = zalloc(sz);
360 		if (!etmq->last_branch)
361 			goto out_free;
362 		etmq->last_branch_rb = zalloc(sz);
363 		if (!etmq->last_branch_rb)
364 			goto out_free;
365 	}
366 
367 	etmq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
368 	if (!etmq->event_buf)
369 		goto out_free;
370 
371 	etmq->etm = etm;
372 	etmq->queue_nr = queue_nr;
373 	etmq->pid = -1;
374 	etmq->tid = -1;
375 	etmq->cpu = -1;
376 
377 	/* Use metadata to fill in trace parameters for trace decoder */
378 	t_params = zalloc(sizeof(*t_params) * etm->num_cpu);
379 
380 	if (!t_params)
381 		goto out_free;
382 
383 	for (i = 0; i < etm->num_cpu; i++) {
384 		if (etm->metadata[i][CS_ETM_MAGIC] == __perf_cs_etmv3_magic) {
385 			u32 etmidr = etm->metadata[i][CS_ETM_ETMIDR];
386 
387 			t_params[i].protocol =
388 					cs_etm__get_v7_protocol_version(etmidr);
389 			t_params[i].etmv3.reg_ctrl =
390 					etm->metadata[i][CS_ETM_ETMCR];
391 			t_params[i].etmv3.reg_trc_id =
392 					etm->metadata[i][CS_ETM_ETMTRACEIDR];
393 		} else if (etm->metadata[i][CS_ETM_MAGIC] ==
394 							__perf_cs_etmv4_magic) {
395 			t_params[i].protocol = CS_ETM_PROTO_ETMV4i;
396 			t_params[i].etmv4.reg_idr0 =
397 					etm->metadata[i][CS_ETMV4_TRCIDR0];
398 			t_params[i].etmv4.reg_idr1 =
399 					etm->metadata[i][CS_ETMV4_TRCIDR1];
400 			t_params[i].etmv4.reg_idr2 =
401 					etm->metadata[i][CS_ETMV4_TRCIDR2];
402 			t_params[i].etmv4.reg_idr8 =
403 					etm->metadata[i][CS_ETMV4_TRCIDR8];
404 			t_params[i].etmv4.reg_configr =
405 					etm->metadata[i][CS_ETMV4_TRCCONFIGR];
406 			t_params[i].etmv4.reg_traceidr =
407 					etm->metadata[i][CS_ETMV4_TRCTRACEIDR];
408 		}
409 	}
410 
411 	/* Set decoder parameters to simply print the trace packets */
412 	d_params.packet_printer = cs_etm__packet_dump;
413 	d_params.operation = CS_ETM_OPERATION_DECODE;
414 	d_params.formatted = true;
415 	d_params.fsyncs = false;
416 	d_params.hsyncs = false;
417 	d_params.frame_aligned = true;
418 	d_params.data = etmq;
419 
420 	etmq->decoder = cs_etm_decoder__new(etm->num_cpu, &d_params, t_params);
421 
422 	zfree(&t_params);
423 
424 	if (!etmq->decoder)
425 		goto out_free;
426 
427 	/*
428 	 * Register a function to handle all memory accesses required by
429 	 * the trace decoder library.
430 	 */
431 	if (cs_etm_decoder__add_mem_access_cb(etmq->decoder,
432 					      0x0L, ((u64) -1L),
433 					      cs_etm__mem_access))
434 		goto out_free_decoder;
435 
436 	etmq->offset = 0;
437 	etmq->period_instructions = 0;
438 
439 	return etmq;
440 
441 out_free_decoder:
442 	cs_etm_decoder__free(etmq->decoder);
443 out_free:
444 	zfree(&etmq->event_buf);
445 	zfree(&etmq->last_branch);
446 	zfree(&etmq->last_branch_rb);
447 	zfree(&etmq->prev_packet);
448 	zfree(&etmq->packet);
449 	free(etmq);
450 
451 	return NULL;
452 }
453 
454 static int cs_etm__setup_queue(struct cs_etm_auxtrace *etm,
455 			       struct auxtrace_queue *queue,
456 			       unsigned int queue_nr)
457 {
458 	struct cs_etm_queue *etmq = queue->priv;
459 
460 	if (list_empty(&queue->head) || etmq)
461 		return 0;
462 
463 	etmq = cs_etm__alloc_queue(etm, queue_nr);
464 
465 	if (!etmq)
466 		return -ENOMEM;
467 
468 	queue->priv = etmq;
469 
470 	if (queue->cpu != -1)
471 		etmq->cpu = queue->cpu;
472 
473 	etmq->tid = queue->tid;
474 
475 	return 0;
476 }
477 
478 static int cs_etm__setup_queues(struct cs_etm_auxtrace *etm)
479 {
480 	unsigned int i;
481 	int ret;
482 
483 	for (i = 0; i < etm->queues.nr_queues; i++) {
484 		ret = cs_etm__setup_queue(etm, &etm->queues.queue_array[i], i);
485 		if (ret)
486 			return ret;
487 	}
488 
489 	return 0;
490 }
491 
492 static int cs_etm__update_queues(struct cs_etm_auxtrace *etm)
493 {
494 	if (etm->queues.new_data) {
495 		etm->queues.new_data = false;
496 		return cs_etm__setup_queues(etm);
497 	}
498 
499 	return 0;
500 }
501 
502 static inline void cs_etm__copy_last_branch_rb(struct cs_etm_queue *etmq)
503 {
504 	struct branch_stack *bs_src = etmq->last_branch_rb;
505 	struct branch_stack *bs_dst = etmq->last_branch;
506 	size_t nr = 0;
507 
508 	/*
509 	 * Set the number of records before early exit: ->nr is used to
510 	 * determine how many branches to copy from ->entries.
511 	 */
512 	bs_dst->nr = bs_src->nr;
513 
514 	/*
515 	 * Early exit when there is nothing to copy.
516 	 */
517 	if (!bs_src->nr)
518 		return;
519 
520 	/*
521 	 * As bs_src->entries is a circular buffer, we need to copy from it in
522 	 * two steps.  First, copy the branches from the most recently inserted
523 	 * branch ->last_branch_pos until the end of bs_src->entries buffer.
524 	 */
525 	nr = etmq->etm->synth_opts.last_branch_sz - etmq->last_branch_pos;
526 	memcpy(&bs_dst->entries[0],
527 	       &bs_src->entries[etmq->last_branch_pos],
528 	       sizeof(struct branch_entry) * nr);
529 
530 	/*
531 	 * If we wrapped around at least once, the branches from the beginning
532 	 * of the bs_src->entries buffer and until the ->last_branch_pos element
533 	 * are older valid branches: copy them over.  The total number of
534 	 * branches copied over will be equal to the number of branches asked by
535 	 * the user in last_branch_sz.
536 	 */
537 	if (bs_src->nr >= etmq->etm->synth_opts.last_branch_sz) {
538 		memcpy(&bs_dst->entries[nr],
539 		       &bs_src->entries[0],
540 		       sizeof(struct branch_entry) * etmq->last_branch_pos);
541 	}
542 }
543 
544 static inline void cs_etm__reset_last_branch_rb(struct cs_etm_queue *etmq)
545 {
546 	etmq->last_branch_pos = 0;
547 	etmq->last_branch_rb->nr = 0;
548 }
549 
550 static inline int cs_etm__t32_instr_size(struct cs_etm_queue *etmq,
551 					 u64 addr) {
552 	u8 instrBytes[2];
553 
554 	cs_etm__mem_access(etmq, addr, ARRAY_SIZE(instrBytes), instrBytes);
555 	/*
556 	 * T32 instruction size is indicated by bits[15:11] of the first
557 	 * 16-bit word of the instruction: 0b11101, 0b11110 and 0b11111
558 	 * denote a 32-bit instruction.
559 	 */
560 	return ((instrBytes[1] & 0xF8) >= 0xE8) ? 4 : 2;
561 }
562 
563 static inline u64 cs_etm__first_executed_instr(struct cs_etm_packet *packet)
564 {
565 	/* Returns 0 for the CS_ETM_DISCONTINUITY packet */
566 	if (packet->sample_type == CS_ETM_DISCONTINUITY)
567 		return 0;
568 
569 	return packet->start_addr;
570 }
571 
572 static inline
573 u64 cs_etm__last_executed_instr(const struct cs_etm_packet *packet)
574 {
575 	/* Returns 0 for the CS_ETM_DISCONTINUITY packet */
576 	if (packet->sample_type == CS_ETM_DISCONTINUITY)
577 		return 0;
578 
579 	return packet->end_addr - packet->last_instr_size;
580 }
581 
582 static inline u64 cs_etm__instr_addr(struct cs_etm_queue *etmq,
583 				     const struct cs_etm_packet *packet,
584 				     u64 offset)
585 {
586 	if (packet->isa == CS_ETM_ISA_T32) {
587 		u64 addr = packet->start_addr;
588 
589 		while (offset > 0) {
590 			addr += cs_etm__t32_instr_size(etmq, addr);
591 			offset--;
592 		}
593 		return addr;
594 	}
595 
596 	/* Assume a 4 byte instruction size (A32/A64) */
597 	return packet->start_addr + offset * 4;
598 }
599 
600 static void cs_etm__update_last_branch_rb(struct cs_etm_queue *etmq)
601 {
602 	struct branch_stack *bs = etmq->last_branch_rb;
603 	struct branch_entry *be;
604 
605 	/*
606 	 * The branches are recorded in a circular buffer in reverse
607 	 * chronological order: we start recording from the last element of the
608 	 * buffer down.  After writing the first element of the stack, move the
609 	 * insert position back to the end of the buffer.
610 	 */
611 	if (!etmq->last_branch_pos)
612 		etmq->last_branch_pos = etmq->etm->synth_opts.last_branch_sz;
613 
614 	etmq->last_branch_pos -= 1;
615 
616 	be       = &bs->entries[etmq->last_branch_pos];
617 	be->from = cs_etm__last_executed_instr(etmq->prev_packet);
618 	be->to	 = cs_etm__first_executed_instr(etmq->packet);
619 	/* No support for mispredict */
620 	be->flags.mispred = 0;
621 	be->flags.predicted = 1;
622 
623 	/*
624 	 * Increment bs->nr until reaching the number of last branches asked by
625 	 * the user on the command line.
626 	 */
627 	if (bs->nr < etmq->etm->synth_opts.last_branch_sz)
628 		bs->nr += 1;
629 }
630 
631 static int cs_etm__inject_event(union perf_event *event,
632 			       struct perf_sample *sample, u64 type)
633 {
634 	event->header.size = perf_event__sample_event_size(sample, type, 0);
635 	return perf_event__synthesize_sample(event, type, 0, sample);
636 }
637 
638 
639 static int
640 cs_etm__get_trace(struct cs_etm_buffer *buff, struct cs_etm_queue *etmq)
641 {
642 	struct auxtrace_buffer *aux_buffer = etmq->buffer;
643 	struct auxtrace_buffer *old_buffer = aux_buffer;
644 	struct auxtrace_queue *queue;
645 
646 	queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
647 
648 	aux_buffer = auxtrace_buffer__next(queue, aux_buffer);
649 
650 	/* If no more data, drop the previous auxtrace_buffer and return */
651 	if (!aux_buffer) {
652 		if (old_buffer)
653 			auxtrace_buffer__drop_data(old_buffer);
654 		buff->len = 0;
655 		return 0;
656 	}
657 
658 	etmq->buffer = aux_buffer;
659 
660 	/* If the aux_buffer doesn't have data associated, try to load it */
661 	if (!aux_buffer->data) {
662 		/* get the file desc associated with the perf data file */
663 		int fd = perf_data__fd(etmq->etm->session->data);
664 
665 		aux_buffer->data = auxtrace_buffer__get_data(aux_buffer, fd);
666 		if (!aux_buffer->data)
667 			return -ENOMEM;
668 	}
669 
670 	/* If valid, drop the previous buffer */
671 	if (old_buffer)
672 		auxtrace_buffer__drop_data(old_buffer);
673 
674 	buff->offset = aux_buffer->offset;
675 	buff->len = aux_buffer->size;
676 	buff->buf = aux_buffer->data;
677 
678 	buff->ref_timestamp = aux_buffer->reference;
679 
680 	return buff->len;
681 }
682 
683 static void cs_etm__set_pid_tid_cpu(struct cs_etm_auxtrace *etm,
684 				    struct auxtrace_queue *queue)
685 {
686 	struct cs_etm_queue *etmq = queue->priv;
687 
688 	/* CPU-wide tracing isn't supported yet */
689 	if (queue->tid == -1)
690 		return;
691 
692 	if ((!etmq->thread) && (etmq->tid != -1))
693 		etmq->thread = machine__find_thread(etm->machine, -1,
694 						    etmq->tid);
695 
696 	if (etmq->thread) {
697 		etmq->pid = etmq->thread->pid_;
698 		if (queue->cpu == -1)
699 			etmq->cpu = etmq->thread->cpu;
700 	}
701 }
702 
703 static int cs_etm__synth_instruction_sample(struct cs_etm_queue *etmq,
704 					    u64 addr, u64 period)
705 {
706 	int ret = 0;
707 	struct cs_etm_auxtrace *etm = etmq->etm;
708 	union perf_event *event = etmq->event_buf;
709 	struct perf_sample sample = {.ip = 0,};
710 
711 	event->sample.header.type = PERF_RECORD_SAMPLE;
712 	event->sample.header.misc = cs_etm__cpu_mode(etmq, addr);
713 	event->sample.header.size = sizeof(struct perf_event_header);
714 
715 	sample.ip = addr;
716 	sample.pid = etmq->pid;
717 	sample.tid = etmq->tid;
718 	sample.id = etmq->etm->instructions_id;
719 	sample.stream_id = etmq->etm->instructions_id;
720 	sample.period = period;
721 	sample.cpu = etmq->packet->cpu;
722 	sample.flags = 0;
723 	sample.insn_len = 1;
724 	sample.cpumode = event->sample.header.misc;
725 
726 	if (etm->synth_opts.last_branch) {
727 		cs_etm__copy_last_branch_rb(etmq);
728 		sample.branch_stack = etmq->last_branch;
729 	}
730 
731 	if (etm->synth_opts.inject) {
732 		ret = cs_etm__inject_event(event, &sample,
733 					   etm->instructions_sample_type);
734 		if (ret)
735 			return ret;
736 	}
737 
738 	ret = perf_session__deliver_synth_event(etm->session, event, &sample);
739 
740 	if (ret)
741 		pr_err(
742 			"CS ETM Trace: failed to deliver instruction event, error %d\n",
743 			ret);
744 
745 	if (etm->synth_opts.last_branch)
746 		cs_etm__reset_last_branch_rb(etmq);
747 
748 	return ret;
749 }
750 
751 /*
752  * The cs etm packet encodes an instruction range between a branch target
753  * and the next taken branch. Generate sample accordingly.
754  */
755 static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq)
756 {
757 	int ret = 0;
758 	struct cs_etm_auxtrace *etm = etmq->etm;
759 	struct perf_sample sample = {.ip = 0,};
760 	union perf_event *event = etmq->event_buf;
761 	struct dummy_branch_stack {
762 		u64			nr;
763 		struct branch_entry	entries;
764 	} dummy_bs;
765 	u64 ip;
766 
767 	ip = cs_etm__last_executed_instr(etmq->prev_packet);
768 
769 	event->sample.header.type = PERF_RECORD_SAMPLE;
770 	event->sample.header.misc = cs_etm__cpu_mode(etmq, ip);
771 	event->sample.header.size = sizeof(struct perf_event_header);
772 
773 	sample.ip = ip;
774 	sample.pid = etmq->pid;
775 	sample.tid = etmq->tid;
776 	sample.addr = cs_etm__first_executed_instr(etmq->packet);
777 	sample.id = etmq->etm->branches_id;
778 	sample.stream_id = etmq->etm->branches_id;
779 	sample.period = 1;
780 	sample.cpu = etmq->packet->cpu;
781 	sample.flags = 0;
782 	sample.cpumode = event->sample.header.misc;
783 
784 	/*
785 	 * perf report cannot handle events without a branch stack
786 	 */
787 	if (etm->synth_opts.last_branch) {
788 		dummy_bs = (struct dummy_branch_stack){
789 			.nr = 1,
790 			.entries = {
791 				.from = sample.ip,
792 				.to = sample.addr,
793 			},
794 		};
795 		sample.branch_stack = (struct branch_stack *)&dummy_bs;
796 	}
797 
798 	if (etm->synth_opts.inject) {
799 		ret = cs_etm__inject_event(event, &sample,
800 					   etm->branches_sample_type);
801 		if (ret)
802 			return ret;
803 	}
804 
805 	ret = perf_session__deliver_synth_event(etm->session, event, &sample);
806 
807 	if (ret)
808 		pr_err(
809 		"CS ETM Trace: failed to deliver instruction event, error %d\n",
810 		ret);
811 
812 	return ret;
813 }
814 
815 struct cs_etm_synth {
816 	struct perf_tool dummy_tool;
817 	struct perf_session *session;
818 };
819 
820 static int cs_etm__event_synth(struct perf_tool *tool,
821 			       union perf_event *event,
822 			       struct perf_sample *sample __maybe_unused,
823 			       struct machine *machine __maybe_unused)
824 {
825 	struct cs_etm_synth *cs_etm_synth =
826 		      container_of(tool, struct cs_etm_synth, dummy_tool);
827 
828 	return perf_session__deliver_synth_event(cs_etm_synth->session,
829 						 event, NULL);
830 }
831 
832 static int cs_etm__synth_event(struct perf_session *session,
833 			       struct perf_event_attr *attr, u64 id)
834 {
835 	struct cs_etm_synth cs_etm_synth;
836 
837 	memset(&cs_etm_synth, 0, sizeof(struct cs_etm_synth));
838 	cs_etm_synth.session = session;
839 
840 	return perf_event__synthesize_attr(&cs_etm_synth.dummy_tool, attr, 1,
841 					   &id, cs_etm__event_synth);
842 }
843 
844 static int cs_etm__synth_events(struct cs_etm_auxtrace *etm,
845 				struct perf_session *session)
846 {
847 	struct perf_evlist *evlist = session->evlist;
848 	struct perf_evsel *evsel;
849 	struct perf_event_attr attr;
850 	bool found = false;
851 	u64 id;
852 	int err;
853 
854 	evlist__for_each_entry(evlist, evsel) {
855 		if (evsel->attr.type == etm->pmu_type) {
856 			found = true;
857 			break;
858 		}
859 	}
860 
861 	if (!found) {
862 		pr_debug("No selected events with CoreSight Trace data\n");
863 		return 0;
864 	}
865 
866 	memset(&attr, 0, sizeof(struct perf_event_attr));
867 	attr.size = sizeof(struct perf_event_attr);
868 	attr.type = PERF_TYPE_HARDWARE;
869 	attr.sample_type = evsel->attr.sample_type & PERF_SAMPLE_MASK;
870 	attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
871 			    PERF_SAMPLE_PERIOD;
872 	if (etm->timeless_decoding)
873 		attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
874 	else
875 		attr.sample_type |= PERF_SAMPLE_TIME;
876 
877 	attr.exclude_user = evsel->attr.exclude_user;
878 	attr.exclude_kernel = evsel->attr.exclude_kernel;
879 	attr.exclude_hv = evsel->attr.exclude_hv;
880 	attr.exclude_host = evsel->attr.exclude_host;
881 	attr.exclude_guest = evsel->attr.exclude_guest;
882 	attr.sample_id_all = evsel->attr.sample_id_all;
883 	attr.read_format = evsel->attr.read_format;
884 
885 	/* create new id val to be a fixed offset from evsel id */
886 	id = evsel->id[0] + 1000000000;
887 
888 	if (!id)
889 		id = 1;
890 
891 	if (etm->synth_opts.branches) {
892 		attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
893 		attr.sample_period = 1;
894 		attr.sample_type |= PERF_SAMPLE_ADDR;
895 		err = cs_etm__synth_event(session, &attr, id);
896 		if (err)
897 			return err;
898 		etm->sample_branches = true;
899 		etm->branches_sample_type = attr.sample_type;
900 		etm->branches_id = id;
901 		id += 1;
902 		attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
903 	}
904 
905 	if (etm->synth_opts.last_branch)
906 		attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
907 
908 	if (etm->synth_opts.instructions) {
909 		attr.config = PERF_COUNT_HW_INSTRUCTIONS;
910 		attr.sample_period = etm->synth_opts.period;
911 		etm->instructions_sample_period = attr.sample_period;
912 		err = cs_etm__synth_event(session, &attr, id);
913 		if (err)
914 			return err;
915 		etm->sample_instructions = true;
916 		etm->instructions_sample_type = attr.sample_type;
917 		etm->instructions_id = id;
918 		id += 1;
919 	}
920 
921 	return 0;
922 }
923 
924 static int cs_etm__sample(struct cs_etm_queue *etmq)
925 {
926 	struct cs_etm_auxtrace *etm = etmq->etm;
927 	struct cs_etm_packet *tmp;
928 	int ret;
929 	u64 instrs_executed = etmq->packet->instr_count;
930 
931 	etmq->period_instructions += instrs_executed;
932 
933 	/*
934 	 * Record a branch when the last instruction in
935 	 * PREV_PACKET is a branch.
936 	 */
937 	if (etm->synth_opts.last_branch &&
938 	    etmq->prev_packet &&
939 	    etmq->prev_packet->sample_type == CS_ETM_RANGE &&
940 	    etmq->prev_packet->last_instr_taken_branch)
941 		cs_etm__update_last_branch_rb(etmq);
942 
943 	if (etm->sample_instructions &&
944 	    etmq->period_instructions >= etm->instructions_sample_period) {
945 		/*
946 		 * Emit instruction sample periodically
947 		 * TODO: allow period to be defined in cycles and clock time
948 		 */
949 
950 		/* Get number of instructions executed after the sample point */
951 		u64 instrs_over = etmq->period_instructions -
952 			etm->instructions_sample_period;
953 
954 		/*
955 		 * Calculate the address of the sampled instruction (-1 as
956 		 * sample is reported as though instruction has just been
957 		 * executed, but PC has not advanced to next instruction)
958 		 */
959 		u64 offset = (instrs_executed - instrs_over - 1);
960 		u64 addr = cs_etm__instr_addr(etmq, etmq->packet, offset);
961 
962 		ret = cs_etm__synth_instruction_sample(
963 			etmq, addr, etm->instructions_sample_period);
964 		if (ret)
965 			return ret;
966 
967 		/* Carry remaining instructions into next sample period */
968 		etmq->period_instructions = instrs_over;
969 	}
970 
971 	if (etm->sample_branches && etmq->prev_packet) {
972 		bool generate_sample = false;
973 
974 		/* Generate sample for tracing on packet */
975 		if (etmq->prev_packet->sample_type == CS_ETM_DISCONTINUITY)
976 			generate_sample = true;
977 
978 		/* Generate sample for branch taken packet */
979 		if (etmq->prev_packet->sample_type == CS_ETM_RANGE &&
980 		    etmq->prev_packet->last_instr_taken_branch)
981 			generate_sample = true;
982 
983 		if (generate_sample) {
984 			ret = cs_etm__synth_branch_sample(etmq);
985 			if (ret)
986 				return ret;
987 		}
988 	}
989 
990 	if (etm->sample_branches || etm->synth_opts.last_branch) {
991 		/*
992 		 * Swap PACKET with PREV_PACKET: PACKET becomes PREV_PACKET for
993 		 * the next incoming packet.
994 		 */
995 		tmp = etmq->packet;
996 		etmq->packet = etmq->prev_packet;
997 		etmq->prev_packet = tmp;
998 	}
999 
1000 	return 0;
1001 }
1002 
1003 static int cs_etm__exception(struct cs_etm_queue *etmq)
1004 {
1005 	/*
1006 	 * When the exception packet is inserted, whether the last instruction
1007 	 * in previous range packet is taken branch or not, we need to force
1008 	 * to set 'prev_packet->last_instr_taken_branch' to true.  This ensures
1009 	 * to generate branch sample for the instruction range before the
1010 	 * exception is trapped to kernel or before the exception returning.
1011 	 *
1012 	 * The exception packet includes the dummy address values, so don't
1013 	 * swap PACKET with PREV_PACKET.  This keeps PREV_PACKET to be useful
1014 	 * for generating instruction and branch samples.
1015 	 */
1016 	if (etmq->prev_packet->sample_type == CS_ETM_RANGE)
1017 		etmq->prev_packet->last_instr_taken_branch = true;
1018 
1019 	return 0;
1020 }
1021 
1022 static int cs_etm__flush(struct cs_etm_queue *etmq)
1023 {
1024 	int err = 0;
1025 	struct cs_etm_auxtrace *etm = etmq->etm;
1026 	struct cs_etm_packet *tmp;
1027 
1028 	if (!etmq->prev_packet)
1029 		return 0;
1030 
1031 	/* Handle start tracing packet */
1032 	if (etmq->prev_packet->sample_type == CS_ETM_EMPTY)
1033 		goto swap_packet;
1034 
1035 	if (etmq->etm->synth_opts.last_branch &&
1036 	    etmq->prev_packet->sample_type == CS_ETM_RANGE) {
1037 		/*
1038 		 * Generate a last branch event for the branches left in the
1039 		 * circular buffer at the end of the trace.
1040 		 *
1041 		 * Use the address of the end of the last reported execution
1042 		 * range
1043 		 */
1044 		u64 addr = cs_etm__last_executed_instr(etmq->prev_packet);
1045 
1046 		err = cs_etm__synth_instruction_sample(
1047 			etmq, addr,
1048 			etmq->period_instructions);
1049 		if (err)
1050 			return err;
1051 
1052 		etmq->period_instructions = 0;
1053 
1054 	}
1055 
1056 	if (etm->sample_branches &&
1057 	    etmq->prev_packet->sample_type == CS_ETM_RANGE) {
1058 		err = cs_etm__synth_branch_sample(etmq);
1059 		if (err)
1060 			return err;
1061 	}
1062 
1063 swap_packet:
1064 	if (etm->sample_branches || etm->synth_opts.last_branch) {
1065 		/*
1066 		 * Swap PACKET with PREV_PACKET: PACKET becomes PREV_PACKET for
1067 		 * the next incoming packet.
1068 		 */
1069 		tmp = etmq->packet;
1070 		etmq->packet = etmq->prev_packet;
1071 		etmq->prev_packet = tmp;
1072 	}
1073 
1074 	return err;
1075 }
1076 
1077 static int cs_etm__end_block(struct cs_etm_queue *etmq)
1078 {
1079 	int err;
1080 
1081 	/*
1082 	 * It has no new packet coming and 'etmq->packet' contains the stale
1083 	 * packet which was set at the previous time with packets swapping;
1084 	 * so skip to generate branch sample to avoid stale packet.
1085 	 *
1086 	 * For this case only flush branch stack and generate a last branch
1087 	 * event for the branches left in the circular buffer at the end of
1088 	 * the trace.
1089 	 */
1090 	if (etmq->etm->synth_opts.last_branch &&
1091 	    etmq->prev_packet->sample_type == CS_ETM_RANGE) {
1092 		/*
1093 		 * Use the address of the end of the last reported execution
1094 		 * range.
1095 		 */
1096 		u64 addr = cs_etm__last_executed_instr(etmq->prev_packet);
1097 
1098 		err = cs_etm__synth_instruction_sample(
1099 			etmq, addr,
1100 			etmq->period_instructions);
1101 		if (err)
1102 			return err;
1103 
1104 		etmq->period_instructions = 0;
1105 	}
1106 
1107 	return 0;
1108 }
1109 
1110 static int cs_etm__run_decoder(struct cs_etm_queue *etmq)
1111 {
1112 	struct cs_etm_auxtrace *etm = etmq->etm;
1113 	struct cs_etm_buffer buffer;
1114 	size_t buffer_used, processed;
1115 	int err = 0;
1116 
1117 	if (!etm->kernel_start)
1118 		etm->kernel_start = machine__kernel_start(etm->machine);
1119 
1120 	/* Go through each buffer in the queue and decode them one by one */
1121 	while (1) {
1122 		buffer_used = 0;
1123 		memset(&buffer, 0, sizeof(buffer));
1124 		err = cs_etm__get_trace(&buffer, etmq);
1125 		if (err <= 0)
1126 			return err;
1127 		/*
1128 		 * We cannot assume consecutive blocks in the data file are
1129 		 * contiguous, reset the decoder to force re-sync.
1130 		 */
1131 		err = cs_etm_decoder__reset(etmq->decoder);
1132 		if (err != 0)
1133 			return err;
1134 
1135 		/* Run trace decoder until buffer consumed or end of trace */
1136 		do {
1137 			processed = 0;
1138 			err = cs_etm_decoder__process_data_block(
1139 				etmq->decoder,
1140 				etmq->offset,
1141 				&buffer.buf[buffer_used],
1142 				buffer.len - buffer_used,
1143 				&processed);
1144 			if (err)
1145 				return err;
1146 
1147 			etmq->offset += processed;
1148 			buffer_used += processed;
1149 
1150 			/* Process each packet in this chunk */
1151 			while (1) {
1152 				err = cs_etm_decoder__get_packet(etmq->decoder,
1153 								 etmq->packet);
1154 				if (err <= 0)
1155 					/*
1156 					 * Stop processing this chunk on
1157 					 * end of data or error
1158 					 */
1159 					break;
1160 
1161 				switch (etmq->packet->sample_type) {
1162 				case CS_ETM_RANGE:
1163 					/*
1164 					 * If the packet contains an instruction
1165 					 * range, generate instruction sequence
1166 					 * events.
1167 					 */
1168 					cs_etm__sample(etmq);
1169 					break;
1170 				case CS_ETM_EXCEPTION:
1171 				case CS_ETM_EXCEPTION_RET:
1172 					/*
1173 					 * If the exception packet is coming,
1174 					 * make sure the previous instruction
1175 					 * range packet to be handled properly.
1176 					 */
1177 					cs_etm__exception(etmq);
1178 					break;
1179 				case CS_ETM_DISCONTINUITY:
1180 					/*
1181 					 * Discontinuity in trace, flush
1182 					 * previous branch stack
1183 					 */
1184 					cs_etm__flush(etmq);
1185 					break;
1186 				case CS_ETM_EMPTY:
1187 					/*
1188 					 * Should not receive empty packet,
1189 					 * report error.
1190 					 */
1191 					pr_err("CS ETM Trace: empty packet\n");
1192 					return -EINVAL;
1193 				default:
1194 					break;
1195 				}
1196 			}
1197 		} while (buffer.len > buffer_used);
1198 
1199 		if (err == 0)
1200 			/* Flush any remaining branch stack entries */
1201 			err = cs_etm__end_block(etmq);
1202 	}
1203 
1204 	return err;
1205 }
1206 
1207 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
1208 					   pid_t tid, u64 time_)
1209 {
1210 	unsigned int i;
1211 	struct auxtrace_queues *queues = &etm->queues;
1212 
1213 	for (i = 0; i < queues->nr_queues; i++) {
1214 		struct auxtrace_queue *queue = &etm->queues.queue_array[i];
1215 		struct cs_etm_queue *etmq = queue->priv;
1216 
1217 		if (etmq && ((tid == -1) || (etmq->tid == tid))) {
1218 			etmq->time = time_;
1219 			cs_etm__set_pid_tid_cpu(etm, queue);
1220 			cs_etm__run_decoder(etmq);
1221 		}
1222 	}
1223 
1224 	return 0;
1225 }
1226 
1227 static int cs_etm__process_event(struct perf_session *session,
1228 				 union perf_event *event,
1229 				 struct perf_sample *sample,
1230 				 struct perf_tool *tool)
1231 {
1232 	int err = 0;
1233 	u64 timestamp;
1234 	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
1235 						   struct cs_etm_auxtrace,
1236 						   auxtrace);
1237 
1238 	if (dump_trace)
1239 		return 0;
1240 
1241 	if (!tool->ordered_events) {
1242 		pr_err("CoreSight ETM Trace requires ordered events\n");
1243 		return -EINVAL;
1244 	}
1245 
1246 	if (!etm->timeless_decoding)
1247 		return -EINVAL;
1248 
1249 	if (sample->time && (sample->time != (u64) -1))
1250 		timestamp = sample->time;
1251 	else
1252 		timestamp = 0;
1253 
1254 	if (timestamp || etm->timeless_decoding) {
1255 		err = cs_etm__update_queues(etm);
1256 		if (err)
1257 			return err;
1258 	}
1259 
1260 	if (event->header.type == PERF_RECORD_EXIT)
1261 		return cs_etm__process_timeless_queues(etm,
1262 						       event->fork.tid,
1263 						       sample->time);
1264 
1265 	return 0;
1266 }
1267 
1268 static int cs_etm__process_auxtrace_event(struct perf_session *session,
1269 					  union perf_event *event,
1270 					  struct perf_tool *tool __maybe_unused)
1271 {
1272 	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
1273 						   struct cs_etm_auxtrace,
1274 						   auxtrace);
1275 	if (!etm->data_queued) {
1276 		struct auxtrace_buffer *buffer;
1277 		off_t  data_offset;
1278 		int fd = perf_data__fd(session->data);
1279 		bool is_pipe = perf_data__is_pipe(session->data);
1280 		int err;
1281 
1282 		if (is_pipe)
1283 			data_offset = 0;
1284 		else {
1285 			data_offset = lseek(fd, 0, SEEK_CUR);
1286 			if (data_offset == -1)
1287 				return -errno;
1288 		}
1289 
1290 		err = auxtrace_queues__add_event(&etm->queues, session,
1291 						 event, data_offset, &buffer);
1292 		if (err)
1293 			return err;
1294 
1295 		if (dump_trace)
1296 			if (auxtrace_buffer__get_data(buffer, fd)) {
1297 				cs_etm__dump_event(etm, buffer);
1298 				auxtrace_buffer__put_data(buffer);
1299 			}
1300 	}
1301 
1302 	return 0;
1303 }
1304 
1305 static bool cs_etm__is_timeless_decoding(struct cs_etm_auxtrace *etm)
1306 {
1307 	struct perf_evsel *evsel;
1308 	struct perf_evlist *evlist = etm->session->evlist;
1309 	bool timeless_decoding = true;
1310 
1311 	/*
1312 	 * Circle through the list of event and complain if we find one
1313 	 * with the time bit set.
1314 	 */
1315 	evlist__for_each_entry(evlist, evsel) {
1316 		if ((evsel->attr.sample_type & PERF_SAMPLE_TIME))
1317 			timeless_decoding = false;
1318 	}
1319 
1320 	return timeless_decoding;
1321 }
1322 
1323 static const char * const cs_etm_global_header_fmts[] = {
1324 	[CS_HEADER_VERSION_0]	= "	Header version		       %llx\n",
1325 	[CS_PMU_TYPE_CPUS]	= "	PMU type/num cpus	       %llx\n",
1326 	[CS_ETM_SNAPSHOT]	= "	Snapshot		       %llx\n",
1327 };
1328 
1329 static const char * const cs_etm_priv_fmts[] = {
1330 	[CS_ETM_MAGIC]		= "	Magic number		       %llx\n",
1331 	[CS_ETM_CPU]		= "	CPU			       %lld\n",
1332 	[CS_ETM_ETMCR]		= "	ETMCR			       %llx\n",
1333 	[CS_ETM_ETMTRACEIDR]	= "	ETMTRACEIDR		       %llx\n",
1334 	[CS_ETM_ETMCCER]	= "	ETMCCER			       %llx\n",
1335 	[CS_ETM_ETMIDR]		= "	ETMIDR			       %llx\n",
1336 };
1337 
1338 static const char * const cs_etmv4_priv_fmts[] = {
1339 	[CS_ETM_MAGIC]		= "	Magic number		       %llx\n",
1340 	[CS_ETM_CPU]		= "	CPU			       %lld\n",
1341 	[CS_ETMV4_TRCCONFIGR]	= "	TRCCONFIGR		       %llx\n",
1342 	[CS_ETMV4_TRCTRACEIDR]	= "	TRCTRACEIDR		       %llx\n",
1343 	[CS_ETMV4_TRCIDR0]	= "	TRCIDR0			       %llx\n",
1344 	[CS_ETMV4_TRCIDR1]	= "	TRCIDR1			       %llx\n",
1345 	[CS_ETMV4_TRCIDR2]	= "	TRCIDR2			       %llx\n",
1346 	[CS_ETMV4_TRCIDR8]	= "	TRCIDR8			       %llx\n",
1347 	[CS_ETMV4_TRCAUTHSTATUS] = "	TRCAUTHSTATUS		       %llx\n",
1348 };
1349 
1350 static void cs_etm__print_auxtrace_info(u64 *val, int num)
1351 {
1352 	int i, j, cpu = 0;
1353 
1354 	for (i = 0; i < CS_HEADER_VERSION_0_MAX; i++)
1355 		fprintf(stdout, cs_etm_global_header_fmts[i], val[i]);
1356 
1357 	for (i = CS_HEADER_VERSION_0_MAX; cpu < num; cpu++) {
1358 		if (val[i] == __perf_cs_etmv3_magic)
1359 			for (j = 0; j < CS_ETM_PRIV_MAX; j++, i++)
1360 				fprintf(stdout, cs_etm_priv_fmts[j], val[i]);
1361 		else if (val[i] == __perf_cs_etmv4_magic)
1362 			for (j = 0; j < CS_ETMV4_PRIV_MAX; j++, i++)
1363 				fprintf(stdout, cs_etmv4_priv_fmts[j], val[i]);
1364 		else
1365 			/* failure.. return */
1366 			return;
1367 	}
1368 }
1369 
1370 int cs_etm__process_auxtrace_info(union perf_event *event,
1371 				  struct perf_session *session)
1372 {
1373 	struct auxtrace_info_event *auxtrace_info = &event->auxtrace_info;
1374 	struct cs_etm_auxtrace *etm = NULL;
1375 	struct int_node *inode;
1376 	unsigned int pmu_type;
1377 	int event_header_size = sizeof(struct perf_event_header);
1378 	int info_header_size;
1379 	int total_size = auxtrace_info->header.size;
1380 	int priv_size = 0;
1381 	int num_cpu;
1382 	int err = 0, idx = -1;
1383 	int i, j, k;
1384 	u64 *ptr, *hdr = NULL;
1385 	u64 **metadata = NULL;
1386 
1387 	/*
1388 	 * sizeof(auxtrace_info_event::type) +
1389 	 * sizeof(auxtrace_info_event::reserved) == 8
1390 	 */
1391 	info_header_size = 8;
1392 
1393 	if (total_size < (event_header_size + info_header_size))
1394 		return -EINVAL;
1395 
1396 	priv_size = total_size - event_header_size - info_header_size;
1397 
1398 	/* First the global part */
1399 	ptr = (u64 *) auxtrace_info->priv;
1400 
1401 	/* Look for version '0' of the header */
1402 	if (ptr[0] != 0)
1403 		return -EINVAL;
1404 
1405 	hdr = zalloc(sizeof(*hdr) * CS_HEADER_VERSION_0_MAX);
1406 	if (!hdr)
1407 		return -ENOMEM;
1408 
1409 	/* Extract header information - see cs-etm.h for format */
1410 	for (i = 0; i < CS_HEADER_VERSION_0_MAX; i++)
1411 		hdr[i] = ptr[i];
1412 	num_cpu = hdr[CS_PMU_TYPE_CPUS] & 0xffffffff;
1413 	pmu_type = (unsigned int) ((hdr[CS_PMU_TYPE_CPUS] >> 32) &
1414 				    0xffffffff);
1415 
1416 	/*
1417 	 * Create an RB tree for traceID-CPU# tuple. Since the conversion has
1418 	 * to be made for each packet that gets decoded, optimizing access in
1419 	 * anything other than a sequential array is worth doing.
1420 	 */
1421 	traceid_list = intlist__new(NULL);
1422 	if (!traceid_list) {
1423 		err = -ENOMEM;
1424 		goto err_free_hdr;
1425 	}
1426 
1427 	metadata = zalloc(sizeof(*metadata) * num_cpu);
1428 	if (!metadata) {
1429 		err = -ENOMEM;
1430 		goto err_free_traceid_list;
1431 	}
1432 
1433 	/*
1434 	 * The metadata is stored in the auxtrace_info section and encodes
1435 	 * the configuration of the ARM embedded trace macrocell which is
1436 	 * required by the trace decoder to properly decode the trace due
1437 	 * to its highly compressed nature.
1438 	 */
1439 	for (j = 0; j < num_cpu; j++) {
1440 		if (ptr[i] == __perf_cs_etmv3_magic) {
1441 			metadata[j] = zalloc(sizeof(*metadata[j]) *
1442 					     CS_ETM_PRIV_MAX);
1443 			if (!metadata[j]) {
1444 				err = -ENOMEM;
1445 				goto err_free_metadata;
1446 			}
1447 			for (k = 0; k < CS_ETM_PRIV_MAX; k++)
1448 				metadata[j][k] = ptr[i + k];
1449 
1450 			/* The traceID is our handle */
1451 			idx = metadata[j][CS_ETM_ETMTRACEIDR];
1452 			i += CS_ETM_PRIV_MAX;
1453 		} else if (ptr[i] == __perf_cs_etmv4_magic) {
1454 			metadata[j] = zalloc(sizeof(*metadata[j]) *
1455 					     CS_ETMV4_PRIV_MAX);
1456 			if (!metadata[j]) {
1457 				err = -ENOMEM;
1458 				goto err_free_metadata;
1459 			}
1460 			for (k = 0; k < CS_ETMV4_PRIV_MAX; k++)
1461 				metadata[j][k] = ptr[i + k];
1462 
1463 			/* The traceID is our handle */
1464 			idx = metadata[j][CS_ETMV4_TRCTRACEIDR];
1465 			i += CS_ETMV4_PRIV_MAX;
1466 		}
1467 
1468 		/* Get an RB node for this CPU */
1469 		inode = intlist__findnew(traceid_list, idx);
1470 
1471 		/* Something went wrong, no need to continue */
1472 		if (!inode) {
1473 			err = PTR_ERR(inode);
1474 			goto err_free_metadata;
1475 		}
1476 
1477 		/*
1478 		 * The node for that CPU should not be taken.
1479 		 * Back out if that's the case.
1480 		 */
1481 		if (inode->priv) {
1482 			err = -EINVAL;
1483 			goto err_free_metadata;
1484 		}
1485 		/* All good, associate the traceID with the CPU# */
1486 		inode->priv = &metadata[j][CS_ETM_CPU];
1487 	}
1488 
1489 	/*
1490 	 * Each of CS_HEADER_VERSION_0_MAX, CS_ETM_PRIV_MAX and
1491 	 * CS_ETMV4_PRIV_MAX mark how many double words are in the
1492 	 * global metadata, and each cpu's metadata respectively.
1493 	 * The following tests if the correct number of double words was
1494 	 * present in the auxtrace info section.
1495 	 */
1496 	if (i * 8 != priv_size) {
1497 		err = -EINVAL;
1498 		goto err_free_metadata;
1499 	}
1500 
1501 	etm = zalloc(sizeof(*etm));
1502 
1503 	if (!etm) {
1504 		err = -ENOMEM;
1505 		goto err_free_metadata;
1506 	}
1507 
1508 	err = auxtrace_queues__init(&etm->queues);
1509 	if (err)
1510 		goto err_free_etm;
1511 
1512 	etm->session = session;
1513 	etm->machine = &session->machines.host;
1514 
1515 	etm->num_cpu = num_cpu;
1516 	etm->pmu_type = pmu_type;
1517 	etm->snapshot_mode = (hdr[CS_ETM_SNAPSHOT] != 0);
1518 	etm->metadata = metadata;
1519 	etm->auxtrace_type = auxtrace_info->type;
1520 	etm->timeless_decoding = cs_etm__is_timeless_decoding(etm);
1521 
1522 	etm->auxtrace.process_event = cs_etm__process_event;
1523 	etm->auxtrace.process_auxtrace_event = cs_etm__process_auxtrace_event;
1524 	etm->auxtrace.flush_events = cs_etm__flush_events;
1525 	etm->auxtrace.free_events = cs_etm__free_events;
1526 	etm->auxtrace.free = cs_etm__free;
1527 	session->auxtrace = &etm->auxtrace;
1528 
1529 	etm->unknown_thread = thread__new(999999999, 999999999);
1530 	if (!etm->unknown_thread)
1531 		goto err_free_queues;
1532 
1533 	/*
1534 	 * Initialize list node so that at thread__zput() we can avoid
1535 	 * segmentation fault at list_del_init().
1536 	 */
1537 	INIT_LIST_HEAD(&etm->unknown_thread->node);
1538 
1539 	err = thread__set_comm(etm->unknown_thread, "unknown", 0);
1540 	if (err)
1541 		goto err_delete_thread;
1542 
1543 	if (thread__init_map_groups(etm->unknown_thread, etm->machine))
1544 		goto err_delete_thread;
1545 
1546 	if (dump_trace) {
1547 		cs_etm__print_auxtrace_info(auxtrace_info->priv, num_cpu);
1548 		return 0;
1549 	}
1550 
1551 	if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
1552 		etm->synth_opts = *session->itrace_synth_opts;
1553 	} else {
1554 		itrace_synth_opts__set_default(&etm->synth_opts,
1555 				session->itrace_synth_opts->default_no_sample);
1556 		etm->synth_opts.callchain = false;
1557 	}
1558 
1559 	err = cs_etm__synth_events(etm, session);
1560 	if (err)
1561 		goto err_delete_thread;
1562 
1563 	err = auxtrace_queues__process_index(&etm->queues, session);
1564 	if (err)
1565 		goto err_delete_thread;
1566 
1567 	etm->data_queued = etm->queues.populated;
1568 
1569 	return 0;
1570 
1571 err_delete_thread:
1572 	thread__zput(etm->unknown_thread);
1573 err_free_queues:
1574 	auxtrace_queues__free(&etm->queues);
1575 	session->auxtrace = NULL;
1576 err_free_etm:
1577 	zfree(&etm);
1578 err_free_metadata:
1579 	/* No need to check @metadata[j], free(NULL) is supported */
1580 	for (j = 0; j < num_cpu; j++)
1581 		free(metadata[j]);
1582 	zfree(&metadata);
1583 err_free_traceid_list:
1584 	intlist__delete(traceid_list);
1585 err_free_hdr:
1586 	zfree(&hdr);
1587 
1588 	return -EINVAL;
1589 }
1590