OSDN Git Service

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