1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <asm/cacheflush.h>
15#include <linux/ftrace.h>
16
17#ifdef CONFIG_FUNCTION_GRAPH_TRACER
18
19
20
21
22void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
23{
24 unsigned long old;
25 int faulted;
26 unsigned long return_hooker = (unsigned long)
27 &return_to_handler;
28
29 if (unlikely(ftrace_graph_is_dead()))
30 return;
31
32 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
33 return;
34
35
36
37
38
39
40 asm volatile(" 1: lwi %0, %2, 0;" \
41 "2: swi %3, %2, 0;" \
42 " addik %1, r0, 0;" \
43 "3:" \
44 " .section .fixup, \"ax\";" \
45 "4: brid 3b;" \
46 " addik %1, r0, 1;" \
47 " .previous;" \
48 " .section __ex_table,\"a\";" \
49 " .word 1b,4b;" \
50 " .word 2b,4b;" \
51 " .previous;" \
52 : "=&r" (old), "=r" (faulted)
53 : "r" (parent), "r" (return_hooker)
54 );
55
56 flush_dcache_range((u32)parent, (u32)parent + 4);
57 flush_icache_range((u32)parent, (u32)parent + 4);
58
59 if (unlikely(faulted)) {
60 ftrace_graph_stop();
61 WARN_ON(1);
62 return;
63 }
64
65 if (function_graph_enter(old, self_addr, 0, NULL))
66 *parent = old;
67}
68#endif
69
70#ifdef CONFIG_DYNAMIC_FTRACE
71
72static int ftrace_modify_code(unsigned long addr, unsigned int value)
73{
74 int faulted = 0;
75
76 __asm__ __volatile__(" 1: swi %2, %1, 0;" \
77 " addik %0, r0, 0;" \
78 "2:" \
79 " .section .fixup, \"ax\";" \
80 "3: brid 2b;" \
81 " addik %0, r0, 1;" \
82 " .previous;" \
83 " .section __ex_table,\"a\";" \
84 " .word 1b,3b;" \
85 " .previous;" \
86 : "=r" (faulted)
87 : "r" (addr), "r" (value)
88 );
89
90 if (unlikely(faulted))
91 return -EFAULT;
92
93 flush_dcache_range(addr, addr + 4);
94 flush_icache_range(addr, addr + 4);
95
96 return 0;
97}
98
99#define MICROBLAZE_NOP 0x80000000
100#define MICROBLAZE_BRI 0xb800000C
101
102static unsigned int recorded;
103static unsigned int imm;
104
105
106#undef USE_FTRACE_NOP
107
108#ifdef USE_FTRACE_NOP
109static unsigned int bralid;
110#endif
111
112int ftrace_make_nop(struct module *mod,
113 struct dyn_ftrace *rec, unsigned long addr)
114{
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 int ret = 0;
132
133 if (recorded == 0) {
134 recorded = 1;
135 imm = *(unsigned int *)rec->ip;
136 pr_debug("%s: imm:0x%x\n", __func__, imm);
137#ifdef USE_FTRACE_NOP
138 bralid = *(unsigned int *)(rec->ip + 4);
139 pr_debug("%s: bralid 0x%x\n", __func__, bralid);
140#endif
141 }
142
143#ifdef USE_FTRACE_NOP
144 ret = ftrace_modify_code(rec->ip, MICROBLAZE_NOP);
145 ret += ftrace_modify_code(rec->ip + 4, MICROBLAZE_NOP);
146#else
147 ret = ftrace_modify_code(rec->ip, MICROBLAZE_BRI);
148#endif
149 return ret;
150}
151
152
153int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
154{
155 int ret;
156 pr_debug("%s: addr:0x%x, rec->ip: 0x%x, imm:0x%x\n",
157 __func__, (unsigned int)addr, (unsigned int)rec->ip, imm);
158 ret = ftrace_modify_code(rec->ip, imm);
159#ifdef USE_FTRACE_NOP
160 pr_debug("%s: bralid:0x%x\n", __func__, bralid);
161 ret += ftrace_modify_code(rec->ip + 4, bralid);
162#endif
163 return ret;
164}
165
166int __init ftrace_dyn_arch_init(void)
167{
168 return 0;
169}
170
171int ftrace_update_ftrace_func(ftrace_func_t func)
172{
173 unsigned long ip = (unsigned long)(&ftrace_call);
174 unsigned int upper = (unsigned int)func;
175 unsigned int lower = (unsigned int)func;
176 int ret = 0;
177
178
179 upper = 0xb0000000 + (upper >> 16);
180 lower = 0x32800000 + (lower & 0xFFFF);
181
182 pr_debug("%s: func=0x%x, ip=0x%x, upper=0x%x, lower=0x%x\n",
183 __func__, (unsigned int)func, (unsigned int)ip, upper, lower);
184
185
186 ret = ftrace_modify_code(ip, upper);
187 ret += ftrace_modify_code(ip + 4, lower);
188
189
190 ret += ftrace_modify_code((unsigned long)&ftrace_caller,
191 MICROBLAZE_NOP);
192
193 return ret;
194}
195
196#ifdef CONFIG_FUNCTION_GRAPH_TRACER
197unsigned int old_jump;
198
199int ftrace_enable_ftrace_graph_caller(void)
200{
201 unsigned int ret;
202 unsigned long ip = (unsigned long)(&ftrace_call_graph);
203
204 old_jump = *(unsigned int *)ip;
205 ret = ftrace_modify_code(ip, MICROBLAZE_NOP);
206
207 pr_debug("%s: Replace instruction: 0x%x\n", __func__, old_jump);
208 return ret;
209}
210
211int ftrace_disable_ftrace_graph_caller(void)
212{
213 unsigned int ret;
214 unsigned long ip = (unsigned long)(&ftrace_call_graph);
215
216 ret = ftrace_modify_code(ip, old_jump);
217
218 pr_debug("%s\n", __func__);
219 return ret;
220}
221#endif
222#endif
223