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