ARM: mm: Free memblock from free_initrd_mem()
[platform/kernel/linux-rpi.git] / arch / riscv / kernel / traps.c
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_ERRATA_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)] __aligned(16);
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 /*
222  * A pseudo spinlock to protect the shadow stack from being used by multiple
223  * harts concurrently.  This isn't a real spinlock because the lock side must
224  * be taken without a valid stack and only a single register, it's only taken
225  * while in the process of panicing anyway so the performance and error
226  * checking a proper spinlock gives us doesn't matter.
227  */
228 unsigned long spin_shadow_stack;
229
230 asmlinkage void handle_bad_stack(struct pt_regs *regs)
231 {
232         unsigned long tsk_stk = (unsigned long)current->stack;
233         unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
234
235         /*
236          * We're done with the shadow stack by this point, as we're on the
237          * overflow stack.  Tell any other concurrent overflowing harts that
238          * they can proceed with panicing by releasing the pseudo-spinlock.
239          *
240          * This pairs with an amoswap.aq in handle_kernel_stack_overflow.
241          */
242         smp_store_release(&spin_shadow_stack, 0);
243
244         console_verbose();
245
246         pr_emerg("Insufficient stack space to handle exception!\n");
247         pr_emerg("Task stack:     [0x%016lx..0x%016lx]\n",
248                         tsk_stk, tsk_stk + THREAD_SIZE);
249         pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
250                         ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
251
252         __show_regs(regs);
253         panic("Kernel stack overflow");
254
255         for (;;)
256                 wait_for_interrupt();
257 }
258 #endif