ftrace.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * arch/arm64/kernel/ftrace.c
  3. *
  4. * Copyright (C) 2013 Linaro Limited
  5. * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/ftrace.h>
  12. #include <linux/module.h>
  13. #include <linux/swab.h>
  14. #include <linux/uaccess.h>
  15. #include <asm/cacheflush.h>
  16. #include <asm/debug-monitors.h>
  17. #include <asm/ftrace.h>
  18. #include <asm/insn.h>
  19. #ifdef CONFIG_DYNAMIC_FTRACE
  20. /*
  21. * Replace a single instruction, which may be a branch or NOP.
  22. * If @validate == true, a replaced instruction is checked against 'old'.
  23. */
  24. static int ftrace_modify_code(unsigned long pc, u32 old, u32 new,
  25. bool validate)
  26. {
  27. u32 replaced;
  28. /*
  29. * Note:
  30. * We are paranoid about modifying text, as if a bug were to happen, it
  31. * could cause us to read or write to someplace that could cause harm.
  32. * Carefully read and modify the code with aarch64_insn_*() which uses
  33. * probe_kernel_*(), and make sure what we read is what we expected it
  34. * to be before modifying it.
  35. */
  36. if (validate) {
  37. if (aarch64_insn_read((void *)pc, &replaced))
  38. return -EFAULT;
  39. if (replaced != old)
  40. return -EINVAL;
  41. }
  42. if (aarch64_insn_patch_text_nosync((void *)pc, new))
  43. return -EPERM;
  44. return 0;
  45. }
  46. /*
  47. * Replace tracer function in ftrace_caller()
  48. */
  49. int ftrace_update_ftrace_func(ftrace_func_t func)
  50. {
  51. unsigned long pc;
  52. u32 new;
  53. pc = (unsigned long)&ftrace_call;
  54. new = aarch64_insn_gen_branch_imm(pc, (unsigned long)func,
  55. AARCH64_INSN_BRANCH_LINK);
  56. return ftrace_modify_code(pc, 0, new, false);
  57. }
  58. /*
  59. * Turn on the call to ftrace_caller() in instrumented function
  60. */
  61. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  62. {
  63. unsigned long pc = rec->ip;
  64. u32 old, new;
  65. long offset = (long)pc - (long)addr;
  66. if (offset < -SZ_128M || offset >= SZ_128M) {
  67. #ifdef CONFIG_ARM64_MODULE_PLTS
  68. struct plt_entry trampoline, *dst;
  69. struct module *mod;
  70. /*
  71. * On kernels that support module PLTs, the offset between the
  72. * branch instruction and its target may legally exceed the
  73. * range of an ordinary relative 'bl' opcode. In this case, we
  74. * need to branch via a trampoline in the module.
  75. *
  76. * NOTE: __module_text_address() must be called with preemption
  77. * disabled, but we can rely on ftrace_lock to ensure that 'mod'
  78. * retains its validity throughout the remainder of this code.
  79. */
  80. preempt_disable();
  81. mod = __module_text_address(pc);
  82. preempt_enable();
  83. if (WARN_ON(!mod))
  84. return -EINVAL;
  85. /*
  86. * There is only one ftrace trampoline per module. For now,
  87. * this is not a problem since on arm64, all dynamic ftrace
  88. * invocations are routed via ftrace_caller(). This will need
  89. * to be revisited if support for multiple ftrace entry points
  90. * is added in the future, but for now, the pr_err() below
  91. * deals with a theoretical issue only.
  92. */
  93. dst = mod->arch.ftrace_trampoline;
  94. trampoline = get_plt_entry(addr);
  95. if (!plt_entries_equal(dst, &trampoline)) {
  96. if (!plt_entries_equal(dst, &(struct plt_entry){})) {
  97. pr_err("ftrace: far branches to multiple entry points unsupported inside a single module\n");
  98. return -EINVAL;
  99. }
  100. /* point the trampoline to our ftrace entry point */
  101. module_disable_ro(mod);
  102. *dst = trampoline;
  103. module_enable_ro(mod, true);
  104. /*
  105. * Ensure updated trampoline is visible to instruction
  106. * fetch before we patch in the branch. Although the
  107. * architecture doesn't require an IPI in this case,
  108. * Neoverse-N1 erratum #1542419 does require one
  109. * if the TLB maintenance in module_enable_ro() is
  110. * skipped due to rodata_enabled. It doesn't seem worth
  111. * it to make it conditional given that this is
  112. * certainly not a fast-path.
  113. */
  114. flush_icache_range((unsigned long)&dst[0],
  115. (unsigned long)&dst[1]);
  116. }
  117. addr = (unsigned long)dst;
  118. #else /* CONFIG_ARM64_MODULE_PLTS */
  119. return -EINVAL;
  120. #endif /* CONFIG_ARM64_MODULE_PLTS */
  121. }
  122. old = aarch64_insn_gen_nop();
  123. new = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
  124. return ftrace_modify_code(pc, old, new, true);
  125. }
  126. /*
  127. * Turn off the call to ftrace_caller() in instrumented function
  128. */
  129. int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
  130. unsigned long addr)
  131. {
  132. unsigned long pc = rec->ip;
  133. bool validate = true;
  134. u32 old = 0, new;
  135. long offset = (long)pc - (long)addr;
  136. if (offset < -SZ_128M || offset >= SZ_128M) {
  137. #ifdef CONFIG_ARM64_MODULE_PLTS
  138. u32 replaced;
  139. /*
  140. * 'mod' is only set at module load time, but if we end up
  141. * dealing with an out-of-range condition, we can assume it
  142. * is due to a module being loaded far away from the kernel.
  143. */
  144. if (!mod) {
  145. preempt_disable();
  146. mod = __module_text_address(pc);
  147. preempt_enable();
  148. if (WARN_ON(!mod))
  149. return -EINVAL;
  150. }
  151. /*
  152. * The instruction we are about to patch may be a branch and
  153. * link instruction that was redirected via a PLT entry. In
  154. * this case, the normal validation will fail, but we can at
  155. * least check that we are dealing with a branch and link
  156. * instruction that points into the right module.
  157. */
  158. if (aarch64_insn_read((void *)pc, &replaced))
  159. return -EFAULT;
  160. if (!aarch64_insn_is_bl(replaced) ||
  161. !within_module(pc + aarch64_get_branch_offset(replaced),
  162. mod))
  163. return -EINVAL;
  164. validate = false;
  165. #else /* CONFIG_ARM64_MODULE_PLTS */
  166. return -EINVAL;
  167. #endif /* CONFIG_ARM64_MODULE_PLTS */
  168. } else {
  169. old = aarch64_insn_gen_branch_imm(pc, addr,
  170. AARCH64_INSN_BRANCH_LINK);
  171. }
  172. new = aarch64_insn_gen_nop();
  173. return ftrace_modify_code(pc, old, new, validate);
  174. }
  175. void arch_ftrace_update_code(int command)
  176. {
  177. ftrace_modify_all_code(command);
  178. }
  179. int __init ftrace_dyn_arch_init(void)
  180. {
  181. return 0;
  182. }
  183. #endif /* CONFIG_DYNAMIC_FTRACE */
  184. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  185. /*
  186. * function_graph tracer expects ftrace_return_to_handler() to be called
  187. * on the way back to parent. For this purpose, this function is called
  188. * in _mcount() or ftrace_caller() to replace return address (*parent) on
  189. * the call stack to return_to_handler.
  190. *
  191. * Note that @frame_pointer is used only for sanity check later.
  192. */
  193. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
  194. unsigned long frame_pointer)
  195. {
  196. unsigned long return_hooker = (unsigned long)&return_to_handler;
  197. unsigned long old;
  198. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  199. return;
  200. /*
  201. * Note:
  202. * No protection against faulting at *parent, which may be seen
  203. * on other archs. It's unlikely on AArch64.
  204. */
  205. old = *parent;
  206. if (!function_graph_enter(old, self_addr, frame_pointer, NULL))
  207. *parent = return_hooker;
  208. }
  209. #ifdef CONFIG_DYNAMIC_FTRACE
  210. /*
  211. * Turn on/off the call to ftrace_graph_caller() in ftrace_caller()
  212. * depending on @enable.
  213. */
  214. static int ftrace_modify_graph_caller(bool enable)
  215. {
  216. unsigned long pc = (unsigned long)&ftrace_graph_call;
  217. u32 branch, nop;
  218. branch = aarch64_insn_gen_branch_imm(pc,
  219. (unsigned long)ftrace_graph_caller,
  220. AARCH64_INSN_BRANCH_NOLINK);
  221. nop = aarch64_insn_gen_nop();
  222. if (enable)
  223. return ftrace_modify_code(pc, nop, branch, true);
  224. else
  225. return ftrace_modify_code(pc, branch, nop, true);
  226. }
  227. int ftrace_enable_ftrace_graph_caller(void)
  228. {
  229. return ftrace_modify_graph_caller(true);
  230. }
  231. int ftrace_disable_ftrace_graph_caller(void)
  232. {
  233. return ftrace_modify_graph_caller(false);
  234. }
  235. #endif /* CONFIG_DYNAMIC_FTRACE */
  236. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */