blob: 50ea0ac8c9bf2c27a53323b93b5d473bd7e1d028 [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 Rostedt8115f3f2008-10-24 09:12:17 -040024static unsigned char ftrace_nop[MCOUNT_INSN_SIZE];
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 Rostedt15adc042008-10-23 09:33:08 -040035static int ftrace_calc_offset(long ip, long addr)
Steven Rostedt3c1720f2008-05-12 21:20:43 +020036{
37 return (int)(addr - ip);
38}
39
Steven Rostedt15adc042008-10-23 09:33:08 -040040unsigned char *ftrace_nop_replace(void)
Steven Rostedt3c1720f2008-05-12 21:20:43 +020041{
Steven Rostedt8115f3f2008-10-24 09:12:17 -040042 return ftrace_nop;
Steven Rostedt3c1720f2008-05-12 21:20:43 +020043}
44
Steven Rostedt15adc042008-10-23 09:33:08 -040045unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
Steven Rostedt3c1720f2008-05-12 21:20:43 +020046{
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
Steven Rostedt15adc042008-10-23 09:33:08 -040059int
Steven Rostedt3d083392008-05-12 21:20:42 +020060ftrace_modify_code(unsigned long ip, unsigned char *old_code,
61 unsigned char *new_code)
62{
Steven Rostedt6f93fc02008-08-20 12:55:07 -040063 unsigned char replaced[MCOUNT_INSN_SIZE];
Steven Rostedt3d083392008-05-12 21:20:42 +020064
65 /*
66 * Note: Due to modules and __init, code can
67 * disappear and change, we need to protect against faulting
Steven Rostedt76aefee2008-10-23 09:33:00 -040068 * as well as code changing. We do this by using the
Steven Rostedtab9a0912008-10-23 09:33:01 -040069 * probe_kernel_* functions.
Steven Rostedt3d083392008-05-12 21:20:42 +020070 *
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 */
Steven Rostedt76aefee2008-10-23 09:33:00 -040074
75 /* read the text we want to modify */
Steven Rostedtab9a0912008-10-23 09:33:01 -040076 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt593eb8a2008-10-23 09:32:59 -040077 return -EFAULT;
Steven Rostedt6f93fc02008-08-20 12:55:07 -040078
Steven Rostedt76aefee2008-10-23 09:33:00 -040079 /* Make sure it is what we expect it to be */
Steven Rostedt6f93fc02008-08-20 12:55:07 -040080 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
Steven Rostedt593eb8a2008-10-23 09:32:59 -040081 return -EINVAL;
Steven Rostedt6f93fc02008-08-20 12:55:07 -040082
Steven Rostedt76aefee2008-10-23 09:33:00 -040083 /* replace the text with the new text */
Steven Rostedtab9a0912008-10-23 09:33:01 -040084 if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE))
Steven Rostedt593eb8a2008-10-23 09:32:59 -040085 return -EPERM;
Steven Rostedt6f93fc02008-08-20 12:55:07 -040086
Steven Rostedt3d083392008-05-12 21:20:42 +020087 sync_core();
88
Steven Rostedt6f93fc02008-08-20 12:55:07 -040089 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +020090}
91
Steven Rostedt15adc042008-10-23 09:33:08 -040092int ftrace_update_ftrace_func(ftrace_func_t func)
Steven Rostedtd61f82d2008-05-12 21:20:43 +020093{
94 unsigned long ip = (unsigned long)(&ftrace_call);
Abhishek Sagar395a59d2008-06-21 23:47:27 +053095 unsigned char old[MCOUNT_INSN_SIZE], *new;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020096 int ret;
97
Abhishek Sagar395a59d2008-06-21 23:47:27 +053098 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
Steven Rostedtd61f82d2008-05-12 21:20:43 +020099 new = ftrace_call_replace(ip, (unsigned long)func);
100 ret = ftrace_modify_code(ip, old, new);
101
102 return ret;
103}
104
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200105int __init ftrace_dyn_arch_init(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +0200106{
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400107 extern const unsigned char ftrace_test_p6nop[];
108 extern const unsigned char ftrace_test_nop5[];
109 extern const unsigned char ftrace_test_jmp[];
110 int faulted = 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200111
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400112 /*
113 * There is no good nop for all x86 archs.
114 * We will default to using the P6_NOP5, but first we
115 * will test to make sure that the nop will actually
116 * work on this CPU. If it faults, we will then
117 * go to a lesser efficient 5 byte nop. If that fails
118 * we then just use a jmp as our nop. This isn't the most
119 * efficient nop, but we can not use a multi part nop
120 * since we would then risk being preempted in the middle
121 * of that nop, and if we enabled tracing then, it might
122 * cause a system crash.
123 *
124 * TODO: check the cpuid to determine the best nop.
125 */
126 asm volatile (
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400127 "ftrace_test_jmp:"
128 "jmp ftrace_test_p6nop\n"
Anders Kaseorg8b273862008-10-09 22:19:08 -0400129 "nop\n"
130 "nop\n"
131 "nop\n" /* 2 byte jmp + 3 bytes */
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400132 "ftrace_test_p6nop:"
133 P6_NOP5
134 "jmp 1f\n"
135 "ftrace_test_nop5:"
136 ".byte 0x66,0x66,0x66,0x66,0x90\n"
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400137 "1:"
138 ".section .fixup, \"ax\"\n"
139 "2: movl $1, %0\n"
140 " jmp ftrace_test_nop5\n"
141 "3: movl $2, %0\n"
142 " jmp 1b\n"
143 ".previous\n"
144 _ASM_EXTABLE(ftrace_test_p6nop, 2b)
145 _ASM_EXTABLE(ftrace_test_nop5, 3b)
146 : "=r"(faulted) : "0" (faulted));
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200147
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400148 switch (faulted) {
149 case 0:
150 pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
Steven Rostedt8115f3f2008-10-24 09:12:17 -0400151 memcpy(ftrace_nop, ftrace_test_p6nop, MCOUNT_INSN_SIZE);
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400152 break;
153 case 1:
154 pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
Steven Rostedt8115f3f2008-10-24 09:12:17 -0400155 memcpy(ftrace_nop, ftrace_test_nop5, MCOUNT_INSN_SIZE);
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400156 break;
157 case 2:
Anders Kaseorg8b273862008-10-09 22:19:08 -0400158 pr_info("ftrace: converting mcount calls to jmp . + 5\n");
Steven Rostedt8115f3f2008-10-24 09:12:17 -0400159 memcpy(ftrace_nop, ftrace_test_jmp, MCOUNT_INSN_SIZE);
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400160 break;
161 }
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200162
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400163 /* The return code is retured via data */
164 *(unsigned long *)data = 0;
Steven Rostedtdfa60ab2008-05-12 21:20:43 +0200165
Steven Rostedt3d083392008-05-12 21:20:42 +0200166 return 0;
167}