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