blob: 1adfbb268d8ee1d1688b7176acdac03a3e490e98 [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>
Steven Rostedte4486fe2008-11-14 16:21:20 -080012#include <linux/uaccess.h>
Steven Rostedt4e491d12008-05-14 23:49:44 -040013#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>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053019#include <asm/ftrace.h>
Steven Rostedt4e491d12008-05-14 23:49:44 -040020
Steven Rostedt4e491d12008-05-14 23:49:44 -040021
22static 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
Abhishek Sagar395a59d2008-06-21 23:47:27 +053031
Steven Rostedt15adc042008-10-23 09:33:08 -040032static unsigned int ftrace_calc_offset(long ip, long addr)
Steven Rostedt4e491d12008-05-14 23:49:44 -040033{
Abhishek Sagar395a59d2008-06-21 23:47:27 +053034 return (int)(addr - ip);
Steven Rostedt4e491d12008-05-14 23:49:44 -040035}
36
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080037static unsigned char *ftrace_nop_replace(void)
Steven Rostedt4e491d12008-05-14 23:49:44 -040038{
39 return (char *)&ftrace_nop;
40}
41
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080042static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
Steven Rostedt4e491d12008-05-14 23:49:44 -040043{
44 static unsigned int op;
45
Steven Rostedtccbfac22008-05-22 14:31:07 -040046 /*
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 */
Steven Rostedt4e491d12008-05-14 23:49:44 -040052 addr = GET_ADDR(addr);
53
54 /* Set to "bl addr" */
Steven Rostedtccbfac22008-05-22 14:31:07 -040055 op = 0x48000001 | (ftrace_calc_offset(ip, addr) & 0x03fffffc);
Steven Rostedt4e491d12008-05-14 23:49:44 -040056
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
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080072static int
Steven Rostedt4e491d12008-05-14 23:49:44 -040073ftrace_modify_code(unsigned long ip, unsigned char *old_code,
74 unsigned char *new_code)
75{
Steven Rostedte4486fe2008-11-14 16:21:20 -080076 unsigned char replaced[MCOUNT_INSN_SIZE];
Steven Rostedt4e491d12008-05-14 23:49:44 -040077
Steven Rostedt4e491d12008-05-14 23:49:44 -040078 /*
79 * Note: Due to modules and __init, code can
80 * disappear and change, we need to protect against faulting
Steven Rostedte4486fe2008-11-14 16:21:20 -080081 * as well as code changing. We do this by using the
82 * probe_kernel_* functions.
Steven Rostedt4e491d12008-05-14 23:49:44 -040083 *
84 * No real locking needed, this code is run through
Steven Rostedte4486fe2008-11-14 16:21:20 -080085 * kstop_machine, or before SMP starts.
Steven Rostedt4e491d12008-05-14 23:49:44 -040086 */
Steven Rostedt4e491d12008-05-14 23:49:44 -040087
Steven Rostedte4486fe2008-11-14 16:21:20 -080088 /* read the text we want to modify */
89 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
90 return -EFAULT;
Steven Rostedt4e491d12008-05-14 23:49:44 -040091
Steven Rostedte4486fe2008-11-14 16:21:20 -080092 /* Make sure it is what we expect it to be */
93 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
94 return -EINVAL;
Steven Rostedt4e491d12008-05-14 23:49:44 -040095
Steven Rostedte4486fe2008-11-14 16:21:20 -080096 /* 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;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400103}
104
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800105static 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
122int 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
142int 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
Steven Rostedt15adc042008-10-23 09:33:08 -0400161int ftrace_update_ftrace_func(ftrace_func_t func)
Steven Rostedt4e491d12008-05-14 23:49:44 -0400162{
163 unsigned long ip = (unsigned long)(&ftrace_call);
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530164 unsigned char old[MCOUNT_INSN_SIZE], *new;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400165 int ret;
166
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530167 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
Steven Rostedt4e491d12008-05-14 23:49:44 -0400168 new = ftrace_call_replace(ip, (unsigned long)func);
169 ret = ftrace_modify_code(ip, old, new);
170
171 return ret;
172}
173
Steven Rostedt4e491d12008-05-14 23:49:44 -0400174int __init ftrace_dyn_arch_init(void *data)
175{
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800176 /* caller expects data to be zero */
177 unsigned long *p = data;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400178
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800179 *p = 0;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400180
181 return 0;
182}
183