This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / i386 / 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 <asm/ptrace.h>
15
16 struct frame_head {
17         struct frame_head * ebp;
18         unsigned long ret;
19 } __attribute__((packed));
20
21
22 static struct frame_head *
23 dump_backtrace(struct frame_head * head)
24 {
25         oprofile_add_trace(head->ret);
26
27         /* frame pointers should strictly progress back up the stack
28          * (towards higher addresses) */
29         if (head >= head->ebp)
30                 return 0;
31
32         return head->ebp;
33 }
34
35
36 #ifdef CONFIG_X86_4G
37 /* With a 4G kernel/user split, user pages are not directly
38  * accessible from the kernel, so don't try
39  */
40 static int pages_present(struct frame_head * head)
41 {
42         return 0;
43 }
44 #else
45 /* check that the page(s) containing the frame head are present */
46 static int pages_present(struct frame_head * head)
47 {
48         struct mm_struct * mm = current->mm;
49
50         /* FIXME: only necessary once per page */
51         if (!check_user_page_readable(mm, (unsigned long)head))
52                 return 0;
53
54         return check_user_page_readable(mm, (unsigned long)(head + 1));
55 }
56 #endif /* CONFIG_X86_4G */
57
58
59 /*
60  * |             | /\ Higher addresses
61  * |             |
62  * --------------- stack base (address of current_thread_info)
63  * | thread info |
64  * .             .
65  * |    stack    |
66  * --------------- saved regs->ebp value if valid (frame_head address)
67  * .             .
68  * --------------- struct pt_regs stored on stack (struct pt_regs *)
69  * |             |
70  * .             .
71  * |             |
72  * --------------- %esp
73  * |             |
74  * |             | \/ Lower addresses
75  *
76  * Thus, &pt_regs <-> stack base restricts the valid(ish) ebp values
77  */
78 #ifdef CONFIG_FRAME_POINTER
79 static int valid_kernel_stack(struct frame_head * head, struct pt_regs * regs)
80 {
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;
84
85         return headaddr > stack && headaddr < stack_base;
86 }
87 #else
88 /* without fp, it's just junk */
89 static int valid_kernel_stack(struct frame_head * head, struct pt_regs * regs)
90 {
91         return 0;
92 }
93 #endif
94
95
96 void
97 x86_backtrace(struct pt_regs * const regs, unsigned int depth)
98 {
99         struct frame_head *head;
100
101 #ifdef CONFIG_X86_64
102         head = (struct frame_head *)regs->rbp;
103 #else
104         head = (struct frame_head *)regs->ebp;
105 #endif
106
107         if (!user_mode(regs)) {
108                 while (depth-- && valid_kernel_stack(head, regs))
109                         head = dump_backtrace(head);
110                 return;
111         }
112
113 #ifdef CONFIG_SMP
114         if (!spin_trylock(&current->mm->page_table_lock))
115                 return;
116 #endif
117
118         while (depth-- && head && pages_present(head))
119                 head = dump_backtrace(head);
120
121 #ifdef CONFIG_SMP
122         spin_unlock(&current->mm->page_table_lock);
123 #endif
124 }