1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/spinlock.h>
3 #include <linux/hardirq.h>
4 #include <linux/ftrace.h>
5 #include <linux/percpu.h>
6 #include <linux/init.h>
7 #include <linux/list.h>
8 #include <trace/syscall.h>
10 #include <asm/ftrace.h>
12 #ifdef CONFIG_DYNAMIC_FTRACE
13 static const u32 ftrace_nop = 0x01000000;
15 static u32 ftrace_call_replace(unsigned long ip, unsigned long addr)
20 off = ((s32)addr - (s32)ip);
21 call = 0x40000000 | ((u32)off >> 2);
26 static int ftrace_modify_code(unsigned long ip, u32 old, u32 new)
32 "1: cas [%[ip]], %[old], %[new]\n"
34 " mov 0, %[faulted]\n"
36 " .section .fixup,#alloc,#execinstr\n"
38 "3: sethi %%hi(2b), %[faulted]\n"
39 " jmpl %[faulted] + %%lo(2b), %%g0\n"
40 " mov 1, %[faulted]\n"
42 " .section __ex_table,\"a\"\n"
46 : "=r" (replaced), [faulted] "=r" (faulted)
47 : [new] "0" (new), [old] "r" (old), [ip] "r" (ip)
50 if (replaced != old && replaced != new)
56 int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsigned long addr)
58 unsigned long ip = rec->ip;
61 old = ftrace_call_replace(ip, addr);
63 return ftrace_modify_code(ip, old, new);
66 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
68 unsigned long ip = rec->ip;
72 new = ftrace_call_replace(ip, addr);
73 return ftrace_modify_code(ip, old, new);
76 int ftrace_update_ftrace_func(ftrace_func_t func)
78 unsigned long ip = (unsigned long)(&ftrace_call);
81 old = *(u32 *) &ftrace_call;
82 new = ftrace_call_replace(ip, (unsigned long)func);
83 return ftrace_modify_code(ip, old, new);
86 int __init ftrace_dyn_arch_init(void)
92 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
94 #ifdef CONFIG_DYNAMIC_FTRACE
95 extern void ftrace_graph_call(void);
97 int ftrace_enable_ftrace_graph_caller(void)
99 unsigned long ip = (unsigned long)(&ftrace_graph_call);
102 old = *(u32 *) &ftrace_graph_call;
103 new = ftrace_call_replace(ip, (unsigned long) &ftrace_graph_caller);
104 return ftrace_modify_code(ip, old, new);
107 int ftrace_disable_ftrace_graph_caller(void)
109 unsigned long ip = (unsigned long)(&ftrace_graph_call);
112 old = *(u32 *) &ftrace_graph_call;
113 new = ftrace_call_replace(ip, (unsigned long) &ftrace_stub);
115 return ftrace_modify_code(ip, old, new);
118 #endif /* !CONFIG_DYNAMIC_FTRACE */
121 * Hook the return address and push it in the stack of return addrs
122 * in current thread info.
124 unsigned long prepare_ftrace_return(unsigned long parent,
125 unsigned long self_addr,
126 unsigned long frame_pointer)
128 unsigned long return_hooker = (unsigned long) &return_to_handler;
129 struct ftrace_graph_ent trace;
131 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
134 trace.func = self_addr;
135 trace.depth = current->curr_ret_stack + 1;
137 /* Only trace if the calling function expects to */
138 if (!ftrace_graph_entry(&trace))
141 if (ftrace_push_return_trace(parent, self_addr, &trace.depth,
142 frame_pointer, NULL) == -EBUSY)
145 return return_hooker;
147 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */