1 /*
2  * HW exception handling
3  *
4  * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
5  * Copyright (C) 2008 PetaLogix
6  *
7  * This file is subject to the terms and conditions of the GNU General
8  * Public License.  See the file COPYING in the main directory of this
9  * archive for more details.
10  */
11 
12 /*
13  * This file handles the architecture-dependent parts of hardware exceptions
14  */
15 
16 #include <linux/kernel.h>
17 #include <linux/signal.h>
18 #include <linux/sched.h>
19 #include <linux/kallsyms.h>
20 #include <linux/module.h>
21 
22 #include <asm/exceptions.h>
23 #include <asm/entry.h>		/* For KM CPU var */
24 #include <asm/uaccess.h>
25 #include <asm/errno.h>
26 #include <asm/ptrace.h>
27 #include <asm/current.h>
28 
29 #define MICROBLAZE_ILL_OPCODE_EXCEPTION	0x02
30 #define MICROBLAZE_IBUS_EXCEPTION	0x03
31 #define MICROBLAZE_DBUS_EXCEPTION	0x04
32 #define MICROBLAZE_DIV_ZERO_EXCEPTION	0x05
33 #define MICROBLAZE_FPU_EXCEPTION	0x06
34 #define MICROBLAZE_PRIVILEG_EXCEPTION	0x07
35 
36 static DEFINE_SPINLOCK(die_lock);
37 
38 void die(const char *str, struct pt_regs *fp, long err)
39 {
40 	console_verbose();
41 	spin_lock_irq(&die_lock);
42 	printk(KERN_WARNING "Oops: %s, sig: %ld\n", str, err);
43 	show_regs(fp);
44 	spin_unlock_irq(&die_lock);
45 	/* do_exit() should take care of panic'ing from an interrupt
46 	 * context so we don't handle it here
47 	 */
48 	do_exit(err);
49 }
50 
51 void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
52 {
53 	siginfo_t info;
54 
55 	if (kernel_mode(regs)) {
56 		debugger(regs);
57 		die("Exception in kernel mode", regs, signr);
58 	}
59 	info.si_signo = signr;
60 	info.si_errno = 0;
61 	info.si_code = code;
62 	info.si_addr = (void __user *) addr;
63 	force_sig_info(signr, &info, current);
64 }
65 
66 asmlinkage void full_exception(struct pt_regs *regs, unsigned int type,
67 							int fsr, int addr)
68 {
69 #if 0
70 	printk(KERN_WARNING "Exception %02x in %s mode, FSR=%08x PC=%08x ESR=%08x\n",
71 			type, user_mode(regs) ? "user" : "kernel", fsr,
72 			(unsigned int) regs->pc, (unsigned int) regs->esr);
73 #endif
74 
75 	switch (type & 0x1F) {
76 	case MICROBLAZE_ILL_OPCODE_EXCEPTION:
77 		_exception(SIGILL, regs, ILL_ILLOPC, addr);
78 		break;
79 	case MICROBLAZE_IBUS_EXCEPTION:
80 		if (user_mode(regs)) {
81 			printk(KERN_WARNING "Instruction bus error exception in user mode.\n");
82 			_exception(SIGBUS, regs, BUS_ADRERR, addr);
83 			return;
84 		}
85 		printk(KERN_WARNING "Instruction bus error exception in kernel mode.\n");
86 		die("bus exception", regs, SIGBUS);
87 		break;
88 	case MICROBLAZE_DBUS_EXCEPTION:
89 		if (user_mode(regs)) {
90 			printk(KERN_WARNING "Data bus error exception in user mode.\n");
91 			_exception(SIGBUS, regs, BUS_ADRERR, addr);
92 			return;
93 		}
94 		printk(KERN_WARNING "Data bus error exception in kernel mode.\n");
95 		die("bus exception", regs, SIGBUS);
96 		break;
97 	case MICROBLAZE_DIV_ZERO_EXCEPTION:
98 		printk(KERN_WARNING "Divide by zero exception\n");
99 		_exception(SIGILL, regs, ILL_ILLOPC, addr);
100 		break;
101 
102 	case MICROBLAZE_FPU_EXCEPTION:
103 		/* IEEE FP exception */
104 		/* I removed fsr variable and use code var for storing fsr */
105 		if (fsr & FSR_IO)
106 			fsr = FPE_FLTINV;
107 		else if (fsr & FSR_OF)
108 			fsr = FPE_FLTOVF;
109 		else if (fsr & FSR_UF)
110 			fsr = FPE_FLTUND;
111 		else if (fsr & FSR_DZ)
112 			fsr = FPE_FLTDIV;
113 		else if (fsr & FSR_DO)
114 			fsr = FPE_FLTRES;
115 		_exception(SIGFPE, regs, fsr, addr);
116 		break;
117 
118 	default:
119 		printk(KERN_WARNING "Unexpected exception %02x "
120 			"PC=%08x in %s mode\n", type, (unsigned int) addr,
121 			kernel_mode(regs) ? "kernel" : "user");
122 	}
123 	return;
124 }
125