ftrace.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Ftrace support for Microblaze.
  3. *
  4. * Copyright (C) 2009 Michal Simek <monstr@monstr.eu>
  5. * Copyright (C) 2009 PetaLogix
  6. *
  7. * Based on MIPS and PowerPC ftrace code
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file "COPYING" in the main directory of this archive
  11. * for more details.
  12. */
  13. #include <asm/cacheflush.h>
  14. #include <linux/ftrace.h>
  15. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  16. /*
  17. * Hook the return address and push it in the stack of return addrs
  18. * in current thread info.
  19. */
  20. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
  21. {
  22. unsigned long old;
  23. int faulted, err;
  24. struct ftrace_graph_ent trace;
  25. unsigned long return_hooker = (unsigned long)
  26. &return_to_handler;
  27. if (unlikely(ftrace_graph_is_dead()))
  28. return;
  29. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  30. return;
  31. /*
  32. * Protect against fault, even if it shouldn't
  33. * happen. This tool is too much intrusive to
  34. * ignore such a protection.
  35. */
  36. asm volatile(" 1: lwi %0, %2, 0;" \
  37. "2: swi %3, %2, 0;" \
  38. " addik %1, r0, 0;" \
  39. "3:" \
  40. " .section .fixup, \"ax\";" \
  41. "4: brid 3b;" \
  42. " addik %1, r0, 1;" \
  43. " .previous;" \
  44. " .section __ex_table,\"a\";" \
  45. " .word 1b,4b;" \
  46. " .word 2b,4b;" \
  47. " .previous;" \
  48. : "=&r" (old), "=r" (faulted)
  49. : "r" (parent), "r" (return_hooker)
  50. );
  51. flush_dcache_range((u32)parent, (u32)parent + 4);
  52. flush_icache_range((u32)parent, (u32)parent + 4);
  53. if (unlikely(faulted)) {
  54. ftrace_graph_stop();
  55. WARN_ON(1);
  56. return;
  57. }
  58. err = ftrace_push_return_trace(old, self_addr, &trace.depth, 0, NULL);
  59. if (err == -EBUSY) {
  60. *parent = old;
  61. return;
  62. }
  63. trace.func = self_addr;
  64. /* Only trace if the calling function expects to */
  65. if (!ftrace_graph_entry(&trace)) {
  66. current->curr_ret_stack--;
  67. *parent = old;
  68. }
  69. }
  70. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  71. #ifdef CONFIG_DYNAMIC_FTRACE
  72. /* save value to addr - it is save to do it in asm */
  73. static int ftrace_modify_code(unsigned long addr, unsigned int value)
  74. {
  75. int faulted = 0;
  76. __asm__ __volatile__(" 1: swi %2, %1, 0;" \
  77. " addik %0, r0, 0;" \
  78. "2:" \
  79. " .section .fixup, \"ax\";" \
  80. "3: brid 2b;" \
  81. " addik %0, r0, 1;" \
  82. " .previous;" \
  83. " .section __ex_table,\"a\";" \
  84. " .word 1b,3b;" \
  85. " .previous;" \
  86. : "=r" (faulted)
  87. : "r" (addr), "r" (value)
  88. );
  89. if (unlikely(faulted))
  90. return -EFAULT;
  91. flush_dcache_range(addr, addr + 4);
  92. flush_icache_range(addr, addr + 4);
  93. return 0;
  94. }
  95. #define MICROBLAZE_NOP 0x80000000
  96. #define MICROBLAZE_BRI 0xb800000C
  97. static unsigned int recorded; /* if save was or not */
  98. static unsigned int imm; /* saving whole imm instruction */
  99. /* There are two approaches howto solve ftrace_make nop function - look below */
  100. #undef USE_FTRACE_NOP
  101. #ifdef USE_FTRACE_NOP
  102. static unsigned int bralid; /* saving whole bralid instruction */
  103. #endif
  104. int ftrace_make_nop(struct module *mod,
  105. struct dyn_ftrace *rec, unsigned long addr)
  106. {
  107. /* we have this part of code which we are working with
  108. * b000c000 imm -16384
  109. * b9fc8e30 bralid r15, -29136 // c0008e30 <_mcount>
  110. * 80000000 or r0, r0, r0
  111. *
  112. * The first solution (!USE_FTRACE_NOP-could be called branch solution)
  113. * b000c000 bri 12 (0xC - jump to any other instruction)
  114. * b9fc8e30 bralid r15, -29136 // c0008e30 <_mcount>
  115. * 80000000 or r0, r0, r0
  116. * any other instruction
  117. *
  118. * The second solution (USE_FTRACE_NOP) - no jump just nops
  119. * 80000000 or r0, r0, r0
  120. * 80000000 or r0, r0, r0
  121. * 80000000 or r0, r0, r0
  122. */
  123. int ret = 0;
  124. if (recorded == 0) {
  125. recorded = 1;
  126. imm = *(unsigned int *)rec->ip;
  127. pr_debug("%s: imm:0x%x\n", __func__, imm);
  128. #ifdef USE_FTRACE_NOP
  129. bralid = *(unsigned int *)(rec->ip + 4);
  130. pr_debug("%s: bralid 0x%x\n", __func__, bralid);
  131. #endif /* USE_FTRACE_NOP */
  132. }
  133. #ifdef USE_FTRACE_NOP
  134. ret = ftrace_modify_code(rec->ip, MICROBLAZE_NOP);
  135. ret += ftrace_modify_code(rec->ip + 4, MICROBLAZE_NOP);
  136. #else /* USE_FTRACE_NOP */
  137. ret = ftrace_modify_code(rec->ip, MICROBLAZE_BRI);
  138. #endif /* USE_FTRACE_NOP */
  139. return ret;
  140. }
  141. /* I believe that first is called ftrace_make_nop before this function */
  142. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  143. {
  144. int ret;
  145. pr_debug("%s: addr:0x%x, rec->ip: 0x%x, imm:0x%x\n",
  146. __func__, (unsigned int)addr, (unsigned int)rec->ip, imm);
  147. ret = ftrace_modify_code(rec->ip, imm);
  148. #ifdef USE_FTRACE_NOP
  149. pr_debug("%s: bralid:0x%x\n", __func__, bralid);
  150. ret += ftrace_modify_code(rec->ip + 4, bralid);
  151. #endif /* USE_FTRACE_NOP */
  152. return ret;
  153. }
  154. int __init ftrace_dyn_arch_init(void)
  155. {
  156. return 0;
  157. }
  158. int ftrace_update_ftrace_func(ftrace_func_t func)
  159. {
  160. unsigned long ip = (unsigned long)(&ftrace_call);
  161. unsigned int upper = (unsigned int)func;
  162. unsigned int lower = (unsigned int)func;
  163. int ret = 0;
  164. /* create proper saving to ftrace_call poll */
  165. upper = 0xb0000000 + (upper >> 16); /* imm func_upper */
  166. lower = 0x32800000 + (lower & 0xFFFF); /* addik r20, r0, func_lower */
  167. pr_debug("%s: func=0x%x, ip=0x%x, upper=0x%x, lower=0x%x\n",
  168. __func__, (unsigned int)func, (unsigned int)ip, upper, lower);
  169. /* save upper and lower code */
  170. ret = ftrace_modify_code(ip, upper);
  171. ret += ftrace_modify_code(ip + 4, lower);
  172. /* We just need to replace the rtsd r15, 8 with NOP */
  173. ret += ftrace_modify_code((unsigned long)&ftrace_caller,
  174. MICROBLAZE_NOP);
  175. return ret;
  176. }
  177. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  178. unsigned int old_jump; /* saving place for jump instruction */
  179. int ftrace_enable_ftrace_graph_caller(void)
  180. {
  181. unsigned int ret;
  182. unsigned long ip = (unsigned long)(&ftrace_call_graph);
  183. old_jump = *(unsigned int *)ip; /* save jump over instruction */
  184. ret = ftrace_modify_code(ip, MICROBLAZE_NOP);
  185. pr_debug("%s: Replace instruction: 0x%x\n", __func__, old_jump);
  186. return ret;
  187. }
  188. int ftrace_disable_ftrace_graph_caller(void)
  189. {
  190. unsigned int ret;
  191. unsigned long ip = (unsigned long)(&ftrace_call_graph);
  192. ret = ftrace_modify_code(ip, old_jump);
  193. pr_debug("%s\n", __func__);
  194. return ret;
  195. }
  196. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  197. #endif /* CONFIG_DYNAMIC_FTRACE */