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