1 // SPDX-License-Identifier: GPL-2.0
3 * Dynamic function tracer architecture backend.
5 * Copyright IBM Corp. 2009,2014
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/text-patching.h>
21 #include <asm/cacheflush.h>
22 #include <asm/ftrace.lds.h>
23 #include <asm/nospec-branch.h>
24 #include <asm/set_memory.h>
29 * To generate function prologue either gcc's hotpatch feature (since gcc 4.8)
30 * or a combination of -pg -mrecord-mcount -mnop-mcount -mfentry flags
31 * (since gcc 9 / clang 10) is used.
32 * In both cases the original and also the disabled function prologue contains
33 * only a single six byte instruction and looks like this:
34 * > brcl 0,0 # offset 0
35 * To enable ftrace the code gets patched like above and afterwards looks
37 * > brasl %r0,ftrace_caller # offset 0
39 * The instruction will be patched by ftrace_make_call / ftrace_make_nop.
40 * The ftrace function gets called with a non-standard C function call ABI
41 * where r0 contains the return address. It is also expected that the called
42 * function only clobbers r0 and r1, but restores r2-r15.
43 * For module code we can't directly jump to ftrace caller, but need a
44 * trampoline (ftrace_plt), which clobbers also r1.
47 void *ftrace_func __read_mostly = ftrace_stub;
55 "ftrace_shared_hotpatch_trampoline_br:\n"
56 " lmg %r0,%r1,2(%r1)\n"
58 "ftrace_shared_hotpatch_trampoline_br_end:\n"
61 #ifdef CONFIG_EXPOLINE
64 "ftrace_shared_hotpatch_trampoline_ex:\n"
65 " lmg %r0,%r1,2(%r1)\n"
66 " ex %r0," __stringify(__LC_BR_R1) "(%r0)\n"
68 "ftrace_shared_hotpatch_trampoline_ex_end:\n"
73 "ftrace_shared_hotpatch_trampoline_exrl:\n"
74 " lmg %r0,%r1,2(%r1)\n"
75 " .insn ril,0xc60000000000,%r0,0f\n" /* exrl */
78 "ftrace_shared_hotpatch_trampoline_exrl_end:\n"
80 #endif /* CONFIG_EXPOLINE */
83 static char *ftrace_plt;
84 #endif /* CONFIG_MODULES */
86 static const char *ftrace_shared_hotpatch_trampoline(const char **end)
88 const char *tstart, *tend;
90 tstart = ftrace_shared_hotpatch_trampoline_br;
91 tend = ftrace_shared_hotpatch_trampoline_br_end;
92 #ifdef CONFIG_EXPOLINE
93 if (!nospec_disable) {
94 tstart = ftrace_shared_hotpatch_trampoline_ex;
95 tend = ftrace_shared_hotpatch_trampoline_ex_end;
96 if (test_facility(35)) { /* exrl */
97 tstart = ftrace_shared_hotpatch_trampoline_exrl;
98 tend = ftrace_shared_hotpatch_trampoline_exrl_end;
101 #endif /* CONFIG_EXPOLINE */
107 bool ftrace_need_init_nop(void)
112 int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
114 static struct ftrace_hotpatch_trampoline *next_vmlinux_trampoline =
115 __ftrace_hotpatch_trampolines_start;
116 static const char orig[6] = { 0xc0, 0x04, 0x00, 0x00, 0x00, 0x00 };
117 static struct ftrace_hotpatch_trampoline *trampoline;
118 struct ftrace_hotpatch_trampoline **next_trampoline;
119 struct ftrace_hotpatch_trampoline *trampolines_end;
120 struct ftrace_hotpatch_trampoline tmp;
121 struct ftrace_insn *insn;
125 BUILD_BUG_ON(sizeof(struct ftrace_hotpatch_trampoline) !=
126 SIZEOF_FTRACE_HOTPATCH_TRAMPOLINE);
128 next_trampoline = &next_vmlinux_trampoline;
129 trampolines_end = __ftrace_hotpatch_trampolines_end;
130 shared = ftrace_shared_hotpatch_trampoline(NULL);
131 #ifdef CONFIG_MODULES
133 next_trampoline = &mod->arch.next_trampoline;
134 trampolines_end = mod->arch.trampolines_end;
139 if (WARN_ON_ONCE(*next_trampoline >= trampolines_end))
141 trampoline = (*next_trampoline)++;
143 /* Check for the compiler-generated fentry nop (brcl 0, .). */
144 if (WARN_ON_ONCE(memcmp((const void *)rec->ip, &orig, sizeof(orig))))
147 /* Generate the trampoline. */
148 tmp.brasl_opc = 0xc015; /* brasl %r1, shared */
149 tmp.brasl_disp = (shared - (const char *)&trampoline->brasl_opc) / 2;
150 tmp.interceptor = FTRACE_ADDR;
151 tmp.rest_of_intercepted_function = rec->ip + sizeof(struct ftrace_insn);
152 s390_kernel_write(trampoline, &tmp, sizeof(tmp));
154 /* Generate a jump to the trampoline. */
155 disp = ((char *)trampoline - (char *)rec->ip) / 2;
156 insn = (struct ftrace_insn *)rec->ip;
157 s390_kernel_write(&insn->disp, &disp, sizeof(disp));
162 static struct ftrace_hotpatch_trampoline *ftrace_get_trampoline(struct dyn_ftrace *rec)
164 struct ftrace_hotpatch_trampoline *trampoline;
165 struct ftrace_insn insn;
169 if (copy_from_kernel_nofault(&insn, (void *)rec->ip, sizeof(insn)))
170 return ERR_PTR(-EFAULT);
171 disp = (s64)insn.disp * 2;
172 trampoline = (void *)(rec->ip + disp);
173 if (get_kernel_nofault(opc, &trampoline->brasl_opc))
174 return ERR_PTR(-EFAULT);
176 return ERR_PTR(-EINVAL);
180 int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
183 struct ftrace_hotpatch_trampoline *trampoline;
186 trampoline = ftrace_get_trampoline(rec);
187 if (IS_ERR(trampoline))
188 return PTR_ERR(trampoline);
189 if (get_kernel_nofault(old, &trampoline->interceptor))
193 s390_kernel_write(&trampoline->interceptor, &addr, sizeof(addr));
197 static void brcl_disable(void *brcl)
199 u8 op = 0x04; /* set mask field to zero */
201 s390_kernel_write((char *)brcl + 1, &op, sizeof(op));
204 int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
207 brcl_disable((void *)rec->ip);
211 static void brcl_enable(void *brcl)
213 u8 op = 0xf4; /* set mask field to all ones */
215 s390_kernel_write((char *)brcl + 1, &op, sizeof(op));
218 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
220 struct ftrace_hotpatch_trampoline *trampoline;
222 trampoline = ftrace_get_trampoline(rec);
223 if (IS_ERR(trampoline))
224 return PTR_ERR(trampoline);
225 s390_kernel_write(&trampoline->interceptor, &addr, sizeof(addr));
226 brcl_enable((void *)rec->ip);
230 int ftrace_update_ftrace_func(ftrace_func_t func)
236 void arch_ftrace_update_code(int command)
238 ftrace_modify_all_code(command);
241 int ftrace_arch_code_modify_post_process(void)
244 * Flush any pre-fetched instructions on all
245 * CPUs to make the new code visible.
247 text_poke_sync_lock();
251 #ifdef CONFIG_MODULES
253 static int __init ftrace_plt_init(void)
255 const char *start, *end;
257 ftrace_plt = module_alloc(PAGE_SIZE);
259 panic("cannot allocate ftrace plt\n");
261 start = ftrace_shared_hotpatch_trampoline(&end);
262 memcpy(ftrace_plt, start, end - start);
263 set_memory_ro((unsigned long)ftrace_plt, 1);
266 device_initcall(ftrace_plt_init);
268 #endif /* CONFIG_MODULES */
270 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
272 * Hook the return address and push it in the stack of return addresses
273 * in current thread info.
275 unsigned long prepare_ftrace_return(unsigned long ra, unsigned long sp,
278 if (unlikely(ftrace_graph_is_dead()))
280 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
282 ip -= MCOUNT_INSN_SIZE;
283 if (!function_graph_enter(ra, ip, 0, (void *) sp))
284 ra = (unsigned long) return_to_handler;
288 NOKPROBE_SYMBOL(prepare_ftrace_return);
291 * Patch the kernel code at ftrace_graph_caller location. The instruction
292 * there is branch relative on condition. To enable the ftrace graph code
293 * block, we simply patch the mask field of the instruction to zero and
294 * turn the instruction into a nop.
295 * To disable the ftrace graph code the mask field will be patched to
296 * all ones, which turns the instruction into an unconditional branch.
298 int ftrace_enable_ftrace_graph_caller(void)
300 brcl_disable(ftrace_graph_caller);
301 text_poke_sync_lock();
305 int ftrace_disable_ftrace_graph_caller(void)
307 brcl_enable(ftrace_graph_caller);
308 text_poke_sync_lock();
312 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
314 #ifdef CONFIG_KPROBES_ON_FTRACE
315 void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
316 struct ftrace_ops *ops, struct ftrace_regs *fregs)
318 struct kprobe_ctlblk *kcb;
319 struct pt_regs *regs;
323 bit = ftrace_test_recursion_trylock(ip, parent_ip);
327 regs = ftrace_get_regs(fregs);
328 p = get_kprobe((kprobe_opcode_t *)ip);
329 if (!regs || unlikely(!p) || kprobe_disabled(p))
332 if (kprobe_running()) {
333 kprobes_inc_nmissed_count(p);
337 __this_cpu_write(current_kprobe, p);
339 kcb = get_kprobe_ctlblk();
340 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
342 instruction_pointer_set(regs, ip);
344 if (!p->pre_handler || !p->pre_handler(p, regs)) {
346 instruction_pointer_set(regs, ip + MCOUNT_INSN_SIZE);
348 if (unlikely(p->post_handler)) {
349 kcb->kprobe_status = KPROBE_HIT_SSDONE;
350 p->post_handler(p, regs, 0);
353 __this_cpu_write(current_kprobe, NULL);
355 ftrace_test_recursion_unlock(bit);
357 NOKPROBE_SYMBOL(kprobe_ftrace_handler);
359 int arch_prepare_kprobe_ftrace(struct kprobe *p)
361 p->ainsn.insn = NULL;