.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * Copyright (C) 2012 Regents of the University of California |
---|
3 | | - * |
---|
4 | | - * This program is free software; you can redistribute it and/or |
---|
5 | | - * modify it under the terms of the GNU General Public License |
---|
6 | | - * as published by the Free Software Foundation, version 2. |
---|
7 | | - * |
---|
8 | | - * This program is distributed in the hope that it will be useful, |
---|
9 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
11 | | - * GNU General Public License for more details. |
---|
12 | 4 | */ |
---|
13 | 5 | |
---|
| 6 | +#include <linux/cpu.h> |
---|
14 | 7 | #include <linux/kernel.h> |
---|
15 | 8 | #include <linux/init.h> |
---|
16 | 9 | #include <linux/sched.h> |
---|
.. | .. |
---|
22 | 15 | #include <linux/mm.h> |
---|
23 | 16 | #include <linux/module.h> |
---|
24 | 17 | #include <linux/irq.h> |
---|
| 18 | +#include <linux/kexec.h> |
---|
25 | 19 | |
---|
26 | 20 | #include <asm/processor.h> |
---|
27 | 21 | #include <asm/ptrace.h> |
---|
.. | .. |
---|
48 | 42 | print_modules(); |
---|
49 | 43 | show_regs(regs); |
---|
50 | 44 | |
---|
51 | | - ret = notify_die(DIE_OOPS, str, regs, 0, regs->scause, SIGSEGV); |
---|
| 45 | + ret = notify_die(DIE_OOPS, str, regs, 0, regs->cause, SIGSEGV); |
---|
| 46 | + |
---|
| 47 | + if (regs && kexec_should_crash(current)) |
---|
| 48 | + crash_kexec(regs); |
---|
52 | 49 | |
---|
53 | 50 | bust_spinlocks(0); |
---|
54 | 51 | add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE); |
---|
.. | .. |
---|
63 | 60 | do_exit(SIGSEGV); |
---|
64 | 61 | } |
---|
65 | 62 | |
---|
66 | | -void do_trap(struct pt_regs *regs, int signo, int code, |
---|
67 | | - unsigned long addr, struct task_struct *tsk) |
---|
| 63 | +void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr) |
---|
68 | 64 | { |
---|
| 65 | + struct task_struct *tsk = current; |
---|
| 66 | + |
---|
69 | 67 | if (show_unhandled_signals && unhandled_signal(tsk, signo) |
---|
70 | 68 | && printk_ratelimit()) { |
---|
71 | 69 | pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT, |
---|
72 | 70 | tsk->comm, task_pid_nr(tsk), signo, code, addr); |
---|
73 | | - print_vma_addr(KERN_CONT " in ", GET_IP(regs)); |
---|
| 71 | + print_vma_addr(KERN_CONT " in ", instruction_pointer(regs)); |
---|
74 | 72 | pr_cont("\n"); |
---|
75 | 73 | show_regs(regs); |
---|
76 | 74 | } |
---|
77 | 75 | |
---|
78 | | - force_sig_fault(signo, code, (void __user *)addr, tsk); |
---|
| 76 | + force_sig_fault(signo, code, (void __user *)addr); |
---|
79 | 77 | } |
---|
80 | 78 | |
---|
81 | 79 | static void do_trap_error(struct pt_regs *regs, int signo, int code, |
---|
82 | 80 | unsigned long addr, const char *str) |
---|
83 | 81 | { |
---|
84 | 82 | if (user_mode(regs)) { |
---|
85 | | - do_trap(regs, signo, code, addr, current); |
---|
| 83 | + do_trap(regs, signo, code, addr); |
---|
86 | 84 | } else { |
---|
87 | 85 | if (!fixup_exception(regs)) |
---|
88 | 86 | die(regs, str); |
---|
.. | .. |
---|
90 | 88 | } |
---|
91 | 89 | |
---|
92 | 90 | #define DO_ERROR_INFO(name, signo, code, str) \ |
---|
93 | | -asmlinkage void name(struct pt_regs *regs) \ |
---|
| 91 | +asmlinkage __visible void name(struct pt_regs *regs) \ |
---|
94 | 92 | { \ |
---|
95 | | - do_trap_error(regs, signo, code, regs->sepc, "Oops - " str); \ |
---|
| 93 | + do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \ |
---|
96 | 94 | } |
---|
97 | 95 | |
---|
98 | 96 | DO_ERROR_INFO(do_trap_unknown, |
---|
.. | .. |
---|
103 | 101 | SIGSEGV, SEGV_ACCERR, "instruction access fault"); |
---|
104 | 102 | DO_ERROR_INFO(do_trap_insn_illegal, |
---|
105 | 103 | SIGILL, ILL_ILLOPC, "illegal instruction"); |
---|
106 | | -DO_ERROR_INFO(do_trap_load_misaligned, |
---|
107 | | - SIGBUS, BUS_ADRALN, "load address misaligned"); |
---|
108 | 104 | DO_ERROR_INFO(do_trap_load_fault, |
---|
109 | 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"); |
---|
110 | 109 | DO_ERROR_INFO(do_trap_store_misaligned, |
---|
111 | | - SIGBUS, BUS_ADRALN, "store (or AMO) address 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 |
---|
112 | 131 | DO_ERROR_INFO(do_trap_store_fault, |
---|
113 | 132 | SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault"); |
---|
114 | 133 | DO_ERROR_INFO(do_trap_ecall_u, |
---|
.. | .. |
---|
118 | 137 | DO_ERROR_INFO(do_trap_ecall_m, |
---|
119 | 138 | SIGILL, ILL_ILLTRP, "environment call from M-mode"); |
---|
120 | 139 | |
---|
121 | | -asmlinkage void do_trap_break(struct pt_regs *regs) |
---|
| 140 | +static inline unsigned long get_break_insn_length(unsigned long pc) |
---|
122 | 141 | { |
---|
123 | | -#ifdef CONFIG_GENERIC_BUG |
---|
124 | | - if (!user_mode(regs)) { |
---|
125 | | - enum bug_trap_type type; |
---|
| 142 | + bug_insn_t insn; |
---|
126 | 143 | |
---|
127 | | - type = report_bug(regs->sepc, regs); |
---|
128 | | - switch (type) { |
---|
129 | | - case BUG_TRAP_TYPE_NONE: |
---|
130 | | - break; |
---|
131 | | - case BUG_TRAP_TYPE_WARN: |
---|
132 | | - regs->sepc += sizeof(bug_insn_t); |
---|
133 | | - return; |
---|
134 | | - case BUG_TRAP_TYPE_BUG: |
---|
135 | | - die(regs, "Kernel BUG"); |
---|
136 | | - } |
---|
137 | | - } |
---|
138 | | -#endif /* CONFIG_GENERIC_BUG */ |
---|
| 144 | + if (get_kernel_nofault(insn, (bug_insn_t *)pc)) |
---|
| 145 | + return 0; |
---|
139 | 146 | |
---|
140 | | - force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)(regs->sepc), current); |
---|
| 147 | + return GET_INSN_LENGTH(insn); |
---|
| 148 | +} |
---|
| 149 | + |
---|
| 150 | +asmlinkage __visible void do_trap_break(struct pt_regs *regs) |
---|
| 151 | +{ |
---|
| 152 | + if (user_mode(regs)) |
---|
| 153 | + force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->epc); |
---|
| 154 | +#ifdef CONFIG_KGDB |
---|
| 155 | + else if (notify_die(DIE_TRAP, "EBREAK", regs, 0, regs->cause, SIGTRAP) |
---|
| 156 | + == NOTIFY_STOP) |
---|
| 157 | + return; |
---|
| 158 | +#endif |
---|
| 159 | + else if (report_bug(regs->epc, regs) == BUG_TRAP_TYPE_WARN) |
---|
| 160 | + regs->epc += get_break_insn_length(regs->epc); |
---|
| 161 | + else |
---|
| 162 | + die(regs, "Kernel BUG"); |
---|
141 | 163 | } |
---|
142 | 164 | |
---|
143 | 165 | #ifdef CONFIG_GENERIC_BUG |
---|
.. | .. |
---|
145 | 167 | { |
---|
146 | 168 | bug_insn_t insn; |
---|
147 | 169 | |
---|
148 | | - if (pc < PAGE_OFFSET) |
---|
| 170 | + if (pc < VMALLOC_START) |
---|
149 | 171 | return 0; |
---|
150 | | - if (probe_kernel_address((bug_insn_t *)pc, insn)) |
---|
| 172 | + if (get_kernel_nofault(insn, (bug_insn_t *)pc)) |
---|
151 | 173 | return 0; |
---|
152 | | - return (insn == __BUG_INSN); |
---|
| 174 | + if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32) |
---|
| 175 | + return (insn == __BUG_INSN_32); |
---|
| 176 | + else |
---|
| 177 | + return ((insn & __COMPRESSED_INSN_MASK) == __BUG_INSN_16); |
---|
153 | 178 | } |
---|
154 | 179 | #endif /* CONFIG_GENERIC_BUG */ |
---|
155 | 180 | |
---|
156 | | -void __init trap_init(void) |
---|
| 181 | +/* stvec & scratch is already set from head.S */ |
---|
| 182 | +void trap_init(void) |
---|
157 | 183 | { |
---|
158 | | - /* |
---|
159 | | - * Set sup0 scratch register to 0, indicating to exception vector |
---|
160 | | - * that we are presently executing in the kernel |
---|
161 | | - */ |
---|
162 | | - csr_write(sscratch, 0); |
---|
163 | | - /* Set the exception vector address */ |
---|
164 | | - csr_write(stvec, &handle_exception); |
---|
165 | | - /* Enable all interrupts */ |
---|
166 | | - csr_write(sie, -1); |
---|
167 | 184 | } |
---|