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