blob: f5a9cb4088080205f00f00da6de468760fe52a78 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// 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>
David Brazdil0f672f62019-12-10 10:32:29 +000014#include <linux/zalloc.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000015
David Brazdil0f672f62019-12-10 10:32:29 +000016#include <opencsd/ocsd_if_types.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000017#include <stdlib.h>
18
19#include "auxtrace.h"
20#include "color.h"
21#include "cs-etm.h"
22#include "cs-etm-decoder/cs-etm-decoder.h"
23#include "debug.h"
David Brazdil0f672f62019-12-10 10:32:29 +000024#include "dso.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000025#include "evlist.h"
26#include "intlist.h"
27#include "machine.h"
28#include "map.h"
29#include "perf.h"
David Brazdil0f672f62019-12-10 10:32:29 +000030#include "session.h"
31#include "map_symbol.h"
32#include "branch.h"
33#include "symbol.h"
34#include "tool.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000035#include "thread.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000036#include "thread-stack.h"
David Brazdil0f672f62019-12-10 10:32:29 +000037#include <tools/libc_compat.h>
38#include "util/synthetic-events.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000039
40#define MAX_TIMESTAMP (~0ULL)
41
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000042struct cs_etm_auxtrace {
43 struct auxtrace auxtrace;
44 struct auxtrace_queues queues;
45 struct auxtrace_heap heap;
46 struct itrace_synth_opts synth_opts;
47 struct perf_session *session;
48 struct machine *machine;
49 struct thread *unknown_thread;
50
51 u8 timeless_decoding;
52 u8 snapshot_mode;
53 u8 data_queued;
54 u8 sample_branches;
55 u8 sample_instructions;
56
57 int num_cpu;
58 u32 auxtrace_type;
59 u64 branches_sample_type;
60 u64 branches_id;
61 u64 instructions_sample_type;
62 u64 instructions_sample_period;
63 u64 instructions_id;
64 u64 **metadata;
65 u64 kernel_start;
66 unsigned int pmu_type;
67};
68
David Brazdil0f672f62019-12-10 10:32:29 +000069struct cs_etm_traceid_queue {
70 u8 trace_chan_id;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000071 pid_t pid, tid;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000072 u64 period_instructions;
David Brazdil0f672f62019-12-10 10:32:29 +000073 size_t last_branch_pos;
74 union perf_event *event_buf;
75 struct thread *thread;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000076 struct branch_stack *last_branch;
77 struct branch_stack *last_branch_rb;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000078 struct cs_etm_packet *prev_packet;
79 struct cs_etm_packet *packet;
David Brazdil0f672f62019-12-10 10:32:29 +000080 struct cs_etm_packet_queue packet_queue;
81};
82
83struct cs_etm_queue {
84 struct cs_etm_auxtrace *etm;
85 struct cs_etm_decoder *decoder;
86 struct auxtrace_buffer *buffer;
87 unsigned int queue_nr;
88 u8 pending_timestamp;
89 u64 offset;
90 const unsigned char *buf;
91 size_t buf_len, buf_used;
92 /* Conversion between traceID and index in traceid_queues array */
93 struct intlist *traceid_queues_list;
94 struct cs_etm_traceid_queue **traceid_queues;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000095};
96
Olivier Deprez0e641232021-09-23 10:07:05 +020097/* RB tree for quick conversion between traceID and metadata pointers */
98static struct intlist *traceid_list;
99
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000100static int cs_etm__update_queues(struct cs_etm_auxtrace *etm);
David Brazdil0f672f62019-12-10 10:32:29 +0000101static int cs_etm__process_queues(struct cs_etm_auxtrace *etm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000102static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
David Brazdil0f672f62019-12-10 10:32:29 +0000103 pid_t tid);
104static int cs_etm__get_data_block(struct cs_etm_queue *etmq);
105static int cs_etm__decode_data_block(struct cs_etm_queue *etmq);
106
107/* PTMs ETMIDR [11:8] set to b0011 */
108#define ETMIDR_PTM_VERSION 0x00000300
109
110/*
111 * A struct auxtrace_heap_item only has a queue_nr and a timestamp to
112 * work with. One option is to modify to auxtrace_heap_XYZ() API or simply
113 * encode the etm queue number as the upper 16 bit and the channel as
114 * the lower 16 bit.
115 */
Olivier Deprez0e641232021-09-23 10:07:05 +0200116#define TO_CS_QUEUE_NR(queue_nr, trace_chan_id) \
David Brazdil0f672f62019-12-10 10:32:29 +0000117 (queue_nr << 16 | trace_chan_id)
118#define TO_QUEUE_NR(cs_queue_nr) (cs_queue_nr >> 16)
119#define TO_TRACE_CHAN_ID(cs_queue_nr) (cs_queue_nr & 0x0000ffff)
120
121static u32 cs_etm__get_v7_protocol_version(u32 etmidr)
122{
123 etmidr &= ETMIDR_PTM_VERSION;
124
125 if (etmidr == ETMIDR_PTM_VERSION)
126 return CS_ETM_PROTO_PTM;
127
128 return CS_ETM_PROTO_ETMV3;
129}
130
131static int cs_etm__get_magic(u8 trace_chan_id, u64 *magic)
132{
133 struct int_node *inode;
134 u64 *metadata;
135
136 inode = intlist__find(traceid_list, trace_chan_id);
137 if (!inode)
138 return -EINVAL;
139
140 metadata = inode->priv;
141 *magic = metadata[CS_ETM_MAGIC];
142 return 0;
143}
144
145int cs_etm__get_cpu(u8 trace_chan_id, int *cpu)
146{
147 struct int_node *inode;
148 u64 *metadata;
149
150 inode = intlist__find(traceid_list, trace_chan_id);
151 if (!inode)
152 return -EINVAL;
153
154 metadata = inode->priv;
155 *cpu = (int)metadata[CS_ETM_CPU];
156 return 0;
157}
158
159void cs_etm__etmq_set_traceid_queue_timestamp(struct cs_etm_queue *etmq,
160 u8 trace_chan_id)
161{
162 /*
163 * Wnen a timestamp packet is encountered the backend code
164 * is stopped so that the front end has time to process packets
165 * that were accumulated in the traceID queue. Since there can
166 * be more than one channel per cs_etm_queue, we need to specify
167 * what traceID queue needs servicing.
168 */
169 etmq->pending_timestamp = trace_chan_id;
170}
171
172static u64 cs_etm__etmq_get_timestamp(struct cs_etm_queue *etmq,
173 u8 *trace_chan_id)
174{
175 struct cs_etm_packet_queue *packet_queue;
176
177 if (!etmq->pending_timestamp)
178 return 0;
179
180 if (trace_chan_id)
181 *trace_chan_id = etmq->pending_timestamp;
182
183 packet_queue = cs_etm__etmq_get_packet_queue(etmq,
184 etmq->pending_timestamp);
185 if (!packet_queue)
186 return 0;
187
188 /* Acknowledge pending status */
189 etmq->pending_timestamp = 0;
190
191 /* See function cs_etm_decoder__do_{hard|soft}_timestamp() */
192 return packet_queue->timestamp;
193}
194
195static void cs_etm__clear_packet_queue(struct cs_etm_packet_queue *queue)
196{
197 int i;
198
199 queue->head = 0;
200 queue->tail = 0;
201 queue->packet_count = 0;
202 for (i = 0; i < CS_ETM_PACKET_MAX_BUFFER; i++) {
203 queue->packet_buffer[i].isa = CS_ETM_ISA_UNKNOWN;
204 queue->packet_buffer[i].start_addr = CS_ETM_INVAL_ADDR;
205 queue->packet_buffer[i].end_addr = CS_ETM_INVAL_ADDR;
206 queue->packet_buffer[i].instr_count = 0;
207 queue->packet_buffer[i].last_instr_taken_branch = false;
208 queue->packet_buffer[i].last_instr_size = 0;
209 queue->packet_buffer[i].last_instr_type = 0;
210 queue->packet_buffer[i].last_instr_subtype = 0;
211 queue->packet_buffer[i].last_instr_cond = 0;
212 queue->packet_buffer[i].flags = 0;
213 queue->packet_buffer[i].exception_number = UINT32_MAX;
214 queue->packet_buffer[i].trace_chan_id = UINT8_MAX;
215 queue->packet_buffer[i].cpu = INT_MIN;
216 }
217}
218
219static void cs_etm__clear_all_packet_queues(struct cs_etm_queue *etmq)
220{
221 int idx;
222 struct int_node *inode;
223 struct cs_etm_traceid_queue *tidq;
224 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
225
226 intlist__for_each_entry(inode, traceid_queues_list) {
227 idx = (int)(intptr_t)inode->priv;
228 tidq = etmq->traceid_queues[idx];
229 cs_etm__clear_packet_queue(&tidq->packet_queue);
230 }
231}
232
233static int cs_etm__init_traceid_queue(struct cs_etm_queue *etmq,
234 struct cs_etm_traceid_queue *tidq,
235 u8 trace_chan_id)
236{
237 int rc = -ENOMEM;
238 struct auxtrace_queue *queue;
239 struct cs_etm_auxtrace *etm = etmq->etm;
240
241 cs_etm__clear_packet_queue(&tidq->packet_queue);
242
243 queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
244 tidq->tid = queue->tid;
245 tidq->pid = -1;
246 tidq->trace_chan_id = trace_chan_id;
247
248 tidq->packet = zalloc(sizeof(struct cs_etm_packet));
249 if (!tidq->packet)
250 goto out;
251
252 tidq->prev_packet = zalloc(sizeof(struct cs_etm_packet));
253 if (!tidq->prev_packet)
254 goto out_free;
255
256 if (etm->synth_opts.last_branch) {
257 size_t sz = sizeof(struct branch_stack);
258
259 sz += etm->synth_opts.last_branch_sz *
260 sizeof(struct branch_entry);
261 tidq->last_branch = zalloc(sz);
262 if (!tidq->last_branch)
263 goto out_free;
264 tidq->last_branch_rb = zalloc(sz);
265 if (!tidq->last_branch_rb)
266 goto out_free;
267 }
268
269 tidq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
270 if (!tidq->event_buf)
271 goto out_free;
272
273 return 0;
274
275out_free:
276 zfree(&tidq->last_branch_rb);
277 zfree(&tidq->last_branch);
278 zfree(&tidq->prev_packet);
279 zfree(&tidq->packet);
280out:
281 return rc;
282}
283
284static struct cs_etm_traceid_queue
285*cs_etm__etmq_get_traceid_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
286{
287 int idx;
288 struct int_node *inode;
289 struct intlist *traceid_queues_list;
290 struct cs_etm_traceid_queue *tidq, **traceid_queues;
291 struct cs_etm_auxtrace *etm = etmq->etm;
292
293 if (etm->timeless_decoding)
294 trace_chan_id = CS_ETM_PER_THREAD_TRACEID;
295
296 traceid_queues_list = etmq->traceid_queues_list;
297
298 /*
299 * Check if the traceid_queue exist for this traceID by looking
300 * in the queue list.
301 */
302 inode = intlist__find(traceid_queues_list, trace_chan_id);
303 if (inode) {
304 idx = (int)(intptr_t)inode->priv;
305 return etmq->traceid_queues[idx];
306 }
307
308 /* We couldn't find a traceid_queue for this traceID, allocate one */
309 tidq = malloc(sizeof(*tidq));
310 if (!tidq)
311 return NULL;
312
313 memset(tidq, 0, sizeof(*tidq));
314
315 /* Get a valid index for the new traceid_queue */
316 idx = intlist__nr_entries(traceid_queues_list);
317 /* Memory for the inode is free'ed in cs_etm_free_traceid_queues () */
318 inode = intlist__findnew(traceid_queues_list, trace_chan_id);
319 if (!inode)
320 goto out_free;
321
322 /* Associate this traceID with this index */
323 inode->priv = (void *)(intptr_t)idx;
324
325 if (cs_etm__init_traceid_queue(etmq, tidq, trace_chan_id))
326 goto out_free;
327
328 /* Grow the traceid_queues array by one unit */
329 traceid_queues = etmq->traceid_queues;
330 traceid_queues = reallocarray(traceid_queues,
331 idx + 1,
332 sizeof(*traceid_queues));
333
334 /*
335 * On failure reallocarray() returns NULL and the original block of
336 * memory is left untouched.
337 */
338 if (!traceid_queues)
339 goto out_free;
340
341 traceid_queues[idx] = tidq;
342 etmq->traceid_queues = traceid_queues;
343
344 return etmq->traceid_queues[idx];
345
346out_free:
347 /*
348 * Function intlist__remove() removes the inode from the list
349 * and delete the memory associated to it.
350 */
351 intlist__remove(traceid_queues_list, inode);
352 free(tidq);
353
354 return NULL;
355}
356
357struct cs_etm_packet_queue
358*cs_etm__etmq_get_packet_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
359{
360 struct cs_etm_traceid_queue *tidq;
361
362 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
363 if (tidq)
364 return &tidq->packet_queue;
365
366 return NULL;
367}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000368
Olivier Deprez0e641232021-09-23 10:07:05 +0200369static void cs_etm__packet_swap(struct cs_etm_auxtrace *etm,
370 struct cs_etm_traceid_queue *tidq)
371{
372 struct cs_etm_packet *tmp;
373
374 if (etm->sample_branches || etm->synth_opts.last_branch ||
375 etm->sample_instructions) {
376 /*
377 * Swap PACKET with PREV_PACKET: PACKET becomes PREV_PACKET for
378 * the next incoming packet.
379 */
380 tmp = tidq->packet;
381 tidq->packet = tidq->prev_packet;
382 tidq->prev_packet = tmp;
383 }
384}
385
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000386static void cs_etm__packet_dump(const char *pkt_string)
387{
388 const char *color = PERF_COLOR_BLUE;
389 int len = strlen(pkt_string);
390
391 if (len && (pkt_string[len-1] == '\n'))
392 color_fprintf(stdout, color, " %s", pkt_string);
393 else
394 color_fprintf(stdout, color, " %s\n", pkt_string);
395
396 fflush(stdout);
397}
398
David Brazdil0f672f62019-12-10 10:32:29 +0000399static void cs_etm__set_trace_param_etmv3(struct cs_etm_trace_params *t_params,
400 struct cs_etm_auxtrace *etm, int idx,
401 u32 etmidr)
402{
403 u64 **metadata = etm->metadata;
404
405 t_params[idx].protocol = cs_etm__get_v7_protocol_version(etmidr);
406 t_params[idx].etmv3.reg_ctrl = metadata[idx][CS_ETM_ETMCR];
407 t_params[idx].etmv3.reg_trc_id = metadata[idx][CS_ETM_ETMTRACEIDR];
408}
409
410static void cs_etm__set_trace_param_etmv4(struct cs_etm_trace_params *t_params,
411 struct cs_etm_auxtrace *etm, int idx)
412{
413 u64 **metadata = etm->metadata;
414
415 t_params[idx].protocol = CS_ETM_PROTO_ETMV4i;
416 t_params[idx].etmv4.reg_idr0 = metadata[idx][CS_ETMV4_TRCIDR0];
417 t_params[idx].etmv4.reg_idr1 = metadata[idx][CS_ETMV4_TRCIDR1];
418 t_params[idx].etmv4.reg_idr2 = metadata[idx][CS_ETMV4_TRCIDR2];
419 t_params[idx].etmv4.reg_idr8 = metadata[idx][CS_ETMV4_TRCIDR8];
420 t_params[idx].etmv4.reg_configr = metadata[idx][CS_ETMV4_TRCCONFIGR];
421 t_params[idx].etmv4.reg_traceidr = metadata[idx][CS_ETMV4_TRCTRACEIDR];
422}
423
424static int cs_etm__init_trace_params(struct cs_etm_trace_params *t_params,
425 struct cs_etm_auxtrace *etm)
426{
427 int i;
428 u32 etmidr;
429 u64 architecture;
430
431 for (i = 0; i < etm->num_cpu; i++) {
432 architecture = etm->metadata[i][CS_ETM_MAGIC];
433
434 switch (architecture) {
435 case __perf_cs_etmv3_magic:
436 etmidr = etm->metadata[i][CS_ETM_ETMIDR];
437 cs_etm__set_trace_param_etmv3(t_params, etm, i, etmidr);
438 break;
439 case __perf_cs_etmv4_magic:
440 cs_etm__set_trace_param_etmv4(t_params, etm, i);
441 break;
442 default:
443 return -EINVAL;
444 }
445 }
446
447 return 0;
448}
449
450static int cs_etm__init_decoder_params(struct cs_etm_decoder_params *d_params,
451 struct cs_etm_queue *etmq,
452 enum cs_etm_decoder_operation mode)
453{
454 int ret = -EINVAL;
455
456 if (!(mode < CS_ETM_OPERATION_MAX))
457 goto out;
458
459 d_params->packet_printer = cs_etm__packet_dump;
460 d_params->operation = mode;
461 d_params->data = etmq;
462 d_params->formatted = true;
463 d_params->fsyncs = false;
464 d_params->hsyncs = false;
465 d_params->frame_aligned = true;
466
467 ret = 0;
468out:
469 return ret;
470}
471
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000472static void cs_etm__dump_event(struct cs_etm_auxtrace *etm,
473 struct auxtrace_buffer *buffer)
474{
David Brazdil0f672f62019-12-10 10:32:29 +0000475 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000476 const char *color = PERF_COLOR_BLUE;
477 struct cs_etm_decoder_params d_params;
478 struct cs_etm_trace_params *t_params;
479 struct cs_etm_decoder *decoder;
480 size_t buffer_used = 0;
481
482 fprintf(stdout, "\n");
483 color_fprintf(stdout, color,
484 ". ... CoreSight ETM Trace data: size %zu bytes\n",
485 buffer->size);
486
487 /* Use metadata to fill in trace parameters for trace decoder */
488 t_params = zalloc(sizeof(*t_params) * etm->num_cpu);
David Brazdil0f672f62019-12-10 10:32:29 +0000489
490 if (!t_params)
491 return;
492
493 if (cs_etm__init_trace_params(t_params, etm))
494 goto out_free;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000495
496 /* Set decoder parameters to simply print the trace packets */
David Brazdil0f672f62019-12-10 10:32:29 +0000497 if (cs_etm__init_decoder_params(&d_params, NULL,
498 CS_ETM_OPERATION_PRINT))
499 goto out_free;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000500
501 decoder = cs_etm_decoder__new(etm->num_cpu, &d_params, t_params);
502
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000503 if (!decoder)
David Brazdil0f672f62019-12-10 10:32:29 +0000504 goto out_free;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000505 do {
506 size_t consumed;
507
508 ret = cs_etm_decoder__process_data_block(
509 decoder, buffer->offset,
510 &((u8 *)buffer->data)[buffer_used],
511 buffer->size - buffer_used, &consumed);
512 if (ret)
513 break;
514
515 buffer_used += consumed;
516 } while (buffer_used < buffer->size);
517
518 cs_etm_decoder__free(decoder);
David Brazdil0f672f62019-12-10 10:32:29 +0000519
520out_free:
521 zfree(&t_params);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000522}
523
524static int cs_etm__flush_events(struct perf_session *session,
525 struct perf_tool *tool)
526{
527 int ret;
528 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
529 struct cs_etm_auxtrace,
530 auxtrace);
531 if (dump_trace)
532 return 0;
533
534 if (!tool->ordered_events)
535 return -EINVAL;
536
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000537 ret = cs_etm__update_queues(etm);
538
539 if (ret < 0)
540 return ret;
541
David Brazdil0f672f62019-12-10 10:32:29 +0000542 if (etm->timeless_decoding)
543 return cs_etm__process_timeless_queues(etm, -1);
544
545 return cs_etm__process_queues(etm);
546}
547
548static void cs_etm__free_traceid_queues(struct cs_etm_queue *etmq)
549{
550 int idx;
551 uintptr_t priv;
552 struct int_node *inode, *tmp;
553 struct cs_etm_traceid_queue *tidq;
554 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
555
556 intlist__for_each_entry_safe(inode, tmp, traceid_queues_list) {
557 priv = (uintptr_t)inode->priv;
558 idx = priv;
559
560 /* Free this traceid_queue from the array */
561 tidq = etmq->traceid_queues[idx];
562 thread__zput(tidq->thread);
563 zfree(&tidq->event_buf);
564 zfree(&tidq->last_branch);
565 zfree(&tidq->last_branch_rb);
566 zfree(&tidq->prev_packet);
567 zfree(&tidq->packet);
568 zfree(&tidq);
569
570 /*
571 * Function intlist__remove() removes the inode from the list
572 * and delete the memory associated to it.
573 */
574 intlist__remove(traceid_queues_list, inode);
575 }
576
577 /* Then the RB tree itself */
578 intlist__delete(traceid_queues_list);
579 etmq->traceid_queues_list = NULL;
580
581 /* finally free the traceid_queues array */
582 zfree(&etmq->traceid_queues);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000583}
584
585static void cs_etm__free_queue(void *priv)
586{
587 struct cs_etm_queue *etmq = priv;
588
589 if (!etmq)
590 return;
591
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000592 cs_etm_decoder__free(etmq->decoder);
David Brazdil0f672f62019-12-10 10:32:29 +0000593 cs_etm__free_traceid_queues(etmq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000594 free(etmq);
595}
596
597static void cs_etm__free_events(struct perf_session *session)
598{
599 unsigned int i;
600 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
601 struct cs_etm_auxtrace,
602 auxtrace);
603 struct auxtrace_queues *queues = &aux->queues;
604
605 for (i = 0; i < queues->nr_queues; i++) {
606 cs_etm__free_queue(queues->queue_array[i].priv);
607 queues->queue_array[i].priv = NULL;
608 }
609
610 auxtrace_queues__free(queues);
611}
612
613static void cs_etm__free(struct perf_session *session)
614{
615 int i;
616 struct int_node *inode, *tmp;
617 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
618 struct cs_etm_auxtrace,
619 auxtrace);
620 cs_etm__free_events(session);
621 session->auxtrace = NULL;
622
David Brazdil0f672f62019-12-10 10:32:29 +0000623 /* First remove all traceID/metadata nodes for the RB tree */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000624 intlist__for_each_entry_safe(inode, tmp, traceid_list)
625 intlist__remove(traceid_list, inode);
626 /* Then the RB tree itself */
627 intlist__delete(traceid_list);
628
629 for (i = 0; i < aux->num_cpu; i++)
630 zfree(&aux->metadata[i]);
631
632 thread__zput(aux->unknown_thread);
633 zfree(&aux->metadata);
634 zfree(&aux);
635}
636
637static u8 cs_etm__cpu_mode(struct cs_etm_queue *etmq, u64 address)
638{
639 struct machine *machine;
640
641 machine = etmq->etm->machine;
642
643 if (address >= etmq->etm->kernel_start) {
644 if (machine__is_host(machine))
645 return PERF_RECORD_MISC_KERNEL;
646 else
647 return PERF_RECORD_MISC_GUEST_KERNEL;
648 } else {
649 if (machine__is_host(machine))
650 return PERF_RECORD_MISC_USER;
651 else if (perf_guest)
652 return PERF_RECORD_MISC_GUEST_USER;
653 else
654 return PERF_RECORD_MISC_HYPERVISOR;
655 }
656}
657
David Brazdil0f672f62019-12-10 10:32:29 +0000658static u32 cs_etm__mem_access(struct cs_etm_queue *etmq, u8 trace_chan_id,
659 u64 address, size_t size, u8 *buffer)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000660{
661 u8 cpumode;
662 u64 offset;
663 int len;
David Brazdil0f672f62019-12-10 10:32:29 +0000664 struct thread *thread;
665 struct machine *machine;
666 struct addr_location al;
667 struct cs_etm_traceid_queue *tidq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000668
669 if (!etmq)
David Brazdil0f672f62019-12-10 10:32:29 +0000670 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000671
672 machine = etmq->etm->machine;
673 cpumode = cs_etm__cpu_mode(etmq, address);
David Brazdil0f672f62019-12-10 10:32:29 +0000674 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
675 if (!tidq)
676 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000677
David Brazdil0f672f62019-12-10 10:32:29 +0000678 thread = tidq->thread;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000679 if (!thread) {
680 if (cpumode != PERF_RECORD_MISC_KERNEL)
David Brazdil0f672f62019-12-10 10:32:29 +0000681 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000682 thread = etmq->etm->unknown_thread;
683 }
684
685 if (!thread__find_map(thread, cpumode, address, &al) || !al.map->dso)
686 return 0;
687
688 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
689 dso__data_status_seen(al.map->dso, DSO_DATA_STATUS_SEEN_ITRACE))
690 return 0;
691
692 offset = al.map->map_ip(al.map, address);
693
694 map__load(al.map);
695
696 len = dso__data_read_offset(al.map->dso, machine, offset, buffer, size);
697
698 if (len <= 0)
699 return 0;
700
701 return len;
702}
703
David Brazdil0f672f62019-12-10 10:32:29 +0000704static struct cs_etm_queue *cs_etm__alloc_queue(struct cs_etm_auxtrace *etm)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000705{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000706 struct cs_etm_decoder_params d_params;
David Brazdil0f672f62019-12-10 10:32:29 +0000707 struct cs_etm_trace_params *t_params = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000708 struct cs_etm_queue *etmq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000709
710 etmq = zalloc(sizeof(*etmq));
711 if (!etmq)
712 return NULL;
713
David Brazdil0f672f62019-12-10 10:32:29 +0000714 etmq->traceid_queues_list = intlist__new(NULL);
715 if (!etmq->traceid_queues_list)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000716 goto out_free;
717
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000718 /* Use metadata to fill in trace parameters for trace decoder */
719 t_params = zalloc(sizeof(*t_params) * etm->num_cpu);
720
721 if (!t_params)
722 goto out_free;
723
David Brazdil0f672f62019-12-10 10:32:29 +0000724 if (cs_etm__init_trace_params(t_params, etm))
725 goto out_free;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000726
David Brazdil0f672f62019-12-10 10:32:29 +0000727 /* Set decoder parameters to decode trace packets */
728 if (cs_etm__init_decoder_params(&d_params, etmq,
729 CS_ETM_OPERATION_DECODE))
730 goto out_free;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000731
732 etmq->decoder = cs_etm_decoder__new(etm->num_cpu, &d_params, t_params);
733
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000734 if (!etmq->decoder)
735 goto out_free;
736
737 /*
738 * Register a function to handle all memory accesses required by
739 * the trace decoder library.
740 */
741 if (cs_etm_decoder__add_mem_access_cb(etmq->decoder,
742 0x0L, ((u64) -1L),
743 cs_etm__mem_access))
744 goto out_free_decoder;
745
David Brazdil0f672f62019-12-10 10:32:29 +0000746 zfree(&t_params);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000747 return etmq;
748
749out_free_decoder:
750 cs_etm_decoder__free(etmq->decoder);
751out_free:
David Brazdil0f672f62019-12-10 10:32:29 +0000752 intlist__delete(etmq->traceid_queues_list);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000753 free(etmq);
754
755 return NULL;
756}
757
758static int cs_etm__setup_queue(struct cs_etm_auxtrace *etm,
759 struct auxtrace_queue *queue,
760 unsigned int queue_nr)
761{
David Brazdil0f672f62019-12-10 10:32:29 +0000762 int ret = 0;
763 unsigned int cs_queue_nr;
764 u8 trace_chan_id;
765 u64 timestamp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000766 struct cs_etm_queue *etmq = queue->priv;
767
768 if (list_empty(&queue->head) || etmq)
David Brazdil0f672f62019-12-10 10:32:29 +0000769 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000770
David Brazdil0f672f62019-12-10 10:32:29 +0000771 etmq = cs_etm__alloc_queue(etm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000772
David Brazdil0f672f62019-12-10 10:32:29 +0000773 if (!etmq) {
774 ret = -ENOMEM;
775 goto out;
776 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000777
778 queue->priv = etmq;
David Brazdil0f672f62019-12-10 10:32:29 +0000779 etmq->etm = etm;
780 etmq->queue_nr = queue_nr;
781 etmq->offset = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000782
David Brazdil0f672f62019-12-10 10:32:29 +0000783 if (etm->timeless_decoding)
784 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000785
David Brazdil0f672f62019-12-10 10:32:29 +0000786 /*
787 * We are under a CPU-wide trace scenario. As such we need to know
788 * when the code that generated the traces started to execute so that
789 * it can be correlated with execution on other CPUs. So we get a
790 * handle on the beginning of traces and decode until we find a
791 * timestamp. The timestamp is then added to the auxtrace min heap
792 * in order to know what nibble (of all the etmqs) to decode first.
793 */
794 while (1) {
795 /*
796 * Fetch an aux_buffer from this etmq. Bail if no more
797 * blocks or an error has been encountered.
798 */
799 ret = cs_etm__get_data_block(etmq);
800 if (ret <= 0)
801 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000802
David Brazdil0f672f62019-12-10 10:32:29 +0000803 /*
804 * Run decoder on the trace block. The decoder will stop when
805 * encountering a timestamp, a full packet queue or the end of
806 * trace for that block.
807 */
808 ret = cs_etm__decode_data_block(etmq);
809 if (ret)
810 goto out;
811
812 /*
813 * Function cs_etm_decoder__do_{hard|soft}_timestamp() does all
814 * the timestamp calculation for us.
815 */
816 timestamp = cs_etm__etmq_get_timestamp(etmq, &trace_chan_id);
817
818 /* We found a timestamp, no need to continue. */
819 if (timestamp)
820 break;
821
822 /*
823 * We didn't find a timestamp so empty all the traceid packet
824 * queues before looking for another timestamp packet, either
825 * in the current data block or a new one. Packets that were
826 * just decoded are useless since no timestamp has been
827 * associated with them. As such simply discard them.
828 */
829 cs_etm__clear_all_packet_queues(etmq);
830 }
831
832 /*
833 * We have a timestamp. Add it to the min heap to reflect when
834 * instructions conveyed by the range packets of this traceID queue
835 * started to execute. Once the same has been done for all the traceID
836 * queues of each etmq, redenring and decoding can start in
837 * chronological order.
838 *
839 * Note that packets decoded above are still in the traceID's packet
840 * queue and will be processed in cs_etm__process_queues().
841 */
Olivier Deprez0e641232021-09-23 10:07:05 +0200842 cs_queue_nr = TO_CS_QUEUE_NR(queue_nr, trace_chan_id);
David Brazdil0f672f62019-12-10 10:32:29 +0000843 ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, timestamp);
844out:
845 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000846}
847
848static int cs_etm__setup_queues(struct cs_etm_auxtrace *etm)
849{
850 unsigned int i;
851 int ret;
852
David Brazdil0f672f62019-12-10 10:32:29 +0000853 if (!etm->kernel_start)
854 etm->kernel_start = machine__kernel_start(etm->machine);
855
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000856 for (i = 0; i < etm->queues.nr_queues; i++) {
857 ret = cs_etm__setup_queue(etm, &etm->queues.queue_array[i], i);
858 if (ret)
859 return ret;
860 }
861
862 return 0;
863}
864
865static int cs_etm__update_queues(struct cs_etm_auxtrace *etm)
866{
867 if (etm->queues.new_data) {
868 etm->queues.new_data = false;
869 return cs_etm__setup_queues(etm);
870 }
871
872 return 0;
873}
874
David Brazdil0f672f62019-12-10 10:32:29 +0000875static inline
876void cs_etm__copy_last_branch_rb(struct cs_etm_queue *etmq,
877 struct cs_etm_traceid_queue *tidq)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000878{
David Brazdil0f672f62019-12-10 10:32:29 +0000879 struct branch_stack *bs_src = tidq->last_branch_rb;
880 struct branch_stack *bs_dst = tidq->last_branch;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000881 size_t nr = 0;
882
883 /*
884 * Set the number of records before early exit: ->nr is used to
885 * determine how many branches to copy from ->entries.
886 */
887 bs_dst->nr = bs_src->nr;
888
889 /*
890 * Early exit when there is nothing to copy.
891 */
892 if (!bs_src->nr)
893 return;
894
895 /*
896 * As bs_src->entries is a circular buffer, we need to copy from it in
897 * two steps. First, copy the branches from the most recently inserted
898 * branch ->last_branch_pos until the end of bs_src->entries buffer.
899 */
David Brazdil0f672f62019-12-10 10:32:29 +0000900 nr = etmq->etm->synth_opts.last_branch_sz - tidq->last_branch_pos;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000901 memcpy(&bs_dst->entries[0],
David Brazdil0f672f62019-12-10 10:32:29 +0000902 &bs_src->entries[tidq->last_branch_pos],
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000903 sizeof(struct branch_entry) * nr);
904
905 /*
906 * If we wrapped around at least once, the branches from the beginning
907 * of the bs_src->entries buffer and until the ->last_branch_pos element
908 * are older valid branches: copy them over. The total number of
909 * branches copied over will be equal to the number of branches asked by
910 * the user in last_branch_sz.
911 */
912 if (bs_src->nr >= etmq->etm->synth_opts.last_branch_sz) {
913 memcpy(&bs_dst->entries[nr],
914 &bs_src->entries[0],
David Brazdil0f672f62019-12-10 10:32:29 +0000915 sizeof(struct branch_entry) * tidq->last_branch_pos);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000916 }
917}
918
David Brazdil0f672f62019-12-10 10:32:29 +0000919static inline
920void cs_etm__reset_last_branch_rb(struct cs_etm_traceid_queue *tidq)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000921{
David Brazdil0f672f62019-12-10 10:32:29 +0000922 tidq->last_branch_pos = 0;
923 tidq->last_branch_rb->nr = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000924}
925
David Brazdil0f672f62019-12-10 10:32:29 +0000926static inline int cs_etm__t32_instr_size(struct cs_etm_queue *etmq,
927 u8 trace_chan_id, u64 addr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000928{
David Brazdil0f672f62019-12-10 10:32:29 +0000929 u8 instrBytes[2];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000930
David Brazdil0f672f62019-12-10 10:32:29 +0000931 cs_etm__mem_access(etmq, trace_chan_id, addr,
932 ARRAY_SIZE(instrBytes), instrBytes);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000933 /*
David Brazdil0f672f62019-12-10 10:32:29 +0000934 * T32 instruction size is indicated by bits[15:11] of the first
935 * 16-bit word of the instruction: 0b11101, 0b11110 and 0b11111
936 * denote a 32-bit instruction.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000937 */
David Brazdil0f672f62019-12-10 10:32:29 +0000938 return ((instrBytes[1] & 0xF8) >= 0xE8) ? 4 : 2;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000939}
940
941static inline u64 cs_etm__first_executed_instr(struct cs_etm_packet *packet)
942{
David Brazdil0f672f62019-12-10 10:32:29 +0000943 /* Returns 0 for the CS_ETM_DISCONTINUITY packet */
944 if (packet->sample_type == CS_ETM_DISCONTINUITY)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000945 return 0;
946
947 return packet->start_addr;
948}
949
David Brazdil0f672f62019-12-10 10:32:29 +0000950static inline
951u64 cs_etm__last_executed_instr(const struct cs_etm_packet *packet)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000952{
David Brazdil0f672f62019-12-10 10:32:29 +0000953 /* Returns 0 for the CS_ETM_DISCONTINUITY packet */
954 if (packet->sample_type == CS_ETM_DISCONTINUITY)
955 return 0;
956
957 return packet->end_addr - packet->last_instr_size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000958}
959
David Brazdil0f672f62019-12-10 10:32:29 +0000960static inline u64 cs_etm__instr_addr(struct cs_etm_queue *etmq,
961 u64 trace_chan_id,
962 const struct cs_etm_packet *packet,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000963 u64 offset)
964{
David Brazdil0f672f62019-12-10 10:32:29 +0000965 if (packet->isa == CS_ETM_ISA_T32) {
966 u64 addr = packet->start_addr;
967
968 while (offset > 0) {
969 addr += cs_etm__t32_instr_size(etmq,
970 trace_chan_id, addr);
971 offset--;
972 }
973 return addr;
974 }
975
976 /* Assume a 4 byte instruction size (A32/A64) */
977 return packet->start_addr + offset * 4;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000978}
979
David Brazdil0f672f62019-12-10 10:32:29 +0000980static void cs_etm__update_last_branch_rb(struct cs_etm_queue *etmq,
981 struct cs_etm_traceid_queue *tidq)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000982{
David Brazdil0f672f62019-12-10 10:32:29 +0000983 struct branch_stack *bs = tidq->last_branch_rb;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000984 struct branch_entry *be;
985
986 /*
987 * The branches are recorded in a circular buffer in reverse
988 * chronological order: we start recording from the last element of the
989 * buffer down. After writing the first element of the stack, move the
990 * insert position back to the end of the buffer.
991 */
David Brazdil0f672f62019-12-10 10:32:29 +0000992 if (!tidq->last_branch_pos)
993 tidq->last_branch_pos = etmq->etm->synth_opts.last_branch_sz;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000994
David Brazdil0f672f62019-12-10 10:32:29 +0000995 tidq->last_branch_pos -= 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000996
David Brazdil0f672f62019-12-10 10:32:29 +0000997 be = &bs->entries[tidq->last_branch_pos];
998 be->from = cs_etm__last_executed_instr(tidq->prev_packet);
999 be->to = cs_etm__first_executed_instr(tidq->packet);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001000 /* No support for mispredict */
1001 be->flags.mispred = 0;
1002 be->flags.predicted = 1;
1003
1004 /*
1005 * Increment bs->nr until reaching the number of last branches asked by
1006 * the user on the command line.
1007 */
1008 if (bs->nr < etmq->etm->synth_opts.last_branch_sz)
1009 bs->nr += 1;
1010}
1011
1012static int cs_etm__inject_event(union perf_event *event,
1013 struct perf_sample *sample, u64 type)
1014{
1015 event->header.size = perf_event__sample_event_size(sample, type, 0);
1016 return perf_event__synthesize_sample(event, type, 0, sample);
1017}
1018
1019
1020static int
David Brazdil0f672f62019-12-10 10:32:29 +00001021cs_etm__get_trace(struct cs_etm_queue *etmq)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001022{
1023 struct auxtrace_buffer *aux_buffer = etmq->buffer;
1024 struct auxtrace_buffer *old_buffer = aux_buffer;
1025 struct auxtrace_queue *queue;
1026
1027 queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
1028
1029 aux_buffer = auxtrace_buffer__next(queue, aux_buffer);
1030
1031 /* If no more data, drop the previous auxtrace_buffer and return */
1032 if (!aux_buffer) {
1033 if (old_buffer)
1034 auxtrace_buffer__drop_data(old_buffer);
David Brazdil0f672f62019-12-10 10:32:29 +00001035 etmq->buf_len = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001036 return 0;
1037 }
1038
1039 etmq->buffer = aux_buffer;
1040
1041 /* If the aux_buffer doesn't have data associated, try to load it */
1042 if (!aux_buffer->data) {
1043 /* get the file desc associated with the perf data file */
1044 int fd = perf_data__fd(etmq->etm->session->data);
1045
1046 aux_buffer->data = auxtrace_buffer__get_data(aux_buffer, fd);
1047 if (!aux_buffer->data)
1048 return -ENOMEM;
1049 }
1050
1051 /* If valid, drop the previous buffer */
1052 if (old_buffer)
1053 auxtrace_buffer__drop_data(old_buffer);
1054
David Brazdil0f672f62019-12-10 10:32:29 +00001055 etmq->buf_used = 0;
1056 etmq->buf_len = aux_buffer->size;
1057 etmq->buf = aux_buffer->data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001058
David Brazdil0f672f62019-12-10 10:32:29 +00001059 return etmq->buf_len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001060}
1061
1062static void cs_etm__set_pid_tid_cpu(struct cs_etm_auxtrace *etm,
David Brazdil0f672f62019-12-10 10:32:29 +00001063 struct cs_etm_traceid_queue *tidq)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001064{
David Brazdil0f672f62019-12-10 10:32:29 +00001065 if ((!tidq->thread) && (tidq->tid != -1))
1066 tidq->thread = machine__find_thread(etm->machine, -1,
1067 tidq->tid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001068
David Brazdil0f672f62019-12-10 10:32:29 +00001069 if (tidq->thread)
1070 tidq->pid = tidq->thread->pid_;
1071}
1072
1073int cs_etm__etmq_set_tid(struct cs_etm_queue *etmq,
1074 pid_t tid, u8 trace_chan_id)
1075{
1076 int cpu, err = -EINVAL;
1077 struct cs_etm_auxtrace *etm = etmq->etm;
1078 struct cs_etm_traceid_queue *tidq;
1079
1080 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
1081 if (!tidq)
1082 return err;
1083
1084 if (cs_etm__get_cpu(trace_chan_id, &cpu) < 0)
1085 return err;
1086
1087 err = machine__set_current_tid(etm->machine, cpu, tid, tid);
1088 if (err)
1089 return err;
1090
1091 tidq->tid = tid;
1092 thread__zput(tidq->thread);
1093
1094 cs_etm__set_pid_tid_cpu(etm, tidq);
1095 return 0;
1096}
1097
1098bool cs_etm__etmq_is_timeless(struct cs_etm_queue *etmq)
1099{
1100 return !!etmq->etm->timeless_decoding;
1101}
1102
1103static void cs_etm__copy_insn(struct cs_etm_queue *etmq,
1104 u64 trace_chan_id,
1105 const struct cs_etm_packet *packet,
1106 struct perf_sample *sample)
1107{
1108 /*
1109 * It's pointless to read instructions for the CS_ETM_DISCONTINUITY
1110 * packet, so directly bail out with 'insn_len' = 0.
1111 */
1112 if (packet->sample_type == CS_ETM_DISCONTINUITY) {
1113 sample->insn_len = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001114 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001115 }
David Brazdil0f672f62019-12-10 10:32:29 +00001116
1117 /*
1118 * T32 instruction size might be 32-bit or 16-bit, decide by calling
1119 * cs_etm__t32_instr_size().
1120 */
1121 if (packet->isa == CS_ETM_ISA_T32)
1122 sample->insn_len = cs_etm__t32_instr_size(etmq, trace_chan_id,
1123 sample->ip);
1124 /* Otherwise, A64 and A32 instruction size are always 32-bit. */
1125 else
1126 sample->insn_len = 4;
1127
1128 cs_etm__mem_access(etmq, trace_chan_id, sample->ip,
1129 sample->insn_len, (void *)sample->insn);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001130}
1131
1132static int cs_etm__synth_instruction_sample(struct cs_etm_queue *etmq,
David Brazdil0f672f62019-12-10 10:32:29 +00001133 struct cs_etm_traceid_queue *tidq,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001134 u64 addr, u64 period)
1135{
1136 int ret = 0;
1137 struct cs_etm_auxtrace *etm = etmq->etm;
David Brazdil0f672f62019-12-10 10:32:29 +00001138 union perf_event *event = tidq->event_buf;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001139 struct perf_sample sample = {.ip = 0,};
1140
1141 event->sample.header.type = PERF_RECORD_SAMPLE;
1142 event->sample.header.misc = cs_etm__cpu_mode(etmq, addr);
1143 event->sample.header.size = sizeof(struct perf_event_header);
1144
1145 sample.ip = addr;
David Brazdil0f672f62019-12-10 10:32:29 +00001146 sample.pid = tidq->pid;
1147 sample.tid = tidq->tid;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001148 sample.id = etmq->etm->instructions_id;
1149 sample.stream_id = etmq->etm->instructions_id;
1150 sample.period = period;
David Brazdil0f672f62019-12-10 10:32:29 +00001151 sample.cpu = tidq->packet->cpu;
1152 sample.flags = tidq->prev_packet->flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001153 sample.cpumode = event->sample.header.misc;
1154
David Brazdil0f672f62019-12-10 10:32:29 +00001155 cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->packet, &sample);
1156
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001157 if (etm->synth_opts.last_branch) {
David Brazdil0f672f62019-12-10 10:32:29 +00001158 cs_etm__copy_last_branch_rb(etmq, tidq);
1159 sample.branch_stack = tidq->last_branch;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001160 }
1161
1162 if (etm->synth_opts.inject) {
1163 ret = cs_etm__inject_event(event, &sample,
1164 etm->instructions_sample_type);
1165 if (ret)
1166 return ret;
1167 }
1168
1169 ret = perf_session__deliver_synth_event(etm->session, event, &sample);
1170
1171 if (ret)
1172 pr_err(
1173 "CS ETM Trace: failed to deliver instruction event, error %d\n",
1174 ret);
1175
1176 if (etm->synth_opts.last_branch)
David Brazdil0f672f62019-12-10 10:32:29 +00001177 cs_etm__reset_last_branch_rb(tidq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001178
1179 return ret;
1180}
1181
1182/*
1183 * The cs etm packet encodes an instruction range between a branch target
1184 * and the next taken branch. Generate sample accordingly.
1185 */
David Brazdil0f672f62019-12-10 10:32:29 +00001186static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq,
1187 struct cs_etm_traceid_queue *tidq)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001188{
1189 int ret = 0;
1190 struct cs_etm_auxtrace *etm = etmq->etm;
1191 struct perf_sample sample = {.ip = 0,};
David Brazdil0f672f62019-12-10 10:32:29 +00001192 union perf_event *event = tidq->event_buf;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001193 struct dummy_branch_stack {
1194 u64 nr;
1195 struct branch_entry entries;
1196 } dummy_bs;
1197 u64 ip;
1198
David Brazdil0f672f62019-12-10 10:32:29 +00001199 ip = cs_etm__last_executed_instr(tidq->prev_packet);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001200
1201 event->sample.header.type = PERF_RECORD_SAMPLE;
1202 event->sample.header.misc = cs_etm__cpu_mode(etmq, ip);
1203 event->sample.header.size = sizeof(struct perf_event_header);
1204
1205 sample.ip = ip;
David Brazdil0f672f62019-12-10 10:32:29 +00001206 sample.pid = tidq->pid;
1207 sample.tid = tidq->tid;
1208 sample.addr = cs_etm__first_executed_instr(tidq->packet);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001209 sample.id = etmq->etm->branches_id;
1210 sample.stream_id = etmq->etm->branches_id;
1211 sample.period = 1;
David Brazdil0f672f62019-12-10 10:32:29 +00001212 sample.cpu = tidq->packet->cpu;
1213 sample.flags = tidq->prev_packet->flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001214 sample.cpumode = event->sample.header.misc;
1215
David Brazdil0f672f62019-12-10 10:32:29 +00001216 cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->prev_packet,
1217 &sample);
1218
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001219 /*
1220 * perf report cannot handle events without a branch stack
1221 */
1222 if (etm->synth_opts.last_branch) {
1223 dummy_bs = (struct dummy_branch_stack){
1224 .nr = 1,
1225 .entries = {
1226 .from = sample.ip,
1227 .to = sample.addr,
1228 },
1229 };
1230 sample.branch_stack = (struct branch_stack *)&dummy_bs;
1231 }
1232
1233 if (etm->synth_opts.inject) {
1234 ret = cs_etm__inject_event(event, &sample,
1235 etm->branches_sample_type);
1236 if (ret)
1237 return ret;
1238 }
1239
1240 ret = perf_session__deliver_synth_event(etm->session, event, &sample);
1241
1242 if (ret)
1243 pr_err(
1244 "CS ETM Trace: failed to deliver instruction event, error %d\n",
1245 ret);
1246
1247 return ret;
1248}
1249
1250struct cs_etm_synth {
1251 struct perf_tool dummy_tool;
1252 struct perf_session *session;
1253};
1254
1255static int cs_etm__event_synth(struct perf_tool *tool,
1256 union perf_event *event,
1257 struct perf_sample *sample __maybe_unused,
1258 struct machine *machine __maybe_unused)
1259{
1260 struct cs_etm_synth *cs_etm_synth =
1261 container_of(tool, struct cs_etm_synth, dummy_tool);
1262
1263 return perf_session__deliver_synth_event(cs_etm_synth->session,
1264 event, NULL);
1265}
1266
1267static int cs_etm__synth_event(struct perf_session *session,
1268 struct perf_event_attr *attr, u64 id)
1269{
1270 struct cs_etm_synth cs_etm_synth;
1271
1272 memset(&cs_etm_synth, 0, sizeof(struct cs_etm_synth));
1273 cs_etm_synth.session = session;
1274
1275 return perf_event__synthesize_attr(&cs_etm_synth.dummy_tool, attr, 1,
1276 &id, cs_etm__event_synth);
1277}
1278
1279static int cs_etm__synth_events(struct cs_etm_auxtrace *etm,
1280 struct perf_session *session)
1281{
David Brazdil0f672f62019-12-10 10:32:29 +00001282 struct evlist *evlist = session->evlist;
1283 struct evsel *evsel;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001284 struct perf_event_attr attr;
1285 bool found = false;
1286 u64 id;
1287 int err;
1288
1289 evlist__for_each_entry(evlist, evsel) {
David Brazdil0f672f62019-12-10 10:32:29 +00001290 if (evsel->core.attr.type == etm->pmu_type) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001291 found = true;
1292 break;
1293 }
1294 }
1295
1296 if (!found) {
1297 pr_debug("No selected events with CoreSight Trace data\n");
1298 return 0;
1299 }
1300
1301 memset(&attr, 0, sizeof(struct perf_event_attr));
1302 attr.size = sizeof(struct perf_event_attr);
1303 attr.type = PERF_TYPE_HARDWARE;
David Brazdil0f672f62019-12-10 10:32:29 +00001304 attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001305 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
1306 PERF_SAMPLE_PERIOD;
1307 if (etm->timeless_decoding)
1308 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
1309 else
1310 attr.sample_type |= PERF_SAMPLE_TIME;
1311
David Brazdil0f672f62019-12-10 10:32:29 +00001312 attr.exclude_user = evsel->core.attr.exclude_user;
1313 attr.exclude_kernel = evsel->core.attr.exclude_kernel;
1314 attr.exclude_hv = evsel->core.attr.exclude_hv;
1315 attr.exclude_host = evsel->core.attr.exclude_host;
1316 attr.exclude_guest = evsel->core.attr.exclude_guest;
1317 attr.sample_id_all = evsel->core.attr.sample_id_all;
1318 attr.read_format = evsel->core.attr.read_format;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001319
1320 /* create new id val to be a fixed offset from evsel id */
David Brazdil0f672f62019-12-10 10:32:29 +00001321 id = evsel->core.id[0] + 1000000000;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001322
1323 if (!id)
1324 id = 1;
1325
1326 if (etm->synth_opts.branches) {
1327 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
1328 attr.sample_period = 1;
1329 attr.sample_type |= PERF_SAMPLE_ADDR;
1330 err = cs_etm__synth_event(session, &attr, id);
1331 if (err)
1332 return err;
1333 etm->sample_branches = true;
1334 etm->branches_sample_type = attr.sample_type;
1335 etm->branches_id = id;
1336 id += 1;
1337 attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
1338 }
1339
1340 if (etm->synth_opts.last_branch)
1341 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
1342
1343 if (etm->synth_opts.instructions) {
1344 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
1345 attr.sample_period = etm->synth_opts.period;
1346 etm->instructions_sample_period = attr.sample_period;
1347 err = cs_etm__synth_event(session, &attr, id);
1348 if (err)
1349 return err;
1350 etm->sample_instructions = true;
1351 etm->instructions_sample_type = attr.sample_type;
1352 etm->instructions_id = id;
1353 id += 1;
1354 }
1355
1356 return 0;
1357}
1358
David Brazdil0f672f62019-12-10 10:32:29 +00001359static int cs_etm__sample(struct cs_etm_queue *etmq,
1360 struct cs_etm_traceid_queue *tidq)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001361{
1362 struct cs_etm_auxtrace *etm = etmq->etm;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001363 int ret;
David Brazdil0f672f62019-12-10 10:32:29 +00001364 u8 trace_chan_id = tidq->trace_chan_id;
Olivier Deprez0e641232021-09-23 10:07:05 +02001365 u64 instrs_prev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001366
Olivier Deprez0e641232021-09-23 10:07:05 +02001367 /* Get instructions remainder from previous packet */
1368 instrs_prev = tidq->period_instructions;
1369
1370 tidq->period_instructions += tidq->packet->instr_count;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001371
1372 /*
1373 * Record a branch when the last instruction in
1374 * PREV_PACKET is a branch.
1375 */
1376 if (etm->synth_opts.last_branch &&
David Brazdil0f672f62019-12-10 10:32:29 +00001377 tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1378 tidq->prev_packet->last_instr_taken_branch)
1379 cs_etm__update_last_branch_rb(etmq, tidq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001380
1381 if (etm->sample_instructions &&
David Brazdil0f672f62019-12-10 10:32:29 +00001382 tidq->period_instructions >= etm->instructions_sample_period) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001383 /*
1384 * Emit instruction sample periodically
1385 * TODO: allow period to be defined in cycles and clock time
1386 */
1387
Olivier Deprez0e641232021-09-23 10:07:05 +02001388 /*
1389 * Below diagram demonstrates the instruction samples
1390 * generation flows:
1391 *
1392 * Instrs Instrs Instrs Instrs
1393 * Sample(n) Sample(n+1) Sample(n+2) Sample(n+3)
1394 * | | | |
1395 * V V V V
1396 * --------------------------------------------------
1397 * ^ ^
1398 * | |
1399 * Period Period
1400 * instructions(Pi) instructions(Pi')
1401 *
1402 * | |
1403 * \---------------- -----------------/
1404 * V
1405 * tidq->packet->instr_count
1406 *
1407 * Instrs Sample(n...) are the synthesised samples occurring
1408 * every etm->instructions_sample_period instructions - as
1409 * defined on the perf command line. Sample(n) is being the
1410 * last sample before the current etm packet, n+1 to n+3
1411 * samples are generated from the current etm packet.
1412 *
1413 * tidq->packet->instr_count represents the number of
1414 * instructions in the current etm packet.
1415 *
1416 * Period instructions (Pi) contains the the number of
1417 * instructions executed after the sample point(n) from the
1418 * previous etm packet. This will always be less than
1419 * etm->instructions_sample_period.
1420 *
1421 * When generate new samples, it combines with two parts
1422 * instructions, one is the tail of the old packet and another
1423 * is the head of the new coming packet, to generate
1424 * sample(n+1); sample(n+2) and sample(n+3) consume the
1425 * instructions with sample period. After sample(n+3), the rest
1426 * instructions will be used by later packet and it is assigned
1427 * to tidq->period_instructions for next round calculation.
1428 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001429
1430 /*
Olivier Deprez0e641232021-09-23 10:07:05 +02001431 * Get the initial offset into the current packet instructions;
1432 * entry conditions ensure that instrs_prev is less than
1433 * etm->instructions_sample_period.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001434 */
Olivier Deprez0e641232021-09-23 10:07:05 +02001435 u64 offset = etm->instructions_sample_period - instrs_prev;
1436 u64 addr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001437
Olivier Deprez0e641232021-09-23 10:07:05 +02001438 while (tidq->period_instructions >=
1439 etm->instructions_sample_period) {
1440 /*
1441 * Calculate the address of the sampled instruction (-1
1442 * as sample is reported as though instruction has just
1443 * been executed, but PC has not advanced to next
1444 * instruction)
1445 */
1446 addr = cs_etm__instr_addr(etmq, trace_chan_id,
1447 tidq->packet, offset - 1);
1448 ret = cs_etm__synth_instruction_sample(
1449 etmq, tidq, addr,
1450 etm->instructions_sample_period);
1451 if (ret)
1452 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001453
Olivier Deprez0e641232021-09-23 10:07:05 +02001454 offset += etm->instructions_sample_period;
1455 tidq->period_instructions -=
1456 etm->instructions_sample_period;
1457 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001458 }
1459
David Brazdil0f672f62019-12-10 10:32:29 +00001460 if (etm->sample_branches) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001461 bool generate_sample = false;
1462
1463 /* Generate sample for tracing on packet */
David Brazdil0f672f62019-12-10 10:32:29 +00001464 if (tidq->prev_packet->sample_type == CS_ETM_DISCONTINUITY)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001465 generate_sample = true;
1466
1467 /* Generate sample for branch taken packet */
David Brazdil0f672f62019-12-10 10:32:29 +00001468 if (tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1469 tidq->prev_packet->last_instr_taken_branch)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001470 generate_sample = true;
1471
1472 if (generate_sample) {
David Brazdil0f672f62019-12-10 10:32:29 +00001473 ret = cs_etm__synth_branch_sample(etmq, tidq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001474 if (ret)
1475 return ret;
1476 }
1477 }
1478
Olivier Deprez0e641232021-09-23 10:07:05 +02001479 cs_etm__packet_swap(etm, tidq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001480
1481 return 0;
1482}
1483
David Brazdil0f672f62019-12-10 10:32:29 +00001484static int cs_etm__exception(struct cs_etm_traceid_queue *tidq)
1485{
1486 /*
1487 * When the exception packet is inserted, whether the last instruction
1488 * in previous range packet is taken branch or not, we need to force
1489 * to set 'prev_packet->last_instr_taken_branch' to true. This ensures
1490 * to generate branch sample for the instruction range before the
1491 * exception is trapped to kernel or before the exception returning.
1492 *
1493 * The exception packet includes the dummy address values, so don't
1494 * swap PACKET with PREV_PACKET. This keeps PREV_PACKET to be useful
1495 * for generating instruction and branch samples.
1496 */
1497 if (tidq->prev_packet->sample_type == CS_ETM_RANGE)
1498 tidq->prev_packet->last_instr_taken_branch = true;
1499
1500 return 0;
1501}
1502
1503static int cs_etm__flush(struct cs_etm_queue *etmq,
1504 struct cs_etm_traceid_queue *tidq)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001505{
1506 int err = 0;
1507 struct cs_etm_auxtrace *etm = etmq->etm;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001508
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001509 /* Handle start tracing packet */
David Brazdil0f672f62019-12-10 10:32:29 +00001510 if (tidq->prev_packet->sample_type == CS_ETM_EMPTY)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001511 goto swap_packet;
1512
1513 if (etmq->etm->synth_opts.last_branch &&
David Brazdil0f672f62019-12-10 10:32:29 +00001514 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001515 /*
1516 * Generate a last branch event for the branches left in the
1517 * circular buffer at the end of the trace.
1518 *
1519 * Use the address of the end of the last reported execution
1520 * range
1521 */
David Brazdil0f672f62019-12-10 10:32:29 +00001522 u64 addr = cs_etm__last_executed_instr(tidq->prev_packet);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001523
1524 err = cs_etm__synth_instruction_sample(
David Brazdil0f672f62019-12-10 10:32:29 +00001525 etmq, tidq, addr,
1526 tidq->period_instructions);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001527 if (err)
1528 return err;
1529
David Brazdil0f672f62019-12-10 10:32:29 +00001530 tidq->period_instructions = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001531
1532 }
1533
1534 if (etm->sample_branches &&
David Brazdil0f672f62019-12-10 10:32:29 +00001535 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1536 err = cs_etm__synth_branch_sample(etmq, tidq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001537 if (err)
1538 return err;
1539 }
1540
1541swap_packet:
Olivier Deprez0e641232021-09-23 10:07:05 +02001542 cs_etm__packet_swap(etm, tidq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001543
1544 return err;
1545}
1546
David Brazdil0f672f62019-12-10 10:32:29 +00001547static int cs_etm__end_block(struct cs_etm_queue *etmq,
1548 struct cs_etm_traceid_queue *tidq)
1549{
1550 int err;
1551
1552 /*
1553 * It has no new packet coming and 'etmq->packet' contains the stale
1554 * packet which was set at the previous time with packets swapping;
1555 * so skip to generate branch sample to avoid stale packet.
1556 *
1557 * For this case only flush branch stack and generate a last branch
1558 * event for the branches left in the circular buffer at the end of
1559 * the trace.
1560 */
1561 if (etmq->etm->synth_opts.last_branch &&
1562 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1563 /*
1564 * Use the address of the end of the last reported execution
1565 * range.
1566 */
1567 u64 addr = cs_etm__last_executed_instr(tidq->prev_packet);
1568
1569 err = cs_etm__synth_instruction_sample(
1570 etmq, tidq, addr,
1571 tidq->period_instructions);
1572 if (err)
1573 return err;
1574
1575 tidq->period_instructions = 0;
1576 }
1577
1578 return 0;
1579}
1580/*
1581 * cs_etm__get_data_block: Fetch a block from the auxtrace_buffer queue
1582 * if need be.
1583 * Returns: < 0 if error
1584 * = 0 if no more auxtrace_buffer to read
1585 * > 0 if the current buffer isn't empty yet
1586 */
1587static int cs_etm__get_data_block(struct cs_etm_queue *etmq)
1588{
1589 int ret;
1590
1591 if (!etmq->buf_len) {
1592 ret = cs_etm__get_trace(etmq);
1593 if (ret <= 0)
1594 return ret;
1595 /*
1596 * We cannot assume consecutive blocks in the data file
1597 * are contiguous, reset the decoder to force re-sync.
1598 */
1599 ret = cs_etm_decoder__reset(etmq->decoder);
1600 if (ret)
1601 return ret;
1602 }
1603
1604 return etmq->buf_len;
1605}
1606
1607static bool cs_etm__is_svc_instr(struct cs_etm_queue *etmq, u8 trace_chan_id,
1608 struct cs_etm_packet *packet,
1609 u64 end_addr)
1610{
1611 /* Initialise to keep compiler happy */
1612 u16 instr16 = 0;
1613 u32 instr32 = 0;
1614 u64 addr;
1615
1616 switch (packet->isa) {
1617 case CS_ETM_ISA_T32:
1618 /*
1619 * The SVC of T32 is defined in ARM DDI 0487D.a, F5.1.247:
1620 *
1621 * b'15 b'8
1622 * +-----------------+--------+
1623 * | 1 1 0 1 1 1 1 1 | imm8 |
1624 * +-----------------+--------+
1625 *
1626 * According to the specifiction, it only defines SVC for T32
1627 * with 16 bits instruction and has no definition for 32bits;
1628 * so below only read 2 bytes as instruction size for T32.
1629 */
1630 addr = end_addr - 2;
1631 cs_etm__mem_access(etmq, trace_chan_id, addr,
1632 sizeof(instr16), (u8 *)&instr16);
1633 if ((instr16 & 0xFF00) == 0xDF00)
1634 return true;
1635
1636 break;
1637 case CS_ETM_ISA_A32:
1638 /*
1639 * The SVC of A32 is defined in ARM DDI 0487D.a, F5.1.247:
1640 *
1641 * b'31 b'28 b'27 b'24
1642 * +---------+---------+-------------------------+
1643 * | !1111 | 1 1 1 1 | imm24 |
1644 * +---------+---------+-------------------------+
1645 */
1646 addr = end_addr - 4;
1647 cs_etm__mem_access(etmq, trace_chan_id, addr,
1648 sizeof(instr32), (u8 *)&instr32);
1649 if ((instr32 & 0x0F000000) == 0x0F000000 &&
1650 (instr32 & 0xF0000000) != 0xF0000000)
1651 return true;
1652
1653 break;
1654 case CS_ETM_ISA_A64:
1655 /*
1656 * The SVC of A64 is defined in ARM DDI 0487D.a, C6.2.294:
1657 *
1658 * b'31 b'21 b'4 b'0
1659 * +-----------------------+---------+-----------+
1660 * | 1 1 0 1 0 1 0 0 0 0 0 | imm16 | 0 0 0 0 1 |
1661 * +-----------------------+---------+-----------+
1662 */
1663 addr = end_addr - 4;
1664 cs_etm__mem_access(etmq, trace_chan_id, addr,
1665 sizeof(instr32), (u8 *)&instr32);
1666 if ((instr32 & 0xFFE0001F) == 0xd4000001)
1667 return true;
1668
1669 break;
1670 case CS_ETM_ISA_UNKNOWN:
1671 default:
1672 break;
1673 }
1674
1675 return false;
1676}
1677
1678static bool cs_etm__is_syscall(struct cs_etm_queue *etmq,
1679 struct cs_etm_traceid_queue *tidq, u64 magic)
1680{
1681 u8 trace_chan_id = tidq->trace_chan_id;
1682 struct cs_etm_packet *packet = tidq->packet;
1683 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1684
1685 if (magic == __perf_cs_etmv3_magic)
1686 if (packet->exception_number == CS_ETMV3_EXC_SVC)
1687 return true;
1688
1689 /*
1690 * ETMv4 exception type CS_ETMV4_EXC_CALL covers SVC, SMC and
1691 * HVC cases; need to check if it's SVC instruction based on
1692 * packet address.
1693 */
1694 if (magic == __perf_cs_etmv4_magic) {
1695 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
1696 cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet,
1697 prev_packet->end_addr))
1698 return true;
1699 }
1700
1701 return false;
1702}
1703
1704static bool cs_etm__is_async_exception(struct cs_etm_traceid_queue *tidq,
1705 u64 magic)
1706{
1707 struct cs_etm_packet *packet = tidq->packet;
1708
1709 if (magic == __perf_cs_etmv3_magic)
1710 if (packet->exception_number == CS_ETMV3_EXC_DEBUG_HALT ||
1711 packet->exception_number == CS_ETMV3_EXC_ASYNC_DATA_ABORT ||
1712 packet->exception_number == CS_ETMV3_EXC_PE_RESET ||
1713 packet->exception_number == CS_ETMV3_EXC_IRQ ||
1714 packet->exception_number == CS_ETMV3_EXC_FIQ)
1715 return true;
1716
1717 if (magic == __perf_cs_etmv4_magic)
1718 if (packet->exception_number == CS_ETMV4_EXC_RESET ||
1719 packet->exception_number == CS_ETMV4_EXC_DEBUG_HALT ||
1720 packet->exception_number == CS_ETMV4_EXC_SYSTEM_ERROR ||
1721 packet->exception_number == CS_ETMV4_EXC_INST_DEBUG ||
1722 packet->exception_number == CS_ETMV4_EXC_DATA_DEBUG ||
1723 packet->exception_number == CS_ETMV4_EXC_IRQ ||
1724 packet->exception_number == CS_ETMV4_EXC_FIQ)
1725 return true;
1726
1727 return false;
1728}
1729
1730static bool cs_etm__is_sync_exception(struct cs_etm_queue *etmq,
1731 struct cs_etm_traceid_queue *tidq,
1732 u64 magic)
1733{
1734 u8 trace_chan_id = tidq->trace_chan_id;
1735 struct cs_etm_packet *packet = tidq->packet;
1736 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1737
1738 if (magic == __perf_cs_etmv3_magic)
1739 if (packet->exception_number == CS_ETMV3_EXC_SMC ||
1740 packet->exception_number == CS_ETMV3_EXC_HYP ||
1741 packet->exception_number == CS_ETMV3_EXC_JAZELLE_THUMBEE ||
1742 packet->exception_number == CS_ETMV3_EXC_UNDEFINED_INSTR ||
1743 packet->exception_number == CS_ETMV3_EXC_PREFETCH_ABORT ||
1744 packet->exception_number == CS_ETMV3_EXC_DATA_FAULT ||
1745 packet->exception_number == CS_ETMV3_EXC_GENERIC)
1746 return true;
1747
1748 if (magic == __perf_cs_etmv4_magic) {
1749 if (packet->exception_number == CS_ETMV4_EXC_TRAP ||
1750 packet->exception_number == CS_ETMV4_EXC_ALIGNMENT ||
1751 packet->exception_number == CS_ETMV4_EXC_INST_FAULT ||
1752 packet->exception_number == CS_ETMV4_EXC_DATA_FAULT)
1753 return true;
1754
1755 /*
1756 * For CS_ETMV4_EXC_CALL, except SVC other instructions
1757 * (SMC, HVC) are taken as sync exceptions.
1758 */
1759 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
1760 !cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet,
1761 prev_packet->end_addr))
1762 return true;
1763
1764 /*
1765 * ETMv4 has 5 bits for exception number; if the numbers
1766 * are in the range ( CS_ETMV4_EXC_FIQ, CS_ETMV4_EXC_END ]
1767 * they are implementation defined exceptions.
1768 *
1769 * For this case, simply take it as sync exception.
1770 */
1771 if (packet->exception_number > CS_ETMV4_EXC_FIQ &&
1772 packet->exception_number <= CS_ETMV4_EXC_END)
1773 return true;
1774 }
1775
1776 return false;
1777}
1778
1779static int cs_etm__set_sample_flags(struct cs_etm_queue *etmq,
1780 struct cs_etm_traceid_queue *tidq)
1781{
1782 struct cs_etm_packet *packet = tidq->packet;
1783 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1784 u8 trace_chan_id = tidq->trace_chan_id;
1785 u64 magic;
1786 int ret;
1787
1788 switch (packet->sample_type) {
1789 case CS_ETM_RANGE:
1790 /*
1791 * Immediate branch instruction without neither link nor
1792 * return flag, it's normal branch instruction within
1793 * the function.
1794 */
1795 if (packet->last_instr_type == OCSD_INSTR_BR &&
1796 packet->last_instr_subtype == OCSD_S_INSTR_NONE) {
1797 packet->flags = PERF_IP_FLAG_BRANCH;
1798
1799 if (packet->last_instr_cond)
1800 packet->flags |= PERF_IP_FLAG_CONDITIONAL;
1801 }
1802
1803 /*
1804 * Immediate branch instruction with link (e.g. BL), this is
1805 * branch instruction for function call.
1806 */
1807 if (packet->last_instr_type == OCSD_INSTR_BR &&
1808 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
1809 packet->flags = PERF_IP_FLAG_BRANCH |
1810 PERF_IP_FLAG_CALL;
1811
1812 /*
1813 * Indirect branch instruction with link (e.g. BLR), this is
1814 * branch instruction for function call.
1815 */
1816 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1817 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
1818 packet->flags = PERF_IP_FLAG_BRANCH |
1819 PERF_IP_FLAG_CALL;
1820
1821 /*
1822 * Indirect branch instruction with subtype of
1823 * OCSD_S_INSTR_V7_IMPLIED_RET, this is explicit hint for
1824 * function return for A32/T32.
1825 */
1826 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1827 packet->last_instr_subtype == OCSD_S_INSTR_V7_IMPLIED_RET)
1828 packet->flags = PERF_IP_FLAG_BRANCH |
1829 PERF_IP_FLAG_RETURN;
1830
1831 /*
1832 * Indirect branch instruction without link (e.g. BR), usually
1833 * this is used for function return, especially for functions
1834 * within dynamic link lib.
1835 */
1836 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1837 packet->last_instr_subtype == OCSD_S_INSTR_NONE)
1838 packet->flags = PERF_IP_FLAG_BRANCH |
1839 PERF_IP_FLAG_RETURN;
1840
1841 /* Return instruction for function return. */
1842 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1843 packet->last_instr_subtype == OCSD_S_INSTR_V8_RET)
1844 packet->flags = PERF_IP_FLAG_BRANCH |
1845 PERF_IP_FLAG_RETURN;
1846
1847 /*
1848 * Decoder might insert a discontinuity in the middle of
1849 * instruction packets, fixup prev_packet with flag
1850 * PERF_IP_FLAG_TRACE_BEGIN to indicate restarting trace.
1851 */
1852 if (prev_packet->sample_type == CS_ETM_DISCONTINUITY)
1853 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
1854 PERF_IP_FLAG_TRACE_BEGIN;
1855
1856 /*
1857 * If the previous packet is an exception return packet
1858 * and the return address just follows SVC instuction,
1859 * it needs to calibrate the previous packet sample flags
1860 * as PERF_IP_FLAG_SYSCALLRET.
1861 */
1862 if (prev_packet->flags == (PERF_IP_FLAG_BRANCH |
1863 PERF_IP_FLAG_RETURN |
1864 PERF_IP_FLAG_INTERRUPT) &&
1865 cs_etm__is_svc_instr(etmq, trace_chan_id,
1866 packet, packet->start_addr))
1867 prev_packet->flags = PERF_IP_FLAG_BRANCH |
1868 PERF_IP_FLAG_RETURN |
1869 PERF_IP_FLAG_SYSCALLRET;
1870 break;
1871 case CS_ETM_DISCONTINUITY:
1872 /*
1873 * The trace is discontinuous, if the previous packet is
1874 * instruction packet, set flag PERF_IP_FLAG_TRACE_END
1875 * for previous packet.
1876 */
1877 if (prev_packet->sample_type == CS_ETM_RANGE)
1878 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
1879 PERF_IP_FLAG_TRACE_END;
1880 break;
1881 case CS_ETM_EXCEPTION:
1882 ret = cs_etm__get_magic(packet->trace_chan_id, &magic);
1883 if (ret)
1884 return ret;
1885
1886 /* The exception is for system call. */
1887 if (cs_etm__is_syscall(etmq, tidq, magic))
1888 packet->flags = PERF_IP_FLAG_BRANCH |
1889 PERF_IP_FLAG_CALL |
1890 PERF_IP_FLAG_SYSCALLRET;
1891 /*
1892 * The exceptions are triggered by external signals from bus,
1893 * interrupt controller, debug module, PE reset or halt.
1894 */
1895 else if (cs_etm__is_async_exception(tidq, magic))
1896 packet->flags = PERF_IP_FLAG_BRANCH |
1897 PERF_IP_FLAG_CALL |
1898 PERF_IP_FLAG_ASYNC |
1899 PERF_IP_FLAG_INTERRUPT;
1900 /*
1901 * Otherwise, exception is caused by trap, instruction &
1902 * data fault, or alignment errors.
1903 */
1904 else if (cs_etm__is_sync_exception(etmq, tidq, magic))
1905 packet->flags = PERF_IP_FLAG_BRANCH |
1906 PERF_IP_FLAG_CALL |
1907 PERF_IP_FLAG_INTERRUPT;
1908
1909 /*
1910 * When the exception packet is inserted, since exception
1911 * packet is not used standalone for generating samples
1912 * and it's affiliation to the previous instruction range
1913 * packet; so set previous range packet flags to tell perf
1914 * it is an exception taken branch.
1915 */
1916 if (prev_packet->sample_type == CS_ETM_RANGE)
1917 prev_packet->flags = packet->flags;
1918 break;
1919 case CS_ETM_EXCEPTION_RET:
1920 /*
1921 * When the exception return packet is inserted, since
1922 * exception return packet is not used standalone for
1923 * generating samples and it's affiliation to the previous
1924 * instruction range packet; so set previous range packet
1925 * flags to tell perf it is an exception return branch.
1926 *
1927 * The exception return can be for either system call or
1928 * other exception types; unfortunately the packet doesn't
1929 * contain exception type related info so we cannot decide
1930 * the exception type purely based on exception return packet.
1931 * If we record the exception number from exception packet and
1932 * reuse it for excpetion return packet, this is not reliable
1933 * due the trace can be discontinuity or the interrupt can
1934 * be nested, thus the recorded exception number cannot be
1935 * used for exception return packet for these two cases.
1936 *
1937 * For exception return packet, we only need to distinguish the
1938 * packet is for system call or for other types. Thus the
1939 * decision can be deferred when receive the next packet which
1940 * contains the return address, based on the return address we
1941 * can read out the previous instruction and check if it's a
1942 * system call instruction and then calibrate the sample flag
1943 * as needed.
1944 */
1945 if (prev_packet->sample_type == CS_ETM_RANGE)
1946 prev_packet->flags = PERF_IP_FLAG_BRANCH |
1947 PERF_IP_FLAG_RETURN |
1948 PERF_IP_FLAG_INTERRUPT;
1949 break;
1950 case CS_ETM_EMPTY:
1951 default:
1952 break;
1953 }
1954
1955 return 0;
1956}
1957
1958static int cs_etm__decode_data_block(struct cs_etm_queue *etmq)
1959{
1960 int ret = 0;
1961 size_t processed = 0;
1962
1963 /*
1964 * Packets are decoded and added to the decoder's packet queue
1965 * until the decoder packet processing callback has requested that
1966 * processing stops or there is nothing left in the buffer. Normal
1967 * operations that stop processing are a timestamp packet or a full
1968 * decoder buffer queue.
1969 */
1970 ret = cs_etm_decoder__process_data_block(etmq->decoder,
1971 etmq->offset,
1972 &etmq->buf[etmq->buf_used],
1973 etmq->buf_len,
1974 &processed);
1975 if (ret)
1976 goto out;
1977
1978 etmq->offset += processed;
1979 etmq->buf_used += processed;
1980 etmq->buf_len -= processed;
1981
1982out:
1983 return ret;
1984}
1985
1986static int cs_etm__process_traceid_queue(struct cs_etm_queue *etmq,
1987 struct cs_etm_traceid_queue *tidq)
1988{
1989 int ret;
1990 struct cs_etm_packet_queue *packet_queue;
1991
1992 packet_queue = &tidq->packet_queue;
1993
1994 /* Process each packet in this chunk */
1995 while (1) {
1996 ret = cs_etm_decoder__get_packet(packet_queue,
1997 tidq->packet);
1998 if (ret <= 0)
1999 /*
2000 * Stop processing this chunk on
2001 * end of data or error
2002 */
2003 break;
2004
2005 /*
2006 * Since packet addresses are swapped in packet
2007 * handling within below switch() statements,
2008 * thus setting sample flags must be called
2009 * prior to switch() statement to use address
2010 * information before packets swapping.
2011 */
2012 ret = cs_etm__set_sample_flags(etmq, tidq);
2013 if (ret < 0)
2014 break;
2015
2016 switch (tidq->packet->sample_type) {
2017 case CS_ETM_RANGE:
2018 /*
2019 * If the packet contains an instruction
2020 * range, generate instruction sequence
2021 * events.
2022 */
2023 cs_etm__sample(etmq, tidq);
2024 break;
2025 case CS_ETM_EXCEPTION:
2026 case CS_ETM_EXCEPTION_RET:
2027 /*
2028 * If the exception packet is coming,
2029 * make sure the previous instruction
2030 * range packet to be handled properly.
2031 */
2032 cs_etm__exception(tidq);
2033 break;
2034 case CS_ETM_DISCONTINUITY:
2035 /*
2036 * Discontinuity in trace, flush
2037 * previous branch stack
2038 */
2039 cs_etm__flush(etmq, tidq);
2040 break;
2041 case CS_ETM_EMPTY:
2042 /*
2043 * Should not receive empty packet,
2044 * report error.
2045 */
2046 pr_err("CS ETM Trace: empty packet\n");
2047 return -EINVAL;
2048 default:
2049 break;
2050 }
2051 }
2052
2053 return ret;
2054}
2055
2056static void cs_etm__clear_all_traceid_queues(struct cs_etm_queue *etmq)
2057{
2058 int idx;
2059 struct int_node *inode;
2060 struct cs_etm_traceid_queue *tidq;
2061 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
2062
2063 intlist__for_each_entry(inode, traceid_queues_list) {
2064 idx = (int)(intptr_t)inode->priv;
2065 tidq = etmq->traceid_queues[idx];
2066
2067 /* Ignore return value */
2068 cs_etm__process_traceid_queue(etmq, tidq);
2069
2070 /*
2071 * Generate an instruction sample with the remaining
2072 * branchstack entries.
2073 */
2074 cs_etm__flush(etmq, tidq);
2075 }
2076}
2077
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002078static int cs_etm__run_decoder(struct cs_etm_queue *etmq)
2079{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002080 int err = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00002081 struct cs_etm_traceid_queue *tidq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002082
David Brazdil0f672f62019-12-10 10:32:29 +00002083 tidq = cs_etm__etmq_get_traceid_queue(etmq, CS_ETM_PER_THREAD_TRACEID);
2084 if (!tidq)
2085 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002086
2087 /* Go through each buffer in the queue and decode them one by one */
2088 while (1) {
David Brazdil0f672f62019-12-10 10:32:29 +00002089 err = cs_etm__get_data_block(etmq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002090 if (err <= 0)
2091 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002092
2093 /* Run trace decoder until buffer consumed or end of trace */
2094 do {
David Brazdil0f672f62019-12-10 10:32:29 +00002095 err = cs_etm__decode_data_block(etmq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002096 if (err)
2097 return err;
2098
David Brazdil0f672f62019-12-10 10:32:29 +00002099 /*
2100 * Process each packet in this chunk, nothing to do if
2101 * an error occurs other than hoping the next one will
2102 * be better.
2103 */
2104 err = cs_etm__process_traceid_queue(etmq, tidq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002105
David Brazdil0f672f62019-12-10 10:32:29 +00002106 } while (etmq->buf_len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002107
2108 if (err == 0)
2109 /* Flush any remaining branch stack entries */
David Brazdil0f672f62019-12-10 10:32:29 +00002110 err = cs_etm__end_block(etmq, tidq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002111 }
2112
2113 return err;
2114}
2115
2116static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
David Brazdil0f672f62019-12-10 10:32:29 +00002117 pid_t tid)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002118{
2119 unsigned int i;
2120 struct auxtrace_queues *queues = &etm->queues;
2121
2122 for (i = 0; i < queues->nr_queues; i++) {
2123 struct auxtrace_queue *queue = &etm->queues.queue_array[i];
2124 struct cs_etm_queue *etmq = queue->priv;
David Brazdil0f672f62019-12-10 10:32:29 +00002125 struct cs_etm_traceid_queue *tidq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002126
David Brazdil0f672f62019-12-10 10:32:29 +00002127 if (!etmq)
2128 continue;
2129
2130 tidq = cs_etm__etmq_get_traceid_queue(etmq,
2131 CS_ETM_PER_THREAD_TRACEID);
2132
2133 if (!tidq)
2134 continue;
2135
2136 if ((tid == -1) || (tidq->tid == tid)) {
2137 cs_etm__set_pid_tid_cpu(etm, tidq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002138 cs_etm__run_decoder(etmq);
2139 }
2140 }
2141
2142 return 0;
2143}
2144
David Brazdil0f672f62019-12-10 10:32:29 +00002145static int cs_etm__process_queues(struct cs_etm_auxtrace *etm)
2146{
2147 int ret = 0;
2148 unsigned int cs_queue_nr, queue_nr;
2149 u8 trace_chan_id;
2150 u64 timestamp;
2151 struct auxtrace_queue *queue;
2152 struct cs_etm_queue *etmq;
2153 struct cs_etm_traceid_queue *tidq;
2154
2155 while (1) {
2156 if (!etm->heap.heap_cnt)
2157 goto out;
2158
2159 /* Take the entry at the top of the min heap */
2160 cs_queue_nr = etm->heap.heap_array[0].queue_nr;
2161 queue_nr = TO_QUEUE_NR(cs_queue_nr);
2162 trace_chan_id = TO_TRACE_CHAN_ID(cs_queue_nr);
2163 queue = &etm->queues.queue_array[queue_nr];
2164 etmq = queue->priv;
2165
2166 /*
2167 * Remove the top entry from the heap since we are about
2168 * to process it.
2169 */
2170 auxtrace_heap__pop(&etm->heap);
2171
2172 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
2173 if (!tidq) {
2174 /*
2175 * No traceID queue has been allocated for this traceID,
2176 * which means something somewhere went very wrong. No
2177 * other choice than simply exit.
2178 */
2179 ret = -EINVAL;
2180 goto out;
2181 }
2182
2183 /*
2184 * Packets associated with this timestamp are already in
2185 * the etmq's traceID queue, so process them.
2186 */
2187 ret = cs_etm__process_traceid_queue(etmq, tidq);
2188 if (ret < 0)
2189 goto out;
2190
2191 /*
2192 * Packets for this timestamp have been processed, time to
2193 * move on to the next timestamp, fetching a new auxtrace_buffer
2194 * if need be.
2195 */
2196refetch:
2197 ret = cs_etm__get_data_block(etmq);
2198 if (ret < 0)
2199 goto out;
2200
2201 /*
2202 * No more auxtrace_buffers to process in this etmq, simply
2203 * move on to another entry in the auxtrace_heap.
2204 */
2205 if (!ret)
2206 continue;
2207
2208 ret = cs_etm__decode_data_block(etmq);
2209 if (ret)
2210 goto out;
2211
2212 timestamp = cs_etm__etmq_get_timestamp(etmq, &trace_chan_id);
2213
2214 if (!timestamp) {
2215 /*
2216 * Function cs_etm__decode_data_block() returns when
2217 * there is no more traces to decode in the current
2218 * auxtrace_buffer OR when a timestamp has been
2219 * encountered on any of the traceID queues. Since we
2220 * did not get a timestamp, there is no more traces to
2221 * process in this auxtrace_buffer. As such empty and
2222 * flush all traceID queues.
2223 */
2224 cs_etm__clear_all_traceid_queues(etmq);
2225
2226 /* Fetch another auxtrace_buffer for this etmq */
2227 goto refetch;
2228 }
2229
2230 /*
2231 * Add to the min heap the timestamp for packets that have
2232 * just been decoded. They will be processed and synthesized
2233 * during the next call to cs_etm__process_traceid_queue() for
2234 * this queue/traceID.
2235 */
2236 cs_queue_nr = TO_CS_QUEUE_NR(queue_nr, trace_chan_id);
2237 ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, timestamp);
2238 }
2239
2240out:
2241 return ret;
2242}
2243
2244static int cs_etm__process_itrace_start(struct cs_etm_auxtrace *etm,
2245 union perf_event *event)
2246{
2247 struct thread *th;
2248
2249 if (etm->timeless_decoding)
2250 return 0;
2251
2252 /*
2253 * Add the tid/pid to the log so that we can get a match when
2254 * we get a contextID from the decoder.
2255 */
2256 th = machine__findnew_thread(etm->machine,
2257 event->itrace_start.pid,
2258 event->itrace_start.tid);
2259 if (!th)
2260 return -ENOMEM;
2261
2262 thread__put(th);
2263
2264 return 0;
2265}
2266
2267static int cs_etm__process_switch_cpu_wide(struct cs_etm_auxtrace *etm,
2268 union perf_event *event)
2269{
2270 struct thread *th;
2271 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
2272
2273 /*
2274 * Context switch in per-thread mode are irrelevant since perf
2275 * will start/stop tracing as the process is scheduled.
2276 */
2277 if (etm->timeless_decoding)
2278 return 0;
2279
2280 /*
2281 * SWITCH_IN events carry the next process to be switched out while
2282 * SWITCH_OUT events carry the process to be switched in. As such
2283 * we don't care about IN events.
2284 */
2285 if (!out)
2286 return 0;
2287
2288 /*
2289 * Add the tid/pid to the log so that we can get a match when
2290 * we get a contextID from the decoder.
2291 */
2292 th = machine__findnew_thread(etm->machine,
2293 event->context_switch.next_prev_pid,
2294 event->context_switch.next_prev_tid);
2295 if (!th)
2296 return -ENOMEM;
2297
2298 thread__put(th);
2299
2300 return 0;
2301}
2302
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002303static int cs_etm__process_event(struct perf_session *session,
2304 union perf_event *event,
2305 struct perf_sample *sample,
2306 struct perf_tool *tool)
2307{
2308 int err = 0;
2309 u64 timestamp;
2310 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2311 struct cs_etm_auxtrace,
2312 auxtrace);
2313
2314 if (dump_trace)
2315 return 0;
2316
2317 if (!tool->ordered_events) {
2318 pr_err("CoreSight ETM Trace requires ordered events\n");
2319 return -EINVAL;
2320 }
2321
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002322 if (sample->time && (sample->time != (u64) -1))
2323 timestamp = sample->time;
2324 else
2325 timestamp = 0;
2326
2327 if (timestamp || etm->timeless_decoding) {
2328 err = cs_etm__update_queues(etm);
2329 if (err)
2330 return err;
2331 }
2332
David Brazdil0f672f62019-12-10 10:32:29 +00002333 if (etm->timeless_decoding &&
2334 event->header.type == PERF_RECORD_EXIT)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002335 return cs_etm__process_timeless_queues(etm,
David Brazdil0f672f62019-12-10 10:32:29 +00002336 event->fork.tid);
2337
2338 if (event->header.type == PERF_RECORD_ITRACE_START)
2339 return cs_etm__process_itrace_start(etm, event);
2340 else if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)
2341 return cs_etm__process_switch_cpu_wide(etm, event);
2342
2343 if (!etm->timeless_decoding &&
2344 event->header.type == PERF_RECORD_AUX)
2345 return cs_etm__process_queues(etm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002346
2347 return 0;
2348}
2349
2350static int cs_etm__process_auxtrace_event(struct perf_session *session,
2351 union perf_event *event,
2352 struct perf_tool *tool __maybe_unused)
2353{
2354 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2355 struct cs_etm_auxtrace,
2356 auxtrace);
2357 if (!etm->data_queued) {
2358 struct auxtrace_buffer *buffer;
2359 off_t data_offset;
2360 int fd = perf_data__fd(session->data);
2361 bool is_pipe = perf_data__is_pipe(session->data);
2362 int err;
2363
2364 if (is_pipe)
2365 data_offset = 0;
2366 else {
2367 data_offset = lseek(fd, 0, SEEK_CUR);
2368 if (data_offset == -1)
2369 return -errno;
2370 }
2371
2372 err = auxtrace_queues__add_event(&etm->queues, session,
2373 event, data_offset, &buffer);
2374 if (err)
2375 return err;
2376
2377 if (dump_trace)
2378 if (auxtrace_buffer__get_data(buffer, fd)) {
2379 cs_etm__dump_event(etm, buffer);
2380 auxtrace_buffer__put_data(buffer);
2381 }
2382 }
2383
2384 return 0;
2385}
2386
2387static bool cs_etm__is_timeless_decoding(struct cs_etm_auxtrace *etm)
2388{
David Brazdil0f672f62019-12-10 10:32:29 +00002389 struct evsel *evsel;
2390 struct evlist *evlist = etm->session->evlist;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002391 bool timeless_decoding = true;
2392
2393 /*
2394 * Circle through the list of event and complain if we find one
2395 * with the time bit set.
2396 */
2397 evlist__for_each_entry(evlist, evsel) {
David Brazdil0f672f62019-12-10 10:32:29 +00002398 if ((evsel->core.attr.sample_type & PERF_SAMPLE_TIME))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002399 timeless_decoding = false;
2400 }
2401
2402 return timeless_decoding;
2403}
2404
2405static const char * const cs_etm_global_header_fmts[] = {
2406 [CS_HEADER_VERSION_0] = " Header version %llx\n",
2407 [CS_PMU_TYPE_CPUS] = " PMU type/num cpus %llx\n",
2408 [CS_ETM_SNAPSHOT] = " Snapshot %llx\n",
2409};
2410
2411static const char * const cs_etm_priv_fmts[] = {
2412 [CS_ETM_MAGIC] = " Magic number %llx\n",
2413 [CS_ETM_CPU] = " CPU %lld\n",
2414 [CS_ETM_ETMCR] = " ETMCR %llx\n",
2415 [CS_ETM_ETMTRACEIDR] = " ETMTRACEIDR %llx\n",
2416 [CS_ETM_ETMCCER] = " ETMCCER %llx\n",
2417 [CS_ETM_ETMIDR] = " ETMIDR %llx\n",
2418};
2419
2420static const char * const cs_etmv4_priv_fmts[] = {
2421 [CS_ETM_MAGIC] = " Magic number %llx\n",
2422 [CS_ETM_CPU] = " CPU %lld\n",
2423 [CS_ETMV4_TRCCONFIGR] = " TRCCONFIGR %llx\n",
2424 [CS_ETMV4_TRCTRACEIDR] = " TRCTRACEIDR %llx\n",
2425 [CS_ETMV4_TRCIDR0] = " TRCIDR0 %llx\n",
2426 [CS_ETMV4_TRCIDR1] = " TRCIDR1 %llx\n",
2427 [CS_ETMV4_TRCIDR2] = " TRCIDR2 %llx\n",
2428 [CS_ETMV4_TRCIDR8] = " TRCIDR8 %llx\n",
2429 [CS_ETMV4_TRCAUTHSTATUS] = " TRCAUTHSTATUS %llx\n",
2430};
2431
David Brazdil0f672f62019-12-10 10:32:29 +00002432static void cs_etm__print_auxtrace_info(__u64 *val, int num)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002433{
2434 int i, j, cpu = 0;
2435
2436 for (i = 0; i < CS_HEADER_VERSION_0_MAX; i++)
2437 fprintf(stdout, cs_etm_global_header_fmts[i], val[i]);
2438
2439 for (i = CS_HEADER_VERSION_0_MAX; cpu < num; cpu++) {
2440 if (val[i] == __perf_cs_etmv3_magic)
2441 for (j = 0; j < CS_ETM_PRIV_MAX; j++, i++)
2442 fprintf(stdout, cs_etm_priv_fmts[j], val[i]);
2443 else if (val[i] == __perf_cs_etmv4_magic)
2444 for (j = 0; j < CS_ETMV4_PRIV_MAX; j++, i++)
2445 fprintf(stdout, cs_etmv4_priv_fmts[j], val[i]);
2446 else
2447 /* failure.. return */
2448 return;
2449 }
2450}
2451
2452int cs_etm__process_auxtrace_info(union perf_event *event,
2453 struct perf_session *session)
2454{
David Brazdil0f672f62019-12-10 10:32:29 +00002455 struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002456 struct cs_etm_auxtrace *etm = NULL;
2457 struct int_node *inode;
2458 unsigned int pmu_type;
2459 int event_header_size = sizeof(struct perf_event_header);
2460 int info_header_size;
2461 int total_size = auxtrace_info->header.size;
2462 int priv_size = 0;
2463 int num_cpu;
2464 int err = 0, idx = -1;
2465 int i, j, k;
2466 u64 *ptr, *hdr = NULL;
2467 u64 **metadata = NULL;
2468
2469 /*
2470 * sizeof(auxtrace_info_event::type) +
2471 * sizeof(auxtrace_info_event::reserved) == 8
2472 */
2473 info_header_size = 8;
2474
2475 if (total_size < (event_header_size + info_header_size))
2476 return -EINVAL;
2477
2478 priv_size = total_size - event_header_size - info_header_size;
2479
2480 /* First the global part */
2481 ptr = (u64 *) auxtrace_info->priv;
2482
2483 /* Look for version '0' of the header */
2484 if (ptr[0] != 0)
2485 return -EINVAL;
2486
2487 hdr = zalloc(sizeof(*hdr) * CS_HEADER_VERSION_0_MAX);
2488 if (!hdr)
2489 return -ENOMEM;
2490
2491 /* Extract header information - see cs-etm.h for format */
2492 for (i = 0; i < CS_HEADER_VERSION_0_MAX; i++)
2493 hdr[i] = ptr[i];
2494 num_cpu = hdr[CS_PMU_TYPE_CPUS] & 0xffffffff;
2495 pmu_type = (unsigned int) ((hdr[CS_PMU_TYPE_CPUS] >> 32) &
2496 0xffffffff);
2497
2498 /*
David Brazdil0f672f62019-12-10 10:32:29 +00002499 * Create an RB tree for traceID-metadata tuple. Since the conversion
2500 * has to be made for each packet that gets decoded, optimizing access
2501 * in anything other than a sequential array is worth doing.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002502 */
2503 traceid_list = intlist__new(NULL);
2504 if (!traceid_list) {
2505 err = -ENOMEM;
2506 goto err_free_hdr;
2507 }
2508
2509 metadata = zalloc(sizeof(*metadata) * num_cpu);
2510 if (!metadata) {
2511 err = -ENOMEM;
2512 goto err_free_traceid_list;
2513 }
2514
2515 /*
2516 * The metadata is stored in the auxtrace_info section and encodes
2517 * the configuration of the ARM embedded trace macrocell which is
2518 * required by the trace decoder to properly decode the trace due
2519 * to its highly compressed nature.
2520 */
2521 for (j = 0; j < num_cpu; j++) {
2522 if (ptr[i] == __perf_cs_etmv3_magic) {
2523 metadata[j] = zalloc(sizeof(*metadata[j]) *
2524 CS_ETM_PRIV_MAX);
2525 if (!metadata[j]) {
2526 err = -ENOMEM;
2527 goto err_free_metadata;
2528 }
2529 for (k = 0; k < CS_ETM_PRIV_MAX; k++)
2530 metadata[j][k] = ptr[i + k];
2531
2532 /* The traceID is our handle */
2533 idx = metadata[j][CS_ETM_ETMTRACEIDR];
2534 i += CS_ETM_PRIV_MAX;
2535 } else if (ptr[i] == __perf_cs_etmv4_magic) {
2536 metadata[j] = zalloc(sizeof(*metadata[j]) *
2537 CS_ETMV4_PRIV_MAX);
2538 if (!metadata[j]) {
2539 err = -ENOMEM;
2540 goto err_free_metadata;
2541 }
2542 for (k = 0; k < CS_ETMV4_PRIV_MAX; k++)
2543 metadata[j][k] = ptr[i + k];
2544
2545 /* The traceID is our handle */
2546 idx = metadata[j][CS_ETMV4_TRCTRACEIDR];
2547 i += CS_ETMV4_PRIV_MAX;
2548 }
2549
2550 /* Get an RB node for this CPU */
2551 inode = intlist__findnew(traceid_list, idx);
2552
2553 /* Something went wrong, no need to continue */
2554 if (!inode) {
David Brazdil0f672f62019-12-10 10:32:29 +00002555 err = -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002556 goto err_free_metadata;
2557 }
2558
2559 /*
2560 * The node for that CPU should not be taken.
2561 * Back out if that's the case.
2562 */
2563 if (inode->priv) {
2564 err = -EINVAL;
2565 goto err_free_metadata;
2566 }
David Brazdil0f672f62019-12-10 10:32:29 +00002567 /* All good, associate the traceID with the metadata pointer */
2568 inode->priv = metadata[j];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002569 }
2570
2571 /*
2572 * Each of CS_HEADER_VERSION_0_MAX, CS_ETM_PRIV_MAX and
2573 * CS_ETMV4_PRIV_MAX mark how many double words are in the
2574 * global metadata, and each cpu's metadata respectively.
2575 * The following tests if the correct number of double words was
2576 * present in the auxtrace info section.
2577 */
2578 if (i * 8 != priv_size) {
2579 err = -EINVAL;
2580 goto err_free_metadata;
2581 }
2582
2583 etm = zalloc(sizeof(*etm));
2584
2585 if (!etm) {
2586 err = -ENOMEM;
2587 goto err_free_metadata;
2588 }
2589
2590 err = auxtrace_queues__init(&etm->queues);
2591 if (err)
2592 goto err_free_etm;
2593
2594 etm->session = session;
2595 etm->machine = &session->machines.host;
2596
2597 etm->num_cpu = num_cpu;
2598 etm->pmu_type = pmu_type;
2599 etm->snapshot_mode = (hdr[CS_ETM_SNAPSHOT] != 0);
2600 etm->metadata = metadata;
2601 etm->auxtrace_type = auxtrace_info->type;
2602 etm->timeless_decoding = cs_etm__is_timeless_decoding(etm);
2603
2604 etm->auxtrace.process_event = cs_etm__process_event;
2605 etm->auxtrace.process_auxtrace_event = cs_etm__process_auxtrace_event;
2606 etm->auxtrace.flush_events = cs_etm__flush_events;
2607 etm->auxtrace.free_events = cs_etm__free_events;
2608 etm->auxtrace.free = cs_etm__free;
2609 session->auxtrace = &etm->auxtrace;
2610
2611 etm->unknown_thread = thread__new(999999999, 999999999);
David Brazdil0f672f62019-12-10 10:32:29 +00002612 if (!etm->unknown_thread) {
2613 err = -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002614 goto err_free_queues;
David Brazdil0f672f62019-12-10 10:32:29 +00002615 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002616
2617 /*
2618 * Initialize list node so that at thread__zput() we can avoid
2619 * segmentation fault at list_del_init().
2620 */
2621 INIT_LIST_HEAD(&etm->unknown_thread->node);
2622
2623 err = thread__set_comm(etm->unknown_thread, "unknown", 0);
2624 if (err)
2625 goto err_delete_thread;
2626
David Brazdil0f672f62019-12-10 10:32:29 +00002627 if (thread__init_map_groups(etm->unknown_thread, etm->machine)) {
2628 err = -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002629 goto err_delete_thread;
David Brazdil0f672f62019-12-10 10:32:29 +00002630 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002631
2632 if (dump_trace) {
2633 cs_etm__print_auxtrace_info(auxtrace_info->priv, num_cpu);
2634 return 0;
2635 }
2636
David Brazdil0f672f62019-12-10 10:32:29 +00002637 if (session->itrace_synth_opts->set) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002638 etm->synth_opts = *session->itrace_synth_opts;
2639 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00002640 itrace_synth_opts__set_default(&etm->synth_opts,
2641 session->itrace_synth_opts->default_no_sample);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002642 etm->synth_opts.callchain = false;
2643 }
2644
2645 err = cs_etm__synth_events(etm, session);
2646 if (err)
2647 goto err_delete_thread;
2648
2649 err = auxtrace_queues__process_index(&etm->queues, session);
2650 if (err)
2651 goto err_delete_thread;
2652
2653 etm->data_queued = etm->queues.populated;
2654
2655 return 0;
2656
2657err_delete_thread:
2658 thread__zput(etm->unknown_thread);
2659err_free_queues:
2660 auxtrace_queues__free(&etm->queues);
2661 session->auxtrace = NULL;
2662err_free_etm:
2663 zfree(&etm);
2664err_free_metadata:
2665 /* No need to check @metadata[j], free(NULL) is supported */
2666 for (j = 0; j < num_cpu; j++)
David Brazdil0f672f62019-12-10 10:32:29 +00002667 zfree(&metadata[j]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002668 zfree(&metadata);
2669err_free_traceid_list:
2670 intlist__delete(traceid_list);
2671err_free_hdr:
2672 zfree(&hdr);
2673
David Brazdil0f672f62019-12-10 10:32:29 +00002674 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002675}