dwarf.c 29 KB

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