dwarf.c 25 KB

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