dwarf.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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. 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. frame = mempool_alloc(dwarf_frame_pool, GFP_ATOMIC);
  494. if (!frame) {
  495. printk(KERN_ERR "Unable to allocate a dwarf frame\n");
  496. UNWINDER_BUG();
  497. }
  498. INIT_LIST_HEAD(&frame->reg_list);
  499. frame->flags = 0;
  500. frame->prev = prev;
  501. frame->return_addr = 0;
  502. fde = dwarf_lookup_fde(pc);
  503. if (!fde) {
  504. /*
  505. * This is our normal exit path - the one that stops the
  506. * recursion. There's two reasons why we might exit
  507. * here,
  508. *
  509. * a) pc has no asscociated DWARF frame info and so
  510. * we don't know how to unwind this frame. This is
  511. * usually the case when we're trying to unwind a
  512. * frame that was called from some assembly code
  513. * that has no DWARF info, e.g. syscalls.
  514. *
  515. * b) the DEBUG info for pc is bogus. There's
  516. * really no way to distinguish this case from the
  517. * case above, which sucks because we could print a
  518. * warning here.
  519. */
  520. goto bail;
  521. }
  522. cie = dwarf_lookup_cie(fde->cie_pointer);
  523. frame->pc = fde->initial_location;
  524. /* CIE initial instructions */
  525. dwarf_cfa_execute_insns(cie->initial_instructions,
  526. cie->instructions_end, cie, fde,
  527. frame, pc);
  528. /* FDE instructions */
  529. dwarf_cfa_execute_insns(fde->instructions, fde->end, cie,
  530. fde, frame, pc);
  531. /* Calculate the CFA */
  532. switch (frame->flags) {
  533. case DWARF_FRAME_CFA_REG_OFFSET:
  534. if (prev) {
  535. reg = dwarf_frame_reg(prev, frame->cfa_register);
  536. UNWINDER_BUG_ON(!reg);
  537. UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET);
  538. addr = prev->cfa + reg->addr;
  539. frame->cfa = __raw_readl(addr);
  540. } else {
  541. /*
  542. * Again, this is the first invocation of this
  543. * recurisve function. We need to physically
  544. * read the contents of a register in order to
  545. * get the Canonical Frame Address for this
  546. * function.
  547. */
  548. frame->cfa = dwarf_read_arch_reg(frame->cfa_register);
  549. }
  550. frame->cfa += frame->cfa_offset;
  551. break;
  552. default:
  553. UNWINDER_BUG();
  554. }
  555. reg = dwarf_frame_reg(frame, DWARF_ARCH_RA_REG);
  556. /*
  557. * If we haven't seen the return address register or the return
  558. * address column is undefined then we must assume that this is
  559. * the end of the callstack.
  560. */
  561. if (!reg || reg->flags == DWARF_UNDEFINED)
  562. goto bail;
  563. UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET);
  564. addr = frame->cfa + reg->addr;
  565. frame->return_addr = __raw_readl(addr);
  566. return frame;
  567. bail:
  568. dwarf_frame_free_regs(frame);
  569. mempool_free(frame, dwarf_frame_pool);
  570. return NULL;
  571. }
  572. static int dwarf_parse_cie(void *entry, void *p, unsigned long len,
  573. unsigned char *end)
  574. {
  575. struct dwarf_cie *cie;
  576. unsigned long flags;
  577. int count;
  578. cie = kzalloc(sizeof(*cie), GFP_KERNEL);
  579. if (!cie)
  580. return -ENOMEM;
  581. cie->length = len;
  582. /*
  583. * Record the offset into the .eh_frame section
  584. * for this CIE. It allows this CIE to be
  585. * quickly and easily looked up from the
  586. * corresponding FDE.
  587. */
  588. cie->cie_pointer = (unsigned long)entry;
  589. cie->version = *(char *)p++;
  590. UNWINDER_BUG_ON(cie->version != 1);
  591. cie->augmentation = p;
  592. p += strlen(cie->augmentation) + 1;
  593. count = dwarf_read_uleb128(p, &cie->code_alignment_factor);
  594. p += count;
  595. count = dwarf_read_leb128(p, &cie->data_alignment_factor);
  596. p += count;
  597. /*
  598. * Which column in the rule table contains the
  599. * return address?
  600. */
  601. if (cie->version == 1) {
  602. cie->return_address_reg = __raw_readb(p);
  603. p++;
  604. } else {
  605. count = dwarf_read_uleb128(p, &cie->return_address_reg);
  606. p += count;
  607. }
  608. if (cie->augmentation[0] == 'z') {
  609. unsigned int length, count;
  610. cie->flags |= DWARF_CIE_Z_AUGMENTATION;
  611. count = dwarf_read_uleb128(p, &length);
  612. p += count;
  613. UNWINDER_BUG_ON((unsigned char *)p > end);
  614. cie->initial_instructions = p + length;
  615. cie->augmentation++;
  616. }
  617. while (*cie->augmentation) {
  618. /*
  619. * "L" indicates a byte showing how the
  620. * LSDA pointer is encoded. Skip it.
  621. */
  622. if (*cie->augmentation == 'L') {
  623. p++;
  624. cie->augmentation++;
  625. } else if (*cie->augmentation == 'R') {
  626. /*
  627. * "R" indicates a byte showing
  628. * how FDE addresses are
  629. * encoded.
  630. */
  631. cie->encoding = *(char *)p++;
  632. cie->augmentation++;
  633. } else if (*cie->augmentation == 'P') {
  634. /*
  635. * "R" indicates a personality
  636. * routine in the CIE
  637. * augmentation.
  638. */
  639. UNWINDER_BUG();
  640. } else if (*cie->augmentation == 'S') {
  641. UNWINDER_BUG();
  642. } else {
  643. /*
  644. * Unknown augmentation. Assume
  645. * 'z' augmentation.
  646. */
  647. p = cie->initial_instructions;
  648. UNWINDER_BUG_ON(!p);
  649. break;
  650. }
  651. }
  652. cie->initial_instructions = p;
  653. cie->instructions_end = end;
  654. /* Add to list */
  655. spin_lock_irqsave(&dwarf_cie_lock, flags);
  656. list_add_tail(&cie->link, &dwarf_cie_list);
  657. spin_unlock_irqrestore(&dwarf_cie_lock, flags);
  658. return 0;
  659. }
  660. static int dwarf_parse_fde(void *entry, u32 entry_type,
  661. void *start, unsigned long len,
  662. unsigned char *end)
  663. {
  664. struct dwarf_fde *fde;
  665. struct dwarf_cie *cie;
  666. unsigned long flags;
  667. int count;
  668. void *p = start;
  669. fde = kzalloc(sizeof(*fde), GFP_KERNEL);
  670. if (!fde)
  671. return -ENOMEM;
  672. fde->length = len;
  673. /*
  674. * In a .eh_frame section the CIE pointer is the
  675. * delta between the address within the FDE
  676. */
  677. fde->cie_pointer = (unsigned long)(p - entry_type - 4);
  678. cie = dwarf_lookup_cie(fde->cie_pointer);
  679. fde->cie = cie;
  680. if (cie->encoding)
  681. count = dwarf_read_encoded_value(p, &fde->initial_location,
  682. cie->encoding);
  683. else
  684. count = dwarf_read_addr(p, &fde->initial_location);
  685. p += count;
  686. if (cie->encoding)
  687. count = dwarf_read_encoded_value(p, &fde->address_range,
  688. cie->encoding & 0x0f);
  689. else
  690. count = dwarf_read_addr(p, &fde->address_range);
  691. p += count;
  692. if (fde->cie->flags & DWARF_CIE_Z_AUGMENTATION) {
  693. unsigned int length;
  694. count = dwarf_read_uleb128(p, &length);
  695. p += count + length;
  696. }
  697. /* Call frame instructions. */
  698. fde->instructions = p;
  699. fde->end = end;
  700. /* Add to list. */
  701. spin_lock_irqsave(&dwarf_fde_lock, flags);
  702. list_add_tail(&fde->link, &dwarf_fde_list);
  703. spin_unlock_irqrestore(&dwarf_fde_lock, flags);
  704. return 0;
  705. }
  706. static void dwarf_unwinder_dump(struct task_struct *task,
  707. struct pt_regs *regs,
  708. unsigned long *sp,
  709. const struct stacktrace_ops *ops,
  710. void *data)
  711. {
  712. struct dwarf_frame *frame, *_frame;
  713. unsigned long return_addr;
  714. _frame = NULL;
  715. return_addr = 0;
  716. while (1) {
  717. frame = dwarf_unwind_stack(return_addr, _frame);
  718. if (_frame) {
  719. dwarf_frame_free_regs(_frame);
  720. mempool_free(_frame, dwarf_frame_pool);
  721. }
  722. _frame = frame;
  723. if (!frame || !frame->return_addr)
  724. break;
  725. return_addr = frame->return_addr;
  726. ops->address(data, return_addr, 1);
  727. }
  728. }
  729. static struct unwinder dwarf_unwinder = {
  730. .name = "dwarf-unwinder",
  731. .dump = dwarf_unwinder_dump,
  732. .rating = 150,
  733. };
  734. static void dwarf_unwinder_cleanup(void)
  735. {
  736. struct dwarf_cie *cie;
  737. struct dwarf_fde *fde;
  738. /*
  739. * Deallocate all the memory allocated for the DWARF unwinder.
  740. * Traverse all the FDE/CIE lists and remove and free all the
  741. * memory associated with those data structures.
  742. */
  743. list_for_each_entry(cie, &dwarf_cie_list, link)
  744. kfree(cie);
  745. list_for_each_entry(fde, &dwarf_fde_list, link)
  746. kfree(fde);
  747. kmem_cache_destroy(dwarf_reg_cachep);
  748. kmem_cache_destroy(dwarf_frame_cachep);
  749. }
  750. /**
  751. * dwarf_unwinder_init - initialise the dwarf unwinder
  752. *
  753. * Build the data structures describing the .dwarf_frame section to
  754. * make it easier to lookup CIE and FDE entries. Because the
  755. * .eh_frame section is packed as tightly as possible it is not
  756. * easy to lookup the FDE for a given PC, so we build a list of FDE
  757. * and CIE entries that make it easier.
  758. */
  759. static int __init dwarf_unwinder_init(void)
  760. {
  761. u32 entry_type;
  762. void *p, *entry;
  763. int count, err;
  764. unsigned long len;
  765. unsigned int c_entries, f_entries;
  766. unsigned char *end;
  767. INIT_LIST_HEAD(&dwarf_cie_list);
  768. INIT_LIST_HEAD(&dwarf_fde_list);
  769. c_entries = 0;
  770. f_entries = 0;
  771. entry = &__start_eh_frame;
  772. dwarf_frame_cachep = kmem_cache_create("dwarf_frames",
  773. sizeof(struct dwarf_frame), 0, SLAB_PANIC, NULL);
  774. dwarf_reg_cachep = kmem_cache_create("dwarf_regs",
  775. sizeof(struct dwarf_reg), 0, SLAB_PANIC, NULL);
  776. dwarf_frame_pool = mempool_create(DWARF_FRAME_MIN_REQ,
  777. mempool_alloc_slab,
  778. mempool_free_slab,
  779. dwarf_frame_cachep);
  780. dwarf_reg_pool = mempool_create(DWARF_REG_MIN_REQ,
  781. mempool_alloc_slab,
  782. mempool_free_slab,
  783. dwarf_reg_cachep);
  784. while ((char *)entry < __stop_eh_frame) {
  785. p = entry;
  786. count = dwarf_entry_len(p, &len);
  787. if (count == 0) {
  788. /*
  789. * We read a bogus length field value. There is
  790. * nothing we can do here apart from disabling
  791. * the DWARF unwinder. We can't even skip this
  792. * entry and move to the next one because 'len'
  793. * tells us where our next entry is.
  794. */
  795. goto out;
  796. } else
  797. p += count;
  798. /* initial length does not include itself */
  799. end = p + len;
  800. entry_type = get_unaligned((u32 *)p);
  801. p += 4;
  802. if (entry_type == DW_EH_FRAME_CIE) {
  803. err = dwarf_parse_cie(entry, p, len, end);
  804. if (err < 0)
  805. goto out;
  806. else
  807. c_entries++;
  808. } else {
  809. err = dwarf_parse_fde(entry, entry_type, p, len, end);
  810. if (err < 0)
  811. goto out;
  812. else
  813. f_entries++;
  814. }
  815. entry = (char *)entry + len + 4;
  816. }
  817. printk(KERN_INFO "DWARF unwinder initialised: read %u CIEs, %u FDEs\n",
  818. c_entries, f_entries);
  819. err = unwinder_register(&dwarf_unwinder);
  820. if (err)
  821. goto out;
  822. return 0;
  823. out:
  824. printk(KERN_ERR "Failed to initialise DWARF unwinder: %d\n", err);
  825. dwarf_unwinder_cleanup();
  826. return -EINVAL;
  827. }
  828. early_initcall(dwarf_unwinder_init);