• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <linux/memory.h>
11 #include <asm/cacheflush.h>
12 #include <asm/patch.h>
13 
14 #ifdef CONFIG_DYNAMIC_FTRACE
ftrace_arch_code_modify_prepare(void)15 int ftrace_arch_code_modify_prepare(void) __acquires(&text_mutex)
16 {
17 	mutex_lock(&text_mutex);
18 
19 	/*
20 	 * The code sequences we use for ftrace can't be patched while the
21 	 * kernel is running, so we need to use stop_machine() to modify them
22 	 * for now.  This doesn't play nice with text_mutex, we use this flag
23 	 * to elide the check.
24 	 */
25 	riscv_patch_in_stop_machine = true;
26 
27 	return 0;
28 }
29 
ftrace_arch_code_modify_post_process(void)30 int ftrace_arch_code_modify_post_process(void) __releases(&text_mutex)
31 {
32 	riscv_patch_in_stop_machine = false;
33 	mutex_unlock(&text_mutex);
34 	return 0;
35 }
36 
ftrace_check_current_call(unsigned long hook_pos,unsigned int * expected)37 static int ftrace_check_current_call(unsigned long hook_pos,
38 				     unsigned int *expected)
39 {
40 	unsigned int replaced[2];
41 	unsigned int nops[2] = {NOP4, NOP4};
42 
43 	/* we expect nops at the hook position */
44 	if (!expected)
45 		expected = nops;
46 
47 	/*
48 	 * Read the text we want to modify;
49 	 * return must be -EFAULT on read error
50 	 */
51 	if (copy_from_kernel_nofault(replaced, (void *)hook_pos,
52 			MCOUNT_INSN_SIZE))
53 		return -EFAULT;
54 
55 	/*
56 	 * Make sure it is what we expect it to be;
57 	 * return must be -EINVAL on failed comparison
58 	 */
59 	if (memcmp(expected, replaced, sizeof(replaced))) {
60 		pr_err("%p: expected (%08x %08x) but got (%08x %08x)\n",
61 		       (void *)hook_pos, expected[0], expected[1], replaced[0],
62 		       replaced[1]);
63 		return -EINVAL;
64 	}
65 
66 	return 0;
67 }
68 
__ftrace_modify_call(unsigned long hook_pos,unsigned long target,bool enable)69 static int __ftrace_modify_call(unsigned long hook_pos, unsigned long target,
70 				bool enable)
71 {
72 	unsigned int call[2];
73 	unsigned int nops[2] = {NOP4, NOP4};
74 
75 	make_call(hook_pos, target, call);
76 
77 	/* Replace the auipc-jalr pair at once. Return -EPERM on write error. */
78 	if (patch_text_nosync
79 	    ((void *)hook_pos, enable ? call : nops, MCOUNT_INSN_SIZE))
80 		return -EPERM;
81 
82 	return 0;
83 }
84 
ftrace_make_call(struct dyn_ftrace * rec,unsigned long addr)85 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
86 {
87 	int ret = ftrace_check_current_call(rec->ip, NULL);
88 
89 	if (ret)
90 		return ret;
91 
92 	return __ftrace_modify_call(rec->ip, addr, true);
93 }
94 
ftrace_make_nop(struct module * mod,struct dyn_ftrace * rec,unsigned long addr)95 int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
96 		    unsigned long addr)
97 {
98 	unsigned int call[2];
99 	int ret;
100 
101 	make_call(rec->ip, addr, call);
102 	ret = ftrace_check_current_call(rec->ip, call);
103 
104 	if (ret)
105 		return ret;
106 
107 	return __ftrace_modify_call(rec->ip, addr, false);
108 }
109 
110 
111 /*
112  * This is called early on, and isn't wrapped by
113  * ftrace_arch_code_modify_{prepare,post_process}() and therefor doesn't hold
114  * text_mutex, which triggers a lockdep failure.  SMP isn't running so we could
115  * just directly poke the text, but it's simpler to just take the lock
116  * ourselves.
117  */
ftrace_init_nop(struct module * mod,struct dyn_ftrace * rec)118 int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
119 {
120 	int out;
121 
122 	mutex_lock(&text_mutex);
123 	out = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
124 	mutex_unlock(&text_mutex);
125 
126 	return out;
127 }
128 
ftrace_update_ftrace_func(ftrace_func_t func)129 int ftrace_update_ftrace_func(ftrace_func_t func)
130 {
131 	int ret = __ftrace_modify_call((unsigned long)&ftrace_call,
132 				       (unsigned long)func, true);
133 	if (!ret) {
134 		ret = __ftrace_modify_call((unsigned long)&ftrace_regs_call,
135 					   (unsigned long)func, true);
136 	}
137 
138 	return ret;
139 }
140 
ftrace_dyn_arch_init(void)141 int __init ftrace_dyn_arch_init(void)
142 {
143 	return 0;
144 }
145 #endif
146 
147 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
ftrace_modify_call(struct dyn_ftrace * rec,unsigned long old_addr,unsigned long addr)148 int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
149 		       unsigned long addr)
150 {
151 	unsigned int call[2];
152 	int ret;
153 
154 	make_call(rec->ip, old_addr, call);
155 	ret = ftrace_check_current_call(rec->ip, call);
156 
157 	if (ret)
158 		return ret;
159 
160 	return __ftrace_modify_call(rec->ip, addr, true);
161 }
162 #endif
163 
164 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
165 /*
166  * Most of this function is copied from arm64.
167  */
prepare_ftrace_return(unsigned long * parent,unsigned long self_addr,unsigned long frame_pointer)168 void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
169 			   unsigned long frame_pointer)
170 {
171 	unsigned long return_hooker = (unsigned long)&return_to_handler;
172 	unsigned long old;
173 
174 	if (unlikely(atomic_read(&current->tracing_graph_pause)))
175 		return;
176 
177 	/*
178 	 * We don't suffer access faults, so no extra fault-recovery assembly
179 	 * is needed here.
180 	 */
181 	old = *parent;
182 
183 	if (!function_graph_enter(old, self_addr, frame_pointer, parent))
184 		*parent = return_hooker;
185 }
186 
187 #ifdef CONFIG_DYNAMIC_FTRACE
188 extern void ftrace_graph_call(void);
ftrace_enable_ftrace_graph_caller(void)189 int ftrace_enable_ftrace_graph_caller(void)
190 {
191 	unsigned int call[2];
192 	static int init_graph = 1;
193 	int ret;
194 
195 	make_call(&ftrace_graph_call, &ftrace_stub, call);
196 
197 	/*
198 	 * When enabling graph tracer for the first time, ftrace_graph_call
199 	 * should contains a call to ftrace_stub.  Once it has been disabled,
200 	 * the 8-bytes at the position becomes NOPs.
201 	 */
202 	if (init_graph) {
203 		ret = ftrace_check_current_call((unsigned long)&ftrace_graph_call,
204 						call);
205 		init_graph = 0;
206 	} else {
207 		ret = ftrace_check_current_call((unsigned long)&ftrace_graph_call,
208 						NULL);
209 	}
210 
211 	if (ret)
212 		return ret;
213 
214 	return __ftrace_modify_call((unsigned long)&ftrace_graph_call,
215 				    (unsigned long)&prepare_ftrace_return, true);
216 }
217 
ftrace_disable_ftrace_graph_caller(void)218 int ftrace_disable_ftrace_graph_caller(void)
219 {
220 	unsigned int call[2];
221 	int ret;
222 
223 	make_call(&ftrace_graph_call, &prepare_ftrace_return, call);
224 
225 	/*
226 	 * This is to make sure that ftrace_enable_ftrace_graph_caller
227 	 * did the right thing.
228 	 */
229 	ret = ftrace_check_current_call((unsigned long)&ftrace_graph_call,
230 					call);
231 
232 	if (ret)
233 		return ret;
234 
235 	return __ftrace_modify_call((unsigned long)&ftrace_graph_call,
236 				    (unsigned long)&prepare_ftrace_return, false);
237 }
238 #endif /* CONFIG_DYNAMIC_FTRACE */
239 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
240