2ef6c8b56311cb575bbfd0bd879486f08d85b365
[GitHub/moto-9609/android_kernel_motorola_exynos9610.git] / arch / x86 / oprofile / backtrace.c
1 /**
2 * @file backtrace.c
3 *
4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
6 *
7 * @author John Levon
8 * @author David Smith
9 */
10
11 #include <linux/oprofile.h>
12 #include <linux/sched.h>
13 #include <linux/mm.h>
14 #include <linux/compat.h>
15 #include <linux/uaccess.h>
16
17 #include <asm/ptrace.h>
18 #include <asm/stacktrace.h>
19
20 static int backtrace_stack(void *data, char *name)
21 {
22 /* Yes, we want all stacks */
23 return 0;
24 }
25
26 static int backtrace_address(void *data, unsigned long addr, int reliable)
27 {
28 unsigned int *depth = data;
29
30 if ((*depth)--)
31 oprofile_add_trace(addr);
32 return 0;
33 }
34
35 static struct stacktrace_ops backtrace_ops = {
36 .stack = backtrace_stack,
37 .address = backtrace_address,
38 .walk_stack = print_context_stack,
39 };
40
41 #ifdef CONFIG_COMPAT
42 static struct stack_frame_ia32 *
43 dump_user_backtrace_32(struct stack_frame_ia32 *head)
44 {
45 /* Also check accessibility of one struct frame_head beyond: */
46 struct stack_frame_ia32 bufhead[2];
47 struct stack_frame_ia32 *fp;
48 unsigned long bytes;
49
50 bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
51 if (bytes != 0)
52 return NULL;
53
54 fp = (struct stack_frame_ia32 *) compat_ptr(bufhead[0].next_frame);
55
56 oprofile_add_trace(bufhead[0].return_address);
57
58 /* frame pointers should strictly progress back up the stack
59 * (towards higher addresses) */
60 if (head >= fp)
61 return NULL;
62
63 return fp;
64 }
65
66 static inline int
67 x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
68 {
69 struct stack_frame_ia32 *head;
70
71 /* User process is IA32 */
72 if (!current || !test_thread_flag(TIF_IA32))
73 return 0;
74
75 head = (struct stack_frame_ia32 *) regs->bp;
76 while (depth-- && head)
77 head = dump_user_backtrace_32(head);
78
79 return 1;
80 }
81
82 #else
83 static inline int
84 x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
85 {
86 return 0;
87 }
88 #endif /* CONFIG_COMPAT */
89
90 static struct stack_frame *dump_user_backtrace(struct stack_frame *head)
91 {
92 /* Also check accessibility of one struct frame_head beyond: */
93 struct stack_frame bufhead[2];
94 unsigned long bytes;
95
96 bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
97 if (bytes != 0)
98 return NULL;
99
100 oprofile_add_trace(bufhead[0].return_address);
101
102 /* frame pointers should strictly progress back up the stack
103 * (towards higher addresses) */
104 if (head >= bufhead[0].next_frame)
105 return NULL;
106
107 return bufhead[0].next_frame;
108 }
109
110 void
111 x86_backtrace(struct pt_regs * const regs, unsigned int depth)
112 {
113 struct stack_frame *head = (struct stack_frame *)frame_pointer(regs);
114
115 if (!user_mode(regs)) {
116 unsigned long stack = kernel_stack_pointer(regs);
117
118 if (!depth)
119 return;
120
121 oprofile_add_trace(regs->ip);
122 if (!--depth)
123 return;
124
125 dump_trace(NULL, regs, (unsigned long *)stack, 0,
126 &backtrace_ops, &depth);
127 return;
128 }
129
130 if (x86_backtrace_32(regs, depth))
131 return;
132
133 while (depth-- && head)
134 head = dump_user_backtrace(head);
135 }