dwarf.c 29 KB

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