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 <linux/uaccess.h>
25 #include <linux/errno.h>
26 #include <linux/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_PRIVILEGED_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 #ifdef CONFIG_MMU
70 	int code;
71 	addr = regs->pc;
72 #endif
73 
74 #if 0
75 	printk(KERN_WARNING "Exception %02x in %s mode, FSR=%08x PC=%08x " \
76 							"ESR=%08x\n",
77 			type, user_mode(regs) ? "user" : "kernel", fsr,
78 			(unsigned int) regs->pc, (unsigned int) regs->esr);
79 #endif
80 
81 	switch (type & 0x1F) {
82 	case MICROBLAZE_ILL_OPCODE_EXCEPTION:
83 		if (user_mode(regs)) {
84 			pr_debug(KERN_WARNING "Illegal opcode exception " \
85 							"in user mode.\n");
86 			_exception(SIGILL, regs, ILL_ILLOPC, addr);
87 			return;
88 		}
89 		printk(KERN_WARNING "Illegal opcode exception " \
90 							"in kernel mode.\n");
91 		die("opcode exception", regs, SIGBUS);
92 		break;
93 	case MICROBLAZE_IBUS_EXCEPTION:
94 		if (user_mode(regs)) {
95 			pr_debug(KERN_WARNING "Instruction bus error " \
96 						"exception in user mode.\n");
97 			_exception(SIGBUS, regs, BUS_ADRERR, addr);
98 			return;
99 		}
100 		printk(KERN_WARNING "Instruction bus error exception " \
101 							"in kernel mode.\n");
102 		die("bus exception", regs, SIGBUS);
103 		break;
104 	case MICROBLAZE_DBUS_EXCEPTION:
105 		if (user_mode(regs)) {
106 			pr_debug(KERN_WARNING "Data bus error exception " \
107 							"in user mode.\n");
108 			_exception(SIGBUS, regs, BUS_ADRERR, addr);
109 			return;
110 		}
111 		printk(KERN_WARNING "Data bus error exception " \
112 							"in kernel mode.\n");
113 		die("bus exception", regs, SIGBUS);
114 		break;
115 	case MICROBLAZE_DIV_ZERO_EXCEPTION:
116 		if (user_mode(regs)) {
117 			pr_debug(KERN_WARNING "Divide by zero exception " \
118 							"in user mode\n");
119 			_exception(SIGILL, regs, FPE_INTDIV, addr);
120 			return;
121 		}
122 		printk(KERN_WARNING "Divide by zero exception " \
123 							"in kernel mode.\n");
124 		die("Divide by exception", regs, SIGBUS);
125 		break;
126 	case MICROBLAZE_FPU_EXCEPTION:
127 		pr_debug(KERN_WARNING "FPU exception\n");
128 		/* IEEE FP exception */
129 		/* I removed fsr variable and use code var for storing fsr */
130 		if (fsr & FSR_IO)
131 			fsr = FPE_FLTINV;
132 		else if (fsr & FSR_OF)
133 			fsr = FPE_FLTOVF;
134 		else if (fsr & FSR_UF)
135 			fsr = FPE_FLTUND;
136 		else if (fsr & FSR_DZ)
137 			fsr = FPE_FLTDIV;
138 		else if (fsr & FSR_DO)
139 			fsr = FPE_FLTRES;
140 		_exception(SIGFPE, regs, fsr, addr);
141 		break;
142 
143 #ifdef CONFIG_MMU
144 	case MICROBLAZE_PRIVILEGED_EXCEPTION:
145 		pr_debug(KERN_WARNING "Privileged exception\n");
146 		/* "brk r0,r0" - used as debug breakpoint */
147 		if (get_user(code, (unsigned long *)regs->pc) == 0
148 			&& code == 0x980c0000) {
149 			_exception(SIGTRAP, regs, TRAP_BRKPT, addr);
150 		} else {
151 			_exception(SIGILL, regs, ILL_PRVOPC, addr);
152 		}
153 		break;
154 #endif
155 	default:
156 	/* FIXME what to do in unexpected exception */
157 		printk(KERN_WARNING "Unexpected exception %02x "
158 			"PC=%08x in %s mode\n", type, (unsigned int) addr,
159 			kernel_mode(regs) ? "kernel" : "user");
160 	}
161 	return;
162 }
163