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/extable.h>
15 #include <linux/module.h> /* print_modules */
16 #include <linux/notifier.h>
17 #include <linux/sched.h>
18 #include <linux/uaccess.h>
20 #include <asm/addrspace.h>
21 #include <asm/mmu_context.h>
23 #include <asm/sysreg.h>
24 #include <asm/traps.h>
26 static DEFINE_SPINLOCK(die_lock);
28 void die(const char *str, struct pt_regs *regs, long err)
30 static int die_counter;
33 spin_lock_irq(&die_lock);
36 printk(KERN_ALERT "Oops: %s, sig: %ld [#%d]\n",
37 str, err, ++die_counter);
42 printk(KERN_CONT "PREEMPT ");
44 #ifdef CONFIG_FRAME_POINTER
45 printk(KERN_CONT "FRAME_POINTER ");
47 if (current_cpu_data.features & AVR32_FEATURE_OCD) {
48 unsigned long did = ocd_read(DID);
49 printk(KERN_CONT "chip: 0x%03lx:0x%04lx rev %lu\n",
54 printk(KERN_CONT "cpu: arch %u r%u / core %u r%u\n",
55 current_cpu_data.arch_type,
56 current_cpu_data.arch_revision,
57 current_cpu_data.cpu_type,
58 current_cpu_data.cpu_revision);
62 show_regs_log_lvl(regs, KERN_EMERG);
63 show_stack_log_lvl(current, regs->sp, regs, KERN_EMERG);
65 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
66 spin_unlock_irq(&die_lock);
69 panic("Fatal exception in interrupt");
72 panic("Fatal exception");
77 void _exception(long signr, struct pt_regs *regs, int code,
82 if (!user_mode(regs)) {
83 const struct exception_table_entry *fixup;
85 /* Are we prepared to handle this kernel fault? */
86 fixup = search_exception_tables(regs->pc);
88 regs->pc = fixup->fixup;
91 die("Unhandled exception in kernel mode", regs, signr);
94 memset(&info, 0, sizeof(info));
95 info.si_signo = signr;
97 info.si_addr = (void __user *)addr;
98 force_sig_info(signr, &info, current);
101 asmlinkage void do_nmi(unsigned long ecr, struct pt_regs *regs)
107 ret = notify_die(DIE_NMI, "NMI", regs, 0, ecr, SIGINT);
113 die("Fatal Non-Maskable Interrupt", regs, SIGINT);
115 printk(KERN_ALERT "Got NMI, but nobody cared. Disabling...\n");
122 asmlinkage void do_critical_exception(unsigned long ecr, struct pt_regs *regs)
124 die("Critical exception", regs, SIGKILL);
127 asmlinkage void do_address_exception(unsigned long ecr, struct pt_regs *regs)
129 _exception(SIGBUS, regs, BUS_ADRALN, regs->pc);
132 /* This way of handling undefined instructions is stolen from ARM */
133 static LIST_HEAD(undef_hook);
134 static DEFINE_SPINLOCK(undef_lock);
136 void register_undef_hook(struct undef_hook *hook)
138 spin_lock_irq(&undef_lock);
139 list_add(&hook->node, &undef_hook);
140 spin_unlock_irq(&undef_lock);
143 void unregister_undef_hook(struct undef_hook *hook)
145 spin_lock_irq(&undef_lock);
146 list_del(&hook->node);
147 spin_unlock_irq(&undef_lock);
150 static int do_cop_absent(u32 insn)
155 if ((insn & 0xfdf00000) == 0xf1900000)
159 cop_nr = (insn >> 13) & 0x7;
161 /* Try enabling the coprocessor */
162 cpucr = sysreg_read(CPUCR);
163 cpucr |= (1 << (24 + cop_nr));
164 sysreg_write(CPUCR, cpucr);
166 cpucr = sysreg_read(CPUCR);
167 if (!(cpucr & (1 << (24 + cop_nr))))
174 int is_valid_bugaddr(unsigned long pc)
176 unsigned short opcode;
178 if (pc < PAGE_OFFSET)
180 if (probe_kernel_address((u16 *)pc, opcode))
183 return opcode == AVR32_BUG_OPCODE;
187 asmlinkage void do_illegal_opcode(unsigned long ecr, struct pt_regs *regs)
190 struct undef_hook *hook;
195 if (!user_mode(regs) && (ecr == ECR_ILLEGAL_OPCODE)) {
196 enum bug_trap_type type;
198 type = report_bug(regs->pc, regs);
200 case BUG_TRAP_TYPE_NONE:
202 case BUG_TRAP_TYPE_WARN:
205 case BUG_TRAP_TYPE_BUG:
206 die("Kernel BUG", regs, SIGKILL);
213 if (user_mode(regs)) {
214 pc = (void __user *)instruction_pointer(regs);
215 if (get_user(insn, (u32 __user *)pc))
218 if (ecr == ECR_COPROC_ABSENT && !do_cop_absent(insn))
221 spin_lock_irq(&undef_lock);
222 list_for_each_entry(hook, &undef_hook, node) {
223 if ((insn & hook->insn_mask) == hook->insn_val) {
224 if (hook->fn(regs, insn) == 0) {
225 spin_unlock_irq(&undef_lock);
230 spin_unlock_irq(&undef_lock);
234 case ECR_PRIVILEGE_VIOLATION:
237 case ECR_COPROC_ABSENT:
245 _exception(SIGILL, regs, code, regs->pc);
249 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->pc);
252 asmlinkage void do_fpe(unsigned long ecr, struct pt_regs *regs)
254 /* We have no FPU yet */
255 _exception(SIGILL, regs, ILL_COPROC, regs->pc);
259 void __init trap_init(void)