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