xref: /openbmc/linux/arch/arm/kernel/unwind.c (revision 1fa6ac37)
1 /*
2  * arch/arm/kernel/unwind.c
3  *
4  * Copyright (C) 2008 ARM Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  *
20  * Stack unwinding support for ARM
21  *
22  * An ARM EABI version of gcc is required to generate the unwind
23  * tables. For information about the structure of the unwind tables,
24  * see "Exception Handling ABI for the ARM Architecture" at:
25  *
26  * http://infocenter.arm.com/help/topic/com.arm.doc.subset.swdev.abi/index.html
27  */
28 
29 #ifndef __CHECKER__
30 #if !defined (__ARM_EABI__)
31 #warning Your compiler does not have EABI support.
32 #warning    ARM unwind is known to compile only with EABI compilers.
33 #warning    Change compiler or disable ARM_UNWIND option.
34 #elif (__GNUC__ == 4 && __GNUC_MINOR__ <= 2)
35 #warning Your compiler is too buggy; it is known to not compile ARM unwind support.
36 #warning    Change compiler or disable ARM_UNWIND option.
37 #endif
38 #endif /* __CHECKER__ */
39 
40 #include <linux/kernel.h>
41 #include <linux/init.h>
42 #include <linux/module.h>
43 #include <linux/sched.h>
44 #include <linux/slab.h>
45 #include <linux/spinlock.h>
46 #include <linux/list.h>
47 
48 #include <asm/stacktrace.h>
49 #include <asm/traps.h>
50 #include <asm/unwind.h>
51 
52 /* Dummy functions to avoid linker complaints */
53 void __aeabi_unwind_cpp_pr0(void)
54 {
55 };
56 EXPORT_SYMBOL(__aeabi_unwind_cpp_pr0);
57 
58 void __aeabi_unwind_cpp_pr1(void)
59 {
60 };
61 EXPORT_SYMBOL(__aeabi_unwind_cpp_pr1);
62 
63 void __aeabi_unwind_cpp_pr2(void)
64 {
65 };
66 EXPORT_SYMBOL(__aeabi_unwind_cpp_pr2);
67 
68 struct unwind_ctrl_block {
69 	unsigned long vrs[16];		/* virtual register set */
70 	unsigned long *insn;		/* pointer to the current instructions word */
71 	int entries;			/* number of entries left to interpret */
72 	int byte;			/* current byte number in the instructions word */
73 };
74 
75 enum regs {
76 #ifdef CONFIG_THUMB2_KERNEL
77 	FP = 7,
78 #else
79 	FP = 11,
80 #endif
81 	SP = 13,
82 	LR = 14,
83 	PC = 15
84 };
85 
86 extern struct unwind_idx __start_unwind_idx[];
87 extern struct unwind_idx __stop_unwind_idx[];
88 
89 static DEFINE_SPINLOCK(unwind_lock);
90 static LIST_HEAD(unwind_tables);
91 
92 /* Convert a prel31 symbol to an absolute address */
93 #define prel31_to_addr(ptr)				\
94 ({							\
95 	/* sign-extend to 32 bits */			\
96 	long offset = (((long)*(ptr)) << 1) >> 1;	\
97 	(unsigned long)(ptr) + offset;			\
98 })
99 
100 /*
101  * Binary search in the unwind index. The entries entries are
102  * guaranteed to be sorted in ascending order by the linker.
103  */
104 static struct unwind_idx *search_index(unsigned long addr,
105 				       struct unwind_idx *first,
106 				       struct unwind_idx *last)
107 {
108 	pr_debug("%s(%08lx, %p, %p)\n", __func__, addr, first, last);
109 
110 	if (addr < first->addr) {
111 		pr_warning("unwind: Unknown symbol address %08lx\n", addr);
112 		return NULL;
113 	} else if (addr >= last->addr)
114 		return last;
115 
116 	while (first < last - 1) {
117 		struct unwind_idx *mid = first + ((last - first + 1) >> 1);
118 
119 		if (addr < mid->addr)
120 			last = mid;
121 		else
122 			first = mid;
123 	}
124 
125 	return first;
126 }
127 
128 static struct unwind_idx *unwind_find_idx(unsigned long addr)
129 {
130 	struct unwind_idx *idx = NULL;
131 	unsigned long flags;
132 
133 	pr_debug("%s(%08lx)\n", __func__, addr);
134 
135 	if (core_kernel_text(addr))
136 		/* main unwind table */
137 		idx = search_index(addr, __start_unwind_idx,
138 				   __stop_unwind_idx - 1);
139 	else {
140 		/* module unwind tables */
141 		struct unwind_table *table;
142 
143 		spin_lock_irqsave(&unwind_lock, flags);
144 		list_for_each_entry(table, &unwind_tables, list) {
145 			if (addr >= table->begin_addr &&
146 			    addr < table->end_addr) {
147 				idx = search_index(addr, table->start,
148 						   table->stop - 1);
149 				break;
150 			}
151 		}
152 		spin_unlock_irqrestore(&unwind_lock, flags);
153 	}
154 
155 	pr_debug("%s: idx = %p\n", __func__, idx);
156 	return idx;
157 }
158 
159 static unsigned long unwind_get_byte(struct unwind_ctrl_block *ctrl)
160 {
161 	unsigned long ret;
162 
163 	if (ctrl->entries <= 0) {
164 		pr_warning("unwind: Corrupt unwind table\n");
165 		return 0;
166 	}
167 
168 	ret = (*ctrl->insn >> (ctrl->byte * 8)) & 0xff;
169 
170 	if (ctrl->byte == 0) {
171 		ctrl->insn++;
172 		ctrl->entries--;
173 		ctrl->byte = 3;
174 	} else
175 		ctrl->byte--;
176 
177 	return ret;
178 }
179 
180 /*
181  * Execute the current unwind instruction.
182  */
183 static int unwind_exec_insn(struct unwind_ctrl_block *ctrl)
184 {
185 	unsigned long insn = unwind_get_byte(ctrl);
186 
187 	pr_debug("%s: insn = %08lx\n", __func__, insn);
188 
189 	if ((insn & 0xc0) == 0x00)
190 		ctrl->vrs[SP] += ((insn & 0x3f) << 2) + 4;
191 	else if ((insn & 0xc0) == 0x40)
192 		ctrl->vrs[SP] -= ((insn & 0x3f) << 2) + 4;
193 	else if ((insn & 0xf0) == 0x80) {
194 		unsigned long mask;
195 		unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
196 		int load_sp, reg = 4;
197 
198 		insn = (insn << 8) | unwind_get_byte(ctrl);
199 		mask = insn & 0x0fff;
200 		if (mask == 0) {
201 			pr_warning("unwind: 'Refuse to unwind' instruction %04lx\n",
202 				   insn);
203 			return -URC_FAILURE;
204 		}
205 
206 		/* pop R4-R15 according to mask */
207 		load_sp = mask & (1 << (13 - 4));
208 		while (mask) {
209 			if (mask & 1)
210 				ctrl->vrs[reg] = *vsp++;
211 			mask >>= 1;
212 			reg++;
213 		}
214 		if (!load_sp)
215 			ctrl->vrs[SP] = (unsigned long)vsp;
216 	} else if ((insn & 0xf0) == 0x90 &&
217 		   (insn & 0x0d) != 0x0d)
218 		ctrl->vrs[SP] = ctrl->vrs[insn & 0x0f];
219 	else if ((insn & 0xf0) == 0xa0) {
220 		unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
221 		int reg;
222 
223 		/* pop R4-R[4+bbb] */
224 		for (reg = 4; reg <= 4 + (insn & 7); reg++)
225 			ctrl->vrs[reg] = *vsp++;
226 		if (insn & 0x80)
227 			ctrl->vrs[14] = *vsp++;
228 		ctrl->vrs[SP] = (unsigned long)vsp;
229 	} else if (insn == 0xb0) {
230 		if (ctrl->vrs[PC] == 0)
231 			ctrl->vrs[PC] = ctrl->vrs[LR];
232 		/* no further processing */
233 		ctrl->entries = 0;
234 	} else if (insn == 0xb1) {
235 		unsigned long mask = unwind_get_byte(ctrl);
236 		unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
237 		int reg = 0;
238 
239 		if (mask == 0 || mask & 0xf0) {
240 			pr_warning("unwind: Spare encoding %04lx\n",
241 			       (insn << 8) | mask);
242 			return -URC_FAILURE;
243 		}
244 
245 		/* pop R0-R3 according to mask */
246 		while (mask) {
247 			if (mask & 1)
248 				ctrl->vrs[reg] = *vsp++;
249 			mask >>= 1;
250 			reg++;
251 		}
252 		ctrl->vrs[SP] = (unsigned long)vsp;
253 	} else if (insn == 0xb2) {
254 		unsigned long uleb128 = unwind_get_byte(ctrl);
255 
256 		ctrl->vrs[SP] += 0x204 + (uleb128 << 2);
257 	} else {
258 		pr_warning("unwind: Unhandled instruction %02lx\n", insn);
259 		return -URC_FAILURE;
260 	}
261 
262 	pr_debug("%s: fp = %08lx sp = %08lx lr = %08lx pc = %08lx\n", __func__,
263 		 ctrl->vrs[FP], ctrl->vrs[SP], ctrl->vrs[LR], ctrl->vrs[PC]);
264 
265 	return URC_OK;
266 }
267 
268 /*
269  * Unwind a single frame starting with *sp for the symbol at *pc. It
270  * updates the *pc and *sp with the new values.
271  */
272 int unwind_frame(struct stackframe *frame)
273 {
274 	unsigned long high, low;
275 	struct unwind_idx *idx;
276 	struct unwind_ctrl_block ctrl;
277 
278 	/* only go to a higher address on the stack */
279 	low = frame->sp;
280 	high = ALIGN(low, THREAD_SIZE) + THREAD_SIZE;
281 
282 	pr_debug("%s(pc = %08lx lr = %08lx sp = %08lx)\n", __func__,
283 		 frame->pc, frame->lr, frame->sp);
284 
285 	if (!kernel_text_address(frame->pc))
286 		return -URC_FAILURE;
287 
288 	idx = unwind_find_idx(frame->pc);
289 	if (!idx) {
290 		pr_warning("unwind: Index not found %08lx\n", frame->pc);
291 		return -URC_FAILURE;
292 	}
293 
294 	ctrl.vrs[FP] = frame->fp;
295 	ctrl.vrs[SP] = frame->sp;
296 	ctrl.vrs[LR] = frame->lr;
297 	ctrl.vrs[PC] = 0;
298 
299 	if (idx->insn == 1)
300 		/* can't unwind */
301 		return -URC_FAILURE;
302 	else if ((idx->insn & 0x80000000) == 0)
303 		/* prel31 to the unwind table */
304 		ctrl.insn = (unsigned long *)prel31_to_addr(&idx->insn);
305 	else if ((idx->insn & 0xff000000) == 0x80000000)
306 		/* only personality routine 0 supported in the index */
307 		ctrl.insn = &idx->insn;
308 	else {
309 		pr_warning("unwind: Unsupported personality routine %08lx in the index at %p\n",
310 			   idx->insn, idx);
311 		return -URC_FAILURE;
312 	}
313 
314 	/* check the personality routine */
315 	if ((*ctrl.insn & 0xff000000) == 0x80000000) {
316 		ctrl.byte = 2;
317 		ctrl.entries = 1;
318 	} else if ((*ctrl.insn & 0xff000000) == 0x81000000) {
319 		ctrl.byte = 1;
320 		ctrl.entries = 1 + ((*ctrl.insn & 0x00ff0000) >> 16);
321 	} else {
322 		pr_warning("unwind: Unsupported personality routine %08lx at %p\n",
323 			   *ctrl.insn, ctrl.insn);
324 		return -URC_FAILURE;
325 	}
326 
327 	while (ctrl.entries > 0) {
328 		int urc = unwind_exec_insn(&ctrl);
329 		if (urc < 0)
330 			return urc;
331 		if (ctrl.vrs[SP] < low || ctrl.vrs[SP] >= high)
332 			return -URC_FAILURE;
333 	}
334 
335 	if (ctrl.vrs[PC] == 0)
336 		ctrl.vrs[PC] = ctrl.vrs[LR];
337 
338 	/* check for infinite loop */
339 	if (frame->pc == ctrl.vrs[PC])
340 		return -URC_FAILURE;
341 
342 	frame->fp = ctrl.vrs[FP];
343 	frame->sp = ctrl.vrs[SP];
344 	frame->lr = ctrl.vrs[LR];
345 	frame->pc = ctrl.vrs[PC];
346 
347 	return URC_OK;
348 }
349 
350 void unwind_backtrace(struct pt_regs *regs, struct task_struct *tsk)
351 {
352 	struct stackframe frame;
353 	register unsigned long current_sp asm ("sp");
354 
355 	pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
356 
357 	if (!tsk)
358 		tsk = current;
359 
360 	if (regs) {
361 		frame.fp = regs->ARM_fp;
362 		frame.sp = regs->ARM_sp;
363 		frame.lr = regs->ARM_lr;
364 		/* PC might be corrupted, use LR in that case. */
365 		frame.pc = kernel_text_address(regs->ARM_pc)
366 			 ? regs->ARM_pc : regs->ARM_lr;
367 	} else if (tsk == current) {
368 		frame.fp = (unsigned long)__builtin_frame_address(0);
369 		frame.sp = current_sp;
370 		frame.lr = (unsigned long)__builtin_return_address(0);
371 		frame.pc = (unsigned long)unwind_backtrace;
372 	} else {
373 		/* task blocked in __switch_to */
374 		frame.fp = thread_saved_fp(tsk);
375 		frame.sp = thread_saved_sp(tsk);
376 		/*
377 		 * The function calling __switch_to cannot be a leaf function
378 		 * so LR is recovered from the stack.
379 		 */
380 		frame.lr = 0;
381 		frame.pc = thread_saved_pc(tsk);
382 	}
383 
384 	while (1) {
385 		int urc;
386 		unsigned long where = frame.pc;
387 
388 		urc = unwind_frame(&frame);
389 		if (urc < 0)
390 			break;
391 		dump_backtrace_entry(where, frame.pc, frame.sp - 4);
392 	}
393 }
394 
395 struct unwind_table *unwind_table_add(unsigned long start, unsigned long size,
396 				      unsigned long text_addr,
397 				      unsigned long text_size)
398 {
399 	unsigned long flags;
400 	struct unwind_idx *idx;
401 	struct unwind_table *tab = kmalloc(sizeof(*tab), GFP_KERNEL);
402 
403 	pr_debug("%s(%08lx, %08lx, %08lx, %08lx)\n", __func__, start, size,
404 		 text_addr, text_size);
405 
406 	if (!tab)
407 		return tab;
408 
409 	tab->start = (struct unwind_idx *)start;
410 	tab->stop = (struct unwind_idx *)(start + size);
411 	tab->begin_addr = text_addr;
412 	tab->end_addr = text_addr + text_size;
413 
414 	/* Convert the symbol addresses to absolute values */
415 	for (idx = tab->start; idx < tab->stop; idx++)
416 		idx->addr = prel31_to_addr(&idx->addr);
417 
418 	spin_lock_irqsave(&unwind_lock, flags);
419 	list_add_tail(&tab->list, &unwind_tables);
420 	spin_unlock_irqrestore(&unwind_lock, flags);
421 
422 	return tab;
423 }
424 
425 void unwind_table_del(struct unwind_table *tab)
426 {
427 	unsigned long flags;
428 
429 	if (!tab)
430 		return;
431 
432 	spin_lock_irqsave(&unwind_lock, flags);
433 	list_del(&tab->list);
434 	spin_unlock_irqrestore(&unwind_lock, flags);
435 
436 	kfree(tab);
437 }
438 
439 int __init unwind_init(void)
440 {
441 	struct unwind_idx *idx;
442 
443 	/* Convert the symbol addresses to absolute values */
444 	for (idx = __start_unwind_idx; idx < __stop_unwind_idx; idx++)
445 		idx->addr = prel31_to_addr(&idx->addr);
446 
447 	pr_debug("unwind: ARM stack unwinding initialised\n");
448 
449 	return 0;
450 }
451