ftrace.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. if (addr == FTRACE_ADDR) {
  67. /* opcode: or r10, lr, zero */
  68. opcode_x0 =
  69. create_Dest_X0(10) |
  70. create_SrcA_X0(TREG_LR) |
  71. create_SrcB_X0(TREG_ZERO) |
  72. create_RRROpcodeExtension_X0(OR_RRR_0_OPCODE_X0) |
  73. create_Opcode_X0(RRR_0_OPCODE_X0);
  74. } else {
  75. /* opcode: fnop */
  76. opcode_x0 =
  77. create_UnaryOpcodeExtension_X0(FNOP_UNARY_OPCODE_X0) |
  78. create_RRROpcodeExtension_X0(UNARY_RRR_0_OPCODE_X0) |
  79. create_Opcode_X0(RRR_0_OPCODE_X0);
  80. }
  81. return opcode_x1 | opcode_x0;
  82. }
  83. static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec)
  84. {
  85. return NOP();
  86. }
  87. static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr)
  88. {
  89. return ftrace_gen_branch(pc, addr, true);
  90. }
  91. static int ftrace_modify_code(unsigned long pc, unsigned long old,
  92. unsigned long new)
  93. {
  94. unsigned long pc_wr;
  95. /* Check if the address is in kernel text space and module space. */
  96. if (!kernel_text_address(pc))
  97. return -EINVAL;
  98. /* Operate on writable kernel text mapping. */
  99. pc_wr = pc - MEM_SV_START + PAGE_OFFSET;
  100. if (probe_kernel_write((void *)pc_wr, &new, MCOUNT_INSN_SIZE))
  101. return -EPERM;
  102. smp_wmb();
  103. if (!machine_stopped && num_online_cpus() > 1)
  104. flush_icache_range(pc, pc + MCOUNT_INSN_SIZE);
  105. return 0;
  106. }
  107. int ftrace_update_ftrace_func(ftrace_func_t func)
  108. {
  109. unsigned long pc, old;
  110. unsigned long new;
  111. int ret;
  112. pc = (unsigned long)&ftrace_call;
  113. memcpy(&old, &ftrace_call, MCOUNT_INSN_SIZE);
  114. new = ftrace_call_replace(pc, (unsigned long)func);
  115. ret = ftrace_modify_code(pc, old, new);
  116. return ret;
  117. }
  118. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  119. {
  120. unsigned long new, old;
  121. unsigned long ip = rec->ip;
  122. old = ftrace_nop_replace(rec);
  123. new = ftrace_call_replace(ip, addr);
  124. return ftrace_modify_code(rec->ip, old, new);
  125. }
  126. int ftrace_make_nop(struct module *mod,
  127. struct dyn_ftrace *rec, unsigned long addr)
  128. {
  129. unsigned long ip = rec->ip;
  130. unsigned long old;
  131. unsigned long new;
  132. int ret;
  133. old = ftrace_call_replace(ip, addr);
  134. new = ftrace_nop_replace(rec);
  135. ret = ftrace_modify_code(ip, old, new);
  136. return ret;
  137. }
  138. int __init ftrace_dyn_arch_init(void *data)
  139. {
  140. *(unsigned long *)data = 0;
  141. return 0;
  142. }
  143. #endif /* CONFIG_DYNAMIC_FTRACE */
  144. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  145. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
  146. unsigned long frame_pointer)
  147. {
  148. unsigned long return_hooker = (unsigned long) &return_to_handler;
  149. struct ftrace_graph_ent trace;
  150. unsigned long old;
  151. int err;
  152. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  153. return;
  154. old = *parent;
  155. *parent = return_hooker;
  156. err = ftrace_push_return_trace(old, self_addr, &trace.depth,
  157. frame_pointer);
  158. if (err == -EBUSY) {
  159. *parent = old;
  160. return;
  161. }
  162. trace.func = self_addr;
  163. /* Only trace if the calling function expects to */
  164. if (!ftrace_graph_entry(&trace)) {
  165. current->curr_ret_stack--;
  166. *parent = old;
  167. }
  168. }
  169. #ifdef CONFIG_DYNAMIC_FTRACE
  170. extern unsigned long ftrace_graph_call;
  171. static int __ftrace_modify_caller(unsigned long *callsite,
  172. void (*func) (void), bool enable)
  173. {
  174. unsigned long caller_fn = (unsigned long) func;
  175. unsigned long pc = (unsigned long) callsite;
  176. unsigned long branch = ftrace_gen_branch(pc, caller_fn, false);
  177. unsigned long nop = NOP();
  178. unsigned long old = enable ? nop : branch;
  179. unsigned long new = enable ? branch : nop;
  180. return ftrace_modify_code(pc, old, new);
  181. }
  182. static int ftrace_modify_graph_caller(bool enable)
  183. {
  184. int ret;
  185. ret = __ftrace_modify_caller(&ftrace_graph_call,
  186. ftrace_graph_caller,
  187. enable);
  188. return ret;
  189. }
  190. int ftrace_enable_ftrace_graph_caller(void)
  191. {
  192. return ftrace_modify_graph_caller(true);
  193. }
  194. int ftrace_disable_ftrace_graph_caller(void)
  195. {
  196. return ftrace_modify_graph_caller(false);
  197. }
  198. #endif /* CONFIG_DYNAMIC_FTRACE */
  199. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */