blob: e12c593ab9cacc94ceae3dcf8cf6d3953cf5c4e5 [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 *
8 */
9
10#include <linux/spinlock.h>
11#include <linux/hardirq.h>
12#include <linux/ftrace.h>
13#include <linux/percpu.h>
14#include <linux/init.h>
15#include <linux/list.h>
16
17#include <asm/cacheflush.h>
18
19#define CALL_BACK 4
20
21static unsigned int ftrace_nop = 0x60000000;
22
23#ifdef CONFIG_PPC32
24# define GET_ADDR(addr) addr
25#else
26/* PowerPC64's functions are data that points to the functions */
27# define GET_ADDR(addr) *(unsigned long *)addr
28#endif
29
Steven Rostedt4e491d12008-05-14 23:49:44 -040030static unsigned int notrace ftrace_calc_offset(long ip, long addr)
31{
32 return (int)((addr + CALL_BACK) - ip);
33}
34
35notrace unsigned char *ftrace_nop_replace(void)
36{
37 return (char *)&ftrace_nop;
38}
39
40notrace unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
41{
42 static unsigned int op;
43
Steven Rostedtccbfac22008-05-22 14:31:07 -040044 /*
45 * It would be nice to just use create_function_call, but that will
46 * update the code itself. Here we need to just return the
47 * instruction that is going to be modified, without modifying the
48 * code.
49 */
Steven Rostedt4e491d12008-05-14 23:49:44 -040050 addr = GET_ADDR(addr);
51
52 /* Set to "bl addr" */
Steven Rostedtccbfac22008-05-22 14:31:07 -040053 op = 0x48000001 | (ftrace_calc_offset(ip, addr) & 0x03fffffc);
Steven Rostedt4e491d12008-05-14 23:49:44 -040054
55 /*
56 * No locking needed, this must be called via kstop_machine
57 * which in essence is like running on a uniprocessor machine.
58 */
59 return (unsigned char *)&op;
60}
61
62#ifdef CONFIG_PPC64
63# define _ASM_ALIGN " .align 3 "
64# define _ASM_PTR " .llong "
65#else
66# define _ASM_ALIGN " .align 2 "
67# define _ASM_PTR " .long "
68#endif
69
70notrace int
71ftrace_modify_code(unsigned long ip, unsigned char *old_code,
72 unsigned char *new_code)
73{
74 unsigned replaced;
75 unsigned old = *(unsigned *)old_code;
76 unsigned new = *(unsigned *)new_code;
77 int faulted = 0;
78
79 /* move the IP back to the start of the call */
80 ip -= CALL_BACK;
81
82 /*
83 * Note: Due to modules and __init, code can
84 * disappear and change, we need to protect against faulting
85 * as well as code changing.
86 *
87 * No real locking needed, this code is run through
88 * kstop_machine.
89 */
90 asm volatile (
91 "1: lwz %1, 0(%2)\n"
92 " cmpw %1, %5\n"
93 " bne 2f\n"
94 " stwu %3, 0(%2)\n"
95 "2:\n"
96 ".section .fixup, \"ax\"\n"
97 "3: li %0, 1\n"
98 " b 2b\n"
99 ".previous\n"
100 ".section __ex_table,\"a\"\n"
101 _ASM_ALIGN "\n"
102 _ASM_PTR "1b, 3b\n"
103 ".previous"
104 : "=r"(faulted), "=r"(replaced)
105 : "r"(ip), "r"(new),
106 "0"(faulted), "r"(old)
107 : "memory");
108
109 if (replaced != old && replaced != new)
110 faulted = 2;
111
112 if (!faulted)
113 flush_icache_range(ip, ip + 8);
114
115 return faulted;
116}
117
118notrace int ftrace_update_ftrace_func(ftrace_func_t func)
119{
120 unsigned long ip = (unsigned long)(&ftrace_call);
121 unsigned char old[4], *new;
122 int ret;
123
124 ip += CALL_BACK;
125
126 memcpy(old, &ftrace_call, 4);
127 new = ftrace_call_replace(ip, (unsigned long)func);
128 ret = ftrace_modify_code(ip, old, new);
129
130 return ret;
131}
132
133notrace int ftrace_mcount_set(unsigned long *data)
134{
135 unsigned long ip = (long)(&mcount_call);
136 unsigned long *addr = data;
137 unsigned char old[4], *new;
138
139 /* ip is at the location, but modify code will subtact this */
140 ip += CALL_BACK;
141
142 /*
143 * Replace the mcount stub with a pointer to the
144 * ip recorder function.
145 */
146 memcpy(old, &mcount_call, 4);
147 new = ftrace_call_replace(ip, *addr);
148 *addr = ftrace_modify_code(ip, old, new);
149
150 return 0;
151}
152
153int __init ftrace_dyn_arch_init(void *data)
154{
155 /* This is running in kstop_machine */
156
157 ftrace_mcount_set(data);
158
159 return 0;
160}
161