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 | 28 | static void printk_stack_address(unsigned long address, int reliable, |
d438f5fd | 29 | char *log_lvl) |
878719e8 | 30 | { |
d438f5fd | 31 | touch_nmi_watchdog(); |
1fc7f61c | 32 | printk("%s [<%p>] %s%pB\n", |
d438f5fd | 33 | log_lvl, (void *)address, reliable ? "" : "? ", |
1fc7f61c | 34 | (void *)address); |
878719e8 NH |
35 | } |
36 | ||
5f01c988 JS |
37 | void printk_address(unsigned long address) |
38 | { | |
39 | pr_cont(" [<%p>] %pS\n", (void *)address, (void *)address); | |
40 | } | |
41 | ||
878719e8 NH |
42 | /* |
43 | * x86-64 can have up to three kernel stacks: | |
44 | * process stack | |
45 | * interrupt stack | |
46 | * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack | |
47 | */ | |
48 | ||
da01e18a | 49 | static inline int valid_stack_ptr(struct task_struct *task, |
878719e8 NH |
50 | void *p, unsigned int size, void *end) |
51 | { | |
aca9c293 | 52 | void *t = task_stack_page(task); |
878719e8 NH |
53 | if (end) { |
54 | if (p < end && p >= (end-THREAD_SIZE)) | |
55 | return 1; | |
56 | else | |
57 | return 0; | |
58 | } | |
9a2e9da3 | 59 | return p >= t && p < t + THREAD_SIZE - size; |
878719e8 NH |
60 | } |
61 | ||
62 | unsigned long | |
da01e18a | 63 | print_context_stack(struct task_struct *task, |
878719e8 NH |
64 | unsigned long *stack, unsigned long bp, |
65 | const struct stacktrace_ops *ops, void *data, | |
7ee991fb | 66 | unsigned long *end, int *graph) |
878719e8 NH |
67 | { |
68 | struct stack_frame *frame = (struct stack_frame *)bp; | |
69 | ||
9a2e9da3 AL |
70 | /* |
71 | * If we overflowed the stack into a guard page, jump back to the | |
72 | * bottom of the usable stack. | |
73 | */ | |
74 | if ((unsigned long)task_stack_page(task) - (unsigned long)stack < | |
75 | PAGE_SIZE) | |
76 | stack = (unsigned long *)task_stack_page(task); | |
77 | ||
da01e18a | 78 | while (valid_stack_ptr(task, stack, sizeof(*stack), end)) { |
408fe5de | 79 | unsigned long addr = *stack; |
878719e8 | 80 | |
878719e8 | 81 | if (__kernel_text_address(addr)) { |
408fe5de JP |
82 | unsigned long real_addr; |
83 | int reliable = 0; | |
84 | ||
878719e8 | 85 | if ((unsigned long) stack == bp + sizeof(long)) { |
408fe5de | 86 | reliable = 1; |
878719e8 NH |
87 | frame = frame->next_frame; |
88 | bp = (unsigned long) frame; | |
878719e8 | 89 | } |
408fe5de | 90 | |
6f727b84 JP |
91 | /* |
92 | * When function graph tracing is enabled for a | |
93 | * function, its return address on the stack is | |
94 | * replaced with the address of an ftrace handler | |
95 | * (return_to_handler). In that case, before printing | |
96 | * the "real" address, we want to print the handler | |
97 | * address as an "unreliable" hint that function graph | |
98 | * tracing was involved. | |
99 | */ | |
408fe5de JP |
100 | real_addr = ftrace_graph_ret_addr(task, graph, addr, |
101 | stack); | |
102 | if (real_addr != addr) | |
6f727b84 JP |
103 | ops->address(data, addr, 0); |
104 | ||
105 | ops->address(data, real_addr, reliable); | |
878719e8 NH |
106 | } |
107 | stack++; | |
108 | } | |
109 | return bp; | |
110 | } | |
06d65bda FW |
111 | EXPORT_SYMBOL_GPL(print_context_stack); |
112 | ||
113 | unsigned long | |
da01e18a | 114 | print_context_stack_bp(struct task_struct *task, |
06d65bda FW |
115 | unsigned long *stack, unsigned long bp, |
116 | const struct stacktrace_ops *ops, void *data, | |
117 | unsigned long *end, int *graph) | |
118 | { | |
119 | struct stack_frame *frame = (struct stack_frame *)bp; | |
408fe5de | 120 | unsigned long *retp = &frame->return_address; |
06d65bda | 121 | |
408fe5de JP |
122 | while (valid_stack_ptr(task, retp, sizeof(*retp), end)) { |
123 | unsigned long addr = *retp; | |
124 | unsigned long real_addr; | |
06d65bda | 125 | |
c2c5d45d FW |
126 | if (!__kernel_text_address(addr)) |
127 | break; | |
128 | ||
408fe5de | 129 | real_addr = ftrace_graph_ret_addr(task, graph, addr, retp); |
6f727b84 JP |
130 | if (ops->address(data, real_addr, 1)) |
131 | break; | |
408fe5de | 132 | |
c2c5d45d | 133 | frame = frame->next_frame; |
408fe5de | 134 | retp = &frame->return_address; |
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 | */ | |
568b329a | 150 | static int print_trace_address(void *data, unsigned long addr, int reliable) |
878719e8 | 151 | { |
1fc7f61c | 152 | printk_stack_address(addr, reliable, data); |
568b329a | 153 | return 0; |
878719e8 NH |
154 | } |
155 | ||
156 | static const struct stacktrace_ops print_trace_ops = { | |
06d65bda FW |
157 | .stack = print_trace_stack, |
158 | .address = print_trace_address, | |
61c1917f | 159 | .walk_stack = print_context_stack, |
878719e8 NH |
160 | }; |
161 | ||
162 | void | |
163 | show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs, | |
e8e999cf | 164 | unsigned long *stack, unsigned long bp, char *log_lvl) |
878719e8 NH |
165 | { |
166 | printk("%sCall Trace:\n", log_lvl); | |
e8e999cf | 167 | dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl); |
878719e8 NH |
168 | } |
169 | ||
878719e8 NH |
170 | void show_stack(struct task_struct *task, unsigned long *sp) |
171 | { | |
a77f2a4e | 172 | unsigned long bp = 0; |
a77f2a4e TH |
173 | |
174 | /* | |
175 | * Stack frames below this one aren't interesting. Don't show them | |
176 | * if we're printing for %current. | |
177 | */ | |
178 | if (!sp && (!task || task == current)) { | |
4b8afafb JP |
179 | sp = get_stack_pointer(current, NULL); |
180 | bp = (unsigned long)get_frame_pointer(current, NULL); | |
a77f2a4e TH |
181 | } |
182 | ||
183 | show_stack_log_lvl(task, NULL, sp, bp, ""); | |
878719e8 NH |
184 | } |
185 | ||
81c2949f BP |
186 | void show_stack_regs(struct pt_regs *regs) |
187 | { | |
5a8ff54c | 188 | show_stack_log_lvl(current, regs, NULL, 0, ""); |
81c2949f BP |
189 | } |
190 | ||
edc35bd7 | 191 | static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED; |
878719e8 NH |
192 | static int die_owner = -1; |
193 | static unsigned int die_nest_count; | |
194 | ||
9326638c | 195 | unsigned long oops_begin(void) |
878719e8 NH |
196 | { |
197 | int cpu; | |
198 | unsigned long flags; | |
199 | ||
200 | oops_enter(); | |
201 | ||
202 | /* racy, but better than risking deadlock. */ | |
203 | raw_local_irq_save(flags); | |
204 | cpu = smp_processor_id(); | |
0199c4e6 | 205 | if (!arch_spin_trylock(&die_lock)) { |
878719e8 NH |
206 | if (cpu == die_owner) |
207 | /* nested oops. should stop eventually */; | |
208 | else | |
0199c4e6 | 209 | arch_spin_lock(&die_lock); |
878719e8 NH |
210 | } |
211 | die_nest_count++; | |
212 | die_owner = cpu; | |
213 | console_verbose(); | |
214 | bust_spinlocks(1); | |
215 | return flags; | |
216 | } | |
81e88fdc | 217 | EXPORT_SYMBOL_GPL(oops_begin); |
9326638c | 218 | NOKPROBE_SYMBOL(oops_begin); |
878719e8 | 219 | |
2deb4be2 AL |
220 | void __noreturn rewind_stack_do_exit(int signr); |
221 | ||
9326638c | 222 | void oops_end(unsigned long flags, struct pt_regs *regs, int signr) |
878719e8 NH |
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"); | |
2deb4be2 AL |
243 | |
244 | /* | |
245 | * We're not going to return, but we might be on an IST stack or | |
246 | * have very little stack space left. Rewind the stack and kill | |
247 | * the task. | |
248 | */ | |
249 | rewind_stack_do_exit(signr); | |
878719e8 | 250 | } |
9326638c | 251 | NOKPROBE_SYMBOL(oops_end); |
878719e8 | 252 | |
9326638c | 253 | int __die(const char *str, struct pt_regs *regs, long err) |
878719e8 NH |
254 | { |
255 | #ifdef CONFIG_X86_32 | |
256 | unsigned short ss; | |
257 | unsigned long sp; | |
258 | #endif | |
b0f4c4b3 | 259 | printk(KERN_DEFAULT |
8fad7ec5 RV |
260 | "%s: %04lx [#%d]%s%s%s%s\n", str, err & 0xffff, ++die_counter, |
261 | IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : "", | |
262 | IS_ENABLED(CONFIG_SMP) ? " SMP" : "", | |
263 | debug_pagealloc_enabled() ? " DEBUG_PAGEALLOC" : "", | |
264 | IS_ENABLED(CONFIG_KASAN) ? " KASAN" : ""); | |
265 | ||
878719e8 | 266 | if (notify_die(DIE_OOPS, str, regs, err, |
51e7dc70 | 267 | current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP) |
878719e8 NH |
268 | return 1; |
269 | ||
0fa0e2f0 | 270 | print_modules(); |
57da8b96 | 271 | show_regs(regs); |
878719e8 | 272 | #ifdef CONFIG_X86_32 |
f39b6f0e | 273 | if (user_mode(regs)) { |
878719e8 NH |
274 | sp = regs->sp; |
275 | ss = regs->ss & 0xffff; | |
a343c75d PA |
276 | } else { |
277 | sp = kernel_stack_pointer(regs); | |
278 | savesegment(ss, ss); | |
878719e8 NH |
279 | } |
280 | printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip); | |
281 | print_symbol("%s", regs->ip); | |
282 | printk(" SS:ESP %04x:%08lx\n", ss, sp); | |
283 | #else | |
284 | /* Executive summary in case the oops scrolled away */ | |
285 | printk(KERN_ALERT "RIP "); | |
5f01c988 | 286 | printk_address(regs->ip); |
878719e8 NH |
287 | printk(" RSP <%016lx>\n", regs->sp); |
288 | #endif | |
289 | return 0; | |
290 | } | |
9326638c | 291 | NOKPROBE_SYMBOL(__die); |
878719e8 NH |
292 | |
293 | /* | |
294 | * This is gone through when something in the kernel has done something bad | |
295 | * and is about to be terminated: | |
296 | */ | |
297 | void die(const char *str, struct pt_regs *regs, long err) | |
298 | { | |
299 | unsigned long flags = oops_begin(); | |
300 | int sig = SIGSEGV; | |
301 | ||
f39b6f0e | 302 | if (!user_mode(regs)) |
878719e8 NH |
303 | report_bug(regs->ip, regs); |
304 | ||
305 | if (__die(str, regs, err)) | |
306 | sig = 0; | |
307 | oops_end(flags, regs, sig); | |
308 | } | |
309 | ||
878719e8 NH |
310 | static int __init kstack_setup(char *s) |
311 | { | |
363f7ce3 SK |
312 | ssize_t ret; |
313 | unsigned long val; | |
314 | ||
878719e8 NH |
315 | if (!s) |
316 | return -EINVAL; | |
363f7ce3 SK |
317 | |
318 | ret = kstrtoul(s, 0, &val); | |
319 | if (ret) | |
320 | return ret; | |
321 | kstack_depth_to_print = val; | |
878719e8 NH |
322 | return 0; |
323 | } | |
324 | early_param("kstack", kstack_setup); | |
325 | ||
326 | static int __init code_bytes_setup(char *s) | |
327 | { | |
363f7ce3 SK |
328 | ssize_t ret; |
329 | unsigned long val; | |
330 | ||
331 | if (!s) | |
332 | return -EINVAL; | |
333 | ||
334 | ret = kstrtoul(s, 0, &val); | |
335 | if (ret) | |
336 | return ret; | |
337 | ||
338 | code_bytes = val; | |
878719e8 NH |
339 | if (code_bytes > 8192) |
340 | code_bytes = 8192; | |
341 | ||
342 | return 1; | |
343 | } | |
344 | __setup("code_bytes=", code_bytes_setup); |