dwarf.c 29 KB

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