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