Merge tag 'sched-urgent-2020-12-27' of git://git.kernel.org/pub/scm/linux/kernel...
[linux/fpc-iii.git] / arch / arm64 / kernel / ftrace.c
blob86a5cf9bc19a1e8ac7d47f529fc2492d072857bc
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * arch/arm64/kernel/ftrace.c
5 * Copyright (C) 2013 Linaro Limited
6 * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
7 */
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>
17 #include <asm/insn.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,
25 bool validate)
27 u32 replaced;
30 * Note:
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.
37 if (validate) {
38 if (aarch64_insn_read((void *)pc, &replaced))
39 return -EFAULT;
41 if (replaced != old)
42 return -EINVAL;
44 if (aarch64_insn_patch_text_nosync((void *)pc, new))
45 return -EPERM;
47 return 0;
51 * Replace tracer function in ftrace_caller()
53 int ftrace_update_ftrace_func(ftrace_func_t func)
55 unsigned long pc;
56 u32 new;
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];
75 #endif
76 return NULL;
80 * Turn on the call to ftrace_caller() in instrumented function
82 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
84 unsigned long pc = rec->ip;
85 u32 old, new;
86 long offset = (long)pc - (long)addr;
88 if (offset < -SZ_128M || offset >= SZ_128M) {
89 struct module *mod;
90 struct plt_entry *plt;
92 if (!IS_ENABLED(CONFIG_ARM64_MODULE_PLTS))
93 return -EINVAL;
96 * On kernels that support module PLTs, the offset between the
97 * branch instruction and its target may legally exceed the
98 * range of an ordinary relative 'bl' opcode. In this case, we
99 * need to branch via a trampoline in the module.
101 * NOTE: __module_text_address() must be called with preemption
102 * disabled, but we can rely on ftrace_lock to ensure that 'mod'
103 * retains its validity throughout the remainder of this code.
105 preempt_disable();
106 mod = __module_text_address(pc);
107 preempt_enable();
109 if (WARN_ON(!mod))
110 return -EINVAL;
112 plt = get_ftrace_plt(mod, addr);
113 if (!plt) {
114 pr_err("ftrace: no module PLT for %ps\n", (void *)addr);
115 return -EINVAL;
118 addr = (unsigned long)plt;
121 old = aarch64_insn_gen_nop();
122 new = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
124 return ftrace_modify_code(pc, old, new, true);
127 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
128 int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
129 unsigned long addr)
131 unsigned long pc = rec->ip;
132 u32 old, new;
134 old = aarch64_insn_gen_branch_imm(pc, old_addr,
135 AARCH64_INSN_BRANCH_LINK);
136 new = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
138 return ftrace_modify_code(pc, old, new, true);
142 * The compiler has inserted two NOPs before the regular function prologue.
143 * All instrumented functions follow the AAPCS, so x0-x8 and x19-x30 are live,
144 * and x9-x18 are free for our use.
146 * At runtime we want to be able to swing a single NOP <-> BL to enable or
147 * disable the ftrace call. The BL requires us to save the original LR value,
148 * so here we insert a <MOV X9, LR> over the first NOP so the instructions
149 * before the regular prologue are:
151 * | Compiled | Disabled | Enabled |
152 * +----------+------------+------------+
153 * | NOP | MOV X9, LR | MOV X9, LR |
154 * | NOP | NOP | BL <entry> |
156 * The LR value will be recovered by ftrace_regs_entry, and restored into LR
157 * before returning to the regular function prologue. When a function is not
158 * being traced, the MOV is not harmful given x9 is not live per the AAPCS.
160 * Note: ftrace_process_locs() has pre-adjusted rec->ip to be the address of
161 * the BL.
163 int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
165 unsigned long pc = rec->ip - AARCH64_INSN_SIZE;
166 u32 old, new;
168 old = aarch64_insn_gen_nop();
169 new = aarch64_insn_gen_move_reg(AARCH64_INSN_REG_9,
170 AARCH64_INSN_REG_LR,
171 AARCH64_INSN_VARIANT_64BIT);
172 return ftrace_modify_code(pc, old, new, true);
174 #endif
177 * Turn off the call to ftrace_caller() in instrumented function
179 int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
180 unsigned long addr)
182 unsigned long pc = rec->ip;
183 bool validate = true;
184 u32 old = 0, new;
185 long offset = (long)pc - (long)addr;
187 if (offset < -SZ_128M || offset >= SZ_128M) {
188 u32 replaced;
190 if (!IS_ENABLED(CONFIG_ARM64_MODULE_PLTS))
191 return -EINVAL;
194 * 'mod' is only set at module load time, but if we end up
195 * dealing with an out-of-range condition, we can assume it
196 * is due to a module being loaded far away from the kernel.
198 if (!mod) {
199 preempt_disable();
200 mod = __module_text_address(pc);
201 preempt_enable();
203 if (WARN_ON(!mod))
204 return -EINVAL;
208 * The instruction we are about to patch may be a branch and
209 * link instruction that was redirected via a PLT entry. In
210 * this case, the normal validation will fail, but we can at
211 * least check that we are dealing with a branch and link
212 * instruction that points into the right module.
214 if (aarch64_insn_read((void *)pc, &replaced))
215 return -EFAULT;
217 if (!aarch64_insn_is_bl(replaced) ||
218 !within_module(pc + aarch64_get_branch_offset(replaced),
219 mod))
220 return -EINVAL;
222 validate = false;
223 } else {
224 old = aarch64_insn_gen_branch_imm(pc, addr,
225 AARCH64_INSN_BRANCH_LINK);
228 new = aarch64_insn_gen_nop();
230 return ftrace_modify_code(pc, old, new, validate);
233 void arch_ftrace_update_code(int command)
235 command |= FTRACE_MAY_SLEEP;
236 ftrace_modify_all_code(command);
239 int __init ftrace_dyn_arch_init(void)
241 return 0;
243 #endif /* CONFIG_DYNAMIC_FTRACE */
245 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
247 * function_graph tracer expects ftrace_return_to_handler() to be called
248 * on the way back to parent. For this purpose, this function is called
249 * in _mcount() or ftrace_caller() to replace return address (*parent) on
250 * the call stack to return_to_handler.
252 * Note that @frame_pointer is used only for sanity check later.
254 void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent,
255 unsigned long frame_pointer)
257 unsigned long return_hooker = (unsigned long)&return_to_handler;
258 unsigned long old;
260 if (unlikely(atomic_read(&current->tracing_graph_pause)))
261 return;
264 * Note:
265 * No protection against faulting at *parent, which may be seen
266 * on other archs. It's unlikely on AArch64.
268 old = *parent;
270 if (!function_graph_enter(old, self_addr, frame_pointer, NULL))
271 *parent = return_hooker;
274 #ifdef CONFIG_DYNAMIC_FTRACE
276 * Turn on/off the call to ftrace_graph_caller() in ftrace_caller()
277 * depending on @enable.
279 static int ftrace_modify_graph_caller(bool enable)
281 unsigned long pc = (unsigned long)&ftrace_graph_call;
282 u32 branch, nop;
284 branch = aarch64_insn_gen_branch_imm(pc,
285 (unsigned long)ftrace_graph_caller,
286 AARCH64_INSN_BRANCH_NOLINK);
287 nop = aarch64_insn_gen_nop();
289 if (enable)
290 return ftrace_modify_code(pc, nop, branch, true);
291 else
292 return ftrace_modify_code(pc, branch, nop, true);
295 int ftrace_enable_ftrace_graph_caller(void)
297 return ftrace_modify_graph_caller(true);
300 int ftrace_disable_ftrace_graph_caller(void)
302 return ftrace_modify_graph_caller(false);
304 #endif /* CONFIG_DYNAMIC_FTRACE */
305 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */