]> git.karo-electronics.de Git - mv-sheeva.git/blob - arch/blackfin/kernel/traps.c
[Blackfin] arch: Don't oops_in_progress if single step is comming from the kernel
[mv-sheeva.git] / arch / blackfin / kernel / traps.c
1 /*
2  * File:         arch/blackfin/kernel/traps.c
3  * Based on:
4  * Author:       Hamish Macdonald
5  *
6  * Created:
7  * Description:  uses S/W interrupt 15 for the system calls
8  *
9  * Modified:
10  *               Copyright 2004-2006 Analog Devices Inc.
11  *
12  * Bugs:         Enter bugs at http://blackfin.uclinux.org/
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, see the file COPYING, or write
26  * to the Free Software Foundation, Inc.,
27  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28  */
29
30 #include <linux/uaccess.h>
31 #include <linux/interrupt.h>
32 #include <linux/module.h>
33 #include <linux/kallsyms.h>
34 #include <linux/fs.h>
35 #include <asm/traps.h>
36 #include <asm/cacheflush.h>
37 #include <asm/blackfin.h>
38 #include <asm/irq_handler.h>
39 #include <linux/irq.h>
40 #include <asm/trace.h>
41 #include <asm/fixed_code.h>
42 #include <asm/dma.h>
43
44 #ifdef CONFIG_KGDB
45 # include <linux/debugger.h>
46 # include <linux/kgdb.h>
47
48 # define CHK_DEBUGGER_TRAP() \
49         do { \
50                 CHK_DEBUGGER(trapnr, sig, info.si_code, fp, ); \
51         } while (0)
52 # define CHK_DEBUGGER_TRAP_MAYBE() \
53         do { \
54                 if (kgdb_connected) \
55                         CHK_DEBUGGER_TRAP(); \
56         } while (0)
57 #else
58 # define CHK_DEBUGGER_TRAP() do { } while (0)
59 # define CHK_DEBUGGER_TRAP_MAYBE() do { } while (0)
60 #endif
61
62 /* Initiate the event table handler */
63 void __init trap_init(void)
64 {
65         CSYNC();
66         bfin_write_EVT3(trap);
67         CSYNC();
68 }
69
70 int kstack_depth_to_print = 48;
71
72 static void decode_address(char *buf, unsigned long address)
73 {
74         struct vm_list_struct *vml;
75         struct task_struct *p;
76         struct mm_struct *mm;
77         unsigned long flags, offset;
78         unsigned int in_exception = bfin_read_IPEND() & 0x10;
79
80 #ifdef CONFIG_KALLSYMS
81         unsigned long symsize;
82         const char *symname;
83         char *modname;
84         char *delim = ":";
85         char namebuf[128];
86
87         /* look up the address and see if we are in kernel space */
88         symname = kallsyms_lookup(address, &symsize, &offset, &modname, namebuf);
89
90         if (symname) {
91                 /* yeah! kernel space! */
92                 if (!modname)
93                         modname = delim = "";
94                 sprintf(buf, "<0x%p> { %s%s%s%s + 0x%lx }",
95                               (void *)address, delim, modname, delim, symname,
96                               (unsigned long)offset);
97                 return;
98
99         }
100 #endif
101
102         /* Problem in fixed code section? */
103         if (address >= FIXED_CODE_START && address < FIXED_CODE_END) {
104                 sprintf(buf, "<0x%p> /* Maybe fixed code section */", (void *)address);
105                 return;
106         }
107
108         /* Problem somewhere before the kernel start address */
109         if (address < CONFIG_BOOT_LOAD) {
110                 sprintf(buf, "<0x%p> /* Maybe null pointer? */", (void *)address);
111                 return;
112         }
113
114         /* looks like we're off in user-land, so let's walk all the
115          * mappings of all our processes and see if we can't be a whee
116          * bit more specific
117          */
118         write_lock_irqsave(&tasklist_lock, flags);
119         for_each_process(p) {
120                 mm = (in_exception ? p->mm : get_task_mm(p));
121                 if (!mm)
122                         continue;
123
124                 vml = mm->context.vmlist;
125                 while (vml) {
126                         struct vm_area_struct *vma = vml->vma;
127
128                         if (address >= vma->vm_start && address < vma->vm_end) {
129                                 char *name = p->comm;
130                                 struct file *file = vma->vm_file;
131                                 if (file) {
132                                         char _tmpbuf[256];
133                                         name = d_path(file->f_dentry,
134                                                       file->f_vfsmnt,
135                                                       _tmpbuf,
136                                                       sizeof(_tmpbuf));
137                                 }
138
139                                 /* FLAT does not have its text aligned to the start of
140                                  * the map while FDPIC ELF does ...
141                                  */
142                                 if (current->mm &&
143                                     (address > current->mm->start_code) &&
144                                     (address < current->mm->end_code))
145                                         offset = address - current->mm->start_code;
146                                 else
147                                         offset = (address - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
148
149                                 sprintf(buf, "<0x%p> [ %s + 0x%lx ]",
150                                         (void *)address, name, offset);
151                                 if (!in_exception)
152                                         mmput(mm);
153                                 goto done;
154                         }
155
156                         vml = vml->next;
157                 }
158                 if (!in_exception)
159                         mmput(mm);
160         }
161
162         /* we were unable to find this address anywhere */
163         sprintf(buf, "<0x%p> /* unknown address */", (void *)address);
164
165 done:
166         write_unlock_irqrestore(&tasklist_lock, flags);
167 }
168
169 asmlinkage void double_fault_c(struct pt_regs *fp)
170 {
171         console_verbose();
172         oops_in_progress = 1;
173         printk(KERN_EMERG "\n" KERN_EMERG "Double Fault\n");
174         dump_bfin_process(fp);
175         dump_bfin_mem(fp);
176         show_regs(fp);
177         panic("Double Fault - unrecoverable event\n");
178
179 }
180
181 asmlinkage void trap_c(struct pt_regs *fp)
182 {
183 #ifdef CONFIG_DEBUG_BFIN_HWTRACE_ON
184         int j;
185 #endif
186         int sig = 0;
187         siginfo_t info;
188         unsigned long trapnr = fp->seqstat & SEQSTAT_EXCAUSE;
189
190         trace_buffer_save(j);
191
192         /* Important - be very careful dereferncing pointers - will lead to
193          * double faults if the stack has become corrupt
194          */
195
196         /* If the fault was caused by a kernel thread, or interrupt handler
197          * we will kernel panic, so the system reboots.
198          * If KGDB is enabled, don't set this for kernel breakpoints
199         */
200
201         /* TODO: check to see if we are in some sort of deferred HWERR
202          * that we should be able to recover from, not kernel panic
203          */
204         if ((bfin_read_IPEND() & 0xFFC0) && (trapnr != VEC_STEP)
205 #ifdef CONFIG_KGDB
206                 && (trapnr != VEC_EXCPT02)
207 #endif
208         ){
209                 console_verbose();
210                 oops_in_progress = 1;
211         } else if (current) {
212                 if (current->mm == NULL) {
213                         console_verbose();
214                         oops_in_progress = 1;
215                 }
216         }
217
218         /* trap_c() will be called for exceptions. During exceptions
219          * processing, the pc value should be set with retx value.
220          * With this change we can cleanup some code in signal.c- TODO
221          */
222         fp->orig_pc = fp->retx;
223         /* printk("exception: 0x%x, ipend=%x, reti=%x, retx=%x\n",
224                 trapnr, fp->ipend, fp->pc, fp->retx); */
225
226         /* send the appropriate signal to the user program */
227         switch (trapnr) {
228
229         /* This table works in conjuction with the one in ./mach-common/entry.S
230          * Some exceptions are handled there (in assembly, in exception space)
231          * Some are handled here, (in C, in interrupt space)
232          * Some, like CPLB, are handled in both, where the normal path is
233          * handled in assembly/exception space, and the error path is handled
234          * here
235          */
236
237         /* 0x00 - Linux Syscall, getting here is an error */
238         /* 0x01 - userspace gdb breakpoint, handled here */
239         case VEC_EXCPT01:
240                 info.si_code = TRAP_ILLTRAP;
241                 sig = SIGTRAP;
242                 CHK_DEBUGGER_TRAP_MAYBE();
243                 /* Check if this is a breakpoint in kernel space */
244                 if (fp->ipend & 0xffc0)
245                         return;
246                 else
247                         break;
248 #ifdef CONFIG_KGDB
249         case VEC_EXCPT02 :               /* gdb connection */
250                 info.si_code = TRAP_ILLTRAP;
251                 sig = SIGTRAP;
252                 CHK_DEBUGGER_TRAP();
253                 return;
254 #else
255         /* 0x02 - User Defined, Caught by default */
256 #endif
257         /* 0x03 - User Defined, userspace stack overflow */
258         case VEC_EXCPT03:
259                 info.si_code = SEGV_STACKFLOW;
260                 sig = SIGSEGV;
261                 printk(KERN_NOTICE EXC_0x03(KERN_NOTICE));
262                 CHK_DEBUGGER_TRAP();
263                 break;
264         /* 0x04 - User Defined, Caught by default */
265         /* 0x05 - User Defined, Caught by default */
266         /* 0x06 - User Defined, Caught by default */
267         /* 0x07 - User Defined, Caught by default */
268         /* 0x08 - User Defined, Caught by default */
269         /* 0x09 - User Defined, Caught by default */
270         /* 0x0A - User Defined, Caught by default */
271         /* 0x0B - User Defined, Caught by default */
272         /* 0x0C - User Defined, Caught by default */
273         /* 0x0D - User Defined, Caught by default */
274         /* 0x0E - User Defined, Caught by default */
275         /* 0x0F - User Defined, Caught by default */
276         /* 0x10 HW Single step, handled here */
277         case VEC_STEP:
278                 info.si_code = TRAP_STEP;
279                 sig = SIGTRAP;
280                 CHK_DEBUGGER_TRAP_MAYBE();
281                 /* Check if this is a single step in kernel space */
282                 if (fp->ipend & 0xffc0)
283                         return;
284                 else
285                         break;
286         /* 0x11 - Trace Buffer Full, handled here */
287         case VEC_OVFLOW:
288                 info.si_code = TRAP_TRACEFLOW;
289                 sig = SIGTRAP;
290                 printk(KERN_NOTICE EXC_0x11(KERN_NOTICE));
291                 CHK_DEBUGGER_TRAP();
292                 break;
293         /* 0x12 - Reserved, Caught by default */
294         /* 0x13 - Reserved, Caught by default */
295         /* 0x14 - Reserved, Caught by default */
296         /* 0x15 - Reserved, Caught by default */
297         /* 0x16 - Reserved, Caught by default */
298         /* 0x17 - Reserved, Caught by default */
299         /* 0x18 - Reserved, Caught by default */
300         /* 0x19 - Reserved, Caught by default */
301         /* 0x1A - Reserved, Caught by default */
302         /* 0x1B - Reserved, Caught by default */
303         /* 0x1C - Reserved, Caught by default */
304         /* 0x1D - Reserved, Caught by default */
305         /* 0x1E - Reserved, Caught by default */
306         /* 0x1F - Reserved, Caught by default */
307         /* 0x20 - Reserved, Caught by default */
308         /* 0x21 - Undefined Instruction, handled here */
309         case VEC_UNDEF_I:
310                 info.si_code = ILL_ILLOPC;
311                 sig = SIGILL;
312                 printk(KERN_NOTICE EXC_0x21(KERN_NOTICE));
313                 CHK_DEBUGGER_TRAP();
314                 break;
315         /* 0x22 - Illegal Instruction Combination, handled here */
316         case VEC_ILGAL_I:
317                 info.si_code = ILL_ILLPARAOP;
318                 sig = SIGILL;
319                 printk(KERN_NOTICE EXC_0x22(KERN_NOTICE));
320                 CHK_DEBUGGER_TRAP();
321                 break;
322         /* 0x23 - Data CPLB protection violation, handled here */
323         case VEC_CPLB_VL:
324                 info.si_code = ILL_CPLB_VI;
325                 sig = SIGBUS;
326                 printk(KERN_NOTICE EXC_0x23(KERN_NOTICE));
327                 CHK_DEBUGGER_TRAP();
328                 break;
329         /* 0x24 - Data access misaligned, handled here */
330         case VEC_MISALI_D:
331                 info.si_code = BUS_ADRALN;
332                 sig = SIGBUS;
333                 printk(KERN_NOTICE EXC_0x24(KERN_NOTICE));
334                 CHK_DEBUGGER_TRAP();
335                 break;
336         /* 0x25 - Unrecoverable Event, handled here */
337         case VEC_UNCOV:
338                 info.si_code = ILL_ILLEXCPT;
339                 sig = SIGILL;
340                 printk(KERN_NOTICE EXC_0x25(KERN_NOTICE));
341                 CHK_DEBUGGER_TRAP();
342                 break;
343         /* 0x26 - Data CPLB Miss, normal case is handled in _cplb_hdr,
344                 error case is handled here */
345         case VEC_CPLB_M:
346                 info.si_code = BUS_ADRALN;
347                 sig = SIGBUS;
348                 printk(KERN_NOTICE EXC_0x26(KERN_NOTICE));
349                 CHK_DEBUGGER_TRAP();
350                 break;
351         /* 0x27 - Data CPLB Multiple Hits - Linux Trap Zero, handled here */
352         case VEC_CPLB_MHIT:
353                 info.si_code = ILL_CPLB_MULHIT;
354 #ifdef CONFIG_DEBUG_HUNT_FOR_ZERO
355                 sig = SIGSEGV;
356                 printk(KERN_NOTICE "NULL pointer access (probably)\n");
357 #else
358                 sig = SIGILL;
359                 printk(KERN_NOTICE EXC_0x27(KERN_NOTICE));
360 #endif
361                 CHK_DEBUGGER_TRAP();
362                 break;
363         /* 0x28 - Emulation Watchpoint, handled here */
364         case VEC_WATCH:
365                 info.si_code = TRAP_WATCHPT;
366                 sig = SIGTRAP;
367                 pr_debug(EXC_0x28(KERN_DEBUG));
368                 CHK_DEBUGGER_TRAP_MAYBE();
369                 /* Check if this is a watchpoint in kernel space */
370                 if (fp->ipend & 0xffc0)
371                         return;
372                 else
373                         break;
374 #ifdef CONFIG_BF535
375         /* 0x29 - Instruction fetch access error (535 only) */
376         case VEC_ISTRU_VL:      /* ADSP-BF535 only (MH) */
377                 info.si_code = BUS_OPFETCH;
378                 sig = SIGBUS;
379                 printk(KERN_NOTICE "BF535: VEC_ISTRU_VL\n");
380                 CHK_DEBUGGER_TRAP();
381                 break;
382 #else
383         /* 0x29 - Reserved, Caught by default */
384 #endif
385         /* 0x2A - Instruction fetch misaligned, handled here */
386         case VEC_MISALI_I:
387                 info.si_code = BUS_ADRALN;
388                 sig = SIGBUS;
389                 printk(KERN_NOTICE EXC_0x2A(KERN_NOTICE));
390                 CHK_DEBUGGER_TRAP();
391                 break;
392         /* 0x2B - Instruction CPLB protection violation, handled here */
393         case VEC_CPLB_I_VL:
394                 info.si_code = ILL_CPLB_VI;
395                 sig = SIGBUS;
396                 printk(KERN_NOTICE EXC_0x2B(KERN_NOTICE));
397                 CHK_DEBUGGER_TRAP();
398                 break;
399         /* 0x2C - Instruction CPLB miss, handled in _cplb_hdr */
400         case VEC_CPLB_I_M:
401                 info.si_code = ILL_CPLB_MISS;
402                 sig = SIGBUS;
403                 printk(KERN_NOTICE EXC_0x2C(KERN_NOTICE));
404                 CHK_DEBUGGER_TRAP();
405                 break;
406         /* 0x2D - Instruction CPLB Multiple Hits, handled here */
407         case VEC_CPLB_I_MHIT:
408                 info.si_code = ILL_CPLB_MULHIT;
409 #ifdef CONFIG_DEBUG_HUNT_FOR_ZERO
410                 sig = SIGSEGV;
411                 printk(KERN_NOTICE "Jump to address 0 - 0x0fff\n");
412 #else
413                 sig = SIGILL;
414                 printk(KERN_NOTICE EXC_0x2D(KERN_NOTICE));
415 #endif
416                 CHK_DEBUGGER_TRAP();
417                 break;
418         /* 0x2E - Illegal use of Supervisor Resource, handled here */
419         case VEC_ILL_RES:
420                 info.si_code = ILL_PRVOPC;
421                 sig = SIGILL;
422                 printk(KERN_NOTICE EXC_0x2E(KERN_NOTICE));
423                 CHK_DEBUGGER_TRAP();
424                 break;
425         /* 0x2F - Reserved, Caught by default */
426         /* 0x30 - Reserved, Caught by default */
427         /* 0x31 - Reserved, Caught by default */
428         /* 0x32 - Reserved, Caught by default */
429         /* 0x33 - Reserved, Caught by default */
430         /* 0x34 - Reserved, Caught by default */
431         /* 0x35 - Reserved, Caught by default */
432         /* 0x36 - Reserved, Caught by default */
433         /* 0x37 - Reserved, Caught by default */
434         /* 0x38 - Reserved, Caught by default */
435         /* 0x39 - Reserved, Caught by default */
436         /* 0x3A - Reserved, Caught by default */
437         /* 0x3B - Reserved, Caught by default */
438         /* 0x3C - Reserved, Caught by default */
439         /* 0x3D - Reserved, Caught by default */
440         /* 0x3E - Reserved, Caught by default */
441         /* 0x3F - Reserved, Caught by default */
442         case VEC_HWERR:
443                 info.si_code = BUS_ADRALN;
444                 sig = SIGBUS;
445                 switch (fp->seqstat & SEQSTAT_HWERRCAUSE) {
446                 /* System MMR Error */
447                 case (SEQSTAT_HWERRCAUSE_SYSTEM_MMR):
448                         info.si_code = BUS_ADRALN;
449                         sig = SIGBUS;
450                         printk(KERN_NOTICE HWC_x2(KERN_NOTICE));
451                         break;
452                 /* External Memory Addressing Error */
453                 case (SEQSTAT_HWERRCAUSE_EXTERN_ADDR):
454                         info.si_code = BUS_ADRERR;
455                         sig = SIGBUS;
456                         printk(KERN_NOTICE HWC_x3(KERN_NOTICE));
457                         break;
458                 /* Performance Monitor Overflow */
459                 case (SEQSTAT_HWERRCAUSE_PERF_FLOW):
460                         printk(KERN_NOTICE HWC_x12(KERN_NOTICE));
461                         break;
462                 /* RAISE 5 instruction */
463                 case (SEQSTAT_HWERRCAUSE_RAISE_5):
464                         printk(KERN_NOTICE HWC_x18(KERN_NOTICE));
465                         break;
466                 default:        /* Reserved */
467                         printk(KERN_NOTICE HWC_default(KERN_NOTICE));
468                         break;
469                 }
470                 CHK_DEBUGGER_TRAP();
471                 break;
472         default:
473                 info.si_code = TRAP_ILLTRAP;
474                 sig = SIGTRAP;
475                 printk(KERN_EMERG "Caught Unhandled Exception, code = %08lx\n",
476                         (fp->seqstat & SEQSTAT_EXCAUSE));
477                 CHK_DEBUGGER_TRAP();
478                 break;
479         }
480
481         BUG_ON(sig == 0);
482
483         if (sig != SIGTRAP) {
484                 unsigned long stack;
485                 dump_bfin_process(fp);
486                 dump_bfin_mem(fp);
487                 show_regs(fp);
488
489                 /* Print out the trace buffer if it makes sense */
490 #ifndef CONFIG_DEBUG_BFIN_NO_KERN_HWTRACE
491                 if (trapnr == VEC_CPLB_I_M || trapnr == VEC_CPLB_M)
492                         printk(KERN_NOTICE "No trace since you do not have "
493                                 "CONFIG_DEBUG_BFIN_NO_KERN_HWTRACE enabled\n"
494                                 KERN_NOTICE "\n");
495                 else
496 #endif
497                         dump_bfin_trace_buffer();
498                 show_stack(current, &stack);
499                 if (oops_in_progress) {
500 #ifndef CONFIG_ACCESS_CHECK
501                         printk(KERN_EMERG "Please turn on "
502                                "CONFIG_ACCESS_CHECK\n");
503 #endif
504                         panic("Kernel exception");
505                 }
506         }
507
508         info.si_signo = sig;
509         info.si_errno = 0;
510         info.si_addr = (void *)fp->pc;
511         force_sig_info(sig, &info, current);
512
513         trace_buffer_restore(j);
514         return;
515 }
516
517 /* Typical exception handling routines  */
518
519 #define EXPAND_LEN ((1 << CONFIG_DEBUG_BFIN_HWTRACE_EXPAND_LEN) * 256 - 1)
520
521 void dump_bfin_trace_buffer(void)
522 {
523 #ifdef CONFIG_DEBUG_BFIN_HWTRACE_ON
524         int tflags, i = 0;
525         char buf[150];
526 #ifdef CONFIG_DEBUG_BFIN_HWTRACE_EXPAND
527         int j, index;
528 #endif
529
530         trace_buffer_save(tflags);
531
532         printk(KERN_NOTICE "Hardware Trace:\n");
533
534         if (likely(bfin_read_TBUFSTAT() & TBUFCNT)) {
535                 for (; bfin_read_TBUFSTAT() & TBUFCNT; i++) {
536                         decode_address(buf, (unsigned long)bfin_read_TBUF());
537                         printk(KERN_NOTICE "%4i Target : %s\n", i, buf);
538                         decode_address(buf, (unsigned long)bfin_read_TBUF());
539                         printk(KERN_NOTICE "     Source : %s\n", buf);
540                 }
541         }
542
543 #ifdef CONFIG_DEBUG_BFIN_HWTRACE_EXPAND
544         if (trace_buff_offset)
545                 index = trace_buff_offset/4 - 1;
546         else
547                 index = EXPAND_LEN;
548
549         j = (1 << CONFIG_DEBUG_BFIN_HWTRACE_EXPAND_LEN) * 128;
550         while (j) {
551                 decode_address(buf, software_trace_buff[index]);
552                 printk(KERN_NOTICE "%4i Target : %s\n", i, buf);
553                 index -= 1;
554                 if (index < 0 )
555                         index = EXPAND_LEN;
556                 decode_address(buf, software_trace_buff[index]);
557                 printk(KERN_NOTICE "     Source : %s\n", buf);
558                 index -= 1;
559                 if (index < 0)
560                         index = EXPAND_LEN;
561                 j--;
562                 i++;
563         }
564 #endif
565
566         trace_buffer_restore(tflags);
567 #endif
568 }
569 EXPORT_SYMBOL(dump_bfin_trace_buffer);
570
571 static void show_trace(struct task_struct *tsk, unsigned long *sp)
572 {
573         unsigned long addr;
574
575         printk(KERN_NOTICE "\n" KERN_NOTICE "Call Trace:\n");
576
577         while (!kstack_end(sp)) {
578                 addr = *sp++;
579                 /*
580                  * If the address is either in the text segment of the
581                  * kernel, or in the region which contains vmalloc'ed
582                  * memory, it *may* be the address of a calling
583                  * routine; if so, print it so that someone tracing
584                  * down the cause of the crash will be able to figure
585                  * out the call path that was taken.
586                  */
587                 if (kernel_text_address(addr))
588                         print_ip_sym(addr);
589         }
590
591         printk(KERN_NOTICE "\n");
592 }
593
594 void show_stack(struct task_struct *task, unsigned long *stack)
595 {
596         unsigned long *endstack, addr;
597         int i;
598
599         /* Cannot call dump_bfin_trace_buffer() here as show_stack() is
600          * called externally in some places in the kernel.
601          */
602
603         if (!stack) {
604                 if (task)
605                         stack = (unsigned long *)task->thread.ksp;
606                 else
607                         stack = (unsigned long *)&stack;
608         }
609
610         addr = (unsigned long)stack;
611         endstack = (unsigned long *)PAGE_ALIGN(addr);
612
613         printk(KERN_NOTICE "Stack from %08lx:", (unsigned long)stack);
614         for (i = 0; i < kstack_depth_to_print; i++) {
615                 if (stack + 1 > endstack)
616                         break;
617                 if (i % 8 == 0)
618                         printk("\n" KERN_NOTICE "       ");
619                 printk(" %08lx", *stack++);
620         }
621         printk("\n");
622
623         show_trace(task, stack);
624 }
625
626 void dump_stack(void)
627 {
628         unsigned long stack;
629 #ifdef CONFIG_DEBUG_BFIN_HWTRACE_ON
630         int tflags;
631 #endif
632         trace_buffer_save(tflags);
633         dump_bfin_trace_buffer();
634         show_stack(current, &stack);
635         trace_buffer_restore(tflags);
636 }
637 EXPORT_SYMBOL(dump_stack);
638
639 void dump_bfin_process(struct pt_regs *fp)
640 {
641         /* We should be able to look at fp->ipend, but we don't push it on the
642          * stack all the time, so do this until we fix that */
643         unsigned int context = bfin_read_IPEND();
644
645         if (oops_in_progress)
646                 printk(KERN_EMERG "Kernel OOPS in progress\n");
647
648         if (context & 0x0020 && (fp->seqstat & SEQSTAT_EXCAUSE) == VEC_HWERR)
649                 printk(KERN_NOTICE "HW Error context\n");
650         else if (context & 0x0020)
651                 printk(KERN_NOTICE "Defered Exception context\n");
652         else if (context & 0x3FC0)
653                 printk(KERN_NOTICE "Interrupt context\n");
654         else if (context & 0x4000)
655                 printk(KERN_NOTICE "Deferred Interrupt context\n");
656         else if (context & 0x8000)
657                 printk(KERN_NOTICE "Kernel process context\n");
658
659         if (current->pid && current->mm) {
660                 printk(KERN_NOTICE "CURRENT PROCESS:\n");
661                 printk(KERN_NOTICE "COMM=%s PID=%d\n",
662                         current->comm, current->pid);
663
664                 printk(KERN_NOTICE "TEXT = 0x%p-0x%p  DATA = 0x%p-0x%p\n"
665                         KERN_NOTICE "BSS = 0x%p-0x%p   USER-STACK = 0x%p\n"
666                         KERN_NOTICE "\n",
667                         (void *)current->mm->start_code,
668                         (void *)current->mm->end_code,
669                         (void *)current->mm->start_data,
670                         (void *)current->mm->end_data,
671                         (void *)current->mm->end_data,
672                         (void *)current->mm->brk,
673                         (void *)current->mm->start_stack);
674         } else
675                 printk(KERN_NOTICE "\n" KERN_NOTICE
676                      "No Valid process in current context\n");
677 }
678
679 void dump_bfin_mem(struct pt_regs *fp)
680 {
681         unsigned short *addr, *erraddr, val = 0, err = 0;
682         char sti = 0, buf[6];
683
684         if (unlikely((fp->seqstat & SEQSTAT_EXCAUSE) == VEC_HWERR))
685                 erraddr = (void *)fp->pc;
686         else
687                 erraddr = (void *)fp->retx;
688
689         printk(KERN_NOTICE "return address: [0x%p]; contents of:", erraddr);
690
691         for (addr = (unsigned short *)((unsigned long)erraddr & ~0xF) - 0x10;
692              addr < (unsigned short *)((unsigned long)erraddr & ~0xF) + 0x10;
693              addr++) {
694                 if (!((unsigned long)addr & 0xF))
695                         printk("\n" KERN_NOTICE "0x%p: ", addr);
696
697                 if (get_user(val, addr)) {
698                         if (addr >= (unsigned short *)L1_CODE_START &&
699                             addr < (unsigned short *)(L1_CODE_START + L1_CODE_LENGTH)) {
700                                 dma_memcpy(&val, addr, sizeof(val));
701                                 sprintf(buf, "%04x", val);
702                         } else if (addr >= (unsigned short *)FIXED_CODE_START &&
703                                 addr <= (unsigned short *)memory_start) {
704                                 val = bfin_read16(addr);
705                                 sprintf(buf, "%04x", val);
706                         } else {
707                                 val = 0;
708                                 sprintf(buf, "????");
709                         }
710                 } else
711                         sprintf(buf, "%04x", val);
712
713                 if (addr == erraddr) {
714                         printk("[%s]", buf);
715                         err = val;
716                 } else
717                         printk(" %s ", buf);
718
719                 /* Do any previous instructions turn on interrupts? */
720                 if (addr <= erraddr &&                          /* in the past */
721                     ((val >= 0x0040 && val <= 0x0047) ||        /* STI instruction */
722                       val == 0x017b))                           /* [SP++] = RETI */
723                         sti = 1;
724         }
725
726         printk("\n");
727
728         /* Hardware error interrupts can be deferred */
729         if (unlikely(sti && (fp->seqstat & SEQSTAT_EXCAUSE) == VEC_HWERR &&
730             oops_in_progress)){
731                 printk(KERN_NOTICE "Looks like this was a deferred error - sorry\n");
732 #ifndef CONFIG_DEBUG_HWERR
733                 printk(KERN_NOTICE "The remaining message may be meaningless\n"
734                         KERN_NOTICE "You should enable CONFIG_DEBUG_HWERR to get a"
735                          " better idea where it came from\n");
736 #else
737                 /* If we are handling only one peripheral interrupt
738                  * and current mm and pid are valid, and the last error
739                  * was in that user space process's text area
740                  * print it out - because that is where the problem exists
741                  */
742                 if ((!(((fp)->ipend & ~0x30) & (((fp)->ipend & ~0x30) - 1))) &&
743                      (current->pid && current->mm)) {
744                         /* And the last RETI points to the current userspace context */
745                         if ((fp + 1)->pc >= current->mm->start_code &&
746                             (fp + 1)->pc <= current->mm->end_code) {
747                                 printk(KERN_NOTICE "It might be better to look around here : \n");
748                                 printk(KERN_NOTICE "-------------------------------------------\n");
749                                 show_regs(fp + 1);
750                                 printk(KERN_NOTICE "-------------------------------------------\n");
751                         }
752                 }
753 #endif
754         }
755 }
756
757 void show_regs(struct pt_regs *fp)
758 {
759         char buf [150];
760         struct irqaction *action;
761         unsigned int i;
762         unsigned long flags;
763
764         printk(KERN_NOTICE "\n" KERN_NOTICE "SEQUENCER STATUS:\n");
765         printk(KERN_NOTICE " SEQSTAT: %08lx  IPEND: %04lx  SYSCFG: %04lx\n",
766                 (long)fp->seqstat, fp->ipend, fp->syscfg);
767         printk(KERN_NOTICE "  HWERRCAUSE: 0x%lx\n",
768                 (fp->seqstat & SEQSTAT_HWERRCAUSE) >> 14);
769         printk(KERN_NOTICE "  EXCAUSE   : 0x%lx\n",
770                 fp->seqstat & SEQSTAT_EXCAUSE);
771         for (i = 6; i <= 15 ; i++) {
772                 if (fp->ipend & (1 << i)) {
773                         decode_address(buf, bfin_read32(EVT0 + 4*i));
774                         printk(KERN_NOTICE "  physical IVG%i asserted : %s\n", i, buf);
775                 }
776         }
777
778         /* if no interrupts are going off, don't print this out */
779         if (fp->ipend & ~0x3F) {
780                 for (i = 0; i < (NR_IRQS - 1); i++) {
781                         spin_lock_irqsave(&irq_desc[i].lock, flags);
782                         action = irq_desc[i].action;
783                         if (!action)
784                                 goto unlock;
785
786                         decode_address(buf, (unsigned int)action->handler);
787                         printk(KERN_NOTICE "  logical irq %3d mapped  : %s", i, buf);
788                         for (action = action->next; action; action = action->next) {
789                                 decode_address(buf, (unsigned int)action->handler);
790                                 printk(", %s", buf);
791                         }
792                         printk("\n");
793 unlock:
794                         spin_unlock_irqrestore(&irq_desc[i].lock, flags);
795                 }
796         }
797
798         decode_address(buf, fp->rete);
799         printk(KERN_NOTICE " RETE: %s\n", buf);
800         decode_address(buf, fp->retn);
801         printk(KERN_NOTICE " RETN: %s\n", buf);
802         decode_address(buf, fp->retx);
803         printk(KERN_NOTICE " RETX: %s\n", buf);
804         decode_address(buf, fp->rets);
805         printk(KERN_NOTICE " RETS: %s\n", buf);
806         decode_address(buf, fp->pc);
807         printk(KERN_NOTICE " PC  : %s\n", buf);
808
809         if (((long)fp->seqstat &  SEQSTAT_EXCAUSE) &&
810             (((long)fp->seqstat & SEQSTAT_EXCAUSE) != VEC_HWERR)) {
811                 decode_address(buf, bfin_read_DCPLB_FAULT_ADDR());
812                 printk(KERN_NOTICE "DCPLB_FAULT_ADDR: %s\n", buf);
813                 decode_address(buf, bfin_read_ICPLB_FAULT_ADDR());
814                 printk(KERN_NOTICE "ICPLB_FAULT_ADDR: %s\n", buf);
815         }
816
817         printk(KERN_NOTICE "\n" KERN_NOTICE "PROCESSOR STATE:\n");
818         printk(KERN_NOTICE " R0 : %08lx    R1 : %08lx    R2 : %08lx    R3 : %08lx\n",
819                 fp->r0, fp->r1, fp->r2, fp->r3);
820         printk(KERN_NOTICE " R4 : %08lx    R5 : %08lx    R6 : %08lx    R7 : %08lx\n",
821                 fp->r4, fp->r5, fp->r6, fp->r7);
822         printk(KERN_NOTICE " P0 : %08lx    P1 : %08lx    P2 : %08lx    P3 : %08lx\n",
823                 fp->p0, fp->p1, fp->p2, fp->p3);
824         printk(KERN_NOTICE " P4 : %08lx    P5 : %08lx    FP : %08lx    SP : %08lx\n",
825                 fp->p4, fp->p5, fp->fp, (long)fp);
826         printk(KERN_NOTICE " LB0: %08lx    LT0: %08lx    LC0: %08lx\n",
827                 fp->lb0, fp->lt0, fp->lc0);
828         printk(KERN_NOTICE " LB1: %08lx    LT1: %08lx    LC1: %08lx\n",
829                 fp->lb1, fp->lt1, fp->lc1);
830         printk(KERN_NOTICE " B0 : %08lx    L0 : %08lx    M0 : %08lx    I0 : %08lx\n",
831                 fp->b0, fp->l0, fp->m0, fp->i0);
832         printk(KERN_NOTICE " B1 : %08lx    L1 : %08lx    M1 : %08lx    I1 : %08lx\n",
833                 fp->b1, fp->l1, fp->m1, fp->i1);
834         printk(KERN_NOTICE " B2 : %08lx    L2 : %08lx    M2 : %08lx    I2 : %08lx\n",
835                 fp->b2, fp->l2, fp->m2, fp->i2);
836         printk(KERN_NOTICE " B3 : %08lx    L3 : %08lx    M3 : %08lx    I3 : %08lx\n",
837                 fp->b3, fp->l3, fp->m3, fp->i3);
838         printk(KERN_NOTICE "A0.w: %08lx   A0.x: %08lx   A1.w: %08lx   A1.x: %08lx\n",
839                 fp->a0w, fp->a0x, fp->a1w, fp->a1x);
840
841         printk(KERN_NOTICE "USP : %08lx  ASTAT: %08lx\n",
842                 rdusp(), fp->astat);
843
844         printk(KERN_NOTICE "\n");
845 }
846
847 #ifdef CONFIG_SYS_BFIN_SPINLOCK_L1
848 asmlinkage int sys_bfin_spinlock(int *spinlock)__attribute__((l1_text));
849 #endif
850
851 asmlinkage int sys_bfin_spinlock(int *spinlock)
852 {
853         int ret = 0;
854         int tmp = 0;
855
856         local_irq_disable();
857         ret = get_user(tmp, spinlock);
858         if (ret == 0) {
859                 if (tmp)
860                         ret = 1;
861                 tmp = 1;
862                 put_user(tmp, spinlock);
863         }
864         local_irq_enable();
865         return ret;
866 }
867
868 int bfin_request_exception(unsigned int exception, void (*handler)(void))
869 {
870         void (*curr_handler)(void);
871
872         if (exception > 0x3F)
873                 return -EINVAL;
874
875         curr_handler = ex_table[exception];
876
877         if (curr_handler != ex_replaceable)
878                 return -EBUSY;
879
880         ex_table[exception] = handler;
881
882         return 0;
883 }
884 EXPORT_SYMBOL(bfin_request_exception);
885
886 int bfin_free_exception(unsigned int exception, void (*handler)(void))
887 {
888         void (*curr_handler)(void);
889
890         if (exception > 0x3F)
891                 return -EINVAL;
892
893         curr_handler = ex_table[exception];
894
895         if (curr_handler != handler)
896                 return -EBUSY;
897
898         ex_table[exception] = ex_replaceable;
899
900         return 0;
901 }
902 EXPORT_SYMBOL(bfin_free_exception);
903
904 void panic_cplb_error(int cplb_panic, struct pt_regs *fp)
905 {
906         switch (cplb_panic) {
907         case CPLB_NO_UNLOCKED:
908                 printk(KERN_EMERG "All CPLBs are locked\n");
909                 break;
910         case CPLB_PROT_VIOL:
911                 return;
912         case CPLB_NO_ADDR_MATCH:
913                 return;
914         case CPLB_UNKNOWN_ERR:
915                 printk(KERN_EMERG "Unknown CPLB Exception\n");
916                 break;
917         }
918
919         oops_in_progress = 1;
920
921         printk(KERN_EMERG "DCPLB_FAULT_ADDR=%p\n", (void *)bfin_read_DCPLB_FAULT_ADDR());
922         printk(KERN_EMERG "ICPLB_FAULT_ADDR=%p\n", (void *)bfin_read_ICPLB_FAULT_ADDR());
923         dump_bfin_process(fp);
924         dump_bfin_mem(fp);
925         show_regs(fp);
926         dump_stack();
927         panic("Unrecoverable event\n");
928 }