ftrace.c 7.1 KB

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