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 | |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 21 | #include <asm/ftrace.h> |
Frederic Weisbecker | caf4b32 | 2008-11-11 07:03:45 +0100 | [diff] [blame] | 22 | #include <linux/ftrace.h> |
Steven Rostedt | 732f3ca | 2008-08-14 18:05:05 -0400 | [diff] [blame] | 23 | #include <asm/nops.h> |
Frederic Weisbecker | caf4b32 | 2008-11-11 07:03:45 +0100 | [diff] [blame] | 24 | #include <asm/nmi.h> |
Steven Rostedt | dfa60ab | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 25 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 26 | |
Frederic Weisbecker | caf4b32 | 2008-11-11 07:03:45 +0100 | [diff] [blame] | 27 | |
| 28 | #ifdef CONFIG_FUNCTION_RET_TRACER |
| 29 | |
| 30 | /* |
| 31 | * These functions are picked from those used on |
| 32 | * this page for dynamic ftrace. They have been |
| 33 | * simplified to ignore all traces in NMI context. |
| 34 | */ |
| 35 | static atomic_t in_nmi; |
| 36 | |
| 37 | void ftrace_nmi_enter(void) |
| 38 | { |
| 39 | atomic_inc(&in_nmi); |
| 40 | } |
| 41 | |
| 42 | void ftrace_nmi_exit(void) |
| 43 | { |
| 44 | atomic_dec(&in_nmi); |
| 45 | } |
| 46 | |
Frederic Weisbecker | caf4b32 | 2008-11-11 07:03:45 +0100 | [diff] [blame] | 47 | /* Add a function return address to the trace stack on thread info.*/ |
| 48 | static int push_return_trace(unsigned long ret, unsigned long long time, |
| 49 | unsigned long func) |
| 50 | { |
| 51 | int index; |
Frederic Weisbecker | 62d59d1 | 2008-11-12 22:47:54 +0100 | [diff] [blame^] | 52 | struct thread_info *ti = current_thread_info(); |
Frederic Weisbecker | caf4b32 | 2008-11-11 07:03:45 +0100 | [diff] [blame] | 53 | |
Frederic Weisbecker | caf4b32 | 2008-11-11 07:03:45 +0100 | [diff] [blame] | 54 | /* The return trace stack is full */ |
Frederic Weisbecker | 62d59d1 | 2008-11-12 22:47:54 +0100 | [diff] [blame^] | 55 | if (ti->curr_ret_stack == FTRACE_RET_STACK_SIZE - 1) |
| 56 | return -EBUSY; |
Frederic Weisbecker | caf4b32 | 2008-11-11 07:03:45 +0100 | [diff] [blame] | 57 | |
| 58 | index = ++ti->curr_ret_stack; |
| 59 | ti->ret_stack[index].ret = ret; |
| 60 | ti->ret_stack[index].func = func; |
| 61 | ti->ret_stack[index].calltime = time; |
| 62 | |
Frederic Weisbecker | 62d59d1 | 2008-11-12 22:47:54 +0100 | [diff] [blame^] | 63 | return 0; |
Frederic Weisbecker | caf4b32 | 2008-11-11 07:03:45 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /* Retrieve a function return address to the trace stack on thread info.*/ |
| 67 | static void pop_return_trace(unsigned long *ret, unsigned long long *time, |
| 68 | unsigned long *func) |
| 69 | { |
Frederic Weisbecker | caf4b32 | 2008-11-11 07:03:45 +0100 | [diff] [blame] | 70 | int index; |
Frederic Weisbecker | caf4b32 | 2008-11-11 07:03:45 +0100 | [diff] [blame] | 71 | |
Frederic Weisbecker | 62d59d1 | 2008-11-12 22:47:54 +0100 | [diff] [blame^] | 72 | struct thread_info *ti = current_thread_info(); |
Frederic Weisbecker | caf4b32 | 2008-11-11 07:03:45 +0100 | [diff] [blame] | 73 | index = ti->curr_ret_stack; |
| 74 | *ret = ti->ret_stack[index].ret; |
| 75 | *func = ti->ret_stack[index].func; |
| 76 | *time = ti->ret_stack[index].calltime; |
| 77 | ti->curr_ret_stack--; |
Frederic Weisbecker | caf4b32 | 2008-11-11 07:03:45 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Send the trace to the ring-buffer. |
| 82 | * @return the original return address. |
| 83 | */ |
| 84 | unsigned long ftrace_return_to_handler(void) |
| 85 | { |
| 86 | struct ftrace_retfunc trace; |
| 87 | pop_return_trace(&trace.ret, &trace.calltime, &trace.func); |
| 88 | trace.rettime = cpu_clock(raw_smp_processor_id()); |
| 89 | ftrace_function_return(&trace); |
| 90 | |
| 91 | return trace.ret; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Hook the return address and push it in the stack of return addrs |
| 96 | * in current thread info. |
| 97 | */ |
| 98 | asmlinkage |
| 99 | void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr) |
| 100 | { |
| 101 | unsigned long old; |
| 102 | unsigned long long calltime; |
| 103 | int faulted; |
| 104 | unsigned long return_hooker = (unsigned long) |
| 105 | &return_to_handler; |
| 106 | |
| 107 | /* Nmi's are currently unsupported */ |
| 108 | if (atomic_read(&in_nmi)) |
| 109 | return; |
| 110 | |
| 111 | /* |
| 112 | * Protect against fault, even if it shouldn't |
| 113 | * happen. This tool is too much intrusive to |
| 114 | * ignore such a protection. |
| 115 | */ |
| 116 | asm volatile( |
| 117 | "1: movl (%[parent_old]), %[old]\n" |
| 118 | "2: movl %[return_hooker], (%[parent_replaced])\n" |
| 119 | " movl $0, %[faulted]\n" |
| 120 | |
| 121 | ".section .fixup, \"ax\"\n" |
| 122 | "3: movl $1, %[faulted]\n" |
| 123 | ".previous\n" |
| 124 | |
| 125 | ".section __ex_table, \"a\"\n" |
| 126 | " .long 1b, 3b\n" |
| 127 | " .long 2b, 3b\n" |
| 128 | ".previous\n" |
| 129 | |
Ingo Molnar | 867f7fb | 2008-11-11 11:18:14 +0100 | [diff] [blame] | 130 | : [parent_replaced] "=r" (parent), [old] "=r" (old), |
Frederic Weisbecker | caf4b32 | 2008-11-11 07:03:45 +0100 | [diff] [blame] | 131 | [faulted] "=r" (faulted) |
| 132 | : [parent_old] "0" (parent), [return_hooker] "r" (return_hooker) |
| 133 | : "memory" |
| 134 | ); |
| 135 | |
| 136 | if (WARN_ON(faulted)) { |
| 137 | unregister_ftrace_return(); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | if (WARN_ON(!__kernel_text_address(old))) { |
| 142 | unregister_ftrace_return(); |
| 143 | *parent = old; |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | calltime = cpu_clock(raw_smp_processor_id()); |
| 148 | |
| 149 | if (push_return_trace(old, calltime, self_addr) == -EBUSY) |
| 150 | *parent = old; |
| 151 | } |
| 152 | |
Frederic Weisbecker | caf4b32 | 2008-11-11 07:03:45 +0100 | [diff] [blame] | 153 | #endif |
| 154 | |
| 155 | #ifdef CONFIG_DYNAMIC_FTRACE |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 156 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 157 | union ftrace_code_union { |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 158 | char code[MCOUNT_INSN_SIZE]; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 159 | struct { |
| 160 | char e8; |
| 161 | int offset; |
| 162 | } __attribute__((packed)); |
| 163 | }; |
| 164 | |
Steven Rostedt | 15adc04 | 2008-10-23 09:33:08 -0400 | [diff] [blame] | 165 | static int ftrace_calc_offset(long ip, long addr) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 166 | { |
| 167 | return (int)(addr - ip); |
| 168 | } |
| 169 | |
Steven Rostedt | 15adc04 | 2008-10-23 09:33:08 -0400 | [diff] [blame] | 170 | unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 171 | { |
| 172 | static union ftrace_code_union calc; |
| 173 | |
| 174 | calc.e8 = 0xe8; |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 175 | calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr); |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 176 | |
| 177 | /* |
| 178 | * No locking needed, this must be called via kstop_machine |
| 179 | * which in essence is like running on a uniprocessor machine. |
| 180 | */ |
| 181 | return calc.code; |
| 182 | } |
| 183 | |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 184 | /* |
| 185 | * Modifying code must take extra care. On an SMP machine, if |
| 186 | * the code being modified is also being executed on another CPU |
| 187 | * that CPU will have undefined results and possibly take a GPF. |
| 188 | * We use kstop_machine to stop other CPUS from exectuing code. |
| 189 | * But this does not stop NMIs from happening. We still need |
| 190 | * to protect against that. We separate out the modification of |
| 191 | * the code to take care of this. |
| 192 | * |
| 193 | * Two buffers are added: An IP buffer and a "code" buffer. |
| 194 | * |
Steven Rostedt | a26a2a2 | 2008-10-31 00:03:22 -0400 | [diff] [blame] | 195 | * 1) Put the instruction pointer into the IP buffer |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 196 | * and the new code into the "code" buffer. |
| 197 | * 2) Set a flag that says we are modifying code |
| 198 | * 3) Wait for any running NMIs to finish. |
| 199 | * 4) Write the code |
| 200 | * 5) clear the flag. |
| 201 | * 6) Wait for any running NMIs to finish. |
| 202 | * |
| 203 | * If an NMI is executed, the first thing it does is to call |
| 204 | * "ftrace_nmi_enter". This will check if the flag is set to write |
| 205 | * and if it is, it will write what is in the IP and "code" buffers. |
| 206 | * |
| 207 | * The trick is, it does not matter if everyone is writing the same |
| 208 | * content to the code location. Also, if a CPU is executing code |
| 209 | * it is OK to write to that code location if the contents being written |
| 210 | * are the same as what exists. |
| 211 | */ |
| 212 | |
Steven Rostedt | a26a2a2 | 2008-10-31 00:03:22 -0400 | [diff] [blame] | 213 | static atomic_t in_nmi = ATOMIC_INIT(0); |
| 214 | static int mod_code_status; /* holds return value of text write */ |
| 215 | static int mod_code_write; /* set when NMI should do the write */ |
| 216 | static void *mod_code_ip; /* holds the IP to write to */ |
| 217 | 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] | 218 | |
Steven Rostedt | a26a2a2 | 2008-10-31 00:03:22 -0400 | [diff] [blame] | 219 | static unsigned nmi_wait_count; |
| 220 | static atomic_t nmi_update_count = ATOMIC_INIT(0); |
Steven Rostedt | b807c3d | 2008-10-30 16:08:33 -0400 | [diff] [blame] | 221 | |
| 222 | int ftrace_arch_read_dyn_info(char *buf, int size) |
| 223 | { |
| 224 | int r; |
| 225 | |
| 226 | r = snprintf(buf, size, "%u %u", |
| 227 | nmi_wait_count, |
| 228 | atomic_read(&nmi_update_count)); |
| 229 | return r; |
| 230 | } |
| 231 | |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 232 | static void ftrace_mod_code(void) |
| 233 | { |
| 234 | /* |
| 235 | * Yes, more than one CPU process can be writing to mod_code_status. |
| 236 | * (and the code itself) |
| 237 | * But if one were to fail, then they all should, and if one were |
| 238 | * to succeed, then they all should. |
| 239 | */ |
| 240 | mod_code_status = probe_kernel_write(mod_code_ip, mod_code_newcode, |
| 241 | MCOUNT_INSN_SIZE); |
| 242 | |
| 243 | } |
| 244 | |
| 245 | void ftrace_nmi_enter(void) |
| 246 | { |
| 247 | atomic_inc(&in_nmi); |
| 248 | /* Must have in_nmi seen before reading write flag */ |
| 249 | smp_mb(); |
Steven Rostedt | b807c3d | 2008-10-30 16:08:33 -0400 | [diff] [blame] | 250 | if (mod_code_write) { |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 251 | ftrace_mod_code(); |
Steven Rostedt | b807c3d | 2008-10-30 16:08:33 -0400 | [diff] [blame] | 252 | atomic_inc(&nmi_update_count); |
| 253 | } |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | void ftrace_nmi_exit(void) |
| 257 | { |
| 258 | /* Finish all executions before clearing in_nmi */ |
| 259 | smp_wmb(); |
| 260 | atomic_dec(&in_nmi); |
| 261 | } |
| 262 | |
| 263 | static void wait_for_nmi(void) |
| 264 | { |
Steven Rostedt | b807c3d | 2008-10-30 16:08:33 -0400 | [diff] [blame] | 265 | int waited = 0; |
| 266 | |
| 267 | while (atomic_read(&in_nmi)) { |
| 268 | waited = 1; |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 269 | cpu_relax(); |
Steven Rostedt | b807c3d | 2008-10-30 16:08:33 -0400 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | if (waited) |
| 273 | nmi_wait_count++; |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static int |
| 277 | do_ftrace_mod_code(unsigned long ip, void *new_code) |
| 278 | { |
| 279 | mod_code_ip = (void *)ip; |
| 280 | mod_code_newcode = new_code; |
| 281 | |
| 282 | /* The buffers need to be visible before we let NMIs write them */ |
| 283 | smp_wmb(); |
| 284 | |
| 285 | mod_code_write = 1; |
| 286 | |
| 287 | /* Make sure write bit is visible before we wait on NMIs */ |
| 288 | smp_mb(); |
| 289 | |
| 290 | wait_for_nmi(); |
| 291 | |
| 292 | /* Make sure all running NMIs have finished before we write the code */ |
| 293 | smp_mb(); |
| 294 | |
| 295 | ftrace_mod_code(); |
| 296 | |
| 297 | /* Make sure the write happens before clearing the bit */ |
| 298 | smp_wmb(); |
| 299 | |
| 300 | mod_code_write = 0; |
| 301 | |
| 302 | /* make sure NMIs see the cleared bit */ |
| 303 | smp_mb(); |
| 304 | |
| 305 | wait_for_nmi(); |
| 306 | |
| 307 | return mod_code_status; |
| 308 | } |
| 309 | |
| 310 | |
Frederic Weisbecker | caf4b32 | 2008-11-11 07:03:45 +0100 | [diff] [blame] | 311 | |
| 312 | |
| 313 | static unsigned char ftrace_nop[MCOUNT_INSN_SIZE]; |
| 314 | |
| 315 | unsigned char *ftrace_nop_replace(void) |
| 316 | { |
| 317 | return ftrace_nop; |
| 318 | } |
| 319 | |
Steven Rostedt | 15adc04 | 2008-10-23 09:33:08 -0400 | [diff] [blame] | 320 | int |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 321 | ftrace_modify_code(unsigned long ip, unsigned char *old_code, |
| 322 | unsigned char *new_code) |
| 323 | { |
Steven Rostedt | 6f93fc0 | 2008-08-20 12:55:07 -0400 | [diff] [blame] | 324 | unsigned char replaced[MCOUNT_INSN_SIZE]; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 325 | |
| 326 | /* |
| 327 | * Note: Due to modules and __init, code can |
| 328 | * disappear and change, we need to protect against faulting |
Steven Rostedt | 76aefee | 2008-10-23 09:33:00 -0400 | [diff] [blame] | 329 | * as well as code changing. We do this by using the |
Steven Rostedt | ab9a091 | 2008-10-23 09:33:01 -0400 | [diff] [blame] | 330 | * probe_kernel_* functions. |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 331 | * |
| 332 | * No real locking needed, this code is run through |
Steven Rostedt | 6f93fc0 | 2008-08-20 12:55:07 -0400 | [diff] [blame] | 333 | * kstop_machine, or before SMP starts. |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 334 | */ |
Steven Rostedt | 76aefee | 2008-10-23 09:33:00 -0400 | [diff] [blame] | 335 | |
| 336 | /* read the text we want to modify */ |
Steven Rostedt | ab9a091 | 2008-10-23 09:33:01 -0400 | [diff] [blame] | 337 | if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE)) |
Steven Rostedt | 593eb8a | 2008-10-23 09:32:59 -0400 | [diff] [blame] | 338 | return -EFAULT; |
Steven Rostedt | 6f93fc0 | 2008-08-20 12:55:07 -0400 | [diff] [blame] | 339 | |
Steven Rostedt | 76aefee | 2008-10-23 09:33:00 -0400 | [diff] [blame] | 340 | /* Make sure it is what we expect it to be */ |
Steven Rostedt | 6f93fc0 | 2008-08-20 12:55:07 -0400 | [diff] [blame] | 341 | if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0) |
Steven Rostedt | 593eb8a | 2008-10-23 09:32:59 -0400 | [diff] [blame] | 342 | return -EINVAL; |
Steven Rostedt | 6f93fc0 | 2008-08-20 12:55:07 -0400 | [diff] [blame] | 343 | |
Steven Rostedt | 76aefee | 2008-10-23 09:33:00 -0400 | [diff] [blame] | 344 | /* replace the text with the new text */ |
Steven Rostedt | 17666f0 | 2008-10-30 16:08:32 -0400 | [diff] [blame] | 345 | if (do_ftrace_mod_code(ip, new_code)) |
Steven Rostedt | 593eb8a | 2008-10-23 09:32:59 -0400 | [diff] [blame] | 346 | return -EPERM; |
Steven Rostedt | 6f93fc0 | 2008-08-20 12:55:07 -0400 | [diff] [blame] | 347 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 348 | sync_core(); |
| 349 | |
Steven Rostedt | 6f93fc0 | 2008-08-20 12:55:07 -0400 | [diff] [blame] | 350 | return 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Steven Rostedt | 15adc04 | 2008-10-23 09:33:08 -0400 | [diff] [blame] | 353 | int ftrace_update_ftrace_func(ftrace_func_t func) |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 354 | { |
| 355 | unsigned long ip = (unsigned long)(&ftrace_call); |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 356 | unsigned char old[MCOUNT_INSN_SIZE], *new; |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 357 | int ret; |
| 358 | |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 359 | memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE); |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 360 | new = ftrace_call_replace(ip, (unsigned long)func); |
| 361 | ret = ftrace_modify_code(ip, old, new); |
| 362 | |
| 363 | return ret; |
| 364 | } |
| 365 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 366 | int __init ftrace_dyn_arch_init(void *data) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 367 | { |
Steven Rostedt | 732f3ca | 2008-08-14 18:05:05 -0400 | [diff] [blame] | 368 | extern const unsigned char ftrace_test_p6nop[]; |
| 369 | extern const unsigned char ftrace_test_nop5[]; |
| 370 | extern const unsigned char ftrace_test_jmp[]; |
| 371 | int faulted = 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 372 | |
Steven Rostedt | 732f3ca | 2008-08-14 18:05:05 -0400 | [diff] [blame] | 373 | /* |
| 374 | * There is no good nop for all x86 archs. |
| 375 | * We will default to using the P6_NOP5, but first we |
| 376 | * will test to make sure that the nop will actually |
| 377 | * work on this CPU. If it faults, we will then |
| 378 | * go to a lesser efficient 5 byte nop. If that fails |
| 379 | * we then just use a jmp as our nop. This isn't the most |
| 380 | * efficient nop, but we can not use a multi part nop |
| 381 | * since we would then risk being preempted in the middle |
| 382 | * of that nop, and if we enabled tracing then, it might |
| 383 | * cause a system crash. |
| 384 | * |
| 385 | * TODO: check the cpuid to determine the best nop. |
| 386 | */ |
| 387 | asm volatile ( |
Steven Rostedt | 732f3ca | 2008-08-14 18:05:05 -0400 | [diff] [blame] | 388 | "ftrace_test_jmp:" |
| 389 | "jmp ftrace_test_p6nop\n" |
Anders Kaseorg | 8b27386 | 2008-10-09 22:19:08 -0400 | [diff] [blame] | 390 | "nop\n" |
| 391 | "nop\n" |
| 392 | "nop\n" /* 2 byte jmp + 3 bytes */ |
Steven Rostedt | 732f3ca | 2008-08-14 18:05:05 -0400 | [diff] [blame] | 393 | "ftrace_test_p6nop:" |
| 394 | P6_NOP5 |
| 395 | "jmp 1f\n" |
| 396 | "ftrace_test_nop5:" |
| 397 | ".byte 0x66,0x66,0x66,0x66,0x90\n" |
Steven Rostedt | 732f3ca | 2008-08-14 18:05:05 -0400 | [diff] [blame] | 398 | "1:" |
| 399 | ".section .fixup, \"ax\"\n" |
| 400 | "2: movl $1, %0\n" |
| 401 | " jmp ftrace_test_nop5\n" |
| 402 | "3: movl $2, %0\n" |
| 403 | " jmp 1b\n" |
| 404 | ".previous\n" |
| 405 | _ASM_EXTABLE(ftrace_test_p6nop, 2b) |
| 406 | _ASM_EXTABLE(ftrace_test_nop5, 3b) |
| 407 | : "=r"(faulted) : "0" (faulted)); |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 408 | |
Steven Rostedt | 732f3ca | 2008-08-14 18:05:05 -0400 | [diff] [blame] | 409 | switch (faulted) { |
| 410 | case 0: |
| 411 | 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] | 412 | memcpy(ftrace_nop, ftrace_test_p6nop, MCOUNT_INSN_SIZE); |
Steven Rostedt | 732f3ca | 2008-08-14 18:05:05 -0400 | [diff] [blame] | 413 | break; |
| 414 | case 1: |
| 415 | 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] | 416 | memcpy(ftrace_nop, ftrace_test_nop5, MCOUNT_INSN_SIZE); |
Steven Rostedt | 732f3ca | 2008-08-14 18:05:05 -0400 | [diff] [blame] | 417 | break; |
| 418 | case 2: |
Anders Kaseorg | 8b27386 | 2008-10-09 22:19:08 -0400 | [diff] [blame] | 419 | pr_info("ftrace: converting mcount calls to jmp . + 5\n"); |
Steven Rostedt | 8115f3f | 2008-10-24 09:12:17 -0400 | [diff] [blame] | 420 | memcpy(ftrace_nop, ftrace_test_jmp, MCOUNT_INSN_SIZE); |
Steven Rostedt | 732f3ca | 2008-08-14 18:05:05 -0400 | [diff] [blame] | 421 | break; |
| 422 | } |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 423 | |
Steven Rostedt | 732f3ca | 2008-08-14 18:05:05 -0400 | [diff] [blame] | 424 | /* The return code is retured via data */ |
| 425 | *(unsigned long *)data = 0; |
Steven Rostedt | dfa60ab | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 426 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 427 | return 0; |
| 428 | } |
Frederic Weisbecker | caf4b32 | 2008-11-11 07:03:45 +0100 | [diff] [blame] | 429 | #endif |