ftrace.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ftrace.h>
  3. #include <linux/uaccess.h>
  4. #include <asm/cacheflush.h>
  5. #ifndef CONFIG_DYNAMIC_FTRACE
  6. extern void (*ftrace_trace_function)(unsigned long, unsigned long,
  7. struct ftrace_ops*, struct pt_regs*);
  8. extern int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace);
  9. extern void ftrace_graph_caller(void);
  10. noinline void __naked ftrace_stub(unsigned long ip, unsigned long parent_ip,
  11. struct ftrace_ops *op, struct pt_regs *regs)
  12. {
  13. __asm__ (""); /* avoid to optimize as pure function */
  14. }
  15. noinline void _mcount(unsigned long parent_ip)
  16. {
  17. /* save all state by the compiler prologue */
  18. unsigned long ip = (unsigned long)__builtin_return_address(0);
  19. if (ftrace_trace_function != ftrace_stub)
  20. ftrace_trace_function(ip - MCOUNT_INSN_SIZE, parent_ip,
  21. NULL, NULL);
  22. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  23. if (ftrace_graph_return != (trace_func_graph_ret_t)ftrace_stub
  24. || ftrace_graph_entry != ftrace_graph_entry_stub)
  25. ftrace_graph_caller();
  26. #endif
  27. /* restore all state by the compiler epilogue */
  28. }
  29. EXPORT_SYMBOL(_mcount);
  30. #else /* CONFIG_DYNAMIC_FTRACE */
  31. noinline void __naked ftrace_stub(unsigned long ip, unsigned long parent_ip,
  32. struct ftrace_ops *op, struct pt_regs *regs)
  33. {
  34. __asm__ (""); /* avoid to optimize as pure function */
  35. }
  36. noinline void __naked _mcount(unsigned long parent_ip)
  37. {
  38. __asm__ (""); /* avoid to optimize as pure function */
  39. }
  40. EXPORT_SYMBOL(_mcount);
  41. #define XSTR(s) STR(s)
  42. #define STR(s) #s
  43. void _ftrace_caller(unsigned long parent_ip)
  44. {
  45. /* save all state needed by the compiler prologue */
  46. /*
  47. * prepare arguments for real tracing function
  48. * first arg : __builtin_return_address(0) - MCOUNT_INSN_SIZE
  49. * second arg : parent_ip
  50. */
  51. __asm__ __volatile__ (
  52. "move $r1, %0 \n\t"
  53. "addi $r0, %1, #-" XSTR(MCOUNT_INSN_SIZE) "\n\t"
  54. :
  55. : "r" (parent_ip), "r" (__builtin_return_address(0)));
  56. /* a placeholder for the call to a real tracing function */
  57. __asm__ __volatile__ (
  58. "ftrace_call: \n\t"
  59. "nop \n\t"
  60. "nop \n\t"
  61. "nop \n\t");
  62. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  63. /* a placeholder for the call to ftrace_graph_caller */
  64. __asm__ __volatile__ (
  65. "ftrace_graph_call: \n\t"
  66. "nop \n\t"
  67. "nop \n\t"
  68. "nop \n\t");
  69. #endif
  70. /* restore all state needed by the compiler epilogue */
  71. }
  72. int __init ftrace_dyn_arch_init(void)
  73. {
  74. return 0;
  75. }
  76. int ftrace_arch_code_modify_prepare(void)
  77. {
  78. set_all_modules_text_rw();
  79. return 0;
  80. }
  81. int ftrace_arch_code_modify_post_process(void)
  82. {
  83. set_all_modules_text_ro();
  84. return 0;
  85. }
  86. static unsigned long gen_sethi_insn(unsigned long addr)
  87. {
  88. unsigned long opcode = 0x46000000;
  89. unsigned long imm = addr >> 12;
  90. unsigned long rt_num = 0xf << 20;
  91. return ENDIAN_CONVERT(opcode | rt_num | imm);
  92. }
  93. static unsigned long gen_ori_insn(unsigned long addr)
  94. {
  95. unsigned long opcode = 0x58000000;
  96. unsigned long imm = addr & 0x0000fff;
  97. unsigned long rt_num = 0xf << 20;
  98. unsigned long ra_num = 0xf << 15;
  99. return ENDIAN_CONVERT(opcode | rt_num | ra_num | imm);
  100. }
  101. static unsigned long gen_jral_insn(unsigned long addr)
  102. {
  103. unsigned long opcode = 0x4a000001;
  104. unsigned long rt_num = 0x1e << 20;
  105. unsigned long rb_num = 0xf << 10;
  106. return ENDIAN_CONVERT(opcode | rt_num | rb_num);
  107. }
  108. static void ftrace_gen_call_insn(unsigned long *call_insns,
  109. unsigned long addr)
  110. {
  111. call_insns[0] = gen_sethi_insn(addr); /* sethi $r15, imm20u */
  112. call_insns[1] = gen_ori_insn(addr); /* ori $r15, $r15, imm15u */
  113. call_insns[2] = gen_jral_insn(addr); /* jral $lp, $r15 */
  114. }
  115. static int __ftrace_modify_code(unsigned long pc, unsigned long *old_insn,
  116. unsigned long *new_insn, bool validate)
  117. {
  118. unsigned long orig_insn[3];
  119. if (validate) {
  120. if (probe_kernel_read(orig_insn, (void *)pc, MCOUNT_INSN_SIZE))
  121. return -EFAULT;
  122. if (memcmp(orig_insn, old_insn, MCOUNT_INSN_SIZE))
  123. return -EINVAL;
  124. }
  125. if (probe_kernel_write((void *)pc, new_insn, MCOUNT_INSN_SIZE))
  126. return -EPERM;
  127. return 0;
  128. }
  129. static int ftrace_modify_code(unsigned long pc, unsigned long *old_insn,
  130. unsigned long *new_insn, bool validate)
  131. {
  132. int ret;
  133. ret = __ftrace_modify_code(pc, old_insn, new_insn, validate);
  134. if (ret)
  135. return ret;
  136. flush_icache_range(pc, pc + MCOUNT_INSN_SIZE);
  137. return ret;
  138. }
  139. int ftrace_update_ftrace_func(ftrace_func_t func)
  140. {
  141. unsigned long pc = (unsigned long)&ftrace_call;
  142. unsigned long old_insn[3] = {INSN_NOP, INSN_NOP, INSN_NOP};
  143. unsigned long new_insn[3] = {INSN_NOP, INSN_NOP, INSN_NOP};
  144. if (func != ftrace_stub)
  145. ftrace_gen_call_insn(new_insn, (unsigned long)func);
  146. return ftrace_modify_code(pc, old_insn, new_insn, false);
  147. }
  148. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  149. {
  150. unsigned long pc = rec->ip;
  151. unsigned long nop_insn[3] = {INSN_NOP, INSN_NOP, INSN_NOP};
  152. unsigned long call_insn[3] = {INSN_NOP, INSN_NOP, INSN_NOP};
  153. ftrace_gen_call_insn(call_insn, addr);
  154. return ftrace_modify_code(pc, nop_insn, call_insn, true);
  155. }
  156. int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
  157. unsigned long addr)
  158. {
  159. unsigned long pc = rec->ip;
  160. unsigned long nop_insn[3] = {INSN_NOP, INSN_NOP, INSN_NOP};
  161. unsigned long call_insn[3] = {INSN_NOP, INSN_NOP, INSN_NOP};
  162. ftrace_gen_call_insn(call_insn, addr);
  163. return ftrace_modify_code(pc, call_insn, nop_insn, true);
  164. }
  165. #endif /* CONFIG_DYNAMIC_FTRACE */
  166. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  167. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
  168. unsigned long frame_pointer)
  169. {
  170. unsigned long return_hooker = (unsigned long)&return_to_handler;
  171. unsigned long old;
  172. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  173. return;
  174. old = *parent;
  175. if (!function_graph_enter(old, self_addr, frame_pointer, NULL))
  176. *parent = return_hooker;
  177. }
  178. noinline void ftrace_graph_caller(void)
  179. {
  180. unsigned long *parent_ip =
  181. (unsigned long *)(__builtin_frame_address(2) - 4);
  182. unsigned long selfpc =
  183. (unsigned long)(__builtin_return_address(1) - MCOUNT_INSN_SIZE);
  184. unsigned long frame_pointer =
  185. (unsigned long)__builtin_frame_address(3);
  186. prepare_ftrace_return(parent_ip, selfpc, frame_pointer);
  187. }
  188. extern unsigned long ftrace_return_to_handler(unsigned long frame_pointer);
  189. void __naked return_to_handler(void)
  190. {
  191. __asm__ __volatile__ (
  192. /* save state needed by the ABI */
  193. "smw.adm $r0,[$sp],$r1,#0x0 \n\t"
  194. /* get original return address */
  195. "move $r0, $fp \n\t"
  196. "bal ftrace_return_to_handler\n\t"
  197. "move $lp, $r0 \n\t"
  198. /* restore state nedded by the ABI */
  199. "lmw.bim $r0,[$sp],$r1,#0x0 \n\t");
  200. }
  201. #ifdef CONFIG_DYNAMIC_FTRACE
  202. extern unsigned long ftrace_graph_call;
  203. static int ftrace_modify_graph_caller(bool enable)
  204. {
  205. unsigned long pc = (unsigned long)&ftrace_graph_call;
  206. unsigned long nop_insn[3] = {INSN_NOP, INSN_NOP, INSN_NOP};
  207. unsigned long call_insn[3] = {INSN_NOP, INSN_NOP, INSN_NOP};
  208. ftrace_gen_call_insn(call_insn, (unsigned long)ftrace_graph_caller);
  209. if (enable)
  210. return ftrace_modify_code(pc, nop_insn, call_insn, true);
  211. else
  212. return ftrace_modify_code(pc, call_insn, nop_insn, true);
  213. }
  214. int ftrace_enable_ftrace_graph_caller(void)
  215. {
  216. return ftrace_modify_graph_caller(true);
  217. }
  218. int ftrace_disable_ftrace_graph_caller(void)
  219. {
  220. return ftrace_modify_graph_caller(false);
  221. }
  222. #endif /* CONFIG_DYNAMIC_FTRACE */
  223. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  224. #ifdef CONFIG_TRACE_IRQFLAGS
  225. noinline void __trace_hardirqs_off(void)
  226. {
  227. trace_hardirqs_off();
  228. }
  229. noinline void __trace_hardirqs_on(void)
  230. {
  231. trace_hardirqs_on();
  232. }
  233. #endif /* CONFIG_TRACE_IRQFLAGS */