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