unwind.c 11 KB

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