blob: d073d981a730306f970aabdf5f7174453c24d226 [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>
17#include <linux/init.h>
18#include <linux/list.h>
19
Abhishek Sagar395a59d2008-06-21 23:47:27 +053020#include <asm/ftrace.h>
Steven Rostedt732f3ca2008-08-14 18:05:05 -040021#include <asm/nops.h>
Steven Rostedtdfa60ab2008-05-12 21:20:43 +020022
Steven Rostedt3d083392008-05-12 21:20:42 +020023
Steven Rostedtdfa60ab2008-05-12 21:20:43 +020024/* Long is fine, even if it is only 4 bytes ;-) */
Harvey Harrison37a52f52008-09-23 15:05:46 -070025static unsigned long *ftrace_nop;
Steven Rostedt3d083392008-05-12 21:20:42 +020026
Steven Rostedt3d083392008-05-12 21:20:42 +020027union ftrace_code_union {
Abhishek Sagar395a59d2008-06-21 23:47:27 +053028 char code[MCOUNT_INSN_SIZE];
Steven Rostedt3d083392008-05-12 21:20:42 +020029 struct {
30 char e8;
31 int offset;
32 } __attribute__((packed));
33};
34
Abhishek Sagar395a59d2008-06-21 23:47:27 +053035
Steven Rostedt3c1720f2008-05-12 21:20:43 +020036static int notrace ftrace_calc_offset(long ip, long addr)
37{
38 return (int)(addr - ip);
39}
40
41notrace unsigned char *ftrace_nop_replace(void)
42{
43 return (char *)ftrace_nop;
44}
45
46notrace unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
47{
48 static union ftrace_code_union calc;
49
50 calc.e8 = 0xe8;
Abhishek Sagar395a59d2008-06-21 23:47:27 +053051 calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
Steven Rostedt3c1720f2008-05-12 21:20:43 +020052
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
60notrace int
Steven Rostedt3d083392008-05-12 21:20:42 +020061ftrace_modify_code(unsigned long ip, unsigned char *old_code,
62 unsigned char *new_code)
63{
Steven Rostedt6f93fc02008-08-20 12:55:07 -040064 unsigned char replaced[MCOUNT_INSN_SIZE];
Steven Rostedt3d083392008-05-12 21:20:42 +020065
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.
70 *
71 * No real locking needed, this code is run through
Steven Rostedt6f93fc02008-08-20 12:55:07 -040072 * kstop_machine, or before SMP starts.
Steven Rostedt3d083392008-05-12 21:20:42 +020073 */
Frédéric Weisbeckerac2b86f2008-09-24 16:31:56 +010074 if (__copy_from_user_inatomic(replaced, (char __user *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt6f93fc02008-08-20 12:55:07 -040075 return 1;
76
77 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
78 return 2;
79
Frédéric Weisbeckerac2b86f2008-09-24 16:31:56 +010080 WARN_ON_ONCE(__copy_to_user_inatomic((char __user *)ip, new_code,
Steven Rostedt6f93fc02008-08-20 12:55:07 -040081 MCOUNT_INSN_SIZE));
82
Steven Rostedt3d083392008-05-12 21:20:42 +020083 sync_core();
84
Steven Rostedt6f93fc02008-08-20 12:55:07 -040085 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +020086}
87
Steven Rostedtd61f82d2008-05-12 21:20:43 +020088notrace int ftrace_update_ftrace_func(ftrace_func_t func)
89{
90 unsigned long ip = (unsigned long)(&ftrace_call);
Abhishek Sagar395a59d2008-06-21 23:47:27 +053091 unsigned char old[MCOUNT_INSN_SIZE], *new;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020092 int ret;
93
Abhishek Sagar395a59d2008-06-21 23:47:27 +053094 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
Steven Rostedtd61f82d2008-05-12 21:20:43 +020095 new = ftrace_call_replace(ip, (unsigned long)func);
96 ret = ftrace_modify_code(ip, old, new);
97
98 return ret;
99}
100
101notrace int ftrace_mcount_set(unsigned long *data)
102{
Steven Rostedt0a376052008-08-14 15:45:12 -0400103 /* mcount is initialized as a nop */
104 *data = 0;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200105 return 0;
106}
107
108int __init ftrace_dyn_arch_init(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +0200109{
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400110 extern const unsigned char ftrace_test_p6nop[];
111 extern const unsigned char ftrace_test_nop5[];
112 extern const unsigned char ftrace_test_jmp[];
113 int faulted = 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200114
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400115 /*
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.
126 *
127 * TODO: check the cpuid to determine the best nop.
128 */
129 asm volatile (
130 "jmp ftrace_test_jmp\n"
131 /* This code needs to stay around */
132 ".section .text, \"ax\"\n"
133 "ftrace_test_jmp:"
134 "jmp ftrace_test_p6nop\n"
Anders Kaseorg8b273862008-10-09 22:19:08 -0400135 "nop\n"
136 "nop\n"
137 "nop\n" /* 2 byte jmp + 3 bytes */
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400138 "ftrace_test_p6nop:"
139 P6_NOP5
140 "jmp 1f\n"
141 "ftrace_test_nop5:"
142 ".byte 0x66,0x66,0x66,0x66,0x90\n"
143 "jmp 1f\n"
144 ".previous\n"
145 "1:"
146 ".section .fixup, \"ax\"\n"
147 "2: movl $1, %0\n"
148 " jmp ftrace_test_nop5\n"
149 "3: movl $2, %0\n"
150 " jmp 1b\n"
151 ".previous\n"
152 _ASM_EXTABLE(ftrace_test_p6nop, 2b)
153 _ASM_EXTABLE(ftrace_test_nop5, 3b)
154 : "=r"(faulted) : "0" (faulted));
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200155
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400156 switch (faulted) {
157 case 0:
158 pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
159 ftrace_nop = (unsigned long *)ftrace_test_p6nop;
160 break;
161 case 1:
162 pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
163 ftrace_nop = (unsigned long *)ftrace_test_nop5;
164 break;
165 case 2:
Anders Kaseorg8b273862008-10-09 22:19:08 -0400166 pr_info("ftrace: converting mcount calls to jmp . + 5\n");
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400167 ftrace_nop = (unsigned long *)ftrace_test_jmp;
168 break;
169 }
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200170
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400171 /* The return code is retured via data */
172 *(unsigned long *)data = 0;
Steven Rostedtdfa60ab2008-05-12 21:20:43 +0200173
Steven Rostedt3d083392008-05-12 21:20:42 +0200174 return 0;
175}