]>
Commit | Line | Data |
---|---|---|
878719e8 NH |
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> | |
712406a6 | 13 | #include <linux/ftrace.h> |
878719e8 NH |
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 | ||
878719e8 NH |
21 | |
22 | int panic_on_unrecovered_nmi; | |
5211a242 | 23 | int panic_on_io_nmi; |
878719e8 NH |
24 | unsigned int code_bytes = 64; |
25 | int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE; | |
26 | static int die_counter; | |
27 | ||
28 | void printk_address(unsigned long address, int reliable) | |
29 | { | |
c767a54b JP |
30 | pr_cont(" [<%p>] %s%pB\n", |
31 | (void *)address, reliable ? "" : "? ", (void *)address); | |
878719e8 NH |
32 | } |
33 | ||
7ee991fb SR |
34 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
35 | static void | |
36 | print_ftrace_graph_addr(unsigned long addr, void *data, | |
37 | const struct stacktrace_ops *ops, | |
38 | struct thread_info *tinfo, int *graph) | |
39 | { | |
65c0ff40 | 40 | struct task_struct *task; |
7ee991fb | 41 | unsigned long ret_addr; |
65c0ff40 | 42 | int index; |
7ee991fb SR |
43 | |
44 | if (addr != (unsigned long)return_to_handler) | |
45 | return; | |
46 | ||
65c0ff40 HD |
47 | task = tinfo->task; |
48 | index = task->curr_ret_stack; | |
49 | ||
7ee991fb SR |
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 | |
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 | ||
878719e8 NH |
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 | ||
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 | |
89 | print_context_stack(struct thread_info *tinfo, | |
90 | unsigned long *stack, unsigned long bp, | |
91 | const struct stacktrace_ops *ops, void *data, | |
7ee991fb | 92 | unsigned long *end, int *graph) |
878719e8 NH |
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 { | |
2c344e9d | 106 | ops->address(data, addr, 0); |
878719e8 | 107 | } |
7ee991fb | 108 | print_ftrace_graph_addr(addr, data, ops, tinfo, graph); |
878719e8 NH |
109 | } |
110 | stack++; | |
111 | } | |
112 | return bp; | |
113 | } | |
06d65bda FW |
114 | EXPORT_SYMBOL_GPL(print_context_stack); |
115 | ||
116 | unsigned long | |
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 | ||
c2c5d45d FW |
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); | |
06d65bda | 135 | } |
c2c5d45d | 136 | |
06d65bda FW |
137 | return (unsigned long)frame; |
138 | } | |
139 | EXPORT_SYMBOL_GPL(print_context_stack_bp); | |
878719e8 | 140 | |
878719e8 NH |
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 | */ | |
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 = { | |
06d65bda FW |
158 | .stack = print_trace_stack, |
159 | .address = print_trace_address, | |
61c1917f | 160 | .walk_stack = print_context_stack, |
878719e8 NH |
161 | }; |
162 | ||
163 | void | |
164 | show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs, | |
e8e999cf | 165 | unsigned long *stack, unsigned long bp, char *log_lvl) |
878719e8 NH |
166 | { |
167 | printk("%sCall Trace:\n", log_lvl); | |
e8e999cf | 168 | dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl); |
878719e8 NH |
169 | } |
170 | ||
171 | void show_trace(struct task_struct *task, struct pt_regs *regs, | |
e8e999cf | 172 | unsigned long *stack, unsigned long bp) |
878719e8 | 173 | { |
e8e999cf | 174 | show_trace_log_lvl(task, regs, stack, bp, ""); |
878719e8 NH |
175 | } |
176 | ||
177 | void show_stack(struct task_struct *task, unsigned long *sp) | |
178 | { | |
a77f2a4e TH |
179 | unsigned long bp = 0; |
180 | unsigned long stack; | |
181 | ||
182 | /* | |
183 | * Stack frames below this one aren't interesting. Don't show them | |
184 | * if we're printing for %current. | |
185 | */ | |
186 | if (!sp && (!task || task == current)) { | |
187 | sp = &stack; | |
188 | bp = stack_frame(current, NULL); | |
189 | } | |
190 | ||
191 | show_stack_log_lvl(task, NULL, sp, bp, ""); | |
878719e8 NH |
192 | } |
193 | ||
edc35bd7 | 194 | static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED; |
878719e8 NH |
195 | static int die_owner = -1; |
196 | static unsigned int die_nest_count; | |
197 | ||
198 | unsigned __kprobes long oops_begin(void) | |
199 | { | |
200 | int cpu; | |
201 | unsigned long flags; | |
202 | ||
203 | oops_enter(); | |
204 | ||
205 | /* racy, but better than risking deadlock. */ | |
206 | raw_local_irq_save(flags); | |
207 | cpu = smp_processor_id(); | |
0199c4e6 | 208 | if (!arch_spin_trylock(&die_lock)) { |
878719e8 NH |
209 | if (cpu == die_owner) |
210 | /* nested oops. should stop eventually */; | |
211 | else | |
0199c4e6 | 212 | arch_spin_lock(&die_lock); |
878719e8 NH |
213 | } |
214 | die_nest_count++; | |
215 | die_owner = cpu; | |
216 | console_verbose(); | |
217 | bust_spinlocks(1); | |
218 | return flags; | |
219 | } | |
81e88fdc | 220 | EXPORT_SYMBOL_GPL(oops_begin); |
878719e8 NH |
221 | |
222 | void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr) | |
223 | { | |
224 | if (regs && kexec_should_crash(current)) | |
225 | crash_kexec(regs); | |
226 | ||
227 | bust_spinlocks(0); | |
228 | die_owner = -1; | |
373d4d09 | 229 | add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE); |
878719e8 NH |
230 | die_nest_count--; |
231 | if (!die_nest_count) | |
232 | /* Nest count reaches zero, release the lock. */ | |
0199c4e6 | 233 | arch_spin_unlock(&die_lock); |
878719e8 NH |
234 | raw_local_irq_restore(flags); |
235 | oops_exit(); | |
236 | ||
237 | if (!signr) | |
238 | return; | |
239 | if (in_interrupt()) | |
240 | panic("Fatal exception in interrupt"); | |
241 | if (panic_on_oops) | |
242 | panic("Fatal exception"); | |
243 | do_exit(signr); | |
244 | } | |
245 | ||
246 | int __kprobes __die(const char *str, struct pt_regs *regs, long err) | |
247 | { | |
248 | #ifdef CONFIG_X86_32 | |
249 | unsigned short ss; | |
250 | unsigned long sp; | |
251 | #endif | |
b0f4c4b3 PB |
252 | printk(KERN_DEFAULT |
253 | "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter); | |
878719e8 NH |
254 | #ifdef CONFIG_PREEMPT |
255 | printk("PREEMPT "); | |
256 | #endif | |
257 | #ifdef CONFIG_SMP | |
258 | printk("SMP "); | |
259 | #endif | |
260 | #ifdef CONFIG_DEBUG_PAGEALLOC | |
261 | printk("DEBUG_PAGEALLOC"); | |
262 | #endif | |
263 | printk("\n"); | |
878719e8 | 264 | if (notify_die(DIE_OOPS, str, regs, err, |
51e7dc70 | 265 | current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP) |
878719e8 NH |
266 | return 1; |
267 | ||
0fa0e2f0 | 268 | print_modules(); |
57da8b96 | 269 | show_regs(regs); |
878719e8 | 270 | #ifdef CONFIG_X86_32 |
a343c75d | 271 | if (user_mode_vm(regs)) { |
878719e8 NH |
272 | sp = regs->sp; |
273 | ss = regs->ss & 0xffff; | |
a343c75d PA |
274 | } else { |
275 | sp = kernel_stack_pointer(regs); | |
276 | savesegment(ss, ss); | |
878719e8 NH |
277 | } |
278 | printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip); | |
279 | print_symbol("%s", regs->ip); | |
280 | printk(" SS:ESP %04x:%08lx\n", ss, sp); | |
281 | #else | |
282 | /* Executive summary in case the oops scrolled away */ | |
283 | printk(KERN_ALERT "RIP "); | |
284 | printk_address(regs->ip, 1); | |
285 | printk(" RSP <%016lx>\n", regs->sp); | |
286 | #endif | |
287 | return 0; | |
288 | } | |
289 | ||
290 | /* | |
291 | * This is gone through when something in the kernel has done something bad | |
292 | * and is about to be terminated: | |
293 | */ | |
294 | void die(const char *str, struct pt_regs *regs, long err) | |
295 | { | |
296 | unsigned long flags = oops_begin(); | |
297 | int sig = SIGSEGV; | |
298 | ||
299 | if (!user_mode_vm(regs)) | |
300 | report_bug(regs->ip, regs); | |
301 | ||
302 | if (__die(str, regs, err)) | |
303 | sig = 0; | |
304 | oops_end(flags, regs, sig); | |
305 | } | |
306 | ||
878719e8 NH |
307 | static int __init kstack_setup(char *s) |
308 | { | |
363f7ce3 SK |
309 | ssize_t ret; |
310 | unsigned long val; | |
311 | ||
878719e8 NH |
312 | if (!s) |
313 | return -EINVAL; | |
363f7ce3 SK |
314 | |
315 | ret = kstrtoul(s, 0, &val); | |
316 | if (ret) | |
317 | return ret; | |
318 | kstack_depth_to_print = val; | |
878719e8 NH |
319 | return 0; |
320 | } | |
321 | early_param("kstack", kstack_setup); | |
322 | ||
323 | static int __init code_bytes_setup(char *s) | |
324 | { | |
363f7ce3 SK |
325 | ssize_t ret; |
326 | unsigned long val; | |
327 | ||
328 | if (!s) | |
329 | return -EINVAL; | |
330 | ||
331 | ret = kstrtoul(s, 0, &val); | |
332 | if (ret) | |
333 | return ret; | |
334 | ||
335 | code_bytes = val; | |
878719e8 NH |
336 | if (code_bytes > 8192) |
337 | code_bytes = 8192; | |
338 | ||
339 | return 1; | |
340 | } | |
341 | __setup("code_bytes=", code_bytes_setup); |