blob: 16a571dea2ef73243ee844395ffc3ec5bd2a946c [file] [log] [blame]
Steven Rostedt3d083392008-05-12 21:20:42 +02001/*
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 Rostedt6f93fc02008-08-20 12:55:07 -040014#include <linux/uaccess.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020015#include <linux/ftrace.h>
16#include <linux/percpu.h>
Ingo Molnar19b3e962008-11-11 11:57:02 +010017#include <linux/sched.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020018#include <linux/init.h>
19#include <linux/list.h>
20
Abhishek Sagar395a59d2008-06-21 23:47:27 +053021#include <asm/ftrace.h>
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +010022#include <linux/ftrace.h>
Steven Rostedt732f3ca2008-08-14 18:05:05 -040023#include <asm/nops.h>
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +010024#include <asm/nmi.h>
Steven Rostedtdfa60ab2008-05-12 21:20:43 +020025
Steven Rostedt3d083392008-05-12 21:20:42 +020026
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +010027
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 */
35static atomic_t in_nmi;
36
37void ftrace_nmi_enter(void)
38{
39 atomic_inc(&in_nmi);
40}
41
42void ftrace_nmi_exit(void)
43{
44 atomic_dec(&in_nmi);
45}
46
47/*
48 * Synchronize accesses to return adresses stack with
49 * interrupts.
50 */
51static raw_spinlock_t ret_stack_lock;
52
53/* Add a function return address to the trace stack on thread info.*/
54static int push_return_trace(unsigned long ret, unsigned long long time,
55 unsigned long func)
56{
57 int index;
58 struct thread_info *ti;
59 unsigned long flags;
60 int err = 0;
61
62 raw_local_irq_save(flags);
63 __raw_spin_lock(&ret_stack_lock);
64
65 ti = current_thread_info();
66 /* The return trace stack is full */
67 if (ti->curr_ret_stack == FTRACE_RET_STACK_SIZE - 1) {
68 err = -EBUSY;
69 goto out;
70 }
71
72 index = ++ti->curr_ret_stack;
73 ti->ret_stack[index].ret = ret;
74 ti->ret_stack[index].func = func;
75 ti->ret_stack[index].calltime = time;
76
77out:
78 __raw_spin_unlock(&ret_stack_lock);
79 raw_local_irq_restore(flags);
80 return err;
81}
82
83/* Retrieve a function return address to the trace stack on thread info.*/
84static void pop_return_trace(unsigned long *ret, unsigned long long *time,
85 unsigned long *func)
86{
87 struct thread_info *ti;
88 int index;
89 unsigned long flags;
90
91 raw_local_irq_save(flags);
92 __raw_spin_lock(&ret_stack_lock);
93
94 ti = current_thread_info();
95 index = ti->curr_ret_stack;
96 *ret = ti->ret_stack[index].ret;
97 *func = ti->ret_stack[index].func;
98 *time = ti->ret_stack[index].calltime;
99 ti->curr_ret_stack--;
100
101 __raw_spin_unlock(&ret_stack_lock);
102 raw_local_irq_restore(flags);
103}
104
105/*
106 * Send the trace to the ring-buffer.
107 * @return the original return address.
108 */
109unsigned long ftrace_return_to_handler(void)
110{
111 struct ftrace_retfunc trace;
112 pop_return_trace(&trace.ret, &trace.calltime, &trace.func);
113 trace.rettime = cpu_clock(raw_smp_processor_id());
114 ftrace_function_return(&trace);
115
116 return trace.ret;
117}
118
119/*
120 * Hook the return address and push it in the stack of return addrs
121 * in current thread info.
122 */
123asmlinkage
124void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
125{
126 unsigned long old;
127 unsigned long long calltime;
128 int faulted;
129 unsigned long return_hooker = (unsigned long)
130 &return_to_handler;
131
132 /* Nmi's are currently unsupported */
133 if (atomic_read(&in_nmi))
134 return;
135
136 /*
137 * Protect against fault, even if it shouldn't
138 * happen. This tool is too much intrusive to
139 * ignore such a protection.
140 */
141 asm volatile(
142 "1: movl (%[parent_old]), %[old]\n"
143 "2: movl %[return_hooker], (%[parent_replaced])\n"
144 " movl $0, %[faulted]\n"
145
146 ".section .fixup, \"ax\"\n"
147 "3: movl $1, %[faulted]\n"
148 ".previous\n"
149
150 ".section __ex_table, \"a\"\n"
151 " .long 1b, 3b\n"
152 " .long 2b, 3b\n"
153 ".previous\n"
154
Ingo Molnar867f7fb2008-11-11 11:18:14 +0100155 : [parent_replaced] "=r" (parent), [old] "=r" (old),
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +0100156 [faulted] "=r" (faulted)
157 : [parent_old] "0" (parent), [return_hooker] "r" (return_hooker)
158 : "memory"
159 );
160
161 if (WARN_ON(faulted)) {
162 unregister_ftrace_return();
163 return;
164 }
165
166 if (WARN_ON(!__kernel_text_address(old))) {
167 unregister_ftrace_return();
168 *parent = old;
169 return;
170 }
171
172 calltime = cpu_clock(raw_smp_processor_id());
173
174 if (push_return_trace(old, calltime, self_addr) == -EBUSY)
175 *parent = old;
176}
177
178static int __init init_ftrace_function_return(void)
179{
180 ret_stack_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
181 return 0;
182}
183device_initcall(init_ftrace_function_return);
184
185
186#endif
187
188#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt3d083392008-05-12 21:20:42 +0200189
Steven Rostedt3d083392008-05-12 21:20:42 +0200190union ftrace_code_union {
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530191 char code[MCOUNT_INSN_SIZE];
Steven Rostedt3d083392008-05-12 21:20:42 +0200192 struct {
193 char e8;
194 int offset;
195 } __attribute__((packed));
196};
197
Steven Rostedt15adc042008-10-23 09:33:08 -0400198static int ftrace_calc_offset(long ip, long addr)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200199{
200 return (int)(addr - ip);
201}
202
Steven Rostedt15adc042008-10-23 09:33:08 -0400203unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200204{
205 static union ftrace_code_union calc;
206
207 calc.e8 = 0xe8;
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530208 calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200209
210 /*
211 * No locking needed, this must be called via kstop_machine
212 * which in essence is like running on a uniprocessor machine.
213 */
214 return calc.code;
215}
216
Steven Rostedt17666f02008-10-30 16:08:32 -0400217/*
218 * Modifying code must take extra care. On an SMP machine, if
219 * the code being modified is also being executed on another CPU
220 * that CPU will have undefined results and possibly take a GPF.
221 * We use kstop_machine to stop other CPUS from exectuing code.
222 * But this does not stop NMIs from happening. We still need
223 * to protect against that. We separate out the modification of
224 * the code to take care of this.
225 *
226 * Two buffers are added: An IP buffer and a "code" buffer.
227 *
Steven Rostedta26a2a22008-10-31 00:03:22 -0400228 * 1) Put the instruction pointer into the IP buffer
Steven Rostedt17666f02008-10-30 16:08:32 -0400229 * and the new code into the "code" buffer.
230 * 2) Set a flag that says we are modifying code
231 * 3) Wait for any running NMIs to finish.
232 * 4) Write the code
233 * 5) clear the flag.
234 * 6) Wait for any running NMIs to finish.
235 *
236 * If an NMI is executed, the first thing it does is to call
237 * "ftrace_nmi_enter". This will check if the flag is set to write
238 * and if it is, it will write what is in the IP and "code" buffers.
239 *
240 * The trick is, it does not matter if everyone is writing the same
241 * content to the code location. Also, if a CPU is executing code
242 * it is OK to write to that code location if the contents being written
243 * are the same as what exists.
244 */
245
Steven Rostedta26a2a22008-10-31 00:03:22 -0400246static atomic_t in_nmi = ATOMIC_INIT(0);
247static int mod_code_status; /* holds return value of text write */
248static int mod_code_write; /* set when NMI should do the write */
249static void *mod_code_ip; /* holds the IP to write to */
250static void *mod_code_newcode; /* holds the text to write to the IP */
Steven Rostedt17666f02008-10-30 16:08:32 -0400251
Steven Rostedta26a2a22008-10-31 00:03:22 -0400252static unsigned nmi_wait_count;
253static atomic_t nmi_update_count = ATOMIC_INIT(0);
Steven Rostedtb807c3d2008-10-30 16:08:33 -0400254
255int ftrace_arch_read_dyn_info(char *buf, int size)
256{
257 int r;
258
259 r = snprintf(buf, size, "%u %u",
260 nmi_wait_count,
261 atomic_read(&nmi_update_count));
262 return r;
263}
264
Steven Rostedt17666f02008-10-30 16:08:32 -0400265static void ftrace_mod_code(void)
266{
267 /*
268 * Yes, more than one CPU process can be writing to mod_code_status.
269 * (and the code itself)
270 * But if one were to fail, then they all should, and if one were
271 * to succeed, then they all should.
272 */
273 mod_code_status = probe_kernel_write(mod_code_ip, mod_code_newcode,
274 MCOUNT_INSN_SIZE);
275
276}
277
278void ftrace_nmi_enter(void)
279{
280 atomic_inc(&in_nmi);
281 /* Must have in_nmi seen before reading write flag */
282 smp_mb();
Steven Rostedtb807c3d2008-10-30 16:08:33 -0400283 if (mod_code_write) {
Steven Rostedt17666f02008-10-30 16:08:32 -0400284 ftrace_mod_code();
Steven Rostedtb807c3d2008-10-30 16:08:33 -0400285 atomic_inc(&nmi_update_count);
286 }
Steven Rostedt17666f02008-10-30 16:08:32 -0400287}
288
289void ftrace_nmi_exit(void)
290{
291 /* Finish all executions before clearing in_nmi */
292 smp_wmb();
293 atomic_dec(&in_nmi);
294}
295
296static void wait_for_nmi(void)
297{
Steven Rostedtb807c3d2008-10-30 16:08:33 -0400298 int waited = 0;
299
300 while (atomic_read(&in_nmi)) {
301 waited = 1;
Steven Rostedt17666f02008-10-30 16:08:32 -0400302 cpu_relax();
Steven Rostedtb807c3d2008-10-30 16:08:33 -0400303 }
304
305 if (waited)
306 nmi_wait_count++;
Steven Rostedt17666f02008-10-30 16:08:32 -0400307}
308
309static int
310do_ftrace_mod_code(unsigned long ip, void *new_code)
311{
312 mod_code_ip = (void *)ip;
313 mod_code_newcode = new_code;
314
315 /* The buffers need to be visible before we let NMIs write them */
316 smp_wmb();
317
318 mod_code_write = 1;
319
320 /* Make sure write bit is visible before we wait on NMIs */
321 smp_mb();
322
323 wait_for_nmi();
324
325 /* Make sure all running NMIs have finished before we write the code */
326 smp_mb();
327
328 ftrace_mod_code();
329
330 /* Make sure the write happens before clearing the bit */
331 smp_wmb();
332
333 mod_code_write = 0;
334
335 /* make sure NMIs see the cleared bit */
336 smp_mb();
337
338 wait_for_nmi();
339
340 return mod_code_status;
341}
342
343
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +0100344
345
346static unsigned char ftrace_nop[MCOUNT_INSN_SIZE];
347
348unsigned char *ftrace_nop_replace(void)
349{
350 return ftrace_nop;
351}
352
Steven Rostedt15adc042008-10-23 09:33:08 -0400353int
Steven Rostedt3d083392008-05-12 21:20:42 +0200354ftrace_modify_code(unsigned long ip, unsigned char *old_code,
355 unsigned char *new_code)
356{
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400357 unsigned char replaced[MCOUNT_INSN_SIZE];
Steven Rostedt3d083392008-05-12 21:20:42 +0200358
359 /*
360 * Note: Due to modules and __init, code can
361 * disappear and change, we need to protect against faulting
Steven Rostedt76aefee2008-10-23 09:33:00 -0400362 * as well as code changing. We do this by using the
Steven Rostedtab9a0912008-10-23 09:33:01 -0400363 * probe_kernel_* functions.
Steven Rostedt3d083392008-05-12 21:20:42 +0200364 *
365 * No real locking needed, this code is run through
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400366 * kstop_machine, or before SMP starts.
Steven Rostedt3d083392008-05-12 21:20:42 +0200367 */
Steven Rostedt76aefee2008-10-23 09:33:00 -0400368
369 /* read the text we want to modify */
Steven Rostedtab9a0912008-10-23 09:33:01 -0400370 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt593eb8a2008-10-23 09:32:59 -0400371 return -EFAULT;
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400372
Steven Rostedt76aefee2008-10-23 09:33:00 -0400373 /* Make sure it is what we expect it to be */
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400374 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
Steven Rostedt593eb8a2008-10-23 09:32:59 -0400375 return -EINVAL;
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400376
Steven Rostedt76aefee2008-10-23 09:33:00 -0400377 /* replace the text with the new text */
Steven Rostedt17666f02008-10-30 16:08:32 -0400378 if (do_ftrace_mod_code(ip, new_code))
Steven Rostedt593eb8a2008-10-23 09:32:59 -0400379 return -EPERM;
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400380
Steven Rostedt3d083392008-05-12 21:20:42 +0200381 sync_core();
382
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400383 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200384}
385
Steven Rostedt15adc042008-10-23 09:33:08 -0400386int ftrace_update_ftrace_func(ftrace_func_t func)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200387{
388 unsigned long ip = (unsigned long)(&ftrace_call);
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530389 unsigned char old[MCOUNT_INSN_SIZE], *new;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200390 int ret;
391
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530392 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200393 new = ftrace_call_replace(ip, (unsigned long)func);
394 ret = ftrace_modify_code(ip, old, new);
395
396 return ret;
397}
398
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200399int __init ftrace_dyn_arch_init(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +0200400{
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400401 extern const unsigned char ftrace_test_p6nop[];
402 extern const unsigned char ftrace_test_nop5[];
403 extern const unsigned char ftrace_test_jmp[];
404 int faulted = 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200405
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400406 /*
407 * There is no good nop for all x86 archs.
408 * We will default to using the P6_NOP5, but first we
409 * will test to make sure that the nop will actually
410 * work on this CPU. If it faults, we will then
411 * go to a lesser efficient 5 byte nop. If that fails
412 * we then just use a jmp as our nop. This isn't the most
413 * efficient nop, but we can not use a multi part nop
414 * since we would then risk being preempted in the middle
415 * of that nop, and if we enabled tracing then, it might
416 * cause a system crash.
417 *
418 * TODO: check the cpuid to determine the best nop.
419 */
420 asm volatile (
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400421 "ftrace_test_jmp:"
422 "jmp ftrace_test_p6nop\n"
Anders Kaseorg8b273862008-10-09 22:19:08 -0400423 "nop\n"
424 "nop\n"
425 "nop\n" /* 2 byte jmp + 3 bytes */
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400426 "ftrace_test_p6nop:"
427 P6_NOP5
428 "jmp 1f\n"
429 "ftrace_test_nop5:"
430 ".byte 0x66,0x66,0x66,0x66,0x90\n"
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400431 "1:"
432 ".section .fixup, \"ax\"\n"
433 "2: movl $1, %0\n"
434 " jmp ftrace_test_nop5\n"
435 "3: movl $2, %0\n"
436 " jmp 1b\n"
437 ".previous\n"
438 _ASM_EXTABLE(ftrace_test_p6nop, 2b)
439 _ASM_EXTABLE(ftrace_test_nop5, 3b)
440 : "=r"(faulted) : "0" (faulted));
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200441
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400442 switch (faulted) {
443 case 0:
444 pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
Steven Rostedt8115f3f2008-10-24 09:12:17 -0400445 memcpy(ftrace_nop, ftrace_test_p6nop, MCOUNT_INSN_SIZE);
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400446 break;
447 case 1:
448 pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
Steven Rostedt8115f3f2008-10-24 09:12:17 -0400449 memcpy(ftrace_nop, ftrace_test_nop5, MCOUNT_INSN_SIZE);
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400450 break;
451 case 2:
Anders Kaseorg8b273862008-10-09 22:19:08 -0400452 pr_info("ftrace: converting mcount calls to jmp . + 5\n");
Steven Rostedt8115f3f2008-10-24 09:12:17 -0400453 memcpy(ftrace_nop, ftrace_test_jmp, MCOUNT_INSN_SIZE);
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400454 break;
455 }
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200456
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400457 /* The return code is retured via data */
458 *(unsigned long *)data = 0;
Steven Rostedtdfa60ab2008-05-12 21:20:43 +0200459
Steven Rostedt3d083392008-05-12 21:20:42 +0200460 return 0;
461}
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +0100462#endif