2 * arch/sparc/kernel/traps.c
4 * Copyright 1995 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright 2000 Jakub Jelinek (jakub@redhat.com)
9 * I hate traps on the sparc, grrr...
12 #include <linux/sched.h> /* for jiffies */
13 #include <linux/kernel.h>
14 #include <linux/kallsyms.h>
15 #include <linux/signal.h>
16 #include <linux/smp.h>
17 #include <linux/smp_lock.h>
18 #include <linux/kdebug.h>
20 #include <asm/delay.h>
21 #include <asm/system.h>
22 #include <asm/ptrace.h>
23 #include <asm/oplib.h>
25 #include <asm/pgtable.h>
26 #include <asm/unistd.h>
27 #include <asm/traps.h>
29 /* #define TRAP_DEBUG */
31 struct trap_trace_entry {
36 void syscall_trace_entry(struct pt_regs *regs)
38 printk("%s[%d]: ", current->comm, task_pid_nr(current));
39 printk("scall<%d> (could be %d)\n", (int) regs->u_regs[UREG_G1],
40 (int) regs->u_regs[UREG_I0]);
43 void syscall_trace_exit(struct pt_regs *regs)
47 void sun4m_nmi(struct pt_regs *regs)
49 unsigned long afsr, afar;
51 printk("Aieee: sun4m NMI received!\n");
52 /* XXX HyperSparc hack XXX */
53 __asm__ __volatile__("mov 0x500, %%g1\n\t"
54 "lda [%%g1] 0x4, %0\n\t"
56 "lda [%%g1] 0x4, %1\n\t" :
57 "=r" (afsr), "=r" (afar));
58 printk("afsr=%08lx afar=%08lx\n", afsr, afar);
59 printk("you lose buddy boy...\n");
64 void sun4d_nmi(struct pt_regs *regs)
66 printk("Aieee: sun4d NMI received!\n");
67 printk("you lose buddy boy...\n");
72 static void instruction_dump(unsigned long *pc)
76 if((((unsigned long) pc) & 3))
79 for(i = -3; i < 6; i++)
80 printk("%c%08lx%c",i?' ':'<',pc[i],i?' ':'>');
84 #define __SAVE __asm__ __volatile__("save %sp, -0x40, %sp\n\t")
85 #define __RESTORE __asm__ __volatile__("restore %g0, %g0, %g0\n\t")
87 void die_if_kernel(char *str, struct pt_regs *regs)
89 static int die_counter;
99 printk("%s(%d): %s [#%d]\n", current->comm, task_pid_nr(current), str, ++die_counter);
101 add_taint(TAINT_DIE);
103 __SAVE; __SAVE; __SAVE; __SAVE;
104 __SAVE; __SAVE; __SAVE; __SAVE;
105 __RESTORE; __RESTORE; __RESTORE; __RESTORE;
106 __RESTORE; __RESTORE; __RESTORE; __RESTORE;
109 struct reg_window *rw = (struct reg_window *)regs->u_regs[UREG_FP];
111 /* Stop the back trace when we hit userland or we
112 * find some badly aligned kernel stack. Set an upper
113 * bound in case our stack is trashed and we loop.
117 (((unsigned long) rw) >= PAGE_OFFSET) &&
118 !(((unsigned long) rw) & 0x7)) {
119 printk("Caller[%08lx]", rw->ins[7]);
120 print_symbol(": %s\n", rw->ins[7]);
121 rw = (struct reg_window *)rw->ins[6];
124 printk("Instruction DUMP:");
125 instruction_dump ((unsigned long *) regs->pc);
126 if(regs->psr & PSR_PS)
131 void do_hw_interrupt(struct pt_regs *regs, unsigned long type)
136 /* Sun OS's puke from bad traps, Linux survives! */
137 printk("Unimplemented Sparc TRAP, type = %02lx\n", type);
138 die_if_kernel("Whee... Hello Mr. Penguin", regs);
141 if(regs->psr & PSR_PS)
142 die_if_kernel("Kernel bad trap", regs);
144 info.si_signo = SIGILL;
146 info.si_code = ILL_ILLTRP;
147 info.si_addr = (void __user *)regs->pc;
148 info.si_trapno = type - 0x80;
149 force_sig_info(SIGILL, &info, current);
152 void do_illegal_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
155 extern int do_user_muldiv (struct pt_regs *, unsigned long);
159 die_if_kernel("Kernel illegal instruction", regs);
161 printk("Ill instr. at pc=%08lx instruction is %08lx\n",
162 regs->pc, *(unsigned long *)regs->pc);
164 if (!do_user_muldiv (regs, pc))
167 info.si_signo = SIGILL;
169 info.si_code = ILL_ILLOPC;
170 info.si_addr = (void __user *)pc;
172 send_sig_info(SIGILL, &info, current);
175 void do_priv_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
181 die_if_kernel("Penguin instruction from Penguin mode??!?!", regs);
182 info.si_signo = SIGILL;
184 info.si_code = ILL_PRVOPC;
185 info.si_addr = (void __user *)pc;
187 send_sig_info(SIGILL, &info, current);
190 /* XXX User may want to be allowed to do this. XXX */
192 void do_memaccess_unaligned(struct pt_regs *regs, unsigned long pc, unsigned long npc,
197 if(regs->psr & PSR_PS) {
198 printk("KERNEL MNA at pc %08lx npc %08lx called by %08lx\n", pc, npc,
199 regs->u_regs[UREG_RETPC]);
200 die_if_kernel("BOGUS", regs);
201 /* die_if_kernel("Kernel MNA access", regs); */
205 instruction_dump ((unsigned long *) regs->pc);
206 printk ("do_MNA!\n");
208 info.si_signo = SIGBUS;
210 info.si_code = BUS_ADRALN;
211 info.si_addr = /* FIXME: Should dig out mna address */ (void *)0;
213 send_sig_info(SIGBUS, &info, current);
216 extern void fpsave(unsigned long *fpregs, unsigned long *fsr,
217 void *fpqueue, unsigned long *fpqdepth);
218 extern void fpload(unsigned long *fpregs, unsigned long *fsr);
220 static unsigned long init_fsr = 0x0UL;
221 static unsigned long init_fregs[32] __attribute__ ((aligned (8))) =
222 { ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
223 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
224 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
225 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL };
227 void do_fpd_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
230 /* Sanity check... */
232 die_if_kernel("Kernel gets FloatingPenguinUnit disabled trap", regs);
234 put_psr(get_psr() | PSR_EF); /* Allow FPU ops. */
237 if(last_task_used_math == current)
239 if(last_task_used_math) {
240 /* Other processes fpu state, save away */
241 struct task_struct *fptask = last_task_used_math;
242 fpsave(&fptask->thread.float_regs[0], &fptask->thread.fsr,
243 &fptask->thread.fpqueue[0], &fptask->thread.fpqdepth);
245 last_task_used_math = current;
247 fpload(¤t->thread.float_regs[0], ¤t->thread.fsr);
249 /* Set initial sane state. */
250 fpload(&init_fregs[0], &init_fsr);
255 fpload(&init_fregs[0], &init_fsr);
258 fpload(¤t->thread.float_regs[0], ¤t->thread.fsr);
260 set_thread_flag(TIF_USEDFPU);
264 static unsigned long fake_regs[32] __attribute__ ((aligned (8)));
265 static unsigned long fake_fsr;
266 static unsigned long fake_queue[32] __attribute__ ((aligned (8)));
267 static unsigned long fake_depth;
269 extern int do_mathemu(struct pt_regs *, struct task_struct *);
271 void do_fpe_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
279 struct task_struct *fpt = last_task_used_math;
281 struct task_struct *fpt = current;
283 put_psr(get_psr() | PSR_EF);
284 /* If nobody owns the fpu right now, just clear the
285 * error into our fake static buffer and hope it don't
286 * happen again. Thank you crashme...
291 if (!test_tsk_thread_flag(fpt, TIF_USEDFPU)) {
293 fpsave(&fake_regs[0], &fake_fsr, &fake_queue[0], &fake_depth);
294 regs->psr &= ~PSR_EF;
297 fpsave(&fpt->thread.float_regs[0], &fpt->thread.fsr,
298 &fpt->thread.fpqueue[0], &fpt->thread.fpqdepth);
300 printk("Hmm, FP exception, fsr was %016lx\n", fpt->thread.fsr);
303 switch ((fpt->thread.fsr & 0x1c000)) {
304 /* switch on the contents of the ftt [floating point trap type] field */
307 printk("IEEE_754_exception\n");
310 case (2 << 14): /* unfinished_FPop (underflow & co) */
311 case (3 << 14): /* unimplemented_FPop (quad stuff, maybe sqrt) */
312 ret = do_mathemu(regs, fpt);
316 printk("sequence_error (OS bug...)\n");
319 printk("hardware_error (uhoh!)\n");
322 printk("invalid_fp_register (user error)\n");
324 #endif /* DEBUG_FPU */
326 /* If we successfully emulated the FPop, we pretend the trap never happened :-> */
328 fpload(¤t->thread.float_regs[0], ¤t->thread.fsr);
331 /* nope, better SIGFPE the offending process... */
334 clear_tsk_thread_flag(fpt, TIF_USEDFPU);
337 /* The first fsr store/load we tried trapped,
338 * the second one will not (we hope).
340 printk("WARNING: FPU exception from kernel mode. at pc=%08lx\n",
342 regs->pc = regs->npc;
346 die_if_kernel("Too many Penguin-FPU traps from kernel mode",
351 fsr = fpt->thread.fsr;
352 info.si_signo = SIGFPE;
354 info.si_addr = (void __user *)pc;
356 info.si_code = __SI_FAULT;
357 if ((fsr & 0x1c000) == (1 << 14)) {
359 info.si_code = FPE_FLTINV;
361 info.si_code = FPE_FLTOVF;
363 info.si_code = FPE_FLTUND;
365 info.si_code = FPE_FLTDIV;
367 info.si_code = FPE_FLTRES;
369 send_sig_info(SIGFPE, &info, fpt);
371 last_task_used_math = NULL;
373 regs->psr &= ~PSR_EF;
378 void handle_tag_overflow(struct pt_regs *regs, unsigned long pc, unsigned long npc,
384 die_if_kernel("Penguin overflow trap from kernel mode", regs);
385 info.si_signo = SIGEMT;
387 info.si_code = EMT_TAGOVF;
388 info.si_addr = (void __user *)pc;
390 send_sig_info(SIGEMT, &info, current);
393 void handle_watchpoint(struct pt_regs *regs, unsigned long pc, unsigned long npc,
397 printk("Watchpoint detected at PC %08lx NPC %08lx PSR %08lx\n",
401 panic("Tell me what a watchpoint trap is, and I'll then deal "
402 "with such a beast...");
405 void handle_reg_access(struct pt_regs *regs, unsigned long pc, unsigned long npc,
411 printk("Register Access Exception at PC %08lx NPC %08lx PSR %08lx\n",
414 info.si_signo = SIGBUS;
416 info.si_code = BUS_OBJERR;
417 info.si_addr = (void __user *)pc;
419 force_sig_info(SIGBUS, &info, current);
422 void handle_cp_disabled(struct pt_regs *regs, unsigned long pc, unsigned long npc,
427 info.si_signo = SIGILL;
429 info.si_code = ILL_COPROC;
430 info.si_addr = (void __user *)pc;
432 send_sig_info(SIGILL, &info, current);
435 void handle_cp_exception(struct pt_regs *regs, unsigned long pc, unsigned long npc,
441 printk("Co-Processor Exception at PC %08lx NPC %08lx PSR %08lx\n",
444 info.si_signo = SIGILL;
446 info.si_code = ILL_COPROC;
447 info.si_addr = (void __user *)pc;
449 send_sig_info(SIGILL, &info, current);
452 void handle_hw_divzero(struct pt_regs *regs, unsigned long pc, unsigned long npc,
457 info.si_signo = SIGFPE;
459 info.si_code = FPE_INTDIV;
460 info.si_addr = (void __user *)pc;
462 send_sig_info(SIGFPE, &info, current);
465 #ifdef CONFIG_DEBUG_BUGVERBOSE
466 void do_BUG(const char *file, int line)
468 // bust_spinlocks(1); XXX Not in our original BUG()
469 printk("kernel BUG at %s:%d!\n", file, line);
473 /* Since we have our mappings set up, on multiprocessors we can spin them
474 * up here so that timer interrupts work during initialization.
477 extern void sparc_cpu_startup(void);
481 extern void thread_info_offsets_are_bolixed_pete(void);
483 /* Force linker to barf if mismatched */
484 if (TI_UWINMASK != offsetof(struct thread_info, uwinmask) ||
485 TI_TASK != offsetof(struct thread_info, task) ||
486 TI_EXECDOMAIN != offsetof(struct thread_info, exec_domain) ||
487 TI_FLAGS != offsetof(struct thread_info, flags) ||
488 TI_CPU != offsetof(struct thread_info, cpu) ||
489 TI_PREEMPT != offsetof(struct thread_info, preempt_count) ||
490 TI_SOFTIRQ != offsetof(struct thread_info, softirq_count) ||
491 TI_HARDIRQ != offsetof(struct thread_info, hardirq_count) ||
492 TI_KSP != offsetof(struct thread_info, ksp) ||
493 TI_KPC != offsetof(struct thread_info, kpc) ||
494 TI_KPSR != offsetof(struct thread_info, kpsr) ||
495 TI_KWIM != offsetof(struct thread_info, kwim) ||
496 TI_REG_WINDOW != offsetof(struct thread_info, reg_window) ||
497 TI_RWIN_SPTRS != offsetof(struct thread_info, rwbuf_stkptrs) ||
498 TI_W_SAVED != offsetof(struct thread_info, w_saved))
499 thread_info_offsets_are_bolixed_pete();
501 /* Attach to the address space of init_task. */
502 atomic_inc(&init_mm.mm_count);
503 current->active_mm = &init_mm;
505 /* NOTE: Other cpus have this done as they are started