ftrace.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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.
  107. */
  108. __flush_icache_range((unsigned long)&dst[0],
  109. (unsigned long)&dst[1]);
  110. }
  111. addr = (unsigned long)dst;
  112. #else /* CONFIG_ARM64_MODULE_PLTS */
  113. return -EINVAL;
  114. #endif /* CONFIG_ARM64_MODULE_PLTS */
  115. }
  116. old = aarch64_insn_gen_nop();
  117. new = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
  118. return ftrace_modify_code(pc, old, new, true);
  119. }
  120. /*
  121. * Turn off the call to ftrace_caller() in instrumented function
  122. */
  123. int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
  124. unsigned long addr)
  125. {
  126. unsigned long pc = rec->ip;
  127. bool validate = true;
  128. u32 old = 0, new;
  129. long offset = (long)pc - (long)addr;
  130. if (offset < -SZ_128M || offset >= SZ_128M) {
  131. #ifdef CONFIG_ARM64_MODULE_PLTS
  132. u32 replaced;
  133. /*
  134. * 'mod' is only set at module load time, but if we end up
  135. * dealing with an out-of-range condition, we can assume it
  136. * is due to a module being loaded far away from the kernel.
  137. */
  138. if (!mod) {
  139. preempt_disable();
  140. mod = __module_text_address(pc);
  141. preempt_enable();
  142. if (WARN_ON(!mod))
  143. return -EINVAL;
  144. }
  145. /*
  146. * The instruction we are about to patch may be a branch and
  147. * link instruction that was redirected via a PLT entry. In
  148. * this case, the normal validation will fail, but we can at
  149. * least check that we are dealing with a branch and link
  150. * instruction that points into the right module.
  151. */
  152. if (aarch64_insn_read((void *)pc, &replaced))
  153. return -EFAULT;
  154. if (!aarch64_insn_is_bl(replaced) ||
  155. !within_module(pc + aarch64_get_branch_offset(replaced),
  156. mod))
  157. return -EINVAL;
  158. validate = false;
  159. #else /* CONFIG_ARM64_MODULE_PLTS */
  160. return -EINVAL;
  161. #endif /* CONFIG_ARM64_MODULE_PLTS */
  162. } else {
  163. old = aarch64_insn_gen_branch_imm(pc, addr,
  164. AARCH64_INSN_BRANCH_LINK);
  165. }
  166. new = aarch64_insn_gen_nop();
  167. return ftrace_modify_code(pc, old, new, validate);
  168. }
  169. void arch_ftrace_update_code(int command)
  170. {
  171. ftrace_modify_all_code(command);
  172. }
  173. int __init ftrace_dyn_arch_init(void)
  174. {
  175. return 0;
  176. }
  177. #endif /* CONFIG_DYNAMIC_FTRACE */
  178. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  179. /*
  180. * function_graph tracer expects ftrace_return_to_handler() to be called
  181. * on the way back to parent. For this purpose, this function is called
  182. * in _mcount() or ftrace_caller() to replace return address (*parent) on
  183. * the call stack to return_to_handler.
  184. *
  185. * Note that @frame_pointer is used only for sanity check later.
  186. */
  187. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
  188. unsigned long frame_pointer)
  189. {
  190. unsigned long return_hooker = (unsigned long)&return_to_handler;
  191. unsigned long old;
  192. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  193. return;
  194. /*
  195. * Note:
  196. * No protection against faulting at *parent, which may be seen
  197. * on other archs. It's unlikely on AArch64.
  198. */
  199. old = *parent;
  200. if (!function_graph_enter(old, self_addr, frame_pointer, NULL))
  201. *parent = return_hooker;
  202. }
  203. #ifdef CONFIG_DYNAMIC_FTRACE
  204. /*
  205. * Turn on/off the call to ftrace_graph_caller() in ftrace_caller()
  206. * depending on @enable.
  207. */
  208. static int ftrace_modify_graph_caller(bool enable)
  209. {
  210. unsigned long pc = (unsigned long)&ftrace_graph_call;
  211. u32 branch, nop;
  212. branch = aarch64_insn_gen_branch_imm(pc,
  213. (unsigned long)ftrace_graph_caller,
  214. AARCH64_INSN_BRANCH_NOLINK);
  215. nop = aarch64_insn_gen_nop();
  216. if (enable)
  217. return ftrace_modify_code(pc, nop, branch, true);
  218. else
  219. return ftrace_modify_code(pc, branch, nop, true);
  220. }
  221. int ftrace_enable_ftrace_graph_caller(void)
  222. {
  223. return ftrace_modify_graph_caller(true);
  224. }
  225. int ftrace_disable_ftrace_graph_caller(void)
  226. {
  227. return ftrace_modify_graph_caller(false);
  228. }
  229. #endif /* CONFIG_DYNAMIC_FTRACE */
  230. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */