ftrace.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. #include <asm/patching.h>
  17. #ifdef CONFIG_DYNAMIC_FTRACE
  18. /*
  19. * Replace a single instruction, which may be a branch or NOP.
  20. * If @validate == true, a replaced instruction is checked against 'old'.
  21. */
  22. static int ftrace_modify_code(unsigned long pc, u32 old, u32 new,
  23. bool validate)
  24. {
  25. u32 replaced;
  26. /*
  27. * Note:
  28. * We are paranoid about modifying text, as if a bug were to happen, it
  29. * could cause us to read or write to someplace that could cause harm.
  30. * Carefully read and modify the code with aarch64_insn_*() which uses
  31. * probe_kernel_*(), and make sure what we read is what we expected it
  32. * to be before modifying it.
  33. */
  34. if (validate) {
  35. if (aarch64_insn_read((void *)pc, &replaced))
  36. return -EFAULT;
  37. if (replaced != old)
  38. return -EINVAL;
  39. }
  40. if (aarch64_insn_patch_text_nosync((void *)pc, new))
  41. return -EPERM;
  42. return 0;
  43. }
  44. /*
  45. * Replace tracer function in ftrace_caller()
  46. */
  47. int ftrace_update_ftrace_func(ftrace_func_t func)
  48. {
  49. unsigned long pc;
  50. u32 new;
  51. pc = (unsigned long)ftrace_call;
  52. new = aarch64_insn_gen_branch_imm(pc, (unsigned long)func,
  53. AARCH64_INSN_BRANCH_LINK);
  54. return ftrace_modify_code(pc, 0, new, false);
  55. }
  56. static struct plt_entry *get_ftrace_plt(struct module *mod, unsigned long addr)
  57. {
  58. #ifdef CONFIG_ARM64_MODULE_PLTS
  59. struct plt_entry *plt = mod->arch.ftrace_trampolines;
  60. if (addr == FTRACE_ADDR)
  61. return &plt[FTRACE_PLT_IDX];
  62. if (addr == FTRACE_REGS_ADDR &&
  63. IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS))
  64. return &plt[FTRACE_REGS_PLT_IDX];
  65. #endif
  66. return NULL;
  67. }
  68. /*
  69. * Find the address the callsite must branch to in order to reach '*addr'.
  70. *
  71. * Due to the limited range of 'BL' instructions, modules may be placed too far
  72. * away to branch directly and must use a PLT.
  73. *
  74. * Returns true when '*addr' contains a reachable target address, or has been
  75. * modified to contain a PLT address. Returns false otherwise.
  76. */
  77. static bool ftrace_find_callable_addr(struct dyn_ftrace *rec,
  78. struct module *mod,
  79. unsigned long *addr)
  80. {
  81. unsigned long pc = rec->ip;
  82. long offset = (long)*addr - (long)pc;
  83. struct plt_entry *plt;
  84. /*
  85. * When the target is within range of the 'BL' instruction, use 'addr'
  86. * as-is and branch to that directly.
  87. */
  88. if (offset >= -SZ_128M && offset < SZ_128M)
  89. return true;
  90. /*
  91. * When the target is outside of the range of a 'BL' instruction, we
  92. * must use a PLT to reach it. We can only place PLTs for modules, and
  93. * only when module PLT support is built-in.
  94. */
  95. if (!IS_ENABLED(CONFIG_ARM64_MODULE_PLTS))
  96. return false;
  97. /*
  98. * 'mod' is only set at module load time, but if we end up
  99. * dealing with an out-of-range condition, we can assume it
  100. * is due to a module being loaded far away from the kernel.
  101. *
  102. * NOTE: __module_text_address() must be called with preemption
  103. * disabled, but we can rely on ftrace_lock to ensure that 'mod'
  104. * retains its validity throughout the remainder of this code.
  105. */
  106. if (!mod) {
  107. preempt_disable();
  108. mod = __module_text_address(pc);
  109. preempt_enable();
  110. }
  111. if (WARN_ON(!mod))
  112. return false;
  113. plt = get_ftrace_plt(mod, *addr);
  114. if (!plt) {
  115. pr_err("ftrace: no module PLT for %ps\n", (void *)*addr);
  116. return false;
  117. }
  118. *addr = (unsigned long)plt;
  119. return true;
  120. }
  121. /*
  122. * Turn on the call to ftrace_caller() in instrumented function
  123. */
  124. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  125. {
  126. unsigned long pc = rec->ip;
  127. u32 old, new;
  128. if (!ftrace_find_callable_addr(rec, NULL, &addr))
  129. return -EINVAL;
  130. old = aarch64_insn_gen_nop();
  131. new = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
  132. return ftrace_modify_code(pc, old, new, true);
  133. }
  134. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  135. int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
  136. unsigned long addr)
  137. {
  138. unsigned long pc = rec->ip;
  139. u32 old, new;
  140. if (!ftrace_find_callable_addr(rec, NULL, &old_addr))
  141. return -EINVAL;
  142. if (!ftrace_find_callable_addr(rec, NULL, &addr))
  143. return -EINVAL;
  144. old = aarch64_insn_gen_branch_imm(pc, old_addr,
  145. AARCH64_INSN_BRANCH_LINK);
  146. new = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
  147. return ftrace_modify_code(pc, old, new, true);
  148. }
  149. /*
  150. * The compiler has inserted two NOPs before the regular function prologue.
  151. * All instrumented functions follow the AAPCS, so x0-x8 and x19-x30 are live,
  152. * and x9-x18 are free for our use.
  153. *
  154. * At runtime we want to be able to swing a single NOP <-> BL to enable or
  155. * disable the ftrace call. The BL requires us to save the original LR value,
  156. * so here we insert a <MOV X9, LR> over the first NOP so the instructions
  157. * before the regular prologue are:
  158. *
  159. * | Compiled | Disabled | Enabled |
  160. * +----------+------------+------------+
  161. * | NOP | MOV X9, LR | MOV X9, LR |
  162. * | NOP | NOP | BL <entry> |
  163. *
  164. * The LR value will be recovered by ftrace_regs_entry, and restored into LR
  165. * before returning to the regular function prologue. When a function is not
  166. * being traced, the MOV is not harmful given x9 is not live per the AAPCS.
  167. *
  168. * Note: ftrace_process_locs() has pre-adjusted rec->ip to be the address of
  169. * the BL.
  170. */
  171. int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
  172. {
  173. unsigned long pc = rec->ip - AARCH64_INSN_SIZE;
  174. u32 old, new;
  175. old = aarch64_insn_gen_nop();
  176. new = aarch64_insn_gen_move_reg(AARCH64_INSN_REG_9,
  177. AARCH64_INSN_REG_LR,
  178. AARCH64_INSN_VARIANT_64BIT);
  179. return ftrace_modify_code(pc, old, new, true);
  180. }
  181. #endif
  182. /*
  183. * Turn off the call to ftrace_caller() in instrumented function
  184. */
  185. int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
  186. unsigned long addr)
  187. {
  188. unsigned long pc = rec->ip;
  189. u32 old = 0, new;
  190. new = aarch64_insn_gen_nop();
  191. /*
  192. * When using mcount, callsites in modules may have been initalized to
  193. * call an arbitrary module PLT (which redirects to the _mcount stub)
  194. * rather than the ftrace PLT we'll use at runtime (which redirects to
  195. * the ftrace trampoline). We can ignore the old PLT when initializing
  196. * the callsite.
  197. *
  198. * Note: 'mod' is only set at module load time.
  199. */
  200. if (!IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS) &&
  201. IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) && mod) {
  202. return aarch64_insn_patch_text_nosync((void *)pc, new);
  203. }
  204. if (!ftrace_find_callable_addr(rec, mod, &addr))
  205. return -EINVAL;
  206. old = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
  207. return ftrace_modify_code(pc, old, new, true);
  208. }
  209. void arch_ftrace_update_code(int command)
  210. {
  211. command |= FTRACE_MAY_SLEEP;
  212. ftrace_modify_all_code(command);
  213. }
  214. #endif /* CONFIG_DYNAMIC_FTRACE */
  215. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  216. /*
  217. * function_graph tracer expects ftrace_return_to_handler() to be called
  218. * on the way back to parent. For this purpose, this function is called
  219. * in _mcount() or ftrace_caller() to replace return address (*parent) on
  220. * the call stack to return_to_handler.
  221. */
  222. void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent,
  223. unsigned long frame_pointer)
  224. {
  225. unsigned long return_hooker = (unsigned long)&return_to_handler;
  226. unsigned long old;
  227. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  228. return;
  229. /*
  230. * Note:
  231. * No protection against faulting at *parent, which may be seen
  232. * on other archs. It's unlikely on AArch64.
  233. */
  234. old = *parent;
  235. if (!function_graph_enter(old, self_addr, frame_pointer,
  236. (void *)frame_pointer)) {
  237. *parent = return_hooker;
  238. }
  239. }
  240. #ifdef CONFIG_DYNAMIC_FTRACE
  241. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  242. void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,
  243. struct ftrace_ops *op, struct ftrace_regs *fregs)
  244. {
  245. /*
  246. * When DYNAMIC_FTRACE_WITH_REGS is selected, `fregs` can never be NULL
  247. * and arch_ftrace_get_regs(fregs) will always give a non-NULL pt_regs
  248. * in which we can safely modify the LR.
  249. */
  250. struct pt_regs *regs = arch_ftrace_get_regs(fregs);
  251. unsigned long *parent = (unsigned long *)&procedure_link_pointer(regs);
  252. prepare_ftrace_return(ip, parent, frame_pointer(regs));
  253. }
  254. #else
  255. /*
  256. * Turn on/off the call to ftrace_graph_caller() in ftrace_caller()
  257. * depending on @enable.
  258. */
  259. static int ftrace_modify_graph_caller(bool enable)
  260. {
  261. unsigned long pc = (unsigned long)&ftrace_graph_call;
  262. u32 branch, nop;
  263. branch = aarch64_insn_gen_branch_imm(pc,
  264. (unsigned long)ftrace_graph_caller,
  265. AARCH64_INSN_BRANCH_NOLINK);
  266. nop = aarch64_insn_gen_nop();
  267. if (enable)
  268. return ftrace_modify_code(pc, nop, branch, true);
  269. else
  270. return ftrace_modify_code(pc, branch, nop, true);
  271. }
  272. int ftrace_enable_ftrace_graph_caller(void)
  273. {
  274. return ftrace_modify_graph_caller(true);
  275. }
  276. int ftrace_disable_ftrace_graph_caller(void)
  277. {
  278. return ftrace_modify_graph_caller(false);
  279. }
  280. #endif /* CONFIG_DYNAMIC_FTRACE_WITH_REGS */
  281. #endif /* CONFIG_DYNAMIC_FTRACE */
  282. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */