1
2
3
4
5#include <linux/kallsyms.h>
6#include <linux/kprobes.h>
7#include <linux/uaccess.h>
8#include <linux/hardirq.h>
9#include <linux/kdebug.h>
10#include <linux/module.h>
11#include <linux/ptrace.h>
12#include <linux/kexec.h>
13#include <linux/bug.h>
14#include <linux/nmi.h>
15#include <linux/sysfs.h>
16
17#include <asm/stacktrace.h>
18
19#include "dumpstack.h"
20
21
22static char x86_stack_ids[][8] = {
23 [DEBUG_STACK - 1] = "#DB",
24 [NMI_STACK - 1] = "NMI",
25 [DOUBLEFAULT_STACK - 1] = "#DF",
26 [STACKFAULT_STACK - 1] = "#SS",
27 [MCE_STACK - 1] = "#MC",
28#if DEBUG_STKSZ > EXCEPTION_STKSZ
29 [N_EXCEPTION_STACKS ...
30 N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
31#endif
32 };
33
34int x86_is_stack_id(int id, char *name)
35{
36 return x86_stack_ids[id - 1] == name;
37}
38
39static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
40 unsigned *usedp, char **idp)
41{
42 unsigned k;
43
44
45
46
47
48 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
49 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
50
51
52
53
54 if (stack >= end)
55 continue;
56
57
58
59
60 if (stack >= end - EXCEPTION_STKSZ) {
61
62
63
64
65
66
67 if (*usedp & (1U << k))
68 break;
69 *usedp |= 1U << k;
70 *idp = x86_stack_ids[k];
71 return (unsigned long *)end;
72 }
73
74
75
76
77
78#if DEBUG_STKSZ > EXCEPTION_STKSZ
79 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
80 unsigned j = N_EXCEPTION_STACKS - 1;
81
82
83
84
85
86
87 do {
88 ++j;
89 end -= EXCEPTION_STKSZ;
90 x86_stack_ids[j][4] = '1' +
91 (j - N_EXCEPTION_STACKS);
92 } while (stack < end - EXCEPTION_STKSZ);
93 if (*usedp & (1U << j))
94 break;
95 *usedp |= 1U << j;
96 *idp = x86_stack_ids[j];
97 return (unsigned long *)end;
98 }
99#endif
100 }
101 return NULL;
102}
103
104
105
106
107
108
109
110
111void dump_trace(struct task_struct *task, struct pt_regs *regs,
112 unsigned long *stack, unsigned long bp,
113 const struct stacktrace_ops *ops, void *data)
114{
115 const unsigned cpu = get_cpu();
116 unsigned long *irq_stack_end =
117 (unsigned long *)per_cpu(irq_stack_ptr, cpu);
118 unsigned used = 0;
119 struct thread_info *tinfo;
120 int graph = 0;
121
122 if (!task)
123 task = current;
124
125 if (!stack) {
126 unsigned long dummy;
127 stack = &dummy;
128 if (task && task != current)
129 stack = (unsigned long *)task->thread.sp;
130 }
131
132#ifdef CONFIG_FRAME_POINTER
133 if (!bp) {
134 if (task == current) {
135
136 get_bp(bp);
137 } else {
138
139 bp = *(unsigned long *) task->thread.sp;
140 }
141 }
142#endif
143
144
145
146
147
148
149 tinfo = task_thread_info(task);
150 for (;;) {
151 char *id;
152 unsigned long *estack_end;
153 estack_end = in_exception_stack(cpu, (unsigned long)stack,
154 &used, &id);
155
156 if (estack_end) {
157 if (ops->stack(data, id) < 0)
158 break;
159
160 bp = print_context_stack(tinfo, stack, bp, ops,
161 data, estack_end, &graph);
162 ops->stack(data, "<EOE>");
163
164
165
166
167
168 stack = (unsigned long *) estack_end[-2];
169 continue;
170 }
171 if (irq_stack_end) {
172 unsigned long *irq_stack;
173 irq_stack = irq_stack_end -
174 (IRQ_STACK_SIZE - 64) / sizeof(*irq_stack);
175
176 if (stack >= irq_stack && stack < irq_stack_end) {
177 if (ops->stack(data, "IRQ") < 0)
178 break;
179 bp = print_context_stack(tinfo, stack, bp,
180 ops, data, irq_stack_end, &graph);
181
182
183
184
185
186 stack = (unsigned long *) (irq_stack_end[-1]);
187 irq_stack_end = NULL;
188 ops->stack(data, "EOI");
189 continue;
190 }
191 }
192 break;
193 }
194
195
196
197
198 bp = print_context_stack(tinfo, stack, bp, ops, data, NULL, &graph);
199 put_cpu();
200}
201EXPORT_SYMBOL(dump_trace);
202
203void
204show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
205 unsigned long *sp, unsigned long bp, char *log_lvl)
206{
207 unsigned long *stack;
208 int i;
209 const int cpu = smp_processor_id();
210 unsigned long *irq_stack_end =
211 (unsigned long *)(per_cpu(irq_stack_ptr, cpu));
212 unsigned long *irq_stack =
213 (unsigned long *)(per_cpu(irq_stack_ptr, cpu) - IRQ_STACK_SIZE);
214
215
216
217
218
219
220 if (sp == NULL) {
221 if (task)
222 sp = (unsigned long *)task->thread.sp;
223 else
224 sp = (unsigned long *)&sp;
225 }
226
227 stack = sp;
228 for (i = 0; i < kstack_depth_to_print; i++) {
229 if (stack >= irq_stack && stack <= irq_stack_end) {
230 if (stack == irq_stack_end) {
231 stack = (unsigned long *) (irq_stack_end[-1]);
232 printk(" <EOI> ");
233 }
234 } else {
235 if (((long) stack & (THREAD_SIZE-1)) == 0)
236 break;
237 }
238 if (i && ((i % STACKSLOTS_PER_LINE) == 0))
239 printk("\n%s", log_lvl);
240 printk(" %016lx", *stack++);
241 touch_nmi_watchdog();
242 }
243 printk("\n");
244 show_trace_log_lvl(task, regs, sp, bp, log_lvl);
245}
246
247void show_registers(struct pt_regs *regs)
248{
249 int i;
250 unsigned long sp;
251 const int cpu = smp_processor_id();
252 struct task_struct *cur = current;
253
254 sp = regs->sp;
255 printk("CPU %d ", cpu);
256 __show_regs(regs, 1);
257 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
258 cur->comm, cur->pid, task_thread_info(cur), cur);
259
260
261
262
263
264 if (!user_mode(regs)) {
265 unsigned int code_prologue = code_bytes * 43 / 64;
266 unsigned int code_len = code_bytes;
267 unsigned char c;
268 u8 *ip;
269
270 printk(KERN_EMERG "Stack:\n");
271 show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
272 regs->bp, KERN_EMERG);
273
274 printk(KERN_EMERG "Code: ");
275
276 ip = (u8 *)regs->ip - code_prologue;
277 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
278
279 ip = (u8 *)regs->ip;
280 code_len = code_len - code_prologue + 1;
281 }
282 for (i = 0; i < code_len; i++, ip++) {
283 if (ip < (u8 *)PAGE_OFFSET ||
284 probe_kernel_address(ip, c)) {
285 printk(" Bad RIP value.");
286 break;
287 }
288 if (ip == (u8 *)regs->ip)
289 printk("<%02x> ", c);
290 else
291 printk("%02x ", c);
292 }
293 }
294 printk("\n");
295}
296
297int is_valid_bugaddr(unsigned long ip)
298{
299 unsigned short ud2;
300
301 if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
302 return 0;
303
304 return ud2 == 0x0b0f;
305}
306
307