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>
18 #include <asm/patching.h>
20 #ifdef CONFIG_DYNAMIC_FTRACE
22 * Replace a single instruction, which may be a branch or NOP.
23 * If @validate == true, a replaced instruction is checked against 'old'.
25 static int ftrace_modify_code(unsigned long pc, u32 old, u32 new,
32 * We are paranoid about modifying text, as if a bug were to happen, it
33 * could cause us to read or write to someplace that could cause harm.
34 * Carefully read and modify the code with aarch64_insn_*() which uses
35 * probe_kernel_*(), and make sure what we read is what we expected it
36 * to be before modifying it.
39 if (aarch64_insn_read((void *)pc, &replaced))
45 if (aarch64_insn_patch_text_nosync((void *)pc, new))
52 * Replace tracer function in ftrace_caller()
54 int ftrace_update_ftrace_func(ftrace_func_t func)
59 pc = (unsigned long)function_nocfi(ftrace_call);
60 new = aarch64_insn_gen_branch_imm(pc, (unsigned long)func,
61 AARCH64_INSN_BRANCH_LINK);
63 return ftrace_modify_code(pc, 0, new, false);
66 static struct plt_entry *get_ftrace_plt(struct module *mod, unsigned long addr)
68 #ifdef CONFIG_ARM64_MODULE_PLTS
69 struct plt_entry *plt = mod->arch.ftrace_trampolines;
71 if (addr == FTRACE_ADDR)
72 return &plt[FTRACE_PLT_IDX];
73 if (addr == FTRACE_REGS_ADDR &&
74 IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS))
75 return &plt[FTRACE_REGS_PLT_IDX];
81 * Find the address the callsite must branch to in order to reach '*addr'.
83 * Due to the limited range of 'BL' instructions, modules may be placed too far
84 * away to branch directly and must use a PLT.
86 * Returns true when '*addr' contains a reachable target address, or has been
87 * modified to contain a PLT address. Returns false otherwise.
89 static bool ftrace_find_callable_addr(struct dyn_ftrace *rec,
93 unsigned long pc = rec->ip;
94 long offset = (long)*addr - (long)pc;
95 struct plt_entry *plt;
98 * When the target is within range of the 'BL' instruction, use 'addr'
99 * as-is and branch to that directly.
101 if (offset >= -SZ_128M && offset < SZ_128M)
105 * When the target is outside of the range of a 'BL' instruction, we
106 * must use a PLT to reach it. We can only place PLTs for modules, and
107 * only when module PLT support is built-in.
109 if (!IS_ENABLED(CONFIG_ARM64_MODULE_PLTS))
113 * 'mod' is only set at module load time, but if we end up
114 * dealing with an out-of-range condition, we can assume it
115 * is due to a module being loaded far away from the kernel.
117 * NOTE: __module_text_address() must be called with preemption
118 * disabled, but we can rely on ftrace_lock to ensure that 'mod'
119 * retains its validity throughout the remainder of this code.
123 mod = __module_text_address(pc);
130 plt = get_ftrace_plt(mod, *addr);
132 pr_err("ftrace: no module PLT for %ps\n", (void *)*addr);
136 *addr = (unsigned long)plt;
141 * Turn on the call to ftrace_caller() in instrumented function
143 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
145 unsigned long pc = rec->ip;
148 if (!ftrace_find_callable_addr(rec, NULL, &addr))
151 old = aarch64_insn_gen_nop();
152 new = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
154 return ftrace_modify_code(pc, old, new, true);
157 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
158 int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
161 unsigned long pc = rec->ip;
164 if (!ftrace_find_callable_addr(rec, NULL, &old_addr))
166 if (!ftrace_find_callable_addr(rec, NULL, &addr))
169 old = aarch64_insn_gen_branch_imm(pc, old_addr,
170 AARCH64_INSN_BRANCH_LINK);
171 new = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
173 return ftrace_modify_code(pc, old, new, true);
177 * The compiler has inserted two NOPs before the regular function prologue.
178 * All instrumented functions follow the AAPCS, so x0-x8 and x19-x30 are live,
179 * and x9-x18 are free for our use.
181 * At runtime we want to be able to swing a single NOP <-> BL to enable or
182 * disable the ftrace call. The BL requires us to save the original LR value,
183 * so here we insert a <MOV X9, LR> over the first NOP so the instructions
184 * before the regular prologue are:
186 * | Compiled | Disabled | Enabled |
187 * +----------+------------+------------+
188 * | NOP | MOV X9, LR | MOV X9, LR |
189 * | NOP | NOP | BL <entry> |
191 * The LR value will be recovered by ftrace_regs_entry, and restored into LR
192 * before returning to the regular function prologue. When a function is not
193 * being traced, the MOV is not harmful given x9 is not live per the AAPCS.
195 * Note: ftrace_process_locs() has pre-adjusted rec->ip to be the address of
198 int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
200 unsigned long pc = rec->ip - AARCH64_INSN_SIZE;
203 old = aarch64_insn_gen_nop();
204 new = aarch64_insn_gen_move_reg(AARCH64_INSN_REG_9,
206 AARCH64_INSN_VARIANT_64BIT);
207 return ftrace_modify_code(pc, old, new, true);
212 * Turn off the call to ftrace_caller() in instrumented function
214 int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
217 unsigned long pc = rec->ip;
220 if (!ftrace_find_callable_addr(rec, mod, &addr))
223 old = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
224 new = aarch64_insn_gen_nop();
226 return ftrace_modify_code(pc, old, new, true);
229 void arch_ftrace_update_code(int command)
231 command |= FTRACE_MAY_SLEEP;
232 ftrace_modify_all_code(command);
235 int __init ftrace_dyn_arch_init(void)
239 #endif /* CONFIG_DYNAMIC_FTRACE */
241 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
243 * function_graph tracer expects ftrace_return_to_handler() to be called
244 * on the way back to parent. For this purpose, this function is called
245 * in _mcount() or ftrace_caller() to replace return address (*parent) on
246 * the call stack to return_to_handler.
248 * Note that @frame_pointer is used only for sanity check later.
250 void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent,
251 unsigned long frame_pointer)
253 unsigned long return_hooker = (unsigned long)&return_to_handler;
256 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
261 * No protection against faulting at *parent, which may be seen
262 * on other archs. It's unlikely on AArch64.
266 if (!function_graph_enter(old, self_addr, frame_pointer, NULL))
267 *parent = return_hooker;
270 #ifdef CONFIG_DYNAMIC_FTRACE
272 * Turn on/off the call to ftrace_graph_caller() in ftrace_caller()
273 * depending on @enable.
275 static int ftrace_modify_graph_caller(bool enable)
277 unsigned long pc = (unsigned long)&ftrace_graph_call;
280 branch = aarch64_insn_gen_branch_imm(pc,
281 (unsigned long)ftrace_graph_caller,
282 AARCH64_INSN_BRANCH_NOLINK);
283 nop = aarch64_insn_gen_nop();
286 return ftrace_modify_code(pc, nop, branch, true);
288 return ftrace_modify_code(pc, branch, nop, true);
291 int ftrace_enable_ftrace_graph_caller(void)
293 return ftrace_modify_graph_caller(true);
296 int ftrace_disable_ftrace_graph_caller(void)
298 return ftrace_modify_graph_caller(false);
300 #endif /* CONFIG_DYNAMIC_FTRACE */
301 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */