ftrace.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright 2012 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. *
  14. * TILE-Gx specific ftrace support
  15. */
  16. #include <linux/ftrace.h>
  17. #include <linux/uaccess.h>
  18. #include <asm/cacheflush.h>
  19. #include <asm/ftrace.h>
  20. #include <asm/sections.h>
  21. #include <arch/opcode.h>
  22. #ifdef CONFIG_DYNAMIC_FTRACE
  23. static inline tilegx_bundle_bits NOP(void)
  24. {
  25. return create_UnaryOpcodeExtension_X0(FNOP_UNARY_OPCODE_X0) |
  26. create_RRROpcodeExtension_X0(UNARY_RRR_0_OPCODE_X0) |
  27. create_Opcode_X0(RRR_0_OPCODE_X0) |
  28. create_UnaryOpcodeExtension_X1(NOP_UNARY_OPCODE_X1) |
  29. create_RRROpcodeExtension_X1(UNARY_RRR_0_OPCODE_X1) |
  30. create_Opcode_X1(RRR_0_OPCODE_X1);
  31. }
  32. static int machine_stopped __read_mostly;
  33. int ftrace_arch_code_modify_prepare(void)
  34. {
  35. machine_stopped = 1;
  36. return 0;
  37. }
  38. int ftrace_arch_code_modify_post_process(void)
  39. {
  40. flush_icache_range(0, CHIP_L1I_CACHE_SIZE());
  41. machine_stopped = 0;
  42. return 0;
  43. }
  44. /*
  45. * Put { move r10, lr; jal ftrace_caller } in a bundle, this lets dynamic
  46. * tracer just add one cycle overhead to every kernel function when disabled.
  47. */
  48. static unsigned long ftrace_gen_branch(unsigned long pc, unsigned long addr,
  49. bool link)
  50. {
  51. tilegx_bundle_bits opcode_x0, opcode_x1;
  52. long pcrel_by_instr = (addr - pc) >> TILEGX_LOG2_BUNDLE_SIZE_IN_BYTES;
  53. if (link) {
  54. /* opcode: jal addr */
  55. opcode_x1 =
  56. create_Opcode_X1(JUMP_OPCODE_X1) |
  57. create_JumpOpcodeExtension_X1(JAL_JUMP_OPCODE_X1) |
  58. create_JumpOff_X1(pcrel_by_instr);
  59. } else {
  60. /* opcode: j addr */
  61. opcode_x1 =
  62. create_Opcode_X1(JUMP_OPCODE_X1) |
  63. create_JumpOpcodeExtension_X1(J_JUMP_OPCODE_X1) |
  64. create_JumpOff_X1(pcrel_by_instr);
  65. }
  66. /*
  67. * Also put { move r10, lr; jal ftrace_stub } in a bundle, which
  68. * is used to replace the instruction in address ftrace_call.
  69. */
  70. if (addr == FTRACE_ADDR || addr == (unsigned long)ftrace_stub) {
  71. /* opcode: or r10, lr, zero */
  72. opcode_x0 =
  73. create_Dest_X0(10) |
  74. create_SrcA_X0(TREG_LR) |
  75. create_SrcB_X0(TREG_ZERO) |
  76. create_RRROpcodeExtension_X0(OR_RRR_0_OPCODE_X0) |
  77. create_Opcode_X0(RRR_0_OPCODE_X0);
  78. } else {
  79. /* opcode: fnop */
  80. opcode_x0 =
  81. create_UnaryOpcodeExtension_X0(FNOP_UNARY_OPCODE_X0) |
  82. create_RRROpcodeExtension_X0(UNARY_RRR_0_OPCODE_X0) |
  83. create_Opcode_X0(RRR_0_OPCODE_X0);
  84. }
  85. return opcode_x1 | opcode_x0;
  86. }
  87. static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec)
  88. {
  89. return NOP();
  90. }
  91. static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr)
  92. {
  93. return ftrace_gen_branch(pc, addr, true);
  94. }
  95. static int ftrace_modify_code(unsigned long pc, unsigned long old,
  96. unsigned long new)
  97. {
  98. unsigned long pc_wr;
  99. /* Check if the address is in kernel text space and module space. */
  100. if (!kernel_text_address(pc))
  101. return -EINVAL;
  102. /* Operate on writable kernel text mapping. */
  103. pc_wr = pc - MEM_SV_START + PAGE_OFFSET;
  104. if (probe_kernel_write((void *)pc_wr, &new, MCOUNT_INSN_SIZE))
  105. return -EPERM;
  106. smp_wmb();
  107. if (!machine_stopped && num_online_cpus() > 1)
  108. flush_icache_range(pc, pc + MCOUNT_INSN_SIZE);
  109. return 0;
  110. }
  111. int ftrace_update_ftrace_func(ftrace_func_t func)
  112. {
  113. unsigned long pc, old;
  114. unsigned long new;
  115. int ret;
  116. pc = (unsigned long)&ftrace_call;
  117. memcpy(&old, &ftrace_call, MCOUNT_INSN_SIZE);
  118. new = ftrace_call_replace(pc, (unsigned long)func);
  119. ret = ftrace_modify_code(pc, old, new);
  120. return ret;
  121. }
  122. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  123. {
  124. unsigned long new, old;
  125. unsigned long ip = rec->ip;
  126. old = ftrace_nop_replace(rec);
  127. new = ftrace_call_replace(ip, addr);
  128. return ftrace_modify_code(rec->ip, old, new);
  129. }
  130. int ftrace_make_nop(struct module *mod,
  131. struct dyn_ftrace *rec, unsigned long addr)
  132. {
  133. unsigned long ip = rec->ip;
  134. unsigned long old;
  135. unsigned long new;
  136. int ret;
  137. old = ftrace_call_replace(ip, addr);
  138. new = ftrace_nop_replace(rec);
  139. ret = ftrace_modify_code(ip, old, new);
  140. return ret;
  141. }
  142. int __init ftrace_dyn_arch_init(void)
  143. {
  144. return 0;
  145. }
  146. #endif /* CONFIG_DYNAMIC_FTRACE */
  147. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  148. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
  149. unsigned long frame_pointer)
  150. {
  151. unsigned long return_hooker = (unsigned long) &return_to_handler;
  152. struct ftrace_graph_ent trace;
  153. unsigned long old;
  154. int err;
  155. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  156. return;
  157. old = *parent;
  158. *parent = return_hooker;
  159. err = ftrace_push_return_trace(old, self_addr, &trace.depth,
  160. frame_pointer);
  161. if (err == -EBUSY) {
  162. *parent = old;
  163. return;
  164. }
  165. trace.func = self_addr;
  166. /* Only trace if the calling function expects to */
  167. if (!ftrace_graph_entry(&trace)) {
  168. current->curr_ret_stack--;
  169. *parent = old;
  170. }
  171. }
  172. #ifdef CONFIG_DYNAMIC_FTRACE
  173. extern unsigned long ftrace_graph_call;
  174. static int __ftrace_modify_caller(unsigned long *callsite,
  175. void (*func) (void), bool enable)
  176. {
  177. unsigned long caller_fn = (unsigned long) func;
  178. unsigned long pc = (unsigned long) callsite;
  179. unsigned long branch = ftrace_gen_branch(pc, caller_fn, false);
  180. unsigned long nop = NOP();
  181. unsigned long old = enable ? nop : branch;
  182. unsigned long new = enable ? branch : nop;
  183. return ftrace_modify_code(pc, old, new);
  184. }
  185. static int ftrace_modify_graph_caller(bool enable)
  186. {
  187. int ret;
  188. ret = __ftrace_modify_caller(&ftrace_graph_call,
  189. ftrace_graph_caller,
  190. enable);
  191. return ret;
  192. }
  193. int ftrace_enable_ftrace_graph_caller(void)
  194. {
  195. return ftrace_modify_graph_caller(true);
  196. }
  197. int ftrace_disable_ftrace_graph_caller(void)
  198. {
  199. return ftrace_modify_graph_caller(false);
  200. }
  201. #endif /* CONFIG_DYNAMIC_FTRACE */
  202. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */