4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
11 #include <linux/oprofile.h>
12 #include <linux/sched.h>
14 #include <asm/ptrace.h>
15 #include <asm/uaccess.h>
18 struct frame_head
* ebp
;
20 } __attribute__((packed
));
22 static struct frame_head
*
23 dump_kernel_backtrace(struct frame_head
* head
)
25 oprofile_add_trace(head
->ret
);
27 /* frame pointers should strictly progress back up the stack
28 * (towards higher addresses) */
29 if (head
>= head
->ebp
)
35 static struct frame_head
*
36 dump_user_backtrace(struct frame_head
* head
)
38 struct frame_head bufhead
[2];
40 /* Also check accessibility of one struct frame_head beyond */
41 if (!access_ok(VERIFY_READ
, head
, sizeof(bufhead
)))
43 if (__copy_from_user_inatomic(bufhead
, head
, sizeof(bufhead
)))
46 oprofile_add_trace(bufhead
[0].ret
);
48 /* frame pointers should strictly progress back up the stack
49 * (towards higher addresses) */
50 if (head
>= bufhead
[0].ebp
)
53 return bufhead
[0].ebp
;
57 * | | /\ Higher addresses
59 * --------------- stack base (address of current_thread_info)
63 * --------------- saved regs->ebp value if valid (frame_head address)
65 * --------------- saved regs->rsp value if x86_64
67 * --------------- struct pt_regs * stored on stack if 32-bit
71 * --------------- %esp
73 * | | \/ Lower addresses
75 * Thus, regs (or regs->rsp for x86_64) <-> stack base restricts the
76 * valid(ish) ebp values. Note: (1) for x86_64, NMI and several other
77 * exceptions use special stacks, maintained by the interrupt stack table
78 * (IST). These stacks are set up in trap_init() in
79 * arch/x86_64/kernel/traps.c. Thus, for x86_64, regs now does not point
80 * to the kernel stack; instead, it points to some location on the NMI
81 * stack. On the other hand, regs->rsp is the stack pointer saved when the
82 * NMI occurred. (2) For 32-bit, regs->esp is not valid because the
83 * processor does not save %esp on the kernel stack when interrupts occur
86 #ifdef CONFIG_FRAME_POINTER
87 static int valid_kernel_stack(struct frame_head
* head
, struct pt_regs
* regs
)
89 unsigned long headaddr
= (unsigned long)head
;
91 unsigned long stack
= (unsigned long)regs
->rsp
;
93 unsigned long stack
= (unsigned long)regs
;
95 unsigned long stack_base
= (stack
& ~(THREAD_SIZE
- 1)) + THREAD_SIZE
;
97 return headaddr
> stack
&& headaddr
< stack_base
;
100 /* without fp, it's just junk */
101 static int valid_kernel_stack(struct frame_head
* head
, struct pt_regs
* regs
)
109 x86_backtrace(struct pt_regs
* const regs
, unsigned int depth
)
111 struct frame_head
*head
;
114 head
= (struct frame_head
*)regs
->rbp
;
116 head
= (struct frame_head
*)regs
->ebp
;
119 if (!user_mode_vm(regs
)) {
120 while (depth
-- && valid_kernel_stack(head
, regs
))
121 head
= dump_kernel_backtrace(head
);
125 while (depth
-- && head
)
126 head
= dump_user_backtrace(head
);