1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Regents of the University of California
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>
16 #include <linux/module.h>
17 #include <linux/irq.h>
18 #include <linux/kexec.h>
20 #include <asm/processor.h>
21 #include <asm/ptrace.h>
24 int show_unhandled_signals = 1;
26 extern asmlinkage void handle_exception(void);
28 static DEFINE_SPINLOCK(die_lock);
30 void die(struct pt_regs *regs, const char *str)
32 static int die_counter;
37 spin_lock_irq(&die_lock);
41 pr_emerg("%s [#%d]\n", str, ++die_counter);
45 ret = notify_die(DIE_OOPS, str, regs, 0, regs->scause, SIGSEGV);
47 if (regs && kexec_should_crash(current))
51 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
52 spin_unlock_irq(&die_lock);
56 panic("Fatal exception in interrupt");
58 panic("Fatal exception");
59 if (ret != NOTIFY_STOP)
60 make_task_dead(SIGSEGV);
63 void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr)
65 struct task_struct *tsk = current;
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));
76 force_sig_fault(signo, code, (void __user *)addr);
79 static void do_trap_error(struct pt_regs *regs, int signo, int code,
80 unsigned long addr, const char *str)
82 if (user_mode(regs)) {
83 do_trap(regs, signo, code, addr);
85 if (!fixup_exception(regs))
90 #define DO_ERROR_INFO(name, signo, code, str) \
91 asmlinkage __visible void name(struct pt_regs *regs) \
93 do_trap_error(regs, signo, code, regs->sepc, "Oops - " str); \
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");
119 static inline unsigned long get_break_insn_length(unsigned long pc)
123 if (probe_kernel_address((bug_insn_t *)pc, insn))
125 return (((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32) ? 4UL : 2UL);
128 asmlinkage __visible void do_trap_break(struct pt_regs *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);
135 die(regs, "Kernel BUG");
138 #ifdef CONFIG_GENERIC_BUG
139 int is_valid_bugaddr(unsigned long pc)
143 if (pc < VMALLOC_START)
145 if (probe_kernel_address((bug_insn_t *)pc, insn))
147 if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
148 return (insn == __BUG_INSN_32);
150 return ((insn & __COMPRESSED_INSN_MASK) == __BUG_INSN_16);
152 #endif /* CONFIG_GENERIC_BUG */
154 void __init trap_init(void)
157 * Set sup0 scratch register to 0, indicating to exception vector
158 * that we are presently executing in the kernel
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);