xref: /openbmc/linux/arch/openrisc/kernel/traps.c (revision 151f4e2b)
1 /*
2  * OpenRISC traps.c
3  *
4  * Linux architectural port borrowing liberally from similar works of
5  * others.  All original copyrights apply as per the original source
6  * declaration.
7  *
8  * Modifications for the OpenRISC architecture:
9  * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
10  * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
11  *
12  *      This program is free software; you can redistribute it and/or
13  *      modify it under the terms of the GNU General Public License
14  *      as published by the Free Software Foundation; either version
15  *      2 of the License, or (at your option) any later version.
16  *
17  *  Here we handle the break vectors not used by the system call
18  *  mechanism, as well as some general stack/register dumping
19  *  things.
20  *
21  */
22 
23 #include <linux/init.h>
24 #include <linux/sched.h>
25 #include <linux/sched/debug.h>
26 #include <linux/sched/task_stack.h>
27 #include <linux/kernel.h>
28 #include <linux/extable.h>
29 #include <linux/kmod.h>
30 #include <linux/string.h>
31 #include <linux/errno.h>
32 #include <linux/ptrace.h>
33 #include <linux/timer.h>
34 #include <linux/mm.h>
35 #include <linux/kallsyms.h>
36 #include <linux/uaccess.h>
37 
38 #include <asm/io.h>
39 #include <asm/pgtable.h>
40 #include <asm/unwinder.h>
41 #include <asm/sections.h>
42 
43 int kstack_depth_to_print = 0x180;
44 int lwa_flag;
45 unsigned long __user *lwa_addr;
46 
47 void print_trace(void *data, unsigned long addr, int reliable)
48 {
49 	pr_emerg("[<%p>] %s%pS\n", (void *) addr, reliable ? "" : "? ",
50 	       (void *) addr);
51 }
52 
53 /* displays a short stack trace */
54 void show_stack(struct task_struct *task, unsigned long *esp)
55 {
56 	if (esp == NULL)
57 		esp = (unsigned long *)&esp;
58 
59 	pr_emerg("Call trace:\n");
60 	unwind_stack(NULL, esp, print_trace);
61 }
62 
63 void show_trace_task(struct task_struct *tsk)
64 {
65 	/*
66 	 * TODO: SysRq-T trace dump...
67 	 */
68 }
69 
70 void show_registers(struct pt_regs *regs)
71 {
72 	int i;
73 	int in_kernel = 1;
74 	unsigned long esp;
75 
76 	esp = (unsigned long)(regs->sp);
77 	if (user_mode(regs))
78 		in_kernel = 0;
79 
80 	printk("CPU #: %d\n"
81 	       "   PC: %08lx    SR: %08lx    SP: %08lx\n",
82 	       smp_processor_id(), regs->pc, regs->sr, regs->sp);
83 	printk("GPR00: %08lx GPR01: %08lx GPR02: %08lx GPR03: %08lx\n",
84 	       0L, regs->gpr[1], regs->gpr[2], regs->gpr[3]);
85 	printk("GPR04: %08lx GPR05: %08lx GPR06: %08lx GPR07: %08lx\n",
86 	       regs->gpr[4], regs->gpr[5], regs->gpr[6], regs->gpr[7]);
87 	printk("GPR08: %08lx GPR09: %08lx GPR10: %08lx GPR11: %08lx\n",
88 	       regs->gpr[8], regs->gpr[9], regs->gpr[10], regs->gpr[11]);
89 	printk("GPR12: %08lx GPR13: %08lx GPR14: %08lx GPR15: %08lx\n",
90 	       regs->gpr[12], regs->gpr[13], regs->gpr[14], regs->gpr[15]);
91 	printk("GPR16: %08lx GPR17: %08lx GPR18: %08lx GPR19: %08lx\n",
92 	       regs->gpr[16], regs->gpr[17], regs->gpr[18], regs->gpr[19]);
93 	printk("GPR20: %08lx GPR21: %08lx GPR22: %08lx GPR23: %08lx\n",
94 	       regs->gpr[20], regs->gpr[21], regs->gpr[22], regs->gpr[23]);
95 	printk("GPR24: %08lx GPR25: %08lx GPR26: %08lx GPR27: %08lx\n",
96 	       regs->gpr[24], regs->gpr[25], regs->gpr[26], regs->gpr[27]);
97 	printk("GPR28: %08lx GPR29: %08lx GPR30: %08lx GPR31: %08lx\n",
98 	       regs->gpr[28], regs->gpr[29], regs->gpr[30], regs->gpr[31]);
99 	printk("  RES: %08lx oGPR11: %08lx\n",
100 	       regs->gpr[11], regs->orig_gpr11);
101 
102 	printk("Process %s (pid: %d, stackpage=%08lx)\n",
103 	       current->comm, current->pid, (unsigned long)current);
104 	/*
105 	 * When in-kernel, we also print out the stack and code at the
106 	 * time of the fault..
107 	 */
108 	if (in_kernel) {
109 
110 		printk("\nStack: ");
111 		show_stack(NULL, (unsigned long *)esp);
112 
113 		printk("\nCode: ");
114 		if (regs->pc < PAGE_OFFSET)
115 			goto bad;
116 
117 		for (i = -24; i < 24; i++) {
118 			unsigned char c;
119 			if (__get_user(c, &((unsigned char *)regs->pc)[i])) {
120 bad:
121 				printk(" Bad PC value.");
122 				break;
123 			}
124 
125 			if (i == 0)
126 				printk("(%02x) ", c);
127 			else
128 				printk("%02x ", c);
129 		}
130 	}
131 	printk("\n");
132 }
133 
134 void nommu_dump_state(struct pt_regs *regs,
135 		      unsigned long ea, unsigned long vector)
136 {
137 	int i;
138 	unsigned long addr, stack = regs->sp;
139 
140 	printk("\n\r[nommu_dump_state] :: ea %lx, vector %lx\n\r", ea, vector);
141 
142 	printk("CPU #: %d\n"
143 	       "   PC: %08lx    SR: %08lx    SP: %08lx\n",
144 	       0, regs->pc, regs->sr, regs->sp);
145 	printk("GPR00: %08lx GPR01: %08lx GPR02: %08lx GPR03: %08lx\n",
146 	       0L, regs->gpr[1], regs->gpr[2], regs->gpr[3]);
147 	printk("GPR04: %08lx GPR05: %08lx GPR06: %08lx GPR07: %08lx\n",
148 	       regs->gpr[4], regs->gpr[5], regs->gpr[6], regs->gpr[7]);
149 	printk("GPR08: %08lx GPR09: %08lx GPR10: %08lx GPR11: %08lx\n",
150 	       regs->gpr[8], regs->gpr[9], regs->gpr[10], regs->gpr[11]);
151 	printk("GPR12: %08lx GPR13: %08lx GPR14: %08lx GPR15: %08lx\n",
152 	       regs->gpr[12], regs->gpr[13], regs->gpr[14], regs->gpr[15]);
153 	printk("GPR16: %08lx GPR17: %08lx GPR18: %08lx GPR19: %08lx\n",
154 	       regs->gpr[16], regs->gpr[17], regs->gpr[18], regs->gpr[19]);
155 	printk("GPR20: %08lx GPR21: %08lx GPR22: %08lx GPR23: %08lx\n",
156 	       regs->gpr[20], regs->gpr[21], regs->gpr[22], regs->gpr[23]);
157 	printk("GPR24: %08lx GPR25: %08lx GPR26: %08lx GPR27: %08lx\n",
158 	       regs->gpr[24], regs->gpr[25], regs->gpr[26], regs->gpr[27]);
159 	printk("GPR28: %08lx GPR29: %08lx GPR30: %08lx GPR31: %08lx\n",
160 	       regs->gpr[28], regs->gpr[29], regs->gpr[30], regs->gpr[31]);
161 	printk("  RES: %08lx oGPR11: %08lx\n",
162 	       regs->gpr[11], regs->orig_gpr11);
163 
164 	printk("Process %s (pid: %d, stackpage=%08lx)\n",
165 	       ((struct task_struct *)(__pa(current)))->comm,
166 	       ((struct task_struct *)(__pa(current)))->pid,
167 	       (unsigned long)current);
168 
169 	printk("\nStack: ");
170 	printk("Stack dump [0x%08lx]:\n", (unsigned long)stack);
171 	for (i = 0; i < kstack_depth_to_print; i++) {
172 		if (((long)stack & (THREAD_SIZE - 1)) == 0)
173 			break;
174 		stack++;
175 
176 		printk("%lx :: sp + %02d: 0x%08lx\n", stack, i * 4,
177 		       *((unsigned long *)(__pa(stack))));
178 	}
179 	printk("\n");
180 
181 	printk("Call Trace:   ");
182 	i = 1;
183 	while (((long)stack & (THREAD_SIZE - 1)) != 0) {
184 		addr = *((unsigned long *)__pa(stack));
185 		stack++;
186 
187 		if (kernel_text_address(addr)) {
188 			if (i && ((i % 6) == 0))
189 				printk("\n ");
190 			printk(" [<%08lx>]", addr);
191 			i++;
192 		}
193 	}
194 	printk("\n");
195 
196 	printk("\nCode: ");
197 
198 	for (i = -24; i < 24; i++) {
199 		unsigned char c;
200 		c = ((unsigned char *)(__pa(regs->pc)))[i];
201 
202 		if (i == 0)
203 			printk("(%02x) ", c);
204 		else
205 			printk("%02x ", c);
206 	}
207 	printk("\n");
208 }
209 
210 /* This is normally the 'Oops' routine */
211 void die(const char *str, struct pt_regs *regs, long err)
212 {
213 
214 	console_verbose();
215 	printk("\n%s#: %04lx\n", str, err & 0xffff);
216 	show_registers(regs);
217 #ifdef CONFIG_JUMP_UPON_UNHANDLED_EXCEPTION
218 	printk("\n\nUNHANDLED_EXCEPTION: entering infinite loop\n");
219 
220 	/* shut down interrupts */
221 	local_irq_disable();
222 
223 	__asm__ __volatile__("l.nop   1");
224 	do {} while (1);
225 #endif
226 	do_exit(SIGSEGV);
227 }
228 
229 /* This is normally the 'Oops' routine */
230 void die_if_kernel(const char *str, struct pt_regs *regs, long err)
231 {
232 	if (user_mode(regs))
233 		return;
234 
235 	die(str, regs, err);
236 }
237 
238 void unhandled_exception(struct pt_regs *regs, int ea, int vector)
239 {
240 	printk("Unable to handle exception at EA =0x%x, vector 0x%x",
241 	       ea, vector);
242 	die("Oops", regs, 9);
243 }
244 
245 void __init trap_init(void)
246 {
247 	/* Nothing needs to be done */
248 }
249 
250 asmlinkage void do_trap(struct pt_regs *regs, unsigned long address)
251 {
252 	force_sig_fault(SIGTRAP, TRAP_TRACE, (void __user *)address, current);
253 
254 	regs->pc += 4;
255 }
256 
257 asmlinkage void do_unaligned_access(struct pt_regs *regs, unsigned long address)
258 {
259 	if (user_mode(regs)) {
260 		/* Send a SIGBUS */
261 		force_sig_fault(SIGBUS, BUS_ADRALN, (void __user *)address, current);
262 	} else {
263 		printk("KERNEL: Unaligned Access 0x%.8lx\n", address);
264 		show_registers(regs);
265 		die("Die:", regs, address);
266 	}
267 
268 }
269 
270 asmlinkage void do_bus_fault(struct pt_regs *regs, unsigned long address)
271 {
272 	if (user_mode(regs)) {
273 		/* Send a SIGBUS */
274 		force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address, current);
275 	} else {		/* Kernel mode */
276 		printk("KERNEL: Bus error (SIGBUS) 0x%.8lx\n", address);
277 		show_registers(regs);
278 		die("Die:", regs, address);
279 	}
280 }
281 
282 static inline int in_delay_slot(struct pt_regs *regs)
283 {
284 #ifdef CONFIG_OPENRISC_NO_SPR_SR_DSX
285 	/* No delay slot flag, do the old way */
286 	unsigned int op, insn;
287 
288 	insn = *((unsigned int *)regs->pc);
289 	op = insn >> 26;
290 	switch (op) {
291 	case 0x00: /* l.j */
292 	case 0x01: /* l.jal */
293 	case 0x03: /* l.bnf */
294 	case 0x04: /* l.bf */
295 	case 0x11: /* l.jr */
296 	case 0x12: /* l.jalr */
297 		return 1;
298 	default:
299 		return 0;
300 	}
301 #else
302 	return mfspr(SPR_SR) & SPR_SR_DSX;
303 #endif
304 }
305 
306 static inline void adjust_pc(struct pt_regs *regs, unsigned long address)
307 {
308 	int displacement;
309 	unsigned int rb, op, jmp;
310 
311 	if (unlikely(in_delay_slot(regs))) {
312 		/* In delay slot, instruction at pc is a branch, simulate it */
313 		jmp = *((unsigned int *)regs->pc);
314 
315 		displacement = sign_extend32(((jmp) & 0x3ffffff) << 2, 27);
316 		rb = (jmp & 0x0000ffff) >> 11;
317 		op = jmp >> 26;
318 
319 		switch (op) {
320 		case 0x00: /* l.j */
321 			regs->pc += displacement;
322 			return;
323 		case 0x01: /* l.jal */
324 			regs->pc += displacement;
325 			regs->gpr[9] = regs->pc + 8;
326 			return;
327 		case 0x03: /* l.bnf */
328 			if (regs->sr & SPR_SR_F)
329 				regs->pc += 8;
330 			else
331 				regs->pc += displacement;
332 			return;
333 		case 0x04: /* l.bf */
334 			if (regs->sr & SPR_SR_F)
335 				regs->pc += displacement;
336 			else
337 				regs->pc += 8;
338 			return;
339 		case 0x11: /* l.jr */
340 			regs->pc = regs->gpr[rb];
341 			return;
342 		case 0x12: /* l.jalr */
343 			regs->pc = regs->gpr[rb];
344 			regs->gpr[9] = regs->pc + 8;
345 			return;
346 		default:
347 			break;
348 		}
349 	} else {
350 		regs->pc += 4;
351 	}
352 }
353 
354 static inline void simulate_lwa(struct pt_regs *regs, unsigned long address,
355 				unsigned int insn)
356 {
357 	unsigned int ra, rd;
358 	unsigned long value;
359 	unsigned long orig_pc;
360 	long imm;
361 
362 	const struct exception_table_entry *entry;
363 
364 	orig_pc = regs->pc;
365 	adjust_pc(regs, address);
366 
367 	ra = (insn >> 16) & 0x1f;
368 	rd = (insn >> 21) & 0x1f;
369 	imm = (short)insn;
370 	lwa_addr = (unsigned long __user *)(regs->gpr[ra] + imm);
371 
372 	if ((unsigned long)lwa_addr & 0x3) {
373 		do_unaligned_access(regs, address);
374 		return;
375 	}
376 
377 	if (get_user(value, lwa_addr)) {
378 		if (user_mode(regs)) {
379 			force_sig(SIGSEGV, current);
380 			return;
381 		}
382 
383 		if ((entry = search_exception_tables(orig_pc))) {
384 			regs->pc = entry->fixup;
385 			return;
386 		}
387 
388 		/* kernel access in kernel space, load it directly */
389 		value = *((unsigned long *)lwa_addr);
390 	}
391 
392 	lwa_flag = 1;
393 	regs->gpr[rd] = value;
394 }
395 
396 static inline void simulate_swa(struct pt_regs *regs, unsigned long address,
397 				unsigned int insn)
398 {
399 	unsigned long __user *vaddr;
400 	unsigned long orig_pc;
401 	unsigned int ra, rb;
402 	long imm;
403 
404 	const struct exception_table_entry *entry;
405 
406 	orig_pc = regs->pc;
407 	adjust_pc(regs, address);
408 
409 	ra = (insn >> 16) & 0x1f;
410 	rb = (insn >> 11) & 0x1f;
411 	imm = (short)(((insn & 0x2200000) >> 10) | (insn & 0x7ff));
412 	vaddr = (unsigned long __user *)(regs->gpr[ra] + imm);
413 
414 	if (!lwa_flag || vaddr != lwa_addr) {
415 		regs->sr &= ~SPR_SR_F;
416 		return;
417 	}
418 
419 	if ((unsigned long)vaddr & 0x3) {
420 		do_unaligned_access(regs, address);
421 		return;
422 	}
423 
424 	if (put_user(regs->gpr[rb], vaddr)) {
425 		if (user_mode(regs)) {
426 			force_sig(SIGSEGV, current);
427 			return;
428 		}
429 
430 		if ((entry = search_exception_tables(orig_pc))) {
431 			regs->pc = entry->fixup;
432 			return;
433 		}
434 
435 		/* kernel access in kernel space, store it directly */
436 		*((unsigned long *)vaddr) = regs->gpr[rb];
437 	}
438 
439 	lwa_flag = 0;
440 	regs->sr |= SPR_SR_F;
441 }
442 
443 #define INSN_LWA	0x1b
444 #define INSN_SWA	0x33
445 
446 asmlinkage void do_illegal_instruction(struct pt_regs *regs,
447 				       unsigned long address)
448 {
449 	unsigned int op;
450 	unsigned int insn = *((unsigned int *)address);
451 
452 	op = insn >> 26;
453 
454 	switch (op) {
455 	case INSN_LWA:
456 		simulate_lwa(regs, address, insn);
457 		return;
458 
459 	case INSN_SWA:
460 		simulate_swa(regs, address, insn);
461 		return;
462 
463 	default:
464 		break;
465 	}
466 
467 	if (user_mode(regs)) {
468 		/* Send a SIGILL */
469 		force_sig_fault(SIGILL, ILL_ILLOPC, (void __user *)address, current);
470 	} else {		/* Kernel mode */
471 		printk("KERNEL: Illegal instruction (SIGILL) 0x%.8lx\n",
472 		       address);
473 		show_registers(regs);
474 		die("Die:", regs, address);
475 	}
476 }
477