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