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/ftrace.h> 14 #include <linux/kexec.h> 15 #include <linux/bug.h> 16 #include <linux/nmi.h> 17 #include <linux/sysfs.h> 18 19 #include <asm/stacktrace.h> 20 21 #include "dumpstack.h" 22 23 int panic_on_unrecovered_nmi; 24 int panic_on_io_nmi; 25 unsigned int code_bytes = 64; 26 int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE; 27 static int die_counter; 28 29 void printk_address(unsigned long address, int reliable) 30 { 31 printk(" [<%p>] %s%pS\n", (void *) address, 32 reliable ? "" : "? ", (void *) address); 33 } 34 35 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 36 static void 37 print_ftrace_graph_addr(unsigned long addr, void *data, 38 const struct stacktrace_ops *ops, 39 struct thread_info *tinfo, int *graph) 40 { 41 struct task_struct *task = tinfo->task; 42 unsigned long ret_addr; 43 int index = task->curr_ret_stack; 44 45 if (addr != (unsigned long)return_to_handler) 46 return; 47 48 if (!task->ret_stack || index < *graph) 49 return; 50 51 index -= *graph; 52 ret_addr = task->ret_stack[index].ret; 53 54 ops->address(data, ret_addr, 1); 55 56 (*graph)++; 57 } 58 #else 59 static inline void 60 print_ftrace_graph_addr(unsigned long addr, void *data, 61 const struct stacktrace_ops *ops, 62 struct thread_info *tinfo, int *graph) 63 { } 64 #endif 65 66 /* 67 * x86-64 can have up to three kernel stacks: 68 * process stack 69 * interrupt stack 70 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack 71 */ 72 73 static inline int valid_stack_ptr(struct thread_info *tinfo, 74 void *p, unsigned int size, void *end) 75 { 76 void *t = tinfo; 77 if (end) { 78 if (p < end && p >= (end-THREAD_SIZE)) 79 return 1; 80 else 81 return 0; 82 } 83 return p > t && p < t + THREAD_SIZE - size; 84 } 85 86 unsigned long 87 print_context_stack(struct thread_info *tinfo, 88 unsigned long *stack, unsigned long bp, 89 const struct stacktrace_ops *ops, void *data, 90 unsigned long *end, int *graph) 91 { 92 struct stack_frame *frame = (struct stack_frame *)bp; 93 94 while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) { 95 unsigned long addr; 96 97 addr = *stack; 98 if (__kernel_text_address(addr)) { 99 if ((unsigned long) stack == bp + sizeof(long)) { 100 ops->address(data, addr, 1); 101 frame = frame->next_frame; 102 bp = (unsigned long) frame; 103 } else { 104 ops->address(data, addr, 0); 105 } 106 print_ftrace_graph_addr(addr, data, ops, tinfo, graph); 107 } 108 stack++; 109 } 110 return bp; 111 } 112 EXPORT_SYMBOL_GPL(print_context_stack); 113 114 unsigned long 115 print_context_stack_bp(struct thread_info *tinfo, 116 unsigned long *stack, unsigned long bp, 117 const struct stacktrace_ops *ops, void *data, 118 unsigned long *end, int *graph) 119 { 120 struct stack_frame *frame = (struct stack_frame *)bp; 121 unsigned long *ret_addr = &frame->return_address; 122 123 while (valid_stack_ptr(tinfo, ret_addr, sizeof(*ret_addr), end)) { 124 unsigned long addr = *ret_addr; 125 126 if (!__kernel_text_address(addr)) 127 break; 128 129 ops->address(data, addr, 1); 130 frame = frame->next_frame; 131 ret_addr = &frame->return_address; 132 print_ftrace_graph_addr(addr, data, ops, tinfo, graph); 133 } 134 135 return (unsigned long)frame; 136 } 137 EXPORT_SYMBOL_GPL(print_context_stack_bp); 138 139 140 static void 141 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol) 142 { 143 printk(data); 144 print_symbol(msg, symbol); 145 printk("\n"); 146 } 147 148 static void print_trace_warning(void *data, char *msg) 149 { 150 printk("%s%s\n", (char *)data, msg); 151 } 152 153 static int print_trace_stack(void *data, char *name) 154 { 155 printk("%s <%s> ", (char *)data, name); 156 return 0; 157 } 158 159 /* 160 * Print one address/symbol entries per line. 161 */ 162 static void print_trace_address(void *data, unsigned long addr, int reliable) 163 { 164 touch_nmi_watchdog(); 165 printk(data); 166 printk_address(addr, reliable); 167 } 168 169 static const struct stacktrace_ops print_trace_ops = { 170 .warning = print_trace_warning, 171 .warning_symbol = print_trace_warning_symbol, 172 .stack = print_trace_stack, 173 .address = print_trace_address, 174 .walk_stack = print_context_stack, 175 }; 176 177 void 178 show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs, 179 unsigned long *stack, unsigned long bp, char *log_lvl) 180 { 181 printk("%sCall Trace:\n", log_lvl); 182 dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl); 183 } 184 185 void show_trace(struct task_struct *task, struct pt_regs *regs, 186 unsigned long *stack, unsigned long bp) 187 { 188 show_trace_log_lvl(task, regs, stack, bp, ""); 189 } 190 191 void show_stack(struct task_struct *task, unsigned long *sp) 192 { 193 show_stack_log_lvl(task, NULL, sp, 0, ""); 194 } 195 196 /* 197 * The architecture-independent dump_stack generator 198 */ 199 void dump_stack(void) 200 { 201 unsigned long bp = 0; 202 unsigned long stack; 203 204 #ifdef CONFIG_FRAME_POINTER 205 if (!bp) 206 get_bp(bp); 207 #endif 208 209 printk("Pid: %d, comm: %.20s %s %s %.*s\n", 210 current->pid, current->comm, print_tainted(), 211 init_utsname()->release, 212 (int)strcspn(init_utsname()->version, " "), 213 init_utsname()->version); 214 show_trace(NULL, NULL, &stack, bp); 215 } 216 EXPORT_SYMBOL(dump_stack); 217 218 static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED; 219 static int die_owner = -1; 220 static unsigned int die_nest_count; 221 222 unsigned __kprobes long oops_begin(void) 223 { 224 int cpu; 225 unsigned long flags; 226 227 /* notify the hw-branch tracer so it may disable tracing and 228 add the last trace to the trace buffer - 229 the earlier this happens, the more useful the trace. */ 230 trace_hw_branch_oops(); 231 232 oops_enter(); 233 234 /* racy, but better than risking deadlock. */ 235 raw_local_irq_save(flags); 236 cpu = smp_processor_id(); 237 if (!arch_spin_trylock(&die_lock)) { 238 if (cpu == die_owner) 239 /* nested oops. should stop eventually */; 240 else 241 arch_spin_lock(&die_lock); 242 } 243 die_nest_count++; 244 die_owner = cpu; 245 console_verbose(); 246 bust_spinlocks(1); 247 return flags; 248 } 249 250 void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr) 251 { 252 if (regs && kexec_should_crash(current)) 253 crash_kexec(regs); 254 255 bust_spinlocks(0); 256 die_owner = -1; 257 add_taint(TAINT_DIE); 258 die_nest_count--; 259 if (!die_nest_count) 260 /* Nest count reaches zero, release the lock. */ 261 arch_spin_unlock(&die_lock); 262 raw_local_irq_restore(flags); 263 oops_exit(); 264 265 if (!signr) 266 return; 267 if (in_interrupt()) 268 panic("Fatal exception in interrupt"); 269 if (panic_on_oops) 270 panic("Fatal exception"); 271 do_exit(signr); 272 } 273 274 int __kprobes __die(const char *str, struct pt_regs *regs, long err) 275 { 276 #ifdef CONFIG_X86_32 277 unsigned short ss; 278 unsigned long sp; 279 #endif 280 printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter); 281 #ifdef CONFIG_PREEMPT 282 printk("PREEMPT "); 283 #endif 284 #ifdef CONFIG_SMP 285 printk("SMP "); 286 #endif 287 #ifdef CONFIG_DEBUG_PAGEALLOC 288 printk("DEBUG_PAGEALLOC"); 289 #endif 290 printk("\n"); 291 sysfs_printk_last_file(); 292 if (notify_die(DIE_OOPS, str, regs, err, 293 current->thread.trap_no, SIGSEGV) == NOTIFY_STOP) 294 return 1; 295 296 show_registers(regs); 297 #ifdef CONFIG_X86_32 298 if (user_mode_vm(regs)) { 299 sp = regs->sp; 300 ss = regs->ss & 0xffff; 301 } else { 302 sp = kernel_stack_pointer(regs); 303 savesegment(ss, ss); 304 } 305 printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip); 306 print_symbol("%s", regs->ip); 307 printk(" SS:ESP %04x:%08lx\n", ss, sp); 308 #else 309 /* Executive summary in case the oops scrolled away */ 310 printk(KERN_ALERT "RIP "); 311 printk_address(regs->ip, 1); 312 printk(" RSP <%016lx>\n", regs->sp); 313 #endif 314 return 0; 315 } 316 317 /* 318 * This is gone through when something in the kernel has done something bad 319 * and is about to be terminated: 320 */ 321 void die(const char *str, struct pt_regs *regs, long err) 322 { 323 unsigned long flags = oops_begin(); 324 int sig = SIGSEGV; 325 326 if (!user_mode_vm(regs)) 327 report_bug(regs->ip, regs); 328 329 if (__die(str, regs, err)) 330 sig = 0; 331 oops_end(flags, regs, sig); 332 } 333 334 void notrace __kprobes 335 die_nmi(char *str, struct pt_regs *regs, int do_panic) 336 { 337 unsigned long flags; 338 339 if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP) 340 return; 341 342 /* 343 * We are in trouble anyway, lets at least try 344 * to get a message out. 345 */ 346 flags = oops_begin(); 347 printk(KERN_EMERG "%s", str); 348 printk(" on CPU%d, ip %08lx, registers:\n", 349 smp_processor_id(), regs->ip); 350 show_registers(regs); 351 oops_end(flags, regs, 0); 352 if (do_panic || panic_on_oops) 353 panic("Non maskable interrupt"); 354 nmi_exit(); 355 local_irq_enable(); 356 do_exit(SIGBUS); 357 } 358 359 static int __init oops_setup(char *s) 360 { 361 if (!s) 362 return -EINVAL; 363 if (!strcmp(s, "panic")) 364 panic_on_oops = 1; 365 return 0; 366 } 367 early_param("oops", oops_setup); 368 369 static int __init kstack_setup(char *s) 370 { 371 if (!s) 372 return -EINVAL; 373 kstack_depth_to_print = simple_strtoul(s, NULL, 0); 374 return 0; 375 } 376 early_param("kstack", kstack_setup); 377 378 static int __init code_bytes_setup(char *s) 379 { 380 code_bytes = simple_strtoul(s, NULL, 0); 381 if (code_bytes > 8192) 382 code_bytes = 8192; 383 384 return 1; 385 } 386 __setup("code_bytes=", code_bytes_setup); 387