blob: 971ac8c36ea775ca58702cb0f17c18aba7b1c4a7 [file] [log] [blame]
Abhishek Sagar014c2572008-05-31 14:23:50 +05301/*
2 * Dynamic function tracing support.
3 *
4 * Copyright (C) 2008 Abhishek Sagar <sagar.abhishek@gmail.com>
Rabin Vincent3b6c2232010-08-10 19:43:28 +01005 * Copyright (C) 2010 Rabin Vincent <rabin@rab.in>
Abhishek Sagar014c2572008-05-31 14:23:50 +05306 *
7 * For licencing details, see COPYING.
8 *
9 * Defines low-level handling of mcount calls when the kernel
10 * is compiled with the -pg flag. When using dynamic ftrace, the
Rabin Vincent3b6c2232010-08-10 19:43:28 +010011 * mcount call-sites get patched with NOP till they are enabled.
12 * All code mutation routines here are called under stop_machine().
Abhishek Sagar014c2572008-05-31 14:23:50 +053013 */
14
15#include <linux/ftrace.h>
Rabin Vincent3b6c2232010-08-10 19:43:28 +010016#include <linux/uaccess.h>
Abhishek Sagar014c2572008-05-31 14:23:50 +053017
Abhishek Sagar395a59d2008-06-21 23:47:27 +053018#include <asm/cacheflush.h>
19#include <asm/ftrace.h>
20
Rabin Vincent72dc43a2010-08-10 19:52:35 +010021#ifdef CONFIG_THUMB2_KERNEL
22#define NOP 0xeb04f85d /* pop.w {lr} */
23#else
Rabin Vincent3b6c2232010-08-10 19:43:28 +010024#define NOP 0xe8bd4000 /* pop {lr} */
Rabin Vincent72dc43a2010-08-10 19:52:35 +010025#endif
Abhishek Sagar014c2572008-05-31 14:23:50 +053026
Rabin Vincent3b6c2232010-08-10 19:43:28 +010027#ifdef CONFIG_OLD_MCOUNT
28#define OLD_MCOUNT_ADDR ((unsigned long) mcount)
29#define OLD_FTRACE_ADDR ((unsigned long) ftrace_caller_old)
Abhishek Sagar014c2572008-05-31 14:23:50 +053030
Rabin Vincent3b6c2232010-08-10 19:43:28 +010031#define OLD_NOP 0xe1a00000 /* mov r0, r0 */
32
33static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec)
Abhishek Sagar014c2572008-05-31 14:23:50 +053034{
Rabin Vincent3b6c2232010-08-10 19:43:28 +010035 return rec->arch.old_mcount ? OLD_NOP : NOP;
Abhishek Sagar014c2572008-05-31 14:23:50 +053036}
37
Rabin Vincent3b6c2232010-08-10 19:43:28 +010038static unsigned long adjust_address(struct dyn_ftrace *rec, unsigned long addr)
39{
40 if (!rec->arch.old_mcount)
41 return addr;
42
43 if (addr == MCOUNT_ADDR)
44 addr = OLD_MCOUNT_ADDR;
45 else if (addr == FTRACE_ADDR)
46 addr = OLD_FTRACE_ADDR;
47
48 return addr;
49}
50#else
51static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec)
52{
53 return NOP;
54}
55
56static unsigned long adjust_address(struct dyn_ftrace *rec, unsigned long addr)
57{
58 return addr;
59}
60#endif
61
Abhishek Sagar014c2572008-05-31 14:23:50 +053062/* construct a branch (BL) instruction to addr */
Rabin Vincent72dc43a2010-08-10 19:52:35 +010063#ifdef CONFIG_THUMB2_KERNEL
64static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr)
65{
66 unsigned long s, j1, j2, i1, i2, imm10, imm11;
67 unsigned long first, second;
68 long offset;
69
70 offset = (long)addr - (long)(pc + 4);
71 if (offset < -16777216 || offset > 16777214) {
72 WARN_ON_ONCE(1);
73 return 0;
74 }
75
76 s = (offset >> 24) & 0x1;
77 i1 = (offset >> 23) & 0x1;
78 i2 = (offset >> 22) & 0x1;
79 imm10 = (offset >> 12) & 0x3ff;
80 imm11 = (offset >> 1) & 0x7ff;
81
82 j1 = (!i1) ^ s;
83 j2 = (!i2) ^ s;
84
85 first = 0xf000 | (s << 10) | imm10;
86 second = 0xd000 | (j1 << 13) | (j2 << 11) | imm11;
87
88 return (second << 16) | first;
89}
90#else
Rabin Vincent3b6c2232010-08-10 19:43:28 +010091static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr)
Abhishek Sagar014c2572008-05-31 14:23:50 +053092{
93 long offset;
94
Rabin Vincent3b6c2232010-08-10 19:43:28 +010095 offset = (long)addr - (long)(pc + 8);
Abhishek Sagar014c2572008-05-31 14:23:50 +053096 if (unlikely(offset < -33554432 || offset > 33554428)) {
97 /* Can't generate branches that far (from ARM ARM). Ftrace
Abhishek Sagar395a59d2008-06-21 23:47:27 +053098 * doesn't generate branches outside of kernel text.
Abhishek Sagar014c2572008-05-31 14:23:50 +053099 */
100 WARN_ON_ONCE(1);
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100101 return 0;
Abhishek Sagar014c2572008-05-31 14:23:50 +0530102 }
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100103
104 offset = (offset >> 2) & 0x00ffffff;
105
106 return 0xeb000000 | offset;
Abhishek Sagar014c2572008-05-31 14:23:50 +0530107}
Rabin Vincent72dc43a2010-08-10 19:52:35 +0100108#endif
Abhishek Sagar014c2572008-05-31 14:23:50 +0530109
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100110static int ftrace_modify_code(unsigned long pc, unsigned long old,
111 unsigned long new)
Abhishek Sagar014c2572008-05-31 14:23:50 +0530112{
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100113 unsigned long replaced;
Abhishek Sagar014c2572008-05-31 14:23:50 +0530114
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100115 if (probe_kernel_read(&replaced, (void *)pc, MCOUNT_INSN_SIZE))
116 return -EFAULT;
Abhishek Sagar014c2572008-05-31 14:23:50 +0530117
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100118 if (replaced != old)
119 return -EINVAL;
Abhishek Sagar014c2572008-05-31 14:23:50 +0530120
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100121 if (probe_kernel_write((void *)pc, &new, MCOUNT_INSN_SIZE))
122 return -EPERM;
Abhishek Sagar014c2572008-05-31 14:23:50 +0530123
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100124 flush_icache_range(pc, pc + MCOUNT_INSN_SIZE);
Abhishek Sagar014c2572008-05-31 14:23:50 +0530125
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100126 return 0;
Abhishek Sagar014c2572008-05-31 14:23:50 +0530127}
128
129int ftrace_update_ftrace_func(ftrace_func_t func)
130{
Abhishek Sagar014c2572008-05-31 14:23:50 +0530131 unsigned long pc, old;
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100132 unsigned long new;
133 int ret;
Abhishek Sagar014c2572008-05-31 14:23:50 +0530134
135 pc = (unsigned long)&ftrace_call;
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530136 memcpy(&old, &ftrace_call, MCOUNT_INSN_SIZE);
Abhishek Sagar014c2572008-05-31 14:23:50 +0530137 new = ftrace_call_replace(pc, (unsigned long)func);
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100138
139 ret = ftrace_modify_code(pc, old, new);
140
141#ifdef CONFIG_OLD_MCOUNT
142 if (!ret) {
143 pc = (unsigned long)&ftrace_call_old;
144 memcpy(&old, &ftrace_call_old, MCOUNT_INSN_SIZE);
145 new = ftrace_call_replace(pc, (unsigned long)func);
146
147 ret = ftrace_modify_code(pc, old, new);
148 }
149#endif
150
Abhishek Sagar014c2572008-05-31 14:23:50 +0530151 return ret;
152}
153
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100154int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
155{
156 unsigned long new, old;
157 unsigned long ip = rec->ip;
158
159 old = ftrace_nop_replace(rec);
160 new = ftrace_call_replace(ip, adjust_address(rec, addr));
161
162 return ftrace_modify_code(rec->ip, old, new);
163}
164
165int ftrace_make_nop(struct module *mod,
166 struct dyn_ftrace *rec, unsigned long addr)
167{
168 unsigned long ip = rec->ip;
169 unsigned long old;
170 unsigned long new;
171 int ret;
172
173 old = ftrace_call_replace(ip, adjust_address(rec, addr));
174 new = ftrace_nop_replace(rec);
175 ret = ftrace_modify_code(ip, old, new);
176
177#ifdef CONFIG_OLD_MCOUNT
178 if (ret == -EINVAL && addr == MCOUNT_ADDR) {
179 rec->arch.old_mcount = true;
180
181 old = ftrace_call_replace(ip, adjust_address(rec, addr));
182 new = ftrace_nop_replace(rec);
183 ret = ftrace_modify_code(ip, old, new);
184 }
185#endif
186
187 return ret;
188}
189
Abhishek Sagar014c2572008-05-31 14:23:50 +0530190int __init ftrace_dyn_arch_init(void *data)
191{
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100192 *(unsigned long *)data = 0;
193
Abhishek Sagar014c2572008-05-31 14:23:50 +0530194 return 0;
195}