dwarf.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2009 Matt Fleming <[email protected]>
  4. *
  5. * This is an implementation of a DWARF unwinder. Its main purpose is
  6. * for generating stacktrace information. Based on the DWARF 3
  7. * specification from http://www.dwarfstd.org.
  8. *
  9. * TODO:
  10. * - DWARF64 doesn't work.
  11. * - Registers with DWARF_VAL_OFFSET rules aren't handled properly.
  12. */
  13. /* #define DEBUG */
  14. #include <linux/kernel.h>
  15. #include <linux/io.h>
  16. #include <linux/list.h>
  17. #include <linux/mempool.h>
  18. #include <linux/mm.h>
  19. #include <linux/elf.h>
  20. #include <linux/ftrace.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <asm/dwarf.h>
  24. #include <asm/unwinder.h>
  25. #include <asm/sections.h>
  26. #include <asm/unaligned.h>
  27. #include <asm/stacktrace.h>
  28. /* Reserve enough memory for two stack frames */
  29. #define DWARF_FRAME_MIN_REQ 2
  30. /* ... with 4 registers per frame. */
  31. #define DWARF_REG_MIN_REQ (DWARF_FRAME_MIN_REQ * 4)
  32. static struct kmem_cache *dwarf_frame_cachep;
  33. static mempool_t *dwarf_frame_pool;
  34. static struct kmem_cache *dwarf_reg_cachep;
  35. static mempool_t *dwarf_reg_pool;
  36. static struct rb_root cie_root;
  37. static DEFINE_SPINLOCK(dwarf_cie_lock);
  38. static struct rb_root fde_root;
  39. static DEFINE_SPINLOCK(dwarf_fde_lock);
  40. static struct dwarf_cie *cached_cie;
  41. static unsigned int dwarf_unwinder_ready;
  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've been called in to before initialization has
  513. * completed, bail out immediately.
  514. */
  515. if (!dwarf_unwinder_ready)
  516. return NULL;
  517. /*
  518. * If we're starting at the top of the stack we need get the
  519. * contents of a physical register to get the CFA in order to
  520. * begin the virtual unwinding of the stack.
  521. *
  522. * NOTE: the return address is guaranteed to be setup by the
  523. * time this function makes its first function call.
  524. */
  525. if (!pc || !prev)
  526. pc = _THIS_IP_;
  527. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  528. /*
  529. * If our stack has been patched by the function graph tracer
  530. * then we might see the address of return_to_handler() where we
  531. * expected to find the real return address.
  532. */
  533. if (pc == (unsigned long)&return_to_handler) {
  534. struct ftrace_ret_stack *ret_stack;
  535. ret_stack = ftrace_graph_get_ret_stack(current, 0);
  536. if (ret_stack)
  537. pc = ret_stack->ret;
  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(ftrace_graph_get_ret_stack(current, 1));
  545. }
  546. #endif
  547. frame = mempool_alloc(dwarf_frame_pool, GFP_ATOMIC);
  548. if (!frame) {
  549. printk(KERN_ERR "Unable to allocate a dwarf frame\n");
  550. UNWINDER_BUG();
  551. }
  552. INIT_LIST_HEAD(&frame->reg_list);
  553. frame->flags = 0;
  554. frame->prev = prev;
  555. frame->return_addr = 0;
  556. fde = dwarf_lookup_fde(pc);
  557. if (!fde) {
  558. /*
  559. * This is our normal exit path. There are two reasons
  560. * why we might exit here,
  561. *
  562. * a) pc has no asscociated DWARF frame info and so
  563. * we don't know how to unwind this frame. This is
  564. * usually the case when we're trying to unwind a
  565. * frame that was called from some assembly code
  566. * that has no DWARF info, e.g. syscalls.
  567. *
  568. * b) the DEBUG info for pc is bogus. There's
  569. * really no way to distinguish this case from the
  570. * case above, which sucks because we could print a
  571. * warning here.
  572. */
  573. goto bail;
  574. }
  575. cie = dwarf_lookup_cie(fde->cie_pointer);
  576. frame->pc = fde->initial_location;
  577. /* CIE initial instructions */
  578. dwarf_cfa_execute_insns(cie->initial_instructions,
  579. cie->instructions_end, cie, fde,
  580. frame, pc);
  581. /* FDE instructions */
  582. dwarf_cfa_execute_insns(fde->instructions, fde->end, cie,
  583. fde, frame, pc);
  584. /* Calculate the CFA */
  585. switch (frame->flags) {
  586. case DWARF_FRAME_CFA_REG_OFFSET:
  587. if (prev) {
  588. reg = dwarf_frame_reg(prev, frame->cfa_register);
  589. UNWINDER_BUG_ON(!reg);
  590. UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET);
  591. addr = prev->cfa + reg->addr;
  592. frame->cfa = __raw_readl(addr);
  593. } else {
  594. /*
  595. * Again, we're starting from the top of the
  596. * stack. We need to physically read
  597. * the contents of a register in order to get
  598. * the Canonical Frame Address for this
  599. * function.
  600. */
  601. frame->cfa = dwarf_read_arch_reg(frame->cfa_register);
  602. }
  603. frame->cfa += frame->cfa_offset;
  604. break;
  605. default:
  606. UNWINDER_BUG();
  607. }
  608. reg = dwarf_frame_reg(frame, DWARF_ARCH_RA_REG);
  609. /*
  610. * If we haven't seen the return address register or the return
  611. * address column is undefined then we must assume that this is
  612. * the end of the callstack.
  613. */
  614. if (!reg || reg->flags == DWARF_UNDEFINED)
  615. goto bail;
  616. UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET);
  617. addr = frame->cfa + reg->addr;
  618. frame->return_addr = __raw_readl(addr);
  619. /*
  620. * Ah, the joys of unwinding through interrupts.
  621. *
  622. * Interrupts are tricky - the DWARF info needs to be _really_
  623. * accurate and unfortunately I'm seeing a lot of bogus DWARF
  624. * info. For example, I've seen interrupts occur in epilogues
  625. * just after the frame pointer (r14) had been restored. The
  626. * problem was that the DWARF info claimed that the CFA could be
  627. * reached by using the value of the frame pointer before it was
  628. * restored.
  629. *
  630. * So until the compiler can be trusted to produce reliable
  631. * DWARF info when it really matters, let's stop unwinding once
  632. * we've calculated the function that was interrupted.
  633. */
  634. if (prev && prev->pc == (unsigned long)ret_from_irq)
  635. frame->return_addr = 0;
  636. return frame;
  637. bail:
  638. dwarf_free_frame(frame);
  639. return NULL;
  640. }
  641. static int dwarf_parse_cie(void *entry, void *p, unsigned long len,
  642. unsigned char *end, struct module *mod)
  643. {
  644. struct rb_node **rb_node = &cie_root.rb_node;
  645. struct rb_node *parent = *rb_node;
  646. struct dwarf_cie *cie;
  647. unsigned long flags;
  648. int count;
  649. cie = kzalloc(sizeof(*cie), GFP_KERNEL);
  650. if (!cie)
  651. return -ENOMEM;
  652. cie->length = len;
  653. /*
  654. * Record the offset into the .eh_frame section
  655. * for this CIE. It allows this CIE to be
  656. * quickly and easily looked up from the
  657. * corresponding FDE.
  658. */
  659. cie->cie_pointer = (unsigned long)entry;
  660. cie->version = *(char *)p++;
  661. UNWINDER_BUG_ON(cie->version != 1);
  662. cie->augmentation = p;
  663. p += strlen(cie->augmentation) + 1;
  664. count = dwarf_read_uleb128(p, &cie->code_alignment_factor);
  665. p += count;
  666. count = dwarf_read_leb128(p, &cie->data_alignment_factor);
  667. p += count;
  668. /*
  669. * Which column in the rule table contains the
  670. * return address?
  671. */
  672. if (cie->version == 1) {
  673. cie->return_address_reg = __raw_readb(p);
  674. p++;
  675. } else {
  676. count = dwarf_read_uleb128(p, &cie->return_address_reg);
  677. p += count;
  678. }
  679. if (cie->augmentation[0] == 'z') {
  680. unsigned int length, count;
  681. cie->flags |= DWARF_CIE_Z_AUGMENTATION;
  682. count = dwarf_read_uleb128(p, &length);
  683. p += count;
  684. UNWINDER_BUG_ON((unsigned char *)p > end);
  685. cie->initial_instructions = p + length;
  686. cie->augmentation++;
  687. }
  688. while (*cie->augmentation) {
  689. /*
  690. * "L" indicates a byte showing how the
  691. * LSDA pointer is encoded. Skip it.
  692. */
  693. if (*cie->augmentation == 'L') {
  694. p++;
  695. cie->augmentation++;
  696. } else if (*cie->augmentation == 'R') {
  697. /*
  698. * "R" indicates a byte showing
  699. * how FDE addresses are
  700. * encoded.
  701. */
  702. cie->encoding = *(char *)p++;
  703. cie->augmentation++;
  704. } else if (*cie->augmentation == 'P') {
  705. /*
  706. * "R" indicates a personality
  707. * routine in the CIE
  708. * augmentation.
  709. */
  710. UNWINDER_BUG();
  711. } else if (*cie->augmentation == 'S') {
  712. UNWINDER_BUG();
  713. } else {
  714. /*
  715. * Unknown augmentation. Assume
  716. * 'z' augmentation.
  717. */
  718. p = cie->initial_instructions;
  719. UNWINDER_BUG_ON(!p);
  720. break;
  721. }
  722. }
  723. cie->initial_instructions = p;
  724. cie->instructions_end = end;
  725. /* Add to list */
  726. spin_lock_irqsave(&dwarf_cie_lock, flags);
  727. while (*rb_node) {
  728. struct dwarf_cie *cie_tmp;
  729. cie_tmp = rb_entry(*rb_node, struct dwarf_cie, node);
  730. parent = *rb_node;
  731. if (cie->cie_pointer < cie_tmp->cie_pointer)
  732. rb_node = &parent->rb_left;
  733. else if (cie->cie_pointer >= cie_tmp->cie_pointer)
  734. rb_node = &parent->rb_right;
  735. else
  736. WARN_ON(1);
  737. }
  738. rb_link_node(&cie->node, parent, rb_node);
  739. rb_insert_color(&cie->node, &cie_root);
  740. #ifdef CONFIG_MODULES
  741. if (mod != NULL)
  742. list_add_tail(&cie->link, &mod->arch.cie_list);
  743. #endif
  744. spin_unlock_irqrestore(&dwarf_cie_lock, flags);
  745. return 0;
  746. }
  747. static int dwarf_parse_fde(void *entry, u32 entry_type,
  748. void *start, unsigned long len,
  749. unsigned char *end, struct module *mod)
  750. {
  751. struct rb_node **rb_node = &fde_root.rb_node;
  752. struct rb_node *parent = *rb_node;
  753. struct dwarf_fde *fde;
  754. struct dwarf_cie *cie;
  755. unsigned long flags;
  756. int count;
  757. void *p = start;
  758. fde = kzalloc(sizeof(*fde), GFP_KERNEL);
  759. if (!fde)
  760. return -ENOMEM;
  761. fde->length = len;
  762. /*
  763. * In a .eh_frame section the CIE pointer is the
  764. * delta between the address within the FDE
  765. */
  766. fde->cie_pointer = (unsigned long)(p - entry_type - 4);
  767. cie = dwarf_lookup_cie(fde->cie_pointer);
  768. fde->cie = cie;
  769. if (cie->encoding)
  770. count = dwarf_read_encoded_value(p, &fde->initial_location,
  771. cie->encoding);
  772. else
  773. count = dwarf_read_addr(p, &fde->initial_location);
  774. p += count;
  775. if (cie->encoding)
  776. count = dwarf_read_encoded_value(p, &fde->address_range,
  777. cie->encoding & 0x0f);
  778. else
  779. count = dwarf_read_addr(p, &fde->address_range);
  780. p += count;
  781. if (fde->cie->flags & DWARF_CIE_Z_AUGMENTATION) {
  782. unsigned int length;
  783. count = dwarf_read_uleb128(p, &length);
  784. p += count + length;
  785. }
  786. /* Call frame instructions. */
  787. fde->instructions = p;
  788. fde->end = end;
  789. /* Add to list. */
  790. spin_lock_irqsave(&dwarf_fde_lock, flags);
  791. while (*rb_node) {
  792. struct dwarf_fde *fde_tmp;
  793. unsigned long tmp_start, tmp_end;
  794. unsigned long start, end;
  795. fde_tmp = rb_entry(*rb_node, struct dwarf_fde, node);
  796. start = fde->initial_location;
  797. end = fde->initial_location + fde->address_range;
  798. tmp_start = fde_tmp->initial_location;
  799. tmp_end = fde_tmp->initial_location + fde_tmp->address_range;
  800. parent = *rb_node;
  801. if (start < tmp_start)
  802. rb_node = &parent->rb_left;
  803. else if (start >= tmp_end)
  804. rb_node = &parent->rb_right;
  805. else
  806. WARN_ON(1);
  807. }
  808. rb_link_node(&fde->node, parent, rb_node);
  809. rb_insert_color(&fde->node, &fde_root);
  810. #ifdef CONFIG_MODULES
  811. if (mod != NULL)
  812. list_add_tail(&fde->link, &mod->arch.fde_list);
  813. #endif
  814. spin_unlock_irqrestore(&dwarf_fde_lock, flags);
  815. return 0;
  816. }
  817. static void dwarf_unwinder_dump(struct task_struct *task,
  818. struct pt_regs *regs,
  819. unsigned long *sp,
  820. const struct stacktrace_ops *ops,
  821. void *data)
  822. {
  823. struct dwarf_frame *frame, *_frame;
  824. unsigned long return_addr;
  825. _frame = NULL;
  826. return_addr = 0;
  827. while (1) {
  828. frame = dwarf_unwind_stack(return_addr, _frame);
  829. if (_frame)
  830. dwarf_free_frame(_frame);
  831. _frame = frame;
  832. if (!frame || !frame->return_addr)
  833. break;
  834. return_addr = frame->return_addr;
  835. ops->address(data, return_addr, 1);
  836. }
  837. if (frame)
  838. dwarf_free_frame(frame);
  839. }
  840. static struct unwinder dwarf_unwinder = {
  841. .name = "dwarf-unwinder",
  842. .dump = dwarf_unwinder_dump,
  843. .rating = 150,
  844. };
  845. static void __init dwarf_unwinder_cleanup(void)
  846. {
  847. struct dwarf_fde *fde, *next_fde;
  848. struct dwarf_cie *cie, *next_cie;
  849. /*
  850. * Deallocate all the memory allocated for the DWARF unwinder.
  851. * Traverse all the FDE/CIE lists and remove and free all the
  852. * memory associated with those data structures.
  853. */
  854. rbtree_postorder_for_each_entry_safe(fde, next_fde, &fde_root, node)
  855. kfree(fde);
  856. rbtree_postorder_for_each_entry_safe(cie, next_cie, &cie_root, node)
  857. kfree(cie);
  858. mempool_destroy(dwarf_reg_pool);
  859. mempool_destroy(dwarf_frame_pool);
  860. kmem_cache_destroy(dwarf_reg_cachep);
  861. kmem_cache_destroy(dwarf_frame_cachep);
  862. }
  863. /**
  864. * dwarf_parse_section - parse DWARF section
  865. * @eh_frame_start: start address of the .eh_frame section
  866. * @eh_frame_end: end address of the .eh_frame section
  867. * @mod: the kernel module containing the .eh_frame section
  868. *
  869. * Parse the information in a .eh_frame section.
  870. */
  871. static int dwarf_parse_section(char *eh_frame_start, char *eh_frame_end,
  872. struct module *mod)
  873. {
  874. u32 entry_type;
  875. void *p, *entry;
  876. int count, err = 0;
  877. unsigned long len = 0;
  878. unsigned int c_entries, f_entries;
  879. unsigned char *end;
  880. c_entries = 0;
  881. f_entries = 0;
  882. entry = eh_frame_start;
  883. while ((char *)entry < eh_frame_end) {
  884. p = entry;
  885. count = dwarf_entry_len(p, &len);
  886. if (count == 0) {
  887. /*
  888. * We read a bogus length field value. There is
  889. * nothing we can do here apart from disabling
  890. * the DWARF unwinder. We can't even skip this
  891. * entry and move to the next one because 'len'
  892. * tells us where our next entry is.
  893. */
  894. err = -EINVAL;
  895. goto out;
  896. } else
  897. p += count;
  898. /* initial length does not include itself */
  899. end = p + len;
  900. entry_type = get_unaligned((u32 *)p);
  901. p += 4;
  902. if (entry_type == DW_EH_FRAME_CIE) {
  903. err = dwarf_parse_cie(entry, p, len, end, mod);
  904. if (err < 0)
  905. goto out;
  906. else
  907. c_entries++;
  908. } else {
  909. err = dwarf_parse_fde(entry, entry_type, p, len,
  910. end, mod);
  911. if (err < 0)
  912. goto out;
  913. else
  914. f_entries++;
  915. }
  916. entry = (char *)entry + len + 4;
  917. }
  918. printk(KERN_INFO "DWARF unwinder initialised: read %u CIEs, %u FDEs\n",
  919. c_entries, f_entries);
  920. return 0;
  921. out:
  922. return err;
  923. }
  924. #ifdef CONFIG_MODULES
  925. int module_dwarf_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
  926. struct module *me)
  927. {
  928. unsigned int i, err;
  929. unsigned long start, end;
  930. char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  931. start = end = 0;
  932. for (i = 1; i < hdr->e_shnum; i++) {
  933. /* Alloc bit cleared means "ignore it." */
  934. if ((sechdrs[i].sh_flags & SHF_ALLOC)
  935. && !strcmp(secstrings+sechdrs[i].sh_name, ".eh_frame")) {
  936. start = sechdrs[i].sh_addr;
  937. end = start + sechdrs[i].sh_size;
  938. break;
  939. }
  940. }
  941. /* Did we find the .eh_frame section? */
  942. if (i != hdr->e_shnum) {
  943. INIT_LIST_HEAD(&me->arch.cie_list);
  944. INIT_LIST_HEAD(&me->arch.fde_list);
  945. err = dwarf_parse_section((char *)start, (char *)end, me);
  946. if (err) {
  947. printk(KERN_WARNING "%s: failed to parse DWARF info\n",
  948. me->name);
  949. return err;
  950. }
  951. }
  952. return 0;
  953. }
  954. /**
  955. * module_dwarf_cleanup - remove FDE/CIEs associated with @mod
  956. * @mod: the module that is being unloaded
  957. *
  958. * Remove any FDEs and CIEs from the global lists that came from
  959. * @mod's .eh_frame section because @mod is being unloaded.
  960. */
  961. void module_dwarf_cleanup(struct module *mod)
  962. {
  963. struct dwarf_fde *fde, *ftmp;
  964. struct dwarf_cie *cie, *ctmp;
  965. unsigned long flags;
  966. spin_lock_irqsave(&dwarf_cie_lock, flags);
  967. list_for_each_entry_safe(cie, ctmp, &mod->arch.cie_list, link) {
  968. list_del(&cie->link);
  969. rb_erase(&cie->node, &cie_root);
  970. kfree(cie);
  971. }
  972. spin_unlock_irqrestore(&dwarf_cie_lock, flags);
  973. spin_lock_irqsave(&dwarf_fde_lock, flags);
  974. list_for_each_entry_safe(fde, ftmp, &mod->arch.fde_list, link) {
  975. list_del(&fde->link);
  976. rb_erase(&fde->node, &fde_root);
  977. kfree(fde);
  978. }
  979. spin_unlock_irqrestore(&dwarf_fde_lock, flags);
  980. }
  981. #endif /* CONFIG_MODULES */
  982. /**
  983. * dwarf_unwinder_init - initialise the dwarf unwinder
  984. *
  985. * Build the data structures describing the .dwarf_frame section to
  986. * make it easier to lookup CIE and FDE entries. Because the
  987. * .eh_frame section is packed as tightly as possible it is not
  988. * easy to lookup the FDE for a given PC, so we build a list of FDE
  989. * and CIE entries that make it easier.
  990. */
  991. static int __init dwarf_unwinder_init(void)
  992. {
  993. int err = -ENOMEM;
  994. dwarf_frame_cachep = kmem_cache_create("dwarf_frames",
  995. sizeof(struct dwarf_frame), 0,
  996. SLAB_PANIC | SLAB_HWCACHE_ALIGN, NULL);
  997. dwarf_reg_cachep = kmem_cache_create("dwarf_regs",
  998. sizeof(struct dwarf_reg), 0,
  999. SLAB_PANIC | SLAB_HWCACHE_ALIGN, NULL);
  1000. dwarf_frame_pool = mempool_create_slab_pool(DWARF_FRAME_MIN_REQ,
  1001. dwarf_frame_cachep);
  1002. if (!dwarf_frame_pool)
  1003. goto out;
  1004. dwarf_reg_pool = mempool_create_slab_pool(DWARF_REG_MIN_REQ,
  1005. dwarf_reg_cachep);
  1006. if (!dwarf_reg_pool)
  1007. goto out;
  1008. err = dwarf_parse_section(__start_eh_frame, __stop_eh_frame, NULL);
  1009. if (err)
  1010. goto out;
  1011. err = unwinder_register(&dwarf_unwinder);
  1012. if (err)
  1013. goto out;
  1014. dwarf_unwinder_ready = 1;
  1015. return 0;
  1016. out:
  1017. printk(KERN_ERR "Failed to initialise DWARF unwinder: %d\n", err);
  1018. dwarf_unwinder_cleanup();
  1019. return err;
  1020. }
  1021. early_initcall(dwarf_unwinder_init);