dwarf.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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. extern void ret_from_irq(void);
  477. /**
  478. * dwarf_unwind_stack - unwind the stack
  479. *
  480. * @pc: address of the function to unwind
  481. * @prev: struct dwarf_frame of the previous stackframe on the callstack
  482. *
  483. * Return a struct dwarf_frame representing the most recent frame
  484. * on the callstack. Each of the lower (older) stack frames are
  485. * linked via the "prev" member.
  486. */
  487. struct dwarf_frame * dwarf_unwind_stack(unsigned long pc,
  488. struct dwarf_frame *prev)
  489. {
  490. struct dwarf_frame *frame;
  491. struct dwarf_cie *cie;
  492. struct dwarf_fde *fde;
  493. struct dwarf_reg *reg;
  494. unsigned long addr;
  495. /*
  496. * If we're starting at the top of the stack we need get the
  497. * contents of a physical register to get the CFA in order to
  498. * begin the virtual unwinding of the stack.
  499. *
  500. * NOTE: the return address is guaranteed to be setup by the
  501. * time this function makes its first function call.
  502. */
  503. if (!pc || !prev)
  504. pc = (unsigned long)current_text_addr();
  505. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  506. /*
  507. * If our stack has been patched by the function graph tracer
  508. * then we might see the address of return_to_handler() where we
  509. * expected to find the real return address.
  510. */
  511. if (pc == (unsigned long)&return_to_handler) {
  512. int index = current->curr_ret_stack;
  513. /*
  514. * We currently have no way of tracking how many
  515. * return_to_handler()'s we've seen. If there is more
  516. * than one patched return address on our stack,
  517. * complain loudly.
  518. */
  519. WARN_ON(index > 0);
  520. pc = current->ret_stack[index].ret;
  521. }
  522. #endif
  523. frame = mempool_alloc(dwarf_frame_pool, GFP_ATOMIC);
  524. if (!frame) {
  525. printk(KERN_ERR "Unable to allocate a dwarf frame\n");
  526. UNWINDER_BUG();
  527. }
  528. INIT_LIST_HEAD(&frame->reg_list);
  529. frame->flags = 0;
  530. frame->prev = prev;
  531. frame->return_addr = 0;
  532. fde = dwarf_lookup_fde(pc);
  533. if (!fde) {
  534. /*
  535. * This is our normal exit path. There are two reasons
  536. * why we might exit here,
  537. *
  538. * a) pc has no asscociated DWARF frame info and so
  539. * we don't know how to unwind this frame. This is
  540. * usually the case when we're trying to unwind a
  541. * frame that was called from some assembly code
  542. * that has no DWARF info, e.g. syscalls.
  543. *
  544. * b) the DEBUG info for pc is bogus. There's
  545. * really no way to distinguish this case from the
  546. * case above, which sucks because we could print a
  547. * warning here.
  548. */
  549. goto bail;
  550. }
  551. cie = dwarf_lookup_cie(fde->cie_pointer);
  552. frame->pc = fde->initial_location;
  553. /* CIE initial instructions */
  554. dwarf_cfa_execute_insns(cie->initial_instructions,
  555. cie->instructions_end, cie, fde,
  556. frame, pc);
  557. /* FDE instructions */
  558. dwarf_cfa_execute_insns(fde->instructions, fde->end, cie,
  559. fde, frame, pc);
  560. /* Calculate the CFA */
  561. switch (frame->flags) {
  562. case DWARF_FRAME_CFA_REG_OFFSET:
  563. if (prev) {
  564. reg = dwarf_frame_reg(prev, frame->cfa_register);
  565. UNWINDER_BUG_ON(!reg);
  566. UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET);
  567. addr = prev->cfa + reg->addr;
  568. frame->cfa = __raw_readl(addr);
  569. } else {
  570. /*
  571. * Again, we're starting from the top of the
  572. * stack. We need to physically read
  573. * the contents of a register in order to get
  574. * the Canonical Frame Address for this
  575. * function.
  576. */
  577. frame->cfa = dwarf_read_arch_reg(frame->cfa_register);
  578. }
  579. frame->cfa += frame->cfa_offset;
  580. break;
  581. default:
  582. UNWINDER_BUG();
  583. }
  584. reg = dwarf_frame_reg(frame, DWARF_ARCH_RA_REG);
  585. /*
  586. * If we haven't seen the return address register or the return
  587. * address column is undefined then we must assume that this is
  588. * the end of the callstack.
  589. */
  590. if (!reg || reg->flags == DWARF_UNDEFINED)
  591. goto bail;
  592. UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET);
  593. addr = frame->cfa + reg->addr;
  594. frame->return_addr = __raw_readl(addr);
  595. /*
  596. * Ah, the joys of unwinding through interrupts.
  597. *
  598. * Interrupts are tricky - the DWARF info needs to be _really_
  599. * accurate and unfortunately I'm seeing a lot of bogus DWARF
  600. * info. For example, I've seen interrupts occur in epilogues
  601. * just after the frame pointer (r14) had been restored. The
  602. * problem was that the DWARF info claimed that the CFA could be
  603. * reached by using the value of the frame pointer before it was
  604. * restored.
  605. *
  606. * So until the compiler can be trusted to produce reliable
  607. * DWARF info when it really matters, let's stop unwinding once
  608. * we've calculated the function that was interrupted.
  609. */
  610. if (prev && prev->pc == (unsigned long)ret_from_irq)
  611. frame->return_addr = 0;
  612. return frame;
  613. bail:
  614. dwarf_free_frame(frame);
  615. return NULL;
  616. }
  617. static int dwarf_parse_cie(void *entry, void *p, unsigned long len,
  618. unsigned char *end, struct module *mod)
  619. {
  620. struct dwarf_cie *cie;
  621. unsigned long flags;
  622. int count;
  623. cie = kzalloc(sizeof(*cie), GFP_KERNEL);
  624. if (!cie)
  625. return -ENOMEM;
  626. cie->length = len;
  627. /*
  628. * Record the offset into the .eh_frame section
  629. * for this CIE. It allows this CIE to be
  630. * quickly and easily looked up from the
  631. * corresponding FDE.
  632. */
  633. cie->cie_pointer = (unsigned long)entry;
  634. cie->version = *(char *)p++;
  635. UNWINDER_BUG_ON(cie->version != 1);
  636. cie->augmentation = p;
  637. p += strlen(cie->augmentation) + 1;
  638. count = dwarf_read_uleb128(p, &cie->code_alignment_factor);
  639. p += count;
  640. count = dwarf_read_leb128(p, &cie->data_alignment_factor);
  641. p += count;
  642. /*
  643. * Which column in the rule table contains the
  644. * return address?
  645. */
  646. if (cie->version == 1) {
  647. cie->return_address_reg = __raw_readb(p);
  648. p++;
  649. } else {
  650. count = dwarf_read_uleb128(p, &cie->return_address_reg);
  651. p += count;
  652. }
  653. if (cie->augmentation[0] == 'z') {
  654. unsigned int length, count;
  655. cie->flags |= DWARF_CIE_Z_AUGMENTATION;
  656. count = dwarf_read_uleb128(p, &length);
  657. p += count;
  658. UNWINDER_BUG_ON((unsigned char *)p > end);
  659. cie->initial_instructions = p + length;
  660. cie->augmentation++;
  661. }
  662. while (*cie->augmentation) {
  663. /*
  664. * "L" indicates a byte showing how the
  665. * LSDA pointer is encoded. Skip it.
  666. */
  667. if (*cie->augmentation == 'L') {
  668. p++;
  669. cie->augmentation++;
  670. } else if (*cie->augmentation == 'R') {
  671. /*
  672. * "R" indicates a byte showing
  673. * how FDE addresses are
  674. * encoded.
  675. */
  676. cie->encoding = *(char *)p++;
  677. cie->augmentation++;
  678. } else if (*cie->augmentation == 'P') {
  679. /*
  680. * "R" indicates a personality
  681. * routine in the CIE
  682. * augmentation.
  683. */
  684. UNWINDER_BUG();
  685. } else if (*cie->augmentation == 'S') {
  686. UNWINDER_BUG();
  687. } else {
  688. /*
  689. * Unknown augmentation. Assume
  690. * 'z' augmentation.
  691. */
  692. p = cie->initial_instructions;
  693. UNWINDER_BUG_ON(!p);
  694. break;
  695. }
  696. }
  697. cie->initial_instructions = p;
  698. cie->instructions_end = end;
  699. cie->mod = mod;
  700. /* Add to list */
  701. spin_lock_irqsave(&dwarf_cie_lock, flags);
  702. list_add_tail(&cie->link, &dwarf_cie_list);
  703. spin_unlock_irqrestore(&dwarf_cie_lock, flags);
  704. return 0;
  705. }
  706. static int dwarf_parse_fde(void *entry, u32 entry_type,
  707. void *start, unsigned long len,
  708. unsigned char *end, struct module *mod)
  709. {
  710. struct dwarf_fde *fde;
  711. struct dwarf_cie *cie;
  712. unsigned long flags;
  713. int count;
  714. void *p = start;
  715. fde = kzalloc(sizeof(*fde), GFP_KERNEL);
  716. if (!fde)
  717. return -ENOMEM;
  718. fde->length = len;
  719. /*
  720. * In a .eh_frame section the CIE pointer is the
  721. * delta between the address within the FDE
  722. */
  723. fde->cie_pointer = (unsigned long)(p - entry_type - 4);
  724. cie = dwarf_lookup_cie(fde->cie_pointer);
  725. fde->cie = cie;
  726. if (cie->encoding)
  727. count = dwarf_read_encoded_value(p, &fde->initial_location,
  728. cie->encoding);
  729. else
  730. count = dwarf_read_addr(p, &fde->initial_location);
  731. p += count;
  732. if (cie->encoding)
  733. count = dwarf_read_encoded_value(p, &fde->address_range,
  734. cie->encoding & 0x0f);
  735. else
  736. count = dwarf_read_addr(p, &fde->address_range);
  737. p += count;
  738. if (fde->cie->flags & DWARF_CIE_Z_AUGMENTATION) {
  739. unsigned int length;
  740. count = dwarf_read_uleb128(p, &length);
  741. p += count + length;
  742. }
  743. /* Call frame instructions. */
  744. fde->instructions = p;
  745. fde->end = end;
  746. fde->mod = mod;
  747. /* Add to list. */
  748. spin_lock_irqsave(&dwarf_fde_lock, flags);
  749. list_add_tail(&fde->link, &dwarf_fde_list);
  750. spin_unlock_irqrestore(&dwarf_fde_lock, flags);
  751. return 0;
  752. }
  753. static void dwarf_unwinder_dump(struct task_struct *task,
  754. struct pt_regs *regs,
  755. unsigned long *sp,
  756. const struct stacktrace_ops *ops,
  757. void *data)
  758. {
  759. struct dwarf_frame *frame, *_frame;
  760. unsigned long return_addr;
  761. _frame = NULL;
  762. return_addr = 0;
  763. while (1) {
  764. frame = dwarf_unwind_stack(return_addr, _frame);
  765. if (_frame)
  766. dwarf_free_frame(_frame);
  767. _frame = frame;
  768. if (!frame || !frame->return_addr)
  769. break;
  770. return_addr = frame->return_addr;
  771. ops->address(data, return_addr, 1);
  772. }
  773. if (frame)
  774. dwarf_free_frame(frame);
  775. }
  776. static struct unwinder dwarf_unwinder = {
  777. .name = "dwarf-unwinder",
  778. .dump = dwarf_unwinder_dump,
  779. .rating = 150,
  780. };
  781. static void dwarf_unwinder_cleanup(void)
  782. {
  783. struct dwarf_cie *cie, *cie_tmp;
  784. struct dwarf_fde *fde, *fde_tmp;
  785. /*
  786. * Deallocate all the memory allocated for the DWARF unwinder.
  787. * Traverse all the FDE/CIE lists and remove and free all the
  788. * memory associated with those data structures.
  789. */
  790. list_for_each_entry_safe(cie, cie_tmp, &dwarf_cie_list, link)
  791. kfree(cie);
  792. list_for_each_entry_safe(fde, fde_tmp, &dwarf_fde_list, link)
  793. kfree(fde);
  794. kmem_cache_destroy(dwarf_reg_cachep);
  795. kmem_cache_destroy(dwarf_frame_cachep);
  796. }
  797. /**
  798. * dwarf_parse_section - parse DWARF section
  799. * @eh_frame_start: start address of the .eh_frame section
  800. * @eh_frame_end: end address of the .eh_frame section
  801. * @mod: the kernel module containing the .eh_frame section
  802. *
  803. * Parse the information in a .eh_frame section.
  804. */
  805. static int dwarf_parse_section(char *eh_frame_start, char *eh_frame_end,
  806. struct module *mod)
  807. {
  808. u32 entry_type;
  809. void *p, *entry;
  810. int count, err = 0;
  811. unsigned long len = 0;
  812. unsigned int c_entries, f_entries;
  813. unsigned char *end;
  814. c_entries = 0;
  815. f_entries = 0;
  816. entry = eh_frame_start;
  817. while ((char *)entry < eh_frame_end) {
  818. p = entry;
  819. count = dwarf_entry_len(p, &len);
  820. if (count == 0) {
  821. /*
  822. * We read a bogus length field value. There is
  823. * nothing we can do here apart from disabling
  824. * the DWARF unwinder. We can't even skip this
  825. * entry and move to the next one because 'len'
  826. * tells us where our next entry is.
  827. */
  828. err = -EINVAL;
  829. goto out;
  830. } else
  831. p += count;
  832. /* initial length does not include itself */
  833. end = p + len;
  834. entry_type = get_unaligned((u32 *)p);
  835. p += 4;
  836. if (entry_type == DW_EH_FRAME_CIE) {
  837. err = dwarf_parse_cie(entry, p, len, end, mod);
  838. if (err < 0)
  839. goto out;
  840. else
  841. c_entries++;
  842. } else {
  843. err = dwarf_parse_fde(entry, entry_type, p, len,
  844. end, mod);
  845. if (err < 0)
  846. goto out;
  847. else
  848. f_entries++;
  849. }
  850. entry = (char *)entry + len + 4;
  851. }
  852. printk(KERN_INFO "DWARF unwinder initialised: read %u CIEs, %u FDEs\n",
  853. c_entries, f_entries);
  854. return 0;
  855. out:
  856. return err;
  857. }
  858. #ifdef CONFIG_MODULES
  859. int module_dwarf_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
  860. struct module *me)
  861. {
  862. unsigned int i, err;
  863. unsigned long start, end;
  864. char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  865. start = end = 0;
  866. for (i = 1; i < hdr->e_shnum; i++) {
  867. /* Alloc bit cleared means "ignore it." */
  868. if ((sechdrs[i].sh_flags & SHF_ALLOC)
  869. && !strcmp(secstrings+sechdrs[i].sh_name, ".eh_frame")) {
  870. start = sechdrs[i].sh_addr;
  871. end = start + sechdrs[i].sh_size;
  872. break;
  873. }
  874. }
  875. /* Did we find the .eh_frame section? */
  876. if (i != hdr->e_shnum) {
  877. err = dwarf_parse_section((char *)start, (char *)end, me);
  878. if (err) {
  879. printk(KERN_WARNING "%s: failed to parse DWARF info\n",
  880. me->name);
  881. return err;
  882. }
  883. }
  884. return 0;
  885. }
  886. /**
  887. * module_dwarf_cleanup - remove FDE/CIEs associated with @mod
  888. * @mod: the module that is being unloaded
  889. *
  890. * Remove any FDEs and CIEs from the global lists that came from
  891. * @mod's .eh_frame section because @mod is being unloaded.
  892. */
  893. void module_dwarf_cleanup(struct module *mod)
  894. {
  895. struct dwarf_fde *fde;
  896. struct dwarf_cie *cie;
  897. unsigned long flags;
  898. spin_lock_irqsave(&dwarf_cie_lock, flags);
  899. again_cie:
  900. list_for_each_entry(cie, &dwarf_cie_list, link) {
  901. if (cie->mod == mod)
  902. break;
  903. }
  904. if (&cie->link != &dwarf_cie_list) {
  905. list_del(&cie->link);
  906. kfree(cie);
  907. goto again_cie;
  908. }
  909. spin_unlock_irqrestore(&dwarf_cie_lock, flags);
  910. spin_lock_irqsave(&dwarf_fde_lock, flags);
  911. again_fde:
  912. list_for_each_entry(fde, &dwarf_fde_list, link) {
  913. if (fde->mod == mod)
  914. break;
  915. }
  916. if (&fde->link != &dwarf_fde_list) {
  917. list_del(&fde->link);
  918. kfree(fde);
  919. goto again_fde;
  920. }
  921. spin_unlock_irqrestore(&dwarf_fde_lock, flags);
  922. }
  923. #endif /* CONFIG_MODULES */
  924. /**
  925. * dwarf_unwinder_init - initialise the dwarf unwinder
  926. *
  927. * Build the data structures describing the .dwarf_frame section to
  928. * make it easier to lookup CIE and FDE entries. Because the
  929. * .eh_frame section is packed as tightly as possible it is not
  930. * easy to lookup the FDE for a given PC, so we build a list of FDE
  931. * and CIE entries that make it easier.
  932. */
  933. static int __init dwarf_unwinder_init(void)
  934. {
  935. int err;
  936. INIT_LIST_HEAD(&dwarf_cie_list);
  937. INIT_LIST_HEAD(&dwarf_fde_list);
  938. dwarf_frame_cachep = kmem_cache_create("dwarf_frames",
  939. sizeof(struct dwarf_frame), 0,
  940. SLAB_PANIC | SLAB_HWCACHE_ALIGN | SLAB_NOTRACK, NULL);
  941. dwarf_reg_cachep = kmem_cache_create("dwarf_regs",
  942. sizeof(struct dwarf_reg), 0,
  943. SLAB_PANIC | SLAB_HWCACHE_ALIGN | SLAB_NOTRACK, NULL);
  944. dwarf_frame_pool = mempool_create(DWARF_FRAME_MIN_REQ,
  945. mempool_alloc_slab,
  946. mempool_free_slab,
  947. dwarf_frame_cachep);
  948. dwarf_reg_pool = mempool_create(DWARF_REG_MIN_REQ,
  949. mempool_alloc_slab,
  950. mempool_free_slab,
  951. dwarf_reg_cachep);
  952. err = dwarf_parse_section(__start_eh_frame, __stop_eh_frame, NULL);
  953. if (err)
  954. goto out;
  955. err = unwinder_register(&dwarf_unwinder);
  956. if (err)
  957. goto out;
  958. return 0;
  959. out:
  960. printk(KERN_ERR "Failed to initialise DWARF unwinder: %d\n", err);
  961. dwarf_unwinder_cleanup();
  962. return -EINVAL;
  963. }
  964. early_initcall(dwarf_unwinder_init);