1
2
3
4
5
6
7
8
9
10#include <linux/moduleloader.h>
11#include <linux/hardirq.h>
12#include <linux/uaccess.h>
13#include <linux/ftrace.h>
14#include <linux/kernel.h>
15#include <linux/types.h>
16#include <linux/kprobes.h>
17#include <trace/syscall.h>
18#include <asm/asm-offsets.h>
19#include <asm/cacheflush.h>
20#include "entry.h"
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58unsigned long ftrace_plt;
59
60static inline void ftrace_generate_orig_insn(struct ftrace_insn *insn)
61{
62#ifdef CC_USING_HOTPATCH
63
64 insn->opc = 0xc004;
65 insn->disp = 0;
66#else
67
68 insn->opc = 0xe3e0;
69 insn->disp = 0xf0080024;
70#endif
71}
72
73static inline int is_kprobe_on_ftrace(struct ftrace_insn *insn)
74{
75#ifdef CONFIG_KPROBES
76 if (insn->opc == BREAKPOINT_INSTRUCTION)
77 return 1;
78#endif
79 return 0;
80}
81
82static inline void ftrace_generate_kprobe_nop_insn(struct ftrace_insn *insn)
83{
84#ifdef CONFIG_KPROBES
85 insn->opc = BREAKPOINT_INSTRUCTION;
86 insn->disp = KPROBE_ON_FTRACE_NOP;
87#endif
88}
89
90static inline void ftrace_generate_kprobe_call_insn(struct ftrace_insn *insn)
91{
92#ifdef CONFIG_KPROBES
93 insn->opc = BREAKPOINT_INSTRUCTION;
94 insn->disp = KPROBE_ON_FTRACE_CALL;
95#endif
96}
97
98int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
99 unsigned long addr)
100{
101 return 0;
102}
103
104int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
105 unsigned long addr)
106{
107 struct ftrace_insn orig, new, old;
108
109 if (probe_kernel_read(&old, (void *) rec->ip, sizeof(old)))
110 return -EFAULT;
111 if (addr == MCOUNT_ADDR) {
112
113 ftrace_generate_orig_insn(&orig);
114 ftrace_generate_nop_insn(&new);
115 } else if (is_kprobe_on_ftrace(&old)) {
116
117
118
119
120
121
122
123 ftrace_generate_kprobe_call_insn(&orig);
124 ftrace_generate_kprobe_nop_insn(&new);
125 } else {
126
127 ftrace_generate_call_insn(&orig, rec->ip);
128 ftrace_generate_nop_insn(&new);
129 }
130
131 if (memcmp(&orig, &old, sizeof(old)))
132 return -EINVAL;
133 s390_kernel_write((void *) rec->ip, &new, sizeof(new));
134 return 0;
135}
136
137int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
138{
139 struct ftrace_insn orig, new, old;
140
141 if (probe_kernel_read(&old, (void *) rec->ip, sizeof(old)))
142 return -EFAULT;
143 if (is_kprobe_on_ftrace(&old)) {
144
145
146
147
148
149
150
151 ftrace_generate_kprobe_nop_insn(&orig);
152 ftrace_generate_kprobe_call_insn(&new);
153 } else {
154
155 ftrace_generate_nop_insn(&orig);
156 ftrace_generate_call_insn(&new, rec->ip);
157 }
158
159 if (memcmp(&orig, &old, sizeof(old)))
160 return -EINVAL;
161 s390_kernel_write((void *) rec->ip, &new, sizeof(new));
162 return 0;
163}
164
165int ftrace_update_ftrace_func(ftrace_func_t func)
166{
167 return 0;
168}
169
170int __init ftrace_dyn_arch_init(void)
171{
172 return 0;
173}
174
175static int __init ftrace_plt_init(void)
176{
177 unsigned int *ip;
178
179 ftrace_plt = (unsigned long) module_alloc(PAGE_SIZE);
180 if (!ftrace_plt)
181 panic("cannot allocate ftrace plt\n");
182 ip = (unsigned int *) ftrace_plt;
183 ip[0] = 0x0d10e310;
184 ip[1] = 0x100a0004;
185 ip[2] = 0x07f10000;
186 ip[3] = FTRACE_ADDR >> 32;
187 ip[4] = FTRACE_ADDR & 0xffffffff;
188 set_memory_ro(ftrace_plt, 1);
189 return 0;
190}
191device_initcall(ftrace_plt_init);
192
193#ifdef CONFIG_FUNCTION_GRAPH_TRACER
194
195
196
197
198unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip)
199{
200 struct ftrace_graph_ent trace;
201
202 if (unlikely(ftrace_graph_is_dead()))
203 goto out;
204 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
205 goto out;
206 ip -= MCOUNT_INSN_SIZE;
207 trace.func = ip;
208 trace.depth = current->curr_ret_stack + 1;
209
210 if (!ftrace_graph_entry(&trace))
211 goto out;
212 if (ftrace_push_return_trace(parent, ip, &trace.depth, 0,
213 NULL) == -EBUSY)
214 goto out;
215 parent = (unsigned long) return_to_handler;
216out:
217 return parent;
218}
219NOKPROBE_SYMBOL(prepare_ftrace_return);
220
221
222
223
224
225
226
227
228
229int ftrace_enable_ftrace_graph_caller(void)
230{
231 u8 op = 0x04;
232
233 s390_kernel_write(__va(ftrace_graph_caller)+1, &op, sizeof(op));
234 return 0;
235}
236
237int ftrace_disable_ftrace_graph_caller(void)
238{
239 u8 op = 0xf4;
240
241 s390_kernel_write(__va(ftrace_graph_caller)+1, &op, sizeof(op));
242 return 0;
243}
244
245#endif
246