2 * Ftrace support for Microblaze.
4 * Copyright (C) 2009 Michal Simek <monstr@monstr.eu>
5 * Copyright (C) 2009 PetaLogix
7 * Based on MIPS and PowerPC ftrace code
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file "COPYING" in the main directory of this archive
14 #include <asm/cacheflush.h>
15 #include <linux/ftrace.h>
17 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
19 * Hook the return address and push it in the stack of return addrs
20 * in current thread info.
22 void prepare_ftrace_return(unsigned long *parent
, unsigned long self_addr
)
26 unsigned long return_hooker
= (unsigned long)
29 if (unlikely(ftrace_graph_is_dead()))
32 if (unlikely(atomic_read(¤t
->tracing_graph_pause
)))
36 * Protect against fault, even if it shouldn't
37 * happen. This tool is too much intrusive to
38 * ignore such a protection.
40 asm volatile(" 1: lwi %0, %2, 0;" \
44 " .section .fixup, \"ax\";" \
48 " .section __ex_table,\"a\";" \
52 : "=&r" (old
), "=r" (faulted
)
53 : "r" (parent
), "r" (return_hooker
)
56 flush_dcache_range((u32
)parent
, (u32
)parent
+ 4);
57 flush_icache_range((u32
)parent
, (u32
)parent
+ 4);
59 if (unlikely(faulted
)) {
65 if (function_graph_enter(old
, self_addr
, 0, NULL
))
68 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
70 #ifdef CONFIG_DYNAMIC_FTRACE
71 /* save value to addr - it is save to do it in asm */
72 static int ftrace_modify_code(unsigned long addr
, unsigned int value
)
76 __asm__
__volatile__(" 1: swi %2, %1, 0;" \
79 " .section .fixup, \"ax\";" \
83 " .section __ex_table,\"a\";" \
87 : "r" (addr
), "r" (value
)
90 if (unlikely(faulted
))
93 flush_dcache_range(addr
, addr
+ 4);
94 flush_icache_range(addr
, addr
+ 4);
99 #define MICROBLAZE_NOP 0x80000000
100 #define MICROBLAZE_BRI 0xb800000C
102 static unsigned int recorded
; /* if save was or not */
103 static unsigned int imm
; /* saving whole imm instruction */
105 /* There are two approaches howto solve ftrace_make nop function - look below */
106 #undef USE_FTRACE_NOP
108 #ifdef USE_FTRACE_NOP
109 static unsigned int bralid
; /* saving whole bralid instruction */
112 int ftrace_make_nop(struct module
*mod
,
113 struct dyn_ftrace
*rec
, unsigned long addr
)
115 /* we have this part of code which we are working with
116 * b000c000 imm -16384
117 * b9fc8e30 bralid r15, -29136 // c0008e30 <_mcount>
118 * 80000000 or r0, r0, r0
120 * The first solution (!USE_FTRACE_NOP-could be called branch solution)
121 * b000c000 bri 12 (0xC - jump to any other instruction)
122 * b9fc8e30 bralid r15, -29136 // c0008e30 <_mcount>
123 * 80000000 or r0, r0, r0
124 * any other instruction
126 * The second solution (USE_FTRACE_NOP) - no jump just nops
127 * 80000000 or r0, r0, r0
128 * 80000000 or r0, r0, r0
129 * 80000000 or r0, r0, r0
135 imm
= *(unsigned int *)rec
->ip
;
136 pr_debug("%s: imm:0x%x\n", __func__
, imm
);
137 #ifdef USE_FTRACE_NOP
138 bralid
= *(unsigned int *)(rec
->ip
+ 4);
139 pr_debug("%s: bralid 0x%x\n", __func__
, bralid
);
140 #endif /* USE_FTRACE_NOP */
143 #ifdef USE_FTRACE_NOP
144 ret
= ftrace_modify_code(rec
->ip
, MICROBLAZE_NOP
);
145 ret
+= ftrace_modify_code(rec
->ip
+ 4, MICROBLAZE_NOP
);
146 #else /* USE_FTRACE_NOP */
147 ret
= ftrace_modify_code(rec
->ip
, MICROBLAZE_BRI
);
148 #endif /* USE_FTRACE_NOP */
152 /* I believe that first is called ftrace_make_nop before this function */
153 int ftrace_make_call(struct dyn_ftrace
*rec
, unsigned long addr
)
156 pr_debug("%s: addr:0x%x, rec->ip: 0x%x, imm:0x%x\n",
157 __func__
, (unsigned int)addr
, (unsigned int)rec
->ip
, imm
);
158 ret
= ftrace_modify_code(rec
->ip
, imm
);
159 #ifdef USE_FTRACE_NOP
160 pr_debug("%s: bralid:0x%x\n", __func__
, bralid
);
161 ret
+= ftrace_modify_code(rec
->ip
+ 4, bralid
);
162 #endif /* USE_FTRACE_NOP */
166 int __init
ftrace_dyn_arch_init(void)
171 int ftrace_update_ftrace_func(ftrace_func_t func
)
173 unsigned long ip
= (unsigned long)(&ftrace_call
);
174 unsigned int upper
= (unsigned int)func
;
175 unsigned int lower
= (unsigned int)func
;
178 /* create proper saving to ftrace_call poll */
179 upper
= 0xb0000000 + (upper
>> 16); /* imm func_upper */
180 lower
= 0x32800000 + (lower
& 0xFFFF); /* addik r20, r0, func_lower */
182 pr_debug("%s: func=0x%x, ip=0x%x, upper=0x%x, lower=0x%x\n",
183 __func__
, (unsigned int)func
, (unsigned int)ip
, upper
, lower
);
185 /* save upper and lower code */
186 ret
= ftrace_modify_code(ip
, upper
);
187 ret
+= ftrace_modify_code(ip
+ 4, lower
);
189 /* We just need to replace the rtsd r15, 8 with NOP */
190 ret
+= ftrace_modify_code((unsigned long)&ftrace_caller
,
196 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
197 unsigned int old_jump
; /* saving place for jump instruction */
199 int ftrace_enable_ftrace_graph_caller(void)
202 unsigned long ip
= (unsigned long)(&ftrace_call_graph
);
204 old_jump
= *(unsigned int *)ip
; /* save jump over instruction */
205 ret
= ftrace_modify_code(ip
, MICROBLAZE_NOP
);
207 pr_debug("%s: Replace instruction: 0x%x\n", __func__
, old_jump
);
211 int ftrace_disable_ftrace_graph_caller(void)
214 unsigned long ip
= (unsigned long)(&ftrace_call_graph
);
216 ret
= ftrace_modify_code(ip
, old_jump
);
218 pr_debug("%s\n", __func__
);
221 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
222 #endif /* CONFIG_DYNAMIC_FTRACE */