hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
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>
....@@ -48,7 +42,10 @@
4842 print_modules();
4943 show_regs(regs);
5044
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);
5249
5350 bust_spinlocks(0);
5451 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
....@@ -63,26 +60,27 @@
6360 do_exit(SIGSEGV);
6461 }
6562
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)
6864 {
65
+ struct task_struct *tsk = current;
66
+
6967 if (show_unhandled_signals && unhandled_signal(tsk, signo)
7068 && printk_ratelimit()) {
7169 pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT,
7270 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));
7472 pr_cont("\n");
7573 show_regs(regs);
7674 }
7775
78
- force_sig_fault(signo, code, (void __user *)addr, tsk);
76
+ force_sig_fault(signo, code, (void __user *)addr);
7977 }
8078
8179 static void do_trap_error(struct pt_regs *regs, int signo, int code,
8280 unsigned long addr, const char *str)
8381 {
8482 if (user_mode(regs)) {
85
- do_trap(regs, signo, code, addr, current);
83
+ do_trap(regs, signo, code, addr);
8684 } else {
8785 if (!fixup_exception(regs))
8886 die(regs, str);
....@@ -90,9 +88,9 @@
9088 }
9189
9290 #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) \
9492 { \
95
- do_trap_error(regs, signo, code, regs->sepc, "Oops - " str); \
93
+ do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \
9694 }
9795
9896 DO_ERROR_INFO(do_trap_unknown,
....@@ -103,12 +101,33 @@
103101 SIGSEGV, SEGV_ACCERR, "instruction access fault");
104102 DO_ERROR_INFO(do_trap_insn_illegal,
105103 SIGILL, ILL_ILLOPC, "illegal instruction");
106
-DO_ERROR_INFO(do_trap_load_misaligned,
107
- SIGBUS, BUS_ADRALN, "load address misaligned");
108104 DO_ERROR_INFO(do_trap_load_fault,
109105 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");
110109 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
112131 DO_ERROR_INFO(do_trap_store_fault,
113132 SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
114133 DO_ERROR_INFO(do_trap_ecall_u,
....@@ -118,26 +137,29 @@
118137 DO_ERROR_INFO(do_trap_ecall_m,
119138 SIGILL, ILL_ILLTRP, "environment call from M-mode");
120139
121
-asmlinkage void do_trap_break(struct pt_regs *regs)
140
+static inline unsigned long get_break_insn_length(unsigned long pc)
122141 {
123
-#ifdef CONFIG_GENERIC_BUG
124
- if (!user_mode(regs)) {
125
- enum bug_trap_type type;
142
+ bug_insn_t insn;
126143
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;
139146
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");
141163 }
142164
143165 #ifdef CONFIG_GENERIC_BUG
....@@ -145,23 +167,18 @@
145167 {
146168 bug_insn_t insn;
147169
148
- if (pc < PAGE_OFFSET)
170
+ if (pc < VMALLOC_START)
149171 return 0;
150
- if (probe_kernel_address((bug_insn_t *)pc, insn))
172
+ if (get_kernel_nofault(insn, (bug_insn_t *)pc))
151173 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);
153178 }
154179 #endif /* CONFIG_GENERIC_BUG */
155180
156
-void __init trap_init(void)
181
+/* stvec & scratch is already set from head.S */
182
+void trap_init(void)
157183 {
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);
167184 }