x86/entry: Remap the TSS into the CPU entry area
[linux-2.6-block.git] / arch / x86 / kernel / dumpstack.c
CommitLineData
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
24int panic_on_unrecovered_nmi;
5211a242 25int panic_on_io_nmi;
878719e8 26unsigned int code_bytes = 64;
878719e8
NH
27static int die_counter;
28
cb76c939
JP
29bool 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
33a2f1a6
AL
46bool in_sysenter_stack(unsigned long *stack, struct stack_info *info)
47{
72f5e08d
AL
48 int cpu = smp_processor_id();
49 struct tss_struct *tss = &get_cpu_entry_area(cpu)->tss;
33a2f1a6
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 66static 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
b02fcf9b
JP
73void 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
80static 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
95void 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)
6e60e583 116 * - SYSENTER stack
e18bcccd 117 *
6e60e583 118 * x86-32 can have up to four stacks:
e18bcccd
JP
119 * - task stack
120 * - softirq stack
121 * - hardirq stack
6e60e583 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
6e60e583
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
b02fcf9b
JP
143 if (regs)
144 show_regs_safe(&stack_info, regs);
b0529bec 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
b02fcf9b 168 * by show_regs_safe() below.
3b3fa11b 169 */
b0529bec
JP
170 if (regs && stack == &regs->ip)
171 goto next;
3b3fa11b 172
e18bcccd
JP
173 if (stack == ret_addr_p)
174 reliable = 1;
175
176 /*
177 * When function graph tracing is enabled for a
178 * function, its return address on the stack is
179 * replaced with the address of an ftrace handler
180 * (return_to_handler). In that case, before printing
181 * the "real" address, we want to print the handler
182 * address as an "unreliable" hint that function graph
183 * tracing was involved.
184 */
185 real_addr = ftrace_graph_ret_addr(task, &graph_idx,
186 addr, stack);
187 if (real_addr != addr)
188 printk_stack_address(addr, 0, log_lvl);
189 printk_stack_address(real_addr, reliable, log_lvl);
190
191 if (!reliable)
192 continue;
193
b0529bec 194next:
e18bcccd
JP
195 /*
196 * Get the next frame from the unwinder. No need to
197 * check for an error: if anything goes wrong, the rest
198 * of the addresses will just be printed as unreliable.
199 */
200 unwind_next_frame(&state);
3b3fa11b
JP
201
202 /* if the frame has entry regs, print them */
203 regs = unwind_get_entry_regs(&state);
b02fcf9b
JP
204 if (regs)
205 show_regs_safe(&stack_info, regs);
e18bcccd
JP
206 }
207
3d02a9c4
JP
208 if (stack_name)
209 printk("%s </%s>\n", log_lvl, stack_name);
e18bcccd 210 }
878719e8
NH
211}
212
878719e8
NH
213void show_stack(struct task_struct *task, unsigned long *sp)
214{
81539169
JP
215 task = task ? : current;
216
a77f2a4e
TH
217 /*
218 * Stack frames below this one aren't interesting. Don't show them
219 * if we're printing for %current.
220 */
e18bcccd 221 if (!sp && task == current)
4b8afafb 222 sp = get_stack_pointer(current, NULL);
a77f2a4e 223
0ee1dd9f 224 show_trace_log_lvl(task, NULL, sp, KERN_DEFAULT);
878719e8
NH
225}
226
81c2949f
BP
227void show_stack_regs(struct pt_regs *regs)
228{
0ee1dd9f 229 show_trace_log_lvl(current, regs, NULL, KERN_DEFAULT);
81c2949f
BP
230}
231
edc35bd7 232static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
878719e8
NH
233static int die_owner = -1;
234static unsigned int die_nest_count;
235
9326638c 236unsigned long oops_begin(void)
878719e8
NH
237{
238 int cpu;
239 unsigned long flags;
240
241 oops_enter();
242
243 /* racy, but better than risking deadlock. */
244 raw_local_irq_save(flags);
245 cpu = smp_processor_id();
0199c4e6 246 if (!arch_spin_trylock(&die_lock)) {
878719e8
NH
247 if (cpu == die_owner)
248 /* nested oops. should stop eventually */;
249 else
0199c4e6 250 arch_spin_lock(&die_lock);
878719e8
NH
251 }
252 die_nest_count++;
253 die_owner = cpu;
254 console_verbose();
255 bust_spinlocks(1);
256 return flags;
257}
81e88fdc 258EXPORT_SYMBOL_GPL(oops_begin);
9326638c 259NOKPROBE_SYMBOL(oops_begin);
878719e8 260
2deb4be2
AL
261void __noreturn rewind_stack_do_exit(int signr);
262
9326638c 263void oops_end(unsigned long flags, struct pt_regs *regs, int signr)
878719e8
NH
264{
265 if (regs && kexec_should_crash(current))
266 crash_kexec(regs);
267
268 bust_spinlocks(0);
269 die_owner = -1;
373d4d09 270 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
878719e8
NH
271 die_nest_count--;
272 if (!die_nest_count)
273 /* Nest count reaches zero, release the lock. */
0199c4e6 274 arch_spin_unlock(&die_lock);
878719e8
NH
275 raw_local_irq_restore(flags);
276 oops_exit();
277
278 if (!signr)
279 return;
280 if (in_interrupt())
281 panic("Fatal exception in interrupt");
282 if (panic_on_oops)
283 panic("Fatal exception");
2deb4be2
AL
284
285 /*
286 * We're not going to return, but we might be on an IST stack or
287 * have very little stack space left. Rewind the stack and kill
288 * the task.
289 */
290 rewind_stack_do_exit(signr);
878719e8 291}
9326638c 292NOKPROBE_SYMBOL(oops_end);
878719e8 293
9326638c 294int __die(const char *str, struct pt_regs *regs, long err)
878719e8
NH
295{
296#ifdef CONFIG_X86_32
297 unsigned short ss;
298 unsigned long sp;
299#endif
b0f4c4b3 300 printk(KERN_DEFAULT
8fad7ec5
RV
301 "%s: %04lx [#%d]%s%s%s%s\n", str, err & 0xffff, ++die_counter,
302 IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : "",
303 IS_ENABLED(CONFIG_SMP) ? " SMP" : "",
304 debug_pagealloc_enabled() ? " DEBUG_PAGEALLOC" : "",
305 IS_ENABLED(CONFIG_KASAN) ? " KASAN" : "");
306
878719e8 307 if (notify_die(DIE_OOPS, str, regs, err,
51e7dc70 308 current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
878719e8
NH
309 return 1;
310
0fa0e2f0 311 print_modules();
57da8b96 312 show_regs(regs);
878719e8 313#ifdef CONFIG_X86_32
f39b6f0e 314 if (user_mode(regs)) {
878719e8 315 sp = regs->sp;
99504819 316 ss = regs->ss;
a343c75d
PA
317 } else {
318 sp = kernel_stack_pointer(regs);
319 savesegment(ss, ss);
878719e8 320 }
bb5e5ce5
JP
321 printk(KERN_EMERG "EIP: %pS SS:ESP: %04x:%08lx\n",
322 (void *)regs->ip, ss, sp);
878719e8
NH
323#else
324 /* Executive summary in case the oops scrolled away */
bb5e5ce5 325 printk(KERN_ALERT "RIP: %pS RSP: %016lx\n", (void *)regs->ip, regs->sp);
878719e8
NH
326#endif
327 return 0;
328}
9326638c 329NOKPROBE_SYMBOL(__die);
878719e8
NH
330
331/*
332 * This is gone through when something in the kernel has done something bad
333 * and is about to be terminated:
334 */
335void die(const char *str, struct pt_regs *regs, long err)
336{
337 unsigned long flags = oops_begin();
338 int sig = SIGSEGV;
339
878719e8
NH
340 if (__die(str, regs, err))
341 sig = 0;
342 oops_end(flags, regs, sig);
343}
344
878719e8
NH
345static int __init code_bytes_setup(char *s)
346{
363f7ce3
SK
347 ssize_t ret;
348 unsigned long val;
349
350 if (!s)
351 return -EINVAL;
352
353 ret = kstrtoul(s, 0, &val);
354 if (ret)
355 return ret;
356
357 code_bytes = val;
878719e8
NH
358 if (code_bytes > 8192)
359 code_bytes = 8192;
360
361 return 1;
362}
363__setup("code_bytes=", code_bytes_setup);