dwarf.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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. */
  15. /* #define DEBUG */
  16. #include <linux/kernel.h>
  17. #include <linux/io.h>
  18. #include <linux/list.h>
  19. #include <linux/mm.h>
  20. #include <asm/dwarf.h>
  21. #include <asm/unwinder.h>
  22. #include <asm/sections.h>
  23. #include <asm/unaligned.h>
  24. #include <asm/dwarf.h>
  25. #include <asm/stacktrace.h>
  26. static LIST_HEAD(dwarf_cie_list);
  27. DEFINE_SPINLOCK(dwarf_cie_lock);
  28. static LIST_HEAD(dwarf_fde_list);
  29. DEFINE_SPINLOCK(dwarf_fde_lock);
  30. static struct dwarf_cie *cached_cie;
  31. /*
  32. * Figure out whether we need to allocate some dwarf registers. If dwarf
  33. * registers have already been allocated then we may need to realloc
  34. * them. "reg" is a register number that we need to be able to access
  35. * after this call.
  36. *
  37. * Register numbers start at zero, therefore we need to allocate space
  38. * for "reg" + 1 registers.
  39. */
  40. static void dwarf_frame_alloc_regs(struct dwarf_frame *frame,
  41. unsigned int reg)
  42. {
  43. struct dwarf_reg *regs;
  44. unsigned int num_regs = reg + 1;
  45. size_t new_size;
  46. size_t old_size;
  47. new_size = num_regs * sizeof(*regs);
  48. old_size = frame->num_regs * sizeof(*regs);
  49. /* Fast path: don't allocate any regs if we've already got enough. */
  50. if (frame->num_regs >= num_regs)
  51. return;
  52. regs = kzalloc(new_size, GFP_ATOMIC);
  53. if (!regs) {
  54. printk(KERN_WARNING "Unable to allocate DWARF registers\n");
  55. /*
  56. * Let's just bomb hard here, we have no way to
  57. * gracefully recover.
  58. */
  59. BUG();
  60. }
  61. if (frame->regs) {
  62. memcpy(regs, frame->regs, old_size);
  63. kfree(frame->regs);
  64. }
  65. frame->regs = regs;
  66. frame->num_regs = num_regs;
  67. }
  68. /**
  69. * dwarf_read_addr - read dwarf data
  70. * @src: source address of data
  71. * @dst: destination address to store the data to
  72. *
  73. * Read 'n' bytes from @src, where 'n' is the size of an address on
  74. * the native machine. We return the number of bytes read, which
  75. * should always be 'n'. We also have to be careful when reading
  76. * from @src and writing to @dst, because they can be arbitrarily
  77. * aligned. Return 'n' - the number of bytes read.
  78. */
  79. static inline int dwarf_read_addr(unsigned long *src, unsigned long *dst)
  80. {
  81. u32 val = get_unaligned(src);
  82. put_unaligned(val, dst);
  83. return sizeof(unsigned long *);
  84. }
  85. /**
  86. * dwarf_read_uleb128 - read unsigned LEB128 data
  87. * @addr: the address where the ULEB128 data is stored
  88. * @ret: address to store the result
  89. *
  90. * Decode an unsigned LEB128 encoded datum. The algorithm is taken
  91. * from Appendix C of the DWARF 3 spec. For information on the
  92. * encodings refer to section "7.6 - Variable Length Data". Return
  93. * the number of bytes read.
  94. */
  95. static inline unsigned long dwarf_read_uleb128(char *addr, unsigned int *ret)
  96. {
  97. unsigned int result;
  98. unsigned char byte;
  99. int shift, count;
  100. result = 0;
  101. shift = 0;
  102. count = 0;
  103. while (1) {
  104. byte = __raw_readb(addr);
  105. addr++;
  106. count++;
  107. result |= (byte & 0x7f) << shift;
  108. shift += 7;
  109. if (!(byte & 0x80))
  110. break;
  111. }
  112. *ret = result;
  113. return count;
  114. }
  115. /**
  116. * dwarf_read_leb128 - read signed LEB128 data
  117. * @addr: the address of the LEB128 encoded data
  118. * @ret: address to store the result
  119. *
  120. * Decode signed LEB128 data. The algorithm is taken from Appendix
  121. * C of the DWARF 3 spec. Return the number of bytes read.
  122. */
  123. static inline unsigned long dwarf_read_leb128(char *addr, int *ret)
  124. {
  125. unsigned char byte;
  126. int result, shift;
  127. int num_bits;
  128. int count;
  129. result = 0;
  130. shift = 0;
  131. count = 0;
  132. while (1) {
  133. byte = __raw_readb(addr);
  134. addr++;
  135. result |= (byte & 0x7f) << shift;
  136. shift += 7;
  137. count++;
  138. if (!(byte & 0x80))
  139. break;
  140. }
  141. /* The number of bits in a signed integer. */
  142. num_bits = 8 * sizeof(result);
  143. if ((shift < num_bits) && (byte & 0x40))
  144. result |= (-1 << shift);
  145. *ret = result;
  146. return count;
  147. }
  148. /**
  149. * dwarf_read_encoded_value - return the decoded value at @addr
  150. * @addr: the address of the encoded value
  151. * @val: where to write the decoded value
  152. * @encoding: the encoding with which we can decode @addr
  153. *
  154. * GCC emits encoded address in the .eh_frame FDE entries. Decode
  155. * the value at @addr using @encoding. The decoded value is written
  156. * to @val and the number of bytes read is returned.
  157. */
  158. static int dwarf_read_encoded_value(char *addr, unsigned long *val,
  159. char encoding)
  160. {
  161. unsigned long decoded_addr = 0;
  162. int count = 0;
  163. switch (encoding & 0x70) {
  164. case DW_EH_PE_absptr:
  165. break;
  166. case DW_EH_PE_pcrel:
  167. decoded_addr = (unsigned long)addr;
  168. break;
  169. default:
  170. pr_debug("encoding=0x%x\n", (encoding & 0x70));
  171. BUG();
  172. }
  173. if ((encoding & 0x07) == 0x00)
  174. encoding |= DW_EH_PE_udata4;
  175. switch (encoding & 0x0f) {
  176. case DW_EH_PE_sdata4:
  177. case DW_EH_PE_udata4:
  178. count += 4;
  179. decoded_addr += get_unaligned((u32 *)addr);
  180. __raw_writel(decoded_addr, val);
  181. break;
  182. default:
  183. pr_debug("encoding=0x%x\n", encoding);
  184. BUG();
  185. }
  186. return count;
  187. }
  188. /**
  189. * dwarf_entry_len - return the length of an FDE or CIE
  190. * @addr: the address of the entry
  191. * @len: the length of the entry
  192. *
  193. * Read the initial_length field of the entry and store the size of
  194. * the entry in @len. We return the number of bytes read. Return a
  195. * count of 0 on error.
  196. */
  197. static inline int dwarf_entry_len(char *addr, unsigned long *len)
  198. {
  199. u32 initial_len;
  200. int count;
  201. initial_len = get_unaligned((u32 *)addr);
  202. count = 4;
  203. /*
  204. * An initial length field value in the range DW_LEN_EXT_LO -
  205. * DW_LEN_EXT_HI indicates an extension, and should not be
  206. * interpreted as a length. The only extension that we currently
  207. * understand is the use of DWARF64 addresses.
  208. */
  209. if (initial_len >= DW_EXT_LO && initial_len <= DW_EXT_HI) {
  210. /*
  211. * The 64-bit length field immediately follows the
  212. * compulsory 32-bit length field.
  213. */
  214. if (initial_len == DW_EXT_DWARF64) {
  215. *len = get_unaligned((u64 *)addr + 4);
  216. count = 12;
  217. } else {
  218. printk(KERN_WARNING "Unknown DWARF extension\n");
  219. count = 0;
  220. }
  221. } else
  222. *len = initial_len;
  223. return count;
  224. }
  225. /**
  226. * dwarf_lookup_cie - locate the cie
  227. * @cie_ptr: pointer to help with lookup
  228. */
  229. static struct dwarf_cie *dwarf_lookup_cie(unsigned long cie_ptr)
  230. {
  231. struct dwarf_cie *cie, *n;
  232. unsigned long flags;
  233. spin_lock_irqsave(&dwarf_cie_lock, flags);
  234. /*
  235. * We've cached the last CIE we looked up because chances are
  236. * that the FDE wants this CIE.
  237. */
  238. if (cached_cie && cached_cie->cie_pointer == cie_ptr) {
  239. cie = cached_cie;
  240. goto out;
  241. }
  242. list_for_each_entry_safe(cie, n, &dwarf_cie_list, link) {
  243. if (cie->cie_pointer == cie_ptr) {
  244. cached_cie = cie;
  245. break;
  246. }
  247. }
  248. /* Couldn't find the entry in the list. */
  249. if (&cie->link == &dwarf_cie_list)
  250. cie = NULL;
  251. out:
  252. spin_unlock_irqrestore(&dwarf_cie_lock, flags);
  253. return cie;
  254. }
  255. /**
  256. * dwarf_lookup_fde - locate the FDE that covers pc
  257. * @pc: the program counter
  258. */
  259. struct dwarf_fde *dwarf_lookup_fde(unsigned long pc)
  260. {
  261. unsigned long flags;
  262. struct dwarf_fde *fde, *n;
  263. spin_lock_irqsave(&dwarf_fde_lock, flags);
  264. list_for_each_entry_safe(fde, n, &dwarf_fde_list, link) {
  265. unsigned long start, end;
  266. start = fde->initial_location;
  267. end = fde->initial_location + fde->address_range;
  268. if (pc >= start && pc < end)
  269. break;
  270. }
  271. /* Couldn't find the entry in the list. */
  272. if (&fde->link == &dwarf_fde_list)
  273. fde = NULL;
  274. spin_unlock_irqrestore(&dwarf_fde_lock, flags);
  275. return fde;
  276. }
  277. /**
  278. * dwarf_cfa_execute_insns - execute instructions to calculate a CFA
  279. * @insn_start: address of the first instruction
  280. * @insn_end: address of the last instruction
  281. * @cie: the CIE for this function
  282. * @fde: the FDE for this function
  283. * @frame: the instructions calculate the CFA for this frame
  284. * @pc: the program counter of the address we're interested in
  285. *
  286. * Execute the Call Frame instruction sequence starting at
  287. * @insn_start and ending at @insn_end. The instructions describe
  288. * how to calculate the Canonical Frame Address of a stackframe.
  289. * Store the results in @frame.
  290. */
  291. static int dwarf_cfa_execute_insns(unsigned char *insn_start,
  292. unsigned char *insn_end,
  293. struct dwarf_cie *cie,
  294. struct dwarf_fde *fde,
  295. struct dwarf_frame *frame,
  296. unsigned long pc)
  297. {
  298. unsigned char insn;
  299. unsigned char *current_insn;
  300. unsigned int count, delta, reg, expr_len, offset;
  301. current_insn = insn_start;
  302. while (current_insn < insn_end && frame->pc <= pc) {
  303. insn = __raw_readb(current_insn++);
  304. /*
  305. * Firstly, handle the opcodes that embed their operands
  306. * in the instructions.
  307. */
  308. switch (DW_CFA_opcode(insn)) {
  309. case DW_CFA_advance_loc:
  310. delta = DW_CFA_operand(insn);
  311. delta *= cie->code_alignment_factor;
  312. frame->pc += delta;
  313. continue;
  314. /* NOTREACHED */
  315. case DW_CFA_offset:
  316. reg = DW_CFA_operand(insn);
  317. count = dwarf_read_uleb128(current_insn, &offset);
  318. current_insn += count;
  319. offset *= cie->data_alignment_factor;
  320. dwarf_frame_alloc_regs(frame, reg);
  321. frame->regs[reg].addr = offset;
  322. frame->regs[reg].flags |= DWARF_REG_OFFSET;
  323. continue;
  324. /* NOTREACHED */
  325. case DW_CFA_restore:
  326. reg = DW_CFA_operand(insn);
  327. continue;
  328. /* NOTREACHED */
  329. }
  330. /*
  331. * Secondly, handle the opcodes that don't embed their
  332. * operands in the instruction.
  333. */
  334. switch (insn) {
  335. case DW_CFA_nop:
  336. continue;
  337. case DW_CFA_advance_loc1:
  338. delta = *current_insn++;
  339. frame->pc += delta * cie->code_alignment_factor;
  340. break;
  341. case DW_CFA_advance_loc2:
  342. delta = get_unaligned((u16 *)current_insn);
  343. current_insn += 2;
  344. frame->pc += delta * cie->code_alignment_factor;
  345. break;
  346. case DW_CFA_advance_loc4:
  347. delta = get_unaligned((u32 *)current_insn);
  348. current_insn += 4;
  349. frame->pc += delta * cie->code_alignment_factor;
  350. break;
  351. case DW_CFA_offset_extended:
  352. count = dwarf_read_uleb128(current_insn, &reg);
  353. current_insn += count;
  354. count = dwarf_read_uleb128(current_insn, &offset);
  355. current_insn += count;
  356. offset *= cie->data_alignment_factor;
  357. break;
  358. case DW_CFA_restore_extended:
  359. count = dwarf_read_uleb128(current_insn, &reg);
  360. current_insn += count;
  361. break;
  362. case DW_CFA_undefined:
  363. count = dwarf_read_uleb128(current_insn, &reg);
  364. current_insn += count;
  365. break;
  366. case DW_CFA_def_cfa:
  367. count = dwarf_read_uleb128(current_insn,
  368. &frame->cfa_register);
  369. current_insn += count;
  370. count = dwarf_read_uleb128(current_insn,
  371. &frame->cfa_offset);
  372. current_insn += count;
  373. frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
  374. break;
  375. case DW_CFA_def_cfa_register:
  376. count = dwarf_read_uleb128(current_insn,
  377. &frame->cfa_register);
  378. current_insn += count;
  379. frame->cfa_offset = 0;
  380. frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
  381. break;
  382. case DW_CFA_def_cfa_offset:
  383. count = dwarf_read_uleb128(current_insn, &offset);
  384. current_insn += count;
  385. frame->cfa_offset = offset;
  386. break;
  387. case DW_CFA_def_cfa_expression:
  388. count = dwarf_read_uleb128(current_insn, &expr_len);
  389. current_insn += count;
  390. frame->cfa_expr = current_insn;
  391. frame->cfa_expr_len = expr_len;
  392. current_insn += expr_len;
  393. frame->flags |= DWARF_FRAME_CFA_REG_EXP;
  394. break;
  395. case DW_CFA_offset_extended_sf:
  396. count = dwarf_read_uleb128(current_insn, &reg);
  397. current_insn += count;
  398. count = dwarf_read_leb128(current_insn, &offset);
  399. current_insn += count;
  400. offset *= cie->data_alignment_factor;
  401. dwarf_frame_alloc_regs(frame, reg);
  402. frame->regs[reg].flags |= DWARF_REG_OFFSET;
  403. frame->regs[reg].addr = offset;
  404. break;
  405. case DW_CFA_val_offset:
  406. count = dwarf_read_uleb128(current_insn, &reg);
  407. current_insn += count;
  408. count = dwarf_read_leb128(current_insn, &offset);
  409. offset *= cie->data_alignment_factor;
  410. frame->regs[reg].flags |= DWARF_REG_OFFSET;
  411. frame->regs[reg].addr = offset;
  412. break;
  413. default:
  414. pr_debug("unhandled DWARF instruction 0x%x\n", insn);
  415. break;
  416. }
  417. }
  418. return 0;
  419. }
  420. /**
  421. * dwarf_unwind_stack - recursively unwind the stack
  422. * @pc: address of the function to unwind
  423. * @prev: struct dwarf_frame of the previous stackframe on the callstack
  424. *
  425. * Return a struct dwarf_frame representing the most recent frame
  426. * on the callstack. Each of the lower (older) stack frames are
  427. * linked via the "prev" member.
  428. */
  429. struct dwarf_frame *dwarf_unwind_stack(unsigned long pc,
  430. struct dwarf_frame *prev)
  431. {
  432. struct dwarf_frame *frame;
  433. struct dwarf_cie *cie;
  434. struct dwarf_fde *fde;
  435. unsigned long addr;
  436. int i, offset;
  437. /*
  438. * If this is the first invocation of this recursive function we
  439. * need get the contents of a physical register to get the CFA
  440. * in order to begin the virtual unwinding of the stack.
  441. *
  442. * NOTE: the return address is guaranteed to be setup by the
  443. * time this function makes its first function call.
  444. */
  445. if (!pc && !prev)
  446. pc = (unsigned long)current_text_addr();
  447. frame = kzalloc(sizeof(*frame), GFP_ATOMIC);
  448. if (!frame)
  449. return NULL;
  450. frame->prev = prev;
  451. fde = dwarf_lookup_fde(pc);
  452. if (!fde) {
  453. /*
  454. * This is our normal exit path - the one that stops the
  455. * recursion. There's two reasons why we might exit
  456. * here,
  457. *
  458. * a) pc has no asscociated DWARF frame info and so
  459. * we don't know how to unwind this frame. This is
  460. * usually the case when we're trying to unwind a
  461. * frame that was called from some assembly code
  462. * that has no DWARF info, e.g. syscalls.
  463. *
  464. * b) the DEBUG info for pc is bogus. There's
  465. * really no way to distinguish this case from the
  466. * case above, which sucks because we could print a
  467. * warning here.
  468. */
  469. return NULL;
  470. }
  471. cie = dwarf_lookup_cie(fde->cie_pointer);
  472. frame->pc = fde->initial_location;
  473. /* CIE initial instructions */
  474. dwarf_cfa_execute_insns(cie->initial_instructions,
  475. cie->instructions_end, cie, fde,
  476. frame, pc);
  477. /* FDE instructions */
  478. dwarf_cfa_execute_insns(fde->instructions, fde->end, cie,
  479. fde, frame, pc);
  480. /* Calculate the CFA */
  481. switch (frame->flags) {
  482. case DWARF_FRAME_CFA_REG_OFFSET:
  483. if (prev) {
  484. BUG_ON(!prev->regs[frame->cfa_register].flags);
  485. addr = prev->cfa;
  486. addr += prev->regs[frame->cfa_register].addr;
  487. frame->cfa = __raw_readl(addr);
  488. } else {
  489. /*
  490. * Again, this is the first invocation of this
  491. * recurisve function. We need to physically
  492. * read the contents of a register in order to
  493. * get the Canonical Frame Address for this
  494. * function.
  495. */
  496. frame->cfa = dwarf_read_arch_reg(frame->cfa_register);
  497. }
  498. frame->cfa += frame->cfa_offset;
  499. break;
  500. default:
  501. BUG();
  502. }
  503. /* If we haven't seen the return address reg, we're screwed. */
  504. BUG_ON(!frame->regs[DWARF_ARCH_RA_REG].flags);
  505. for (i = 0; i <= frame->num_regs; i++) {
  506. struct dwarf_reg *reg = &frame->regs[i];
  507. if (!reg->flags)
  508. continue;
  509. offset = reg->addr;
  510. offset += frame->cfa;
  511. }
  512. addr = frame->cfa + frame->regs[DWARF_ARCH_RA_REG].addr;
  513. frame->return_addr = __raw_readl(addr);
  514. frame->next = dwarf_unwind_stack(frame->return_addr, frame);
  515. return frame;
  516. }
  517. static int dwarf_parse_cie(void *entry, void *p, unsigned long len,
  518. unsigned char *end)
  519. {
  520. struct dwarf_cie *cie;
  521. unsigned long flags;
  522. int count;
  523. cie = kzalloc(sizeof(*cie), GFP_KERNEL);
  524. if (!cie)
  525. return -ENOMEM;
  526. cie->length = len;
  527. /*
  528. * Record the offset into the .eh_frame section
  529. * for this CIE. It allows this CIE to be
  530. * quickly and easily looked up from the
  531. * corresponding FDE.
  532. */
  533. cie->cie_pointer = (unsigned long)entry;
  534. cie->version = *(char *)p++;
  535. BUG_ON(cie->version != 1);
  536. cie->augmentation = p;
  537. p += strlen(cie->augmentation) + 1;
  538. count = dwarf_read_uleb128(p, &cie->code_alignment_factor);
  539. p += count;
  540. count = dwarf_read_leb128(p, &cie->data_alignment_factor);
  541. p += count;
  542. /*
  543. * Which column in the rule table contains the
  544. * return address?
  545. */
  546. if (cie->version == 1) {
  547. cie->return_address_reg = __raw_readb(p);
  548. p++;
  549. } else {
  550. count = dwarf_read_uleb128(p, &cie->return_address_reg);
  551. p += count;
  552. }
  553. if (cie->augmentation[0] == 'z') {
  554. unsigned int length, count;
  555. cie->flags |= DWARF_CIE_Z_AUGMENTATION;
  556. count = dwarf_read_uleb128(p, &length);
  557. p += count;
  558. BUG_ON((unsigned char *)p > end);
  559. cie->initial_instructions = p + length;
  560. cie->augmentation++;
  561. }
  562. while (*cie->augmentation) {
  563. /*
  564. * "L" indicates a byte showing how the
  565. * LSDA pointer is encoded. Skip it.
  566. */
  567. if (*cie->augmentation == 'L') {
  568. p++;
  569. cie->augmentation++;
  570. } else if (*cie->augmentation == 'R') {
  571. /*
  572. * "R" indicates a byte showing
  573. * how FDE addresses are
  574. * encoded.
  575. */
  576. cie->encoding = *(char *)p++;
  577. cie->augmentation++;
  578. } else if (*cie->augmentation == 'P') {
  579. /*
  580. * "R" indicates a personality
  581. * routine in the CIE
  582. * augmentation.
  583. */
  584. BUG();
  585. } else if (*cie->augmentation == 'S') {
  586. BUG();
  587. } else {
  588. /*
  589. * Unknown augmentation. Assume
  590. * 'z' augmentation.
  591. */
  592. p = cie->initial_instructions;
  593. BUG_ON(!p);
  594. break;
  595. }
  596. }
  597. cie->initial_instructions = p;
  598. cie->instructions_end = end;
  599. /* Add to list */
  600. spin_lock_irqsave(&dwarf_cie_lock, flags);
  601. list_add_tail(&cie->link, &dwarf_cie_list);
  602. spin_unlock_irqrestore(&dwarf_cie_lock, flags);
  603. return 0;
  604. }
  605. static int dwarf_parse_fde(void *entry, u32 entry_type,
  606. void *start, unsigned long len)
  607. {
  608. struct dwarf_fde *fde;
  609. struct dwarf_cie *cie;
  610. unsigned long flags;
  611. int count;
  612. void *p = start;
  613. fde = kzalloc(sizeof(*fde), GFP_KERNEL);
  614. if (!fde)
  615. return -ENOMEM;
  616. fde->length = len;
  617. /*
  618. * In a .eh_frame section the CIE pointer is the
  619. * delta between the address within the FDE
  620. */
  621. fde->cie_pointer = (unsigned long)(p - entry_type - 4);
  622. cie = dwarf_lookup_cie(fde->cie_pointer);
  623. fde->cie = cie;
  624. if (cie->encoding)
  625. count = dwarf_read_encoded_value(p, &fde->initial_location,
  626. cie->encoding);
  627. else
  628. count = dwarf_read_addr(p, &fde->initial_location);
  629. p += count;
  630. if (cie->encoding)
  631. count = dwarf_read_encoded_value(p, &fde->address_range,
  632. cie->encoding & 0x0f);
  633. else
  634. count = dwarf_read_addr(p, &fde->address_range);
  635. p += count;
  636. if (fde->cie->flags & DWARF_CIE_Z_AUGMENTATION) {
  637. unsigned int length;
  638. count = dwarf_read_uleb128(p, &length);
  639. p += count + length;
  640. }
  641. /* Call frame instructions. */
  642. fde->instructions = p;
  643. fde->end = start + len;
  644. /* Add to list. */
  645. spin_lock_irqsave(&dwarf_fde_lock, flags);
  646. list_add_tail(&fde->link, &dwarf_fde_list);
  647. spin_unlock_irqrestore(&dwarf_fde_lock, flags);
  648. return 0;
  649. }
  650. static void dwarf_unwinder_dump(struct task_struct *task, struct pt_regs *regs,
  651. unsigned long *sp,
  652. const struct stacktrace_ops *ops, void *data)
  653. {
  654. struct dwarf_frame *frame;
  655. frame = dwarf_unwind_stack(0, NULL);
  656. while (frame && frame->return_addr) {
  657. ops->address(data, frame->return_addr, 1);
  658. frame = frame->next;
  659. }
  660. }
  661. static struct unwinder dwarf_unwinder = {
  662. .name = "dwarf-unwinder",
  663. .dump = dwarf_unwinder_dump,
  664. .rating = 150,
  665. };
  666. static void dwarf_unwinder_cleanup(void)
  667. {
  668. struct dwarf_cie *cie, *m;
  669. struct dwarf_fde *fde, *n;
  670. unsigned long flags;
  671. /*
  672. * Deallocate all the memory allocated for the DWARF unwinder.
  673. * Traverse all the FDE/CIE lists and remove and free all the
  674. * memory associated with those data structures.
  675. */
  676. spin_lock_irqsave(&dwarf_cie_lock, flags);
  677. list_for_each_entry_safe(cie, m, &dwarf_cie_list, link)
  678. kfree(cie);
  679. spin_unlock_irqrestore(&dwarf_cie_lock, flags);
  680. spin_lock_irqsave(&dwarf_fde_lock, flags);
  681. list_for_each_entry_safe(fde, n, &dwarf_fde_list, link)
  682. kfree(fde);
  683. spin_unlock_irqrestore(&dwarf_fde_lock, flags);
  684. }
  685. /**
  686. * dwarf_unwinder_init - initialise the dwarf unwinder
  687. *
  688. * Build the data structures describing the .dwarf_frame section to
  689. * make it easier to lookup CIE and FDE entries. Because the
  690. * .eh_frame section is packed as tightly as possible it is not
  691. * easy to lookup the FDE for a given PC, so we build a list of FDE
  692. * and CIE entries that make it easier.
  693. */
  694. void dwarf_unwinder_init(void)
  695. {
  696. u32 entry_type;
  697. void *p, *entry;
  698. int count, err;
  699. unsigned long len;
  700. unsigned int c_entries, f_entries;
  701. unsigned char *end;
  702. INIT_LIST_HEAD(&dwarf_cie_list);
  703. INIT_LIST_HEAD(&dwarf_fde_list);
  704. c_entries = 0;
  705. f_entries = 0;
  706. entry = &__start_eh_frame;
  707. while ((char *)entry < __stop_eh_frame) {
  708. p = entry;
  709. count = dwarf_entry_len(p, &len);
  710. if (count == 0) {
  711. /*
  712. * We read a bogus length field value. There is
  713. * nothing we can do here apart from disabling
  714. * the DWARF unwinder. We can't even skip this
  715. * entry and move to the next one because 'len'
  716. * tells us where our next entry is.
  717. */
  718. goto out;
  719. } else
  720. p += count;
  721. /* initial length does not include itself */
  722. end = p + len;
  723. entry_type = get_unaligned((u32 *)p);
  724. p += 4;
  725. if (entry_type == DW_EH_FRAME_CIE) {
  726. err = dwarf_parse_cie(entry, p, len, end);
  727. if (err < 0)
  728. goto out;
  729. else
  730. c_entries++;
  731. } else {
  732. err = dwarf_parse_fde(entry, entry_type, p, len);
  733. if (err < 0)
  734. goto out;
  735. else
  736. f_entries++;
  737. }
  738. entry = (char *)entry + len + 4;
  739. }
  740. printk(KERN_INFO "DWARF unwinder initialised: read %u CIEs, %u FDEs\n",
  741. c_entries, f_entries);
  742. err = unwinder_register(&dwarf_unwinder);
  743. if (err)
  744. goto out;
  745. return;
  746. out:
  747. printk(KERN_ERR "Failed to initialise DWARF unwinder: %d\n", err);
  748. dwarf_unwinder_cleanup();
  749. }