blob: 4151c91254e834a23c83f2da769412cd855b0a1b [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>
14#include <linux/ftrace.h>
15#include <linux/percpu.h>
16#include <linux/init.h>
17#include <linux/list.h>
18
Abhishek Sagar395a59d2008-06-21 23:47:27 +053019#include <asm/ftrace.h>
Steven Rostedt732f3ca2008-08-14 18:05:05 -040020#include <asm/nops.h>
Steven Rostedtdfa60ab2008-05-12 21:20:43 +020021
Steven Rostedt3d083392008-05-12 21:20:42 +020022
Steven Rostedtdfa60ab2008-05-12 21:20:43 +020023/* Long is fine, even if it is only 4 bytes ;-) */
24static long *ftrace_nop;
Steven Rostedt3d083392008-05-12 21:20:42 +020025
Steven Rostedt3d083392008-05-12 21:20:42 +020026union ftrace_code_union {
Abhishek Sagar395a59d2008-06-21 23:47:27 +053027 char code[MCOUNT_INSN_SIZE];
Steven Rostedt3d083392008-05-12 21:20:42 +020028 struct {
29 char e8;
30 int offset;
31 } __attribute__((packed));
32};
33
Abhishek Sagar395a59d2008-06-21 23:47:27 +053034
Steven Rostedt3c1720f2008-05-12 21:20:43 +020035static int notrace ftrace_calc_offset(long ip, long addr)
36{
37 return (int)(addr - ip);
38}
39
40notrace unsigned char *ftrace_nop_replace(void)
41{
42 return (char *)ftrace_nop;
43}
44
45notrace unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
46{
47 static union ftrace_code_union calc;
48
49 calc.e8 = 0xe8;
Abhishek Sagar395a59d2008-06-21 23:47:27 +053050 calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
Steven Rostedt3c1720f2008-05-12 21:20:43 +020051
52 /*
53 * No locking needed, this must be called via kstop_machine
54 * which in essence is like running on a uniprocessor machine.
55 */
56 return calc.code;
57}
58
59notrace int
Steven Rostedt3d083392008-05-12 21:20:42 +020060ftrace_modify_code(unsigned long ip, unsigned char *old_code,
61 unsigned char *new_code)
62{
Steven Rostedtdfa60ab2008-05-12 21:20:43 +020063 unsigned replaced;
64 unsigned old = *(unsigned *)old_code; /* 4 bytes */
65 unsigned new = *(unsigned *)new_code; /* 4 bytes */
66 unsigned char newch = new_code[4];
Steven Rostedt3d083392008-05-12 21:20:42 +020067 int faulted = 0;
68
69 /*
70 * Note: Due to modules and __init, code can
71 * disappear and change, we need to protect against faulting
72 * as well as code changing.
73 *
74 * No real locking needed, this code is run through
75 * kstop_machine.
76 */
77 asm volatile (
78 "1: lock\n"
Steven Rostedtdfa60ab2008-05-12 21:20:43 +020079 " cmpxchg %3, (%2)\n"
80 " jnz 2f\n"
81 " movb %b4, 4(%2)\n"
Steven Rostedt3d083392008-05-12 21:20:42 +020082 "2:\n"
83 ".section .fixup, \"ax\"\n"
Steven Rostedta56be3f2008-05-12 21:20:56 +020084 "3: movl $1, %0\n"
85 " jmp 2b\n"
Steven Rostedt3d083392008-05-12 21:20:42 +020086 ".previous\n"
87 _ASM_EXTABLE(1b, 3b)
88 : "=r"(faulted), "=a"(replaced)
Ingo Molnaree4311a2008-06-17 17:43:02 +020089 : "r"(ip), "r"(new), "c"(newch),
Steven Rostedtdfa60ab2008-05-12 21:20:43 +020090 "0"(faulted), "a"(old)
Steven Rostedt3d083392008-05-12 21:20:42 +020091 : "memory");
92 sync_core();
93
Steven Rostedtdfa60ab2008-05-12 21:20:43 +020094 if (replaced != old && replaced != new)
Steven Rostedt3d083392008-05-12 21:20:42 +020095 faulted = 2;
96
97 return faulted;
98}
99
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200100notrace int ftrace_update_ftrace_func(ftrace_func_t func)
101{
102 unsigned long ip = (unsigned long)(&ftrace_call);
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530103 unsigned char old[MCOUNT_INSN_SIZE], *new;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200104 int ret;
105
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530106 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200107 new = ftrace_call_replace(ip, (unsigned long)func);
108 ret = ftrace_modify_code(ip, old, new);
109
110 return ret;
111}
112
113notrace int ftrace_mcount_set(unsigned long *data)
114{
Steven Rostedt0a376052008-08-14 15:45:12 -0400115 /* mcount is initialized as a nop */
116 *data = 0;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200117 return 0;
118}
119
120int __init ftrace_dyn_arch_init(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +0200121{
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400122 extern const unsigned char ftrace_test_p6nop[];
123 extern const unsigned char ftrace_test_nop5[];
124 extern const unsigned char ftrace_test_jmp[];
125 int faulted = 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200126
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400127 /*
128 * There is no good nop for all x86 archs.
129 * We will default to using the P6_NOP5, but first we
130 * will test to make sure that the nop will actually
131 * work on this CPU. If it faults, we will then
132 * go to a lesser efficient 5 byte nop. If that fails
133 * we then just use a jmp as our nop. This isn't the most
134 * efficient nop, but we can not use a multi part nop
135 * since we would then risk being preempted in the middle
136 * of that nop, and if we enabled tracing then, it might
137 * cause a system crash.
138 *
139 * TODO: check the cpuid to determine the best nop.
140 */
141 asm volatile (
142 "jmp ftrace_test_jmp\n"
143 /* This code needs to stay around */
144 ".section .text, \"ax\"\n"
145 "ftrace_test_jmp:"
146 "jmp ftrace_test_p6nop\n"
147 ".byte 0x00,0x00,0x00\n" /* 2 byte jmp + 3 bytes */
148 "ftrace_test_p6nop:"
149 P6_NOP5
150 "jmp 1f\n"
151 "ftrace_test_nop5:"
152 ".byte 0x66,0x66,0x66,0x66,0x90\n"
153 "jmp 1f\n"
154 ".previous\n"
155 "1:"
156 ".section .fixup, \"ax\"\n"
157 "2: movl $1, %0\n"
158 " jmp ftrace_test_nop5\n"
159 "3: movl $2, %0\n"
160 " jmp 1b\n"
161 ".previous\n"
162 _ASM_EXTABLE(ftrace_test_p6nop, 2b)
163 _ASM_EXTABLE(ftrace_test_nop5, 3b)
164 : "=r"(faulted) : "0" (faulted));
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200165
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400166 switch (faulted) {
167 case 0:
168 pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
169 ftrace_nop = (unsigned long *)ftrace_test_p6nop;
170 break;
171 case 1:
172 pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
173 ftrace_nop = (unsigned long *)ftrace_test_nop5;
174 break;
175 case 2:
176 pr_info("ftrace: converting mcount calls to jmp 1f\n");
177 ftrace_nop = (unsigned long *)ftrace_test_jmp;
178 break;
179 }
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200180
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400181 /* The return code is retured via data */
182 *(unsigned long *)data = 0;
Steven Rostedtdfa60ab2008-05-12 21:20:43 +0200183
Steven Rostedt3d083392008-05-12 21:20:42 +0200184 return 0;
185}