OSDN Git Service

Revert "usb: dwc3: turn off VBUS when leaving host mode"
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / tools / perf / util / auxtrace.c
1 /*
2  * auxtrace.c: AUX area trace support
3  * Copyright (c) 2013-2015, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope 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  */
15
16 #include <sys/types.h>
17 #include <sys/mman.h>
18 #include <stdbool.h>
19
20 #include <linux/kernel.h>
21 #include <linux/perf_event.h>
22 #include <linux/types.h>
23 #include <linux/bitops.h>
24 #include <linux/log2.h>
25 #include <linux/string.h>
26
27 #include <sys/param.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <errno.h>
33 #include <linux/list.h>
34
35 #include "../perf.h"
36 #include "util.h"
37 #include "evlist.h"
38 #include "cpumap.h"
39 #include "thread_map.h"
40 #include "asm/bug.h"
41 #include "auxtrace.h"
42
43 #include <linux/hash.h>
44
45 #include "event.h"
46 #include "session.h"
47 #include "debug.h"
48 #include "parse-options.h"
49
50 #include "intel-pt.h"
51 #include "intel-bts.h"
52 #include "cs-etm.h"
53
54 int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
55                         struct auxtrace_mmap_params *mp,
56                         void *userpg, int fd)
57 {
58         struct perf_event_mmap_page *pc = userpg;
59
60         WARN_ONCE(mm->base, "Uninitialized auxtrace_mmap\n");
61
62         mm->userpg = userpg;
63         mm->mask = mp->mask;
64         mm->len = mp->len;
65         mm->prev = 0;
66         mm->idx = mp->idx;
67         mm->tid = mp->tid;
68         mm->cpu = mp->cpu;
69
70         if (!mp->len) {
71                 mm->base = NULL;
72                 return 0;
73         }
74
75 #if BITS_PER_LONG != 64 && !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
76         pr_err("Cannot use AUX area tracing mmaps\n");
77         return -1;
78 #endif
79
80         pc->aux_offset = mp->offset;
81         pc->aux_size = mp->len;
82
83         mm->base = mmap(NULL, mp->len, mp->prot, MAP_SHARED, fd, mp->offset);
84         if (mm->base == MAP_FAILED) {
85                 pr_debug2("failed to mmap AUX area\n");
86                 mm->base = NULL;
87                 return -1;
88         }
89
90         return 0;
91 }
92
93 void auxtrace_mmap__munmap(struct auxtrace_mmap *mm)
94 {
95         if (mm->base) {
96                 munmap(mm->base, mm->len);
97                 mm->base = NULL;
98         }
99 }
100
101 void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
102                                 off_t auxtrace_offset,
103                                 unsigned int auxtrace_pages,
104                                 bool auxtrace_overwrite)
105 {
106         if (auxtrace_pages) {
107                 mp->offset = auxtrace_offset;
108                 mp->len = auxtrace_pages * (size_t)page_size;
109                 mp->mask = is_power_of_2(mp->len) ? mp->len - 1 : 0;
110                 mp->prot = PROT_READ | (auxtrace_overwrite ? 0 : PROT_WRITE);
111                 pr_debug2("AUX area mmap length %zu\n", mp->len);
112         } else {
113                 mp->len = 0;
114         }
115 }
116
117 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
118                                    struct perf_evlist *evlist, int idx,
119                                    bool per_cpu)
120 {
121         mp->idx = idx;
122
123         if (per_cpu) {
124                 mp->cpu = evlist->cpus->map[idx];
125                 if (evlist->threads)
126                         mp->tid = thread_map__pid(evlist->threads, 0);
127                 else
128                         mp->tid = -1;
129         } else {
130                 mp->cpu = -1;
131                 mp->tid = thread_map__pid(evlist->threads, idx);
132         }
133 }
134
135 #define AUXTRACE_INIT_NR_QUEUES 32
136
137 static struct auxtrace_queue *auxtrace_alloc_queue_array(unsigned int nr_queues)
138 {
139         struct auxtrace_queue *queue_array;
140         unsigned int max_nr_queues, i;
141
142         max_nr_queues = UINT_MAX / sizeof(struct auxtrace_queue);
143         if (nr_queues > max_nr_queues)
144                 return NULL;
145
146         queue_array = calloc(nr_queues, sizeof(struct auxtrace_queue));
147         if (!queue_array)
148                 return NULL;
149
150         for (i = 0; i < nr_queues; i++) {
151                 INIT_LIST_HEAD(&queue_array[i].head);
152                 queue_array[i].priv = NULL;
153         }
154
155         return queue_array;
156 }
157
158 int auxtrace_queues__init(struct auxtrace_queues *queues)
159 {
160         queues->nr_queues = AUXTRACE_INIT_NR_QUEUES;
161         queues->queue_array = auxtrace_alloc_queue_array(queues->nr_queues);
162         if (!queues->queue_array)
163                 return -ENOMEM;
164         return 0;
165 }
166
167 static int auxtrace_queues__grow(struct auxtrace_queues *queues,
168                                  unsigned int new_nr_queues)
169 {
170         unsigned int nr_queues = queues->nr_queues;
171         struct auxtrace_queue *queue_array;
172         unsigned int i;
173
174         if (!nr_queues)
175                 nr_queues = AUXTRACE_INIT_NR_QUEUES;
176
177         while (nr_queues && nr_queues < new_nr_queues)
178                 nr_queues <<= 1;
179
180         if (nr_queues < queues->nr_queues || nr_queues < new_nr_queues)
181                 return -EINVAL;
182
183         queue_array = auxtrace_alloc_queue_array(nr_queues);
184         if (!queue_array)
185                 return -ENOMEM;
186
187         for (i = 0; i < queues->nr_queues; i++) {
188                 list_splice_tail(&queues->queue_array[i].head,
189                                  &queue_array[i].head);
190                 queue_array[i].tid = queues->queue_array[i].tid;
191                 queue_array[i].cpu = queues->queue_array[i].cpu;
192                 queue_array[i].set = queues->queue_array[i].set;
193                 queue_array[i].priv = queues->queue_array[i].priv;
194         }
195
196         queues->nr_queues = nr_queues;
197         queues->queue_array = queue_array;
198
199         return 0;
200 }
201
202 static void *auxtrace_copy_data(u64 size, struct perf_session *session)
203 {
204         int fd = perf_data_file__fd(session->file);
205         void *p;
206         ssize_t ret;
207
208         if (size > SSIZE_MAX)
209                 return NULL;
210
211         p = malloc(size);
212         if (!p)
213                 return NULL;
214
215         ret = readn(fd, p, size);
216         if (ret != (ssize_t)size) {
217                 free(p);
218                 return NULL;
219         }
220
221         return p;
222 }
223
224 static int auxtrace_queues__add_buffer(struct auxtrace_queues *queues,
225                                        unsigned int idx,
226                                        struct auxtrace_buffer *buffer)
227 {
228         struct auxtrace_queue *queue;
229         int err;
230
231         if (idx >= queues->nr_queues) {
232                 err = auxtrace_queues__grow(queues, idx + 1);
233                 if (err)
234                         return err;
235         }
236
237         queue = &queues->queue_array[idx];
238
239         if (!queue->set) {
240                 queue->set = true;
241                 queue->tid = buffer->tid;
242                 queue->cpu = buffer->cpu;
243         } else if (buffer->cpu != queue->cpu || buffer->tid != queue->tid) {
244                 pr_err("auxtrace queue conflict: cpu %d, tid %d vs cpu %d, tid %d\n",
245                        queue->cpu, queue->tid, buffer->cpu, buffer->tid);
246                 return -EINVAL;
247         }
248
249         buffer->buffer_nr = queues->next_buffer_nr++;
250
251         list_add_tail(&buffer->list, &queue->head);
252
253         queues->new_data = true;
254         queues->populated = true;
255
256         return 0;
257 }
258
259 /* Limit buffers to 32MiB on 32-bit */
260 #define BUFFER_LIMIT_FOR_32_BIT (32 * 1024 * 1024)
261
262 static int auxtrace_queues__split_buffer(struct auxtrace_queues *queues,
263                                          unsigned int idx,
264                                          struct auxtrace_buffer *buffer)
265 {
266         u64 sz = buffer->size;
267         bool consecutive = false;
268         struct auxtrace_buffer *b;
269         int err;
270
271         while (sz > BUFFER_LIMIT_FOR_32_BIT) {
272                 b = memdup(buffer, sizeof(struct auxtrace_buffer));
273                 if (!b)
274                         return -ENOMEM;
275                 b->size = BUFFER_LIMIT_FOR_32_BIT;
276                 b->consecutive = consecutive;
277                 err = auxtrace_queues__add_buffer(queues, idx, b);
278                 if (err) {
279                         auxtrace_buffer__free(b);
280                         return err;
281                 }
282                 buffer->data_offset += BUFFER_LIMIT_FOR_32_BIT;
283                 sz -= BUFFER_LIMIT_FOR_32_BIT;
284                 consecutive = true;
285         }
286
287         buffer->size = sz;
288         buffer->consecutive = consecutive;
289
290         return 0;
291 }
292
293 static int auxtrace_queues__add_event_buffer(struct auxtrace_queues *queues,
294                                              struct perf_session *session,
295                                              unsigned int idx,
296                                              struct auxtrace_buffer *buffer)
297 {
298         if (session->one_mmap) {
299                 buffer->data = buffer->data_offset - session->one_mmap_offset +
300                                session->one_mmap_addr;
301         } else if (perf_data_file__is_pipe(session->file)) {
302                 buffer->data = auxtrace_copy_data(buffer->size, session);
303                 if (!buffer->data)
304                         return -ENOMEM;
305                 buffer->data_needs_freeing = true;
306         } else if (BITS_PER_LONG == 32 &&
307                    buffer->size > BUFFER_LIMIT_FOR_32_BIT) {
308                 int err;
309
310                 err = auxtrace_queues__split_buffer(queues, idx, buffer);
311                 if (err)
312                         return err;
313         }
314
315         return auxtrace_queues__add_buffer(queues, idx, buffer);
316 }
317
318 int auxtrace_queues__add_event(struct auxtrace_queues *queues,
319                                struct perf_session *session,
320                                union perf_event *event, off_t data_offset,
321                                struct auxtrace_buffer **buffer_ptr)
322 {
323         struct auxtrace_buffer *buffer;
324         unsigned int idx;
325         int err;
326
327         buffer = zalloc(sizeof(struct auxtrace_buffer));
328         if (!buffer)
329                 return -ENOMEM;
330
331         buffer->pid = -1;
332         buffer->tid = event->auxtrace.tid;
333         buffer->cpu = event->auxtrace.cpu;
334         buffer->data_offset = data_offset;
335         buffer->offset = event->auxtrace.offset;
336         buffer->reference = event->auxtrace.reference;
337         buffer->size = event->auxtrace.size;
338         idx = event->auxtrace.idx;
339
340         err = auxtrace_queues__add_event_buffer(queues, session, idx, buffer);
341         if (err)
342                 goto out_err;
343
344         if (buffer_ptr)
345                 *buffer_ptr = buffer;
346
347         return 0;
348
349 out_err:
350         auxtrace_buffer__free(buffer);
351         return err;
352 }
353
354 static int auxtrace_queues__add_indexed_event(struct auxtrace_queues *queues,
355                                               struct perf_session *session,
356                                               off_t file_offset, size_t sz)
357 {
358         union perf_event *event;
359         int err;
360         char buf[PERF_SAMPLE_MAX_SIZE];
361
362         err = perf_session__peek_event(session, file_offset, buf,
363                                        PERF_SAMPLE_MAX_SIZE, &event, NULL);
364         if (err)
365                 return err;
366
367         if (event->header.type == PERF_RECORD_AUXTRACE) {
368                 if (event->header.size < sizeof(struct auxtrace_event) ||
369                     event->header.size != sz) {
370                         err = -EINVAL;
371                         goto out;
372                 }
373                 file_offset += event->header.size;
374                 err = auxtrace_queues__add_event(queues, session, event,
375                                                  file_offset, NULL);
376         }
377 out:
378         return err;
379 }
380
381 void auxtrace_queues__free(struct auxtrace_queues *queues)
382 {
383         unsigned int i;
384
385         for (i = 0; i < queues->nr_queues; i++) {
386                 while (!list_empty(&queues->queue_array[i].head)) {
387                         struct auxtrace_buffer *buffer;
388
389                         buffer = list_entry(queues->queue_array[i].head.next,
390                                             struct auxtrace_buffer, list);
391                         list_del(&buffer->list);
392                         auxtrace_buffer__free(buffer);
393                 }
394         }
395
396         zfree(&queues->queue_array);
397         queues->nr_queues = 0;
398 }
399
400 static void auxtrace_heapify(struct auxtrace_heap_item *heap_array,
401                              unsigned int pos, unsigned int queue_nr,
402                              u64 ordinal)
403 {
404         unsigned int parent;
405
406         while (pos) {
407                 parent = (pos - 1) >> 1;
408                 if (heap_array[parent].ordinal <= ordinal)
409                         break;
410                 heap_array[pos] = heap_array[parent];
411                 pos = parent;
412         }
413         heap_array[pos].queue_nr = queue_nr;
414         heap_array[pos].ordinal = ordinal;
415 }
416
417 int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr,
418                        u64 ordinal)
419 {
420         struct auxtrace_heap_item *heap_array;
421
422         if (queue_nr >= heap->heap_sz) {
423                 unsigned int heap_sz = AUXTRACE_INIT_NR_QUEUES;
424
425                 while (heap_sz <= queue_nr)
426                         heap_sz <<= 1;
427                 heap_array = realloc(heap->heap_array,
428                                      heap_sz * sizeof(struct auxtrace_heap_item));
429                 if (!heap_array)
430                         return -ENOMEM;
431                 heap->heap_array = heap_array;
432                 heap->heap_sz = heap_sz;
433         }
434
435         auxtrace_heapify(heap->heap_array, heap->heap_cnt++, queue_nr, ordinal);
436
437         return 0;
438 }
439
440 void auxtrace_heap__free(struct auxtrace_heap *heap)
441 {
442         zfree(&heap->heap_array);
443         heap->heap_cnt = 0;
444         heap->heap_sz = 0;
445 }
446
447 void auxtrace_heap__pop(struct auxtrace_heap *heap)
448 {
449         unsigned int pos, last, heap_cnt = heap->heap_cnt;
450         struct auxtrace_heap_item *heap_array;
451
452         if (!heap_cnt)
453                 return;
454
455         heap->heap_cnt -= 1;
456
457         heap_array = heap->heap_array;
458
459         pos = 0;
460         while (1) {
461                 unsigned int left, right;
462
463                 left = (pos << 1) + 1;
464                 if (left >= heap_cnt)
465                         break;
466                 right = left + 1;
467                 if (right >= heap_cnt) {
468                         heap_array[pos] = heap_array[left];
469                         return;
470                 }
471                 if (heap_array[left].ordinal < heap_array[right].ordinal) {
472                         heap_array[pos] = heap_array[left];
473                         pos = left;
474                 } else {
475                         heap_array[pos] = heap_array[right];
476                         pos = right;
477                 }
478         }
479
480         last = heap_cnt - 1;
481         auxtrace_heapify(heap_array, pos, heap_array[last].queue_nr,
482                          heap_array[last].ordinal);
483 }
484
485 size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr,
486                                        struct perf_evlist *evlist)
487 {
488         if (itr)
489                 return itr->info_priv_size(itr, evlist);
490         return 0;
491 }
492
493 static int auxtrace_not_supported(void)
494 {
495         pr_err("AUX area tracing is not supported on this architecture\n");
496         return -EINVAL;
497 }
498
499 int auxtrace_record__info_fill(struct auxtrace_record *itr,
500                                struct perf_session *session,
501                                struct auxtrace_info_event *auxtrace_info,
502                                size_t priv_size)
503 {
504         if (itr)
505                 return itr->info_fill(itr, session, auxtrace_info, priv_size);
506         return auxtrace_not_supported();
507 }
508
509 void auxtrace_record__free(struct auxtrace_record *itr)
510 {
511         if (itr)
512                 itr->free(itr);
513 }
514
515 int auxtrace_record__snapshot_start(struct auxtrace_record *itr)
516 {
517         if (itr && itr->snapshot_start)
518                 return itr->snapshot_start(itr);
519         return 0;
520 }
521
522 int auxtrace_record__snapshot_finish(struct auxtrace_record *itr)
523 {
524         if (itr && itr->snapshot_finish)
525                 return itr->snapshot_finish(itr);
526         return 0;
527 }
528
529 int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx,
530                                    struct auxtrace_mmap *mm,
531                                    unsigned char *data, u64 *head, u64 *old)
532 {
533         if (itr && itr->find_snapshot)
534                 return itr->find_snapshot(itr, idx, mm, data, head, old);
535         return 0;
536 }
537
538 int auxtrace_record__options(struct auxtrace_record *itr,
539                              struct perf_evlist *evlist,
540                              struct record_opts *opts)
541 {
542         if (itr)
543                 return itr->recording_options(itr, evlist, opts);
544         return 0;
545 }
546
547 u64 auxtrace_record__reference(struct auxtrace_record *itr)
548 {
549         if (itr)
550                 return itr->reference(itr);
551         return 0;
552 }
553
554 int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
555                                     struct record_opts *opts, const char *str)
556 {
557         if (!str)
558                 return 0;
559
560         if (itr)
561                 return itr->parse_snapshot_options(itr, opts, str);
562
563         pr_err("No AUX area tracing to snapshot\n");
564         return -EINVAL;
565 }
566
567 struct auxtrace_record *__weak
568 auxtrace_record__init(struct perf_evlist *evlist __maybe_unused, int *err)
569 {
570         *err = 0;
571         return NULL;
572 }
573
574 static int auxtrace_index__alloc(struct list_head *head)
575 {
576         struct auxtrace_index *auxtrace_index;
577
578         auxtrace_index = malloc(sizeof(struct auxtrace_index));
579         if (!auxtrace_index)
580                 return -ENOMEM;
581
582         auxtrace_index->nr = 0;
583         INIT_LIST_HEAD(&auxtrace_index->list);
584
585         list_add_tail(&auxtrace_index->list, head);
586
587         return 0;
588 }
589
590 void auxtrace_index__free(struct list_head *head)
591 {
592         struct auxtrace_index *auxtrace_index, *n;
593
594         list_for_each_entry_safe(auxtrace_index, n, head, list) {
595                 list_del(&auxtrace_index->list);
596                 free(auxtrace_index);
597         }
598 }
599
600 static struct auxtrace_index *auxtrace_index__last(struct list_head *head)
601 {
602         struct auxtrace_index *auxtrace_index;
603         int err;
604
605         if (list_empty(head)) {
606                 err = auxtrace_index__alloc(head);
607                 if (err)
608                         return NULL;
609         }
610
611         auxtrace_index = list_entry(head->prev, struct auxtrace_index, list);
612
613         if (auxtrace_index->nr >= PERF_AUXTRACE_INDEX_ENTRY_COUNT) {
614                 err = auxtrace_index__alloc(head);
615                 if (err)
616                         return NULL;
617                 auxtrace_index = list_entry(head->prev, struct auxtrace_index,
618                                             list);
619         }
620
621         return auxtrace_index;
622 }
623
624 int auxtrace_index__auxtrace_event(struct list_head *head,
625                                    union perf_event *event, off_t file_offset)
626 {
627         struct auxtrace_index *auxtrace_index;
628         size_t nr;
629
630         auxtrace_index = auxtrace_index__last(head);
631         if (!auxtrace_index)
632                 return -ENOMEM;
633
634         nr = auxtrace_index->nr;
635         auxtrace_index->entries[nr].file_offset = file_offset;
636         auxtrace_index->entries[nr].sz = event->header.size;
637         auxtrace_index->nr += 1;
638
639         return 0;
640 }
641
642 static int auxtrace_index__do_write(int fd,
643                                     struct auxtrace_index *auxtrace_index)
644 {
645         struct auxtrace_index_entry ent;
646         size_t i;
647
648         for (i = 0; i < auxtrace_index->nr; i++) {
649                 ent.file_offset = auxtrace_index->entries[i].file_offset;
650                 ent.sz = auxtrace_index->entries[i].sz;
651                 if (writen(fd, &ent, sizeof(ent)) != sizeof(ent))
652                         return -errno;
653         }
654         return 0;
655 }
656
657 int auxtrace_index__write(int fd, struct list_head *head)
658 {
659         struct auxtrace_index *auxtrace_index;
660         u64 total = 0;
661         int err;
662
663         list_for_each_entry(auxtrace_index, head, list)
664                 total += auxtrace_index->nr;
665
666         if (writen(fd, &total, sizeof(total)) != sizeof(total))
667                 return -errno;
668
669         list_for_each_entry(auxtrace_index, head, list) {
670                 err = auxtrace_index__do_write(fd, auxtrace_index);
671                 if (err)
672                         return err;
673         }
674
675         return 0;
676 }
677
678 static int auxtrace_index__process_entry(int fd, struct list_head *head,
679                                          bool needs_swap)
680 {
681         struct auxtrace_index *auxtrace_index;
682         struct auxtrace_index_entry ent;
683         size_t nr;
684
685         if (readn(fd, &ent, sizeof(ent)) != sizeof(ent))
686                 return -1;
687
688         auxtrace_index = auxtrace_index__last(head);
689         if (!auxtrace_index)
690                 return -1;
691
692         nr = auxtrace_index->nr;
693         if (needs_swap) {
694                 auxtrace_index->entries[nr].file_offset =
695                                                 bswap_64(ent.file_offset);
696                 auxtrace_index->entries[nr].sz = bswap_64(ent.sz);
697         } else {
698                 auxtrace_index->entries[nr].file_offset = ent.file_offset;
699                 auxtrace_index->entries[nr].sz = ent.sz;
700         }
701
702         auxtrace_index->nr = nr + 1;
703
704         return 0;
705 }
706
707 int auxtrace_index__process(int fd, u64 size, struct perf_session *session,
708                             bool needs_swap)
709 {
710         struct list_head *head = &session->auxtrace_index;
711         u64 nr;
712
713         if (readn(fd, &nr, sizeof(u64)) != sizeof(u64))
714                 return -1;
715
716         if (needs_swap)
717                 nr = bswap_64(nr);
718
719         if (sizeof(u64) + nr * sizeof(struct auxtrace_index_entry) > size)
720                 return -1;
721
722         while (nr--) {
723                 int err;
724
725                 err = auxtrace_index__process_entry(fd, head, needs_swap);
726                 if (err)
727                         return -1;
728         }
729
730         return 0;
731 }
732
733 static int auxtrace_queues__process_index_entry(struct auxtrace_queues *queues,
734                                                 struct perf_session *session,
735                                                 struct auxtrace_index_entry *ent)
736 {
737         return auxtrace_queues__add_indexed_event(queues, session,
738                                                   ent->file_offset, ent->sz);
739 }
740
741 int auxtrace_queues__process_index(struct auxtrace_queues *queues,
742                                    struct perf_session *session)
743 {
744         struct auxtrace_index *auxtrace_index;
745         struct auxtrace_index_entry *ent;
746         size_t i;
747         int err;
748
749         list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
750                 for (i = 0; i < auxtrace_index->nr; i++) {
751                         ent = &auxtrace_index->entries[i];
752                         err = auxtrace_queues__process_index_entry(queues,
753                                                                    session,
754                                                                    ent);
755                         if (err)
756                                 return err;
757                 }
758         }
759         return 0;
760 }
761
762 struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue,
763                                               struct auxtrace_buffer *buffer)
764 {
765         if (buffer) {
766                 if (list_is_last(&buffer->list, &queue->head))
767                         return NULL;
768                 return list_entry(buffer->list.next, struct auxtrace_buffer,
769                                   list);
770         } else {
771                 if (list_empty(&queue->head))
772                         return NULL;
773                 return list_entry(queue->head.next, struct auxtrace_buffer,
774                                   list);
775         }
776 }
777
778 void *auxtrace_buffer__get_data(struct auxtrace_buffer *buffer, int fd)
779 {
780         size_t adj = buffer->data_offset & (page_size - 1);
781         size_t size = buffer->size + adj;
782         off_t file_offset = buffer->data_offset - adj;
783         void *addr;
784
785         if (buffer->data)
786                 return buffer->data;
787
788         addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, file_offset);
789         if (addr == MAP_FAILED)
790                 return NULL;
791
792         buffer->mmap_addr = addr;
793         buffer->mmap_size = size;
794
795         buffer->data = addr + adj;
796
797         return buffer->data;
798 }
799
800 void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer)
801 {
802         if (!buffer->data || !buffer->mmap_addr)
803                 return;
804         munmap(buffer->mmap_addr, buffer->mmap_size);
805         buffer->mmap_addr = NULL;
806         buffer->mmap_size = 0;
807         buffer->data = NULL;
808         buffer->use_data = NULL;
809 }
810
811 void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer)
812 {
813         auxtrace_buffer__put_data(buffer);
814         if (buffer->data_needs_freeing) {
815                 buffer->data_needs_freeing = false;
816                 zfree(&buffer->data);
817                 buffer->use_data = NULL;
818                 buffer->size = 0;
819         }
820 }
821
822 void auxtrace_buffer__free(struct auxtrace_buffer *buffer)
823 {
824         auxtrace_buffer__drop_data(buffer);
825         free(buffer);
826 }
827
828 void auxtrace_synth_error(struct auxtrace_error_event *auxtrace_error, int type,
829                           int code, int cpu, pid_t pid, pid_t tid, u64 ip,
830                           const char *msg)
831 {
832         size_t size;
833
834         memset(auxtrace_error, 0, sizeof(struct auxtrace_error_event));
835
836         auxtrace_error->header.type = PERF_RECORD_AUXTRACE_ERROR;
837         auxtrace_error->type = type;
838         auxtrace_error->code = code;
839         auxtrace_error->cpu = cpu;
840         auxtrace_error->pid = pid;
841         auxtrace_error->tid = tid;
842         auxtrace_error->ip = ip;
843         strlcpy(auxtrace_error->msg, msg, MAX_AUXTRACE_ERROR_MSG);
844
845         size = (void *)auxtrace_error->msg - (void *)auxtrace_error +
846                strlen(auxtrace_error->msg) + 1;
847         auxtrace_error->header.size = PERF_ALIGN(size, sizeof(u64));
848 }
849
850 int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr,
851                                          struct perf_tool *tool,
852                                          struct perf_session *session,
853                                          perf_event__handler_t process)
854 {
855         union perf_event *ev;
856         size_t priv_size;
857         int err;
858
859         pr_debug2("Synthesizing auxtrace information\n");
860         priv_size = auxtrace_record__info_priv_size(itr, session->evlist);
861         ev = zalloc(sizeof(struct auxtrace_info_event) + priv_size);
862         if (!ev)
863                 return -ENOMEM;
864
865         ev->auxtrace_info.header.type = PERF_RECORD_AUXTRACE_INFO;
866         ev->auxtrace_info.header.size = sizeof(struct auxtrace_info_event) +
867                                         priv_size;
868         err = auxtrace_record__info_fill(itr, session, &ev->auxtrace_info,
869                                          priv_size);
870         if (err)
871                 goto out_free;
872
873         err = process(tool, ev, NULL, NULL);
874 out_free:
875         free(ev);
876         return err;
877 }
878
879 static bool auxtrace__dont_decode(struct perf_session *session)
880 {
881         return !session->itrace_synth_opts ||
882                session->itrace_synth_opts->dont_decode;
883 }
884
885 int perf_event__process_auxtrace_info(struct perf_tool *tool __maybe_unused,
886                                       union perf_event *event,
887                                       struct perf_session *session)
888 {
889         enum auxtrace_type type = event->auxtrace_info.type;
890
891         if (dump_trace)
892                 fprintf(stdout, " type: %u\n", type);
893
894         switch (type) {
895         case PERF_AUXTRACE_INTEL_PT:
896                 return intel_pt_process_auxtrace_info(event, session);
897         case PERF_AUXTRACE_INTEL_BTS:
898                 return intel_bts_process_auxtrace_info(event, session);
899         case PERF_AUXTRACE_CS_ETM:
900                 return cs_etm__process_auxtrace_info(event, session);
901         case PERF_AUXTRACE_UNKNOWN:
902         default:
903                 return -EINVAL;
904         }
905 }
906
907 s64 perf_event__process_auxtrace(struct perf_tool *tool,
908                                  union perf_event *event,
909                                  struct perf_session *session)
910 {
911         s64 err;
912
913         if (dump_trace)
914                 fprintf(stdout, " size: %#"PRIx64"  offset: %#"PRIx64"  ref: %#"PRIx64"  idx: %u  tid: %d  cpu: %d\n",
915                         event->auxtrace.size, event->auxtrace.offset,
916                         event->auxtrace.reference, event->auxtrace.idx,
917                         event->auxtrace.tid, event->auxtrace.cpu);
918
919         if (auxtrace__dont_decode(session))
920                 return event->auxtrace.size;
921
922         if (!session->auxtrace || event->header.type != PERF_RECORD_AUXTRACE)
923                 return -EINVAL;
924
925         err = session->auxtrace->process_auxtrace_event(session, event, tool);
926         if (err < 0)
927                 return err;
928
929         return event->auxtrace.size;
930 }
931
932 #define PERF_ITRACE_DEFAULT_PERIOD_TYPE         PERF_ITRACE_PERIOD_NANOSECS
933 #define PERF_ITRACE_DEFAULT_PERIOD              100000
934 #define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ        16
935 #define PERF_ITRACE_MAX_CALLCHAIN_SZ            1024
936 #define PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ      64
937 #define PERF_ITRACE_MAX_LAST_BRANCH_SZ          1024
938
939 void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts)
940 {
941         synth_opts->instructions = true;
942         synth_opts->branches = true;
943         synth_opts->transactions = true;
944         synth_opts->errors = true;
945         synth_opts->period_type = PERF_ITRACE_DEFAULT_PERIOD_TYPE;
946         synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
947         synth_opts->callchain_sz = PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
948         synth_opts->last_branch_sz = PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
949 }
950
951 /*
952  * Please check tools/perf/Documentation/perf-script.txt for information
953  * about the options parsed here, which is introduced after this cset,
954  * when support in 'perf script' for these options is introduced.
955  */
956 int itrace_parse_synth_opts(const struct option *opt, const char *str,
957                             int unset)
958 {
959         struct itrace_synth_opts *synth_opts = opt->value;
960         const char *p;
961         char *endptr;
962         bool period_type_set = false;
963         bool period_set = false;
964
965         synth_opts->set = true;
966
967         if (unset) {
968                 synth_opts->dont_decode = true;
969                 return 0;
970         }
971
972         if (!str) {
973                 itrace_synth_opts__set_default(synth_opts);
974                 return 0;
975         }
976
977         for (p = str; *p;) {
978                 switch (*p++) {
979                 case 'i':
980                         synth_opts->instructions = true;
981                         while (*p == ' ' || *p == ',')
982                                 p += 1;
983                         if (isdigit(*p)) {
984                                 synth_opts->period = strtoull(p, &endptr, 10);
985                                 period_set = true;
986                                 p = endptr;
987                                 while (*p == ' ' || *p == ',')
988                                         p += 1;
989                                 switch (*p++) {
990                                 case 'i':
991                                         synth_opts->period_type =
992                                                 PERF_ITRACE_PERIOD_INSTRUCTIONS;
993                                         period_type_set = true;
994                                         break;
995                                 case 't':
996                                         synth_opts->period_type =
997                                                 PERF_ITRACE_PERIOD_TICKS;
998                                         period_type_set = true;
999                                         break;
1000                                 case 'm':
1001                                         synth_opts->period *= 1000;
1002                                         /* Fall through */
1003                                 case 'u':
1004                                         synth_opts->period *= 1000;
1005                                         /* Fall through */
1006                                 case 'n':
1007                                         if (*p++ != 's')
1008                                                 goto out_err;
1009                                         synth_opts->period_type =
1010                                                 PERF_ITRACE_PERIOD_NANOSECS;
1011                                         period_type_set = true;
1012                                         break;
1013                                 case '\0':
1014                                         goto out;
1015                                 default:
1016                                         goto out_err;
1017                                 }
1018                         }
1019                         break;
1020                 case 'b':
1021                         synth_opts->branches = true;
1022                         break;
1023                 case 'x':
1024                         synth_opts->transactions = true;
1025                         break;
1026                 case 'e':
1027                         synth_opts->errors = true;
1028                         break;
1029                 case 'd':
1030                         synth_opts->log = true;
1031                         break;
1032                 case 'c':
1033                         synth_opts->branches = true;
1034                         synth_opts->calls = true;
1035                         break;
1036                 case 'r':
1037                         synth_opts->branches = true;
1038                         synth_opts->returns = true;
1039                         break;
1040                 case 'g':
1041                         synth_opts->callchain = true;
1042                         synth_opts->callchain_sz =
1043                                         PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
1044                         while (*p == ' ' || *p == ',')
1045                                 p += 1;
1046                         if (isdigit(*p)) {
1047                                 unsigned int val;
1048
1049                                 val = strtoul(p, &endptr, 10);
1050                                 p = endptr;
1051                                 if (!val || val > PERF_ITRACE_MAX_CALLCHAIN_SZ)
1052                                         goto out_err;
1053                                 synth_opts->callchain_sz = val;
1054                         }
1055                         break;
1056                 case 'l':
1057                         synth_opts->last_branch = true;
1058                         synth_opts->last_branch_sz =
1059                                         PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
1060                         while (*p == ' ' || *p == ',')
1061                                 p += 1;
1062                         if (isdigit(*p)) {
1063                                 unsigned int val;
1064
1065                                 val = strtoul(p, &endptr, 10);
1066                                 p = endptr;
1067                                 if (!val ||
1068                                     val > PERF_ITRACE_MAX_LAST_BRANCH_SZ)
1069                                         goto out_err;
1070                                 synth_opts->last_branch_sz = val;
1071                         }
1072                         break;
1073                 case ' ':
1074                 case ',':
1075                         break;
1076                 default:
1077                         goto out_err;
1078                 }
1079         }
1080 out:
1081         if (synth_opts->instructions) {
1082                 if (!period_type_set)
1083                         synth_opts->period_type =
1084                                         PERF_ITRACE_DEFAULT_PERIOD_TYPE;
1085                 if (!period_set)
1086                         synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
1087         }
1088
1089         return 0;
1090
1091 out_err:
1092         pr_err("Bad Instruction Tracing options '%s'\n", str);
1093         return -EINVAL;
1094 }
1095
1096 static const char * const auxtrace_error_type_name[] = {
1097         [PERF_AUXTRACE_ERROR_ITRACE] = "instruction trace",
1098 };
1099
1100 static const char *auxtrace_error_name(int type)
1101 {
1102         const char *error_type_name = NULL;
1103
1104         if (type < PERF_AUXTRACE_ERROR_MAX)
1105                 error_type_name = auxtrace_error_type_name[type];
1106         if (!error_type_name)
1107                 error_type_name = "unknown AUX";
1108         return error_type_name;
1109 }
1110
1111 size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp)
1112 {
1113         struct auxtrace_error_event *e = &event->auxtrace_error;
1114         int ret;
1115
1116         ret = fprintf(fp, " %s error type %u",
1117                       auxtrace_error_name(e->type), e->type);
1118         ret += fprintf(fp, " cpu %d pid %d tid %d ip %#"PRIx64" code %u: %s\n",
1119                        e->cpu, e->pid, e->tid, e->ip, e->code, e->msg);
1120         return ret;
1121 }
1122
1123 void perf_session__auxtrace_error_inc(struct perf_session *session,
1124                                       union perf_event *event)
1125 {
1126         struct auxtrace_error_event *e = &event->auxtrace_error;
1127
1128         if (e->type < PERF_AUXTRACE_ERROR_MAX)
1129                 session->evlist->stats.nr_auxtrace_errors[e->type] += 1;
1130 }
1131
1132 void events_stats__auxtrace_error_warn(const struct events_stats *stats)
1133 {
1134         int i;
1135
1136         for (i = 0; i < PERF_AUXTRACE_ERROR_MAX; i++) {
1137                 if (!stats->nr_auxtrace_errors[i])
1138                         continue;
1139                 ui__warning("%u %s errors\n",
1140                             stats->nr_auxtrace_errors[i],
1141                             auxtrace_error_name(i));
1142         }
1143 }
1144
1145 int perf_event__process_auxtrace_error(struct perf_tool *tool __maybe_unused,
1146                                        union perf_event *event,
1147                                        struct perf_session *session)
1148 {
1149         if (auxtrace__dont_decode(session))
1150                 return 0;
1151
1152         perf_event__fprintf_auxtrace_error(event, stdout);
1153         return 0;
1154 }
1155
1156 static int __auxtrace_mmap__read(struct auxtrace_mmap *mm,
1157                                  struct auxtrace_record *itr,
1158                                  struct perf_tool *tool, process_auxtrace_t fn,
1159                                  bool snapshot, size_t snapshot_size)
1160 {
1161         u64 head, old = mm->prev, offset, ref;
1162         unsigned char *data = mm->base;
1163         size_t size, head_off, old_off, len1, len2, padding;
1164         union perf_event ev;
1165         void *data1, *data2;
1166
1167         if (snapshot) {
1168                 head = auxtrace_mmap__read_snapshot_head(mm);
1169                 if (auxtrace_record__find_snapshot(itr, mm->idx, mm, data,
1170                                                    &head, &old))
1171                         return -1;
1172         } else {
1173                 head = auxtrace_mmap__read_head(mm);
1174         }
1175
1176         if (old == head)
1177                 return 0;
1178
1179         pr_debug3("auxtrace idx %d old %#"PRIx64" head %#"PRIx64" diff %#"PRIx64"\n",
1180                   mm->idx, old, head, head - old);
1181
1182         if (mm->mask) {
1183                 head_off = head & mm->mask;
1184                 old_off = old & mm->mask;
1185         } else {
1186                 head_off = head % mm->len;
1187                 old_off = old % mm->len;
1188         }
1189
1190         if (head_off > old_off)
1191                 size = head_off - old_off;
1192         else
1193                 size = mm->len - (old_off - head_off);
1194
1195         if (snapshot && size > snapshot_size)
1196                 size = snapshot_size;
1197
1198         ref = auxtrace_record__reference(itr);
1199
1200         if (head > old || size <= head || mm->mask) {
1201                 offset = head - size;
1202         } else {
1203                 /*
1204                  * When the buffer size is not a power of 2, 'head' wraps at the
1205                  * highest multiple of the buffer size, so we have to subtract
1206                  * the remainder here.
1207                  */
1208                 u64 rem = (0ULL - mm->len) % mm->len;
1209
1210                 offset = head - size - rem;
1211         }
1212
1213         if (size > head_off) {
1214                 len1 = size - head_off;
1215                 data1 = &data[mm->len - len1];
1216                 len2 = head_off;
1217                 data2 = &data[0];
1218         } else {
1219                 len1 = size;
1220                 data1 = &data[head_off - len1];
1221                 len2 = 0;
1222                 data2 = NULL;
1223         }
1224
1225         if (itr->alignment) {
1226                 unsigned int unwanted = len1 % itr->alignment;
1227
1228                 len1 -= unwanted;
1229                 size -= unwanted;
1230         }
1231
1232         /* padding must be written by fn() e.g. record__process_auxtrace() */
1233         padding = size & (PERF_AUXTRACE_RECORD_ALIGNMENT - 1);
1234         if (padding)
1235                 padding = PERF_AUXTRACE_RECORD_ALIGNMENT - padding;
1236
1237         memset(&ev, 0, sizeof(ev));
1238         ev.auxtrace.header.type = PERF_RECORD_AUXTRACE;
1239         ev.auxtrace.header.size = sizeof(ev.auxtrace);
1240         ev.auxtrace.size = size + padding;
1241         ev.auxtrace.offset = offset;
1242         ev.auxtrace.reference = ref;
1243         ev.auxtrace.idx = mm->idx;
1244         ev.auxtrace.tid = mm->tid;
1245         ev.auxtrace.cpu = mm->cpu;
1246
1247         if (fn(tool, &ev, data1, len1, data2, len2))
1248                 return -1;
1249
1250         mm->prev = head;
1251
1252         if (!snapshot) {
1253                 auxtrace_mmap__write_tail(mm, head);
1254                 if (itr->read_finish) {
1255                         int err;
1256
1257                         err = itr->read_finish(itr, mm->idx);
1258                         if (err < 0)
1259                                 return err;
1260                 }
1261         }
1262
1263         return 1;
1264 }
1265
1266 int auxtrace_mmap__read(struct auxtrace_mmap *mm, struct auxtrace_record *itr,
1267                         struct perf_tool *tool, process_auxtrace_t fn)
1268 {
1269         return __auxtrace_mmap__read(mm, itr, tool, fn, false, 0);
1270 }
1271
1272 int auxtrace_mmap__read_snapshot(struct auxtrace_mmap *mm,
1273                                  struct auxtrace_record *itr,
1274                                  struct perf_tool *tool, process_auxtrace_t fn,
1275                                  size_t snapshot_size)
1276 {
1277         return __auxtrace_mmap__read(mm, itr, tool, fn, true, snapshot_size);
1278 }
1279
1280 /**
1281  * struct auxtrace_cache - hash table to implement a cache
1282  * @hashtable: the hashtable
1283  * @sz: hashtable size (number of hlists)
1284  * @entry_size: size of an entry
1285  * @limit: limit the number of entries to this maximum, when reached the cache
1286  *         is dropped and caching begins again with an empty cache
1287  * @cnt: current number of entries
1288  * @bits: hashtable size (@sz = 2^@bits)
1289  */
1290 struct auxtrace_cache {
1291         struct hlist_head *hashtable;
1292         size_t sz;
1293         size_t entry_size;
1294         size_t limit;
1295         size_t cnt;
1296         unsigned int bits;
1297 };
1298
1299 struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
1300                                            unsigned int limit_percent)
1301 {
1302         struct auxtrace_cache *c;
1303         struct hlist_head *ht;
1304         size_t sz, i;
1305
1306         c = zalloc(sizeof(struct auxtrace_cache));
1307         if (!c)
1308                 return NULL;
1309
1310         sz = 1UL << bits;
1311
1312         ht = calloc(sz, sizeof(struct hlist_head));
1313         if (!ht)
1314                 goto out_free;
1315
1316         for (i = 0; i < sz; i++)
1317                 INIT_HLIST_HEAD(&ht[i]);
1318
1319         c->hashtable = ht;
1320         c->sz = sz;
1321         c->entry_size = entry_size;
1322         c->limit = (c->sz * limit_percent) / 100;
1323         c->bits = bits;
1324
1325         return c;
1326
1327 out_free:
1328         free(c);
1329         return NULL;
1330 }
1331
1332 static void auxtrace_cache__drop(struct auxtrace_cache *c)
1333 {
1334         struct auxtrace_cache_entry *entry;
1335         struct hlist_node *tmp;
1336         size_t i;
1337
1338         if (!c)
1339                 return;
1340
1341         for (i = 0; i < c->sz; i++) {
1342                 hlist_for_each_entry_safe(entry, tmp, &c->hashtable[i], hash) {
1343                         hlist_del(&entry->hash);
1344                         auxtrace_cache__free_entry(c, entry);
1345                 }
1346         }
1347
1348         c->cnt = 0;
1349 }
1350
1351 void auxtrace_cache__free(struct auxtrace_cache *c)
1352 {
1353         if (!c)
1354                 return;
1355
1356         auxtrace_cache__drop(c);
1357         free(c->hashtable);
1358         free(c);
1359 }
1360
1361 void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c)
1362 {
1363         return malloc(c->entry_size);
1364 }
1365
1366 void auxtrace_cache__free_entry(struct auxtrace_cache *c __maybe_unused,
1367                                 void *entry)
1368 {
1369         free(entry);
1370 }
1371
1372 int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
1373                         struct auxtrace_cache_entry *entry)
1374 {
1375         if (c->limit && ++c->cnt > c->limit)
1376                 auxtrace_cache__drop(c);
1377
1378         entry->key = key;
1379         hlist_add_head(&entry->hash, &c->hashtable[hash_32(key, c->bits)]);
1380
1381         return 0;
1382 }
1383
1384 void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key)
1385 {
1386         struct auxtrace_cache_entry *entry;
1387         struct hlist_head *hlist;
1388
1389         if (!c)
1390                 return NULL;
1391
1392         hlist = &c->hashtable[hash_32(key, c->bits)];
1393         hlist_for_each_entry(entry, hlist, hash) {
1394                 if (entry->key == key)
1395                         return entry;
1396         }
1397
1398         return NULL;
1399 }