xref: /linux/arch/riscv/kernel/traps.c (revision c22b0bcb1dd024cb9caad9230e3a387d8b061df5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 Regents of the University of California
4  */
5 
6 #include <linux/cpu.h>
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/kprobes.h>
16 #include <linux/mm.h>
17 #include <linux/module.h>
18 #include <linux/irq.h>
19 
20 #include <asm/processor.h>
21 #include <asm/ptrace.h>
22 #include <asm/csr.h>
23 
24 int show_unhandled_signals = 1;
25 
26 extern asmlinkage void handle_exception(void);
27 
28 static DEFINE_SPINLOCK(die_lock);
29 
30 void die(struct pt_regs *regs, const char *str)
31 {
32 	static int die_counter;
33 	int ret;
34 
35 	oops_enter();
36 
37 	spin_lock_irq(&die_lock);
38 	console_verbose();
39 	bust_spinlocks(1);
40 
41 	pr_emerg("%s [#%d]\n", str, ++die_counter);
42 	print_modules();
43 	show_regs(regs);
44 
45 	ret = notify_die(DIE_OOPS, str, regs, 0, regs->cause, SIGSEGV);
46 
47 	bust_spinlocks(0);
48 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
49 	spin_unlock_irq(&die_lock);
50 	oops_exit();
51 
52 	if (in_interrupt())
53 		panic("Fatal exception in interrupt");
54 	if (panic_on_oops)
55 		panic("Fatal exception");
56 	if (ret != NOTIFY_STOP)
57 		do_exit(SIGSEGV);
58 }
59 
60 void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr)
61 {
62 	struct task_struct *tsk = current;
63 
64 	if (show_unhandled_signals && unhandled_signal(tsk, signo)
65 	    && printk_ratelimit()) {
66 		pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT,
67 			tsk->comm, task_pid_nr(tsk), signo, code, addr);
68 		print_vma_addr(KERN_CONT " in ", instruction_pointer(regs));
69 		pr_cont("\n");
70 		show_regs(regs);
71 	}
72 
73 	force_sig_fault(signo, code, (void __user *)addr);
74 }
75 
76 static void do_trap_error(struct pt_regs *regs, int signo, int code,
77 	unsigned long addr, const char *str)
78 {
79 	if (user_mode(regs)) {
80 		do_trap(regs, signo, code, addr);
81 	} else {
82 		if (!fixup_exception(regs))
83 			die(regs, str);
84 	}
85 }
86 
87 #define DO_ERROR_INFO(name, signo, code, str)				\
88 asmlinkage __visible void name(struct pt_regs *regs)			\
89 {									\
90 	do_trap_error(regs, signo, code, regs->epc, "Oops - " str);	\
91 }
92 
93 DO_ERROR_INFO(do_trap_unknown,
94 	SIGILL, ILL_ILLTRP, "unknown exception");
95 DO_ERROR_INFO(do_trap_insn_misaligned,
96 	SIGBUS, BUS_ADRALN, "instruction address misaligned");
97 DO_ERROR_INFO(do_trap_insn_fault,
98 	SIGSEGV, SEGV_ACCERR, "instruction access fault");
99 DO_ERROR_INFO(do_trap_insn_illegal,
100 	SIGILL, ILL_ILLOPC, "illegal instruction");
101 DO_ERROR_INFO(do_trap_load_fault,
102 	SIGSEGV, SEGV_ACCERR, "load access fault");
103 #ifndef CONFIG_RISCV_M_MODE
104 DO_ERROR_INFO(do_trap_load_misaligned,
105 	SIGBUS, BUS_ADRALN, "Oops - load address misaligned");
106 DO_ERROR_INFO(do_trap_store_misaligned,
107 	SIGBUS, BUS_ADRALN, "Oops - store (or AMO) address misaligned");
108 #else
109 int handle_misaligned_load(struct pt_regs *regs);
110 int handle_misaligned_store(struct pt_regs *regs);
111 
112 asmlinkage void do_trap_load_misaligned(struct pt_regs *regs)
113 {
114 	if (!handle_misaligned_load(regs))
115 		return;
116 	do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
117 		      "Oops - load address misaligned");
118 }
119 
120 asmlinkage void do_trap_store_misaligned(struct pt_regs *regs)
121 {
122 	if (!handle_misaligned_store(regs))
123 		return;
124 	do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
125 		      "Oops - store (or AMO) address misaligned");
126 }
127 #endif
128 DO_ERROR_INFO(do_trap_store_fault,
129 	SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
130 DO_ERROR_INFO(do_trap_ecall_u,
131 	SIGILL, ILL_ILLTRP, "environment call from U-mode");
132 DO_ERROR_INFO(do_trap_ecall_s,
133 	SIGILL, ILL_ILLTRP, "environment call from S-mode");
134 DO_ERROR_INFO(do_trap_ecall_m,
135 	SIGILL, ILL_ILLTRP, "environment call from M-mode");
136 
137 static inline unsigned long get_break_insn_length(unsigned long pc)
138 {
139 	bug_insn_t insn;
140 
141 	if (get_kernel_nofault(insn, (bug_insn_t *)pc))
142 		return 0;
143 
144 	return GET_INSN_LENGTH(insn);
145 }
146 
147 asmlinkage __visible void do_trap_break(struct pt_regs *regs)
148 {
149 #ifdef CONFIG_KPROBES
150 	if (kprobe_single_step_handler(regs))
151 		return;
152 
153 	if (kprobe_breakpoint_handler(regs))
154 		return;
155 #endif
156 
157 	if (user_mode(regs))
158 		force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->epc);
159 #ifdef CONFIG_KGDB
160 	else if (notify_die(DIE_TRAP, "EBREAK", regs, 0, regs->cause, SIGTRAP)
161 								== NOTIFY_STOP)
162 		return;
163 #endif
164 	else if (report_bug(regs->epc, regs) == BUG_TRAP_TYPE_WARN)
165 		regs->epc += get_break_insn_length(regs->epc);
166 	else
167 		die(regs, "Kernel BUG");
168 }
169 
170 #ifdef CONFIG_GENERIC_BUG
171 int is_valid_bugaddr(unsigned long pc)
172 {
173 	bug_insn_t insn;
174 
175 	if (pc < VMALLOC_START)
176 		return 0;
177 	if (get_kernel_nofault(insn, (bug_insn_t *)pc))
178 		return 0;
179 	if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
180 		return (insn == __BUG_INSN_32);
181 	else
182 		return ((insn & __COMPRESSED_INSN_MASK) == __BUG_INSN_16);
183 }
184 #endif /* CONFIG_GENERIC_BUG */
185 
186 /* stvec & scratch is already set from head.S */
187 void trap_init(void)
188 {
189 }
190