d547c8df157da7f31c5459e2fd4f2395faf1bc34
[sfrench/cifs-2.6.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                 die("Unhandled exception in kernel mode", regs, signr);
80
81         memset(&info, 0, sizeof(info));
82         info.si_signo = signr;
83         info.si_code = code;
84         info.si_addr = (void __user *)addr;
85         force_sig_info(signr, &info, current);
86
87         /*
88          * Init gets no signals that it doesn't have a handler for.
89          * That's all very well, but if it has caused a synchronous
90          * exception and we ignore the resulting signal, it will just
91          * generate the same exception over and over again and we get
92          * nowhere.  Better to kill it and let the kernel panic.
93          */
94         if (is_global_init(current)) {
95                 __sighandler_t handler;
96
97                 spin_lock_irq(&current->sighand->siglock);
98                 handler = current->sighand->action[signr-1].sa.sa_handler;
99                 spin_unlock_irq(&current->sighand->siglock);
100                 if (handler == SIG_DFL) {
101                         /* init has generated a synchronous exception
102                            and it doesn't have a handler for the signal */
103                         printk(KERN_CRIT "init has generated signal %ld "
104                                "but has no handler for it\n", signr);
105                         do_exit(signr);
106                 }
107         }
108 }
109
110 asmlinkage void do_nmi(unsigned long ecr, struct pt_regs *regs)
111 {
112         int ret;
113
114         nmi_enter();
115
116         ret = notify_die(DIE_NMI, "NMI", regs, 0, ecr, SIGINT);
117         switch (ret) {
118         case NOTIFY_OK:
119         case NOTIFY_STOP:
120                 break;
121         case NOTIFY_BAD:
122                 die("Fatal Non-Maskable Interrupt", regs, SIGINT);
123         default:
124                 printk(KERN_ALERT "Got NMI, but nobody cared. Disabling...\n");
125                 nmi_disable();
126                 break;
127         }
128         nmi_exit();
129 }
130
131 asmlinkage void do_critical_exception(unsigned long ecr, struct pt_regs *regs)
132 {
133         die("Critical exception", regs, SIGKILL);
134 }
135
136 asmlinkage void do_address_exception(unsigned long ecr, struct pt_regs *regs)
137 {
138         _exception(SIGBUS, regs, BUS_ADRALN, regs->pc);
139 }
140
141 /* This way of handling undefined instructions is stolen from ARM */
142 static LIST_HEAD(undef_hook);
143 static DEFINE_SPINLOCK(undef_lock);
144
145 void register_undef_hook(struct undef_hook *hook)
146 {
147         spin_lock_irq(&undef_lock);
148         list_add(&hook->node, &undef_hook);
149         spin_unlock_irq(&undef_lock);
150 }
151
152 void unregister_undef_hook(struct undef_hook *hook)
153 {
154         spin_lock_irq(&undef_lock);
155         list_del(&hook->node);
156         spin_unlock_irq(&undef_lock);
157 }
158
159 static int do_cop_absent(u32 insn)
160 {
161         int cop_nr;
162         u32 cpucr;
163
164         if ((insn & 0xfdf00000) == 0xf1900000)
165                 /* LDC0 */
166                 cop_nr = 0;
167         else
168                 cop_nr = (insn >> 13) & 0x7;
169
170         /* Try enabling the coprocessor */
171         cpucr = sysreg_read(CPUCR);
172         cpucr |= (1 << (24 + cop_nr));
173         sysreg_write(CPUCR, cpucr);
174
175         cpucr = sysreg_read(CPUCR);
176         if (!(cpucr & (1 << (24 + cop_nr))))
177                 return -ENODEV;
178
179         return 0;
180 }
181
182 #ifdef CONFIG_BUG
183 int is_valid_bugaddr(unsigned long pc)
184 {
185         unsigned short opcode;
186
187         if (pc < PAGE_OFFSET)
188                 return 0;
189         if (probe_kernel_address((u16 *)pc, opcode))
190                 return 0;
191
192         return opcode == AVR32_BUG_OPCODE;
193 }
194 #endif
195
196 asmlinkage void do_illegal_opcode(unsigned long ecr, struct pt_regs *regs)
197 {
198         u32 insn;
199         struct undef_hook *hook;
200         void __user *pc;
201         long code;
202
203 #ifdef CONFIG_BUG
204         if (!user_mode(regs) && (ecr == ECR_ILLEGAL_OPCODE)) {
205                 enum bug_trap_type type;
206
207                 type = report_bug(regs->pc, regs);
208                 switch (type) {
209                 case BUG_TRAP_TYPE_NONE:
210                         break;
211                 case BUG_TRAP_TYPE_WARN:
212                         regs->pc += 2;
213                         return;
214                 case BUG_TRAP_TYPE_BUG:
215                         die("Kernel BUG", regs, SIGKILL);
216                 }
217         }
218 #endif
219
220         local_irq_enable();
221
222         if (user_mode(regs)) {
223                 pc = (void __user *)instruction_pointer(regs);
224                 if (get_user(insn, (u32 __user *)pc))
225                         goto invalid_area;
226
227                 if (ecr == ECR_COPROC_ABSENT && !do_cop_absent(insn))
228                         return;
229
230                 spin_lock_irq(&undef_lock);
231                 list_for_each_entry(hook, &undef_hook, node) {
232                         if ((insn & hook->insn_mask) == hook->insn_val) {
233                                 if (hook->fn(regs, insn) == 0) {
234                                         spin_unlock_irq(&undef_lock);
235                                         return;
236                                 }
237                         }
238                 }
239                 spin_unlock_irq(&undef_lock);
240         }
241
242         switch (ecr) {
243         case ECR_PRIVILEGE_VIOLATION:
244                 code = ILL_PRVOPC;
245                 break;
246         case ECR_COPROC_ABSENT:
247                 code = ILL_COPROC;
248                 break;
249         default:
250                 code = ILL_ILLOPC;
251                 break;
252         }
253
254         _exception(SIGILL, regs, code, regs->pc);
255         return;
256
257 invalid_area:
258         _exception(SIGSEGV, regs, SEGV_MAPERR, regs->pc);
259 }
260
261 asmlinkage void do_fpe(unsigned long ecr, struct pt_regs *regs)
262 {
263         /* We have no FPU yet */
264         _exception(SIGILL, regs, ILL_COPROC, regs->pc);
265 }
266
267
268 void __init trap_init(void)
269 {
270
271 }