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>
30 int show_unhandled_signals = 1;
32 static DEFINE_SPINLOCK(die_lock);
34 static void dump_kernel_instr(const char *loglvl, struct pt_regs *regs)
36 char str[sizeof("0000 ") * 12 + 2 + 1], *p = str;
37 const u16 *insns = (u16 *)instruction_pointer(regs);
42 for (i = -10; i < 2; i++) {
43 bad = get_kernel_nofault(val, &insns[i]);
45 p += sprintf(p, i == 0 ? "(%04hx) " : "%04hx ", val);
47 printk("%sCode: Unable to access instruction at 0x%px.\n",
52 printk("%sCode: %s\n", loglvl, str);
55 void die(struct pt_regs *regs, const char *str)
57 static int die_counter;
64 spin_lock_irqsave(&die_lock, flags);
68 pr_emerg("%s [#%d]\n", str, ++die_counter);
72 dump_kernel_instr(KERN_EMERG, regs);
75 cause = regs ? regs->cause : -1;
76 ret = notify_die(DIE_OOPS, str, regs, 0, cause, SIGSEGV);
78 if (kexec_should_crash(current))
82 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
83 spin_unlock_irqrestore(&die_lock, flags);
87 panic("Fatal exception in interrupt");
89 panic("Fatal exception");
90 if (ret != NOTIFY_STOP)
91 make_task_dead(SIGSEGV);
94 void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr)
96 struct task_struct *tsk = current;
98 if (show_unhandled_signals && unhandled_signal(tsk, signo)
99 && printk_ratelimit()) {
100 pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT,
101 tsk->comm, task_pid_nr(tsk), signo, code, addr);
102 print_vma_addr(KERN_CONT " in ", instruction_pointer(regs));
107 force_sig_fault(signo, code, (void __user *)addr);
110 static void do_trap_error(struct pt_regs *regs, int signo, int code,
111 unsigned long addr, const char *str)
113 current->thread.bad_cause = regs->cause;
115 if (user_mode(regs)) {
116 do_trap(regs, signo, code, addr);
118 if (!fixup_exception(regs))
123 #if defined(CONFIG_XIP_KERNEL) && defined(CONFIG_RISCV_ALTERNATIVE)
124 #define __trap_section __noinstr_section(".xip.traps")
126 #define __trap_section noinstr
128 #define DO_ERROR_INFO(name, signo, code, str) \
129 asmlinkage __visible __trap_section void name(struct pt_regs *regs) \
131 if (user_mode(regs)) { \
132 irqentry_enter_from_user_mode(regs); \
133 do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \
134 irqentry_exit_to_user_mode(regs); \
136 irqentry_state_t state = irqentry_nmi_enter(regs); \
137 do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \
138 irqentry_nmi_exit(regs, state); \
142 DO_ERROR_INFO(do_trap_unknown,
143 SIGILL, ILL_ILLTRP, "unknown exception");
144 DO_ERROR_INFO(do_trap_insn_misaligned,
145 SIGBUS, BUS_ADRALN, "instruction address misaligned");
146 DO_ERROR_INFO(do_trap_insn_fault,
147 SIGSEGV, SEGV_ACCERR, "instruction access fault");
148 DO_ERROR_INFO(do_trap_insn_illegal,
149 SIGILL, ILL_ILLOPC, "illegal instruction");
150 DO_ERROR_INFO(do_trap_load_fault,
151 SIGSEGV, SEGV_ACCERR, "load access fault");
152 #ifndef CONFIG_RISCV_M_MODE
153 DO_ERROR_INFO(do_trap_load_misaligned,
154 SIGBUS, BUS_ADRALN, "Oops - load address misaligned");
155 DO_ERROR_INFO(do_trap_store_misaligned,
156 SIGBUS, BUS_ADRALN, "Oops - store (or AMO) address misaligned");
158 int handle_misaligned_load(struct pt_regs *regs);
159 int handle_misaligned_store(struct pt_regs *regs);
161 asmlinkage __visible __trap_section void do_trap_load_misaligned(struct pt_regs *regs)
163 if (user_mode(regs)) {
164 irqentry_enter_from_user_mode(regs);
166 if (handle_misaligned_load(regs))
167 do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
168 "Oops - load address misaligned");
170 irqentry_exit_to_user_mode(regs);
172 irqentry_state_t state = irqentry_nmi_enter(regs);
174 if (handle_misaligned_load(regs))
175 do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
176 "Oops - load address misaligned");
178 irqentry_nmi_exit(regs, state);
182 asmlinkage __visible __trap_section void do_trap_store_misaligned(struct pt_regs *regs)
184 if (user_mode(regs)) {
185 irqentry_enter_from_user_mode(regs);
187 if (handle_misaligned_store(regs))
188 do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
189 "Oops - store (or AMO) address misaligned");
191 irqentry_exit_to_user_mode(regs);
193 irqentry_state_t state = irqentry_nmi_enter(regs);
195 if (handle_misaligned_store(regs))
196 do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
197 "Oops - store (or AMO) address misaligned");
199 irqentry_nmi_exit(regs, state);
203 DO_ERROR_INFO(do_trap_store_fault,
204 SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
205 DO_ERROR_INFO(do_trap_ecall_s,
206 SIGILL, ILL_ILLTRP, "environment call from S-mode");
207 DO_ERROR_INFO(do_trap_ecall_m,
208 SIGILL, ILL_ILLTRP, "environment call from M-mode");
210 static inline unsigned long get_break_insn_length(unsigned long pc)
214 if (get_kernel_nofault(insn, (bug_insn_t *)pc))
217 return GET_INSN_LENGTH(insn);
220 void handle_break(struct pt_regs *regs)
222 #ifdef CONFIG_KPROBES
223 if (kprobe_single_step_handler(regs))
226 if (kprobe_breakpoint_handler(regs))
229 #ifdef CONFIG_UPROBES
230 if (uprobe_single_step_handler(regs))
233 if (uprobe_breakpoint_handler(regs))
236 current->thread.bad_cause = regs->cause;
239 force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->epc);
241 else if (notify_die(DIE_TRAP, "EBREAK", regs, 0, regs->cause, SIGTRAP)
245 else if (report_bug(regs->epc, regs) == BUG_TRAP_TYPE_WARN)
246 regs->epc += get_break_insn_length(regs->epc);
248 die(regs, "Kernel BUG");
251 asmlinkage __visible __trap_section void do_trap_break(struct pt_regs *regs)
253 if (user_mode(regs)) {
254 irqentry_enter_from_user_mode(regs);
258 irqentry_exit_to_user_mode(regs);
260 irqentry_state_t state = irqentry_nmi_enter(regs);
264 irqentry_nmi_exit(regs, state);
268 asmlinkage __visible __trap_section void do_trap_ecall_u(struct pt_regs *regs)
270 if (user_mode(regs)) {
271 ulong syscall = regs->a7;
274 regs->orig_a0 = regs->a0;
276 syscall = syscall_enter_from_user_mode(regs, syscall);
278 if (syscall < NR_syscalls)
279 syscall_handler(regs, syscall);
283 syscall_exit_to_user_mode(regs);
285 irqentry_state_t state = irqentry_nmi_enter(regs);
287 do_trap_error(regs, SIGILL, ILL_ILLTRP, regs->epc,
288 "Oops - environment call from U-mode");
290 irqentry_nmi_exit(regs, state);
296 asmlinkage __visible noinstr void do_page_fault(struct pt_regs *regs)
298 irqentry_state_t state = irqentry_enter(regs);
300 handle_page_fault(regs);
304 irqentry_exit(regs, state);
308 asmlinkage __visible noinstr void do_irq(struct pt_regs *regs)
310 struct pt_regs *old_regs;
311 irqentry_state_t state = irqentry_enter(regs);
314 old_regs = set_irq_regs(regs);
315 handle_arch_irq(regs);
316 set_irq_regs(old_regs);
319 irqentry_exit(regs, state);
322 #ifdef CONFIG_GENERIC_BUG
323 int is_valid_bugaddr(unsigned long pc)
327 if (pc < VMALLOC_START)
329 if (get_kernel_nofault(insn, (bug_insn_t *)pc))
331 if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
332 return (insn == __BUG_INSN_32);
334 return ((insn & __COMPRESSED_INSN_MASK) == __BUG_INSN_16);
336 #endif /* CONFIG_GENERIC_BUG */
338 #ifdef CONFIG_VMAP_STACK
340 * Extra stack space that allows us to provide panic messages when the kernel
341 * has overflowed its stack.
343 static DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)],
344 overflow_stack)__aligned(16);
346 * A temporary stack for use by handle_kernel_stack_overflow. This is used so
347 * we can call into C code to get the per-hart overflow stack. Usage of this
348 * stack must be protected by spin_shadow_stack.
350 long shadow_stack[SHADOW_OVERFLOW_STACK_SIZE/sizeof(long)] __aligned(16);
353 * A pseudo spinlock to protect the shadow stack from being used by multiple
354 * harts concurrently. This isn't a real spinlock because the lock side must
355 * be taken without a valid stack and only a single register, it's only taken
356 * while in the process of panicing anyway so the performance and error
357 * checking a proper spinlock gives us doesn't matter.
359 unsigned long spin_shadow_stack;
361 asmlinkage unsigned long get_overflow_stack(void)
363 return (unsigned long)this_cpu_ptr(overflow_stack) +
367 asmlinkage void handle_bad_stack(struct pt_regs *regs)
369 unsigned long tsk_stk = (unsigned long)current->stack;
370 unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
373 * We're done with the shadow stack by this point, as we're on the
374 * overflow stack. Tell any other concurrent overflowing harts that
375 * they can proceed with panicing by releasing the pseudo-spinlock.
377 * This pairs with an amoswap.aq in handle_kernel_stack_overflow.
379 smp_store_release(&spin_shadow_stack, 0);
383 pr_emerg("Insufficient stack space to handle exception!\n");
384 pr_emerg("Task stack: [0x%016lx..0x%016lx]\n",
385 tsk_stk, tsk_stk + THREAD_SIZE);
386 pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
387 ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
390 panic("Kernel stack overflow");
393 wait_for_interrupt();