2 * Copyright 2012 Tilera Corporation. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
14 * TILE-Gx specific ftrace support
17 #include <linux/ftrace.h>
18 #include <linux/uaccess.h>
20 #include <asm/cacheflush.h>
21 #include <asm/ftrace.h>
22 #include <asm/sections.h>
25 #include <arch/opcode.h>
27 #ifdef CONFIG_DYNAMIC_FTRACE
29 static int machine_stopped __read_mostly
;
31 int ftrace_arch_code_modify_prepare(void)
37 int ftrace_arch_code_modify_post_process(void)
39 flush_icache_range(0, CHIP_L1I_CACHE_SIZE());
45 * Put { move r10, lr; jal ftrace_caller } in a bundle, this lets dynamic
46 * tracer just add one cycle overhead to every kernel function when disabled.
48 static unsigned long ftrace_gen_branch(unsigned long pc
, unsigned long addr
,
51 tilegx_bundle_bits opcode_x0
, opcode_x1
;
52 long pcrel_by_instr
= (addr
- pc
) >> TILEGX_LOG2_BUNDLE_SIZE_IN_BYTES
;
55 /* opcode: jal addr */
57 create_Opcode_X1(JUMP_OPCODE_X1
) |
58 create_JumpOpcodeExtension_X1(JAL_JUMP_OPCODE_X1
) |
59 create_JumpOff_X1(pcrel_by_instr
);
63 create_Opcode_X1(JUMP_OPCODE_X1
) |
64 create_JumpOpcodeExtension_X1(J_JUMP_OPCODE_X1
) |
65 create_JumpOff_X1(pcrel_by_instr
);
69 * Also put { move r10, lr; jal ftrace_stub } in a bundle, which
70 * is used to replace the instruction in address ftrace_call.
72 if (addr
== FTRACE_ADDR
|| addr
== (unsigned long)ftrace_stub
) {
73 /* opcode: or r10, lr, zero */
76 create_SrcA_X0(TREG_LR
) |
77 create_SrcB_X0(TREG_ZERO
) |
78 create_RRROpcodeExtension_X0(OR_RRR_0_OPCODE_X0
) |
79 create_Opcode_X0(RRR_0_OPCODE_X0
);
83 create_UnaryOpcodeExtension_X0(FNOP_UNARY_OPCODE_X0
) |
84 create_RRROpcodeExtension_X0(UNARY_RRR_0_OPCODE_X0
) |
85 create_Opcode_X0(RRR_0_OPCODE_X0
);
88 return opcode_x1
| opcode_x0
;
91 static unsigned long ftrace_nop_replace(struct dyn_ftrace
*rec
)
96 static unsigned long ftrace_call_replace(unsigned long pc
, unsigned long addr
)
98 return ftrace_gen_branch(pc
, addr
, true);
101 static int ftrace_modify_code(unsigned long pc
, unsigned long old
,
106 /* Check if the address is in kernel text space and module space. */
107 if (!kernel_text_address(pc
))
110 /* Operate on writable kernel text mapping. */
111 pc_wr
= ktext_writable_addr(pc
);
113 if (probe_kernel_write((void *)pc_wr
, &new, MCOUNT_INSN_SIZE
))
118 if (!machine_stopped
&& num_online_cpus() > 1)
119 flush_icache_range(pc
, pc
+ MCOUNT_INSN_SIZE
);
124 int ftrace_update_ftrace_func(ftrace_func_t func
)
126 unsigned long pc
, old
;
130 pc
= (unsigned long)&ftrace_call
;
131 memcpy(&old
, &ftrace_call
, MCOUNT_INSN_SIZE
);
132 new = ftrace_call_replace(pc
, (unsigned long)func
);
134 ret
= ftrace_modify_code(pc
, old
, new);
139 int ftrace_make_call(struct dyn_ftrace
*rec
, unsigned long addr
)
141 unsigned long new, old
;
142 unsigned long ip
= rec
->ip
;
144 old
= ftrace_nop_replace(rec
);
145 new = ftrace_call_replace(ip
, addr
);
147 return ftrace_modify_code(rec
->ip
, old
, new);
150 int ftrace_make_nop(struct module
*mod
,
151 struct dyn_ftrace
*rec
, unsigned long addr
)
153 unsigned long ip
= rec
->ip
;
158 old
= ftrace_call_replace(ip
, addr
);
159 new = ftrace_nop_replace(rec
);
160 ret
= ftrace_modify_code(ip
, old
, new);
165 int __init
ftrace_dyn_arch_init(void)
169 #endif /* CONFIG_DYNAMIC_FTRACE */
171 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
172 void prepare_ftrace_return(unsigned long *parent
, unsigned long self_addr
,
173 unsigned long frame_pointer
)
175 unsigned long return_hooker
= (unsigned long) &return_to_handler
;
176 struct ftrace_graph_ent trace
;
180 if (unlikely(atomic_read(¤t
->tracing_graph_pause
)))
184 *parent
= return_hooker
;
186 err
= ftrace_push_return_trace(old
, self_addr
, &trace
.depth
,
187 frame_pointer
, NULL
);
193 trace
.func
= self_addr
;
195 /* Only trace if the calling function expects to */
196 if (!ftrace_graph_entry(&trace
)) {
197 current
->curr_ret_stack
--;
202 #ifdef CONFIG_DYNAMIC_FTRACE
203 extern unsigned long ftrace_graph_call
;
205 static int __ftrace_modify_caller(unsigned long *callsite
,
206 void (*func
) (void), bool enable
)
208 unsigned long caller_fn
= (unsigned long) func
;
209 unsigned long pc
= (unsigned long) callsite
;
210 unsigned long branch
= ftrace_gen_branch(pc
, caller_fn
, false);
211 unsigned long nop
= NOP();
212 unsigned long old
= enable
? nop
: branch
;
213 unsigned long new = enable
? branch
: nop
;
215 return ftrace_modify_code(pc
, old
, new);
218 static int ftrace_modify_graph_caller(bool enable
)
222 ret
= __ftrace_modify_caller(&ftrace_graph_call
,
229 int ftrace_enable_ftrace_graph_caller(void)
231 return ftrace_modify_graph_caller(true);
234 int ftrace_disable_ftrace_graph_caller(void)
236 return ftrace_modify_graph_caller(false);
238 #endif /* CONFIG_DYNAMIC_FTRACE */
239 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */