Merge branch 'merge' of git://git.kernel.org/pub/scm/linux/kernel/git/paulus/powerpc
[linux-2.6] / arch / avr32 / kernel / traps.c
1 /*
2  * Copyright (C) 2004-2006 Atmel Corporation
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8
9 #include <linux/bug.h>
10 #include <linux/init.h>
11 #include <linux/kallsyms.h>
12 #include <linux/module.h>
13 #include <linux/notifier.h>
14 #include <linux/sched.h>
15 #include <linux/uaccess.h>
16
17 #include <asm/addrspace.h>
18 #include <asm/mmu_context.h>
19 #include <asm/ocd.h>
20 #include <asm/sysreg.h>
21 #include <asm/traps.h>
22
23 static DEFINE_SPINLOCK(die_lock);
24
25 void NORET_TYPE die(const char *str, struct pt_regs *regs, long err)
26 {
27         static int die_counter;
28
29         console_verbose();
30         spin_lock_irq(&die_lock);
31         bust_spinlocks(1);
32
33         printk(KERN_ALERT "Oops: %s, sig: %ld [#%d]\n" KERN_EMERG,
34                str, err, ++die_counter);
35 #ifdef CONFIG_PREEMPT
36         printk("PREEMPT ");
37 #endif
38 #ifdef CONFIG_FRAME_POINTER
39         printk("FRAME_POINTER ");
40 #endif
41         if (current_cpu_data.features & AVR32_FEATURE_OCD) {
42                 unsigned long did = __mfdr(DBGREG_DID);
43                 printk("chip: 0x%03lx:0x%04lx rev %lu\n",
44                        (did >> 1) & 0x7ff,
45                        (did >> 12) & 0x7fff,
46                        (did >> 28) & 0xf);
47         } else {
48                 printk("cpu: arch %u r%u / core %u r%u\n",
49                        current_cpu_data.arch_type,
50                        current_cpu_data.arch_revision,
51                        current_cpu_data.cpu_type,
52                        current_cpu_data.cpu_revision);
53         }
54
55         print_modules();
56         show_regs_log_lvl(regs, KERN_EMERG);
57         show_stack_log_lvl(current, regs->sp, regs, KERN_EMERG);
58         bust_spinlocks(0);
59         add_taint(TAINT_DIE);
60         spin_unlock_irq(&die_lock);
61
62         if (in_interrupt())
63                 panic("Fatal exception in interrupt");
64
65         if (panic_on_oops)
66                 panic("Fatal exception");
67
68         do_exit(err);
69 }
70
71 void _exception(long signr, struct pt_regs *regs, int code,
72                 unsigned long addr)
73 {
74         siginfo_t info;
75
76         if (!user_mode(regs))
77                 die("Unhandled exception in kernel mode", regs, signr);
78
79         memset(&info, 0, sizeof(info));
80         info.si_signo = signr;
81         info.si_code = code;
82         info.si_addr = (void __user *)addr;
83         force_sig_info(signr, &info, current);
84
85         /*
86          * Init gets no signals that it doesn't have a handler for.
87          * That's all very well, but if it has caused a synchronous
88          * exception and we ignore the resulting signal, it will just
89          * generate the same exception over and over again and we get
90          * nowhere.  Better to kill it and let the kernel panic.
91          */
92         if (is_global_init(current)) {
93                 __sighandler_t handler;
94
95                 spin_lock_irq(&current->sighand->siglock);
96                 handler = current->sighand->action[signr-1].sa.sa_handler;
97                 spin_unlock_irq(&current->sighand->siglock);
98                 if (handler == SIG_DFL) {
99                         /* init has generated a synchronous exception
100                            and it doesn't have a handler for the signal */
101                         printk(KERN_CRIT "init has generated signal %ld "
102                                "but has no handler for it\n", signr);
103                         do_exit(signr);
104                 }
105         }
106 }
107
108 asmlinkage void do_nmi(unsigned long ecr, struct pt_regs *regs)
109 {
110         printk(KERN_ALERT "Got Non-Maskable Interrupt, dumping regs\n");
111         show_regs_log_lvl(regs, KERN_ALERT);
112         show_stack_log_lvl(current, regs->sp, regs, KERN_ALERT);
113 }
114
115 asmlinkage void do_critical_exception(unsigned long ecr, struct pt_regs *regs)
116 {
117         die("Critical exception", regs, SIGKILL);
118 }
119
120 asmlinkage void do_address_exception(unsigned long ecr, struct pt_regs *regs)
121 {
122         _exception(SIGBUS, regs, BUS_ADRALN, regs->pc);
123 }
124
125 /* This way of handling undefined instructions is stolen from ARM */
126 static LIST_HEAD(undef_hook);
127 static DEFINE_SPINLOCK(undef_lock);
128
129 void register_undef_hook(struct undef_hook *hook)
130 {
131         spin_lock_irq(&undef_lock);
132         list_add(&hook->node, &undef_hook);
133         spin_unlock_irq(&undef_lock);
134 }
135
136 void unregister_undef_hook(struct undef_hook *hook)
137 {
138         spin_lock_irq(&undef_lock);
139         list_del(&hook->node);
140         spin_unlock_irq(&undef_lock);
141 }
142
143 static int do_cop_absent(u32 insn)
144 {
145         int cop_nr;
146         u32 cpucr;
147
148         if ((insn & 0xfdf00000) == 0xf1900000)
149                 /* LDC0 */
150                 cop_nr = 0;
151         else
152                 cop_nr = (insn >> 13) & 0x7;
153
154         /* Try enabling the coprocessor */
155         cpucr = sysreg_read(CPUCR);
156         cpucr |= (1 << (24 + cop_nr));
157         sysreg_write(CPUCR, cpucr);
158
159         cpucr = sysreg_read(CPUCR);
160         if (!(cpucr & (1 << (24 + cop_nr))))
161                 return -ENODEV;
162
163         return 0;
164 }
165
166 int is_valid_bugaddr(unsigned long pc)
167 {
168         unsigned short opcode;
169
170         if (pc < PAGE_OFFSET)
171                 return 0;
172         if (probe_kernel_address((u16 *)pc, opcode))
173                 return 0;
174
175         return opcode == AVR32_BUG_OPCODE;
176 }
177
178 asmlinkage void do_illegal_opcode(unsigned long ecr, struct pt_regs *regs)
179 {
180         u32 insn;
181         struct undef_hook *hook;
182         void __user *pc;
183         long code;
184
185         if (!user_mode(regs) && (ecr == ECR_ILLEGAL_OPCODE)) {
186                 enum bug_trap_type type;
187
188                 type = report_bug(regs->pc, regs);
189                 switch (type) {
190                 case BUG_TRAP_TYPE_NONE:
191                         break;
192                 case BUG_TRAP_TYPE_WARN:
193                         regs->pc += 2;
194                         return;
195                 case BUG_TRAP_TYPE_BUG:
196                         die("Kernel BUG", regs, SIGKILL);
197                 }
198         }
199
200         local_irq_enable();
201
202         if (user_mode(regs)) {
203                 pc = (void __user *)instruction_pointer(regs);
204                 if (get_user(insn, (u32 __user *)pc))
205                         goto invalid_area;
206
207                 if (ecr == ECR_COPROC_ABSENT && !do_cop_absent(insn))
208                         return;
209
210                 spin_lock_irq(&undef_lock);
211                 list_for_each_entry(hook, &undef_hook, node) {
212                         if ((insn & hook->insn_mask) == hook->insn_val) {
213                                 if (hook->fn(regs, insn) == 0) {
214                                         spin_unlock_irq(&undef_lock);
215                                         return;
216                                 }
217                         }
218                 }
219                 spin_unlock_irq(&undef_lock);
220         }
221
222         switch (ecr) {
223         case ECR_PRIVILEGE_VIOLATION:
224                 code = ILL_PRVOPC;
225                 break;
226         case ECR_COPROC_ABSENT:
227                 code = ILL_COPROC;
228                 break;
229         default:
230                 code = ILL_ILLOPC;
231                 break;
232         }
233
234         _exception(SIGILL, regs, code, regs->pc);
235         return;
236
237 invalid_area:
238         _exception(SIGSEGV, regs, SEGV_MAPERR, regs->pc);
239 }
240
241 asmlinkage void do_fpe(unsigned long ecr, struct pt_regs *regs)
242 {
243         /* We have no FPU yet */
244         _exception(SIGILL, regs, ILL_COPROC, regs->pc);
245 }
246
247
248 void __init trap_init(void)
249 {
250
251 }