1 // SPDX-License-Identifier: GPL-2.0-only
3 * arch/arm64/kernel/ftrace.c
5 * Copyright (C) 2013 Linaro Limited
6 * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
9 #include <linux/ftrace.h>
10 #include <linux/module.h>
11 #include <linux/swab.h>
12 #include <linux/uaccess.h>
14 #include <asm/cacheflush.h>
15 #include <asm/debug-monitors.h>
16 #include <asm/ftrace.h>
19 #ifdef CONFIG_DYNAMIC_FTRACE
21 * Replace a single instruction, which may be a branch or NOP.
22 * If @validate == true, a replaced instruction is checked against 'old'.
24 static int ftrace_modify_code(unsigned long pc, u32 old, u32 new,
31 * We are paranoid about modifying text, as if a bug were to happen, it
32 * could cause us to read or write to someplace that could cause harm.
33 * Carefully read and modify the code with aarch64_insn_*() which uses
34 * probe_kernel_*(), and make sure what we read is what we expected it
35 * to be before modifying it.
38 if (aarch64_insn_read((void *)pc, &replaced))
44 if (aarch64_insn_patch_text_nosync((void *)pc, new))
51 * Replace tracer function in ftrace_caller()
53 int ftrace_update_ftrace_func(ftrace_func_t func)
58 pc = (unsigned long)&ftrace_call;
59 new = aarch64_insn_gen_branch_imm(pc, (unsigned long)func,
60 AARCH64_INSN_BRANCH_LINK);
62 return ftrace_modify_code(pc, 0, new, false);
65 static struct plt_entry *get_ftrace_plt(struct module *mod, unsigned long addr)
67 #ifdef CONFIG_ARM64_MODULE_PLTS
68 struct plt_entry *plt = mod->arch.ftrace_trampolines;
70 if (addr == FTRACE_ADDR)
71 return &plt[FTRACE_PLT_IDX];
72 if (addr == FTRACE_REGS_ADDR &&
73 IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS))
74 return &plt[FTRACE_REGS_PLT_IDX];
80 * Find the address the callsite must branch to in order to reach '*addr'.
82 * Due to the limited range of 'BL' instructions, modules may be placed too far
83 * away to branch directly and must use a PLT.
85 * Returns true when '*addr' contains a reachable target address, or has been
86 * modified to contain a PLT address. Returns false otherwise.
88 static bool ftrace_find_callable_addr(struct dyn_ftrace *rec,
92 unsigned long pc = rec->ip;
93 long offset = (long)*addr - (long)pc;
94 struct plt_entry *plt;
97 * When the target is within range of the 'BL' instruction, use 'addr'
98 * as-is and branch to that directly.
100 if (offset >= -SZ_128M && offset < SZ_128M)
104 * When the target is outside of the range of a 'BL' instruction, we
105 * must use a PLT to reach it. We can only place PLTs for modules, and
106 * only when module PLT support is built-in.
108 if (!IS_ENABLED(CONFIG_ARM64_MODULE_PLTS))
112 * 'mod' is only set at module load time, but if we end up
113 * dealing with an out-of-range condition, we can assume it
114 * is due to a module being loaded far away from the kernel.
116 * NOTE: __module_text_address() must be called with preemption
117 * disabled, but we can rely on ftrace_lock to ensure that 'mod'
118 * retains its validity throughout the remainder of this code.
122 mod = __module_text_address(pc);
129 plt = get_ftrace_plt(mod, *addr);
131 pr_err("ftrace: no module PLT for %ps\n", (void *)*addr);
135 *addr = (unsigned long)plt;
140 * Turn on the call to ftrace_caller() in instrumented function
142 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
144 unsigned long pc = rec->ip;
147 if (!ftrace_find_callable_addr(rec, NULL, &addr))
150 old = aarch64_insn_gen_nop();
151 new = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
153 return ftrace_modify_code(pc, old, new, true);
156 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
157 int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
160 unsigned long pc = rec->ip;
163 if (!ftrace_find_callable_addr(rec, NULL, &old_addr))
165 if (!ftrace_find_callable_addr(rec, NULL, &addr))
168 old = aarch64_insn_gen_branch_imm(pc, old_addr,
169 AARCH64_INSN_BRANCH_LINK);
170 new = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
172 return ftrace_modify_code(pc, old, new, true);
176 * The compiler has inserted two NOPs before the regular function prologue.
177 * All instrumented functions follow the AAPCS, so x0-x8 and x19-x30 are live,
178 * and x9-x18 are free for our use.
180 * At runtime we want to be able to swing a single NOP <-> BL to enable or
181 * disable the ftrace call. The BL requires us to save the original LR value,
182 * so here we insert a <MOV X9, LR> over the first NOP so the instructions
183 * before the regular prologue are:
185 * | Compiled | Disabled | Enabled |
186 * +----------+------------+------------+
187 * | NOP | MOV X9, LR | MOV X9, LR |
188 * | NOP | NOP | BL <entry> |
190 * The LR value will be recovered by ftrace_regs_entry, and restored into LR
191 * before returning to the regular function prologue. When a function is not
192 * being traced, the MOV is not harmful given x9 is not live per the AAPCS.
194 * Note: ftrace_process_locs() has pre-adjusted rec->ip to be the address of
197 int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
199 unsigned long pc = rec->ip - AARCH64_INSN_SIZE;
202 old = aarch64_insn_gen_nop();
203 new = aarch64_insn_gen_move_reg(AARCH64_INSN_REG_9,
205 AARCH64_INSN_VARIANT_64BIT);
206 return ftrace_modify_code(pc, old, new, true);
211 * Turn off the call to ftrace_caller() in instrumented function
213 int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
216 unsigned long pc = rec->ip;
219 new = aarch64_insn_gen_nop();
222 * When using mcount, callsites in modules may have been initalized to
223 * call an arbitrary module PLT (which redirects to the _mcount stub)
224 * rather than the ftrace PLT we'll use at runtime (which redirects to
225 * the ftrace trampoline). We can ignore the old PLT when initializing
228 * Note: 'mod' is only set at module load time.
230 if (!IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS) &&
231 IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) && mod) {
232 return aarch64_insn_patch_text_nosync((void *)pc, new);
235 if (!ftrace_find_callable_addr(rec, mod, &addr))
238 old = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
240 return ftrace_modify_code(pc, old, new, true);
243 void arch_ftrace_update_code(int command)
245 command |= FTRACE_MAY_SLEEP;
246 ftrace_modify_all_code(command);
249 int __init ftrace_dyn_arch_init(void)
253 #endif /* CONFIG_DYNAMIC_FTRACE */
255 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
257 * function_graph tracer expects ftrace_return_to_handler() to be called
258 * on the way back to parent. For this purpose, this function is called
259 * in _mcount() or ftrace_caller() to replace return address (*parent) on
260 * the call stack to return_to_handler.
262 * Note that @frame_pointer is used only for sanity check later.
264 void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent,
265 unsigned long frame_pointer)
267 unsigned long return_hooker = (unsigned long)&return_to_handler;
270 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
275 * No protection against faulting at *parent, which may be seen
276 * on other archs. It's unlikely on AArch64.
280 if (!function_graph_enter(old, self_addr, frame_pointer, NULL))
281 *parent = return_hooker;
284 #ifdef CONFIG_DYNAMIC_FTRACE
286 * Turn on/off the call to ftrace_graph_caller() in ftrace_caller()
287 * depending on @enable.
289 static int ftrace_modify_graph_caller(bool enable)
291 unsigned long pc = (unsigned long)&ftrace_graph_call;
294 branch = aarch64_insn_gen_branch_imm(pc,
295 (unsigned long)ftrace_graph_caller,
296 AARCH64_INSN_BRANCH_NOLINK);
297 nop = aarch64_insn_gen_nop();
300 return ftrace_modify_code(pc, nop, branch, true);
302 return ftrace_modify_code(pc, branch, nop, true);
305 int ftrace_enable_ftrace_graph_caller(void)
307 return ftrace_modify_graph_caller(true);
310 int ftrace_disable_ftrace_graph_caller(void)
312 return ftrace_modify_graph_caller(false);
314 #endif /* CONFIG_DYNAMIC_FTRACE */
315 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */