Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @file common.c |
| 3 | * |
| 4 | * @remark Copyright 2004 Oprofile Authors |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 5 | * @remark Copyright 2010 ARM Ltd. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * @remark Read the file COPYING |
| 7 | * |
| 8 | * @author Zwane Mwaikambo |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 9 | * @author Will Deacon [move to perf] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 12 | #include <linux/cpumask.h> |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 13 | #include <linux/init.h> |
| 14 | #include <linux/mutex.h> |
| 15 | #include <linux/oprofile.h> |
| 16 | #include <linux/perf_event.h> |
Will Deacon | d1e86d6 | 2010-04-30 11:38:39 +0100 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
Russell King | ae92dc9 | 2006-03-16 11:32:51 +0000 | [diff] [blame] | 18 | #include <linux/slab.h> |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 19 | #include <asm/stacktrace.h> |
| 20 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 22 | #include <asm/perf_event.h> |
| 23 | #include <asm/ptrace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 25 | #ifdef CONFIG_HW_PERF_EVENTS |
Will Deacon | 4295b89 | 2012-07-06 15:45:00 +0100 | [diff] [blame] | 26 | |
| 27 | /* |
| 28 | * OProfile has a curious naming scheme for the ARM PMUs, but they are |
| 29 | * part of the user ABI so we need to map from the perf PMU name for |
| 30 | * supported PMUs. |
| 31 | */ |
| 32 | static struct op_perf_name { |
| 33 | char *perf_name; |
| 34 | char *op_name; |
| 35 | } op_perf_name_map[] = { |
Mark Rutland | 3d1ff75 | 2012-12-19 16:33:24 +0000 | [diff] [blame] | 36 | { "armv5_xscale1", "arm/xscale1" }, |
| 37 | { "armv5_xscale2", "arm/xscale2" }, |
| 38 | { "armv6_1136", "arm/armv6" }, |
| 39 | { "armv6_1156", "arm/armv6" }, |
| 40 | { "armv6_1176", "arm/armv6" }, |
| 41 | { "armv6_11mpcore", "arm/mpcore" }, |
| 42 | { "armv7_cortex_a8", "arm/armv7" }, |
| 43 | { "armv7_cortex_a9", "arm/armv7-ca9" }, |
Will Deacon | 4295b89 | 2012-07-06 15:45:00 +0100 | [diff] [blame] | 44 | }; |
| 45 | |
Matt Fleming | 5694633 | 2010-10-08 21:42:17 +0100 | [diff] [blame] | 46 | char *op_name_from_perf_id(void) |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 47 | { |
Will Deacon | 4295b89 | 2012-07-06 15:45:00 +0100 | [diff] [blame] | 48 | int i; |
| 49 | struct op_perf_name names; |
| 50 | const char *perf_name = perf_pmu_name(); |
Matt Fleming | 5694633 | 2010-10-08 21:42:17 +0100 | [diff] [blame] | 51 | |
Will Deacon | 4295b89 | 2012-07-06 15:45:00 +0100 | [diff] [blame] | 52 | for (i = 0; i < ARRAY_SIZE(op_perf_name_map); ++i) { |
| 53 | names = op_perf_name_map[i]; |
| 54 | if (!strcmp(names.perf_name, perf_name)) |
| 55 | return names.op_name; |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 56 | } |
Will Deacon | 4295b89 | 2012-07-06 15:45:00 +0100 | [diff] [blame] | 57 | |
| 58 | return NULL; |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 59 | } |
Ari Kauppi | d14dd7e | 2011-01-20 13:57:19 -0500 | [diff] [blame] | 60 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 62 | static int report_trace(struct stackframe *frame, void *d) |
| 63 | { |
| 64 | unsigned int *depth = d; |
| 65 | |
| 66 | if (*depth) { |
| 67 | oprofile_add_trace(frame->pc); |
| 68 | (*depth)--; |
| 69 | } |
| 70 | |
| 71 | return *depth == 0; |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * The registers we're interested in are at the end of the variable |
| 76 | * length saved register structure. The fp points at the end of this |
| 77 | * structure so the address of this struct is: |
| 78 | * (struct frame_tail *)(xxx->fp)-1 |
| 79 | */ |
| 80 | struct frame_tail { |
| 81 | struct frame_tail *fp; |
| 82 | unsigned long sp; |
| 83 | unsigned long lr; |
| 84 | } __attribute__((packed)); |
| 85 | |
| 86 | static struct frame_tail* user_backtrace(struct frame_tail *tail) |
| 87 | { |
| 88 | struct frame_tail buftail[2]; |
| 89 | |
| 90 | /* Also check accessibility of one struct frame_tail beyond */ |
| 91 | if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) |
| 92 | return NULL; |
| 93 | if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail))) |
| 94 | return NULL; |
| 95 | |
| 96 | oprofile_add_trace(buftail[0].lr); |
| 97 | |
| 98 | /* frame pointers should strictly progress back up the stack |
| 99 | * (towards higher addresses) */ |
Rabin Vincent | cb06199 | 2011-02-09 11:35:12 +0100 | [diff] [blame] | 100 | if (tail + 1 >= buftail[0].fp) |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 101 | return NULL; |
| 102 | |
| 103 | return buftail[0].fp-1; |
| 104 | } |
| 105 | |
| 106 | static void arm_backtrace(struct pt_regs * const regs, unsigned int depth) |
| 107 | { |
| 108 | struct frame_tail *tail = ((struct frame_tail *) regs->ARM_fp) - 1; |
| 109 | |
| 110 | if (!user_mode(regs)) { |
| 111 | struct stackframe frame; |
Nikolay Borisov | e8a5dbc | 2014-06-03 19:51:32 +0100 | [diff] [blame] | 112 | arm_get_current_stackframe(regs, &frame); |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 113 | walk_stackframe(&frame, report_trace, &depth); |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | while (depth-- && tail && !((unsigned long) tail & 3)) |
| 118 | tail = user_backtrace(tail); |
| 119 | } |
| 120 | |
Matt Fleming | 58850e2 | 2010-09-27 20:35:29 +0100 | [diff] [blame] | 121 | int __init oprofile_arch_init(struct oprofile_operations *ops) |
| 122 | { |
Ari Kauppi | d14dd7e | 2011-01-20 13:57:19 -0500 | [diff] [blame] | 123 | /* provide backtrace support also in timer mode: */ |
Matt Fleming | 58850e2 | 2010-09-27 20:35:29 +0100 | [diff] [blame] | 124 | ops->backtrace = arm_backtrace; |
| 125 | |
| 126 | return oprofile_perf_init(ops); |
| 127 | } |
| 128 | |
Vladimir Zapolskiy | 55205c9 | 2011-12-22 16:15:40 +0100 | [diff] [blame] | 129 | void oprofile_arch_exit(void) |
Matt Fleming | 58850e2 | 2010-09-27 20:35:29 +0100 | [diff] [blame] | 130 | { |
| 131 | oprofile_perf_exit(); |
| 132 | } |