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 <asm/cacheflush.h>
11 #include <asm/patch.h>
13 #ifdef CONFIG_DYNAMIC_FTRACE
14 static int ftrace_check_current_call(unsigned long hook_pos
,
15 unsigned int *expected
)
17 unsigned int replaced
[2];
18 unsigned int nops
[2] = {NOP4
, NOP4
};
20 /* we expect nops at the hook position */
25 * Read the text we want to modify;
26 * return must be -EFAULT on read error
28 if (probe_kernel_read(replaced
, (void *)hook_pos
, MCOUNT_INSN_SIZE
))
32 * Make sure it is what we expect it to be;
33 * return must be -EINVAL on failed comparison
35 if (memcmp(expected
, replaced
, sizeof(replaced
))) {
36 pr_err("%p: expected (%08x %08x) but got (%08x %08x)\n",
37 (void *)hook_pos
, expected
[0], expected
[1], replaced
[0],
45 static int __ftrace_modify_call(unsigned long hook_pos
, unsigned long target
,
49 unsigned int nops
[2] = {NOP4
, NOP4
};
51 make_call(hook_pos
, target
, call
);
53 /* Replace the auipc-jalr pair at once. Return -EPERM on write error. */
54 if (riscv_patch_text_nosync
55 ((void *)hook_pos
, enable
? call
: nops
, MCOUNT_INSN_SIZE
))
61 int ftrace_make_call(struct dyn_ftrace
*rec
, unsigned long addr
)
63 int ret
= ftrace_check_current_call(rec
->ip
, NULL
);
68 return __ftrace_modify_call(rec
->ip
, addr
, true);
71 int ftrace_make_nop(struct module
*mod
, struct dyn_ftrace
*rec
,
77 make_call(rec
->ip
, addr
, call
);
78 ret
= ftrace_check_current_call(rec
->ip
, call
);
83 return __ftrace_modify_call(rec
->ip
, addr
, false);
86 int ftrace_update_ftrace_func(ftrace_func_t func
)
88 int ret
= __ftrace_modify_call((unsigned long)&ftrace_call
,
89 (unsigned long)func
, true);
91 ret
= __ftrace_modify_call((unsigned long)&ftrace_regs_call
,
92 (unsigned long)func
, true);
98 int __init
ftrace_dyn_arch_init(void)
104 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
105 int ftrace_modify_call(struct dyn_ftrace
*rec
, unsigned long old_addr
,
108 unsigned int call
[2];
111 make_call(rec
->ip
, old_addr
, call
);
112 ret
= ftrace_check_current_call(rec
->ip
, call
);
117 return __ftrace_modify_call(rec
->ip
, addr
, true);
121 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
123 * Most of this function is copied from arm64.
125 void prepare_ftrace_return(unsigned long *parent
, unsigned long self_addr
,
126 unsigned long frame_pointer
)
128 unsigned long return_hooker
= (unsigned long)&return_to_handler
;
131 if (unlikely(atomic_read(¤t
->tracing_graph_pause
)))
135 * We don't suffer access faults, so no extra fault-recovery assembly
140 if (!function_graph_enter(old
, self_addr
, frame_pointer
, parent
))
141 *parent
= return_hooker
;
144 #ifdef CONFIG_DYNAMIC_FTRACE
145 extern void ftrace_graph_call(void);
146 int ftrace_enable_ftrace_graph_caller(void)
148 unsigned int call
[2];
149 static int init_graph
= 1;
152 make_call(&ftrace_graph_call
, &ftrace_stub
, call
);
155 * When enabling graph tracer for the first time, ftrace_graph_call
156 * should contains a call to ftrace_stub. Once it has been disabled,
157 * the 8-bytes at the position becomes NOPs.
160 ret
= ftrace_check_current_call((unsigned long)&ftrace_graph_call
,
164 ret
= ftrace_check_current_call((unsigned long)&ftrace_graph_call
,
171 return __ftrace_modify_call((unsigned long)&ftrace_graph_call
,
172 (unsigned long)&prepare_ftrace_return
, true);
175 int ftrace_disable_ftrace_graph_caller(void)
177 unsigned int call
[2];
180 make_call(&ftrace_graph_call
, &prepare_ftrace_return
, call
);
183 * This is to make sure that ftrace_enable_ftrace_graph_caller
184 * did the right thing.
186 ret
= ftrace_check_current_call((unsigned long)&ftrace_graph_call
,
192 return __ftrace_modify_call((unsigned long)&ftrace_graph_call
,
193 (unsigned long)&prepare_ftrace_return
, false);
195 #endif /* CONFIG_DYNAMIC_FTRACE */
196 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */