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