Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.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 void backtrace_warning_symbol(void *data, char *msg,
21                                      unsigned long symbol)
22 {
23         /* Ignore warnings */
24 }
25
26 static void backtrace_warning(void *data, char *msg)
27 {
28         /* Ignore warnings */
29 }
30
31 static int backtrace_stack(void *data, char *name)
32 {
33         /* Yes, we want all stacks */
34         return 0;
35 }
36
37 static void backtrace_address(void *data, unsigned long addr, int reliable)
38 {
39         unsigned int *depth = data;
40
41         if ((*depth)--)
42                 oprofile_add_trace(addr);
43 }
44
45 static struct stacktrace_ops backtrace_ops = {
46         .warning        = backtrace_warning,
47         .warning_symbol = backtrace_warning_symbol,
48         .stack          = backtrace_stack,
49         .address        = backtrace_address,
50         .walk_stack     = print_context_stack,
51 };
52
53 #ifdef CONFIG_COMPAT
54 static struct stack_frame_ia32 *
55 dump_user_backtrace_32(struct stack_frame_ia32 *head)
56 {
57         /* Also check accessibility of one struct frame_head beyond: */
58         struct stack_frame_ia32 bufhead[2];
59         struct stack_frame_ia32 *fp;
60         unsigned long bytes;
61
62         bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
63         if (bytes != sizeof(bufhead))
64                 return NULL;
65
66         fp = (struct stack_frame_ia32 *) compat_ptr(bufhead[0].next_frame);
67
68         oprofile_add_trace(bufhead[0].return_address);
69
70         /* frame pointers should strictly progress back up the stack
71         * (towards higher addresses) */
72         if (head >= fp)
73                 return NULL;
74
75         return fp;
76 }
77
78 static inline int
79 x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
80 {
81         struct stack_frame_ia32 *head;
82
83         /* User process is IA32 */
84         if (!current || !test_thread_flag(TIF_IA32))
85                 return 0;
86
87         head = (struct stack_frame_ia32 *) regs->bp;
88         while (depth-- && head)
89                 head = dump_user_backtrace_32(head);
90
91         return 1;
92 }
93
94 #else
95 static inline int
96 x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
97 {
98         return 0;
99 }
100 #endif /* CONFIG_COMPAT */
101
102 static struct stack_frame *dump_user_backtrace(struct stack_frame *head)
103 {
104         /* Also check accessibility of one struct frame_head beyond: */
105         struct stack_frame bufhead[2];
106         unsigned long bytes;
107
108         bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
109         if (bytes != sizeof(bufhead))
110                 return NULL;
111
112         oprofile_add_trace(bufhead[0].return_address);
113
114         /* frame pointers should strictly progress back up the stack
115          * (towards higher addresses) */
116         if (head >= bufhead[0].next_frame)
117                 return NULL;
118
119         return bufhead[0].next_frame;
120 }
121
122 void
123 x86_backtrace(struct pt_regs * const regs, unsigned int depth)
124 {
125         struct stack_frame *head = (struct stack_frame *)frame_pointer(regs);
126
127         if (!user_mode_vm(regs)) {
128                 unsigned long stack = kernel_stack_pointer(regs);
129                 if (depth)
130                         dump_trace(NULL, regs, (unsigned long *)stack, 0,
131                                    &backtrace_ops, &depth);
132                 return;
133         }
134
135         if (x86_backtrace_32(regs, depth))
136                 return;
137
138         while (depth-- && head)
139                 head = dump_user_backtrace(head);
140 }