unwind.c 11 KB

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