ftrace.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2013 Linaro Limited
  4. * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
  5. * Copyright (C) 2017 Andes Technology Corporation
  6. */
  7. #include <linux/ftrace.h>
  8. #include <linux/uaccess.h>
  9. #include <linux/memory.h>
  10. #include <asm/cacheflush.h>
  11. #include <asm/patch.h>
  12. #ifdef CONFIG_DYNAMIC_FTRACE
  13. int ftrace_arch_code_modify_prepare(void) __acquires(&text_mutex)
  14. {
  15. mutex_lock(&text_mutex);
  16. return 0;
  17. }
  18. int ftrace_arch_code_modify_post_process(void) __releases(&text_mutex)
  19. {
  20. mutex_unlock(&text_mutex);
  21. return 0;
  22. }
  23. static int ftrace_check_current_call(unsigned long hook_pos,
  24. unsigned int *expected)
  25. {
  26. unsigned int replaced[2];
  27. unsigned int nops[2] = {NOP4, NOP4};
  28. /* we expect nops at the hook position */
  29. if (!expected)
  30. expected = nops;
  31. /*
  32. * Read the text we want to modify;
  33. * return must be -EFAULT on read error
  34. */
  35. if (copy_from_kernel_nofault(replaced, (void *)hook_pos,
  36. MCOUNT_INSN_SIZE))
  37. return -EFAULT;
  38. /*
  39. * Make sure it is what we expect it to be;
  40. * return must be -EINVAL on failed comparison
  41. */
  42. if (memcmp(expected, replaced, sizeof(replaced))) {
  43. pr_err("%p: expected (%08x %08x) but got (%08x %08x)\n",
  44. (void *)hook_pos, expected[0], expected[1], replaced[0],
  45. replaced[1]);
  46. return -EINVAL;
  47. }
  48. return 0;
  49. }
  50. static int __ftrace_modify_call(unsigned long hook_pos, unsigned long target,
  51. bool enable)
  52. {
  53. unsigned int call[2];
  54. unsigned int nops[2] = {NOP4, NOP4};
  55. make_call(hook_pos, target, call);
  56. /* Replace the auipc-jalr pair at once. Return -EPERM on write error. */
  57. if (patch_text_nosync
  58. ((void *)hook_pos, enable ? call : nops, MCOUNT_INSN_SIZE))
  59. return -EPERM;
  60. return 0;
  61. }
  62. /*
  63. * Put 5 instructions with 16 bytes at the front of function within
  64. * patchable function entry nops' area.
  65. *
  66. * 0: REG_S ra, -SZREG(sp)
  67. * 1: auipc ra, 0x?
  68. * 2: jalr -?(ra)
  69. * 3: REG_L ra, -SZREG(sp)
  70. *
  71. * So the opcodes is:
  72. * 0: 0xfe113c23 (sd)/0xfe112e23 (sw)
  73. * 1: 0x???????? -> auipc
  74. * 2: 0x???????? -> jalr
  75. * 3: 0xff813083 (ld)/0xffc12083 (lw)
  76. */
  77. #if __riscv_xlen == 64
  78. #define INSN0 0xfe113c23
  79. #define INSN3 0xff813083
  80. #elif __riscv_xlen == 32
  81. #define INSN0 0xfe112e23
  82. #define INSN3 0xffc12083
  83. #endif
  84. #define FUNC_ENTRY_SIZE 16
  85. #define FUNC_ENTRY_JMP 4
  86. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  87. {
  88. unsigned int call[4] = {INSN0, 0, 0, INSN3};
  89. unsigned long target = addr;
  90. unsigned long caller = rec->ip + FUNC_ENTRY_JMP;
  91. call[1] = to_auipc_insn((unsigned int)(target - caller));
  92. call[2] = to_jalr_insn((unsigned int)(target - caller));
  93. if (patch_text_nosync((void *)rec->ip, call, FUNC_ENTRY_SIZE))
  94. return -EPERM;
  95. return 0;
  96. }
  97. int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
  98. unsigned long addr)
  99. {
  100. unsigned int nops[4] = {NOP4, NOP4, NOP4, NOP4};
  101. if (patch_text_nosync((void *)rec->ip, nops, FUNC_ENTRY_SIZE))
  102. return -EPERM;
  103. return 0;
  104. }
  105. /*
  106. * This is called early on, and isn't wrapped by
  107. * ftrace_arch_code_modify_{prepare,post_process}() and therefor doesn't hold
  108. * text_mutex, which triggers a lockdep failure. SMP isn't running so we could
  109. * just directly poke the text, but it's simpler to just take the lock
  110. * ourselves.
  111. */
  112. int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
  113. {
  114. int out;
  115. ftrace_arch_code_modify_prepare();
  116. out = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
  117. ftrace_arch_code_modify_post_process();
  118. return out;
  119. }
  120. int ftrace_update_ftrace_func(ftrace_func_t func)
  121. {
  122. int ret = __ftrace_modify_call((unsigned long)&ftrace_call,
  123. (unsigned long)func, true);
  124. if (!ret) {
  125. ret = __ftrace_modify_call((unsigned long)&ftrace_regs_call,
  126. (unsigned long)func, true);
  127. }
  128. return ret;
  129. }
  130. int __init ftrace_dyn_arch_init(void)
  131. {
  132. return 0;
  133. }
  134. #endif
  135. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  136. int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
  137. unsigned long addr)
  138. {
  139. unsigned int call[2];
  140. unsigned long caller = rec->ip + FUNC_ENTRY_JMP;
  141. int ret;
  142. make_call(caller, old_addr, call);
  143. ret = ftrace_check_current_call(caller, call);
  144. if (ret)
  145. return ret;
  146. return __ftrace_modify_call(caller, addr, true);
  147. }
  148. #endif
  149. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  150. /*
  151. * Most of this function is copied from arm64.
  152. */
  153. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
  154. unsigned long frame_pointer)
  155. {
  156. unsigned long return_hooker = (unsigned long)&return_to_handler;
  157. unsigned long old;
  158. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  159. return;
  160. /*
  161. * We don't suffer access faults, so no extra fault-recovery assembly
  162. * is needed here.
  163. */
  164. old = *parent;
  165. if (!function_graph_enter(old, self_addr, frame_pointer, parent))
  166. *parent = return_hooker;
  167. }
  168. #ifdef CONFIG_DYNAMIC_FTRACE
  169. extern void ftrace_graph_call(void);
  170. extern void ftrace_graph_regs_call(void);
  171. int ftrace_enable_ftrace_graph_caller(void)
  172. {
  173. int ret;
  174. ret = __ftrace_modify_call((unsigned long)&ftrace_graph_call,
  175. (unsigned long)&prepare_ftrace_return, true);
  176. if (ret)
  177. return ret;
  178. return __ftrace_modify_call((unsigned long)&ftrace_graph_regs_call,
  179. (unsigned long)&prepare_ftrace_return, true);
  180. }
  181. int ftrace_disable_ftrace_graph_caller(void)
  182. {
  183. int ret;
  184. ret = __ftrace_modify_call((unsigned long)&ftrace_graph_call,
  185. (unsigned long)&prepare_ftrace_return, false);
  186. if (ret)
  187. return ret;
  188. return __ftrace_modify_call((unsigned long)&ftrace_graph_regs_call,
  189. (unsigned long)&prepare_ftrace_return, false);
  190. }
  191. #endif /* CONFIG_DYNAMIC_FTRACE */
  192. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */