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>
16 #include <linux/uprobes.h>
17 #include <asm/uprobes.h>
19 #include <linux/module.h>
20 #include <linux/irq.h>
21 #include <linux/kexec.h>
22 #include <linux/entry-common.h>
24 #include <asm/asm-prototypes.h>
28 #include <asm/processor.h>
29 #include <asm/ptrace.h>
30 #include <asm/syscall.h>
31 #include <asm/thread_info.h>
32 #include <asm/vector.h>
33 #include <asm/irq_stack.h>
35 int show_unhandled_signals = 1;
37 static DEFINE_SPINLOCK(die_lock);
39 static int copy_code(struct pt_regs *regs, u16 *val, const u16 *insns)
41 const void __user *uaddr = (__force const void __user *)insns;
44 return get_kernel_nofault(*val, insns);
46 /* The user space code from other tasks cannot be accessed. */
47 if (regs != task_pt_regs(current))
50 return copy_from_user_nofault(val, uaddr, sizeof(*val));
53 static void dump_instr(const char *loglvl, struct pt_regs *regs)
55 char str[sizeof("0000 ") * 12 + 2 + 1], *p = str;
56 const u16 *insns = (u16 *)instruction_pointer(regs);
61 for (i = -10; i < 2; i++) {
62 bad = copy_code(regs, &val, &insns[i]);
64 p += sprintf(p, i == 0 ? "(%04hx) " : "%04hx ", val);
66 printk("%sCode: Unable to access instruction at 0x%px.\n",
71 printk("%sCode: %s\n", loglvl, str);
74 void die(struct pt_regs *regs, const char *str)
76 static int die_counter;
83 spin_lock_irqsave(&die_lock, flags);
87 pr_emerg("%s [#%d]\n", str, ++die_counter);
91 dump_instr(KERN_EMERG, regs);
94 cause = regs ? regs->cause : -1;
95 ret = notify_die(DIE_OOPS, str, regs, 0, cause, SIGSEGV);
97 if (kexec_should_crash(current))
101 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
102 spin_unlock_irqrestore(&die_lock, flags);
106 panic("Fatal exception in interrupt");
108 panic("Fatal exception");
109 if (ret != NOTIFY_STOP)
110 make_task_dead(SIGSEGV);
113 void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr)
115 struct task_struct *tsk = current;
117 if (show_unhandled_signals && unhandled_signal(tsk, signo)
118 && printk_ratelimit()) {
119 pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT,
120 tsk->comm, task_pid_nr(tsk), signo, code, addr);
121 print_vma_addr(KERN_CONT " in ", instruction_pointer(regs));
124 dump_instr(KERN_EMERG, regs);
127 force_sig_fault(signo, code, (void __user *)addr);
130 static void do_trap_error(struct pt_regs *regs, int signo, int code,
131 unsigned long addr, const char *str)
133 current->thread.bad_cause = regs->cause;
135 if (user_mode(regs)) {
136 do_trap(regs, signo, code, addr);
138 if (!fixup_exception(regs))
143 #if defined(CONFIG_XIP_KERNEL) && defined(CONFIG_RISCV_ALTERNATIVE)
144 #define __trap_section __noinstr_section(".xip.traps")
146 #define __trap_section noinstr
148 #define DO_ERROR_INFO(name, signo, code, str) \
149 asmlinkage __visible __trap_section void name(struct pt_regs *regs) \
151 if (user_mode(regs)) { \
152 irqentry_enter_from_user_mode(regs); \
153 do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \
154 irqentry_exit_to_user_mode(regs); \
156 irqentry_state_t state = irqentry_nmi_enter(regs); \
157 do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \
158 irqentry_nmi_exit(regs, state); \
162 DO_ERROR_INFO(do_trap_unknown,
163 SIGILL, ILL_ILLTRP, "unknown exception");
164 DO_ERROR_INFO(do_trap_insn_misaligned,
165 SIGBUS, BUS_ADRALN, "instruction address misaligned");
166 DO_ERROR_INFO(do_trap_insn_fault,
167 SIGSEGV, SEGV_ACCERR, "instruction access fault");
169 asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *regs)
173 if (user_mode(regs)) {
174 irqentry_enter_from_user_mode(regs);
178 handled = riscv_v_first_use_handler(regs);
183 do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
184 "Oops - illegal instruction");
186 irqentry_exit_to_user_mode(regs);
188 irqentry_state_t state = irqentry_nmi_enter(regs);
190 do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
191 "Oops - illegal instruction");
193 irqentry_nmi_exit(regs, state);
197 DO_ERROR_INFO(do_trap_load_fault,
198 SIGSEGV, SEGV_ACCERR, "load access fault");
200 asmlinkage __visible __trap_section void do_trap_load_misaligned(struct pt_regs *regs)
202 if (user_mode(regs)) {
203 irqentry_enter_from_user_mode(regs);
205 if (handle_misaligned_load(regs))
206 do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
207 "Oops - load address misaligned");
209 irqentry_exit_to_user_mode(regs);
211 irqentry_state_t state = irqentry_nmi_enter(regs);
213 if (handle_misaligned_load(regs))
214 do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
215 "Oops - load address misaligned");
217 irqentry_nmi_exit(regs, state);
221 asmlinkage __visible __trap_section void do_trap_store_misaligned(struct pt_regs *regs)
223 if (user_mode(regs)) {
224 irqentry_enter_from_user_mode(regs);
226 if (handle_misaligned_store(regs))
227 do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
228 "Oops - store (or AMO) address misaligned");
230 irqentry_exit_to_user_mode(regs);
232 irqentry_state_t state = irqentry_nmi_enter(regs);
234 if (handle_misaligned_store(regs))
235 do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
236 "Oops - store (or AMO) address misaligned");
238 irqentry_nmi_exit(regs, state);
241 DO_ERROR_INFO(do_trap_store_fault,
242 SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
243 DO_ERROR_INFO(do_trap_ecall_s,
244 SIGILL, ILL_ILLTRP, "environment call from S-mode");
245 DO_ERROR_INFO(do_trap_ecall_m,
246 SIGILL, ILL_ILLTRP, "environment call from M-mode");
248 static inline unsigned long get_break_insn_length(unsigned long pc)
252 if (get_kernel_nofault(insn, (bug_insn_t *)pc))
255 return GET_INSN_LENGTH(insn);
258 static bool probe_single_step_handler(struct pt_regs *regs)
260 bool user = user_mode(regs);
262 return user ? uprobe_single_step_handler(regs) : kprobe_single_step_handler(regs);
265 static bool probe_breakpoint_handler(struct pt_regs *regs)
267 bool user = user_mode(regs);
269 return user ? uprobe_breakpoint_handler(regs) : kprobe_breakpoint_handler(regs);
272 void handle_break(struct pt_regs *regs)
274 if (probe_single_step_handler(regs))
277 if (probe_breakpoint_handler(regs))
280 current->thread.bad_cause = regs->cause;
283 force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->epc);
285 else if (notify_die(DIE_TRAP, "EBREAK", regs, 0, regs->cause, SIGTRAP)
289 else if (report_bug(regs->epc, regs) == BUG_TRAP_TYPE_WARN ||
290 handle_cfi_failure(regs) == BUG_TRAP_TYPE_WARN)
291 regs->epc += get_break_insn_length(regs->epc);
293 die(regs, "Kernel BUG");
296 asmlinkage __visible __trap_section void do_trap_break(struct pt_regs *regs)
298 if (user_mode(regs)) {
299 irqentry_enter_from_user_mode(regs);
303 irqentry_exit_to_user_mode(regs);
305 irqentry_state_t state = irqentry_nmi_enter(regs);
309 irqentry_nmi_exit(regs, state);
313 asmlinkage __visible __trap_section void do_trap_ecall_u(struct pt_regs *regs)
315 if (user_mode(regs)) {
316 long syscall = regs->a7;
319 regs->orig_a0 = regs->a0;
321 riscv_v_vstate_discard(regs);
323 syscall = syscall_enter_from_user_mode(regs, syscall);
325 if (syscall >= 0 && syscall < NR_syscalls)
326 syscall_handler(regs, syscall);
327 else if (syscall != -1)
330 syscall_exit_to_user_mode(regs);
332 irqentry_state_t state = irqentry_nmi_enter(regs);
334 do_trap_error(regs, SIGILL, ILL_ILLTRP, regs->epc,
335 "Oops - environment call from U-mode");
337 irqentry_nmi_exit(regs, state);
343 asmlinkage __visible noinstr void do_page_fault(struct pt_regs *regs)
345 irqentry_state_t state = irqentry_enter(regs);
347 handle_page_fault(regs);
351 irqentry_exit(regs, state);
355 static void noinstr handle_riscv_irq(struct pt_regs *regs)
357 struct pt_regs *old_regs;
360 old_regs = set_irq_regs(regs);
361 handle_arch_irq(regs);
362 set_irq_regs(old_regs);
366 asmlinkage void noinstr do_irq(struct pt_regs *regs)
368 irqentry_state_t state = irqentry_enter(regs);
370 if (IS_ENABLED(CONFIG_IRQ_STACKS) && on_thread_stack())
371 call_on_irq_stack(regs, handle_riscv_irq);
373 handle_riscv_irq(regs);
375 irqentry_exit(regs, state);
378 #ifdef CONFIG_GENERIC_BUG
379 int is_valid_bugaddr(unsigned long pc)
383 if (pc < VMALLOC_START)
385 if (get_kernel_nofault(insn, (bug_insn_t *)pc))
387 if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
388 return (insn == __BUG_INSN_32);
390 return ((insn & __COMPRESSED_INSN_MASK) == __BUG_INSN_16);
392 #endif /* CONFIG_GENERIC_BUG */
394 #ifdef CONFIG_VMAP_STACK
395 DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)],
396 overflow_stack)__aligned(16);
398 asmlinkage void handle_bad_stack(struct pt_regs *regs)
400 unsigned long tsk_stk = (unsigned long)current->stack;
401 unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
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();