blob: b1e5e2244eca4c46f0fb1c8b68efdea8b7338e1f [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 Rostedt15adc042008-10-23 09:33:08 -040036static int ftrace_calc_offset(long ip, long addr)
Steven Rostedt3c1720f2008-05-12 21:20:43 +020037{
38 return (int)(addr - ip);
39}
40
Steven Rostedt15adc042008-10-23 09:33:08 -040041unsigned char *ftrace_nop_replace(void)
Steven Rostedt3c1720f2008-05-12 21:20:43 +020042{
43 return (char *)ftrace_nop;
44}
45
Steven Rostedt15adc042008-10-23 09:33:08 -040046unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
Steven Rostedt3c1720f2008-05-12 21:20:43 +020047{
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
Steven Rostedt15adc042008-10-23 09:33:08 -040060int
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
Steven Rostedt76aefee2008-10-23 09:33:00 -040069 * as well as code changing. We do this by using the
Steven Rostedtab9a0912008-10-23 09:33:01 -040070 * probe_kernel_* functions.
Steven Rostedt3d083392008-05-12 21:20:42 +020071 *
72 * No real locking needed, this code is run through
Steven Rostedt6f93fc02008-08-20 12:55:07 -040073 * kstop_machine, or before SMP starts.
Steven Rostedt3d083392008-05-12 21:20:42 +020074 */
Steven Rostedt76aefee2008-10-23 09:33:00 -040075
76 /* read the text we want to modify */
Steven Rostedtab9a0912008-10-23 09:33:01 -040077 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt593eb8a2008-10-23 09:32:59 -040078 return -EFAULT;
Steven Rostedt6f93fc02008-08-20 12:55:07 -040079
Steven Rostedt76aefee2008-10-23 09:33:00 -040080 /* Make sure it is what we expect it to be */
Steven Rostedt6f93fc02008-08-20 12:55:07 -040081 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
Steven Rostedt593eb8a2008-10-23 09:32:59 -040082 return -EINVAL;
Steven Rostedt6f93fc02008-08-20 12:55:07 -040083
Steven Rostedt76aefee2008-10-23 09:33:00 -040084 /* replace the text with the new text */
Steven Rostedtab9a0912008-10-23 09:33:01 -040085 if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE))
Steven Rostedt593eb8a2008-10-23 09:32:59 -040086 return -EPERM;
Steven Rostedt6f93fc02008-08-20 12:55:07 -040087
Steven Rostedt3d083392008-05-12 21:20:42 +020088 sync_core();
89
Steven Rostedt6f93fc02008-08-20 12:55:07 -040090 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +020091}
92
Steven Rostedt15adc042008-10-23 09:33:08 -040093int ftrace_update_ftrace_func(ftrace_func_t func)
Steven Rostedtd61f82d2008-05-12 21:20:43 +020094{
95 unsigned long ip = (unsigned long)(&ftrace_call);
Abhishek Sagar395a59d2008-06-21 23:47:27 +053096 unsigned char old[MCOUNT_INSN_SIZE], *new;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020097 int ret;
98
Abhishek Sagar395a59d2008-06-21 23:47:27 +053099 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200100 new = ftrace_call_replace(ip, (unsigned long)func);
101 ret = ftrace_modify_code(ip, old, new);
102
103 return ret;
104}
105
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200106int __init ftrace_dyn_arch_init(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +0200107{
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400108 extern const unsigned char ftrace_test_p6nop[];
109 extern const unsigned char ftrace_test_nop5[];
110 extern const unsigned char ftrace_test_jmp[];
111 int faulted = 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200112
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400113 /*
114 * There is no good nop for all x86 archs.
115 * We will default to using the P6_NOP5, but first we
116 * will test to make sure that the nop will actually
117 * work on this CPU. If it faults, we will then
118 * go to a lesser efficient 5 byte nop. If that fails
119 * we then just use a jmp as our nop. This isn't the most
120 * efficient nop, but we can not use a multi part nop
121 * since we would then risk being preempted in the middle
122 * of that nop, and if we enabled tracing then, it might
123 * cause a system crash.
124 *
125 * TODO: check the cpuid to determine the best nop.
126 */
127 asm volatile (
128 "jmp ftrace_test_jmp\n"
129 /* This code needs to stay around */
130 ".section .text, \"ax\"\n"
131 "ftrace_test_jmp:"
132 "jmp ftrace_test_p6nop\n"
Anders Kaseorg8b273862008-10-09 22:19:08 -0400133 "nop\n"
134 "nop\n"
135 "nop\n" /* 2 byte jmp + 3 bytes */
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400136 "ftrace_test_p6nop:"
137 P6_NOP5
138 "jmp 1f\n"
139 "ftrace_test_nop5:"
140 ".byte 0x66,0x66,0x66,0x66,0x90\n"
141 "jmp 1f\n"
142 ".previous\n"
143 "1:"
144 ".section .fixup, \"ax\"\n"
145 "2: movl $1, %0\n"
146 " jmp ftrace_test_nop5\n"
147 "3: movl $2, %0\n"
148 " jmp 1b\n"
149 ".previous\n"
150 _ASM_EXTABLE(ftrace_test_p6nop, 2b)
151 _ASM_EXTABLE(ftrace_test_nop5, 3b)
152 : "=r"(faulted) : "0" (faulted));
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200153
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400154 switch (faulted) {
155 case 0:
156 pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
157 ftrace_nop = (unsigned long *)ftrace_test_p6nop;
158 break;
159 case 1:
160 pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
161 ftrace_nop = (unsigned long *)ftrace_test_nop5;
162 break;
163 case 2:
Anders Kaseorg8b273862008-10-09 22:19:08 -0400164 pr_info("ftrace: converting mcount calls to jmp . + 5\n");
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400165 ftrace_nop = (unsigned long *)ftrace_test_jmp;
166 break;
167 }
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200168
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400169 /* The return code is retured via data */
170 *(unsigned long *)data = 0;
Steven Rostedtdfa60ab2008-05-12 21:20:43 +0200171
Steven Rostedt3d083392008-05-12 21:20:42 +0200172 return 0;
173}