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