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>
24 #include <arch/opcode.h>
26 #ifdef CONFIG_DYNAMIC_FTRACE
28 static inline tilegx_bundle_bits
NOP(void)
30 return create_UnaryOpcodeExtension_X0(FNOP_UNARY_OPCODE_X0
) |
31 create_RRROpcodeExtension_X0(UNARY_RRR_0_OPCODE_X0
) |
32 create_Opcode_X0(RRR_0_OPCODE_X0
) |
33 create_UnaryOpcodeExtension_X1(NOP_UNARY_OPCODE_X1
) |
34 create_RRROpcodeExtension_X1(UNARY_RRR_0_OPCODE_X1
) |
35 create_Opcode_X1(RRR_0_OPCODE_X1
);
38 static int machine_stopped __read_mostly
;
40 int ftrace_arch_code_modify_prepare(void)
46 int ftrace_arch_code_modify_post_process(void)
48 flush_icache_range(0, CHIP_L1I_CACHE_SIZE());
54 * Put { move r10, lr; jal ftrace_caller } in a bundle, this lets dynamic
55 * tracer just add one cycle overhead to every kernel function when disabled.
57 static unsigned long ftrace_gen_branch(unsigned long pc
, unsigned long addr
,
60 tilegx_bundle_bits opcode_x0
, opcode_x1
;
61 long pcrel_by_instr
= (addr
- pc
) >> TILEGX_LOG2_BUNDLE_SIZE_IN_BYTES
;
64 /* opcode: jal addr */
66 create_Opcode_X1(JUMP_OPCODE_X1
) |
67 create_JumpOpcodeExtension_X1(JAL_JUMP_OPCODE_X1
) |
68 create_JumpOff_X1(pcrel_by_instr
);
72 create_Opcode_X1(JUMP_OPCODE_X1
) |
73 create_JumpOpcodeExtension_X1(J_JUMP_OPCODE_X1
) |
74 create_JumpOff_X1(pcrel_by_instr
);
77 if (addr
== FTRACE_ADDR
) {
78 /* opcode: or r10, lr, zero */
81 create_SrcA_X0(TREG_LR
) |
82 create_SrcB_X0(TREG_ZERO
) |
83 create_RRROpcodeExtension_X0(OR_RRR_0_OPCODE_X0
) |
84 create_Opcode_X0(RRR_0_OPCODE_X0
);
88 create_UnaryOpcodeExtension_X0(FNOP_UNARY_OPCODE_X0
) |
89 create_RRROpcodeExtension_X0(UNARY_RRR_0_OPCODE_X0
) |
90 create_Opcode_X0(RRR_0_OPCODE_X0
);
93 return opcode_x1
| opcode_x0
;
96 static unsigned long ftrace_nop_replace(struct dyn_ftrace
*rec
)
101 static unsigned long ftrace_call_replace(unsigned long pc
, unsigned long addr
)
103 return ftrace_gen_branch(pc
, addr
, true);
106 static int ftrace_modify_code(unsigned long pc
, unsigned long old
,
111 /* Check if the address is in kernel text space and module space. */
112 if (!kernel_text_address(pc
))
115 /* Operate on writable kernel text mapping. */
116 pc_wr
= pc
- MEM_SV_START
+ PAGE_OFFSET
;
118 if (probe_kernel_write((void *)pc_wr
, &new, MCOUNT_INSN_SIZE
))
123 if (!machine_stopped
&& num_online_cpus() > 1)
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);
144 int ftrace_make_call(struct dyn_ftrace
*rec
, unsigned long addr
)
146 unsigned long new, old
;
147 unsigned long ip
= rec
->ip
;
149 old
= ftrace_nop_replace(rec
);
150 new = ftrace_call_replace(ip
, addr
);
152 return ftrace_modify_code(rec
->ip
, old
, new);
155 int ftrace_make_nop(struct module
*mod
,
156 struct dyn_ftrace
*rec
, unsigned long addr
)
158 unsigned long ip
= rec
->ip
;
163 old
= ftrace_call_replace(ip
, addr
);
164 new = ftrace_nop_replace(rec
);
165 ret
= ftrace_modify_code(ip
, old
, new);
170 int __init
ftrace_dyn_arch_init(void *data
)
172 *(unsigned long *)data
= 0;
176 #endif /* CONFIG_DYNAMIC_FTRACE */
178 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
179 void prepare_ftrace_return(unsigned long *parent
, unsigned long self_addr
,
180 unsigned long frame_pointer
)
182 unsigned long return_hooker
= (unsigned long) &return_to_handler
;
183 struct ftrace_graph_ent trace
;
187 if (unlikely(atomic_read(¤t
->tracing_graph_pause
)))
191 *parent
= return_hooker
;
193 err
= ftrace_push_return_trace(old
, self_addr
, &trace
.depth
,
200 trace
.func
= self_addr
;
202 /* Only trace if the calling function expects to */
203 if (!ftrace_graph_entry(&trace
)) {
204 current
->curr_ret_stack
--;
209 #ifdef CONFIG_DYNAMIC_FTRACE
210 extern unsigned long ftrace_graph_call
;
212 static int __ftrace_modify_caller(unsigned long *callsite
,
213 void (*func
) (void), bool enable
)
215 unsigned long caller_fn
= (unsigned long) func
;
216 unsigned long pc
= (unsigned long) callsite
;
217 unsigned long branch
= ftrace_gen_branch(pc
, caller_fn
, false);
218 unsigned long nop
= NOP();
219 unsigned long old
= enable
? nop
: branch
;
220 unsigned long new = enable
? branch
: nop
;
222 return ftrace_modify_code(pc
, old
, new);
225 static int ftrace_modify_graph_caller(bool enable
)
229 ret
= __ftrace_modify_caller(&ftrace_graph_call
,
236 int ftrace_enable_ftrace_graph_caller(void)
238 return ftrace_modify_graph_caller(true);
241 int ftrace_disable_ftrace_graph_caller(void)
243 return ftrace_modify_graph_caller(false);
245 #endif /* CONFIG_DYNAMIC_FTRACE */
246 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */