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 | d1e86d6 | 2010-04-30 11:38:39 +0100 | [diff] [blame] | 13 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/errno.h> |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 15 | #include <linux/init.h> |
| 16 | #include <linux/mutex.h> |
| 17 | #include <linux/oprofile.h> |
| 18 | #include <linux/perf_event.h> |
Will Deacon | d1e86d6 | 2010-04-30 11:38:39 +0100 | [diff] [blame] | 19 | #include <linux/platform_device.h> |
Russell King | ae92dc9 | 2006-03-16 11:32:51 +0000 | [diff] [blame] | 20 | #include <linux/slab.h> |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 21 | #include <asm/stacktrace.h> |
| 22 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 24 | #include <asm/perf_event.h> |
| 25 | #include <asm/ptrace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 27 | #ifdef CONFIG_HW_PERF_EVENTS |
Matt Fleming | 5694633 | 2010-10-08 21:42:17 +0100 | [diff] [blame] | 28 | char *op_name_from_perf_id(void) |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 29 | { |
Matt Fleming | 5694633 | 2010-10-08 21:42:17 +0100 | [diff] [blame] | 30 | enum arm_perf_pmu_ids id = armpmu_get_pmu_id(); |
| 31 | |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 32 | switch (id) { |
| 33 | case ARM_PERF_PMU_ID_XSCALE1: |
| 34 | return "arm/xscale1"; |
| 35 | case ARM_PERF_PMU_ID_XSCALE2: |
| 36 | return "arm/xscale2"; |
| 37 | case ARM_PERF_PMU_ID_V6: |
| 38 | return "arm/armv6"; |
| 39 | case ARM_PERF_PMU_ID_V6MP: |
| 40 | return "arm/mpcore"; |
| 41 | case ARM_PERF_PMU_ID_CA8: |
| 42 | return "arm/armv7"; |
| 43 | case ARM_PERF_PMU_ID_CA9: |
| 44 | return "arm/armv7-ca9"; |
| 45 | default: |
| 46 | return NULL; |
| 47 | } |
| 48 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 50 | static int report_trace(struct stackframe *frame, void *d) |
| 51 | { |
| 52 | unsigned int *depth = d; |
| 53 | |
| 54 | if (*depth) { |
| 55 | oprofile_add_trace(frame->pc); |
| 56 | (*depth)--; |
| 57 | } |
| 58 | |
| 59 | return *depth == 0; |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * The registers we're interested in are at the end of the variable |
| 64 | * length saved register structure. The fp points at the end of this |
| 65 | * structure so the address of this struct is: |
| 66 | * (struct frame_tail *)(xxx->fp)-1 |
| 67 | */ |
| 68 | struct frame_tail { |
| 69 | struct frame_tail *fp; |
| 70 | unsigned long sp; |
| 71 | unsigned long lr; |
| 72 | } __attribute__((packed)); |
| 73 | |
| 74 | static struct frame_tail* user_backtrace(struct frame_tail *tail) |
| 75 | { |
| 76 | struct frame_tail buftail[2]; |
| 77 | |
| 78 | /* Also check accessibility of one struct frame_tail beyond */ |
| 79 | if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) |
| 80 | return NULL; |
| 81 | if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail))) |
| 82 | return NULL; |
| 83 | |
| 84 | oprofile_add_trace(buftail[0].lr); |
| 85 | |
| 86 | /* frame pointers should strictly progress back up the stack |
| 87 | * (towards higher addresses) */ |
| 88 | if (tail >= buftail[0].fp) |
| 89 | return NULL; |
| 90 | |
| 91 | return buftail[0].fp-1; |
| 92 | } |
| 93 | |
| 94 | static void arm_backtrace(struct pt_regs * const regs, unsigned int depth) |
| 95 | { |
| 96 | struct frame_tail *tail = ((struct frame_tail *) regs->ARM_fp) - 1; |
| 97 | |
| 98 | if (!user_mode(regs)) { |
| 99 | struct stackframe frame; |
| 100 | frame.fp = regs->ARM_fp; |
| 101 | frame.sp = regs->ARM_sp; |
| 102 | frame.lr = regs->ARM_lr; |
| 103 | frame.pc = regs->ARM_pc; |
| 104 | walk_stackframe(&frame, report_trace, &depth); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | while (depth-- && tail && !((unsigned long) tail & 3)) |
| 109 | tail = user_backtrace(tail); |
| 110 | } |
| 111 | |
Matt Fleming | 58850e2 | 2010-09-27 20:35:29 +0100 | [diff] [blame] | 112 | int __init oprofile_arch_init(struct oprofile_operations *ops) |
| 113 | { |
| 114 | ops->backtrace = arm_backtrace; |
| 115 | |
| 116 | return oprofile_perf_init(ops); |
| 117 | } |
| 118 | |
Matt Fleming | 58850e2 | 2010-09-27 20:35:29 +0100 | [diff] [blame] | 119 | void __exit oprofile_arch_exit(void) |
| 120 | { |
| 121 | oprofile_perf_exit(); |
| 122 | } |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 123 | #else |
| 124 | int __init oprofile_arch_init(struct oprofile_operations *ops) |
| 125 | { |
| 126 | pr_info("oprofile: hardware counters not available\n"); |
| 127 | return -ENODEV; |
| 128 | } |
Will Deacon | c7fd239 | 2010-08-29 14:52:00 -0400 | [diff] [blame] | 129 | void __exit oprofile_arch_exit(void) {} |
Will Deacon | 8c1fc96 | 2010-04-30 11:36:54 +0100 | [diff] [blame] | 130 | #endif /* CONFIG_HW_PERF_EVENTS */ |