1adfbb268d8ee1d1688b7176acdac03a3e490e98
[firefly-linux-kernel-4.4.55.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/uaccess.h>
13 #include <linux/ftrace.h>
14 #include <linux/percpu.h>
15 #include <linux/init.h>
16 #include <linux/list.h>
17
18 #include <asm/cacheflush.h>
19 #include <asm/ftrace.h>
20
21
22 static unsigned int ftrace_nop = 0x60000000;
23
24 #ifdef CONFIG_PPC32
25 # define GET_ADDR(addr) addr
26 #else
27 /* PowerPC64's functions are data that points to the functions */
28 # define GET_ADDR(addr) *(unsigned long *)addr
29 #endif
30
31
32 static unsigned int ftrace_calc_offset(long ip, long addr)
33 {
34         return (int)(addr - ip);
35 }
36
37 static unsigned char *ftrace_nop_replace(void)
38 {
39         return (char *)&ftrace_nop;
40 }
41
42 static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
43 {
44         static unsigned int op;
45
46         /*
47          * It would be nice to just use create_function_call, but that will
48          * update the code itself. Here we need to just return the
49          * instruction that is going to be modified, without modifying the
50          * code.
51          */
52         addr = GET_ADDR(addr);
53
54         /* Set to "bl addr" */
55         op = 0x48000001 | (ftrace_calc_offset(ip, addr) & 0x03fffffc);
56
57         /*
58          * No locking needed, this must be called via kstop_machine
59          * which in essence is like running on a uniprocessor machine.
60          */
61         return (unsigned char *)&op;
62 }
63
64 #ifdef CONFIG_PPC64
65 # define _ASM_ALIGN     " .align 3 "
66 # define _ASM_PTR       " .llong "
67 #else
68 # define _ASM_ALIGN     " .align 2 "
69 # define _ASM_PTR       " .long "
70 #endif
71
72 static int
73 ftrace_modify_code(unsigned long ip, unsigned char *old_code,
74                    unsigned char *new_code)
75 {
76         unsigned char replaced[MCOUNT_INSN_SIZE];
77
78         /*
79          * Note: Due to modules and __init, code can
80          *  disappear and change, we need to protect against faulting
81          *  as well as code changing. We do this by using the
82          *  probe_kernel_* functions.
83          *
84          * No real locking needed, this code is run through
85          * kstop_machine, or before SMP starts.
86          */
87
88         /* read the text we want to modify */
89         if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
90                 return -EFAULT;
91
92         /* Make sure it is what we expect it to be */
93         if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
94                 return -EINVAL;
95
96         /* replace the text with the new text */
97         if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE))
98                 return -EPERM;
99
100         flush_icache_range(ip, ip + 8);
101
102         return 0;
103 }
104
105 static int test_24bit_addr(unsigned long ip, unsigned long addr)
106 {
107         long diff;
108
109         /*
110          * Can we get to addr from ip in 24 bits?
111          *  (26 really, since we mulitply by 4 for 4 byte alignment)
112          */
113         diff = addr - ip;
114
115         /*
116          * Return true if diff is less than 1 << 25
117          *  and greater than -1 << 26.
118          */
119         return (diff < (1 << 25)) && (diff > (-1 << 26));
120 }
121
122 int ftrace_make_nop(struct module *mod,
123                     struct dyn_ftrace *rec, unsigned long addr)
124 {
125         unsigned char *old, *new;
126
127         /*
128          * If the calling address is more that 24 bits away,
129          * then we had to use a trampoline to make the call.
130          * Otherwise just update the call site.
131          */
132         if (test_24bit_addr(rec->ip, addr)) {
133                 /* within range */
134                 old = ftrace_call_replace(rec->ip, addr);
135                 new = ftrace_nop_replace();
136                 return ftrace_modify_code(rec->ip, old, new);
137         }
138
139         return 0;
140 }
141
142 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
143 {
144         unsigned char *old, *new;
145
146         /*
147          * If the calling address is more that 24 bits away,
148          * then we had to use a trampoline to make the call.
149          * Otherwise just update the call site.
150          */
151         if (test_24bit_addr(rec->ip, addr)) {
152                 /* within range */
153                 old = ftrace_nop_replace();
154                 new = ftrace_call_replace(rec->ip, addr);
155                 return ftrace_modify_code(rec->ip, old, new);
156         }
157
158         return 0;
159 }
160
161 int ftrace_update_ftrace_func(ftrace_func_t func)
162 {
163         unsigned long ip = (unsigned long)(&ftrace_call);
164         unsigned char old[MCOUNT_INSN_SIZE], *new;
165         int ret;
166
167         memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
168         new = ftrace_call_replace(ip, (unsigned long)func);
169         ret = ftrace_modify_code(ip, old, new);
170
171         return ret;
172 }
173
174 int __init ftrace_dyn_arch_init(void *data)
175 {
176         /* caller expects data to be zero */
177         unsigned long *p = data;
178
179         *p = 0;
180
181         return 0;
182 }
183