unwind.c 14 KB

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