Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 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> |
Steven Rostedt | e4486fe | 2008-11-14 16:21:20 -0800 | [diff] [blame^] | 12 | #include <linux/uaccess.h> |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 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> |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 19 | #include <asm/ftrace.h> |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 20 | |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 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 | |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 31 | |
Steven Rostedt | 15adc04 | 2008-10-23 09:33:08 -0400 | [diff] [blame] | 32 | static unsigned int ftrace_calc_offset(long ip, long addr) |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 33 | { |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 34 | return (int)(addr - ip); |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 35 | } |
| 36 | |
Steven Rostedt | 8fd6e5a | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 37 | static unsigned char *ftrace_nop_replace(void) |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 38 | { |
| 39 | return (char *)&ftrace_nop; |
| 40 | } |
| 41 | |
Steven Rostedt | 8fd6e5a | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 42 | static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr) |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 43 | { |
| 44 | static unsigned int op; |
| 45 | |
Steven Rostedt | ccbfac2 | 2008-05-22 14:31:07 -0400 | [diff] [blame] | 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 | */ |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 52 | addr = GET_ADDR(addr); |
| 53 | |
| 54 | /* Set to "bl addr" */ |
Steven Rostedt | ccbfac2 | 2008-05-22 14:31:07 -0400 | [diff] [blame] | 55 | op = 0x48000001 | (ftrace_calc_offset(ip, addr) & 0x03fffffc); |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 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 | |
Steven Rostedt | 8fd6e5a | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 72 | static int |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 73 | ftrace_modify_code(unsigned long ip, unsigned char *old_code, |
| 74 | unsigned char *new_code) |
| 75 | { |
Steven Rostedt | e4486fe | 2008-11-14 16:21:20 -0800 | [diff] [blame^] | 76 | unsigned char replaced[MCOUNT_INSN_SIZE]; |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 77 | |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 78 | /* |
| 79 | * Note: Due to modules and __init, code can |
| 80 | * disappear and change, we need to protect against faulting |
Steven Rostedt | e4486fe | 2008-11-14 16:21:20 -0800 | [diff] [blame^] | 81 | * as well as code changing. We do this by using the |
| 82 | * probe_kernel_* functions. |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 83 | * |
| 84 | * No real locking needed, this code is run through |
Steven Rostedt | e4486fe | 2008-11-14 16:21:20 -0800 | [diff] [blame^] | 85 | * kstop_machine, or before SMP starts. |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 86 | */ |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 87 | |
Steven Rostedt | e4486fe | 2008-11-14 16:21:20 -0800 | [diff] [blame^] | 88 | /* read the text we want to modify */ |
| 89 | if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE)) |
| 90 | return -EFAULT; |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 91 | |
Steven Rostedt | e4486fe | 2008-11-14 16:21:20 -0800 | [diff] [blame^] | 92 | /* Make sure it is what we expect it to be */ |
| 93 | if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0) |
| 94 | return -EINVAL; |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 95 | |
Steven Rostedt | e4486fe | 2008-11-14 16:21:20 -0800 | [diff] [blame^] | 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; |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 103 | } |
| 104 | |
Steven Rostedt | 8fd6e5a | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 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 | |
Steven Rostedt | 15adc04 | 2008-10-23 09:33:08 -0400 | [diff] [blame] | 161 | int ftrace_update_ftrace_func(ftrace_func_t func) |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 162 | { |
| 163 | unsigned long ip = (unsigned long)(&ftrace_call); |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 164 | unsigned char old[MCOUNT_INSN_SIZE], *new; |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 165 | int ret; |
| 166 | |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 167 | memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE); |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 168 | new = ftrace_call_replace(ip, (unsigned long)func); |
| 169 | ret = ftrace_modify_code(ip, old, new); |
| 170 | |
| 171 | return ret; |
| 172 | } |
| 173 | |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 174 | int __init ftrace_dyn_arch_init(void *data) |
| 175 | { |
Steven Rostedt | 8fd6e5a | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 176 | /* caller expects data to be zero */ |
| 177 | unsigned long *p = data; |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 178 | |
Steven Rostedt | 8fd6e5a | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 179 | *p = 0; |
Steven Rostedt | 4e491d1 | 2008-05-14 23:49:44 -0400 | [diff] [blame] | 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |