exceptions.c 4.1 KB
Newer Older
M
Michal Simek 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * HW exception handling
 *
 * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
 * Copyright (C) 2008 PetaLogix
 *
 * This file is subject to the terms and conditions of the GNU General
 * Public License.  See the file COPYING in the main directory of this
 * archive for more details.
 */

/*
 * This file handles the architecture-dependent parts of hardware exceptions
 */

16
#include <linux/export.h>
M
Michal Simek 已提交
17 18 19 20 21 22 23
#include <linux/kernel.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/kallsyms.h>

#include <asm/exceptions.h>
#include <asm/entry.h>		/* For KM CPU var */
24 25 26
#include <linux/uaccess.h>
#include <linux/errno.h>
#include <linux/ptrace.h>
M
Michal Simek 已提交
27
#include <asm/current.h>
28
#include <asm/cacheflush.h>
M
Michal Simek 已提交
29 30 31 32 33 34

#define MICROBLAZE_ILL_OPCODE_EXCEPTION	0x02
#define MICROBLAZE_IBUS_EXCEPTION	0x03
#define MICROBLAZE_DBUS_EXCEPTION	0x04
#define MICROBLAZE_DIV_ZERO_EXCEPTION	0x05
#define MICROBLAZE_FPU_EXCEPTION	0x06
35
#define MICROBLAZE_PRIVILEGED_EXCEPTION	0x07
M
Michal Simek 已提交
36 37 38 39 40 41 42

static DEFINE_SPINLOCK(die_lock);

void die(const char *str, struct pt_regs *fp, long err)
{
	console_verbose();
	spin_lock_irq(&die_lock);
43
	pr_warn("Oops: %s, sig: %ld\n", str, err);
M
Michal Simek 已提交
44 45 46 47 48 49 50 51
	show_regs(fp);
	spin_unlock_irq(&die_lock);
	/* do_exit() should take care of panic'ing from an interrupt
	 * context so we don't handle it here
	 */
	do_exit(err);
}

52
/* for user application debugging */
53
asmlinkage void sw_exception(struct pt_regs *regs)
54 55
{
	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->r16);
56 57
	flush_dcache_range(regs->r16, regs->r16 + 0x4);
	flush_icache_range(regs->r16, regs->r16 + 0x4);
58 59
}

M
Michal Simek 已提交
60 61 62 63
void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
{
	siginfo_t info;

64
	if (kernel_mode(regs))
M
Michal Simek 已提交
65
		die("Exception in kernel mode", regs, signr);
66

M
Michal Simek 已提交
67 68 69 70 71 72 73 74 75 76
	info.si_signo = signr;
	info.si_errno = 0;
	info.si_code = code;
	info.si_addr = (void __user *) addr;
	force_sig_info(signr, &info, current);
}

asmlinkage void full_exception(struct pt_regs *regs, unsigned int type,
							int fsr, int addr)
{
77 78 79 80
#ifdef CONFIG_MMU
	addr = regs->pc;
#endif

M
Michal Simek 已提交
81
#if 0
82
	pr_warn("Exception %02x in %s mode, FSR=%08x PC=%08x ESR=%08x\n",
M
Michal Simek 已提交
83 84 85 86 87 88
			type, user_mode(regs) ? "user" : "kernel", fsr,
			(unsigned int) regs->pc, (unsigned int) regs->esr);
#endif

	switch (type & 0x1F) {
	case MICROBLAZE_ILL_OPCODE_EXCEPTION:
89
		if (user_mode(regs)) {
90
			pr_debug("Illegal opcode exception in user mode\n");
91 92 93
			_exception(SIGILL, regs, ILL_ILLOPC, addr);
			return;
		}
94
		pr_warn("Illegal opcode exception in kernel mode.\n");
95
		die("opcode exception", regs, SIGBUS);
M
Michal Simek 已提交
96 97 98
		break;
	case MICROBLAZE_IBUS_EXCEPTION:
		if (user_mode(regs)) {
99
			pr_debug("Instruction bus error exception in user mode\n");
M
Michal Simek 已提交
100 101 102
			_exception(SIGBUS, regs, BUS_ADRERR, addr);
			return;
		}
103
		pr_warn("Instruction bus error exception in kernel mode.\n");
M
Michal Simek 已提交
104 105 106 107
		die("bus exception", regs, SIGBUS);
		break;
	case MICROBLAZE_DBUS_EXCEPTION:
		if (user_mode(regs)) {
108
			pr_debug("Data bus error exception in user mode\n");
M
Michal Simek 已提交
109 110 111
			_exception(SIGBUS, regs, BUS_ADRERR, addr);
			return;
		}
112
		pr_warn("Data bus error exception in kernel mode.\n");
M
Michal Simek 已提交
113 114 115
		die("bus exception", regs, SIGBUS);
		break;
	case MICROBLAZE_DIV_ZERO_EXCEPTION:
116
		if (user_mode(regs)) {
117
			pr_debug("Divide by zero exception in user mode\n");
118
			_exception(SIGFPE, regs, FPE_INTDIV, addr);
119 120
			return;
		}
121
		pr_warn("Divide by zero exception in kernel mode.\n");
122
		die("Divide by zero exception", regs, SIGBUS);
M
Michal Simek 已提交
123 124
		break;
	case MICROBLAZE_FPU_EXCEPTION:
125
		pr_debug("FPU exception\n");
M
Michal Simek 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
		/* IEEE FP exception */
		/* I removed fsr variable and use code var for storing fsr */
		if (fsr & FSR_IO)
			fsr = FPE_FLTINV;
		else if (fsr & FSR_OF)
			fsr = FPE_FLTOVF;
		else if (fsr & FSR_UF)
			fsr = FPE_FLTUND;
		else if (fsr & FSR_DZ)
			fsr = FPE_FLTDIV;
		else if (fsr & FSR_DO)
			fsr = FPE_FLTRES;
		_exception(SIGFPE, regs, fsr, addr);
		break;

141 142
#ifdef CONFIG_MMU
	case MICROBLAZE_PRIVILEGED_EXCEPTION:
143
		pr_debug("Privileged exception\n");
144
		_exception(SIGILL, regs, ILL_PRVOPC, addr);
145 146
		break;
#endif
M
Michal Simek 已提交
147
	default:
148
	/* FIXME what to do in unexpected exception */
149 150
		pr_warn("Unexpected exception %02x PC=%08x in %s mode\n",
			type, (unsigned int) addr,
M
Michal Simek 已提交
151 152 153 154
			kernel_mode(regs) ? "kernel" : "user");
	}
	return;
}