Linux 4.18.10
[linux/fpc-iii.git] / arch / riscv / kernel / ftrace.c
blob1157b6b52d259fa79edf0d44f589022174aff18c
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Copyright (C) 2013 Linaro Limited
4 * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
5 * Copyright (C) 2017 Andes Technology Corporation
6 */
8 #include <linux/ftrace.h>
9 #include <linux/uaccess.h>
10 #include <asm/cacheflush.h>
12 #ifdef CONFIG_DYNAMIC_FTRACE
13 static int ftrace_check_current_call(unsigned long hook_pos,
14 unsigned int *expected)
16 unsigned int replaced[2];
17 unsigned int nops[2] = {NOP4, NOP4};
19 /* we expect nops at the hook position */
20 if (!expected)
21 expected = nops;
24 * Read the text we want to modify;
25 * return must be -EFAULT on read error
27 if (probe_kernel_read(replaced, (void *)hook_pos, MCOUNT_INSN_SIZE))
28 return -EFAULT;
31 * Make sure it is what we expect it to be;
32 * return must be -EINVAL on failed comparison
34 if (memcmp(expected, replaced, sizeof(replaced))) {
35 pr_err("%p: expected (%08x %08x) but get (%08x %08x)",
36 (void *)hook_pos, expected[0], expected[1], replaced[0],
37 replaced[1]);
38 return -EINVAL;
41 return 0;
44 static int __ftrace_modify_call(unsigned long hook_pos, unsigned long target,
45 bool enable)
47 unsigned int call[2];
48 unsigned int nops[2] = {NOP4, NOP4};
49 int ret = 0;
51 make_call(hook_pos, target, call);
53 /* replace the auipc-jalr pair at once */
54 ret = probe_kernel_write((void *)hook_pos, enable ? call : nops,
55 MCOUNT_INSN_SIZE);
56 /* return must be -EPERM on write error */
57 if (ret)
58 return -EPERM;
60 smp_mb();
61 flush_icache_range((void *)hook_pos, (void *)hook_pos + MCOUNT_INSN_SIZE);
63 return 0;
66 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
68 int ret = ftrace_check_current_call(rec->ip, NULL);
70 if (ret)
71 return ret;
73 return __ftrace_modify_call(rec->ip, addr, true);
76 int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
77 unsigned long addr)
79 unsigned int call[2];
80 int ret;
82 make_call(rec->ip, addr, call);
83 ret = ftrace_check_current_call(rec->ip, call);
85 if (ret)
86 return ret;
88 return __ftrace_modify_call(rec->ip, addr, false);
91 int ftrace_update_ftrace_func(ftrace_func_t func)
93 int ret = __ftrace_modify_call((unsigned long)&ftrace_call,
94 (unsigned long)func, true);
95 if (!ret) {
96 ret = __ftrace_modify_call((unsigned long)&ftrace_regs_call,
97 (unsigned long)func, true);
100 return ret;
103 int __init ftrace_dyn_arch_init(void)
105 return 0;
107 #endif
109 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
110 int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
111 unsigned long addr)
113 unsigned int call[2];
114 int ret;
116 make_call(rec->ip, old_addr, call);
117 ret = ftrace_check_current_call(rec->ip, call);
119 if (ret)
120 return ret;
122 return __ftrace_modify_call(rec->ip, addr, true);
124 #endif
126 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
128 * Most of this function is copied from arm64.
130 void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
131 unsigned long frame_pointer)
133 unsigned long return_hooker = (unsigned long)&return_to_handler;
134 unsigned long old;
135 struct ftrace_graph_ent trace;
136 int err;
138 if (unlikely(atomic_read(&current->tracing_graph_pause)))
139 return;
142 * We don't suffer access faults, so no extra fault-recovery assembly
143 * is needed here.
145 old = *parent;
147 trace.func = self_addr;
148 trace.depth = current->curr_ret_stack + 1;
150 if (!ftrace_graph_entry(&trace))
151 return;
153 err = ftrace_push_return_trace(old, self_addr, &trace.depth,
154 frame_pointer, parent);
155 if (err == -EBUSY)
156 return;
157 *parent = return_hooker;
160 #ifdef CONFIG_DYNAMIC_FTRACE
161 extern void ftrace_graph_call(void);
162 int ftrace_enable_ftrace_graph_caller(void)
164 unsigned int call[2];
165 static int init_graph = 1;
166 int ret;
168 make_call(&ftrace_graph_call, &ftrace_stub, call);
171 * When enabling graph tracer for the first time, ftrace_graph_call
172 * should contains a call to ftrace_stub. Once it has been disabled,
173 * the 8-bytes at the position becomes NOPs.
175 if (init_graph) {
176 ret = ftrace_check_current_call((unsigned long)&ftrace_graph_call,
177 call);
178 init_graph = 0;
179 } else {
180 ret = ftrace_check_current_call((unsigned long)&ftrace_graph_call,
181 NULL);
184 if (ret)
185 return ret;
187 return __ftrace_modify_call((unsigned long)&ftrace_graph_call,
188 (unsigned long)&prepare_ftrace_return, true);
191 int ftrace_disable_ftrace_graph_caller(void)
193 unsigned int call[2];
194 int ret;
196 make_call(&ftrace_graph_call, &prepare_ftrace_return, call);
199 * This is to make sure that ftrace_enable_ftrace_graph_caller
200 * did the right thing.
202 ret = ftrace_check_current_call((unsigned long)&ftrace_graph_call,
203 call);
205 if (ret)
206 return ret;
208 return __ftrace_modify_call((unsigned long)&ftrace_graph_call,
209 (unsigned long)&prepare_ftrace_return, false);
211 #endif /* CONFIG_DYNAMIC_FTRACE */
212 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */