dumpstack: x86: add "end" parameter to valid_stack_ptr and print_context_stack
[linux-2.6] / arch / x86 / kernel / dumpstack_32.c
1 /*
2  *  Copyright (C) 1991, 1992  Linus Torvalds
3  *  Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
4  */
5 #include <linux/kallsyms.h>
6 #include <linux/kprobes.h>
7 #include <linux/uaccess.h>
8 #include <linux/utsname.h>
9 #include <linux/hardirq.h>
10 #include <linux/kdebug.h>
11 #include <linux/module.h>
12 #include <linux/ptrace.h>
13 #include <linux/kexec.h>
14 #include <linux/bug.h>
15 #include <linux/nmi.h>
16
17 #include <asm/stacktrace.h>
18
19 int panic_on_unrecovered_nmi;
20 int kstack_depth_to_print = 24;
21 static unsigned int code_bytes = 64;
22 static int die_counter;
23
24 void printk_address(unsigned long address, int reliable)
25 {
26         printk(" [<%p>] %s%pS\n", (void *) address,
27                         reliable ? "" : "? ", (void *) address);
28 }
29
30 static inline int valid_stack_ptr(struct thread_info *tinfo,
31                         void *p, unsigned int size, void *end)
32 {
33         void *t = tinfo;
34         if (end) {
35                 if (p < end && p >= (end-THREAD_SIZE))
36                         return 1;
37                 else
38                         return 0;
39         }
40         return p > t && p < t + THREAD_SIZE - size;
41 }
42
43 /* The form of the top of the frame on the stack */
44 struct stack_frame {
45         struct stack_frame *next_frame;
46         unsigned long return_address;
47 };
48
49 static inline unsigned long
50 print_context_stack(struct thread_info *tinfo,
51                 unsigned long *stack, unsigned long bp,
52                 const struct stacktrace_ops *ops, void *data,
53                 unsigned long *end)
54 {
55         struct stack_frame *frame = (struct stack_frame *)bp;
56
57         while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
58                 unsigned long addr;
59
60                 addr = *stack;
61                 if (__kernel_text_address(addr)) {
62                         if ((unsigned long) stack == bp + sizeof(long)) {
63                                 ops->address(data, addr, 1);
64                                 frame = frame->next_frame;
65                                 bp = (unsigned long) frame;
66                         } else {
67                                 ops->address(data, addr, bp == 0);
68                         }
69                 }
70                 stack++;
71         }
72         return bp;
73 }
74
75 void dump_trace(struct task_struct *task, struct pt_regs *regs,
76                 unsigned long *stack, unsigned long bp,
77                 const struct stacktrace_ops *ops, void *data)
78 {
79         if (!task)
80                 task = current;
81
82         if (!stack) {
83                 unsigned long dummy;
84                 stack = &dummy;
85                 if (task != current)
86                         stack = (unsigned long *)task->thread.sp;
87         }
88
89 #ifdef CONFIG_FRAME_POINTER
90         if (!bp) {
91                 if (task == current) {
92                         /* Grab bp right from our regs */
93                         asm("movl %%ebp, %0" : "=r" (bp) :);
94                 } else {
95                         /* bp is the last reg pushed by switch_to */
96                         bp = *(unsigned long *) task->thread.sp;
97                 }
98         }
99 #endif
100
101         for (;;) {
102                 struct thread_info *context;
103
104                 context = (struct thread_info *)
105                         ((unsigned long)stack & (~(THREAD_SIZE - 1)));
106                 bp = print_context_stack(context, stack, bp, ops, data, NULL);
107                 /*
108                  * Should be after the line below, but somewhere
109                  * in early boot context comes out corrupted and we
110                  * can't reference it:
111                  */
112                 if (ops->stack(data, "IRQ") < 0)
113                         break;
114                 stack = (unsigned long *)context->previous_esp;
115                 if (!stack)
116                         break;
117                 touch_nmi_watchdog();
118         }
119 }
120 EXPORT_SYMBOL(dump_trace);
121
122 static void
123 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
124 {
125         printk(data);
126         print_symbol(msg, symbol);
127         printk("\n");
128 }
129
130 static void print_trace_warning(void *data, char *msg)
131 {
132         printk("%s%s\n", (char *)data, msg);
133 }
134
135 static int print_trace_stack(void *data, char *name)
136 {
137         return 0;
138 }
139
140 /*
141  * Print one address/symbol entries per line.
142  */
143 static void print_trace_address(void *data, unsigned long addr, int reliable)
144 {
145         printk("%s [<%08lx>] ", (char *)data, addr);
146         if (!reliable)
147                 printk("? ");
148         print_symbol("%s\n", addr);
149         touch_nmi_watchdog();
150 }
151
152 static const struct stacktrace_ops print_trace_ops = {
153         .warning = print_trace_warning,
154         .warning_symbol = print_trace_warning_symbol,
155         .stack = print_trace_stack,
156         .address = print_trace_address,
157 };
158
159 static void
160 show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
161                 unsigned long *stack, unsigned long bp, char *log_lvl)
162 {
163         dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
164         printk("%s =======================\n", log_lvl);
165 }
166
167 void show_trace(struct task_struct *task, struct pt_regs *regs,
168                 unsigned long *stack, unsigned long bp)
169 {
170         show_trace_log_lvl(task, regs, stack, bp, "");
171 }
172
173 static void
174 show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
175                    unsigned long *sp, unsigned long bp, char *log_lvl)
176 {
177         unsigned long *stack;
178         int i;
179
180         if (sp == NULL) {
181                 if (task)
182                         sp = (unsigned long *)task->thread.sp;
183                 else
184                         sp = (unsigned long *)&sp;
185         }
186
187         stack = sp;
188         for (i = 0; i < kstack_depth_to_print; i++) {
189                 if (kstack_end(stack))
190                         break;
191                 if (i && ((i % 8) == 0))
192                         printk("\n%s       ", log_lvl);
193                 printk("%08lx ", *stack++);
194         }
195         printk("\n%sCall Trace:\n", log_lvl);
196
197         show_trace_log_lvl(task, regs, sp, bp, log_lvl);
198 }
199
200 void show_stack(struct task_struct *task, unsigned long *sp)
201 {
202         printk("       ");
203         show_stack_log_lvl(task, NULL, sp, 0, "");
204 }
205
206 /*
207  * The architecture-independent dump_stack generator
208  */
209 void dump_stack(void)
210 {
211         unsigned long bp = 0;
212         unsigned long stack;
213
214 #ifdef CONFIG_FRAME_POINTER
215         if (!bp)
216                 asm("movl %%ebp, %0" : "=r" (bp):);
217 #endif
218
219         printk("Pid: %d, comm: %.20s %s %s %.*s\n",
220                 current->pid, current->comm, print_tainted(),
221                 init_utsname()->release,
222                 (int)strcspn(init_utsname()->version, " "),
223                 init_utsname()->version);
224
225         show_trace(current, NULL, &stack, bp);
226 }
227
228 EXPORT_SYMBOL(dump_stack);
229
230 void show_registers(struct pt_regs *regs)
231 {
232         int i;
233
234         print_modules();
235         __show_regs(regs, 0);
236
237         printk(KERN_EMERG "Process %.*s (pid: %d, ti=%p task=%p task.ti=%p)",
238                 TASK_COMM_LEN, current->comm, task_pid_nr(current),
239                 current_thread_info(), current, task_thread_info(current));
240         /*
241          * When in-kernel, we also print out the stack and code at the
242          * time of the fault..
243          */
244         if (!user_mode_vm(regs)) {
245                 unsigned int code_prologue = code_bytes * 43 / 64;
246                 unsigned int code_len = code_bytes;
247                 unsigned char c;
248                 u8 *ip;
249
250                 printk("\n" KERN_EMERG "Stack: ");
251                 show_stack_log_lvl(NULL, regs, &regs->sp, 0, KERN_EMERG);
252
253                 printk(KERN_EMERG "Code: ");
254
255                 ip = (u8 *)regs->ip - code_prologue;
256                 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
257                         /* try starting at EIP */
258                         ip = (u8 *)regs->ip;
259                         code_len = code_len - code_prologue + 1;
260                 }
261                 for (i = 0; i < code_len; i++, ip++) {
262                         if (ip < (u8 *)PAGE_OFFSET ||
263                                         probe_kernel_address(ip, c)) {
264                                 printk(" Bad EIP value.");
265                                 break;
266                         }
267                         if (ip == (u8 *)regs->ip)
268                                 printk("<%02x> ", c);
269                         else
270                                 printk("%02x ", c);
271                 }
272         }
273         printk("\n");
274 }
275
276 int is_valid_bugaddr(unsigned long ip)
277 {
278         unsigned short ud2;
279
280         if (ip < PAGE_OFFSET)
281                 return 0;
282         if (probe_kernel_address((unsigned short *)ip, ud2))
283                 return 0;
284
285         return ud2 == 0x0b0f;
286 }
287
288 static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
289 static int die_owner = -1;
290 static unsigned int die_nest_count;
291
292 unsigned __kprobes long oops_begin(void)
293 {
294         unsigned long flags;
295
296         oops_enter();
297
298         if (die_owner != raw_smp_processor_id()) {
299                 console_verbose();
300                 raw_local_irq_save(flags);
301                 __raw_spin_lock(&die_lock);
302                 die_owner = smp_processor_id();
303                 die_nest_count = 0;
304                 bust_spinlocks(1);
305         } else {
306                 raw_local_irq_save(flags);
307         }
308         die_nest_count++;
309         return flags;
310 }
311
312 void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
313 {
314         bust_spinlocks(0);
315         die_owner = -1;
316         add_taint(TAINT_DIE);
317         __raw_spin_unlock(&die_lock);
318         raw_local_irq_restore(flags);
319
320         if (!regs)
321                 return;
322
323         if (kexec_should_crash(current))
324                 crash_kexec(regs);
325
326         if (in_interrupt())
327                 panic("Fatal exception in interrupt");
328
329         if (panic_on_oops)
330                 panic("Fatal exception");
331
332         oops_exit();
333         do_exit(signr);
334 }
335
336 int __kprobes __die(const char *str, struct pt_regs *regs, long err)
337 {
338         unsigned short ss;
339         unsigned long sp;
340
341         printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
342 #ifdef CONFIG_PREEMPT
343         printk("PREEMPT ");
344 #endif
345 #ifdef CONFIG_SMP
346         printk("SMP ");
347 #endif
348 #ifdef CONFIG_DEBUG_PAGEALLOC
349         printk("DEBUG_PAGEALLOC");
350 #endif
351         printk("\n");
352         if (notify_die(DIE_OOPS, str, regs, err,
353                         current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
354                 return 1;
355
356         show_registers(regs);
357         /* Executive summary in case the oops scrolled away */
358         sp = (unsigned long) (&regs->sp);
359         savesegment(ss, ss);
360         if (user_mode(regs)) {
361                 sp = regs->sp;
362                 ss = regs->ss & 0xffff;
363         }
364         printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
365         print_symbol("%s", regs->ip);
366         printk(" SS:ESP %04x:%08lx\n", ss, sp);
367         return 0;
368 }
369
370 /*
371  * This is gone through when something in the kernel has done something bad
372  * and is about to be terminated:
373  */
374 void die(const char *str, struct pt_regs *regs, long err)
375 {
376         unsigned long flags = oops_begin();
377
378         if (die_nest_count < 3) {
379                 report_bug(regs->ip, regs);
380
381                 if (__die(str, regs, err))
382                         regs = NULL;
383         } else {
384                 printk(KERN_EMERG "Recursive die() failure, output suppressed\n");
385         }
386
387         oops_end(flags, regs, SIGSEGV);
388 }
389
390 static DEFINE_SPINLOCK(nmi_print_lock);
391
392 void notrace __kprobes
393 die_nmi(char *str, struct pt_regs *regs, int do_panic)
394 {
395         if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
396                 return;
397
398         spin_lock(&nmi_print_lock);
399         /*
400         * We are in trouble anyway, lets at least try
401         * to get a message out:
402         */
403         bust_spinlocks(1);
404         printk(KERN_EMERG "%s", str);
405         printk(" on CPU%d, ip %08lx, registers:\n",
406                 smp_processor_id(), regs->ip);
407         show_registers(regs);
408         if (do_panic)
409                 panic("Non maskable interrupt");
410         console_silent();
411         spin_unlock(&nmi_print_lock);
412         bust_spinlocks(0);
413
414         /*
415          * If we are in kernel we are probably nested up pretty bad
416          * and might aswell get out now while we still can:
417          */
418         if (!user_mode_vm(regs)) {
419                 current->thread.trap_no = 2;
420                 crash_kexec(regs);
421         }
422
423         do_exit(SIGSEGV);
424 }
425
426 static int __init kstack_setup(char *s)
427 {
428         kstack_depth_to_print = simple_strtoul(s, NULL, 0);
429
430         return 1;
431 }
432 __setup("kstack=", kstack_setup);
433
434 static int __init code_bytes_setup(char *s)
435 {
436         code_bytes = simple_strtoul(s, NULL, 0);
437         if (code_bytes > 8192)
438                 code_bytes = 8192;
439
440         return 1;
441 }
442 __setup("code_bytes=", code_bytes_setup);