1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2013 Linaro Limited
4 * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
5 * Copyright (C) 2017 Andes Technology Corporation
8 #include <linux/ftrace.h>
9 #include <linux/uaccess.h>
10 #include <linux/memory.h>
11 #include <asm/cacheflush.h>
12 #include <asm/patch.h>
14 #ifdef CONFIG_DYNAMIC_FTRACE
15 void ftrace_arch_code_modify_prepare(void) __acquires(&text_mutex)
17 mutex_lock(&text_mutex);
20 void ftrace_arch_code_modify_post_process(void) __releases(&text_mutex)
22 mutex_unlock(&text_mutex);
25 static int ftrace_check_current_call(unsigned long hook_pos,
26 unsigned int *expected)
28 unsigned int replaced[2];
29 unsigned int nops[2] = {NOP4, NOP4};
31 /* we expect nops at the hook position */
36 * Read the text we want to modify;
37 * return must be -EFAULT on read error
39 if (copy_from_kernel_nofault(replaced, (void *)hook_pos,
44 * Make sure it is what we expect it to be;
45 * return must be -EINVAL on failed comparison
47 if (memcmp(expected, replaced, sizeof(replaced))) {
48 pr_err("%p: expected (%08x %08x) but got (%08x %08x)\n",
49 (void *)hook_pos, expected[0], expected[1], replaced[0],
57 static int __ftrace_modify_call(unsigned long hook_pos, unsigned long target,
61 unsigned int nops[2] = {NOP4, NOP4};
63 make_call(hook_pos, target, call);
65 /* Replace the auipc-jalr pair at once. Return -EPERM on write error. */
67 ((void *)hook_pos, enable ? call : nops, MCOUNT_INSN_SIZE))
74 * Put 5 instructions with 16 bytes at the front of function within
75 * patchable function entry nops' area.
77 * 0: REG_S ra, -SZREG(sp)
80 * 3: REG_L ra, -SZREG(sp)
83 * 0: 0xfe113c23 (sd)/0xfe112e23 (sw)
84 * 1: 0x???????? -> auipc
85 * 2: 0x???????? -> jalr
86 * 3: 0xff813083 (ld)/0xffc12083 (lw)
88 #if __riscv_xlen == 64
89 #define INSN0 0xfe113c23
90 #define INSN3 0xff813083
91 #elif __riscv_xlen == 32
92 #define INSN0 0xfe112e23
93 #define INSN3 0xffc12083
96 #define FUNC_ENTRY_SIZE 16
97 #define FUNC_ENTRY_JMP 4
99 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
101 unsigned int call[4] = {INSN0, 0, 0, INSN3};
102 unsigned long target = addr;
103 unsigned long caller = rec->ip + FUNC_ENTRY_JMP;
105 call[1] = to_auipc_insn((unsigned int)(target - caller));
106 call[2] = to_jalr_insn((unsigned int)(target - caller));
108 if (patch_text_nosync((void *)rec->ip, call, FUNC_ENTRY_SIZE))
114 int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
117 unsigned int nops[4] = {NOP4, NOP4, NOP4, NOP4};
119 if (patch_text_nosync((void *)rec->ip, nops, FUNC_ENTRY_SIZE))
127 * This is called early on, and isn't wrapped by
128 * ftrace_arch_code_modify_{prepare,post_process}() and therefor doesn't hold
129 * text_mutex, which triggers a lockdep failure. SMP isn't running so we could
130 * just directly poke the text, but it's simpler to just take the lock
133 int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
137 ftrace_arch_code_modify_prepare();
138 out = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
139 ftrace_arch_code_modify_post_process();
144 int ftrace_update_ftrace_func(ftrace_func_t func)
146 int ret = __ftrace_modify_call((unsigned long)&ftrace_call,
147 (unsigned long)func, true);
149 ret = __ftrace_modify_call((unsigned long)&ftrace_regs_call,
150 (unsigned long)func, true);
157 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
158 int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
161 unsigned int call[2];
162 unsigned long caller = rec->ip + FUNC_ENTRY_JMP;
165 make_call(caller, old_addr, call);
166 ret = ftrace_check_current_call(caller, call);
171 return __ftrace_modify_call(caller, addr, true);
175 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
177 * Most of this function is copied from arm64.
179 void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
180 unsigned long frame_pointer)
182 unsigned long return_hooker = (unsigned long)&return_to_handler;
185 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
189 * We don't suffer access faults, so no extra fault-recovery assembly
194 if (!function_graph_enter(old, self_addr, frame_pointer, parent))
195 *parent = return_hooker;
198 #ifdef CONFIG_DYNAMIC_FTRACE
199 extern void ftrace_graph_call(void);
200 extern void ftrace_graph_regs_call(void);
201 int ftrace_enable_ftrace_graph_caller(void)
205 ret = __ftrace_modify_call((unsigned long)&ftrace_graph_call,
206 (unsigned long)&prepare_ftrace_return, true);
210 return __ftrace_modify_call((unsigned long)&ftrace_graph_regs_call,
211 (unsigned long)&prepare_ftrace_return, true);
214 int ftrace_disable_ftrace_graph_caller(void)
218 ret = __ftrace_modify_call((unsigned long)&ftrace_graph_call,
219 (unsigned long)&prepare_ftrace_return, false);
223 return __ftrace_modify_call((unsigned long)&ftrace_graph_regs_call,
224 (unsigned long)&prepare_ftrace_return, false);
226 #endif /* CONFIG_DYNAMIC_FTRACE */
227 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */