dwarf.c 23 KB

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