ftrace.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Dynamic function tracing support.
  3. *
  4. * Copyright (C) 2008 Abhishek Sagar <sagar.abhishek@gmail.com>
  5. * Copyright (C) 2010 Rabin Vincent <rabin@rab.in>
  6. *
  7. * For licencing details, see COPYING.
  8. *
  9. * Defines low-level handling of mcount calls when the kernel
  10. * is compiled with the -pg flag. When using dynamic ftrace, the
  11. * mcount call-sites get patched with NOP till they are enabled.
  12. * All code mutation routines here are called under stop_machine().
  13. */
  14. #include <linux/ftrace.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/module.h>
  17. #include <linux/stop_machine.h>
  18. #include <asm/cacheflush.h>
  19. #include <asm/opcodes.h>
  20. #include <asm/ftrace.h>
  21. #include <asm/insn.h>
  22. #include <asm/set_memory.h>
  23. #ifdef CONFIG_THUMB2_KERNEL
  24. #define NOP 0xf85deb04 /* pop.w {lr} */
  25. #else
  26. #define NOP 0xe8bd4000 /* pop {lr} */
  27. #endif
  28. #ifdef CONFIG_DYNAMIC_FTRACE
  29. static int __ftrace_modify_code(void *data)
  30. {
  31. int *command = data;
  32. set_kernel_text_rw();
  33. ftrace_modify_all_code(*command);
  34. set_kernel_text_ro();
  35. return 0;
  36. }
  37. void arch_ftrace_update_code(int command)
  38. {
  39. stop_machine(__ftrace_modify_code, &command, NULL);
  40. }
  41. static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec)
  42. {
  43. return NOP;
  44. }
  45. static unsigned long adjust_address(struct dyn_ftrace *rec, unsigned long addr)
  46. {
  47. return addr;
  48. }
  49. int ftrace_arch_code_modify_prepare(void)
  50. {
  51. set_all_modules_text_rw();
  52. return 0;
  53. }
  54. int ftrace_arch_code_modify_post_process(void)
  55. {
  56. set_all_modules_text_ro();
  57. /* Make sure any TLB misses during machine stop are cleared. */
  58. flush_tlb_all();
  59. return 0;
  60. }
  61. static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr)
  62. {
  63. return arm_gen_branch_link(pc, addr);
  64. }
  65. static int ftrace_modify_code(unsigned long pc, unsigned long old,
  66. unsigned long new, bool validate)
  67. {
  68. unsigned long replaced;
  69. if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) {
  70. old = __opcode_to_mem_thumb32(old);
  71. new = __opcode_to_mem_thumb32(new);
  72. } else {
  73. old = __opcode_to_mem_arm(old);
  74. new = __opcode_to_mem_arm(new);
  75. }
  76. if (validate) {
  77. if (probe_kernel_read(&replaced, (void *)pc, MCOUNT_INSN_SIZE))
  78. return -EFAULT;
  79. if (replaced != old)
  80. return -EINVAL;
  81. }
  82. if (probe_kernel_write((void *)pc, &new, MCOUNT_INSN_SIZE))
  83. return -EPERM;
  84. flush_icache_range(pc, pc + MCOUNT_INSN_SIZE);
  85. return 0;
  86. }
  87. int ftrace_update_ftrace_func(ftrace_func_t func)
  88. {
  89. unsigned long pc;
  90. unsigned long new;
  91. int ret;
  92. pc = (unsigned long)&ftrace_call;
  93. new = ftrace_call_replace(pc, (unsigned long)func);
  94. ret = ftrace_modify_code(pc, 0, new, false);
  95. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  96. if (!ret) {
  97. pc = (unsigned long)&ftrace_regs_call;
  98. new = ftrace_call_replace(pc, (unsigned long)func);
  99. ret = ftrace_modify_code(pc, 0, new, false);
  100. }
  101. #endif
  102. return ret;
  103. }
  104. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  105. {
  106. unsigned long new, old;
  107. unsigned long ip = rec->ip;
  108. old = ftrace_nop_replace(rec);
  109. new = ftrace_call_replace(ip, adjust_address(rec, addr));
  110. return ftrace_modify_code(rec->ip, old, new, true);
  111. }
  112. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  113. int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
  114. unsigned long addr)
  115. {
  116. unsigned long new, old;
  117. unsigned long ip = rec->ip;
  118. old = ftrace_call_replace(ip, adjust_address(rec, old_addr));
  119. new = ftrace_call_replace(ip, adjust_address(rec, addr));
  120. return ftrace_modify_code(rec->ip, old, new, true);
  121. }
  122. #endif
  123. int ftrace_make_nop(struct module *mod,
  124. struct dyn_ftrace *rec, unsigned long addr)
  125. {
  126. unsigned long ip = rec->ip;
  127. unsigned long old;
  128. unsigned long new;
  129. int ret;
  130. old = ftrace_call_replace(ip, adjust_address(rec, addr));
  131. new = ftrace_nop_replace(rec);
  132. ret = ftrace_modify_code(ip, old, new, true);
  133. return ret;
  134. }
  135. int __init ftrace_dyn_arch_init(void)
  136. {
  137. return 0;
  138. }
  139. #endif /* CONFIG_DYNAMIC_FTRACE */
  140. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  141. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
  142. unsigned long frame_pointer)
  143. {
  144. unsigned long return_hooker = (unsigned long) &return_to_handler;
  145. unsigned long old;
  146. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  147. return;
  148. old = *parent;
  149. *parent = return_hooker;
  150. if (function_graph_enter(old, self_addr, frame_pointer, NULL))
  151. *parent = old;
  152. }
  153. #ifdef CONFIG_DYNAMIC_FTRACE
  154. extern unsigned long ftrace_graph_call;
  155. extern unsigned long ftrace_graph_call_old;
  156. extern void ftrace_graph_caller_old(void);
  157. extern unsigned long ftrace_graph_regs_call;
  158. extern void ftrace_graph_regs_caller(void);
  159. static int __ftrace_modify_caller(unsigned long *callsite,
  160. void (*func) (void), bool enable)
  161. {
  162. unsigned long caller_fn = (unsigned long) func;
  163. unsigned long pc = (unsigned long) callsite;
  164. unsigned long branch = arm_gen_branch(pc, caller_fn);
  165. unsigned long nop = 0xe1a00000; /* mov r0, r0 */
  166. unsigned long old = enable ? nop : branch;
  167. unsigned long new = enable ? branch : nop;
  168. return ftrace_modify_code(pc, old, new, true);
  169. }
  170. static int ftrace_modify_graph_caller(bool enable)
  171. {
  172. int ret;
  173. ret = __ftrace_modify_caller(&ftrace_graph_call,
  174. ftrace_graph_caller,
  175. enable);
  176. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  177. if (!ret)
  178. ret = __ftrace_modify_caller(&ftrace_graph_regs_call,
  179. ftrace_graph_regs_caller,
  180. enable);
  181. #endif
  182. return ret;
  183. }
  184. int ftrace_enable_ftrace_graph_caller(void)
  185. {
  186. return ftrace_modify_graph_caller(true);
  187. }
  188. int ftrace_disable_ftrace_graph_caller(void)
  189. {
  190. return ftrace_modify_graph_caller(false);
  191. }
  192. #endif /* CONFIG_DYNAMIC_FTRACE */
  193. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */