unwind.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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) && !defined(__clang__)
  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/export.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. const 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 const struct unwind_idx __start_unwind_idx[];
  78. static const struct unwind_idx *__origin_unwind_idx;
  79. extern const struct unwind_idx __stop_unwind_idx[];
  80. static DEFINE_SPINLOCK(unwind_lock);
  81. static LIST_HEAD(unwind_tables);
  82. /* Convert a prel31 symbol to an absolute address */
  83. #define prel31_to_addr(ptr) \
  84. ({ \
  85. /* sign-extend to 32 bits */ \
  86. long offset = (((long)*(ptr)) << 1) >> 1; \
  87. (unsigned long)(ptr) + offset; \
  88. })
  89. /*
  90. * Binary search in the unwind index. The entries are
  91. * guaranteed to be sorted in ascending order by the linker.
  92. *
  93. * start = first entry
  94. * origin = first entry with positive offset (or stop if there is no such entry)
  95. * stop - 1 = last entry
  96. */
  97. static const struct unwind_idx *search_index(unsigned long addr,
  98. const struct unwind_idx *start,
  99. const struct unwind_idx *origin,
  100. const struct unwind_idx *stop)
  101. {
  102. unsigned long addr_prel31;
  103. pr_debug("%s(%08lx, %p, %p, %p)\n",
  104. __func__, addr, start, origin, stop);
  105. /*
  106. * only search in the section with the matching sign. This way the
  107. * prel31 numbers can be compared as unsigned longs.
  108. */
  109. if (addr < (unsigned long)start)
  110. /* negative offsets: [start; origin) */
  111. stop = origin;
  112. else
  113. /* positive offsets: [origin; stop) */
  114. start = origin;
  115. /* prel31 for address relavive to start */
  116. addr_prel31 = (addr - (unsigned long)start) & 0x7fffffff;
  117. while (start < stop - 1) {
  118. const struct unwind_idx *mid = start + ((stop - start) >> 1);
  119. /*
  120. * As addr_prel31 is relative to start an offset is needed to
  121. * make it relative to mid.
  122. */
  123. if (addr_prel31 - ((unsigned long)mid - (unsigned long)start) <
  124. mid->addr_offset)
  125. stop = mid;
  126. else {
  127. /* keep addr_prel31 relative to start */
  128. addr_prel31 -= ((unsigned long)mid -
  129. (unsigned long)start);
  130. start = mid;
  131. }
  132. }
  133. if (likely(start->addr_offset <= addr_prel31))
  134. return start;
  135. else {
  136. pr_warning("unwind: Unknown symbol address %08lx\n", addr);
  137. return NULL;
  138. }
  139. }
  140. static const struct unwind_idx *unwind_find_origin(
  141. const struct unwind_idx *start, const struct unwind_idx *stop)
  142. {
  143. pr_debug("%s(%p, %p)\n", __func__, start, stop);
  144. while (start < stop) {
  145. const struct unwind_idx *mid = start + ((stop - start) >> 1);
  146. if (mid->addr_offset >= 0x40000000)
  147. /* negative offset */
  148. start = mid + 1;
  149. else
  150. /* positive offset */
  151. stop = mid;
  152. }
  153. pr_debug("%s -> %p\n", __func__, stop);
  154. return stop;
  155. }
  156. static const struct unwind_idx *unwind_find_idx(unsigned long addr)
  157. {
  158. const struct unwind_idx *idx = NULL;
  159. unsigned long flags;
  160. pr_debug("%s(%08lx)\n", __func__, addr);
  161. if (core_kernel_text(addr)) {
  162. if (unlikely(!__origin_unwind_idx))
  163. __origin_unwind_idx =
  164. unwind_find_origin(__start_unwind_idx,
  165. __stop_unwind_idx);
  166. /* main unwind table */
  167. idx = search_index(addr, __start_unwind_idx,
  168. __origin_unwind_idx,
  169. __stop_unwind_idx);
  170. } else {
  171. /* module unwind tables */
  172. struct unwind_table *table;
  173. spin_lock_irqsave(&unwind_lock, flags);
  174. list_for_each_entry(table, &unwind_tables, list) {
  175. if (addr >= table->begin_addr &&
  176. addr < table->end_addr) {
  177. idx = search_index(addr, table->start,
  178. table->origin,
  179. table->stop);
  180. /* Move-to-front to exploit common traces */
  181. list_move(&table->list, &unwind_tables);
  182. break;
  183. }
  184. }
  185. spin_unlock_irqrestore(&unwind_lock, flags);
  186. }
  187. pr_debug("%s: idx = %p\n", __func__, idx);
  188. return idx;
  189. }
  190. static unsigned long unwind_get_byte(struct unwind_ctrl_block *ctrl)
  191. {
  192. unsigned long ret;
  193. if (ctrl->entries <= 0) {
  194. pr_warning("unwind: Corrupt unwind table\n");
  195. return 0;
  196. }
  197. ret = (*ctrl->insn >> (ctrl->byte * 8)) & 0xff;
  198. if (ctrl->byte == 0) {
  199. ctrl->insn++;
  200. ctrl->entries--;
  201. ctrl->byte = 3;
  202. } else
  203. ctrl->byte--;
  204. return ret;
  205. }
  206. /*
  207. * Execute the current unwind instruction.
  208. */
  209. static int unwind_exec_insn(struct unwind_ctrl_block *ctrl)
  210. {
  211. unsigned long insn = unwind_get_byte(ctrl);
  212. pr_debug("%s: insn = %08lx\n", __func__, insn);
  213. if ((insn & 0xc0) == 0x00)
  214. ctrl->vrs[SP] += ((insn & 0x3f) << 2) + 4;
  215. else if ((insn & 0xc0) == 0x40)
  216. ctrl->vrs[SP] -= ((insn & 0x3f) << 2) + 4;
  217. else if ((insn & 0xf0) == 0x80) {
  218. unsigned long mask;
  219. unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
  220. int load_sp, reg = 4;
  221. insn = (insn << 8) | unwind_get_byte(ctrl);
  222. mask = insn & 0x0fff;
  223. if (mask == 0) {
  224. pr_warning("unwind: 'Refuse to unwind' instruction %04lx\n",
  225. insn);
  226. return -URC_FAILURE;
  227. }
  228. /* pop R4-R15 according to mask */
  229. load_sp = mask & (1 << (13 - 4));
  230. while (mask) {
  231. if (mask & 1)
  232. ctrl->vrs[reg] = *vsp++;
  233. mask >>= 1;
  234. reg++;
  235. }
  236. if (!load_sp)
  237. ctrl->vrs[SP] = (unsigned long)vsp;
  238. } else if ((insn & 0xf0) == 0x90 &&
  239. (insn & 0x0d) != 0x0d)
  240. ctrl->vrs[SP] = ctrl->vrs[insn & 0x0f];
  241. else if ((insn & 0xf0) == 0xa0) {
  242. unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
  243. int reg;
  244. /* pop R4-R[4+bbb] */
  245. for (reg = 4; reg <= 4 + (insn & 7); reg++)
  246. ctrl->vrs[reg] = *vsp++;
  247. if (insn & 0x80)
  248. ctrl->vrs[14] = *vsp++;
  249. ctrl->vrs[SP] = (unsigned long)vsp;
  250. } else if (insn == 0xb0) {
  251. if (ctrl->vrs[PC] == 0)
  252. ctrl->vrs[PC] = ctrl->vrs[LR];
  253. /* no further processing */
  254. ctrl->entries = 0;
  255. } else if (insn == 0xb1) {
  256. unsigned long mask = unwind_get_byte(ctrl);
  257. unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
  258. int reg = 0;
  259. if (mask == 0 || mask & 0xf0) {
  260. pr_warning("unwind: Spare encoding %04lx\n",
  261. (insn << 8) | mask);
  262. return -URC_FAILURE;
  263. }
  264. /* pop R0-R3 according to mask */
  265. while (mask) {
  266. if (mask & 1)
  267. ctrl->vrs[reg] = *vsp++;
  268. mask >>= 1;
  269. reg++;
  270. }
  271. ctrl->vrs[SP] = (unsigned long)vsp;
  272. } else if (insn == 0xb2) {
  273. unsigned long uleb128 = unwind_get_byte(ctrl);
  274. ctrl->vrs[SP] += 0x204 + (uleb128 << 2);
  275. } else {
  276. pr_warning("unwind: Unhandled instruction %02lx\n", insn);
  277. return -URC_FAILURE;
  278. }
  279. pr_debug("%s: fp = %08lx sp = %08lx lr = %08lx pc = %08lx\n", __func__,
  280. ctrl->vrs[FP], ctrl->vrs[SP], ctrl->vrs[LR], ctrl->vrs[PC]);
  281. return URC_OK;
  282. }
  283. /*
  284. * Unwind a single frame starting with *sp for the symbol at *pc. It
  285. * updates the *pc and *sp with the new values.
  286. */
  287. int unwind_frame(struct stackframe *frame)
  288. {
  289. unsigned long high, low;
  290. const struct unwind_idx *idx;
  291. struct unwind_ctrl_block ctrl;
  292. /* only go to a higher address on the stack */
  293. low = frame->sp;
  294. high = ALIGN(low, THREAD_SIZE);
  295. pr_debug("%s(pc = %08lx lr = %08lx sp = %08lx)\n", __func__,
  296. frame->pc, frame->lr, frame->sp);
  297. if (!kernel_text_address(frame->pc))
  298. return -URC_FAILURE;
  299. idx = unwind_find_idx(frame->pc);
  300. if (!idx) {
  301. pr_warning("unwind: Index not found %08lx\n", frame->pc);
  302. return -URC_FAILURE;
  303. }
  304. ctrl.vrs[FP] = frame->fp;
  305. ctrl.vrs[SP] = frame->sp;
  306. ctrl.vrs[LR] = frame->lr;
  307. ctrl.vrs[PC] = 0;
  308. if (idx->insn == 1)
  309. /* can't unwind */
  310. return -URC_FAILURE;
  311. else if ((idx->insn & 0x80000000) == 0)
  312. /* prel31 to the unwind table */
  313. ctrl.insn = (unsigned long *)prel31_to_addr(&idx->insn);
  314. else if ((idx->insn & 0xff000000) == 0x80000000)
  315. /* only personality routine 0 supported in the index */
  316. ctrl.insn = &idx->insn;
  317. else {
  318. pr_warning("unwind: Unsupported personality routine %08lx in the index at %p\n",
  319. idx->insn, idx);
  320. return -URC_FAILURE;
  321. }
  322. /* check the personality routine */
  323. if ((*ctrl.insn & 0xff000000) == 0x80000000) {
  324. ctrl.byte = 2;
  325. ctrl.entries = 1;
  326. } else if ((*ctrl.insn & 0xff000000) == 0x81000000) {
  327. ctrl.byte = 1;
  328. ctrl.entries = 1 + ((*ctrl.insn & 0x00ff0000) >> 16);
  329. } else {
  330. pr_warning("unwind: Unsupported personality routine %08lx at %p\n",
  331. *ctrl.insn, ctrl.insn);
  332. return -URC_FAILURE;
  333. }
  334. #ifdef CONFIG_ARCH_MSM8226
  335. /* Fix for user-stack corruption for better debugging purpose */
  336. if (ctrl.entries == 0 || ctrl.byte ==0)
  337. return -URC_FAILURE;
  338. #endif
  339. while (ctrl.entries > 0) {
  340. int urc = unwind_exec_insn(&ctrl);
  341. if (urc < 0)
  342. return urc;
  343. if (ctrl.vrs[SP] < low || ctrl.vrs[SP] >= high)
  344. return -URC_FAILURE;
  345. }
  346. if (ctrl.vrs[PC] == 0)
  347. ctrl.vrs[PC] = ctrl.vrs[LR];
  348. /* check for infinite loop */
  349. if (frame->pc == ctrl.vrs[PC])
  350. return -URC_FAILURE;
  351. frame->fp = ctrl.vrs[FP];
  352. frame->sp = ctrl.vrs[SP];
  353. frame->lr = ctrl.vrs[LR];
  354. frame->pc = ctrl.vrs[PC];
  355. return URC_OK;
  356. }
  357. void unwind_backtrace(struct pt_regs *regs, struct task_struct *tsk)
  358. {
  359. struct stackframe frame;
  360. pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
  361. if (!tsk)
  362. tsk = current;
  363. if (regs) {
  364. frame.fp = regs->ARM_fp;
  365. frame.sp = regs->ARM_sp;
  366. frame.lr = regs->ARM_lr;
  367. /* PC might be corrupted, use LR in that case. */
  368. frame.pc = kernel_text_address(regs->ARM_pc)
  369. ? regs->ARM_pc : regs->ARM_lr;
  370. } else if (tsk == current) {
  371. frame.fp = (unsigned long)__builtin_frame_address(0);
  372. frame.sp = current_stack_pointer;
  373. frame.lr = (unsigned long)__builtin_return_address(0);
  374. frame.pc = (unsigned long)unwind_backtrace;
  375. } else {
  376. /* task blocked in __switch_to */
  377. frame.fp = thread_saved_fp(tsk);
  378. frame.sp = thread_saved_sp(tsk);
  379. /*
  380. * The function calling __switch_to cannot be a leaf function
  381. * so LR is recovered from the stack.
  382. */
  383. frame.lr = 0;
  384. frame.pc = thread_saved_pc(tsk);
  385. }
  386. while (1) {
  387. int urc;
  388. unsigned long where = frame.pc;
  389. urc = unwind_frame(&frame);
  390. if (urc < 0)
  391. break;
  392. dump_backtrace_entry(where, frame.pc, frame.sp - 4);
  393. }
  394. }
  395. struct unwind_table *unwind_table_add(unsigned long start, unsigned long size,
  396. unsigned long text_addr,
  397. unsigned long text_size)
  398. {
  399. unsigned long flags;
  400. struct unwind_table *tab = kmalloc(sizeof(*tab), GFP_KERNEL);
  401. pr_debug("%s(%08lx, %08lx, %08lx, %08lx)\n", __func__, start, size,
  402. text_addr, text_size);
  403. if (!tab)
  404. return tab;
  405. tab->start = (const struct unwind_idx *)start;
  406. tab->stop = (const struct unwind_idx *)(start + size);
  407. tab->origin = unwind_find_origin(tab->start, tab->stop);
  408. tab->begin_addr = text_addr;
  409. tab->end_addr = text_addr + text_size;
  410. spin_lock_irqsave(&unwind_lock, flags);
  411. list_add_tail(&tab->list, &unwind_tables);
  412. spin_unlock_irqrestore(&unwind_lock, flags);
  413. return tab;
  414. }
  415. void unwind_table_del(struct unwind_table *tab)
  416. {
  417. unsigned long flags;
  418. if (!tab)
  419. return;
  420. spin_lock_irqsave(&unwind_lock, flags);
  421. list_del(&tab->list);
  422. spin_unlock_irqrestore(&unwind_lock, flags);
  423. kfree(tab);
  424. }