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