.. | .. |
---|
| 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> |
---|
.. | .. |
---|
37 | 31 | { |
---|
38 | 32 | static int die_counter; |
---|
39 | 33 | int ret; |
---|
| 34 | + long cause; |
---|
| 35 | + unsigned long flags; |
---|
40 | 36 | |
---|
41 | 37 | oops_enter(); |
---|
42 | 38 | |
---|
43 | | - spin_lock_irq(&die_lock); |
---|
| 39 | + spin_lock_irqsave(&die_lock, flags); |
---|
44 | 40 | console_verbose(); |
---|
45 | 41 | bust_spinlocks(1); |
---|
46 | 42 | |
---|
47 | 43 | pr_emerg("%s [#%d]\n", str, ++die_counter); |
---|
48 | 44 | print_modules(); |
---|
49 | | - show_regs(regs); |
---|
| 45 | + if (regs) |
---|
| 46 | + show_regs(regs); |
---|
50 | 47 | |
---|
51 | | - ret = notify_die(DIE_OOPS, str, regs, 0, regs->scause, SIGSEGV); |
---|
| 48 | + cause = regs ? regs->cause : -1; |
---|
| 49 | + ret = notify_die(DIE_OOPS, str, regs, 0, cause, SIGSEGV); |
---|
| 50 | + |
---|
| 51 | + if (kexec_should_crash(current)) |
---|
| 52 | + crash_kexec(regs); |
---|
52 | 53 | |
---|
53 | 54 | bust_spinlocks(0); |
---|
54 | 55 | add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE); |
---|
55 | | - spin_unlock_irq(&die_lock); |
---|
| 56 | + spin_unlock_irqrestore(&die_lock, flags); |
---|
56 | 57 | oops_exit(); |
---|
57 | 58 | |
---|
58 | 59 | if (in_interrupt()) |
---|
.. | .. |
---|
60 | 61 | if (panic_on_oops) |
---|
61 | 62 | panic("Fatal exception"); |
---|
62 | 63 | if (ret != NOTIFY_STOP) |
---|
63 | | - do_exit(SIGSEGV); |
---|
| 64 | + make_task_dead(SIGSEGV); |
---|
64 | 65 | } |
---|
65 | 66 | |
---|
66 | | -void do_trap(struct pt_regs *regs, int signo, int code, |
---|
67 | | - unsigned long addr, struct task_struct *tsk) |
---|
| 67 | +void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr) |
---|
68 | 68 | { |
---|
| 69 | + struct task_struct *tsk = current; |
---|
| 70 | + |
---|
69 | 71 | if (show_unhandled_signals && unhandled_signal(tsk, signo) |
---|
70 | 72 | && printk_ratelimit()) { |
---|
71 | 73 | pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT, |
---|
72 | 74 | tsk->comm, task_pid_nr(tsk), signo, code, addr); |
---|
73 | | - print_vma_addr(KERN_CONT " in ", GET_IP(regs)); |
---|
| 75 | + print_vma_addr(KERN_CONT " in ", instruction_pointer(regs)); |
---|
74 | 76 | pr_cont("\n"); |
---|
75 | 77 | show_regs(regs); |
---|
76 | 78 | } |
---|
77 | 79 | |
---|
78 | | - force_sig_fault(signo, code, (void __user *)addr, tsk); |
---|
| 80 | + force_sig_fault(signo, code, (void __user *)addr); |
---|
79 | 81 | } |
---|
80 | 82 | |
---|
81 | 83 | static void do_trap_error(struct pt_regs *regs, int signo, int code, |
---|
82 | 84 | unsigned long addr, const char *str) |
---|
83 | 85 | { |
---|
84 | 86 | if (user_mode(regs)) { |
---|
85 | | - do_trap(regs, signo, code, addr, current); |
---|
| 87 | + do_trap(regs, signo, code, addr); |
---|
86 | 88 | } else { |
---|
87 | 89 | if (!fixup_exception(regs)) |
---|
88 | 90 | die(regs, str); |
---|
.. | .. |
---|
90 | 92 | } |
---|
91 | 93 | |
---|
92 | 94 | #define DO_ERROR_INFO(name, signo, code, str) \ |
---|
93 | | -asmlinkage void name(struct pt_regs *regs) \ |
---|
| 95 | +asmlinkage __visible void name(struct pt_regs *regs) \ |
---|
94 | 96 | { \ |
---|
95 | | - do_trap_error(regs, signo, code, regs->sepc, "Oops - " str); \ |
---|
| 97 | + do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \ |
---|
96 | 98 | } |
---|
97 | 99 | |
---|
98 | 100 | DO_ERROR_INFO(do_trap_unknown, |
---|
.. | .. |
---|
103 | 105 | SIGSEGV, SEGV_ACCERR, "instruction access fault"); |
---|
104 | 106 | DO_ERROR_INFO(do_trap_insn_illegal, |
---|
105 | 107 | SIGILL, ILL_ILLOPC, "illegal instruction"); |
---|
106 | | -DO_ERROR_INFO(do_trap_load_misaligned, |
---|
107 | | - SIGBUS, BUS_ADRALN, "load address misaligned"); |
---|
108 | 108 | DO_ERROR_INFO(do_trap_load_fault, |
---|
109 | 109 | SIGSEGV, SEGV_ACCERR, "load access fault"); |
---|
| 110 | +#ifndef CONFIG_RISCV_M_MODE |
---|
| 111 | +DO_ERROR_INFO(do_trap_load_misaligned, |
---|
| 112 | + SIGBUS, BUS_ADRALN, "Oops - load address misaligned"); |
---|
110 | 113 | DO_ERROR_INFO(do_trap_store_misaligned, |
---|
111 | | - SIGBUS, BUS_ADRALN, "store (or AMO) address misaligned"); |
---|
| 114 | + SIGBUS, BUS_ADRALN, "Oops - store (or AMO) address misaligned"); |
---|
| 115 | +#else |
---|
| 116 | +int handle_misaligned_load(struct pt_regs *regs); |
---|
| 117 | +int handle_misaligned_store(struct pt_regs *regs); |
---|
| 118 | + |
---|
| 119 | +asmlinkage void do_trap_load_misaligned(struct pt_regs *regs) |
---|
| 120 | +{ |
---|
| 121 | + if (!handle_misaligned_load(regs)) |
---|
| 122 | + return; |
---|
| 123 | + do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc, |
---|
| 124 | + "Oops - load address misaligned"); |
---|
| 125 | +} |
---|
| 126 | + |
---|
| 127 | +asmlinkage void do_trap_store_misaligned(struct pt_regs *regs) |
---|
| 128 | +{ |
---|
| 129 | + if (!handle_misaligned_store(regs)) |
---|
| 130 | + return; |
---|
| 131 | + do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc, |
---|
| 132 | + "Oops - store (or AMO) address misaligned"); |
---|
| 133 | +} |
---|
| 134 | +#endif |
---|
112 | 135 | DO_ERROR_INFO(do_trap_store_fault, |
---|
113 | 136 | SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault"); |
---|
114 | 137 | DO_ERROR_INFO(do_trap_ecall_u, |
---|
.. | .. |
---|
118 | 141 | DO_ERROR_INFO(do_trap_ecall_m, |
---|
119 | 142 | SIGILL, ILL_ILLTRP, "environment call from M-mode"); |
---|
120 | 143 | |
---|
121 | | -asmlinkage void do_trap_break(struct pt_regs *regs) |
---|
| 144 | +static inline unsigned long get_break_insn_length(unsigned long pc) |
---|
122 | 145 | { |
---|
123 | | -#ifdef CONFIG_GENERIC_BUG |
---|
124 | | - if (!user_mode(regs)) { |
---|
125 | | - enum bug_trap_type type; |
---|
| 146 | + bug_insn_t insn; |
---|
126 | 147 | |
---|
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 */ |
---|
| 148 | + if (get_kernel_nofault(insn, (bug_insn_t *)pc)) |
---|
| 149 | + return 0; |
---|
139 | 150 | |
---|
140 | | - force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)(regs->sepc), current); |
---|
| 151 | + return GET_INSN_LENGTH(insn); |
---|
| 152 | +} |
---|
| 153 | + |
---|
| 154 | +asmlinkage __visible void do_trap_break(struct pt_regs *regs) |
---|
| 155 | +{ |
---|
| 156 | + if (user_mode(regs)) |
---|
| 157 | + force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->epc); |
---|
| 158 | +#ifdef CONFIG_KGDB |
---|
| 159 | + else if (notify_die(DIE_TRAP, "EBREAK", regs, 0, regs->cause, SIGTRAP) |
---|
| 160 | + == NOTIFY_STOP) |
---|
| 161 | + return; |
---|
| 162 | +#endif |
---|
| 163 | + else if (report_bug(regs->epc, regs) == BUG_TRAP_TYPE_WARN) |
---|
| 164 | + regs->epc += get_break_insn_length(regs->epc); |
---|
| 165 | + else |
---|
| 166 | + die(regs, "Kernel BUG"); |
---|
141 | 167 | } |
---|
142 | 168 | |
---|
143 | 169 | #ifdef CONFIG_GENERIC_BUG |
---|
.. | .. |
---|
145 | 171 | { |
---|
146 | 172 | bug_insn_t insn; |
---|
147 | 173 | |
---|
148 | | - if (pc < PAGE_OFFSET) |
---|
| 174 | + if (pc < VMALLOC_START) |
---|
149 | 175 | return 0; |
---|
150 | | - if (probe_kernel_address((bug_insn_t *)pc, insn)) |
---|
| 176 | + if (get_kernel_nofault(insn, (bug_insn_t *)pc)) |
---|
151 | 177 | return 0; |
---|
152 | | - return (insn == __BUG_INSN); |
---|
| 178 | + if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32) |
---|
| 179 | + return (insn == __BUG_INSN_32); |
---|
| 180 | + else |
---|
| 181 | + return ((insn & __COMPRESSED_INSN_MASK) == __BUG_INSN_16); |
---|
153 | 182 | } |
---|
154 | 183 | #endif /* CONFIG_GENERIC_BUG */ |
---|
155 | 184 | |
---|
156 | | -void __init trap_init(void) |
---|
| 185 | +/* stvec & scratch is already set from head.S */ |
---|
| 186 | +void trap_init(void) |
---|
157 | 187 | { |
---|
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 | 188 | } |
---|