ftrace.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 <asm/insn.h>
  22. #include <arch/opcode.h>
  23. #ifdef CONFIG_DYNAMIC_FTRACE
  24. static int machine_stopped __read_mostly;
  25. int ftrace_arch_code_modify_prepare(void)
  26. {
  27. machine_stopped = 1;
  28. return 0;
  29. }
  30. int ftrace_arch_code_modify_post_process(void)
  31. {
  32. flush_icache_range(0, CHIP_L1I_CACHE_SIZE());
  33. machine_stopped = 0;
  34. return 0;
  35. }
  36. /*
  37. * Put { move r10, lr; jal ftrace_caller } in a bundle, this lets dynamic
  38. * tracer just add one cycle overhead to every kernel function when disabled.
  39. */
  40. static unsigned long ftrace_gen_branch(unsigned long pc, unsigned long addr,
  41. bool link)
  42. {
  43. tilegx_bundle_bits opcode_x0, opcode_x1;
  44. long pcrel_by_instr = (addr - pc) >> TILEGX_LOG2_BUNDLE_SIZE_IN_BYTES;
  45. if (link) {
  46. /* opcode: jal addr */
  47. opcode_x1 =
  48. create_Opcode_X1(JUMP_OPCODE_X1) |
  49. create_JumpOpcodeExtension_X1(JAL_JUMP_OPCODE_X1) |
  50. create_JumpOff_X1(pcrel_by_instr);
  51. } else {
  52. /* opcode: j addr */
  53. opcode_x1 =
  54. create_Opcode_X1(JUMP_OPCODE_X1) |
  55. create_JumpOpcodeExtension_X1(J_JUMP_OPCODE_X1) |
  56. create_JumpOff_X1(pcrel_by_instr);
  57. }
  58. /*
  59. * Also put { move r10, lr; jal ftrace_stub } in a bundle, which
  60. * is used to replace the instruction in address ftrace_call.
  61. */
  62. if (addr == FTRACE_ADDR || addr == (unsigned long)ftrace_stub) {
  63. /* opcode: or r10, lr, zero */
  64. opcode_x0 =
  65. create_Dest_X0(10) |
  66. create_SrcA_X0(TREG_LR) |
  67. create_SrcB_X0(TREG_ZERO) |
  68. create_RRROpcodeExtension_X0(OR_RRR_0_OPCODE_X0) |
  69. create_Opcode_X0(RRR_0_OPCODE_X0);
  70. } else {
  71. /* opcode: fnop */
  72. opcode_x0 =
  73. create_UnaryOpcodeExtension_X0(FNOP_UNARY_OPCODE_X0) |
  74. create_RRROpcodeExtension_X0(UNARY_RRR_0_OPCODE_X0) |
  75. create_Opcode_X0(RRR_0_OPCODE_X0);
  76. }
  77. return opcode_x1 | opcode_x0;
  78. }
  79. static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec)
  80. {
  81. return NOP();
  82. }
  83. static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr)
  84. {
  85. return ftrace_gen_branch(pc, addr, true);
  86. }
  87. static int ftrace_modify_code(unsigned long pc, unsigned long old,
  88. unsigned long new)
  89. {
  90. unsigned long pc_wr;
  91. /* Check if the address is in kernel text space and module space. */
  92. if (!kernel_text_address(pc))
  93. return -EINVAL;
  94. /* Operate on writable kernel text mapping. */
  95. pc_wr = ktext_writable_addr(pc);
  96. if (probe_kernel_write((void *)pc_wr, &new, MCOUNT_INSN_SIZE))
  97. return -EPERM;
  98. smp_wmb();
  99. if (!machine_stopped && num_online_cpus() > 1)
  100. flush_icache_range(pc, pc + MCOUNT_INSN_SIZE);
  101. return 0;
  102. }
  103. int ftrace_update_ftrace_func(ftrace_func_t func)
  104. {
  105. unsigned long pc, old;
  106. unsigned long new;
  107. int ret;
  108. pc = (unsigned long)&ftrace_call;
  109. memcpy(&old, &ftrace_call, MCOUNT_INSN_SIZE);
  110. new = ftrace_call_replace(pc, (unsigned long)func);
  111. ret = ftrace_modify_code(pc, old, new);
  112. return ret;
  113. }
  114. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  115. {
  116. unsigned long new, old;
  117. unsigned long ip = rec->ip;
  118. old = ftrace_nop_replace(rec);
  119. new = ftrace_call_replace(ip, addr);
  120. return ftrace_modify_code(rec->ip, old, new);
  121. }
  122. int ftrace_make_nop(struct module *mod,
  123. struct dyn_ftrace *rec, unsigned long addr)
  124. {
  125. unsigned long ip = rec->ip;
  126. unsigned long old;
  127. unsigned long new;
  128. int ret;
  129. old = ftrace_call_replace(ip, addr);
  130. new = ftrace_nop_replace(rec);
  131. ret = ftrace_modify_code(ip, old, new);
  132. return ret;
  133. }
  134. int __init ftrace_dyn_arch_init(void)
  135. {
  136. return 0;
  137. }
  138. #endif /* CONFIG_DYNAMIC_FTRACE */
  139. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  140. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
  141. unsigned long frame_pointer)
  142. {
  143. unsigned long return_hooker = (unsigned long) &return_to_handler;
  144. struct ftrace_graph_ent trace;
  145. unsigned long old;
  146. int err;
  147. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  148. return;
  149. old = *parent;
  150. *parent = return_hooker;
  151. err = ftrace_push_return_trace(old, self_addr, &trace.depth,
  152. frame_pointer, NULL);
  153. if (err == -EBUSY) {
  154. *parent = old;
  155. return;
  156. }
  157. trace.func = self_addr;
  158. /* Only trace if the calling function expects to */
  159. if (!ftrace_graph_entry(&trace)) {
  160. current->curr_ret_stack--;
  161. *parent = old;
  162. }
  163. }
  164. #ifdef CONFIG_DYNAMIC_FTRACE
  165. extern unsigned long ftrace_graph_call;
  166. static int __ftrace_modify_caller(unsigned long *callsite,
  167. void (*func) (void), bool enable)
  168. {
  169. unsigned long caller_fn = (unsigned long) func;
  170. unsigned long pc = (unsigned long) callsite;
  171. unsigned long branch = ftrace_gen_branch(pc, caller_fn, false);
  172. unsigned long nop = NOP();
  173. unsigned long old = enable ? nop : branch;
  174. unsigned long new = enable ? branch : nop;
  175. return ftrace_modify_code(pc, old, new);
  176. }
  177. static int ftrace_modify_graph_caller(bool enable)
  178. {
  179. int ret;
  180. ret = __ftrace_modify_caller(&ftrace_graph_call,
  181. ftrace_graph_caller,
  182. enable);
  183. return ret;
  184. }
  185. int ftrace_enable_ftrace_graph_caller(void)
  186. {
  187. return ftrace_modify_graph_caller(true);
  188. }
  189. int ftrace_disable_ftrace_graph_caller(void)
  190. {
  191. return ftrace_modify_graph_caller(false);
  192. }
  193. #endif /* CONFIG_DYNAMIC_FTRACE */
  194. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */