blob: cf99c411503e3571594a680a01bf0967d8e134b3 [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
Michal Simekd64af912013-02-01 13:10:35 +010016#include <linux/export.h>
Michal Simekc4df4bc2009-03-27 14:25:13 +010017#include <linux/kernel.h>
18#include <linux/signal.h>
19#include <linux/sched.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010020#include <linux/sched/debug.h>
Michal Simekc4df4bc2009-03-27 14:25:13 +010021#include <linux/kallsyms.h>
Michal Simekc4df4bc2009-03-27 14:25:13 +010022
23#include <asm/exceptions.h>
24#include <asm/entry.h> /* For KM CPU var */
Michal Simek4bb73c32009-05-26 16:30:24 +020025#include <linux/uaccess.h>
26#include <linux/errno.h>
27#include <linux/ptrace.h>
Michal Simekc4df4bc2009-03-27 14:25:13 +010028#include <asm/current.h>
Michal Simek17b93142010-11-12 14:27:10 +010029#include <asm/cacheflush.h>
Michal Simekc4df4bc2009-03-27 14:25:13 +010030
31#define MICROBLAZE_ILL_OPCODE_EXCEPTION 0x02
32#define MICROBLAZE_IBUS_EXCEPTION 0x03
33#define MICROBLAZE_DBUS_EXCEPTION 0x04
34#define MICROBLAZE_DIV_ZERO_EXCEPTION 0x05
35#define MICROBLAZE_FPU_EXCEPTION 0x06
Michal Simek4bb73c32009-05-26 16:30:24 +020036#define MICROBLAZE_PRIVILEGED_EXCEPTION 0x07
Michal Simekc4df4bc2009-03-27 14:25:13 +010037
38static DEFINE_SPINLOCK(die_lock);
39
40void die(const char *str, struct pt_regs *fp, long err)
41{
42 console_verbose();
43 spin_lock_irq(&die_lock);
Michal Simek6bd55f02012-12-27 10:40:38 +010044 pr_warn("Oops: %s, sig: %ld\n", str, err);
Michal Simekc4df4bc2009-03-27 14:25:13 +010045 show_regs(fp);
46 spin_unlock_irq(&die_lock);
47 /* do_exit() should take care of panic'ing from an interrupt
48 * context so we don't handle it here
49 */
50 do_exit(err);
51}
52
Michal Simek751f1602010-08-03 11:26:51 +020053/* for user application debugging */
Michal Simekf6999802011-02-07 11:36:25 +010054asmlinkage void sw_exception(struct pt_regs *regs)
Michal Simek751f1602010-08-03 11:26:51 +020055{
56 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->r16);
Michal Simek17b93142010-11-12 14:27:10 +010057 flush_dcache_range(regs->r16, regs->r16 + 0x4);
58 flush_icache_range(regs->r16, regs->r16 + 0x4);
Michal Simek751f1602010-08-03 11:26:51 +020059}
60
Michal Simekc4df4bc2009-03-27 14:25:13 +010061void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
62{
Michal Simek6bd55f02012-12-27 10:40:38 +010063 if (kernel_mode(regs))
Michal Simekc4df4bc2009-03-27 14:25:13 +010064 die("Exception in kernel mode", regs, signr);
Michal Simek6bd55f02012-12-27 10:40:38 +010065
Eric W. Biederman2e1661d22019-05-23 11:04:24 -050066 force_sig_fault(signr, code, (void __user *)addr);
Michal Simekc4df4bc2009-03-27 14:25:13 +010067}
68
69asmlinkage void full_exception(struct pt_regs *regs, unsigned int type,
70 int fsr, int addr)
71{
Michal Simek4bb73c32009-05-26 16:30:24 +020072#ifdef CONFIG_MMU
Michal Simek4bb73c32009-05-26 16:30:24 +020073 addr = regs->pc;
74#endif
75
Michal Simekc4df4bc2009-03-27 14:25:13 +010076#if 0
Michal Simek6bd55f02012-12-27 10:40:38 +010077 pr_warn("Exception %02x in %s mode, FSR=%08x PC=%08x ESR=%08x\n",
Michal Simekc4df4bc2009-03-27 14:25:13 +010078 type, user_mode(regs) ? "user" : "kernel", fsr,
79 (unsigned int) regs->pc, (unsigned int) regs->esr);
80#endif
81
82 switch (type & 0x1F) {
83 case MICROBLAZE_ILL_OPCODE_EXCEPTION:
Michal Simek4bb73c32009-05-26 16:30:24 +020084 if (user_mode(regs)) {
Joe Perchesc4554c32010-09-11 22:10:51 -070085 pr_debug("Illegal opcode exception in user mode\n");
Michal Simek4bb73c32009-05-26 16:30:24 +020086 _exception(SIGILL, regs, ILL_ILLOPC, addr);
87 return;
88 }
Michal Simek6bd55f02012-12-27 10:40:38 +010089 pr_warn("Illegal opcode exception in kernel mode.\n");
Michal Simek4bb73c32009-05-26 16:30:24 +020090 die("opcode exception", regs, SIGBUS);
Michal Simekc4df4bc2009-03-27 14:25:13 +010091 break;
92 case MICROBLAZE_IBUS_EXCEPTION:
93 if (user_mode(regs)) {
Joe Perchesc4554c32010-09-11 22:10:51 -070094 pr_debug("Instruction bus error exception in user mode\n");
Michal Simekc4df4bc2009-03-27 14:25:13 +010095 _exception(SIGBUS, regs, BUS_ADRERR, addr);
96 return;
97 }
Michal Simek6bd55f02012-12-27 10:40:38 +010098 pr_warn("Instruction bus error exception in kernel mode.\n");
Michal Simekc4df4bc2009-03-27 14:25:13 +010099 die("bus exception", regs, SIGBUS);
100 break;
101 case MICROBLAZE_DBUS_EXCEPTION:
102 if (user_mode(regs)) {
Joe Perchesc4554c32010-09-11 22:10:51 -0700103 pr_debug("Data bus error exception in user mode\n");
Michal Simekc4df4bc2009-03-27 14:25:13 +0100104 _exception(SIGBUS, regs, BUS_ADRERR, addr);
105 return;
106 }
Michal Simek6bd55f02012-12-27 10:40:38 +0100107 pr_warn("Data bus error exception in kernel mode.\n");
Michal Simekc4df4bc2009-03-27 14:25:13 +0100108 die("bus exception", regs, SIGBUS);
109 break;
110 case MICROBLAZE_DIV_ZERO_EXCEPTION:
Michal Simek4bb73c32009-05-26 16:30:24 +0200111 if (user_mode(regs)) {
Joe Perchesc4554c32010-09-11 22:10:51 -0700112 pr_debug("Divide by zero exception in user mode\n");
Edgar E. Iglesias15ec0902011-08-22 19:58:06 +0200113 _exception(SIGFPE, regs, FPE_INTDIV, addr);
Michal Simek4bb73c32009-05-26 16:30:24 +0200114 return;
115 }
Michal Simek6bd55f02012-12-27 10:40:38 +0100116 pr_warn("Divide by zero exception in kernel mode.\n");
Randy Dunlapf3ff8212010-04-21 14:11:34 -0700117 die("Divide by zero exception", regs, SIGBUS);
Michal Simekc4df4bc2009-03-27 14:25:13 +0100118 break;
Michal Simekc4df4bc2009-03-27 14:25:13 +0100119 case MICROBLAZE_FPU_EXCEPTION:
Joe Perchesc4554c32010-09-11 22:10:51 -0700120 pr_debug("FPU exception\n");
Michal Simekc4df4bc2009-03-27 14:25:13 +0100121 /* IEEE FP exception */
122 /* I removed fsr variable and use code var for storing fsr */
123 if (fsr & FSR_IO)
124 fsr = FPE_FLTINV;
125 else if (fsr & FSR_OF)
126 fsr = FPE_FLTOVF;
127 else if (fsr & FSR_UF)
128 fsr = FPE_FLTUND;
129 else if (fsr & FSR_DZ)
130 fsr = FPE_FLTDIV;
131 else if (fsr & FSR_DO)
132 fsr = FPE_FLTRES;
133 _exception(SIGFPE, regs, fsr, addr);
134 break;
135
Michal Simek4bb73c32009-05-26 16:30:24 +0200136#ifdef CONFIG_MMU
137 case MICROBLAZE_PRIVILEGED_EXCEPTION:
Joe Perchesc4554c32010-09-11 22:10:51 -0700138 pr_debug("Privileged exception\n");
Michal Simek04256092010-08-03 11:32:20 +0200139 _exception(SIGILL, regs, ILL_PRVOPC, addr);
Michal Simek4bb73c32009-05-26 16:30:24 +0200140 break;
141#endif
Michal Simekc4df4bc2009-03-27 14:25:13 +0100142 default:
Michal Simek4bb73c32009-05-26 16:30:24 +0200143 /* FIXME what to do in unexpected exception */
Michal Simek6bd55f02012-12-27 10:40:38 +0100144 pr_warn("Unexpected exception %02x PC=%08x in %s mode\n",
145 type, (unsigned int) addr,
Michal Simekc4df4bc2009-03-27 14:25:13 +0100146 kernel_mode(regs) ? "kernel" : "user");
147 }
148 return;
149}