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