dwarf.c 25 KB

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