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