]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/avr32/kernel/traps.c
[AVR32] Clean up exception handling code
[mirror_ubuntu-artful-kernel.git] / arch / avr32 / kernel / traps.c
CommitLineData
5f97f7f9
HS
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 */
623b0355
HS
8
9#include <linux/bug.h>
5f97f7f9 10#include <linux/init.h>
5f97f7f9 11#include <linux/kallsyms.h>
623b0355 12#include <linux/module.h>
5f97f7f9 13#include <linux/notifier.h>
623b0355
HS
14#include <linux/sched.h>
15#include <linux/uaccess.h>
5f97f7f9 16
5f97f7f9 17#include <asm/addrspace.h>
5f97f7f9 18#include <asm/mmu_context.h>
623b0355
HS
19#include <asm/ocd.h>
20#include <asm/sysreg.h>
21#include <asm/traps.h>
5f97f7f9
HS
22
23ATOMIC_NOTIFIER_HEAD(avr32_die_chain);
24
25int register_die_notifier(struct notifier_block *nb)
26{
5f97f7f9
HS
27 return atomic_notifier_chain_register(&avr32_die_chain, nb);
28}
29EXPORT_SYMBOL(register_die_notifier);
30
31int unregister_die_notifier(struct notifier_block *nb)
32{
33 return atomic_notifier_chain_unregister(&avr32_die_chain, nb);
34}
35EXPORT_SYMBOL(unregister_die_notifier);
36
37static DEFINE_SPINLOCK(die_lock);
38
623b0355 39void NORET_TYPE die(const char *str, struct pt_regs *regs, long err)
5f97f7f9 40{
5f97f7f9
HS
41 static int die_counter;
42
43 console_verbose();
44 spin_lock_irq(&die_lock);
45 bust_spinlocks(1);
46
623b0355
HS
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);
5f97f7f9
HS
67 }
68
623b0355
HS
69 print_modules();
70 show_regs_log_lvl(regs, KERN_EMERG);
71 show_stack_log_lvl(current, regs->sp, regs, KERN_EMERG);
5f97f7f9
HS
72 bust_spinlocks(0);
73 spin_unlock_irq(&die_lock);
623b0355
HS
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);
5f97f7f9
HS
82}
83
623b0355
HS
84void _exception(long signr, struct pt_regs *regs, int code,
85 unsigned long addr)
5f97f7f9 86{
623b0355
HS
87 siginfo_t info;
88
5f97f7f9 89 if (!user_mode(regs))
623b0355
HS
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);
5f97f7f9 97
5f97f7f9 98 /*
623b0355
HS
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.
5f97f7f9 104 */
623b0355
HS
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}
5f97f7f9 120
623b0355
HS
121asmlinkage 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);
5f97f7f9
HS
126}
127
128asmlinkage void do_critical_exception(unsigned long ecr, struct pt_regs *regs)
129{
623b0355 130 die("Critical exception", regs, SIGKILL);
5f97f7f9
HS
131}
132
133asmlinkage void do_address_exception(unsigned long ecr, struct pt_regs *regs)
134{
623b0355 135 _exception(SIGBUS, regs, BUS_ADRALN, regs->pc);
5f97f7f9
HS
136}
137
138/* This way of handling undefined instructions is stolen from ARM */
139static LIST_HEAD(undef_hook);
140static spinlock_t undef_lock = SPIN_LOCK_UNLOCKED;
141
142void 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
149void 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
156static int do_cop_absent(u32 insn)
157{
158 int cop_nr;
159 u32 cpucr;
623b0355
HS
160
161 if ((insn & 0xfdf00000) == 0xf1900000)
5f97f7f9
HS
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);
623b0355
HS
173 if (!(cpucr & (1 << (24 + cop_nr))))
174 return -ENODEV;
5f97f7f9
HS
175
176 return 0;
177}
178
623b0355 179int is_valid_bugaddr(unsigned long pc)
5f97f7f9 180{
623b0355
HS
181 unsigned short opcode;
182
183 if (pc < PAGE_OFFSET)
184 return 0;
185 if (probe_kernel_address((u16 *)pc, opcode))
186 return 0;
5f97f7f9 187
623b0355 188 return opcode == AVR32_BUG_OPCODE;
5f97f7f9 189}
5f97f7f9
HS
190
191asmlinkage void do_illegal_opcode(unsigned long ecr, struct pt_regs *regs)
192{
193 u32 insn;
194 struct undef_hook *hook;
5f97f7f9 195 void __user *pc;
623b0355 196 long code;
5f97f7f9 197
623b0355
HS
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 }
5f97f7f9
HS
212
213 local_irq_enable();
214
623b0355
HS
215 if (user_mode(regs)) {
216 pc = (void __user *)instruction_pointer(regs);
217 if (get_user(insn, (u32 __user *)pc))
218 goto invalid_area;
5f97f7f9 219
623b0355 220 if (ecr == ECR_COPROC_ABSENT && !do_cop_absent(insn))
5f97f7f9 221 return;
5f97f7f9 222
623b0355
HS
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 }
5f97f7f9
HS
230 }
231 }
623b0355 232 spin_unlock_irq(&undef_lock);
5f97f7f9 233 }
5f97f7f9 234
5f97f7f9 235 switch (ecr) {
5f97f7f9 236 case ECR_PRIVILEGE_VIOLATION:
623b0355 237 code = ILL_PRVOPC;
5f97f7f9
HS
238 break;
239 case ECR_COPROC_ABSENT:
623b0355 240 code = ILL_COPROC;
5f97f7f9
HS
241 break;
242 default:
623b0355
HS
243 code = ILL_ILLOPC;
244 break;
5f97f7f9
HS
245 }
246
623b0355 247 _exception(SIGILL, regs, code, regs->pc);
5f97f7f9
HS
248 return;
249
623b0355
HS
250invalid_area:
251 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->pc);
5f97f7f9
HS
252}
253
254asmlinkage void do_fpe(unsigned long ecr, struct pt_regs *regs)
255{
623b0355
HS
256 /* We have no FPU yet */
257 _exception(SIGILL, regs, ILL_COPROC, regs->pc);
5f97f7f9
HS
258}
259
260
261void __init trap_init(void)
262{
263
264}