1 // SPDX-License-Identifier: GPL-2.0
4 #include <linux/types.h>
5 #include <linux/kdebug.h>
6 #include <linux/signal.h>
7 #include <linux/sched.h>
8 #include <linux/sched/debug.h>
9 #include <linux/sched/task_stack.h>
10 #include <linux/uaccess.h>
11 #include <linux/hardirq.h>
12 #include <linux/kernel.h>
13 #include <linux/kexec.h>
14 #include <linux/sched/signal.h>
16 #include <linux/extable.h>
17 #include <linux/module.h> /* print_modules */
19 #include <asm/ftrace.h>
20 #include <asm/unwinder.h>
21 #include <asm/traps.h>
23 static DEFINE_SPINLOCK(die_lock);
25 void __noreturn die(const char *str, struct pt_regs *regs, long err)
27 static int die_counter;
31 spin_lock_irq(&die_lock);
35 printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
39 printk("Process: %s (pid: %d, stack limit = %p)\n", current->comm,
40 task_pid_nr(current), task_stack_page(current) + 1);
42 if (!user_mode(regs) || in_interrupt())
43 dump_mem("Stack: ", KERN_DEFAULT, regs->regs[15],
44 THREAD_SIZE + (unsigned long)task_stack_page(current));
46 notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV);
49 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
50 spin_unlock_irq(&die_lock);
53 if (kexec_should_crash(current))
57 panic("Fatal exception in interrupt");
60 panic("Fatal exception");
62 make_task_dead(SIGSEGV);
65 void die_if_kernel(const char *str, struct pt_regs *regs, long err)
72 * try and fix up kernelspace address errors
73 * - userspace errors just cause EFAULT to be returned, resulting in SEGV
74 * - kernel/userspace interfaces cause a jump to an appropriate handler
75 * - other kernel errors are bad
77 void die_if_no_fixup(const char *str, struct pt_regs *regs, long err)
79 if (!user_mode(regs)) {
80 const struct exception_table_entry *fixup;
81 fixup = search_exception_tables(regs->pc);
83 regs->pc = fixup->fixup;
91 #ifdef CONFIG_GENERIC_BUG
92 static void handle_BUG(struct pt_regs *regs)
94 const struct bug_entry *bug;
95 unsigned long bugaddr = regs->pc;
96 enum bug_trap_type tt;
98 if (!is_valid_bugaddr(bugaddr))
101 bug = find_bug(bugaddr);
103 /* Switch unwinders when unwind_stack() is called */
104 if (bug->flags & BUGFLAG_UNWINDER)
105 unwinder_faulted = 1;
107 tt = report_bug(bugaddr, regs);
108 if (tt == BUG_TRAP_TYPE_WARN) {
109 regs->pc += instruction_size(bugaddr);
114 die("Kernel BUG", regs, TRAPA_BUG_OPCODE & 0xff);
117 int is_valid_bugaddr(unsigned long addr)
121 if (addr < PAGE_OFFSET)
123 if (get_kernel_nofault(opcode, (insn_size_t *)addr))
125 if (opcode == TRAPA_BUG_OPCODE)
133 * Generic trap handler.
135 BUILD_TRAP_HANDLER(debug)
140 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
142 if (notify_die(DIE_TRAP, "debug trap", regs, 0, vec & 0xff,
143 SIGTRAP) == NOTIFY_STOP)
150 * Special handler for BUG() traps.
152 BUILD_TRAP_HANDLER(bug)
157 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
159 if (notify_die(DIE_TRAP, "bug trap", regs, 0, TRAPA_BUG_OPCODE & 0xff,
160 SIGTRAP) == NOTIFY_STOP)
163 #ifdef CONFIG_GENERIC_BUG
164 if (__kernel_text_address(instruction_pointer(regs))) {
165 insn_size_t insn = *(insn_size_t *)instruction_pointer(regs);
166 if (insn == TRAPA_BUG_OPCODE)
175 BUILD_TRAP_HANDLER(nmi)
179 arch_ftrace_nmi_enter();
182 this_cpu_inc(irq_stat.__nmi_count);
184 switch (notify_die(DIE_NMI, "NMI", regs, 0, vec & 0xff, SIGINT)) {
189 die("Fatal Non-Maskable Interrupt", regs, SIGINT);
191 printk(KERN_ALERT "Got NMI, but nobody cared. Ignoring...\n");
197 arch_ftrace_nmi_exit();