1 /* $Id: traps.c,v 1.64 2000/09/03 15:00:49 anton Exp $
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/config.h>
13 #include <linux/sched.h> /* for jiffies */
14 #include <linux/kernel.h>
15 #include <linux/kallsyms.h>
16 #include <linux/signal.h>
17 #include <linux/smp.h>
18 #include <linux/smp_lock.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/kdebug.h>
27 #include <asm/unistd.h>
28 #include <asm/traps.h>
30 /* #define TRAP_DEBUG */
32 struct trap_trace_entry {
38 struct trap_trace_entry trapbuf[1024];
40 void syscall_trace_entry(struct pt_regs *regs)
42 printk("%s[%d]: ", current->comm, current->pid);
43 printk("scall<%d> (could be %d)\n", (int) regs->u_regs[UREG_G1],
44 (int) regs->u_regs[UREG_I0]);
47 void syscall_trace_exit(struct pt_regs *regs)
51 void sun4m_nmi(struct pt_regs *regs)
53 unsigned long afsr, afar;
55 printk("Aieee: sun4m NMI received!\n");
56 /* XXX HyperSparc hack XXX */
57 __asm__ __volatile__("mov 0x500, %%g1\n\t"
58 "lda [%%g1] 0x4, %0\n\t"
60 "lda [%%g1] 0x4, %1\n\t" :
61 "=r" (afsr), "=r" (afar));
62 printk("afsr=%08lx afar=%08lx\n", afsr, afar);
63 printk("you lose buddy boy...\n");
68 void sun4d_nmi(struct pt_regs *regs)
70 printk("Aieee: sun4d NMI received!\n");
71 printk("you lose buddy boy...\n");
76 void instruction_dump (unsigned long *pc)
80 if((((unsigned long) pc) & 3))
83 for(i = -3; i < 6; i++)
84 printk("%c%08lx%c",i?' ':'<',pc[i],i?' ':'>');
88 #define __SAVE __asm__ __volatile__("save %sp, -0x40, %sp\n\t")
89 #define __RESTORE __asm__ __volatile__("restore %g0, %g0, %g0\n\t")
91 void die_if_kernel(char *str, struct pt_regs *regs)
93 static int die_counter;
103 printk("%s(%d): %s [#%d]\n", current->comm, current->pid, str, ++die_counter);
106 __SAVE; __SAVE; __SAVE; __SAVE;
107 __SAVE; __SAVE; __SAVE; __SAVE;
108 __RESTORE; __RESTORE; __RESTORE; __RESTORE;
109 __RESTORE; __RESTORE; __RESTORE; __RESTORE;
112 struct reg_window *rw = (struct reg_window *)regs->u_regs[UREG_FP];
114 /* Stop the back trace when we hit userland or we
115 * find some badly aligned kernel stack. Set an upper
116 * bound in case our stack is trashed and we loop.
120 (((unsigned long) rw) >= PAGE_OFFSET) &&
121 !(((unsigned long) rw) & 0x7)) {
122 printk("Caller[%08lx]", rw->ins[7]);
123 print_symbol(": %s\n", rw->ins[7]);
124 rw = (struct reg_window *)rw->ins[6];
127 printk("Instruction DUMP:");
128 instruction_dump ((unsigned long *) regs->pc);
129 if(regs->psr & PSR_PS)
134 void do_hw_interrupt(struct pt_regs *regs, unsigned long type)
139 /* Sun OS's puke from bad traps, Linux survives! */
140 printk("Unimplemented Sparc TRAP, type = %02lx\n", type);
141 die_if_kernel("Whee... Hello Mr. Penguin", regs);
144 if(regs->psr & PSR_PS)
145 die_if_kernel("Kernel bad trap", regs);
147 info.si_signo = SIGILL;
149 info.si_code = ILL_ILLTRP;
150 info.si_addr = (void __user *)regs->pc;
151 info.si_trapno = type - 0x80;
152 force_sig_info(SIGILL, &info, current);
155 void do_illegal_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
158 extern int do_user_muldiv (struct pt_regs *, unsigned long);
162 die_if_kernel("Kernel illegal instruction", regs);
164 printk("Ill instr. at pc=%08lx instruction is %08lx\n",
165 regs->pc, *(unsigned long *)regs->pc);
167 if (!do_user_muldiv (regs, pc))
170 info.si_signo = SIGILL;
172 info.si_code = ILL_ILLOPC;
173 info.si_addr = (void __user *)pc;
175 send_sig_info(SIGILL, &info, current);
178 void do_priv_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
184 die_if_kernel("Penguin instruction from Penguin mode??!?!", regs);
185 info.si_signo = SIGILL;
187 info.si_code = ILL_PRVOPC;
188 info.si_addr = (void __user *)pc;
190 send_sig_info(SIGILL, &info, current);
193 /* XXX User may want to be allowed to do this. XXX */
195 void do_memaccess_unaligned(struct pt_regs *regs, unsigned long pc, unsigned long npc,
200 if(regs->psr & PSR_PS) {
201 printk("KERNEL MNA at pc %08lx npc %08lx called by %08lx\n", pc, npc,
202 regs->u_regs[UREG_RETPC]);
203 die_if_kernel("BOGUS", regs);
204 /* die_if_kernel("Kernel MNA access", regs); */
208 instruction_dump ((unsigned long *) regs->pc);
209 printk ("do_MNA!\n");
211 info.si_signo = SIGBUS;
213 info.si_code = BUS_ADRALN;
214 info.si_addr = /* FIXME: Should dig out mna address */ (void *)0;
216 send_sig_info(SIGBUS, &info, current);
219 extern void fpsave(unsigned long *fpregs, unsigned long *fsr,
220 void *fpqueue, unsigned long *fpqdepth);
221 extern void fpload(unsigned long *fpregs, unsigned long *fsr);
223 static unsigned long init_fsr = 0x0UL;
224 static unsigned long init_fregs[32] __attribute__ ((aligned (8))) =
225 { ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
226 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
227 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
228 ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL };
230 void do_fpd_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
233 /* Sanity check... */
235 die_if_kernel("Kernel gets FloatingPenguinUnit disabled trap", regs);
237 put_psr(get_psr() | PSR_EF); /* Allow FPU ops. */
240 if(last_task_used_math == current)
242 if(last_task_used_math) {
243 /* Other processes fpu state, save away */
244 struct task_struct *fptask = last_task_used_math;
245 fpsave(&fptask->thread.float_regs[0], &fptask->thread.fsr,
246 &fptask->thread.fpqueue[0], &fptask->thread.fpqdepth);
248 last_task_used_math = current;
250 fpload(¤t->thread.float_regs[0], ¤t->thread.fsr);
252 /* Set initial sane state. */
253 fpload(&init_fregs[0], &init_fsr);
258 fpload(&init_fregs[0], &init_fsr);
261 fpload(¤t->thread.float_regs[0], ¤t->thread.fsr);
263 current_thread_info()->flags |= _TIF_USEDFPU;
267 static unsigned long fake_regs[32] __attribute__ ((aligned (8)));
268 static unsigned long fake_fsr;
269 static unsigned long fake_queue[32] __attribute__ ((aligned (8)));
270 static unsigned long fake_depth;
272 extern int do_mathemu(struct pt_regs *, struct task_struct *);
274 void do_fpe_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
282 struct task_struct *fpt = last_task_used_math;
284 struct task_struct *fpt = current;
286 put_psr(get_psr() | PSR_EF);
287 /* If nobody owns the fpu right now, just clear the
288 * error into our fake static buffer and hope it don't
289 * happen again. Thank you crashme...
294 if(!(task_thread_info(fpt)->flags & _TIF_USEDFPU)) {
296 fpsave(&fake_regs[0], &fake_fsr, &fake_queue[0], &fake_depth);
297 regs->psr &= ~PSR_EF;
300 fpsave(&fpt->thread.float_regs[0], &fpt->thread.fsr,
301 &fpt->thread.fpqueue[0], &fpt->thread.fpqdepth);
303 printk("Hmm, FP exception, fsr was %016lx\n", fpt->thread.fsr);
306 switch ((fpt->thread.fsr & 0x1c000)) {
307 /* switch on the contents of the ftt [floating point trap type] field */
310 printk("IEEE_754_exception\n");
313 case (2 << 14): /* unfinished_FPop (underflow & co) */
314 case (3 << 14): /* unimplemented_FPop (quad stuff, maybe sqrt) */
315 ret = do_mathemu(regs, fpt);
319 printk("sequence_error (OS bug...)\n");
322 printk("hardware_error (uhoh!)\n");
325 printk("invalid_fp_register (user error)\n");
327 #endif /* DEBUG_FPU */
329 /* If we successfully emulated the FPop, we pretend the trap never happened :-> */
331 fpload(¤t->thread.float_regs[0], ¤t->thread.fsr);
334 /* nope, better SIGFPE the offending process... */
337 task_thread_info(fpt)->flags &= ~_TIF_USEDFPU;
340 /* The first fsr store/load we tried trapped,
341 * the second one will not (we hope).
343 printk("WARNING: FPU exception from kernel mode. at pc=%08lx\n",
345 regs->pc = regs->npc;
349 die_if_kernel("Too many Penguin-FPU traps from kernel mode",
354 fsr = fpt->thread.fsr;
355 info.si_signo = SIGFPE;
357 info.si_addr = (void __user *)pc;
359 info.si_code = __SI_FAULT;
360 if ((fsr & 0x1c000) == (1 << 14)) {
362 info.si_code = FPE_FLTINV;
364 info.si_code = FPE_FLTOVF;
366 info.si_code = FPE_FLTUND;
368 info.si_code = FPE_FLTDIV;
370 info.si_code = FPE_FLTRES;
372 send_sig_info(SIGFPE, &info, fpt);
374 last_task_used_math = NULL;
376 regs->psr &= ~PSR_EF;
381 void handle_tag_overflow(struct pt_regs *regs, unsigned long pc, unsigned long npc,
387 die_if_kernel("Penguin overflow trap from kernel mode", regs);
388 info.si_signo = SIGEMT;
390 info.si_code = EMT_TAGOVF;
391 info.si_addr = (void __user *)pc;
393 send_sig_info(SIGEMT, &info, current);
396 void handle_watchpoint(struct pt_regs *regs, unsigned long pc, unsigned long npc,
400 printk("Watchpoint detected at PC %08lx NPC %08lx PSR %08lx\n",
404 panic("Tell me what a watchpoint trap is, and I'll then deal "
405 "with such a beast...");
408 void handle_reg_access(struct pt_regs *regs, unsigned long pc, unsigned long npc,
414 printk("Register Access Exception at PC %08lx NPC %08lx PSR %08lx\n",
417 info.si_signo = SIGBUS;
419 info.si_code = BUS_OBJERR;
420 info.si_addr = (void __user *)pc;
422 force_sig_info(SIGBUS, &info, current);
425 void handle_cp_disabled(struct pt_regs *regs, unsigned long pc, unsigned long npc,
430 info.si_signo = SIGILL;
432 info.si_code = ILL_COPROC;
433 info.si_addr = (void __user *)pc;
435 send_sig_info(SIGILL, &info, current);
438 void handle_cp_exception(struct pt_regs *regs, unsigned long pc, unsigned long npc,
444 printk("Co-Processor Exception at PC %08lx NPC %08lx PSR %08lx\n",
447 info.si_signo = SIGILL;
449 info.si_code = ILL_COPROC;
450 info.si_addr = (void __user *)pc;
452 send_sig_info(SIGILL, &info, current);
455 void handle_hw_divzero(struct pt_regs *regs, unsigned long pc, unsigned long npc,
460 info.si_signo = SIGFPE;
462 info.si_code = FPE_INTDIV;
463 info.si_addr = (void __user *)pc;
465 send_sig_info(SIGFPE, &info, current);
468 #ifdef CONFIG_DEBUG_BUGVERBOSE
469 void do_BUG(const char *file, int line)
471 // bust_spinlocks(1); XXX Not in our original BUG()
472 printk("kernel BUG at %s:%d!\n", file, line);
476 /* Since we have our mappings set up, on multiprocessors we can spin them
477 * up here so that timer interrupts work during initialization.
480 extern void sparc_cpu_startup(void);
482 int linux_smp_still_initting;
483 unsigned int thiscpus_tbr;
488 extern void thread_info_offsets_are_bolixed_pete(void);
490 /* Force linker to barf if mismatched */
491 if (TI_UWINMASK != offsetof(struct thread_info, uwinmask) ||
492 TI_TASK != offsetof(struct thread_info, task) ||
493 TI_EXECDOMAIN != offsetof(struct thread_info, exec_domain) ||
494 TI_FLAGS != offsetof(struct thread_info, flags) ||
495 TI_CPU != offsetof(struct thread_info, cpu) ||
496 TI_PREEMPT != offsetof(struct thread_info, preempt_count) ||
497 TI_SOFTIRQ != offsetof(struct thread_info, softirq_count) ||
498 TI_HARDIRQ != offsetof(struct thread_info, hardirq_count) ||
499 TI_KSP != offsetof(struct thread_info, ksp) ||
500 TI_KPC != offsetof(struct thread_info, kpc) ||
501 TI_KPSR != offsetof(struct thread_info, kpsr) ||
502 TI_KWIM != offsetof(struct thread_info, kwim) ||
503 TI_REG_WINDOW != offsetof(struct thread_info, reg_window) ||
504 TI_RWIN_SPTRS != offsetof(struct thread_info, rwbuf_stkptrs) ||
505 TI_W_SAVED != offsetof(struct thread_info, w_saved))
506 thread_info_offsets_are_bolixed_pete();
508 /* Attach to the address space of init_task. */
509 atomic_inc(&init_mm.mm_count);
510 current->active_mm = &init_mm;
512 /* NOTE: Other cpus have this done as they are started