1/* 2 * arch/sparc/kernel/traps.c 3 * 4 * Copyright 1995, 2008 David S. Miller (davem@davemloft.net) 5 * Copyright 2000 Jakub Jelinek (jakub@redhat.com) 6 */ 7 8/* 9 * I hate traps on the sparc, grrr... 10 */ 11 12#include <linux/sched.h> /* for jiffies */ 13#include <linux/kernel.h> 14#include <linux/signal.h> 15#include <linux/smp.h> 16#include <linux/kdebug.h> 17 18#include <asm/delay.h> 19#include <asm/system.h> 20#include <asm/ptrace.h> 21#include <asm/oplib.h> 22#include <asm/page.h> 23#include <asm/pgtable.h> 24#include <asm/unistd.h> 25#include <asm/traps.h> 26 27#include "entry.h" 28#include "kernel.h" 29 30/* #define TRAP_DEBUG */ 31 32static void instruction_dump(unsigned long *pc) 33{ 34 int i; 35 36 if((((unsigned long) pc) & 3)) 37 return; 38 39 for(i = -3; i < 6; i++) 40 printk("%c%08lx%c",i?' ':'<',pc[i],i?' ':'>'); 41 printk("\n"); 42} 43 44#define __SAVE __asm__ __volatile__("save %sp, -0x40, %sp\n\t") 45#define __RESTORE __asm__ __volatile__("restore %g0, %g0, %g0\n\t") 46 47void die_if_kernel(char *str, struct pt_regs *regs) 48{ 49 static int die_counter; 50 int count = 0; 51 52 /* Amuse the user. */ 53 printk( 54" \\|/ ____ \\|/\n" 55" \"@'/ ,. \\`@\"\n" 56" /_| \\__/ |_\\\n" 57" \\__U_/\n"); 58 59 printk("%s(%d): %s [#%d]\n", current->comm, task_pid_nr(current), str, ++die_counter); 60 show_regs(regs); 61 add_taint(TAINT_DIE); 62 63 __SAVE; __SAVE; __SAVE; __SAVE; 64 __SAVE; __SAVE; __SAVE; __SAVE; 65 __RESTORE; __RESTORE; __RESTORE; __RESTORE; 66 __RESTORE; __RESTORE; __RESTORE; __RESTORE; 67 68 { 69 struct reg_window32 *rw = (struct reg_window32 *)regs->u_regs[UREG_FP]; 70 71 /* Stop the back trace when we hit userland or we 72 * find some badly aligned kernel stack. Set an upper 73 * bound in case our stack is trashed and we loop. 74 */ 75 while(rw && 76 count++ < 30 && 77 (((unsigned long) rw) >= PAGE_OFFSET) && 78 !(((unsigned long) rw) & 0x7)) { 79 printk("Caller[%08lx]: %pS\n", rw->ins[7], 80 (void *) rw->ins[7]); 81 rw = (struct reg_window32 *)rw->ins[6]; 82 } 83 } 84 printk("Instruction DUMP:"); 85 instruction_dump ((unsigned long *) regs->pc); 86 if(regs->psr & PSR_PS) 87 do_exit(SIGKILL); 88 do_exit(SIGSEGV); 89} 90 91void do_hw_interrupt(struct pt_regs *regs, unsigned long type) 92{ 93 siginfo_t info; 94 95 if(type < 0x80) { 96 /* Sun OS's puke from bad traps, Linux survives! */ 97 printk("Unimplemented Sparc TRAP, type = %02lx\n", type); 98 die_if_kernel("Whee... Hello Mr. Penguin", regs); 99 } 100 101 if(regs->psr & PSR_PS) 102 die_if_kernel("Kernel bad trap", regs); 103 104 info.si_signo = SIGILL; 105 info.si_errno = 0; 106 info.si_code = ILL_ILLTRP; 107 info.si_addr = (void __user *)regs->pc; 108 info.si_trapno = type - 0x80; 109 force_sig_info(SIGILL, &info, current); 110} 111 112void do_illegal_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc, 113 unsigned long psr) 114{ 115 siginfo_t info; 116 117 if(psr & PSR_PS) 118 die_if_kernel("Kernel illegal instruction", regs); 119#ifdef TRAP_DEBUG 120 printk("Ill instr. at pc=%08lx instruction is %08lx\n", 121 regs->pc, *(unsigned long *)regs->pc); 122#endif 123 if (!do_user_muldiv (regs, pc)) 124 return; 125 126 info.si_signo = SIGILL; 127 info.si_errno = 0; 128 info.si_code = ILL_ILLOPC; 129 info.si_addr = (void __user *)pc; 130 info.si_trapno = 0; 131 send_sig_info(SIGILL, &info, current); 132} 133 134void do_priv_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc, 135 unsigned long psr) 136{ 137 siginfo_t info; 138 139 if(psr & PSR_PS) 140 die_if_kernel("Penguin instruction from Penguin mode??!?!", regs); 141 info.si_signo = SIGILL; 142 info.si_errno = 0; 143 info.si_code = ILL_PRVOPC; 144 info.si_addr = (void __user *)pc; 145 info.si_trapno = 0; 146 send_sig_info(SIGILL, &info, current); 147} 148 149 150void do_memaccess_unaligned(struct pt_regs *regs, unsigned long pc, unsigned long npc, 151 unsigned long psr) 152{ 153 siginfo_t info; 154 155 if(regs->psr & PSR_PS) { 156 printk("KERNEL MNA at pc %08lx npc %08lx called by %08lx\n", pc, npc, 157 regs->u_regs[UREG_RETPC]); 158 die_if_kernel("BOGUS", regs); 159 /* die_if_kernel("Kernel MNA access", regs); */ 160 } 161 info.si_signo = SIGBUS; 162 info.si_errno = 0; 163 info.si_code = BUS_ADRALN; 164 info.si_addr = (void *)0; 165 info.si_trapno = 0; 166 send_sig_info(SIGBUS, &info, current); 167} 168 169static unsigned long init_fsr = 0x0UL; 170static unsigned long init_fregs[32] __attribute__ ((aligned (8))) = 171 { ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, 172 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, 173 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, 174 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL }; 175 176void do_fpd_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc, 177 unsigned long psr) 178{ 179 /* Sanity check... */ 180 if(psr & PSR_PS) 181 die_if_kernel("Kernel gets FloatingPenguinUnit disabled trap", regs); 182 183 put_psr(get_psr() | PSR_EF); /* Allow FPU ops. */ 184 regs->psr |= PSR_EF; 185#ifndef CONFIG_SMP 186 if(last_task_used_math == current) 187 return; 188 if(last_task_used_math) { 189 /* Other processes fpu state, save away */ 190 struct task_struct *fptask = last_task_used_math; 191 fpsave(&fptask->thread.float_regs[0], &fptask->thread.fsr, 192 &fptask->thread.fpqueue[0], &fptask->thread.fpqdepth); 193 } 194 last_task_used_math = current; 195 if(used_math()) { 196 fpload(¤t->thread.float_regs[0], ¤t->thread.fsr); 197 } else { 198 /* Set initial sane state. */ 199 fpload(&init_fregs[0], &init_fsr); 200 set_used_math(); 201 } 202#else 203 if(!used_math()) { 204 fpload(&init_fregs[0], &init_fsr); 205 set_used_math(); 206 } else { 207 fpload(¤t->thread.float_regs[0], ¤t->thread.fsr); 208 } 209 set_thread_flag(TIF_USEDFPU); 210#endif 211} 212 213static unsigned long fake_regs[32] __attribute__ ((aligned (8))); 214static unsigned long fake_fsr; 215static unsigned long fake_queue[32] __attribute__ ((aligned (8))); 216static unsigned long fake_depth; 217 218extern int do_mathemu(struct pt_regs *, struct task_struct *); 219 220void do_fpe_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc, 221 unsigned long psr) 222{ 223 static int calls; 224 siginfo_t info; 225 unsigned long fsr; 226 int ret = 0; 227#ifndef CONFIG_SMP 228 struct task_struct *fpt = last_task_used_math; 229#else 230 struct task_struct *fpt = current; 231#endif 232 put_psr(get_psr() | PSR_EF); 233 /* If nobody owns the fpu right now, just clear the 234 * error into our fake static buffer and hope it don't 235 * happen again. Thank you crashme... 236 */ 237#ifndef CONFIG_SMP 238 if(!fpt) { 239#else 240 if (!test_tsk_thread_flag(fpt, TIF_USEDFPU)) { 241#endif 242 fpsave(&fake_regs[0], &fake_fsr, &fake_queue[0], &fake_depth); 243 regs->psr &= ~PSR_EF; 244 return; 245 } 246 fpsave(&fpt->thread.float_regs[0], &fpt->thread.fsr, 247 &fpt->thread.fpqueue[0], &fpt->thread.fpqdepth); 248#ifdef DEBUG_FPU 249 printk("Hmm, FP exception, fsr was %016lx\n", fpt->thread.fsr); 250#endif 251 252 switch ((fpt->thread.fsr & 0x1c000)) { 253 /* switch on the contents of the ftt [floating point trap type] field */ 254#ifdef DEBUG_FPU 255 case (1 << 14): 256 printk("IEEE_754_exception\n"); 257 break; 258#endif 259 case (2 << 14): /* unfinished_FPop (underflow & co) */ 260 case (3 << 14): /* unimplemented_FPop (quad stuff, maybe sqrt) */ 261 ret = do_mathemu(regs, fpt); 262 break; 263#ifdef DEBUG_FPU 264 case (4 << 14): 265 printk("sequence_error (OS bug...)\n"); 266 break; 267 case (5 << 14): 268 printk("hardware_error (uhoh!)\n"); 269 break; 270 case (6 << 14): 271 printk("invalid_fp_register (user error)\n"); 272 break; 273#endif /* DEBUG_FPU */ 274 } 275 /* If we successfully emulated the FPop, we pretend the trap never happened :-> */ 276 if (ret) { 277 fpload(¤t->thread.float_regs[0], ¤t->thread.fsr); 278 return; 279 } 280 /* nope, better SIGFPE the offending process... */ 281 282#ifdef CONFIG_SMP 283 clear_tsk_thread_flag(fpt, TIF_USEDFPU); 284#endif 285 if(psr & PSR_PS) { 286 /* The first fsr store/load we tried trapped, 287 * the second one will not (we hope). 288 */ 289 printk("WARNING: FPU exception from kernel mode. at pc=%08lx\n", 290 regs->pc); 291 regs->pc = regs->npc; 292 regs->npc += 4; 293 calls++; 294 if(calls > 2) 295 die_if_kernel("Too many Penguin-FPU traps from kernel mode", 296 regs); 297 return; 298 } 299 300 fsr = fpt->thread.fsr; 301 info.si_signo = SIGFPE; 302 info.si_errno = 0; 303 info.si_addr = (void __user *)pc; 304 info.si_trapno = 0; 305 info.si_code = __SI_FAULT; 306 if ((fsr & 0x1c000) == (1 << 14)) { 307 if (fsr & 0x10) 308 info.si_code = FPE_FLTINV; 309 else if (fsr & 0x08) 310 info.si_code = FPE_FLTOVF; 311 else if (fsr & 0x04) 312 info.si_code = FPE_FLTUND; 313 else if (fsr & 0x02) 314 info.si_code = FPE_FLTDIV; 315 else if (fsr & 0x01) 316 info.si_code = FPE_FLTRES; 317 } 318 send_sig_info(SIGFPE, &info, fpt); 319#ifndef CONFIG_SMP 320 last_task_used_math = NULL; 321#endif 322 regs->psr &= ~PSR_EF; 323 if(calls > 0) 324 calls=0; 325} 326 327void handle_tag_overflow(struct pt_regs *regs, unsigned long pc, unsigned long npc, 328 unsigned long psr) 329{ 330 siginfo_t info; 331 332 if(psr & PSR_PS) 333 die_if_kernel("Penguin overflow trap from kernel mode", regs); 334 info.si_signo = SIGEMT; 335 info.si_errno = 0; 336 info.si_code = EMT_TAGOVF; 337 info.si_addr = (void __user *)pc; 338 info.si_trapno = 0; 339 send_sig_info(SIGEMT, &info, current); 340} 341 342void handle_watchpoint(struct pt_regs *regs, unsigned long pc, unsigned long npc, 343 unsigned long psr) 344{ 345#ifdef TRAP_DEBUG 346 printk("Watchpoint detected at PC %08lx NPC %08lx PSR %08lx\n", 347 pc, npc, psr); 348#endif 349 if(psr & PSR_PS) 350 panic("Tell me what a watchpoint trap is, and I'll then deal " 351 "with such a beast..."); 352} 353 354void handle_reg_access(struct pt_regs *regs, unsigned long pc, unsigned long npc, 355 unsigned long psr) 356{ 357 siginfo_t info; 358 359#ifdef TRAP_DEBUG 360 printk("Register Access Exception at PC %08lx NPC %08lx PSR %08lx\n", 361 pc, npc, psr); 362#endif 363 info.si_signo = SIGBUS; 364 info.si_errno = 0; 365 info.si_code = BUS_OBJERR; 366 info.si_addr = (void __user *)pc; 367 info.si_trapno = 0; 368 force_sig_info(SIGBUS, &info, current); 369} 370 371void handle_cp_disabled(struct pt_regs *regs, unsigned long pc, unsigned long npc, 372 unsigned long psr) 373{ 374 siginfo_t info; 375 376 info.si_signo = SIGILL; 377 info.si_errno = 0; 378 info.si_code = ILL_COPROC; 379 info.si_addr = (void __user *)pc; 380 info.si_trapno = 0; 381 send_sig_info(SIGILL, &info, current); 382} 383 384void handle_cp_exception(struct pt_regs *regs, unsigned long pc, unsigned long npc, 385 unsigned long psr) 386{ 387 siginfo_t info; 388 389#ifdef TRAP_DEBUG 390 printk("Co-Processor Exception at PC %08lx NPC %08lx PSR %08lx\n", 391 pc, npc, psr); 392#endif 393 info.si_signo = SIGILL; 394 info.si_errno = 0; 395 info.si_code = ILL_COPROC; 396 info.si_addr = (void __user *)pc; 397 info.si_trapno = 0; 398 send_sig_info(SIGILL, &info, current); 399} 400 401void handle_hw_divzero(struct pt_regs *regs, unsigned long pc, unsigned long npc, 402 unsigned long psr) 403{ 404 siginfo_t info; 405 406 info.si_signo = SIGFPE; 407 info.si_errno = 0; 408 info.si_code = FPE_INTDIV; 409 info.si_addr = (void __user *)pc; 410 info.si_trapno = 0; 411 send_sig_info(SIGFPE, &info, current); 412} 413 414#ifdef CONFIG_DEBUG_BUGVERBOSE 415void do_BUG(const char *file, int line) 416{ 417 printk("kernel BUG at %s:%d!\n", file, line); 418} 419EXPORT_SYMBOL(do_BUG); 420#endif 421 422/* Since we have our mappings set up, on multiprocessors we can spin them 423 * up here so that timer interrupts work during initialization. 424 */ 425 426void trap_init(void) 427{ 428 extern void thread_info_offsets_are_bolixed_pete(void); 429 430 /* Force linker to barf if mismatched */ 431 if (TI_UWINMASK != offsetof(struct thread_info, uwinmask) || 432 TI_TASK != offsetof(struct thread_info, task) || 433 TI_EXECDOMAIN != offsetof(struct thread_info, exec_domain) || 434 TI_FLAGS != offsetof(struct thread_info, flags) || 435 TI_CPU != offsetof(struct thread_info, cpu) || 436 TI_PREEMPT != offsetof(struct thread_info, preempt_count) || 437 TI_SOFTIRQ != offsetof(struct thread_info, softirq_count) || 438 TI_HARDIRQ != offsetof(struct thread_info, hardirq_count) || 439 TI_KSP != offsetof(struct thread_info, ksp) || 440 TI_KPC != offsetof(struct thread_info, kpc) || 441 TI_KPSR != offsetof(struct thread_info, kpsr) || 442 TI_KWIM != offsetof(struct thread_info, kwim) || 443 TI_REG_WINDOW != offsetof(struct thread_info, reg_window) || 444 TI_RWIN_SPTRS != offsetof(struct thread_info, rwbuf_stkptrs) || 445 TI_W_SAVED != offsetof(struct thread_info, w_saved)) 446 thread_info_offsets_are_bolixed_pete(); 447 448 /* Attach to the address space of init_task. */ 449 atomic_inc(&init_mm.mm_count); 450 current->active_mm = &init_mm; 451 452 /* NOTE: Other cpus have this done as they are started 453 * up on SMP. 454 */ 455} 456