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