common.c 3.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/**
 * @file common.c
 *
 * @remark Copyright 2004 Oprofile Authors
5
 * @remark Copyright 2010 ARM Ltd.
L
Linus Torvalds 已提交
6 7 8
 * @remark Read the file COPYING
 *
 * @author Zwane Mwaikambo
9
 * @author Will Deacon [move to perf]
L
Linus Torvalds 已提交
10 11
 */

12
#include <linux/cpumask.h>
L
Linus Torvalds 已提交
13
#include <linux/init.h>
14
#include <linux/mutex.h>
L
Linus Torvalds 已提交
15
#include <linux/oprofile.h>
16
#include <linux/perf_event.h>
17
#include <linux/platform_device.h>
18
#include <linux/slab.h>
19 20 21 22 23
#include <asm/stacktrace.h>
#include <linux/uaccess.h>

#include <asm/perf_event.h>
#include <asm/ptrace.h>
L
Linus Torvalds 已提交
24

25
#ifdef CONFIG_HW_PERF_EVENTS
26 27 28 29 30 31 32 33 34 35

/*
 * OProfile has a curious naming scheme for the ARM PMUs, but they are
 * part of the user ABI so we need to map from the perf PMU name for
 * supported PMUs.
 */
static struct op_perf_name {
	char *perf_name;
	char *op_name;
} op_perf_name_map[] = {
M
Mark Rutland 已提交
36 37 38 39 40 41 42 43
	{ "armv5_xscale1",	"arm/xscale1"	},
	{ "armv5_xscale2",	"arm/xscale2"	},
	{ "armv6_1136",		"arm/armv6"	},
	{ "armv6_1156",		"arm/armv6"	},
	{ "armv6_1176",		"arm/armv6"	},
	{ "armv6_11mpcore",	"arm/mpcore"	},
	{ "armv7_cortex_a8",	"arm/armv7"	},
	{ "armv7_cortex_a9",	"arm/armv7-ca9"	},
44 45
};

46
char *op_name_from_perf_id(void)
47
{
48 49 50
	int i;
	struct op_perf_name names;
	const char *perf_name = perf_pmu_name();
51

52 53 54 55
	for (i = 0; i < ARRAY_SIZE(op_perf_name_map); ++i) {
		names = op_perf_name_map[i];
		if (!strcmp(names.perf_name, perf_name))
			return names.op_name;
56
	}
57 58

	return NULL;
59
}
60
#endif
L
Linus Torvalds 已提交
61

62
static int report_trace(struct stackframe *frame, void *d)
L
Linus Torvalds 已提交
63
{
64
	unsigned int *depth = d;
65

66 67 68 69
	if (*depth) {
		oprofile_add_trace(frame->pc);
		(*depth)--;
	}
70

71 72
	return *depth == 0;
}
73

74 75 76 77 78 79 80 81 82 83 84
/*
 * The registers we're interested in are at the end of the variable
 * length saved register structure. The fp points at the end of this
 * structure so the address of this struct is:
 * (struct frame_tail *)(xxx->fp)-1
 */
struct frame_tail {
	struct frame_tail *fp;
	unsigned long sp;
	unsigned long lr;
} __attribute__((packed));
85

86 87 88
static struct frame_tail* user_backtrace(struct frame_tail *tail)
{
	struct frame_tail buftail[2];
89

90 91 92 93 94
	/* Also check accessibility of one struct frame_tail beyond */
	if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
		return NULL;
	if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail)))
		return NULL;
95

96
	oprofile_add_trace(buftail[0].lr);
97

98 99
	/* frame pointers should strictly progress back up the stack
	 * (towards higher addresses) */
100
	if (tail + 1 >= buftail[0].fp)
101 102 103 104 105 106 107 108 109 110 111
		return NULL;

	return buftail[0].fp-1;
}

static void arm_backtrace(struct pt_regs * const regs, unsigned int depth)
{
	struct frame_tail *tail = ((struct frame_tail *) regs->ARM_fp) - 1;

	if (!user_mode(regs)) {
		struct stackframe frame;
112
		arm_get_current_stackframe(regs, &frame);
113 114 115 116 117 118 119 120
		walk_stackframe(&frame, report_trace, &depth);
		return;
	}

	while (depth-- && tail && !((unsigned long) tail & 3))
		tail = user_backtrace(tail);
}

121 122
int __init oprofile_arch_init(struct oprofile_operations *ops)
{
123
	/* provide backtrace support also in timer mode: */
124 125 126 127 128
	ops->backtrace		= arm_backtrace;

	return oprofile_perf_init(ops);
}

129
void oprofile_arch_exit(void)
130 131 132
{
	oprofile_perf_exit();
}