unwind.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. #include <linux/kernel.h>
  29. #include <linux/init.h>
  30. #include <linux/module.h>
  31. #include <linux/sched.h>
  32. #include <linux/slab.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/list.h>
  35. #include <asm/stacktrace.h>
  36. #include <asm/traps.h>
  37. #include <asm/unwind.h>
  38. /* Dummy functions to avoid linker complaints */
  39. void __aeabi_unwind_cpp_pr0(void)
  40. {
  41. };
  42. EXPORT_SYMBOL(__aeabi_unwind_cpp_pr0);
  43. void __aeabi_unwind_cpp_pr1(void)
  44. {
  45. };
  46. EXPORT_SYMBOL(__aeabi_unwind_cpp_pr1);
  47. void __aeabi_unwind_cpp_pr2(void)
  48. {
  49. };
  50. EXPORT_SYMBOL(__aeabi_unwind_cpp_pr2);
  51. struct unwind_ctrl_block {
  52. unsigned long vrs[16]; /* virtual register set */
  53. unsigned long *insn; /* pointer to the current instructions word */
  54. int entries; /* number of entries left to interpret */
  55. int byte; /* current byte number in the instructions word */
  56. };
  57. enum regs {
  58. #ifdef CONFIG_THUMB2_KERNEL
  59. FP = 7,
  60. #else
  61. FP = 11,
  62. #endif
  63. SP = 13,
  64. LR = 14,
  65. PC = 15
  66. };
  67. extern struct unwind_idx __start_unwind_idx[];
  68. extern struct unwind_idx __stop_unwind_idx[];
  69. static DEFINE_SPINLOCK(unwind_lock);
  70. static LIST_HEAD(unwind_tables);
  71. /* Convert a prel31 symbol to an absolute address */
  72. #define prel31_to_addr(ptr) \
  73. ({ \
  74. /* sign-extend to 32 bits */ \
  75. long offset = (((long)*(ptr)) << 1) >> 1; \
  76. (unsigned long)(ptr) + offset; \
  77. })
  78. /*
  79. * Binary search in the unwind index. The entries entries are
  80. * guaranteed to be sorted in ascending order by the linker.
  81. */
  82. static struct unwind_idx *search_index(unsigned long addr,
  83. struct unwind_idx *first,
  84. struct unwind_idx *last)
  85. {
  86. pr_debug("%s(%08lx, %p, %p)\n", __func__, addr, first, last);
  87. if (addr < first->addr) {
  88. pr_warning("unwind: Unknown symbol address %08lx\n", addr);
  89. return NULL;
  90. } else if (addr >= last->addr)
  91. return last;
  92. while (first < last - 1) {
  93. struct unwind_idx *mid = first + ((last - first + 1) >> 1);
  94. if (addr < mid->addr)
  95. last = mid;
  96. else
  97. first = mid;
  98. }
  99. return first;
  100. }
  101. static struct unwind_idx *unwind_find_idx(unsigned long addr)
  102. {
  103. struct unwind_idx *idx = NULL;
  104. unsigned long flags;
  105. pr_debug("%s(%08lx)\n", __func__, addr);
  106. if (core_kernel_text(addr))
  107. /* main unwind table */
  108. idx = search_index(addr, __start_unwind_idx,
  109. __stop_unwind_idx - 1);
  110. else {
  111. /* module unwind tables */
  112. struct unwind_table *table;
  113. spin_lock_irqsave(&unwind_lock, flags);
  114. list_for_each_entry(table, &unwind_tables, list) {
  115. if (addr >= table->begin_addr &&
  116. addr < table->end_addr) {
  117. idx = search_index(addr, table->start,
  118. table->stop - 1);
  119. break;
  120. }
  121. }
  122. spin_unlock_irqrestore(&unwind_lock, flags);
  123. }
  124. pr_debug("%s: idx = %p\n", __func__, idx);
  125. return idx;
  126. }
  127. static unsigned long unwind_get_byte(struct unwind_ctrl_block *ctrl)
  128. {
  129. unsigned long ret;
  130. if (ctrl->entries <= 0) {
  131. pr_warning("unwind: Corrupt unwind table\n");
  132. return 0;
  133. }
  134. ret = (*ctrl->insn >> (ctrl->byte * 8)) & 0xff;
  135. if (ctrl->byte == 0) {
  136. ctrl->insn++;
  137. ctrl->entries--;
  138. ctrl->byte = 3;
  139. } else
  140. ctrl->byte--;
  141. return ret;
  142. }
  143. /*
  144. * Execute the current unwind instruction.
  145. */
  146. static int unwind_exec_insn(struct unwind_ctrl_block *ctrl)
  147. {
  148. unsigned long insn = unwind_get_byte(ctrl);
  149. pr_debug("%s: insn = %08lx\n", __func__, insn);
  150. if ((insn & 0xc0) == 0x00)
  151. ctrl->vrs[SP] += ((insn & 0x3f) << 2) + 4;
  152. else if ((insn & 0xc0) == 0x40)
  153. ctrl->vrs[SP] -= ((insn & 0x3f) << 2) + 4;
  154. else if ((insn & 0xf0) == 0x80) {
  155. unsigned long mask;
  156. unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
  157. int load_sp, reg = 4;
  158. insn = (insn << 8) | unwind_get_byte(ctrl);
  159. mask = insn & 0x0fff;
  160. if (mask == 0) {
  161. pr_warning("unwind: 'Refuse to unwind' instruction %04lx\n",
  162. insn);
  163. return -URC_FAILURE;
  164. }
  165. /* pop R4-R15 according to mask */
  166. load_sp = mask & (1 << (13 - 4));
  167. while (mask) {
  168. if (mask & 1)
  169. ctrl->vrs[reg] = *vsp++;
  170. mask >>= 1;
  171. reg++;
  172. }
  173. if (!load_sp)
  174. ctrl->vrs[SP] = (unsigned long)vsp;
  175. } else if ((insn & 0xf0) == 0x90 &&
  176. (insn & 0x0d) != 0x0d)
  177. ctrl->vrs[SP] = ctrl->vrs[insn & 0x0f];
  178. else if ((insn & 0xf0) == 0xa0) {
  179. unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
  180. int reg;
  181. /* pop R4-R[4+bbb] */
  182. for (reg = 4; reg <= 4 + (insn & 7); reg++)
  183. ctrl->vrs[reg] = *vsp++;
  184. if (insn & 0x80)
  185. ctrl->vrs[14] = *vsp++;
  186. ctrl->vrs[SP] = (unsigned long)vsp;
  187. } else if (insn == 0xb0) {
  188. if (ctrl->vrs[PC] == 0)
  189. ctrl->vrs[PC] = ctrl->vrs[LR];
  190. /* no further processing */
  191. ctrl->entries = 0;
  192. } else if (insn == 0xb1) {
  193. unsigned long mask = unwind_get_byte(ctrl);
  194. unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
  195. int reg = 0;
  196. if (mask == 0 || mask & 0xf0) {
  197. pr_warning("unwind: Spare encoding %04lx\n",
  198. (insn << 8) | mask);
  199. return -URC_FAILURE;
  200. }
  201. /* pop R0-R3 according to mask */
  202. while (mask) {
  203. if (mask & 1)
  204. ctrl->vrs[reg] = *vsp++;
  205. mask >>= 1;
  206. reg++;
  207. }
  208. ctrl->vrs[SP] = (unsigned long)vsp;
  209. } else if (insn == 0xb2) {
  210. unsigned long uleb128 = unwind_get_byte(ctrl);
  211. ctrl->vrs[SP] += 0x204 + (uleb128 << 2);
  212. } else {
  213. pr_warning("unwind: Unhandled instruction %02lx\n", insn);
  214. return -URC_FAILURE;
  215. }
  216. pr_debug("%s: fp = %08lx sp = %08lx lr = %08lx pc = %08lx\n", __func__,
  217. ctrl->vrs[FP], ctrl->vrs[SP], ctrl->vrs[LR], ctrl->vrs[PC]);
  218. return URC_OK;
  219. }
  220. /*
  221. * Unwind a single frame starting with *sp for the symbol at *pc. It
  222. * updates the *pc and *sp with the new values.
  223. */
  224. int unwind_frame(struct stackframe *frame)
  225. {
  226. unsigned long high, low;
  227. struct unwind_idx *idx;
  228. struct unwind_ctrl_block ctrl;
  229. /* only go to a higher address on the stack */
  230. low = frame->sp;
  231. high = ALIGN(low, THREAD_SIZE) + THREAD_SIZE;
  232. pr_debug("%s(pc = %08lx lr = %08lx sp = %08lx)\n", __func__,
  233. frame->pc, frame->lr, frame->sp);
  234. if (!kernel_text_address(frame->pc))
  235. return -URC_FAILURE;
  236. idx = unwind_find_idx(frame->pc);
  237. if (!idx) {
  238. pr_warning("unwind: Index not found %08lx\n", frame->pc);
  239. return -URC_FAILURE;
  240. }
  241. ctrl.vrs[FP] = frame->fp;
  242. ctrl.vrs[SP] = frame->sp;
  243. ctrl.vrs[LR] = frame->lr;
  244. ctrl.vrs[PC] = 0;
  245. if (idx->insn == 1)
  246. /* can't unwind */
  247. return -URC_FAILURE;
  248. else if ((idx->insn & 0x80000000) == 0)
  249. /* prel31 to the unwind table */
  250. ctrl.insn = (unsigned long *)prel31_to_addr(&idx->insn);
  251. else if ((idx->insn & 0xff000000) == 0x80000000)
  252. /* only personality routine 0 supported in the index */
  253. ctrl.insn = &idx->insn;
  254. else {
  255. pr_warning("unwind: Unsupported personality routine %08lx in the index at %p\n",
  256. idx->insn, idx);
  257. return -URC_FAILURE;
  258. }
  259. /* check the personality routine */
  260. if ((*ctrl.insn & 0xff000000) == 0x80000000) {
  261. ctrl.byte = 2;
  262. ctrl.entries = 1;
  263. } else if ((*ctrl.insn & 0xff000000) == 0x81000000) {
  264. ctrl.byte = 1;
  265. ctrl.entries = 1 + ((*ctrl.insn & 0x00ff0000) >> 16);
  266. } else {
  267. pr_warning("unwind: Unsupported personality routine %08lx at %p\n",
  268. *ctrl.insn, ctrl.insn);
  269. return -URC_FAILURE;
  270. }
  271. while (ctrl.entries > 0) {
  272. int urc = unwind_exec_insn(&ctrl);
  273. if (urc < 0)
  274. return urc;
  275. if (ctrl.vrs[SP] < low || ctrl.vrs[SP] >= high)
  276. return -URC_FAILURE;
  277. }
  278. if (ctrl.vrs[PC] == 0)
  279. ctrl.vrs[PC] = ctrl.vrs[LR];
  280. /* check for infinite loop */
  281. if (frame->pc == ctrl.vrs[PC])
  282. return -URC_FAILURE;
  283. frame->fp = ctrl.vrs[FP];
  284. frame->sp = ctrl.vrs[SP];
  285. frame->lr = ctrl.vrs[LR];
  286. frame->pc = ctrl.vrs[PC];
  287. return URC_OK;
  288. }
  289. void unwind_backtrace(struct pt_regs *regs, struct task_struct *tsk)
  290. {
  291. struct stackframe frame;
  292. register unsigned long current_sp asm ("sp");
  293. pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
  294. if (!tsk)
  295. tsk = current;
  296. if (regs) {
  297. frame.fp = regs->ARM_fp;
  298. frame.sp = regs->ARM_sp;
  299. frame.lr = regs->ARM_lr;
  300. frame.pc = regs->ARM_pc;
  301. } else if (tsk == current) {
  302. frame.fp = (unsigned long)__builtin_frame_address(0);
  303. frame.sp = current_sp;
  304. frame.lr = (unsigned long)__builtin_return_address(0);
  305. frame.pc = (unsigned long)unwind_backtrace;
  306. } else {
  307. /* task blocked in __switch_to */
  308. frame.fp = thread_saved_fp(tsk);
  309. frame.sp = thread_saved_sp(tsk);
  310. /*
  311. * The function calling __switch_to cannot be a leaf function
  312. * so LR is recovered from the stack.
  313. */
  314. frame.lr = 0;
  315. frame.pc = thread_saved_pc(tsk);
  316. }
  317. while (1) {
  318. int urc;
  319. unsigned long where = frame.pc;
  320. urc = unwind_frame(&frame);
  321. if (urc < 0)
  322. break;
  323. dump_backtrace_entry(where, frame.pc, frame.sp - 4);
  324. }
  325. }
  326. struct unwind_table *unwind_table_add(unsigned long start, unsigned long size,
  327. unsigned long text_addr,
  328. unsigned long text_size)
  329. {
  330. unsigned long flags;
  331. struct unwind_idx *idx;
  332. struct unwind_table *tab = kmalloc(sizeof(*tab), GFP_KERNEL);
  333. pr_debug("%s(%08lx, %08lx, %08lx, %08lx)\n", __func__, start, size,
  334. text_addr, text_size);
  335. if (!tab)
  336. return tab;
  337. tab->start = (struct unwind_idx *)start;
  338. tab->stop = (struct unwind_idx *)(start + size);
  339. tab->begin_addr = text_addr;
  340. tab->end_addr = text_addr + text_size;
  341. /* Convert the symbol addresses to absolute values */
  342. for (idx = tab->start; idx < tab->stop; idx++)
  343. idx->addr = prel31_to_addr(&idx->addr);
  344. spin_lock_irqsave(&unwind_lock, flags);
  345. list_add_tail(&tab->list, &unwind_tables);
  346. spin_unlock_irqrestore(&unwind_lock, flags);
  347. return tab;
  348. }
  349. void unwind_table_del(struct unwind_table *tab)
  350. {
  351. unsigned long flags;
  352. if (!tab)
  353. return;
  354. spin_lock_irqsave(&unwind_lock, flags);
  355. list_del(&tab->list);
  356. spin_unlock_irqrestore(&unwind_lock, flags);
  357. kfree(tab);
  358. }
  359. int __init unwind_init(void)
  360. {
  361. struct unwind_idx *idx;
  362. /* Convert the symbol addresses to absolute values */
  363. for (idx = __start_unwind_idx; idx < __stop_unwind_idx; idx++)
  364. idx->addr = prel31_to_addr(&idx->addr);
  365. pr_debug("unwind: ARM stack unwinding initialised\n");
  366. return 0;
  367. }