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 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",
36 str, err, ++die_counter);
41 printk(KERN_CONT "PREEMPT ");
43 #ifdef CONFIG_FRAME_POINTER
44 printk(KERN_CONT "FRAME_POINTER ");
46 if (current_cpu_data.features & AVR32_FEATURE_OCD) {
47 unsigned long did = ocd_read(DID);
48 printk(KERN_CONT "chip: 0x%03lx:0x%04lx rev %lu\n",
53 printk(KERN_CONT "cpu: arch %u r%u / core %u r%u\n",
54 current_cpu_data.arch_type,
55 current_cpu_data.arch_revision,
56 current_cpu_data.cpu_type,
57 current_cpu_data.cpu_revision);
61 show_regs_log_lvl(regs, KERN_EMERG);
62 show_stack_log_lvl(current, regs->sp, regs, KERN_EMERG);
65 spin_unlock_irq(&die_lock);
68 panic("Fatal exception in interrupt");
71 panic("Fatal exception");
76 void _exception(long signr, struct pt_regs *regs, int code,
81 if (!user_mode(regs)) {
82 const struct exception_table_entry *fixup;
84 /* Are we prepared to handle this kernel fault? */
85 fixup = search_exception_tables(regs->pc);
87 regs->pc = fixup->fixup;
90 die("Unhandled exception in kernel mode", regs, signr);
93 memset(&info, 0, sizeof(info));
94 info.si_signo = signr;
96 info.si_addr = (void __user *)addr;
97 force_sig_info(signr, &info, current);
100 asmlinkage void do_nmi(unsigned long ecr, struct pt_regs *regs)
106 ret = notify_die(DIE_NMI, "NMI", regs, 0, ecr, SIGINT);
112 die("Fatal Non-Maskable Interrupt", regs, SIGINT);
114 printk(KERN_ALERT "Got NMI, but nobody cared. Disabling...\n");
121 asmlinkage void do_critical_exception(unsigned long ecr, struct pt_regs *regs)
123 die("Critical exception", regs, SIGKILL);
126 asmlinkage void do_address_exception(unsigned long ecr, struct pt_regs *regs)
128 _exception(SIGBUS, regs, BUS_ADRALN, regs->pc);
131 /* This way of handling undefined instructions is stolen from ARM */
132 static LIST_HEAD(undef_hook);
133 static DEFINE_SPINLOCK(undef_lock);
135 void register_undef_hook(struct undef_hook *hook)
137 spin_lock_irq(&undef_lock);
138 list_add(&hook->node, &undef_hook);
139 spin_unlock_irq(&undef_lock);
142 void unregister_undef_hook(struct undef_hook *hook)
144 spin_lock_irq(&undef_lock);
145 list_del(&hook->node);
146 spin_unlock_irq(&undef_lock);
149 static int do_cop_absent(u32 insn)
154 if ((insn & 0xfdf00000) == 0xf1900000)
158 cop_nr = (insn >> 13) & 0x7;
160 /* Try enabling the coprocessor */
161 cpucr = sysreg_read(CPUCR);
162 cpucr |= (1 << (24 + cop_nr));
163 sysreg_write(CPUCR, cpucr);
165 cpucr = sysreg_read(CPUCR);
166 if (!(cpucr & (1 << (24 + cop_nr))))
173 int is_valid_bugaddr(unsigned long pc)
175 unsigned short opcode;
177 if (pc < PAGE_OFFSET)
179 if (probe_kernel_address((u16 *)pc, opcode))
182 return opcode == AVR32_BUG_OPCODE;
186 asmlinkage void do_illegal_opcode(unsigned long ecr, struct pt_regs *regs)
189 struct undef_hook *hook;
194 if (!user_mode(regs) && (ecr == ECR_ILLEGAL_OPCODE)) {
195 enum bug_trap_type type;
197 type = report_bug(regs->pc, regs);
199 case BUG_TRAP_TYPE_NONE:
201 case BUG_TRAP_TYPE_WARN:
204 case BUG_TRAP_TYPE_BUG:
205 die("Kernel BUG", regs, SIGKILL);
212 if (user_mode(regs)) {
213 pc = (void __user *)instruction_pointer(regs);
214 if (get_user(insn, (u32 __user *)pc))
217 if (ecr == ECR_COPROC_ABSENT && !do_cop_absent(insn))
220 spin_lock_irq(&undef_lock);
221 list_for_each_entry(hook, &undef_hook, node) {
222 if ((insn & hook->insn_mask) == hook->insn_val) {
223 if (hook->fn(regs, insn) == 0) {
224 spin_unlock_irq(&undef_lock);
229 spin_unlock_irq(&undef_lock);
233 case ECR_PRIVILEGE_VIOLATION:
236 case ECR_COPROC_ABSENT:
244 _exception(SIGILL, regs, code, regs->pc);
248 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->pc);
251 asmlinkage void do_fpe(unsigned long ecr, struct pt_regs *regs)
253 /* We have no FPU yet */
254 _exception(SIGILL, regs, ILL_COPROC, regs->pc);
258 void __init trap_init(void)