2 * Code for replacing ftrace calls with jumps.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
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.
12 #include <linux/spinlock.h>
13 #include <linux/hardirq.h>
14 #include <linux/uaccess.h>
15 #include <linux/ftrace.h>
16 #include <linux/percpu.h>
17 #include <linux/init.h>
18 #include <linux/list.h>
20 #include <asm/ftrace.h>
24 /* Long is fine, even if it is only 4 bytes ;-) */
25 static unsigned long *ftrace_nop;
27 union ftrace_code_union {
28 char code[MCOUNT_INSN_SIZE];
32 } __attribute__((packed));
36 static int notrace ftrace_calc_offset(long ip, long addr)
38 return (int)(addr - ip);
41 notrace unsigned char *ftrace_nop_replace(void)
43 return (char *)ftrace_nop;
46 notrace unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
48 static union ftrace_code_union calc;
51 calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
54 * No locking needed, this must be called via kstop_machine
55 * which in essence is like running on a uniprocessor machine.
61 ftrace_modify_code(unsigned long ip, unsigned char *old_code,
62 unsigned char *new_code)
64 unsigned char replaced[MCOUNT_INSN_SIZE];
67 * Note: Due to modules and __init, code can
68 * disappear and change, we need to protect against faulting
69 * as well as code changing.
71 * No real locking needed, this code is run through
72 * kstop_machine, or before SMP starts.
74 if (__copy_from_user_inatomic(replaced, (char __user *)ip, MCOUNT_INSN_SIZE))
77 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
80 WARN_ON_ONCE(__copy_to_user_inatomic((char __user *)ip, new_code,
88 notrace int ftrace_update_ftrace_func(ftrace_func_t func)
90 unsigned long ip = (unsigned long)(&ftrace_call);
91 unsigned char old[MCOUNT_INSN_SIZE], *new;
94 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
95 new = ftrace_call_replace(ip, (unsigned long)func);
96 ret = ftrace_modify_code(ip, old, new);
101 notrace int ftrace_mcount_set(unsigned long *data)
103 /* mcount is initialized as a nop */
108 int __init ftrace_dyn_arch_init(void *data)
110 extern const unsigned char ftrace_test_p6nop[];
111 extern const unsigned char ftrace_test_nop5[];
112 extern const unsigned char ftrace_test_jmp[];
116 * There is no good nop for all x86 archs.
117 * We will default to using the P6_NOP5, but first we
118 * will test to make sure that the nop will actually
119 * work on this CPU. If it faults, we will then
120 * go to a lesser efficient 5 byte nop. If that fails
121 * we then just use a jmp as our nop. This isn't the most
122 * efficient nop, but we can not use a multi part nop
123 * since we would then risk being preempted in the middle
124 * of that nop, and if we enabled tracing then, it might
125 * cause a system crash.
127 * TODO: check the cpuid to determine the best nop.
130 "jmp ftrace_test_jmp\n"
131 /* This code needs to stay around */
132 ".section .text, \"ax\"\n"
134 "jmp ftrace_test_p6nop\n"
137 "nop\n" /* 2 byte jmp + 3 bytes */
142 ".byte 0x66,0x66,0x66,0x66,0x90\n"
146 ".section .fixup, \"ax\"\n"
148 " jmp ftrace_test_nop5\n"
152 _ASM_EXTABLE(ftrace_test_p6nop, 2b)
153 _ASM_EXTABLE(ftrace_test_nop5, 3b)
154 : "=r"(faulted) : "0" (faulted));
158 pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
159 ftrace_nop = (unsigned long *)ftrace_test_p6nop;
162 pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
163 ftrace_nop = (unsigned long *)ftrace_test_nop5;
166 pr_info("ftrace: converting mcount calls to jmp . + 5\n");
167 ftrace_nop = (unsigned long *)ftrace_test_jmp;
171 /* The return code is retured via data */
172 *(unsigned long *)data = 0;