xref: /openbmc/linux/arch/riscv/kernel/traps.c (revision 8f762fe5)
1 /*
2  * Copyright (C) 2012 Regents of the University of California
3  *
4  *   This program is free software; you can redistribute it and/or
5  *   modify it under the terms of the GNU General Public License
6  *   as published by the Free Software Foundation, version 2.
7  *
8  *   This program is distributed in the hope that it will be useful,
9  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *   GNU General Public License for more details.
12  */
13 
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/sched.h>
17 #include <linux/sched/debug.h>
18 #include <linux/sched/signal.h>
19 #include <linux/signal.h>
20 #include <linux/kdebug.h>
21 #include <linux/uaccess.h>
22 #include <linux/mm.h>
23 #include <linux/module.h>
24 #include <linux/irq.h>
25 
26 #include <asm/processor.h>
27 #include <asm/ptrace.h>
28 #include <asm/csr.h>
29 
30 int show_unhandled_signals = 1;
31 
32 extern asmlinkage void handle_exception(void);
33 
34 static DEFINE_SPINLOCK(die_lock);
35 
36 void die(struct pt_regs *regs, const char *str)
37 {
38 	static int die_counter;
39 	int ret;
40 
41 	oops_enter();
42 
43 	spin_lock_irq(&die_lock);
44 	console_verbose();
45 	bust_spinlocks(1);
46 
47 	pr_emerg("%s [#%d]\n", str, ++die_counter);
48 	print_modules();
49 	show_regs(regs);
50 
51 	ret = notify_die(DIE_OOPS, str, regs, 0, regs->scause, SIGSEGV);
52 
53 	bust_spinlocks(0);
54 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
55 	spin_unlock_irq(&die_lock);
56 	oops_exit();
57 
58 	if (in_interrupt())
59 		panic("Fatal exception in interrupt");
60 	if (panic_on_oops)
61 		panic("Fatal exception");
62 	if (ret != NOTIFY_STOP)
63 		do_exit(SIGSEGV);
64 }
65 
66 void do_trap(struct pt_regs *regs, int signo, int code,
67 	unsigned long addr, struct task_struct *tsk)
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, tsk);
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 	if (user_mode(regs)) {
85 		do_trap(regs, signo, code, addr, current);
86 	} else {
87 		if (!fixup_exception(regs))
88 			die(regs, str);
89 	}
90 }
91 
92 #define DO_ERROR_INFO(name, signo, code, str)				\
93 asmlinkage void name(struct pt_regs *regs)				\
94 {									\
95 	do_trap_error(regs, signo, code, regs->sepc, "Oops - " str);	\
96 }
97 
98 DO_ERROR_INFO(do_trap_unknown,
99 	SIGILL, ILL_ILLTRP, "unknown exception");
100 DO_ERROR_INFO(do_trap_insn_misaligned,
101 	SIGBUS, BUS_ADRALN, "instruction address misaligned");
102 DO_ERROR_INFO(do_trap_insn_fault,
103 	SIGSEGV, SEGV_ACCERR, "instruction access fault");
104 DO_ERROR_INFO(do_trap_insn_illegal,
105 	SIGILL, ILL_ILLOPC, "illegal instruction");
106 DO_ERROR_INFO(do_trap_load_misaligned,
107 	SIGBUS, BUS_ADRALN, "load address misaligned");
108 DO_ERROR_INFO(do_trap_load_fault,
109 	SIGSEGV, SEGV_ACCERR, "load access fault");
110 DO_ERROR_INFO(do_trap_store_misaligned,
111 	SIGBUS, BUS_ADRALN, "store (or AMO) address misaligned");
112 DO_ERROR_INFO(do_trap_store_fault,
113 	SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
114 DO_ERROR_INFO(do_trap_ecall_u,
115 	SIGILL, ILL_ILLTRP, "environment call from U-mode");
116 DO_ERROR_INFO(do_trap_ecall_s,
117 	SIGILL, ILL_ILLTRP, "environment call from S-mode");
118 DO_ERROR_INFO(do_trap_ecall_m,
119 	SIGILL, ILL_ILLTRP, "environment call from M-mode");
120 
121 #ifdef CONFIG_GENERIC_BUG
122 static inline unsigned long get_break_insn_length(unsigned long pc)
123 {
124 	bug_insn_t insn;
125 
126 	if (probe_kernel_address((bug_insn_t *)pc, insn))
127 		return 0;
128 	return (((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32) ? 4UL : 2UL);
129 }
130 #endif /* CONFIG_GENERIC_BUG */
131 
132 asmlinkage void do_trap_break(struct pt_regs *regs)
133 {
134 #ifdef CONFIG_GENERIC_BUG
135 	if (!user_mode(regs)) {
136 		enum bug_trap_type type;
137 
138 		type = report_bug(regs->sepc, regs);
139 		switch (type) {
140 		case BUG_TRAP_TYPE_NONE:
141 			break;
142 		case BUG_TRAP_TYPE_WARN:
143 			regs->sepc += get_break_insn_length(regs->sepc);
144 			break;
145 		case BUG_TRAP_TYPE_BUG:
146 			die(regs, "Kernel BUG");
147 		}
148 	}
149 #endif /* CONFIG_GENERIC_BUG */
150 
151 	force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)(regs->sepc), current);
152 }
153 
154 #ifdef CONFIG_GENERIC_BUG
155 int is_valid_bugaddr(unsigned long pc)
156 {
157 	bug_insn_t insn;
158 
159 	if (pc < VMALLOC_START)
160 		return 0;
161 	if (probe_kernel_address((bug_insn_t *)pc, insn))
162 		return 0;
163 	if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
164 		return (insn == __BUG_INSN_32);
165 	else
166 		return ((insn & __COMPRESSED_INSN_MASK) == __BUG_INSN_16);
167 }
168 #endif /* CONFIG_GENERIC_BUG */
169 
170 void __init trap_init(void)
171 {
172 	/*
173 	 * Set sup0 scratch register to 0, indicating to exception vector
174 	 * that we are presently executing in the kernel
175 	 */
176 	csr_write(CSR_SSCRATCH, 0);
177 	/* Set the exception vector address */
178 	csr_write(CSR_STVEC, &handle_exception);
179 	/* Enable all interrupts */
180 	csr_write(CSR_SIE, -1);
181 }
182