4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
11 #include <linux/oprofile.h>
12 #include <linux/sched.h>
14 #include <linux/compat.h>
15 #include <linux/highmem.h>
17 #include <asm/ptrace.h>
18 #include <asm/uaccess.h>
19 #include <asm/stacktrace.h>
21 static int backtrace_stack(void *data
, char *name
)
23 /* Yes, we want all stacks */
27 static void backtrace_address(void *data
, unsigned long addr
, int reliable
)
29 unsigned int *depth
= data
;
32 oprofile_add_trace(addr
);
35 static struct stacktrace_ops backtrace_ops
= {
36 .stack
= backtrace_stack
,
37 .address
= backtrace_address
,
38 .walk_stack
= print_context_stack
,
41 /* from arch/x86/kernel/cpu/perf_event.c: */
44 * best effort, GUP based copy_from_user() that assumes IRQ or NMI context
47 copy_from_user_nmi(void *to
, const void __user
*from
, unsigned long n
)
49 unsigned long offset
, addr
= (unsigned long)from
;
50 unsigned long size
, len
= 0;
56 ret
= __get_user_pages_fast(addr
, 1, 0, &page
);
60 offset
= addr
& (PAGE_SIZE
- 1);
61 size
= min(PAGE_SIZE
- offset
, n
- len
);
63 map
= kmap_atomic(page
);
64 memcpy(to
, map
+offset
, size
);
78 static struct stack_frame_ia32
*
79 dump_user_backtrace_32(struct stack_frame_ia32
*head
)
81 /* Also check accessibility of one struct frame_head beyond: */
82 struct stack_frame_ia32 bufhead
[2];
83 struct stack_frame_ia32
*fp
;
86 bytes
= copy_from_user_nmi(bufhead
, head
, sizeof(bufhead
));
87 if (bytes
!= sizeof(bufhead
))
90 fp
= (struct stack_frame_ia32
*) compat_ptr(bufhead
[0].next_frame
);
92 oprofile_add_trace(bufhead
[0].return_address
);
94 /* frame pointers should strictly progress back up the stack
95 * (towards higher addresses) */
103 x86_backtrace_32(struct pt_regs
* const regs
, unsigned int depth
)
105 struct stack_frame_ia32
*head
;
107 /* User process is 32-bit */
108 if (!current
|| !test_thread_flag(TIF_IA32
))
111 head
= (struct stack_frame_ia32
*) regs
->bp
;
112 while (depth
-- && head
)
113 head
= dump_user_backtrace_32(head
);
120 x86_backtrace_32(struct pt_regs
* const regs
, unsigned int depth
)
124 #endif /* CONFIG_COMPAT */
126 static struct stack_frame
*dump_user_backtrace(struct stack_frame
*head
)
128 /* Also check accessibility of one struct frame_head beyond: */
129 struct stack_frame bufhead
[2];
132 bytes
= copy_from_user_nmi(bufhead
, head
, sizeof(bufhead
));
133 if (bytes
!= sizeof(bufhead
))
136 oprofile_add_trace(bufhead
[0].return_address
);
138 /* frame pointers should strictly progress back up the stack
139 * (towards higher addresses) */
140 if (head
>= bufhead
[0].next_frame
)
143 return bufhead
[0].next_frame
;
147 x86_backtrace(struct pt_regs
* const regs
, unsigned int depth
)
149 struct stack_frame
*head
= (struct stack_frame
*)frame_pointer(regs
);
151 if (!user_mode_vm(regs
)) {
152 unsigned long stack
= kernel_stack_pointer(regs
);
154 dump_trace(NULL
, regs
, (unsigned long *)stack
, 0,
155 &backtrace_ops
, &depth
);
159 if (x86_backtrace_32(regs
, depth
))
162 while (depth
-- && head
)
163 head
= dump_user_backtrace(head
);