ftrace.c 5.8 KB

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