Commit | Line | Data |
---|---|---|
8a9fd832 | 1 | // SPDX-License-Identifier: GPL-2.0 |
440a23b3 | 2 | /* |
440a23b3 MP |
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> | |
47f0d94c | 10 | #include <linux/coresight-pmu.h> |
440a23b3 MP |
11 | #include <linux/err.h> |
12 | #include <linux/kernel.h> | |
13 | #include <linux/log2.h> | |
14 | #include <linux/types.h> | |
7f7c536f | 15 | #include <linux/zalloc.h> |
440a23b3 | 16 | |
06220bf4 | 17 | #include <opencsd/ocsd_if_types.h> |
440a23b3 MP |
18 | #include <stdlib.h> |
19 | ||
20 | #include "auxtrace.h" | |
21 | #include "color.h" | |
22 | #include "cs-etm.h" | |
68ffe390 | 23 | #include "cs-etm-decoder/cs-etm-decoder.h" |
440a23b3 | 24 | #include "debug.h" |
4a3cec84 | 25 | #include "dso.h" |
440a23b3 MP |
26 | #include "evlist.h" |
27 | #include "intlist.h" | |
28 | #include "machine.h" | |
29 | #include "map.h" | |
30 | #include "perf.h" | |
f2a39fe8 | 31 | #include "session.h" |
d3300a3c ACM |
32 | #include "map_symbol.h" |
33 | #include "branch.h" | |
859dcf64 | 34 | #include "symbol.h" |
4a3cec84 | 35 | #include "tool.h" |
440a23b3 | 36 | #include "thread.h" |
440a23b3 | 37 | #include "thread-stack.h" |
c152d4d4 | 38 | #include <tools/libc_compat.h> |
055c67ed | 39 | #include "util/synthetic-events.h" |
440a23b3 | 40 | |
440a23b3 MP |
41 | struct cs_etm_auxtrace { |
42 | struct auxtrace auxtrace; | |
43 | struct auxtrace_queues queues; | |
44 | struct auxtrace_heap heap; | |
45 | struct itrace_synth_opts synth_opts; | |
46 | struct perf_session *session; | |
47 | struct machine *machine; | |
48 | struct thread *unknown_thread; | |
49 | ||
50 | u8 timeless_decoding; | |
51 | u8 snapshot_mode; | |
52 | u8 data_queued; | |
53 | u8 sample_branches; | |
e573e978 | 54 | u8 sample_instructions; |
440a23b3 MP |
55 | |
56 | int num_cpu; | |
1ac9e0b5 | 57 | u64 latest_kernel_timestamp; |
440a23b3 MP |
58 | u32 auxtrace_type; |
59 | u64 branches_sample_type; | |
60 | u64 branches_id; | |
e573e978 RW |
61 | u64 instructions_sample_type; |
62 | u64 instructions_sample_period; | |
63 | u64 instructions_id; | |
440a23b3 | 64 | u64 **metadata; |
440a23b3 MP |
65 | unsigned int pmu_type; |
66 | }; | |
67 | ||
c7bfa2fd MP |
68 | struct cs_etm_traceid_queue { |
69 | u8 trace_chan_id; | |
0abb868b | 70 | pid_t pid, tid; |
c7bfa2fd MP |
71 | u64 period_instructions; |
72 | size_t last_branch_pos; | |
73 | union perf_event *event_buf; | |
3c21d7d8 | 74 | struct thread *thread; |
c7bfa2fd MP |
75 | struct branch_stack *last_branch; |
76 | struct branch_stack *last_branch_rb; | |
77 | struct cs_etm_packet *prev_packet; | |
78 | struct cs_etm_packet *packet; | |
79 | struct cs_etm_packet_queue packet_queue; | |
80 | }; | |
81 | ||
440a23b3 MP |
82 | struct cs_etm_queue { |
83 | struct cs_etm_auxtrace *etm; | |
440a23b3 MP |
84 | struct cs_etm_decoder *decoder; |
85 | struct auxtrace_buffer *buffer; | |
440a23b3 | 86 | unsigned int queue_nr; |
aadd6ba4 | 87 | u8 pending_timestamp_chan_id; |
440a23b3 | 88 | u64 offset; |
23cfcd6d MP |
89 | const unsigned char *buf; |
90 | size_t buf_len, buf_used; | |
c152d4d4 MP |
91 | /* Conversion between traceID and index in traceid_queues array */ |
92 | struct intlist *traceid_queues_list; | |
93 | struct cs_etm_traceid_queue **traceid_queues; | |
440a23b3 MP |
94 | }; |
95 | ||
168200b6 LY |
96 | /* RB tree for quick conversion between traceID and metadata pointers */ |
97 | static struct intlist *traceid_list; | |
98 | ||
21fe8dc1 | 99 | static int cs_etm__process_queues(struct cs_etm_auxtrace *etm); |
9f878b29 | 100 | static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm, |
fc7ac413 | 101 | pid_t tid); |
21fe8dc1 MP |
102 | static int cs_etm__get_data_block(struct cs_etm_queue *etmq); |
103 | static int cs_etm__decode_data_block(struct cs_etm_queue *etmq); | |
9f878b29 | 104 | |
15a5cd19 MP |
105 | /* PTMs ETMIDR [11:8] set to b0011 */ |
106 | #define ETMIDR_PTM_VERSION 0x00000300 | |
107 | ||
21fe8dc1 MP |
108 | /* |
109 | * A struct auxtrace_heap_item only has a queue_nr and a timestamp to | |
110 | * work with. One option is to modify to auxtrace_heap_XYZ() API or simply | |
111 | * encode the etm queue number as the upper 16 bit and the channel as | |
112 | * the lower 16 bit. | |
113 | */ | |
9d604aad | 114 | #define TO_CS_QUEUE_NR(queue_nr, trace_chan_id) \ |
21fe8dc1 MP |
115 | (queue_nr << 16 | trace_chan_id) |
116 | #define TO_QUEUE_NR(cs_queue_nr) (cs_queue_nr >> 16) | |
117 | #define TO_TRACE_CHAN_ID(cs_queue_nr) (cs_queue_nr & 0x0000ffff) | |
118 | ||
15a5cd19 MP |
119 | static u32 cs_etm__get_v7_protocol_version(u32 etmidr) |
120 | { | |
121 | etmidr &= ETMIDR_PTM_VERSION; | |
122 | ||
123 | if (etmidr == ETMIDR_PTM_VERSION) | |
124 | return CS_ETM_PROTO_PTM; | |
125 | ||
126 | return CS_ETM_PROTO_ETMV3; | |
127 | } | |
128 | ||
96dce7f4 LY |
129 | static int cs_etm__get_magic(u8 trace_chan_id, u64 *magic) |
130 | { | |
131 | struct int_node *inode; | |
132 | u64 *metadata; | |
133 | ||
134 | inode = intlist__find(traceid_list, trace_chan_id); | |
135 | if (!inode) | |
136 | return -EINVAL; | |
137 | ||
138 | metadata = inode->priv; | |
139 | *magic = metadata[CS_ETM_MAGIC]; | |
140 | return 0; | |
141 | } | |
142 | ||
95c6fe97 LY |
143 | int cs_etm__get_cpu(u8 trace_chan_id, int *cpu) |
144 | { | |
145 | struct int_node *inode; | |
146 | u64 *metadata; | |
147 | ||
148 | inode = intlist__find(traceid_list, trace_chan_id); | |
149 | if (!inode) | |
150 | return -EINVAL; | |
151 | ||
152 | metadata = inode->priv; | |
153 | *cpu = (int)metadata[CS_ETM_CPU]; | |
154 | return 0; | |
155 | } | |
156 | ||
47f0d94c LY |
157 | /* |
158 | * The returned PID format is presented by two bits: | |
159 | * | |
160 | * Bit ETM_OPT_CTXTID: CONTEXTIDR or CONTEXTIDR_EL1 is traced; | |
161 | * Bit ETM_OPT_CTXTID2: CONTEXTIDR_EL2 is traced. | |
162 | * | |
163 | * It's possible that the two bits ETM_OPT_CTXTID and ETM_OPT_CTXTID2 | |
164 | * are enabled at the same time when the session runs on an EL2 kernel. | |
165 | * This means the CONTEXTIDR_EL1 and CONTEXTIDR_EL2 both will be | |
166 | * recorded in the trace data, the tool will selectively use | |
167 | * CONTEXTIDR_EL2 as PID. | |
168 | */ | |
169 | int cs_etm__get_pid_fmt(u8 trace_chan_id, u64 *pid_fmt) | |
170 | { | |
171 | struct int_node *inode; | |
172 | u64 *metadata, val; | |
173 | ||
174 | inode = intlist__find(traceid_list, trace_chan_id); | |
175 | if (!inode) | |
176 | return -EINVAL; | |
177 | ||
178 | metadata = inode->priv; | |
179 | ||
180 | if (metadata[CS_ETM_MAGIC] == __perf_cs_etmv3_magic) { | |
181 | val = metadata[CS_ETM_ETMCR]; | |
182 | /* CONTEXTIDR is traced */ | |
183 | if (val & BIT(ETM_OPT_CTXTID)) | |
184 | *pid_fmt = BIT(ETM_OPT_CTXTID); | |
185 | } else { | |
186 | val = metadata[CS_ETMV4_TRCCONFIGR]; | |
187 | /* CONTEXTIDR_EL2 is traced */ | |
188 | if (val & (BIT(ETM4_CFG_BIT_VMID) | BIT(ETM4_CFG_BIT_VMID_OPT))) | |
189 | *pid_fmt = BIT(ETM_OPT_CTXTID2); | |
190 | /* CONTEXTIDR_EL1 is traced */ | |
191 | else if (val & BIT(ETM4_CFG_BIT_CTXTID)) | |
192 | *pid_fmt = BIT(ETM_OPT_CTXTID); | |
193 | } | |
194 | ||
195 | return 0; | |
196 | } | |
197 | ||
675f302f MP |
198 | void cs_etm__etmq_set_traceid_queue_timestamp(struct cs_etm_queue *etmq, |
199 | u8 trace_chan_id) | |
200 | { | |
201 | /* | |
4d39c89f | 202 | * When a timestamp packet is encountered the backend code |
675f302f MP |
203 | * is stopped so that the front end has time to process packets |
204 | * that were accumulated in the traceID queue. Since there can | |
205 | * be more than one channel per cs_etm_queue, we need to specify | |
206 | * what traceID queue needs servicing. | |
207 | */ | |
aadd6ba4 | 208 | etmq->pending_timestamp_chan_id = trace_chan_id; |
675f302f MP |
209 | } |
210 | ||
21fe8dc1 MP |
211 | static u64 cs_etm__etmq_get_timestamp(struct cs_etm_queue *etmq, |
212 | u8 *trace_chan_id) | |
213 | { | |
214 | struct cs_etm_packet_queue *packet_queue; | |
215 | ||
aadd6ba4 | 216 | if (!etmq->pending_timestamp_chan_id) |
21fe8dc1 MP |
217 | return 0; |
218 | ||
219 | if (trace_chan_id) | |
aadd6ba4 | 220 | *trace_chan_id = etmq->pending_timestamp_chan_id; |
21fe8dc1 MP |
221 | |
222 | packet_queue = cs_etm__etmq_get_packet_queue(etmq, | |
aadd6ba4 | 223 | etmq->pending_timestamp_chan_id); |
21fe8dc1 MP |
224 | if (!packet_queue) |
225 | return 0; | |
226 | ||
227 | /* Acknowledge pending status */ | |
aadd6ba4 | 228 | etmq->pending_timestamp_chan_id = 0; |
21fe8dc1 MP |
229 | |
230 | /* See function cs_etm_decoder__do_{hard|soft}_timestamp() */ | |
aadd6ba4 | 231 | return packet_queue->cs_timestamp; |
21fe8dc1 MP |
232 | } |
233 | ||
5f7cb035 MP |
234 | static void cs_etm__clear_packet_queue(struct cs_etm_packet_queue *queue) |
235 | { | |
236 | int i; | |
237 | ||
238 | queue->head = 0; | |
239 | queue->tail = 0; | |
240 | queue->packet_count = 0; | |
241 | for (i = 0; i < CS_ETM_PACKET_MAX_BUFFER; i++) { | |
242 | queue->packet_buffer[i].isa = CS_ETM_ISA_UNKNOWN; | |
243 | queue->packet_buffer[i].start_addr = CS_ETM_INVAL_ADDR; | |
244 | queue->packet_buffer[i].end_addr = CS_ETM_INVAL_ADDR; | |
245 | queue->packet_buffer[i].instr_count = 0; | |
246 | queue->packet_buffer[i].last_instr_taken_branch = false; | |
247 | queue->packet_buffer[i].last_instr_size = 0; | |
248 | queue->packet_buffer[i].last_instr_type = 0; | |
249 | queue->packet_buffer[i].last_instr_subtype = 0; | |
250 | queue->packet_buffer[i].last_instr_cond = 0; | |
251 | queue->packet_buffer[i].flags = 0; | |
252 | queue->packet_buffer[i].exception_number = UINT32_MAX; | |
253 | queue->packet_buffer[i].trace_chan_id = UINT8_MAX; | |
254 | queue->packet_buffer[i].cpu = INT_MIN; | |
255 | } | |
256 | } | |
257 | ||
21fe8dc1 MP |
258 | static void cs_etm__clear_all_packet_queues(struct cs_etm_queue *etmq) |
259 | { | |
260 | int idx; | |
261 | struct int_node *inode; | |
262 | struct cs_etm_traceid_queue *tidq; | |
263 | struct intlist *traceid_queues_list = etmq->traceid_queues_list; | |
264 | ||
265 | intlist__for_each_entry(inode, traceid_queues_list) { | |
266 | idx = (int)(intptr_t)inode->priv; | |
267 | tidq = etmq->traceid_queues[idx]; | |
268 | cs_etm__clear_packet_queue(&tidq->packet_queue); | |
269 | } | |
270 | } | |
271 | ||
c7bfa2fd MP |
272 | static int cs_etm__init_traceid_queue(struct cs_etm_queue *etmq, |
273 | struct cs_etm_traceid_queue *tidq, | |
274 | u8 trace_chan_id) | |
275 | { | |
276 | int rc = -ENOMEM; | |
0abb868b | 277 | struct auxtrace_queue *queue; |
c7bfa2fd MP |
278 | struct cs_etm_auxtrace *etm = etmq->etm; |
279 | ||
280 | cs_etm__clear_packet_queue(&tidq->packet_queue); | |
281 | ||
0abb868b MP |
282 | queue = &etmq->etm->queues.queue_array[etmq->queue_nr]; |
283 | tidq->tid = queue->tid; | |
284 | tidq->pid = -1; | |
c7bfa2fd MP |
285 | tidq->trace_chan_id = trace_chan_id; |
286 | ||
287 | tidq->packet = zalloc(sizeof(struct cs_etm_packet)); | |
288 | if (!tidq->packet) | |
289 | goto out; | |
290 | ||
291 | tidq->prev_packet = zalloc(sizeof(struct cs_etm_packet)); | |
292 | if (!tidq->prev_packet) | |
293 | goto out_free; | |
294 | ||
295 | if (etm->synth_opts.last_branch) { | |
296 | size_t sz = sizeof(struct branch_stack); | |
297 | ||
298 | sz += etm->synth_opts.last_branch_sz * | |
299 | sizeof(struct branch_entry); | |
300 | tidq->last_branch = zalloc(sz); | |
301 | if (!tidq->last_branch) | |
302 | goto out_free; | |
303 | tidq->last_branch_rb = zalloc(sz); | |
304 | if (!tidq->last_branch_rb) | |
305 | goto out_free; | |
306 | } | |
307 | ||
308 | tidq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE); | |
309 | if (!tidq->event_buf) | |
310 | goto out_free; | |
311 | ||
312 | return 0; | |
313 | ||
314 | out_free: | |
315 | zfree(&tidq->last_branch_rb); | |
316 | zfree(&tidq->last_branch); | |
317 | zfree(&tidq->prev_packet); | |
318 | zfree(&tidq->packet); | |
319 | out: | |
320 | return rc; | |
321 | } | |
322 | ||
323 | static struct cs_etm_traceid_queue | |
324 | *cs_etm__etmq_get_traceid_queue(struct cs_etm_queue *etmq, u8 trace_chan_id) | |
325 | { | |
c152d4d4 MP |
326 | int idx; |
327 | struct int_node *inode; | |
328 | struct intlist *traceid_queues_list; | |
329 | struct cs_etm_traceid_queue *tidq, **traceid_queues; | |
c7bfa2fd MP |
330 | struct cs_etm_auxtrace *etm = etmq->etm; |
331 | ||
c152d4d4 MP |
332 | if (etm->timeless_decoding) |
333 | trace_chan_id = CS_ETM_PER_THREAD_TRACEID; | |
c7bfa2fd | 334 | |
c152d4d4 | 335 | traceid_queues_list = etmq->traceid_queues_list; |
c7bfa2fd | 336 | |
c152d4d4 MP |
337 | /* |
338 | * Check if the traceid_queue exist for this traceID by looking | |
339 | * in the queue list. | |
340 | */ | |
341 | inode = intlist__find(traceid_queues_list, trace_chan_id); | |
342 | if (inode) { | |
343 | idx = (int)(intptr_t)inode->priv; | |
344 | return etmq->traceid_queues[idx]; | |
345 | } | |
c7bfa2fd | 346 | |
c152d4d4 | 347 | /* We couldn't find a traceid_queue for this traceID, allocate one */ |
c7bfa2fd MP |
348 | tidq = malloc(sizeof(*tidq)); |
349 | if (!tidq) | |
350 | return NULL; | |
351 | ||
352 | memset(tidq, 0, sizeof(*tidq)); | |
353 | ||
c152d4d4 MP |
354 | /* Get a valid index for the new traceid_queue */ |
355 | idx = intlist__nr_entries(traceid_queues_list); | |
356 | /* Memory for the inode is free'ed in cs_etm_free_traceid_queues () */ | |
357 | inode = intlist__findnew(traceid_queues_list, trace_chan_id); | |
358 | if (!inode) | |
359 | goto out_free; | |
360 | ||
361 | /* Associate this traceID with this index */ | |
362 | inode->priv = (void *)(intptr_t)idx; | |
363 | ||
c7bfa2fd MP |
364 | if (cs_etm__init_traceid_queue(etmq, tidq, trace_chan_id)) |
365 | goto out_free; | |
366 | ||
c152d4d4 MP |
367 | /* Grow the traceid_queues array by one unit */ |
368 | traceid_queues = etmq->traceid_queues; | |
369 | traceid_queues = reallocarray(traceid_queues, | |
370 | idx + 1, | |
371 | sizeof(*traceid_queues)); | |
372 | ||
373 | /* | |
374 | * On failure reallocarray() returns NULL and the original block of | |
375 | * memory is left untouched. | |
376 | */ | |
377 | if (!traceid_queues) | |
378 | goto out_free; | |
379 | ||
380 | traceid_queues[idx] = tidq; | |
381 | etmq->traceid_queues = traceid_queues; | |
c7bfa2fd | 382 | |
c152d4d4 | 383 | return etmq->traceid_queues[idx]; |
c7bfa2fd MP |
384 | |
385 | out_free: | |
c152d4d4 MP |
386 | /* |
387 | * Function intlist__remove() removes the inode from the list | |
388 | * and delete the memory associated to it. | |
389 | */ | |
390 | intlist__remove(traceid_queues_list, inode); | |
c7bfa2fd MP |
391 | free(tidq); |
392 | ||
393 | return NULL; | |
394 | } | |
395 | ||
5f7cb035 | 396 | struct cs_etm_packet_queue |
c7bfa2fd | 397 | *cs_etm__etmq_get_packet_queue(struct cs_etm_queue *etmq, u8 trace_chan_id) |
5f7cb035 | 398 | { |
c7bfa2fd MP |
399 | struct cs_etm_traceid_queue *tidq; |
400 | ||
401 | tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id); | |
402 | if (tidq) | |
403 | return &tidq->packet_queue; | |
404 | ||
405 | return NULL; | |
5f7cb035 MP |
406 | } |
407 | ||
d0175156 LY |
408 | static void cs_etm__packet_swap(struct cs_etm_auxtrace *etm, |
409 | struct cs_etm_traceid_queue *tidq) | |
410 | { | |
411 | struct cs_etm_packet *tmp; | |
412 | ||
413 | if (etm->sample_branches || etm->synth_opts.last_branch || | |
414 | etm->sample_instructions) { | |
415 | /* | |
416 | * Swap PACKET with PREV_PACKET: PACKET becomes PREV_PACKET for | |
417 | * the next incoming packet. | |
418 | */ | |
419 | tmp = tidq->packet; | |
420 | tidq->packet = tidq->prev_packet; | |
421 | tidq->prev_packet = tmp; | |
422 | } | |
423 | } | |
424 | ||
68ffe390 MP |
425 | static void cs_etm__packet_dump(const char *pkt_string) |
426 | { | |
427 | const char *color = PERF_COLOR_BLUE; | |
428 | int len = strlen(pkt_string); | |
429 | ||
430 | if (len && (pkt_string[len-1] == '\n')) | |
431 | color_fprintf(stdout, color, " %s", pkt_string); | |
432 | else | |
433 | color_fprintf(stdout, color, " %s\n", pkt_string); | |
434 | ||
435 | fflush(stdout); | |
436 | } | |
437 | ||
2507a3d9 MP |
438 | static void cs_etm__set_trace_param_etmv3(struct cs_etm_trace_params *t_params, |
439 | struct cs_etm_auxtrace *etm, int idx, | |
440 | u32 etmidr) | |
441 | { | |
442 | u64 **metadata = etm->metadata; | |
443 | ||
444 | t_params[idx].protocol = cs_etm__get_v7_protocol_version(etmidr); | |
445 | t_params[idx].etmv3.reg_ctrl = metadata[idx][CS_ETM_ETMCR]; | |
446 | t_params[idx].etmv3.reg_trc_id = metadata[idx][CS_ETM_ETMTRACEIDR]; | |
447 | } | |
448 | ||
449 | static void cs_etm__set_trace_param_etmv4(struct cs_etm_trace_params *t_params, | |
450 | struct cs_etm_auxtrace *etm, int idx) | |
451 | { | |
452 | u64 **metadata = etm->metadata; | |
453 | ||
454 | t_params[idx].protocol = CS_ETM_PROTO_ETMV4i; | |
455 | t_params[idx].etmv4.reg_idr0 = metadata[idx][CS_ETMV4_TRCIDR0]; | |
456 | t_params[idx].etmv4.reg_idr1 = metadata[idx][CS_ETMV4_TRCIDR1]; | |
457 | t_params[idx].etmv4.reg_idr2 = metadata[idx][CS_ETMV4_TRCIDR2]; | |
458 | t_params[idx].etmv4.reg_idr8 = metadata[idx][CS_ETMV4_TRCIDR8]; | |
459 | t_params[idx].etmv4.reg_configr = metadata[idx][CS_ETMV4_TRCCONFIGR]; | |
460 | t_params[idx].etmv4.reg_traceidr = metadata[idx][CS_ETMV4_TRCTRACEIDR]; | |
461 | } | |
462 | ||
463 | static int cs_etm__init_trace_params(struct cs_etm_trace_params *t_params, | |
9182f04a JC |
464 | struct cs_etm_auxtrace *etm, |
465 | int decoders) | |
2507a3d9 MP |
466 | { |
467 | int i; | |
468 | u32 etmidr; | |
469 | u64 architecture; | |
470 | ||
9182f04a | 471 | for (i = 0; i < decoders; i++) { |
2507a3d9 MP |
472 | architecture = etm->metadata[i][CS_ETM_MAGIC]; |
473 | ||
474 | switch (architecture) { | |
475 | case __perf_cs_etmv3_magic: | |
476 | etmidr = etm->metadata[i][CS_ETM_ETMIDR]; | |
477 | cs_etm__set_trace_param_etmv3(t_params, etm, i, etmidr); | |
478 | break; | |
479 | case __perf_cs_etmv4_magic: | |
480 | cs_etm__set_trace_param_etmv4(t_params, etm, i); | |
481 | break; | |
482 | default: | |
483 | return -EINVAL; | |
484 | } | |
485 | } | |
486 | ||
487 | return 0; | |
488 | } | |
489 | ||
65963e5b MP |
490 | static int cs_etm__init_decoder_params(struct cs_etm_decoder_params *d_params, |
491 | struct cs_etm_queue *etmq, | |
9182f04a JC |
492 | enum cs_etm_decoder_operation mode, |
493 | bool formatted) | |
65963e5b MP |
494 | { |
495 | int ret = -EINVAL; | |
496 | ||
497 | if (!(mode < CS_ETM_OPERATION_MAX)) | |
498 | goto out; | |
499 | ||
500 | d_params->packet_printer = cs_etm__packet_dump; | |
501 | d_params->operation = mode; | |
502 | d_params->data = etmq; | |
9182f04a | 503 | d_params->formatted = formatted; |
65963e5b MP |
504 | d_params->fsyncs = false; |
505 | d_params->hsyncs = false; | |
506 | d_params->frame_aligned = true; | |
507 | ||
508 | ret = 0; | |
509 | out: | |
510 | return ret; | |
511 | } | |
512 | ||
04aaad26 | 513 | static void cs_etm__dump_event(struct cs_etm_queue *etmq, |
68ffe390 MP |
514 | struct auxtrace_buffer *buffer) |
515 | { | |
2507a3d9 | 516 | int ret; |
68ffe390 | 517 | const char *color = PERF_COLOR_BLUE; |
68ffe390 MP |
518 | size_t buffer_used = 0; |
519 | ||
520 | fprintf(stdout, "\n"); | |
521 | color_fprintf(stdout, color, | |
522 | ". ... CoreSight ETM Trace data: size %zu bytes\n", | |
523 | buffer->size); | |
524 | ||
68ffe390 MP |
525 | do { |
526 | size_t consumed; | |
527 | ||
528 | ret = cs_etm_decoder__process_data_block( | |
04aaad26 | 529 | etmq->decoder, buffer->offset, |
68ffe390 MP |
530 | &((u8 *)buffer->data)[buffer_used], |
531 | buffer->size - buffer_used, &consumed); | |
532 | if (ret) | |
533 | break; | |
534 | ||
535 | buffer_used += consumed; | |
536 | } while (buffer_used < buffer->size); | |
537 | ||
04aaad26 | 538 | cs_etm_decoder__reset(etmq->decoder); |
68ffe390 MP |
539 | } |
540 | ||
440a23b3 MP |
541 | static int cs_etm__flush_events(struct perf_session *session, |
542 | struct perf_tool *tool) | |
543 | { | |
9f878b29 MP |
544 | struct cs_etm_auxtrace *etm = container_of(session->auxtrace, |
545 | struct cs_etm_auxtrace, | |
546 | auxtrace); | |
547 | if (dump_trace) | |
548 | return 0; | |
549 | ||
550 | if (!tool->ordered_events) | |
551 | return -EINVAL; | |
552 | ||
21fe8dc1 MP |
553 | if (etm->timeless_decoding) |
554 | return cs_etm__process_timeless_queues(etm, -1); | |
555 | ||
556 | return cs_etm__process_queues(etm); | |
440a23b3 MP |
557 | } |
558 | ||
c152d4d4 MP |
559 | static void cs_etm__free_traceid_queues(struct cs_etm_queue *etmq) |
560 | { | |
561 | int idx; | |
562 | uintptr_t priv; | |
563 | struct int_node *inode, *tmp; | |
564 | struct cs_etm_traceid_queue *tidq; | |
565 | struct intlist *traceid_queues_list = etmq->traceid_queues_list; | |
566 | ||
567 | intlist__for_each_entry_safe(inode, tmp, traceid_queues_list) { | |
568 | priv = (uintptr_t)inode->priv; | |
569 | idx = priv; | |
570 | ||
571 | /* Free this traceid_queue from the array */ | |
572 | tidq = etmq->traceid_queues[idx]; | |
573 | thread__zput(tidq->thread); | |
574 | zfree(&tidq->event_buf); | |
575 | zfree(&tidq->last_branch); | |
576 | zfree(&tidq->last_branch_rb); | |
577 | zfree(&tidq->prev_packet); | |
578 | zfree(&tidq->packet); | |
579 | zfree(&tidq); | |
580 | ||
581 | /* | |
582 | * Function intlist__remove() removes the inode from the list | |
583 | * and delete the memory associated to it. | |
584 | */ | |
585 | intlist__remove(traceid_queues_list, inode); | |
586 | } | |
587 | ||
588 | /* Then the RB tree itself */ | |
589 | intlist__delete(traceid_queues_list); | |
590 | etmq->traceid_queues_list = NULL; | |
591 | ||
592 | /* finally free the traceid_queues array */ | |
d8f9da24 | 593 | zfree(&etmq->traceid_queues); |
c152d4d4 MP |
594 | } |
595 | ||
440a23b3 MP |
596 | static void cs_etm__free_queue(void *priv) |
597 | { | |
598 | struct cs_etm_queue *etmq = priv; | |
599 | ||
099c1130 MP |
600 | if (!etmq) |
601 | return; | |
602 | ||
099c1130 | 603 | cs_etm_decoder__free(etmq->decoder); |
c152d4d4 | 604 | cs_etm__free_traceid_queues(etmq); |
440a23b3 MP |
605 | free(etmq); |
606 | } | |
607 | ||
608 | static void cs_etm__free_events(struct perf_session *session) | |
609 | { | |
610 | unsigned int i; | |
611 | struct cs_etm_auxtrace *aux = container_of(session->auxtrace, | |
612 | struct cs_etm_auxtrace, | |
613 | auxtrace); | |
614 | struct auxtrace_queues *queues = &aux->queues; | |
615 | ||
616 | for (i = 0; i < queues->nr_queues; i++) { | |
617 | cs_etm__free_queue(queues->queue_array[i].priv); | |
618 | queues->queue_array[i].priv = NULL; | |
619 | } | |
620 | ||
621 | auxtrace_queues__free(queues); | |
622 | } | |
623 | ||
624 | static void cs_etm__free(struct perf_session *session) | |
625 | { | |
cd8bfd8c TJ |
626 | int i; |
627 | struct int_node *inode, *tmp; | |
440a23b3 MP |
628 | struct cs_etm_auxtrace *aux = container_of(session->auxtrace, |
629 | struct cs_etm_auxtrace, | |
630 | auxtrace); | |
631 | cs_etm__free_events(session); | |
632 | session->auxtrace = NULL; | |
633 | ||
95c6fe97 | 634 | /* First remove all traceID/metadata nodes for the RB tree */ |
cd8bfd8c TJ |
635 | intlist__for_each_entry_safe(inode, tmp, traceid_list) |
636 | intlist__remove(traceid_list, inode); | |
637 | /* Then the RB tree itself */ | |
638 | intlist__delete(traceid_list); | |
639 | ||
640 | for (i = 0; i < aux->num_cpu; i++) | |
641 | zfree(&aux->metadata[i]); | |
642 | ||
46d53620 | 643 | thread__zput(aux->unknown_thread); |
cd8bfd8c | 644 | zfree(&aux->metadata); |
440a23b3 MP |
645 | zfree(&aux); |
646 | } | |
647 | ||
a58ab57c AH |
648 | static bool cs_etm__evsel_is_auxtrace(struct perf_session *session, |
649 | struct evsel *evsel) | |
650 | { | |
651 | struct cs_etm_auxtrace *aux = container_of(session->auxtrace, | |
652 | struct cs_etm_auxtrace, | |
653 | auxtrace); | |
654 | ||
655 | return evsel->core.attr.type == aux->pmu_type; | |
656 | } | |
657 | ||
d6c9c05f LY |
658 | static u8 cs_etm__cpu_mode(struct cs_etm_queue *etmq, u64 address) |
659 | { | |
660 | struct machine *machine; | |
661 | ||
662 | machine = etmq->etm->machine; | |
663 | ||
6f38e115 | 664 | if (address >= machine__kernel_start(machine)) { |
d6c9c05f LY |
665 | if (machine__is_host(machine)) |
666 | return PERF_RECORD_MISC_KERNEL; | |
667 | else | |
668 | return PERF_RECORD_MISC_GUEST_KERNEL; | |
669 | } else { | |
670 | if (machine__is_host(machine)) | |
671 | return PERF_RECORD_MISC_USER; | |
672 | else if (perf_guest) | |
673 | return PERF_RECORD_MISC_GUEST_USER; | |
674 | else | |
675 | return PERF_RECORD_MISC_HYPERVISOR; | |
676 | } | |
677 | } | |
678 | ||
af21577c MP |
679 | static u32 cs_etm__mem_access(struct cs_etm_queue *etmq, u8 trace_chan_id, |
680 | u64 address, size_t size, u8 *buffer) | |
20d9c478 MP |
681 | { |
682 | u8 cpumode; | |
683 | u64 offset; | |
684 | int len; | |
c152d4d4 MP |
685 | struct thread *thread; |
686 | struct machine *machine; | |
687 | struct addr_location al; | |
688 | struct cs_etm_traceid_queue *tidq; | |
af21577c | 689 | |
20d9c478 | 690 | if (!etmq) |
d3267ad4 | 691 | return 0; |
20d9c478 MP |
692 | |
693 | machine = etmq->etm->machine; | |
d6c9c05f | 694 | cpumode = cs_etm__cpu_mode(etmq, address); |
c152d4d4 MP |
695 | tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id); |
696 | if (!tidq) | |
697 | return 0; | |
20d9c478 | 698 | |
c152d4d4 | 699 | thread = tidq->thread; |
20d9c478 MP |
700 | if (!thread) { |
701 | if (cpumode != PERF_RECORD_MISC_KERNEL) | |
d3267ad4 | 702 | return 0; |
20d9c478 MP |
703 | thread = etmq->etm->unknown_thread; |
704 | } | |
705 | ||
71a84b5a | 706 | if (!thread__find_map(thread, cpumode, address, &al) || !al.map->dso) |
20d9c478 MP |
707 | return 0; |
708 | ||
709 | if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR && | |
710 | dso__data_status_seen(al.map->dso, DSO_DATA_STATUS_SEEN_ITRACE)) | |
711 | return 0; | |
712 | ||
713 | offset = al.map->map_ip(al.map, address); | |
714 | ||
715 | map__load(al.map); | |
716 | ||
717 | len = dso__data_read_offset(al.map->dso, machine, offset, buffer, size); | |
718 | ||
719 | if (len <= 0) | |
720 | return 0; | |
721 | ||
722 | return len; | |
723 | } | |
724 | ||
9182f04a JC |
725 | static struct cs_etm_queue *cs_etm__alloc_queue(struct cs_etm_auxtrace *etm, |
726 | bool formatted) | |
20d9c478 | 727 | { |
20d9c478 | 728 | struct cs_etm_decoder_params d_params; |
ae4d9f52 | 729 | struct cs_etm_trace_params *t_params = NULL; |
20d9c478 | 730 | struct cs_etm_queue *etmq; |
9182f04a JC |
731 | /* |
732 | * Each queue can only contain data from one CPU when unformatted, so only one decoder is | |
733 | * needed. | |
734 | */ | |
735 | int decoders = formatted ? etm->num_cpu : 1; | |
20d9c478 MP |
736 | |
737 | etmq = zalloc(sizeof(*etmq)); | |
738 | if (!etmq) | |
739 | return NULL; | |
740 | ||
c152d4d4 MP |
741 | etmq->traceid_queues_list = intlist__new(NULL); |
742 | if (!etmq->traceid_queues_list) | |
743 | goto out_free; | |
744 | ||
20d9c478 | 745 | /* Use metadata to fill in trace parameters for trace decoder */ |
9182f04a | 746 | t_params = zalloc(sizeof(*t_params) * decoders); |
20d9c478 MP |
747 | |
748 | if (!t_params) | |
749 | goto out_free; | |
750 | ||
9182f04a | 751 | if (cs_etm__init_trace_params(t_params, etm, decoders)) |
2507a3d9 | 752 | goto out_free; |
20d9c478 | 753 | |
e4aa592d | 754 | /* Set decoder parameters to decode trace packets */ |
65963e5b | 755 | if (cs_etm__init_decoder_params(&d_params, etmq, |
04aaad26 | 756 | dump_trace ? CS_ETM_OPERATION_PRINT : |
9182f04a JC |
757 | CS_ETM_OPERATION_DECODE, |
758 | formatted)) | |
65963e5b | 759 | goto out_free; |
20d9c478 | 760 | |
9182f04a JC |
761 | etmq->decoder = cs_etm_decoder__new(decoders, &d_params, |
762 | t_params); | |
20d9c478 | 763 | |
20d9c478 MP |
764 | if (!etmq->decoder) |
765 | goto out_free; | |
766 | ||
767 | /* | |
768 | * Register a function to handle all memory accesses required by | |
769 | * the trace decoder library. | |
770 | */ | |
771 | if (cs_etm_decoder__add_mem_access_cb(etmq->decoder, | |
772 | 0x0L, ((u64) -1L), | |
773 | cs_etm__mem_access)) | |
774 | goto out_free_decoder; | |
775 | ||
ae4d9f52 | 776 | zfree(&t_params); |
20d9c478 MP |
777 | return etmq; |
778 | ||
779 | out_free_decoder: | |
780 | cs_etm_decoder__free(etmq->decoder); | |
781 | out_free: | |
c152d4d4 | 782 | intlist__delete(etmq->traceid_queues_list); |
20d9c478 MP |
783 | free(etmq); |
784 | ||
785 | return NULL; | |
786 | } | |
787 | ||
788 | static int cs_etm__setup_queue(struct cs_etm_auxtrace *etm, | |
789 | struct auxtrace_queue *queue, | |
9182f04a JC |
790 | unsigned int queue_nr, |
791 | bool formatted) | |
20d9c478 MP |
792 | { |
793 | struct cs_etm_queue *etmq = queue->priv; | |
794 | ||
795 | if (list_empty(&queue->head) || etmq) | |
9ac8afd5 | 796 | return 0; |
20d9c478 | 797 | |
9182f04a | 798 | etmq = cs_etm__alloc_queue(etm, formatted); |
20d9c478 | 799 | |
9ac8afd5 JC |
800 | if (!etmq) |
801 | return -ENOMEM; | |
20d9c478 MP |
802 | |
803 | queue->priv = etmq; | |
4f5b3713 MP |
804 | etmq->etm = etm; |
805 | etmq->queue_nr = queue_nr; | |
4f5b3713 | 806 | etmq->offset = 0; |
20d9c478 | 807 | |
9ac8afd5 JC |
808 | return 0; |
809 | } | |
810 | ||
811 | static int cs_etm__queue_first_cs_timestamp(struct cs_etm_auxtrace *etm, | |
812 | struct cs_etm_queue *etmq, | |
813 | unsigned int queue_nr) | |
814 | { | |
815 | int ret = 0; | |
816 | unsigned int cs_queue_nr; | |
817 | u8 trace_chan_id; | |
818 | u64 cs_timestamp; | |
21fe8dc1 MP |
819 | |
820 | /* | |
821 | * We are under a CPU-wide trace scenario. As such we need to know | |
822 | * when the code that generated the traces started to execute so that | |
823 | * it can be correlated with execution on other CPUs. So we get a | |
824 | * handle on the beginning of traces and decode until we find a | |
825 | * timestamp. The timestamp is then added to the auxtrace min heap | |
826 | * in order to know what nibble (of all the etmqs) to decode first. | |
827 | */ | |
828 | while (1) { | |
829 | /* | |
830 | * Fetch an aux_buffer from this etmq. Bail if no more | |
831 | * blocks or an error has been encountered. | |
832 | */ | |
833 | ret = cs_etm__get_data_block(etmq); | |
834 | if (ret <= 0) | |
835 | goto out; | |
836 | ||
837 | /* | |
838 | * Run decoder on the trace block. The decoder will stop when | |
aadd6ba4 | 839 | * encountering a CS timestamp, a full packet queue or the end of |
21fe8dc1 MP |
840 | * trace for that block. |
841 | */ | |
842 | ret = cs_etm__decode_data_block(etmq); | |
843 | if (ret) | |
844 | goto out; | |
845 | ||
846 | /* | |
847 | * Function cs_etm_decoder__do_{hard|soft}_timestamp() does all | |
848 | * the timestamp calculation for us. | |
849 | */ | |
aadd6ba4 | 850 | cs_timestamp = cs_etm__etmq_get_timestamp(etmq, &trace_chan_id); |
21fe8dc1 MP |
851 | |
852 | /* We found a timestamp, no need to continue. */ | |
aadd6ba4 | 853 | if (cs_timestamp) |
21fe8dc1 MP |
854 | break; |
855 | ||
856 | /* | |
857 | * We didn't find a timestamp so empty all the traceid packet | |
858 | * queues before looking for another timestamp packet, either | |
859 | * in the current data block or a new one. Packets that were | |
860 | * just decoded are useless since no timestamp has been | |
861 | * associated with them. As such simply discard them. | |
862 | */ | |
863 | cs_etm__clear_all_packet_queues(etmq); | |
864 | } | |
865 | ||
866 | /* | |
867 | * We have a timestamp. Add it to the min heap to reflect when | |
868 | * instructions conveyed by the range packets of this traceID queue | |
869 | * started to execute. Once the same has been done for all the traceID | |
870 | * queues of each etmq, redenring and decoding can start in | |
871 | * chronological order. | |
872 | * | |
873 | * Note that packets decoded above are still in the traceID's packet | |
874 | * queue and will be processed in cs_etm__process_queues(). | |
875 | */ | |
9d604aad | 876 | cs_queue_nr = TO_CS_QUEUE_NR(queue_nr, trace_chan_id); |
aadd6ba4 | 877 | ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, cs_timestamp); |
4f5b3713 MP |
878 | out: |
879 | return ret; | |
20d9c478 MP |
880 | } |
881 | ||
c7bfa2fd MP |
882 | static inline |
883 | void cs_etm__copy_last_branch_rb(struct cs_etm_queue *etmq, | |
884 | struct cs_etm_traceid_queue *tidq) | |
e573e978 | 885 | { |
c7bfa2fd MP |
886 | struct branch_stack *bs_src = tidq->last_branch_rb; |
887 | struct branch_stack *bs_dst = tidq->last_branch; | |
e573e978 RW |
888 | size_t nr = 0; |
889 | ||
890 | /* | |
891 | * Set the number of records before early exit: ->nr is used to | |
892 | * determine how many branches to copy from ->entries. | |
893 | */ | |
894 | bs_dst->nr = bs_src->nr; | |
895 | ||
896 | /* | |
897 | * Early exit when there is nothing to copy. | |
898 | */ | |
899 | if (!bs_src->nr) | |
900 | return; | |
901 | ||
902 | /* | |
903 | * As bs_src->entries is a circular buffer, we need to copy from it in | |
904 | * two steps. First, copy the branches from the most recently inserted | |
905 | * branch ->last_branch_pos until the end of bs_src->entries buffer. | |
906 | */ | |
c7bfa2fd | 907 | nr = etmq->etm->synth_opts.last_branch_sz - tidq->last_branch_pos; |
e573e978 | 908 | memcpy(&bs_dst->entries[0], |
c7bfa2fd | 909 | &bs_src->entries[tidq->last_branch_pos], |
e573e978 RW |
910 | sizeof(struct branch_entry) * nr); |
911 | ||
912 | /* | |
913 | * If we wrapped around at least once, the branches from the beginning | |
914 | * of the bs_src->entries buffer and until the ->last_branch_pos element | |
915 | * are older valid branches: copy them over. The total number of | |
916 | * branches copied over will be equal to the number of branches asked by | |
917 | * the user in last_branch_sz. | |
918 | */ | |
919 | if (bs_src->nr >= etmq->etm->synth_opts.last_branch_sz) { | |
920 | memcpy(&bs_dst->entries[nr], | |
921 | &bs_src->entries[0], | |
c7bfa2fd | 922 | sizeof(struct branch_entry) * tidq->last_branch_pos); |
e573e978 RW |
923 | } |
924 | } | |
925 | ||
c7bfa2fd MP |
926 | static inline |
927 | void cs_etm__reset_last_branch_rb(struct cs_etm_traceid_queue *tidq) | |
e573e978 | 928 | { |
c7bfa2fd MP |
929 | tidq->last_branch_pos = 0; |
930 | tidq->last_branch_rb->nr = 0; | |
e573e978 RW |
931 | } |
932 | ||
a7ee4d62 | 933 | static inline int cs_etm__t32_instr_size(struct cs_etm_queue *etmq, |
af21577c MP |
934 | u8 trace_chan_id, u64 addr) |
935 | { | |
a7ee4d62 | 936 | u8 instrBytes[2]; |
6035b680 | 937 | |
af21577c MP |
938 | cs_etm__mem_access(etmq, trace_chan_id, addr, |
939 | ARRAY_SIZE(instrBytes), instrBytes); | |
e573e978 | 940 | /* |
a7ee4d62 RW |
941 | * T32 instruction size is indicated by bits[15:11] of the first |
942 | * 16-bit word of the instruction: 0b11101, 0b11110 and 0b11111 | |
943 | * denote a 32-bit instruction. | |
e573e978 | 944 | */ |
a7ee4d62 | 945 | return ((instrBytes[1] & 0xF8) >= 0xE8) ? 4 : 2; |
e573e978 RW |
946 | } |
947 | ||
6035b680 LY |
948 | static inline u64 cs_etm__first_executed_instr(struct cs_etm_packet *packet) |
949 | { | |
49ccf87b LY |
950 | /* Returns 0 for the CS_ETM_DISCONTINUITY packet */ |
951 | if (packet->sample_type == CS_ETM_DISCONTINUITY) | |
6035b680 LY |
952 | return 0; |
953 | ||
954 | return packet->start_addr; | |
955 | } | |
956 | ||
a7ee4d62 RW |
957 | static inline |
958 | u64 cs_etm__last_executed_instr(const struct cs_etm_packet *packet) | |
e573e978 | 959 | { |
49ccf87b LY |
960 | /* Returns 0 for the CS_ETM_DISCONTINUITY packet */ |
961 | if (packet->sample_type == CS_ETM_DISCONTINUITY) | |
a7ee4d62 RW |
962 | return 0; |
963 | ||
964 | return packet->end_addr - packet->last_instr_size; | |
e573e978 RW |
965 | } |
966 | ||
a7ee4d62 | 967 | static inline u64 cs_etm__instr_addr(struct cs_etm_queue *etmq, |
af21577c | 968 | u64 trace_chan_id, |
a7ee4d62 | 969 | const struct cs_etm_packet *packet, |
e573e978 RW |
970 | u64 offset) |
971 | { | |
a7ee4d62 RW |
972 | if (packet->isa == CS_ETM_ISA_T32) { |
973 | u64 addr = packet->start_addr; | |
974 | ||
bc010dd6 | 975 | while (offset) { |
af21577c MP |
976 | addr += cs_etm__t32_instr_size(etmq, |
977 | trace_chan_id, addr); | |
a7ee4d62 RW |
978 | offset--; |
979 | } | |
980 | return addr; | |
981 | } | |
982 | ||
983 | /* Assume a 4 byte instruction size (A32/A64) */ | |
984 | return packet->start_addr + offset * 4; | |
e573e978 RW |
985 | } |
986 | ||
c7bfa2fd MP |
987 | static void cs_etm__update_last_branch_rb(struct cs_etm_queue *etmq, |
988 | struct cs_etm_traceid_queue *tidq) | |
e573e978 | 989 | { |
c7bfa2fd | 990 | struct branch_stack *bs = tidq->last_branch_rb; |
e573e978 RW |
991 | struct branch_entry *be; |
992 | ||
993 | /* | |
994 | * The branches are recorded in a circular buffer in reverse | |
995 | * chronological order: we start recording from the last element of the | |
996 | * buffer down. After writing the first element of the stack, move the | |
997 | * insert position back to the end of the buffer. | |
998 | */ | |
c7bfa2fd MP |
999 | if (!tidq->last_branch_pos) |
1000 | tidq->last_branch_pos = etmq->etm->synth_opts.last_branch_sz; | |
e573e978 | 1001 | |
c7bfa2fd | 1002 | tidq->last_branch_pos -= 1; |
e573e978 | 1003 | |
c7bfa2fd MP |
1004 | be = &bs->entries[tidq->last_branch_pos]; |
1005 | be->from = cs_etm__last_executed_instr(tidq->prev_packet); | |
1006 | be->to = cs_etm__first_executed_instr(tidq->packet); | |
e573e978 RW |
1007 | /* No support for mispredict */ |
1008 | be->flags.mispred = 0; | |
1009 | be->flags.predicted = 1; | |
1010 | ||
1011 | /* | |
1012 | * Increment bs->nr until reaching the number of last branches asked by | |
1013 | * the user on the command line. | |
1014 | */ | |
1015 | if (bs->nr < etmq->etm->synth_opts.last_branch_sz) | |
1016 | bs->nr += 1; | |
1017 | } | |
1018 | ||
1019 | static int cs_etm__inject_event(union perf_event *event, | |
1020 | struct perf_sample *sample, u64 type) | |
1021 | { | |
1022 | event->header.size = perf_event__sample_event_size(sample, type, 0); | |
1023 | return perf_event__synthesize_sample(event, type, 0, sample); | |
1024 | } | |
1025 | ||
1026 | ||
9f878b29 | 1027 | static int |
23cfcd6d | 1028 | cs_etm__get_trace(struct cs_etm_queue *etmq) |
9f878b29 MP |
1029 | { |
1030 | struct auxtrace_buffer *aux_buffer = etmq->buffer; | |
1031 | struct auxtrace_buffer *old_buffer = aux_buffer; | |
1032 | struct auxtrace_queue *queue; | |
1033 | ||
1034 | queue = &etmq->etm->queues.queue_array[etmq->queue_nr]; | |
1035 | ||
1036 | aux_buffer = auxtrace_buffer__next(queue, aux_buffer); | |
1037 | ||
1038 | /* If no more data, drop the previous auxtrace_buffer and return */ | |
1039 | if (!aux_buffer) { | |
1040 | if (old_buffer) | |
1041 | auxtrace_buffer__drop_data(old_buffer); | |
23cfcd6d | 1042 | etmq->buf_len = 0; |
9f878b29 MP |
1043 | return 0; |
1044 | } | |
1045 | ||
1046 | etmq->buffer = aux_buffer; | |
1047 | ||
1048 | /* If the aux_buffer doesn't have data associated, try to load it */ | |
1049 | if (!aux_buffer->data) { | |
1050 | /* get the file desc associated with the perf data file */ | |
1051 | int fd = perf_data__fd(etmq->etm->session->data); | |
1052 | ||
1053 | aux_buffer->data = auxtrace_buffer__get_data(aux_buffer, fd); | |
1054 | if (!aux_buffer->data) | |
1055 | return -ENOMEM; | |
1056 | } | |
1057 | ||
1058 | /* If valid, drop the previous buffer */ | |
1059 | if (old_buffer) | |
1060 | auxtrace_buffer__drop_data(old_buffer); | |
1061 | ||
23cfcd6d MP |
1062 | etmq->buf_used = 0; |
1063 | etmq->buf_len = aux_buffer->size; | |
1064 | etmq->buf = aux_buffer->data; | |
9f878b29 | 1065 | |
23cfcd6d | 1066 | return etmq->buf_len; |
9f878b29 MP |
1067 | } |
1068 | ||
3a088799 | 1069 | static void cs_etm__set_pid_tid_cpu(struct cs_etm_auxtrace *etm, |
0abb868b | 1070 | struct cs_etm_traceid_queue *tidq) |
9f878b29 | 1071 | { |
0abb868b | 1072 | if ((!tidq->thread) && (tidq->tid != -1)) |
3c21d7d8 | 1073 | tidq->thread = machine__find_thread(etm->machine, -1, |
0abb868b | 1074 | tidq->tid); |
9f878b29 | 1075 | |
3c21d7d8 | 1076 | if (tidq->thread) |
0abb868b | 1077 | tidq->pid = tidq->thread->pid_; |
9f878b29 MP |
1078 | } |
1079 | ||
0a6be300 MP |
1080 | int cs_etm__etmq_set_tid(struct cs_etm_queue *etmq, |
1081 | pid_t tid, u8 trace_chan_id) | |
1082 | { | |
1083 | int cpu, err = -EINVAL; | |
1084 | struct cs_etm_auxtrace *etm = etmq->etm; | |
1085 | struct cs_etm_traceid_queue *tidq; | |
1086 | ||
1087 | tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id); | |
1088 | if (!tidq) | |
1089 | return err; | |
1090 | ||
1091 | if (cs_etm__get_cpu(trace_chan_id, &cpu) < 0) | |
1092 | return err; | |
1093 | ||
1094 | err = machine__set_current_tid(etm->machine, cpu, tid, tid); | |
1095 | if (err) | |
1096 | return err; | |
1097 | ||
1098 | tidq->tid = tid; | |
1099 | thread__zput(tidq->thread); | |
1100 | ||
1101 | cs_etm__set_pid_tid_cpu(etm, tidq); | |
1102 | return 0; | |
1103 | } | |
1104 | ||
675f302f MP |
1105 | bool cs_etm__etmq_is_timeless(struct cs_etm_queue *etmq) |
1106 | { | |
1107 | return !!etmq->etm->timeless_decoding; | |
1108 | } | |
1109 | ||
a4973d8f LY |
1110 | static void cs_etm__copy_insn(struct cs_etm_queue *etmq, |
1111 | u64 trace_chan_id, | |
1112 | const struct cs_etm_packet *packet, | |
1113 | struct perf_sample *sample) | |
1114 | { | |
1115 | /* | |
1116 | * It's pointless to read instructions for the CS_ETM_DISCONTINUITY | |
1117 | * packet, so directly bail out with 'insn_len' = 0. | |
1118 | */ | |
1119 | if (packet->sample_type == CS_ETM_DISCONTINUITY) { | |
1120 | sample->insn_len = 0; | |
1121 | return; | |
1122 | } | |
1123 | ||
1124 | /* | |
1125 | * T32 instruction size might be 32-bit or 16-bit, decide by calling | |
1126 | * cs_etm__t32_instr_size(). | |
1127 | */ | |
1128 | if (packet->isa == CS_ETM_ISA_T32) | |
1129 | sample->insn_len = cs_etm__t32_instr_size(etmq, trace_chan_id, | |
1130 | sample->ip); | |
1131 | /* Otherwise, A64 and A32 instruction size are always 32-bit. */ | |
1132 | else | |
1133 | sample->insn_len = 4; | |
1134 | ||
1135 | cs_etm__mem_access(etmq, trace_chan_id, sample->ip, | |
1136 | sample->insn_len, (void *)sample->insn); | |
1137 | } | |
1138 | ||
e573e978 | 1139 | static int cs_etm__synth_instruction_sample(struct cs_etm_queue *etmq, |
c7bfa2fd | 1140 | struct cs_etm_traceid_queue *tidq, |
e573e978 RW |
1141 | u64 addr, u64 period) |
1142 | { | |
1143 | int ret = 0; | |
1144 | struct cs_etm_auxtrace *etm = etmq->etm; | |
c7bfa2fd | 1145 | union perf_event *event = tidq->event_buf; |
e573e978 RW |
1146 | struct perf_sample sample = {.ip = 0,}; |
1147 | ||
1148 | event->sample.header.type = PERF_RECORD_SAMPLE; | |
d6c9c05f | 1149 | event->sample.header.misc = cs_etm__cpu_mode(etmq, addr); |
e573e978 RW |
1150 | event->sample.header.size = sizeof(struct perf_event_header); |
1151 | ||
1ac9e0b5 JC |
1152 | if (!etm->timeless_decoding) |
1153 | sample.time = etm->latest_kernel_timestamp; | |
e573e978 | 1154 | sample.ip = addr; |
0abb868b MP |
1155 | sample.pid = tidq->pid; |
1156 | sample.tid = tidq->tid; | |
e573e978 RW |
1157 | sample.id = etmq->etm->instructions_id; |
1158 | sample.stream_id = etmq->etm->instructions_id; | |
1159 | sample.period = period; | |
c7bfa2fd MP |
1160 | sample.cpu = tidq->packet->cpu; |
1161 | sample.flags = tidq->prev_packet->flags; | |
d6c9c05f | 1162 | sample.cpumode = event->sample.header.misc; |
e573e978 | 1163 | |
a4973d8f LY |
1164 | cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->packet, &sample); |
1165 | ||
695378b5 | 1166 | if (etm->synth_opts.last_branch) |
c7bfa2fd | 1167 | sample.branch_stack = tidq->last_branch; |
e573e978 RW |
1168 | |
1169 | if (etm->synth_opts.inject) { | |
1170 | ret = cs_etm__inject_event(event, &sample, | |
1171 | etm->instructions_sample_type); | |
1172 | if (ret) | |
1173 | return ret; | |
1174 | } | |
1175 | ||
1176 | ret = perf_session__deliver_synth_event(etm->session, event, &sample); | |
1177 | ||
1178 | if (ret) | |
1179 | pr_err( | |
1180 | "CS ETM Trace: failed to deliver instruction event, error %d\n", | |
1181 | ret); | |
1182 | ||
e573e978 RW |
1183 | return ret; |
1184 | } | |
1185 | ||
b12235b1 MP |
1186 | /* |
1187 | * The cs etm packet encodes an instruction range between a branch target | |
1188 | * and the next taken branch. Generate sample accordingly. | |
1189 | */ | |
c7bfa2fd MP |
1190 | static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq, |
1191 | struct cs_etm_traceid_queue *tidq) | |
b12235b1 MP |
1192 | { |
1193 | int ret = 0; | |
1194 | struct cs_etm_auxtrace *etm = etmq->etm; | |
1195 | struct perf_sample sample = {.ip = 0,}; | |
c7bfa2fd | 1196 | union perf_event *event = tidq->event_buf; |
e573e978 RW |
1197 | struct dummy_branch_stack { |
1198 | u64 nr; | |
42bbabed | 1199 | u64 hw_idx; |
e573e978 RW |
1200 | struct branch_entry entries; |
1201 | } dummy_bs; | |
d6c9c05f LY |
1202 | u64 ip; |
1203 | ||
c7bfa2fd | 1204 | ip = cs_etm__last_executed_instr(tidq->prev_packet); |
b12235b1 MP |
1205 | |
1206 | event->sample.header.type = PERF_RECORD_SAMPLE; | |
d6c9c05f | 1207 | event->sample.header.misc = cs_etm__cpu_mode(etmq, ip); |
b12235b1 MP |
1208 | event->sample.header.size = sizeof(struct perf_event_header); |
1209 | ||
1ac9e0b5 JC |
1210 | if (!etm->timeless_decoding) |
1211 | sample.time = etm->latest_kernel_timestamp; | |
d6c9c05f | 1212 | sample.ip = ip; |
0abb868b MP |
1213 | sample.pid = tidq->pid; |
1214 | sample.tid = tidq->tid; | |
c7bfa2fd | 1215 | sample.addr = cs_etm__first_executed_instr(tidq->packet); |
b12235b1 MP |
1216 | sample.id = etmq->etm->branches_id; |
1217 | sample.stream_id = etmq->etm->branches_id; | |
1218 | sample.period = 1; | |
c7bfa2fd MP |
1219 | sample.cpu = tidq->packet->cpu; |
1220 | sample.flags = tidq->prev_packet->flags; | |
d6c9c05f | 1221 | sample.cpumode = event->sample.header.misc; |
b12235b1 | 1222 | |
a4973d8f LY |
1223 | cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->prev_packet, |
1224 | &sample); | |
1225 | ||
e573e978 RW |
1226 | /* |
1227 | * perf report cannot handle events without a branch stack | |
1228 | */ | |
1229 | if (etm->synth_opts.last_branch) { | |
1230 | dummy_bs = (struct dummy_branch_stack){ | |
1231 | .nr = 1, | |
42bbabed | 1232 | .hw_idx = -1ULL, |
e573e978 RW |
1233 | .entries = { |
1234 | .from = sample.ip, | |
1235 | .to = sample.addr, | |
1236 | }, | |
1237 | }; | |
1238 | sample.branch_stack = (struct branch_stack *)&dummy_bs; | |
1239 | } | |
1240 | ||
1241 | if (etm->synth_opts.inject) { | |
1242 | ret = cs_etm__inject_event(event, &sample, | |
1243 | etm->branches_sample_type); | |
1244 | if (ret) | |
1245 | return ret; | |
1246 | } | |
1247 | ||
b12235b1 MP |
1248 | ret = perf_session__deliver_synth_event(etm->session, event, &sample); |
1249 | ||
1250 | if (ret) | |
1251 | pr_err( | |
1252 | "CS ETM Trace: failed to deliver instruction event, error %d\n", | |
1253 | ret); | |
1254 | ||
1255 | return ret; | |
1256 | } | |
1257 | ||
1258 | struct cs_etm_synth { | |
1259 | struct perf_tool dummy_tool; | |
1260 | struct perf_session *session; | |
1261 | }; | |
1262 | ||
1263 | static int cs_etm__event_synth(struct perf_tool *tool, | |
1264 | union perf_event *event, | |
1265 | struct perf_sample *sample __maybe_unused, | |
1266 | struct machine *machine __maybe_unused) | |
1267 | { | |
1268 | struct cs_etm_synth *cs_etm_synth = | |
1269 | container_of(tool, struct cs_etm_synth, dummy_tool); | |
1270 | ||
1271 | return perf_session__deliver_synth_event(cs_etm_synth->session, | |
1272 | event, NULL); | |
1273 | } | |
1274 | ||
1275 | static int cs_etm__synth_event(struct perf_session *session, | |
1276 | struct perf_event_attr *attr, u64 id) | |
1277 | { | |
1278 | struct cs_etm_synth cs_etm_synth; | |
1279 | ||
1280 | memset(&cs_etm_synth, 0, sizeof(struct cs_etm_synth)); | |
1281 | cs_etm_synth.session = session; | |
1282 | ||
1283 | return perf_event__synthesize_attr(&cs_etm_synth.dummy_tool, attr, 1, | |
1284 | &id, cs_etm__event_synth); | |
1285 | } | |
1286 | ||
1287 | static int cs_etm__synth_events(struct cs_etm_auxtrace *etm, | |
1288 | struct perf_session *session) | |
1289 | { | |
63503dba | 1290 | struct evlist *evlist = session->evlist; |
32dcd021 | 1291 | struct evsel *evsel; |
b12235b1 MP |
1292 | struct perf_event_attr attr; |
1293 | bool found = false; | |
1294 | u64 id; | |
1295 | int err; | |
1296 | ||
1297 | evlist__for_each_entry(evlist, evsel) { | |
1fc632ce | 1298 | if (evsel->core.attr.type == etm->pmu_type) { |
b12235b1 MP |
1299 | found = true; |
1300 | break; | |
1301 | } | |
1302 | } | |
1303 | ||
1304 | if (!found) { | |
1305 | pr_debug("No selected events with CoreSight Trace data\n"); | |
1306 | return 0; | |
1307 | } | |
1308 | ||
1309 | memset(&attr, 0, sizeof(struct perf_event_attr)); | |
1310 | attr.size = sizeof(struct perf_event_attr); | |
1311 | attr.type = PERF_TYPE_HARDWARE; | |
1fc632ce | 1312 | attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK; |
b12235b1 MP |
1313 | attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID | |
1314 | PERF_SAMPLE_PERIOD; | |
1315 | if (etm->timeless_decoding) | |
1316 | attr.sample_type &= ~(u64)PERF_SAMPLE_TIME; | |
1317 | else | |
1318 | attr.sample_type |= PERF_SAMPLE_TIME; | |
1319 | ||
1fc632ce JO |
1320 | attr.exclude_user = evsel->core.attr.exclude_user; |
1321 | attr.exclude_kernel = evsel->core.attr.exclude_kernel; | |
1322 | attr.exclude_hv = evsel->core.attr.exclude_hv; | |
1323 | attr.exclude_host = evsel->core.attr.exclude_host; | |
1324 | attr.exclude_guest = evsel->core.attr.exclude_guest; | |
1325 | attr.sample_id_all = evsel->core.attr.sample_id_all; | |
1326 | attr.read_format = evsel->core.attr.read_format; | |
b12235b1 MP |
1327 | |
1328 | /* create new id val to be a fixed offset from evsel id */ | |
deaf3219 | 1329 | id = evsel->core.id[0] + 1000000000; |
b12235b1 MP |
1330 | |
1331 | if (!id) | |
1332 | id = 1; | |
1333 | ||
1334 | if (etm->synth_opts.branches) { | |
1335 | attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS; | |
1336 | attr.sample_period = 1; | |
1337 | attr.sample_type |= PERF_SAMPLE_ADDR; | |
1338 | err = cs_etm__synth_event(session, &attr, id); | |
1339 | if (err) | |
1340 | return err; | |
1341 | etm->sample_branches = true; | |
1342 | etm->branches_sample_type = attr.sample_type; | |
1343 | etm->branches_id = id; | |
e573e978 RW |
1344 | id += 1; |
1345 | attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR; | |
1346 | } | |
1347 | ||
f5f8e7e5 | 1348 | if (etm->synth_opts.last_branch) { |
e573e978 | 1349 | attr.sample_type |= PERF_SAMPLE_BRANCH_STACK; |
f5f8e7e5 AG |
1350 | /* |
1351 | * We don't use the hardware index, but the sample generation | |
1352 | * code uses the new format branch_stack with this field, | |
1353 | * so the event attributes must indicate that it's present. | |
1354 | */ | |
1355 | attr.branch_sample_type |= PERF_SAMPLE_BRANCH_HW_INDEX; | |
1356 | } | |
e573e978 RW |
1357 | |
1358 | if (etm->synth_opts.instructions) { | |
1359 | attr.config = PERF_COUNT_HW_INSTRUCTIONS; | |
1360 | attr.sample_period = etm->synth_opts.period; | |
1361 | etm->instructions_sample_period = attr.sample_period; | |
1362 | err = cs_etm__synth_event(session, &attr, id); | |
1363 | if (err) | |
1364 | return err; | |
1365 | etm->sample_instructions = true; | |
1366 | etm->instructions_sample_type = attr.sample_type; | |
1367 | etm->instructions_id = id; | |
1368 | id += 1; | |
b12235b1 MP |
1369 | } |
1370 | ||
1371 | return 0; | |
1372 | } | |
1373 | ||
c7bfa2fd MP |
1374 | static int cs_etm__sample(struct cs_etm_queue *etmq, |
1375 | struct cs_etm_traceid_queue *tidq) | |
b12235b1 | 1376 | { |
e573e978 | 1377 | struct cs_etm_auxtrace *etm = etmq->etm; |
b12235b1 | 1378 | int ret; |
af21577c | 1379 | u8 trace_chan_id = tidq->trace_chan_id; |
c9f5baa1 | 1380 | u64 instrs_prev; |
b12235b1 | 1381 | |
c9f5baa1 LY |
1382 | /* Get instructions remainder from previous packet */ |
1383 | instrs_prev = tidq->period_instructions; | |
1384 | ||
1385 | tidq->period_instructions += tidq->packet->instr_count; | |
e573e978 RW |
1386 | |
1387 | /* | |
1388 | * Record a branch when the last instruction in | |
1389 | * PREV_PACKET is a branch. | |
1390 | */ | |
1391 | if (etm->synth_opts.last_branch && | |
c7bfa2fd MP |
1392 | tidq->prev_packet->sample_type == CS_ETM_RANGE && |
1393 | tidq->prev_packet->last_instr_taken_branch) | |
1394 | cs_etm__update_last_branch_rb(etmq, tidq); | |
e573e978 RW |
1395 | |
1396 | if (etm->sample_instructions && | |
c7bfa2fd | 1397 | tidq->period_instructions >= etm->instructions_sample_period) { |
e573e978 RW |
1398 | /* |
1399 | * Emit instruction sample periodically | |
1400 | * TODO: allow period to be defined in cycles and clock time | |
1401 | */ | |
1402 | ||
e573e978 | 1403 | /* |
c9f5baa1 LY |
1404 | * Below diagram demonstrates the instruction samples |
1405 | * generation flows: | |
1406 | * | |
1407 | * Instrs Instrs Instrs Instrs | |
1408 | * Sample(n) Sample(n+1) Sample(n+2) Sample(n+3) | |
1409 | * | | | | | |
1410 | * V V V V | |
1411 | * -------------------------------------------------- | |
1412 | * ^ ^ | |
1413 | * | | | |
1414 | * Period Period | |
1415 | * instructions(Pi) instructions(Pi') | |
1416 | * | |
1417 | * | | | |
1418 | * \---------------- -----------------/ | |
1419 | * V | |
1420 | * tidq->packet->instr_count | |
1421 | * | |
1422 | * Instrs Sample(n...) are the synthesised samples occurring | |
1423 | * every etm->instructions_sample_period instructions - as | |
1424 | * defined on the perf command line. Sample(n) is being the | |
1425 | * last sample before the current etm packet, n+1 to n+3 | |
1426 | * samples are generated from the current etm packet. | |
1427 | * | |
1428 | * tidq->packet->instr_count represents the number of | |
1429 | * instructions in the current etm packet. | |
1430 | * | |
1431 | * Period instructions (Pi) contains the the number of | |
1432 | * instructions executed after the sample point(n) from the | |
1433 | * previous etm packet. This will always be less than | |
1434 | * etm->instructions_sample_period. | |
1435 | * | |
1436 | * When generate new samples, it combines with two parts | |
1437 | * instructions, one is the tail of the old packet and another | |
1438 | * is the head of the new coming packet, to generate | |
1439 | * sample(n+1); sample(n+2) and sample(n+3) consume the | |
1440 | * instructions with sample period. After sample(n+3), the rest | |
1441 | * instructions will be used by later packet and it is assigned | |
1442 | * to tidq->period_instructions for next round calculation. | |
e573e978 | 1443 | */ |
e573e978 | 1444 | |
c9f5baa1 LY |
1445 | /* |
1446 | * Get the initial offset into the current packet instructions; | |
1447 | * entry conditions ensure that instrs_prev is less than | |
1448 | * etm->instructions_sample_period. | |
1449 | */ | |
1450 | u64 offset = etm->instructions_sample_period - instrs_prev; | |
1451 | u64 addr; | |
1452 | ||
695378b5 LY |
1453 | /* Prepare last branches for instruction sample */ |
1454 | if (etm->synth_opts.last_branch) | |
1455 | cs_etm__copy_last_branch_rb(etmq, tidq); | |
1456 | ||
c9f5baa1 LY |
1457 | while (tidq->period_instructions >= |
1458 | etm->instructions_sample_period) { | |
1459 | /* | |
1460 | * Calculate the address of the sampled instruction (-1 | |
1461 | * as sample is reported as though instruction has just | |
1462 | * been executed, but PC has not advanced to next | |
1463 | * instruction) | |
1464 | */ | |
1465 | addr = cs_etm__instr_addr(etmq, trace_chan_id, | |
1466 | tidq->packet, offset - 1); | |
1467 | ret = cs_etm__synth_instruction_sample( | |
1468 | etmq, tidq, addr, | |
1469 | etm->instructions_sample_period); | |
1470 | if (ret) | |
1471 | return ret; | |
e573e978 | 1472 | |
c9f5baa1 LY |
1473 | offset += etm->instructions_sample_period; |
1474 | tidq->period_instructions -= | |
1475 | etm->instructions_sample_period; | |
1476 | } | |
e573e978 RW |
1477 | } |
1478 | ||
cf0c37b6 | 1479 | if (etm->sample_branches) { |
14a85b1e LY |
1480 | bool generate_sample = false; |
1481 | ||
1482 | /* Generate sample for tracing on packet */ | |
c7bfa2fd | 1483 | if (tidq->prev_packet->sample_type == CS_ETM_DISCONTINUITY) |
14a85b1e LY |
1484 | generate_sample = true; |
1485 | ||
1486 | /* Generate sample for branch taken packet */ | |
c7bfa2fd MP |
1487 | if (tidq->prev_packet->sample_type == CS_ETM_RANGE && |
1488 | tidq->prev_packet->last_instr_taken_branch) | |
14a85b1e LY |
1489 | generate_sample = true; |
1490 | ||
1491 | if (generate_sample) { | |
c7bfa2fd | 1492 | ret = cs_etm__synth_branch_sample(etmq, tidq); |
14a85b1e LY |
1493 | if (ret) |
1494 | return ret; | |
1495 | } | |
e573e978 | 1496 | } |
b12235b1 | 1497 | |
d0175156 | 1498 | cs_etm__packet_swap(etm, tidq); |
b12235b1 MP |
1499 | |
1500 | return 0; | |
1501 | } | |
1502 | ||
c7bfa2fd | 1503 | static int cs_etm__exception(struct cs_etm_traceid_queue *tidq) |
7100b12c LY |
1504 | { |
1505 | /* | |
1506 | * When the exception packet is inserted, whether the last instruction | |
1507 | * in previous range packet is taken branch or not, we need to force | |
1508 | * to set 'prev_packet->last_instr_taken_branch' to true. This ensures | |
1509 | * to generate branch sample for the instruction range before the | |
1510 | * exception is trapped to kernel or before the exception returning. | |
1511 | * | |
1512 | * The exception packet includes the dummy address values, so don't | |
1513 | * swap PACKET with PREV_PACKET. This keeps PREV_PACKET to be useful | |
1514 | * for generating instruction and branch samples. | |
1515 | */ | |
c7bfa2fd MP |
1516 | if (tidq->prev_packet->sample_type == CS_ETM_RANGE) |
1517 | tidq->prev_packet->last_instr_taken_branch = true; | |
7100b12c LY |
1518 | |
1519 | return 0; | |
1520 | } | |
1521 | ||
c7bfa2fd MP |
1522 | static int cs_etm__flush(struct cs_etm_queue *etmq, |
1523 | struct cs_etm_traceid_queue *tidq) | |
256e751c RW |
1524 | { |
1525 | int err = 0; | |
d603b4e9 | 1526 | struct cs_etm_auxtrace *etm = etmq->etm; |
256e751c | 1527 | |
3eb3e07b | 1528 | /* Handle start tracing packet */ |
c7bfa2fd | 1529 | if (tidq->prev_packet->sample_type == CS_ETM_EMPTY) |
3eb3e07b LY |
1530 | goto swap_packet; |
1531 | ||
256e751c | 1532 | if (etmq->etm->synth_opts.last_branch && |
c7bfa2fd | 1533 | tidq->prev_packet->sample_type == CS_ETM_RANGE) { |
695378b5 LY |
1534 | u64 addr; |
1535 | ||
1536 | /* Prepare last branches for instruction sample */ | |
1537 | cs_etm__copy_last_branch_rb(etmq, tidq); | |
1538 | ||
256e751c RW |
1539 | /* |
1540 | * Generate a last branch event for the branches left in the | |
1541 | * circular buffer at the end of the trace. | |
1542 | * | |
1543 | * Use the address of the end of the last reported execution | |
1544 | * range | |
1545 | */ | |
695378b5 | 1546 | addr = cs_etm__last_executed_instr(tidq->prev_packet); |
256e751c RW |
1547 | |
1548 | err = cs_etm__synth_instruction_sample( | |
c7bfa2fd MP |
1549 | etmq, tidq, addr, |
1550 | tidq->period_instructions); | |
6cd4ac6a LY |
1551 | if (err) |
1552 | return err; | |
1553 | ||
c7bfa2fd | 1554 | tidq->period_instructions = 0; |
256e751c | 1555 | |
3eb3e07b LY |
1556 | } |
1557 | ||
d603b4e9 | 1558 | if (etm->sample_branches && |
c7bfa2fd MP |
1559 | tidq->prev_packet->sample_type == CS_ETM_RANGE) { |
1560 | err = cs_etm__synth_branch_sample(etmq, tidq); | |
d603b4e9 LY |
1561 | if (err) |
1562 | return err; | |
1563 | } | |
1564 | ||
3eb3e07b | 1565 | swap_packet: |
d0175156 | 1566 | cs_etm__packet_swap(etm, tidq); |
256e751c | 1567 | |
f1410028 LY |
1568 | /* Reset last branches after flush the trace */ |
1569 | if (etm->synth_opts.last_branch) | |
1570 | cs_etm__reset_last_branch_rb(tidq); | |
1571 | ||
256e751c RW |
1572 | return err; |
1573 | } | |
1574 | ||
c7bfa2fd MP |
1575 | static int cs_etm__end_block(struct cs_etm_queue *etmq, |
1576 | struct cs_etm_traceid_queue *tidq) | |
24fff5eb LY |
1577 | { |
1578 | int err; | |
1579 | ||
1580 | /* | |
1581 | * It has no new packet coming and 'etmq->packet' contains the stale | |
1582 | * packet which was set at the previous time with packets swapping; | |
1583 | * so skip to generate branch sample to avoid stale packet. | |
1584 | * | |
1585 | * For this case only flush branch stack and generate a last branch | |
1586 | * event for the branches left in the circular buffer at the end of | |
1587 | * the trace. | |
1588 | */ | |
1589 | if (etmq->etm->synth_opts.last_branch && | |
c7bfa2fd | 1590 | tidq->prev_packet->sample_type == CS_ETM_RANGE) { |
695378b5 LY |
1591 | u64 addr; |
1592 | ||
1593 | /* Prepare last branches for instruction sample */ | |
1594 | cs_etm__copy_last_branch_rb(etmq, tidq); | |
1595 | ||
24fff5eb LY |
1596 | /* |
1597 | * Use the address of the end of the last reported execution | |
1598 | * range. | |
1599 | */ | |
695378b5 | 1600 | addr = cs_etm__last_executed_instr(tidq->prev_packet); |
24fff5eb LY |
1601 | |
1602 | err = cs_etm__synth_instruction_sample( | |
c7bfa2fd MP |
1603 | etmq, tidq, addr, |
1604 | tidq->period_instructions); | |
24fff5eb LY |
1605 | if (err) |
1606 | return err; | |
1607 | ||
c7bfa2fd | 1608 | tidq->period_instructions = 0; |
24fff5eb LY |
1609 | } |
1610 | ||
1611 | return 0; | |
1612 | } | |
8224531c MP |
1613 | /* |
1614 | * cs_etm__get_data_block: Fetch a block from the auxtrace_buffer queue | |
1615 | * if need be. | |
1616 | * Returns: < 0 if error | |
1617 | * = 0 if no more auxtrace_buffer to read | |
1618 | * > 0 if the current buffer isn't empty yet | |
1619 | */ | |
1620 | static int cs_etm__get_data_block(struct cs_etm_queue *etmq) | |
1621 | { | |
1622 | int ret; | |
1623 | ||
1624 | if (!etmq->buf_len) { | |
1625 | ret = cs_etm__get_trace(etmq); | |
1626 | if (ret <= 0) | |
1627 | return ret; | |
1628 | /* | |
1629 | * We cannot assume consecutive blocks in the data file | |
1630 | * are contiguous, reset the decoder to force re-sync. | |
1631 | */ | |
1632 | ret = cs_etm_decoder__reset(etmq->decoder); | |
1633 | if (ret) | |
1634 | return ret; | |
1635 | } | |
1636 | ||
1637 | return etmq->buf_len; | |
1638 | } | |
24fff5eb | 1639 | |
af21577c | 1640 | static bool cs_etm__is_svc_instr(struct cs_etm_queue *etmq, u8 trace_chan_id, |
96dce7f4 LY |
1641 | struct cs_etm_packet *packet, |
1642 | u64 end_addr) | |
1643 | { | |
c152d4d4 MP |
1644 | /* Initialise to keep compiler happy */ |
1645 | u16 instr16 = 0; | |
1646 | u32 instr32 = 0; | |
96dce7f4 LY |
1647 | u64 addr; |
1648 | ||
1649 | switch (packet->isa) { | |
1650 | case CS_ETM_ISA_T32: | |
1651 | /* | |
1652 | * The SVC of T32 is defined in ARM DDI 0487D.a, F5.1.247: | |
1653 | * | |
1654 | * b'15 b'8 | |
1655 | * +-----------------+--------+ | |
1656 | * | 1 1 0 1 1 1 1 1 | imm8 | | |
1657 | * +-----------------+--------+ | |
1658 | * | |
4d39c89f | 1659 | * According to the specification, it only defines SVC for T32 |
96dce7f4 LY |
1660 | * with 16 bits instruction and has no definition for 32bits; |
1661 | * so below only read 2 bytes as instruction size for T32. | |
1662 | */ | |
1663 | addr = end_addr - 2; | |
af21577c MP |
1664 | cs_etm__mem_access(etmq, trace_chan_id, addr, |
1665 | sizeof(instr16), (u8 *)&instr16); | |
96dce7f4 LY |
1666 | if ((instr16 & 0xFF00) == 0xDF00) |
1667 | return true; | |
1668 | ||
1669 | break; | |
1670 | case CS_ETM_ISA_A32: | |
1671 | /* | |
1672 | * The SVC of A32 is defined in ARM DDI 0487D.a, F5.1.247: | |
1673 | * | |
1674 | * b'31 b'28 b'27 b'24 | |
1675 | * +---------+---------+-------------------------+ | |
1676 | * | !1111 | 1 1 1 1 | imm24 | | |
1677 | * +---------+---------+-------------------------+ | |
1678 | */ | |
1679 | addr = end_addr - 4; | |
af21577c MP |
1680 | cs_etm__mem_access(etmq, trace_chan_id, addr, |
1681 | sizeof(instr32), (u8 *)&instr32); | |
96dce7f4 LY |
1682 | if ((instr32 & 0x0F000000) == 0x0F000000 && |
1683 | (instr32 & 0xF0000000) != 0xF0000000) | |
1684 | return true; | |
1685 | ||
1686 | break; | |
1687 | case CS_ETM_ISA_A64: | |
1688 | /* | |
1689 | * The SVC of A64 is defined in ARM DDI 0487D.a, C6.2.294: | |
1690 | * | |
1691 | * b'31 b'21 b'4 b'0 | |
1692 | * +-----------------------+---------+-----------+ | |
1693 | * | 1 1 0 1 0 1 0 0 0 0 0 | imm16 | 0 0 0 0 1 | | |
1694 | * +-----------------------+---------+-----------+ | |
1695 | */ | |
1696 | addr = end_addr - 4; | |
af21577c MP |
1697 | cs_etm__mem_access(etmq, trace_chan_id, addr, |
1698 | sizeof(instr32), (u8 *)&instr32); | |
96dce7f4 LY |
1699 | if ((instr32 & 0xFFE0001F) == 0xd4000001) |
1700 | return true; | |
1701 | ||
1702 | break; | |
1703 | case CS_ETM_ISA_UNKNOWN: | |
1704 | default: | |
1705 | break; | |
1706 | } | |
1707 | ||
1708 | return false; | |
1709 | } | |
1710 | ||
c7bfa2fd MP |
1711 | static bool cs_etm__is_syscall(struct cs_etm_queue *etmq, |
1712 | struct cs_etm_traceid_queue *tidq, u64 magic) | |
96dce7f4 | 1713 | { |
af21577c | 1714 | u8 trace_chan_id = tidq->trace_chan_id; |
c7bfa2fd MP |
1715 | struct cs_etm_packet *packet = tidq->packet; |
1716 | struct cs_etm_packet *prev_packet = tidq->prev_packet; | |
96dce7f4 LY |
1717 | |
1718 | if (magic == __perf_cs_etmv3_magic) | |
1719 | if (packet->exception_number == CS_ETMV3_EXC_SVC) | |
1720 | return true; | |
1721 | ||
1722 | /* | |
1723 | * ETMv4 exception type CS_ETMV4_EXC_CALL covers SVC, SMC and | |
1724 | * HVC cases; need to check if it's SVC instruction based on | |
1725 | * packet address. | |
1726 | */ | |
1727 | if (magic == __perf_cs_etmv4_magic) { | |
1728 | if (packet->exception_number == CS_ETMV4_EXC_CALL && | |
af21577c | 1729 | cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet, |
96dce7f4 LY |
1730 | prev_packet->end_addr)) |
1731 | return true; | |
1732 | } | |
1733 | ||
1734 | return false; | |
1735 | } | |
1736 | ||
c7bfa2fd MP |
1737 | static bool cs_etm__is_async_exception(struct cs_etm_traceid_queue *tidq, |
1738 | u64 magic) | |
96dce7f4 | 1739 | { |
c7bfa2fd | 1740 | struct cs_etm_packet *packet = tidq->packet; |
96dce7f4 LY |
1741 | |
1742 | if (magic == __perf_cs_etmv3_magic) | |
1743 | if (packet->exception_number == CS_ETMV3_EXC_DEBUG_HALT || | |
1744 | packet->exception_number == CS_ETMV3_EXC_ASYNC_DATA_ABORT || | |
1745 | packet->exception_number == CS_ETMV3_EXC_PE_RESET || | |
1746 | packet->exception_number == CS_ETMV3_EXC_IRQ || | |
1747 | packet->exception_number == CS_ETMV3_EXC_FIQ) | |
1748 | return true; | |
1749 | ||
1750 | if (magic == __perf_cs_etmv4_magic) | |
1751 | if (packet->exception_number == CS_ETMV4_EXC_RESET || | |
1752 | packet->exception_number == CS_ETMV4_EXC_DEBUG_HALT || | |
1753 | packet->exception_number == CS_ETMV4_EXC_SYSTEM_ERROR || | |
1754 | packet->exception_number == CS_ETMV4_EXC_INST_DEBUG || | |
1755 | packet->exception_number == CS_ETMV4_EXC_DATA_DEBUG || | |
1756 | packet->exception_number == CS_ETMV4_EXC_IRQ || | |
1757 | packet->exception_number == CS_ETMV4_EXC_FIQ) | |
1758 | return true; | |
1759 | ||
1760 | return false; | |
1761 | } | |
1762 | ||
c7bfa2fd MP |
1763 | static bool cs_etm__is_sync_exception(struct cs_etm_queue *etmq, |
1764 | struct cs_etm_traceid_queue *tidq, | |
1765 | u64 magic) | |
96dce7f4 | 1766 | { |
af21577c | 1767 | u8 trace_chan_id = tidq->trace_chan_id; |
c7bfa2fd MP |
1768 | struct cs_etm_packet *packet = tidq->packet; |
1769 | struct cs_etm_packet *prev_packet = tidq->prev_packet; | |
96dce7f4 LY |
1770 | |
1771 | if (magic == __perf_cs_etmv3_magic) | |
1772 | if (packet->exception_number == CS_ETMV3_EXC_SMC || | |
1773 | packet->exception_number == CS_ETMV3_EXC_HYP || | |
1774 | packet->exception_number == CS_ETMV3_EXC_JAZELLE_THUMBEE || | |
1775 | packet->exception_number == CS_ETMV3_EXC_UNDEFINED_INSTR || | |
1776 | packet->exception_number == CS_ETMV3_EXC_PREFETCH_ABORT || | |
1777 | packet->exception_number == CS_ETMV3_EXC_DATA_FAULT || | |
1778 | packet->exception_number == CS_ETMV3_EXC_GENERIC) | |
1779 | return true; | |
1780 | ||
1781 | if (magic == __perf_cs_etmv4_magic) { | |
1782 | if (packet->exception_number == CS_ETMV4_EXC_TRAP || | |
1783 | packet->exception_number == CS_ETMV4_EXC_ALIGNMENT || | |
1784 | packet->exception_number == CS_ETMV4_EXC_INST_FAULT || | |
1785 | packet->exception_number == CS_ETMV4_EXC_DATA_FAULT) | |
1786 | return true; | |
1787 | ||
1788 | /* | |
1789 | * For CS_ETMV4_EXC_CALL, except SVC other instructions | |
1790 | * (SMC, HVC) are taken as sync exceptions. | |
1791 | */ | |
1792 | if (packet->exception_number == CS_ETMV4_EXC_CALL && | |
af21577c | 1793 | !cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet, |
96dce7f4 LY |
1794 | prev_packet->end_addr)) |
1795 | return true; | |
1796 | ||
1797 | /* | |
1798 | * ETMv4 has 5 bits for exception number; if the numbers | |
1799 | * are in the range ( CS_ETMV4_EXC_FIQ, CS_ETMV4_EXC_END ] | |
1800 | * they are implementation defined exceptions. | |
1801 | * | |
1802 | * For this case, simply take it as sync exception. | |
1803 | */ | |
1804 | if (packet->exception_number > CS_ETMV4_EXC_FIQ && | |
1805 | packet->exception_number <= CS_ETMV4_EXC_END) | |
1806 | return true; | |
1807 | } | |
1808 | ||
1809 | return false; | |
1810 | } | |
1811 | ||
c7bfa2fd MP |
1812 | static int cs_etm__set_sample_flags(struct cs_etm_queue *etmq, |
1813 | struct cs_etm_traceid_queue *tidq) | |
06220bf4 | 1814 | { |
c7bfa2fd MP |
1815 | struct cs_etm_packet *packet = tidq->packet; |
1816 | struct cs_etm_packet *prev_packet = tidq->prev_packet; | |
af21577c | 1817 | u8 trace_chan_id = tidq->trace_chan_id; |
96dce7f4 LY |
1818 | u64 magic; |
1819 | int ret; | |
06220bf4 LY |
1820 | |
1821 | switch (packet->sample_type) { | |
1822 | case CS_ETM_RANGE: | |
1823 | /* | |
1824 | * Immediate branch instruction without neither link nor | |
1825 | * return flag, it's normal branch instruction within | |
1826 | * the function. | |
1827 | */ | |
1828 | if (packet->last_instr_type == OCSD_INSTR_BR && | |
1829 | packet->last_instr_subtype == OCSD_S_INSTR_NONE) { | |
1830 | packet->flags = PERF_IP_FLAG_BRANCH; | |
1831 | ||
1832 | if (packet->last_instr_cond) | |
1833 | packet->flags |= PERF_IP_FLAG_CONDITIONAL; | |
1834 | } | |
1835 | ||
1836 | /* | |
1837 | * Immediate branch instruction with link (e.g. BL), this is | |
1838 | * branch instruction for function call. | |
1839 | */ | |
1840 | if (packet->last_instr_type == OCSD_INSTR_BR && | |
1841 | packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK) | |
1842 | packet->flags = PERF_IP_FLAG_BRANCH | | |
1843 | PERF_IP_FLAG_CALL; | |
1844 | ||
1845 | /* | |
1846 | * Indirect branch instruction with link (e.g. BLR), this is | |
1847 | * branch instruction for function call. | |
1848 | */ | |
1849 | if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT && | |
1850 | packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK) | |
1851 | packet->flags = PERF_IP_FLAG_BRANCH | | |
1852 | PERF_IP_FLAG_CALL; | |
1853 | ||
1854 | /* | |
1855 | * Indirect branch instruction with subtype of | |
1856 | * OCSD_S_INSTR_V7_IMPLIED_RET, this is explicit hint for | |
1857 | * function return for A32/T32. | |
1858 | */ | |
1859 | if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT && | |
1860 | packet->last_instr_subtype == OCSD_S_INSTR_V7_IMPLIED_RET) | |
1861 | packet->flags = PERF_IP_FLAG_BRANCH | | |
1862 | PERF_IP_FLAG_RETURN; | |
1863 | ||
1864 | /* | |
1865 | * Indirect branch instruction without link (e.g. BR), usually | |
1866 | * this is used for function return, especially for functions | |
1867 | * within dynamic link lib. | |
1868 | */ | |
1869 | if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT && | |
1870 | packet->last_instr_subtype == OCSD_S_INSTR_NONE) | |
1871 | packet->flags = PERF_IP_FLAG_BRANCH | | |
1872 | PERF_IP_FLAG_RETURN; | |
1873 | ||
1874 | /* Return instruction for function return. */ | |
1875 | if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT && | |
1876 | packet->last_instr_subtype == OCSD_S_INSTR_V8_RET) | |
1877 | packet->flags = PERF_IP_FLAG_BRANCH | | |
1878 | PERF_IP_FLAG_RETURN; | |
465eaaa8 LY |
1879 | |
1880 | /* | |
1881 | * Decoder might insert a discontinuity in the middle of | |
1882 | * instruction packets, fixup prev_packet with flag | |
1883 | * PERF_IP_FLAG_TRACE_BEGIN to indicate restarting trace. | |
1884 | */ | |
1885 | if (prev_packet->sample_type == CS_ETM_DISCONTINUITY) | |
1886 | prev_packet->flags |= PERF_IP_FLAG_BRANCH | | |
1887 | PERF_IP_FLAG_TRACE_BEGIN; | |
173e65f6 LY |
1888 | |
1889 | /* | |
1890 | * If the previous packet is an exception return packet | |
4d39c89f | 1891 | * and the return address just follows SVC instruction, |
173e65f6 LY |
1892 | * it needs to calibrate the previous packet sample flags |
1893 | * as PERF_IP_FLAG_SYSCALLRET. | |
1894 | */ | |
1895 | if (prev_packet->flags == (PERF_IP_FLAG_BRANCH | | |
1896 | PERF_IP_FLAG_RETURN | | |
1897 | PERF_IP_FLAG_INTERRUPT) && | |
af21577c MP |
1898 | cs_etm__is_svc_instr(etmq, trace_chan_id, |
1899 | packet, packet->start_addr)) | |
173e65f6 LY |
1900 | prev_packet->flags = PERF_IP_FLAG_BRANCH | |
1901 | PERF_IP_FLAG_RETURN | | |
1902 | PERF_IP_FLAG_SYSCALLRET; | |
06220bf4 LY |
1903 | break; |
1904 | case CS_ETM_DISCONTINUITY: | |
465eaaa8 LY |
1905 | /* |
1906 | * The trace is discontinuous, if the previous packet is | |
1907 | * instruction packet, set flag PERF_IP_FLAG_TRACE_END | |
1908 | * for previous packet. | |
1909 | */ | |
1910 | if (prev_packet->sample_type == CS_ETM_RANGE) | |
1911 | prev_packet->flags |= PERF_IP_FLAG_BRANCH | | |
1912 | PERF_IP_FLAG_TRACE_END; | |
1913 | break; | |
06220bf4 | 1914 | case CS_ETM_EXCEPTION: |
96dce7f4 LY |
1915 | ret = cs_etm__get_magic(packet->trace_chan_id, &magic); |
1916 | if (ret) | |
1917 | return ret; | |
1918 | ||
1919 | /* The exception is for system call. */ | |
c7bfa2fd | 1920 | if (cs_etm__is_syscall(etmq, tidq, magic)) |
96dce7f4 LY |
1921 | packet->flags = PERF_IP_FLAG_BRANCH | |
1922 | PERF_IP_FLAG_CALL | | |
1923 | PERF_IP_FLAG_SYSCALLRET; | |
1924 | /* | |
1925 | * The exceptions are triggered by external signals from bus, | |
1926 | * interrupt controller, debug module, PE reset or halt. | |
1927 | */ | |
c7bfa2fd | 1928 | else if (cs_etm__is_async_exception(tidq, magic)) |
96dce7f4 LY |
1929 | packet->flags = PERF_IP_FLAG_BRANCH | |
1930 | PERF_IP_FLAG_CALL | | |
1931 | PERF_IP_FLAG_ASYNC | | |
1932 | PERF_IP_FLAG_INTERRUPT; | |
1933 | /* | |
1934 | * Otherwise, exception is caused by trap, instruction & | |
1935 | * data fault, or alignment errors. | |
1936 | */ | |
c7bfa2fd | 1937 | else if (cs_etm__is_sync_exception(etmq, tidq, magic)) |
96dce7f4 LY |
1938 | packet->flags = PERF_IP_FLAG_BRANCH | |
1939 | PERF_IP_FLAG_CALL | | |
1940 | PERF_IP_FLAG_INTERRUPT; | |
1941 | ||
1942 | /* | |
1943 | * When the exception packet is inserted, since exception | |
1944 | * packet is not used standalone for generating samples | |
1945 | * and it's affiliation to the previous instruction range | |
1946 | * packet; so set previous range packet flags to tell perf | |
1947 | * it is an exception taken branch. | |
1948 | */ | |
1949 | if (prev_packet->sample_type == CS_ETM_RANGE) | |
1950 | prev_packet->flags = packet->flags; | |
1951 | break; | |
06220bf4 | 1952 | case CS_ETM_EXCEPTION_RET: |
173e65f6 LY |
1953 | /* |
1954 | * When the exception return packet is inserted, since | |
1955 | * exception return packet is not used standalone for | |
1956 | * generating samples and it's affiliation to the previous | |
1957 | * instruction range packet; so set previous range packet | |
1958 | * flags to tell perf it is an exception return branch. | |
1959 | * | |
1960 | * The exception return can be for either system call or | |
1961 | * other exception types; unfortunately the packet doesn't | |
1962 | * contain exception type related info so we cannot decide | |
1963 | * the exception type purely based on exception return packet. | |
1964 | * If we record the exception number from exception packet and | |
4d39c89f | 1965 | * reuse it for exception return packet, this is not reliable |
173e65f6 LY |
1966 | * due the trace can be discontinuity or the interrupt can |
1967 | * be nested, thus the recorded exception number cannot be | |
1968 | * used for exception return packet for these two cases. | |
1969 | * | |
1970 | * For exception return packet, we only need to distinguish the | |
1971 | * packet is for system call or for other types. Thus the | |
1972 | * decision can be deferred when receive the next packet which | |
1973 | * contains the return address, based on the return address we | |
1974 | * can read out the previous instruction and check if it's a | |
1975 | * system call instruction and then calibrate the sample flag | |
1976 | * as needed. | |
1977 | */ | |
1978 | if (prev_packet->sample_type == CS_ETM_RANGE) | |
1979 | prev_packet->flags = PERF_IP_FLAG_BRANCH | | |
1980 | PERF_IP_FLAG_RETURN | | |
1981 | PERF_IP_FLAG_INTERRUPT; | |
1982 | break; | |
06220bf4 LY |
1983 | case CS_ETM_EMPTY: |
1984 | default: | |
1985 | break; | |
1986 | } | |
1987 | ||
1988 | return 0; | |
1989 | } | |
1990 | ||
f74f349c MP |
1991 | static int cs_etm__decode_data_block(struct cs_etm_queue *etmq) |
1992 | { | |
1993 | int ret = 0; | |
1994 | size_t processed = 0; | |
1995 | ||
1996 | /* | |
1997 | * Packets are decoded and added to the decoder's packet queue | |
1998 | * until the decoder packet processing callback has requested that | |
1999 | * processing stops or there is nothing left in the buffer. Normal | |
2000 | * operations that stop processing are a timestamp packet or a full | |
2001 | * decoder buffer queue. | |
2002 | */ | |
2003 | ret = cs_etm_decoder__process_data_block(etmq->decoder, | |
2004 | etmq->offset, | |
2005 | &etmq->buf[etmq->buf_used], | |
2006 | etmq->buf_len, | |
2007 | &processed); | |
2008 | if (ret) | |
2009 | goto out; | |
2010 | ||
2011 | etmq->offset += processed; | |
2012 | etmq->buf_used += processed; | |
2013 | etmq->buf_len -= processed; | |
2014 | ||
2015 | out: | |
2016 | return ret; | |
2017 | } | |
2018 | ||
c7bfa2fd MP |
2019 | static int cs_etm__process_traceid_queue(struct cs_etm_queue *etmq, |
2020 | struct cs_etm_traceid_queue *tidq) | |
3fa0e83e MP |
2021 | { |
2022 | int ret; | |
5f7cb035 MP |
2023 | struct cs_etm_packet_queue *packet_queue; |
2024 | ||
c7bfa2fd | 2025 | packet_queue = &tidq->packet_queue; |
3fa0e83e | 2026 | |
882f4874 MP |
2027 | /* Process each packet in this chunk */ |
2028 | while (1) { | |
2029 | ret = cs_etm_decoder__get_packet(packet_queue, | |
c7bfa2fd | 2030 | tidq->packet); |
882f4874 MP |
2031 | if (ret <= 0) |
2032 | /* | |
2033 | * Stop processing this chunk on | |
2034 | * end of data or error | |
2035 | */ | |
2036 | break; | |
3fa0e83e | 2037 | |
882f4874 MP |
2038 | /* |
2039 | * Since packet addresses are swapped in packet | |
2040 | * handling within below switch() statements, | |
2041 | * thus setting sample flags must be called | |
2042 | * prior to switch() statement to use address | |
2043 | * information before packets swapping. | |
2044 | */ | |
c7bfa2fd | 2045 | ret = cs_etm__set_sample_flags(etmq, tidq); |
882f4874 MP |
2046 | if (ret < 0) |
2047 | break; | |
2048 | ||
c7bfa2fd | 2049 | switch (tidq->packet->sample_type) { |
882f4874 MP |
2050 | case CS_ETM_RANGE: |
2051 | /* | |
2052 | * If the packet contains an instruction | |
2053 | * range, generate instruction sequence | |
2054 | * events. | |
2055 | */ | |
c7bfa2fd | 2056 | cs_etm__sample(etmq, tidq); |
882f4874 MP |
2057 | break; |
2058 | case CS_ETM_EXCEPTION: | |
2059 | case CS_ETM_EXCEPTION_RET: | |
3fa0e83e | 2060 | /* |
882f4874 MP |
2061 | * If the exception packet is coming, |
2062 | * make sure the previous instruction | |
2063 | * range packet to be handled properly. | |
3fa0e83e | 2064 | */ |
c7bfa2fd | 2065 | cs_etm__exception(tidq); |
882f4874 MP |
2066 | break; |
2067 | case CS_ETM_DISCONTINUITY: | |
2068 | /* | |
2069 | * Discontinuity in trace, flush | |
2070 | * previous branch stack | |
2071 | */ | |
c7bfa2fd | 2072 | cs_etm__flush(etmq, tidq); |
882f4874 MP |
2073 | break; |
2074 | case CS_ETM_EMPTY: | |
2075 | /* | |
2076 | * Should not receive empty packet, | |
2077 | * report error. | |
2078 | */ | |
2079 | pr_err("CS ETM Trace: empty packet\n"); | |
2080 | return -EINVAL; | |
2081 | default: | |
2082 | break; | |
3fa0e83e | 2083 | } |
882f4874 | 2084 | } |
3fa0e83e MP |
2085 | |
2086 | return ret; | |
2087 | } | |
2088 | ||
21fe8dc1 MP |
2089 | static void cs_etm__clear_all_traceid_queues(struct cs_etm_queue *etmq) |
2090 | { | |
2091 | int idx; | |
2092 | struct int_node *inode; | |
2093 | struct cs_etm_traceid_queue *tidq; | |
2094 | struct intlist *traceid_queues_list = etmq->traceid_queues_list; | |
2095 | ||
2096 | intlist__for_each_entry(inode, traceid_queues_list) { | |
2097 | idx = (int)(intptr_t)inode->priv; | |
2098 | tidq = etmq->traceid_queues[idx]; | |
2099 | ||
2100 | /* Ignore return value */ | |
2101 | cs_etm__process_traceid_queue(etmq, tidq); | |
2102 | ||
2103 | /* | |
2104 | * Generate an instruction sample with the remaining | |
2105 | * branchstack entries. | |
2106 | */ | |
2107 | cs_etm__flush(etmq, tidq); | |
2108 | } | |
2109 | } | |
2110 | ||
9f878b29 MP |
2111 | static int cs_etm__run_decoder(struct cs_etm_queue *etmq) |
2112 | { | |
9f878b29 | 2113 | int err = 0; |
c7bfa2fd MP |
2114 | struct cs_etm_traceid_queue *tidq; |
2115 | ||
2116 | tidq = cs_etm__etmq_get_traceid_queue(etmq, CS_ETM_PER_THREAD_TRACEID); | |
2117 | if (!tidq) | |
2118 | return -EINVAL; | |
9f878b29 | 2119 | |
9f878b29 | 2120 | /* Go through each buffer in the queue and decode them one by one */ |
e573e978 | 2121 | while (1) { |
8224531c MP |
2122 | err = cs_etm__get_data_block(etmq); |
2123 | if (err <= 0) | |
2124 | return err; | |
9f878b29 | 2125 | |
e573e978 RW |
2126 | /* Run trace decoder until buffer consumed or end of trace */ |
2127 | do { | |
f74f349c | 2128 | err = cs_etm__decode_data_block(etmq); |
e573e978 RW |
2129 | if (err) |
2130 | return err; | |
2131 | ||
3fa0e83e MP |
2132 | /* |
2133 | * Process each packet in this chunk, nothing to do if | |
2134 | * an error occurs other than hoping the next one will | |
2135 | * be better. | |
2136 | */ | |
c7bfa2fd | 2137 | err = cs_etm__process_traceid_queue(etmq, tidq); |
e573e978 | 2138 | |
23cfcd6d | 2139 | } while (etmq->buf_len); |
b12235b1 | 2140 | |
256e751c RW |
2141 | if (err == 0) |
2142 | /* Flush any remaining branch stack entries */ | |
c7bfa2fd | 2143 | err = cs_etm__end_block(etmq, tidq); |
e573e978 | 2144 | } |
9f878b29 MP |
2145 | |
2146 | return err; | |
2147 | } | |
2148 | ||
2149 | static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm, | |
fc7ac413 | 2150 | pid_t tid) |
9f878b29 MP |
2151 | { |
2152 | unsigned int i; | |
2153 | struct auxtrace_queues *queues = &etm->queues; | |
2154 | ||
2155 | for (i = 0; i < queues->nr_queues; i++) { | |
2156 | struct auxtrace_queue *queue = &etm->queues.queue_array[i]; | |
2157 | struct cs_etm_queue *etmq = queue->priv; | |
0abb868b MP |
2158 | struct cs_etm_traceid_queue *tidq; |
2159 | ||
2160 | if (!etmq) | |
2161 | continue; | |
2162 | ||
2163 | tidq = cs_etm__etmq_get_traceid_queue(etmq, | |
2164 | CS_ETM_PER_THREAD_TRACEID); | |
2165 | ||
2166 | if (!tidq) | |
2167 | continue; | |
9f878b29 | 2168 | |
0abb868b | 2169 | if ((tid == -1) || (tidq->tid == tid)) { |
0a6be300 | 2170 | cs_etm__set_pid_tid_cpu(etm, tidq); |
9f878b29 MP |
2171 | cs_etm__run_decoder(etmq); |
2172 | } | |
2173 | } | |
2174 | ||
2175 | return 0; | |
2176 | } | |
2177 | ||
21fe8dc1 MP |
2178 | static int cs_etm__process_queues(struct cs_etm_auxtrace *etm) |
2179 | { | |
2180 | int ret = 0; | |
9ac8afd5 | 2181 | unsigned int cs_queue_nr, queue_nr, i; |
21fe8dc1 | 2182 | u8 trace_chan_id; |
aadd6ba4 | 2183 | u64 cs_timestamp; |
21fe8dc1 MP |
2184 | struct auxtrace_queue *queue; |
2185 | struct cs_etm_queue *etmq; | |
2186 | struct cs_etm_traceid_queue *tidq; | |
2187 | ||
9ac8afd5 JC |
2188 | /* |
2189 | * Pre-populate the heap with one entry from each queue so that we can | |
2190 | * start processing in time order across all queues. | |
2191 | */ | |
2192 | for (i = 0; i < etm->queues.nr_queues; i++) { | |
2193 | etmq = etm->queues.queue_array[i].priv; | |
2194 | if (!etmq) | |
2195 | continue; | |
2196 | ||
2197 | ret = cs_etm__queue_first_cs_timestamp(etm, etmq, i); | |
2198 | if (ret) | |
2199 | return ret; | |
2200 | } | |
2201 | ||
21fe8dc1 MP |
2202 | while (1) { |
2203 | if (!etm->heap.heap_cnt) | |
2204 | goto out; | |
2205 | ||
2206 | /* Take the entry at the top of the min heap */ | |
2207 | cs_queue_nr = etm->heap.heap_array[0].queue_nr; | |
2208 | queue_nr = TO_QUEUE_NR(cs_queue_nr); | |
2209 | trace_chan_id = TO_TRACE_CHAN_ID(cs_queue_nr); | |
2210 | queue = &etm->queues.queue_array[queue_nr]; | |
2211 | etmq = queue->priv; | |
2212 | ||
2213 | /* | |
2214 | * Remove the top entry from the heap since we are about | |
2215 | * to process it. | |
2216 | */ | |
2217 | auxtrace_heap__pop(&etm->heap); | |
2218 | ||
2219 | tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id); | |
2220 | if (!tidq) { | |
2221 | /* | |
2222 | * No traceID queue has been allocated for this traceID, | |
2223 | * which means something somewhere went very wrong. No | |
2224 | * other choice than simply exit. | |
2225 | */ | |
2226 | ret = -EINVAL; | |
2227 | goto out; | |
2228 | } | |
2229 | ||
2230 | /* | |
2231 | * Packets associated with this timestamp are already in | |
2232 | * the etmq's traceID queue, so process them. | |
2233 | */ | |
2234 | ret = cs_etm__process_traceid_queue(etmq, tidq); | |
2235 | if (ret < 0) | |
2236 | goto out; | |
2237 | ||
2238 | /* | |
2239 | * Packets for this timestamp have been processed, time to | |
2240 | * move on to the next timestamp, fetching a new auxtrace_buffer | |
2241 | * if need be. | |
2242 | */ | |
2243 | refetch: | |
2244 | ret = cs_etm__get_data_block(etmq); | |
2245 | if (ret < 0) | |
2246 | goto out; | |
2247 | ||
2248 | /* | |
2249 | * No more auxtrace_buffers to process in this etmq, simply | |
2250 | * move on to another entry in the auxtrace_heap. | |
2251 | */ | |
2252 | if (!ret) | |
2253 | continue; | |
2254 | ||
2255 | ret = cs_etm__decode_data_block(etmq); | |
2256 | if (ret) | |
2257 | goto out; | |
2258 | ||
aadd6ba4 | 2259 | cs_timestamp = cs_etm__etmq_get_timestamp(etmq, &trace_chan_id); |
21fe8dc1 | 2260 | |
aadd6ba4 | 2261 | if (!cs_timestamp) { |
21fe8dc1 MP |
2262 | /* |
2263 | * Function cs_etm__decode_data_block() returns when | |
2264 | * there is no more traces to decode in the current | |
2265 | * auxtrace_buffer OR when a timestamp has been | |
2266 | * encountered on any of the traceID queues. Since we | |
2267 | * did not get a timestamp, there is no more traces to | |
2268 | * process in this auxtrace_buffer. As such empty and | |
2269 | * flush all traceID queues. | |
2270 | */ | |
2271 | cs_etm__clear_all_traceid_queues(etmq); | |
2272 | ||
2273 | /* Fetch another auxtrace_buffer for this etmq */ | |
2274 | goto refetch; | |
2275 | } | |
2276 | ||
2277 | /* | |
2278 | * Add to the min heap the timestamp for packets that have | |
2279 | * just been decoded. They will be processed and synthesized | |
2280 | * during the next call to cs_etm__process_traceid_queue() for | |
2281 | * this queue/traceID. | |
2282 | */ | |
2283 | cs_queue_nr = TO_CS_QUEUE_NR(queue_nr, trace_chan_id); | |
aadd6ba4 | 2284 | ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, cs_timestamp); |
21fe8dc1 MP |
2285 | } |
2286 | ||
2287 | out: | |
2288 | return ret; | |
2289 | } | |
2290 | ||
a465f3c3 MP |
2291 | static int cs_etm__process_itrace_start(struct cs_etm_auxtrace *etm, |
2292 | union perf_event *event) | |
2293 | { | |
2294 | struct thread *th; | |
2295 | ||
2296 | if (etm->timeless_decoding) | |
2297 | return 0; | |
2298 | ||
2299 | /* | |
2300 | * Add the tid/pid to the log so that we can get a match when | |
2301 | * we get a contextID from the decoder. | |
2302 | */ | |
2303 | th = machine__findnew_thread(etm->machine, | |
2304 | event->itrace_start.pid, | |
2305 | event->itrace_start.tid); | |
2306 | if (!th) | |
2307 | return -ENOMEM; | |
2308 | ||
2309 | thread__put(th); | |
2310 | ||
2311 | return 0; | |
2312 | } | |
2313 | ||
e0d170fa MP |
2314 | static int cs_etm__process_switch_cpu_wide(struct cs_etm_auxtrace *etm, |
2315 | union perf_event *event) | |
2316 | { | |
2317 | struct thread *th; | |
2318 | bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT; | |
2319 | ||
2320 | /* | |
2321 | * Context switch in per-thread mode are irrelevant since perf | |
2322 | * will start/stop tracing as the process is scheduled. | |
2323 | */ | |
2324 | if (etm->timeless_decoding) | |
2325 | return 0; | |
2326 | ||
2327 | /* | |
2328 | * SWITCH_IN events carry the next process to be switched out while | |
2329 | * SWITCH_OUT events carry the process to be switched in. As such | |
2330 | * we don't care about IN events. | |
2331 | */ | |
2332 | if (!out) | |
2333 | return 0; | |
2334 | ||
2335 | /* | |
2336 | * Add the tid/pid to the log so that we can get a match when | |
2337 | * we get a contextID from the decoder. | |
2338 | */ | |
2339 | th = machine__findnew_thread(etm->machine, | |
2340 | event->context_switch.next_prev_pid, | |
2341 | event->context_switch.next_prev_tid); | |
2342 | if (!th) | |
2343 | return -ENOMEM; | |
2344 | ||
2345 | thread__put(th); | |
2346 | ||
2347 | return 0; | |
2348 | } | |
2349 | ||
440a23b3 MP |
2350 | static int cs_etm__process_event(struct perf_session *session, |
2351 | union perf_event *event, | |
2352 | struct perf_sample *sample, | |
2353 | struct perf_tool *tool) | |
2354 | { | |
aadd6ba4 | 2355 | u64 sample_kernel_timestamp; |
20d9c478 MP |
2356 | struct cs_etm_auxtrace *etm = container_of(session->auxtrace, |
2357 | struct cs_etm_auxtrace, | |
2358 | auxtrace); | |
2359 | ||
20d9c478 MP |
2360 | if (dump_trace) |
2361 | return 0; | |
2362 | ||
2363 | if (!tool->ordered_events) { | |
2364 | pr_err("CoreSight ETM Trace requires ordered events\n"); | |
2365 | return -EINVAL; | |
2366 | } | |
2367 | ||
20d9c478 | 2368 | if (sample->time && (sample->time != (u64) -1)) |
aadd6ba4 | 2369 | sample_kernel_timestamp = sample->time; |
20d9c478 | 2370 | else |
aadd6ba4 | 2371 | sample_kernel_timestamp = 0; |
20d9c478 | 2372 | |
0323dea3 JC |
2373 | /* |
2374 | * Don't wait for cs_etm__flush_events() in per-thread/timeless mode to start the decode. We | |
2375 | * need the tid of the PERF_RECORD_EXIT event to assign to the synthesised samples because | |
2376 | * ETM_OPT_CTXTID is not enabled. | |
2377 | */ | |
21fe8dc1 MP |
2378 | if (etm->timeless_decoding && |
2379 | event->header.type == PERF_RECORD_EXIT) | |
9f878b29 | 2380 | return cs_etm__process_timeless_queues(etm, |
fc7ac413 | 2381 | event->fork.tid); |
9f878b29 | 2382 | |
a465f3c3 MP |
2383 | if (event->header.type == PERF_RECORD_ITRACE_START) |
2384 | return cs_etm__process_itrace_start(etm, event); | |
e0d170fa MP |
2385 | else if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) |
2386 | return cs_etm__process_switch_cpu_wide(etm, event); | |
a465f3c3 | 2387 | |
1ac9e0b5 JC |
2388 | if (!etm->timeless_decoding && event->header.type == PERF_RECORD_AUX) { |
2389 | /* | |
2390 | * Record the latest kernel timestamp available in the header | |
2391 | * for samples so that synthesised samples occur from this point | |
2392 | * onwards. | |
2393 | */ | |
2394 | etm->latest_kernel_timestamp = sample_kernel_timestamp; | |
1ac9e0b5 | 2395 | } |
21fe8dc1 | 2396 | |
440a23b3 MP |
2397 | return 0; |
2398 | } | |
2399 | ||
48e8a7b5 JC |
2400 | static void dump_queued_data(struct cs_etm_auxtrace *etm, |
2401 | struct perf_record_auxtrace *event) | |
2402 | { | |
2403 | struct auxtrace_buffer *buf; | |
2404 | unsigned int i; | |
2405 | /* | |
2406 | * Find all buffers with same reference in the queues and dump them. | |
2407 | * This is because the queues can contain multiple entries of the same | |
2408 | * buffer that were split on aux records. | |
2409 | */ | |
2410 | for (i = 0; i < etm->queues.nr_queues; ++i) | |
2411 | list_for_each_entry(buf, &etm->queues.queue_array[i].head, list) | |
2412 | if (buf->reference == event->reference) | |
04aaad26 | 2413 | cs_etm__dump_event(etm->queues.queue_array[i].priv, buf); |
48e8a7b5 JC |
2414 | } |
2415 | ||
440a23b3 MP |
2416 | static int cs_etm__process_auxtrace_event(struct perf_session *session, |
2417 | union perf_event *event, | |
68ffe390 | 2418 | struct perf_tool *tool __maybe_unused) |
440a23b3 | 2419 | { |
68ffe390 MP |
2420 | struct cs_etm_auxtrace *etm = container_of(session->auxtrace, |
2421 | struct cs_etm_auxtrace, | |
2422 | auxtrace); | |
2423 | if (!etm->data_queued) { | |
2424 | struct auxtrace_buffer *buffer; | |
2425 | off_t data_offset; | |
2426 | int fd = perf_data__fd(session->data); | |
2427 | bool is_pipe = perf_data__is_pipe(session->data); | |
2428 | int err; | |
ca50db59 | 2429 | int idx = event->auxtrace.idx; |
68ffe390 MP |
2430 | |
2431 | if (is_pipe) | |
2432 | data_offset = 0; | |
2433 | else { | |
2434 | data_offset = lseek(fd, 0, SEEK_CUR); | |
2435 | if (data_offset == -1) | |
2436 | return -errno; | |
2437 | } | |
2438 | ||
2439 | err = auxtrace_queues__add_event(&etm->queues, session, | |
2440 | event, data_offset, &buffer); | |
2441 | if (err) | |
2442 | return err; | |
2443 | ||
9182f04a JC |
2444 | /* |
2445 | * Knowing if the trace is formatted or not requires a lookup of | |
2446 | * the aux record so only works in non-piped mode where data is | |
2447 | * queued in cs_etm__queue_aux_records(). Always assume | |
2448 | * formatted in piped mode (true). | |
2449 | */ | |
ca50db59 | 2450 | err = cs_etm__setup_queue(etm, &etm->queues.queue_array[idx], |
9182f04a | 2451 | idx, true); |
ca50db59 JC |
2452 | if (err) |
2453 | return err; | |
2454 | ||
68ffe390 MP |
2455 | if (dump_trace) |
2456 | if (auxtrace_buffer__get_data(buffer, fd)) { | |
04aaad26 | 2457 | cs_etm__dump_event(etm->queues.queue_array[idx].priv, buffer); |
68ffe390 MP |
2458 | auxtrace_buffer__put_data(buffer); |
2459 | } | |
48e8a7b5 JC |
2460 | } else if (dump_trace) |
2461 | dump_queued_data(etm, &event->auxtrace); | |
68ffe390 | 2462 | |
440a23b3 MP |
2463 | return 0; |
2464 | } | |
2465 | ||
2466 | static bool cs_etm__is_timeless_decoding(struct cs_etm_auxtrace *etm) | |
2467 | { | |
32dcd021 | 2468 | struct evsel *evsel; |
63503dba | 2469 | struct evlist *evlist = etm->session->evlist; |
440a23b3 MP |
2470 | bool timeless_decoding = true; |
2471 | ||
c36c1ef6 JC |
2472 | /* Override timeless mode with user input from --itrace=Z */ |
2473 | if (etm->synth_opts.timeless_decoding) | |
2474 | return true; | |
2475 | ||
440a23b3 MP |
2476 | /* |
2477 | * Circle through the list of event and complain if we find one | |
2478 | * with the time bit set. | |
2479 | */ | |
2480 | evlist__for_each_entry(evlist, evsel) { | |
1fc632ce | 2481 | if ((evsel->core.attr.sample_type & PERF_SAMPLE_TIME)) |
440a23b3 MP |
2482 | timeless_decoding = false; |
2483 | } | |
2484 | ||
2485 | return timeless_decoding; | |
2486 | } | |
2487 | ||
cd8bfd8c | 2488 | static const char * const cs_etm_global_header_fmts[] = { |
42b2b570 | 2489 | [CS_HEADER_VERSION] = " Header version %llx\n", |
cd8bfd8c TJ |
2490 | [CS_PMU_TYPE_CPUS] = " PMU type/num cpus %llx\n", |
2491 | [CS_ETM_SNAPSHOT] = " Snapshot %llx\n", | |
2492 | }; | |
2493 | ||
2494 | static const char * const cs_etm_priv_fmts[] = { | |
2495 | [CS_ETM_MAGIC] = " Magic number %llx\n", | |
2496 | [CS_ETM_CPU] = " CPU %lld\n", | |
42b2b570 | 2497 | [CS_ETM_NR_TRC_PARAMS] = " NR_TRC_PARAMS %llx\n", |
cd8bfd8c TJ |
2498 | [CS_ETM_ETMCR] = " ETMCR %llx\n", |
2499 | [CS_ETM_ETMTRACEIDR] = " ETMTRACEIDR %llx\n", | |
2500 | [CS_ETM_ETMCCER] = " ETMCCER %llx\n", | |
2501 | [CS_ETM_ETMIDR] = " ETMIDR %llx\n", | |
2502 | }; | |
2503 | ||
2504 | static const char * const cs_etmv4_priv_fmts[] = { | |
2505 | [CS_ETM_MAGIC] = " Magic number %llx\n", | |
2506 | [CS_ETM_CPU] = " CPU %lld\n", | |
42b2b570 | 2507 | [CS_ETM_NR_TRC_PARAMS] = " NR_TRC_PARAMS %llx\n", |
cd8bfd8c TJ |
2508 | [CS_ETMV4_TRCCONFIGR] = " TRCCONFIGR %llx\n", |
2509 | [CS_ETMV4_TRCTRACEIDR] = " TRCTRACEIDR %llx\n", | |
2510 | [CS_ETMV4_TRCIDR0] = " TRCIDR0 %llx\n", | |
2511 | [CS_ETMV4_TRCIDR1] = " TRCIDR1 %llx\n", | |
2512 | [CS_ETMV4_TRCIDR2] = " TRCIDR2 %llx\n", | |
2513 | [CS_ETMV4_TRCIDR8] = " TRCIDR8 %llx\n", | |
2514 | [CS_ETMV4_TRCAUTHSTATUS] = " TRCAUTHSTATUS %llx\n", | |
2515 | }; | |
2516 | ||
42b2b570 ML |
2517 | static const char * const param_unk_fmt = |
2518 | " Unknown parameter [%d] %llx\n"; | |
2519 | static const char * const magic_unk_fmt = | |
2520 | " Magic number Unknown %llx\n"; | |
2521 | ||
2522 | static int cs_etm__print_cpu_metadata_v0(__u64 *val, int *offset) | |
cd8bfd8c | 2523 | { |
42b2b570 ML |
2524 | int i = *offset, j, nr_params = 0, fmt_offset; |
2525 | __u64 magic; | |
cd8bfd8c | 2526 | |
42b2b570 ML |
2527 | /* check magic value */ |
2528 | magic = val[i + CS_ETM_MAGIC]; | |
2529 | if ((magic != __perf_cs_etmv3_magic) && | |
2530 | (magic != __perf_cs_etmv4_magic)) { | |
2531 | /* failure - note bad magic value */ | |
2532 | fprintf(stdout, magic_unk_fmt, magic); | |
2533 | return -EINVAL; | |
2534 | } | |
2535 | ||
2536 | /* print common header block */ | |
2537 | fprintf(stdout, cs_etm_priv_fmts[CS_ETM_MAGIC], val[i++]); | |
2538 | fprintf(stdout, cs_etm_priv_fmts[CS_ETM_CPU], val[i++]); | |
2539 | ||
2540 | if (magic == __perf_cs_etmv3_magic) { | |
2541 | nr_params = CS_ETM_NR_TRC_PARAMS_V0; | |
2542 | fmt_offset = CS_ETM_ETMCR; | |
2543 | /* after common block, offset format index past NR_PARAMS */ | |
2544 | for (j = fmt_offset; j < nr_params + fmt_offset; j++, i++) | |
2545 | fprintf(stdout, cs_etm_priv_fmts[j], val[i]); | |
2546 | } else if (magic == __perf_cs_etmv4_magic) { | |
2547 | nr_params = CS_ETMV4_NR_TRC_PARAMS_V0; | |
2548 | fmt_offset = CS_ETMV4_TRCCONFIGR; | |
2549 | /* after common block, offset format index past NR_PARAMS */ | |
2550 | for (j = fmt_offset; j < nr_params + fmt_offset; j++, i++) | |
2551 | fprintf(stdout, cs_etmv4_priv_fmts[j], val[i]); | |
2552 | } | |
2553 | *offset = i; | |
2554 | return 0; | |
2555 | } | |
2556 | ||
2557 | static int cs_etm__print_cpu_metadata_v1(__u64 *val, int *offset) | |
2558 | { | |
2559 | int i = *offset, j, total_params = 0; | |
2560 | __u64 magic; | |
2561 | ||
2562 | magic = val[i + CS_ETM_MAGIC]; | |
2563 | /* total params to print is NR_PARAMS + common block size for v1 */ | |
2564 | total_params = val[i + CS_ETM_NR_TRC_PARAMS] + CS_ETM_COMMON_BLK_MAX_V1; | |
cd8bfd8c | 2565 | |
42b2b570 ML |
2566 | if (magic == __perf_cs_etmv3_magic) { |
2567 | for (j = 0; j < total_params; j++, i++) { | |
2568 | /* if newer record - could be excess params */ | |
2569 | if (j >= CS_ETM_PRIV_MAX) | |
2570 | fprintf(stdout, param_unk_fmt, j, val[i]); | |
2571 | else | |
cd8bfd8c | 2572 | fprintf(stdout, cs_etm_priv_fmts[j], val[i]); |
42b2b570 ML |
2573 | } |
2574 | } else if (magic == __perf_cs_etmv4_magic) { | |
2575 | for (j = 0; j < total_params; j++, i++) { | |
2576 | /* if newer record - could be excess params */ | |
2577 | if (j >= CS_ETMV4_PRIV_MAX) | |
2578 | fprintf(stdout, param_unk_fmt, j, val[i]); | |
2579 | else | |
cd8bfd8c | 2580 | fprintf(stdout, cs_etmv4_priv_fmts[j], val[i]); |
42b2b570 ML |
2581 | } |
2582 | } else { | |
2583 | /* failure - note bad magic value and error out */ | |
2584 | fprintf(stdout, magic_unk_fmt, magic); | |
2585 | return -EINVAL; | |
2586 | } | |
2587 | *offset = i; | |
2588 | return 0; | |
2589 | } | |
2590 | ||
2591 | static void cs_etm__print_auxtrace_info(__u64 *val, int num) | |
2592 | { | |
2593 | int i, cpu = 0, version, err; | |
2594 | ||
2595 | /* bail out early on bad header version */ | |
2596 | version = val[0]; | |
2597 | if (version > CS_HEADER_CURRENT_VERSION) { | |
2598 | /* failure.. return */ | |
2599 | fprintf(stdout, " Unknown Header Version = %x, ", version); | |
2600 | fprintf(stdout, "Version supported <= %x\n", CS_HEADER_CURRENT_VERSION); | |
2601 | return; | |
2602 | } | |
2603 | ||
2604 | for (i = 0; i < CS_HEADER_VERSION_MAX; i++) | |
2605 | fprintf(stdout, cs_etm_global_header_fmts[i], val[i]); | |
2606 | ||
2607 | for (i = CS_HEADER_VERSION_MAX; cpu < num; cpu++) { | |
2608 | if (version == 0) | |
2609 | err = cs_etm__print_cpu_metadata_v0(val, &i); | |
2610 | else if (version == 1) | |
2611 | err = cs_etm__print_cpu_metadata_v1(val, &i); | |
2612 | if (err) | |
cd8bfd8c TJ |
2613 | return; |
2614 | } | |
2615 | } | |
2616 | ||
42b2b570 ML |
2617 | /* |
2618 | * Read a single cpu parameter block from the auxtrace_info priv block. | |
2619 | * | |
2620 | * For version 1 there is a per cpu nr_params entry. If we are handling | |
2621 | * version 1 file, then there may be less, the same, or more params | |
2622 | * indicated by this value than the compile time number we understand. | |
2623 | * | |
2624 | * For a version 0 info block, there are a fixed number, and we need to | |
2625 | * fill out the nr_param value in the metadata we create. | |
2626 | */ | |
2627 | static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, | |
2628 | int out_blk_size, int nr_params_v0) | |
2629 | { | |
2630 | u64 *metadata = NULL; | |
2631 | int hdr_version; | |
2632 | int nr_in_params, nr_out_params, nr_cmn_params; | |
2633 | int i, k; | |
2634 | ||
2635 | metadata = zalloc(sizeof(*metadata) * out_blk_size); | |
2636 | if (!metadata) | |
2637 | return NULL; | |
2638 | ||
2639 | /* read block current index & version */ | |
2640 | i = *buff_in_offset; | |
2641 | hdr_version = buff_in[CS_HEADER_VERSION]; | |
2642 | ||
2643 | if (!hdr_version) { | |
2644 | /* read version 0 info block into a version 1 metadata block */ | |
2645 | nr_in_params = nr_params_v0; | |
2646 | metadata[CS_ETM_MAGIC] = buff_in[i + CS_ETM_MAGIC]; | |
2647 | metadata[CS_ETM_CPU] = buff_in[i + CS_ETM_CPU]; | |
2648 | metadata[CS_ETM_NR_TRC_PARAMS] = nr_in_params; | |
2649 | /* remaining block params at offset +1 from source */ | |
2650 | for (k = CS_ETM_COMMON_BLK_MAX_V1 - 1; k < nr_in_params; k++) | |
2651 | metadata[k + 1] = buff_in[i + k]; | |
2652 | /* version 0 has 2 common params */ | |
2653 | nr_cmn_params = 2; | |
2654 | } else { | |
2655 | /* read version 1 info block - input and output nr_params may differ */ | |
2656 | /* version 1 has 3 common params */ | |
2657 | nr_cmn_params = 3; | |
2658 | nr_in_params = buff_in[i + CS_ETM_NR_TRC_PARAMS]; | |
2659 | ||
2660 | /* if input has more params than output - skip excess */ | |
2661 | nr_out_params = nr_in_params + nr_cmn_params; | |
2662 | if (nr_out_params > out_blk_size) | |
2663 | nr_out_params = out_blk_size; | |
2664 | ||
2665 | for (k = CS_ETM_MAGIC; k < nr_out_params; k++) | |
2666 | metadata[k] = buff_in[i + k]; | |
2667 | ||
2668 | /* record the actual nr params we copied */ | |
2669 | metadata[CS_ETM_NR_TRC_PARAMS] = nr_out_params - nr_cmn_params; | |
2670 | } | |
2671 | ||
2672 | /* adjust in offset by number of in params used */ | |
2673 | i += nr_in_params + nr_cmn_params; | |
2674 | *buff_in_offset = i; | |
2675 | return metadata; | |
2676 | } | |
2677 | ||
83d1fc92 JC |
2678 | /** |
2679 | * Puts a fragment of an auxtrace buffer into the auxtrace queues based | |
2680 | * on the bounds of aux_event, if it matches with the buffer that's at | |
2681 | * file_offset. | |
2682 | * | |
2683 | * Normally, whole auxtrace buffers would be added to the queue. But we | |
2684 | * want to reset the decoder for every PERF_RECORD_AUX event, and the decoder | |
2685 | * is reset across each buffer, so splitting the buffers up in advance has | |
2686 | * the same effect. | |
2687 | */ | |
2688 | static int cs_etm__queue_aux_fragment(struct perf_session *session, off_t file_offset, size_t sz, | |
2689 | struct perf_record_aux *aux_event, struct perf_sample *sample) | |
2690 | { | |
2691 | int err; | |
2692 | char buf[PERF_SAMPLE_MAX_SIZE]; | |
2693 | union perf_event *auxtrace_event_union; | |
2694 | struct perf_record_auxtrace *auxtrace_event; | |
2695 | union perf_event auxtrace_fragment; | |
2696 | __u64 aux_offset, aux_size; | |
ca50db59 | 2697 | __u32 idx; |
9182f04a | 2698 | bool formatted; |
83d1fc92 JC |
2699 | |
2700 | struct cs_etm_auxtrace *etm = container_of(session->auxtrace, | |
2701 | struct cs_etm_auxtrace, | |
2702 | auxtrace); | |
2703 | ||
2704 | /* | |
2705 | * There should be a PERF_RECORD_AUXTRACE event at the file_offset that we got | |
2706 | * from looping through the auxtrace index. | |
2707 | */ | |
2708 | err = perf_session__peek_event(session, file_offset, buf, | |
2709 | PERF_SAMPLE_MAX_SIZE, &auxtrace_event_union, NULL); | |
2710 | if (err) | |
2711 | return err; | |
2712 | auxtrace_event = &auxtrace_event_union->auxtrace; | |
2713 | if (auxtrace_event->header.type != PERF_RECORD_AUXTRACE) | |
2714 | return -EINVAL; | |
2715 | ||
2716 | if (auxtrace_event->header.size < sizeof(struct perf_record_auxtrace) || | |
2717 | auxtrace_event->header.size != sz) { | |
2718 | return -EINVAL; | |
2719 | } | |
2720 | ||
2721 | /* | |
2722 | * In per-thread mode, CPU is set to -1, but TID will be set instead. See | |
2723 | * auxtrace_mmap_params__set_idx(). Return 'not found' if neither CPU nor TID match. | |
2724 | */ | |
2725 | if ((auxtrace_event->cpu == (__u32) -1 && auxtrace_event->tid != sample->tid) || | |
2726 | auxtrace_event->cpu != sample->cpu) | |
2727 | return 1; | |
2728 | ||
2729 | if (aux_event->flags & PERF_AUX_FLAG_OVERWRITE) { | |
2730 | /* | |
2731 | * Clamp size in snapshot mode. The buffer size is clamped in | |
2732 | * __auxtrace_mmap__read() for snapshots, so the aux record size doesn't reflect | |
2733 | * the buffer size. | |
2734 | */ | |
2735 | aux_size = min(aux_event->aux_size, auxtrace_event->size); | |
2736 | ||
2737 | /* | |
2738 | * In this mode, the head also points to the end of the buffer so aux_offset | |
2739 | * needs to have the size subtracted so it points to the beginning as in normal mode | |
2740 | */ | |
2741 | aux_offset = aux_event->aux_offset - aux_size; | |
2742 | } else { | |
2743 | aux_size = aux_event->aux_size; | |
2744 | aux_offset = aux_event->aux_offset; | |
2745 | } | |
2746 | ||
2747 | if (aux_offset >= auxtrace_event->offset && | |
2748 | aux_offset + aux_size <= auxtrace_event->offset + auxtrace_event->size) { | |
2749 | /* | |
2750 | * If this AUX event was inside this buffer somewhere, create a new auxtrace event | |
2751 | * based on the sizes of the aux event, and queue that fragment. | |
2752 | */ | |
2753 | auxtrace_fragment.auxtrace = *auxtrace_event; | |
2754 | auxtrace_fragment.auxtrace.size = aux_size; | |
2755 | auxtrace_fragment.auxtrace.offset = aux_offset; | |
2756 | file_offset += aux_offset - auxtrace_event->offset + auxtrace_event->header.size; | |
2757 | ||
2758 | pr_debug3("CS ETM: Queue buffer size: %#"PRI_lx64" offset: %#"PRI_lx64 | |
2759 | " tid: %d cpu: %d\n", aux_size, aux_offset, sample->tid, sample->cpu); | |
ca50db59 JC |
2760 | err = auxtrace_queues__add_event(&etm->queues, session, &auxtrace_fragment, |
2761 | file_offset, NULL); | |
2762 | if (err) | |
2763 | return err; | |
2764 | ||
2765 | idx = auxtrace_event->idx; | |
9182f04a JC |
2766 | formatted = !(aux_event->flags & PERF_AUX_FLAG_CORESIGHT_FORMAT_RAW); |
2767 | return cs_etm__setup_queue(etm, &etm->queues.queue_array[idx], | |
2768 | idx, formatted); | |
83d1fc92 JC |
2769 | } |
2770 | ||
2771 | /* Wasn't inside this buffer, but there were no parse errors. 1 == 'not found' */ | |
2772 | return 1; | |
2773 | } | |
2774 | ||
2775 | static int cs_etm__queue_aux_records_cb(struct perf_session *session, union perf_event *event, | |
2776 | u64 offset __maybe_unused, void *data __maybe_unused) | |
2777 | { | |
2778 | struct perf_sample sample; | |
2779 | int ret; | |
2780 | struct auxtrace_index_entry *ent; | |
2781 | struct auxtrace_index *auxtrace_index; | |
2782 | struct evsel *evsel; | |
2783 | size_t i; | |
2784 | ||
2785 | /* Don't care about any other events, we're only queuing buffers for AUX events */ | |
2786 | if (event->header.type != PERF_RECORD_AUX) | |
2787 | return 0; | |
2788 | ||
2789 | if (event->header.size < sizeof(struct perf_record_aux)) | |
2790 | return -EINVAL; | |
2791 | ||
2792 | /* Truncated Aux records can have 0 size and shouldn't result in anything being queued. */ | |
2793 | if (!event->aux.aux_size) | |
2794 | return 0; | |
2795 | ||
2796 | /* | |
2797 | * Parse the sample, we need the sample_id_all data that comes after the event so that the | |
2798 | * CPU or PID can be matched to an AUXTRACE buffer's CPU or PID. | |
2799 | */ | |
2800 | evsel = evlist__event2evsel(session->evlist, event); | |
2801 | if (!evsel) | |
2802 | return -EINVAL; | |
2803 | ret = evsel__parse_sample(evsel, event, &sample); | |
2804 | if (ret) | |
2805 | return ret; | |
2806 | ||
2807 | /* | |
2808 | * Loop through the auxtrace index to find the buffer that matches up with this aux event. | |
2809 | */ | |
2810 | list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) { | |
2811 | for (i = 0; i < auxtrace_index->nr; i++) { | |
2812 | ent = &auxtrace_index->entries[i]; | |
2813 | ret = cs_etm__queue_aux_fragment(session, ent->file_offset, | |
2814 | ent->sz, &event->aux, &sample); | |
2815 | /* | |
2816 | * Stop search on error or successful values. Continue search on | |
2817 | * 1 ('not found') | |
2818 | */ | |
2819 | if (ret != 1) | |
2820 | return ret; | |
2821 | } | |
2822 | } | |
2823 | ||
2824 | /* | |
2825 | * Couldn't find the buffer corresponding to this aux record, something went wrong. Warn but | |
2826 | * don't exit with an error because it will still be possible to decode other aux records. | |
2827 | */ | |
2828 | pr_err("CS ETM: Couldn't find auxtrace buffer for aux_offset: %#"PRI_lx64 | |
2829 | " tid: %d cpu: %d\n", event->aux.aux_offset, sample.tid, sample.cpu); | |
2830 | return 0; | |
2831 | } | |
2832 | ||
2833 | static int cs_etm__queue_aux_records(struct perf_session *session) | |
2834 | { | |
2835 | struct auxtrace_index *index = list_first_entry_or_null(&session->auxtrace_index, | |
2836 | struct auxtrace_index, list); | |
2837 | if (index && index->nr > 0) | |
2838 | return perf_session__peek_events(session, session->header.data_offset, | |
2839 | session->header.data_size, | |
2840 | cs_etm__queue_aux_records_cb, NULL); | |
2841 | ||
2842 | /* | |
2843 | * We would get here if there are no entries in the index (either no auxtrace | |
2844 | * buffers or no index at all). Fail silently as there is the possibility of | |
2845 | * queueing them in cs_etm__process_auxtrace_event() if etm->data_queued is still | |
2846 | * false. | |
2847 | * | |
2848 | * In that scenario, buffers will not be split by AUX records. | |
2849 | */ | |
2850 | return 0; | |
2851 | } | |
2852 | ||
440a23b3 MP |
2853 | int cs_etm__process_auxtrace_info(union perf_event *event, |
2854 | struct perf_session *session) | |
2855 | { | |
72932371 | 2856 | struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info; |
440a23b3 | 2857 | struct cs_etm_auxtrace *etm = NULL; |
cd8bfd8c TJ |
2858 | struct int_node *inode; |
2859 | unsigned int pmu_type; | |
440a23b3 MP |
2860 | int event_header_size = sizeof(struct perf_event_header); |
2861 | int info_header_size; | |
2862 | int total_size = auxtrace_info->header.size; | |
cd8bfd8c | 2863 | int priv_size = 0; |
42b2b570 ML |
2864 | int num_cpu, trcidr_idx; |
2865 | int err = 0; | |
2866 | int i, j; | |
cd8bfd8c TJ |
2867 | u64 *ptr, *hdr = NULL; |
2868 | u64 **metadata = NULL; | |
42b2b570 | 2869 | u64 hdr_version; |
440a23b3 MP |
2870 | |
2871 | /* | |
2872 | * sizeof(auxtrace_info_event::type) + | |
2873 | * sizeof(auxtrace_info_event::reserved) == 8 | |
2874 | */ | |
2875 | info_header_size = 8; | |
2876 | ||
2877 | if (total_size < (event_header_size + info_header_size)) | |
2878 | return -EINVAL; | |
2879 | ||
cd8bfd8c TJ |
2880 | priv_size = total_size - event_header_size - info_header_size; |
2881 | ||
2882 | /* First the global part */ | |
2883 | ptr = (u64 *) auxtrace_info->priv; | |
2884 | ||
42b2b570 ML |
2885 | /* Look for version of the header */ |
2886 | hdr_version = ptr[0]; | |
2887 | if (hdr_version > CS_HEADER_CURRENT_VERSION) { | |
2888 | /* print routine will print an error on bad version */ | |
2889 | if (dump_trace) | |
2890 | cs_etm__print_auxtrace_info(auxtrace_info->priv, 0); | |
cd8bfd8c | 2891 | return -EINVAL; |
42b2b570 | 2892 | } |
cd8bfd8c | 2893 | |
42b2b570 | 2894 | hdr = zalloc(sizeof(*hdr) * CS_HEADER_VERSION_MAX); |
cd8bfd8c TJ |
2895 | if (!hdr) |
2896 | return -ENOMEM; | |
2897 | ||
2898 | /* Extract header information - see cs-etm.h for format */ | |
42b2b570 | 2899 | for (i = 0; i < CS_HEADER_VERSION_MAX; i++) |
cd8bfd8c TJ |
2900 | hdr[i] = ptr[i]; |
2901 | num_cpu = hdr[CS_PMU_TYPE_CPUS] & 0xffffffff; | |
2902 | pmu_type = (unsigned int) ((hdr[CS_PMU_TYPE_CPUS] >> 32) & | |
2903 | 0xffffffff); | |
2904 | ||
2905 | /* | |
95c6fe97 LY |
2906 | * Create an RB tree for traceID-metadata tuple. Since the conversion |
2907 | * has to be made for each packet that gets decoded, optimizing access | |
2908 | * in anything other than a sequential array is worth doing. | |
cd8bfd8c TJ |
2909 | */ |
2910 | traceid_list = intlist__new(NULL); | |
2911 | if (!traceid_list) { | |
2912 | err = -ENOMEM; | |
2913 | goto err_free_hdr; | |
2914 | } | |
2915 | ||
2916 | metadata = zalloc(sizeof(*metadata) * num_cpu); | |
2917 | if (!metadata) { | |
2918 | err = -ENOMEM; | |
2919 | goto err_free_traceid_list; | |
2920 | } | |
2921 | ||
2922 | /* | |
2923 | * The metadata is stored in the auxtrace_info section and encodes | |
2924 | * the configuration of the ARM embedded trace macrocell which is | |
2925 | * required by the trace decoder to properly decode the trace due | |
2926 | * to its highly compressed nature. | |
2927 | */ | |
2928 | for (j = 0; j < num_cpu; j++) { | |
2929 | if (ptr[i] == __perf_cs_etmv3_magic) { | |
42b2b570 ML |
2930 | metadata[j] = |
2931 | cs_etm__create_meta_blk(ptr, &i, | |
2932 | CS_ETM_PRIV_MAX, | |
2933 | CS_ETM_NR_TRC_PARAMS_V0); | |
cd8bfd8c TJ |
2934 | |
2935 | /* The traceID is our handle */ | |
42b2b570 ML |
2936 | trcidr_idx = CS_ETM_ETMTRACEIDR; |
2937 | ||
cd8bfd8c | 2938 | } else if (ptr[i] == __perf_cs_etmv4_magic) { |
42b2b570 ML |
2939 | metadata[j] = |
2940 | cs_etm__create_meta_blk(ptr, &i, | |
2941 | CS_ETMV4_PRIV_MAX, | |
2942 | CS_ETMV4_NR_TRC_PARAMS_V0); | |
cd8bfd8c TJ |
2943 | |
2944 | /* The traceID is our handle */ | |
42b2b570 ML |
2945 | trcidr_idx = CS_ETMV4_TRCTRACEIDR; |
2946 | } | |
2947 | ||
2948 | if (!metadata[j]) { | |
2949 | err = -ENOMEM; | |
2950 | goto err_free_metadata; | |
cd8bfd8c TJ |
2951 | } |
2952 | ||
2953 | /* Get an RB node for this CPU */ | |
42b2b570 | 2954 | inode = intlist__findnew(traceid_list, metadata[j][trcidr_idx]); |
cd8bfd8c TJ |
2955 | |
2956 | /* Something went wrong, no need to continue */ | |
2957 | if (!inode) { | |
edc82a99 | 2958 | err = -ENOMEM; |
cd8bfd8c TJ |
2959 | goto err_free_metadata; |
2960 | } | |
2961 | ||
2962 | /* | |
2963 | * The node for that CPU should not be taken. | |
2964 | * Back out if that's the case. | |
2965 | */ | |
2966 | if (inode->priv) { | |
2967 | err = -EINVAL; | |
2968 | goto err_free_metadata; | |
2969 | } | |
95c6fe97 LY |
2970 | /* All good, associate the traceID with the metadata pointer */ |
2971 | inode->priv = metadata[j]; | |
cd8bfd8c TJ |
2972 | } |
2973 | ||
2974 | /* | |
42b2b570 | 2975 | * Each of CS_HEADER_VERSION_MAX, CS_ETM_PRIV_MAX and |
cd8bfd8c TJ |
2976 | * CS_ETMV4_PRIV_MAX mark how many double words are in the |
2977 | * global metadata, and each cpu's metadata respectively. | |
2978 | * The following tests if the correct number of double words was | |
2979 | * present in the auxtrace info section. | |
2980 | */ | |
2981 | if (i * 8 != priv_size) { | |
2982 | err = -EINVAL; | |
2983 | goto err_free_metadata; | |
2984 | } | |
2985 | ||
440a23b3 MP |
2986 | etm = zalloc(sizeof(*etm)); |
2987 | ||
cd8bfd8c | 2988 | if (!etm) { |
440a23b3 | 2989 | err = -ENOMEM; |
cd8bfd8c TJ |
2990 | goto err_free_metadata; |
2991 | } | |
440a23b3 MP |
2992 | |
2993 | err = auxtrace_queues__init(&etm->queues); | |
2994 | if (err) | |
2995 | goto err_free_etm; | |
2996 | ||
cac31418 JC |
2997 | if (session->itrace_synth_opts->set) { |
2998 | etm->synth_opts = *session->itrace_synth_opts; | |
2999 | } else { | |
3000 | itrace_synth_opts__set_default(&etm->synth_opts, | |
3001 | session->itrace_synth_opts->default_no_sample); | |
3002 | etm->synth_opts.callchain = false; | |
3003 | } | |
3004 | ||
440a23b3 MP |
3005 | etm->session = session; |
3006 | etm->machine = &session->machines.host; | |
3007 | ||
cd8bfd8c TJ |
3008 | etm->num_cpu = num_cpu; |
3009 | etm->pmu_type = pmu_type; | |
3010 | etm->snapshot_mode = (hdr[CS_ETM_SNAPSHOT] != 0); | |
3011 | etm->metadata = metadata; | |
440a23b3 MP |
3012 | etm->auxtrace_type = auxtrace_info->type; |
3013 | etm->timeless_decoding = cs_etm__is_timeless_decoding(etm); | |
3014 | ||
3015 | etm->auxtrace.process_event = cs_etm__process_event; | |
3016 | etm->auxtrace.process_auxtrace_event = cs_etm__process_auxtrace_event; | |
3017 | etm->auxtrace.flush_events = cs_etm__flush_events; | |
3018 | etm->auxtrace.free_events = cs_etm__free_events; | |
3019 | etm->auxtrace.free = cs_etm__free; | |
a58ab57c | 3020 | etm->auxtrace.evsel_is_auxtrace = cs_etm__evsel_is_auxtrace; |
440a23b3 MP |
3021 | session->auxtrace = &etm->auxtrace; |
3022 | ||
46d53620 | 3023 | etm->unknown_thread = thread__new(999999999, 999999999); |
6285bd15 Y |
3024 | if (!etm->unknown_thread) { |
3025 | err = -ENOMEM; | |
46d53620 | 3026 | goto err_free_queues; |
6285bd15 | 3027 | } |
46d53620 LY |
3028 | |
3029 | /* | |
3030 | * Initialize list node so that at thread__zput() we can avoid | |
3031 | * segmentation fault at list_del_init(). | |
3032 | */ | |
3033 | INIT_LIST_HEAD(&etm->unknown_thread->node); | |
3034 | ||
3035 | err = thread__set_comm(etm->unknown_thread, "unknown", 0); | |
3036 | if (err) | |
3037 | goto err_delete_thread; | |
3038 | ||
79b6bb73 | 3039 | if (thread__init_maps(etm->unknown_thread, etm->machine)) { |
6285bd15 | 3040 | err = -ENOMEM; |
46d53620 | 3041 | goto err_delete_thread; |
6285bd15 | 3042 | } |
46d53620 | 3043 | |
cd8bfd8c TJ |
3044 | if (dump_trace) { |
3045 | cs_etm__print_auxtrace_info(auxtrace_info->priv, num_cpu); | |
cd8bfd8c | 3046 | } |
440a23b3 | 3047 | |
b12235b1 MP |
3048 | err = cs_etm__synth_events(etm, session); |
3049 | if (err) | |
46d53620 | 3050 | goto err_delete_thread; |
b12235b1 | 3051 | |
83d1fc92 | 3052 | err = cs_etm__queue_aux_records(session); |
440a23b3 | 3053 | if (err) |
46d53620 | 3054 | goto err_delete_thread; |
440a23b3 MP |
3055 | |
3056 | etm->data_queued = etm->queues.populated; | |
9182f04a JC |
3057 | /* |
3058 | * Print warning in pipe mode, see cs_etm__process_auxtrace_event() and | |
3059 | * cs_etm__queue_aux_fragment() for details relating to limitations. | |
3060 | */ | |
3061 | if (!etm->data_queued) | |
3062 | pr_warning("CS ETM warning: Coresight decode and TRBE support requires random file access.\n" | |
3063 | "Continuing with best effort decoding in piped mode.\n\n"); | |
440a23b3 MP |
3064 | |
3065 | return 0; | |
3066 | ||
46d53620 LY |
3067 | err_delete_thread: |
3068 | thread__zput(etm->unknown_thread); | |
440a23b3 MP |
3069 | err_free_queues: |
3070 | auxtrace_queues__free(&etm->queues); | |
3071 | session->auxtrace = NULL; | |
3072 | err_free_etm: | |
3073 | zfree(&etm); | |
cd8bfd8c TJ |
3074 | err_free_metadata: |
3075 | /* No need to check @metadata[j], free(NULL) is supported */ | |
3076 | for (j = 0; j < num_cpu; j++) | |
d8f9da24 | 3077 | zfree(&metadata[j]); |
cd8bfd8c TJ |
3078 | zfree(&metadata); |
3079 | err_free_traceid_list: | |
3080 | intlist__delete(traceid_list); | |
3081 | err_free_hdr: | |
3082 | zfree(&hdr); | |
42b2b570 ML |
3083 | /* |
3084 | * At this point, as a minimum we have valid header. Dump the rest of | |
3085 | * the info section - the print routines will error out on structural | |
3086 | * issues. | |
3087 | */ | |
3088 | if (dump_trace) | |
3089 | cs_etm__print_auxtrace_info(auxtrace_info->priv, num_cpu); | |
6285bd15 | 3090 | return err; |
440a23b3 | 3091 | } |