0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/bitops.h>
0010 #include <linux/coresight-pmu.h>
0011 #include <linux/err.h>
0012 #include <linux/kernel.h>
0013 #include <linux/log2.h>
0014 #include <linux/types.h>
0015 #include <linux/zalloc.h>
0016
0017 #include <opencsd/ocsd_if_types.h>
0018 #include <stdlib.h>
0019
0020 #include "auxtrace.h"
0021 #include "color.h"
0022 #include "cs-etm.h"
0023 #include "cs-etm-decoder/cs-etm-decoder.h"
0024 #include "debug.h"
0025 #include "dso.h"
0026 #include "evlist.h"
0027 #include "intlist.h"
0028 #include "machine.h"
0029 #include "map.h"
0030 #include "perf.h"
0031 #include "session.h"
0032 #include "map_symbol.h"
0033 #include "branch.h"
0034 #include "symbol.h"
0035 #include "tool.h"
0036 #include "thread.h"
0037 #include "thread-stack.h"
0038 #include <tools/libc_compat.h>
0039 #include "util/synthetic-events.h"
0040
0041 struct cs_etm_auxtrace {
0042 struct auxtrace auxtrace;
0043 struct auxtrace_queues queues;
0044 struct auxtrace_heap heap;
0045 struct itrace_synth_opts synth_opts;
0046 struct perf_session *session;
0047 struct machine *machine;
0048 struct thread *unknown_thread;
0049
0050 u8 timeless_decoding;
0051 u8 snapshot_mode;
0052 u8 data_queued;
0053
0054 int num_cpu;
0055 u64 latest_kernel_timestamp;
0056 u32 auxtrace_type;
0057 u64 branches_sample_type;
0058 u64 branches_id;
0059 u64 instructions_sample_type;
0060 u64 instructions_sample_period;
0061 u64 instructions_id;
0062 u64 **metadata;
0063 unsigned int pmu_type;
0064 };
0065
0066 struct cs_etm_traceid_queue {
0067 u8 trace_chan_id;
0068 pid_t pid, tid;
0069 u64 period_instructions;
0070 size_t last_branch_pos;
0071 union perf_event *event_buf;
0072 struct thread *thread;
0073 struct branch_stack *last_branch;
0074 struct branch_stack *last_branch_rb;
0075 struct cs_etm_packet *prev_packet;
0076 struct cs_etm_packet *packet;
0077 struct cs_etm_packet_queue packet_queue;
0078 };
0079
0080 struct cs_etm_queue {
0081 struct cs_etm_auxtrace *etm;
0082 struct cs_etm_decoder *decoder;
0083 struct auxtrace_buffer *buffer;
0084 unsigned int queue_nr;
0085 u8 pending_timestamp_chan_id;
0086 u64 offset;
0087 const unsigned char *buf;
0088 size_t buf_len, buf_used;
0089
0090 struct intlist *traceid_queues_list;
0091 struct cs_etm_traceid_queue **traceid_queues;
0092 };
0093
0094
0095 static struct intlist *traceid_list;
0096
0097 static int cs_etm__process_queues(struct cs_etm_auxtrace *etm);
0098 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
0099 pid_t tid);
0100 static int cs_etm__get_data_block(struct cs_etm_queue *etmq);
0101 static int cs_etm__decode_data_block(struct cs_etm_queue *etmq);
0102
0103
0104 #define ETMIDR_PTM_VERSION 0x00000300
0105
0106
0107
0108
0109
0110
0111
0112 #define TO_CS_QUEUE_NR(queue_nr, trace_chan_id) \
0113 (queue_nr << 16 | trace_chan_id)
0114 #define TO_QUEUE_NR(cs_queue_nr) (cs_queue_nr >> 16)
0115 #define TO_TRACE_CHAN_ID(cs_queue_nr) (cs_queue_nr & 0x0000ffff)
0116
0117 static u32 cs_etm__get_v7_protocol_version(u32 etmidr)
0118 {
0119 etmidr &= ETMIDR_PTM_VERSION;
0120
0121 if (etmidr == ETMIDR_PTM_VERSION)
0122 return CS_ETM_PROTO_PTM;
0123
0124 return CS_ETM_PROTO_ETMV3;
0125 }
0126
0127 static int cs_etm__get_magic(u8 trace_chan_id, u64 *magic)
0128 {
0129 struct int_node *inode;
0130 u64 *metadata;
0131
0132 inode = intlist__find(traceid_list, trace_chan_id);
0133 if (!inode)
0134 return -EINVAL;
0135
0136 metadata = inode->priv;
0137 *magic = metadata[CS_ETM_MAGIC];
0138 return 0;
0139 }
0140
0141 int cs_etm__get_cpu(u8 trace_chan_id, int *cpu)
0142 {
0143 struct int_node *inode;
0144 u64 *metadata;
0145
0146 inode = intlist__find(traceid_list, trace_chan_id);
0147 if (!inode)
0148 return -EINVAL;
0149
0150 metadata = inode->priv;
0151 *cpu = (int)metadata[CS_ETM_CPU];
0152 return 0;
0153 }
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167 int cs_etm__get_pid_fmt(u8 trace_chan_id, u64 *pid_fmt)
0168 {
0169 struct int_node *inode;
0170 u64 *metadata, val;
0171
0172 inode = intlist__find(traceid_list, trace_chan_id);
0173 if (!inode)
0174 return -EINVAL;
0175
0176 metadata = inode->priv;
0177
0178 if (metadata[CS_ETM_MAGIC] == __perf_cs_etmv3_magic) {
0179 val = metadata[CS_ETM_ETMCR];
0180
0181 if (val & BIT(ETM_OPT_CTXTID))
0182 *pid_fmt = BIT(ETM_OPT_CTXTID);
0183 } else {
0184 val = metadata[CS_ETMV4_TRCCONFIGR];
0185
0186 if (val & (BIT(ETM4_CFG_BIT_VMID) | BIT(ETM4_CFG_BIT_VMID_OPT)))
0187 *pid_fmt = BIT(ETM_OPT_CTXTID2);
0188
0189 else if (val & BIT(ETM4_CFG_BIT_CTXTID))
0190 *pid_fmt = BIT(ETM_OPT_CTXTID);
0191 }
0192
0193 return 0;
0194 }
0195
0196 void cs_etm__etmq_set_traceid_queue_timestamp(struct cs_etm_queue *etmq,
0197 u8 trace_chan_id)
0198 {
0199
0200
0201
0202
0203
0204
0205
0206 etmq->pending_timestamp_chan_id = trace_chan_id;
0207 }
0208
0209 static u64 cs_etm__etmq_get_timestamp(struct cs_etm_queue *etmq,
0210 u8 *trace_chan_id)
0211 {
0212 struct cs_etm_packet_queue *packet_queue;
0213
0214 if (!etmq->pending_timestamp_chan_id)
0215 return 0;
0216
0217 if (trace_chan_id)
0218 *trace_chan_id = etmq->pending_timestamp_chan_id;
0219
0220 packet_queue = cs_etm__etmq_get_packet_queue(etmq,
0221 etmq->pending_timestamp_chan_id);
0222 if (!packet_queue)
0223 return 0;
0224
0225
0226 etmq->pending_timestamp_chan_id = 0;
0227
0228
0229 return packet_queue->cs_timestamp;
0230 }
0231
0232 static void cs_etm__clear_packet_queue(struct cs_etm_packet_queue *queue)
0233 {
0234 int i;
0235
0236 queue->head = 0;
0237 queue->tail = 0;
0238 queue->packet_count = 0;
0239 for (i = 0; i < CS_ETM_PACKET_MAX_BUFFER; i++) {
0240 queue->packet_buffer[i].isa = CS_ETM_ISA_UNKNOWN;
0241 queue->packet_buffer[i].start_addr = CS_ETM_INVAL_ADDR;
0242 queue->packet_buffer[i].end_addr = CS_ETM_INVAL_ADDR;
0243 queue->packet_buffer[i].instr_count = 0;
0244 queue->packet_buffer[i].last_instr_taken_branch = false;
0245 queue->packet_buffer[i].last_instr_size = 0;
0246 queue->packet_buffer[i].last_instr_type = 0;
0247 queue->packet_buffer[i].last_instr_subtype = 0;
0248 queue->packet_buffer[i].last_instr_cond = 0;
0249 queue->packet_buffer[i].flags = 0;
0250 queue->packet_buffer[i].exception_number = UINT32_MAX;
0251 queue->packet_buffer[i].trace_chan_id = UINT8_MAX;
0252 queue->packet_buffer[i].cpu = INT_MIN;
0253 }
0254 }
0255
0256 static void cs_etm__clear_all_packet_queues(struct cs_etm_queue *etmq)
0257 {
0258 int idx;
0259 struct int_node *inode;
0260 struct cs_etm_traceid_queue *tidq;
0261 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
0262
0263 intlist__for_each_entry(inode, traceid_queues_list) {
0264 idx = (int)(intptr_t)inode->priv;
0265 tidq = etmq->traceid_queues[idx];
0266 cs_etm__clear_packet_queue(&tidq->packet_queue);
0267 }
0268 }
0269
0270 static int cs_etm__init_traceid_queue(struct cs_etm_queue *etmq,
0271 struct cs_etm_traceid_queue *tidq,
0272 u8 trace_chan_id)
0273 {
0274 int rc = -ENOMEM;
0275 struct auxtrace_queue *queue;
0276 struct cs_etm_auxtrace *etm = etmq->etm;
0277
0278 cs_etm__clear_packet_queue(&tidq->packet_queue);
0279
0280 queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
0281 tidq->tid = queue->tid;
0282 tidq->pid = -1;
0283 tidq->trace_chan_id = trace_chan_id;
0284
0285 tidq->packet = zalloc(sizeof(struct cs_etm_packet));
0286 if (!tidq->packet)
0287 goto out;
0288
0289 tidq->prev_packet = zalloc(sizeof(struct cs_etm_packet));
0290 if (!tidq->prev_packet)
0291 goto out_free;
0292
0293 if (etm->synth_opts.last_branch) {
0294 size_t sz = sizeof(struct branch_stack);
0295
0296 sz += etm->synth_opts.last_branch_sz *
0297 sizeof(struct branch_entry);
0298 tidq->last_branch = zalloc(sz);
0299 if (!tidq->last_branch)
0300 goto out_free;
0301 tidq->last_branch_rb = zalloc(sz);
0302 if (!tidq->last_branch_rb)
0303 goto out_free;
0304 }
0305
0306 tidq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
0307 if (!tidq->event_buf)
0308 goto out_free;
0309
0310 return 0;
0311
0312 out_free:
0313 zfree(&tidq->last_branch_rb);
0314 zfree(&tidq->last_branch);
0315 zfree(&tidq->prev_packet);
0316 zfree(&tidq->packet);
0317 out:
0318 return rc;
0319 }
0320
0321 static struct cs_etm_traceid_queue
0322 *cs_etm__etmq_get_traceid_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
0323 {
0324 int idx;
0325 struct int_node *inode;
0326 struct intlist *traceid_queues_list;
0327 struct cs_etm_traceid_queue *tidq, **traceid_queues;
0328 struct cs_etm_auxtrace *etm = etmq->etm;
0329
0330 if (etm->timeless_decoding)
0331 trace_chan_id = CS_ETM_PER_THREAD_TRACEID;
0332
0333 traceid_queues_list = etmq->traceid_queues_list;
0334
0335
0336
0337
0338
0339 inode = intlist__find(traceid_queues_list, trace_chan_id);
0340 if (inode) {
0341 idx = (int)(intptr_t)inode->priv;
0342 return etmq->traceid_queues[idx];
0343 }
0344
0345
0346 tidq = malloc(sizeof(*tidq));
0347 if (!tidq)
0348 return NULL;
0349
0350 memset(tidq, 0, sizeof(*tidq));
0351
0352
0353 idx = intlist__nr_entries(traceid_queues_list);
0354
0355 inode = intlist__findnew(traceid_queues_list, trace_chan_id);
0356 if (!inode)
0357 goto out_free;
0358
0359
0360 inode->priv = (void *)(intptr_t)idx;
0361
0362 if (cs_etm__init_traceid_queue(etmq, tidq, trace_chan_id))
0363 goto out_free;
0364
0365
0366 traceid_queues = etmq->traceid_queues;
0367 traceid_queues = reallocarray(traceid_queues,
0368 idx + 1,
0369 sizeof(*traceid_queues));
0370
0371
0372
0373
0374
0375 if (!traceid_queues)
0376 goto out_free;
0377
0378 traceid_queues[idx] = tidq;
0379 etmq->traceid_queues = traceid_queues;
0380
0381 return etmq->traceid_queues[idx];
0382
0383 out_free:
0384
0385
0386
0387
0388 intlist__remove(traceid_queues_list, inode);
0389 free(tidq);
0390
0391 return NULL;
0392 }
0393
0394 struct cs_etm_packet_queue
0395 *cs_etm__etmq_get_packet_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
0396 {
0397 struct cs_etm_traceid_queue *tidq;
0398
0399 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
0400 if (tidq)
0401 return &tidq->packet_queue;
0402
0403 return NULL;
0404 }
0405
0406 static void cs_etm__packet_swap(struct cs_etm_auxtrace *etm,
0407 struct cs_etm_traceid_queue *tidq)
0408 {
0409 struct cs_etm_packet *tmp;
0410
0411 if (etm->synth_opts.branches || etm->synth_opts.last_branch ||
0412 etm->synth_opts.instructions) {
0413
0414
0415
0416
0417 tmp = tidq->packet;
0418 tidq->packet = tidq->prev_packet;
0419 tidq->prev_packet = tmp;
0420 }
0421 }
0422
0423 static void cs_etm__packet_dump(const char *pkt_string)
0424 {
0425 const char *color = PERF_COLOR_BLUE;
0426 int len = strlen(pkt_string);
0427
0428 if (len && (pkt_string[len-1] == '\n'))
0429 color_fprintf(stdout, color, " %s", pkt_string);
0430 else
0431 color_fprintf(stdout, color, " %s\n", pkt_string);
0432
0433 fflush(stdout);
0434 }
0435
0436 static void cs_etm__set_trace_param_etmv3(struct cs_etm_trace_params *t_params,
0437 struct cs_etm_auxtrace *etm, int idx,
0438 u32 etmidr)
0439 {
0440 u64 **metadata = etm->metadata;
0441
0442 t_params[idx].protocol = cs_etm__get_v7_protocol_version(etmidr);
0443 t_params[idx].etmv3.reg_ctrl = metadata[idx][CS_ETM_ETMCR];
0444 t_params[idx].etmv3.reg_trc_id = metadata[idx][CS_ETM_ETMTRACEIDR];
0445 }
0446
0447 static void cs_etm__set_trace_param_etmv4(struct cs_etm_trace_params *t_params,
0448 struct cs_etm_auxtrace *etm, int idx)
0449 {
0450 u64 **metadata = etm->metadata;
0451
0452 t_params[idx].protocol = CS_ETM_PROTO_ETMV4i;
0453 t_params[idx].etmv4.reg_idr0 = metadata[idx][CS_ETMV4_TRCIDR0];
0454 t_params[idx].etmv4.reg_idr1 = metadata[idx][CS_ETMV4_TRCIDR1];
0455 t_params[idx].etmv4.reg_idr2 = metadata[idx][CS_ETMV4_TRCIDR2];
0456 t_params[idx].etmv4.reg_idr8 = metadata[idx][CS_ETMV4_TRCIDR8];
0457 t_params[idx].etmv4.reg_configr = metadata[idx][CS_ETMV4_TRCCONFIGR];
0458 t_params[idx].etmv4.reg_traceidr = metadata[idx][CS_ETMV4_TRCTRACEIDR];
0459 }
0460
0461 static void cs_etm__set_trace_param_ete(struct cs_etm_trace_params *t_params,
0462 struct cs_etm_auxtrace *etm, int idx)
0463 {
0464 u64 **metadata = etm->metadata;
0465
0466 t_params[idx].protocol = CS_ETM_PROTO_ETE;
0467 t_params[idx].ete.reg_idr0 = metadata[idx][CS_ETMV4_TRCIDR0];
0468 t_params[idx].ete.reg_idr1 = metadata[idx][CS_ETMV4_TRCIDR1];
0469 t_params[idx].ete.reg_idr2 = metadata[idx][CS_ETMV4_TRCIDR2];
0470 t_params[idx].ete.reg_idr8 = metadata[idx][CS_ETMV4_TRCIDR8];
0471 t_params[idx].ete.reg_configr = metadata[idx][CS_ETMV4_TRCCONFIGR];
0472 t_params[idx].ete.reg_traceidr = metadata[idx][CS_ETMV4_TRCTRACEIDR];
0473 t_params[idx].ete.reg_devarch = metadata[idx][CS_ETE_TRCDEVARCH];
0474 }
0475
0476 static int cs_etm__init_trace_params(struct cs_etm_trace_params *t_params,
0477 struct cs_etm_auxtrace *etm,
0478 int decoders)
0479 {
0480 int i;
0481 u32 etmidr;
0482 u64 architecture;
0483
0484 for (i = 0; i < decoders; i++) {
0485 architecture = etm->metadata[i][CS_ETM_MAGIC];
0486
0487 switch (architecture) {
0488 case __perf_cs_etmv3_magic:
0489 etmidr = etm->metadata[i][CS_ETM_ETMIDR];
0490 cs_etm__set_trace_param_etmv3(t_params, etm, i, etmidr);
0491 break;
0492 case __perf_cs_etmv4_magic:
0493 cs_etm__set_trace_param_etmv4(t_params, etm, i);
0494 break;
0495 case __perf_cs_ete_magic:
0496 cs_etm__set_trace_param_ete(t_params, etm, i);
0497 break;
0498 default:
0499 return -EINVAL;
0500 }
0501 }
0502
0503 return 0;
0504 }
0505
0506 static int cs_etm__init_decoder_params(struct cs_etm_decoder_params *d_params,
0507 struct cs_etm_queue *etmq,
0508 enum cs_etm_decoder_operation mode,
0509 bool formatted)
0510 {
0511 int ret = -EINVAL;
0512
0513 if (!(mode < CS_ETM_OPERATION_MAX))
0514 goto out;
0515
0516 d_params->packet_printer = cs_etm__packet_dump;
0517 d_params->operation = mode;
0518 d_params->data = etmq;
0519 d_params->formatted = formatted;
0520 d_params->fsyncs = false;
0521 d_params->hsyncs = false;
0522 d_params->frame_aligned = true;
0523
0524 ret = 0;
0525 out:
0526 return ret;
0527 }
0528
0529 static void cs_etm__dump_event(struct cs_etm_queue *etmq,
0530 struct auxtrace_buffer *buffer)
0531 {
0532 int ret;
0533 const char *color = PERF_COLOR_BLUE;
0534 size_t buffer_used = 0;
0535
0536 fprintf(stdout, "\n");
0537 color_fprintf(stdout, color,
0538 ". ... CoreSight %s Trace data: size %#zx bytes\n",
0539 cs_etm_decoder__get_name(etmq->decoder), buffer->size);
0540
0541 do {
0542 size_t consumed;
0543
0544 ret = cs_etm_decoder__process_data_block(
0545 etmq->decoder, buffer->offset,
0546 &((u8 *)buffer->data)[buffer_used],
0547 buffer->size - buffer_used, &consumed);
0548 if (ret)
0549 break;
0550
0551 buffer_used += consumed;
0552 } while (buffer_used < buffer->size);
0553
0554 cs_etm_decoder__reset(etmq->decoder);
0555 }
0556
0557 static int cs_etm__flush_events(struct perf_session *session,
0558 struct perf_tool *tool)
0559 {
0560 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
0561 struct cs_etm_auxtrace,
0562 auxtrace);
0563 if (dump_trace)
0564 return 0;
0565
0566 if (!tool->ordered_events)
0567 return -EINVAL;
0568
0569 if (etm->timeless_decoding)
0570 return cs_etm__process_timeless_queues(etm, -1);
0571
0572 return cs_etm__process_queues(etm);
0573 }
0574
0575 static void cs_etm__free_traceid_queues(struct cs_etm_queue *etmq)
0576 {
0577 int idx;
0578 uintptr_t priv;
0579 struct int_node *inode, *tmp;
0580 struct cs_etm_traceid_queue *tidq;
0581 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
0582
0583 intlist__for_each_entry_safe(inode, tmp, traceid_queues_list) {
0584 priv = (uintptr_t)inode->priv;
0585 idx = priv;
0586
0587
0588 tidq = etmq->traceid_queues[idx];
0589 thread__zput(tidq->thread);
0590 zfree(&tidq->event_buf);
0591 zfree(&tidq->last_branch);
0592 zfree(&tidq->last_branch_rb);
0593 zfree(&tidq->prev_packet);
0594 zfree(&tidq->packet);
0595 zfree(&tidq);
0596
0597
0598
0599
0600
0601 intlist__remove(traceid_queues_list, inode);
0602 }
0603
0604
0605 intlist__delete(traceid_queues_list);
0606 etmq->traceid_queues_list = NULL;
0607
0608
0609 zfree(&etmq->traceid_queues);
0610 }
0611
0612 static void cs_etm__free_queue(void *priv)
0613 {
0614 struct cs_etm_queue *etmq = priv;
0615
0616 if (!etmq)
0617 return;
0618
0619 cs_etm_decoder__free(etmq->decoder);
0620 cs_etm__free_traceid_queues(etmq);
0621 free(etmq);
0622 }
0623
0624 static void cs_etm__free_events(struct perf_session *session)
0625 {
0626 unsigned int i;
0627 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
0628 struct cs_etm_auxtrace,
0629 auxtrace);
0630 struct auxtrace_queues *queues = &aux->queues;
0631
0632 for (i = 0; i < queues->nr_queues; i++) {
0633 cs_etm__free_queue(queues->queue_array[i].priv);
0634 queues->queue_array[i].priv = NULL;
0635 }
0636
0637 auxtrace_queues__free(queues);
0638 }
0639
0640 static void cs_etm__free(struct perf_session *session)
0641 {
0642 int i;
0643 struct int_node *inode, *tmp;
0644 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
0645 struct cs_etm_auxtrace,
0646 auxtrace);
0647 cs_etm__free_events(session);
0648 session->auxtrace = NULL;
0649
0650
0651 intlist__for_each_entry_safe(inode, tmp, traceid_list)
0652 intlist__remove(traceid_list, inode);
0653
0654 intlist__delete(traceid_list);
0655
0656 for (i = 0; i < aux->num_cpu; i++)
0657 zfree(&aux->metadata[i]);
0658
0659 thread__zput(aux->unknown_thread);
0660 zfree(&aux->metadata);
0661 zfree(&aux);
0662 }
0663
0664 static bool cs_etm__evsel_is_auxtrace(struct perf_session *session,
0665 struct evsel *evsel)
0666 {
0667 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
0668 struct cs_etm_auxtrace,
0669 auxtrace);
0670
0671 return evsel->core.attr.type == aux->pmu_type;
0672 }
0673
0674 static u8 cs_etm__cpu_mode(struct cs_etm_queue *etmq, u64 address)
0675 {
0676 struct machine *machine;
0677
0678 machine = etmq->etm->machine;
0679
0680 if (address >= machine__kernel_start(machine)) {
0681 if (machine__is_host(machine))
0682 return PERF_RECORD_MISC_KERNEL;
0683 else
0684 return PERF_RECORD_MISC_GUEST_KERNEL;
0685 } else {
0686 if (machine__is_host(machine))
0687 return PERF_RECORD_MISC_USER;
0688 else if (perf_guest)
0689 return PERF_RECORD_MISC_GUEST_USER;
0690 else
0691 return PERF_RECORD_MISC_HYPERVISOR;
0692 }
0693 }
0694
0695 static u32 cs_etm__mem_access(struct cs_etm_queue *etmq, u8 trace_chan_id,
0696 u64 address, size_t size, u8 *buffer)
0697 {
0698 u8 cpumode;
0699 u64 offset;
0700 int len;
0701 struct thread *thread;
0702 struct machine *machine;
0703 struct addr_location al;
0704 struct cs_etm_traceid_queue *tidq;
0705
0706 if (!etmq)
0707 return 0;
0708
0709 machine = etmq->etm->machine;
0710 cpumode = cs_etm__cpu_mode(etmq, address);
0711 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
0712 if (!tidq)
0713 return 0;
0714
0715 thread = tidq->thread;
0716 if (!thread) {
0717 if (cpumode != PERF_RECORD_MISC_KERNEL)
0718 return 0;
0719 thread = etmq->etm->unknown_thread;
0720 }
0721
0722 if (!thread__find_map(thread, cpumode, address, &al) || !al.map->dso)
0723 return 0;
0724
0725 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
0726 dso__data_status_seen(al.map->dso, DSO_DATA_STATUS_SEEN_ITRACE))
0727 return 0;
0728
0729 offset = al.map->map_ip(al.map, address);
0730
0731 map__load(al.map);
0732
0733 len = dso__data_read_offset(al.map->dso, machine, offset, buffer, size);
0734
0735 if (len <= 0) {
0736 ui__warning_once("CS ETM Trace: Missing DSO. Use 'perf archive' or debuginfod to export data from the traced system.\n"
0737 " Enable CONFIG_PROC_KCORE or use option '-k /path/to/vmlinux' for kernel symbols.\n");
0738 if (!al.map->dso->auxtrace_warned) {
0739 pr_err("CS ETM Trace: Debug data not found for address %#"PRIx64" in %s\n",
0740 address,
0741 al.map->dso->long_name ? al.map->dso->long_name : "Unknown");
0742 al.map->dso->auxtrace_warned = true;
0743 }
0744 return 0;
0745 }
0746
0747 return len;
0748 }
0749
0750 static struct cs_etm_queue *cs_etm__alloc_queue(struct cs_etm_auxtrace *etm,
0751 bool formatted)
0752 {
0753 struct cs_etm_decoder_params d_params;
0754 struct cs_etm_trace_params *t_params = NULL;
0755 struct cs_etm_queue *etmq;
0756
0757
0758
0759
0760 int decoders = formatted ? etm->num_cpu : 1;
0761
0762 etmq = zalloc(sizeof(*etmq));
0763 if (!etmq)
0764 return NULL;
0765
0766 etmq->traceid_queues_list = intlist__new(NULL);
0767 if (!etmq->traceid_queues_list)
0768 goto out_free;
0769
0770
0771 t_params = zalloc(sizeof(*t_params) * decoders);
0772
0773 if (!t_params)
0774 goto out_free;
0775
0776 if (cs_etm__init_trace_params(t_params, etm, decoders))
0777 goto out_free;
0778
0779
0780 if (cs_etm__init_decoder_params(&d_params, etmq,
0781 dump_trace ? CS_ETM_OPERATION_PRINT :
0782 CS_ETM_OPERATION_DECODE,
0783 formatted))
0784 goto out_free;
0785
0786 etmq->decoder = cs_etm_decoder__new(decoders, &d_params,
0787 t_params);
0788
0789 if (!etmq->decoder)
0790 goto out_free;
0791
0792
0793
0794
0795
0796 if (cs_etm_decoder__add_mem_access_cb(etmq->decoder,
0797 0x0L, ((u64) -1L),
0798 cs_etm__mem_access))
0799 goto out_free_decoder;
0800
0801 zfree(&t_params);
0802 return etmq;
0803
0804 out_free_decoder:
0805 cs_etm_decoder__free(etmq->decoder);
0806 out_free:
0807 intlist__delete(etmq->traceid_queues_list);
0808 free(etmq);
0809
0810 return NULL;
0811 }
0812
0813 static int cs_etm__setup_queue(struct cs_etm_auxtrace *etm,
0814 struct auxtrace_queue *queue,
0815 unsigned int queue_nr,
0816 bool formatted)
0817 {
0818 struct cs_etm_queue *etmq = queue->priv;
0819
0820 if (list_empty(&queue->head) || etmq)
0821 return 0;
0822
0823 etmq = cs_etm__alloc_queue(etm, formatted);
0824
0825 if (!etmq)
0826 return -ENOMEM;
0827
0828 queue->priv = etmq;
0829 etmq->etm = etm;
0830 etmq->queue_nr = queue_nr;
0831 etmq->offset = 0;
0832
0833 return 0;
0834 }
0835
0836 static int cs_etm__queue_first_cs_timestamp(struct cs_etm_auxtrace *etm,
0837 struct cs_etm_queue *etmq,
0838 unsigned int queue_nr)
0839 {
0840 int ret = 0;
0841 unsigned int cs_queue_nr;
0842 u8 trace_chan_id;
0843 u64 cs_timestamp;
0844
0845
0846
0847
0848
0849
0850
0851
0852
0853 while (1) {
0854
0855
0856
0857
0858 ret = cs_etm__get_data_block(etmq);
0859 if (ret <= 0)
0860 goto out;
0861
0862
0863
0864
0865
0866
0867 ret = cs_etm__decode_data_block(etmq);
0868 if (ret)
0869 goto out;
0870
0871
0872
0873
0874
0875 cs_timestamp = cs_etm__etmq_get_timestamp(etmq, &trace_chan_id);
0876
0877
0878 if (cs_timestamp)
0879 break;
0880
0881
0882
0883
0884
0885
0886
0887
0888 cs_etm__clear_all_packet_queues(etmq);
0889 }
0890
0891
0892
0893
0894
0895
0896
0897
0898
0899
0900
0901 cs_queue_nr = TO_CS_QUEUE_NR(queue_nr, trace_chan_id);
0902 ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, cs_timestamp);
0903 out:
0904 return ret;
0905 }
0906
0907 static inline
0908 void cs_etm__copy_last_branch_rb(struct cs_etm_queue *etmq,
0909 struct cs_etm_traceid_queue *tidq)
0910 {
0911 struct branch_stack *bs_src = tidq->last_branch_rb;
0912 struct branch_stack *bs_dst = tidq->last_branch;
0913 size_t nr = 0;
0914
0915
0916
0917
0918
0919 bs_dst->nr = bs_src->nr;
0920
0921
0922
0923
0924 if (!bs_src->nr)
0925 return;
0926
0927
0928
0929
0930
0931
0932 nr = etmq->etm->synth_opts.last_branch_sz - tidq->last_branch_pos;
0933 memcpy(&bs_dst->entries[0],
0934 &bs_src->entries[tidq->last_branch_pos],
0935 sizeof(struct branch_entry) * nr);
0936
0937
0938
0939
0940
0941
0942
0943
0944 if (bs_src->nr >= etmq->etm->synth_opts.last_branch_sz) {
0945 memcpy(&bs_dst->entries[nr],
0946 &bs_src->entries[0],
0947 sizeof(struct branch_entry) * tidq->last_branch_pos);
0948 }
0949 }
0950
0951 static inline
0952 void cs_etm__reset_last_branch_rb(struct cs_etm_traceid_queue *tidq)
0953 {
0954 tidq->last_branch_pos = 0;
0955 tidq->last_branch_rb->nr = 0;
0956 }
0957
0958 static inline int cs_etm__t32_instr_size(struct cs_etm_queue *etmq,
0959 u8 trace_chan_id, u64 addr)
0960 {
0961 u8 instrBytes[2];
0962
0963 cs_etm__mem_access(etmq, trace_chan_id, addr,
0964 ARRAY_SIZE(instrBytes), instrBytes);
0965
0966
0967
0968
0969
0970 return ((instrBytes[1] & 0xF8) >= 0xE8) ? 4 : 2;
0971 }
0972
0973 static inline u64 cs_etm__first_executed_instr(struct cs_etm_packet *packet)
0974 {
0975
0976 if (packet->sample_type == CS_ETM_DISCONTINUITY)
0977 return 0;
0978
0979 return packet->start_addr;
0980 }
0981
0982 static inline
0983 u64 cs_etm__last_executed_instr(const struct cs_etm_packet *packet)
0984 {
0985
0986 if (packet->sample_type == CS_ETM_DISCONTINUITY)
0987 return 0;
0988
0989 return packet->end_addr - packet->last_instr_size;
0990 }
0991
0992 static inline u64 cs_etm__instr_addr(struct cs_etm_queue *etmq,
0993 u64 trace_chan_id,
0994 const struct cs_etm_packet *packet,
0995 u64 offset)
0996 {
0997 if (packet->isa == CS_ETM_ISA_T32) {
0998 u64 addr = packet->start_addr;
0999
1000 while (offset) {
1001 addr += cs_etm__t32_instr_size(etmq,
1002 trace_chan_id, addr);
1003 offset--;
1004 }
1005 return addr;
1006 }
1007
1008
1009 return packet->start_addr + offset * 4;
1010 }
1011
1012 static void cs_etm__update_last_branch_rb(struct cs_etm_queue *etmq,
1013 struct cs_etm_traceid_queue *tidq)
1014 {
1015 struct branch_stack *bs = tidq->last_branch_rb;
1016 struct branch_entry *be;
1017
1018
1019
1020
1021
1022
1023
1024 if (!tidq->last_branch_pos)
1025 tidq->last_branch_pos = etmq->etm->synth_opts.last_branch_sz;
1026
1027 tidq->last_branch_pos -= 1;
1028
1029 be = &bs->entries[tidq->last_branch_pos];
1030 be->from = cs_etm__last_executed_instr(tidq->prev_packet);
1031 be->to = cs_etm__first_executed_instr(tidq->packet);
1032
1033 be->flags.mispred = 0;
1034 be->flags.predicted = 1;
1035
1036
1037
1038
1039
1040 if (bs->nr < etmq->etm->synth_opts.last_branch_sz)
1041 bs->nr += 1;
1042 }
1043
1044 static int cs_etm__inject_event(union perf_event *event,
1045 struct perf_sample *sample, u64 type)
1046 {
1047 event->header.size = perf_event__sample_event_size(sample, type, 0);
1048 return perf_event__synthesize_sample(event, type, 0, sample);
1049 }
1050
1051
1052 static int
1053 cs_etm__get_trace(struct cs_etm_queue *etmq)
1054 {
1055 struct auxtrace_buffer *aux_buffer = etmq->buffer;
1056 struct auxtrace_buffer *old_buffer = aux_buffer;
1057 struct auxtrace_queue *queue;
1058
1059 queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
1060
1061 aux_buffer = auxtrace_buffer__next(queue, aux_buffer);
1062
1063
1064 if (!aux_buffer) {
1065 if (old_buffer)
1066 auxtrace_buffer__drop_data(old_buffer);
1067 etmq->buf_len = 0;
1068 return 0;
1069 }
1070
1071 etmq->buffer = aux_buffer;
1072
1073
1074 if (!aux_buffer->data) {
1075
1076 int fd = perf_data__fd(etmq->etm->session->data);
1077
1078 aux_buffer->data = auxtrace_buffer__get_data(aux_buffer, fd);
1079 if (!aux_buffer->data)
1080 return -ENOMEM;
1081 }
1082
1083
1084 if (old_buffer)
1085 auxtrace_buffer__drop_data(old_buffer);
1086
1087 etmq->buf_used = 0;
1088 etmq->buf_len = aux_buffer->size;
1089 etmq->buf = aux_buffer->data;
1090
1091 return etmq->buf_len;
1092 }
1093
1094 static void cs_etm__set_pid_tid_cpu(struct cs_etm_auxtrace *etm,
1095 struct cs_etm_traceid_queue *tidq)
1096 {
1097 if ((!tidq->thread) && (tidq->tid != -1))
1098 tidq->thread = machine__find_thread(etm->machine, -1,
1099 tidq->tid);
1100
1101 if (tidq->thread)
1102 tidq->pid = tidq->thread->pid_;
1103 }
1104
1105 int cs_etm__etmq_set_tid(struct cs_etm_queue *etmq,
1106 pid_t tid, u8 trace_chan_id)
1107 {
1108 int cpu, err = -EINVAL;
1109 struct cs_etm_auxtrace *etm = etmq->etm;
1110 struct cs_etm_traceid_queue *tidq;
1111
1112 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
1113 if (!tidq)
1114 return err;
1115
1116 if (cs_etm__get_cpu(trace_chan_id, &cpu) < 0)
1117 return err;
1118
1119 err = machine__set_current_tid(etm->machine, cpu, tid, tid);
1120 if (err)
1121 return err;
1122
1123 tidq->tid = tid;
1124 thread__zput(tidq->thread);
1125
1126 cs_etm__set_pid_tid_cpu(etm, tidq);
1127 return 0;
1128 }
1129
1130 bool cs_etm__etmq_is_timeless(struct cs_etm_queue *etmq)
1131 {
1132 return !!etmq->etm->timeless_decoding;
1133 }
1134
1135 static void cs_etm__copy_insn(struct cs_etm_queue *etmq,
1136 u64 trace_chan_id,
1137 const struct cs_etm_packet *packet,
1138 struct perf_sample *sample)
1139 {
1140
1141
1142
1143
1144 if (packet->sample_type == CS_ETM_DISCONTINUITY) {
1145 sample->insn_len = 0;
1146 return;
1147 }
1148
1149
1150
1151
1152
1153 if (packet->isa == CS_ETM_ISA_T32)
1154 sample->insn_len = cs_etm__t32_instr_size(etmq, trace_chan_id,
1155 sample->ip);
1156
1157 else
1158 sample->insn_len = 4;
1159
1160 cs_etm__mem_access(etmq, trace_chan_id, sample->ip,
1161 sample->insn_len, (void *)sample->insn);
1162 }
1163
1164 static int cs_etm__synth_instruction_sample(struct cs_etm_queue *etmq,
1165 struct cs_etm_traceid_queue *tidq,
1166 u64 addr, u64 period)
1167 {
1168 int ret = 0;
1169 struct cs_etm_auxtrace *etm = etmq->etm;
1170 union perf_event *event = tidq->event_buf;
1171 struct perf_sample sample = {.ip = 0,};
1172
1173 event->sample.header.type = PERF_RECORD_SAMPLE;
1174 event->sample.header.misc = cs_etm__cpu_mode(etmq, addr);
1175 event->sample.header.size = sizeof(struct perf_event_header);
1176
1177 if (!etm->timeless_decoding)
1178 sample.time = etm->latest_kernel_timestamp;
1179 sample.ip = addr;
1180 sample.pid = tidq->pid;
1181 sample.tid = tidq->tid;
1182 sample.id = etmq->etm->instructions_id;
1183 sample.stream_id = etmq->etm->instructions_id;
1184 sample.period = period;
1185 sample.cpu = tidq->packet->cpu;
1186 sample.flags = tidq->prev_packet->flags;
1187 sample.cpumode = event->sample.header.misc;
1188
1189 cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->packet, &sample);
1190
1191 if (etm->synth_opts.last_branch)
1192 sample.branch_stack = tidq->last_branch;
1193
1194 if (etm->synth_opts.inject) {
1195 ret = cs_etm__inject_event(event, &sample,
1196 etm->instructions_sample_type);
1197 if (ret)
1198 return ret;
1199 }
1200
1201 ret = perf_session__deliver_synth_event(etm->session, event, &sample);
1202
1203 if (ret)
1204 pr_err(
1205 "CS ETM Trace: failed to deliver instruction event, error %d\n",
1206 ret);
1207
1208 return ret;
1209 }
1210
1211
1212
1213
1214
1215 static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq,
1216 struct cs_etm_traceid_queue *tidq)
1217 {
1218 int ret = 0;
1219 struct cs_etm_auxtrace *etm = etmq->etm;
1220 struct perf_sample sample = {.ip = 0,};
1221 union perf_event *event = tidq->event_buf;
1222 struct dummy_branch_stack {
1223 u64 nr;
1224 u64 hw_idx;
1225 struct branch_entry entries;
1226 } dummy_bs;
1227 u64 ip;
1228
1229 ip = cs_etm__last_executed_instr(tidq->prev_packet);
1230
1231 event->sample.header.type = PERF_RECORD_SAMPLE;
1232 event->sample.header.misc = cs_etm__cpu_mode(etmq, ip);
1233 event->sample.header.size = sizeof(struct perf_event_header);
1234
1235 if (!etm->timeless_decoding)
1236 sample.time = etm->latest_kernel_timestamp;
1237 sample.ip = ip;
1238 sample.pid = tidq->pid;
1239 sample.tid = tidq->tid;
1240 sample.addr = cs_etm__first_executed_instr(tidq->packet);
1241 sample.id = etmq->etm->branches_id;
1242 sample.stream_id = etmq->etm->branches_id;
1243 sample.period = 1;
1244 sample.cpu = tidq->packet->cpu;
1245 sample.flags = tidq->prev_packet->flags;
1246 sample.cpumode = event->sample.header.misc;
1247
1248 cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->prev_packet,
1249 &sample);
1250
1251
1252
1253
1254 if (etm->synth_opts.last_branch) {
1255 dummy_bs = (struct dummy_branch_stack){
1256 .nr = 1,
1257 .hw_idx = -1ULL,
1258 .entries = {
1259 .from = sample.ip,
1260 .to = sample.addr,
1261 },
1262 };
1263 sample.branch_stack = (struct branch_stack *)&dummy_bs;
1264 }
1265
1266 if (etm->synth_opts.inject) {
1267 ret = cs_etm__inject_event(event, &sample,
1268 etm->branches_sample_type);
1269 if (ret)
1270 return ret;
1271 }
1272
1273 ret = perf_session__deliver_synth_event(etm->session, event, &sample);
1274
1275 if (ret)
1276 pr_err(
1277 "CS ETM Trace: failed to deliver instruction event, error %d\n",
1278 ret);
1279
1280 return ret;
1281 }
1282
1283 struct cs_etm_synth {
1284 struct perf_tool dummy_tool;
1285 struct perf_session *session;
1286 };
1287
1288 static int cs_etm__event_synth(struct perf_tool *tool,
1289 union perf_event *event,
1290 struct perf_sample *sample __maybe_unused,
1291 struct machine *machine __maybe_unused)
1292 {
1293 struct cs_etm_synth *cs_etm_synth =
1294 container_of(tool, struct cs_etm_synth, dummy_tool);
1295
1296 return perf_session__deliver_synth_event(cs_etm_synth->session,
1297 event, NULL);
1298 }
1299
1300 static int cs_etm__synth_event(struct perf_session *session,
1301 struct perf_event_attr *attr, u64 id)
1302 {
1303 struct cs_etm_synth cs_etm_synth;
1304
1305 memset(&cs_etm_synth, 0, sizeof(struct cs_etm_synth));
1306 cs_etm_synth.session = session;
1307
1308 return perf_event__synthesize_attr(&cs_etm_synth.dummy_tool, attr, 1,
1309 &id, cs_etm__event_synth);
1310 }
1311
1312 static int cs_etm__synth_events(struct cs_etm_auxtrace *etm,
1313 struct perf_session *session)
1314 {
1315 struct evlist *evlist = session->evlist;
1316 struct evsel *evsel;
1317 struct perf_event_attr attr;
1318 bool found = false;
1319 u64 id;
1320 int err;
1321
1322 evlist__for_each_entry(evlist, evsel) {
1323 if (evsel->core.attr.type == etm->pmu_type) {
1324 found = true;
1325 break;
1326 }
1327 }
1328
1329 if (!found) {
1330 pr_debug("No selected events with CoreSight Trace data\n");
1331 return 0;
1332 }
1333
1334 memset(&attr, 0, sizeof(struct perf_event_attr));
1335 attr.size = sizeof(struct perf_event_attr);
1336 attr.type = PERF_TYPE_HARDWARE;
1337 attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
1338 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
1339 PERF_SAMPLE_PERIOD;
1340 if (etm->timeless_decoding)
1341 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
1342 else
1343 attr.sample_type |= PERF_SAMPLE_TIME;
1344
1345 attr.exclude_user = evsel->core.attr.exclude_user;
1346 attr.exclude_kernel = evsel->core.attr.exclude_kernel;
1347 attr.exclude_hv = evsel->core.attr.exclude_hv;
1348 attr.exclude_host = evsel->core.attr.exclude_host;
1349 attr.exclude_guest = evsel->core.attr.exclude_guest;
1350 attr.sample_id_all = evsel->core.attr.sample_id_all;
1351 attr.read_format = evsel->core.attr.read_format;
1352
1353
1354 id = evsel->core.id[0] + 1000000000;
1355
1356 if (!id)
1357 id = 1;
1358
1359 if (etm->synth_opts.branches) {
1360 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
1361 attr.sample_period = 1;
1362 attr.sample_type |= PERF_SAMPLE_ADDR;
1363 err = cs_etm__synth_event(session, &attr, id);
1364 if (err)
1365 return err;
1366 etm->branches_sample_type = attr.sample_type;
1367 etm->branches_id = id;
1368 id += 1;
1369 attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
1370 }
1371
1372 if (etm->synth_opts.last_branch) {
1373 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
1374
1375
1376
1377
1378
1379 attr.branch_sample_type |= PERF_SAMPLE_BRANCH_HW_INDEX;
1380 }
1381
1382 if (etm->synth_opts.instructions) {
1383 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
1384 attr.sample_period = etm->synth_opts.period;
1385 etm->instructions_sample_period = attr.sample_period;
1386 err = cs_etm__synth_event(session, &attr, id);
1387 if (err)
1388 return err;
1389 etm->instructions_sample_type = attr.sample_type;
1390 etm->instructions_id = id;
1391 id += 1;
1392 }
1393
1394 return 0;
1395 }
1396
1397 static int cs_etm__sample(struct cs_etm_queue *etmq,
1398 struct cs_etm_traceid_queue *tidq)
1399 {
1400 struct cs_etm_auxtrace *etm = etmq->etm;
1401 int ret;
1402 u8 trace_chan_id = tidq->trace_chan_id;
1403 u64 instrs_prev;
1404
1405
1406 instrs_prev = tidq->period_instructions;
1407
1408 tidq->period_instructions += tidq->packet->instr_count;
1409
1410
1411
1412
1413
1414 if (etm->synth_opts.last_branch &&
1415 tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1416 tidq->prev_packet->last_instr_taken_branch)
1417 cs_etm__update_last_branch_rb(etmq, tidq);
1418
1419 if (etm->synth_opts.instructions &&
1420 tidq->period_instructions >= etm->instructions_sample_period) {
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473 u64 offset = etm->instructions_sample_period - instrs_prev;
1474 u64 addr;
1475
1476
1477 if (etm->synth_opts.last_branch)
1478 cs_etm__copy_last_branch_rb(etmq, tidq);
1479
1480 while (tidq->period_instructions >=
1481 etm->instructions_sample_period) {
1482
1483
1484
1485
1486
1487
1488 addr = cs_etm__instr_addr(etmq, trace_chan_id,
1489 tidq->packet, offset - 1);
1490 ret = cs_etm__synth_instruction_sample(
1491 etmq, tidq, addr,
1492 etm->instructions_sample_period);
1493 if (ret)
1494 return ret;
1495
1496 offset += etm->instructions_sample_period;
1497 tidq->period_instructions -=
1498 etm->instructions_sample_period;
1499 }
1500 }
1501
1502 if (etm->synth_opts.branches) {
1503 bool generate_sample = false;
1504
1505
1506 if (tidq->prev_packet->sample_type == CS_ETM_DISCONTINUITY)
1507 generate_sample = true;
1508
1509
1510 if (tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1511 tidq->prev_packet->last_instr_taken_branch)
1512 generate_sample = true;
1513
1514 if (generate_sample) {
1515 ret = cs_etm__synth_branch_sample(etmq, tidq);
1516 if (ret)
1517 return ret;
1518 }
1519 }
1520
1521 cs_etm__packet_swap(etm, tidq);
1522
1523 return 0;
1524 }
1525
1526 static int cs_etm__exception(struct cs_etm_traceid_queue *tidq)
1527 {
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539 if (tidq->prev_packet->sample_type == CS_ETM_RANGE)
1540 tidq->prev_packet->last_instr_taken_branch = true;
1541
1542 return 0;
1543 }
1544
1545 static int cs_etm__flush(struct cs_etm_queue *etmq,
1546 struct cs_etm_traceid_queue *tidq)
1547 {
1548 int err = 0;
1549 struct cs_etm_auxtrace *etm = etmq->etm;
1550
1551
1552 if (tidq->prev_packet->sample_type == CS_ETM_EMPTY)
1553 goto swap_packet;
1554
1555 if (etmq->etm->synth_opts.last_branch &&
1556 etmq->etm->synth_opts.instructions &&
1557 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1558 u64 addr;
1559
1560
1561 cs_etm__copy_last_branch_rb(etmq, tidq);
1562
1563
1564
1565
1566
1567
1568
1569
1570 addr = cs_etm__last_executed_instr(tidq->prev_packet);
1571
1572 err = cs_etm__synth_instruction_sample(
1573 etmq, tidq, addr,
1574 tidq->period_instructions);
1575 if (err)
1576 return err;
1577
1578 tidq->period_instructions = 0;
1579
1580 }
1581
1582 if (etm->synth_opts.branches &&
1583 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1584 err = cs_etm__synth_branch_sample(etmq, tidq);
1585 if (err)
1586 return err;
1587 }
1588
1589 swap_packet:
1590 cs_etm__packet_swap(etm, tidq);
1591
1592
1593 if (etm->synth_opts.last_branch)
1594 cs_etm__reset_last_branch_rb(tidq);
1595
1596 return err;
1597 }
1598
1599 static int cs_etm__end_block(struct cs_etm_queue *etmq,
1600 struct cs_etm_traceid_queue *tidq)
1601 {
1602 int err;
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613 if (etmq->etm->synth_opts.last_branch &&
1614 etmq->etm->synth_opts.instructions &&
1615 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1616 u64 addr;
1617
1618
1619 cs_etm__copy_last_branch_rb(etmq, tidq);
1620
1621
1622
1623
1624
1625 addr = cs_etm__last_executed_instr(tidq->prev_packet);
1626
1627 err = cs_etm__synth_instruction_sample(
1628 etmq, tidq, addr,
1629 tidq->period_instructions);
1630 if (err)
1631 return err;
1632
1633 tidq->period_instructions = 0;
1634 }
1635
1636 return 0;
1637 }
1638
1639
1640
1641
1642
1643
1644
1645 static int cs_etm__get_data_block(struct cs_etm_queue *etmq)
1646 {
1647 int ret;
1648
1649 if (!etmq->buf_len) {
1650 ret = cs_etm__get_trace(etmq);
1651 if (ret <= 0)
1652 return ret;
1653
1654
1655
1656
1657 ret = cs_etm_decoder__reset(etmq->decoder);
1658 if (ret)
1659 return ret;
1660 }
1661
1662 return etmq->buf_len;
1663 }
1664
1665 static bool cs_etm__is_svc_instr(struct cs_etm_queue *etmq, u8 trace_chan_id,
1666 struct cs_etm_packet *packet,
1667 u64 end_addr)
1668 {
1669
1670 u16 instr16 = 0;
1671 u32 instr32 = 0;
1672 u64 addr;
1673
1674 switch (packet->isa) {
1675 case CS_ETM_ISA_T32:
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688 addr = end_addr - 2;
1689 cs_etm__mem_access(etmq, trace_chan_id, addr,
1690 sizeof(instr16), (u8 *)&instr16);
1691 if ((instr16 & 0xFF00) == 0xDF00)
1692 return true;
1693
1694 break;
1695 case CS_ETM_ISA_A32:
1696
1697
1698
1699
1700
1701
1702
1703
1704 addr = end_addr - 4;
1705 cs_etm__mem_access(etmq, trace_chan_id, addr,
1706 sizeof(instr32), (u8 *)&instr32);
1707 if ((instr32 & 0x0F000000) == 0x0F000000 &&
1708 (instr32 & 0xF0000000) != 0xF0000000)
1709 return true;
1710
1711 break;
1712 case CS_ETM_ISA_A64:
1713
1714
1715
1716
1717
1718
1719
1720
1721 addr = end_addr - 4;
1722 cs_etm__mem_access(etmq, trace_chan_id, addr,
1723 sizeof(instr32), (u8 *)&instr32);
1724 if ((instr32 & 0xFFE0001F) == 0xd4000001)
1725 return true;
1726
1727 break;
1728 case CS_ETM_ISA_UNKNOWN:
1729 default:
1730 break;
1731 }
1732
1733 return false;
1734 }
1735
1736 static bool cs_etm__is_syscall(struct cs_etm_queue *etmq,
1737 struct cs_etm_traceid_queue *tidq, u64 magic)
1738 {
1739 u8 trace_chan_id = tidq->trace_chan_id;
1740 struct cs_etm_packet *packet = tidq->packet;
1741 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1742
1743 if (magic == __perf_cs_etmv3_magic)
1744 if (packet->exception_number == CS_ETMV3_EXC_SVC)
1745 return true;
1746
1747
1748
1749
1750
1751
1752 if (magic == __perf_cs_etmv4_magic) {
1753 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
1754 cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet,
1755 prev_packet->end_addr))
1756 return true;
1757 }
1758
1759 return false;
1760 }
1761
1762 static bool cs_etm__is_async_exception(struct cs_etm_traceid_queue *tidq,
1763 u64 magic)
1764 {
1765 struct cs_etm_packet *packet = tidq->packet;
1766
1767 if (magic == __perf_cs_etmv3_magic)
1768 if (packet->exception_number == CS_ETMV3_EXC_DEBUG_HALT ||
1769 packet->exception_number == CS_ETMV3_EXC_ASYNC_DATA_ABORT ||
1770 packet->exception_number == CS_ETMV3_EXC_PE_RESET ||
1771 packet->exception_number == CS_ETMV3_EXC_IRQ ||
1772 packet->exception_number == CS_ETMV3_EXC_FIQ)
1773 return true;
1774
1775 if (magic == __perf_cs_etmv4_magic)
1776 if (packet->exception_number == CS_ETMV4_EXC_RESET ||
1777 packet->exception_number == CS_ETMV4_EXC_DEBUG_HALT ||
1778 packet->exception_number == CS_ETMV4_EXC_SYSTEM_ERROR ||
1779 packet->exception_number == CS_ETMV4_EXC_INST_DEBUG ||
1780 packet->exception_number == CS_ETMV4_EXC_DATA_DEBUG ||
1781 packet->exception_number == CS_ETMV4_EXC_IRQ ||
1782 packet->exception_number == CS_ETMV4_EXC_FIQ)
1783 return true;
1784
1785 return false;
1786 }
1787
1788 static bool cs_etm__is_sync_exception(struct cs_etm_queue *etmq,
1789 struct cs_etm_traceid_queue *tidq,
1790 u64 magic)
1791 {
1792 u8 trace_chan_id = tidq->trace_chan_id;
1793 struct cs_etm_packet *packet = tidq->packet;
1794 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1795
1796 if (magic == __perf_cs_etmv3_magic)
1797 if (packet->exception_number == CS_ETMV3_EXC_SMC ||
1798 packet->exception_number == CS_ETMV3_EXC_HYP ||
1799 packet->exception_number == CS_ETMV3_EXC_JAZELLE_THUMBEE ||
1800 packet->exception_number == CS_ETMV3_EXC_UNDEFINED_INSTR ||
1801 packet->exception_number == CS_ETMV3_EXC_PREFETCH_ABORT ||
1802 packet->exception_number == CS_ETMV3_EXC_DATA_FAULT ||
1803 packet->exception_number == CS_ETMV3_EXC_GENERIC)
1804 return true;
1805
1806 if (magic == __perf_cs_etmv4_magic) {
1807 if (packet->exception_number == CS_ETMV4_EXC_TRAP ||
1808 packet->exception_number == CS_ETMV4_EXC_ALIGNMENT ||
1809 packet->exception_number == CS_ETMV4_EXC_INST_FAULT ||
1810 packet->exception_number == CS_ETMV4_EXC_DATA_FAULT)
1811 return true;
1812
1813
1814
1815
1816
1817 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
1818 !cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet,
1819 prev_packet->end_addr))
1820 return true;
1821
1822
1823
1824
1825
1826
1827
1828
1829 if (packet->exception_number > CS_ETMV4_EXC_FIQ &&
1830 packet->exception_number <= CS_ETMV4_EXC_END)
1831 return true;
1832 }
1833
1834 return false;
1835 }
1836
1837 static int cs_etm__set_sample_flags(struct cs_etm_queue *etmq,
1838 struct cs_etm_traceid_queue *tidq)
1839 {
1840 struct cs_etm_packet *packet = tidq->packet;
1841 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1842 u8 trace_chan_id = tidq->trace_chan_id;
1843 u64 magic;
1844 int ret;
1845
1846 switch (packet->sample_type) {
1847 case CS_ETM_RANGE:
1848
1849
1850
1851
1852
1853 if (packet->last_instr_type == OCSD_INSTR_BR &&
1854 packet->last_instr_subtype == OCSD_S_INSTR_NONE) {
1855 packet->flags = PERF_IP_FLAG_BRANCH;
1856
1857 if (packet->last_instr_cond)
1858 packet->flags |= PERF_IP_FLAG_CONDITIONAL;
1859 }
1860
1861
1862
1863
1864
1865 if (packet->last_instr_type == OCSD_INSTR_BR &&
1866 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
1867 packet->flags = PERF_IP_FLAG_BRANCH |
1868 PERF_IP_FLAG_CALL;
1869
1870
1871
1872
1873
1874 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1875 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
1876 packet->flags = PERF_IP_FLAG_BRANCH |
1877 PERF_IP_FLAG_CALL;
1878
1879
1880
1881
1882
1883
1884 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1885 packet->last_instr_subtype == OCSD_S_INSTR_V7_IMPLIED_RET)
1886 packet->flags = PERF_IP_FLAG_BRANCH |
1887 PERF_IP_FLAG_RETURN;
1888
1889
1890
1891
1892
1893
1894 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1895 packet->last_instr_subtype == OCSD_S_INSTR_NONE)
1896 packet->flags = PERF_IP_FLAG_BRANCH |
1897 PERF_IP_FLAG_RETURN;
1898
1899
1900 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1901 packet->last_instr_subtype == OCSD_S_INSTR_V8_RET)
1902 packet->flags = PERF_IP_FLAG_BRANCH |
1903 PERF_IP_FLAG_RETURN;
1904
1905
1906
1907
1908
1909
1910 if (prev_packet->sample_type == CS_ETM_DISCONTINUITY)
1911 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
1912 PERF_IP_FLAG_TRACE_BEGIN;
1913
1914
1915
1916
1917
1918
1919
1920 if (prev_packet->flags == (PERF_IP_FLAG_BRANCH |
1921 PERF_IP_FLAG_RETURN |
1922 PERF_IP_FLAG_INTERRUPT) &&
1923 cs_etm__is_svc_instr(etmq, trace_chan_id,
1924 packet, packet->start_addr))
1925 prev_packet->flags = PERF_IP_FLAG_BRANCH |
1926 PERF_IP_FLAG_RETURN |
1927 PERF_IP_FLAG_SYSCALLRET;
1928 break;
1929 case CS_ETM_DISCONTINUITY:
1930
1931
1932
1933
1934
1935 if (prev_packet->sample_type == CS_ETM_RANGE)
1936 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
1937 PERF_IP_FLAG_TRACE_END;
1938 break;
1939 case CS_ETM_EXCEPTION:
1940 ret = cs_etm__get_magic(packet->trace_chan_id, &magic);
1941 if (ret)
1942 return ret;
1943
1944
1945 if (cs_etm__is_syscall(etmq, tidq, magic))
1946 packet->flags = PERF_IP_FLAG_BRANCH |
1947 PERF_IP_FLAG_CALL |
1948 PERF_IP_FLAG_SYSCALLRET;
1949
1950
1951
1952
1953 else if (cs_etm__is_async_exception(tidq, magic))
1954 packet->flags = PERF_IP_FLAG_BRANCH |
1955 PERF_IP_FLAG_CALL |
1956 PERF_IP_FLAG_ASYNC |
1957 PERF_IP_FLAG_INTERRUPT;
1958
1959
1960
1961
1962 else if (cs_etm__is_sync_exception(etmq, tidq, magic))
1963 packet->flags = PERF_IP_FLAG_BRANCH |
1964 PERF_IP_FLAG_CALL |
1965 PERF_IP_FLAG_INTERRUPT;
1966
1967
1968
1969
1970
1971
1972
1973
1974 if (prev_packet->sample_type == CS_ETM_RANGE)
1975 prev_packet->flags = packet->flags;
1976 break;
1977 case CS_ETM_EXCEPTION_RET:
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003 if (prev_packet->sample_type == CS_ETM_RANGE)
2004 prev_packet->flags = PERF_IP_FLAG_BRANCH |
2005 PERF_IP_FLAG_RETURN |
2006 PERF_IP_FLAG_INTERRUPT;
2007 break;
2008 case CS_ETM_EMPTY:
2009 default:
2010 break;
2011 }
2012
2013 return 0;
2014 }
2015
2016 static int cs_etm__decode_data_block(struct cs_etm_queue *etmq)
2017 {
2018 int ret = 0;
2019 size_t processed = 0;
2020
2021
2022
2023
2024
2025
2026
2027
2028 ret = cs_etm_decoder__process_data_block(etmq->decoder,
2029 etmq->offset,
2030 &etmq->buf[etmq->buf_used],
2031 etmq->buf_len,
2032 &processed);
2033 if (ret)
2034 goto out;
2035
2036 etmq->offset += processed;
2037 etmq->buf_used += processed;
2038 etmq->buf_len -= processed;
2039
2040 out:
2041 return ret;
2042 }
2043
2044 static int cs_etm__process_traceid_queue(struct cs_etm_queue *etmq,
2045 struct cs_etm_traceid_queue *tidq)
2046 {
2047 int ret;
2048 struct cs_etm_packet_queue *packet_queue;
2049
2050 packet_queue = &tidq->packet_queue;
2051
2052
2053 while (1) {
2054 ret = cs_etm_decoder__get_packet(packet_queue,
2055 tidq->packet);
2056 if (ret <= 0)
2057
2058
2059
2060
2061 break;
2062
2063
2064
2065
2066
2067
2068
2069
2070 ret = cs_etm__set_sample_flags(etmq, tidq);
2071 if (ret < 0)
2072 break;
2073
2074 switch (tidq->packet->sample_type) {
2075 case CS_ETM_RANGE:
2076
2077
2078
2079
2080
2081 cs_etm__sample(etmq, tidq);
2082 break;
2083 case CS_ETM_EXCEPTION:
2084 case CS_ETM_EXCEPTION_RET:
2085
2086
2087
2088
2089
2090 cs_etm__exception(tidq);
2091 break;
2092 case CS_ETM_DISCONTINUITY:
2093
2094
2095
2096
2097 cs_etm__flush(etmq, tidq);
2098 break;
2099 case CS_ETM_EMPTY:
2100
2101
2102
2103
2104 pr_err("CS ETM Trace: empty packet\n");
2105 return -EINVAL;
2106 default:
2107 break;
2108 }
2109 }
2110
2111 return ret;
2112 }
2113
2114 static void cs_etm__clear_all_traceid_queues(struct cs_etm_queue *etmq)
2115 {
2116 int idx;
2117 struct int_node *inode;
2118 struct cs_etm_traceid_queue *tidq;
2119 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
2120
2121 intlist__for_each_entry(inode, traceid_queues_list) {
2122 idx = (int)(intptr_t)inode->priv;
2123 tidq = etmq->traceid_queues[idx];
2124
2125
2126 cs_etm__process_traceid_queue(etmq, tidq);
2127
2128
2129
2130
2131
2132 cs_etm__flush(etmq, tidq);
2133 }
2134 }
2135
2136 static int cs_etm__run_decoder(struct cs_etm_queue *etmq)
2137 {
2138 int err = 0;
2139 struct cs_etm_traceid_queue *tidq;
2140
2141 tidq = cs_etm__etmq_get_traceid_queue(etmq, CS_ETM_PER_THREAD_TRACEID);
2142 if (!tidq)
2143 return -EINVAL;
2144
2145
2146 while (1) {
2147 err = cs_etm__get_data_block(etmq);
2148 if (err <= 0)
2149 return err;
2150
2151
2152 do {
2153 err = cs_etm__decode_data_block(etmq);
2154 if (err)
2155 return err;
2156
2157
2158
2159
2160
2161
2162 err = cs_etm__process_traceid_queue(etmq, tidq);
2163
2164 } while (etmq->buf_len);
2165
2166 if (err == 0)
2167
2168 err = cs_etm__end_block(etmq, tidq);
2169 }
2170
2171 return err;
2172 }
2173
2174 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
2175 pid_t tid)
2176 {
2177 unsigned int i;
2178 struct auxtrace_queues *queues = &etm->queues;
2179
2180 for (i = 0; i < queues->nr_queues; i++) {
2181 struct auxtrace_queue *queue = &etm->queues.queue_array[i];
2182 struct cs_etm_queue *etmq = queue->priv;
2183 struct cs_etm_traceid_queue *tidq;
2184
2185 if (!etmq)
2186 continue;
2187
2188 tidq = cs_etm__etmq_get_traceid_queue(etmq,
2189 CS_ETM_PER_THREAD_TRACEID);
2190
2191 if (!tidq)
2192 continue;
2193
2194 if ((tid == -1) || (tidq->tid == tid)) {
2195 cs_etm__set_pid_tid_cpu(etm, tidq);
2196 cs_etm__run_decoder(etmq);
2197 }
2198 }
2199
2200 return 0;
2201 }
2202
2203 static int cs_etm__process_queues(struct cs_etm_auxtrace *etm)
2204 {
2205 int ret = 0;
2206 unsigned int cs_queue_nr, queue_nr, i;
2207 u8 trace_chan_id;
2208 u64 cs_timestamp;
2209 struct auxtrace_queue *queue;
2210 struct cs_etm_queue *etmq;
2211 struct cs_etm_traceid_queue *tidq;
2212
2213
2214
2215
2216
2217 for (i = 0; i < etm->queues.nr_queues; i++) {
2218 etmq = etm->queues.queue_array[i].priv;
2219 if (!etmq)
2220 continue;
2221
2222 ret = cs_etm__queue_first_cs_timestamp(etm, etmq, i);
2223 if (ret)
2224 return ret;
2225 }
2226
2227 while (1) {
2228 if (!etm->heap.heap_cnt)
2229 goto out;
2230
2231
2232 cs_queue_nr = etm->heap.heap_array[0].queue_nr;
2233 queue_nr = TO_QUEUE_NR(cs_queue_nr);
2234 trace_chan_id = TO_TRACE_CHAN_ID(cs_queue_nr);
2235 queue = &etm->queues.queue_array[queue_nr];
2236 etmq = queue->priv;
2237
2238
2239
2240
2241
2242 auxtrace_heap__pop(&etm->heap);
2243
2244 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
2245 if (!tidq) {
2246
2247
2248
2249
2250
2251 ret = -EINVAL;
2252 goto out;
2253 }
2254
2255
2256
2257
2258
2259 ret = cs_etm__process_traceid_queue(etmq, tidq);
2260 if (ret < 0)
2261 goto out;
2262
2263
2264
2265
2266
2267
2268 refetch:
2269 ret = cs_etm__get_data_block(etmq);
2270 if (ret < 0)
2271 goto out;
2272
2273
2274
2275
2276
2277 if (!ret)
2278 continue;
2279
2280 ret = cs_etm__decode_data_block(etmq);
2281 if (ret)
2282 goto out;
2283
2284 cs_timestamp = cs_etm__etmq_get_timestamp(etmq, &trace_chan_id);
2285
2286 if (!cs_timestamp) {
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296 cs_etm__clear_all_traceid_queues(etmq);
2297
2298
2299 goto refetch;
2300 }
2301
2302
2303
2304
2305
2306
2307
2308 cs_queue_nr = TO_CS_QUEUE_NR(queue_nr, trace_chan_id);
2309 ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, cs_timestamp);
2310 }
2311
2312 out:
2313 return ret;
2314 }
2315
2316 static int cs_etm__process_itrace_start(struct cs_etm_auxtrace *etm,
2317 union perf_event *event)
2318 {
2319 struct thread *th;
2320
2321 if (etm->timeless_decoding)
2322 return 0;
2323
2324
2325
2326
2327
2328 th = machine__findnew_thread(etm->machine,
2329 event->itrace_start.pid,
2330 event->itrace_start.tid);
2331 if (!th)
2332 return -ENOMEM;
2333
2334 thread__put(th);
2335
2336 return 0;
2337 }
2338
2339 static int cs_etm__process_switch_cpu_wide(struct cs_etm_auxtrace *etm,
2340 union perf_event *event)
2341 {
2342 struct thread *th;
2343 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
2344
2345
2346
2347
2348
2349 if (etm->timeless_decoding)
2350 return 0;
2351
2352
2353
2354
2355
2356
2357 if (!out)
2358 return 0;
2359
2360
2361
2362
2363
2364 th = machine__findnew_thread(etm->machine,
2365 event->context_switch.next_prev_pid,
2366 event->context_switch.next_prev_tid);
2367 if (!th)
2368 return -ENOMEM;
2369
2370 thread__put(th);
2371
2372 return 0;
2373 }
2374
2375 static int cs_etm__process_event(struct perf_session *session,
2376 union perf_event *event,
2377 struct perf_sample *sample,
2378 struct perf_tool *tool)
2379 {
2380 u64 sample_kernel_timestamp;
2381 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2382 struct cs_etm_auxtrace,
2383 auxtrace);
2384
2385 if (dump_trace)
2386 return 0;
2387
2388 if (!tool->ordered_events) {
2389 pr_err("CoreSight ETM Trace requires ordered events\n");
2390 return -EINVAL;
2391 }
2392
2393 if (sample->time && (sample->time != (u64) -1))
2394 sample_kernel_timestamp = sample->time;
2395 else
2396 sample_kernel_timestamp = 0;
2397
2398
2399
2400
2401
2402
2403 if (etm->timeless_decoding &&
2404 event->header.type == PERF_RECORD_EXIT)
2405 return cs_etm__process_timeless_queues(etm,
2406 event->fork.tid);
2407
2408 if (event->header.type == PERF_RECORD_ITRACE_START)
2409 return cs_etm__process_itrace_start(etm, event);
2410 else if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)
2411 return cs_etm__process_switch_cpu_wide(etm, event);
2412
2413 if (!etm->timeless_decoding && event->header.type == PERF_RECORD_AUX) {
2414
2415
2416
2417
2418
2419 etm->latest_kernel_timestamp = sample_kernel_timestamp;
2420 }
2421
2422 return 0;
2423 }
2424
2425 static void dump_queued_data(struct cs_etm_auxtrace *etm,
2426 struct perf_record_auxtrace *event)
2427 {
2428 struct auxtrace_buffer *buf;
2429 unsigned int i;
2430
2431
2432
2433
2434
2435 for (i = 0; i < etm->queues.nr_queues; ++i)
2436 list_for_each_entry(buf, &etm->queues.queue_array[i].head, list)
2437 if (buf->reference == event->reference)
2438 cs_etm__dump_event(etm->queues.queue_array[i].priv, buf);
2439 }
2440
2441 static int cs_etm__process_auxtrace_event(struct perf_session *session,
2442 union perf_event *event,
2443 struct perf_tool *tool __maybe_unused)
2444 {
2445 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2446 struct cs_etm_auxtrace,
2447 auxtrace);
2448 if (!etm->data_queued) {
2449 struct auxtrace_buffer *buffer;
2450 off_t data_offset;
2451 int fd = perf_data__fd(session->data);
2452 bool is_pipe = perf_data__is_pipe(session->data);
2453 int err;
2454 int idx = event->auxtrace.idx;
2455
2456 if (is_pipe)
2457 data_offset = 0;
2458 else {
2459 data_offset = lseek(fd, 0, SEEK_CUR);
2460 if (data_offset == -1)
2461 return -errno;
2462 }
2463
2464 err = auxtrace_queues__add_event(&etm->queues, session,
2465 event, data_offset, &buffer);
2466 if (err)
2467 return err;
2468
2469
2470
2471
2472
2473
2474
2475 err = cs_etm__setup_queue(etm, &etm->queues.queue_array[idx],
2476 idx, true);
2477 if (err)
2478 return err;
2479
2480 if (dump_trace)
2481 if (auxtrace_buffer__get_data(buffer, fd)) {
2482 cs_etm__dump_event(etm->queues.queue_array[idx].priv, buffer);
2483 auxtrace_buffer__put_data(buffer);
2484 }
2485 } else if (dump_trace)
2486 dump_queued_data(etm, &event->auxtrace);
2487
2488 return 0;
2489 }
2490
2491 static bool cs_etm__is_timeless_decoding(struct cs_etm_auxtrace *etm)
2492 {
2493 struct evsel *evsel;
2494 struct evlist *evlist = etm->session->evlist;
2495 bool timeless_decoding = true;
2496
2497
2498 if (etm->synth_opts.timeless_decoding)
2499 return true;
2500
2501
2502
2503
2504
2505 evlist__for_each_entry(evlist, evsel) {
2506 if ((evsel->core.attr.sample_type & PERF_SAMPLE_TIME))
2507 timeless_decoding = false;
2508 }
2509
2510 return timeless_decoding;
2511 }
2512
2513 static const char * const cs_etm_global_header_fmts[] = {
2514 [CS_HEADER_VERSION] = " Header version %llx\n",
2515 [CS_PMU_TYPE_CPUS] = " PMU type/num cpus %llx\n",
2516 [CS_ETM_SNAPSHOT] = " Snapshot %llx\n",
2517 };
2518
2519 static const char * const cs_etm_priv_fmts[] = {
2520 [CS_ETM_MAGIC] = " Magic number %llx\n",
2521 [CS_ETM_CPU] = " CPU %lld\n",
2522 [CS_ETM_NR_TRC_PARAMS] = " NR_TRC_PARAMS %llx\n",
2523 [CS_ETM_ETMCR] = " ETMCR %llx\n",
2524 [CS_ETM_ETMTRACEIDR] = " ETMTRACEIDR %llx\n",
2525 [CS_ETM_ETMCCER] = " ETMCCER %llx\n",
2526 [CS_ETM_ETMIDR] = " ETMIDR %llx\n",
2527 };
2528
2529 static const char * const cs_etmv4_priv_fmts[] = {
2530 [CS_ETM_MAGIC] = " Magic number %llx\n",
2531 [CS_ETM_CPU] = " CPU %lld\n",
2532 [CS_ETM_NR_TRC_PARAMS] = " NR_TRC_PARAMS %llx\n",
2533 [CS_ETMV4_TRCCONFIGR] = " TRCCONFIGR %llx\n",
2534 [CS_ETMV4_TRCTRACEIDR] = " TRCTRACEIDR %llx\n",
2535 [CS_ETMV4_TRCIDR0] = " TRCIDR0 %llx\n",
2536 [CS_ETMV4_TRCIDR1] = " TRCIDR1 %llx\n",
2537 [CS_ETMV4_TRCIDR2] = " TRCIDR2 %llx\n",
2538 [CS_ETMV4_TRCIDR8] = " TRCIDR8 %llx\n",
2539 [CS_ETMV4_TRCAUTHSTATUS] = " TRCAUTHSTATUS %llx\n",
2540 [CS_ETE_TRCDEVARCH] = " TRCDEVARCH %llx\n"
2541 };
2542
2543 static const char * const param_unk_fmt =
2544 " Unknown parameter [%d] %llx\n";
2545 static const char * const magic_unk_fmt =
2546 " Magic number Unknown %llx\n";
2547
2548 static int cs_etm__print_cpu_metadata_v0(__u64 *val, int *offset)
2549 {
2550 int i = *offset, j, nr_params = 0, fmt_offset;
2551 __u64 magic;
2552
2553
2554 magic = val[i + CS_ETM_MAGIC];
2555 if ((magic != __perf_cs_etmv3_magic) &&
2556 (magic != __perf_cs_etmv4_magic)) {
2557
2558 fprintf(stdout, magic_unk_fmt, magic);
2559 return -EINVAL;
2560 }
2561
2562
2563 fprintf(stdout, cs_etm_priv_fmts[CS_ETM_MAGIC], val[i++]);
2564 fprintf(stdout, cs_etm_priv_fmts[CS_ETM_CPU], val[i++]);
2565
2566 if (magic == __perf_cs_etmv3_magic) {
2567 nr_params = CS_ETM_NR_TRC_PARAMS_V0;
2568 fmt_offset = CS_ETM_ETMCR;
2569
2570 for (j = fmt_offset; j < nr_params + fmt_offset; j++, i++)
2571 fprintf(stdout, cs_etm_priv_fmts[j], val[i]);
2572 } else if (magic == __perf_cs_etmv4_magic) {
2573 nr_params = CS_ETMV4_NR_TRC_PARAMS_V0;
2574 fmt_offset = CS_ETMV4_TRCCONFIGR;
2575
2576 for (j = fmt_offset; j < nr_params + fmt_offset; j++, i++)
2577 fprintf(stdout, cs_etmv4_priv_fmts[j], val[i]);
2578 }
2579 *offset = i;
2580 return 0;
2581 }
2582
2583 static int cs_etm__print_cpu_metadata_v1(__u64 *val, int *offset)
2584 {
2585 int i = *offset, j, total_params = 0;
2586 __u64 magic;
2587
2588 magic = val[i + CS_ETM_MAGIC];
2589
2590 total_params = val[i + CS_ETM_NR_TRC_PARAMS] + CS_ETM_COMMON_BLK_MAX_V1;
2591
2592 if (magic == __perf_cs_etmv3_magic) {
2593 for (j = 0; j < total_params; j++, i++) {
2594
2595 if (j >= CS_ETM_PRIV_MAX)
2596 fprintf(stdout, param_unk_fmt, j, val[i]);
2597 else
2598 fprintf(stdout, cs_etm_priv_fmts[j], val[i]);
2599 }
2600 } else if (magic == __perf_cs_etmv4_magic || magic == __perf_cs_ete_magic) {
2601
2602
2603
2604
2605
2606 for (j = 0; j < total_params; j++, i++) {
2607
2608 if (j >= CS_ETE_PRIV_MAX)
2609 fprintf(stdout, param_unk_fmt, j, val[i]);
2610 else
2611 fprintf(stdout, cs_etmv4_priv_fmts[j], val[i]);
2612 }
2613 } else {
2614
2615 fprintf(stdout, magic_unk_fmt, magic);
2616 return -EINVAL;
2617 }
2618 *offset = i;
2619 return 0;
2620 }
2621
2622 static void cs_etm__print_auxtrace_info(__u64 *val, int num)
2623 {
2624 int i, cpu = 0, version, err;
2625
2626
2627 version = val[0];
2628 if (version > CS_HEADER_CURRENT_VERSION) {
2629
2630 fprintf(stdout, " Unknown Header Version = %x, ", version);
2631 fprintf(stdout, "Version supported <= %x\n", CS_HEADER_CURRENT_VERSION);
2632 return;
2633 }
2634
2635 for (i = 0; i < CS_HEADER_VERSION_MAX; i++)
2636 fprintf(stdout, cs_etm_global_header_fmts[i], val[i]);
2637
2638 for (i = CS_HEADER_VERSION_MAX; cpu < num; cpu++) {
2639 if (version == 0)
2640 err = cs_etm__print_cpu_metadata_v0(val, &i);
2641 else if (version == 1)
2642 err = cs_etm__print_cpu_metadata_v1(val, &i);
2643 if (err)
2644 return;
2645 }
2646 }
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658 static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset,
2659 int out_blk_size, int nr_params_v0)
2660 {
2661 u64 *metadata = NULL;
2662 int hdr_version;
2663 int nr_in_params, nr_out_params, nr_cmn_params;
2664 int i, k;
2665
2666 metadata = zalloc(sizeof(*metadata) * out_blk_size);
2667 if (!metadata)
2668 return NULL;
2669
2670
2671 i = *buff_in_offset;
2672 hdr_version = buff_in[CS_HEADER_VERSION];
2673
2674 if (!hdr_version) {
2675
2676 nr_in_params = nr_params_v0;
2677 metadata[CS_ETM_MAGIC] = buff_in[i + CS_ETM_MAGIC];
2678 metadata[CS_ETM_CPU] = buff_in[i + CS_ETM_CPU];
2679 metadata[CS_ETM_NR_TRC_PARAMS] = nr_in_params;
2680
2681 for (k = CS_ETM_COMMON_BLK_MAX_V1 - 1; k < nr_in_params; k++)
2682 metadata[k + 1] = buff_in[i + k];
2683
2684 nr_cmn_params = 2;
2685 } else {
2686
2687
2688 nr_cmn_params = 3;
2689 nr_in_params = buff_in[i + CS_ETM_NR_TRC_PARAMS];
2690
2691
2692 nr_out_params = nr_in_params + nr_cmn_params;
2693 if (nr_out_params > out_blk_size)
2694 nr_out_params = out_blk_size;
2695
2696 for (k = CS_ETM_MAGIC; k < nr_out_params; k++)
2697 metadata[k] = buff_in[i + k];
2698
2699
2700 metadata[CS_ETM_NR_TRC_PARAMS] = nr_out_params - nr_cmn_params;
2701 }
2702
2703
2704 i += nr_in_params + nr_cmn_params;
2705 *buff_in_offset = i;
2706 return metadata;
2707 }
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719 static int cs_etm__queue_aux_fragment(struct perf_session *session, off_t file_offset, size_t sz,
2720 struct perf_record_aux *aux_event, struct perf_sample *sample)
2721 {
2722 int err;
2723 char buf[PERF_SAMPLE_MAX_SIZE];
2724 union perf_event *auxtrace_event_union;
2725 struct perf_record_auxtrace *auxtrace_event;
2726 union perf_event auxtrace_fragment;
2727 __u64 aux_offset, aux_size;
2728 __u32 idx;
2729 bool formatted;
2730
2731 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2732 struct cs_etm_auxtrace,
2733 auxtrace);
2734
2735
2736
2737
2738
2739 err = perf_session__peek_event(session, file_offset, buf,
2740 PERF_SAMPLE_MAX_SIZE, &auxtrace_event_union, NULL);
2741 if (err)
2742 return err;
2743 auxtrace_event = &auxtrace_event_union->auxtrace;
2744 if (auxtrace_event->header.type != PERF_RECORD_AUXTRACE)
2745 return -EINVAL;
2746
2747 if (auxtrace_event->header.size < sizeof(struct perf_record_auxtrace) ||
2748 auxtrace_event->header.size != sz) {
2749 return -EINVAL;
2750 }
2751
2752
2753
2754
2755
2756 if ((auxtrace_event->cpu == (__u32) -1 && auxtrace_event->tid != sample->tid) ||
2757 auxtrace_event->cpu != sample->cpu)
2758 return 1;
2759
2760 if (aux_event->flags & PERF_AUX_FLAG_OVERWRITE) {
2761
2762
2763
2764
2765
2766 aux_size = min(aux_event->aux_size, auxtrace_event->size);
2767
2768
2769
2770
2771
2772 aux_offset = aux_event->aux_offset - aux_size;
2773 } else {
2774 aux_size = aux_event->aux_size;
2775 aux_offset = aux_event->aux_offset;
2776 }
2777
2778 if (aux_offset >= auxtrace_event->offset &&
2779 aux_offset + aux_size <= auxtrace_event->offset + auxtrace_event->size) {
2780
2781
2782
2783
2784 auxtrace_fragment.auxtrace = *auxtrace_event;
2785 auxtrace_fragment.auxtrace.size = aux_size;
2786 auxtrace_fragment.auxtrace.offset = aux_offset;
2787 file_offset += aux_offset - auxtrace_event->offset + auxtrace_event->header.size;
2788
2789 pr_debug3("CS ETM: Queue buffer size: %#"PRI_lx64" offset: %#"PRI_lx64
2790 " tid: %d cpu: %d\n", aux_size, aux_offset, sample->tid, sample->cpu);
2791 err = auxtrace_queues__add_event(&etm->queues, session, &auxtrace_fragment,
2792 file_offset, NULL);
2793 if (err)
2794 return err;
2795
2796 idx = auxtrace_event->idx;
2797 formatted = !(aux_event->flags & PERF_AUX_FLAG_CORESIGHT_FORMAT_RAW);
2798 return cs_etm__setup_queue(etm, &etm->queues.queue_array[idx],
2799 idx, formatted);
2800 }
2801
2802
2803 return 1;
2804 }
2805
2806 static int cs_etm__queue_aux_records_cb(struct perf_session *session, union perf_event *event,
2807 u64 offset __maybe_unused, void *data __maybe_unused)
2808 {
2809 struct perf_sample sample;
2810 int ret;
2811 struct auxtrace_index_entry *ent;
2812 struct auxtrace_index *auxtrace_index;
2813 struct evsel *evsel;
2814 size_t i;
2815
2816
2817 if (event->header.type != PERF_RECORD_AUX)
2818 return 0;
2819
2820 if (event->header.size < sizeof(struct perf_record_aux))
2821 return -EINVAL;
2822
2823
2824 if (!event->aux.aux_size)
2825 return 0;
2826
2827
2828
2829
2830
2831 evsel = evlist__event2evsel(session->evlist, event);
2832 if (!evsel)
2833 return -EINVAL;
2834 ret = evsel__parse_sample(evsel, event, &sample);
2835 if (ret)
2836 return ret;
2837
2838
2839
2840
2841 list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
2842 for (i = 0; i < auxtrace_index->nr; i++) {
2843 ent = &auxtrace_index->entries[i];
2844 ret = cs_etm__queue_aux_fragment(session, ent->file_offset,
2845 ent->sz, &event->aux, &sample);
2846
2847
2848
2849
2850 if (ret != 1)
2851 return ret;
2852 }
2853 }
2854
2855
2856
2857
2858
2859 pr_err("CS ETM: Couldn't find auxtrace buffer for aux_offset: %#"PRI_lx64
2860 " tid: %d cpu: %d\n", event->aux.aux_offset, sample.tid, sample.cpu);
2861 return 0;
2862 }
2863
2864 static int cs_etm__queue_aux_records(struct perf_session *session)
2865 {
2866 struct auxtrace_index *index = list_first_entry_or_null(&session->auxtrace_index,
2867 struct auxtrace_index, list);
2868 if (index && index->nr > 0)
2869 return perf_session__peek_events(session, session->header.data_offset,
2870 session->header.data_size,
2871 cs_etm__queue_aux_records_cb, NULL);
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881 return 0;
2882 }
2883
2884 int cs_etm__process_auxtrace_info(union perf_event *event,
2885 struct perf_session *session)
2886 {
2887 struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
2888 struct cs_etm_auxtrace *etm = NULL;
2889 struct int_node *inode;
2890 unsigned int pmu_type;
2891 int event_header_size = sizeof(struct perf_event_header);
2892 int info_header_size;
2893 int total_size = auxtrace_info->header.size;
2894 int priv_size = 0;
2895 int num_cpu, trcidr_idx;
2896 int err = 0;
2897 int i, j;
2898 u64 *ptr, *hdr = NULL;
2899 u64 **metadata = NULL;
2900 u64 hdr_version;
2901
2902
2903
2904
2905
2906 info_header_size = 8;
2907
2908 if (total_size < (event_header_size + info_header_size))
2909 return -EINVAL;
2910
2911 priv_size = total_size - event_header_size - info_header_size;
2912
2913
2914 ptr = (u64 *) auxtrace_info->priv;
2915
2916
2917 hdr_version = ptr[0];
2918 if (hdr_version > CS_HEADER_CURRENT_VERSION) {
2919
2920 if (dump_trace)
2921 cs_etm__print_auxtrace_info(auxtrace_info->priv, 0);
2922 return -EINVAL;
2923 }
2924
2925 hdr = zalloc(sizeof(*hdr) * CS_HEADER_VERSION_MAX);
2926 if (!hdr)
2927 return -ENOMEM;
2928
2929
2930 for (i = 0; i < CS_HEADER_VERSION_MAX; i++)
2931 hdr[i] = ptr[i];
2932 num_cpu = hdr[CS_PMU_TYPE_CPUS] & 0xffffffff;
2933 pmu_type = (unsigned int) ((hdr[CS_PMU_TYPE_CPUS] >> 32) &
2934 0xffffffff);
2935
2936
2937
2938
2939
2940
2941 traceid_list = intlist__new(NULL);
2942 if (!traceid_list) {
2943 err = -ENOMEM;
2944 goto err_free_hdr;
2945 }
2946
2947 metadata = zalloc(sizeof(*metadata) * num_cpu);
2948 if (!metadata) {
2949 err = -ENOMEM;
2950 goto err_free_traceid_list;
2951 }
2952
2953
2954
2955
2956
2957
2958
2959 for (j = 0; j < num_cpu; j++) {
2960 if (ptr[i] == __perf_cs_etmv3_magic) {
2961 metadata[j] =
2962 cs_etm__create_meta_blk(ptr, &i,
2963 CS_ETM_PRIV_MAX,
2964 CS_ETM_NR_TRC_PARAMS_V0);
2965
2966
2967 trcidr_idx = CS_ETM_ETMTRACEIDR;
2968
2969 } else if (ptr[i] == __perf_cs_etmv4_magic) {
2970 metadata[j] =
2971 cs_etm__create_meta_blk(ptr, &i,
2972 CS_ETMV4_PRIV_MAX,
2973 CS_ETMV4_NR_TRC_PARAMS_V0);
2974
2975
2976 trcidr_idx = CS_ETMV4_TRCTRACEIDR;
2977 } else if (ptr[i] == __perf_cs_ete_magic) {
2978 metadata[j] = cs_etm__create_meta_blk(ptr, &i, CS_ETE_PRIV_MAX, -1);
2979
2980
2981 trcidr_idx = CS_ETMV4_TRCTRACEIDR;
2982 } else {
2983 ui__error("CS ETM Trace: Unrecognised magic number %#"PRIx64". File could be from a newer version of perf.\n",
2984 ptr[i]);
2985 err = -EINVAL;
2986 goto err_free_metadata;
2987 }
2988
2989 if (!metadata[j]) {
2990 err = -ENOMEM;
2991 goto err_free_metadata;
2992 }
2993
2994
2995 inode = intlist__findnew(traceid_list, metadata[j][trcidr_idx]);
2996
2997
2998 if (!inode) {
2999 err = -ENOMEM;
3000 goto err_free_metadata;
3001 }
3002
3003
3004
3005
3006
3007 if (inode->priv) {
3008 err = -EINVAL;
3009 goto err_free_metadata;
3010 }
3011
3012 inode->priv = metadata[j];
3013 }
3014
3015
3016
3017
3018
3019
3020
3021
3022 if (i * 8 != priv_size) {
3023 err = -EINVAL;
3024 goto err_free_metadata;
3025 }
3026
3027 etm = zalloc(sizeof(*etm));
3028
3029 if (!etm) {
3030 err = -ENOMEM;
3031 goto err_free_metadata;
3032 }
3033
3034 err = auxtrace_queues__init(&etm->queues);
3035 if (err)
3036 goto err_free_etm;
3037
3038 if (session->itrace_synth_opts->set) {
3039 etm->synth_opts = *session->itrace_synth_opts;
3040 } else {
3041 itrace_synth_opts__set_default(&etm->synth_opts,
3042 session->itrace_synth_opts->default_no_sample);
3043 etm->synth_opts.callchain = false;
3044 }
3045
3046 etm->session = session;
3047 etm->machine = &session->machines.host;
3048
3049 etm->num_cpu = num_cpu;
3050 etm->pmu_type = pmu_type;
3051 etm->snapshot_mode = (hdr[CS_ETM_SNAPSHOT] != 0);
3052 etm->metadata = metadata;
3053 etm->auxtrace_type = auxtrace_info->type;
3054 etm->timeless_decoding = cs_etm__is_timeless_decoding(etm);
3055
3056 etm->auxtrace.process_event = cs_etm__process_event;
3057 etm->auxtrace.process_auxtrace_event = cs_etm__process_auxtrace_event;
3058 etm->auxtrace.flush_events = cs_etm__flush_events;
3059 etm->auxtrace.free_events = cs_etm__free_events;
3060 etm->auxtrace.free = cs_etm__free;
3061 etm->auxtrace.evsel_is_auxtrace = cs_etm__evsel_is_auxtrace;
3062 session->auxtrace = &etm->auxtrace;
3063
3064 etm->unknown_thread = thread__new(999999999, 999999999);
3065 if (!etm->unknown_thread) {
3066 err = -ENOMEM;
3067 goto err_free_queues;
3068 }
3069
3070
3071
3072
3073
3074 INIT_LIST_HEAD(&etm->unknown_thread->node);
3075
3076 err = thread__set_comm(etm->unknown_thread, "unknown", 0);
3077 if (err)
3078 goto err_delete_thread;
3079
3080 if (thread__init_maps(etm->unknown_thread, etm->machine)) {
3081 err = -ENOMEM;
3082 goto err_delete_thread;
3083 }
3084
3085 if (dump_trace) {
3086 cs_etm__print_auxtrace_info(auxtrace_info->priv, num_cpu);
3087 }
3088
3089 err = cs_etm__synth_events(etm, session);
3090 if (err)
3091 goto err_delete_thread;
3092
3093 err = cs_etm__queue_aux_records(session);
3094 if (err)
3095 goto err_delete_thread;
3096
3097 etm->data_queued = etm->queues.populated;
3098
3099
3100
3101
3102 if (!etm->data_queued)
3103 pr_warning("CS ETM warning: Coresight decode and TRBE support requires random file access.\n"
3104 "Continuing with best effort decoding in piped mode.\n\n");
3105
3106 return 0;
3107
3108 err_delete_thread:
3109 thread__zput(etm->unknown_thread);
3110 err_free_queues:
3111 auxtrace_queues__free(&etm->queues);
3112 session->auxtrace = NULL;
3113 err_free_etm:
3114 zfree(&etm);
3115 err_free_metadata:
3116
3117 for (j = 0; j < num_cpu; j++)
3118 zfree(&metadata[j]);
3119 zfree(&metadata);
3120 err_free_traceid_list:
3121 intlist__delete(traceid_list);
3122 err_free_hdr:
3123 zfree(&hdr);
3124
3125
3126
3127
3128
3129 if (dump_trace)
3130 cs_etm__print_auxtrace_info(auxtrace_info->priv, num_cpu);
3131 return err;
3132 }