unwind.c 11 KB

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