1 // SPDX-License-Identifier: GPL-2.0
3 * Code for tracing calls in Linux kernel.
4 * Copyright (C) 2009-2016 Helge Deller <deller@gmx.de>
6 * based on code for x86 which is:
7 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
9 * future possible enhancements:
10 * - add CONFIG_STACK_TRACER
13 #include <linux/init.h>
14 #include <linux/ftrace.h>
15 #include <linux/uaccess.h>
16 #include <linux/kprobes.h>
17 #include <linux/ptrace.h>
19 #include <asm/assembly.h>
20 #include <asm/sections.h>
21 #include <asm/ftrace.h>
22 #include <asm/patch.h>
24 #define __hot __section(".text.hot")
26 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
28 * Hook the return address and push it in the stack of return addrs
29 * in current thread info.
31 static void __hot prepare_ftrace_return(unsigned long *parent,
32 unsigned long self_addr)
35 extern int parisc_return_to_handler;
37 if (unlikely(ftrace_graph_is_dead()))
40 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
45 if (!function_graph_enter(old, self_addr, 0, NULL))
46 /* activate parisc_return_to_handler() as return point */
47 *parent = (unsigned long) &parisc_return_to_handler;
49 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
51 void notrace __hot ftrace_function_trampoline(unsigned long parent,
52 unsigned long self_addr,
53 unsigned long org_sp_gr3,
54 struct ftrace_regs *fregs)
56 #ifndef CONFIG_DYNAMIC_FTRACE
57 extern ftrace_func_t ftrace_trace_function;
59 extern struct ftrace_ops *function_trace_op;
61 if (function_trace_op->flags & FTRACE_OPS_FL_ENABLED &&
62 ftrace_trace_function != ftrace_stub)
63 ftrace_trace_function(self_addr, parent,
64 function_trace_op, fregs);
66 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
67 if (dereference_function_descriptor(ftrace_graph_return) !=
68 dereference_function_descriptor(ftrace_stub) ||
69 ftrace_graph_entry != ftrace_graph_entry_stub) {
70 unsigned long *parent_rp;
72 /* calculate pointer to %rp in stack */
73 parent_rp = (unsigned long *) (org_sp_gr3 - RP_OFFSET);
74 /* sanity check: parent_rp should hold parent */
75 if (*parent_rp != parent)
78 prepare_ftrace_return(parent_rp, self_addr);
84 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
85 int ftrace_enable_ftrace_graph_caller(void)
90 int ftrace_disable_ftrace_graph_caller(void)
96 #ifdef CONFIG_DYNAMIC_FTRACE
98 int __init ftrace_dyn_arch_init(void)
102 int ftrace_update_ftrace_func(ftrace_func_t func)
107 int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
113 unsigned long ftrace_call_adjust(unsigned long addr)
115 return addr+(FTRACE_PATCHABLE_FUNCTION_SIZE-1)*4;
118 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
120 u32 insn[FTRACE_PATCHABLE_FUNCTION_SIZE];
126 unsigned long addr2 =
127 (unsigned long)dereference_function_descriptor((void *)addr);
129 u32 ftrace_trampoline[] = {
130 0x73c10208, /* std,ma r1,100(sp) */
131 0x0c2110c1, /* ldd -10(r1),r1 */
132 0xe820d002, /* bve,n (r1) */
135 0xe83f1fd7, /* b,l,n .-14,r1 */
138 u32 ftrace_trampoline_unaligned[] = {
141 0x37de0200, /* ldo 100(sp),sp */
142 0x73c13e01, /* std r1,-100(sp) */
143 0x34213ff9, /* ldo -4(r1),r1 */
144 0x50213fc1, /* ldd -20(r1),r1 */
145 0xe820d002, /* bve,n (r1) */
146 0xe83f1fcf, /* b,l,n .-20,r1 */
149 BUILD_BUG_ON(ARRAY_SIZE(ftrace_trampoline_unaligned) >
150 FTRACE_PATCHABLE_FUNCTION_SIZE);
152 u32 ftrace_trampoline[] = {
154 0x6fc10080, /* stw,ma r1,40(sp) */
155 0x48213fd1, /* ldw -18(r1),r1 */
156 0xe820c002, /* bv,n r0(r1) */
157 0xe83f1fdf, /* b,l,n .-c,r1 */
161 BUILD_BUG_ON(ARRAY_SIZE(ftrace_trampoline) >
162 FTRACE_PATCHABLE_FUNCTION_SIZE);
164 size = sizeof(ftrace_trampoline);
165 tramp = ftrace_trampoline;
169 size = sizeof(ftrace_trampoline_unaligned);
170 tramp = ftrace_trampoline_unaligned;
174 ip = (void *)(rec->ip + 4 - size);
176 ret = copy_from_kernel_nofault(insn, ip, size);
180 for (i = 0; i < size / 4; i++) {
181 if (insn[i] != INSN_NOP)
185 __patch_text_multiple(ip, tramp, size);
189 int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
192 u32 insn[FTRACE_PATCHABLE_FUNCTION_SIZE];
195 for (i = 0; i < ARRAY_SIZE(insn); i++)
198 __patch_text((void *)rec->ip, INSN_NOP);
199 __patch_text_multiple((void *)rec->ip + 4 - sizeof(insn),
200 insn, sizeof(insn)-4);
205 #ifdef CONFIG_KPROBES_ON_FTRACE
206 void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
207 struct ftrace_ops *ops, struct ftrace_regs *fregs)
209 struct kprobe_ctlblk *kcb;
210 struct pt_regs *regs;
214 bit = ftrace_test_recursion_trylock(ip, parent_ip);
218 regs = ftrace_get_regs(fregs);
219 preempt_disable_notrace();
220 p = get_kprobe((kprobe_opcode_t *)ip);
221 if (unlikely(!p) || kprobe_disabled(p))
224 if (kprobe_running()) {
225 kprobes_inc_nmissed_count(p);
229 __this_cpu_write(current_kprobe, p);
231 kcb = get_kprobe_ctlblk();
232 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
235 regs->iaoq[1] = ip + 4;
237 if (!p->pre_handler || !p->pre_handler(p, regs)) {
238 regs->iaoq[0] = ip + 4;
239 regs->iaoq[1] = ip + 8;
241 if (unlikely(p->post_handler)) {
242 kcb->kprobe_status = KPROBE_HIT_SSDONE;
243 p->post_handler(p, regs, 0);
246 __this_cpu_write(current_kprobe, NULL);
248 preempt_enable_notrace();
249 ftrace_test_recursion_unlock(bit);
251 NOKPROBE_SYMBOL(kprobe_ftrace_handler);
253 int arch_prepare_kprobe_ftrace(struct kprobe *p)
255 p->ainsn.insn = NULL;