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