blob: d558a8a49016b2c6eccb50f3685cc136fdc621ad [file] [log] [blame]
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +02001/*
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>
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +02008#include <linux/hardirq.h>
9#include <linux/kdebug.h>
10#include <linux/module.h>
11#include <linux/ptrace.h>
12#include <linux/kexec.h>
Ingo Molnarb8030902009-11-26 08:17:31 +010013#include <linux/sysfs.h>
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +020014#include <linux/bug.h>
15#include <linux/nmi.h>
16
17#include <asm/stacktrace.h>
18
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +020019
Ingo Molnarb8030902009-11-26 08:17:31 +010020#define N_EXCEPTION_STACKS_END \
21 (N_EXCEPTION_STACKS + DEBUG_STKSZ/EXCEPTION_STKSZ - 2)
Frederic Weisbecker0406ca62009-07-01 21:02:09 +020022
23static char x86_stack_ids[][8] = {
Ingo Molnarb8030902009-11-26 08:17:31 +010024 [ DEBUG_STACK-1 ] = "#DB",
25 [ NMI_STACK-1 ] = "NMI",
26 [ DOUBLEFAULT_STACK-1 ] = "#DF",
Ingo Molnarb8030902009-11-26 08:17:31 +010027 [ MCE_STACK-1 ] = "#MC",
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +020028#if DEBUG_STKSZ > EXCEPTION_STKSZ
Ingo Molnarb8030902009-11-26 08:17:31 +010029 [ N_EXCEPTION_STACKS ...
30 N_EXCEPTION_STACKS_END ] = "#DB[?]"
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +020031#endif
Ingo Molnarb8030902009-11-26 08:17:31 +010032};
Frederic Weisbecker0406ca62009-07-01 21:02:09 +020033
Frederic Weisbecker0406ca62009-07-01 21:02:09 +020034static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
Ingo Molnarb8030902009-11-26 08:17:31 +010035 unsigned *usedp, char **idp)
Frederic Weisbecker0406ca62009-07-01 21:02:09 +020036{
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +020037 unsigned k;
38
39 /*
40 * Iterate over all exception stacks, and figure out whether
41 * 'stack' is in one of them:
42 */
43 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
44 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
45 /*
46 * Is 'stack' above this exception frame's end?
47 * If yes then skip to the next frame.
48 */
49 if (stack >= end)
50 continue;
51 /*
52 * Is 'stack' above this exception frame's start address?
53 * If yes then we found the right frame.
54 */
55 if (stack >= end - EXCEPTION_STKSZ) {
56 /*
57 * Make sure we only iterate through an exception
58 * stack once. If it comes up for the second time
59 * then there's something wrong going on - just
60 * break out and return NULL:
61 */
62 if (*usedp & (1U << k))
63 break;
64 *usedp |= 1U << k;
Frederic Weisbecker0406ca62009-07-01 21:02:09 +020065 *idp = x86_stack_ids[k];
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +020066 return (unsigned long *)end;
67 }
68 /*
69 * If this is a debug stack, and if it has a larger size than
70 * the usual exception stacks, then 'stack' might still
71 * be within the lower portion of the debug stack:
72 */
73#if DEBUG_STKSZ > EXCEPTION_STKSZ
74 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
75 unsigned j = N_EXCEPTION_STACKS - 1;
76
77 /*
78 * Black magic. A large debug stack is composed of
79 * multiple exception stack entries, which we
80 * iterate through now. Dont look:
81 */
82 do {
83 ++j;
84 end -= EXCEPTION_STKSZ;
Frederic Weisbecker0406ca62009-07-01 21:02:09 +020085 x86_stack_ids[j][4] = '1' +
86 (j - N_EXCEPTION_STACKS);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +020087 } while (stack < end - EXCEPTION_STKSZ);
88 if (*usedp & (1U << j))
89 break;
90 *usedp |= 1U << j;
Frederic Weisbecker0406ca62009-07-01 21:02:09 +020091 *idp = x86_stack_ids[j];
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +020092 return (unsigned long *)end;
93 }
94#endif
95 }
96 return NULL;
97}
98
Frederic Weisbeckeraf2d8282009-12-06 05:34:27 +010099static inline int
100in_irq_stack(unsigned long *stack, unsigned long *irq_stack,
101 unsigned long *irq_stack_end)
102{
103 return (stack >= irq_stack && stack < irq_stack_end);
104}
105
Steven Rostedt2223f6f2014-02-06 09:41:32 -0500106static const unsigned long irq_stack_size =
107 (IRQ_STACK_SIZE - 64) / sizeof(unsigned long);
108
109enum stack_type {
110 STACK_IS_UNKNOWN,
111 STACK_IS_NORMAL,
112 STACK_IS_EXCEPTION,
113 STACK_IS_IRQ,
114};
115
116static enum stack_type
Steven Rostedt (Red Hat)1aabc592014-04-02 13:26:40 -0400117analyze_stack(int cpu, struct task_struct *task, unsigned long *stack,
Steven Rostedt (Red Hat)63c956542014-04-02 13:26:41 -0400118 unsigned long **stack_end, unsigned long *irq_stack,
119 unsigned *used, char **id)
Steven Rostedt2223f6f2014-02-06 09:41:32 -0500120{
Steven Rostedt2223f6f2014-02-06 09:41:32 -0500121 unsigned long addr;
Steven Rostedt2223f6f2014-02-06 09:41:32 -0500122
123 addr = ((unsigned long)stack & (~(THREAD_SIZE - 1)));
124 if ((unsigned long)task_stack_page(task) == addr)
125 return STACK_IS_NORMAL;
126
127 *stack_end = in_exception_stack(cpu, (unsigned long)stack,
Steven Rostedt (Red Hat)1aabc592014-04-02 13:26:40 -0400128 used, id);
Steven Rostedt2223f6f2014-02-06 09:41:32 -0500129 if (*stack_end)
130 return STACK_IS_EXCEPTION;
131
Steven Rostedt (Red Hat)63c956542014-04-02 13:26:41 -0400132 if (!irq_stack)
133 return STACK_IS_NORMAL;
Steven Rostedt2223f6f2014-02-06 09:41:32 -0500134
Steven Rostedt (Red Hat)63c956542014-04-02 13:26:41 -0400135 *stack_end = irq_stack;
136 irq_stack = irq_stack - irq_stack_size;
Steven Rostedt2223f6f2014-02-06 09:41:32 -0500137
138 if (in_irq_stack(stack, irq_stack, *stack_end))
139 return STACK_IS_IRQ;
140
141 return STACK_IS_UNKNOWN;
142}
143
Frederic Weisbeckeraf2d8282009-12-06 05:34:27 +0100144/*
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200145 * x86-64 can have up to three kernel stacks:
146 * process stack
147 * interrupt stack
148 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
149 */
150
Namhyung Kime8e999cf2011-03-18 11:40:06 +0900151void dump_trace(struct task_struct *task, struct pt_regs *regs,
152 unsigned long *stack, unsigned long bp,
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200153 const struct stacktrace_ops *ops, void *data)
154{
155 const unsigned cpu = get_cpu();
Steven Rostedt (Red Hat)63c956542014-04-02 13:26:41 -0400156 unsigned long *irq_stack = (unsigned long *)per_cpu(irq_stack_ptr, cpu);
Jesper Juhl2e5aa682011-01-24 22:41:11 +0100157 unsigned long dummy;
Steven Rostedt (Red Hat)1aabc592014-04-02 13:26:40 -0400158 unsigned used = 0;
Steven Rostedt2223f6f2014-02-06 09:41:32 -0500159 int graph = 0;
160 int done = 0;
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200161
162 if (!task)
163 task = current;
164
165 if (!stack) {
Frederic Weisbecker47ce11a2011-06-30 19:04:56 +0200166 if (regs)
167 stack = (unsigned long *)regs->sp;
Dan Carpenterd0caf2922012-01-28 13:52:46 +0300168 else if (task != current)
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200169 stack = (unsigned long *)task->thread.sp;
Frederic Weisbecker47ce11a2011-06-30 19:04:56 +0200170 else
171 stack = &dummy;
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200172 }
173
Namhyung Kime8e999cf2011-03-18 11:40:06 +0900174 if (!bp)
175 bp = stack_frame(task, regs);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200176 /*
177 * Print function call entries in all stacks, starting at the
178 * current stack address. If the stacks consist of nested
179 * exceptions
180 */
Steven Rostedt2223f6f2014-02-06 09:41:32 -0500181 while (!done) {
182 unsigned long *stack_end;
183 enum stack_type stype;
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200184 char *id;
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200185
Steven Rostedt (Red Hat)63c956542014-04-02 13:26:41 -0400186 stype = analyze_stack(cpu, task, stack, &stack_end,
187 irq_stack, &used, &id);
Steven Rostedt2223f6f2014-02-06 09:41:32 -0500188
189 /* Default finish unless specified to continue */
190 done = 1;
191
192 switch (stype) {
193
194 /* Break out early if we are on the thread stack */
195 case STACK_IS_NORMAL:
196 break;
197
198 case STACK_IS_EXCEPTION:
199
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200200 if (ops->stack(data, id) < 0)
201 break;
202
Linus Torvaldsda01e182016-06-23 12:20:01 -0700203 bp = ops->walk_stack(task, stack, bp, ops,
Steven Rostedt2223f6f2014-02-06 09:41:32 -0500204 data, stack_end, &graph);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200205 ops->stack(data, "<EOE>");
206 /*
207 * We link to the next stack via the
208 * second-to-last pointer (index -2 to end) in the
209 * exception stack:
210 */
Steven Rostedt2223f6f2014-02-06 09:41:32 -0500211 stack = (unsigned long *) stack_end[-2];
212 done = 0;
213 break;
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200214
Steven Rostedt2223f6f2014-02-06 09:41:32 -0500215 case STACK_IS_IRQ:
216
217 if (ops->stack(data, "IRQ") < 0)
218 break;
Linus Torvaldsda01e182016-06-23 12:20:01 -0700219 bp = ops->walk_stack(task, stack, bp,
Steven Rostedt2223f6f2014-02-06 09:41:32 -0500220 ops, data, stack_end, &graph);
221 /*
222 * We link to the next stack (which would be
223 * the process stack normally) the last
224 * pointer (index -1 to end) in the IRQ stack:
225 */
226 stack = (unsigned long *) (stack_end[-1]);
Steven Rostedt (Red Hat)63c956542014-04-02 13:26:41 -0400227 irq_stack = NULL;
Steven Rostedt2223f6f2014-02-06 09:41:32 -0500228 ops->stack(data, "EOI");
229 done = 0;
230 break;
231
232 case STACK_IS_UNKNOWN:
233 ops->stack(data, "UNK");
234 break;
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200235 }
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200236 }
237
238 /*
239 * This handles the process stack:
240 */
Linus Torvaldsda01e182016-06-23 12:20:01 -0700241 bp = ops->walk_stack(task, stack, bp, ops, data, NULL, &graph);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200242 put_cpu();
243}
244EXPORT_SYMBOL(dump_trace);
245
Neil Horman878719e2008-10-23 10:40:06 -0400246void
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200247show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
Namhyung Kime8e999cf2011-03-18 11:40:06 +0900248 unsigned long *sp, unsigned long bp, char *log_lvl)
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200249{
Ingo Molnar67f2de02009-11-26 08:29:10 +0100250 unsigned long *irq_stack_end;
251 unsigned long *irq_stack;
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200252 unsigned long *stack;
Ingo Molnar67f2de02009-11-26 08:29:10 +0100253 int cpu;
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200254 int i;
Ingo Molnar67f2de02009-11-26 08:29:10 +0100255
256 preempt_disable();
257 cpu = smp_processor_id();
258
259 irq_stack_end = (unsigned long *)(per_cpu(irq_stack_ptr, cpu));
260 irq_stack = (unsigned long *)(per_cpu(irq_stack_ptr, cpu) - IRQ_STACK_SIZE);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200261
262 /*
Ingo Molnar67f2de02009-11-26 08:29:10 +0100263 * Debugging aid: "show_stack(NULL, NULL);" prints the
264 * back trace for this cpu:
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200265 */
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200266 if (sp == NULL) {
267 if (task)
268 sp = (unsigned long *)task->thread.sp;
269 else
270 sp = (unsigned long *)&sp;
271 }
272
273 stack = sp;
274 for (i = 0; i < kstack_depth_to_print; i++) {
Brian Gerst26f80bd2009-01-19 00:38:58 +0900275 if (stack >= irq_stack && stack <= irq_stack_end) {
276 if (stack == irq_stack_end) {
277 stack = (unsigned long *) (irq_stack_end[-1]);
Joe Perchesc767a542012-05-21 19:50:07 -0700278 pr_cont(" <EOI> ");
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200279 }
280 } else {
Adrien Schildknecht04769ae2015-02-22 16:23:58 +0100281 if (kstack_end(stack))
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200282 break;
283 }
Adrien Schildknecht1fc7f612015-02-20 03:34:21 +0100284 if ((i % STACKSLOTS_PER_LINE) == 0) {
285 if (i != 0)
286 pr_cont("\n");
287 printk("%s %016lx", log_lvl, *stack++);
288 } else
289 pr_cont(" %016lx", *stack++);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200290 touch_nmi_watchdog();
291 }
Ingo Molnar67f2de02009-11-26 08:29:10 +0100292 preempt_enable();
293
Joe Perchesc767a542012-05-21 19:50:07 -0700294 pr_cont("\n");
Namhyung Kime8e999cf2011-03-18 11:40:06 +0900295 show_trace_log_lvl(task, regs, sp, bp, log_lvl);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200296}
297
Jan Beulich57da8b92012-05-09 08:47:37 +0100298void show_regs(struct pt_regs *regs)
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200299{
300 int i;
301 unsigned long sp;
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200302
303 sp = regs->sp;
Tejun Heoa43cb952013-04-30 15:27:17 -0700304 show_regs_print_info(KERN_DEFAULT);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200305 __show_regs(regs, 1);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200306
307 /*
308 * When in-kernel, we also print out the stack and code at the
309 * time of the fault..
310 */
311 if (!user_mode(regs)) {
312 unsigned int code_prologue = code_bytes * 43 / 64;
313 unsigned int code_len = code_bytes;
314 unsigned char c;
315 u8 *ip;
316
Prarit Bhargavab0f4c4b2012-01-26 08:55:34 -0500317 printk(KERN_DEFAULT "Stack:\n");
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200318 show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
Prarit Bhargavab0f4c4b2012-01-26 08:55:34 -0500319 0, KERN_DEFAULT);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200320
Prarit Bhargavab0f4c4b2012-01-26 08:55:34 -0500321 printk(KERN_DEFAULT "Code: ");
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200322
323 ip = (u8 *)regs->ip - code_prologue;
324 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
Alexander van Heukelum8a541662008-10-04 23:12:46 +0200325 /* try starting at IP */
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200326 ip = (u8 *)regs->ip;
327 code_len = code_len - code_prologue + 1;
328 }
329 for (i = 0; i < code_len; i++, ip++) {
330 if (ip < (u8 *)PAGE_OFFSET ||
331 probe_kernel_address(ip, c)) {
Joe Perchesc767a542012-05-21 19:50:07 -0700332 pr_cont(" Bad RIP value.");
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200333 break;
334 }
335 if (ip == (u8 *)regs->ip)
Joe Perchesc767a542012-05-21 19:50:07 -0700336 pr_cont("<%02x> ", c);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200337 else
Joe Perchesc767a542012-05-21 19:50:07 -0700338 pr_cont("%02x ", c);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200339 }
340 }
Joe Perchesc767a542012-05-21 19:50:07 -0700341 pr_cont("\n");
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200342}
343
344int is_valid_bugaddr(unsigned long ip)
345{
346 unsigned short ud2;
347
348 if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
349 return 0;
350
351 return ud2 == 0x0b0f;
352}