blob: 78cdd7fbecd008cfd3b4fe4c8baa7d4868225eaf [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{
Liu Ping Fana95fc582014-02-26 10:23:01 +080077 addr = ppc_function_entry((void *)addr);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080078
Steven Rostedt0029ff82008-11-25 14:06:19 -080079 /* use the create_branch to verify that this offset can be branched */
80 return create_branch((unsigned int *)ip, addr, 0);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080081}
82
Steven Rostedt17be5b32009-02-05 21:33:09 -080083#ifdef CONFIG_MODULES
84
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080085static int is_bl_op(unsigned int op)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080086{
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080087 return (op & 0xfc000003) == 0x48000001;
88}
89
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080090static unsigned long find_bl_target(unsigned long ip, unsigned int op)
91{
92 static int offset;
93
94 offset = (op & 0x03fffffc);
95 /* make it signed */
96 if (offset & 0x02000000)
97 offset |= 0xfe000000;
98
99 return ip + (long)offset;
100}
101
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800102#ifdef CONFIG_PPC64
103static int
104__ftrace_make_nop(struct module *mod,
105 struct dyn_ftrace *rec, unsigned long addr)
106{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800107 unsigned int op;
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800108 unsigned long ptr;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800109 unsigned long ip = rec->ip;
Anton Blanchard62c9da62014-04-04 16:52:58 +1100110 void *tramp;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800111
112 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800113 if (probe_kernel_read(&op, (void *)ip, sizeof(int)))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800114 return -EFAULT;
115
116 /* Make sure that that this is still a 24bit jump */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800117 if (!is_bl_op(op)) {
118 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800119 return -EINVAL;
120 }
121
122 /* lets find where the pointer goes */
Anton Blanchard62c9da62014-04-04 16:52:58 +1100123 tramp = (void *)find_bl_target(ip, op);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800124
Anton Blanchard62c9da62014-04-04 16:52:58 +1100125 pr_devel("ip:%lx jumps to %p", ip, tramp);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800126
Anton Blanchard62c9da62014-04-04 16:52:58 +1100127 if (!is_module_trampoline(tramp)) {
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800128 printk(KERN_ERR "Not a trampoline\n");
129 return -EINVAL;
130 }
131
Anton Blanchard62c9da62014-04-04 16:52:58 +1100132 if (module_trampoline_target(mod, tramp, &ptr)) {
133 printk(KERN_ERR "Failed to get trampoline target\n");
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800134 return -EFAULT;
135 }
136
Anton Blanchard62c9da62014-04-04 16:52:58 +1100137 pr_devel("trampoline target %lx", ptr);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800138
139 /* This should match what was called */
Michael Ellerman4a9e3f82009-05-28 19:33:34 +0000140 if (ptr != ppc_function_entry((void *)addr)) {
Anton Blanchard62c9da62014-04-04 16:52:58 +1100141 printk(KERN_ERR "addr %lx does not match expected %lx\n",
142 ptr, ppc_function_entry((void *)addr));
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800143 return -EINVAL;
144 }
145
146 /*
Anton Blanchard62c9da62014-04-04 16:52:58 +1100147 * Our original call site looks like:
148 *
149 * bl <tramp>
150 * ld r2,XX(r1)
151 *
152 * Milton Miller pointed out that we can not simply nop the branch.
153 * If a task was preempted when calling a trace function, the nops
154 * will remove the way to restore the TOC in r2 and the r2 TOC will
155 * get corrupted.
156 *
157 * Use a b +8 to jump over the load.
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800158 */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800159 op = 0x48000008; /* b +8 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800160
Steven Rostedt65b8c722012-04-26 08:31:19 +0000161 if (patch_instruction((unsigned int *)ip, op))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800162 return -EPERM;
163
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800164 return 0;
165}
166
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800167#else /* !PPC64 */
168static int
169__ftrace_make_nop(struct module *mod,
170 struct dyn_ftrace *rec, unsigned long addr)
171{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800172 unsigned int op;
173 unsigned int jmp[4];
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500174 unsigned long ip = rec->ip;
175 unsigned long tramp;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500176
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800177 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500178 return -EFAULT;
179
180 /* Make sure that that this is still a 24bit jump */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800181 if (!is_bl_op(op)) {
182 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500183 return -EINVAL;
184 }
185
186 /* lets find where the pointer goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800187 tramp = find_bl_target(ip, op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500188
189 /*
190 * On PPC32 the trampoline looks like:
roger blofeldfd5a4292012-06-21 05:27:14 +0000191 * 0x3d, 0x80, 0x00, 0x00 lis r12,sym@ha
192 * 0x39, 0x8c, 0x00, 0x00 addi r12,r12,sym@l
193 * 0x7d, 0x89, 0x03, 0xa6 mtctr r12
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800194 * 0x4e, 0x80, 0x04, 0x20 bctr
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500195 */
196
Michael Ellerman021376a2009-05-13 20:30:24 +0000197 pr_devel("ip:%lx jumps to %lx", ip, tramp);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500198
199 /* Find where the trampoline jumps to */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800200 if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500201 printk(KERN_ERR "Failed to read %lx\n", tramp);
202 return -EFAULT;
203 }
204
Michael Ellerman021376a2009-05-13 20:30:24 +0000205 pr_devel(" %08x %08x ", jmp[0], jmp[1]);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500206
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800207 /* verify that this is what we expect it to be */
roger blofeldfd5a4292012-06-21 05:27:14 +0000208 if (((jmp[0] & 0xffff0000) != 0x3d800000) ||
209 ((jmp[1] & 0xffff0000) != 0x398c0000) ||
210 (jmp[2] != 0x7d8903a6) ||
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800211 (jmp[3] != 0x4e800420)) {
212 printk(KERN_ERR "Not a trampoline\n");
213 return -EINVAL;
214 }
215
216 tramp = (jmp[1] & 0xffff) |
217 ((jmp[0] & 0xffff) << 16);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500218 if (tramp & 0x8000)
219 tramp -= 0x10000;
220
Michael Ellerman021376a2009-05-13 20:30:24 +0000221 pr_devel(" %lx ", tramp);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500222
223 if (tramp != addr) {
224 printk(KERN_ERR
225 "Trampoline location %08lx does not match addr\n",
226 tramp);
227 return -EINVAL;
228 }
229
Kumar Gala16c57b32009-02-10 20:10:44 +0000230 op = PPC_INST_NOP;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500231
Steven Rostedt65b8c722012-04-26 08:31:19 +0000232 if (patch_instruction((unsigned int *)ip, op))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500233 return -EPERM;
234
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800235 return 0;
236}
237#endif /* PPC64 */
Steven Rostedt17be5b32009-02-05 21:33:09 -0800238#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800239
240int ftrace_make_nop(struct module *mod,
241 struct dyn_ftrace *rec, unsigned long addr)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800242{
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800243 unsigned long ip = rec->ip;
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800244 unsigned int old, new;
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800245
246 /*
247 * If the calling address is more that 24 bits away,
248 * then we had to use a trampoline to make the call.
249 * Otherwise just update the call site.
250 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800251 if (test_24bit_addr(ip, addr)) {
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800252 /* within range */
Steven Rostedt46542882009-02-10 22:19:54 -0800253 old = ftrace_call_replace(ip, addr, 1);
Michael Ellerman92e02a52009-05-28 19:33:36 +0000254 new = PPC_INST_NOP;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800255 return ftrace_modify_code(ip, old, new);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800256 }
257
Steven Rostedt17be5b32009-02-05 21:33:09 -0800258#ifdef CONFIG_MODULES
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800259 /*
260 * Out of range jumps are called from modules.
261 * We should either already have a pointer to the module
262 * or it has been passed in.
263 */
264 if (!rec->arch.mod) {
265 if (!mod) {
266 printk(KERN_ERR "No module loaded addr=%lx\n",
267 addr);
268 return -EFAULT;
269 }
270 rec->arch.mod = mod;
271 } else if (mod) {
272 if (mod != rec->arch.mod) {
273 printk(KERN_ERR
274 "Record mod %p not equal to passed in mod %p\n",
275 rec->arch.mod, mod);
276 return -EINVAL;
277 }
278 /* nothing to do if mod == rec->arch.mod */
279 } else
280 mod = rec->arch.mod;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800281
282 return __ftrace_make_nop(mod, rec, addr);
Steven Rostedt17be5b32009-02-05 21:33:09 -0800283#else
284 /* We should not get here without modules */
285 return -EINVAL;
286#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800287}
288
Steven Rostedt17be5b32009-02-05 21:33:09 -0800289#ifdef CONFIG_MODULES
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800290#ifdef CONFIG_PPC64
291static int
292__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
293{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800294 unsigned int op[2];
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800295 unsigned long ip = rec->ip;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800296
297 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800298 if (probe_kernel_read(op, (void *)ip, MCOUNT_INSN_SIZE * 2))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800299 return -EFAULT;
300
301 /*
302 * It should be pointing to two nops or
303 * b +8; ld r2,40(r1)
304 */
305 if (((op[0] != 0x48000008) || (op[1] != 0xe8410028)) &&
Kumar Gala16c57b32009-02-10 20:10:44 +0000306 ((op[0] != PPC_INST_NOP) || (op[1] != PPC_INST_NOP))) {
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800307 printk(KERN_ERR "Expected NOPs but have %x %x\n", op[0], op[1]);
308 return -EINVAL;
309 }
310
311 /* If we never set up a trampoline to ftrace_caller, then bail */
312 if (!rec->arch.mod->arch.tramp) {
313 printk(KERN_ERR "No ftrace trampoline\n");
314 return -EINVAL;
315 }
316
Steven Rostedt0029ff82008-11-25 14:06:19 -0800317 /* create the branch to the trampoline */
318 op[0] = create_branch((unsigned int *)ip,
319 rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
320 if (!op[0]) {
321 printk(KERN_ERR "REL24 out of range!\n");
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800322 return -EINVAL;
323 }
324
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800325 /* ld r2,40(r1) */
326 op[1] = 0xe8410028;
327
Michael Ellerman021376a2009-05-13 20:30:24 +0000328 pr_devel("write to %lx\n", rec->ip);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800329
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800330 if (probe_kernel_write((void *)ip, op, MCOUNT_INSN_SIZE * 2))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800331 return -EPERM;
332
Steven Rostedtec682ce2008-11-25 10:22:48 -0800333 flush_icache_range(ip, ip + 8);
334
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800335 return 0;
336}
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800337#else
338static int
339__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
340{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800341 unsigned int op;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500342 unsigned long ip = rec->ip;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500343
344 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800345 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500346 return -EFAULT;
347
348 /* It should be pointing to a nop */
Kumar Gala16c57b32009-02-10 20:10:44 +0000349 if (op != PPC_INST_NOP) {
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800350 printk(KERN_ERR "Expected NOP but have %x\n", op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500351 return -EINVAL;
352 }
353
354 /* If we never set up a trampoline to ftrace_caller, then bail */
355 if (!rec->arch.mod->arch.tramp) {
356 printk(KERN_ERR "No ftrace trampoline\n");
357 return -EINVAL;
358 }
359
Steven Rostedt0029ff82008-11-25 14:06:19 -0800360 /* create the branch to the trampoline */
361 op = create_branch((unsigned int *)ip,
362 rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
363 if (!op) {
364 printk(KERN_ERR "REL24 out of range!\n");
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500365 return -EINVAL;
366 }
367
Michael Ellerman021376a2009-05-13 20:30:24 +0000368 pr_devel("write to %lx\n", rec->ip);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500369
Steven Rostedt65b8c722012-04-26 08:31:19 +0000370 if (patch_instruction((unsigned int *)ip, op))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500371 return -EPERM;
372
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800373 return 0;
374}
375#endif /* CONFIG_PPC64 */
Steven Rostedt17be5b32009-02-05 21:33:09 -0800376#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800377
378int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
379{
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800380 unsigned long ip = rec->ip;
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800381 unsigned int old, new;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800382
383 /*
384 * If the calling address is more that 24 bits away,
385 * then we had to use a trampoline to make the call.
386 * Otherwise just update the call site.
387 */
388 if (test_24bit_addr(ip, addr)) {
389 /* within range */
Michael Ellerman92e02a52009-05-28 19:33:36 +0000390 old = PPC_INST_NOP;
Steven Rostedt46542882009-02-10 22:19:54 -0800391 new = ftrace_call_replace(ip, addr, 1);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800392 return ftrace_modify_code(ip, old, new);
393 }
394
Steven Rostedt17be5b32009-02-05 21:33:09 -0800395#ifdef CONFIG_MODULES
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800396 /*
397 * Out of range jumps are called from modules.
398 * Being that we are converting from nop, it had better
399 * already have a module defined.
400 */
401 if (!rec->arch.mod) {
402 printk(KERN_ERR "No module loaded\n");
403 return -EINVAL;
404 }
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800405
406 return __ftrace_make_call(rec, addr);
Steven Rostedt17be5b32009-02-05 21:33:09 -0800407#else
408 /* We should not get here without modules */
409 return -EINVAL;
410#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800411}
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800412
Steven Rostedt15adc042008-10-23 09:33:08 -0400413int ftrace_update_ftrace_func(ftrace_func_t func)
Steven Rostedt4e491d12008-05-14 23:49:44 -0400414{
415 unsigned long ip = (unsigned long)(&ftrace_call);
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800416 unsigned int old, new;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400417 int ret;
418
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800419 old = *(unsigned int *)&ftrace_call;
Steven Rostedt46542882009-02-10 22:19:54 -0800420 new = ftrace_call_replace(ip, (unsigned long)func, 1);
Steven Rostedt4e491d12008-05-14 23:49:44 -0400421 ret = ftrace_modify_code(ip, old, new);
422
423 return ret;
424}
425
Steven Rostedtee456bb2012-04-26 08:31:17 +0000426static int __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
427{
428 unsigned long ftrace_addr = (unsigned long)FTRACE_ADDR;
429 int ret;
430
431 ret = ftrace_update_record(rec, enable);
432
433 switch (ret) {
434 case FTRACE_UPDATE_IGNORE:
435 return 0;
436 case FTRACE_UPDATE_MAKE_CALL:
437 return ftrace_make_call(rec, ftrace_addr);
438 case FTRACE_UPDATE_MAKE_NOP:
439 return ftrace_make_nop(NULL, rec, ftrace_addr);
440 }
441
442 return 0;
443}
444
445void ftrace_replace_code(int enable)
446{
447 struct ftrace_rec_iter *iter;
448 struct dyn_ftrace *rec;
449 int ret;
450
451 for (iter = ftrace_rec_iter_start(); iter;
452 iter = ftrace_rec_iter_next(iter)) {
453 rec = ftrace_rec_iter_record(iter);
454 ret = __ftrace_replace_code(rec, enable);
455 if (ret) {
456 ftrace_bug(ret, rec->ip);
457 return;
458 }
459 }
460}
461
462void arch_ftrace_update_code(int command)
463{
464 if (command & FTRACE_UPDATE_CALLS)
465 ftrace_replace_code(1);
466 else if (command & FTRACE_DISABLE_CALLS)
467 ftrace_replace_code(0);
468
469 if (command & FTRACE_UPDATE_TRACE_FUNC)
470 ftrace_update_ftrace_func(ftrace_trace_function);
471
472 if (command & FTRACE_START_FUNC_RET)
473 ftrace_enable_ftrace_graph_caller();
474 else if (command & FTRACE_STOP_FUNC_RET)
475 ftrace_disable_ftrace_graph_caller();
476}
477
Jiri Slaby3a36cb12014-02-24 19:59:59 +0100478int __init ftrace_dyn_arch_init(void)
Steven Rostedt4e491d12008-05-14 23:49:44 -0400479{
Steven Rostedt4e491d12008-05-14 23:49:44 -0400480 return 0;
481}
Steven Rostedt6794c782009-02-09 21:10:27 -0800482#endif /* CONFIG_DYNAMIC_FTRACE */
483
484#ifdef CONFIG_FUNCTION_GRAPH_TRACER
485
Steven Rostedt46542882009-02-10 22:19:54 -0800486#ifdef CONFIG_DYNAMIC_FTRACE
487extern void ftrace_graph_call(void);
488extern void ftrace_graph_stub(void);
489
490int ftrace_enable_ftrace_graph_caller(void)
491{
492 unsigned long ip = (unsigned long)(&ftrace_graph_call);
493 unsigned long addr = (unsigned long)(&ftrace_graph_caller);
494 unsigned long stub = (unsigned long)(&ftrace_graph_stub);
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800495 unsigned int old, new;
Steven Rostedt46542882009-02-10 22:19:54 -0800496
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800497 old = ftrace_call_replace(ip, stub, 0);
Steven Rostedt46542882009-02-10 22:19:54 -0800498 new = ftrace_call_replace(ip, addr, 0);
499
500 return ftrace_modify_code(ip, old, new);
501}
502
503int ftrace_disable_ftrace_graph_caller(void)
504{
505 unsigned long ip = (unsigned long)(&ftrace_graph_call);
506 unsigned long addr = (unsigned long)(&ftrace_graph_caller);
507 unsigned long stub = (unsigned long)(&ftrace_graph_stub);
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800508 unsigned int old, new;
Steven Rostedt46542882009-02-10 22:19:54 -0800509
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800510 old = ftrace_call_replace(ip, addr, 0);
Steven Rostedt46542882009-02-10 22:19:54 -0800511 new = ftrace_call_replace(ip, stub, 0);
512
513 return ftrace_modify_code(ip, old, new);
514}
515#endif /* CONFIG_DYNAMIC_FTRACE */
516
Steven Rostedtbb725342009-02-11 12:45:49 -0800517#ifdef CONFIG_PPC64
518extern void mod_return_to_handler(void);
519#endif
520
Steven Rostedt6794c782009-02-09 21:10:27 -0800521/*
522 * Hook the return address and push it in the stack of return addrs
523 * in current thread info.
524 */
525void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
526{
527 unsigned long old;
Steven Rostedt6794c782009-02-09 21:10:27 -0800528 int faulted;
529 struct ftrace_graph_ent trace;
Steven Rostedtbb725342009-02-11 12:45:49 -0800530 unsigned long return_hooker = (unsigned long)&return_to_handler;
Steven Rostedt6794c782009-02-09 21:10:27 -0800531
532 if (unlikely(atomic_read(&current->tracing_graph_pause)))
533 return;
534
Michael Ellermanf4952f62009-04-06 04:40:45 +0000535#ifdef CONFIG_PPC64
Steven Rostedtbb725342009-02-11 12:45:49 -0800536 /* non core kernel code needs to save and restore the TOC */
537 if (REGION_ID(self_addr) != KERNEL_REGION_ID)
538 return_hooker = (unsigned long)&mod_return_to_handler;
539#endif
540
Michael Ellerman4a9e3f82009-05-28 19:33:34 +0000541 return_hooker = ppc_function_entry((void *)return_hooker);
Steven Rostedt6794c782009-02-09 21:10:27 -0800542
543 /*
544 * Protect against fault, even if it shouldn't
545 * happen. This tool is too much intrusive to
546 * ignore such a protection.
547 */
548 asm volatile(
549 "1: " PPC_LL "%[old], 0(%[parent])\n"
550 "2: " PPC_STL "%[return_hooker], 0(%[parent])\n"
551 " li %[faulted], 0\n"
Steven Rostedtfad4f472009-02-11 19:10:57 -0500552 "3:\n"
Steven Rostedt6794c782009-02-09 21:10:27 -0800553
554 ".section .fixup, \"ax\"\n"
555 "4: li %[faulted], 1\n"
556 " b 3b\n"
557 ".previous\n"
558
559 ".section __ex_table,\"a\"\n"
560 PPC_LONG_ALIGN "\n"
561 PPC_LONG "1b,4b\n"
562 PPC_LONG "2b,4b\n"
563 ".previous"
564
Steven Rostedtc3cf8662009-05-15 04:33:54 +0000565 : [old] "=&r" (old), [faulted] "=r" (faulted)
Steven Rostedt6794c782009-02-09 21:10:27 -0800566 : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
567 : "memory"
568 );
569
570 if (unlikely(faulted)) {
571 ftrace_graph_stop();
572 WARN_ON(1);
573 return;
574 }
575
Steven Rostedtbac821a2012-07-18 12:35:28 +0000576 trace.func = self_addr;
577 trace.depth = current->curr_ret_stack + 1;
578
579 /* Only trace if the calling function expects to */
580 if (!ftrace_graph_entry(&trace)) {
Steven Rostedt6794c782009-02-09 21:10:27 -0800581 *parent = old;
582 return;
583 }
584
Steven Rostedtbac821a2012-07-18 12:35:28 +0000585 if (ftrace_push_return_trace(old, self_addr, &trace.depth, 0) == -EBUSY)
Steven Rostedt6794c782009-02-09 21:10:27 -0800586 *parent = old;
Steven Rostedt6794c782009-02-09 21:10:27 -0800587}
588#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Ian Munsie02424d82011-02-02 17:27:24 +0000589
590#if defined(CONFIG_FTRACE_SYSCALLS) && defined(CONFIG_PPC64)
591unsigned long __init arch_syscall_addr(int nr)
592{
593 return sys_call_table[nr*2];
594}
595#endif /* CONFIG_FTRACE_SYSCALLS && CONFIG_PPC64 */