sparc/kernel/: possible cleanups
[safe/jmp/linux-2.6] / arch / sparc / kernel / traps.c
1 /*
2  * arch/sparc/kernel/traps.c
3  *
4  * Copyright 1995 David S. Miller (davem@caip.rutgers.edu)
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/kallsyms.h>
15 #include <linux/signal.h>
16 #include <linux/smp.h>
17 #include <linux/smp_lock.h>
18 #include <linux/kdebug.h>
19
20 #include <asm/delay.h>
21 #include <asm/system.h>
22 #include <asm/ptrace.h>
23 #include <asm/oplib.h>
24 #include <asm/page.h>
25 #include <asm/pgtable.h>
26 #include <asm/unistd.h>
27 #include <asm/traps.h>
28
29 /* #define TRAP_DEBUG */
30
31 struct trap_trace_entry {
32         unsigned long pc;
33         unsigned long type;
34 };
35
36 void syscall_trace_entry(struct pt_regs *regs)
37 {
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]);
41 }
42
43 void syscall_trace_exit(struct pt_regs *regs)
44 {
45 }
46
47 void sun4m_nmi(struct pt_regs *regs)
48 {
49         unsigned long afsr, afar;
50
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"
55                              "mov 0x600, %%g1\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");
60         show_regs(regs);
61         prom_halt();
62 }
63
64 void sun4d_nmi(struct pt_regs *regs)
65 {
66         printk("Aieee: sun4d NMI received!\n");
67         printk("you lose buddy boy...\n");
68         show_regs(regs);
69         prom_halt();
70 }
71
72 static void instruction_dump(unsigned long *pc)
73 {
74         int i;
75         
76         if((((unsigned long) pc) & 3))
77                 return;
78
79         for(i = -3; i < 6; i++)
80                 printk("%c%08lx%c",i?' ':'<',pc[i],i?' ':'>');
81         printk("\n");
82 }
83
84 #define __SAVE __asm__ __volatile__("save %sp, -0x40, %sp\n\t")
85 #define __RESTORE __asm__ __volatile__("restore %g0, %g0, %g0\n\t")
86
87 void die_if_kernel(char *str, struct pt_regs *regs)
88 {
89         static int die_counter;
90         int count = 0;
91
92         /* Amuse the user. */
93         printk(
94 "              \\|/ ____ \\|/\n"
95 "              \"@'/ ,. \\`@\"\n"
96 "              /_| \\__/ |_\\\n"
97 "                 \\__U_/\n");
98
99         printk("%s(%d): %s [#%d]\n", current->comm, task_pid_nr(current), str, ++die_counter);
100         show_regs(regs);
101         add_taint(TAINT_DIE);
102
103         __SAVE; __SAVE; __SAVE; __SAVE;
104         __SAVE; __SAVE; __SAVE; __SAVE;
105         __RESTORE; __RESTORE; __RESTORE; __RESTORE;
106         __RESTORE; __RESTORE; __RESTORE; __RESTORE;
107
108         {
109                 struct reg_window *rw = (struct reg_window *)regs->u_regs[UREG_FP];
110
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.
114                  */
115                 while(rw                                        &&
116                       count++ < 30                              &&
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];
122                 }
123         }
124         printk("Instruction DUMP:");
125         instruction_dump ((unsigned long *) regs->pc);
126         if(regs->psr & PSR_PS)
127                 do_exit(SIGKILL);
128         do_exit(SIGSEGV);
129 }
130
131 void do_hw_interrupt(struct pt_regs *regs, unsigned long type)
132 {
133         siginfo_t info;
134
135         if(type < 0x80) {
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);
139         }       
140
141         if(regs->psr & PSR_PS)
142                 die_if_kernel("Kernel bad trap", regs);
143
144         info.si_signo = SIGILL;
145         info.si_errno = 0;
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);
150 }
151
152 void do_illegal_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
153                             unsigned long psr)
154 {
155         extern int do_user_muldiv (struct pt_regs *, unsigned long);
156         siginfo_t info;
157
158         if(psr & PSR_PS)
159                 die_if_kernel("Kernel illegal instruction", regs);
160 #ifdef TRAP_DEBUG
161         printk("Ill instr. at pc=%08lx instruction is %08lx\n",
162                regs->pc, *(unsigned long *)regs->pc);
163 #endif
164         if (!do_user_muldiv (regs, pc))
165                 return;
166
167         info.si_signo = SIGILL;
168         info.si_errno = 0;
169         info.si_code = ILL_ILLOPC;
170         info.si_addr = (void __user *)pc;
171         info.si_trapno = 0;
172         send_sig_info(SIGILL, &info, current);
173 }
174
175 void do_priv_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
176                          unsigned long psr)
177 {
178         siginfo_t info;
179
180         if(psr & PSR_PS)
181                 die_if_kernel("Penguin instruction from Penguin mode??!?!", regs);
182         info.si_signo = SIGILL;
183         info.si_errno = 0;
184         info.si_code = ILL_PRVOPC;
185         info.si_addr = (void __user *)pc;
186         info.si_trapno = 0;
187         send_sig_info(SIGILL, &info, current);
188 }
189
190 /* XXX User may want to be allowed to do this. XXX */
191
192 void do_memaccess_unaligned(struct pt_regs *regs, unsigned long pc, unsigned long npc,
193                             unsigned long psr)
194 {
195         siginfo_t info;
196
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); */
202         }
203 #if 0
204         show_regs (regs);
205         instruction_dump ((unsigned long *) regs->pc);
206         printk ("do_MNA!\n");
207 #endif
208         info.si_signo = SIGBUS;
209         info.si_errno = 0;
210         info.si_code = BUS_ADRALN;
211         info.si_addr = /* FIXME: Should dig out mna address */ (void *)0;
212         info.si_trapno = 0;
213         send_sig_info(SIGBUS, &info, current);
214 }
215
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);
219
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 };
226
227 void do_fpd_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
228                  unsigned long psr)
229 {
230         /* Sanity check... */
231         if(psr & PSR_PS)
232                 die_if_kernel("Kernel gets FloatingPenguinUnit disabled trap", regs);
233
234         put_psr(get_psr() | PSR_EF);    /* Allow FPU ops. */
235         regs->psr |= PSR_EF;
236 #ifndef CONFIG_SMP
237         if(last_task_used_math == current)
238                 return;
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);
244         }
245         last_task_used_math = current;
246         if(used_math()) {
247                 fpload(&current->thread.float_regs[0], &current->thread.fsr);
248         } else {
249                 /* Set initial sane state. */
250                 fpload(&init_fregs[0], &init_fsr);
251                 set_used_math();
252         }
253 #else
254         if(!used_math()) {
255                 fpload(&init_fregs[0], &init_fsr);
256                 set_used_math();
257         } else {
258                 fpload(&current->thread.float_regs[0], &current->thread.fsr);
259         }
260         set_thread_flag(TIF_USEDFPU);
261 #endif
262 }
263
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;
268
269 extern int do_mathemu(struct pt_regs *, struct task_struct *);
270
271 void do_fpe_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
272                  unsigned long psr)
273 {
274         static int calls;
275         siginfo_t info;
276         unsigned long fsr;
277         int ret = 0;
278 #ifndef CONFIG_SMP
279         struct task_struct *fpt = last_task_used_math;
280 #else
281         struct task_struct *fpt = current;
282 #endif
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...
287          */
288 #ifndef CONFIG_SMP
289         if(!fpt) {
290 #else
291         if (!test_tsk_thread_flag(fpt, TIF_USEDFPU)) {
292 #endif
293                 fpsave(&fake_regs[0], &fake_fsr, &fake_queue[0], &fake_depth);
294                 regs->psr &= ~PSR_EF;
295                 return;
296         }
297         fpsave(&fpt->thread.float_regs[0], &fpt->thread.fsr,
298                &fpt->thread.fpqueue[0], &fpt->thread.fpqdepth);
299 #ifdef DEBUG_FPU
300         printk("Hmm, FP exception, fsr was %016lx\n", fpt->thread.fsr);
301 #endif
302
303         switch ((fpt->thread.fsr & 0x1c000)) {
304         /* switch on the contents of the ftt [floating point trap type] field */
305 #ifdef DEBUG_FPU
306         case (1 << 14):
307                 printk("IEEE_754_exception\n");
308                 break;
309 #endif
310         case (2 << 14):  /* unfinished_FPop (underflow & co) */
311         case (3 << 14):  /* unimplemented_FPop (quad stuff, maybe sqrt) */
312                 ret = do_mathemu(regs, fpt);
313                 break;
314 #ifdef DEBUG_FPU
315         case (4 << 14):
316                 printk("sequence_error (OS bug...)\n");
317                 break;
318         case (5 << 14):
319                 printk("hardware_error (uhoh!)\n");
320                 break;
321         case (6 << 14):
322                 printk("invalid_fp_register (user error)\n");
323                 break;
324 #endif /* DEBUG_FPU */
325         }
326         /* If we successfully emulated the FPop, we pretend the trap never happened :-> */
327         if (ret) {
328                 fpload(&current->thread.float_regs[0], &current->thread.fsr);
329                 return;
330         }
331         /* nope, better SIGFPE the offending process... */
332                
333 #ifdef CONFIG_SMP
334         clear_tsk_thread_flag(fpt, TIF_USEDFPU);
335 #endif
336         if(psr & PSR_PS) {
337                 /* The first fsr store/load we tried trapped,
338                  * the second one will not (we hope).
339                  */
340                 printk("WARNING: FPU exception from kernel mode. at pc=%08lx\n",
341                        regs->pc);
342                 regs->pc = regs->npc;
343                 regs->npc += 4;
344                 calls++;
345                 if(calls > 2)
346                         die_if_kernel("Too many Penguin-FPU traps from kernel mode",
347                                       regs);
348                 return;
349         }
350
351         fsr = fpt->thread.fsr;
352         info.si_signo = SIGFPE;
353         info.si_errno = 0;
354         info.si_addr = (void __user *)pc;
355         info.si_trapno = 0;
356         info.si_code = __SI_FAULT;
357         if ((fsr & 0x1c000) == (1 << 14)) {
358                 if (fsr & 0x10)
359                         info.si_code = FPE_FLTINV;
360                 else if (fsr & 0x08)
361                         info.si_code = FPE_FLTOVF;
362                 else if (fsr & 0x04)
363                         info.si_code = FPE_FLTUND;
364                 else if (fsr & 0x02)
365                         info.si_code = FPE_FLTDIV;
366                 else if (fsr & 0x01)
367                         info.si_code = FPE_FLTRES;
368         }
369         send_sig_info(SIGFPE, &info, fpt);
370 #ifndef CONFIG_SMP
371         last_task_used_math = NULL;
372 #endif
373         regs->psr &= ~PSR_EF;
374         if(calls > 0)
375                 calls=0;
376 }
377
378 void handle_tag_overflow(struct pt_regs *regs, unsigned long pc, unsigned long npc,
379                          unsigned long psr)
380 {
381         siginfo_t info;
382
383         if(psr & PSR_PS)
384                 die_if_kernel("Penguin overflow trap from kernel mode", regs);
385         info.si_signo = SIGEMT;
386         info.si_errno = 0;
387         info.si_code = EMT_TAGOVF;
388         info.si_addr = (void __user *)pc;
389         info.si_trapno = 0;
390         send_sig_info(SIGEMT, &info, current);
391 }
392
393 void handle_watchpoint(struct pt_regs *regs, unsigned long pc, unsigned long npc,
394                        unsigned long psr)
395 {
396 #ifdef TRAP_DEBUG
397         printk("Watchpoint detected at PC %08lx NPC %08lx PSR %08lx\n",
398                pc, npc, psr);
399 #endif
400         if(psr & PSR_PS)
401                 panic("Tell me what a watchpoint trap is, and I'll then deal "
402                       "with such a beast...");
403 }
404
405 void handle_reg_access(struct pt_regs *regs, unsigned long pc, unsigned long npc,
406                        unsigned long psr)
407 {
408         siginfo_t info;
409
410 #ifdef TRAP_DEBUG
411         printk("Register Access Exception at PC %08lx NPC %08lx PSR %08lx\n",
412                pc, npc, psr);
413 #endif
414         info.si_signo = SIGBUS;
415         info.si_errno = 0;
416         info.si_code = BUS_OBJERR;
417         info.si_addr = (void __user *)pc;
418         info.si_trapno = 0;
419         force_sig_info(SIGBUS, &info, current);
420 }
421
422 void handle_cp_disabled(struct pt_regs *regs, unsigned long pc, unsigned long npc,
423                         unsigned long psr)
424 {
425         siginfo_t info;
426
427         info.si_signo = SIGILL;
428         info.si_errno = 0;
429         info.si_code = ILL_COPROC;
430         info.si_addr = (void __user *)pc;
431         info.si_trapno = 0;
432         send_sig_info(SIGILL, &info, current);
433 }
434
435 void handle_cp_exception(struct pt_regs *regs, unsigned long pc, unsigned long npc,
436                          unsigned long psr)
437 {
438         siginfo_t info;
439
440 #ifdef TRAP_DEBUG
441         printk("Co-Processor Exception at PC %08lx NPC %08lx PSR %08lx\n",
442                pc, npc, psr);
443 #endif
444         info.si_signo = SIGILL;
445         info.si_errno = 0;
446         info.si_code = ILL_COPROC;
447         info.si_addr = (void __user *)pc;
448         info.si_trapno = 0;
449         send_sig_info(SIGILL, &info, current);
450 }
451
452 void handle_hw_divzero(struct pt_regs *regs, unsigned long pc, unsigned long npc,
453                        unsigned long psr)
454 {
455         siginfo_t info;
456
457         info.si_signo = SIGFPE;
458         info.si_errno = 0;
459         info.si_code = FPE_INTDIV;
460         info.si_addr = (void __user *)pc;
461         info.si_trapno = 0;
462         send_sig_info(SIGFPE, &info, current);
463 }
464
465 #ifdef CONFIG_DEBUG_BUGVERBOSE
466 void do_BUG(const char *file, int line)
467 {
468         // bust_spinlocks(1);   XXX Not in our original BUG()
469         printk("kernel BUG at %s:%d!\n", file, line);
470 }
471 #endif
472
473 /* Since we have our mappings set up, on multiprocessors we can spin them
474  * up here so that timer interrupts work during initialization.
475  */
476
477 extern void sparc_cpu_startup(void);
478
479 void trap_init(void)
480 {
481         extern void thread_info_offsets_are_bolixed_pete(void);
482
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();
500
501         /* Attach to the address space of init_task. */
502         atomic_inc(&init_mm.mm_count);
503         current->active_mm = &init_mm;
504
505         /* NOTE: Other cpus have this done as they are started
506          *       up on SMP.
507          */
508 }