dwarf.c 29 KB

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