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