blob: e0c6f8c2bd9d4f1fdf746926756de75de2405fa0 [file] [log] [blame]
Michal Simekc4df4bc2009-03-27 14:25:13 +01001/*
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 */
Michal Simek4bb73c32009-05-26 16:30:24 +020024#include <linux/uaccess.h>
25#include <linux/errno.h>
26#include <linux/ptrace.h>
Michal Simekc4df4bc2009-03-27 14:25:13 +010027#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
Michal Simek4bb73c32009-05-26 16:30:24 +020034#define MICROBLAZE_PRIVILEGED_EXCEPTION 0x07
Michal Simekc4df4bc2009-03-27 14:25:13 +010035
36static DEFINE_SPINLOCK(die_lock);
37
38void 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
Michal Simek751f1602010-08-03 11:26:51 +020051/* for user application debugging */
52void sw_exception(struct pt_regs *regs)
53{
54 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->r16);
55}
56
Michal Simekc4df4bc2009-03-27 14:25:13 +010057void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
58{
59 siginfo_t info;
60
61 if (kernel_mode(regs)) {
62 debugger(regs);
63 die("Exception in kernel mode", regs, signr);
64 }
65 info.si_signo = signr;
66 info.si_errno = 0;
67 info.si_code = code;
68 info.si_addr = (void __user *) addr;
69 force_sig_info(signr, &info, current);
70}
71
72asmlinkage void full_exception(struct pt_regs *regs, unsigned int type,
73 int fsr, int addr)
74{
Michal Simek4bb73c32009-05-26 16:30:24 +020075#ifdef CONFIG_MMU
76 int code;
77 addr = regs->pc;
78#endif
79
Michal Simekc4df4bc2009-03-27 14:25:13 +010080#if 0
Michal Simekbfc32ad2009-09-15 09:49:53 +020081 printk(KERN_WARNING "Exception %02x in %s mode, FSR=%08x PC=%08x " \
82 "ESR=%08x\n",
Michal Simekc4df4bc2009-03-27 14:25:13 +010083 type, user_mode(regs) ? "user" : "kernel", fsr,
84 (unsigned int) regs->pc, (unsigned int) regs->esr);
85#endif
86
87 switch (type & 0x1F) {
88 case MICROBLAZE_ILL_OPCODE_EXCEPTION:
Michal Simek4bb73c32009-05-26 16:30:24 +020089 if (user_mode(regs)) {
Michal Simekbfc32ad2009-09-15 09:49:53 +020090 pr_debug(KERN_WARNING "Illegal opcode exception " \
91 "in user mode.\n");
Michal Simek4bb73c32009-05-26 16:30:24 +020092 _exception(SIGILL, regs, ILL_ILLOPC, addr);
93 return;
94 }
Michal Simekbfc32ad2009-09-15 09:49:53 +020095 printk(KERN_WARNING "Illegal opcode exception " \
96 "in kernel mode.\n");
Michal Simek4bb73c32009-05-26 16:30:24 +020097 die("opcode exception", regs, SIGBUS);
Michal Simekc4df4bc2009-03-27 14:25:13 +010098 break;
99 case MICROBLAZE_IBUS_EXCEPTION:
100 if (user_mode(regs)) {
Michal Simekbfc32ad2009-09-15 09:49:53 +0200101 pr_debug(KERN_WARNING "Instruction bus error " \
102 "exception in user mode.\n");
Michal Simekc4df4bc2009-03-27 14:25:13 +0100103 _exception(SIGBUS, regs, BUS_ADRERR, addr);
104 return;
105 }
Michal Simekbfc32ad2009-09-15 09:49:53 +0200106 printk(KERN_WARNING "Instruction bus error exception " \
107 "in kernel mode.\n");
Michal Simekc4df4bc2009-03-27 14:25:13 +0100108 die("bus exception", regs, SIGBUS);
109 break;
110 case MICROBLAZE_DBUS_EXCEPTION:
111 if (user_mode(regs)) {
Michal Simekbfc32ad2009-09-15 09:49:53 +0200112 pr_debug(KERN_WARNING "Data bus error exception " \
113 "in user mode.\n");
Michal Simekc4df4bc2009-03-27 14:25:13 +0100114 _exception(SIGBUS, regs, BUS_ADRERR, addr);
115 return;
116 }
Michal Simekbfc32ad2009-09-15 09:49:53 +0200117 printk(KERN_WARNING "Data bus error exception " \
118 "in kernel mode.\n");
Michal Simekc4df4bc2009-03-27 14:25:13 +0100119 die("bus exception", regs, SIGBUS);
120 break;
121 case MICROBLAZE_DIV_ZERO_EXCEPTION:
Michal Simek4bb73c32009-05-26 16:30:24 +0200122 if (user_mode(regs)) {
Michal Simekbfc32ad2009-09-15 09:49:53 +0200123 pr_debug(KERN_WARNING "Divide by zero exception " \
124 "in user mode\n");
Michal Simek23902d92009-09-22 08:58:47 +0200125 _exception(SIGILL, regs, FPE_INTDIV, addr);
Michal Simek4bb73c32009-05-26 16:30:24 +0200126 return;
127 }
Michal Simekbfc32ad2009-09-15 09:49:53 +0200128 printk(KERN_WARNING "Divide by zero exception " \
129 "in kernel mode.\n");
Randy Dunlapf3ff8212010-04-21 14:11:34 -0700130 die("Divide by zero exception", regs, SIGBUS);
Michal Simekc4df4bc2009-03-27 14:25:13 +0100131 break;
Michal Simekc4df4bc2009-03-27 14:25:13 +0100132 case MICROBLAZE_FPU_EXCEPTION:
Michal Simekbfc32ad2009-09-15 09:49:53 +0200133 pr_debug(KERN_WARNING "FPU exception\n");
Michal Simekc4df4bc2009-03-27 14:25:13 +0100134 /* IEEE FP exception */
135 /* I removed fsr variable and use code var for storing fsr */
136 if (fsr & FSR_IO)
137 fsr = FPE_FLTINV;
138 else if (fsr & FSR_OF)
139 fsr = FPE_FLTOVF;
140 else if (fsr & FSR_UF)
141 fsr = FPE_FLTUND;
142 else if (fsr & FSR_DZ)
143 fsr = FPE_FLTDIV;
144 else if (fsr & FSR_DO)
145 fsr = FPE_FLTRES;
146 _exception(SIGFPE, regs, fsr, addr);
147 break;
148
Michal Simek4bb73c32009-05-26 16:30:24 +0200149#ifdef CONFIG_MMU
150 case MICROBLAZE_PRIVILEGED_EXCEPTION:
Michal Simekbfc32ad2009-09-15 09:49:53 +0200151 pr_debug(KERN_WARNING "Privileged exception\n");
Michal Simek751f1602010-08-03 11:26:51 +0200152 /* "brk r0,r0" - used as debug breakpoint - old toolchain */
Michal Simek4bb73c32009-05-26 16:30:24 +0200153 if (get_user(code, (unsigned long *)regs->pc) == 0
154 && code == 0x980c0000) {
155 _exception(SIGTRAP, regs, TRAP_BRKPT, addr);
156 } else {
157 _exception(SIGILL, regs, ILL_PRVOPC, addr);
158 }
159 break;
160#endif
Michal Simekc4df4bc2009-03-27 14:25:13 +0100161 default:
Michal Simek4bb73c32009-05-26 16:30:24 +0200162 /* FIXME what to do in unexpected exception */
Michal Simekc4df4bc2009-03-27 14:25:13 +0100163 printk(KERN_WARNING "Unexpected exception %02x "
164 "PC=%08x in %s mode\n", type, (unsigned int) addr,
165 kernel_mode(regs) ? "kernel" : "user");
166 }
167 return;
168}