xref: /linux/arch/x86/kernel/dumpstack_64.c (revision 878719e831d9e076961aa15d4049a57a6668c67a)
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>
13 #include <linux/kexec.h>
14 #include <linux/bug.h>
15 #include <linux/nmi.h>
16 #include <linux/sysfs.h>
17 
18 #include <asm/stacktrace.h>
19 
20 #include "dumpstack.h"
21 
22 static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
23 					unsigned *usedp, char **idp)
24 {
25 	static char ids[][8] = {
26 		[DEBUG_STACK - 1] = "#DB",
27 		[NMI_STACK - 1] = "NMI",
28 		[DOUBLEFAULT_STACK - 1] = "#DF",
29 		[STACKFAULT_STACK - 1] = "#SS",
30 		[MCE_STACK - 1] = "#MC",
31 #if DEBUG_STKSZ > EXCEPTION_STKSZ
32 		[N_EXCEPTION_STACKS ...
33 			N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
34 #endif
35 	};
36 	unsigned k;
37 
38 	/*
39 	 * Iterate over all exception stacks, and figure out whether
40 	 * 'stack' is in one of them:
41 	 */
42 	for (k = 0; k < N_EXCEPTION_STACKS; k++) {
43 		unsigned long end = per_cpu(orig_ist, cpu).ist[k];
44 		/*
45 		 * Is 'stack' above this exception frame's end?
46 		 * If yes then skip to the next frame.
47 		 */
48 		if (stack >= end)
49 			continue;
50 		/*
51 		 * Is 'stack' above this exception frame's start address?
52 		 * If yes then we found the right frame.
53 		 */
54 		if (stack >= end - EXCEPTION_STKSZ) {
55 			/*
56 			 * Make sure we only iterate through an exception
57 			 * stack once. If it comes up for the second time
58 			 * then there's something wrong going on - just
59 			 * break out and return NULL:
60 			 */
61 			if (*usedp & (1U << k))
62 				break;
63 			*usedp |= 1U << k;
64 			*idp = ids[k];
65 			return (unsigned long *)end;
66 		}
67 		/*
68 		 * If this is a debug stack, and if it has a larger size than
69 		 * the usual exception stacks, then 'stack' might still
70 		 * be within the lower portion of the debug stack:
71 		 */
72 #if DEBUG_STKSZ > EXCEPTION_STKSZ
73 		if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
74 			unsigned j = N_EXCEPTION_STACKS - 1;
75 
76 			/*
77 			 * Black magic. A large debug stack is composed of
78 			 * multiple exception stack entries, which we
79 			 * iterate through now. Dont look:
80 			 */
81 			do {
82 				++j;
83 				end -= EXCEPTION_STKSZ;
84 				ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
85 			} while (stack < end - EXCEPTION_STKSZ);
86 			if (*usedp & (1U << j))
87 				break;
88 			*usedp |= 1U << j;
89 			*idp = ids[j];
90 			return (unsigned long *)end;
91 		}
92 #endif
93 	}
94 	return NULL;
95 }
96 
97 /*
98  * x86-64 can have up to three kernel stacks:
99  * process stack
100  * interrupt stack
101  * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
102  */
103 
104 void dump_trace(struct task_struct *task, struct pt_regs *regs,
105 		unsigned long *stack, unsigned long bp,
106 		const struct stacktrace_ops *ops, void *data)
107 {
108 	const unsigned cpu = get_cpu();
109 	unsigned long *irqstack_end = (unsigned long *)cpu_pda(cpu)->irqstackptr;
110 	unsigned used = 0;
111 	struct thread_info *tinfo;
112 
113 	if (!task)
114 		task = current;
115 
116 	if (!stack) {
117 		unsigned long dummy;
118 		stack = &dummy;
119 		if (task && task != current)
120 			stack = (unsigned long *)task->thread.sp;
121 	}
122 
123 #ifdef CONFIG_FRAME_POINTER
124 	if (!bp) {
125 		if (task == current) {
126 			/* Grab bp right from our regs */
127 			get_bp(bp);
128 		} else {
129 			/* bp is the last reg pushed by switch_to */
130 			bp = *(unsigned long *) task->thread.sp;
131 		}
132 	}
133 #endif
134 
135 	/*
136 	 * Print function call entries in all stacks, starting at the
137 	 * current stack address. If the stacks consist of nested
138 	 * exceptions
139 	 */
140 	tinfo = task_thread_info(task);
141 	for (;;) {
142 		char *id;
143 		unsigned long *estack_end;
144 		estack_end = in_exception_stack(cpu, (unsigned long)stack,
145 						&used, &id);
146 
147 		if (estack_end) {
148 			if (ops->stack(data, id) < 0)
149 				break;
150 
151 			bp = print_context_stack(tinfo, stack, bp, ops,
152 							data, estack_end);
153 			ops->stack(data, "<EOE>");
154 			/*
155 			 * We link to the next stack via the
156 			 * second-to-last pointer (index -2 to end) in the
157 			 * exception stack:
158 			 */
159 			stack = (unsigned long *) estack_end[-2];
160 			continue;
161 		}
162 		if (irqstack_end) {
163 			unsigned long *irqstack;
164 			irqstack = irqstack_end -
165 				(IRQSTACKSIZE - 64) / sizeof(*irqstack);
166 
167 			if (stack >= irqstack && stack < irqstack_end) {
168 				if (ops->stack(data, "IRQ") < 0)
169 					break;
170 				bp = print_context_stack(tinfo, stack, bp,
171 						ops, data, irqstack_end);
172 				/*
173 				 * We link to the next stack (which would be
174 				 * the process stack normally) the last
175 				 * pointer (index -1 to end) in the IRQ stack:
176 				 */
177 				stack = (unsigned long *) (irqstack_end[-1]);
178 				irqstack_end = NULL;
179 				ops->stack(data, "EOI");
180 				continue;
181 			}
182 		}
183 		break;
184 	}
185 
186 	/*
187 	 * This handles the process stack:
188 	 */
189 	bp = print_context_stack(tinfo, stack, bp, ops, data, NULL);
190 	put_cpu();
191 }
192 EXPORT_SYMBOL(dump_trace);
193 
194 void
195 show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
196 		unsigned long *sp, unsigned long bp, char *log_lvl)
197 {
198 	unsigned long *stack;
199 	int i;
200 	const int cpu = smp_processor_id();
201 	unsigned long *irqstack_end =
202 		(unsigned long *) (cpu_pda(cpu)->irqstackptr);
203 	unsigned long *irqstack =
204 		(unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
205 
206 	/*
207 	 * debugging aid: "show_stack(NULL, NULL);" prints the
208 	 * back trace for this cpu.
209 	 */
210 
211 	if (sp == NULL) {
212 		if (task)
213 			sp = (unsigned long *)task->thread.sp;
214 		else
215 			sp = (unsigned long *)&sp;
216 	}
217 
218 	stack = sp;
219 	for (i = 0; i < kstack_depth_to_print; i++) {
220 		if (stack >= irqstack && stack <= irqstack_end) {
221 			if (stack == irqstack_end) {
222 				stack = (unsigned long *) (irqstack_end[-1]);
223 				printk(" <EOI> ");
224 			}
225 		} else {
226 		if (((long) stack & (THREAD_SIZE-1)) == 0)
227 			break;
228 		}
229 		if (i && ((i % STACKSLOTS_PER_LINE) == 0))
230 			printk("\n%s", log_lvl);
231 		printk(" %016lx", *stack++);
232 		touch_nmi_watchdog();
233 	}
234 	printk("\n");
235 	show_trace_log_lvl(task, regs, sp, bp, log_lvl);
236 }
237 
238 void show_registers(struct pt_regs *regs)
239 {
240 	int i;
241 	unsigned long sp;
242 	const int cpu = smp_processor_id();
243 	struct task_struct *cur = cpu_pda(cpu)->pcurrent;
244 
245 	sp = regs->sp;
246 	printk("CPU %d ", cpu);
247 	__show_regs(regs, 1);
248 	printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
249 		cur->comm, cur->pid, task_thread_info(cur), cur);
250 
251 	/*
252 	 * When in-kernel, we also print out the stack and code at the
253 	 * time of the fault..
254 	 */
255 	if (!user_mode(regs)) {
256 		unsigned int code_prologue = code_bytes * 43 / 64;
257 		unsigned int code_len = code_bytes;
258 		unsigned char c;
259 		u8 *ip;
260 
261 		printk(KERN_EMERG "Stack:\n");
262 		show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
263 				regs->bp, KERN_EMERG);
264 
265 		printk(KERN_EMERG "Code: ");
266 
267 		ip = (u8 *)regs->ip - code_prologue;
268 		if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
269 			/* try starting at IP */
270 			ip = (u8 *)regs->ip;
271 			code_len = code_len - code_prologue + 1;
272 		}
273 		for (i = 0; i < code_len; i++, ip++) {
274 			if (ip < (u8 *)PAGE_OFFSET ||
275 					probe_kernel_address(ip, c)) {
276 				printk(" Bad RIP value.");
277 				break;
278 			}
279 			if (ip == (u8 *)regs->ip)
280 				printk("<%02x> ", c);
281 			else
282 				printk("%02x ", c);
283 		}
284 	}
285 	printk("\n");
286 }
287 
288 int is_valid_bugaddr(unsigned long ip)
289 {
290 	unsigned short ud2;
291 
292 	if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
293 		return 0;
294 
295 	return ud2 == 0x0b0f;
296 }
297 
298