Merge branch 'x86-platform-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / arch / mn10300 / kernel / traps.c
1 /* MN10300 Exception handling
2  *
3  * Copyright (C) 2007 Matsushita Electric Industrial Co., Ltd.
4  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5  * Modified by David Howells (dhowells@redhat.com)
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public Licence
9  * as published by the Free Software Foundation; either version
10  * 2 of the Licence, or (at your option) any later version.
11  */
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <linux/errno.h>
16 #include <linux/ptrace.h>
17 #include <linux/timer.h>
18 #include <linux/mm.h>
19 #include <linux/smp.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/spinlock.h>
23 #include <linux/interrupt.h>
24 #include <linux/kallsyms.h>
25 #include <linux/pci.h>
26 #include <linux/kdebug.h>
27 #include <linux/bug.h>
28 #include <linux/irq.h>
29 #include <asm/processor.h>
30 #include <asm/system.h>
31 #include <asm/uaccess.h>
32 #include <asm/io.h>
33 #include <asm/atomic.h>
34 #include <asm/smp.h>
35 #include <asm/pgalloc.h>
36 #include <asm/cacheflush.h>
37 #include <asm/cpu-regs.h>
38 #include <asm/busctl-regs.h>
39 #include <unit/leds.h>
40 #include <asm/fpu.h>
41 #include <asm/gdb-stub.h>
42 #include <asm/sections.h>
43
44 #if (CONFIG_INTERRUPT_VECTOR_BASE & 0xffffff)
45 #error "INTERRUPT_VECTOR_BASE not aligned to 16MiB boundary!"
46 #endif
47
48 int kstack_depth_to_print = 24;
49
50 spinlock_t die_lock = __SPIN_LOCK_UNLOCKED(die_lock);
51
52 ATOMIC_NOTIFIER_HEAD(mn10300_die_chain);
53
54 /*
55  * These constants are for searching for possible module text
56  * segments. MODULE_RANGE is a guess of how much space is likely
57  * to be vmalloced.
58  */
59 #define MODULE_RANGE (8 * 1024 * 1024)
60
61 #define DO_ERROR(signr, prologue, str, name)                    \
62 asmlinkage void name(struct pt_regs *regs, u32 intcode)         \
63 {                                                               \
64         prologue;                                               \
65         if (die_if_no_fixup(str, regs, intcode))                \
66                 return;                                         \
67         force_sig(signr, current);                              \
68 }
69
70 #define DO_EINFO(signr, prologue, str, name, sicode)                    \
71 asmlinkage void name(struct pt_regs *regs, u32 intcode)                 \
72 {                                                                       \
73         siginfo_t info;                                                 \
74         prologue;                                                       \
75         if (die_if_no_fixup(str, regs, intcode))                        \
76                 return;                                                 \
77         info.si_signo = signr;                                          \
78         if (signr == SIGILL && sicode == ILL_ILLOPC) {                  \
79                 uint8_t opcode;                                         \
80                 if (get_user(opcode, (uint8_t __user *)regs->pc) == 0)  \
81                         if (opcode == 0xff)                             \
82                                 info.si_signo = SIGTRAP;                \
83         }                                                               \
84         info.si_errno = 0;                                              \
85         info.si_code = sicode;                                          \
86         info.si_addr = (void *) regs->pc;                               \
87         force_sig_info(info.si_signo, &info, current);                  \
88 }
89
90 DO_ERROR(SIGTRAP, {}, "trap",                   trap);
91 DO_ERROR(SIGSEGV, {}, "ibreak",                 ibreak);
92 DO_ERROR(SIGSEGV, {}, "obreak",                 obreak);
93 DO_EINFO(SIGSEGV, {}, "access error",           access_error,   SEGV_ACCERR);
94 DO_EINFO(SIGSEGV, {}, "insn access error",      insn_acc_error, SEGV_ACCERR);
95 DO_EINFO(SIGSEGV, {}, "data access error",      data_acc_error, SEGV_ACCERR);
96 DO_EINFO(SIGILL,  {}, "privileged opcode",      priv_op,        ILL_PRVOPC);
97 DO_EINFO(SIGILL,  {}, "invalid opcode",         invalid_op,     ILL_ILLOPC);
98 DO_EINFO(SIGILL,  {}, "invalid ex opcode",      invalid_exop,   ILL_ILLOPC);
99 DO_EINFO(SIGBUS,  {}, "invalid address",        mem_error,      BUS_ADRERR);
100 DO_EINFO(SIGBUS,  {}, "bus error",              bus_error,      BUS_ADRERR);
101
102 DO_ERROR(SIGTRAP,
103 #ifndef CONFIG_MN10300_USING_JTAG
104          DCR &= ~0x0001,
105 #else
106          {},
107 #endif
108          "single step", istep);
109
110 /*
111  * handle NMI
112  */
113 asmlinkage void nmi(struct pt_regs *regs, enum exception_code code)
114 {
115         /* see if gdbstub wants to deal with it */
116 #ifdef CONFIG_GDBSTUB
117         if (gdbstub_intercept(regs, code))
118                 return;
119 #endif
120
121         printk(KERN_WARNING "--- Register Dump ---\n");
122         show_registers(regs);
123         printk(KERN_WARNING "---------------------\n");
124 }
125
126 /*
127  * show a stack trace from the specified stack pointer
128  */
129 void show_trace(unsigned long *sp)
130 {
131         unsigned long *stack, addr, module_start, module_end;
132         int i;
133
134         printk(KERN_EMERG "\nCall Trace:");
135
136         stack = sp;
137         i = 0;
138         module_start = VMALLOC_START;
139         module_end = VMALLOC_END;
140
141         while (((long) stack & (THREAD_SIZE - 1)) != 0) {
142                 addr = *stack++;
143                 if (__kernel_text_address(addr)) {
144 #if 1
145                         printk(" [<%08lx>]", addr);
146                         print_symbol(" %s", addr);
147                         printk("\n");
148 #else
149                         if ((i % 6) == 0)
150                                 printk(KERN_EMERG "  ");
151                         printk("[<%08lx>] ", addr);
152                         i++;
153 #endif
154                 }
155         }
156
157         printk("\n");
158 }
159
160 /*
161  * show the raw stack from the specified stack pointer
162  */
163 void show_stack(struct task_struct *task, unsigned long *sp)
164 {
165         unsigned long *stack;
166         int i;
167
168         if (!sp)
169                 sp = (unsigned long *) &sp;
170
171         stack = sp;
172         printk(KERN_EMERG "Stack:");
173         for (i = 0; i < kstack_depth_to_print; i++) {
174                 if (((long) stack & (THREAD_SIZE - 1)) == 0)
175                         break;
176                 if ((i % 8) == 0)
177                         printk(KERN_EMERG "  ");
178                 printk("%08lx ", *stack++);
179         }
180
181         show_trace(sp);
182 }
183
184 /*
185  * the architecture-independent dump_stack generator
186  */
187 void dump_stack(void)
188 {
189         unsigned long stack;
190
191         show_stack(current, &stack);
192 }
193 EXPORT_SYMBOL(dump_stack);
194
195 /*
196  * dump the register file in the specified exception frame
197  */
198 void show_registers_only(struct pt_regs *regs)
199 {
200         unsigned long ssp;
201
202         ssp = (unsigned long) regs + sizeof(*regs);
203
204         printk(KERN_EMERG "PC:  %08lx EPSW:  %08lx  SSP: %08lx mode: %s\n",
205                regs->pc, regs->epsw, ssp, user_mode(regs) ? "User" : "Super");
206         printk(KERN_EMERG "d0:  %08lx   d1:  %08lx   d2: %08lx   d3: %08lx\n",
207                regs->d0, regs->d1, regs->d2, regs->d3);
208         printk(KERN_EMERG "a0:  %08lx   a1:  %08lx   a2: %08lx   a3: %08lx\n",
209                regs->a0, regs->a1, regs->a2, regs->a3);
210         printk(KERN_EMERG "e0:  %08lx   e1:  %08lx   e2: %08lx   e3: %08lx\n",
211                regs->e0, regs->e1, regs->e2, regs->e3);
212         printk(KERN_EMERG "e4:  %08lx   e5:  %08lx   e6: %08lx   e7: %08lx\n",
213                regs->e4, regs->e5, regs->e6, regs->e7);
214         printk(KERN_EMERG "lar: %08lx   lir: %08lx  mdr: %08lx  usp: %08lx\n",
215                regs->lar, regs->lir, regs->mdr, regs->sp);
216         printk(KERN_EMERG "cvf: %08lx   crl: %08lx  crh: %08lx  drq: %08lx\n",
217                regs->mcvf, regs->mcrl, regs->mcrh, regs->mdrq);
218         printk(KERN_EMERG "threadinfo=%p task=%p)\n",
219                current_thread_info(), current);
220
221         if ((unsigned long) current >= PAGE_OFFSET &&
222             (unsigned long) current < (unsigned long)high_memory)
223                 printk(KERN_EMERG "Process %s (pid: %d)\n",
224                        current->comm, current->pid);
225
226 #ifdef CONFIG_SMP
227         printk(KERN_EMERG "CPUID:  %08x\n", CPUID);
228 #endif
229         printk(KERN_EMERG "CPUP:   %04hx\n", CPUP);
230         printk(KERN_EMERG "TBR:    %08x\n", TBR);
231         printk(KERN_EMERG "DEAR:   %08x\n", DEAR);
232         printk(KERN_EMERG "sISR:   %08x\n", sISR);
233         printk(KERN_EMERG "NMICR:  %04hx\n", NMICR);
234         printk(KERN_EMERG "BCBERR: %08x\n", BCBERR);
235         printk(KERN_EMERG "BCBEAR: %08x\n", BCBEAR);
236         printk(KERN_EMERG "MMUFCR: %08x\n", MMUFCR);
237         printk(KERN_EMERG "IPTEU : %08x  IPTEL2: %08x\n", IPTEU, IPTEL2);
238         printk(KERN_EMERG "DPTEU:  %08x  DPTEL2: %08x\n", DPTEU, DPTEL2);
239 }
240
241 /*
242  * dump the registers and the stack
243  */
244 void show_registers(struct pt_regs *regs)
245 {
246         unsigned long sp;
247         int i;
248
249         show_registers_only(regs);
250
251         if (!user_mode(regs))
252                 sp = (unsigned long) regs + sizeof(*regs);
253         else
254                 sp = regs->sp;
255
256         /* when in-kernel, we also print out the stack and code at the
257          * time of the fault..
258          */
259         if (!user_mode(regs)) {
260                 printk(KERN_EMERG "\n");
261                 show_stack(current, (unsigned long *) sp);
262
263 #if 0
264                 printk(KERN_EMERG "\nCode: ");
265                 if (regs->pc < PAGE_OFFSET)
266                         goto bad;
267
268                 for (i = 0; i < 20; i++) {
269                         unsigned char c;
270                         if (__get_user(c, &((unsigned char *) regs->pc)[i]))
271                                 goto bad;
272                         printk("%02x ", c);
273                 }
274 #else
275                 i = 0;
276 #endif
277         }
278
279         printk("\n");
280         return;
281
282 #if 0
283 bad:
284         printk(KERN_EMERG " Bad PC value.");
285         break;
286 #endif
287 }
288
289 /*
290  *
291  */
292 void show_trace_task(struct task_struct *tsk)
293 {
294         unsigned long sp = tsk->thread.sp;
295
296         /* User space on another CPU? */
297         if ((sp ^ (unsigned long) tsk) & (PAGE_MASK << 1))
298                 return;
299
300         show_trace((unsigned long *) sp);
301 }
302
303 /*
304  * note the untimely death of part of the kernel
305  */
306 void die(const char *str, struct pt_regs *regs, enum exception_code code)
307 {
308         console_verbose();
309         spin_lock_irq(&die_lock);
310         printk(KERN_EMERG "\n%s: %04x\n",
311                str, code & 0xffff);
312         show_registers(regs);
313
314         if (regs->pc >= 0x02000000 && regs->pc < 0x04000000 &&
315             (regs->epsw & (EPSW_IM | EPSW_IE)) != (EPSW_IM | EPSW_IE)) {
316                 printk(KERN_EMERG "Exception in usermode interrupt handler\n");
317                 printk(KERN_EMERG "\nPlease connect to kernel debugger !!\n");
318                 asm volatile ("0: bra 0b");
319         }
320
321         spin_unlock_irq(&die_lock);
322         do_exit(SIGSEGV);
323 }
324
325 /*
326  * see if there's a fixup handler we can force a jump to when an exception
327  * happens due to something kernel code did
328  */
329 int die_if_no_fixup(const char *str, struct pt_regs *regs,
330                     enum exception_code code)
331 {
332         if (user_mode(regs))
333                 return 0;
334
335         peripheral_leds_display_exception(code);
336
337         switch (code) {
338                 /* see if we can fixup the kernel accessing memory */
339         case EXCEP_ITLBMISS:
340         case EXCEP_DTLBMISS:
341         case EXCEP_IAERROR:
342         case EXCEP_DAERROR:
343         case EXCEP_MEMERR:
344         case EXCEP_MISALIGN:
345         case EXCEP_BUSERROR:
346         case EXCEP_ILLDATACC:
347         case EXCEP_IOINSACC:
348         case EXCEP_PRIVINSACC:
349         case EXCEP_PRIVDATACC:
350         case EXCEP_DATINSACC:
351                 if (fixup_exception(regs))
352                         return 1;
353         case EXCEP_UNIMPINS:
354                 if (regs->pc && *(uint8_t *)regs->pc == 0xff)
355                         if (notify_die(DIE_BREAKPOINT, str, regs, code, 0, 0))
356                                 return 1;
357                 break;
358         default:
359                 break;
360         }
361
362         /* see if gdbstub wants to deal with it */
363 #ifdef CONFIG_GDBSTUB
364         if (gdbstub_intercept(regs, code))
365                 return 1;
366 #endif
367
368         if (notify_die(DIE_GPF, str, regs, code, 0, 0))
369                 return 1;
370
371         /* make the process die as the last resort */
372         die(str, regs, code);
373 }
374
375 /*
376  * handle unsupported syscall instructions (syscall 1-15)
377  */
378 static asmlinkage void unsupported_syscall(struct pt_regs *regs,
379                                            enum exception_code code)
380 {
381         struct task_struct *tsk = current;
382         siginfo_t info;
383
384         /* catch a kernel BUG() */
385         if (code == EXCEP_SYSCALL15 && !user_mode(regs)) {
386                 if (report_bug(regs->pc, regs) == BUG_TRAP_TYPE_BUG) {
387 #ifdef CONFIG_GDBSTUB
388                         gdbstub_intercept(regs, code);
389 #endif
390                 }
391         }
392
393         regs->pc -= 2; /* syscall return addr is _after_ the instruction */
394
395         die_if_no_fixup("An unsupported syscall insn was used by the kernel\n",
396                         regs, code);
397
398         info.si_signo   = SIGILL;
399         info.si_errno   = ENOSYS;
400         info.si_code    = ILL_ILLTRP;
401         info.si_addr    = (void *) regs->pc;
402         force_sig_info(SIGILL, &info, tsk);
403 }
404
405 /*
406  * display the register file when the stack pointer gets clobbered
407  */
408 asmlinkage void do_double_fault(struct pt_regs *regs)
409 {
410         struct task_struct *tsk = current;
411
412         strcpy(tsk->comm, "emergency tsk");
413         tsk->pid = 0;
414         console_verbose();
415         printk(KERN_EMERG "--- double fault ---\n");
416         show_registers(regs);
417 }
418
419 /*
420  * asynchronous bus error (external, usually I/O DMA)
421  */
422 asmlinkage void io_bus_error(u32 bcberr, u32 bcbear, struct pt_regs *regs)
423 {
424         console_verbose();
425
426         printk(KERN_EMERG "Asynchronous I/O Bus Error\n");
427         printk(KERN_EMERG "==========================\n");
428
429         if (bcberr & BCBERR_BEME)
430                 printk(KERN_EMERG "- Multiple recorded errors\n");
431
432         printk(KERN_EMERG "- Faulting Buses:%s%s%s\n",
433                bcberr & BCBERR_BEMR_CI  ? " CPU-Ins-Fetch" : "",
434                bcberr & BCBERR_BEMR_CD  ? " CPU-Data" : "",
435                bcberr & BCBERR_BEMR_DMA ? " DMA" : "");
436
437         printk(KERN_EMERG "- %s %s access made to %s at address %08x\n",
438                bcberr & BCBERR_BEBST ? "Burst" : "Single",
439                bcberr & BCBERR_BERW ? "Read" : "Write",
440                bcberr & BCBERR_BESB_MON  ? "Monitor Space" :
441                bcberr & BCBERR_BESB_IO   ? "Internal CPU I/O Space" :
442                bcberr & BCBERR_BESB_EX   ? "External I/O Bus" :
443                bcberr & BCBERR_BESB_OPEX ? "External Memory Bus" :
444                "On Chip Memory",
445                bcbear
446                );
447
448         printk(KERN_EMERG "- Detected by the %s\n",
449                bcberr&BCBERR_BESD ? "Bus Control Unit" : "Slave Bus");
450
451 #ifdef CONFIG_PCI
452 #define BRIDGEREGB(X) (*(volatile __u8  *)(0xBE040000 + (X)))
453 #define BRIDGEREGW(X) (*(volatile __u16 *)(0xBE040000 + (X)))
454 #define BRIDGEREGL(X) (*(volatile __u32 *)(0xBE040000 + (X)))
455
456         printk(KERN_EMERG "- PCI Memory Paging Reg:         %08x\n",
457                *(volatile __u32 *) (0xBFFFFFF4));
458         printk(KERN_EMERG "- PCI Bridge Base Address 0:     %08x\n",
459                BRIDGEREGL(PCI_BASE_ADDRESS_0));
460         printk(KERN_EMERG "- PCI Bridge AMPCI Base Address: %08x\n",
461                BRIDGEREGL(0x48));
462         printk(KERN_EMERG "- PCI Bridge Command:                %04hx\n",
463                BRIDGEREGW(PCI_COMMAND));
464         printk(KERN_EMERG "- PCI Bridge Status:                 %04hx\n",
465                BRIDGEREGW(PCI_STATUS));
466         printk(KERN_EMERG "- PCI Bridge Int Status:         %08hx\n",
467                BRIDGEREGL(0x4c));
468 #endif
469
470         printk(KERN_EMERG "\n");
471         show_registers(regs);
472
473         panic("Halted due to asynchronous I/O Bus Error\n");
474 }
475
476 /*
477  * handle an exception for which a handler has not yet been installed
478  */
479 asmlinkage void uninitialised_exception(struct pt_regs *regs,
480                                         enum exception_code code)
481 {
482
483         /* see if gdbstub wants to deal with it */
484 #ifdef CONFIG_GDBSTUB
485         if (gdbstub_intercept(regs, code))
486                 return;
487 #endif
488
489         peripheral_leds_display_exception(code);
490         printk(KERN_EMERG "Uninitialised Exception 0x%04x\n", code & 0xFFFF);
491         show_registers(regs);
492
493         for (;;)
494                 continue;
495 }
496
497 /*
498  * set an interrupt stub to jump to a handler
499  * ! NOTE: this does *not* flush the caches
500  */
501 void __init __set_intr_stub(enum exception_code code, void *handler)
502 {
503         unsigned long addr;
504         u8 *vector = (u8 *)(CONFIG_INTERRUPT_VECTOR_BASE + code);
505
506         addr = (unsigned long) handler - (unsigned long) vector;
507         vector[0] = 0xdc;               /* JMP handler */
508         vector[1] = addr;
509         vector[2] = addr >> 8;
510         vector[3] = addr >> 16;
511         vector[4] = addr >> 24;
512         vector[5] = 0xcb;
513         vector[6] = 0xcb;
514         vector[7] = 0xcb;
515 }
516
517 /*
518  * set an interrupt stub to jump to a handler
519  */
520 void __init set_intr_stub(enum exception_code code, void *handler)
521 {
522         unsigned long addr;
523         u8 *vector = (u8 *)(CONFIG_INTERRUPT_VECTOR_BASE + code);
524         unsigned long flags;
525
526         addr = (unsigned long) handler - (unsigned long) vector;
527
528         flags = arch_local_cli_save();
529
530         vector[0] = 0xdc;               /* JMP handler */
531         vector[1] = addr;
532         vector[2] = addr >> 8;
533         vector[3] = addr >> 16;
534         vector[4] = addr >> 24;
535         vector[5] = 0xcb;
536         vector[6] = 0xcb;
537         vector[7] = 0xcb;
538
539         arch_local_irq_restore(flags);
540
541 #ifndef CONFIG_MN10300_CACHE_SNOOP
542         mn10300_dcache_flush_inv();
543         mn10300_icache_inv();
544 #endif
545 }
546
547 /*
548  * initialise the exception table
549  */
550 void __init trap_init(void)
551 {
552         set_excp_vector(EXCEP_TRAP,             trap);
553         set_excp_vector(EXCEP_ISTEP,            istep);
554         set_excp_vector(EXCEP_IBREAK,           ibreak);
555         set_excp_vector(EXCEP_OBREAK,           obreak);
556
557         set_excp_vector(EXCEP_PRIVINS,          priv_op);
558         set_excp_vector(EXCEP_UNIMPINS,         invalid_op);
559         set_excp_vector(EXCEP_UNIMPEXINS,       invalid_exop);
560         set_excp_vector(EXCEP_MEMERR,           mem_error);
561         set_excp_vector(EXCEP_MISALIGN,         misalignment);
562         set_excp_vector(EXCEP_BUSERROR,         bus_error);
563         set_excp_vector(EXCEP_ILLINSACC,        insn_acc_error);
564         set_excp_vector(EXCEP_ILLDATACC,        data_acc_error);
565         set_excp_vector(EXCEP_IOINSACC,         insn_acc_error);
566         set_excp_vector(EXCEP_PRIVINSACC,       insn_acc_error);
567         set_excp_vector(EXCEP_PRIVDATACC,       data_acc_error);
568         set_excp_vector(EXCEP_DATINSACC,        insn_acc_error);
569         set_excp_vector(EXCEP_FPU_UNIMPINS,     fpu_invalid_op);
570         set_excp_vector(EXCEP_FPU_OPERATION,    fpu_exception);
571
572         set_excp_vector(EXCEP_NMI,              nmi);
573
574         set_excp_vector(EXCEP_SYSCALL1,         unsupported_syscall);
575         set_excp_vector(EXCEP_SYSCALL2,         unsupported_syscall);
576         set_excp_vector(EXCEP_SYSCALL3,         unsupported_syscall);
577         set_excp_vector(EXCEP_SYSCALL4,         unsupported_syscall);
578         set_excp_vector(EXCEP_SYSCALL5,         unsupported_syscall);
579         set_excp_vector(EXCEP_SYSCALL6,         unsupported_syscall);
580         set_excp_vector(EXCEP_SYSCALL7,         unsupported_syscall);
581         set_excp_vector(EXCEP_SYSCALL8,         unsupported_syscall);
582         set_excp_vector(EXCEP_SYSCALL9,         unsupported_syscall);
583         set_excp_vector(EXCEP_SYSCALL10,        unsupported_syscall);
584         set_excp_vector(EXCEP_SYSCALL11,        unsupported_syscall);
585         set_excp_vector(EXCEP_SYSCALL12,        unsupported_syscall);
586         set_excp_vector(EXCEP_SYSCALL13,        unsupported_syscall);
587         set_excp_vector(EXCEP_SYSCALL14,        unsupported_syscall);
588         set_excp_vector(EXCEP_SYSCALL15,        unsupported_syscall);
589 }
590
591 /*
592  * determine if a program counter value is a valid bug address
593  */
594 int is_valid_bugaddr(unsigned long pc)
595 {
596         return pc >= PAGE_OFFSET;
597 }