original comment: +Wilson03172004,marked due to this pci host does not support MWI
[linux-2.4.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/config.h>
13 #include <linux/sched.h>  /* for jiffies */
14 #include <linux/kernel.h>
15 #include <linux/signal.h>
16 #include <linux/smp.h>
17 #include <linux/smp_lock.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/kdebug.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         int count = 0;
93
94         /* Amuse the user. */
95         printk(
96 "              \\|/ ____ \\|/\n"
97 "              \"@'/ ,. \\`@\"\n"
98 "              /_| \\__/ |_\\\n"
99 "                 \\__U_/\n");
100
101         printk("%s(%d): %s\n", current->comm, current->pid, str);
102         show_regs(regs);
103
104         __SAVE; __SAVE; __SAVE; __SAVE;
105         __SAVE; __SAVE; __SAVE; __SAVE;
106         __RESTORE; __RESTORE; __RESTORE; __RESTORE;
107         __RESTORE; __RESTORE; __RESTORE; __RESTORE;
108
109         {
110                 struct reg_window *rw = (struct reg_window *)regs->u_regs[UREG_FP];
111
112                 /* Stop the back trace when we hit userland or we
113                  * find some badly aligned kernel stack. Set an upper
114                  * bound in case our stack is trashed and we loop.
115                  */
116                 while(rw                                        &&
117                       count++ < 30                              &&
118                       (((unsigned long) rw) >= PAGE_OFFSET)     &&
119                       !(((unsigned long) rw) & 0x7)) {
120                         printk("Caller[%08lx]\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(unsigned long type, unsigned long psr, unsigned long pc)
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", current->thread.kregs);
139         }       
140
141         if(psr & PSR_PS)
142                 die_if_kernel("Kernel bad trap", current->thread.kregs);
143
144         info.si_signo = SIGILL;
145         info.si_errno = 0;
146         info.si_code = ILL_ILLTRP;
147         info.si_addr = (void *)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 *)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 *)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(current->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                 current->used_math = 1;
252         }
253 #else
254         if(!current->used_math) {
255                 fpload(&init_fregs[0], &init_fsr);
256                 current->used_math = 1;
257         } else {
258                 fpload(&current->thread.float_regs[0], &current->thread.fsr);
259         }
260         current->flags |= PF_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(!(fpt->flags & PF_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         fpt->flags &= ~PF_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 *)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 *)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 *)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 *)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 *)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 *)pc;
461         info.si_trapno = 0;
462         send_sig_info(SIGFPE, &info, current);
463 }
464
465 /* Since we have our mappings set up, on multiprocessors we can spin them
466  * up here so that timer interrupts work during initialization.
467  */
468
469 extern void sparc_cpu_startup(void);
470
471 int linux_smp_still_initting;
472 unsigned int thiscpus_tbr;
473 int thiscpus_mid;
474
475 void trap_init(void)
476 {
477         /* Attach to the address space of init_task. */
478         atomic_inc(&init_mm.mm_count);
479         current->active_mm = &init_mm;
480
481         /* NOTE: Other cpus have this done as they are started
482          *       up on SMP.
483          */
484 }