dwarf.c 26 KB

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