ftrace.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. #ifdef CONFIG_OLD_MCOUNT
  42. #define OLD_MCOUNT_ADDR ((unsigned long) mcount)
  43. #define OLD_FTRACE_ADDR ((unsigned long) ftrace_caller_old)
  44. #define OLD_NOP 0xe1a00000 /* mov r0, r0 */
  45. static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec)
  46. {
  47. return rec->arch.old_mcount ? OLD_NOP : NOP;
  48. }
  49. static unsigned long adjust_address(struct dyn_ftrace *rec, unsigned long addr)
  50. {
  51. if (!rec->arch.old_mcount)
  52. return addr;
  53. if (addr == MCOUNT_ADDR)
  54. addr = OLD_MCOUNT_ADDR;
  55. else if (addr == FTRACE_ADDR)
  56. addr = OLD_FTRACE_ADDR;
  57. return addr;
  58. }
  59. #else
  60. static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec)
  61. {
  62. return NOP;
  63. }
  64. static unsigned long adjust_address(struct dyn_ftrace *rec, unsigned long addr)
  65. {
  66. return addr;
  67. }
  68. #endif
  69. int ftrace_arch_code_modify_prepare(void)
  70. {
  71. set_all_modules_text_rw();
  72. return 0;
  73. }
  74. int ftrace_arch_code_modify_post_process(void)
  75. {
  76. set_all_modules_text_ro();
  77. /* Make sure any TLB misses during machine stop are cleared. */
  78. flush_tlb_all();
  79. return 0;
  80. }
  81. static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr)
  82. {
  83. return arm_gen_branch_link(pc, addr);
  84. }
  85. static int ftrace_modify_code(unsigned long pc, unsigned long old,
  86. unsigned long new, bool validate)
  87. {
  88. unsigned long replaced;
  89. if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) {
  90. old = __opcode_to_mem_thumb32(old);
  91. new = __opcode_to_mem_thumb32(new);
  92. } else {
  93. old = __opcode_to_mem_arm(old);
  94. new = __opcode_to_mem_arm(new);
  95. }
  96. if (validate) {
  97. if (probe_kernel_read(&replaced, (void *)pc, MCOUNT_INSN_SIZE))
  98. return -EFAULT;
  99. if (replaced != old)
  100. return -EINVAL;
  101. }
  102. if (probe_kernel_write((void *)pc, &new, MCOUNT_INSN_SIZE))
  103. return -EPERM;
  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;
  110. unsigned long new;
  111. int ret;
  112. pc = (unsigned long)&ftrace_call;
  113. new = ftrace_call_replace(pc, (unsigned long)func);
  114. ret = ftrace_modify_code(pc, 0, new, false);
  115. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  116. if (!ret) {
  117. pc = (unsigned long)&ftrace_regs_call;
  118. new = ftrace_call_replace(pc, (unsigned long)func);
  119. ret = ftrace_modify_code(pc, 0, new, false);
  120. }
  121. #endif
  122. #ifdef CONFIG_OLD_MCOUNT
  123. if (!ret) {
  124. pc = (unsigned long)&ftrace_call_old;
  125. new = ftrace_call_replace(pc, (unsigned long)func);
  126. ret = ftrace_modify_code(pc, 0, new, false);
  127. }
  128. #endif
  129. return ret;
  130. }
  131. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  132. {
  133. unsigned long new, old;
  134. unsigned long ip = rec->ip;
  135. old = ftrace_nop_replace(rec);
  136. new = ftrace_call_replace(ip, adjust_address(rec, addr));
  137. return ftrace_modify_code(rec->ip, old, new, true);
  138. }
  139. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  140. int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
  141. unsigned long addr)
  142. {
  143. unsigned long new, old;
  144. unsigned long ip = rec->ip;
  145. old = ftrace_call_replace(ip, adjust_address(rec, old_addr));
  146. new = ftrace_call_replace(ip, adjust_address(rec, addr));
  147. return ftrace_modify_code(rec->ip, old, new, true);
  148. }
  149. #endif
  150. int ftrace_make_nop(struct module *mod,
  151. struct dyn_ftrace *rec, unsigned long addr)
  152. {
  153. unsigned long ip = rec->ip;
  154. unsigned long old;
  155. unsigned long new;
  156. int ret;
  157. old = ftrace_call_replace(ip, adjust_address(rec, addr));
  158. new = ftrace_nop_replace(rec);
  159. ret = ftrace_modify_code(ip, old, new, true);
  160. #ifdef CONFIG_OLD_MCOUNT
  161. if (ret == -EINVAL && addr == MCOUNT_ADDR) {
  162. rec->arch.old_mcount = true;
  163. old = ftrace_call_replace(ip, adjust_address(rec, addr));
  164. new = ftrace_nop_replace(rec);
  165. ret = ftrace_modify_code(ip, old, new, true);
  166. }
  167. #endif
  168. return ret;
  169. }
  170. int __init ftrace_dyn_arch_init(void)
  171. {
  172. return 0;
  173. }
  174. #endif /* CONFIG_DYNAMIC_FTRACE */
  175. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  176. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
  177. unsigned long frame_pointer)
  178. {
  179. unsigned long return_hooker = (unsigned long) &return_to_handler;
  180. unsigned long old;
  181. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  182. return;
  183. old = *parent;
  184. *parent = return_hooker;
  185. if (function_graph_enter(old, self_addr, frame_pointer, NULL))
  186. *parent = old;
  187. }
  188. #ifdef CONFIG_DYNAMIC_FTRACE
  189. extern unsigned long ftrace_graph_call;
  190. extern unsigned long ftrace_graph_call_old;
  191. extern void ftrace_graph_caller_old(void);
  192. extern unsigned long ftrace_graph_regs_call;
  193. extern void ftrace_graph_regs_caller(void);
  194. static int __ftrace_modify_caller(unsigned long *callsite,
  195. void (*func) (void), bool enable)
  196. {
  197. unsigned long caller_fn = (unsigned long) func;
  198. unsigned long pc = (unsigned long) callsite;
  199. unsigned long branch = arm_gen_branch(pc, caller_fn);
  200. unsigned long nop = 0xe1a00000; /* mov r0, r0 */
  201. unsigned long old = enable ? nop : branch;
  202. unsigned long new = enable ? branch : nop;
  203. return ftrace_modify_code(pc, old, new, true);
  204. }
  205. static int ftrace_modify_graph_caller(bool enable)
  206. {
  207. int ret;
  208. ret = __ftrace_modify_caller(&ftrace_graph_call,
  209. ftrace_graph_caller,
  210. enable);
  211. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  212. if (!ret)
  213. ret = __ftrace_modify_caller(&ftrace_graph_regs_call,
  214. ftrace_graph_regs_caller,
  215. enable);
  216. #endif
  217. #ifdef CONFIG_OLD_MCOUNT
  218. if (!ret)
  219. ret = __ftrace_modify_caller(&ftrace_graph_call_old,
  220. ftrace_graph_caller_old,
  221. enable);
  222. #endif
  223. return ret;
  224. }
  225. int ftrace_enable_ftrace_graph_caller(void)
  226. {
  227. return ftrace_modify_graph_caller(true);
  228. }
  229. int ftrace_disable_ftrace_graph_caller(void)
  230. {
  231. return ftrace_modify_graph_caller(false);
  232. }
  233. #endif /* CONFIG_DYNAMIC_FTRACE */
  234. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */