]>
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> | |
b17b0153 | 13 | #include <linux/sched/debug.h> |
68db0cf1 | 14 | #include <linux/sched/task_stack.h> |
712406a6 | 15 | #include <linux/ftrace.h> |
878719e8 NH |
16 | #include <linux/kexec.h> |
17 | #include <linux/bug.h> | |
18 | #include <linux/nmi.h> | |
19 | #include <linux/sysfs.h> | |
20 | ||
21 | #include <asm/stacktrace.h> | |
e18bcccd | 22 | #include <asm/unwind.h> |
878719e8 NH |
23 | |
24 | int panic_on_unrecovered_nmi; | |
5211a242 | 25 | int panic_on_io_nmi; |
878719e8 | 26 | unsigned int code_bytes = 64; |
878719e8 NH |
27 | static int die_counter; |
28 | ||
cb76c939 JP |
29 | bool in_task_stack(unsigned long *stack, struct task_struct *task, |
30 | struct stack_info *info) | |
31 | { | |
32 | unsigned long *begin = task_stack_page(task); | |
33 | unsigned long *end = task_stack_page(task) + THREAD_SIZE; | |
34 | ||
35 | if (stack < begin || stack >= end) | |
36 | return false; | |
37 | ||
38 | info->type = STACK_TYPE_TASK; | |
39 | info->begin = begin; | |
40 | info->end = end; | |
41 | info->next_sp = NULL; | |
42 | ||
43 | return true; | |
44 | } | |
45 | ||
72e90cc5 AL |
46 | bool in_sysenter_stack(unsigned long *stack, struct stack_info *info) |
47 | { | |
475b37e7 AL |
48 | int cpu = smp_processor_id(); |
49 | struct tss_struct *tss = &get_cpu_entry_area(cpu)->tss; | |
72e90cc5 AL |
50 | |
51 | /* Treat the canary as part of the stack for unwinding purposes. */ | |
52 | void *begin = &tss->SYSENTER_stack_canary; | |
53 | void *end = (void *)&tss->SYSENTER_stack + sizeof(tss->SYSENTER_stack); | |
54 | ||
55 | if ((void *)stack < begin || (void *)stack >= end) | |
56 | return false; | |
57 | ||
58 | info->type = STACK_TYPE_SYSENTER; | |
59 | info->begin = begin; | |
60 | info->end = end; | |
61 | info->next_sp = NULL; | |
62 | ||
63 | return true; | |
64 | } | |
65 | ||
1fc7f61c | 66 | static void printk_stack_address(unsigned long address, int reliable, |
d438f5fd | 67 | char *log_lvl) |
878719e8 | 68 | { |
d438f5fd | 69 | touch_nmi_watchdog(); |
bb5e5ce5 | 70 | printk("%s %s%pB\n", log_lvl, reliable ? "" : "? ", (void *)address); |
5f01c988 JS |
71 | } |
72 | ||
9e51f396 JP |
73 | void show_iret_regs(struct pt_regs *regs) |
74 | { | |
75 | printk(KERN_DEFAULT "RIP: %04x:%pS\n", (int)regs->cs, (void *)regs->ip); | |
76 | printk(KERN_DEFAULT "RSP: %04x:%016lx EFLAGS: %08lx", (int)regs->ss, | |
77 | regs->sp, regs->flags); | |
78 | } | |
79 | ||
80 | static void show_regs_safe(struct stack_info *info, struct pt_regs *regs) | |
81 | { | |
82 | if (on_stack(info, regs, sizeof(*regs))) | |
83 | __show_regs(regs, 0); | |
84 | else if (on_stack(info, (void *)regs + IRET_FRAME_OFFSET, | |
85 | IRET_FRAME_SIZE)) { | |
86 | /* | |
87 | * When an interrupt or exception occurs in entry code, the | |
88 | * full pt_regs might not have been saved yet. In that case | |
89 | * just print the iret frame. | |
90 | */ | |
91 | show_iret_regs(regs); | |
92 | } | |
93 | } | |
94 | ||
e18bcccd JP |
95 | void show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs, |
96 | unsigned long *stack, char *log_lvl) | |
878719e8 | 97 | { |
e18bcccd JP |
98 | struct unwind_state state; |
99 | struct stack_info stack_info = {0}; | |
100 | unsigned long visit_mask = 0; | |
101 | int graph_idx = 0; | |
878719e8 | 102 | |
e18bcccd | 103 | printk("%sCall Trace:\n", log_lvl); |
878719e8 | 104 | |
e18bcccd | 105 | unwind_start(&state, task, regs, stack); |
f4474c9f | 106 | stack = stack ? : get_stack_pointer(task, regs); |
878719e8 | 107 | |
e18bcccd JP |
108 | /* |
109 | * Iterate through the stacks, starting with the current stack pointer. | |
110 | * Each stack has a pointer to the next one. | |
111 | * | |
112 | * x86-64 can have several stacks: | |
113 | * - task stack | |
114 | * - interrupt stack | |
115 | * - HW exception stacks (double fault, nmi, debug, mce) | |
1ab51120 | 116 | * - SYSENTER stack |
e18bcccd | 117 | * |
1ab51120 | 118 | * x86-32 can have up to four stacks: |
e18bcccd JP |
119 | * - task stack |
120 | * - softirq stack | |
121 | * - hardirq stack | |
1ab51120 | 122 | * - SYSENTER stack |
e18bcccd | 123 | */ |
e335bb51 | 124 | for (regs = NULL; stack; stack = PTR_ALIGN(stack_info.next_sp, sizeof(long))) { |
3d02a9c4 | 125 | const char *stack_name; |
e18bcccd | 126 | |
1ab51120 AL |
127 | if (get_stack_info(stack, task, &stack_info, &visit_mask)) { |
128 | /* | |
129 | * We weren't on a valid stack. It's possible that | |
130 | * we overflowed a valid stack into a guard page. | |
131 | * See if the next page up is valid so that we can | |
132 | * generate some kind of backtrace if this happens. | |
133 | */ | |
134 | stack = (unsigned long *)PAGE_ALIGN((unsigned long)stack); | |
135 | if (get_stack_info(stack, task, &stack_info, &visit_mask)) | |
136 | break; | |
137 | } | |
e18bcccd | 138 | |
3d02a9c4 JP |
139 | stack_name = stack_type_name(stack_info.type); |
140 | if (stack_name) | |
141 | printk("%s <%s>\n", log_lvl, stack_name); | |
e18bcccd | 142 | |
9e51f396 JP |
143 | if (regs) |
144 | show_regs_safe(&stack_info, regs); | |
145 | ||
e18bcccd JP |
146 | /* |
147 | * Scan the stack, printing any text addresses we find. At the | |
148 | * same time, follow proper stack frames with the unwinder. | |
149 | * | |
150 | * Addresses found during the scan which are not reported by | |
151 | * the unwinder are considered to be additional clues which are | |
152 | * sometimes useful for debugging and are prefixed with '?'. | |
153 | * This also serves as a failsafe option in case the unwinder | |
154 | * goes off in the weeds. | |
155 | */ | |
156 | for (; stack < stack_info.end; stack++) { | |
157 | unsigned long real_addr; | |
158 | int reliable = 0; | |
91e08ab0 | 159 | unsigned long addr = READ_ONCE_NOCHECK(*stack); |
e18bcccd JP |
160 | unsigned long *ret_addr_p = |
161 | unwind_get_return_address_ptr(&state); | |
162 | ||
163 | if (!__kernel_text_address(addr)) | |
164 | continue; | |
165 | ||
3b3fa11b JP |
166 | /* |
167 | * Don't print regs->ip again if it was already printed | |
9e51f396 | 168 | * by show_regs_safe() below. |
3b3fa11b JP |
169 | */ |
170 | if (regs && stack == ®s->ip) { | |
171 | unwind_next_frame(&state); | |
172 | continue; | |
173 | } | |
174 | ||
e18bcccd JP |
175 | if (stack == ret_addr_p) |
176 | reliable = 1; | |
177 | ||
178 | /* | |
179 | * When function graph tracing is enabled for a | |
180 | * function, its return address on the stack is | |
181 | * replaced with the address of an ftrace handler | |
182 | * (return_to_handler). In that case, before printing | |
183 | * the "real" address, we want to print the handler | |
184 | * address as an "unreliable" hint that function graph | |
185 | * tracing was involved. | |
186 | */ | |
187 | real_addr = ftrace_graph_ret_addr(task, &graph_idx, | |
188 | addr, stack); | |
189 | if (real_addr != addr) | |
190 | printk_stack_address(addr, 0, log_lvl); | |
191 | printk_stack_address(real_addr, reliable, log_lvl); | |
192 | ||
193 | if (!reliable) | |
194 | continue; | |
195 | ||
196 | /* | |
197 | * Get the next frame from the unwinder. No need to | |
198 | * check for an error: if anything goes wrong, the rest | |
199 | * of the addresses will just be printed as unreliable. | |
200 | */ | |
201 | unwind_next_frame(&state); | |
3b3fa11b JP |
202 | |
203 | /* if the frame has entry regs, print them */ | |
204 | regs = unwind_get_entry_regs(&state); | |
205 | if (regs) | |
9e51f396 | 206 | show_regs_safe(&stack_info, regs); |
e18bcccd JP |
207 | } |
208 | ||
3d02a9c4 JP |
209 | if (stack_name) |
210 | printk("%s </%s>\n", log_lvl, stack_name); | |
e18bcccd | 211 | } |
878719e8 NH |
212 | } |
213 | ||
878719e8 NH |
214 | void show_stack(struct task_struct *task, unsigned long *sp) |
215 | { | |
81539169 JP |
216 | task = task ? : current; |
217 | ||
a77f2a4e TH |
218 | /* |
219 | * Stack frames below this one aren't interesting. Don't show them | |
220 | * if we're printing for %current. | |
221 | */ | |
e18bcccd | 222 | if (!sp && task == current) |
4b8afafb | 223 | sp = get_stack_pointer(current, NULL); |
a77f2a4e | 224 | |
0ee1dd9f | 225 | show_trace_log_lvl(task, NULL, sp, KERN_DEFAULT); |
878719e8 NH |
226 | } |
227 | ||
81c2949f BP |
228 | void show_stack_regs(struct pt_regs *regs) |
229 | { | |
0ee1dd9f | 230 | show_trace_log_lvl(current, regs, NULL, KERN_DEFAULT); |
81c2949f BP |
231 | } |
232 | ||
edc35bd7 | 233 | static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED; |
878719e8 NH |
234 | static int die_owner = -1; |
235 | static unsigned int die_nest_count; | |
236 | ||
9326638c | 237 | unsigned long oops_begin(void) |
878719e8 NH |
238 | { |
239 | int cpu; | |
240 | unsigned long flags; | |
241 | ||
242 | oops_enter(); | |
243 | ||
244 | /* racy, but better than risking deadlock. */ | |
245 | raw_local_irq_save(flags); | |
246 | cpu = smp_processor_id(); | |
0199c4e6 | 247 | if (!arch_spin_trylock(&die_lock)) { |
878719e8 NH |
248 | if (cpu == die_owner) |
249 | /* nested oops. should stop eventually */; | |
250 | else | |
0199c4e6 | 251 | arch_spin_lock(&die_lock); |
878719e8 NH |
252 | } |
253 | die_nest_count++; | |
254 | die_owner = cpu; | |
255 | console_verbose(); | |
256 | bust_spinlocks(1); | |
257 | return flags; | |
258 | } | |
81e88fdc | 259 | EXPORT_SYMBOL_GPL(oops_begin); |
9326638c | 260 | NOKPROBE_SYMBOL(oops_begin); |
878719e8 | 261 | |
2deb4be2 AL |
262 | void __noreturn rewind_stack_do_exit(int signr); |
263 | ||
9326638c | 264 | void oops_end(unsigned long flags, struct pt_regs *regs, int signr) |
878719e8 NH |
265 | { |
266 | if (regs && kexec_should_crash(current)) | |
267 | crash_kexec(regs); | |
268 | ||
269 | bust_spinlocks(0); | |
270 | die_owner = -1; | |
373d4d09 | 271 | add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE); |
878719e8 NH |
272 | die_nest_count--; |
273 | if (!die_nest_count) | |
274 | /* Nest count reaches zero, release the lock. */ | |
0199c4e6 | 275 | arch_spin_unlock(&die_lock); |
878719e8 NH |
276 | raw_local_irq_restore(flags); |
277 | oops_exit(); | |
278 | ||
279 | if (!signr) | |
280 | return; | |
281 | if (in_interrupt()) | |
282 | panic("Fatal exception in interrupt"); | |
283 | if (panic_on_oops) | |
284 | panic("Fatal exception"); | |
2deb4be2 AL |
285 | |
286 | /* | |
287 | * We're not going to return, but we might be on an IST stack or | |
288 | * have very little stack space left. Rewind the stack and kill | |
289 | * the task. | |
290 | */ | |
291 | rewind_stack_do_exit(signr); | |
878719e8 | 292 | } |
9326638c | 293 | NOKPROBE_SYMBOL(oops_end); |
878719e8 | 294 | |
9326638c | 295 | int __die(const char *str, struct pt_regs *regs, long err) |
878719e8 NH |
296 | { |
297 | #ifdef CONFIG_X86_32 | |
298 | unsigned short ss; | |
299 | unsigned long sp; | |
300 | #endif | |
b0f4c4b3 | 301 | printk(KERN_DEFAULT |
8fad7ec5 RV |
302 | "%s: %04lx [#%d]%s%s%s%s\n", str, err & 0xffff, ++die_counter, |
303 | IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : "", | |
304 | IS_ENABLED(CONFIG_SMP) ? " SMP" : "", | |
305 | debug_pagealloc_enabled() ? " DEBUG_PAGEALLOC" : "", | |
306 | IS_ENABLED(CONFIG_KASAN) ? " KASAN" : ""); | |
307 | ||
878719e8 | 308 | if (notify_die(DIE_OOPS, str, regs, err, |
51e7dc70 | 309 | current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP) |
878719e8 NH |
310 | return 1; |
311 | ||
0fa0e2f0 | 312 | print_modules(); |
57da8b96 | 313 | show_regs(regs); |
878719e8 | 314 | #ifdef CONFIG_X86_32 |
f39b6f0e | 315 | if (user_mode(regs)) { |
878719e8 NH |
316 | sp = regs->sp; |
317 | ss = regs->ss & 0xffff; | |
a343c75d PA |
318 | } else { |
319 | sp = kernel_stack_pointer(regs); | |
320 | savesegment(ss, ss); | |
878719e8 | 321 | } |
bb5e5ce5 JP |
322 | printk(KERN_EMERG "EIP: %pS SS:ESP: %04x:%08lx\n", |
323 | (void *)regs->ip, ss, sp); | |
878719e8 NH |
324 | #else |
325 | /* Executive summary in case the oops scrolled away */ | |
bb5e5ce5 | 326 | printk(KERN_ALERT "RIP: %pS RSP: %016lx\n", (void *)regs->ip, regs->sp); |
878719e8 NH |
327 | #endif |
328 | return 0; | |
329 | } | |
9326638c | 330 | NOKPROBE_SYMBOL(__die); |
878719e8 NH |
331 | |
332 | /* | |
333 | * This is gone through when something in the kernel has done something bad | |
334 | * and is about to be terminated: | |
335 | */ | |
336 | void die(const char *str, struct pt_regs *regs, long err) | |
337 | { | |
338 | unsigned long flags = oops_begin(); | |
339 | int sig = SIGSEGV; | |
340 | ||
878719e8 NH |
341 | if (__die(str, regs, err)) |
342 | sig = 0; | |
343 | oops_end(flags, regs, sig); | |
344 | } | |
345 | ||
878719e8 NH |
346 | static int __init code_bytes_setup(char *s) |
347 | { | |
363f7ce3 SK |
348 | ssize_t ret; |
349 | unsigned long val; | |
350 | ||
351 | if (!s) | |
352 | return -EINVAL; | |
353 | ||
354 | ret = kstrtoul(s, 0, &val); | |
355 | if (ret) | |
356 | return ret; | |
357 | ||
358 | code_bytes = val; | |
878719e8 NH |
359 | if (code_bytes > 8192) |
360 | code_bytes = 8192; | |
361 | ||
362 | return 1; | |
363 | } | |
364 | __setup("code_bytes=", code_bytes_setup); |