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 static int ret_addr; /* initialized as 0 by default */
156 /* I believe that first is called ftrace_make_nop before this function */
157 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
160 ret_addr = addr; /* saving where the barrier jump is */
161 pr_debug("%s: addr:0x%x, rec->ip: 0x%x, imm:0x%x\n",
162 __func__, (unsigned int)addr, (unsigned int)rec->ip, imm);
163 ret = ftrace_modify_code(rec->ip, imm);
164 #ifdef USE_FTRACE_NOP
165 pr_debug("%s: bralid:0x%x\n", __func__, bralid);
166 ret += ftrace_modify_code(rec->ip + 4, bralid);
167 #endif /* USE_FTRACE_NOP */
171 int __init ftrace_dyn_arch_init(void *data)
173 /* The return code is retured via data */
174 *(unsigned long *)data = 0;
179 int ftrace_update_ftrace_func(ftrace_func_t func)
181 unsigned long ip = (unsigned long)(&ftrace_call);
182 unsigned int upper = (unsigned int)func;
183 unsigned int lower = (unsigned int)func;
186 /* create proper saving to ftrace_call poll */
187 upper = 0xb0000000 + (upper >> 16); /* imm func_upper */
188 lower = 0x32800000 + (lower & 0xFFFF); /* addik r20, r0, func_lower */
190 pr_debug("%s: func=0x%x, ip=0x%x, upper=0x%x, lower=0x%x\n",
191 __func__, (unsigned int)func, (unsigned int)ip, upper, lower);
193 /* save upper and lower code */
194 ret = ftrace_modify_code(ip, upper);
195 ret += ftrace_modify_code(ip + 4, lower);
197 /* We just need to remove the rtsd r15, 8 by NOP */
200 ret += ftrace_modify_code(ret_addr, MICROBLAZE_NOP);
204 /* All changes are done - lets do caches consistent */
209 #endif /* CONFIG_DYNAMIC_FTRACE */