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