Merge tag 'v4.4.63' into linux-linaro-lsk-v4.4
[firefly-linux-kernel-4.4.55.git] / tools / perf / util / cs-etm.c
1 /*
2  * Copyright(C) 2016 Linaro Limited. All rights reserved.
3  * Author: Tor Jeremiassen <tor.jeremiassen@linaro.org>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <linux/err.h>
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/bitops.h>
22 #include <linux/log2.h>
23
24 #include "perf.h"
25 #include "thread_map.h"
26 #include "thread.h"
27 #include "thread-stack.h"
28 #include "callchain.h"
29 #include "auxtrace.h"
30 #include "evlist.h"
31 #include "machine.h"
32 #include "util.h"
33 #include "util/intlist.h"
34 #include "color.h"
35 #include "cs-etm.h"
36 #include "cs-etm-decoder/cs-etm-decoder.h"
37 #include "debug.h"
38
39 #include <stdlib.h>
40
41 #define KiB(x) ((x) * 1024)
42 #define MiB(x) ((x) * 1024 * 1024)
43 #define MAX_TIMESTAMP (~0ULL)
44
45 struct cs_etm_auxtrace {
46         struct auxtrace         auxtrace;
47         struct auxtrace_queues  queues;
48         struct auxtrace_heap    heap;
49         u64                    **metadata;
50         u32                     auxtrace_type;
51         struct perf_session    *session;
52         struct machine         *machine;
53         struct perf_evsel      *switch_evsel;
54         struct thread          *unknown_thread;
55         uint32_t                num_cpu;
56         bool                    timeless_decoding;
57         bool                    sampling_mode;
58         bool                    snapshot_mode;
59         bool                    data_queued;
60         bool                    sync_switch;
61         bool                    synth_needs_swap;
62         int                     have_sched_switch;
63
64         bool                    sample_instructions;
65         u64                     instructions_sample_type;
66         u64                     instructions_sample_period;
67         u64                     instructions_id;
68         struct itrace_synth_opts synth_opts;
69         unsigned                pmu_type;
70 };
71
72 struct cs_etm_queue {
73         struct cs_etm_auxtrace *etm;
74         unsigned                queue_nr;
75         struct auxtrace_buffer *buffer;
76         const struct           cs_etm_state *state;
77         struct ip_callchain    *chain;
78         union perf_event       *event_buf;
79         bool                    on_heap;
80         bool                    step_through_buffers;
81         bool                    use_buffer_pid_tid;
82         pid_t                   pid, tid;
83         int                     cpu;
84         struct thread          *thread;
85         u64                     time;
86         u64                     timestamp;
87         bool                    stop;
88         struct cs_etm_decoder  *decoder;
89         u64                     offset;
90         bool                    eot;
91         bool                    kernel_mapped;
92 };
93
94 static int cs_etm__get_trace(struct cs_etm_buffer *buff, struct cs_etm_queue *etmq);
95 static int cs_etm__update_queues(struct cs_etm_auxtrace *);
96 static int cs_etm__process_queues(struct cs_etm_auxtrace *, u64);
97 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *, pid_t, u64);
98 static uint32_t cs_etm__mem_access(struct cs_etm_queue *, uint64_t , size_t , uint8_t *);
99
100 static void cs_etm__packet_dump(const char *pkt_string)
101 {
102         const char *color = PERF_COLOR_BLUE;
103
104         color_fprintf(stdout,color, "  %s\n", pkt_string);
105         fflush(stdout);
106 }
107
108 static void cs_etm__dump_event(struct cs_etm_auxtrace *etm,
109                               struct auxtrace_buffer *buffer)
110 {
111         const char *color = PERF_COLOR_BLUE;
112         struct cs_etm_decoder_params d_params;
113         struct cs_etm_trace_params *t_params;
114         struct cs_etm_decoder *decoder;
115         size_t buffer_used = 0;
116         size_t i;
117
118         fprintf(stdout,"\n");
119         color_fprintf(stdout, color,
120                      ". ... CoreSight ETM Trace data: size %zu bytes\n",
121                      buffer->size);
122
123         t_params = zalloc(sizeof(struct cs_etm_trace_params) * etm->num_cpu);
124         for (i = 0; i < etm->num_cpu; ++i) {
125                 t_params[i].protocol = CS_ETM_PROTO_ETMV4i;
126                 t_params[i].reg_idr0 = etm->metadata[i][CS_ETMV4_TRCIDR0];
127                 t_params[i].reg_idr1 = etm->metadata[i][CS_ETMV4_TRCIDR1];
128                 t_params[i].reg_idr2 = etm->metadata[i][CS_ETMV4_TRCIDR2];
129                 t_params[i].reg_idr8 = etm->metadata[i][CS_ETMV4_TRCIDR8];
130                 t_params[i].reg_configr = etm->metadata[i][CS_ETMV4_TRCCONFIGR];
131                 t_params[i].reg_traceidr = etm->metadata[i][CS_ETMV4_TRCTRACEIDR];
132   //[CS_ETMV4_TRCAUTHSTATUS] = "   TRCAUTHSTATUS                  %"PRIx64"\n",
133         }
134         d_params.packet_printer = cs_etm__packet_dump;
135         d_params.operation = CS_ETM_OPERATION_PRINT;
136         d_params.formatted = true;
137         d_params.fsyncs = false;
138         d_params.hsyncs = false;
139         d_params.frame_aligned = true;
140
141         decoder = cs_etm_decoder__new(etm->num_cpu,&d_params, t_params);
142
143         zfree(&t_params);
144
145         if (decoder == NULL) {
146                 return; 
147         }
148         do {
149             size_t consumed;
150             cs_etm_decoder__process_data_block(decoder,buffer->offset,&(((uint8_t *)buffer->data)[buffer_used]),buffer->size - buffer_used, &consumed);
151             buffer_used += consumed;
152         } while(buffer_used < buffer->size);
153         cs_etm_decoder__free(decoder);
154 }
155                               
156 static int cs_etm__flush_events(struct perf_session *session, struct perf_tool *tool){
157         struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
158                                                    struct cs_etm_auxtrace,
159                                                    auxtrace);
160
161         int ret;
162
163         if (dump_trace)
164                 return 0;
165
166         if (!tool->ordered_events)
167                 return -EINVAL;
168
169         ret = cs_etm__update_queues(etm);
170
171         if (ret < 0) 
172                 return ret;
173
174         if (etm->timeless_decoding)
175                 return cs_etm__process_timeless_queues(etm,-1,MAX_TIMESTAMP - 1);
176
177         return cs_etm__process_queues(etm, MAX_TIMESTAMP);
178 }
179
180 static void  cs_etm__set_pid_tid_cpu(struct cs_etm_auxtrace *etm,
181                                     struct auxtrace_queue *queue)
182 {
183         struct cs_etm_queue *etmq = queue->priv;
184
185         if ((queue->tid == -1) || (etm->have_sched_switch)) {
186                 etmq->tid = machine__get_current_tid(etm->machine, etmq->cpu);
187                 thread__zput(etmq->thread);
188         }
189
190         if ((!etmq->thread) && (etmq->tid != -1)) {
191                 etmq->thread = machine__find_thread(etm->machine,-1,etmq->tid);
192         }
193
194         if (etmq->thread) {
195                 etmq->pid = etmq->thread->pid_;
196                 if (queue->cpu == -1) {
197                         etmq->cpu = etmq->thread->cpu;
198                 }
199         }
200 }
201
202 static void cs_etm__free_queue(void *priv)
203 {
204         struct cs_etm_queue *etmq = priv;
205
206         if (!etmq)
207                 return;
208
209         thread__zput(etmq->thread);
210         cs_etm_decoder__free(etmq->decoder);
211         zfree(&etmq->event_buf);
212         zfree(&etmq->chain);
213         free(etmq);
214 }
215
216 static void cs_etm__free_events(struct perf_session *session)
217 {
218         struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
219                                                    struct cs_etm_auxtrace,
220                                                    auxtrace);
221
222         struct auxtrace_queues *queues = &(aux->queues);
223
224         unsigned i;
225
226         for (i = 0; i < queues->nr_queues; ++i) {
227                 cs_etm__free_queue(queues->queue_array[i].priv);
228                 queues->queue_array[i].priv = 0;
229         }
230
231         auxtrace_queues__free(queues);
232
233 }
234
235 static void cs_etm__free(struct perf_session *session)
236 {
237
238         size_t i;
239         struct int_node *inode, *tmp;
240         struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
241                                                    struct cs_etm_auxtrace,
242                                                    auxtrace);
243         auxtrace_heap__free(&aux->heap);
244         cs_etm__free_events(session);
245         session->auxtrace = NULL;
246
247         /* First remove all traceID/CPU# nodes from the RB tree */
248         intlist__for_each_safe(inode, tmp, traceid_list)
249                 intlist__remove(traceid_list, inode);
250         /* Then the RB tree itself */
251         intlist__delete(traceid_list);
252
253         //thread__delete(aux->unknown_thread);
254         for (i = 0; i < aux->num_cpu; ++i) {
255                 zfree(&aux->metadata[i]);
256         }
257         zfree(&aux->metadata);
258         free(aux);
259 }
260
261 static void cs_etm__use_buffer_pid_tid(struct cs_etm_queue *etmq,
262                                       struct auxtrace_queue *queue,
263                                       struct auxtrace_buffer *buffer)
264 {
265         if ((queue->cpu == -1) && (buffer->cpu != -1)) {
266                 etmq->cpu = buffer->cpu;
267         }
268
269         etmq->pid = buffer->pid;
270         etmq->tid = buffer->tid;
271
272         thread__zput(etmq->thread);
273
274         if (etmq->tid != -1) {
275                 if (etmq->pid != -1) {
276                         etmq->thread = machine__findnew_thread(etmq->etm->machine,
277                                                                etmq->pid,
278                                                                etmq->tid);
279                 } else {
280                         etmq->thread = machine__findnew_thread(etmq->etm->machine,
281                                                                -1,
282                                                                etmq->tid);
283                 }
284         }
285 }
286
287
288 static int cs_etm__get_trace(struct cs_etm_buffer *buff, struct cs_etm_queue *etmq)
289 {
290         struct auxtrace_buffer *aux_buffer = etmq->buffer;
291         struct auxtrace_buffer *old_buffer = aux_buffer;
292         struct auxtrace_queue *queue;
293
294         if (etmq->stop) {
295                 buff->len = 0;
296                 return 0;
297         }
298
299         queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
300
301         aux_buffer = auxtrace_buffer__next(queue,aux_buffer);
302
303         if (!aux_buffer) {
304                 if (old_buffer) {
305                         auxtrace_buffer__drop_data(old_buffer);
306                 }
307                 buff->len = 0;
308                 return 0;
309         }
310
311         etmq->buffer = aux_buffer;
312
313         if (!aux_buffer->data) {
314                 int fd = perf_data_file__fd(etmq->etm->session->file);
315
316                 aux_buffer->data = auxtrace_buffer__get_data(aux_buffer, fd);
317                 if (!aux_buffer->data)
318                         return -ENOMEM;
319         }
320
321         if (old_buffer)
322                 auxtrace_buffer__drop_data(old_buffer);
323
324         if (aux_buffer->use_data) {
325                 buff->offset = aux_buffer->offset;
326                 buff->len = aux_buffer->use_size;
327                 buff->buf = aux_buffer->use_data;
328         } else {
329                 buff->offset = aux_buffer->offset;
330                 buff->len = aux_buffer->size;
331                 buff->buf = aux_buffer->data;
332         }
333         /*
334         buff->offset = 0;
335         buff->len = sizeof(cstrace);
336         buff->buf = cstrace;
337         */
338
339         buff->ref_timestamp = aux_buffer->reference;
340
341         if (etmq->use_buffer_pid_tid && 
342             ((etmq->pid != aux_buffer->pid) || 
343              (etmq->tid != aux_buffer->tid))) {
344                 cs_etm__use_buffer_pid_tid(etmq,queue,aux_buffer);
345         }
346
347         if (etmq->step_through_buffers)
348                 etmq->stop = true;
349
350         return buff->len;
351 }
352
353 static struct cs_etm_queue *cs_etm__alloc_queue(struct cs_etm_auxtrace *etm,
354                                                unsigned int queue_nr)
355 {
356         struct cs_etm_decoder_params d_params;
357         struct cs_etm_trace_params   *t_params;
358         struct cs_etm_queue *etmq;
359         size_t i;
360
361         etmq = zalloc(sizeof(struct cs_etm_queue));
362         if (!etmq)
363                 return NULL;
364
365         if (etm->synth_opts.callchain) {
366                 size_t sz = sizeof(struct ip_callchain);
367
368                 sz += etm->synth_opts.callchain_sz * sizeof(u64);
369                 etmq->chain = zalloc(sz);
370                 if (!etmq->chain)
371                         goto out_free;
372         } else {
373                 etmq->chain = NULL;
374         }
375
376         etmq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
377         if (!etmq->event_buf)
378                 goto out_free;
379
380         etmq->etm = etm;
381         etmq->queue_nr = queue_nr;
382         etmq->pid = -1;
383         etmq->tid = -1;
384         etmq->cpu = -1;
385         etmq->stop = false;
386         etmq->kernel_mapped = false;
387
388         t_params = zalloc(sizeof(struct cs_etm_trace_params)*etm->num_cpu);
389
390         for (i = 0; i < etm->num_cpu; ++i) {
391                 t_params[i].reg_idr0 = etm->metadata[i][CS_ETMV4_TRCIDR0];
392                 t_params[i].reg_idr1 = etm->metadata[i][CS_ETMV4_TRCIDR1];
393                 t_params[i].reg_idr2 = etm->metadata[i][CS_ETMV4_TRCIDR2];
394                 t_params[i].reg_idr8 = etm->metadata[i][CS_ETMV4_TRCIDR8];
395                 t_params[i].reg_configr = etm->metadata[i][CS_ETMV4_TRCCONFIGR];
396                 t_params[i].reg_traceidr = etm->metadata[i][CS_ETMV4_TRCTRACEIDR];
397                 t_params[i].protocol = CS_ETM_PROTO_ETMV4i;
398         }
399         d_params.packet_printer = cs_etm__packet_dump;
400         d_params.operation = CS_ETM_OPERATION_DECODE;    
401         d_params.formatted = true;
402         d_params.fsyncs = false;
403         d_params.hsyncs = false;
404         d_params.frame_aligned = true;
405         d_params.data = etmq;
406
407         etmq->decoder = cs_etm_decoder__new(etm->num_cpu,&d_params,t_params);
408
409
410         zfree(&t_params);
411
412         if (!etmq->decoder)
413                 goto out_free;
414
415         etmq->offset = 0;
416         etmq->eot = false;
417
418         return etmq;
419
420 out_free:
421         zfree(&etmq->event_buf);
422         zfree(&etmq->chain);
423         free(etmq);
424         return NULL;
425 }
426
427 static int cs_etm__setup_queue(struct cs_etm_auxtrace *etm, 
428                               struct auxtrace_queue *queue,
429                               unsigned int queue_nr)
430 {
431         struct cs_etm_queue *etmq = queue->priv;
432
433         if (list_empty(&(queue->head))) 
434                 return 0;
435
436         if (etmq == NULL) {
437                 etmq = cs_etm__alloc_queue(etm,queue_nr);
438
439                 if (etmq == NULL) {
440                         return -ENOMEM;
441                 }
442
443                 queue->priv = etmq;
444
445                 if (queue->cpu != -1) {
446                         etmq->cpu = queue->cpu;
447                 }
448
449                 etmq->tid = queue->tid;
450
451                 if (etm->sampling_mode) {
452                         if (etm->timeless_decoding)
453                                 etmq->step_through_buffers = true;
454                         if (etm->timeless_decoding || !etm->have_sched_switch)
455                                 etmq->use_buffer_pid_tid = true;
456                 }
457         }
458         
459         if (!etmq->on_heap && 
460             (!etm->sync_switch)) {
461                 const struct cs_etm_state *state;
462                 int ret = 0;
463
464                 if (etm->timeless_decoding)
465                         return ret;
466
467                 //cs_etm__log("queue %u getting timestamp\n",queue_nr);
468                 //cs_etm__log("queue %u decoding cpu %d pid %d tid %d\n",
469                            //queue_nr, etmq->cpu, etmq->pid, etmq->tid);
470                 (void) state;
471                 return ret;
472                 /*
473                 while (1) {
474                         state = cs_etm_decoder__decode(etmq->decoder);
475                         if (state->err) {
476                                 if (state->err == CS_ETM_ERR_NODATA) {
477                                         //cs_etm__log("queue %u has no timestamp\n",
478                                                    //queue_nr);
479                                         return 0;
480                                 }
481                                 continue;
482                         }
483                         if (state->timestamp)
484                                 break;
485                 }
486
487                 etmq->timestamp = state->timestamp;
488                 //cs_etm__log("queue %u timestamp 0x%"PRIx64 "\n",
489                            //queue_nr, etmq->timestamp);
490                 etmq->state = state;
491                 etmq->have_sample = true;
492                 //cs_etm__sample_flags(etmq);
493                 ret = auxtrace_heap__add(&etm->heap, queue_nr, etmq->timestamp);
494                 if (ret)
495                         return ret;
496                 etmq->on_heap = true;
497                 */
498         }
499         
500         return 0;
501 }
502
503
504 static int cs_etm__setup_queues(struct cs_etm_auxtrace *etm)
505 {
506         unsigned int i;
507         int ret;
508
509         for (i = 0; i < etm->queues.nr_queues; i++) {
510                 ret = cs_etm__setup_queue(etm, &(etm->queues.queue_array[i]),i);
511                 if (ret)
512                         return ret;
513         }
514         return 0;
515 }
516
517 #if 0
518 struct cs_etm_cache_entry {
519         struct auxtrace_cache_entry     entry;
520         uint64_t                        icount;
521         uint64_t                        bcount;
522 };
523
524 static size_t cs_etm__cache_divisor(void)
525 {
526         static size_t d = 64;
527
528         return d;
529 }
530
531 static size_t cs_etm__cache_size(struct dso *dso,
532                                 struct machine *machine)
533 {
534         off_t size;
535
536         size = dso__data_size(dso,machine);
537         size /= cs_etm__cache_divisor();
538
539         if (size < 1000) 
540                 return 10;
541
542         if (size > (1 << 21)) 
543                 return 21;
544
545         return 32 - __builtin_clz(size);
546 }
547
548 static struct auxtrace_cache *cs_etm__cache(struct dso *dso,
549                                            struct machine *machine)
550 {
551         struct auxtrace_cache *c;
552         size_t bits;
553
554         if (dso->auxtrace_cache)
555                 return dso->auxtrace_cache;
556
557         bits = cs_etm__cache_size(dso,machine);
558
559         c = auxtrace_cache__new(bits, sizeof(struct cs_etm_cache_entry), 200);
560
561         dso->auxtrace_cache = c;
562
563         return c;
564 }
565
566 static int cs_etm__cache_add(struct dso *dso, struct machine *machine,
567                             uint64_t offset, uint64_t icount, uint64_t bcount)
568 {
569         struct auxtrace_cache *c = cs_etm__cache(dso, machine);
570         struct cs_etm_cache_entry *e;
571         int err;
572
573         if (!c)
574                 return -ENOMEM;
575
576         e = auxtrace_cache__alloc_entry(c);
577         if (!e)
578                 return -ENOMEM;
579
580         e->icount = icount;
581         e->bcount = bcount;
582
583         err = auxtrace_cache__add(c, offset, &e->entry);
584
585         if (err)
586                 auxtrace_cache__free_entry(c, e);
587
588         return err;
589 }
590
591 static struct cs_etm_cache_entry *cs_etm__cache_lookup(struct dso *dso,
592                                                       struct machine *machine,
593                                                       uint64_t offset)
594 {
595         struct auxtrace_cache *c = cs_etm__cache(dso, machine);
596
597         if (!c)
598                 return NULL;
599
600         return auxtrace_cache__lookup(dso->auxtrace_cache, offset);
601 }
602 #endif
603
604 static int cs_etm__synth_instruction_sample(struct cs_etm_queue *etmq,
605                                            struct cs_etm_packet *packet)
606 {
607         int ret = 0;
608         struct cs_etm_auxtrace *etm = etmq->etm;
609         union perf_event *event = etmq->event_buf;
610         struct perf_sample sample = {.ip = 0,};
611         uint64_t start_addr = packet->start_addr;
612         uint64_t end_addr = packet->end_addr;
613
614         event->sample.header.type = PERF_RECORD_SAMPLE;
615         event->sample.header.misc = PERF_RECORD_MISC_USER;
616         event->sample.header.size = sizeof(struct perf_event_header);
617
618
619         sample.ip = start_addr;
620         sample.pid = etmq->pid;
621         sample.tid = etmq->tid;
622         sample.addr = end_addr;
623         sample.id = etmq->etm->instructions_id;
624         sample.stream_id = etmq->etm->instructions_id;
625         sample.period = (end_addr - start_addr) >> 2; 
626         sample.cpu = packet->cpu;
627         sample.flags = 0; // etmq->flags;
628         sample.insn_len = 1; // etmq->insn_len;
629
630         //etmq->last_insn_cnt = etmq->state->tot_insn_cnt;
631
632 #if 0
633         {
634                 struct   addr_location al;
635                 uint64_t offset;
636                 struct   thread *thread;
637                 struct   machine *machine = etmq->etm->machine;
638                 uint8_t  cpumode;
639                 struct   cs_etm_cache_entry *e;
640                 uint8_t  buf[256];
641                 size_t   bufsz;
642
643                 thread = etmq->thread;
644
645                 if (!thread) {
646                         thread = etmq->etm->unknown_thread;
647                 }
648
649                 if (start_addr > 0xffffffc000000000UL) {
650                         cpumode = PERF_RECORD_MISC_KERNEL;
651                 } else {
652                         cpumode = PERF_RECORD_MISC_USER;
653                 }
654
655                 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, start_addr,&al);
656                 if (!al.map || !al.map->dso) {
657                         goto endTest;
658                 }
659                 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
660                     dso__data_status_seen(al.map->dso,DSO_DATA_STATUS_SEEN_ITRACE)) {
661                         goto endTest;
662                 }
663
664                 offset = al.map->map_ip(al.map,start_addr);
665
666
667                 e = cs_etm__cache_lookup(al.map->dso, machine, offset);
668
669                 if (e) {
670                   (void) e;
671                 } else {
672                         int len;
673                         map__load(al.map, machine->symbol_filter);
674
675                         bufsz = sizeof(buf);
676                         len = dso__data_read_offset(al.map->dso, machine,
677                                                     offset, buf, bufsz);
678
679                         if (len <= 0) {
680                                 goto endTest;
681                         }
682
683                         cs_etm__cache_add(al.map->dso, machine, offset, (end_addr - start_addr) >> 2, end_addr - start_addr);
684
685                 }
686 endTest:
687                 (void) offset;
688         }
689 #endif
690
691         ret = perf_session__deliver_synth_event(etm->session,event, &sample);
692
693         if (ret) {
694                 pr_err("CS ETM Trace: failed to deliver instruction event, error %d\n", ret);
695
696         }
697         return ret;
698 }
699
700 struct cs_etm_synth {
701         struct perf_tool dummy_tool;
702         struct perf_session *session;
703 };
704
705
706 static int cs_etm__event_synth(struct perf_tool *tool,
707                               union perf_event *event,
708                               struct perf_sample *sample,
709                               struct machine *machine)
710 {
711         struct cs_etm_synth *cs_etm_synth =
712                       container_of(tool, struct cs_etm_synth, dummy_tool);
713
714         (void) sample;
715         (void) machine;
716
717         return perf_session__deliver_synth_event(cs_etm_synth->session, event, NULL);
718
719 }
720
721
722 static int cs_etm__synth_event(struct perf_session *session,
723                               struct perf_event_attr *attr, u64 id)
724 {
725         struct cs_etm_synth cs_etm_synth;
726
727         memset(&cs_etm_synth, 0, sizeof(struct cs_etm_synth));
728         cs_etm_synth.session = session;
729
730         return perf_event__synthesize_attr(&cs_etm_synth.dummy_tool, attr, 1,
731                                            &id, cs_etm__event_synth);
732 }
733
734 static int cs_etm__synth_events(struct cs_etm_auxtrace *etm, 
735                                struct perf_session *session)
736 {
737         struct perf_evlist *evlist = session->evlist;
738         struct perf_evsel *evsel;
739         struct perf_event_attr attr;
740         bool found = false;
741         u64 id;
742         int err;
743
744         evlist__for_each(evlist, evsel) {
745
746                 if (evsel->attr.type == etm->pmu_type) {
747                         found = true;
748                         break;
749                 }
750         }
751
752         if (!found) {
753                 pr_debug("There are no selected events with Core Sight Trace data\n");
754                 return 0;
755         }
756
757         memset(&attr, 0, sizeof(struct perf_event_attr));
758         attr.size = sizeof(struct perf_event_attr);
759         attr.type = PERF_TYPE_HARDWARE;
760         attr.sample_type = evsel->attr.sample_type & PERF_SAMPLE_MASK;
761         attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
762                             PERF_SAMPLE_PERIOD;
763         if (etm->timeless_decoding) 
764                 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
765         else
766                 attr.sample_type |= PERF_SAMPLE_TIME;
767
768         attr.exclude_user = evsel->attr.exclude_user;
769         attr.exclude_kernel = evsel->attr.exclude_kernel;
770         attr.exclude_hv = evsel->attr.exclude_hv;
771         attr.exclude_host = evsel->attr.exclude_host;
772         attr.exclude_guest = evsel->attr.exclude_guest;
773         attr.sample_id_all = evsel->attr.sample_id_all;
774         attr.read_format = evsel->attr.read_format;
775
776         id = evsel->id[0] + 1000000000;
777
778         if (!id)
779                 id = 1;
780
781         if (etm->synth_opts.instructions) {
782                 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
783                 attr.sample_period = etm->synth_opts.period;
784                 etm->instructions_sample_period = attr.sample_period;
785                 err = cs_etm__synth_event(session, &attr, id);
786
787                 if (err) {
788                         pr_err("%s: failed to synthesize 'instructions' event type\n",
789                                __func__);
790                         return err;
791                 }
792                 etm->sample_instructions = true;
793                 etm->instructions_sample_type = attr.sample_type;
794                 etm->instructions_id = id;
795                 id += 1;
796         }
797
798         etm->synth_needs_swap = evsel->needs_swap;
799         return 0;
800 }
801
802 static int cs_etm__sample(struct cs_etm_queue *etmq)
803 {
804         //const struct cs_etm_state *state = etmq->state;
805         struct cs_etm_packet packet;
806         //struct cs_etm_auxtrace *etm = etmq->etm;
807         int err;
808
809         err = cs_etm_decoder__get_packet(etmq->decoder,&packet);
810         // if there is no sample, it returns err = -1, no real error
811
812         if (!err && packet.sample_type & CS_ETM_RANGE) {
813                 err = cs_etm__synth_instruction_sample(etmq,&packet);
814                 if (err)
815                         return err;
816         }
817         return 0;
818 }
819
820 static int cs_etm__run_decoder(struct cs_etm_queue *etmq, u64 *timestamp)
821 {
822         struct cs_etm_buffer buffer;
823         size_t buffer_used;
824         int err = 0;
825
826         /* Go through each buffer in the queue and decode them one by one */
827 more:
828         buffer_used = 0;
829         memset(&buffer, 0, sizeof(buffer));
830         err = cs_etm__get_trace(&buffer,etmq);
831         if (err <= 0)
832                 return err;
833
834         do {
835             size_t processed = 0;
836             etmq->state = cs_etm_decoder__process_data_block(etmq->decoder,
837                                                etmq->offset,
838                                                &buffer.buf[buffer_used],
839                                                buffer.len-buffer_used,
840                                                &processed);
841             err = etmq->state->err;
842             etmq->offset += processed;
843             buffer_used += processed;
844             if (!err)
845                 cs_etm__sample(etmq);
846         } while (!etmq->eot && (buffer.len > buffer_used));
847 goto more;
848
849         (void) timestamp;
850
851         return err;
852 }
853
854 static int cs_etm__update_queues(struct cs_etm_auxtrace *etm)
855 {
856   if (etm->queues.new_data) {
857         etm->queues.new_data = false;
858         return cs_etm__setup_queues(etm);
859   }
860   return 0;
861 }
862
863 static int cs_etm__process_queues(struct cs_etm_auxtrace *etm, u64 timestamp)
864 {
865         unsigned int queue_nr;
866         u64 ts;
867         int ret;
868
869         while (1) {
870                 struct auxtrace_queue *queue;
871                 struct cs_etm_queue *etmq;
872         
873                 if (!etm->heap.heap_cnt)
874                         return 0;
875         
876                 if (etm->heap.heap_array[0].ordinal >= timestamp)
877                         return 0;
878         
879                 queue_nr = etm->heap.heap_array[0].queue_nr;
880                 queue = &etm->queues.queue_array[queue_nr];
881                 etmq = queue->priv;
882         
883                 //cs_etm__log("queue %u processing 0x%" PRIx64 " to 0x%" PRIx64 "\n",
884                            //queue_nr, etm->heap.heap_array[0].ordinal,
885                            //timestamp);
886
887                 auxtrace_heap__pop(&etm->heap);
888
889                 if (etm->heap.heap_cnt) {
890                         ts = etm->heap.heap_array[0].ordinal + 1;
891                         if (ts > timestamp)
892                                 ts = timestamp;
893                 } else {
894                         ts = timestamp;
895                 }
896
897                 cs_etm__set_pid_tid_cpu(etm, queue);
898
899                 ret = cs_etm__run_decoder(etmq, &ts);
900
901                 if (ret < 0) {
902                         auxtrace_heap__add(&etm->heap, queue_nr, ts);
903                         return ret;
904                 }
905
906                 if (!ret) {
907                         ret = auxtrace_heap__add(&etm->heap, queue_nr, ts);
908                         if (ret < 0)
909                                 return ret;
910                 } else {
911                         etmq->on_heap = false;
912                 }
913         }
914         return 0;
915 }
916
917 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
918                                           pid_t tid,
919                                           u64 time_)
920 {
921         struct auxtrace_queues *queues = &etm->queues;
922         unsigned int i;
923         u64 ts = 0;
924         
925         for (i = 0; i < queues->nr_queues; ++i) {
926                 struct auxtrace_queue *queue = &(etm->queues.queue_array[i]);
927                 struct cs_etm_queue *etmq = queue->priv;
928
929                 if (etmq && ((tid == -1) || (etmq->tid == tid))) {
930                         etmq->time = time_;
931                         cs_etm__set_pid_tid_cpu(etm, queue);
932                         cs_etm__run_decoder(etmq,&ts);
933
934                 }
935         }
936         return 0;
937 }
938
939 static struct cs_etm_queue *cs_etm__cpu_to_etmq(struct cs_etm_auxtrace *etm, 
940                                                int cpu)
941 {
942         unsigned q,j;
943
944         if (etm->queues.nr_queues == 0)
945                 return NULL;
946
947         if (cpu < 0)
948                 q = 0;
949         else if ((unsigned) cpu >= etm->queues.nr_queues)
950                 q = etm->queues.nr_queues - 1;
951         else 
952                 q = cpu;
953
954         if (etm->queues.queue_array[q].cpu == cpu)
955                 return etm->queues.queue_array[q].priv;
956
957         for (j = 0; q > 0; j++) {
958                 if (etm->queues.queue_array[--q].cpu == cpu)
959                         return etm->queues.queue_array[q].priv;
960         }
961
962         for (; j < etm->queues.nr_queues; j++) {
963                 if (etm->queues.queue_array[j].cpu == cpu)
964                         return etm->queues.queue_array[j].priv;
965
966         }
967
968         return NULL;
969 }
970
971 static uint32_t cs_etm__mem_access(struct cs_etm_queue *etmq, uint64_t address, size_t size, uint8_t *buffer)
972 {
973         struct   addr_location al;
974         uint64_t offset;
975         struct   thread *thread;
976         struct   machine *machine;
977         uint8_t  cpumode;
978         int len;
979
980         if (etmq == NULL)
981                 return -1;
982
983         machine = etmq->etm->machine;
984         thread = etmq->thread;
985         if (address > 0xffffffc000000000UL) {
986                 cpumode = PERF_RECORD_MISC_KERNEL;
987         } else {
988                 cpumode = PERF_RECORD_MISC_USER;
989         }
990
991         thread__find_addr_map(thread, cpumode, MAP__FUNCTION, address,&al);
992
993         if (!al.map || !al.map->dso) {
994                 return 0;
995         }
996
997         if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
998             dso__data_status_seen(al.map->dso,DSO_DATA_STATUS_SEEN_ITRACE)) {
999                 return 0;
1000         }
1001
1002         offset = al.map->map_ip(al.map,address);
1003
1004         map__load(al.map, machine->symbol_filter);
1005
1006         len = dso__data_read_offset(al.map->dso, machine,
1007                                     offset, buffer, size);
1008
1009         if (len <= 0) {
1010                 return 0;
1011         }
1012
1013         return len;
1014 }
1015
1016 static bool check_need_swap(int file_endian)
1017 {
1018         const int data = 1;
1019         u8 *check = (u8 *)&data;
1020         int host_endian;
1021
1022         if (check[0] == 1)
1023                 host_endian = ELFDATA2LSB;
1024         else
1025                 host_endian = ELFDATA2MSB;
1026
1027         return host_endian != file_endian;
1028 }
1029
1030 static int cs_etm__read_elf_info(const char *fname, uint64_t *foffset, uint64_t *fstart, uint64_t *fsize)
1031 {
1032         FILE *fp;
1033         u8 e_ident[EI_NIDENT];
1034         int ret = -1;
1035         bool need_swap = false;
1036         size_t buf_size;
1037         void *buf;
1038         int i;
1039
1040         fp = fopen(fname, "r");
1041         if (fp == NULL)
1042                 return -1;
1043
1044         if (fread(e_ident, sizeof(e_ident), 1, fp) != 1)
1045                 goto out;
1046
1047         if (memcmp(e_ident, ELFMAG, SELFMAG) ||
1048             e_ident[EI_VERSION] != EV_CURRENT)
1049                 goto out;
1050
1051         need_swap = check_need_swap(e_ident[EI_DATA]);
1052
1053         /* for simplicity */
1054         fseek(fp, 0, SEEK_SET);
1055
1056         if (e_ident[EI_CLASS] == ELFCLASS32) {
1057                 Elf32_Ehdr ehdr;
1058                 Elf32_Phdr *phdr;
1059
1060                 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
1061                         goto out;
1062
1063                 if (need_swap) {
1064                         ehdr.e_phoff = bswap_32(ehdr.e_phoff);
1065                         ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
1066                         ehdr.e_phnum = bswap_16(ehdr.e_phnum);
1067                 }
1068
1069                 buf_size = ehdr.e_phentsize * ehdr.e_phnum;
1070                 buf = malloc(buf_size);
1071                 if (buf == NULL)
1072                         goto out;
1073
1074                 fseek(fp, ehdr.e_phoff, SEEK_SET);
1075                 if (fread(buf, buf_size, 1, fp) != 1)
1076                         goto out_free;
1077
1078                 for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
1079
1080                         if (need_swap) {
1081                                 phdr->p_type = bswap_32(phdr->p_type);
1082                                 phdr->p_offset = bswap_32(phdr->p_offset);
1083                                 phdr->p_filesz = bswap_32(phdr->p_filesz);
1084                         }
1085
1086                         if (phdr->p_type != PT_LOAD)
1087                                 continue;
1088
1089                         *foffset = phdr->p_offset;
1090                         *fstart = phdr->p_vaddr;
1091                         *fsize = phdr->p_filesz;
1092                         ret = 0;
1093                         break;
1094                 }
1095         } else {
1096                 Elf64_Ehdr ehdr;
1097                 Elf64_Phdr *phdr;
1098
1099                 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
1100                         goto out;
1101
1102                 if (need_swap) {
1103                         ehdr.e_phoff = bswap_64(ehdr.e_phoff);
1104                         ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
1105                         ehdr.e_phnum = bswap_16(ehdr.e_phnum);
1106                 }
1107
1108                 buf_size = ehdr.e_phentsize * ehdr.e_phnum;
1109                 buf = malloc(buf_size);
1110                 if (buf == NULL)
1111                         goto out;
1112
1113                 fseek(fp, ehdr.e_phoff, SEEK_SET);
1114                 if (fread(buf, buf_size, 1, fp) != 1)
1115                         goto out_free;
1116
1117                 for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
1118
1119                         if (need_swap) {
1120                                 phdr->p_type = bswap_32(phdr->p_type);
1121                                 phdr->p_offset = bswap_64(phdr->p_offset);
1122                                 phdr->p_filesz = bswap_64(phdr->p_filesz);
1123                         }
1124
1125                         if (phdr->p_type != PT_LOAD)
1126                                 continue;
1127
1128                         *foffset = phdr->p_offset;
1129                         *fstart = phdr->p_vaddr;
1130                         *fsize = phdr->p_filesz;
1131                         ret = 0;
1132                         break;
1133                 }
1134         }
1135 out_free:
1136         free(buf);
1137 out:
1138         fclose(fp);
1139         return ret;
1140 }
1141
1142 static int cs_etm__process_event(struct perf_session *session,
1143                                 union perf_event *event,
1144                                 struct perf_sample *sample,
1145                                 struct perf_tool *tool)
1146 {
1147         struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
1148                                                    struct cs_etm_auxtrace,
1149                                                    auxtrace);
1150
1151         u64 timestamp;
1152         int err = 0;
1153
1154         if (dump_trace) 
1155                 return 0;
1156
1157         if (!tool->ordered_events) {
1158                 pr_err("CoreSight ETM Trace requires ordered events\n");
1159                 return -EINVAL;
1160         }
1161
1162         if (sample->time && (sample->time != (u64)-1))
1163                 timestamp = sample->time;
1164         else
1165                 timestamp = 0;
1166
1167         if (timestamp || etm->timeless_decoding) {
1168                 err = cs_etm__update_queues(etm);
1169                 if (err)
1170                         return err;
1171
1172         }
1173
1174         if (event->header.type == PERF_RECORD_MMAP2) {
1175                 struct dso *dso;
1176                 int cpu;
1177                 struct cs_etm_queue *etmq;
1178
1179                 cpu = sample->cpu;
1180
1181                 etmq = cs_etm__cpu_to_etmq(etm,cpu);
1182
1183                 if (!etmq) {
1184                         return -1;
1185                 }
1186
1187                 dso = dsos__find(&(etm->machine->dsos),event->mmap2.filename,false);
1188                 if (NULL != dso) {
1189                         err = cs_etm_decoder__add_mem_access_cb(
1190                             etmq->decoder,
1191                             event->mmap2.start, 
1192                             event->mmap2.len, 
1193                             cs_etm__mem_access);
1194                 }
1195
1196                 if ((symbol_conf.vmlinux_name != NULL) && (!etmq->kernel_mapped)) {
1197                         uint64_t foffset;
1198                         uint64_t fstart;
1199                         uint64_t fsize;
1200
1201                         err = cs_etm__read_elf_info(symbol_conf.vmlinux_name,
1202                                                       &foffset,&fstart,&fsize);
1203
1204                         if (!err) {
1205                                 cs_etm_decoder__add_bin_file(
1206                                         etmq->decoder,
1207                                         foffset,
1208                                         fstart,
1209                                         fsize & ~0x1ULL,
1210                                         symbol_conf.vmlinux_name);
1211
1212                                 etmq->kernel_mapped = true;
1213                         }
1214                 }
1215
1216         }
1217
1218         if (etm->timeless_decoding) {
1219                 if (event->header.type == PERF_RECORD_EXIT) {
1220                         err = cs_etm__process_timeless_queues(etm,
1221                                                              event->fork.tid,
1222                                                              sample->time);
1223                 }
1224         } else if (timestamp) {
1225                 err = cs_etm__process_queues(etm, timestamp);
1226         }
1227
1228         //cs_etm__log("event %s (%u): cpu %d time%"PRIu64" tsc %#"PRIx64"\n",
1229                    //perf_event__name(event->header.type), event->header.type,
1230                    //sample->cpu, sample->time, timestamp);
1231         return err;
1232 }
1233
1234 static int cs_etm__process_auxtrace_event(struct perf_session *session,
1235                                   union perf_event *event,
1236                                   struct perf_tool *tool)
1237 {
1238         struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
1239                                                    struct cs_etm_auxtrace,
1240                                                    auxtrace);
1241
1242         (void) tool;
1243
1244         if (!etm->data_queued) {
1245                 struct auxtrace_buffer *buffer;
1246                 off_t  data_offset;
1247                 int fd = perf_data_file__fd(session->file);
1248                 bool is_pipe = perf_data_file__is_pipe(session->file);
1249                 int err;
1250
1251                 if (is_pipe) {
1252                         data_offset = 0;
1253                 } else {
1254                         data_offset = lseek(fd, 0, SEEK_CUR);
1255                         if (data_offset == -1) {
1256                                 return -errno;
1257                         }
1258                 }
1259
1260                 err = auxtrace_queues__add_event(&etm->queues,
1261                                                  session,
1262                                                  event,
1263                                                  data_offset,
1264                                                  &buffer);
1265                 if (err)
1266                         return err;
1267
1268                 if (dump_trace)
1269                 {
1270                         if (auxtrace_buffer__get_data(buffer,fd)) {
1271                                 cs_etm__dump_event(etm,buffer);
1272                                 auxtrace_buffer__put_data(buffer);
1273                         }
1274                 }
1275         } 
1276
1277         return 0;
1278
1279 }
1280
1281 static const char * const cs_etm_global_header_fmts[] = {
1282   [CS_HEADER_VERSION_0]    = "   Header version                 %"PRIx64"\n",
1283   [CS_PMU_TYPE_CPUS]       = "   PMU type/num cpus              %"PRIx64"\n",
1284   [CS_ETM_SNAPSHOT]        = "   Snapshot                       %"PRIx64"\n",
1285 };
1286
1287 static const char * const cs_etm_priv_fmts[] = {
1288   [CS_ETM_MAGIC]           = "   Magic number                   %"PRIx64"\n",
1289   [CS_ETM_CPU]             = "   CPU                            %"PRIx64"\n",
1290   [CS_ETM_ETMCR]           = "   ETMCR                          %"PRIx64"\n",
1291   [CS_ETM_ETMTRACEIDR]     = "   ETMTRACEIDR                    %"PRIx64"\n",
1292   [CS_ETM_ETMCCER]         = "   ETMCCER                        %"PRIx64"\n",
1293   [CS_ETM_ETMIDR]          = "   ETMIDR                         %"PRIx64"\n",
1294 };
1295
1296 static const char * const cs_etmv4_priv_fmts[] = {
1297   [CS_ETM_MAGIC]           = "   Magic number                   %"PRIx64"\n",
1298   [CS_ETM_CPU]             = "   CPU                            %"PRIx64"\n",
1299   [CS_ETMV4_TRCCONFIGR]    = "   TRCCONFIGR                     %"PRIx64"\n",
1300   [CS_ETMV4_TRCTRACEIDR]   = "   TRCTRACEIDR                    %"PRIx64"\n",
1301   [CS_ETMV4_TRCIDR0]       = "   TRCIDR0                        %"PRIx64"\n",
1302   [CS_ETMV4_TRCIDR1]       = "   TRCIDR1                        %"PRIx64"\n",
1303   [CS_ETMV4_TRCIDR2]       = "   TRCIDR2                        %"PRIx64"\n",
1304   [CS_ETMV4_TRCIDR8]       = "   TRCIDR8                        %"PRIx64"\n",
1305   [CS_ETMV4_TRCAUTHSTATUS] = "   TRCAUTHSTATUS                  %"PRIx64"\n",
1306 };
1307
1308 static void cs_etm__print_auxtrace_info(u64 *val, size_t num)
1309 {
1310         unsigned i,j,cpu;
1311
1312         for (i = 0, cpu = 0; cpu < num; ++cpu) {
1313
1314                 if (val[i] == __perf_cs_etmv3_magic) {
1315                         for (j = 0; j < CS_ETM_PRIV_MAX; ++j, ++i) {
1316                                 fprintf(stdout,cs_etm_priv_fmts[j],val[i]);
1317                         }
1318                 } else if (val[i] == __perf_cs_etmv4_magic) {
1319                         for (j = 0; j < CS_ETMV4_PRIV_MAX; ++j, ++i) {
1320                                 fprintf(stdout,cs_etmv4_priv_fmts[j],val[i]);
1321                         }
1322                 } else {
1323                         // failure.. return
1324                         return;
1325                 }
1326         }
1327 }
1328
1329 int cs_etm__process_auxtrace_info(union perf_event *event,
1330                                  struct perf_session *session)
1331 {
1332         struct auxtrace_info_event *auxtrace_info = &(event->auxtrace_info);
1333         size_t event_header_size = sizeof(struct perf_event_header);
1334         size_t info_header_size = 8;
1335         size_t total_size = auxtrace_info->header.size;
1336         size_t priv_size = 0;
1337         size_t num_cpu;
1338         struct cs_etm_auxtrace *etm = 0;
1339         int err = 0, idx = -1;
1340         u64 *ptr;
1341         u64 *hdr = NULL;
1342         u64 **metadata = NULL;
1343         size_t i,j,k;
1344         unsigned pmu_type;
1345         struct int_node *inode;
1346
1347         /*
1348          * sizeof(auxtrace_info_event::type) +
1349          * sizeof(auxtrace_info_event::reserved) == 8
1350          */
1351         info_header_size = 8;
1352
1353         if (total_size < (event_header_size + info_header_size))
1354                 return -EINVAL;
1355
1356         priv_size = total_size - event_header_size - info_header_size;
1357
1358         // First the global part
1359
1360         ptr = (u64 *) auxtrace_info->priv;
1361         if (ptr[0] == 0) {
1362                 hdr = zalloc(sizeof(u64 *) * CS_HEADER_VERSION_0_MAX);
1363                 if (hdr == NULL) {
1364                         return -EINVAL;
1365                 }
1366                 for (i = 0; i < CS_HEADER_VERSION_0_MAX; ++i) {
1367                         hdr[i] = ptr[i];
1368                 }
1369                 num_cpu = hdr[CS_PMU_TYPE_CPUS] & 0xffffffff;
1370                 pmu_type = (unsigned) ((hdr[CS_PMU_TYPE_CPUS] >> 32) & 0xffffffff);
1371         } else {
1372                 return -EINVAL;
1373         }
1374
1375         /*
1376          * Create an RB tree for traceID-CPU# tuple.  Since the conversion has
1377          * to be made for each packet that gets decoded optimizing access in
1378          * anything other than a sequential array is worth doing.
1379          */
1380         traceid_list = intlist__new(NULL);
1381         if (!traceid_list)
1382                 return -ENOMEM;
1383
1384         metadata = zalloc(sizeof(u64 *) * num_cpu);
1385         if (!metadata) {
1386                 err = -ENOMEM;
1387                 goto err_free_traceid_list;
1388         }
1389
1390         if (metadata == NULL) {
1391                 return -EINVAL;
1392         }
1393
1394         for (j = 0; j < num_cpu; ++j) {
1395                 if (ptr[i] == __perf_cs_etmv3_magic) {
1396                         metadata[j] = zalloc(sizeof(u64)*CS_ETM_PRIV_MAX);
1397                         if (metadata == NULL)
1398                                 return -EINVAL;
1399                         for (k = 0; k < CS_ETM_PRIV_MAX; k++) {
1400                                 metadata[j][k] = ptr[i+k];
1401                         }
1402
1403                         /* The traceID is our handle */
1404                         idx = metadata[j][CS_ETM_ETMIDR];
1405                         i += CS_ETM_PRIV_MAX;
1406                 } else if (ptr[i] == __perf_cs_etmv4_magic) {
1407                         metadata[j] = zalloc(sizeof(u64)*CS_ETMV4_PRIV_MAX);
1408                         if (metadata == NULL)
1409                                 return -EINVAL;
1410                         for (k = 0; k < CS_ETMV4_PRIV_MAX; k++) {
1411                                 metadata[j][k] = ptr[i+k];
1412                         }
1413
1414                         /* The traceID is our handle */
1415                         idx = metadata[j][CS_ETMV4_TRCTRACEIDR];
1416                         i += CS_ETMV4_PRIV_MAX;
1417                 }
1418
1419                 /* Get an RB node for this CPU */
1420                 inode = intlist__findnew(traceid_list, idx);
1421
1422                 /* Something went wrong, no need to continue */
1423                 if (!inode) {
1424                         err = PTR_ERR(inode);
1425                         goto err_free_metadata;
1426                 }
1427
1428                 /*
1429                  * The node for that CPU should not have been taken already.
1430                  * Backout if that's the case.
1431                  */
1432                 if (inode->priv) {
1433                         err = -EINVAL;
1434                         goto err_free_metadata;
1435                 }
1436
1437                 /* All good, associate the traceID with the CPU# */
1438                 inode->priv = &metadata[j][CS_ETM_CPU];
1439
1440         }
1441
1442         if (i*8 != priv_size)
1443                 return -EINVAL;
1444
1445         if (dump_trace)
1446                 cs_etm__print_auxtrace_info(auxtrace_info->priv,num_cpu);
1447
1448         etm = zalloc(sizeof(struct cs_etm_auxtrace));
1449
1450         etm->num_cpu = num_cpu;
1451         etm->pmu_type = pmu_type;
1452         etm->snapshot_mode = (hdr[CS_ETM_SNAPSHOT] != 0);
1453
1454         if (!etm)
1455                 return -ENOMEM;
1456
1457
1458         err = auxtrace_queues__init(&etm->queues);
1459         if (err)
1460                 goto err_free;
1461
1462         etm->unknown_thread = thread__new(999999999,999999999);
1463         if (etm->unknown_thread == NULL) {
1464                 err = -ENOMEM;
1465                 goto err_free_queues;
1466         }
1467         err = thread__set_comm(etm->unknown_thread, "unknown", 0);
1468         if (err) {
1469                 goto err_delete_thread;
1470         }
1471
1472         if (thread__init_map_groups(etm->unknown_thread,
1473                                     etm->machine)) {
1474                 err = -ENOMEM;
1475                 goto err_delete_thread;
1476         }
1477
1478         etm->timeless_decoding = true;
1479         etm->sampling_mode = false;
1480         etm->metadata = metadata;
1481         etm->session = session;
1482         etm->machine = &session->machines.host;
1483         etm->auxtrace_type = auxtrace_info->type;
1484
1485         etm->auxtrace.process_event = cs_etm__process_event;
1486         etm->auxtrace.process_auxtrace_event = cs_etm__process_auxtrace_event;
1487         etm->auxtrace.flush_events = cs_etm__flush_events;
1488         etm->auxtrace.free_events  = cs_etm__free_events;
1489         etm->auxtrace.free         = cs_etm__free;
1490         session->auxtrace = &(etm->auxtrace);
1491
1492         if (dump_trace)
1493                 return 0;
1494
1495         if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
1496                 etm->synth_opts = *session->itrace_synth_opts;
1497         } else {
1498                 itrace_synth_opts__set_default(&etm->synth_opts);
1499         }
1500         etm->synth_opts.branches = false;
1501         etm->synth_opts.callchain = false;
1502         etm->synth_opts.calls = false;
1503         etm->synth_opts.returns = false;
1504
1505         err = cs_etm__synth_events(etm, session);
1506         if (err)
1507                 goto err_delete_thread;
1508
1509         err = auxtrace_queues__process_index(&etm->queues, session);
1510         if (err)
1511                 goto err_delete_thread;
1512
1513         etm->data_queued = etm->queues.populated;
1514
1515         return 0;
1516
1517 err_delete_thread:
1518         thread__delete(etm->unknown_thread);
1519 err_free_queues:
1520         auxtrace_queues__free(&etm->queues);
1521         session->auxtrace = NULL;
1522 err_free:
1523         free(etm);
1524 err_free_metadata:
1525         /* No need to check @metadata[j], free(NULL) is supported */
1526         for (j = 0; j < num_cpu; ++j)
1527                 free(metadata[j]);
1528         free(metadata);
1529 err_free_traceid_list:
1530         intlist__delete(traceid_list);
1531
1532         return err;
1533 }