2 * Copyright (C) 2004-2006 Atmel Corporation
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.
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>
17 #include <asm/addrspace.h>
18 #include <asm/mmu_context.h>
20 #include <asm/sysreg.h>
21 #include <asm/traps.h>
23 ATOMIC_NOTIFIER_HEAD(avr32_die_chain);
25 int register_die_notifier(struct notifier_block *nb)
27 return atomic_notifier_chain_register(&avr32_die_chain, nb);
29 EXPORT_SYMBOL(register_die_notifier);
31 int unregister_die_notifier(struct notifier_block *nb)
33 return atomic_notifier_chain_unregister(&avr32_die_chain, nb);
35 EXPORT_SYMBOL(unregister_die_notifier);
37 static DEFINE_SPINLOCK(die_lock);
39 void NORET_TYPE die(const char *str, struct pt_regs *regs, long err)
41 static int die_counter;
44 spin_lock_irq(&die_lock);
47 printk(KERN_ALERT "Oops: %s, sig: %ld [#%d]\n" KERN_EMERG,
48 str, err, ++die_counter);
52 #ifdef CONFIG_FRAME_POINTER
53 printk("FRAME_POINTER ");
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",
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);
70 show_regs_log_lvl(regs, KERN_EMERG);
71 show_stack_log_lvl(current, regs->sp, regs, KERN_EMERG);
73 spin_unlock_irq(&die_lock);
76 panic("Fatal exception in interrupt");
79 panic("Fatal exception");
84 void _exception(long signr, struct pt_regs *regs, int code,
90 die("Unhandled exception in kernel mode", regs, signr);
92 memset(&info, 0, sizeof(info));
93 info.si_signo = signr;
95 info.si_addr = (void __user *)addr;
96 force_sig_info(signr, &info, current);
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.
105 if (is_init(current)) {
106 __sighandler_t handler;
108 spin_lock_irq(¤t->sighand->siglock);
109 handler = current->sighand->action[signr-1].sa.sa_handler;
110 spin_unlock_irq(¤t->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);
121 asmlinkage void do_nmi(unsigned long ecr, struct pt_regs *regs)
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);
128 asmlinkage void do_critical_exception(unsigned long ecr, struct pt_regs *regs)
130 die("Critical exception", regs, SIGKILL);
133 asmlinkage void do_address_exception(unsigned long ecr, struct pt_regs *regs)
135 _exception(SIGBUS, regs, BUS_ADRALN, regs->pc);
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;
142 void register_undef_hook(struct undef_hook *hook)
144 spin_lock_irq(&undef_lock);
145 list_add(&hook->node, &undef_hook);
146 spin_unlock_irq(&undef_lock);
149 void unregister_undef_hook(struct undef_hook *hook)
151 spin_lock_irq(&undef_lock);
152 list_del(&hook->node);
153 spin_unlock_irq(&undef_lock);
156 static int do_cop_absent(u32 insn)
161 if ((insn & 0xfdf00000) == 0xf1900000)
165 cop_nr = (insn >> 13) & 0x7;
167 /* Try enabling the coprocessor */
168 cpucr = sysreg_read(CPUCR);
169 cpucr |= (1 << (24 + cop_nr));
170 sysreg_write(CPUCR, cpucr);
172 cpucr = sysreg_read(CPUCR);
173 if (!(cpucr & (1 << (24 + cop_nr))))
179 int is_valid_bugaddr(unsigned long pc)
181 unsigned short opcode;
183 if (pc < PAGE_OFFSET)
185 if (probe_kernel_address((u16 *)pc, opcode))
188 return opcode == AVR32_BUG_OPCODE;
191 asmlinkage void do_illegal_opcode(unsigned long ecr, struct pt_regs *regs)
194 struct undef_hook *hook;
198 if (!user_mode(regs) && (ecr == ECR_ILLEGAL_OPCODE)) {
199 enum bug_trap_type type;
201 type = report_bug(regs->pc);
203 case BUG_TRAP_TYPE_NONE:
205 case BUG_TRAP_TYPE_WARN:
208 case BUG_TRAP_TYPE_BUG:
209 die("Kernel BUG", regs, SIGKILL);
215 if (user_mode(regs)) {
216 pc = (void __user *)instruction_pointer(regs);
217 if (get_user(insn, (u32 __user *)pc))
220 if (ecr == ECR_COPROC_ABSENT && !do_cop_absent(insn))
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);
232 spin_unlock_irq(&undef_lock);
236 case ECR_PRIVILEGE_VIOLATION:
239 case ECR_COPROC_ABSENT:
247 _exception(SIGILL, regs, code, regs->pc);
251 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->pc);
254 asmlinkage void do_fpe(unsigned long ecr, struct pt_regs *regs)
256 /* We have no FPU yet */
257 _exception(SIGILL, regs, ILL_COPROC, regs->pc);
261 void __init trap_init(void)