Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [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 to Ingo Molnar, for suggesting the idea. |
| 7 | * Mathieu Desnoyers, for suggesting postponing the modifications. |
| 8 | * Arjan van de Ven, for keeping me straight, and explaining to me |
| 9 | * the dangers of modifying code on the run. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/hardirq.h> |
Steven Rostedt | 6f93fc0 | 2008-08-20 12:55:07 -0400 | [diff] [blame] | 14 | #include <linux/uaccess.h> |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 15 | #include <linux/ftrace.h> |
| 16 | #include <linux/percpu.h> |
Ingo Molnar | 19b3e96 | 2008-11-11 11:57:02 +0100 | [diff] [blame] | 17 | #include <linux/sched.h> |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 18 | #include <linux/init.h> |
| 19 | #include <linux/list.h> |
| 20 | |
Frederic Weisbecker | 47788c5 | 2009-04-08 20:40:59 +0200 | [diff] [blame^] | 21 | #include <trace/syscall.h> |
| 22 | |
Steven Rostedt | 1623963 | 2009-02-17 17:57:30 -0500 | [diff] [blame] | 23 | #include <asm/cacheflush.h> |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 24 | #include <asm/ftrace.h> |
Steven Rostedt | 732f3ca | 2008-08-14 18:05:05 -0400 | [diff] [blame] | 25 | #include <asm/nops.h> |
Frederic Weisbecker | caf4b32 | 2008-11-11 07:03:45 +0100 | [diff] [blame] | 26 | #include <asm/nmi.h> |
Steven Rostedt | dfa60ab | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 27 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 28 | |
Frederic Weisbecker | caf4b32 | 2008-11-11 07:03:45 +0100 | [diff] [blame] | 29 | #ifdef CONFIG_DYNAMIC_FTRACE |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 30 | |
Steven Rostedt | 1623963 | 2009-02-17 17:57:30 -0500 | [diff] [blame] | 31 | int ftrace_arch_code_modify_prepare(void) |
| 32 | { |
| 33 | set_kernel_text_rw(); |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | int ftrace_arch_code_modify_post_process(void) |
| 38 | { |
| 39 | set_kernel_text_ro(); |
| 40 | return 0; |
| 41 | } |
| 42 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 43 | union ftrace_code_union { |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 44 | char code[MCOUNT_INSN_SIZE]; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 45 | struct { |
| 46 | char e8; |
| 47 | int offset; |
| 48 | } __attribute__((packed)); |
| 49 | }; |
| 50 | |
Steven Rostedt | 15adc04 | 2008-10-23 09:33:08 -0400 | [diff] [blame] | 51 | static int ftrace_calc_offset(long ip, long addr) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 52 | { |
| 53 | return (int)(addr - ip); |
| 54 | } |
| 55 | |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 56 | static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 57 | { |
| 58 | static union ftrace_code_union calc; |
| 59 | |
| 60 | calc.e8 = 0xe8; |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 61 | calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr); |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 62 | |
| 63 | /* |
| 64 | * No locking needed, this must be called via kstop_machine |
| 65 | * which in essence is like running on a uniprocessor machine. |
| 66 | */ |
| 67 | return calc.code; |
| 68 | } |
| 69 | |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 70 | /* |
| 71 | * Modifying code must take extra care. On an SMP machine, if |
| 72 | * the code being modified is also being executed on another CPU |
| 73 | * that CPU will have undefined results and possibly take a GPF. |
| 74 | * We use kstop_machine to stop other CPUS from exectuing code. |
| 75 | * But this does not stop NMIs from happening. We still need |
| 76 | * to protect against that. We separate out the modification of |
| 77 | * the code to take care of this. |
| 78 | * |
| 79 | * Two buffers are added: An IP buffer and a "code" buffer. |
| 80 | * |
Steven Rostedt | a26a2a2 | 2008-10-31 00:03:22 -0400 | [diff] [blame] | 81 | * 1) Put the instruction pointer into the IP buffer |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 82 | * and the new code into the "code" buffer. |
Lai Jiangshan | e9d9df4 | 2009-03-18 16:42:57 +0800 | [diff] [blame] | 83 | * 2) Wait for any running NMIs to finish and set a flag that says |
| 84 | * we are modifying code, it is done in an atomic operation. |
| 85 | * 3) Write the code |
| 86 | * 4) clear the flag. |
| 87 | * 5) Wait for any running NMIs to finish. |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 88 | * |
| 89 | * If an NMI is executed, the first thing it does is to call |
| 90 | * "ftrace_nmi_enter". This will check if the flag is set to write |
| 91 | * and if it is, it will write what is in the IP and "code" buffers. |
| 92 | * |
| 93 | * The trick is, it does not matter if everyone is writing the same |
| 94 | * content to the code location. Also, if a CPU is executing code |
| 95 | * it is OK to write to that code location if the contents being written |
| 96 | * are the same as what exists. |
| 97 | */ |
| 98 | |
Lai Jiangshan | e9d9df4 | 2009-03-18 16:42:57 +0800 | [diff] [blame] | 99 | #define MOD_CODE_WRITE_FLAG (1 << 31) /* set when NMI should do the write */ |
Steven Rostedt | 4e6ea14 | 2009-02-05 22:30:07 -0500 | [diff] [blame] | 100 | static atomic_t nmi_running = ATOMIC_INIT(0); |
Steven Rostedt | a26a2a2 | 2008-10-31 00:03:22 -0400 | [diff] [blame] | 101 | static int mod_code_status; /* holds return value of text write */ |
Steven Rostedt | a26a2a2 | 2008-10-31 00:03:22 -0400 | [diff] [blame] | 102 | static void *mod_code_ip; /* holds the IP to write to */ |
| 103 | static void *mod_code_newcode; /* holds the text to write to the IP */ |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 104 | |
Steven Rostedt | a26a2a2 | 2008-10-31 00:03:22 -0400 | [diff] [blame] | 105 | static unsigned nmi_wait_count; |
| 106 | static atomic_t nmi_update_count = ATOMIC_INIT(0); |
Steven Rostedt | b807c3d | 2008-10-30 16:08:33 -0400 | [diff] [blame] | 107 | |
| 108 | int ftrace_arch_read_dyn_info(char *buf, int size) |
| 109 | { |
| 110 | int r; |
| 111 | |
| 112 | r = snprintf(buf, size, "%u %u", |
| 113 | nmi_wait_count, |
| 114 | atomic_read(&nmi_update_count)); |
| 115 | return r; |
| 116 | } |
| 117 | |
Lai Jiangshan | e9d9df4 | 2009-03-18 16:42:57 +0800 | [diff] [blame] | 118 | static void clear_mod_flag(void) |
| 119 | { |
| 120 | int old = atomic_read(&nmi_running); |
| 121 | |
| 122 | for (;;) { |
| 123 | int new = old & ~MOD_CODE_WRITE_FLAG; |
| 124 | |
| 125 | if (old == new) |
| 126 | break; |
| 127 | |
| 128 | old = atomic_cmpxchg(&nmi_running, old, new); |
| 129 | } |
| 130 | } |
| 131 | |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 132 | static void ftrace_mod_code(void) |
| 133 | { |
| 134 | /* |
| 135 | * Yes, more than one CPU process can be writing to mod_code_status. |
| 136 | * (and the code itself) |
| 137 | * But if one were to fail, then they all should, and if one were |
| 138 | * to succeed, then they all should. |
| 139 | */ |
| 140 | mod_code_status = probe_kernel_write(mod_code_ip, mod_code_newcode, |
| 141 | MCOUNT_INSN_SIZE); |
Steven Rostedt | 90c7ac4 | 2009-02-19 13:32:57 -0500 | [diff] [blame] | 142 | |
| 143 | /* if we fail, then kill any new writers */ |
| 144 | if (mod_code_status) |
Lai Jiangshan | e9d9df4 | 2009-03-18 16:42:57 +0800 | [diff] [blame] | 145 | clear_mod_flag(); |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 146 | } |
| 147 | |
Steven Rostedt | a81bd80 | 2009-02-06 01:45:16 -0500 | [diff] [blame] | 148 | void ftrace_nmi_enter(void) |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 149 | { |
Lai Jiangshan | e9d9df4 | 2009-03-18 16:42:57 +0800 | [diff] [blame] | 150 | if (atomic_inc_return(&nmi_running) & MOD_CODE_WRITE_FLAG) { |
| 151 | smp_rmb(); |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 152 | ftrace_mod_code(); |
Steven Rostedt | b807c3d | 2008-10-30 16:08:33 -0400 | [diff] [blame] | 153 | atomic_inc(&nmi_update_count); |
| 154 | } |
Lai Jiangshan | e9d9df4 | 2009-03-18 16:42:57 +0800 | [diff] [blame] | 155 | /* Must have previous changes seen before executions */ |
| 156 | smp_mb(); |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 157 | } |
| 158 | |
Steven Rostedt | a81bd80 | 2009-02-06 01:45:16 -0500 | [diff] [blame] | 159 | void ftrace_nmi_exit(void) |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 160 | { |
Steven Rostedt | 4e6ea14 | 2009-02-05 22:30:07 -0500 | [diff] [blame] | 161 | /* Finish all executions before clearing nmi_running */ |
Lai Jiangshan | e9d9df4 | 2009-03-18 16:42:57 +0800 | [diff] [blame] | 162 | smp_mb(); |
Steven Rostedt | 4e6ea14 | 2009-02-05 22:30:07 -0500 | [diff] [blame] | 163 | atomic_dec(&nmi_running); |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 164 | } |
| 165 | |
Lai Jiangshan | e9d9df4 | 2009-03-18 16:42:57 +0800 | [diff] [blame] | 166 | static void wait_for_nmi_and_set_mod_flag(void) |
| 167 | { |
| 168 | if (!atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG)) |
| 169 | return; |
| 170 | |
| 171 | do { |
| 172 | cpu_relax(); |
| 173 | } while (atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG)); |
| 174 | |
| 175 | nmi_wait_count++; |
| 176 | } |
| 177 | |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 178 | static void wait_for_nmi(void) |
| 179 | { |
Steven Rostedt | 4e6ea14 | 2009-02-05 22:30:07 -0500 | [diff] [blame] | 180 | if (!atomic_read(&nmi_running)) |
Cyrill Gorcunov | 8902528 | 2009-01-26 18:28:02 +0300 | [diff] [blame] | 181 | return; |
Steven Rostedt | b807c3d | 2008-10-30 16:08:33 -0400 | [diff] [blame] | 182 | |
Cyrill Gorcunov | 8902528 | 2009-01-26 18:28:02 +0300 | [diff] [blame] | 183 | do { |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 184 | cpu_relax(); |
Steven Rostedt | 4e6ea14 | 2009-02-05 22:30:07 -0500 | [diff] [blame] | 185 | } while (atomic_read(&nmi_running)); |
Steven Rostedt | b807c3d | 2008-10-30 16:08:33 -0400 | [diff] [blame] | 186 | |
Cyrill Gorcunov | 8902528 | 2009-01-26 18:28:02 +0300 | [diff] [blame] | 187 | nmi_wait_count++; |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static int |
| 191 | do_ftrace_mod_code(unsigned long ip, void *new_code) |
| 192 | { |
| 193 | mod_code_ip = (void *)ip; |
| 194 | mod_code_newcode = new_code; |
| 195 | |
| 196 | /* The buffers need to be visible before we let NMIs write them */ |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 197 | smp_mb(); |
| 198 | |
Lai Jiangshan | e9d9df4 | 2009-03-18 16:42:57 +0800 | [diff] [blame] | 199 | wait_for_nmi_and_set_mod_flag(); |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 200 | |
| 201 | /* Make sure all running NMIs have finished before we write the code */ |
| 202 | smp_mb(); |
| 203 | |
| 204 | ftrace_mod_code(); |
| 205 | |
| 206 | /* Make sure the write happens before clearing the bit */ |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 207 | smp_mb(); |
| 208 | |
Lai Jiangshan | e9d9df4 | 2009-03-18 16:42:57 +0800 | [diff] [blame] | 209 | clear_mod_flag(); |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 210 | wait_for_nmi(); |
| 211 | |
| 212 | return mod_code_status; |
| 213 | } |
| 214 | |
| 215 | |
Frederic Weisbecker | caf4b32 | 2008-11-11 07:03:45 +0100 | [diff] [blame] | 216 | |
| 217 | |
| 218 | static unsigned char ftrace_nop[MCOUNT_INSN_SIZE]; |
| 219 | |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 220 | static unsigned char *ftrace_nop_replace(void) |
Frederic Weisbecker | caf4b32 | 2008-11-11 07:03:45 +0100 | [diff] [blame] | 221 | { |
| 222 | return ftrace_nop; |
| 223 | } |
| 224 | |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 225 | static int |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 226 | ftrace_modify_code(unsigned long ip, unsigned char *old_code, |
| 227 | unsigned char *new_code) |
| 228 | { |
Steven Rostedt | 6f93fc0 | 2008-08-20 12:55:07 -0400 | [diff] [blame] | 229 | unsigned char replaced[MCOUNT_INSN_SIZE]; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 230 | |
| 231 | /* |
| 232 | * Note: Due to modules and __init, code can |
| 233 | * disappear and change, we need to protect against faulting |
Steven Rostedt | 76aefee | 2008-10-23 09:33:00 -0400 | [diff] [blame] | 234 | * as well as code changing. We do this by using the |
Steven Rostedt | ab9a091 | 2008-10-23 09:33:01 -0400 | [diff] [blame] | 235 | * probe_kernel_* functions. |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 236 | * |
| 237 | * No real locking needed, this code is run through |
Steven Rostedt | 6f93fc0 | 2008-08-20 12:55:07 -0400 | [diff] [blame] | 238 | * kstop_machine, or before SMP starts. |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 239 | */ |
Steven Rostedt | 76aefee | 2008-10-23 09:33:00 -0400 | [diff] [blame] | 240 | |
| 241 | /* read the text we want to modify */ |
Steven Rostedt | ab9a091 | 2008-10-23 09:33:01 -0400 | [diff] [blame] | 242 | if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE)) |
Steven Rostedt | 593eb8a | 2008-10-23 09:32:59 -0400 | [diff] [blame] | 243 | return -EFAULT; |
Steven Rostedt | 6f93fc0 | 2008-08-20 12:55:07 -0400 | [diff] [blame] | 244 | |
Steven Rostedt | 76aefee | 2008-10-23 09:33:00 -0400 | [diff] [blame] | 245 | /* Make sure it is what we expect it to be */ |
Steven Rostedt | 6f93fc0 | 2008-08-20 12:55:07 -0400 | [diff] [blame] | 246 | if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0) |
Steven Rostedt | 593eb8a | 2008-10-23 09:32:59 -0400 | [diff] [blame] | 247 | return -EINVAL; |
Steven Rostedt | 6f93fc0 | 2008-08-20 12:55:07 -0400 | [diff] [blame] | 248 | |
Steven Rostedt | 76aefee | 2008-10-23 09:33:00 -0400 | [diff] [blame] | 249 | /* replace the text with the new text */ |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 250 | if (do_ftrace_mod_code(ip, new_code)) |
Steven Rostedt | 593eb8a | 2008-10-23 09:32:59 -0400 | [diff] [blame] | 251 | return -EPERM; |
Steven Rostedt | 6f93fc0 | 2008-08-20 12:55:07 -0400 | [diff] [blame] | 252 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 253 | sync_core(); |
| 254 | |
Steven Rostedt | 6f93fc0 | 2008-08-20 12:55:07 -0400 | [diff] [blame] | 255 | return 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Steven Rostedt | 31e8890 | 2008-11-14 16:21:19 -0800 | [diff] [blame] | 258 | int ftrace_make_nop(struct module *mod, |
| 259 | struct dyn_ftrace *rec, unsigned long addr) |
| 260 | { |
| 261 | unsigned char *new, *old; |
| 262 | unsigned long ip = rec->ip; |
| 263 | |
| 264 | old = ftrace_call_replace(ip, addr); |
| 265 | new = ftrace_nop_replace(); |
| 266 | |
| 267 | return ftrace_modify_code(rec->ip, old, new); |
| 268 | } |
| 269 | |
| 270 | int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) |
| 271 | { |
| 272 | unsigned char *new, *old; |
| 273 | unsigned long ip = rec->ip; |
| 274 | |
| 275 | old = ftrace_nop_replace(); |
| 276 | new = ftrace_call_replace(ip, addr); |
| 277 | |
| 278 | return ftrace_modify_code(rec->ip, old, new); |
| 279 | } |
| 280 | |
Steven Rostedt | 15adc04 | 2008-10-23 09:33:08 -0400 | [diff] [blame] | 281 | int ftrace_update_ftrace_func(ftrace_func_t func) |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 282 | { |
| 283 | unsigned long ip = (unsigned long)(&ftrace_call); |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 284 | unsigned char old[MCOUNT_INSN_SIZE], *new; |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 285 | int ret; |
| 286 | |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 287 | memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE); |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 288 | new = ftrace_call_replace(ip, (unsigned long)func); |
| 289 | ret = ftrace_modify_code(ip, old, new); |
| 290 | |
| 291 | return ret; |
| 292 | } |
| 293 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 294 | int __init ftrace_dyn_arch_init(void *data) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 295 | { |
Steven Rostedt | 732f3ca | 2008-08-14 18:05:05 -0400 | [diff] [blame] | 296 | extern const unsigned char ftrace_test_p6nop[]; |
| 297 | extern const unsigned char ftrace_test_nop5[]; |
| 298 | extern const unsigned char ftrace_test_jmp[]; |
| 299 | int faulted = 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 300 | |
Steven Rostedt | 732f3ca | 2008-08-14 18:05:05 -0400 | [diff] [blame] | 301 | /* |
| 302 | * There is no good nop for all x86 archs. |
| 303 | * We will default to using the P6_NOP5, but first we |
| 304 | * will test to make sure that the nop will actually |
| 305 | * work on this CPU. If it faults, we will then |
| 306 | * go to a lesser efficient 5 byte nop. If that fails |
| 307 | * we then just use a jmp as our nop. This isn't the most |
| 308 | * efficient nop, but we can not use a multi part nop |
| 309 | * since we would then risk being preempted in the middle |
| 310 | * of that nop, and if we enabled tracing then, it might |
| 311 | * cause a system crash. |
| 312 | * |
| 313 | * TODO: check the cpuid to determine the best nop. |
| 314 | */ |
| 315 | asm volatile ( |
Steven Rostedt | 732f3ca | 2008-08-14 18:05:05 -0400 | [diff] [blame] | 316 | "ftrace_test_jmp:" |
| 317 | "jmp ftrace_test_p6nop\n" |
Anders Kaseorg | 8b27386 | 2008-10-09 22:19:08 -0400 | [diff] [blame] | 318 | "nop\n" |
| 319 | "nop\n" |
| 320 | "nop\n" /* 2 byte jmp + 3 bytes */ |
Steven Rostedt | 732f3ca | 2008-08-14 18:05:05 -0400 | [diff] [blame] | 321 | "ftrace_test_p6nop:" |
| 322 | P6_NOP5 |
| 323 | "jmp 1f\n" |
| 324 | "ftrace_test_nop5:" |
| 325 | ".byte 0x66,0x66,0x66,0x66,0x90\n" |
Steven Rostedt | 732f3ca | 2008-08-14 18:05:05 -0400 | [diff] [blame] | 326 | "1:" |
| 327 | ".section .fixup, \"ax\"\n" |
| 328 | "2: movl $1, %0\n" |
| 329 | " jmp ftrace_test_nop5\n" |
| 330 | "3: movl $2, %0\n" |
| 331 | " jmp 1b\n" |
| 332 | ".previous\n" |
| 333 | _ASM_EXTABLE(ftrace_test_p6nop, 2b) |
| 334 | _ASM_EXTABLE(ftrace_test_nop5, 3b) |
| 335 | : "=r"(faulted) : "0" (faulted)); |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 336 | |
Steven Rostedt | 732f3ca | 2008-08-14 18:05:05 -0400 | [diff] [blame] | 337 | switch (faulted) { |
| 338 | case 0: |
| 339 | pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n"); |
Steven Rostedt | 8115f3f | 2008-10-24 09:12:17 -0400 | [diff] [blame] | 340 | memcpy(ftrace_nop, ftrace_test_p6nop, MCOUNT_INSN_SIZE); |
Steven Rostedt | 732f3ca | 2008-08-14 18:05:05 -0400 | [diff] [blame] | 341 | break; |
| 342 | case 1: |
| 343 | pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n"); |
Steven Rostedt | 8115f3f | 2008-10-24 09:12:17 -0400 | [diff] [blame] | 344 | memcpy(ftrace_nop, ftrace_test_nop5, MCOUNT_INSN_SIZE); |
Steven Rostedt | 732f3ca | 2008-08-14 18:05:05 -0400 | [diff] [blame] | 345 | break; |
| 346 | case 2: |
Anders Kaseorg | 8b27386 | 2008-10-09 22:19:08 -0400 | [diff] [blame] | 347 | pr_info("ftrace: converting mcount calls to jmp . + 5\n"); |
Steven Rostedt | 8115f3f | 2008-10-24 09:12:17 -0400 | [diff] [blame] | 348 | memcpy(ftrace_nop, ftrace_test_jmp, MCOUNT_INSN_SIZE); |
Steven Rostedt | 732f3ca | 2008-08-14 18:05:05 -0400 | [diff] [blame] | 349 | break; |
| 350 | } |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 351 | |
Steven Rostedt | 732f3ca | 2008-08-14 18:05:05 -0400 | [diff] [blame] | 352 | /* The return code is retured via data */ |
| 353 | *(unsigned long *)data = 0; |
Steven Rostedt | dfa60ab | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 354 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 355 | return 0; |
| 356 | } |
Frederic Weisbecker | caf4b32 | 2008-11-11 07:03:45 +0100 | [diff] [blame] | 357 | #endif |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 358 | |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 359 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 360 | |
Steven Rostedt | 5a45cfe | 2008-11-26 00:16:24 -0500 | [diff] [blame] | 361 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 362 | extern void ftrace_graph_call(void); |
| 363 | |
| 364 | static int ftrace_mod_jmp(unsigned long ip, |
| 365 | int old_offset, int new_offset) |
| 366 | { |
| 367 | unsigned char code[MCOUNT_INSN_SIZE]; |
| 368 | |
| 369 | if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE)) |
| 370 | return -EFAULT; |
| 371 | |
| 372 | if (code[0] != 0xe9 || old_offset != *(int *)(&code[1])) |
| 373 | return -EINVAL; |
| 374 | |
| 375 | *(int *)(&code[1]) = new_offset; |
| 376 | |
| 377 | if (do_ftrace_mod_code(ip, &code)) |
| 378 | return -EPERM; |
| 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | int ftrace_enable_ftrace_graph_caller(void) |
| 384 | { |
| 385 | unsigned long ip = (unsigned long)(&ftrace_graph_call); |
| 386 | int old_offset, new_offset; |
| 387 | |
| 388 | old_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE); |
| 389 | new_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE); |
| 390 | |
| 391 | return ftrace_mod_jmp(ip, old_offset, new_offset); |
| 392 | } |
| 393 | |
| 394 | int ftrace_disable_ftrace_graph_caller(void) |
| 395 | { |
| 396 | unsigned long ip = (unsigned long)(&ftrace_graph_call); |
| 397 | int old_offset, new_offset; |
| 398 | |
| 399 | old_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE); |
| 400 | new_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE); |
| 401 | |
| 402 | return ftrace_mod_jmp(ip, old_offset, new_offset); |
| 403 | } |
| 404 | |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 405 | #endif /* !CONFIG_DYNAMIC_FTRACE */ |
| 406 | |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 407 | /* |
| 408 | * Hook the return address and push it in the stack of return addrs |
| 409 | * in current thread info. |
| 410 | */ |
| 411 | void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr) |
| 412 | { |
| 413 | unsigned long old; |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 414 | int faulted; |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 415 | struct ftrace_graph_ent trace; |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 416 | unsigned long return_hooker = (unsigned long) |
| 417 | &return_to_handler; |
| 418 | |
| 419 | /* Nmi's are currently unsupported */ |
Steven Rostedt | 9a5fd90 | 2009-02-06 01:14:26 -0500 | [diff] [blame] | 420 | if (unlikely(in_nmi())) |
Frederic Weisbecker | 380c4b1 | 2008-12-06 03:43:41 +0100 | [diff] [blame] | 421 | return; |
| 422 | |
| 423 | if (unlikely(atomic_read(¤t->tracing_graph_pause))) |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 424 | return; |
| 425 | |
| 426 | /* |
| 427 | * Protect against fault, even if it shouldn't |
| 428 | * happen. This tool is too much intrusive to |
| 429 | * ignore such a protection. |
| 430 | */ |
| 431 | asm volatile( |
Steven Rostedt | 9666578 | 2009-02-10 11:53:23 -0500 | [diff] [blame] | 432 | "1: " _ASM_MOV " (%[parent]), %[old]\n" |
| 433 | "2: " _ASM_MOV " %[return_hooker], (%[parent])\n" |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 434 | " movl $0, %[faulted]\n" |
Steven Rostedt | e3944bf | 2009-02-10 13:07:13 -0500 | [diff] [blame] | 435 | "3:\n" |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 436 | |
| 437 | ".section .fixup, \"ax\"\n" |
Steven Rostedt | e3944bf | 2009-02-10 13:07:13 -0500 | [diff] [blame] | 438 | "4: movl $1, %[faulted]\n" |
| 439 | " jmp 3b\n" |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 440 | ".previous\n" |
| 441 | |
Steven Rostedt | e3944bf | 2009-02-10 13:07:13 -0500 | [diff] [blame] | 442 | _ASM_EXTABLE(1b, 4b) |
| 443 | _ASM_EXTABLE(2b, 4b) |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 444 | |
Steven Rostedt | 9666578 | 2009-02-10 11:53:23 -0500 | [diff] [blame] | 445 | : [old] "=r" (old), [faulted] "=r" (faulted) |
| 446 | : [parent] "r" (parent), [return_hooker] "r" (return_hooker) |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 447 | : "memory" |
| 448 | ); |
| 449 | |
Steven Rostedt | 14a866c | 2008-12-02 23:50:02 -0500 | [diff] [blame] | 450 | if (unlikely(faulted)) { |
| 451 | ftrace_graph_stop(); |
| 452 | WARN_ON(1); |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 453 | return; |
| 454 | } |
| 455 | |
Steven Rostedt | 5d1a03d | 2009-03-23 23:38:49 -0400 | [diff] [blame] | 456 | if (ftrace_push_return_trace(old, self_addr, &trace.depth) == -EBUSY) { |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 457 | *parent = old; |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 458 | return; |
| 459 | } |
| 460 | |
| 461 | trace.func = self_addr; |
Frederic Weisbecker | 287b6e6 | 2008-11-26 00:57:25 +0100 | [diff] [blame] | 462 | |
Steven Rostedt | e49dc19 | 2008-12-02 23:50:05 -0500 | [diff] [blame] | 463 | /* Only trace if the calling function expects to */ |
| 464 | if (!ftrace_graph_entry(&trace)) { |
| 465 | current->curr_ret_stack--; |
| 466 | *parent = old; |
| 467 | } |
Frederic Weisbecker | e7d3737 | 2008-11-16 06:02:06 +0100 | [diff] [blame] | 468 | } |
Frederic Weisbecker | fb52607 | 2008-11-25 21:07:04 +0100 | [diff] [blame] | 469 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
Frederic Weisbecker | f58ba10 | 2009-03-13 15:42:12 +0100 | [diff] [blame] | 470 | |
| 471 | #ifdef CONFIG_FTRACE_SYSCALLS |
| 472 | |
| 473 | extern unsigned long __start_syscalls_metadata[]; |
| 474 | extern unsigned long __stop_syscalls_metadata[]; |
| 475 | extern unsigned long *sys_call_table; |
| 476 | |
| 477 | static struct syscall_metadata **syscalls_metadata; |
| 478 | |
| 479 | static struct syscall_metadata *find_syscall_meta(unsigned long *syscall) |
| 480 | { |
| 481 | struct syscall_metadata *start; |
| 482 | struct syscall_metadata *stop; |
| 483 | char str[KSYM_SYMBOL_LEN]; |
| 484 | |
| 485 | |
| 486 | start = (struct syscall_metadata *)__start_syscalls_metadata; |
| 487 | stop = (struct syscall_metadata *)__stop_syscalls_metadata; |
| 488 | kallsyms_lookup((unsigned long) syscall, NULL, NULL, NULL, str); |
| 489 | |
| 490 | for ( ; start < stop; start++) { |
| 491 | if (start->name && !strcmp(start->name, str)) |
| 492 | return start; |
| 493 | } |
| 494 | return NULL; |
| 495 | } |
| 496 | |
| 497 | struct syscall_metadata *syscall_nr_to_meta(int nr) |
| 498 | { |
| 499 | if (!syscalls_metadata || nr >= FTRACE_SYSCALL_MAX || nr < 0) |
| 500 | return NULL; |
| 501 | |
| 502 | return syscalls_metadata[nr]; |
| 503 | } |
| 504 | |
| 505 | void arch_init_ftrace_syscalls(void) |
| 506 | { |
| 507 | int i; |
| 508 | struct syscall_metadata *meta; |
| 509 | unsigned long **psys_syscall_table = &sys_call_table; |
| 510 | static atomic_t refs; |
| 511 | |
| 512 | if (atomic_inc_return(&refs) != 1) |
| 513 | goto end; |
| 514 | |
| 515 | syscalls_metadata = kzalloc(sizeof(*syscalls_metadata) * |
| 516 | FTRACE_SYSCALL_MAX, GFP_KERNEL); |
| 517 | if (!syscalls_metadata) { |
| 518 | WARN_ON(1); |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | for (i = 0; i < FTRACE_SYSCALL_MAX; i++) { |
| 523 | meta = find_syscall_meta(psys_syscall_table[i]); |
| 524 | syscalls_metadata[i] = meta; |
| 525 | } |
| 526 | return; |
| 527 | |
| 528 | /* Paranoid: avoid overflow */ |
| 529 | end: |
| 530 | atomic_dec(&refs); |
| 531 | } |
| 532 | #endif |