dwarf.c 23 KB

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