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 int ftrace_arch_code_modify_prepare(void) __acquires(&text_mutex)
17 mutex_lock(&text_mutex);
21 int ftrace_arch_code_modify_post_process(void) __releases(&text_mutex)
23 mutex_unlock(&text_mutex);
27 static int ftrace_check_current_call(unsigned long hook_pos,
28 unsigned int *expected)
30 unsigned int replaced[2];
31 unsigned int nops[2] = {NOP4, NOP4};
33 /* we expect nops at the hook position */
38 * Read the text we want to modify;
39 * return must be -EFAULT on read error
41 if (probe_kernel_read(replaced, (void *)hook_pos, MCOUNT_INSN_SIZE))
45 * Make sure it is what we expect it to be;
46 * return must be -EINVAL on failed comparison
48 if (memcmp(expected, replaced, sizeof(replaced))) {
49 pr_err("%p: expected (%08x %08x) but got (%08x %08x)\n",
50 (void *)hook_pos, expected[0], expected[1], replaced[0],
58 static int __ftrace_modify_call(unsigned long hook_pos, unsigned long target,
62 unsigned int nops[2] = {NOP4, NOP4};
64 make_call(hook_pos, target, call);
66 /* Replace the auipc-jalr pair at once. Return -EPERM on write error. */
68 ((void *)hook_pos, enable ? call : nops, MCOUNT_INSN_SIZE))
74 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
76 int ret = ftrace_check_current_call(rec->ip, NULL);
81 return __ftrace_modify_call(rec->ip, addr, true);
84 int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
90 make_call(rec->ip, addr, call);
91 ret = ftrace_check_current_call(rec->ip, call);
96 return __ftrace_modify_call(rec->ip, addr, false);
99 int ftrace_update_ftrace_func(ftrace_func_t func)
101 int ret = __ftrace_modify_call((unsigned long)&ftrace_call,
102 (unsigned long)func, true);
104 ret = __ftrace_modify_call((unsigned long)&ftrace_regs_call,
105 (unsigned long)func, true);
111 int __init ftrace_dyn_arch_init(void)
117 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
118 int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
121 unsigned int call[2];
124 make_call(rec->ip, old_addr, call);
125 ret = ftrace_check_current_call(rec->ip, call);
130 return __ftrace_modify_call(rec->ip, addr, true);
134 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
136 * Most of this function is copied from arm64.
138 void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
139 unsigned long frame_pointer)
141 unsigned long return_hooker = (unsigned long)&return_to_handler;
144 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
148 * We don't suffer access faults, so no extra fault-recovery assembly
153 if (!function_graph_enter(old, self_addr, frame_pointer, parent))
154 *parent = return_hooker;
157 #ifdef CONFIG_DYNAMIC_FTRACE
158 extern void ftrace_graph_call(void);
159 int ftrace_enable_ftrace_graph_caller(void)
161 unsigned int call[2];
162 static int init_graph = 1;
165 make_call(&ftrace_graph_call, &ftrace_stub, call);
168 * When enabling graph tracer for the first time, ftrace_graph_call
169 * should contains a call to ftrace_stub. Once it has been disabled,
170 * the 8-bytes at the position becomes NOPs.
173 ret = ftrace_check_current_call((unsigned long)&ftrace_graph_call,
177 ret = ftrace_check_current_call((unsigned long)&ftrace_graph_call,
184 return __ftrace_modify_call((unsigned long)&ftrace_graph_call,
185 (unsigned long)&prepare_ftrace_return, true);
188 int ftrace_disable_ftrace_graph_caller(void)
190 unsigned int call[2];
193 make_call(&ftrace_graph_call, &prepare_ftrace_return, call);
196 * This is to make sure that ftrace_enable_ftrace_graph_caller
197 * did the right thing.
199 ret = ftrace_check_current_call((unsigned long)&ftrace_graph_call,
205 return __ftrace_modify_call((unsigned long)&ftrace_graph_call,
206 (unsigned long)&prepare_ftrace_return, false);
208 #endif /* CONFIG_DYNAMIC_FTRACE */
209 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */