1 #include <linux/bug.h> 2 #include <linux/io.h> 3 #include <linux/types.h> 4 #include <linux/kdebug.h> 5 #include <linux/signal.h> 6 #include <linux/sched.h> 7 #include <linux/uaccess.h> 8 #include <linux/hardirq.h> 9 #include <linux/kernel.h> 10 #include <linux/kexec.h> 11 #include <linux/sched/signal.h> 12 13 #include <linux/extable.h> 14 #include <linux/module.h> /* print_modules */ 15 #include <asm/unwinder.h> 16 #include <asm/traps.h> 17 18 static DEFINE_SPINLOCK(die_lock); 19 20 void die(const char *str, struct pt_regs *regs, long err) 21 { 22 static int die_counter; 23 24 oops_enter(); 25 26 spin_lock_irq(&die_lock); 27 console_verbose(); 28 bust_spinlocks(1); 29 30 printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter); 31 print_modules(); 32 show_regs(regs); 33 34 printk("Process: %s (pid: %d, stack limit = %p)\n", current->comm, 35 task_pid_nr(current), task_stack_page(current) + 1); 36 37 if (!user_mode(regs) || in_interrupt()) 38 dump_mem("Stack: ", regs->regs[15], THREAD_SIZE + 39 (unsigned long)task_stack_page(current)); 40 41 notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV); 42 43 bust_spinlocks(0); 44 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE); 45 spin_unlock_irq(&die_lock); 46 oops_exit(); 47 48 if (kexec_should_crash(current)) 49 crash_kexec(regs); 50 51 if (in_interrupt()) 52 panic("Fatal exception in interrupt"); 53 54 if (panic_on_oops) 55 panic("Fatal exception"); 56 57 do_exit(SIGSEGV); 58 } 59 60 void die_if_kernel(const char *str, struct pt_regs *regs, long err) 61 { 62 if (!user_mode(regs)) 63 die(str, regs, err); 64 } 65 66 /* 67 * try and fix up kernelspace address errors 68 * - userspace errors just cause EFAULT to be returned, resulting in SEGV 69 * - kernel/userspace interfaces cause a jump to an appropriate handler 70 * - other kernel errors are bad 71 */ 72 void die_if_no_fixup(const char *str, struct pt_regs *regs, long err) 73 { 74 if (!user_mode(regs)) { 75 const struct exception_table_entry *fixup; 76 fixup = search_exception_tables(regs->pc); 77 if (fixup) { 78 regs->pc = fixup->fixup; 79 return; 80 } 81 82 die(str, regs, err); 83 } 84 } 85 86 #ifdef CONFIG_GENERIC_BUG 87 static void handle_BUG(struct pt_regs *regs) 88 { 89 const struct bug_entry *bug; 90 unsigned long bugaddr = regs->pc; 91 enum bug_trap_type tt; 92 93 if (!is_valid_bugaddr(bugaddr)) 94 goto invalid; 95 96 bug = find_bug(bugaddr); 97 98 /* Switch unwinders when unwind_stack() is called */ 99 if (bug->flags & BUGFLAG_UNWINDER) 100 unwinder_faulted = 1; 101 102 tt = report_bug(bugaddr, regs); 103 if (tt == BUG_TRAP_TYPE_WARN) { 104 regs->pc += instruction_size(bugaddr); 105 return; 106 } 107 108 invalid: 109 die("Kernel BUG", regs, TRAPA_BUG_OPCODE & 0xff); 110 } 111 112 int is_valid_bugaddr(unsigned long addr) 113 { 114 insn_size_t opcode; 115 116 if (addr < PAGE_OFFSET) 117 return 0; 118 if (probe_kernel_address((insn_size_t *)addr, opcode)) 119 return 0; 120 if (opcode == TRAPA_BUG_OPCODE) 121 return 1; 122 123 return 0; 124 } 125 #endif 126 127 /* 128 * Generic trap handler. 129 */ 130 BUILD_TRAP_HANDLER(debug) 131 { 132 TRAP_HANDLER_DECL; 133 134 /* Rewind */ 135 regs->pc -= instruction_size(__raw_readw(regs->pc - 4)); 136 137 if (notify_die(DIE_TRAP, "debug trap", regs, 0, vec & 0xff, 138 SIGTRAP) == NOTIFY_STOP) 139 return; 140 141 force_sig(SIGTRAP, current); 142 } 143 144 /* 145 * Special handler for BUG() traps. 146 */ 147 BUILD_TRAP_HANDLER(bug) 148 { 149 TRAP_HANDLER_DECL; 150 151 /* Rewind */ 152 regs->pc -= instruction_size(__raw_readw(regs->pc - 4)); 153 154 if (notify_die(DIE_TRAP, "bug trap", regs, 0, TRAPA_BUG_OPCODE & 0xff, 155 SIGTRAP) == NOTIFY_STOP) 156 return; 157 158 #ifdef CONFIG_GENERIC_BUG 159 if (__kernel_text_address(instruction_pointer(regs))) { 160 insn_size_t insn = *(insn_size_t *)instruction_pointer(regs); 161 if (insn == TRAPA_BUG_OPCODE) 162 handle_BUG(regs); 163 return; 164 } 165 #endif 166 167 force_sig(SIGTRAP, current); 168 } 169 170 BUILD_TRAP_HANDLER(nmi) 171 { 172 unsigned int cpu = smp_processor_id(); 173 TRAP_HANDLER_DECL; 174 175 nmi_enter(); 176 nmi_count(cpu)++; 177 178 switch (notify_die(DIE_NMI, "NMI", regs, 0, vec & 0xff, SIGINT)) { 179 case NOTIFY_OK: 180 case NOTIFY_STOP: 181 break; 182 case NOTIFY_BAD: 183 die("Fatal Non-Maskable Interrupt", regs, SIGINT); 184 default: 185 printk(KERN_ALERT "Got NMI, but nobody cared. Ignoring...\n"); 186 break; 187 } 188 189 nmi_exit(); 190 } 191