1c4df4bc1SMichal Simek /*
2c4df4bc1SMichal Simek  * HW exception handling
3c4df4bc1SMichal Simek  *
4c4df4bc1SMichal Simek  * Copyright (C) 2008-2009 Michal Simek <[email protected]>
5c4df4bc1SMichal Simek  * Copyright (C) 2008 PetaLogix
6c4df4bc1SMichal Simek  *
7c4df4bc1SMichal Simek  * This file is subject to the terms and conditions of the GNU General
8c4df4bc1SMichal Simek  * Public License.  See the file COPYING in the main directory of this
9c4df4bc1SMichal Simek  * archive for more details.
10c4df4bc1SMichal Simek  */
11c4df4bc1SMichal Simek 
12c4df4bc1SMichal Simek /*
13c4df4bc1SMichal Simek  * This file handles the architecture-dependent parts of hardware exceptions
14c4df4bc1SMichal Simek  */
15c4df4bc1SMichal Simek 
16d64af918SMichal Simek #include <linux/export.h>
17c4df4bc1SMichal Simek #include <linux/kernel.h>
18c4df4bc1SMichal Simek #include <linux/signal.h>
19c4df4bc1SMichal Simek #include <linux/sched.h>
20b17b0153SIngo Molnar #include <linux/sched/debug.h>
21c4df4bc1SMichal Simek #include <linux/kallsyms.h>
22c4df4bc1SMichal Simek 
23c4df4bc1SMichal Simek #include <asm/exceptions.h>
24c4df4bc1SMichal Simek #include <asm/entry.h>		/* For KM CPU var */
254bb73c3dSMichal Simek #include <linux/uaccess.h>
264bb73c3dSMichal Simek #include <linux/errno.h>
274bb73c3dSMichal Simek #include <linux/ptrace.h>
28c4df4bc1SMichal Simek #include <asm/current.h>
2917b93146SMichal Simek #include <asm/cacheflush.h>
30c4df4bc1SMichal Simek 
31c4df4bc1SMichal Simek #define MICROBLAZE_ILL_OPCODE_EXCEPTION	0x02
32c4df4bc1SMichal Simek #define MICROBLAZE_IBUS_EXCEPTION	0x03
33c4df4bc1SMichal Simek #define MICROBLAZE_DBUS_EXCEPTION	0x04
34c4df4bc1SMichal Simek #define MICROBLAZE_DIV_ZERO_EXCEPTION	0x05
35c4df4bc1SMichal Simek #define MICROBLAZE_FPU_EXCEPTION	0x06
364bb73c3dSMichal Simek #define MICROBLAZE_PRIVILEGED_EXCEPTION	0x07
37c4df4bc1SMichal Simek 
38c4df4bc1SMichal Simek static DEFINE_SPINLOCK(die_lock);
39c4df4bc1SMichal Simek 
die(const char * str,struct pt_regs * fp,long err)40c4df4bc1SMichal Simek void die(const char *str, struct pt_regs *fp, long err)
41c4df4bc1SMichal Simek {
42c4df4bc1SMichal Simek 	console_verbose();
43c4df4bc1SMichal Simek 	spin_lock_irq(&die_lock);
446bd55f0bSMichal Simek 	pr_warn("Oops: %s, sig: %ld\n", str, err);
45c4df4bc1SMichal Simek 	show_regs(fp);
46c4df4bc1SMichal Simek 	spin_unlock_irq(&die_lock);
47*0e25498fSEric W. Biederman 	/* make_task_dead() should take care of panic'ing from an interrupt
48c4df4bc1SMichal Simek 	 * context so we don't handle it here
49c4df4bc1SMichal Simek 	 */
50*0e25498fSEric W. Biederman 	make_task_dead(err);
51c4df4bc1SMichal Simek }
52c4df4bc1SMichal Simek 
53751f1605SMichal Simek /* for user application debugging */
sw_exception(struct pt_regs * regs)54f699980bSMichal Simek asmlinkage void sw_exception(struct pt_regs *regs)
55751f1605SMichal Simek {
56751f1605SMichal Simek 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->r16);
5717b93146SMichal Simek 	flush_dcache_range(regs->r16, regs->r16 + 0x4);
5817b93146SMichal Simek 	flush_icache_range(regs->r16, regs->r16 + 0x4);
59751f1605SMichal Simek }
60751f1605SMichal Simek 
_exception(int signr,struct pt_regs * regs,int code,unsigned long addr)61c4df4bc1SMichal Simek void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
62c4df4bc1SMichal Simek {
636bd55f0bSMichal Simek 	if (kernel_mode(regs))
64c4df4bc1SMichal Simek 		die("Exception in kernel mode", regs, signr);
656bd55f0bSMichal Simek 
662e1661d2SEric W. Biederman 	force_sig_fault(signr, code, (void __user *)addr);
67c4df4bc1SMichal Simek }
68c4df4bc1SMichal Simek 
full_exception(struct pt_regs * regs,unsigned int type,int fsr,int addr)69c4df4bc1SMichal Simek asmlinkage void full_exception(struct pt_regs *regs, unsigned int type,
70c4df4bc1SMichal Simek 							int fsr, int addr)
71c4df4bc1SMichal Simek {
724bb73c3dSMichal Simek 	addr = regs->pc;
734bb73c3dSMichal Simek 
74c4df4bc1SMichal Simek #if 0
756bd55f0bSMichal Simek 	pr_warn("Exception %02x in %s mode, FSR=%08x PC=%08x ESR=%08x\n",
76c4df4bc1SMichal Simek 			type, user_mode(regs) ? "user" : "kernel", fsr,
77c4df4bc1SMichal Simek 			(unsigned int) regs->pc, (unsigned int) regs->esr);
78c4df4bc1SMichal Simek #endif
79c4df4bc1SMichal Simek 
80c4df4bc1SMichal Simek 	switch (type & 0x1F) {
81c4df4bc1SMichal Simek 	case MICROBLAZE_ILL_OPCODE_EXCEPTION:
824bb73c3dSMichal Simek 		if (user_mode(regs)) {
83c4554c32SJoe Perches 			pr_debug("Illegal opcode exception in user mode\n");
84c4df4bc1SMichal Simek 			_exception(SIGILL, regs, ILL_ILLOPC, addr);
854bb73c3dSMichal Simek 			return;
864bb73c3dSMichal Simek 		}
876bd55f0bSMichal Simek 		pr_warn("Illegal opcode exception in kernel mode.\n");
884bb73c3dSMichal Simek 		die("opcode exception", regs, SIGBUS);
89c4df4bc1SMichal Simek 		break;
90c4df4bc1SMichal Simek 	case MICROBLAZE_IBUS_EXCEPTION:
91c4df4bc1SMichal Simek 		if (user_mode(regs)) {
92c4554c32SJoe Perches 			pr_debug("Instruction bus error exception in user mode\n");
93c4df4bc1SMichal Simek 			_exception(SIGBUS, regs, BUS_ADRERR, addr);
94c4df4bc1SMichal Simek 			return;
95c4df4bc1SMichal Simek 		}
966bd55f0bSMichal Simek 		pr_warn("Instruction bus error exception in kernel mode.\n");
97c4df4bc1SMichal Simek 		die("bus exception", regs, SIGBUS);
98c4df4bc1SMichal Simek 		break;
99c4df4bc1SMichal Simek 	case MICROBLAZE_DBUS_EXCEPTION:
100c4df4bc1SMichal Simek 		if (user_mode(regs)) {
101c4554c32SJoe Perches 			pr_debug("Data bus error exception in user mode\n");
102c4df4bc1SMichal Simek 			_exception(SIGBUS, regs, BUS_ADRERR, addr);
103c4df4bc1SMichal Simek 			return;
104c4df4bc1SMichal Simek 		}
1056bd55f0bSMichal Simek 		pr_warn("Data bus error exception in kernel mode.\n");
106c4df4bc1SMichal Simek 		die("bus exception", regs, SIGBUS);
107c4df4bc1SMichal Simek 		break;
108c4df4bc1SMichal Simek 	case MICROBLAZE_DIV_ZERO_EXCEPTION:
1094bb73c3dSMichal Simek 		if (user_mode(regs)) {
110c4554c32SJoe Perches 			pr_debug("Divide by zero exception in user mode\n");
11115ec0908SEdgar E. Iglesias 			_exception(SIGFPE, regs, FPE_INTDIV, addr);
1124bb73c3dSMichal Simek 			return;
1134bb73c3dSMichal Simek 		}
1146bd55f0bSMichal Simek 		pr_warn("Divide by zero exception in kernel mode.\n");
115f3ff8212SRandy Dunlap 		die("Divide by zero exception", regs, SIGBUS);
116c4df4bc1SMichal Simek 		break;
117c4df4bc1SMichal Simek 	case MICROBLAZE_FPU_EXCEPTION:
118c4554c32SJoe Perches 		pr_debug("FPU exception\n");
119c4df4bc1SMichal Simek 		/* IEEE FP exception */
120c4df4bc1SMichal Simek 		/* I removed fsr variable and use code var for storing fsr */
121c4df4bc1SMichal Simek 		if (fsr & FSR_IO)
122c4df4bc1SMichal Simek 			fsr = FPE_FLTINV;
123c4df4bc1SMichal Simek 		else if (fsr & FSR_OF)
124c4df4bc1SMichal Simek 			fsr = FPE_FLTOVF;
125c4df4bc1SMichal Simek 		else if (fsr & FSR_UF)
126c4df4bc1SMichal Simek 			fsr = FPE_FLTUND;
127c4df4bc1SMichal Simek 		else if (fsr & FSR_DZ)
128c4df4bc1SMichal Simek 			fsr = FPE_FLTDIV;
129c4df4bc1SMichal Simek 		else if (fsr & FSR_DO)
130c4df4bc1SMichal Simek 			fsr = FPE_FLTRES;
131c4df4bc1SMichal Simek 		_exception(SIGFPE, regs, fsr, addr);
132c4df4bc1SMichal Simek 		break;
1334bb73c3dSMichal Simek 	case MICROBLAZE_PRIVILEGED_EXCEPTION:
134c4554c32SJoe Perches 		pr_debug("Privileged exception\n");
1354bb73c3dSMichal Simek 		_exception(SIGILL, regs, ILL_PRVOPC, addr);
1364bb73c3dSMichal Simek 		break;
137c4df4bc1SMichal Simek 	default:
1384bb73c3dSMichal Simek 	/* FIXME what to do in unexpected exception */
1396bd55f0bSMichal Simek 		pr_warn("Unexpected exception %02x PC=%08x in %s mode\n",
1406bd55f0bSMichal Simek 			type, (unsigned int) addr,
141c4df4bc1SMichal Simek 			kernel_mode(regs) ? "kernel" : "user");
142c4df4bc1SMichal Simek 	}
143c4df4bc1SMichal Simek 	return;
144c4df4bc1SMichal Simek }
145