dwarf.c 29 KB

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