xref: /linux/arch/riscv/kernel/traps.c (revision 7c83232161f609bbc452a1255f823f41afc411dd)
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 #include <linux/entry-common.h>
21 
22 #include <asm/asm-prototypes.h>
23 #include <asm/bug.h>
24 #include <asm/cfi.h>
25 #include <asm/csr.h>
26 #include <asm/processor.h>
27 #include <asm/ptrace.h>
28 #include <asm/syscall.h>
29 #include <asm/thread_info.h>
30 #include <asm/vector.h>
31 #include <asm/irq_stack.h>
32 
33 int show_unhandled_signals = 1;
34 
35 static DEFINE_SPINLOCK(die_lock);
36 
37 static void dump_kernel_instr(const char *loglvl, struct pt_regs *regs)
38 {
39 	char str[sizeof("0000 ") * 12 + 2 + 1], *p = str;
40 	const u16 *insns = (u16 *)instruction_pointer(regs);
41 	long bad;
42 	u16 val;
43 	int i;
44 
45 	for (i = -10; i < 2; i++) {
46 		bad = get_kernel_nofault(val, &insns[i]);
47 		if (!bad) {
48 			p += sprintf(p, i == 0 ? "(%04hx) " : "%04hx ", val);
49 		} else {
50 			printk("%sCode: Unable to access instruction at 0x%px.\n",
51 			       loglvl, &insns[i]);
52 			return;
53 		}
54 	}
55 	printk("%sCode: %s\n", loglvl, str);
56 }
57 
58 void die(struct pt_regs *regs, const char *str)
59 {
60 	static int die_counter;
61 	int ret;
62 	long cause;
63 	unsigned long flags;
64 
65 	oops_enter();
66 
67 	spin_lock_irqsave(&die_lock, flags);
68 	console_verbose();
69 	bust_spinlocks(1);
70 
71 	pr_emerg("%s [#%d]\n", str, ++die_counter);
72 	print_modules();
73 	if (regs) {
74 		show_regs(regs);
75 		dump_kernel_instr(KERN_EMERG, regs);
76 	}
77 
78 	cause = regs ? regs->cause : -1;
79 	ret = notify_die(DIE_OOPS, str, regs, 0, cause, SIGSEGV);
80 
81 	if (kexec_should_crash(current))
82 		crash_kexec(regs);
83 
84 	bust_spinlocks(0);
85 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
86 	spin_unlock_irqrestore(&die_lock, flags);
87 	oops_exit();
88 
89 	if (in_interrupt())
90 		panic("Fatal exception in interrupt");
91 	if (panic_on_oops)
92 		panic("Fatal exception");
93 	if (ret != NOTIFY_STOP)
94 		make_task_dead(SIGSEGV);
95 }
96 
97 void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr)
98 {
99 	struct task_struct *tsk = current;
100 
101 	if (show_unhandled_signals && unhandled_signal(tsk, signo)
102 	    && printk_ratelimit()) {
103 		pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT,
104 			tsk->comm, task_pid_nr(tsk), signo, code, addr);
105 		print_vma_addr(KERN_CONT " in ", instruction_pointer(regs));
106 		pr_cont("\n");
107 		__show_regs(regs);
108 	}
109 
110 	force_sig_fault(signo, code, (void __user *)addr);
111 }
112 
113 static void do_trap_error(struct pt_regs *regs, int signo, int code,
114 	unsigned long addr, const char *str)
115 {
116 	current->thread.bad_cause = regs->cause;
117 
118 	if (user_mode(regs)) {
119 		do_trap(regs, signo, code, addr);
120 	} else {
121 		if (!fixup_exception(regs))
122 			die(regs, str);
123 	}
124 }
125 
126 #if defined(CONFIG_XIP_KERNEL) && defined(CONFIG_RISCV_ALTERNATIVE)
127 #define __trap_section __noinstr_section(".xip.traps")
128 #else
129 #define __trap_section noinstr
130 #endif
131 #define DO_ERROR_INFO(name, signo, code, str)					\
132 asmlinkage __visible __trap_section void name(struct pt_regs *regs)		\
133 {										\
134 	if (user_mode(regs)) {							\
135 		irqentry_enter_from_user_mode(regs);				\
136 		do_trap_error(regs, signo, code, regs->epc, "Oops - " str);	\
137 		irqentry_exit_to_user_mode(regs);				\
138 	} else {								\
139 		irqentry_state_t state = irqentry_nmi_enter(regs);		\
140 		do_trap_error(regs, signo, code, regs->epc, "Oops - " str);	\
141 		irqentry_nmi_exit(regs, state);					\
142 	}									\
143 }
144 
145 DO_ERROR_INFO(do_trap_unknown,
146 	SIGILL, ILL_ILLTRP, "unknown exception");
147 DO_ERROR_INFO(do_trap_insn_misaligned,
148 	SIGBUS, BUS_ADRALN, "instruction address misaligned");
149 DO_ERROR_INFO(do_trap_insn_fault,
150 	SIGSEGV, SEGV_ACCERR, "instruction access fault");
151 
152 asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *regs)
153 {
154 	bool handled;
155 
156 	if (user_mode(regs)) {
157 		irqentry_enter_from_user_mode(regs);
158 
159 		local_irq_enable();
160 
161 		handled = riscv_v_first_use_handler(regs);
162 
163 		local_irq_disable();
164 
165 		if (!handled)
166 			do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
167 				      "Oops - illegal instruction");
168 
169 		irqentry_exit_to_user_mode(regs);
170 	} else {
171 		irqentry_state_t state = irqentry_nmi_enter(regs);
172 
173 		do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
174 			      "Oops - illegal instruction");
175 
176 		irqentry_nmi_exit(regs, state);
177 	}
178 }
179 
180 DO_ERROR_INFO(do_trap_load_fault,
181 	SIGSEGV, SEGV_ACCERR, "load access fault");
182 
183 asmlinkage __visible __trap_section void do_trap_load_misaligned(struct pt_regs *regs)
184 {
185 	if (user_mode(regs)) {
186 		irqentry_enter_from_user_mode(regs);
187 
188 		if (handle_misaligned_load(regs))
189 			do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
190 			      "Oops - load address misaligned");
191 
192 		irqentry_exit_to_user_mode(regs);
193 	} else {
194 		irqentry_state_t state = irqentry_nmi_enter(regs);
195 
196 		if (handle_misaligned_load(regs))
197 			do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
198 			      "Oops - load address misaligned");
199 
200 		irqentry_nmi_exit(regs, state);
201 	}
202 }
203 
204 asmlinkage __visible __trap_section void do_trap_store_misaligned(struct pt_regs *regs)
205 {
206 	if (user_mode(regs)) {
207 		irqentry_enter_from_user_mode(regs);
208 
209 		if (handle_misaligned_store(regs))
210 			do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
211 				"Oops - store (or AMO) address misaligned");
212 
213 		irqentry_exit_to_user_mode(regs);
214 	} else {
215 		irqentry_state_t state = irqentry_nmi_enter(regs);
216 
217 		if (handle_misaligned_store(regs))
218 			do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
219 				"Oops - store (or AMO) address misaligned");
220 
221 		irqentry_nmi_exit(regs, state);
222 	}
223 }
224 DO_ERROR_INFO(do_trap_store_fault,
225 	SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
226 DO_ERROR_INFO(do_trap_ecall_s,
227 	SIGILL, ILL_ILLTRP, "environment call from S-mode");
228 DO_ERROR_INFO(do_trap_ecall_m,
229 	SIGILL, ILL_ILLTRP, "environment call from M-mode");
230 
231 static inline unsigned long get_break_insn_length(unsigned long pc)
232 {
233 	bug_insn_t insn;
234 
235 	if (get_kernel_nofault(insn, (bug_insn_t *)pc))
236 		return 0;
237 
238 	return GET_INSN_LENGTH(insn);
239 }
240 
241 void handle_break(struct pt_regs *regs)
242 {
243 #ifdef CONFIG_KPROBES
244 	if (kprobe_single_step_handler(regs))
245 		return;
246 
247 	if (kprobe_breakpoint_handler(regs))
248 		return;
249 #endif
250 #ifdef CONFIG_UPROBES
251 	if (uprobe_single_step_handler(regs))
252 		return;
253 
254 	if (uprobe_breakpoint_handler(regs))
255 		return;
256 #endif
257 	current->thread.bad_cause = regs->cause;
258 
259 	if (user_mode(regs))
260 		force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->epc);
261 #ifdef CONFIG_KGDB
262 	else if (notify_die(DIE_TRAP, "EBREAK", regs, 0, regs->cause, SIGTRAP)
263 								== NOTIFY_STOP)
264 		return;
265 #endif
266 	else if (report_bug(regs->epc, regs) == BUG_TRAP_TYPE_WARN ||
267 		 handle_cfi_failure(regs) == BUG_TRAP_TYPE_WARN)
268 		regs->epc += get_break_insn_length(regs->epc);
269 	else
270 		die(regs, "Kernel BUG");
271 }
272 
273 asmlinkage __visible __trap_section void do_trap_break(struct pt_regs *regs)
274 {
275 	if (user_mode(regs)) {
276 		irqentry_enter_from_user_mode(regs);
277 
278 		handle_break(regs);
279 
280 		irqentry_exit_to_user_mode(regs);
281 	} else {
282 		irqentry_state_t state = irqentry_nmi_enter(regs);
283 
284 		handle_break(regs);
285 
286 		irqentry_nmi_exit(regs, state);
287 	}
288 }
289 
290 asmlinkage __visible __trap_section void do_trap_ecall_u(struct pt_regs *regs)
291 {
292 	if (user_mode(regs)) {
293 		long syscall = regs->a7;
294 
295 		regs->epc += 4;
296 		regs->orig_a0 = regs->a0;
297 
298 		riscv_v_vstate_discard(regs);
299 
300 		syscall = syscall_enter_from_user_mode(regs, syscall);
301 
302 		if (syscall >= 0 && syscall < NR_syscalls)
303 			syscall_handler(regs, syscall);
304 		else if (syscall != -1)
305 			regs->a0 = -ENOSYS;
306 
307 		syscall_exit_to_user_mode(regs);
308 	} else {
309 		irqentry_state_t state = irqentry_nmi_enter(regs);
310 
311 		do_trap_error(regs, SIGILL, ILL_ILLTRP, regs->epc,
312 			"Oops - environment call from U-mode");
313 
314 		irqentry_nmi_exit(regs, state);
315 	}
316 
317 }
318 
319 #ifdef CONFIG_MMU
320 asmlinkage __visible noinstr void do_page_fault(struct pt_regs *regs)
321 {
322 	irqentry_state_t state = irqentry_enter(regs);
323 
324 	handle_page_fault(regs);
325 
326 	local_irq_disable();
327 
328 	irqentry_exit(regs, state);
329 }
330 #endif
331 
332 static void noinstr handle_riscv_irq(struct pt_regs *regs)
333 {
334 	struct pt_regs *old_regs;
335 
336 	irq_enter_rcu();
337 	old_regs = set_irq_regs(regs);
338 	handle_arch_irq(regs);
339 	set_irq_regs(old_regs);
340 	irq_exit_rcu();
341 }
342 
343 asmlinkage void noinstr do_irq(struct pt_regs *regs)
344 {
345 	irqentry_state_t state = irqentry_enter(regs);
346 #ifdef CONFIG_IRQ_STACKS
347 	if (on_thread_stack()) {
348 		ulong *sp = per_cpu(irq_stack_ptr, smp_processor_id())
349 					+ IRQ_STACK_SIZE/sizeof(ulong);
350 		__asm__ __volatile(
351 		"addi	sp, sp, -"RISCV_SZPTR  "\n"
352 		REG_S"  ra, (sp)		\n"
353 		"addi	sp, sp, -"RISCV_SZPTR  "\n"
354 		REG_S"  s0, (sp)		\n"
355 		"addi	s0, sp, 2*"RISCV_SZPTR "\n"
356 		"move	sp, %[sp]		\n"
357 		"move	a0, %[regs]		\n"
358 		"call	handle_riscv_irq	\n"
359 		"addi	sp, s0, -2*"RISCV_SZPTR"\n"
360 		REG_L"  s0, (sp)		\n"
361 		"addi	sp, sp, "RISCV_SZPTR   "\n"
362 		REG_L"  ra, (sp)		\n"
363 		"addi	sp, sp, "RISCV_SZPTR   "\n"
364 		:
365 		: [sp] "r" (sp), [regs] "r" (regs)
366 		: "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7",
367 		  "t0", "t1", "t2", "t3", "t4", "t5", "t6",
368 #ifndef CONFIG_FRAME_POINTER
369 		  "s0",
370 #endif
371 		  "memory");
372 	} else
373 #endif
374 		handle_riscv_irq(regs);
375 
376 	irqentry_exit(regs, state);
377 }
378 
379 #ifdef CONFIG_GENERIC_BUG
380 int is_valid_bugaddr(unsigned long pc)
381 {
382 	bug_insn_t insn;
383 
384 	if (pc < VMALLOC_START)
385 		return 0;
386 	if (get_kernel_nofault(insn, (bug_insn_t *)pc))
387 		return 0;
388 	if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
389 		return (insn == __BUG_INSN_32);
390 	else
391 		return ((insn & __COMPRESSED_INSN_MASK) == __BUG_INSN_16);
392 }
393 #endif /* CONFIG_GENERIC_BUG */
394 
395 #ifdef CONFIG_VMAP_STACK
396 /*
397  * Extra stack space that allows us to provide panic messages when the kernel
398  * has overflowed its stack.
399  */
400 static DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)],
401 		overflow_stack)__aligned(16);
402 /*
403  * A temporary stack for use by handle_kernel_stack_overflow.  This is used so
404  * we can call into C code to get the per-hart overflow stack.  Usage of this
405  * stack must be protected by spin_shadow_stack.
406  */
407 long shadow_stack[SHADOW_OVERFLOW_STACK_SIZE/sizeof(long)] __aligned(16);
408 
409 /*
410  * A pseudo spinlock to protect the shadow stack from being used by multiple
411  * harts concurrently.  This isn't a real spinlock because the lock side must
412  * be taken without a valid stack and only a single register, it's only taken
413  * while in the process of panicing anyway so the performance and error
414  * checking a proper spinlock gives us doesn't matter.
415  */
416 unsigned long spin_shadow_stack;
417 
418 asmlinkage unsigned long get_overflow_stack(void)
419 {
420 	return (unsigned long)this_cpu_ptr(overflow_stack) +
421 		OVERFLOW_STACK_SIZE;
422 }
423 
424 asmlinkage void handle_bad_stack(struct pt_regs *regs)
425 {
426 	unsigned long tsk_stk = (unsigned long)current->stack;
427 	unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
428 
429 	/*
430 	 * We're done with the shadow stack by this point, as we're on the
431 	 * overflow stack.  Tell any other concurrent overflowing harts that
432 	 * they can proceed with panicing by releasing the pseudo-spinlock.
433 	 *
434 	 * This pairs with an amoswap.aq in handle_kernel_stack_overflow.
435 	 */
436 	smp_store_release(&spin_shadow_stack, 0);
437 
438 	console_verbose();
439 
440 	pr_emerg("Insufficient stack space to handle exception!\n");
441 	pr_emerg("Task stack:     [0x%016lx..0x%016lx]\n",
442 			tsk_stk, tsk_stk + THREAD_SIZE);
443 	pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
444 			ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
445 
446 	__show_regs(regs);
447 	panic("Kernel stack overflow");
448 
449 	for (;;)
450 		wait_for_interrupt();
451 }
452 #endif
453