blob: 71ce4cbb7e9f268840369513ac411052a3d27d8b [file] [log] [blame]
Steven Rostedt4e491d12008-05-14 23:49:44 -04001/*
2 * Code for replacing ftrace calls with jumps.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 *
6 * Thanks goes out to P.A. Semi, Inc for supplying me with a PPC64 box.
7 *
Steven Rostedt6794c782009-02-09 21:10:27 -08008 * Added function graph tracer code, taken from x86 that was written
9 * by Frederic Weisbecker, and ported to PPC by Steven Rostedt.
10 *
Steven Rostedt4e491d12008-05-14 23:49:44 -040011 */
12
13#include <linux/spinlock.h>
14#include <linux/hardirq.h>
Steven Rostedte4486fe2008-11-14 16:21:20 -080015#include <linux/uaccess.h>
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080016#include <linux/module.h>
Steven Rostedt4e491d12008-05-14 23:49:44 -040017#include <linux/ftrace.h>
18#include <linux/percpu.h>
19#include <linux/init.h>
20#include <linux/list.h>
21
22#include <asm/cacheflush.h>
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080023#include <asm/code-patching.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053024#include <asm/ftrace.h>
Ian Munsie02424d82011-02-02 17:27:24 +000025#include <asm/syscall.h>
Steven Rostedt4e491d12008-05-14 23:49:44 -040026
Steven Rostedt4e491d12008-05-14 23:49:44 -040027
Steven Rostedt6794c782009-02-09 21:10:27 -080028#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedtb54dcfe2009-02-13 06:31:39 -080029static unsigned int
Steven Rostedt46542882009-02-10 22:19:54 -080030ftrace_call_replace(unsigned long ip, unsigned long addr, int link)
Steven Rostedt4e491d12008-05-14 23:49:44 -040031{
Steven Rostedtb54dcfe2009-02-13 06:31:39 -080032 unsigned int op;
Steven Rostedt4e491d12008-05-14 23:49:44 -040033
Michael Ellerman4a9e3f82009-05-28 19:33:34 +000034 addr = ppc_function_entry((void *)addr);
Steven Rostedt4e491d12008-05-14 23:49:44 -040035
Steven Rostedt46542882009-02-10 22:19:54 -080036 /* if (link) set op to 'bl' else 'b' */
Steven Rostedtbb9b9032009-02-13 06:45:27 -080037 op = create_branch((unsigned int *)ip, addr, link ? 1 : 0);
Steven Rostedt4e491d12008-05-14 23:49:44 -040038
Steven Rostedtb54dcfe2009-02-13 06:31:39 -080039 return op;
Steven Rostedt4e491d12008-05-14 23:49:44 -040040}
41
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080042static int
Steven Rostedtb54dcfe2009-02-13 06:31:39 -080043ftrace_modify_code(unsigned long ip, unsigned int old, unsigned int new)
Steven Rostedt4e491d12008-05-14 23:49:44 -040044{
Steven Rostedtb54dcfe2009-02-13 06:31:39 -080045 unsigned int replaced;
Steven Rostedt4e491d12008-05-14 23:49:44 -040046
Steven Rostedt4e491d12008-05-14 23:49:44 -040047 /*
48 * Note: Due to modules and __init, code can
49 * disappear and change, we need to protect against faulting
Steven Rostedte4486fe2008-11-14 16:21:20 -080050 * as well as code changing. We do this by using the
51 * probe_kernel_* functions.
Steven Rostedt4e491d12008-05-14 23:49:44 -040052 *
53 * No real locking needed, this code is run through
Steven Rostedte4486fe2008-11-14 16:21:20 -080054 * kstop_machine, or before SMP starts.
Steven Rostedt4e491d12008-05-14 23:49:44 -040055 */
Steven Rostedt4e491d12008-05-14 23:49:44 -040056
Steven Rostedte4486fe2008-11-14 16:21:20 -080057 /* read the text we want to modify */
Steven Rostedtb54dcfe2009-02-13 06:31:39 -080058 if (probe_kernel_read(&replaced, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedte4486fe2008-11-14 16:21:20 -080059 return -EFAULT;
Steven Rostedt4e491d12008-05-14 23:49:44 -040060
Steven Rostedte4486fe2008-11-14 16:21:20 -080061 /* Make sure it is what we expect it to be */
Steven Rostedtb54dcfe2009-02-13 06:31:39 -080062 if (replaced != old)
Steven Rostedte4486fe2008-11-14 16:21:20 -080063 return -EINVAL;
Steven Rostedt4e491d12008-05-14 23:49:44 -040064
Steven Rostedte4486fe2008-11-14 16:21:20 -080065 /* replace the text with the new text */
Steven Rostedt65b8c722012-04-26 08:31:19 +000066 if (patch_instruction((unsigned int *)ip, new))
Steven Rostedte4486fe2008-11-14 16:21:20 -080067 return -EPERM;
68
Steven Rostedte4486fe2008-11-14 16:21:20 -080069 return 0;
Steven Rostedt4e491d12008-05-14 23:49:44 -040070}
71
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080072/*
73 * Helper functions that are the same for both PPC64 and PPC32.
74 */
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080075static int test_24bit_addr(unsigned long ip, unsigned long addr)
76{
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080077
Steven Rostedt0029ff82008-11-25 14:06:19 -080078 /* use the create_branch to verify that this offset can be branched */
79 return create_branch((unsigned int *)ip, addr, 0);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080080}
81
Steven Rostedt17be5b32009-02-05 21:33:09 -080082#ifdef CONFIG_MODULES
83
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080084static int is_bl_op(unsigned int op)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080085{
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080086 return (op & 0xfc000003) == 0x48000001;
87}
88
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080089static unsigned long find_bl_target(unsigned long ip, unsigned int op)
90{
91 static int offset;
92
93 offset = (op & 0x03fffffc);
94 /* make it signed */
95 if (offset & 0x02000000)
96 offset |= 0xfe000000;
97
98 return ip + (long)offset;
99}
100
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800101#ifdef CONFIG_PPC64
102static int
103__ftrace_make_nop(struct module *mod,
104 struct dyn_ftrace *rec, unsigned long addr)
105{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800106 unsigned int op;
107 unsigned int jmp[5];
108 unsigned long ptr;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800109 unsigned long ip = rec->ip;
110 unsigned long tramp;
111 int offset;
112
113 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800114 if (probe_kernel_read(&op, (void *)ip, sizeof(int)))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800115 return -EFAULT;
116
117 /* Make sure that that this is still a 24bit jump */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800118 if (!is_bl_op(op)) {
119 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800120 return -EINVAL;
121 }
122
123 /* lets find where the pointer goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800124 tramp = find_bl_target(ip, op);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800125
126 /*
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800127 * On PPC64 the trampoline looks like:
128 * 0x3d, 0x82, 0x00, 0x00, addis r12,r2, <high>
129 * 0x39, 0x8c, 0x00, 0x00, addi r12,r12, <low>
130 * Where the bytes 2,3,6 and 7 make up the 32bit offset
131 * to the TOC that holds the pointer.
132 * to jump to.
133 * 0xf8, 0x41, 0x00, 0x28, std r2,40(r1)
134 * 0xe9, 0x6c, 0x00, 0x20, ld r11,32(r12)
135 * The actually address is 32 bytes from the offset
136 * into the TOC.
137 * 0xe8, 0x4c, 0x00, 0x28, ld r2,40(r12)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800138 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800139
Michael Ellerman021376a2009-05-13 20:30:24 +0000140 pr_devel("ip:%lx jumps to %lx r2: %lx", ip, tramp, mod->arch.toc);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800141
142 /* Find where the trampoline jumps to */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800143 if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800144 printk(KERN_ERR "Failed to read %lx\n", tramp);
145 return -EFAULT;
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800146 }
147
Michael Ellerman021376a2009-05-13 20:30:24 +0000148 pr_devel(" %08x %08x", jmp[0], jmp[1]);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800149
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800150 /* verify that this is what we expect it to be */
151 if (((jmp[0] & 0xffff0000) != 0x3d820000) ||
152 ((jmp[1] & 0xffff0000) != 0x398c0000) ||
153 (jmp[2] != 0xf8410028) ||
154 (jmp[3] != 0xe96c0020) ||
155 (jmp[4] != 0xe84c0028)) {
156 printk(KERN_ERR "Not a trampoline\n");
157 return -EINVAL;
158 }
159
Steven Rostedtf25f9072009-02-07 20:22:40 +0000160 /* The bottom half is signed extended */
161 offset = ((unsigned)((unsigned short)jmp[0]) << 16) +
162 (int)((short)jmp[1]);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800163
Michael Ellerman021376a2009-05-13 20:30:24 +0000164 pr_devel(" %x ", offset);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800165
166 /* get the address this jumps too */
167 tramp = mod->arch.toc + offset + 32;
Michael Ellerman021376a2009-05-13 20:30:24 +0000168 pr_devel("toc: %lx", tramp);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800169
170 if (probe_kernel_read(jmp, (void *)tramp, 8)) {
171 printk(KERN_ERR "Failed to read %lx\n", tramp);
172 return -EFAULT;
173 }
174
Michael Ellerman021376a2009-05-13 20:30:24 +0000175 pr_devel(" %08x %08x\n", jmp[0], jmp[1]);
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800176
Eugene Surovegin30647432013-09-20 11:42:21 -0700177#ifdef __LITTLE_ENDIAN__
178 ptr = ((unsigned long)jmp[1] << 32) + jmp[0];
179#else
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800180 ptr = ((unsigned long)jmp[0] << 32) + jmp[1];
Eugene Surovegin30647432013-09-20 11:42:21 -0700181#endif
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800182
183 /* This should match what was called */
Michael Ellerman4a9e3f82009-05-28 19:33:34 +0000184 if (ptr != ppc_function_entry((void *)addr)) {
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800185 printk(KERN_ERR "addr does not match %lx\n", ptr);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800186 return -EINVAL;
187 }
188
189 /*
190 * We want to nop the line, but the next line is
191 * 0xe8, 0x41, 0x00, 0x28 ld r2,40(r1)
192 * This needs to be turned to a nop too.
193 */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800194 if (probe_kernel_read(&op, (void *)(ip+4), MCOUNT_INSN_SIZE))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800195 return -EFAULT;
196
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800197 if (op != 0xe8410028) {
198 printk(KERN_ERR "Next line is not ld! (%08x)\n", op);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800199 return -EINVAL;
200 }
201
202 /*
203 * Milton Miller pointed out that we can not blindly do nops.
204 * If a task was preempted when calling a trace function,
205 * the nops will remove the way to restore the TOC in r2
206 * and the r2 TOC will get corrupted.
207 */
208
209 /*
210 * Replace:
211 * bl <tramp> <==== will be replaced with "b 1f"
212 * ld r2,40(r1)
213 * 1:
214 */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800215 op = 0x48000008; /* b +8 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800216
Steven Rostedt65b8c722012-04-26 08:31:19 +0000217 if (patch_instruction((unsigned int *)ip, op))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800218 return -EPERM;
219
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800220 return 0;
221}
222
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800223#else /* !PPC64 */
224static int
225__ftrace_make_nop(struct module *mod,
226 struct dyn_ftrace *rec, unsigned long addr)
227{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800228 unsigned int op;
229 unsigned int jmp[4];
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500230 unsigned long ip = rec->ip;
231 unsigned long tramp;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500232
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800233 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500234 return -EFAULT;
235
236 /* Make sure that that this is still a 24bit jump */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800237 if (!is_bl_op(op)) {
238 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500239 return -EINVAL;
240 }
241
242 /* lets find where the pointer goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800243 tramp = find_bl_target(ip, op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500244
245 /*
246 * On PPC32 the trampoline looks like:
roger blofeldfd5a4292012-06-21 05:27:14 +0000247 * 0x3d, 0x80, 0x00, 0x00 lis r12,sym@ha
248 * 0x39, 0x8c, 0x00, 0x00 addi r12,r12,sym@l
249 * 0x7d, 0x89, 0x03, 0xa6 mtctr r12
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800250 * 0x4e, 0x80, 0x04, 0x20 bctr
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500251 */
252
Michael Ellerman021376a2009-05-13 20:30:24 +0000253 pr_devel("ip:%lx jumps to %lx", ip, tramp);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500254
255 /* Find where the trampoline jumps to */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800256 if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500257 printk(KERN_ERR "Failed to read %lx\n", tramp);
258 return -EFAULT;
259 }
260
Michael Ellerman021376a2009-05-13 20:30:24 +0000261 pr_devel(" %08x %08x ", jmp[0], jmp[1]);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500262
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800263 /* verify that this is what we expect it to be */
roger blofeldfd5a4292012-06-21 05:27:14 +0000264 if (((jmp[0] & 0xffff0000) != 0x3d800000) ||
265 ((jmp[1] & 0xffff0000) != 0x398c0000) ||
266 (jmp[2] != 0x7d8903a6) ||
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800267 (jmp[3] != 0x4e800420)) {
268 printk(KERN_ERR "Not a trampoline\n");
269 return -EINVAL;
270 }
271
272 tramp = (jmp[1] & 0xffff) |
273 ((jmp[0] & 0xffff) << 16);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500274 if (tramp & 0x8000)
275 tramp -= 0x10000;
276
Michael Ellerman021376a2009-05-13 20:30:24 +0000277 pr_devel(" %lx ", tramp);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500278
279 if (tramp != addr) {
280 printk(KERN_ERR
281 "Trampoline location %08lx does not match addr\n",
282 tramp);
283 return -EINVAL;
284 }
285
Kumar Gala16c57b32009-02-10 20:10:44 +0000286 op = PPC_INST_NOP;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500287
Steven Rostedt65b8c722012-04-26 08:31:19 +0000288 if (patch_instruction((unsigned int *)ip, op))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500289 return -EPERM;
290
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800291 return 0;
292}
293#endif /* PPC64 */
Steven Rostedt17be5b32009-02-05 21:33:09 -0800294#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800295
296int ftrace_make_nop(struct module *mod,
297 struct dyn_ftrace *rec, unsigned long addr)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800298{
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800299 unsigned long ip = rec->ip;
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800300 unsigned int old, new;
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800301
302 /*
303 * If the calling address is more that 24 bits away,
304 * then we had to use a trampoline to make the call.
305 * Otherwise just update the call site.
306 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800307 if (test_24bit_addr(ip, addr)) {
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800308 /* within range */
Steven Rostedt46542882009-02-10 22:19:54 -0800309 old = ftrace_call_replace(ip, addr, 1);
Michael Ellerman92e02a52009-05-28 19:33:36 +0000310 new = PPC_INST_NOP;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800311 return ftrace_modify_code(ip, old, new);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800312 }
313
Steven Rostedt17be5b32009-02-05 21:33:09 -0800314#ifdef CONFIG_MODULES
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800315 /*
316 * Out of range jumps are called from modules.
317 * We should either already have a pointer to the module
318 * or it has been passed in.
319 */
320 if (!rec->arch.mod) {
321 if (!mod) {
322 printk(KERN_ERR "No module loaded addr=%lx\n",
323 addr);
324 return -EFAULT;
325 }
326 rec->arch.mod = mod;
327 } else if (mod) {
328 if (mod != rec->arch.mod) {
329 printk(KERN_ERR
330 "Record mod %p not equal to passed in mod %p\n",
331 rec->arch.mod, mod);
332 return -EINVAL;
333 }
334 /* nothing to do if mod == rec->arch.mod */
335 } else
336 mod = rec->arch.mod;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800337
338 return __ftrace_make_nop(mod, rec, addr);
Steven Rostedt17be5b32009-02-05 21:33:09 -0800339#else
340 /* We should not get here without modules */
341 return -EINVAL;
342#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800343}
344
Steven Rostedt17be5b32009-02-05 21:33:09 -0800345#ifdef CONFIG_MODULES
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800346#ifdef CONFIG_PPC64
347static int
348__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
349{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800350 unsigned int op[2];
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800351 unsigned long ip = rec->ip;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800352
353 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800354 if (probe_kernel_read(op, (void *)ip, MCOUNT_INSN_SIZE * 2))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800355 return -EFAULT;
356
357 /*
358 * It should be pointing to two nops or
359 * b +8; ld r2,40(r1)
360 */
361 if (((op[0] != 0x48000008) || (op[1] != 0xe8410028)) &&
Kumar Gala16c57b32009-02-10 20:10:44 +0000362 ((op[0] != PPC_INST_NOP) || (op[1] != PPC_INST_NOP))) {
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800363 printk(KERN_ERR "Expected NOPs but have %x %x\n", op[0], op[1]);
364 return -EINVAL;
365 }
366
367 /* If we never set up a trampoline to ftrace_caller, then bail */
368 if (!rec->arch.mod->arch.tramp) {
369 printk(KERN_ERR "No ftrace trampoline\n");
370 return -EINVAL;
371 }
372
Steven Rostedt0029ff82008-11-25 14:06:19 -0800373 /* create the branch to the trampoline */
374 op[0] = create_branch((unsigned int *)ip,
375 rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
376 if (!op[0]) {
377 printk(KERN_ERR "REL24 out of range!\n");
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800378 return -EINVAL;
379 }
380
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800381 /* ld r2,40(r1) */
382 op[1] = 0xe8410028;
383
Michael Ellerman021376a2009-05-13 20:30:24 +0000384 pr_devel("write to %lx\n", rec->ip);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800385
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800386 if (probe_kernel_write((void *)ip, op, MCOUNT_INSN_SIZE * 2))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800387 return -EPERM;
388
Steven Rostedtec682ce2008-11-25 10:22:48 -0800389 flush_icache_range(ip, ip + 8);
390
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800391 return 0;
392}
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800393#else
394static int
395__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
396{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800397 unsigned int op;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500398 unsigned long ip = rec->ip;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500399
400 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800401 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500402 return -EFAULT;
403
404 /* It should be pointing to a nop */
Kumar Gala16c57b32009-02-10 20:10:44 +0000405 if (op != PPC_INST_NOP) {
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800406 printk(KERN_ERR "Expected NOP but have %x\n", op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500407 return -EINVAL;
408 }
409
410 /* If we never set up a trampoline to ftrace_caller, then bail */
411 if (!rec->arch.mod->arch.tramp) {
412 printk(KERN_ERR "No ftrace trampoline\n");
413 return -EINVAL;
414 }
415
Steven Rostedt0029ff82008-11-25 14:06:19 -0800416 /* create the branch to the trampoline */
417 op = create_branch((unsigned int *)ip,
418 rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
419 if (!op) {
420 printk(KERN_ERR "REL24 out of range!\n");
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500421 return -EINVAL;
422 }
423
Michael Ellerman021376a2009-05-13 20:30:24 +0000424 pr_devel("write to %lx\n", rec->ip);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500425
Steven Rostedt65b8c722012-04-26 08:31:19 +0000426 if (patch_instruction((unsigned int *)ip, op))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500427 return -EPERM;
428
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800429 return 0;
430}
431#endif /* CONFIG_PPC64 */
Steven Rostedt17be5b32009-02-05 21:33:09 -0800432#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800433
434int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
435{
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800436 unsigned long ip = rec->ip;
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800437 unsigned int old, new;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800438
439 /*
440 * If the calling address is more that 24 bits away,
441 * then we had to use a trampoline to make the call.
442 * Otherwise just update the call site.
443 */
444 if (test_24bit_addr(ip, addr)) {
445 /* within range */
Michael Ellerman92e02a52009-05-28 19:33:36 +0000446 old = PPC_INST_NOP;
Steven Rostedt46542882009-02-10 22:19:54 -0800447 new = ftrace_call_replace(ip, addr, 1);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800448 return ftrace_modify_code(ip, old, new);
449 }
450
Steven Rostedt17be5b32009-02-05 21:33:09 -0800451#ifdef CONFIG_MODULES
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800452 /*
453 * Out of range jumps are called from modules.
454 * Being that we are converting from nop, it had better
455 * already have a module defined.
456 */
457 if (!rec->arch.mod) {
458 printk(KERN_ERR "No module loaded\n");
459 return -EINVAL;
460 }
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800461
462 return __ftrace_make_call(rec, addr);
Steven Rostedt17be5b32009-02-05 21:33:09 -0800463#else
464 /* We should not get here without modules */
465 return -EINVAL;
466#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800467}
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800468
Steven Rostedt15adc042008-10-23 09:33:08 -0400469int ftrace_update_ftrace_func(ftrace_func_t func)
Steven Rostedt4e491d12008-05-14 23:49:44 -0400470{
471 unsigned long ip = (unsigned long)(&ftrace_call);
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800472 unsigned int old, new;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400473 int ret;
474
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800475 old = *(unsigned int *)&ftrace_call;
Steven Rostedt46542882009-02-10 22:19:54 -0800476 new = ftrace_call_replace(ip, (unsigned long)func, 1);
Steven Rostedt4e491d12008-05-14 23:49:44 -0400477 ret = ftrace_modify_code(ip, old, new);
478
479 return ret;
480}
481
Steven Rostedtee456bb2012-04-26 08:31:17 +0000482static int __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
483{
484 unsigned long ftrace_addr = (unsigned long)FTRACE_ADDR;
485 int ret;
486
487 ret = ftrace_update_record(rec, enable);
488
489 switch (ret) {
490 case FTRACE_UPDATE_IGNORE:
491 return 0;
492 case FTRACE_UPDATE_MAKE_CALL:
493 return ftrace_make_call(rec, ftrace_addr);
494 case FTRACE_UPDATE_MAKE_NOP:
495 return ftrace_make_nop(NULL, rec, ftrace_addr);
496 }
497
498 return 0;
499}
500
501void ftrace_replace_code(int enable)
502{
503 struct ftrace_rec_iter *iter;
504 struct dyn_ftrace *rec;
505 int ret;
506
507 for (iter = ftrace_rec_iter_start(); iter;
508 iter = ftrace_rec_iter_next(iter)) {
509 rec = ftrace_rec_iter_record(iter);
510 ret = __ftrace_replace_code(rec, enable);
511 if (ret) {
512 ftrace_bug(ret, rec->ip);
513 return;
514 }
515 }
516}
517
518void arch_ftrace_update_code(int command)
519{
520 if (command & FTRACE_UPDATE_CALLS)
521 ftrace_replace_code(1);
522 else if (command & FTRACE_DISABLE_CALLS)
523 ftrace_replace_code(0);
524
525 if (command & FTRACE_UPDATE_TRACE_FUNC)
526 ftrace_update_ftrace_func(ftrace_trace_function);
527
528 if (command & FTRACE_START_FUNC_RET)
529 ftrace_enable_ftrace_graph_caller();
530 else if (command & FTRACE_STOP_FUNC_RET)
531 ftrace_disable_ftrace_graph_caller();
532}
533
Jiri Slaby3a36cb12014-02-24 19:59:59 +0100534int __init ftrace_dyn_arch_init(void)
Steven Rostedt4e491d12008-05-14 23:49:44 -0400535{
Steven Rostedt4e491d12008-05-14 23:49:44 -0400536 return 0;
537}
Steven Rostedt6794c782009-02-09 21:10:27 -0800538#endif /* CONFIG_DYNAMIC_FTRACE */
539
540#ifdef CONFIG_FUNCTION_GRAPH_TRACER
541
Steven Rostedt46542882009-02-10 22:19:54 -0800542#ifdef CONFIG_DYNAMIC_FTRACE
543extern void ftrace_graph_call(void);
544extern void ftrace_graph_stub(void);
545
546int ftrace_enable_ftrace_graph_caller(void)
547{
548 unsigned long ip = (unsigned long)(&ftrace_graph_call);
549 unsigned long addr = (unsigned long)(&ftrace_graph_caller);
550 unsigned long stub = (unsigned long)(&ftrace_graph_stub);
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800551 unsigned int old, new;
Steven Rostedt46542882009-02-10 22:19:54 -0800552
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800553 old = ftrace_call_replace(ip, stub, 0);
Steven Rostedt46542882009-02-10 22:19:54 -0800554 new = ftrace_call_replace(ip, addr, 0);
555
556 return ftrace_modify_code(ip, old, new);
557}
558
559int ftrace_disable_ftrace_graph_caller(void)
560{
561 unsigned long ip = (unsigned long)(&ftrace_graph_call);
562 unsigned long addr = (unsigned long)(&ftrace_graph_caller);
563 unsigned long stub = (unsigned long)(&ftrace_graph_stub);
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800564 unsigned int old, new;
Steven Rostedt46542882009-02-10 22:19:54 -0800565
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800566 old = ftrace_call_replace(ip, addr, 0);
Steven Rostedt46542882009-02-10 22:19:54 -0800567 new = ftrace_call_replace(ip, stub, 0);
568
569 return ftrace_modify_code(ip, old, new);
570}
571#endif /* CONFIG_DYNAMIC_FTRACE */
572
Steven Rostedtbb725342009-02-11 12:45:49 -0800573#ifdef CONFIG_PPC64
574extern void mod_return_to_handler(void);
575#endif
576
Steven Rostedt6794c782009-02-09 21:10:27 -0800577/*
578 * Hook the return address and push it in the stack of return addrs
579 * in current thread info.
580 */
581void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
582{
583 unsigned long old;
Steven Rostedt6794c782009-02-09 21:10:27 -0800584 int faulted;
585 struct ftrace_graph_ent trace;
Steven Rostedtbb725342009-02-11 12:45:49 -0800586 unsigned long return_hooker = (unsigned long)&return_to_handler;
Steven Rostedt6794c782009-02-09 21:10:27 -0800587
588 if (unlikely(atomic_read(&current->tracing_graph_pause)))
589 return;
590
Michael Ellermanf4952f62009-04-06 04:40:45 +0000591#ifdef CONFIG_PPC64
Steven Rostedtbb725342009-02-11 12:45:49 -0800592 /* non core kernel code needs to save and restore the TOC */
593 if (REGION_ID(self_addr) != KERNEL_REGION_ID)
594 return_hooker = (unsigned long)&mod_return_to_handler;
595#endif
596
Michael Ellerman4a9e3f82009-05-28 19:33:34 +0000597 return_hooker = ppc_function_entry((void *)return_hooker);
Steven Rostedt6794c782009-02-09 21:10:27 -0800598
599 /*
600 * Protect against fault, even if it shouldn't
601 * happen. This tool is too much intrusive to
602 * ignore such a protection.
603 */
604 asm volatile(
605 "1: " PPC_LL "%[old], 0(%[parent])\n"
606 "2: " PPC_STL "%[return_hooker], 0(%[parent])\n"
607 " li %[faulted], 0\n"
Steven Rostedtfad4f472009-02-11 19:10:57 -0500608 "3:\n"
Steven Rostedt6794c782009-02-09 21:10:27 -0800609
610 ".section .fixup, \"ax\"\n"
611 "4: li %[faulted], 1\n"
612 " b 3b\n"
613 ".previous\n"
614
615 ".section __ex_table,\"a\"\n"
616 PPC_LONG_ALIGN "\n"
617 PPC_LONG "1b,4b\n"
618 PPC_LONG "2b,4b\n"
619 ".previous"
620
Steven Rostedtc3cf8662009-05-15 04:33:54 +0000621 : [old] "=&r" (old), [faulted] "=r" (faulted)
Steven Rostedt6794c782009-02-09 21:10:27 -0800622 : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
623 : "memory"
624 );
625
626 if (unlikely(faulted)) {
627 ftrace_graph_stop();
628 WARN_ON(1);
629 return;
630 }
631
Steven Rostedtbac821a2012-07-18 12:35:28 +0000632 trace.func = self_addr;
633 trace.depth = current->curr_ret_stack + 1;
634
635 /* Only trace if the calling function expects to */
636 if (!ftrace_graph_entry(&trace)) {
Steven Rostedt6794c782009-02-09 21:10:27 -0800637 *parent = old;
638 return;
639 }
640
Steven Rostedtbac821a2012-07-18 12:35:28 +0000641 if (ftrace_push_return_trace(old, self_addr, &trace.depth, 0) == -EBUSY)
Steven Rostedt6794c782009-02-09 21:10:27 -0800642 *parent = old;
Steven Rostedt6794c782009-02-09 21:10:27 -0800643}
644#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Ian Munsie02424d82011-02-02 17:27:24 +0000645
646#if defined(CONFIG_FTRACE_SYSCALLS) && defined(CONFIG_PPC64)
647unsigned long __init arch_syscall_addr(int nr)
648{
649 return sys_call_table[nr*2];
650}
651#endif /* CONFIG_FTRACE_SYSCALLS && CONFIG_PPC64 */