dwarf.c 26 KB

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