0001
0002
0003
0004
0005
0006
0007 #include <linux/kernel.h>
0008 #include <linux/sched.h>
0009 #include <linux/perf_event.h>
0010 #include <linux/percpu.h>
0011 #include <linux/uaccess.h>
0012 #include <linux/mm.h>
0013 #include <asm/ptrace.h>
0014 #include <asm/sigcontext.h>
0015 #include <asm/ucontext.h>
0016 #include <asm/vdso.h>
0017 #include <asm/pte-walk.h>
0018
0019 #include "callchain.h"
0020
0021
0022
0023
0024
0025
0026 static int valid_next_sp(unsigned long sp, unsigned long prev_sp)
0027 {
0028 if (sp & 0xf)
0029 return 0;
0030 if (!validate_sp(sp, current, STACK_FRAME_OVERHEAD))
0031 return 0;
0032 if (sp >= prev_sp + STACK_FRAME_MIN_SIZE)
0033 return 1;
0034
0035
0036
0037
0038 if ((sp & ~(THREAD_SIZE - 1)) != (prev_sp & ~(THREAD_SIZE - 1)))
0039 return 1;
0040 return 0;
0041 }
0042
0043 void __no_sanitize_address
0044 perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
0045 {
0046 unsigned long sp, next_sp;
0047 unsigned long next_ip;
0048 unsigned long lr;
0049 long level = 0;
0050 unsigned long *fp;
0051
0052 lr = regs->link;
0053 sp = regs->gpr[1];
0054 perf_callchain_store(entry, perf_instruction_pointer(regs));
0055
0056 if (!validate_sp(sp, current, STACK_FRAME_OVERHEAD))
0057 return;
0058
0059 for (;;) {
0060 fp = (unsigned long *) sp;
0061 next_sp = fp[0];
0062
0063 if (next_sp == sp + STACK_INT_FRAME_SIZE &&
0064 fp[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
0065
0066
0067
0068
0069 regs = (struct pt_regs *)(sp + STACK_FRAME_OVERHEAD);
0070 next_ip = regs->nip;
0071 lr = regs->link;
0072 level = 0;
0073 perf_callchain_store_context(entry, PERF_CONTEXT_KERNEL);
0074
0075 } else {
0076 if (level == 0)
0077 next_ip = lr;
0078 else
0079 next_ip = fp[STACK_FRAME_LR_SAVE];
0080
0081
0082
0083
0084
0085
0086
0087
0088 if ((level == 1 && next_ip == lr) ||
0089 (level <= 1 && !kernel_text_address(next_ip)))
0090 next_ip = 0;
0091
0092 ++level;
0093 }
0094
0095 perf_callchain_store(entry, next_ip);
0096 if (!valid_next_sp(next_sp, sp))
0097 return;
0098 sp = next_sp;
0099 }
0100 }
0101
0102 void
0103 perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
0104 {
0105 if (!is_32bit_task())
0106 perf_callchain_user_64(entry, regs);
0107 else
0108 perf_callchain_user_32(entry, regs);
0109 }