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