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>
20 #include <asm/asm-prototypes.h>
22 #include <asm/processor.h>
23 #include <asm/ptrace.h>
26 int show_unhandled_signals = 1;
28 static DEFINE_SPINLOCK(die_lock);
30 void die(struct pt_regs *regs, const char *str)
32 static int die_counter;
37 spin_lock_irq(&die_lock);
41 pr_emerg("%s [#%d]\n", str, ++die_counter);
45 ret = notify_die(DIE_OOPS, str, regs, 0, regs->cause, SIGSEGV);
48 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
49 spin_unlock_irq(&die_lock);
53 panic("Fatal exception in interrupt");
55 panic("Fatal exception");
56 if (ret != NOTIFY_STOP)
60 void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr)
62 struct task_struct *tsk = current;
64 if (show_unhandled_signals && unhandled_signal(tsk, signo)
65 && printk_ratelimit()) {
66 pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT,
67 tsk->comm, task_pid_nr(tsk), signo, code, addr);
68 print_vma_addr(KERN_CONT " in ", instruction_pointer(regs));
73 force_sig_fault(signo, code, (void __user *)addr);
76 static void do_trap_error(struct pt_regs *regs, int signo, int code,
77 unsigned long addr, const char *str)
79 current->thread.bad_cause = regs->cause;
81 if (user_mode(regs)) {
82 do_trap(regs, signo, code, addr);
84 if (!fixup_exception(regs))
89 #define DO_ERROR_INFO(name, signo, code, str) \
90 asmlinkage __visible void name(struct pt_regs *regs) \
92 do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \
95 DO_ERROR_INFO(do_trap_unknown,
96 SIGILL, ILL_ILLTRP, "unknown exception");
97 DO_ERROR_INFO(do_trap_insn_misaligned,
98 SIGBUS, BUS_ADRALN, "instruction address misaligned");
99 DO_ERROR_INFO(do_trap_insn_fault,
100 SIGSEGV, SEGV_ACCERR, "instruction access fault");
101 DO_ERROR_INFO(do_trap_insn_illegal,
102 SIGILL, ILL_ILLOPC, "illegal instruction");
103 DO_ERROR_INFO(do_trap_load_fault,
104 SIGSEGV, SEGV_ACCERR, "load access fault");
105 #ifndef CONFIG_RISCV_M_MODE
106 DO_ERROR_INFO(do_trap_load_misaligned,
107 SIGBUS, BUS_ADRALN, "Oops - load address misaligned");
108 DO_ERROR_INFO(do_trap_store_misaligned,
109 SIGBUS, BUS_ADRALN, "Oops - store (or AMO) address misaligned");
111 int handle_misaligned_load(struct pt_regs *regs);
112 int handle_misaligned_store(struct pt_regs *regs);
114 asmlinkage void do_trap_load_misaligned(struct pt_regs *regs)
116 if (!handle_misaligned_load(regs))
118 do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
119 "Oops - load address misaligned");
122 asmlinkage void do_trap_store_misaligned(struct pt_regs *regs)
124 if (!handle_misaligned_store(regs))
126 do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
127 "Oops - store (or AMO) address misaligned");
130 DO_ERROR_INFO(do_trap_store_fault,
131 SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
132 DO_ERROR_INFO(do_trap_ecall_u,
133 SIGILL, ILL_ILLTRP, "environment call from U-mode");
134 DO_ERROR_INFO(do_trap_ecall_s,
135 SIGILL, ILL_ILLTRP, "environment call from S-mode");
136 DO_ERROR_INFO(do_trap_ecall_m,
137 SIGILL, ILL_ILLTRP, "environment call from M-mode");
139 static inline unsigned long get_break_insn_length(unsigned long pc)
143 if (get_kernel_nofault(insn, (bug_insn_t *)pc))
146 return GET_INSN_LENGTH(insn);
149 asmlinkage __visible void do_trap_break(struct pt_regs *regs)
151 #ifdef CONFIG_KPROBES
152 if (kprobe_single_step_handler(regs))
155 if (kprobe_breakpoint_handler(regs))
158 #ifdef CONFIG_UPROBES
159 if (uprobe_single_step_handler(regs))
162 if (uprobe_breakpoint_handler(regs))
165 current->thread.bad_cause = regs->cause;
168 force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->epc);
170 else if (notify_die(DIE_TRAP, "EBREAK", regs, 0, regs->cause, SIGTRAP)
174 else if (report_bug(regs->epc, regs) == BUG_TRAP_TYPE_WARN)
175 regs->epc += get_break_insn_length(regs->epc);
177 die(regs, "Kernel BUG");
179 NOKPROBE_SYMBOL(do_trap_break);
181 #ifdef CONFIG_GENERIC_BUG
182 int is_valid_bugaddr(unsigned long pc)
186 if (pc < VMALLOC_START)
188 if (get_kernel_nofault(insn, (bug_insn_t *)pc))
190 if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
191 return (insn == __BUG_INSN_32);
193 return ((insn & __COMPRESSED_INSN_MASK) == __BUG_INSN_16);
195 #endif /* CONFIG_GENERIC_BUG */
197 /* stvec & scratch is already set from head.S */
198 void __init trap_init(void)
202 #ifdef CONFIG_VMAP_STACK
203 static DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)],
204 overflow_stack)__aligned(16);
206 * shadow stack, handled_ kernel_ stack_ overflow(in kernel/entry.S) is used
207 * to get per-cpu overflow stack(get_overflow_stack).
209 long shadow_stack[SHADOW_OVERFLOW_STACK_SIZE/sizeof(long)];
210 asmlinkage unsigned long get_overflow_stack(void)
212 return (unsigned long)this_cpu_ptr(overflow_stack) +
217 * A pseudo spinlock to protect the shadow stack from being used by multiple
218 * harts concurrently. This isn't a real spinlock because the lock side must
219 * be taken without a valid stack and only a single register, it's only taken
220 * while in the process of panicing anyway so the performance and error
221 * checking a proper spinlock gives us doesn't matter.
223 unsigned long spin_shadow_stack;
225 asmlinkage void handle_bad_stack(struct pt_regs *regs)
227 unsigned long tsk_stk = (unsigned long)current->stack;
228 unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
231 * We're done with the shadow stack by this point, as we're on the
232 * overflow stack. Tell any other concurrent overflowing harts that
233 * they can proceed with panicing by releasing the pseudo-spinlock.
235 * This pairs with an amoswap.aq in handle_kernel_stack_overflow.
237 smp_store_release(&spin_shadow_stack, 0);
241 pr_emerg("Insufficient stack space to handle exception!\n");
242 pr_emerg("Task stack: [0x%016lx..0x%016lx]\n",
243 tsk_stk, tsk_stk + THREAD_SIZE);
244 pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
245 ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
248 panic("Kernel stack overflow");
251 wait_for_interrupt();