1 // SPDX-License-Identifier: GPL-2.0
3 * Dynamic function tracer architecture backend.
5 * Copyright IBM Corp. 2009,2014
7 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>,
8 * Martin Schwidefsky <schwidefsky@de.ibm.com>
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 int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
168 static void brcl_disable(void *brcl)
170 u8 op = 0x04; /* set mask field to zero */
172 s390_kernel_write((char *)brcl + 1, &op, sizeof(op));
175 int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
178 brcl_disable((void *)rec->ip);
182 static void brcl_enable(void *brcl)
184 u8 op = 0xf4; /* set mask field to all ones */
186 s390_kernel_write((char *)brcl + 1, &op, sizeof(op));
189 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
191 brcl_enable((void *)rec->ip);
195 int ftrace_update_ftrace_func(ftrace_func_t func)
201 void arch_ftrace_update_code(int command)
203 ftrace_modify_all_code(command);
206 int ftrace_arch_code_modify_post_process(void)
209 * Flush any pre-fetched instructions on all
210 * CPUs to make the new code visible.
212 text_poke_sync_lock();
216 #ifdef CONFIG_MODULES
218 static int __init ftrace_plt_init(void)
220 const char *start, *end;
222 ftrace_plt = module_alloc(PAGE_SIZE);
224 panic("cannot allocate ftrace plt\n");
226 start = ftrace_shared_hotpatch_trampoline(&end);
227 memcpy(ftrace_plt, start, end - start);
228 set_memory_ro((unsigned long)ftrace_plt, 1);
231 device_initcall(ftrace_plt_init);
233 #endif /* CONFIG_MODULES */
235 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
237 * Hook the return address and push it in the stack of return addresses
238 * in current thread info.
240 unsigned long prepare_ftrace_return(unsigned long ra, unsigned long sp,
243 if (unlikely(ftrace_graph_is_dead()))
245 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
247 ip -= MCOUNT_INSN_SIZE;
248 if (!function_graph_enter(ra, ip, 0, (void *) sp))
249 ra = (unsigned long) return_to_handler;
253 NOKPROBE_SYMBOL(prepare_ftrace_return);
256 * Patch the kernel code at ftrace_graph_caller location. The instruction
257 * there is branch relative on condition. To enable the ftrace graph code
258 * block, we simply patch the mask field of the instruction to zero and
259 * turn the instruction into a nop.
260 * To disable the ftrace graph code the mask field will be patched to
261 * all ones, which turns the instruction into an unconditional branch.
263 int ftrace_enable_ftrace_graph_caller(void)
265 brcl_disable(ftrace_graph_caller);
266 text_poke_sync_lock();
270 int ftrace_disable_ftrace_graph_caller(void)
272 brcl_enable(ftrace_graph_caller);
273 text_poke_sync_lock();
277 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
279 #ifdef CONFIG_KPROBES_ON_FTRACE
280 void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
281 struct ftrace_ops *ops, struct ftrace_regs *fregs)
283 struct kprobe_ctlblk *kcb;
284 struct pt_regs *regs;
288 bit = ftrace_test_recursion_trylock(ip, parent_ip);
292 regs = ftrace_get_regs(fregs);
293 p = get_kprobe((kprobe_opcode_t *)ip);
294 if (unlikely(!p) || kprobe_disabled(p))
297 if (kprobe_running()) {
298 kprobes_inc_nmissed_count(p);
302 __this_cpu_write(current_kprobe, p);
304 kcb = get_kprobe_ctlblk();
305 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
307 instruction_pointer_set(regs, ip);
309 if (!p->pre_handler || !p->pre_handler(p, regs)) {
311 instruction_pointer_set(regs, ip + MCOUNT_INSN_SIZE);
313 if (unlikely(p->post_handler)) {
314 kcb->kprobe_status = KPROBE_HIT_SSDONE;
315 p->post_handler(p, regs, 0);
318 __this_cpu_write(current_kprobe, NULL);
320 ftrace_test_recursion_unlock(bit);
322 NOKPROBE_SYMBOL(kprobe_ftrace_handler);
324 int arch_prepare_kprobe_ftrace(struct kprobe *p)
326 p->ainsn.insn = NULL;