Michal Simek | c4df4bc | 2009-03-27 14:25:13 +0100 | [diff] [blame] | 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 | |
Michal Simek | d64af91 | 2013-02-01 13:10:35 +0100 | [diff] [blame] | 16 | #include <linux/export.h> |
Michal Simek | c4df4bc | 2009-03-27 14:25:13 +0100 | [diff] [blame] | 17 | #include <linux/kernel.h> |
| 18 | #include <linux/signal.h> |
| 19 | #include <linux/sched.h> |
Ingo Molnar | b17b015 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 20 | #include <linux/sched/debug.h> |
Michal Simek | c4df4bc | 2009-03-27 14:25:13 +0100 | [diff] [blame] | 21 | #include <linux/kallsyms.h> |
Michal Simek | c4df4bc | 2009-03-27 14:25:13 +0100 | [diff] [blame] | 22 | |
| 23 | #include <asm/exceptions.h> |
| 24 | #include <asm/entry.h> /* For KM CPU var */ |
Michal Simek | 4bb73c3 | 2009-05-26 16:30:24 +0200 | [diff] [blame] | 25 | #include <linux/uaccess.h> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/ptrace.h> |
Michal Simek | c4df4bc | 2009-03-27 14:25:13 +0100 | [diff] [blame] | 28 | #include <asm/current.h> |
Michal Simek | 17b9314 | 2010-11-12 14:27:10 +0100 | [diff] [blame] | 29 | #include <asm/cacheflush.h> |
Michal Simek | c4df4bc | 2009-03-27 14:25:13 +0100 | [diff] [blame] | 30 | |
| 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 Simek | 4bb73c3 | 2009-05-26 16:30:24 +0200 | [diff] [blame] | 36 | #define MICROBLAZE_PRIVILEGED_EXCEPTION 0x07 |
Michal Simek | c4df4bc | 2009-03-27 14:25:13 +0100 | [diff] [blame] | 37 | |
| 38 | static DEFINE_SPINLOCK(die_lock); |
| 39 | |
| 40 | void die(const char *str, struct pt_regs *fp, long err) |
| 41 | { |
| 42 | console_verbose(); |
| 43 | spin_lock_irq(&die_lock); |
Michal Simek | 6bd55f0 | 2012-12-27 10:40:38 +0100 | [diff] [blame] | 44 | pr_warn("Oops: %s, sig: %ld\n", str, err); |
Michal Simek | c4df4bc | 2009-03-27 14:25:13 +0100 | [diff] [blame] | 45 | 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 Simek | 751f160 | 2010-08-03 11:26:51 +0200 | [diff] [blame] | 53 | /* for user application debugging */ |
Michal Simek | f699980 | 2011-02-07 11:36:25 +0100 | [diff] [blame] | 54 | asmlinkage void sw_exception(struct pt_regs *regs) |
Michal Simek | 751f160 | 2010-08-03 11:26:51 +0200 | [diff] [blame] | 55 | { |
| 56 | _exception(SIGTRAP, regs, TRAP_BRKPT, regs->r16); |
Michal Simek | 17b9314 | 2010-11-12 14:27:10 +0100 | [diff] [blame] | 57 | flush_dcache_range(regs->r16, regs->r16 + 0x4); |
| 58 | flush_icache_range(regs->r16, regs->r16 + 0x4); |
Michal Simek | 751f160 | 2010-08-03 11:26:51 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Michal Simek | c4df4bc | 2009-03-27 14:25:13 +0100 | [diff] [blame] | 61 | void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr) |
| 62 | { |
Michal Simek | 6bd55f0 | 2012-12-27 10:40:38 +0100 | [diff] [blame] | 63 | if (kernel_mode(regs)) |
Michal Simek | c4df4bc | 2009-03-27 14:25:13 +0100 | [diff] [blame] | 64 | die("Exception in kernel mode", regs, signr); |
Michal Simek | 6bd55f0 | 2012-12-27 10:40:38 +0100 | [diff] [blame] | 65 | |
Eric W. Biederman | 2e1661d2 | 2019-05-23 11:04:24 -0500 | [diff] [blame] | 66 | force_sig_fault(signr, code, (void __user *)addr); |
Michal Simek | c4df4bc | 2009-03-27 14:25:13 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | asmlinkage void full_exception(struct pt_regs *regs, unsigned int type, |
| 70 | int fsr, int addr) |
| 71 | { |
Michal Simek | 4bb73c3 | 2009-05-26 16:30:24 +0200 | [diff] [blame] | 72 | #ifdef CONFIG_MMU |
Michal Simek | 4bb73c3 | 2009-05-26 16:30:24 +0200 | [diff] [blame] | 73 | addr = regs->pc; |
| 74 | #endif |
| 75 | |
Michal Simek | c4df4bc | 2009-03-27 14:25:13 +0100 | [diff] [blame] | 76 | #if 0 |
Michal Simek | 6bd55f0 | 2012-12-27 10:40:38 +0100 | [diff] [blame] | 77 | pr_warn("Exception %02x in %s mode, FSR=%08x PC=%08x ESR=%08x\n", |
Michal Simek | c4df4bc | 2009-03-27 14:25:13 +0100 | [diff] [blame] | 78 | 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 Simek | 4bb73c3 | 2009-05-26 16:30:24 +0200 | [diff] [blame] | 84 | if (user_mode(regs)) { |
Joe Perches | c4554c3 | 2010-09-11 22:10:51 -0700 | [diff] [blame] | 85 | pr_debug("Illegal opcode exception in user mode\n"); |
Michal Simek | 4bb73c3 | 2009-05-26 16:30:24 +0200 | [diff] [blame] | 86 | _exception(SIGILL, regs, ILL_ILLOPC, addr); |
| 87 | return; |
| 88 | } |
Michal Simek | 6bd55f0 | 2012-12-27 10:40:38 +0100 | [diff] [blame] | 89 | pr_warn("Illegal opcode exception in kernel mode.\n"); |
Michal Simek | 4bb73c3 | 2009-05-26 16:30:24 +0200 | [diff] [blame] | 90 | die("opcode exception", regs, SIGBUS); |
Michal Simek | c4df4bc | 2009-03-27 14:25:13 +0100 | [diff] [blame] | 91 | break; |
| 92 | case MICROBLAZE_IBUS_EXCEPTION: |
| 93 | if (user_mode(regs)) { |
Joe Perches | c4554c3 | 2010-09-11 22:10:51 -0700 | [diff] [blame] | 94 | pr_debug("Instruction bus error exception in user mode\n"); |
Michal Simek | c4df4bc | 2009-03-27 14:25:13 +0100 | [diff] [blame] | 95 | _exception(SIGBUS, regs, BUS_ADRERR, addr); |
| 96 | return; |
| 97 | } |
Michal Simek | 6bd55f0 | 2012-12-27 10:40:38 +0100 | [diff] [blame] | 98 | pr_warn("Instruction bus error exception in kernel mode.\n"); |
Michal Simek | c4df4bc | 2009-03-27 14:25:13 +0100 | [diff] [blame] | 99 | die("bus exception", regs, SIGBUS); |
| 100 | break; |
| 101 | case MICROBLAZE_DBUS_EXCEPTION: |
| 102 | if (user_mode(regs)) { |
Joe Perches | c4554c3 | 2010-09-11 22:10:51 -0700 | [diff] [blame] | 103 | pr_debug("Data bus error exception in user mode\n"); |
Michal Simek | c4df4bc | 2009-03-27 14:25:13 +0100 | [diff] [blame] | 104 | _exception(SIGBUS, regs, BUS_ADRERR, addr); |
| 105 | return; |
| 106 | } |
Michal Simek | 6bd55f0 | 2012-12-27 10:40:38 +0100 | [diff] [blame] | 107 | pr_warn("Data bus error exception in kernel mode.\n"); |
Michal Simek | c4df4bc | 2009-03-27 14:25:13 +0100 | [diff] [blame] | 108 | die("bus exception", regs, SIGBUS); |
| 109 | break; |
| 110 | case MICROBLAZE_DIV_ZERO_EXCEPTION: |
Michal Simek | 4bb73c3 | 2009-05-26 16:30:24 +0200 | [diff] [blame] | 111 | if (user_mode(regs)) { |
Joe Perches | c4554c3 | 2010-09-11 22:10:51 -0700 | [diff] [blame] | 112 | pr_debug("Divide by zero exception in user mode\n"); |
Edgar E. Iglesias | 15ec090 | 2011-08-22 19:58:06 +0200 | [diff] [blame] | 113 | _exception(SIGFPE, regs, FPE_INTDIV, addr); |
Michal Simek | 4bb73c3 | 2009-05-26 16:30:24 +0200 | [diff] [blame] | 114 | return; |
| 115 | } |
Michal Simek | 6bd55f0 | 2012-12-27 10:40:38 +0100 | [diff] [blame] | 116 | pr_warn("Divide by zero exception in kernel mode.\n"); |
Randy Dunlap | f3ff821 | 2010-04-21 14:11:34 -0700 | [diff] [blame] | 117 | die("Divide by zero exception", regs, SIGBUS); |
Michal Simek | c4df4bc | 2009-03-27 14:25:13 +0100 | [diff] [blame] | 118 | break; |
Michal Simek | c4df4bc | 2009-03-27 14:25:13 +0100 | [diff] [blame] | 119 | case MICROBLAZE_FPU_EXCEPTION: |
Joe Perches | c4554c3 | 2010-09-11 22:10:51 -0700 | [diff] [blame] | 120 | pr_debug("FPU exception\n"); |
Michal Simek | c4df4bc | 2009-03-27 14:25:13 +0100 | [diff] [blame] | 121 | /* 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 Simek | 4bb73c3 | 2009-05-26 16:30:24 +0200 | [diff] [blame] | 136 | #ifdef CONFIG_MMU |
| 137 | case MICROBLAZE_PRIVILEGED_EXCEPTION: |
Joe Perches | c4554c3 | 2010-09-11 22:10:51 -0700 | [diff] [blame] | 138 | pr_debug("Privileged exception\n"); |
Michal Simek | 0425609 | 2010-08-03 11:32:20 +0200 | [diff] [blame] | 139 | _exception(SIGILL, regs, ILL_PRVOPC, addr); |
Michal Simek | 4bb73c3 | 2009-05-26 16:30:24 +0200 | [diff] [blame] | 140 | break; |
| 141 | #endif |
Michal Simek | c4df4bc | 2009-03-27 14:25:13 +0100 | [diff] [blame] | 142 | default: |
Michal Simek | 4bb73c3 | 2009-05-26 16:30:24 +0200 | [diff] [blame] | 143 | /* FIXME what to do in unexpected exception */ |
Michal Simek | 6bd55f0 | 2012-12-27 10:40:38 +0100 | [diff] [blame] | 144 | pr_warn("Unexpected exception %02x PC=%08x in %s mode\n", |
| 145 | type, (unsigned int) addr, |
Michal Simek | c4df4bc | 2009-03-27 14:25:13 +0100 | [diff] [blame] | 146 | kernel_mode(regs) ? "kernel" : "user"); |
| 147 | } |
| 148 | return; |
| 149 | } |