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