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