]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/x86/kernel/ftrace.c
ftrace: use probe_kernel
[linux-2.6-omap-h63xx.git] / arch / x86 / kernel / ftrace.c
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>
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>
19
20 #include <asm/ftrace.h>
21 #include <asm/nops.h>
22
23
24 /* Long is fine, even if it is only 4 bytes ;-) */
25 static unsigned long *ftrace_nop;
26
27 union ftrace_code_union {
28         char code[MCOUNT_INSN_SIZE];
29         struct {
30                 char e8;
31                 int offset;
32         } __attribute__((packed));
33 };
34
35
36 static int notrace ftrace_calc_offset(long ip, long addr)
37 {
38         return (int)(addr - ip);
39 }
40
41 notrace unsigned char *ftrace_nop_replace(void)
42 {
43         return (char *)ftrace_nop;
44 }
45
46 notrace unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
47 {
48         static union ftrace_code_union calc;
49
50         calc.e8         = 0xe8;
51         calc.offset     = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
52
53         /*
54          * No locking needed, this must be called via kstop_machine
55          * which in essence is like running on a uniprocessor machine.
56          */
57         return calc.code;
58 }
59
60 notrace int
61 ftrace_modify_code(unsigned long ip, unsigned char *old_code,
62                    unsigned char *new_code)
63 {
64         unsigned char replaced[MCOUNT_INSN_SIZE];
65
66         /*
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. We do this by using the
70          *  probe_kernel_* functions.
71          *
72          * No real locking needed, this code is run through
73          * kstop_machine, or before SMP starts.
74          */
75
76         /* read the text we want to modify */
77         if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
78                 return -EFAULT;
79
80         /* Make sure it is what we expect it to be */
81         if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
82                 return -EINVAL;
83
84         /* replace the text with the new text */
85         if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE))
86                 return -EPERM;
87
88         sync_core();
89
90         return 0;
91 }
92
93 notrace int ftrace_update_ftrace_func(ftrace_func_t func)
94 {
95         unsigned long ip = (unsigned long)(&ftrace_call);
96         unsigned char old[MCOUNT_INSN_SIZE], *new;
97         int ret;
98
99         memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
100         new = ftrace_call_replace(ip, (unsigned long)func);
101         ret = ftrace_modify_code(ip, old, new);
102
103         return ret;
104 }
105
106 notrace int ftrace_mcount_set(unsigned long *data)
107 {
108         /* mcount is initialized as a nop */
109         *data = 0;
110         return 0;
111 }
112
113 int __init ftrace_dyn_arch_init(void *data)
114 {
115         extern const unsigned char ftrace_test_p6nop[];
116         extern const unsigned char ftrace_test_nop5[];
117         extern const unsigned char ftrace_test_jmp[];
118         int faulted = 0;
119
120         /*
121          * There is no good nop for all x86 archs.
122          * We will default to using the P6_NOP5, but first we
123          * will test to make sure that the nop will actually
124          * work on this CPU. If it faults, we will then
125          * go to a lesser efficient 5 byte nop. If that fails
126          * we then just use a jmp as our nop. This isn't the most
127          * efficient nop, but we can not use a multi part nop
128          * since we would then risk being preempted in the middle
129          * of that nop, and if we enabled tracing then, it might
130          * cause a system crash.
131          *
132          * TODO: check the cpuid to determine the best nop.
133          */
134         asm volatile (
135                 "jmp ftrace_test_jmp\n"
136                 /* This code needs to stay around */
137                 ".section .text, \"ax\"\n"
138                 "ftrace_test_jmp:"
139                 "jmp ftrace_test_p6nop\n"
140                 "nop\n"
141                 "nop\n"
142                 "nop\n"  /* 2 byte jmp + 3 bytes */
143                 "ftrace_test_p6nop:"
144                 P6_NOP5
145                 "jmp 1f\n"
146                 "ftrace_test_nop5:"
147                 ".byte 0x66,0x66,0x66,0x66,0x90\n"
148                 "jmp 1f\n"
149                 ".previous\n"
150                 "1:"
151                 ".section .fixup, \"ax\"\n"
152                 "2:     movl $1, %0\n"
153                 "       jmp ftrace_test_nop5\n"
154                 "3:     movl $2, %0\n"
155                 "       jmp 1b\n"
156                 ".previous\n"
157                 _ASM_EXTABLE(ftrace_test_p6nop, 2b)
158                 _ASM_EXTABLE(ftrace_test_nop5, 3b)
159                 : "=r"(faulted) : "0" (faulted));
160
161         switch (faulted) {
162         case 0:
163                 pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
164                 ftrace_nop = (unsigned long *)ftrace_test_p6nop;
165                 break;
166         case 1:
167                 pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
168                 ftrace_nop = (unsigned long *)ftrace_test_nop5;
169                 break;
170         case 2:
171                 pr_info("ftrace: converting mcount calls to jmp . + 5\n");
172                 ftrace_nop = (unsigned long *)ftrace_test_jmp;
173                 break;
174         }
175
176         /* The return code is retured via data */
177         *(unsigned long *)data = 0;
178
179         return 0;
180 }