unwind.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. /* Move-to-front to exploit common traces */
  130. list_move(&table->list, &unwind_tables);
  131. break;
  132. }
  133. }
  134. spin_unlock_irqrestore(&unwind_lock, flags);
  135. }
  136. pr_debug("%s: idx = %p\n", __func__, idx);
  137. return idx;
  138. }
  139. static unsigned long unwind_get_byte(struct unwind_ctrl_block *ctrl)
  140. {
  141. unsigned long ret;
  142. if (ctrl->entries <= 0) {
  143. pr_warning("unwind: Corrupt unwind table\n");
  144. return 0;
  145. }
  146. ret = (*ctrl->insn >> (ctrl->byte * 8)) & 0xff;
  147. if (ctrl->byte == 0) {
  148. ctrl->insn++;
  149. ctrl->entries--;
  150. ctrl->byte = 3;
  151. } else
  152. ctrl->byte--;
  153. return ret;
  154. }
  155. /*
  156. * Execute the current unwind instruction.
  157. */
  158. static int unwind_exec_insn(struct unwind_ctrl_block *ctrl)
  159. {
  160. unsigned long insn = unwind_get_byte(ctrl);
  161. pr_debug("%s: insn = %08lx\n", __func__, insn);
  162. if ((insn & 0xc0) == 0x00)
  163. ctrl->vrs[SP] += ((insn & 0x3f) << 2) + 4;
  164. else if ((insn & 0xc0) == 0x40)
  165. ctrl->vrs[SP] -= ((insn & 0x3f) << 2) + 4;
  166. else if ((insn & 0xf0) == 0x80) {
  167. unsigned long mask;
  168. unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
  169. int load_sp, reg = 4;
  170. insn = (insn << 8) | unwind_get_byte(ctrl);
  171. mask = insn & 0x0fff;
  172. if (mask == 0) {
  173. pr_warning("unwind: 'Refuse to unwind' instruction %04lx\n",
  174. insn);
  175. return -URC_FAILURE;
  176. }
  177. /* pop R4-R15 according to mask */
  178. load_sp = mask & (1 << (13 - 4));
  179. while (mask) {
  180. if (mask & 1)
  181. ctrl->vrs[reg] = *vsp++;
  182. mask >>= 1;
  183. reg++;
  184. }
  185. if (!load_sp)
  186. ctrl->vrs[SP] = (unsigned long)vsp;
  187. } else if ((insn & 0xf0) == 0x90 &&
  188. (insn & 0x0d) != 0x0d)
  189. ctrl->vrs[SP] = ctrl->vrs[insn & 0x0f];
  190. else if ((insn & 0xf0) == 0xa0) {
  191. unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
  192. int reg;
  193. /* pop R4-R[4+bbb] */
  194. for (reg = 4; reg <= 4 + (insn & 7); reg++)
  195. ctrl->vrs[reg] = *vsp++;
  196. if (insn & 0x80)
  197. ctrl->vrs[14] = *vsp++;
  198. ctrl->vrs[SP] = (unsigned long)vsp;
  199. } else if (insn == 0xb0) {
  200. if (ctrl->vrs[PC] == 0)
  201. ctrl->vrs[PC] = ctrl->vrs[LR];
  202. /* no further processing */
  203. ctrl->entries = 0;
  204. } else if (insn == 0xb1) {
  205. unsigned long mask = unwind_get_byte(ctrl);
  206. unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
  207. int reg = 0;
  208. if (mask == 0 || mask & 0xf0) {
  209. pr_warning("unwind: Spare encoding %04lx\n",
  210. (insn << 8) | mask);
  211. return -URC_FAILURE;
  212. }
  213. /* pop R0-R3 according to mask */
  214. while (mask) {
  215. if (mask & 1)
  216. ctrl->vrs[reg] = *vsp++;
  217. mask >>= 1;
  218. reg++;
  219. }
  220. ctrl->vrs[SP] = (unsigned long)vsp;
  221. } else if (insn == 0xb2) {
  222. unsigned long uleb128 = unwind_get_byte(ctrl);
  223. ctrl->vrs[SP] += 0x204 + (uleb128 << 2);
  224. } else {
  225. pr_warning("unwind: Unhandled instruction %02lx\n", insn);
  226. return -URC_FAILURE;
  227. }
  228. pr_debug("%s: fp = %08lx sp = %08lx lr = %08lx pc = %08lx\n", __func__,
  229. ctrl->vrs[FP], ctrl->vrs[SP], ctrl->vrs[LR], ctrl->vrs[PC]);
  230. return URC_OK;
  231. }
  232. /*
  233. * Unwind a single frame starting with *sp for the symbol at *pc. It
  234. * updates the *pc and *sp with the new values.
  235. */
  236. int unwind_frame(struct stackframe *frame)
  237. {
  238. unsigned long high, low;
  239. struct unwind_idx *idx;
  240. struct unwind_ctrl_block ctrl;
  241. /* only go to a higher address on the stack */
  242. low = frame->sp;
  243. high = ALIGN(low, THREAD_SIZE);
  244. pr_debug("%s(pc = %08lx lr = %08lx sp = %08lx)\n", __func__,
  245. frame->pc, frame->lr, frame->sp);
  246. if (!kernel_text_address(frame->pc))
  247. return -URC_FAILURE;
  248. idx = unwind_find_idx(frame->pc);
  249. if (!idx) {
  250. pr_warning("unwind: Index not found %08lx\n", frame->pc);
  251. return -URC_FAILURE;
  252. }
  253. ctrl.vrs[FP] = frame->fp;
  254. ctrl.vrs[SP] = frame->sp;
  255. ctrl.vrs[LR] = frame->lr;
  256. ctrl.vrs[PC] = 0;
  257. if (idx->insn == 1)
  258. /* can't unwind */
  259. return -URC_FAILURE;
  260. else if ((idx->insn & 0x80000000) == 0)
  261. /* prel31 to the unwind table */
  262. ctrl.insn = (unsigned long *)prel31_to_addr(&idx->insn);
  263. else if ((idx->insn & 0xff000000) == 0x80000000)
  264. /* only personality routine 0 supported in the index */
  265. ctrl.insn = &idx->insn;
  266. else {
  267. pr_warning("unwind: Unsupported personality routine %08lx in the index at %p\n",
  268. idx->insn, idx);
  269. return -URC_FAILURE;
  270. }
  271. /* check the personality routine */
  272. if ((*ctrl.insn & 0xff000000) == 0x80000000) {
  273. ctrl.byte = 2;
  274. ctrl.entries = 1;
  275. } else if ((*ctrl.insn & 0xff000000) == 0x81000000) {
  276. ctrl.byte = 1;
  277. ctrl.entries = 1 + ((*ctrl.insn & 0x00ff0000) >> 16);
  278. } else {
  279. pr_warning("unwind: Unsupported personality routine %08lx at %p\n",
  280. *ctrl.insn, ctrl.insn);
  281. return -URC_FAILURE;
  282. }
  283. while (ctrl.entries > 0) {
  284. int urc = unwind_exec_insn(&ctrl);
  285. if (urc < 0)
  286. return urc;
  287. if (ctrl.vrs[SP] < low || ctrl.vrs[SP] >= high)
  288. return -URC_FAILURE;
  289. }
  290. if (ctrl.vrs[PC] == 0)
  291. ctrl.vrs[PC] = ctrl.vrs[LR];
  292. /* check for infinite loop */
  293. if (frame->pc == ctrl.vrs[PC])
  294. return -URC_FAILURE;
  295. frame->fp = ctrl.vrs[FP];
  296. frame->sp = ctrl.vrs[SP];
  297. frame->lr = ctrl.vrs[LR];
  298. frame->pc = ctrl.vrs[PC];
  299. return URC_OK;
  300. }
  301. void unwind_backtrace(struct pt_regs *regs, struct task_struct *tsk)
  302. {
  303. struct stackframe frame;
  304. register unsigned long current_sp asm ("sp");
  305. pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
  306. if (!tsk)
  307. tsk = current;
  308. if (regs) {
  309. frame.fp = regs->ARM_fp;
  310. frame.sp = regs->ARM_sp;
  311. frame.lr = regs->ARM_lr;
  312. /* PC might be corrupted, use LR in that case. */
  313. frame.pc = kernel_text_address(regs->ARM_pc)
  314. ? regs->ARM_pc : regs->ARM_lr;
  315. } else if (tsk == current) {
  316. frame.fp = (unsigned long)__builtin_frame_address(0);
  317. frame.sp = current_sp;
  318. frame.lr = (unsigned long)__builtin_return_address(0);
  319. frame.pc = (unsigned long)unwind_backtrace;
  320. } else {
  321. /* task blocked in __switch_to */
  322. frame.fp = thread_saved_fp(tsk);
  323. frame.sp = thread_saved_sp(tsk);
  324. /*
  325. * The function calling __switch_to cannot be a leaf function
  326. * so LR is recovered from the stack.
  327. */
  328. frame.lr = 0;
  329. frame.pc = thread_saved_pc(tsk);
  330. }
  331. while (1) {
  332. int urc;
  333. unsigned long where = frame.pc;
  334. urc = unwind_frame(&frame);
  335. if (urc < 0)
  336. break;
  337. dump_backtrace_entry(where, frame.pc, frame.sp - 4);
  338. }
  339. }
  340. struct unwind_table *unwind_table_add(unsigned long start, unsigned long size,
  341. unsigned long text_addr,
  342. unsigned long text_size)
  343. {
  344. unsigned long flags;
  345. struct unwind_idx *idx;
  346. struct unwind_table *tab = kmalloc(sizeof(*tab), GFP_KERNEL);
  347. pr_debug("%s(%08lx, %08lx, %08lx, %08lx)\n", __func__, start, size,
  348. text_addr, text_size);
  349. if (!tab)
  350. return tab;
  351. tab->start = (struct unwind_idx *)start;
  352. tab->stop = (struct unwind_idx *)(start + size);
  353. tab->begin_addr = text_addr;
  354. tab->end_addr = text_addr + text_size;
  355. /* Convert the symbol addresses to absolute values */
  356. for (idx = tab->start; idx < tab->stop; idx++)
  357. idx->addr = prel31_to_addr(&idx->addr);
  358. spin_lock_irqsave(&unwind_lock, flags);
  359. list_add_tail(&tab->list, &unwind_tables);
  360. spin_unlock_irqrestore(&unwind_lock, flags);
  361. return tab;
  362. }
  363. void unwind_table_del(struct unwind_table *tab)
  364. {
  365. unsigned long flags;
  366. if (!tab)
  367. return;
  368. spin_lock_irqsave(&unwind_lock, flags);
  369. list_del(&tab->list);
  370. spin_unlock_irqrestore(&unwind_lock, flags);
  371. kfree(tab);
  372. }
  373. int __init unwind_init(void)
  374. {
  375. struct unwind_idx *idx;
  376. /* Convert the symbol addresses to absolute values */
  377. for (idx = __start_unwind_idx; idx < __stop_unwind_idx; idx++)
  378. idx->addr = prel31_to_addr(&idx->addr);
  379. pr_debug("unwind: ARM stack unwinding initialised\n");
  380. return 0;
  381. }