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 struct ftrace_graph_ent trace;
27 unsigned long return_hooker = (unsigned long)
30 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
34 * Protect against fault, even if it shouldn't
35 * happen. This tool is too much intrusive to
36 * ignore such a protection.
38 asm volatile(" 1: lwi %0, %2, 0; \
42 .section .fixup, \"ax\"; \
46 .section __ex_table,\"a\"; \
50 : "=&r" (old), "=r" (faulted)
51 : "r" (parent), "r" (return_hooker)
54 if (unlikely(faulted)) {
60 err = ftrace_push_return_trace(old, self_addr, &trace.depth, 0);
66 trace.func = self_addr;
67 /* Only trace if the calling function expects to */
68 if (!ftrace_graph_entry(&trace)) {
69 current->curr_ret_stack--;
73 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
75 #ifdef CONFIG_DYNAMIC_FTRACE
76 /* save value to addr - it is save to do it in asm */
77 static int ftrace_modify_code(unsigned long addr, unsigned int value)
81 __asm__ __volatile__(" 1: swi %2, %1, 0; \
84 .section .fixup, \"ax\"; \
88 .section __ex_table,\"a\"; \
92 : "r" (addr), "r" (value)
95 if (unlikely(faulted))
101 #define MICROBLAZE_NOP 0x80000000
102 #define MICROBLAZE_BRI 0xb800000C
104 static unsigned int recorded; /* if save was or not */
105 static unsigned int imm; /* saving whole imm instruction */
107 /* There are two approaches howto solve ftrace_make nop function - look below */
108 #undef USE_FTRACE_NOP
110 #ifdef USE_FTRACE_NOP
111 static unsigned int bralid; /* saving whole bralid instruction */
114 int ftrace_make_nop(struct module *mod,
115 struct dyn_ftrace *rec, unsigned long addr)
117 /* we have this part of code which we are working with
118 * b000c000 imm -16384
119 * b9fc8e30 bralid r15, -29136 // c0008e30 <_mcount>
120 * 80000000 or r0, r0, r0
122 * The first solution (!USE_FTRACE_NOP-could be called branch solution)
123 * b000c000 bri 12 (0xC - jump to any other instruction)
124 * b9fc8e30 bralid r15, -29136 // c0008e30 <_mcount>
125 * 80000000 or r0, r0, r0
126 * any other instruction
128 * The second solution (USE_FTRACE_NOP) - no jump just nops
129 * 80000000 or r0, r0, r0
130 * 80000000 or r0, r0, r0
131 * 80000000 or r0, r0, r0
137 imm = *(unsigned int *)rec->ip;
138 pr_debug("%s: imm:0x%x\n", __func__, imm);
139 #ifdef USE_FTRACE_NOP
140 bralid = *(unsigned int *)(rec->ip + 4);
141 pr_debug("%s: bralid 0x%x\n", __func__, bralid);
142 #endif /* USE_FTRACE_NOP */
145 #ifdef USE_FTRACE_NOP
146 ret = ftrace_modify_code(rec->ip, MICROBLAZE_NOP);
147 ret += ftrace_modify_code(rec->ip + 4, MICROBLAZE_NOP);
148 #else /* USE_FTRACE_NOP */
149 ret = ftrace_modify_code(rec->ip, MICROBLAZE_BRI);
150 #endif /* USE_FTRACE_NOP */
154 /* I believe that first is called ftrace_make_nop before this function */
155 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
158 pr_debug("%s: addr:0x%x, rec->ip: 0x%x, imm:0x%x\n",
159 __func__, (unsigned int)addr, (unsigned int)rec->ip, imm);
160 ret = ftrace_modify_code(rec->ip, imm);
161 #ifdef USE_FTRACE_NOP
162 pr_debug("%s: bralid:0x%x\n", __func__, bralid);
163 ret += ftrace_modify_code(rec->ip + 4, bralid);
164 #endif /* USE_FTRACE_NOP */
168 int __init ftrace_dyn_arch_init(void *data)
170 /* The return code is retured via data */
171 *(unsigned long *)data = 0;
176 int ftrace_update_ftrace_func(ftrace_func_t func)
178 unsigned long ip = (unsigned long)(&ftrace_call);
179 unsigned int upper = (unsigned int)func;
180 unsigned int lower = (unsigned int)func;
183 /* create proper saving to ftrace_call poll */
184 upper = 0xb0000000 + (upper >> 16); /* imm func_upper */
185 lower = 0x32800000 + (lower & 0xFFFF); /* addik r20, r0, func_lower */
187 pr_debug("%s: func=0x%x, ip=0x%x, upper=0x%x, lower=0x%x\n",
188 __func__, (unsigned int)func, (unsigned int)ip, upper, lower);
190 /* save upper and lower code */
191 ret = ftrace_modify_code(ip, upper);
192 ret += ftrace_modify_code(ip + 4, lower);
194 /* We just need to replace the rtsd r15, 8 with NOP */
195 ret += ftrace_modify_code((unsigned long)&ftrace_caller,
198 /* All changes are done - lets do caches consistent */
203 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
204 unsigned int old_jump; /* saving place for jump instruction */
206 int ftrace_enable_ftrace_graph_caller(void)
209 unsigned long ip = (unsigned long)(&ftrace_call_graph);
211 old_jump = *(unsigned int *)ip; /* save jump over instruction */
212 ret = ftrace_modify_code(ip, MICROBLAZE_NOP);
215 pr_debug("%s: Replace instruction: 0x%x\n", __func__, old_jump);
219 int ftrace_disable_ftrace_graph_caller(void)
222 unsigned long ip = (unsigned long)(&ftrace_call_graph);
224 ret = ftrace_modify_code(ip, old_jump);
227 pr_debug("%s\n", __func__);
230 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
231 #endif /* CONFIG_DYNAMIC_FTRACE */