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>
21 #include <asm/asm-prototypes.h>
24 #include <asm/processor.h>
25 #include <asm/ptrace.h>
26 #include <asm/thread_info.h>
28 int show_unhandled_signals = 1;
30 static DEFINE_SPINLOCK(die_lock);
32 void die(struct pt_regs *regs, const char *str)
34 static int die_counter;
39 spin_lock_irq(&die_lock);
43 pr_emerg("%s [#%d]\n", str, ++die_counter);
47 ret = notify_die(DIE_OOPS, str, regs, 0, regs->cause, SIGSEGV);
49 if (regs && kexec_should_crash(current))
53 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
54 spin_unlock_irq(&die_lock);
58 panic("Fatal exception in interrupt");
60 panic("Fatal exception");
61 if (ret != NOTIFY_STOP)
62 make_task_dead(SIGSEGV);
65 void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr)
67 struct task_struct *tsk = current;
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));
78 force_sig_fault(signo, code, (void __user *)addr);
81 static void do_trap_error(struct pt_regs *regs, int signo, int code,
82 unsigned long addr, const char *str)
84 current->thread.bad_cause = regs->cause;
86 if (user_mode(regs)) {
87 do_trap(regs, signo, code, addr);
89 if (!fixup_exception(regs))
94 #if defined(CONFIG_XIP_KERNEL) && defined(CONFIG_RISCV_ALTERNATIVE)
95 #define __trap_section __section(".xip.traps")
97 #define __trap_section
99 #define DO_ERROR_INFO(name, signo, code, str) \
100 asmlinkage __visible __trap_section void name(struct pt_regs *regs) \
102 do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \
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");
121 int handle_misaligned_load(struct pt_regs *regs);
122 int handle_misaligned_store(struct pt_regs *regs);
124 asmlinkage void __trap_section do_trap_load_misaligned(struct pt_regs *regs)
126 if (!handle_misaligned_load(regs))
128 do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
129 "Oops - load address misaligned");
132 asmlinkage void __trap_section do_trap_store_misaligned(struct pt_regs *regs)
134 if (!handle_misaligned_store(regs))
136 do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
137 "Oops - store (or AMO) address misaligned");
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");
149 static inline unsigned long get_break_insn_length(unsigned long pc)
153 if (get_kernel_nofault(insn, (bug_insn_t *)pc))
156 return GET_INSN_LENGTH(insn);
159 asmlinkage __visible __trap_section void do_trap_break(struct pt_regs *regs)
161 #ifdef CONFIG_KPROBES
162 if (kprobe_single_step_handler(regs))
165 if (kprobe_breakpoint_handler(regs))
168 #ifdef CONFIG_UPROBES
169 if (uprobe_single_step_handler(regs))
172 if (uprobe_breakpoint_handler(regs))
175 current->thread.bad_cause = regs->cause;
178 force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->epc);
180 else if (notify_die(DIE_TRAP, "EBREAK", regs, 0, regs->cause, SIGTRAP)
184 else if (report_bug(regs->epc, regs) == BUG_TRAP_TYPE_WARN)
185 regs->epc += get_break_insn_length(regs->epc);
187 die(regs, "Kernel BUG");
189 NOKPROBE_SYMBOL(do_trap_break);
191 #ifdef CONFIG_GENERIC_BUG
192 int is_valid_bugaddr(unsigned long pc)
196 if (pc < VMALLOC_START)
198 if (get_kernel_nofault(insn, (bug_insn_t *)pc))
200 if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
201 return (insn == __BUG_INSN_32);
203 return ((insn & __COMPRESSED_INSN_MASK) == __BUG_INSN_16);
205 #endif /* CONFIG_GENERIC_BUG */
207 #ifdef CONFIG_VMAP_STACK
208 static DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)],
209 overflow_stack)__aligned(16);
211 * shadow stack, handled_ kernel_ stack_ overflow(in kernel/entry.S) is used
212 * to get per-cpu overflow stack(get_overflow_stack).
214 long shadow_stack[SHADOW_OVERFLOW_STACK_SIZE/sizeof(long)];
215 asmlinkage unsigned long get_overflow_stack(void)
217 return (unsigned long)this_cpu_ptr(overflow_stack) +
221 asmlinkage void handle_bad_stack(struct pt_regs *regs)
223 unsigned long tsk_stk = (unsigned long)current->stack;
224 unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
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);
235 panic("Kernel stack overflow");
238 wait_for_interrupt();