blob: 81d402e9ebd1c01a0b16f410c2b159e9c9ecb1d8 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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
9#include <linux/ftrace.h>
10#include <linux/module.h>
11#include <linux/swab.h>
12#include <linux/uaccess.h>
13
14#include <asm/cacheflush.h>
15#include <asm/debug-monitors.h>
16#include <asm/ftrace.h>
17#include <asm/insn.h>
18
19#ifdef CONFIG_DYNAMIC_FTRACE
20/*
21 * Replace a single instruction, which may be a branch or NOP.
22 * If @validate == true, a replaced instruction is checked against 'old'.
23 */
24static int ftrace_modify_code(unsigned long pc, u32 old, u32 new,
25 bool validate)
26{
27 u32 replaced;
28
29 /*
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.
36 */
37 if (validate) {
38 if (aarch64_insn_read((void *)pc, &replaced))
39 return -EFAULT;
40
41 if (replaced != old)
42 return -EINVAL;
43 }
44 if (aarch64_insn_patch_text_nosync((void *)pc, new))
45 return -EPERM;
46
47 return 0;
48}
49
50/*
51 * Replace tracer function in ftrace_caller()
52 */
53int ftrace_update_ftrace_func(ftrace_func_t func)
54{
55 unsigned long pc;
56 u32 new;
57
58 pc = (unsigned long)__va_function(ftrace_call);
59 new = aarch64_insn_gen_branch_imm(pc, (unsigned long)func,
60 AARCH64_INSN_BRANCH_LINK);
61
62 return ftrace_modify_code(pc, 0, new, false);
63}
64
65/*
66 * Turn on the call to ftrace_caller() in instrumented function
67 */
68int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
69{
70 unsigned long pc = rec->ip;
71 u32 old, new;
72 long offset = (long)addr - (long)pc;
73
74 if (offset < -SZ_128M || offset >= SZ_128M) {
75#ifdef CONFIG_ARM64_MODULE_PLTS
76 struct module *mod;
77
78 /*
79 * There is only one ftrace trampoline per module. For now,
80 * this is not a problem since on arm64, all dynamic ftrace
81 * invocations are routed via ftrace_caller(). This will need
82 * to be revisited if support for multiple ftrace entry points
83 * is added in the future, but for now, the pr_err() below
84 * deals with a theoretical issue only.
85 */
86 if (addr != FTRACE_ADDR) {
87 pr_err("ftrace: far branches to multiple entry points unsupported inside a single module\n");
88 return -EINVAL;
89 }
90
91 /*
92 * On kernels that support module PLTs, the offset between the
93 * branch instruction and its target may legally exceed the
94 * range of an ordinary relative 'bl' opcode. In this case, we
95 * need to branch via a trampoline in the module.
96 *
97 * NOTE: __module_text_address() must be called with preemption
98 * disabled, but we can rely on ftrace_lock to ensure that 'mod'
99 * retains its validity throughout the remainder of this code.
100 */
101 preempt_disable();
102 mod = __module_text_address(pc);
103 preempt_enable();
104
105 if (WARN_ON(!mod))
106 return -EINVAL;
107
108 addr = (unsigned long)mod->arch.ftrace_trampoline;
109#else /* CONFIG_ARM64_MODULE_PLTS */
110 return -EINVAL;
111#endif /* CONFIG_ARM64_MODULE_PLTS */
112 }
113
114 old = aarch64_insn_gen_nop();
115 new = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
116
117 return ftrace_modify_code(pc, old, new, true);
118}
119
120/*
121 * Turn off the call to ftrace_caller() in instrumented function
122 */
123int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
124 unsigned long addr)
125{
126 unsigned long pc = rec->ip;
127 bool validate = true;
128 u32 old = 0, new;
129 long offset = (long)addr - (long)pc;
130
131 if (offset < -SZ_128M || offset >= SZ_128M) {
132#ifdef CONFIG_ARM64_MODULE_PLTS
133 u32 replaced;
134
135 /*
136 * 'mod' is only set at module load time, but if we end up
137 * dealing with an out-of-range condition, we can assume it
138 * is due to a module being loaded far away from the kernel.
139 */
140 if (!mod) {
141 preempt_disable();
142 mod = __module_text_address(pc);
143 preempt_enable();
144
145 if (WARN_ON(!mod))
146 return -EINVAL;
147 }
148
149 /*
150 * The instruction we are about to patch may be a branch and
151 * link instruction that was redirected via a PLT entry. In
152 * this case, the normal validation will fail, but we can at
153 * least check that we are dealing with a branch and link
154 * instruction that points into the right module.
155 */
156 if (aarch64_insn_read((void *)pc, &replaced))
157 return -EFAULT;
158
159 if (!aarch64_insn_is_bl(replaced) ||
160 !within_module(pc + aarch64_get_branch_offset(replaced),
161 mod))
162 return -EINVAL;
163
164 validate = false;
165#else /* CONFIG_ARM64_MODULE_PLTS */
166 return -EINVAL;
167#endif /* CONFIG_ARM64_MODULE_PLTS */
168 } else {
169 old = aarch64_insn_gen_branch_imm(pc, addr,
170 AARCH64_INSN_BRANCH_LINK);
171 }
172
173 new = aarch64_insn_gen_nop();
174
175 return ftrace_modify_code(pc, old, new, validate);
176}
177
178void arch_ftrace_update_code(int command)
179{
180 command |= FTRACE_MAY_SLEEP;
181 ftrace_modify_all_code(command);
182}
183
184int __init ftrace_dyn_arch_init(void)
185{
186 return 0;
187}
188#endif /* CONFIG_DYNAMIC_FTRACE */
189
190#ifdef CONFIG_FUNCTION_GRAPH_TRACER
191/*
192 * function_graph tracer expects ftrace_return_to_handler() to be called
193 * on the way back to parent. For this purpose, this function is called
194 * in _mcount() or ftrace_caller() to replace return address (*parent) on
195 * the call stack to return_to_handler.
196 *
197 * Note that @frame_pointer is used only for sanity check later.
198 */
199void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent,
200 unsigned long frame_pointer)
201{
202 unsigned long return_hooker = (unsigned long)&return_to_handler;
203 unsigned long old;
204
205 if (unlikely(atomic_read(&current->tracing_graph_pause)))
206 return;
207
208 /*
209 * Note:
210 * No protection against faulting at *parent, which may be seen
211 * on other archs. It's unlikely on AArch64.
212 */
213 old = *parent;
214
215 if (!function_graph_enter(old, self_addr, frame_pointer, NULL))
216 *parent = return_hooker;
217}
218
219#ifdef CONFIG_DYNAMIC_FTRACE
220/*
221 * Turn on/off the call to ftrace_graph_caller() in ftrace_caller()
222 * depending on @enable.
223 */
224static int ftrace_modify_graph_caller(bool enable)
225{
226 unsigned long pc = (unsigned long)&ftrace_graph_call;
227 u32 branch, nop;
228
229 branch = aarch64_insn_gen_branch_imm(pc,
230 (unsigned long)ftrace_graph_caller,
231 AARCH64_INSN_BRANCH_NOLINK);
232 nop = aarch64_insn_gen_nop();
233
234 if (enable)
235 return ftrace_modify_code(pc, nop, branch, true);
236 else
237 return ftrace_modify_code(pc, branch, nop, true);
238}
239
240int ftrace_enable_ftrace_graph_caller(void)
241{
242 return ftrace_modify_graph_caller(true);
243}
244
245int ftrace_disable_ftrace_graph_caller(void)
246{
247 return ftrace_modify_graph_caller(false);
248}
249#endif /* CONFIG_DYNAMIC_FTRACE */
250#endif /* CONFIG_FUNCTION_GRAPH_TRACER */