dwarf.c 29 KB

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