blob: d7950317d3a1d5e126e49835e01d2408c613b111 [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
Michael Ellerman072c4c02014-06-17 16:15:36 +100013#define pr_fmt(fmt) "ftrace-powerpc: " fmt
14
Steven Rostedt4e491d12008-05-14 23:49:44 -040015#include <linux/spinlock.h>
16#include <linux/hardirq.h>
Steven Rostedte4486fe2008-11-14 16:21:20 -080017#include <linux/uaccess.h>
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080018#include <linux/module.h>
Steven Rostedt4e491d12008-05-14 23:49:44 -040019#include <linux/ftrace.h>
20#include <linux/percpu.h>
21#include <linux/init.h>
22#include <linux/list.h>
23
24#include <asm/cacheflush.h>
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080025#include <asm/code-patching.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053026#include <asm/ftrace.h>
Ian Munsie02424d82011-02-02 17:27:24 +000027#include <asm/syscall.h>
Steven Rostedt4e491d12008-05-14 23:49:44 -040028
Steven Rostedt4e491d12008-05-14 23:49:44 -040029
Steven Rostedt6794c782009-02-09 21:10:27 -080030#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedtb54dcfe2009-02-13 06:31:39 -080031static unsigned int
Steven Rostedt46542882009-02-10 22:19:54 -080032ftrace_call_replace(unsigned long ip, unsigned long addr, int link)
Steven Rostedt4e491d12008-05-14 23:49:44 -040033{
Steven Rostedtb54dcfe2009-02-13 06:31:39 -080034 unsigned int op;
Steven Rostedt4e491d12008-05-14 23:49:44 -040035
Michael Ellerman4a9e3f82009-05-28 19:33:34 +000036 addr = ppc_function_entry((void *)addr);
Steven Rostedt4e491d12008-05-14 23:49:44 -040037
Steven Rostedt46542882009-02-10 22:19:54 -080038 /* if (link) set op to 'bl' else 'b' */
Steven Rostedtbb9b9032009-02-13 06:45:27 -080039 op = create_branch((unsigned int *)ip, addr, link ? 1 : 0);
Steven Rostedt4e491d12008-05-14 23:49:44 -040040
Steven Rostedtb54dcfe2009-02-13 06:31:39 -080041 return op;
Steven Rostedt4e491d12008-05-14 23:49:44 -040042}
43
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080044static int
Steven Rostedtb54dcfe2009-02-13 06:31:39 -080045ftrace_modify_code(unsigned long ip, unsigned int old, unsigned int new)
Steven Rostedt4e491d12008-05-14 23:49:44 -040046{
Steven Rostedtb54dcfe2009-02-13 06:31:39 -080047 unsigned int replaced;
Steven Rostedt4e491d12008-05-14 23:49:44 -040048
Steven Rostedt4e491d12008-05-14 23:49:44 -040049 /*
50 * Note: Due to modules and __init, code can
51 * disappear and change, we need to protect against faulting
Steven Rostedte4486fe2008-11-14 16:21:20 -080052 * as well as code changing. We do this by using the
53 * probe_kernel_* functions.
Steven Rostedt4e491d12008-05-14 23:49:44 -040054 *
55 * No real locking needed, this code is run through
Steven Rostedte4486fe2008-11-14 16:21:20 -080056 * kstop_machine, or before SMP starts.
Steven Rostedt4e491d12008-05-14 23:49:44 -040057 */
Steven Rostedt4e491d12008-05-14 23:49:44 -040058
Steven Rostedte4486fe2008-11-14 16:21:20 -080059 /* read the text we want to modify */
Steven Rostedtb54dcfe2009-02-13 06:31:39 -080060 if (probe_kernel_read(&replaced, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedte4486fe2008-11-14 16:21:20 -080061 return -EFAULT;
Steven Rostedt4e491d12008-05-14 23:49:44 -040062
Steven Rostedte4486fe2008-11-14 16:21:20 -080063 /* Make sure it is what we expect it to be */
Steven Rostedtb54dcfe2009-02-13 06:31:39 -080064 if (replaced != old)
Steven Rostedte4486fe2008-11-14 16:21:20 -080065 return -EINVAL;
Steven Rostedt4e491d12008-05-14 23:49:44 -040066
Steven Rostedte4486fe2008-11-14 16:21:20 -080067 /* replace the text with the new text */
Steven Rostedt65b8c722012-04-26 08:31:19 +000068 if (patch_instruction((unsigned int *)ip, new))
Steven Rostedte4486fe2008-11-14 16:21:20 -080069 return -EPERM;
70
Steven Rostedte4486fe2008-11-14 16:21:20 -080071 return 0;
Steven Rostedt4e491d12008-05-14 23:49:44 -040072}
73
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080074/*
75 * Helper functions that are the same for both PPC64 and PPC32.
76 */
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080077static int test_24bit_addr(unsigned long ip, unsigned long addr)
78{
Liu Ping Fana95fc582014-02-26 10:23:01 +080079 addr = ppc_function_entry((void *)addr);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080080
Steven Rostedt0029ff82008-11-25 14:06:19 -080081 /* use the create_branch to verify that this offset can be branched */
82 return create_branch((unsigned int *)ip, addr, 0);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080083}
84
Steven Rostedt17be5b32009-02-05 21:33:09 -080085#ifdef CONFIG_MODULES
86
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080087static int is_bl_op(unsigned int op)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080088{
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080089 return (op & 0xfc000003) == 0x48000001;
90}
91
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080092static unsigned long find_bl_target(unsigned long ip, unsigned int op)
93{
94 static int offset;
95
96 offset = (op & 0x03fffffc);
97 /* make it signed */
98 if (offset & 0x02000000)
99 offset |= 0xfe000000;
100
101 return ip + (long)offset;
102}
103
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800104#ifdef CONFIG_PPC64
105static int
106__ftrace_make_nop(struct module *mod,
107 struct dyn_ftrace *rec, unsigned long addr)
108{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800109 unsigned int op;
Michael Ellermand84e0d62014-06-17 16:15:35 +1000110 unsigned long entry, ptr;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800111 unsigned long ip = rec->ip;
Anton Blanchard62c9da62014-04-04 16:52:58 +1100112 void *tramp;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800113
114 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800115 if (probe_kernel_read(&op, (void *)ip, sizeof(int)))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800116 return -EFAULT;
117
118 /* Make sure that that this is still a 24bit jump */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800119 if (!is_bl_op(op)) {
Michael Ellerman072c4c02014-06-17 16:15:36 +1000120 pr_err("Not expected bl: opcode is %x\n", op);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800121 return -EINVAL;
122 }
123
124 /* lets find where the pointer goes */
Anton Blanchard62c9da62014-04-04 16:52:58 +1100125 tramp = (void *)find_bl_target(ip, op);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800126
Anton Blanchard62c9da62014-04-04 16:52:58 +1100127 pr_devel("ip:%lx jumps to %p", ip, tramp);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800128
Anton Blanchard62c9da62014-04-04 16:52:58 +1100129 if (!is_module_trampoline(tramp)) {
Michael Ellerman072c4c02014-06-17 16:15:36 +1000130 pr_err("Not a trampoline\n");
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800131 return -EINVAL;
132 }
133
Anton Blanchard62c9da62014-04-04 16:52:58 +1100134 if (module_trampoline_target(mod, tramp, &ptr)) {
Michael Ellerman072c4c02014-06-17 16:15:36 +1000135 pr_err("Failed to get trampoline target\n");
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800136 return -EFAULT;
137 }
138
Anton Blanchard62c9da62014-04-04 16:52:58 +1100139 pr_devel("trampoline target %lx", ptr);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800140
Michael Ellermand84e0d62014-06-17 16:15:35 +1000141 entry = ppc_global_function_entry((void *)addr);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800142 /* This should match what was called */
Michael Ellermand84e0d62014-06-17 16:15:35 +1000143 if (ptr != entry) {
Michael Ellerman072c4c02014-06-17 16:15:36 +1000144 pr_err("addr %lx does not match expected %lx\n", ptr, entry);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800145 return -EINVAL;
146 }
147
148 /*
Anton Blanchard62c9da62014-04-04 16:52:58 +1100149 * Our original call site looks like:
150 *
151 * bl <tramp>
152 * ld r2,XX(r1)
153 *
154 * Milton Miller pointed out that we can not simply nop the branch.
155 * If a task was preempted when calling a trace function, the nops
156 * will remove the way to restore the TOC in r2 and the r2 TOC will
157 * get corrupted.
158 *
159 * Use a b +8 to jump over the load.
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800160 */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800161 op = 0x48000008; /* b +8 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800162
Steven Rostedt65b8c722012-04-26 08:31:19 +0000163 if (patch_instruction((unsigned int *)ip, op))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800164 return -EPERM;
165
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800166 return 0;
167}
168
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800169#else /* !PPC64 */
170static int
171__ftrace_make_nop(struct module *mod,
172 struct dyn_ftrace *rec, unsigned long addr)
173{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800174 unsigned int op;
175 unsigned int jmp[4];
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500176 unsigned long ip = rec->ip;
177 unsigned long tramp;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500178
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800179 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500180 return -EFAULT;
181
182 /* Make sure that that this is still a 24bit jump */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800183 if (!is_bl_op(op)) {
Michael Ellerman072c4c02014-06-17 16:15:36 +1000184 pr_err("Not expected bl: opcode is %x\n", op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500185 return -EINVAL;
186 }
187
188 /* lets find where the pointer goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800189 tramp = find_bl_target(ip, op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500190
191 /*
192 * On PPC32 the trampoline looks like:
roger blofeldfd5a4292012-06-21 05:27:14 +0000193 * 0x3d, 0x80, 0x00, 0x00 lis r12,sym@ha
194 * 0x39, 0x8c, 0x00, 0x00 addi r12,r12,sym@l
195 * 0x7d, 0x89, 0x03, 0xa6 mtctr r12
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800196 * 0x4e, 0x80, 0x04, 0x20 bctr
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500197 */
198
Michael Ellerman021376a2009-05-13 20:30:24 +0000199 pr_devel("ip:%lx jumps to %lx", ip, tramp);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500200
201 /* Find where the trampoline jumps to */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800202 if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
Michael Ellerman072c4c02014-06-17 16:15:36 +1000203 pr_err("Failed to read %lx\n", tramp);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500204 return -EFAULT;
205 }
206
Michael Ellerman021376a2009-05-13 20:30:24 +0000207 pr_devel(" %08x %08x ", jmp[0], jmp[1]);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500208
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800209 /* verify that this is what we expect it to be */
roger blofeldfd5a4292012-06-21 05:27:14 +0000210 if (((jmp[0] & 0xffff0000) != 0x3d800000) ||
211 ((jmp[1] & 0xffff0000) != 0x398c0000) ||
212 (jmp[2] != 0x7d8903a6) ||
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800213 (jmp[3] != 0x4e800420)) {
Michael Ellerman072c4c02014-06-17 16:15:36 +1000214 pr_err("Not a trampoline\n");
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800215 return -EINVAL;
216 }
217
218 tramp = (jmp[1] & 0xffff) |
219 ((jmp[0] & 0xffff) << 16);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500220 if (tramp & 0x8000)
221 tramp -= 0x10000;
222
Michael Ellerman021376a2009-05-13 20:30:24 +0000223 pr_devel(" %lx ", tramp);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500224
225 if (tramp != addr) {
Michael Ellerman072c4c02014-06-17 16:15:36 +1000226 pr_err("Trampoline location %08lx does not match addr\n",
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500227 tramp);
228 return -EINVAL;
229 }
230
Kumar Gala16c57b32009-02-10 20:10:44 +0000231 op = PPC_INST_NOP;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500232
Steven Rostedt65b8c722012-04-26 08:31:19 +0000233 if (patch_instruction((unsigned int *)ip, op))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500234 return -EPERM;
235
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800236 return 0;
237}
238#endif /* PPC64 */
Steven Rostedt17be5b32009-02-05 21:33:09 -0800239#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800240
241int ftrace_make_nop(struct module *mod,
242 struct dyn_ftrace *rec, unsigned long addr)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800243{
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800244 unsigned long ip = rec->ip;
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800245 unsigned int old, new;
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800246
247 /*
248 * If the calling address is more that 24 bits away,
249 * then we had to use a trampoline to make the call.
250 * Otherwise just update the call site.
251 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800252 if (test_24bit_addr(ip, addr)) {
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800253 /* within range */
Steven Rostedt46542882009-02-10 22:19:54 -0800254 old = ftrace_call_replace(ip, addr, 1);
Michael Ellerman92e02a52009-05-28 19:33:36 +0000255 new = PPC_INST_NOP;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800256 return ftrace_modify_code(ip, old, new);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800257 }
258
Steven Rostedt17be5b32009-02-05 21:33:09 -0800259#ifdef CONFIG_MODULES
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800260 /*
261 * Out of range jumps are called from modules.
262 * We should either already have a pointer to the module
263 * or it has been passed in.
264 */
265 if (!rec->arch.mod) {
266 if (!mod) {
Michael Ellerman072c4c02014-06-17 16:15:36 +1000267 pr_err("No module loaded addr=%lx\n", addr);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800268 return -EFAULT;
269 }
270 rec->arch.mod = mod;
271 } else if (mod) {
272 if (mod != rec->arch.mod) {
Michael Ellerman072c4c02014-06-17 16:15:36 +1000273 pr_err("Record mod %p not equal to passed in mod %p\n",
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800274 rec->arch.mod, mod);
275 return -EINVAL;
276 }
277 /* nothing to do if mod == rec->arch.mod */
278 } else
279 mod = rec->arch.mod;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800280
281 return __ftrace_make_nop(mod, rec, addr);
Steven Rostedt17be5b32009-02-05 21:33:09 -0800282#else
283 /* We should not get here without modules */
284 return -EINVAL;
285#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800286}
287
Steven Rostedt17be5b32009-02-05 21:33:09 -0800288#ifdef CONFIG_MODULES
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800289#ifdef CONFIG_PPC64
290static int
291__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
292{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800293 unsigned int op[2];
Anton Blanchard24a1bdc2014-04-04 16:54:04 +1100294 void *ip = (void *)rec->ip;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800295
296 /* read where this goes */
Anton Blanchard24a1bdc2014-04-04 16:54:04 +1100297 if (probe_kernel_read(op, ip, sizeof(op)))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800298 return -EFAULT;
299
300 /*
Anton Blanchard24a1bdc2014-04-04 16:54:04 +1100301 * We expect to see:
302 *
303 * b +8
304 * ld r2,XX(r1)
305 *
306 * The load offset is different depending on the ABI. For simplicity
307 * just mask it out when doing the compare.
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800308 */
Michael Ellermandfc382a2014-06-17 16:15:33 +1000309 if ((op[0] != 0x48000008) || ((op[1] & 0xffff0000) != 0xe8410000)) {
Michael Ellerman072c4c02014-06-17 16:15:36 +1000310 pr_err("Unexpected call sequence: %x %x\n", op[0], op[1]);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800311 return -EINVAL;
312 }
313
314 /* If we never set up a trampoline to ftrace_caller, then bail */
315 if (!rec->arch.mod->arch.tramp) {
Michael Ellerman072c4c02014-06-17 16:15:36 +1000316 pr_err("No ftrace trampoline\n");
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800317 return -EINVAL;
318 }
319
Anton Blanchard24a1bdc2014-04-04 16:54:04 +1100320 /* Ensure branch is within 24 bits */
Michael Ellermanb7b348c2014-06-17 16:15:34 +1000321 if (!create_branch(ip, rec->arch.mod->arch.tramp, BRANCH_SET_LINK)) {
Michael Ellerman072c4c02014-06-17 16:15:36 +1000322 pr_err("Branch out of range\n");
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800323 return -EINVAL;
324 }
325
Anton Blanchard24a1bdc2014-04-04 16:54:04 +1100326 if (patch_branch(ip, rec->arch.mod->arch.tramp, BRANCH_SET_LINK)) {
Michael Ellerman072c4c02014-06-17 16:15:36 +1000327 pr_err("REL24 out of range!\n");
Anton Blanchard24a1bdc2014-04-04 16:54:04 +1100328 return -EINVAL;
329 }
Steven Rostedtec682ce2008-11-25 10:22:48 -0800330
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800331 return 0;
332}
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800333#else
334static int
335__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
336{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800337 unsigned int op;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500338 unsigned long ip = rec->ip;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500339
340 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800341 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500342 return -EFAULT;
343
344 /* It should be pointing to a nop */
Kumar Gala16c57b32009-02-10 20:10:44 +0000345 if (op != PPC_INST_NOP) {
Michael Ellerman072c4c02014-06-17 16:15:36 +1000346 pr_err("Expected NOP but have %x\n", op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500347 return -EINVAL;
348 }
349
350 /* If we never set up a trampoline to ftrace_caller, then bail */
351 if (!rec->arch.mod->arch.tramp) {
Michael Ellerman072c4c02014-06-17 16:15:36 +1000352 pr_err("No ftrace trampoline\n");
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500353 return -EINVAL;
354 }
355
Steven Rostedt0029ff82008-11-25 14:06:19 -0800356 /* create the branch to the trampoline */
357 op = create_branch((unsigned int *)ip,
358 rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
359 if (!op) {
Michael Ellerman072c4c02014-06-17 16:15:36 +1000360 pr_err("REL24 out of range!\n");
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500361 return -EINVAL;
362 }
363
Michael Ellerman021376a2009-05-13 20:30:24 +0000364 pr_devel("write to %lx\n", rec->ip);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500365
Steven Rostedt65b8c722012-04-26 08:31:19 +0000366 if (patch_instruction((unsigned int *)ip, op))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500367 return -EPERM;
368
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800369 return 0;
370}
371#endif /* CONFIG_PPC64 */
Steven Rostedt17be5b32009-02-05 21:33:09 -0800372#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800373
374int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
375{
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800376 unsigned long ip = rec->ip;
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800377 unsigned int old, new;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800378
379 /*
380 * If the calling address is more that 24 bits away,
381 * then we had to use a trampoline to make the call.
382 * Otherwise just update the call site.
383 */
384 if (test_24bit_addr(ip, addr)) {
385 /* within range */
Michael Ellerman92e02a52009-05-28 19:33:36 +0000386 old = PPC_INST_NOP;
Steven Rostedt46542882009-02-10 22:19:54 -0800387 new = ftrace_call_replace(ip, addr, 1);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800388 return ftrace_modify_code(ip, old, new);
389 }
390
Steven Rostedt17be5b32009-02-05 21:33:09 -0800391#ifdef CONFIG_MODULES
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800392 /*
393 * Out of range jumps are called from modules.
394 * Being that we are converting from nop, it had better
395 * already have a module defined.
396 */
397 if (!rec->arch.mod) {
Michael Ellerman072c4c02014-06-17 16:15:36 +1000398 pr_err("No module loaded\n");
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800399 return -EINVAL;
400 }
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800401
402 return __ftrace_make_call(rec, addr);
Steven Rostedt17be5b32009-02-05 21:33:09 -0800403#else
404 /* We should not get here without modules */
405 return -EINVAL;
406#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800407}
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800408
Steven Rostedt15adc042008-10-23 09:33:08 -0400409int ftrace_update_ftrace_func(ftrace_func_t func)
Steven Rostedt4e491d12008-05-14 23:49:44 -0400410{
411 unsigned long ip = (unsigned long)(&ftrace_call);
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800412 unsigned int old, new;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400413 int ret;
414
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800415 old = *(unsigned int *)&ftrace_call;
Steven Rostedt46542882009-02-10 22:19:54 -0800416 new = ftrace_call_replace(ip, (unsigned long)func, 1);
Steven Rostedt4e491d12008-05-14 23:49:44 -0400417 ret = ftrace_modify_code(ip, old, new);
418
419 return ret;
420}
421
Steven Rostedtee456bb2012-04-26 08:31:17 +0000422static int __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
423{
424 unsigned long ftrace_addr = (unsigned long)FTRACE_ADDR;
425 int ret;
426
427 ret = ftrace_update_record(rec, enable);
428
429 switch (ret) {
430 case FTRACE_UPDATE_IGNORE:
431 return 0;
432 case FTRACE_UPDATE_MAKE_CALL:
433 return ftrace_make_call(rec, ftrace_addr);
434 case FTRACE_UPDATE_MAKE_NOP:
435 return ftrace_make_nop(NULL, rec, ftrace_addr);
436 }
437
438 return 0;
439}
440
441void ftrace_replace_code(int enable)
442{
443 struct ftrace_rec_iter *iter;
444 struct dyn_ftrace *rec;
445 int ret;
446
447 for (iter = ftrace_rec_iter_start(); iter;
448 iter = ftrace_rec_iter_next(iter)) {
449 rec = ftrace_rec_iter_record(iter);
450 ret = __ftrace_replace_code(rec, enable);
451 if (ret) {
452 ftrace_bug(ret, rec->ip);
453 return;
454 }
455 }
456}
457
458void arch_ftrace_update_code(int command)
459{
460 if (command & FTRACE_UPDATE_CALLS)
461 ftrace_replace_code(1);
462 else if (command & FTRACE_DISABLE_CALLS)
463 ftrace_replace_code(0);
464
465 if (command & FTRACE_UPDATE_TRACE_FUNC)
466 ftrace_update_ftrace_func(ftrace_trace_function);
467
468 if (command & FTRACE_START_FUNC_RET)
469 ftrace_enable_ftrace_graph_caller();
470 else if (command & FTRACE_STOP_FUNC_RET)
471 ftrace_disable_ftrace_graph_caller();
472}
473
Jiri Slaby3a36cb12014-02-24 19:59:59 +0100474int __init ftrace_dyn_arch_init(void)
Steven Rostedt4e491d12008-05-14 23:49:44 -0400475{
Steven Rostedt4e491d12008-05-14 23:49:44 -0400476 return 0;
477}
Steven Rostedt6794c782009-02-09 21:10:27 -0800478#endif /* CONFIG_DYNAMIC_FTRACE */
479
480#ifdef CONFIG_FUNCTION_GRAPH_TRACER
481
Steven Rostedt46542882009-02-10 22:19:54 -0800482#ifdef CONFIG_DYNAMIC_FTRACE
483extern void ftrace_graph_call(void);
484extern void ftrace_graph_stub(void);
485
486int ftrace_enable_ftrace_graph_caller(void)
487{
488 unsigned long ip = (unsigned long)(&ftrace_graph_call);
489 unsigned long addr = (unsigned long)(&ftrace_graph_caller);
490 unsigned long stub = (unsigned long)(&ftrace_graph_stub);
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800491 unsigned int old, new;
Steven Rostedt46542882009-02-10 22:19:54 -0800492
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800493 old = ftrace_call_replace(ip, stub, 0);
Steven Rostedt46542882009-02-10 22:19:54 -0800494 new = ftrace_call_replace(ip, addr, 0);
495
496 return ftrace_modify_code(ip, old, new);
497}
498
499int ftrace_disable_ftrace_graph_caller(void)
500{
501 unsigned long ip = (unsigned long)(&ftrace_graph_call);
502 unsigned long addr = (unsigned long)(&ftrace_graph_caller);
503 unsigned long stub = (unsigned long)(&ftrace_graph_stub);
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800504 unsigned int old, new;
Steven Rostedt46542882009-02-10 22:19:54 -0800505
Steven Rostedtb54dcfe2009-02-13 06:31:39 -0800506 old = ftrace_call_replace(ip, addr, 0);
Steven Rostedt46542882009-02-10 22:19:54 -0800507 new = ftrace_call_replace(ip, stub, 0);
508
509 return ftrace_modify_code(ip, old, new);
510}
511#endif /* CONFIG_DYNAMIC_FTRACE */
512
Steven Rostedt6794c782009-02-09 21:10:27 -0800513/*
514 * Hook the return address and push it in the stack of return addrs
Anton Blanchardb3c18722014-09-17 17:07:04 +1000515 * in current thread info. Return the address we want to divert to.
Steven Rostedt6794c782009-02-09 21:10:27 -0800516 */
Anton Blanchardb3c18722014-09-17 17:07:04 +1000517unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip)
Steven Rostedt6794c782009-02-09 21:10:27 -0800518{
Steven Rostedt6794c782009-02-09 21:10:27 -0800519 struct ftrace_graph_ent trace;
Anton Blanchard7d56c652014-09-17 17:07:03 +1000520 unsigned long return_hooker;
Steven Rostedt6794c782009-02-09 21:10:27 -0800521
Steven Rostedt (Red Hat)96d4f432014-06-25 10:27:30 -0400522 if (unlikely(ftrace_graph_is_dead()))
Anton Blanchardb3c18722014-09-17 17:07:04 +1000523 goto out;
Steven Rostedt (Red Hat)96d4f432014-06-25 10:27:30 -0400524
Steven Rostedt6794c782009-02-09 21:10:27 -0800525 if (unlikely(atomic_read(&current->tracing_graph_pause)))
Anton Blanchardb3c18722014-09-17 17:07:04 +1000526 goto out;
Steven Rostedt6794c782009-02-09 21:10:27 -0800527
Anton Blanchard7d56c652014-09-17 17:07:03 +1000528 return_hooker = ppc_function_entry(return_to_handler);
Steven Rostedt6794c782009-02-09 21:10:27 -0800529
Anton Blanchardb3c18722014-09-17 17:07:04 +1000530 trace.func = ip;
Steven Rostedtbac821a2012-07-18 12:35:28 +0000531 trace.depth = current->curr_ret_stack + 1;
532
533 /* Only trace if the calling function expects to */
Anton Blanchardb3c18722014-09-17 17:07:04 +1000534 if (!ftrace_graph_entry(&trace))
535 goto out;
Steven Rostedt6794c782009-02-09 21:10:27 -0800536
Anton Blanchardb3c18722014-09-17 17:07:04 +1000537 if (ftrace_push_return_trace(parent, ip, &trace.depth, 0) == -EBUSY)
538 goto out;
539
540 parent = return_hooker;
541out:
542 return parent;
Steven Rostedt6794c782009-02-09 21:10:27 -0800543}
544#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Ian Munsie02424d82011-02-02 17:27:24 +0000545
546#if defined(CONFIG_FTRACE_SYSCALLS) && defined(CONFIG_PPC64)
547unsigned long __init arch_syscall_addr(int nr)
548{
549 return sys_call_table[nr*2];
550}
551#endif /* CONFIG_FTRACE_SYSCALLS && CONFIG_PPC64 */