dwarf.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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/ftrace.h>
  23. #include <asm/dwarf.h>
  24. #include <asm/unwinder.h>
  25. #include <asm/sections.h>
  26. #include <asm/unaligned.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. regp = dwarf_frame_alloc_reg(frame, reg);
  396. regp->flags |= DWARF_UNDEFINED;
  397. break;
  398. case DW_CFA_def_cfa:
  399. count = dwarf_read_uleb128(current_insn,
  400. &frame->cfa_register);
  401. current_insn += count;
  402. count = dwarf_read_uleb128(current_insn,
  403. &frame->cfa_offset);
  404. current_insn += count;
  405. frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
  406. break;
  407. case DW_CFA_def_cfa_register:
  408. count = dwarf_read_uleb128(current_insn,
  409. &frame->cfa_register);
  410. current_insn += count;
  411. frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
  412. break;
  413. case DW_CFA_def_cfa_offset:
  414. count = dwarf_read_uleb128(current_insn, &offset);
  415. current_insn += count;
  416. frame->cfa_offset = offset;
  417. break;
  418. case DW_CFA_def_cfa_expression:
  419. count = dwarf_read_uleb128(current_insn, &expr_len);
  420. current_insn += count;
  421. frame->cfa_expr = current_insn;
  422. frame->cfa_expr_len = expr_len;
  423. current_insn += expr_len;
  424. frame->flags |= DWARF_FRAME_CFA_REG_EXP;
  425. break;
  426. case DW_CFA_offset_extended_sf:
  427. count = dwarf_read_uleb128(current_insn, &reg);
  428. current_insn += count;
  429. count = dwarf_read_leb128(current_insn, &offset);
  430. current_insn += count;
  431. offset *= cie->data_alignment_factor;
  432. regp = dwarf_frame_alloc_reg(frame, reg);
  433. regp->flags |= DWARF_REG_OFFSET;
  434. regp->addr = offset;
  435. break;
  436. case DW_CFA_val_offset:
  437. count = dwarf_read_uleb128(current_insn, &reg);
  438. current_insn += count;
  439. count = dwarf_read_leb128(current_insn, &offset);
  440. offset *= cie->data_alignment_factor;
  441. regp = dwarf_frame_alloc_reg(frame, reg);
  442. regp->flags |= DWARF_VAL_OFFSET;
  443. regp->addr = offset;
  444. break;
  445. case DW_CFA_GNU_args_size:
  446. count = dwarf_read_uleb128(current_insn, &offset);
  447. current_insn += count;
  448. break;
  449. case DW_CFA_GNU_negative_offset_extended:
  450. count = dwarf_read_uleb128(current_insn, &reg);
  451. current_insn += count;
  452. count = dwarf_read_uleb128(current_insn, &offset);
  453. offset *= cie->data_alignment_factor;
  454. regp = dwarf_frame_alloc_reg(frame, reg);
  455. regp->flags |= DWARF_REG_OFFSET;
  456. regp->addr = -offset;
  457. break;
  458. default:
  459. pr_debug("unhandled DWARF instruction 0x%x\n", insn);
  460. UNWINDER_BUG();
  461. break;
  462. }
  463. }
  464. return 0;
  465. }
  466. /**
  467. * dwarf_unwind_stack - recursively unwind the stack
  468. * @pc: address of the function to unwind
  469. * @prev: struct dwarf_frame of the previous stackframe on the callstack
  470. *
  471. * Return a struct dwarf_frame representing the most recent frame
  472. * on the callstack. Each of the lower (older) stack frames are
  473. * linked via the "prev" member.
  474. */
  475. struct dwarf_frame * dwarf_unwind_stack(unsigned long pc,
  476. struct dwarf_frame *prev)
  477. {
  478. struct dwarf_frame *frame;
  479. struct dwarf_cie *cie;
  480. struct dwarf_fde *fde;
  481. struct dwarf_reg *reg;
  482. unsigned long addr;
  483. /*
  484. * If this is the first invocation of this recursive function we
  485. * need get the contents of a physical register to get the CFA
  486. * in order to begin the virtual unwinding of the stack.
  487. *
  488. * NOTE: the return address is guaranteed to be setup by the
  489. * time this function makes its first function call.
  490. */
  491. if (!pc || !prev)
  492. pc = (unsigned long)current_text_addr();
  493. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  494. /*
  495. * If our stack has been patched by the function graph tracer
  496. * then we might see the address of return_to_handler() where we
  497. * expected to find the real return address.
  498. */
  499. if (pc == (unsigned long)&return_to_handler) {
  500. int index = current->curr_ret_stack;
  501. /*
  502. * We currently have no way of tracking how many
  503. * return_to_handler()'s we've seen. If there is more
  504. * than one patched return address on our stack,
  505. * complain loudly.
  506. */
  507. WARN_ON(index > 0);
  508. pc = current->ret_stack[index].ret;
  509. }
  510. #endif
  511. frame = mempool_alloc(dwarf_frame_pool, GFP_ATOMIC);
  512. if (!frame) {
  513. printk(KERN_ERR "Unable to allocate a dwarf frame\n");
  514. UNWINDER_BUG();
  515. }
  516. INIT_LIST_HEAD(&frame->reg_list);
  517. frame->flags = 0;
  518. frame->prev = prev;
  519. frame->return_addr = 0;
  520. fde = dwarf_lookup_fde(pc);
  521. if (!fde) {
  522. /*
  523. * This is our normal exit path - the one that stops the
  524. * recursion. There's two reasons why we might exit
  525. * here,
  526. *
  527. * a) pc has no asscociated DWARF frame info and so
  528. * we don't know how to unwind this frame. This is
  529. * usually the case when we're trying to unwind a
  530. * frame that was called from some assembly code
  531. * that has no DWARF info, e.g. syscalls.
  532. *
  533. * b) the DEBUG info for pc is bogus. There's
  534. * really no way to distinguish this case from the
  535. * case above, which sucks because we could print a
  536. * warning here.
  537. */
  538. goto bail;
  539. }
  540. cie = dwarf_lookup_cie(fde->cie_pointer);
  541. frame->pc = fde->initial_location;
  542. /* CIE initial instructions */
  543. dwarf_cfa_execute_insns(cie->initial_instructions,
  544. cie->instructions_end, cie, fde,
  545. frame, pc);
  546. /* FDE instructions */
  547. dwarf_cfa_execute_insns(fde->instructions, fde->end, cie,
  548. fde, frame, pc);
  549. /* Calculate the CFA */
  550. switch (frame->flags) {
  551. case DWARF_FRAME_CFA_REG_OFFSET:
  552. if (prev) {
  553. reg = dwarf_frame_reg(prev, frame->cfa_register);
  554. UNWINDER_BUG_ON(!reg);
  555. UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET);
  556. addr = prev->cfa + reg->addr;
  557. frame->cfa = __raw_readl(addr);
  558. } else {
  559. /*
  560. * Again, this is the first invocation of this
  561. * recurisve function. We need to physically
  562. * read the contents of a register in order to
  563. * get the Canonical Frame Address for this
  564. * function.
  565. */
  566. frame->cfa = dwarf_read_arch_reg(frame->cfa_register);
  567. }
  568. frame->cfa += frame->cfa_offset;
  569. break;
  570. default:
  571. UNWINDER_BUG();
  572. }
  573. reg = dwarf_frame_reg(frame, DWARF_ARCH_RA_REG);
  574. /*
  575. * If we haven't seen the return address register or the return
  576. * address column is undefined then we must assume that this is
  577. * the end of the callstack.
  578. */
  579. if (!reg || reg->flags == DWARF_UNDEFINED)
  580. goto bail;
  581. UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET);
  582. addr = frame->cfa + reg->addr;
  583. frame->return_addr = __raw_readl(addr);
  584. return frame;
  585. bail:
  586. dwarf_frame_free_regs(frame);
  587. mempool_free(frame, dwarf_frame_pool);
  588. return NULL;
  589. }
  590. static int dwarf_parse_cie(void *entry, void *p, unsigned long len,
  591. unsigned char *end)
  592. {
  593. struct dwarf_cie *cie;
  594. unsigned long flags;
  595. int count;
  596. cie = kzalloc(sizeof(*cie), GFP_KERNEL);
  597. if (!cie)
  598. return -ENOMEM;
  599. cie->length = len;
  600. /*
  601. * Record the offset into the .eh_frame section
  602. * for this CIE. It allows this CIE to be
  603. * quickly and easily looked up from the
  604. * corresponding FDE.
  605. */
  606. cie->cie_pointer = (unsigned long)entry;
  607. cie->version = *(char *)p++;
  608. UNWINDER_BUG_ON(cie->version != 1);
  609. cie->augmentation = p;
  610. p += strlen(cie->augmentation) + 1;
  611. count = dwarf_read_uleb128(p, &cie->code_alignment_factor);
  612. p += count;
  613. count = dwarf_read_leb128(p, &cie->data_alignment_factor);
  614. p += count;
  615. /*
  616. * Which column in the rule table contains the
  617. * return address?
  618. */
  619. if (cie->version == 1) {
  620. cie->return_address_reg = __raw_readb(p);
  621. p++;
  622. } else {
  623. count = dwarf_read_uleb128(p, &cie->return_address_reg);
  624. p += count;
  625. }
  626. if (cie->augmentation[0] == 'z') {
  627. unsigned int length, count;
  628. cie->flags |= DWARF_CIE_Z_AUGMENTATION;
  629. count = dwarf_read_uleb128(p, &length);
  630. p += count;
  631. UNWINDER_BUG_ON((unsigned char *)p > end);
  632. cie->initial_instructions = p + length;
  633. cie->augmentation++;
  634. }
  635. while (*cie->augmentation) {
  636. /*
  637. * "L" indicates a byte showing how the
  638. * LSDA pointer is encoded. Skip it.
  639. */
  640. if (*cie->augmentation == 'L') {
  641. p++;
  642. cie->augmentation++;
  643. } else if (*cie->augmentation == 'R') {
  644. /*
  645. * "R" indicates a byte showing
  646. * how FDE addresses are
  647. * encoded.
  648. */
  649. cie->encoding = *(char *)p++;
  650. cie->augmentation++;
  651. } else if (*cie->augmentation == 'P') {
  652. /*
  653. * "R" indicates a personality
  654. * routine in the CIE
  655. * augmentation.
  656. */
  657. UNWINDER_BUG();
  658. } else if (*cie->augmentation == 'S') {
  659. UNWINDER_BUG();
  660. } else {
  661. /*
  662. * Unknown augmentation. Assume
  663. * 'z' augmentation.
  664. */
  665. p = cie->initial_instructions;
  666. UNWINDER_BUG_ON(!p);
  667. break;
  668. }
  669. }
  670. cie->initial_instructions = p;
  671. cie->instructions_end = end;
  672. /* Add to list */
  673. spin_lock_irqsave(&dwarf_cie_lock, flags);
  674. list_add_tail(&cie->link, &dwarf_cie_list);
  675. spin_unlock_irqrestore(&dwarf_cie_lock, flags);
  676. return 0;
  677. }
  678. static int dwarf_parse_fde(void *entry, u32 entry_type,
  679. void *start, unsigned long len,
  680. unsigned char *end)
  681. {
  682. struct dwarf_fde *fde;
  683. struct dwarf_cie *cie;
  684. unsigned long flags;
  685. int count;
  686. void *p = start;
  687. fde = kzalloc(sizeof(*fde), GFP_KERNEL);
  688. if (!fde)
  689. return -ENOMEM;
  690. fde->length = len;
  691. /*
  692. * In a .eh_frame section the CIE pointer is the
  693. * delta between the address within the FDE
  694. */
  695. fde->cie_pointer = (unsigned long)(p - entry_type - 4);
  696. cie = dwarf_lookup_cie(fde->cie_pointer);
  697. fde->cie = cie;
  698. if (cie->encoding)
  699. count = dwarf_read_encoded_value(p, &fde->initial_location,
  700. cie->encoding);
  701. else
  702. count = dwarf_read_addr(p, &fde->initial_location);
  703. p += count;
  704. if (cie->encoding)
  705. count = dwarf_read_encoded_value(p, &fde->address_range,
  706. cie->encoding & 0x0f);
  707. else
  708. count = dwarf_read_addr(p, &fde->address_range);
  709. p += count;
  710. if (fde->cie->flags & DWARF_CIE_Z_AUGMENTATION) {
  711. unsigned int length;
  712. count = dwarf_read_uleb128(p, &length);
  713. p += count + length;
  714. }
  715. /* Call frame instructions. */
  716. fde->instructions = p;
  717. fde->end = end;
  718. /* Add to list. */
  719. spin_lock_irqsave(&dwarf_fde_lock, flags);
  720. list_add_tail(&fde->link, &dwarf_fde_list);
  721. spin_unlock_irqrestore(&dwarf_fde_lock, flags);
  722. return 0;
  723. }
  724. static void dwarf_unwinder_dump(struct task_struct *task,
  725. struct pt_regs *regs,
  726. unsigned long *sp,
  727. const struct stacktrace_ops *ops,
  728. void *data)
  729. {
  730. struct dwarf_frame *frame, *_frame;
  731. unsigned long return_addr;
  732. _frame = NULL;
  733. return_addr = 0;
  734. while (1) {
  735. frame = dwarf_unwind_stack(return_addr, _frame);
  736. if (_frame) {
  737. dwarf_frame_free_regs(_frame);
  738. mempool_free(_frame, dwarf_frame_pool);
  739. }
  740. _frame = frame;
  741. if (!frame || !frame->return_addr)
  742. break;
  743. return_addr = frame->return_addr;
  744. ops->address(data, return_addr, 1);
  745. }
  746. }
  747. static struct unwinder dwarf_unwinder = {
  748. .name = "dwarf-unwinder",
  749. .dump = dwarf_unwinder_dump,
  750. .rating = 150,
  751. };
  752. static void dwarf_unwinder_cleanup(void)
  753. {
  754. struct dwarf_cie *cie;
  755. struct dwarf_fde *fde;
  756. /*
  757. * Deallocate all the memory allocated for the DWARF unwinder.
  758. * Traverse all the FDE/CIE lists and remove and free all the
  759. * memory associated with those data structures.
  760. */
  761. list_for_each_entry(cie, &dwarf_cie_list, link)
  762. kfree(cie);
  763. list_for_each_entry(fde, &dwarf_fde_list, link)
  764. kfree(fde);
  765. kmem_cache_destroy(dwarf_reg_cachep);
  766. kmem_cache_destroy(dwarf_frame_cachep);
  767. }
  768. /**
  769. * dwarf_unwinder_init - initialise the dwarf unwinder
  770. *
  771. * Build the data structures describing the .dwarf_frame section to
  772. * make it easier to lookup CIE and FDE entries. Because the
  773. * .eh_frame section is packed as tightly as possible it is not
  774. * easy to lookup the FDE for a given PC, so we build a list of FDE
  775. * and CIE entries that make it easier.
  776. */
  777. static int __init dwarf_unwinder_init(void)
  778. {
  779. u32 entry_type;
  780. void *p, *entry;
  781. int count, err = 0;
  782. unsigned long len;
  783. unsigned int c_entries, f_entries;
  784. unsigned char *end;
  785. INIT_LIST_HEAD(&dwarf_cie_list);
  786. INIT_LIST_HEAD(&dwarf_fde_list);
  787. c_entries = 0;
  788. f_entries = 0;
  789. entry = &__start_eh_frame;
  790. dwarf_frame_cachep = kmem_cache_create("dwarf_frames",
  791. sizeof(struct dwarf_frame), 0,
  792. SLAB_PANIC | SLAB_HWCACHE_ALIGN | SLAB_NOTRACK, NULL);
  793. dwarf_reg_cachep = kmem_cache_create("dwarf_regs",
  794. sizeof(struct dwarf_reg), 0,
  795. SLAB_PANIC | SLAB_HWCACHE_ALIGN | SLAB_NOTRACK, NULL);
  796. dwarf_frame_pool = mempool_create(DWARF_FRAME_MIN_REQ,
  797. mempool_alloc_slab,
  798. mempool_free_slab,
  799. dwarf_frame_cachep);
  800. dwarf_reg_pool = mempool_create(DWARF_REG_MIN_REQ,
  801. mempool_alloc_slab,
  802. mempool_free_slab,
  803. dwarf_reg_cachep);
  804. while ((char *)entry < __stop_eh_frame) {
  805. p = entry;
  806. count = dwarf_entry_len(p, &len);
  807. if (count == 0) {
  808. /*
  809. * We read a bogus length field value. There is
  810. * nothing we can do here apart from disabling
  811. * the DWARF unwinder. We can't even skip this
  812. * entry and move to the next one because 'len'
  813. * tells us where our next entry is.
  814. */
  815. goto out;
  816. } else
  817. p += count;
  818. /* initial length does not include itself */
  819. end = p + len;
  820. entry_type = get_unaligned((u32 *)p);
  821. p += 4;
  822. if (entry_type == DW_EH_FRAME_CIE) {
  823. err = dwarf_parse_cie(entry, p, len, end);
  824. if (err < 0)
  825. goto out;
  826. else
  827. c_entries++;
  828. } else {
  829. err = dwarf_parse_fde(entry, entry_type, p, len, end);
  830. if (err < 0)
  831. goto out;
  832. else
  833. f_entries++;
  834. }
  835. entry = (char *)entry + len + 4;
  836. }
  837. printk(KERN_INFO "DWARF unwinder initialised: read %u CIEs, %u FDEs\n",
  838. c_entries, f_entries);
  839. err = unwinder_register(&dwarf_unwinder);
  840. if (err)
  841. goto out;
  842. return 0;
  843. out:
  844. printk(KERN_ERR "Failed to initialise DWARF unwinder: %d\n", err);
  845. dwarf_unwinder_cleanup();
  846. return -EINVAL;
  847. }
  848. early_initcall(dwarf_unwinder_init);