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/kprobes.h> 16 #include <linux/mm.h> 17 #include <linux/module.h> 18 #include <linux/irq.h> 19 20 #include <asm/bug.h> 21 #include <asm/processor.h> 22 #include <asm/ptrace.h> 23 #include <asm/csr.h> 24 25 int show_unhandled_signals = 1; 26 27 extern asmlinkage void handle_exception(void); 28 29 static DEFINE_SPINLOCK(die_lock); 30 31 void die(struct pt_regs *regs, const char *str) 32 { 33 static int die_counter; 34 int ret; 35 36 oops_enter(); 37 38 spin_lock_irq(&die_lock); 39 console_verbose(); 40 bust_spinlocks(1); 41 42 pr_emerg("%s [#%d]\n", str, ++die_counter); 43 print_modules(); 44 show_regs(regs); 45 46 ret = notify_die(DIE_OOPS, str, regs, 0, regs->cause, SIGSEGV); 47 48 bust_spinlocks(0); 49 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE); 50 spin_unlock_irq(&die_lock); 51 oops_exit(); 52 53 if (in_interrupt()) 54 panic("Fatal exception in interrupt"); 55 if (panic_on_oops) 56 panic("Fatal exception"); 57 if (ret != NOTIFY_STOP) 58 do_exit(SIGSEGV); 59 } 60 61 void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr) 62 { 63 struct task_struct *tsk = current; 64 65 if (show_unhandled_signals && unhandled_signal(tsk, signo) 66 && printk_ratelimit()) { 67 pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT, 68 tsk->comm, task_pid_nr(tsk), signo, code, addr); 69 print_vma_addr(KERN_CONT " in ", instruction_pointer(regs)); 70 pr_cont("\n"); 71 __show_regs(regs); 72 } 73 74 force_sig_fault(signo, code, (void __user *)addr); 75 } 76 77 static void do_trap_error(struct pt_regs *regs, int signo, int code, 78 unsigned long addr, const char *str) 79 { 80 current->thread.bad_cause = regs->cause; 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->epc, "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_fault, 105 SIGSEGV, SEGV_ACCERR, "load access fault"); 106 #ifndef CONFIG_RISCV_M_MODE 107 DO_ERROR_INFO(do_trap_load_misaligned, 108 SIGBUS, BUS_ADRALN, "Oops - load address misaligned"); 109 DO_ERROR_INFO(do_trap_store_misaligned, 110 SIGBUS, BUS_ADRALN, "Oops - store (or AMO) address misaligned"); 111 #else 112 int handle_misaligned_load(struct pt_regs *regs); 113 int handle_misaligned_store(struct pt_regs *regs); 114 115 asmlinkage void do_trap_load_misaligned(struct pt_regs *regs) 116 { 117 if (!handle_misaligned_load(regs)) 118 return; 119 do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc, 120 "Oops - load address misaligned"); 121 } 122 123 asmlinkage void do_trap_store_misaligned(struct pt_regs *regs) 124 { 125 if (!handle_misaligned_store(regs)) 126 return; 127 do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc, 128 "Oops - store (or AMO) address misaligned"); 129 } 130 #endif 131 DO_ERROR_INFO(do_trap_store_fault, 132 SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault"); 133 DO_ERROR_INFO(do_trap_ecall_u, 134 SIGILL, ILL_ILLTRP, "environment call from U-mode"); 135 DO_ERROR_INFO(do_trap_ecall_s, 136 SIGILL, ILL_ILLTRP, "environment call from S-mode"); 137 DO_ERROR_INFO(do_trap_ecall_m, 138 SIGILL, ILL_ILLTRP, "environment call from M-mode"); 139 140 static inline unsigned long get_break_insn_length(unsigned long pc) 141 { 142 bug_insn_t insn; 143 144 if (get_kernel_nofault(insn, (bug_insn_t *)pc)) 145 return 0; 146 147 return GET_INSN_LENGTH(insn); 148 } 149 150 asmlinkage __visible void do_trap_break(struct pt_regs *regs) 151 { 152 #ifdef CONFIG_KPROBES 153 if (kprobe_single_step_handler(regs)) 154 return; 155 156 if (kprobe_breakpoint_handler(regs)) 157 return; 158 #endif 159 #ifdef CONFIG_UPROBES 160 if (uprobe_single_step_handler(regs)) 161 return; 162 163 if (uprobe_breakpoint_handler(regs)) 164 return; 165 #endif 166 current->thread.bad_cause = regs->cause; 167 168 if (user_mode(regs)) 169 force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->epc); 170 #ifdef CONFIG_KGDB 171 else if (notify_die(DIE_TRAP, "EBREAK", regs, 0, regs->cause, SIGTRAP) 172 == NOTIFY_STOP) 173 return; 174 #endif 175 else if (report_bug(regs->epc, regs) == BUG_TRAP_TYPE_WARN) 176 regs->epc += get_break_insn_length(regs->epc); 177 else 178 die(regs, "Kernel BUG"); 179 } 180 181 #ifdef CONFIG_GENERIC_BUG 182 int is_valid_bugaddr(unsigned long pc) 183 { 184 bug_insn_t insn; 185 186 if (pc < VMALLOC_START) 187 return 0; 188 if (get_kernel_nofault(insn, (bug_insn_t *)pc)) 189 return 0; 190 if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32) 191 return (insn == __BUG_INSN_32); 192 else 193 return ((insn & __COMPRESSED_INSN_MASK) == __BUG_INSN_16); 194 } 195 #endif /* CONFIG_GENERIC_BUG */ 196 197 /* stvec & scratch is already set from head.S */ 198 void trap_init(void) 199 { 200 } 201