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