GNU Linux-libre 4.19.295-gnu1
[releases.git] / arch / riscv / kernel / traps.c
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 #include <linux/kexec.h>
26
27 #include <asm/processor.h>
28 #include <asm/ptrace.h>
29 #include <asm/csr.h>
30
31 int show_unhandled_signals = 1;
32
33 extern asmlinkage void handle_exception(void);
34
35 static DEFINE_SPINLOCK(die_lock);
36
37 void die(struct pt_regs *regs, const char *str)
38 {
39         static int die_counter;
40         int ret;
41
42         oops_enter();
43
44         spin_lock_irq(&die_lock);
45         console_verbose();
46         bust_spinlocks(1);
47
48         pr_emerg("%s [#%d]\n", str, ++die_counter);
49         print_modules();
50         show_regs(regs);
51
52         ret = notify_die(DIE_OOPS, str, regs, 0, regs->scause, SIGSEGV);
53
54         if (regs && kexec_should_crash(current))
55                 crash_kexec(regs);
56
57         bust_spinlocks(0);
58         add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
59         spin_unlock_irq(&die_lock);
60         oops_exit();
61
62         if (in_interrupt())
63                 panic("Fatal exception in interrupt");
64         if (panic_on_oops)
65                 panic("Fatal exception");
66         if (ret != NOTIFY_STOP)
67                 make_task_dead(SIGSEGV);
68 }
69
70 void do_trap(struct pt_regs *regs, int signo, int code,
71         unsigned long addr, struct task_struct *tsk)
72 {
73         if (show_unhandled_signals && unhandled_signal(tsk, signo)
74             && printk_ratelimit()) {
75                 pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT,
76                         tsk->comm, task_pid_nr(tsk), signo, code, addr);
77                 print_vma_addr(KERN_CONT " in ", GET_IP(regs));
78                 pr_cont("\n");
79                 show_regs(regs);
80         }
81
82         force_sig_fault(signo, code, (void __user *)addr, tsk);
83 }
84
85 static void do_trap_error(struct pt_regs *regs, int signo, int code,
86         unsigned long addr, const char *str)
87 {
88         if (user_mode(regs)) {
89                 do_trap(regs, signo, code, addr, current);
90         } else {
91                 if (!fixup_exception(regs))
92                         die(regs, str);
93         }
94 }
95
96 #define DO_ERROR_INFO(name, signo, code, str)                           \
97 asmlinkage void name(struct pt_regs *regs)                              \
98 {                                                                       \
99         do_trap_error(regs, signo, code, regs->sepc, "Oops - " str);    \
100 }
101
102 DO_ERROR_INFO(do_trap_unknown,
103         SIGILL, ILL_ILLTRP, "unknown exception");
104 DO_ERROR_INFO(do_trap_insn_misaligned,
105         SIGBUS, BUS_ADRALN, "instruction address misaligned");
106 DO_ERROR_INFO(do_trap_insn_fault,
107         SIGSEGV, SEGV_ACCERR, "instruction access fault");
108 DO_ERROR_INFO(do_trap_insn_illegal,
109         SIGILL, ILL_ILLOPC, "illegal instruction");
110 DO_ERROR_INFO(do_trap_load_misaligned,
111         SIGBUS, BUS_ADRALN, "load address misaligned");
112 DO_ERROR_INFO(do_trap_load_fault,
113         SIGSEGV, SEGV_ACCERR, "load access fault");
114 DO_ERROR_INFO(do_trap_store_misaligned,
115         SIGBUS, BUS_ADRALN, "store (or AMO) address misaligned");
116 DO_ERROR_INFO(do_trap_store_fault,
117         SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
118 DO_ERROR_INFO(do_trap_ecall_u,
119         SIGILL, ILL_ILLTRP, "environment call from U-mode");
120 DO_ERROR_INFO(do_trap_ecall_s,
121         SIGILL, ILL_ILLTRP, "environment call from S-mode");
122 DO_ERROR_INFO(do_trap_ecall_m,
123         SIGILL, ILL_ILLTRP, "environment call from M-mode");
124
125 asmlinkage void do_trap_break(struct pt_regs *regs)
126 {
127 #ifdef CONFIG_GENERIC_BUG
128         if (!user_mode(regs)) {
129                 enum bug_trap_type type;
130
131                 type = report_bug(regs->sepc, regs);
132                 switch (type) {
133                 case BUG_TRAP_TYPE_NONE:
134                         break;
135                 case BUG_TRAP_TYPE_WARN:
136                         regs->sepc += sizeof(bug_insn_t);
137                         return;
138                 case BUG_TRAP_TYPE_BUG:
139                         die(regs, "Kernel BUG");
140                 }
141         }
142 #endif /* CONFIG_GENERIC_BUG */
143
144         force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)(regs->sepc), current);
145 }
146
147 #ifdef CONFIG_GENERIC_BUG
148 int is_valid_bugaddr(unsigned long pc)
149 {
150         bug_insn_t insn;
151
152         if (pc < PAGE_OFFSET)
153                 return 0;
154         if (probe_kernel_address((bug_insn_t *)pc, insn))
155                 return 0;
156         return (insn == __BUG_INSN);
157 }
158 #endif /* CONFIG_GENERIC_BUG */
159
160 void __init trap_init(void)
161 {
162         /*
163          * Set sup0 scratch register to 0, indicating to exception vector
164          * that we are presently executing in the kernel
165          */
166         csr_write(sscratch, 0);
167         /* Set the exception vector address */
168         csr_write(stvec, &handle_exception);
169         /* Enable all interrupts */
170         csr_write(sie, -1);
171 }