blob: 80af34739a9ae9284e323d380c08a9c0328a9f30 [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
Joe Perches3bb258b2009-10-04 17:53:29 -070012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
Steven Rostedt3d083392008-05-12 21:20:42 +020014#include <linux/spinlock.h>
15#include <linux/hardirq.h>
Steven Rostedt6f93fc02008-08-20 12:55:07 -040016#include <linux/uaccess.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020017#include <linux/ftrace.h>
18#include <linux/percpu.h>
Ingo Molnar19b3e962008-11-11 11:57:02 +010019#include <linux/sched.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020020#include <linux/init.h>
21#include <linux/list.h>
matthieu castet84e1c6b2010-11-16 22:35:16 +010022#include <linux/module.h>
Steven Rostedt08d636b2011-08-16 09:57:10 -040023#include <linux/kprobes.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020024
Frederic Weisbecker47788c52009-04-08 20:40:59 +020025#include <trace/syscall.h>
26
Steven Rostedt16239632009-02-17 17:57:30 -050027#include <asm/cacheflush.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053028#include <asm/ftrace.h>
Steven Rostedt732f3ca2008-08-14 18:05:05 -040029#include <asm/nops.h>
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +010030#include <asm/nmi.h>
Steven Rostedtdfa60ab2008-05-12 21:20:43 +020031
Steven Rostedt3d083392008-05-12 21:20:42 +020032
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +010033#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt3d083392008-05-12 21:20:42 +020034
Steven Rostedt0c54dd32010-02-25 08:42:06 -050035/*
36 * modifying_code is set to notify NMIs that they need to use
37 * memory barriers when entering or exiting. But we don't want
38 * to burden NMIs with unnecessary memory barriers when code
39 * modification is not being done (which is most of the time).
40 *
41 * A mutex is already held when ftrace_arch_code_modify_prepare
42 * and post_process are called. No locks need to be taken here.
43 *
44 * Stop machine will make sure currently running NMIs are done
45 * and new NMIs will see the updated variable before we need
46 * to worry about NMIs doing memory barriers.
47 */
48static int modifying_code __read_mostly;
49static DEFINE_PER_CPU(int, save_modifying_code);
50
Steven Rostedt16239632009-02-17 17:57:30 -050051int ftrace_arch_code_modify_prepare(void)
52{
53 set_kernel_text_rw();
matthieu castet84e1c6b2010-11-16 22:35:16 +010054 set_all_modules_text_rw();
Steven Rostedt0c54dd32010-02-25 08:42:06 -050055 modifying_code = 1;
Steven Rostedt16239632009-02-17 17:57:30 -050056 return 0;
57}
58
59int ftrace_arch_code_modify_post_process(void)
60{
Steven Rostedt0c54dd32010-02-25 08:42:06 -050061 modifying_code = 0;
matthieu castet84e1c6b2010-11-16 22:35:16 +010062 set_all_modules_text_ro();
Steven Rostedt16239632009-02-17 17:57:30 -050063 set_kernel_text_ro();
64 return 0;
65}
66
Steven Rostedt3d083392008-05-12 21:20:42 +020067union ftrace_code_union {
Abhishek Sagar395a59d2008-06-21 23:47:27 +053068 char code[MCOUNT_INSN_SIZE];
Steven Rostedt3d083392008-05-12 21:20:42 +020069 struct {
70 char e8;
71 int offset;
72 } __attribute__((packed));
73};
74
Steven Rostedt15adc042008-10-23 09:33:08 -040075static int ftrace_calc_offset(long ip, long addr)
Steven Rostedt3c1720f2008-05-12 21:20:43 +020076{
77 return (int)(addr - ip);
78}
79
Steven Rostedt31e88902008-11-14 16:21:19 -080080static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
Steven Rostedt3c1720f2008-05-12 21:20:43 +020081{
82 static union ftrace_code_union calc;
83
84 calc.e8 = 0xe8;
Abhishek Sagar395a59d2008-06-21 23:47:27 +053085 calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
Steven Rostedt3c1720f2008-05-12 21:20:43 +020086
87 /*
88 * No locking needed, this must be called via kstop_machine
89 * which in essence is like running on a uniprocessor machine.
90 */
91 return calc.code;
92}
93
Steven Rostedt17666f02008-10-30 16:08:32 -040094/*
95 * Modifying code must take extra care. On an SMP machine, if
96 * the code being modified is also being executed on another CPU
97 * that CPU will have undefined results and possibly take a GPF.
98 * We use kstop_machine to stop other CPUS from exectuing code.
99 * But this does not stop NMIs from happening. We still need
100 * to protect against that. We separate out the modification of
101 * the code to take care of this.
102 *
103 * Two buffers are added: An IP buffer and a "code" buffer.
104 *
Steven Rostedta26a2a22008-10-31 00:03:22 -0400105 * 1) Put the instruction pointer into the IP buffer
Steven Rostedt17666f02008-10-30 16:08:32 -0400106 * and the new code into the "code" buffer.
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800107 * 2) Wait for any running NMIs to finish and set a flag that says
108 * we are modifying code, it is done in an atomic operation.
109 * 3) Write the code
110 * 4) clear the flag.
111 * 5) Wait for any running NMIs to finish.
Steven Rostedt17666f02008-10-30 16:08:32 -0400112 *
113 * If an NMI is executed, the first thing it does is to call
114 * "ftrace_nmi_enter". This will check if the flag is set to write
115 * and if it is, it will write what is in the IP and "code" buffers.
116 *
117 * The trick is, it does not matter if everyone is writing the same
118 * content to the code location. Also, if a CPU is executing code
119 * it is OK to write to that code location if the contents being written
120 * are the same as what exists.
121 */
122
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800123#define MOD_CODE_WRITE_FLAG (1 << 31) /* set when NMI should do the write */
Steven Rostedt4e6ea142009-02-05 22:30:07 -0500124static atomic_t nmi_running = ATOMIC_INIT(0);
Steven Rostedta26a2a22008-10-31 00:03:22 -0400125static int mod_code_status; /* holds return value of text write */
Steven Rostedta26a2a22008-10-31 00:03:22 -0400126static void *mod_code_ip; /* holds the IP to write to */
Rakib Mullick0d098a72011-05-12 23:33:40 +0600127static const void *mod_code_newcode; /* holds the text to write to the IP */
Steven Rostedt17666f02008-10-30 16:08:32 -0400128
Steven Rostedta26a2a22008-10-31 00:03:22 -0400129static unsigned nmi_wait_count;
130static atomic_t nmi_update_count = ATOMIC_INIT(0);
Steven Rostedtb807c3d2008-10-30 16:08:33 -0400131
132int ftrace_arch_read_dyn_info(char *buf, int size)
133{
134 int r;
135
136 r = snprintf(buf, size, "%u %u",
137 nmi_wait_count,
138 atomic_read(&nmi_update_count));
139 return r;
140}
141
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800142static void clear_mod_flag(void)
143{
144 int old = atomic_read(&nmi_running);
145
146 for (;;) {
147 int new = old & ~MOD_CODE_WRITE_FLAG;
148
149 if (old == new)
150 break;
151
152 old = atomic_cmpxchg(&nmi_running, old, new);
153 }
154}
155
Steven Rostedt17666f02008-10-30 16:08:32 -0400156static void ftrace_mod_code(void)
157{
158 /*
159 * Yes, more than one CPU process can be writing to mod_code_status.
160 * (and the code itself)
161 * But if one were to fail, then they all should, and if one were
162 * to succeed, then they all should.
163 */
164 mod_code_status = probe_kernel_write(mod_code_ip, mod_code_newcode,
165 MCOUNT_INSN_SIZE);
Steven Rostedt90c7ac42009-02-19 13:32:57 -0500166
167 /* if we fail, then kill any new writers */
168 if (mod_code_status)
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800169 clear_mod_flag();
Steven Rostedt17666f02008-10-30 16:08:32 -0400170}
171
Steven Rostedta81bd802009-02-06 01:45:16 -0500172void ftrace_nmi_enter(void)
Steven Rostedt17666f02008-10-30 16:08:32 -0400173{
Tejun Heo0a3aee02010-12-18 16:28:55 +0100174 __this_cpu_write(save_modifying_code, modifying_code);
Steven Rostedt0c54dd32010-02-25 08:42:06 -0500175
Tejun Heo0a3aee02010-12-18 16:28:55 +0100176 if (!__this_cpu_read(save_modifying_code))
Steven Rostedt0c54dd32010-02-25 08:42:06 -0500177 return;
178
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800179 if (atomic_inc_return(&nmi_running) & MOD_CODE_WRITE_FLAG) {
180 smp_rmb();
Steven Rostedt17666f02008-10-30 16:08:32 -0400181 ftrace_mod_code();
Steven Rostedtb807c3d2008-10-30 16:08:33 -0400182 atomic_inc(&nmi_update_count);
183 }
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800184 /* Must have previous changes seen before executions */
185 smp_mb();
Steven Rostedt17666f02008-10-30 16:08:32 -0400186}
187
Steven Rostedta81bd802009-02-06 01:45:16 -0500188void ftrace_nmi_exit(void)
Steven Rostedt17666f02008-10-30 16:08:32 -0400189{
Tejun Heo0a3aee02010-12-18 16:28:55 +0100190 if (!__this_cpu_read(save_modifying_code))
Steven Rostedt0c54dd32010-02-25 08:42:06 -0500191 return;
192
Steven Rostedt4e6ea142009-02-05 22:30:07 -0500193 /* Finish all executions before clearing nmi_running */
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800194 smp_mb();
Steven Rostedt4e6ea142009-02-05 22:30:07 -0500195 atomic_dec(&nmi_running);
Steven Rostedt17666f02008-10-30 16:08:32 -0400196}
197
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800198static void wait_for_nmi_and_set_mod_flag(void)
199{
200 if (!atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG))
201 return;
202
203 do {
204 cpu_relax();
205 } while (atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG));
206
207 nmi_wait_count++;
208}
209
Steven Rostedt17666f02008-10-30 16:08:32 -0400210static void wait_for_nmi(void)
211{
Steven Rostedt4e6ea142009-02-05 22:30:07 -0500212 if (!atomic_read(&nmi_running))
Cyrill Gorcunov89025282009-01-26 18:28:02 +0300213 return;
Steven Rostedtb807c3d2008-10-30 16:08:33 -0400214
Cyrill Gorcunov89025282009-01-26 18:28:02 +0300215 do {
Steven Rostedt17666f02008-10-30 16:08:32 -0400216 cpu_relax();
Steven Rostedt4e6ea142009-02-05 22:30:07 -0500217 } while (atomic_read(&nmi_running));
Steven Rostedtb807c3d2008-10-30 16:08:33 -0400218
Cyrill Gorcunov89025282009-01-26 18:28:02 +0300219 nmi_wait_count++;
Steven Rostedt17666f02008-10-30 16:08:32 -0400220}
221
Suresh Siddha55ca3cc2009-10-28 18:46:57 -0800222static inline int
223within(unsigned long addr, unsigned long start, unsigned long end)
224{
225 return addr >= start && addr < end;
226}
227
Steven Rostedt17666f02008-10-30 16:08:32 -0400228static int
Rakib Mullick0d098a72011-05-12 23:33:40 +0600229do_ftrace_mod_code(unsigned long ip, const void *new_code)
Steven Rostedt17666f02008-10-30 16:08:32 -0400230{
Suresh Siddha55ca3cc2009-10-28 18:46:57 -0800231 /*
232 * On x86_64, kernel text mappings are mapped read-only with
233 * CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead
234 * of the kernel text mapping to modify the kernel text.
235 *
236 * For 32bit kernels, these mappings are same and we can use
237 * kernel identity mapping to modify code.
238 */
239 if (within(ip, (unsigned long)_text, (unsigned long)_etext))
240 ip = (unsigned long)__va(__pa(ip));
241
Steven Rostedt17666f02008-10-30 16:08:32 -0400242 mod_code_ip = (void *)ip;
243 mod_code_newcode = new_code;
244
245 /* The buffers need to be visible before we let NMIs write them */
Steven Rostedt17666f02008-10-30 16:08:32 -0400246 smp_mb();
247
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800248 wait_for_nmi_and_set_mod_flag();
Steven Rostedt17666f02008-10-30 16:08:32 -0400249
250 /* Make sure all running NMIs have finished before we write the code */
251 smp_mb();
252
253 ftrace_mod_code();
254
255 /* Make sure the write happens before clearing the bit */
Steven Rostedt17666f02008-10-30 16:08:32 -0400256 smp_mb();
257
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800258 clear_mod_flag();
Steven Rostedt17666f02008-10-30 16:08:32 -0400259 wait_for_nmi();
260
261 return mod_code_status;
262}
263
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700264static const unsigned char *ftrace_nop_replace(void)
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +0100265{
H. Peter Anvindc326fc2011-04-18 15:19:51 -0700266 return ideal_nops[NOP_ATOMIC5];
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +0100267}
268
Steven Rostedt31e88902008-11-14 16:21:19 -0800269static int
Rakib Mullick0d098a72011-05-12 23:33:40 +0600270ftrace_modify_code(unsigned long ip, unsigned const char *old_code,
271 unsigned const char *new_code)
Steven Rostedt3d083392008-05-12 21:20:42 +0200272{
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400273 unsigned char replaced[MCOUNT_INSN_SIZE];
Steven Rostedt3d083392008-05-12 21:20:42 +0200274
275 /*
276 * Note: Due to modules and __init, code can
277 * disappear and change, we need to protect against faulting
Steven Rostedt76aefee2008-10-23 09:33:00 -0400278 * as well as code changing. We do this by using the
Steven Rostedtab9a0912008-10-23 09:33:01 -0400279 * probe_kernel_* functions.
Steven Rostedt3d083392008-05-12 21:20:42 +0200280 *
281 * No real locking needed, this code is run through
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400282 * kstop_machine, or before SMP starts.
Steven Rostedt3d083392008-05-12 21:20:42 +0200283 */
Steven Rostedt76aefee2008-10-23 09:33:00 -0400284
285 /* read the text we want to modify */
Steven Rostedtab9a0912008-10-23 09:33:01 -0400286 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt593eb8a2008-10-23 09:32:59 -0400287 return -EFAULT;
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400288
Steven Rostedt76aefee2008-10-23 09:33:00 -0400289 /* Make sure it is what we expect it to be */
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400290 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
Steven Rostedt593eb8a2008-10-23 09:32:59 -0400291 return -EINVAL;
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400292
Steven Rostedt76aefee2008-10-23 09:33:00 -0400293 /* replace the text with the new text */
Steven Rostedt17666f02008-10-30 16:08:32 -0400294 if (do_ftrace_mod_code(ip, new_code))
Steven Rostedt593eb8a2008-10-23 09:32:59 -0400295 return -EPERM;
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400296
Steven Rostedt3d083392008-05-12 21:20:42 +0200297 sync_core();
298
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400299 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200300}
301
Steven Rostedt31e88902008-11-14 16:21:19 -0800302int ftrace_make_nop(struct module *mod,
303 struct dyn_ftrace *rec, unsigned long addr)
304{
Rakib Mullick0d098a72011-05-12 23:33:40 +0600305 unsigned const char *new, *old;
Steven Rostedt31e88902008-11-14 16:21:19 -0800306 unsigned long ip = rec->ip;
307
308 old = ftrace_call_replace(ip, addr);
309 new = ftrace_nop_replace();
310
311 return ftrace_modify_code(rec->ip, old, new);
312}
313
314int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
315{
Rakib Mullick0d098a72011-05-12 23:33:40 +0600316 unsigned const char *new, *old;
Steven Rostedt31e88902008-11-14 16:21:19 -0800317 unsigned long ip = rec->ip;
318
319 old = ftrace_nop_replace();
320 new = ftrace_call_replace(ip, addr);
321
322 return ftrace_modify_code(rec->ip, old, new);
323}
324
Steven Rostedt15adc042008-10-23 09:33:08 -0400325int ftrace_update_ftrace_func(ftrace_func_t func)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200326{
327 unsigned long ip = (unsigned long)(&ftrace_call);
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530328 unsigned char old[MCOUNT_INSN_SIZE], *new;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200329 int ret;
330
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530331 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200332 new = ftrace_call_replace(ip, (unsigned long)func);
333 ret = ftrace_modify_code(ip, old, new);
334
335 return ret;
336}
337
Steven Rostedt08d636b2011-08-16 09:57:10 -0400338int modifying_ftrace_code __read_mostly;
339
340/*
341 * A breakpoint was added to the code address we are about to
342 * modify, and this is the handle that will just skip over it.
343 * We are either changing a nop into a trace call, or a trace
344 * call to a nop. While the change is taking place, we treat
345 * it just like it was a nop.
346 */
347int ftrace_int3_handler(struct pt_regs *regs)
348{
349 if (WARN_ON_ONCE(!regs))
350 return 0;
351
352 if (!ftrace_location(regs->ip - 1))
353 return 0;
354
355 regs->ip += MCOUNT_INSN_SIZE - 1;
356
357 return 1;
358}
359
360static int ftrace_write(unsigned long ip, const char *val, int size)
361{
362 /*
363 * On x86_64, kernel text mappings are mapped read-only with
364 * CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead
365 * of the kernel text mapping to modify the kernel text.
366 *
367 * For 32bit kernels, these mappings are same and we can use
368 * kernel identity mapping to modify code.
369 */
370 if (within(ip, (unsigned long)_text, (unsigned long)_etext))
371 ip = (unsigned long)__va(__pa(ip));
372
373 return probe_kernel_write((void *)ip, val, size);
374}
375
376static int add_break(unsigned long ip, const char *old)
377{
378 unsigned char replaced[MCOUNT_INSN_SIZE];
379 unsigned char brk = BREAKPOINT_INSTRUCTION;
380
381 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
382 return -EFAULT;
383
384 /* Make sure it is what we expect it to be */
385 if (memcmp(replaced, old, MCOUNT_INSN_SIZE) != 0)
386 return -EINVAL;
387
388 if (ftrace_write(ip, &brk, 1))
389 return -EPERM;
390
391 return 0;
392}
393
394static int add_brk_on_call(struct dyn_ftrace *rec, unsigned long addr)
395{
396 unsigned const char *old;
397 unsigned long ip = rec->ip;
398
399 old = ftrace_call_replace(ip, addr);
400
401 return add_break(rec->ip, old);
402}
403
404
405static int add_brk_on_nop(struct dyn_ftrace *rec)
406{
407 unsigned const char *old;
408
409 old = ftrace_nop_replace();
410
411 return add_break(rec->ip, old);
412}
413
414static int add_breakpoints(struct dyn_ftrace *rec, int enable)
415{
416 unsigned long ftrace_addr;
417 int ret;
418
419 ret = ftrace_test_record(rec, enable);
420
421 ftrace_addr = (unsigned long)FTRACE_ADDR;
422
423 switch (ret) {
424 case FTRACE_UPDATE_IGNORE:
425 return 0;
426
427 case FTRACE_UPDATE_MAKE_CALL:
428 /* converting nop to call */
429 return add_brk_on_nop(rec);
430
431 case FTRACE_UPDATE_MAKE_NOP:
432 /* converting a call to a nop */
433 return add_brk_on_call(rec, ftrace_addr);
434 }
435 return 0;
436}
437
438/*
439 * On error, we need to remove breakpoints. This needs to
440 * be done caefully. If the address does not currently have a
441 * breakpoint, we know we are done. Otherwise, we look at the
442 * remaining 4 bytes of the instruction. If it matches a nop
443 * we replace the breakpoint with the nop. Otherwise we replace
444 * it with the call instruction.
445 */
446static int remove_breakpoint(struct dyn_ftrace *rec)
447{
448 unsigned char ins[MCOUNT_INSN_SIZE];
449 unsigned char brk = BREAKPOINT_INSTRUCTION;
450 const unsigned char *nop;
451 unsigned long ftrace_addr;
452 unsigned long ip = rec->ip;
453
454 /* If we fail the read, just give up */
455 if (probe_kernel_read(ins, (void *)ip, MCOUNT_INSN_SIZE))
456 return -EFAULT;
457
458 /* If this does not have a breakpoint, we are done */
459 if (ins[0] != brk)
460 return -1;
461
462 nop = ftrace_nop_replace();
463
464 /*
465 * If the last 4 bytes of the instruction do not match
466 * a nop, then we assume that this is a call to ftrace_addr.
467 */
468 if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0) {
469 /*
470 * For extra paranoidism, we check if the breakpoint is on
471 * a call that would actually jump to the ftrace_addr.
472 * If not, don't touch the breakpoint, we make just create
473 * a disaster.
474 */
475 ftrace_addr = (unsigned long)FTRACE_ADDR;
476 nop = ftrace_call_replace(ip, ftrace_addr);
477
478 if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0)
479 return -EINVAL;
480 }
481
482 return probe_kernel_write((void *)ip, &nop[0], 1);
483}
484
485static int add_update_code(unsigned long ip, unsigned const char *new)
486{
487 /* skip breakpoint */
488 ip++;
489 new++;
490 if (ftrace_write(ip, new, MCOUNT_INSN_SIZE - 1))
491 return -EPERM;
492 return 0;
493}
494
495static int add_update_call(struct dyn_ftrace *rec, unsigned long addr)
496{
497 unsigned long ip = rec->ip;
498 unsigned const char *new;
499
500 new = ftrace_call_replace(ip, addr);
501 return add_update_code(ip, new);
502}
503
504static int add_update_nop(struct dyn_ftrace *rec)
505{
506 unsigned long ip = rec->ip;
507 unsigned const char *new;
508
509 new = ftrace_nop_replace();
510 return add_update_code(ip, new);
511}
512
513static int add_update(struct dyn_ftrace *rec, int enable)
514{
515 unsigned long ftrace_addr;
516 int ret;
517
518 ret = ftrace_test_record(rec, enable);
519
520 ftrace_addr = (unsigned long)FTRACE_ADDR;
521
522 switch (ret) {
523 case FTRACE_UPDATE_IGNORE:
524 return 0;
525
526 case FTRACE_UPDATE_MAKE_CALL:
527 /* converting nop to call */
528 return add_update_call(rec, ftrace_addr);
529
530 case FTRACE_UPDATE_MAKE_NOP:
531 /* converting a call to a nop */
532 return add_update_nop(rec);
533 }
534
535 return 0;
536}
537
538static int finish_update_call(struct dyn_ftrace *rec, unsigned long addr)
539{
540 unsigned long ip = rec->ip;
541 unsigned const char *new;
542
543 new = ftrace_call_replace(ip, addr);
544
545 if (ftrace_write(ip, new, 1))
546 return -EPERM;
547
548 return 0;
549}
550
551static int finish_update_nop(struct dyn_ftrace *rec)
552{
553 unsigned long ip = rec->ip;
554 unsigned const char *new;
555
556 new = ftrace_nop_replace();
557
558 if (ftrace_write(ip, new, 1))
559 return -EPERM;
560 return 0;
561}
562
563static int finish_update(struct dyn_ftrace *rec, int enable)
564{
565 unsigned long ftrace_addr;
566 int ret;
567
568 ret = ftrace_update_record(rec, enable);
569
570 ftrace_addr = (unsigned long)FTRACE_ADDR;
571
572 switch (ret) {
573 case FTRACE_UPDATE_IGNORE:
574 return 0;
575
576 case FTRACE_UPDATE_MAKE_CALL:
577 /* converting nop to call */
578 return finish_update_call(rec, ftrace_addr);
579
580 case FTRACE_UPDATE_MAKE_NOP:
581 /* converting a call to a nop */
582 return finish_update_nop(rec);
583 }
584
585 return 0;
586}
587
588static void do_sync_core(void *data)
589{
590 sync_core();
591}
592
593static void run_sync(void)
594{
595 int enable_irqs = irqs_disabled();
596
597 /* We may be called with interrupts disbled (on bootup). */
598 if (enable_irqs)
599 local_irq_enable();
600 on_each_cpu(do_sync_core, NULL, 1);
601 if (enable_irqs)
602 local_irq_disable();
603}
604
605static void ftrace_replace_code(int enable)
606{
607 struct ftrace_rec_iter *iter;
608 struct dyn_ftrace *rec;
609 const char *report = "adding breakpoints";
610 int count = 0;
611 int ret;
612
613 for_ftrace_rec_iter(iter) {
614 rec = ftrace_rec_iter_record(iter);
615
616 ret = add_breakpoints(rec, enable);
617 if (ret)
618 goto remove_breakpoints;
619 count++;
620 }
621
622 run_sync();
623
624 report = "updating code";
625
626 for_ftrace_rec_iter(iter) {
627 rec = ftrace_rec_iter_record(iter);
628
629 ret = add_update(rec, enable);
630 if (ret)
631 goto remove_breakpoints;
632 }
633
634 run_sync();
635
636 report = "removing breakpoints";
637
638 for_ftrace_rec_iter(iter) {
639 rec = ftrace_rec_iter_record(iter);
640
641 ret = finish_update(rec, enable);
642 if (ret)
643 goto remove_breakpoints;
644 }
645
646 run_sync();
647
648 return;
649
650 remove_breakpoints:
651 ftrace_bug(ret, rec ? rec->ip : 0);
652 printk(KERN_WARNING "Failed on %s (%d):\n", report, count);
653 for_ftrace_rec_iter(iter) {
654 rec = ftrace_rec_iter_record(iter);
655 remove_breakpoint(rec);
656 }
657}
658
659void arch_ftrace_update_code(int command)
660{
661 modifying_ftrace_code++;
662
663 if (command & FTRACE_UPDATE_CALLS)
664 ftrace_replace_code(1);
665 else if (command & FTRACE_DISABLE_CALLS)
666 ftrace_replace_code(0);
667
668 if (command & FTRACE_UPDATE_TRACE_FUNC)
669 ftrace_update_ftrace_func(ftrace_trace_function);
670
671 if (command & FTRACE_START_FUNC_RET)
672 ftrace_enable_ftrace_graph_caller();
673 else if (command & FTRACE_STOP_FUNC_RET)
674 ftrace_disable_ftrace_graph_caller();
675
676 modifying_ftrace_code--;
677}
678
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200679int __init ftrace_dyn_arch_init(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +0200680{
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400681 /* The return code is retured via data */
682 *(unsigned long *)data = 0;
Steven Rostedtdfa60ab2008-05-12 21:20:43 +0200683
Steven Rostedt3d083392008-05-12 21:20:42 +0200684 return 0;
685}
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +0100686#endif
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100687
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100688#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100689
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500690#ifdef CONFIG_DYNAMIC_FTRACE
691extern void ftrace_graph_call(void);
692
693static int ftrace_mod_jmp(unsigned long ip,
694 int old_offset, int new_offset)
695{
696 unsigned char code[MCOUNT_INSN_SIZE];
697
698 if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE))
699 return -EFAULT;
700
701 if (code[0] != 0xe9 || old_offset != *(int *)(&code[1]))
702 return -EINVAL;
703
704 *(int *)(&code[1]) = new_offset;
705
706 if (do_ftrace_mod_code(ip, &code))
707 return -EPERM;
708
709 return 0;
710}
711
712int ftrace_enable_ftrace_graph_caller(void)
713{
714 unsigned long ip = (unsigned long)(&ftrace_graph_call);
715 int old_offset, new_offset;
716
717 old_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
718 new_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
719
720 return ftrace_mod_jmp(ip, old_offset, new_offset);
721}
722
723int ftrace_disable_ftrace_graph_caller(void)
724{
725 unsigned long ip = (unsigned long)(&ftrace_graph_call);
726 int old_offset, new_offset;
727
728 old_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
729 new_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
730
731 return ftrace_mod_jmp(ip, old_offset, new_offset);
732}
733
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100734#endif /* !CONFIG_DYNAMIC_FTRACE */
735
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100736/*
737 * Hook the return address and push it in the stack of return addrs
738 * in current thread info.
739 */
Steven Rostedt71e308a2009-06-18 12:45:08 -0400740void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
741 unsigned long frame_pointer)
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100742{
743 unsigned long old;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100744 int faulted;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100745 struct ftrace_graph_ent trace;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100746 unsigned long return_hooker = (unsigned long)
747 &return_to_handler;
748
Frederic Weisbecker380c4b12008-12-06 03:43:41 +0100749 if (unlikely(atomic_read(&current->tracing_graph_pause)))
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100750 return;
751
752 /*
753 * Protect against fault, even if it shouldn't
754 * happen. This tool is too much intrusive to
755 * ignore such a protection.
756 */
757 asm volatile(
Steven Rostedt96665782009-02-10 11:53:23 -0500758 "1: " _ASM_MOV " (%[parent]), %[old]\n"
759 "2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100760 " movl $0, %[faulted]\n"
Steven Rostedte3944bf2009-02-10 13:07:13 -0500761 "3:\n"
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100762
763 ".section .fixup, \"ax\"\n"
Steven Rostedte3944bf2009-02-10 13:07:13 -0500764 "4: movl $1, %[faulted]\n"
765 " jmp 3b\n"
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100766 ".previous\n"
767
Steven Rostedte3944bf2009-02-10 13:07:13 -0500768 _ASM_EXTABLE(1b, 4b)
769 _ASM_EXTABLE(2b, 4b)
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100770
Steven Rostedtaa512a22009-05-13 13:52:19 -0400771 : [old] "=&r" (old), [faulted] "=r" (faulted)
Steven Rostedt96665782009-02-10 11:53:23 -0500772 : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100773 : "memory"
774 );
775
Steven Rostedt14a866c2008-12-02 23:50:02 -0500776 if (unlikely(faulted)) {
777 ftrace_graph_stop();
778 WARN_ON(1);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100779 return;
780 }
781
Steven Rostedt722b3c72011-02-11 20:36:02 -0500782 trace.func = self_addr;
783 trace.depth = current->curr_ret_stack + 1;
784
785 /* Only trace if the calling function expects to */
786 if (!ftrace_graph_entry(&trace)) {
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100787 *parent = old;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100788 return;
789 }
790
Steven Rostedt722b3c72011-02-11 20:36:02 -0500791 if (ftrace_push_return_trace(old, self_addr, &trace.depth,
792 frame_pointer) == -EBUSY) {
Steven Rostedte49dc192008-12-02 23:50:05 -0500793 *parent = old;
Steven Rostedt722b3c72011-02-11 20:36:02 -0500794 return;
Steven Rostedte49dc192008-12-02 23:50:05 -0500795 }
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100796}
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100797#endif /* CONFIG_FUNCTION_GRAPH_TRACER */