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_WITH_ARGS
26 #define FREGS_OFFSET(n, field) \
29 .offset = offsetof(struct ftrace_regs, field), \
32 static const struct fregs_offset fregs_offsets[] = {
33 FREGS_OFFSET("x0", regs[0]),
34 FREGS_OFFSET("x1", regs[1]),
35 FREGS_OFFSET("x2", regs[2]),
36 FREGS_OFFSET("x3", regs[3]),
37 FREGS_OFFSET("x4", regs[4]),
38 FREGS_OFFSET("x5", regs[5]),
39 FREGS_OFFSET("x6", regs[6]),
40 FREGS_OFFSET("x7", regs[7]),
41 FREGS_OFFSET("x8", regs[8]),
43 FREGS_OFFSET("x29", fp),
44 FREGS_OFFSET("x30", lr),
45 FREGS_OFFSET("lr", lr),
47 FREGS_OFFSET("sp", sp),
48 FREGS_OFFSET("pc", pc),
51 int ftrace_regs_query_register_offset(const char *name)
53 for (int i = 0; i < ARRAY_SIZE(fregs_offsets); i++) {
54 const struct fregs_offset *roff = &fregs_offsets[i];
55 if (!strcmp(roff->name, name))
64 * Replace a single instruction, which may be a branch or NOP.
65 * If @validate == true, a replaced instruction is checked against 'old'.
67 static int ftrace_modify_code(unsigned long pc, u32 old, u32 new,
74 * We are paranoid about modifying text, as if a bug were to happen, it
75 * could cause us to read or write to someplace that could cause harm.
76 * Carefully read and modify the code with aarch64_insn_*() which uses
77 * probe_kernel_*(), and make sure what we read is what we expected it
78 * to be before modifying it.
81 if (aarch64_insn_read((void *)pc, &replaced))
87 if (aarch64_insn_patch_text_nosync((void *)pc, new))
94 * Replace tracer function in ftrace_caller()
96 int ftrace_update_ftrace_func(ftrace_func_t func)
101 pc = (unsigned long)ftrace_call;
102 new = aarch64_insn_gen_branch_imm(pc, (unsigned long)func,
103 AARCH64_INSN_BRANCH_LINK);
105 return ftrace_modify_code(pc, 0, new, false);
108 static struct plt_entry *get_ftrace_plt(struct module *mod, unsigned long addr)
110 #ifdef CONFIG_ARM64_MODULE_PLTS
111 struct plt_entry *plt = mod->arch.ftrace_trampolines;
113 if (addr == FTRACE_ADDR)
114 return &plt[FTRACE_PLT_IDX];
120 * Find the address the callsite must branch to in order to reach '*addr'.
122 * Due to the limited range of 'BL' instructions, modules may be placed too far
123 * away to branch directly and must use a PLT.
125 * Returns true when '*addr' contains a reachable target address, or has been
126 * modified to contain a PLT address. Returns false otherwise.
128 static bool ftrace_find_callable_addr(struct dyn_ftrace *rec,
132 unsigned long pc = rec->ip;
133 long offset = (long)*addr - (long)pc;
134 struct plt_entry *plt;
137 * When the target is within range of the 'BL' instruction, use 'addr'
138 * as-is and branch to that directly.
140 if (offset >= -SZ_128M && offset < SZ_128M)
144 * When the target is outside of the range of a 'BL' instruction, we
145 * must use a PLT to reach it. We can only place PLTs for modules, and
146 * only when module PLT support is built-in.
148 if (!IS_ENABLED(CONFIG_ARM64_MODULE_PLTS))
152 * 'mod' is only set at module load time, but if we end up
153 * dealing with an out-of-range condition, we can assume it
154 * is due to a module being loaded far away from the kernel.
156 * NOTE: __module_text_address() must be called with preemption
157 * disabled, but we can rely on ftrace_lock to ensure that 'mod'
158 * retains its validity throughout the remainder of this code.
162 mod = __module_text_address(pc);
169 plt = get_ftrace_plt(mod, *addr);
171 pr_err("ftrace: no module PLT for %ps\n", (void *)*addr);
175 *addr = (unsigned long)plt;
180 * Turn on the call to ftrace_caller() in instrumented function
182 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
184 unsigned long pc = rec->ip;
187 if (!ftrace_find_callable_addr(rec, NULL, &addr))
190 old = aarch64_insn_gen_nop();
191 new = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
193 return ftrace_modify_code(pc, old, new, true);
196 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_ARGS
198 * The compiler has inserted two NOPs before the regular function prologue.
199 * All instrumented functions follow the AAPCS, so x0-x8 and x19-x30 are live,
200 * and x9-x18 are free for our use.
202 * At runtime we want to be able to swing a single NOP <-> BL to enable or
203 * disable the ftrace call. The BL requires us to save the original LR value,
204 * so here we insert a <MOV X9, LR> over the first NOP so the instructions
205 * before the regular prologue are:
207 * | Compiled | Disabled | Enabled |
208 * +----------+------------+------------+
209 * | NOP | MOV X9, LR | MOV X9, LR |
210 * | NOP | NOP | BL <entry> |
212 * The LR value will be recovered by ftrace_caller, and restored into LR
213 * before returning to the regular function prologue. When a function is not
214 * being traced, the MOV is not harmful given x9 is not live per the AAPCS.
216 * Note: ftrace_process_locs() has pre-adjusted rec->ip to be the address of
219 int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
221 unsigned long pc = rec->ip - AARCH64_INSN_SIZE;
224 old = aarch64_insn_gen_nop();
225 new = aarch64_insn_gen_move_reg(AARCH64_INSN_REG_9,
227 AARCH64_INSN_VARIANT_64BIT);
228 return ftrace_modify_code(pc, old, new, true);
233 * Turn off the call to ftrace_caller() in instrumented function
235 int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
238 unsigned long pc = rec->ip;
241 new = aarch64_insn_gen_nop();
244 * When using mcount, callsites in modules may have been initalized to
245 * call an arbitrary module PLT (which redirects to the _mcount stub)
246 * rather than the ftrace PLT we'll use at runtime (which redirects to
247 * the ftrace trampoline). We can ignore the old PLT when initializing
250 * Note: 'mod' is only set at module load time.
252 if (!IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_ARGS) &&
253 IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) && mod) {
254 return aarch64_insn_patch_text_nosync((void *)pc, new);
257 if (!ftrace_find_callable_addr(rec, mod, &addr))
260 old = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
262 return ftrace_modify_code(pc, old, new, true);
265 void arch_ftrace_update_code(int command)
267 command |= FTRACE_MAY_SLEEP;
268 ftrace_modify_all_code(command);
271 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
273 * function_graph tracer expects ftrace_return_to_handler() to be called
274 * on the way back to parent. For this purpose, this function is called
275 * in _mcount() or ftrace_caller() to replace return address (*parent) on
276 * the call stack to return_to_handler.
278 void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent,
279 unsigned long frame_pointer)
281 unsigned long return_hooker = (unsigned long)&return_to_handler;
284 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
289 * No protection against faulting at *parent, which may be seen
290 * on other archs. It's unlikely on AArch64.
294 if (!function_graph_enter(old, self_addr, frame_pointer,
295 (void *)frame_pointer)) {
296 *parent = return_hooker;
300 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_ARGS
301 void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,
302 struct ftrace_ops *op, struct ftrace_regs *fregs)
304 prepare_ftrace_return(ip, &fregs->lr, fregs->fp);
308 * Turn on/off the call to ftrace_graph_caller() in ftrace_caller()
309 * depending on @enable.
311 static int ftrace_modify_graph_caller(bool enable)
313 unsigned long pc = (unsigned long)&ftrace_graph_call;
316 branch = aarch64_insn_gen_branch_imm(pc,
317 (unsigned long)ftrace_graph_caller,
318 AARCH64_INSN_BRANCH_NOLINK);
319 nop = aarch64_insn_gen_nop();
322 return ftrace_modify_code(pc, nop, branch, true);
324 return ftrace_modify_code(pc, branch, nop, true);
327 int ftrace_enable_ftrace_graph_caller(void)
329 return ftrace_modify_graph_caller(true);
332 int ftrace_disable_ftrace_graph_caller(void)
334 return ftrace_modify_graph_caller(false);
336 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_ARGS */
337 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */