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