1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright(C) 2015-2018 Linaro Limited.
5 * Author: Tor Jeremiassen <tor@ti.com>
6 * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
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 #include <linux/zalloc.h>
16 #include <opencsd/ocsd_if_types.h>
22 #include "cs-etm-decoder/cs-etm-decoder.h"
31 #include "map_symbol.h"
36 #include "thread-stack.h"
37 #include <tools/libc_compat.h>
38 #include "util/synthetic-events.h"
40 #define MAX_TIMESTAMP (~0ULL)
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
;
55 u8 sample_instructions
;
59 u64 branches_sample_type
;
61 u64 instructions_sample_type
;
62 u64 instructions_sample_period
;
66 unsigned int pmu_type
;
69 struct cs_etm_traceid_queue
{
72 u64 period_instructions
;
73 size_t last_branch_pos
;
74 union perf_event
*event_buf
;
75 struct thread
*thread
;
76 struct branch_stack
*last_branch
;
77 struct branch_stack
*last_branch_rb
;
78 struct cs_etm_packet
*prev_packet
;
79 struct cs_etm_packet
*packet
;
80 struct cs_etm_packet_queue packet_queue
;
84 struct cs_etm_auxtrace
*etm
;
85 struct cs_etm_decoder
*decoder
;
86 struct auxtrace_buffer
*buffer
;
87 unsigned int queue_nr
;
90 const unsigned char *buf
;
91 size_t buf_len
, buf_used
;
92 /* Conversion between traceID and index in traceid_queues array */
93 struct intlist
*traceid_queues_list
;
94 struct cs_etm_traceid_queue
**traceid_queues
;
97 static int cs_etm__update_queues(struct cs_etm_auxtrace
*etm
);
98 static int cs_etm__process_queues(struct cs_etm_auxtrace
*etm
);
99 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace
*etm
,
101 static int cs_etm__get_data_block(struct cs_etm_queue
*etmq
);
102 static int cs_etm__decode_data_block(struct cs_etm_queue
*etmq
);
104 /* PTMs ETMIDR [11:8] set to b0011 */
105 #define ETMIDR_PTM_VERSION 0x00000300
108 * A struct auxtrace_heap_item only has a queue_nr and a timestamp to
109 * work with. One option is to modify to auxtrace_heap_XYZ() API or simply
110 * encode the etm queue number as the upper 16 bit and the channel as
113 #define TO_CS_QUEUE_NR(queue_nr, trace_chan_id) \
114 (queue_nr << 16 | trace_chan_id)
115 #define TO_QUEUE_NR(cs_queue_nr) (cs_queue_nr >> 16)
116 #define TO_TRACE_CHAN_ID(cs_queue_nr) (cs_queue_nr & 0x0000ffff)
118 static u32
cs_etm__get_v7_protocol_version(u32 etmidr
)
120 etmidr
&= ETMIDR_PTM_VERSION
;
122 if (etmidr
== ETMIDR_PTM_VERSION
)
123 return CS_ETM_PROTO_PTM
;
125 return CS_ETM_PROTO_ETMV3
;
128 static int cs_etm__get_magic(u8 trace_chan_id
, u64
*magic
)
130 struct int_node
*inode
;
133 inode
= intlist__find(traceid_list
, trace_chan_id
);
137 metadata
= inode
->priv
;
138 *magic
= metadata
[CS_ETM_MAGIC
];
142 int cs_etm__get_cpu(u8 trace_chan_id
, int *cpu
)
144 struct int_node
*inode
;
147 inode
= intlist__find(traceid_list
, trace_chan_id
);
151 metadata
= inode
->priv
;
152 *cpu
= (int)metadata
[CS_ETM_CPU
];
156 void cs_etm__etmq_set_traceid_queue_timestamp(struct cs_etm_queue
*etmq
,
160 * Wnen a timestamp packet is encountered the backend code
161 * is stopped so that the front end has time to process packets
162 * that were accumulated in the traceID queue. Since there can
163 * be more than one channel per cs_etm_queue, we need to specify
164 * what traceID queue needs servicing.
166 etmq
->pending_timestamp
= trace_chan_id
;
169 static u64
cs_etm__etmq_get_timestamp(struct cs_etm_queue
*etmq
,
172 struct cs_etm_packet_queue
*packet_queue
;
174 if (!etmq
->pending_timestamp
)
178 *trace_chan_id
= etmq
->pending_timestamp
;
180 packet_queue
= cs_etm__etmq_get_packet_queue(etmq
,
181 etmq
->pending_timestamp
);
185 /* Acknowledge pending status */
186 etmq
->pending_timestamp
= 0;
188 /* See function cs_etm_decoder__do_{hard|soft}_timestamp() */
189 return packet_queue
->timestamp
;
192 static void cs_etm__clear_packet_queue(struct cs_etm_packet_queue
*queue
)
198 queue
->packet_count
= 0;
199 for (i
= 0; i
< CS_ETM_PACKET_MAX_BUFFER
; i
++) {
200 queue
->packet_buffer
[i
].isa
= CS_ETM_ISA_UNKNOWN
;
201 queue
->packet_buffer
[i
].start_addr
= CS_ETM_INVAL_ADDR
;
202 queue
->packet_buffer
[i
].end_addr
= CS_ETM_INVAL_ADDR
;
203 queue
->packet_buffer
[i
].instr_count
= 0;
204 queue
->packet_buffer
[i
].last_instr_taken_branch
= false;
205 queue
->packet_buffer
[i
].last_instr_size
= 0;
206 queue
->packet_buffer
[i
].last_instr_type
= 0;
207 queue
->packet_buffer
[i
].last_instr_subtype
= 0;
208 queue
->packet_buffer
[i
].last_instr_cond
= 0;
209 queue
->packet_buffer
[i
].flags
= 0;
210 queue
->packet_buffer
[i
].exception_number
= UINT32_MAX
;
211 queue
->packet_buffer
[i
].trace_chan_id
= UINT8_MAX
;
212 queue
->packet_buffer
[i
].cpu
= INT_MIN
;
216 static void cs_etm__clear_all_packet_queues(struct cs_etm_queue
*etmq
)
219 struct int_node
*inode
;
220 struct cs_etm_traceid_queue
*tidq
;
221 struct intlist
*traceid_queues_list
= etmq
->traceid_queues_list
;
223 intlist__for_each_entry(inode
, traceid_queues_list
) {
224 idx
= (int)(intptr_t)inode
->priv
;
225 tidq
= etmq
->traceid_queues
[idx
];
226 cs_etm__clear_packet_queue(&tidq
->packet_queue
);
230 static int cs_etm__init_traceid_queue(struct cs_etm_queue
*etmq
,
231 struct cs_etm_traceid_queue
*tidq
,
235 struct auxtrace_queue
*queue
;
236 struct cs_etm_auxtrace
*etm
= etmq
->etm
;
238 cs_etm__clear_packet_queue(&tidq
->packet_queue
);
240 queue
= &etmq
->etm
->queues
.queue_array
[etmq
->queue_nr
];
241 tidq
->tid
= queue
->tid
;
243 tidq
->trace_chan_id
= trace_chan_id
;
245 tidq
->packet
= zalloc(sizeof(struct cs_etm_packet
));
249 tidq
->prev_packet
= zalloc(sizeof(struct cs_etm_packet
));
250 if (!tidq
->prev_packet
)
253 if (etm
->synth_opts
.last_branch
) {
254 size_t sz
= sizeof(struct branch_stack
);
256 sz
+= etm
->synth_opts
.last_branch_sz
*
257 sizeof(struct branch_entry
);
258 tidq
->last_branch
= zalloc(sz
);
259 if (!tidq
->last_branch
)
261 tidq
->last_branch_rb
= zalloc(sz
);
262 if (!tidq
->last_branch_rb
)
266 tidq
->event_buf
= malloc(PERF_SAMPLE_MAX_SIZE
);
267 if (!tidq
->event_buf
)
273 zfree(&tidq
->last_branch_rb
);
274 zfree(&tidq
->last_branch
);
275 zfree(&tidq
->prev_packet
);
276 zfree(&tidq
->packet
);
281 static struct cs_etm_traceid_queue
282 *cs_etm__etmq_get_traceid_queue(struct cs_etm_queue
*etmq
, u8 trace_chan_id
)
285 struct int_node
*inode
;
286 struct intlist
*traceid_queues_list
;
287 struct cs_etm_traceid_queue
*tidq
, **traceid_queues
;
288 struct cs_etm_auxtrace
*etm
= etmq
->etm
;
290 if (etm
->timeless_decoding
)
291 trace_chan_id
= CS_ETM_PER_THREAD_TRACEID
;
293 traceid_queues_list
= etmq
->traceid_queues_list
;
296 * Check if the traceid_queue exist for this traceID by looking
299 inode
= intlist__find(traceid_queues_list
, trace_chan_id
);
301 idx
= (int)(intptr_t)inode
->priv
;
302 return etmq
->traceid_queues
[idx
];
305 /* We couldn't find a traceid_queue for this traceID, allocate one */
306 tidq
= malloc(sizeof(*tidq
));
310 memset(tidq
, 0, sizeof(*tidq
));
312 /* Get a valid index for the new traceid_queue */
313 idx
= intlist__nr_entries(traceid_queues_list
);
314 /* Memory for the inode is free'ed in cs_etm_free_traceid_queues () */
315 inode
= intlist__findnew(traceid_queues_list
, trace_chan_id
);
319 /* Associate this traceID with this index */
320 inode
->priv
= (void *)(intptr_t)idx
;
322 if (cs_etm__init_traceid_queue(etmq
, tidq
, trace_chan_id
))
325 /* Grow the traceid_queues array by one unit */
326 traceid_queues
= etmq
->traceid_queues
;
327 traceid_queues
= reallocarray(traceid_queues
,
329 sizeof(*traceid_queues
));
332 * On failure reallocarray() returns NULL and the original block of
333 * memory is left untouched.
338 traceid_queues
[idx
] = tidq
;
339 etmq
->traceid_queues
= traceid_queues
;
341 return etmq
->traceid_queues
[idx
];
345 * Function intlist__remove() removes the inode from the list
346 * and delete the memory associated to it.
348 intlist__remove(traceid_queues_list
, inode
);
354 struct cs_etm_packet_queue
355 *cs_etm__etmq_get_packet_queue(struct cs_etm_queue
*etmq
, u8 trace_chan_id
)
357 struct cs_etm_traceid_queue
*tidq
;
359 tidq
= cs_etm__etmq_get_traceid_queue(etmq
, trace_chan_id
);
361 return &tidq
->packet_queue
;
366 static void cs_etm__packet_swap(struct cs_etm_auxtrace
*etm
,
367 struct cs_etm_traceid_queue
*tidq
)
369 struct cs_etm_packet
*tmp
;
371 if (etm
->sample_branches
|| etm
->synth_opts
.last_branch
||
372 etm
->sample_instructions
) {
374 * Swap PACKET with PREV_PACKET: PACKET becomes PREV_PACKET for
375 * the next incoming packet.
378 tidq
->packet
= tidq
->prev_packet
;
379 tidq
->prev_packet
= tmp
;
383 static void cs_etm__packet_dump(const char *pkt_string
)
385 const char *color
= PERF_COLOR_BLUE
;
386 int len
= strlen(pkt_string
);
388 if (len
&& (pkt_string
[len
-1] == '\n'))
389 color_fprintf(stdout
, color
, " %s", pkt_string
);
391 color_fprintf(stdout
, color
, " %s\n", pkt_string
);
396 static void cs_etm__set_trace_param_etmv3(struct cs_etm_trace_params
*t_params
,
397 struct cs_etm_auxtrace
*etm
, int idx
,
400 u64
**metadata
= etm
->metadata
;
402 t_params
[idx
].protocol
= cs_etm__get_v7_protocol_version(etmidr
);
403 t_params
[idx
].etmv3
.reg_ctrl
= metadata
[idx
][CS_ETM_ETMCR
];
404 t_params
[idx
].etmv3
.reg_trc_id
= metadata
[idx
][CS_ETM_ETMTRACEIDR
];
407 static void cs_etm__set_trace_param_etmv4(struct cs_etm_trace_params
*t_params
,
408 struct cs_etm_auxtrace
*etm
, int idx
)
410 u64
**metadata
= etm
->metadata
;
412 t_params
[idx
].protocol
= CS_ETM_PROTO_ETMV4i
;
413 t_params
[idx
].etmv4
.reg_idr0
= metadata
[idx
][CS_ETMV4_TRCIDR0
];
414 t_params
[idx
].etmv4
.reg_idr1
= metadata
[idx
][CS_ETMV4_TRCIDR1
];
415 t_params
[idx
].etmv4
.reg_idr2
= metadata
[idx
][CS_ETMV4_TRCIDR2
];
416 t_params
[idx
].etmv4
.reg_idr8
= metadata
[idx
][CS_ETMV4_TRCIDR8
];
417 t_params
[idx
].etmv4
.reg_configr
= metadata
[idx
][CS_ETMV4_TRCCONFIGR
];
418 t_params
[idx
].etmv4
.reg_traceidr
= metadata
[idx
][CS_ETMV4_TRCTRACEIDR
];
421 static int cs_etm__init_trace_params(struct cs_etm_trace_params
*t_params
,
422 struct cs_etm_auxtrace
*etm
)
428 for (i
= 0; i
< etm
->num_cpu
; i
++) {
429 architecture
= etm
->metadata
[i
][CS_ETM_MAGIC
];
431 switch (architecture
) {
432 case __perf_cs_etmv3_magic
:
433 etmidr
= etm
->metadata
[i
][CS_ETM_ETMIDR
];
434 cs_etm__set_trace_param_etmv3(t_params
, etm
, i
, etmidr
);
436 case __perf_cs_etmv4_magic
:
437 cs_etm__set_trace_param_etmv4(t_params
, etm
, i
);
447 static int cs_etm__init_decoder_params(struct cs_etm_decoder_params
*d_params
,
448 struct cs_etm_queue
*etmq
,
449 enum cs_etm_decoder_operation mode
)
453 if (!(mode
< CS_ETM_OPERATION_MAX
))
456 d_params
->packet_printer
= cs_etm__packet_dump
;
457 d_params
->operation
= mode
;
458 d_params
->data
= etmq
;
459 d_params
->formatted
= true;
460 d_params
->fsyncs
= false;
461 d_params
->hsyncs
= false;
462 d_params
->frame_aligned
= true;
469 static void cs_etm__dump_event(struct cs_etm_auxtrace
*etm
,
470 struct auxtrace_buffer
*buffer
)
473 const char *color
= PERF_COLOR_BLUE
;
474 struct cs_etm_decoder_params d_params
;
475 struct cs_etm_trace_params
*t_params
;
476 struct cs_etm_decoder
*decoder
;
477 size_t buffer_used
= 0;
479 fprintf(stdout
, "\n");
480 color_fprintf(stdout
, color
,
481 ". ... CoreSight ETM Trace data: size %zu bytes\n",
484 /* Use metadata to fill in trace parameters for trace decoder */
485 t_params
= zalloc(sizeof(*t_params
) * etm
->num_cpu
);
490 if (cs_etm__init_trace_params(t_params
, etm
))
493 /* Set decoder parameters to simply print the trace packets */
494 if (cs_etm__init_decoder_params(&d_params
, NULL
,
495 CS_ETM_OPERATION_PRINT
))
498 decoder
= cs_etm_decoder__new(etm
->num_cpu
, &d_params
, t_params
);
505 ret
= cs_etm_decoder__process_data_block(
506 decoder
, buffer
->offset
,
507 &((u8
*)buffer
->data
)[buffer_used
],
508 buffer
->size
- buffer_used
, &consumed
);
512 buffer_used
+= consumed
;
513 } while (buffer_used
< buffer
->size
);
515 cs_etm_decoder__free(decoder
);
521 static int cs_etm__flush_events(struct perf_session
*session
,
522 struct perf_tool
*tool
)
525 struct cs_etm_auxtrace
*etm
= container_of(session
->auxtrace
,
526 struct cs_etm_auxtrace
,
531 if (!tool
->ordered_events
)
534 ret
= cs_etm__update_queues(etm
);
539 if (etm
->timeless_decoding
)
540 return cs_etm__process_timeless_queues(etm
, -1);
542 return cs_etm__process_queues(etm
);
545 static void cs_etm__free_traceid_queues(struct cs_etm_queue
*etmq
)
549 struct int_node
*inode
, *tmp
;
550 struct cs_etm_traceid_queue
*tidq
;
551 struct intlist
*traceid_queues_list
= etmq
->traceid_queues_list
;
553 intlist__for_each_entry_safe(inode
, tmp
, traceid_queues_list
) {
554 priv
= (uintptr_t)inode
->priv
;
557 /* Free this traceid_queue from the array */
558 tidq
= etmq
->traceid_queues
[idx
];
559 thread__zput(tidq
->thread
);
560 zfree(&tidq
->event_buf
);
561 zfree(&tidq
->last_branch
);
562 zfree(&tidq
->last_branch_rb
);
563 zfree(&tidq
->prev_packet
);
564 zfree(&tidq
->packet
);
568 * Function intlist__remove() removes the inode from the list
569 * and delete the memory associated to it.
571 intlist__remove(traceid_queues_list
, inode
);
574 /* Then the RB tree itself */
575 intlist__delete(traceid_queues_list
);
576 etmq
->traceid_queues_list
= NULL
;
578 /* finally free the traceid_queues array */
579 zfree(&etmq
->traceid_queues
);
582 static void cs_etm__free_queue(void *priv
)
584 struct cs_etm_queue
*etmq
= priv
;
589 cs_etm_decoder__free(etmq
->decoder
);
590 cs_etm__free_traceid_queues(etmq
);
594 static void cs_etm__free_events(struct perf_session
*session
)
597 struct cs_etm_auxtrace
*aux
= container_of(session
->auxtrace
,
598 struct cs_etm_auxtrace
,
600 struct auxtrace_queues
*queues
= &aux
->queues
;
602 for (i
= 0; i
< queues
->nr_queues
; i
++) {
603 cs_etm__free_queue(queues
->queue_array
[i
].priv
);
604 queues
->queue_array
[i
].priv
= NULL
;
607 auxtrace_queues__free(queues
);
610 static void cs_etm__free(struct perf_session
*session
)
613 struct int_node
*inode
, *tmp
;
614 struct cs_etm_auxtrace
*aux
= container_of(session
->auxtrace
,
615 struct cs_etm_auxtrace
,
617 cs_etm__free_events(session
);
618 session
->auxtrace
= NULL
;
620 /* First remove all traceID/metadata nodes for the RB tree */
621 intlist__for_each_entry_safe(inode
, tmp
, traceid_list
)
622 intlist__remove(traceid_list
, inode
);
623 /* Then the RB tree itself */
624 intlist__delete(traceid_list
);
626 for (i
= 0; i
< aux
->num_cpu
; i
++)
627 zfree(&aux
->metadata
[i
]);
629 thread__zput(aux
->unknown_thread
);
630 zfree(&aux
->metadata
);
634 static u8
cs_etm__cpu_mode(struct cs_etm_queue
*etmq
, u64 address
)
636 struct machine
*machine
;
638 machine
= etmq
->etm
->machine
;
640 if (address
>= etmq
->etm
->kernel_start
) {
641 if (machine__is_host(machine
))
642 return PERF_RECORD_MISC_KERNEL
;
644 return PERF_RECORD_MISC_GUEST_KERNEL
;
646 if (machine__is_host(machine
))
647 return PERF_RECORD_MISC_USER
;
649 return PERF_RECORD_MISC_GUEST_USER
;
651 return PERF_RECORD_MISC_HYPERVISOR
;
655 static u32
cs_etm__mem_access(struct cs_etm_queue
*etmq
, u8 trace_chan_id
,
656 u64 address
, size_t size
, u8
*buffer
)
661 struct thread
*thread
;
662 struct machine
*machine
;
663 struct addr_location al
;
664 struct cs_etm_traceid_queue
*tidq
;
669 machine
= etmq
->etm
->machine
;
670 cpumode
= cs_etm__cpu_mode(etmq
, address
);
671 tidq
= cs_etm__etmq_get_traceid_queue(etmq
, trace_chan_id
);
675 thread
= tidq
->thread
;
677 if (cpumode
!= PERF_RECORD_MISC_KERNEL
)
679 thread
= etmq
->etm
->unknown_thread
;
682 if (!thread__find_map(thread
, cpumode
, address
, &al
) || !al
.map
->dso
)
685 if (al
.map
->dso
->data
.status
== DSO_DATA_STATUS_ERROR
&&
686 dso__data_status_seen(al
.map
->dso
, DSO_DATA_STATUS_SEEN_ITRACE
))
689 offset
= al
.map
->map_ip(al
.map
, address
);
693 len
= dso__data_read_offset(al
.map
->dso
, machine
, offset
, buffer
, size
);
701 static struct cs_etm_queue
*cs_etm__alloc_queue(struct cs_etm_auxtrace
*etm
)
703 struct cs_etm_decoder_params d_params
;
704 struct cs_etm_trace_params
*t_params
= NULL
;
705 struct cs_etm_queue
*etmq
;
707 etmq
= zalloc(sizeof(*etmq
));
711 etmq
->traceid_queues_list
= intlist__new(NULL
);
712 if (!etmq
->traceid_queues_list
)
715 /* Use metadata to fill in trace parameters for trace decoder */
716 t_params
= zalloc(sizeof(*t_params
) * etm
->num_cpu
);
721 if (cs_etm__init_trace_params(t_params
, etm
))
724 /* Set decoder parameters to decode trace packets */
725 if (cs_etm__init_decoder_params(&d_params
, etmq
,
726 CS_ETM_OPERATION_DECODE
))
729 etmq
->decoder
= cs_etm_decoder__new(etm
->num_cpu
, &d_params
, t_params
);
735 * Register a function to handle all memory accesses required by
736 * the trace decoder library.
738 if (cs_etm_decoder__add_mem_access_cb(etmq
->decoder
,
741 goto out_free_decoder
;
747 cs_etm_decoder__free(etmq
->decoder
);
749 intlist__delete(etmq
->traceid_queues_list
);
755 static int cs_etm__setup_queue(struct cs_etm_auxtrace
*etm
,
756 struct auxtrace_queue
*queue
,
757 unsigned int queue_nr
)
760 unsigned int cs_queue_nr
;
763 struct cs_etm_queue
*etmq
= queue
->priv
;
765 if (list_empty(&queue
->head
) || etmq
)
768 etmq
= cs_etm__alloc_queue(etm
);
777 etmq
->queue_nr
= queue_nr
;
780 if (etm
->timeless_decoding
)
784 * We are under a CPU-wide trace scenario. As such we need to know
785 * when the code that generated the traces started to execute so that
786 * it can be correlated with execution on other CPUs. So we get a
787 * handle on the beginning of traces and decode until we find a
788 * timestamp. The timestamp is then added to the auxtrace min heap
789 * in order to know what nibble (of all the etmqs) to decode first.
793 * Fetch an aux_buffer from this etmq. Bail if no more
794 * blocks or an error has been encountered.
796 ret
= cs_etm__get_data_block(etmq
);
801 * Run decoder on the trace block. The decoder will stop when
802 * encountering a timestamp, a full packet queue or the end of
803 * trace for that block.
805 ret
= cs_etm__decode_data_block(etmq
);
810 * Function cs_etm_decoder__do_{hard|soft}_timestamp() does all
811 * the timestamp calculation for us.
813 timestamp
= cs_etm__etmq_get_timestamp(etmq
, &trace_chan_id
);
815 /* We found a timestamp, no need to continue. */
820 * We didn't find a timestamp so empty all the traceid packet
821 * queues before looking for another timestamp packet, either
822 * in the current data block or a new one. Packets that were
823 * just decoded are useless since no timestamp has been
824 * associated with them. As such simply discard them.
826 cs_etm__clear_all_packet_queues(etmq
);
830 * We have a timestamp. Add it to the min heap to reflect when
831 * instructions conveyed by the range packets of this traceID queue
832 * started to execute. Once the same has been done for all the traceID
833 * queues of each etmq, redenring and decoding can start in
834 * chronological order.
836 * Note that packets decoded above are still in the traceID's packet
837 * queue and will be processed in cs_etm__process_queues().
839 cs_queue_nr
= TO_CS_QUEUE_NR(queue_nr
, trace_chan_id
);
840 ret
= auxtrace_heap__add(&etm
->heap
, cs_queue_nr
, timestamp
);
845 static int cs_etm__setup_queues(struct cs_etm_auxtrace
*etm
)
850 if (!etm
->kernel_start
)
851 etm
->kernel_start
= machine__kernel_start(etm
->machine
);
853 for (i
= 0; i
< etm
->queues
.nr_queues
; i
++) {
854 ret
= cs_etm__setup_queue(etm
, &etm
->queues
.queue_array
[i
], i
);
862 static int cs_etm__update_queues(struct cs_etm_auxtrace
*etm
)
864 if (etm
->queues
.new_data
) {
865 etm
->queues
.new_data
= false;
866 return cs_etm__setup_queues(etm
);
873 void cs_etm__copy_last_branch_rb(struct cs_etm_queue
*etmq
,
874 struct cs_etm_traceid_queue
*tidq
)
876 struct branch_stack
*bs_src
= tidq
->last_branch_rb
;
877 struct branch_stack
*bs_dst
= tidq
->last_branch
;
881 * Set the number of records before early exit: ->nr is used to
882 * determine how many branches to copy from ->entries.
884 bs_dst
->nr
= bs_src
->nr
;
887 * Early exit when there is nothing to copy.
893 * As bs_src->entries is a circular buffer, we need to copy from it in
894 * two steps. First, copy the branches from the most recently inserted
895 * branch ->last_branch_pos until the end of bs_src->entries buffer.
897 nr
= etmq
->etm
->synth_opts
.last_branch_sz
- tidq
->last_branch_pos
;
898 memcpy(&bs_dst
->entries
[0],
899 &bs_src
->entries
[tidq
->last_branch_pos
],
900 sizeof(struct branch_entry
) * nr
);
903 * If we wrapped around at least once, the branches from the beginning
904 * of the bs_src->entries buffer and until the ->last_branch_pos element
905 * are older valid branches: copy them over. The total number of
906 * branches copied over will be equal to the number of branches asked by
907 * the user in last_branch_sz.
909 if (bs_src
->nr
>= etmq
->etm
->synth_opts
.last_branch_sz
) {
910 memcpy(&bs_dst
->entries
[nr
],
912 sizeof(struct branch_entry
) * tidq
->last_branch_pos
);
917 void cs_etm__reset_last_branch_rb(struct cs_etm_traceid_queue
*tidq
)
919 tidq
->last_branch_pos
= 0;
920 tidq
->last_branch_rb
->nr
= 0;
923 static inline int cs_etm__t32_instr_size(struct cs_etm_queue
*etmq
,
924 u8 trace_chan_id
, u64 addr
)
928 cs_etm__mem_access(etmq
, trace_chan_id
, addr
,
929 ARRAY_SIZE(instrBytes
), instrBytes
);
931 * T32 instruction size is indicated by bits[15:11] of the first
932 * 16-bit word of the instruction: 0b11101, 0b11110 and 0b11111
933 * denote a 32-bit instruction.
935 return ((instrBytes
[1] & 0xF8) >= 0xE8) ? 4 : 2;
938 static inline u64
cs_etm__first_executed_instr(struct cs_etm_packet
*packet
)
940 /* Returns 0 for the CS_ETM_DISCONTINUITY packet */
941 if (packet
->sample_type
== CS_ETM_DISCONTINUITY
)
944 return packet
->start_addr
;
948 u64
cs_etm__last_executed_instr(const struct cs_etm_packet
*packet
)
950 /* Returns 0 for the CS_ETM_DISCONTINUITY packet */
951 if (packet
->sample_type
== CS_ETM_DISCONTINUITY
)
954 return packet
->end_addr
- packet
->last_instr_size
;
957 static inline u64
cs_etm__instr_addr(struct cs_etm_queue
*etmq
,
959 const struct cs_etm_packet
*packet
,
962 if (packet
->isa
== CS_ETM_ISA_T32
) {
963 u64 addr
= packet
->start_addr
;
966 addr
+= cs_etm__t32_instr_size(etmq
,
967 trace_chan_id
, addr
);
973 /* Assume a 4 byte instruction size (A32/A64) */
974 return packet
->start_addr
+ offset
* 4;
977 static void cs_etm__update_last_branch_rb(struct cs_etm_queue
*etmq
,
978 struct cs_etm_traceid_queue
*tidq
)
980 struct branch_stack
*bs
= tidq
->last_branch_rb
;
981 struct branch_entry
*be
;
984 * The branches are recorded in a circular buffer in reverse
985 * chronological order: we start recording from the last element of the
986 * buffer down. After writing the first element of the stack, move the
987 * insert position back to the end of the buffer.
989 if (!tidq
->last_branch_pos
)
990 tidq
->last_branch_pos
= etmq
->etm
->synth_opts
.last_branch_sz
;
992 tidq
->last_branch_pos
-= 1;
994 be
= &bs
->entries
[tidq
->last_branch_pos
];
995 be
->from
= cs_etm__last_executed_instr(tidq
->prev_packet
);
996 be
->to
= cs_etm__first_executed_instr(tidq
->packet
);
997 /* No support for mispredict */
998 be
->flags
.mispred
= 0;
999 be
->flags
.predicted
= 1;
1002 * Increment bs->nr until reaching the number of last branches asked by
1003 * the user on the command line.
1005 if (bs
->nr
< etmq
->etm
->synth_opts
.last_branch_sz
)
1009 static int cs_etm__inject_event(union perf_event
*event
,
1010 struct perf_sample
*sample
, u64 type
)
1012 event
->header
.size
= perf_event__sample_event_size(sample
, type
, 0);
1013 return perf_event__synthesize_sample(event
, type
, 0, sample
);
1018 cs_etm__get_trace(struct cs_etm_queue
*etmq
)
1020 struct auxtrace_buffer
*aux_buffer
= etmq
->buffer
;
1021 struct auxtrace_buffer
*old_buffer
= aux_buffer
;
1022 struct auxtrace_queue
*queue
;
1024 queue
= &etmq
->etm
->queues
.queue_array
[etmq
->queue_nr
];
1026 aux_buffer
= auxtrace_buffer__next(queue
, aux_buffer
);
1028 /* If no more data, drop the previous auxtrace_buffer and return */
1031 auxtrace_buffer__drop_data(old_buffer
);
1036 etmq
->buffer
= aux_buffer
;
1038 /* If the aux_buffer doesn't have data associated, try to load it */
1039 if (!aux_buffer
->data
) {
1040 /* get the file desc associated with the perf data file */
1041 int fd
= perf_data__fd(etmq
->etm
->session
->data
);
1043 aux_buffer
->data
= auxtrace_buffer__get_data(aux_buffer
, fd
);
1044 if (!aux_buffer
->data
)
1048 /* If valid, drop the previous buffer */
1050 auxtrace_buffer__drop_data(old_buffer
);
1053 etmq
->buf_len
= aux_buffer
->size
;
1054 etmq
->buf
= aux_buffer
->data
;
1056 return etmq
->buf_len
;
1059 static void cs_etm__set_pid_tid_cpu(struct cs_etm_auxtrace
*etm
,
1060 struct cs_etm_traceid_queue
*tidq
)
1062 if ((!tidq
->thread
) && (tidq
->tid
!= -1))
1063 tidq
->thread
= machine__find_thread(etm
->machine
, -1,
1067 tidq
->pid
= tidq
->thread
->pid_
;
1070 int cs_etm__etmq_set_tid(struct cs_etm_queue
*etmq
,
1071 pid_t tid
, u8 trace_chan_id
)
1073 int cpu
, err
= -EINVAL
;
1074 struct cs_etm_auxtrace
*etm
= etmq
->etm
;
1075 struct cs_etm_traceid_queue
*tidq
;
1077 tidq
= cs_etm__etmq_get_traceid_queue(etmq
, trace_chan_id
);
1081 if (cs_etm__get_cpu(trace_chan_id
, &cpu
) < 0)
1084 err
= machine__set_current_tid(etm
->machine
, cpu
, tid
, tid
);
1089 thread__zput(tidq
->thread
);
1091 cs_etm__set_pid_tid_cpu(etm
, tidq
);
1095 bool cs_etm__etmq_is_timeless(struct cs_etm_queue
*etmq
)
1097 return !!etmq
->etm
->timeless_decoding
;
1100 static void cs_etm__copy_insn(struct cs_etm_queue
*etmq
,
1102 const struct cs_etm_packet
*packet
,
1103 struct perf_sample
*sample
)
1106 * It's pointless to read instructions for the CS_ETM_DISCONTINUITY
1107 * packet, so directly bail out with 'insn_len' = 0.
1109 if (packet
->sample_type
== CS_ETM_DISCONTINUITY
) {
1110 sample
->insn_len
= 0;
1115 * T32 instruction size might be 32-bit or 16-bit, decide by calling
1116 * cs_etm__t32_instr_size().
1118 if (packet
->isa
== CS_ETM_ISA_T32
)
1119 sample
->insn_len
= cs_etm__t32_instr_size(etmq
, trace_chan_id
,
1121 /* Otherwise, A64 and A32 instruction size are always 32-bit. */
1123 sample
->insn_len
= 4;
1125 cs_etm__mem_access(etmq
, trace_chan_id
, sample
->ip
,
1126 sample
->insn_len
, (void *)sample
->insn
);
1129 static int cs_etm__synth_instruction_sample(struct cs_etm_queue
*etmq
,
1130 struct cs_etm_traceid_queue
*tidq
,
1131 u64 addr
, u64 period
)
1134 struct cs_etm_auxtrace
*etm
= etmq
->etm
;
1135 union perf_event
*event
= tidq
->event_buf
;
1136 struct perf_sample sample
= {.ip
= 0,};
1138 event
->sample
.header
.type
= PERF_RECORD_SAMPLE
;
1139 event
->sample
.header
.misc
= cs_etm__cpu_mode(etmq
, addr
);
1140 event
->sample
.header
.size
= sizeof(struct perf_event_header
);
1143 sample
.pid
= tidq
->pid
;
1144 sample
.tid
= tidq
->tid
;
1145 sample
.id
= etmq
->etm
->instructions_id
;
1146 sample
.stream_id
= etmq
->etm
->instructions_id
;
1147 sample
.period
= period
;
1148 sample
.cpu
= tidq
->packet
->cpu
;
1149 sample
.flags
= tidq
->prev_packet
->flags
;
1150 sample
.cpumode
= event
->sample
.header
.misc
;
1152 cs_etm__copy_insn(etmq
, tidq
->trace_chan_id
, tidq
->packet
, &sample
);
1154 if (etm
->synth_opts
.last_branch
)
1155 sample
.branch_stack
= tidq
->last_branch
;
1157 if (etm
->synth_opts
.inject
) {
1158 ret
= cs_etm__inject_event(event
, &sample
,
1159 etm
->instructions_sample_type
);
1164 ret
= perf_session__deliver_synth_event(etm
->session
, event
, &sample
);
1168 "CS ETM Trace: failed to deliver instruction event, error %d\n",
1175 * The cs etm packet encodes an instruction range between a branch target
1176 * and the next taken branch. Generate sample accordingly.
1178 static int cs_etm__synth_branch_sample(struct cs_etm_queue
*etmq
,
1179 struct cs_etm_traceid_queue
*tidq
)
1182 struct cs_etm_auxtrace
*etm
= etmq
->etm
;
1183 struct perf_sample sample
= {.ip
= 0,};
1184 union perf_event
*event
= tidq
->event_buf
;
1185 struct dummy_branch_stack
{
1188 struct branch_entry entries
;
1192 ip
= cs_etm__last_executed_instr(tidq
->prev_packet
);
1194 event
->sample
.header
.type
= PERF_RECORD_SAMPLE
;
1195 event
->sample
.header
.misc
= cs_etm__cpu_mode(etmq
, ip
);
1196 event
->sample
.header
.size
= sizeof(struct perf_event_header
);
1199 sample
.pid
= tidq
->pid
;
1200 sample
.tid
= tidq
->tid
;
1201 sample
.addr
= cs_etm__first_executed_instr(tidq
->packet
);
1202 sample
.id
= etmq
->etm
->branches_id
;
1203 sample
.stream_id
= etmq
->etm
->branches_id
;
1205 sample
.cpu
= tidq
->packet
->cpu
;
1206 sample
.flags
= tidq
->prev_packet
->flags
;
1207 sample
.cpumode
= event
->sample
.header
.misc
;
1209 cs_etm__copy_insn(etmq
, tidq
->trace_chan_id
, tidq
->prev_packet
,
1213 * perf report cannot handle events without a branch stack
1215 if (etm
->synth_opts
.last_branch
) {
1216 dummy_bs
= (struct dummy_branch_stack
){
1224 sample
.branch_stack
= (struct branch_stack
*)&dummy_bs
;
1227 if (etm
->synth_opts
.inject
) {
1228 ret
= cs_etm__inject_event(event
, &sample
,
1229 etm
->branches_sample_type
);
1234 ret
= perf_session__deliver_synth_event(etm
->session
, event
, &sample
);
1238 "CS ETM Trace: failed to deliver instruction event, error %d\n",
1244 struct cs_etm_synth
{
1245 struct perf_tool dummy_tool
;
1246 struct perf_session
*session
;
1249 static int cs_etm__event_synth(struct perf_tool
*tool
,
1250 union perf_event
*event
,
1251 struct perf_sample
*sample __maybe_unused
,
1252 struct machine
*machine __maybe_unused
)
1254 struct cs_etm_synth
*cs_etm_synth
=
1255 container_of(tool
, struct cs_etm_synth
, dummy_tool
);
1257 return perf_session__deliver_synth_event(cs_etm_synth
->session
,
1261 static int cs_etm__synth_event(struct perf_session
*session
,
1262 struct perf_event_attr
*attr
, u64 id
)
1264 struct cs_etm_synth cs_etm_synth
;
1266 memset(&cs_etm_synth
, 0, sizeof(struct cs_etm_synth
));
1267 cs_etm_synth
.session
= session
;
1269 return perf_event__synthesize_attr(&cs_etm_synth
.dummy_tool
, attr
, 1,
1270 &id
, cs_etm__event_synth
);
1273 static int cs_etm__synth_events(struct cs_etm_auxtrace
*etm
,
1274 struct perf_session
*session
)
1276 struct evlist
*evlist
= session
->evlist
;
1277 struct evsel
*evsel
;
1278 struct perf_event_attr attr
;
1283 evlist__for_each_entry(evlist
, evsel
) {
1284 if (evsel
->core
.attr
.type
== etm
->pmu_type
) {
1291 pr_debug("No selected events with CoreSight Trace data\n");
1295 memset(&attr
, 0, sizeof(struct perf_event_attr
));
1296 attr
.size
= sizeof(struct perf_event_attr
);
1297 attr
.type
= PERF_TYPE_HARDWARE
;
1298 attr
.sample_type
= evsel
->core
.attr
.sample_type
& PERF_SAMPLE_MASK
;
1299 attr
.sample_type
|= PERF_SAMPLE_IP
| PERF_SAMPLE_TID
|
1301 if (etm
->timeless_decoding
)
1302 attr
.sample_type
&= ~(u64
)PERF_SAMPLE_TIME
;
1304 attr
.sample_type
|= PERF_SAMPLE_TIME
;
1306 attr
.exclude_user
= evsel
->core
.attr
.exclude_user
;
1307 attr
.exclude_kernel
= evsel
->core
.attr
.exclude_kernel
;
1308 attr
.exclude_hv
= evsel
->core
.attr
.exclude_hv
;
1309 attr
.exclude_host
= evsel
->core
.attr
.exclude_host
;
1310 attr
.exclude_guest
= evsel
->core
.attr
.exclude_guest
;
1311 attr
.sample_id_all
= evsel
->core
.attr
.sample_id_all
;
1312 attr
.read_format
= evsel
->core
.attr
.read_format
;
1314 /* create new id val to be a fixed offset from evsel id */
1315 id
= evsel
->core
.id
[0] + 1000000000;
1320 if (etm
->synth_opts
.branches
) {
1321 attr
.config
= PERF_COUNT_HW_BRANCH_INSTRUCTIONS
;
1322 attr
.sample_period
= 1;
1323 attr
.sample_type
|= PERF_SAMPLE_ADDR
;
1324 err
= cs_etm__synth_event(session
, &attr
, id
);
1327 etm
->sample_branches
= true;
1328 etm
->branches_sample_type
= attr
.sample_type
;
1329 etm
->branches_id
= id
;
1331 attr
.sample_type
&= ~(u64
)PERF_SAMPLE_ADDR
;
1334 if (etm
->synth_opts
.last_branch
)
1335 attr
.sample_type
|= PERF_SAMPLE_BRANCH_STACK
;
1337 if (etm
->synth_opts
.instructions
) {
1338 attr
.config
= PERF_COUNT_HW_INSTRUCTIONS
;
1339 attr
.sample_period
= etm
->synth_opts
.period
;
1340 etm
->instructions_sample_period
= attr
.sample_period
;
1341 err
= cs_etm__synth_event(session
, &attr
, id
);
1344 etm
->sample_instructions
= true;
1345 etm
->instructions_sample_type
= attr
.sample_type
;
1346 etm
->instructions_id
= id
;
1353 static int cs_etm__sample(struct cs_etm_queue
*etmq
,
1354 struct cs_etm_traceid_queue
*tidq
)
1356 struct cs_etm_auxtrace
*etm
= etmq
->etm
;
1358 u8 trace_chan_id
= tidq
->trace_chan_id
;
1361 /* Get instructions remainder from previous packet */
1362 instrs_prev
= tidq
->period_instructions
;
1364 tidq
->period_instructions
+= tidq
->packet
->instr_count
;
1367 * Record a branch when the last instruction in
1368 * PREV_PACKET is a branch.
1370 if (etm
->synth_opts
.last_branch
&&
1371 tidq
->prev_packet
->sample_type
== CS_ETM_RANGE
&&
1372 tidq
->prev_packet
->last_instr_taken_branch
)
1373 cs_etm__update_last_branch_rb(etmq
, tidq
);
1375 if (etm
->sample_instructions
&&
1376 tidq
->period_instructions
>= etm
->instructions_sample_period
) {
1378 * Emit instruction sample periodically
1379 * TODO: allow period to be defined in cycles and clock time
1383 * Below diagram demonstrates the instruction samples
1386 * Instrs Instrs Instrs Instrs
1387 * Sample(n) Sample(n+1) Sample(n+2) Sample(n+3)
1390 * --------------------------------------------------
1394 * instructions(Pi) instructions(Pi')
1397 * \---------------- -----------------/
1399 * tidq->packet->instr_count
1401 * Instrs Sample(n...) are the synthesised samples occurring
1402 * every etm->instructions_sample_period instructions - as
1403 * defined on the perf command line. Sample(n) is being the
1404 * last sample before the current etm packet, n+1 to n+3
1405 * samples are generated from the current etm packet.
1407 * tidq->packet->instr_count represents the number of
1408 * instructions in the current etm packet.
1410 * Period instructions (Pi) contains the the number of
1411 * instructions executed after the sample point(n) from the
1412 * previous etm packet. This will always be less than
1413 * etm->instructions_sample_period.
1415 * When generate new samples, it combines with two parts
1416 * instructions, one is the tail of the old packet and another
1417 * is the head of the new coming packet, to generate
1418 * sample(n+1); sample(n+2) and sample(n+3) consume the
1419 * instructions with sample period. After sample(n+3), the rest
1420 * instructions will be used by later packet and it is assigned
1421 * to tidq->period_instructions for next round calculation.
1425 * Get the initial offset into the current packet instructions;
1426 * entry conditions ensure that instrs_prev is less than
1427 * etm->instructions_sample_period.
1429 u64 offset
= etm
->instructions_sample_period
- instrs_prev
;
1432 /* Prepare last branches for instruction sample */
1433 if (etm
->synth_opts
.last_branch
)
1434 cs_etm__copy_last_branch_rb(etmq
, tidq
);
1436 while (tidq
->period_instructions
>=
1437 etm
->instructions_sample_period
) {
1439 * Calculate the address of the sampled instruction (-1
1440 * as sample is reported as though instruction has just
1441 * been executed, but PC has not advanced to next
1444 addr
= cs_etm__instr_addr(etmq
, trace_chan_id
,
1445 tidq
->packet
, offset
- 1);
1446 ret
= cs_etm__synth_instruction_sample(
1448 etm
->instructions_sample_period
);
1452 offset
+= etm
->instructions_sample_period
;
1453 tidq
->period_instructions
-=
1454 etm
->instructions_sample_period
;
1458 if (etm
->sample_branches
) {
1459 bool generate_sample
= false;
1461 /* Generate sample for tracing on packet */
1462 if (tidq
->prev_packet
->sample_type
== CS_ETM_DISCONTINUITY
)
1463 generate_sample
= true;
1465 /* Generate sample for branch taken packet */
1466 if (tidq
->prev_packet
->sample_type
== CS_ETM_RANGE
&&
1467 tidq
->prev_packet
->last_instr_taken_branch
)
1468 generate_sample
= true;
1470 if (generate_sample
) {
1471 ret
= cs_etm__synth_branch_sample(etmq
, tidq
);
1477 cs_etm__packet_swap(etm
, tidq
);
1482 static int cs_etm__exception(struct cs_etm_traceid_queue
*tidq
)
1485 * When the exception packet is inserted, whether the last instruction
1486 * in previous range packet is taken branch or not, we need to force
1487 * to set 'prev_packet->last_instr_taken_branch' to true. This ensures
1488 * to generate branch sample for the instruction range before the
1489 * exception is trapped to kernel or before the exception returning.
1491 * The exception packet includes the dummy address values, so don't
1492 * swap PACKET with PREV_PACKET. This keeps PREV_PACKET to be useful
1493 * for generating instruction and branch samples.
1495 if (tidq
->prev_packet
->sample_type
== CS_ETM_RANGE
)
1496 tidq
->prev_packet
->last_instr_taken_branch
= true;
1501 static int cs_etm__flush(struct cs_etm_queue
*etmq
,
1502 struct cs_etm_traceid_queue
*tidq
)
1505 struct cs_etm_auxtrace
*etm
= etmq
->etm
;
1507 /* Handle start tracing packet */
1508 if (tidq
->prev_packet
->sample_type
== CS_ETM_EMPTY
)
1511 if (etmq
->etm
->synth_opts
.last_branch
&&
1512 tidq
->prev_packet
->sample_type
== CS_ETM_RANGE
) {
1515 /* Prepare last branches for instruction sample */
1516 cs_etm__copy_last_branch_rb(etmq
, tidq
);
1519 * Generate a last branch event for the branches left in the
1520 * circular buffer at the end of the trace.
1522 * Use the address of the end of the last reported execution
1525 addr
= cs_etm__last_executed_instr(tidq
->prev_packet
);
1527 err
= cs_etm__synth_instruction_sample(
1529 tidq
->period_instructions
);
1533 tidq
->period_instructions
= 0;
1537 if (etm
->sample_branches
&&
1538 tidq
->prev_packet
->sample_type
== CS_ETM_RANGE
) {
1539 err
= cs_etm__synth_branch_sample(etmq
, tidq
);
1545 cs_etm__packet_swap(etm
, tidq
);
1547 /* Reset last branches after flush the trace */
1548 if (etm
->synth_opts
.last_branch
)
1549 cs_etm__reset_last_branch_rb(tidq
);
1554 static int cs_etm__end_block(struct cs_etm_queue
*etmq
,
1555 struct cs_etm_traceid_queue
*tidq
)
1560 * It has no new packet coming and 'etmq->packet' contains the stale
1561 * packet which was set at the previous time with packets swapping;
1562 * so skip to generate branch sample to avoid stale packet.
1564 * For this case only flush branch stack and generate a last branch
1565 * event for the branches left in the circular buffer at the end of
1568 if (etmq
->etm
->synth_opts
.last_branch
&&
1569 tidq
->prev_packet
->sample_type
== CS_ETM_RANGE
) {
1572 /* Prepare last branches for instruction sample */
1573 cs_etm__copy_last_branch_rb(etmq
, tidq
);
1576 * Use the address of the end of the last reported execution
1579 addr
= cs_etm__last_executed_instr(tidq
->prev_packet
);
1581 err
= cs_etm__synth_instruction_sample(
1583 tidq
->period_instructions
);
1587 tidq
->period_instructions
= 0;
1593 * cs_etm__get_data_block: Fetch a block from the auxtrace_buffer queue
1595 * Returns: < 0 if error
1596 * = 0 if no more auxtrace_buffer to read
1597 * > 0 if the current buffer isn't empty yet
1599 static int cs_etm__get_data_block(struct cs_etm_queue
*etmq
)
1603 if (!etmq
->buf_len
) {
1604 ret
= cs_etm__get_trace(etmq
);
1608 * We cannot assume consecutive blocks in the data file
1609 * are contiguous, reset the decoder to force re-sync.
1611 ret
= cs_etm_decoder__reset(etmq
->decoder
);
1616 return etmq
->buf_len
;
1619 static bool cs_etm__is_svc_instr(struct cs_etm_queue
*etmq
, u8 trace_chan_id
,
1620 struct cs_etm_packet
*packet
,
1623 /* Initialise to keep compiler happy */
1628 switch (packet
->isa
) {
1629 case CS_ETM_ISA_T32
:
1631 * The SVC of T32 is defined in ARM DDI 0487D.a, F5.1.247:
1634 * +-----------------+--------+
1635 * | 1 1 0 1 1 1 1 1 | imm8 |
1636 * +-----------------+--------+
1638 * According to the specifiction, it only defines SVC for T32
1639 * with 16 bits instruction and has no definition for 32bits;
1640 * so below only read 2 bytes as instruction size for T32.
1642 addr
= end_addr
- 2;
1643 cs_etm__mem_access(etmq
, trace_chan_id
, addr
,
1644 sizeof(instr16
), (u8
*)&instr16
);
1645 if ((instr16
& 0xFF00) == 0xDF00)
1649 case CS_ETM_ISA_A32
:
1651 * The SVC of A32 is defined in ARM DDI 0487D.a, F5.1.247:
1653 * b'31 b'28 b'27 b'24
1654 * +---------+---------+-------------------------+
1655 * | !1111 | 1 1 1 1 | imm24 |
1656 * +---------+---------+-------------------------+
1658 addr
= end_addr
- 4;
1659 cs_etm__mem_access(etmq
, trace_chan_id
, addr
,
1660 sizeof(instr32
), (u8
*)&instr32
);
1661 if ((instr32
& 0x0F000000) == 0x0F000000 &&
1662 (instr32
& 0xF0000000) != 0xF0000000)
1666 case CS_ETM_ISA_A64
:
1668 * The SVC of A64 is defined in ARM DDI 0487D.a, C6.2.294:
1671 * +-----------------------+---------+-----------+
1672 * | 1 1 0 1 0 1 0 0 0 0 0 | imm16 | 0 0 0 0 1 |
1673 * +-----------------------+---------+-----------+
1675 addr
= end_addr
- 4;
1676 cs_etm__mem_access(etmq
, trace_chan_id
, addr
,
1677 sizeof(instr32
), (u8
*)&instr32
);
1678 if ((instr32
& 0xFFE0001F) == 0xd4000001)
1682 case CS_ETM_ISA_UNKNOWN
:
1690 static bool cs_etm__is_syscall(struct cs_etm_queue
*etmq
,
1691 struct cs_etm_traceid_queue
*tidq
, u64 magic
)
1693 u8 trace_chan_id
= tidq
->trace_chan_id
;
1694 struct cs_etm_packet
*packet
= tidq
->packet
;
1695 struct cs_etm_packet
*prev_packet
= tidq
->prev_packet
;
1697 if (magic
== __perf_cs_etmv3_magic
)
1698 if (packet
->exception_number
== CS_ETMV3_EXC_SVC
)
1702 * ETMv4 exception type CS_ETMV4_EXC_CALL covers SVC, SMC and
1703 * HVC cases; need to check if it's SVC instruction based on
1706 if (magic
== __perf_cs_etmv4_magic
) {
1707 if (packet
->exception_number
== CS_ETMV4_EXC_CALL
&&
1708 cs_etm__is_svc_instr(etmq
, trace_chan_id
, prev_packet
,
1709 prev_packet
->end_addr
))
1716 static bool cs_etm__is_async_exception(struct cs_etm_traceid_queue
*tidq
,
1719 struct cs_etm_packet
*packet
= tidq
->packet
;
1721 if (magic
== __perf_cs_etmv3_magic
)
1722 if (packet
->exception_number
== CS_ETMV3_EXC_DEBUG_HALT
||
1723 packet
->exception_number
== CS_ETMV3_EXC_ASYNC_DATA_ABORT
||
1724 packet
->exception_number
== CS_ETMV3_EXC_PE_RESET
||
1725 packet
->exception_number
== CS_ETMV3_EXC_IRQ
||
1726 packet
->exception_number
== CS_ETMV3_EXC_FIQ
)
1729 if (magic
== __perf_cs_etmv4_magic
)
1730 if (packet
->exception_number
== CS_ETMV4_EXC_RESET
||
1731 packet
->exception_number
== CS_ETMV4_EXC_DEBUG_HALT
||
1732 packet
->exception_number
== CS_ETMV4_EXC_SYSTEM_ERROR
||
1733 packet
->exception_number
== CS_ETMV4_EXC_INST_DEBUG
||
1734 packet
->exception_number
== CS_ETMV4_EXC_DATA_DEBUG
||
1735 packet
->exception_number
== CS_ETMV4_EXC_IRQ
||
1736 packet
->exception_number
== CS_ETMV4_EXC_FIQ
)
1742 static bool cs_etm__is_sync_exception(struct cs_etm_queue
*etmq
,
1743 struct cs_etm_traceid_queue
*tidq
,
1746 u8 trace_chan_id
= tidq
->trace_chan_id
;
1747 struct cs_etm_packet
*packet
= tidq
->packet
;
1748 struct cs_etm_packet
*prev_packet
= tidq
->prev_packet
;
1750 if (magic
== __perf_cs_etmv3_magic
)
1751 if (packet
->exception_number
== CS_ETMV3_EXC_SMC
||
1752 packet
->exception_number
== CS_ETMV3_EXC_HYP
||
1753 packet
->exception_number
== CS_ETMV3_EXC_JAZELLE_THUMBEE
||
1754 packet
->exception_number
== CS_ETMV3_EXC_UNDEFINED_INSTR
||
1755 packet
->exception_number
== CS_ETMV3_EXC_PREFETCH_ABORT
||
1756 packet
->exception_number
== CS_ETMV3_EXC_DATA_FAULT
||
1757 packet
->exception_number
== CS_ETMV3_EXC_GENERIC
)
1760 if (magic
== __perf_cs_etmv4_magic
) {
1761 if (packet
->exception_number
== CS_ETMV4_EXC_TRAP
||
1762 packet
->exception_number
== CS_ETMV4_EXC_ALIGNMENT
||
1763 packet
->exception_number
== CS_ETMV4_EXC_INST_FAULT
||
1764 packet
->exception_number
== CS_ETMV4_EXC_DATA_FAULT
)
1768 * For CS_ETMV4_EXC_CALL, except SVC other instructions
1769 * (SMC, HVC) are taken as sync exceptions.
1771 if (packet
->exception_number
== CS_ETMV4_EXC_CALL
&&
1772 !cs_etm__is_svc_instr(etmq
, trace_chan_id
, prev_packet
,
1773 prev_packet
->end_addr
))
1777 * ETMv4 has 5 bits for exception number; if the numbers
1778 * are in the range ( CS_ETMV4_EXC_FIQ, CS_ETMV4_EXC_END ]
1779 * they are implementation defined exceptions.
1781 * For this case, simply take it as sync exception.
1783 if (packet
->exception_number
> CS_ETMV4_EXC_FIQ
&&
1784 packet
->exception_number
<= CS_ETMV4_EXC_END
)
1791 static int cs_etm__set_sample_flags(struct cs_etm_queue
*etmq
,
1792 struct cs_etm_traceid_queue
*tidq
)
1794 struct cs_etm_packet
*packet
= tidq
->packet
;
1795 struct cs_etm_packet
*prev_packet
= tidq
->prev_packet
;
1796 u8 trace_chan_id
= tidq
->trace_chan_id
;
1800 switch (packet
->sample_type
) {
1803 * Immediate branch instruction without neither link nor
1804 * return flag, it's normal branch instruction within
1807 if (packet
->last_instr_type
== OCSD_INSTR_BR
&&
1808 packet
->last_instr_subtype
== OCSD_S_INSTR_NONE
) {
1809 packet
->flags
= PERF_IP_FLAG_BRANCH
;
1811 if (packet
->last_instr_cond
)
1812 packet
->flags
|= PERF_IP_FLAG_CONDITIONAL
;
1816 * Immediate branch instruction with link (e.g. BL), this is
1817 * branch instruction for function call.
1819 if (packet
->last_instr_type
== OCSD_INSTR_BR
&&
1820 packet
->last_instr_subtype
== OCSD_S_INSTR_BR_LINK
)
1821 packet
->flags
= PERF_IP_FLAG_BRANCH
|
1825 * Indirect branch instruction with link (e.g. BLR), this is
1826 * branch instruction for function call.
1828 if (packet
->last_instr_type
== OCSD_INSTR_BR_INDIRECT
&&
1829 packet
->last_instr_subtype
== OCSD_S_INSTR_BR_LINK
)
1830 packet
->flags
= PERF_IP_FLAG_BRANCH
|
1834 * Indirect branch instruction with subtype of
1835 * OCSD_S_INSTR_V7_IMPLIED_RET, this is explicit hint for
1836 * function return for A32/T32.
1838 if (packet
->last_instr_type
== OCSD_INSTR_BR_INDIRECT
&&
1839 packet
->last_instr_subtype
== OCSD_S_INSTR_V7_IMPLIED_RET
)
1840 packet
->flags
= PERF_IP_FLAG_BRANCH
|
1841 PERF_IP_FLAG_RETURN
;
1844 * Indirect branch instruction without link (e.g. BR), usually
1845 * this is used for function return, especially for functions
1846 * within dynamic link lib.
1848 if (packet
->last_instr_type
== OCSD_INSTR_BR_INDIRECT
&&
1849 packet
->last_instr_subtype
== OCSD_S_INSTR_NONE
)
1850 packet
->flags
= PERF_IP_FLAG_BRANCH
|
1851 PERF_IP_FLAG_RETURN
;
1853 /* Return instruction for function return. */
1854 if (packet
->last_instr_type
== OCSD_INSTR_BR_INDIRECT
&&
1855 packet
->last_instr_subtype
== OCSD_S_INSTR_V8_RET
)
1856 packet
->flags
= PERF_IP_FLAG_BRANCH
|
1857 PERF_IP_FLAG_RETURN
;
1860 * Decoder might insert a discontinuity in the middle of
1861 * instruction packets, fixup prev_packet with flag
1862 * PERF_IP_FLAG_TRACE_BEGIN to indicate restarting trace.
1864 if (prev_packet
->sample_type
== CS_ETM_DISCONTINUITY
)
1865 prev_packet
->flags
|= PERF_IP_FLAG_BRANCH
|
1866 PERF_IP_FLAG_TRACE_BEGIN
;
1869 * If the previous packet is an exception return packet
1870 * and the return address just follows SVC instuction,
1871 * it needs to calibrate the previous packet sample flags
1872 * as PERF_IP_FLAG_SYSCALLRET.
1874 if (prev_packet
->flags
== (PERF_IP_FLAG_BRANCH
|
1875 PERF_IP_FLAG_RETURN
|
1876 PERF_IP_FLAG_INTERRUPT
) &&
1877 cs_etm__is_svc_instr(etmq
, trace_chan_id
,
1878 packet
, packet
->start_addr
))
1879 prev_packet
->flags
= PERF_IP_FLAG_BRANCH
|
1880 PERF_IP_FLAG_RETURN
|
1881 PERF_IP_FLAG_SYSCALLRET
;
1883 case CS_ETM_DISCONTINUITY
:
1885 * The trace is discontinuous, if the previous packet is
1886 * instruction packet, set flag PERF_IP_FLAG_TRACE_END
1887 * for previous packet.
1889 if (prev_packet
->sample_type
== CS_ETM_RANGE
)
1890 prev_packet
->flags
|= PERF_IP_FLAG_BRANCH
|
1891 PERF_IP_FLAG_TRACE_END
;
1893 case CS_ETM_EXCEPTION
:
1894 ret
= cs_etm__get_magic(packet
->trace_chan_id
, &magic
);
1898 /* The exception is for system call. */
1899 if (cs_etm__is_syscall(etmq
, tidq
, magic
))
1900 packet
->flags
= PERF_IP_FLAG_BRANCH
|
1902 PERF_IP_FLAG_SYSCALLRET
;
1904 * The exceptions are triggered by external signals from bus,
1905 * interrupt controller, debug module, PE reset or halt.
1907 else if (cs_etm__is_async_exception(tidq
, magic
))
1908 packet
->flags
= PERF_IP_FLAG_BRANCH
|
1910 PERF_IP_FLAG_ASYNC
|
1911 PERF_IP_FLAG_INTERRUPT
;
1913 * Otherwise, exception is caused by trap, instruction &
1914 * data fault, or alignment errors.
1916 else if (cs_etm__is_sync_exception(etmq
, tidq
, magic
))
1917 packet
->flags
= PERF_IP_FLAG_BRANCH
|
1919 PERF_IP_FLAG_INTERRUPT
;
1922 * When the exception packet is inserted, since exception
1923 * packet is not used standalone for generating samples
1924 * and it's affiliation to the previous instruction range
1925 * packet; so set previous range packet flags to tell perf
1926 * it is an exception taken branch.
1928 if (prev_packet
->sample_type
== CS_ETM_RANGE
)
1929 prev_packet
->flags
= packet
->flags
;
1931 case CS_ETM_EXCEPTION_RET
:
1933 * When the exception return packet is inserted, since
1934 * exception return packet is not used standalone for
1935 * generating samples and it's affiliation to the previous
1936 * instruction range packet; so set previous range packet
1937 * flags to tell perf it is an exception return branch.
1939 * The exception return can be for either system call or
1940 * other exception types; unfortunately the packet doesn't
1941 * contain exception type related info so we cannot decide
1942 * the exception type purely based on exception return packet.
1943 * If we record the exception number from exception packet and
1944 * reuse it for excpetion return packet, this is not reliable
1945 * due the trace can be discontinuity or the interrupt can
1946 * be nested, thus the recorded exception number cannot be
1947 * used for exception return packet for these two cases.
1949 * For exception return packet, we only need to distinguish the
1950 * packet is for system call or for other types. Thus the
1951 * decision can be deferred when receive the next packet which
1952 * contains the return address, based on the return address we
1953 * can read out the previous instruction and check if it's a
1954 * system call instruction and then calibrate the sample flag
1957 if (prev_packet
->sample_type
== CS_ETM_RANGE
)
1958 prev_packet
->flags
= PERF_IP_FLAG_BRANCH
|
1959 PERF_IP_FLAG_RETURN
|
1960 PERF_IP_FLAG_INTERRUPT
;
1970 static int cs_etm__decode_data_block(struct cs_etm_queue
*etmq
)
1973 size_t processed
= 0;
1976 * Packets are decoded and added to the decoder's packet queue
1977 * until the decoder packet processing callback has requested that
1978 * processing stops or there is nothing left in the buffer. Normal
1979 * operations that stop processing are a timestamp packet or a full
1980 * decoder buffer queue.
1982 ret
= cs_etm_decoder__process_data_block(etmq
->decoder
,
1984 &etmq
->buf
[etmq
->buf_used
],
1990 etmq
->offset
+= processed
;
1991 etmq
->buf_used
+= processed
;
1992 etmq
->buf_len
-= processed
;
1998 static int cs_etm__process_traceid_queue(struct cs_etm_queue
*etmq
,
1999 struct cs_etm_traceid_queue
*tidq
)
2002 struct cs_etm_packet_queue
*packet_queue
;
2004 packet_queue
= &tidq
->packet_queue
;
2006 /* Process each packet in this chunk */
2008 ret
= cs_etm_decoder__get_packet(packet_queue
,
2012 * Stop processing this chunk on
2013 * end of data or error
2018 * Since packet addresses are swapped in packet
2019 * handling within below switch() statements,
2020 * thus setting sample flags must be called
2021 * prior to switch() statement to use address
2022 * information before packets swapping.
2024 ret
= cs_etm__set_sample_flags(etmq
, tidq
);
2028 switch (tidq
->packet
->sample_type
) {
2031 * If the packet contains an instruction
2032 * range, generate instruction sequence
2035 cs_etm__sample(etmq
, tidq
);
2037 case CS_ETM_EXCEPTION
:
2038 case CS_ETM_EXCEPTION_RET
:
2040 * If the exception packet is coming,
2041 * make sure the previous instruction
2042 * range packet to be handled properly.
2044 cs_etm__exception(tidq
);
2046 case CS_ETM_DISCONTINUITY
:
2048 * Discontinuity in trace, flush
2049 * previous branch stack
2051 cs_etm__flush(etmq
, tidq
);
2055 * Should not receive empty packet,
2058 pr_err("CS ETM Trace: empty packet\n");
2068 static void cs_etm__clear_all_traceid_queues(struct cs_etm_queue
*etmq
)
2071 struct int_node
*inode
;
2072 struct cs_etm_traceid_queue
*tidq
;
2073 struct intlist
*traceid_queues_list
= etmq
->traceid_queues_list
;
2075 intlist__for_each_entry(inode
, traceid_queues_list
) {
2076 idx
= (int)(intptr_t)inode
->priv
;
2077 tidq
= etmq
->traceid_queues
[idx
];
2079 /* Ignore return value */
2080 cs_etm__process_traceid_queue(etmq
, tidq
);
2083 * Generate an instruction sample with the remaining
2084 * branchstack entries.
2086 cs_etm__flush(etmq
, tidq
);
2090 static int cs_etm__run_decoder(struct cs_etm_queue
*etmq
)
2093 struct cs_etm_traceid_queue
*tidq
;
2095 tidq
= cs_etm__etmq_get_traceid_queue(etmq
, CS_ETM_PER_THREAD_TRACEID
);
2099 /* Go through each buffer in the queue and decode them one by one */
2101 err
= cs_etm__get_data_block(etmq
);
2105 /* Run trace decoder until buffer consumed or end of trace */
2107 err
= cs_etm__decode_data_block(etmq
);
2112 * Process each packet in this chunk, nothing to do if
2113 * an error occurs other than hoping the next one will
2116 err
= cs_etm__process_traceid_queue(etmq
, tidq
);
2118 } while (etmq
->buf_len
);
2121 /* Flush any remaining branch stack entries */
2122 err
= cs_etm__end_block(etmq
, tidq
);
2128 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace
*etm
,
2132 struct auxtrace_queues
*queues
= &etm
->queues
;
2134 for (i
= 0; i
< queues
->nr_queues
; i
++) {
2135 struct auxtrace_queue
*queue
= &etm
->queues
.queue_array
[i
];
2136 struct cs_etm_queue
*etmq
= queue
->priv
;
2137 struct cs_etm_traceid_queue
*tidq
;
2142 tidq
= cs_etm__etmq_get_traceid_queue(etmq
,
2143 CS_ETM_PER_THREAD_TRACEID
);
2148 if ((tid
== -1) || (tidq
->tid
== tid
)) {
2149 cs_etm__set_pid_tid_cpu(etm
, tidq
);
2150 cs_etm__run_decoder(etmq
);
2157 static int cs_etm__process_queues(struct cs_etm_auxtrace
*etm
)
2160 unsigned int cs_queue_nr
, queue_nr
;
2163 struct auxtrace_queue
*queue
;
2164 struct cs_etm_queue
*etmq
;
2165 struct cs_etm_traceid_queue
*tidq
;
2168 if (!etm
->heap
.heap_cnt
)
2171 /* Take the entry at the top of the min heap */
2172 cs_queue_nr
= etm
->heap
.heap_array
[0].queue_nr
;
2173 queue_nr
= TO_QUEUE_NR(cs_queue_nr
);
2174 trace_chan_id
= TO_TRACE_CHAN_ID(cs_queue_nr
);
2175 queue
= &etm
->queues
.queue_array
[queue_nr
];
2179 * Remove the top entry from the heap since we are about
2182 auxtrace_heap__pop(&etm
->heap
);
2184 tidq
= cs_etm__etmq_get_traceid_queue(etmq
, trace_chan_id
);
2187 * No traceID queue has been allocated for this traceID,
2188 * which means something somewhere went very wrong. No
2189 * other choice than simply exit.
2196 * Packets associated with this timestamp are already in
2197 * the etmq's traceID queue, so process them.
2199 ret
= cs_etm__process_traceid_queue(etmq
, tidq
);
2204 * Packets for this timestamp have been processed, time to
2205 * move on to the next timestamp, fetching a new auxtrace_buffer
2209 ret
= cs_etm__get_data_block(etmq
);
2214 * No more auxtrace_buffers to process in this etmq, simply
2215 * move on to another entry in the auxtrace_heap.
2220 ret
= cs_etm__decode_data_block(etmq
);
2224 timestamp
= cs_etm__etmq_get_timestamp(etmq
, &trace_chan_id
);
2228 * Function cs_etm__decode_data_block() returns when
2229 * there is no more traces to decode in the current
2230 * auxtrace_buffer OR when a timestamp has been
2231 * encountered on any of the traceID queues. Since we
2232 * did not get a timestamp, there is no more traces to
2233 * process in this auxtrace_buffer. As such empty and
2234 * flush all traceID queues.
2236 cs_etm__clear_all_traceid_queues(etmq
);
2238 /* Fetch another auxtrace_buffer for this etmq */
2243 * Add to the min heap the timestamp for packets that have
2244 * just been decoded. They will be processed and synthesized
2245 * during the next call to cs_etm__process_traceid_queue() for
2246 * this queue/traceID.
2248 cs_queue_nr
= TO_CS_QUEUE_NR(queue_nr
, trace_chan_id
);
2249 ret
= auxtrace_heap__add(&etm
->heap
, cs_queue_nr
, timestamp
);
2256 static int cs_etm__process_itrace_start(struct cs_etm_auxtrace
*etm
,
2257 union perf_event
*event
)
2261 if (etm
->timeless_decoding
)
2265 * Add the tid/pid to the log so that we can get a match when
2266 * we get a contextID from the decoder.
2268 th
= machine__findnew_thread(etm
->machine
,
2269 event
->itrace_start
.pid
,
2270 event
->itrace_start
.tid
);
2279 static int cs_etm__process_switch_cpu_wide(struct cs_etm_auxtrace
*etm
,
2280 union perf_event
*event
)
2283 bool out
= event
->header
.misc
& PERF_RECORD_MISC_SWITCH_OUT
;
2286 * Context switch in per-thread mode are irrelevant since perf
2287 * will start/stop tracing as the process is scheduled.
2289 if (etm
->timeless_decoding
)
2293 * SWITCH_IN events carry the next process to be switched out while
2294 * SWITCH_OUT events carry the process to be switched in. As such
2295 * we don't care about IN events.
2301 * Add the tid/pid to the log so that we can get a match when
2302 * we get a contextID from the decoder.
2304 th
= machine__findnew_thread(etm
->machine
,
2305 event
->context_switch
.next_prev_pid
,
2306 event
->context_switch
.next_prev_tid
);
2315 static int cs_etm__process_event(struct perf_session
*session
,
2316 union perf_event
*event
,
2317 struct perf_sample
*sample
,
2318 struct perf_tool
*tool
)
2322 struct cs_etm_auxtrace
*etm
= container_of(session
->auxtrace
,
2323 struct cs_etm_auxtrace
,
2329 if (!tool
->ordered_events
) {
2330 pr_err("CoreSight ETM Trace requires ordered events\n");
2334 if (sample
->time
&& (sample
->time
!= (u64
) -1))
2335 timestamp
= sample
->time
;
2339 if (timestamp
|| etm
->timeless_decoding
) {
2340 err
= cs_etm__update_queues(etm
);
2345 if (etm
->timeless_decoding
&&
2346 event
->header
.type
== PERF_RECORD_EXIT
)
2347 return cs_etm__process_timeless_queues(etm
,
2350 if (event
->header
.type
== PERF_RECORD_ITRACE_START
)
2351 return cs_etm__process_itrace_start(etm
, event
);
2352 else if (event
->header
.type
== PERF_RECORD_SWITCH_CPU_WIDE
)
2353 return cs_etm__process_switch_cpu_wide(etm
, event
);
2355 if (!etm
->timeless_decoding
&&
2356 event
->header
.type
== PERF_RECORD_AUX
)
2357 return cs_etm__process_queues(etm
);
2362 static int cs_etm__process_auxtrace_event(struct perf_session
*session
,
2363 union perf_event
*event
,
2364 struct perf_tool
*tool __maybe_unused
)
2366 struct cs_etm_auxtrace
*etm
= container_of(session
->auxtrace
,
2367 struct cs_etm_auxtrace
,
2369 if (!etm
->data_queued
) {
2370 struct auxtrace_buffer
*buffer
;
2372 int fd
= perf_data__fd(session
->data
);
2373 bool is_pipe
= perf_data__is_pipe(session
->data
);
2379 data_offset
= lseek(fd
, 0, SEEK_CUR
);
2380 if (data_offset
== -1)
2384 err
= auxtrace_queues__add_event(&etm
->queues
, session
,
2385 event
, data_offset
, &buffer
);
2390 if (auxtrace_buffer__get_data(buffer
, fd
)) {
2391 cs_etm__dump_event(etm
, buffer
);
2392 auxtrace_buffer__put_data(buffer
);
2399 static bool cs_etm__is_timeless_decoding(struct cs_etm_auxtrace
*etm
)
2401 struct evsel
*evsel
;
2402 struct evlist
*evlist
= etm
->session
->evlist
;
2403 bool timeless_decoding
= true;
2406 * Circle through the list of event and complain if we find one
2407 * with the time bit set.
2409 evlist__for_each_entry(evlist
, evsel
) {
2410 if ((evsel
->core
.attr
.sample_type
& PERF_SAMPLE_TIME
))
2411 timeless_decoding
= false;
2414 return timeless_decoding
;
2417 static const char * const cs_etm_global_header_fmts
[] = {
2418 [CS_HEADER_VERSION_0
] = " Header version %llx\n",
2419 [CS_PMU_TYPE_CPUS
] = " PMU type/num cpus %llx\n",
2420 [CS_ETM_SNAPSHOT
] = " Snapshot %llx\n",
2423 static const char * const cs_etm_priv_fmts
[] = {
2424 [CS_ETM_MAGIC
] = " Magic number %llx\n",
2425 [CS_ETM_CPU
] = " CPU %lld\n",
2426 [CS_ETM_ETMCR
] = " ETMCR %llx\n",
2427 [CS_ETM_ETMTRACEIDR
] = " ETMTRACEIDR %llx\n",
2428 [CS_ETM_ETMCCER
] = " ETMCCER %llx\n",
2429 [CS_ETM_ETMIDR
] = " ETMIDR %llx\n",
2432 static const char * const cs_etmv4_priv_fmts
[] = {
2433 [CS_ETM_MAGIC
] = " Magic number %llx\n",
2434 [CS_ETM_CPU
] = " CPU %lld\n",
2435 [CS_ETMV4_TRCCONFIGR
] = " TRCCONFIGR %llx\n",
2436 [CS_ETMV4_TRCTRACEIDR
] = " TRCTRACEIDR %llx\n",
2437 [CS_ETMV4_TRCIDR0
] = " TRCIDR0 %llx\n",
2438 [CS_ETMV4_TRCIDR1
] = " TRCIDR1 %llx\n",
2439 [CS_ETMV4_TRCIDR2
] = " TRCIDR2 %llx\n",
2440 [CS_ETMV4_TRCIDR8
] = " TRCIDR8 %llx\n",
2441 [CS_ETMV4_TRCAUTHSTATUS
] = " TRCAUTHSTATUS %llx\n",
2444 static void cs_etm__print_auxtrace_info(__u64
*val
, int num
)
2448 for (i
= 0; i
< CS_HEADER_VERSION_0_MAX
; i
++)
2449 fprintf(stdout
, cs_etm_global_header_fmts
[i
], val
[i
]);
2451 for (i
= CS_HEADER_VERSION_0_MAX
; cpu
< num
; cpu
++) {
2452 if (val
[i
] == __perf_cs_etmv3_magic
)
2453 for (j
= 0; j
< CS_ETM_PRIV_MAX
; j
++, i
++)
2454 fprintf(stdout
, cs_etm_priv_fmts
[j
], val
[i
]);
2455 else if (val
[i
] == __perf_cs_etmv4_magic
)
2456 for (j
= 0; j
< CS_ETMV4_PRIV_MAX
; j
++, i
++)
2457 fprintf(stdout
, cs_etmv4_priv_fmts
[j
], val
[i
]);
2459 /* failure.. return */
2464 int cs_etm__process_auxtrace_info(union perf_event
*event
,
2465 struct perf_session
*session
)
2467 struct perf_record_auxtrace_info
*auxtrace_info
= &event
->auxtrace_info
;
2468 struct cs_etm_auxtrace
*etm
= NULL
;
2469 struct int_node
*inode
;
2470 unsigned int pmu_type
;
2471 int event_header_size
= sizeof(struct perf_event_header
);
2472 int info_header_size
;
2473 int total_size
= auxtrace_info
->header
.size
;
2476 int err
= 0, idx
= -1;
2478 u64
*ptr
, *hdr
= NULL
;
2479 u64
**metadata
= NULL
;
2482 * sizeof(auxtrace_info_event::type) +
2483 * sizeof(auxtrace_info_event::reserved) == 8
2485 info_header_size
= 8;
2487 if (total_size
< (event_header_size
+ info_header_size
))
2490 priv_size
= total_size
- event_header_size
- info_header_size
;
2492 /* First the global part */
2493 ptr
= (u64
*) auxtrace_info
->priv
;
2495 /* Look for version '0' of the header */
2499 hdr
= zalloc(sizeof(*hdr
) * CS_HEADER_VERSION_0_MAX
);
2503 /* Extract header information - see cs-etm.h for format */
2504 for (i
= 0; i
< CS_HEADER_VERSION_0_MAX
; i
++)
2506 num_cpu
= hdr
[CS_PMU_TYPE_CPUS
] & 0xffffffff;
2507 pmu_type
= (unsigned int) ((hdr
[CS_PMU_TYPE_CPUS
] >> 32) &
2511 * Create an RB tree for traceID-metadata tuple. Since the conversion
2512 * has to be made for each packet that gets decoded, optimizing access
2513 * in anything other than a sequential array is worth doing.
2515 traceid_list
= intlist__new(NULL
);
2516 if (!traceid_list
) {
2521 metadata
= zalloc(sizeof(*metadata
) * num_cpu
);
2524 goto err_free_traceid_list
;
2528 * The metadata is stored in the auxtrace_info section and encodes
2529 * the configuration of the ARM embedded trace macrocell which is
2530 * required by the trace decoder to properly decode the trace due
2531 * to its highly compressed nature.
2533 for (j
= 0; j
< num_cpu
; j
++) {
2534 if (ptr
[i
] == __perf_cs_etmv3_magic
) {
2535 metadata
[j
] = zalloc(sizeof(*metadata
[j
]) *
2539 goto err_free_metadata
;
2541 for (k
= 0; k
< CS_ETM_PRIV_MAX
; k
++)
2542 metadata
[j
][k
] = ptr
[i
+ k
];
2544 /* The traceID is our handle */
2545 idx
= metadata
[j
][CS_ETM_ETMTRACEIDR
];
2546 i
+= CS_ETM_PRIV_MAX
;
2547 } else if (ptr
[i
] == __perf_cs_etmv4_magic
) {
2548 metadata
[j
] = zalloc(sizeof(*metadata
[j
]) *
2552 goto err_free_metadata
;
2554 for (k
= 0; k
< CS_ETMV4_PRIV_MAX
; k
++)
2555 metadata
[j
][k
] = ptr
[i
+ k
];
2557 /* The traceID is our handle */
2558 idx
= metadata
[j
][CS_ETMV4_TRCTRACEIDR
];
2559 i
+= CS_ETMV4_PRIV_MAX
;
2562 /* Get an RB node for this CPU */
2563 inode
= intlist__findnew(traceid_list
, idx
);
2565 /* Something went wrong, no need to continue */
2568 goto err_free_metadata
;
2572 * The node for that CPU should not be taken.
2573 * Back out if that's the case.
2577 goto err_free_metadata
;
2579 /* All good, associate the traceID with the metadata pointer */
2580 inode
->priv
= metadata
[j
];
2584 * Each of CS_HEADER_VERSION_0_MAX, CS_ETM_PRIV_MAX and
2585 * CS_ETMV4_PRIV_MAX mark how many double words are in the
2586 * global metadata, and each cpu's metadata respectively.
2587 * The following tests if the correct number of double words was
2588 * present in the auxtrace info section.
2590 if (i
* 8 != priv_size
) {
2592 goto err_free_metadata
;
2595 etm
= zalloc(sizeof(*etm
));
2599 goto err_free_metadata
;
2602 err
= auxtrace_queues__init(&etm
->queues
);
2606 etm
->session
= session
;
2607 etm
->machine
= &session
->machines
.host
;
2609 etm
->num_cpu
= num_cpu
;
2610 etm
->pmu_type
= pmu_type
;
2611 etm
->snapshot_mode
= (hdr
[CS_ETM_SNAPSHOT
] != 0);
2612 etm
->metadata
= metadata
;
2613 etm
->auxtrace_type
= auxtrace_info
->type
;
2614 etm
->timeless_decoding
= cs_etm__is_timeless_decoding(etm
);
2616 etm
->auxtrace
.process_event
= cs_etm__process_event
;
2617 etm
->auxtrace
.process_auxtrace_event
= cs_etm__process_auxtrace_event
;
2618 etm
->auxtrace
.flush_events
= cs_etm__flush_events
;
2619 etm
->auxtrace
.free_events
= cs_etm__free_events
;
2620 etm
->auxtrace
.free
= cs_etm__free
;
2621 session
->auxtrace
= &etm
->auxtrace
;
2623 etm
->unknown_thread
= thread__new(999999999, 999999999);
2624 if (!etm
->unknown_thread
) {
2626 goto err_free_queues
;
2630 * Initialize list node so that at thread__zput() we can avoid
2631 * segmentation fault at list_del_init().
2633 INIT_LIST_HEAD(&etm
->unknown_thread
->node
);
2635 err
= thread__set_comm(etm
->unknown_thread
, "unknown", 0);
2637 goto err_delete_thread
;
2639 if (thread__init_maps(etm
->unknown_thread
, etm
->machine
)) {
2641 goto err_delete_thread
;
2645 cs_etm__print_auxtrace_info(auxtrace_info
->priv
, num_cpu
);
2649 if (session
->itrace_synth_opts
->set
) {
2650 etm
->synth_opts
= *session
->itrace_synth_opts
;
2652 itrace_synth_opts__set_default(&etm
->synth_opts
,
2653 session
->itrace_synth_opts
->default_no_sample
);
2654 etm
->synth_opts
.callchain
= false;
2657 err
= cs_etm__synth_events(etm
, session
);
2659 goto err_delete_thread
;
2661 err
= auxtrace_queues__process_index(&etm
->queues
, session
);
2663 goto err_delete_thread
;
2665 etm
->data_queued
= etm
->queues
.populated
;
2670 thread__zput(etm
->unknown_thread
);
2672 auxtrace_queues__free(&etm
->queues
);
2673 session
->auxtrace
= NULL
;
2677 /* No need to check @metadata[j], free(NULL) is supported */
2678 for (j
= 0; j
< num_cpu
; j
++)
2679 zfree(&metadata
[j
]);
2681 err_free_traceid_list
:
2682 intlist__delete(traceid_list
);