^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2012 Regents of the University of California
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/sched/debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/kdebug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/processor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <asm/csr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int show_unhandled_signals = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) extern asmlinkage void handle_exception(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static DEFINE_SPINLOCK(die_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) void die(struct pt_regs *regs, const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static int die_counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) oops_enter();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) spin_lock_irq(&die_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) console_verbose();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) bust_spinlocks(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) pr_emerg("%s [#%d]\n", str, ++die_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) print_modules();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) show_regs(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) ret = notify_die(DIE_OOPS, str, regs, 0, regs->cause, SIGSEGV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) bust_spinlocks(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) spin_unlock_irq(&die_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) oops_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (in_interrupt())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) panic("Fatal exception in interrupt");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (panic_on_oops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) panic("Fatal exception");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (ret != NOTIFY_STOP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) do_exit(SIGSEGV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct task_struct *tsk = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (show_unhandled_signals && unhandled_signal(tsk, signo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) && printk_ratelimit()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) tsk->comm, task_pid_nr(tsk), signo, code, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) print_vma_addr(KERN_CONT " in ", instruction_pointer(regs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) pr_cont("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) show_regs(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) force_sig_fault(signo, code, (void __user *)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static void do_trap_error(struct pt_regs *regs, int signo, int code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) unsigned long addr, const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (user_mode(regs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) do_trap(regs, signo, code, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (!fixup_exception(regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) die(regs, str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #define DO_ERROR_INFO(name, signo, code, str) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) asmlinkage __visible void name(struct pt_regs *regs) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) DO_ERROR_INFO(do_trap_unknown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) SIGILL, ILL_ILLTRP, "unknown exception");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) DO_ERROR_INFO(do_trap_insn_misaligned,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) SIGBUS, BUS_ADRALN, "instruction address misaligned");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) DO_ERROR_INFO(do_trap_insn_fault,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) SIGSEGV, SEGV_ACCERR, "instruction access fault");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) DO_ERROR_INFO(do_trap_insn_illegal,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) SIGILL, ILL_ILLOPC, "illegal instruction");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) DO_ERROR_INFO(do_trap_load_fault,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) SIGSEGV, SEGV_ACCERR, "load access fault");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) #ifndef CONFIG_RISCV_M_MODE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) DO_ERROR_INFO(do_trap_load_misaligned,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) SIGBUS, BUS_ADRALN, "Oops - load address misaligned");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) DO_ERROR_INFO(do_trap_store_misaligned,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) SIGBUS, BUS_ADRALN, "Oops - store (or AMO) address misaligned");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int handle_misaligned_load(struct pt_regs *regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) int handle_misaligned_store(struct pt_regs *regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) asmlinkage void do_trap_load_misaligned(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (!handle_misaligned_load(regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) "Oops - load address misaligned");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) asmlinkage void do_trap_store_misaligned(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (!handle_misaligned_store(regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) "Oops - store (or AMO) address misaligned");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) DO_ERROR_INFO(do_trap_store_fault,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) DO_ERROR_INFO(do_trap_ecall_u,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) SIGILL, ILL_ILLTRP, "environment call from U-mode");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) DO_ERROR_INFO(do_trap_ecall_s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) SIGILL, ILL_ILLTRP, "environment call from S-mode");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) DO_ERROR_INFO(do_trap_ecall_m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) SIGILL, ILL_ILLTRP, "environment call from M-mode");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static inline unsigned long get_break_insn_length(unsigned long pc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) bug_insn_t insn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (get_kernel_nofault(insn, (bug_insn_t *)pc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return GET_INSN_LENGTH(insn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) asmlinkage __visible void do_trap_break(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (user_mode(regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->epc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) #ifdef CONFIG_KGDB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) else if (notify_die(DIE_TRAP, "EBREAK", regs, 0, regs->cause, SIGTRAP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) == NOTIFY_STOP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) else if (report_bug(regs->epc, regs) == BUG_TRAP_TYPE_WARN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) regs->epc += get_break_insn_length(regs->epc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) die(regs, "Kernel BUG");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) #ifdef CONFIG_GENERIC_BUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) int is_valid_bugaddr(unsigned long pc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) bug_insn_t insn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (pc < VMALLOC_START)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (get_kernel_nofault(insn, (bug_insn_t *)pc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return (insn == __BUG_INSN_32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return ((insn & __COMPRESSED_INSN_MASK) == __BUG_INSN_16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) #endif /* CONFIG_GENERIC_BUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /* stvec & scratch is already set from head.S */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) void trap_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }