1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Regents of the University of California
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>
17 #include <linux/module.h>
18 #include <linux/irq.h>
19 #include <linux/kexec.h>
20 #include <linux/entry-common.h>
22 #include <asm/asm-prototypes.h>
25 #include <asm/processor.h>
26 #include <asm/ptrace.h>
27 #include <asm/syscall.h>
28 #include <asm/thread_info.h>
29 #include <asm/vector.h>
31 int show_unhandled_signals = 1;
33 static DEFINE_SPINLOCK(die_lock);
35 static void dump_kernel_instr(const char *loglvl, struct pt_regs *regs)
37 char str[sizeof("0000 ") * 12 + 2 + 1], *p = str;
38 const u16 *insns = (u16 *)instruction_pointer(regs);
43 for (i = -10; i < 2; i++) {
44 bad = get_kernel_nofault(val, &insns[i]);
46 p += sprintf(p, i == 0 ? "(%04hx) " : "%04hx ", val);
48 printk("%sCode: Unable to access instruction at 0x%px.\n",
53 printk("%sCode: %s\n", loglvl, str);
56 void die(struct pt_regs *regs, const char *str)
58 static int die_counter;
65 spin_lock_irqsave(&die_lock, flags);
69 pr_emerg("%s [#%d]\n", str, ++die_counter);
73 dump_kernel_instr(KERN_EMERG, regs);
76 cause = regs ? regs->cause : -1;
77 ret = notify_die(DIE_OOPS, str, regs, 0, cause, SIGSEGV);
79 if (kexec_should_crash(current))
83 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
84 spin_unlock_irqrestore(&die_lock, flags);
88 panic("Fatal exception in interrupt");
90 panic("Fatal exception");
91 if (ret != NOTIFY_STOP)
92 make_task_dead(SIGSEGV);
95 void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr)
97 struct task_struct *tsk = current;
99 if (show_unhandled_signals && unhandled_signal(tsk, signo)
100 && printk_ratelimit()) {
101 pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT,
102 tsk->comm, task_pid_nr(tsk), signo, code, addr);
103 print_vma_addr(KERN_CONT " in ", instruction_pointer(regs));
108 force_sig_fault(signo, code, (void __user *)addr);
111 static void do_trap_error(struct pt_regs *regs, int signo, int code,
112 unsigned long addr, const char *str)
114 current->thread.bad_cause = regs->cause;
116 if (user_mode(regs)) {
117 do_trap(regs, signo, code, addr);
119 if (!fixup_exception(regs))
124 #if defined(CONFIG_XIP_KERNEL) && defined(CONFIG_RISCV_ALTERNATIVE)
125 #define __trap_section __noinstr_section(".xip.traps")
127 #define __trap_section noinstr
129 #define DO_ERROR_INFO(name, signo, code, str) \
130 asmlinkage __visible __trap_section void name(struct pt_regs *regs) \
132 if (user_mode(regs)) { \
133 irqentry_enter_from_user_mode(regs); \
134 do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \
135 irqentry_exit_to_user_mode(regs); \
137 irqentry_state_t state = irqentry_nmi_enter(regs); \
138 do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \
139 irqentry_nmi_exit(regs, state); \
143 DO_ERROR_INFO(do_trap_unknown,
144 SIGILL, ILL_ILLTRP, "unknown exception");
145 DO_ERROR_INFO(do_trap_insn_misaligned,
146 SIGBUS, BUS_ADRALN, "instruction address misaligned");
147 DO_ERROR_INFO(do_trap_insn_fault,
148 SIGSEGV, SEGV_ACCERR, "instruction access fault");
150 asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *regs)
152 if (user_mode(regs)) {
153 irqentry_enter_from_user_mode(regs);
157 if (!riscv_v_first_use_handler(regs))
158 do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
159 "Oops - illegal instruction");
161 irqentry_exit_to_user_mode(regs);
163 irqentry_state_t state = irqentry_nmi_enter(regs);
165 do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
166 "Oops - illegal instruction");
168 irqentry_nmi_exit(regs, state);
172 DO_ERROR_INFO(do_trap_load_fault,
173 SIGSEGV, SEGV_ACCERR, "load access fault");
174 #ifndef CONFIG_RISCV_M_MODE
175 DO_ERROR_INFO(do_trap_load_misaligned,
176 SIGBUS, BUS_ADRALN, "Oops - load address misaligned");
177 DO_ERROR_INFO(do_trap_store_misaligned,
178 SIGBUS, BUS_ADRALN, "Oops - store (or AMO) address misaligned");
180 int handle_misaligned_load(struct pt_regs *regs);
181 int handle_misaligned_store(struct pt_regs *regs);
183 asmlinkage __visible __trap_section void do_trap_load_misaligned(struct pt_regs *regs)
185 if (user_mode(regs)) {
186 irqentry_enter_from_user_mode(regs);
188 if (handle_misaligned_load(regs))
189 do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
190 "Oops - load address misaligned");
192 irqentry_exit_to_user_mode(regs);
194 irqentry_state_t state = irqentry_nmi_enter(regs);
196 if (handle_misaligned_load(regs))
197 do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
198 "Oops - load address misaligned");
200 irqentry_nmi_exit(regs, state);
204 asmlinkage __visible __trap_section void do_trap_store_misaligned(struct pt_regs *regs)
206 if (user_mode(regs)) {
207 irqentry_enter_from_user_mode(regs);
209 if (handle_misaligned_store(regs))
210 do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
211 "Oops - store (or AMO) address misaligned");
213 irqentry_exit_to_user_mode(regs);
215 irqentry_state_t state = irqentry_nmi_enter(regs);
217 if (handle_misaligned_store(regs))
218 do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
219 "Oops - store (or AMO) address misaligned");
221 irqentry_nmi_exit(regs, state);
225 DO_ERROR_INFO(do_trap_store_fault,
226 SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
227 DO_ERROR_INFO(do_trap_ecall_s,
228 SIGILL, ILL_ILLTRP, "environment call from S-mode");
229 DO_ERROR_INFO(do_trap_ecall_m,
230 SIGILL, ILL_ILLTRP, "environment call from M-mode");
232 static inline unsigned long get_break_insn_length(unsigned long pc)
236 if (get_kernel_nofault(insn, (bug_insn_t *)pc))
239 return GET_INSN_LENGTH(insn);
242 void handle_break(struct pt_regs *regs)
244 #ifdef CONFIG_KPROBES
245 if (kprobe_single_step_handler(regs))
248 if (kprobe_breakpoint_handler(regs))
251 #ifdef CONFIG_UPROBES
252 if (uprobe_single_step_handler(regs))
255 if (uprobe_breakpoint_handler(regs))
258 current->thread.bad_cause = regs->cause;
261 force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->epc);
263 else if (notify_die(DIE_TRAP, "EBREAK", regs, 0, regs->cause, SIGTRAP)
267 else if (report_bug(regs->epc, regs) == BUG_TRAP_TYPE_WARN)
268 regs->epc += get_break_insn_length(regs->epc);
270 die(regs, "Kernel BUG");
273 asmlinkage __visible __trap_section void do_trap_break(struct pt_regs *regs)
275 if (user_mode(regs)) {
276 irqentry_enter_from_user_mode(regs);
280 irqentry_exit_to_user_mode(regs);
282 irqentry_state_t state = irqentry_nmi_enter(regs);
286 irqentry_nmi_exit(regs, state);
290 asmlinkage __visible __trap_section void do_trap_ecall_u(struct pt_regs *regs)
292 if (user_mode(regs)) {
293 ulong syscall = regs->a7;
296 regs->orig_a0 = regs->a0;
298 syscall = syscall_enter_from_user_mode(regs, syscall);
300 if (syscall < NR_syscalls)
301 syscall_handler(regs, syscall);
305 syscall_exit_to_user_mode(regs);
307 irqentry_state_t state = irqentry_nmi_enter(regs);
309 do_trap_error(regs, SIGILL, ILL_ILLTRP, regs->epc,
310 "Oops - environment call from U-mode");
312 irqentry_nmi_exit(regs, state);
318 asmlinkage __visible noinstr void do_page_fault(struct pt_regs *regs)
320 irqentry_state_t state = irqentry_enter(regs);
322 handle_page_fault(regs);
326 irqentry_exit(regs, state);
330 asmlinkage __visible noinstr void do_irq(struct pt_regs *regs)
332 struct pt_regs *old_regs;
333 irqentry_state_t state = irqentry_enter(regs);
336 old_regs = set_irq_regs(regs);
337 handle_arch_irq(regs);
338 set_irq_regs(old_regs);
341 irqentry_exit(regs, state);
344 #ifdef CONFIG_GENERIC_BUG
345 int is_valid_bugaddr(unsigned long pc)
349 if (pc < VMALLOC_START)
351 if (get_kernel_nofault(insn, (bug_insn_t *)pc))
353 if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
354 return (insn == __BUG_INSN_32);
356 return ((insn & __COMPRESSED_INSN_MASK) == __BUG_INSN_16);
358 #endif /* CONFIG_GENERIC_BUG */
360 #ifdef CONFIG_VMAP_STACK
362 * Extra stack space that allows us to provide panic messages when the kernel
363 * has overflowed its stack.
365 static DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)],
366 overflow_stack)__aligned(16);
368 * A temporary stack for use by handle_kernel_stack_overflow. This is used so
369 * we can call into C code to get the per-hart overflow stack. Usage of this
370 * stack must be protected by spin_shadow_stack.
372 long shadow_stack[SHADOW_OVERFLOW_STACK_SIZE/sizeof(long)] __aligned(16);
375 * A pseudo spinlock to protect the shadow stack from being used by multiple
376 * harts concurrently. This isn't a real spinlock because the lock side must
377 * be taken without a valid stack and only a single register, it's only taken
378 * while in the process of panicing anyway so the performance and error
379 * checking a proper spinlock gives us doesn't matter.
381 unsigned long spin_shadow_stack;
383 asmlinkage unsigned long get_overflow_stack(void)
385 return (unsigned long)this_cpu_ptr(overflow_stack) +
389 asmlinkage void handle_bad_stack(struct pt_regs *regs)
391 unsigned long tsk_stk = (unsigned long)current->stack;
392 unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
395 * We're done with the shadow stack by this point, as we're on the
396 * overflow stack. Tell any other concurrent overflowing harts that
397 * they can proceed with panicing by releasing the pseudo-spinlock.
399 * This pairs with an amoswap.aq in handle_kernel_stack_overflow.
401 smp_store_release(&spin_shadow_stack, 0);
405 pr_emerg("Insufficient stack space to handle exception!\n");
406 pr_emerg("Task stack: [0x%016lx..0x%016lx]\n",
407 tsk_stk, tsk_stk + THREAD_SIZE);
408 pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
409 ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
412 panic("Kernel stack overflow");
415 wait_for_interrupt();