dwarf.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. /*
  2. * Copyright (C) 2009 Matt Fleming <matt@console-pimps.org>
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * This is an implementation of a DWARF unwinder. Its main purpose is
  9. * for generating stacktrace information. Based on the DWARF 3
  10. * specification from http://www.dwarfstd.org.
  11. *
  12. * TODO:
  13. * - DWARF64 doesn't work.
  14. * - Registers with DWARF_VAL_OFFSET rules aren't handled properly.
  15. */
  16. /* #define DEBUG */
  17. #include <linux/kernel.h>
  18. #include <linux/io.h>
  19. #include <linux/list.h>
  20. #include <linux/mempool.h>
  21. #include <linux/mm.h>
  22. #include <linux/elf.h>
  23. #include <linux/ftrace.h>
  24. #include <linux/module.h>
  25. #include <linux/slab.h>
  26. #include <asm/dwarf.h>
  27. #include <asm/unwinder.h>
  28. #include <asm/sections.h>
  29. #include <asm/unaligned.h>
  30. #include <asm/stacktrace.h>
  31. /* Reserve enough memory for two stack frames */
  32. #define DWARF_FRAME_MIN_REQ 2
  33. /* ... with 4 registers per frame. */
  34. #define DWARF_REG_MIN_REQ (DWARF_FRAME_MIN_REQ * 4)
  35. static struct kmem_cache *dwarf_frame_cachep;
  36. static mempool_t *dwarf_frame_pool;
  37. static struct kmem_cache *dwarf_reg_cachep;
  38. static mempool_t *dwarf_reg_pool;
  39. static struct rb_root cie_root;
  40. static DEFINE_SPINLOCK(dwarf_cie_lock);
  41. static struct rb_root fde_root;
  42. static DEFINE_SPINLOCK(dwarf_fde_lock);
  43. static struct dwarf_cie *cached_cie;
  44. static unsigned int dwarf_unwinder_ready;
  45. /**
  46. * dwarf_frame_alloc_reg - allocate memory for a DWARF register
  47. * @frame: the DWARF frame whose list of registers we insert on
  48. * @reg_num: the register number
  49. *
  50. * Allocate space for, and initialise, a dwarf reg from
  51. * dwarf_reg_pool and insert it onto the (unsorted) linked-list of
  52. * dwarf registers for @frame.
  53. *
  54. * Return the initialised DWARF reg.
  55. */
  56. static struct dwarf_reg *dwarf_frame_alloc_reg(struct dwarf_frame *frame,
  57. unsigned int reg_num)
  58. {
  59. struct dwarf_reg *reg;
  60. reg = mempool_alloc(dwarf_reg_pool, GFP_ATOMIC);
  61. if (!reg) {
  62. printk(KERN_WARNING "Unable to allocate a DWARF register\n");
  63. /*
  64. * Let's just bomb hard here, we have no way to
  65. * gracefully recover.
  66. */
  67. UNWINDER_BUG();
  68. }
  69. reg->number = reg_num;
  70. reg->addr = 0;
  71. reg->flags = 0;
  72. list_add(&reg->link, &frame->reg_list);
  73. return reg;
  74. }
  75. static void dwarf_frame_free_regs(struct dwarf_frame *frame)
  76. {
  77. struct dwarf_reg *reg, *n;
  78. list_for_each_entry_safe(reg, n, &frame->reg_list, link) {
  79. list_del(&reg->link);
  80. mempool_free(reg, dwarf_reg_pool);
  81. }
  82. }
  83. /**
  84. * dwarf_frame_reg - return a DWARF register
  85. * @frame: the DWARF frame to search in for @reg_num
  86. * @reg_num: the register number to search for
  87. *
  88. * Lookup and return the dwarf reg @reg_num for this frame. Return
  89. * NULL if @reg_num is an register invalid number.
  90. */
  91. static struct dwarf_reg *dwarf_frame_reg(struct dwarf_frame *frame,
  92. unsigned int reg_num)
  93. {
  94. struct dwarf_reg *reg;
  95. list_for_each_entry(reg, &frame->reg_list, link) {
  96. if (reg->number == reg_num)
  97. return reg;
  98. }
  99. return NULL;
  100. }
  101. /**
  102. * dwarf_read_addr - read dwarf data
  103. * @src: source address of data
  104. * @dst: destination address to store the data to
  105. *
  106. * Read 'n' bytes from @src, where 'n' is the size of an address on
  107. * the native machine. We return the number of bytes read, which
  108. * should always be 'n'. We also have to be careful when reading
  109. * from @src and writing to @dst, because they can be arbitrarily
  110. * aligned. Return 'n' - the number of bytes read.
  111. */
  112. static inline int dwarf_read_addr(unsigned long *src, unsigned long *dst)
  113. {
  114. u32 val = get_unaligned(src);
  115. put_unaligned(val, dst);
  116. return sizeof(unsigned long *);
  117. }
  118. /**
  119. * dwarf_read_uleb128 - read unsigned LEB128 data
  120. * @addr: the address where the ULEB128 data is stored
  121. * @ret: address to store the result
  122. *
  123. * Decode an unsigned LEB128 encoded datum. The algorithm is taken
  124. * from Appendix C of the DWARF 3 spec. For information on the
  125. * encodings refer to section "7.6 - Variable Length Data". Return
  126. * the number of bytes read.
  127. */
  128. static inline unsigned long dwarf_read_uleb128(char *addr, unsigned int *ret)
  129. {
  130. unsigned int result;
  131. unsigned char byte;
  132. int shift, count;
  133. result = 0;
  134. shift = 0;
  135. count = 0;
  136. while (1) {
  137. byte = __raw_readb(addr);
  138. addr++;
  139. count++;
  140. result |= (byte & 0x7f) << shift;
  141. shift += 7;
  142. if (!(byte & 0x80))
  143. break;
  144. }
  145. *ret = result;
  146. return count;
  147. }
  148. /**
  149. * dwarf_read_leb128 - read signed LEB128 data
  150. * @addr: the address of the LEB128 encoded data
  151. * @ret: address to store the result
  152. *
  153. * Decode signed LEB128 data. The algorithm is taken from Appendix
  154. * C of the DWARF 3 spec. Return the number of bytes read.
  155. */
  156. static inline unsigned long dwarf_read_leb128(char *addr, int *ret)
  157. {
  158. unsigned char byte;
  159. int result, shift;
  160. int num_bits;
  161. int count;
  162. result = 0;
  163. shift = 0;
  164. count = 0;
  165. while (1) {
  166. byte = __raw_readb(addr);
  167. addr++;
  168. result |= (byte & 0x7f) << shift;
  169. shift += 7;
  170. count++;
  171. if (!(byte & 0x80))
  172. break;
  173. }
  174. /* The number of bits in a signed integer. */
  175. num_bits = 8 * sizeof(result);
  176. if ((shift < num_bits) && (byte & 0x40))
  177. result |= (-1 << shift);
  178. *ret = result;
  179. return count;
  180. }
  181. /**
  182. * dwarf_read_encoded_value - return the decoded value at @addr
  183. * @addr: the address of the encoded value
  184. * @val: where to write the decoded value
  185. * @encoding: the encoding with which we can decode @addr
  186. *
  187. * GCC emits encoded address in the .eh_frame FDE entries. Decode
  188. * the value at @addr using @encoding. The decoded value is written
  189. * to @val and the number of bytes read is returned.
  190. */
  191. static int dwarf_read_encoded_value(char *addr, unsigned long *val,
  192. char encoding)
  193. {
  194. unsigned long decoded_addr = 0;
  195. int count = 0;
  196. switch (encoding & 0x70) {
  197. case DW_EH_PE_absptr:
  198. break;
  199. case DW_EH_PE_pcrel:
  200. decoded_addr = (unsigned long)addr;
  201. break;
  202. default:
  203. pr_debug("encoding=0x%x\n", (encoding & 0x70));
  204. UNWINDER_BUG();
  205. }
  206. if ((encoding & 0x07) == 0x00)
  207. encoding |= DW_EH_PE_udata4;
  208. switch (encoding & 0x0f) {
  209. case DW_EH_PE_sdata4:
  210. case DW_EH_PE_udata4:
  211. count += 4;
  212. decoded_addr += get_unaligned((u32 *)addr);
  213. __raw_writel(decoded_addr, val);
  214. break;
  215. default:
  216. pr_debug("encoding=0x%x\n", encoding);
  217. UNWINDER_BUG();
  218. }
  219. return count;
  220. }
  221. /**
  222. * dwarf_entry_len - return the length of an FDE or CIE
  223. * @addr: the address of the entry
  224. * @len: the length of the entry
  225. *
  226. * Read the initial_length field of the entry and store the size of
  227. * the entry in @len. We return the number of bytes read. Return a
  228. * count of 0 on error.
  229. */
  230. static inline int dwarf_entry_len(char *addr, unsigned long *len)
  231. {
  232. u32 initial_len;
  233. int count;
  234. initial_len = get_unaligned((u32 *)addr);
  235. count = 4;
  236. /*
  237. * An initial length field value in the range DW_LEN_EXT_LO -
  238. * DW_LEN_EXT_HI indicates an extension, and should not be
  239. * interpreted as a length. The only extension that we currently
  240. * understand is the use of DWARF64 addresses.
  241. */
  242. if (initial_len >= DW_EXT_LO && initial_len <= DW_EXT_HI) {
  243. /*
  244. * The 64-bit length field immediately follows the
  245. * compulsory 32-bit length field.
  246. */
  247. if (initial_len == DW_EXT_DWARF64) {
  248. *len = get_unaligned((u64 *)addr + 4);
  249. count = 12;
  250. } else {
  251. printk(KERN_WARNING "Unknown DWARF extension\n");
  252. count = 0;
  253. }
  254. } else
  255. *len = initial_len;
  256. return count;
  257. }
  258. /**
  259. * dwarf_lookup_cie - locate the cie
  260. * @cie_ptr: pointer to help with lookup
  261. */
  262. static struct dwarf_cie *dwarf_lookup_cie(unsigned long cie_ptr)
  263. {
  264. struct rb_node **rb_node = &cie_root.rb_node;
  265. struct dwarf_cie *cie = NULL;
  266. unsigned long flags;
  267. spin_lock_irqsave(&dwarf_cie_lock, flags);
  268. /*
  269. * We've cached the last CIE we looked up because chances are
  270. * that the FDE wants this CIE.
  271. */
  272. if (cached_cie && cached_cie->cie_pointer == cie_ptr) {
  273. cie = cached_cie;
  274. goto out;
  275. }
  276. while (*rb_node) {
  277. struct dwarf_cie *cie_tmp;
  278. cie_tmp = rb_entry(*rb_node, struct dwarf_cie, node);
  279. BUG_ON(!cie_tmp);
  280. if (cie_ptr == cie_tmp->cie_pointer) {
  281. cie = cie_tmp;
  282. cached_cie = cie_tmp;
  283. goto out;
  284. } else {
  285. if (cie_ptr < cie_tmp->cie_pointer)
  286. rb_node = &(*rb_node)->rb_left;
  287. else
  288. rb_node = &(*rb_node)->rb_right;
  289. }
  290. }
  291. out:
  292. spin_unlock_irqrestore(&dwarf_cie_lock, flags);
  293. return cie;
  294. }
  295. /**
  296. * dwarf_lookup_fde - locate the FDE that covers pc
  297. * @pc: the program counter
  298. */
  299. struct dwarf_fde *dwarf_lookup_fde(unsigned long pc)
  300. {
  301. struct rb_node **rb_node = &fde_root.rb_node;
  302. struct dwarf_fde *fde = NULL;
  303. unsigned long flags;
  304. spin_lock_irqsave(&dwarf_fde_lock, flags);
  305. while (*rb_node) {
  306. struct dwarf_fde *fde_tmp;
  307. unsigned long tmp_start, tmp_end;
  308. fde_tmp = rb_entry(*rb_node, struct dwarf_fde, node);
  309. BUG_ON(!fde_tmp);
  310. tmp_start = fde_tmp->initial_location;
  311. tmp_end = fde_tmp->initial_location + fde_tmp->address_range;
  312. if (pc < tmp_start) {
  313. rb_node = &(*rb_node)->rb_left;
  314. } else {
  315. if (pc < tmp_end) {
  316. fde = fde_tmp;
  317. goto out;
  318. } else
  319. rb_node = &(*rb_node)->rb_right;
  320. }
  321. }
  322. out:
  323. spin_unlock_irqrestore(&dwarf_fde_lock, flags);
  324. return fde;
  325. }
  326. /**
  327. * dwarf_cfa_execute_insns - execute instructions to calculate a CFA
  328. * @insn_start: address of the first instruction
  329. * @insn_end: address of the last instruction
  330. * @cie: the CIE for this function
  331. * @fde: the FDE for this function
  332. * @frame: the instructions calculate the CFA for this frame
  333. * @pc: the program counter of the address we're interested in
  334. *
  335. * Execute the Call Frame instruction sequence starting at
  336. * @insn_start and ending at @insn_end. The instructions describe
  337. * how to calculate the Canonical Frame Address of a stackframe.
  338. * Store the results in @frame.
  339. */
  340. static int dwarf_cfa_execute_insns(unsigned char *insn_start,
  341. unsigned char *insn_end,
  342. struct dwarf_cie *cie,
  343. struct dwarf_fde *fde,
  344. struct dwarf_frame *frame,
  345. unsigned long pc)
  346. {
  347. unsigned char insn;
  348. unsigned char *current_insn;
  349. unsigned int count, delta, reg, expr_len, offset;
  350. struct dwarf_reg *regp;
  351. current_insn = insn_start;
  352. while (current_insn < insn_end && frame->pc <= pc) {
  353. insn = __raw_readb(current_insn++);
  354. /*
  355. * Firstly, handle the opcodes that embed their operands
  356. * in the instructions.
  357. */
  358. switch (DW_CFA_opcode(insn)) {
  359. case DW_CFA_advance_loc:
  360. delta = DW_CFA_operand(insn);
  361. delta *= cie->code_alignment_factor;
  362. frame->pc += delta;
  363. continue;
  364. /* NOTREACHED */
  365. case DW_CFA_offset:
  366. reg = DW_CFA_operand(insn);
  367. count = dwarf_read_uleb128(current_insn, &offset);
  368. current_insn += count;
  369. offset *= cie->data_alignment_factor;
  370. regp = dwarf_frame_alloc_reg(frame, reg);
  371. regp->addr = offset;
  372. regp->flags |= DWARF_REG_OFFSET;
  373. continue;
  374. /* NOTREACHED */
  375. case DW_CFA_restore:
  376. reg = DW_CFA_operand(insn);
  377. continue;
  378. /* NOTREACHED */
  379. }
  380. /*
  381. * Secondly, handle the opcodes that don't embed their
  382. * operands in the instruction.
  383. */
  384. switch (insn) {
  385. case DW_CFA_nop:
  386. continue;
  387. case DW_CFA_advance_loc1:
  388. delta = *current_insn++;
  389. frame->pc += delta * cie->code_alignment_factor;
  390. break;
  391. case DW_CFA_advance_loc2:
  392. delta = get_unaligned((u16 *)current_insn);
  393. current_insn += 2;
  394. frame->pc += delta * cie->code_alignment_factor;
  395. break;
  396. case DW_CFA_advance_loc4:
  397. delta = get_unaligned((u32 *)current_insn);
  398. current_insn += 4;
  399. frame->pc += delta * cie->code_alignment_factor;
  400. break;
  401. case DW_CFA_offset_extended:
  402. count = dwarf_read_uleb128(current_insn, &reg);
  403. current_insn += count;
  404. count = dwarf_read_uleb128(current_insn, &offset);
  405. current_insn += count;
  406. offset *= cie->data_alignment_factor;
  407. break;
  408. case DW_CFA_restore_extended:
  409. count = dwarf_read_uleb128(current_insn, &reg);
  410. current_insn += count;
  411. break;
  412. case DW_CFA_undefined:
  413. count = dwarf_read_uleb128(current_insn, &reg);
  414. current_insn += count;
  415. regp = dwarf_frame_alloc_reg(frame, reg);
  416. regp->flags |= DWARF_UNDEFINED;
  417. break;
  418. case DW_CFA_def_cfa:
  419. count = dwarf_read_uleb128(current_insn,
  420. &frame->cfa_register);
  421. current_insn += count;
  422. count = dwarf_read_uleb128(current_insn,
  423. &frame->cfa_offset);
  424. current_insn += count;
  425. frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
  426. break;
  427. case DW_CFA_def_cfa_register:
  428. count = dwarf_read_uleb128(current_insn,
  429. &frame->cfa_register);
  430. current_insn += count;
  431. frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
  432. break;
  433. case DW_CFA_def_cfa_offset:
  434. count = dwarf_read_uleb128(current_insn, &offset);
  435. current_insn += count;
  436. frame->cfa_offset = offset;
  437. break;
  438. case DW_CFA_def_cfa_expression:
  439. count = dwarf_read_uleb128(current_insn, &expr_len);
  440. current_insn += count;
  441. frame->cfa_expr = current_insn;
  442. frame->cfa_expr_len = expr_len;
  443. current_insn += expr_len;
  444. frame->flags |= DWARF_FRAME_CFA_REG_EXP;
  445. break;
  446. case DW_CFA_offset_extended_sf:
  447. count = dwarf_read_uleb128(current_insn, &reg);
  448. current_insn += count;
  449. count = dwarf_read_leb128(current_insn, &offset);
  450. current_insn += count;
  451. offset *= cie->data_alignment_factor;
  452. regp = dwarf_frame_alloc_reg(frame, reg);
  453. regp->flags |= DWARF_REG_OFFSET;
  454. regp->addr = offset;
  455. break;
  456. case DW_CFA_val_offset:
  457. count = dwarf_read_uleb128(current_insn, &reg);
  458. current_insn += count;
  459. count = dwarf_read_leb128(current_insn, &offset);
  460. offset *= cie->data_alignment_factor;
  461. regp = dwarf_frame_alloc_reg(frame, reg);
  462. regp->flags |= DWARF_VAL_OFFSET;
  463. regp->addr = offset;
  464. break;
  465. case DW_CFA_GNU_args_size:
  466. count = dwarf_read_uleb128(current_insn, &offset);
  467. current_insn += count;
  468. break;
  469. case DW_CFA_GNU_negative_offset_extended:
  470. count = dwarf_read_uleb128(current_insn, &reg);
  471. current_insn += count;
  472. count = dwarf_read_uleb128(current_insn, &offset);
  473. offset *= cie->data_alignment_factor;
  474. regp = dwarf_frame_alloc_reg(frame, reg);
  475. regp->flags |= DWARF_REG_OFFSET;
  476. regp->addr = -offset;
  477. break;
  478. default:
  479. pr_debug("unhandled DWARF instruction 0x%x\n", insn);
  480. UNWINDER_BUG();
  481. break;
  482. }
  483. }
  484. return 0;
  485. }
  486. /**
  487. * dwarf_free_frame - free the memory allocated for @frame
  488. * @frame: the frame to free
  489. */
  490. void dwarf_free_frame(struct dwarf_frame *frame)
  491. {
  492. dwarf_frame_free_regs(frame);
  493. mempool_free(frame, dwarf_frame_pool);
  494. }
  495. extern void ret_from_irq(void);
  496. /**
  497. * dwarf_unwind_stack - unwind the stack
  498. *
  499. * @pc: address of the function to unwind
  500. * @prev: struct dwarf_frame of the previous stackframe on the callstack
  501. *
  502. * Return a struct dwarf_frame representing the most recent frame
  503. * on the callstack. Each of the lower (older) stack frames are
  504. * linked via the "prev" member.
  505. */
  506. struct dwarf_frame *dwarf_unwind_stack(unsigned long pc,
  507. struct dwarf_frame *prev)
  508. {
  509. struct dwarf_frame *frame;
  510. struct dwarf_cie *cie;
  511. struct dwarf_fde *fde;
  512. struct dwarf_reg *reg;
  513. unsigned long addr;
  514. /*
  515. * If we've been called in to before initialization has
  516. * completed, bail out immediately.
  517. */
  518. if (!dwarf_unwinder_ready)
  519. return NULL;
  520. /*
  521. * If we're starting at the top of the stack we need get the
  522. * contents of a physical register to get the CFA in order to
  523. * begin the virtual unwinding of the stack.
  524. *
  525. * NOTE: the return address is guaranteed to be setup by the
  526. * time this function makes its first function call.
  527. */
  528. if (!pc || !prev)
  529. pc = (unsigned long)current_text_addr();
  530. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  531. /*
  532. * If our stack has been patched by the function graph tracer
  533. * then we might see the address of return_to_handler() where we
  534. * expected to find the real return address.
  535. */
  536. if (pc == (unsigned long)&return_to_handler) {
  537. int index = current->curr_ret_stack;
  538. /*
  539. * We currently have no way of tracking how many
  540. * return_to_handler()'s we've seen. If there is more
  541. * than one patched return address on our stack,
  542. * complain loudly.
  543. */
  544. WARN_ON(index > 0);
  545. pc = current->ret_stack[index].ret;
  546. }
  547. #endif
  548. frame = mempool_alloc(dwarf_frame_pool, GFP_ATOMIC);
  549. if (!frame) {
  550. printk(KERN_ERR "Unable to allocate a dwarf frame\n");
  551. UNWINDER_BUG();
  552. }
  553. INIT_LIST_HEAD(&frame->reg_list);
  554. frame->flags = 0;
  555. frame->prev = prev;
  556. frame->return_addr = 0;
  557. fde = dwarf_lookup_fde(pc);
  558. if (!fde) {
  559. /*
  560. * This is our normal exit path. There are two reasons
  561. * why we might exit here,
  562. *
  563. * a) pc has no asscociated DWARF frame info and so
  564. * we don't know how to unwind this frame. This is
  565. * usually the case when we're trying to unwind a
  566. * frame that was called from some assembly code
  567. * that has no DWARF info, e.g. syscalls.
  568. *
  569. * b) the DEBUG info for pc is bogus. There's
  570. * really no way to distinguish this case from the
  571. * case above, which sucks because we could print a
  572. * warning here.
  573. */
  574. goto bail;
  575. }
  576. cie = dwarf_lookup_cie(fde->cie_pointer);
  577. frame->pc = fde->initial_location;
  578. /* CIE initial instructions */
  579. dwarf_cfa_execute_insns(cie->initial_instructions,
  580. cie->instructions_end, cie, fde,
  581. frame, pc);
  582. /* FDE instructions */
  583. dwarf_cfa_execute_insns(fde->instructions, fde->end, cie,
  584. fde, frame, pc);
  585. /* Calculate the CFA */
  586. switch (frame->flags) {
  587. case DWARF_FRAME_CFA_REG_OFFSET:
  588. if (prev) {
  589. reg = dwarf_frame_reg(prev, frame->cfa_register);
  590. UNWINDER_BUG_ON(!reg);
  591. UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET);
  592. addr = prev->cfa + reg->addr;
  593. frame->cfa = __raw_readl(addr);
  594. } else {
  595. /*
  596. * Again, we're starting from the top of the
  597. * stack. We need to physically read
  598. * the contents of a register in order to get
  599. * the Canonical Frame Address for this
  600. * function.
  601. */
  602. frame->cfa = dwarf_read_arch_reg(frame->cfa_register);
  603. }
  604. frame->cfa += frame->cfa_offset;
  605. break;
  606. default:
  607. UNWINDER_BUG();
  608. }
  609. reg = dwarf_frame_reg(frame, DWARF_ARCH_RA_REG);
  610. /*
  611. * If we haven't seen the return address register or the return
  612. * address column is undefined then we must assume that this is
  613. * the end of the callstack.
  614. */
  615. if (!reg || reg->flags == DWARF_UNDEFINED)
  616. goto bail;
  617. UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET);
  618. addr = frame->cfa + reg->addr;
  619. frame->return_addr = __raw_readl(addr);
  620. /*
  621. * Ah, the joys of unwinding through interrupts.
  622. *
  623. * Interrupts are tricky - the DWARF info needs to be _really_
  624. * accurate and unfortunately I'm seeing a lot of bogus DWARF
  625. * info. For example, I've seen interrupts occur in epilogues
  626. * just after the frame pointer (r14) had been restored. The
  627. * problem was that the DWARF info claimed that the CFA could be
  628. * reached by using the value of the frame pointer before it was
  629. * restored.
  630. *
  631. * So until the compiler can be trusted to produce reliable
  632. * DWARF info when it really matters, let's stop unwinding once
  633. * we've calculated the function that was interrupted.
  634. */
  635. if (prev && prev->pc == (unsigned long)ret_from_irq)
  636. frame->return_addr = 0;
  637. return frame;
  638. bail:
  639. dwarf_free_frame(frame);
  640. return NULL;
  641. }
  642. static int dwarf_parse_cie(void *entry, void *p, unsigned long len,
  643. unsigned char *end, struct module *mod)
  644. {
  645. struct rb_node **rb_node = &cie_root.rb_node;
  646. struct rb_node *parent = *rb_node;
  647. struct dwarf_cie *cie;
  648. unsigned long flags;
  649. int count;
  650. cie = kzalloc(sizeof(*cie), GFP_KERNEL);
  651. if (!cie)
  652. return -ENOMEM;
  653. cie->length = len;
  654. /*
  655. * Record the offset into the .eh_frame section
  656. * for this CIE. It allows this CIE to be
  657. * quickly and easily looked up from the
  658. * corresponding FDE.
  659. */
  660. cie->cie_pointer = (unsigned long)entry;
  661. cie->version = *(char *)p++;
  662. UNWINDER_BUG_ON(cie->version != 1);
  663. cie->augmentation = p;
  664. p += strlen(cie->augmentation) + 1;
  665. count = dwarf_read_uleb128(p, &cie->code_alignment_factor);
  666. p += count;
  667. count = dwarf_read_leb128(p, &cie->data_alignment_factor);
  668. p += count;
  669. /*
  670. * Which column in the rule table contains the
  671. * return address?
  672. */
  673. if (cie->version == 1) {
  674. cie->return_address_reg = __raw_readb(p);
  675. p++;
  676. } else {
  677. count = dwarf_read_uleb128(p, &cie->return_address_reg);
  678. p += count;
  679. }
  680. if (cie->augmentation[0] == 'z') {
  681. unsigned int length, count;
  682. cie->flags |= DWARF_CIE_Z_AUGMENTATION;
  683. count = dwarf_read_uleb128(p, &length);
  684. p += count;
  685. UNWINDER_BUG_ON((unsigned char *)p > end);
  686. cie->initial_instructions = p + length;
  687. cie->augmentation++;
  688. }
  689. while (*cie->augmentation) {
  690. /*
  691. * "L" indicates a byte showing how the
  692. * LSDA pointer is encoded. Skip it.
  693. */
  694. if (*cie->augmentation == 'L') {
  695. p++;
  696. cie->augmentation++;
  697. } else if (*cie->augmentation == 'R') {
  698. /*
  699. * "R" indicates a byte showing
  700. * how FDE addresses are
  701. * encoded.
  702. */
  703. cie->encoding = *(char *)p++;
  704. cie->augmentation++;
  705. } else if (*cie->augmentation == 'P') {
  706. /*
  707. * "R" indicates a personality
  708. * routine in the CIE
  709. * augmentation.
  710. */
  711. UNWINDER_BUG();
  712. } else if (*cie->augmentation == 'S') {
  713. UNWINDER_BUG();
  714. } else {
  715. /*
  716. * Unknown augmentation. Assume
  717. * 'z' augmentation.
  718. */
  719. p = cie->initial_instructions;
  720. UNWINDER_BUG_ON(!p);
  721. break;
  722. }
  723. }
  724. cie->initial_instructions = p;
  725. cie->instructions_end = end;
  726. /* Add to list */
  727. spin_lock_irqsave(&dwarf_cie_lock, flags);
  728. while (*rb_node) {
  729. struct dwarf_cie *cie_tmp;
  730. cie_tmp = rb_entry(*rb_node, struct dwarf_cie, node);
  731. parent = *rb_node;
  732. if (cie->cie_pointer < cie_tmp->cie_pointer)
  733. rb_node = &parent->rb_left;
  734. else if (cie->cie_pointer >= cie_tmp->cie_pointer)
  735. rb_node = &parent->rb_right;
  736. else
  737. WARN_ON(1);
  738. }
  739. rb_link_node(&cie->node, parent, rb_node);
  740. rb_insert_color(&cie->node, &cie_root);
  741. #ifdef CONFIG_MODULES
  742. if (mod != NULL)
  743. list_add_tail(&cie->link, &mod->arch.cie_list);
  744. #endif
  745. spin_unlock_irqrestore(&dwarf_cie_lock, flags);
  746. return 0;
  747. }
  748. static int dwarf_parse_fde(void *entry, u32 entry_type,
  749. void *start, unsigned long len,
  750. unsigned char *end, struct module *mod)
  751. {
  752. struct rb_node **rb_node = &fde_root.rb_node;
  753. struct rb_node *parent = *rb_node;
  754. struct dwarf_fde *fde;
  755. struct dwarf_cie *cie;
  756. unsigned long flags;
  757. int count;
  758. void *p = start;
  759. fde = kzalloc(sizeof(*fde), GFP_KERNEL);
  760. if (!fde)
  761. return -ENOMEM;
  762. fde->length = len;
  763. /*
  764. * In a .eh_frame section the CIE pointer is the
  765. * delta between the address within the FDE
  766. */
  767. fde->cie_pointer = (unsigned long)(p - entry_type - 4);
  768. cie = dwarf_lookup_cie(fde->cie_pointer);
  769. fde->cie = cie;
  770. if (cie->encoding)
  771. count = dwarf_read_encoded_value(p, &fde->initial_location,
  772. cie->encoding);
  773. else
  774. count = dwarf_read_addr(p, &fde->initial_location);
  775. p += count;
  776. if (cie->encoding)
  777. count = dwarf_read_encoded_value(p, &fde->address_range,
  778. cie->encoding & 0x0f);
  779. else
  780. count = dwarf_read_addr(p, &fde->address_range);
  781. p += count;
  782. if (fde->cie->flags & DWARF_CIE_Z_AUGMENTATION) {
  783. unsigned int length;
  784. count = dwarf_read_uleb128(p, &length);
  785. p += count + length;
  786. }
  787. /* Call frame instructions. */
  788. fde->instructions = p;
  789. fde->end = end;
  790. /* Add to list. */
  791. spin_lock_irqsave(&dwarf_fde_lock, flags);
  792. while (*rb_node) {
  793. struct dwarf_fde *fde_tmp;
  794. unsigned long tmp_start, tmp_end;
  795. unsigned long start, end;
  796. fde_tmp = rb_entry(*rb_node, struct dwarf_fde, node);
  797. start = fde->initial_location;
  798. end = fde->initial_location + fde->address_range;
  799. tmp_start = fde_tmp->initial_location;
  800. tmp_end = fde_tmp->initial_location + fde_tmp->address_range;
  801. parent = *rb_node;
  802. if (start < tmp_start)
  803. rb_node = &parent->rb_left;
  804. else if (start >= tmp_end)
  805. rb_node = &parent->rb_right;
  806. else
  807. WARN_ON(1);
  808. }
  809. rb_link_node(&fde->node, parent, rb_node);
  810. rb_insert_color(&fde->node, &fde_root);
  811. #ifdef CONFIG_MODULES
  812. if (mod != NULL)
  813. list_add_tail(&fde->link, &mod->arch.fde_list);
  814. #endif
  815. spin_unlock_irqrestore(&dwarf_fde_lock, flags);
  816. return 0;
  817. }
  818. static void dwarf_unwinder_dump(struct task_struct *task,
  819. struct pt_regs *regs,
  820. unsigned long *sp,
  821. const struct stacktrace_ops *ops,
  822. void *data)
  823. {
  824. struct dwarf_frame *frame, *_frame;
  825. unsigned long return_addr;
  826. _frame = NULL;
  827. return_addr = 0;
  828. while (1) {
  829. frame = dwarf_unwind_stack(return_addr, _frame);
  830. if (_frame)
  831. dwarf_free_frame(_frame);
  832. _frame = frame;
  833. if (!frame || !frame->return_addr)
  834. break;
  835. return_addr = frame->return_addr;
  836. ops->address(data, return_addr, 1);
  837. }
  838. if (frame)
  839. dwarf_free_frame(frame);
  840. }
  841. static struct unwinder dwarf_unwinder = {
  842. .name = "dwarf-unwinder",
  843. .dump = dwarf_unwinder_dump,
  844. .rating = 150,
  845. };
  846. static void __init dwarf_unwinder_cleanup(void)
  847. {
  848. struct dwarf_fde *fde, *next_fde;
  849. struct dwarf_cie *cie, *next_cie;
  850. /*
  851. * Deallocate all the memory allocated for the DWARF unwinder.
  852. * Traverse all the FDE/CIE lists and remove and free all the
  853. * memory associated with those data structures.
  854. */
  855. rbtree_postorder_for_each_entry_safe(fde, next_fde, &fde_root, node)
  856. kfree(fde);
  857. rbtree_postorder_for_each_entry_safe(cie, next_cie, &cie_root, node)
  858. kfree(cie);
  859. if (dwarf_reg_pool)
  860. mempool_destroy(dwarf_reg_pool);
  861. if (dwarf_frame_pool)
  862. mempool_destroy(dwarf_frame_pool);
  863. kmem_cache_destroy(dwarf_reg_cachep);
  864. kmem_cache_destroy(dwarf_frame_cachep);
  865. }
  866. /**
  867. * dwarf_parse_section - parse DWARF section
  868. * @eh_frame_start: start address of the .eh_frame section
  869. * @eh_frame_end: end address of the .eh_frame section
  870. * @mod: the kernel module containing the .eh_frame section
  871. *
  872. * Parse the information in a .eh_frame section.
  873. */
  874. static int dwarf_parse_section(char *eh_frame_start, char *eh_frame_end,
  875. struct module *mod)
  876. {
  877. u32 entry_type;
  878. void *p, *entry;
  879. int count, err = 0;
  880. unsigned long len = 0;
  881. unsigned int c_entries, f_entries;
  882. unsigned char *end;
  883. c_entries = 0;
  884. f_entries = 0;
  885. entry = eh_frame_start;
  886. while ((char *)entry < eh_frame_end) {
  887. p = entry;
  888. count = dwarf_entry_len(p, &len);
  889. if (count == 0) {
  890. /*
  891. * We read a bogus length field value. There is
  892. * nothing we can do here apart from disabling
  893. * the DWARF unwinder. We can't even skip this
  894. * entry and move to the next one because 'len'
  895. * tells us where our next entry is.
  896. */
  897. err = -EINVAL;
  898. goto out;
  899. } else
  900. p += count;
  901. /* initial length does not include itself */
  902. end = p + len;
  903. entry_type = get_unaligned((u32 *)p);
  904. p += 4;
  905. if (entry_type == DW_EH_FRAME_CIE) {
  906. err = dwarf_parse_cie(entry, p, len, end, mod);
  907. if (err < 0)
  908. goto out;
  909. else
  910. c_entries++;
  911. } else {
  912. err = dwarf_parse_fde(entry, entry_type, p, len,
  913. end, mod);
  914. if (err < 0)
  915. goto out;
  916. else
  917. f_entries++;
  918. }
  919. entry = (char *)entry + len + 4;
  920. }
  921. printk(KERN_INFO "DWARF unwinder initialised: read %u CIEs, %u FDEs\n",
  922. c_entries, f_entries);
  923. return 0;
  924. out:
  925. return err;
  926. }
  927. #ifdef CONFIG_MODULES
  928. int module_dwarf_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
  929. struct module *me)
  930. {
  931. unsigned int i, err;
  932. unsigned long start, end;
  933. char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  934. start = end = 0;
  935. for (i = 1; i < hdr->e_shnum; i++) {
  936. /* Alloc bit cleared means "ignore it." */
  937. if ((sechdrs[i].sh_flags & SHF_ALLOC)
  938. && !strcmp(secstrings+sechdrs[i].sh_name, ".eh_frame")) {
  939. start = sechdrs[i].sh_addr;
  940. end = start + sechdrs[i].sh_size;
  941. break;
  942. }
  943. }
  944. /* Did we find the .eh_frame section? */
  945. if (i != hdr->e_shnum) {
  946. INIT_LIST_HEAD(&me->arch.cie_list);
  947. INIT_LIST_HEAD(&me->arch.fde_list);
  948. err = dwarf_parse_section((char *)start, (char *)end, me);
  949. if (err) {
  950. printk(KERN_WARNING "%s: failed to parse DWARF info\n",
  951. me->name);
  952. return err;
  953. }
  954. }
  955. return 0;
  956. }
  957. /**
  958. * module_dwarf_cleanup - remove FDE/CIEs associated with @mod
  959. * @mod: the module that is being unloaded
  960. *
  961. * Remove any FDEs and CIEs from the global lists that came from
  962. * @mod's .eh_frame section because @mod is being unloaded.
  963. */
  964. void module_dwarf_cleanup(struct module *mod)
  965. {
  966. struct dwarf_fde *fde, *ftmp;
  967. struct dwarf_cie *cie, *ctmp;
  968. unsigned long flags;
  969. spin_lock_irqsave(&dwarf_cie_lock, flags);
  970. list_for_each_entry_safe(cie, ctmp, &mod->arch.cie_list, link) {
  971. list_del(&cie->link);
  972. rb_erase(&cie->node, &cie_root);
  973. kfree(cie);
  974. }
  975. spin_unlock_irqrestore(&dwarf_cie_lock, flags);
  976. spin_lock_irqsave(&dwarf_fde_lock, flags);
  977. list_for_each_entry_safe(fde, ftmp, &mod->arch.fde_list, link) {
  978. list_del(&fde->link);
  979. rb_erase(&fde->node, &fde_root);
  980. kfree(fde);
  981. }
  982. spin_unlock_irqrestore(&dwarf_fde_lock, flags);
  983. }
  984. #endif /* CONFIG_MODULES */
  985. /**
  986. * dwarf_unwinder_init - initialise the dwarf unwinder
  987. *
  988. * Build the data structures describing the .dwarf_frame section to
  989. * make it easier to lookup CIE and FDE entries. Because the
  990. * .eh_frame section is packed as tightly as possible it is not
  991. * easy to lookup the FDE for a given PC, so we build a list of FDE
  992. * and CIE entries that make it easier.
  993. */
  994. static int __init dwarf_unwinder_init(void)
  995. {
  996. int err = -ENOMEM;
  997. dwarf_frame_cachep = kmem_cache_create("dwarf_frames",
  998. sizeof(struct dwarf_frame), 0,
  999. SLAB_PANIC | SLAB_HWCACHE_ALIGN | SLAB_NOTRACK, NULL);
  1000. dwarf_reg_cachep = kmem_cache_create("dwarf_regs",
  1001. sizeof(struct dwarf_reg), 0,
  1002. SLAB_PANIC | SLAB_HWCACHE_ALIGN | SLAB_NOTRACK, NULL);
  1003. dwarf_frame_pool = mempool_create_slab_pool(DWARF_FRAME_MIN_REQ,
  1004. dwarf_frame_cachep);
  1005. if (!dwarf_frame_pool)
  1006. goto out;
  1007. dwarf_reg_pool = mempool_create_slab_pool(DWARF_REG_MIN_REQ,
  1008. dwarf_reg_cachep);
  1009. if (!dwarf_reg_pool)
  1010. goto out;
  1011. err = dwarf_parse_section(__start_eh_frame, __stop_eh_frame, NULL);
  1012. if (err)
  1013. goto out;
  1014. err = unwinder_register(&dwarf_unwinder);
  1015. if (err)
  1016. goto out;
  1017. dwarf_unwinder_ready = 1;
  1018. return 0;
  1019. out:
  1020. printk(KERN_ERR "Failed to initialise DWARF unwinder: %d\n", err);
  1021. dwarf_unwinder_cleanup();
  1022. return err;
  1023. }
  1024. early_initcall(dwarf_unwinder_init);