blob: afd7afba96ec53c965140c5c8c7a359a75951671 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
2 * @file common.c
3 *
4 * @remark Copyright 2004 Oprofile Authors
Will Deacon8c1fc962010-04-30 11:36:54 +01005 * @remark Copyright 2010 ARM Ltd.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * @remark Read the file COPYING
7 *
8 * @author Zwane Mwaikambo
Will Deacon8c1fc962010-04-30 11:36:54 +01009 * @author Will Deacon [move to perf]
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Will Deacon8c1fc962010-04-30 11:36:54 +010012#include <linux/cpumask.h>
Will Deacon8c1fc962010-04-30 11:36:54 +010013#include <linux/init.h>
14#include <linux/mutex.h>
15#include <linux/oprofile.h>
16#include <linux/perf_event.h>
Will Deacond1e86d62010-04-30 11:38:39 +010017#include <linux/platform_device.h>
Russell Kingae92dc92006-03-16 11:32:51 +000018#include <linux/slab.h>
Will Deacon8c1fc962010-04-30 11:36:54 +010019#include <asm/stacktrace.h>
20#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Will Deacon8c1fc962010-04-30 11:36:54 +010022#include <asm/perf_event.h>
23#include <asm/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Will Deacon8c1fc962010-04-30 11:36:54 +010025#ifdef CONFIG_HW_PERF_EVENTS
Matt Fleming56946332010-10-08 21:42:17 +010026char *op_name_from_perf_id(void)
Will Deacon8c1fc962010-04-30 11:36:54 +010027{
Matt Fleming56946332010-10-08 21:42:17 +010028 enum arm_perf_pmu_ids id = armpmu_get_pmu_id();
29
Will Deacon8c1fc962010-04-30 11:36:54 +010030 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";
39 case ARM_PERF_PMU_ID_CA8:
40 return "arm/armv7";
41 case ARM_PERF_PMU_ID_CA9:
42 return "arm/armv7-ca9";
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070043 case ARM_PERF_PMU_ID_SCORPION:
44 return "arm/armv7-scorpion";
45 case ARM_PERF_PMU_ID_SCORPIONMP:
46 return "arm/armv7-scorpionmp";
47 case ARM_PERF_PMU_ID_KRAIT:
48 return "arm/armv7-krait";
Will Deacon8c1fc962010-04-30 11:36:54 +010049 default:
50 return NULL;
51 }
52}
Ari Kauppid14dd7e2011-01-20 13:57:19 -050053#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Will Deacon8c1fc962010-04-30 11:36:54 +010055static int report_trace(struct stackframe *frame, void *d)
56{
57 unsigned int *depth = d;
58
59 if (*depth) {
60 oprofile_add_trace(frame->pc);
61 (*depth)--;
62 }
63
64 return *depth == 0;
65}
66
67/*
68 * The registers we're interested in are at the end of the variable
69 * length saved register structure. The fp points at the end of this
70 * structure so the address of this struct is:
71 * (struct frame_tail *)(xxx->fp)-1
72 */
73struct frame_tail {
74 struct frame_tail *fp;
75 unsigned long sp;
76 unsigned long lr;
77} __attribute__((packed));
78
79static struct frame_tail* user_backtrace(struct frame_tail *tail)
80{
81 struct frame_tail buftail[2];
82
83 /* Also check accessibility of one struct frame_tail beyond */
84 if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
85 return NULL;
86 if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail)))
87 return NULL;
88
89 oprofile_add_trace(buftail[0].lr);
90
91 /* frame pointers should strictly progress back up the stack
92 * (towards higher addresses) */
Rabin Vincentcb061992011-02-09 11:35:12 +010093 if (tail + 1 >= buftail[0].fp)
Will Deacon8c1fc962010-04-30 11:36:54 +010094 return NULL;
95
96 return buftail[0].fp-1;
97}
98
99static void arm_backtrace(struct pt_regs * const regs, unsigned int depth)
100{
101 struct frame_tail *tail = ((struct frame_tail *) regs->ARM_fp) - 1;
102
103 if (!user_mode(regs)) {
104 struct stackframe frame;
105 frame.fp = regs->ARM_fp;
106 frame.sp = regs->ARM_sp;
107 frame.lr = regs->ARM_lr;
108 frame.pc = regs->ARM_pc;
109 walk_stackframe(&frame, report_trace, &depth);
110 return;
111 }
112
113 while (depth-- && tail && !((unsigned long) tail & 3))
114 tail = user_backtrace(tail);
115}
116
Matt Fleming58850e22010-09-27 20:35:29 +0100117int __init oprofile_arch_init(struct oprofile_operations *ops)
118{
Ari Kauppid14dd7e2011-01-20 13:57:19 -0500119 /* provide backtrace support also in timer mode: */
Matt Fleming58850e22010-09-27 20:35:29 +0100120 ops->backtrace = arm_backtrace;
121
122 return oprofile_perf_init(ops);
123}
124
Matt Fleming58850e22010-09-27 20:35:29 +0100125void __exit oprofile_arch_exit(void)
126{
127 oprofile_perf_exit();
128}