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>
17 struct frame_head * ebp;
19 } __attribute__((packed));
22 static struct frame_head *
23 dump_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)
37 /* With a 4G kernel/user split, user pages are not directly
38 * accessible from the kernel, so don't try
40 static int pages_present(struct frame_head * head)
45 /* check that the page(s) containing the frame head are present */
46 static int pages_present(struct frame_head * head)
48 struct mm_struct * mm = current->mm;
50 /* FIXME: only necessary once per page */
51 if (!check_user_page_readable(mm, (unsigned long)head))
54 return check_user_page_readable(mm, (unsigned long)(head + 1));
56 #endif /* CONFIG_X86_4G */
60 * | | /\ Higher addresses
62 * --------------- stack base (address of current_thread_info)
66 * --------------- saved regs->ebp value if valid (frame_head address)
68 * --------------- struct pt_regs stored on stack (struct pt_regs *)
72 * --------------- %esp
74 * | | \/ Lower addresses
76 * Thus, &pt_regs <-> stack base restricts the valid(ish) ebp values
78 #ifdef CONFIG_FRAME_POINTER
79 static int valid_kernel_stack(struct frame_head * head, struct pt_regs * regs)
81 unsigned long headaddr = (unsigned long)head;
82 unsigned long stack = (unsigned long)regs;
83 unsigned long stack_base = (stack & ~(THREAD_SIZE - 1)) + THREAD_SIZE;
85 return headaddr > stack && headaddr < stack_base;
88 /* without fp, it's just junk */
89 static int valid_kernel_stack(struct frame_head * head, struct pt_regs * regs)
97 x86_backtrace(struct pt_regs * const regs, unsigned int depth)
99 struct frame_head *head;
102 head = (struct frame_head *)regs->rbp;
104 head = (struct frame_head *)regs->ebp;
107 if (!user_mode(regs)) {
108 while (depth-- && valid_kernel_stack(head, regs))
109 head = dump_backtrace(head);
114 if (!spin_trylock(¤t->mm->page_table_lock))
118 while (depth-- && head && pages_present(head))
119 head = dump_backtrace(head);
122 spin_unlock(¤t->mm->page_table_lock);