2 * File: arch/blackfin/kernel/traps.c
4 * Author: Hamish Macdonald
7 * Description: uses S/W interrupt 15 for the system calls
10 * Copyright 2004-2006 Analog Devices Inc.
12 * Bugs: Enter bugs at http://blackfin.uclinux.org/
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.
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.
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
30 #include <asm/uaccess.h>
31 #include <asm/traps.h>
32 #include <asm/cacheflush.h>
33 #include <asm/blackfin.h>
34 #include <asm/uaccess.h>
35 #include <asm/irq_handler.h>
36 #include <linux/interrupt.h>
37 #include <linux/module.h>
38 #include <linux/kallsyms.h>
41 # include <linux/debugger.h>
42 # include <linux/kgdb.h>
45 /* Initiate the event table handler */
46 void __init trap_init(void)
49 bfin_write_EVT3(trap);
53 asmlinkage void trap_c(struct pt_regs *fp);
55 int kstack_depth_to_print = 48;
57 static int printk_address(unsigned long address)
59 struct vm_list_struct *vml;
60 struct task_struct *p;
64 #ifdef CONFIG_KALLSYMS
65 unsigned long symsize;
71 /* look up the address and see if we are in kernel space */
72 symname = kallsyms_lookup(address, &symsize, &offset, &modname, namebuf);
75 /* yeah! kernel space! */
78 return printk("<0x%p> { %s%s%s%s + 0x%lx }",
79 (void*)address, delim, modname, delim, symname,
80 (unsigned long)offset);
85 /* looks like we're off in user-land, so let's walk all the
86 * mappings of all our processes and see if we can't be a whee
89 write_lock_irq(&tasklist_lock);
95 vml = mm->context.vmlist;
97 struct vm_area_struct *vma = vml->vma;
99 if (address >= vma->vm_start && address < vma->vm_end) {
100 char *name = p->comm;
101 struct file *file = vma->vm_file;
104 name = d_path(file->f_dentry,
110 /* FLAT does not have its text aligned to the start of
111 * the map while FDPIC ELF does ...
114 (address > current->mm->start_code) &&
115 (address < current->mm->end_code))
116 offset = address - current->mm->start_code;
118 offset = (address - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
120 write_unlock_irq(&tasklist_lock);
121 return printk("<0x%p> [ %s + 0x%lx ]",
122 (void*)address, name, offset);
128 write_unlock_irq(&tasklist_lock);
130 /* we were unable to find this address anywhere */
131 return printk("[<0x%p>]", (void*)address);
134 #define trace_buffer_save(x) \
136 (x) = bfin_read_TBUFCTL(); \
137 bfin_write_TBUFCTL((x) & ~TBUFEN); \
139 #define trace_buffer_restore(x) \
141 bfin_write_TBUFCTL((x)); \
144 asmlinkage void trap_c(struct pt_regs *fp)
148 unsigned long trapnr = fp->seqstat & SEQSTAT_EXCAUSE;
151 # define CHK_DEBUGGER_TRAP() do { CHK_DEBUGGER(trapnr, sig, info.si_code, fp,); } while (0)
152 # define CHK_DEBUGGER_TRAP_MAYBE() do { if (kgdb_connected) CHK_DEBUGGER_TRAP(); } while (0)
154 # define CHK_DEBUGGER_TRAP() do { } while (0)
155 # define CHK_DEBUGGER_TRAP_MAYBE() do { } while (0)
158 trace_buffer_save(j);
160 /* trap_c() will be called for exceptions. During exceptions
161 * processing, the pc value should be set with retx value.
162 * With this change we can cleanup some code in signal.c- TODO
164 fp->orig_pc = fp->retx;
165 /* printk("exception: 0x%x, ipend=%x, reti=%x, retx=%x\n",
166 trapnr, fp->ipend, fp->pc, fp->retx); */
168 /* send the appropriate signal to the user program */
171 /* This table works in conjuction with the one in ./mach-common/entry.S
172 * Some exceptions are handled there (in assembly, in exception space)
173 * Some are handled here, (in C, in interrupt space)
174 * Some, like CPLB, are handled in both, where the normal path is
175 * handled in assembly/exception space, and the error path is handled
179 /* 0x00 - Linux Syscall, getting here is an error */
180 /* 0x01 - userspace gdb breakpoint, handled here */
182 info.si_code = TRAP_ILLTRAP;
184 CHK_DEBUGGER_TRAP_MAYBE();
185 /* Check if this is a breakpoint in kernel space */
186 if (fp->ipend & 0xffc0)
191 case VEC_EXCPT02 : /* gdb connection */
192 info.si_code = TRAP_ILLTRAP;
197 /* 0x02 - User Defined, Caught by default */
199 /* 0x03 - Atomic test and set */
201 info.si_code = SEGV_STACKFLOW;
203 printk(KERN_EMERG EXC_0x03);
206 /* 0x04 - spinlock - handled by _ex_spinlock,
207 getting here is an error */
208 /* 0x05 - User Defined, Caught by default */
209 /* 0x06 - User Defined, Caught by default */
210 /* 0x07 - User Defined, Caught by default */
211 /* 0x08 - User Defined, Caught by default */
212 /* 0x09 - User Defined, Caught by default */
213 /* 0x0A - User Defined, Caught by default */
214 /* 0x0B - User Defined, Caught by default */
215 /* 0x0C - User Defined, Caught by default */
216 /* 0x0D - User Defined, Caught by default */
217 /* 0x0E - User Defined, Caught by default */
218 /* 0x0F - User Defined, Caught by default */
219 /* 0x10 HW Single step, handled here */
221 info.si_code = TRAP_STEP;
223 CHK_DEBUGGER_TRAP_MAYBE();
224 /* Check if this is a single step in kernel space */
225 if (fp->ipend & 0xffc0)
229 /* 0x11 - Trace Buffer Full, handled here */
231 info.si_code = TRAP_TRACEFLOW;
233 printk(KERN_EMERG EXC_0x11);
236 /* 0x12 - Reserved, Caught by default */
237 /* 0x13 - Reserved, Caught by default */
238 /* 0x14 - Reserved, Caught by default */
239 /* 0x15 - Reserved, Caught by default */
240 /* 0x16 - Reserved, Caught by default */
241 /* 0x17 - Reserved, Caught by default */
242 /* 0x18 - Reserved, Caught by default */
243 /* 0x19 - Reserved, Caught by default */
244 /* 0x1A - Reserved, Caught by default */
245 /* 0x1B - Reserved, Caught by default */
246 /* 0x1C - Reserved, Caught by default */
247 /* 0x1D - Reserved, Caught by default */
248 /* 0x1E - Reserved, Caught by default */
249 /* 0x1F - Reserved, Caught by default */
250 /* 0x20 - Reserved, Caught by default */
251 /* 0x21 - Undefined Instruction, handled here */
253 info.si_code = ILL_ILLOPC;
255 printk(KERN_EMERG EXC_0x21);
258 /* 0x22 - Illegal Instruction Combination, handled here */
260 info.si_code = ILL_ILLPARAOP;
262 printk(KERN_EMERG EXC_0x22);
265 /* 0x23 - Data CPLB Protection Violation,
266 normal case is handled in _cplb_hdr */
268 info.si_code = ILL_CPLB_VI;
270 printk(KERN_EMERG EXC_0x23);
273 /* 0x24 - Data access misaligned, handled here */
275 info.si_code = BUS_ADRALN;
277 printk(KERN_EMERG EXC_0x24);
280 /* 0x25 - Unrecoverable Event, handled here */
282 info.si_code = ILL_ILLEXCPT;
284 printk(KERN_EMERG EXC_0x25);
287 /* 0x26 - Data CPLB Miss, normal case is handled in _cplb_hdr,
288 error case is handled here */
290 info.si_code = BUS_ADRALN;
292 printk(KERN_EMERG EXC_0x26);
295 /* 0x27 - Data CPLB Multiple Hits - Linux Trap Zero, handled here */
297 info.si_code = ILL_CPLB_MULHIT;
298 #ifdef CONFIG_DEBUG_HUNT_FOR_ZERO
300 printk(KERN_EMERG "\n\nNULL pointer access (probably)\n");
303 printk(KERN_EMERG EXC_0x27);
307 /* 0x28 - Emulation Watchpoint, handled here */
309 info.si_code = TRAP_WATCHPT;
312 CHK_DEBUGGER_TRAP_MAYBE();
313 /* Check if this is a watchpoint in kernel space */
314 if (fp->ipend & 0xffc0)
319 /* 0x29 - Instruction fetch access error (535 only) */
320 case VEC_ISTRU_VL: /* ADSP-BF535 only (MH) */
321 info.si_code = BUS_OPFETCH;
323 printk(KERN_EMERG "BF535: VEC_ISTRU_VL\n");
327 /* 0x29 - Reserved, Caught by default */
329 /* 0x2A - Instruction fetch misaligned, handled here */
331 info.si_code = BUS_ADRALN;
333 printk(KERN_EMERG EXC_0x2A);
336 /* 0x2B - Instruction CPLB protection Violation,
337 handled in _cplb_hdr */
339 info.si_code = ILL_CPLB_VI;
341 printk(KERN_EMERG EXC_0x2B);
344 /* 0x2C - Instruction CPLB miss, handled in _cplb_hdr */
346 info.si_code = ILL_CPLB_MISS;
348 printk(KERN_EMERG EXC_0x2C);
351 /* 0x2D - Instruction CPLB Multiple Hits, handled here */
352 case VEC_CPLB_I_MHIT:
353 info.si_code = ILL_CPLB_MULHIT;
354 #ifdef CONFIG_DEBUG_HUNT_FOR_ZERO
356 printk(KERN_EMERG "\n\nJump to address 0 - 0x0fff\n");
359 printk(KERN_EMERG EXC_0x2D);
363 /* 0x2E - Illegal use of Supervisor Resource, handled here */
365 info.si_code = ILL_PRVOPC;
367 printk(KERN_EMERG EXC_0x2E);
370 /* 0x2F - Reserved, Caught by default */
371 /* 0x30 - Reserved, Caught by default */
372 /* 0x31 - Reserved, Caught by default */
373 /* 0x32 - Reserved, Caught by default */
374 /* 0x33 - Reserved, Caught by default */
375 /* 0x34 - Reserved, Caught by default */
376 /* 0x35 - Reserved, Caught by default */
377 /* 0x36 - Reserved, Caught by default */
378 /* 0x37 - Reserved, Caught by default */
379 /* 0x38 - Reserved, Caught by default */
380 /* 0x39 - Reserved, Caught by default */
381 /* 0x3A - Reserved, Caught by default */
382 /* 0x3B - Reserved, Caught by default */
383 /* 0x3C - Reserved, Caught by default */
384 /* 0x3D - Reserved, Caught by default */
385 /* 0x3E - Reserved, Caught by default */
386 /* 0x3F - Reserved, Caught by default */
388 info.si_code = TRAP_ILLTRAP;
390 printk(KERN_EMERG "Caught Unhandled Exception, code = %08lx\n",
391 (fp->seqstat & SEQSTAT_EXCAUSE));
398 info.si_addr = (void *)fp->pc;
399 force_sig_info(sig, &info, current);
400 if (sig != 0 && sig != SIGTRAP) {
402 dump_bfin_regs(fp, (void *)fp->retx);
403 dump_bfin_trace_buffer();
404 show_stack(current, &stack);
405 if (current->mm == NULL)
406 panic("Kernel exception");
409 /* if the address that we are about to return to is not valid, set it
410 * to a valid address, if we have a current application or panic
412 if (!(fp->pc <= physical_mem_end
413 #if L1_CODE_LENGTH != 0
414 || (fp->pc >= L1_CODE_START &&
415 fp->pc <= (L1_CODE_START + L1_CODE_LENGTH))
419 fp->pc = current->mm->start_code;
421 printk(KERN_EMERG "I can't return to memory that doesn't exist - bad things happen\n");
422 panic("Help - I've fallen and can't get up\n");
426 trace_buffer_restore(j);
430 /* Typical exception handling routines */
432 void dump_bfin_trace_buffer(void)
435 trace_buffer_save(tflags);
437 if (likely(bfin_read_TBUFSTAT() & TBUFCNT)) {
439 printk(KERN_EMERG "Hardware Trace:\n");
440 for (i = 0; bfin_read_TBUFSTAT() & TBUFCNT; i++) {
441 printk(KERN_EMERG "%2i Target : ", i);
442 printk_address((unsigned long)bfin_read_TBUF());
443 printk("\n" KERN_EMERG " Source : ");
444 printk_address((unsigned long)bfin_read_TBUF());
449 trace_buffer_restore(tflags);
451 EXPORT_SYMBOL(dump_bfin_trace_buffer);
453 static void show_trace(struct task_struct *tsk, unsigned long *sp)
457 printk("\nCall Trace:");
458 #ifdef CONFIG_KALLSYMS
462 while (!kstack_end(sp)) {
465 * If the address is either in the text segment of the
466 * kernel, or in the region which contains vmalloc'ed
467 * memory, it *may* be the address of a calling
468 * routine; if so, print it so that someone tracing
469 * down the cause of the crash will be able to figure
470 * out the call path that was taken.
472 if (kernel_text_address(addr))
479 void show_stack(struct task_struct *task, unsigned long *stack)
481 unsigned long *endstack, addr;
484 /* Cannot call dump_bfin_trace_buffer() here as show_stack() is
485 * called externally in some places in the kernel.
490 stack = (unsigned long *)task->thread.ksp;
492 stack = (unsigned long *)&stack;
495 addr = (unsigned long)stack;
496 endstack = (unsigned long *)PAGE_ALIGN(addr);
498 printk(KERN_EMERG "Stack from %08lx:", (unsigned long)stack);
499 for (i = 0; i < kstack_depth_to_print; i++) {
500 if (stack + 1 > endstack)
503 printk("\n" KERN_EMERG " ");
504 printk(" %08lx", *stack++);
507 show_trace(task, stack);
510 void dump_stack(void)
514 trace_buffer_save(tflags);
515 dump_bfin_trace_buffer();
516 show_stack(current, &stack);
517 trace_buffer_restore(tflags);
520 EXPORT_SYMBOL(dump_stack);
522 void dump_bfin_regs(struct pt_regs *fp, void *retaddr)
525 printk("\nCURRENT PROCESS:\n\n");
526 printk("COMM=%s PID=%d\n", current->comm, current->pid);
529 ("\nNo Valid pid - Either things are really messed up, or you are in the kernel\n");
533 printk("TEXT = 0x%p-0x%p DATA = 0x%p-0x%p\n"
534 "BSS = 0x%p-0x%p USER-STACK = 0x%p\n\n",
535 (void*)current->mm->start_code,
536 (void*)current->mm->end_code,
537 (void*)current->mm->start_data,
538 (void*)current->mm->end_data,
539 (void*)current->mm->end_data,
540 (void*)current->mm->brk,
541 (void*)current->mm->start_stack);
544 printk("return address: 0x%p; contents of [PC-16...PC+8]:\n", retaddr);
545 if (retaddr != 0 && retaddr <= (void*)physical_mem_end
546 #if L1_CODE_LENGTH != 0
547 /* FIXME: Copy the code out of L1 Instruction SRAM through dma
549 && !(retaddr >= (void*)L1_CODE_START
550 && retaddr < (void*)(L1_CODE_START + L1_CODE_LENGTH))
554 unsigned short x = 0;
555 for (i = -16; i < 8; i++) {
556 if (get_user(x, (unsigned short *)retaddr + i))
558 #ifndef CONFIG_DEBUG_HWERR
559 /* If one of the last few instructions was a STI
560 * it is likily that the error occured awhile ago
561 * and we just noticed
563 if (x >= 0x0040 && x <= 0x0047 && i <= 0)
564 panic("\n\nWARNING : You should reconfigure the kernel to turn on\n"
565 " 'Hardware error interrupt debugging'\n"
566 " The rest of this error is meanless\n");
576 printk("Cannot look at the [PC] for it is in unreadable L1 SRAM - sorry\n");
580 printk("RETE: %08lx RETN: %08lx RETX: %08lx RETS: %08lx\n",
581 fp->rete, fp->retn, fp->retx, fp->rets);
582 printk("IPEND: %04lx SYSCFG: %04lx\n", fp->ipend, fp->syscfg);
583 printk("SEQSTAT: %08lx SP: %08lx\n", (long)fp->seqstat, (long)fp);
584 printk("R0: %08lx R1: %08lx R2: %08lx R3: %08lx\n",
585 fp->r0, fp->r1, fp->r2, fp->r3);
586 printk("R4: %08lx R5: %08lx R6: %08lx R7: %08lx\n",
587 fp->r4, fp->r5, fp->r6, fp->r7);
588 printk("P0: %08lx P1: %08lx P2: %08lx P3: %08lx\n",
589 fp->p0, fp->p1, fp->p2, fp->p3);
590 printk("P4: %08lx P5: %08lx FP: %08lx\n", fp->p4, fp->p5, fp->fp);
591 printk("A0.w: %08lx A0.x: %08lx A1.w: %08lx A1.x: %08lx\n",
592 fp->a0w, fp->a0x, fp->a1w, fp->a1x);
594 printk("LB0: %08lx LT0: %08lx LC0: %08lx\n", fp->lb0, fp->lt0,
596 printk("LB1: %08lx LT1: %08lx LC1: %08lx\n", fp->lb1, fp->lt1,
598 printk("B0: %08lx L0: %08lx M0: %08lx I0: %08lx\n", fp->b0, fp->l0,
600 printk("B1: %08lx L1: %08lx M1: %08lx I1: %08lx\n", fp->b1, fp->l1,
602 printk("B2: %08lx L2: %08lx M2: %08lx I2: %08lx\n", fp->b2, fp->l2,
604 printk("B3: %08lx L3: %08lx M3: %08lx I3: %08lx\n", fp->b3, fp->l3,
607 printk("\nUSP: %08lx ASTAT: %08lx\n", rdusp(), fp->astat);
608 if ((long)fp->seqstat & SEQSTAT_EXCAUSE) {
609 printk(KERN_EMERG "DCPLB_FAULT_ADDR=%p\n", (void*)bfin_read_DCPLB_FAULT_ADDR());
610 printk(KERN_EMERG "ICPLB_FAULT_ADDR=%p\n", (void*)bfin_read_ICPLB_FAULT_ADDR());
616 #ifdef CONFIG_SYS_BFIN_SPINLOCK_L1
617 asmlinkage int sys_bfin_spinlock(int *spinlock)__attribute__((l1_text));
620 asmlinkage int sys_bfin_spinlock(int *spinlock)
626 ret = get_user(tmp, spinlock);
631 put_user(tmp, spinlock);
637 void panic_cplb_error(int cplb_panic, struct pt_regs *fp)
639 switch (cplb_panic) {
640 case CPLB_NO_UNLOCKED:
641 printk(KERN_EMERG "All CPLBs are locked\n");
645 case CPLB_NO_ADDR_MATCH:
647 case CPLB_UNKNOWN_ERR:
648 printk(KERN_EMERG "Unknown CPLB Exception\n");
652 printk(KERN_EMERG "DCPLB_FAULT_ADDR=%p\n", (void*)bfin_read_DCPLB_FAULT_ADDR());
653 printk(KERN_EMERG "ICPLB_FAULT_ADDR=%p\n", (void*)bfin_read_ICPLB_FAULT_ADDR());
654 dump_bfin_regs(fp, (void *)fp->retx);
656 panic("Unrecoverable event\n");