Staging: hv: mousevsc: Cleanup alloc_input_device()
[zen-stable.git] / arch / arm / kernel / unwind.c
blobd2cb0b3c987216b4b7eea39289b739fbc7d0136d
1 /*
2 * arch/arm/kernel/unwind.c
4 * Copyright (C) 2008 ARM Limited
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.
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.
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
20 * Stack unwinding support for ARM
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:
26 * http://infocenter.arm.com/help/topic/com.arm.doc.subset.swdev.abi/index.html
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__ */
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>
48 #include <asm/stacktrace.h>
49 #include <asm/traps.h>
50 #include <asm/unwind.h>
52 /* Dummy functions to avoid linker complaints */
53 void __aeabi_unwind_cpp_pr0(void)
56 EXPORT_SYMBOL(__aeabi_unwind_cpp_pr0);
58 void __aeabi_unwind_cpp_pr1(void)
61 EXPORT_SYMBOL(__aeabi_unwind_cpp_pr1);
63 void __aeabi_unwind_cpp_pr2(void)
66 EXPORT_SYMBOL(__aeabi_unwind_cpp_pr2);
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 */
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
86 extern struct unwind_idx __start_unwind_idx[];
87 extern struct unwind_idx __stop_unwind_idx[];
89 static DEFINE_SPINLOCK(unwind_lock);
90 static LIST_HEAD(unwind_tables);
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; \
101 * Binary search in the unwind index. The entries entries are
102 * guaranteed to be sorted in ascending order by the linker.
104 static struct unwind_idx *search_index(unsigned long addr,
105 struct unwind_idx *first,
106 struct unwind_idx *last)
108 pr_debug("%s(%08lx, %p, %p)\n", __func__, addr, first, last);
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;
116 while (first < last - 1) {
117 struct unwind_idx *mid = first + ((last - first + 1) >> 1);
119 if (addr < mid->addr)
120 last = mid;
121 else
122 first = mid;
125 return first;
128 static struct unwind_idx *unwind_find_idx(unsigned long addr)
130 struct unwind_idx *idx = NULL;
131 unsigned long flags;
133 pr_debug("%s(%08lx)\n", __func__, addr);
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;
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 /* Move-to-front to exploit common traces */
150 list_move(&table->list, &unwind_tables);
151 break;
154 spin_unlock_irqrestore(&unwind_lock, flags);
157 pr_debug("%s: idx = %p\n", __func__, idx);
158 return idx;
161 static unsigned long unwind_get_byte(struct unwind_ctrl_block *ctrl)
163 unsigned long ret;
165 if (ctrl->entries <= 0) {
166 pr_warning("unwind: Corrupt unwind table\n");
167 return 0;
170 ret = (*ctrl->insn >> (ctrl->byte * 8)) & 0xff;
172 if (ctrl->byte == 0) {
173 ctrl->insn++;
174 ctrl->entries--;
175 ctrl->byte = 3;
176 } else
177 ctrl->byte--;
179 return ret;
183 * Execute the current unwind instruction.
185 static int unwind_exec_insn(struct unwind_ctrl_block *ctrl)
187 unsigned long insn = unwind_get_byte(ctrl);
189 pr_debug("%s: insn = %08lx\n", __func__, insn);
191 if ((insn & 0xc0) == 0x00)
192 ctrl->vrs[SP] += ((insn & 0x3f) << 2) + 4;
193 else if ((insn & 0xc0) == 0x40)
194 ctrl->vrs[SP] -= ((insn & 0x3f) << 2) + 4;
195 else if ((insn & 0xf0) == 0x80) {
196 unsigned long mask;
197 unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
198 int load_sp, reg = 4;
200 insn = (insn << 8) | unwind_get_byte(ctrl);
201 mask = insn & 0x0fff;
202 if (mask == 0) {
203 pr_warning("unwind: 'Refuse to unwind' instruction %04lx\n",
204 insn);
205 return -URC_FAILURE;
208 /* pop R4-R15 according to mask */
209 load_sp = mask & (1 << (13 - 4));
210 while (mask) {
211 if (mask & 1)
212 ctrl->vrs[reg] = *vsp++;
213 mask >>= 1;
214 reg++;
216 if (!load_sp)
217 ctrl->vrs[SP] = (unsigned long)vsp;
218 } else if ((insn & 0xf0) == 0x90 &&
219 (insn & 0x0d) != 0x0d)
220 ctrl->vrs[SP] = ctrl->vrs[insn & 0x0f];
221 else if ((insn & 0xf0) == 0xa0) {
222 unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
223 int reg;
225 /* pop R4-R[4+bbb] */
226 for (reg = 4; reg <= 4 + (insn & 7); reg++)
227 ctrl->vrs[reg] = *vsp++;
228 if (insn & 0x80)
229 ctrl->vrs[14] = *vsp++;
230 ctrl->vrs[SP] = (unsigned long)vsp;
231 } else if (insn == 0xb0) {
232 if (ctrl->vrs[PC] == 0)
233 ctrl->vrs[PC] = ctrl->vrs[LR];
234 /* no further processing */
235 ctrl->entries = 0;
236 } else if (insn == 0xb1) {
237 unsigned long mask = unwind_get_byte(ctrl);
238 unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
239 int reg = 0;
241 if (mask == 0 || mask & 0xf0) {
242 pr_warning("unwind: Spare encoding %04lx\n",
243 (insn << 8) | mask);
244 return -URC_FAILURE;
247 /* pop R0-R3 according to mask */
248 while (mask) {
249 if (mask & 1)
250 ctrl->vrs[reg] = *vsp++;
251 mask >>= 1;
252 reg++;
254 ctrl->vrs[SP] = (unsigned long)vsp;
255 } else if (insn == 0xb2) {
256 unsigned long uleb128 = unwind_get_byte(ctrl);
258 ctrl->vrs[SP] += 0x204 + (uleb128 << 2);
259 } else {
260 pr_warning("unwind: Unhandled instruction %02lx\n", insn);
261 return -URC_FAILURE;
264 pr_debug("%s: fp = %08lx sp = %08lx lr = %08lx pc = %08lx\n", __func__,
265 ctrl->vrs[FP], ctrl->vrs[SP], ctrl->vrs[LR], ctrl->vrs[PC]);
267 return URC_OK;
271 * Unwind a single frame starting with *sp for the symbol at *pc. It
272 * updates the *pc and *sp with the new values.
274 int unwind_frame(struct stackframe *frame)
276 unsigned long high, low;
277 struct unwind_idx *idx;
278 struct unwind_ctrl_block ctrl;
280 /* only go to a higher address on the stack */
281 low = frame->sp;
282 high = ALIGN(low, THREAD_SIZE);
284 pr_debug("%s(pc = %08lx lr = %08lx sp = %08lx)\n", __func__,
285 frame->pc, frame->lr, frame->sp);
287 if (!kernel_text_address(frame->pc))
288 return -URC_FAILURE;
290 idx = unwind_find_idx(frame->pc);
291 if (!idx) {
292 pr_warning("unwind: Index not found %08lx\n", frame->pc);
293 return -URC_FAILURE;
296 ctrl.vrs[FP] = frame->fp;
297 ctrl.vrs[SP] = frame->sp;
298 ctrl.vrs[LR] = frame->lr;
299 ctrl.vrs[PC] = 0;
301 if (idx->insn == 1)
302 /* can't unwind */
303 return -URC_FAILURE;
304 else if ((idx->insn & 0x80000000) == 0)
305 /* prel31 to the unwind table */
306 ctrl.insn = (unsigned long *)prel31_to_addr(&idx->insn);
307 else if ((idx->insn & 0xff000000) == 0x80000000)
308 /* only personality routine 0 supported in the index */
309 ctrl.insn = &idx->insn;
310 else {
311 pr_warning("unwind: Unsupported personality routine %08lx in the index at %p\n",
312 idx->insn, idx);
313 return -URC_FAILURE;
316 /* check the personality routine */
317 if ((*ctrl.insn & 0xff000000) == 0x80000000) {
318 ctrl.byte = 2;
319 ctrl.entries = 1;
320 } else if ((*ctrl.insn & 0xff000000) == 0x81000000) {
321 ctrl.byte = 1;
322 ctrl.entries = 1 + ((*ctrl.insn & 0x00ff0000) >> 16);
323 } else {
324 pr_warning("unwind: Unsupported personality routine %08lx at %p\n",
325 *ctrl.insn, ctrl.insn);
326 return -URC_FAILURE;
329 while (ctrl.entries > 0) {
330 int urc = unwind_exec_insn(&ctrl);
331 if (urc < 0)
332 return urc;
333 if (ctrl.vrs[SP] < low || ctrl.vrs[SP] >= high)
334 return -URC_FAILURE;
337 if (ctrl.vrs[PC] == 0)
338 ctrl.vrs[PC] = ctrl.vrs[LR];
340 /* check for infinite loop */
341 if (frame->pc == ctrl.vrs[PC])
342 return -URC_FAILURE;
344 frame->fp = ctrl.vrs[FP];
345 frame->sp = ctrl.vrs[SP];
346 frame->lr = ctrl.vrs[LR];
347 frame->pc = ctrl.vrs[PC];
349 return URC_OK;
352 void unwind_backtrace(struct pt_regs *regs, struct task_struct *tsk)
354 struct stackframe frame;
355 register unsigned long current_sp asm ("sp");
357 pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
359 if (!tsk)
360 tsk = current;
362 if (regs) {
363 frame.fp = regs->ARM_fp;
364 frame.sp = regs->ARM_sp;
365 frame.lr = regs->ARM_lr;
366 /* PC might be corrupted, use LR in that case. */
367 frame.pc = kernel_text_address(regs->ARM_pc)
368 ? regs->ARM_pc : regs->ARM_lr;
369 } else if (tsk == current) {
370 frame.fp = (unsigned long)__builtin_frame_address(0);
371 frame.sp = current_sp;
372 frame.lr = (unsigned long)__builtin_return_address(0);
373 frame.pc = (unsigned long)unwind_backtrace;
374 } else {
375 /* task blocked in __switch_to */
376 frame.fp = thread_saved_fp(tsk);
377 frame.sp = thread_saved_sp(tsk);
379 * The function calling __switch_to cannot be a leaf function
380 * so LR is recovered from the stack.
382 frame.lr = 0;
383 frame.pc = thread_saved_pc(tsk);
386 while (1) {
387 int urc;
388 unsigned long where = frame.pc;
390 urc = unwind_frame(&frame);
391 if (urc < 0)
392 break;
393 dump_backtrace_entry(where, frame.pc, frame.sp - 4);
397 struct unwind_table *unwind_table_add(unsigned long start, unsigned long size,
398 unsigned long text_addr,
399 unsigned long text_size)
401 unsigned long flags;
402 struct unwind_idx *idx;
403 struct unwind_table *tab = kmalloc(sizeof(*tab), GFP_KERNEL);
405 pr_debug("%s(%08lx, %08lx, %08lx, %08lx)\n", __func__, start, size,
406 text_addr, text_size);
408 if (!tab)
409 return tab;
411 tab->start = (struct unwind_idx *)start;
412 tab->stop = (struct unwind_idx *)(start + size);
413 tab->begin_addr = text_addr;
414 tab->end_addr = text_addr + text_size;
416 /* Convert the symbol addresses to absolute values */
417 for (idx = tab->start; idx < tab->stop; idx++)
418 idx->addr = prel31_to_addr(&idx->addr);
420 spin_lock_irqsave(&unwind_lock, flags);
421 list_add_tail(&tab->list, &unwind_tables);
422 spin_unlock_irqrestore(&unwind_lock, flags);
424 return tab;
427 void unwind_table_del(struct unwind_table *tab)
429 unsigned long flags;
431 if (!tab)
432 return;
434 spin_lock_irqsave(&unwind_lock, flags);
435 list_del(&tab->list);
436 spin_unlock_irqrestore(&unwind_lock, flags);
438 kfree(tab);
441 int __init unwind_init(void)
443 struct unwind_idx *idx;
445 /* Convert the symbol addresses to absolute values */
446 for (idx = __start_unwind_idx; idx < __stop_unwind_idx; idx++)
447 idx->addr = prel31_to_addr(&idx->addr);
449 pr_debug("unwind: ARM stack unwinding initialised\n");
451 return 0;