hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/arch/riscv/kernel/traps.c
....@@ -1,16 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * 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.
124 */
135
6
+#include <linux/cpu.h>
147 #include <linux/kernel.h>
158 #include <linux/init.h>
169 #include <linux/sched.h>
....@@ -22,6 +15,7 @@
2215 #include <linux/mm.h>
2316 #include <linux/module.h>
2417 #include <linux/irq.h>
18
+#include <linux/kexec.h>
2519
2620 #include <asm/processor.h>
2721 #include <asm/ptrace.h>
....@@ -37,22 +31,29 @@
3731 {
3832 static int die_counter;
3933 int ret;
34
+ long cause;
35
+ unsigned long flags;
4036
4137 oops_enter();
4238
43
- spin_lock_irq(&die_lock);
39
+ spin_lock_irqsave(&die_lock, flags);
4440 console_verbose();
4541 bust_spinlocks(1);
4642
4743 pr_emerg("%s [#%d]\n", str, ++die_counter);
4844 print_modules();
49
- show_regs(regs);
45
+ if (regs)
46
+ show_regs(regs);
5047
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);
5253
5354 bust_spinlocks(0);
5455 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
55
- spin_unlock_irq(&die_lock);
56
+ spin_unlock_irqrestore(&die_lock, flags);
5657 oops_exit();
5758
5859 if (in_interrupt())
....@@ -60,29 +61,30 @@
6061 if (panic_on_oops)
6162 panic("Fatal exception");
6263 if (ret != NOTIFY_STOP)
63
- do_exit(SIGSEGV);
64
+ make_task_dead(SIGSEGV);
6465 }
6566
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)
6868 {
69
+ struct task_struct *tsk = current;
70
+
6971 if (show_unhandled_signals && unhandled_signal(tsk, signo)
7072 && printk_ratelimit()) {
7173 pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT,
7274 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));
7476 pr_cont("\n");
7577 show_regs(regs);
7678 }
7779
78
- force_sig_fault(signo, code, (void __user *)addr, tsk);
80
+ force_sig_fault(signo, code, (void __user *)addr);
7981 }
8082
8183 static void do_trap_error(struct pt_regs *regs, int signo, int code,
8284 unsigned long addr, const char *str)
8385 {
8486 if (user_mode(regs)) {
85
- do_trap(regs, signo, code, addr, current);
87
+ do_trap(regs, signo, code, addr);
8688 } else {
8789 if (!fixup_exception(regs))
8890 die(regs, str);
....@@ -90,9 +92,9 @@
9092 }
9193
9294 #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) \
9496 { \
95
- do_trap_error(regs, signo, code, regs->sepc, "Oops - " str); \
97
+ do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \
9698 }
9799
98100 DO_ERROR_INFO(do_trap_unknown,
....@@ -103,12 +105,33 @@
103105 SIGSEGV, SEGV_ACCERR, "instruction access fault");
104106 DO_ERROR_INFO(do_trap_insn_illegal,
105107 SIGILL, ILL_ILLOPC, "illegal instruction");
106
-DO_ERROR_INFO(do_trap_load_misaligned,
107
- SIGBUS, BUS_ADRALN, "load address misaligned");
108108 DO_ERROR_INFO(do_trap_load_fault,
109109 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");
110113 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
112135 DO_ERROR_INFO(do_trap_store_fault,
113136 SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
114137 DO_ERROR_INFO(do_trap_ecall_u,
....@@ -118,26 +141,29 @@
118141 DO_ERROR_INFO(do_trap_ecall_m,
119142 SIGILL, ILL_ILLTRP, "environment call from M-mode");
120143
121
-asmlinkage void do_trap_break(struct pt_regs *regs)
144
+static inline unsigned long get_break_insn_length(unsigned long pc)
122145 {
123
-#ifdef CONFIG_GENERIC_BUG
124
- if (!user_mode(regs)) {
125
- enum bug_trap_type type;
146
+ bug_insn_t insn;
126147
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;
139150
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");
141167 }
142168
143169 #ifdef CONFIG_GENERIC_BUG
....@@ -145,23 +171,18 @@
145171 {
146172 bug_insn_t insn;
147173
148
- if (pc < PAGE_OFFSET)
174
+ if (pc < VMALLOC_START)
149175 return 0;
150
- if (probe_kernel_address((bug_insn_t *)pc, insn))
176
+ if (get_kernel_nofault(insn, (bug_insn_t *)pc))
151177 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);
153182 }
154183 #endif /* CONFIG_GENERIC_BUG */
155184
156
-void __init trap_init(void)
185
+/* stvec & scratch is already set from head.S */
186
+void trap_init(void)
157187 {
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);
167188 }