^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * HW exception handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2008 PetaLogix
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * This file is subject to the terms and conditions of the GNU General
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Public License. See the file COPYING in the main directory of this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * archive for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * This file handles the architecture-dependent parts of hardware exceptions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/sched/debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/kallsyms.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <asm/exceptions.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <asm/entry.h> /* For KM CPU var */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <asm/current.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <asm/cacheflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define MICROBLAZE_ILL_OPCODE_EXCEPTION 0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define MICROBLAZE_IBUS_EXCEPTION 0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define MICROBLAZE_DBUS_EXCEPTION 0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define MICROBLAZE_DIV_ZERO_EXCEPTION 0x05
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define MICROBLAZE_FPU_EXCEPTION 0x06
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define MICROBLAZE_PRIVILEGED_EXCEPTION 0x07
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static DEFINE_SPINLOCK(die_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) void die(const char *str, struct pt_regs *fp, long err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) console_verbose();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) spin_lock_irq(&die_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) pr_warn("Oops: %s, sig: %ld\n", str, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) show_regs(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) spin_unlock_irq(&die_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* do_exit() should take care of panic'ing from an interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * context so we don't handle it here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) do_exit(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /* for user application debugging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) asmlinkage void sw_exception(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) _exception(SIGTRAP, regs, TRAP_BRKPT, regs->r16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) flush_dcache_range(regs->r16, regs->r16 + 0x4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) flush_icache_range(regs->r16, regs->r16 + 0x4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (kernel_mode(regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) die("Exception in kernel mode", regs, signr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) force_sig_fault(signr, code, (void __user *)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) asmlinkage void full_exception(struct pt_regs *regs, unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) int fsr, int addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #ifdef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) addr = regs->pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) pr_warn("Exception %02x in %s mode, FSR=%08x PC=%08x ESR=%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) type, user_mode(regs) ? "user" : "kernel", fsr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) (unsigned int) regs->pc, (unsigned int) regs->esr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) switch (type & 0x1F) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) case MICROBLAZE_ILL_OPCODE_EXCEPTION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (user_mode(regs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) pr_debug("Illegal opcode exception in user mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) _exception(SIGILL, regs, ILL_ILLOPC, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) pr_warn("Illegal opcode exception in kernel mode.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) die("opcode exception", regs, SIGBUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) case MICROBLAZE_IBUS_EXCEPTION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (user_mode(regs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) pr_debug("Instruction bus error exception in user mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) _exception(SIGBUS, regs, BUS_ADRERR, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) pr_warn("Instruction bus error exception in kernel mode.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) die("bus exception", regs, SIGBUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) case MICROBLAZE_DBUS_EXCEPTION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (user_mode(regs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) pr_debug("Data bus error exception in user mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) _exception(SIGBUS, regs, BUS_ADRERR, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) pr_warn("Data bus error exception in kernel mode.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) die("bus exception", regs, SIGBUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) case MICROBLAZE_DIV_ZERO_EXCEPTION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (user_mode(regs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) pr_debug("Divide by zero exception in user mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) _exception(SIGFPE, regs, FPE_INTDIV, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) pr_warn("Divide by zero exception in kernel mode.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) die("Divide by zero exception", regs, SIGBUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) case MICROBLAZE_FPU_EXCEPTION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) pr_debug("FPU exception\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) /* IEEE FP exception */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /* I removed fsr variable and use code var for storing fsr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (fsr & FSR_IO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) fsr = FPE_FLTINV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) else if (fsr & FSR_OF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) fsr = FPE_FLTOVF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) else if (fsr & FSR_UF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) fsr = FPE_FLTUND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) else if (fsr & FSR_DZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) fsr = FPE_FLTDIV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) else if (fsr & FSR_DO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) fsr = FPE_FLTRES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) _exception(SIGFPE, regs, fsr, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) #ifdef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) case MICROBLAZE_PRIVILEGED_EXCEPTION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) pr_debug("Privileged exception\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) _exception(SIGILL, regs, ILL_PRVOPC, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) /* FIXME what to do in unexpected exception */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) pr_warn("Unexpected exception %02x PC=%08x in %s mode\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) type, (unsigned int) addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) kernel_mode(regs) ? "kernel" : "user");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }