1 /*
2  * Backtrace support for Microblaze
3  *
4  * Copyright (C) 2010  Digital Design Corporation
5  *
6  * Based on arch/sh/kernel/cpu/sh5/unwind.c code which is:
7  * Copyright (C) 2004  Paul Mundt
8  * Copyright (C) 2004  Richard Curnow
9  *
10  * This file is subject to the terms and conditions of the GNU General Public
11  * License.  See the file "COPYING" in the main directory of this archive
12  * for more details.
13  */
14 
15 /* #define DEBUG 1 */
16 #include <linux/export.h>
17 #include <linux/kallsyms.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/sched/task_stack.h>
21 #include <linux/stacktrace.h>
22 #include <linux/types.h>
23 #include <linux/errno.h>
24 #include <linux/io.h>
25 #include <asm/sections.h>
26 #include <asm/exceptions.h>
27 #include <asm/unwind.h>
28 #include <asm/switch_to.h>
29 
30 struct stack_trace;
31 
32 /*
33  * On Microblaze, finding the previous stack frame is a little tricky.
34  * At this writing (3/2010), Microblaze does not support CONFIG_FRAME_POINTERS,
35  * and even if it did, gcc (4.1.2) does not store the frame pointer at
36  * a consistent offset within each frame. To determine frame size, it is
37  * necessary to search for the assembly instruction that creates or reclaims
38  * the frame and extract the size from it.
39  *
40  * Microblaze stores the stack pointer in r1, and creates a frame via
41  *
42  *     addik r1, r1, -FRAME_SIZE
43  *
44  * The frame is reclaimed via
45  *
46  *     addik r1, r1, FRAME_SIZE
47  *
48  * Frame creation occurs at or near the top of a function.
49  * Depending on the compiler, reclaim may occur at the end, or before
50  * a mid-function return.
51  *
52  * A stack frame is usually not created in a leaf function.
53  *
54  */
55 
56 /**
57  * get_frame_size - Extract the stack adjustment from an
58  *                  "addik r1, r1, adjust" instruction
59  * @instr : Microblaze instruction
60  *
61  * Return - Number of stack bytes the instruction reserves or reclaims
62  */
get_frame_size(unsigned long instr)63 static inline long get_frame_size(unsigned long instr)
64 {
65 	return abs((s16)(instr & 0xFFFF));
66 }
67 
68 /**
69  * find_frame_creation - Search backward to find the instruction that creates
70  *                       the stack frame (hopefully, for the same function the
71  *                       initial PC is in).
72  * @pc : Program counter at which to begin the search
73  *
74  * Return - PC at which stack frame creation occurs
75  *          NULL if this cannot be found, i.e. a leaf function
76  */
find_frame_creation(unsigned long * pc)77 static unsigned long *find_frame_creation(unsigned long *pc)
78 {
79 	int i;
80 
81 	/* NOTE: Distance to search is arbitrary
82 	 *	 250 works well for most things,
83 	 *	 750 picks up things like tcp_recvmsg(),
84 	 *	1000 needed for fat_fill_super()
85 	 */
86 	for (i = 0; i < 1000; i++, pc--) {
87 		unsigned long instr;
88 		s16 frame_size;
89 
90 		if (!kernel_text_address((unsigned long) pc))
91 			return NULL;
92 
93 		instr = *pc;
94 
95 		/* addik r1, r1, foo ? */
96 		if ((instr & 0xFFFF0000) != 0x30210000)
97 			continue;	/* No */
98 
99 		frame_size = get_frame_size(instr);
100 		if ((frame_size < 8) || (frame_size & 3)) {
101 			pr_debug("    Invalid frame size %d at 0x%p\n",
102 				 frame_size, pc);
103 			return NULL;
104 		}
105 
106 		pr_debug("    Found frame creation at 0x%p, size %d\n", pc,
107 			 frame_size);
108 		return pc;
109 	}
110 
111 	return NULL;
112 }
113 
114 /**
115  * lookup_prev_stack_frame - Find the stack frame of the previous function.
116  * @fp          : Frame (stack) pointer for current function
117  * @pc          : Program counter within current function
118  * @leaf_return : r15 value within current function. If the current function
119  *		  is a leaf, this is the caller's return address.
120  * @pprev_fp    : On exit, set to frame (stack) pointer for previous function
121  * @pprev_pc    : On exit, set to current function caller's return address
122  *
123  * Return - 0 on success, -EINVAL if the previous frame cannot be found
124  */
lookup_prev_stack_frame(unsigned long fp,unsigned long pc,unsigned long leaf_return,unsigned long * pprev_fp,unsigned long * pprev_pc)125 static int lookup_prev_stack_frame(unsigned long fp, unsigned long pc,
126 				   unsigned long leaf_return,
127 				   unsigned long *pprev_fp,
128 				   unsigned long *pprev_pc)
129 {
130 	unsigned long *prologue = NULL;
131 
132 	/* _switch_to is a special leaf function */
133 	if (pc != (unsigned long) &_switch_to)
134 		prologue = find_frame_creation((unsigned long *)pc);
135 
136 	if (prologue) {
137 		long frame_size = get_frame_size(*prologue);
138 
139 		*pprev_fp = fp + frame_size;
140 		*pprev_pc = *(unsigned long *)fp;
141 	} else {
142 		if (!leaf_return)
143 			return -EINVAL;
144 		*pprev_pc = leaf_return;
145 		*pprev_fp = fp;
146 	}
147 
148 	/* NOTE: don't check kernel_text_address here, to allow display
149 	 *	 of userland return address
150 	 */
151 	return (!*pprev_pc || (*pprev_pc & 3)) ? -EINVAL : 0;
152 }
153 
154 static void microblaze_unwind_inner(struct task_struct *task,
155 				    unsigned long pc, unsigned long fp,
156 				    unsigned long leaf_return,
157 				    struct stack_trace *trace,
158 				    const char *loglvl);
159 
160 /**
161  * unwind_trap - Unwind through a system trap, that stored previous state
162  *		 on the stack.
163  */
164 #ifdef CONFIG_MMU
unwind_trap(struct task_struct * task,unsigned long pc,unsigned long fp,struct stack_trace * trace,const char * loglvl)165 static inline void unwind_trap(struct task_struct *task, unsigned long pc,
166 				unsigned long fp, struct stack_trace *trace,
167 				const char *loglvl)
168 {
169 	/* To be implemented */
170 }
171 #else
unwind_trap(struct task_struct * task,unsigned long pc,unsigned long fp,struct stack_trace * trace,const char * loglvl)172 static inline void unwind_trap(struct task_struct *task, unsigned long pc,
173 				unsigned long fp, struct stack_trace *trace,
174 				const char *loglvl)
175 {
176 	const struct pt_regs *regs = (const struct pt_regs *) fp;
177 	microblaze_unwind_inner(task, regs->pc, regs->r1, regs->r15, trace, loglvl);
178 }
179 #endif
180 
181 /**
182  * microblaze_unwind_inner - Unwind the stack from the specified point
183  * @task  : Task whose stack we are to unwind (may be NULL)
184  * @pc    : Program counter from which we start unwinding
185  * @fp    : Frame (stack) pointer from which we start unwinding
186  * @leaf_return : Value of r15 at pc. If the function is a leaf, this is
187  *				  the caller's return address.
188  * @trace : Where to store stack backtrace (PC values).
189  *	    NULL == print backtrace to kernel log
190  * @loglvl : Used for printk log level if (trace == NULL).
191  */
microblaze_unwind_inner(struct task_struct * task,unsigned long pc,unsigned long fp,unsigned long leaf_return,struct stack_trace * trace,const char * loglvl)192 static void microblaze_unwind_inner(struct task_struct *task,
193 			     unsigned long pc, unsigned long fp,
194 			     unsigned long leaf_return,
195 			     struct stack_trace *trace,
196 			     const char *loglvl)
197 {
198 	int ofs = 0;
199 
200 	pr_debug("    Unwinding with PC=%p, FP=%p\n", (void *)pc, (void *)fp);
201 	if (!pc || !fp || (pc & 3) || (fp & 3)) {
202 		pr_debug("    Invalid state for unwind, aborting\n");
203 		return;
204 	}
205 	for (; pc != 0;) {
206 		unsigned long next_fp, next_pc = 0;
207 		unsigned long return_to = pc +  2 * sizeof(unsigned long);
208 		const struct trap_handler_info *handler =
209 			&microblaze_trap_handlers;
210 
211 		/* Is previous function the HW exception handler? */
212 		if ((return_to >= (unsigned long)&_hw_exception_handler)
213 		    &&(return_to < (unsigned long)&ex_handler_unhandled)) {
214 			/*
215 			 * HW exception handler doesn't save all registers,
216 			 * so we open-code a special case of unwind_trap()
217 			 */
218 #ifndef CONFIG_MMU
219 			const struct pt_regs *regs =
220 				(const struct pt_regs *) fp;
221 #endif
222 			printk("%sHW EXCEPTION\n", loglvl);
223 #ifndef CONFIG_MMU
224 			microblaze_unwind_inner(task, regs->r17 - 4,
225 						fp + EX_HANDLER_STACK_SIZ,
226 						regs->r15, trace, loglvl);
227 #endif
228 			return;
229 		}
230 
231 		/* Is previous function a trap handler? */
232 		for (; handler->start_addr; ++handler) {
233 			if ((return_to >= handler->start_addr)
234 			    && (return_to <= handler->end_addr)) {
235 				if (!trace)
236 					printk("%s%s\n", loglvl, handler->trap_name);
237 				unwind_trap(task, pc, fp, trace, loglvl);
238 				return;
239 			}
240 		}
241 		pc -= ofs;
242 
243 		if (trace) {
244 #ifdef CONFIG_STACKTRACE
245 			if (trace->skip > 0)
246 				trace->skip--;
247 			else
248 				trace->entries[trace->nr_entries++] = pc;
249 
250 			if (trace->nr_entries >= trace->max_entries)
251 				break;
252 #endif
253 		} else {
254 			/* Have we reached userland? */
255 			if (unlikely(pc == task_pt_regs(task)->pc)) {
256 				printk("%s[<%p>] PID %lu [%s]\n",
257 					loglvl, (void *) pc,
258 					(unsigned long) task->pid,
259 					task->comm);
260 				break;
261 			} else
262 				print_ip_sym(loglvl, pc);
263 		}
264 
265 		/* Stop when we reach anything not part of the kernel */
266 		if (!kernel_text_address(pc))
267 			break;
268 
269 		if (lookup_prev_stack_frame(fp, pc, leaf_return, &next_fp,
270 					    &next_pc) == 0) {
271 			ofs = sizeof(unsigned long);
272 			pc = next_pc & ~3;
273 			fp = next_fp;
274 			leaf_return = 0;
275 		} else {
276 			pr_debug("    Failed to find previous stack frame\n");
277 			break;
278 		}
279 
280 		pr_debug("    Next PC=%p, next FP=%p\n",
281 			 (void *)next_pc, (void *)next_fp);
282 	}
283 }
284 
285 /**
286  * microblaze_unwind - Stack unwinder for Microblaze (external entry point)
287  * @task  : Task whose stack we are to unwind (NULL == current)
288  * @trace : Where to store stack backtrace (PC values).
289  *	    NULL == print backtrace to kernel log
290  * @loglvl : Used for printk log level if (trace == NULL).
291  */
microblaze_unwind(struct task_struct * task,struct stack_trace * trace,const char * loglvl)292 void microblaze_unwind(struct task_struct *task, struct stack_trace *trace,
293 		       const char *loglvl)
294 {
295 	if (task) {
296 		if (task == current) {
297 			const struct pt_regs *regs = task_pt_regs(task);
298 			microblaze_unwind_inner(task, regs->pc, regs->r1,
299 						regs->r15, trace, loglvl);
300 		} else {
301 			struct thread_info *thread_info =
302 				(struct thread_info *)(task->stack);
303 			const struct cpu_context *cpu_context =
304 				&thread_info->cpu_context;
305 
306 			microblaze_unwind_inner(task,
307 						(unsigned long) &_switch_to,
308 						cpu_context->r1,
309 						cpu_context->r15,
310 						trace, loglvl);
311 		}
312 	} else {
313 		unsigned long pc, fp;
314 
315 		__asm__ __volatile__ ("or %0, r1, r0" : "=r" (fp));
316 
317 		__asm__ __volatile__ (
318 			"brlid %0, 0f;"
319 			"nop;"
320 			"0:"
321 			: "=r" (pc)
322 		);
323 
324 		/* Since we are not a leaf function, use leaf_return = 0 */
325 		microblaze_unwind_inner(current, pc, fp, 0, trace, loglvl);
326 	}
327 }
328 
329