]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/powerpc/kernel/ftrace.c
Merge branch 'linus' into tracing/sysprof
[karo-tx-linux.git] / arch / powerpc / kernel / ftrace.c
1 /*
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
21 static 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
30 notrace int ftrace_ip_converted(unsigned long ip)
31 {
32         unsigned int save;
33
34         ip -= CALL_BACK;
35         save = *(unsigned int *)ip;
36
37         return save == ftrace_nop;
38 }
39
40 static unsigned int notrace ftrace_calc_offset(long ip, long addr)
41 {
42         return (int)((addr + CALL_BACK) - ip);
43 }
44
45 notrace unsigned char *ftrace_nop_replace(void)
46 {
47         return (char *)&ftrace_nop;
48 }
49
50 notrace unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
51 {
52         static unsigned int op;
53
54         addr = GET_ADDR(addr);
55
56         /* Set to "bl addr" */
57         op = 0x48000001 | (ftrace_calc_offset(ip, addr) & 0x03fffffe);
58
59         /*
60          * No locking needed, this must be called via kstop_machine
61          * which in essence is like running on a uniprocessor machine.
62          */
63         return (unsigned char *)&op;
64 }
65
66 #ifdef CONFIG_PPC64
67 # define _ASM_ALIGN     " .align 3 "
68 # define _ASM_PTR       " .llong "
69 #else
70 # define _ASM_ALIGN     " .align 2 "
71 # define _ASM_PTR       " .long "
72 #endif
73
74 notrace int
75 ftrace_modify_code(unsigned long ip, unsigned char *old_code,
76                    unsigned char *new_code)
77 {
78         unsigned replaced;
79         unsigned old = *(unsigned *)old_code;
80         unsigned new = *(unsigned *)new_code;
81         int faulted = 0;
82
83         /* move the IP back to the start of the call */
84         ip -= CALL_BACK;
85
86         /*
87          * Note: Due to modules and __init, code can
88          *  disappear and change, we need to protect against faulting
89          *  as well as code changing.
90          *
91          * No real locking needed, this code is run through
92          * kstop_machine.
93          */
94         asm volatile (
95                 "1: lwz         %1, 0(%2)\n"
96                 "   cmpw        %1, %5\n"
97                 "   bne         2f\n"
98                 "   stwu        %3, 0(%2)\n"
99                 "2:\n"
100                 ".section .fixup, \"ax\"\n"
101                 "3:     li %0, 1\n"
102                 "       b 2b\n"
103                 ".previous\n"
104                 ".section __ex_table,\"a\"\n"
105                 _ASM_ALIGN "\n"
106                 _ASM_PTR "1b, 3b\n"
107                 ".previous"
108                 : "=r"(faulted), "=r"(replaced)
109                 : "r"(ip), "r"(new),
110                   "0"(faulted), "r"(old)
111                 : "memory");
112
113         if (replaced != old && replaced != new)
114                 faulted = 2;
115
116         if (!faulted)
117                 flush_icache_range(ip, ip + 8);
118
119         return faulted;
120 }
121
122 notrace int ftrace_update_ftrace_func(ftrace_func_t func)
123 {
124         unsigned long ip = (unsigned long)(&ftrace_call);
125         unsigned char old[4], *new;
126         int ret;
127
128         ip += CALL_BACK;
129
130         memcpy(old, &ftrace_call, 4);
131         new = ftrace_call_replace(ip, (unsigned long)func);
132         ret = ftrace_modify_code(ip, old, new);
133
134         return ret;
135 }
136
137 notrace int ftrace_mcount_set(unsigned long *data)
138 {
139         unsigned long ip = (long)(&mcount_call);
140         unsigned long *addr = data;
141         unsigned char old[4], *new;
142
143         /* ip is at the location, but modify code will subtact this */
144         ip += CALL_BACK;
145
146         /*
147          * Replace the mcount stub with a pointer to the
148          * ip recorder function.
149          */
150         memcpy(old, &mcount_call, 4);
151         new = ftrace_call_replace(ip, *addr);
152         *addr = ftrace_modify_code(ip, old, new);
153
154         return 0;
155 }
156
157 int __init ftrace_dyn_arch_init(void *data)
158 {
159         /* This is running in kstop_machine */
160
161         ftrace_mcount_set(data);
162
163         return 0;
164 }
165