GNU Linux-libre 4.19.286-gnu1
[releases.git] / arch / riscv / kernel / ftrace.c
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  */
7
8 #include <linux/ftrace.h>
9 #include <linux/uaccess.h>
10 #include <asm/cacheflush.h>
11
12 #ifdef CONFIG_DYNAMIC_FTRACE
13 static int ftrace_check_current_call(unsigned long hook_pos,
14                                      unsigned int *expected)
15 {
16         unsigned int replaced[2];
17         unsigned int nops[2] = {NOP4, NOP4};
18
19         /* we expect nops at the hook position */
20         if (!expected)
21                 expected = nops;
22
23         /*
24          * Read the text we want to modify;
25          * return must be -EFAULT on read error
26          */
27         if (probe_kernel_read(replaced, (void *)hook_pos, MCOUNT_INSN_SIZE))
28                 return -EFAULT;
29
30         /*
31          * Make sure it is what we expect it to be;
32          * return must be -EINVAL on failed comparison
33          */
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;
39         }
40
41         return 0;
42 }
43
44 static int __ftrace_modify_call(unsigned long hook_pos, unsigned long target,
45                                 bool enable)
46 {
47         unsigned int call[2];
48         unsigned int nops[2] = {NOP4, NOP4};
49         int ret = 0;
50
51         make_call(hook_pos, target, call);
52
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;
59
60         smp_mb();
61         flush_icache_range((void *)hook_pos, (void *)hook_pos + MCOUNT_INSN_SIZE);
62
63         return 0;
64 }
65
66 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
67 {
68         int ret = ftrace_check_current_call(rec->ip, NULL);
69
70         if (ret)
71                 return ret;
72
73         return __ftrace_modify_call(rec->ip, addr, true);
74 }
75
76 int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
77                     unsigned long addr)
78 {
79         unsigned int call[2];
80         int ret;
81
82         make_call(rec->ip, addr, call);
83         ret = ftrace_check_current_call(rec->ip, call);
84
85         if (ret)
86                 return ret;
87
88         return __ftrace_modify_call(rec->ip, addr, false);
89 }
90
91
92 /*
93  * This is called early on, and isn't wrapped by
94  * ftrace_arch_code_modify_{prepare,post_process}() and therefor doesn't hold
95  * text_mutex, which triggers a lockdep failure.  SMP isn't running so we could
96  * just directly poke the text, but it's simpler to just take the lock
97  * ourselves.
98  */
99 int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
100 {
101         int out;
102
103         ftrace_arch_code_modify_prepare();
104         out = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
105         ftrace_arch_code_modify_post_process();
106
107         return out;
108 }
109
110 int ftrace_update_ftrace_func(ftrace_func_t func)
111 {
112         int ret = __ftrace_modify_call((unsigned long)&ftrace_call,
113                                        (unsigned long)func, true);
114         if (!ret) {
115                 ret = __ftrace_modify_call((unsigned long)&ftrace_regs_call,
116                                            (unsigned long)func, true);
117         }
118
119         return ret;
120 }
121
122 int __init ftrace_dyn_arch_init(void)
123 {
124         return 0;
125 }
126 #endif
127
128 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
129 int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
130                        unsigned long addr)
131 {
132         unsigned int call[2];
133         int ret;
134
135         make_call(rec->ip, old_addr, call);
136         ret = ftrace_check_current_call(rec->ip, call);
137
138         if (ret)
139                 return ret;
140
141         return __ftrace_modify_call(rec->ip, addr, true);
142 }
143 #endif
144
145 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
146 /*
147  * Most of this function is copied from arm64.
148  */
149 void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
150                            unsigned long frame_pointer)
151 {
152         unsigned long return_hooker = (unsigned long)&return_to_handler;
153         unsigned long old;
154
155         if (unlikely(atomic_read(&current->tracing_graph_pause)))
156                 return;
157
158         /*
159          * We don't suffer access faults, so no extra fault-recovery assembly
160          * is needed here.
161          */
162         old = *parent;
163
164         if (!function_graph_enter(old, self_addr, frame_pointer, parent))
165                 *parent = return_hooker;
166 }
167
168 #ifdef CONFIG_DYNAMIC_FTRACE
169 extern void ftrace_graph_call(void);
170 int ftrace_enable_ftrace_graph_caller(void)
171 {
172         unsigned int call[2];
173         static int init_graph = 1;
174         int ret;
175
176         make_call(&ftrace_graph_call, &ftrace_stub, call);
177
178         /*
179          * When enabling graph tracer for the first time, ftrace_graph_call
180          * should contains a call to ftrace_stub.  Once it has been disabled,
181          * the 8-bytes at the position becomes NOPs.
182          */
183         if (init_graph) {
184                 ret = ftrace_check_current_call((unsigned long)&ftrace_graph_call,
185                                                 call);
186                 init_graph = 0;
187         } else {
188                 ret = ftrace_check_current_call((unsigned long)&ftrace_graph_call,
189                                                 NULL);
190         }
191
192         if (ret)
193                 return ret;
194
195         return __ftrace_modify_call((unsigned long)&ftrace_graph_call,
196                                     (unsigned long)&prepare_ftrace_return, true);
197 }
198
199 int ftrace_disable_ftrace_graph_caller(void)
200 {
201         unsigned int call[2];
202         int ret;
203
204         make_call(&ftrace_graph_call, &prepare_ftrace_return, call);
205
206         /*
207          * This is to make sure that ftrace_enable_ftrace_graph_caller
208          * did the right thing.
209          */
210         ret = ftrace_check_current_call((unsigned long)&ftrace_graph_call,
211                                         call);
212
213         if (ret)
214                 return ret;
215
216         return __ftrace_modify_call((unsigned long)&ftrace_graph_call,
217                                     (unsigned long)&prepare_ftrace_return, false);
218 }
219 #endif /* CONFIG_DYNAMIC_FTRACE */
220 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */