1 /* 2 * Hardware exception handling 3 * 4 * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch> 5 * Copyright (C) 2004 Microtronix Datacom Ltd. 6 * Copyright (C) 2001 Vic Phillips 7 * 8 * This file is subject to the terms and conditions of the GNU General 9 * Public License. See the file COPYING in the main directory of this 10 * archive for more details. 11 */ 12 13 #include <linux/sched.h> 14 #include <linux/sched/debug.h> 15 #include <linux/kernel.h> 16 #include <linux/signal.h> 17 #include <linux/export.h> 18 #include <linux/mm.h> 19 #include <linux/ptrace.h> 20 21 #include <asm/traps.h> 22 #include <asm/sections.h> 23 #include <linux/uaccess.h> 24 25 static DEFINE_SPINLOCK(die_lock); 26 27 static void _send_sig(int signo, int code, unsigned long addr) 28 { 29 force_sig_fault(signo, code, (void __user *) addr); 30 } 31 32 void die(const char *str, struct pt_regs *regs, long err) 33 { 34 console_verbose(); 35 spin_lock_irq(&die_lock); 36 pr_warn("Oops: %s, sig: %ld\n", str, err); 37 show_regs(regs); 38 spin_unlock_irq(&die_lock); 39 /* 40 * do_exit() should take care of panic'ing from an interrupt 41 * context so we don't handle it here 42 */ 43 do_exit(err); 44 } 45 46 void _exception(int signo, struct pt_regs *regs, int code, unsigned long addr) 47 { 48 if (!user_mode(regs)) 49 die("Exception in kernel mode", regs, signo); 50 51 _send_sig(signo, code, addr); 52 } 53 54 /* 55 * The show_stack(), show_stack_loglvl() are external API 56 * which we do not use ourselves. 57 */ 58 59 int kstack_depth_to_print = 48; 60 61 void show_stack_loglvl(struct task_struct *task, unsigned long *stack, 62 const char *loglvl) 63 { 64 unsigned long *endstack, addr; 65 int i; 66 67 if (!stack) { 68 if (task) 69 stack = (unsigned long *)task->thread.ksp; 70 else 71 stack = (unsigned long *)&stack; 72 } 73 74 addr = (unsigned long) stack; 75 endstack = (unsigned long *) PAGE_ALIGN(addr); 76 77 printk("%sStack from %08lx:", loglvl, (unsigned long)stack); 78 for (i = 0; i < kstack_depth_to_print; i++) { 79 if (stack + 1 > endstack) 80 break; 81 if (i % 8 == 0) 82 printk("%s\n ", loglvl); 83 printk("%s %08lx", loglvl, *stack++); 84 } 85 86 printk("%s\nCall Trace:", loglvl); 87 i = 0; 88 while (stack + 1 <= endstack) { 89 addr = *stack++; 90 /* 91 * If the address is either in the text segment of the 92 * kernel, or in the region which contains vmalloc'ed 93 * memory, it *may* be the address of a calling 94 * routine; if so, print it so that someone tracing 95 * down the cause of the crash will be able to figure 96 * out the call path that was taken. 97 */ 98 if (((addr >= (unsigned long) _stext) && 99 (addr <= (unsigned long) _etext))) { 100 if (i % 4 == 0) 101 pr_emerg("\n "); 102 printk("%s [<%08lx>]", loglvl, addr); 103 i++; 104 } 105 } 106 printk("%s\n", loglvl); 107 } 108 109 void show_stack(struct task_struct *task, unsigned long *stack) 110 { 111 show_stack_loglvl(task, stack, KERN_EMERG); 112 } 113 114 void __init trap_init(void) 115 { 116 /* Nothing to do here */ 117 } 118 119 /* Breakpoint handler */ 120 asmlinkage void breakpoint_c(struct pt_regs *fp) 121 { 122 /* 123 * The breakpoint entry code has moved the PC on by 4 bytes, so we must 124 * move it back. This could be done on the host but we do it here 125 * because monitor.S of JTAG gdbserver does it too. 126 */ 127 fp->ea -= 4; 128 _exception(SIGTRAP, fp, TRAP_BRKPT, fp->ea); 129 } 130 131 #ifndef CONFIG_NIOS2_ALIGNMENT_TRAP 132 /* Alignment exception handler */ 133 asmlinkage void handle_unaligned_c(struct pt_regs *fp, int cause) 134 { 135 unsigned long addr = RDCTL(CTL_BADADDR); 136 137 cause >>= 2; 138 fp->ea -= 4; 139 140 if (fixup_exception(fp)) 141 return; 142 143 if (!user_mode(fp)) { 144 pr_alert("Unaligned access from kernel mode, this might be a hardware\n"); 145 pr_alert("problem, dump registers and restart the instruction\n"); 146 pr_alert(" BADADDR 0x%08lx\n", addr); 147 pr_alert(" cause %d\n", cause); 148 pr_alert(" op-code 0x%08lx\n", *(unsigned long *)(fp->ea)); 149 show_regs(fp); 150 return; 151 } 152 153 _exception(SIGBUS, fp, BUS_ADRALN, addr); 154 } 155 #endif /* CONFIG_NIOS2_ALIGNMENT_TRAP */ 156 157 /* Illegal instruction handler */ 158 asmlinkage void handle_illegal_c(struct pt_regs *fp) 159 { 160 fp->ea -= 4; 161 _exception(SIGILL, fp, ILL_ILLOPC, fp->ea); 162 } 163 164 /* Supervisor instruction handler */ 165 asmlinkage void handle_supervisor_instr(struct pt_regs *fp) 166 { 167 fp->ea -= 4; 168 _exception(SIGILL, fp, ILL_PRVOPC, fp->ea); 169 } 170 171 /* Division error handler */ 172 asmlinkage void handle_diverror_c(struct pt_regs *fp) 173 { 174 fp->ea -= 4; 175 _exception(SIGFPE, fp, FPE_INTDIV, fp->ea); 176 } 177 178 /* Unhandled exception handler */ 179 asmlinkage void unhandled_exception(struct pt_regs *regs, int cause) 180 { 181 unsigned long addr = RDCTL(CTL_BADADDR); 182 183 cause /= 4; 184 185 pr_emerg("Unhandled exception #%d in %s mode (badaddr=0x%08lx)\n", 186 cause, user_mode(regs) ? "user" : "kernel", addr); 187 188 regs->ea -= 4; 189 show_regs(regs); 190 191 pr_emerg("opcode: 0x%08lx\n", *(unsigned long *)(regs->ea)); 192 } 193 194 asmlinkage void handle_trap_1_c(struct pt_regs *fp) 195 { 196 _send_sig(SIGUSR1, 0, fp->ea); 197 } 198 199 asmlinkage void handle_trap_2_c(struct pt_regs *fp) 200 { 201 _send_sig(SIGUSR2, 0, fp->ea); 202 } 203 204 asmlinkage void handle_trap_3_c(struct pt_regs *fp) 205 { 206 _send_sig(SIGILL, ILL_ILLTRP, fp->ea); 207 } 208