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