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 |
Matt Fleming | 5694633 | 2010-10-08 21:42:17 +0100 | [diff] [blame] | 26 | char *op_name_from_perf_id(void) |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 27 | { |
Matt Fleming | 5694633 | 2010-10-08 21:42:17 +0100 | [diff] [blame] | 28 | enum arm_perf_pmu_ids id = armpmu_get_pmu_id(); |
| 29 | |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 30 | switch (id) { |
| 31 | case ARM_PERF_PMU_ID_XSCALE1: |
| 32 | return "arm/xscale1"; |
| 33 | case ARM_PERF_PMU_ID_XSCALE2: |
| 34 | return "arm/xscale2"; |
| 35 | case ARM_PERF_PMU_ID_V6: |
| 36 | return "arm/armv6"; |
| 37 | case ARM_PERF_PMU_ID_V6MP: |
| 38 | return "arm/mpcore"; |
Neil Leeder | 1540f31 | 2011-08-31 10:04:23 -0400 | [diff] [blame] | 39 | case ARM_PERF_PMU_ID_CA5: |
| 40 | return "arm/armv7"; |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 41 | case ARM_PERF_PMU_ID_CA8: |
| 42 | return "arm/armv7"; |
| 43 | case ARM_PERF_PMU_ID_CA9: |
| 44 | return "arm/armv7-ca9"; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 45 | case ARM_PERF_PMU_ID_SCORPION: |
| 46 | return "arm/armv7-scorpion"; |
| 47 | case ARM_PERF_PMU_ID_SCORPIONMP: |
| 48 | return "arm/armv7-scorpionmp"; |
| 49 | case ARM_PERF_PMU_ID_KRAIT: |
| 50 | return "arm/armv7-krait"; |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 51 | default: |
| 52 | return NULL; |
| 53 | } |
| 54 | } |
Ari Kauppi | d14dd7e | 2011-01-20 13:57:19 -0500 | [diff] [blame] | 55 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 57 | static int report_trace(struct stackframe *frame, void *d) |
| 58 | { |
| 59 | unsigned int *depth = d; |
| 60 | |
| 61 | if (*depth) { |
| 62 | oprofile_add_trace(frame->pc); |
| 63 | (*depth)--; |
| 64 | } |
| 65 | |
| 66 | return *depth == 0; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * The registers we're interested in are at the end of the variable |
| 71 | * length saved register structure. The fp points at the end of this |
| 72 | * structure so the address of this struct is: |
| 73 | * (struct frame_tail *)(xxx->fp)-1 |
| 74 | */ |
| 75 | struct frame_tail { |
| 76 | struct frame_tail *fp; |
| 77 | unsigned long sp; |
| 78 | unsigned long lr; |
| 79 | } __attribute__((packed)); |
| 80 | |
| 81 | static struct frame_tail* user_backtrace(struct frame_tail *tail) |
| 82 | { |
| 83 | struct frame_tail buftail[2]; |
| 84 | |
| 85 | /* Also check accessibility of one struct frame_tail beyond */ |
| 86 | if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) |
| 87 | return NULL; |
| 88 | if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail))) |
| 89 | return NULL; |
| 90 | |
| 91 | oprofile_add_trace(buftail[0].lr); |
| 92 | |
| 93 | /* frame pointers should strictly progress back up the stack |
| 94 | * (towards higher addresses) */ |
Rabin Vincent | cb06199 | 2011-02-09 11:35:12 +0100 | [diff] [blame] | 95 | if (tail + 1 >= buftail[0].fp) |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 96 | return NULL; |
| 97 | |
| 98 | return buftail[0].fp-1; |
| 99 | } |
| 100 | |
| 101 | static void arm_backtrace(struct pt_regs * const regs, unsigned int depth) |
| 102 | { |
| 103 | struct frame_tail *tail = ((struct frame_tail *) regs->ARM_fp) - 1; |
| 104 | |
| 105 | if (!user_mode(regs)) { |
| 106 | struct stackframe frame; |
| 107 | frame.fp = regs->ARM_fp; |
| 108 | frame.sp = regs->ARM_sp; |
| 109 | frame.lr = regs->ARM_lr; |
| 110 | frame.pc = regs->ARM_pc; |
| 111 | walk_stackframe(&frame, report_trace, &depth); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | while (depth-- && tail && !((unsigned long) tail & 3)) |
| 116 | tail = user_backtrace(tail); |
| 117 | } |
| 118 | |
Matt Fleming | 58850e2 | 2010-09-27 20:35:29 +0100 | [diff] [blame] | 119 | int __init oprofile_arch_init(struct oprofile_operations *ops) |
| 120 | { |
Ari Kauppi | d14dd7e | 2011-01-20 13:57:19 -0500 | [diff] [blame] | 121 | /* provide backtrace support also in timer mode: */ |
Matt Fleming | 58850e2 | 2010-09-27 20:35:29 +0100 | [diff] [blame] | 122 | ops->backtrace = arm_backtrace; |
| 123 | |
| 124 | return oprofile_perf_init(ops); |
| 125 | } |
| 126 | |
Vladimir Zapolskiy | 4347b83 | 2011-12-22 16:15:40 +0100 | [diff] [blame] | 127 | void oprofile_arch_exit(void) |
Matt Fleming | 58850e2 | 2010-09-27 20:35:29 +0100 | [diff] [blame] | 128 | { |
| 129 | oprofile_perf_exit(); |
| 130 | } |