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/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>
19 #include <asm/addrspace.h>
20 #include <asm/mmu_context.h>
22 #include <asm/sysreg.h>
23 #include <asm/traps.h>
25 static DEFINE_SPINLOCK(die_lock);
27 void NORET_TYPE die(const char *str, struct pt_regs *regs, long err)
29 static int die_counter;
32 spin_lock_irq(&die_lock);
35 printk(KERN_ALERT "Oops: %s, sig: %ld [#%d]\n" KERN_EMERG,
36 str, err, ++die_counter);
40 #ifdef CONFIG_FRAME_POINTER
41 printk("FRAME_POINTER ");
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",
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);
58 show_regs_log_lvl(regs, KERN_EMERG);
59 show_stack_log_lvl(current, regs->sp, regs, KERN_EMERG);
62 spin_unlock_irq(&die_lock);
65 panic("Fatal exception in interrupt");
68 panic("Fatal exception");
73 void _exception(long signr, struct pt_regs *regs, int code,
78 if (!user_mode(regs)) {
79 const struct exception_table_entry *fixup;
81 /* Are we prepared to handle this kernel fault? */
82 fixup = search_exception_tables(regs->pc);
84 regs->pc = fixup->fixup;
87 die("Unhandled exception in kernel mode", regs, signr);
90 memset(&info, 0, sizeof(info));
91 info.si_signo = signr;
93 info.si_addr = (void __user *)addr;
94 force_sig_info(signr, &info, current);
97 * Init gets no signals that it doesn't have a handler for.
98 * That's all very well, but if it has caused a synchronous
99 * exception and we ignore the resulting signal, it will just
100 * generate the same exception over and over again and we get
101 * nowhere. Better to kill it and let the kernel panic.
103 if (is_global_init(current)) {
104 __sighandler_t handler;
106 spin_lock_irq(¤t->sighand->siglock);
107 handler = current->sighand->action[signr-1].sa.sa_handler;
108 spin_unlock_irq(¤t->sighand->siglock);
109 if (handler == SIG_DFL) {
110 /* init has generated a synchronous exception
111 and it doesn't have a handler for the signal */
112 printk(KERN_CRIT "init has generated signal %ld "
113 "but has no handler for it\n", signr);
119 asmlinkage void do_nmi(unsigned long ecr, struct pt_regs *regs)
125 ret = notify_die(DIE_NMI, "NMI", regs, 0, ecr, SIGINT);
131 die("Fatal Non-Maskable Interrupt", regs, SIGINT);
133 printk(KERN_ALERT "Got NMI, but nobody cared. Disabling...\n");
140 asmlinkage void do_critical_exception(unsigned long ecr, struct pt_regs *regs)
142 die("Critical exception", regs, SIGKILL);
145 asmlinkage void do_address_exception(unsigned long ecr, struct pt_regs *regs)
147 _exception(SIGBUS, regs, BUS_ADRALN, regs->pc);
150 /* This way of handling undefined instructions is stolen from ARM */
151 static LIST_HEAD(undef_hook);
152 static DEFINE_SPINLOCK(undef_lock);
154 void register_undef_hook(struct undef_hook *hook)
156 spin_lock_irq(&undef_lock);
157 list_add(&hook->node, &undef_hook);
158 spin_unlock_irq(&undef_lock);
161 void unregister_undef_hook(struct undef_hook *hook)
163 spin_lock_irq(&undef_lock);
164 list_del(&hook->node);
165 spin_unlock_irq(&undef_lock);
168 static int do_cop_absent(u32 insn)
173 if ((insn & 0xfdf00000) == 0xf1900000)
177 cop_nr = (insn >> 13) & 0x7;
179 /* Try enabling the coprocessor */
180 cpucr = sysreg_read(CPUCR);
181 cpucr |= (1 << (24 + cop_nr));
182 sysreg_write(CPUCR, cpucr);
184 cpucr = sysreg_read(CPUCR);
185 if (!(cpucr & (1 << (24 + cop_nr))))
192 int is_valid_bugaddr(unsigned long pc)
194 unsigned short opcode;
196 if (pc < PAGE_OFFSET)
198 if (probe_kernel_address((u16 *)pc, opcode))
201 return opcode == AVR32_BUG_OPCODE;
205 asmlinkage void do_illegal_opcode(unsigned long ecr, struct pt_regs *regs)
208 struct undef_hook *hook;
213 if (!user_mode(regs) && (ecr == ECR_ILLEGAL_OPCODE)) {
214 enum bug_trap_type type;
216 type = report_bug(regs->pc, regs);
218 case BUG_TRAP_TYPE_NONE:
220 case BUG_TRAP_TYPE_WARN:
223 case BUG_TRAP_TYPE_BUG:
224 die("Kernel BUG", regs, SIGKILL);
231 if (user_mode(regs)) {
232 pc = (void __user *)instruction_pointer(regs);
233 if (get_user(insn, (u32 __user *)pc))
236 if (ecr == ECR_COPROC_ABSENT && !do_cop_absent(insn))
239 spin_lock_irq(&undef_lock);
240 list_for_each_entry(hook, &undef_hook, node) {
241 if ((insn & hook->insn_mask) == hook->insn_val) {
242 if (hook->fn(regs, insn) == 0) {
243 spin_unlock_irq(&undef_lock);
248 spin_unlock_irq(&undef_lock);
252 case ECR_PRIVILEGE_VIOLATION:
255 case ECR_COPROC_ABSENT:
263 _exception(SIGILL, regs, code, regs->pc);
267 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->pc);
270 asmlinkage void do_fpe(unsigned long ecr, struct pt_regs *regs)
272 /* We have no FPU yet */
273 _exception(SIGILL, regs, ILL_COPROC, regs->pc);
277 void __init trap_init(void)