Blackfin: make hardware trace output a little more useful
[linux-3.10.git] / arch / blackfin / kernel / traps.c
1 /*
2  * Main exception handling logic.
3  *
4  * Copyright 2004-2010 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2 or later
7  */
8
9 #include <linux/bug.h>
10 #include <linux/uaccess.h>
11 #include <linux/module.h>
12 #include <asm/traps.h>
13 #include <asm/cplb.h>
14 #include <asm/blackfin.h>
15 #include <asm/irq_handler.h>
16 #include <linux/irq.h>
17 #include <asm/trace.h>
18 #include <asm/fixed_code.h>
19
20 #ifdef CONFIG_KGDB
21 # include <linux/kgdb.h>
22
23 # define CHK_DEBUGGER_TRAP() \
24         do { \
25                 kgdb_handle_exception(trapnr, sig, info.si_code, fp); \
26         } while (0)
27 # define CHK_DEBUGGER_TRAP_MAYBE() \
28         do { \
29                 if (kgdb_connected) \
30                         CHK_DEBUGGER_TRAP(); \
31         } while (0)
32 #else
33 # define CHK_DEBUGGER_TRAP() do { } while (0)
34 # define CHK_DEBUGGER_TRAP_MAYBE() do { } while (0)
35 #endif
36
37
38 #ifdef CONFIG_DEBUG_VERBOSE
39 #define verbose_printk(fmt, arg...) \
40         printk(fmt, ##arg)
41 #else
42 #define verbose_printk(fmt, arg...) \
43         ({ if (0) printk(fmt, ##arg); 0; })
44 #endif
45
46 #if defined(CONFIG_DEBUG_MMRS) || defined(CONFIG_DEBUG_MMRS_MODULE)
47 u32 last_seqstat;
48 #ifdef CONFIG_DEBUG_MMRS_MODULE
49 EXPORT_SYMBOL(last_seqstat);
50 #endif
51 #endif
52
53 /* Initiate the event table handler */
54 void __init trap_init(void)
55 {
56         CSYNC();
57         bfin_write_EVT3(trap);
58         CSYNC();
59 }
60
61 static int kernel_mode_regs(struct pt_regs *regs)
62 {
63         return regs->ipend & 0xffc0;
64 }
65
66 asmlinkage notrace void trap_c(struct pt_regs *fp)
67 {
68 #ifdef CONFIG_DEBUG_BFIN_HWTRACE_ON
69         int j;
70 #endif
71         unsigned int cpu = raw_smp_processor_id();
72         const char *strerror = NULL;
73         int sig = 0;
74         siginfo_t info;
75         unsigned long trapnr = fp->seqstat & SEQSTAT_EXCAUSE;
76
77         trace_buffer_save(j);
78 #if defined(CONFIG_DEBUG_MMRS) || defined(CONFIG_DEBUG_MMRS_MODULE)
79         last_seqstat = (u32)fp->seqstat;
80 #endif
81
82         /* Important - be very careful dereferncing pointers - will lead to
83          * double faults if the stack has become corrupt
84          */
85
86         /* trap_c() will be called for exceptions. During exceptions
87          * processing, the pc value should be set with retx value.
88          * With this change we can cleanup some code in signal.c- TODO
89          */
90         fp->orig_pc = fp->retx;
91         /* printk("exception: 0x%x, ipend=%x, reti=%x, retx=%x\n",
92                 trapnr, fp->ipend, fp->pc, fp->retx); */
93
94         /* send the appropriate signal to the user program */
95         switch (trapnr) {
96
97         /* This table works in conjuction with the one in ./mach-common/entry.S
98          * Some exceptions are handled there (in assembly, in exception space)
99          * Some are handled here, (in C, in interrupt space)
100          * Some, like CPLB, are handled in both, where the normal path is
101          * handled in assembly/exception space, and the error path is handled
102          * here
103          */
104
105         /* 0x00 - Linux Syscall, getting here is an error */
106         /* 0x01 - userspace gdb breakpoint, handled here */
107         case VEC_EXCPT01:
108                 info.si_code = TRAP_ILLTRAP;
109                 sig = SIGTRAP;
110                 CHK_DEBUGGER_TRAP_MAYBE();
111                 /* Check if this is a breakpoint in kernel space */
112                 if (kernel_mode_regs(fp))
113                         goto traps_done;
114                 else
115                         break;
116         /* 0x03 - User Defined, userspace stack overflow */
117         case VEC_EXCPT03:
118                 info.si_code = SEGV_STACKFLOW;
119                 sig = SIGSEGV;
120                 strerror = KERN_NOTICE EXC_0x03(KERN_NOTICE);
121                 CHK_DEBUGGER_TRAP_MAYBE();
122                 break;
123         /* 0x02 - KGDB initial connection and break signal trap */
124         case VEC_EXCPT02:
125 #ifdef CONFIG_KGDB
126                 info.si_code = TRAP_ILLTRAP;
127                 sig = SIGTRAP;
128                 CHK_DEBUGGER_TRAP();
129                 goto traps_done;
130 #endif
131         /* 0x04 - User Defined */
132         /* 0x05 - User Defined */
133         /* 0x06 - User Defined */
134         /* 0x07 - User Defined */
135         /* 0x08 - User Defined */
136         /* 0x09 - User Defined */
137         /* 0x0A - User Defined */
138         /* 0x0B - User Defined */
139         /* 0x0C - User Defined */
140         /* 0x0D - User Defined */
141         /* 0x0E - User Defined */
142         /* 0x0F - User Defined */
143         /* If we got here, it is most likely that someone was trying to use a
144          * custom exception handler, and it is not actually installed properly
145          */
146         case VEC_EXCPT04 ... VEC_EXCPT15:
147                 info.si_code = ILL_ILLPARAOP;
148                 sig = SIGILL;
149                 strerror = KERN_NOTICE EXC_0x04(KERN_NOTICE);
150                 CHK_DEBUGGER_TRAP_MAYBE();
151                 break;
152         /* 0x10 HW Single step, handled here */
153         case VEC_STEP:
154                 info.si_code = TRAP_STEP;
155                 sig = SIGTRAP;
156                 CHK_DEBUGGER_TRAP_MAYBE();
157                 /* Check if this is a single step in kernel space */
158                 if (kernel_mode_regs(fp))
159                         goto traps_done;
160                 else
161                         break;
162         /* 0x11 - Trace Buffer Full, handled here */
163         case VEC_OVFLOW:
164                 info.si_code = TRAP_TRACEFLOW;
165                 sig = SIGTRAP;
166                 strerror = KERN_NOTICE EXC_0x11(KERN_NOTICE);
167                 CHK_DEBUGGER_TRAP_MAYBE();
168                 break;
169         /* 0x12 - Reserved, Caught by default */
170         /* 0x13 - Reserved, Caught by default */
171         /* 0x14 - Reserved, Caught by default */
172         /* 0x15 - Reserved, Caught by default */
173         /* 0x16 - Reserved, Caught by default */
174         /* 0x17 - Reserved, Caught by default */
175         /* 0x18 - Reserved, Caught by default */
176         /* 0x19 - Reserved, Caught by default */
177         /* 0x1A - Reserved, Caught by default */
178         /* 0x1B - Reserved, Caught by default */
179         /* 0x1C - Reserved, Caught by default */
180         /* 0x1D - Reserved, Caught by default */
181         /* 0x1E - Reserved, Caught by default */
182         /* 0x1F - Reserved, Caught by default */
183         /* 0x20 - Reserved, Caught by default */
184         /* 0x21 - Undefined Instruction, handled here */
185         case VEC_UNDEF_I:
186 #ifdef CONFIG_BUG
187                 if (kernel_mode_regs(fp)) {
188                         switch (report_bug(fp->pc, fp)) {
189                         case BUG_TRAP_TYPE_NONE:
190                                 break;
191                         case BUG_TRAP_TYPE_WARN:
192                                 dump_bfin_trace_buffer();
193                                 fp->pc += 2;
194                                 goto traps_done;
195                         case BUG_TRAP_TYPE_BUG:
196                                 /* call to panic() will dump trace, and it is
197                                  * off at this point, so it won't be clobbered
198                                  */
199                                 panic("BUG()");
200                         }
201                 }
202 #endif
203                 info.si_code = ILL_ILLOPC;
204                 sig = SIGILL;
205                 strerror = KERN_NOTICE EXC_0x21(KERN_NOTICE);
206                 CHK_DEBUGGER_TRAP_MAYBE();
207                 break;
208         /* 0x22 - Illegal Instruction Combination, handled here */
209         case VEC_ILGAL_I:
210                 info.si_code = ILL_ILLPARAOP;
211                 sig = SIGILL;
212                 strerror = KERN_NOTICE EXC_0x22(KERN_NOTICE);
213                 CHK_DEBUGGER_TRAP_MAYBE();
214                 break;
215         /* 0x23 - Data CPLB protection violation, handled here */
216         case VEC_CPLB_VL:
217                 info.si_code = ILL_CPLB_VI;
218                 sig = SIGSEGV;
219                 strerror = KERN_NOTICE EXC_0x23(KERN_NOTICE);
220                 CHK_DEBUGGER_TRAP_MAYBE();
221                 break;
222         /* 0x24 - Data access misaligned, handled here */
223         case VEC_MISALI_D:
224                 info.si_code = BUS_ADRALN;
225                 sig = SIGBUS;
226                 strerror = KERN_NOTICE EXC_0x24(KERN_NOTICE);
227                 CHK_DEBUGGER_TRAP_MAYBE();
228                 break;
229         /* 0x25 - Unrecoverable Event, handled here */
230         case VEC_UNCOV:
231                 info.si_code = ILL_ILLEXCPT;
232                 sig = SIGILL;
233                 strerror = KERN_NOTICE EXC_0x25(KERN_NOTICE);
234                 CHK_DEBUGGER_TRAP_MAYBE();
235                 break;
236         /* 0x26 - Data CPLB Miss, normal case is handled in _cplb_hdr,
237                 error case is handled here */
238         case VEC_CPLB_M:
239                 info.si_code = BUS_ADRALN;
240                 sig = SIGBUS;
241                 strerror = KERN_NOTICE EXC_0x26(KERN_NOTICE);
242                 break;
243         /* 0x27 - Data CPLB Multiple Hits - Linux Trap Zero, handled here */
244         case VEC_CPLB_MHIT:
245                 info.si_code = ILL_CPLB_MULHIT;
246                 sig = SIGSEGV;
247 #ifdef CONFIG_DEBUG_HUNT_FOR_ZERO
248                 if (cpu_pda[cpu].dcplb_fault_addr < FIXED_CODE_START)
249                         strerror = KERN_NOTICE "NULL pointer access\n";
250                 else
251 #endif
252                         strerror = KERN_NOTICE EXC_0x27(KERN_NOTICE);
253                 CHK_DEBUGGER_TRAP_MAYBE();
254                 break;
255         /* 0x28 - Emulation Watchpoint, handled here */
256         case VEC_WATCH:
257                 info.si_code = TRAP_WATCHPT;
258                 sig = SIGTRAP;
259                 pr_debug(EXC_0x28(KERN_DEBUG));
260                 CHK_DEBUGGER_TRAP_MAYBE();
261                 /* Check if this is a watchpoint in kernel space */
262                 if (kernel_mode_regs(fp))
263                         goto traps_done;
264                 else
265                         break;
266 #ifdef CONFIG_BF535
267         /* 0x29 - Instruction fetch access error (535 only) */
268         case VEC_ISTRU_VL:      /* ADSP-BF535 only (MH) */
269                 info.si_code = BUS_OPFETCH;
270                 sig = SIGBUS;
271                 strerror = KERN_NOTICE "BF535: VEC_ISTRU_VL\n";
272                 CHK_DEBUGGER_TRAP_MAYBE();
273                 break;
274 #else
275         /* 0x29 - Reserved, Caught by default */
276 #endif
277         /* 0x2A - Instruction fetch misaligned, handled here */
278         case VEC_MISALI_I:
279                 info.si_code = BUS_ADRALN;
280                 sig = SIGBUS;
281                 strerror = KERN_NOTICE EXC_0x2A(KERN_NOTICE);
282                 CHK_DEBUGGER_TRAP_MAYBE();
283                 break;
284         /* 0x2B - Instruction CPLB protection violation, handled here */
285         case VEC_CPLB_I_VL:
286                 info.si_code = ILL_CPLB_VI;
287                 sig = SIGBUS;
288                 strerror = KERN_NOTICE EXC_0x2B(KERN_NOTICE);
289                 CHK_DEBUGGER_TRAP_MAYBE();
290                 break;
291         /* 0x2C - Instruction CPLB miss, handled in _cplb_hdr */
292         case VEC_CPLB_I_M:
293                 info.si_code = ILL_CPLB_MISS;
294                 sig = SIGBUS;
295                 strerror = KERN_NOTICE EXC_0x2C(KERN_NOTICE);
296                 break;
297         /* 0x2D - Instruction CPLB Multiple Hits, handled here */
298         case VEC_CPLB_I_MHIT:
299                 info.si_code = ILL_CPLB_MULHIT;
300                 sig = SIGSEGV;
301 #ifdef CONFIG_DEBUG_HUNT_FOR_ZERO
302                 if (cpu_pda[cpu].icplb_fault_addr < FIXED_CODE_START)
303                         strerror = KERN_NOTICE "Jump to NULL address\n";
304                 else
305 #endif
306                         strerror = KERN_NOTICE EXC_0x2D(KERN_NOTICE);
307                 CHK_DEBUGGER_TRAP_MAYBE();
308                 break;
309         /* 0x2E - Illegal use of Supervisor Resource, handled here */
310         case VEC_ILL_RES:
311                 info.si_code = ILL_PRVOPC;
312                 sig = SIGILL;
313                 strerror = KERN_NOTICE EXC_0x2E(KERN_NOTICE);
314                 CHK_DEBUGGER_TRAP_MAYBE();
315                 break;
316         /* 0x2F - Reserved, Caught by default */
317         /* 0x30 - Reserved, Caught by default */
318         /* 0x31 - Reserved, Caught by default */
319         /* 0x32 - Reserved, Caught by default */
320         /* 0x33 - Reserved, Caught by default */
321         /* 0x34 - Reserved, Caught by default */
322         /* 0x35 - Reserved, Caught by default */
323         /* 0x36 - Reserved, Caught by default */
324         /* 0x37 - Reserved, Caught by default */
325         /* 0x38 - Reserved, Caught by default */
326         /* 0x39 - Reserved, Caught by default */
327         /* 0x3A - Reserved, Caught by default */
328         /* 0x3B - Reserved, Caught by default */
329         /* 0x3C - Reserved, Caught by default */
330         /* 0x3D - Reserved, Caught by default */
331         /* 0x3E - Reserved, Caught by default */
332         /* 0x3F - Reserved, Caught by default */
333         case VEC_HWERR:
334                 info.si_code = BUS_ADRALN;
335                 sig = SIGBUS;
336                 switch (fp->seqstat & SEQSTAT_HWERRCAUSE) {
337                 /* System MMR Error */
338                 case (SEQSTAT_HWERRCAUSE_SYSTEM_MMR):
339                         info.si_code = BUS_ADRALN;
340                         sig = SIGBUS;
341                         strerror = KERN_NOTICE HWC_x2(KERN_NOTICE);
342                         break;
343                 /* External Memory Addressing Error */
344                 case (SEQSTAT_HWERRCAUSE_EXTERN_ADDR):
345                         if (ANOMALY_05000310) {
346                                 static unsigned long anomaly_rets;
347
348                                 if ((fp->pc >= (L1_CODE_START + L1_CODE_LENGTH - 512)) &&
349                                     (fp->pc < (L1_CODE_START + L1_CODE_LENGTH))) {
350                                         /*
351                                          * A false hardware error will happen while fetching at
352                                          * the L1 instruction SRAM boundary.  Ignore it.
353                                          */
354                                         anomaly_rets = fp->rets;
355                                         goto traps_done;
356                                 } else if (fp->rets == anomaly_rets) {
357                                         /*
358                                          * While boundary code returns to a function, at the ret
359                                          * point, a new false hardware error might occur too based
360                                          * on tests.  Ignore it too.
361                                          */
362                                         goto traps_done;
363                                 } else if ((fp->rets >= (L1_CODE_START + L1_CODE_LENGTH - 512)) &&
364                                            (fp->rets < (L1_CODE_START + L1_CODE_LENGTH))) {
365                                         /*
366                                          * If boundary code calls a function, at the entry point,
367                                          * a new false hardware error maybe happen based on tests.
368                                          * Ignore it too.
369                                          */
370                                         goto traps_done;
371                                 } else
372                                         anomaly_rets = 0;
373                         }
374
375                         info.si_code = BUS_ADRERR;
376                         sig = SIGBUS;
377                         strerror = KERN_NOTICE HWC_x3(KERN_NOTICE);
378                         break;
379                 /* Performance Monitor Overflow */
380                 case (SEQSTAT_HWERRCAUSE_PERF_FLOW):
381                         strerror = KERN_NOTICE HWC_x12(KERN_NOTICE);
382                         break;
383                 /* RAISE 5 instruction */
384                 case (SEQSTAT_HWERRCAUSE_RAISE_5):
385                         printk(KERN_NOTICE HWC_x18(KERN_NOTICE));
386                         break;
387                 default:        /* Reserved */
388                         printk(KERN_NOTICE HWC_default(KERN_NOTICE));
389                         break;
390                 }
391                 CHK_DEBUGGER_TRAP_MAYBE();
392                 break;
393         /*
394          * We should be handling all known exception types above,
395          * if we get here we hit a reserved one, so panic
396          */
397         default:
398                 info.si_code = ILL_ILLPARAOP;
399                 sig = SIGILL;
400                 verbose_printk(KERN_EMERG "Caught Unhandled Exception, code = %08lx\n",
401                         (fp->seqstat & SEQSTAT_EXCAUSE));
402                 CHK_DEBUGGER_TRAP_MAYBE();
403                 break;
404         }
405
406         BUG_ON(sig == 0);
407
408         /* If the fault was caused by a kernel thread, or interrupt handler
409          * we will kernel panic, so the system reboots.
410          */
411         if (kernel_mode_regs(fp) || (current && !current->mm)) {
412                 console_verbose();
413                 oops_in_progress = 1;
414         }
415
416         if (sig != SIGTRAP) {
417                 if (strerror)
418                         verbose_printk(strerror);
419
420                 dump_bfin_process(fp);
421                 dump_bfin_mem(fp);
422                 show_regs(fp);
423
424                 /* Print out the trace buffer if it makes sense */
425 #ifndef CONFIG_DEBUG_BFIN_NO_KERN_HWTRACE
426                 if (trapnr == VEC_CPLB_I_M || trapnr == VEC_CPLB_M)
427                         verbose_printk(KERN_NOTICE "No trace since you do not have "
428                                "CONFIG_DEBUG_BFIN_NO_KERN_HWTRACE enabled\n\n");
429                 else
430 #endif
431                         dump_bfin_trace_buffer();
432
433                 if (oops_in_progress) {
434                         /* Dump the current kernel stack */
435                         verbose_printk(KERN_NOTICE "Kernel Stack\n");
436                         show_stack(current, NULL);
437                         print_modules();
438 #ifndef CONFIG_ACCESS_CHECK
439                         verbose_printk(KERN_EMERG "Please turn on "
440                                "CONFIG_ACCESS_CHECK\n");
441 #endif
442                         panic("Kernel exception");
443                 } else {
444 #ifdef CONFIG_DEBUG_VERBOSE
445                         unsigned long *stack;
446                         /* Dump the user space stack */
447                         stack = (unsigned long *)rdusp();
448                         verbose_printk(KERN_NOTICE "Userspace Stack\n");
449                         show_stack(NULL, stack);
450 #endif
451                 }
452         }
453
454 #ifdef CONFIG_IPIPE
455         if (!ipipe_trap_notify(fp->seqstat & 0x3f, fp))
456 #endif
457         {
458                 info.si_signo = sig;
459                 info.si_errno = 0;
460                 switch (trapnr) {
461                 case VEC_CPLB_VL:
462                 case VEC_MISALI_D:
463                 case VEC_CPLB_M:
464                 case VEC_CPLB_MHIT:
465                         info.si_addr = (void __user *)cpu_pda[cpu].dcplb_fault_addr;
466                         break;
467                 default:
468                         info.si_addr = (void __user *)fp->pc;
469                         break;
470                 }
471                 force_sig_info(sig, &info, current);
472         }
473
474         if ((ANOMALY_05000461 && trapnr == VEC_HWERR && !access_ok(VERIFY_READ, fp->pc, 8)) ||
475             (ANOMALY_05000281 && trapnr == VEC_HWERR) ||
476             (ANOMALY_05000189 && (trapnr == VEC_CPLB_I_VL || trapnr == VEC_CPLB_VL)))
477                 fp->pc = SAFE_USER_INSTRUCTION;
478
479  traps_done:
480         trace_buffer_restore(j);
481 }
482
483 asmlinkage void double_fault_c(struct pt_regs *fp)
484 {
485 #ifdef CONFIG_DEBUG_BFIN_HWTRACE_ON
486         int j;
487         trace_buffer_save(j);
488 #endif
489
490         console_verbose();
491         oops_in_progress = 1;
492 #ifdef CONFIG_DEBUG_VERBOSE
493         printk(KERN_EMERG "Double Fault\n");
494 #ifdef CONFIG_DEBUG_DOUBLEFAULT_PRINT
495         if (((long)fp->seqstat &  SEQSTAT_EXCAUSE) == VEC_UNCOV) {
496                 unsigned int cpu = raw_smp_processor_id();
497                 char buf[150];
498                 decode_address(buf, cpu_pda[cpu].retx_doublefault);
499                 printk(KERN_EMERG "While handling exception (EXCAUSE = 0x%x) at %s:\n",
500                         (unsigned int)cpu_pda[cpu].seqstat_doublefault & SEQSTAT_EXCAUSE, buf);
501                 decode_address(buf, cpu_pda[cpu].dcplb_doublefault_addr);
502                 printk(KERN_NOTICE "   DCPLB_FAULT_ADDR: %s\n", buf);
503                 decode_address(buf, cpu_pda[cpu].icplb_doublefault_addr);
504                 printk(KERN_NOTICE "   ICPLB_FAULT_ADDR: %s\n", buf);
505
506                 decode_address(buf, fp->retx);
507                 printk(KERN_NOTICE "The instruction at %s caused a double exception\n", buf);
508         } else
509 #endif
510         {
511                 dump_bfin_process(fp);
512                 dump_bfin_mem(fp);
513                 show_regs(fp);
514                 dump_bfin_trace_buffer();
515         }
516 #endif
517         panic("Double Fault - unrecoverable event");
518
519 }
520
521
522 void panic_cplb_error(int cplb_panic, struct pt_regs *fp)
523 {
524         switch (cplb_panic) {
525         case CPLB_NO_UNLOCKED:
526                 printk(KERN_EMERG "All CPLBs are locked\n");
527                 break;
528         case CPLB_PROT_VIOL:
529                 return;
530         case CPLB_NO_ADDR_MATCH:
531                 return;
532         case CPLB_UNKNOWN_ERR:
533                 printk(KERN_EMERG "Unknown CPLB Exception\n");
534                 break;
535         }
536
537         oops_in_progress = 1;
538
539         dump_bfin_process(fp);
540         dump_bfin_mem(fp);
541         show_regs(fp);
542         dump_stack();
543         panic("Unrecoverable event");
544 }
545
546 #ifdef CONFIG_BUG
547 int is_valid_bugaddr(unsigned long addr)
548 {
549         unsigned int opcode;
550
551         if (!get_instruction(&opcode, (unsigned short *)addr))
552                 return 0;
553
554         return opcode == BFIN_BUG_OPCODE;
555 }
556 #endif
557
558 /* stub this out */
559 #ifndef CONFIG_DEBUG_VERBOSE
560 void show_regs(struct pt_regs *fp)
561 {
562
563 }
564 #endif