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