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 22 int panic_on_unrecovered_nmi; 23 int panic_on_io_nmi; 24 unsigned int code_bytes = 64; 25 int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE; 26 static int die_counter; 27 28 static void printk_stack_address(unsigned long address, int reliable, 29 void *data) 30 { 31 printk("%s [<%p>] %s%pB\n", 32 (char *)data, (void *)address, reliable ? "" : "? ", 33 (void *)address); 34 } 35 36 void printk_address(unsigned long address) 37 { 38 pr_cont(" [<%p>] %pS\n", (void *)address, (void *)address); 39 } 40 41 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 42 static void 43 print_ftrace_graph_addr(unsigned long addr, void *data, 44 const struct stacktrace_ops *ops, 45 struct task_struct *task, int *graph) 46 { 47 unsigned long ret_addr; 48 int index; 49 50 if (addr != (unsigned long)return_to_handler) 51 return; 52 53 index = task->curr_ret_stack; 54 55 if (!task->ret_stack || index < *graph) 56 return; 57 58 index -= *graph; 59 ret_addr = task->ret_stack[index].ret; 60 61 ops->address(data, ret_addr, 1); 62 63 (*graph)++; 64 } 65 #else 66 static inline void 67 print_ftrace_graph_addr(unsigned long addr, void *data, 68 const struct stacktrace_ops *ops, 69 struct task_struct *task, int *graph) 70 { } 71 #endif 72 73 /* 74 * x86-64 can have up to three kernel stacks: 75 * process stack 76 * interrupt stack 77 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack 78 */ 79 80 static inline int valid_stack_ptr(struct task_struct *task, 81 void *p, unsigned int size, void *end) 82 { 83 void *t = task_stack_page(task); 84 if (end) { 85 if (p < end && p >= (end-THREAD_SIZE)) 86 return 1; 87 else 88 return 0; 89 } 90 return p > t && p < t + THREAD_SIZE - size; 91 } 92 93 unsigned long 94 print_context_stack(struct task_struct *task, 95 unsigned long *stack, unsigned long bp, 96 const struct stacktrace_ops *ops, void *data, 97 unsigned long *end, int *graph) 98 { 99 struct stack_frame *frame = (struct stack_frame *)bp; 100 101 while (valid_stack_ptr(task, stack, sizeof(*stack), end)) { 102 unsigned long addr; 103 104 addr = *stack; 105 if (__kernel_text_address(addr)) { 106 if ((unsigned long) stack == bp + sizeof(long)) { 107 ops->address(data, addr, 1); 108 frame = frame->next_frame; 109 bp = (unsigned long) frame; 110 } else { 111 ops->address(data, addr, 0); 112 } 113 print_ftrace_graph_addr(addr, data, ops, task, graph); 114 } 115 stack++; 116 } 117 return bp; 118 } 119 EXPORT_SYMBOL_GPL(print_context_stack); 120 121 unsigned long 122 print_context_stack_bp(struct task_struct *task, 123 unsigned long *stack, unsigned long bp, 124 const struct stacktrace_ops *ops, void *data, 125 unsigned long *end, int *graph) 126 { 127 struct stack_frame *frame = (struct stack_frame *)bp; 128 unsigned long *ret_addr = &frame->return_address; 129 130 while (valid_stack_ptr(task, ret_addr, sizeof(*ret_addr), end)) { 131 unsigned long addr = *ret_addr; 132 133 if (!__kernel_text_address(addr)) 134 break; 135 136 if (ops->address(data, addr, 1)) 137 break; 138 frame = frame->next_frame; 139 ret_addr = &frame->return_address; 140 print_ftrace_graph_addr(addr, data, ops, task, graph); 141 } 142 143 return (unsigned long)frame; 144 } 145 EXPORT_SYMBOL_GPL(print_context_stack_bp); 146 147 static int print_trace_stack(void *data, char *name) 148 { 149 printk("%s <%s> ", (char *)data, name); 150 return 0; 151 } 152 153 /* 154 * Print one address/symbol entries per line. 155 */ 156 static int print_trace_address(void *data, unsigned long addr, int reliable) 157 { 158 touch_nmi_watchdog(); 159 printk_stack_address(addr, reliable, data); 160 return 0; 161 } 162 163 static const struct stacktrace_ops print_trace_ops = { 164 .stack = print_trace_stack, 165 .address = print_trace_address, 166 .walk_stack = print_context_stack, 167 }; 168 169 void 170 show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs, 171 unsigned long *stack, unsigned long bp, char *log_lvl) 172 { 173 printk("%sCall Trace:\n", log_lvl); 174 dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl); 175 } 176 177 void show_trace(struct task_struct *task, struct pt_regs *regs, 178 unsigned long *stack, unsigned long bp) 179 { 180 show_trace_log_lvl(task, regs, stack, bp, ""); 181 } 182 183 void show_stack(struct task_struct *task, unsigned long *sp) 184 { 185 unsigned long bp = 0; 186 unsigned long stack; 187 188 /* 189 * Stack frames below this one aren't interesting. Don't show them 190 * if we're printing for %current. 191 */ 192 if (!sp && (!task || task == current)) { 193 sp = &stack; 194 bp = stack_frame(current, NULL); 195 } 196 197 show_stack_log_lvl(task, NULL, sp, bp, ""); 198 } 199 200 static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED; 201 static int die_owner = -1; 202 static unsigned int die_nest_count; 203 204 unsigned long oops_begin(void) 205 { 206 int cpu; 207 unsigned long flags; 208 209 oops_enter(); 210 211 /* racy, but better than risking deadlock. */ 212 raw_local_irq_save(flags); 213 cpu = smp_processor_id(); 214 if (!arch_spin_trylock(&die_lock)) { 215 if (cpu == die_owner) 216 /* nested oops. should stop eventually */; 217 else 218 arch_spin_lock(&die_lock); 219 } 220 die_nest_count++; 221 die_owner = cpu; 222 console_verbose(); 223 bust_spinlocks(1); 224 return flags; 225 } 226 EXPORT_SYMBOL_GPL(oops_begin); 227 NOKPROBE_SYMBOL(oops_begin); 228 229 void oops_end(unsigned long flags, struct pt_regs *regs, int signr) 230 { 231 if (regs && kexec_should_crash(current)) 232 crash_kexec(regs); 233 234 bust_spinlocks(0); 235 die_owner = -1; 236 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE); 237 die_nest_count--; 238 if (!die_nest_count) 239 /* Nest count reaches zero, release the lock. */ 240 arch_spin_unlock(&die_lock); 241 raw_local_irq_restore(flags); 242 oops_exit(); 243 244 if (!signr) 245 return; 246 if (in_interrupt()) 247 panic("Fatal exception in interrupt"); 248 if (panic_on_oops) 249 panic("Fatal exception"); 250 do_exit(signr); 251 } 252 NOKPROBE_SYMBOL(oops_end); 253 254 int __die(const char *str, struct pt_regs *regs, long err) 255 { 256 #ifdef CONFIG_X86_32 257 unsigned short ss; 258 unsigned long sp; 259 #endif 260 printk(KERN_DEFAULT 261 "%s: %04lx [#%d]%s%s%s%s\n", str, err & 0xffff, ++die_counter, 262 IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : "", 263 IS_ENABLED(CONFIG_SMP) ? " SMP" : "", 264 debug_pagealloc_enabled() ? " DEBUG_PAGEALLOC" : "", 265 IS_ENABLED(CONFIG_KASAN) ? " KASAN" : ""); 266 267 if (notify_die(DIE_OOPS, str, regs, err, 268 current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP) 269 return 1; 270 271 print_modules(); 272 show_regs(regs); 273 #ifdef CONFIG_X86_32 274 if (user_mode(regs)) { 275 sp = regs->sp; 276 ss = regs->ss & 0xffff; 277 } else { 278 sp = kernel_stack_pointer(regs); 279 savesegment(ss, ss); 280 } 281 printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip); 282 print_symbol("%s", regs->ip); 283 printk(" SS:ESP %04x:%08lx\n", ss, sp); 284 #else 285 /* Executive summary in case the oops scrolled away */ 286 printk(KERN_ALERT "RIP "); 287 printk_address(regs->ip); 288 printk(" RSP <%016lx>\n", regs->sp); 289 #endif 290 return 0; 291 } 292 NOKPROBE_SYMBOL(__die); 293 294 /* 295 * This is gone through when something in the kernel has done something bad 296 * and is about to be terminated: 297 */ 298 void die(const char *str, struct pt_regs *regs, long err) 299 { 300 unsigned long flags = oops_begin(); 301 int sig = SIGSEGV; 302 303 if (!user_mode(regs)) 304 report_bug(regs->ip, regs); 305 306 if (__die(str, regs, err)) 307 sig = 0; 308 oops_end(flags, regs, sig); 309 } 310 311 static int __init kstack_setup(char *s) 312 { 313 ssize_t ret; 314 unsigned long val; 315 316 if (!s) 317 return -EINVAL; 318 319 ret = kstrtoul(s, 0, &val); 320 if (ret) 321 return ret; 322 kstack_depth_to_print = val; 323 return 0; 324 } 325 early_param("kstack", kstack_setup); 326 327 static int __init code_bytes_setup(char *s) 328 { 329 ssize_t ret; 330 unsigned long val; 331 332 if (!s) 333 return -EINVAL; 334 335 ret = kstrtoul(s, 0, &val); 336 if (ret) 337 return ret; 338 339 code_bytes = val; 340 if (code_bytes > 8192) 341 code_bytes = 8192; 342 343 return 1; 344 } 345 __setup("code_bytes=", code_bytes_setup); 346