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