ftrace.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * arch/arm64/kernel/ftrace.c
  4. *
  5. * Copyright (C) 2013 Linaro Limited
  6. * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
  7. */
  8. #include <linux/ftrace.h>
  9. #include <linux/module.h>
  10. #include <linux/swab.h>
  11. #include <linux/uaccess.h>
  12. #include <asm/cacheflush.h>
  13. #include <asm/debug-monitors.h>
  14. #include <asm/ftrace.h>
  15. #include <asm/insn.h>
  16. #ifdef CONFIG_DYNAMIC_FTRACE
  17. /*
  18. * Replace a single instruction, which may be a branch or NOP.
  19. * If @validate == true, a replaced instruction is checked against 'old'.
  20. */
  21. static int ftrace_modify_code(unsigned long pc, u32 old, u32 new,
  22. bool validate)
  23. {
  24. u32 replaced;
  25. /*
  26. * Note:
  27. * We are paranoid about modifying text, as if a bug were to happen, it
  28. * could cause us to read or write to someplace that could cause harm.
  29. * Carefully read and modify the code with aarch64_insn_*() which uses
  30. * probe_kernel_*(), and make sure what we read is what we expected it
  31. * to be before modifying it.
  32. */
  33. if (validate) {
  34. if (aarch64_insn_read((void *)pc, &replaced))
  35. return -EFAULT;
  36. if (replaced != old)
  37. return -EINVAL;
  38. }
  39. if (aarch64_insn_patch_text_nosync((void *)pc, new))
  40. return -EPERM;
  41. return 0;
  42. }
  43. /*
  44. * Replace tracer function in ftrace_caller()
  45. */
  46. int ftrace_update_ftrace_func(ftrace_func_t func)
  47. {
  48. unsigned long pc;
  49. u32 new;
  50. pc = (unsigned long)&ftrace_call;
  51. new = aarch64_insn_gen_branch_imm(pc, (unsigned long)func,
  52. AARCH64_INSN_BRANCH_LINK);
  53. return ftrace_modify_code(pc, 0, new, false);
  54. }
  55. static struct plt_entry *get_ftrace_plt(struct module *mod, unsigned long addr)
  56. {
  57. #ifdef CONFIG_ARM64_MODULE_PLTS
  58. struct plt_entry *plt = mod->arch.ftrace_trampolines;
  59. if (addr == FTRACE_ADDR)
  60. return &plt[FTRACE_PLT_IDX];
  61. if (addr == FTRACE_REGS_ADDR &&
  62. IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS))
  63. return &plt[FTRACE_REGS_PLT_IDX];
  64. #endif
  65. return NULL;
  66. }
  67. /*
  68. * Turn on the call to ftrace_caller() in instrumented function
  69. */
  70. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  71. {
  72. unsigned long pc = rec->ip;
  73. u32 old, new;
  74. long offset = (long)pc - (long)addr;
  75. if (offset < -SZ_128M || offset >= SZ_128M) {
  76. struct module *mod;
  77. struct plt_entry *plt;
  78. if (!IS_ENABLED(CONFIG_ARM64_MODULE_PLTS))
  79. return -EINVAL;
  80. /*
  81. * On kernels that support module PLTs, the offset between the
  82. * branch instruction and its target may legally exceed the
  83. * range of an ordinary relative 'bl' opcode. In this case, we
  84. * need to branch via a trampoline in the module.
  85. *
  86. * NOTE: __module_text_address() must be called with preemption
  87. * disabled, but we can rely on ftrace_lock to ensure that 'mod'
  88. * retains its validity throughout the remainder of this code.
  89. */
  90. preempt_disable();
  91. mod = __module_text_address(pc);
  92. preempt_enable();
  93. if (WARN_ON(!mod))
  94. return -EINVAL;
  95. plt = get_ftrace_plt(mod, addr);
  96. if (!plt) {
  97. pr_err("ftrace: no module PLT for %ps\n", (void *)addr);
  98. return -EINVAL;
  99. }
  100. addr = (unsigned long)plt;
  101. }
  102. old = aarch64_insn_gen_nop();
  103. new = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
  104. return ftrace_modify_code(pc, old, new, true);
  105. }
  106. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  107. int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
  108. unsigned long addr)
  109. {
  110. unsigned long pc = rec->ip;
  111. u32 old, new;
  112. old = aarch64_insn_gen_branch_imm(pc, old_addr,
  113. AARCH64_INSN_BRANCH_LINK);
  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. * The compiler has inserted two NOPs before the regular function prologue.
  119. * All instrumented functions follow the AAPCS, so x0-x8 and x19-x30 are live,
  120. * and x9-x18 are free for our use.
  121. *
  122. * At runtime we want to be able to swing a single NOP <-> BL to enable or
  123. * disable the ftrace call. The BL requires us to save the original LR value,
  124. * so here we insert a <MOV X9, LR> over the first NOP so the instructions
  125. * before the regular prologue are:
  126. *
  127. * | Compiled | Disabled | Enabled |
  128. * +----------+------------+------------+
  129. * | NOP | MOV X9, LR | MOV X9, LR |
  130. * | NOP | NOP | BL <entry> |
  131. *
  132. * The LR value will be recovered by ftrace_regs_entry, and restored into LR
  133. * before returning to the regular function prologue. When a function is not
  134. * being traced, the MOV is not harmful given x9 is not live per the AAPCS.
  135. *
  136. * Note: ftrace_process_locs() has pre-adjusted rec->ip to be the address of
  137. * the BL.
  138. */
  139. int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
  140. {
  141. unsigned long pc = rec->ip - AARCH64_INSN_SIZE;
  142. u32 old, new;
  143. old = aarch64_insn_gen_nop();
  144. new = aarch64_insn_gen_move_reg(AARCH64_INSN_REG_9,
  145. AARCH64_INSN_REG_LR,
  146. AARCH64_INSN_VARIANT_64BIT);
  147. return ftrace_modify_code(pc, old, new, true);
  148. }
  149. #endif
  150. /*
  151. * Turn off the call to ftrace_caller() in instrumented function
  152. */
  153. int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
  154. unsigned long addr)
  155. {
  156. unsigned long pc = rec->ip;
  157. bool validate = true;
  158. u32 old = 0, new;
  159. long offset = (long)pc - (long)addr;
  160. if (offset < -SZ_128M || offset >= SZ_128M) {
  161. u32 replaced;
  162. if (!IS_ENABLED(CONFIG_ARM64_MODULE_PLTS))
  163. return -EINVAL;
  164. /*
  165. * 'mod' is only set at module load time, but if we end up
  166. * dealing with an out-of-range condition, we can assume it
  167. * is due to a module being loaded far away from the kernel.
  168. */
  169. if (!mod) {
  170. preempt_disable();
  171. mod = __module_text_address(pc);
  172. preempt_enable();
  173. if (WARN_ON(!mod))
  174. return -EINVAL;
  175. }
  176. /*
  177. * The instruction we are about to patch may be a branch and
  178. * link instruction that was redirected via a PLT entry. In
  179. * this case, the normal validation will fail, but we can at
  180. * least check that we are dealing with a branch and link
  181. * instruction that points into the right module.
  182. */
  183. if (aarch64_insn_read((void *)pc, &replaced))
  184. return -EFAULT;
  185. if (!aarch64_insn_is_bl(replaced) ||
  186. !within_module(pc + aarch64_get_branch_offset(replaced),
  187. mod))
  188. return -EINVAL;
  189. validate = false;
  190. } else {
  191. old = aarch64_insn_gen_branch_imm(pc, addr,
  192. AARCH64_INSN_BRANCH_LINK);
  193. }
  194. new = aarch64_insn_gen_nop();
  195. return ftrace_modify_code(pc, old, new, validate);
  196. }
  197. void arch_ftrace_update_code(int command)
  198. {
  199. command |= FTRACE_MAY_SLEEP;
  200. ftrace_modify_all_code(command);
  201. }
  202. int __init ftrace_dyn_arch_init(void)
  203. {
  204. return 0;
  205. }
  206. #endif /* CONFIG_DYNAMIC_FTRACE */
  207. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  208. /*
  209. * function_graph tracer expects ftrace_return_to_handler() to be called
  210. * on the way back to parent. For this purpose, this function is called
  211. * in _mcount() or ftrace_caller() to replace return address (*parent) on
  212. * the call stack to return_to_handler.
  213. *
  214. * Note that @frame_pointer is used only for sanity check later.
  215. */
  216. void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent,
  217. unsigned long frame_pointer)
  218. {
  219. unsigned long return_hooker = (unsigned long)&return_to_handler;
  220. unsigned long old;
  221. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  222. return;
  223. /*
  224. * Note:
  225. * No protection against faulting at *parent, which may be seen
  226. * on other archs. It's unlikely on AArch64.
  227. */
  228. old = *parent;
  229. if (!function_graph_enter(old, self_addr, frame_pointer, NULL))
  230. *parent = return_hooker;
  231. }
  232. #ifdef CONFIG_DYNAMIC_FTRACE
  233. /*
  234. * Turn on/off the call to ftrace_graph_caller() in ftrace_caller()
  235. * depending on @enable.
  236. */
  237. static int ftrace_modify_graph_caller(bool enable)
  238. {
  239. unsigned long pc = (unsigned long)&ftrace_graph_call;
  240. u32 branch, nop;
  241. branch = aarch64_insn_gen_branch_imm(pc,
  242. (unsigned long)ftrace_graph_caller,
  243. AARCH64_INSN_BRANCH_NOLINK);
  244. nop = aarch64_insn_gen_nop();
  245. if (enable)
  246. return ftrace_modify_code(pc, nop, branch, true);
  247. else
  248. return ftrace_modify_code(pc, branch, nop, true);
  249. }
  250. int ftrace_enable_ftrace_graph_caller(void)
  251. {
  252. return ftrace_modify_graph_caller(true);
  253. }
  254. int ftrace_disable_ftrace_graph_caller(void)
  255. {
  256. return ftrace_modify_graph_caller(false);
  257. }
  258. #endif /* CONFIG_DYNAMIC_FTRACE */
  259. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */