2 * Dynamic function tracing support.
4 * Copyright (C) 2008 Abhishek Sagar <sagar.abhishek@gmail.com>
5 * Copyright (C) 2010 Rabin Vincent <rabin@rab.in>
7 * For licencing details, see COPYING.
9 * Defines low-level handling of mcount calls when the kernel
10 * is compiled with the -pg flag. When using dynamic ftrace, the
11 * mcount call-sites get patched with NOP till they are enabled.
12 * All code mutation routines here are called under stop_machine().
15 #include <linux/ftrace.h>
16 #include <linux/uaccess.h>
18 #include <asm/cacheflush.h>
19 #include <asm/ftrace.h>
21 #ifdef CONFIG_THUMB2_KERNEL
22 #define NOP 0xeb04f85d /* pop.w {lr} */
24 #define NOP 0xe8bd4000 /* pop {lr} */
27 #ifdef CONFIG_OLD_MCOUNT
28 #define OLD_MCOUNT_ADDR ((unsigned long) mcount)
29 #define OLD_FTRACE_ADDR ((unsigned long) ftrace_caller_old)
31 #define OLD_NOP 0xe1a00000 /* mov r0, r0 */
33 static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec)
35 return rec->arch.old_mcount ? OLD_NOP : NOP;
38 static unsigned long adjust_address(struct dyn_ftrace *rec, unsigned long addr)
40 if (!rec->arch.old_mcount)
43 if (addr == MCOUNT_ADDR)
44 addr = OLD_MCOUNT_ADDR;
45 else if (addr == FTRACE_ADDR)
46 addr = OLD_FTRACE_ADDR;
51 static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec)
56 static unsigned long adjust_address(struct dyn_ftrace *rec, unsigned long addr)
62 /* construct a branch (BL) instruction to addr */
63 #ifdef CONFIG_THUMB2_KERNEL
64 static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr)
66 unsigned long s, j1, j2, i1, i2, imm10, imm11;
67 unsigned long first, second;
70 offset = (long)addr - (long)(pc + 4);
71 if (offset < -16777216 || offset > 16777214) {
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;
85 first = 0xf000 | (s << 10) | imm10;
86 second = 0xd000 | (j1 << 13) | (j2 << 11) | imm11;
88 return (second << 16) | first;
91 static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr)
95 offset = (long)addr - (long)(pc + 8);
96 if (unlikely(offset < -33554432 || offset > 33554428)) {
97 /* Can't generate branches that far (from ARM ARM). Ftrace
98 * doesn't generate branches outside of kernel text.
104 offset = (offset >> 2) & 0x00ffffff;
106 return 0xeb000000 | offset;
110 static int ftrace_modify_code(unsigned long pc, unsigned long old,
113 unsigned long replaced;
115 if (probe_kernel_read(&replaced, (void *)pc, MCOUNT_INSN_SIZE))
121 if (probe_kernel_write((void *)pc, &new, MCOUNT_INSN_SIZE))
124 flush_icache_range(pc, pc + MCOUNT_INSN_SIZE);
129 int ftrace_update_ftrace_func(ftrace_func_t func)
131 unsigned long pc, old;
135 pc = (unsigned long)&ftrace_call;
136 memcpy(&old, &ftrace_call, MCOUNT_INSN_SIZE);
137 new = ftrace_call_replace(pc, (unsigned long)func);
139 ret = ftrace_modify_code(pc, old, new);
141 #ifdef CONFIG_OLD_MCOUNT
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);
147 ret = ftrace_modify_code(pc, old, new);
154 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
156 unsigned long new, old;
157 unsigned long ip = rec->ip;
159 old = ftrace_nop_replace(rec);
160 new = ftrace_call_replace(ip, adjust_address(rec, addr));
162 return ftrace_modify_code(rec->ip, old, new);
165 int ftrace_make_nop(struct module *mod,
166 struct dyn_ftrace *rec, unsigned long addr)
168 unsigned long ip = rec->ip;
173 old = ftrace_call_replace(ip, adjust_address(rec, addr));
174 new = ftrace_nop_replace(rec);
175 ret = ftrace_modify_code(ip, old, new);
177 #ifdef CONFIG_OLD_MCOUNT
178 if (ret == -EINVAL && addr == MCOUNT_ADDR) {
179 rec->arch.old_mcount = true;
181 old = ftrace_call_replace(ip, adjust_address(rec, addr));
182 new = ftrace_nop_replace(rec);
183 ret = ftrace_modify_code(ip, old, new);
190 int __init ftrace_dyn_arch_init(void *data)
192 *(unsigned long *)data = 0;