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(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. flush_dcache_range((u32)parent, (u32)parent + 4);
  50. flush_icache_range((u32)parent, (u32)parent + 4);
  51. if (unlikely(faulted)) {
  52. ftrace_graph_stop();
  53. WARN_ON(1);
  54. return;
  55. }
  56. err = ftrace_push_return_trace(old, self_addr, &trace.depth, 0);
  57. if (err == -EBUSY) {
  58. *parent = old;
  59. return;
  60. }
  61. trace.func = self_addr;
  62. /* Only trace if the calling function expects to */
  63. if (!ftrace_graph_entry(&trace)) {
  64. current->curr_ret_stack--;
  65. *parent = old;
  66. }
  67. }
  68. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  69. #ifdef CONFIG_DYNAMIC_FTRACE
  70. /* save value to addr - it is save to do it in asm */
  71. static int ftrace_modify_code(unsigned long addr, unsigned int value)
  72. {
  73. int faulted = 0;
  74. __asm__ __volatile__(" 1: swi %2, %1, 0; \
  75. addik %0, r0, 0; \
  76. 2: \
  77. .section .fixup, \"ax\"; \
  78. 3: brid 2b; \
  79. addik %0, r0, 1; \
  80. .previous; \
  81. .section __ex_table,\"a\"; \
  82. .word 1b,3b; \
  83. .previous;" \
  84. : "=r" (faulted)
  85. : "r" (addr), "r" (value)
  86. );
  87. if (unlikely(faulted))
  88. return -EFAULT;
  89. flush_dcache_range(addr, addr + 4);
  90. flush_icache_range(addr, addr + 4);
  91. return 0;
  92. }
  93. #define MICROBLAZE_NOP 0x80000000
  94. #define MICROBLAZE_BRI 0xb800000C
  95. static unsigned int recorded; /* if save was or not */
  96. static unsigned int imm; /* saving whole imm instruction */
  97. /* There are two approaches howto solve ftrace_make nop function - look below */
  98. #undef USE_FTRACE_NOP
  99. #ifdef USE_FTRACE_NOP
  100. static unsigned int bralid; /* saving whole bralid instruction */
  101. #endif
  102. int ftrace_make_nop(struct module *mod,
  103. struct dyn_ftrace *rec, unsigned long addr)
  104. {
  105. /* we have this part of code which we are working with
  106. * b000c000 imm -16384
  107. * b9fc8e30 bralid r15, -29136 // c0008e30 <_mcount>
  108. * 80000000 or r0, r0, r0
  109. *
  110. * The first solution (!USE_FTRACE_NOP-could be called branch solution)
  111. * b000c000 bri 12 (0xC - jump to any other instruction)
  112. * b9fc8e30 bralid r15, -29136 // c0008e30 <_mcount>
  113. * 80000000 or r0, r0, r0
  114. * any other instruction
  115. *
  116. * The second solution (USE_FTRACE_NOP) - no jump just nops
  117. * 80000000 or r0, r0, r0
  118. * 80000000 or r0, r0, r0
  119. * 80000000 or r0, r0, r0
  120. */
  121. int ret = 0;
  122. if (recorded == 0) {
  123. recorded = 1;
  124. imm = *(unsigned int *)rec->ip;
  125. pr_debug("%s: imm:0x%x\n", __func__, imm);
  126. #ifdef USE_FTRACE_NOP
  127. bralid = *(unsigned int *)(rec->ip + 4);
  128. pr_debug("%s: bralid 0x%x\n", __func__, bralid);
  129. #endif /* USE_FTRACE_NOP */
  130. }
  131. #ifdef USE_FTRACE_NOP
  132. ret = ftrace_modify_code(rec->ip, MICROBLAZE_NOP);
  133. ret += ftrace_modify_code(rec->ip + 4, MICROBLAZE_NOP);
  134. #else /* USE_FTRACE_NOP */
  135. ret = ftrace_modify_code(rec->ip, MICROBLAZE_BRI);
  136. #endif /* USE_FTRACE_NOP */
  137. return ret;
  138. }
  139. /* I believe that first is called ftrace_make_nop before this function */
  140. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  141. {
  142. int ret;
  143. pr_debug("%s: addr:0x%x, rec->ip: 0x%x, imm:0x%x\n",
  144. __func__, (unsigned int)addr, (unsigned int)rec->ip, imm);
  145. ret = ftrace_modify_code(rec->ip, imm);
  146. #ifdef USE_FTRACE_NOP
  147. pr_debug("%s: bralid:0x%x\n", __func__, bralid);
  148. ret += ftrace_modify_code(rec->ip + 4, bralid);
  149. #endif /* USE_FTRACE_NOP */
  150. return ret;
  151. }
  152. int __init ftrace_dyn_arch_init(void *data)
  153. {
  154. /* The return code is retured via data */
  155. *(unsigned long *)data = 0;
  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 */