ftrace.c 6.0 KB

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