• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Dynamic function tracer architecture backend.
4  *
5  * Copyright IBM Corp. 2009,2014
6  *
7  *   Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>,
8  *		Martin Schwidefsky <schwidefsky@de.ibm.com>
9  */
10 
11 #include <linux/moduleloader.h>
12 #include <linux/hardirq.h>
13 #include <linux/uaccess.h>
14 #include <linux/ftrace.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/kprobes.h>
18 #include <trace/syscall.h>
19 #include <asm/asm-offsets.h>
20 #include <asm/cacheflush.h>
21 #include <asm/ftrace.lds.h>
22 #include <asm/nospec-branch.h>
23 #include <asm/set_memory.h>
24 #include "entry.h"
25 #include "ftrace.h"
26 
27 /*
28  * To generate function prologue either gcc's hotpatch feature (since gcc 4.8)
29  * or a combination of -pg -mrecord-mcount -mnop-mcount -mfentry flags
30  * (since gcc 9 / clang 10) is used.
31  * In both cases the original and also the disabled function prologue contains
32  * only a single six byte instruction and looks like this:
33  * >	brcl	0,0			# offset 0
34  * To enable ftrace the code gets patched like above and afterwards looks
35  * like this:
36  * >	brasl	%r0,ftrace_caller	# offset 0
37  *
38  * The instruction will be patched by ftrace_make_call / ftrace_make_nop.
39  * The ftrace function gets called with a non-standard C function call ABI
40  * where r0 contains the return address. It is also expected that the called
41  * function only clobbers r0 and r1, but restores r2-r15.
42  * For module code we can't directly jump to ftrace caller, but need a
43  * trampoline (ftrace_plt), which clobbers also r1.
44  */
45 
46 void *ftrace_func __read_mostly = ftrace_stub;
47 struct ftrace_insn {
48 	u16 opc;
49 	s32 disp;
50 } __packed;
51 
52 asm(
53 	"	.align 16\n"
54 	"ftrace_shared_hotpatch_trampoline_br:\n"
55 	"	lmg	%r0,%r1,2(%r1)\n"
56 	"	br	%r1\n"
57 	"ftrace_shared_hotpatch_trampoline_br_end:\n"
58 );
59 
60 #ifdef CONFIG_EXPOLINE
61 asm(
62 	"	.align 16\n"
63 	"ftrace_shared_hotpatch_trampoline_ex:\n"
64 	"	lmg	%r0,%r1,2(%r1)\n"
65 	"	ex	%r0," __stringify(__LC_BR_R1) "(%r0)\n"
66 	"	j	.\n"
67 	"ftrace_shared_hotpatch_trampoline_ex_end:\n"
68 );
69 
70 asm(
71 	"	.align 16\n"
72 	"ftrace_shared_hotpatch_trampoline_exrl:\n"
73 	"	lmg	%r0,%r1,2(%r1)\n"
74 	"	.insn	ril,0xc60000000000,%r0,0f\n" /* exrl */
75 	"	j	.\n"
76 	"0:	br	%r1\n"
77 	"ftrace_shared_hotpatch_trampoline_exrl_end:\n"
78 );
79 #endif /* CONFIG_EXPOLINE */
80 
81 #ifdef CONFIG_MODULES
82 static char *ftrace_plt;
83 #endif /* CONFIG_MODULES */
84 
ftrace_shared_hotpatch_trampoline(const char ** end)85 static const char *ftrace_shared_hotpatch_trampoline(const char **end)
86 {
87 	const char *tstart, *tend;
88 
89 	tstart = ftrace_shared_hotpatch_trampoline_br;
90 	tend = ftrace_shared_hotpatch_trampoline_br_end;
91 #ifdef CONFIG_EXPOLINE
92 	if (!nospec_disable) {
93 		tstart = ftrace_shared_hotpatch_trampoline_ex;
94 		tend = ftrace_shared_hotpatch_trampoline_ex_end;
95 		if (test_facility(35)) { /* exrl */
96 			tstart = ftrace_shared_hotpatch_trampoline_exrl;
97 			tend = ftrace_shared_hotpatch_trampoline_exrl_end;
98 		}
99 	}
100 #endif /* CONFIG_EXPOLINE */
101 	if (end)
102 		*end = tend;
103 	return tstart;
104 }
105 
ftrace_need_init_nop(void)106 bool ftrace_need_init_nop(void)
107 {
108 	return true;
109 }
110 
ftrace_init_nop(struct module * mod,struct dyn_ftrace * rec)111 int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
112 {
113 	static struct ftrace_hotpatch_trampoline *next_vmlinux_trampoline =
114 		__ftrace_hotpatch_trampolines_start;
115 	static const char orig[6] = { 0xc0, 0x04, 0x00, 0x00, 0x00, 0x00 };
116 	static struct ftrace_hotpatch_trampoline *trampoline;
117 	struct ftrace_hotpatch_trampoline **next_trampoline;
118 	struct ftrace_hotpatch_trampoline *trampolines_end;
119 	struct ftrace_hotpatch_trampoline tmp;
120 	struct ftrace_insn *insn;
121 	const char *shared;
122 	s32 disp;
123 
124 	BUILD_BUG_ON(sizeof(struct ftrace_hotpatch_trampoline) !=
125 		     SIZEOF_FTRACE_HOTPATCH_TRAMPOLINE);
126 
127 	next_trampoline = &next_vmlinux_trampoline;
128 	trampolines_end = __ftrace_hotpatch_trampolines_end;
129 	shared = ftrace_shared_hotpatch_trampoline(NULL);
130 #ifdef CONFIG_MODULES
131 	if (mod) {
132 		next_trampoline = &mod->arch.next_trampoline;
133 		trampolines_end = mod->arch.trampolines_end;
134 		shared = ftrace_plt;
135 	}
136 #endif
137 
138 	if (WARN_ON_ONCE(*next_trampoline >= trampolines_end))
139 		return -ENOMEM;
140 	trampoline = (*next_trampoline)++;
141 
142 	/* Check for the compiler-generated fentry nop (brcl 0, .). */
143 	if (WARN_ON_ONCE(memcmp((const void *)rec->ip, &orig, sizeof(orig))))
144 		return -EINVAL;
145 
146 	/* Generate the trampoline. */
147 	tmp.brasl_opc = 0xc015; /* brasl %r1, shared */
148 	tmp.brasl_disp = (shared - (const char *)&trampoline->brasl_opc) / 2;
149 	tmp.interceptor = FTRACE_ADDR;
150 	tmp.rest_of_intercepted_function = rec->ip + sizeof(struct ftrace_insn);
151 	s390_kernel_write(trampoline, &tmp, sizeof(tmp));
152 
153 	/* Generate a jump to the trampoline. */
154 	disp = ((char *)trampoline - (char *)rec->ip) / 2;
155 	insn = (struct ftrace_insn *)rec->ip;
156 	s390_kernel_write(&insn->disp, &disp, sizeof(disp));
157 
158 	return 0;
159 }
160 
ftrace_modify_call(struct dyn_ftrace * rec,unsigned long old_addr,unsigned long addr)161 int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
162 		       unsigned long addr)
163 {
164 	return 0;
165 }
166 
brcl_disable(void * brcl)167 static void brcl_disable(void *brcl)
168 {
169 	u8 op = 0x04; /* set mask field to zero */
170 
171 	s390_kernel_write((char *)brcl + 1, &op, sizeof(op));
172 }
173 
ftrace_make_nop(struct module * mod,struct dyn_ftrace * rec,unsigned long addr)174 int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
175 		    unsigned long addr)
176 {
177 	brcl_disable((void *)rec->ip);
178 	return 0;
179 }
180 
brcl_enable(void * brcl)181 static void brcl_enable(void *brcl)
182 {
183 	u8 op = 0xf4; /* set mask field to all ones */
184 
185 	s390_kernel_write((char *)brcl + 1, &op, sizeof(op));
186 }
187 
ftrace_make_call(struct dyn_ftrace * rec,unsigned long addr)188 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
189 {
190 	brcl_enable((void *)rec->ip);
191 	return 0;
192 }
193 
ftrace_update_ftrace_func(ftrace_func_t func)194 int ftrace_update_ftrace_func(ftrace_func_t func)
195 {
196 	ftrace_func = func;
197 	return 0;
198 }
199 
ftrace_dyn_arch_init(void)200 int __init ftrace_dyn_arch_init(void)
201 {
202 	return 0;
203 }
204 
arch_ftrace_update_code(int command)205 void arch_ftrace_update_code(int command)
206 {
207 	ftrace_modify_all_code(command);
208 }
209 
__ftrace_sync(void * dummy)210 static void __ftrace_sync(void *dummy)
211 {
212 }
213 
ftrace_arch_code_modify_post_process(void)214 int ftrace_arch_code_modify_post_process(void)
215 {
216 	/* Send SIGP to the other CPUs, so they see the new code. */
217 	smp_call_function(__ftrace_sync, NULL, 1);
218 	return 0;
219 }
220 
221 #ifdef CONFIG_MODULES
222 
ftrace_plt_init(void)223 static int __init ftrace_plt_init(void)
224 {
225 	const char *start, *end;
226 
227 	ftrace_plt = module_alloc(PAGE_SIZE);
228 	if (!ftrace_plt)
229 		panic("cannot allocate ftrace plt\n");
230 
231 	start = ftrace_shared_hotpatch_trampoline(&end);
232 	memcpy(ftrace_plt, start, end - start);
233 	set_memory_ro((unsigned long)ftrace_plt, 1);
234 	return 0;
235 }
236 device_initcall(ftrace_plt_init);
237 
238 #endif /* CONFIG_MODULES */
239 
240 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
241 /*
242  * Hook the return address and push it in the stack of return addresses
243  * in current thread info.
244  */
prepare_ftrace_return(unsigned long ra,unsigned long sp,unsigned long ip)245 unsigned long prepare_ftrace_return(unsigned long ra, unsigned long sp,
246 				    unsigned long ip)
247 {
248 	if (unlikely(ftrace_graph_is_dead()))
249 		goto out;
250 	if (unlikely(atomic_read(&current->tracing_graph_pause)))
251 		goto out;
252 	ip -= MCOUNT_INSN_SIZE;
253 	if (!function_graph_enter(ra, ip, 0, (void *) sp))
254 		ra = (unsigned long) return_to_handler;
255 out:
256 	return ra;
257 }
258 NOKPROBE_SYMBOL(prepare_ftrace_return);
259 
260 /*
261  * Patch the kernel code at ftrace_graph_caller location. The instruction
262  * there is branch relative on condition. To enable the ftrace graph code
263  * block, we simply patch the mask field of the instruction to zero and
264  * turn the instruction into a nop.
265  * To disable the ftrace graph code the mask field will be patched to
266  * all ones, which turns the instruction into an unconditional branch.
267  */
ftrace_enable_ftrace_graph_caller(void)268 int ftrace_enable_ftrace_graph_caller(void)
269 {
270 	brcl_disable(ftrace_graph_caller);
271 	return 0;
272 }
273 
ftrace_disable_ftrace_graph_caller(void)274 int ftrace_disable_ftrace_graph_caller(void)
275 {
276 	brcl_enable(ftrace_graph_caller);
277 	return 0;
278 }
279 
280 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
281 
282 #ifdef CONFIG_KPROBES_ON_FTRACE
kprobe_ftrace_handler(unsigned long ip,unsigned long parent_ip,struct ftrace_ops * ops,struct ftrace_regs * fregs)283 void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
284 		struct ftrace_ops *ops, struct ftrace_regs *fregs)
285 {
286 	struct kprobe_ctlblk *kcb;
287 	struct pt_regs *regs;
288 	struct kprobe *p;
289 	int bit;
290 
291 	bit = ftrace_test_recursion_trylock(ip, parent_ip);
292 	if (bit < 0)
293 		return;
294 
295 	regs = ftrace_get_regs(fregs);
296 	preempt_disable_notrace();
297 	p = get_kprobe((kprobe_opcode_t *)ip);
298 	if (unlikely(!p) || kprobe_disabled(p))
299 		goto out;
300 
301 	if (kprobe_running()) {
302 		kprobes_inc_nmissed_count(p);
303 		goto out;
304 	}
305 
306 	__this_cpu_write(current_kprobe, p);
307 
308 	kcb = get_kprobe_ctlblk();
309 	kcb->kprobe_status = KPROBE_HIT_ACTIVE;
310 
311 	instruction_pointer_set(regs, ip);
312 
313 	if (!p->pre_handler || !p->pre_handler(p, regs)) {
314 
315 		instruction_pointer_set(regs, ip + MCOUNT_INSN_SIZE);
316 
317 		if (unlikely(p->post_handler)) {
318 			kcb->kprobe_status = KPROBE_HIT_SSDONE;
319 			p->post_handler(p, regs, 0);
320 		}
321 	}
322 	__this_cpu_write(current_kprobe, NULL);
323 out:
324 	preempt_enable_notrace();
325 	ftrace_test_recursion_unlock(bit);
326 }
327 NOKPROBE_SYMBOL(kprobe_ftrace_handler);
328 
arch_prepare_kprobe_ftrace(struct kprobe * p)329 int arch_prepare_kprobe_ftrace(struct kprobe *p)
330 {
331 	p->ainsn.insn = NULL;
332 	return 0;
333 }
334 #endif
335