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 | ||
1fc7f61c AS |
28 | static void printk_stack_address(unsigned long address, int reliable, |
29 | void *data) | |
878719e8 | 30 | { |
1fc7f61c AS |
31 | printk("%s [<%p>] %s%pB\n", |
32 | (char *)data, (void *)address, reliable ? "" : "? ", | |
33 | (void *)address); | |
878719e8 NH |
34 | } |
35 | ||
5f01c988 JS |
36 | void printk_address(unsigned long address) |
37 | { | |
38 | pr_cont(" [<%p>] %pS\n", (void *)address, (void *)address); | |
39 | } | |
40 | ||
7ee991fb SR |
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, | |
da01e18a | 45 | struct task_struct *task, int *graph) |
7ee991fb | 46 | { |
7ee991fb | 47 | unsigned long ret_addr; |
65c0ff40 | 48 | int index; |
7ee991fb SR |
49 | |
50 | if (addr != (unsigned long)return_to_handler) | |
51 | return; | |
52 | ||
65c0ff40 HD |
53 | index = task->curr_ret_stack; |
54 | ||
7ee991fb SR |
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, | |
da01e18a | 69 | struct task_struct *task, int *graph) |
7ee991fb SR |
70 | { } |
71 | #endif | |
72 | ||
878719e8 NH |
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 | ||
da01e18a | 80 | static inline int valid_stack_ptr(struct task_struct *task, |
878719e8 NH |
81 | void *p, unsigned int size, void *end) |
82 | { | |
aca9c293 | 83 | void *t = task_stack_page(task); |
878719e8 NH |
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 | |
da01e18a | 94 | print_context_stack(struct task_struct *task, |
878719e8 NH |
95 | unsigned long *stack, unsigned long bp, |
96 | const struct stacktrace_ops *ops, void *data, | |
7ee991fb | 97 | unsigned long *end, int *graph) |
878719e8 NH |
98 | { |
99 | struct stack_frame *frame = (struct stack_frame *)bp; | |
100 | ||
da01e18a | 101 | while (valid_stack_ptr(task, stack, sizeof(*stack), end)) { |
878719e8 NH |
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 { | |
2c344e9d | 111 | ops->address(data, addr, 0); |
878719e8 | 112 | } |
da01e18a | 113 | print_ftrace_graph_addr(addr, data, ops, task, graph); |
878719e8 NH |
114 | } |
115 | stack++; | |
116 | } | |
117 | return bp; | |
118 | } | |
06d65bda FW |
119 | EXPORT_SYMBOL_GPL(print_context_stack); |
120 | ||
121 | unsigned long | |
da01e18a | 122 | print_context_stack_bp(struct task_struct *task, |
06d65bda FW |
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 | ||
da01e18a | 130 | while (valid_stack_ptr(task, ret_addr, sizeof(*ret_addr), end)) { |
06d65bda FW |
131 | unsigned long addr = *ret_addr; |
132 | ||
c2c5d45d FW |
133 | if (!__kernel_text_address(addr)) |
134 | break; | |
135 | ||
568b329a AS |
136 | if (ops->address(data, addr, 1)) |
137 | break; | |
c2c5d45d FW |
138 | frame = frame->next_frame; |
139 | ret_addr = &frame->return_address; | |
da01e18a | 140 | print_ftrace_graph_addr(addr, data, ops, task, graph); |
06d65bda | 141 | } |
c2c5d45d | 142 | |
06d65bda FW |
143 | return (unsigned long)frame; |
144 | } | |
145 | EXPORT_SYMBOL_GPL(print_context_stack_bp); | |
878719e8 | 146 | |
878719e8 NH |
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 | */ | |
568b329a | 156 | static int print_trace_address(void *data, unsigned long addr, int reliable) |
878719e8 NH |
157 | { |
158 | touch_nmi_watchdog(); | |
1fc7f61c | 159 | printk_stack_address(addr, reliable, data); |
568b329a | 160 | return 0; |
878719e8 NH |
161 | } |
162 | ||
163 | static const struct stacktrace_ops print_trace_ops = { | |
06d65bda FW |
164 | .stack = print_trace_stack, |
165 | .address = print_trace_address, | |
61c1917f | 166 | .walk_stack = print_context_stack, |
878719e8 NH |
167 | }; |
168 | ||
169 | void | |
170 | show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs, | |
e8e999cf | 171 | unsigned long *stack, unsigned long bp, char *log_lvl) |
878719e8 NH |
172 | { |
173 | printk("%sCall Trace:\n", log_lvl); | |
e8e999cf | 174 | dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl); |
878719e8 NH |
175 | } |
176 | ||
177 | void show_trace(struct task_struct *task, struct pt_regs *regs, | |
e8e999cf | 178 | unsigned long *stack, unsigned long bp) |
878719e8 | 179 | { |
e8e999cf | 180 | show_trace_log_lvl(task, regs, stack, bp, ""); |
878719e8 NH |
181 | } |
182 | ||
183 | void show_stack(struct task_struct *task, unsigned long *sp) | |
184 | { | |
a77f2a4e TH |
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, ""); | |
878719e8 NH |
198 | } |
199 | ||
edc35bd7 | 200 | static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED; |
878719e8 NH |
201 | static int die_owner = -1; |
202 | static unsigned int die_nest_count; | |
203 | ||
9326638c | 204 | unsigned long oops_begin(void) |
878719e8 NH |
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(); | |
0199c4e6 | 214 | if (!arch_spin_trylock(&die_lock)) { |
878719e8 NH |
215 | if (cpu == die_owner) |
216 | /* nested oops. should stop eventually */; | |
217 | else | |
0199c4e6 | 218 | arch_spin_lock(&die_lock); |
878719e8 NH |
219 | } |
220 | die_nest_count++; | |
221 | die_owner = cpu; | |
222 | console_verbose(); | |
223 | bust_spinlocks(1); | |
224 | return flags; | |
225 | } | |
81e88fdc | 226 | EXPORT_SYMBOL_GPL(oops_begin); |
9326638c | 227 | NOKPROBE_SYMBOL(oops_begin); |
878719e8 | 228 | |
9326638c | 229 | void oops_end(unsigned long flags, struct pt_regs *regs, int signr) |
878719e8 NH |
230 | { |
231 | if (regs && kexec_should_crash(current)) | |
232 | crash_kexec(regs); | |
233 | ||
234 | bust_spinlocks(0); | |
235 | die_owner = -1; | |
373d4d09 | 236 | add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE); |
878719e8 NH |
237 | die_nest_count--; |
238 | if (!die_nest_count) | |
239 | /* Nest count reaches zero, release the lock. */ | |
0199c4e6 | 240 | arch_spin_unlock(&die_lock); |
878719e8 NH |
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 | } | |
9326638c | 252 | NOKPROBE_SYMBOL(oops_end); |
878719e8 | 253 | |
9326638c | 254 | int __die(const char *str, struct pt_regs *regs, long err) |
878719e8 NH |
255 | { |
256 | #ifdef CONFIG_X86_32 | |
257 | unsigned short ss; | |
258 | unsigned long sp; | |
259 | #endif | |
b0f4c4b3 | 260 | printk(KERN_DEFAULT |
8fad7ec5 RV |
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 | ||
878719e8 | 267 | if (notify_die(DIE_OOPS, str, regs, err, |
51e7dc70 | 268 | current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP) |
878719e8 NH |
269 | return 1; |
270 | ||
0fa0e2f0 | 271 | print_modules(); |
57da8b96 | 272 | show_regs(regs); |
878719e8 | 273 | #ifdef CONFIG_X86_32 |
f39b6f0e | 274 | if (user_mode(regs)) { |
878719e8 NH |
275 | sp = regs->sp; |
276 | ss = regs->ss & 0xffff; | |
a343c75d PA |
277 | } else { |
278 | sp = kernel_stack_pointer(regs); | |
279 | savesegment(ss, ss); | |
878719e8 NH |
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 "); | |
5f01c988 | 287 | printk_address(regs->ip); |
878719e8 NH |
288 | printk(" RSP <%016lx>\n", regs->sp); |
289 | #endif | |
290 | return 0; | |
291 | } | |
9326638c | 292 | NOKPROBE_SYMBOL(__die); |
878719e8 NH |
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 | ||
f39b6f0e | 303 | if (!user_mode(regs)) |
878719e8 NH |
304 | report_bug(regs->ip, regs); |
305 | ||
306 | if (__die(str, regs, err)) | |
307 | sig = 0; | |
308 | oops_end(flags, regs, sig); | |
309 | } | |
310 | ||
878719e8 NH |
311 | static int __init kstack_setup(char *s) |
312 | { | |
363f7ce3 SK |
313 | ssize_t ret; |
314 | unsigned long val; | |
315 | ||
878719e8 NH |
316 | if (!s) |
317 | return -EINVAL; | |
363f7ce3 SK |
318 | |
319 | ret = kstrtoul(s, 0, &val); | |
320 | if (ret) | |
321 | return ret; | |
322 | kstack_depth_to_print = val; | |
878719e8 NH |
323 | return 0; |
324 | } | |
325 | early_param("kstack", kstack_setup); | |
326 | ||
327 | static int __init code_bytes_setup(char *s) | |
328 | { | |
363f7ce3 SK |
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; | |
878719e8 NH |
340 | if (code_bytes > 8192) |
341 | code_bytes = 8192; | |
342 | ||
343 | return 1; | |
344 | } | |
345 | __setup("code_bytes=", code_bytes_setup); |